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 #ifdef LIBUSB_GLOBAL_INCLUDE_FILE
28 #include LIBUSB_GLOBAL_INCLUDE_FILE
38 #include <sys/queue.h>
39 #include <sys/endian.h>
42 #define libusb_device_handle libusb20_device
45 #include "libusb20_desc.h"
46 #include "libusb20_int.h"
51 libusb10_add_pollfd(libusb_context *ctx, struct libusb_super_pollfd *pollfd,
52 struct libusb20_device *pdev, int fd, short events)
57 if (pollfd->entry.tqe_prev != NULL)
58 return; /* already queued */
64 pollfd->pollfd.fd = fd;
65 pollfd->pollfd.events = events;
68 TAILQ_INSERT_TAIL(&ctx->pollfds, pollfd, entry);
72 ctx->fd_added_cb(fd, events, ctx->fd_cb_user_data);
76 libusb10_remove_pollfd(libusb_context *ctx, struct libusb_super_pollfd *pollfd)
81 if (pollfd->entry.tqe_prev == NULL)
82 return; /* already dequeued */
85 TAILQ_REMOVE(&ctx->pollfds, pollfd, entry);
86 pollfd->entry.tqe_prev = NULL;
89 if (ctx->fd_removed_cb)
90 ctx->fd_removed_cb(pollfd->pollfd.fd, ctx->fd_cb_user_data);
93 /* This function must be called locked */
96 libusb10_handle_events_sub(struct libusb_context *ctx, struct timeval *tv)
98 struct libusb_device *dev;
99 struct libusb20_device **ppdev;
100 struct libusb_super_pollfd *pfd;
102 struct libusb_super_transfer *sxfer;
103 struct libusb_transfer *uxfer;
109 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb10_handle_events_sub enter");
113 TAILQ_FOREACH(pfd, &ctx->pollfds, entry)
116 fds = alloca(sizeof(*fds) * nfds);
118 return (LIBUSB_ERROR_NO_MEM);
120 ppdev = alloca(sizeof(*ppdev) * nfds);
122 return (LIBUSB_ERROR_NO_MEM);
124 TAILQ_FOREACH(pfd, &ctx->pollfds, entry) {
125 fds[i].fd = pfd->pollfd.fd;
126 fds[i].events = pfd->pollfd.events;
128 ppdev[i] = pfd->pdev;
129 if (pfd->pdev != NULL)
130 libusb_get_device(pfd->pdev)->refcnt++;
137 timeout = (tv->tv_sec * 1000) + ((tv->tv_usec + 999) / 1000);
140 err = poll(fds, nfds, timeout);
143 if ((err == -1) && (errno == EINTR))
144 err = LIBUSB_ERROR_INTERRUPTED;
146 err = LIBUSB_ERROR_IO;
149 for (i = 0; i != (int)nfds; i++) {
150 if (ppdev[i] != NULL) {
152 libusb_unref_device(libusb_get_device(ppdev[i]));
158 for (i = 0; i != (int)nfds; i++) {
159 if (ppdev[i] != NULL) {
160 dev = libusb_get_device(ppdev[i]);
162 if (fds[i].revents != 0) {
163 err = libusb20_dev_process(ppdev[i]);
166 /* set device is gone */
167 dev->device_is_gone = 1;
169 /* remove USB device from polling loop */
170 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
172 /* cancel all pending transfers */
173 libusb10_cancel_all_transfer_locked(ppdev[i], dev);
177 libusb_unref_device(dev);
183 while (read(fds[i].fd, &dummy, 1) == 1)
192 /* Do all done callbacks */
194 while ((sxfer = TAILQ_FIRST(&ctx->tr_done))) {
197 TAILQ_REMOVE(&ctx->tr_done, sxfer, entry);
198 sxfer->entry.tqe_prev = NULL;
204 uxfer = (struct libusb_transfer *)(
205 ((uint8_t *)sxfer) + sizeof(*sxfer));
207 /* Allow the callback to free the transfer itself. */
208 flags = uxfer->flags;
210 if (uxfer->callback != NULL)
211 (uxfer->callback) (uxfer);
213 /* Check if the USB transfer should be automatically freed. */
214 if (flags & LIBUSB_TRANSFER_FREE_TRANSFER)
215 libusb_free_transfer(uxfer);
223 /* Wakeup other waiters */
224 pthread_cond_broadcast(&ctx->ctx_cond);
229 /* Polling and timing */
232 libusb_try_lock_events(libusb_context *ctx)
236 ctx = GET_CONTEXT(ctx);
240 err = CTX_TRYLOCK(ctx);
244 err = (ctx->ctx_handler != NO_THREAD);
248 ctx->ctx_handler = pthread_self();
254 libusb_lock_events(libusb_context *ctx)
256 ctx = GET_CONTEXT(ctx);
258 if (ctx->ctx_handler == NO_THREAD)
259 ctx->ctx_handler = pthread_self();
263 libusb_unlock_events(libusb_context *ctx)
265 ctx = GET_CONTEXT(ctx);
266 if (ctx->ctx_handler == pthread_self()) {
267 ctx->ctx_handler = NO_THREAD;
268 pthread_cond_broadcast(&ctx->ctx_cond);
274 libusb_event_handling_ok(libusb_context *ctx)
276 ctx = GET_CONTEXT(ctx);
277 return (ctx->ctx_handler == pthread_self());
281 libusb_event_handler_active(libusb_context *ctx)
283 ctx = GET_CONTEXT(ctx);
284 return (ctx->ctx_handler != NO_THREAD);
288 libusb_lock_event_waiters(libusb_context *ctx)
290 ctx = GET_CONTEXT(ctx);
295 libusb_unlock_event_waiters(libusb_context *ctx)
297 ctx = GET_CONTEXT(ctx);
302 libusb_wait_for_event(libusb_context *ctx, struct timeval *tv)
307 ctx = GET_CONTEXT(ctx);
308 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_wait_for_event enter");
311 pthread_cond_wait(&ctx->ctx_cond,
313 /* try to grab polling of actual events, if any */
314 if (ctx->ctx_handler == NO_THREAD)
315 ctx->ctx_handler = pthread_self();
318 err = clock_gettime(CLOCK_MONOTONIC, &ts);
320 return (LIBUSB_ERROR_OTHER);
323 * The "tv" arguments points to a relative time structure and
324 * not an absolute time structure.
326 ts.tv_sec += tv->tv_sec;
327 ts.tv_nsec += tv->tv_usec * 1000;
328 if (ts.tv_nsec >= 1000000000) {
329 ts.tv_nsec -= 1000000000;
332 err = pthread_cond_timedwait(&ctx->ctx_cond,
333 &ctx->ctx_lock, &ts);
334 /* try to grab polling of actual events, if any */
335 if (ctx->ctx_handler == NO_THREAD)
336 ctx->ctx_handler = pthread_self();
338 if (err == ETIMEDOUT)
345 libusb_handle_events_timeout_completed(libusb_context *ctx,
346 struct timeval *tv, int *completed)
350 ctx = GET_CONTEXT(ctx);
352 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_timeout_completed enter");
354 libusb_lock_events(ctx);
357 if (completed != NULL) {
358 if (*completed != 0 || err != 0)
361 err = libusb_handle_events_locked(ctx, tv);
362 if (completed == NULL)
366 libusb_unlock_events(ctx);
368 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_timeout_completed exit");
374 libusb_handle_events_completed(libusb_context *ctx, int *completed)
376 return (libusb_handle_events_timeout_completed(ctx, NULL, completed));
380 libusb_handle_events_timeout(libusb_context *ctx, struct timeval *tv)
382 return (libusb_handle_events_timeout_completed(ctx, tv, NULL));
386 libusb_handle_events(libusb_context *ctx)
388 return (libusb_handle_events_timeout_completed(ctx, NULL, NULL));
392 libusb_handle_events_locked(libusb_context *ctx, struct timeval *tv)
396 ctx = GET_CONTEXT(ctx);
398 if (libusb_event_handling_ok(ctx)) {
399 err = libusb10_handle_events_sub(ctx, tv);
401 err = libusb_wait_for_event(ctx, tv);
403 err = LIBUSB_ERROR_TIMEOUT;
409 libusb_get_next_timeout(libusb_context *ctx, struct timeval *tv)
411 /* all timeouts are currently being done by the kernel */
417 libusb_set_pollfd_notifiers(libusb_context *ctx,
418 libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb,
421 ctx = GET_CONTEXT(ctx);
423 ctx->fd_added_cb = added_cb;
424 ctx->fd_removed_cb = removed_cb;
425 ctx->fd_cb_user_data = user_data;
428 const struct libusb_pollfd **
429 libusb_get_pollfds(libusb_context *ctx)
431 struct libusb_super_pollfd *pollfd;
435 ctx = GET_CONTEXT(ctx);
440 TAILQ_FOREACH(pollfd, &ctx->pollfds, entry)
443 ret = calloc(i + 1, sizeof(struct libusb_pollfd *));
448 TAILQ_FOREACH(pollfd, &ctx->pollfds, entry)
449 ret[i++] = &pollfd->pollfd;
454 return ((const struct libusb_pollfd **)ret);
458 /* Synchronous device I/O */
461 libusb_control_transfer(libusb_device_handle *devh,
462 uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
463 uint8_t *data, uint16_t wLength, unsigned int timeout)
465 struct LIBUSB20_CONTROL_SETUP_DECODED req;
470 return (LIBUSB_ERROR_INVALID_PARAM);
472 if ((wLength != 0) && (data == NULL))
473 return (LIBUSB_ERROR_INVALID_PARAM);
475 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req);
477 req.bmRequestType = bmRequestType;
478 req.bRequest = bRequest;
481 req.wLength = wLength;
483 err = libusb20_dev_request_sync(devh, &req, data,
484 &actlen, timeout, 0);
486 if (err == LIBUSB20_ERROR_PIPE)
487 return (LIBUSB_ERROR_PIPE);
488 else if (err == LIBUSB20_ERROR_TIMEOUT)
489 return (LIBUSB_ERROR_TIMEOUT);
491 return (LIBUSB_ERROR_NO_DEVICE);
496 static libusb_context *
497 libusb10_get_context_by_device_handle(libusb_device_handle *devh)
502 ctx = libusb_get_device(devh)->ctx;
506 return (GET_CONTEXT(ctx));
510 libusb10_do_transfer_cb(struct libusb_transfer *transfer)
515 ctx = libusb10_get_context_by_device_handle(transfer->dev_handle);
517 DPRINTF(ctx, LIBUSB_DEBUG_TRANSFER, "sync I/O done");
519 pdone = transfer->user_data;
524 * TODO: Replace the following function. Allocating and freeing on a
525 * per-transfer basis is slow. --HPS
528 libusb10_do_transfer(libusb_device_handle *devh,
529 uint8_t endpoint, uint8_t *data, int length,
530 int *transferred, unsigned int timeout, int type)
533 struct libusb_transfer *xfer;
538 return (LIBUSB_ERROR_INVALID_PARAM);
540 if ((length != 0) && (data == NULL))
541 return (LIBUSB_ERROR_INVALID_PARAM);
543 xfer = libusb_alloc_transfer(0);
545 return (LIBUSB_ERROR_NO_MEM);
547 ctx = libusb_get_device(devh)->ctx;
549 xfer->dev_handle = devh;
550 xfer->endpoint = endpoint;
552 xfer->timeout = timeout;
554 xfer->length = length;
555 xfer->user_data = (void *)&done;
556 xfer->callback = libusb10_do_transfer_cb;
559 if ((ret = libusb_submit_transfer(xfer)) < 0) {
560 libusb_free_transfer(xfer);
564 if ((ret = libusb_handle_events(ctx)) < 0) {
565 libusb_cancel_transfer(xfer);
566 usleep(1000); /* nice it */
570 *transferred = xfer->actual_length;
572 switch (xfer->status) {
573 case LIBUSB_TRANSFER_COMPLETED:
576 case LIBUSB_TRANSFER_TIMED_OUT:
577 ret = LIBUSB_ERROR_TIMEOUT;
579 case LIBUSB_TRANSFER_OVERFLOW:
580 ret = LIBUSB_ERROR_OVERFLOW;
582 case LIBUSB_TRANSFER_STALL:
583 ret = LIBUSB_ERROR_PIPE;
585 case LIBUSB_TRANSFER_NO_DEVICE:
586 ret = LIBUSB_ERROR_NO_DEVICE;
589 ret = LIBUSB_ERROR_OTHER;
593 libusb_free_transfer(xfer);
598 libusb_bulk_transfer(libusb_device_handle *devh,
599 uint8_t endpoint, uint8_t *data, int length,
600 int *transferred, unsigned int timeout)
605 ctx = libusb10_get_context_by_device_handle(devh);
607 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_bulk_transfer enter");
609 ret = libusb10_do_transfer(devh, endpoint, data, length, transferred,
610 timeout, LIBUSB_TRANSFER_TYPE_BULK);
612 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_bulk_transfer leave");
617 libusb_interrupt_transfer(libusb_device_handle *devh,
618 uint8_t endpoint, uint8_t *data, int length,
619 int *transferred, unsigned int timeout)
624 ctx = libusb10_get_context_by_device_handle(devh);
626 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_interrupt_transfer enter");
628 ret = libusb10_do_transfer(devh, endpoint, data, length, transferred,
629 timeout, LIBUSB_TRANSFER_TYPE_INTERRUPT);
631 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_interrupt_transfer leave");
636 libusb_get_iso_packet_buffer(struct libusb_transfer *transfer, uint32_t off)
641 if (transfer->num_iso_packets < 0)
644 if (off >= (uint32_t)transfer->num_iso_packets)
647 ptr = transfer->buffer;
651 for (n = 0; n != off; n++) {
652 ptr += transfer->iso_packet_desc[n].length;
658 libusb_get_iso_packet_buffer_simple(struct libusb_transfer *transfer, uint32_t off)
662 if (transfer->num_iso_packets < 0)
665 if (off >= (uint32_t)transfer->num_iso_packets)
668 ptr = transfer->buffer;
672 ptr += transfer->iso_packet_desc[0].length * off;
678 libusb_set_iso_packet_lengths(struct libusb_transfer *transfer, uint32_t length)
682 if (transfer->num_iso_packets < 0)
685 for (n = 0; n != transfer->num_iso_packets; n++)
686 transfer->iso_packet_desc[n].length = length;
690 libusb_control_transfer_get_data(struct libusb_transfer *transfer)
692 if (transfer->buffer == NULL)
695 return (transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE);
698 struct libusb_control_setup *
699 libusb_control_transfer_get_setup(struct libusb_transfer *transfer)
701 return ((struct libusb_control_setup *)transfer->buffer);
705 libusb_fill_control_setup(uint8_t *buf, uint8_t bmRequestType,
706 uint8_t bRequest, uint16_t wValue,
707 uint16_t wIndex, uint16_t wLength)
709 struct libusb_control_setup *req = (struct libusb_control_setup *)buf;
711 /* The alignment is OK for all fields below. */
712 req->bmRequestType = bmRequestType;
713 req->bRequest = bRequest;
714 req->wValue = htole16(wValue);
715 req->wIndex = htole16(wIndex);
716 req->wLength = htole16(wLength);
720 libusb_fill_control_transfer(struct libusb_transfer *transfer,
721 libusb_device_handle *devh, uint8_t *buf,
722 libusb_transfer_cb_fn callback, void *user_data,
725 struct libusb_control_setup *setup = (struct libusb_control_setup *)buf;
727 transfer->dev_handle = devh;
728 transfer->endpoint = 0;
729 transfer->type = LIBUSB_TRANSFER_TYPE_CONTROL;
730 transfer->timeout = timeout;
731 transfer->buffer = buf;
733 transfer->length = LIBUSB_CONTROL_SETUP_SIZE
734 + le16toh(setup->wLength);
736 transfer->length = 0;
737 transfer->user_data = user_data;
738 transfer->callback = callback;
743 libusb_fill_bulk_transfer(struct libusb_transfer *transfer,
744 libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf,
745 int length, libusb_transfer_cb_fn callback, void *user_data,
748 transfer->dev_handle = devh;
749 transfer->endpoint = endpoint;
750 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
751 transfer->timeout = timeout;
752 transfer->buffer = buf;
753 transfer->length = length;
754 transfer->user_data = user_data;
755 transfer->callback = callback;
759 libusb_fill_interrupt_transfer(struct libusb_transfer *transfer,
760 libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf,
761 int length, libusb_transfer_cb_fn callback, void *user_data,
764 transfer->dev_handle = devh;
765 transfer->endpoint = endpoint;
766 transfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT;
767 transfer->timeout = timeout;
768 transfer->buffer = buf;
769 transfer->length = length;
770 transfer->user_data = user_data;
771 transfer->callback = callback;
775 libusb_fill_iso_transfer(struct libusb_transfer *transfer,
776 libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf,
777 int length, int npacket, libusb_transfer_cb_fn callback,
778 void *user_data, uint32_t timeout)
780 transfer->dev_handle = devh;
781 transfer->endpoint = endpoint;
782 transfer->type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS;
783 transfer->timeout = timeout;
784 transfer->buffer = buf;
785 transfer->length = length;
786 transfer->num_iso_packets = npacket;
787 transfer->user_data = user_data;
788 transfer->callback = callback;
792 libusb_alloc_streams(libusb_device_handle *dev, uint32_t num_streams,
793 unsigned char *endpoints, int num_endpoints)
796 return (LIBUSB_ERROR_INVALID_PARAM);
801 libusb_free_streams(libusb_device_handle *dev, unsigned char *endpoints, int num_endpoints)
808 libusb_transfer_set_stream_id(struct libusb_transfer *transfer, uint32_t stream_id)
810 struct libusb_super_transfer *sxfer;
812 if (transfer == NULL)
815 sxfer = (struct libusb_super_transfer *)(
816 ((uint8_t *)transfer) - sizeof(*sxfer));
819 sxfer->stream_id = stream_id;
823 libusb_transfer_get_stream_id(struct libusb_transfer *transfer)
825 struct libusb_super_transfer *sxfer;
827 if (transfer == NULL)
830 sxfer = (struct libusb_super_transfer *)(
831 ((uint8_t *)transfer) - sizeof(*sxfer));
834 return (sxfer->stream_id);