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,
317 err = clock_gettime(CLOCK_MONOTONIC, &ts);
319 return (LIBUSB_ERROR_OTHER);
322 * The "tv" arguments points to a relative time structure and
323 * not an absolute time structure.
325 ts.tv_sec += tv->tv_sec;
326 ts.tv_nsec += tv->tv_usec * 1000;
327 if (ts.tv_nsec >= 1000000000) {
328 ts.tv_nsec -= 1000000000;
331 err = pthread_cond_timedwait(&ctx->ctx_cond,
332 &ctx->ctx_lock, &ts);
334 if (err == ETIMEDOUT)
341 libusb_handle_events_timeout_completed(libusb_context *ctx,
342 struct timeval *tv, int *completed)
346 ctx = GET_CONTEXT(ctx);
348 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_timeout_completed enter");
350 libusb_lock_events(ctx);
353 if (completed != NULL) {
354 if (*completed != 0 || err != 0)
357 err = libusb_handle_events_locked(ctx, tv);
358 if (completed == NULL)
362 libusb_unlock_events(ctx);
364 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_timeout_completed exit");
370 libusb_handle_events_completed(libusb_context *ctx, int *completed)
372 return (libusb_handle_events_timeout_completed(ctx, NULL, completed));
376 libusb_handle_events_timeout(libusb_context *ctx, struct timeval *tv)
378 return (libusb_handle_events_timeout_completed(ctx, tv, NULL));
382 libusb_handle_events(libusb_context *ctx)
384 return (libusb_handle_events_timeout_completed(ctx, NULL, NULL));
388 libusb_handle_events_locked(libusb_context *ctx, struct timeval *tv)
392 ctx = GET_CONTEXT(ctx);
394 if (libusb_event_handling_ok(ctx)) {
395 err = libusb10_handle_events_sub(ctx, tv);
397 err = libusb_wait_for_event(ctx, tv);
399 err = LIBUSB_ERROR_TIMEOUT;
405 libusb_get_next_timeout(libusb_context *ctx, struct timeval *tv)
407 /* all timeouts are currently being done by the kernel */
413 libusb_set_pollfd_notifiers(libusb_context *ctx,
414 libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb,
417 ctx = GET_CONTEXT(ctx);
419 ctx->fd_added_cb = added_cb;
420 ctx->fd_removed_cb = removed_cb;
421 ctx->fd_cb_user_data = user_data;
424 const struct libusb_pollfd **
425 libusb_get_pollfds(libusb_context *ctx)
427 struct libusb_super_pollfd *pollfd;
431 ctx = GET_CONTEXT(ctx);
436 TAILQ_FOREACH(pollfd, &ctx->pollfds, entry)
439 ret = calloc(i + 1, sizeof(struct libusb_pollfd *));
444 TAILQ_FOREACH(pollfd, &ctx->pollfds, entry)
445 ret[i++] = &pollfd->pollfd;
450 return ((const struct libusb_pollfd **)ret);
454 /* Synchronous device I/O */
457 libusb_control_transfer(libusb_device_handle *devh,
458 uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
459 uint8_t *data, uint16_t wLength, unsigned int timeout)
461 struct LIBUSB20_CONTROL_SETUP_DECODED req;
466 return (LIBUSB_ERROR_INVALID_PARAM);
468 if ((wLength != 0) && (data == NULL))
469 return (LIBUSB_ERROR_INVALID_PARAM);
471 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req);
473 req.bmRequestType = bmRequestType;
474 req.bRequest = bRequest;
477 req.wLength = wLength;
479 err = libusb20_dev_request_sync(devh, &req, data,
480 &actlen, timeout, 0);
482 if (err == LIBUSB20_ERROR_PIPE)
483 return (LIBUSB_ERROR_PIPE);
484 else if (err == LIBUSB20_ERROR_TIMEOUT)
485 return (LIBUSB_ERROR_TIMEOUT);
487 return (LIBUSB_ERROR_NO_DEVICE);
493 libusb10_do_transfer_cb(struct libusb_transfer *transfer)
498 ctx = GET_CONTEXT(NULL);
500 DPRINTF(ctx, LIBUSB_DEBUG_TRANSFER, "sync I/O done");
502 pdone = transfer->user_data;
507 * TODO: Replace the following function. Allocating and freeing on a
508 * per-transfer basis is slow. --HPS
511 libusb10_do_transfer(libusb_device_handle *devh,
512 uint8_t endpoint, uint8_t *data, int length,
513 int *transferred, unsigned int timeout, int type)
516 struct libusb_transfer *xfer;
521 return (LIBUSB_ERROR_INVALID_PARAM);
523 if ((length != 0) && (data == NULL))
524 return (LIBUSB_ERROR_INVALID_PARAM);
526 xfer = libusb_alloc_transfer(0);
528 return (LIBUSB_ERROR_NO_MEM);
530 ctx = libusb_get_device(devh)->ctx;
532 xfer->dev_handle = devh;
533 xfer->endpoint = endpoint;
535 xfer->timeout = timeout;
537 xfer->length = length;
538 xfer->user_data = (void *)&done;
539 xfer->callback = libusb10_do_transfer_cb;
542 if ((ret = libusb_submit_transfer(xfer)) < 0) {
543 libusb_free_transfer(xfer);
547 if ((ret = libusb_handle_events(ctx)) < 0) {
548 libusb_cancel_transfer(xfer);
549 usleep(1000); /* nice it */
553 *transferred = xfer->actual_length;
555 switch (xfer->status) {
556 case LIBUSB_TRANSFER_COMPLETED:
559 case LIBUSB_TRANSFER_TIMED_OUT:
560 ret = LIBUSB_ERROR_TIMEOUT;
562 case LIBUSB_TRANSFER_OVERFLOW:
563 ret = LIBUSB_ERROR_OVERFLOW;
565 case LIBUSB_TRANSFER_STALL:
566 ret = LIBUSB_ERROR_PIPE;
568 case LIBUSB_TRANSFER_NO_DEVICE:
569 ret = LIBUSB_ERROR_NO_DEVICE;
572 ret = LIBUSB_ERROR_OTHER;
576 libusb_free_transfer(xfer);
581 libusb_bulk_transfer(libusb_device_handle *devh,
582 uint8_t endpoint, uint8_t *data, int length,
583 int *transferred, unsigned int timeout)
588 ctx = GET_CONTEXT(NULL);
589 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_bulk_transfer enter");
591 ret = libusb10_do_transfer(devh, endpoint, data, length, transferred,
592 timeout, LIBUSB_TRANSFER_TYPE_BULK);
594 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_bulk_transfer leave");
599 libusb_interrupt_transfer(libusb_device_handle *devh,
600 uint8_t endpoint, uint8_t *data, int length,
601 int *transferred, unsigned int timeout)
606 ctx = GET_CONTEXT(NULL);
607 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_interrupt_transfer enter");
609 ret = libusb10_do_transfer(devh, endpoint, data, length, transferred,
610 timeout, LIBUSB_TRANSFER_TYPE_INTERRUPT);
612 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_interrupt_transfer leave");
617 libusb_get_iso_packet_buffer(struct libusb_transfer *transfer, uint32_t off)
622 if (transfer->num_iso_packets < 0)
625 if (off >= (uint32_t)transfer->num_iso_packets)
628 ptr = transfer->buffer;
632 for (n = 0; n != off; n++) {
633 ptr += transfer->iso_packet_desc[n].length;
639 libusb_get_iso_packet_buffer_simple(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 ptr += transfer->iso_packet_desc[0].length * off;
659 libusb_set_iso_packet_lengths(struct libusb_transfer *transfer, uint32_t length)
663 if (transfer->num_iso_packets < 0)
666 for (n = 0; n != transfer->num_iso_packets; n++)
667 transfer->iso_packet_desc[n].length = length;
671 libusb_control_transfer_get_data(struct libusb_transfer *transfer)
673 if (transfer->buffer == NULL)
676 return (transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE);
679 struct libusb_control_setup *
680 libusb_control_transfer_get_setup(struct libusb_transfer *transfer)
682 return ((struct libusb_control_setup *)transfer->buffer);
686 libusb_fill_control_setup(uint8_t *buf, uint8_t bmRequestType,
687 uint8_t bRequest, uint16_t wValue,
688 uint16_t wIndex, uint16_t wLength)
690 struct libusb_control_setup *req = (struct libusb_control_setup *)buf;
692 /* The alignment is OK for all fields below. */
693 req->bmRequestType = bmRequestType;
694 req->bRequest = bRequest;
695 req->wValue = htole16(wValue);
696 req->wIndex = htole16(wIndex);
697 req->wLength = htole16(wLength);
701 libusb_fill_control_transfer(struct libusb_transfer *transfer,
702 libusb_device_handle *devh, uint8_t *buf,
703 libusb_transfer_cb_fn callback, void *user_data,
706 struct libusb_control_setup *setup = (struct libusb_control_setup *)buf;
708 transfer->dev_handle = devh;
709 transfer->endpoint = 0;
710 transfer->type = LIBUSB_TRANSFER_TYPE_CONTROL;
711 transfer->timeout = timeout;
712 transfer->buffer = buf;
714 transfer->length = LIBUSB_CONTROL_SETUP_SIZE
715 + le16toh(setup->wLength);
717 transfer->length = 0;
718 transfer->user_data = user_data;
719 transfer->callback = callback;
724 libusb_fill_bulk_transfer(struct libusb_transfer *transfer,
725 libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf,
726 int length, libusb_transfer_cb_fn callback, void *user_data,
729 transfer->dev_handle = devh;
730 transfer->endpoint = endpoint;
731 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
732 transfer->timeout = timeout;
733 transfer->buffer = buf;
734 transfer->length = length;
735 transfer->user_data = user_data;
736 transfer->callback = callback;
740 libusb_fill_interrupt_transfer(struct libusb_transfer *transfer,
741 libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf,
742 int length, libusb_transfer_cb_fn callback, void *user_data,
745 transfer->dev_handle = devh;
746 transfer->endpoint = endpoint;
747 transfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT;
748 transfer->timeout = timeout;
749 transfer->buffer = buf;
750 transfer->length = length;
751 transfer->user_data = user_data;
752 transfer->callback = callback;
756 libusb_fill_iso_transfer(struct libusb_transfer *transfer,
757 libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf,
758 int length, int npacket, libusb_transfer_cb_fn callback,
759 void *user_data, uint32_t timeout)
761 transfer->dev_handle = devh;
762 transfer->endpoint = endpoint;
763 transfer->type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS;
764 transfer->timeout = timeout;
765 transfer->buffer = buf;
766 transfer->length = length;
767 transfer->num_iso_packets = npacket;
768 transfer->user_data = user_data;
769 transfer->callback = callback;
773 libusb_alloc_streams(libusb_device_handle *dev, uint32_t num_streams,
774 unsigned char *endpoints, int num_endpoints)
777 return (LIBUSB_ERROR_INVALID_PARAM);
782 libusb_free_streams(libusb_device_handle *dev, unsigned char *endpoints, int num_endpoints)
789 libusb_transfer_set_stream_id(struct libusb_transfer *transfer, uint32_t stream_id)
791 struct libusb_super_transfer *sxfer;
793 if (transfer == NULL)
796 sxfer = (struct libusb_super_transfer *)(
797 ((uint8_t *)transfer) - sizeof(*sxfer));
800 sxfer->stream_id = stream_id;
804 libusb_transfer_get_stream_id(struct libusb_transfer *transfer)
806 struct libusb_super_transfer *sxfer;
808 if (transfer == NULL)
811 sxfer = (struct libusb_super_transfer *)(
812 ((uint8_t *)transfer) - sizeof(*sxfer));
815 return (sxfer->stream_id);