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
34 #include <sys/queue.h>
35 #include <sys/endian.h>
38 #include "libusb20_desc.h"
39 #include "libusb20_int.h"
44 libusb10_add_pollfd(libusb_context *ctx, struct libusb_super_pollfd *pollfd,
45 struct libusb20_device *pdev, int fd, short events)
50 if (pollfd->entry.tqe_prev != NULL)
51 return; /* already queued */
57 pollfd->pollfd.fd = fd;
58 pollfd->pollfd.events = events;
61 TAILQ_INSERT_TAIL(&ctx->pollfds, pollfd, entry);
65 ctx->fd_added_cb(fd, events, ctx->fd_cb_user_data);
69 libusb10_remove_pollfd(libusb_context *ctx, struct libusb_super_pollfd *pollfd)
74 if (pollfd->entry.tqe_prev == NULL)
75 return; /* already dequeued */
78 TAILQ_REMOVE(&ctx->pollfds, pollfd, entry);
79 pollfd->entry.tqe_prev = NULL;
82 if (ctx->fd_removed_cb)
83 ctx->fd_removed_cb(pollfd->pollfd.fd, ctx->fd_cb_user_data);
86 /* This function must be called locked */
89 libusb10_handle_events_sub(struct libusb_context *ctx, struct timeval *tv)
91 struct libusb_device *dev;
92 struct libusb20_device **ppdev;
93 struct libusb_super_pollfd *pfd;
95 struct libusb_super_transfer *sxfer;
96 struct libusb_transfer *uxfer;
102 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb10_handle_events_sub enter");
106 TAILQ_FOREACH(pfd, &ctx->pollfds, entry)
109 fds = alloca(sizeof(*fds) * nfds);
111 return (LIBUSB_ERROR_NO_MEM);
113 ppdev = alloca(sizeof(*ppdev) * nfds);
115 return (LIBUSB_ERROR_NO_MEM);
117 TAILQ_FOREACH(pfd, &ctx->pollfds, entry) {
118 fds[i].fd = pfd->pollfd.fd;
119 fds[i].events = pfd->pollfd.events;
121 ppdev[i] = pfd->pdev;
122 if (pfd->pdev != NULL)
123 libusb_get_device(pfd->pdev)->refcnt++;
130 timeout = (tv->tv_sec * 1000) + ((tv->tv_usec + 999) / 1000);
133 err = poll(fds, nfds, timeout);
136 if ((err == -1) && (errno == EINTR))
137 err = LIBUSB_ERROR_INTERRUPTED;
139 err = LIBUSB_ERROR_IO;
142 for (i = 0; i != nfds; i++) {
143 if (ppdev[i] != NULL) {
145 libusb_unref_device(libusb_get_device(ppdev[i]));
151 for (i = 0; i != nfds; i++) {
152 if (ppdev[i] != NULL) {
153 dev = libusb_get_device(ppdev[i]);
155 if (fds[i].revents == 0)
156 err = 0; /* nothing to do */
158 err = libusb20_dev_process(ppdev[i]);
161 /* cancel all transfers - device is gone */
162 libusb10_cancel_all_transfer(dev);
164 /* remove USB device from polling loop */
165 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
168 libusb_unref_device(dev);
175 if (read(fds[i].fd, &dummy, 1) != 1)
185 /* Do all done callbacks */
187 while ((sxfer = TAILQ_FIRST(&ctx->tr_done))) {
188 TAILQ_REMOVE(&ctx->tr_done, sxfer, entry);
189 sxfer->entry.tqe_prev = NULL;
195 uxfer = (struct libusb_transfer *)(
196 ((uint8_t *)sxfer) + sizeof(*sxfer));
198 if (uxfer->callback != NULL)
199 (uxfer->callback) (uxfer);
201 if (uxfer->flags & LIBUSB_TRANSFER_FREE_BUFFER)
204 if (uxfer->flags & LIBUSB_TRANSFER_FREE_TRANSFER)
205 libusb_free_transfer(uxfer);
213 /* Wakeup other waiters */
214 pthread_cond_broadcast(&ctx->ctx_cond);
219 /* Polling and timing */
222 libusb_try_lock_events(libusb_context *ctx)
226 ctx = GET_CONTEXT(ctx);
230 err = CTX_TRYLOCK(ctx);
234 err = (ctx->ctx_handler != NO_THREAD);
238 ctx->ctx_handler = pthread_self();
244 libusb_lock_events(libusb_context *ctx)
246 ctx = GET_CONTEXT(ctx);
248 if (ctx->ctx_handler == NO_THREAD)
249 ctx->ctx_handler = pthread_self();
253 libusb_unlock_events(libusb_context *ctx)
255 ctx = GET_CONTEXT(ctx);
256 if (ctx->ctx_handler == pthread_self()) {
257 ctx->ctx_handler = NO_THREAD;
258 pthread_cond_broadcast(&ctx->ctx_cond);
264 libusb_event_handling_ok(libusb_context *ctx)
266 ctx = GET_CONTEXT(ctx);
267 return (ctx->ctx_handler == pthread_self());
271 libusb_event_handler_active(libusb_context *ctx)
273 ctx = GET_CONTEXT(ctx);
274 return (ctx->ctx_handler != NO_THREAD);
278 libusb_lock_event_waiters(libusb_context *ctx)
280 ctx = GET_CONTEXT(ctx);
285 libusb_unlock_event_waiters(libusb_context *ctx)
287 ctx = GET_CONTEXT(ctx);
292 libusb_wait_for_event(libusb_context *ctx, struct timeval *tv)
297 ctx = GET_CONTEXT(ctx);
298 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_wait_for_event enter");
301 pthread_cond_wait(&ctx->ctx_cond,
305 err = clock_gettime(CLOCK_REALTIME, &ts);
307 return (LIBUSB_ERROR_OTHER);
309 ts.tv_sec = tv->tv_sec;
310 ts.tv_nsec = tv->tv_usec * 1000;
311 if (ts.tv_nsec >= 1000000000) {
312 ts.tv_nsec -= 1000000000;
315 err = pthread_cond_timedwait(&ctx->ctx_cond,
316 &ctx->ctx_lock, &ts);
318 if (err == ETIMEDOUT)
325 libusb_handle_events_timeout(libusb_context *ctx, struct timeval *tv)
329 ctx = GET_CONTEXT(ctx);
331 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_timeout enter");
333 libusb_lock_events(ctx);
335 err = libusb_handle_events_locked(ctx, tv);
337 libusb_unlock_events(ctx);
339 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_timeout leave");
345 libusb_handle_events(libusb_context *ctx)
347 return (libusb_handle_events_timeout(ctx, NULL));
351 libusb_handle_events_locked(libusb_context *ctx, struct timeval *tv)
355 ctx = GET_CONTEXT(ctx);
357 if (libusb_event_handling_ok(ctx)) {
358 err = libusb10_handle_events_sub(ctx, tv);
360 libusb_wait_for_event(ctx, tv);
367 libusb_get_next_timeout(libusb_context *ctx, struct timeval *tv)
369 /* all timeouts are currently being done by the kernel */
375 libusb_set_pollfd_notifiers(libusb_context *ctx,
376 libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb,
379 ctx = GET_CONTEXT(ctx);
381 ctx->fd_added_cb = added_cb;
382 ctx->fd_removed_cb = removed_cb;
383 ctx->fd_cb_user_data = user_data;
386 struct libusb_pollfd **
387 libusb_get_pollfds(libusb_context *ctx)
389 struct libusb_super_pollfd *pollfd;
393 ctx = GET_CONTEXT(ctx);
398 TAILQ_FOREACH(pollfd, &ctx->pollfds, entry)
401 ret = calloc(i + 1, sizeof(struct libusb_pollfd *));
406 TAILQ_FOREACH(pollfd, &ctx->pollfds, entry)
407 ret[i++] = &pollfd->pollfd;
416 /* Synchronous device I/O */
419 libusb_control_transfer(libusb_device_handle *devh,
420 uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
421 uint8_t *data, uint16_t wLength, unsigned int timeout)
423 struct LIBUSB20_CONTROL_SETUP_DECODED req;
428 return (LIBUSB_ERROR_INVALID_PARAM);
430 if ((wLength != 0) && (data == NULL))
431 return (LIBUSB_ERROR_INVALID_PARAM);
433 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req);
435 req.bmRequestType = bmRequestType;
436 req.bRequest = bRequest;
439 req.wLength = wLength;
441 err = libusb20_dev_request_sync(devh, &req, data,
442 &actlen, timeout, 0);
444 if (err == LIBUSB20_ERROR_PIPE)
445 return (LIBUSB_ERROR_PIPE);
446 else if (err == LIBUSB20_ERROR_TIMEOUT)
447 return (LIBUSB_ERROR_TIMEOUT);
449 return (LIBUSB_ERROR_NO_DEVICE);
455 libusb10_do_transfer_cb(struct libusb_transfer *transfer)
460 ctx = GET_CONTEXT(NULL);
462 DPRINTF(ctx, LIBUSB_DEBUG_TRANSFER, "sync I/O done");
464 pdone = transfer->user_data;
469 * TODO: Replace the following function. Allocating and freeing on a
470 * per-transfer basis is slow. --HPS
473 libusb10_do_transfer(libusb_device_handle *devh,
474 uint8_t endpoint, uint8_t *data, int length,
475 int *transferred, unsigned int timeout, int type)
478 struct libusb_transfer *xfer;
479 volatile int complet;
483 return (LIBUSB_ERROR_INVALID_PARAM);
485 if ((length != 0) && (data == NULL))
486 return (LIBUSB_ERROR_INVALID_PARAM);
488 xfer = libusb_alloc_transfer(0);
490 return (LIBUSB_ERROR_NO_MEM);
492 ctx = libusb_get_device(devh)->ctx;
494 xfer->dev_handle = devh;
495 xfer->endpoint = endpoint;
497 xfer->timeout = timeout;
499 xfer->length = length;
500 xfer->user_data = (void *)&complet;
501 xfer->callback = libusb10_do_transfer_cb;
504 if ((ret = libusb_submit_transfer(xfer)) < 0) {
505 libusb_free_transfer(xfer);
508 while (complet == 0) {
509 if ((ret = libusb_handle_events(ctx)) < 0) {
510 libusb_cancel_transfer(xfer);
511 usleep(1000); /* nice it */
515 *transferred = xfer->actual_length;
517 switch (xfer->status) {
518 case LIBUSB_TRANSFER_COMPLETED:
521 case LIBUSB_TRANSFER_TIMED_OUT:
522 ret = LIBUSB_ERROR_TIMEOUT;
524 case LIBUSB_TRANSFER_OVERFLOW:
525 ret = LIBUSB_ERROR_OVERFLOW;
527 case LIBUSB_TRANSFER_STALL:
528 ret = LIBUSB_ERROR_PIPE;
530 case LIBUSB_TRANSFER_NO_DEVICE:
531 ret = LIBUSB_ERROR_NO_DEVICE;
534 ret = LIBUSB_ERROR_OTHER;
538 libusb_free_transfer(xfer);
543 libusb_bulk_transfer(libusb_device_handle *devh,
544 uint8_t endpoint, uint8_t *data, int length,
545 int *transferred, unsigned int timeout)
550 ctx = GET_CONTEXT(NULL);
551 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_bulk_transfer enter");
553 ret = libusb10_do_transfer(devh, endpoint, data, length, transferred,
554 timeout, LIBUSB_TRANSFER_TYPE_BULK);
556 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_bulk_transfer leave");
561 libusb_interrupt_transfer(libusb_device_handle *devh,
562 uint8_t endpoint, uint8_t *data, int length,
563 int *transferred, unsigned int timeout)
568 ctx = GET_CONTEXT(NULL);
569 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_interrupt_transfer enter");
571 ret = libusb10_do_transfer(devh, endpoint, data, length, transferred,
572 timeout, LIBUSB_TRANSFER_TYPE_INTERRUPT);
574 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_interrupt_transfer leave");
579 libusb_get_iso_packet_buffer(struct libusb_transfer *transfer, uint32_t index)
584 if (transfer->num_iso_packets < 0)
587 if (index >= (uint32_t)transfer->num_iso_packets)
590 ptr = transfer->buffer;
594 for (n = 0; n != index; n++) {
595 ptr += transfer->iso_packet_desc[n].length;
601 libusb_get_iso_packet_buffer_simple(struct libusb_transfer *transfer, uint32_t index)
605 if (transfer->num_iso_packets < 0)
608 if (index >= (uint32_t)transfer->num_iso_packets)
611 ptr = transfer->buffer;
615 ptr += transfer->iso_packet_desc[0].length * index;
621 libusb_set_iso_packet_lengths(struct libusb_transfer *transfer, uint32_t length)
625 if (transfer->num_iso_packets < 0)
628 for (n = 0; n != transfer->num_iso_packets; n++)
629 transfer->iso_packet_desc[n].length = length;
633 libusb_control_transfer_get_data(struct libusb_transfer *transfer)
635 if (transfer->buffer == NULL)
638 return (transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE);
641 struct libusb_control_setup *
642 libusb_control_transfer_get_setup(struct libusb_transfer *transfer)
644 return ((struct libusb_control_setup *)transfer->buffer);
648 libusb_fill_control_setup(uint8_t *buf, uint8_t bmRequestType,
649 uint8_t bRequest, uint16_t wValue,
650 uint16_t wIndex, uint16_t wLength)
652 struct libusb_control_setup *req = (struct libusb_control_setup *)buf;
654 /* The alignment is OK for all fields below. */
655 req->bmRequestType = bmRequestType;
656 req->bRequest = bRequest;
657 req->wValue = htole16(wValue);
658 req->wIndex = htole16(wIndex);
659 req->wLength = htole16(wLength);
663 libusb_fill_control_transfer(struct libusb_transfer *transfer,
664 libusb_device_handle *devh, uint8_t *buf,
665 libusb_transfer_cb_fn callback, void *user_data,
668 struct libusb_control_setup *setup = (struct libusb_control_setup *)buf;
670 transfer->dev_handle = devh;
671 transfer->endpoint = 0;
672 transfer->type = LIBUSB_TRANSFER_TYPE_CONTROL;
673 transfer->timeout = timeout;
674 transfer->buffer = buf;
676 transfer->length = LIBUSB_CONTROL_SETUP_SIZE
677 + le16toh(setup->wLength);
679 transfer->length = 0;
680 transfer->user_data = user_data;
681 transfer->callback = callback;
686 libusb_fill_bulk_transfer(struct libusb_transfer *transfer,
687 libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf,
688 int length, libusb_transfer_cb_fn callback, void *user_data,
691 transfer->dev_handle = devh;
692 transfer->endpoint = endpoint;
693 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
694 transfer->timeout = timeout;
695 transfer->buffer = buf;
696 transfer->length = length;
697 transfer->user_data = user_data;
698 transfer->callback = callback;
702 libusb_fill_interrupt_transfer(struct libusb_transfer *transfer,
703 libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf,
704 int length, libusb_transfer_cb_fn callback, void *user_data,
707 transfer->dev_handle = devh;
708 transfer->endpoint = endpoint;
709 transfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT;
710 transfer->timeout = timeout;
711 transfer->buffer = buf;
712 transfer->length = length;
713 transfer->user_data = user_data;
714 transfer->callback = callback;
718 libusb_fill_iso_transfer(struct libusb_transfer *transfer,
719 libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf,
720 int length, int npacket, libusb_transfer_cb_fn callback,
721 void *user_data, uint32_t timeout)
723 transfer->dev_handle = devh;
724 transfer->endpoint = endpoint;
725 transfer->type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS;
726 transfer->timeout = timeout;
727 transfer->buffer = buf;
728 transfer->length = length;
729 transfer->num_iso_packets = npacket;
730 transfer->user_data = user_data;
731 transfer->callback = callback;