linux: hotplug_enumerate and _disconnect take active_contexts_lock
[libusbx.git] / libusb / libusb.h
blob691587529ca9efddc11c0fcdf638e5b8ec708221
1 /*
2 * Public libusbx 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 Nathan Hjelm <hjelmn@cs.unm.edu>
7 * For more information, please visit: http://libusbx.org
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #ifndef LIBUSB_H
25 #define LIBUSB_H
27 #ifdef _MSC_VER
28 /* on MS environments, the inline keyword is available in C++ only */
29 #if !defined(__cplusplus)
30 #define inline __inline
31 #endif
32 /* ssize_t is also not available (copy/paste from MinGW) */
33 #ifndef _SSIZE_T_DEFINED
34 #define _SSIZE_T_DEFINED
35 #undef ssize_t
36 #ifdef _WIN64
37 typedef __int64 ssize_t;
38 #else
39 typedef int ssize_t;
40 #endif /* _WIN64 */
41 #endif /* _SSIZE_T_DEFINED */
42 #endif /* _MSC_VER */
44 /* stdint.h is not available on older MSVC */
45 #if defined(_MSC_VER) && (_MSC_VER < 1600) && (!defined(_STDINT)) && (!defined(_STDINT_H))
46 typedef unsigned __int8 uint8_t;
47 typedef unsigned __int16 uint16_t;
48 typedef unsigned __int32 uint32_t;
49 #else
50 #include <stdint.h>
51 #endif
53 #if !defined(_WIN32_WCE)
54 #include <sys/types.h>
55 #endif
57 #if defined(__linux) || defined(__APPLE__) || defined(__CYGWIN__)
58 #include <sys/time.h>
59 #endif
61 #include <time.h>
62 #include <limits.h>
64 /* 'interface' might be defined as a macro on Windows, so we need to
65 * undefine it so as not to break the current libusbx API, because
66 * libusb_config_descriptor has an 'interface' member
67 * As this can be problematic if you include windows.h after libusb.h
68 * in your sources, we force windows.h to be included first. */
69 #if defined(_WIN32) || defined(__CYGWIN__) || defined(_WIN32_WCE)
70 #include <windows.h>
71 #if defined(interface)
72 #undef interface
73 #endif
74 #if !defined(__CYGWIN__)
75 #include <winsock.h>
76 #endif
77 #endif
79 /** \def LIBUSB_CALL
80 * \ingroup misc
81 * libusbx's Windows calling convention.
83 * Under Windows, the selection of available compilers and configurations
84 * means that, unlike other platforms, there is not <em>one true calling
85 * convention</em> (calling convention: the manner in which parameters are
86 * passed to funcions in the generated assembly code).
88 * Matching the Windows API itself, libusbx uses the WINAPI convention (which
89 * translates to the <tt>stdcall</tt> convention) and guarantees that the
90 * library is compiled in this way. The public header file also includes
91 * appropriate annotations so that your own software will use the right
92 * convention, even if another convention is being used by default within
93 * your codebase.
95 * The one consideration that you must apply in your software is to mark
96 * all functions which you use as libusbx callbacks with this LIBUSB_CALL
97 * annotation, so that they too get compiled for the correct calling
98 * convention.
100 * On non-Windows operating systems, this macro is defined as nothing. This
101 * means that you can apply it to your code without worrying about
102 * cross-platform compatibility.
104 /* LIBUSB_CALL must be defined on both definition and declaration of libusbx
105 * functions. You'd think that declaration would be enough, but cygwin will
106 * complain about conflicting types unless both are marked this way.
107 * The placement of this macro is important too; it must appear after the
108 * return type, before the function name. See internal documentation for
109 * API_EXPORTED.
111 #if defined(_WIN32) || defined(__CYGWIN__) || defined(_WIN32_WCE)
112 #define LIBUSB_CALL WINAPI
113 #else
114 #define LIBUSB_CALL
115 #endif
117 /** \def LIBUSBX_API_VERSION
118 * \ingroup misc
119 * libusbx's API version.
121 * Since version 1.0.13, to help with feature detection, libusbx defines
122 * a LIBUSBX_API_VERSION macro that gets increased every time there is a
123 * significant change to the API, such as the introduction of a new call,
124 * the definition of a new macro/enum member, or any other element that
125 * libusbx applications may want to detect at compilation time.
127 * The macro is typically used in an application as follows:
128 * \code
129 * #if defined(LIBUSBX_API_VERSION) && (LIBUSBX_API_VERSION >= 0x01001234)
130 * // Use one of the newer features from the libusbx API
131 * #endif
132 * \endcode
134 * Another feature of LIBUSBX_API_VERSION is that it can be used to detect
135 * whether you are compiling against the libusb or the libusbx library.
137 * Internally, LIBUSBX_API_VERSION is defined as follows:
138 * (libusbx major << 24) | (libusbx minor << 16) | (16 bit incremental)
140 #define LIBUSBX_API_VERSION 0x01000102
142 #ifdef __cplusplus
143 extern "C" {
144 #endif
146 /** \def libusb_cpu_to_le16
147 * \ingroup misc
148 * Convert a 16-bit value from host-endian to little-endian format. On
149 * little endian systems, this function does nothing. On big endian systems,
150 * the bytes are swapped.
151 * \param x the host-endian value to convert
152 * \returns the value in little-endian byte order
154 static inline uint16_t libusb_cpu_to_le16(const uint16_t x)
156 union {
157 uint8_t b8[2];
158 uint16_t b16;
159 } _tmp;
160 _tmp.b8[1] = x >> 8;
161 _tmp.b8[0] = x & 0xff;
162 return _tmp.b16;
165 /** \def libusb_le16_to_cpu
166 * \ingroup misc
167 * Convert a 16-bit value from little-endian to host-endian format. On
168 * little endian systems, this function does nothing. On big endian systems,
169 * the bytes are swapped.
170 * \param x the little-endian value to convert
171 * \returns the value in host-endian byte order
173 #define libusb_le16_to_cpu libusb_cpu_to_le16
175 /* standard USB stuff */
177 /** \ingroup desc
178 * Device and/or Interface Class codes */
179 enum libusb_class_code {
180 /** In the context of a \ref libusb_device_descriptor "device descriptor",
181 * this bDeviceClass value indicates that each interface specifies its
182 * own class information and all interfaces operate independently.
184 LIBUSB_CLASS_PER_INTERFACE = 0,
186 /** Audio class */
187 LIBUSB_CLASS_AUDIO = 1,
189 /** Communications class */
190 LIBUSB_CLASS_COMM = 2,
192 /** Human Interface Device class */
193 LIBUSB_CLASS_HID = 3,
195 /** Physical */
196 LIBUSB_CLASS_PHYSICAL = 5,
198 /** Printer class */
199 LIBUSB_CLASS_PRINTER = 7,
201 /** Image class */
202 LIBUSB_CLASS_PTP = 6, /* legacy name from libusb-0.1 usb.h */
203 LIBUSB_CLASS_IMAGE = 6,
205 /** Mass storage class */
206 LIBUSB_CLASS_MASS_STORAGE = 8,
208 /** Hub class */
209 LIBUSB_CLASS_HUB = 9,
211 /** Data class */
212 LIBUSB_CLASS_DATA = 10,
214 /** Smart Card */
215 LIBUSB_CLASS_SMART_CARD = 0x0b,
217 /** Content Security */
218 LIBUSB_CLASS_CONTENT_SECURITY = 0x0d,
220 /** Video */
221 LIBUSB_CLASS_VIDEO = 0x0e,
223 /** Personal Healthcare */
224 LIBUSB_CLASS_PERSONAL_HEALTHCARE = 0x0f,
226 /** Diagnostic Device */
227 LIBUSB_CLASS_DIAGNOSTIC_DEVICE = 0xdc,
229 /** Wireless class */
230 LIBUSB_CLASS_WIRELESS = 0xe0,
232 /** Application class */
233 LIBUSB_CLASS_APPLICATION = 0xfe,
235 /** Class is vendor-specific */
236 LIBUSB_CLASS_VENDOR_SPEC = 0xff
239 /** \ingroup desc
240 * Descriptor types as defined by the USB specification. */
241 enum libusb_descriptor_type {
242 /** Device descriptor. See libusb_device_descriptor. */
243 LIBUSB_DT_DEVICE = 0x01,
245 /** Configuration descriptor. See libusb_config_descriptor. */
246 LIBUSB_DT_CONFIG = 0x02,
248 /** String descriptor */
249 LIBUSB_DT_STRING = 0x03,
251 /** Interface descriptor. See libusb_interface_descriptor. */
252 LIBUSB_DT_INTERFACE = 0x04,
254 /** Endpoint descriptor. See libusb_endpoint_descriptor. */
255 LIBUSB_DT_ENDPOINT = 0x05,
257 /** HID descriptor */
258 LIBUSB_DT_HID = 0x21,
260 /** HID report descriptor */
261 LIBUSB_DT_REPORT = 0x22,
263 /** Physical descriptor */
264 LIBUSB_DT_PHYSICAL = 0x23,
266 /** Hub descriptor */
267 LIBUSB_DT_HUB = 0x29,
269 /** SuperSpeed Hub descriptor */
270 LIBUSB_DT_SUPERSPEED_HUB = 0x2A,
273 /* Descriptor sizes per descriptor type */
274 #define LIBUSB_DT_DEVICE_SIZE 18
275 #define LIBUSB_DT_CONFIG_SIZE 9
276 #define LIBUSB_DT_INTERFACE_SIZE 9
277 #define LIBUSB_DT_ENDPOINT_SIZE 7
278 #define LIBUSB_DT_ENDPOINT_AUDIO_SIZE 9 /* Audio extension */
279 #define LIBUSB_DT_HUB_NONVAR_SIZE 7
281 #define LIBUSB_ENDPOINT_ADDRESS_MASK 0x0f /* in bEndpointAddress */
282 #define LIBUSB_ENDPOINT_DIR_MASK 0x80
284 /** \ingroup desc
285 * Endpoint direction. Values for bit 7 of the
286 * \ref libusb_endpoint_descriptor::bEndpointAddress "endpoint address" scheme.
288 enum libusb_endpoint_direction {
289 /** In: device-to-host */
290 LIBUSB_ENDPOINT_IN = 0x80,
292 /** Out: host-to-device */
293 LIBUSB_ENDPOINT_OUT = 0x00
296 #define LIBUSB_TRANSFER_TYPE_MASK 0x03 /* in bmAttributes */
298 /** \ingroup desc
299 * Endpoint transfer type. Values for bits 0:1 of the
300 * \ref libusb_endpoint_descriptor::bmAttributes "endpoint attributes" field.
302 enum libusb_transfer_type {
303 /** Control endpoint */
304 LIBUSB_TRANSFER_TYPE_CONTROL = 0,
306 /** Isochronous endpoint */
307 LIBUSB_TRANSFER_TYPE_ISOCHRONOUS = 1,
309 /** Bulk endpoint */
310 LIBUSB_TRANSFER_TYPE_BULK = 2,
312 /** Interrupt endpoint */
313 LIBUSB_TRANSFER_TYPE_INTERRUPT = 3
316 /** \ingroup misc
317 * Standard requests, as defined in table 9-5 of the USB 3.0 specifications */
318 enum libusb_standard_request {
319 /** Request status of the specific recipient */
320 LIBUSB_REQUEST_GET_STATUS = 0x00,
322 /** Clear or disable a specific feature */
323 LIBUSB_REQUEST_CLEAR_FEATURE = 0x01,
325 /* 0x02 is reserved */
327 /** Set or enable a specific feature */
328 LIBUSB_REQUEST_SET_FEATURE = 0x03,
330 /* 0x04 is reserved */
332 /** Set device address for all future accesses */
333 LIBUSB_REQUEST_SET_ADDRESS = 0x05,
335 /** Get the specified descriptor */
336 LIBUSB_REQUEST_GET_DESCRIPTOR = 0x06,
338 /** Used to update existing descriptors or add new descriptors */
339 LIBUSB_REQUEST_SET_DESCRIPTOR = 0x07,
341 /** Get the current device configuration value */
342 LIBUSB_REQUEST_GET_CONFIGURATION = 0x08,
344 /** Set device configuration */
345 LIBUSB_REQUEST_SET_CONFIGURATION = 0x09,
347 /** Return the selected alternate setting for the specified interface */
348 LIBUSB_REQUEST_GET_INTERFACE = 0x0A,
350 /** Select an alternate interface for the specified interface */
351 LIBUSB_REQUEST_SET_INTERFACE = 0x0B,
353 /** Set then report an endpoint's synchronization frame */
354 LIBUSB_REQUEST_SYNCH_FRAME = 0x0C,
356 /** Sets both the U1 and U2 Exit Latency */
357 LIBUSB_REQUEST_SET_SEL = 0x30,
359 /** Delay from the time a host transmits a packet to the time it is
360 * received by the device. */
361 LIBUSB_SET_ISOCH_DELAY = 0x31,
364 /** \ingroup misc
365 * Request type bits of the
366 * \ref libusb_control_setup::bmRequestType "bmRequestType" field in control
367 * transfers. */
368 enum libusb_request_type {
369 /** Standard */
370 LIBUSB_REQUEST_TYPE_STANDARD = (0x00 << 5),
372 /** Class */
373 LIBUSB_REQUEST_TYPE_CLASS = (0x01 << 5),
375 /** Vendor */
376 LIBUSB_REQUEST_TYPE_VENDOR = (0x02 << 5),
378 /** Reserved */
379 LIBUSB_REQUEST_TYPE_RESERVED = (0x03 << 5)
382 /** \ingroup misc
383 * Recipient bits of the
384 * \ref libusb_control_setup::bmRequestType "bmRequestType" field in control
385 * transfers. Values 4 through 31 are reserved. */
386 enum libusb_request_recipient {
387 /** Device */
388 LIBUSB_RECIPIENT_DEVICE = 0x00,
390 /** Interface */
391 LIBUSB_RECIPIENT_INTERFACE = 0x01,
393 /** Endpoint */
394 LIBUSB_RECIPIENT_ENDPOINT = 0x02,
396 /** Other */
397 LIBUSB_RECIPIENT_OTHER = 0x03,
400 #define LIBUSB_ISO_SYNC_TYPE_MASK 0x0C
402 /** \ingroup desc
403 * Synchronization type for isochronous endpoints. Values for bits 2:3 of the
404 * \ref libusb_endpoint_descriptor::bmAttributes "bmAttributes" field in
405 * libusb_endpoint_descriptor.
407 enum libusb_iso_sync_type {
408 /** No synchronization */
409 LIBUSB_ISO_SYNC_TYPE_NONE = 0,
411 /** Asynchronous */
412 LIBUSB_ISO_SYNC_TYPE_ASYNC = 1,
414 /** Adaptive */
415 LIBUSB_ISO_SYNC_TYPE_ADAPTIVE = 2,
417 /** Synchronous */
418 LIBUSB_ISO_SYNC_TYPE_SYNC = 3
421 #define LIBUSB_ISO_USAGE_TYPE_MASK 0x30
423 /** \ingroup desc
424 * Usage type for isochronous endpoints. Values for bits 4:5 of the
425 * \ref libusb_endpoint_descriptor::bmAttributes "bmAttributes" field in
426 * libusb_endpoint_descriptor.
428 enum libusb_iso_usage_type {
429 /** Data endpoint */
430 LIBUSB_ISO_USAGE_TYPE_DATA = 0,
432 /** Feedback endpoint */
433 LIBUSB_ISO_USAGE_TYPE_FEEDBACK = 1,
435 /** Implicit feedback Data endpoint */
436 LIBUSB_ISO_USAGE_TYPE_IMPLICIT = 2,
439 /** \ingroup desc
440 * A structure representing the standard USB device descriptor. This
441 * descriptor is documented in section 9.6.1 of the USB 3.0 specification.
442 * All multiple-byte fields are represented in host-endian format.
444 struct libusb_device_descriptor {
445 /** Size of this descriptor (in bytes) */
446 uint8_t bLength;
448 /** Descriptor type. Will have value
449 * \ref libusb_descriptor_type::LIBUSB_DT_DEVICE LIBUSB_DT_DEVICE in this
450 * context. */
451 uint8_t bDescriptorType;
453 /** USB specification release number in binary-coded decimal. A value of
454 * 0x0200 indicates USB 2.0, 0x0110 indicates USB 1.1, etc. */
455 uint16_t bcdUSB;
457 /** USB-IF class code for the device. See \ref libusb_class_code. */
458 uint8_t bDeviceClass;
460 /** USB-IF subclass code for the device, qualified by the bDeviceClass
461 * value */
462 uint8_t bDeviceSubClass;
464 /** USB-IF protocol code for the device, qualified by the bDeviceClass and
465 * bDeviceSubClass values */
466 uint8_t bDeviceProtocol;
468 /** Maximum packet size for endpoint 0 */
469 uint8_t bMaxPacketSize0;
471 /** USB-IF vendor ID */
472 uint16_t idVendor;
474 /** USB-IF product ID */
475 uint16_t idProduct;
477 /** Device release number in binary-coded decimal */
478 uint16_t bcdDevice;
480 /** Index of string descriptor describing manufacturer */
481 uint8_t iManufacturer;
483 /** Index of string descriptor describing product */
484 uint8_t iProduct;
486 /** Index of string descriptor containing device serial number */
487 uint8_t iSerialNumber;
489 /** Number of possible configurations */
490 uint8_t bNumConfigurations;
493 /** \ingroup desc
494 * A structure representing the standard USB endpoint descriptor. This
495 * descriptor is documented in section 9.6.6 of the USB 3.0 specification.
496 * All multiple-byte fields are represented in host-endian format.
498 struct libusb_endpoint_descriptor {
499 /** Size of this descriptor (in bytes) */
500 uint8_t bLength;
502 /** Descriptor type. Will have value
503 * \ref libusb_descriptor_type::LIBUSB_DT_ENDPOINT LIBUSB_DT_ENDPOINT in
504 * this context. */
505 uint8_t bDescriptorType;
507 /** The address of the endpoint described by this descriptor. Bits 0:3 are
508 * the endpoint number. Bits 4:6 are reserved. Bit 7 indicates direction,
509 * see \ref libusb_endpoint_direction.
511 uint8_t bEndpointAddress;
513 /** Attributes which apply to the endpoint when it is configured using
514 * the bConfigurationValue. Bits 0:1 determine the transfer type and
515 * correspond to \ref libusb_transfer_type. Bits 2:3 are only used for
516 * isochronous endpoints and correspond to \ref libusb_iso_sync_type.
517 * Bits 4:5 are also only used for isochronous endpoints and correspond to
518 * \ref libusb_iso_usage_type. Bits 6:7 are reserved.
520 uint8_t bmAttributes;
522 /** Maximum packet size this endpoint is capable of sending/receiving. */
523 uint16_t wMaxPacketSize;
525 /** Interval for polling endpoint for data transfers. */
526 uint8_t bInterval;
528 /** For audio devices only: the rate at which synchronization feedback
529 * is provided. */
530 uint8_t bRefresh;
532 /** For audio devices only: the address if the synch endpoint */
533 uint8_t bSynchAddress;
535 /** Extra descriptors. If libusbx encounters unknown endpoint descriptors,
536 * it will store them here, should you wish to parse them. */
537 const unsigned char *extra;
539 /** Length of the extra descriptors, in bytes. */
540 int extra_length;
543 /** \ingroup desc
544 * A structure representing the standard USB interface descriptor. This
545 * descriptor is documented in section 9.6.5 of the USB 3.0 specification.
546 * All multiple-byte fields are represented in host-endian format.
548 struct libusb_interface_descriptor {
549 /** Size of this descriptor (in bytes) */
550 uint8_t bLength;
552 /** Descriptor type. Will have value
553 * \ref libusb_descriptor_type::LIBUSB_DT_INTERFACE LIBUSB_DT_INTERFACE
554 * in this context. */
555 uint8_t bDescriptorType;
557 /** Number of this interface */
558 uint8_t bInterfaceNumber;
560 /** Value used to select this alternate setting for this interface */
561 uint8_t bAlternateSetting;
563 /** Number of endpoints used by this interface (excluding the control
564 * endpoint). */
565 uint8_t bNumEndpoints;
567 /** USB-IF class code for this interface. See \ref libusb_class_code. */
568 uint8_t bInterfaceClass;
570 /** USB-IF subclass code for this interface, qualified by the
571 * bInterfaceClass value */
572 uint8_t bInterfaceSubClass;
574 /** USB-IF protocol code for this interface, qualified by the
575 * bInterfaceClass and bInterfaceSubClass values */
576 uint8_t bInterfaceProtocol;
578 /** Index of string descriptor describing this interface */
579 uint8_t iInterface;
581 /** Array of endpoint descriptors. This length of this array is determined
582 * by the bNumEndpoints field. */
583 const struct libusb_endpoint_descriptor *endpoint;
585 /** Extra descriptors. If libusbx encounters unknown interface descriptors,
586 * it will store them here, should you wish to parse them. */
587 const unsigned char *extra;
589 /** Length of the extra descriptors, in bytes. */
590 int extra_length;
593 /** \ingroup desc
594 * A collection of alternate settings for a particular USB interface.
596 struct libusb_interface {
597 /** Array of interface descriptors. The length of this array is determined
598 * by the num_altsetting field. */
599 const struct libusb_interface_descriptor *altsetting;
601 /** The number of alternate settings that belong to this interface */
602 int num_altsetting;
605 /** \ingroup desc
606 * A structure representing the standard USB configuration descriptor. This
607 * descriptor is documented in section 9.6.3 of the USB 3.0 specification.
608 * All multiple-byte fields are represented in host-endian format.
610 struct libusb_config_descriptor {
611 /** Size of this descriptor (in bytes) */
612 uint8_t bLength;
614 /** Descriptor type. Will have value
615 * \ref libusb_descriptor_type::LIBUSB_DT_CONFIG LIBUSB_DT_CONFIG
616 * in this context. */
617 uint8_t bDescriptorType;
619 /** Total length of data returned for this configuration */
620 uint16_t wTotalLength;
622 /** Number of interfaces supported by this configuration */
623 uint8_t bNumInterfaces;
625 /** Identifier value for this configuration */
626 uint8_t bConfigurationValue;
628 /** Index of string descriptor describing this configuration */
629 uint8_t iConfiguration;
631 /** Configuration characteristics */
632 uint8_t bmAttributes;
634 /** Maximum power consumption of the USB device from this bus in this
635 * configuration when the device is fully opreation. Expressed in units
636 * of 2 mA. */
637 uint8_t MaxPower;
639 /** Array of interfaces supported by this configuration. The length of
640 * this array is determined by the bNumInterfaces field. */
641 const struct libusb_interface *interface;
643 /** Extra descriptors. If libusbx encounters unknown configuration
644 * descriptors, it will store them here, should you wish to parse them. */
645 const unsigned char *extra;
647 /** Length of the extra descriptors, in bytes. */
648 int extra_length;
651 /** \ingroup asyncio
652 * Setup packet for control transfers. */
653 struct libusb_control_setup {
654 /** Request type. Bits 0:4 determine recipient, see
655 * \ref libusb_request_recipient. Bits 5:6 determine type, see
656 * \ref libusb_request_type. Bit 7 determines data transfer direction, see
657 * \ref libusb_endpoint_direction.
659 uint8_t bmRequestType;
661 /** Request. If the type bits of bmRequestType are equal to
662 * \ref libusb_request_type::LIBUSB_REQUEST_TYPE_STANDARD
663 * "LIBUSB_REQUEST_TYPE_STANDARD" then this field refers to
664 * \ref libusb_standard_request. For other cases, use of this field is
665 * application-specific. */
666 uint8_t bRequest;
668 /** Value. Varies according to request */
669 uint16_t wValue;
671 /** Index. Varies according to request, typically used to pass an index
672 * or offset */
673 uint16_t wIndex;
675 /** Number of bytes to transfer */
676 uint16_t wLength;
679 #define LIBUSB_CONTROL_SETUP_SIZE (sizeof(struct libusb_control_setup))
681 /* libusbx */
683 struct libusb_context;
684 struct libusb_device;
685 struct libusb_device_handle;
686 struct libusb_hotplug_callback;
688 /** \ingroup lib
689 * Structure providing the version of the libusbx runtime
691 struct libusb_version {
692 /** Library major version. */
693 const uint16_t major;
695 /** Library minor version. */
696 const uint16_t minor;
698 /** Library micro version. */
699 const uint16_t micro;
701 /** Library nano version. */
702 const uint16_t nano;
704 /** Library release candidate suffix string, e.g. "-rc4". */
705 const char *rc;
707 /** For ABI compatibility only. */
708 const char* describe;
711 /** \ingroup lib
712 * Structure representing a libusbx session. The concept of individual libusbx
713 * sessions allows for your program to use two libraries (or dynamically
714 * load two modules) which both independently use libusb. This will prevent
715 * interference between the individual libusbx users - for example
716 * libusb_set_debug() will not affect the other user of the library, and
717 * libusb_exit() will not destroy resources that the other user is still
718 * using.
720 * Sessions are created by libusb_init() and destroyed through libusb_exit().
721 * If your application is guaranteed to only ever include a single libusbx
722 * user (i.e. you), you do not have to worry about contexts: pass NULL in
723 * every function call where a context is required. The default context
724 * will be used.
726 * For more information, see \ref contexts.
728 typedef struct libusb_context libusb_context;
730 /** \ingroup dev
731 * Structure representing a USB device detected on the system. This is an
732 * opaque type for which you are only ever provided with a pointer, usually
733 * originating from libusb_get_device_list().
735 * Certain operations can be performed on a device, but in order to do any
736 * I/O you will have to first obtain a device handle using libusb_open().
738 * Devices are reference counted with libusb_ref_device() and
739 * libusb_unref_device(), and are freed when the reference count reaches 0.
740 * New devices presented by libusb_get_device_list() have a reference count of
741 * 1, and libusb_free_device_list() can optionally decrease the reference count
742 * on all devices in the list. libusb_open() adds another reference which is
743 * later destroyed by libusb_close().
745 typedef struct libusb_device libusb_device;
748 /** \ingroup dev
749 * Structure representing a handle on a USB device. This is an opaque type for
750 * which you are only ever provided with a pointer, usually originating from
751 * libusb_open().
753 * A device handle is used to perform I/O and other operations. When finished
754 * with a device handle, you should call libusb_close().
756 typedef struct libusb_device_handle libusb_device_handle;
758 /** \ingroup dev
759 * Speed codes. Indicates the speed at which the device is operating.
761 enum libusb_speed {
762 /** The OS doesn't report or know the device speed. */
763 LIBUSB_SPEED_UNKNOWN = 0,
765 /** The device is operating at low speed (1.5MBit/s). */
766 LIBUSB_SPEED_LOW = 1,
768 /** The device is operating at full speed (12MBit/s). */
769 LIBUSB_SPEED_FULL = 2,
771 /** The device is operating at high speed (480MBit/s). */
772 LIBUSB_SPEED_HIGH = 3,
774 /** The device is operating at super speed (5000MBit/s). */
775 LIBUSB_SPEED_SUPER = 4,
778 /** \ingroup misc
779 * Error codes. Most libusbx functions return 0 on success or one of these
780 * codes on failure.
781 * You can call \ref libusb_error_name() to retrieve a string representation
782 * of an error code.
784 enum libusb_error {
785 /** Success (no error) */
786 LIBUSB_SUCCESS = 0,
788 /** Input/output error */
789 LIBUSB_ERROR_IO = -1,
791 /** Invalid parameter */
792 LIBUSB_ERROR_INVALID_PARAM = -2,
794 /** Access denied (insufficient permissions) */
795 LIBUSB_ERROR_ACCESS = -3,
797 /** No such device (it may have been disconnected) */
798 LIBUSB_ERROR_NO_DEVICE = -4,
800 /** Entity not found */
801 LIBUSB_ERROR_NOT_FOUND = -5,
803 /** Resource busy */
804 LIBUSB_ERROR_BUSY = -6,
806 /** Operation timed out */
807 LIBUSB_ERROR_TIMEOUT = -7,
809 /** Overflow */
810 LIBUSB_ERROR_OVERFLOW = -8,
812 /** Pipe error */
813 LIBUSB_ERROR_PIPE = -9,
815 /** System call interrupted (perhaps due to signal) */
816 LIBUSB_ERROR_INTERRUPTED = -10,
818 /** Insufficient memory */
819 LIBUSB_ERROR_NO_MEM = -11,
821 /** Operation not supported or unimplemented on this platform */
822 LIBUSB_ERROR_NOT_SUPPORTED = -12,
824 /* NB! Remember to update libusb_error_name()
825 when adding new error codes here. */
827 /** Other error */
828 LIBUSB_ERROR_OTHER = -99,
831 /** \ingroup asyncio
832 * Transfer status codes */
833 enum libusb_transfer_status {
834 /** Transfer completed without error. Note that this does not indicate
835 * that the entire amount of requested data was transferred. */
836 LIBUSB_TRANSFER_COMPLETED,
838 /** Transfer failed */
839 LIBUSB_TRANSFER_ERROR,
841 /** Transfer timed out */
842 LIBUSB_TRANSFER_TIMED_OUT,
844 /** Transfer was cancelled */
845 LIBUSB_TRANSFER_CANCELLED,
847 /** For bulk/interrupt endpoints: halt condition detected (endpoint
848 * stalled). For control endpoints: control request not supported. */
849 LIBUSB_TRANSFER_STALL,
851 /** Device was disconnected */
852 LIBUSB_TRANSFER_NO_DEVICE,
854 /** Device sent more data than requested */
855 LIBUSB_TRANSFER_OVERFLOW,
857 /* NB! Remember to update libusb_error_name()
858 when adding new status codes here. */
861 /** \ingroup asyncio
862 * libusb_transfer.flags values */
863 enum libusb_transfer_flags {
864 /** Report short frames as errors */
865 LIBUSB_TRANSFER_SHORT_NOT_OK = 1<<0,
867 /** Automatically free() transfer buffer during libusb_free_transfer() */
868 LIBUSB_TRANSFER_FREE_BUFFER = 1<<1,
870 /** Automatically call libusb_free_transfer() after callback returns.
871 * If this flag is set, it is illegal to call libusb_free_transfer()
872 * from your transfer callback, as this will result in a double-free
873 * when this flag is acted upon. */
874 LIBUSB_TRANSFER_FREE_TRANSFER = 1<<2,
876 /** Terminate transfers that are a multiple of the endpoint's
877 * wMaxPacketSize with an extra zero length packet. This is useful
878 * when a device protocol mandates that each logical request is
879 * terminated by an incomplete packet (i.e. the logical requests are
880 * not separated by other means).
882 * This flag only affects host-to-device transfers to bulk and interrupt
883 * endpoints. In other situations, it is ignored.
885 * This flag only affects transfers with a length that is a multiple of
886 * the endpoint's wMaxPacketSize. On transfers of other lengths, this
887 * flag has no effect. Therefore, if you are working with a device that
888 * needs a ZLP whenever the end of the logical request falls on a packet
889 * boundary, then it is sensible to set this flag on <em>every</em>
890 * transfer (you do not have to worry about only setting it on transfers
891 * that end on the boundary).
893 * This flag is currently only supported on Linux.
894 * On other systems, libusb_submit_transfer() will return
895 * LIBUSB_ERROR_NOT_SUPPORTED for every transfer where this flag is set.
897 * Available since libusb-1.0.9.
899 LIBUSB_TRANSFER_ADD_ZERO_PACKET = 1 << 3,
902 /** \ingroup asyncio
903 * Isochronous packet descriptor. */
904 struct libusb_iso_packet_descriptor {
905 /** Length of data to request in this packet */
906 unsigned int length;
908 /** Amount of data that was actually transferred */
909 unsigned int actual_length;
911 /** Status code for this packet */
912 enum libusb_transfer_status status;
915 struct libusb_transfer;
917 /** \ingroup asyncio
918 * Asynchronous transfer callback function type. When submitting asynchronous
919 * transfers, you pass a pointer to a callback function of this type via the
920 * \ref libusb_transfer::callback "callback" member of the libusb_transfer
921 * structure. libusbx will call this function later, when the transfer has
922 * completed or failed. See \ref asyncio for more information.
923 * \param transfer The libusb_transfer struct the callback function is being
924 * notified about.
926 typedef void (LIBUSB_CALL *libusb_transfer_cb_fn)(struct libusb_transfer *transfer);
928 /** \ingroup asyncio
929 * The generic USB transfer structure. The user populates this structure and
930 * then submits it in order to request a transfer. After the transfer has
931 * completed, the library populates the transfer with the results and passes
932 * it back to the user.
934 struct libusb_transfer {
935 /** Handle of the device that this transfer will be submitted to */
936 libusb_device_handle *dev_handle;
938 /** A bitwise OR combination of \ref libusb_transfer_flags. */
939 uint8_t flags;
941 /** Address of the endpoint where this transfer will be sent. */
942 unsigned char endpoint;
944 /** Type of the endpoint from \ref libusb_transfer_type */
945 unsigned char type;
947 /** Timeout for this transfer in millseconds. A value of 0 indicates no
948 * timeout. */
949 unsigned int timeout;
951 /** The status of the transfer. Read-only, and only for use within
952 * transfer callback function.
954 * If this is an isochronous transfer, this field may read COMPLETED even
955 * if there were errors in the frames. Use the
956 * \ref libusb_iso_packet_descriptor::status "status" field in each packet
957 * to determine if errors occurred. */
958 enum libusb_transfer_status status;
960 /** Length of the data buffer */
961 int length;
963 /** Actual length of data that was transferred. Read-only, and only for
964 * use within transfer callback function. Not valid for isochronous
965 * endpoint transfers. */
966 int actual_length;
968 /** Callback function. This will be invoked when the transfer completes,
969 * fails, or is cancelled. */
970 libusb_transfer_cb_fn callback;
972 /** User context data to pass to the callback function. */
973 void *user_data;
975 /** Data buffer */
976 unsigned char *buffer;
978 /** Number of isochronous packets. Only used for I/O with isochronous
979 * endpoints. */
980 int num_iso_packets;
982 /** Isochronous packet descriptors, for isochronous transfers only. */
983 struct libusb_iso_packet_descriptor iso_packet_desc
984 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
985 [] /* valid C99 code */
986 #else
987 [0] /* non-standard, but usually working code */
988 #endif
992 /** \ingroup misc
993 * Capabilities supported by this instance of libusb. Test if the loaded
994 * library supports a given capability by calling
995 * \ref libusb_has_capability().
997 enum libusb_capability {
998 /** The libusb_has_capability() API is available. */
999 LIBUSB_CAP_HAS_CAPABILITY = 0x0000,
1000 /** Hotplug support is available. */
1001 LIBUSB_CAP_HAS_HOTPLUG = 0x0001,
1002 /** The library can access HID devices without requiring user intervention.
1003 * Note that before being able to actually access an HID device, you may
1004 * still have to call additional libusbx functions such as
1005 * \ref libusb_detach_kernel_driver(). */
1006 LIBUSB_CAP_HAS_HID_ACCESS = 0x0100,
1007 /** The library supports detaching of the default USB driver, using
1008 * \ref libusb_detach_kernel_driver(), if one is set by the OS kernel */
1009 LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER = 0x0101
1012 /** \ingroup lib
1013 * Log message levels.
1014 * - LIBUSB_LOG_LEVEL_NONE (0) : no messages ever printed by the library (default)
1015 * - LIBUSB_LOG_LEVEL_ERROR (1) : error messages are printed to stderr
1016 * - LIBUSB_LOG_LEVEL_WARNING (2) : warning and error messages are printed to stderr
1017 * - LIBUSB_LOG_LEVEL_INFO (3) : informational messages are printed to stdout, warning
1018 * and error messages are printed to stderr
1019 * - LIBUSB_LOG_LEVEL_DEBUG (4) : debug and informational messages are printed to stdout,
1020 * warnings and errors to stderr
1022 enum libusb_log_level {
1023 LIBUSB_LOG_LEVEL_NONE = 0,
1024 LIBUSB_LOG_LEVEL_ERROR,
1025 LIBUSB_LOG_LEVEL_WARNING,
1026 LIBUSB_LOG_LEVEL_INFO,
1027 LIBUSB_LOG_LEVEL_DEBUG,
1030 int LIBUSB_CALL libusb_init(libusb_context **ctx);
1031 void LIBUSB_CALL libusb_exit(libusb_context *ctx);
1032 void LIBUSB_CALL libusb_set_debug(libusb_context *ctx, int level);
1033 const struct libusb_version * LIBUSB_CALL libusb_get_version(void);
1034 int LIBUSB_CALL libusb_has_capability(uint32_t capability);
1035 const char * LIBUSB_CALL libusb_error_name(int errcode);
1037 ssize_t LIBUSB_CALL libusb_get_device_list(libusb_context *ctx,
1038 libusb_device ***list);
1039 void LIBUSB_CALL libusb_free_device_list(libusb_device **list,
1040 int unref_devices);
1041 libusb_device * LIBUSB_CALL libusb_ref_device(libusb_device *dev);
1042 void LIBUSB_CALL libusb_unref_device(libusb_device *dev);
1044 int LIBUSB_CALL libusb_get_configuration(libusb_device_handle *dev,
1045 int *config);
1046 int LIBUSB_CALL libusb_get_device_descriptor(libusb_device *dev,
1047 struct libusb_device_descriptor *desc);
1048 int LIBUSB_CALL libusb_get_active_config_descriptor(libusb_device *dev,
1049 struct libusb_config_descriptor **config);
1050 int LIBUSB_CALL libusb_get_config_descriptor(libusb_device *dev,
1051 uint8_t config_index, struct libusb_config_descriptor **config);
1052 int LIBUSB_CALL libusb_get_config_descriptor_by_value(libusb_device *dev,
1053 uint8_t bConfigurationValue, struct libusb_config_descriptor **config);
1054 void LIBUSB_CALL libusb_free_config_descriptor(
1055 struct libusb_config_descriptor *config);
1056 uint8_t LIBUSB_CALL libusb_get_bus_number(libusb_device *dev);
1057 uint8_t LIBUSB_CALL libusb_get_port_number(libusb_device *dev);
1058 libusb_device * LIBUSB_CALL libusb_get_parent(libusb_device *dev);
1059 int LIBUSB_CALL libusb_get_port_path(libusb_context *ctx, libusb_device *dev, uint8_t* path, uint8_t path_length);
1060 uint8_t LIBUSB_CALL libusb_get_device_address(libusb_device *dev);
1061 int LIBUSB_CALL libusb_get_device_speed(libusb_device *dev);
1062 int LIBUSB_CALL libusb_get_max_packet_size(libusb_device *dev,
1063 unsigned char endpoint);
1064 int LIBUSB_CALL libusb_get_max_iso_packet_size(libusb_device *dev,
1065 unsigned char endpoint);
1067 int LIBUSB_CALL libusb_open(libusb_device *dev, libusb_device_handle **handle);
1068 void LIBUSB_CALL libusb_close(libusb_device_handle *dev_handle);
1069 libusb_device * LIBUSB_CALL libusb_get_device(libusb_device_handle *dev_handle);
1071 int LIBUSB_CALL libusb_set_configuration(libusb_device_handle *dev,
1072 int configuration);
1073 int LIBUSB_CALL libusb_claim_interface(libusb_device_handle *dev,
1074 int interface_number);
1075 int LIBUSB_CALL libusb_release_interface(libusb_device_handle *dev,
1076 int interface_number);
1078 libusb_device_handle * LIBUSB_CALL libusb_open_device_with_vid_pid(
1079 libusb_context *ctx, uint16_t vendor_id, uint16_t product_id);
1081 int LIBUSB_CALL libusb_set_interface_alt_setting(libusb_device_handle *dev,
1082 int interface_number, int alternate_setting);
1083 int LIBUSB_CALL libusb_clear_halt(libusb_device_handle *dev,
1084 unsigned char endpoint);
1085 int LIBUSB_CALL libusb_reset_device(libusb_device_handle *dev);
1087 int LIBUSB_CALL libusb_kernel_driver_active(libusb_device_handle *dev,
1088 int interface_number);
1089 int LIBUSB_CALL libusb_detach_kernel_driver(libusb_device_handle *dev,
1090 int interface_number);
1091 int LIBUSB_CALL libusb_attach_kernel_driver(libusb_device_handle *dev,
1092 int interface_number);
1094 /* async I/O */
1096 /** \ingroup asyncio
1097 * Get the data section of a control transfer. This convenience function is here
1098 * to remind you that the data does not start until 8 bytes into the actual
1099 * buffer, as the setup packet comes first.
1101 * Calling this function only makes sense from a transfer callback function,
1102 * or situations where you have already allocated a suitably sized buffer at
1103 * transfer->buffer.
1105 * \param transfer a transfer
1106 * \returns pointer to the first byte of the data section
1108 static inline unsigned char *libusb_control_transfer_get_data(
1109 struct libusb_transfer *transfer)
1111 return transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE;
1114 /** \ingroup asyncio
1115 * Get the control setup packet of a control transfer. This convenience
1116 * function is here to remind you that the control setup occupies the first
1117 * 8 bytes of the transfer data buffer.
1119 * Calling this function only makes sense from a transfer callback function,
1120 * or situations where you have already allocated a suitably sized buffer at
1121 * transfer->buffer.
1123 * \param transfer a transfer
1124 * \returns a casted pointer to the start of the transfer data buffer
1126 static inline struct libusb_control_setup *libusb_control_transfer_get_setup(
1127 struct libusb_transfer *transfer)
1129 return (struct libusb_control_setup *) transfer->buffer;
1132 /** \ingroup asyncio
1133 * Helper function to populate the setup packet (first 8 bytes of the data
1134 * buffer) for a control transfer. The wIndex, wValue and wLength values should
1135 * be given in host-endian byte order.
1137 * \param buffer buffer to output the setup packet into
1138 * \param bmRequestType see the
1139 * \ref libusb_control_setup::bmRequestType "bmRequestType" field of
1140 * \ref libusb_control_setup
1141 * \param bRequest see the
1142 * \ref libusb_control_setup::bRequest "bRequest" field of
1143 * \ref libusb_control_setup
1144 * \param wValue see the
1145 * \ref libusb_control_setup::wValue "wValue" field of
1146 * \ref libusb_control_setup
1147 * \param wIndex see the
1148 * \ref libusb_control_setup::wIndex "wIndex" field of
1149 * \ref libusb_control_setup
1150 * \param wLength see the
1151 * \ref libusb_control_setup::wLength "wLength" field of
1152 * \ref libusb_control_setup
1154 static inline void libusb_fill_control_setup(unsigned char *buffer,
1155 uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
1156 uint16_t wLength)
1158 struct libusb_control_setup *setup = (struct libusb_control_setup *) buffer;
1159 setup->bmRequestType = bmRequestType;
1160 setup->bRequest = bRequest;
1161 setup->wValue = libusb_cpu_to_le16(wValue);
1162 setup->wIndex = libusb_cpu_to_le16(wIndex);
1163 setup->wLength = libusb_cpu_to_le16(wLength);
1166 struct libusb_transfer * LIBUSB_CALL libusb_alloc_transfer(int iso_packets);
1167 int LIBUSB_CALL libusb_submit_transfer(struct libusb_transfer *transfer);
1168 int LIBUSB_CALL libusb_cancel_transfer(struct libusb_transfer *transfer);
1169 void LIBUSB_CALL libusb_free_transfer(struct libusb_transfer *transfer);
1171 /** \ingroup asyncio
1172 * Helper function to populate the required \ref libusb_transfer fields
1173 * for a control transfer.
1175 * If you pass a transfer buffer to this function, the first 8 bytes will
1176 * be interpreted as a control setup packet, and the wLength field will be
1177 * used to automatically populate the \ref libusb_transfer::length "length"
1178 * field of the transfer. Therefore the recommended approach is:
1179 * -# Allocate a suitably sized data buffer (including space for control setup)
1180 * -# Call libusb_fill_control_setup()
1181 * -# If this is a host-to-device transfer with a data stage, put the data
1182 * in place after the setup packet
1183 * -# Call this function
1184 * -# Call libusb_submit_transfer()
1186 * It is also legal to pass a NULL buffer to this function, in which case this
1187 * function will not attempt to populate the length field. Remember that you
1188 * must then populate the buffer and length fields later.
1190 * \param transfer the transfer to populate
1191 * \param dev_handle handle of the device that will handle the transfer
1192 * \param buffer data buffer. If provided, this function will interpret the
1193 * first 8 bytes as a setup packet and infer the transfer length from that.
1194 * \param callback callback function to be invoked on transfer completion
1195 * \param user_data user data to pass to callback function
1196 * \param timeout timeout for the transfer in milliseconds
1198 static inline void libusb_fill_control_transfer(
1199 struct libusb_transfer *transfer, libusb_device_handle *dev_handle,
1200 unsigned char *buffer, libusb_transfer_cb_fn callback, void *user_data,
1201 unsigned int timeout)
1203 struct libusb_control_setup *setup = (struct libusb_control_setup *) buffer;
1204 transfer->dev_handle = dev_handle;
1205 transfer->endpoint = 0;
1206 transfer->type = LIBUSB_TRANSFER_TYPE_CONTROL;
1207 transfer->timeout = timeout;
1208 transfer->buffer = buffer;
1209 if (setup)
1210 transfer->length = LIBUSB_CONTROL_SETUP_SIZE
1211 + libusb_le16_to_cpu(setup->wLength);
1212 transfer->user_data = user_data;
1213 transfer->callback = callback;
1216 /** \ingroup asyncio
1217 * Helper function to populate the required \ref libusb_transfer fields
1218 * for a bulk transfer.
1220 * \param transfer the transfer to populate
1221 * \param dev_handle handle of the device that will handle the transfer
1222 * \param endpoint address of the endpoint where this transfer will be sent
1223 * \param buffer data buffer
1224 * \param length length of data buffer
1225 * \param callback callback function to be invoked on transfer completion
1226 * \param user_data user data to pass to callback function
1227 * \param timeout timeout for the transfer in milliseconds
1229 static inline void libusb_fill_bulk_transfer(struct libusb_transfer *transfer,
1230 libusb_device_handle *dev_handle, unsigned char endpoint,
1231 unsigned char *buffer, int length, libusb_transfer_cb_fn callback,
1232 void *user_data, unsigned int timeout)
1234 transfer->dev_handle = dev_handle;
1235 transfer->endpoint = endpoint;
1236 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1237 transfer->timeout = timeout;
1238 transfer->buffer = buffer;
1239 transfer->length = length;
1240 transfer->user_data = user_data;
1241 transfer->callback = callback;
1244 /** \ingroup asyncio
1245 * Helper function to populate the required \ref libusb_transfer fields
1246 * for an interrupt transfer.
1248 * \param transfer the transfer to populate
1249 * \param dev_handle handle of the device that will handle the transfer
1250 * \param endpoint address of the endpoint where this transfer will be sent
1251 * \param buffer data buffer
1252 * \param length length of data buffer
1253 * \param callback callback function to be invoked on transfer completion
1254 * \param user_data user data to pass to callback function
1255 * \param timeout timeout for the transfer in milliseconds
1257 static inline void libusb_fill_interrupt_transfer(
1258 struct libusb_transfer *transfer, libusb_device_handle *dev_handle,
1259 unsigned char endpoint, unsigned char *buffer, int length,
1260 libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
1262 transfer->dev_handle = dev_handle;
1263 transfer->endpoint = endpoint;
1264 transfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT;
1265 transfer->timeout = timeout;
1266 transfer->buffer = buffer;
1267 transfer->length = length;
1268 transfer->user_data = user_data;
1269 transfer->callback = callback;
1272 /** \ingroup asyncio
1273 * Helper function to populate the required \ref libusb_transfer fields
1274 * for an isochronous transfer.
1276 * \param transfer the transfer to populate
1277 * \param dev_handle handle of the device that will handle the transfer
1278 * \param endpoint address of the endpoint where this transfer will be sent
1279 * \param buffer data buffer
1280 * \param length length of data buffer
1281 * \param num_iso_packets the number of isochronous packets
1282 * \param callback callback function to be invoked on transfer completion
1283 * \param user_data user data to pass to callback function
1284 * \param timeout timeout for the transfer in milliseconds
1286 static inline void libusb_fill_iso_transfer(struct libusb_transfer *transfer,
1287 libusb_device_handle *dev_handle, unsigned char endpoint,
1288 unsigned char *buffer, int length, int num_iso_packets,
1289 libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
1291 transfer->dev_handle = dev_handle;
1292 transfer->endpoint = endpoint;
1293 transfer->type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS;
1294 transfer->timeout = timeout;
1295 transfer->buffer = buffer;
1296 transfer->length = length;
1297 transfer->num_iso_packets = num_iso_packets;
1298 transfer->user_data = user_data;
1299 transfer->callback = callback;
1302 /** \ingroup asyncio
1303 * Convenience function to set the length of all packets in an isochronous
1304 * transfer, based on the num_iso_packets field in the transfer structure.
1306 * \param transfer a transfer
1307 * \param length the length to set in each isochronous packet descriptor
1308 * \see libusb_get_max_packet_size()
1310 static inline void libusb_set_iso_packet_lengths(
1311 struct libusb_transfer *transfer, unsigned int length)
1313 int i;
1314 for (i = 0; i < transfer->num_iso_packets; i++)
1315 transfer->iso_packet_desc[i].length = length;
1318 /** \ingroup asyncio
1319 * Convenience function to locate the position of an isochronous packet
1320 * within the buffer of an isochronous transfer.
1322 * This is a thorough function which loops through all preceding packets,
1323 * accumulating their lengths to find the position of the specified packet.
1324 * Typically you will assign equal lengths to each packet in the transfer,
1325 * and hence the above method is sub-optimal. You may wish to use
1326 * libusb_get_iso_packet_buffer_simple() instead.
1328 * \param transfer a transfer
1329 * \param packet the packet to return the address of
1330 * \returns the base address of the packet buffer inside the transfer buffer,
1331 * or NULL if the packet does not exist.
1332 * \see libusb_get_iso_packet_buffer_simple()
1334 static inline unsigned char *libusb_get_iso_packet_buffer(
1335 struct libusb_transfer *transfer, unsigned int packet)
1337 int i;
1338 size_t offset = 0;
1339 int _packet;
1341 /* oops..slight bug in the API. packet is an unsigned int, but we use
1342 * signed integers almost everywhere else. range-check and convert to
1343 * signed to avoid compiler warnings. FIXME for libusb-2. */
1344 if (packet > INT_MAX)
1345 return NULL;
1346 _packet = packet;
1348 if (_packet >= transfer->num_iso_packets)
1349 return NULL;
1351 for (i = 0; i < _packet; i++)
1352 offset += transfer->iso_packet_desc[i].length;
1354 return transfer->buffer + offset;
1357 /** \ingroup asyncio
1358 * Convenience function to locate the position of an isochronous packet
1359 * within the buffer of an isochronous transfer, for transfers where each
1360 * packet is of identical size.
1362 * This function relies on the assumption that every packet within the transfer
1363 * is of identical size to the first packet. Calculating the location of
1364 * the packet buffer is then just a simple calculation:
1365 * <tt>buffer + (packet_size * packet)</tt>
1367 * Do not use this function on transfers other than those that have identical
1368 * packet lengths for each packet.
1370 * \param transfer a transfer
1371 * \param packet the packet to return the address of
1372 * \returns the base address of the packet buffer inside the transfer buffer,
1373 * or NULL if the packet does not exist.
1374 * \see libusb_get_iso_packet_buffer()
1376 static inline unsigned char *libusb_get_iso_packet_buffer_simple(
1377 struct libusb_transfer *transfer, unsigned int packet)
1379 int _packet;
1381 /* oops..slight bug in the API. packet is an unsigned int, but we use
1382 * signed integers almost everywhere else. range-check and convert to
1383 * signed to avoid compiler warnings. FIXME for libusb-2. */
1384 if (packet > INT_MAX)
1385 return NULL;
1386 _packet = packet;
1388 if (_packet >= transfer->num_iso_packets)
1389 return NULL;
1391 return transfer->buffer + (transfer->iso_packet_desc[0].length * _packet);
1394 /* sync I/O */
1396 int LIBUSB_CALL libusb_control_transfer(libusb_device_handle *dev_handle,
1397 uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
1398 unsigned char *data, uint16_t wLength, unsigned int timeout);
1400 int LIBUSB_CALL libusb_bulk_transfer(libusb_device_handle *dev_handle,
1401 unsigned char endpoint, unsigned char *data, int length,
1402 int *actual_length, unsigned int timeout);
1404 int LIBUSB_CALL libusb_interrupt_transfer(libusb_device_handle *dev_handle,
1405 unsigned char endpoint, unsigned char *data, int length,
1406 int *actual_length, unsigned int timeout);
1408 /** \ingroup desc
1409 * Retrieve a descriptor from the default control pipe.
1410 * This is a convenience function which formulates the appropriate control
1411 * message to retrieve the descriptor.
1413 * \param dev a device handle
1414 * \param desc_type the descriptor type, see \ref libusb_descriptor_type
1415 * \param desc_index the index of the descriptor to retrieve
1416 * \param data output buffer for descriptor
1417 * \param length size of data buffer
1418 * \returns number of bytes returned in data, or LIBUSB_ERROR code on failure
1420 static inline int libusb_get_descriptor(libusb_device_handle *dev,
1421 uint8_t desc_type, uint8_t desc_index, unsigned char *data, int length)
1423 return libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN,
1424 LIBUSB_REQUEST_GET_DESCRIPTOR, (desc_type << 8) | desc_index, 0, data,
1425 (uint16_t) length, 1000);
1428 /** \ingroup desc
1429 * Retrieve a descriptor from a device.
1430 * This is a convenience function which formulates the appropriate control
1431 * message to retrieve the descriptor. The string returned is Unicode, as
1432 * detailed in the USB specifications.
1434 * \param dev a device handle
1435 * \param desc_index the index of the descriptor to retrieve
1436 * \param langid the language ID for the string descriptor
1437 * \param data output buffer for descriptor
1438 * \param length size of data buffer
1439 * \returns number of bytes returned in data, or LIBUSB_ERROR code on failure
1440 * \see libusb_get_string_descriptor_ascii()
1442 static inline int libusb_get_string_descriptor(libusb_device_handle *dev,
1443 uint8_t desc_index, uint16_t langid, unsigned char *data, int length)
1445 return libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN,
1446 LIBUSB_REQUEST_GET_DESCRIPTOR, (uint16_t)((LIBUSB_DT_STRING << 8) | desc_index),
1447 langid, data, (uint16_t) length, 1000);
1450 int LIBUSB_CALL libusb_get_string_descriptor_ascii(libusb_device_handle *dev,
1451 uint8_t desc_index, unsigned char *data, int length);
1453 /* polling and timeouts */
1455 int LIBUSB_CALL libusb_try_lock_events(libusb_context *ctx);
1456 void LIBUSB_CALL libusb_lock_events(libusb_context *ctx);
1457 void LIBUSB_CALL libusb_unlock_events(libusb_context *ctx);
1458 int LIBUSB_CALL libusb_event_handling_ok(libusb_context *ctx);
1459 int LIBUSB_CALL libusb_event_handler_active(libusb_context *ctx);
1460 void LIBUSB_CALL libusb_lock_event_waiters(libusb_context *ctx);
1461 void LIBUSB_CALL libusb_unlock_event_waiters(libusb_context *ctx);
1462 int LIBUSB_CALL libusb_wait_for_event(libusb_context *ctx, struct timeval *tv);
1464 int LIBUSB_CALL libusb_handle_events_timeout(libusb_context *ctx,
1465 struct timeval *tv);
1466 int LIBUSB_CALL libusb_handle_events_timeout_completed(libusb_context *ctx,
1467 struct timeval *tv, int *completed);
1468 int LIBUSB_CALL libusb_handle_events(libusb_context *ctx);
1469 int LIBUSB_CALL libusb_handle_events_completed(libusb_context *ctx, int *completed);
1470 int LIBUSB_CALL libusb_handle_events_locked(libusb_context *ctx,
1471 struct timeval *tv);
1472 int LIBUSB_CALL libusb_pollfds_handle_timeouts(libusb_context *ctx);
1473 int LIBUSB_CALL libusb_get_next_timeout(libusb_context *ctx,
1474 struct timeval *tv);
1476 /** \ingroup poll
1477 * File descriptor for polling
1479 struct libusb_pollfd {
1480 /** Numeric file descriptor */
1481 int fd;
1483 /** Event flags to poll for from <poll.h>. POLLIN indicates that you
1484 * should monitor this file descriptor for becoming ready to read from,
1485 * and POLLOUT indicates that you should monitor this file descriptor for
1486 * nonblocking write readiness. */
1487 short events;
1490 /** \ingroup poll
1491 * Callback function, invoked when a new file descriptor should be added
1492 * to the set of file descriptors monitored for events.
1493 * \param fd the new file descriptor
1494 * \param events events to monitor for, see \ref libusb_pollfd for a
1495 * description
1496 * \param user_data User data pointer specified in
1497 * libusb_set_pollfd_notifiers() call
1498 * \see libusb_set_pollfd_notifiers()
1500 typedef void (LIBUSB_CALL *libusb_pollfd_added_cb)(int fd, short events,
1501 void *user_data);
1503 /** \ingroup poll
1504 * Callback function, invoked when a file descriptor should be removed from
1505 * the set of file descriptors being monitored for events. After returning
1506 * from this callback, do not use that file descriptor again.
1507 * \param fd the file descriptor to stop monitoring
1508 * \param user_data User data pointer specified in
1509 * libusb_set_pollfd_notifiers() call
1510 * \see libusb_set_pollfd_notifiers()
1512 typedef void (LIBUSB_CALL *libusb_pollfd_removed_cb)(int fd, void *user_data);
1514 const struct libusb_pollfd ** LIBUSB_CALL libusb_get_pollfds(
1515 libusb_context *ctx);
1516 void LIBUSB_CALL libusb_set_pollfd_notifiers(libusb_context *ctx,
1517 libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb,
1518 void *user_data);
1520 /** \ingroup hotplug
1521 * Callback handle.
1523 * Callbacks handles are generated by libusb_hotplug_register_callback()
1524 * and can be used to deregister callbacks. Callback handles are unique
1525 * per libusb_context and it is safe to call libusb_hotplug_deregister_callback()
1526 * on an already deregisted callback.
1528 * Since version 1.0.16, \ref LIBUSBX_API_VERSION >= 0x01000102
1530 * For more information, see \ref hotplug.
1532 typedef int libusb_hotplug_callback_handle;
1534 /** \ingroup hotplug
1536 * Since version 1.0.16, \ref LIBUSBX_API_VERSION >= 0x01000102
1538 * Flags for hotplug events */
1539 typedef enum {
1540 /** Arm the callback and fire it for all matching currently attached devices. */
1541 LIBUSB_HOTPLUG_ENUMERATE = 1,
1542 } libusb_hotplug_flag;
1544 /** \ingroup hotplug
1546 * Since version 1.0.16, \ref LIBUSBX_API_VERSION >= 0x01000102
1548 * Hotplug events */
1549 typedef enum {
1550 /** A device has been plugged in and is ready to use */
1551 LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED = 0x01,
1553 /** A device has left and is no longer available.
1554 * It is the user's responsibility to call libusb_close on any handle associated with a disconnected device.
1555 * It is safe to call libusb_get_device_descriptor on a device that has left */
1556 LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT = 0x02,
1557 } libusb_hotplug_event;
1559 /** \ingroup hotplug
1560 * Wildcard matching for hotplug events */
1561 #define LIBUSB_HOTPLUG_MATCH_ANY -1
1563 /** \ingroup hotplug
1564 * Hotplug callback function type. When requesting hotplug event notifications,
1565 * you pass a pointer to a callback function of this type.
1567 * This callback may be called by an internal event thread and as such it is
1568 * recommended the callback do minimal processing before returning.
1570 * libusbx will call this function later, when a matching event had happened on
1571 * a matching device. See \ref hotplug for more information.
1573 * It is safe to call either libusb_hotplug_register_callback() or
1574 * libusb_hotplug_deregister_callback() from within a callback function.
1576 * Since version 1.0.16, \ref LIBUSBX_API_VERSION >= 0x01000102
1578 * \param libusb_context context of this notification
1579 * \param device libusb_device this event occurred on
1580 * \param event event that occurred
1581 * \param user_data user data provided when this callback was registered
1582 * \returns bool whether this callback is finished processing events.
1583 * returning 1 will cause this callback to be deregistered
1585 typedef int (LIBUSB_CALL *libusb_hotplug_callback_fn)(libusb_context *ctx,
1586 libusb_device *device,
1587 libusb_hotplug_event event,
1588 void *user_data);
1590 /** \ingroup hotplug
1591 * Register a hotplug callback function
1593 * Register a callback with the libusb_context. The callback will fire
1594 * when a matching event occurs on a matching device. The callback is
1595 * armed until either it is deregistered with libusb_hotplug_deregister_callback()
1596 * or the supplied callback returns 1 to indicate it is finished processing events.
1598 * Since version 1.0.16, \ref LIBUSBX_API_VERSION >= 0x01000102
1600 * \param[in] ctx context to register this callback with
1601 * \param[in] events bitwise or of events that will trigger this callback. See \ref
1602 * libusb_hotplug_event
1603 * \param[in] flags hotplug callback flags. See \ref libusb_hotplug_flag
1604 * \param[in] vendor_id the vendor id to match or \ref LIBUSB_HOTPLUG_MATCH_ANY
1605 * \param[in] product_id the product id to match or \ref LIBUSB_HOTPLUG_MATCH_ANY
1606 * \param[in] dev_class the device class to match or \ref LIBUSB_HOTPLUG_MATCH_ANY
1607 * \param[in] cb_fn the function to be invoked on a matching event/device
1608 * \param[in] user_data user data to pass to the callback function
1609 * \param[out] handle pointer to store the handle of the allocated callback (can be NULL)
1610 * \returns LIBUSB_SUCCESS on success LIBUSB_ERROR code on failure
1612 int LIBUSB_CALL libusb_hotplug_register_callback(libusb_context *ctx,
1613 libusb_hotplug_event events,
1614 libusb_hotplug_flag flags,
1615 int vendor_id, int product_id,
1616 int dev_class,
1617 libusb_hotplug_callback_fn cb_fn,
1618 void *user_data,
1619 libusb_hotplug_callback_handle *handle);
1621 /** \ingroup hotplug
1622 * Deregisters a hotplug callback.
1624 * Deregister a callback from a libusb_context. This function is safe to call from within
1625 * a hotplug callback.
1627 * Since version 1.0.16, \ref LIBUSBX_API_VERSION >= 0x01000102
1629 * \param[in] ctx context this callback is registered with
1630 * \param[in] handle the handle of the callback to deregister
1632 void LIBUSB_CALL libusb_hotplug_deregister_callback(libusb_context *ctx,
1633 libusb_hotplug_callback_handle handle);
1635 #ifdef __cplusplus
1637 #endif
1639 #endif