2 * Public libusb header file
3 * Copyright (C) 2007-2008 Daniel Drake <dsd@gentoo.org>
4 * Copyright (c) 2001 Johannes Erdfelt <johannes@erdfelt.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include <sys/types.h>
34 /** \def libusb_cpu_to_le16
36 * Convert a 16-bit value from host-endian to little-endian format. On
37 * little endian systems, this function does nothing. On big endian systems,
38 * the bytes are swapped.
39 * \param x the host-endian value to convert
40 * \returns the value in little-endian byte order
42 #define libusb_cpu_to_le16(x) ({ \
47 uint16_t _tmp2 = (uint16_t)(x); \
48 _tmp.b8[1] = _tmp2 >> 8; \
49 _tmp.b8[0] = _tmp2 & 0xff; \
53 /** \def libusb_le16_to_cpu
55 * Convert a 16-bit value from little-endian to host-endian format. On
56 * little endian systems, this function does nothing. On big endian systems,
57 * the bytes are swapped.
58 * \param x the little-endian value to convert
59 * \returns the value in host-endian byte order
61 #define libusb_le16_to_cpu libusb_cpu_to_le16
63 /* standard USB stuff */
66 * Device and/or Interface Class codes */
67 enum libusb_class_code
{
68 /** In the context of a \ref libusb_device_descriptor "device descriptor",
69 * this bDeviceClass value indicates that each interface specifies its
70 * own class information and all interfaces operate independently.
72 LIBUSB_CLASS_PER_INTERFACE
= 0,
75 LIBUSB_CLASS_AUDIO
= 1,
77 /** Communications class */
78 LIBUSB_CLASS_COMM
= 2,
80 /** Human Interface Device class */
84 LIBUSB_CLASS_PRINTER
= 7,
86 /** Picture transfer protocol class */
89 /** Mass storage class */
90 LIBUSB_CLASS_MASS_STORAGE
= 8,
96 LIBUSB_CLASS_DATA
= 10,
98 /** Class is vendor-specific */
99 LIBUSB_CLASS_VENDOR_SPEC
= 0xff
103 * Descriptor types as defined by the USB specification. */
104 enum libusb_descriptor_type
{
105 /** Device descriptor. See libusb_device_descriptor. */
106 LIBUSB_DT_DEVICE
= 0x01,
108 /** Configuration descriptor. See libusb_config_descriptor. */
109 LIBUSB_DT_CONFIG
= 0x02,
111 /** String descriptor */
112 LIBUSB_DT_STRING
= 0x03,
114 /** Interface descriptor. See libusb_interface_descriptor. */
115 LIBUSB_DT_INTERFACE
= 0x04,
117 /** Endpoint descriptor. See libusb_endpoint_descriptor. */
118 LIBUSB_DT_ENDPOINT
= 0x05,
120 /** HID descriptor */
121 LIBUSB_DT_HID
= 0x21,
123 /** HID report descriptor */
124 LIBUSB_DT_REPORT
= 0x22,
126 /** Physical descriptor */
127 LIBUSB_DT_PHYSICAL
= 0x23,
129 /** Hub descriptor */
133 /* Descriptor sizes per descriptor type */
134 #define LIBUSB_DT_DEVICE_SIZE 18
135 #define LIBUSB_DT_CONFIG_SIZE 9
136 #define LIBUSB_DT_INTERFACE_SIZE 9
137 #define LIBUSB_DT_ENDPOINT_SIZE 7
138 #define LIBUSB_DT_ENDPOINT_AUDIO_SIZE 9 /* Audio extension */
139 #define LIBUSB_DT_HUB_NONVAR_SIZE 7
141 #define LIBUSB_ENDPOINT_ADDRESS_MASK 0x0f /* in bEndpointAddress */
142 #define LIBUSB_ENDPOINT_DIR_MASK 0x80
145 * Endpoint direction. Values for bit 7 of the
146 * \ref libusb_endpoint_descriptor::bEndpointAddress "endpoint address" scheme.
148 enum libusb_endpoint_direction
{
149 /** In: device-to-host */
150 LIBUSB_ENDPOINT_IN
= 0x80,
152 /** Out: host-to-device */
153 LIBUSB_ENDPOINT_OUT
= 0x00
156 #define LIBUSB_TRANSFER_TYPE_MASK 0x03 /* in bmAttributes */
159 * Endpoint transfer type. Values for bits 0:1 of the
160 * \ref libusb_endpoint_descriptor::bmAttributes "endpoint attributes" field.
162 enum libusb_transfer_type
{
163 /** Control endpoint */
164 LIBUSB_TRANSFER_TYPE_CONTROL
= 0,
166 /** Isochronous endpoint */
167 LIBUSB_TRANSFER_TYPE_ISOCHRONOUS
= 1,
170 LIBUSB_TRANSFER_TYPE_BULK
= 2,
172 /** Interrupt endpoint */
173 LIBUSB_TRANSFER_TYPE_INTERRUPT
= 3
177 * Standard requests, as defined in table 9-3 of the USB2 specifications */
178 enum libusb_standard_request
{
179 /** Request status of the specific recipient */
180 LIBUSB_REQUEST_GET_STATUS
= 0x00,
182 /** Clear or disable a specific feature */
183 LIBUSB_REQUEST_CLEAR_FEATURE
= 0x01,
185 /* 0x02 is reserved */
187 /** Set or enable a specific feature */
188 LIBUSB_REQUEST_SET_FEATURE
= 0x03,
190 /* 0x04 is reserved */
192 /** Set device address for all future accesses */
193 LIBUSB_REQUEST_SET_ADDRESS
= 0x05,
195 /** Get the specified descriptor */
196 LIBUSB_REQUEST_GET_DESCRIPTOR
= 0x06,
198 /** Used to update existing descriptors or add new descriptors */
199 LIBUSB_REQUEST_SET_DESCRIPTOR
= 0x07,
201 /** Get the current device configuration value */
202 LIBUSB_REQUEST_GET_CONFIGURATION
= 0x08,
204 /** Set device configuration */
205 LIBUSB_REQUEST_SET_CONFIGURATION
= 0x09,
207 /** Return the selected alternate setting for the specified interface */
208 LIBUSB_REQUEST_GET_INTERFACE
= 0x0A,
210 /** Select an alternate interface for the specified interface */
211 LIBUSB_REQUEST_SET_INTERFACE
= 0x0B,
213 /** Set then report an endpoint's synchronization frame */
214 LIBUSB_REQUEST_SYNCH_FRAME
= 0x0C
218 * Request type bits of the
219 * \ref libusb_control_setup::bmRequestType "bmRequestType" field in control
221 enum libusb_request_type
{
223 LIBUSB_REQUEST_TYPE_STANDARD
= (0x00 << 5),
226 LIBUSB_REQUEST_TYPE_CLASS
= (0x01 << 5),
229 LIBUSB_REQUEST_TYPE_VENDOR
= (0x02 << 5),
232 LIBUSB_REQUEST_TYPE_RESERVED
= (0x03 << 5)
236 * Recipient bits of the
237 * \ref libusb_control_setup::bmRequestType "bmRequestType" field in control
238 * transfers. Values 4 through 31 are reserved. */
239 enum libusb_request_recipient
{
241 LIBUSB_RECIPIENT_DEVICE
= 0x00,
244 LIBUSB_RECIPIENT_INTERFACE
= 0x01,
247 LIBUSB_RECIPIENT_ENDPOINT
= 0x02,
250 LIBUSB_RECIPIENT_OTHER
= 0x03
253 #define LIBUSB_ISO_SYNC_TYPE_MASK 0x0C
256 * Synchronization type for isochronous endpoints. Values for bits 2:3 of the
257 * \ref libusb_endpoint_descriptor::bmAttributes "bmAttributes" field in
258 * libusb_endpoint_descriptor.
260 enum libusb_iso_sync_type
{
261 /** No synchronization */
262 LIBUSB_ISO_SYNC_TYPE_NONE
= 0,
265 LIBUSB_ISO_SYNC_TYPE_ASYNC
= 1,
268 LIBUSB_ISO_SYNC_TYPE_ADAPTIVE
= 2,
271 LIBUSB_ISO_SYNC_TYPE_SYNC
= 3
274 #define LIBUSB_ISO_USAGE_TYPE_MASK 0x30
277 * Usage type for isochronous endpoints. Values for bits 4:5 of the
278 * \ref libusb_endpoint_descriptor::bmAttributes "bmAttributes" field in
279 * libusb_endpoint_descriptor.
281 enum libusb_iso_usage_type
{
283 LIBUSB_ISO_USAGE_TYPE_DATA
= 0,
285 /** Feedback endpoint */
286 LIBUSB_ISO_USAGE_TYPE_FEEDBACK
= 1,
288 /** Implicit feedback Data endpoint */
289 LIBUSB_ISO_USAGE_TYPE_IMPLICIT
= 2
293 * A structure representing the standard USB device descriptor. This
294 * descriptor is documented in section 9.6.1 of the USB 2.0 specification.
295 * All multiple-byte fields are represented in host-endian format.
297 struct libusb_device_descriptor
{
298 /** Size of this descriptor (in bytes) */
301 /** Descriptor type. Will have value
302 * \ref libusb_descriptor_type::LIBUSB_DT_DEVICE LIBUSB_DT_DEVICE in this
304 uint8_t bDescriptorType
;
306 /** USB specification release number in binary-coded decimal. A value of
307 * 0x0200 indicates USB 2.0, 0x0110 indicates USB 1.1, etc. */
310 /** USB-IF class code for the device. See \ref libusb_class_code. */
311 uint8_t bDeviceClass
;
313 /** USB-IF subclass code for the device, qualified by the bDeviceClass
315 uint8_t bDeviceSubClass
;
317 /** USB-IF protocol code for the device, qualified by the bDeviceClass and
318 * bDeviceSubClass values */
319 uint8_t bDeviceProtocol
;
321 /** Maximum packet size for endpoint 0 */
322 uint8_t bMaxPacketSize0
;
324 /** USB-IF vendor ID */
327 /** USB-IF product ID */
330 /** Device release number in binary-coded decimal */
333 /** Index of string descriptor describing manufacturer */
334 uint8_t iManufacturer
;
336 /** Index of string descriptor describing product */
339 /** Index of string descriptor containing device serial number */
340 uint8_t iSerialNumber
;
342 /** Number of possible configurations */
343 uint8_t bNumConfigurations
;
347 * A structure representing the standard USB endpoint descriptor. This
348 * descriptor is documented in section 9.6.3 of the USB 2.0 specification.
349 * All multiple-byte fields are represented in host-endian format.
351 struct libusb_endpoint_descriptor
{
352 /** Size of this descriptor (in bytes) */
355 /** Descriptor type. Will have value
356 * \ref libusb_descriptor_type::LIBUSB_DT_ENDPOINT LIBUSB_DT_ENDPOINT in
358 uint8_t bDescriptorType
;
360 /** The address of the endpoint described by this descriptor. Bits 0:3 are
361 * the endpoint number. Bits 4:6 are reserved. Bit 7 indicates direction,
362 * see \ref libusb_endpoint_direction.
364 uint8_t bEndpointAddress
;
366 /** Attributes which apply to the endpoint when it is configured using
367 * the bConfigurationValue. Bits 0:1 determine the transfer type and
368 * correspond to \ref libusb_transfer_type. Bits 2:3 are only used for
369 * isochronous endpoints and correspond to \ref libusb_iso_sync_type.
370 * Bits 4:5 are also only used for isochronous endpoints and correspond to
371 * \ref libusb_iso_usage_type. Bits 6:7 are reserved.
373 uint8_t bmAttributes
;
375 /** Maximum packet size this endpoint is capable of sending/receiving. */
376 uint16_t wMaxPacketSize
;
378 /** Interval for polling endpoint for data transfers. */
381 /** For audio devices only: the rate at which synchronization feedback
385 /** For audio devices only: the address if the synch endpoint */
386 uint8_t bSynchAddress
;
388 /** Extra descriptors. If libusb encounters unknown endpoint descriptors,
389 * it will store them here, should you wish to parse them. */
390 const unsigned char *extra
;
392 /** Length of the extra descriptors, in bytes. */
397 * A structure representing the standard USB interface descriptor. This
398 * descriptor is documented in section 9.6.5 of the USB 2.0 specification.
399 * All multiple-byte fields are represented in host-endian format.
401 struct libusb_interface_descriptor
{
402 /** Size of this descriptor (in bytes) */
405 /** Descriptor type. Will have value
406 * \ref libusb_descriptor_type::LIBUSB_DT_INTERFACE LIBUSB_DT_INTERFACE
407 * in this context. */
408 uint8_t bDescriptorType
;
410 /** Number of this interface */
411 uint8_t bInterfaceNumber
;
413 /** Value used to select this alternate setting for this interface */
414 uint8_t bAlternateSetting
;
416 /** Number of endpoints used by this interface (excluding the control
418 uint8_t bNumEndpoints
;
420 /** USB-IF class code for this interface. See \ref libusb_class_code. */
421 uint8_t bInterfaceClass
;
423 /** USB-IF subclass code for this interface, qualified by the
424 * bInterfaceClass value */
425 uint8_t bInterfaceSubClass
;
427 /** USB-IF protocol code for this interface, qualified by the
428 * bInterfaceClass and bInterfaceSubClass values */
429 uint8_t bInterfaceProtocol
;
431 /** Index of string descriptor describing this interface */
434 /** Array of endpoint descriptors. This length of this array is determined
435 * by the bNumEndpoints field. */
436 const struct libusb_endpoint_descriptor
*endpoint
;
438 /** Extra descriptors. If libusb encounters unknown interface descriptors,
439 * it will store them here, should you wish to parse them. */
440 const unsigned char *extra
;
442 /** Length of the extra descriptors, in bytes. */
447 * A collection of alternate settings for a particular USB interface.
449 struct libusb_interface
{
450 /** Array of interface descriptors. The length of this array is determined
451 * by the num_altsetting field. */
452 const struct libusb_interface_descriptor
*altsetting
;
454 /** The number of alternate settings that belong to this interface */
459 * A structure representing the standard USB configuration descriptor. This
460 * descriptor is documented in section 9.6.3 of the USB 2.0 specification.
461 * All multiple-byte fields are represented in host-endian format.
463 struct libusb_config_descriptor
{
464 /** Size of this descriptor (in bytes) */
467 /** Descriptor type. Will have value
468 * \ref libusb_descriptor_type::LIBUSB_DT_CONFIG LIBUSB_DT_CONFIG
469 * in this context. */
470 uint8_t bDescriptorType
;
472 /** Total length of data returned for this configuration */
473 uint16_t wTotalLength
;
475 /** Number of interfaces supported by this configuration */
476 uint8_t bNumInterfaces
;
478 /** Identifier value for this configuration */
479 uint8_t bConfigurationValue
;
481 /** Index of string descriptor describing this configuration */
482 uint8_t iConfiguration
;
484 /** Configuration characteristics */
485 uint8_t bmAttributes
;
487 /** Maximum power consumption of the USB device from this bus in this
488 * configuration when the device is fully opreation. Expressed in units
492 /** Array of interfaces supported by this configuration. The length of
493 * this array is determined by the bNumInterfaces field. */
494 const struct libusb_interface
*interface
;
496 /** Extra descriptors. If libusb encounters unknown configuration
497 * descriptors, it will store them here, should you wish to parse them. */
498 const unsigned char *extra
;
500 /** Length of the extra descriptors, in bytes. */
505 * Setup packet for control transfers. */
506 struct libusb_control_setup
{
507 /** Request type. Bits 0:4 determine recipient, see
508 * \ref libusb_request_recipient. Bits 5:6 determine type, see
509 * \ref libusb_request_type. Bit 7 determines data transfer direction, see
510 * \ref libusb_endpoint_direction.
512 uint8_t bmRequestType
;
514 /** Request. If the type bits of bmRequestType are equal to
515 * \ref libusb_request_type::LIBUSB_REQUEST_TYPE_STANDARD
516 * "LIBUSB_REQUEST_TYPE_STANDARD" then this field refers to
517 * \ref libusb_standard_request. For other cases, use of this field is
518 * application-specific. */
521 /** Value. Varies according to request */
524 /** Index. Varies according to request, typically used to pass an index
528 /** Number of bytes to transfer */
532 #define LIBUSB_CONTROL_SETUP_SIZE (sizeof(struct libusb_control_setup))
536 struct libusb_context
;
537 struct libusb_device
;
538 struct libusb_device_handle
;
541 * Structure representing a libusb session. The concept of individual libusb
542 * sessions allows for your program to use two libraries (or dynamically
543 * load two modules) which both independently use libusb. This will prevent
544 * interference between the individual libusb users - for example
545 * libusb_set_debug() will not affect the other user of the library, and
546 * libusb_exit() will not destroy resources that the other user is still
549 * Sessions are created by libusb_init() and destroyed through libusb_exit().
550 * If your application is guaranteed to only ever include a single libusb
551 * user (i.e. you), you do not have to worry about contexts: pass NULL in
552 * every function call where a context is required. The default context
555 * For more information, see \ref contexts.
557 typedef struct libusb_context libusb_context
;
560 * Structure representing a USB device detected on the system. This is an
561 * opaque type for which you are only ever provided with a pointer, usually
562 * originating from libusb_get_device_list().
564 * Certain operations can be performed on a device, but in order to do any
565 * I/O you will have to first obtain a device handle using libusb_open().
567 * Devices are reference counted with libusb_device_ref() and
568 * libusb_device_unref(), and are freed when the reference count reaches 0.
569 * New devices presented by libusb_get_device_list() have a reference count of
570 * 1, and libusb_free_device_list() can optionally decrease the reference count
571 * on all devices in the list. libusb_open() adds another reference which is
572 * later destroyed by libusb_close().
574 typedef struct libusb_device libusb_device
;
578 * Structure representing a handle on a USB device. This is an opaque type for
579 * which you are only ever provided with a pointer, usually originating from
582 * A device handle is used to perform I/O and other operations. When finished
583 * with a device handle, you should call libusb_close().
585 typedef struct libusb_device_handle libusb_device_handle
;
588 * Error codes. Most libusb functions return 0 on success or one of these
592 /** Success (no error) */
595 /** Input/output error */
596 LIBUSB_ERROR_IO
= -1,
598 /** Invalid parameter */
599 LIBUSB_ERROR_INVALID_PARAM
= -2,
601 /** Access denied (insufficient permissions) */
602 LIBUSB_ERROR_ACCESS
= -3,
604 /** No such device (it may have been disconnected) */
605 LIBUSB_ERROR_NO_DEVICE
= -4,
607 /** Entity not found */
608 LIBUSB_ERROR_NOT_FOUND
= -5,
611 LIBUSB_ERROR_BUSY
= -6,
613 /** Operation timed out */
614 LIBUSB_ERROR_TIMEOUT
= -7,
617 LIBUSB_ERROR_OVERFLOW
= -8,
620 LIBUSB_ERROR_PIPE
= -9,
622 /** System call interrupted (perhaps due to signal) */
623 LIBUSB_ERROR_INTERRUPTED
= -10,
625 /** Insufficient memory */
626 LIBUSB_ERROR_NO_MEM
= -11,
628 /** Operation not supported or unimplemented on this platform */
629 LIBUSB_ERROR_NOT_SUPPORTED
= -12,
632 LIBUSB_ERROR_OTHER
= -99
636 * Transfer status codes */
637 enum libusb_transfer_status
{
638 /** Transfer completed without error. Note that this does not indicate
639 * that the entire amount of requested data was transferred. */
640 LIBUSB_TRANSFER_COMPLETED
,
642 /** Transfer failed */
643 LIBUSB_TRANSFER_ERROR
,
645 /** Transfer timed out */
646 LIBUSB_TRANSFER_TIMED_OUT
,
648 /** Transfer was cancelled */
649 LIBUSB_TRANSFER_CANCELLED
,
651 /** For bulk/interrupt endpoints: halt condition detected (endpoint
652 * stalled). For control endpoints: control request not supported. */
653 LIBUSB_TRANSFER_STALL
,
655 /** Device was disconnected */
656 LIBUSB_TRANSFER_NO_DEVICE
,
658 /** Device sent more data than requested */
659 LIBUSB_TRANSFER_OVERFLOW
663 * libusb_transfer.flags values */
664 enum libusb_transfer_flags
{
665 /** Report short frames as errors */
666 LIBUSB_TRANSFER_SHORT_NOT_OK
= 1<<0,
668 /** Automatically free() transfer buffer during libusb_free_transfer() */
669 LIBUSB_TRANSFER_FREE_BUFFER
= 1<<1,
671 /** Automatically call libusb_free_transfer() after callback returns.
672 * If this flag is set, it is illegal to call libusb_free_transfer()
673 * from your transfer callback, as this will result in a double-free
674 * when this flag is acted upon. */
675 LIBUSB_TRANSFER_FREE_TRANSFER
= 1<<2
679 * Isochronous packet descriptor. */
680 struct libusb_iso_packet_descriptor
{
681 /** Length of data to request in this packet */
684 /** Amount of data that was actually transferred */
685 unsigned int actual_length
;
687 /** Status code for this packet */
688 enum libusb_transfer_status status
;
691 struct libusb_transfer
;
694 * Asynchronous transfer callback function type. When submitting asynchronous
695 * transfers, you pass a pointer to a callback function of this type via the
696 * \ref libusb_transfer::callback "callback" member of the libusb_transfer
697 * structure. libusb will call this function later, when the transfer has
698 * completed or failed. See \ref asyncio for more information.
699 * \param transfer The libusb_transfer struct the callback function is being
702 typedef void (*libusb_transfer_cb_fn
)(struct libusb_transfer
*transfer
);
705 * The generic USB transfer structure. The user populates this structure and
706 * then submits it in order to request a transfer. After the transfer has
707 * completed, the library populates the transfer with the results and passes
708 * it back to the user.
710 struct libusb_transfer
{
711 /** Handle of the device that this transfer will be submitted to */
712 libusb_device_handle
*dev_handle
;
714 /** A bitwise OR combination of \ref libusb_transfer_flags. */
717 /** Address of the endpoint where this transfer will be sent. */
718 unsigned char endpoint
;
720 /** Type of the endpoint from \ref libusb_transfer_type */
723 /** Timeout for this transfer in millseconds. A value of 0 indicates no
725 unsigned int timeout
;
727 /** The status of the transfer. Read-only, and only for use within
728 * transfer callback function.
730 * If this is an isochronous transfer, this field may read COMPLETED even
731 * if there were errors in the frames. Use the
732 * \ref libusb_iso_packet_descriptor::status "status" field in each packet
733 * to determine if errors occurred. */
734 enum libusb_transfer_status status
;
736 /** Length of the data buffer */
739 /** Actual length of data that was transferred. Read-only, and only for
740 * use within transfer callback function. Not valid for isochronous
741 * endpoint transfers. */
744 /** Callback function. This will be invoked when the transfer completes,
745 * fails, or is cancelled. */
746 libusb_transfer_cb_fn callback
;
748 /** User context data to pass to the callback function. */
752 unsigned char *buffer
;
754 /** Number of isochronous packets. Only used for I/O with isochronous
758 /** Isochronous packet descriptors, for isochronous transfers only. */
759 struct libusb_iso_packet_descriptor iso_packet_desc
760 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
761 [] /* valid C99 code */
763 [0] /* non-standard, but usually working code */
768 int libusb_init(libusb_context
**ctx
);
769 void libusb_exit(libusb_context
*ctx
);
770 void libusb_set_debug(libusb_context
*ctx
, int level
);
772 ssize_t
libusb_get_device_list(libusb_context
*ctx
,
773 libusb_device
***list
);
774 void libusb_free_device_list(libusb_device
**list
, int unref_devices
);
775 libusb_device
*libusb_ref_device(libusb_device
*dev
);
776 void libusb_unref_device(libusb_device
*dev
);
778 int libusb_get_configuration(libusb_device_handle
*dev
, int *config
);
779 int libusb_get_device_descriptor(libusb_device
*dev
,
780 struct libusb_device_descriptor
*desc
);
781 int libusb_get_active_config_descriptor(libusb_device
*dev
,
782 struct libusb_config_descriptor
**config
);
783 int libusb_get_config_descriptor(libusb_device
*dev
, uint8_t config_index
,
784 struct libusb_config_descriptor
**config
);
785 int libusb_get_config_descriptor_by_value(libusb_device
*dev
,
786 uint8_t bConfigurationValue
, struct libusb_config_descriptor
**config
);
787 void libusb_free_config_descriptor(struct libusb_config_descriptor
*config
);
788 uint8_t libusb_get_bus_number(libusb_device
*dev
);
789 uint8_t libusb_get_device_address(libusb_device
*dev
);
790 int libusb_get_max_packet_size(libusb_device
*dev
, unsigned char endpoint
);
791 int libusb_get_max_iso_packet_size(libusb_device
*dev
, unsigned char endpoint
);
793 int libusb_open(libusb_device
*dev
, libusb_device_handle
**handle
);
794 void libusb_close(libusb_device_handle
*dev_handle
);
795 libusb_device
*libusb_get_device(libusb_device_handle
*dev_handle
);
797 int libusb_set_configuration(libusb_device_handle
*dev
, int configuration
);
798 int libusb_claim_interface(libusb_device_handle
*dev
, int iface
);
799 int libusb_release_interface(libusb_device_handle
*dev
, int iface
);
801 libusb_device_handle
*libusb_open_device_with_vid_pid(libusb_context
*ctx
,
802 uint16_t vendor_id
, uint16_t product_id
);
804 int libusb_set_interface_alt_setting(libusb_device_handle
*dev
,
805 int interface_number
, int alternate_setting
);
806 int libusb_clear_halt(libusb_device_handle
*dev
, unsigned char endpoint
);
807 int libusb_reset_device(libusb_device_handle
*dev
);
809 int libusb_kernel_driver_active(libusb_device_handle
*dev
, int interface
);
810 int libusb_detach_kernel_driver(libusb_device_handle
*dev
, int interface
);
811 int libusb_attach_kernel_driver(libusb_device_handle
*dev
, int interface
);
816 * Get the data section of a control transfer. This convenience function is here
817 * to remind you that the data does not start until 8 bytes into the actual
818 * buffer, as the setup packet comes first.
820 * Calling this function only makes sense from a transfer callback function,
821 * or situations where you have already allocated a suitably sized buffer at
824 * \param transfer a transfer
825 * \returns pointer to the first byte of the data section
827 static inline unsigned char *libusb_control_transfer_get_data(
828 struct libusb_transfer
*transfer
)
830 return transfer
->buffer
+ LIBUSB_CONTROL_SETUP_SIZE
;
834 * Get the control setup packet of a control transfer. This convenience
835 * function is here to remind you that the control setup occupies the first
836 * 8 bytes of the transfer data buffer.
838 * Calling this function only makes sense from a transfer callback function,
839 * or situations where you have already allocated a suitably sized buffer at
842 * \param transfer a transfer
843 * \returns a casted pointer to the start of the transfer data buffer
845 static inline struct libusb_control_setup
*libusb_control_transfer_get_setup(
846 struct libusb_transfer
*transfer
)
848 return (struct libusb_control_setup
*) transfer
->buffer
;
852 * Helper function to populate the setup packet (first 8 bytes of the data
853 * buffer) for a control transfer. The wIndex, wValue and wLength values should
854 * be given in host-endian byte order.
856 * \param buffer buffer to output the setup packet into
857 * \param bmRequestType see the
858 * \ref libusb_control_setup::bmRequestType "bmRequestType" field of
859 * \ref libusb_control_setup
860 * \param bRequest see the
861 * \ref libusb_control_setup::bRequest "bRequest" field of
862 * \ref libusb_control_setup
863 * \param wValue see the
864 * \ref libusb_control_setup::wValue "wValue" field of
865 * \ref libusb_control_setup
866 * \param wIndex see the
867 * \ref libusb_control_setup::wIndex "wIndex" field of
868 * \ref libusb_control_setup
869 * \param wLength see the
870 * \ref libusb_control_setup::wLength "wLength" field of
871 * \ref libusb_control_setup
873 static inline void libusb_fill_control_setup(unsigned char *buffer
,
874 uint8_t bmRequestType
, uint8_t bRequest
, uint16_t wValue
, uint16_t wIndex
,
877 struct libusb_control_setup
*setup
= (struct libusb_control_setup
*) buffer
;
878 setup
->bmRequestType
= bmRequestType
;
879 setup
->bRequest
= bRequest
;
880 setup
->wValue
= libusb_cpu_to_le16(wValue
);
881 setup
->wIndex
= libusb_cpu_to_le16(wIndex
);
882 setup
->wLength
= libusb_cpu_to_le16(wLength
);
885 struct libusb_transfer
*libusb_alloc_transfer(int iso_packets
);
886 int libusb_submit_transfer(struct libusb_transfer
*transfer
);
887 int libusb_cancel_transfer(struct libusb_transfer
*transfer
);
888 void libusb_free_transfer(struct libusb_transfer
*transfer
);
891 * Helper function to populate the required \ref libusb_transfer fields
892 * for a control transfer.
894 * If you pass a transfer buffer to this function, the first 8 bytes will
895 * be interpreted as a control setup packet, and the wLength field will be
896 * used to automatically populate the \ref libusb_transfer::length "length"
897 * field of the transfer. Therefore the recommended approach is:
898 * -# Allocate a suitably sized data buffer (including space for control setup)
899 * -# Call libusb_fill_control_setup()
900 * -# If this is a host-to-device transfer with a data stage, put the data
901 * in place after the setup packet
902 * -# Call this function
903 * -# Call libusb_submit_transfer()
905 * It is also legal to pass a NULL buffer to this function, in which case this
906 * function will not attempt to populate the length field. Remember that you
907 * must then populate the buffer and length fields later.
909 * \param transfer the transfer to populate
910 * \param dev_handle handle of the device that will handle the transfer
911 * \param buffer data buffer. If provided, this function will interpret the
912 * first 8 bytes as a setup packet and infer the transfer length from that.
913 * \param callback callback function to be invoked on transfer completion
914 * \param user_data user data to pass to callback function
915 * \param timeout timeout for the transfer in milliseconds
917 static inline void libusb_fill_control_transfer(
918 struct libusb_transfer
*transfer
, libusb_device_handle
*dev_handle
,
919 unsigned char *buffer
, libusb_transfer_cb_fn callback
, void *user_data
,
920 unsigned int timeout
)
922 struct libusb_control_setup
*setup
= (struct libusb_control_setup
*) buffer
;
923 transfer
->dev_handle
= dev_handle
;
924 transfer
->endpoint
= 0;
925 transfer
->type
= LIBUSB_TRANSFER_TYPE_CONTROL
;
926 transfer
->timeout
= timeout
;
927 transfer
->buffer
= buffer
;
929 transfer
->length
= LIBUSB_CONTROL_SETUP_SIZE
930 + libusb_le16_to_cpu(setup
->wLength
);
931 transfer
->user_data
= user_data
;
932 transfer
->callback
= callback
;
936 * Helper function to populate the required \ref libusb_transfer fields
937 * for a bulk transfer.
939 * \param transfer the transfer to populate
940 * \param dev_handle handle of the device that will handle the transfer
941 * \param endpoint address of the endpoint where this transfer will be sent
942 * \param buffer data buffer
943 * \param length length of data buffer
944 * \param callback callback function to be invoked on transfer completion
945 * \param user_data user data to pass to callback function
946 * \param timeout timeout for the transfer in milliseconds
948 static inline void libusb_fill_bulk_transfer(struct libusb_transfer
*transfer
,
949 libusb_device_handle
*dev_handle
, unsigned char endpoint
,
950 unsigned char *buffer
, int length
, libusb_transfer_cb_fn callback
,
951 void *user_data
, unsigned int timeout
)
953 transfer
->dev_handle
= dev_handle
;
954 transfer
->endpoint
= endpoint
;
955 transfer
->type
= LIBUSB_TRANSFER_TYPE_BULK
;
956 transfer
->timeout
= timeout
;
957 transfer
->buffer
= buffer
;
958 transfer
->length
= length
;
959 transfer
->user_data
= user_data
;
960 transfer
->callback
= callback
;
964 * Helper function to populate the required \ref libusb_transfer fields
965 * for an interrupt transfer.
967 * \param transfer the transfer to populate
968 * \param dev_handle handle of the device that will handle the transfer
969 * \param endpoint address of the endpoint where this transfer will be sent
970 * \param buffer data buffer
971 * \param length length of data buffer
972 * \param callback callback function to be invoked on transfer completion
973 * \param user_data user data to pass to callback function
974 * \param timeout timeout for the transfer in milliseconds
976 static inline void libusb_fill_interrupt_transfer(
977 struct libusb_transfer
*transfer
, libusb_device_handle
*dev_handle
,
978 unsigned char endpoint
, unsigned char *buffer
, int length
,
979 libusb_transfer_cb_fn callback
, void *user_data
, unsigned int timeout
)
981 transfer
->dev_handle
= dev_handle
;
982 transfer
->endpoint
= endpoint
;
983 transfer
->type
= LIBUSB_TRANSFER_TYPE_INTERRUPT
;
984 transfer
->timeout
= timeout
;
985 transfer
->buffer
= buffer
;
986 transfer
->length
= length
;
987 transfer
->user_data
= user_data
;
988 transfer
->callback
= callback
;
992 * Helper function to populate the required \ref libusb_transfer fields
993 * for an isochronous transfer.
995 * \param transfer the transfer to populate
996 * \param dev_handle handle of the device that will handle the transfer
997 * \param endpoint address of the endpoint where this transfer will be sent
998 * \param buffer data buffer
999 * \param length length of data buffer
1000 * \param num_iso_packets the number of isochronous packets
1001 * \param callback callback function to be invoked on transfer completion
1002 * \param user_data user data to pass to callback function
1003 * \param timeout timeout for the transfer in milliseconds
1005 static inline void libusb_fill_iso_transfer(struct libusb_transfer
*transfer
,
1006 libusb_device_handle
*dev_handle
, unsigned char endpoint
,
1007 unsigned char *buffer
, int length
, int num_iso_packets
,
1008 libusb_transfer_cb_fn callback
, void *user_data
, unsigned int timeout
)
1010 transfer
->dev_handle
= dev_handle
;
1011 transfer
->endpoint
= endpoint
;
1012 transfer
->type
= LIBUSB_TRANSFER_TYPE_ISOCHRONOUS
;
1013 transfer
->timeout
= timeout
;
1014 transfer
->buffer
= buffer
;
1015 transfer
->length
= length
;
1016 transfer
->num_iso_packets
= num_iso_packets
;
1017 transfer
->user_data
= user_data
;
1018 transfer
->callback
= callback
;
1021 /** \ingroup asyncio
1022 * Convenience function to set the length of all packets in an isochronous
1023 * transfer, based on the num_iso_packets field in the transfer structure.
1025 * \param transfer a transfer
1026 * \param length the length to set in each isochronous packet descriptor
1027 * \see libusb_get_max_packet_size()
1029 static inline void libusb_set_iso_packet_lengths(
1030 struct libusb_transfer
*transfer
, unsigned int length
)
1033 for (i
= 0; i
< transfer
->num_iso_packets
; i
++)
1034 transfer
->iso_packet_desc
[i
].length
= length
;
1037 /** \ingroup asyncio
1038 * Convenience function to locate the position of an isochronous packet
1039 * within the buffer of an isochronous transfer.
1041 * This is a thorough function which loops through all preceding packets,
1042 * accumulating their lengths to find the position of the specified packet.
1043 * Typically you will assign equal lengths to each packet in the transfer,
1044 * and hence the above method is sub-optimal. You may wish to use
1045 * libusb_get_iso_packet_buffer_simple() instead.
1047 * \param transfer a transfer
1048 * \param packet the packet to return the address of
1049 * \returns the base address of the packet buffer inside the transfer buffer,
1050 * or NULL if the packet does not exist.
1051 * \see libusb_get_iso_packet_buffer_simple()
1053 static inline unsigned char *libusb_get_iso_packet_buffer(
1054 struct libusb_transfer
*transfer
, unsigned int packet
)
1060 /* oops..slight bug in the API. packet is an unsigned int, but we use
1061 * signed integers almost everywhere else. range-check and convert to
1062 * signed to avoid compiler warnings. FIXME for libusb-2. */
1063 if (packet
> INT_MAX
)
1067 if (_packet
>= transfer
->num_iso_packets
)
1070 for (i
= 0; i
< _packet
; i
++)
1071 offset
+= transfer
->iso_packet_desc
[i
].length
;
1073 return transfer
->buffer
+ offset
;
1076 /** \ingroup asyncio
1077 * Convenience function to locate the position of an isochronous packet
1078 * within the buffer of an isochronous transfer, for transfers where each
1079 * packet is of identical size.
1081 * This function relies on the assumption that every packet within the transfer
1082 * is of identical size to the first packet. Calculating the location of
1083 * the packet buffer is then just a simple calculation:
1084 * <tt>buffer + (packet_size * packet)</tt>
1086 * Do not use this function on transfers other than those that have identical
1087 * packet lengths for each packet.
1089 * \param transfer a transfer
1090 * \param packet the packet to return the address of
1091 * \returns the base address of the packet buffer inside the transfer buffer,
1092 * or NULL if the packet does not exist.
1093 * \see libusb_get_iso_packet_buffer()
1095 static inline unsigned char *libusb_get_iso_packet_buffer_simple(
1096 struct libusb_transfer
*transfer
, unsigned int packet
)
1100 /* oops..slight bug in the API. packet is an unsigned int, but we use
1101 * signed integers almost everywhere else. range-check and convert to
1102 * signed to avoid compiler warnings. FIXME for libusb-2. */
1103 if (packet
> INT_MAX
)
1107 if (_packet
>= transfer
->num_iso_packets
)
1110 return transfer
->buffer
+ (transfer
->iso_packet_desc
[0].length
* _packet
);
1115 int libusb_control_transfer(libusb_device_handle
*dev_handle
,
1116 uint8_t request_type
, uint8_t request
, uint16_t value
, uint16_t index
,
1117 unsigned char *data
, uint16_t length
, unsigned int timeout
);
1119 int libusb_bulk_transfer(libusb_device_handle
*dev_handle
,
1120 unsigned char endpoint
, unsigned char *data
, int length
,
1121 int *actual_length
, unsigned int timeout
);
1123 int libusb_interrupt_transfer(libusb_device_handle
*dev_handle
,
1124 unsigned char endpoint
, unsigned char *data
, int length
,
1125 int *actual_length
, unsigned int timeout
);
1128 * Retrieve a descriptor from the default control pipe.
1129 * This is a convenience function which formulates the appropriate control
1130 * message to retrieve the descriptor.
1132 * \param dev a device handle
1133 * \param desc_type the descriptor type, see \ref libusb_descriptor_type
1134 * \param desc_index the index of the descriptor to retrieve
1135 * \param data output buffer for descriptor
1136 * \param length size of data buffer
1137 * \returns number of bytes returned in data, or LIBUSB_ERROR code on failure
1139 static inline int libusb_get_descriptor(libusb_device_handle
*dev
,
1140 uint8_t desc_type
, uint8_t desc_index
, unsigned char *data
, int length
)
1142 return libusb_control_transfer(dev
, LIBUSB_ENDPOINT_IN
,
1143 LIBUSB_REQUEST_GET_DESCRIPTOR
, (desc_type
<< 8) | desc_index
, 0, data
,
1148 * Retrieve a descriptor from a device.
1149 * This is a convenience function which formulates the appropriate control
1150 * message to retrieve the descriptor. The string returned is Unicode, as
1151 * detailed in the USB specifications.
1153 * \param dev a device handle
1154 * \param desc_index the index of the descriptor to retrieve
1155 * \param langid the language ID for the string descriptor
1156 * \param data output buffer for descriptor
1157 * \param length size of data buffer
1158 * \returns number of bytes returned in data, or LIBUSB_ERROR code on failure
1159 * \see libusb_get_string_descriptor_ascii()
1161 static inline int libusb_get_string_descriptor(libusb_device_handle
*dev
,
1162 uint8_t desc_index
, uint16_t langid
, unsigned char *data
, int length
)
1164 return libusb_control_transfer(dev
, LIBUSB_ENDPOINT_IN
,
1165 LIBUSB_REQUEST_GET_DESCRIPTOR
, (LIBUSB_DT_STRING
<< 8) | desc_index
,
1166 langid
, data
, length
, 1000);
1169 int libusb_get_string_descriptor_ascii(libusb_device_handle
*dev
,
1170 uint8_t index
, unsigned char *data
, int length
);
1172 /* polling and timeouts */
1174 int libusb_try_lock_events(libusb_context
*ctx
);
1175 void libusb_lock_events(libusb_context
*ctx
);
1176 void libusb_unlock_events(libusb_context
*ctx
);
1177 int libusb_event_handling_ok(libusb_context
*ctx
);
1178 int libusb_event_handler_active(libusb_context
*ctx
);
1179 void libusb_lock_event_waiters(libusb_context
*ctx
);
1180 void libusb_unlock_event_waiters(libusb_context
*ctx
);
1181 int libusb_wait_for_event(libusb_context
*ctx
, struct timeval
*tv
);
1183 int libusb_handle_events_timeout(libusb_context
*ctx
, struct timeval
*tv
);
1184 int libusb_handle_events(libusb_context
*ctx
);
1185 int libusb_handle_events_locked(libusb_context
*ctx
, struct timeval
*tv
);
1186 int libusb_pollfds_handle_timeouts(libusb_context
*ctx
);
1187 int libusb_get_next_timeout(libusb_context
*ctx
, struct timeval
*tv
);
1190 * File descriptor for polling
1192 struct libusb_pollfd
{
1193 /** Numeric file descriptor */
1196 /** Event flags to poll for from <poll.h>. POLLIN indicates that you
1197 * should monitor this file descriptor for becoming ready to read from,
1198 * and POLLOUT indicates that you should monitor this file descriptor for
1199 * nonblocking write readiness. */
1204 * Callback function, invoked when a new file descriptor should be added
1205 * to the set of file descriptors monitored for events.
1206 * \param fd the new file descriptor
1207 * \param events events to monitor for, see \ref libusb_pollfd for a
1209 * \param user_data User data pointer specified in
1210 * libusb_set_pollfd_notifiers() call
1211 * \see libusb_set_pollfd_notifiers()
1213 typedef void (*libusb_pollfd_added_cb
)(int fd
, short events
, void *user_data
);
1216 * Callback function, invoked when a file descriptor should be removed from
1217 * the set of file descriptors being monitored for events. After returning
1218 * from this callback, do not use that file descriptor again.
1219 * \param fd the file descriptor to stop monitoring
1220 * \param user_data User data pointer specified in
1221 * libusb_set_pollfd_notifiers() call
1222 * \see libusb_set_pollfd_notifiers()
1224 typedef void (*libusb_pollfd_removed_cb
)(int fd
, void *user_data
);
1226 const struct libusb_pollfd
**libusb_get_pollfds(libusb_context
*ctx
);
1227 void libusb_set_pollfd_notifiers(libusb_context
*ctx
,
1228 libusb_pollfd_added_cb added_cb
, libusb_pollfd_removed_cb removed_cb
,