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
27 #include <sys/types.h>
30 #define bswap16(x) (((x & 0xff) << 8) | (x >> 8))
31 #if __BYTE_ORDER == __LITTLE_ENDIAN
32 #define libusb_cpu_to_le16(x) (x)
33 #define libusb_le16_to_cpu(x) (x)
34 #elif __BYTE_ORDER == __BIG_ENDIAN
35 #define libusb_le16_to_cpu(x) bswap16(x)
36 #define libusb_cpu_to_le16(x) bswap16(x)
38 #error "Unrecognized endianness"
41 /** \def libusb_cpu_to_le16
43 * Convert a 16-bit value from host-endian to little-endian format. On
44 * little endian systems, this function does nothing. On big endian systems,
45 * the bytes are swapped.
46 * \param x the host-endian value to convert
47 * \returns the value in little-endian byte order
50 /** \def libusb_le16_to_cpu
52 * Convert a 16-bit value from little-endian to host-endian format. On
53 * little endian systems, this function does nothing. On big endian systems,
54 * the bytes are swapped.
55 * \param x the little-endian value to convert
56 * \returns the value in host-endian byte order
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 */
130 LIBUSB_DT_HUB = 0x29,
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_device;
537 struct libusb_device_handle;
540 * Structure representing a USB device detected on the system. This is an
541 * opaque type for which you are only ever provided with a pointer, usually
542 * originating from libusb_get_device_list().
544 * Certain operations can be performed on a device, but in order to do any
545 * I/O you will have to first obtain a device handle using libusb_open().
547 * Devices are reference counted with libusb_device_ref() and
548 * libusb_device_unref(), and are freed when the reference count reaches 0.
549 * New devices presented by libusb_get_device_list() have a reference count of
550 * 1, and libusb_free_device_list() can optionally decrease the reference count
551 * on all devices in the list. libusb_open() adds another reference which is
552 * later destroyed by libusb_close().
554 typedef struct libusb_device libusb_device;
558 * Structure representing a handle on a USB device. This is an opaque type for
559 * which you are only ever provided with a pointer, usually originating from
562 * A device handle is used to perform I/O and other operations. When finished
563 * with a device handle, you should call libusb_close().
565 typedef struct libusb_device_handle libusb_device_handle;
568 * Error codes. Most libusb functions return 0 on success or one of these
572 /** Success (no error) */
575 /** Input/output error */
576 LIBUSB_ERROR_IO = -1,
578 /** Invalid parameter */
579 LIBUSB_ERROR_INVALID_PARAM = -2,
581 /** Access denied (insufficient permissions) */
582 LIBUSB_ERROR_ACCESS = -3,
584 /** No such device (it may have been disconnected) */
585 LIBUSB_ERROR_NO_DEVICE = -4,
587 /** Entity not found */
588 LIBUSB_ERROR_NOT_FOUND = -5,
591 LIBUSB_ERROR_BUSY = -6,
593 /** Operation timed out */
594 LIBUSB_ERROR_TIMEOUT = -7,
597 LIBUSB_ERROR_PIPE = -8,
599 /** System call interrupted (perhaps due to signal) */
600 LIBUSB_ERROR_INTERRUPTED = -9,
602 /** Insufficient memory */
603 LIBUSB_ERROR_NO_MEM = -10,
605 /** Operation not supported or unimplemented on this platform */
606 LIBUSB_ERROR_NOT_SUPPORTED = -11,
609 LIBUSB_ERROR_OTHER = -12,
613 * Transfer status codes */
614 enum libusb_transfer_status {
615 /** Transfer completed without error. Note that this does not indicate
616 * that the entire amount of requested data was transferred. */
617 LIBUSB_TRANSFER_COMPLETED,
619 /** Transfer failed */
620 LIBUSB_TRANSFER_ERROR,
622 /** Transfer timed out */
623 LIBUSB_TRANSFER_TIMED_OUT,
625 /** Transfer was cancelled */
626 LIBUSB_TRANSFER_CANCELLED,
628 /** For bulk/interrupt endpoints: halt condition detected (endpoint
629 * stalled). For control endpoints: control request not supported. */
630 LIBUSB_TRANSFER_STALL,
632 /** Device was disconnected */
633 LIBUSB_TRANSFER_NO_DEVICE,
637 * libusb_transfer.flags values */
638 enum libusb_transfer_flags {
639 /** Report short frames as errors */
640 LIBUSB_TRANSFER_SHORT_NOT_OK = 1<<0,
642 /** Automatically free() transfer buffer during libusb_free_transfer() */
643 LIBUSB_TRANSFER_FREE_BUFFER = 1<<1,
645 /** Automatically call libusb_free_transfer() after callback returns.
646 * If this flag is set, it is illegal to call libusb_free_transfer()
647 * from your transfer callback, as this will result in a double-free
648 * when this flag is acted upon. */
649 LIBUSB_TRANSFER_FREE_TRANSFER = 1<<2,
653 * Isochronous packet descriptor. */
654 struct libusb_iso_packet_descriptor {
655 /** Length of data to request in this packet */
658 /** Amount of data that was actually transferred */
659 unsigned int actual_length;
661 /** Status code for this packet */
662 enum libusb_transfer_status status;
665 struct libusb_transfer;
667 typedef void (*libusb_transfer_cb_fn)(struct libusb_transfer *transfer);
670 * The generic USB transfer structure.
672 struct libusb_transfer {
673 /** Handle of the device that this transfer will be submitted to */
674 libusb_device_handle *dev_handle;
676 /** A bitwise OR combination of \ref libusb_transfer_flags. */
679 /** Address of the endpoint where this transfer will be sent. */
680 unsigned char endpoint;
682 /** Type of the endpoint from \ref libusb_transfer_type */
685 /** Timeout for this transfer in millseconds. A value of 0 indicates no
687 unsigned int timeout;
689 /** The status of the transfer. Read-only, and only for use within
690 * transfer callback function.
692 * If this is an isochronous transfer, this field may read COMPLETED even
693 * if there were errors in the frames. Use the
694 * \ref libusb_iso_packet_descriptor::status "status" field in each packet
695 * to determine if errors occurred. */
696 enum libusb_transfer_status status;
698 /** Length of the data buffer */
701 /** Actual length of data that was transferred. Read-only, and only for
702 * use within transfer callback function. Not valid for isochronous
703 * endpoint transfers. */
706 /** Callback function. This will be invoked when the transfer completes,
707 * fails, or is cancelled. */
708 libusb_transfer_cb_fn callback;
710 /** User context data to pass to the callback function. */
714 unsigned char *buffer;
716 /** Number of isochronous packets. Only used for I/O with isochronous
720 /** Isochronous packet descriptors, for isochronous transfers only. */
721 struct libusb_iso_packet_descriptor iso_packet_desc[0];
724 int libusb_init(void);
725 void libusb_exit(void);
726 void libusb_set_debug(int level);
728 ssize_t libusb_get_device_list(libusb_device ***list);
729 void libusb_free_device_list(libusb_device **list, int unref_devices);
730 libusb_device *libusb_ref_device(libusb_device *dev);
731 void libusb_unref_device(libusb_device *dev);
733 int libusb_get_device_descriptor(libusb_device *dev,
734 struct libusb_device_descriptor *desc);
735 int libusb_get_active_config_descriptor(libusb_device *dev,
736 struct libusb_config_descriptor **config);
737 int libusb_get_config_descriptor(libusb_device *dev, uint8_t config_index,
738 struct libusb_config_descriptor **config);
739 int libusb_get_config_descriptor_by_value(libusb_device *dev,
740 uint8_t bConfigurationValue, struct libusb_config_descriptor **config);
741 void libusb_free_config_descriptor(struct libusb_config_descriptor *config);
742 uint8_t libusb_get_bus_number(libusb_device *dev);
743 uint8_t libusb_get_device_address(libusb_device *dev);
744 int libusb_get_max_packet_size(libusb_device *dev, unsigned char endpoint);
746 int libusb_open(libusb_device *dev, libusb_device_handle **handle);
747 void libusb_close(libusb_device_handle *dev_handle);
748 libusb_device *libusb_get_device(libusb_device_handle *dev_handle);
750 int libusb_set_configuration(libusb_device_handle *dev, int configuration);
751 int libusb_claim_interface(libusb_device_handle *dev, int iface);
752 int libusb_release_interface(libusb_device_handle *dev, int iface);
754 libusb_device_handle *libusb_open_device_with_vid_pid(uint16_t vendor_id,
755 uint16_t product_id);
757 int libusb_set_interface_alt_setting(libusb_device_handle *dev,
758 int interface_number, int alternate_setting);
759 int libusb_clear_halt(libusb_device_handle *dev, unsigned char endpoint);
760 int libusb_reset_device(libusb_device_handle *dev);
762 int libusb_kernel_driver_active(libusb_device_handle *dev, int interface);
763 int libusb_detach_kernel_driver(libusb_device_handle *dev, int interface);
768 * Get the data section of a control transfer. This convenience function is here
769 * to remind you that the data does not start until 8 bytes into the actual
770 * buffer, as the setup packet comes first.
772 * Calling this function only makes sense from a transfer callback function,
773 * or situations where you have already allocated a suitably sized buffer at
776 * \param transfer a transfer
777 * \returns pointer to the first byte of the data section
779 static inline unsigned char *libusb_control_transfer_get_data(
780 struct libusb_transfer *transfer)
782 return transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE;
786 * Get the control setup packet of a control transfer. This convenience
787 * function is here to remind you that the control setup occupies the first
788 * 8 bytes of the transfer data buffer.
790 * Calling this function only makes sense from a transfer callback function,
791 * or situations where you have already allocated a suitably sized buffer at
794 * \param transfer a transfer
795 * \returns a casted pointer to the start of the transfer data buffer
797 static inline struct libusb_control_setup *libusb_control_transfer_get_setup(
798 struct libusb_transfer *transfer)
800 return (struct libusb_control_setup *) transfer->buffer;
804 * Helper function to populate the setup packet (first 8 bytes of the data
805 * buffer) for a control transfer. The wIndex, wValue and wLength values should
806 * be given in host-endian byte order.
808 * \param buffer buffer to output the setup packet into
809 * \param bmRequestType see the
810 * \ref libusb_control_setup::bmRequestType "bmRequestType" field of
811 * \ref libusb_control_setup
812 * \param bRequest see the
813 * \ref libusb_control_setup::bRequest "bRequest" field of
814 * \ref libusb_control_setup
815 * \param wValue see the
816 * \ref libusb_control_setup::wValue "wValue" field of
817 * \ref libusb_control_setup
818 * \param wIndex see the
819 * \ref libusb_control_setup::wIndex "wIndex" field of
820 * \ref libusb_control_setup
821 * \param wLength see the
822 * \ref libusb_control_setup::wLength "wLength" field of
823 * \ref libusb_control_setup
825 static inline void libusb_fill_control_setup(unsigned char *buffer,
826 uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
829 struct libusb_control_setup *setup = (struct libusb_control_setup *) buffer;
830 setup->bmRequestType = bmRequestType;
831 setup->bRequest = bRequest;
832 setup->wValue = libusb_cpu_to_le16(wValue);
833 setup->wIndex = libusb_cpu_to_le16(wIndex);
834 setup->wLength = libusb_cpu_to_le16(wLength);
837 struct libusb_transfer *libusb_alloc_transfer(int iso_packets);
838 int libusb_submit_transfer(struct libusb_transfer *transfer);
839 int libusb_cancel_transfer(struct libusb_transfer *transfer);
840 void libusb_free_transfer(struct libusb_transfer *transfer);
843 * Helper function to populate the required \ref libusb_transfer fields
844 * for a control transfer.
846 * If you pass a transfer buffer to this function, the first 8 bytes will
847 * be interpreted as a control setup packet, and the wLength field will be
848 * used to automatically populate the \ref libusb_transfer::length "length"
849 * field of the transfer. Therefore the recommended approach is:
850 * -# Allocate a suitably sized data buffer (including space for control setup)
851 * -# Call libusb_fill_control_setup()
852 * -# If this is a host-to-device transfer with a data stage, put the data
853 * in place after the setup packet
854 * -# Call this function
855 * -# Call libusb_submit_transfer()
857 * It is also legal to pass a NULL buffer to this function, in which case this
858 * function will not attempt to populate the length field. Remember that you
859 * must then populate the buffer and length fields later.
861 * \param transfer the transfer to populate
862 * \param dev_handle handle of the device that will handle the transfer
863 * \param buffer data buffer. If provided, this function will interpret the
864 * first 8 bytes as a setup packet and infer the transfer length from that.
865 * \param callback callback function to be invoked on transfer completion
866 * \param user_data user data to pass to callback function
867 * \param timeout timeout for the transfer in milliseconds
869 static inline void libusb_fill_control_transfer(
870 struct libusb_transfer *transfer, libusb_device_handle *dev_handle,
871 unsigned char *buffer, libusb_transfer_cb_fn callback, void *user_data,
872 unsigned int timeout)
874 struct libusb_control_setup *setup = (struct libusb_control_setup *) buffer;
875 transfer->dev_handle = dev_handle;
876 transfer->endpoint = 0;
877 transfer->type = LIBUSB_TRANSFER_TYPE_CONTROL;
878 transfer->timeout = timeout;
879 transfer->buffer = buffer;
881 transfer->length = LIBUSB_CONTROL_SETUP_SIZE
882 + libusb_le16_to_cpu(setup->wLength);
883 transfer->user_data = user_data;
884 transfer->callback = callback;
888 * Helper function to populate the required \ref libusb_transfer fields
889 * for a bulk transfer.
891 * \param transfer the transfer to populate
892 * \param dev_handle handle of the device that will handle the transfer
893 * \param endpoint address of the endpoint where this transfer will be sent
894 * \param buffer data buffer
895 * \param length length of data buffer
896 * \param callback callback function to be invoked on transfer completion
897 * \param user_data user data to pass to callback function
898 * \param timeout timeout for the transfer in milliseconds
900 static inline void libusb_fill_bulk_transfer(struct libusb_transfer *transfer,
901 libusb_device_handle *dev_handle, unsigned char endpoint,
902 unsigned char *buffer, int length, libusb_transfer_cb_fn callback,
903 void *user_data, unsigned int timeout)
905 transfer->dev_handle = dev_handle;
906 transfer->endpoint = endpoint;
907 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
908 transfer->timeout = timeout;
909 transfer->buffer = buffer;
910 transfer->length = length;
911 transfer->user_data = user_data;
912 transfer->callback = callback;
916 * Helper function to populate the required \ref libusb_transfer fields
917 * for an interrupt transfer.
919 * \param transfer the transfer to populate
920 * \param dev_handle handle of the device that will handle the transfer
921 * \param endpoint address of the endpoint where this transfer will be sent
922 * \param buffer data buffer
923 * \param length length of data buffer
924 * \param callback callback function to be invoked on transfer completion
925 * \param user_data user data to pass to callback function
926 * \param timeout timeout for the transfer in milliseconds
928 static inline void libusb_fill_interrupt_transfer(
929 struct libusb_transfer *transfer, libusb_device_handle *dev_handle,
930 unsigned char endpoint, unsigned char *buffer, int length,
931 libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
933 transfer->dev_handle = dev_handle;
934 transfer->endpoint = endpoint;
935 transfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT;
936 transfer->timeout = timeout;
937 transfer->buffer = buffer;
938 transfer->length = length;
939 transfer->user_data = user_data;
940 transfer->callback = callback;
944 * Helper function to populate the required \ref libusb_transfer fields
945 * for an isochronous transfer.
947 * \param transfer the transfer to populate
948 * \param dev_handle handle of the device that will handle the transfer
949 * \param endpoint address of the endpoint where this transfer will be sent
950 * \param buffer data buffer
951 * \param length length of data buffer
952 * \param num_iso_packets the number of isochronous packets
953 * \param callback callback function to be invoked on transfer completion
954 * \param user_data user data to pass to callback function
955 * \param timeout timeout for the transfer in milliseconds
957 static inline void libusb_fill_iso_transfer(struct libusb_transfer *transfer,
958 libusb_device_handle *dev_handle, unsigned char endpoint,
959 unsigned char *buffer, int length, int num_iso_packets,
960 libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
962 transfer->dev_handle = dev_handle;
963 transfer->endpoint = endpoint;
964 transfer->type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS;
965 transfer->timeout = timeout;
966 transfer->buffer = buffer;
967 transfer->length = length;
968 transfer->num_iso_packets = num_iso_packets;
969 transfer->user_data = user_data;
970 transfer->callback = callback;
974 * Convenience function to set the length of all packets in an isochronous
975 * transfer, based on the num_iso_packets field in the transfer structure.
977 * \param transfer a transfer
978 * \param length the length to set in each isochronous packet descriptor
979 * \see libusb_get_max_packet_size()
981 static inline void libusb_set_iso_packet_lengths(
982 struct libusb_transfer *transfer, unsigned int length)
985 for (i = 0; i < transfer->num_iso_packets; i++)
986 transfer->iso_packet_desc[i].length = length;
990 * Convenience function to locate the position of an isochronous packet
991 * within the buffer of an isochronous transfer.
993 * This is a thorough function which loops through all preceding packets,
994 * accumulating their lengths to find the position of the specified packet.
995 * Typically you will assign equal lengths to each packet in the transfer,
996 * and hence the above method is sub-optimal. You may wish to use
997 * libusb_get_iso_packet_buffer_simple() instead.
999 * \param transfer a transfer
1000 * \param packet the packet to return the address of
1001 * \returns the base address of the packet buffer inside the transfer buffer,
1002 * or NULL if the packet does not exist.
1003 * \see libusb_get_iso_packet_buffer_simple()
1005 static inline unsigned char *libusb_get_iso_packet_buffer(
1006 struct libusb_transfer *transfer, unsigned int packet)
1011 if (packet >= transfer->num_iso_packets)
1014 for (i = 0; i < packet; i++)
1015 offset += transfer->iso_packet_desc[i].length;
1017 return transfer->buffer + offset;
1020 /** \ingroup asyncio
1021 * Convenience function to locate the position of an isochronous packet
1022 * within the buffer of an isochronous transfer, for transfers where each
1023 * packet is of identical size.
1025 * This function relies on the assumption that every packet within the transfer
1026 * is of identical size to the first packet. Calculating the location of
1027 * the packet buffer is then just a simple calculation:
1028 * <tt>buffer + (packet_size * packet)</tt>
1030 * Do not use this function on transfers other than those that have identical
1031 * packet lengths for each packet.
1033 * \param transfer a transfer
1034 * \param packet the packet to return the address of
1035 * \returns the base address of the packet buffer inside the transfer buffer,
1036 * or NULL if the packet does not exist.
1037 * \see libusb_get_iso_packet_buffer()
1039 static inline unsigned char *libusb_get_iso_packet_buffer_simple(
1040 struct libusb_transfer *transfer, unsigned int packet)
1042 if (packet >= transfer->num_iso_packets)
1045 return transfer->buffer + (transfer->iso_packet_desc[0].length * packet);
1050 int libusb_control_transfer(libusb_device_handle *dev_handle,
1051 uint8_t request_type, uint8_t request, uint16_t value, uint16_t index,
1052 unsigned char *data, uint16_t length, unsigned int timeout);
1054 int libusb_bulk_transfer(libusb_device_handle *dev_handle,
1055 unsigned char endpoint, unsigned char *data, int length,
1056 int *actual_length, unsigned int timeout);
1058 int libusb_interrupt_transfer(libusb_device_handle *dev_handle,
1059 unsigned char endpoint, unsigned char *data, int length,
1060 int *actual_length, unsigned int timeout);
1063 * Retrieve a descriptor from the default control pipe.
1064 * This is a convenience function which formulates the appropriate control
1065 * message to retrieve the descriptor.
1067 * \param dev a device handle
1068 * \param desc_type the descriptor type, see \ref libusb_descriptor_type
1069 * \param desc_index the index of the descriptor to retrieve
1070 * \param data output buffer for descriptor
1071 * \param length size of data buffer
1072 * \returns number of bytes returned in data, or LIBUSB_ERROR code on failure
1074 static inline int libusb_get_descriptor(libusb_device_handle *dev,
1075 uint8_t desc_type, uint8_t desc_index, unsigned char *data, int length)
1077 return libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN,
1078 LIBUSB_REQUEST_GET_DESCRIPTOR, (desc_type << 8) | desc_index, 0, data,
1083 * Retrieve a descriptor from a device.
1084 * This is a convenience function which formulates the appropriate control
1085 * message to retrieve the descriptor. The string returned is Unicode, as
1086 * detailed in the USB specifications.
1088 * \param dev a device handle
1089 * \param desc_index the index of the descriptor to retrieve
1090 * \param langid the language ID for the string descriptor
1091 * \param data output buffer for descriptor
1092 * \param length size of data buffer
1093 * \returns number of bytes returned in data, or LIBUSB_ERROR code on failure
1094 * \see libusb_get_string_descriptor_ascii()
1096 static inline int libusb_get_string_descriptor(libusb_device_handle *dev,
1097 uint8_t desc_index, uint16_t langid, unsigned char *data, int length)
1099 return libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN,
1100 LIBUSB_REQUEST_GET_DESCRIPTOR, (LIBUSB_DT_STRING << 8) | desc_index,
1101 langid, data, length, 1000);
1104 int libusb_get_string_descriptor_ascii(libusb_device_handle *dev,
1105 uint8_t index, unsigned char *data, int length);
1107 /* polling and timeouts */
1109 int libusb_try_lock_events(void);
1110 void libusb_lock_events(void);
1111 void libusb_unlock_events(void);
1112 int libusb_event_handler_active(void);
1113 void libusb_lock_event_waiters(void);
1114 void libusb_unlock_event_waiters(void);
1115 int libusb_wait_for_event(struct timeval *tv);
1117 int libusb_handle_events_timeout(struct timeval *tv);
1118 int libusb_handle_events(void);
1119 int libusb_handle_events_locked(struct timeval *tv);
1120 int libusb_get_next_timeout(struct timeval *tv);
1123 * File descriptor for polling
1125 struct libusb_pollfd {
1126 /** Numeric file descriptor */
1129 /** Event flags to poll for from <poll.h>. POLLIN indicates that you
1130 * should monitor this file descriptor for becoming ready to read from,
1131 * and POLLOUT indicates that you should monitor this file descriptor for
1132 * nonblocking write readiness. */
1137 * Callback function, invoked when a new file descriptor should be added
1138 * to the set of file descriptors monitored for events.
1139 * \param fd the new file descriptor
1140 * \param events events to monitor for, see \ref libusb_pollfd for a
1142 * \see libusb_set_pollfd_notifiers()
1144 typedef void (*libusb_pollfd_added_cb)(int fd, short events);
1147 * Callback function, invoked when a file descriptor should be removed from
1148 * the set of file descriptors being monitored for events. After returning
1149 * from this callback, do not use that file descriptor again.
1150 * \param fd the file descriptor to stop monitoring
1151 * \see libusb_set_pollfd_notifiers()
1153 typedef void (*libusb_pollfd_removed_cb)(int fd);
1155 const struct libusb_pollfd **libusb_get_pollfds(void);
1156 void libusb_set_pollfd_notifiers(libusb_pollfd_added_cb added_cb,
1157 libusb_pollfd_removed_cb removed_cb);