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