xref: /freebsd/lib/libusb/libusb.h (revision 7850fa71f55a16f414bb21163d80a03a5ab34522)
1 /* $FreeBSD$ */
2 /*-
3  * Copyright (c) 2009 Sylvestre Gallon. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #ifndef __LIBUSB_H__
28 #define	__LIBUSB_H__
29 
30 #include <stdint.h>
31 #include <time.h>
32 #include <string.h>
33 #include <pthread.h>
34 
35 #include <sys/time.h>
36 #include <sys/types.h>
37 #include <sys/endian.h>
38 
39 #ifdef __cplusplus
40 extern	"C" {
41 #endif
42 #if 0
43 }					/* indent fix */
44 
45 #endif
46 
47 struct list_head {
48 	struct list_head *prev, *next;
49 };
50 
51 /* libusb enums */
52 
53 enum libusb_class_code {
54 	LIBUSB_CLASS_PER_INTERFACE = 0,
55 	LIBUSB_CLASS_AUDIO = 1,
56 	LIBUSB_CLASS_COMM = 2,
57 	LIBUSB_CLASS_HID = 3,
58 	LIBUSB_CLASS_PTP = 6,
59 	LIBUSB_CLASS_PRINTER = 7,
60 	LIBUSB_CLASS_MASS_STORAGE = 8,
61 	LIBUSB_CLASS_HUB = 9,
62 	LIBUSB_CLASS_DATA = 10,
63 	LIBUSB_CLASS_VENDOR_SPEC = 0xff,
64 };
65 
66 enum libusb_descriptor_type {
67 	LIBUSB_DT_DEVICE = 0x01,
68 	LIBUSB_DT_CONFIG = 0x02,
69 	LIBUSB_DT_STRING = 0x03,
70 	LIBUSB_DT_INTERFACE = 0x04,
71 	LIBUSB_DT_ENDPOINT = 0x05,
72 	LIBUSB_DT_HID = 0x21,
73 	LIBUSB_DT_REPORT = 0x22,
74 	LIBUSB_DT_PHYSICAL = 0x23,
75 	LIBUSB_DT_HUB = 0x29,
76 };
77 
78 #define	LIBUSB_DT_DEVICE_SIZE		18
79 #define	LIBUSB_DT_CONFIG_SIZE		9
80 #define	LIBUSB_DT_INTERFACE_SIZE	9
81 #define	LIBUSB_DT_ENDPOINT_SIZE		7
82 #define	LIBUSB_DT_ENDPOINT_AUDIO_SIZE	9
83 #define	LIBUSB_DT_HUB_NONVAR_SIZE	7
84 
85 #define	LIBUSB_ENDPOINT_ADDRESS_MASK	0x0f
86 #define	LIBUSB_ENDPOINT_DIR_MASK	0x80
87 
88 enum libusb_endpoint_direction {
89 	LIBUSB_ENDPOINT_IN = 0x80,
90 	LIBUSB_ENDPOINT_OUT = 0x00,
91 };
92 
93 #define	LIBUSB_TRANSFER_TYPE_MASK	0x03
94 
95 enum libusb_transfer_type {
96 	LIBUSB_TRANSFER_TYPE_CONTROL = 0,
97 	LIBUSB_TRANSFER_TYPE_ISOCHRONOUS = 1,
98 	LIBUSB_TRANSFER_TYPE_BULK = 2,
99 	LIBUSB_TRANSFER_TYPE_INTERRUPT = 3,
100 };
101 
102 enum libusb_standard_request {
103 	LIBUSB_REQUEST_GET_STATUS = 0x00,
104 	LIBUSB_REQUEST_CLEAR_FEATURE = 0x01,
105 	LIBUSB_REQUEST_SET_FEATURE = 0x03,
106 	LIBUSB_REQUEST_SET_ADDRESS = 0x05,
107 	LIBUSB_REQUEST_GET_DESCRIPTOR = 0x06,
108 	LIBUSB_REQUEST_SET_DESCRIPTOR = 0x07,
109 	LIBUSB_REQUEST_GET_CONFIGURATION = 0x08,
110 	LIBUSB_REQUEST_SET_CONFIGURATION = 0x09,
111 	LIBUSB_REQUEST_GET_INTERFACE = 0x0A,
112 	LIBUSB_REQUEST_SET_INTERFACE = 0x0B,
113 	LIBUSB_REQUEST_SYNCH_FRAME = 0x0C,
114 };
115 
116 enum libusb_request_type {
117 	LIBUSB_REQUEST_TYPE_STANDARD = (0x00 << 5),
118 	LIBUSB_REQUEST_TYPE_CLASS = (0x01 << 5),
119 	LIBUSB_REQUEST_TYPE_VENDOR = (0x02 << 5),
120 	LIBUSB_REQUEST_TYPE_RESERVED = (0x03 << 5),
121 };
122 
123 enum libusb_request_recipient {
124 	LIBUSB_RECIPIENT_DEVICE = 0x00,
125 	LIBUSB_RECIPIENT_INTERFACE = 0x01,
126 	LIBUSB_RECIPIENT_ENDPOINT = 0x02,
127 	LIBUSB_RECIPIENT_OTHER = 0x03,
128 };
129 
130 #define	LIBUSB_ISO_SYNC_TYPE_MASK	0x0C
131 
132 enum libusb_iso_sync_type {
133 	LIBUSB_ISO_SYNC_TYPE_NONE = 0,
134 	LIBUSB_ISO_SYNC_TYPE_ASYNC = 1,
135 	LIBUSB_ISO_SYNC_TYPE_ADAPTIVE = 2,
136 	LIBUSB_ISO_SYNC_TYPE_SYNC = 3,
137 };
138 
139 #define	LIBUSB_ISO_USAGE_TYPE_MASK 0x30
140 
141 enum libusb_iso_usage_type {
142 	LIBUSB_ISO_USAGE_TYPE_DATA = 0,
143 	LIBUSB_ISO_USAGE_TYPE_FEEDBACK = 1,
144 	LIBUSB_ISO_USAGE_TYPE_IMPLICIT = 2,
145 };
146 
147 enum libusb_error {
148 	LIBUSB_SUCCESS = 0,
149 	LIBUSB_ERROR_IO = -1,
150 	LIBUSB_ERROR_INVALID_PARAM = -2,
151 	LIBUSB_ERROR_ACCESS = -3,
152 	LIBUSB_ERROR_NO_DEVICE = -4,
153 	LIBUSB_ERROR_NOT_FOUND = -5,
154 	LIBUSB_ERROR_BUSY = -6,
155 	LIBUSB_ERROR_TIMEOUT = -7,
156 	LIBUSB_ERROR_OVERFLOW = -8,
157 	LIBUSB_ERROR_PIPE = -9,
158 	LIBUSB_ERROR_INTERRUPTED = -10,
159 	LIBUSB_ERROR_NO_MEM = -11,
160 	LIBUSB_ERROR_NOT_SUPPORTED = -12,
161 	LIBUSB_ERROR_OTHER = -99,
162 };
163 
164 enum libusb_transfer_status {
165 	LIBUSB_TRANSFER_COMPLETED,
166 	LIBUSB_TRANSFER_ERROR,
167 	LIBUSB_TRANSFER_TIMED_OUT,
168 	LIBUSB_TRANSFER_CANCELLED,
169 	LIBUSB_TRANSFER_STALL,
170 	LIBUSB_TRANSFER_NO_DEVICE,
171 	LIBUSB_TRANSFER_OVERFLOW,
172 };
173 
174 enum libusb_transfer_flags {
175 	LIBUSB_TRANSFER_SHORT_NOT_OK = 1 << 0,
176 	LIBUSB_TRANSFER_FREE_BUFFER = 1 << 1,
177 	LIBUSB_TRANSFER_FREE_TRANSFER = 1 << 2,
178 };
179 
180 enum libusb_debug_level {
181 	LIBUSB_DEBUG_NO=0,
182 	LIBUSB_DEBUG_FUNCTION=1,
183 	LIBUSB_DEBUG_TRANSFER=2,
184 };
185 
186 /* libusb structures */
187 
188 typedef void (*libusb_pollfd_added_cb) (int fd, short events, void *user_data);
189 typedef void (*libusb_pollfd_removed_cb) (int fd, void *user_data);
190 
191 typedef struct libusb_context {
192 	int	debug;
193 	int	debug_fixed;
194 
195 	int	ctrl_pipe[2];
196 
197 	struct list_head usb_devs;
198 	pthread_mutex_t usb_devs_lock;
199 
200 	struct list_head open_devs;
201 	pthread_mutex_t open_devs_lock;
202 
203 	struct list_head flying_transfers;
204 	pthread_mutex_t flying_transfers_lock;
205 
206 	struct list_head pollfds;
207 	pthread_mutex_t pollfds_lock;
208 
209 	unsigned int pollfd_modify;
210 	pthread_mutex_t pollfd_modify_lock;
211 
212 	libusb_pollfd_added_cb fd_added_cb;
213 	libusb_pollfd_removed_cb fd_removed_cb;
214 	void   *fd_cb_user_data;
215 
216 	pthread_mutex_t events_lock;
217 	int	event_handler_active;
218 
219 	pthread_mutex_t event_waiters_lock;
220 	pthread_cond_t event_waiters_cond;
221 }	libusb_context;
222 
223 typedef struct libusb_device {
224 	pthread_mutex_t lock;
225 	int	refcnt;
226 
227 	struct libusb_context *ctx;
228 
229 	uint8_t	bus_number;
230 	uint8_t	device_address;
231 	uint8_t	num_configurations;
232 
233 	struct list_head list;
234 	unsigned long session_data;
235 	void   *os_priv;
236 }	libusb_device;
237 
238 typedef struct libusb_device_handle {
239 	pthread_mutex_t lock;
240 	unsigned long claimed_interfaces;
241 
242 	struct list_head list;
243 	struct libusb_device *dev;
244 	void   *os_priv;
245 }	libusb_device_handle;
246 
247 typedef struct libusb_device_descriptor {
248 	uint8_t	bLength;
249 	uint8_t	bDescriptorType;
250 	uint16_t bcdUSB;
251 	uint8_t	bDeviceClass;
252 	uint8_t	bDeviceSubClass;
253 	uint8_t	bDeviceProtocol;
254 	uint8_t	bMaxPacketSize0;
255 	uint16_t idVendor;
256 	uint16_t idProduct;
257 	uint16_t bcdDevice;
258 	uint8_t	iManufacturer;
259 	uint8_t	iProduct;
260 	uint8_t	iSerialNumber;
261 	uint8_t	bNumConfigurations;
262 }	libusb_device_descriptor;
263 
264 typedef struct libusb_endpoint_descriptor {
265 	uint8_t	bLength;
266 	uint8_t	bDescriptorType;
267 	uint8_t	bEndpointAddress;
268 	uint8_t	bmAttributes;
269 	uint16_t wMaxPacketSize;
270 	uint8_t	bInterval;
271 	uint8_t	bRefresh;
272 	uint8_t	bSynchAddress;
273 	unsigned char *extra;
274 	int	extra_length;
275 }	libusb_endpoint_descriptor __aligned(sizeof(void *));
276 
277 typedef struct libusb_interface_descriptor {
278 	uint8_t	bLength;
279 	uint8_t	bDescriptorType;
280 	uint8_t	bInterfaceNumber;
281 	uint8_t	bAlternateSetting;
282 	uint8_t	bNumEndpoints;
283 	uint8_t	bInterfaceClass;
284 	uint8_t	bInterfaceSubClass;
285 	uint8_t	bInterfaceProtocol;
286 	uint8_t	iInterface;
287 	struct libusb_endpoint_descriptor *endpoint;
288 	unsigned char *extra;
289 	int	extra_length;
290 }	libusb_interface_descriptor __aligned(sizeof(void *));
291 
292 typedef struct libusb_interface {
293 	struct libusb_interface_descriptor *altsetting;
294 	int	num_altsetting;
295 }	libusb_interface __aligned(sizeof(void *));
296 
297 typedef struct libusb_config_descriptor {
298 	uint8_t	bLength;
299 	uint8_t	bDescriptorType;
300 	uint16_t wTotalLength;
301 	uint8_t	bNumInterfaces;
302 	uint8_t	bConfigurationValue;
303 	uint8_t	iConfiguration;
304 	uint8_t	bmAttributes;
305 	uint8_t	MaxPower;
306 	struct libusb_interface *interface;
307 	unsigned char *extra;
308 	int	extra_length;
309 }	libusb_config_descriptor __aligned(sizeof(void *));
310 
311 typedef struct libusb_control_setup {
312 	uint8_t	bmRequestType;
313 	uint8_t	bRequest;
314 	uint16_t wValue;
315 	uint16_t wIndex;
316 	uint16_t wLength;
317 }	libusb_control_setup;
318 
319 typedef struct libusb_iso_packet_descriptor {
320 	unsigned int length;
321 	unsigned int actual_length;
322 	enum libusb_transfer_status status;
323 }	libusb_iso_packet_descriptor __aligned(sizeof(void *));
324 
325 struct libusb_transfer;
326 
327 typedef void (*libusb_transfer_cb_fn) (struct libusb_transfer *transfer);
328 
329 typedef struct libusb_transfer {
330 	libusb_device_handle *dev_handle;
331 	uint8_t	flags;
332 	unsigned int endpoint;
333 	unsigned char type;
334 	unsigned int timeout;
335 	enum libusb_transfer_status status;
336 	int	length;
337 	int	actual_length;
338 	libusb_transfer_cb_fn callback;
339 	void   *user_data;
340 	unsigned char *buffer;
341 	void *os_priv;
342 	int	num_iso_packets;
343 	struct libusb_iso_packet_descriptor iso_packet_desc[0];
344 }	libusb_transfer __aligned(sizeof(void *));
345 
346 typedef struct libusb_pollfd {
347 	int	fd;
348 	short	events;
349 }	libusb_pollfd;
350 
351 /* Library initialisation */
352 
353 void	libusb_set_debug(libusb_context * ctx, int level);
354 int	libusb_init(libusb_context ** context);
355 void	libusb_exit(struct libusb_context *ctx);
356 
357 /* Device handling and enumeration */
358 
359 ssize_t libusb_get_device_list(libusb_context * ctx, libusb_device *** list);
360 void	libusb_free_device_list(libusb_device ** list, int unref_devices);
361 uint8_t	libusb_get_bus_number(libusb_device * dev);
362 uint8_t	libusb_get_device_address(libusb_device * dev);
363 int	libusb_get_max_packet_size(libusb_device * dev, unsigned char endpoint);
364 libusb_device *libusb_ref_device(libusb_device * dev);
365 void	libusb_unref_device(libusb_device * dev);
366 int	libusb_open(libusb_device * dev, libusb_device_handle ** devh);
367 libusb_device_handle *libusb_open_device_with_vid_pid(libusb_context * ctx, uint16_t vendor_id, uint16_t product_id);
368 void	libusb_close(libusb_device_handle * devh);
369 libusb_device *libusb_get_device(libusb_device_handle * devh);
370 int	libusb_get_configuration(libusb_device_handle * devh, int *config);
371 int	libusb_set_configuration(libusb_device_handle * devh, int configuration);
372 int	libusb_claim_interface(libusb_device_handle * devh, int interface_number);
373 int	libusb_release_interface(libusb_device_handle * devh, int interface_number);
374 int 	libusb_kernel_driver_active(libusb_device_handle * devh, int interface);
375 int 	libusb_detach_kernel_driver(libusb_device_handle * devh, int interface);
376 int 	libusb_attach_kernel_driver(libusb_device_handle * devh, int interface);
377 int	libusb_set_interface_alt_setting(libusb_device_handle * devh, int interface_number, int alternate_setting);
378 
379 /* USB Descriptors */
380 
381 int	libusb_get_device_descriptor(libusb_device * dev, struct libusb_device_descriptor *desc);
382 int	libusb_get_active_config_descriptor(libusb_device * dev, struct libusb_config_descriptor **config);
383 int	libusb_get_config_descriptor(libusb_device * dev, uint8_t config_index, struct libusb_config_descriptor **config);
384 int	libusb_get_config_descriptor_by_value(libusb_device * dev, uint8_t bConfigurationValue, struct libusb_config_descriptor **config);
385 void	libusb_free_config_descriptor(struct libusb_config_descriptor *config);
386 int	libusb_get_string_descriptor_ascii(libusb_device_handle * dev, uint8_t desc_index, unsigned char *data, int length);
387 
388 /* Asynchronous device I/O*/
389 
390 struct libusb_transfer *libusb_alloc_transfer(int iso_packets);
391 void	libusb_free_transfer(struct libusb_transfer *transfer);
392 int	libusb_submit_transfer(struct libusb_transfer *transfer);
393 int	libusb_cancel_transfer(struct libusb_transfer *transfer);
394 unsigned char *libusb_get_iso_packet_buffer_simple(struct libusb_transfer *transfer, unsigned int packet);
395 
396 /* Polling and timing */
397 
398 int	libusb_try_lock_events(libusb_context * ctx);
399 void	libusb_lock_events(libusb_context * ctx);
400 void	libusb_unlock_events(libusb_context * ctx);
401 int	libusb_event_handling_ok(libusb_context * ctx);
402 int	libusb_event_handler_active(libusb_context * ctx);
403 void	libusb_lock_event_waiters(libusb_context * ctx);
404 void	libusb_unlock_event_waiters(libusb_context * ctx);
405 int	libusb_wait_for_event(libusb_context * ctx, struct timeval *tv);
406 int	libusb_handle_events_timeout(libusb_context * ctx, struct timeval *tv);
407 int	libusb_handle_events(libusb_context * ctx);
408 int	libusb_handle_events_locked(libusb_context * ctx, struct timeval *tv);
409 int	libusb_get_next_timeout(libusb_context * ctx, struct timeval *tv);
410 void	libusb_set_pollfd_notifiers(libusb_context * ctx, libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb, void *user_data);
411 struct libusb_pollfd **libusb_get_pollfds(libusb_context * ctx);
412 
413 /* Synchronous device I/O */
414 
415 int	libusb_control_transfer(libusb_device_handle * devh, uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, unsigned char *data, uint16_t wLength, unsigned int timeout);
416 int	libusb_bulk_transfer(struct libusb_device_handle *devh, unsigned char endpoint, unsigned char *data, int length, int *transferred, unsigned int timeout);
417 int	libusb_interrupt_transfer(struct libusb_device_handle *devh, unsigned char endpoint, unsigned char *data, int length, int *transferred, unsigned int timeout);
418 
419 #if 0
420 {					/* indent fix */
421 #endif
422 #ifdef __cplusplus
423 }
424 
425 #endif
426 
427 #endif					/* __LIBUSB_H__ */
428