123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974 |
- #ifndef LIBUSBI_H
- #define LIBUSBI_H
- #include <config.h>
- #include <stddef.h>
- #include <stdint.h>
- #include <time.h>
- #include <stdarg.h>
- #ifdef HAVE_POLL_H
- #include <poll.h>
- #endif
- #include <libusb.h>
- #include <version.h>
- #define API_EXPORTED LIBUSB_CALL DEFAULT_VISIBILITY
- #define DEVICE_DESC_LENGTH 18
- #define USB_MAXENDPOINTS 32
- #define USB_MAXINTERFACES 32
- #define USB_MAXCONFIG 8
- struct list_head {
- struct list_head *prev, *next;
- };
- #define list_entry(ptr, type, member) \
- ((type *)((uintptr_t)(ptr) - (uintptr_t)(&((type *)0L)->member)))
- #define list_for_each_entry(pos, head, member, type) \
- for (pos = list_entry((head)->next, type, member); \
- &pos->member != (head); \
- pos = list_entry(pos->member.next, type, member))
- #define list_for_each_entry_safe(pos, n, head, member, type) \
- for (pos = list_entry((head)->next, type, member), \
- n = list_entry(pos->member.next, type, member); \
- &pos->member != (head); \
- pos = n, n = list_entry(n->member.next, type, member))
- #define list_empty(entry) ((entry)->next == (entry))
- static inline void list_init(struct list_head *entry)
- {
- entry->prev = entry->next = entry;
- }
- static inline void list_add(struct list_head *entry, struct list_head *head)
- {
- entry->next = head->next;
- entry->prev = head;
- head->next->prev = entry;
- head->next = entry;
- }
- static inline void list_add_tail(struct list_head *entry,
- struct list_head *head)
- {
- entry->next = head;
- entry->prev = head->prev;
- head->prev->next = entry;
- head->prev = entry;
- }
- static inline void list_del(struct list_head *entry)
- {
- entry->next->prev = entry->prev;
- entry->prev->next = entry->next;
- }
- #define container_of(ptr, type, member) ({ \
- const typeof( ((type *)0)->member ) *mptr = (ptr); \
- (type *)( (char *)mptr - offsetof(type,member) );})
- #define MIN(a, b) ((a) < (b) ? (a) : (b))
- #define MAX(a, b) ((a) > (b) ? (a) : (b))
- #define TIMESPEC_IS_SET(ts) ((ts)->tv_sec != 0 || (ts)->tv_nsec != 0)
- enum usbi_log_level {
- LOG_LEVEL_DEBUG,
- LOG_LEVEL_INFO,
- LOG_LEVEL_WARNING,
- LOG_LEVEL_ERROR,
- };
- void usbi_log(struct libusb_context *ctx, enum usbi_log_level level,
- const char *function, const char *format, ...);
- void usbi_log_v(struct libusb_context *ctx, enum usbi_log_level level,
- const char *function, const char *format, va_list args);
- #if !defined(_MSC_VER) || _MSC_VER >= 1400
- #ifdef ENABLE_LOGGING
- #define _usbi_log(ctx, level, ...) usbi_log(ctx, level, __FUNCTION__, __VA_ARGS__)
- #else
- #define _usbi_log(ctx, level, ...) do { (void)(ctx); } while(0)
- #endif
- #ifdef ENABLE_DEBUG_LOGGING
- #define usbi_dbg(...) _usbi_log(NULL, LOG_LEVEL_DEBUG, __VA_ARGS__)
- #else
- #define usbi_dbg(...) do {} while(0)
- #endif
- #define usbi_info(ctx, ...) _usbi_log(ctx, LOG_LEVEL_INFO, __VA_ARGS__)
- #define usbi_warn(ctx, ...) _usbi_log(ctx, LOG_LEVEL_WARNING, __VA_ARGS__)
- #define usbi_err(ctx, ...) _usbi_log(ctx, LOG_LEVEL_ERROR, __VA_ARGS__)
- #else
- static inline void usbi_info(struct libusb_context *ctx, const char *fmt, ...)
- {
- #ifdef ENABLE_LOGGING
- va_list args;
- va_start(args, fmt);
- usbi_log_v(ctx, LOG_LEVEL_INFO, "", fmt, args);
- va_end(args);
- #else
- (void)ctx;
- #endif
- }
- static inline void usbi_warn(struct libusb_context *ctx, const char *fmt, ...)
- {
- #ifdef ENABLE_LOGGING
- va_list args;
- va_start(args, fmt);
- usbi_log_v(ctx, LOG_LEVEL_WARNING, "", fmt, args);
- va_end(args);
- #else
- (void)ctx;
- #endif
- }
- static inline void usbi_err(struct libusb_context *ctx, const char *fmt, ...)
- {
- #ifdef ENABLE_LOGGING
- va_list args;
- va_start(args, fmt);
- usbi_log_v(ctx, LOG_LEVEL_ERROR, "", fmt, args);
- va_end(args);
- #else
- (void)ctx;
- #endif
- }
- static inline void usbi_dbg(const char *fmt, ...)
- {
- #ifdef ENABLE_DEBUG_LOGGING
- va_list args;
- va_start(args, fmt);
- usbi_log_v(NULL, LOG_LEVEL_DEBUG, "", fmt, args);
- va_end(args);
- #else
- (void)fmt;
- #endif
- }
- #endif
- #define USBI_GET_CONTEXT(ctx) if (!(ctx)) (ctx) = usbi_default_context
- #define DEVICE_CTX(dev) ((dev)->ctx)
- #define HANDLE_CTX(handle) (DEVICE_CTX((handle)->dev))
- #define TRANSFER_CTX(transfer) (HANDLE_CTX((transfer)->dev_handle))
- #define ITRANSFER_CTX(transfer) \
- (TRANSFER_CTX(USBI_TRANSFER_TO_LIBUSB_TRANSFER(transfer)))
- #define IS_EPIN(ep) (0 != ((ep) & LIBUSB_ENDPOINT_IN))
- #define IS_EPOUT(ep) (!IS_EPIN(ep))
- #define IS_XFERIN(xfer) (0 != ((xfer)->endpoint & LIBUSB_ENDPOINT_IN))
- #define IS_XFEROUT(xfer) (!IS_XFERIN(xfer))
- #if defined(THREADS_POSIX)
- #include <os/threads_posix.h>
- #elif defined(OS_WINDOWS)
- #include <os/threads_windows.h>
- #endif
- #if defined(OS_LINUX) || defined(OS_DARWIN) || defined(OS_OPENBSD)
- #include <unistd.h>
- #include <os/poll_posix.h>
- #elif defined(OS_WINDOWS)
- #include <os/poll_windows.h>
- #endif
- #if defined(OS_WINDOWS) && !defined(__GCC__)
- #undef HAVE_GETTIMEOFDAY
- int usbi_gettimeofday(struct timeval *tp, void *tzp);
- #define LIBUSB_GETTIMEOFDAY_WIN32
- #define HAVE_USBI_GETTIMEOFDAY
- #else
- #ifdef HAVE_GETTIMEOFDAY
- #define usbi_gettimeofday(tv, tz) gettimeofday((tv), (tz))
- #define HAVE_USBI_GETTIMEOFDAY
- #endif
- #endif
- extern struct libusb_context *usbi_default_context;
- struct libusb_context {
- int debug;
- int debug_fixed;
-
- int ctrl_pipe[2];
- struct list_head usb_devs;
- usbi_mutex_t usb_devs_lock;
-
- struct list_head open_devs;
- usbi_mutex_t open_devs_lock;
-
- struct list_head hotplug_cbs;
- usbi_mutex_t hotplug_cbs_lock;
- int hotplug_pipe[2];
-
- struct list_head flying_transfers;
- usbi_mutex_t flying_transfers_lock;
-
- struct list_head pollfds;
- usbi_mutex_t pollfds_lock;
-
- unsigned int pollfd_modify;
- usbi_mutex_t pollfd_modify_lock;
-
- libusb_pollfd_added_cb fd_added_cb;
- libusb_pollfd_removed_cb fd_removed_cb;
- void *fd_cb_user_data;
-
- usbi_mutex_t events_lock;
-
- int event_handler_active;
-
- usbi_mutex_t event_waiters_lock;
- usbi_cond_t event_waiters_cond;
- #ifdef USBI_TIMERFD_AVAILABLE
-
- int timerfd;
- #endif
- struct list_head list;
- };
- #ifdef USBI_TIMERFD_AVAILABLE
- #define usbi_using_timerfd(ctx) ((ctx)->timerfd >= 0)
- #else
- #define usbi_using_timerfd(ctx) (0)
- #endif
- struct libusb_device {
-
- usbi_mutex_t lock;
- int refcnt;
- struct libusb_context *ctx;
- uint8_t bus_number;
- uint8_t device_address;
- uint8_t num_configurations;
- enum libusb_speed speed;
- struct list_head list;
- unsigned long session_data;
- struct libusb_device_descriptor device_descriptor;
- int attached;
- unsigned char os_priv
- #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
- []
- #else
- [0]
- #endif
- ;
- };
- struct libusb_device_handle {
-
- usbi_mutex_t lock;
- unsigned long claimed_interfaces;
- struct list_head list;
- struct libusb_device *dev;
- unsigned char os_priv
- #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
- []
- #else
- [0]
- #endif
- ;
- };
- enum {
- USBI_CLOCK_MONOTONIC,
- USBI_CLOCK_REALTIME
- };
- struct usbi_transfer {
- int num_iso_packets;
- struct list_head list;
- struct timeval timeout;
- int transferred;
- uint8_t flags;
-
- usbi_mutex_t lock;
- };
- enum usbi_transfer_flags {
-
- USBI_TRANSFER_TIMED_OUT = 1 << 0,
-
- USBI_TRANSFER_OS_HANDLES_TIMEOUT = 1 << 1,
-
- USBI_TRANSFER_CANCELLING = 1 << 2,
-
- USBI_TRANSFER_DEVICE_DISAPPEARED = 1 << 3,
-
- USBI_TRANSFER_UPDATED_FDS = 1 << 4,
- };
- #define USBI_TRANSFER_TO_LIBUSB_TRANSFER(transfer) \
- ((struct libusb_transfer *)(((unsigned char *)(transfer)) \
- + sizeof(struct usbi_transfer)))
- #define LIBUSB_TRANSFER_TO_USBI_TRANSFER(transfer) \
- ((struct usbi_transfer *)(((unsigned char *)(transfer)) \
- - sizeof(struct usbi_transfer)))
- static inline void *usbi_transfer_get_os_priv(struct usbi_transfer *transfer)
- {
- return ((unsigned char *)transfer) + sizeof(struct usbi_transfer)
- + sizeof(struct libusb_transfer)
- + (transfer->num_iso_packets
- * sizeof(struct libusb_iso_packet_descriptor));
- }
- struct usb_descriptor_header {
- uint8_t bLength;
- uint8_t bDescriptorType;
- };
- int usbi_io_init(struct libusb_context *ctx);
- void usbi_io_exit(struct libusb_context *ctx);
- struct libusb_device *usbi_alloc_device(struct libusb_context *ctx,
- unsigned long session_id);
- struct libusb_device *usbi_get_device_by_session_id(struct libusb_context *ctx,
- unsigned long session_id);
- int usbi_sanitize_device(struct libusb_device *dev);
- void usbi_handle_disconnect(struct libusb_device_handle *handle);
- int usbi_handle_transfer_completion(struct usbi_transfer *itransfer,
- enum libusb_transfer_status status);
- int usbi_handle_transfer_cancellation(struct usbi_transfer *transfer);
- int usbi_parse_descriptor(const unsigned char *source, const char *descriptor,
- void *dest, int host_endian);
- int usbi_device_cache_descriptor(libusb_device *dev);
- int usbi_get_config_index_by_value(struct libusb_device *dev,
- uint8_t bConfigurationValue, int *idx);
- void usbi_connect_device (struct libusb_device *dev);
- void usbi_disconnect_device (struct libusb_device *dev);
- struct usbi_pollfd {
-
- struct libusb_pollfd pollfd;
- struct list_head list;
- };
- int usbi_add_pollfd(struct libusb_context *ctx, int fd, short events);
- void usbi_remove_pollfd(struct libusb_context *ctx, int fd);
- void usbi_fd_notification(struct libusb_context *ctx);
- struct discovered_devs {
- size_t len;
- size_t capacity;
- struct libusb_device *devices
- #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
- [] /* valid C99 code */
- #else
- [0] /* non-standard, but usually working code */
- #endif
- ;
- };
- struct discovered_devs *discovered_devs_append(
- struct discovered_devs *discdevs, struct libusb_device *dev);
- struct usbi_os_backend {
-
- const char *name;
-
- int (*init)(struct libusb_context *ctx);
-
- void (*exit)(void);
-
- int (*get_device_list)(struct libusb_context *ctx,
- struct discovered_devs **discdevs);
-
- int (*open)(struct libusb_device_handle *handle);
-
- void (*close)(struct libusb_device_handle *handle);
-
- int (*get_device_descriptor)(struct libusb_device *device,
- unsigned char *buffer, int *host_endian);
-
- int (*get_active_config_descriptor)(struct libusb_device *device,
- unsigned char *buffer, size_t len, int *host_endian);
-
- int (*get_config_descriptor)(struct libusb_device *device,
- uint8_t config_index, unsigned char *buffer, size_t len,
- int *host_endian);
-
- int (*get_configuration)(struct libusb_device_handle *handle, int *config);
-
- int (*set_configuration)(struct libusb_device_handle *handle, int config);
-
- int (*claim_interface)(struct libusb_device_handle *handle, int interface_number);
-
- int (*release_interface)(struct libusb_device_handle *handle, int interface_number);
-
- int (*set_interface_altsetting)(struct libusb_device_handle *handle,
- int interface_number, int altsetting);
-
- int (*clear_halt)(struct libusb_device_handle *handle,
- unsigned char endpoint);
-
- int (*reset_device)(struct libusb_device_handle *handle);
-
- int (*kernel_driver_active)(struct libusb_device_handle *handle,
- int interface_number);
-
- int (*detach_kernel_driver)(struct libusb_device_handle *handle,
- int interface_number);
-
- int (*attach_kernel_driver)(struct libusb_device_handle *handle,
- int interface_number);
-
- void (*destroy_device)(struct libusb_device *dev);
-
- int (*submit_transfer)(struct usbi_transfer *itransfer);
-
- int (*cancel_transfer)(struct usbi_transfer *itransfer);
-
- void (*clear_transfer_priv)(struct usbi_transfer *itransfer);
-
- int (*handle_events)(struct libusb_context *ctx,
- struct pollfd *fds, POLL_NFDS_TYPE nfds, int num_ready);
-
- int (*clock_gettime)(int clkid, struct timespec *tp);
- #ifdef USBI_TIMERFD_AVAILABLE
-
- clockid_t (*get_timerfd_clockid)(void);
- #endif
-
- size_t device_priv_size;
-
- size_t device_handle_priv_size;
-
- size_t transfer_priv_size;
-
-
- size_t add_iso_packet_size;
- };
- extern const struct usbi_os_backend * const usbi_backend;
- extern const struct usbi_os_backend linux_usbfs_backend;
- extern const struct usbi_os_backend darwin_backend;
- extern const struct usbi_os_backend openbsd_backend;
- extern const struct usbi_os_backend windows_backend;
- extern struct list_head active_contexts_list;
- extern usbi_mutex_static_t active_contexts_lock;
- #endif
|