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 = 0; /* nothing to do */
165 err = libusb20_dev_process(ppdev[i]);
168 /* cancel all transfers - device is gone */
169 libusb10_cancel_all_transfer(dev);
171 /* remove USB device from polling loop */
172 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
175 libusb_unref_device(dev);
182 if (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,
315 err = clock_gettime(CLOCK_MONOTONIC, &ts);
317 return (LIBUSB_ERROR_OTHER);
320 * The "tv" arguments points to a relative time structure and
321 * not an absolute time structure.
323 ts.tv_sec += tv->tv_sec;
324 ts.tv_nsec += tv->tv_usec * 1000;
325 if (ts.tv_nsec >= 1000000000) {
326 ts.tv_nsec -= 1000000000;
329 err = pthread_cond_timedwait(&ctx->ctx_cond,
330 &ctx->ctx_lock, &ts);
332 if (err == ETIMEDOUT)
339 libusb_handle_events_timeout(libusb_context *ctx, struct timeval *tv)
343 ctx = GET_CONTEXT(ctx);
345 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_timeout enter");
347 libusb_lock_events(ctx);
349 err = libusb_handle_events_locked(ctx, tv);
351 libusb_unlock_events(ctx);
353 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_timeout leave");
359 libusb_handle_events(libusb_context *ctx)
361 return (libusb_handle_events_timeout(ctx, NULL));
365 libusb_handle_events_locked(libusb_context *ctx, struct timeval *tv)
369 ctx = GET_CONTEXT(ctx);
371 if (libusb_event_handling_ok(ctx)) {
372 err = libusb10_handle_events_sub(ctx, tv);
374 libusb_wait_for_event(ctx, tv);
381 libusb_get_next_timeout(libusb_context *ctx, struct timeval *tv)
383 /* all timeouts are currently being done by the kernel */
389 libusb_set_pollfd_notifiers(libusb_context *ctx,
390 libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb,
393 ctx = GET_CONTEXT(ctx);
395 ctx->fd_added_cb = added_cb;
396 ctx->fd_removed_cb = removed_cb;
397 ctx->fd_cb_user_data = user_data;
400 struct libusb_pollfd **
401 libusb_get_pollfds(libusb_context *ctx)
403 struct libusb_super_pollfd *pollfd;
407 ctx = GET_CONTEXT(ctx);
412 TAILQ_FOREACH(pollfd, &ctx->pollfds, entry)
415 ret = calloc(i + 1, sizeof(struct libusb_pollfd *));
420 TAILQ_FOREACH(pollfd, &ctx->pollfds, entry)
421 ret[i++] = &pollfd->pollfd;
430 /* Synchronous device I/O */
433 libusb_control_transfer(libusb_device_handle *devh,
434 uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
435 uint8_t *data, uint16_t wLength, unsigned int timeout)
437 struct LIBUSB20_CONTROL_SETUP_DECODED req;
442 return (LIBUSB_ERROR_INVALID_PARAM);
444 if ((wLength != 0) && (data == NULL))
445 return (LIBUSB_ERROR_INVALID_PARAM);
447 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req);
449 req.bmRequestType = bmRequestType;
450 req.bRequest = bRequest;
453 req.wLength = wLength;
455 err = libusb20_dev_request_sync(devh, &req, data,
456 &actlen, timeout, 0);
458 if (err == LIBUSB20_ERROR_PIPE)
459 return (LIBUSB_ERROR_PIPE);
460 else if (err == LIBUSB20_ERROR_TIMEOUT)
461 return (LIBUSB_ERROR_TIMEOUT);
463 return (LIBUSB_ERROR_NO_DEVICE);
469 libusb10_do_transfer_cb(struct libusb_transfer *transfer)
474 ctx = GET_CONTEXT(NULL);
476 DPRINTF(ctx, LIBUSB_DEBUG_TRANSFER, "sync I/O done");
478 pdone = transfer->user_data;
483 * TODO: Replace the following function. Allocating and freeing on a
484 * per-transfer basis is slow. --HPS
487 libusb10_do_transfer(libusb_device_handle *devh,
488 uint8_t endpoint, uint8_t *data, int length,
489 int *transferred, unsigned int timeout, int type)
492 struct libusb_transfer *xfer;
497 return (LIBUSB_ERROR_INVALID_PARAM);
499 if ((length != 0) && (data == NULL))
500 return (LIBUSB_ERROR_INVALID_PARAM);
502 xfer = libusb_alloc_transfer(0);
504 return (LIBUSB_ERROR_NO_MEM);
506 ctx = libusb_get_device(devh)->ctx;
508 xfer->dev_handle = devh;
509 xfer->endpoint = endpoint;
511 xfer->timeout = timeout;
513 xfer->length = length;
514 xfer->user_data = (void *)&done;
515 xfer->callback = libusb10_do_transfer_cb;
518 if ((ret = libusb_submit_transfer(xfer)) < 0) {
519 libusb_free_transfer(xfer);
523 if ((ret = libusb_handle_events(ctx)) < 0) {
524 libusb_cancel_transfer(xfer);
525 usleep(1000); /* nice it */
529 *transferred = xfer->actual_length;
531 switch (xfer->status) {
532 case LIBUSB_TRANSFER_COMPLETED:
535 case LIBUSB_TRANSFER_TIMED_OUT:
536 ret = LIBUSB_ERROR_TIMEOUT;
538 case LIBUSB_TRANSFER_OVERFLOW:
539 ret = LIBUSB_ERROR_OVERFLOW;
541 case LIBUSB_TRANSFER_STALL:
542 ret = LIBUSB_ERROR_PIPE;
544 case LIBUSB_TRANSFER_NO_DEVICE:
545 ret = LIBUSB_ERROR_NO_DEVICE;
548 ret = LIBUSB_ERROR_OTHER;
552 libusb_free_transfer(xfer);
557 libusb_bulk_transfer(libusb_device_handle *devh,
558 uint8_t endpoint, uint8_t *data, int length,
559 int *transferred, unsigned int timeout)
564 ctx = GET_CONTEXT(NULL);
565 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_bulk_transfer enter");
567 ret = libusb10_do_transfer(devh, endpoint, data, length, transferred,
568 timeout, LIBUSB_TRANSFER_TYPE_BULK);
570 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_bulk_transfer leave");
575 libusb_interrupt_transfer(libusb_device_handle *devh,
576 uint8_t endpoint, uint8_t *data, int length,
577 int *transferred, unsigned int timeout)
582 ctx = GET_CONTEXT(NULL);
583 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_interrupt_transfer enter");
585 ret = libusb10_do_transfer(devh, endpoint, data, length, transferred,
586 timeout, LIBUSB_TRANSFER_TYPE_INTERRUPT);
588 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_interrupt_transfer leave");
593 libusb_get_iso_packet_buffer(struct libusb_transfer *transfer, uint32_t off)
598 if (transfer->num_iso_packets < 0)
601 if (off >= (uint32_t)transfer->num_iso_packets)
604 ptr = transfer->buffer;
608 for (n = 0; n != off; n++) {
609 ptr += transfer->iso_packet_desc[n].length;
615 libusb_get_iso_packet_buffer_simple(struct libusb_transfer *transfer, uint32_t off)
619 if (transfer->num_iso_packets < 0)
622 if (off >= (uint32_t)transfer->num_iso_packets)
625 ptr = transfer->buffer;
629 ptr += transfer->iso_packet_desc[0].length * off;
635 libusb_set_iso_packet_lengths(struct libusb_transfer *transfer, uint32_t length)
639 if (transfer->num_iso_packets < 0)
642 for (n = 0; n != transfer->num_iso_packets; n++)
643 transfer->iso_packet_desc[n].length = length;
647 libusb_control_transfer_get_data(struct libusb_transfer *transfer)
649 if (transfer->buffer == NULL)
652 return (transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE);
655 struct libusb_control_setup *
656 libusb_control_transfer_get_setup(struct libusb_transfer *transfer)
658 return ((struct libusb_control_setup *)transfer->buffer);
662 libusb_fill_control_setup(uint8_t *buf, uint8_t bmRequestType,
663 uint8_t bRequest, uint16_t wValue,
664 uint16_t wIndex, uint16_t wLength)
666 struct libusb_control_setup *req = (struct libusb_control_setup *)buf;
668 /* The alignment is OK for all fields below. */
669 req->bmRequestType = bmRequestType;
670 req->bRequest = bRequest;
671 req->wValue = htole16(wValue);
672 req->wIndex = htole16(wIndex);
673 req->wLength = htole16(wLength);
677 libusb_fill_control_transfer(struct libusb_transfer *transfer,
678 libusb_device_handle *devh, uint8_t *buf,
679 libusb_transfer_cb_fn callback, void *user_data,
682 struct libusb_control_setup *setup = (struct libusb_control_setup *)buf;
684 transfer->dev_handle = devh;
685 transfer->endpoint = 0;
686 transfer->type = LIBUSB_TRANSFER_TYPE_CONTROL;
687 transfer->timeout = timeout;
688 transfer->buffer = buf;
690 transfer->length = LIBUSB_CONTROL_SETUP_SIZE
691 + le16toh(setup->wLength);
693 transfer->length = 0;
694 transfer->user_data = user_data;
695 transfer->callback = callback;
700 libusb_fill_bulk_transfer(struct libusb_transfer *transfer,
701 libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf,
702 int length, libusb_transfer_cb_fn callback, void *user_data,
705 transfer->dev_handle = devh;
706 transfer->endpoint = endpoint;
707 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
708 transfer->timeout = timeout;
709 transfer->buffer = buf;
710 transfer->length = length;
711 transfer->user_data = user_data;
712 transfer->callback = callback;
716 libusb_fill_interrupt_transfer(struct libusb_transfer *transfer,
717 libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf,
718 int length, libusb_transfer_cb_fn callback, void *user_data,
721 transfer->dev_handle = devh;
722 transfer->endpoint = endpoint;
723 transfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT;
724 transfer->timeout = timeout;
725 transfer->buffer = buf;
726 transfer->length = length;
727 transfer->user_data = user_data;
728 transfer->callback = callback;
732 libusb_fill_iso_transfer(struct libusb_transfer *transfer,
733 libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf,
734 int length, int npacket, libusb_transfer_cb_fn callback,
735 void *user_data, uint32_t timeout)
737 transfer->dev_handle = devh;
738 transfer->endpoint = endpoint;
739 transfer->type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS;
740 transfer->timeout = timeout;
741 transfer->buffer = buf;
742 transfer->length = length;
743 transfer->num_iso_packets = npacket;
744 transfer->user_data = user_data;
745 transfer->callback = callback;