struct libusb_device *ret = NULL;
usbi_mutex_lock(&ctx->usb_devs_lock);
- list_for_each_entry(dev, &ctx->usb_devs, list, struct libusb_device)
+ for_each_device(ctx, dev) {
if (dev->session_data == session_id) {
ret = libusb_ref_device(dev);
break;
}
+ }
usbi_mutex_unlock(&ctx->usb_devs_lock);
return ret;
usbi_backend.hotplug_poll();
usbi_mutex_lock(&ctx->usb_devs_lock);
- list_for_each_entry(dev, &ctx->usb_devs, list, struct libusb_device) {
+ for_each_device(ctx, dev) {
discdevs = discovered_devs_append(discdevs, dev);
if (!discdevs) {
usbi_mutex_lock(&ctx->flying_transfers_lock);
/* safe iteration because transfers may be being deleted */
- list_for_each_entry_safe(itransfer, tmp, &ctx->flying_transfers, list, struct usbi_transfer) {
+ for_each_transfer_safe(ctx, itransfer, tmp) {
struct libusb_transfer *transfer =
USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
usbi_mutex_static_unlock(&active_contexts_lock);
usbi_mutex_lock(&ctx->usb_devs_lock);
- list_for_each_entry_safe(dev, next, &ctx->usb_devs, list, struct libusb_device) {
+ for_each_device_safe(ctx, dev, next) {
list_del(&dev->list);
libusb_unref_device(dev);
}
libusb_handle_events_timeout(ctx, &tv);
usbi_mutex_lock(&ctx->usb_devs_lock);
- list_for_each_entry_safe(dev, next, &ctx->usb_devs, list, struct libusb_device) {
+ for_each_device_safe(ctx, dev, next) {
list_del(&dev->list);
libusb_unref_device(dev);
}
usbi_mutex_lock(&ctx->hotplug_cbs_lock);
- list_for_each_entry_safe(hotplug_cb, next, &ctx->hotplug_cbs, list, struct libusb_hotplug_callback) {
+ for_each_hotplug_cb_safe(ctx, hotplug_cb, next) {
if (hotplug_cb->flags & USBI_HOTPLUG_NEEDS_FREE) {
/* process deregistration in usbi_hotplug_deregister() */
continue;
ctx = usbi_get_context(ctx);
usbi_mutex_lock(&ctx->hotplug_cbs_lock);
- list_for_each_entry(hotplug_cb, &ctx->hotplug_cbs, list, struct libusb_hotplug_callback) {
+ for_each_hotplug_cb(ctx, hotplug_cb) {
if (callback_handle == hotplug_cb->handle) {
/* Mark this callback for deregistration */
hotplug_cb->flags |= USBI_HOTPLUG_NEEDS_FREE;
ctx = usbi_get_context(ctx);
usbi_mutex_lock(&ctx->hotplug_cbs_lock);
- list_for_each_entry(hotplug_cb, &ctx->hotplug_cbs, list, struct libusb_hotplug_callback) {
+ for_each_hotplug_cb(ctx, hotplug_cb) {
if (callback_handle == hotplug_cb->handle) {
user_data = hotplug_cb->user_data;
}
struct libusb_hotplug_callback *hotplug_cb, *next;
usbi_mutex_lock(&ctx->hotplug_cbs_lock);
- list_for_each_entry_safe(hotplug_cb, next, &ctx->hotplug_cbs, list, struct libusb_hotplug_callback) {
+ for_each_hotplug_cb_safe(ctx, hotplug_cb, next) {
if (forced || (hotplug_cb->flags & USBI_HOTPLUG_NEEDS_FREE)) {
usbi_dbg("freeing hotplug cb %p with handle %d", hotplug_cb,
hotplug_cb->handle);
struct list_head list;
};
+#define for_each_hotplug_cb(ctx, c) \
+ for_each_helper(c, &(ctx)->hotplug_cbs, struct libusb_hotplug_callback)
+
+#define for_each_hotplug_cb_safe(ctx, c, n) \
+ for_each_safe_helper(c, n, &(ctx)->hotplug_cbs, struct libusb_hotplug_callback)
+
void usbi_hotplug_deregister(struct libusb_context *ctx, int forced);
void usbi_hotplug_match(struct libusb_context *ctx, struct libusb_device *dev,
libusb_hotplug_event event);
static void cleanup_removed_pollfds(struct libusb_context *ctx)
{
struct usbi_pollfd *ipollfd, *tmp;
- list_for_each_entry_safe(ipollfd, tmp, &ctx->removed_ipollfds, list, struct usbi_pollfd) {
+
+ for_each_removed_pollfd_safe(ctx, ipollfd, tmp) {
list_del(&ipollfd->list);
free(ipollfd);
}
{
struct usbi_transfer *itransfer;
- list_for_each_entry(itransfer, &ctx->flying_transfers, list, struct usbi_transfer) {
+ for_each_transfer(ctx, itransfer) {
struct timespec *cur_ts = &itransfer->timeout;
/* if we've reached transfers of infinite timeout, then we have no
}
/* otherwise, find appropriate place in list */
- list_for_each_entry(cur, &ctx->flying_transfers, list, struct usbi_transfer) {
+ for_each_transfer(ctx, cur) {
/* find first timeout that occurs after the transfer in question */
struct timespec *cur_ts = &cur->timeout;
/* iterate through flying transfers list, finding all transfers that
* have expired timeouts */
- list_for_each_entry(itransfer, &ctx->flying_transfers, list, struct usbi_transfer) {
+ for_each_transfer(ctx, itransfer) {
struct timespec *cur_ts = &itransfer->timeout;
/* if we've reached transfers of infinite timeout, we're all done */
goto done;
}
- list_for_each_entry(ipollfd, &ctx->ipollfds, list, struct usbi_pollfd) {
+ for_each_pollfd(ctx, ipollfd) {
struct libusb_pollfd *pollfd = &ipollfd->pollfd;
ctx->pollfds[i].fd = pollfd->fd;
ctx->pollfds[i].events = pollfd->events;
}
#endif
- list_for_each_entry(ipollfd, &ctx->removed_ipollfds, list, struct usbi_pollfd) {
+ for_each_removed_pollfd(ctx, ipollfd) {
usbi_nfds_t n;
for (n = internal_nfds ; n < nfds ; n++) {
}
/* find next transfer which hasn't already been processed as timed out */
- list_for_each_entry(itransfer, &ctx->flying_transfers, list, struct usbi_transfer) {
+ for_each_transfer(ctx, itransfer) {
if (itransfer->timeout_flags & (USBI_TRANSFER_TIMEOUT_HANDLED | USBI_TRANSFER_OS_HANDLES_TIMEOUT))
continue;
usbi_dbg("remove fd %d", fd);
usbi_mutex_lock(&ctx->event_data_lock);
- list_for_each_entry(ipollfd, &ctx->ipollfds, list, struct usbi_pollfd)
+ for_each_pollfd(ctx, ipollfd) {
if (ipollfd->pollfd.fd == fd) {
found = 1;
break;
}
+ }
if (!found) {
usbi_dbg("couldn't find fd %d to remove", fd);
if (!ret)
goto out;
- list_for_each_entry(ipollfd, &ctx->ipollfds, list, struct usbi_pollfd)
+ for_each_pollfd(ctx, ipollfd)
ret[i++] = (struct libusb_pollfd *)ipollfd;
ret[ctx->pollfds_cnt] = NULL;
*/
void usbi_handle_disconnect(struct libusb_device_handle *dev_handle)
{
+ struct libusb_context *ctx = HANDLE_CTX(dev_handle);
struct usbi_transfer *cur;
struct usbi_transfer *to_cancel;
while (1) {
to_cancel = NULL;
- usbi_mutex_lock(&HANDLE_CTX(dev_handle)->flying_transfers_lock);
- list_for_each_entry(cur, &HANDLE_CTX(dev_handle)->flying_transfers, list, struct usbi_transfer)
+ usbi_mutex_lock(&ctx->flying_transfers_lock);
+ for_each_transfer(ctx, cur) {
if (USBI_TRANSFER_TO_LIBUSB_TRANSFER(cur)->dev_handle == dev_handle) {
usbi_mutex_lock(&cur->lock);
if (cur->state_flags & USBI_TRANSFER_IN_FLIGHT)
if (to_cancel)
break;
}
- usbi_mutex_unlock(&HANDLE_CTX(dev_handle)->flying_transfers_lock);
+ }
+ usbi_mutex_unlock(&ctx->flying_transfers_lock);
if (!to_cancel)
break;
#define list_first_entry(ptr, type, member) \
list_entry((ptr)->next, type, member)
+#define list_next_entry(ptr, type, member) \
+ list_entry((ptr)->member.next, type, member)
+
/* Get each entry from a list
* pos - A structure pointer has a "member" element
* head - list head
* type - the type of the first parameter
*/
#define list_for_each_entry(pos, head, member, type) \
- for (pos = list_entry((head)->next, type, member); \
+ for (pos = list_first_entry(head, type, member); \
&pos->member != (head); \
- pos = list_entry(pos->member.next, type, member))
+ pos = list_next_entry(pos, 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); \
+ for (pos = list_first_entry(head, type, member), \
+ n = list_next_entry(pos, type, member); \
&pos->member != (head); \
- pos = n, n = list_entry(n->member.next, type, member))
+ pos = n, n = list_next_entry(n, type, member))
+
+/* Helper macros to iterate over a list. The structure pointed
+ * to by "pos" must have a list_head member named "list".
+ */
+#define for_each_helper(pos, head, type) \
+ list_for_each_entry(pos, head, list, type)
+
+#define for_each_safe_helper(pos, n, head, type) \
+ list_for_each_entry_safe(pos, n, head, list, type)
#define list_empty(entry) ((entry)->next == (entry))
extern struct list_head active_contexts_list;
extern usbi_mutex_static_t active_contexts_lock;
+#define for_each_context(c) \
+ for_each_helper(c, &active_contexts_list, struct libusb_context)
+
+#define for_each_device(ctx, d) \
+ for_each_helper(d, &(ctx)->usb_devs, struct libusb_device)
+
+#define for_each_device_safe(ctx, d, n) \
+ for_each_safe_helper(d, n, &(ctx)->usb_devs, struct libusb_device)
+
+#define for_each_open_device(ctx, h) \
+ for_each_helper(h, &(ctx)->open_devs, struct libusb_device_handle)
+
+#define for_each_transfer(ctx, t) \
+ for_each_helper(t, &(ctx)->flying_transfers, struct usbi_transfer)
+
+#define for_each_transfer_safe(ctx, t, n) \
+ for_each_safe_helper(t, n, &(ctx)->flying_transfers, struct usbi_transfer)
+
+#define for_each_pollfd(ctx, p) \
+ for_each_helper(p, &(ctx)->ipollfds, struct usbi_pollfd)
+
+#define for_each_removed_pollfd(ctx, p) \
+ for_each_helper(p, &(ctx)->removed_ipollfds, struct usbi_pollfd)
+
+#define for_each_removed_pollfd_safe(ctx, p, n) \
+ for_each_safe_helper(p, n, &(ctx)->removed_ipollfds, struct usbi_pollfd)
+
#ifdef __cplusplus
}
#endif
}
/* add this device to each active context's device list */
- list_for_each_entry(ctx, &active_contexts_list, list, struct libusb_context) {
+ for_each_context(ctx) {
process_new_device (ctx, cached_device, old_session_id);
}
continue;
}
- list_for_each_entry(ctx, &active_contexts_list, list, struct libusb_context) {
+ for_each_context(ctx) {
usbi_dbg ("notifying context %p of device disconnect", ctx);
dev = usbi_get_device_by_session_id(ctx, (unsigned long) session);
unsigned long session_id = (unsigned long)&fDevice;
usbi_mutex_lock(&active_contexts_lock);
- list_for_each_entry(ctx, &active_contexts_list, list, struct libusb_context) {
+ for_each_context(ctx) {
struct libusb_device *dev = usbi_get_device_by_session_id(ctx, session_id);
if (dev) {
usbi_dbg("using previously allocated device with location %lu", session_id);
unsigned long session_id = (unsigned long)&fDevice;
usbi_mutex_lock(&active_contexts_lock);
- list_for_each_entry(ctx, &active_contexts_list, list, struct libusb_context) {
+ for_each_context(ctx) {
dev = usbi_get_device_by_session_id(ctx, session_id);
if (dev != NULL) {
usbi_disconnect_device(dev);
retry:
/* find the parent in the context */
usbi_mutex_lock(&ctx->usb_devs_lock);
- list_for_each_entry(it, &ctx->usb_devs, list, struct libusb_device) {
+ for_each_device(ctx, it) {
struct linux_device_priv *priv = usbi_get_device_priv(it);
if (priv->sysfs_dir) {
struct libusb_context *ctx;
usbi_mutex_static_lock(&active_contexts_lock);
- list_for_each_entry(ctx, &active_contexts_list, list, struct libusb_context) {
+ for_each_context(ctx) {
linux_enumerate_device(ctx, busnum, devaddr, sys_name);
}
usbi_mutex_static_unlock(&active_contexts_lock);
unsigned long session_id = busnum << 8 | devaddr;
usbi_mutex_static_lock(&active_contexts_lock);
- list_for_each_entry(ctx, &active_contexts_list, list, struct libusb_context) {
+ for_each_context(ctx) {
dev = usbi_get_device_by_session_id(ctx, session_id);
if (dev) {
usbi_disconnect_device(dev);
continue;
num_ready--;
- list_for_each_entry(handle, &ctx->open_devs, list, struct libusb_device_handle) {
+ for_each_open_device(ctx, handle) {
hpriv = usbi_get_device_handle_priv(handle);
if (hpriv->fd == pollfd->fd)
break;
transfer_priv = NULL;
usbi_mutex_lock(&ctx->flying_transfers_lock);
- list_for_each_entry(itransfer, &ctx->flying_transfers, list, struct usbi_transfer) {
+ for_each_transfer(ctx, itransfer) {
transfer_priv = usbi_get_transfer_priv(itransfer);
if (transfer_priv->pollable_fd.fd == fds[i].fd)
break;
-#define LIBUSB_NANO 11529
+#define LIBUSB_NANO 11530