3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 * Copyright (c) 2009 Sylvestre Gallon. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #ifdef LIBUSB_GLOBAL_INCLUDE_FILE
30 #include LIBUSB_GLOBAL_INCLUDE_FILE
40 #include <sys/queue.h>
41 #include <sys/endian.h>
44 #define libusb_device_handle libusb20_device
47 #include "libusb20_desc.h"
48 #include "libusb20_int.h"
53 libusb10_add_pollfd(libusb_context *ctx, struct libusb_super_pollfd *pollfd,
54 struct libusb20_device *pdev, int fd, short events)
59 if (pollfd->entry.tqe_prev != NULL)
60 return; /* already queued */
66 pollfd->pollfd.fd = fd;
67 pollfd->pollfd.events = events;
70 TAILQ_INSERT_TAIL(&ctx->pollfds, pollfd, entry);
74 ctx->fd_added_cb(fd, events, ctx->fd_cb_user_data);
78 libusb10_remove_pollfd(libusb_context *ctx, struct libusb_super_pollfd *pollfd)
83 if (pollfd->entry.tqe_prev == NULL)
84 return; /* already dequeued */
87 TAILQ_REMOVE(&ctx->pollfds, pollfd, entry);
88 pollfd->entry.tqe_prev = NULL;
91 if (ctx->fd_removed_cb)
92 ctx->fd_removed_cb(pollfd->pollfd.fd, ctx->fd_cb_user_data);
95 /* This function must be called locked */
98 libusb10_handle_events_sub(struct libusb_context *ctx, struct timeval *tv)
100 struct libusb_device *dev;
101 struct libusb20_device **ppdev;
102 struct libusb_super_pollfd *pfd;
104 struct libusb_super_transfer *sxfer;
105 struct libusb_transfer *uxfer;
111 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb10_handle_events_sub enter");
115 TAILQ_FOREACH(pfd, &ctx->pollfds, entry)
118 fds = alloca(sizeof(*fds) * nfds);
120 return (LIBUSB_ERROR_NO_MEM);
122 ppdev = alloca(sizeof(*ppdev) * nfds);
124 return (LIBUSB_ERROR_NO_MEM);
126 TAILQ_FOREACH(pfd, &ctx->pollfds, entry) {
127 fds[i].fd = pfd->pollfd.fd;
128 fds[i].events = pfd->pollfd.events;
130 ppdev[i] = pfd->pdev;
131 if (pfd->pdev != NULL)
132 libusb_get_device(pfd->pdev)->refcnt++;
139 timeout = (tv->tv_sec * 1000) + ((tv->tv_usec + 999) / 1000);
142 err = poll(fds, nfds, timeout);
145 if ((err == -1) && (errno == EINTR))
146 err = LIBUSB_ERROR_INTERRUPTED;
148 err = LIBUSB_ERROR_IO;
151 for (i = 0; i != (int)nfds; i++) {
152 if (ppdev[i] != NULL) {
154 libusb_unref_device(libusb_get_device(ppdev[i]));
160 for (i = 0; i != (int)nfds; i++) {
161 if (ppdev[i] != NULL) {
162 dev = libusb_get_device(ppdev[i]);
164 if (fds[i].revents != 0) {
165 err = libusb20_dev_process(ppdev[i]);
169 * When the device is opened
170 * set the "device_is_gone"
171 * flag. This prevents the
172 * client from submitting new
173 * USB transfers to a detached
176 if (ppdev[i]->is_opened)
177 dev->device_is_gone = 1;
179 /* remove USB device from polling loop */
180 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
182 /* cancel all pending transfers */
183 libusb10_cancel_all_transfer_locked(ppdev[i], dev);
187 libusb_unref_device(dev);
193 while (read(fds[i].fd, &dummy, 1) == 1)
202 /* Do all done callbacks */
204 while ((sxfer = TAILQ_FIRST(&ctx->tr_done))) {
207 TAILQ_REMOVE(&ctx->tr_done, sxfer, entry);
208 sxfer->entry.tqe_prev = NULL;
214 uxfer = (struct libusb_transfer *)(
215 ((uint8_t *)sxfer) + sizeof(*sxfer));
217 /* Allow the callback to free the transfer itself. */
218 flags = uxfer->flags;
220 if (uxfer->callback != NULL)
221 (uxfer->callback) (uxfer);
223 /* Check if the USB transfer should be automatically freed. */
224 if (flags & LIBUSB_TRANSFER_FREE_TRANSFER)
225 libusb_free_transfer(uxfer);
233 /* Wakeup other waiters */
234 pthread_cond_broadcast(&ctx->ctx_cond);
239 /* Polling and timing */
242 libusb_try_lock_events(libusb_context *ctx)
246 ctx = GET_CONTEXT(ctx);
250 err = CTX_TRYLOCK(ctx);
254 err = (ctx->ctx_handler != NO_THREAD);
258 ctx->ctx_handler = pthread_self();
264 libusb_lock_events(libusb_context *ctx)
266 ctx = GET_CONTEXT(ctx);
268 if (ctx->ctx_handler == NO_THREAD)
269 ctx->ctx_handler = pthread_self();
273 libusb_unlock_events(libusb_context *ctx)
275 ctx = GET_CONTEXT(ctx);
276 if (ctx->ctx_handler == pthread_self()) {
277 ctx->ctx_handler = NO_THREAD;
278 pthread_cond_broadcast(&ctx->ctx_cond);
284 libusb_event_handling_ok(libusb_context *ctx)
286 ctx = GET_CONTEXT(ctx);
287 return (ctx->ctx_handler == pthread_self());
291 libusb_event_handler_active(libusb_context *ctx)
293 ctx = GET_CONTEXT(ctx);
294 return (ctx->ctx_handler != NO_THREAD);
298 libusb_lock_event_waiters(libusb_context *ctx)
300 ctx = GET_CONTEXT(ctx);
305 libusb_unlock_event_waiters(libusb_context *ctx)
307 ctx = GET_CONTEXT(ctx);
312 libusb_wait_for_event(libusb_context *ctx, struct timeval *tv)
317 ctx = GET_CONTEXT(ctx);
318 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_wait_for_event enter");
321 pthread_cond_wait(&ctx->ctx_cond,
323 /* try to grab polling of actual events, if any */
324 if (ctx->ctx_handler == NO_THREAD)
325 ctx->ctx_handler = pthread_self();
328 err = clock_gettime(CLOCK_MONOTONIC, &ts);
330 return (LIBUSB_ERROR_OTHER);
333 * The "tv" arguments points to a relative time structure and
334 * not an absolute time structure.
336 ts.tv_sec += tv->tv_sec;
337 ts.tv_nsec += tv->tv_usec * 1000;
338 if (ts.tv_nsec >= 1000000000) {
339 ts.tv_nsec -= 1000000000;
342 err = pthread_cond_timedwait(&ctx->ctx_cond,
343 &ctx->ctx_lock, &ts);
344 /* try to grab polling of actual events, if any */
345 if (ctx->ctx_handler == NO_THREAD)
346 ctx->ctx_handler = pthread_self();
348 if (err == ETIMEDOUT)
355 libusb_handle_events_timeout_completed(libusb_context *ctx,
356 struct timeval *tv, int *completed)
360 ctx = GET_CONTEXT(ctx);
362 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_timeout_completed enter");
364 libusb_lock_events(ctx);
367 if (completed != NULL) {
368 if (*completed != 0 || err != 0)
371 err = libusb_handle_events_locked(ctx, tv);
372 if (completed == NULL)
376 libusb_unlock_events(ctx);
378 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_timeout_completed exit");
384 libusb_handle_events_completed(libusb_context *ctx, int *completed)
386 return (libusb_handle_events_timeout_completed(ctx, NULL, completed));
390 libusb_handle_events_timeout(libusb_context *ctx, struct timeval *tv)
392 return (libusb_handle_events_timeout_completed(ctx, tv, NULL));
396 libusb_handle_events(libusb_context *ctx)
398 return (libusb_handle_events_timeout_completed(ctx, NULL, NULL));
402 libusb_handle_events_locked(libusb_context *ctx, struct timeval *tv)
406 ctx = GET_CONTEXT(ctx);
408 if (libusb_event_handling_ok(ctx)) {
409 err = libusb10_handle_events_sub(ctx, tv);
411 err = libusb_wait_for_event(ctx, tv);
413 err = LIBUSB_ERROR_TIMEOUT;
419 libusb_get_next_timeout(libusb_context *ctx, struct timeval *tv)
421 /* all timeouts are currently being done by the kernel */
427 libusb_set_pollfd_notifiers(libusb_context *ctx,
428 libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb,
431 ctx = GET_CONTEXT(ctx);
433 ctx->fd_added_cb = added_cb;
434 ctx->fd_removed_cb = removed_cb;
435 ctx->fd_cb_user_data = user_data;
438 const struct libusb_pollfd **
439 libusb_get_pollfds(libusb_context *ctx)
441 struct libusb_super_pollfd *pollfd;
445 ctx = GET_CONTEXT(ctx);
450 TAILQ_FOREACH(pollfd, &ctx->pollfds, entry)
453 ret = calloc(i + 1, sizeof(struct libusb_pollfd *));
458 TAILQ_FOREACH(pollfd, &ctx->pollfds, entry)
459 ret[i++] = &pollfd->pollfd;
464 return ((const struct libusb_pollfd **)ret);
468 /* Synchronous device I/O */
471 libusb_control_transfer(libusb_device_handle *devh,
472 uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
473 uint8_t *data, uint16_t wLength, unsigned int timeout)
475 struct LIBUSB20_CONTROL_SETUP_DECODED req;
480 return (LIBUSB_ERROR_INVALID_PARAM);
482 if ((wLength != 0) && (data == NULL))
483 return (LIBUSB_ERROR_INVALID_PARAM);
485 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req);
487 req.bmRequestType = bmRequestType;
488 req.bRequest = bRequest;
491 req.wLength = wLength;
493 err = libusb20_dev_request_sync(devh, &req, data,
494 &actlen, timeout, 0);
496 if (err == LIBUSB20_ERROR_PIPE)
497 return (LIBUSB_ERROR_PIPE);
498 else if (err == LIBUSB20_ERROR_TIMEOUT)
499 return (LIBUSB_ERROR_TIMEOUT);
501 return (LIBUSB_ERROR_NO_DEVICE);
506 static libusb_context *
507 libusb10_get_context_by_device_handle(libusb_device_handle *devh)
512 ctx = libusb_get_device(devh)->ctx;
516 return (GET_CONTEXT(ctx));
520 libusb10_do_transfer_cb(struct libusb_transfer *transfer)
525 ctx = libusb10_get_context_by_device_handle(transfer->dev_handle);
527 DPRINTF(ctx, LIBUSB_DEBUG_TRANSFER, "sync I/O done");
529 pdone = transfer->user_data;
534 * TODO: Replace the following function. Allocating and freeing on a
535 * per-transfer basis is slow. --HPS
538 libusb10_do_transfer(libusb_device_handle *devh,
539 uint8_t endpoint, uint8_t *data, int length,
540 int *transferred, unsigned int timeout, int type)
543 struct libusb_transfer *xfer;
548 return (LIBUSB_ERROR_INVALID_PARAM);
550 if ((length != 0) && (data == NULL))
551 return (LIBUSB_ERROR_INVALID_PARAM);
553 xfer = libusb_alloc_transfer(0);
555 return (LIBUSB_ERROR_NO_MEM);
557 ctx = libusb_get_device(devh)->ctx;
559 xfer->dev_handle = devh;
560 xfer->endpoint = endpoint;
562 xfer->timeout = timeout;
564 xfer->length = length;
565 xfer->user_data = (void *)&done;
566 xfer->callback = libusb10_do_transfer_cb;
569 if ((ret = libusb_submit_transfer(xfer)) < 0) {
570 libusb_free_transfer(xfer);
574 if ((ret = libusb_handle_events(ctx)) < 0) {
575 libusb_cancel_transfer(xfer);
576 usleep(1000); /* nice it */
580 *transferred = xfer->actual_length;
582 switch (xfer->status) {
583 case LIBUSB_TRANSFER_COMPLETED:
586 case LIBUSB_TRANSFER_TIMED_OUT:
587 ret = LIBUSB_ERROR_TIMEOUT;
589 case LIBUSB_TRANSFER_OVERFLOW:
590 ret = LIBUSB_ERROR_OVERFLOW;
592 case LIBUSB_TRANSFER_STALL:
593 ret = LIBUSB_ERROR_PIPE;
595 case LIBUSB_TRANSFER_NO_DEVICE:
596 ret = LIBUSB_ERROR_NO_DEVICE;
599 ret = LIBUSB_ERROR_OTHER;
603 libusb_free_transfer(xfer);
608 libusb_bulk_transfer(libusb_device_handle *devh,
609 uint8_t endpoint, uint8_t *data, int length,
610 int *transferred, unsigned int timeout)
615 ctx = libusb10_get_context_by_device_handle(devh);
617 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_bulk_transfer enter");
619 ret = libusb10_do_transfer(devh, endpoint, data, length, transferred,
620 timeout, LIBUSB_TRANSFER_TYPE_BULK);
622 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_bulk_transfer leave");
627 libusb_interrupt_transfer(libusb_device_handle *devh,
628 uint8_t endpoint, uint8_t *data, int length,
629 int *transferred, unsigned int timeout)
634 ctx = libusb10_get_context_by_device_handle(devh);
636 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_interrupt_transfer enter");
638 ret = libusb10_do_transfer(devh, endpoint, data, length, transferred,
639 timeout, LIBUSB_TRANSFER_TYPE_INTERRUPT);
641 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_interrupt_transfer leave");
646 libusb_get_iso_packet_buffer(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 for (n = 0; n != off; n++) {
662 ptr += transfer->iso_packet_desc[n].length;
668 libusb_get_iso_packet_buffer_simple(struct libusb_transfer *transfer, uint32_t off)
672 if (transfer->num_iso_packets < 0)
675 if (off >= (uint32_t)transfer->num_iso_packets)
678 ptr = transfer->buffer;
682 ptr += transfer->iso_packet_desc[0].length * off;
688 libusb_set_iso_packet_lengths(struct libusb_transfer *transfer, uint32_t length)
692 if (transfer->num_iso_packets < 0)
695 for (n = 0; n != transfer->num_iso_packets; n++)
696 transfer->iso_packet_desc[n].length = length;
700 libusb_control_transfer_get_data(struct libusb_transfer *transfer)
702 if (transfer->buffer == NULL)
705 return (transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE);
708 struct libusb_control_setup *
709 libusb_control_transfer_get_setup(struct libusb_transfer *transfer)
711 return ((struct libusb_control_setup *)transfer->buffer);
715 libusb_fill_control_setup(uint8_t *buf, uint8_t bmRequestType,
716 uint8_t bRequest, uint16_t wValue,
717 uint16_t wIndex, uint16_t wLength)
719 struct libusb_control_setup *req = (struct libusb_control_setup *)buf;
721 /* The alignment is OK for all fields below. */
722 req->bmRequestType = bmRequestType;
723 req->bRequest = bRequest;
724 req->wValue = htole16(wValue);
725 req->wIndex = htole16(wIndex);
726 req->wLength = htole16(wLength);
730 libusb_fill_control_transfer(struct libusb_transfer *transfer,
731 libusb_device_handle *devh, uint8_t *buf,
732 libusb_transfer_cb_fn callback, void *user_data,
735 struct libusb_control_setup *setup = (struct libusb_control_setup *)buf;
737 transfer->dev_handle = devh;
738 transfer->endpoint = 0;
739 transfer->type = LIBUSB_TRANSFER_TYPE_CONTROL;
740 transfer->timeout = timeout;
741 transfer->buffer = buf;
743 transfer->length = LIBUSB_CONTROL_SETUP_SIZE
744 + le16toh(setup->wLength);
746 transfer->length = 0;
747 transfer->user_data = user_data;
748 transfer->callback = callback;
753 libusb_fill_bulk_transfer(struct libusb_transfer *transfer,
754 libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf,
755 int length, libusb_transfer_cb_fn callback, void *user_data,
758 transfer->dev_handle = devh;
759 transfer->endpoint = endpoint;
760 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
761 transfer->timeout = timeout;
762 transfer->buffer = buf;
763 transfer->length = length;
764 transfer->user_data = user_data;
765 transfer->callback = callback;
769 libusb_fill_interrupt_transfer(struct libusb_transfer *transfer,
770 libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf,
771 int length, libusb_transfer_cb_fn callback, void *user_data,
774 transfer->dev_handle = devh;
775 transfer->endpoint = endpoint;
776 transfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT;
777 transfer->timeout = timeout;
778 transfer->buffer = buf;
779 transfer->length = length;
780 transfer->user_data = user_data;
781 transfer->callback = callback;
785 libusb_fill_iso_transfer(struct libusb_transfer *transfer,
786 libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf,
787 int length, int npacket, libusb_transfer_cb_fn callback,
788 void *user_data, uint32_t timeout)
790 transfer->dev_handle = devh;
791 transfer->endpoint = endpoint;
792 transfer->type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS;
793 transfer->timeout = timeout;
794 transfer->buffer = buf;
795 transfer->length = length;
796 transfer->num_iso_packets = npacket;
797 transfer->user_data = user_data;
798 transfer->callback = callback;
802 libusb_alloc_streams(libusb_device_handle *dev, uint32_t num_streams,
803 unsigned char *endpoints, int num_endpoints)
806 return (LIBUSB_ERROR_INVALID_PARAM);
811 libusb_free_streams(libusb_device_handle *dev, unsigned char *endpoints, int num_endpoints)
818 libusb_transfer_set_stream_id(struct libusb_transfer *transfer, uint32_t stream_id)
820 struct libusb_super_transfer *sxfer;
822 if (transfer == NULL)
825 sxfer = (struct libusb_super_transfer *)(
826 ((uint8_t *)transfer) - sizeof(*sxfer));
829 sxfer->stream_id = stream_id;
833 libusb_transfer_get_stream_id(struct libusb_transfer *transfer)
835 struct libusb_super_transfer *sxfer;
837 if (transfer == NULL)
840 sxfer = (struct libusb_super_transfer *)(
841 ((uint8_t *)transfer) - sizeof(*sxfer));
844 return (sxfer->stream_id);