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 #include <sys/endian.h> 33 #include <sys/queue.h> 34 35 #include <stdint.h> 36 #include <time.h> 37 #include <string.h> 38 #include <pthread.h> 39 40 41 #ifdef __cplusplus 42 extern "C" { 43 #endif 44 #if 0 45 } /* indent fix */ 46 47 #endif 48 49 /* libusb enums */ 50 51 enum libusb_class_code { 52 LIBUSB_CLASS_PER_INTERFACE = 0, 53 LIBUSB_CLASS_AUDIO = 1, 54 LIBUSB_CLASS_COMM = 2, 55 LIBUSB_CLASS_HID = 3, 56 LIBUSB_CLASS_PTP = 6, 57 LIBUSB_CLASS_PRINTER = 7, 58 LIBUSB_CLASS_MASS_STORAGE = 8, 59 LIBUSB_CLASS_HUB = 9, 60 LIBUSB_CLASS_DATA = 10, 61 LIBUSB_CLASS_VENDOR_SPEC = 0xff, 62 }; 63 64 enum libusb_descriptor_type { 65 LIBUSB_DT_DEVICE = 0x01, 66 LIBUSB_DT_CONFIG = 0x02, 67 LIBUSB_DT_STRING = 0x03, 68 LIBUSB_DT_INTERFACE = 0x04, 69 LIBUSB_DT_ENDPOINT = 0x05, 70 LIBUSB_DT_HID = 0x21, 71 LIBUSB_DT_REPORT = 0x22, 72 LIBUSB_DT_PHYSICAL = 0x23, 73 LIBUSB_DT_HUB = 0x29, 74 }; 75 76 #define LIBUSB_DT_DEVICE_SIZE 18 77 #define LIBUSB_DT_CONFIG_SIZE 9 78 #define LIBUSB_DT_INTERFACE_SIZE 9 79 #define LIBUSB_DT_ENDPOINT_SIZE 7 80 #define LIBUSB_DT_ENDPOINT_AUDIO_SIZE 9 81 #define LIBUSB_DT_HUB_NONVAR_SIZE 7 82 83 #define LIBUSB_ENDPOINT_ADDRESS_MASK 0x0f 84 #define LIBUSB_ENDPOINT_DIR_MASK 0x80 85 86 enum libusb_endpoint_direction { 87 LIBUSB_ENDPOINT_IN = 0x80, 88 LIBUSB_ENDPOINT_OUT = 0x00, 89 }; 90 91 #define LIBUSB_TRANSFER_TYPE_MASK 0x03 92 93 enum libusb_transfer_type { 94 LIBUSB_TRANSFER_TYPE_CONTROL = 0, 95 LIBUSB_TRANSFER_TYPE_ISOCHRONOUS = 1, 96 LIBUSB_TRANSFER_TYPE_BULK = 2, 97 LIBUSB_TRANSFER_TYPE_INTERRUPT = 3, 98 }; 99 100 enum libusb_standard_request { 101 LIBUSB_REQUEST_GET_STATUS = 0x00, 102 LIBUSB_REQUEST_CLEAR_FEATURE = 0x01, 103 LIBUSB_REQUEST_SET_FEATURE = 0x03, 104 LIBUSB_REQUEST_SET_ADDRESS = 0x05, 105 LIBUSB_REQUEST_GET_DESCRIPTOR = 0x06, 106 LIBUSB_REQUEST_SET_DESCRIPTOR = 0x07, 107 LIBUSB_REQUEST_GET_CONFIGURATION = 0x08, 108 LIBUSB_REQUEST_SET_CONFIGURATION = 0x09, 109 LIBUSB_REQUEST_GET_INTERFACE = 0x0A, 110 LIBUSB_REQUEST_SET_INTERFACE = 0x0B, 111 LIBUSB_REQUEST_SYNCH_FRAME = 0x0C, 112 }; 113 114 enum libusb_request_type { 115 LIBUSB_REQUEST_TYPE_STANDARD = (0x00 << 5), 116 LIBUSB_REQUEST_TYPE_CLASS = (0x01 << 5), 117 LIBUSB_REQUEST_TYPE_VENDOR = (0x02 << 5), 118 LIBUSB_REQUEST_TYPE_RESERVED = (0x03 << 5), 119 }; 120 121 enum libusb_request_recipient { 122 LIBUSB_RECIPIENT_DEVICE = 0x00, 123 LIBUSB_RECIPIENT_INTERFACE = 0x01, 124 LIBUSB_RECIPIENT_ENDPOINT = 0x02, 125 LIBUSB_RECIPIENT_OTHER = 0x03, 126 }; 127 128 #define LIBUSB_ISO_SYNC_TYPE_MASK 0x0C 129 130 enum libusb_iso_sync_type { 131 LIBUSB_ISO_SYNC_TYPE_NONE = 0, 132 LIBUSB_ISO_SYNC_TYPE_ASYNC = 1, 133 LIBUSB_ISO_SYNC_TYPE_ADAPTIVE = 2, 134 LIBUSB_ISO_SYNC_TYPE_SYNC = 3, 135 }; 136 137 #define LIBUSB_ISO_USAGE_TYPE_MASK 0x30 138 139 enum libusb_iso_usage_type { 140 LIBUSB_ISO_USAGE_TYPE_DATA = 0, 141 LIBUSB_ISO_USAGE_TYPE_FEEDBACK = 1, 142 LIBUSB_ISO_USAGE_TYPE_IMPLICIT = 2, 143 }; 144 145 enum libusb_error { 146 LIBUSB_SUCCESS = 0, 147 LIBUSB_ERROR_IO = -1, 148 LIBUSB_ERROR_INVALID_PARAM = -2, 149 LIBUSB_ERROR_ACCESS = -3, 150 LIBUSB_ERROR_NO_DEVICE = -4, 151 LIBUSB_ERROR_NOT_FOUND = -5, 152 LIBUSB_ERROR_BUSY = -6, 153 LIBUSB_ERROR_TIMEOUT = -7, 154 LIBUSB_ERROR_OVERFLOW = -8, 155 LIBUSB_ERROR_PIPE = -9, 156 LIBUSB_ERROR_INTERRUPTED = -10, 157 LIBUSB_ERROR_NO_MEM = -11, 158 LIBUSB_ERROR_NOT_SUPPORTED = -12, 159 LIBUSB_ERROR_OTHER = -99, 160 }; 161 162 enum libusb_transfer_status { 163 LIBUSB_TRANSFER_COMPLETED, 164 LIBUSB_TRANSFER_ERROR, 165 LIBUSB_TRANSFER_TIMED_OUT, 166 LIBUSB_TRANSFER_CANCELLED, 167 LIBUSB_TRANSFER_STALL, 168 LIBUSB_TRANSFER_NO_DEVICE, 169 LIBUSB_TRANSFER_OVERFLOW, 170 }; 171 172 enum libusb_transfer_flags { 173 LIBUSB_TRANSFER_SHORT_NOT_OK = 1 << 0, 174 LIBUSB_TRANSFER_FREE_BUFFER = 1 << 1, 175 LIBUSB_TRANSFER_FREE_TRANSFER = 1 << 2, 176 }; 177 178 enum libusb_debug_level { 179 LIBUSB_DEBUG_NO=0, 180 LIBUSB_DEBUG_FUNCTION=1, 181 LIBUSB_DEBUG_TRANSFER=2, 182 }; 183 184 /* internal structures */ 185 186 typedef struct libusb_pollfd { 187 int fd; 188 short events; 189 } libusb_pollfd; 190 191 struct usb_pollfd { 192 TAILQ_ENTRY(usb_pollfd) list; 193 struct libusb_pollfd pollfd; 194 }; 195 196 struct usb_transfer { 197 TAILQ_ENTRY(usb_transfer) list; 198 int num_iso_packets; 199 struct timeval timeout; 200 int transferred; 201 uint8_t flags; 202 }; 203 204 struct usb_ep_tr { 205 TAILQ_ENTRY(usb_ep_tr) list; 206 uint8_t addr; 207 uint8_t idx; 208 uint8_t flags; 209 void *os_priv; 210 }; 211 /* libusb structures */ 212 213 typedef void (*libusb_pollfd_added_cb) (int fd, short events, void *user_data); 214 typedef void (*libusb_pollfd_removed_cb) (int fd, void *user_data); 215 216 typedef struct libusb_context { 217 int debug; 218 int debug_fixed; 219 220 int ctrl_pipe[2]; 221 222 TAILQ_HEAD(usb_devs_list, libusb_device) usb_devs; 223 pthread_mutex_t usb_devs_lock; 224 225 TAILQ_HEAD(open_devs_list, libusb_device_handle) open_devs; 226 pthread_mutex_t open_devs_lock; 227 228 TAILQ_HEAD(flying_transfers_list, usb_transfer) flying_transfers; 229 pthread_mutex_t flying_transfers_lock; 230 231 TAILQ_HEAD(pollfds_list, usb_pollfd) pollfds; 232 pthread_mutex_t pollfds_lock; 233 234 unsigned int pollfd_modify; 235 pthread_mutex_t pollfd_modify_lock; 236 237 libusb_pollfd_added_cb fd_added_cb; 238 libusb_pollfd_removed_cb fd_removed_cb; 239 void *fd_cb_user_data; 240 241 pthread_mutex_t events_lock; 242 int event_handler_active; 243 244 pthread_mutex_t event_waiters_lock; 245 pthread_cond_t event_waiters_cond; 246 } libusb_context; 247 248 typedef struct libusb_device { 249 pthread_mutex_t lock; 250 int refcnt; 251 252 struct libusb_context *ctx; 253 254 uint8_t bus_number; 255 uint8_t device_address; 256 uint8_t num_configurations; 257 258 TAILQ_ENTRY(libusb_device) list; 259 unsigned long session_data; 260 void *os_priv; 261 } libusb_device; 262 263 typedef struct libusb_device_handle { 264 pthread_mutex_t lock; 265 unsigned long claimed_interfaces; 266 267 TAILQ_ENTRY(libusb_device_handle) list; 268 struct libusb_device *dev; 269 void *os_priv; 270 TAILQ_HEAD(ep_list, usb_ep_tr) ep_list; 271 } libusb_device_handle; 272 273 typedef struct libusb_device_descriptor { 274 uint8_t bLength; 275 uint8_t bDescriptorType; 276 uint16_t bcdUSB; 277 uint8_t bDeviceClass; 278 uint8_t bDeviceSubClass; 279 uint8_t bDeviceProtocol; 280 uint8_t bMaxPacketSize0; 281 uint16_t idVendor; 282 uint16_t idProduct; 283 uint16_t bcdDevice; 284 uint8_t iManufacturer; 285 uint8_t iProduct; 286 uint8_t iSerialNumber; 287 uint8_t bNumConfigurations; 288 } libusb_device_descriptor; 289 290 typedef struct libusb_endpoint_descriptor { 291 uint8_t bLength; 292 uint8_t bDescriptorType; 293 uint8_t bEndpointAddress; 294 uint8_t bmAttributes; 295 uint16_t wMaxPacketSize; 296 uint8_t bInterval; 297 uint8_t bRefresh; 298 uint8_t bSynchAddress; 299 unsigned char *extra; 300 int extra_length; 301 } libusb_endpoint_descriptor __aligned(sizeof(void *)); 302 303 typedef struct libusb_interface_descriptor { 304 uint8_t bLength; 305 uint8_t bDescriptorType; 306 uint8_t bInterfaceNumber; 307 uint8_t bAlternateSetting; 308 uint8_t bNumEndpoints; 309 uint8_t bInterfaceClass; 310 uint8_t bInterfaceSubClass; 311 uint8_t bInterfaceProtocol; 312 uint8_t iInterface; 313 struct libusb_endpoint_descriptor *endpoint; 314 unsigned char *extra; 315 int extra_length; 316 } libusb_interface_descriptor __aligned(sizeof(void *)); 317 318 typedef struct libusb_interface { 319 struct libusb_interface_descriptor *altsetting; 320 int num_altsetting; 321 } libusb_interface __aligned(sizeof(void *)); 322 323 typedef struct libusb_config_descriptor { 324 uint8_t bLength; 325 uint8_t bDescriptorType; 326 uint16_t wTotalLength; 327 uint8_t bNumInterfaces; 328 uint8_t bConfigurationValue; 329 uint8_t iConfiguration; 330 uint8_t bmAttributes; 331 uint8_t MaxPower; 332 struct libusb_interface *interface; 333 unsigned char *extra; 334 int extra_length; 335 } libusb_config_descriptor __aligned(sizeof(void *)); 336 337 typedef struct libusb_control_setup { 338 uint8_t bmRequestType; 339 uint8_t bRequest; 340 uint16_t wValue; 341 uint16_t wIndex; 342 uint16_t wLength; 343 } libusb_control_setup; 344 345 typedef struct libusb_iso_packet_descriptor { 346 unsigned int length; 347 unsigned int actual_length; 348 enum libusb_transfer_status status; 349 } libusb_iso_packet_descriptor __aligned(sizeof(void *)); 350 351 struct libusb_transfer; 352 353 typedef void (*libusb_transfer_cb_fn) (struct libusb_transfer *transfer); 354 355 typedef struct libusb_transfer { 356 libusb_device_handle *dev_handle; 357 uint8_t flags; 358 unsigned int endpoint; 359 unsigned char type; 360 unsigned int timeout; 361 enum libusb_transfer_status status; 362 int length; 363 int actual_length; 364 libusb_transfer_cb_fn callback; 365 void *user_data; 366 unsigned char *buffer; 367 void *os_priv; 368 int num_iso_packets; 369 struct libusb_iso_packet_descriptor iso_packet_desc[0]; 370 } libusb_transfer __aligned(sizeof(void *)); 371 372 /* Library initialisation */ 373 374 void libusb_set_debug(libusb_context * ctx, int level); 375 int libusb_init(libusb_context ** context); 376 void libusb_exit(struct libusb_context *ctx); 377 378 /* Device handling and enumeration */ 379 380 ssize_t libusb_get_device_list(libusb_context * ctx, libusb_device *** list); 381 void libusb_free_device_list(libusb_device ** list, int unref_devices); 382 uint8_t libusb_get_bus_number(libusb_device * dev); 383 uint8_t libusb_get_device_address(libusb_device * dev); 384 int libusb_clear_halt(libusb_device_handle *devh, unsigned char endpoint); 385 int libusb_get_max_packet_size(libusb_device * dev, unsigned char endpoint); 386 libusb_device *libusb_ref_device(libusb_device * dev); 387 void libusb_unref_device(libusb_device * dev); 388 int libusb_open(libusb_device * dev, libusb_device_handle ** devh); 389 libusb_device_handle *libusb_open_device_with_vid_pid(libusb_context * ctx, uint16_t vendor_id, uint16_t product_id); 390 void libusb_close(libusb_device_handle * devh); 391 libusb_device *libusb_get_device(libusb_device_handle * devh); 392 int libusb_get_configuration(libusb_device_handle * devh, int *config); 393 int libusb_set_configuration(libusb_device_handle * devh, int configuration); 394 int libusb_claim_interface(libusb_device_handle * devh, int interface_number); 395 int libusb_release_interface(libusb_device_handle * devh, int interface_number); 396 int libusb_kernel_driver_active(libusb_device_handle * devh, int interface); 397 int libusb_detach_kernel_driver(libusb_device_handle * devh, int interface); 398 int libusb_attach_kernel_driver(libusb_device_handle * devh, int interface); 399 int libusb_set_interface_alt_setting(libusb_device_handle * devh, int interface_number, int alternate_setting); 400 401 /* USB Descriptors */ 402 403 int libusb_get_device_descriptor(libusb_device * dev, struct libusb_device_descriptor *desc); 404 int libusb_get_active_config_descriptor(libusb_device * dev, struct libusb_config_descriptor **config); 405 int libusb_get_config_descriptor(libusb_device * dev, uint8_t config_index, struct libusb_config_descriptor **config); 406 int libusb_get_config_descriptor_by_value(libusb_device * dev, uint8_t bConfigurationValue, struct libusb_config_descriptor **config); 407 void libusb_free_config_descriptor(struct libusb_config_descriptor *config); 408 int libusb_get_string_descriptor_ascii(libusb_device_handle * dev, uint8_t desc_index, unsigned char *data, int length); 409 410 /* Asynchronous device I/O*/ 411 412 struct libusb_transfer *libusb_alloc_transfer(int iso_packets); 413 void libusb_free_transfer(struct libusb_transfer *transfer); 414 int libusb_submit_transfer(struct libusb_transfer *transfer); 415 int libusb_cancel_transfer(struct libusb_transfer *transfer); 416 unsigned char *libusb_get_iso_packet_buffer_simple(struct libusb_transfer *transfer, unsigned int packet); 417 418 /* Polling and timing */ 419 420 int libusb_try_lock_events(libusb_context * ctx); 421 void libusb_lock_events(libusb_context * ctx); 422 void libusb_unlock_events(libusb_context * ctx); 423 int libusb_event_handling_ok(libusb_context * ctx); 424 int libusb_event_handler_active(libusb_context * ctx); 425 void libusb_lock_event_waiters(libusb_context * ctx); 426 void libusb_unlock_event_waiters(libusb_context * ctx); 427 int libusb_wait_for_event(libusb_context * ctx, struct timeval *tv); 428 int libusb_handle_events_timeout(libusb_context * ctx, struct timeval *tv); 429 int libusb_handle_events(libusb_context * ctx); 430 int libusb_handle_events_locked(libusb_context * ctx, struct timeval *tv); 431 int libusb_get_next_timeout(libusb_context * ctx, struct timeval *tv); 432 void libusb_set_pollfd_notifiers(libusb_context * ctx, libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb, void *user_data); 433 struct libusb_pollfd **libusb_get_pollfds(libusb_context * ctx); 434 435 /* Synchronous device I/O */ 436 437 int libusb_control_transfer(libusb_device_handle * devh, uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, unsigned char *data, uint16_t wLength, unsigned int timeout); 438 int libusb_bulk_transfer(struct libusb_device_handle *devh, unsigned char endpoint, unsigned char *data, int length, int *transferred, unsigned int timeout); 439 int libusb_interrupt_transfer(struct libusb_device_handle *devh, unsigned char endpoint, unsigned char *data, int length, int *transferred, unsigned int timeout); 440 441 #if 0 442 { /* indent fix */ 443 #endif 444 #ifdef __cplusplus 445 } 446 447 #endif 448 449 #endif /* __LIBUSB_H__ */ 450