libusb
libusb.h
00001 /*
00002  * Public libusb header file
00003  * Copyright (C) 2007-2008 Daniel Drake <dsd@gentoo.org>
00004  * Copyright (c) 2001 Johannes Erdfelt <johannes@erdfelt.com>
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00019  */
00020 
00021 #ifndef LIBUSB_H
00022 #define LIBUSB_H
00023 
00024 #ifdef _MSC_VER
00025 /* on MS environments, the inline keyword is available in C++ only */
00026 #define inline __inline
00027 /* ssize_t is also not available (copy/paste from MinGW) */
00028 #ifndef _SSIZE_T_DEFINED
00029 #define _SSIZE_T_DEFINED
00030 #undef ssize_t
00031 #ifdef _WIN64
00032   typedef __int64 ssize_t;
00033 #else
00034   typedef int ssize_t;
00035 #endif /* _WIN64 */
00036 #endif /* _SSIZE_T_DEFINED */
00037 #endif /* _MSC_VER */
00038 
00039 /* stdint.h is also not usually available on MS */
00040 #if defined(_MSC_VER) && (_MSC_VER < 1600) && (!defined(_STDINT)) && (!defined(_STDINT_H))
00041 typedef unsigned __int8   uint8_t;
00042 typedef unsigned __int16  uint16_t;
00043 typedef unsigned __int32  uint32_t;
00044 #else
00045 #include <stdint.h>
00046 #endif
00047 
00048 #include <sys/types.h>
00049 #include <time.h>
00050 #include <limits.h>
00051 
00052 #if defined(__linux) || defined(__APPLE__) || defined(__CYGWIN__)
00053 #include <sys/time.h>
00054 #endif
00055 
00056 /* 'interface' might be defined as a macro on Windows, so we need to
00057  * undefine it so as not to break the current libusb API, because
00058  * libusb_config_descriptor has an 'interface' member
00059  * As this can be problematic if you include windows.h after libusb.h
00060  * in your sources, we force windows.h to be included first. */
00061 #if defined(_WIN32) || defined(__CYGWIN__)
00062 #include <windows.h>
00063 #if defined(interface)
00064 #undef interface
00065 #endif
00066 #endif
00067 
00093 /* LIBUSB_CALL must be defined on both definition and declaration of libusb
00094  * functions. You'd think that declaration would be enough, but cygwin will
00095  * complain about conflicting types unless both are marked this way.
00096  * The placement of this macro is important too; it must appear after the
00097  * return type, before the function name. See internal documentation for
00098  * API_EXPORTED.
00099  */
00100 #if defined(_WIN32) || defined(__CYGWIN__)
00101 #define LIBUSB_CALL WINAPI
00102 #else
00103 #define LIBUSB_CALL
00104 #endif
00105 
00106 #ifdef __cplusplus
00107 extern "C" {
00108 #endif
00109 
00118 static inline uint16_t libusb_cpu_to_le16(const uint16_t x)
00119 {
00120     union {
00121         uint8_t  b8[2];
00122         uint16_t b16;
00123     } _tmp;
00124     _tmp.b8[1] = x >> 8;
00125     _tmp.b8[0] = x & 0xff;
00126     return _tmp.b16;
00127 }
00128 
00137 #define libusb_le16_to_cpu libusb_cpu_to_le16
00138 
00139 /* standard USB stuff */
00140 
00143 enum libusb_class_code {
00148     LIBUSB_CLASS_PER_INTERFACE = 0,
00149 
00151     LIBUSB_CLASS_AUDIO = 1,
00152 
00154     LIBUSB_CLASS_COMM = 2,
00155 
00157     LIBUSB_CLASS_HID = 3,
00158 
00160     LIBUSB_CLASS_PHYSICAL = 5,
00161 
00163     LIBUSB_CLASS_PRINTER = 7,
00164 
00166     LIBUSB_CLASS_PTP = 6, /* legacy name from libusb-0.1 usb.h */
00167     LIBUSB_CLASS_IMAGE = 6,
00168 
00170     LIBUSB_CLASS_MASS_STORAGE = 8,
00171 
00173     LIBUSB_CLASS_HUB = 9,
00174 
00176     LIBUSB_CLASS_DATA = 10,
00177 
00179     LIBUSB_CLASS_SMART_CARD = 0x0b,
00180 
00182     LIBUSB_CLASS_CONTENT_SECURITY = 0x0d,
00183 
00185     LIBUSB_CLASS_VIDEO = 0x0e,
00186 
00188     LIBUSB_CLASS_PERSONAL_HEALTHCARE = 0x0f,
00189 
00191     LIBUSB_CLASS_DIAGNOSTIC_DEVICE = 0xdc,
00192 
00194     LIBUSB_CLASS_WIRELESS = 0xe0,
00195 
00197     LIBUSB_CLASS_APPLICATION = 0xfe,
00198 
00200     LIBUSB_CLASS_VENDOR_SPEC = 0xff
00201 };
00202 
00205 enum libusb_descriptor_type {
00207     LIBUSB_DT_DEVICE = 0x01,
00208 
00210     LIBUSB_DT_CONFIG = 0x02,
00211 
00213     LIBUSB_DT_STRING = 0x03,
00214 
00216     LIBUSB_DT_INTERFACE = 0x04,
00217 
00219     LIBUSB_DT_ENDPOINT = 0x05,
00220 
00222     LIBUSB_DT_HID = 0x21,
00223 
00225     LIBUSB_DT_REPORT = 0x22,
00226 
00228     LIBUSB_DT_PHYSICAL = 0x23,
00229 
00231     LIBUSB_DT_HUB = 0x29
00232 };
00233 
00234 /* Descriptor sizes per descriptor type */
00235 #define LIBUSB_DT_DEVICE_SIZE           18
00236 #define LIBUSB_DT_CONFIG_SIZE           9
00237 #define LIBUSB_DT_INTERFACE_SIZE        9
00238 #define LIBUSB_DT_ENDPOINT_SIZE     7
00239 #define LIBUSB_DT_ENDPOINT_AUDIO_SIZE   9   /* Audio extension */
00240 #define LIBUSB_DT_HUB_NONVAR_SIZE       7
00241 
00242 #define LIBUSB_ENDPOINT_ADDRESS_MASK    0x0f    /* in bEndpointAddress */
00243 #define LIBUSB_ENDPOINT_DIR_MASK        0x80
00244 
00249 enum libusb_endpoint_direction {
00251     LIBUSB_ENDPOINT_IN = 0x80,
00252 
00254     LIBUSB_ENDPOINT_OUT = 0x00
00255 };
00256 
00257 #define LIBUSB_TRANSFER_TYPE_MASK           0x03    /* in bmAttributes */
00258 
00263 enum libusb_transfer_type {
00265     LIBUSB_TRANSFER_TYPE_CONTROL = 0,
00266 
00268     LIBUSB_TRANSFER_TYPE_ISOCHRONOUS = 1,
00269 
00271     LIBUSB_TRANSFER_TYPE_BULK = 2,
00272 
00274     LIBUSB_TRANSFER_TYPE_INTERRUPT = 3
00275 };
00276 
00279 enum libusb_standard_request {
00281     LIBUSB_REQUEST_GET_STATUS = 0x00,
00282 
00284     LIBUSB_REQUEST_CLEAR_FEATURE = 0x01,
00285 
00286     /* 0x02 is reserved */
00287 
00289     LIBUSB_REQUEST_SET_FEATURE = 0x03,
00290 
00291     /* 0x04 is reserved */
00292 
00294     LIBUSB_REQUEST_SET_ADDRESS = 0x05,
00295 
00297     LIBUSB_REQUEST_GET_DESCRIPTOR = 0x06,
00298 
00300     LIBUSB_REQUEST_SET_DESCRIPTOR = 0x07,
00301 
00303     LIBUSB_REQUEST_GET_CONFIGURATION = 0x08,
00304 
00306     LIBUSB_REQUEST_SET_CONFIGURATION = 0x09,
00307 
00309     LIBUSB_REQUEST_GET_INTERFACE = 0x0A,
00310 
00312     LIBUSB_REQUEST_SET_INTERFACE = 0x0B,
00313 
00315     LIBUSB_REQUEST_SYNCH_FRAME = 0x0C
00316 };
00317 
00322 enum libusb_request_type {
00324     LIBUSB_REQUEST_TYPE_STANDARD = (0x00 << 5),
00325 
00327     LIBUSB_REQUEST_TYPE_CLASS = (0x01 << 5),
00328 
00330     LIBUSB_REQUEST_TYPE_VENDOR = (0x02 << 5),
00331 
00333     LIBUSB_REQUEST_TYPE_RESERVED = (0x03 << 5)
00334 };
00335 
00340 enum libusb_request_recipient {
00342     LIBUSB_RECIPIENT_DEVICE = 0x00,
00343 
00345     LIBUSB_RECIPIENT_INTERFACE = 0x01,
00346 
00348     LIBUSB_RECIPIENT_ENDPOINT = 0x02,
00349 
00351     LIBUSB_RECIPIENT_OTHER = 0x03
00352 };
00353 
00354 #define LIBUSB_ISO_SYNC_TYPE_MASK       0x0C
00355 
00361 enum libusb_iso_sync_type {
00363     LIBUSB_ISO_SYNC_TYPE_NONE = 0,
00364 
00366     LIBUSB_ISO_SYNC_TYPE_ASYNC = 1,
00367 
00369     LIBUSB_ISO_SYNC_TYPE_ADAPTIVE = 2,
00370 
00372     LIBUSB_ISO_SYNC_TYPE_SYNC = 3
00373 };
00374 
00375 #define LIBUSB_ISO_USAGE_TYPE_MASK 0x30
00376 
00382 enum libusb_iso_usage_type {
00384     LIBUSB_ISO_USAGE_TYPE_DATA = 0,
00385 
00387     LIBUSB_ISO_USAGE_TYPE_FEEDBACK = 1,
00388 
00390     LIBUSB_ISO_USAGE_TYPE_IMPLICIT = 2
00391 };
00392 
00398 struct libusb_device_descriptor {
00400     uint8_t  bLength;
00401 
00405     uint8_t  bDescriptorType;
00406 
00409     uint16_t bcdUSB;
00410 
00412     uint8_t  bDeviceClass;
00413 
00416     uint8_t  bDeviceSubClass;
00417 
00420     uint8_t  bDeviceProtocol;
00421 
00423     uint8_t  bMaxPacketSize0;
00424 
00426     uint16_t idVendor;
00427 
00429     uint16_t idProduct;
00430 
00432     uint16_t bcdDevice;
00433 
00435     uint8_t  iManufacturer;
00436 
00438     uint8_t  iProduct;
00439 
00441     uint8_t  iSerialNumber;
00442 
00444     uint8_t  bNumConfigurations;
00445 };
00446 
00452 struct libusb_endpoint_descriptor {
00454     uint8_t  bLength;
00455 
00459     uint8_t  bDescriptorType;
00460 
00465     uint8_t  bEndpointAddress;
00466 
00474     uint8_t  bmAttributes;
00475 
00477     uint16_t wMaxPacketSize;
00478 
00480     uint8_t  bInterval;
00481 
00484     uint8_t  bRefresh;
00485 
00487     uint8_t  bSynchAddress;
00488 
00491     const unsigned char *extra;
00492 
00494     int extra_length;
00495 };
00496 
00502 struct libusb_interface_descriptor {
00504     uint8_t  bLength;
00505 
00509     uint8_t  bDescriptorType;
00510 
00512     uint8_t  bInterfaceNumber;
00513 
00515     uint8_t  bAlternateSetting;
00516 
00519     uint8_t  bNumEndpoints;
00520 
00522     uint8_t  bInterfaceClass;
00523 
00526     uint8_t  bInterfaceSubClass;
00527 
00530     uint8_t  bInterfaceProtocol;
00531 
00533     uint8_t  iInterface;
00534 
00537     const struct libusb_endpoint_descriptor *endpoint;
00538 
00541     const unsigned char *extra;
00542 
00544     int extra_length;
00545 };
00546 
00550 struct libusb_interface {
00553     const struct libusb_interface_descriptor *altsetting;
00554 
00556     int num_altsetting;
00557 };
00558 
00564 struct libusb_config_descriptor {
00566     uint8_t  bLength;
00567 
00571     uint8_t  bDescriptorType;
00572 
00574     uint16_t wTotalLength;
00575 
00577     uint8_t  bNumInterfaces;
00578 
00580     uint8_t  bConfigurationValue;
00581 
00583     uint8_t  iConfiguration;
00584 
00586     uint8_t  bmAttributes;
00587 
00591     uint8_t  MaxPower;
00592 
00595     const struct libusb_interface *interface;
00596 
00599     const unsigned char *extra;
00600 
00602     int extra_length;
00603 };
00604 
00607 struct libusb_control_setup {
00613     uint8_t  bmRequestType;
00614 
00620     uint8_t  bRequest;
00621 
00623     uint16_t wValue;
00624 
00627     uint16_t wIndex;
00628 
00630     uint16_t wLength;
00631 };
00632 
00633 #define LIBUSB_CONTROL_SETUP_SIZE (sizeof(struct libusb_control_setup))
00634 
00635 /* libusb */
00636 
00637 struct libusb_context;
00638 struct libusb_device;
00639 struct libusb_device_handle;
00640 
00658 typedef struct libusb_context libusb_context;
00659 
00675 typedef struct libusb_device libusb_device;
00676 
00677 
00686 typedef struct libusb_device_handle libusb_device_handle;
00687 
00691 enum libusb_speed {
00693     LIBUSB_SPEED_UNKNOWN = 0,
00694 
00696     LIBUSB_SPEED_LOW = 1,
00697 
00699     LIBUSB_SPEED_FULL = 2,
00700 
00702     LIBUSB_SPEED_HIGH = 3,
00703 
00705     LIBUSB_SPEED_SUPER = 4,
00706 };
00707 
00714 enum libusb_error {
00716     LIBUSB_SUCCESS = 0,
00717 
00719     LIBUSB_ERROR_IO = -1,
00720 
00722     LIBUSB_ERROR_INVALID_PARAM = -2,
00723 
00725     LIBUSB_ERROR_ACCESS = -3,
00726 
00728     LIBUSB_ERROR_NO_DEVICE = -4,
00729 
00731     LIBUSB_ERROR_NOT_FOUND = -5,
00732 
00734     LIBUSB_ERROR_BUSY = -6,
00735 
00737     LIBUSB_ERROR_TIMEOUT = -7,
00738 
00740     LIBUSB_ERROR_OVERFLOW = -8,
00741 
00743     LIBUSB_ERROR_PIPE = -9,
00744 
00746     LIBUSB_ERROR_INTERRUPTED = -10,
00747 
00749     LIBUSB_ERROR_NO_MEM = -11,
00750 
00752     LIBUSB_ERROR_NOT_SUPPORTED = -12,
00753 
00754     /* NB! Remember to update libusb_error_name()
00755        when adding new error codes here. */
00756 
00758     LIBUSB_ERROR_OTHER = -99
00759 };
00760 
00763 enum libusb_transfer_status {
00766     LIBUSB_TRANSFER_COMPLETED,
00767 
00769     LIBUSB_TRANSFER_ERROR,
00770 
00772     LIBUSB_TRANSFER_TIMED_OUT,
00773 
00775     LIBUSB_TRANSFER_CANCELLED,
00776 
00779     LIBUSB_TRANSFER_STALL,
00780 
00782     LIBUSB_TRANSFER_NO_DEVICE,
00783 
00785     LIBUSB_TRANSFER_OVERFLOW
00786 };
00787 
00790 enum libusb_transfer_flags {
00792     LIBUSB_TRANSFER_SHORT_NOT_OK = 1<<0,
00793 
00795     LIBUSB_TRANSFER_FREE_BUFFER = 1<<1,
00796 
00801     LIBUSB_TRANSFER_FREE_TRANSFER = 1<<2
00802 };
00803 
00806 struct libusb_iso_packet_descriptor {
00808     unsigned int length;
00809 
00811     unsigned int actual_length;
00812 
00814     enum libusb_transfer_status status;
00815 };
00816 
00817 struct libusb_transfer;
00818 
00828 typedef void (LIBUSB_CALL *libusb_transfer_cb_fn)(struct libusb_transfer *transfer);
00829 
00836 struct libusb_transfer {
00838     libusb_device_handle *dev_handle;
00839 
00841     uint8_t flags;
00842 
00844     unsigned char endpoint;
00845 
00847     unsigned char type;
00848 
00851     unsigned int timeout;
00852 
00860     enum libusb_transfer_status status;
00861 
00863     int length;
00864 
00868     int actual_length;
00869 
00872     libusb_transfer_cb_fn callback;
00873 
00875     void *user_data;
00876 
00878     unsigned char *buffer;
00879 
00882     int num_iso_packets;
00883 
00885     struct libusb_iso_packet_descriptor iso_packet_desc
00886 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
00887     [] /* valid C99 code */
00888 #else
00889     [0] /* non-standard, but usually working code */
00890 #endif
00891     ;
00892 };
00893 
00899 enum libusb_capability {
00901     LIBUSB_CAP_HAS_CAPABILITY = 0,
00902 };
00903 
00904 int LIBUSB_CALL libusb_init(libusb_context **ctx);
00905 void LIBUSB_CALL libusb_exit(libusb_context *ctx);
00906 void LIBUSB_CALL libusb_set_debug(libusb_context *ctx, int level);
00907 int LIBUSB_CALL libusb_has_capability(uint32_t capability);
00908 const char * LIBUSB_CALL libusb_error_name(int errcode);
00909 
00910 ssize_t LIBUSB_CALL libusb_get_device_list(libusb_context *ctx,
00911     libusb_device ***list);
00912 void LIBUSB_CALL libusb_free_device_list(libusb_device **list,
00913     int unref_devices);
00914 libusb_device * LIBUSB_CALL libusb_ref_device(libusb_device *dev);
00915 void LIBUSB_CALL libusb_unref_device(libusb_device *dev);
00916 
00917 int LIBUSB_CALL libusb_get_configuration(libusb_device_handle *dev,
00918     int *config);
00919 int LIBUSB_CALL libusb_get_device_descriptor(libusb_device *dev,
00920     struct libusb_device_descriptor *desc);
00921 int LIBUSB_CALL libusb_get_active_config_descriptor(libusb_device *dev,
00922     struct libusb_config_descriptor **config);
00923 int LIBUSB_CALL libusb_get_config_descriptor(libusb_device *dev,
00924     uint8_t config_index, struct libusb_config_descriptor **config);
00925 int LIBUSB_CALL libusb_get_config_descriptor_by_value(libusb_device *dev,
00926     uint8_t bConfigurationValue, struct libusb_config_descriptor **config);
00927 void LIBUSB_CALL libusb_free_config_descriptor(
00928     struct libusb_config_descriptor *config);
00929 uint8_t LIBUSB_CALL libusb_get_bus_number(libusb_device *dev);
00930 uint8_t LIBUSB_CALL libusb_get_device_address(libusb_device *dev);
00931 int LIBUSB_CALL libusb_get_device_speed(libusb_device *dev);
00932 int LIBUSB_CALL libusb_get_max_packet_size(libusb_device *dev,
00933     unsigned char endpoint);
00934 int LIBUSB_CALL libusb_get_max_iso_packet_size(libusb_device *dev,
00935     unsigned char endpoint);
00936 
00937 int LIBUSB_CALL libusb_open(libusb_device *dev, libusb_device_handle **handle);
00938 void LIBUSB_CALL libusb_close(libusb_device_handle *dev_handle);
00939 libusb_device * LIBUSB_CALL libusb_get_device(libusb_device_handle *dev_handle);
00940 
00941 int LIBUSB_CALL libusb_set_configuration(libusb_device_handle *dev,
00942     int configuration);
00943 int LIBUSB_CALL libusb_claim_interface(libusb_device_handle *dev,
00944     int interface_number);
00945 int LIBUSB_CALL libusb_release_interface(libusb_device_handle *dev,
00946     int interface_number);
00947 
00948 libusb_device_handle * LIBUSB_CALL libusb_open_device_with_vid_pid(
00949     libusb_context *ctx, uint16_t vendor_id, uint16_t product_id);
00950 
00951 int LIBUSB_CALL libusb_set_interface_alt_setting(libusb_device_handle *dev,
00952     int interface_number, int alternate_setting);
00953 int LIBUSB_CALL libusb_clear_halt(libusb_device_handle *dev,
00954     unsigned char endpoint);
00955 int LIBUSB_CALL libusb_reset_device(libusb_device_handle *dev);
00956 
00957 int LIBUSB_CALL libusb_kernel_driver_active(libusb_device_handle *dev,
00958     int interface_number);
00959 int LIBUSB_CALL libusb_detach_kernel_driver(libusb_device_handle *dev,
00960     int interface_number);
00961 int LIBUSB_CALL libusb_attach_kernel_driver(libusb_device_handle *dev,
00962     int interface_number);
00963 
00964 /* async I/O */
00965 
00978 static inline unsigned char *libusb_control_transfer_get_data(
00979     struct libusb_transfer *transfer)
00980 {
00981     return transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE;
00982 }
00983 
00996 static inline struct libusb_control_setup *libusb_control_transfer_get_setup(
00997     struct libusb_transfer *transfer)
00998 {
00999     return (struct libusb_control_setup *) transfer->buffer;
01000 }
01001 
01024 static inline void libusb_fill_control_setup(unsigned char *buffer,
01025     uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
01026     uint16_t wLength)
01027 {
01028     struct libusb_control_setup *setup = (struct libusb_control_setup *) buffer;
01029     setup->bmRequestType = bmRequestType;
01030     setup->bRequest = bRequest;
01031     setup->wValue = libusb_cpu_to_le16(wValue);
01032     setup->wIndex = libusb_cpu_to_le16(wIndex);
01033     setup->wLength = libusb_cpu_to_le16(wLength);
01034 }
01035 
01036 struct libusb_transfer * LIBUSB_CALL libusb_alloc_transfer(int iso_packets);
01037 int LIBUSB_CALL libusb_submit_transfer(struct libusb_transfer *transfer);
01038 int LIBUSB_CALL libusb_cancel_transfer(struct libusb_transfer *transfer);
01039 void LIBUSB_CALL libusb_free_transfer(struct libusb_transfer *transfer);
01040 
01068 static inline void libusb_fill_control_transfer(
01069     struct libusb_transfer *transfer, libusb_device_handle *dev_handle,
01070     unsigned char *buffer, libusb_transfer_cb_fn callback, void *user_data,
01071     unsigned int timeout)
01072 {
01073     struct libusb_control_setup *setup = (struct libusb_control_setup *) buffer;
01074     transfer->dev_handle = dev_handle;
01075     transfer->endpoint = 0;
01076     transfer->type = LIBUSB_TRANSFER_TYPE_CONTROL;
01077     transfer->timeout = timeout;
01078     transfer->buffer = buffer;
01079     if (setup)
01080         transfer->length = LIBUSB_CONTROL_SETUP_SIZE
01081             + libusb_le16_to_cpu(setup->wLength);
01082     transfer->user_data = user_data;
01083     transfer->callback = callback;
01084 }
01085 
01099 static inline void libusb_fill_bulk_transfer(struct libusb_transfer *transfer,
01100     libusb_device_handle *dev_handle, unsigned char endpoint,
01101     unsigned char *buffer, int length, libusb_transfer_cb_fn callback,
01102     void *user_data, unsigned int timeout)
01103 {
01104     transfer->dev_handle = dev_handle;
01105     transfer->endpoint = endpoint;
01106     transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
01107     transfer->timeout = timeout;
01108     transfer->buffer = buffer;
01109     transfer->length = length;
01110     transfer->user_data = user_data;
01111     transfer->callback = callback;
01112 }
01113 
01127 static inline void libusb_fill_interrupt_transfer(
01128     struct libusb_transfer *transfer, libusb_device_handle *dev_handle,
01129     unsigned char endpoint, unsigned char *buffer, int length,
01130     libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
01131 {
01132     transfer->dev_handle = dev_handle;
01133     transfer->endpoint = endpoint;
01134     transfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT;
01135     transfer->timeout = timeout;
01136     transfer->buffer = buffer;
01137     transfer->length = length;
01138     transfer->user_data = user_data;
01139     transfer->callback = callback;
01140 }
01141 
01156 static inline void libusb_fill_iso_transfer(struct libusb_transfer *transfer,
01157     libusb_device_handle *dev_handle, unsigned char endpoint,
01158     unsigned char *buffer, int length, int num_iso_packets,
01159     libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
01160 {
01161     transfer->dev_handle = dev_handle;
01162     transfer->endpoint = endpoint;
01163     transfer->type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS;
01164     transfer->timeout = timeout;
01165     transfer->buffer = buffer;
01166     transfer->length = length;
01167     transfer->num_iso_packets = num_iso_packets;
01168     transfer->user_data = user_data;
01169     transfer->callback = callback;
01170 }
01171 
01180 static inline void libusb_set_iso_packet_lengths(
01181     struct libusb_transfer *transfer, unsigned int length)
01182 {
01183     int i;
01184     for (i = 0; i < transfer->num_iso_packets; i++)
01185         transfer->iso_packet_desc[i].length = length;
01186 }
01187 
01204 static inline unsigned char *libusb_get_iso_packet_buffer(
01205     struct libusb_transfer *transfer, unsigned int packet)
01206 {
01207     int i;
01208     size_t offset = 0;
01209     int _packet;
01210 
01211     /* oops..slight bug in the API. packet is an unsigned int, but we use
01212      * signed integers almost everywhere else. range-check and convert to
01213      * signed to avoid compiler warnings. FIXME for libusb-2. */
01214     if (packet > INT_MAX)
01215         return NULL;
01216     _packet = packet;
01217 
01218     if (_packet >= transfer->num_iso_packets)
01219         return NULL;
01220 
01221     for (i = 0; i < _packet; i++)
01222         offset += transfer->iso_packet_desc[i].length;
01223 
01224     return transfer->buffer + offset;
01225 }
01226 
01246 static inline unsigned char *libusb_get_iso_packet_buffer_simple(
01247     struct libusb_transfer *transfer, unsigned int packet)
01248 {
01249     int _packet;
01250 
01251     /* oops..slight bug in the API. packet is an unsigned int, but we use
01252      * signed integers almost everywhere else. range-check and convert to
01253      * signed to avoid compiler warnings. FIXME for libusb-2. */
01254     if (packet > INT_MAX)
01255         return NULL;
01256     _packet = packet;
01257 
01258     if (_packet >= transfer->num_iso_packets)
01259         return NULL;
01260 
01261     return transfer->buffer + (transfer->iso_packet_desc[0].length * _packet);
01262 }
01263 
01264 /* sync I/O */
01265 
01266 int LIBUSB_CALL libusb_control_transfer(libusb_device_handle *dev_handle,
01267     uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
01268     unsigned char *data, uint16_t wLength, unsigned int timeout);
01269 
01270 int LIBUSB_CALL libusb_bulk_transfer(libusb_device_handle *dev_handle,
01271     unsigned char endpoint, unsigned char *data, int length,
01272     int *actual_length, unsigned int timeout);
01273 
01274 int LIBUSB_CALL libusb_interrupt_transfer(libusb_device_handle *dev_handle,
01275     unsigned char endpoint, unsigned char *data, int length,
01276     int *actual_length, unsigned int timeout);
01277 
01290 static inline int libusb_get_descriptor(libusb_device_handle *dev,
01291     uint8_t desc_type, uint8_t desc_index, unsigned char *data, int length)
01292 {
01293     return libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN,
01294         LIBUSB_REQUEST_GET_DESCRIPTOR, (desc_type << 8) | desc_index, 0, data,
01295         (uint16_t) length, 1000);
01296 }
01297 
01312 static inline int libusb_get_string_descriptor(libusb_device_handle *dev,
01313     uint8_t desc_index, uint16_t langid, unsigned char *data, int length)
01314 {
01315     return libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN,
01316         LIBUSB_REQUEST_GET_DESCRIPTOR, (LIBUSB_DT_STRING << 8) | desc_index,
01317         langid, data, (uint16_t) length, 1000);
01318 }
01319 
01320 int LIBUSB_CALL libusb_get_string_descriptor_ascii(libusb_device_handle *dev,
01321     uint8_t desc_index, unsigned char *data, int length);
01322 
01323 /* polling and timeouts */
01324 
01325 int LIBUSB_CALL libusb_try_lock_events(libusb_context *ctx);
01326 void LIBUSB_CALL libusb_lock_events(libusb_context *ctx);
01327 void LIBUSB_CALL libusb_unlock_events(libusb_context *ctx);
01328 int LIBUSB_CALL libusb_event_handling_ok(libusb_context *ctx);
01329 int LIBUSB_CALL libusb_event_handler_active(libusb_context *ctx);
01330 void LIBUSB_CALL libusb_lock_event_waiters(libusb_context *ctx);
01331 void LIBUSB_CALL libusb_unlock_event_waiters(libusb_context *ctx);
01332 int LIBUSB_CALL libusb_wait_for_event(libusb_context *ctx, struct timeval *tv);
01333 
01334 int LIBUSB_CALL libusb_handle_events_timeout(libusb_context *ctx,
01335     struct timeval *tv);
01336 int LIBUSB_CALL libusb_handle_events_timeout_completed(libusb_context *ctx,
01337     struct timeval *tv, int *completed);
01338 int LIBUSB_CALL libusb_handle_events(libusb_context *ctx);
01339 int LIBUSB_CALL libusb_handle_events_completed(libusb_context *ctx, int *completed);
01340 int LIBUSB_CALL libusb_handle_events_locked(libusb_context *ctx,
01341     struct timeval *tv);
01342 int LIBUSB_CALL libusb_pollfds_handle_timeouts(libusb_context *ctx);
01343 int LIBUSB_CALL libusb_get_next_timeout(libusb_context *ctx,
01344     struct timeval *tv);
01345 
01349 struct libusb_pollfd {
01351     int fd;
01352 
01357     short events;
01358 };
01359 
01370 typedef void (LIBUSB_CALL *libusb_pollfd_added_cb)(int fd, short events,
01371     void *user_data);
01372 
01382 typedef void (LIBUSB_CALL *libusb_pollfd_removed_cb)(int fd, void *user_data);
01383 
01384 const struct libusb_pollfd ** LIBUSB_CALL libusb_get_pollfds(
01385     libusb_context *ctx);
01386 void LIBUSB_CALL libusb_set_pollfd_notifiers(libusb_context *ctx,
01387     libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb,
01388     void *user_data);
01389 
01390 #ifdef __cplusplus
01391 }
01392 #endif
01393 
01394 #endif