1 /* 2 * Public libusbx header file 3 * Copyright 2001 Johannes Erdfelt <johannes (at) erdfelt.com> 4 * Copyright 2007-2008 Daniel Drake <dsd (at) gentoo.org> 5 * Copyright 2012 Pete Batard <pete (at) akeo.ie> 6 * Copyright 2012 Nathan Hjelm <hjelmn (at) cs.unm.edu> 7 * For more information, please visit: http://libusbx.org 8 * 9 * This library is free software; you can redistribute it and/or 10 * modify it under the terms of the GNU Lesser General Public 11 * License as published by the Free Software Foundation; either 12 * version 2.1 of the License, or (at your option) any later version. 13 * 14 * This library is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 * Lesser General Public License for more details. 18 * 19 * You should have received a copy of the GNU Lesser General Public 20 * License along with this library; if not, write to the Free Software 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 22 */ 23 24 #ifndef LIBUSB_H 25 #define LIBUSB_H 26 27 #ifdef _MSC_VER 28 // Disable warning 4200 for [0]. 29 #pragma warning(push) 30 #pragma warning(disable: 4200) 31 #endif 32 33 #ifdef _MSC_VER 34 /* on MS environments, the inline keyword is available in C++ only */ 35 #if !defined(__cplusplus) 36 #define inline __inline 37 #endif 38 /* ssize_t is also not available (copy/paste from MinGW) */ 39 #ifndef _SSIZE_T_DEFINED 40 #define _SSIZE_T_DEFINED 41 #undef ssize_t 42 #ifdef _WIN64 43 typedef __int64 ssize_t; 44 #else 45 typedef int ssize_t; 46 #endif /* _WIN64 */ 47 #endif /* _SSIZE_T_DEFINED */ 48 #endif /* _MSC_VER */ 49 50 /* stdint.h is not available on older MSVC */ 51 #if defined(_MSC_VER) && (_MSC_VER < 1600) && (!defined(_STDINT)) && (!defined(_STDINT_H)) 52 typedef unsigned __int8 uint8_t; 53 typedef unsigned __int16 uint16_t; 54 typedef unsigned __int32 uint32_t; 55 #else 56 #include <stdint.h> 57 #endif 58 59 #if !defined(_WIN32_WCE) 60 #include <sys/types.h> 61 #endif 62 63 #if defined(__linux) || defined(__APPLE__) || defined(__CYGWIN__) 64 #include <sys/time.h> 65 #endif 66 67 #include <time.h> 68 #include <limits.h> 69 70 /* 'interface' might be defined as a macro on Windows, so we need to 71 * undefine it so as not to break the current libusbx API, because 72 * libusb_config_descriptor has an 'interface' member 73 * As this can be problematic if you include windows.h after libusb.h 74 * in your sources, we force windows.h to be included first. */ 75 #if defined(_WIN32) || defined(__CYGWIN__) || defined(_WIN32_WCE) 76 #include <windows.h> 77 #if defined(interface) 78 #undef interface 79 #endif 80 #if !defined(__CYGWIN__) 81 #include <winsock.h> 82 #endif 83 #endif 84 85 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5) 86 #define LIBUSB_DEPRECATED_FOR(f) \ 87 __attribute__((deprecated("Use " #f " instead"))) 88 #else 89 #define LIBUSB_DEPRECATED_FOR(f) 90 #endif /* __GNUC__ */ 91 92 /** \def LIBUSB_CALL 93 * \ingroup misc 94 * libusbx's Windows calling convention. 95 * 96 * Under Windows, the selection of available compilers and configurations 97 * means that, unlike other platforms, there is not <em>one true calling 98 * convention</em> (calling convention: the manner in which parameters are 99 * passed to funcions in the generated assembly code). 100 * 101 * Matching the Windows API itself, libusbx uses the WINAPI convention (which 102 * translates to the <tt>stdcall</tt> convention) and guarantees that the 103 * library is compiled in this way. The public header file also includes 104 * appropriate annotations so that your own software will use the right 105 * convention, even if another convention is being used by default within 106 * your codebase. 107 * 108 * The one consideration that you must apply in your software is to mark 109 * all functions which you use as libusbx callbacks with this LIBUSB_CALL 110 * annotation, so that they too get compiled for the correct calling 111 * convention. 112 * 113 * On non-Windows operating systems, this macro is defined as nothing. This 114 * means that you can apply it to your code without worrying about 115 * cross-platform compatibility. 116 */ 117 /* LIBUSB_CALL must be defined on both definition and declaration of libusbx 118 * functions. You'd think that declaration would be enough, but cygwin will 119 * complain about conflicting types unless both are marked this way. 120 * The placement of this macro is important too; it must appear after the 121 * return type, before the function name. See internal documentation for 122 * API_EXPORTED. 123 */ 124 #if defined(_WIN32) || defined(__CYGWIN__) || defined(_WIN32_WCE) 125 #define LIBUSB_CALL WINAPI 126 #else 127 #define LIBUSB_CALL 128 #endif 129 130 /** \def LIBUSBX_API_VERSION 131 * \ingroup misc 132 * libusbx's API version. 133 * 134 * Since version 1.0.13, to help with feature detection, libusbx defines 135 * a LIBUSBX_API_VERSION macro that gets increased every time there is a 136 * significant change to the API, such as the introduction of a new call, 137 * the definition of a new macro/enum member, or any other element that 138 * libusbx applications may want to detect at compilation time. 139 * 140 * The macro is typically used in an application as follows: 141 * \code 142 * #if defined(LIBUSBX_API_VERSION) && (LIBUSBX_API_VERSION >= 0x01001234) 143 * // Use one of the newer features from the libusbx API 144 * #endif 145 * \endcode 146 * 147 * Another feature of LIBUSBX_API_VERSION is that it can be used to detect 148 * whether you are compiling against the libusb or the libusbx library. 149 * 150 * Internally, LIBUSBX_API_VERSION is defined as follows: 151 * (libusbx major << 24) | (libusbx minor << 16) | (16 bit incremental) 152 */ 153 #define LIBUSBX_API_VERSION 0x01000102 154 155 #ifdef __cplusplus 156 extern "C" { 157 #endif 158 159 /** \fn libusb_cpu_to_le16 160 * \ingroup misc 161 * Convert a 16-bit value from host-endian to little-endian format. On 162 * little endian systems, this function does nothing. On big endian systems, 163 * the bytes are swapped. 164 * \param x the host-endian value to convert 165 * \returns the value in little-endian byte order 166 */ 167 static inline uint16_t libusb_cpu_to_le16(const uint16_t x) 168 { 169 union { 170 uint8_t b8[2]; 171 uint16_t b16; 172 } _tmp; 173 _tmp.b8[1] = (uint8_t) (x >> 8); 174 _tmp.b8[0] = (uint8_t) (x & 0xff); 175 return _tmp.b16; 176 } 177 178 /** \def libusb_le16_to_cpu 179 * \ingroup misc 180 * Convert a 16-bit value from little-endian to host-endian format. On 181 * little endian systems, this function does nothing. On big endian systems, 182 * the bytes are swapped. 183 * \param x the little-endian value to convert 184 * \returns the value in host-endian byte order 185 */ 186 #define libusb_le16_to_cpu libusb_cpu_to_le16 187 188 /* standard USB stuff */ 189 190 /** \ingroup desc 191 * Device and/or Interface Class codes */ 192 enum libusb_class_code { 193 /** In the context of a \ref libusb_device_descriptor "device descriptor", 194 * this bDeviceClass value indicates that each interface specifies its 195 * own class information and all interfaces operate independently. 196 */ 197 LIBUSB_CLASS_PER_INTERFACE = 0, 198 199 /** Audio class */ 200 LIBUSB_CLASS_AUDIO = 1, 201 202 /** Communications class */ 203 LIBUSB_CLASS_COMM = 2, 204 205 /** Human Interface Device class */ 206 LIBUSB_CLASS_HID = 3, 207 208 /** Physical */ 209 LIBUSB_CLASS_PHYSICAL = 5, 210 211 /** Printer class */ 212 LIBUSB_CLASS_PRINTER = 7, 213 214 /** Image class */ 215 LIBUSB_CLASS_PTP = 6, /* legacy name from libusb-0.1 usb.h */ 216 LIBUSB_CLASS_IMAGE = 6, 217 218 /** Mass storage class */ 219 LIBUSB_CLASS_MASS_STORAGE = 8, 220 221 /** Hub class */ 222 LIBUSB_CLASS_HUB = 9, 223 224 /** Data class */ 225 LIBUSB_CLASS_DATA = 10, 226 227 /** Smart Card */ 228 LIBUSB_CLASS_SMART_CARD = 0x0b, 229 230 /** Content Security */ 231 LIBUSB_CLASS_CONTENT_SECURITY = 0x0d, 232 233 /** Video */ 234 LIBUSB_CLASS_VIDEO = 0x0e, 235 236 /** Personal Healthcare */ 237 LIBUSB_CLASS_PERSONAL_HEALTHCARE = 0x0f, 238 239 /** Diagnostic Device */ 240 LIBUSB_CLASS_DIAGNOSTIC_DEVICE = 0xdc, 241 242 /** Wireless class */ 243 LIBUSB_CLASS_WIRELESS = 0xe0, 244 245 /** Application class */ 246 LIBUSB_CLASS_APPLICATION = 0xfe, 247 248 /** Class is vendor-specific */ 249 LIBUSB_CLASS_VENDOR_SPEC = 0xff 250 }; 251 252 /** \ingroup desc 253 * Descriptor types as defined by the USB specification. */ 254 enum libusb_descriptor_type { 255 /** Device descriptor. See libusb_device_descriptor. */ 256 LIBUSB_DT_DEVICE = 0x01, 257 258 /** Configuration descriptor. See libusb_config_descriptor. */ 259 LIBUSB_DT_CONFIG = 0x02, 260 261 /** String descriptor */ 262 LIBUSB_DT_STRING = 0x03, 263 264 /** Interface descriptor. See libusb_interface_descriptor. */ 265 LIBUSB_DT_INTERFACE = 0x04, 266 267 /** Endpoint descriptor. See libusb_endpoint_descriptor. */ 268 LIBUSB_DT_ENDPOINT = 0x05, 269 270 /** BOS descriptor */ 271 LIBUSB_DT_BOS = 0x0f, 272 273 /** Device Capability descriptor */ 274 LIBUSB_DT_DEVICE_CAPABILITY = 0x10, 275 276 /** HID descriptor */ 277 LIBUSB_DT_HID = 0x21, 278 279 /** HID report descriptor */ 280 LIBUSB_DT_REPORT = 0x22, 281 282 /** Physical descriptor */ 283 LIBUSB_DT_PHYSICAL = 0x23, 284 285 /** Hub descriptor */ 286 LIBUSB_DT_HUB = 0x29, 287 288 /** SuperSpeed Hub descriptor */ 289 LIBUSB_DT_SUPERSPEED_HUB = 0x2a, 290 291 /** SuperSpeed Endpoint Companion descriptor */ 292 LIBUSB_DT_SS_ENDPOINT_COMPANION = 0x30 293 }; 294 295 /* Descriptor sizes per descriptor type */ 296 #define LIBUSB_DT_DEVICE_SIZE 18 297 #define LIBUSB_DT_CONFIG_SIZE 9 298 #define LIBUSB_DT_INTERFACE_SIZE 9 299 #define LIBUSB_DT_ENDPOINT_SIZE 7 300 #define LIBUSB_DT_ENDPOINT_AUDIO_SIZE 9 /* Audio extension */ 301 #define LIBUSB_DT_HUB_NONVAR_SIZE 7 302 #define LIBUSB_DT_SS_ENDPOINT_COMPANION_SIZE 6 303 #define LIBUSB_DT_BOS_SIZE 5 304 #define LIBUSB_DT_DEVICE_CAPABILITY_SIZE 3 305 306 /* BOS descriptor sizes */ 307 #define LIBUSB_BT_USB_2_0_EXTENSION_SIZE 7 308 #define LIBUSB_BT_SS_USB_DEVICE_CAPABILITY_SIZE 10 309 #define LIBUSB_BT_CONTAINER_ID_SIZE 20 310 311 /* We unwrap the BOS => define its max size */ 312 #define LIBUSB_DT_BOS_MAX_SIZE ((LIBUSB_DT_BOS_SIZE) +\ 313 (LIBUSB_BT_USB_2_0_EXTENSION_SIZE) +\ 314 (LIBUSB_BT_SS_USB_DEVICE_CAPABILITY_SIZE) +\ 315 (LIBUSB_BT_CONTAINER_ID_SIZE)) 316 317 #define LIBUSB_ENDPOINT_ADDRESS_MASK 0x0f /* in bEndpointAddress */ 318 #define LIBUSB_ENDPOINT_DIR_MASK 0x80 319 320 /** \ingroup desc 321 * Endpoint direction. Values for bit 7 of the 322 * \ref libusb_endpoint_descriptor::bEndpointAddress "endpoint address" scheme. 323 */ 324 enum libusb_endpoint_direction { 325 /** In: device-to-host */ 326 LIBUSB_ENDPOINT_IN = 0x80, 327 328 /** Out: host-to-device */ 329 LIBUSB_ENDPOINT_OUT = 0x00 330 }; 331 332 #define LIBUSB_TRANSFER_TYPE_MASK 0x03 /* in bmAttributes */ 333 334 /** \ingroup desc 335 * Endpoint transfer type. Values for bits 0:1 of the 336 * \ref libusb_endpoint_descriptor::bmAttributes "endpoint attributes" field. 337 */ 338 enum libusb_transfer_type { 339 /** Control endpoint */ 340 LIBUSB_TRANSFER_TYPE_CONTROL = 0, 341 342 /** Isochronous endpoint */ 343 LIBUSB_TRANSFER_TYPE_ISOCHRONOUS = 1, 344 345 /** Bulk endpoint */ 346 LIBUSB_TRANSFER_TYPE_BULK = 2, 347 348 /** Interrupt endpoint */ 349 LIBUSB_TRANSFER_TYPE_INTERRUPT = 3 350 }; 351 352 /** \ingroup misc 353 * Standard requests, as defined in table 9-5 of the USB 3.0 specifications */ 354 enum libusb_standard_request { 355 /** Request status of the specific recipient */ 356 LIBUSB_REQUEST_GET_STATUS = 0x00, 357 358 /** Clear or disable a specific feature */ 359 LIBUSB_REQUEST_CLEAR_FEATURE = 0x01, 360 361 /* 0x02 is reserved */ 362 363 /** Set or enable a specific feature */ 364 LIBUSB_REQUEST_SET_FEATURE = 0x03, 365 366 /* 0x04 is reserved */ 367 368 /** Set device address for all future accesses */ 369 LIBUSB_REQUEST_SET_ADDRESS = 0x05, 370 371 /** Get the specified descriptor */ 372 LIBUSB_REQUEST_GET_DESCRIPTOR = 0x06, 373 374 /** Used to update existing descriptors or add new descriptors */ 375 LIBUSB_REQUEST_SET_DESCRIPTOR = 0x07, 376 377 /** Get the current device configuration value */ 378 LIBUSB_REQUEST_GET_CONFIGURATION = 0x08, 379 380 /** Set device configuration */ 381 LIBUSB_REQUEST_SET_CONFIGURATION = 0x09, 382 383 /** Return the selected alternate setting for the specified interface */ 384 LIBUSB_REQUEST_GET_INTERFACE = 0x0A, 385 386 /** Select an alternate interface for the specified interface */ 387 LIBUSB_REQUEST_SET_INTERFACE = 0x0B, 388 389 /** Set then report an endpoint's synchronization frame */ 390 LIBUSB_REQUEST_SYNCH_FRAME = 0x0C, 391 392 /** Sets both the U1 and U2 Exit Latency */ 393 LIBUSB_REQUEST_SET_SEL = 0x30, 394 395 /** Delay from the time a host transmits a packet to the time it is 396 * received by the device. */ 397 LIBUSB_SET_ISOCH_DELAY = 0x31, 398 }; 399 400 /** \ingroup misc 401 * Request type bits of the 402 * \ref libusb_control_setup::bmRequestType "bmRequestType" field in control 403 * transfers. */ 404 enum libusb_request_type { 405 /** Standard */ 406 LIBUSB_REQUEST_TYPE_STANDARD = (0x00 << 5), 407 408 /** Class */ 409 LIBUSB_REQUEST_TYPE_CLASS = (0x01 << 5), 410 411 /** Vendor */ 412 LIBUSB_REQUEST_TYPE_VENDOR = (0x02 << 5), 413 414 /** Reserved */ 415 LIBUSB_REQUEST_TYPE_RESERVED = (0x03 << 5) 416 }; 417 418 /** \ingroup misc 419 * Recipient bits of the 420 * \ref libusb_control_setup::bmRequestType "bmRequestType" field in control 421 * transfers. Values 4 through 31 are reserved. */ 422 enum libusb_request_recipient { 423 /** Device */ 424 LIBUSB_RECIPIENT_DEVICE = 0x00, 425 426 /** Interface */ 427 LIBUSB_RECIPIENT_INTERFACE = 0x01, 428 429 /** Endpoint */ 430 LIBUSB_RECIPIENT_ENDPOINT = 0x02, 431 432 /** Other */ 433 LIBUSB_RECIPIENT_OTHER = 0x03, 434 }; 435 436 #define LIBUSB_ISO_SYNC_TYPE_MASK 0x0C 437 438 /** \ingroup desc 439 * Synchronization type for isochronous endpoints. Values for bits 2:3 of the 440 * \ref libusb_endpoint_descriptor::bmAttributes "bmAttributes" field in 441 * libusb_endpoint_descriptor. 442 */ 443 enum libusb_iso_sync_type { 444 /** No synchronization */ 445 LIBUSB_ISO_SYNC_TYPE_NONE = 0, 446 447 /** Asynchronous */ 448 LIBUSB_ISO_SYNC_TYPE_ASYNC = 1, 449 450 /** Adaptive */ 451 LIBUSB_ISO_SYNC_TYPE_ADAPTIVE = 2, 452 453 /** Synchronous */ 454 LIBUSB_ISO_SYNC_TYPE_SYNC = 3 455 }; 456 457 #define LIBUSB_ISO_USAGE_TYPE_MASK 0x30 458 459 /** \ingroup desc 460 * Usage type for isochronous endpoints. Values for bits 4:5 of the 461 * \ref libusb_endpoint_descriptor::bmAttributes "bmAttributes" field in 462 * libusb_endpoint_descriptor. 463 */ 464 enum libusb_iso_usage_type { 465 /** Data endpoint */ 466 LIBUSB_ISO_USAGE_TYPE_DATA = 0, 467 468 /** Feedback endpoint */ 469 LIBUSB_ISO_USAGE_TYPE_FEEDBACK = 1, 470 471 /** Implicit feedback Data endpoint */ 472 LIBUSB_ISO_USAGE_TYPE_IMPLICIT = 2, 473 }; 474 475 /** \ingroup desc 476 * A structure representing the standard USB device descriptor. This 477 * descriptor is documented in section 9.6.1 of the USB 3.0 specification. 478 * All multiple-byte fields are represented in host-endian format. 479 */ 480 struct libusb_device_descriptor { 481 /** Size of this descriptor (in bytes) */ 482 uint8_t bLength; 483 484 /** Descriptor type. Will have value 485 * \ref libusb_descriptor_type::LIBUSB_DT_DEVICE LIBUSB_DT_DEVICE in this 486 * context. */ 487 uint8_t bDescriptorType; 488 489 /** USB specification release number in binary-coded decimal. A value of 490 * 0x0200 indicates USB 2.0, 0x0110 indicates USB 1.1, etc. */ 491 uint16_t bcdUSB; 492 493 /** USB-IF class code for the device. See \ref libusb_class_code. */ 494 uint8_t bDeviceClass; 495 496 /** USB-IF subclass code for the device, qualified by the bDeviceClass 497 * value */ 498 uint8_t bDeviceSubClass; 499 500 /** USB-IF protocol code for the device, qualified by the bDeviceClass and 501 * bDeviceSubClass values */ 502 uint8_t bDeviceProtocol; 503 504 /** Maximum packet size for endpoint 0 */ 505 uint8_t bMaxPacketSize0; 506 507 /** USB-IF vendor ID */ 508 uint16_t idVendor; 509 510 /** USB-IF product ID */ 511 uint16_t idProduct; 512 513 /** Device release number in binary-coded decimal */ 514 uint16_t bcdDevice; 515 516 /** Index of string descriptor describing manufacturer */ 517 uint8_t iManufacturer; 518 519 /** Index of string descriptor describing product */ 520 uint8_t iProduct; 521 522 /** Index of string descriptor containing device serial number */ 523 uint8_t iSerialNumber; 524 525 /** Number of possible configurations */ 526 uint8_t bNumConfigurations; 527 }; 528 529 /** \ingroup desc 530 * A structure representing the standard USB endpoint descriptor. This 531 * descriptor is documented in section 9.6.6 of the USB 3.0 specification. 532 * All multiple-byte fields are represented in host-endian format. 533 */ 534 struct libusb_endpoint_descriptor { 535 /** Size of this descriptor (in bytes) */ 536 uint8_t bLength; 537 538 /** Descriptor type. Will have value 539 * \ref libusb_descriptor_type::LIBUSB_DT_ENDPOINT LIBUSB_DT_ENDPOINT in 540 * this context. */ 541 uint8_t bDescriptorType; 542 543 /** The address of the endpoint described by this descriptor. Bits 0:3 are 544 * the endpoint number. Bits 4:6 are reserved. Bit 7 indicates direction, 545 * see \ref libusb_endpoint_direction. 546 */ 547 uint8_t bEndpointAddress; 548 549 /** Attributes which apply to the endpoint when it is configured using 550 * the bConfigurationValue. Bits 0:1 determine the transfer type and 551 * correspond to \ref libusb_transfer_type. Bits 2:3 are only used for 552 * isochronous endpoints and correspond to \ref libusb_iso_sync_type. 553 * Bits 4:5 are also only used for isochronous endpoints and correspond to 554 * \ref libusb_iso_usage_type. Bits 6:7 are reserved. 555 */ 556 uint8_t bmAttributes; 557 558 /** Maximum packet size this endpoint is capable of sending/receiving. */ 559 uint16_t wMaxPacketSize; 560 561 /** Interval for polling endpoint for data transfers. */ 562 uint8_t bInterval; 563 564 /** For audio devices only: the rate at which synchronization feedback 565 * is provided. */ 566 uint8_t bRefresh; 567 568 /** For audio devices only: the address if the synch endpoint */ 569 uint8_t bSynchAddress; 570 571 /** Extra descriptors. If libusbx encounters unknown endpoint descriptors, 572 * it will store them here, should you wish to parse them. */ 573 const unsigned char *extra; 574 575 /** Length of the extra descriptors, in bytes. */ 576 int extra_length; 577 }; 578 579 /** \ingroup desc 580 * A structure representing the standard USB interface descriptor. This 581 * descriptor is documented in section 9.6.5 of the USB 3.0 specification. 582 * All multiple-byte fields are represented in host-endian format. 583 */ 584 struct libusb_interface_descriptor { 585 /** Size of this descriptor (in bytes) */ 586 uint8_t bLength; 587 588 /** Descriptor type. Will have value 589 * \ref libusb_descriptor_type::LIBUSB_DT_INTERFACE LIBUSB_DT_INTERFACE 590 * in this context. */ 591 uint8_t bDescriptorType; 592 593 /** Number of this interface */ 594 uint8_t bInterfaceNumber; 595 596 /** Value used to select this alternate setting for this interface */ 597 uint8_t bAlternateSetting; 598 599 /** Number of endpoints used by this interface (excluding the control 600 * endpoint). */ 601 uint8_t bNumEndpoints; 602 603 /** USB-IF class code for this interface. See \ref libusb_class_code. */ 604 uint8_t bInterfaceClass; 605 606 /** USB-IF subclass code for this interface, qualified by the 607 * bInterfaceClass value */ 608 uint8_t bInterfaceSubClass; 609 610 /** USB-IF protocol code for this interface, qualified by the 611 * bInterfaceClass and bInterfaceSubClass values */ 612 uint8_t bInterfaceProtocol; 613 614 /** Index of string descriptor describing this interface */ 615 uint8_t iInterface; 616 617 /** Array of endpoint descriptors. This length of this array is determined 618 * by the bNumEndpoints field. */ 619 const struct libusb_endpoint_descriptor *endpoint; 620 621 /** Extra descriptors. If libusbx encounters unknown interface descriptors, 622 * it will store them here, should you wish to parse them. */ 623 const unsigned char *extra; 624 625 /** Length of the extra descriptors, in bytes. */ 626 int extra_length; 627 }; 628 629 /** \ingroup desc 630 * A collection of alternate settings for a particular USB interface. 631 */ 632 struct libusb_interface { 633 /** Array of interface descriptors. The length of this array is determined 634 * by the num_altsetting field. */ 635 const struct libusb_interface_descriptor *altsetting; 636 637 /** The number of alternate settings that belong to this interface */ 638 int num_altsetting; 639 }; 640 641 /** \ingroup desc 642 * A structure representing the standard USB configuration descriptor. This 643 * descriptor is documented in section 9.6.3 of the USB 3.0 specification. 644 * All multiple-byte fields are represented in host-endian format. 645 */ 646 struct libusb_config_descriptor { 647 /** Size of this descriptor (in bytes) */ 648 uint8_t bLength; 649 650 /** Descriptor type. Will have value 651 * \ref libusb_descriptor_type::LIBUSB_DT_CONFIG LIBUSB_DT_CONFIG 652 * in this context. */ 653 uint8_t bDescriptorType; 654 655 /** Total length of data returned for this configuration */ 656 uint16_t wTotalLength; 657 658 /** Number of interfaces supported by this configuration */ 659 uint8_t bNumInterfaces; 660 661 /** Identifier value for this configuration */ 662 uint8_t bConfigurationValue; 663 664 /** Index of string descriptor describing this configuration */ 665 uint8_t iConfiguration; 666 667 /** Configuration characteristics */ 668 uint8_t bmAttributes; 669 670 /** Maximum power consumption of the USB device from this bus in this 671 * configuration when the device is fully opreation. Expressed in units 672 * of 2 mA. */ 673 uint8_t MaxPower; 674 675 /** Array of interfaces supported by this configuration. The length of 676 * this array is determined by the bNumInterfaces field. */ 677 const struct libusb_interface *interface; 678 679 /** Extra descriptors. If libusbx encounters unknown configuration 680 * descriptors, it will store them here, should you wish to parse them. */ 681 const unsigned char *extra; 682 683 /** Length of the extra descriptors, in bytes. */ 684 int extra_length; 685 }; 686 687 /** \ingroup desc 688 * A structure representing the superspeed endpoint companion 689 * descriptor. This descriptor is documented in section 9.6.7 of 690 * the USB 3.0 specification. All multiple-byte fields are represented in 691 * host-endian format. 692 */ 693 struct libusb_ss_endpoint_companion_descriptor { 694 695 /** Size of this descriptor (in bytes) */ 696 uint8_t bLength; 697 698 /** Descriptor type. Will have value 699 * \ref libusb_descriptor_type::LIBUSB_DT_SS_ENDPOINT_COMPANION in 700 * this context. */ 701 uint8_t bDescriptorType; 702 703 704 /** The maximum number of packets the endpoint can send or 705 * recieve as part of a burst. */ 706 uint8_t bMaxBurst; 707 708 /** In bulk EP: bits 4:0 represents the maximum number of 709 * streams the EP supports. In isochronous EP: bits 1:0 710 * represents the Mult - a zero based value that determines 711 * the maximum number of packets within a service interval */ 712 uint8_t bmAttributes; 713 714 /** The total number of bytes this EP will transfer every 715 * service interval. valid only for periodic EPs. */ 716 uint16_t wBytesPerInterval; 717 }; 718 719 /** \ingroup desc 720 * A generic representation of a BOS Device Capability descriptor. It is 721 * advised to check bDevCapabilityType and call the matching 722 * libusb_get_*_descriptor function to get a structure fully matching the type. 723 */ 724 struct libusb_bos_dev_capability_descriptor { 725 /** Size of this descriptor (in bytes) */ 726 uint8_t bLength; 727 /** Descriptor type. Will have value 728 * \ref libusb_descriptor_type::LIBUSB_DT_DEVICE_CAPABILITY 729 * LIBUSB_DT_DEVICE_CAPABILITY in this context. */ 730 uint8_t bDescriptorType; 731 /** Device Capability type */ 732 uint8_t bDevCapabilityType; 733 /** Device Capability data (bLength - 3 bytes) */ 734 uint8_t dev_capability_data 735 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) 736 [] /* valid C99 code */ 737 #else 738 [0] /* non-standard, but usually working code */ 739 #endif 740 ; 741 }; 742 743 /** \ingroup desc 744 * A structure representing the Binary Device Object Store (BOS) descriptor. 745 * This descriptor is documented in section 9.6.2 of the USB 3.0 specification. 746 * All multiple-byte fields are represented in host-endian format. 747 */ 748 struct libusb_bos_descriptor { 749 /** Size of this descriptor (in bytes) */ 750 uint8_t bLength; 751 752 /** Descriptor type. Will have value 753 * \ref libusb_descriptor_type::LIBUSB_DT_BOS LIBUSB_DT_BOS 754 * in this context. */ 755 uint8_t bDescriptorType; 756 757 /** Length of this descriptor and all of its sub descriptors */ 758 uint16_t wTotalLength; 759 760 /** The number of separate device capability descriptors in 761 * the BOS */ 762 uint8_t bNumDeviceCaps; 763 764 /** bNumDeviceCap Device Capability Descriptors */ 765 struct libusb_bos_dev_capability_descriptor *dev_capability 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 /** \ingroup desc 775 * A structure representing the USB 2.0 Extension descriptor 776 * This descriptor is documented in section 9.6.2.1 of the USB 3.0 specification. 777 * All multiple-byte fields are represented in host-endian format. 778 */ 779 struct libusb_usb_2_0_extension_descriptor { 780 /** Size of this descriptor (in bytes) */ 781 uint8_t bLength; 782 783 /** Descriptor type. Will have value 784 * \ref libusb_descriptor_type::LIBUSB_DT_DEVICE_CAPABILITY 785 * LIBUSB_DT_DEVICE_CAPABILITY in this context. */ 786 uint8_t bDescriptorType; 787 788 /** Capability type. Will have value 789 * \ref libusb_capability_type::LIBUSB_BT_USB_2_0_EXTENSION 790 * LIBUSB_BT_USB_2_0_EXTENSION in this context. */ 791 uint8_t bDevCapabilityType; 792 793 /** Bitmap encoding of supported device level features. 794 * A value of one in a bit location indicates a feature is 795 * supported; a value of zero indicates it is not supported. 796 * See \ref libusb_usb_2_0_extension_attributes. */ 797 uint32_t bmAttributes; 798 }; 799 800 /** \ingroup desc 801 * A structure representing the SuperSpeed USB Device Capability descriptor 802 * This descriptor is documented in section 9.6.2.2 of the USB 3.0 specification. 803 * All multiple-byte fields are represented in host-endian format. 804 */ 805 struct libusb_ss_usb_device_capability_descriptor { 806 /** Size of this descriptor (in bytes) */ 807 uint8_t bLength; 808 809 /** Descriptor type. Will have value 810 * \ref libusb_descriptor_type::LIBUSB_DT_DEVICE_CAPABILITY 811 * LIBUSB_DT_DEVICE_CAPABILITY in this context. */ 812 uint8_t bDescriptorType; 813 814 /** Capability type. Will have value 815 * \ref libusb_capability_type::LIBUSB_BT_SS_USB_DEVICE_CAPABILITY 816 * LIBUSB_BT_SS_USB_DEVICE_CAPABILITY in this context. */ 817 uint8_t bDevCapabilityType; 818 819 /** Bitmap encoding of supported device level features. 820 * A value of one in a bit location indicates a feature is 821 * supported; a value of zero indicates it is not supported. 822 * See \ref libusb_ss_usb_device_capability_attributes. */ 823 uint8_t bmAttributes; 824 825 /** Bitmap encoding of the speed supported by this device when 826 * operating in SuperSpeed mode. See \ref libusb_supported_speed. */ 827 uint16_t wSpeedSupported; 828 829 /** The lowest speed at which all the functionality supported 830 * by the device is available to the user. For example if the 831 * device supports all its functionality when connected at 832 * full speed and above then it sets this value to 1. */ 833 uint8_t bFunctionalitySupport; 834 835 /** U1 Device Exit Latency. */ 836 uint8_t bU1DevExitLat; 837 838 /** U2 Device Exit Latency. */ 839 uint16_t bU2DevExitLat; 840 }; 841 842 /** \ingroup desc 843 * A structure representing the Container ID descriptor. 844 * This descriptor is documented in section 9.6.2.3 of the USB 3.0 specification. 845 * All multiple-byte fields, except UUIDs, are represented in host-endian format. 846 */ 847 struct libusb_container_id_descriptor { 848 /** Size of this descriptor (in bytes) */ 849 uint8_t bLength; 850 851 /** Descriptor type. Will have value 852 * \ref libusb_descriptor_type::LIBUSB_DT_DEVICE_CAPABILITY 853 * LIBUSB_DT_DEVICE_CAPABILITY in this context. */ 854 uint8_t bDescriptorType; 855 856 /** Capability type. Will have value 857 * \ref libusb_capability_type::LIBUSB_BT_CONTAINER_ID 858 * LIBUSB_BT_CONTAINER_ID in this context. */ 859 uint8_t bDevCapabilityType; 860 861 /** Reserved field */ 862 uint8_t bReserved; 863 864 /** 128 bit UUID */ 865 uint8_t ContainerID[16]; 866 }; 867 868 /** \ingroup asyncio 869 * Setup packet for control transfers. */ 870 struct libusb_control_setup { 871 /** Request type. Bits 0:4 determine recipient, see 872 * \ref libusb_request_recipient. Bits 5:6 determine type, see 873 * \ref libusb_request_type. Bit 7 determines data transfer direction, see 874 * \ref libusb_endpoint_direction. 875 */ 876 uint8_t bmRequestType; 877 878 /** Request. If the type bits of bmRequestType are equal to 879 * \ref libusb_request_type::LIBUSB_REQUEST_TYPE_STANDARD 880 * "LIBUSB_REQUEST_TYPE_STANDARD" then this field refers to 881 * \ref libusb_standard_request. For other cases, use of this field is 882 * application-specific. */ 883 uint8_t bRequest; 884 885 /** Value. Varies according to request */ 886 uint16_t wValue; 887 888 /** Index. Varies according to request, typically used to pass an index 889 * or offset */ 890 uint16_t wIndex; 891 892 /** Number of bytes to transfer */ 893 uint16_t wLength; 894 }; 895 896 #define LIBUSB_CONTROL_SETUP_SIZE (sizeof(struct libusb_control_setup)) 897 898 /* libusbx */ 899 900 struct libusb_context; 901 struct libusb_device; 902 struct libusb_device_handle; 903 struct libusb_hotplug_callback; 904 905 /** \ingroup lib 906 * Structure providing the version of the libusbx runtime 907 */ 908 struct libusb_version { 909 /** Library major version. */ 910 const uint16_t major; 911 912 /** Library minor version. */ 913 const uint16_t minor; 914 915 /** Library micro version. */ 916 const uint16_t micro; 917 918 /** Library nano version. */ 919 const uint16_t nano; 920 921 /** Library release candidate suffix string, e.g. "-rc4". */ 922 const char *rc; 923 924 /** For ABI compatibility only. */ 925 const char* describe; 926 }; 927 928 /** \ingroup lib 929 * Structure representing a libusbx session. The concept of individual libusbx 930 * sessions allows for your program to use two libraries (or dynamically 931 * load two modules) which both independently use libusb. This will prevent 932 * interference between the individual libusbx users - for example 933 * libusb_set_debug() will not affect the other user of the library, and 934 * libusb_exit() will not destroy resources that the other user is still 935 * using. 936 * 937 * Sessions are created by libusb_init() and destroyed through libusb_exit(). 938 * If your application is guaranteed to only ever include a single libusbx 939 * user (i.e. you), you do not have to worry about contexts: pass NULL in 940 * every function call where a context is required. The default context 941 * will be used. 942 * 943 * For more information, see \ref contexts. 944 */ 945 typedef struct libusb_context libusb_context; 946 947 /** \ingroup dev 948 * Structure representing a USB device detected on the system. This is an 949 * opaque type for which you are only ever provided with a pointer, usually 950 * originating from libusb_get_device_list(). 951 * 952 * Certain operations can be performed on a device, but in order to do any 953 * I/O you will have to first obtain a device handle using libusb_open(). 954 * 955 * Devices are reference counted with libusb_ref_device() and 956 * libusb_unref_device(), and are freed when the reference count reaches 0. 957 * New devices presented by libusb_get_device_list() have a reference count of 958 * 1, and libusb_free_device_list() can optionally decrease the reference count 959 * on all devices in the list. libusb_open() adds another reference which is 960 * later destroyed by libusb_close(). 961 */ 962 typedef struct libusb_device libusb_device; 963 964 965 /** \ingroup dev 966 * Structure representing a handle on a USB device. This is an opaque type for 967 * which you are only ever provided with a pointer, usually originating from 968 * libusb_open(). 969 * 970 * A device handle is used to perform I/O and other operations. When finished 971 * with a device handle, you should call libusb_close(). 972 */ 973 typedef struct libusb_device_handle libusb_device_handle; 974 975 /** \ingroup dev 976 * Speed codes. Indicates the speed at which the device is operating. 977 */ 978 enum libusb_speed { 979 /** The OS doesn't report or know the device speed. */ 980 LIBUSB_SPEED_UNKNOWN = 0, 981 982 /** The device is operating at low speed (1.5MBit/s). */ 983 LIBUSB_SPEED_LOW = 1, 984 985 /** The device is operating at full speed (12MBit/s). */ 986 LIBUSB_SPEED_FULL = 2, 987 988 /** The device is operating at high speed (480MBit/s). */ 989 LIBUSB_SPEED_HIGH = 3, 990 991 /** The device is operating at super speed (5000MBit/s). */ 992 LIBUSB_SPEED_SUPER = 4, 993 }; 994 995 /** \ingroup dev 996 * Supported speeds (wSpeedSupported) bitfield. Indicates what 997 * speeds the device supports. 998 */ 999 enum libusb_supported_speed { 1000 /** Low speed operation supported (1.5MBit/s). */ 1001 LIBUSB_LOW_SPEED_OPERATION = 1, 1002 1003 /** Full speed operation supported (12MBit/s). */ 1004 LIBUSB_FULL_SPEED_OPERATION = 2, 1005 1006 /** High speed operation supported (480MBit/s). */ 1007 LIBUSB_HIGH_SPEED_OPERATION = 4, 1008 1009 /** Superspeed operation supported (5000MBit/s). */ 1010 LIBUSB_SUPER_SPEED_OPERATION = 8, 1011 }; 1012 1013 /** \ingroup dev 1014 * Masks for the bits of the 1015 * \ref libusb_usb_2_0_extension_descriptor::bmAttributes "bmAttributes" field 1016 * of the USB 2.0 Extension descriptor. 1017 */ 1018 enum libusb_usb_2_0_extension_attributes { 1019 /** Supports Link Power Management (LPM) */ 1020 LIBUSB_BM_LPM_SUPPORT = 2, 1021 }; 1022 1023 /** \ingroup dev 1024 * Masks for the bits of the 1025 * \ref libusb_ss_usb_device_capability_descriptor::bmAttributes "bmAttributes" field 1026 * field of the SuperSpeed USB Device Capability descriptor. 1027 */ 1028 enum libusb_ss_usb_device_capability_attributes { 1029 /** Supports Latency Tolerance Messages (LTM) */ 1030 LIBUSB_BM_LTM_SUPPORT = 2, 1031 }; 1032 1033 /** \ingroup dev 1034 * USB capability types 1035 */ 1036 enum libusb_bos_type { 1037 /** Wireless USB device capability */ 1038 LIBUSB_BT_WIRELESS_USB_DEVICE_CAPABILITY = 1, 1039 1040 /** USB 2.0 extensions */ 1041 LIBUSB_BT_USB_2_0_EXTENSION = 2, 1042 1043 /** SuperSpeed USB device capability */ 1044 LIBUSB_BT_SS_USB_DEVICE_CAPABILITY = 3, 1045 1046 /** Container ID type */ 1047 LIBUSB_BT_CONTAINER_ID = 4, 1048 }; 1049 1050 /** \ingroup misc 1051 * Error codes. Most libusbx functions return 0 on success or one of these 1052 * codes on failure. 1053 * You can call libusb_error_name() to retrieve a string representation of an 1054 * error code or libusb_strerror() to get an end-user suitable description of 1055 * an error code. 1056 */ 1057 enum libusb_error { 1058 /** Success (no error) */ 1059 LIBUSB_SUCCESS = 0, 1060 1061 /** Input/output error */ 1062 LIBUSB_ERROR_IO = -1, 1063 1064 /** Invalid parameter */ 1065 LIBUSB_ERROR_INVALID_PARAM = -2, 1066 1067 /** Access denied (insufficient permissions) */ 1068 LIBUSB_ERROR_ACCESS = -3, 1069 1070 /** No such device (it may have been disconnected) */ 1071 LIBUSB_ERROR_NO_DEVICE = -4, 1072 1073 /** Entity not found */ 1074 LIBUSB_ERROR_NOT_FOUND = -5, 1075 1076 /** Resource busy */ 1077 LIBUSB_ERROR_BUSY = -6, 1078 1079 /** Operation timed out */ 1080 LIBUSB_ERROR_TIMEOUT = -7, 1081 1082 /** Overflow */ 1083 LIBUSB_ERROR_OVERFLOW = -8, 1084 1085 /** Pipe error */ 1086 LIBUSB_ERROR_PIPE = -9, 1087 1088 /** System call interrupted (perhaps due to signal) */ 1089 LIBUSB_ERROR_INTERRUPTED = -10, 1090 1091 /** Insufficient memory */ 1092 LIBUSB_ERROR_NO_MEM = -11, 1093 1094 /** Operation not supported or unimplemented on this platform */ 1095 LIBUSB_ERROR_NOT_SUPPORTED = -12, 1096 1097 /* NB: Remember to update LIBUSB_ERROR_COUNT below as well as the 1098 message strings in strerror.c when adding new error codes here. */ 1099 1100 /** Other error */ 1101 LIBUSB_ERROR_OTHER = -99, 1102 }; 1103 1104 /* Total number of error codes in enum libusb_error */ 1105 #define LIBUSB_ERROR_COUNT 14 1106 1107 /** \ingroup asyncio 1108 * Transfer status codes */ 1109 enum libusb_transfer_status { 1110 /** Transfer completed without error. Note that this does not indicate 1111 * that the entire amount of requested data was transferred. */ 1112 LIBUSB_TRANSFER_COMPLETED, 1113 1114 /** Transfer failed */ 1115 LIBUSB_TRANSFER_ERROR, 1116 1117 /** Transfer timed out */ 1118 LIBUSB_TRANSFER_TIMED_OUT, 1119 1120 /** Transfer was cancelled */ 1121 LIBUSB_TRANSFER_CANCELLED, 1122 1123 /** For bulk/interrupt endpoints: halt condition detected (endpoint 1124 * stalled). For control endpoints: control request not supported. */ 1125 LIBUSB_TRANSFER_STALL, 1126 1127 /** Device was disconnected */ 1128 LIBUSB_TRANSFER_NO_DEVICE, 1129 1130 /** Device sent more data than requested */ 1131 LIBUSB_TRANSFER_OVERFLOW, 1132 1133 /* NB! Remember to update libusb_error_name() 1134 when adding new status codes here. */ 1135 }; 1136 1137 /** \ingroup asyncio 1138 * libusb_transfer.flags values */ 1139 enum libusb_transfer_flags { 1140 /** Report short frames as errors */ 1141 LIBUSB_TRANSFER_SHORT_NOT_OK = 1<<0, 1142 1143 /** Automatically free() transfer buffer during libusb_free_transfer() */ 1144 LIBUSB_TRANSFER_FREE_BUFFER = 1<<1, 1145 1146 /** Automatically call libusb_free_transfer() after callback returns. 1147 * If this flag is set, it is illegal to call libusb_free_transfer() 1148 * from your transfer callback, as this will result in a double-free 1149 * when this flag is acted upon. */ 1150 LIBUSB_TRANSFER_FREE_TRANSFER = 1<<2, 1151 1152 /** Terminate transfers that are a multiple of the endpoint's 1153 * wMaxPacketSize with an extra zero length packet. This is useful 1154 * when a device protocol mandates that each logical request is 1155 * terminated by an incomplete packet (i.e. the logical requests are 1156 * not separated by other means). 1157 * 1158 * This flag only affects host-to-device transfers to bulk and interrupt 1159 * endpoints. In other situations, it is ignored. 1160 * 1161 * This flag only affects transfers with a length that is a multiple of 1162 * the endpoint's wMaxPacketSize. On transfers of other lengths, this 1163 * flag has no effect. Therefore, if you are working with a device that 1164 * needs a ZLP whenever the end of the logical request falls on a packet 1165 * boundary, then it is sensible to set this flag on <em>every</em> 1166 * transfer (you do not have to worry about only setting it on transfers 1167 * that end on the boundary). 1168 * 1169 * This flag is currently only supported on Linux. 1170 * On other systems, libusb_submit_transfer() will return 1171 * LIBUSB_ERROR_NOT_SUPPORTED for every transfer where this flag is set. 1172 * 1173 * Available since libusb-1.0.9. 1174 */ 1175 LIBUSB_TRANSFER_ADD_ZERO_PACKET = 1 << 3, 1176 }; 1177 1178 /** \ingroup asyncio 1179 * Isochronous packet descriptor. */ 1180 struct libusb_iso_packet_descriptor { 1181 /** Length of data to request in this packet */ 1182 unsigned int length; 1183 1184 /** Amount of data that was actually transferred */ 1185 unsigned int actual_length; 1186 1187 /** Status code for this packet */ 1188 enum libusb_transfer_status status; 1189 }; 1190 1191 struct libusb_transfer; 1192 1193 /** \ingroup asyncio 1194 * Asynchronous transfer callback function type. When submitting asynchronous 1195 * transfers, you pass a pointer to a callback function of this type via the 1196 * \ref libusb_transfer::callback "callback" member of the libusb_transfer 1197 * structure. libusbx will call this function later, when the transfer has 1198 * completed or failed. See \ref asyncio for more information. 1199 * \param transfer The libusb_transfer struct the callback function is being 1200 * notified about. 1201 */ 1202 typedef void (LIBUSB_CALL *libusb_transfer_cb_fn)(struct libusb_transfer *transfer); 1203 1204 /** \ingroup asyncio 1205 * The generic USB transfer structure. The user populates this structure and 1206 * then submits it in order to request a transfer. After the transfer has 1207 * completed, the library populates the transfer with the results and passes 1208 * it back to the user. 1209 */ 1210 struct libusb_transfer { 1211 /** Handle of the device that this transfer will be submitted to */ 1212 libusb_device_handle *dev_handle; 1213 1214 /** A bitwise OR combination of \ref libusb_transfer_flags. */ 1215 uint8_t flags; 1216 1217 /** Address of the endpoint where this transfer will be sent. */ 1218 unsigned char endpoint; 1219 1220 /** Type of the endpoint from \ref libusb_transfer_type */ 1221 unsigned char type; 1222 1223 /** Timeout for this transfer in millseconds. A value of 0 indicates no 1224 * timeout. */ 1225 unsigned int timeout; 1226 1227 /** The status of the transfer. Read-only, and only for use within 1228 * transfer callback function. 1229 * 1230 * If this is an isochronous transfer, this field may read COMPLETED even 1231 * if there were errors in the frames. Use the 1232 * \ref libusb_iso_packet_descriptor::status "status" field in each packet 1233 * to determine if errors occurred. */ 1234 enum libusb_transfer_status status; 1235 1236 /** Length of the data buffer */ 1237 int length; 1238 1239 /** Actual length of data that was transferred. Read-only, and only for 1240 * use within transfer callback function. Not valid for isochronous 1241 * endpoint transfers. */ 1242 int actual_length; 1243 1244 /** Callback function. This will be invoked when the transfer completes, 1245 * fails, or is cancelled. */ 1246 libusb_transfer_cb_fn callback; 1247 1248 /** User context data to pass to the callback function. */ 1249 void *user_data; 1250 1251 /** Data buffer */ 1252 unsigned char *buffer; 1253 1254 /** Number of isochronous packets. Only used for I/O with isochronous 1255 * endpoints. */ 1256 int num_iso_packets; 1257 1258 /** Isochronous packet descriptors, for isochronous transfers only. */ 1259 struct libusb_iso_packet_descriptor iso_packet_desc 1260 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) 1261 [] /* valid C99 code */ 1262 #else 1263 [0] /* non-standard, but usually working code */ 1264 #endif 1265 ; 1266 }; 1267 1268 /** \ingroup misc 1269 * Capabilities supported by an instance of libusb on the current running 1270 * platform. Test if the loaded library supports a given capability by calling 1271 * \ref libusb_has_capability(). 1272 */ 1273 enum libusb_capability { 1274 /** The libusb_has_capability() API is available. */ 1275 LIBUSB_CAP_HAS_CAPABILITY = 0x0000, 1276 /** Hotplug support is available on this platform. */ 1277 LIBUSB_CAP_HAS_HOTPLUG = 0x0001, 1278 /** The library can access HID devices without requiring user intervention. 1279 * Note that before being able to actually access an HID device, you may 1280 * still have to call additional libusbx functions such as 1281 * \ref libusb_detach_kernel_driver(). */ 1282 LIBUSB_CAP_HAS_HID_ACCESS = 0x0100, 1283 /** The library supports detaching of the default USB driver, using 1284 * \ref libusb_detach_kernel_driver(), if one is set by the OS kernel */ 1285 LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER = 0x0101 1286 }; 1287 1288 /** \ingroup lib 1289 * Log message levels. 1290 * - LIBUSB_LOG_LEVEL_NONE (0) : no messages ever printed by the library (default) 1291 * - LIBUSB_LOG_LEVEL_ERROR (1) : error messages are printed to stderr 1292 * - LIBUSB_LOG_LEVEL_WARNING (2) : warning and error messages are printed to stderr 1293 * - LIBUSB_LOG_LEVEL_INFO (3) : informational messages are printed to stdout, warning 1294 * and error messages are printed to stderr 1295 * - LIBUSB_LOG_LEVEL_DEBUG (4) : debug and informational messages are printed to stdout, 1296 * warnings and errors to stderr 1297 */ 1298 enum libusb_log_level { 1299 LIBUSB_LOG_LEVEL_NONE = 0, 1300 LIBUSB_LOG_LEVEL_ERROR, 1301 LIBUSB_LOG_LEVEL_WARNING, 1302 LIBUSB_LOG_LEVEL_INFO, 1303 LIBUSB_LOG_LEVEL_DEBUG, 1304 }; 1305 1306 int LIBUSB_CALL libusb_init(libusb_context **ctx); 1307 void LIBUSB_CALL libusb_exit(libusb_context *ctx); 1308 void LIBUSB_CALL libusb_set_debug(libusb_context *ctx, int level); 1309 const struct libusb_version * LIBUSB_CALL libusb_get_version(void); 1310 int LIBUSB_CALL libusb_has_capability(uint32_t capability); 1311 const char * LIBUSB_CALL libusb_error_name(int errcode); 1312 int LIBUSB_CALL libusb_setlocale(const char *locale); 1313 const char * LIBUSB_CALL libusb_strerror(enum libusb_error errcode); 1314 1315 ssize_t LIBUSB_CALL libusb_get_device_list(libusb_context *ctx, 1316 libusb_device ***list); 1317 void LIBUSB_CALL libusb_free_device_list(libusb_device **list, 1318 int unref_devices); 1319 libusb_device * LIBUSB_CALL libusb_ref_device(libusb_device *dev); 1320 void LIBUSB_CALL libusb_unref_device(libusb_device *dev); 1321 1322 int LIBUSB_CALL libusb_get_configuration(libusb_device_handle *dev, 1323 int *config); 1324 int LIBUSB_CALL libusb_get_device_descriptor(libusb_device *dev, 1325 struct libusb_device_descriptor *desc); 1326 int LIBUSB_CALL libusb_get_active_config_descriptor(libusb_device *dev, 1327 struct libusb_config_descriptor **config); 1328 int LIBUSB_CALL libusb_get_config_descriptor(libusb_device *dev, 1329 uint8_t config_index, struct libusb_config_descriptor **config); 1330 int LIBUSB_CALL libusb_get_config_descriptor_by_value(libusb_device *dev, 1331 uint8_t bConfigurationValue, struct libusb_config_descriptor **config); 1332 void LIBUSB_CALL libusb_free_config_descriptor( 1333 struct libusb_config_descriptor *config); 1334 int LIBUSB_CALL libusb_get_ss_endpoint_companion_descriptor( 1335 struct libusb_context *ctx, 1336 const struct libusb_endpoint_descriptor *endpoint, 1337 struct libusb_ss_endpoint_companion_descriptor **ep_comp); 1338 void LIBUSB_CALL libusb_free_ss_endpoint_companion_descriptor( 1339 struct libusb_ss_endpoint_companion_descriptor *ep_comp); 1340 int LIBUSB_CALL libusb_get_bos_descriptor(libusb_device_handle *handle, 1341 struct libusb_bos_descriptor **bos); 1342 void LIBUSB_CALL libusb_free_bos_descriptor(struct libusb_bos_descriptor *bos); 1343 int LIBUSB_CALL libusb_get_usb_2_0_extension_descriptor( 1344 struct libusb_context *ctx, 1345 struct libusb_bos_dev_capability_descriptor *dev_cap, 1346 struct libusb_usb_2_0_extension_descriptor **usb_2_0_extension); 1347 void LIBUSB_CALL libusb_free_usb_2_0_extension_descriptor( 1348 struct libusb_usb_2_0_extension_descriptor *usb_2_0_extension); 1349 int LIBUSB_CALL libusb_get_ss_usb_device_capability_descriptor( 1350 struct libusb_context *ctx, 1351 struct libusb_bos_dev_capability_descriptor *dev_cap, 1352 struct libusb_ss_usb_device_capability_descriptor **ss_usb_device_cap); 1353 void LIBUSB_CALL libusb_free_ss_usb_device_capability_descriptor( 1354 struct libusb_ss_usb_device_capability_descriptor *ss_usb_device_cap); 1355 int LIBUSB_CALL libusb_get_container_id_descriptor(struct libusb_context *ctx, 1356 struct libusb_bos_dev_capability_descriptor *dev_cap, 1357 struct libusb_container_id_descriptor **container_id); 1358 void LIBUSB_CALL libusb_free_container_id_descriptor( 1359 struct libusb_container_id_descriptor *container_id); 1360 uint8_t LIBUSB_CALL libusb_get_bus_number(libusb_device *dev); 1361 uint8_t LIBUSB_CALL libusb_get_port_number(libusb_device *dev); 1362 int LIBUSB_CALL libusb_get_port_numbers(libusb_device *dev, uint8_t* port_numbers, int port_numbers_len); 1363 LIBUSB_DEPRECATED_FOR(libusb_get_port_numbers) 1364 int LIBUSB_CALL libusb_get_port_path(libusb_context *ctx, libusb_device *dev, uint8_t* path, uint8_t path_length); 1365 libusb_device * LIBUSB_CALL libusb_get_parent(libusb_device *dev); 1366 uint8_t LIBUSB_CALL libusb_get_device_address(libusb_device *dev); 1367 int LIBUSB_CALL libusb_get_device_speed(libusb_device *dev); 1368 int LIBUSB_CALL libusb_get_max_packet_size(libusb_device *dev, 1369 unsigned char endpoint); 1370 int LIBUSB_CALL libusb_get_max_iso_packet_size(libusb_device *dev, 1371 unsigned char endpoint); 1372 1373 int LIBUSB_CALL libusb_open(libusb_device *dev, libusb_device_handle **handle); 1374 void LIBUSB_CALL libusb_close(libusb_device_handle *dev_handle); 1375 libusb_device * LIBUSB_CALL libusb_get_device(libusb_device_handle *dev_handle); 1376 1377 int LIBUSB_CALL libusb_set_configuration(libusb_device_handle *dev, 1378 int configuration); 1379 int LIBUSB_CALL libusb_claim_interface(libusb_device_handle *dev, 1380 int interface_number); 1381 int LIBUSB_CALL libusb_release_interface(libusb_device_handle *dev, 1382 int interface_number); 1383 1384 libusb_device_handle * LIBUSB_CALL libusb_open_device_with_vid_pid( 1385 libusb_context *ctx, uint16_t vendor_id, uint16_t product_id); 1386 1387 int LIBUSB_CALL libusb_set_interface_alt_setting(libusb_device_handle *dev, 1388 int interface_number, int alternate_setting); 1389 int LIBUSB_CALL libusb_clear_halt(libusb_device_handle *dev, 1390 unsigned char endpoint); 1391 int LIBUSB_CALL libusb_reset_device(libusb_device_handle *dev); 1392 1393 int LIBUSB_CALL libusb_kernel_driver_active(libusb_device_handle *dev, 1394 int interface_number); 1395 int LIBUSB_CALL libusb_detach_kernel_driver(libusb_device_handle *dev, 1396 int interface_number); 1397 int LIBUSB_CALL libusb_attach_kernel_driver(libusb_device_handle *dev, 1398 int interface_number); 1399 int LIBUSB_CALL libusb_set_auto_detach_kernel_driver( 1400 libusb_device_handle *dev, int enable); 1401 1402 /* async I/O */ 1403 1404 /** \ingroup asyncio 1405 * Get the data section of a control transfer. This convenience function is here 1406 * to remind you that the data does not start until 8 bytes into the actual 1407 * buffer, as the setup packet comes first. 1408 * 1409 * Calling this function only makes sense from a transfer callback function, 1410 * or situations where you have already allocated a suitably sized buffer at 1411 * transfer->buffer. 1412 * 1413 * \param transfer a transfer 1414 * \returns pointer to the first byte of the data section 1415 */ 1416 static inline unsigned char *libusb_control_transfer_get_data( 1417 struct libusb_transfer *transfer) 1418 { 1419 return transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE; 1420 } 1421 1422 /** \ingroup asyncio 1423 * Get the control setup packet of a control transfer. This convenience 1424 * function is here to remind you that the control setup occupies the first 1425 * 8 bytes of the transfer data buffer. 1426 * 1427 * Calling this function only makes sense from a transfer callback function, 1428 * or situations where you have already allocated a suitably sized buffer at 1429 * transfer->buffer. 1430 * 1431 * \param transfer a transfer 1432 * \returns a casted pointer to the start of the transfer data buffer 1433 */ 1434 static inline struct libusb_control_setup *libusb_control_transfer_get_setup( 1435 struct libusb_transfer *transfer) 1436 { 1437 return (struct libusb_control_setup *)(void *) transfer->buffer; 1438 } 1439 1440 /** \ingroup asyncio 1441 * Helper function to populate the setup packet (first 8 bytes of the data 1442 * buffer) for a control transfer. The wIndex, wValue and wLength values should 1443 * be given in host-endian byte order. 1444 * 1445 * \param buffer buffer to output the setup packet into 1446 * This pointer must be aligned to at least 2 bytes boundary. 1447 * \param bmRequestType see the 1448 * \ref libusb_control_setup::bmRequestType "bmRequestType" field of 1449 * \ref libusb_control_setup 1450 * \param bRequest see the 1451 * \ref libusb_control_setup::bRequest "bRequest" field of 1452 * \ref libusb_control_setup 1453 * \param wValue see the 1454 * \ref libusb_control_setup::wValue "wValue" field of 1455 * \ref libusb_control_setup 1456 * \param wIndex see the 1457 * \ref libusb_control_setup::wIndex "wIndex" field of 1458 * \ref libusb_control_setup 1459 * \param wLength see the 1460 * \ref libusb_control_setup::wLength "wLength" field of 1461 * \ref libusb_control_setup 1462 */ 1463 static inline void libusb_fill_control_setup(unsigned char *buffer, 1464 uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, 1465 uint16_t wLength) 1466 { 1467 struct libusb_control_setup *setup = (struct libusb_control_setup *)(void *) buffer; 1468 setup->bmRequestType = bmRequestType; 1469 setup->bRequest = bRequest; 1470 setup->wValue = libusb_cpu_to_le16(wValue); 1471 setup->wIndex = libusb_cpu_to_le16(wIndex); 1472 setup->wLength = libusb_cpu_to_le16(wLength); 1473 } 1474 1475 struct libusb_transfer * LIBUSB_CALL libusb_alloc_transfer(int iso_packets); 1476 int LIBUSB_CALL libusb_submit_transfer(struct libusb_transfer *transfer); 1477 int LIBUSB_CALL libusb_cancel_transfer(struct libusb_transfer *transfer); 1478 void LIBUSB_CALL libusb_free_transfer(struct libusb_transfer *transfer); 1479 1480 /** \ingroup asyncio 1481 * Helper function to populate the required \ref libusb_transfer fields 1482 * for a control transfer. 1483 * 1484 * If you pass a transfer buffer to this function, the first 8 bytes will 1485 * be interpreted as a control setup packet, and the wLength field will be 1486 * used to automatically populate the \ref libusb_transfer::length "length" 1487 * field of the transfer. Therefore the recommended approach is: 1488 * -# Allocate a suitably sized data buffer (including space for control setup) 1489 * -# Call libusb_fill_control_setup() 1490 * -# If this is a host-to-device transfer with a data stage, put the data 1491 * in place after the setup packet 1492 * -# Call this function 1493 * -# Call libusb_submit_transfer() 1494 * 1495 * It is also legal to pass a NULL buffer to this function, in which case this 1496 * function will not attempt to populate the length field. Remember that you 1497 * must then populate the buffer and length fields later. 1498 * 1499 * \param transfer the transfer to populate 1500 * \param dev_handle handle of the device that will handle the transfer 1501 * \param buffer data buffer. If provided, this function will interpret the 1502 * first 8 bytes as a setup packet and infer the transfer length from that. 1503 * This pointer must be aligned to at least 2 bytes boundary. 1504 * \param callback callback function to be invoked on transfer completion 1505 * \param user_data user data to pass to callback function 1506 * \param timeout timeout for the transfer in milliseconds 1507 */ 1508 static inline void libusb_fill_control_transfer( 1509 struct libusb_transfer *transfer, libusb_device_handle *dev_handle, 1510 unsigned char *buffer, libusb_transfer_cb_fn callback, void *user_data, 1511 unsigned int timeout) 1512 { 1513 struct libusb_control_setup *setup = (struct libusb_control_setup *)(void *) buffer; 1514 transfer->dev_handle = dev_handle; 1515 transfer->endpoint = 0; 1516 transfer->type = LIBUSB_TRANSFER_TYPE_CONTROL; 1517 transfer->timeout = timeout; 1518 transfer->buffer = buffer; 1519 if (setup) 1520 transfer->length = (int) (LIBUSB_CONTROL_SETUP_SIZE 1521 + libusb_le16_to_cpu(setup->wLength)); 1522 transfer->user_data = user_data; 1523 transfer->callback = callback; 1524 } 1525 1526 /** \ingroup asyncio 1527 * Helper function to populate the required \ref libusb_transfer fields 1528 * for a bulk transfer. 1529 * 1530 * \param transfer the transfer to populate 1531 * \param dev_handle handle of the device that will handle the transfer 1532 * \param endpoint address of the endpoint where this transfer will be sent 1533 * \param buffer data buffer 1534 * \param length length of data buffer 1535 * \param callback callback function to be invoked on transfer completion 1536 * \param user_data user data to pass to callback function 1537 * \param timeout timeout for the transfer in milliseconds 1538 */ 1539 static inline void libusb_fill_bulk_transfer(struct libusb_transfer *transfer, 1540 libusb_device_handle *dev_handle, unsigned char endpoint, 1541 unsigned char *buffer, int length, libusb_transfer_cb_fn callback, 1542 void *user_data, unsigned int timeout) 1543 { 1544 transfer->dev_handle = dev_handle; 1545 transfer->endpoint = endpoint; 1546 transfer->type = LIBUSB_TRANSFER_TYPE_BULK; 1547 transfer->timeout = timeout; 1548 transfer->buffer = buffer; 1549 transfer->length = length; 1550 transfer->user_data = user_data; 1551 transfer->callback = callback; 1552 } 1553 1554 /** \ingroup asyncio 1555 * Helper function to populate the required \ref libusb_transfer fields 1556 * for an interrupt transfer. 1557 * 1558 * \param transfer the transfer to populate 1559 * \param dev_handle handle of the device that will handle the transfer 1560 * \param endpoint address of the endpoint where this transfer will be sent 1561 * \param buffer data buffer 1562 * \param length length of data buffer 1563 * \param callback callback function to be invoked on transfer completion 1564 * \param user_data user data to pass to callback function 1565 * \param timeout timeout for the transfer in milliseconds 1566 */ 1567 static inline void libusb_fill_interrupt_transfer( 1568 struct libusb_transfer *transfer, libusb_device_handle *dev_handle, 1569 unsigned char endpoint, unsigned char *buffer, int length, 1570 libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout) 1571 { 1572 transfer->dev_handle = dev_handle; 1573 transfer->endpoint = endpoint; 1574 transfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT; 1575 transfer->timeout = timeout; 1576 transfer->buffer = buffer; 1577 transfer->length = length; 1578 transfer->user_data = user_data; 1579 transfer->callback = callback; 1580 } 1581 1582 /** \ingroup asyncio 1583 * Helper function to populate the required \ref libusb_transfer fields 1584 * for an isochronous transfer. 1585 * 1586 * \param transfer the transfer to populate 1587 * \param dev_handle handle of the device that will handle the transfer 1588 * \param endpoint address of the endpoint where this transfer will be sent 1589 * \param buffer data buffer 1590 * \param length length of data buffer 1591 * \param num_iso_packets the number of isochronous packets 1592 * \param callback callback function to be invoked on transfer completion 1593 * \param user_data user data to pass to callback function 1594 * \param timeout timeout for the transfer in milliseconds 1595 */ 1596 static inline void libusb_fill_iso_transfer(struct libusb_transfer *transfer, 1597 libusb_device_handle *dev_handle, unsigned char endpoint, 1598 unsigned char *buffer, int length, int num_iso_packets, 1599 libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout) 1600 { 1601 transfer->dev_handle = dev_handle; 1602 transfer->endpoint = endpoint; 1603 transfer->type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS; 1604 transfer->timeout = timeout; 1605 transfer->buffer = buffer; 1606 transfer->length = length; 1607 transfer->num_iso_packets = num_iso_packets; 1608 transfer->user_data = user_data; 1609 transfer->callback = callback; 1610 } 1611 1612 /** \ingroup asyncio 1613 * Convenience function to set the length of all packets in an isochronous 1614 * transfer, based on the num_iso_packets field in the transfer structure. 1615 * 1616 * \param transfer a transfer 1617 * \param length the length to set in each isochronous packet descriptor 1618 * \see libusb_get_max_packet_size() 1619 */ 1620 static inline void libusb_set_iso_packet_lengths( 1621 struct libusb_transfer *transfer, unsigned int length) 1622 { 1623 int i; 1624 for (i = 0; i < transfer->num_iso_packets; i++) 1625 transfer->iso_packet_desc[i].length = length; 1626 } 1627 1628 /** \ingroup asyncio 1629 * Convenience function to locate the position of an isochronous packet 1630 * within the buffer of an isochronous transfer. 1631 * 1632 * This is a thorough function which loops through all preceding packets, 1633 * accumulating their lengths to find the position of the specified packet. 1634 * Typically you will assign equal lengths to each packet in the transfer, 1635 * and hence the above method is sub-optimal. You may wish to use 1636 * libusb_get_iso_packet_buffer_simple() instead. 1637 * 1638 * \param transfer a transfer 1639 * \param packet the packet to return the address of 1640 * \returns the base address of the packet buffer inside the transfer buffer, 1641 * or NULL if the packet does not exist. 1642 * \see libusb_get_iso_packet_buffer_simple() 1643 */ 1644 static inline unsigned char *libusb_get_iso_packet_buffer( 1645 struct libusb_transfer *transfer, unsigned int packet) 1646 { 1647 int i; 1648 size_t offset = 0; 1649 int _packet; 1650 1651 /* oops..slight bug in the API. packet is an unsigned int, but we use 1652 * signed integers almost everywhere else. range-check and convert to 1653 * signed to avoid compiler warnings. FIXME for libusb-2. */ 1654 if (packet > INT_MAX) 1655 return NULL; 1656 _packet = (int) packet; 1657 1658 if (_packet >= transfer->num_iso_packets) 1659 return NULL; 1660 1661 for (i = 0; i < _packet; i++) 1662 offset += transfer->iso_packet_desc[i].length; 1663 1664 return transfer->buffer + offset; 1665 } 1666 1667 /** \ingroup asyncio 1668 * Convenience function to locate the position of an isochronous packet 1669 * within the buffer of an isochronous transfer, for transfers where each 1670 * packet is of identical size. 1671 * 1672 * This function relies on the assumption that every packet within the transfer 1673 * is of identical size to the first packet. Calculating the location of 1674 * the packet buffer is then just a simple calculation: 1675 * <tt>buffer + (packet_size * packet)</tt> 1676 * 1677 * Do not use this function on transfers other than those that have identical 1678 * packet lengths for each packet. 1679 * 1680 * \param transfer a transfer 1681 * \param packet the packet to return the address of 1682 * \returns the base address of the packet buffer inside the transfer buffer, 1683 * or NULL if the packet does not exist. 1684 * \see libusb_get_iso_packet_buffer() 1685 */ 1686 static inline unsigned char *libusb_get_iso_packet_buffer_simple( 1687 struct libusb_transfer *transfer, unsigned int packet) 1688 { 1689 int _packet; 1690 1691 /* oops..slight bug in the API. packet is an unsigned int, but we use 1692 * signed integers almost everywhere else. range-check and convert to 1693 * signed to avoid compiler warnings. FIXME for libusb-2. */ 1694 if (packet > INT_MAX) 1695 return NULL; 1696 _packet = (int) packet; 1697 1698 if (_packet >= transfer->num_iso_packets) 1699 return NULL; 1700 1701 return transfer->buffer + ((int) transfer->iso_packet_desc[0].length * _packet); 1702 } 1703 1704 /* sync I/O */ 1705 1706 int LIBUSB_CALL libusb_control_transfer(libusb_device_handle *dev_handle, 1707 uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, 1708 unsigned char *data, uint16_t wLength, unsigned int timeout); 1709 1710 int LIBUSB_CALL libusb_bulk_transfer(libusb_device_handle *dev_handle, 1711 unsigned char endpoint, unsigned char *data, int length, 1712 int *actual_length, unsigned int timeout); 1713 1714 int LIBUSB_CALL libusb_interrupt_transfer(libusb_device_handle *dev_handle, 1715 unsigned char endpoint, unsigned char *data, int length, 1716 int *actual_length, unsigned int timeout); 1717 1718 /** \ingroup desc 1719 * Retrieve a descriptor from the default control pipe. 1720 * This is a convenience function which formulates the appropriate control 1721 * message to retrieve the descriptor. 1722 * 1723 * \param dev a device handle 1724 * \param desc_type the descriptor type, see \ref libusb_descriptor_type 1725 * \param desc_index the index of the descriptor to retrieve 1726 * \param data output buffer for descriptor 1727 * \param length size of data buffer 1728 * \returns number of bytes returned in data, or LIBUSB_ERROR code on failure 1729 */ 1730 static inline int libusb_get_descriptor(libusb_device_handle *dev, 1731 uint8_t desc_type, uint8_t desc_index, unsigned char *data, int length) 1732 { 1733 return libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN, 1734 LIBUSB_REQUEST_GET_DESCRIPTOR, (uint16_t) ((desc_type << 8) | desc_index), 1735 0, data, (uint16_t) length, 1000); 1736 } 1737 1738 /** \ingroup desc 1739 * Retrieve a descriptor from a device. 1740 * This is a convenience function which formulates the appropriate control 1741 * message to retrieve the descriptor. The string returned is Unicode, as 1742 * detailed in the USB specifications. 1743 * 1744 * \param dev a device handle 1745 * \param desc_index the index of the descriptor to retrieve 1746 * \param langid the language ID for the string descriptor 1747 * \param data output buffer for descriptor 1748 * \param length size of data buffer 1749 * \returns number of bytes returned in data, or LIBUSB_ERROR code on failure 1750 * \see libusb_get_string_descriptor_ascii() 1751 */ 1752 static inline int libusb_get_string_descriptor(libusb_device_handle *dev, 1753 uint8_t desc_index, uint16_t langid, unsigned char *data, int length) 1754 { 1755 return libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN, 1756 LIBUSB_REQUEST_GET_DESCRIPTOR, (uint16_t)((LIBUSB_DT_STRING << 8) | desc_index), 1757 langid, data, (uint16_t) length, 1000); 1758 } 1759 1760 int LIBUSB_CALL libusb_get_string_descriptor_ascii(libusb_device_handle *dev, 1761 uint8_t desc_index, unsigned char *data, int length); 1762 1763 /* polling and timeouts */ 1764 1765 int LIBUSB_CALL libusb_try_lock_events(libusb_context *ctx); 1766 void LIBUSB_CALL libusb_lock_events(libusb_context *ctx); 1767 void LIBUSB_CALL libusb_unlock_events(libusb_context *ctx); 1768 int LIBUSB_CALL libusb_event_handling_ok(libusb_context *ctx); 1769 int LIBUSB_CALL libusb_event_handler_active(libusb_context *ctx); 1770 void LIBUSB_CALL libusb_lock_event_waiters(libusb_context *ctx); 1771 void LIBUSB_CALL libusb_unlock_event_waiters(libusb_context *ctx); 1772 int LIBUSB_CALL libusb_wait_for_event(libusb_context *ctx, struct timeval *tv); 1773 1774 int LIBUSB_CALL libusb_handle_events_timeout(libusb_context *ctx, 1775 struct timeval *tv); 1776 int LIBUSB_CALL libusb_handle_events_timeout_completed(libusb_context *ctx, 1777 struct timeval *tv, int *completed); 1778 int LIBUSB_CALL libusb_handle_events(libusb_context *ctx); 1779 int LIBUSB_CALL libusb_handle_events_completed(libusb_context *ctx, int *completed); 1780 int LIBUSB_CALL libusb_handle_events_locked(libusb_context *ctx, 1781 struct timeval *tv); 1782 int LIBUSB_CALL libusb_pollfds_handle_timeouts(libusb_context *ctx); 1783 int LIBUSB_CALL libusb_get_next_timeout(libusb_context *ctx, 1784 struct timeval *tv); 1785 1786 /** \ingroup poll 1787 * File descriptor for polling 1788 */ 1789 struct libusb_pollfd { 1790 /** Numeric file descriptor */ 1791 int fd; 1792 1793 /** Event flags to poll for from <poll.h>. POLLIN indicates that you 1794 * should monitor this file descriptor for becoming ready to read from, 1795 * and POLLOUT indicates that you should monitor this file descriptor for 1796 * nonblocking write readiness. */ 1797 short events; 1798 }; 1799 1800 /** \ingroup poll 1801 * Callback function, invoked when a new file descriptor should be added 1802 * to the set of file descriptors monitored for events. 1803 * \param fd the new file descriptor 1804 * \param events events to monitor for, see \ref libusb_pollfd for a 1805 * description 1806 * \param user_data User data pointer specified in 1807 * libusb_set_pollfd_notifiers() call 1808 * \see libusb_set_pollfd_notifiers() 1809 */ 1810 typedef void (LIBUSB_CALL *libusb_pollfd_added_cb)(int fd, short events, 1811 void *user_data); 1812 1813 /** \ingroup poll 1814 * Callback function, invoked when a file descriptor should be removed from 1815 * the set of file descriptors being monitored for events. After returning 1816 * from this callback, do not use that file descriptor again. 1817 * \param fd the file descriptor to stop monitoring 1818 * \param user_data User data pointer specified in 1819 * libusb_set_pollfd_notifiers() call 1820 * \see libusb_set_pollfd_notifiers() 1821 */ 1822 typedef void (LIBUSB_CALL *libusb_pollfd_removed_cb)(int fd, void *user_data); 1823 1824 const struct libusb_pollfd ** LIBUSB_CALL libusb_get_pollfds( 1825 libusb_context *ctx); 1826 void LIBUSB_CALL libusb_set_pollfd_notifiers(libusb_context *ctx, 1827 libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb, 1828 void *user_data); 1829 1830 /** \ingroup hotplug 1831 * Callback handle. 1832 * 1833 * Callbacks handles are generated by libusb_hotplug_register_callback() 1834 * and can be used to deregister callbacks. Callback handles are unique 1835 * per libusb_context and it is safe to call libusb_hotplug_deregister_callback() 1836 * on an already deregisted callback. 1837 * 1838 * Since version 1.0.16, \ref LIBUSBX_API_VERSION >= 0x01000102 1839 * 1840 * For more information, see \ref hotplug. 1841 */ 1842 typedef int libusb_hotplug_callback_handle; 1843 1844 /** \ingroup hotplug 1845 * 1846 * Since version 1.0.16, \ref LIBUSBX_API_VERSION >= 0x01000102 1847 * 1848 * Flags for hotplug events */ 1849 typedef enum { 1850 /** Arm the callback and fire it for all matching currently attached devices. */ 1851 LIBUSB_HOTPLUG_ENUMERATE = 1, 1852 } libusb_hotplug_flag; 1853 1854 /** \ingroup hotplug 1855 * 1856 * Since version 1.0.16, \ref LIBUSBX_API_VERSION >= 0x01000102 1857 * 1858 * Hotplug events */ 1859 typedef enum { 1860 /** A device has been plugged in and is ready to use */ 1861 LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED = 0x01, 1862 1863 /** A device has left and is no longer available. 1864 * It is the user's responsibility to call libusb_close on any handle associated with a disconnected device. 1865 * It is safe to call libusb_get_device_descriptor on a device that has left */ 1866 LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT = 0x02, 1867 } libusb_hotplug_event; 1868 1869 /** \ingroup hotplug 1870 * Wildcard matching for hotplug events */ 1871 #define LIBUSB_HOTPLUG_MATCH_ANY -1 1872 1873 /** \ingroup hotplug 1874 * Hotplug callback function type. When requesting hotplug event notifications, 1875 * you pass a pointer to a callback function of this type. 1876 * 1877 * This callback may be called by an internal event thread and as such it is 1878 * recommended the callback do minimal processing before returning. 1879 * 1880 * libusbx will call this function later, when a matching event had happened on 1881 * a matching device. See \ref hotplug for more information. 1882 * 1883 * It is safe to call either libusb_hotplug_register_callback() or 1884 * libusb_hotplug_deregister_callback() from within a callback function. 1885 * 1886 * Since version 1.0.16, \ref LIBUSBX_API_VERSION >= 0x01000102 1887 * 1888 * \param ctx context of this notification 1889 * \param device libusb_device this event occurred on 1890 * \param event event that occurred 1891 * \param user_data user data provided when this callback was registered 1892 * \returns bool whether this callback is finished processing events. 1893 * returning 1 will cause this callback to be deregistered 1894 */ 1895 typedef int (LIBUSB_CALL *libusb_hotplug_callback_fn)(libusb_context *ctx, 1896 libusb_device *device, 1897 libusb_hotplug_event event, 1898 void *user_data); 1899 1900 /** \ingroup hotplug 1901 * Register a hotplug callback function 1902 * 1903 * Register a callback with the libusb_context. The callback will fire 1904 * when a matching event occurs on a matching device. The callback is 1905 * armed until either it is deregistered with libusb_hotplug_deregister_callback() 1906 * or the supplied callback returns 1 to indicate it is finished processing events. 1907 * 1908 * If the \ref LIBUSB_HOTPLUG_ENUMERATE is passed the callback will be 1909 * called with a \ref LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED for all devices 1910 * already plugged into the machine. Note that libusbx modifies its internal 1911 * device list from a separate thread, while calling hotplug callbacks from 1912 * libusb_handle_events(), so it is possible for a device to already be present 1913 * on, or removed from, its internal device list, while the hotplug callbacks 1914 * still need to be dispatched. This means that when using \ref 1915 * LIBUSB_HOTPLUG_ENUMERATE, your callback may be called twice for the arrival 1916 * of the same device, once from libusb_hotplug_register_callback() and once 1917 * from libusb_handle_events(); and/or your callback may be called for the 1918 * removal of a device for which an arrived call was never made. 1919 * 1920 * Since version 1.0.16, \ref LIBUSBX_API_VERSION >= 0x01000102 1921 * 1922 * \param[in] ctx context to register this callback with 1923 * \param[in] events bitwise or of events that will trigger this callback. See \ref 1924 * libusb_hotplug_event 1925 * \param[in] flags hotplug callback flags. See \ref libusb_hotplug_flag 1926 * \param[in] vendor_id the vendor id to match or \ref LIBUSB_HOTPLUG_MATCH_ANY 1927 * \param[in] product_id the product id to match or \ref LIBUSB_HOTPLUG_MATCH_ANY 1928 * \param[in] dev_class the device class to match or \ref LIBUSB_HOTPLUG_MATCH_ANY 1929 * \param[in] cb_fn the function to be invoked on a matching event/device 1930 * \param[in] user_data user data to pass to the callback function 1931 * \param[out] handle pointer to store the handle of the allocated callback (can be NULL) 1932 * \returns LIBUSB_SUCCESS on success LIBUSB_ERROR code on failure 1933 */ 1934 int LIBUSB_CALL libusb_hotplug_register_callback(libusb_context *ctx, 1935 libusb_hotplug_event events, 1936 libusb_hotplug_flag flags, 1937 int vendor_id, int product_id, 1938 int dev_class, 1939 libusb_hotplug_callback_fn cb_fn, 1940 void *user_data, 1941 libusb_hotplug_callback_handle *handle); 1942 1943 /** \ingroup hotplug 1944 * Deregisters a hotplug callback. 1945 * 1946 * Deregister a callback from a libusb_context. This function is safe to call from within 1947 * a hotplug callback. 1948 * 1949 * Since version 1.0.16, \ref LIBUSBX_API_VERSION >= 0x01000102 1950 * 1951 * \param[in] ctx context this callback is registered with 1952 * \param[in] handle the handle of the callback to deregister 1953 */ 1954 void LIBUSB_CALL libusb_hotplug_deregister_callback(libusb_context *ctx, 1955 libusb_hotplug_callback_handle handle); 1956 1957 #ifdef __cplusplus 1958 } 1959 #endif 1960 1961 #ifdef _MSC_VER 1962 #pragma warning(pop) 1963 #endif 1964 1965 #endif 1966