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
32 /* standard USB stuff */
35 * Device and/or Interface Class codes */
36 enum libusb_class_code {
37 /** In the context of a \ref libusb_device_descriptor "device descriptor",
38 * this bDeviceClass value indicates that each interface specifies its
39 * own class information and all interfaces operate independently.
41 LIBUSB_CLASS_PER_INTERFACE = 0,
44 LIBUSB_CLASS_AUDIO = 1,
46 /** Communications class */
47 LIBUSB_CLASS_COMM = 2,
49 /** Human Interface Device class */
53 LIBUSB_CLASS_PRINTER = 7,
55 /** Picture transfer protocol class */
58 /** Mass storage class */
59 LIBUSB_CLASS_MASS_STORAGE = 8,
65 LIBUSB_CLASS_DATA = 10,
67 /** Class is vendor-specific */
68 LIBUSB_CLASS_VENDOR_SPEC = 0xff,
72 * Descriptor types as defined by the USB specification. */
73 enum libusb_descriptor_type {
74 /** Device descriptor. See libusb_device_descriptor. */
75 LIBUSB_DT_DEVICE = 0x01,
77 /** Configuration descriptor. See libusb_config_descriptor. */
78 LIBUSB_DT_CONFIG = 0x02,
80 /** String descriptor */
81 LIBUSB_DT_STRING = 0x03,
83 /** Interface descriptor. See libusb_interface_descriptor. */
84 LIBUSB_DT_INTERFACE = 0x04,
86 /** Endpoint descriptor. See libusb_endpoint_descriptor. */
87 LIBUSB_DT_ENDPOINT = 0x05,
92 /** HID report descriptor */
93 LIBUSB_DT_REPORT = 0x22,
95 /** Physical descriptor */
96 LIBUSB_DT_PHYSICAL = 0x23,
102 /* Descriptor sizes per descriptor type */
103 #define LIBUSB_DT_DEVICE_SIZE 18
104 #define LIBUSB_DT_CONFIG_SIZE 9
105 #define LIBUSB_DT_INTERFACE_SIZE 9
106 #define LIBUSB_DT_ENDPOINT_SIZE 7
107 #define LIBUSB_DT_ENDPOINT_AUDIO_SIZE 9 /* Audio extension */
108 #define LIBUSB_DT_HUB_NONVAR_SIZE 7
110 #define LIBUSB_ENDPOINT_ADDRESS_MASK 0x0f /* in bEndpointAddress */
111 #define LIBUSB_ENDPOINT_DIR_MASK 0x80
114 * Endpoint direction. Values for bit 7 of the
115 * \ref libusb_endpoint_descriptor::bEndpointAddress "endpoint address" scheme.
117 enum libusb_endpoint_direction {
118 /** In: device-to-host */
119 LIBUSB_ENDPOINT_IN = 0x80,
121 /** Out: host-to-device */
122 LIBUSB_ENDPOINT_OUT = 0x00,
125 #define LIBUSB_TRANSFER_TYPE_MASK 0x03 /* in bmAttributes */
128 * Endpoint transfer type. Values for bits 0:1 of the
129 * \ref libusb_endpoint_descriptor::bmAttributes "endpoint attributes" field.
131 enum libusb_transfer_type {
132 /** Control endpoint */
133 LIBUSB_TRANSFER_TYPE_CONTROL = 0,
135 /** Isochronous endpoint */
136 LIBUSB_TRANSFER_TYPE_ISOCHRONOUS = 1,
139 LIBUSB_TRANSFER_TYPE_BULK = 2,
141 /** Interrupt endpoint */
142 LIBUSB_TRANSFER_TYPE_INTERRUPT = 3,
145 /** Standard requests, as defined in table 9-3 of the USB2 specifications */
146 enum libusb_standard_request {
147 /** Request status of the specific recipient */
148 LIBUSB_REQUEST_GET_STATUS = 0x00,
150 /** Clear or disable a specific feature */
151 LIBUSB_REQUEST_CLEAR_FEATURE = 0x01,
153 /* 0x02 is reserved */
155 /** Set or enable a specific feature */
156 LIBUSB_REQUEST_SET_FEATURE = 0x03,
158 /* 0x04 is reserved */
160 /** Set device address for all future accesses */
161 LIBUSB_REQUEST_SET_ADDRESS = 0x05,
163 /** Get the specified descriptor */
164 LIBUSB_REQUEST_GET_DESCRIPTOR = 0x06,
166 /** Used to update existing descriptors or add new descriptors */
167 LIBUSB_REQUEST_SET_DESCRIPTOR = 0x07,
169 /** Get the current device configuration value */
170 LIBUSB_REQUEST_GET_CONFIGURATION = 0x08,
172 /** Set device configuration */
173 LIBUSB_REQUEST_SET_CONFIGURATION = 0x09,
175 /** Return the selected alternate setting for the specified interface */
176 LIBUSB_REQUEST_GET_INTERFACE = 0x0A,
178 /** Select an alternate interface for the specified interface */
179 LIBUSB_REQUEST_SET_INTERFACE = 0x0B,
181 /** Set then report an endpoint's synchronization frame */
182 LIBUSB_REQUEST_SYNCH_FRAME = 0x0C,
186 * Request type bits of the
187 * \ref libusb_control_setup::bmRequestType "bmRequestType" field in control
189 enum libusb_request_type {
191 LIBUSB_REQUEST_TYPE_STANDARD = (0x00 << 5),
194 LIBUSB_REQUEST_TYPE_CLASS = (0x01 << 5),
197 LIBUSB_REQUEST_TYPE_VENDOR = (0x02 << 5),
200 LIBUSB_REQUEST_TYPE_RESERVED = (0x03 << 5),
204 * Recipient bits of the
205 * \ref libusb_control_setup::bmRequestType "bmRequestType" field in control
206 * transfers. Values 4 through 31 are reserved. */
207 enum libusb_request_recipient {
209 LIBUSB_RECIPIENT_DEVICE = 0x00,
212 LIBUSB_RECIPIENT_INTERFACE = 0x01,
215 LIBUSB_RECIPIENT_ENDPOINT = 0x02,
218 LIBUSB_RECIPIENT_OTHER = 0x03,
221 #define LIBUSB_ISO_SYNC_TYPE_MASK 0x0C
224 * Synchronization type for isochronous endpoints. Values for bits 2:3 of the
225 * \ref libusb_endpoint_descriptor::bmAttributes "bmAttributes" field in
226 * libusb_endpoint_descriptor.
228 enum libusb_iso_sync_type {
229 /** No synchronization */
230 LIBUSB_ISO_SYNC_TYPE_NONE = 0,
233 LIBUSB_ISO_SYNC_TYPE_ASYNC = 1,
236 LIBUSB_ISO_SYNC_TYPE_ADAPTIVE = 2,
239 LIBUSB_ISO_SYNC_TYPE_SYNC = 3,
242 #define LIBUSB_ISO_USAGE_TYPE_MASK 0x30
245 * Usage type for isochronous endpoints. Values for bits 4:5 of the
246 * \ref libusb_endpoint_descriptor::bmAttributes "bmAttributes" field in
247 * libusb_endpoint_descriptor.
249 enum libusb_iso_usage_type {
251 LIBUSB_ISO_USAGE_TYPE_DATA = 0,
253 /** Feedback endpoint */
254 LIBUSB_ISO_USAGE_TYPE_FEEDBACK = 1,
256 /** Implicit feedback Data endpoint */
257 LIBUSB_ISO_USAGE_TYPE_IMPLICIT = 2,
261 * A structure representing the standard USB device descriptor. This
262 * descriptor is documented in section 9.6.1 of the USB 2.0 specification.
263 * All multiple-byte fields are represented in host-endian format.
265 struct libusb_device_descriptor {
266 /** Size of this descriptor (in bytes) */
269 /** Descriptor type. Will have value
270 * \ref libusb_descriptor_type::LIBUSB_DT_DEVICE LIBUSB_DT_DEVICE in this
272 uint8_t bDescriptorType;
274 /** USB specification release number in binary-coded decimal. A value of
275 * 0x0200 indicates USB 2.0, 0x0110 indicates USB 1.1, etc. */
278 /** USB-IF class code for the device. See \ref libusb_class_code. */
279 uint8_t bDeviceClass;
281 /** USB-IF subclass code for the device, qualified by the bDeviceClass
283 uint8_t bDeviceSubClass;
285 /** USB-IF protocol code for the device, qualified by the bDeviceClass and
286 * bDeviceSubClass values */
287 uint8_t bDeviceProtocol;
289 /** Maximum packet size for endpoint 0 */
290 uint8_t bMaxPacketSize0;
292 /** USB-IF vendor ID */
295 /** USB-IF product ID */
298 /** Device release number in binary-coded decimal */
301 /** Index of string descriptor describing manufacturer */
302 uint8_t iManufacturer;
304 /** Index of string descriptor describing product */
307 /** Index of string descriptor containing device serial number */
308 uint8_t iSerialNumber;
310 /** Number of possible configurations */
311 uint8_t bNumConfigurations;
315 * A structure representing the standard USB endpoint descriptor. This
316 * descriptor is documented in section 9.6.3 of the USB 2.0 specification.
317 * All multiple-byte fields are represented in host-endian format.
319 struct libusb_endpoint_descriptor {
320 /** Size of this descriptor (in bytes) */
323 /** Descriptor type. Will have value
324 * \ref libusb_descriptor_type::LIBUSB_DT_ENDPOINT LIBUSB_DT_ENDPOINT in
326 uint8_t bDescriptorType;
328 /** The address of the endpoint described by this descriptor. Bits 0:3 are
329 * the endpoint number. Bits 4:6 are reserved. Bit 7 indicates direction,
330 * see \ref libusb_endpoint_direction.
332 uint8_t bEndpointAddress;
334 /** Attributes which apply to the endpoint when it is configured using
335 * the bConfigurationValue. Bits 0:1 determine the transfer type and
336 * correspond to \ref libusb_transfer_type. Bits 2:3 are only used for
337 * isochronous endpoints and correspond to \ref libusb_iso_sync_type.
338 * Bits 4:5 are also only used for isochronous endpoints and correspond to
339 * \ref libusb_iso_usage_type. Bits 6:7 are reserved.
341 uint8_t bmAttributes;
343 /** Maximum packet size this endpoint is capable of sending/receiving. */
344 uint16_t wMaxPacketSize;
346 /** Interval for polling endpoint for data transfers. */
350 uint8_t bSynchAddress;
352 const unsigned char *extra; /* Extra descriptors */
357 * A structure representing the standard USB interface descriptor. This
358 * descriptor is documented in section 9.6.5 of the USB 2.0 specification.
359 * All multiple-byte fields are represented in host-endian format.
361 struct libusb_interface_descriptor {
362 /** Size of this descriptor (in bytes) */
365 /** Descriptor type. Will have value
366 * \ref libusb_descriptor_type::LIBUSB_DT_INTERFACE LIBUSB_DT_INTERFACE
367 * in this context. */
368 uint8_t bDescriptorType;
370 /** Number of this interface */
371 uint8_t bInterfaceNumber;
373 /** Value used to select this alternate setting for this interface */
374 uint8_t bAlternateSetting;
376 /** Number of endpoints used by this interface (excluding the control
378 uint8_t bNumEndpoints;
380 /** USB-IF class code for this interface. See \ref libusb_class_code. */
381 uint8_t bInterfaceClass;
383 /** USB-IF subclass code for this interface, qualified by the
384 * bInterfaceClass value */
385 uint8_t bInterfaceSubClass;
387 /** USB-IF protocol code for this interface, qualified by the
388 * bInterfaceClass and bInterfaceSubClass values */
389 uint8_t bInterfaceProtocol;
391 /** Index of string descriptor describing this interface */
394 /** Array of endpoint descriptors. This length of this array is determined
395 * by the bNumEndpoints field. */
396 const struct libusb_endpoint_descriptor *endpoint;
398 const unsigned char *extra; /* Extra descriptors */
403 * A collection of alternate settings for a particular USB interface.
405 struct libusb_interface {
406 /** Array of interface descriptors. The length of this array is determined
407 * by the num_altsetting field. */
408 const struct libusb_interface_descriptor *altsetting;
410 /** The number of alternate settings that belong to this interface */
415 * A structure representing the standard USB configuration descriptor. This
416 * descriptor is documented in section 9.6.3 of the USB 2.0 specification.
417 * All multiple-byte fields are represented in host-endian format.
419 struct libusb_config_descriptor {
420 /** Size of this descriptor (in bytes) */
423 /** Descriptor type. Will have value
424 * \ref libusb_descriptor_type::LIBUSB_DT_CONFIG LIBUSB_DT_CONFIG
425 * in this context. */
426 uint8_t bDescriptorType;
428 /** Total length of data returned for this configuration */
429 uint16_t wTotalLength;
431 /** Number of interfaces supported by this configuration */
432 uint8_t bNumInterfaces;
434 /** Identifier value for this configuration */
435 uint8_t bConfigurationValue;
437 /** Index of string descriptor describing this configuration */
438 uint8_t iConfiguration;
440 /** Configuration characteristics */
441 uint8_t bmAttributes;
443 /** Maximum power consumption of the USB device from this bus in this
444 * configuration when the device is fully opreation. Expressed in units
448 /** Array of interfaces supported by this configuration. The length of
449 * this array is determined by the bNumInterfaces field. */
450 const struct libusb_interface *interface;
452 const unsigned char *extra; /* Extra descriptors */
457 * Setup packet for control transfers. */
458 struct libusb_control_setup {
459 /** Request type. Bits 0:4 determine recipient, see
460 * \ref libusb_request_recipient. Bits 5:6 determine type, see
461 * \ref libusb_request_type. Bit 7 determines data transfer direction, see
462 * \ref libusb_endpoint_direction.
464 uint8_t bmRequestType;
466 /** Request. If the type bits of bmRequestType are equal to
467 * \ref libusb_request_type::LIBUSB_REQUEST_TYPE_STANDARD
468 * "LIBUSB_REQUEST_TYPE_STANDARD" then this field refers to
469 * \ref libusb_standard_request. For other cases, use of this field is
470 * application-specific. */
473 /** Value. Varies according to request */
476 /** Index. Varies according to request, typically used to pass an index
480 /** Number of bytes to transfer */
482 } __attribute__((packed));
484 #define LIBUSB_CONTROL_SETUP_SIZE (sizeof(struct libusb_control_setup))
488 struct libusb_device;
489 struct libusb_device_handle;
492 * Structure representing a USB device detected on the system. This is an
493 * opaque type for which you are only ever provided with a pointer, usually
494 * originating from libusb_get_device_list().
496 * Certain operations can be performed on a device, but in order to do any
497 * I/O you will have to first obtain a device handle using libusb_open().
499 * Devices are reference counted with libusb_device_ref() and
500 * libusb_device_unref(), and are freed when the reference count reaches 0.
501 * New devices presented by libusb_get_device_list() have a reference count of
502 * 1, and libusb_free_device_list() can optionally decrease the reference count
503 * on all devices in the list. libusb_open() adds another reference which is
504 * later destroyed by libusb_close().
506 typedef struct libusb_device libusb_device;
510 * Structure representing a handle on a USB device. This is an opaque type for
511 * which you are only ever provided with a pointer, usually originating from
514 * A device handle is used to perform I/O and other operations. When finished
515 * with a device handle, you should call libusb_close().
517 typedef struct libusb_device_handle libusb_device_handle;
521 LIBUSB_ERROR_IO = -1,
522 LIBUSB_ERROR_INVALID_PARAM = -2,
523 LIBUSB_ERROR_ACCESS = -3,
524 LIBUSB_ERROR_NOT_FOUND = -4,
525 LIBUSB_ERROR_BUSY = -5,
526 LIBUSB_ERROR_NO_MEM = -6,
527 LIBUSB_ERROR_OTHER = -7,
531 * Transfer status codes */
532 enum libusb_transfer_status {
533 /** Transfer completed without error. Note that this does not indicate
534 * that the entire amount of requested data was transferred. */
535 LIBUSB_TRANSFER_COMPLETED,
537 /** Transfer failed */
538 LIBUSB_TRANSFER_ERROR,
540 /** Transfer timed out */
541 LIBUSB_TRANSFER_TIMED_OUT,
543 /** Transfer was cancelled */
544 LIBUSB_TRANSFER_CANCELLED,
548 * libusb_transfer.flags values */
549 enum libusb_transfer_flags {
550 /** Report short frames as errors */
551 LIBUSB_TRANSFER_SHORT_NOT_OK = 1<<0,
553 /** Automatically free() transfer buffer during libusb_free_transfer() */
554 LIBUSB_TRANSFER_FREE_BUFFER = 1<<1,
556 /** Automatically call libusb_free_transfer() after callback returns.
557 * If this flag is set, it is illegal to call libusb_free_transfer()
558 * from your transfer callback, as this will result in a double-free
559 * when this flag is acted upon. */
560 LIBUSB_TRANSFER_FREE_TRANSFER = 1<<2,
564 * Isochronous packet descriptor. */
565 struct libusb_iso_packet_descriptor {
566 /** Length of data to request in this packet */
569 /** Amount of data that was actually transferred */
570 unsigned int actual_length;
572 /** Status code for this packet */
573 enum libusb_transfer_status status;
576 struct libusb_transfer;
578 typedef void (*libusb_transfer_cb_fn)(struct libusb_transfer *transfer);
581 * The generic USB transfer structure.
583 struct libusb_transfer {
584 /** Handle of the device that this transfer will be submitted to */
585 libusb_device_handle *dev_handle;
587 /** A bitwise OR combination of \ref libusb_transfer_flags. */
590 /** Address of the endpoint where this transfer will be sent. */
591 unsigned char endpoint;
593 /** Type of the endpoint from \ref libusb_transfer_type */
596 /** Timeout for this transfer in millseconds. A value of 0 indicates no
598 unsigned int timeout;
600 /** The status of the transfer. Read-only, and only for use within
601 * transfer callback function.
603 * If this is an isochronous transfer, this field may read COMPLETED even
604 * if there were errors in the frames. Use the
605 * \ref libusb_iso_packet_descriptor::status "status" field in each packet
606 * to determine if errors occurred. */
607 enum libusb_transfer_status status;
609 /** Length of the data buffer */
612 /** Actual length of data that was transferred. Read-only, and only for
613 * use within transfer callback function. Not valid for isochronous
614 * endpoint transfers. */
617 /** Callback function. This will be invoked when the transfer completes,
618 * fails, or is cancelled. */
619 libusb_transfer_cb_fn callback;
621 /** User context data to pass to the callback function. */
625 unsigned char *buffer;
627 /** Number of isochronous packets. Only used for I/O with isochronous
631 /** Isochronous packet descriptors, for isochronous transfers only. */
632 struct libusb_iso_packet_descriptor iso_packet_desc[0];
635 int libusb_init(void);
636 void libusb_exit(void);
638 size_t libusb_get_device_list(libusb_device ***list);
639 void libusb_free_device_list(libusb_device **list, int unref_devices);
640 const struct libusb_device_descriptor *libusb_get_device_descriptor(
642 const struct libusb_config_descriptor *libusb_get_config_descriptor(
644 libusb_device *libusb_ref_device(libusb_device *dev);
645 void libusb_unref_device(libusb_device *dev);
646 uint8_t libusb_get_bus_number(libusb_device *dev);
647 uint8_t libusb_get_device_address(libusb_device *dev);
649 libusb_device_handle *libusb_open(libusb_device *dev);
650 void libusb_close(libusb_device_handle *dev_handle);
651 libusb_device *libusb_get_device(libusb_device_handle *dev_handle);
653 int libusb_set_configuration(libusb_device_handle *dev, int configuration);
654 int libusb_claim_interface(libusb_device_handle *dev, int iface);
655 int libusb_release_interface(libusb_device_handle *dev, int iface);
657 libusb_device_handle *libusb_open_device_with_vid_pid(uint16_t vendor_id,
658 uint16_t product_id);
660 int libusb_set_interface_alt_setting(libusb_device_handle *dev,
661 int interface_number, int alternate_setting);
662 int libusb_clear_halt(libusb_device_handle *dev, unsigned char endpoint);
667 * Get the data section of a control transfer. This convenience function is here
668 * to remind you that the data does not start until 8 bytes into the actual
669 * buffer, as the setup packet comes first.
671 * Calling this function only makes sense from a transfer callback function,
672 * or situations where you have already allocated a suitably sized buffer at
675 * \param transfer a transfer
676 * \returns pointer to the first byte of the data section
678 static inline unsigned char *libusb_control_transfer_get_data(
679 struct libusb_transfer *transfer)
681 return transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE;
685 * Get the control setup packet of a control transfer. This convenience
686 * function is here to remind you that the control setup occupies the first
687 * 8 bytes of the transfer data buffer.
689 * Calling this function only makes sense from a transfer callback function,
690 * or situations where you have already allocated a suitably sized buffer at
693 * \param transfer a transfer
694 * \returns a casted pointer to the start of the transfer data buffer
696 static inline struct libusb_control_setup *libusb_control_transfer_get_setup(
697 struct libusb_transfer *transfer)
699 return (struct libusb_control_setup *) transfer->buffer;
703 * Helper function to populate the setup packet (first 8 bytes of the data
704 * buffer) for a control transfer.
706 * \param buffer buffer to output the setup packet into
707 * \param bmRequestType see the
708 * \ref libusb_control_setup::bmRequestType "bmRequestType" field of
709 * \ref libusb_control_setup
710 * \param bRequest see the
711 * \ref libusb_control_setup::bRequest "bRequest" field of
712 * \ref libusb_control_setup
713 * \param wValue see the
714 * \ref libusb_control_setup::wValue "wValue" field of
715 * \ref libusb_control_setup
716 * \param wIndex see the
717 * \ref libusb_control_setup::wIndex "wIndex" field of
718 * \ref libusb_control_setup
719 * \param wLength see the
720 * \ref libusb_control_setup::wLength "wLength" field of
721 * \ref libusb_control_setup
723 static inline void libusb_fill_control_setup(unsigned char *buffer,
724 uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
727 struct libusb_control_setup *setup = (struct libusb_control_setup *) buffer;
728 setup->bmRequestType = bmRequestType;
729 setup->bRequest = bRequest;
730 setup->wValue = wValue;
731 setup->wIndex = wIndex;
732 setup->wLength = wLength;
735 struct libusb_transfer *libusb_alloc_transfer(int iso_packets);
736 int libusb_submit_transfer(struct libusb_transfer *transfer);
737 int libusb_cancel_transfer(struct libusb_transfer *transfer);
738 void libusb_free_transfer(struct libusb_transfer *transfer);
741 * Helper function to populate the required \ref libusb_transfer fields
742 * for a control transfer.
744 * If you pass a transfer buffer to this function, the first 8 bytes will
745 * be interpreted as a control setup packet, and the wLength field will be
746 * used to automatically populate the \ref libusb_transfer::length "length"
747 * field of the transfer. Therefore the recommended approach is:
748 * -# Allocate a suitably sized data buffer (including space for control setup)
749 * -# Call libusb_fill_control_setup()
750 * -# If this is a host-to-device transfer with a data stage, put the data
751 * in place after the setup packet
752 * -# Call this function
753 * -# Call libusb_submit_transfer()
755 * It is also legal to pass a NULL buffer to this function, in which case this
756 * function will not attempt to populate the length field. Remember that you
757 * must populate the buffer and length fields later.
759 * \param transfer the transfer to populate
760 * \param dev_handle handle of the device that will handle the transfer
761 * \param buffer data buffer. If provided, this function will interpret the
762 * first 8 bytes as a setup packet and infer the transfer length from that.
763 * \param callback callback function to be invoked on transfer completion
764 * \param user_data user data to pass to callback function
765 * \param timeout timeout for the transfer in milliseconds
767 static inline void libusb_fill_control_transfer(
768 struct libusb_transfer *transfer, libusb_device_handle *dev_handle,
769 unsigned char *buffer, libusb_transfer_cb_fn callback, void *user_data,
770 unsigned int timeout)
772 struct libusb_control_setup *setup = (struct libusb_control_setup *) buffer;
773 transfer->dev_handle = dev_handle;
774 transfer->endpoint = 0;
775 transfer->type = LIBUSB_TRANSFER_TYPE_CONTROL;
776 transfer->timeout = timeout;
777 transfer->buffer = buffer;
779 transfer->length = LIBUSB_CONTROL_SETUP_SIZE + setup->wLength;
780 transfer->user_data = user_data;
781 transfer->callback = callback;
785 * Helper function to populate the required \ref libusb_transfer fields
786 * for a bulk transfer.
788 * \param transfer the transfer to populate
789 * \param dev_handle handle of the device that will handle the transfer
790 * \param endpoint address of the endpoint where this transfer will be sent
791 * \param buffer data buffer
792 * \param length length of data buffer
793 * \param callback callback function to be invoked on transfer completion
794 * \param user_data user data to pass to callback function
795 * \param timeout timeout for the transfer in milliseconds
797 static inline void libusb_fill_bulk_transfer(struct libusb_transfer *transfer,
798 libusb_device_handle *dev_handle, unsigned char endpoint,
799 unsigned char *buffer, int length, libusb_transfer_cb_fn callback,
800 void *user_data, unsigned int timeout)
802 transfer->dev_handle = dev_handle;
803 transfer->endpoint = endpoint;
804 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
805 transfer->timeout = timeout;
806 transfer->buffer = buffer;
807 transfer->length = length;
808 transfer->user_data = user_data;
809 transfer->callback = callback;
813 * Helper function to populate the required \ref libusb_transfer fields
814 * for an interrupt transfer.
816 * \param transfer the transfer to populate
817 * \param dev_handle handle of the device that will handle the transfer
818 * \param endpoint address of the endpoint where this transfer will be sent
819 * \param buffer data buffer
820 * \param length length of data buffer
821 * \param callback callback function to be invoked on transfer completion
822 * \param user_data user data to pass to callback function
823 * \param timeout timeout for the transfer in milliseconds
825 static inline void libusb_fill_interrupt_transfer(
826 struct libusb_transfer *transfer, libusb_device_handle *dev_handle,
827 unsigned char endpoint, unsigned char *buffer, int length,
828 libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
830 transfer->dev_handle = dev_handle;
831 transfer->endpoint = endpoint;
832 transfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT;
833 transfer->timeout = timeout;
834 transfer->buffer = buffer;
835 transfer->length = length;
836 transfer->user_data = user_data;
837 transfer->callback = callback;
841 * Helper function to populate the required \ref libusb_transfer fields
842 * for an isochronous transfer.
844 * \param transfer the transfer to populate
845 * \param dev_handle handle of the device that will handle the transfer
846 * \param endpoint address of the endpoint where this transfer will be sent
847 * \param buffer data buffer
848 * \param length length of data buffer
849 * \param num_iso_packets the number of isochronous packets
850 * \param callback callback function to be invoked on transfer completion
851 * \param user_data user data to pass to callback function
852 * \param timeout timeout for the transfer in milliseconds
854 static inline void libusb_fill_iso_transfer(struct libusb_transfer *transfer,
855 libusb_device_handle *dev_handle, unsigned char endpoint,
856 unsigned char *buffer, int length, int num_iso_packets,
857 libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
859 transfer->dev_handle = dev_handle;
860 transfer->endpoint = endpoint;
861 transfer->type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS;
862 transfer->timeout = timeout;
863 transfer->buffer = buffer;
864 transfer->length = length;
865 transfer->num_iso_packets = num_iso_packets;
866 transfer->user_data = user_data;
867 transfer->callback = callback;
872 int libusb_control_transfer(libusb_device_handle *dev_handle,
873 uint8_t request_type, uint8_t request, uint16_t value, uint16_t index,
874 unsigned char *data, uint16_t length, unsigned int timeout);
876 int libusb_bulk_transfer(libusb_device_handle *dev_handle,
877 unsigned char endpoint, unsigned char *data, int length,
878 int *actual_length, unsigned int timeout);
880 int libusb_interrupt_transfer(libusb_device_handle *dev_handle,
881 unsigned char endpoint, unsigned char *data, int length,
882 int *actual_length, unsigned int timeout);
884 /* polling and timeouts */
887 * File descriptor for polling
889 struct libusb_pollfd {
890 /** Numeric file descriptor */
893 /** Event flags to poll for from <poll.h>. POLLIN indicates that you
894 * should monitor this file descriptor for becoming ready to read from,
895 * and POLLOUT indicates that you should monitor this file descriptor for
896 * nonblocking write readiness. */
900 int libusb_handle_events_timeout(struct timeval *tv);
901 int libusb_handle_events(void);
902 int libusb_get_next_timeout(struct timeval *tv);
903 const struct libusb_pollfd **libusb_get_pollfds(void);
906 * Callback function, invoked when a new file descriptor should be added
907 * to the set of file descriptors monitored for events.
908 * \param fd the new file descriptor
909 * \param events events to monitor for, see \ref libusb_pollfd for a
911 * \see libusb_set_pollfd_notifiers
913 typedef void (*libusb_pollfd_added_cb)(int fd, short events);
916 * Callback function, invoked when a file descriptor should be removed from
917 * the set of file descriptors being monitored for events. After returning
918 * from this callback, do not use that file descriptor again.
919 * \param fd the file descriptor to stop monitoring
920 * \see libusb_set_pollfd_notifiers
922 typedef void (*libusb_pollfd_removed_cb)(int fd);
923 void libusb_set_pollfd_notifiers(libusb_pollfd_added_cb added_cb,
924 libusb_pollfd_removed_cb removed_cb);