libusb-1.0
libusbi.h
1 /*
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 Google LLC. All rights reserved.
7  * Copyright © 2020 Chris Dickens <christopher.a.dickens@gmail.com>
8  *
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.
13  *
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.
18  *
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
22  */
23 
24 #ifndef LIBUSBI_H
25 #define LIBUSBI_H
26 
27 #include <config.h>
28 
29 #include <assert.h>
30 #include <stdarg.h>
31 #include <stddef.h>
32 #include <stdlib.h>
33 #ifdef HAVE_SYS_TIME_H
34 #include <sys/time.h>
35 #endif
36 
37 #include "libusb.h"
38 
39 /* Not all C standard library headers define static_assert in assert.h
40  * Additionally, Visual Studio treats static_assert as a keyword.
41  */
42 #if !defined(__cplusplus) && !defined(static_assert) && !defined(_MSC_VER)
43 #define static_assert(cond, msg) _Static_assert(cond, msg)
44 #endif
45 
46 #define container_of(ptr, type, member) \
47  ((type *)((uintptr_t)(ptr) - (uintptr_t)offsetof(type, member)))
48 
49 #ifndef ARRAYSIZE
50 #define ARRAYSIZE(array) (sizeof(array) / sizeof(array[0]))
51 #endif
52 
53 #ifndef CLAMP
54 #define CLAMP(val, min, max) \
55  ((val) < (min) ? (min) : ((val) > (max) ? (max) : (val)))
56 #endif
57 
58 #ifndef MIN
59 #define MIN(a, b) ((a) < (b) ? (a) : (b))
60 #endif
61 
62 #ifndef MAX
63 #define MAX(a, b) ((a) > (b) ? (a) : (b))
64 #endif
65 
66 /* The following is used to silence warnings for unused variables */
67 #if defined(UNREFERENCED_PARAMETER)
68 #define UNUSED(var) UNREFERENCED_PARAMETER(var)
69 #else
70 #define UNUSED(var) do { (void)(var); } while(0)
71 #endif
72 
73 /* Macro to align a value up to the next multiple of the size of a pointer */
74 #define PTR_ALIGN(v) \
75  (((v) + (sizeof(void *) - 1)) & ~(sizeof(void *) - 1))
76 
77 /* Internal abstraction for event handling */
78 #if defined(EVENTS_POSIX)
79 #include "os/events_posix.h"
80 #elif defined(EVENTS_WINDOWS)
81 #include "os/events_windows.h"
82 #endif
83 
84 /* Internal abstraction for thread synchronization */
85 #if defined(THREADS_POSIX)
86 #include "os/threads_posix.h"
87 #elif defined(THREADS_WINDOWS)
88 #include "os/threads_windows.h"
89 #endif
90 
91 /* Inside the libusb code, mark all public functions as follows:
92  * return_type API_EXPORTED function_name(params) { ... }
93  * But if the function returns a pointer, mark it as follows:
94  * DEFAULT_VISIBILITY return_type * LIBUSB_CALL function_name(params) { ... }
95  * In the libusb public header, mark all declarations as:
96  * return_type LIBUSB_CALL function_name(params);
97  */
98 #define API_EXPORTED LIBUSB_CALL DEFAULT_VISIBILITY
99 
100 /* Macro to decorate printf-like functions, in order to get
101  * compiler warnings about format string mistakes.
102  */
103 #ifndef _MSC_VER
104 #define USBI_PRINTFLIKE(formatarg, firstvararg) \
105  __attribute__ ((__format__ (__printf__, formatarg, firstvararg)))
106 #else
107 #define USBI_PRINTFLIKE(formatarg, firstvararg)
108 #endif
109 
110 #ifdef __cplusplus
111 extern "C" {
112 #endif
113 
114 #define USB_MAXENDPOINTS 32
115 #define USB_MAXINTERFACES 32
116 #define USB_MAXCONFIG 8
117 
118 /* Backend specific capabilities */
119 #define USBI_CAP_HAS_HID_ACCESS 0x00010000
120 #define USBI_CAP_SUPPORTS_DETACH_KERNEL_DRIVER 0x00020000
121 
122 /* Maximum number of bytes in a log line */
123 #define USBI_MAX_LOG_LEN 1024
124 /* Terminator for log lines */
125 #define USBI_LOG_LINE_END "\n"
126 
127 struct list_head {
128  struct list_head *prev, *next;
129 };
130 
131 /* Get an entry from the list
132  * ptr - the address of this list_head element in "type"
133  * type - the data type that contains "member"
134  * member - the list_head element in "type"
135  */
136 #define list_entry(ptr, type, member) \
137  container_of(ptr, type, member)
138 
139 #define list_first_entry(ptr, type, member) \
140  list_entry((ptr)->next, type, member)
141 
142 #define list_next_entry(ptr, type, member) \
143  list_entry((ptr)->member.next, type, member)
144 
145 /* Get each entry from a list
146  * pos - A structure pointer has a "member" element
147  * head - list head
148  * member - the list_head element in "pos"
149  * type - the type of the first parameter
150  */
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))
155 
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))
161 
162 /* Helper macros to iterate over a list. The structure pointed
163  * to by "pos" must have a list_head member named "list".
164  */
165 #define for_each_helper(pos, head, type) \
166  list_for_each_entry(pos, head, list, type)
167 
168 #define for_each_safe_helper(pos, n, head, type) \
169  list_for_each_entry_safe(pos, n, head, list, type)
170 
171 #define list_empty(entry) ((entry)->next == (entry))
172 
173 static inline void list_init(struct list_head *entry)
174 {
175  entry->prev = entry->next = entry;
176 }
177 
178 static inline void list_add(struct list_head *entry, struct list_head *head)
179 {
180  entry->next = head->next;
181  entry->prev = head;
182 
183  head->next->prev = entry;
184  head->next = entry;
185 }
186 
187 static inline void list_add_tail(struct list_head *entry,
188  struct list_head *head)
189 {
190  entry->next = head;
191  entry->prev = head->prev;
192 
193  head->prev->next = entry;
194  head->prev = entry;
195 }
196 
197 static inline void list_del(struct list_head *entry)
198 {
199  entry->next->prev = entry->prev;
200  entry->prev->next = entry->next;
201  entry->next = entry->prev = NULL;
202 }
203 
204 static inline void list_cut(struct list_head *list, struct list_head *head)
205 {
206  if (list_empty(head))
207  return;
208 
209  list->next = head->next;
210  list->next->prev = list;
211  list->prev = head->prev;
212  list->prev->next = list;
213 
214  list_init(head);
215 }
216 
217 static inline void *usbi_reallocf(void *ptr, size_t size)
218 {
219  void *ret = realloc(ptr, size);
220 
221  if (!ret)
222  free(ptr);
223  return ret;
224 }
225 
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) \
233  do { \
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; \
239  } \
240  } while (0)
241 
242 #if defined(_WIN32)
243 #define TIMEVAL_TV_SEC_TYPE long
244 #else
245 #define TIMEVAL_TV_SEC_TYPE time_t
246 #endif
247 
248 /* Some platforms don't have this define */
249 #ifndef TIMESPEC_TO_TIMEVAL
250 #define TIMESPEC_TO_TIMEVAL(tv, ts) \
251  do { \
252  (tv)->tv_sec = (TIMEVAL_TV_SEC_TYPE) (ts)->tv_sec; \
253  (tv)->tv_usec = (ts)->tv_nsec / 1000L; \
254  } while (0)
255 #endif
256 
257 #ifdef ENABLE_LOGGING
258 
259 #if defined(_MSC_VER) && (_MSC_VER < 1900)
260 #include <stdio.h>
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
266 #endif /* defined(_MSC_VER) && (_MSC_VER < 1900) */
267 
268 void usbi_log(struct libusb_context *ctx, enum libusb_log_level level,
269  const char *function, const char *format, ...) USBI_PRINTFLIKE(4, 5);
270 
271 void usbi_log_v(struct libusb_context *ctx, enum libusb_log_level level,
272  const char *function, const char *format, va_list args) USBI_PRINTFLIKE(4, 0);
273 
274 #define _usbi_log(ctx, level, ...) usbi_log(ctx, level, __func__, __VA_ARGS__)
275 
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__)
280 
281 #else /* ENABLE_LOGGING */
282 
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)
287 
288 #endif /* ENABLE_LOGGING */
289 
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)))
295 
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))
300 
302 #if defined(ENABLE_LOGGING) && !defined(ENABLE_DEBUG_LOGGING)
303  enum libusb_log_level debug;
304  int debug_fixed;
305  libusb_log_cb log_handler;
306 #endif
307 
308  /* used for signalling occurrence of an internal event. */
309  usbi_event_t event;
310 
311 #ifdef HAVE_OS_TIMER
312  /* used for timeout handling, if supported by OS.
313  * this timer is maintained to trigger on the next pending timeout */
314  usbi_timer_t timer;
315 #endif
316 
317  struct list_head usb_devs;
318  usbi_mutex_t usb_devs_lock;
319 
320  /* A list of open handles. Backends are free to traverse this if required.
321  */
322  struct list_head open_devs;
323  usbi_mutex_t open_devs_lock;
324 
325  /* A list of registered hotplug callbacks */
326  struct list_head hotplug_cbs;
327  libusb_hotplug_callback_handle next_hotplug_cb_handle;
328  usbi_mutex_t hotplug_cbs_lock;
329 
330  /* this is a list of in-flight transfer handles, sorted by timeout
331  * expiration. URBs to timeout the soonest are placed at the beginning of
332  * the list, URBs that will time out later are placed after, and urbs with
333  * infinite timeout are always placed at the very end. */
334  struct list_head flying_transfers;
335  /* Note paths taking both this and usbi_transfer->lock must always
336  * take this lock first */
337  usbi_mutex_t flying_transfers_lock;
338 
339 #if !defined(_WIN32) && !defined(__CYGWIN__)
340  /* user callbacks for pollfd changes */
341  libusb_pollfd_added_cb fd_added_cb;
342  libusb_pollfd_removed_cb fd_removed_cb;
343  void *fd_cb_user_data;
344 #endif
345 
346  /* ensures that only one thread is handling events at any one time */
347  usbi_mutex_t events_lock;
348 
349  /* used to see if there is an active thread doing event handling */
350  int event_handler_active;
351 
352  /* A thread-local storage key to track which thread is performing event
353  * handling */
354  usbi_tls_key_t event_handling_key;
355 
356  /* used to wait for event completion in threads other than the one that is
357  * event handling */
358  usbi_mutex_t event_waiters_lock;
359  usbi_cond_t event_waiters_cond;
360 
361  /* A lock to protect internal context event data. */
362  usbi_mutex_t event_data_lock;
363 
364  /* A bitmask of flags that are set to indicate specific events that need to
365  * be handled. Protected by event_data_lock. */
366  unsigned int event_flags;
367 
368  /* A counter that is set when we want to interrupt and prevent event handling,
369  * in order to safely close a device. Protected by event_data_lock. */
370  unsigned int device_close;
371 
372  /* A list of currently active event sources. Protected by event_data_lock. */
373  struct list_head event_sources;
374 
375  /* A list of event sources that have been removed since the last time
376  * event sources were waited on. Protected by event_data_lock. */
377  struct list_head removed_event_sources;
378 
379  /* A pointer and count to platform-specific data used for monitoring event
380  * sources. Only accessed during event handling. */
381  void *event_data;
382  unsigned int event_data_cnt;
383 
384  /* A list of pending hotplug messages. Protected by event_data_lock. */
385  struct list_head hotplug_msgs;
386 
387  /* A list of pending completed transfers. Protected by event_data_lock. */
388  struct list_head completed_transfers;
389 
390  struct list_head list;
391 };
392 
393 extern struct libusb_context *usbi_default_context;
394 
395 extern struct list_head active_contexts_list;
396 extern usbi_mutex_static_t active_contexts_lock;
397 
398 static inline struct libusb_context *usbi_get_context(struct libusb_context *ctx)
399 {
400  return ctx ? ctx : usbi_default_context;
401 }
402 
403 enum usbi_event_flags {
404  /* The list of event sources has been modified */
405  USBI_EVENT_EVENT_SOURCES_MODIFIED = 1U << 0,
406 
407  /* The user has interrupted the event handler */
408  USBI_EVENT_USER_INTERRUPT = 1U << 1,
409 
410  /* A hotplug callback deregistration is pending */
411  USBI_EVENT_HOTPLUG_CB_DEREGISTERED = 1U << 2,
412 
413  /* One or more hotplug messages are pending */
414  USBI_EVENT_HOTPLUG_MSG_PENDING = 1U << 3,
415 
416  /* One or more completed transfers are pending */
417  USBI_EVENT_TRANSFER_COMPLETED = 1U << 4,
418 
419  /* A device is in the process of being closed */
420  USBI_EVENT_DEVICE_CLOSE = 1U << 5,
421 };
422 
423 /* Macros for managing event handling state */
424 static inline int usbi_handling_events(struct libusb_context *ctx)
425 {
426  return usbi_tls_key_get(ctx->event_handling_key) != NULL;
427 }
428 
429 static inline void usbi_start_event_handling(struct libusb_context *ctx)
430 {
431  usbi_tls_key_set(ctx->event_handling_key, ctx);
432 }
433 
434 static inline void usbi_end_event_handling(struct libusb_context *ctx)
435 {
436  usbi_tls_key_set(ctx->event_handling_key, NULL);
437 }
438 
440  /* lock protects refcnt, everything else is finalized at initialization
441  * time */
442  usbi_mutex_t lock;
443  int refcnt;
444 
445  struct libusb_context *ctx;
446  struct libusb_device *parent_dev;
447 
448  uint8_t bus_number;
449  uint8_t port_number;
450  uint8_t device_address;
451  enum libusb_speed speed;
452 
453  struct list_head list;
454  unsigned long session_data;
455 
456  struct libusb_device_descriptor device_descriptor;
457  int attached;
458 };
459 
461  /* lock protects claimed_interfaces */
462  usbi_mutex_t lock;
463  unsigned long claimed_interfaces;
464 
465  struct list_head list;
466  struct libusb_device *dev;
467  int auto_detach_kernel_driver;
468 };
469 
470 /* Function called by backend during device initialization to convert
471  * multi-byte fields in the device descriptor to host-endian format.
472  */
473 static inline void usbi_localize_device_descriptor(struct libusb_device_descriptor *desc)
474 {
475  desc->bcdUSB = libusb_le16_to_cpu(desc->bcdUSB);
476  desc->idVendor = libusb_le16_to_cpu(desc->idVendor);
477  desc->idProduct = libusb_le16_to_cpu(desc->idProduct);
478  desc->bcdDevice = libusb_le16_to_cpu(desc->bcdDevice);
479 }
480 
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
485 #else
486 /* If the platform doesn't provide the clock_gettime() function, the backend
487  * must provide its own implementation. Two clocks must be supported by the
488  * backend: USBI_CLOCK_REALTIME, and USBI_CLOCK_MONOTONIC.
489  *
490  * Description of clocks:
491  * USBI_CLOCK_REALTIME: clock returns time since system epoch.
492  * USBI_CLOCK_MONOTONIC: clock returns time since unspecified start time
493  * (usually boot).
494  */
495 #define USBI_CLOCK_REALTIME 0
496 #define USBI_CLOCK_MONOTONIC 1
497 int usbi_clock_gettime(int clk_id, struct timespec *tp);
498 #endif
499 
500 /* in-memory transfer layout:
501  *
502  * 1. os private data
503  * 2. struct usbi_transfer
504  * 3. struct libusb_transfer (which includes iso packets) [variable size]
505  *
506  * from a libusb_transfer, you can get the usbi_transfer by rewinding the
507  * appropriate number of bytes.
508  */
509 
511  int num_iso_packets;
512  struct list_head list;
513  struct list_head completed_list;
514  struct timespec timeout;
515  int transferred;
516  uint32_t stream_id;
517  uint32_t state_flags; /* Protected by usbi_transfer->lock */
518  uint32_t timeout_flags; /* Protected by the flying_stransfers_lock */
519 
520  /* this lock is held during libusb_submit_transfer() and
521  * libusb_cancel_transfer() (allowing the OS backend to prevent duplicate
522  * cancellation, submission-during-cancellation, etc). the OS backend
523  * should also take this lock in the handle_events path, to prevent the user
524  * cancelling the transfer from another thread while you are processing
525  * its completion (presumably there would be races within your OS backend
526  * if this were possible).
527  * Note paths taking both this and the flying_transfers_lock must
528  * always take the flying_transfers_lock first */
529  usbi_mutex_t lock;
530 
531  void *priv;
532 };
533 
534 enum usbi_transfer_state_flags {
535  /* Transfer successfully submitted by backend */
536  USBI_TRANSFER_IN_FLIGHT = 1U << 0,
537 
538  /* Cancellation was requested via libusb_cancel_transfer() */
539  USBI_TRANSFER_CANCELLING = 1U << 1,
540 
541  /* Operation on the transfer failed because the device disappeared */
542  USBI_TRANSFER_DEVICE_DISAPPEARED = 1U << 2,
543 };
544 
545 enum usbi_transfer_timeout_flags {
546  /* Set by backend submit_transfer() if the OS handles timeout */
547  USBI_TRANSFER_OS_HANDLES_TIMEOUT = 1U << 0,
548 
549  /* The transfer timeout has been handled */
550  USBI_TRANSFER_TIMEOUT_HANDLED = 1U << 1,
551 
552  /* The transfer timeout was successfully processed */
553  USBI_TRANSFER_TIMED_OUT = 1U << 2,
554 };
555 
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))))
564 
565 #ifdef _MSC_VER
566 #pragma pack(push, 1)
567 #endif
568 
569 /* All standard descriptors have these 2 fields in common */
571  uint8_t bLength;
572  uint8_t bDescriptorType;
573 } LIBUSB_PACKED;
574 
576  uint8_t bLength;
577  uint8_t bDescriptorType;
578  uint16_t bcdUSB;
579  uint8_t bDeviceClass;
580  uint8_t bDeviceSubClass;
581  uint8_t bDeviceProtocol;
582  uint8_t bMaxPacketSize0;
583  uint16_t idVendor;
584  uint16_t idProduct;
585  uint16_t bcdDevice;
586  uint8_t iManufacturer;
587  uint8_t iProduct;
588  uint8_t iSerialNumber;
589  uint8_t bNumConfigurations;
590 } LIBUSB_PACKED;
591 
593  uint8_t bLength;
594  uint8_t bDescriptorType;
595  uint16_t wTotalLength;
596  uint8_t bNumInterfaces;
597  uint8_t bConfigurationValue;
598  uint8_t iConfiguration;
599  uint8_t bmAttributes;
600  uint8_t bMaxPower;
601 } LIBUSB_PACKED;
602 
604  uint8_t bLength;
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;
612  uint8_t iInterface;
613 } LIBUSB_PACKED;
614 
616  uint8_t bLength;
617  uint8_t bDescriptorType;
618  uint16_t wData[];
619 } LIBUSB_PACKED;
620 
622  uint8_t bLength;
623  uint8_t bDescriptorType;
624  uint16_t wTotalLength;
625  uint8_t bNumDeviceCaps;
626 } LIBUSB_PACKED;
627 
628 #ifdef _MSC_VER
629 #pragma pack(pop)
630 #endif
631 
632 /* shared data and functions */
633 
634 int usbi_io_init(struct libusb_context *ctx);
635 void usbi_io_exit(struct libusb_context *ctx);
636 
637 struct libusb_device *usbi_alloc_device(struct libusb_context *ctx,
638  unsigned long session_id);
639 struct libusb_device *usbi_get_device_by_session_id(struct libusb_context *ctx,
640  unsigned long session_id);
641 int usbi_sanitize_device(struct libusb_device *dev);
642 void usbi_handle_disconnect(struct libusb_device_handle *dev_handle);
643 
644 int usbi_handle_transfer_completion(struct usbi_transfer *itransfer,
645  enum libusb_transfer_status status);
646 int usbi_handle_transfer_cancellation(struct usbi_transfer *itransfer);
647 void usbi_signal_transfer_completion(struct usbi_transfer *itransfer);
648 
649 void usbi_connect_device(struct libusb_device *dev);
650 void usbi_disconnect_device(struct libusb_device *dev);
651 
654  usbi_os_handle_t os_handle;
655  short poll_events;
656  } data;
657  struct list_head list;
658 };
659 
660 int usbi_add_event_source(struct libusb_context *ctx, usbi_os_handle_t os_handle,
661  short poll_events);
662 void usbi_remove_event_source(struct libusb_context *ctx, usbi_os_handle_t os_handle);
663 
664 /* OS event abstraction */
665 
666 int usbi_create_event(usbi_event_t *event);
667 void usbi_destroy_event(usbi_event_t *event);
668 void usbi_signal_event(usbi_event_t *event);
669 void usbi_clear_event(usbi_event_t *event);
670 
671 #ifdef HAVE_OS_TIMER
672 int usbi_create_timer(usbi_timer_t *timer);
673 void usbi_destroy_timer(usbi_timer_t *timer);
674 int usbi_arm_timer(usbi_timer_t *timer, const struct timespec *timeout);
675 int usbi_disarm_timer(usbi_timer_t *timer);
676 #endif
677 
678 static inline int usbi_using_timer(struct libusb_context *ctx)
679 {
680 #ifdef HAVE_OS_TIMER
681  return usbi_timer_valid(&ctx->timer);
682 #else
683  UNUSED(ctx);
684  return 0;
685 #endif
686 }
687 
689  union {
690  struct {
691  unsigned int event_triggered:1;
692 #ifdef HAVE_OS_TIMER
693  unsigned int timer_triggered:1;
694 #endif
695  };
696  unsigned int event_bits;
697  };
698  void *event_data;
699  unsigned int event_data_count;
700  unsigned int num_ready;
701 };
702 
703 int usbi_alloc_event_data(struct libusb_context *ctx);
704 int usbi_wait_for_events(struct libusb_context *ctx,
705  struct usbi_reported_events *reported_events, int timeout_ms);
706 
707 /* accessor functions for structure private data */
708 
709 static inline void *usbi_get_context_priv(struct libusb_context *ctx)
710 {
711  return (unsigned char *)ctx + PTR_ALIGN(sizeof(*ctx));
712 }
713 
714 static inline void *usbi_get_device_priv(struct libusb_device *dev)
715 {
716  return (unsigned char *)dev + PTR_ALIGN(sizeof(*dev));
717 }
718 
719 static inline void *usbi_get_device_handle_priv(struct libusb_device_handle *dev_handle)
720 {
721  return (unsigned char *)dev_handle + PTR_ALIGN(sizeof(*dev_handle));
722 }
723 
724 static inline void *usbi_get_transfer_priv(struct usbi_transfer *itransfer)
725 {
726  return itransfer->priv;
727 }
728 
729 /* device discovery */
730 
731 /* we traverse usbfs without knowing how many devices we are going to find.
732  * so we create this discovered_devs model which is similar to a linked-list
733  * which grows when required. it can be freed once discovery has completed,
734  * eliminating the need for a list node in the libusb_device structure
735  * itself. */
737  size_t len;
738  size_t capacity;
739  struct libusb_device *devices[ZERO_SIZED_ARRAY];
740 };
741 
742 struct discovered_devs *discovered_devs_append(
743  struct discovered_devs *discdevs, struct libusb_device *dev);
744 
745 /* OS abstraction */
746 
747 /* This is the interface that OS backends need to implement.
748  * All fields are mandatory, except ones explicitly noted as optional. */
750  /* A human-readable name for your backend, e.g. "Linux usbfs" */
751  const char *name;
752 
753  /* Binary mask for backend specific capabilities */
754  uint32_t caps;
755 
756  /* Perform initialization of your backend. You might use this function
757  * to determine specific capabilities of the system, allocate required
758  * data structures for later, etc.
759  *
760  * This function is called when a libusb user initializes the library
761  * prior to use.
762  *
763  * Return 0 on success, or a LIBUSB_ERROR code on failure.
764  */
765  int (*init)(struct libusb_context *ctx);
766 
767  /* Deinitialization. Optional. This function should destroy anything
768  * that was set up by init.
769  *
770  * This function is called when the user deinitializes the library.
771  */
772  void (*exit)(struct libusb_context *ctx);
773 
774  /* Set a backend-specific option. Optional.
775  *
776  * This function is called when the user calls libusb_set_option() and
777  * the option is not handled by the core library.
778  *
779  * Return 0 on success, or a LIBUSB_ERROR code on failure.
780  */
781  int (*set_option)(struct libusb_context *ctx, enum libusb_option option,
782  va_list args);
783 
784  /* Enumerate all the USB devices on the system, returning them in a list
785  * of discovered devices.
786  *
787  * Your implementation should enumerate all devices on the system,
788  * regardless of whether they have been seen before or not.
789  *
790  * When you have found a device, compute a session ID for it. The session
791  * ID should uniquely represent that particular device for that particular
792  * connection session since boot (i.e. if you disconnect and reconnect a
793  * device immediately after, it should be assigned a different session ID).
794  * If your OS cannot provide a unique session ID as described above,
795  * presenting a session ID of (bus_number << 8 | device_address) should
796  * be sufficient. Bus numbers and device addresses wrap and get reused,
797  * but that is an unlikely case.
798  *
799  * After computing a session ID for a device, call
800  * usbi_get_device_by_session_id(). This function checks if libusb already
801  * knows about the device, and if so, it provides you with a reference
802  * to a libusb_device structure for it.
803  *
804  * If usbi_get_device_by_session_id() returns NULL, it is time to allocate
805  * a new device structure for the device. Call usbi_alloc_device() to
806  * obtain a new libusb_device structure with reference count 1. Populate
807  * the bus_number and device_address attributes of the new device, and
808  * perform any other internal backend initialization you need to do. At
809  * this point, you should be ready to provide device descriptors and so
810  * on through the get_*_descriptor functions. Finally, call
811  * usbi_sanitize_device() to perform some final sanity checks on the
812  * device. Assuming all of the above succeeded, we can now continue.
813  * If any of the above failed, remember to unreference the device that
814  * was returned by usbi_alloc_device().
815  *
816  * At this stage we have a populated libusb_device structure (either one
817  * that was found earlier, or one that we have just allocated and
818  * populated). This can now be added to the discovered devices list
819  * using discovered_devs_append(). Note that discovered_devs_append()
820  * may reallocate the list, returning a new location for it, and also
821  * note that reallocation can fail. Your backend should handle these
822  * error conditions appropriately.
823  *
824  * This function should not generate any bus I/O and should not block.
825  * If I/O is required (e.g. reading the active configuration value), it is
826  * OK to ignore these suggestions :)
827  *
828  * This function is executed when the user wishes to retrieve a list
829  * of USB devices connected to the system.
830  *
831  * If the backend has hotplug support, this function is not used!
832  *
833  * Return 0 on success, or a LIBUSB_ERROR code on failure.
834  */
835  int (*get_device_list)(struct libusb_context *ctx,
836  struct discovered_devs **discdevs);
837 
838  /* Apps which were written before hotplug support, may listen for
839  * hotplug events on their own and call libusb_get_device_list on
840  * device addition. In this case libusb_get_device_list will likely
841  * return a list without the new device in there, as the hotplug
842  * event thread will still be busy enumerating the device, which may
843  * take a while, or may not even have seen the event yet.
844  *
845  * To avoid this libusb_get_device_list will call this optional
846  * function for backends with hotplug support before copying
847  * ctx->usb_devs to the user. In this function the backend should
848  * ensure any pending hotplug events are fully processed before
849  * returning.
850  *
851  * Optional, should be implemented by backends with hotplug support.
852  */
853  void (*hotplug_poll)(void);
854 
855  /* Wrap a platform-specific device handle for I/O and other USB
856  * operations. The device handle is preallocated for you.
857  *
858  * Your backend should allocate any internal resources required for I/O
859  * and other operations so that those operations can happen (hopefully)
860  * without hiccup. This is also a good place to inform libusb that it
861  * should monitor certain file descriptors related to this device -
862  * see the usbi_add_event_source() function.
863  *
864  * Your backend should also initialize the device structure
865  * (dev_handle->dev), which is NULL at the beginning of the call.
866  *
867  * This function should not generate any bus I/O and should not block.
868  *
869  * This function is called when the user attempts to wrap an existing
870  * platform-specific device handle for a device.
871  *
872  * Return:
873  * - 0 on success
874  * - LIBUSB_ERROR_ACCESS if the user has insufficient permissions
875  * - another LIBUSB_ERROR code on other failure
876  *
877  * Do not worry about freeing the handle on failed open, the upper layers
878  * do this for you.
879  */
880  int (*wrap_sys_device)(struct libusb_context *ctx,
881  struct libusb_device_handle *dev_handle, intptr_t sys_dev);
882 
883  /* Open a device for I/O and other USB operations. The device handle
884  * is preallocated for you, you can retrieve the device in question
885  * through handle->dev.
886  *
887  * Your backend should allocate any internal resources required for I/O
888  * and other operations so that those operations can happen (hopefully)
889  * without hiccup. This is also a good place to inform libusb that it
890  * should monitor certain file descriptors related to this device -
891  * see the usbi_add_event_source() function.
892  *
893  * This function should not generate any bus I/O and should not block.
894  *
895  * This function is called when the user attempts to obtain a device
896  * handle for a device.
897  *
898  * Return:
899  * - 0 on success
900  * - LIBUSB_ERROR_ACCESS if the user has insufficient permissions
901  * - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since
902  * discovery
903  * - another LIBUSB_ERROR code on other failure
904  *
905  * Do not worry about freeing the handle on failed open, the upper layers
906  * do this for you.
907  */
908  int (*open)(struct libusb_device_handle *dev_handle);
909 
910  /* Close a device such that the handle cannot be used again. Your backend
911  * should destroy any resources that were allocated in the open path.
912  * This may also be a good place to call usbi_remove_event_source() to
913  * inform libusb of any event sources associated with this device that
914  * should no longer be monitored.
915  *
916  * This function is called when the user closes a device handle.
917  */
918  void (*close)(struct libusb_device_handle *dev_handle);
919 
920  /* Get the ACTIVE configuration descriptor for a device.
921  *
922  * The descriptor should be retrieved from memory, NOT via bus I/O to the
923  * device. This means that you may have to cache it in a private structure
924  * during get_device_list enumeration. You may also have to keep track
925  * of which configuration is active when the user changes it.
926  *
927  * This function is expected to write len bytes of data into buffer, which
928  * is guaranteed to be big enough. If you can only do a partial write,
929  * return an error code.
930  *
931  * This function is expected to return the descriptor in bus-endian format
932  * (LE).
933  *
934  * Return:
935  * - 0 on success
936  * - LIBUSB_ERROR_NOT_FOUND if the device is in unconfigured state
937  * - another LIBUSB_ERROR code on other failure
938  */
939  int (*get_active_config_descriptor)(struct libusb_device *device,
940  void *buffer, size_t len);
941 
942  /* Get a specific configuration descriptor for a device.
943  *
944  * The descriptor should be retrieved from memory, NOT via bus I/O to the
945  * device. This means that you may have to cache it in a private structure
946  * during get_device_list enumeration.
947  *
948  * The requested descriptor is expressed as a zero-based index (i.e. 0
949  * indicates that we are requesting the first descriptor). The index does
950  * not (necessarily) equal the bConfigurationValue of the configuration
951  * being requested.
952  *
953  * This function is expected to write len bytes of data into buffer, which
954  * is guaranteed to be big enough. If you can only do a partial write,
955  * return an error code.
956  *
957  * This function is expected to return the descriptor in bus-endian format
958  * (LE).
959  *
960  * Return the length read on success or a LIBUSB_ERROR code on failure.
961  */
962  int (*get_config_descriptor)(struct libusb_device *device,
963  uint8_t config_index, void *buffer, size_t len);
964 
965  /* Like get_config_descriptor but then by bConfigurationValue instead
966  * of by index.
967  *
968  * Optional, if not present the core will call get_config_descriptor
969  * for all configs until it finds the desired bConfigurationValue.
970  *
971  * Returns a pointer to the raw-descriptor in *buffer, this memory
972  * is valid as long as device is valid.
973  *
974  * Returns the length of the returned raw-descriptor on success,
975  * or a LIBUSB_ERROR code on failure.
976  */
977  int (*get_config_descriptor_by_value)(struct libusb_device *device,
978  uint8_t bConfigurationValue, void **buffer);
979 
980  /* Get the bConfigurationValue for the active configuration for a device.
981  * Optional. This should only be implemented if you can retrieve it from
982  * cache (don't generate I/O).
983  *
984  * If you cannot retrieve this from cache, either do not implement this
985  * function, or return LIBUSB_ERROR_NOT_SUPPORTED. This will cause
986  * libusb to retrieve the information through a standard control transfer.
987  *
988  * This function must be non-blocking.
989  * Return:
990  * - 0 on success
991  * - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it
992  * was opened
993  * - LIBUSB_ERROR_NOT_SUPPORTED if the value cannot be retrieved without
994  * blocking
995  * - another LIBUSB_ERROR code on other failure.
996  */
997  int (*get_configuration)(struct libusb_device_handle *dev_handle, uint8_t *config);
998 
999  /* Set the active configuration for a device.
1000  *
1001  * A configuration value of -1 should put the device in unconfigured state.
1002  *
1003  * This function can block.
1004  *
1005  * Return:
1006  * - 0 on success
1007  * - LIBUSB_ERROR_NOT_FOUND if the configuration does not exist
1008  * - LIBUSB_ERROR_BUSY if interfaces are currently claimed (and hence
1009  * configuration cannot be changed)
1010  * - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it
1011  * was opened
1012  * - another LIBUSB_ERROR code on other failure.
1013  */
1014  int (*set_configuration)(struct libusb_device_handle *dev_handle, int config);
1015 
1016  /* Claim an interface. When claimed, the application can then perform
1017  * I/O to an interface's endpoints.
1018  *
1019  * This function should not generate any bus I/O and should not block.
1020  * Interface claiming is a logical operation that simply ensures that
1021  * no other drivers/applications are using the interface, and after
1022  * claiming, no other drivers/applications can use the interface because
1023  * we now "own" it.
1024  *
1025  * Return:
1026  * - 0 on success
1027  * - LIBUSB_ERROR_NOT_FOUND if the interface does not exist
1028  * - LIBUSB_ERROR_BUSY if the interface is in use by another driver/app
1029  * - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it
1030  * was opened
1031  * - another LIBUSB_ERROR code on other failure
1032  */
1033  int (*claim_interface)(struct libusb_device_handle *dev_handle, uint8_t interface_number);
1034 
1035  /* Release a previously claimed interface.
1036  *
1037  * This function should also generate a SET_INTERFACE control request,
1038  * resetting the alternate setting of that interface to 0. It's OK for
1039  * this function to block as a result.
1040  *
1041  * You will only ever be asked to release an interface which was
1042  * successfully claimed earlier.
1043  *
1044  * Return:
1045  * - 0 on success
1046  * - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it
1047  * was opened
1048  * - another LIBUSB_ERROR code on other failure
1049  */
1050  int (*release_interface)(struct libusb_device_handle *dev_handle, uint8_t interface_number);
1051 
1052  /* Set the alternate setting for an interface.
1053  *
1054  * You will only ever be asked to set the alternate setting for an
1055  * interface which was successfully claimed earlier.
1056  *
1057  * It's OK for this function to block.
1058  *
1059  * Return:
1060  * - 0 on success
1061  * - LIBUSB_ERROR_NOT_FOUND if the alternate setting does not exist
1062  * - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it
1063  * was opened
1064  * - another LIBUSB_ERROR code on other failure
1065  */
1066  int (*set_interface_altsetting)(struct libusb_device_handle *dev_handle,
1067  uint8_t interface_number, uint8_t altsetting);
1068 
1069  /* Clear a halt/stall condition on an endpoint.
1070  *
1071  * It's OK for this function to block.
1072  *
1073  * Return:
1074  * - 0 on success
1075  * - LIBUSB_ERROR_NOT_FOUND if the endpoint does not exist
1076  * - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it
1077  * was opened
1078  * - another LIBUSB_ERROR code on other failure
1079  */
1080  int (*clear_halt)(struct libusb_device_handle *dev_handle,
1081  unsigned char endpoint);
1082 
1083  /* Perform a USB port reset to reinitialize a device. Optional.
1084  *
1085  * If possible, the device handle should still be usable after the reset
1086  * completes, assuming that the device descriptors did not change during
1087  * reset and all previous interface state can be restored.
1088  *
1089  * If something changes, or you cannot easily locate/verify the reset
1090  * device, return LIBUSB_ERROR_NOT_FOUND. This prompts the application
1091  * to close the old handle and re-enumerate the device.
1092  *
1093  * Return:
1094  * - 0 on success
1095  * - LIBUSB_ERROR_NOT_FOUND if re-enumeration is required, or if the device
1096  * has been disconnected since it was opened
1097  * - another LIBUSB_ERROR code on other failure
1098  */
1099  int (*reset_device)(struct libusb_device_handle *dev_handle);
1100 
1101  /* Alloc num_streams usb3 bulk streams on the passed in endpoints */
1102  int (*alloc_streams)(struct libusb_device_handle *dev_handle,
1103  uint32_t num_streams, unsigned char *endpoints, int num_endpoints);
1104 
1105  /* Free usb3 bulk streams allocated with alloc_streams */
1106  int (*free_streams)(struct libusb_device_handle *dev_handle,
1107  unsigned char *endpoints, int num_endpoints);
1108 
1109  /* Allocate persistent DMA memory for the given device, suitable for
1110  * zerocopy. May return NULL on failure. Optional to implement.
1111  */
1112  void *(*dev_mem_alloc)(struct libusb_device_handle *handle, size_t len);
1113 
1114  /* Free memory allocated by dev_mem_alloc. */
1115  int (*dev_mem_free)(struct libusb_device_handle *handle, void *buffer,
1116  size_t len);
1117 
1118  /* Determine if a kernel driver is active on an interface. Optional.
1119  *
1120  * The presence of a kernel driver on an interface indicates that any
1121  * calls to claim_interface would fail with the LIBUSB_ERROR_BUSY code.
1122  *
1123  * Return:
1124  * - 0 if no driver is active
1125  * - 1 if a driver is active
1126  * - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it
1127  * was opened
1128  * - another LIBUSB_ERROR code on other failure
1129  */
1130  int (*kernel_driver_active)(struct libusb_device_handle *dev_handle,
1131  uint8_t interface_number);
1132 
1133  /* Detach a kernel driver from an interface. Optional.
1134  *
1135  * After detaching a kernel driver, the interface should be available
1136  * for claim.
1137  *
1138  * Return:
1139  * - 0 on success
1140  * - LIBUSB_ERROR_NOT_FOUND if no kernel driver was active
1141  * - LIBUSB_ERROR_INVALID_PARAM if the interface does not exist
1142  * - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it
1143  * was opened
1144  * - another LIBUSB_ERROR code on other failure
1145  */
1146  int (*detach_kernel_driver)(struct libusb_device_handle *dev_handle,
1147  uint8_t interface_number);
1148 
1149  /* Attach a kernel driver to an interface. Optional.
1150  *
1151  * Reattach a kernel driver to the device.
1152  *
1153  * Return:
1154  * - 0 on success
1155  * - LIBUSB_ERROR_NOT_FOUND if no kernel driver was active
1156  * - LIBUSB_ERROR_INVALID_PARAM if the interface does not exist
1157  * - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it
1158  * was opened
1159  * - LIBUSB_ERROR_BUSY if a program or driver has claimed the interface,
1160  * preventing reattachment
1161  * - another LIBUSB_ERROR code on other failure
1162  */
1163  int (*attach_kernel_driver)(struct libusb_device_handle *dev_handle,
1164  uint8_t interface_number);
1165 
1166  /* Destroy a device. Optional.
1167  *
1168  * This function is called when the last reference to a device is
1169  * destroyed. It should free any resources allocated in the get_device_list
1170  * path.
1171  */
1172  void (*destroy_device)(struct libusb_device *dev);
1173 
1174  /* Submit a transfer. Your implementation should take the transfer,
1175  * morph it into whatever form your platform requires, and submit it
1176  * asynchronously.
1177  *
1178  * This function must not block.
1179  *
1180  * This function gets called with the flying_transfers_lock locked!
1181  *
1182  * Return:
1183  * - 0 on success
1184  * - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
1185  * - another LIBUSB_ERROR code on other failure
1186  */
1187  int (*submit_transfer)(struct usbi_transfer *itransfer);
1188 
1189  /* Cancel a previously submitted transfer.
1190  *
1191  * This function must not block. The transfer cancellation must complete
1192  * later, resulting in a call to usbi_handle_transfer_cancellation()
1193  * from the context of handle_events.
1194  */
1195  int (*cancel_transfer)(struct usbi_transfer *itransfer);
1196 
1197  /* Clear a transfer as if it has completed or cancelled, but do not
1198  * report any completion/cancellation to the library. You should free
1199  * all private data from the transfer as if you were just about to report
1200  * completion or cancellation.
1201  *
1202  * This function might seem a bit out of place. It is used when libusb
1203  * detects a disconnected device - it calls this function for all pending
1204  * transfers before reporting completion (with the disconnect code) to
1205  * the user. Maybe we can improve upon this internal interface in future.
1206  */
1207  void (*clear_transfer_priv)(struct usbi_transfer *itransfer);
1208 
1209  /* Handle any pending events on event sources. Optional.
1210  *
1211  * Provide this function when event sources directly indicate device
1212  * or transfer activity. If your backend does not have such event sources,
1213  * implement the handle_transfer_completion function below.
1214  *
1215  * This involves monitoring any active transfers and processing their
1216  * completion or cancellation.
1217  *
1218  * The function is passed a pointer that represents platform-specific
1219  * data for monitoring event sources (size count). This data is to be
1220  * (re)allocated as necessary when event sources are modified.
1221  * The num_ready parameter indicates the number of event sources that
1222  * have reported events. This should be enough information for you to
1223  * determine which actions need to be taken on the currently active
1224  * transfers.
1225  *
1226  * For any cancelled transfers, call usbi_handle_transfer_cancellation().
1227  * For completed transfers, call usbi_handle_transfer_completion().
1228  * For control/bulk/interrupt transfers, populate the "transferred"
1229  * element of the appropriate usbi_transfer structure before calling the
1230  * above functions. For isochronous transfers, populate the status and
1231  * transferred fields of the iso packet descriptors of the transfer.
1232  *
1233  * This function should also be able to detect disconnection of the
1234  * device, reporting that situation with usbi_handle_disconnect().
1235  *
1236  * When processing an event related to a transfer, you probably want to
1237  * take usbi_transfer.lock to prevent races. See the documentation for
1238  * the usbi_transfer structure.
1239  *
1240  * Return 0 on success, or a LIBUSB_ERROR code on failure.
1241  */
1242  int (*handle_events)(struct libusb_context *ctx,
1243  void *event_data, unsigned int count, unsigned int num_ready);
1244 
1245  /* Handle transfer completion. Optional.
1246  *
1247  * Provide this function when there are no event sources available that
1248  * directly indicate device or transfer activity. If your backend does
1249  * have such event sources, implement the handle_events function above.
1250  *
1251  * Your backend must tell the library when a transfer has completed by
1252  * calling usbi_signal_transfer_completion(). You should store any private
1253  * information about the transfer and its completion status in the transfer's
1254  * private backend data.
1255  *
1256  * During event handling, this function will be called on each transfer for
1257  * which usbi_signal_transfer_completion() was called.
1258  *
1259  * For any cancelled transfers, call usbi_handle_transfer_cancellation().
1260  * For completed transfers, call usbi_handle_transfer_completion().
1261  * For control/bulk/interrupt transfers, populate the "transferred"
1262  * element of the appropriate usbi_transfer structure before calling the
1263  * above functions. For isochronous transfers, populate the status and
1264  * transferred fields of the iso packet descriptors of the transfer.
1265  *
1266  * Return 0 on success, or a LIBUSB_ERROR code on failure.
1267  */
1268  int (*handle_transfer_completion)(struct usbi_transfer *itransfer);
1269 
1270  /* Number of bytes to reserve for per-context private backend data.
1271  * This private data area is accessible by calling
1272  * usbi_get_context_priv() on the libusb_context instance.
1273  */
1274  size_t context_priv_size;
1275 
1276  /* Number of bytes to reserve for per-device private backend data.
1277  * This private data area is accessible by calling
1278  * usbi_get_device_priv() on the libusb_device instance.
1279  */
1280  size_t device_priv_size;
1281 
1282  /* Number of bytes to reserve for per-handle private backend data.
1283  * This private data area is accessible by calling
1284  * usbi_get_device_handle_priv() on the libusb_device_handle instance.
1285  */
1286  size_t device_handle_priv_size;
1287 
1288  /* Number of bytes to reserve for per-transfer private backend data.
1289  * This private data area is accessible by calling
1290  * usbi_get_transfer_priv() on the usbi_transfer instance.
1291  */
1292  size_t transfer_priv_size;
1293 };
1294 
1295 extern const struct usbi_os_backend usbi_backend;
1296 
1297 #define for_each_context(c) \
1298  for_each_helper(c, &active_contexts_list, struct libusb_context)
1299 
1300 #define for_each_device(ctx, d) \
1301  for_each_helper(d, &(ctx)->usb_devs, struct libusb_device)
1302 
1303 #define for_each_device_safe(ctx, d, n) \
1304  for_each_safe_helper(d, n, &(ctx)->usb_devs, struct libusb_device)
1305 
1306 #define for_each_open_device(ctx, h) \
1307  for_each_helper(h, &(ctx)->open_devs, struct libusb_device_handle)
1308 
1309 #define for_each_transfer(ctx, t) \
1310  for_each_helper(t, &(ctx)->flying_transfers, struct usbi_transfer)
1311 
1312 #define for_each_transfer_safe(ctx, t, n) \
1313  for_each_safe_helper(t, n, &(ctx)->flying_transfers, struct usbi_transfer)
1314 
1315 #define for_each_event_source(ctx, e) \
1316  for_each_helper(e, &(ctx)->event_sources, struct usbi_event_source)
1317 
1318 #define for_each_removed_event_source(ctx, e) \
1319  for_each_helper(e, &(ctx)->removed_event_sources, struct usbi_event_source)
1320 
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)
1323 
1324 #ifdef __cplusplus
1325 }
1326 #endif
1327 
1328 #endif
Definition: libusbi.h:301
Definition: libusbi.h:749
#define libusb_le16_to_cpu
Definition: libusb.h:175
Definition: events_windows.h:35
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: libusb.h:525
Definition: getopt.h:94
Definition: libusbi.h:460
Definition: libusbi.h:652
libusb_transfer_status
Definition: libusb.h:1115
Definition: libusbi.h:570
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