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 #define libusb_device_handle libusb20_device
40 #include "libusb20_desc.h"
41 #include "libusb20_int.h"
46 libusb10_add_pollfd(libusb_context *ctx, struct libusb_super_pollfd *pollfd,
47 struct libusb20_device *pdev, int fd, short events)
52 if (pollfd->entry.tqe_prev != NULL)
53 return; /* already queued */
59 pollfd->pollfd.fd = fd;
60 pollfd->pollfd.events = events;
63 TAILQ_INSERT_TAIL(&ctx->pollfds, pollfd, entry);
67 ctx->fd_added_cb(fd, events, ctx->fd_cb_user_data);
71 libusb10_remove_pollfd(libusb_context *ctx, struct libusb_super_pollfd *pollfd)
76 if (pollfd->entry.tqe_prev == NULL)
77 return; /* already dequeued */
80 TAILQ_REMOVE(&ctx->pollfds, pollfd, entry);
81 pollfd->entry.tqe_prev = NULL;
84 if (ctx->fd_removed_cb)
85 ctx->fd_removed_cb(pollfd->pollfd.fd, ctx->fd_cb_user_data);
88 /* This function must be called locked */
91 libusb10_handle_events_sub(struct libusb_context *ctx, struct timeval *tv)
93 struct libusb_device *dev;
94 struct libusb20_device **ppdev;
95 struct libusb_super_pollfd *pfd;
97 struct libusb_super_transfer *sxfer;
98 struct libusb_transfer *uxfer;
104 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb10_handle_events_sub enter");
108 TAILQ_FOREACH(pfd, &ctx->pollfds, entry)
111 fds = alloca(sizeof(*fds) * nfds);
113 return (LIBUSB_ERROR_NO_MEM);
115 ppdev = alloca(sizeof(*ppdev) * nfds);
117 return (LIBUSB_ERROR_NO_MEM);
119 TAILQ_FOREACH(pfd, &ctx->pollfds, entry) {
120 fds[i].fd = pfd->pollfd.fd;
121 fds[i].events = pfd->pollfd.events;
123 ppdev[i] = pfd->pdev;
124 if (pfd->pdev != NULL)
125 libusb_get_device(pfd->pdev)->refcnt++;
132 timeout = (tv->tv_sec * 1000) + ((tv->tv_usec + 999) / 1000);
135 err = poll(fds, nfds, timeout);
138 if ((err == -1) && (errno == EINTR))
139 err = LIBUSB_ERROR_INTERRUPTED;
141 err = LIBUSB_ERROR_IO;
144 for (i = 0; i != (int)nfds; i++) {
145 if (ppdev[i] != NULL) {
147 libusb_unref_device(libusb_get_device(ppdev[i]));
153 for (i = 0; i != (int)nfds; i++) {
154 if (ppdev[i] != NULL) {
155 dev = libusb_get_device(ppdev[i]);
157 if (fds[i].revents != 0) {
158 err = libusb20_dev_process(ppdev[i]);
161 /* set device is gone */
162 dev->device_is_gone = 1;
164 /* remove USB device from polling loop */
165 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
167 /* cancel all pending transfers */
168 libusb10_cancel_all_transfer_locked(ppdev[i], dev);
172 libusb_unref_device(dev);
178 while (read(fds[i].fd, &dummy, 1) == 1)
187 /* Do all done callbacks */
189 while ((sxfer = TAILQ_FIRST(&ctx->tr_done))) {
192 TAILQ_REMOVE(&ctx->tr_done, sxfer, entry);
193 sxfer->entry.tqe_prev = NULL;
199 uxfer = (struct libusb_transfer *)(
200 ((uint8_t *)sxfer) + sizeof(*sxfer));
202 /* Allow the callback to free the transfer itself. */
203 flags = uxfer->flags;
205 if (uxfer->callback != NULL)
206 (uxfer->callback) (uxfer);
208 /* Check if the USB transfer should be automatically freed. */
209 if (flags & LIBUSB_TRANSFER_FREE_TRANSFER)
210 libusb_free_transfer(uxfer);
218 /* Wakeup other waiters */
219 pthread_cond_broadcast(&ctx->ctx_cond);
224 /* Polling and timing */
227 libusb_try_lock_events(libusb_context *ctx)
231 ctx = GET_CONTEXT(ctx);
235 err = CTX_TRYLOCK(ctx);
239 err = (ctx->ctx_handler != NO_THREAD);
243 ctx->ctx_handler = pthread_self();
249 libusb_lock_events(libusb_context *ctx)
251 ctx = GET_CONTEXT(ctx);
253 if (ctx->ctx_handler == NO_THREAD)
254 ctx->ctx_handler = pthread_self();
258 libusb_unlock_events(libusb_context *ctx)
260 ctx = GET_CONTEXT(ctx);
261 if (ctx->ctx_handler == pthread_self()) {
262 ctx->ctx_handler = NO_THREAD;
263 pthread_cond_broadcast(&ctx->ctx_cond);
269 libusb_event_handling_ok(libusb_context *ctx)
271 ctx = GET_CONTEXT(ctx);
272 return (ctx->ctx_handler == pthread_self());
276 libusb_event_handler_active(libusb_context *ctx)
278 ctx = GET_CONTEXT(ctx);
279 return (ctx->ctx_handler != NO_THREAD);
283 libusb_lock_event_waiters(libusb_context *ctx)
285 ctx = GET_CONTEXT(ctx);
290 libusb_unlock_event_waiters(libusb_context *ctx)
292 ctx = GET_CONTEXT(ctx);
297 libusb_wait_for_event(libusb_context *ctx, struct timeval *tv)
302 ctx = GET_CONTEXT(ctx);
303 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_wait_for_event enter");
306 pthread_cond_wait(&ctx->ctx_cond,
310 err = clock_gettime(CLOCK_MONOTONIC, &ts);
312 return (LIBUSB_ERROR_OTHER);
315 * The "tv" arguments points to a relative time structure and
316 * not an absolute time structure.
318 ts.tv_sec += tv->tv_sec;
319 ts.tv_nsec += tv->tv_usec * 1000;
320 if (ts.tv_nsec >= 1000000000) {
321 ts.tv_nsec -= 1000000000;
324 err = pthread_cond_timedwait(&ctx->ctx_cond,
325 &ctx->ctx_lock, &ts);
327 if (err == ETIMEDOUT)
334 libusb_handle_events_timeout_completed(libusb_context *ctx,
335 struct timeval *tv, int *completed)
339 ctx = GET_CONTEXT(ctx);
341 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_timeout_completed enter");
343 libusb_lock_events(ctx);
346 if (completed != NULL) {
347 if (*completed != 0 || err != 0)
350 err = libusb_handle_events_locked(ctx, tv);
351 if (completed == NULL)
355 libusb_unlock_events(ctx);
357 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_timeout_completed exit");
363 libusb_handle_events_completed(libusb_context *ctx, int *completed)
365 return (libusb_handle_events_timeout_completed(ctx, NULL, completed));
369 libusb_handle_events_timeout(libusb_context *ctx, struct timeval *tv)
371 return (libusb_handle_events_timeout_completed(ctx, tv, NULL));
375 libusb_handle_events(libusb_context *ctx)
377 return (libusb_handle_events_timeout_completed(ctx, NULL, NULL));
381 libusb_handle_events_locked(libusb_context *ctx, struct timeval *tv)
385 ctx = GET_CONTEXT(ctx);
387 if (libusb_event_handling_ok(ctx)) {
388 err = libusb10_handle_events_sub(ctx, tv);
390 err = libusb_wait_for_event(ctx, tv);
392 err = LIBUSB_ERROR_TIMEOUT;
398 libusb_get_next_timeout(libusb_context *ctx, struct timeval *tv)
400 /* all timeouts are currently being done by the kernel */
406 libusb_set_pollfd_notifiers(libusb_context *ctx,
407 libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb,
410 ctx = GET_CONTEXT(ctx);
412 ctx->fd_added_cb = added_cb;
413 ctx->fd_removed_cb = removed_cb;
414 ctx->fd_cb_user_data = user_data;
417 const struct libusb_pollfd **
418 libusb_get_pollfds(libusb_context *ctx)
420 struct libusb_super_pollfd *pollfd;
424 ctx = GET_CONTEXT(ctx);
429 TAILQ_FOREACH(pollfd, &ctx->pollfds, entry)
432 ret = calloc(i + 1, sizeof(struct libusb_pollfd *));
437 TAILQ_FOREACH(pollfd, &ctx->pollfds, entry)
438 ret[i++] = &pollfd->pollfd;
443 return ((const struct libusb_pollfd **)ret);
447 /* Synchronous device I/O */
450 libusb_control_transfer(libusb_device_handle *devh,
451 uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
452 uint8_t *data, uint16_t wLength, unsigned int timeout)
454 struct LIBUSB20_CONTROL_SETUP_DECODED req;
459 return (LIBUSB_ERROR_INVALID_PARAM);
461 if ((wLength != 0) && (data == NULL))
462 return (LIBUSB_ERROR_INVALID_PARAM);
464 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req);
466 req.bmRequestType = bmRequestType;
467 req.bRequest = bRequest;
470 req.wLength = wLength;
472 err = libusb20_dev_request_sync(devh, &req, data,
473 &actlen, timeout, 0);
475 if (err == LIBUSB20_ERROR_PIPE)
476 return (LIBUSB_ERROR_PIPE);
477 else if (err == LIBUSB20_ERROR_TIMEOUT)
478 return (LIBUSB_ERROR_TIMEOUT);
480 return (LIBUSB_ERROR_NO_DEVICE);
485 static libusb_context *
486 libusb10_get_context_by_device_handle(libusb_device_handle *devh)
491 ctx = libusb_get_device(devh)->ctx;
495 return (GET_CONTEXT(ctx));
499 libusb10_do_transfer_cb(struct libusb_transfer *transfer)
504 ctx = libusb10_get_context_by_device_handle(transfer->dev_handle);
506 DPRINTF(ctx, LIBUSB_DEBUG_TRANSFER, "sync I/O done");
508 pdone = transfer->user_data;
513 * TODO: Replace the following function. Allocating and freeing on a
514 * per-transfer basis is slow. --HPS
517 libusb10_do_transfer(libusb_device_handle *devh,
518 uint8_t endpoint, uint8_t *data, int length,
519 int *transferred, unsigned int timeout, int type)
522 struct libusb_transfer *xfer;
527 return (LIBUSB_ERROR_INVALID_PARAM);
529 if ((length != 0) && (data == NULL))
530 return (LIBUSB_ERROR_INVALID_PARAM);
532 xfer = libusb_alloc_transfer(0);
534 return (LIBUSB_ERROR_NO_MEM);
536 ctx = libusb_get_device(devh)->ctx;
538 xfer->dev_handle = devh;
539 xfer->endpoint = endpoint;
541 xfer->timeout = timeout;
543 xfer->length = length;
544 xfer->user_data = (void *)&done;
545 xfer->callback = libusb10_do_transfer_cb;
548 if ((ret = libusb_submit_transfer(xfer)) < 0) {
549 libusb_free_transfer(xfer);
553 if ((ret = libusb_handle_events(ctx)) < 0) {
554 libusb_cancel_transfer(xfer);
555 usleep(1000); /* nice it */
559 *transferred = xfer->actual_length;
561 switch (xfer->status) {
562 case LIBUSB_TRANSFER_COMPLETED:
565 case LIBUSB_TRANSFER_TIMED_OUT:
566 ret = LIBUSB_ERROR_TIMEOUT;
568 case LIBUSB_TRANSFER_OVERFLOW:
569 ret = LIBUSB_ERROR_OVERFLOW;
571 case LIBUSB_TRANSFER_STALL:
572 ret = LIBUSB_ERROR_PIPE;
574 case LIBUSB_TRANSFER_NO_DEVICE:
575 ret = LIBUSB_ERROR_NO_DEVICE;
578 ret = LIBUSB_ERROR_OTHER;
582 libusb_free_transfer(xfer);
587 libusb_bulk_transfer(libusb_device_handle *devh,
588 uint8_t endpoint, uint8_t *data, int length,
589 int *transferred, unsigned int timeout)
594 ctx = libusb10_get_context_by_device_handle(devh);
596 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_bulk_transfer enter");
598 ret = libusb10_do_transfer(devh, endpoint, data, length, transferred,
599 timeout, LIBUSB_TRANSFER_TYPE_BULK);
601 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_bulk_transfer leave");
606 libusb_interrupt_transfer(libusb_device_handle *devh,
607 uint8_t endpoint, uint8_t *data, int length,
608 int *transferred, unsigned int timeout)
613 ctx = libusb10_get_context_by_device_handle(devh);
615 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_interrupt_transfer enter");
617 ret = libusb10_do_transfer(devh, endpoint, data, length, transferred,
618 timeout, LIBUSB_TRANSFER_TYPE_INTERRUPT);
620 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_interrupt_transfer leave");
625 libusb_get_iso_packet_buffer(struct libusb_transfer *transfer, uint32_t off)
630 if (transfer->num_iso_packets < 0)
633 if (off >= (uint32_t)transfer->num_iso_packets)
636 ptr = transfer->buffer;
640 for (n = 0; n != off; n++) {
641 ptr += transfer->iso_packet_desc[n].length;
647 libusb_get_iso_packet_buffer_simple(struct libusb_transfer *transfer, uint32_t off)
651 if (transfer->num_iso_packets < 0)
654 if (off >= (uint32_t)transfer->num_iso_packets)
657 ptr = transfer->buffer;
661 ptr += transfer->iso_packet_desc[0].length * off;
667 libusb_set_iso_packet_lengths(struct libusb_transfer *transfer, uint32_t length)
671 if (transfer->num_iso_packets < 0)
674 for (n = 0; n != transfer->num_iso_packets; n++)
675 transfer->iso_packet_desc[n].length = length;
679 libusb_control_transfer_get_data(struct libusb_transfer *transfer)
681 if (transfer->buffer == NULL)
684 return (transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE);
687 struct libusb_control_setup *
688 libusb_control_transfer_get_setup(struct libusb_transfer *transfer)
690 return ((struct libusb_control_setup *)transfer->buffer);
694 libusb_fill_control_setup(uint8_t *buf, uint8_t bmRequestType,
695 uint8_t bRequest, uint16_t wValue,
696 uint16_t wIndex, uint16_t wLength)
698 struct libusb_control_setup *req = (struct libusb_control_setup *)buf;
700 /* The alignment is OK for all fields below. */
701 req->bmRequestType = bmRequestType;
702 req->bRequest = bRequest;
703 req->wValue = htole16(wValue);
704 req->wIndex = htole16(wIndex);
705 req->wLength = htole16(wLength);
709 libusb_fill_control_transfer(struct libusb_transfer *transfer,
710 libusb_device_handle *devh, uint8_t *buf,
711 libusb_transfer_cb_fn callback, void *user_data,
714 struct libusb_control_setup *setup = (struct libusb_control_setup *)buf;
716 transfer->dev_handle = devh;
717 transfer->endpoint = 0;
718 transfer->type = LIBUSB_TRANSFER_TYPE_CONTROL;
719 transfer->timeout = timeout;
720 transfer->buffer = buf;
722 transfer->length = LIBUSB_CONTROL_SETUP_SIZE
723 + le16toh(setup->wLength);
725 transfer->length = 0;
726 transfer->user_data = user_data;
727 transfer->callback = callback;
732 libusb_fill_bulk_transfer(struct libusb_transfer *transfer,
733 libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf,
734 int length, libusb_transfer_cb_fn callback, void *user_data,
737 transfer->dev_handle = devh;
738 transfer->endpoint = endpoint;
739 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
740 transfer->timeout = timeout;
741 transfer->buffer = buf;
742 transfer->length = length;
743 transfer->user_data = user_data;
744 transfer->callback = callback;
748 libusb_fill_interrupt_transfer(struct libusb_transfer *transfer,
749 libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf,
750 int length, libusb_transfer_cb_fn callback, void *user_data,
753 transfer->dev_handle = devh;
754 transfer->endpoint = endpoint;
755 transfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT;
756 transfer->timeout = timeout;
757 transfer->buffer = buf;
758 transfer->length = length;
759 transfer->user_data = user_data;
760 transfer->callback = callback;
764 libusb_fill_iso_transfer(struct libusb_transfer *transfer,
765 libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf,
766 int length, int npacket, libusb_transfer_cb_fn callback,
767 void *user_data, uint32_t timeout)
769 transfer->dev_handle = devh;
770 transfer->endpoint = endpoint;
771 transfer->type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS;
772 transfer->timeout = timeout;
773 transfer->buffer = buf;
774 transfer->length = length;
775 transfer->num_iso_packets = npacket;
776 transfer->user_data = user_data;
777 transfer->callback = callback;
781 libusb_alloc_streams(libusb_device_handle *dev, uint32_t num_streams,
782 unsigned char *endpoints, int num_endpoints)
785 return (LIBUSB_ERROR_NOT_SUPPORTED);
789 libusb_free_streams(libusb_device_handle *dev, unsigned char *endpoints, int num_endpoints)
796 libusb_transfer_set_stream_id(struct libusb_transfer *transfer, uint32_t stream_id)
798 struct libusb_super_transfer *sxfer;
800 if (transfer == NULL)
803 sxfer = (struct libusb_super_transfer *)(
804 ((uint8_t *)transfer) - sizeof(*sxfer));
807 sxfer->stream_id = stream_id;
811 libusb_transfer_get_stream_id(struct libusb_transfer *transfer)
813 struct libusb_super_transfer *sxfer;
815 if (transfer == NULL)
818 sxfer = (struct libusb_super_transfer *)(
819 ((uint8_t *)transfer) - sizeof(*sxfer));
822 return (sxfer->stream_id);