3 * Copyright (c) 2009 Sylvestre Gallon. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/queue.h>
37 #include "libusb20_desc.h"
38 #include "libusb20_int.h"
43 usb_add_pollfd(libusb_context *ctx, int fd, short events)
45 struct usb_pollfd *pollfd;
48 return (LIBUSB_ERROR_INVALID_PARAM);
50 pollfd = malloc(sizeof(*pollfd));
52 return (LIBUSB_ERROR_NO_MEM);
54 pollfd->pollfd.fd = fd;
55 pollfd->pollfd.events = events;
57 pthread_mutex_lock(&ctx->pollfds_lock);
58 TAILQ_INSERT_TAIL(&ctx->pollfds, pollfd, list);
59 pthread_mutex_unlock(&ctx->pollfds_lock);
62 ctx->fd_added_cb(fd, events, ctx->fd_cb_user_data);
67 usb_remove_pollfd(libusb_context *ctx, int fd)
69 struct usb_pollfd *pollfd;
73 pthread_mutex_lock(&ctx->pollfds_lock);
75 TAILQ_FOREACH(pollfd, &ctx->pollfds, list) {
76 if (pollfd->pollfd.fd == fd) {
83 pthread_mutex_unlock(&ctx->pollfds_lock);
87 TAILQ_REMOVE(&ctx->pollfds, pollfd, list);
88 pthread_mutex_unlock(&ctx->pollfds_lock);
91 if (ctx->fd_removed_cb)
92 ctx->fd_removed_cb(fd, ctx->fd_cb_user_data);
96 usb_handle_transfer_completion(struct usb_transfer *uxfer,
97 enum libusb_transfer_status status)
99 libusb_transfer *xfer;
103 xfer = (struct libusb_transfer *) ((uint8_t *)uxfer +
104 sizeof(struct usb_transfer));
105 ctx = xfer->dev_handle->dev->ctx;
107 pthread_mutex_lock(&ctx->flying_transfers_lock);
108 TAILQ_REMOVE(&ctx->flying_transfers, uxfer, list);
109 pthread_mutex_unlock(&ctx->flying_transfers_lock);
111 if (status == LIBUSB_TRANSFER_COMPLETED && xfer->flags &
112 LIBUSB_TRANSFER_SHORT_NOT_OK) {
114 if (xfer->type == LIBUSB_TRANSFER_TYPE_CONTROL)
115 len -= sizeof(libusb_control_setup);
116 if (len != uxfer->transferred) {
117 status = LIBUSB_TRANSFER_ERROR;
121 xfer->status = status;
122 xfer->actual_length = uxfer->transferred;
125 xfer->callback(xfer);
126 if (xfer->flags & LIBUSB_TRANSFER_FREE_TRANSFER)
127 libusb_free_transfer(xfer);
129 pthread_mutex_lock(&ctx->event_waiters_lock);
130 pthread_cond_broadcast(&ctx->event_waiters_cond);
131 pthread_mutex_unlock(&ctx->event_waiters_lock);
135 usb_handle_disconnect(struct libusb_device_handle *devh)
137 struct libusb_context *ctx;
138 struct libusb_transfer *xfer;
139 struct usb_transfer *cur;
140 struct usb_transfer *to_cancel;
142 ctx = devh->dev->ctx;
145 pthread_mutex_lock(&ctx->flying_transfers_lock);
147 TAILQ_FOREACH(cur, &ctx->flying_transfers, list) {
148 xfer = (struct libusb_transfer *) ((uint8_t *)cur +
149 sizeof(struct usb_transfer));
150 if (xfer->dev_handle == devh) {
155 pthread_mutex_unlock(&ctx->flying_transfers_lock);
157 if (to_cancel == NULL)
160 usb_handle_transfer_completion(to_cancel, LIBUSB_TRANSFER_NO_DEVICE);
166 get_next_timeout(libusb_context *ctx, struct timeval *tv, struct timeval *out)
168 struct timeval timeout;
170 if (libusb_get_next_timeout(ctx, &timeout)) {
171 if (timerisset(&timeout) == 0)
173 if (timercmp(&timeout, tv, <) != 0)
185 handle_timeouts(struct libusb_context *ctx)
187 struct timespec sys_ts;
188 struct timeval sys_tv;
189 struct timeval *cur_tv;
190 struct usb_transfer *xfer;
191 struct libusb_transfer *uxfer;
197 pthread_mutex_lock(&ctx->flying_transfers_lock);
198 if (TAILQ_EMPTY(&ctx->flying_transfers))
201 ret = clock_gettime(CLOCK_MONOTONIC, &sys_ts);
202 TIMESPEC_TO_TIMEVAL(&sys_tv, &sys_ts);
204 TAILQ_FOREACH(xfer, &ctx->flying_transfers, list) {
205 cur_tv = &xfer->timeout;
207 if (timerisset(cur_tv) == 0)
210 if (xfer->flags & USB_TIMED_OUT)
213 if ((cur_tv->tv_sec > sys_tv.tv_sec) || (cur_tv->tv_sec == sys_tv.tv_sec &&
214 cur_tv->tv_usec > sys_tv.tv_usec))
217 xfer->flags |= USB_TIMED_OUT;
218 uxfer = (libusb_transfer *) ((uint8_t *)xfer +
219 sizeof(struct usb_transfer));
220 ret = libusb_cancel_transfer(uxfer);
223 pthread_mutex_unlock(&ctx->flying_transfers_lock);
228 handle_events(struct libusb_context *ctx, struct timeval *tv)
230 struct libusb_pollfd *tmppollfd;
231 struct libusb_device_handle *devh;
232 struct usb_pollfd *ipollfd;
242 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "handle_events enter");
247 pthread_mutex_lock(&ctx->pollfds_lock);
248 TAILQ_FOREACH(ipollfd, &ctx->pollfds, list)
251 fds = alloca(sizeof(*fds) * nfds);
253 return (LIBUSB_ERROR_NO_MEM);
255 TAILQ_FOREACH(ipollfd, &ctx->pollfds, list) {
256 tmppollfd = &ipollfd->pollfd;
257 tmpfd = tmppollfd->fd;
260 fds[i].events = tmppollfd->events;
264 pthread_mutex_unlock(&ctx->pollfds_lock);
266 timeout = (tv->tv_sec * 1000) + (tv->tv_usec / 1000);
267 if (tv->tv_usec % 1000)
270 ret = poll(fds, nfds, timeout);
272 return (handle_timeouts(ctx));
273 else if (ret == -1 && errno == EINTR)
274 return (LIBUSB_ERROR_INTERRUPTED);
276 return (LIBUSB_ERROR_IO);
278 if (fds[0].revents) {
288 pthread_mutex_lock(&ctx->open_devs_lock);
289 for (i = 0, devh = NULL ; i < nfds && ret > 0 ; i++) {
296 TAILQ_FOREACH(devh, &ctx->open_devs, list) {
297 if (libusb20_dev_get_fd(devh->os_priv) == tfds->fd)
301 if (tfds->revents & POLLERR) {
302 usb_remove_pollfd(ctx, libusb20_dev_get_fd(devh->os_priv));
304 usb_handle_disconnect(devh);
309 pthread_mutex_lock(&libusb20_lock);
310 DPRINTF(ctx, LIBUSB_DEBUG_TRANSFER, "LIBUSB20_PROCESS");
312 ret = libusb20_dev_process(devh->os_priv);
313 pthread_mutex_unlock(&libusb20_lock);
316 if (ret == 0 || ret == LIBUSB20_ERROR_NO_DEVICE)
324 pthread_mutex_unlock(&ctx->open_devs_lock);
327 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "handle_events leave");
331 /* Polling and timing */
334 libusb_try_lock_events(libusb_context * ctx)
339 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_try_lock_events enter");
341 pthread_mutex_lock(&ctx->pollfd_modify_lock);
342 ret = ctx->pollfd_modify;
343 pthread_mutex_unlock(&ctx->pollfd_modify_lock);
348 ret = pthread_mutex_trylock(&ctx->events_lock);
353 ctx->event_handler_active = 1;
355 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_try_lock_events leave");
360 libusb_lock_events(libusb_context * ctx)
363 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_lock_events enter");
365 pthread_mutex_lock(&ctx->events_lock);
366 ctx->event_handler_active = 1;
368 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_lock_events leave");
372 libusb_unlock_events(libusb_context * ctx)
375 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_unlock_events enter");
377 ctx->event_handler_active = 0;
378 pthread_mutex_unlock(&ctx->events_lock);
380 pthread_mutex_lock(&ctx->event_waiters_lock);
381 pthread_cond_broadcast(&ctx->event_waiters_cond);
382 pthread_mutex_unlock(&ctx->event_waiters_lock);
384 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_unlock_events leave");
388 libusb_event_handling_ok(libusb_context * ctx)
393 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_event_handling_ok enter");
395 pthread_mutex_lock(&ctx->pollfd_modify_lock);
396 ret = ctx->pollfd_modify;
397 pthread_mutex_unlock(&ctx->pollfd_modify_lock);
402 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_event_handling_ok leave");
407 libusb_event_handler_active(libusb_context * ctx)
412 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_event_handler_active enter");
414 pthread_mutex_lock(&ctx->pollfd_modify_lock);
415 ret = ctx->pollfd_modify;
416 pthread_mutex_unlock(&ctx->pollfd_modify_lock);
421 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_event_handler_active leave");
422 return (ctx->event_handler_active);
426 libusb_lock_event_waiters(libusb_context * ctx)
429 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_lock_event_waiters enter");
431 pthread_mutex_lock(&ctx->event_waiters_lock);
433 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_lock_event_waiters leave");
437 libusb_unlock_event_waiters(libusb_context * ctx)
440 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_unlock_event_waiters enter");
442 pthread_mutex_unlock(&ctx->event_waiters_lock);
444 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_unlock_event_waiters leave");
448 libusb_wait_for_event(libusb_context * ctx, struct timeval *tv)
454 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_wait_for_event enter");
457 pthread_cond_wait(&ctx->event_waiters_cond,
458 &ctx->event_waiters_lock);
462 ret = clock_gettime(CLOCK_REALTIME, &ts);
464 return (LIBUSB_ERROR_OTHER);
466 ts.tv_sec = tv->tv_sec;
467 ts.tv_nsec = tv->tv_usec * 1000;
468 if (ts.tv_nsec > 1000000000) {
469 ts.tv_nsec -= 1000000000;
473 ret = pthread_cond_timedwait(&ctx->event_waiters_cond,
474 &ctx->event_waiters_lock, &ts);
476 if (ret == ETIMEDOUT)
479 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_wait_for_event leave");
484 libusb_handle_events_timeout(libusb_context * ctx, struct timeval *tv)
486 struct timeval poll_timeout;
490 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_timeout enter");
492 ret = get_next_timeout(ctx, tv, &poll_timeout);
494 return handle_timeouts(ctx);
497 if (libusb_try_lock_events(ctx) == 0) {
498 ret = handle_events(ctx, &poll_timeout);
499 libusb_unlock_events(ctx);
503 libusb_lock_event_waiters(ctx);
504 if (libusb_event_handler_active(ctx) == 0) {
505 libusb_unlock_event_waiters(ctx);
509 ret = libusb_wait_for_event(ctx, &poll_timeout);
510 libusb_unlock_event_waiters(ctx);
515 return (handle_timeouts(ctx));
517 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_timeout leave");
522 libusb_handle_events(libusb_context * ctx)
528 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events enter");
532 ret = libusb_handle_events_timeout(ctx, &tv);
534 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events leave");
539 libusb_handle_events_locked(libusb_context * ctx, struct timeval *tv)
542 struct timeval poll_tv;
545 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_locked enter");
547 ret = get_next_timeout(ctx, tv, &poll_tv);
549 return handle_timeouts(ctx);
552 ret = handle_events(ctx, &poll_tv);
554 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_locked leave");
559 libusb_get_next_timeout(libusb_context * ctx, struct timeval *tv)
561 struct usb_transfer *xfer;
562 struct timeval *next_tv;
563 struct timeval cur_tv;
564 struct timespec cur_ts;
569 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_get_next_timeout enter");
572 pthread_mutex_lock(&ctx->flying_transfers_lock);
573 if (TAILQ_EMPTY(&ctx->flying_transfers)) {
574 pthread_mutex_unlock(&ctx->flying_transfers_lock);
578 TAILQ_FOREACH(xfer, &ctx->flying_transfers, list) {
579 if (!(xfer->flags & USB_TIMED_OUT)) {
584 pthread_mutex_unlock(&ctx->flying_transfers_lock);
590 next_tv = &xfer->timeout;
591 if (timerisset(next_tv) == 0)
594 ret = clock_gettime(CLOCK_MONOTONIC, &cur_ts);
596 return (LIBUSB_ERROR_OTHER);
597 TIMESPEC_TO_TIMEVAL(&cur_tv, &cur_ts);
599 if (timercmp(&cur_tv, next_tv, >=) != 0)
602 timersub(next_tv, &cur_tv, tv);
604 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_get_next_timeout leave");
609 libusb_set_pollfd_notifiers(libusb_context * ctx,
610 libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb,
614 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_set_pollfd_notifiers enter");
616 ctx->fd_added_cb = added_cb;
617 ctx->fd_removed_cb = removed_cb;
618 ctx->fd_cb_user_data = user_data;
620 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_set_pollfd_notifiers leave");
623 struct libusb_pollfd **
624 libusb_get_pollfds(libusb_context * ctx)
626 struct usb_pollfd *pollfd;
631 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_get_pollfds enter");
634 pthread_mutex_lock(&ctx->pollfds_lock);
635 TAILQ_FOREACH(pollfd, &ctx->pollfds, list)
638 ret = calloc(i + 1 , sizeof(struct libusb_pollfd *));
640 pthread_mutex_unlock(&ctx->pollfds_lock);
645 TAILQ_FOREACH(pollfd, &ctx->pollfds, list)
646 ret[i++] = (struct libusb_pollfd *) pollfd;
649 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_get_pollfds leave");
654 /* Synchronous device I/O */
656 static void ctrl_tr_cb(struct libusb_transfer *transfer)
663 DPRINTF(ctx, LIBUSB_DEBUG_TRANSFER, "CALLBACK ENTER");
665 complet = transfer->user_data;
670 libusb_control_transfer(libusb_device_handle * devh,
671 uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
672 unsigned char *data, uint16_t wLength, unsigned int timeout)
674 struct libusb_transfer *xfer;
675 struct libusb_control_setup *ctr;
681 ctx = devh->dev->ctx;
683 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_control_transfer enter");
685 if (devh == NULL || data == NULL)
686 return (LIBUSB_ERROR_NO_MEM);
688 xfer = libusb_alloc_transfer(0);
690 return (LIBUSB_ERROR_NO_MEM);
692 buff = malloc(sizeof(libusb_control_setup) + wLength);
694 libusb_free_transfer(xfer);
695 return (LIBUSB_ERROR_NO_MEM);
698 ctr = (libusb_control_setup *)buff;
699 ctr->bmRequestType = bmRequestType;
700 ctr->bRequest = bRequest;
701 ctr->wValue = wValue;
702 ctr->wIndex = wIndex;
703 ctr->wLength = wLength;
704 if ((bmRequestType & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_OUT)
705 memcpy(buff + sizeof(libusb_control_setup), data, wLength);
707 xfer->dev_handle = devh;
709 xfer->type = LIBUSB_TRANSFER_TYPE_CONTROL;
710 xfer->timeout = timeout;
712 xfer->length = sizeof(libusb_control_setup) + wLength;
713 xfer->user_data = &complet;
714 xfer->callback = ctrl_tr_cb;
715 xfer->flags = LIBUSB_TRANSFER_FREE_TRANSFER;
718 if ((ret = libusb_submit_transfer(xfer)) < 0) {
719 libusb_free_transfer(xfer);
724 if ((ret = libusb_handle_events(ctx)) < 0) {
725 libusb_cancel_transfer(xfer);
727 if (libusb_handle_events(ctx) < 0) {
730 libusb_free_transfer(xfer);
735 if ((bmRequestType & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_IN)
736 memcpy(data, buff + sizeof(libusb_control_setup), wLength);
738 switch (xfer->status) {
739 case LIBUSB_TRANSFER_COMPLETED:
740 ret = xfer->actual_length;
742 case LIBUSB_TRANSFER_TIMED_OUT:
743 case LIBUSB_TRANSFER_STALL:
744 case LIBUSB_TRANSFER_NO_DEVICE:
748 ret = LIBUSB_ERROR_OTHER;
750 libusb_free_transfer(xfer);
752 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_control_transfer leave");
757 do_transfer(struct libusb_device_handle *devh,
758 unsigned char endpoint, unsigned char *data, int length,
759 int *transferred, unsigned int timeout, int type)
761 struct libusb_transfer *xfer;
766 if (devh == NULL || data == NULL)
767 return (LIBUSB_ERROR_NO_MEM);
769 xfer = libusb_alloc_transfer(0);
771 return (LIBUSB_ERROR_NO_MEM);
773 ctx = devh->dev->ctx;
776 xfer->dev_handle = devh;
777 xfer->endpoint = endpoint;
779 xfer->timeout = timeout;
781 xfer->length = length;
782 xfer->user_data = &complet;
783 xfer->callback = ctrl_tr_cb;
786 DPRINTF(ctx, LIBUSB_DEBUG_TRANSFER, "SUBMIT_TRANSFER");
787 if ((ret = libusb_submit_transfer(xfer)) < 0) {
788 libusb_free_transfer(xfer);
789 DPRINTF(ctx, LIBUSB_DEBUG_TRANSFER, "SUBMIT_TRANSFER FAILED %i", ret);
793 while (complet == 0) {
794 if ((ret = libusb_handle_events(ctx)) < 0) {
795 libusb_cancel_transfer(xfer);
796 libusb_free_transfer(xfer);
797 while (complet == 0) {
798 if (libusb_handle_events(ctx) < 0)
805 *transferred = xfer->actual_length;
806 DPRINTF(ctx, LIBUSB_DEBUG_TRANSFER, "xfer->status %i", xfer->status);
807 switch (xfer->status) {
808 case LIBUSB_TRANSFER_COMPLETED:
809 ret = xfer->actual_length;
811 case LIBUSB_TRANSFER_TIMED_OUT:
812 case LIBUSB_TRANSFER_OVERFLOW:
813 case LIBUSB_TRANSFER_STALL:
814 case LIBUSB_TRANSFER_NO_DEVICE:
818 ret = LIBUSB_ERROR_OTHER;
821 libusb_free_transfer(xfer);
826 libusb_bulk_transfer(struct libusb_device_handle *devh,
827 unsigned char endpoint, unsigned char *data, int length,
828 int *transferred, unsigned int timeout)
835 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_bulk_transfer enter");
837 ret = do_transfer(devh, endpoint, data, length, transferred,
838 timeout, LIBUSB_TRANSFER_TYPE_BULK);
840 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_bulk_transfer leave");
845 * Need to fix xfer->type
848 libusb_interrupt_transfer(struct libusb_device_handle *devh,
849 unsigned char endpoint, unsigned char *data, int length,
850 int *transferred, unsigned int timeout)
857 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_interrupt_transfer enter");
859 ret = do_transfer(devh, endpoint, data, length, transferred,
860 timeout, LIBUSB_TRANSFER_TYPE_INTERRUPT);
862 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_interrupt_transfer leave");