00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef __LIBUSB_H__
00022 #define __LIBUSB_H__
00023
00024 #include <stdint.h>
00025 #include <sys/time.h>
00026 #include <sys/types.h>
00027 #include <time.h>
00028 #include <limits.h>
00029
00030 #ifdef __cplusplus
00031 extern "C" {
00032 #endif
00033
00042 #define libusb_cpu_to_le16(x) ({ \
00043 union { \
00044 uint8_t b8[2]; \
00045 uint16_t b16; \
00046 } _tmp; \
00047 uint16_t _tmp2 = (uint16_t)(x); \
00048 _tmp.b8[1] = _tmp2 >> 8; \
00049 _tmp.b8[0] = _tmp2 & 0xff; \
00050 _tmp.b16; \
00051 })
00052
00061 #define libusb_le16_to_cpu libusb_cpu_to_le16
00062
00063
00064
00067 enum libusb_class_code {
00072 LIBUSB_CLASS_PER_INTERFACE = 0,
00073
00075 LIBUSB_CLASS_AUDIO = 1,
00076
00078 LIBUSB_CLASS_COMM = 2,
00079
00081 LIBUSB_CLASS_HID = 3,
00082
00084 LIBUSB_CLASS_PRINTER = 7,
00085
00087 LIBUSB_CLASS_PTP = 6,
00088
00090 LIBUSB_CLASS_MASS_STORAGE = 8,
00091
00093 LIBUSB_CLASS_HUB = 9,
00094
00096 LIBUSB_CLASS_DATA = 10,
00097
00099 LIBUSB_CLASS_WIRELESS = 0xe0,
00100
00102 LIBUSB_CLASS_APPLICATION = 0xfe,
00103
00105 LIBUSB_CLASS_VENDOR_SPEC = 0xff
00106 };
00107
00110 enum libusb_descriptor_type {
00112 LIBUSB_DT_DEVICE = 0x01,
00113
00115 LIBUSB_DT_CONFIG = 0x02,
00116
00118 LIBUSB_DT_STRING = 0x03,
00119
00121 LIBUSB_DT_INTERFACE = 0x04,
00122
00124 LIBUSB_DT_ENDPOINT = 0x05,
00125
00127 LIBUSB_DT_HID = 0x21,
00128
00130 LIBUSB_DT_REPORT = 0x22,
00131
00133 LIBUSB_DT_PHYSICAL = 0x23,
00134
00136 LIBUSB_DT_HUB = 0x29
00137 };
00138
00139
00140 #define LIBUSB_DT_DEVICE_SIZE 18
00141 #define LIBUSB_DT_CONFIG_SIZE 9
00142 #define LIBUSB_DT_INTERFACE_SIZE 9
00143 #define LIBUSB_DT_ENDPOINT_SIZE 7
00144 #define LIBUSB_DT_ENDPOINT_AUDIO_SIZE 9
00145 #define LIBUSB_DT_HUB_NONVAR_SIZE 7
00146
00147 #define LIBUSB_ENDPOINT_ADDRESS_MASK 0x0f
00148 #define LIBUSB_ENDPOINT_DIR_MASK 0x80
00149
00154 enum libusb_endpoint_direction {
00156 LIBUSB_ENDPOINT_IN = 0x80,
00157
00159 LIBUSB_ENDPOINT_OUT = 0x00
00160 };
00161
00162 #define LIBUSB_TRANSFER_TYPE_MASK 0x03
00163
00168 enum libusb_transfer_type {
00170 LIBUSB_TRANSFER_TYPE_CONTROL = 0,
00171
00173 LIBUSB_TRANSFER_TYPE_ISOCHRONOUS = 1,
00174
00176 LIBUSB_TRANSFER_TYPE_BULK = 2,
00177
00179 LIBUSB_TRANSFER_TYPE_INTERRUPT = 3
00180 };
00181
00184 enum libusb_standard_request {
00186 LIBUSB_REQUEST_GET_STATUS = 0x00,
00187
00189 LIBUSB_REQUEST_CLEAR_FEATURE = 0x01,
00190
00191
00192
00194 LIBUSB_REQUEST_SET_FEATURE = 0x03,
00195
00196
00197
00199 LIBUSB_REQUEST_SET_ADDRESS = 0x05,
00200
00202 LIBUSB_REQUEST_GET_DESCRIPTOR = 0x06,
00203
00205 LIBUSB_REQUEST_SET_DESCRIPTOR = 0x07,
00206
00208 LIBUSB_REQUEST_GET_CONFIGURATION = 0x08,
00209
00211 LIBUSB_REQUEST_SET_CONFIGURATION = 0x09,
00212
00214 LIBUSB_REQUEST_GET_INTERFACE = 0x0A,
00215
00217 LIBUSB_REQUEST_SET_INTERFACE = 0x0B,
00218
00220 LIBUSB_REQUEST_SYNCH_FRAME = 0x0C
00221 };
00222
00227 enum libusb_request_type {
00229 LIBUSB_REQUEST_TYPE_STANDARD = (0x00 << 5),
00230
00232 LIBUSB_REQUEST_TYPE_CLASS = (0x01 << 5),
00233
00235 LIBUSB_REQUEST_TYPE_VENDOR = (0x02 << 5),
00236
00238 LIBUSB_REQUEST_TYPE_RESERVED = (0x03 << 5)
00239 };
00240
00245 enum libusb_request_recipient {
00247 LIBUSB_RECIPIENT_DEVICE = 0x00,
00248
00250 LIBUSB_RECIPIENT_INTERFACE = 0x01,
00251
00253 LIBUSB_RECIPIENT_ENDPOINT = 0x02,
00254
00256 LIBUSB_RECIPIENT_OTHER = 0x03
00257 };
00258
00259 #define LIBUSB_ISO_SYNC_TYPE_MASK 0x0C
00260
00266 enum libusb_iso_sync_type {
00268 LIBUSB_ISO_SYNC_TYPE_NONE = 0,
00269
00271 LIBUSB_ISO_SYNC_TYPE_ASYNC = 1,
00272
00274 LIBUSB_ISO_SYNC_TYPE_ADAPTIVE = 2,
00275
00277 LIBUSB_ISO_SYNC_TYPE_SYNC = 3
00278 };
00279
00280 #define LIBUSB_ISO_USAGE_TYPE_MASK 0x30
00281
00287 enum libusb_iso_usage_type {
00289 LIBUSB_ISO_USAGE_TYPE_DATA = 0,
00290
00292 LIBUSB_ISO_USAGE_TYPE_FEEDBACK = 1,
00293
00295 LIBUSB_ISO_USAGE_TYPE_IMPLICIT = 2
00296 };
00297
00303 struct libusb_device_descriptor {
00305 uint8_t bLength;
00306
00310 uint8_t bDescriptorType;
00311
00314 uint16_t bcdUSB;
00315
00317 uint8_t bDeviceClass;
00318
00321 uint8_t bDeviceSubClass;
00322
00325 uint8_t bDeviceProtocol;
00326
00328 uint8_t bMaxPacketSize0;
00329
00331 uint16_t idVendor;
00332
00334 uint16_t idProduct;
00335
00337 uint16_t bcdDevice;
00338
00340 uint8_t iManufacturer;
00341
00343 uint8_t iProduct;
00344
00346 uint8_t iSerialNumber;
00347
00349 uint8_t bNumConfigurations;
00350 };
00351
00357 struct libusb_endpoint_descriptor {
00359 uint8_t bLength;
00360
00364 uint8_t bDescriptorType;
00365
00370 uint8_t bEndpointAddress;
00371
00379 uint8_t bmAttributes;
00380
00382 uint16_t wMaxPacketSize;
00383
00385 uint8_t bInterval;
00386
00389 uint8_t bRefresh;
00390
00392 uint8_t bSynchAddress;
00393
00396 const unsigned char *extra;
00397
00399 int extra_length;
00400 };
00401
00407 struct libusb_interface_descriptor {
00409 uint8_t bLength;
00410
00414 uint8_t bDescriptorType;
00415
00417 uint8_t bInterfaceNumber;
00418
00420 uint8_t bAlternateSetting;
00421
00424 uint8_t bNumEndpoints;
00425
00427 uint8_t bInterfaceClass;
00428
00431 uint8_t bInterfaceSubClass;
00432
00435 uint8_t bInterfaceProtocol;
00436
00438 uint8_t iInterface;
00439
00442 const struct libusb_endpoint_descriptor *endpoint;
00443
00446 const unsigned char *extra;
00447
00449 int extra_length;
00450 };
00451
00455 struct libusb_interface {
00458 const struct libusb_interface_descriptor *altsetting;
00459
00461 int num_altsetting;
00462 };
00463
00469 struct libusb_config_descriptor {
00471 uint8_t bLength;
00472
00476 uint8_t bDescriptorType;
00477
00479 uint16_t wTotalLength;
00480
00482 uint8_t bNumInterfaces;
00483
00485 uint8_t bConfigurationValue;
00486
00488 uint8_t iConfiguration;
00489
00491 uint8_t bmAttributes;
00492
00496 uint8_t MaxPower;
00497
00500 const struct libusb_interface *interface;
00501
00504 const unsigned char *extra;
00505
00507 int extra_length;
00508 };
00509
00512 struct libusb_control_setup {
00518 uint8_t bmRequestType;
00519
00525 uint8_t bRequest;
00526
00528 uint16_t wValue;
00529
00532 uint16_t wIndex;
00533
00535 uint16_t wLength;
00536 };
00537
00538 #define LIBUSB_CONTROL_SETUP_SIZE (sizeof(struct libusb_control_setup))
00539
00540
00541
00542 struct libusb_context;
00543 struct libusb_device;
00544 struct libusb_device_handle;
00545
00563 typedef struct libusb_context libusb_context;
00564
00580 typedef struct libusb_device libusb_device;
00581
00582
00591 typedef struct libusb_device_handle libusb_device_handle;
00592
00597 enum libusb_error {
00599 LIBUSB_SUCCESS = 0,
00600
00602 LIBUSB_ERROR_IO = -1,
00603
00605 LIBUSB_ERROR_INVALID_PARAM = -2,
00606
00608 LIBUSB_ERROR_ACCESS = -3,
00609
00611 LIBUSB_ERROR_NO_DEVICE = -4,
00612
00614 LIBUSB_ERROR_NOT_FOUND = -5,
00615
00617 LIBUSB_ERROR_BUSY = -6,
00618
00620 LIBUSB_ERROR_TIMEOUT = -7,
00621
00623 LIBUSB_ERROR_OVERFLOW = -8,
00624
00626 LIBUSB_ERROR_PIPE = -9,
00627
00629 LIBUSB_ERROR_INTERRUPTED = -10,
00630
00632 LIBUSB_ERROR_NO_MEM = -11,
00633
00635 LIBUSB_ERROR_NOT_SUPPORTED = -12,
00636
00638 LIBUSB_ERROR_OTHER = -99
00639 };
00640
00643 enum libusb_transfer_status {
00646 LIBUSB_TRANSFER_COMPLETED,
00647
00649 LIBUSB_TRANSFER_ERROR,
00650
00652 LIBUSB_TRANSFER_TIMED_OUT,
00653
00655 LIBUSB_TRANSFER_CANCELLED,
00656
00659 LIBUSB_TRANSFER_STALL,
00660
00662 LIBUSB_TRANSFER_NO_DEVICE,
00663
00665 LIBUSB_TRANSFER_OVERFLOW
00666 };
00667
00670 enum libusb_transfer_flags {
00672 LIBUSB_TRANSFER_SHORT_NOT_OK = 1<<0,
00673
00675 LIBUSB_TRANSFER_FREE_BUFFER = 1<<1,
00676
00681 LIBUSB_TRANSFER_FREE_TRANSFER = 1<<2
00682 };
00683
00686 struct libusb_iso_packet_descriptor {
00688 unsigned int length;
00689
00691 unsigned int actual_length;
00692
00694 enum libusb_transfer_status status;
00695 };
00696
00697 struct libusb_transfer;
00698
00708 typedef void (*libusb_transfer_cb_fn)(struct libusb_transfer *transfer);
00709
00716 struct libusb_transfer {
00718 libusb_device_handle *dev_handle;
00719
00721 uint8_t flags;
00722
00724 unsigned char endpoint;
00725
00727 unsigned char type;
00728
00731 unsigned int timeout;
00732
00740 enum libusb_transfer_status status;
00741
00743 int length;
00744
00748 int actual_length;
00749
00752 libusb_transfer_cb_fn callback;
00753
00755 void *user_data;
00756
00758 unsigned char *buffer;
00759
00762 int num_iso_packets;
00763
00765 struct libusb_iso_packet_descriptor iso_packet_desc
00766 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
00767 []
00768 #else
00769 [0]
00770 #endif
00771 ;
00772 };
00773
00774 int libusb_init(libusb_context **ctx);
00775 void libusb_exit(libusb_context *ctx);
00776 void libusb_set_debug(libusb_context *ctx, int level);
00777
00778 ssize_t libusb_get_device_list(libusb_context *ctx,
00779 libusb_device ***list);
00780 void libusb_free_device_list(libusb_device **list, int unref_devices);
00781 libusb_device *libusb_ref_device(libusb_device *dev);
00782 void libusb_unref_device(libusb_device *dev);
00783
00784 int libusb_get_configuration(libusb_device_handle *dev, int *config);
00785 int libusb_get_device_descriptor(libusb_device *dev,
00786 struct libusb_device_descriptor *desc);
00787 int libusb_get_active_config_descriptor(libusb_device *dev,
00788 struct libusb_config_descriptor **config);
00789 int libusb_get_config_descriptor(libusb_device *dev, uint8_t config_index,
00790 struct libusb_config_descriptor **config);
00791 int libusb_get_config_descriptor_by_value(libusb_device *dev,
00792 uint8_t bConfigurationValue, struct libusb_config_descriptor **config);
00793 void libusb_free_config_descriptor(struct libusb_config_descriptor *config);
00794 uint8_t libusb_get_bus_number(libusb_device *dev);
00795 uint8_t libusb_get_device_address(libusb_device *dev);
00796 int libusb_get_max_packet_size(libusb_device *dev, unsigned char endpoint);
00797 int libusb_get_max_iso_packet_size(libusb_device *dev, unsigned char endpoint);
00798
00799 int libusb_open(libusb_device *dev, libusb_device_handle **handle);
00800 void libusb_close(libusb_device_handle *dev_handle);
00801 libusb_device *libusb_get_device(libusb_device_handle *dev_handle);
00802
00803 int libusb_set_configuration(libusb_device_handle *dev, int configuration);
00804 int libusb_claim_interface(libusb_device_handle *dev, int iface);
00805 int libusb_release_interface(libusb_device_handle *dev, int iface);
00806
00807 libusb_device_handle *libusb_open_device_with_vid_pid(libusb_context *ctx,
00808 uint16_t vendor_id, uint16_t product_id);
00809
00810 int libusb_set_interface_alt_setting(libusb_device_handle *dev,
00811 int interface_number, int alternate_setting);
00812 int libusb_clear_halt(libusb_device_handle *dev, unsigned char endpoint);
00813 int libusb_reset_device(libusb_device_handle *dev);
00814
00815 int libusb_kernel_driver_active(libusb_device_handle *dev, int interface);
00816 int libusb_detach_kernel_driver(libusb_device_handle *dev, int interface);
00817 int libusb_attach_kernel_driver(libusb_device_handle *dev, int interface);
00818
00819
00820
00833 static inline unsigned char *libusb_control_transfer_get_data(
00834 struct libusb_transfer *transfer)
00835 {
00836 return transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE;
00837 }
00838
00851 static inline struct libusb_control_setup *libusb_control_transfer_get_setup(
00852 struct libusb_transfer *transfer)
00853 {
00854 return (struct libusb_control_setup *) transfer->buffer;
00855 }
00856
00879 static inline void libusb_fill_control_setup(unsigned char *buffer,
00880 uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
00881 uint16_t wLength)
00882 {
00883 struct libusb_control_setup *setup = (struct libusb_control_setup *) buffer;
00884 setup->bmRequestType = bmRequestType;
00885 setup->bRequest = bRequest;
00886 setup->wValue = libusb_cpu_to_le16(wValue);
00887 setup->wIndex = libusb_cpu_to_le16(wIndex);
00888 setup->wLength = libusb_cpu_to_le16(wLength);
00889 }
00890
00891 struct libusb_transfer *libusb_alloc_transfer(int iso_packets);
00892 int libusb_submit_transfer(struct libusb_transfer *transfer);
00893 int libusb_cancel_transfer(struct libusb_transfer *transfer);
00894 void libusb_free_transfer(struct libusb_transfer *transfer);
00895
00923 static inline void libusb_fill_control_transfer(
00924 struct libusb_transfer *transfer, libusb_device_handle *dev_handle,
00925 unsigned char *buffer, libusb_transfer_cb_fn callback, void *user_data,
00926 unsigned int timeout)
00927 {
00928 struct libusb_control_setup *setup = (struct libusb_control_setup *) buffer;
00929 transfer->dev_handle = dev_handle;
00930 transfer->endpoint = 0;
00931 transfer->type = LIBUSB_TRANSFER_TYPE_CONTROL;
00932 transfer->timeout = timeout;
00933 transfer->buffer = buffer;
00934 if (setup)
00935 transfer->length = LIBUSB_CONTROL_SETUP_SIZE
00936 + libusb_le16_to_cpu(setup->wLength);
00937 transfer->user_data = user_data;
00938 transfer->callback = callback;
00939 }
00940
00954 static inline void libusb_fill_bulk_transfer(struct libusb_transfer *transfer,
00955 libusb_device_handle *dev_handle, unsigned char endpoint,
00956 unsigned char *buffer, int length, libusb_transfer_cb_fn callback,
00957 void *user_data, unsigned int timeout)
00958 {
00959 transfer->dev_handle = dev_handle;
00960 transfer->endpoint = endpoint;
00961 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
00962 transfer->timeout = timeout;
00963 transfer->buffer = buffer;
00964 transfer->length = length;
00965 transfer->user_data = user_data;
00966 transfer->callback = callback;
00967 }
00968
00982 static inline void libusb_fill_interrupt_transfer(
00983 struct libusb_transfer *transfer, libusb_device_handle *dev_handle,
00984 unsigned char endpoint, unsigned char *buffer, int length,
00985 libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
00986 {
00987 transfer->dev_handle = dev_handle;
00988 transfer->endpoint = endpoint;
00989 transfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT;
00990 transfer->timeout = timeout;
00991 transfer->buffer = buffer;
00992 transfer->length = length;
00993 transfer->user_data = user_data;
00994 transfer->callback = callback;
00995 }
00996
01011 static inline void libusb_fill_iso_transfer(struct libusb_transfer *transfer,
01012 libusb_device_handle *dev_handle, unsigned char endpoint,
01013 unsigned char *buffer, int length, int num_iso_packets,
01014 libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
01015 {
01016 transfer->dev_handle = dev_handle;
01017 transfer->endpoint = endpoint;
01018 transfer->type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS;
01019 transfer->timeout = timeout;
01020 transfer->buffer = buffer;
01021 transfer->length = length;
01022 transfer->num_iso_packets = num_iso_packets;
01023 transfer->user_data = user_data;
01024 transfer->callback = callback;
01025 }
01026
01035 static inline void libusb_set_iso_packet_lengths(
01036 struct libusb_transfer *transfer, unsigned int length)
01037 {
01038 int i;
01039 for (i = 0; i < transfer->num_iso_packets; i++)
01040 transfer->iso_packet_desc[i].length = length;
01041 }
01042
01059 static inline unsigned char *libusb_get_iso_packet_buffer(
01060 struct libusb_transfer *transfer, unsigned int packet)
01061 {
01062 int i;
01063 size_t offset = 0;
01064 int _packet;
01065
01066
01067
01068
01069 if (packet > INT_MAX)
01070 return NULL;
01071 _packet = packet;
01072
01073 if (_packet >= transfer->num_iso_packets)
01074 return NULL;
01075
01076 for (i = 0; i < _packet; i++)
01077 offset += transfer->iso_packet_desc[i].length;
01078
01079 return transfer->buffer + offset;
01080 }
01081
01101 static inline unsigned char *libusb_get_iso_packet_buffer_simple(
01102 struct libusb_transfer *transfer, unsigned int packet)
01103 {
01104 int _packet;
01105
01106
01107
01108
01109 if (packet > INT_MAX)
01110 return NULL;
01111 _packet = packet;
01112
01113 if (_packet >= transfer->num_iso_packets)
01114 return NULL;
01115
01116 return transfer->buffer + (transfer->iso_packet_desc[0].length * _packet);
01117 }
01118
01119
01120
01121 int libusb_control_transfer(libusb_device_handle *dev_handle,
01122 uint8_t request_type, uint8_t request, uint16_t value, uint16_t index,
01123 unsigned char *data, uint16_t length, unsigned int timeout);
01124
01125 int libusb_bulk_transfer(libusb_device_handle *dev_handle,
01126 unsigned char endpoint, unsigned char *data, int length,
01127 int *actual_length, unsigned int timeout);
01128
01129 int libusb_interrupt_transfer(libusb_device_handle *dev_handle,
01130 unsigned char endpoint, unsigned char *data, int length,
01131 int *actual_length, unsigned int timeout);
01132
01145 static inline int libusb_get_descriptor(libusb_device_handle *dev,
01146 uint8_t desc_type, uint8_t desc_index, unsigned char *data, int length)
01147 {
01148 return libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN,
01149 LIBUSB_REQUEST_GET_DESCRIPTOR, (desc_type << 8) | desc_index, 0, data,
01150 length, 1000);
01151 }
01152
01167 static inline int libusb_get_string_descriptor(libusb_device_handle *dev,
01168 uint8_t desc_index, uint16_t langid, unsigned char *data, int length)
01169 {
01170 return libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN,
01171 LIBUSB_REQUEST_GET_DESCRIPTOR, (LIBUSB_DT_STRING << 8) | desc_index,
01172 langid, data, length, 1000);
01173 }
01174
01175 int libusb_get_string_descriptor_ascii(libusb_device_handle *dev,
01176 uint8_t index, unsigned char *data, int length);
01177
01178
01179
01180 int libusb_try_lock_events(libusb_context *ctx);
01181 void libusb_lock_events(libusb_context *ctx);
01182 void libusb_unlock_events(libusb_context *ctx);
01183 int libusb_event_handling_ok(libusb_context *ctx);
01184 int libusb_event_handler_active(libusb_context *ctx);
01185 void libusb_lock_event_waiters(libusb_context *ctx);
01186 void libusb_unlock_event_waiters(libusb_context *ctx);
01187 int libusb_wait_for_event(libusb_context *ctx, struct timeval *tv);
01188
01189 int libusb_handle_events_timeout(libusb_context *ctx, struct timeval *tv);
01190 int libusb_handle_events(libusb_context *ctx);
01191 int libusb_handle_events_locked(libusb_context *ctx, struct timeval *tv);
01192 int libusb_pollfds_handle_timeouts(libusb_context *ctx);
01193 int libusb_get_next_timeout(libusb_context *ctx, struct timeval *tv);
01194
01198 struct libusb_pollfd {
01200 int fd;
01201
01206 short events;
01207 };
01208
01219 typedef void (*libusb_pollfd_added_cb)(int fd, short events, void *user_data);
01220
01230 typedef void (*libusb_pollfd_removed_cb)(int fd, void *user_data);
01231
01232 const struct libusb_pollfd **libusb_get_pollfds(libusb_context *ctx);
01233 void libusb_set_pollfd_notifiers(libusb_context *ctx,
01234 libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb,
01235 void *user_data);
01236
01237 #ifdef __cplusplus
01238 }
01239 #endif
01240
01241 #endif