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 #else
00044 #include <stdint.h>
00045 #endif
00046 
00047 #include <sys/types.h>
00048 #include <time.h>
00049 #include <limits.h>
00050 
00051 #if defined(__linux) || defined(__APPLE__) || defined(__CYGWIN__)
00052 #include <sys/time.h>
00053 #endif
00054 
00055 /* 'interface' might be defined as a macro on Windows, so we need to
00056  * undefine it so as not to break the current libusb API, because
00057  * libusb_config_descriptor has an 'interface' member
00058  * As this can be problematic if you include windows.h after libusb.h
00059  * in your sources, we force windows.h to be included first. */
00060 #if defined(_WIN32) || defined(__CYGWIN__)
00061 #include <windows.h>
00062 #if defined(interface)
00063 #undef interface
00064 #endif
00065 #endif
00066 
00092 /* LIBUSB_CALL must be defined on both definition and declaration of libusb
00093  * functions. You'd think that declaration would be enough, but cygwin will
00094  * complain about conflicting types unless both are marked this way.
00095  * The placement of this macro is important too; it must appear after the
00096  * return type, before the function name. See internal documentation for
00097  * API_EXPORTED.
00098  */
00099 #if defined(_WIN32) || defined(__CYGWIN__)
00100 #define LIBUSB_CALL WINAPI
00101 #else
00102 #define LIBUSB_CALL
00103 #endif
00104 
00105 #ifdef __cplusplus
00106 extern "C" {
00107 #endif
00108 
00117 static inline uint16_t libusb_cpu_to_le16(const uint16_t x)
00118 {
00119     union {
00120         uint8_t  b8[2];
00121         uint16_t b16;
00122     } _tmp;
00123     _tmp.b8[1] = x >> 8;
00124     _tmp.b8[0] = x & 0xff;
00125     return _tmp.b16;
00126 }
00127 
00136 #define libusb_le16_to_cpu libusb_cpu_to_le16
00137 
00138 /* standard USB stuff */
00139 
00142 enum libusb_class_code {
00147     LIBUSB_CLASS_PER_INTERFACE = 0,
00148 
00150     LIBUSB_CLASS_AUDIO = 1,
00151 
00153     LIBUSB_CLASS_COMM = 2,
00154 
00156     LIBUSB_CLASS_HID = 3,
00157 
00159     LIBUSB_CLASS_PHYSICAL = 5,
00160 
00162     LIBUSB_CLASS_PRINTER = 7,
00163 
00165     LIBUSB_CLASS_PTP = 6, /* legacy name from libusb-0.1 usb.h */
00166     LIBUSB_CLASS_IMAGE = 6,
00167 
00169     LIBUSB_CLASS_MASS_STORAGE = 8,
00170 
00172     LIBUSB_CLASS_HUB = 9,
00173 
00175     LIBUSB_CLASS_DATA = 10,
00176 
00178     LIBUSB_CLASS_SMART_CARD = 0x0b,
00179 
00181     LIBUSB_CLASS_CONTENT_SECURITY = 0x0d,
00182 
00184     LIBUSB_CLASS_VIDEO = 0x0e,
00185 
00187     LIBUSB_CLASS_PERSONAL_HEALTHCARE = 0x0f,
00188 
00190     LIBUSB_CLASS_DIAGNOSTIC_DEVICE = 0xdc,
00191 
00193     LIBUSB_CLASS_WIRELESS = 0xe0,
00194 
00196     LIBUSB_CLASS_APPLICATION = 0xfe,
00197 
00199     LIBUSB_CLASS_VENDOR_SPEC = 0xff
00200 };
00201 
00204 enum libusb_descriptor_type {
00206     LIBUSB_DT_DEVICE = 0x01,
00207 
00209     LIBUSB_DT_CONFIG = 0x02,
00210 
00212     LIBUSB_DT_STRING = 0x03,
00213 
00215     LIBUSB_DT_INTERFACE = 0x04,
00216 
00218     LIBUSB_DT_ENDPOINT = 0x05,
00219 
00221     LIBUSB_DT_HID = 0x21,
00222 
00224     LIBUSB_DT_REPORT = 0x22,
00225 
00227     LIBUSB_DT_PHYSICAL = 0x23,
00228 
00230     LIBUSB_DT_HUB = 0x29
00231 };
00232 
00233 /* Descriptor sizes per descriptor type */
00234 #define LIBUSB_DT_DEVICE_SIZE           18
00235 #define LIBUSB_DT_CONFIG_SIZE           9
00236 #define LIBUSB_DT_INTERFACE_SIZE        9
00237 #define LIBUSB_DT_ENDPOINT_SIZE     7
00238 #define LIBUSB_DT_ENDPOINT_AUDIO_SIZE   9   /* Audio extension */
00239 #define LIBUSB_DT_HUB_NONVAR_SIZE       7
00240 
00241 #define LIBUSB_ENDPOINT_ADDRESS_MASK    0x0f    /* in bEndpointAddress */
00242 #define LIBUSB_ENDPOINT_DIR_MASK        0x80
00243 
00248 enum libusb_endpoint_direction {
00250     LIBUSB_ENDPOINT_IN = 0x80,
00251 
00253     LIBUSB_ENDPOINT_OUT = 0x00
00254 };
00255 
00256 #define LIBUSB_TRANSFER_TYPE_MASK           0x03    /* in bmAttributes */
00257 
00262 enum libusb_transfer_type {
00264     LIBUSB_TRANSFER_TYPE_CONTROL = 0,
00265 
00267     LIBUSB_TRANSFER_TYPE_ISOCHRONOUS = 1,
00268 
00270     LIBUSB_TRANSFER_TYPE_BULK = 2,
00271 
00273     LIBUSB_TRANSFER_TYPE_INTERRUPT = 3
00274 };
00275 
00278 enum libusb_standard_request {
00280     LIBUSB_REQUEST_GET_STATUS = 0x00,
00281 
00283     LIBUSB_REQUEST_CLEAR_FEATURE = 0x01,
00284 
00285     /* 0x02 is reserved */
00286 
00288     LIBUSB_REQUEST_SET_FEATURE = 0x03,
00289 
00290     /* 0x04 is reserved */
00291 
00293     LIBUSB_REQUEST_SET_ADDRESS = 0x05,
00294 
00296     LIBUSB_REQUEST_GET_DESCRIPTOR = 0x06,
00297 
00299     LIBUSB_REQUEST_SET_DESCRIPTOR = 0x07,
00300 
00302     LIBUSB_REQUEST_GET_CONFIGURATION = 0x08,
00303 
00305     LIBUSB_REQUEST_SET_CONFIGURATION = 0x09,
00306 
00308     LIBUSB_REQUEST_GET_INTERFACE = 0x0A,
00309 
00311     LIBUSB_REQUEST_SET_INTERFACE = 0x0B,
00312 
00314     LIBUSB_REQUEST_SYNCH_FRAME = 0x0C
00315 };
00316 
00321 enum libusb_request_type {
00323     LIBUSB_REQUEST_TYPE_STANDARD = (0x00 << 5),
00324 
00326     LIBUSB_REQUEST_TYPE_CLASS = (0x01 << 5),
00327 
00329     LIBUSB_REQUEST_TYPE_VENDOR = (0x02 << 5),
00330 
00332     LIBUSB_REQUEST_TYPE_RESERVED = (0x03 << 5)
00333 };
00334 
00339 enum libusb_request_recipient {
00341     LIBUSB_RECIPIENT_DEVICE = 0x00,
00342 
00344     LIBUSB_RECIPIENT_INTERFACE = 0x01,
00345 
00347     LIBUSB_RECIPIENT_ENDPOINT = 0x02,
00348 
00350     LIBUSB_RECIPIENT_OTHER = 0x03
00351 };
00352 
00353 #define LIBUSB_ISO_SYNC_TYPE_MASK       0x0C
00354 
00360 enum libusb_iso_sync_type {
00362     LIBUSB_ISO_SYNC_TYPE_NONE = 0,
00363 
00365     LIBUSB_ISO_SYNC_TYPE_ASYNC = 1,
00366 
00368     LIBUSB_ISO_SYNC_TYPE_ADAPTIVE = 2,
00369 
00371     LIBUSB_ISO_SYNC_TYPE_SYNC = 3
00372 };
00373 
00374 #define LIBUSB_ISO_USAGE_TYPE_MASK 0x30
00375 
00381 enum libusb_iso_usage_type {
00383     LIBUSB_ISO_USAGE_TYPE_DATA = 0,
00384 
00386     LIBUSB_ISO_USAGE_TYPE_FEEDBACK = 1,
00387 
00389     LIBUSB_ISO_USAGE_TYPE_IMPLICIT = 2
00390 };
00391 
00397 struct libusb_device_descriptor {
00399     uint8_t  bLength;
00400 
00404     uint8_t  bDescriptorType;
00405 
00408     uint16_t bcdUSB;
00409 
00411     uint8_t  bDeviceClass;
00412 
00415     uint8_t  bDeviceSubClass;
00416 
00419     uint8_t  bDeviceProtocol;
00420 
00422     uint8_t  bMaxPacketSize0;
00423 
00425     uint16_t idVendor;
00426 
00428     uint16_t idProduct;
00429 
00431     uint16_t bcdDevice;
00432 
00434     uint8_t  iManufacturer;
00435 
00437     uint8_t  iProduct;
00438 
00440     uint8_t  iSerialNumber;
00441 
00443     uint8_t  bNumConfigurations;
00444 };
00445 
00451 struct libusb_endpoint_descriptor {
00453     uint8_t  bLength;
00454 
00458     uint8_t  bDescriptorType;
00459 
00464     uint8_t  bEndpointAddress;
00465 
00473     uint8_t  bmAttributes;
00474 
00476     uint16_t wMaxPacketSize;
00477 
00479     uint8_t  bInterval;
00480 
00483     uint8_t  bRefresh;
00484 
00486     uint8_t  bSynchAddress;
00487 
00490     const unsigned char *extra;
00491 
00493     int extra_length;
00494 };
00495 
00501 struct libusb_interface_descriptor {
00503     uint8_t  bLength;
00504 
00508     uint8_t  bDescriptorType;
00509 
00511     uint8_t  bInterfaceNumber;
00512 
00514     uint8_t  bAlternateSetting;
00515 
00518     uint8_t  bNumEndpoints;
00519 
00521     uint8_t  bInterfaceClass;
00522 
00525     uint8_t  bInterfaceSubClass;
00526 
00529     uint8_t  bInterfaceProtocol;
00530 
00532     uint8_t  iInterface;
00533 
00536     const struct libusb_endpoint_descriptor *endpoint;
00537 
00540     const unsigned char *extra;
00541 
00543     int extra_length;
00544 };
00545 
00549 struct libusb_interface {
00552     const struct libusb_interface_descriptor *altsetting;
00553 
00555     int num_altsetting;
00556 };
00557 
00563 struct libusb_config_descriptor {
00565     uint8_t  bLength;
00566 
00570     uint8_t  bDescriptorType;
00571 
00573     uint16_t wTotalLength;
00574 
00576     uint8_t  bNumInterfaces;
00577 
00579     uint8_t  bConfigurationValue;
00580 
00582     uint8_t  iConfiguration;
00583 
00585     uint8_t  bmAttributes;
00586 
00590     uint8_t  MaxPower;
00591 
00594     const struct libusb_interface *interface;
00595 
00598     const unsigned char *extra;
00599 
00601     int extra_length;
00602 };
00603 
00606 struct libusb_control_setup {
00612     uint8_t  bmRequestType;
00613 
00619     uint8_t  bRequest;
00620 
00622     uint16_t wValue;
00623 
00626     uint16_t wIndex;
00627 
00629     uint16_t wLength;
00630 };
00631 
00632 #define LIBUSB_CONTROL_SETUP_SIZE (sizeof(struct libusb_control_setup))
00633 
00634 /* libusb */
00635 
00636 struct libusb_context;
00637 struct libusb_device;
00638 struct libusb_device_handle;
00639 
00657 typedef struct libusb_context libusb_context;
00658 
00674 typedef struct libusb_device libusb_device;
00675 
00676 
00685 typedef struct libusb_device_handle libusb_device_handle;
00686 
00690 enum libusb_speed {
00692     LIBUSB_SPEED_UNKNOWN = 0,
00693 
00695     LIBUSB_SPEED_LOW = 1,
00696 
00698     LIBUSB_SPEED_FULL = 2,
00699 
00701     LIBUSB_SPEED_HIGH = 3,
00702 
00704     LIBUSB_SPEED_SUPER = 4,
00705 };
00706 
00711 enum libusb_error {
00713     LIBUSB_SUCCESS = 0,
00714 
00716     LIBUSB_ERROR_IO = -1,
00717 
00719     LIBUSB_ERROR_INVALID_PARAM = -2,
00720 
00722     LIBUSB_ERROR_ACCESS = -3,
00723 
00725     LIBUSB_ERROR_NO_DEVICE = -4,
00726 
00728     LIBUSB_ERROR_NOT_FOUND = -5,
00729 
00731     LIBUSB_ERROR_BUSY = -6,
00732 
00734     LIBUSB_ERROR_TIMEOUT = -7,
00735 
00737     LIBUSB_ERROR_OVERFLOW = -8,
00738 
00740     LIBUSB_ERROR_PIPE = -9,
00741 
00743     LIBUSB_ERROR_INTERRUPTED = -10,
00744 
00746     LIBUSB_ERROR_NO_MEM = -11,
00747 
00749     LIBUSB_ERROR_NOT_SUPPORTED = -12,
00750 
00752     LIBUSB_ERROR_OTHER = -99
00753 };
00754 
00757 enum libusb_transfer_status {
00760     LIBUSB_TRANSFER_COMPLETED,
00761 
00763     LIBUSB_TRANSFER_ERROR,
00764 
00766     LIBUSB_TRANSFER_TIMED_OUT,
00767 
00769     LIBUSB_TRANSFER_CANCELLED,
00770 
00773     LIBUSB_TRANSFER_STALL,
00774 
00776     LIBUSB_TRANSFER_NO_DEVICE,
00777 
00779     LIBUSB_TRANSFER_OVERFLOW
00780 };
00781 
00784 enum libusb_transfer_flags {
00786     LIBUSB_TRANSFER_SHORT_NOT_OK = 1<<0,
00787 
00789     LIBUSB_TRANSFER_FREE_BUFFER = 1<<1,
00790 
00795     LIBUSB_TRANSFER_FREE_TRANSFER = 1<<2
00796 };
00797 
00800 struct libusb_iso_packet_descriptor {
00802     unsigned int length;
00803 
00805     unsigned int actual_length;
00806 
00808     enum libusb_transfer_status status;
00809 };
00810 
00811 struct libusb_transfer;
00812 
00822 typedef void (LIBUSB_CALL *libusb_transfer_cb_fn)(struct libusb_transfer *transfer);
00823 
00830 struct libusb_transfer {
00832     libusb_device_handle *dev_handle;
00833 
00835     uint8_t flags;
00836 
00838     unsigned char endpoint;
00839 
00841     unsigned char type;
00842 
00845     unsigned int timeout;
00846 
00854     enum libusb_transfer_status status;
00855 
00857     int length;
00858 
00862     int actual_length;
00863 
00866     libusb_transfer_cb_fn callback;
00867 
00869     void *user_data;
00870 
00872     unsigned char *buffer;
00873 
00876     int num_iso_packets;
00877 
00879     struct libusb_iso_packet_descriptor iso_packet_desc
00880 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
00881     [] /* valid C99 code */
00882 #else
00883     [0] /* non-standard, but usually working code */
00884 #endif
00885     ;
00886 };
00887 
00888 int LIBUSB_CALL libusb_init(libusb_context **ctx);
00889 void LIBUSB_CALL libusb_exit(libusb_context *ctx);
00890 void LIBUSB_CALL libusb_set_debug(libusb_context *ctx, int level);
00891 
00892 ssize_t LIBUSB_CALL libusb_get_device_list(libusb_context *ctx,
00893     libusb_device ***list);
00894 void LIBUSB_CALL libusb_free_device_list(libusb_device **list,
00895     int unref_devices);
00896 libusb_device * LIBUSB_CALL libusb_ref_device(libusb_device *dev);
00897 void LIBUSB_CALL libusb_unref_device(libusb_device *dev);
00898 
00899 int LIBUSB_CALL libusb_get_configuration(libusb_device_handle *dev,
00900     int *config);
00901 int LIBUSB_CALL libusb_get_device_descriptor(libusb_device *dev,
00902     struct libusb_device_descriptor *desc);
00903 int LIBUSB_CALL libusb_get_active_config_descriptor(libusb_device *dev,
00904     struct libusb_config_descriptor **config);
00905 int LIBUSB_CALL libusb_get_config_descriptor(libusb_device *dev,
00906     uint8_t config_index, struct libusb_config_descriptor **config);
00907 int LIBUSB_CALL libusb_get_config_descriptor_by_value(libusb_device *dev,
00908     uint8_t bConfigurationValue, struct libusb_config_descriptor **config);
00909 void LIBUSB_CALL libusb_free_config_descriptor(
00910     struct libusb_config_descriptor *config);
00911 uint8_t LIBUSB_CALL libusb_get_bus_number(libusb_device *dev);
00912 uint8_t LIBUSB_CALL libusb_get_device_address(libusb_device *dev);
00913 enum libusb_speed LIBUSB_CALL libusb_get_device_speed(libusb_device *dev);
00914 int LIBUSB_CALL libusb_get_max_packet_size(libusb_device *dev,
00915     unsigned char endpoint);
00916 int LIBUSB_CALL libusb_get_max_iso_packet_size(libusb_device *dev,
00917     unsigned char endpoint);
00918 
00919 int LIBUSB_CALL libusb_open(libusb_device *dev, libusb_device_handle **handle);
00920 void LIBUSB_CALL libusb_close(libusb_device_handle *dev_handle);
00921 libusb_device * LIBUSB_CALL libusb_get_device(libusb_device_handle *dev_handle);
00922 
00923 int LIBUSB_CALL libusb_set_configuration(libusb_device_handle *dev,
00924     int configuration);
00925 int LIBUSB_CALL libusb_claim_interface(libusb_device_handle *dev,
00926     int interface_number);
00927 int LIBUSB_CALL libusb_release_interface(libusb_device_handle *dev,
00928     int interface_number);
00929 
00930 libusb_device_handle * LIBUSB_CALL libusb_open_device_with_vid_pid(
00931     libusb_context *ctx, uint16_t vendor_id, uint16_t product_id);
00932 
00933 int LIBUSB_CALL libusb_set_interface_alt_setting(libusb_device_handle *dev,
00934     int interface_number, int alternate_setting);
00935 int LIBUSB_CALL libusb_clear_halt(libusb_device_handle *dev,
00936     unsigned char endpoint);
00937 int LIBUSB_CALL libusb_reset_device(libusb_device_handle *dev);
00938 
00939 int LIBUSB_CALL libusb_kernel_driver_active(libusb_device_handle *dev,
00940     int interface_number);
00941 int LIBUSB_CALL libusb_detach_kernel_driver(libusb_device_handle *dev,
00942     int interface_number);
00943 int LIBUSB_CALL libusb_attach_kernel_driver(libusb_device_handle *dev,
00944     int interface_number);
00945 
00946 /* async I/O */
00947 
00960 static inline unsigned char *libusb_control_transfer_get_data(
00961     struct libusb_transfer *transfer)
00962 {
00963     return transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE;
00964 }
00965 
00978 static inline struct libusb_control_setup *libusb_control_transfer_get_setup(
00979     struct libusb_transfer *transfer)
00980 {
00981     return (struct libusb_control_setup *) transfer->buffer;
00982 }
00983 
01006 static inline void libusb_fill_control_setup(unsigned char *buffer,
01007     uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
01008     uint16_t wLength)
01009 {
01010     struct libusb_control_setup *setup = (struct libusb_control_setup *) buffer;
01011     setup->bmRequestType = bmRequestType;
01012     setup->bRequest = bRequest;
01013     setup->wValue = libusb_cpu_to_le16(wValue);
01014     setup->wIndex = libusb_cpu_to_le16(wIndex);
01015     setup->wLength = libusb_cpu_to_le16(wLength);
01016 }
01017 
01018 struct libusb_transfer * LIBUSB_CALL libusb_alloc_transfer(int iso_packets);
01019 int LIBUSB_CALL libusb_submit_transfer(struct libusb_transfer *transfer);
01020 int LIBUSB_CALL libusb_cancel_transfer(struct libusb_transfer *transfer);
01021 void LIBUSB_CALL libusb_free_transfer(struct libusb_transfer *transfer);
01022 
01050 static inline void libusb_fill_control_transfer(
01051     struct libusb_transfer *transfer, libusb_device_handle *dev_handle,
01052     unsigned char *buffer, libusb_transfer_cb_fn callback, void *user_data,
01053     unsigned int timeout)
01054 {
01055     struct libusb_control_setup *setup = (struct libusb_control_setup *) buffer;
01056     transfer->dev_handle = dev_handle;
01057     transfer->endpoint = 0;
01058     transfer->type = LIBUSB_TRANSFER_TYPE_CONTROL;
01059     transfer->timeout = timeout;
01060     transfer->buffer = buffer;
01061     if (setup)
01062         transfer->length = LIBUSB_CONTROL_SETUP_SIZE
01063             + libusb_le16_to_cpu(setup->wLength);
01064     transfer->user_data = user_data;
01065     transfer->callback = callback;
01066 }
01067 
01081 static inline void libusb_fill_bulk_transfer(struct libusb_transfer *transfer,
01082     libusb_device_handle *dev_handle, unsigned char endpoint,
01083     unsigned char *buffer, int length, libusb_transfer_cb_fn callback,
01084     void *user_data, unsigned int timeout)
01085 {
01086     transfer->dev_handle = dev_handle;
01087     transfer->endpoint = endpoint;
01088     transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
01089     transfer->timeout = timeout;
01090     transfer->buffer = buffer;
01091     transfer->length = length;
01092     transfer->user_data = user_data;
01093     transfer->callback = callback;
01094 }
01095 
01109 static inline void libusb_fill_interrupt_transfer(
01110     struct libusb_transfer *transfer, libusb_device_handle *dev_handle,
01111     unsigned char endpoint, unsigned char *buffer, int length,
01112     libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
01113 {
01114     transfer->dev_handle = dev_handle;
01115     transfer->endpoint = endpoint;
01116     transfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT;
01117     transfer->timeout = timeout;
01118     transfer->buffer = buffer;
01119     transfer->length = length;
01120     transfer->user_data = user_data;
01121     transfer->callback = callback;
01122 }
01123 
01138 static inline void libusb_fill_iso_transfer(struct libusb_transfer *transfer,
01139     libusb_device_handle *dev_handle, unsigned char endpoint,
01140     unsigned char *buffer, int length, int num_iso_packets,
01141     libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
01142 {
01143     transfer->dev_handle = dev_handle;
01144     transfer->endpoint = endpoint;
01145     transfer->type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS;
01146     transfer->timeout = timeout;
01147     transfer->buffer = buffer;
01148     transfer->length = length;
01149     transfer->num_iso_packets = num_iso_packets;
01150     transfer->user_data = user_data;
01151     transfer->callback = callback;
01152 }
01153 
01162 static inline void libusb_set_iso_packet_lengths(
01163     struct libusb_transfer *transfer, unsigned int length)
01164 {
01165     int i;
01166     for (i = 0; i < transfer->num_iso_packets; i++)
01167         transfer->iso_packet_desc[i].length = length;
01168 }
01169 
01186 static inline unsigned char *libusb_get_iso_packet_buffer(
01187     struct libusb_transfer *transfer, unsigned int packet)
01188 {
01189     int i;
01190     size_t offset = 0;
01191     int _packet;
01192 
01193     /* oops..slight bug in the API. packet is an unsigned int, but we use
01194      * signed integers almost everywhere else. range-check and convert to
01195      * signed to avoid compiler warnings. FIXME for libusb-2. */
01196     if (packet > INT_MAX)
01197         return NULL;
01198     _packet = packet;
01199 
01200     if (_packet >= transfer->num_iso_packets)
01201         return NULL;
01202 
01203     for (i = 0; i < _packet; i++)
01204         offset += transfer->iso_packet_desc[i].length;
01205 
01206     return transfer->buffer + offset;
01207 }
01208 
01228 static inline unsigned char *libusb_get_iso_packet_buffer_simple(
01229     struct libusb_transfer *transfer, unsigned int packet)
01230 {
01231     int _packet;
01232 
01233     /* oops..slight bug in the API. packet is an unsigned int, but we use
01234      * signed integers almost everywhere else. range-check and convert to
01235      * signed to avoid compiler warnings. FIXME for libusb-2. */
01236     if (packet > INT_MAX)
01237         return NULL;
01238     _packet = packet;
01239 
01240     if (_packet >= transfer->num_iso_packets)
01241         return NULL;
01242 
01243     return transfer->buffer + (transfer->iso_packet_desc[0].length * _packet);
01244 }
01245 
01246 /* sync I/O */
01247 
01248 int LIBUSB_CALL libusb_control_transfer(libusb_device_handle *dev_handle,
01249     uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
01250     unsigned char *data, uint16_t wLength, unsigned int timeout);
01251 
01252 int LIBUSB_CALL libusb_bulk_transfer(libusb_device_handle *dev_handle,
01253     unsigned char endpoint, unsigned char *data, int length,
01254     int *actual_length, unsigned int timeout);
01255 
01256 int LIBUSB_CALL libusb_interrupt_transfer(libusb_device_handle *dev_handle,
01257     unsigned char endpoint, unsigned char *data, int length,
01258     int *actual_length, unsigned int timeout);
01259 
01272 static inline int libusb_get_descriptor(libusb_device_handle *dev,
01273     uint8_t desc_type, uint8_t desc_index, unsigned char *data, int length)
01274 {
01275     return libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN,
01276         LIBUSB_REQUEST_GET_DESCRIPTOR, (desc_type << 8) | desc_index, 0, data,
01277         (uint16_t) length, 1000);
01278 }
01279 
01294 static inline int libusb_get_string_descriptor(libusb_device_handle *dev,
01295     uint8_t desc_index, uint16_t langid, unsigned char *data, int length)
01296 {
01297     return libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN,
01298         LIBUSB_REQUEST_GET_DESCRIPTOR, (LIBUSB_DT_STRING << 8) | desc_index,
01299         langid, data, (uint16_t) length, 1000);
01300 }
01301 
01302 int LIBUSB_CALL libusb_get_string_descriptor_ascii(libusb_device_handle *dev,
01303     uint8_t desc_index, unsigned char *data, int length);
01304 
01305 /* polling and timeouts */
01306 
01307 int LIBUSB_CALL libusb_try_lock_events(libusb_context *ctx);
01308 void LIBUSB_CALL libusb_lock_events(libusb_context *ctx);
01309 void LIBUSB_CALL libusb_unlock_events(libusb_context *ctx);
01310 int LIBUSB_CALL libusb_event_handling_ok(libusb_context *ctx);
01311 int LIBUSB_CALL libusb_event_handler_active(libusb_context *ctx);
01312 void LIBUSB_CALL libusb_lock_event_waiters(libusb_context *ctx);
01313 void LIBUSB_CALL libusb_unlock_event_waiters(libusb_context *ctx);
01314 int LIBUSB_CALL libusb_wait_for_event(libusb_context *ctx, struct timeval *tv);
01315 
01316 int LIBUSB_CALL libusb_handle_events_timeout(libusb_context *ctx,
01317     struct timeval *tv);
01318 int LIBUSB_CALL libusb_handle_events(libusb_context *ctx);
01319 int LIBUSB_CALL libusb_handle_events_locked(libusb_context *ctx,
01320     struct timeval *tv);
01321 int LIBUSB_CALL libusb_pollfds_handle_timeouts(libusb_context *ctx);
01322 int LIBUSB_CALL libusb_get_next_timeout(libusb_context *ctx,
01323     struct timeval *tv);
01324 
01328 struct libusb_pollfd {
01330     int fd;
01331 
01336     short events;
01337 };
01338 
01349 typedef void (LIBUSB_CALL *libusb_pollfd_added_cb)(int fd, short events,
01350     void *user_data);
01351 
01361 typedef void (LIBUSB_CALL *libusb_pollfd_removed_cb)(int fd, void *user_data);
01362 
01363 const struct libusb_pollfd ** LIBUSB_CALL libusb_get_pollfds(
01364     libusb_context *ctx);
01365 void LIBUSB_CALL libusb_set_pollfd_notifiers(libusb_context *ctx,
01366     libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb,
01367     void *user_data);
01368 
01369 #ifdef __cplusplus
01370 }
01371 #endif
01372 
01373 #endif