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
34 #include <sys/queue.h>
37 #include "libusb20_desc.h"
38 #include "libusb20_int.h"
43 libusb10_add_pollfd(libusb_context *ctx, struct libusb_super_pollfd *pollfd,
44 struct libusb20_device *pdev, int fd, short events)
49 if (pollfd->entry.tqe_prev != NULL)
50 return; /* already queued */
56 pollfd->pollfd.fd = fd;
57 pollfd->pollfd.events = events;
60 TAILQ_INSERT_TAIL(&ctx->pollfds, pollfd, entry);
64 ctx->fd_added_cb(fd, events, ctx->fd_cb_user_data);
68 libusb10_remove_pollfd(libusb_context *ctx, struct libusb_super_pollfd *pollfd)
73 if (pollfd->entry.tqe_prev == NULL)
74 return; /* already dequeued */
77 TAILQ_REMOVE(&ctx->pollfds, pollfd, entry);
78 pollfd->entry.tqe_prev = NULL;
81 if (ctx->fd_removed_cb)
82 ctx->fd_removed_cb(pollfd->pollfd.fd, ctx->fd_cb_user_data);
85 /* This function must be called locked */
88 libusb10_handle_events_sub(struct libusb_context *ctx, struct timeval *tv)
90 struct libusb_device *dev;
91 struct libusb20_device **ppdev;
92 struct libusb_super_pollfd *pfd;
94 struct libusb_super_transfer *sxfer;
95 struct libusb_transfer *uxfer;
101 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb10_handle_events_sub enter");
105 TAILQ_FOREACH(pfd, &ctx->pollfds, entry)
108 fds = alloca(sizeof(*fds) * nfds);
110 return (LIBUSB_ERROR_NO_MEM);
112 ppdev = alloca(sizeof(*ppdev) * nfds);
114 return (LIBUSB_ERROR_NO_MEM);
116 TAILQ_FOREACH(pfd, &ctx->pollfds, entry) {
117 fds[i].fd = pfd->pollfd.fd;
118 fds[i].events = pfd->pollfd.events;
120 ppdev[i] = pfd->pdev;
121 if (pfd->pdev != NULL)
122 libusb_get_device(pfd->pdev)->refcnt++;
129 timeout = (tv->tv_sec * 1000) + ((tv->tv_usec + 999) / 1000);
132 err = poll(fds, nfds, timeout);
135 if ((err == -1) && (errno == EINTR))
136 err = LIBUSB_ERROR_INTERRUPTED;
138 err = LIBUSB_ERROR_IO;
141 for (i = 0; i != nfds; i++) {
142 if (ppdev[i] != NULL) {
144 libusb_unref_device(libusb_get_device(ppdev[i]));
150 for (i = 0; i != nfds; i++) {
151 if (fds[i].revents == 0)
153 if (ppdev[i] != NULL) {
154 dev = libusb_get_device(ppdev[i]);
156 err = libusb20_dev_process(ppdev[i]);
158 /* cancel all transfers - device is gone */
159 libusb10_cancel_all_transfer(dev);
161 * make sure we don't go into an infinite
164 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
167 libusb_unref_device(dev);
174 if (read(fds[i].fd, &dummy, 1) != 1)
184 /* Do all done callbacks */
186 while ((sxfer = TAILQ_FIRST(&ctx->tr_done))) {
187 TAILQ_REMOVE(&ctx->tr_done, sxfer, entry);
188 sxfer->entry.tqe_prev = NULL;
194 uxfer = (struct libusb_transfer *)(
195 ((uint8_t *)sxfer) + sizeof(*sxfer));
197 if (uxfer->callback != NULL)
198 (uxfer->callback) (uxfer);
200 if (uxfer->flags & LIBUSB_TRANSFER_FREE_BUFFER)
203 if (uxfer->flags & LIBUSB_TRANSFER_FREE_TRANSFER)
204 libusb_free_transfer(uxfer);
212 /* Wakeup other waiters */
213 pthread_cond_broadcast(&ctx->ctx_cond);
218 /* Polling and timing */
221 libusb_try_lock_events(libusb_context *ctx)
225 ctx = GET_CONTEXT(ctx);
229 err = CTX_TRYLOCK(ctx);
233 err = (ctx->ctx_handler != NO_THREAD);
237 ctx->ctx_handler = pthread_self();
243 libusb_lock_events(libusb_context *ctx)
245 ctx = GET_CONTEXT(ctx);
247 if (ctx->ctx_handler == NO_THREAD)
248 ctx->ctx_handler = pthread_self();
252 libusb_unlock_events(libusb_context *ctx)
254 ctx = GET_CONTEXT(ctx);
255 if (ctx->ctx_handler == pthread_self()) {
256 ctx->ctx_handler = NO_THREAD;
257 pthread_cond_broadcast(&ctx->ctx_cond);
263 libusb_event_handling_ok(libusb_context *ctx)
265 ctx = GET_CONTEXT(ctx);
266 return (ctx->ctx_handler == pthread_self());
270 libusb_event_handler_active(libusb_context *ctx)
272 ctx = GET_CONTEXT(ctx);
273 return (ctx->ctx_handler != NO_THREAD);
277 libusb_lock_event_waiters(libusb_context *ctx)
279 ctx = GET_CONTEXT(ctx);
284 libusb_unlock_event_waiters(libusb_context *ctx)
286 ctx = GET_CONTEXT(ctx);
291 libusb_wait_for_event(libusb_context *ctx, struct timeval *tv)
296 ctx = GET_CONTEXT(ctx);
297 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_wait_for_event enter");
300 pthread_cond_wait(&ctx->ctx_cond,
304 err = clock_gettime(CLOCK_REALTIME, &ts);
306 return (LIBUSB_ERROR_OTHER);
308 ts.tv_sec = tv->tv_sec;
309 ts.tv_nsec = tv->tv_usec * 1000;
310 if (ts.tv_nsec >= 1000000000) {
311 ts.tv_nsec -= 1000000000;
314 err = pthread_cond_timedwait(&ctx->ctx_cond,
315 &ctx->ctx_lock, &ts);
317 if (err == ETIMEDOUT)
324 libusb_handle_events_timeout(libusb_context *ctx, struct timeval *tv)
328 ctx = GET_CONTEXT(ctx);
330 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_timeout enter");
332 libusb_lock_events(ctx);
334 err = libusb_handle_events_locked(ctx, tv);
336 libusb_unlock_events(ctx);
338 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_timeout leave");
344 libusb_handle_events(libusb_context *ctx)
346 return (libusb_handle_events_timeout(ctx, NULL));
350 libusb_handle_events_locked(libusb_context *ctx, struct timeval *tv)
354 ctx = GET_CONTEXT(ctx);
356 if (libusb_event_handling_ok(ctx)) {
357 err = libusb10_handle_events_sub(ctx, tv);
359 libusb_wait_for_event(ctx, tv);
366 libusb_get_next_timeout(libusb_context *ctx, struct timeval *tv)
368 /* all timeouts are currently being done by the kernel */
374 libusb_set_pollfd_notifiers(libusb_context *ctx,
375 libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb,
378 ctx = GET_CONTEXT(ctx);
380 ctx->fd_added_cb = added_cb;
381 ctx->fd_removed_cb = removed_cb;
382 ctx->fd_cb_user_data = user_data;
385 struct libusb_pollfd **
386 libusb_get_pollfds(libusb_context *ctx)
388 struct libusb_super_pollfd *pollfd;
392 ctx = GET_CONTEXT(ctx);
397 TAILQ_FOREACH(pollfd, &ctx->pollfds, entry)
400 ret = calloc(i + 1, sizeof(struct libusb_pollfd *));
405 TAILQ_FOREACH(pollfd, &ctx->pollfds, entry)
406 ret[i++] = &pollfd->pollfd;
415 /* Synchronous device I/O */
418 libusb_control_transfer(libusb_device_handle *devh,
419 uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
420 uint8_t *data, uint16_t wLength, unsigned int timeout)
422 struct LIBUSB20_CONTROL_SETUP_DECODED req;
427 return (LIBUSB_ERROR_INVALID_PARAM);
429 if ((wLength != 0) && (data == NULL))
430 return (LIBUSB_ERROR_INVALID_PARAM);
432 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req);
434 req.bmRequestType = bmRequestType;
435 req.bRequest = bRequest;
438 req.wLength = wLength;
440 err = libusb20_dev_request_sync(devh, &req, data,
441 &actlen, timeout, 0);
443 if (err == LIBUSB20_ERROR_PIPE)
444 return (LIBUSB_ERROR_PIPE);
445 else if (err == LIBUSB20_ERROR_TIMEOUT)
446 return (LIBUSB_ERROR_TIMEOUT);
448 return (LIBUSB_ERROR_NO_DEVICE);
454 libusb10_do_transfer_cb(struct libusb_transfer *transfer)
459 ctx = GET_CONTEXT(NULL);
461 DPRINTF(ctx, LIBUSB_DEBUG_TRANSFER, "sync I/O done");
463 pdone = transfer->user_data;
468 * TODO: Replace the following function. Allocating and freeing on a
469 * per-transfer basis is slow. --HPS
472 libusb10_do_transfer(libusb_device_handle *devh,
473 uint8_t endpoint, uint8_t *data, int length,
474 int *transferred, unsigned int timeout, int type)
477 struct libusb_transfer *xfer;
478 volatile int complet;
482 return (LIBUSB_ERROR_INVALID_PARAM);
484 if ((length != 0) && (data == NULL))
485 return (LIBUSB_ERROR_INVALID_PARAM);
487 xfer = libusb_alloc_transfer(0);
489 return (LIBUSB_ERROR_NO_MEM);
491 ctx = libusb_get_device(devh)->ctx;
493 xfer->dev_handle = devh;
494 xfer->endpoint = endpoint;
496 xfer->timeout = timeout;
498 xfer->length = length;
499 xfer->user_data = (void *)&complet;
500 xfer->callback = libusb10_do_transfer_cb;
503 if ((ret = libusb_submit_transfer(xfer)) < 0) {
504 libusb_free_transfer(xfer);
507 while (complet == 0) {
508 if ((ret = libusb_handle_events(ctx)) < 0) {
509 libusb_cancel_transfer(xfer);
510 usleep(1000); /* nice it */
514 *transferred = xfer->actual_length;
516 switch (xfer->status) {
517 case LIBUSB_TRANSFER_COMPLETED:
520 case LIBUSB_TRANSFER_TIMED_OUT:
521 ret = LIBUSB_ERROR_TIMEOUT;
523 case LIBUSB_TRANSFER_OVERFLOW:
524 ret = LIBUSB_ERROR_OVERFLOW;
526 case LIBUSB_TRANSFER_STALL:
527 ret = LIBUSB_ERROR_PIPE;
529 case LIBUSB_TRANSFER_NO_DEVICE:
530 ret = LIBUSB_ERROR_NO_DEVICE;
533 ret = LIBUSB_ERROR_OTHER;
537 libusb_free_transfer(xfer);
542 libusb_bulk_transfer(libusb_device_handle *devh,
543 uint8_t endpoint, uint8_t *data, int length,
544 int *transferred, unsigned int timeout)
549 ctx = GET_CONTEXT(NULL);
550 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_bulk_transfer enter");
552 ret = libusb10_do_transfer(devh, endpoint, data, length, transferred,
553 timeout, LIBUSB_TRANSFER_TYPE_BULK);
555 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_bulk_transfer leave");
560 libusb_interrupt_transfer(libusb_device_handle *devh,
561 uint8_t endpoint, uint8_t *data, int length,
562 int *transferred, unsigned int timeout)
567 ctx = GET_CONTEXT(NULL);
568 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_interrupt_transfer enter");
570 ret = libusb10_do_transfer(devh, endpoint, data, length, transferred,
571 timeout, LIBUSB_TRANSFER_TYPE_INTERRUPT);
573 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_interrupt_transfer leave");