• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <sys/types.h>
27 #include <time.h>
28 #include <limits.h>
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 /** \def libusb_cpu_to_le16
35  * \ingroup misc
36  * Convert a 16-bit value from host-endian to little-endian format. On
37  * little endian systems, this function does nothing. On big endian systems,
38  * the bytes are swapped.
39  * \param x the host-endian value to convert
40  * \returns the value in little-endian byte order
41  */
42 #define libusb_cpu_to_le16(x) ({ \
43 	union { \
44 		uint8_t  b8[2]; \
45 		uint16_t b16; \
46 	} _tmp; \
47 	uint16_t _tmp2 = (uint16_t)(x); \
48 	_tmp.b8[1] = _tmp2 >> 8; \
49 	_tmp.b8[0] = _tmp2 & 0xff; \
50 	_tmp.b16; \
51 })
52 
53 /** \def libusb_le16_to_cpu
54  * \ingroup misc
55  * Convert a 16-bit value from little-endian to host-endian format. On
56  * little endian systems, this function does nothing. On big endian systems,
57  * the bytes are swapped.
58  * \param x the little-endian value to convert
59  * \returns the value in host-endian byte order
60  */
61 #define libusb_le16_to_cpu libusb_cpu_to_le16
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 	/** Wireless class */
99 	LIBUSB_CLASS_WIRELESS = 0xe0,
100 
101 	/** Application class */
102 	LIBUSB_CLASS_APPLICATION = 0xfe,
103 
104 	/** Class is vendor-specific */
105 	LIBUSB_CLASS_VENDOR_SPEC = 0xff
106 };
107 
108 /** \ingroup desc
109  * Descriptor types as defined by the USB specification. */
110 enum libusb_descriptor_type {
111 	/** Device descriptor. See libusb_device_descriptor. */
112 	LIBUSB_DT_DEVICE = 0x01,
113 
114 	/** Configuration descriptor. See libusb_config_descriptor. */
115 	LIBUSB_DT_CONFIG = 0x02,
116 
117 	/** String descriptor */
118 	LIBUSB_DT_STRING = 0x03,
119 
120 	/** Interface descriptor. See libusb_interface_descriptor. */
121 	LIBUSB_DT_INTERFACE = 0x04,
122 
123 	/** Endpoint descriptor. See libusb_endpoint_descriptor. */
124 	LIBUSB_DT_ENDPOINT = 0x05,
125 
126 	/** HID descriptor */
127 	LIBUSB_DT_HID = 0x21,
128 
129 	/** HID report descriptor */
130 	LIBUSB_DT_REPORT = 0x22,
131 
132 	/** Physical descriptor */
133 	LIBUSB_DT_PHYSICAL = 0x23,
134 
135 	/** Hub descriptor */
136 	LIBUSB_DT_HUB = 0x29
137 };
138 
139 /* Descriptor sizes per descriptor type */
140 #define LIBUSB_DT_DEVICE_SIZE			18
141 #define LIBUSB_DT_CONFIG_SIZE			9
142 #define LIBUSB_DT_INTERFACE_SIZE		9
143 #define LIBUSB_DT_ENDPOINT_SIZE		7
144 #define LIBUSB_DT_ENDPOINT_AUDIO_SIZE	9	/* Audio extension */
145 #define LIBUSB_DT_HUB_NONVAR_SIZE		7
146 
147 #define LIBUSB_ENDPOINT_ADDRESS_MASK	0x0f    /* in bEndpointAddress */
148 #define LIBUSB_ENDPOINT_DIR_MASK		0x80
149 
150 /** \ingroup desc
151  * Endpoint direction. Values for bit 7 of the
152  * \ref libusb_endpoint_descriptor::bEndpointAddress "endpoint address" scheme.
153  */
154 enum libusb_endpoint_direction {
155 	/** In: device-to-host */
156 	LIBUSB_ENDPOINT_IN = 0x80,
157 
158 	/** Out: host-to-device */
159 	LIBUSB_ENDPOINT_OUT = 0x00
160 };
161 
162 #define LIBUSB_TRANSFER_TYPE_MASK			0x03    /* in bmAttributes */
163 
164 /** \ingroup desc
165  * Endpoint transfer type. Values for bits 0:1 of the
166  * \ref libusb_endpoint_descriptor::bmAttributes "endpoint attributes" field.
167  */
168 enum libusb_transfer_type {
169 	/** Control endpoint */
170 	LIBUSB_TRANSFER_TYPE_CONTROL = 0,
171 
172 	/** Isochronous endpoint */
173 	LIBUSB_TRANSFER_TYPE_ISOCHRONOUS = 1,
174 
175 	/** Bulk endpoint */
176 	LIBUSB_TRANSFER_TYPE_BULK = 2,
177 
178 	/** Interrupt endpoint */
179 	LIBUSB_TRANSFER_TYPE_INTERRUPT = 3
180 };
181 
182 /** \ingroup misc
183  * Standard requests, as defined in table 9-3 of the USB2 specifications */
184 enum libusb_standard_request {
185 	/** Request status of the specific recipient */
186 	LIBUSB_REQUEST_GET_STATUS = 0x00,
187 
188 	/** Clear or disable a specific feature */
189 	LIBUSB_REQUEST_CLEAR_FEATURE = 0x01,
190 
191 	/* 0x02 is reserved */
192 
193 	/** Set or enable a specific feature */
194 	LIBUSB_REQUEST_SET_FEATURE = 0x03,
195 
196 	/* 0x04 is reserved */
197 
198 	/** Set device address for all future accesses */
199 	LIBUSB_REQUEST_SET_ADDRESS = 0x05,
200 
201 	/** Get the specified descriptor */
202 	LIBUSB_REQUEST_GET_DESCRIPTOR = 0x06,
203 
204 	/** Used to update existing descriptors or add new descriptors */
205 	LIBUSB_REQUEST_SET_DESCRIPTOR = 0x07,
206 
207 	/** Get the current device configuration value */
208 	LIBUSB_REQUEST_GET_CONFIGURATION = 0x08,
209 
210 	/** Set device configuration */
211 	LIBUSB_REQUEST_SET_CONFIGURATION = 0x09,
212 
213 	/** Return the selected alternate setting for the specified interface */
214 	LIBUSB_REQUEST_GET_INTERFACE = 0x0A,
215 
216 	/** Select an alternate interface for the specified interface */
217 	LIBUSB_REQUEST_SET_INTERFACE = 0x0B,
218 
219 	/** Set then report an endpoint's synchronization frame */
220 	LIBUSB_REQUEST_SYNCH_FRAME = 0x0C
221 };
222 
223 /** \ingroup misc
224  * Request type bits of the
225  * \ref libusb_control_setup::bmRequestType "bmRequestType" field in control
226  * transfers. */
227 enum libusb_request_type {
228 	/** Standard */
229 	LIBUSB_REQUEST_TYPE_STANDARD = (0x00 << 5),
230 
231 	/** Class */
232 	LIBUSB_REQUEST_TYPE_CLASS = (0x01 << 5),
233 
234 	/** Vendor */
235 	LIBUSB_REQUEST_TYPE_VENDOR = (0x02 << 5),
236 
237 	/** Reserved */
238 	LIBUSB_REQUEST_TYPE_RESERVED = (0x03 << 5)
239 };
240 
241 /** \ingroup misc
242  * Recipient bits of the
243  * \ref libusb_control_setup::bmRequestType "bmRequestType" field in control
244  * transfers. Values 4 through 31 are reserved. */
245 enum libusb_request_recipient {
246 	/** Device */
247 	LIBUSB_RECIPIENT_DEVICE = 0x00,
248 
249 	/** Interface */
250 	LIBUSB_RECIPIENT_INTERFACE = 0x01,
251 
252 	/** Endpoint */
253 	LIBUSB_RECIPIENT_ENDPOINT = 0x02,
254 
255 	/** Other */
256 	LIBUSB_RECIPIENT_OTHER = 0x03
257 };
258 
259 #define LIBUSB_ISO_SYNC_TYPE_MASK		0x0C
260 
261 /** \ingroup desc
262  * Synchronization type for isochronous endpoints. Values for bits 2:3 of the
263  * \ref libusb_endpoint_descriptor::bmAttributes "bmAttributes" field in
264  * libusb_endpoint_descriptor.
265  */
266 enum libusb_iso_sync_type {
267 	/** No synchronization */
268 	LIBUSB_ISO_SYNC_TYPE_NONE = 0,
269 
270 	/** Asynchronous */
271 	LIBUSB_ISO_SYNC_TYPE_ASYNC = 1,
272 
273 	/** Adaptive */
274 	LIBUSB_ISO_SYNC_TYPE_ADAPTIVE = 2,
275 
276 	/** Synchronous */
277 	LIBUSB_ISO_SYNC_TYPE_SYNC = 3
278 };
279 
280 #define LIBUSB_ISO_USAGE_TYPE_MASK 0x30
281 
282 /** \ingroup desc
283  * Usage type for isochronous endpoints. Values for bits 4:5 of the
284  * \ref libusb_endpoint_descriptor::bmAttributes "bmAttributes" field in
285  * libusb_endpoint_descriptor.
286  */
287 enum libusb_iso_usage_type {
288 	/** Data endpoint */
289 	LIBUSB_ISO_USAGE_TYPE_DATA = 0,
290 
291 	/** Feedback endpoint */
292 	LIBUSB_ISO_USAGE_TYPE_FEEDBACK = 1,
293 
294 	/** Implicit feedback Data endpoint */
295 	LIBUSB_ISO_USAGE_TYPE_IMPLICIT = 2
296 };
297 
298 /** \ingroup desc
299  * A structure representing the standard USB device descriptor. This
300  * descriptor is documented in section 9.6.1 of the USB 2.0 specification.
301  * All multiple-byte fields are represented in host-endian format.
302  */
303 struct libusb_device_descriptor {
304 	/** Size of this descriptor (in bytes) */
305 	uint8_t  bLength;
306 
307 	/** Descriptor type. Will have value
308 	 * \ref libusb_descriptor_type::LIBUSB_DT_DEVICE LIBUSB_DT_DEVICE in this
309 	 * context. */
310 	uint8_t  bDescriptorType;
311 
312 	/** USB specification release number in binary-coded decimal. A value of
313 	 * 0x0200 indicates USB 2.0, 0x0110 indicates USB 1.1, etc. */
314 	uint16_t bcdUSB;
315 
316 	/** USB-IF class code for the device. See \ref libusb_class_code. */
317 	uint8_t  bDeviceClass;
318 
319 	/** USB-IF subclass code for the device, qualified by the bDeviceClass
320 	 * value */
321 	uint8_t  bDeviceSubClass;
322 
323 	/** USB-IF protocol code for the device, qualified by the bDeviceClass and
324 	 * bDeviceSubClass values */
325 	uint8_t  bDeviceProtocol;
326 
327 	/** Maximum packet size for endpoint 0 */
328 	uint8_t  bMaxPacketSize0;
329 
330 	/** USB-IF vendor ID */
331 	uint16_t idVendor;
332 
333 	/** USB-IF product ID */
334 	uint16_t idProduct;
335 
336 	/** Device release number in binary-coded decimal */
337 	uint16_t bcdDevice;
338 
339 	/** Index of string descriptor describing manufacturer */
340 	uint8_t  iManufacturer;
341 
342 	/** Index of string descriptor describing product */
343 	uint8_t  iProduct;
344 
345 	/** Index of string descriptor containing device serial number */
346 	uint8_t  iSerialNumber;
347 
348 	/** Number of possible configurations */
349 	uint8_t  bNumConfigurations;
350 };
351 
352 /** \ingroup desc
353  * A structure representing the standard USB endpoint descriptor. This
354  * descriptor is documented in section 9.6.3 of the USB 2.0 specification.
355  * All multiple-byte fields are represented in host-endian format.
356  */
357 struct libusb_endpoint_descriptor {
358 	/** Size of this descriptor (in bytes) */
359 	uint8_t  bLength;
360 
361 	/** Descriptor type. Will have value
362 	 * \ref libusb_descriptor_type::LIBUSB_DT_ENDPOINT LIBUSB_DT_ENDPOINT in
363 	 * this context. */
364 	uint8_t  bDescriptorType;
365 
366 	/** The address of the endpoint described by this descriptor. Bits 0:3 are
367 	 * the endpoint number. Bits 4:6 are reserved. Bit 7 indicates direction,
368 	 * see \ref libusb_endpoint_direction.
369 	 */
370 	uint8_t  bEndpointAddress;
371 
372 	/** Attributes which apply to the endpoint when it is configured using
373 	 * the bConfigurationValue. Bits 0:1 determine the transfer type and
374 	 * correspond to \ref libusb_transfer_type. Bits 2:3 are only used for
375 	 * isochronous endpoints and correspond to \ref libusb_iso_sync_type.
376 	 * Bits 4:5 are also only used for isochronous endpoints and correspond to
377 	 * \ref libusb_iso_usage_type. Bits 6:7 are reserved.
378 	 */
379 	uint8_t  bmAttributes;
380 
381 	/** Maximum packet size this endpoint is capable of sending/receiving. */
382 	uint16_t wMaxPacketSize;
383 
384 	/** Interval for polling endpoint for data transfers. */
385 	uint8_t  bInterval;
386 
387 	/** For audio devices only: the rate at which synchronization feedback
388 	 * is provided. */
389 	uint8_t  bRefresh;
390 
391 	/** For audio devices only: the address if the synch endpoint */
392 	uint8_t  bSynchAddress;
393 
394 	/** Extra descriptors. If libusb encounters unknown endpoint descriptors,
395 	 * it will store them here, should you wish to parse them. */
396 	const unsigned char *extra;
397 
398 	/** Length of the extra descriptors, in bytes. */
399 	int extra_length;
400 };
401 
402 /** \ingroup desc
403  * A structure representing the standard USB interface descriptor. This
404  * descriptor is documented in section 9.6.5 of the USB 2.0 specification.
405  * All multiple-byte fields are represented in host-endian format.
406  */
407 struct libusb_interface_descriptor {
408 	/** Size of this descriptor (in bytes) */
409 	uint8_t  bLength;
410 
411 	/** Descriptor type. Will have value
412 	 * \ref libusb_descriptor_type::LIBUSB_DT_INTERFACE LIBUSB_DT_INTERFACE
413 	 * in this context. */
414 	uint8_t  bDescriptorType;
415 
416 	/** Number of this interface */
417 	uint8_t  bInterfaceNumber;
418 
419 	/** Value used to select this alternate setting for this interface */
420 	uint8_t  bAlternateSetting;
421 
422 	/** Number of endpoints used by this interface (excluding the control
423 	 * endpoint). */
424 	uint8_t  bNumEndpoints;
425 
426 	/** USB-IF class code for this interface. See \ref libusb_class_code. */
427 	uint8_t  bInterfaceClass;
428 
429 	/** USB-IF subclass code for this interface, qualified by the
430 	 * bInterfaceClass value */
431 	uint8_t  bInterfaceSubClass;
432 
433 	/** USB-IF protocol code for this interface, qualified by the
434 	 * bInterfaceClass and bInterfaceSubClass values */
435 	uint8_t  bInterfaceProtocol;
436 
437 	/** Index of string descriptor describing this interface */
438 	uint8_t  iInterface;
439 
440 	/** Array of endpoint descriptors. This length of this array is determined
441 	 * by the bNumEndpoints field. */
442 	const struct libusb_endpoint_descriptor *endpoint;
443 
444 	/** Extra descriptors. If libusb encounters unknown interface descriptors,
445 	 * it will store them here, should you wish to parse them. */
446 	const unsigned char *extra;
447 
448 	/** Length of the extra descriptors, in bytes. */
449 	int extra_length;
450 };
451 
452 /** \ingroup desc
453  * A collection of alternate settings for a particular USB interface.
454  */
455 struct libusb_interface {
456 	/** Array of interface descriptors. The length of this array is determined
457 	 * by the num_altsetting field. */
458 	const struct libusb_interface_descriptor *altsetting;
459 
460 	/** The number of alternate settings that belong to this interface */
461 	int num_altsetting;
462 };
463 
464 /** \ingroup desc
465  * A structure representing the standard USB configuration descriptor. This
466  * descriptor is documented in section 9.6.3 of the USB 2.0 specification.
467  * All multiple-byte fields are represented in host-endian format.
468  */
469 struct libusb_config_descriptor {
470 	/** Size of this descriptor (in bytes) */
471 	uint8_t  bLength;
472 
473 	/** Descriptor type. Will have value
474 	 * \ref libusb_descriptor_type::LIBUSB_DT_CONFIG LIBUSB_DT_CONFIG
475 	 * in this context. */
476 	uint8_t  bDescriptorType;
477 
478 	/** Total length of data returned for this configuration */
479 	uint16_t wTotalLength;
480 
481 	/** Number of interfaces supported by this configuration */
482 	uint8_t  bNumInterfaces;
483 
484 	/** Identifier value for this configuration */
485 	uint8_t  bConfigurationValue;
486 
487 	/** Index of string descriptor describing this configuration */
488 	uint8_t  iConfiguration;
489 
490 	/** Configuration characteristics */
491 	uint8_t  bmAttributes;
492 
493 	/** Maximum power consumption of the USB device from this bus in this
494 	 * configuration when the device is fully opreation. Expressed in units
495 	 * of 2 mA. */
496 	uint8_t  MaxPower;
497 
498 	/** Array of interfaces supported by this configuration. The length of
499 	 * this array is determined by the bNumInterfaces field. */
500 	const struct libusb_interface *interface;
501 
502 	/** Extra descriptors. If libusb encounters unknown configuration
503 	 * descriptors, it will store them here, should you wish to parse them. */
504 	const unsigned char *extra;
505 
506 	/** Length of the extra descriptors, in bytes. */
507 	int extra_length;
508 };
509 
510 /** \ingroup asyncio
511  * Setup packet for control transfers. */
512 struct libusb_control_setup {
513 	/** Request type. Bits 0:4 determine recipient, see
514 	 * \ref libusb_request_recipient. Bits 5:6 determine type, see
515 	 * \ref libusb_request_type. Bit 7 determines data transfer direction, see
516 	 * \ref libusb_endpoint_direction.
517 	 */
518 	uint8_t  bmRequestType;
519 
520 	/** Request. If the type bits of bmRequestType are equal to
521 	 * \ref libusb_request_type::LIBUSB_REQUEST_TYPE_STANDARD
522 	 * "LIBUSB_REQUEST_TYPE_STANDARD" then this field refers to
523 	 * \ref libusb_standard_request. For other cases, use of this field is
524 	 * application-specific. */
525 	uint8_t  bRequest;
526 
527 	/** Value. Varies according to request */
528 	uint16_t wValue;
529 
530 	/** Index. Varies according to request, typically used to pass an index
531 	 * or offset */
532 	uint16_t wIndex;
533 
534 	/** Number of bytes to transfer */
535 	uint16_t wLength;
536 };
537 
538 #define LIBUSB_CONTROL_SETUP_SIZE (sizeof(struct libusb_control_setup))
539 
540 /* libusb */
541 
542 struct libusb_context;
543 struct libusb_device;
544 struct libusb_device_handle;
545 
546 /** \ingroup lib
547  * Structure representing a libusb session. The concept of individual libusb
548  * sessions allows for your program to use two libraries (or dynamically
549  * load two modules) which both independently use libusb. This will prevent
550  * interference between the individual libusb users - for example
551  * libusb_set_debug() will not affect the other user of the library, and
552  * libusb_exit() will not destroy resources that the other user is still
553  * using.
554  *
555  * Sessions are created by libusb_init() and destroyed through libusb_exit().
556  * If your application is guaranteed to only ever include a single libusb
557  * user (i.e. you), you do not have to worry about contexts: pass NULL in
558  * every function call where a context is required. The default context
559  * will be used.
560  *
561  * For more information, see \ref contexts.
562  */
563 typedef struct libusb_context libusb_context;
564 
565 /** \ingroup dev
566  * Structure representing a USB device detected on the system. This is an
567  * opaque type for which you are only ever provided with a pointer, usually
568  * originating from libusb_get_device_list().
569  *
570  * Certain operations can be performed on a device, but in order to do any
571  * I/O you will have to first obtain a device handle using libusb_open().
572  *
573  * Devices are reference counted with libusb_device_ref() and
574  * libusb_device_unref(), and are freed when the reference count reaches 0.
575  * New devices presented by libusb_get_device_list() have a reference count of
576  * 1, and libusb_free_device_list() can optionally decrease the reference count
577  * on all devices in the list. libusb_open() adds another reference which is
578  * later destroyed by libusb_close().
579  */
580 typedef struct libusb_device libusb_device;
581 
582 
583 /** \ingroup dev
584  * Structure representing a handle on a USB device. This is an opaque type for
585  * which you are only ever provided with a pointer, usually originating from
586  * libusb_open().
587  *
588  * A device handle is used to perform I/O and other operations. When finished
589  * with a device handle, you should call libusb_close().
590  */
591 typedef struct libusb_device_handle libusb_device_handle;
592 
593 /** \ingroup misc
594  * Error codes. Most libusb functions return 0 on success or one of these
595  * codes on failure.
596  */
597 enum libusb_error {
598 	/** Success (no error) */
599 	LIBUSB_SUCCESS = 0,
600 
601 	/** Input/output error */
602 	LIBUSB_ERROR_IO = -1,
603 
604 	/** Invalid parameter */
605 	LIBUSB_ERROR_INVALID_PARAM = -2,
606 
607 	/** Access denied (insufficient permissions) */
608 	LIBUSB_ERROR_ACCESS = -3,
609 
610 	/** No such device (it may have been disconnected) */
611 	LIBUSB_ERROR_NO_DEVICE = -4,
612 
613 	/** Entity not found */
614 	LIBUSB_ERROR_NOT_FOUND = -5,
615 
616 	/** Resource busy */
617 	LIBUSB_ERROR_BUSY = -6,
618 
619 	/** Operation timed out */
620 	LIBUSB_ERROR_TIMEOUT = -7,
621 
622 	/** Overflow */
623 	LIBUSB_ERROR_OVERFLOW = -8,
624 
625 	/** Pipe error */
626 	LIBUSB_ERROR_PIPE = -9,
627 
628 	/** System call interrupted (perhaps due to signal) */
629 	LIBUSB_ERROR_INTERRUPTED = -10,
630 
631 	/** Insufficient memory */
632 	LIBUSB_ERROR_NO_MEM = -11,
633 
634 	/** Operation not supported or unimplemented on this platform */
635 	LIBUSB_ERROR_NOT_SUPPORTED = -12,
636 
637 	/** Other error */
638 	LIBUSB_ERROR_OTHER = -99
639 };
640 
641 /** \ingroup asyncio
642  * Transfer status codes */
643 enum libusb_transfer_status {
644 	/** Transfer completed without error. Note that this does not indicate
645 	 * that the entire amount of requested data was transferred. */
646 	LIBUSB_TRANSFER_COMPLETED,
647 
648 	/** Transfer failed */
649 	LIBUSB_TRANSFER_ERROR,
650 
651 	/** Transfer timed out */
652 	LIBUSB_TRANSFER_TIMED_OUT,
653 
654 	/** Transfer was cancelled */
655 	LIBUSB_TRANSFER_CANCELLED,
656 
657 	/** For bulk/interrupt endpoints: halt condition detected (endpoint
658 	 * stalled). For control endpoints: control request not supported. */
659 	LIBUSB_TRANSFER_STALL,
660 
661 	/** Device was disconnected */
662 	LIBUSB_TRANSFER_NO_DEVICE,
663 
664 	/** Device sent more data than requested */
665 	LIBUSB_TRANSFER_OVERFLOW
666 };
667 
668 /** \ingroup asyncio
669  * libusb_transfer.flags values */
670 enum libusb_transfer_flags {
671 	/** Report short frames as errors */
672 	LIBUSB_TRANSFER_SHORT_NOT_OK = 1<<0,
673 
674 	/** Automatically free() transfer buffer during libusb_free_transfer() */
675 	LIBUSB_TRANSFER_FREE_BUFFER = 1<<1,
676 
677 	/** Automatically call libusb_free_transfer() after callback returns.
678 	 * If this flag is set, it is illegal to call libusb_free_transfer()
679 	 * from your transfer callback, as this will result in a double-free
680 	 * when this flag is acted upon. */
681 	LIBUSB_TRANSFER_FREE_TRANSFER = 1<<2
682 };
683 
684 /** \ingroup asyncio
685  * Isochronous packet descriptor. */
686 struct libusb_iso_packet_descriptor {
687 	/** Length of data to request in this packet */
688 	unsigned int length;
689 
690 	/** Amount of data that was actually transferred */
691 	unsigned int actual_length;
692 
693 	/** Status code for this packet */
694 	enum libusb_transfer_status status;
695 };
696 
697 struct libusb_transfer;
698 
699 /** \ingroup asyncio
700  * Asynchronous transfer callback function type. When submitting asynchronous
701  * transfers, you pass a pointer to a callback function of this type via the
702  * \ref libusb_transfer::callback "callback" member of the libusb_transfer
703  * structure. libusb will call this function later, when the transfer has
704  * completed or failed. See \ref asyncio for more information.
705  * \param transfer The libusb_transfer struct the callback function is being
706  * notified about.
707  */
708 typedef void (*libusb_transfer_cb_fn)(struct libusb_transfer *transfer);
709 
710 /** \ingroup asyncio
711  * The generic USB transfer structure. The user populates this structure and
712  * then submits it in order to request a transfer. After the transfer has
713  * completed, the library populates the transfer with the results and passes
714  * it back to the user.
715  */
716 struct libusb_transfer {
717 	/** Handle of the device that this transfer will be submitted to */
718 	libusb_device_handle *dev_handle;
719 
720 	/** A bitwise OR combination of \ref libusb_transfer_flags. */
721 	uint8_t flags;
722 
723 	/** Address of the endpoint where this transfer will be sent. */
724 	unsigned char endpoint;
725 
726 	/** Type of the endpoint from \ref libusb_transfer_type */
727 	unsigned char type;
728 
729 	/** Timeout for this transfer in millseconds. A value of 0 indicates no
730 	 * timeout. */
731 	unsigned int timeout;
732 
733 	/** The status of the transfer. Read-only, and only for use within
734 	 * transfer callback function.
735 	 *
736 	 * If this is an isochronous transfer, this field may read COMPLETED even
737 	 * if there were errors in the frames. Use the
738 	 * \ref libusb_iso_packet_descriptor::status "status" field in each packet
739 	 * to determine if errors occurred. */
740 	enum libusb_transfer_status status;
741 
742 	/** Length of the data buffer */
743 	int length;
744 
745 	/** Actual length of data that was transferred. Read-only, and only for
746 	 * use within transfer callback function. Not valid for isochronous
747 	 * endpoint transfers. */
748 	int actual_length;
749 
750 	/** Callback function. This will be invoked when the transfer completes,
751 	 * fails, or is cancelled. */
752 	libusb_transfer_cb_fn callback;
753 
754 	/** User context data to pass to the callback function. */
755 	void *user_data;
756 
757 	/** Data buffer */
758 	unsigned char *buffer;
759 
760 	/** Number of isochronous packets. Only used for I/O with isochronous
761 	 * endpoints. */
762 	int num_iso_packets;
763 
764 	/** Isochronous packet descriptors, for isochronous transfers only. */
765 	struct libusb_iso_packet_descriptor iso_packet_desc
766 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
767 	[] /* valid C99 code */
768 #else
769 	[0] /* non-standard, but usually working code */
770 #endif
771 	;
772 };
773 
774 int libusb_init(libusb_context **ctx);
775 void libusb_exit(libusb_context *ctx);
776 void libusb_set_debug(libusb_context *ctx, int level);
777 
778 ssize_t libusb_get_device_list(libusb_context *ctx,
779 	libusb_device ***list);
780 void libusb_free_device_list(libusb_device **list, int unref_devices);
781 libusb_device *libusb_ref_device(libusb_device *dev);
782 void libusb_unref_device(libusb_device *dev);
783 
784 int libusb_get_configuration(libusb_device_handle *dev, int *config);
785 int libusb_get_device_descriptor(libusb_device *dev,
786 	struct libusb_device_descriptor *desc);
787 int libusb_get_active_config_descriptor(libusb_device *dev,
788 	struct libusb_config_descriptor **config);
789 int libusb_get_config_descriptor(libusb_device *dev, uint8_t config_index,
790 	struct libusb_config_descriptor **config);
791 int libusb_get_config_descriptor_by_value(libusb_device *dev,
792 	uint8_t bConfigurationValue, struct libusb_config_descriptor **config);
793 void libusb_free_config_descriptor(struct libusb_config_descriptor *config);
794 uint8_t libusb_get_bus_number(libusb_device *dev);
795 uint8_t libusb_get_device_address(libusb_device *dev);
796 int libusb_get_max_packet_size(libusb_device *dev, unsigned char endpoint);
797 int libusb_get_max_iso_packet_size(libusb_device *dev, unsigned char endpoint);
798 
799 int libusb_open(libusb_device *dev, libusb_device_handle **handle);
800 void libusb_close(libusb_device_handle *dev_handle);
801 libusb_device *libusb_get_device(libusb_device_handle *dev_handle);
802 
803 int libusb_set_configuration(libusb_device_handle *dev, int configuration);
804 int libusb_claim_interface(libusb_device_handle *dev, int iface);
805 int libusb_release_interface(libusb_device_handle *dev, int iface);
806 
807 libusb_device_handle *libusb_open_device_with_vid_pid(libusb_context *ctx,
808 	uint16_t vendor_id, uint16_t product_id);
809 
810 int libusb_set_interface_alt_setting(libusb_device_handle *dev,
811 	int interface_number, int alternate_setting);
812 int libusb_clear_halt(libusb_device_handle *dev, unsigned char endpoint);
813 int libusb_reset_device(libusb_device_handle *dev);
814 
815 int libusb_kernel_driver_active(libusb_device_handle *dev, int interface);
816 int libusb_detach_kernel_driver(libusb_device_handle *dev, int interface);
817 int libusb_attach_kernel_driver(libusb_device_handle *dev, int interface);
818 
819 /* async I/O */
820 
821 /** \ingroup asyncio
822  * Get the data section of a control transfer. This convenience function is here
823  * to remind you that the data does not start until 8 bytes into the actual
824  * buffer, as the setup packet comes first.
825  *
826  * Calling this function only makes sense from a transfer callback function,
827  * or situations where you have already allocated a suitably sized buffer at
828  * transfer->buffer.
829  *
830  * \param transfer a transfer
831  * \returns pointer to the first byte of the data section
832  */
libusb_control_transfer_get_data(struct libusb_transfer * transfer)833 static inline unsigned char *libusb_control_transfer_get_data(
834 	struct libusb_transfer *transfer)
835 {
836 	return transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE;
837 }
838 
839 /** \ingroup asyncio
840  * Get the control setup packet of a control transfer. This convenience
841  * function is here to remind you that the control setup occupies the first
842  * 8 bytes of the transfer data buffer.
843  *
844  * Calling this function only makes sense from a transfer callback function,
845  * or situations where you have already allocated a suitably sized buffer at
846  * transfer->buffer.
847  *
848  * \param transfer a transfer
849  * \returns a casted pointer to the start of the transfer data buffer
850  */
libusb_control_transfer_get_setup(struct libusb_transfer * transfer)851 static inline struct libusb_control_setup *libusb_control_transfer_get_setup(
852 	struct libusb_transfer *transfer)
853 {
854 	return (struct libusb_control_setup *) transfer->buffer;
855 }
856 
857 /** \ingroup asyncio
858  * Helper function to populate the setup packet (first 8 bytes of the data
859  * buffer) for a control transfer. The wIndex, wValue and wLength values should
860  * be given in host-endian byte order.
861  *
862  * \param buffer buffer to output the setup packet into
863  * \param bmRequestType see the
864  * \ref libusb_control_setup::bmRequestType "bmRequestType" field of
865  * \ref libusb_control_setup
866  * \param bRequest see the
867  * \ref libusb_control_setup::bRequest "bRequest" field of
868  * \ref libusb_control_setup
869  * \param wValue see the
870  * \ref libusb_control_setup::wValue "wValue" field of
871  * \ref libusb_control_setup
872  * \param wIndex see the
873  * \ref libusb_control_setup::wIndex "wIndex" field of
874  * \ref libusb_control_setup
875  * \param wLength see the
876  * \ref libusb_control_setup::wLength "wLength" field of
877  * \ref libusb_control_setup
878  */
libusb_fill_control_setup(unsigned char * buffer,uint8_t bmRequestType,uint8_t bRequest,uint16_t wValue,uint16_t wIndex,uint16_t wLength)879 static inline void libusb_fill_control_setup(unsigned char *buffer,
880 	uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
881 	uint16_t wLength)
882 {
883 	struct libusb_control_setup *setup = (struct libusb_control_setup *) buffer;
884 	setup->bmRequestType = bmRequestType;
885 	setup->bRequest = bRequest;
886 	setup->wValue = libusb_cpu_to_le16(wValue);
887 	setup->wIndex = libusb_cpu_to_le16(wIndex);
888 	setup->wLength = libusb_cpu_to_le16(wLength);
889 }
890 
891 struct libusb_transfer *libusb_alloc_transfer(int iso_packets);
892 int libusb_submit_transfer(struct libusb_transfer *transfer);
893 int libusb_cancel_transfer(struct libusb_transfer *transfer);
894 void libusb_free_transfer(struct libusb_transfer *transfer);
895 
896 /** \ingroup asyncio
897  * Helper function to populate the required \ref libusb_transfer fields
898  * for a control transfer.
899  *
900  * If you pass a transfer buffer to this function, the first 8 bytes will
901  * be interpreted as a control setup packet, and the wLength field will be
902  * used to automatically populate the \ref libusb_transfer::length "length"
903  * field of the transfer. Therefore the recommended approach is:
904  * -# Allocate a suitably sized data buffer (including space for control setup)
905  * -# Call libusb_fill_control_setup()
906  * -# If this is a host-to-device transfer with a data stage, put the data
907  *    in place after the setup packet
908  * -# Call this function
909  * -# Call libusb_submit_transfer()
910  *
911  * It is also legal to pass a NULL buffer to this function, in which case this
912  * function will not attempt to populate the length field. Remember that you
913  * must then populate the buffer and length fields later.
914  *
915  * \param transfer the transfer to populate
916  * \param dev_handle handle of the device that will handle the transfer
917  * \param buffer data buffer. If provided, this function will interpret the
918  * first 8 bytes as a setup packet and infer the transfer length from that.
919  * \param callback callback function to be invoked on transfer completion
920  * \param user_data user data to pass to callback function
921  * \param timeout timeout for the transfer in milliseconds
922  */
libusb_fill_control_transfer(struct libusb_transfer * transfer,libusb_device_handle * dev_handle,unsigned char * buffer,libusb_transfer_cb_fn callback,void * user_data,unsigned int timeout)923 static inline void libusb_fill_control_transfer(
924 	struct libusb_transfer *transfer, libusb_device_handle *dev_handle,
925 	unsigned char *buffer, libusb_transfer_cb_fn callback, void *user_data,
926 	unsigned int timeout)
927 {
928 	struct libusb_control_setup *setup = (struct libusb_control_setup *) buffer;
929 	transfer->dev_handle = dev_handle;
930 	transfer->endpoint = 0;
931 	transfer->type = LIBUSB_TRANSFER_TYPE_CONTROL;
932 	transfer->timeout = timeout;
933 	transfer->buffer = buffer;
934 	if (setup)
935 		transfer->length = LIBUSB_CONTROL_SETUP_SIZE
936 			+ libusb_le16_to_cpu(setup->wLength);
937 	transfer->user_data = user_data;
938 	transfer->callback = callback;
939 }
940 
941 /** \ingroup asyncio
942  * Helper function to populate the required \ref libusb_transfer fields
943  * for a bulk transfer.
944  *
945  * \param transfer the transfer to populate
946  * \param dev_handle handle of the device that will handle the transfer
947  * \param endpoint address of the endpoint where this transfer will be sent
948  * \param buffer data buffer
949  * \param length length of data buffer
950  * \param callback callback function to be invoked on transfer completion
951  * \param user_data user data to pass to callback function
952  * \param timeout timeout for the transfer in milliseconds
953  */
libusb_fill_bulk_transfer(struct libusb_transfer * transfer,libusb_device_handle * dev_handle,unsigned char endpoint,unsigned char * buffer,int length,libusb_transfer_cb_fn callback,void * user_data,unsigned int timeout)954 static inline void libusb_fill_bulk_transfer(struct libusb_transfer *transfer,
955 	libusb_device_handle *dev_handle, unsigned char endpoint,
956 	unsigned char *buffer, int length, libusb_transfer_cb_fn callback,
957 	void *user_data, unsigned int timeout)
958 {
959 	transfer->dev_handle = dev_handle;
960 	transfer->endpoint = endpoint;
961 	transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
962 	transfer->timeout = timeout;
963 	transfer->buffer = buffer;
964 	transfer->length = length;
965 	transfer->user_data = user_data;
966 	transfer->callback = callback;
967 }
968 
969 /** \ingroup asyncio
970  * Helper function to populate the required \ref libusb_transfer fields
971  * for an interrupt transfer.
972  *
973  * \param transfer the transfer to populate
974  * \param dev_handle handle of the device that will handle the transfer
975  * \param endpoint address of the endpoint where this transfer will be sent
976  * \param buffer data buffer
977  * \param length length of data buffer
978  * \param callback callback function to be invoked on transfer completion
979  * \param user_data user data to pass to callback function
980  * \param timeout timeout for the transfer in milliseconds
981  */
libusb_fill_interrupt_transfer(struct libusb_transfer * transfer,libusb_device_handle * dev_handle,unsigned char endpoint,unsigned char * buffer,int length,libusb_transfer_cb_fn callback,void * user_data,unsigned int timeout)982 static inline void libusb_fill_interrupt_transfer(
983 	struct libusb_transfer *transfer, libusb_device_handle *dev_handle,
984 	unsigned char endpoint, unsigned char *buffer, int length,
985 	libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
986 {
987 	transfer->dev_handle = dev_handle;
988 	transfer->endpoint = endpoint;
989 	transfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT;
990 	transfer->timeout = timeout;
991 	transfer->buffer = buffer;
992 	transfer->length = length;
993 	transfer->user_data = user_data;
994 	transfer->callback = callback;
995 }
996 
997 /** \ingroup asyncio
998  * Helper function to populate the required \ref libusb_transfer fields
999  * for an isochronous transfer.
1000  *
1001  * \param transfer the transfer to populate
1002  * \param dev_handle handle of the device that will handle the transfer
1003  * \param endpoint address of the endpoint where this transfer will be sent
1004  * \param buffer data buffer
1005  * \param length length of data buffer
1006  * \param num_iso_packets the number of isochronous packets
1007  * \param callback callback function to be invoked on transfer completion
1008  * \param user_data user data to pass to callback function
1009  * \param timeout timeout for the transfer in milliseconds
1010  */
libusb_fill_iso_transfer(struct libusb_transfer * transfer,libusb_device_handle * dev_handle,unsigned char endpoint,unsigned char * buffer,int length,int num_iso_packets,libusb_transfer_cb_fn callback,void * user_data,unsigned int timeout)1011 static inline void libusb_fill_iso_transfer(struct libusb_transfer *transfer,
1012 	libusb_device_handle *dev_handle, unsigned char endpoint,
1013 	unsigned char *buffer, int length, int num_iso_packets,
1014 	libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
1015 {
1016 	transfer->dev_handle = dev_handle;
1017 	transfer->endpoint = endpoint;
1018 	transfer->type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS;
1019 	transfer->timeout = timeout;
1020 	transfer->buffer = buffer;
1021 	transfer->length = length;
1022 	transfer->num_iso_packets = num_iso_packets;
1023 	transfer->user_data = user_data;
1024 	transfer->callback = callback;
1025 }
1026 
1027 /** \ingroup asyncio
1028  * Convenience function to set the length of all packets in an isochronous
1029  * transfer, based on the num_iso_packets field in the transfer structure.
1030  *
1031  * \param transfer a transfer
1032  * \param length the length to set in each isochronous packet descriptor
1033  * \see libusb_get_max_packet_size()
1034  */
libusb_set_iso_packet_lengths(struct libusb_transfer * transfer,unsigned int length)1035 static inline void libusb_set_iso_packet_lengths(
1036 	struct libusb_transfer *transfer, unsigned int length)
1037 {
1038 	int i;
1039 	for (i = 0; i < transfer->num_iso_packets; i++)
1040 		transfer->iso_packet_desc[i].length = length;
1041 }
1042 
1043 /** \ingroup asyncio
1044  * Convenience function to locate the position of an isochronous packet
1045  * within the buffer of an isochronous transfer.
1046  *
1047  * This is a thorough function which loops through all preceding packets,
1048  * accumulating their lengths to find the position of the specified packet.
1049  * Typically you will assign equal lengths to each packet in the transfer,
1050  * and hence the above method is sub-optimal. You may wish to use
1051  * libusb_get_iso_packet_buffer_simple() instead.
1052  *
1053  * \param transfer a transfer
1054  * \param packet the packet to return the address of
1055  * \returns the base address of the packet buffer inside the transfer buffer,
1056  * or NULL if the packet does not exist.
1057  * \see libusb_get_iso_packet_buffer_simple()
1058  */
libusb_get_iso_packet_buffer(struct libusb_transfer * transfer,unsigned int packet)1059 static inline unsigned char *libusb_get_iso_packet_buffer(
1060 	struct libusb_transfer *transfer, unsigned int packet)
1061 {
1062 	int i;
1063 	size_t offset = 0;
1064 	int _packet;
1065 
1066 	/* oops..slight bug in the API. packet is an unsigned int, but we use
1067 	 * signed integers almost everywhere else. range-check and convert to
1068 	 * signed to avoid compiler warnings. FIXME for libusb-2. */
1069 	if (packet > INT_MAX)
1070 		return NULL;
1071 	_packet = packet;
1072 
1073 	if (_packet >= transfer->num_iso_packets)
1074 		return NULL;
1075 
1076 	for (i = 0; i < _packet; i++)
1077 		offset += transfer->iso_packet_desc[i].length;
1078 
1079 	return transfer->buffer + offset;
1080 }
1081 
1082 /** \ingroup asyncio
1083  * Convenience function to locate the position of an isochronous packet
1084  * within the buffer of an isochronous transfer, for transfers where each
1085  * packet is of identical size.
1086  *
1087  * This function relies on the assumption that every packet within the transfer
1088  * is of identical size to the first packet. Calculating the location of
1089  * the packet buffer is then just a simple calculation:
1090  * <tt>buffer + (packet_size * packet)</tt>
1091  *
1092  * Do not use this function on transfers other than those that have identical
1093  * packet lengths for each packet.
1094  *
1095  * \param transfer a transfer
1096  * \param packet the packet to return the address of
1097  * \returns the base address of the packet buffer inside the transfer buffer,
1098  * or NULL if the packet does not exist.
1099  * \see libusb_get_iso_packet_buffer()
1100  */
libusb_get_iso_packet_buffer_simple(struct libusb_transfer * transfer,unsigned int packet)1101 static inline unsigned char *libusb_get_iso_packet_buffer_simple(
1102 	struct libusb_transfer *transfer, unsigned int packet)
1103 {
1104 	int _packet;
1105 
1106 	/* oops..slight bug in the API. packet is an unsigned int, but we use
1107 	 * signed integers almost everywhere else. range-check and convert to
1108 	 * signed to avoid compiler warnings. FIXME for libusb-2. */
1109 	if (packet > INT_MAX)
1110 		return NULL;
1111 	_packet = packet;
1112 
1113 	if (_packet >= transfer->num_iso_packets)
1114 		return NULL;
1115 
1116 	return transfer->buffer + (transfer->iso_packet_desc[0].length * _packet);
1117 }
1118 
1119 /* sync I/O */
1120 
1121 int libusb_control_transfer(libusb_device_handle *dev_handle,
1122 	uint8_t request_type, uint8_t request, uint16_t value, uint16_t index,
1123 	unsigned char *data, uint16_t length, unsigned int timeout);
1124 
1125 int libusb_bulk_transfer(libusb_device_handle *dev_handle,
1126 	unsigned char endpoint, unsigned char *data, int length,
1127 	int *actual_length, unsigned int timeout);
1128 
1129 int libusb_interrupt_transfer(libusb_device_handle *dev_handle,
1130 	unsigned char endpoint, unsigned char *data, int length,
1131 	int *actual_length, unsigned int timeout);
1132 
1133 /** \ingroup desc
1134  * Retrieve a descriptor from the default control pipe.
1135  * This is a convenience function which formulates the appropriate control
1136  * message to retrieve the descriptor.
1137  *
1138  * \param dev a device handle
1139  * \param desc_type the descriptor type, see \ref libusb_descriptor_type
1140  * \param desc_index the index of the descriptor to retrieve
1141  * \param data output buffer for descriptor
1142  * \param length size of data buffer
1143  * \returns number of bytes returned in data, or LIBUSB_ERROR code on failure
1144  */
libusb_get_descriptor(libusb_device_handle * dev,uint8_t desc_type,uint8_t desc_index,unsigned char * data,int length)1145 static inline int libusb_get_descriptor(libusb_device_handle *dev,
1146 	uint8_t desc_type, uint8_t desc_index, unsigned char *data, int length)
1147 {
1148 	return libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN,
1149 		LIBUSB_REQUEST_GET_DESCRIPTOR, (desc_type << 8) | desc_index, 0, data,
1150 		length, 1000);
1151 }
1152 
1153 /** \ingroup desc
1154  * Retrieve a descriptor from a device.
1155  * This is a convenience function which formulates the appropriate control
1156  * message to retrieve the descriptor. The string returned is Unicode, as
1157  * detailed in the USB specifications.
1158  *
1159  * \param dev a device handle
1160  * \param desc_index the index of the descriptor to retrieve
1161  * \param langid the language ID for the string descriptor
1162  * \param data output buffer for descriptor
1163  * \param length size of data buffer
1164  * \returns number of bytes returned in data, or LIBUSB_ERROR code on failure
1165  * \see libusb_get_string_descriptor_ascii()
1166  */
libusb_get_string_descriptor(libusb_device_handle * dev,uint8_t desc_index,uint16_t langid,unsigned char * data,int length)1167 static inline int libusb_get_string_descriptor(libusb_device_handle *dev,
1168 	uint8_t desc_index, uint16_t langid, unsigned char *data, int length)
1169 {
1170 	return libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN,
1171 		LIBUSB_REQUEST_GET_DESCRIPTOR, (LIBUSB_DT_STRING << 8) | desc_index,
1172 		langid, data, length, 1000);
1173 }
1174 
1175 int libusb_get_string_descriptor_ascii(libusb_device_handle *dev,
1176 	uint8_t index, unsigned char *data, int length);
1177 
1178 /* polling and timeouts */
1179 
1180 int libusb_try_lock_events(libusb_context *ctx);
1181 void libusb_lock_events(libusb_context *ctx);
1182 void libusb_unlock_events(libusb_context *ctx);
1183 int libusb_event_handling_ok(libusb_context *ctx);
1184 int libusb_event_handler_active(libusb_context *ctx);
1185 void libusb_lock_event_waiters(libusb_context *ctx);
1186 void libusb_unlock_event_waiters(libusb_context *ctx);
1187 int libusb_wait_for_event(libusb_context *ctx, struct timeval *tv);
1188 
1189 int libusb_handle_events_timeout(libusb_context *ctx, struct timeval *tv);
1190 int libusb_handle_events(libusb_context *ctx);
1191 int libusb_handle_events_locked(libusb_context *ctx, struct timeval *tv);
1192 int libusb_pollfds_handle_timeouts(libusb_context *ctx);
1193 int libusb_get_next_timeout(libusb_context *ctx, struct timeval *tv);
1194 
1195 /** \ingroup poll
1196  * File descriptor for polling
1197  */
1198 struct libusb_pollfd {
1199 	/** Numeric file descriptor */
1200 	int fd;
1201 
1202 	/** Event flags to poll for from <poll.h>. POLLIN indicates that you
1203 	 * should monitor this file descriptor for becoming ready to read from,
1204 	 * and POLLOUT indicates that you should monitor this file descriptor for
1205 	 * nonblocking write readiness. */
1206 	short events;
1207 };
1208 
1209 /** \ingroup poll
1210  * Callback function, invoked when a new file descriptor should be added
1211  * to the set of file descriptors monitored for events.
1212  * \param fd the new file descriptor
1213  * \param events events to monitor for, see \ref libusb_pollfd for a
1214  * description
1215  * \param user_data User data pointer specified in
1216  * libusb_set_pollfd_notifiers() call
1217  * \see libusb_set_pollfd_notifiers()
1218  */
1219 typedef void (*libusb_pollfd_added_cb)(int fd, short events, void *user_data);
1220 
1221 /** \ingroup poll
1222  * Callback function, invoked when a file descriptor should be removed from
1223  * the set of file descriptors being monitored for events. After returning
1224  * from this callback, do not use that file descriptor again.
1225  * \param fd the file descriptor to stop monitoring
1226  * \param user_data User data pointer specified in
1227  * libusb_set_pollfd_notifiers() call
1228  * \see libusb_set_pollfd_notifiers()
1229  */
1230 typedef void (*libusb_pollfd_removed_cb)(int fd, void *user_data);
1231 
1232 const struct libusb_pollfd **libusb_get_pollfds(libusb_context *ctx);
1233 void libusb_set_pollfd_notifiers(libusb_context *ctx,
1234 	libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb,
1235 	void *user_data);
1236 
1237 #ifdef __cplusplus
1238 }
1239 #endif
1240 
1241 #endif
1242