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