2 * Internal header for libusb
3 * Copyright © 2007-2009 Daniel Drake <dsd@gentoo.org>
4 * Copyright © 2001 Johannes Erdfelt <johannes@erdfelt.com>
5 * Copyright © 2019 Nathan Hjelm <hjelmn@cs.umm.edu>
6 * Copyright © 2019-2020 Google LLC. All rights reserved.
7 * Copyright © 2020 Chris Dickens <christopher.a.dickens@gmail.com>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
34 #ifdef HAVE_SYS_TIME_H
40 /* Not all C standard library headers define static_assert in assert.h
41 * Additionally, Visual Studio treats static_assert as a keyword.
43 #if !defined(__cplusplus) && !defined(static_assert) && !defined(_MSC_VER)
44 #define static_assert(cond, msg) _Static_assert(cond, msg)
48 #define ASSERT_EQ(expression, value) (void)expression
49 #define ASSERT_NE(expression, value) (void)expression
51 #define ASSERT_EQ(expression, value) assert(expression == value)
52 #define ASSERT_NE(expression, value) assert(expression != value)
55 #define container_of(ptr, type, member) \
56 ((type *)((uintptr_t)(ptr) - (uintptr_t)offsetof(type, member)))
59 #define ARRAYSIZE(array) (sizeof(array) / sizeof(array[0]))
63 #define CLAMP(val, min, max) \
64 ((val) < (min) ? (min) : ((val) > (max) ? (max) : (val)))
68 #define MIN(a, b) ((a) < (b) ? (a) : (b))
72 #define MAX(a, b) ((a) > (b) ? (a) : (b))
75 /* The following is used to silence warnings for unused variables */
76 #if defined(UNREFERENCED_PARAMETER)
77 #define UNUSED(var) UNREFERENCED_PARAMETER(var)
79 #define UNUSED(var) do { (void)(var); } while(0)
82 /* Macro to align a value up to the next multiple of the size of a pointer */
83 #define PTR_ALIGN(v) \
84 (((v) + (sizeof(void *) - 1)) & ~(sizeof(void *) - 1))
88 * Useful for reference counting or when accessing a value without a lock
90 * The following atomic operations are defined:
91 * usbi_atomic_load() - Atomically read a variable's value
92 * usbi_atomic_store() - Atomically write a new value value to a variable
93 * usbi_atomic_inc() - Atomically increment a variable's value and return the new value
94 * usbi_atomic_dec() - Atomically decrement a variable's value and return the new value
96 * All of these operations are ordered with each other, thus the effects of
97 * any one operation is guaranteed to be seen by any other operation.
100 typedef volatile LONG usbi_atomic_t;
101 #define usbi_atomic_load(a) (*(a))
102 #define usbi_atomic_store(a, v) (*(a)) = (v)
103 #define usbi_atomic_inc(a) InterlockedIncrement((a))
104 #define usbi_atomic_dec(a) InterlockedDecrement((a))
106 #include <stdatomic.h>
107 typedef atomic_long usbi_atomic_t;
108 #define usbi_atomic_load(a) atomic_load((a))
109 #define usbi_atomic_store(a, v) atomic_store((a), (v))
110 #define usbi_atomic_inc(a) (atomic_fetch_add((a), 1) + 1)
111 #define usbi_atomic_dec(a) (atomic_fetch_add((a), -1) - 1)
114 /* Internal abstractions for event handling and thread synchronization */
115 #if defined(PLATFORM_POSIX)
116 #include "os/events_posix.h"
117 #include "os/threads_posix.h"
118 #elif defined(PLATFORM_WINDOWS)
119 #include "os/events_windows.h"
120 #include "os/threads_windows.h"
123 /* Inside the libusb code, mark all public functions as follows:
124 * return_type API_EXPORTED function_name(params) { ... }
125 * But if the function returns a pointer, mark it as follows:
126 * DEFAULT_VISIBILITY return_type * LIBUSB_CALL function_name(params) { ... }
127 * In the libusb public header, mark all declarations as:
128 * return_type LIBUSB_CALL function_name(params);
130 #define API_EXPORTED LIBUSB_CALL DEFAULT_VISIBILITY
136 #define USB_MAXENDPOINTS 32
137 #define USB_MAXINTERFACES 32
138 #define USB_MAXCONFIG 8
140 /* Backend specific capabilities */
141 #define USBI_CAP_HAS_HID_ACCESS 0x00010000
142 #define USBI_CAP_SUPPORTS_DETACH_KERNEL_DRIVER 0x00020000
144 /* Maximum number of bytes in a log line */
145 #define USBI_MAX_LOG_LEN 1024
146 /* Terminator for log lines */
147 #define USBI_LOG_LINE_END "\n"
150 struct list_head *prev, *next;
153 /* Get an entry from the list
154 * ptr - the address of this list_head element in "type"
155 * type - the data type that contains "member"
156 * member - the list_head element in "type"
158 #define list_entry(ptr, type, member) \
159 container_of(ptr, type, member)
161 #define list_first_entry(ptr, type, member) \
162 list_entry((ptr)->next, type, member)
164 #define list_next_entry(ptr, type, member) \
165 list_entry((ptr)->member.next, type, member)
167 /* Get each entry from a list
168 * pos - A structure pointer has a "member" element
170 * member - the list_head element in "pos"
171 * type - the type of the first parameter
173 #define list_for_each_entry(pos, head, member, type) \
174 for (pos = list_first_entry(head, type, member); \
175 &pos->member != (head); \
176 pos = list_next_entry(pos, type, member))
178 #define list_for_each_entry_safe(pos, n, head, member, type) \
179 for (pos = list_first_entry(head, type, member), \
180 n = list_next_entry(pos, type, member); \
181 &pos->member != (head); \
182 pos = n, n = list_next_entry(n, type, member))
184 /* Helper macros to iterate over a list. The structure pointed
185 * to by "pos" must have a list_head member named "list".
187 #define for_each_helper(pos, head, type) \
188 list_for_each_entry(pos, head, list, type)
190 #define for_each_safe_helper(pos, n, head, type) \
191 list_for_each_entry_safe(pos, n, head, list, type)
193 #define list_empty(entry) ((entry)->next == (entry))
195 static inline void list_init(struct list_head *entry)
197 entry->prev = entry->next = entry;
200 static inline void list_add(struct list_head *entry, struct list_head *head)
202 entry->next = head->next;
205 head->next->prev = entry;
209 static inline void list_add_tail(struct list_head *entry,
210 struct list_head *head)
213 entry->prev = head->prev;
215 head->prev->next = entry;
219 static inline void list_del(struct list_head *entry)
221 entry->next->prev = entry->prev;
222 entry->prev->next = entry->next;
223 entry->next = entry->prev = NULL;
226 static inline void list_cut(struct list_head *list, struct list_head *head)
228 if (list_empty(head)) {
233 list->next = head->next;
234 list->next->prev = list;
235 list->prev = head->prev;
236 list->prev->next = list;
241 static inline void list_splice_front(struct list_head *list, struct list_head *head)
243 list->next->prev = head;
244 list->prev->next = head->next;
245 head->next->prev = list->prev;
246 head->next = list->next;
249 static inline void *usbi_reallocf(void *ptr, size_t size)
251 void *ret = realloc(ptr, size);
258 #if !defined(USEC_PER_SEC)
259 #define USEC_PER_SEC 1000000L
262 #if !defined(NSEC_PER_SEC)
263 #define NSEC_PER_SEC 1000000000L
266 #define TIMEVAL_IS_VALID(tv) \
267 ((tv)->tv_sec >= 0 && \
268 (tv)->tv_usec >= 0 && (tv)->tv_usec < USEC_PER_SEC)
270 #define TIMESPEC_IS_SET(ts) ((ts)->tv_sec || (ts)->tv_nsec)
271 #define TIMESPEC_CLEAR(ts) (ts)->tv_sec = (ts)->tv_nsec = 0
272 #define TIMESPEC_CMP(a, b, CMP) \
273 (((a)->tv_sec == (b)->tv_sec) \
274 ? ((a)->tv_nsec CMP (b)->tv_nsec) \
275 : ((a)->tv_sec CMP (b)->tv_sec))
276 #define TIMESPEC_SUB(a, b, result) \
278 (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
279 (result)->tv_nsec = (a)->tv_nsec - (b)->tv_nsec; \
280 if ((result)->tv_nsec < 0L) { \
281 --(result)->tv_sec; \
282 (result)->tv_nsec += NSEC_PER_SEC; \
286 #if defined(PLATFORM_WINDOWS)
287 #define TIMEVAL_TV_SEC_TYPE long
289 #define TIMEVAL_TV_SEC_TYPE time_t
292 /* Some platforms don't have this define */
293 #ifndef TIMESPEC_TO_TIMEVAL
294 #define TIMESPEC_TO_TIMEVAL(tv, ts) \
296 (tv)->tv_sec = (TIMEVAL_TV_SEC_TYPE) (ts)->tv_sec; \
297 (tv)->tv_usec = (ts)->tv_nsec / 1000L; \
301 #ifdef ENABLE_LOGGING
303 #if defined(_MSC_VER) && (_MSC_VER < 1900)
305 #define snprintf usbi_snprintf
306 #define vsnprintf usbi_vsnprintf
307 int usbi_snprintf(char *dst, size_t size, const char *format, ...);
308 int usbi_vsnprintf(char *dst, size_t size, const char *format, va_list args);
309 #define LIBUSB_PRINTF_WIN32
310 #endif /* defined(_MSC_VER) && (_MSC_VER < 1900) */
312 void usbi_log(struct libusb_context *ctx, enum libusb_log_level level,
313 const char *function, const char *format, ...) PRINTF_FORMAT(4, 5);
315 #define _usbi_log(ctx, level, ...) usbi_log(ctx, level, __func__, __VA_ARGS__)
317 #define usbi_err(ctx, ...) _usbi_log(ctx, LIBUSB_LOG_LEVEL_ERROR, __VA_ARGS__)
318 #define usbi_warn(ctx, ...) _usbi_log(ctx, LIBUSB_LOG_LEVEL_WARNING, __VA_ARGS__)
319 #define usbi_info(ctx, ...) _usbi_log(ctx, LIBUSB_LOG_LEVEL_INFO, __VA_ARGS__)
320 #define usbi_dbg(ctx ,...) _usbi_log(ctx, LIBUSB_LOG_LEVEL_DEBUG, __VA_ARGS__)
322 #else /* ENABLE_LOGGING */
324 #define usbi_err(ctx, ...) UNUSED(ctx)
325 #define usbi_warn(ctx, ...) UNUSED(ctx)
326 #define usbi_info(ctx, ...) UNUSED(ctx)
327 #define usbi_dbg(ctx, ...) do {} while (0)
329 #endif /* ENABLE_LOGGING */
331 #define DEVICE_CTX(dev) ((dev)->ctx)
332 #define HANDLE_CTX(handle) (DEVICE_CTX((handle)->dev))
333 #define TRANSFER_CTX(transfer) (HANDLE_CTX((transfer)->dev_handle))
334 #define ITRANSFER_CTX(itransfer) \
335 (TRANSFER_CTX(USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer)))
337 #define IS_EPIN(ep) (0 != ((ep) & LIBUSB_ENDPOINT_IN))
338 #define IS_EPOUT(ep) (!IS_EPIN(ep))
339 #define IS_XFERIN(xfer) (0 != ((xfer)->endpoint & LIBUSB_ENDPOINT_IN))
340 #define IS_XFEROUT(xfer) (!IS_XFERIN(xfer))
342 struct libusb_context {
343 #if defined(ENABLE_LOGGING) && !defined(ENABLE_DEBUG_LOGGING)
344 enum libusb_log_level debug;
346 libusb_log_cb log_handler;
349 /* used for signalling occurrence of an internal event. */
353 /* used for timeout handling, if supported by OS.
354 * this timer is maintained to trigger on the next pending timeout */
358 struct list_head usb_devs;
359 usbi_mutex_t usb_devs_lock;
361 /* A list of open handles. Backends are free to traverse this if required.
363 struct list_head open_devs;
364 usbi_mutex_t open_devs_lock;
366 /* A list of registered hotplug callbacks */
367 struct list_head hotplug_cbs;
368 libusb_hotplug_callback_handle next_hotplug_cb_handle;
369 usbi_mutex_t hotplug_cbs_lock;
371 /* A flag to indicate that the context is ready for hotplug notifications */
372 usbi_atomic_t hotplug_ready;
374 /* this is a list of in-flight transfer handles, sorted by timeout
375 * expiration. URBs to timeout the soonest are placed at the beginning of
376 * the list, URBs that will time out later are placed after, and urbs with
377 * infinite timeout are always placed at the very end. */
378 struct list_head flying_transfers;
379 /* Note paths taking both this and usbi_transfer->lock must always
380 * take this lock first */
381 usbi_mutex_t flying_transfers_lock;
383 #if !defined(PLATFORM_WINDOWS)
384 /* user callbacks for pollfd changes */
385 libusb_pollfd_added_cb fd_added_cb;
386 libusb_pollfd_removed_cb fd_removed_cb;
387 void *fd_cb_user_data;
390 /* ensures that only one thread is handling events at any one time */
391 usbi_mutex_t events_lock;
393 /* used to see if there is an active thread doing event handling */
394 int event_handler_active;
396 /* A thread-local storage key to track which thread is performing event
398 usbi_tls_key_t event_handling_key;
400 /* used to wait for event completion in threads other than the one that is
402 usbi_mutex_t event_waiters_lock;
403 usbi_cond_t event_waiters_cond;
405 /* A lock to protect internal context event data. */
406 usbi_mutex_t event_data_lock;
408 /* A bitmask of flags that are set to indicate specific events that need to
409 * be handled. Protected by event_data_lock. */
410 unsigned int event_flags;
412 /* A counter that is set when we want to interrupt and prevent event handling,
413 * in order to safely close a device. Protected by event_data_lock. */
414 unsigned int device_close;
416 /* A list of currently active event sources. Protected by event_data_lock. */
417 struct list_head event_sources;
419 /* A list of event sources that have been removed since the last time
420 * event sources were waited on. Protected by event_data_lock. */
421 struct list_head removed_event_sources;
423 /* A pointer and count to platform-specific data used for monitoring event
424 * sources. Only accessed during event handling. */
426 unsigned int event_data_cnt;
428 /* A list of pending hotplug messages. Protected by event_data_lock. */
429 struct list_head hotplug_msgs;
431 /* A list of pending completed transfers. Protected by event_data_lock. */
432 struct list_head completed_transfers;
434 struct list_head list;
437 extern struct libusb_context *usbi_default_context;
439 extern struct list_head active_contexts_list;
440 extern usbi_mutex_static_t active_contexts_lock;
442 static inline struct libusb_context *usbi_get_context(struct libusb_context *ctx)
444 return ctx ? ctx : usbi_default_context;
447 enum usbi_event_flags {
448 /* The list of event sources has been modified */
449 USBI_EVENT_EVENT_SOURCES_MODIFIED = 1U << 0,
451 /* The user has interrupted the event handler */
452 USBI_EVENT_USER_INTERRUPT = 1U << 1,
454 /* A hotplug callback deregistration is pending */
455 USBI_EVENT_HOTPLUG_CB_DEREGISTERED = 1U << 2,
457 /* One or more hotplug messages are pending */
458 USBI_EVENT_HOTPLUG_MSG_PENDING = 1U << 3,
460 /* One or more completed transfers are pending */
461 USBI_EVENT_TRANSFER_COMPLETED = 1U << 4,
463 /* A device is in the process of being closed */
464 USBI_EVENT_DEVICE_CLOSE = 1U << 5,
467 /* Macros for managing event handling state */
468 static inline int usbi_handling_events(struct libusb_context *ctx)
470 return usbi_tls_key_get(ctx->event_handling_key) != NULL;
473 static inline void usbi_start_event_handling(struct libusb_context *ctx)
475 usbi_tls_key_set(ctx->event_handling_key, ctx);
478 static inline void usbi_end_event_handling(struct libusb_context *ctx)
480 usbi_tls_key_set(ctx->event_handling_key, NULL);
483 struct libusb_device {
484 usbi_atomic_t refcnt;
486 struct libusb_context *ctx;
487 struct libusb_device *parent_dev;
491 uint8_t device_address;
492 enum libusb_speed speed;
494 struct list_head list;
495 unsigned long session_data;
497 struct libusb_device_descriptor device_descriptor;
498 usbi_atomic_t attached;
501 struct libusb_device_handle {
502 /* lock protects claimed_interfaces */
504 unsigned long claimed_interfaces;
506 struct list_head list;
507 struct libusb_device *dev;
508 int auto_detach_kernel_driver;
511 /* Function called by backend during device initialization to convert
512 * multi-byte fields in the device descriptor to host-endian format.
514 static inline void usbi_localize_device_descriptor(struct libusb_device_descriptor *desc)
516 desc->bcdUSB = libusb_le16_to_cpu(desc->bcdUSB);
517 desc->idVendor = libusb_le16_to_cpu(desc->idVendor);
518 desc->idProduct = libusb_le16_to_cpu(desc->idProduct);
519 desc->bcdDevice = libusb_le16_to_cpu(desc->bcdDevice);
522 #ifdef HAVE_CLOCK_GETTIME
523 static inline void usbi_get_monotonic_time(struct timespec *tp)
525 ASSERT_EQ(clock_gettime(CLOCK_MONOTONIC, tp), 0);
527 static inline void usbi_get_real_time(struct timespec *tp)
529 ASSERT_EQ(clock_gettime(CLOCK_REALTIME, tp), 0);
532 /* If the platform doesn't provide the clock_gettime() function, the backend
533 * must provide its own clock implementations. Two clock functions are
536 * usbi_get_monotonic_time(): returns the time since an unspecified starting
537 * point (usually boot) that is monotonically
539 * usbi_get_real_time(): returns the time since system epoch.
541 void usbi_get_monotonic_time(struct timespec *tp);
542 void usbi_get_real_time(struct timespec *tp);
545 /* in-memory transfer layout:
548 * 2. struct usbi_transfer
549 * 3. struct libusb_transfer (which includes iso packets) [variable size]
551 * from a libusb_transfer, you can get the usbi_transfer by rewinding the
552 * appropriate number of bytes.
555 struct usbi_transfer {
557 struct list_head list;
558 struct list_head completed_list;
559 struct timespec timeout;
562 uint32_t state_flags; /* Protected by usbi_transfer->lock */
563 uint32_t timeout_flags; /* Protected by the flying_stransfers_lock */
565 /* this lock is held during libusb_submit_transfer() and
566 * libusb_cancel_transfer() (allowing the OS backend to prevent duplicate
567 * cancellation, submission-during-cancellation, etc). the OS backend
568 * should also take this lock in the handle_events path, to prevent the user
569 * cancelling the transfer from another thread while you are processing
570 * its completion (presumably there would be races within your OS backend
571 * if this were possible).
572 * Note paths taking both this and the flying_transfers_lock must
573 * always take the flying_transfers_lock first */
579 enum usbi_transfer_state_flags {
580 /* Transfer successfully submitted by backend */
581 USBI_TRANSFER_IN_FLIGHT = 1U << 0,
583 /* Cancellation was requested via libusb_cancel_transfer() */
584 USBI_TRANSFER_CANCELLING = 1U << 1,
586 /* Operation on the transfer failed because the device disappeared */
587 USBI_TRANSFER_DEVICE_DISAPPEARED = 1U << 2,
590 enum usbi_transfer_timeout_flags {
591 /* Set by backend submit_transfer() if the OS handles timeout */
592 USBI_TRANSFER_OS_HANDLES_TIMEOUT = 1U << 0,
594 /* The transfer timeout has been handled */
595 USBI_TRANSFER_TIMEOUT_HANDLED = 1U << 1,
597 /* The transfer timeout was successfully processed */
598 USBI_TRANSFER_TIMED_OUT = 1U << 2,
601 #define USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer) \
602 ((struct libusb_transfer *) \
603 ((unsigned char *)(itransfer) \
604 + PTR_ALIGN(sizeof(struct usbi_transfer))))
605 #define LIBUSB_TRANSFER_TO_USBI_TRANSFER(transfer) \
606 ((struct usbi_transfer *) \
607 ((unsigned char *)(transfer) \
608 - PTR_ALIGN(sizeof(struct usbi_transfer))))
611 #pragma pack(push, 1)
614 /* All standard descriptors have these 2 fields in common */
615 struct usbi_descriptor_header {
617 uint8_t bDescriptorType;
620 struct usbi_device_descriptor {
622 uint8_t bDescriptorType;
624 uint8_t bDeviceClass;
625 uint8_t bDeviceSubClass;
626 uint8_t bDeviceProtocol;
627 uint8_t bMaxPacketSize0;
631 uint8_t iManufacturer;
633 uint8_t iSerialNumber;
634 uint8_t bNumConfigurations;
637 struct usbi_configuration_descriptor {
639 uint8_t bDescriptorType;
640 uint16_t wTotalLength;
641 uint8_t bNumInterfaces;
642 uint8_t bConfigurationValue;
643 uint8_t iConfiguration;
644 uint8_t bmAttributes;
648 struct usbi_interface_descriptor {
650 uint8_t bDescriptorType;
651 uint8_t bInterfaceNumber;
652 uint8_t bAlternateSetting;
653 uint8_t bNumEndpoints;
654 uint8_t bInterfaceClass;
655 uint8_t bInterfaceSubClass;
656 uint8_t bInterfaceProtocol;
660 struct usbi_string_descriptor {
662 uint8_t bDescriptorType;
663 uint16_t wData[ZERO_SIZED_ARRAY];
666 struct usbi_bos_descriptor {
668 uint8_t bDescriptorType;
669 uint16_t wTotalLength;
670 uint8_t bNumDeviceCaps;
677 union usbi_config_desc_buf {
678 struct usbi_configuration_descriptor desc;
679 uint8_t buf[LIBUSB_DT_CONFIG_SIZE];
680 uint16_t align; /* Force 2-byte alignment */
683 union usbi_string_desc_buf {
684 struct usbi_string_descriptor desc;
685 uint8_t buf[255]; /* Some devices choke on size > 255 */
686 uint16_t align; /* Force 2-byte alignment */
689 union usbi_bos_desc_buf {
690 struct usbi_bos_descriptor desc;
691 uint8_t buf[LIBUSB_DT_BOS_SIZE];
692 uint16_t align; /* Force 2-byte alignment */
695 enum usbi_hotplug_flags {
696 /* This callback is interested in device arrivals */
697 USBI_HOTPLUG_DEVICE_ARRIVED = LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED,
699 /* This callback is interested in device removals */
700 USBI_HOTPLUG_DEVICE_LEFT = LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT,
702 /* IMPORTANT: The values for the below entries must start *after*
703 * the highest value of the above entries!!!
706 /* The vendor_id field is valid for matching */
707 USBI_HOTPLUG_VENDOR_ID_VALID = (1U << 3),
709 /* The product_id field is valid for matching */
710 USBI_HOTPLUG_PRODUCT_ID_VALID = (1U << 4),
712 /* The dev_class field is valid for matching */
713 USBI_HOTPLUG_DEV_CLASS_VALID = (1U << 5),
715 /* This callback has been unregistered and needs to be freed */
716 USBI_HOTPLUG_NEEDS_FREE = (1U << 6),
719 struct usbi_hotplug_callback {
720 /* Flags that control how this callback behaves */
723 /* Vendor ID to match (if flags says this is valid) */
726 /* Product ID to match (if flags says this is valid) */
729 /* Device class to match (if flags says this is valid) */
732 /* Callback function to invoke for matching event/device */
733 libusb_hotplug_callback_fn cb;
735 /* Handle for this callback (used to match on deregister) */
736 libusb_hotplug_callback_handle handle;
738 /* User data that will be passed to the callback function */
741 /* List this callback is registered in (ctx->hotplug_cbs) */
742 struct list_head list;
745 struct usbi_hotplug_message {
746 /* The hotplug event that occurred */
747 libusb_hotplug_event event;
749 /* The device for which this hotplug event occurred */
750 struct libusb_device *device;
752 /* List this message is contained in (ctx->hotplug_msgs) */
753 struct list_head list;
756 /* shared data and functions */
758 void usbi_hotplug_init(struct libusb_context *ctx);
759 void usbi_hotplug_exit(struct libusb_context *ctx);
760 void usbi_hotplug_notification(struct libusb_context *ctx, struct libusb_device *dev,
761 libusb_hotplug_event event);
762 void usbi_hotplug_process(struct libusb_context *ctx, struct list_head *hotplug_msgs);
764 int usbi_io_init(struct libusb_context *ctx);
765 void usbi_io_exit(struct libusb_context *ctx);
767 struct libusb_device *usbi_alloc_device(struct libusb_context *ctx,
768 unsigned long session_id);
769 struct libusb_device *usbi_get_device_by_session_id(struct libusb_context *ctx,
770 unsigned long session_id);
771 int usbi_sanitize_device(struct libusb_device *dev);
772 void usbi_handle_disconnect(struct libusb_device_handle *dev_handle);
774 int usbi_handle_transfer_completion(struct usbi_transfer *itransfer,
775 enum libusb_transfer_status status);
776 int usbi_handle_transfer_cancellation(struct usbi_transfer *itransfer);
777 void usbi_signal_transfer_completion(struct usbi_transfer *itransfer);
779 void usbi_connect_device(struct libusb_device *dev);
780 void usbi_disconnect_device(struct libusb_device *dev);
782 struct usbi_event_source {
783 struct usbi_event_source_data {
784 usbi_os_handle_t os_handle;
787 struct list_head list;
790 int usbi_add_event_source(struct libusb_context *ctx, usbi_os_handle_t os_handle,
792 void usbi_remove_event_source(struct libusb_context *ctx, usbi_os_handle_t os_handle);
801 /* OS event abstraction */
803 int usbi_create_event(usbi_event_t *event);
804 void usbi_destroy_event(usbi_event_t *event);
805 void usbi_signal_event(usbi_event_t *event);
806 void usbi_clear_event(usbi_event_t *event);
809 int usbi_create_timer(usbi_timer_t *timer);
810 void usbi_destroy_timer(usbi_timer_t *timer);
811 int usbi_arm_timer(usbi_timer_t *timer, const struct timespec *timeout);
812 int usbi_disarm_timer(usbi_timer_t *timer);
815 static inline int usbi_using_timer(struct libusb_context *ctx)
818 return usbi_timer_valid(&ctx->timer);
825 struct usbi_reported_events {
828 unsigned int event_triggered:1;
830 unsigned int timer_triggered:1;
833 unsigned int event_bits;
836 unsigned int event_data_count;
837 unsigned int num_ready;
840 int usbi_alloc_event_data(struct libusb_context *ctx);
841 int usbi_wait_for_events(struct libusb_context *ctx,
842 struct usbi_reported_events *reported_events, int timeout_ms);
844 /* accessor functions for structure private data */
846 static inline void *usbi_get_context_priv(struct libusb_context *ctx)
848 return (unsigned char *)ctx + PTR_ALIGN(sizeof(*ctx));
851 static inline void *usbi_get_device_priv(struct libusb_device *dev)
853 return (unsigned char *)dev + PTR_ALIGN(sizeof(*dev));
856 static inline void *usbi_get_device_handle_priv(struct libusb_device_handle *dev_handle)
858 return (unsigned char *)dev_handle + PTR_ALIGN(sizeof(*dev_handle));
861 static inline void *usbi_get_transfer_priv(struct usbi_transfer *itransfer)
863 return itransfer->priv;
866 /* device discovery */
868 /* we traverse usbfs without knowing how many devices we are going to find.
869 * so we create this discovered_devs model which is similar to a linked-list
870 * which grows when required. it can be freed once discovery has completed,
871 * eliminating the need for a list node in the libusb_device structure
873 struct discovered_devs {
876 struct libusb_device *devices[ZERO_SIZED_ARRAY];
879 struct discovered_devs *discovered_devs_append(
880 struct discovered_devs *discdevs, struct libusb_device *dev);
884 /* This is the interface that OS backends need to implement.
885 * All fields are mandatory, except ones explicitly noted as optional. */
886 struct usbi_os_backend {
887 /* A human-readable name for your backend, e.g. "Linux usbfs" */
890 /* Binary mask for backend specific capabilities */
893 /* Perform initialization of your backend. You might use this function
894 * to determine specific capabilities of the system, allocate required
895 * data structures for later, etc.
897 * This function is called when a libusb user initializes the library
898 * prior to use. Mutual exclusion with other init and exit calls is
899 * guaranteed when this function is called.
901 * Return 0 on success, or a LIBUSB_ERROR code on failure.
903 int (*init)(struct libusb_context *ctx);
905 /* Deinitialization. Optional. This function should destroy anything
906 * that was set up by init.
908 * This function is called when the user deinitializes the library.
909 * Mutual exclusion with other init and exit calls is guaranteed when
910 * this function is called.
912 void (*exit)(struct libusb_context *ctx);
914 /* Set a backend-specific option. Optional.
916 * This function is called when the user calls libusb_set_option() and
917 * the option is not handled by the core library.
919 * Return 0 on success, or a LIBUSB_ERROR code on failure.
921 int (*set_option)(struct libusb_context *ctx, enum libusb_option option,
924 /* Enumerate all the USB devices on the system, returning them in a list
925 * of discovered devices.
927 * Your implementation should enumerate all devices on the system,
928 * regardless of whether they have been seen before or not.
930 * When you have found a device, compute a session ID for it. The session
931 * ID should uniquely represent that particular device for that particular
932 * connection session since boot (i.e. if you disconnect and reconnect a
933 * device immediately after, it should be assigned a different session ID).
934 * If your OS cannot provide a unique session ID as described above,
935 * presenting a session ID of (bus_number << 8 | device_address) should
936 * be sufficient. Bus numbers and device addresses wrap and get reused,
937 * but that is an unlikely case.
939 * After computing a session ID for a device, call
940 * usbi_get_device_by_session_id(). This function checks if libusb already
941 * knows about the device, and if so, it provides you with a reference
942 * to a libusb_device structure for it.
944 * If usbi_get_device_by_session_id() returns NULL, it is time to allocate
945 * a new device structure for the device. Call usbi_alloc_device() to
946 * obtain a new libusb_device structure with reference count 1. Populate
947 * the bus_number and device_address attributes of the new device, and
948 * perform any other internal backend initialization you need to do. At
949 * this point, you should be ready to provide device descriptors and so
950 * on through the get_*_descriptor functions. Finally, call
951 * usbi_sanitize_device() to perform some final sanity checks on the
952 * device. Assuming all of the above succeeded, we can now continue.
953 * If any of the above failed, remember to unreference the device that
954 * was returned by usbi_alloc_device().
956 * At this stage we have a populated libusb_device structure (either one
957 * that was found earlier, or one that we have just allocated and
958 * populated). This can now be added to the discovered devices list
959 * using discovered_devs_append(). Note that discovered_devs_append()
960 * may reallocate the list, returning a new location for it, and also
961 * note that reallocation can fail. Your backend should handle these
962 * error conditions appropriately.
964 * This function should not generate any bus I/O and should not block.
965 * If I/O is required (e.g. reading the active configuration value), it is
966 * OK to ignore these suggestions :)
968 * This function is executed when the user wishes to retrieve a list
969 * of USB devices connected to the system.
971 * If the backend has hotplug support, this function is not used!
973 * Return 0 on success, or a LIBUSB_ERROR code on failure.
975 int (*get_device_list)(struct libusb_context *ctx,
976 struct discovered_devs **discdevs);
978 /* Apps which were written before hotplug support, may listen for
979 * hotplug events on their own and call libusb_get_device_list on
980 * device addition. In this case libusb_get_device_list will likely
981 * return a list without the new device in there, as the hotplug
982 * event thread will still be busy enumerating the device, which may
983 * take a while, or may not even have seen the event yet.
985 * To avoid this libusb_get_device_list will call this optional
986 * function for backends with hotplug support before copying
987 * ctx->usb_devs to the user. In this function the backend should
988 * ensure any pending hotplug events are fully processed before
991 * Optional, should be implemented by backends with hotplug support.
993 void (*hotplug_poll)(void);
995 /* Wrap a platform-specific device handle for I/O and other USB
996 * operations. The device handle is preallocated for you.
998 * Your backend should allocate any internal resources required for I/O
999 * and other operations so that those operations can happen (hopefully)
1000 * without hiccup. This is also a good place to inform libusb that it
1001 * should monitor certain file descriptors related to this device -
1002 * see the usbi_add_event_source() function.
1004 * Your backend should also initialize the device structure
1005 * (dev_handle->dev), which is NULL at the beginning of the call.
1007 * This function should not generate any bus I/O and should not block.
1009 * This function is called when the user attempts to wrap an existing
1010 * platform-specific device handle for a device.
1014 * - LIBUSB_ERROR_ACCESS if the user has insufficient permissions
1015 * - another LIBUSB_ERROR code on other failure
1017 * Do not worry about freeing the handle on failed open, the upper layers
1020 int (*wrap_sys_device)(struct libusb_context *ctx,
1021 struct libusb_device_handle *dev_handle, intptr_t sys_dev);
1023 /* Open a device for I/O and other USB operations. The device handle
1024 * is preallocated for you, you can retrieve the device in question
1025 * through handle->dev.
1027 * Your backend should allocate any internal resources required for I/O
1028 * and other operations so that those operations can happen (hopefully)
1029 * without hiccup. This is also a good place to inform libusb that it
1030 * should monitor certain file descriptors related to this device -
1031 * see the usbi_add_event_source() function.
1033 * This function should not generate any bus I/O and should not block.
1035 * This function is called when the user attempts to obtain a device
1036 * handle for a device.
1040 * - LIBUSB_ERROR_ACCESS if the user has insufficient permissions
1041 * - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since
1043 * - another LIBUSB_ERROR code on other failure
1045 * Do not worry about freeing the handle on failed open, the upper layers
1048 int (*open)(struct libusb_device_handle *dev_handle);
1050 /* Close a device such that the handle cannot be used again. Your backend
1051 * should destroy any resources that were allocated in the open path.
1052 * This may also be a good place to call usbi_remove_event_source() to
1053 * inform libusb of any event sources associated with this device that
1054 * should no longer be monitored.
1056 * This function is called when the user closes a device handle.
1058 void (*close)(struct libusb_device_handle *dev_handle);
1060 /* Get the ACTIVE configuration descriptor for a device.
1062 * The descriptor should be retrieved from memory, NOT via bus I/O to the
1063 * device. This means that you may have to cache it in a private structure
1064 * during get_device_list enumeration. You may also have to keep track
1065 * of which configuration is active when the user changes it.
1067 * This function is expected to write len bytes of data into buffer, which
1068 * is guaranteed to be big enough. If you can only do a partial write,
1069 * return an error code.
1071 * This function is expected to return the descriptor in bus-endian format
1076 * - LIBUSB_ERROR_NOT_FOUND if the device is in unconfigured state
1077 * - another LIBUSB_ERROR code on other failure
1079 int (*get_active_config_descriptor)(struct libusb_device *device,
1080 void *buffer, size_t len);
1082 /* Get a specific configuration descriptor for a device.
1084 * The descriptor should be retrieved from memory, NOT via bus I/O to the
1085 * device. This means that you may have to cache it in a private structure
1086 * during get_device_list enumeration.
1088 * The requested descriptor is expressed as a zero-based index (i.e. 0
1089 * indicates that we are requesting the first descriptor). The index does
1090 * not (necessarily) equal the bConfigurationValue of the configuration
1093 * This function is expected to write len bytes of data into buffer, which
1094 * is guaranteed to be big enough. If you can only do a partial write,
1095 * return an error code.
1097 * This function is expected to return the descriptor in bus-endian format
1100 * Return the length read on success or a LIBUSB_ERROR code on failure.
1102 int (*get_config_descriptor)(struct libusb_device *device,
1103 uint8_t config_index, void *buffer, size_t len);
1105 /* Like get_config_descriptor but then by bConfigurationValue instead
1108 * Optional, if not present the core will call get_config_descriptor
1109 * for all configs until it finds the desired bConfigurationValue.
1111 * Returns a pointer to the raw-descriptor in *buffer, this memory
1112 * is valid as long as device is valid.
1114 * Returns the length of the returned raw-descriptor on success,
1115 * or a LIBUSB_ERROR code on failure.
1117 int (*get_config_descriptor_by_value)(struct libusb_device *device,
1118 uint8_t bConfigurationValue, void **buffer);
1120 /* Get the bConfigurationValue for the active configuration for a device.
1121 * Optional. This should only be implemented if you can retrieve it from
1122 * cache (don't generate I/O).
1124 * If you cannot retrieve this from cache, either do not implement this
1125 * function, or return LIBUSB_ERROR_NOT_SUPPORTED. This will cause
1126 * libusb to retrieve the information through a standard control transfer.
1128 * This function must be non-blocking.
1131 * - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it
1133 * - LIBUSB_ERROR_NOT_SUPPORTED if the value cannot be retrieved without
1135 * - another LIBUSB_ERROR code on other failure.
1137 int (*get_configuration)(struct libusb_device_handle *dev_handle, uint8_t *config);
1139 /* Set the active configuration for a device.
1141 * A configuration value of -1 should put the device in unconfigured state.
1143 * This function can block.
1147 * - LIBUSB_ERROR_NOT_FOUND if the configuration does not exist
1148 * - LIBUSB_ERROR_BUSY if interfaces are currently claimed (and hence
1149 * configuration cannot be changed)
1150 * - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it
1152 * - another LIBUSB_ERROR code on other failure.
1154 int (*set_configuration)(struct libusb_device_handle *dev_handle, int config);
1156 /* Claim an interface. When claimed, the application can then perform
1157 * I/O to an interface's endpoints.
1159 * This function should not generate any bus I/O and should not block.
1160 * Interface claiming is a logical operation that simply ensures that
1161 * no other drivers/applications are using the interface, and after
1162 * claiming, no other drivers/applications can use the interface because
1167 * - LIBUSB_ERROR_NOT_FOUND if the interface does not exist
1168 * - LIBUSB_ERROR_BUSY if the interface is in use by another driver/app
1169 * - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it
1171 * - another LIBUSB_ERROR code on other failure
1173 int (*claim_interface)(struct libusb_device_handle *dev_handle, uint8_t interface_number);
1175 /* Release a previously claimed interface.
1177 * This function should also generate a SET_INTERFACE control request,
1178 * resetting the alternate setting of that interface to 0. It's OK for
1179 * this function to block as a result.
1181 * You will only ever be asked to release an interface which was
1182 * successfully claimed earlier.
1186 * - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it
1188 * - another LIBUSB_ERROR code on other failure
1190 int (*release_interface)(struct libusb_device_handle *dev_handle, uint8_t interface_number);
1192 /* Set the alternate setting for an interface.
1194 * You will only ever be asked to set the alternate setting for an
1195 * interface which was successfully claimed earlier.
1197 * It's OK for this function to block.
1201 * - LIBUSB_ERROR_NOT_FOUND if the alternate setting does not exist
1202 * - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it
1204 * - another LIBUSB_ERROR code on other failure
1206 int (*set_interface_altsetting)(struct libusb_device_handle *dev_handle,
1207 uint8_t interface_number, uint8_t altsetting);
1209 /* Clear a halt/stall condition on an endpoint.
1211 * It's OK for this function to block.
1215 * - LIBUSB_ERROR_NOT_FOUND if the endpoint does not exist
1216 * - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it
1218 * - another LIBUSB_ERROR code on other failure
1220 int (*clear_halt)(struct libusb_device_handle *dev_handle,
1221 unsigned char endpoint);
1223 /* Perform a USB port reset to reinitialize a device. Optional.
1225 * If possible, the device handle should still be usable after the reset
1226 * completes, assuming that the device descriptors did not change during
1227 * reset and all previous interface state can be restored.
1229 * If something changes, or you cannot easily locate/verify the reset
1230 * device, return LIBUSB_ERROR_NOT_FOUND. This prompts the application
1231 * to close the old handle and re-enumerate the device.
1235 * - LIBUSB_ERROR_NOT_FOUND if re-enumeration is required, or if the device
1236 * has been disconnected since it was opened
1237 * - another LIBUSB_ERROR code on other failure
1239 int (*reset_device)(struct libusb_device_handle *dev_handle);
1241 /* Alloc num_streams usb3 bulk streams on the passed in endpoints */
1242 int (*alloc_streams)(struct libusb_device_handle *dev_handle,
1243 uint32_t num_streams, unsigned char *endpoints, int num_endpoints);
1245 /* Free usb3 bulk streams allocated with alloc_streams */
1246 int (*free_streams)(struct libusb_device_handle *dev_handle,
1247 unsigned char *endpoints, int num_endpoints);
1249 /* Allocate persistent DMA memory for the given device, suitable for
1250 * zerocopy. May return NULL on failure. Optional to implement.
1252 void *(*dev_mem_alloc)(struct libusb_device_handle *handle, size_t len);
1254 /* Free memory allocated by dev_mem_alloc. */
1255 int (*dev_mem_free)(struct libusb_device_handle *handle, void *buffer,
1258 /* Determine if a kernel driver is active on an interface. Optional.
1260 * The presence of a kernel driver on an interface indicates that any
1261 * calls to claim_interface would fail with the LIBUSB_ERROR_BUSY code.
1264 * - 0 if no driver is active
1265 * - 1 if a driver is active
1266 * - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it
1268 * - another LIBUSB_ERROR code on other failure
1270 int (*kernel_driver_active)(struct libusb_device_handle *dev_handle,
1271 uint8_t interface_number);
1273 /* Detach a kernel driver from an interface. Optional.
1275 * After detaching a kernel driver, the interface should be available
1280 * - LIBUSB_ERROR_NOT_FOUND if no kernel driver was active
1281 * - LIBUSB_ERROR_INVALID_PARAM if the interface does not exist
1282 * - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it
1284 * - another LIBUSB_ERROR code on other failure
1286 int (*detach_kernel_driver)(struct libusb_device_handle *dev_handle,
1287 uint8_t interface_number);
1289 /* Attach a kernel driver to an interface. Optional.
1291 * Reattach a kernel driver to the device.
1295 * - LIBUSB_ERROR_NOT_FOUND if no kernel driver was active
1296 * - LIBUSB_ERROR_INVALID_PARAM if the interface does not exist
1297 * - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it
1299 * - LIBUSB_ERROR_BUSY if a program or driver has claimed the interface,
1300 * preventing reattachment
1301 * - another LIBUSB_ERROR code on other failure
1303 int (*attach_kernel_driver)(struct libusb_device_handle *dev_handle,
1304 uint8_t interface_number);
1306 /* Destroy a device. Optional.
1308 * This function is called when the last reference to a device is
1309 * destroyed. It should free any resources allocated in the get_device_list
1312 void (*destroy_device)(struct libusb_device *dev);
1314 /* Submit a transfer. Your implementation should take the transfer,
1315 * morph it into whatever form your platform requires, and submit it
1318 * This function must not block.
1320 * This function gets called with the flying_transfers_lock locked!
1324 * - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
1325 * - another LIBUSB_ERROR code on other failure
1327 int (*submit_transfer)(struct usbi_transfer *itransfer);
1329 /* Cancel a previously submitted transfer.
1331 * This function must not block. The transfer cancellation must complete
1332 * later, resulting in a call to usbi_handle_transfer_cancellation()
1333 * from the context of handle_events.
1335 int (*cancel_transfer)(struct usbi_transfer *itransfer);
1337 /* Clear a transfer as if it has completed or cancelled, but do not
1338 * report any completion/cancellation to the library. You should free
1339 * all private data from the transfer as if you were just about to report
1340 * completion or cancellation.
1342 * This function might seem a bit out of place. It is used when libusb
1343 * detects a disconnected device - it calls this function for all pending
1344 * transfers before reporting completion (with the disconnect code) to
1345 * the user. Maybe we can improve upon this internal interface in future.
1347 void (*clear_transfer_priv)(struct usbi_transfer *itransfer);
1349 /* Handle any pending events on event sources. Optional.
1351 * Provide this function when event sources directly indicate device
1352 * or transfer activity. If your backend does not have such event sources,
1353 * implement the handle_transfer_completion function below.
1355 * This involves monitoring any active transfers and processing their
1356 * completion or cancellation.
1358 * The function is passed a pointer that represents platform-specific
1359 * data for monitoring event sources (size count). This data is to be
1360 * (re)allocated as necessary when event sources are modified.
1361 * The num_ready parameter indicates the number of event sources that
1362 * have reported events. This should be enough information for you to
1363 * determine which actions need to be taken on the currently active
1366 * For any cancelled transfers, call usbi_handle_transfer_cancellation().
1367 * For completed transfers, call usbi_handle_transfer_completion().
1368 * For control/bulk/interrupt transfers, populate the "transferred"
1369 * element of the appropriate usbi_transfer structure before calling the
1370 * above functions. For isochronous transfers, populate the status and
1371 * transferred fields of the iso packet descriptors of the transfer.
1373 * This function should also be able to detect disconnection of the
1374 * device, reporting that situation with usbi_handle_disconnect().
1376 * When processing an event related to a transfer, you probably want to
1377 * take usbi_transfer.lock to prevent races. See the documentation for
1378 * the usbi_transfer structure.
1380 * Return 0 on success, or a LIBUSB_ERROR code on failure.
1382 int (*handle_events)(struct libusb_context *ctx,
1383 void *event_data, unsigned int count, unsigned int num_ready);
1385 /* Handle transfer completion. Optional.
1387 * Provide this function when there are no event sources available that
1388 * directly indicate device or transfer activity. If your backend does
1389 * have such event sources, implement the handle_events function above.
1391 * Your backend must tell the library when a transfer has completed by
1392 * calling usbi_signal_transfer_completion(). You should store any private
1393 * information about the transfer and its completion status in the transfer's
1394 * private backend data.
1396 * During event handling, this function will be called on each transfer for
1397 * which usbi_signal_transfer_completion() was called.
1399 * For any cancelled transfers, call usbi_handle_transfer_cancellation().
1400 * For completed transfers, call usbi_handle_transfer_completion().
1401 * For control/bulk/interrupt transfers, populate the "transferred"
1402 * element of the appropriate usbi_transfer structure before calling the
1403 * above functions. For isochronous transfers, populate the status and
1404 * transferred fields of the iso packet descriptors of the transfer.
1406 * Return 0 on success, or a LIBUSB_ERROR code on failure.
1408 int (*handle_transfer_completion)(struct usbi_transfer *itransfer);
1410 /* Number of bytes to reserve for per-context private backend data.
1411 * This private data area is accessible by calling
1412 * usbi_get_context_priv() on the libusb_context instance.
1414 size_t context_priv_size;
1416 /* Number of bytes to reserve for per-device private backend data.
1417 * This private data area is accessible by calling
1418 * usbi_get_device_priv() on the libusb_device instance.
1420 size_t device_priv_size;
1422 /* Number of bytes to reserve for per-handle private backend data.
1423 * This private data area is accessible by calling
1424 * usbi_get_device_handle_priv() on the libusb_device_handle instance.
1426 size_t device_handle_priv_size;
1428 /* Number of bytes to reserve for per-transfer private backend data.
1429 * This private data area is accessible by calling
1430 * usbi_get_transfer_priv() on the usbi_transfer instance.
1432 size_t transfer_priv_size;
1435 extern const struct usbi_os_backend usbi_backend;
1437 #define for_each_context(c) \
1438 for_each_helper(c, &active_contexts_list, struct libusb_context)
1440 #define for_each_device(ctx, d) \
1441 for_each_helper(d, &(ctx)->usb_devs, struct libusb_device)
1443 #define for_each_device_safe(ctx, d, n) \
1444 for_each_safe_helper(d, n, &(ctx)->usb_devs, struct libusb_device)
1446 #define for_each_open_device(ctx, h) \
1447 for_each_helper(h, &(ctx)->open_devs, struct libusb_device_handle)
1449 #define __for_each_transfer(list, t) \
1450 for_each_helper(t, (list), struct usbi_transfer)
1452 #define for_each_transfer(ctx, t) \
1453 __for_each_transfer(&(ctx)->flying_transfers, t)
1455 #define __for_each_transfer_safe(list, t, n) \
1456 for_each_safe_helper(t, n, (list), struct usbi_transfer)
1458 #define for_each_transfer_safe(ctx, t, n) \
1459 __for_each_transfer_safe(&(ctx)->flying_transfers, t, n)
1461 #define __for_each_completed_transfer_safe(list, t, n) \
1462 list_for_each_entry_safe(t, n, (list), completed_list, struct usbi_transfer)
1464 #define for_each_event_source(ctx, e) \
1465 for_each_helper(e, &(ctx)->event_sources, struct usbi_event_source)
1467 #define for_each_removed_event_source(ctx, e) \
1468 for_each_helper(e, &(ctx)->removed_event_sources, struct usbi_event_source)
1470 #define for_each_removed_event_source_safe(ctx, e, n) \
1471 for_each_safe_helper(e, n, &(ctx)->removed_event_sources, struct usbi_event_source)
1473 #define for_each_hotplug_cb(ctx, c) \
1474 for_each_helper(c, &(ctx)->hotplug_cbs, struct usbi_hotplug_callback)
1476 #define for_each_hotplug_cb_safe(ctx, c, n) \
1477 for_each_safe_helper(c, n, &(ctx)->hotplug_cbs, struct usbi_hotplug_callback)