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