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]);
168 /* set device is gone */
169 dev->device_is_gone = 1;
171 /* remove USB device from polling loop */
172 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
174 /* cancel all pending transfers */
175 libusb10_cancel_all_transfer_locked(ppdev[i], dev);
179 libusb_unref_device(dev);
185 while (read(fds[i].fd, &dummy, 1) == 1)
194 /* Do all done callbacks */
196 while ((sxfer = TAILQ_FIRST(&ctx->tr_done))) {
199 TAILQ_REMOVE(&ctx->tr_done, sxfer, entry);
200 sxfer->entry.tqe_prev = NULL;
206 uxfer = (struct libusb_transfer *)(
207 ((uint8_t *)sxfer) + sizeof(*sxfer));
209 /* Allow the callback to free the transfer itself. */
210 flags = uxfer->flags;
212 if (uxfer->callback != NULL)
213 (uxfer->callback) (uxfer);
215 /* Check if the USB transfer should be automatically freed. */
216 if (flags & LIBUSB_TRANSFER_FREE_TRANSFER)
217 libusb_free_transfer(uxfer);
225 /* Wakeup other waiters */
226 pthread_cond_broadcast(&ctx->ctx_cond);
231 /* Polling and timing */
234 libusb_try_lock_events(libusb_context *ctx)
238 ctx = GET_CONTEXT(ctx);
242 err = CTX_TRYLOCK(ctx);
246 err = (ctx->ctx_handler != NO_THREAD);
250 ctx->ctx_handler = pthread_self();
256 libusb_lock_events(libusb_context *ctx)
258 ctx = GET_CONTEXT(ctx);
260 if (ctx->ctx_handler == NO_THREAD)
261 ctx->ctx_handler = pthread_self();
265 libusb_unlock_events(libusb_context *ctx)
267 ctx = GET_CONTEXT(ctx);
268 if (ctx->ctx_handler == pthread_self()) {
269 ctx->ctx_handler = NO_THREAD;
270 pthread_cond_broadcast(&ctx->ctx_cond);
276 libusb_event_handling_ok(libusb_context *ctx)
278 ctx = GET_CONTEXT(ctx);
279 return (ctx->ctx_handler == pthread_self());
283 libusb_event_handler_active(libusb_context *ctx)
285 ctx = GET_CONTEXT(ctx);
286 return (ctx->ctx_handler != NO_THREAD);
290 libusb_lock_event_waiters(libusb_context *ctx)
292 ctx = GET_CONTEXT(ctx);
297 libusb_unlock_event_waiters(libusb_context *ctx)
299 ctx = GET_CONTEXT(ctx);
304 libusb_wait_for_event(libusb_context *ctx, struct timeval *tv)
309 ctx = GET_CONTEXT(ctx);
310 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_wait_for_event enter");
313 pthread_cond_wait(&ctx->ctx_cond,
315 /* try to grab polling of actual events, if any */
316 if (ctx->ctx_handler == NO_THREAD)
317 ctx->ctx_handler = pthread_self();
320 err = clock_gettime(CLOCK_MONOTONIC, &ts);
322 return (LIBUSB_ERROR_OTHER);
325 * The "tv" arguments points to a relative time structure and
326 * not an absolute time structure.
328 ts.tv_sec += tv->tv_sec;
329 ts.tv_nsec += tv->tv_usec * 1000;
330 if (ts.tv_nsec >= 1000000000) {
331 ts.tv_nsec -= 1000000000;
334 err = pthread_cond_timedwait(&ctx->ctx_cond,
335 &ctx->ctx_lock, &ts);
336 /* try to grab polling of actual events, if any */
337 if (ctx->ctx_handler == NO_THREAD)
338 ctx->ctx_handler = pthread_self();
340 if (err == ETIMEDOUT)
347 libusb_handle_events_timeout_completed(libusb_context *ctx,
348 struct timeval *tv, int *completed)
352 ctx = GET_CONTEXT(ctx);
354 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_timeout_completed enter");
356 libusb_lock_events(ctx);
359 if (completed != NULL) {
360 if (*completed != 0 || err != 0)
363 err = libusb_handle_events_locked(ctx, tv);
364 if (completed == NULL)
368 libusb_unlock_events(ctx);
370 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_timeout_completed exit");
376 libusb_handle_events_completed(libusb_context *ctx, int *completed)
378 return (libusb_handle_events_timeout_completed(ctx, NULL, completed));
382 libusb_handle_events_timeout(libusb_context *ctx, struct timeval *tv)
384 return (libusb_handle_events_timeout_completed(ctx, tv, NULL));
388 libusb_handle_events(libusb_context *ctx)
390 return (libusb_handle_events_timeout_completed(ctx, NULL, NULL));
394 libusb_handle_events_locked(libusb_context *ctx, struct timeval *tv)
398 ctx = GET_CONTEXT(ctx);
400 if (libusb_event_handling_ok(ctx)) {
401 err = libusb10_handle_events_sub(ctx, tv);
403 err = libusb_wait_for_event(ctx, tv);
405 err = LIBUSB_ERROR_TIMEOUT;
411 libusb_get_next_timeout(libusb_context *ctx, struct timeval *tv)
413 /* all timeouts are currently being done by the kernel */
419 libusb_set_pollfd_notifiers(libusb_context *ctx,
420 libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb,
423 ctx = GET_CONTEXT(ctx);
425 ctx->fd_added_cb = added_cb;
426 ctx->fd_removed_cb = removed_cb;
427 ctx->fd_cb_user_data = user_data;
430 const struct libusb_pollfd **
431 libusb_get_pollfds(libusb_context *ctx)
433 struct libusb_super_pollfd *pollfd;
437 ctx = GET_CONTEXT(ctx);
442 TAILQ_FOREACH(pollfd, &ctx->pollfds, entry)
445 ret = calloc(i + 1, sizeof(struct libusb_pollfd *));
450 TAILQ_FOREACH(pollfd, &ctx->pollfds, entry)
451 ret[i++] = &pollfd->pollfd;
456 return ((const struct libusb_pollfd **)ret);
460 /* Synchronous device I/O */
463 libusb_control_transfer(libusb_device_handle *devh,
464 uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
465 uint8_t *data, uint16_t wLength, unsigned int timeout)
467 struct LIBUSB20_CONTROL_SETUP_DECODED req;
472 return (LIBUSB_ERROR_INVALID_PARAM);
474 if ((wLength != 0) && (data == NULL))
475 return (LIBUSB_ERROR_INVALID_PARAM);
477 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req);
479 req.bmRequestType = bmRequestType;
480 req.bRequest = bRequest;
483 req.wLength = wLength;
485 err = libusb20_dev_request_sync(devh, &req, data,
486 &actlen, timeout, 0);
488 if (err == LIBUSB20_ERROR_PIPE)
489 return (LIBUSB_ERROR_PIPE);
490 else if (err == LIBUSB20_ERROR_TIMEOUT)
491 return (LIBUSB_ERROR_TIMEOUT);
493 return (LIBUSB_ERROR_NO_DEVICE);
498 static libusb_context *
499 libusb10_get_context_by_device_handle(libusb_device_handle *devh)
504 ctx = libusb_get_device(devh)->ctx;
508 return (GET_CONTEXT(ctx));
512 libusb10_do_transfer_cb(struct libusb_transfer *transfer)
517 ctx = libusb10_get_context_by_device_handle(transfer->dev_handle);
519 DPRINTF(ctx, LIBUSB_DEBUG_TRANSFER, "sync I/O done");
521 pdone = transfer->user_data;
526 * TODO: Replace the following function. Allocating and freeing on a
527 * per-transfer basis is slow. --HPS
530 libusb10_do_transfer(libusb_device_handle *devh,
531 uint8_t endpoint, uint8_t *data, int length,
532 int *transferred, unsigned int timeout, int type)
535 struct libusb_transfer *xfer;
540 return (LIBUSB_ERROR_INVALID_PARAM);
542 if ((length != 0) && (data == NULL))
543 return (LIBUSB_ERROR_INVALID_PARAM);
545 xfer = libusb_alloc_transfer(0);
547 return (LIBUSB_ERROR_NO_MEM);
549 ctx = libusb_get_device(devh)->ctx;
551 xfer->dev_handle = devh;
552 xfer->endpoint = endpoint;
554 xfer->timeout = timeout;
556 xfer->length = length;
557 xfer->user_data = (void *)&done;
558 xfer->callback = libusb10_do_transfer_cb;
561 if ((ret = libusb_submit_transfer(xfer)) < 0) {
562 libusb_free_transfer(xfer);
566 if ((ret = libusb_handle_events(ctx)) < 0) {
567 libusb_cancel_transfer(xfer);
568 usleep(1000); /* nice it */
572 *transferred = xfer->actual_length;
574 switch (xfer->status) {
575 case LIBUSB_TRANSFER_COMPLETED:
578 case LIBUSB_TRANSFER_TIMED_OUT:
579 ret = LIBUSB_ERROR_TIMEOUT;
581 case LIBUSB_TRANSFER_OVERFLOW:
582 ret = LIBUSB_ERROR_OVERFLOW;
584 case LIBUSB_TRANSFER_STALL:
585 ret = LIBUSB_ERROR_PIPE;
587 case LIBUSB_TRANSFER_NO_DEVICE:
588 ret = LIBUSB_ERROR_NO_DEVICE;
591 ret = LIBUSB_ERROR_OTHER;
595 libusb_free_transfer(xfer);
600 libusb_bulk_transfer(libusb_device_handle *devh,
601 uint8_t endpoint, uint8_t *data, int length,
602 int *transferred, unsigned int timeout)
607 ctx = libusb10_get_context_by_device_handle(devh);
609 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_bulk_transfer enter");
611 ret = libusb10_do_transfer(devh, endpoint, data, length, transferred,
612 timeout, LIBUSB_TRANSFER_TYPE_BULK);
614 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_bulk_transfer leave");
619 libusb_interrupt_transfer(libusb_device_handle *devh,
620 uint8_t endpoint, uint8_t *data, int length,
621 int *transferred, unsigned int timeout)
626 ctx = libusb10_get_context_by_device_handle(devh);
628 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_interrupt_transfer enter");
630 ret = libusb10_do_transfer(devh, endpoint, data, length, transferred,
631 timeout, LIBUSB_TRANSFER_TYPE_INTERRUPT);
633 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_interrupt_transfer leave");
638 libusb_get_iso_packet_buffer(struct libusb_transfer *transfer, uint32_t off)
643 if (transfer->num_iso_packets < 0)
646 if (off >= (uint32_t)transfer->num_iso_packets)
649 ptr = transfer->buffer;
653 for (n = 0; n != off; n++) {
654 ptr += transfer->iso_packet_desc[n].length;
660 libusb_get_iso_packet_buffer_simple(struct libusb_transfer *transfer, uint32_t off)
664 if (transfer->num_iso_packets < 0)
667 if (off >= (uint32_t)transfer->num_iso_packets)
670 ptr = transfer->buffer;
674 ptr += transfer->iso_packet_desc[0].length * off;
680 libusb_set_iso_packet_lengths(struct libusb_transfer *transfer, uint32_t length)
684 if (transfer->num_iso_packets < 0)
687 for (n = 0; n != transfer->num_iso_packets; n++)
688 transfer->iso_packet_desc[n].length = length;
692 libusb_control_transfer_get_data(struct libusb_transfer *transfer)
694 if (transfer->buffer == NULL)
697 return (transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE);
700 struct libusb_control_setup *
701 libusb_control_transfer_get_setup(struct libusb_transfer *transfer)
703 return ((struct libusb_control_setup *)transfer->buffer);
707 libusb_fill_control_setup(uint8_t *buf, uint8_t bmRequestType,
708 uint8_t bRequest, uint16_t wValue,
709 uint16_t wIndex, uint16_t wLength)
711 struct libusb_control_setup *req = (struct libusb_control_setup *)buf;
713 /* The alignment is OK for all fields below. */
714 req->bmRequestType = bmRequestType;
715 req->bRequest = bRequest;
716 req->wValue = htole16(wValue);
717 req->wIndex = htole16(wIndex);
718 req->wLength = htole16(wLength);
722 libusb_fill_control_transfer(struct libusb_transfer *transfer,
723 libusb_device_handle *devh, uint8_t *buf,
724 libusb_transfer_cb_fn callback, void *user_data,
727 struct libusb_control_setup *setup = (struct libusb_control_setup *)buf;
729 transfer->dev_handle = devh;
730 transfer->endpoint = 0;
731 transfer->type = LIBUSB_TRANSFER_TYPE_CONTROL;
732 transfer->timeout = timeout;
733 transfer->buffer = buf;
735 transfer->length = LIBUSB_CONTROL_SETUP_SIZE
736 + le16toh(setup->wLength);
738 transfer->length = 0;
739 transfer->user_data = user_data;
740 transfer->callback = callback;
745 libusb_fill_bulk_transfer(struct libusb_transfer *transfer,
746 libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf,
747 int length, libusb_transfer_cb_fn callback, void *user_data,
750 transfer->dev_handle = devh;
751 transfer->endpoint = endpoint;
752 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
753 transfer->timeout = timeout;
754 transfer->buffer = buf;
755 transfer->length = length;
756 transfer->user_data = user_data;
757 transfer->callback = callback;
761 libusb_fill_interrupt_transfer(struct libusb_transfer *transfer,
762 libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf,
763 int length, libusb_transfer_cb_fn callback, void *user_data,
766 transfer->dev_handle = devh;
767 transfer->endpoint = endpoint;
768 transfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT;
769 transfer->timeout = timeout;
770 transfer->buffer = buf;
771 transfer->length = length;
772 transfer->user_data = user_data;
773 transfer->callback = callback;
777 libusb_fill_iso_transfer(struct libusb_transfer *transfer,
778 libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf,
779 int length, int npacket, libusb_transfer_cb_fn callback,
780 void *user_data, uint32_t timeout)
782 transfer->dev_handle = devh;
783 transfer->endpoint = endpoint;
784 transfer->type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS;
785 transfer->timeout = timeout;
786 transfer->buffer = buf;
787 transfer->length = length;
788 transfer->num_iso_packets = npacket;
789 transfer->user_data = user_data;
790 transfer->callback = callback;
794 libusb_alloc_streams(libusb_device_handle *dev, uint32_t num_streams,
795 unsigned char *endpoints, int num_endpoints)
798 return (LIBUSB_ERROR_INVALID_PARAM);
803 libusb_free_streams(libusb_device_handle *dev, unsigned char *endpoints, int num_endpoints)
810 libusb_transfer_set_stream_id(struct libusb_transfer *transfer, uint32_t stream_id)
812 struct libusb_super_transfer *sxfer;
814 if (transfer == NULL)
817 sxfer = (struct libusb_super_transfer *)(
818 ((uint8_t *)transfer) - sizeof(*sxfer));
821 sxfer->stream_id = stream_id;
825 libusb_transfer_get_stream_id(struct libusb_transfer *transfer)
827 struct libusb_super_transfer *sxfer;
829 if (transfer == NULL)
832 sxfer = (struct libusb_super_transfer *)(
833 ((uint8_t *)transfer) - sizeof(*sxfer));
836 return (sxfer->stream_id);