123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469 |
- #include "libusbi.h"
- #define VALID_HOTPLUG_EVENTS \
- (LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED | \
- LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT)
- #define VALID_HOTPLUG_FLAGS \
- (LIBUSB_HOTPLUG_ENUMERATE)
- void usbi_hotplug_init(struct libusb_context *ctx)
- {
-
- if (!libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG))
- return;
- usbi_mutex_init(&ctx->hotplug_cbs_lock);
- list_init(&ctx->hotplug_cbs);
- ctx->next_hotplug_cb_handle = 1;
- usbi_atomic_store(&ctx->hotplug_ready, 1);
- }
- void usbi_hotplug_exit(struct libusb_context *ctx)
- {
- struct usbi_hotplug_callback *hotplug_cb, *next_cb;
- struct usbi_hotplug_message *msg;
- struct libusb_device *dev, *next_dev;
-
- if (!libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG))
- return;
- if (!usbi_atomic_load(&ctx->hotplug_ready))
- return;
-
- for_each_hotplug_cb_safe(ctx, hotplug_cb, next_cb) {
- list_del(&hotplug_cb->list);
- free(hotplug_cb);
- }
-
- while (!list_empty(&ctx->hotplug_msgs)) {
- msg = list_first_entry(&ctx->hotplug_msgs, struct usbi_hotplug_message, list);
-
- if (msg->event == LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT)
- libusb_unref_device(msg->device);
- list_del(&msg->list);
- free(msg);
- }
-
- for_each_device_safe(ctx, dev, next_dev) {
-
- if (usbi_atomic_load(&dev->refcnt) == 1) {
- list_del(&dev->list);
- }
- if (dev->parent_dev && usbi_atomic_load(&dev->parent_dev->refcnt) == 1) {
-
- assert (dev->parent_dev != next_dev);
- list_del(&dev->parent_dev->list);
- }
- libusb_unref_device(dev);
- }
- usbi_mutex_destroy(&ctx->hotplug_cbs_lock);
- }
- static int usbi_hotplug_match_cb(struct libusb_device *dev,
- libusb_hotplug_event event, struct usbi_hotplug_callback *hotplug_cb)
- {
- if (!(hotplug_cb->flags & event)) {
- return 0;
- }
- if ((hotplug_cb->flags & USBI_HOTPLUG_VENDOR_ID_VALID) &&
- hotplug_cb->vendor_id != dev->device_descriptor.idVendor) {
- return 0;
- }
- if ((hotplug_cb->flags & USBI_HOTPLUG_PRODUCT_ID_VALID) &&
- hotplug_cb->product_id != dev->device_descriptor.idProduct) {
- return 0;
- }
- if ((hotplug_cb->flags & USBI_HOTPLUG_DEV_CLASS_VALID) &&
- hotplug_cb->dev_class != dev->device_descriptor.bDeviceClass) {
- return 0;
- }
- return hotplug_cb->cb(DEVICE_CTX(dev), dev, event, hotplug_cb->user_data);
- }
- void usbi_hotplug_notification(struct libusb_context *ctx, struct libusb_device *dev,
- libusb_hotplug_event event)
- {
- struct usbi_hotplug_message *msg;
- unsigned int event_flags;
-
- if (!usbi_atomic_load(&ctx->hotplug_ready))
- return;
- msg = calloc(1, sizeof(*msg));
- if (!msg) {
- usbi_err(ctx, "error allocating hotplug message");
- return;
- }
- msg->event = event;
- msg->device = dev;
-
- usbi_mutex_lock(&ctx->event_data_lock);
- event_flags = ctx->event_flags;
- ctx->event_flags |= USBI_EVENT_HOTPLUG_MSG_PENDING;
- list_add_tail(&msg->list, &ctx->hotplug_msgs);
- if (!event_flags)
- usbi_signal_event(&ctx->event);
- usbi_mutex_unlock(&ctx->event_data_lock);
- }
- void usbi_hotplug_process(struct libusb_context *ctx, struct list_head *hotplug_msgs)
- {
- struct usbi_hotplug_callback *hotplug_cb, *next_cb;
- struct usbi_hotplug_message *msg;
- int r;
- usbi_mutex_lock(&ctx->hotplug_cbs_lock);
-
- while (!list_empty(hotplug_msgs)) {
- msg = list_first_entry(hotplug_msgs, struct usbi_hotplug_message, list);
- for_each_hotplug_cb_safe(ctx, hotplug_cb, next_cb) {
-
- if (hotplug_cb->flags & USBI_HOTPLUG_NEEDS_FREE)
- continue;
- usbi_mutex_unlock(&ctx->hotplug_cbs_lock);
- r = usbi_hotplug_match_cb(msg->device, msg->event, hotplug_cb);
- usbi_mutex_lock(&ctx->hotplug_cbs_lock);
- if (r) {
- list_del(&hotplug_cb->list);
- free(hotplug_cb);
- }
- }
-
- if (msg->event == LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT)
- libusb_unref_device(msg->device);
- list_del(&msg->list);
- free(msg);
- }
-
- for_each_hotplug_cb_safe(ctx, hotplug_cb, next_cb) {
- if (hotplug_cb->flags & USBI_HOTPLUG_NEEDS_FREE) {
- usbi_dbg(ctx, "freeing hotplug cb %p with handle %d",
- hotplug_cb, hotplug_cb->handle);
- list_del(&hotplug_cb->list);
- free(hotplug_cb);
- }
- }
- usbi_mutex_unlock(&ctx->hotplug_cbs_lock);
- }
- int API_EXPORTED libusb_hotplug_register_callback(libusb_context *ctx,
- int events, int flags,
- int vendor_id, int product_id, int dev_class,
- libusb_hotplug_callback_fn cb_fn, void *user_data,
- libusb_hotplug_callback_handle *callback_handle)
- {
- struct usbi_hotplug_callback *hotplug_cb;
-
- if (!events || (~VALID_HOTPLUG_EVENTS & events) ||
- (~VALID_HOTPLUG_FLAGS & flags) ||
- (LIBUSB_HOTPLUG_MATCH_ANY != vendor_id && (~0xffff & vendor_id)) ||
- (LIBUSB_HOTPLUG_MATCH_ANY != product_id && (~0xffff & product_id)) ||
- (LIBUSB_HOTPLUG_MATCH_ANY != dev_class && (~0xff & dev_class)) ||
- !cb_fn) {
- return LIBUSB_ERROR_INVALID_PARAM;
- }
-
- if (!libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG))
- return LIBUSB_ERROR_NOT_SUPPORTED;
- ctx = usbi_get_context(ctx);
- hotplug_cb = calloc(1, sizeof(*hotplug_cb));
- if (!hotplug_cb)
- return LIBUSB_ERROR_NO_MEM;
- hotplug_cb->flags = (uint8_t)events;
- if (LIBUSB_HOTPLUG_MATCH_ANY != vendor_id) {
- hotplug_cb->flags |= USBI_HOTPLUG_VENDOR_ID_VALID;
- hotplug_cb->vendor_id = (uint16_t)vendor_id;
- }
- if (LIBUSB_HOTPLUG_MATCH_ANY != product_id) {
- hotplug_cb->flags |= USBI_HOTPLUG_PRODUCT_ID_VALID;
- hotplug_cb->product_id = (uint16_t)product_id;
- }
- if (LIBUSB_HOTPLUG_MATCH_ANY != dev_class) {
- hotplug_cb->flags |= USBI_HOTPLUG_DEV_CLASS_VALID;
- hotplug_cb->dev_class = (uint8_t)dev_class;
- }
- hotplug_cb->cb = cb_fn;
- hotplug_cb->user_data = user_data;
- usbi_mutex_lock(&ctx->hotplug_cbs_lock);
-
- hotplug_cb->handle = ctx->next_hotplug_cb_handle++;
-
- if (ctx->next_hotplug_cb_handle < 0)
- ctx->next_hotplug_cb_handle = 1;
- list_add(&hotplug_cb->list, &ctx->hotplug_cbs);
- usbi_mutex_unlock(&ctx->hotplug_cbs_lock);
- usbi_dbg(ctx, "new hotplug cb %p with handle %d", hotplug_cb, hotplug_cb->handle);
- if ((flags & LIBUSB_HOTPLUG_ENUMERATE) && (events & LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED)) {
- ssize_t i, len;
- struct libusb_device **devs;
- len = libusb_get_device_list(ctx, &devs);
- if (len < 0) {
- libusb_hotplug_deregister_callback(ctx, hotplug_cb->handle);
- return (int)len;
- }
- for (i = 0; i < len; i++) {
- usbi_hotplug_match_cb(devs[i],
- LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED,
- hotplug_cb);
- }
- libusb_free_device_list(devs, 1);
- }
- if (callback_handle)
- *callback_handle = hotplug_cb->handle;
- return LIBUSB_SUCCESS;
- }
- void API_EXPORTED libusb_hotplug_deregister_callback(libusb_context *ctx,
- libusb_hotplug_callback_handle callback_handle)
- {
- struct usbi_hotplug_callback *hotplug_cb;
- int deregistered = 0;
-
- if (!libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG))
- return;
- usbi_dbg(ctx, "deregister hotplug cb %d", callback_handle);
- ctx = usbi_get_context(ctx);
- usbi_mutex_lock(&ctx->hotplug_cbs_lock);
- for_each_hotplug_cb(ctx, hotplug_cb) {
- if (callback_handle == hotplug_cb->handle) {
-
- hotplug_cb->flags |= USBI_HOTPLUG_NEEDS_FREE;
- deregistered = 1;
- break;
- }
- }
- usbi_mutex_unlock(&ctx->hotplug_cbs_lock);
- if (deregistered) {
- unsigned int event_flags;
- usbi_mutex_lock(&ctx->event_data_lock);
- event_flags = ctx->event_flags;
- ctx->event_flags |= USBI_EVENT_HOTPLUG_CB_DEREGISTERED;
- if (!event_flags)
- usbi_signal_event(&ctx->event);
- usbi_mutex_unlock(&ctx->event_data_lock);
- }
- }
- DEFAULT_VISIBILITY
- void * LIBUSB_CALL libusb_hotplug_get_user_data(libusb_context *ctx,
- libusb_hotplug_callback_handle callback_handle)
- {
- struct usbi_hotplug_callback *hotplug_cb;
- void *user_data = NULL;
-
- if (!libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG))
- return NULL;
- usbi_dbg(ctx, "get hotplug cb %d user data", callback_handle);
- ctx = usbi_get_context(ctx);
- usbi_mutex_lock(&ctx->hotplug_cbs_lock);
- for_each_hotplug_cb(ctx, hotplug_cb) {
- if (callback_handle == hotplug_cb->handle) {
- user_data = hotplug_cb->user_data;
- break;
- }
- }
- usbi_mutex_unlock(&ctx->hotplug_cbs_lock);
- return user_data;
- }
|