xref: /freebsd/lib/libusb/libusb10.h (revision 6bda9f26d2eda066285a55edd0e508b799b1a02a)
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/cdefs.h>
33 #include <sys/queue.h>
34 #include <netlink/netlink.h>
35 #include <netlink/netlink_generic.h>
36 #include <netlink/netlink_snl.h>
37 #include <netlink/netlink_snl_generic.h>
38 #include <netlink/netlink_sysevent.h>
39 
40 #endif
41 
42 #define	GET_CONTEXT(ctx) (((ctx) == NULL) ? usbi_default_context : (ctx))
43 #define	UNEXPORTED __attribute__((__visibility__("hidden")))
44 #define	CTX_LOCK(ctx) pthread_mutex_lock(&(ctx)->ctx_lock)
45 #define	CTX_TRYLOCK(ctx) pthread_mutex_trylock(&(ctx)->ctx_lock)
46 #define	CTX_UNLOCK(ctx) pthread_mutex_unlock(&(ctx)->ctx_lock)
47 #define	HOTPLUG_LOCK(ctx) pthread_mutex_lock(&(ctx)->hotplug_lock)
48 #define	HOTPLUG_UNLOCK(ctx) pthread_mutex_unlock(&(ctx)->hotplug_lock)
49 
50 void libusb_log_va_args(struct libusb_context *ctx, enum libusb_log_level level,
51     const char *fmt, ...) __printflike(3, 4);
52 
53 #define DPRINTF(ctx, dbg, format, ...) \
54 	libusb_log_va_args(ctx, dbg, format, ##__VA_ARGS__)
55 
56 /* internal structures */
57 
58 struct libusb_super_pollfd {
59 	TAILQ_ENTRY(libusb_super_pollfd) entry;
60 	struct libusb20_device *pdev;
61 	struct libusb_pollfd pollfd;
62 };
63 
64 struct libusb_super_transfer {
65 	TAILQ_ENTRY(libusb_super_transfer) entry;
66 	uint8_t *curr_data;
67 	uint32_t rem_len;
68 	uint32_t last_len;
69 	uint32_t stream_id;
70 	uint8_t	state;
71 #define	LIBUSB_SUPER_XFER_ST_NONE 0
72 #define	LIBUSB_SUPER_XFER_ST_PEND 1
73 };
74 
75 struct libusb_hotplug_callback_handle_struct {
76 	TAILQ_ENTRY(libusb_hotplug_callback_handle_struct) entry;
77 	int events;
78 	int vendor;
79 	int product;
80 	int devclass;
81 	libusb_hotplug_callback_fn fn;
82 	void *user_data;
83 	int id;
84 };
85 
86 TAILQ_HEAD(libusb_device_head, libusb_device);
87 
88 typedef enum {
89 	usb_event_none,
90 	usb_event_scan,
91 	usb_event_devd,
92 	usb_event_netlink
93 } usb_event_mode_t;
94 
95 struct libusb_context {
96 	int	debug;
97 	int	debug_fixed;
98 	int	event;
99 	int	tr_done_ref;
100 	int	tr_done_gen;
101 	usb_event_mode_t usb_event_mode;
102 	int	devd_pipe;
103 	struct	snl_state ss;
104 
105 	pthread_mutex_t ctx_lock;
106   	pthread_mutex_t hotplug_lock;
107 	pthread_cond_t ctx_cond;
108 	pthread_t hotplug_handler;
109 	pthread_t ctx_handler;
110 #define	NO_THREAD ((pthread_t)-1)
111 
112 	TAILQ_HEAD(, libusb_super_pollfd) pollfds;
113 	TAILQ_HEAD(, libusb_super_transfer) tr_done;
114 	TAILQ_HEAD(, libusb_hotplug_callback_handle_struct) hotplug_cbh;
115 	struct libusb_device_head hotplug_devs;
116 
117 	struct libusb_super_pollfd ctx_poll;
118 
119 	libusb_pollfd_added_cb fd_added_cb;
120 	libusb_pollfd_removed_cb fd_removed_cb;
121 	void   *fd_cb_user_data;
122 	libusb_log_cb log_cb;
123 	int no_discovery;
124 	int next_callback_id;
125 
126 	/* backend context holding the USB file descriptors */
127 	struct libusb20_be_ctx *be_ctx;
128 };
129 
130 struct libusb_device {
131 	int	refcnt;
132 
133 	int	device_is_gone;
134 
135 	uint32_t claimed_interfaces;
136 
137 	struct libusb_super_pollfd dev_poll;
138 
139 	struct libusb_context *ctx;
140 	struct libusb_device *parent_dev;
141 
142 	TAILQ_ENTRY(libusb_device) hotplug_entry;
143 
144 	TAILQ_HEAD(, libusb_super_transfer) tr_head;
145 
146 	struct libusb20_device *os_priv;
147 };
148 
149 struct libusb_language_context {
150 	const char *lang_name;
151 	/* All error Plus 1 UNKNOWN */
152 	const char *err_strs[LIBUSB_ERROR_COUNT + 1];
153 };
154 
155 extern struct libusb_context *usbi_default_context;
156 
157 void	libusb10_add_pollfd(libusb_context *ctx, struct libusb_super_pollfd *pollfd, struct libusb20_device *pdev, int fd, short events);
158 void	libusb10_remove_pollfd(libusb_context *ctx, struct libusb_super_pollfd *pollfd);
159 void	libusb10_cancel_all_transfer(libusb_device *dev);
160 void	libusb10_cancel_all_transfer_locked(struct libusb20_device *pdev, struct libusb_device *dev);
161 
162 #endif					/* __LIBUSB10_H__ */
163