libusbi.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  1. /*
  2. * Internal header for libusb
  3. * Copyright (C) 2007-2009 Daniel Drake <dsd@gentoo.org>
  4. * Copyright (c) 2001 Johannes Erdfelt <johannes@erdfelt.com>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #ifndef LIBUSBI_H
  21. #define LIBUSBI_H
  22. #include <config.h>
  23. #include <stddef.h>
  24. #include <stdint.h>
  25. #include <time.h>
  26. #include <stdarg.h>
  27. #ifdef HAVE_POLL_H
  28. #include <poll.h>
  29. #endif
  30. #include <libusb.h>
  31. #include <version.h>
  32. /* Inside the libusb code, mark all public functions as follows:
  33. * return_type API_EXPORTED function_name(params) { ... }
  34. * But if the function returns a pointer, mark it as follows:
  35. * DEFAULT_VISIBILITY return_type * LIBUSB_CALL function_name(params) { ... }
  36. * In the libusb public header, mark all declarations as:
  37. * return_type LIBUSB_CALL function_name(params);
  38. */
  39. #define API_EXPORTED LIBUSB_CALL DEFAULT_VISIBILITY
  40. #define DEVICE_DESC_LENGTH 18
  41. #define USB_MAXENDPOINTS 32
  42. #define USB_MAXINTERFACES 32
  43. #define USB_MAXCONFIG 8
  44. struct list_head {
  45. struct list_head *prev, *next;
  46. };
  47. /* Get an entry from the list
  48. * ptr - the address of this list_head element in "type"
  49. * type - the data type that contains "member"
  50. * member - the list_head element in "type"
  51. */
  52. #define list_entry(ptr, type, member) \
  53. ((type *)((uintptr_t)(ptr) - (uintptr_t)(&((type *)0L)->member)))
  54. /* Get each entry from a list
  55. * pos - A structure pointer has a "member" element
  56. * head - list head
  57. * member - the list_head element in "pos"
  58. * type - the type of the first parameter
  59. */
  60. #define list_for_each_entry(pos, head, member, type) \
  61. for (pos = list_entry((head)->next, type, member); \
  62. &pos->member != (head); \
  63. pos = list_entry(pos->member.next, type, member))
  64. #define list_for_each_entry_safe(pos, n, head, member, type) \
  65. for (pos = list_entry((head)->next, type, member), \
  66. n = list_entry(pos->member.next, type, member); \
  67. &pos->member != (head); \
  68. pos = n, n = list_entry(n->member.next, type, member))
  69. #define list_empty(entry) ((entry)->next == (entry))
  70. static inline void list_init(struct list_head *entry)
  71. {
  72. entry->prev = entry->next = entry;
  73. }
  74. static inline void list_add(struct list_head *entry, struct list_head *head)
  75. {
  76. entry->next = head->next;
  77. entry->prev = head;
  78. head->next->prev = entry;
  79. head->next = entry;
  80. }
  81. static inline void list_add_tail(struct list_head *entry,
  82. struct list_head *head)
  83. {
  84. entry->next = head;
  85. entry->prev = head->prev;
  86. head->prev->next = entry;
  87. head->prev = entry;
  88. }
  89. static inline void list_del(struct list_head *entry)
  90. {
  91. entry->next->prev = entry->prev;
  92. entry->prev->next = entry->next;
  93. }
  94. #define container_of(ptr, type, member) ({ \
  95. const typeof( ((type *)0)->member ) *mptr = (ptr); \
  96. (type *)( (char *)mptr - offsetof(type,member) );})
  97. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  98. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  99. #define TIMESPEC_IS_SET(ts) ((ts)->tv_sec != 0 || (ts)->tv_nsec != 0)
  100. enum usbi_log_level {
  101. LOG_LEVEL_DEBUG,
  102. LOG_LEVEL_INFO,
  103. LOG_LEVEL_WARNING,
  104. LOG_LEVEL_ERROR,
  105. };
  106. void usbi_log(struct libusb_context *ctx, enum usbi_log_level level,
  107. const char *function, const char *format, ...);
  108. void usbi_log_v(struct libusb_context *ctx, enum usbi_log_level level,
  109. const char *function, const char *format, va_list args);
  110. #if !defined(_MSC_VER) || _MSC_VER >= 1400
  111. #ifdef ENABLE_LOGGING
  112. #define _usbi_log(ctx, level, ...) usbi_log(ctx, level, __FUNCTION__, __VA_ARGS__)
  113. #else
  114. #define _usbi_log(ctx, level, ...) do { (void)(ctx); } while(0)
  115. #endif
  116. #ifdef ENABLE_DEBUG_LOGGING
  117. #define usbi_dbg(...) _usbi_log(NULL, LOG_LEVEL_DEBUG, __VA_ARGS__)
  118. #else
  119. #define usbi_dbg(...) do {} while(0)
  120. #endif
  121. #define usbi_info(ctx, ...) _usbi_log(ctx, LOG_LEVEL_INFO, __VA_ARGS__)
  122. #define usbi_warn(ctx, ...) _usbi_log(ctx, LOG_LEVEL_WARNING, __VA_ARGS__)
  123. #define usbi_err(ctx, ...) _usbi_log(ctx, LOG_LEVEL_ERROR, __VA_ARGS__)
  124. #else /* !defined(_MSC_VER) || _MSC_VER >= 1400 */
  125. /* Old MS compilers don't support variadic macros. The code is simple, so we
  126. * repeat it for each loglevel. Note that the debug case is special.
  127. *
  128. * Support for variadic macros was introduced in Visual C++ 2005.
  129. * http://msdn.microsoft.com/en-us/library/ms177415%28v=VS.80%29.aspx
  130. */
  131. static inline void usbi_info(struct libusb_context *ctx, const char *fmt, ...)
  132. {
  133. #ifdef ENABLE_LOGGING
  134. va_list args;
  135. va_start(args, fmt);
  136. usbi_log_v(ctx, LOG_LEVEL_INFO, "", fmt, args);
  137. va_end(args);
  138. #else
  139. (void)ctx;
  140. #endif
  141. }
  142. static inline void usbi_warn(struct libusb_context *ctx, const char *fmt, ...)
  143. {
  144. #ifdef ENABLE_LOGGING
  145. va_list args;
  146. va_start(args, fmt);
  147. usbi_log_v(ctx, LOG_LEVEL_WARNING, "", fmt, args);
  148. va_end(args);
  149. #else
  150. (void)ctx;
  151. #endif
  152. }
  153. static inline void usbi_err(struct libusb_context *ctx, const char *fmt, ...)
  154. {
  155. #ifdef ENABLE_LOGGING
  156. va_list args;
  157. va_start(args, fmt);
  158. usbi_log_v(ctx, LOG_LEVEL_ERROR, "", fmt, args);
  159. va_end(args);
  160. #else
  161. (void)ctx;
  162. #endif
  163. }
  164. static inline void usbi_dbg(const char *fmt, ...)
  165. {
  166. #ifdef ENABLE_DEBUG_LOGGING
  167. va_list args;
  168. va_start(args, fmt);
  169. usbi_log_v(NULL, LOG_LEVEL_DEBUG, "", fmt, args);
  170. va_end(args);
  171. #else
  172. (void)fmt;
  173. #endif
  174. }
  175. #endif /* !defined(_MSC_VER) || _MSC_VER >= 1400 */
  176. #define USBI_GET_CONTEXT(ctx) if (!(ctx)) (ctx) = usbi_default_context
  177. #define DEVICE_CTX(dev) ((dev)->ctx)
  178. #define HANDLE_CTX(handle) (DEVICE_CTX((handle)->dev))
  179. #define TRANSFER_CTX(transfer) (HANDLE_CTX((transfer)->dev_handle))
  180. #define ITRANSFER_CTX(transfer) \
  181. (TRANSFER_CTX(USBI_TRANSFER_TO_LIBUSB_TRANSFER(transfer)))
  182. #define IS_EPIN(ep) (0 != ((ep) & LIBUSB_ENDPOINT_IN))
  183. #define IS_EPOUT(ep) (!IS_EPIN(ep))
  184. #define IS_XFERIN(xfer) (0 != ((xfer)->endpoint & LIBUSB_ENDPOINT_IN))
  185. #define IS_XFEROUT(xfer) (!IS_XFERIN(xfer))
  186. /* Internal abstractions for thread synchronization and poll */
  187. #if defined(THREADS_POSIX)
  188. #include <os/threads_posix.h>
  189. #elif defined(OS_WINDOWS)
  190. #include <os/threads_windows.h>
  191. #endif
  192. #if defined(OS_LINUX) || defined(OS_DARWIN) || defined(OS_OPENBSD)
  193. #include <unistd.h>
  194. #include <os/poll_posix.h>
  195. #elif defined(OS_WINDOWS)
  196. #include <os/poll_windows.h>
  197. #endif
  198. #if defined(OS_WINDOWS) && !defined(__GCC__)
  199. #undef HAVE_GETTIMEOFDAY
  200. int usbi_gettimeofday(struct timeval *tp, void *tzp);
  201. #define LIBUSB_GETTIMEOFDAY_WIN32
  202. #define HAVE_USBI_GETTIMEOFDAY
  203. #else
  204. #ifdef HAVE_GETTIMEOFDAY
  205. #define usbi_gettimeofday(tv, tz) gettimeofday((tv), (tz))
  206. #define HAVE_USBI_GETTIMEOFDAY
  207. #endif
  208. #endif
  209. extern struct libusb_context *usbi_default_context;
  210. struct libusb_context {
  211. int debug;
  212. int debug_fixed;
  213. /* internal control pipe, used for interrupting event handling when
  214. * something needs to modify poll fds. */
  215. int ctrl_pipe[2];
  216. struct list_head usb_devs;
  217. usbi_mutex_t usb_devs_lock;
  218. /* A list of open handles. Backends are free to traverse this if required.
  219. */
  220. struct list_head open_devs;
  221. usbi_mutex_t open_devs_lock;
  222. /* A list of registered hotplug callbacks */
  223. struct list_head hotplug_cbs;
  224. usbi_mutex_t hotplug_cbs_lock;
  225. int hotplug_pipe[2];
  226. /* this is a list of in-flight transfer handles, sorted by timeout
  227. * expiration. URBs to timeout the soonest are placed at the beginning of
  228. * the list, URBs that will time out later are placed after, and urbs with
  229. * infinite timeout are always placed at the very end. */
  230. struct list_head flying_transfers;
  231. usbi_mutex_t flying_transfers_lock;
  232. /* list of poll fds */
  233. struct list_head pollfds;
  234. usbi_mutex_t pollfds_lock;
  235. /* a counter that is set when we want to interrupt event handling, in order
  236. * to modify the poll fd set. and a lock to protect it. */
  237. unsigned int pollfd_modify;
  238. usbi_mutex_t pollfd_modify_lock;
  239. /* user callbacks for pollfd changes */
  240. libusb_pollfd_added_cb fd_added_cb;
  241. libusb_pollfd_removed_cb fd_removed_cb;
  242. void *fd_cb_user_data;
  243. /* ensures that only one thread is handling events at any one time */
  244. usbi_mutex_t events_lock;
  245. /* used to see if there is an active thread doing event handling */
  246. int event_handler_active;
  247. /* used to wait for event completion in threads other than the one that is
  248. * event handling */
  249. usbi_mutex_t event_waiters_lock;
  250. usbi_cond_t event_waiters_cond;
  251. #ifdef USBI_TIMERFD_AVAILABLE
  252. /* used for timeout handling, if supported by OS.
  253. * this timerfd is maintained to trigger on the next pending timeout */
  254. int timerfd;
  255. #endif
  256. struct list_head list;
  257. };
  258. #ifdef USBI_TIMERFD_AVAILABLE
  259. #define usbi_using_timerfd(ctx) ((ctx)->timerfd >= 0)
  260. #else
  261. #define usbi_using_timerfd(ctx) (0)
  262. #endif
  263. struct libusb_device {
  264. /* lock protects refcnt, everything else is finalized at initialization
  265. * time */
  266. usbi_mutex_t lock;
  267. int refcnt;
  268. struct libusb_context *ctx;
  269. uint8_t bus_number;
  270. uint8_t device_address;
  271. uint8_t num_configurations;
  272. enum libusb_speed speed;
  273. struct list_head list;
  274. unsigned long session_data;
  275. struct libusb_device_descriptor device_descriptor;
  276. int attached;
  277. unsigned char os_priv
  278. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
  279. [] /* valid C99 code */
  280. #else
  281. [0] /* non-standard, but usually working code */
  282. #endif
  283. ;
  284. };
  285. struct libusb_device_handle {
  286. /* lock protects claimed_interfaces */
  287. usbi_mutex_t lock;
  288. unsigned long claimed_interfaces;
  289. struct list_head list;
  290. struct libusb_device *dev;
  291. unsigned char os_priv
  292. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
  293. [] /* valid C99 code */
  294. #else
  295. [0] /* non-standard, but usually working code */
  296. #endif
  297. ;
  298. };
  299. enum {
  300. USBI_CLOCK_MONOTONIC,
  301. USBI_CLOCK_REALTIME
  302. };
  303. /* in-memory transfer layout:
  304. *
  305. * 1. struct usbi_transfer
  306. * 2. struct libusb_transfer (which includes iso packets) [variable size]
  307. * 3. os private data [variable size]
  308. *
  309. * from a libusb_transfer, you can get the usbi_transfer by rewinding the
  310. * appropriate number of bytes.
  311. * the usbi_transfer includes the number of allocated packets, so you can
  312. * determine the size of the transfer and hence the start and length of the
  313. * OS-private data.
  314. */
  315. struct usbi_transfer {
  316. int num_iso_packets;
  317. struct list_head list;
  318. struct timeval timeout;
  319. int transferred;
  320. uint8_t flags;
  321. /* this lock is held during libusb_submit_transfer() and
  322. * libusb_cancel_transfer() (allowing the OS backend to prevent duplicate
  323. * cancellation, submission-during-cancellation, etc). the OS backend
  324. * should also take this lock in the handle_events path, to prevent the user
  325. * cancelling the transfer from another thread while you are processing
  326. * its completion (presumably there would be races within your OS backend
  327. * if this were possible). */
  328. usbi_mutex_t lock;
  329. };
  330. enum usbi_transfer_flags {
  331. /* The transfer has timed out */
  332. USBI_TRANSFER_TIMED_OUT = 1 << 0,
  333. /* Set by backend submit_transfer() if the OS handles timeout */
  334. USBI_TRANSFER_OS_HANDLES_TIMEOUT = 1 << 1,
  335. /* Cancellation was requested via libusb_cancel_transfer() */
  336. USBI_TRANSFER_CANCELLING = 1 << 2,
  337. /* Operation on the transfer failed because the device disappeared */
  338. USBI_TRANSFER_DEVICE_DISAPPEARED = 1 << 3,
  339. /* Set by backend submit_transfer() if the fds in use were updated */
  340. USBI_TRANSFER_UPDATED_FDS = 1 << 4,
  341. };
  342. #define USBI_TRANSFER_TO_LIBUSB_TRANSFER(transfer) \
  343. ((struct libusb_transfer *)(((unsigned char *)(transfer)) \
  344. + sizeof(struct usbi_transfer)))
  345. #define LIBUSB_TRANSFER_TO_USBI_TRANSFER(transfer) \
  346. ((struct usbi_transfer *)(((unsigned char *)(transfer)) \
  347. - sizeof(struct usbi_transfer)))
  348. static inline void *usbi_transfer_get_os_priv(struct usbi_transfer *transfer)
  349. {
  350. return ((unsigned char *)transfer) + sizeof(struct usbi_transfer)
  351. + sizeof(struct libusb_transfer)
  352. + (transfer->num_iso_packets
  353. * sizeof(struct libusb_iso_packet_descriptor));
  354. }
  355. /* bus structures */
  356. /* All standard descriptors have these 2 fields in common */
  357. struct usb_descriptor_header {
  358. uint8_t bLength;
  359. uint8_t bDescriptorType;
  360. };
  361. /* shared data and functions */
  362. int usbi_io_init(struct libusb_context *ctx);
  363. void usbi_io_exit(struct libusb_context *ctx);
  364. struct libusb_device *usbi_alloc_device(struct libusb_context *ctx,
  365. unsigned long session_id);
  366. struct libusb_device *usbi_get_device_by_session_id(struct libusb_context *ctx,
  367. unsigned long session_id);
  368. int usbi_sanitize_device(struct libusb_device *dev);
  369. void usbi_handle_disconnect(struct libusb_device_handle *handle);
  370. int usbi_handle_transfer_completion(struct usbi_transfer *itransfer,
  371. enum libusb_transfer_status status);
  372. int usbi_handle_transfer_cancellation(struct usbi_transfer *transfer);
  373. int usbi_parse_descriptor(const unsigned char *source, const char *descriptor,
  374. void *dest, int host_endian);
  375. int usbi_device_cache_descriptor(libusb_device *dev);
  376. int usbi_get_config_index_by_value(struct libusb_device *dev,
  377. uint8_t bConfigurationValue, int *idx);
  378. void usbi_connect_device (struct libusb_device *dev);
  379. void usbi_disconnect_device (struct libusb_device *dev);
  380. /* polling */
  381. struct usbi_pollfd {
  382. /* must come first */
  383. struct libusb_pollfd pollfd;
  384. struct list_head list;
  385. };
  386. int usbi_add_pollfd(struct libusb_context *ctx, int fd, short events);
  387. void usbi_remove_pollfd(struct libusb_context *ctx, int fd);
  388. void usbi_fd_notification(struct libusb_context *ctx);
  389. /* device discovery */
  390. /* we traverse usbfs without knowing how many devices we are going to find.
  391. * so we create this discovered_devs model which is similar to a linked-list
  392. * which grows when required. it can be freed once discovery has completed,
  393. * eliminating the need for a list node in the libusb_device structure
  394. * itself. */
  395. struct discovered_devs {
  396. size_t len;
  397. size_t capacity;
  398. struct libusb_device *devices
  399. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
  400. [] /* valid C99 code */
  401. #else
  402. [0] /* non-standard, but usually working code */
  403. #endif
  404. ;
  405. };
  406. struct discovered_devs *discovered_devs_append(
  407. struct discovered_devs *discdevs, struct libusb_device *dev);
  408. /* OS abstraction */
  409. /* This is the interface that OS backends need to implement.
  410. * All fields are mandatory, except ones explicitly noted as optional. */
  411. struct usbi_os_backend {
  412. /* A human-readable name for your backend, e.g. "Linux usbfs" */
  413. const char *name;
  414. /* Perform initialization of your backend. You might use this function
  415. * to determine specific capabilities of the system, allocate required
  416. * data structures for later, etc.
  417. *
  418. * This function is called when a libusb user initializes the library
  419. * prior to use.
  420. *
  421. * Return 0 on success, or a LIBUSB_ERROR code on failure.
  422. */
  423. int (*init)(struct libusb_context *ctx);
  424. /* Deinitialization. Optional. This function should destroy anything
  425. * that was set up by init.
  426. *
  427. * This function is called when the user deinitializes the library.
  428. */
  429. void (*exit)(void);
  430. /* Enumerate all the USB devices on the system, returning them in a list
  431. * of discovered devices.
  432. *
  433. * Your implementation should enumerate all devices on the system,
  434. * regardless of whether they have been seen before or not.
  435. *
  436. * When you have found a device, compute a session ID for it. The session
  437. * ID should uniquely represent that particular device for that particular
  438. * connection session since boot (i.e. if you disconnect and reconnect a
  439. * device immediately after, it should be assigned a different session ID).
  440. * If your OS cannot provide a unique session ID as described above,
  441. * presenting a session ID of (bus_number << 8 | device_address) should
  442. * be sufficient. Bus numbers and device addresses wrap and get reused,
  443. * but that is an unlikely case.
  444. *
  445. * After computing a session ID for a device, call
  446. * usbi_get_device_by_session_id(). This function checks if libusb already
  447. * knows about the device, and if so, it provides you with a libusb_device
  448. * structure for it.
  449. *
  450. * If usbi_get_device_by_session_id() returns NULL, it is time to allocate
  451. * a new device structure for the device. Call usbi_alloc_device() to
  452. * obtain a new libusb_device structure with reference count 1. Populate
  453. * the bus_number and device_address attributes of the new device, and
  454. * perform any other internal backend initialization you need to do. At
  455. * this point, you should be ready to provide device descriptors and so
  456. * on through the get_*_descriptor functions. Finally, call
  457. * usbi_sanitize_device() to perform some final sanity checks on the
  458. * device. Assuming all of the above succeeded, we can now continue.
  459. * If any of the above failed, remember to unreference the device that
  460. * was returned by usbi_alloc_device().
  461. *
  462. * At this stage we have a populated libusb_device structure (either one
  463. * that was found earlier, or one that we have just allocated and
  464. * populated). This can now be added to the discovered devices list
  465. * using discovered_devs_append(). Note that discovered_devs_append()
  466. * may reallocate the list, returning a new location for it, and also
  467. * note that reallocation can fail. Your backend should handle these
  468. * error conditions appropriately.
  469. *
  470. * This function should not generate any bus I/O and should not block.
  471. * If I/O is required (e.g. reading the active configuration value), it is
  472. * OK to ignore these suggestions :)
  473. *
  474. * This function is executed when the user wishes to retrieve a list
  475. * of USB devices connected to the system.
  476. *
  477. * Return 0 on success, or a LIBUSB_ERROR code on failure.
  478. */
  479. int (*get_device_list)(struct libusb_context *ctx,
  480. struct discovered_devs **discdevs);
  481. /* Open a device for I/O and other USB operations. The device handle
  482. * is preallocated for you, you can retrieve the device in question
  483. * through handle->dev.
  484. *
  485. * Your backend should allocate any internal resources required for I/O
  486. * and other operations so that those operations can happen (hopefully)
  487. * without hiccup. This is also a good place to inform libusb that it
  488. * should monitor certain file descriptors related to this device -
  489. * see the usbi_add_pollfd() function.
  490. *
  491. * This function should not generate any bus I/O and should not block.
  492. *
  493. * This function is called when the user attempts to obtain a device
  494. * handle for a device.
  495. *
  496. * Return:
  497. * - 0 on success
  498. * - LIBUSB_ERROR_ACCESS if the user has insufficient permissions
  499. * - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since
  500. * discovery
  501. * - another LIBUSB_ERROR code on other failure
  502. *
  503. * Do not worry about freeing the handle on failed open, the upper layers
  504. * do this for you.
  505. */
  506. int (*open)(struct libusb_device_handle *handle);
  507. /* Close a device such that the handle cannot be used again. Your backend
  508. * should destroy any resources that were allocated in the open path.
  509. * This may also be a good place to call usbi_remove_pollfd() to inform
  510. * libusb of any file descriptors associated with this device that should
  511. * no longer be monitored.
  512. *
  513. * This function is called when the user closes a device handle.
  514. */
  515. void (*close)(struct libusb_device_handle *handle);
  516. /* Retrieve the device descriptor from a device.
  517. *
  518. * The descriptor should be retrieved from memory, NOT via bus I/O to the
  519. * device. This means that you may have to cache it in a private structure
  520. * during get_device_list enumeration. Alternatively, you may be able
  521. * to retrieve it from a kernel interface (some Linux setups can do this)
  522. * still without generating bus I/O.
  523. *
  524. * This function is expected to write DEVICE_DESC_LENGTH (18) bytes into
  525. * buffer, which is guaranteed to be big enough.
  526. *
  527. * This function is called when sanity-checking a device before adding
  528. * it to the list of discovered devices, and also when the user requests
  529. * to read the device descriptor.
  530. *
  531. * This function is expected to return the descriptor in bus-endian format
  532. * (LE). If it returns the multi-byte values in host-endian format,
  533. * set the host_endian output parameter to "1".
  534. *
  535. * Return 0 on success or a LIBUSB_ERROR code on failure.
  536. */
  537. int (*get_device_descriptor)(struct libusb_device *device,
  538. unsigned char *buffer, int *host_endian);
  539. /* Get the ACTIVE configuration descriptor for a device.
  540. *
  541. * The descriptor should be retrieved from memory, NOT via bus I/O to the
  542. * device. This means that you may have to cache it in a private structure
  543. * during get_device_list enumeration. You may also have to keep track
  544. * of which configuration is active when the user changes it.
  545. *
  546. * This function is expected to write len bytes of data into buffer, which
  547. * is guaranteed to be big enough. If you can only do a partial write,
  548. * return an error code.
  549. *
  550. * This function is expected to return the descriptor in bus-endian format
  551. * (LE). If it returns the multi-byte values in host-endian format,
  552. * set the host_endian output parameter to "1".
  553. *
  554. * Return:
  555. * - 0 on success
  556. * - LIBUSB_ERROR_NOT_FOUND if the device is in unconfigured state
  557. * - another LIBUSB_ERROR code on other failure
  558. */
  559. int (*get_active_config_descriptor)(struct libusb_device *device,
  560. unsigned char *buffer, size_t len, int *host_endian);
  561. /* Get a specific configuration descriptor for a device.
  562. *
  563. * The descriptor should be retrieved from memory, NOT via bus I/O to the
  564. * device. This means that you may have to cache it in a private structure
  565. * during get_device_list enumeration.
  566. *
  567. * The requested descriptor is expressed as a zero-based index (i.e. 0
  568. * indicates that we are requesting the first descriptor). The index does
  569. * not (necessarily) equal the bConfigurationValue of the configuration
  570. * being requested.
  571. *
  572. * This function is expected to write len bytes of data into buffer, which
  573. * is guaranteed to be big enough. If you can only do a partial write,
  574. * return an error code.
  575. *
  576. * This function is expected to return the descriptor in bus-endian format
  577. * (LE). If it returns the multi-byte values in host-endian format,
  578. * set the host_endian output parameter to "1".
  579. *
  580. * Return 0 on success or a LIBUSB_ERROR code on failure.
  581. */
  582. int (*get_config_descriptor)(struct libusb_device *device,
  583. uint8_t config_index, unsigned char *buffer, size_t len,
  584. int *host_endian);
  585. /* Get the bConfigurationValue for the active configuration for a device.
  586. * Optional. This should only be implemented if you can retrieve it from
  587. * cache (don't generate I/O).
  588. *
  589. * If you cannot retrieve this from cache, either do not implement this
  590. * function, or return LIBUSB_ERROR_NOT_SUPPORTED. This will cause
  591. * libusb to retrieve the information through a standard control transfer.
  592. *
  593. * This function must be non-blocking.
  594. * Return:
  595. * - 0 on success
  596. * - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it
  597. * was opened
  598. * - LIBUSB_ERROR_NOT_SUPPORTED if the value cannot be retrieved without
  599. * blocking
  600. * - another LIBUSB_ERROR code on other failure.
  601. */
  602. int (*get_configuration)(struct libusb_device_handle *handle, int *config);
  603. /* Set the active configuration for a device.
  604. *
  605. * A configuration value of -1 should put the device in unconfigured state.
  606. *
  607. * This function can block.
  608. *
  609. * Return:
  610. * - 0 on success
  611. * - LIBUSB_ERROR_NOT_FOUND if the configuration does not exist
  612. * - LIBUSB_ERROR_BUSY if interfaces are currently claimed (and hence
  613. * configuration cannot be changed)
  614. * - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it
  615. * was opened
  616. * - another LIBUSB_ERROR code on other failure.
  617. */
  618. int (*set_configuration)(struct libusb_device_handle *handle, int config);
  619. /* Claim an interface. When claimed, the application can then perform
  620. * I/O to an interface's endpoints.
  621. *
  622. * This function should not generate any bus I/O and should not block.
  623. * Interface claiming is a logical operation that simply ensures that
  624. * no other drivers/applications are using the interface, and after
  625. * claiming, no other drivers/applicatiosn can use the interface because
  626. * we now "own" it.
  627. *
  628. * Return:
  629. * - 0 on success
  630. * - LIBUSB_ERROR_NOT_FOUND if the interface does not exist
  631. * - LIBUSB_ERROR_BUSY if the interface is in use by another driver/app
  632. * - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it
  633. * was opened
  634. * - another LIBUSB_ERROR code on other failure
  635. */
  636. int (*claim_interface)(struct libusb_device_handle *handle, int interface_number);
  637. /* Release a previously claimed interface.
  638. *
  639. * This function should also generate a SET_INTERFACE control request,
  640. * resetting the alternate setting of that interface to 0. It's OK for
  641. * this function to block as a result.
  642. *
  643. * You will only ever be asked to release an interface which was
  644. * successfully claimed earlier.
  645. *
  646. * Return:
  647. * - 0 on success
  648. * - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it
  649. * was opened
  650. * - another LIBUSB_ERROR code on other failure
  651. */
  652. int (*release_interface)(struct libusb_device_handle *handle, int interface_number);
  653. /* Set the alternate setting for an interface.
  654. *
  655. * You will only ever be asked to set the alternate setting for an
  656. * interface which was successfully claimed earlier.
  657. *
  658. * It's OK for this function to block.
  659. *
  660. * Return:
  661. * - 0 on success
  662. * - LIBUSB_ERROR_NOT_FOUND if the alternate setting does not exist
  663. * - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it
  664. * was opened
  665. * - another LIBUSB_ERROR code on other failure
  666. */
  667. int (*set_interface_altsetting)(struct libusb_device_handle *handle,
  668. int interface_number, int altsetting);
  669. /* Clear a halt/stall condition on an endpoint.
  670. *
  671. * It's OK for this function to block.
  672. *
  673. * Return:
  674. * - 0 on success
  675. * - LIBUSB_ERROR_NOT_FOUND if the endpoint does not exist
  676. * - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it
  677. * was opened
  678. * - another LIBUSB_ERROR code on other failure
  679. */
  680. int (*clear_halt)(struct libusb_device_handle *handle,
  681. unsigned char endpoint);
  682. /* Perform a USB port reset to reinitialize a device.
  683. *
  684. * If possible, the handle should still be usable after the reset
  685. * completes, assuming that the device descriptors did not change during
  686. * reset and all previous interface state can be restored.
  687. *
  688. * If something changes, or you cannot easily locate/verify the resetted
  689. * device, return LIBUSB_ERROR_NOT_FOUND. This prompts the application
  690. * to close the old handle and re-enumerate the device.
  691. *
  692. * Return:
  693. * - 0 on success
  694. * - LIBUSB_ERROR_NOT_FOUND if re-enumeration is required, or if the device
  695. * has been disconnected since it was opened
  696. * - another LIBUSB_ERROR code on other failure
  697. */
  698. int (*reset_device)(struct libusb_device_handle *handle);
  699. /* Determine if a kernel driver is active on an interface. Optional.
  700. *
  701. * The presence of a kernel driver on an interface indicates that any
  702. * calls to claim_interface would fail with the LIBUSB_ERROR_BUSY code.
  703. *
  704. * Return:
  705. * - 0 if no driver is active
  706. * - 1 if a driver is active
  707. * - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it
  708. * was opened
  709. * - another LIBUSB_ERROR code on other failure
  710. */
  711. int (*kernel_driver_active)(struct libusb_device_handle *handle,
  712. int interface_number);
  713. /* Detach a kernel driver from an interface. Optional.
  714. *
  715. * After detaching a kernel driver, the interface should be available
  716. * for claim.
  717. *
  718. * Return:
  719. * - 0 on success
  720. * - LIBUSB_ERROR_NOT_FOUND if no kernel driver was active
  721. * - LIBUSB_ERROR_INVALID_PARAM if the interface does not exist
  722. * - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it
  723. * was opened
  724. * - another LIBUSB_ERROR code on other failure
  725. */
  726. int (*detach_kernel_driver)(struct libusb_device_handle *handle,
  727. int interface_number);
  728. /* Attach a kernel driver to an interface. Optional.
  729. *
  730. * Reattach a kernel driver to the device.
  731. *
  732. * Return:
  733. * - 0 on success
  734. * - LIBUSB_ERROR_NOT_FOUND if no kernel driver was active
  735. * - LIBUSB_ERROR_INVALID_PARAM if the interface does not exist
  736. * - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it
  737. * was opened
  738. * - LIBUSB_ERROR_BUSY if a program or driver has claimed the interface,
  739. * preventing reattachment
  740. * - another LIBUSB_ERROR code on other failure
  741. */
  742. int (*attach_kernel_driver)(struct libusb_device_handle *handle,
  743. int interface_number);
  744. /* Destroy a device. Optional.
  745. *
  746. * This function is called when the last reference to a device is
  747. * destroyed. It should free any resources allocated in the get_device_list
  748. * path.
  749. */
  750. void (*destroy_device)(struct libusb_device *dev);
  751. /* Submit a transfer. Your implementation should take the transfer,
  752. * morph it into whatever form your platform requires, and submit it
  753. * asynchronously.
  754. *
  755. * This function must not block.
  756. *
  757. * Return:
  758. * - 0 on success
  759. * - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
  760. * - another LIBUSB_ERROR code on other failure
  761. */
  762. int (*submit_transfer)(struct usbi_transfer *itransfer);
  763. /* Cancel a previously submitted transfer.
  764. *
  765. * This function must not block. The transfer cancellation must complete
  766. * later, resulting in a call to usbi_handle_transfer_cancellation()
  767. * from the context of handle_events.
  768. */
  769. int (*cancel_transfer)(struct usbi_transfer *itransfer);
  770. /* Clear a transfer as if it has completed or cancelled, but do not
  771. * report any completion/cancellation to the library. You should free
  772. * all private data from the transfer as if you were just about to report
  773. * completion or cancellation.
  774. *
  775. * This function might seem a bit out of place. It is used when libusb
  776. * detects a disconnected device - it calls this function for all pending
  777. * transfers before reporting completion (with the disconnect code) to
  778. * the user. Maybe we can improve upon this internal interface in future.
  779. */
  780. void (*clear_transfer_priv)(struct usbi_transfer *itransfer);
  781. /* Handle any pending events. This involves monitoring any active
  782. * transfers and processing their completion or cancellation.
  783. *
  784. * The function is passed an array of pollfd structures (size nfds)
  785. * as a result of the poll() system call. The num_ready parameter
  786. * indicates the number of file descriptors that have reported events
  787. * (i.e. the poll() return value). This should be enough information
  788. * for you to determine which actions need to be taken on the currently
  789. * active transfers.
  790. *
  791. * For any cancelled transfers, call usbi_handle_transfer_cancellation().
  792. * For completed transfers, call usbi_handle_transfer_completion().
  793. * For control/bulk/interrupt transfers, populate the "transferred"
  794. * element of the appropriate usbi_transfer structure before calling the
  795. * above functions. For isochronous transfers, populate the status and
  796. * transferred fields of the iso packet descriptors of the transfer.
  797. *
  798. * This function should also be able to detect disconnection of the
  799. * device, reporting that situation with usbi_handle_disconnect().
  800. *
  801. * When processing an event related to a transfer, you probably want to
  802. * take usbi_transfer.lock to prevent races. See the documentation for
  803. * the usbi_transfer structure.
  804. *
  805. * Return 0 on success, or a LIBUSB_ERROR code on failure.
  806. */
  807. int (*handle_events)(struct libusb_context *ctx,
  808. struct pollfd *fds, POLL_NFDS_TYPE nfds, int num_ready);
  809. /* Get time from specified clock. At least two clocks must be implemented
  810. by the backend: USBI_CLOCK_REALTIME, and USBI_CLOCK_MONOTONIC.
  811. Description of clocks:
  812. USBI_CLOCK_REALTIME : clock returns time since system epoch.
  813. USBI_CLOCK_MONOTONIC: clock returns time since unspecified start
  814. time (usually boot).
  815. */
  816. int (*clock_gettime)(int clkid, struct timespec *tp);
  817. #ifdef USBI_TIMERFD_AVAILABLE
  818. /* clock ID of the clock that should be used for timerfd */
  819. clockid_t (*get_timerfd_clockid)(void);
  820. #endif
  821. /* Number of bytes to reserve for per-device private backend data.
  822. * This private data area is accessible through the "os_priv" field of
  823. * struct libusb_device. */
  824. size_t device_priv_size;
  825. /* Number of bytes to reserve for per-handle private backend data.
  826. * This private data area is accessible through the "os_priv" field of
  827. * struct libusb_device. */
  828. size_t device_handle_priv_size;
  829. /* Number of bytes to reserve for per-transfer private backend data.
  830. * This private data area is accessible by calling
  831. * usbi_transfer_get_os_priv() on the appropriate usbi_transfer instance.
  832. */
  833. size_t transfer_priv_size;
  834. /* Mumber of additional bytes for os_priv for each iso packet.
  835. * Can your backend use this? */
  836. /* FIXME: linux can't use this any more. if other OS's cannot either,
  837. * then remove this */
  838. size_t add_iso_packet_size;
  839. };
  840. extern const struct usbi_os_backend * const usbi_backend;
  841. extern const struct usbi_os_backend linux_usbfs_backend;
  842. extern const struct usbi_os_backend darwin_backend;
  843. extern const struct usbi_os_backend openbsd_backend;
  844. extern const struct usbi_os_backend windows_backend;
  845. extern struct list_head active_contexts_list;
  846. extern usbi_mutex_static_t active_contexts_lock;
  847. #endif