Core: Introduce LIBUSBX_API_VERSION macro
[libusbx.git] / libusb / libusb.h
blobfd1a210e17495441999eb5a591c82981cf67ce66
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 * For more information, please visit: http://libusbx.org
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #ifndef LIBUSB_H
24 #define LIBUSB_H
26 #ifdef _MSC_VER
27 /* on MS environments, the inline keyword is available in C++ only */
28 #if !defined(__cplusplus)
29 #define inline __inline
30 #endif
31 /* ssize_t is also not available (copy/paste from MinGW) */
32 #ifndef _SSIZE_T_DEFINED
33 #define _SSIZE_T_DEFINED
34 #undef ssize_t
35 #ifdef _WIN64
36 typedef __int64 ssize_t;
37 #else
38 typedef int ssize_t;
39 #endif /* _WIN64 */
40 #endif /* _SSIZE_T_DEFINED */
41 #endif /* _MSC_VER */
43 /* stdint.h is also not usually available on MS */
44 #if defined(_MSC_VER) && (_MSC_VER < 1600) && (!defined(_STDINT)) && (!defined(_STDINT_H))
45 typedef unsigned __int8 uint8_t;
46 typedef unsigned __int16 uint16_t;
47 typedef unsigned __int32 uint32_t;
48 #else
49 #include <stdint.h>
50 #endif
52 #include <sys/types.h>
53 #include <time.h>
54 #include <limits.h>
56 #if defined(__linux) || defined(__APPLE__) || defined(__CYGWIN__)
57 #include <sys/time.h>
58 #endif
60 /* 'interface' might be defined as a macro on Windows, so we need to
61 * undefine it so as not to break the current libusbx API, because
62 * libusb_config_descriptor has an 'interface' member
63 * As this can be problematic if you include windows.h after libusb.h
64 * in your sources, we force windows.h to be included first. */
65 #if defined(_WIN32) || defined(__CYGWIN__)
66 #include <windows.h>
67 #if defined(interface)
68 #undef interface
69 #endif
70 #endif
72 /** \def LIBUSB_CALL
73 * \ingroup misc
74 * libusbx's Windows calling convention.
76 * Under Windows, the selection of available compilers and configurations
77 * means that, unlike other platforms, there is not <em>one true calling
78 * convention</em> (calling convention: the manner in which parameters are
79 * passed to funcions in the generated assembly code).
81 * Matching the Windows API itself, libusbx uses the WINAPI convention (which
82 * translates to the <tt>stdcall</tt> convention) and guarantees that the
83 * library is compiled in this way. The public header file also includes
84 * appropriate annotations so that your own software will use the right
85 * convention, even if another convention is being used by default within
86 * your codebase.
88 * The one consideration that you must apply in your software is to mark
89 * all functions which you use as libusbx callbacks with this LIBUSB_CALL
90 * annotation, so that they too get compiled for the correct calling
91 * convention.
93 * On non-Windows operating systems, this macro is defined as nothing. This
94 * means that you can apply it to your code without worrying about
95 * cross-platform compatibility.
97 /* LIBUSB_CALL must be defined on both definition and declaration of libusbx
98 * functions. You'd think that declaration would be enough, but cygwin will
99 * complain about conflicting types unless both are marked this way.
100 * The placement of this macro is important too; it must appear after the
101 * return type, before the function name. See internal documentation for
102 * API_EXPORTED.
104 #if defined(_WIN32) || defined(__CYGWIN__)
105 #define LIBUSB_CALL WINAPI
106 #else
107 #define LIBUSB_CALL
108 #endif
110 /** \def LIBUSBX_API_VERSION
111 * \ingroup misc
112 * libusbx's API version.
114 * Since version 1.0.13, to help with feature detection, libusbx defines
115 * a LIBUSBX_API_VERSION macro that gets increased every time there is a
116 * significant change to the API, such as the introduction of a new call,
117 * the definition of a new macro/enum member, or any other element that
118 * libusbx applications may want to detect at compilation time.
120 * The macro is typically used in an application as follows:
121 * #if defined(LIBUSBX_API_VERSION) && (LIBUSBX_API_VERSION >= 0x01001234)
122 * // Use one of the newer features from the libusbx API
123 * #endif
125 * Another feature of LIBUSBX_API_VERSION is that it can be used to detect
126 * whether you are compiling against the libusb or the libusbx library.
128 * Internally, LIBUSBX_API_VERSION is defined as follows:
129 * (libusbx major << 24) | (libusbx minor << 16) | (16 bit incremental)
131 #define LIBUSBX_API_VERSION 0x01000100
133 #ifdef __cplusplus
134 extern "C" {
135 #endif
137 /** \def libusb_cpu_to_le16
138 * \ingroup misc
139 * Convert a 16-bit value from host-endian to little-endian format. On
140 * little endian systems, this function does nothing. On big endian systems,
141 * the bytes are swapped.
142 * \param x the host-endian value to convert
143 * \returns the value in little-endian byte order
145 static inline uint16_t libusb_cpu_to_le16(const uint16_t x)
147 union {
148 uint8_t b8[2];
149 uint16_t b16;
150 } _tmp;
151 _tmp.b8[1] = x >> 8;
152 _tmp.b8[0] = x & 0xff;
153 return _tmp.b16;
156 /** \def libusb_le16_to_cpu
157 * \ingroup misc
158 * Convert a 16-bit value from little-endian to host-endian format. On
159 * little endian systems, this function does nothing. On big endian systems,
160 * the bytes are swapped.
161 * \param x the little-endian value to convert
162 * \returns the value in host-endian byte order
164 #define libusb_le16_to_cpu libusb_cpu_to_le16
166 /* standard USB stuff */
168 /** \ingroup desc
169 * Device and/or Interface Class codes */
170 enum libusb_class_code {
171 /** In the context of a \ref libusb_device_descriptor "device descriptor",
172 * this bDeviceClass value indicates that each interface specifies its
173 * own class information and all interfaces operate independently.
175 LIBUSB_CLASS_PER_INTERFACE = 0,
177 /** Audio class */
178 LIBUSB_CLASS_AUDIO = 1,
180 /** Communications class */
181 LIBUSB_CLASS_COMM = 2,
183 /** Human Interface Device class */
184 LIBUSB_CLASS_HID = 3,
186 /** Physical */
187 LIBUSB_CLASS_PHYSICAL = 5,
189 /** Printer class */
190 LIBUSB_CLASS_PRINTER = 7,
192 /** Image class */
193 LIBUSB_CLASS_PTP = 6, /* legacy name from libusb-0.1 usb.h */
194 LIBUSB_CLASS_IMAGE = 6,
196 /** Mass storage class */
197 LIBUSB_CLASS_MASS_STORAGE = 8,
199 /** Hub class */
200 LIBUSB_CLASS_HUB = 9,
202 /** Data class */
203 LIBUSB_CLASS_DATA = 10,
205 /** Smart Card */
206 LIBUSB_CLASS_SMART_CARD = 0x0b,
208 /** Content Security */
209 LIBUSB_CLASS_CONTENT_SECURITY = 0x0d,
211 /** Video */
212 LIBUSB_CLASS_VIDEO = 0x0e,
214 /** Personal Healthcare */
215 LIBUSB_CLASS_PERSONAL_HEALTHCARE = 0x0f,
217 /** Diagnostic Device */
218 LIBUSB_CLASS_DIAGNOSTIC_DEVICE = 0xdc,
220 /** Wireless class */
221 LIBUSB_CLASS_WIRELESS = 0xe0,
223 /** Application class */
224 LIBUSB_CLASS_APPLICATION = 0xfe,
226 /** Class is vendor-specific */
227 LIBUSB_CLASS_VENDOR_SPEC = 0xff
230 /** \ingroup desc
231 * Descriptor types as defined by the USB specification. */
232 enum libusb_descriptor_type {
233 /** Device descriptor. See libusb_device_descriptor. */
234 LIBUSB_DT_DEVICE = 0x01,
236 /** Configuration descriptor. See libusb_config_descriptor. */
237 LIBUSB_DT_CONFIG = 0x02,
239 /** String descriptor */
240 LIBUSB_DT_STRING = 0x03,
242 /** Interface descriptor. See libusb_interface_descriptor. */
243 LIBUSB_DT_INTERFACE = 0x04,
245 /** Endpoint descriptor. See libusb_endpoint_descriptor. */
246 LIBUSB_DT_ENDPOINT = 0x05,
248 /** HID descriptor */
249 LIBUSB_DT_HID = 0x21,
251 /** HID report descriptor */
252 LIBUSB_DT_REPORT = 0x22,
254 /** Physical descriptor */
255 LIBUSB_DT_PHYSICAL = 0x23,
257 /** Hub descriptor */
258 LIBUSB_DT_HUB = 0x29,
260 /** SuperSpeed Hub descriptor */
261 LIBUSB_DT_SUPERSPEED_HUB = 0x2A,
264 /* Descriptor sizes per descriptor type */
265 #define LIBUSB_DT_DEVICE_SIZE 18
266 #define LIBUSB_DT_CONFIG_SIZE 9
267 #define LIBUSB_DT_INTERFACE_SIZE 9
268 #define LIBUSB_DT_ENDPOINT_SIZE 7
269 #define LIBUSB_DT_ENDPOINT_AUDIO_SIZE 9 /* Audio extension */
270 #define LIBUSB_DT_HUB_NONVAR_SIZE 7
272 #define LIBUSB_ENDPOINT_ADDRESS_MASK 0x0f /* in bEndpointAddress */
273 #define LIBUSB_ENDPOINT_DIR_MASK 0x80
275 /** \ingroup desc
276 * Endpoint direction. Values for bit 7 of the
277 * \ref libusb_endpoint_descriptor::bEndpointAddress "endpoint address" scheme.
279 enum libusb_endpoint_direction {
280 /** In: device-to-host */
281 LIBUSB_ENDPOINT_IN = 0x80,
283 /** Out: host-to-device */
284 LIBUSB_ENDPOINT_OUT = 0x00
287 #define LIBUSB_TRANSFER_TYPE_MASK 0x03 /* in bmAttributes */
289 /** \ingroup desc
290 * Endpoint transfer type. Values for bits 0:1 of the
291 * \ref libusb_endpoint_descriptor::bmAttributes "endpoint attributes" field.
293 enum libusb_transfer_type {
294 /** Control endpoint */
295 LIBUSB_TRANSFER_TYPE_CONTROL = 0,
297 /** Isochronous endpoint */
298 LIBUSB_TRANSFER_TYPE_ISOCHRONOUS = 1,
300 /** Bulk endpoint */
301 LIBUSB_TRANSFER_TYPE_BULK = 2,
303 /** Interrupt endpoint */
304 LIBUSB_TRANSFER_TYPE_INTERRUPT = 3
307 /** \ingroup misc
308 * Standard requests, as defined in table 9-5 of the USB 3.0 specifications */
309 enum libusb_standard_request {
310 /** Request status of the specific recipient */
311 LIBUSB_REQUEST_GET_STATUS = 0x00,
313 /** Clear or disable a specific feature */
314 LIBUSB_REQUEST_CLEAR_FEATURE = 0x01,
316 /* 0x02 is reserved */
318 /** Set or enable a specific feature */
319 LIBUSB_REQUEST_SET_FEATURE = 0x03,
321 /* 0x04 is reserved */
323 /** Set device address for all future accesses */
324 LIBUSB_REQUEST_SET_ADDRESS = 0x05,
326 /** Get the specified descriptor */
327 LIBUSB_REQUEST_GET_DESCRIPTOR = 0x06,
329 /** Used to update existing descriptors or add new descriptors */
330 LIBUSB_REQUEST_SET_DESCRIPTOR = 0x07,
332 /** Get the current device configuration value */
333 LIBUSB_REQUEST_GET_CONFIGURATION = 0x08,
335 /** Set device configuration */
336 LIBUSB_REQUEST_SET_CONFIGURATION = 0x09,
338 /** Return the selected alternate setting for the specified interface */
339 LIBUSB_REQUEST_GET_INTERFACE = 0x0A,
341 /** Select an alternate interface for the specified interface */
342 LIBUSB_REQUEST_SET_INTERFACE = 0x0B,
344 /** Set then report an endpoint's synchronization frame */
345 LIBUSB_REQUEST_SYNCH_FRAME = 0x0C,
347 /** Sets both the U1 and U2 Exit Latency */
348 LIBUSB_REQUEST_SET_SEL = 0x30,
350 /** Delay from the time a host transmits a packet to the time it is
351 * received by the device. */
352 LIBUSB_SET_ISOCH_DELAY = 0x31,
355 /** \ingroup misc
356 * Request type bits of the
357 * \ref libusb_control_setup::bmRequestType "bmRequestType" field in control
358 * transfers. */
359 enum libusb_request_type {
360 /** Standard */
361 LIBUSB_REQUEST_TYPE_STANDARD = (0x00 << 5),
363 /** Class */
364 LIBUSB_REQUEST_TYPE_CLASS = (0x01 << 5),
366 /** Vendor */
367 LIBUSB_REQUEST_TYPE_VENDOR = (0x02 << 5),
369 /** Reserved */
370 LIBUSB_REQUEST_TYPE_RESERVED = (0x03 << 5)
373 /** \ingroup misc
374 * Recipient bits of the
375 * \ref libusb_control_setup::bmRequestType "bmRequestType" field in control
376 * transfers. Values 4 through 31 are reserved. */
377 enum libusb_request_recipient {
378 /** Device */
379 LIBUSB_RECIPIENT_DEVICE = 0x00,
381 /** Interface */
382 LIBUSB_RECIPIENT_INTERFACE = 0x01,
384 /** Endpoint */
385 LIBUSB_RECIPIENT_ENDPOINT = 0x02,
387 /** Other */
388 LIBUSB_RECIPIENT_OTHER = 0x03,
391 #define LIBUSB_ISO_SYNC_TYPE_MASK 0x0C
393 /** \ingroup desc
394 * Synchronization type for isochronous endpoints. Values for bits 2:3 of the
395 * \ref libusb_endpoint_descriptor::bmAttributes "bmAttributes" field in
396 * libusb_endpoint_descriptor.
398 enum libusb_iso_sync_type {
399 /** No synchronization */
400 LIBUSB_ISO_SYNC_TYPE_NONE = 0,
402 /** Asynchronous */
403 LIBUSB_ISO_SYNC_TYPE_ASYNC = 1,
405 /** Adaptive */
406 LIBUSB_ISO_SYNC_TYPE_ADAPTIVE = 2,
408 /** Synchronous */
409 LIBUSB_ISO_SYNC_TYPE_SYNC = 3
412 #define LIBUSB_ISO_USAGE_TYPE_MASK 0x30
414 /** \ingroup desc
415 * Usage type for isochronous endpoints. Values for bits 4:5 of the
416 * \ref libusb_endpoint_descriptor::bmAttributes "bmAttributes" field in
417 * libusb_endpoint_descriptor.
419 enum libusb_iso_usage_type {
420 /** Data endpoint */
421 LIBUSB_ISO_USAGE_TYPE_DATA = 0,
423 /** Feedback endpoint */
424 LIBUSB_ISO_USAGE_TYPE_FEEDBACK = 1,
426 /** Implicit feedback Data endpoint */
427 LIBUSB_ISO_USAGE_TYPE_IMPLICIT = 2,
430 /** \ingroup desc
431 * A structure representing the standard USB device descriptor. This
432 * descriptor is documented in section 9.6.1 of the USB 3.0 specification.
433 * All multiple-byte fields are represented in host-endian format.
435 struct libusb_device_descriptor {
436 /** Size of this descriptor (in bytes) */
437 uint8_t bLength;
439 /** Descriptor type. Will have value
440 * \ref libusb_descriptor_type::LIBUSB_DT_DEVICE LIBUSB_DT_DEVICE in this
441 * context. */
442 uint8_t bDescriptorType;
444 /** USB specification release number in binary-coded decimal. A value of
445 * 0x0200 indicates USB 2.0, 0x0110 indicates USB 1.1, etc. */
446 uint16_t bcdUSB;
448 /** USB-IF class code for the device. See \ref libusb_class_code. */
449 uint8_t bDeviceClass;
451 /** USB-IF subclass code for the device, qualified by the bDeviceClass
452 * value */
453 uint8_t bDeviceSubClass;
455 /** USB-IF protocol code for the device, qualified by the bDeviceClass and
456 * bDeviceSubClass values */
457 uint8_t bDeviceProtocol;
459 /** Maximum packet size for endpoint 0 */
460 uint8_t bMaxPacketSize0;
462 /** USB-IF vendor ID */
463 uint16_t idVendor;
465 /** USB-IF product ID */
466 uint16_t idProduct;
468 /** Device release number in binary-coded decimal */
469 uint16_t bcdDevice;
471 /** Index of string descriptor describing manufacturer */
472 uint8_t iManufacturer;
474 /** Index of string descriptor describing product */
475 uint8_t iProduct;
477 /** Index of string descriptor containing device serial number */
478 uint8_t iSerialNumber;
480 /** Number of possible configurations */
481 uint8_t bNumConfigurations;
484 /** \ingroup desc
485 * A structure representing the standard USB endpoint descriptor. This
486 * descriptor is documented in section 9.6.6 of the USB 3.0 specification.
487 * All multiple-byte fields are represented in host-endian format.
489 struct libusb_endpoint_descriptor {
490 /** Size of this descriptor (in bytes) */
491 uint8_t bLength;
493 /** Descriptor type. Will have value
494 * \ref libusb_descriptor_type::LIBUSB_DT_ENDPOINT LIBUSB_DT_ENDPOINT in
495 * this context. */
496 uint8_t bDescriptorType;
498 /** The address of the endpoint described by this descriptor. Bits 0:3 are
499 * the endpoint number. Bits 4:6 are reserved. Bit 7 indicates direction,
500 * see \ref libusb_endpoint_direction.
502 uint8_t bEndpointAddress;
504 /** Attributes which apply to the endpoint when it is configured using
505 * the bConfigurationValue. Bits 0:1 determine the transfer type and
506 * correspond to \ref libusb_transfer_type. Bits 2:3 are only used for
507 * isochronous endpoints and correspond to \ref libusb_iso_sync_type.
508 * Bits 4:5 are also only used for isochronous endpoints and correspond to
509 * \ref libusb_iso_usage_type. Bits 6:7 are reserved.
511 uint8_t bmAttributes;
513 /** Maximum packet size this endpoint is capable of sending/receiving. */
514 uint16_t wMaxPacketSize;
516 /** Interval for polling endpoint for data transfers. */
517 uint8_t bInterval;
519 /** For audio devices only: the rate at which synchronization feedback
520 * is provided. */
521 uint8_t bRefresh;
523 /** For audio devices only: the address if the synch endpoint */
524 uint8_t bSynchAddress;
526 /** Extra descriptors. If libusbx encounters unknown endpoint descriptors,
527 * it will store them here, should you wish to parse them. */
528 const unsigned char *extra;
530 /** Length of the extra descriptors, in bytes. */
531 int extra_length;
534 /** \ingroup desc
535 * A structure representing the standard USB interface descriptor. This
536 * descriptor is documented in section 9.6.5 of the USB 3.0 specification.
537 * All multiple-byte fields are represented in host-endian format.
539 struct libusb_interface_descriptor {
540 /** Size of this descriptor (in bytes) */
541 uint8_t bLength;
543 /** Descriptor type. Will have value
544 * \ref libusb_descriptor_type::LIBUSB_DT_INTERFACE LIBUSB_DT_INTERFACE
545 * in this context. */
546 uint8_t bDescriptorType;
548 /** Number of this interface */
549 uint8_t bInterfaceNumber;
551 /** Value used to select this alternate setting for this interface */
552 uint8_t bAlternateSetting;
554 /** Number of endpoints used by this interface (excluding the control
555 * endpoint). */
556 uint8_t bNumEndpoints;
558 /** USB-IF class code for this interface. See \ref libusb_class_code. */
559 uint8_t bInterfaceClass;
561 /** USB-IF subclass code for this interface, qualified by the
562 * bInterfaceClass value */
563 uint8_t bInterfaceSubClass;
565 /** USB-IF protocol code for this interface, qualified by the
566 * bInterfaceClass and bInterfaceSubClass values */
567 uint8_t bInterfaceProtocol;
569 /** Index of string descriptor describing this interface */
570 uint8_t iInterface;
572 /** Array of endpoint descriptors. This length of this array is determined
573 * by the bNumEndpoints field. */
574 const struct libusb_endpoint_descriptor *endpoint;
576 /** Extra descriptors. If libusbx encounters unknown interface descriptors,
577 * it will store them here, should you wish to parse them. */
578 const unsigned char *extra;
580 /** Length of the extra descriptors, in bytes. */
581 int extra_length;
584 /** \ingroup desc
585 * A collection of alternate settings for a particular USB interface.
587 struct libusb_interface {
588 /** Array of interface descriptors. The length of this array is determined
589 * by the num_altsetting field. */
590 const struct libusb_interface_descriptor *altsetting;
592 /** The number of alternate settings that belong to this interface */
593 int num_altsetting;
596 /** \ingroup desc
597 * A structure representing the standard USB configuration descriptor. This
598 * descriptor is documented in section 9.6.3 of the USB 3.0 specification.
599 * All multiple-byte fields are represented in host-endian format.
601 struct libusb_config_descriptor {
602 /** Size of this descriptor (in bytes) */
603 uint8_t bLength;
605 /** Descriptor type. Will have value
606 * \ref libusb_descriptor_type::LIBUSB_DT_CONFIG LIBUSB_DT_CONFIG
607 * in this context. */
608 uint8_t bDescriptorType;
610 /** Total length of data returned for this configuration */
611 uint16_t wTotalLength;
613 /** Number of interfaces supported by this configuration */
614 uint8_t bNumInterfaces;
616 /** Identifier value for this configuration */
617 uint8_t bConfigurationValue;
619 /** Index of string descriptor describing this configuration */
620 uint8_t iConfiguration;
622 /** Configuration characteristics */
623 uint8_t bmAttributes;
625 /** Maximum power consumption of the USB device from this bus in this
626 * configuration when the device is fully opreation. Expressed in units
627 * of 2 mA. */
628 uint8_t bMaxPower;
630 /** Array of interfaces supported by this configuration. The length of
631 * this array is determined by the bNumInterfaces field. */
632 const struct libusb_interface *interface;
634 /** Extra descriptors. If libusbx encounters unknown configuration
635 * descriptors, it will store them here, should you wish to parse them. */
636 const unsigned char *extra;
638 /** Length of the extra descriptors, in bytes. */
639 int extra_length;
642 /** \ingroup asyncio
643 * Setup packet for control transfers. */
644 struct libusb_control_setup {
645 /** Request type. Bits 0:4 determine recipient, see
646 * \ref libusb_request_recipient. Bits 5:6 determine type, see
647 * \ref libusb_request_type. Bit 7 determines data transfer direction, see
648 * \ref libusb_endpoint_direction.
650 uint8_t bmRequestType;
652 /** Request. If the type bits of bmRequestType are equal to
653 * \ref libusb_request_type::LIBUSB_REQUEST_TYPE_STANDARD
654 * "LIBUSB_REQUEST_TYPE_STANDARD" then this field refers to
655 * \ref libusb_standard_request. For other cases, use of this field is
656 * application-specific. */
657 uint8_t bRequest;
659 /** Value. Varies according to request */
660 uint16_t wValue;
662 /** Index. Varies according to request, typically used to pass an index
663 * or offset */
664 uint16_t wIndex;
666 /** Number of bytes to transfer */
667 uint16_t wLength;
670 #define LIBUSB_CONTROL_SETUP_SIZE (sizeof(struct libusb_control_setup))
672 /* libusbx */
674 struct libusb_context;
675 struct libusb_device;
676 struct libusb_device_handle;
678 /** \ingroup lib
679 * Structure providing the version of the libusbx runtime
681 struct libusb_version {
682 /** Library major version. */
683 const uint16_t major;
685 /** Library minor version. */
686 const uint16_t minor;
688 /** Library micro version. */
689 const uint16_t micro;
691 /** Library nano version. */
692 const uint16_t nano;
694 /** Library release candidate suffix string, e.g. "-rc4". */
695 const char *rc;
697 /** For ABI compatibility only. */
698 const char* describe;
701 /** \ingroup lib
702 * Structure representing a libusbx session. The concept of individual libusbx
703 * sessions allows for your program to use two libraries (or dynamically
704 * load two modules) which both independently use libusb. This will prevent
705 * interference between the individual libusbx users - for example
706 * libusb_set_debug() will not affect the other user of the library, and
707 * libusb_exit() will not destroy resources that the other user is still
708 * using.
710 * Sessions are created by libusb_init() and destroyed through libusb_exit().
711 * If your application is guaranteed to only ever include a single libusbx
712 * user (i.e. you), you do not have to worry about contexts: pass NULL in
713 * every function call where a context is required. The default context
714 * will be used.
716 * For more information, see \ref contexts.
718 typedef struct libusb_context libusb_context;
720 /** \ingroup dev
721 * Structure representing a USB device detected on the system. This is an
722 * opaque type for which you are only ever provided with a pointer, usually
723 * originating from libusb_get_device_list().
725 * Certain operations can be performed on a device, but in order to do any
726 * I/O you will have to first obtain a device handle using libusb_open().
728 * Devices are reference counted with libusb_device_ref() and
729 * libusb_device_unref(), and are freed when the reference count reaches 0.
730 * New devices presented by libusb_get_device_list() have a reference count of
731 * 1, and libusb_free_device_list() can optionally decrease the reference count
732 * on all devices in the list. libusb_open() adds another reference which is
733 * later destroyed by libusb_close().
735 typedef struct libusb_device libusb_device;
738 /** \ingroup dev
739 * Structure representing a handle on a USB device. This is an opaque type for
740 * which you are only ever provided with a pointer, usually originating from
741 * libusb_open().
743 * A device handle is used to perform I/O and other operations. When finished
744 * with a device handle, you should call libusb_close().
746 typedef struct libusb_device_handle libusb_device_handle;
748 /** \ingroup dev
749 * Speed codes. Indicates the speed at which the device is operating.
751 enum libusb_speed {
752 /** The OS doesn't report or know the device speed. */
753 LIBUSB_SPEED_UNKNOWN = 0,
755 /** The device is operating at low speed (1.5MBit/s). */
756 LIBUSB_SPEED_LOW = 1,
758 /** The device is operating at full speed (12MBit/s). */
759 LIBUSB_SPEED_FULL = 2,
761 /** The device is operating at high speed (480MBit/s). */
762 LIBUSB_SPEED_HIGH = 3,
764 /** The device is operating at super speed (5000MBit/s). */
765 LIBUSB_SPEED_SUPER = 4,
768 /** \ingroup misc
769 * Error codes. Most libusbx functions return 0 on success or one of these
770 * codes on failure.
771 * You can call \ref libusb_error_name() to retrieve a string representation
772 * of an error code.
774 enum libusb_error {
775 /** Success (no error) */
776 LIBUSB_SUCCESS = 0,
778 /** Input/output error */
779 LIBUSB_ERROR_IO = -1,
781 /** Invalid parameter */
782 LIBUSB_ERROR_INVALID_PARAM = -2,
784 /** Access denied (insufficient permissions) */
785 LIBUSB_ERROR_ACCESS = -3,
787 /** No such device (it may have been disconnected) */
788 LIBUSB_ERROR_NO_DEVICE = -4,
790 /** Entity not found */
791 LIBUSB_ERROR_NOT_FOUND = -5,
793 /** Resource busy */
794 LIBUSB_ERROR_BUSY = -6,
796 /** Operation timed out */
797 LIBUSB_ERROR_TIMEOUT = -7,
799 /** Overflow */
800 LIBUSB_ERROR_OVERFLOW = -8,
802 /** Pipe error */
803 LIBUSB_ERROR_PIPE = -9,
805 /** System call interrupted (perhaps due to signal) */
806 LIBUSB_ERROR_INTERRUPTED = -10,
808 /** Insufficient memory */
809 LIBUSB_ERROR_NO_MEM = -11,
811 /** Operation not supported or unimplemented on this platform */
812 LIBUSB_ERROR_NOT_SUPPORTED = -12,
814 /* NB! Remember to update libusb_error_name()
815 when adding new error codes here. */
817 /** Other error */
818 LIBUSB_ERROR_OTHER = -99,
821 /** \ingroup asyncio
822 * Transfer status codes */
823 enum libusb_transfer_status {
824 /** Transfer completed without error. Note that this does not indicate
825 * that the entire amount of requested data was transferred. */
826 LIBUSB_TRANSFER_COMPLETED,
828 /** Transfer failed */
829 LIBUSB_TRANSFER_ERROR,
831 /** Transfer timed out */
832 LIBUSB_TRANSFER_TIMED_OUT,
834 /** Transfer was cancelled */
835 LIBUSB_TRANSFER_CANCELLED,
837 /** For bulk/interrupt endpoints: halt condition detected (endpoint
838 * stalled). For control endpoints: control request not supported. */
839 LIBUSB_TRANSFER_STALL,
841 /** Device was disconnected */
842 LIBUSB_TRANSFER_NO_DEVICE,
844 /** Device sent more data than requested */
845 LIBUSB_TRANSFER_OVERFLOW,
847 /* NB! Remember to update libusb_error_name()
848 when adding new status codes here. */
851 /** \ingroup asyncio
852 * libusb_transfer.flags values */
853 enum libusb_transfer_flags {
854 /** Report short frames as errors */
855 LIBUSB_TRANSFER_SHORT_NOT_OK = 1<<0,
857 /** Automatically free() transfer buffer during libusb_free_transfer() */
858 LIBUSB_TRANSFER_FREE_BUFFER = 1<<1,
860 /** Automatically call libusb_free_transfer() after callback returns.
861 * If this flag is set, it is illegal to call libusb_free_transfer()
862 * from your transfer callback, as this will result in a double-free
863 * when this flag is acted upon. */
864 LIBUSB_TRANSFER_FREE_TRANSFER = 1<<2,
866 /** Terminate transfers that are a multiple of the endpoint's
867 * wMaxPacketSize with an extra zero length packet. This is useful
868 * when a device protocol mandates that each logical request is
869 * terminated by an incomplete packet (i.e. the logical requests are
870 * not separated by other means).
872 * This flag only affects host-to-device transfers to bulk and interrupt
873 * endpoints. In other situations, it is ignored.
875 * This flag only affects transfers with a length that is a multiple of
876 * the endpoint's wMaxPacketSize. On transfers of other lengths, this
877 * flag has no effect. Therefore, if you are working with a device that
878 * needs a ZLP whenever the end of the logical request falls on a packet
879 * boundary, then it is sensible to set this flag on <em>every</em>
880 * transfer (you do not have to worry about only setting it on transfers
881 * that end on the boundary).
883 * This flag is currently only supported on Linux.
884 * On other systems, libusb_submit_transfer() will return
885 * LIBUSB_ERROR_NOT_SUPPORTED for every transfer where this flag is set.
887 * Available since libusb-1.0.9.
889 LIBUSB_TRANSFER_ADD_ZERO_PACKET = 1 << 3,
892 /** \ingroup asyncio
893 * Isochronous packet descriptor. */
894 struct libusb_iso_packet_descriptor {
895 /** Length of data to request in this packet */
896 unsigned int length;
898 /** Amount of data that was actually transferred */
899 unsigned int actual_length;
901 /** Status code for this packet */
902 enum libusb_transfer_status status;
905 struct libusb_transfer;
907 /** \ingroup asyncio
908 * Asynchronous transfer callback function type. When submitting asynchronous
909 * transfers, you pass a pointer to a callback function of this type via the
910 * \ref libusb_transfer::callback "callback" member of the libusb_transfer
911 * structure. libusbx will call this function later, when the transfer has
912 * completed or failed. See \ref asyncio for more information.
913 * \param transfer The libusb_transfer struct the callback function is being
914 * notified about.
916 typedef void (LIBUSB_CALL *libusb_transfer_cb_fn)(struct libusb_transfer *transfer);
918 /** \ingroup asyncio
919 * The generic USB transfer structure. The user populates this structure and
920 * then submits it in order to request a transfer. After the transfer has
921 * completed, the library populates the transfer with the results and passes
922 * it back to the user.
924 struct libusb_transfer {
925 /** Handle of the device that this transfer will be submitted to */
926 libusb_device_handle *dev_handle;
928 /** A bitwise OR combination of \ref libusb_transfer_flags. */
929 uint8_t flags;
931 /** Address of the endpoint where this transfer will be sent. */
932 unsigned char endpoint;
934 /** Type of the endpoint from \ref libusb_transfer_type */
935 unsigned char type;
937 /** Timeout for this transfer in millseconds. A value of 0 indicates no
938 * timeout. */
939 unsigned int timeout;
941 /** The status of the transfer. Read-only, and only for use within
942 * transfer callback function.
944 * If this is an isochronous transfer, this field may read COMPLETED even
945 * if there were errors in the frames. Use the
946 * \ref libusb_iso_packet_descriptor::status "status" field in each packet
947 * to determine if errors occurred. */
948 enum libusb_transfer_status status;
950 /** Length of the data buffer */
951 int length;
953 /** Actual length of data that was transferred. Read-only, and only for
954 * use within transfer callback function. Not valid for isochronous
955 * endpoint transfers. */
956 int actual_length;
958 /** Callback function. This will be invoked when the transfer completes,
959 * fails, or is cancelled. */
960 libusb_transfer_cb_fn callback;
962 /** User context data to pass to the callback function. */
963 void *user_data;
965 /** Data buffer */
966 unsigned char *buffer;
968 /** Number of isochronous packets. Only used for I/O with isochronous
969 * endpoints. */
970 int num_iso_packets;
972 /** Isochronous packet descriptors, for isochronous transfers only. */
973 struct libusb_iso_packet_descriptor iso_packet_desc
974 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
975 [] /* valid C99 code */
976 #else
977 [0] /* non-standard, but usually working code */
978 #endif
982 /** \ingroup misc
983 * Capabilities supported by this instance of libusb. Test if the loaded
984 * library supports a given capability by calling
985 * \ref libusb_has_capability().
987 enum libusb_capability {
988 /** The libusb_has_capability() API is available. */
989 LIBUSB_CAP_HAS_CAPABILITY = 0,
992 /** \ingroup lib
993 * Log message levels.
994 * - LIBUSB_LOG_LEVEL_NONE (0) : no messages ever printed by the library (default)
995 * - LIBUSB_LOG_LEVEL_ERROR (1) : error messages are printed to stderr
996 * - LIBUSB_LOG_LEVEL_WARNING (2) : warning and error messages are printed to stderr
997 * - LIBUSB_LOG_LEVEL_INFO (3) : informational messages are printed to stdout, warning
998 * and error messages are printed to stderr
999 * - LIBUSB_LOG_LEVEL_DEBUG (4) : debug and informational messages are printed to stdout,
1000 * warnings and errors to stderr
1002 enum libusb_log_level {
1003 LIBUSB_LOG_LEVEL_NONE = 0,
1004 LIBUSB_LOG_LEVEL_ERROR,
1005 LIBUSB_LOG_LEVEL_WARNING,
1006 LIBUSB_LOG_LEVEL_INFO,
1007 LIBUSB_LOG_LEVEL_DEBUG,
1010 int LIBUSB_CALL libusb_init(libusb_context **ctx);
1011 void LIBUSB_CALL libusb_exit(libusb_context *ctx);
1012 void LIBUSB_CALL libusb_set_debug(libusb_context *ctx, int level);
1013 const struct libusb_version * LIBUSB_CALL libusb_get_version(void);
1014 int LIBUSB_CALL libusb_has_capability(uint32_t capability);
1015 const char * LIBUSB_CALL libusb_error_name(int errcode);
1017 ssize_t LIBUSB_CALL libusb_get_device_list(libusb_context *ctx,
1018 libusb_device ***list);
1019 void LIBUSB_CALL libusb_free_device_list(libusb_device **list,
1020 int unref_devices);
1021 libusb_device * LIBUSB_CALL libusb_ref_device(libusb_device *dev);
1022 void LIBUSB_CALL libusb_unref_device(libusb_device *dev);
1024 int LIBUSB_CALL libusb_get_configuration(libusb_device_handle *dev,
1025 int *config);
1026 int LIBUSB_CALL libusb_get_device_descriptor(libusb_device *dev,
1027 struct libusb_device_descriptor *desc);
1028 int LIBUSB_CALL libusb_get_active_config_descriptor(libusb_device *dev,
1029 struct libusb_config_descriptor **config);
1030 int LIBUSB_CALL libusb_get_config_descriptor(libusb_device *dev,
1031 uint8_t config_index, struct libusb_config_descriptor **config);
1032 int LIBUSB_CALL libusb_get_config_descriptor_by_value(libusb_device *dev,
1033 uint8_t bConfigurationValue, struct libusb_config_descriptor **config);
1034 void LIBUSB_CALL libusb_free_config_descriptor(
1035 struct libusb_config_descriptor *config);
1036 uint8_t LIBUSB_CALL libusb_get_bus_number(libusb_device *dev);
1037 uint8_t LIBUSB_CALL libusb_get_port_number(libusb_device *dev);
1038 libusb_device * LIBUSB_CALL libusb_get_parent(libusb_device *dev);
1039 int LIBUSB_CALL libusb_get_port_path(libusb_context *ctx, libusb_device *dev, uint8_t* path, uint8_t path_length);
1040 uint8_t LIBUSB_CALL libusb_get_device_address(libusb_device *dev);
1041 int LIBUSB_CALL libusb_get_device_speed(libusb_device *dev);
1042 int LIBUSB_CALL libusb_get_max_packet_size(libusb_device *dev,
1043 unsigned char endpoint);
1044 int LIBUSB_CALL libusb_get_max_iso_packet_size(libusb_device *dev,
1045 unsigned char endpoint);
1047 int LIBUSB_CALL libusb_open(libusb_device *dev, libusb_device_handle **handle);
1048 void LIBUSB_CALL libusb_close(libusb_device_handle *dev_handle);
1049 libusb_device * LIBUSB_CALL libusb_get_device(libusb_device_handle *dev_handle);
1051 int LIBUSB_CALL libusb_set_configuration(libusb_device_handle *dev,
1052 int configuration);
1053 int LIBUSB_CALL libusb_claim_interface(libusb_device_handle *dev,
1054 int interface_number);
1055 int LIBUSB_CALL libusb_release_interface(libusb_device_handle *dev,
1056 int interface_number);
1058 libusb_device_handle * LIBUSB_CALL libusb_open_device_with_vid_pid(
1059 libusb_context *ctx, uint16_t vendor_id, uint16_t product_id);
1061 int LIBUSB_CALL libusb_set_interface_alt_setting(libusb_device_handle *dev,
1062 int interface_number, int alternate_setting);
1063 int LIBUSB_CALL libusb_clear_halt(libusb_device_handle *dev,
1064 unsigned char endpoint);
1065 int LIBUSB_CALL libusb_reset_device(libusb_device_handle *dev);
1067 int LIBUSB_CALL libusb_kernel_driver_active(libusb_device_handle *dev,
1068 int interface_number);
1069 int LIBUSB_CALL libusb_detach_kernel_driver(libusb_device_handle *dev,
1070 int interface_number);
1071 int LIBUSB_CALL libusb_attach_kernel_driver(libusb_device_handle *dev,
1072 int interface_number);
1074 /* async I/O */
1076 /** \ingroup asyncio
1077 * Get the data section of a control transfer. This convenience function is here
1078 * to remind you that the data does not start until 8 bytes into the actual
1079 * buffer, as the setup packet comes first.
1081 * Calling this function only makes sense from a transfer callback function,
1082 * or situations where you have already allocated a suitably sized buffer at
1083 * transfer->buffer.
1085 * \param transfer a transfer
1086 * \returns pointer to the first byte of the data section
1088 static inline unsigned char *libusb_control_transfer_get_data(
1089 struct libusb_transfer *transfer)
1091 return transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE;
1094 /** \ingroup asyncio
1095 * Get the control setup packet of a control transfer. This convenience
1096 * function is here to remind you that the control setup occupies the first
1097 * 8 bytes of the transfer data buffer.
1099 * Calling this function only makes sense from a transfer callback function,
1100 * or situations where you have already allocated a suitably sized buffer at
1101 * transfer->buffer.
1103 * \param transfer a transfer
1104 * \returns a casted pointer to the start of the transfer data buffer
1106 static inline struct libusb_control_setup *libusb_control_transfer_get_setup(
1107 struct libusb_transfer *transfer)
1109 return (struct libusb_control_setup *) transfer->buffer;
1112 /** \ingroup asyncio
1113 * Helper function to populate the setup packet (first 8 bytes of the data
1114 * buffer) for a control transfer. The wIndex, wValue and wLength values should
1115 * be given in host-endian byte order.
1117 * \param buffer buffer to output the setup packet into
1118 * \param bmRequestType see the
1119 * \ref libusb_control_setup::bmRequestType "bmRequestType" field of
1120 * \ref libusb_control_setup
1121 * \param bRequest see the
1122 * \ref libusb_control_setup::bRequest "bRequest" field of
1123 * \ref libusb_control_setup
1124 * \param wValue see the
1125 * \ref libusb_control_setup::wValue "wValue" field of
1126 * \ref libusb_control_setup
1127 * \param wIndex see the
1128 * \ref libusb_control_setup::wIndex "wIndex" field of
1129 * \ref libusb_control_setup
1130 * \param wLength see the
1131 * \ref libusb_control_setup::wLength "wLength" field of
1132 * \ref libusb_control_setup
1134 static inline void libusb_fill_control_setup(unsigned char *buffer,
1135 uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
1136 uint16_t wLength)
1138 struct libusb_control_setup *setup = (struct libusb_control_setup *) buffer;
1139 setup->bmRequestType = bmRequestType;
1140 setup->bRequest = bRequest;
1141 setup->wValue = libusb_cpu_to_le16(wValue);
1142 setup->wIndex = libusb_cpu_to_le16(wIndex);
1143 setup->wLength = libusb_cpu_to_le16(wLength);
1146 struct libusb_transfer * LIBUSB_CALL libusb_alloc_transfer(int iso_packets);
1147 int LIBUSB_CALL libusb_submit_transfer(struct libusb_transfer *transfer);
1148 int LIBUSB_CALL libusb_cancel_transfer(struct libusb_transfer *transfer);
1149 void LIBUSB_CALL libusb_free_transfer(struct libusb_transfer *transfer);
1151 /** \ingroup asyncio
1152 * Helper function to populate the required \ref libusb_transfer fields
1153 * for a control transfer.
1155 * If you pass a transfer buffer to this function, the first 8 bytes will
1156 * be interpreted as a control setup packet, and the wLength field will be
1157 * used to automatically populate the \ref libusb_transfer::length "length"
1158 * field of the transfer. Therefore the recommended approach is:
1159 * -# Allocate a suitably sized data buffer (including space for control setup)
1160 * -# Call libusb_fill_control_setup()
1161 * -# If this is a host-to-device transfer with a data stage, put the data
1162 * in place after the setup packet
1163 * -# Call this function
1164 * -# Call libusb_submit_transfer()
1166 * It is also legal to pass a NULL buffer to this function, in which case this
1167 * function will not attempt to populate the length field. Remember that you
1168 * must then populate the buffer and length fields later.
1170 * \param transfer the transfer to populate
1171 * \param dev_handle handle of the device that will handle the transfer
1172 * \param buffer data buffer. If provided, this function will interpret the
1173 * first 8 bytes as a setup packet and infer the transfer length from that.
1174 * \param callback callback function to be invoked on transfer completion
1175 * \param user_data user data to pass to callback function
1176 * \param timeout timeout for the transfer in milliseconds
1178 static inline void libusb_fill_control_transfer(
1179 struct libusb_transfer *transfer, libusb_device_handle *dev_handle,
1180 unsigned char *buffer, libusb_transfer_cb_fn callback, void *user_data,
1181 unsigned int timeout)
1183 struct libusb_control_setup *setup = (struct libusb_control_setup *) buffer;
1184 transfer->dev_handle = dev_handle;
1185 transfer->endpoint = 0;
1186 transfer->type = LIBUSB_TRANSFER_TYPE_CONTROL;
1187 transfer->timeout = timeout;
1188 transfer->buffer = buffer;
1189 if (setup)
1190 transfer->length = LIBUSB_CONTROL_SETUP_SIZE
1191 + libusb_le16_to_cpu(setup->wLength);
1192 transfer->user_data = user_data;
1193 transfer->callback = callback;
1196 /** \ingroup asyncio
1197 * Helper function to populate the required \ref libusb_transfer fields
1198 * for a bulk transfer.
1200 * \param transfer the transfer to populate
1201 * \param dev_handle handle of the device that will handle the transfer
1202 * \param endpoint address of the endpoint where this transfer will be sent
1203 * \param buffer data buffer
1204 * \param length length of data buffer
1205 * \param callback callback function to be invoked on transfer completion
1206 * \param user_data user data to pass to callback function
1207 * \param timeout timeout for the transfer in milliseconds
1209 static inline void libusb_fill_bulk_transfer(struct libusb_transfer *transfer,
1210 libusb_device_handle *dev_handle, unsigned char endpoint,
1211 unsigned char *buffer, int length, libusb_transfer_cb_fn callback,
1212 void *user_data, unsigned int timeout)
1214 transfer->dev_handle = dev_handle;
1215 transfer->endpoint = endpoint;
1216 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1217 transfer->timeout = timeout;
1218 transfer->buffer = buffer;
1219 transfer->length = length;
1220 transfer->user_data = user_data;
1221 transfer->callback = callback;
1224 /** \ingroup asyncio
1225 * Helper function to populate the required \ref libusb_transfer fields
1226 * for an interrupt transfer.
1228 * \param transfer the transfer to populate
1229 * \param dev_handle handle of the device that will handle the transfer
1230 * \param endpoint address of the endpoint where this transfer will be sent
1231 * \param buffer data buffer
1232 * \param length length of data buffer
1233 * \param callback callback function to be invoked on transfer completion
1234 * \param user_data user data to pass to callback function
1235 * \param timeout timeout for the transfer in milliseconds
1237 static inline void libusb_fill_interrupt_transfer(
1238 struct libusb_transfer *transfer, libusb_device_handle *dev_handle,
1239 unsigned char endpoint, unsigned char *buffer, int length,
1240 libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
1242 transfer->dev_handle = dev_handle;
1243 transfer->endpoint = endpoint;
1244 transfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT;
1245 transfer->timeout = timeout;
1246 transfer->buffer = buffer;
1247 transfer->length = length;
1248 transfer->user_data = user_data;
1249 transfer->callback = callback;
1252 /** \ingroup asyncio
1253 * Helper function to populate the required \ref libusb_transfer fields
1254 * for an isochronous transfer.
1256 * \param transfer the transfer to populate
1257 * \param dev_handle handle of the device that will handle the transfer
1258 * \param endpoint address of the endpoint where this transfer will be sent
1259 * \param buffer data buffer
1260 * \param length length of data buffer
1261 * \param num_iso_packets the number of isochronous packets
1262 * \param callback callback function to be invoked on transfer completion
1263 * \param user_data user data to pass to callback function
1264 * \param timeout timeout for the transfer in milliseconds
1266 static inline void libusb_fill_iso_transfer(struct libusb_transfer *transfer,
1267 libusb_device_handle *dev_handle, unsigned char endpoint,
1268 unsigned char *buffer, int length, int num_iso_packets,
1269 libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
1271 transfer->dev_handle = dev_handle;
1272 transfer->endpoint = endpoint;
1273 transfer->type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS;
1274 transfer->timeout = timeout;
1275 transfer->buffer = buffer;
1276 transfer->length = length;
1277 transfer->num_iso_packets = num_iso_packets;
1278 transfer->user_data = user_data;
1279 transfer->callback = callback;
1282 /** \ingroup asyncio
1283 * Convenience function to set the length of all packets in an isochronous
1284 * transfer, based on the num_iso_packets field in the transfer structure.
1286 * \param transfer a transfer
1287 * \param length the length to set in each isochronous packet descriptor
1288 * \see libusb_get_max_packet_size()
1290 static inline void libusb_set_iso_packet_lengths(
1291 struct libusb_transfer *transfer, unsigned int length)
1293 int i;
1294 for (i = 0; i < transfer->num_iso_packets; i++)
1295 transfer->iso_packet_desc[i].length = length;
1298 /** \ingroup asyncio
1299 * Convenience function to locate the position of an isochronous packet
1300 * within the buffer of an isochronous transfer.
1302 * This is a thorough function which loops through all preceding packets,
1303 * accumulating their lengths to find the position of the specified packet.
1304 * Typically you will assign equal lengths to each packet in the transfer,
1305 * and hence the above method is sub-optimal. You may wish to use
1306 * libusb_get_iso_packet_buffer_simple() instead.
1308 * \param transfer a transfer
1309 * \param packet the packet to return the address of
1310 * \returns the base address of the packet buffer inside the transfer buffer,
1311 * or NULL if the packet does not exist.
1312 * \see libusb_get_iso_packet_buffer_simple()
1314 static inline unsigned char *libusb_get_iso_packet_buffer(
1315 struct libusb_transfer *transfer, unsigned int packet)
1317 int i;
1318 size_t offset = 0;
1319 int _packet;
1321 /* oops..slight bug in the API. packet is an unsigned int, but we use
1322 * signed integers almost everywhere else. range-check and convert to
1323 * signed to avoid compiler warnings. FIXME for libusb-2. */
1324 if (packet > INT_MAX)
1325 return NULL;
1326 _packet = packet;
1328 if (_packet >= transfer->num_iso_packets)
1329 return NULL;
1331 for (i = 0; i < _packet; i++)
1332 offset += transfer->iso_packet_desc[i].length;
1334 return transfer->buffer + offset;
1337 /** \ingroup asyncio
1338 * Convenience function to locate the position of an isochronous packet
1339 * within the buffer of an isochronous transfer, for transfers where each
1340 * packet is of identical size.
1342 * This function relies on the assumption that every packet within the transfer
1343 * is of identical size to the first packet. Calculating the location of
1344 * the packet buffer is then just a simple calculation:
1345 * <tt>buffer + (packet_size * packet)</tt>
1347 * Do not use this function on transfers other than those that have identical
1348 * packet lengths for each packet.
1350 * \param transfer a transfer
1351 * \param packet the packet to return the address of
1352 * \returns the base address of the packet buffer inside the transfer buffer,
1353 * or NULL if the packet does not exist.
1354 * \see libusb_get_iso_packet_buffer()
1356 static inline unsigned char *libusb_get_iso_packet_buffer_simple(
1357 struct libusb_transfer *transfer, unsigned int packet)
1359 int _packet;
1361 /* oops..slight bug in the API. packet is an unsigned int, but we use
1362 * signed integers almost everywhere else. range-check and convert to
1363 * signed to avoid compiler warnings. FIXME for libusb-2. */
1364 if (packet > INT_MAX)
1365 return NULL;
1366 _packet = packet;
1368 if (_packet >= transfer->num_iso_packets)
1369 return NULL;
1371 return transfer->buffer + (transfer->iso_packet_desc[0].length * _packet);
1374 /* sync I/O */
1376 int LIBUSB_CALL libusb_control_transfer(libusb_device_handle *dev_handle,
1377 uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
1378 unsigned char *data, uint16_t wLength, unsigned int timeout);
1380 int LIBUSB_CALL libusb_bulk_transfer(libusb_device_handle *dev_handle,
1381 unsigned char endpoint, unsigned char *data, int length,
1382 int *actual_length, unsigned int timeout);
1384 int LIBUSB_CALL libusb_interrupt_transfer(libusb_device_handle *dev_handle,
1385 unsigned char endpoint, unsigned char *data, int length,
1386 int *actual_length, unsigned int timeout);
1388 /** \ingroup desc
1389 * Retrieve a descriptor from the default control pipe.
1390 * This is a convenience function which formulates the appropriate control
1391 * message to retrieve the descriptor.
1393 * \param dev a device handle
1394 * \param desc_type the descriptor type, see \ref libusb_descriptor_type
1395 * \param desc_index the index of the descriptor to retrieve
1396 * \param data output buffer for descriptor
1397 * \param length size of data buffer
1398 * \returns number of bytes returned in data, or LIBUSB_ERROR code on failure
1400 static inline int libusb_get_descriptor(libusb_device_handle *dev,
1401 uint8_t desc_type, uint8_t desc_index, unsigned char *data, int length)
1403 return libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN,
1404 LIBUSB_REQUEST_GET_DESCRIPTOR, (desc_type << 8) | desc_index, 0, data,
1405 (uint16_t) length, 1000);
1408 /** \ingroup desc
1409 * Retrieve a descriptor from a device.
1410 * This is a convenience function which formulates the appropriate control
1411 * message to retrieve the descriptor. The string returned is Unicode, as
1412 * detailed in the USB specifications.
1414 * \param dev a device handle
1415 * \param desc_index the index of the descriptor to retrieve
1416 * \param langid the language ID for the string descriptor
1417 * \param data output buffer for descriptor
1418 * \param length size of data buffer
1419 * \returns number of bytes returned in data, or LIBUSB_ERROR code on failure
1420 * \see libusb_get_string_descriptor_ascii()
1422 static inline int libusb_get_string_descriptor(libusb_device_handle *dev,
1423 uint8_t desc_index, uint16_t langid, unsigned char *data, int length)
1425 return libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN,
1426 LIBUSB_REQUEST_GET_DESCRIPTOR, (uint16_t)((LIBUSB_DT_STRING << 8) | desc_index),
1427 langid, data, (uint16_t) length, 1000);
1430 int LIBUSB_CALL libusb_get_string_descriptor_ascii(libusb_device_handle *dev,
1431 uint8_t desc_index, unsigned char *data, int length);
1433 /* polling and timeouts */
1435 int LIBUSB_CALL libusb_try_lock_events(libusb_context *ctx);
1436 void LIBUSB_CALL libusb_lock_events(libusb_context *ctx);
1437 void LIBUSB_CALL libusb_unlock_events(libusb_context *ctx);
1438 int LIBUSB_CALL libusb_event_handling_ok(libusb_context *ctx);
1439 int LIBUSB_CALL libusb_event_handler_active(libusb_context *ctx);
1440 void LIBUSB_CALL libusb_lock_event_waiters(libusb_context *ctx);
1441 void LIBUSB_CALL libusb_unlock_event_waiters(libusb_context *ctx);
1442 int LIBUSB_CALL libusb_wait_for_event(libusb_context *ctx, struct timeval *tv);
1444 int LIBUSB_CALL libusb_handle_events_timeout(libusb_context *ctx,
1445 struct timeval *tv);
1446 int LIBUSB_CALL libusb_handle_events_timeout_completed(libusb_context *ctx,
1447 struct timeval *tv, int *completed);
1448 int LIBUSB_CALL libusb_handle_events(libusb_context *ctx);
1449 int LIBUSB_CALL libusb_handle_events_completed(libusb_context *ctx, int *completed);
1450 int LIBUSB_CALL libusb_handle_events_locked(libusb_context *ctx,
1451 struct timeval *tv);
1452 int LIBUSB_CALL libusb_pollfds_handle_timeouts(libusb_context *ctx);
1453 int LIBUSB_CALL libusb_get_next_timeout(libusb_context *ctx,
1454 struct timeval *tv);
1456 /** \ingroup poll
1457 * File descriptor for polling
1459 struct libusb_pollfd {
1460 /** Numeric file descriptor */
1461 int fd;
1463 /** Event flags to poll for from <poll.h>. POLLIN indicates that you
1464 * should monitor this file descriptor for becoming ready to read from,
1465 * and POLLOUT indicates that you should monitor this file descriptor for
1466 * nonblocking write readiness. */
1467 short events;
1470 /** \ingroup poll
1471 * Callback function, invoked when a new file descriptor should be added
1472 * to the set of file descriptors monitored for events.
1473 * \param fd the new file descriptor
1474 * \param events events to monitor for, see \ref libusb_pollfd for a
1475 * description
1476 * \param user_data User data pointer specified in
1477 * libusb_set_pollfd_notifiers() call
1478 * \see libusb_set_pollfd_notifiers()
1480 typedef void (LIBUSB_CALL *libusb_pollfd_added_cb)(int fd, short events,
1481 void *user_data);
1483 /** \ingroup poll
1484 * Callback function, invoked when a file descriptor should be removed from
1485 * the set of file descriptors being monitored for events. After returning
1486 * from this callback, do not use that file descriptor again.
1487 * \param fd the file descriptor to stop monitoring
1488 * \param user_data User data pointer specified in
1489 * libusb_set_pollfd_notifiers() call
1490 * \see libusb_set_pollfd_notifiers()
1492 typedef void (LIBUSB_CALL *libusb_pollfd_removed_cb)(int fd, void *user_data);
1494 const struct libusb_pollfd ** LIBUSB_CALL libusb_get_pollfds(
1495 libusb_context *ctx);
1496 void LIBUSB_CALL libusb_set_pollfd_notifiers(libusb_context *ctx,
1497 libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb,
1498 void *user_data);
1500 #ifdef __cplusplus
1502 #endif
1504 #endif