33 #ifdef HAVE_SYS_TIME_H 42 #if !defined(__cplusplus) && !defined(static_assert) && !defined(_MSC_VER) 43 #define static_assert(cond, msg) _Static_assert(cond, msg) 46 #define container_of(ptr, type, member) \ 47 ((type *)((uintptr_t)(ptr) - (uintptr_t)offsetof(type, member))) 50 #define ARRAYSIZE(array) (sizeof(array) / sizeof(array[0])) 54 #define CLAMP(val, min, max) \ 55 ((val) < (min) ? (min) : ((val) > (max) ? (max) : (val))) 59 #define MIN(a, b) ((a) < (b) ? (a) : (b)) 63 #define MAX(a, b) ((a) > (b) ? (a) : (b)) 67 #if defined(UNREFERENCED_PARAMETER) 68 #define UNUSED(var) UNREFERENCED_PARAMETER(var) 70 #define UNUSED(var) do { (void)(var); } while(0) 74 #define PTR_ALIGN(v) \ 75 (((v) + (sizeof(void *) - 1)) & ~(sizeof(void *) - 1)) 78 #if defined(EVENTS_POSIX) 79 #include "os/events_posix.h" 80 #elif defined(EVENTS_WINDOWS) 81 #include "os/events_windows.h" 85 #if defined(THREADS_POSIX) 86 #include "os/threads_posix.h" 87 #elif defined(THREADS_WINDOWS) 88 #include "os/threads_windows.h" 98 #define API_EXPORTED LIBUSB_CALL DEFAULT_VISIBILITY 104 #define USBI_PRINTFLIKE(formatarg, firstvararg) \ 105 __attribute__ ((__format__ (__printf__, formatarg, firstvararg))) 107 #define USBI_PRINTFLIKE(formatarg, firstvararg) 114 #define USB_MAXENDPOINTS 32 115 #define USB_MAXINTERFACES 32 116 #define USB_MAXCONFIG 8 119 #define USBI_CAP_HAS_HID_ACCESS 0x00010000 120 #define USBI_CAP_SUPPORTS_DETACH_KERNEL_DRIVER 0x00020000 123 #define USBI_MAX_LOG_LEN 1024 125 #define USBI_LOG_LINE_END "\n" 136 #define list_entry(ptr, type, member) \ 137 container_of(ptr, type, member) 139 #define list_first_entry(ptr, type, member) \ 140 list_entry((ptr)->next, type, member) 142 #define list_next_entry(ptr, type, member) \ 143 list_entry((ptr)->member.next, type, member) 151 #define list_for_each_entry(pos, head, member, type) \ 152 for (pos = list_first_entry(head, type, member); \ 153 &pos->member != (head); \ 154 pos = list_next_entry(pos, type, member)) 156 #define list_for_each_entry_safe(pos, n, head, member, type) \ 157 for (pos = list_first_entry(head, type, member), \ 158 n = list_next_entry(pos, type, member); \ 159 &pos->member != (head); \ 160 pos = n, n = list_next_entry(n, type, member)) 165 #define for_each_helper(pos, head, type) \ 166 list_for_each_entry(pos, head, list, type) 168 #define for_each_safe_helper(pos, n, head, type) \ 169 list_for_each_entry_safe(pos, n, head, list, type) 171 #define list_empty(entry) ((entry)->next == (entry)) 173 static inline void list_init(
struct list_head *entry)
175 entry->prev = entry->next = entry;
180 entry->next = head->next;
183 head->next->prev = entry;
187 static inline void list_add_tail(
struct list_head *entry,
191 entry->prev = head->prev;
193 head->prev->next = entry;
197 static inline void list_del(
struct list_head *entry)
199 entry->next->prev = entry->prev;
200 entry->prev->next = entry->next;
201 entry->next = entry->prev = NULL;
206 if (list_empty(head))
209 list->next = head->next;
210 list->next->prev = list;
211 list->prev = head->prev;
212 list->prev->next = list;
217 static inline void *usbi_reallocf(
void *ptr,
size_t size)
219 void *ret = realloc(ptr, size);
226 #define TIMESPEC_IS_SET(ts) ((ts)->tv_sec || (ts)->tv_nsec) 227 #define TIMESPEC_CLEAR(ts) (ts)->tv_sec = (ts)->tv_nsec = 0 228 #define TIMESPEC_CMP(a, b, CMP) \ 229 (((a)->tv_sec == (b)->tv_sec) \ 230 ? ((a)->tv_nsec CMP (b)->tv_nsec) \ 231 : ((a)->tv_sec CMP (b)->tv_sec)) 232 #define TIMESPEC_SUB(a, b, result) \ 234 (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \ 235 (result)->tv_nsec = (a)->tv_nsec - (b)->tv_nsec; \ 236 if ((result)->tv_nsec < 0L) { \ 237 --(result)->tv_sec; \ 238 (result)->tv_nsec += 1000000000L; \ 243 #define TIMEVAL_TV_SEC_TYPE long 245 #define TIMEVAL_TV_SEC_TYPE time_t 249 #ifndef TIMESPEC_TO_TIMEVAL 250 #define TIMESPEC_TO_TIMEVAL(tv, ts) \ 252 (tv)->tv_sec = (TIMEVAL_TV_SEC_TYPE) (ts)->tv_sec; \ 253 (tv)->tv_usec = (ts)->tv_nsec / 1000L; \ 257 #ifdef ENABLE_LOGGING 259 #if defined(_MSC_VER) && (_MSC_VER < 1900) 261 #define snprintf usbi_snprintf 262 #define vsnprintf usbi_vsnprintf 263 int usbi_snprintf(
char *dst,
size_t size,
const char *format, ...);
264 int usbi_vsnprintf(
char *dst,
size_t size,
const char *format, va_list ap);
265 #define LIBUSB_PRINTF_WIN32 269 const char *
function,
const char *format, ...) USBI_PRINTFLIKE(4, 5);
272 const char *
function,
const char *format, va_list args) USBI_PRINTFLIKE(4, 0);
274 #define _usbi_log(ctx, level, ...) usbi_log(ctx, level, __func__, __VA_ARGS__) 276 #define usbi_err(ctx, ...) _usbi_log(ctx, LIBUSB_LOG_LEVEL_ERROR, __VA_ARGS__) 277 #define usbi_warn(ctx, ...) _usbi_log(ctx, LIBUSB_LOG_LEVEL_WARNING, __VA_ARGS__) 278 #define usbi_info(ctx, ...) _usbi_log(ctx, LIBUSB_LOG_LEVEL_INFO, __VA_ARGS__) 279 #define usbi_dbg(...) _usbi_log(NULL, LIBUSB_LOG_LEVEL_DEBUG, __VA_ARGS__) 283 #define usbi_err(ctx, ...) UNUSED(ctx) 284 #define usbi_warn(ctx, ...) UNUSED(ctx) 285 #define usbi_info(ctx, ...) UNUSED(ctx) 286 #define usbi_dbg(...) do {} while (0) 290 #define DEVICE_CTX(dev) ((dev)->ctx) 291 #define HANDLE_CTX(handle) (DEVICE_CTX((handle)->dev)) 292 #define TRANSFER_CTX(transfer) (HANDLE_CTX((transfer)->dev_handle)) 293 #define ITRANSFER_CTX(itransfer) \ 294 (TRANSFER_CTX(USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer))) 296 #define IS_EPIN(ep) (0 != ((ep) & LIBUSB_ENDPOINT_IN)) 297 #define IS_EPOUT(ep) (!IS_EPIN(ep)) 298 #define IS_XFERIN(xfer) (0 != ((xfer)->endpoint & LIBUSB_ENDPOINT_IN)) 299 #define IS_XFEROUT(xfer) (!IS_XFERIN(xfer)) 302 #if defined(ENABLE_LOGGING) && !defined(ENABLE_DEBUG_LOGGING) 318 usbi_mutex_t usb_devs_lock;
323 usbi_mutex_t open_devs_lock;
328 usbi_mutex_t hotplug_cbs_lock;
337 usbi_mutex_t flying_transfers_lock;
339 #if !defined(_WIN32) && !defined(__CYGWIN__) 343 void *fd_cb_user_data;
347 usbi_mutex_t events_lock;
350 int event_handler_active;
354 usbi_tls_key_t event_handling_key;
358 usbi_mutex_t event_waiters_lock;
359 usbi_cond_t event_waiters_cond;
362 usbi_mutex_t event_data_lock;
366 unsigned int event_flags;
370 unsigned int device_close;
382 unsigned int event_data_cnt;
395 extern struct list_head active_contexts_list;
396 extern usbi_mutex_static_t active_contexts_lock;
400 return ctx ? ctx : usbi_default_context;
403 enum usbi_event_flags {
405 USBI_EVENT_EVENT_SOURCES_MODIFIED = 1U << 0,
408 USBI_EVENT_USER_INTERRUPT = 1U << 1,
411 USBI_EVENT_HOTPLUG_CB_DEREGISTERED = 1U << 2,
414 USBI_EVENT_HOTPLUG_MSG_PENDING = 1U << 3,
417 USBI_EVENT_TRANSFER_COMPLETED = 1U << 4,
420 USBI_EVENT_DEVICE_CLOSE = 1U << 5,
424 static inline int usbi_handling_events(
struct libusb_context *ctx)
426 return usbi_tls_key_get(ctx->event_handling_key) != NULL;
429 static inline void usbi_start_event_handling(
struct libusb_context *ctx)
431 usbi_tls_key_set(ctx->event_handling_key, ctx);
434 static inline void usbi_end_event_handling(
struct libusb_context *ctx)
436 usbi_tls_key_set(ctx->event_handling_key, NULL);
450 uint8_t device_address;
454 unsigned long session_data;
463 unsigned long claimed_interfaces;
467 int auto_detach_kernel_driver;
481 #ifdef HAVE_CLOCK_GETTIME 482 #define USBI_CLOCK_REALTIME CLOCK_REALTIME 483 #define USBI_CLOCK_MONOTONIC CLOCK_MONOTONIC 484 #define usbi_clock_gettime clock_gettime 495 #define USBI_CLOCK_REALTIME 0 496 #define USBI_CLOCK_MONOTONIC 1 497 int usbi_clock_gettime(
int clk_id,
struct timespec *tp);
517 uint32_t state_flags;
518 uint32_t timeout_flags;
534 enum usbi_transfer_state_flags {
536 USBI_TRANSFER_IN_FLIGHT = 1U << 0,
539 USBI_TRANSFER_CANCELLING = 1U << 1,
542 USBI_TRANSFER_DEVICE_DISAPPEARED = 1U << 2,
545 enum usbi_transfer_timeout_flags {
547 USBI_TRANSFER_OS_HANDLES_TIMEOUT = 1U << 0,
550 USBI_TRANSFER_TIMEOUT_HANDLED = 1U << 1,
553 USBI_TRANSFER_TIMED_OUT = 1U << 2,
556 #define USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer) \ 557 ((struct libusb_transfer *) \ 558 ((unsigned char *)(itransfer) \ 559 + PTR_ALIGN(sizeof(struct usbi_transfer)))) 560 #define LIBUSB_TRANSFER_TO_USBI_TRANSFER(transfer) \ 561 ((struct usbi_transfer *) \ 562 ((unsigned char *)(transfer) \ 563 - PTR_ALIGN(sizeof(struct usbi_transfer)))) 566 #pragma pack(push, 1) 572 uint8_t bDescriptorType;
577 uint8_t bDescriptorType;
579 uint8_t bDeviceClass;
580 uint8_t bDeviceSubClass;
581 uint8_t bDeviceProtocol;
582 uint8_t bMaxPacketSize0;
586 uint8_t iManufacturer;
588 uint8_t iSerialNumber;
589 uint8_t bNumConfigurations;
594 uint8_t bDescriptorType;
595 uint16_t wTotalLength;
596 uint8_t bNumInterfaces;
597 uint8_t bConfigurationValue;
598 uint8_t iConfiguration;
599 uint8_t bmAttributes;
605 uint8_t bDescriptorType;
606 uint8_t bInterfaceNumber;
607 uint8_t bAlternateSetting;
608 uint8_t bNumEndpoints;
609 uint8_t bInterfaceClass;
610 uint8_t bInterfaceSubClass;
611 uint8_t bInterfaceProtocol;
617 uint8_t bDescriptorType;
623 uint8_t bDescriptorType;
624 uint16_t wTotalLength;
625 uint8_t bNumDeviceCaps;
638 unsigned long session_id);
640 unsigned long session_id);
644 int usbi_handle_transfer_completion(
struct usbi_transfer *itransfer,
646 int usbi_handle_transfer_cancellation(
struct usbi_transfer *itransfer);
647 void usbi_signal_transfer_completion(
struct usbi_transfer *itransfer);
654 usbi_os_handle_t os_handle;
660 int usbi_add_event_source(
struct libusb_context *ctx, usbi_os_handle_t os_handle,
662 void usbi_remove_event_source(
struct libusb_context *ctx, usbi_os_handle_t os_handle);
681 return usbi_timer_valid(&ctx->timer);
691 unsigned int event_triggered:1;
693 unsigned int timer_triggered:1;
696 unsigned int event_bits;
699 unsigned int event_data_count;
700 unsigned int num_ready;
709 static inline void *usbi_get_context_priv(
struct libusb_context *ctx)
711 return (
unsigned char *)ctx + PTR_ALIGN(
sizeof(*ctx));
714 static inline void *usbi_get_device_priv(
struct libusb_device *dev)
716 return (
unsigned char *)dev + PTR_ALIGN(
sizeof(*dev));
721 return (
unsigned char *)dev_handle + PTR_ALIGN(
sizeof(*dev_handle));
724 static inline void *usbi_get_transfer_priv(
struct usbi_transfer *itransfer)
726 return itransfer->priv;
939 int (*get_active_config_descriptor)(
struct libusb_device *device,
940 void *buffer,
size_t len);
963 uint8_t config_index,
void *buffer,
size_t len);
977 int (*get_config_descriptor_by_value)(
struct libusb_device *device,
978 uint8_t bConfigurationValue,
void **buffer);
1067 uint8_t interface_number, uint8_t altsetting);
1103 uint32_t num_streams,
unsigned char *endpoints,
int num_endpoints);
1107 unsigned char *endpoints,
int num_endpoints);
1131 uint8_t interface_number);
1147 uint8_t interface_number);
1164 uint8_t interface_number);
1243 void *event_data,
unsigned int count,
unsigned int num_ready);
1268 int (*handle_transfer_completion)(
struct usbi_transfer *itransfer);
1274 size_t context_priv_size;
1280 size_t device_priv_size;
1286 size_t device_handle_priv_size;
1292 size_t transfer_priv_size;
1297 #define for_each_context(c) \ 1298 for_each_helper(c, &active_contexts_list, struct libusb_context) 1300 #define for_each_device(ctx, d) \ 1301 for_each_helper(d, &(ctx)->usb_devs, struct libusb_device) 1303 #define for_each_device_safe(ctx, d, n) \ 1304 for_each_safe_helper(d, n, &(ctx)->usb_devs, struct libusb_device) 1306 #define for_each_open_device(ctx, h) \ 1307 for_each_helper(h, &(ctx)->open_devs, struct libusb_device_handle) 1309 #define for_each_transfer(ctx, t) \ 1310 for_each_helper(t, &(ctx)->flying_transfers, struct usbi_transfer) 1312 #define for_each_transfer_safe(ctx, t, n) \ 1313 for_each_safe_helper(t, n, &(ctx)->flying_transfers, struct usbi_transfer) 1315 #define for_each_event_source(ctx, e) \ 1316 for_each_helper(e, &(ctx)->event_sources, struct usbi_event_source) 1318 #define for_each_removed_event_source(ctx, e) \ 1319 for_each_helper(e, &(ctx)->removed_event_sources, struct usbi_event_source) 1321 #define for_each_removed_event_source_safe(ctx, e, n) \ 1322 for_each_safe_helper(e, n, &(ctx)->removed_event_sources, struct usbi_event_source) Definition: libusbi.h:301
Definition: libusbi.h:749
#define libusb_le16_to_cpu
Definition: libusb.h:175
Definition: events_windows.h:35
Definition: libusbi.h:653
libusb_option
Definition: libusb.h:2059
libusb_speed
Definition: libusb.h:1017
uint16_t bcdDevice
Definition: libusb.h:559
Definition: libusbi.h:615
Definition: libusbi.h:127
Definition: libusbi.h:460
Definition: libusbi.h:652
libusb_transfer_status
Definition: libusb.h:1115
int libusb_hotplug_callback_handle
Definition: libusb.h:1930
Definition: libusbi.h:575
typedef void(LIBUSB_CALL *libusb_transfer_cb_fn)(struct libusb_transfer *transfer)
Definition: libusbi.h:439
Definition: libusbi.h:592
Definition: libusbi.h:603
uint16_t idProduct
Definition: libusb.h:556
Definition: libusbi.h:621
uint16_t idVendor
Definition: libusb.h:553
void(LIBUSB_CALL * libusb_log_cb)(libusb_context *ctx, enum libusb_log_level level, const char *str)
Definition: libusb.h:1343
Definition: sunos_usb.h:37
void(LIBUSB_CALL * libusb_pollfd_added_cb)(int fd, short events, void *user_data)
Definition: libusb.h:1897
void(LIBUSB_CALL * libusb_pollfd_removed_cb)(int fd, void *user_data)
Definition: libusb.h:1909
Definition: threads_windows.h:65
libusb_log_level
Definition: libusb.h:1306
Definition: libusbi.h:510
Definition: libusbi.h:736
uint16_t bcdUSB
Definition: libusb.h:536
Definition: libusbi.h:688
Definition: events_posix.h:37