1 /* $FreeBSD$ */ 2 /*- 3 * Copyright (c) 2009 Sylvestre Gallon. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #ifndef __LIBUSB_H__ 28 #define __LIBUSB_H__ 29 30 #include <sys/time.h> 31 #include <sys/types.h> 32 33 #ifdef __cplusplus 34 extern "C" { 35 #endif 36 #if 0 37 } /* indent fix */ 38 39 #endif 40 41 /* libusb enums */ 42 43 enum libusb_class_code { 44 LIBUSB_CLASS_PER_INTERFACE = 0, 45 LIBUSB_CLASS_AUDIO = 1, 46 LIBUSB_CLASS_COMM = 2, 47 LIBUSB_CLASS_HID = 3, 48 LIBUSB_CLASS_PTP = 6, 49 LIBUSB_CLASS_PRINTER = 7, 50 LIBUSB_CLASS_MASS_STORAGE = 8, 51 LIBUSB_CLASS_HUB = 9, 52 LIBUSB_CLASS_DATA = 10, 53 LIBUSB_CLASS_VENDOR_SPEC = 0xff, 54 }; 55 56 enum libusb_descriptor_type { 57 LIBUSB_DT_DEVICE = 0x01, 58 LIBUSB_DT_CONFIG = 0x02, 59 LIBUSB_DT_STRING = 0x03, 60 LIBUSB_DT_INTERFACE = 0x04, 61 LIBUSB_DT_ENDPOINT = 0x05, 62 LIBUSB_DT_HID = 0x21, 63 LIBUSB_DT_REPORT = 0x22, 64 LIBUSB_DT_PHYSICAL = 0x23, 65 LIBUSB_DT_HUB = 0x29, 66 LIBUSB_DT_BOS = 0x0f, 67 LIBUSB_DT_DEVICE_CAPABILITY = 0x10, 68 LIBUSB_DT_SS_ENDPOINT_COMPANION = 0x30, 69 }; 70 71 enum libusb_device_capability_type { 72 LIBUSB_WIRELESS_USB_DEVICE_CAPABILITY = 0x1, 73 LIBUSB_USB_2_0_EXTENSION_DEVICE_CAPABILITY = 0x2, 74 LIBUSB_SS_USB_DEVICE_CAPABILITY = 0x3, 75 LIBUSB_CONTAINER_ID_DEVICE_CAPABILITY = 0x4, 76 }; 77 78 #define LIBUSB_DT_DEVICE_SIZE 18 79 #define LIBUSB_DT_CONFIG_SIZE 9 80 #define LIBUSB_DT_INTERFACE_SIZE 9 81 #define LIBUSB_DT_ENDPOINT_SIZE 7 82 #define LIBUSB_DT_ENDPOINT_AUDIO_SIZE 9 83 #define LIBUSB_DT_HUB_NONVAR_SIZE 7 84 #define LIBUSB_DT_SS_ENDPOINT_COMPANION_SIZE 6 85 #define LIBUSB_DT_BOS_SIZE 5 86 #define LIBUSB_USB_2_0_EXTENSION_DEVICE_CAPABILITY_SIZE 7 87 #define LIBUSB_SS_USB_DEVICE_CAPABILITY_SIZE 10 88 89 #define LIBUSB_ENDPOINT_ADDRESS_MASK 0x0f 90 #define LIBUSB_ENDPOINT_DIR_MASK 0x80 91 92 enum libusb_endpoint_direction { 93 LIBUSB_ENDPOINT_IN = 0x80, 94 LIBUSB_ENDPOINT_OUT = 0x00, 95 }; 96 97 #define LIBUSB_TRANSFER_TYPE_MASK 0x03 98 99 enum libusb_transfer_type { 100 LIBUSB_TRANSFER_TYPE_CONTROL = 0, 101 LIBUSB_TRANSFER_TYPE_ISOCHRONOUS = 1, 102 LIBUSB_TRANSFER_TYPE_BULK = 2, 103 LIBUSB_TRANSFER_TYPE_INTERRUPT = 3, 104 }; 105 106 enum libusb_standard_request { 107 LIBUSB_REQUEST_GET_STATUS = 0x00, 108 LIBUSB_REQUEST_CLEAR_FEATURE = 0x01, 109 LIBUSB_REQUEST_SET_FEATURE = 0x03, 110 LIBUSB_REQUEST_SET_ADDRESS = 0x05, 111 LIBUSB_REQUEST_GET_DESCRIPTOR = 0x06, 112 LIBUSB_REQUEST_SET_DESCRIPTOR = 0x07, 113 LIBUSB_REQUEST_GET_CONFIGURATION = 0x08, 114 LIBUSB_REQUEST_SET_CONFIGURATION = 0x09, 115 LIBUSB_REQUEST_GET_INTERFACE = 0x0A, 116 LIBUSB_REQUEST_SET_INTERFACE = 0x0B, 117 LIBUSB_REQUEST_SYNCH_FRAME = 0x0C, 118 }; 119 120 enum libusb_request_type { 121 LIBUSB_REQUEST_TYPE_STANDARD = (0x00 << 5), 122 LIBUSB_REQUEST_TYPE_CLASS = (0x01 << 5), 123 LIBUSB_REQUEST_TYPE_VENDOR = (0x02 << 5), 124 LIBUSB_REQUEST_TYPE_RESERVED = (0x03 << 5), 125 }; 126 127 enum libusb_request_recipient { 128 LIBUSB_RECIPIENT_DEVICE = 0x00, 129 LIBUSB_RECIPIENT_INTERFACE = 0x01, 130 LIBUSB_RECIPIENT_ENDPOINT = 0x02, 131 LIBUSB_RECIPIENT_OTHER = 0x03, 132 }; 133 134 #define LIBUSB_ISO_SYNC_TYPE_MASK 0x0C 135 136 enum libusb_iso_sync_type { 137 LIBUSB_ISO_SYNC_TYPE_NONE = 0, 138 LIBUSB_ISO_SYNC_TYPE_ASYNC = 1, 139 LIBUSB_ISO_SYNC_TYPE_ADAPTIVE = 2, 140 LIBUSB_ISO_SYNC_TYPE_SYNC = 3, 141 }; 142 143 #define LIBUSB_ISO_USAGE_TYPE_MASK 0x30 144 145 enum libusb_iso_usage_type { 146 LIBUSB_ISO_USAGE_TYPE_DATA = 0, 147 LIBUSB_ISO_USAGE_TYPE_FEEDBACK = 1, 148 LIBUSB_ISO_USAGE_TYPE_IMPLICIT = 2, 149 }; 150 151 enum libusb_error { 152 LIBUSB_SUCCESS = 0, 153 LIBUSB_ERROR_IO = -1, 154 LIBUSB_ERROR_INVALID_PARAM = -2, 155 LIBUSB_ERROR_ACCESS = -3, 156 LIBUSB_ERROR_NO_DEVICE = -4, 157 LIBUSB_ERROR_NOT_FOUND = -5, 158 LIBUSB_ERROR_BUSY = -6, 159 LIBUSB_ERROR_TIMEOUT = -7, 160 LIBUSB_ERROR_OVERFLOW = -8, 161 LIBUSB_ERROR_PIPE = -9, 162 LIBUSB_ERROR_INTERRUPTED = -10, 163 LIBUSB_ERROR_NO_MEM = -11, 164 LIBUSB_ERROR_NOT_SUPPORTED = -12, 165 LIBUSB_ERROR_OTHER = -99, 166 }; 167 168 enum libusb_speed { 169 LIBUSB_SPEED_UNKNOWN = 0, 170 LIBUSB_SPEED_LOW = 1, 171 LIBUSB_SPEED_FULL = 2, 172 LIBUSB_SPEED_HIGH = 3, 173 LIBUSB_SPEED_SUPER = 4, 174 }; 175 176 enum libusb_transfer_status { 177 LIBUSB_TRANSFER_COMPLETED, 178 LIBUSB_TRANSFER_ERROR, 179 LIBUSB_TRANSFER_TIMED_OUT, 180 LIBUSB_TRANSFER_CANCELLED, 181 LIBUSB_TRANSFER_STALL, 182 LIBUSB_TRANSFER_NO_DEVICE, 183 LIBUSB_TRANSFER_OVERFLOW, 184 }; 185 186 enum libusb_transfer_flags { 187 LIBUSB_TRANSFER_SHORT_NOT_OK = 1 << 0, 188 LIBUSB_TRANSFER_FREE_BUFFER = 1 << 1, 189 LIBUSB_TRANSFER_FREE_TRANSFER = 1 << 2, 190 }; 191 192 enum libusb_debug_level { 193 LIBUSB_DEBUG_NO=0, 194 LIBUSB_DEBUG_FUNCTION=1, 195 LIBUSB_DEBUG_TRANSFER=2, 196 }; 197 198 /* libusb structures */ 199 200 struct libusb_context; 201 struct libusb_device; 202 struct libusb_transfer; 203 struct libusb_device_handle; 204 205 struct libusb_pollfd { 206 int fd; 207 short events; 208 }; 209 210 typedef struct libusb_context libusb_context; 211 typedef struct libusb_device libusb_device; 212 typedef struct libusb_device_handle libusb_device_handle; 213 typedef struct libusb_pollfd libusb_pollfd; 214 typedef void (*libusb_pollfd_added_cb) (int fd, short events, void *user_data); 215 typedef void (*libusb_pollfd_removed_cb) (int fd, void *user_data); 216 217 typedef struct libusb_device_descriptor { 218 uint8_t bLength; 219 uint8_t bDescriptorType; 220 uint16_t bcdUSB; 221 uint8_t bDeviceClass; 222 uint8_t bDeviceSubClass; 223 uint8_t bDeviceProtocol; 224 uint8_t bMaxPacketSize0; 225 uint16_t idVendor; 226 uint16_t idProduct; 227 uint16_t bcdDevice; 228 uint8_t iManufacturer; 229 uint8_t iProduct; 230 uint8_t iSerialNumber; 231 uint8_t bNumConfigurations; 232 } libusb_device_descriptor; 233 234 typedef struct libusb_endpoint_descriptor { 235 uint8_t bLength; 236 uint8_t bDescriptorType; 237 uint8_t bEndpointAddress; 238 uint8_t bmAttributes; 239 uint16_t wMaxPacketSize; 240 uint8_t bInterval; 241 uint8_t bRefresh; 242 uint8_t bSynchAddress; 243 uint8_t *extra; 244 int extra_length; 245 } libusb_endpoint_descriptor __aligned(sizeof(void *)); 246 247 typedef struct libusb_ss_endpoint_companion_descriptor { 248 uint8_t bLength; 249 uint8_t bDescriptorType; 250 uint8_t bMaxBurst; 251 uint8_t bmAttributes; 252 uint16_t wBytesPerInterval; 253 } libusb_ss_endpoint_companion_descriptor __aligned(sizeof(void *)); 254 255 typedef struct libusb_interface_descriptor { 256 uint8_t bLength; 257 uint8_t bDescriptorType; 258 uint8_t bInterfaceNumber; 259 uint8_t bAlternateSetting; 260 uint8_t bNumEndpoints; 261 uint8_t bInterfaceClass; 262 uint8_t bInterfaceSubClass; 263 uint8_t bInterfaceProtocol; 264 uint8_t iInterface; 265 struct libusb_endpoint_descriptor *endpoint; 266 uint8_t *extra; 267 int extra_length; 268 } libusb_interface_descriptor __aligned(sizeof(void *)); 269 270 typedef struct libusb_interface { 271 struct libusb_interface_descriptor *altsetting; 272 int num_altsetting; 273 } libusb_interface __aligned(sizeof(void *)); 274 275 typedef struct libusb_config_descriptor { 276 uint8_t bLength; 277 uint8_t bDescriptorType; 278 uint16_t wTotalLength; 279 uint8_t bNumInterfaces; 280 uint8_t bConfigurationValue; 281 uint8_t iConfiguration; 282 uint8_t bmAttributes; 283 uint8_t MaxPower; 284 struct libusb_interface *interface; 285 uint8_t *extra; 286 int extra_length; 287 } libusb_config_descriptor __aligned(sizeof(void *)); 288 289 typedef struct libusb_usb_2_0_device_capability_descriptor { 290 uint8_t bLength; 291 uint8_t bDescriptorType; 292 uint8_t bDevCapabilityType; 293 uint32_t bmAttributes; 294 #define LIBUSB_USB_2_0_CAPABILITY_LPM_SUPPORT (1 << 1) 295 } libusb_usb_2_0_device_capability_descriptor __aligned(sizeof(void *)); 296 297 typedef struct libusb_ss_usb_device_capability_descriptor { 298 uint8_t bLength; 299 uint8_t bDescriptorType; 300 uint8_t bDevCapabilityType; 301 uint8_t bmAttributes; 302 #define LIBUSB_SS_USB_CAPABILITY_LPM_SUPPORT (1 << 1) 303 uint16_t wSpeedSupported; 304 #define LIBUSB_CAPABILITY_LOW_SPEED_OPERATION (1) 305 #define LIBUSB_CAPABILITY_FULL_SPEED_OPERATION (1 << 1) 306 #define LIBUSB_CAPABILITY_HIGH_SPEED_OPERATION (1 << 2) 307 #define LIBUSB_CAPABILITY_5GBPS_OPERATION (1 << 3) 308 uint8_t bFunctionalitySupport; 309 uint8_t bU1DevExitLat; 310 uint16_t wU2DevExitLat; 311 } libusb_ss_usb_device_capability_descriptor __aligned(sizeof(void *)); 312 313 typedef struct libusb_bos_descriptor { 314 uint8_t bLength; 315 uint8_t bDescriptorType; 316 uint16_t wTotalLength; 317 uint8_t bNumDeviceCapabilities; 318 struct libusb_usb_2_0_device_capability_descriptor *usb_2_0_ext_cap; 319 struct libusb_ss_usb_device_capability_descriptor *ss_usb_cap; 320 } libusb_bos_descriptor __aligned(sizeof(void *)); 321 322 typedef struct libusb_control_setup { 323 uint8_t bmRequestType; 324 uint8_t bRequest; 325 uint16_t wValue; 326 uint16_t wIndex; 327 uint16_t wLength; 328 } libusb_control_setup; 329 330 #define LIBUSB_CONTROL_SETUP_SIZE 8 /* bytes */ 331 332 typedef struct libusb_iso_packet_descriptor { 333 uint32_t length; 334 uint32_t actual_length; 335 enum libusb_transfer_status status; 336 } libusb_iso_packet_descriptor __aligned(sizeof(void *)); 337 338 typedef void (*libusb_transfer_cb_fn) (struct libusb_transfer *transfer); 339 340 typedef struct libusb_transfer { 341 libusb_device_handle *dev_handle; 342 uint8_t flags; 343 uint8_t endpoint; 344 uint8_t type; 345 uint32_t timeout; 346 enum libusb_transfer_status status; 347 int length; 348 int actual_length; 349 libusb_transfer_cb_fn callback; 350 void *user_data; 351 uint8_t *buffer; 352 int num_iso_packets; 353 struct libusb_iso_packet_descriptor iso_packet_desc[0]; 354 } libusb_transfer __aligned(sizeof(void *)); 355 356 /* Library initialisation */ 357 358 void libusb_set_debug(libusb_context * ctx, int level); 359 const char *libusb_strerror(int code); 360 const char *libusb_error_name(int code); 361 int libusb_init(libusb_context ** context); 362 void libusb_exit(struct libusb_context *ctx); 363 364 /* Device handling and enumeration */ 365 366 ssize_t libusb_get_device_list(libusb_context * ctx, libusb_device *** list); 367 void libusb_free_device_list(libusb_device ** list, int unref_devices); 368 uint8_t libusb_get_bus_number(libusb_device * dev); 369 uint8_t libusb_get_device_address(libusb_device * dev); 370 enum libusb_speed libusb_get_device_speed(libusb_device * dev); 371 int libusb_clear_halt(libusb_device_handle *devh, uint8_t endpoint); 372 int libusb_get_max_packet_size(libusb_device * dev, uint8_t endpoint); 373 int libusb_get_max_iso_packet_size(libusb_device * dev, uint8_t endpoint); 374 libusb_device *libusb_ref_device(libusb_device * dev); 375 void libusb_unref_device(libusb_device * dev); 376 int libusb_open(libusb_device * dev, libusb_device_handle ** devh); 377 libusb_device_handle *libusb_open_device_with_vid_pid(libusb_context * ctx, uint16_t vendor_id, uint16_t product_id); 378 void libusb_close(libusb_device_handle * devh); 379 libusb_device *libusb_get_device(libusb_device_handle * devh); 380 int libusb_get_configuration(libusb_device_handle * devh, int *config); 381 int libusb_set_configuration(libusb_device_handle * devh, int configuration); 382 int libusb_claim_interface(libusb_device_handle * devh, int interface_number); 383 int libusb_release_interface(libusb_device_handle * devh, int interface_number); 384 int libusb_reset_device(libusb_device_handle * devh); 385 int libusb_check_connected(libusb_device_handle * devh); 386 int libusb_kernel_driver_active(libusb_device_handle * devh, int interface); 387 int libusb_get_driver_np(libusb_device_handle * devh, int interface, char *name, int namelen); 388 int libusb_get_driver(libusb_device_handle * devh, int interface, char *name, int namelen); 389 int libusb_detach_kernel_driver_np(libusb_device_handle * devh, int interface); 390 int libusb_detach_kernel_driver(libusb_device_handle * devh, int interface); 391 int libusb_attach_kernel_driver(libusb_device_handle * devh, int interface); 392 int libusb_set_interface_alt_setting(libusb_device_handle * devh, int interface_number, int alternate_setting); 393 394 /* USB Descriptors */ 395 396 int libusb_get_device_descriptor(libusb_device * dev, struct libusb_device_descriptor *desc); 397 int libusb_get_active_config_descriptor(libusb_device * dev, struct libusb_config_descriptor **config); 398 int libusb_get_config_descriptor(libusb_device * dev, uint8_t config_index, struct libusb_config_descriptor **config); 399 int libusb_get_config_descriptor_by_value(libusb_device * dev, uint8_t bConfigurationValue, struct libusb_config_descriptor **config); 400 void libusb_free_config_descriptor(struct libusb_config_descriptor *config); 401 int libusb_get_string_descriptor(libusb_device_handle * devh, uint8_t desc_index, uint16_t langid, unsigned char *data, int length); 402 int libusb_get_string_descriptor_ascii(libusb_device_handle * devh, uint8_t desc_index, uint8_t *data, int length); 403 int libusb_get_descriptor(libusb_device_handle * devh, uint8_t desc_type, uint8_t desc_index, uint8_t *data, int length); 404 int libusb_parse_ss_endpoint_comp(const void *buf, int len, struct libusb_ss_endpoint_companion_descriptor **ep_comp); 405 void libusb_free_ss_endpoint_comp(struct libusb_ss_endpoint_companion_descriptor *ep_comp); 406 int libusb_parse_bos_descriptor(const void *buf, int len, struct libusb_bos_descriptor **bos); 407 void libusb_free_bos_descriptor(struct libusb_bos_descriptor *bos); 408 409 /* Asynchronous device I/O */ 410 411 struct libusb_transfer *libusb_alloc_transfer(int iso_packets); 412 void libusb_free_transfer(struct libusb_transfer *transfer); 413 int libusb_submit_transfer(struct libusb_transfer *transfer); 414 int libusb_cancel_transfer(struct libusb_transfer *transfer); 415 uint8_t *libusb_get_iso_packet_buffer(struct libusb_transfer *transfer, uint32_t index); 416 uint8_t *libusb_get_iso_packet_buffer_simple(struct libusb_transfer *transfer, uint32_t index); 417 void libusb_set_iso_packet_lengths(struct libusb_transfer *transfer, uint32_t length); 418 uint8_t *libusb_control_transfer_get_data(struct libusb_transfer *transfer); 419 struct libusb_control_setup *libusb_control_transfer_get_setup(struct libusb_transfer *transfer); 420 void libusb_fill_control_setup(uint8_t *buf, uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, uint16_t wLength); 421 void libusb_fill_control_transfer(struct libusb_transfer *transfer, libusb_device_handle *devh, uint8_t *buf, libusb_transfer_cb_fn callback, void *user_data, uint32_t timeout); 422 void libusb_fill_bulk_transfer(struct libusb_transfer *transfer, libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf, int length, libusb_transfer_cb_fn callback, void *user_data, uint32_t timeout); 423 void libusb_fill_interrupt_transfer(struct libusb_transfer *transfer, libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf, int length, libusb_transfer_cb_fn callback, void *user_data, uint32_t timeout); 424 void libusb_fill_iso_transfer(struct libusb_transfer *transfer, libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf, int length, int npacket, libusb_transfer_cb_fn callback, void *user_data, uint32_t timeout); 425 426 /* Polling and timing */ 427 428 int libusb_try_lock_events(libusb_context * ctx); 429 void libusb_lock_events(libusb_context * ctx); 430 void libusb_unlock_events(libusb_context * ctx); 431 int libusb_event_handling_ok(libusb_context * ctx); 432 int libusb_event_handler_active(libusb_context * ctx); 433 void libusb_lock_event_waiters(libusb_context * ctx); 434 void libusb_unlock_event_waiters(libusb_context * ctx); 435 int libusb_wait_for_event(libusb_context * ctx, struct timeval *tv); 436 int libusb_handle_events_timeout(libusb_context * ctx, struct timeval *tv); 437 int libusb_handle_events(libusb_context * ctx); 438 int libusb_handle_events_locked(libusb_context * ctx, struct timeval *tv); 439 int libusb_get_next_timeout(libusb_context * ctx, struct timeval *tv); 440 void libusb_set_pollfd_notifiers(libusb_context * ctx, libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb, void *user_data); 441 struct libusb_pollfd **libusb_get_pollfds(libusb_context * ctx); 442 443 /* Synchronous device I/O */ 444 445 int libusb_control_transfer(libusb_device_handle * devh, uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, uint8_t *data, uint16_t wLength, uint32_t timeout); 446 int libusb_bulk_transfer(libusb_device_handle * devh, uint8_t endpoint, uint8_t *data, int length, int *transferred, uint32_t timeout); 447 int libusb_interrupt_transfer(libusb_device_handle * devh, uint8_t endpoint, uint8_t *data, int length, int *transferred, uint32_t timeout); 448 449 /* Byte-order */ 450 451 uint16_t libusb_cpu_to_le16(uint16_t x); 452 uint16_t libusb_le16_to_cpu(uint16_t x); 453 454 #if 0 455 { /* indent fix */ 456 #endif 457 #ifdef __cplusplus 458 } 459 460 #endif 461 462 #endif /* __LIBUSB_H__ */ 463