• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Public libusb header file
3  * Copyright © 2001 Johannes Erdfelt <johannes@erdfelt.com>
4  * Copyright © 2007-2008 Daniel Drake <dsd@gentoo.org>
5  * Copyright © 2012 Pete Batard <pete@akeo.ie>
6  * Copyright © 2012-2018 Nathan Hjelm <hjelmn@cs.unm.edu>
7  * Copyright © 2014-2020 Chris Dickens <christopher.a.dickens@gmail.com>
8  * For more information, please visit: http://libusb.info
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  */
24 
25 #ifndef LIBUSB_H
26 #define LIBUSB_H
27 
28 #if defined(_MSC_VER)
29 #pragma warning(push)
30 /* Disable: warning C4200: nonstandard extension used : zero-sized array in struct/union */
31 #pragma warning(disable:4200)
32 /* on MS environments, the inline keyword is available in C++ only */
33 #if !defined(__cplusplus)
34 #define inline __inline
35 #endif
36 /* ssize_t is also not available */
37 #ifndef _SSIZE_T_DEFINED
38 #define _SSIZE_T_DEFINED
39 #include <basetsd.h>
40 typedef SSIZE_T ssize_t;
41 #endif /* _SSIZE_T_DEFINED */
42 #endif /* _MSC_VER */
43 
44 #include <limits.h>
45 #include <stdint.h>
46 #include <sys/types.h>
47 #if !defined(_MSC_VER)
48 #include <sys/time.h>
49 #endif
50 #include <time.h>
51 
52 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
53 #define ZERO_SIZED_ARRAY		/* [] - valid C99 code */
54 #else
55 #define ZERO_SIZED_ARRAY	0	/* [0] - non-standard, but usually working code */
56 #endif /* __STDC_VERSION__ */
57 
58 /* 'interface' might be defined as a macro on Windows, so we need to
59  * undefine it so as not to break the current libusb API, because
60  * libusb_config_descriptor has an 'interface' member
61  * As this can be problematic if you include windows.h after libusb.h
62  * in your sources, we force windows.h to be included first. */
63 #if defined(_WIN32) || defined(__CYGWIN__)
64 #include <windows.h>
65 #if defined(interface)
66 #undef interface
67 #endif
68 #if !defined(__CYGWIN__)
69 #include <winsock.h>
70 #endif
71 #endif /* _WIN32 || __CYGWIN__ */
72 
73 #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5))
74 #define LIBUSB_DEPRECATED_FOR(f) __attribute__ ((deprecated ("Use " #f " instead")))
75 #elif defined(__GNUC__) && (__GNUC__ >= 3)
76 #define LIBUSB_DEPRECATED_FOR(f) __attribute__ ((deprecated))
77 #else
78 #define LIBUSB_DEPRECATED_FOR(f)
79 #endif /* __GNUC__ */
80 
81 #if defined(__GNUC__)
82 #define LIBUSB_PACKED __attribute__ ((packed))
83 #else
84 #define LIBUSB_PACKED
85 #endif /* __GNUC__ */
86 
87 /** \def LIBUSB_CALL
88  * \ingroup libusb_misc
89  * libusb's Windows calling convention.
90  *
91  * Under Windows, the selection of available compilers and configurations
92  * means that, unlike other platforms, there is not <em>one true calling
93  * convention</em> (calling convention: the manner in which parameters are
94  * passed to functions in the generated assembly code).
95  *
96  * Matching the Windows API itself, libusb uses the WINAPI convention (which
97  * translates to the <tt>stdcall</tt> convention) and guarantees that the
98  * library is compiled in this way. The public header file also includes
99  * appropriate annotations so that your own software will use the right
100  * convention, even if another convention is being used by default within
101  * your codebase.
102  *
103  * The one consideration that you must apply in your software is to mark
104  * all functions which you use as libusb callbacks with this LIBUSB_CALL
105  * annotation, so that they too get compiled for the correct calling
106  * convention.
107  *
108  * On non-Windows operating systems, this macro is defined as nothing. This
109  * means that you can apply it to your code without worrying about
110  * cross-platform compatibility.
111  */
112 /* LIBUSB_CALL must be defined on both definition and declaration of libusb
113  * functions. You'd think that declaration would be enough, but cygwin will
114  * complain about conflicting types unless both are marked this way.
115  * The placement of this macro is important too; it must appear after the
116  * return type, before the function name. See internal documentation for
117  * API_EXPORTED.
118  */
119 #if defined(_WIN32) || defined(__CYGWIN__)
120 #define LIBUSB_CALL WINAPI
121 #else
122 #define LIBUSB_CALL
123 #endif /* _WIN32 || __CYGWIN__ */
124 
125 /** \def LIBUSB_API_VERSION
126  * \ingroup libusb_misc
127  * libusb's API version.
128  *
129  * Since version 1.0.13, to help with feature detection, libusb defines
130  * a LIBUSB_API_VERSION macro that gets increased every time there is a
131  * significant change to the API, such as the introduction of a new call,
132  * the definition of a new macro/enum member, or any other element that
133  * libusb applications may want to detect at compilation time.
134  *
135  * The macro is typically used in an application as follows:
136  * \code
137  * #if defined(LIBUSB_API_VERSION) && (LIBUSB_API_VERSION >= 0x01001234)
138  * // Use one of the newer features from the libusb API
139  * #endif
140  * \endcode
141  *
142  * Internally, LIBUSB_API_VERSION is defined as follows:
143  * (libusb major << 24) | (libusb minor << 16) | (16 bit incremental)
144  */
145 #define LIBUSB_API_VERSION 0x01000109
146 
147 /* The following is kept for compatibility, but will be deprecated in the future */
148 #define LIBUSBX_API_VERSION LIBUSB_API_VERSION
149 
150 #if defined(__cplusplus)
151 extern "C" {
152 #endif
153 
154 /**
155  * \ingroup libusb_misc
156  * Convert a 16-bit value from host-endian to little-endian format. On
157  * little endian systems, this function does nothing. On big endian systems,
158  * the bytes are swapped.
159  * \param x the host-endian value to convert
160  * \returns the value in little-endian byte order
161  */
libusb_cpu_to_le16(const uint16_t x)162 static inline uint16_t libusb_cpu_to_le16(const uint16_t x)
163 {
164 	union {
165 		uint8_t  b8[2];
166 		uint16_t b16;
167 	} _tmp;
168 	_tmp.b8[1] = (uint8_t) (x >> 8);
169 	_tmp.b8[0] = (uint8_t) (x & 0xff);
170 	return _tmp.b16;
171 }
172 
173 /** \def libusb_le16_to_cpu
174  * \ingroup libusb_misc
175  * Convert a 16-bit value from little-endian to host-endian format. On
176  * little endian systems, this function does nothing. On big endian systems,
177  * the bytes are swapped.
178  * \param x the little-endian value to convert
179  * \returns the value in host-endian byte order
180  */
181 #define libusb_le16_to_cpu libusb_cpu_to_le16
182 
183 /* standard USB stuff */
184 
185 /** \ingroup libusb_desc
186  * Device and/or Interface Class codes */
187 enum libusb_class_code {
188 	/** In the context of a \ref libusb_device_descriptor "device descriptor",
189 	 * this bDeviceClass value indicates that each interface specifies its
190 	 * own class information and all interfaces operate independently.
191 	 */
192 	LIBUSB_CLASS_PER_INTERFACE = 0x00,
193 
194 	/** Audio class */
195 	LIBUSB_CLASS_AUDIO = 0x01,
196 
197 	/** Communications class */
198 	LIBUSB_CLASS_COMM = 0x02,
199 
200 	/** Human Interface Device class */
201 	LIBUSB_CLASS_HID = 0x03,
202 
203 	/** Physical */
204 	LIBUSB_CLASS_PHYSICAL = 0x05,
205 
206 	/** Image class */
207 	LIBUSB_CLASS_IMAGE = 0x06,
208 	LIBUSB_CLASS_PTP = 0x06, /* legacy name from libusb-0.1 usb.h */
209 
210 	/** Printer class */
211 	LIBUSB_CLASS_PRINTER = 0x07,
212 
213 	/** Mass storage class */
214 	LIBUSB_CLASS_MASS_STORAGE = 0x08,
215 
216 	/** Hub class */
217 	LIBUSB_CLASS_HUB = 0x09,
218 
219 	/** Data class */
220 	LIBUSB_CLASS_DATA = 0x0a,
221 
222 	/** Smart Card */
223 	LIBUSB_CLASS_SMART_CARD = 0x0b,
224 
225 	/** Content Security */
226 	LIBUSB_CLASS_CONTENT_SECURITY = 0x0d,
227 
228 	/** Video */
229 	LIBUSB_CLASS_VIDEO = 0x0e,
230 
231 	/** Personal Healthcare */
232 	LIBUSB_CLASS_PERSONAL_HEALTHCARE = 0x0f,
233 
234 	/** Diagnostic Device */
235 	LIBUSB_CLASS_DIAGNOSTIC_DEVICE = 0xdc,
236 
237 	/** Wireless class */
238 	LIBUSB_CLASS_WIRELESS = 0xe0,
239 
240 	/** Miscellaneous class */
241 	LIBUSB_CLASS_MISCELLANEOUS = 0xef,
242 
243 	/** Application class */
244 	LIBUSB_CLASS_APPLICATION = 0xfe,
245 
246 	/** Class is vendor-specific */
247 	LIBUSB_CLASS_VENDOR_SPEC = 0xff
248 };
249 
250 /** \ingroup libusb_desc
251  * Descriptor types as defined by the USB specification. */
252 enum libusb_descriptor_type {
253 	/** Device descriptor. See libusb_device_descriptor. */
254 	LIBUSB_DT_DEVICE = 0x01,
255 
256 	/** Configuration descriptor. See libusb_config_descriptor. */
257 	LIBUSB_DT_CONFIG = 0x02,
258 
259 	/** String descriptor */
260 	LIBUSB_DT_STRING = 0x03,
261 
262 	/** Interface descriptor. See libusb_interface_descriptor. */
263 	LIBUSB_DT_INTERFACE = 0x04,
264 
265 	/** Endpoint descriptor. See libusb_endpoint_descriptor. */
266 	LIBUSB_DT_ENDPOINT = 0x05,
267 
268 	/** BOS descriptor */
269 	LIBUSB_DT_BOS = 0x0f,
270 
271 	/** Device Capability descriptor */
272 	LIBUSB_DT_DEVICE_CAPABILITY = 0x10,
273 
274 	/** HID descriptor */
275 	LIBUSB_DT_HID = 0x21,
276 
277 	/** HID report descriptor */
278 	LIBUSB_DT_REPORT = 0x22,
279 
280 	/** Physical descriptor */
281 	LIBUSB_DT_PHYSICAL = 0x23,
282 
283 	/** Hub descriptor */
284 	LIBUSB_DT_HUB = 0x29,
285 
286 	/** SuperSpeed Hub descriptor */
287 	LIBUSB_DT_SUPERSPEED_HUB = 0x2a,
288 
289 	/** SuperSpeed Endpoint Companion descriptor */
290 	LIBUSB_DT_SS_ENDPOINT_COMPANION = 0x30
291 };
292 
293 /* Descriptor sizes per descriptor type */
294 #define LIBUSB_DT_DEVICE_SIZE			18
295 #define LIBUSB_DT_CONFIG_SIZE			9
296 #define LIBUSB_DT_INTERFACE_SIZE		9
297 #define LIBUSB_DT_ENDPOINT_SIZE			7
298 #define LIBUSB_DT_ENDPOINT_AUDIO_SIZE		9	/* Audio extension */
299 #define LIBUSB_DT_HUB_NONVAR_SIZE		7
300 #define LIBUSB_DT_SS_ENDPOINT_COMPANION_SIZE	6
301 #define LIBUSB_DT_BOS_SIZE			5
302 #define LIBUSB_DT_DEVICE_CAPABILITY_SIZE	3
303 
304 /* BOS descriptor sizes */
305 #define LIBUSB_BT_USB_2_0_EXTENSION_SIZE	7
306 #define LIBUSB_BT_SS_USB_DEVICE_CAPABILITY_SIZE	10
307 #define LIBUSB_BT_CONTAINER_ID_SIZE		20
308 
309 /* We unwrap the BOS => define its max size */
310 #define LIBUSB_DT_BOS_MAX_SIZE				\
311 	(LIBUSB_DT_BOS_SIZE +				\
312 	 LIBUSB_BT_USB_2_0_EXTENSION_SIZE +		\
313 	 LIBUSB_BT_SS_USB_DEVICE_CAPABILITY_SIZE +	\
314 	 LIBUSB_BT_CONTAINER_ID_SIZE)
315 
316 #define LIBUSB_ENDPOINT_ADDRESS_MASK		0x0f	/* in bEndpointAddress */
317 #define LIBUSB_ENDPOINT_DIR_MASK		0x80
318 
319 /** \ingroup libusb_desc
320  * Endpoint direction. Values for bit 7 of the
321  * \ref libusb_endpoint_descriptor::bEndpointAddress "endpoint address" scheme.
322  */
323 enum libusb_endpoint_direction {
324 	/** Out: host-to-device */
325 	LIBUSB_ENDPOINT_OUT = 0x00,
326 
327 	/** In: device-to-host */
328 	LIBUSB_ENDPOINT_IN = 0x80
329 };
330 
331 #define LIBUSB_TRANSFER_TYPE_MASK		0x03	/* in bmAttributes */
332 
333 /** \ingroup libusb_desc
334  * Endpoint transfer type. Values for bits 0:1 of the
335  * \ref libusb_endpoint_descriptor::bmAttributes "endpoint attributes" field.
336  */
337 enum libusb_endpoint_transfer_type {
338 	/** Control endpoint */
339 	LIBUSB_ENDPOINT_TRANSFER_TYPE_CONTROL = 0x0,
340 
341 	/** Isochronous endpoint */
342 	LIBUSB_ENDPOINT_TRANSFER_TYPE_ISOCHRONOUS = 0x1,
343 
344 	/** Bulk endpoint */
345 	LIBUSB_ENDPOINT_TRANSFER_TYPE_BULK = 0x2,
346 
347 	/** Interrupt endpoint */
348 	LIBUSB_ENDPOINT_TRANSFER_TYPE_INTERRUPT = 0x3
349 };
350 
351 /** \ingroup libusb_misc
352  * Standard requests, as defined in table 9-5 of the USB 3.0 specifications */
353 enum libusb_standard_request {
354 	/** Request status of the specific recipient */
355 	LIBUSB_REQUEST_GET_STATUS = 0x00,
356 
357 	/** Clear or disable a specific feature */
358 	LIBUSB_REQUEST_CLEAR_FEATURE = 0x01,
359 
360 	/* 0x02 is reserved */
361 
362 	/** Set or enable a specific feature */
363 	LIBUSB_REQUEST_SET_FEATURE = 0x03,
364 
365 	/* 0x04 is reserved */
366 
367 	/** Set device address for all future accesses */
368 	LIBUSB_REQUEST_SET_ADDRESS = 0x05,
369 
370 	/** Get the specified descriptor */
371 	LIBUSB_REQUEST_GET_DESCRIPTOR = 0x06,
372 
373 	/** Used to update existing descriptors or add new descriptors */
374 	LIBUSB_REQUEST_SET_DESCRIPTOR = 0x07,
375 
376 	/** Get the current device configuration value */
377 	LIBUSB_REQUEST_GET_CONFIGURATION = 0x08,
378 
379 	/** Set device configuration */
380 	LIBUSB_REQUEST_SET_CONFIGURATION = 0x09,
381 
382 	/** Return the selected alternate setting for the specified interface */
383 	LIBUSB_REQUEST_GET_INTERFACE = 0x0a,
384 
385 	/** Select an alternate interface for the specified interface */
386 	LIBUSB_REQUEST_SET_INTERFACE = 0x0b,
387 
388 	/** Set then report an endpoint's synchronization frame */
389 	LIBUSB_REQUEST_SYNCH_FRAME = 0x0c,
390 
391 	/** Sets both the U1 and U2 Exit Latency */
392 	LIBUSB_REQUEST_SET_SEL = 0x30,
393 
394 	/** Delay from the time a host transmits a packet to the time it is
395 	  * received by the device. */
396 	LIBUSB_SET_ISOCH_DELAY = 0x31
397 };
398 
399 /** \ingroup libusb_misc
400  * Request type bits of the
401  * \ref libusb_control_setup::bmRequestType "bmRequestType" field in control
402  * transfers. */
403 enum libusb_request_type {
404 	/** Standard */
405 	LIBUSB_REQUEST_TYPE_STANDARD = (0x00 << 5),
406 
407 	/** Class */
408 	LIBUSB_REQUEST_TYPE_CLASS = (0x01 << 5),
409 
410 	/** Vendor */
411 	LIBUSB_REQUEST_TYPE_VENDOR = (0x02 << 5),
412 
413 	/** Reserved */
414 	LIBUSB_REQUEST_TYPE_RESERVED = (0x03 << 5)
415 };
416 
417 /** \ingroup libusb_misc
418  * Recipient bits of the
419  * \ref libusb_control_setup::bmRequestType "bmRequestType" field in control
420  * transfers. Values 4 through 31 are reserved. */
421 enum libusb_request_recipient {
422 	/** Device */
423 	LIBUSB_RECIPIENT_DEVICE = 0x00,
424 
425 	/** Interface */
426 	LIBUSB_RECIPIENT_INTERFACE = 0x01,
427 
428 	/** Endpoint */
429 	LIBUSB_RECIPIENT_ENDPOINT = 0x02,
430 
431 	/** Other */
432 	LIBUSB_RECIPIENT_OTHER = 0x03
433 };
434 
435 #define LIBUSB_ISO_SYNC_TYPE_MASK	0x0c
436 
437 /** \ingroup libusb_desc
438  * Synchronization type for isochronous endpoints. Values for bits 2:3 of the
439  * \ref libusb_endpoint_descriptor::bmAttributes "bmAttributes" field in
440  * libusb_endpoint_descriptor.
441  */
442 enum libusb_iso_sync_type {
443 	/** No synchronization */
444 	LIBUSB_ISO_SYNC_TYPE_NONE = 0x0,
445 
446 	/** Asynchronous */
447 	LIBUSB_ISO_SYNC_TYPE_ASYNC = 0x1,
448 
449 	/** Adaptive */
450 	LIBUSB_ISO_SYNC_TYPE_ADAPTIVE = 0x2,
451 
452 	/** Synchronous */
453 	LIBUSB_ISO_SYNC_TYPE_SYNC = 0x3
454 };
455 
456 #define LIBUSB_ISO_USAGE_TYPE_MASK	0x30
457 
458 /** \ingroup libusb_desc
459  * Usage type for isochronous endpoints. Values for bits 4:5 of the
460  * \ref libusb_endpoint_descriptor::bmAttributes "bmAttributes" field in
461  * libusb_endpoint_descriptor.
462  */
463 enum libusb_iso_usage_type {
464 	/** Data endpoint */
465 	LIBUSB_ISO_USAGE_TYPE_DATA = 0x0,
466 
467 	/** Feedback endpoint */
468 	LIBUSB_ISO_USAGE_TYPE_FEEDBACK = 0x1,
469 
470 	/** Implicit feedback Data endpoint */
471 	LIBUSB_ISO_USAGE_TYPE_IMPLICIT = 0x2
472 };
473 
474 /** \ingroup libusb_desc
475  * Supported speeds (wSpeedSupported) bitfield. Indicates what
476  * speeds the device supports.
477  */
478 enum libusb_supported_speed {
479 	/** Low speed operation supported (1.5MBit/s). */
480 	LIBUSB_LOW_SPEED_OPERATION = (1 << 0),
481 
482 	/** Full speed operation supported (12MBit/s). */
483 	LIBUSB_FULL_SPEED_OPERATION = (1 << 1),
484 
485 	/** High speed operation supported (480MBit/s). */
486 	LIBUSB_HIGH_SPEED_OPERATION = (1 << 2),
487 
488 	/** Superspeed operation supported (5000MBit/s). */
489 	LIBUSB_SUPER_SPEED_OPERATION = (1 << 3)
490 };
491 
492 /** \ingroup libusb_desc
493  * Masks for the bits of the
494  * \ref libusb_usb_2_0_extension_descriptor::bmAttributes "bmAttributes" field
495  * of the USB 2.0 Extension descriptor.
496  */
497 enum libusb_usb_2_0_extension_attributes {
498 	/** Supports Link Power Management (LPM) */
499 	LIBUSB_BM_LPM_SUPPORT = (1 << 1)
500 };
501 
502 /** \ingroup libusb_desc
503  * Masks for the bits of the
504  * \ref libusb_ss_usb_device_capability_descriptor::bmAttributes "bmAttributes" field
505  * field of the SuperSpeed USB Device Capability descriptor.
506  */
507 enum libusb_ss_usb_device_capability_attributes {
508 	/** Supports Latency Tolerance Messages (LTM) */
509 	LIBUSB_BM_LTM_SUPPORT = (1 << 1)
510 };
511 
512 /** \ingroup libusb_desc
513  * USB capability types
514  */
515 enum libusb_bos_type {
516 	/** Wireless USB device capability */
517 	LIBUSB_BT_WIRELESS_USB_DEVICE_CAPABILITY = 0x01,
518 
519 	/** USB 2.0 extensions */
520 	LIBUSB_BT_USB_2_0_EXTENSION = 0x02,
521 
522 	/** SuperSpeed USB device capability */
523 	LIBUSB_BT_SS_USB_DEVICE_CAPABILITY = 0x03,
524 
525 	/** Container ID type */
526 	LIBUSB_BT_CONTAINER_ID = 0x04
527 };
528 
529 /** \ingroup libusb_desc
530  * A structure representing the standard USB device descriptor. This
531  * descriptor is documented in section 9.6.1 of the USB 3.0 specification.
532  * All multiple-byte fields are represented in host-endian format.
533  */
534 struct libusb_device_descriptor {
535 	/** Size of this descriptor (in bytes) */
536 	uint8_t  bLength;
537 
538 	/** Descriptor type. Will have value
539 	 * \ref libusb_descriptor_type::LIBUSB_DT_DEVICE LIBUSB_DT_DEVICE in this
540 	 * context. */
541 	uint8_t  bDescriptorType;
542 
543 	/** USB specification release number in binary-coded decimal. A value of
544 	 * 0x0200 indicates USB 2.0, 0x0110 indicates USB 1.1, etc. */
545 	uint16_t bcdUSB;
546 
547 	/** USB-IF class code for the device. See \ref libusb_class_code. */
548 	uint8_t  bDeviceClass;
549 
550 	/** USB-IF subclass code for the device, qualified by the bDeviceClass
551 	 * value */
552 	uint8_t  bDeviceSubClass;
553 
554 	/** USB-IF protocol code for the device, qualified by the bDeviceClass and
555 	 * bDeviceSubClass values */
556 	uint8_t  bDeviceProtocol;
557 
558 	/** Maximum packet size for endpoint 0 */
559 	uint8_t  bMaxPacketSize0;
560 
561 	/** USB-IF vendor ID */
562 	uint16_t idVendor;
563 
564 	/** USB-IF product ID */
565 	uint16_t idProduct;
566 
567 	/** Device release number in binary-coded decimal */
568 	uint16_t bcdDevice;
569 
570 	/** Index of string descriptor describing manufacturer */
571 	uint8_t  iManufacturer;
572 
573 	/** Index of string descriptor describing product */
574 	uint8_t  iProduct;
575 
576 	/** Index of string descriptor containing device serial number */
577 	uint8_t  iSerialNumber;
578 
579 	/** Number of possible configurations */
580 	uint8_t  bNumConfigurations;
581 };
582 
583 /** \ingroup libusb_desc
584  * A structure representing the standard USB endpoint descriptor. This
585  * descriptor is documented in section 9.6.6 of the USB 3.0 specification.
586  * All multiple-byte fields are represented in host-endian format.
587  */
588 struct libusb_endpoint_descriptor {
589 	/** Size of this descriptor (in bytes) */
590 	uint8_t  bLength;
591 
592 	/** Descriptor type. Will have value
593 	 * \ref libusb_descriptor_type::LIBUSB_DT_ENDPOINT LIBUSB_DT_ENDPOINT in
594 	 * this context. */
595 	uint8_t  bDescriptorType;
596 
597 	/** The address of the endpoint described by this descriptor. Bits 0:3 are
598 	 * the endpoint number. Bits 4:6 are reserved. Bit 7 indicates direction,
599 	 * see \ref libusb_endpoint_direction. */
600 	uint8_t  bEndpointAddress;
601 
602 	/** Attributes which apply to the endpoint when it is configured using
603 	 * the bConfigurationValue. Bits 0:1 determine the transfer type and
604 	 * correspond to \ref libusb_endpoint_transfer_type. Bits 2:3 are only used
605 	 * for isochronous endpoints and correspond to \ref libusb_iso_sync_type.
606 	 * Bits 4:5 are also only used for isochronous endpoints and correspond to
607 	 * \ref libusb_iso_usage_type. Bits 6:7 are reserved. */
608 	uint8_t  bmAttributes;
609 
610 	/** Maximum packet size this endpoint is capable of sending/receiving. */
611 	uint16_t wMaxPacketSize;
612 
613 	/** Interval for polling endpoint for data transfers. */
614 	uint8_t  bInterval;
615 
616 	/** For audio devices only: the rate at which synchronization feedback
617 	 * is provided. */
618 	uint8_t  bRefresh;
619 
620 	/** For audio devices only: the address if the synch endpoint */
621 	uint8_t  bSynchAddress;
622 
623 	/** Extra descriptors. If libusb encounters unknown endpoint descriptors,
624 	 * it will store them here, should you wish to parse them. */
625 	const unsigned char *extra;
626 
627 	/** Length of the extra descriptors, in bytes. Must be non-negative. */
628 	int extra_length;
629 };
630 
631 /** \ingroup libusb_desc
632  * A structure representing the standard USB interface descriptor. This
633  * descriptor is documented in section 9.6.5 of the USB 3.0 specification.
634  * All multiple-byte fields are represented in host-endian format.
635  */
636 struct libusb_interface_descriptor {
637 	/** Size of this descriptor (in bytes) */
638 	uint8_t  bLength;
639 
640 	/** Descriptor type. Will have value
641 	 * \ref libusb_descriptor_type::LIBUSB_DT_INTERFACE LIBUSB_DT_INTERFACE
642 	 * in this context. */
643 	uint8_t  bDescriptorType;
644 
645 	/** Number of this interface */
646 	uint8_t  bInterfaceNumber;
647 
648 	/** Value used to select this alternate setting for this interface */
649 	uint8_t  bAlternateSetting;
650 
651 	/** Number of endpoints used by this interface (excluding the control
652 	 * endpoint). */
653 	uint8_t  bNumEndpoints;
654 
655 	/** USB-IF class code for this interface. See \ref libusb_class_code. */
656 	uint8_t  bInterfaceClass;
657 
658 	/** USB-IF subclass code for this interface, qualified by the
659 	 * bInterfaceClass value */
660 	uint8_t  bInterfaceSubClass;
661 
662 	/** USB-IF protocol code for this interface, qualified by the
663 	 * bInterfaceClass and bInterfaceSubClass values */
664 	uint8_t  bInterfaceProtocol;
665 
666 	/** Index of string descriptor describing this interface */
667 	uint8_t  iInterface;
668 
669 	/** Array of endpoint descriptors. This length of this array is determined
670 	 * by the bNumEndpoints field. */
671 	const struct libusb_endpoint_descriptor *endpoint;
672 
673 	/** Extra descriptors. If libusb encounters unknown interface descriptors,
674 	 * it will store them here, should you wish to parse them. */
675 	const unsigned char *extra;
676 
677 	/** Length of the extra descriptors, in bytes. Must be non-negative. */
678 	int extra_length;
679 };
680 
681 /** \ingroup libusb_desc
682  * A collection of alternate settings for a particular USB interface.
683  */
684 struct libusb_interface {
685 	/** Array of interface descriptors. The length of this array is determined
686 	 * by the num_altsetting field. */
687 	const struct libusb_interface_descriptor *altsetting;
688 
689 	/** The number of alternate settings that belong to this interface.
690 	 * Must be non-negative. */
691 	int num_altsetting;
692 };
693 
694 /** \ingroup libusb_desc
695  * A structure representing the standard USB configuration descriptor. This
696  * descriptor is documented in section 9.6.3 of the USB 3.0 specification.
697  * All multiple-byte fields are represented in host-endian format.
698  */
699 struct libusb_config_descriptor {
700 	/** Size of this descriptor (in bytes) */
701 	uint8_t  bLength;
702 
703 	/** Descriptor type. Will have value
704 	 * \ref libusb_descriptor_type::LIBUSB_DT_CONFIG LIBUSB_DT_CONFIG
705 	 * in this context. */
706 	uint8_t  bDescriptorType;
707 
708 	/** Total length of data returned for this configuration */
709 	uint16_t wTotalLength;
710 
711 	/** Number of interfaces supported by this configuration */
712 	uint8_t  bNumInterfaces;
713 
714 	/** Identifier value for this configuration */
715 	uint8_t  bConfigurationValue;
716 
717 	/** Index of string descriptor describing this configuration */
718 	uint8_t  iConfiguration;
719 
720 	/** Configuration characteristics */
721 	uint8_t  bmAttributes;
722 
723 	/** Maximum power consumption of the USB device from this bus in this
724 	 * configuration when the device is fully operation. Expressed in units
725 	 * of 2 mA when the device is operating in high-speed mode and in units
726 	 * of 8 mA when the device is operating in super-speed mode. */
727 	uint8_t  MaxPower;
728 
729 	/** Array of interfaces supported by this configuration. The length of
730 	 * this array is determined by the bNumInterfaces field. */
731 	const struct libusb_interface *interface;
732 
733 	/** Extra descriptors. If libusb encounters unknown configuration
734 	 * descriptors, it will store them here, should you wish to parse them. */
735 	const unsigned char *extra;
736 
737 	/** Length of the extra descriptors, in bytes. Must be non-negative. */
738 	int extra_length;
739 };
740 
741 /** \ingroup libusb_desc
742  * A structure representing the superspeed endpoint companion
743  * descriptor. This descriptor is documented in section 9.6.7 of
744  * the USB 3.0 specification. All multiple-byte fields are represented in
745  * host-endian format.
746  */
747 struct libusb_ss_endpoint_companion_descriptor {
748 	/** Size of this descriptor (in bytes) */
749 	uint8_t  bLength;
750 
751 	/** Descriptor type. Will have value
752 	 * \ref libusb_descriptor_type::LIBUSB_DT_SS_ENDPOINT_COMPANION in
753 	 * this context. */
754 	uint8_t  bDescriptorType;
755 
756 	/** The maximum number of packets the endpoint can send or
757 	 *  receive as part of a burst. */
758 	uint8_t  bMaxBurst;
759 
760 	/** In bulk EP: bits 4:0 represents the maximum number of
761 	 *  streams the EP supports. In isochronous EP: bits 1:0
762 	 *  represents the Mult - a zero based value that determines
763 	 *  the maximum number of packets within a service interval  */
764 	uint8_t  bmAttributes;
765 
766 	/** The total number of bytes this EP will transfer every
767 	 *  service interval. Valid only for periodic EPs. */
768 	uint16_t wBytesPerInterval;
769 };
770 
771 /** \ingroup libusb_desc
772  * A generic representation of a BOS Device Capability descriptor. It is
773  * advised to check bDevCapabilityType and call the matching
774  * libusb_get_*_descriptor function to get a structure fully matching the type.
775  */
776 struct libusb_bos_dev_capability_descriptor {
777 	/** Size of this descriptor (in bytes) */
778 	uint8_t  bLength;
779 
780 	/** Descriptor type. Will have value
781 	 * \ref libusb_descriptor_type::LIBUSB_DT_DEVICE_CAPABILITY
782 	 * LIBUSB_DT_DEVICE_CAPABILITY in this context. */
783 	uint8_t  bDescriptorType;
784 
785 	/** Device Capability type */
786 	uint8_t  bDevCapabilityType;
787 
788 	/** Device Capability data (bLength - 3 bytes) */
789 	uint8_t  dev_capability_data[ZERO_SIZED_ARRAY];
790 };
791 
792 /** \ingroup libusb_desc
793  * A structure representing the Binary Device Object Store (BOS) descriptor.
794  * This descriptor is documented in section 9.6.2 of the USB 3.0 specification.
795  * All multiple-byte fields are represented in host-endian format.
796  */
797 struct libusb_bos_descriptor {
798 	/** Size of this descriptor (in bytes) */
799 	uint8_t  bLength;
800 
801 	/** Descriptor type. Will have value
802 	 * \ref libusb_descriptor_type::LIBUSB_DT_BOS LIBUSB_DT_BOS
803 	 * in this context. */
804 	uint8_t  bDescriptorType;
805 
806 	/** Length of this descriptor and all of its sub descriptors */
807 	uint16_t wTotalLength;
808 
809 	/** The number of separate device capability descriptors in
810 	 * the BOS */
811 	uint8_t  bNumDeviceCaps;
812 
813 	/** bNumDeviceCap Device Capability Descriptors */
814 	struct libusb_bos_dev_capability_descriptor *dev_capability[ZERO_SIZED_ARRAY];
815 };
816 
817 /** \ingroup libusb_desc
818  * A structure representing the USB 2.0 Extension descriptor
819  * This descriptor is documented in section 9.6.2.1 of the USB 3.0 specification.
820  * All multiple-byte fields are represented in host-endian format.
821  */
822 struct libusb_usb_2_0_extension_descriptor {
823 	/** Size of this descriptor (in bytes) */
824 	uint8_t  bLength;
825 
826 	/** Descriptor type. Will have value
827 	 * \ref libusb_descriptor_type::LIBUSB_DT_DEVICE_CAPABILITY
828 	 * LIBUSB_DT_DEVICE_CAPABILITY in this context. */
829 	uint8_t  bDescriptorType;
830 
831 	/** Capability type. Will have value
832 	 * \ref libusb_capability_type::LIBUSB_BT_USB_2_0_EXTENSION
833 	 * LIBUSB_BT_USB_2_0_EXTENSION in this context. */
834 	uint8_t  bDevCapabilityType;
835 
836 	/** Bitmap encoding of supported device level features.
837 	 * A value of one in a bit location indicates a feature is
838 	 * supported; a value of zero indicates it is not supported.
839 	 * See \ref libusb_usb_2_0_extension_attributes. */
840 	uint32_t bmAttributes;
841 };
842 
843 /** \ingroup libusb_desc
844  * A structure representing the SuperSpeed USB Device Capability descriptor
845  * This descriptor is documented in section 9.6.2.2 of the USB 3.0 specification.
846  * All multiple-byte fields are represented in host-endian format.
847  */
848 struct libusb_ss_usb_device_capability_descriptor {
849 	/** Size of this descriptor (in bytes) */
850 	uint8_t  bLength;
851 
852 	/** Descriptor type. Will have value
853 	 * \ref libusb_descriptor_type::LIBUSB_DT_DEVICE_CAPABILITY
854 	 * LIBUSB_DT_DEVICE_CAPABILITY in this context. */
855 	uint8_t  bDescriptorType;
856 
857 	/** Capability type. Will have value
858 	 * \ref libusb_capability_type::LIBUSB_BT_SS_USB_DEVICE_CAPABILITY
859 	 * LIBUSB_BT_SS_USB_DEVICE_CAPABILITY in this context. */
860 	uint8_t  bDevCapabilityType;
861 
862 	/** Bitmap encoding of supported device level features.
863 	 * A value of one in a bit location indicates a feature is
864 	 * supported; a value of zero indicates it is not supported.
865 	 * See \ref libusb_ss_usb_device_capability_attributes. */
866 	uint8_t  bmAttributes;
867 
868 	/** Bitmap encoding of the speed supported by this device when
869 	 * operating in SuperSpeed mode. See \ref libusb_supported_speed. */
870 	uint16_t wSpeedSupported;
871 
872 	/** The lowest speed at which all the functionality supported
873 	 * by the device is available to the user. For example if the
874 	 * device supports all its functionality when connected at
875 	 * full speed and above then it sets this value to 1. */
876 	uint8_t  bFunctionalitySupport;
877 
878 	/** U1 Device Exit Latency. */
879 	uint8_t  bU1DevExitLat;
880 
881 	/** U2 Device Exit Latency. */
882 	uint16_t bU2DevExitLat;
883 };
884 
885 /** \ingroup libusb_desc
886  * A structure representing the Container ID descriptor.
887  * This descriptor is documented in section 9.6.2.3 of the USB 3.0 specification.
888  * All multiple-byte fields, except UUIDs, are represented in host-endian format.
889  */
890 struct libusb_container_id_descriptor {
891 	/** Size of this descriptor (in bytes) */
892 	uint8_t  bLength;
893 
894 	/** Descriptor type. Will have value
895 	 * \ref libusb_descriptor_type::LIBUSB_DT_DEVICE_CAPABILITY
896 	 * LIBUSB_DT_DEVICE_CAPABILITY in this context. */
897 	uint8_t  bDescriptorType;
898 
899 	/** Capability type. Will have value
900 	 * \ref libusb_capability_type::LIBUSB_BT_CONTAINER_ID
901 	 * LIBUSB_BT_CONTAINER_ID in this context. */
902 	uint8_t  bDevCapabilityType;
903 
904 	/** Reserved field */
905 	uint8_t  bReserved;
906 
907 	/** 128 bit UUID */
908 	uint8_t  ContainerID[16];
909 };
910 
911 /** \ingroup libusb_asyncio
912  * Setup packet for control transfers. */
913 #if defined(_MSC_VER) || defined(__WATCOMC__)
914 #pragma pack(push, 1)
915 #endif
916 struct libusb_control_setup {
917 	/** Request type. Bits 0:4 determine recipient, see
918 	 * \ref libusb_request_recipient. Bits 5:6 determine type, see
919 	 * \ref libusb_request_type. Bit 7 determines data transfer direction, see
920 	 * \ref libusb_endpoint_direction.
921 	 */
922 	uint8_t  bmRequestType;
923 
924 	/** Request. If the type bits of bmRequestType are equal to
925 	 * \ref libusb_request_type::LIBUSB_REQUEST_TYPE_STANDARD
926 	 * "LIBUSB_REQUEST_TYPE_STANDARD" then this field refers to
927 	 * \ref libusb_standard_request. For other cases, use of this field is
928 	 * application-specific. */
929 	uint8_t  bRequest;
930 
931 	/** Value. Varies according to request */
932 	uint16_t wValue;
933 
934 	/** Index. Varies according to request, typically used to pass an index
935 	 * or offset */
936 	uint16_t wIndex;
937 
938 	/** Number of bytes to transfer */
939 	uint16_t wLength;
940 } LIBUSB_PACKED;
941 #if defined(_MSC_VER) || defined(__WATCOMC__)
942 #pragma pack(pop)
943 #endif
944 
945 #define LIBUSB_CONTROL_SETUP_SIZE (sizeof(struct libusb_control_setup))
946 
947 /* libusb */
948 
949 struct libusb_context;
950 struct libusb_device;
951 struct libusb_device_handle;
952 
953 /** \ingroup libusb_lib
954  * Structure providing the version of the libusb runtime
955  */
956 struct libusb_version {
957 	/** Library major version. */
958 	const uint16_t major;
959 
960 	/** Library minor version. */
961 	const uint16_t minor;
962 
963 	/** Library micro version. */
964 	const uint16_t micro;
965 
966 	/** Library nano version. */
967 	const uint16_t nano;
968 
969 	/** Library release candidate suffix string, e.g. "-rc4". */
970 	const char *rc;
971 
972 	/** For ABI compatibility only. */
973 	const char *describe;
974 };
975 
976 /** \ingroup libusb_lib
977  * Structure representing a libusb session. The concept of individual libusb
978  * sessions allows for your program to use two libraries (or dynamically
979  * load two modules) which both independently use libusb. This will prevent
980  * interference between the individual libusb users - for example
981  * libusb_set_option() will not affect the other user of the library, and
982  * libusb_exit() will not destroy resources that the other user is still
983  * using.
984  *
985  * Sessions are created by libusb_init() and destroyed through libusb_exit().
986  * If your application is guaranteed to only ever include a single libusb
987  * user (i.e. you), you do not have to worry about contexts: pass NULL in
988  * every function call where a context is required, and the default context
989  * will be used. Note that libusb_set_option(NULL, ...) is special, and adds
990  * an option to a list of default options for new contexts.
991  *
992  * For more information, see \ref libusb_contexts.
993  */
994 typedef struct libusb_context libusb_context;
995 
996 /** \ingroup libusb_dev
997  * Structure representing a USB device detected on the system. This is an
998  * opaque type for which you are only ever provided with a pointer, usually
999  * originating from libusb_get_device_list() or libusb_hotplug_register_callback().
1000  *
1001  * Certain operations can be performed on a device, but in order to do any
1002  * I/O you will have to first obtain a device handle using libusb_open().
1003  *
1004  * Devices are reference counted with libusb_ref_device() and
1005  * libusb_unref_device(), and are freed when the reference count reaches 0.
1006  * New devices presented by libusb_get_device_list() have a reference count of
1007  * 1, and libusb_free_device_list() can optionally decrease the reference count
1008  * on all devices in the list. libusb_open() adds another reference which is
1009  * later destroyed by libusb_close().
1010  */
1011 typedef struct libusb_device libusb_device;
1012 
1013 
1014 /** \ingroup libusb_dev
1015  * Structure representing a handle on a USB device. This is an opaque type for
1016  * which you are only ever provided with a pointer, usually originating from
1017  * libusb_open().
1018  *
1019  * A device handle is used to perform I/O and other operations. When finished
1020  * with a device handle, you should call libusb_close().
1021  */
1022 typedef struct libusb_device_handle libusb_device_handle;
1023 
1024 /** \ingroup libusb_dev
1025  * Speed codes. Indicates the speed at which the device is operating.
1026  */
1027 enum libusb_speed {
1028 	/** The OS doesn't report or know the device speed. */
1029 	LIBUSB_SPEED_UNKNOWN = 0,
1030 
1031 	/** The device is operating at low speed (1.5MBit/s). */
1032 	LIBUSB_SPEED_LOW = 1,
1033 
1034 	/** The device is operating at full speed (12MBit/s). */
1035 	LIBUSB_SPEED_FULL = 2,
1036 
1037 	/** The device is operating at high speed (480MBit/s). */
1038 	LIBUSB_SPEED_HIGH = 3,
1039 
1040 	/** The device is operating at super speed (5000MBit/s). */
1041 	LIBUSB_SPEED_SUPER = 4,
1042 
1043 	/** The device is operating at super speed plus (10000MBit/s). */
1044 	LIBUSB_SPEED_SUPER_PLUS = 5
1045 };
1046 
1047 /** \ingroup libusb_misc
1048  * Error codes. Most libusb functions return 0 on success or one of these
1049  * codes on failure.
1050  * You can call libusb_error_name() to retrieve a string representation of an
1051  * error code or libusb_strerror() to get an end-user suitable description of
1052  * an error code.
1053  */
1054 enum libusb_error {
1055 	/** Success (no error) */
1056 	LIBUSB_SUCCESS = 0,
1057 
1058 	/** Input/output error */
1059 	LIBUSB_ERROR_IO = -1,
1060 
1061 	/** Invalid parameter */
1062 	LIBUSB_ERROR_INVALID_PARAM = -2,
1063 
1064 	/** Access denied (insufficient permissions) */
1065 	LIBUSB_ERROR_ACCESS = -3,
1066 
1067 	/** No such device (it may have been disconnected) */
1068 	LIBUSB_ERROR_NO_DEVICE = -4,
1069 
1070 	/** Entity not found */
1071 	LIBUSB_ERROR_NOT_FOUND = -5,
1072 
1073 	/** Resource busy */
1074 	LIBUSB_ERROR_BUSY = -6,
1075 
1076 	/** Operation timed out */
1077 	LIBUSB_ERROR_TIMEOUT = -7,
1078 
1079 	/** Overflow */
1080 	LIBUSB_ERROR_OVERFLOW = -8,
1081 
1082 	/** Pipe error */
1083 	LIBUSB_ERROR_PIPE = -9,
1084 
1085 	/** System call interrupted (perhaps due to signal) */
1086 	LIBUSB_ERROR_INTERRUPTED = -10,
1087 
1088 	/** Insufficient memory */
1089 	LIBUSB_ERROR_NO_MEM = -11,
1090 
1091 	/** Operation not supported or unimplemented on this platform */
1092 	LIBUSB_ERROR_NOT_SUPPORTED = -12,
1093 
1094 	/* NB: Remember to update LIBUSB_ERROR_COUNT below as well as the
1095 	   message strings in strerror.c when adding new error codes here. */
1096 
1097 	/** Other error */
1098 	LIBUSB_ERROR_OTHER = -99
1099 };
1100 
1101 /* Total number of error codes in enum libusb_error */
1102 #define LIBUSB_ERROR_COUNT 14
1103 
1104 /** \ingroup libusb_asyncio
1105  * Transfer type */
1106 enum libusb_transfer_type {
1107 	/** Control transfer */
1108 	LIBUSB_TRANSFER_TYPE_CONTROL = 0U,
1109 
1110 	/** Isochronous transfer */
1111 	LIBUSB_TRANSFER_TYPE_ISOCHRONOUS = 1U,
1112 
1113 	/** Bulk transfer */
1114 	LIBUSB_TRANSFER_TYPE_BULK = 2U,
1115 
1116 	/** Interrupt transfer */
1117 	LIBUSB_TRANSFER_TYPE_INTERRUPT = 3U,
1118 
1119 	/** Bulk stream transfer */
1120 	LIBUSB_TRANSFER_TYPE_BULK_STREAM = 4U
1121 };
1122 
1123 /** \ingroup libusb_asyncio
1124  * Transfer status codes */
1125 enum libusb_transfer_status {
1126 	/** Transfer completed without error. Note that this does not indicate
1127 	 * that the entire amount of requested data was transferred. */
1128 	LIBUSB_TRANSFER_COMPLETED,
1129 
1130 	/** Transfer failed */
1131 	LIBUSB_TRANSFER_ERROR,
1132 
1133 	/** Transfer timed out */
1134 	LIBUSB_TRANSFER_TIMED_OUT,
1135 
1136 	/** Transfer was cancelled */
1137 	LIBUSB_TRANSFER_CANCELLED,
1138 
1139 	/** For bulk/interrupt endpoints: halt condition detected (endpoint
1140 	 * stalled). For control endpoints: control request not supported. */
1141 	LIBUSB_TRANSFER_STALL,
1142 
1143 	/** Device was disconnected */
1144 	LIBUSB_TRANSFER_NO_DEVICE,
1145 
1146 	/** Device sent more data than requested */
1147 	LIBUSB_TRANSFER_OVERFLOW
1148 
1149 	/* NB! Remember to update libusb_error_name()
1150 	   when adding new status codes here. */
1151 };
1152 
1153 /** \ingroup libusb_asyncio
1154  * libusb_transfer.flags values */
1155 enum libusb_transfer_flags {
1156 	/** Report short frames as errors */
1157 	LIBUSB_TRANSFER_SHORT_NOT_OK = (1U << 0),
1158 
1159 	/** Automatically free() transfer buffer during libusb_free_transfer().
1160 	 * Note that buffers allocated with libusb_dev_mem_alloc() should not
1161 	 * be attempted freed in this way, since free() is not an appropriate
1162 	 * way to release such memory. */
1163 	LIBUSB_TRANSFER_FREE_BUFFER = (1U << 1),
1164 
1165 	/** Automatically call libusb_free_transfer() after callback returns.
1166 	 * If this flag is set, it is illegal to call libusb_free_transfer()
1167 	 * from your transfer callback, as this will result in a double-free
1168 	 * when this flag is acted upon. */
1169 	LIBUSB_TRANSFER_FREE_TRANSFER = (1U << 2),
1170 
1171 	/** Terminate transfers that are a multiple of the endpoint's
1172 	 * wMaxPacketSize with an extra zero length packet. This is useful
1173 	 * when a device protocol mandates that each logical request is
1174 	 * terminated by an incomplete packet (i.e. the logical requests are
1175 	 * not separated by other means).
1176 	 *
1177 	 * This flag only affects host-to-device transfers to bulk and interrupt
1178 	 * endpoints. In other situations, it is ignored.
1179 	 *
1180 	 * This flag only affects transfers with a length that is a multiple of
1181 	 * the endpoint's wMaxPacketSize. On transfers of other lengths, this
1182 	 * flag has no effect. Therefore, if you are working with a device that
1183 	 * needs a ZLP whenever the end of the logical request falls on a packet
1184 	 * boundary, then it is sensible to set this flag on <em>every</em>
1185 	 * transfer (you do not have to worry about only setting it on transfers
1186 	 * that end on the boundary).
1187 	 *
1188 	 * This flag is currently only supported on Linux.
1189 	 * On other systems, libusb_submit_transfer() will return
1190 	 * LIBUSB_ERROR_NOT_SUPPORTED for every transfer where this flag is set.
1191 	 *
1192 	 * Available since libusb-1.0.9.
1193 	 */
1194 	LIBUSB_TRANSFER_ADD_ZERO_PACKET = (1U << 3)
1195 };
1196 
1197 /** \ingroup libusb_asyncio
1198  * Isochronous packet descriptor. */
1199 struct libusb_iso_packet_descriptor {
1200 	/** Length of data to request in this packet */
1201 	unsigned int length;
1202 
1203 	/** Amount of data that was actually transferred */
1204 	unsigned int actual_length;
1205 
1206 	/** Status code for this packet */
1207 	enum libusb_transfer_status status;
1208 };
1209 
1210 struct libusb_transfer;
1211 
1212 /** \ingroup libusb_asyncio
1213  * Asynchronous transfer callback function type. When submitting asynchronous
1214  * transfers, you pass a pointer to a callback function of this type via the
1215  * \ref libusb_transfer::callback "callback" member of the libusb_transfer
1216  * structure. libusb will call this function later, when the transfer has
1217  * completed or failed. See \ref libusb_asyncio for more information.
1218  * \param transfer The libusb_transfer struct the callback function is being
1219  * notified about.
1220  */
1221 typedef void (LIBUSB_CALL *libusb_transfer_cb_fn)(struct libusb_transfer *transfer);
1222 
1223 /** \ingroup libusb_asyncio
1224  * The generic USB transfer structure. The user populates this structure and
1225  * then submits it in order to request a transfer. After the transfer has
1226  * completed, the library populates the transfer with the results and passes
1227  * it back to the user.
1228  */
1229 struct libusb_transfer {
1230 	/** Handle of the device that this transfer will be submitted to */
1231 	libusb_device_handle *dev_handle;
1232 
1233 	/** A bitwise OR combination of \ref libusb_transfer_flags. */
1234 	uint8_t flags;
1235 
1236 	/** Address of the endpoint where this transfer will be sent. */
1237 	unsigned char endpoint;
1238 
1239 	/** Type of the transfer from \ref libusb_transfer_type */
1240 	unsigned char type;
1241 
1242 	/** Timeout for this transfer in milliseconds. A value of 0 indicates no
1243 	 * timeout. */
1244 	unsigned int timeout;
1245 
1246 	/** The status of the transfer. Read-only, and only for use within
1247 	 * transfer callback function.
1248 	 *
1249 	 * If this is an isochronous transfer, this field may read COMPLETED even
1250 	 * if there were errors in the frames. Use the
1251 	 * \ref libusb_iso_packet_descriptor::status "status" field in each packet
1252 	 * to determine if errors occurred. */
1253 	enum libusb_transfer_status status;
1254 
1255 	/** Length of the data buffer. Must be non-negative. */
1256 	int length;
1257 
1258 	/** Actual length of data that was transferred. Read-only, and only for
1259 	 * use within transfer callback function. Not valid for isochronous
1260 	 * endpoint transfers. */
1261 	int actual_length;
1262 
1263 	/** Callback function. This will be invoked when the transfer completes,
1264 	 * fails, or is cancelled. */
1265 	libusb_transfer_cb_fn callback;
1266 
1267 	/** User context data. Useful for associating specific data to a transfer
1268 	 * that can be accessed from within the callback function.
1269 	 *
1270 	 * This field may be set manually or is taken as the `user_data` parameter
1271 	 * of the following functions:
1272 	 * - libusb_fill_bulk_transfer()
1273 	 * - libusb_fill_bulk_stream_transfer()
1274 	 * - libusb_fill_control_transfer()
1275 	 * - libusb_fill_interrupt_transfer()
1276 	 * - libusb_fill_iso_transfer() */
1277 	void *user_data;
1278 
1279 	/** Data buffer */
1280 	unsigned char *buffer;
1281 
1282 	/** Number of isochronous packets. Only used for I/O with isochronous
1283 	 * endpoints. Must be non-negative. */
1284 	int num_iso_packets;
1285 
1286 	/** Isochronous packet descriptors, for isochronous transfers only. */
1287 	struct libusb_iso_packet_descriptor iso_packet_desc[ZERO_SIZED_ARRAY];
1288 };
1289 
1290 /** \ingroup libusb_misc
1291  * Capabilities supported by an instance of libusb on the current running
1292  * platform. Test if the loaded library supports a given capability by calling
1293  * \ref libusb_has_capability().
1294  */
1295 enum libusb_capability {
1296 	/** The libusb_has_capability() API is available. */
1297 	LIBUSB_CAP_HAS_CAPABILITY = 0x0000U,
1298 
1299 	/** Hotplug support is available on this platform. */
1300 	LIBUSB_CAP_HAS_HOTPLUG = 0x0001U,
1301 
1302 	/** The library can access HID devices without requiring user intervention.
1303 	 * Note that before being able to actually access an HID device, you may
1304 	 * still have to call additional libusb functions such as
1305 	 * \ref libusb_detach_kernel_driver(). */
1306 	LIBUSB_CAP_HAS_HID_ACCESS = 0x0100U,
1307 
1308 	/** The library supports detaching of the default USB driver, using
1309 	 * \ref libusb_detach_kernel_driver(), if one is set by the OS kernel */
1310 	LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER = 0x0101U
1311 };
1312 
1313 /** \ingroup libusb_lib
1314  *  Log message levels.
1315  */
1316 enum libusb_log_level {
1317 	/** (0) : No messages ever emitted by the library (default) */
1318 	LIBUSB_LOG_LEVEL_NONE = 0,
1319 
1320 	/** (1) : Error messages are emitted */
1321 	LIBUSB_LOG_LEVEL_ERROR = 1,
1322 
1323 	/** (2) : Warning and error messages are emitted */
1324 	LIBUSB_LOG_LEVEL_WARNING = 2,
1325 
1326 	/** (3) : Informational, warning and error messages are emitted */
1327 	LIBUSB_LOG_LEVEL_INFO = 3,
1328 
1329 	/** (4) : All messages are emitted */
1330 	LIBUSB_LOG_LEVEL_DEBUG = 4
1331 };
1332 
1333 /** \ingroup libusb_lib
1334  *  Log callback mode.
1335  *
1336  *  Since version 1.0.23, \ref LIBUSB_API_VERSION >= 0x01000107
1337  *
1338  * \see libusb_set_log_cb()
1339  */
1340 enum libusb_log_cb_mode {
1341 	/** Callback function handling all log messages. */
1342 	LIBUSB_LOG_CB_GLOBAL = (1 << 0),
1343 
1344 	/** Callback function handling context related log messages. */
1345 	LIBUSB_LOG_CB_CONTEXT = (1 << 1)
1346 };
1347 
1348 /** \ingroup libusb_lib
1349  * Callback function for handling log messages.
1350  * \param ctx the context which is related to the log message, or NULL if it
1351  * is a global log message
1352  * \param level the log level, see \ref libusb_log_level for a description
1353  * \param str the log message
1354  *
1355  * Since version 1.0.23, \ref LIBUSB_API_VERSION >= 0x01000107
1356  *
1357  * \see libusb_set_log_cb()
1358  */
1359 typedef void (LIBUSB_CALL *libusb_log_cb)(libusb_context *ctx,
1360 	enum libusb_log_level level, const char *str);
1361 
1362 int LIBUSB_CALL libusb_init(libusb_context **ctx);
1363 void LIBUSB_CALL libusb_exit(libusb_context *ctx);
1364 LIBUSB_DEPRECATED_FOR(libusb_set_option)
1365 void LIBUSB_CALL libusb_set_debug(libusb_context *ctx, int level);
1366 void LIBUSB_CALL libusb_set_log_cb(libusb_context *ctx, libusb_log_cb cb, int mode);
1367 const struct libusb_version * LIBUSB_CALL libusb_get_version(void);
1368 int LIBUSB_CALL libusb_has_capability(uint32_t capability);
1369 const char * LIBUSB_CALL libusb_error_name(int errcode);
1370 int LIBUSB_CALL libusb_setlocale(const char *locale);
1371 const char * LIBUSB_CALL libusb_strerror(int errcode);
1372 
1373 ssize_t LIBUSB_CALL libusb_get_device_list(libusb_context *ctx,
1374 	libusb_device ***list);
1375 void LIBUSB_CALL libusb_free_device_list(libusb_device **list,
1376 	int unref_devices);
1377 libusb_device * LIBUSB_CALL libusb_ref_device(libusb_device *dev);
1378 void LIBUSB_CALL libusb_unref_device(libusb_device *dev);
1379 
1380 int LIBUSB_CALL libusb_get_configuration(libusb_device_handle *dev,
1381 	int *config);
1382 int LIBUSB_CALL libusb_get_device_descriptor(libusb_device *dev,
1383 	struct libusb_device_descriptor *desc);
1384 int LIBUSB_CALL libusb_get_active_config_descriptor(libusb_device *dev,
1385 	struct libusb_config_descriptor **config);
1386 int LIBUSB_CALL libusb_get_config_descriptor(libusb_device *dev,
1387 	uint8_t config_index, struct libusb_config_descriptor **config);
1388 int LIBUSB_CALL libusb_get_config_descriptor_by_value(libusb_device *dev,
1389 	uint8_t bConfigurationValue, struct libusb_config_descriptor **config);
1390 void LIBUSB_CALL libusb_free_config_descriptor(
1391 	struct libusb_config_descriptor *config);
1392 int LIBUSB_CALL libusb_get_ss_endpoint_companion_descriptor(
1393 	libusb_context *ctx,
1394 	const struct libusb_endpoint_descriptor *endpoint,
1395 	struct libusb_ss_endpoint_companion_descriptor **ep_comp);
1396 void LIBUSB_CALL libusb_free_ss_endpoint_companion_descriptor(
1397 	struct libusb_ss_endpoint_companion_descriptor *ep_comp);
1398 int LIBUSB_CALL libusb_get_bos_descriptor(libusb_device_handle *dev_handle,
1399 	struct libusb_bos_descriptor **bos);
1400 void LIBUSB_CALL libusb_free_bos_descriptor(struct libusb_bos_descriptor *bos);
1401 int LIBUSB_CALL libusb_get_usb_2_0_extension_descriptor(
1402 	libusb_context *ctx,
1403 	struct libusb_bos_dev_capability_descriptor *dev_cap,
1404 	struct libusb_usb_2_0_extension_descriptor **usb_2_0_extension);
1405 void LIBUSB_CALL libusb_free_usb_2_0_extension_descriptor(
1406 	struct libusb_usb_2_0_extension_descriptor *usb_2_0_extension);
1407 int LIBUSB_CALL libusb_get_ss_usb_device_capability_descriptor(
1408 	libusb_context *ctx,
1409 	struct libusb_bos_dev_capability_descriptor *dev_cap,
1410 	struct libusb_ss_usb_device_capability_descriptor **ss_usb_device_cap);
1411 void LIBUSB_CALL libusb_free_ss_usb_device_capability_descriptor(
1412 	struct libusb_ss_usb_device_capability_descriptor *ss_usb_device_cap);
1413 int LIBUSB_CALL libusb_get_container_id_descriptor(libusb_context *ctx,
1414 	struct libusb_bos_dev_capability_descriptor *dev_cap,
1415 	struct libusb_container_id_descriptor **container_id);
1416 void LIBUSB_CALL libusb_free_container_id_descriptor(
1417 	struct libusb_container_id_descriptor *container_id);
1418 uint8_t LIBUSB_CALL libusb_get_bus_number(libusb_device *dev);
1419 uint8_t LIBUSB_CALL libusb_get_port_number(libusb_device *dev);
1420 int LIBUSB_CALL libusb_get_port_numbers(libusb_device *dev, uint8_t *port_numbers, int port_numbers_len);
1421 LIBUSB_DEPRECATED_FOR(libusb_get_port_numbers)
1422 int LIBUSB_CALL libusb_get_port_path(libusb_context *ctx, libusb_device *dev, uint8_t *path, uint8_t path_length);
1423 libusb_device * LIBUSB_CALL libusb_get_parent(libusb_device *dev);
1424 uint8_t LIBUSB_CALL libusb_get_device_address(libusb_device *dev);
1425 int LIBUSB_CALL libusb_get_device_speed(libusb_device *dev);
1426 int LIBUSB_CALL libusb_get_max_packet_size(libusb_device *dev,
1427 	unsigned char endpoint);
1428 int LIBUSB_CALL libusb_get_max_iso_packet_size(libusb_device *dev,
1429 	unsigned char endpoint);
1430 
1431 int LIBUSB_CALL libusb_wrap_sys_device(libusb_context *ctx, intptr_t sys_dev, libusb_device_handle **dev_handle);
1432 int LIBUSB_CALL libusb_open(libusb_device *dev, libusb_device_handle **dev_handle);
1433 void LIBUSB_CALL libusb_close(libusb_device_handle *dev_handle);
1434 libusb_device * LIBUSB_CALL libusb_get_device(libusb_device_handle *dev_handle);
1435 
1436 int LIBUSB_CALL libusb_set_configuration(libusb_device_handle *dev_handle,
1437 	int configuration);
1438 int LIBUSB_CALL libusb_claim_interface(libusb_device_handle *dev_handle,
1439 	int interface_number);
1440 int LIBUSB_CALL libusb_release_interface(libusb_device_handle *dev_handle,
1441 	int interface_number);
1442 
1443 libusb_device_handle * LIBUSB_CALL libusb_open_device_with_vid_pid(
1444 	libusb_context *ctx, uint16_t vendor_id, uint16_t product_id);
1445 
1446 int LIBUSB_CALL libusb_set_interface_alt_setting(libusb_device_handle *dev_handle,
1447 	int interface_number, int alternate_setting);
1448 int LIBUSB_CALL libusb_clear_halt(libusb_device_handle *dev_handle,
1449 	unsigned char endpoint);
1450 int LIBUSB_CALL libusb_reset_device(libusb_device_handle *dev_handle);
1451 
1452 int LIBUSB_CALL libusb_alloc_streams(libusb_device_handle *dev_handle,
1453 	uint32_t num_streams, unsigned char *endpoints, int num_endpoints);
1454 int LIBUSB_CALL libusb_free_streams(libusb_device_handle *dev_handle,
1455 	unsigned char *endpoints, int num_endpoints);
1456 
1457 unsigned char * LIBUSB_CALL libusb_dev_mem_alloc(libusb_device_handle *dev_handle,
1458 	size_t length);
1459 int LIBUSB_CALL libusb_dev_mem_free(libusb_device_handle *dev_handle,
1460 	unsigned char *buffer, size_t length);
1461 
1462 int LIBUSB_CALL libusb_kernel_driver_active(libusb_device_handle *dev_handle,
1463 	int interface_number);
1464 int LIBUSB_CALL libusb_detach_kernel_driver(libusb_device_handle *dev_handle,
1465 	int interface_number);
1466 int LIBUSB_CALL libusb_attach_kernel_driver(libusb_device_handle *dev_handle,
1467 	int interface_number);
1468 int LIBUSB_CALL libusb_set_auto_detach_kernel_driver(
1469 	libusb_device_handle *dev_handle, int enable);
1470 
1471 /* async I/O */
1472 
1473 /** \ingroup libusb_asyncio
1474  * Get the data section of a control transfer. This convenience function is here
1475  * to remind you that the data does not start until 8 bytes into the actual
1476  * buffer, as the setup packet comes first.
1477  *
1478  * Calling this function only makes sense from a transfer callback function,
1479  * or situations where you have already allocated a suitably sized buffer at
1480  * transfer->buffer.
1481  *
1482  * \param transfer a transfer
1483  * \returns pointer to the first byte of the data section
1484  */
libusb_control_transfer_get_data(struct libusb_transfer * transfer)1485 static inline unsigned char *libusb_control_transfer_get_data(
1486 	struct libusb_transfer *transfer)
1487 {
1488 	return transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE;
1489 }
1490 
1491 /** \ingroup libusb_asyncio
1492  * Get the control setup packet of a control transfer. This convenience
1493  * function is here to remind you that the control setup occupies the first
1494  * 8 bytes of the transfer data buffer.
1495  *
1496  * Calling this function only makes sense from a transfer callback function,
1497  * or situations where you have already allocated a suitably sized buffer at
1498  * transfer->buffer.
1499  *
1500  * \param transfer a transfer
1501  * \returns a casted pointer to the start of the transfer data buffer
1502  */
libusb_control_transfer_get_setup(struct libusb_transfer * transfer)1503 static inline struct libusb_control_setup *libusb_control_transfer_get_setup(
1504 	struct libusb_transfer *transfer)
1505 {
1506 	return (struct libusb_control_setup *)(void *)transfer->buffer;
1507 }
1508 
1509 /** \ingroup libusb_asyncio
1510  * Helper function to populate the setup packet (first 8 bytes of the data
1511  * buffer) for a control transfer. The wIndex, wValue and wLength values should
1512  * be given in host-endian byte order.
1513  *
1514  * \param buffer buffer to output the setup packet into
1515  * This pointer must be aligned to at least 2 bytes boundary.
1516  * \param bmRequestType see the
1517  * \ref libusb_control_setup::bmRequestType "bmRequestType" field of
1518  * \ref libusb_control_setup
1519  * \param bRequest see the
1520  * \ref libusb_control_setup::bRequest "bRequest" field of
1521  * \ref libusb_control_setup
1522  * \param wValue see the
1523  * \ref libusb_control_setup::wValue "wValue" field of
1524  * \ref libusb_control_setup
1525  * \param wIndex see the
1526  * \ref libusb_control_setup::wIndex "wIndex" field of
1527  * \ref libusb_control_setup
1528  * \param wLength see the
1529  * \ref libusb_control_setup::wLength "wLength" field of
1530  * \ref libusb_control_setup
1531  */
libusb_fill_control_setup(unsigned char * buffer,uint8_t bmRequestType,uint8_t bRequest,uint16_t wValue,uint16_t wIndex,uint16_t wLength)1532 static inline void libusb_fill_control_setup(unsigned char *buffer,
1533 	uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
1534 	uint16_t wLength)
1535 {
1536 	struct libusb_control_setup *setup = (struct libusb_control_setup *)(void *)buffer;
1537 	setup->bmRequestType = bmRequestType;
1538 	setup->bRequest = bRequest;
1539 	setup->wValue = libusb_cpu_to_le16(wValue);
1540 	setup->wIndex = libusb_cpu_to_le16(wIndex);
1541 	setup->wLength = libusb_cpu_to_le16(wLength);
1542 }
1543 
1544 struct libusb_transfer * LIBUSB_CALL libusb_alloc_transfer(int iso_packets);
1545 int LIBUSB_CALL libusb_submit_transfer(struct libusb_transfer *transfer);
1546 int LIBUSB_CALL libusb_cancel_transfer(struct libusb_transfer *transfer);
1547 void LIBUSB_CALL libusb_free_transfer(struct libusb_transfer *transfer);
1548 void LIBUSB_CALL libusb_transfer_set_stream_id(
1549 	struct libusb_transfer *transfer, uint32_t stream_id);
1550 uint32_t LIBUSB_CALL libusb_transfer_get_stream_id(
1551 	struct libusb_transfer *transfer);
1552 
1553 /** \ingroup libusb_asyncio
1554  * Helper function to populate the required \ref libusb_transfer fields
1555  * for a control transfer.
1556  *
1557  * If you pass a transfer buffer to this function, the first 8 bytes will
1558  * be interpreted as a control setup packet, and the wLength field will be
1559  * used to automatically populate the \ref libusb_transfer::length "length"
1560  * field of the transfer. Therefore the recommended approach is:
1561  * -# Allocate a suitably sized data buffer (including space for control setup)
1562  * -# Call libusb_fill_control_setup()
1563  * -# If this is a host-to-device transfer with a data stage, put the data
1564  *    in place after the setup packet
1565  * -# Call this function
1566  * -# Call libusb_submit_transfer()
1567  *
1568  * It is also legal to pass a NULL buffer to this function, in which case this
1569  * function will not attempt to populate the length field. Remember that you
1570  * must then populate the buffer and length fields later.
1571  *
1572  * \param transfer the transfer to populate
1573  * \param dev_handle handle of the device that will handle the transfer
1574  * \param buffer data buffer. If provided, this function will interpret the
1575  * first 8 bytes as a setup packet and infer the transfer length from that.
1576  * This pointer must be aligned to at least 2 bytes boundary.
1577  * \param callback callback function to be invoked on transfer completion
1578  * \param user_data user data to pass to callback function
1579  * \param timeout timeout for the transfer in milliseconds
1580  */
libusb_fill_control_transfer(struct libusb_transfer * transfer,libusb_device_handle * dev_handle,unsigned char * buffer,libusb_transfer_cb_fn callback,void * user_data,unsigned int timeout)1581 static inline void libusb_fill_control_transfer(
1582 	struct libusb_transfer *transfer, libusb_device_handle *dev_handle,
1583 	unsigned char *buffer, libusb_transfer_cb_fn callback, void *user_data,
1584 	unsigned int timeout)
1585 {
1586 	struct libusb_control_setup *setup = (struct libusb_control_setup *)(void *)buffer;
1587 	transfer->dev_handle = dev_handle;
1588 	transfer->endpoint = 0;
1589 	transfer->type = LIBUSB_TRANSFER_TYPE_CONTROL;
1590 	transfer->timeout = timeout;
1591 	transfer->buffer = buffer;
1592 	if (setup)
1593 		transfer->length = (int) (LIBUSB_CONTROL_SETUP_SIZE
1594 			+ libusb_le16_to_cpu(setup->wLength));
1595 	transfer->user_data = user_data;
1596 	transfer->callback = callback;
1597 }
1598 
1599 /** \ingroup libusb_asyncio
1600  * Helper function to populate the required \ref libusb_transfer fields
1601  * for a bulk transfer.
1602  *
1603  * \param transfer the transfer to populate
1604  * \param dev_handle handle of the device that will handle the transfer
1605  * \param endpoint address of the endpoint where this transfer will be sent
1606  * \param buffer data buffer
1607  * \param length length of data buffer
1608  * \param callback callback function to be invoked on transfer completion
1609  * \param user_data user data to pass to callback function
1610  * \param timeout timeout for the transfer in milliseconds
1611  */
libusb_fill_bulk_transfer(struct libusb_transfer * transfer,libusb_device_handle * dev_handle,unsigned char endpoint,unsigned char * buffer,int length,libusb_transfer_cb_fn callback,void * user_data,unsigned int timeout)1612 static inline void libusb_fill_bulk_transfer(struct libusb_transfer *transfer,
1613 	libusb_device_handle *dev_handle, unsigned char endpoint,
1614 	unsigned char *buffer, int length, libusb_transfer_cb_fn callback,
1615 	void *user_data, unsigned int timeout)
1616 {
1617 	transfer->dev_handle = dev_handle;
1618 	transfer->endpoint = endpoint;
1619 	transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1620 	transfer->timeout = timeout;
1621 	transfer->buffer = buffer;
1622 	transfer->length = length;
1623 	transfer->user_data = user_data;
1624 	transfer->callback = callback;
1625 }
1626 
1627 /** \ingroup libusb_asyncio
1628  * Helper function to populate the required \ref libusb_transfer fields
1629  * for a bulk transfer using bulk streams.
1630  *
1631  * Since version 1.0.19, \ref LIBUSB_API_VERSION >= 0x01000103
1632  *
1633  * \param transfer the transfer to populate
1634  * \param dev_handle handle of the device that will handle the transfer
1635  * \param endpoint address of the endpoint where this transfer will be sent
1636  * \param stream_id bulk stream id for this transfer
1637  * \param buffer data buffer
1638  * \param length length of data buffer
1639  * \param callback callback function to be invoked on transfer completion
1640  * \param user_data user data to pass to callback function
1641  * \param timeout timeout for the transfer in milliseconds
1642  */
libusb_fill_bulk_stream_transfer(struct libusb_transfer * transfer,libusb_device_handle * dev_handle,unsigned char endpoint,uint32_t stream_id,unsigned char * buffer,int length,libusb_transfer_cb_fn callback,void * user_data,unsigned int timeout)1643 static inline void libusb_fill_bulk_stream_transfer(
1644 	struct libusb_transfer *transfer, libusb_device_handle *dev_handle,
1645 	unsigned char endpoint, uint32_t stream_id,
1646 	unsigned char *buffer, int length, libusb_transfer_cb_fn callback,
1647 	void *user_data, unsigned int timeout)
1648 {
1649 	libusb_fill_bulk_transfer(transfer, dev_handle, endpoint, buffer,
1650 				  length, callback, user_data, timeout);
1651 	transfer->type = LIBUSB_TRANSFER_TYPE_BULK_STREAM;
1652 	libusb_transfer_set_stream_id(transfer, stream_id);
1653 }
1654 
1655 /** \ingroup libusb_asyncio
1656  * Helper function to populate the required \ref libusb_transfer fields
1657  * for an interrupt transfer.
1658  *
1659  * \param transfer the transfer to populate
1660  * \param dev_handle handle of the device that will handle the transfer
1661  * \param endpoint address of the endpoint where this transfer will be sent
1662  * \param buffer data buffer
1663  * \param length length of data buffer
1664  * \param callback callback function to be invoked on transfer completion
1665  * \param user_data user data to pass to callback function
1666  * \param timeout timeout for the transfer in milliseconds
1667  */
libusb_fill_interrupt_transfer(struct libusb_transfer * transfer,libusb_device_handle * dev_handle,unsigned char endpoint,unsigned char * buffer,int length,libusb_transfer_cb_fn callback,void * user_data,unsigned int timeout)1668 static inline void libusb_fill_interrupt_transfer(
1669 	struct libusb_transfer *transfer, libusb_device_handle *dev_handle,
1670 	unsigned char endpoint, unsigned char *buffer, int length,
1671 	libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
1672 {
1673 	transfer->dev_handle = dev_handle;
1674 	transfer->endpoint = endpoint;
1675 	transfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT;
1676 	transfer->timeout = timeout;
1677 	transfer->buffer = buffer;
1678 	transfer->length = length;
1679 	transfer->user_data = user_data;
1680 	transfer->callback = callback;
1681 }
1682 
1683 /** \ingroup libusb_asyncio
1684  * Helper function to populate the required \ref libusb_transfer fields
1685  * for an isochronous transfer.
1686  *
1687  * \param transfer the transfer to populate
1688  * \param dev_handle handle of the device that will handle the transfer
1689  * \param endpoint address of the endpoint where this transfer will be sent
1690  * \param buffer data buffer
1691  * \param length length of data buffer
1692  * \param num_iso_packets the number of isochronous packets
1693  * \param callback callback function to be invoked on transfer completion
1694  * \param user_data user data to pass to callback function
1695  * \param timeout timeout for the transfer in milliseconds
1696  */
libusb_fill_iso_transfer(struct libusb_transfer * transfer,libusb_device_handle * dev_handle,unsigned char endpoint,unsigned char * buffer,int length,int num_iso_packets,libusb_transfer_cb_fn callback,void * user_data,unsigned int timeout)1697 static inline void libusb_fill_iso_transfer(struct libusb_transfer *transfer,
1698 	libusb_device_handle *dev_handle, unsigned char endpoint,
1699 	unsigned char *buffer, int length, int num_iso_packets,
1700 	libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
1701 {
1702 	transfer->dev_handle = dev_handle;
1703 	transfer->endpoint = endpoint;
1704 	transfer->type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS;
1705 	transfer->timeout = timeout;
1706 	transfer->buffer = buffer;
1707 	transfer->length = length;
1708 	transfer->num_iso_packets = num_iso_packets;
1709 	transfer->user_data = user_data;
1710 	transfer->callback = callback;
1711 }
1712 
1713 /** \ingroup libusb_asyncio
1714  * Convenience function to set the length of all packets in an isochronous
1715  * transfer, based on the num_iso_packets field in the transfer structure.
1716  *
1717  * \param transfer a transfer
1718  * \param length the length to set in each isochronous packet descriptor
1719  * \see libusb_get_max_packet_size()
1720  */
libusb_set_iso_packet_lengths(struct libusb_transfer * transfer,unsigned int length)1721 static inline void libusb_set_iso_packet_lengths(
1722 	struct libusb_transfer *transfer, unsigned int length)
1723 {
1724 	int i;
1725 
1726 	for (i = 0; i < transfer->num_iso_packets; i++)
1727 		transfer->iso_packet_desc[i].length = length;
1728 }
1729 
1730 /** \ingroup libusb_asyncio
1731  * Convenience function to locate the position of an isochronous packet
1732  * within the buffer of an isochronous transfer.
1733  *
1734  * This is a thorough function which loops through all preceding packets,
1735  * accumulating their lengths to find the position of the specified packet.
1736  * Typically you will assign equal lengths to each packet in the transfer,
1737  * and hence the above method is sub-optimal. You may wish to use
1738  * libusb_get_iso_packet_buffer_simple() instead.
1739  *
1740  * \param transfer a transfer
1741  * \param packet the packet to return the address of
1742  * \returns the base address of the packet buffer inside the transfer buffer,
1743  * or NULL if the packet does not exist.
1744  * \see libusb_get_iso_packet_buffer_simple()
1745  */
libusb_get_iso_packet_buffer(struct libusb_transfer * transfer,unsigned int packet)1746 static inline unsigned char *libusb_get_iso_packet_buffer(
1747 	struct libusb_transfer *transfer, unsigned int packet)
1748 {
1749 	int i;
1750 	size_t offset = 0;
1751 	int _packet;
1752 
1753 	/* oops..slight bug in the API. packet is an unsigned int, but we use
1754 	 * signed integers almost everywhere else. range-check and convert to
1755 	 * signed to avoid compiler warnings. FIXME for libusb-2. */
1756 	if (packet > INT_MAX)
1757 		return NULL;
1758 	_packet = (int) packet;
1759 
1760 	if (_packet >= transfer->num_iso_packets)
1761 		return NULL;
1762 
1763 	for (i = 0; i < _packet; i++)
1764 		offset += transfer->iso_packet_desc[i].length;
1765 
1766 	return transfer->buffer + offset;
1767 }
1768 
1769 /** \ingroup libusb_asyncio
1770  * Convenience function to locate the position of an isochronous packet
1771  * within the buffer of an isochronous transfer, for transfers where each
1772  * packet is of identical size.
1773  *
1774  * This function relies on the assumption that every packet within the transfer
1775  * is of identical size to the first packet. Calculating the location of
1776  * the packet buffer is then just a simple calculation:
1777  * <tt>buffer + (packet_size * packet)</tt>
1778  *
1779  * Do not use this function on transfers other than those that have identical
1780  * packet lengths for each packet.
1781  *
1782  * \param transfer a transfer
1783  * \param packet the packet to return the address of
1784  * \returns the base address of the packet buffer inside the transfer buffer,
1785  * or NULL if the packet does not exist.
1786  * \see libusb_get_iso_packet_buffer()
1787  */
libusb_get_iso_packet_buffer_simple(struct libusb_transfer * transfer,unsigned int packet)1788 static inline unsigned char *libusb_get_iso_packet_buffer_simple(
1789 	struct libusb_transfer *transfer, unsigned int packet)
1790 {
1791 	int _packet;
1792 
1793 	/* oops..slight bug in the API. packet is an unsigned int, but we use
1794 	 * signed integers almost everywhere else. range-check and convert to
1795 	 * signed to avoid compiler warnings. FIXME for libusb-2. */
1796 	if (packet > INT_MAX)
1797 		return NULL;
1798 	_packet = (int) packet;
1799 
1800 	if (_packet >= transfer->num_iso_packets)
1801 		return NULL;
1802 
1803 	return transfer->buffer + ((int) transfer->iso_packet_desc[0].length * _packet);
1804 }
1805 
1806 /* sync I/O */
1807 
1808 int LIBUSB_CALL libusb_control_transfer(libusb_device_handle *dev_handle,
1809 	uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
1810 	unsigned char *data, uint16_t wLength, unsigned int timeout);
1811 
1812 int LIBUSB_CALL libusb_bulk_transfer(libusb_device_handle *dev_handle,
1813 	unsigned char endpoint, unsigned char *data, int length,
1814 	int *actual_length, unsigned int timeout);
1815 
1816 int LIBUSB_CALL libusb_interrupt_transfer(libusb_device_handle *dev_handle,
1817 	unsigned char endpoint, unsigned char *data, int length,
1818 	int *actual_length, unsigned int timeout);
1819 
1820 /** \ingroup libusb_desc
1821  * Retrieve a descriptor from the default control pipe.
1822  * This is a convenience function which formulates the appropriate control
1823  * message to retrieve the descriptor.
1824  *
1825  * \param dev_handle a device handle
1826  * \param desc_type the descriptor type, see \ref libusb_descriptor_type
1827  * \param desc_index the index of the descriptor to retrieve
1828  * \param data output buffer for descriptor
1829  * \param length size of data buffer
1830  * \returns number of bytes returned in data, or LIBUSB_ERROR code on failure
1831  */
libusb_get_descriptor(libusb_device_handle * dev_handle,uint8_t desc_type,uint8_t desc_index,unsigned char * data,int length)1832 static inline int libusb_get_descriptor(libusb_device_handle *dev_handle,
1833 	uint8_t desc_type, uint8_t desc_index, unsigned char *data, int length)
1834 {
1835 	return libusb_control_transfer(dev_handle, LIBUSB_ENDPOINT_IN,
1836 		LIBUSB_REQUEST_GET_DESCRIPTOR, (uint16_t) ((desc_type << 8) | desc_index),
1837 		0, data, (uint16_t) length, 1000);
1838 }
1839 
1840 /** \ingroup libusb_desc
1841  * Retrieve a descriptor from a device.
1842  * This is a convenience function which formulates the appropriate control
1843  * message to retrieve the descriptor. The string returned is Unicode, as
1844  * detailed in the USB specifications.
1845  *
1846  * \param dev_handle a device handle
1847  * \param desc_index the index of the descriptor to retrieve
1848  * \param langid the language ID for the string descriptor
1849  * \param data output buffer for descriptor
1850  * \param length size of data buffer
1851  * \returns number of bytes returned in data, or LIBUSB_ERROR code on failure
1852  * \see libusb_get_string_descriptor_ascii()
1853  */
libusb_get_string_descriptor(libusb_device_handle * dev_handle,uint8_t desc_index,uint16_t langid,unsigned char * data,int length)1854 static inline int libusb_get_string_descriptor(libusb_device_handle *dev_handle,
1855 	uint8_t desc_index, uint16_t langid, unsigned char *data, int length)
1856 {
1857 	return libusb_control_transfer(dev_handle, LIBUSB_ENDPOINT_IN,
1858 		LIBUSB_REQUEST_GET_DESCRIPTOR, (uint16_t)((LIBUSB_DT_STRING << 8) | desc_index),
1859 		langid, data, (uint16_t) length, 1000);
1860 }
1861 
1862 int LIBUSB_CALL libusb_get_string_descriptor_ascii(libusb_device_handle *dev_handle,
1863 	uint8_t desc_index, unsigned char *data, int length);
1864 
1865 /* polling and timeouts */
1866 
1867 int LIBUSB_CALL libusb_try_lock_events(libusb_context *ctx);
1868 void LIBUSB_CALL libusb_lock_events(libusb_context *ctx);
1869 void LIBUSB_CALL libusb_unlock_events(libusb_context *ctx);
1870 int LIBUSB_CALL libusb_event_handling_ok(libusb_context *ctx);
1871 int LIBUSB_CALL libusb_event_handler_active(libusb_context *ctx);
1872 void LIBUSB_CALL libusb_interrupt_event_handler(libusb_context *ctx);
1873 void LIBUSB_CALL libusb_lock_event_waiters(libusb_context *ctx);
1874 void LIBUSB_CALL libusb_unlock_event_waiters(libusb_context *ctx);
1875 int LIBUSB_CALL libusb_wait_for_event(libusb_context *ctx, struct timeval *tv);
1876 
1877 int LIBUSB_CALL libusb_handle_events_timeout(libusb_context *ctx,
1878 	struct timeval *tv);
1879 int LIBUSB_CALL libusb_handle_events_timeout_completed(libusb_context *ctx,
1880 	struct timeval *tv, int *completed);
1881 int LIBUSB_CALL libusb_handle_events(libusb_context *ctx);
1882 int LIBUSB_CALL libusb_handle_events_completed(libusb_context *ctx, int *completed);
1883 int LIBUSB_CALL libusb_handle_events_locked(libusb_context *ctx,
1884 	struct timeval *tv);
1885 int LIBUSB_CALL libusb_pollfds_handle_timeouts(libusb_context *ctx);
1886 int LIBUSB_CALL libusb_get_next_timeout(libusb_context *ctx,
1887 	struct timeval *tv);
1888 
1889 /** \ingroup libusb_poll
1890  * File descriptor for polling
1891  */
1892 struct libusb_pollfd {
1893 	/** Numeric file descriptor */
1894 	int fd;
1895 
1896 	/** Event flags to poll for from <poll.h>. POLLIN indicates that you
1897 	 * should monitor this file descriptor for becoming ready to read from,
1898 	 * and POLLOUT indicates that you should monitor this file descriptor for
1899 	 * nonblocking write readiness. */
1900 	short events;
1901 };
1902 
1903 /** \ingroup libusb_poll
1904  * Callback function, invoked when a new file descriptor should be added
1905  * to the set of file descriptors monitored for events.
1906  * \param fd the new file descriptor
1907  * \param events events to monitor for, see \ref libusb_pollfd for a
1908  * description
1909  * \param user_data User data pointer specified in
1910  * libusb_set_pollfd_notifiers() call
1911  * \see libusb_set_pollfd_notifiers()
1912  */
1913 typedef void (LIBUSB_CALL *libusb_pollfd_added_cb)(int fd, short events,
1914 	void *user_data);
1915 
1916 /** \ingroup libusb_poll
1917  * Callback function, invoked when a file descriptor should be removed from
1918  * the set of file descriptors being monitored for events. After returning
1919  * from this callback, do not use that file descriptor again.
1920  * \param fd the file descriptor to stop monitoring
1921  * \param user_data User data pointer specified in
1922  * libusb_set_pollfd_notifiers() call
1923  * \see libusb_set_pollfd_notifiers()
1924  */
1925 typedef void (LIBUSB_CALL *libusb_pollfd_removed_cb)(int fd, void *user_data);
1926 
1927 const struct libusb_pollfd ** LIBUSB_CALL libusb_get_pollfds(
1928 	libusb_context *ctx);
1929 void LIBUSB_CALL libusb_free_pollfds(const struct libusb_pollfd **pollfds);
1930 void LIBUSB_CALL libusb_set_pollfd_notifiers(libusb_context *ctx,
1931 	libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb,
1932 	void *user_data);
1933 
1934 /** \ingroup libusb_hotplug
1935  * Callback handle.
1936  *
1937  * Callbacks handles are generated by libusb_hotplug_register_callback()
1938  * and can be used to deregister callbacks. Callback handles are unique
1939  * per libusb_context and it is safe to call libusb_hotplug_deregister_callback()
1940  * on an already deregistered callback.
1941  *
1942  * Since version 1.0.16, \ref LIBUSB_API_VERSION >= 0x01000102
1943  *
1944  * For more information, see \ref libusb_hotplug.
1945  */
1946 typedef int libusb_hotplug_callback_handle;
1947 
1948 /** \ingroup libusb_hotplug
1949  *
1950  * Since version 1.0.16, \ref LIBUSB_API_VERSION >= 0x01000102
1951  *
1952  * Hotplug events */
1953 typedef enum {
1954 	/** A device has been plugged in and is ready to use */
1955 	LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED = (1 << 0),
1956 
1957 	/** A device has left and is no longer available.
1958 	 * It is the user's responsibility to call libusb_close on any handle associated with a disconnected device.
1959 	 * It is safe to call libusb_get_device_descriptor on a device that has left */
1960 	LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT = (1 << 1)
1961 } libusb_hotplug_event;
1962 
1963 /** \ingroup libusb_hotplug
1964  *
1965  * Since version 1.0.16, \ref LIBUSB_API_VERSION >= 0x01000102
1966  *
1967  * Hotplug flags */
1968 typedef enum {
1969 	/** Arm the callback and fire it for all matching currently attached devices. */
1970 	LIBUSB_HOTPLUG_ENUMERATE = (1 << 0)
1971 } libusb_hotplug_flag;
1972 
1973 /** \ingroup libusb_hotplug
1974  * Convenience macro when not using any flags */
1975 #define LIBUSB_HOTPLUG_NO_FLAGS 0
1976 
1977 /** \ingroup libusb_hotplug
1978  * Wildcard matching for hotplug events */
1979 #define LIBUSB_HOTPLUG_MATCH_ANY -1
1980 
1981 /** \ingroup libusb_hotplug
1982  * Hotplug callback function type. When requesting hotplug event notifications,
1983  * you pass a pointer to a callback function of this type.
1984  *
1985  * This callback may be called by an internal event thread and as such it is
1986  * recommended the callback do minimal processing before returning.
1987  *
1988  * libusb will call this function later, when a matching event had happened on
1989  * a matching device. See \ref libusb_hotplug for more information.
1990  *
1991  * It is safe to call either libusb_hotplug_register_callback() or
1992  * libusb_hotplug_deregister_callback() from within a callback function.
1993  *
1994  * Since version 1.0.16, \ref LIBUSB_API_VERSION >= 0x01000102
1995  *
1996  * \param ctx            context of this notification
1997  * \param device         libusb_device this event occurred on
1998  * \param event          event that occurred
1999  * \param user_data      user data provided when this callback was registered
2000  * \returns bool whether this callback is finished processing events.
2001  *                       returning 1 will cause this callback to be deregistered
2002  */
2003 typedef int (LIBUSB_CALL *libusb_hotplug_callback_fn)(libusb_context *ctx,
2004 	libusb_device *device, libusb_hotplug_event event, void *user_data);
2005 
2006 /** \ingroup libusb_hotplug
2007  * Register a hotplug callback function
2008  *
2009  * Register a callback with the libusb_context. The callback will fire
2010  * when a matching event occurs on a matching device. The callback is
2011  * armed until either it is deregistered with libusb_hotplug_deregister_callback()
2012  * or the supplied callback returns 1 to indicate it is finished processing events.
2013  *
2014  * If the \ref LIBUSB_HOTPLUG_ENUMERATE is passed the callback will be
2015  * called with a \ref LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED for all devices
2016  * already plugged into the machine. Note that libusb modifies its internal
2017  * device list from a separate thread, while calling hotplug callbacks from
2018  * libusb_handle_events(), so it is possible for a device to already be present
2019  * on, or removed from, its internal device list, while the hotplug callbacks
2020  * still need to be dispatched. This means that when using \ref
2021  * LIBUSB_HOTPLUG_ENUMERATE, your callback may be called twice for the arrival
2022  * of the same device, once from libusb_hotplug_register_callback() and once
2023  * from libusb_handle_events(); and/or your callback may be called for the
2024  * removal of a device for which an arrived call was never made.
2025  *
2026  * Since version 1.0.16, \ref LIBUSB_API_VERSION >= 0x01000102
2027  *
2028  * \param[in] ctx context to register this callback with
2029  * \param[in] events bitwise or of hotplug events that will trigger this callback.
2030  *            See \ref libusb_hotplug_event
2031  * \param[in] flags bitwise or of hotplug flags that affect registration.
2032  *            See \ref libusb_hotplug_flag
2033  * \param[in] vendor_id the vendor id to match or \ref LIBUSB_HOTPLUG_MATCH_ANY
2034  * \param[in] product_id the product id to match or \ref LIBUSB_HOTPLUG_MATCH_ANY
2035  * \param[in] dev_class the device class to match or \ref LIBUSB_HOTPLUG_MATCH_ANY
2036  * \param[in] cb_fn the function to be invoked on a matching event/device
2037  * \param[in] user_data user data to pass to the callback function
2038  * \param[out] callback_handle pointer to store the handle of the allocated callback (can be NULL)
2039  * \returns LIBUSB_SUCCESS on success LIBUSB_ERROR code on failure
2040  */
2041 int LIBUSB_CALL libusb_hotplug_register_callback(libusb_context *ctx,
2042 	int events, int flags,
2043 	int vendor_id, int product_id, int dev_class,
2044 	libusb_hotplug_callback_fn cb_fn, void *user_data,
2045 	libusb_hotplug_callback_handle *callback_handle);
2046 
2047 /** \ingroup libusb_hotplug
2048  * Deregisters a hotplug callback.
2049  *
2050  * Deregister a callback from a libusb_context. This function is safe to call from within
2051  * a hotplug callback.
2052  *
2053  * Since version 1.0.16, \ref LIBUSB_API_VERSION >= 0x01000102
2054  *
2055  * \param[in] ctx context this callback is registered with
2056  * \param[in] callback_handle the handle of the callback to deregister
2057  */
2058 void LIBUSB_CALL libusb_hotplug_deregister_callback(libusb_context *ctx,
2059 	libusb_hotplug_callback_handle callback_handle);
2060 
2061 /** \ingroup libusb_hotplug
2062  * Gets the user_data associated with a hotplug callback.
2063  *
2064  * Since version v1.0.24 \ref LIBUSB_API_VERSION >= 0x01000108
2065  *
2066  * \param[in] ctx context this callback is registered with
2067  * \param[in] callback_handle the handle of the callback to get the user_data of
2068  */
2069 void * LIBUSB_CALL libusb_hotplug_get_user_data(libusb_context *ctx,
2070 	libusb_hotplug_callback_handle callback_handle);
2071 
2072 /** \ingroup libusb_lib
2073  * Available option values for libusb_set_option().
2074  */
2075 enum libusb_option {
2076 	/** Set the log message verbosity.
2077 	 *
2078 	 * The default level is LIBUSB_LOG_LEVEL_NONE, which means no messages are ever
2079 	 * printed. If you choose to increase the message verbosity level, ensure
2080 	 * that your application does not close the stderr file descriptor.
2081 	 *
2082 	 * You are advised to use level LIBUSB_LOG_LEVEL_WARNING. libusb is conservative
2083 	 * with its message logging and most of the time, will only log messages that
2084 	 * explain error conditions and other oddities. This will help you debug
2085 	 * your software.
2086 	 *
2087 	 * If the LIBUSB_DEBUG environment variable was set when libusb was
2088 	 * initialized, this function does nothing: the message verbosity is fixed
2089 	 * to the value in the environment variable.
2090 	 *
2091 	 * If libusb was compiled without any message logging, this function does
2092 	 * nothing: you'll never get any messages.
2093 	 *
2094 	 * If libusb was compiled with verbose debug message logging, this function
2095 	 * does nothing: you'll always get messages from all levels.
2096 	 */
2097 	LIBUSB_OPTION_LOG_LEVEL = 0,
2098 
2099 	/** Use the UsbDk backend for a specific context, if available.
2100 	 *
2101 	 * This option should be set immediately after calling libusb_init(), otherwise
2102 	 * unspecified behavior may occur.
2103 	 *
2104 	 * Only valid on Windows.
2105 	 */
2106 	LIBUSB_OPTION_USE_USBDK = 1,
2107 
2108 	/** Do not scan for devices
2109 	 *
2110 	 * With this option set, libusb will skip scanning devices in
2111 	 * libusb_init(). Must be set before calling libusb_init().
2112 	 *
2113 	 * Hotplug functionality will also be deactivated.
2114 	 *
2115 	 * The option is useful in combination with libusb_wrap_sys_device(),
2116 	 * which can access a device directly without prior device scanning.
2117 	 *
2118 	 * This is typically needed on Android, where access to USB devices
2119 	 * is limited.
2120 	 *
2121 	 * For LIBUSB_API_VERSION 0x01000108 it was called LIBUSB_OPTION_WEAK_AUTHORITY
2122 	 *
2123 	 * Only valid on Linux.
2124 	 */
2125 	LIBUSB_OPTION_NO_DEVICE_DISCOVERY = 2,
2126 
2127 #define LIBUSB_OPTION_WEAK_AUTHORITY LIBUSB_OPTION_NO_DEVICE_DISCOVERY
2128 
2129 	LIBUSB_OPTION_MAX = 3
2130 };
2131 
2132 int LIBUSB_CALL libusb_set_option(libusb_context *ctx, enum libusb_option option, ...);
2133 
2134 #ifdef _MSC_VER
2135 #pragma warning(pop)
2136 #endif
2137 
2138 #if defined(__cplusplus)
2139 }
2140 #endif
2141 
2142 #endif
2143