295de38a9aee65cbe4ec0a15bb60dc0b0997005e
[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 <endian.h>
25 #include <stdint.h>
26 #include <sys/time.h>
27 #include <sys/types.h>
28 #include <time.h>
29
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)
37 #else
38 #error "Unrecognized endianness"
39 #endif
40
41 /** \def libusb_cpu_to_le16
42  * \ingroup misc
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
48  */
49
50 /** \def libusb_le16_to_cpu
51  * \ingroup misc
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
57  */
58
59 #ifdef __cplusplus
60 extern "C" {
61 #endif
62
63 /* standard USB stuff */
64
65 /** \ingroup desc
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.
71          */
72         LIBUSB_CLASS_PER_INTERFACE = 0,
73
74         /** Audio class */
75         LIBUSB_CLASS_AUDIO = 1,
76
77         /** Communications class */
78         LIBUSB_CLASS_COMM = 2,
79
80         /** Human Interface Device class */
81         LIBUSB_CLASS_HID = 3,
82
83         /** Printer dclass */
84         LIBUSB_CLASS_PRINTER = 7,
85
86         /** Picture transfer protocol class */
87         LIBUSB_CLASS_PTP = 6,
88
89         /** Mass storage class */
90         LIBUSB_CLASS_MASS_STORAGE = 8,
91
92         /** Hub class */
93         LIBUSB_CLASS_HUB = 9,
94
95         /** Data class */
96         LIBUSB_CLASS_DATA = 10,
97
98         /** Class is vendor-specific */
99         LIBUSB_CLASS_VENDOR_SPEC = 0xff,
100 };
101
102 /** \ingroup desc
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,
107
108         /** Configuration descriptor. See libusb_config_descriptor. */
109         LIBUSB_DT_CONFIG = 0x02,
110
111         /** String descriptor */
112         LIBUSB_DT_STRING = 0x03,
113
114         /** Interface descriptor. See libusb_interface_descriptor. */
115         LIBUSB_DT_INTERFACE = 0x04,
116
117         /** Endpoint descriptor. See libusb_endpoint_descriptor. */
118         LIBUSB_DT_ENDPOINT = 0x05,
119
120         /** HID descriptor */
121         LIBUSB_DT_HID = 0x21,
122
123         /** HID report descriptor */
124         LIBUSB_DT_REPORT = 0x22,
125
126         /** Physical descriptor */
127         LIBUSB_DT_PHYSICAL = 0x23,
128
129         /** Hub descriptor */
130         LIBUSB_DT_HUB = 0x29,
131 };
132
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
140
141 #define LIBUSB_ENDPOINT_ADDRESS_MASK    0x0f    /* in bEndpointAddress */
142 #define LIBUSB_ENDPOINT_DIR_MASK                0x80
143
144 /** \ingroup desc
145  * Endpoint direction. Values for bit 7 of the
146  * \ref libusb_endpoint_descriptor::bEndpointAddress "endpoint address" scheme.
147  */
148 enum libusb_endpoint_direction {
149         /** In: device-to-host */
150         LIBUSB_ENDPOINT_IN = 0x80,
151
152         /** Out: host-to-device */
153         LIBUSB_ENDPOINT_OUT = 0x00,
154 };
155
156 #define LIBUSB_TRANSFER_TYPE_MASK                       0x03    /* in bmAttributes */
157
158 /** \ingroup desc
159  * Endpoint transfer type. Values for bits 0:1 of the
160  * \ref libusb_endpoint_descriptor::bmAttributes "endpoint attributes" field.
161  */
162 enum libusb_transfer_type {
163         /** Control endpoint */
164         LIBUSB_TRANSFER_TYPE_CONTROL = 0,
165
166         /** Isochronous endpoint */
167         LIBUSB_TRANSFER_TYPE_ISOCHRONOUS = 1,
168
169         /** Bulk endpoint */
170         LIBUSB_TRANSFER_TYPE_BULK = 2,
171
172         /** Interrupt endpoint */
173         LIBUSB_TRANSFER_TYPE_INTERRUPT = 3,
174 };
175
176 /** \ingroup misc
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,
181
182         /** Clear or disable a specific feature */
183         LIBUSB_REQUEST_CLEAR_FEATURE = 0x01,
184
185         /* 0x02 is reserved */
186
187         /** Set or enable a specific feature */
188         LIBUSB_REQUEST_SET_FEATURE = 0x03,
189
190         /* 0x04 is reserved */
191
192         /** Set device address for all future accesses */
193         LIBUSB_REQUEST_SET_ADDRESS = 0x05,
194
195         /** Get the specified descriptor */
196         LIBUSB_REQUEST_GET_DESCRIPTOR = 0x06,
197
198         /** Used to update existing descriptors or add new descriptors */
199         LIBUSB_REQUEST_SET_DESCRIPTOR = 0x07,
200
201         /** Get the current device configuration value */
202         LIBUSB_REQUEST_GET_CONFIGURATION = 0x08,
203
204         /** Set device configuration */
205         LIBUSB_REQUEST_SET_CONFIGURATION = 0x09,
206
207         /** Return the selected alternate setting for the specified interface */
208         LIBUSB_REQUEST_GET_INTERFACE = 0x0A,
209         
210         /** Select an alternate interface for the specified interface */
211         LIBUSB_REQUEST_SET_INTERFACE = 0x0B,
212
213         /** Set then report an endpoint's synchronization frame */
214         LIBUSB_REQUEST_SYNCH_FRAME = 0x0C,
215 };
216
217 /** \ingroup misc
218  * Request type bits of the
219  * \ref libusb_control_setup::bmRequestType "bmRequestType" field in control
220  * transfers. */
221 enum libusb_request_type {
222         /** Standard */
223         LIBUSB_REQUEST_TYPE_STANDARD = (0x00 << 5),
224
225         /** Class */
226         LIBUSB_REQUEST_TYPE_CLASS = (0x01 << 5),
227
228         /** Vendor */
229         LIBUSB_REQUEST_TYPE_VENDOR = (0x02 << 5),
230
231         /** Reserved */
232         LIBUSB_REQUEST_TYPE_RESERVED = (0x03 << 5),
233 };
234
235 /** \ingroup misc
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 {
240         /** Device */
241         LIBUSB_RECIPIENT_DEVICE = 0x00,
242
243         /** Interface */
244         LIBUSB_RECIPIENT_INTERFACE = 0x01,
245
246         /** Endpoint */
247         LIBUSB_RECIPIENT_ENDPOINT = 0x02,
248
249         /** Other */
250         LIBUSB_RECIPIENT_OTHER = 0x03,
251 };
252
253 #define LIBUSB_ISO_SYNC_TYPE_MASK               0x0C
254
255 /** \ingroup desc
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.
259  */
260 enum libusb_iso_sync_type {
261         /** No synchronization */
262         LIBUSB_ISO_SYNC_TYPE_NONE = 0,
263
264         /** Asynchronous */
265         LIBUSB_ISO_SYNC_TYPE_ASYNC = 1,
266
267         /** Adaptive */
268         LIBUSB_ISO_SYNC_TYPE_ADAPTIVE = 2,
269
270         /** Synchronous */
271         LIBUSB_ISO_SYNC_TYPE_SYNC = 3,
272 };
273
274 #define LIBUSB_ISO_USAGE_TYPE_MASK 0x30
275
276 /** \ingroup desc
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.
280  */
281 enum libusb_iso_usage_type {
282         /** Data endpoint */
283         LIBUSB_ISO_USAGE_TYPE_DATA = 0,
284
285         /** Feedback endpoint */
286         LIBUSB_ISO_USAGE_TYPE_FEEDBACK = 1,
287
288         /** Implicit feedback Data endpoint */
289         LIBUSB_ISO_USAGE_TYPE_IMPLICIT = 2,
290 };
291
292 /** \ingroup desc
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.
296  */
297 struct libusb_device_descriptor {
298         /** Size of this descriptor (in bytes) */
299         uint8_t  bLength;
300
301         /** Descriptor type. Will have value
302          * \ref libusb_descriptor_type::LIBUSB_DT_DEVICE LIBUSB_DT_DEVICE in this
303          * context. */
304         uint8_t  bDescriptorType;
305
306         /** USB specification release number in binary-coded decimal. A value of
307          * 0x0200 indicates USB 2.0, 0x0110 indicates USB 1.1, etc. */
308         uint16_t bcdUSB;
309
310         /** USB-IF class code for the device. See \ref libusb_class_code. */
311         uint8_t  bDeviceClass;
312
313         /** USB-IF subclass code for the device, qualified by the bDeviceClass
314          * value */
315         uint8_t  bDeviceSubClass;
316
317         /** USB-IF protocol code for the device, qualified by the bDeviceClass and
318          * bDeviceSubClass values */
319         uint8_t  bDeviceProtocol;
320
321         /** Maximum packet size for endpoint 0 */
322         uint8_t  bMaxPacketSize0;
323
324         /** USB-IF vendor ID */
325         uint16_t idVendor;
326
327         /** USB-IF product ID */
328         uint16_t idProduct;
329
330         /** Device release number in binary-coded decimal */
331         uint16_t bcdDevice;
332
333         /** Index of string descriptor describing manufacturer */
334         uint8_t  iManufacturer;
335
336         /** Index of string descriptor describing product */
337         uint8_t  iProduct;
338
339         /** Index of string descriptor containing device serial number */
340         uint8_t  iSerialNumber;
341
342         /** Number of possible configurations */
343         uint8_t  bNumConfigurations;
344 };
345
346 /** \ingroup desc
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.
350  */
351 struct libusb_endpoint_descriptor {
352         /** Size of this descriptor (in bytes) */
353         uint8_t  bLength;
354
355         /** Descriptor type. Will have value
356          * \ref libusb_descriptor_type::LIBUSB_DT_ENDPOINT LIBUSB_DT_ENDPOINT in
357          * this context. */
358         uint8_t  bDescriptorType;
359
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.
363          */
364         uint8_t  bEndpointAddress;
365
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.
372          */
373         uint8_t  bmAttributes;
374
375         /** Maximum packet size this endpoint is capable of sending/receiving. */
376         uint16_t wMaxPacketSize;
377
378         /** Interval for polling endpoint for data transfers. */
379         uint8_t  bInterval;
380
381         /** For audio devices only: the rate at which synchronization feedback
382          * is provided. */
383         uint8_t  bRefresh;
384
385         /** For audio devices only: the address if the synch endpoint */
386         uint8_t  bSynchAddress;
387
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;
391
392         /** Length of the extra descriptors, in bytes. */
393         int extra_length;
394 };
395
396 /** \ingroup desc
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.
400  */
401 struct libusb_interface_descriptor {
402         /** Size of this descriptor (in bytes) */
403         uint8_t  bLength;
404
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;
409
410         /** Number of this interface */
411         uint8_t  bInterfaceNumber;
412
413         /** Value used to select this alternate setting for this interface */
414         uint8_t  bAlternateSetting;
415
416         /** Number of endpoints used by this interface (excluding the control
417          * endpoint). */
418         uint8_t  bNumEndpoints;
419         
420         /** USB-IF class code for this interface. See \ref libusb_class_code. */
421         uint8_t  bInterfaceClass;
422
423         /** USB-IF subclass code for this interface, qualified by the
424          * bInterfaceClass value */
425         uint8_t  bInterfaceSubClass;
426
427         /** USB-IF protocol code for this interface, qualified by the
428          * bInterfaceClass and bInterfaceSubClass values */
429         uint8_t  bInterfaceProtocol;
430
431         /** Index of string descriptor describing this interface */
432         uint8_t  iInterface;
433
434         /** Array of endpoint descriptors. This length of this array is determined
435          * by the bNumEndpoints field. */
436         const struct libusb_endpoint_descriptor *endpoint;
437
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;
441
442         /** Length of the extra descriptors, in bytes. */
443         int extra_length;
444 };
445
446 /** \ingroup desc
447  * A collection of alternate settings for a particular USB interface.
448  */
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;
453
454         /** The number of alternate settings that belong to this interface */
455         int num_altsetting;
456 };
457
458 /** \ingroup desc
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.
462  */
463 struct libusb_config_descriptor {
464         /** Size of this descriptor (in bytes) */
465         uint8_t  bLength;
466
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;
471
472         /** Total length of data returned for this configuration */
473         uint16_t wTotalLength;
474
475         /** Number of interfaces supported by this configuration */
476         uint8_t  bNumInterfaces;
477
478         /** Identifier value for this configuration */
479         uint8_t  bConfigurationValue;
480
481         /** Index of string descriptor describing this configuration */
482         uint8_t  iConfiguration;
483
484         /** Configuration characteristics */
485         uint8_t  bmAttributes;
486
487         /** Maximum power consumption of the USB device from this bus in this
488          * configuration when the device is fully opreation. Expressed in units
489          * of 2 mA. */
490         uint8_t  MaxPower;
491
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;
495
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;
499
500         /** Length of the extra descriptors, in bytes. */
501         int extra_length;
502 };
503
504 /** \ingroup asyncio
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.
511          */
512         uint8_t  bmRequestType;
513
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. */
519         uint8_t  bRequest;
520
521         /** Value. Varies according to request */
522         uint16_t wValue;
523
524         /** Index. Varies according to request, typically used to pass an index
525          * or offset */
526         uint16_t wIndex;
527
528         /** Number of bytes to transfer */
529         uint16_t wLength;
530 };
531
532 #define LIBUSB_CONTROL_SETUP_SIZE (sizeof(struct libusb_control_setup))
533
534 /* libusb */
535
536 struct libusb_device;
537 struct libusb_device_handle;
538
539 /** \ingroup dev
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().
543  *
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().
546  *
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().
553  */
554 typedef struct libusb_device libusb_device;
555
556
557 /** \ingroup dev
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
560  * libusb_open().
561  *
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().
564  */
565 typedef struct libusb_device_handle libusb_device_handle;
566
567 /** \ingroup misc
568  * Error codes. Most libusb functions return 0 on success or one of these
569  * codes on failure.
570  */
571 enum libusb_error {
572         /** Success (no error) */
573         LIBUSB_SUCCESS = 0,
574
575         /** Input/output error */
576         LIBUSB_ERROR_IO = -1,
577
578         /** Invalid parameter */
579         LIBUSB_ERROR_INVALID_PARAM = -2,
580
581         /** Access denied (insufficient permissions) */
582         LIBUSB_ERROR_ACCESS = -3,
583
584         /** No such device (it may have been disconnected) */
585         LIBUSB_ERROR_NO_DEVICE = -4,
586
587         /** Entity not found */
588         LIBUSB_ERROR_NOT_FOUND = -5,
589
590         /** Resource busy */
591         LIBUSB_ERROR_BUSY = -6,
592
593         /** Operation timed out */
594         LIBUSB_ERROR_TIMEOUT = -7,
595
596         /** Pipe error */
597         LIBUSB_ERROR_PIPE = -8,
598
599         /** System call interrupted (perhaps due to signal) */
600         LIBUSB_ERROR_INTERRUPTED = -9,
601
602         /** Insufficient memory */
603         LIBUSB_ERROR_NO_MEM = -10,
604
605         /** Operation not supported or unimplemented on this platform */
606         LIBUSB_ERROR_NOT_SUPPORTED = -11,
607
608         /** Other error */
609         LIBUSB_ERROR_OTHER = -12,
610 };
611
612 /** \ingroup asyncio
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,
618
619         /** Transfer failed */
620         LIBUSB_TRANSFER_ERROR,
621
622         /** Transfer timed out */
623         LIBUSB_TRANSFER_TIMED_OUT,
624
625         /** Transfer was cancelled */
626         LIBUSB_TRANSFER_CANCELLED,
627
628         /** For bulk/interrupt endpoints: halt condition detected (endpoint
629          * stalled). For control endpoints: control request not supported. */
630         LIBUSB_TRANSFER_STALL,
631
632         /** Device was disconnected */
633         LIBUSB_TRANSFER_NO_DEVICE,
634 };
635
636 /** \ingroup asyncio
637  * libusb_transfer.flags values */
638 enum libusb_transfer_flags {
639         /** Report short frames as errors */
640         LIBUSB_TRANSFER_SHORT_NOT_OK = 1<<0,
641
642         /** Automatically free() transfer buffer during libusb_free_transfer() */
643         LIBUSB_TRANSFER_FREE_BUFFER = 1<<1,
644
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,
650 };
651
652 /** \ingroup asyncio
653  * Isochronous packet descriptor. */
654 struct libusb_iso_packet_descriptor {
655         /** Length of data to request in this packet */
656         unsigned int length;
657
658         /** Amount of data that was actually transferred */
659         unsigned int actual_length;
660
661         /** Status code for this packet */
662         enum libusb_transfer_status status;
663 };
664
665 struct libusb_transfer;
666
667 typedef void (*libusb_transfer_cb_fn)(struct libusb_transfer *transfer);
668
669 /** \ingroup asyncio
670  * The generic USB transfer structure.
671  */
672 struct libusb_transfer {
673         /** Handle of the device that this transfer will be submitted to */
674         libusb_device_handle *dev_handle;
675
676         /** A bitwise OR combination of \ref libusb_transfer_flags. */
677         uint8_t flags;
678
679         /** Address of the endpoint where this transfer will be sent. */
680         unsigned char endpoint;
681
682         /** Type of the endpoint from \ref libusb_transfer_type */
683         unsigned char type;
684
685         /** Timeout for this transfer in millseconds. A value of 0 indicates no
686          * timeout. */
687         unsigned int timeout;
688
689         /** The status of the transfer. Read-only, and only for use within
690          * transfer callback function.
691          *
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;
697
698         /** Length of the data buffer */
699         int length;
700
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. */
704         int actual_length;
705
706         /** Callback function. This will be invoked when the transfer completes,
707          * fails, or is cancelled. */
708         libusb_transfer_cb_fn callback;
709
710         /** User context data to pass to the callback function. */
711         void *user_data;
712
713         /** Data buffer */
714         unsigned char *buffer;
715
716         /** Number of isochronous packets. Only used for I/O with isochronous
717          * endpoints. */
718         int num_iso_packets;
719
720         /** Isochronous packet descriptors, for isochronous transfers only. */
721         struct libusb_iso_packet_descriptor iso_packet_desc[0];
722 };
723
724 int libusb_init(void);
725 void libusb_exit(void);
726 void libusb_set_debug(int level);
727
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);
732
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);
745
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);
749
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);
753
754 libusb_device_handle *libusb_open_device_with_vid_pid(uint16_t vendor_id,
755         uint16_t product_id);
756
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);
761
762 int libusb_kernel_driver_active(libusb_device_handle *dev, int interface);
763 int libusb_detach_kernel_driver(libusb_device_handle *dev, int interface);
764
765 /* async I/O */
766
767 /** \ingroup asyncio
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. 
771  *
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
774  * transfer->buffer.
775  *
776  * \param transfer a transfer
777  * \returns pointer to the first byte of the data section
778  */
779 static inline unsigned char *libusb_control_transfer_get_data(
780         struct libusb_transfer *transfer)
781 {
782         return transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE;
783 }
784
785 /** \ingroup asyncio
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.
789  *
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
792  * transfer->buffer.
793  *
794  * \param transfer a transfer
795  * \returns a casted pointer to the start of the transfer data buffer
796  */
797 static inline struct libusb_control_setup *libusb_control_transfer_get_setup(
798         struct libusb_transfer *transfer)
799 {
800         return (struct libusb_control_setup *) transfer->buffer;
801 }
802
803 /** \ingroup asyncio
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.
807  * 
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
824  */
825 static inline void libusb_fill_control_setup(unsigned char *buffer,
826         uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
827         uint16_t wLength)
828 {
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);
835 }
836
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);
841
842 /** \ingroup asyncio
843  * Helper function to populate the required \ref libusb_transfer fields
844  * for a control transfer.
845  *
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()
856  *
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.
860  *
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
868  */
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)
873 {
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;
880         if (setup)
881                 transfer->length = LIBUSB_CONTROL_SETUP_SIZE
882                         + libusb_le16_to_cpu(setup->wLength);
883         transfer->user_data = user_data;
884         transfer->callback = callback;
885 }
886
887 /** \ingroup asyncio
888  * Helper function to populate the required \ref libusb_transfer fields
889  * for a bulk transfer.
890  *
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
899  */
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)
904 {
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;
913 }
914
915 /** \ingroup asyncio
916  * Helper function to populate the required \ref libusb_transfer fields
917  * for an interrupt transfer.
918  *
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
927  */
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)
932 {
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;
941 }
942
943 /** \ingroup asyncio
944  * Helper function to populate the required \ref libusb_transfer fields
945  * for an isochronous transfer.
946  *
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
956  */
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)
961 {
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;
971 }
972
973 /** \ingroup asyncio
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.
976  *
977  * \param transfer a transfer
978  * \param length the length to set in each isochronous packet descriptor
979  * \see libusb_get_max_packet_size()
980  */
981 static inline void libusb_set_iso_packet_lengths(
982         struct libusb_transfer *transfer, unsigned int length)
983 {
984         int i;
985         for (i = 0; i < transfer->num_iso_packets; i++)
986                 transfer->iso_packet_desc[i].length = length;
987 }
988
989 /** \ingroup asyncio
990  * Convenience function to locate the position of an isochronous packet
991  * within the buffer of an isochronous transfer.
992  *
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.
998  * 
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()
1004  */
1005 static inline unsigned char *libusb_get_iso_packet_buffer(
1006         struct libusb_transfer *transfer, unsigned int packet)
1007 {
1008         int i;
1009         size_t offset = 0;
1010
1011         if (packet >= transfer->num_iso_packets)
1012                 return NULL;
1013
1014         for (i = 0; i < packet; i++)
1015                 offset += transfer->iso_packet_desc[i].length;
1016
1017         return transfer->buffer + offset;
1018 }
1019
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.
1024  *
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>
1029  *
1030  * Do not use this function on transfers other than those that have identical
1031  * packet lengths for each packet.
1032  *
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()
1038  */
1039 static inline unsigned char *libusb_get_iso_packet_buffer_simple(
1040         struct libusb_transfer *transfer, unsigned int packet)
1041 {
1042         if (packet >= transfer->num_iso_packets)
1043                 return NULL;
1044         
1045         return transfer->buffer + (transfer->iso_packet_desc[0].length * packet);
1046 }
1047
1048 /* sync I/O */
1049
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);
1053
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);
1057
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);
1061
1062 /** \ingroup desc
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.
1066  *
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
1073  */
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)
1076 {
1077         return libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN,
1078                 LIBUSB_REQUEST_GET_DESCRIPTOR, (desc_type << 8) | desc_index, 0, data,
1079                 length, 1000);
1080 }
1081
1082 /** \ingroup desc
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.
1087  *
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()
1095  */
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)
1098 {
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);
1102 }
1103
1104 int libusb_get_string_descriptor_ascii(libusb_device_handle *dev,
1105         uint8_t index, unsigned char *data, int length);
1106
1107 /* polling and timeouts */
1108
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);
1116
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);
1121
1122 /** \ingroup poll
1123  * File descriptor for polling
1124  */
1125 struct libusb_pollfd {
1126         /** Numeric file descriptor */
1127         int fd;
1128
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. */
1133         short events;
1134 };
1135
1136 /** \ingroup poll
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
1141  * description
1142  * \see libusb_set_pollfd_notifiers()
1143  */
1144 typedef void (*libusb_pollfd_added_cb)(int fd, short events);
1145
1146 /** \ingroup poll
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()
1152  */
1153 typedef void (*libusb_pollfd_removed_cb)(int fd);
1154
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);
1158
1159 #ifdef __cplusplus
1160 }
1161 #endif
1162
1163 #endif