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