2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2009 Sylvestre Gallon. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #ifdef LIBUSB_GLOBAL_INCLUDE_FILE
29 #include LIBUSB_GLOBAL_INCLUDE_FILE
39 #include <sys/queue.h>
40 #include <sys/endian.h>
43 #define libusb_device_handle libusb20_device
46 #include "libusb20_desc.h"
47 #include "libusb20_int.h"
52 libusb10_add_pollfd(libusb_context *ctx, struct libusb_super_pollfd *pollfd,
53 struct libusb20_device *pdev, int fd, short events)
58 if (pollfd->entry.tqe_prev != NULL)
59 return; /* already queued */
65 pollfd->pollfd.fd = fd;
66 pollfd->pollfd.events = events;
69 TAILQ_INSERT_TAIL(&ctx->pollfds, pollfd, entry);
73 ctx->fd_added_cb(fd, events, ctx->fd_cb_user_data);
77 libusb10_remove_pollfd(libusb_context *ctx, struct libusb_super_pollfd *pollfd)
82 if (pollfd->entry.tqe_prev == NULL)
83 return; /* already dequeued */
86 TAILQ_REMOVE(&ctx->pollfds, pollfd, entry);
87 pollfd->entry.tqe_prev = NULL;
90 if (ctx->fd_removed_cb)
91 ctx->fd_removed_cb(pollfd->pollfd.fd, ctx->fd_cb_user_data);
94 /* This function must be called locked */
97 libusb10_handle_events_sub(struct libusb_context *ctx, struct timeval *tv)
99 struct libusb_device *dev;
100 struct libusb20_device **ppdev;
101 struct libusb_super_pollfd *pfd;
103 struct libusb_super_transfer *sxfer;
104 struct libusb_transfer *uxfer;
110 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb10_handle_events_sub enter");
114 TAILQ_FOREACH(pfd, &ctx->pollfds, entry)
117 fds = alloca(sizeof(*fds) * nfds);
119 return (LIBUSB_ERROR_NO_MEM);
121 ppdev = alloca(sizeof(*ppdev) * nfds);
123 return (LIBUSB_ERROR_NO_MEM);
125 TAILQ_FOREACH(pfd, &ctx->pollfds, entry) {
126 fds[i].fd = pfd->pollfd.fd;
127 fds[i].events = pfd->pollfd.events;
129 ppdev[i] = pfd->pdev;
130 if (pfd->pdev != NULL)
131 libusb_get_device(pfd->pdev)->refcnt++;
138 timeout = (tv->tv_sec * 1000) + ((tv->tv_usec + 999) / 1000);
141 err = poll(fds, nfds, timeout);
144 if ((err == -1) && (errno == EINTR))
145 err = LIBUSB_ERROR_INTERRUPTED;
147 err = LIBUSB_ERROR_IO;
150 for (i = 0; i != (int)nfds; i++) {
151 if (ppdev[i] != NULL) {
153 libusb_unref_device(libusb_get_device(ppdev[i]));
159 for (i = 0; i != (int)nfds; i++) {
160 if (ppdev[i] != NULL) {
161 dev = libusb_get_device(ppdev[i]);
163 if (fds[i].revents != 0) {
164 err = libusb20_dev_process(ppdev[i]);
168 * When the device is opened
169 * set the "device_is_gone"
170 * flag. This prevents the
171 * client from submitting new
172 * USB transfers to a detached
175 if (ppdev[i]->is_opened)
176 dev->device_is_gone = 1;
178 /* remove USB device from polling loop */
179 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
181 /* cancel all pending transfers */
182 libusb10_cancel_all_transfer_locked(ppdev[i], dev);
186 libusb_unref_device(dev);
192 while (read(fds[i].fd, &dummy, 1) == 1)
201 /* Do all done callbacks */
203 while ((sxfer = TAILQ_FIRST(&ctx->tr_done))) {
206 TAILQ_REMOVE(&ctx->tr_done, sxfer, entry);
207 sxfer->entry.tqe_prev = NULL;
213 uxfer = (struct libusb_transfer *)(
214 ((uint8_t *)sxfer) + sizeof(*sxfer));
216 /* Allow the callback to free the transfer itself. */
217 flags = uxfer->flags;
219 if (uxfer->callback != NULL)
220 (uxfer->callback) (uxfer);
222 /* Check if the USB transfer should be automatically freed. */
223 if (flags & LIBUSB_TRANSFER_FREE_TRANSFER)
224 libusb_free_transfer(uxfer);
232 /* Wakeup other waiters */
233 pthread_cond_broadcast(&ctx->ctx_cond);
238 /* Polling and timing */
241 libusb_try_lock_events(libusb_context *ctx)
245 ctx = GET_CONTEXT(ctx);
249 err = CTX_TRYLOCK(ctx);
253 err = (ctx->ctx_handler != NO_THREAD);
257 ctx->ctx_handler = pthread_self();
263 libusb_lock_events(libusb_context *ctx)
265 ctx = GET_CONTEXT(ctx);
267 if (ctx->ctx_handler == NO_THREAD)
268 ctx->ctx_handler = pthread_self();
272 libusb_unlock_events(libusb_context *ctx)
274 ctx = GET_CONTEXT(ctx);
275 if (ctx->ctx_handler == pthread_self()) {
276 ctx->ctx_handler = NO_THREAD;
277 pthread_cond_broadcast(&ctx->ctx_cond);
283 libusb_event_handling_ok(libusb_context *ctx)
285 ctx = GET_CONTEXT(ctx);
286 return (ctx->ctx_handler == pthread_self());
290 libusb_event_handler_active(libusb_context *ctx)
292 ctx = GET_CONTEXT(ctx);
293 return (ctx->ctx_handler != NO_THREAD);
297 libusb_lock_event_waiters(libusb_context *ctx)
299 ctx = GET_CONTEXT(ctx);
304 libusb_unlock_event_waiters(libusb_context *ctx)
306 ctx = GET_CONTEXT(ctx);
311 libusb_wait_for_event(libusb_context *ctx, struct timeval *tv)
316 ctx = GET_CONTEXT(ctx);
317 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_wait_for_event enter");
320 pthread_cond_wait(&ctx->ctx_cond,
322 /* try to grab polling of actual events, if any */
323 if (ctx->ctx_handler == NO_THREAD)
324 ctx->ctx_handler = pthread_self();
327 err = clock_gettime(CLOCK_MONOTONIC, &ts);
329 return (LIBUSB_ERROR_OTHER);
332 * The "tv" arguments points to a relative time structure and
333 * not an absolute time structure.
335 ts.tv_sec += tv->tv_sec;
336 ts.tv_nsec += tv->tv_usec * 1000;
337 if (ts.tv_nsec >= 1000000000) {
338 ts.tv_nsec -= 1000000000;
341 err = pthread_cond_timedwait(&ctx->ctx_cond,
342 &ctx->ctx_lock, &ts);
343 /* try to grab polling of actual events, if any */
344 if (ctx->ctx_handler == NO_THREAD)
345 ctx->ctx_handler = pthread_self();
347 if (err == ETIMEDOUT)
354 libusb_handle_events_timeout_completed(libusb_context *ctx,
355 struct timeval *tv, int *completed)
359 ctx = GET_CONTEXT(ctx);
361 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_timeout_completed enter");
363 libusb_lock_events(ctx);
366 if (completed != NULL) {
367 if (*completed != 0 || err != 0)
370 err = libusb_handle_events_locked(ctx, tv);
371 if (completed == NULL)
375 libusb_unlock_events(ctx);
377 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_timeout_completed exit");
383 libusb_handle_events_completed(libusb_context *ctx, int *completed)
385 return (libusb_handle_events_timeout_completed(ctx, NULL, completed));
389 libusb_handle_events_timeout(libusb_context *ctx, struct timeval *tv)
391 return (libusb_handle_events_timeout_completed(ctx, tv, NULL));
395 libusb_handle_events(libusb_context *ctx)
397 return (libusb_handle_events_timeout_completed(ctx, NULL, NULL));
401 libusb_handle_events_locked(libusb_context *ctx, struct timeval *tv)
405 ctx = GET_CONTEXT(ctx);
407 if (libusb_event_handling_ok(ctx)) {
408 err = libusb10_handle_events_sub(ctx, tv);
410 err = libusb_wait_for_event(ctx, tv);
412 err = LIBUSB_ERROR_TIMEOUT;
418 libusb_get_next_timeout(libusb_context *ctx, struct timeval *tv)
420 /* all timeouts are currently being done by the kernel */
426 libusb_set_pollfd_notifiers(libusb_context *ctx,
427 libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb,
430 ctx = GET_CONTEXT(ctx);
432 ctx->fd_added_cb = added_cb;
433 ctx->fd_removed_cb = removed_cb;
434 ctx->fd_cb_user_data = user_data;
437 const struct libusb_pollfd **
438 libusb_get_pollfds(libusb_context *ctx)
440 struct libusb_super_pollfd *pollfd;
444 ctx = GET_CONTEXT(ctx);
449 TAILQ_FOREACH(pollfd, &ctx->pollfds, entry)
452 ret = calloc(i + 1, sizeof(struct libusb_pollfd *));
457 TAILQ_FOREACH(pollfd, &ctx->pollfds, entry)
458 ret[i++] = &pollfd->pollfd;
463 return ((const struct libusb_pollfd **)ret);
467 /* Synchronous device I/O */
470 libusb_control_transfer(libusb_device_handle *devh,
471 uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
472 uint8_t *data, uint16_t wLength, unsigned int timeout)
474 struct LIBUSB20_CONTROL_SETUP_DECODED req;
479 return (LIBUSB_ERROR_INVALID_PARAM);
481 if ((wLength != 0) && (data == NULL))
482 return (LIBUSB_ERROR_INVALID_PARAM);
484 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req);
486 req.bmRequestType = bmRequestType;
487 req.bRequest = bRequest;
490 req.wLength = wLength;
492 err = libusb20_dev_request_sync(devh, &req, data,
493 &actlen, timeout, 0);
495 if (err == LIBUSB20_ERROR_PIPE)
496 return (LIBUSB_ERROR_PIPE);
497 else if (err == LIBUSB20_ERROR_TIMEOUT)
498 return (LIBUSB_ERROR_TIMEOUT);
500 return (LIBUSB_ERROR_NO_DEVICE);
505 static libusb_context *
506 libusb10_get_context_by_device_handle(libusb_device_handle *devh)
511 ctx = libusb_get_device(devh)->ctx;
515 return (GET_CONTEXT(ctx));
519 libusb10_do_transfer_cb(struct libusb_transfer *transfer)
524 ctx = libusb10_get_context_by_device_handle(transfer->dev_handle);
526 DPRINTF(ctx, LIBUSB_DEBUG_TRANSFER, "sync I/O done");
528 pdone = transfer->user_data;
533 * TODO: Replace the following function. Allocating and freeing on a
534 * per-transfer basis is slow. --HPS
537 libusb10_do_transfer(libusb_device_handle *devh,
538 uint8_t endpoint, uint8_t *data, int length,
539 int *transferred, unsigned int timeout, int type)
542 struct libusb_transfer *xfer;
547 return (LIBUSB_ERROR_INVALID_PARAM);
549 if ((length != 0) && (data == NULL))
550 return (LIBUSB_ERROR_INVALID_PARAM);
552 xfer = libusb_alloc_transfer(0);
554 return (LIBUSB_ERROR_NO_MEM);
556 ctx = libusb_get_device(devh)->ctx;
558 xfer->dev_handle = devh;
559 xfer->endpoint = endpoint;
561 xfer->timeout = timeout;
563 xfer->length = length;
564 xfer->user_data = (void *)&done;
565 xfer->callback = libusb10_do_transfer_cb;
568 if ((ret = libusb_submit_transfer(xfer)) < 0) {
569 libusb_free_transfer(xfer);
573 if ((ret = libusb_handle_events(ctx)) < 0) {
574 libusb_cancel_transfer(xfer);
575 usleep(1000); /* nice it */
579 *transferred = xfer->actual_length;
581 switch (xfer->status) {
582 case LIBUSB_TRANSFER_COMPLETED:
585 case LIBUSB_TRANSFER_TIMED_OUT:
586 ret = LIBUSB_ERROR_TIMEOUT;
588 case LIBUSB_TRANSFER_OVERFLOW:
589 ret = LIBUSB_ERROR_OVERFLOW;
591 case LIBUSB_TRANSFER_STALL:
592 ret = LIBUSB_ERROR_PIPE;
594 case LIBUSB_TRANSFER_NO_DEVICE:
595 ret = LIBUSB_ERROR_NO_DEVICE;
598 ret = LIBUSB_ERROR_OTHER;
602 libusb_free_transfer(xfer);
607 libusb_bulk_transfer(libusb_device_handle *devh,
608 uint8_t endpoint, uint8_t *data, int length,
609 int *transferred, unsigned int timeout)
614 ctx = libusb10_get_context_by_device_handle(devh);
616 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_bulk_transfer enter");
618 ret = libusb10_do_transfer(devh, endpoint, data, length, transferred,
619 timeout, LIBUSB_TRANSFER_TYPE_BULK);
621 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_bulk_transfer leave");
626 libusb_interrupt_transfer(libusb_device_handle *devh,
627 uint8_t endpoint, uint8_t *data, int length,
628 int *transferred, unsigned int timeout)
633 ctx = libusb10_get_context_by_device_handle(devh);
635 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_interrupt_transfer enter");
637 ret = libusb10_do_transfer(devh, endpoint, data, length, transferred,
638 timeout, LIBUSB_TRANSFER_TYPE_INTERRUPT);
640 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_interrupt_transfer leave");
645 libusb_get_iso_packet_buffer(struct libusb_transfer *transfer, uint32_t off)
650 if (transfer->num_iso_packets < 0)
653 if (off >= (uint32_t)transfer->num_iso_packets)
656 ptr = transfer->buffer;
660 for (n = 0; n != off; n++) {
661 ptr += transfer->iso_packet_desc[n].length;
667 libusb_get_iso_packet_buffer_simple(struct libusb_transfer *transfer, uint32_t off)
671 if (transfer->num_iso_packets < 0)
674 if (off >= (uint32_t)transfer->num_iso_packets)
677 ptr = transfer->buffer;
681 ptr += transfer->iso_packet_desc[0].length * off;
687 libusb_set_iso_packet_lengths(struct libusb_transfer *transfer, uint32_t length)
691 if (transfer->num_iso_packets < 0)
694 for (n = 0; n != transfer->num_iso_packets; n++)
695 transfer->iso_packet_desc[n].length = length;
699 libusb_control_transfer_get_data(struct libusb_transfer *transfer)
701 if (transfer->buffer == NULL)
704 return (transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE);
707 struct libusb_control_setup *
708 libusb_control_transfer_get_setup(struct libusb_transfer *transfer)
710 return ((struct libusb_control_setup *)transfer->buffer);
714 libusb_fill_control_setup(uint8_t *buf, uint8_t bmRequestType,
715 uint8_t bRequest, uint16_t wValue,
716 uint16_t wIndex, uint16_t wLength)
718 struct libusb_control_setup *req = (struct libusb_control_setup *)buf;
720 /* The alignment is OK for all fields below. */
721 req->bmRequestType = bmRequestType;
722 req->bRequest = bRequest;
723 req->wValue = htole16(wValue);
724 req->wIndex = htole16(wIndex);
725 req->wLength = htole16(wLength);
729 libusb_fill_control_transfer(struct libusb_transfer *transfer,
730 libusb_device_handle *devh, uint8_t *buf,
731 libusb_transfer_cb_fn callback, void *user_data,
734 struct libusb_control_setup *setup = (struct libusb_control_setup *)buf;
736 transfer->dev_handle = devh;
737 transfer->endpoint = 0;
738 transfer->type = LIBUSB_TRANSFER_TYPE_CONTROL;
739 transfer->timeout = timeout;
740 transfer->buffer = buf;
742 transfer->length = LIBUSB_CONTROL_SETUP_SIZE
743 + le16toh(setup->wLength);
745 transfer->length = 0;
746 transfer->user_data = user_data;
747 transfer->callback = callback;
752 libusb_fill_bulk_transfer(struct libusb_transfer *transfer,
753 libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf,
754 int length, libusb_transfer_cb_fn callback, void *user_data,
757 transfer->dev_handle = devh;
758 transfer->endpoint = endpoint;
759 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
760 transfer->timeout = timeout;
761 transfer->buffer = buf;
762 transfer->length = length;
763 transfer->user_data = user_data;
764 transfer->callback = callback;
768 libusb_fill_interrupt_transfer(struct libusb_transfer *transfer,
769 libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf,
770 int length, libusb_transfer_cb_fn callback, void *user_data,
773 transfer->dev_handle = devh;
774 transfer->endpoint = endpoint;
775 transfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT;
776 transfer->timeout = timeout;
777 transfer->buffer = buf;
778 transfer->length = length;
779 transfer->user_data = user_data;
780 transfer->callback = callback;
784 libusb_fill_iso_transfer(struct libusb_transfer *transfer,
785 libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf,
786 int length, int npacket, libusb_transfer_cb_fn callback,
787 void *user_data, uint32_t timeout)
789 transfer->dev_handle = devh;
790 transfer->endpoint = endpoint;
791 transfer->type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS;
792 transfer->timeout = timeout;
793 transfer->buffer = buf;
794 transfer->length = length;
795 transfer->num_iso_packets = npacket;
796 transfer->user_data = user_data;
797 transfer->callback = callback;
801 libusb_alloc_streams(libusb_device_handle *dev, uint32_t num_streams,
802 unsigned char *endpoints, int num_endpoints)
805 return (LIBUSB_ERROR_INVALID_PARAM);
810 libusb_free_streams(libusb_device_handle *dev, unsigned char *endpoints, int num_endpoints)
817 libusb_transfer_set_stream_id(struct libusb_transfer *transfer, uint32_t stream_id)
819 struct libusb_super_transfer *sxfer;
821 if (transfer == NULL)
824 sxfer = (struct libusb_super_transfer *)(
825 ((uint8_t *)transfer) - sizeof(*sxfer));
828 sxfer->stream_id = stream_id;
832 libusb_transfer_get_stream_id(struct libusb_transfer *transfer)
834 struct libusb_super_transfer *sxfer;
836 if (transfer == NULL)
839 sxfer = (struct libusb_super_transfer *)(
840 ((uint8_t *)transfer) - sizeof(*sxfer));
843 return (sxfer->stream_id);