xref: /freebsd/lib/libusb/libusb10.h (revision 61e21613)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2009 Sylvestre Gallon. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #ifndef __LIBUSB10_H__
29 #define	__LIBUSB10_H__
30 
31 #ifndef LIBUSB_GLOBAL_INCLUDE_FILE
32 #include <sys/queue.h>
33 #endif
34 
35 #define	GET_CONTEXT(ctx) (((ctx) == NULL) ? usbi_default_context : (ctx))
36 #define	UNEXPORTED __attribute__((__visibility__("hidden")))
37 #define	CTX_LOCK(ctx) pthread_mutex_lock(&(ctx)->ctx_lock)
38 #define	CTX_TRYLOCK(ctx) pthread_mutex_trylock(&(ctx)->ctx_lock)
39 #define	CTX_UNLOCK(ctx) pthread_mutex_unlock(&(ctx)->ctx_lock)
40 #define	HOTPLUG_LOCK(ctx) pthread_mutex_lock(&(ctx)->hotplug_lock)
41 #define	HOTPLUG_UNLOCK(ctx) pthread_mutex_unlock(&(ctx)->hotplug_lock)
42 
43 #define	DPRINTF(ctx, dbg, format, ...) do {			\
44 	switch (dbg) {						\
45 	case LIBUSB_DEBUG_FUNCTION:				\
46 		if ((ctx)->debug & LIBUSB_DEBUG_FUNCTION) {	\
47 			printf("LIBUSB_FUNCTION: "		\
48 			       format "\n", ## __VA_ARGS__);	\
49 		}						\
50 		break;						\
51 	case LIBUSB_DEBUG_TRANSFER:				\
52 		if ((ctx)->debug & LIBUSB_DEBUG_TRANSFER) { 	\
53 			printf("LIBUSB_TRANSFER: "		\
54 			       format "\n", ## __VA_ARGS__);	\
55 		}						\
56 		break;						\
57 	default:						\
58 		break;						\
59 	}							\
60 } while (0)
61 
62 /* internal structures */
63 
64 struct libusb_super_pollfd {
65 	TAILQ_ENTRY(libusb_super_pollfd) entry;
66 	struct libusb20_device *pdev;
67 	struct libusb_pollfd pollfd;
68 };
69 
70 struct libusb_super_transfer {
71 	TAILQ_ENTRY(libusb_super_transfer) entry;
72 	uint8_t *curr_data;
73 	uint32_t rem_len;
74 	uint32_t last_len;
75 	uint32_t stream_id;
76 	uint8_t	state;
77 #define	LIBUSB_SUPER_XFER_ST_NONE 0
78 #define	LIBUSB_SUPER_XFER_ST_PEND 1
79 };
80 
81 struct libusb_hotplug_callback_handle_struct {
82 	TAILQ_ENTRY(libusb_hotplug_callback_handle_struct) entry;
83 	int events;
84 	int vendor;
85 	int product;
86 	int devclass;
87 	libusb_hotplug_callback_fn fn;
88 	void *user_data;
89 };
90 
91 TAILQ_HEAD(libusb_device_head, libusb_device);
92 
93 struct libusb_context {
94 	int	debug;
95 	int	debug_fixed;
96 	int	ctrl_pipe[2];
97 	int	tr_done_ref;
98 	int	tr_done_gen;
99 
100 	pthread_mutex_t ctx_lock;
101   	pthread_mutex_t hotplug_lock;
102 	pthread_cond_t ctx_cond;
103 	pthread_t hotplug_handler;
104 	pthread_t ctx_handler;
105 #define	NO_THREAD ((pthread_t)-1)
106 
107 	TAILQ_HEAD(, libusb_super_pollfd) pollfds;
108 	TAILQ_HEAD(, libusb_super_transfer) tr_done;
109 	TAILQ_HEAD(, libusb_hotplug_callback_handle_struct) hotplug_cbh;
110 	struct libusb_device_head hotplug_devs;
111 
112 	struct libusb_super_pollfd ctx_poll;
113 
114 	libusb_pollfd_added_cb fd_added_cb;
115 	libusb_pollfd_removed_cb fd_removed_cb;
116 	void   *fd_cb_user_data;
117 };
118 
119 struct libusb_device {
120 	int	refcnt;
121 
122 	int	device_is_gone;
123 
124 	uint32_t claimed_interfaces;
125 
126 	struct libusb_super_pollfd dev_poll;
127 
128 	struct libusb_context *ctx;
129 
130 	TAILQ_ENTRY(libusb_device) hotplug_entry;
131 
132 	TAILQ_HEAD(, libusb_super_transfer) tr_head;
133 
134 	struct libusb20_device *os_priv;
135 };
136 
137 extern struct libusb_context *usbi_default_context;
138 
139 void	libusb10_add_pollfd(libusb_context *ctx, struct libusb_super_pollfd *pollfd, struct libusb20_device *pdev, int fd, short events);
140 void	libusb10_remove_pollfd(libusb_context *ctx, struct libusb_super_pollfd *pollfd);
141 void	libusb10_cancel_all_transfer(libusb_device *dev);
142 void	libusb10_cancel_all_transfer_locked(struct libusb20_device *pdev, struct libusb_device *dev);
143 
144 #endif					/* __LIBUSB10_H__ */
145