Add functionality to clear endpoint halt conditions
[platform/upstream/libusb.git] / libusb / libusb.h
1 /*
2  * Public libusb header file
3  * Copyright (C) 2007-2008 Daniel Drake <dsd@gentoo.org>
4  * Copyright (c) 2001 Johannes Erdfelt <johannes@erdfelt.com>
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #ifndef __LIBUSB_H__
22 #define __LIBUSB_H__
23
24 #include <stdint.h>
25 #include <sys/time.h>
26 #include <time.h>
27
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31
32 /* standard USB stuff */
33
34 /** \ingroup desc
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.
40          */
41         LIBUSB_CLASS_PER_INTERFACE = 0,
42
43         /** Audio class */
44         LIBUSB_CLASS_AUDIO = 1,
45
46         /** Communications class */
47         LIBUSB_CLASS_COMM = 2,
48
49         /** Human Interface Device class */
50         LIBUSB_CLASS_HID = 3,
51
52         /** Printer dclass */
53         LIBUSB_CLASS_PRINTER = 7,
54
55         /** Picture transfer protocol class */
56         LIBUSB_CLASS_PTP = 6,
57
58         /** Mass storage class */
59         LIBUSB_CLASS_MASS_STORAGE = 8,
60
61         /** Hub class */
62         LIBUSB_CLASS_HUB = 9,
63
64         /** Data class */
65         LIBUSB_CLASS_DATA = 10,
66
67         /** Class is vendor-specific */
68         LIBUSB_CLASS_VENDOR_SPEC = 0xff,
69 };
70
71 /** \ingroup desc
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,
76
77         /** Configuration descriptor. See libusb_config_descriptor. */
78         LIBUSB_DT_CONFIG = 0x02,
79
80         /** String descriptor */
81         LIBUSB_DT_STRING = 0x03,
82
83         /** Interface descriptor. See libusb_interface_descriptor. */
84         LIBUSB_DT_INTERFACE = 0x04,
85
86         /** Endpoint descriptor. See libusb_endpoint_descriptor. */
87         LIBUSB_DT_ENDPOINT = 0x05,
88
89         /** HID descriptor */
90         LIBUSB_DT_HID = 0x21,
91
92         /** HID report descriptor */
93         LIBUSB_DT_REPORT = 0x22,
94
95         /** Physical descriptor */
96         LIBUSB_DT_PHYSICAL = 0x23,
97
98         /** Hub descriptor */
99         LIBUSB_DT_HUB = 0x29,
100 };
101
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
109
110 #define LIBUSB_ENDPOINT_ADDRESS_MASK    0x0f    /* in bEndpointAddress */
111 #define LIBUSB_ENDPOINT_DIR_MASK                0x80
112
113 /** \ingroup desc
114  * Endpoint direction. Values for bit 7 of the
115  * \ref libusb_endpoint_descriptor::bEndpointAddress "endpoint address" scheme.
116  */
117 enum libusb_endpoint_direction {
118         /** In: device-to-host */
119         LIBUSB_ENDPOINT_IN = 0x80,
120
121         /** Out: host-to-device */
122         LIBUSB_ENDPOINT_OUT = 0x00,
123 };
124
125 #define LIBUSB_TRANSFER_TYPE_MASK                       0x03    /* in bmAttributes */
126
127 /** \ingroup desc
128  * Endpoint transfer type. Values for bits 0:1 of the
129  * \ref libusb_endpoint_descriptor::bmAttributes "endpoint attributes" field.
130  */
131 enum libusb_transfer_type {
132         /** Control endpoint */
133         LIBUSB_TRANSFER_TYPE_CONTROL = 0,
134
135         /** Isochronous endpoint */
136         LIBUSB_TRANSFER_TYPE_ISOCHRONOUS = 1,
137
138         /** Bulk endpoint */
139         LIBUSB_TRANSFER_TYPE_BULK = 2,
140
141         /** Interrupt endpoint */
142         LIBUSB_TRANSFER_TYPE_INTERRUPT = 3,
143 };
144
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,
149
150         /** Clear or disable a specific feature */
151         LIBUSB_REQUEST_CLEAR_FEATURE = 0x01,
152
153         /* 0x02 is reserved */
154
155         /** Set or enable a specific feature */
156         LIBUSB_REQUEST_SET_FEATURE = 0x03,
157
158         /* 0x04 is reserved */
159
160         /** Set device address for all future accesses */
161         LIBUSB_REQUEST_SET_ADDRESS = 0x05,
162
163         /** Get the specified descriptor */
164         LIBUSB_REQUEST_GET_DESCRIPTOR = 0x06,
165
166         /** Used to update existing descriptors or add new descriptors */
167         LIBUSB_REQUEST_SET_DESCRIPTOR = 0x07,
168
169         /** Get the current device configuration value */
170         LIBUSB_REQUEST_GET_CONFIGURATION = 0x08,
171
172         /** Set device configuration */
173         LIBUSB_REQUEST_SET_CONFIGURATION = 0x09,
174
175         /** Return the selected alternate setting for the specified interface */
176         LIBUSB_REQUEST_GET_INTERFACE = 0x0A,
177         
178         /** Select an alternate interface for the specified interface */
179         LIBUSB_REQUEST_SET_INTERFACE = 0x0B,
180
181         /** Set then report an endpoint's synchronization frame */
182         LIBUSB_REQUEST_SYNCH_FRAME = 0x0C,
183 };
184
185 /** \ingroup misc
186  * Request type bits of the
187  * \ref libusb_control_setup::bmRequestType "bmRequestType" field in control
188  * transfers. */
189 enum libusb_request_type {
190         /** Standard */
191         LIBUSB_REQUEST_TYPE_STANDARD = (0x00 << 5),
192
193         /** Class */
194         LIBUSB_REQUEST_TYPE_CLASS = (0x01 << 5),
195
196         /** Vendor */
197         LIBUSB_REQUEST_TYPE_VENDOR = (0x02 << 5),
198
199         /** Reserved */
200         LIBUSB_REQUEST_TYPE_RESERVED = (0x03 << 5),
201 };
202
203 /** \ingroup misc
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 {
208         /** Device */
209         LIBUSB_RECIPIENT_DEVICE = 0x00,
210
211         /** Interface */
212         LIBUSB_RECIPIENT_INTERFACE = 0x01,
213
214         /** Endpoint */
215         LIBUSB_RECIPIENT_ENDPOINT = 0x02,
216
217         /** Other */
218         LIBUSB_RECIPIENT_OTHER = 0x03,
219 };
220
221 #define LIBUSB_ISO_SYNC_TYPE_MASK               0x0C
222
223 /** \ingroup desc
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.
227  */
228 enum libusb_iso_sync_type {
229         /** No synchronization */
230         LIBUSB_ISO_SYNC_TYPE_NONE = 0,
231
232         /** Asynchronous */
233         LIBUSB_ISO_SYNC_TYPE_ASYNC = 1,
234
235         /** Adaptive */
236         LIBUSB_ISO_SYNC_TYPE_ADAPTIVE = 2,
237
238         /** Synchronous */
239         LIBUSB_ISO_SYNC_TYPE_SYNC = 3,
240 };
241
242 #define LIBUSB_ISO_USAGE_TYPE_MASK 0x30
243
244 /** \ingroup desc
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.
248  */
249 enum libusb_iso_usage_type {
250         /** Data endpoint */
251         LIBUSB_ISO_USAGE_TYPE_DATA = 0,
252
253         /** Feedback endpoint */
254         LIBUSB_ISO_USAGE_TYPE_FEEDBACK = 1,
255
256         /** Implicit feedback Data endpoint */
257         LIBUSB_ISO_USAGE_TYPE_IMPLICIT = 2,
258 };
259
260 /** \ingroup desc
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.
264  */
265 struct libusb_device_descriptor {
266         /** Size of this descriptor (in bytes) */
267         uint8_t  bLength;
268
269         /** Descriptor type. Will have value
270          * \ref libusb_descriptor_type::LIBUSB_DT_DEVICE LIBUSB_DT_DEVICE in this
271          * context. */
272         uint8_t  bDescriptorType;
273
274         /** USB specification release number in binary-coded decimal. A value of
275          * 0x0200 indicates USB 2.0, 0x0110 indicates USB 1.1, etc. */
276         uint16_t bcdUSB;
277
278         /** USB-IF class code for the device. See \ref libusb_class_code. */
279         uint8_t  bDeviceClass;
280
281         /** USB-IF subclass code for the device, qualified by the bDeviceClass
282          * value */
283         uint8_t  bDeviceSubClass;
284
285         /** USB-IF protocol code for the device, qualified by the bDeviceClass and
286          * bDeviceSubClass values */
287         uint8_t  bDeviceProtocol;
288
289         /** Maximum packet size for endpoint 0 */
290         uint8_t  bMaxPacketSize0;
291
292         /** USB-IF vendor ID */
293         uint16_t idVendor;
294
295         /** USB-IF product ID */
296         uint16_t idProduct;
297
298         /** Device release number in binary-coded decimal */
299         uint16_t bcdDevice;
300
301         /** Index of string descriptor describing manufacturer */
302         uint8_t  iManufacturer;
303
304         /** Index of string descriptor describing product */
305         uint8_t  iProduct;
306
307         /** Index of string descriptor containing device serial number */
308         uint8_t  iSerialNumber;
309
310         /** Number of possible configurations */
311         uint8_t  bNumConfigurations;
312 };
313
314 /** \ingroup desc
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.
318  */
319 struct libusb_endpoint_descriptor {
320         /** Size of this descriptor (in bytes) */
321         uint8_t  bLength;
322
323         /** Descriptor type. Will have value
324          * \ref libusb_descriptor_type::LIBUSB_DT_ENDPOINT LIBUSB_DT_ENDPOINT in
325          * this context. */
326         uint8_t  bDescriptorType;
327
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.
331          */
332         uint8_t  bEndpointAddress;
333
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.
340          */
341         uint8_t  bmAttributes;
342
343         /** Maximum packet size this endpoint is capable of sending/receiving. */
344         uint16_t wMaxPacketSize;
345
346         /** Interval for polling endpoint for data transfers. */
347         uint8_t  bInterval;
348
349         uint8_t  bRefresh;
350         uint8_t  bSynchAddress;
351
352         const unsigned char *extra;     /* Extra descriptors */
353         int extralen;
354 };
355
356 /** \ingroup desc
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.
360  */
361 struct libusb_interface_descriptor {
362         /** Size of this descriptor (in bytes) */
363         uint8_t  bLength;
364
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;
369
370         /** Number of this interface */
371         uint8_t  bInterfaceNumber;
372
373         /** Value used to select this alternate setting for this interface */
374         uint8_t  bAlternateSetting;
375
376         /** Number of endpoints used by this interface (excluding the control
377          * endpoint). */
378         uint8_t  bNumEndpoints;
379         
380         /** USB-IF class code for this interface. See \ref libusb_class_code. */
381         uint8_t  bInterfaceClass;
382
383         /** USB-IF subclass code for this interface, qualified by the
384          * bInterfaceClass value */
385         uint8_t  bInterfaceSubClass;
386
387         /** USB-IF protocol code for this interface, qualified by the
388          * bInterfaceClass and bInterfaceSubClass values */
389         uint8_t  bInterfaceProtocol;
390
391         /** Index of string descriptor describing this interface */
392         uint8_t  iInterface;
393
394         /** Array of endpoint descriptors. This length of this array is determined
395          * by the bNumEndpoints field. */
396         const struct libusb_endpoint_descriptor *endpoint;
397
398         const unsigned char *extra;     /* Extra descriptors */
399         int extralen;
400 };
401
402 /** \ingroup desc
403  * A collection of alternate settings for a particular USB interface.
404  */
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;
409
410         /** The number of alternate settings that belong to this interface */
411         int num_altsetting;
412 };
413
414 /** \ingroup desc
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.
418  */
419 struct libusb_config_descriptor {
420         /** Size of this descriptor (in bytes) */
421         uint8_t  bLength;
422
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;
427
428         /** Total length of data returned for this configuration */
429         uint16_t wTotalLength;
430
431         /** Number of interfaces supported by this configuration */
432         uint8_t  bNumInterfaces;
433
434         /** Identifier value for this configuration */
435         uint8_t  bConfigurationValue;
436
437         /** Index of string descriptor describing this configuration */
438         uint8_t  iConfiguration;
439
440         /** Configuration characteristics */
441         uint8_t  bmAttributes;
442
443         /** Maximum power consumption of the USB device from this bus in this
444          * configuration when the device is fully opreation. Expressed in units
445          * of 2 mA. */
446         uint8_t  MaxPower;
447
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;
451
452         const unsigned char *extra;     /* Extra descriptors */
453         int extralen;
454 };
455
456 /** \ingroup asyncio
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.
463          */
464         uint8_t  bmRequestType;
465
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. */
471         uint8_t  bRequest;
472
473         /** Value. Varies according to request */
474         uint16_t wValue;
475
476         /** Index. Varies according to request, typically used to pass an index
477          * or offset */
478         uint16_t wIndex;
479
480         /** Number of bytes to transfer */
481         uint16_t wLength;
482 } __attribute__((packed));
483
484 #define LIBUSB_CONTROL_SETUP_SIZE (sizeof(struct libusb_control_setup))
485
486 /* libusb */
487
488 struct libusb_device;
489 struct libusb_device_handle;
490
491 /** \ingroup dev
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().
495  *
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().
498  *
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().
505  */
506 typedef struct libusb_device libusb_device;
507
508
509 /** \ingroup dev
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
512  * libusb_open().
513  *
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().
516  */
517 typedef struct libusb_device_handle libusb_device_handle;
518
519 enum libusb_error {
520         LIBUSB_SUCCESS = 0,
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,
528 };
529
530 /** \ingroup asyncio
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,
536
537         /** Transfer failed */
538         LIBUSB_TRANSFER_ERROR,
539
540         /** Transfer timed out */
541         LIBUSB_TRANSFER_TIMED_OUT,
542
543         /** Transfer was cancelled */
544         LIBUSB_TRANSFER_CANCELLED,
545 };
546
547 /** \ingroup asyncio
548  * libusb_transfer.flags values */
549 enum libusb_transfer_flags {
550         /** Report short frames as errors */
551         LIBUSB_TRANSFER_SHORT_NOT_OK = 1<<0,
552
553         /** Automatically free() transfer buffer during libusb_free_transfer() */
554         LIBUSB_TRANSFER_FREE_BUFFER = 1<<1,
555
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,
561 };
562
563 /** \ingroup asyncio
564  * Isochronous packet descriptor. */
565 struct libusb_iso_packet_descriptor {
566         /** Length of data to request in this packet */
567         unsigned int length;
568
569         /** Amount of data that was actually transferred */
570         unsigned int actual_length;
571
572         /** Status code for this packet */
573         enum libusb_transfer_status status;
574 };
575
576 struct libusb_transfer;
577
578 typedef void (*libusb_transfer_cb_fn)(struct libusb_transfer *transfer);
579
580 /** \ingroup asyncio
581  * The generic USB transfer structure.
582  */
583 struct libusb_transfer {
584         /** Handle of the device that this transfer will be submitted to */
585         libusb_device_handle *dev_handle;
586
587         /** A bitwise OR combination of \ref libusb_transfer_flags. */
588         uint8_t flags;
589
590         /** Address of the endpoint where this transfer will be sent. */
591         unsigned char endpoint;
592
593         /** Type of the endpoint from \ref libusb_transfer_type */
594         unsigned char type;
595
596         /** Timeout for this transfer in millseconds. A value of 0 indicates no
597          * timeout. */
598         unsigned int timeout;
599
600         /** The status of the transfer. Read-only, and only for use within
601          * transfer callback function.
602          *
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;
608
609         /** Length of the data buffer */
610         int length;
611
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. */
615         int actual_length;
616
617         /** Callback function. This will be invoked when the transfer completes,
618          * fails, or is cancelled. */
619         libusb_transfer_cb_fn callback;
620
621         /** User context data to pass to the callback function. */
622         void *user_data;
623
624         /** Data buffer */
625         unsigned char *buffer;
626
627         /** Number of isochronous packets. Only used for I/O with isochronous
628          * endpoints. */
629         int num_iso_packets;
630
631         /** Isochronous packet descriptors, for isochronous transfers only. */
632         struct libusb_iso_packet_descriptor iso_packet_desc[0];
633 };
634
635 int libusb_init(void);
636 void libusb_exit(void);
637
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(
641         libusb_device *dev);
642 const struct libusb_config_descriptor *libusb_get_config_descriptor(
643         libusb_device *dev);
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);
648
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);
652
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);
656
657 libusb_device_handle *libusb_open_device_with_vid_pid(uint16_t vendor_id,
658         uint16_t product_id);
659
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);
663
664 /* async I/O */
665
666 /** \ingroup asyncio
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. 
670  *
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
673  * transfer->buffer.
674  *
675  * \param transfer a transfer
676  * \returns pointer to the first byte of the data section
677  */
678 static inline unsigned char *libusb_control_transfer_get_data(
679         struct libusb_transfer *transfer)
680 {
681         return transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE;
682 }
683
684 /** \ingroup asyncio
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.
688  *
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
691  * transfer->buffer.
692  *
693  * \param transfer a transfer
694  * \returns a casted pointer to the start of the transfer data buffer
695  */
696 static inline struct libusb_control_setup *libusb_control_transfer_get_setup(
697         struct libusb_transfer *transfer)
698 {
699         return (struct libusb_control_setup *) transfer->buffer;
700 }
701
702 /** \ingroup asyncio
703  * Helper function to populate the setup packet (first 8 bytes of the data
704  * buffer) for a control transfer.
705  * 
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
722  */
723 static inline void libusb_fill_control_setup(unsigned char *buffer,
724         uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
725         uint16_t wLength)
726 {
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;
733 }
734
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);
739
740 /** \ingroup asyncio
741  * Helper function to populate the required \ref libusb_transfer fields
742  * for a control transfer.
743  *
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()
754  *
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.
758  *
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
766  */
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)
771 {
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;
778         if (setup)
779                 transfer->length = LIBUSB_CONTROL_SETUP_SIZE + setup->wLength;
780         transfer->user_data = user_data;
781         transfer->callback = callback;
782 }
783
784 /** \ingroup asyncio
785  * Helper function to populate the required \ref libusb_transfer fields
786  * for a bulk transfer.
787  *
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
796  */
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)
801 {
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;
810 }
811
812 /** \ingroup asyncio
813  * Helper function to populate the required \ref libusb_transfer fields
814  * for an interrupt transfer.
815  *
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
824  */
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)
829 {
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;
838 }
839
840 /** \ingroup asyncio
841  * Helper function to populate the required \ref libusb_transfer fields
842  * for an isochronous transfer.
843  *
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
853  */
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)
858 {
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;
868 }
869
870 /* sync I/O */
871
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);
875
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);
879
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);
883
884 /* polling and timeouts */
885
886 /** \ingroup poll
887  * File descriptor for polling
888  */
889 struct libusb_pollfd {
890         /** Numeric file descriptor */
891         int fd;
892
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. */
897         short events;
898 };
899
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);
904
905 /** \ingroup poll
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
910  * description
911  * \see libusb_set_pollfd_notifiers
912  */
913 typedef void (*libusb_pollfd_added_cb)(int fd, short events);
914
915 /** \ingroup poll
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
921  */
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);
925
926 #ifdef __cplusplus
927 }
928 #endif
929
930 #endif