| 1 |
/* |
| 2 |
* Internal header for libusb |
| 3 |
* Copyright © 2007-2009 Daniel Drake <[email protected]> |
| 4 |
* Copyright © 2001 Johannes Erdfelt <[email protected]> |
| 5 |
* Copyright © 2019 Nathan Hjelm <[email protected]> |
| 6 |
* Copyright © 2019-2020 Google LLC. All rights reserved. |
| 7 |
* Copyright © 2020 Chris Dickens <[email protected]> |
| 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 |
#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 |
((type *)((uintptr_t)(ptr) - (uintptr_t)offsetof(type, member))) |
| 57 |
|
| 58 |
#ifndef ARRAYSIZE |
| 59 |
#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 |
#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 |
#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 |
(((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 |
#define usbi_atomic_load(a) atomic_load((a)) |
| 120 |
#define usbi_atomic_store(a, v) atomic_store((a), (v)) |
| 121 |
#define usbi_atomic_inc(a) (atomic_fetch_add((a), 1) + 1) |
| 122 |
#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 |
#define USB_MAXENDPOINTS 32 |
| 150 |
#define USB_MAXINTERFACES 32 |
| 151 |
#define USB_MAXCONFIG 8 |
| 152 |
|
| 153 |
/* Backend specific capabilities */ |
| 154 |
#define USBI_CAP_HAS_HID_ACCESS 0x00010000 |
| 155 |
#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 |
container_of(ptr, type, member) |
| 173 |
|
| 174 |
#define list_first_entry(ptr, type, member) \ |
| 175 |
list_entry((ptr)->next, type, member) |
| 176 |
|
| 177 |
#define list_next_entry(ptr, type, member) \ |
| 178 |
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 |
for (pos = list_first_entry(head, type, member); \ |
| 188 |
&pos->member != (head); \ |
| 189 |
pos = list_next_entry(pos, type, member)) |
| 190 |
|
| 191 |
#define list_for_each_entry_safe(pos, n, head, member, type) \ |
| 192 |
for (pos = list_first_entry(head, type, member), \ |
| 193 |
n = list_next_entry(pos, type, member); \ |
| 194 |
&pos->member != (head); \ |
| 195 |
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 |
list_for_each_entry(pos, head, list, type) |
| 202 |
|
| 203 |
#define for_each_safe_helper(pos, n, head, type) \ |
| 204 |
list_for_each_entry_safe(pos, n, head, list, type) |
| 205 |
|
| 206 |
#define list_empty(entry) ((entry)->next == (entry)) |
| 207 |
|
| 208 |
static inline void list_init(struct list_head *entry) |
| 209 |
{ |
| 210 |
entry->prev = entry->next = entry; |
| 211 |
} |
| 212 |
|
| 213 |
static inline void list_add(struct list_head *entry, struct list_head *head) |
| 214 |
{ |
| 215 |
entry->next = head->next; |
| 216 |
entry->prev = head; |
| 217 |
|
| 218 |
head->next->prev = entry; |
| 219 |
head->next = entry; |
| 220 |
} |
| 221 |
|
| 222 |
static inline void list_add_tail(struct list_head *entry, |
| 223 |
struct list_head *head) |
| 224 |
{ |
| 225 |
entry->next = head; |
| 226 |
entry->prev = head->prev; |
| 227 |
|
| 228 |
head->prev->next = entry; |
| 229 |
head->prev = entry; |
| 230 |
} |
| 231 |
|
| 232 |
static inline void list_del(struct list_head *entry) |
| 233 |
{ |
| 234 |
entry->next->prev = entry->prev; |
| 235 |
entry->prev->next = entry->next; |
| 236 |
entry->next = entry->prev = NULL; |
| 237 |
} |
| 238 |
|
| 239 |
static inline void list_cut(struct list_head *list, struct list_head *head) |
| 240 |
{ |
| 241 |
if (list_empty(head)) { |
| 242 |
list_init(list); |
| 243 |
return; |
| 244 |
} |
| 245 |
|
| 246 |
list->next = head->next; |
| 247 |
list->next->prev = list; |
| 248 |
list->prev = head->prev; |
| 249 |
list->prev->next = list; |
| 250 |
|
| 251 |
list_init(head); |
| 252 |
} |
| 253 |
|
| 254 |
static inline void list_splice_front(struct list_head *list, struct list_head *head) |
| 255 |
{ |
| 256 |
list->next->prev = head; |
| 257 |
list->prev->next = head->next; |
| 258 |
head->next->prev = list->prev; |
| 259 |
head->next = list->next; |
| 260 |
} |
| 261 |
|
| 262 |
static inline void *usbi_reallocf(void *ptr, size_t size) |
| 263 |
{ |
| 264 |
void *ret = realloc(ptr, size); |
| 265 |
|
| 266 |
if (!ret) |
| 267 |
free(ptr); |
| 268 |
return ret; |
| 269 |
} |
| 270 |
|
| 271 |
#if !defined(USEC_PER_SEC) |
| 272 |
#define USEC_PER_SEC 1000000L |
| 273 |
#endif |
| 274 |
|
| 275 |
#if !defined(NSEC_PER_SEC) |
| 276 |
#define NSEC_PER_SEC 1000000000L |
| 277 |
#endif |
| 278 |
|
| 279 |
#define TIMEVAL_IS_VALID(tv) \ |
| 280 |
((tv)->tv_sec >= 0 && \ |
| 281 |
(tv)->tv_usec >= 0 && (tv)->tv_usec < USEC_PER_SEC) |
| 282 |
|
| 283 |
#define TIMESPEC_IS_SET(ts) ((ts)->tv_sec || (ts)->tv_nsec) |
| 284 |
#define TIMESPEC_CLEAR(ts) (ts)->tv_sec = (ts)->tv_nsec = 0 |
| 285 |
#define TIMESPEC_CMP(a, b, CMP) \ |
| 286 |
(((a)->tv_sec == (b)->tv_sec) \ |
| 287 |
? ((a)->tv_nsec CMP (b)->tv_nsec) \ |
| 288 |
: ((a)->tv_sec CMP (b)->tv_sec)) |
| 289 |
#define TIMESPEC_SUB(a, b, result) \ |
| 290 |
do { \ |
| 291 |
(result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \ |
| 292 |
(result)->tv_nsec = (a)->tv_nsec - (b)->tv_nsec; \ |
| 293 |
if ((result)->tv_nsec < 0L) { \ |
| 294 |
--(result)->tv_sec; \ |
| 295 |
(result)->tv_nsec += NSEC_PER_SEC; \ |
| 296 |
} \ |
| 297 |
} 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 |
#define usbi_err(ctx, ...) do { (void)(ctx); } while(0) |
| 338 |
#define usbi_warn(ctx, ...) do { (void)(ctx); } while(0) |
| 339 |
#define usbi_info(ctx, ...) do { (void)(ctx); } while(0) |
| 340 |
#define usbi_dbg(ctx, ...) do { (void)(ctx); } while(0) |
| 341 |
|
| 342 |
#endif /* ENABLE_LOGGING */ |
| 343 |
|
| 344 |
#define DEVICE_CTX(dev) ((dev)->ctx) |
| 345 |
#define HANDLE_CTX(handle) ((handle) ? DEVICE_CTX((handle)->dev) : NULL) |
| 346 |
#define ITRANSFER_CTX(itransfer) \ |
| 347 |
((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 |
#define IS_XFERIN(xfer) (0 != ((xfer)->endpoint & LIBUSB_ENDPOINT_IN)) |
| 354 |
#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 |
usbi_mutex_t usb_devs_lock; |
| 373 |
struct list_head usb_devs; |
| 374 |
|
| 375 |
/* A list of open handles. Backends are free to traverse this if required. |
| 376 |
*/ |
| 377 |
usbi_mutex_t open_devs_lock; |
| 378 |
struct list_head open_devs; |
| 379 |
|
| 380 |
/* A list of registered hotplug callbacks */ |
| 381 |
usbi_mutex_t hotplug_cbs_lock; |
| 382 |
struct list_head hotplug_cbs; |
| 383 |
libusb_hotplug_callback_handle next_hotplug_cb_handle; |
| 384 |
|
| 385 |
/* A flag to indicate that the context is ready for hotplug notifications */ |
| 386 |
usbi_atomic_t hotplug_ready; |
| 387 |
|
| 388 |
/* Note paths taking both this and usbi_transfer->lock must always |
| 389 |
* take this lock first */ |
| 390 |
usbi_mutex_t flying_transfers_lock; |
| 391 |
/* this is a list of in-flight transfer handles, sorted by timeout |
| 392 |
* expiration. URBs to timeout the soonest are placed at the beginning of |
| 393 |
* the list, URBs that will time out later are placed after, and urbs with |
| 394 |
* infinite timeout are always placed at the very end. */ |
| 395 |
struct list_head flying_transfers; |
| 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 usbi_mutex_static_t active_contexts_lock; |
| 455 |
extern struct list_head active_contexts_list; |
| 456 |
|
| 457 |
static inline struct libusb_context *usbi_get_context(struct libusb_context *ctx) |
| 458 |
{ |
| 459 |
static int warned = 0; |
| 460 |
|
| 461 |
if (!ctx) { |
| 462 |
ctx = usbi_default_context; |
| 463 |
} |
| 464 |
if (!ctx) { |
| 465 |
ctx = usbi_fallback_context; |
| 466 |
if (ctx && warned == 0) { |
| 467 |
usbi_err(ctx, "API misuse! Using non-default context as implicit default."); |
| 468 |
warned = 1; |
| 469 |
} |
| 470 |
} |
| 471 |
return ctx; |
| 472 |
} |
| 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 |
{ |
| 497 |
return usbi_tls_key_get(ctx->event_handling_key) != NULL; |
| 498 |
} |
| 499 |
|
| 500 |
static inline void usbi_start_event_handling(struct libusb_context *ctx) |
| 501 |
{ |
| 502 |
usbi_tls_key_set(ctx->event_handling_key, ctx); |
| 503 |
} |
| 504 |
|
| 505 |
static inline void usbi_end_event_handling(struct libusb_context *ctx) |
| 506 |
{ |
| 507 |
usbi_tls_key_set(ctx->event_handling_key, NULL); |
| 508 |
} |
| 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 |
char * device_strings_utf8[LIBUSB_DEVICE_STRING_COUNT]; |
| 528 |
}; |
| 529 |
|
| 530 |
struct libusb_device_handle { |
| 531 |
/* lock protects claimed_interfaces */ |
| 532 |
usbi_mutex_t lock; |
| 533 |
unsigned long claimed_interfaces; |
| 534 |
|
| 535 |
struct list_head list; |
| 536 |
struct libusb_device *dev; |
| 537 |
int auto_detach_kernel_driver; |
| 538 |
}; |
| 539 |
|
| 540 |
/* Function called by backend during device initialization to convert |
| 541 |
* multi-byte fields in the device descriptor to host-endian format. |
| 542 |
*/ |
| 543 |
static inline void usbi_localize_device_descriptor(struct libusb_device_descriptor *desc) |
| 544 |
{ |
| 545 |
desc->bcdUSB = libusb_le16_to_cpu(desc->bcdUSB); |
| 546 |
desc->idVendor = libusb_le16_to_cpu(desc->idVendor); |
| 547 |
desc->idProduct = libusb_le16_to_cpu(desc->idProduct); |
| 548 |
desc->bcdDevice = libusb_le16_to_cpu(desc->bcdDevice); |
| 549 |
} |
| 550 |
|
| 551 |
#if defined(HAVE_CLOCK_GETTIME) && !defined(__APPLE__) |
| 552 |
static inline void usbi_get_monotonic_time(struct timespec *tp) |
| 553 |
{ |
| 554 |
ASSERT_EQ(clock_gettime(CLOCK_MONOTONIC, tp), 0); |
| 555 |
} |
| 556 |
static inline void usbi_get_real_time(struct timespec *tp) |
| 557 |
{ |
| 558 |
ASSERT_EQ(clock_gettime(CLOCK_REALTIME, tp), 0); |
| 559 |
} |
| 560 |
#else |
| 561 |
/* If the platform doesn't provide the clock_gettime() function, the backend |
| 562 |
* must provide its own clock implementations. Two clock functions are |
| 563 |
* required: |
| 564 |
* |
| 565 |
* usbi_get_monotonic_time(): returns the time since an unspecified starting |
| 566 |
* point (usually boot) that is monotonically |
| 567 |
* increasing. |
| 568 |
* usbi_get_real_time(): returns the time since system epoch. |
| 569 |
*/ |
| 570 |
void usbi_get_monotonic_time(struct timespec *tp); |
| 571 |
void usbi_get_real_time(struct timespec *tp); |
| 572 |
#endif |
| 573 |
|
| 574 |
/* in-memory transfer layout: |
| 575 |
* |
| 576 |
* 1. os private data |
| 577 |
* 2. struct usbi_transfer |
| 578 |
* 3. struct libusb_transfer (which includes iso packets) [variable size] |
| 579 |
* |
| 580 |
* You can convert between them with the macros: |
| 581 |
* TRANSFER_PRIV_TO_USBI_TRANSFER |
| 582 |
* USBI_TRANSFER_TO_TRANSFER_PRIV |
| 583 |
* USBI_TRANSFER_TO_LIBUSB_TRANSFER |
| 584 |
* LIBUSB_TRANSFER_TO_USBI_TRANSFER |
| 585 |
*/ |
| 586 |
|
| 587 |
struct usbi_transfer { |
| 588 |
/* this lock is held during libusb_submit_transfer() and |
| 589 |
* libusb_cancel_transfer() (allowing the OS backend to prevent duplicate |
| 590 |
* cancellation, submission-during-cancellation, etc). the OS backend |
| 591 |
* should also take this lock in the handle_events path, to prevent the user |
| 592 |
* cancelling the transfer from another thread while you are processing |
| 593 |
* its completion (presumably there would be races within your OS backend |
| 594 |
* if this were possible). |
| 595 |
* Note paths taking both this and the flying_transfers_lock must |
| 596 |
* always take the flying_transfers_lock first */ |
| 597 |
usbi_mutex_t lock; |
| 598 |
|
| 599 |
struct list_head list; |
| 600 |
struct list_head completed_list; |
| 601 |
struct timespec timeout; |
| 602 |
int transferred; |
| 603 |
uint32_t stream_id; |
| 604 |
uint32_t state_flags; /* Protected by usbi_transfer->lock */ |
| 605 |
uint32_t timeout_flags; /* Protected by the flying_transfers_lock */ |
| 606 |
|
| 607 |
/* The device reference is held until destruction for logging |
| 608 |
* even after dev_handle is set to NULL. */ |
| 609 |
struct libusb_device *dev; |
| 610 |
|
| 611 |
void *priv; |
| 612 |
}; |
| 613 |
|
| 614 |
enum usbi_transfer_state_flags { |
| 615 |
/* Transfer successfully submitted by backend */ |
| 616 |
USBI_TRANSFER_IN_FLIGHT = 1U << 0, |
| 617 |
|
| 618 |
/* Cancellation was requested via libusb_cancel_transfer() */ |
| 619 |
USBI_TRANSFER_CANCELLING = 1U << 1, |
| 620 |
|
| 621 |
/* Operation on the transfer failed because the device disappeared */ |
| 622 |
USBI_TRANSFER_DEVICE_DISAPPEARED = 1U << 2, |
| 623 |
}; |
| 624 |
|
| 625 |
enum usbi_transfer_timeout_flags { |
| 626 |
/* Set by backend submit_transfer() if the OS handles timeout */ |
| 627 |
USBI_TRANSFER_OS_HANDLES_TIMEOUT = 1U << 0, |
| 628 |
|
| 629 |
/* The transfer timeout has been handled */ |
| 630 |
USBI_TRANSFER_TIMEOUT_HANDLED = 1U << 1, |
| 631 |
|
| 632 |
/* The transfer timeout was successfully processed */ |
| 633 |
USBI_TRANSFER_TIMED_OUT = 1U << 2, |
| 634 |
}; |
| 635 |
|
| 636 |
#define TRANSFER_PRIV_TO_USBI_TRANSFER(transfer_priv) \ |
| 637 |
((struct usbi_transfer *) \ |
| 638 |
((unsigned char *)(transfer_priv) \ |
| 639 |
+ PTR_ALIGN(sizeof(*transfer_priv)))) |
| 640 |
|
| 641 |
#define USBI_TRANSFER_TO_TRANSFER_PRIV(itransfer) \ |
| 642 |
((unsigned char *) \ |
| 643 |
((unsigned char *)(itransfer) \ |
| 644 |
- PTR_ALIGN(usbi_backend.transfer_priv_size))) |
| 645 |
|
| 646 |
#define USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer) \ |
| 647 |
((struct libusb_transfer *) \ |
| 648 |
((unsigned char *)(itransfer) \ |
| 649 |
+ PTR_ALIGN(sizeof(struct usbi_transfer)))) |
| 650 |
|
| 651 |
#define LIBUSB_TRANSFER_TO_USBI_TRANSFER(transfer) \ |
| 652 |
((struct usbi_transfer *) \ |
| 653 |
((unsigned char *)(transfer) \ |
| 654 |
- PTR_ALIGN(sizeof(struct usbi_transfer)))) |
| 655 |
|
| 656 |
#ifdef _MSC_VER |
| 657 |
#pragma pack(push, 1) |
| 658 |
#endif |
| 659 |
|
| 660 |
/* All standard descriptors have these 2 fields in common */ |
| 661 |
struct usbi_descriptor_header { |
| 662 |
uint8_t bLength; |
| 663 |
uint8_t bDescriptorType; |
| 664 |
} LIBUSB_PACKED; |
| 665 |
|
| 666 |
struct usbi_device_descriptor { |
| 667 |
uint8_t bLength; |
| 668 |
uint8_t bDescriptorType; |
| 669 |
uint16_t bcdUSB; |
| 670 |
uint8_t bDeviceClass; |
| 671 |
uint8_t bDeviceSubClass; |
| 672 |
uint8_t bDeviceProtocol; |
| 673 |
uint8_t bMaxPacketSize0; |
| 674 |
uint16_t idVendor; |
| 675 |
uint16_t idProduct; |
| 676 |
uint16_t bcdDevice; |
| 677 |
uint8_t iManufacturer; |
| 678 |
uint8_t iProduct; |
| 679 |
uint8_t iSerialNumber; |
| 680 |
uint8_t bNumConfigurations; |
| 681 |
} LIBUSB_PACKED; |
| 682 |
|
| 683 |
struct usbi_configuration_descriptor { |
| 684 |
uint8_t bLength; |
| 685 |
uint8_t bDescriptorType; |
| 686 |
uint16_t wTotalLength; |
| 687 |
uint8_t bNumInterfaces; |
| 688 |
uint8_t bConfigurationValue; |
| 689 |
uint8_t iConfiguration; |
| 690 |
uint8_t bmAttributes; |
| 691 |
uint8_t bMaxPower; |
| 692 |
} LIBUSB_PACKED; |
| 693 |
|
| 694 |
struct usbi_interface_descriptor { |
| 695 |
uint8_t bLength; |
| 696 |
uint8_t bDescriptorType; |
| 697 |
uint8_t bInterfaceNumber; |
| 698 |
uint8_t bAlternateSetting; |
| 699 |
uint8_t bNumEndpoints; |
| 700 |
uint8_t bInterfaceClass; |
| 701 |
uint8_t bInterfaceSubClass; |
| 702 |
uint8_t bInterfaceProtocol; |
| 703 |
uint8_t iInterface; |
| 704 |
} LIBUSB_PACKED; |
| 705 |
|
| 706 |
struct usbi_string_descriptor { |
| 707 |
uint8_t bLength; |
| 708 |
uint8_t bDescriptorType; |
| 709 |
uint16_t wData[LIBUSB_FLEXIBLE_ARRAY]; |
| 710 |
} LIBUSB_PACKED; |
| 711 |
|
| 712 |
struct usbi_bos_descriptor { |
| 713 |
uint8_t bLength; |
| 714 |
uint8_t bDescriptorType; |
| 715 |
uint16_t wTotalLength; |
| 716 |
uint8_t bNumDeviceCaps; |
| 717 |
} LIBUSB_PACKED; |
| 718 |
|
| 719 |
#ifdef _MSC_VER |
| 720 |
#pragma pack(pop) |
| 721 |
#endif |
| 722 |
|
| 723 |
union usbi_config_desc_buf { |
| 724 |
struct usbi_configuration_descriptor desc; |
| 725 |
uint8_t buf[LIBUSB_DT_CONFIG_SIZE]; |
| 726 |
uint16_t align; /* Force 2-byte alignment */ |
| 727 |
}; |
| 728 |
|
| 729 |
union usbi_string_desc_buf { |
| 730 |
struct usbi_string_descriptor desc; |
| 731 |
uint8_t buf[255]; /* Some devices choke on size > 255 */ |
| 732 |
uint16_t align; /* Force 2-byte alignment */ |
| 733 |
}; |
| 734 |
|
| 735 |
union usbi_bos_desc_buf { |
| 736 |
struct usbi_bos_descriptor desc; |
| 737 |
uint8_t buf[LIBUSB_DT_BOS_SIZE]; |
| 738 |
uint16_t align; /* Force 2-byte alignment */ |
| 739 |
}; |
| 740 |
|
| 741 |
enum usbi_hotplug_flags { |
| 742 |
/* This callback is interested in device arrivals */ |
| 743 |
USBI_HOTPLUG_DEVICE_ARRIVED = LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED, |
| 744 |
|
| 745 |
/* This callback is interested in device removals */ |
| 746 |
USBI_HOTPLUG_DEVICE_LEFT = LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT, |
| 747 |
|
| 748 |
/* IMPORTANT: The values for the below entries must start *after* |
| 749 |
* the highest value of the above entries!!! |
| 750 |
*/ |
| 751 |
|
| 752 |
/* The vendor_id field is valid for matching */ |
| 753 |
USBI_HOTPLUG_VENDOR_ID_VALID = (1U << 3), |
| 754 |
|
| 755 |
/* The product_id field is valid for matching */ |
| 756 |
USBI_HOTPLUG_PRODUCT_ID_VALID = (1U << 4), |
| 757 |
|
| 758 |
/* The dev_class field is valid for matching */ |
| 759 |
USBI_HOTPLUG_DEV_CLASS_VALID = (1U << 5), |
| 760 |
|
| 761 |
/* This callback has been unregistered and needs to be freed */ |
| 762 |
USBI_HOTPLUG_NEEDS_FREE = (1U << 6), |
| 763 |
}; |
| 764 |
|
| 765 |
struct usbi_hotplug_callback { |
| 766 |
/* Flags that control how this callback behaves */ |
| 767 |
uint8_t flags; |
| 768 |
|
| 769 |
/* Vendor ID to match (if flags says this is valid) */ |
| 770 |
uint16_t vendor_id; |
| 771 |
|
| 772 |
/* Product ID to match (if flags says this is valid) */ |
| 773 |
uint16_t product_id; |
| 774 |
|
| 775 |
/* Device class to match (if flags says this is valid) */ |
| 776 |
uint8_t dev_class; |
| 777 |
|
| 778 |
/* Callback function to invoke for matching event/device */ |
| 779 |
libusb_hotplug_callback_fn cb; |
| 780 |
|
| 781 |
/* Handle for this callback (used to match on deregister) */ |
| 782 |
libusb_hotplug_callback_handle handle; |
| 783 |
|
| 784 |
/* User data that will be passed to the callback function */ |
| 785 |
void *user_data; |
| 786 |
|
| 787 |
/* List this callback is registered in (ctx->hotplug_cbs) */ |
| 788 |
struct list_head list; |
| 789 |
}; |
| 790 |
|
| 791 |
struct usbi_hotplug_message { |
| 792 |
/* The hotplug event that occurred */ |
| 793 |
libusb_hotplug_event event; |
| 794 |
|
| 795 |
/* The device for which this hotplug event occurred */ |
| 796 |
struct libusb_device *device; |
| 797 |
|
| 798 |
/* List this message is contained in (ctx->hotplug_msgs) */ |
| 799 |
struct list_head list; |
| 800 |
}; |
| 801 |
|
| 802 |
/* shared data and functions */ |
| 803 |
|
| 804 |
void usbi_hotplug_init(struct libusb_context *ctx); |
| 805 |
void usbi_hotplug_exit(struct libusb_context *ctx); |
| 806 |
void usbi_hotplug_notification(struct libusb_context *ctx, struct libusb_device *dev, |
| 807 |
libusb_hotplug_event event); |
| 808 |
void usbi_hotplug_process(struct libusb_context *ctx, struct list_head *hotplug_msgs); |
| 809 |
|
| 810 |
int usbi_io_init(struct libusb_context *ctx); |
| 811 |
void usbi_io_exit(struct libusb_context *ctx); |
| 812 |
|
| 813 |
struct libusb_device *usbi_alloc_device(struct libusb_context *ctx, |
| 814 |
unsigned long session_id); |
| 815 |
struct libusb_device *usbi_get_device_by_session_id(struct libusb_context *ctx, |
| 816 |
unsigned long session_id); |
| 817 |
int usbi_sanitize_device(struct libusb_device *dev); |
| 818 |
void usbi_handle_disconnect(struct libusb_context *ctx, struct libusb_device_handle *dev_handle); |
| 819 |
|
| 820 |
int usbi_handle_transfer_completion(struct usbi_transfer *itransfer, |
| 821 |
enum libusb_transfer_status status); |
| 822 |
int usbi_handle_transfer_cancellation(struct usbi_transfer *itransfer); |
| 823 |
void usbi_signal_transfer_completion(struct usbi_transfer *itransfer); |
| 824 |
|
| 825 |
void usbi_attach_device(struct libusb_device *dev); |
| 826 |
void usbi_detach_device(struct libusb_device *dev); |
| 827 |
|
| 828 |
void usbi_connect_device(struct libusb_device *dev); |
| 829 |
void usbi_disconnect_device(struct libusb_device *dev); |
| 830 |
|
| 831 |
struct usbi_event_source_data { |
| 832 |
usbi_os_handle_t os_handle; |
| 833 |
short poll_events; |
| 834 |
}; |
| 835 |
|
| 836 |
struct usbi_event_source { |
| 837 |
struct usbi_event_source_data data; |
| 838 |
struct list_head list; |
| 839 |
}; |
| 840 |
|
| 841 |
int usbi_add_event_source(struct libusb_context *ctx, usbi_os_handle_t os_handle, |
| 842 |
short poll_events); |
| 843 |
void usbi_remove_event_source(struct libusb_context *ctx, usbi_os_handle_t os_handle); |
| 844 |
|
| 845 |
struct usbi_option { |
| 846 |
int is_set; |
| 847 |
union { |
| 848 |
int ival; |
| 849 |
libusb_log_cb log_cbval; |
| 850 |
} arg; |
| 851 |
}; |
| 852 |
|
| 853 |
/* OS event abstraction */ |
| 854 |
|
| 855 |
int usbi_create_event(usbi_event_t *event); |
| 856 |
void usbi_destroy_event(usbi_event_t *event); |
| 857 |
void usbi_signal_event(usbi_event_t *event); |
| 858 |
void usbi_clear_event(usbi_event_t *event); |
| 859 |
|
| 860 |
#ifdef HAVE_OS_TIMER |
| 861 |
int usbi_create_timer(usbi_timer_t *timer); |
| 862 |
void usbi_destroy_timer(usbi_timer_t *timer); |
| 863 |
int usbi_arm_timer(usbi_timer_t *timer, const struct timespec *timeout); |
| 864 |
int usbi_disarm_timer(usbi_timer_t *timer); |
| 865 |
#endif |
| 866 |
|
| 867 |
static inline int usbi_using_timer(struct libusb_context *ctx) |
| 868 |
{ |
| 869 |
#ifdef HAVE_OS_TIMER |
| 870 |
return usbi_timer_valid(&ctx->timer); |
| 871 |
#else |
| 872 |
UNUSED(ctx); |
| 873 |
return 0; |
| 874 |
#endif |
| 875 |
} |
| 876 |
|
| 877 |
struct usbi_reported_events { |
| 878 |
union { |
| 879 |
struct { |
| 880 |
unsigned int event_triggered:1; |
| 881 |
#ifdef HAVE_OS_TIMER |
| 882 |
unsigned int timer_triggered:1; |
| 883 |
#endif |
| 884 |
}; |
| 885 |
unsigned int event_bits; |
| 886 |
}; |
| 887 |
void *event_data; |
| 888 |
unsigned int event_data_count; |
| 889 |
unsigned int num_ready; |
| 890 |
}; |
| 891 |
|
| 892 |
int usbi_alloc_event_data(struct libusb_context *ctx); |
| 893 |
int usbi_wait_for_events(struct libusb_context *ctx, |
| 894 |
struct usbi_reported_events *reported_events, int timeout_ms); |
| 895 |
|
| 896 |
/* accessor functions for structure private data */ |
| 897 |
|
| 898 |
static inline void *usbi_get_context_priv(struct libusb_context *ctx) |
| 899 |
{ |
| 900 |
return (unsigned char *)ctx + PTR_ALIGN(sizeof(*ctx)); |
| 901 |
} |
| 902 |
|
| 903 |
static inline void *usbi_get_device_priv(struct libusb_device *dev) |
| 904 |
{ |
| 905 |
return (unsigned char *)dev + PTR_ALIGN(sizeof(*dev)); |
| 906 |
} |
| 907 |
|
| 908 |
static inline void *usbi_get_device_handle_priv(struct libusb_device_handle *dev_handle) |
| 909 |
{ |
| 910 |
return (unsigned char *)dev_handle + PTR_ALIGN(sizeof(*dev_handle)); |
| 911 |
} |
| 912 |
|
| 913 |
static inline void *usbi_get_transfer_priv(struct usbi_transfer *itransfer) |
| 914 |
{ |
| 915 |
return itransfer->priv; |
| 916 |
} |
| 917 |
|
| 918 |
/* device discovery */ |
| 919 |
|
| 920 |
/* we traverse usbfs without knowing how many devices we are going to find. |
| 921 |
* so we create this discovered_devs model which is similar to a linked-list |
| 922 |
* which grows when required. it can be freed once discovery has completed, |
| 923 |
* eliminating the need for a list node in the libusb_device structure |
| 924 |
* itself. */ |
| 925 |
struct discovered_devs { |
| 926 |
size_t len; |
| 927 |
size_t capacity; |
| 928 |
struct libusb_device *devices[LIBUSB_FLEXIBLE_ARRAY]; |
| 929 |
}; |
| 930 |
|
| 931 |
struct discovered_devs *discovered_devs_append( |
| 932 |
struct discovered_devs *discdevs, struct libusb_device *dev); |
| 933 |
|
| 934 |
/* OS abstraction */ |
| 935 |
|
| 936 |
/* This is the interface that OS backends need to implement. |
| 937 |
* All fields are mandatory, except ones explicitly noted as optional. */ |
| 938 |
struct usbi_os_backend { |
| 939 |
/* A human-readable name for your backend, e.g. "Linux usbfs" */ |
| 940 |
const char *name; |
| 941 |
|
| 942 |
/* Binary mask for backend specific capabilities */ |
| 943 |
uint32_t caps; |
| 944 |
|
| 945 |
/* Perform initialization of your backend. You might use this function |
| 946 |
* to determine specific capabilities of the system, allocate required |
| 947 |
* data structures for later, etc. |
| 948 |
* |
| 949 |
* This function is called when a libusb user initializes the library |
| 950 |
* prior to use. Mutual exclusion with other init and exit calls is |
| 951 |
* guaranteed when this function is called. |
| 952 |
* |
| 953 |
* Return 0 on success, or a LIBUSB_ERROR code on failure. |
| 954 |
*/ |
| 955 |
int (*init)(struct libusb_context *ctx); |
| 956 |
|
| 957 |
/* Deinitialization. Optional. This function should destroy anything |
| 958 |
* that was set up by init. |
| 959 |
* |
| 960 |
* This function is called when the user deinitializes the library. |
| 961 |
* Mutual exclusion with other init and exit calls is guaranteed when |
| 962 |
* this function is called. |
| 963 |
*/ |
| 964 |
void (*exit)(struct libusb_context *ctx); |
| 965 |
|
| 966 |
/* Set a backend-specific option. Optional. |
| 967 |
* |
| 968 |
* This function is called when the user calls libusb_set_option() and |
| 969 |
* the option is not handled by the core library. |
| 970 |
* |
| 971 |
* Return 0 on success, or a LIBUSB_ERROR code on failure. |
| 972 |
*/ |
| 973 |
int (*set_option)(struct libusb_context *ctx, enum libusb_option option, |
| 974 |
va_list args); |
| 975 |
|
| 976 |
/* Enumerate all the USB devices on the system, returning them in a list |
| 977 |
* of discovered devices. |
| 978 |
* |
| 979 |
* Your implementation should enumerate all devices on the system, |
| 980 |
* regardless of whether they have been seen before or not. |
| 981 |
* |
| 982 |
* When you have found a device, compute a session ID for it. The session |
| 983 |
* ID should uniquely represent that particular device for that particular |
| 984 |
* connection session since boot (i.e. if you disconnect and reconnect a |
| 985 |
* device immediately after, it should be assigned a different session ID). |
| 986 |
* If your OS cannot provide a unique session ID as described above, |
| 987 |
* presenting a session ID of (bus_number << 8 | device_address) should |
| 988 |
* be sufficient. Bus numbers and device addresses wrap and get reused, |
| 989 |
* but that is an unlikely case. |
| 990 |
* |
| 991 |
* After computing a session ID for a device, call |
| 992 |
* usbi_get_device_by_session_id(). This function checks if libusb already |
| 993 |
* knows about the device, and if so, it provides you with a reference |
| 994 |
* to a libusb_device structure for it. |
| 995 |
* |
| 996 |
* If usbi_get_device_by_session_id() returns NULL, it is time to allocate |
| 997 |
* a new device structure for the device. Call usbi_alloc_device() to |
| 998 |
* obtain a new libusb_device structure with reference count 1. Populate |
| 999 |
* the bus_number and device_address attributes of the new device, and |
| 1000 |
* perform any other internal backend initialization you need to do. At |
| 1001 |
* this point, you should be ready to provide device descriptors and so |
| 1002 |
* on through the get_*_descriptor functions. Finally, call |
| 1003 |
* usbi_sanitize_device() to perform some final sanity checks on the |
| 1004 |
* device. Assuming all of the above succeeded, call |
| 1005 |
* usbi_connect_device() to add the device to the context's device list |
| 1006 |
* and make it discoverable by usbi_get_device_by_session_id(). |
| 1007 |
* If any of the above failed, remember to unreference the device that |
| 1008 |
* was returned by usbi_alloc_device(). |
| 1009 |
* |
| 1010 |
* At this stage we have a populated libusb_device structure (either one |
| 1011 |
* that was found earlier, or one that we have just allocated and |
| 1012 |
* populated). This can now be added to the discovered devices list |
| 1013 |
* using discovered_devs_append(). Note that discovered_devs_append() |
| 1014 |
* may reallocate the list, returning a new location for it, and also |
| 1015 |
* note that reallocation can fail. Your backend should handle these |
| 1016 |
* error conditions appropriately. |
| 1017 |
* |
| 1018 |
* This function should not generate any bus I/O and should not block. |
| 1019 |
* If I/O is required (e.g. reading the active configuration value), it is |
| 1020 |
* OK to ignore these suggestions :) |
| 1021 |
* |
| 1022 |
* This function is executed when the user wishes to retrieve a list |
| 1023 |
* of USB devices connected to the system. |
| 1024 |
* |
| 1025 |
* If the backend has hotplug support, this function is not used! |
| 1026 |
* |
| 1027 |
* Return 0 on success, or a LIBUSB_ERROR code on failure. |
| 1028 |
*/ |
| 1029 |
int (*get_device_list)(struct libusb_context *ctx, |
| 1030 |
struct discovered_devs **discdevs); |
| 1031 |
|
| 1032 |
/* Retrieve a device string without needing to open the device. |
| 1033 |
* |
| 1034 |
* The string should be retrieved without opening the device |
| 1035 |
* and ideally without performing USB transactions to the device. |
| 1036 |
* Most operating systems read and cache the common string |
| 1037 |
* descriptors. Use the OS-specific calls to retrieve these strings. |
| 1038 |
* |
| 1039 |
* Since the USB string descriptor could be processed by the OS, |
| 1040 |
* this function returns a UTF-8 encoded string. |
| 1041 |
* |
| 1042 |
* The string will be returned untranslated or in the default OS language |
| 1043 |
* when supported by the OS and USB device. |
| 1044 |
* |
| 1045 |
* This function must not write more than length bytes into data, |
| 1046 |
* including the null terminator. |
| 1047 |
* |
| 1048 |
* Return: |
| 1049 |
* - The actual length in bytes including the null termintor on success. |
| 1050 |
* - LIBUSB_ERROR_NO_DEVICE if device not found. |
| 1051 |
* - LIBUSB_ERROR_INVALID_PARAM if any parameter is invalid. |
| 1052 |
* - another LIBUSB_ERROR code on other failure |
| 1053 |
*/ |
| 1054 |
int (*get_device_string)(libusb_device *dev, |
| 1055 |
enum libusb_device_string_type string_type, char *data, int length); |
| 1056 |
|
| 1057 |
/* Apps which were written before hotplug support, may listen for |
| 1058 |
* hotplug events on their own and call libusb_get_device_list on |
| 1059 |
* device addition. In this case libusb_get_device_list will likely |
| 1060 |
* return a list without the new device in there, as the hotplug |
| 1061 |
* event thread will still be busy enumerating the device, which may |
| 1062 |
* take a while, or may not even have seen the event yet. |
| 1063 |
* |
| 1064 |
* To avoid this libusb_get_device_list will call this optional |
| 1065 |
* function for backends with hotplug support before copying |
| 1066 |
* ctx->usb_devs to the user. In this function the backend should |
| 1067 |
* ensure any pending hotplug events are fully processed before |
| 1068 |
* returning. |
| 1069 |
* |
| 1070 |
* Optional, should be implemented by backends with hotplug support. |
| 1071 |
*/ |
| 1072 |
void (*hotplug_poll)(void); |
| 1073 |
|
| 1074 |
/* Wrap a platform-specific device handle for I/O and other USB |
| 1075 |
* operations. The device handle is preallocated for you. |
| 1076 |
* |
| 1077 |
* Your backend should allocate any internal resources required for I/O |
| 1078 |
* and other operations so that those operations can happen (hopefully) |
| 1079 |
* without hiccup. This is also a good place to inform libusb that it |
| 1080 |
* should monitor certain file descriptors related to this device - |
| 1081 |
* see the usbi_add_event_source() function. |
| 1082 |
* |
| 1083 |
* Your backend should also initialize the device structure |
| 1084 |
* (dev_handle->dev), which is NULL at the beginning of the call. |
| 1085 |
* |
| 1086 |
* This function should not generate any bus I/O and should not block. |
| 1087 |
* |
| 1088 |
* This function is called when the user attempts to wrap an existing |
| 1089 |
* platform-specific device handle for a device. |
| 1090 |
* |
| 1091 |
* Return: |
| 1092 |
* - 0 on success |
| 1093 |
* - LIBUSB_ERROR_ACCESS if the user has insufficient permissions |
| 1094 |
* - another LIBUSB_ERROR code on other failure |
| 1095 |
* |
| 1096 |
* Do not worry about freeing the handle on failed open, the upper layers |
| 1097 |
* do this for you. |
| 1098 |
*/ |
| 1099 |
int (*wrap_sys_device)(struct libusb_context *ctx, |
| 1100 |
struct libusb_device_handle *dev_handle, intptr_t sys_dev); |
| 1101 |
|
| 1102 |
/* Open a device for I/O and other USB operations. The device handle |
| 1103 |
* is preallocated for you, you can retrieve the device in question |
| 1104 |
* through handle->dev. |
| 1105 |
* |
| 1106 |
* Your backend should allocate any internal resources required for I/O |
| 1107 |
* and other operations so that those operations can happen (hopefully) |
| 1108 |
* without hiccup. This is also a good place to inform libusb that it |
| 1109 |
* should monitor certain file descriptors related to this device - |
| 1110 |
* see the usbi_add_event_source() function. |
| 1111 |
* |
| 1112 |
* This function should not generate any bus I/O and should not block. |
| 1113 |
* |
| 1114 |
* This function is called when the user attempts to obtain a device |
| 1115 |
* handle for a device. |
| 1116 |
* |
| 1117 |
* Return: |
| 1118 |
* - 0 on success |
| 1119 |
* - LIBUSB_ERROR_ACCESS if the user has insufficient permissions |
| 1120 |
* - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since |
| 1121 |
* discovery |
| 1122 |
* - another LIBUSB_ERROR code on other failure |
| 1123 |
* |
| 1124 |
* Do not worry about freeing the handle on failed open, the upper layers |
| 1125 |
* do this for you. |
| 1126 |
*/ |
| 1127 |
int (*open)(struct libusb_device_handle *dev_handle); |
| 1128 |
|
| 1129 |
/* Close a device such that the handle cannot be used again. Your backend |
| 1130 |
* should destroy any resources that were allocated in the open path. |
| 1131 |
* This may also be a good place to call usbi_remove_event_source() to |
| 1132 |
* inform libusb of any event sources associated with this device that |
| 1133 |
* should no longer be monitored. |
| 1134 |
* |
| 1135 |
* This function is called when the user closes a device handle. |
| 1136 |
*/ |
| 1137 |
void (*close)(struct libusb_device_handle *dev_handle); |
| 1138 |
|
| 1139 |
/* Get the ACTIVE configuration descriptor for a device. |
| 1140 |
* |
| 1141 |
* The descriptor should be retrieved from memory, NOT via bus I/O to the |
| 1142 |
* device. This means that you may have to cache it in a private structure |
| 1143 |
* during get_device_list enumeration. You may also have to keep track |
| 1144 |
* of which configuration is active when the user changes it. |
| 1145 |
* |
| 1146 |
* This function is expected to write len bytes of data into buffer, which |
| 1147 |
* is guaranteed to be big enough. If you can only do a partial write, |
| 1148 |
* return an error code. |
| 1149 |
* |
| 1150 |
* This function is expected to return the descriptor in bus-endian format |
| 1151 |
* (LE). |
| 1152 |
* |
| 1153 |
* Return: |
| 1154 |
* - 0 on success |
| 1155 |
* - LIBUSB_ERROR_NOT_FOUND if the device is in unconfigured state |
| 1156 |
* - another LIBUSB_ERROR code on other failure |
| 1157 |
*/ |
| 1158 |
int (*get_active_config_descriptor)(struct libusb_device *device, |
| 1159 |
void *buffer, size_t len); |
| 1160 |
|
| 1161 |
/* Get a specific configuration descriptor for a device. |
| 1162 |
* |
| 1163 |
* The descriptor should be retrieved from memory, NOT via bus I/O to the |
| 1164 |
* device. This means that you may have to cache it in a private structure |
| 1165 |
* during get_device_list enumeration. |
| 1166 |
* |
| 1167 |
* The requested descriptor is expressed as a zero-based index (i.e. 0 |
| 1168 |
* indicates that we are requesting the first descriptor). The index does |
| 1169 |
* not (necessarily) equal the bConfigurationValue of the configuration |
| 1170 |
* being requested. |
| 1171 |
* |
| 1172 |
* This function is expected to write len bytes of data into buffer, which |
| 1173 |
* is guaranteed to be big enough. If you can only do a partial write, |
| 1174 |
* return an error code. |
| 1175 |
* |
| 1176 |
* This function is expected to return the descriptor in bus-endian format |
| 1177 |
* (LE). |
| 1178 |
* |
| 1179 |
* Return the length read on success or a LIBUSB_ERROR code on failure. |
| 1180 |
*/ |
| 1181 |
int (*get_config_descriptor)(struct libusb_device *device, |
| 1182 |
uint8_t config_index, void *buffer, size_t len); |
| 1183 |
|
| 1184 |
/* Like get_config_descriptor but then by bConfigurationValue instead |
| 1185 |
* of by index. |
| 1186 |
* |
| 1187 |
* Optional, if not present the core will call get_config_descriptor |
| 1188 |
* for all configs until it finds the desired bConfigurationValue. |
| 1189 |
* |
| 1190 |
* Returns a pointer to the raw-descriptor in *buffer, this memory |
| 1191 |
* is valid as long as device is valid. |
| 1192 |
* |
| 1193 |
* Returns the length of the returned raw-descriptor on success, |
| 1194 |
* or a LIBUSB_ERROR code on failure. |
| 1195 |
*/ |
| 1196 |
int (*get_config_descriptor_by_value)(struct libusb_device *device, |
| 1197 |
uint8_t bConfigurationValue, void **buffer); |
| 1198 |
|
| 1199 |
/* Get the bConfigurationValue for the active configuration for a device. |
| 1200 |
* Optional. This should only be implemented if you can retrieve it from |
| 1201 |
* cache (don't generate I/O). |
| 1202 |
* |
| 1203 |
* If you cannot retrieve this from cache, either do not implement this |
| 1204 |
* function, or return LIBUSB_ERROR_NOT_SUPPORTED. This will cause |
| 1205 |
* libusb to retrieve the information through a standard control transfer. |
| 1206 |
* |
| 1207 |
* This function must be non-blocking. |
| 1208 |
* Return: |
| 1209 |
* - 0 on success |
| 1210 |
* - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it |
| 1211 |
* was opened |
| 1212 |
* - LIBUSB_ERROR_NOT_SUPPORTED if the value cannot be retrieved without |
| 1213 |
* blocking |
| 1214 |
* - another LIBUSB_ERROR code on other failure. |
| 1215 |
*/ |
| 1216 |
int (*get_configuration)(struct libusb_device_handle *dev_handle, uint8_t *config); |
| 1217 |
|
| 1218 |
/* Set the active configuration for a device. |
| 1219 |
* |
| 1220 |
* A configuration value of -1 should put the device in unconfigured state. |
| 1221 |
* |
| 1222 |
* This function can block. |
| 1223 |
* |
| 1224 |
* Return: |
| 1225 |
* - 0 on success |
| 1226 |
* - LIBUSB_ERROR_NOT_FOUND if the configuration does not exist |
| 1227 |
* - LIBUSB_ERROR_BUSY if interfaces are currently claimed (and hence |
| 1228 |
* configuration cannot be changed) |
| 1229 |
* - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it |
| 1230 |
* was opened |
| 1231 |
* - another LIBUSB_ERROR code on other failure. |
| 1232 |
*/ |
| 1233 |
int (*set_configuration)(struct libusb_device_handle *dev_handle, int config); |
| 1234 |
|
| 1235 |
/* Claim an interface. When claimed, the application can then perform |
| 1236 |
* I/O to an interface's endpoints. |
| 1237 |
* |
| 1238 |
* This function should not generate any bus I/O and should not block. |
| 1239 |
* Interface claiming is a logical operation that simply ensures that |
| 1240 |
* no other drivers/applications are using the interface, and after |
| 1241 |
* claiming, no other drivers/applications can use the interface because |
| 1242 |
* we now "own" it. |
| 1243 |
* |
| 1244 |
* This function gets called with dev_handle->lock locked! |
| 1245 |
* |
| 1246 |
* Return: |
| 1247 |
* - 0 on success |
| 1248 |
* - LIBUSB_ERROR_NOT_FOUND if the interface does not exist |
| 1249 |
* - LIBUSB_ERROR_BUSY if the interface is in use by another driver/app |
| 1250 |
* - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it |
| 1251 |
* was opened |
| 1252 |
* - another LIBUSB_ERROR code on other failure |
| 1253 |
*/ |
| 1254 |
int (*claim_interface)(struct libusb_device_handle *dev_handle, uint8_t interface_number); |
| 1255 |
|
| 1256 |
/* Release a previously claimed interface. |
| 1257 |
* |
| 1258 |
* This function should also generate a SET_INTERFACE control request, |
| 1259 |
* resetting the alternate setting of that interface to 0. It's OK for |
| 1260 |
* this function to block as a result. |
| 1261 |
* |
| 1262 |
* You will only ever be asked to release an interface which was |
| 1263 |
* successfully claimed earlier. |
| 1264 |
* |
| 1265 |
* This function gets called with dev_handle->lock locked! |
| 1266 |
* |
| 1267 |
* Return: |
| 1268 |
* - 0 on success |
| 1269 |
* - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it |
| 1270 |
* was opened |
| 1271 |
* - another LIBUSB_ERROR code on other failure |
| 1272 |
*/ |
| 1273 |
int (*release_interface)(struct libusb_device_handle *dev_handle, uint8_t interface_number); |
| 1274 |
|
| 1275 |
/* Set the alternate setting for an interface. |
| 1276 |
* |
| 1277 |
* You will only ever be asked to set the alternate setting for an |
| 1278 |
* interface which was successfully claimed earlier. |
| 1279 |
* |
| 1280 |
* It's OK for this function to block. |
| 1281 |
* |
| 1282 |
* Return: |
| 1283 |
* - 0 on success |
| 1284 |
* - LIBUSB_ERROR_NOT_FOUND if the alternate setting does not exist |
| 1285 |
* - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it |
| 1286 |
* was opened |
| 1287 |
* - another LIBUSB_ERROR code on other failure |
| 1288 |
*/ |
| 1289 |
int (*set_interface_altsetting)(struct libusb_device_handle *dev_handle, |
| 1290 |
uint8_t interface_number, uint8_t altsetting); |
| 1291 |
|
| 1292 |
/* Clear a halt/stall condition on an endpoint. |
| 1293 |
* |
| 1294 |
* It's OK for this function to block. |
| 1295 |
* |
| 1296 |
* Return: |
| 1297 |
* - 0 on success |
| 1298 |
* - LIBUSB_ERROR_NOT_FOUND if the endpoint does not exist |
| 1299 |
* - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it |
| 1300 |
* was opened |
| 1301 |
* - another LIBUSB_ERROR code on other failure |
| 1302 |
*/ |
| 1303 |
int (*clear_halt)(struct libusb_device_handle *dev_handle, |
| 1304 |
unsigned char endpoint); |
| 1305 |
|
| 1306 |
/* Perform a USB port reset to reinitialize a device. Optional. |
| 1307 |
* |
| 1308 |
* If possible, the device handle should still be usable after the reset |
| 1309 |
* completes, assuming that the device descriptors did not change during |
| 1310 |
* reset and all previous interface state can be restored. |
| 1311 |
* |
| 1312 |
* If something changes, or you cannot easily locate/verify the reset |
| 1313 |
* device, return LIBUSB_ERROR_NOT_FOUND. This prompts the application |
| 1314 |
* to close the old handle and re-enumerate the device. |
| 1315 |
* |
| 1316 |
* Return: |
| 1317 |
* - 0 on success |
| 1318 |
* - LIBUSB_ERROR_NOT_FOUND if re-enumeration is required, or if the device |
| 1319 |
* has been disconnected since it was opened |
| 1320 |
* - another LIBUSB_ERROR code on other failure |
| 1321 |
*/ |
| 1322 |
int (*reset_device)(struct libusb_device_handle *dev_handle); |
| 1323 |
|
| 1324 |
/* Alloc num_streams usb3 bulk streams on the passed in endpoints */ |
| 1325 |
int (*alloc_streams)(struct libusb_device_handle *dev_handle, |
| 1326 |
uint32_t num_streams, unsigned char *endpoints, int num_endpoints); |
| 1327 |
|
| 1328 |
/* Free usb3 bulk streams allocated with alloc_streams */ |
| 1329 |
int (*free_streams)(struct libusb_device_handle *dev_handle, |
| 1330 |
unsigned char *endpoints, int num_endpoints); |
| 1331 |
|
| 1332 |
/* Allocate persistent DMA memory for the given device, suitable for |
| 1333 |
* zerocopy. May return NULL on failure. Optional to implement. |
| 1334 |
*/ |
| 1335 |
void *(*dev_mem_alloc)(struct libusb_device_handle *handle, size_t len); |
| 1336 |
|
| 1337 |
/* Free memory allocated by dev_mem_alloc. */ |
| 1338 |
int (*dev_mem_free)(struct libusb_device_handle *handle, void *buffer, |
| 1339 |
size_t len); |
| 1340 |
|
| 1341 |
/* Determine if a kernel driver is active on an interface. Optional. |
| 1342 |
* |
| 1343 |
* The presence of a kernel driver on an interface indicates that any |
| 1344 |
* calls to claim_interface would fail with the LIBUSB_ERROR_BUSY code. |
| 1345 |
* |
| 1346 |
* Return: |
| 1347 |
* - 0 if no driver is active |
| 1348 |
* - 1 if a driver is active |
| 1349 |
* - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it |
| 1350 |
* was opened |
| 1351 |
* - another LIBUSB_ERROR code on other failure |
| 1352 |
*/ |
| 1353 |
int (*kernel_driver_active)(struct libusb_device_handle *dev_handle, |
| 1354 |
uint8_t interface_number); |
| 1355 |
|
| 1356 |
/* Detach a kernel driver from an interface. Optional. |
| 1357 |
* |
| 1358 |
* After detaching a kernel driver, the interface should be available |
| 1359 |
* for claim. |
| 1360 |
* |
| 1361 |
* Return: |
| 1362 |
* - 0 on success |
| 1363 |
* - LIBUSB_ERROR_NOT_FOUND if no kernel driver was active |
| 1364 |
* - LIBUSB_ERROR_INVALID_PARAM if the interface does not exist |
| 1365 |
* - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it |
| 1366 |
* was opened |
| 1367 |
* - another LIBUSB_ERROR code on other failure |
| 1368 |
*/ |
| 1369 |
int (*detach_kernel_driver)(struct libusb_device_handle *dev_handle, |
| 1370 |
uint8_t interface_number); |
| 1371 |
|
| 1372 |
/* Attach a kernel driver to an interface. Optional. |
| 1373 |
* |
| 1374 |
* Reattach a kernel driver to the device. |
| 1375 |
* |
| 1376 |
* Return: |
| 1377 |
* - 0 on success |
| 1378 |
* - LIBUSB_ERROR_NOT_FOUND if no kernel driver was active |
| 1379 |
* - LIBUSB_ERROR_INVALID_PARAM if the interface does not exist |
| 1380 |
* - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it |
| 1381 |
* was opened |
| 1382 |
* - LIBUSB_ERROR_BUSY if a program or driver has claimed the interface, |
| 1383 |
* preventing reattachment |
| 1384 |
* - another LIBUSB_ERROR code on other failure |
| 1385 |
*/ |
| 1386 |
int (*attach_kernel_driver)(struct libusb_device_handle *dev_handle, |
| 1387 |
uint8_t interface_number); |
| 1388 |
|
| 1389 |
/** Check if RAW_IO is supported by an endpoint. |
| 1390 |
* |
| 1391 |
* Return: |
| 1392 |
* - 1 if yes |
| 1393 |
* - 0 if no |
| 1394 |
* - a LIBUSB_ERROR code on failure |
| 1395 |
*/ |
| 1396 |
int (*endpoint_supports_raw_io)(struct libusb_device_handle* dev_handle, |
| 1397 |
uint8_t endpoint); |
| 1398 |
|
| 1399 |
/** Enable/disable RAW_IO for an endpoint. |
| 1400 |
* |
| 1401 |
* Return: |
| 1402 |
* - 0 on success |
| 1403 |
* - LIBUSB_ERROR_NOT_SUPPORTED if RAW_IO is not supported by the endpoint |
| 1404 |
* - another LIBUSB_ERROR code on other failure |
| 1405 |
*/ |
| 1406 |
int (*endpoint_set_raw_io)(struct libusb_device_handle* dev_handle, |
| 1407 |
uint8_t endpoint, int enable); |
| 1408 |
|
| 1409 |
/* Retrieve the maximum transfer size in bytes supported for WinUSB RAW_IO |
| 1410 |
* for an inbound bulk or interrupt endpoint on an open device. Optional. |
| 1411 |
* |
| 1412 |
* Return: |
| 1413 |
* - a positive maximum transfer size on success |
| 1414 |
* - a LIBUSB_ERROR code on failure |
| 1415 |
*/ |
| 1416 |
int (*get_max_raw_io_transfer_size)( |
| 1417 |
struct libusb_device_handle *dev_handle, |
| 1418 |
uint8_t endpoint); |
| 1419 |
|
| 1420 |
/* Destroy a device. Optional. |
| 1421 |
* |
| 1422 |
* This function is called when the last reference to a device is |
| 1423 |
* destroyed. It should free any resources allocated in the get_device_list |
| 1424 |
* path. |
| 1425 |
*/ |
| 1426 |
void (*destroy_device)(struct libusb_device *dev); |
| 1427 |
|
| 1428 |
/* Submit a transfer. Your implementation should take the transfer, |
| 1429 |
* morph it into whatever form your platform requires, and submit it |
| 1430 |
* asynchronously. |
| 1431 |
* |
| 1432 |
* This function must not block. |
| 1433 |
* |
| 1434 |
* This function gets called with itransfer->lock locked! |
| 1435 |
* |
| 1436 |
* Return: |
| 1437 |
* - 0 on success |
| 1438 |
* - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected |
| 1439 |
* - another LIBUSB_ERROR code on other failure |
| 1440 |
*/ |
| 1441 |
int (*submit_transfer)(struct usbi_transfer *itransfer); |
| 1442 |
|
| 1443 |
/* Cancel a previously submitted transfer. |
| 1444 |
* |
| 1445 |
* This function must not block. The transfer cancellation must complete |
| 1446 |
* later, resulting in a call to usbi_handle_transfer_cancellation() |
| 1447 |
* from the context of handle_events. |
| 1448 |
* |
| 1449 |
* This function gets called with itransfer->lock locked! |
| 1450 |
*/ |
| 1451 |
int (*cancel_transfer)(struct usbi_transfer *itransfer); |
| 1452 |
|
| 1453 |
/* Clear a transfer as if it has completed or cancelled, but do not |
| 1454 |
* report any completion/cancellation to the library. You should free |
| 1455 |
* all private data from the transfer as if you were just about to report |
| 1456 |
* completion or cancellation. |
| 1457 |
* |
| 1458 |
* This function might seem a bit out of place. It is used when libusb |
| 1459 |
* detects a disconnected device - it calls this function for all pending |
| 1460 |
* transfers before reporting completion (with the disconnect code) to |
| 1461 |
* the user. Maybe we can improve upon this internal interface in future. |
| 1462 |
*/ |
| 1463 |
void (*clear_transfer_priv)(struct usbi_transfer *itransfer); |
| 1464 |
|
| 1465 |
/* Handle any pending events on event sources. Optional. |
| 1466 |
* |
| 1467 |
* Provide this function when event sources directly indicate device |
| 1468 |
* or transfer activity. If your backend does not have such event sources, |
| 1469 |
* implement the handle_transfer_completion function below. |
| 1470 |
* |
| 1471 |
* This involves monitoring any active transfers and processing their |
| 1472 |
* completion or cancellation. |
| 1473 |
* |
| 1474 |
* The function is passed a pointer that represents platform-specific |
| 1475 |
* data for monitoring event sources (size count). This data is to be |
| 1476 |
* (re)allocated as necessary when event sources are modified. |
| 1477 |
* The num_ready parameter indicates the number of event sources that |
| 1478 |
* have reported events. This should be enough information for you to |
| 1479 |
* determine which actions need to be taken on the currently active |
| 1480 |
* transfers. |
| 1481 |
* |
| 1482 |
* For any cancelled transfers, call usbi_handle_transfer_cancellation(). |
| 1483 |
* For completed transfers, call usbi_handle_transfer_completion(). |
| 1484 |
* For control/bulk/interrupt transfers, populate the "transferred" |
| 1485 |
* element of the appropriate usbi_transfer structure before calling the |
| 1486 |
* above functions. For isochronous transfers, populate the status and |
| 1487 |
* transferred fields of the iso packet descriptors of the transfer. |
| 1488 |
* |
| 1489 |
* This function should also be able to detect disconnection of the |
| 1490 |
* device, reporting that situation with usbi_handle_disconnect(). |
| 1491 |
* |
| 1492 |
* When processing an event related to a transfer, you probably want to |
| 1493 |
* take usbi_transfer.lock to prevent races. See the documentation for |
| 1494 |
* the usbi_transfer structure. |
| 1495 |
* |
| 1496 |
* Return 0 on success, or a LIBUSB_ERROR code on failure. |
| 1497 |
*/ |
| 1498 |
int (*handle_events)(struct libusb_context *ctx, |
| 1499 |
void *event_data, unsigned int count, unsigned int num_ready); |
| 1500 |
|
| 1501 |
/* Handle transfer completion. Optional. |
| 1502 |
* |
| 1503 |
* Provide this function when there are no event sources available that |
| 1504 |
* directly indicate device or transfer activity. If your backend does |
| 1505 |
* have such event sources, implement the handle_events function above. |
| 1506 |
* |
| 1507 |
* Your backend must tell the library when a transfer has completed by |
| 1508 |
* calling usbi_signal_transfer_completion(). You should store any private |
| 1509 |
* information about the transfer and its completion status in the transfer's |
| 1510 |
* private backend data. |
| 1511 |
* |
| 1512 |
* During event handling, this function will be called on each transfer for |
| 1513 |
* which usbi_signal_transfer_completion() was called. |
| 1514 |
* |
| 1515 |
* For any cancelled transfers, call usbi_handle_transfer_cancellation(). |
| 1516 |
* For completed transfers, call usbi_handle_transfer_completion(). |
| 1517 |
* For control/bulk/interrupt transfers, populate the "transferred" |
| 1518 |
* element of the appropriate usbi_transfer structure before calling the |
| 1519 |
* above functions. For isochronous transfers, populate the status and |
| 1520 |
* transferred fields of the iso packet descriptors of the transfer. |
| 1521 |
* |
| 1522 |
* Return 0 on success, or a LIBUSB_ERROR code on failure. |
| 1523 |
*/ |
| 1524 |
int (*handle_transfer_completion)(struct usbi_transfer *itransfer); |
| 1525 |
|
| 1526 |
/* Number of bytes to reserve for per-context private backend data. |
| 1527 |
* This private data area is accessible by calling |
| 1528 |
* usbi_get_context_priv() on the libusb_context instance. |
| 1529 |
*/ |
| 1530 |
size_t context_priv_size; |
| 1531 |
|
| 1532 |
/* Number of bytes to reserve for per-device private backend data. |
| 1533 |
* This private data area is accessible by calling |
| 1534 |
* usbi_get_device_priv() on the libusb_device instance. |
| 1535 |
*/ |
| 1536 |
size_t device_priv_size; |
| 1537 |
|
| 1538 |
/* Number of bytes to reserve for per-handle private backend data. |
| 1539 |
* This private data area is accessible by calling |
| 1540 |
* usbi_get_device_handle_priv() on the libusb_device_handle instance. |
| 1541 |
*/ |
| 1542 |
size_t device_handle_priv_size; |
| 1543 |
|
| 1544 |
/* Number of bytes to reserve for per-transfer private backend data. |
| 1545 |
* This private data area is accessible by calling |
| 1546 |
* usbi_get_transfer_priv() on the usbi_transfer instance. |
| 1547 |
*/ |
| 1548 |
size_t transfer_priv_size; |
| 1549 |
}; |
| 1550 |
|
| 1551 |
extern const struct usbi_os_backend usbi_backend; |
| 1552 |
|
| 1553 |
#define for_each_context(c) \ |
| 1554 |
for_each_helper(c, &active_contexts_list, struct libusb_context) |
| 1555 |
|
| 1556 |
#define for_each_device(ctx, d) \ |
| 1557 |
for_each_helper(d, &(ctx)->usb_devs, struct libusb_device) |
| 1558 |
|
| 1559 |
#define for_each_device_safe(ctx, d, n) \ |
| 1560 |
for_each_safe_helper(d, n, &(ctx)->usb_devs, struct libusb_device) |
| 1561 |
|
| 1562 |
#define for_each_open_device(ctx, h) \ |
| 1563 |
for_each_helper(h, &(ctx)->open_devs, struct libusb_device_handle) |
| 1564 |
|
| 1565 |
#define __for_each_transfer(list, t) \ |
| 1566 |
for_each_helper(t, (list), struct usbi_transfer) |
| 1567 |
|
| 1568 |
#define for_each_transfer(ctx, t) \ |
| 1569 |
__for_each_transfer(&(ctx)->flying_transfers, t) |
| 1570 |
|
| 1571 |
#define __for_each_transfer_safe(list, t, n) \ |
| 1572 |
for_each_safe_helper(t, n, (list), struct usbi_transfer) |
| 1573 |
|
| 1574 |
#define for_each_transfer_safe(ctx, t, n) \ |
| 1575 |
__for_each_transfer_safe(&(ctx)->flying_transfers, t, n) |
| 1576 |
|
| 1577 |
#define __for_each_completed_transfer_safe(list, t, n) \ |
| 1578 |
list_for_each_entry_safe(t, n, (list), completed_list, struct usbi_transfer) |
| 1579 |
|
| 1580 |
#define for_each_event_source(ctx, e) \ |
| 1581 |
for_each_helper(e, &(ctx)->event_sources, struct usbi_event_source) |
| 1582 |
|
| 1583 |
#define for_each_removed_event_source(ctx, e) \ |
| 1584 |
for_each_helper(e, &(ctx)->removed_event_sources, struct usbi_event_source) |
| 1585 |
|
| 1586 |
#define for_each_removed_event_source_safe(ctx, e, n) \ |
| 1587 |
for_each_safe_helper(e, n, &(ctx)->removed_event_sources, struct usbi_event_source) |
| 1588 |
|
| 1589 |
#define for_each_hotplug_cb(ctx, c) \ |
| 1590 |
for_each_helper(c, &(ctx)->hotplug_cbs, struct usbi_hotplug_callback) |
| 1591 |
|
| 1592 |
#define for_each_hotplug_cb_safe(ctx, c, n) \ |
| 1593 |
for_each_safe_helper(c, n, &(ctx)->hotplug_cbs, struct usbi_hotplug_callback) |
| 1594 |
|
| 1595 |
#ifdef __cplusplus |
| 1596 |
} |
| 1597 |
#endif |
| 1598 |
|
| 1599 |
#endif |