1/* $FreeBSD: head/lib/libusb/libusb10_io.c 199055 2009-11-08 20:03:52Z thompsa $ */
2/*-
3 * Copyright (c) 2009 Sylvestre Gallon. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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
24 * SUCH DAMAGE.
25 */
26
27#include <stdlib.h>
28#include <unistd.h>
29#include <stdio.h>
30#include <poll.h>
31#include <pthread.h>
32#include <time.h>
33#include <errno.h>
34#include <sys/queue.h>
35#include <sys/endian.h>
36
37#include "libusb20.h"
38#include "libusb20_desc.h"
39#include "libusb20_int.h"
40#include "libusb.h"
41#include "libusb10.h"
42
43UNEXPORTED void
44libusb10_add_pollfd(libusb_context *ctx, struct libusb_super_pollfd *pollfd,
45 struct libusb20_device *pdev, int fd, short events)
46{
47 if (ctx == NULL)
48 return; /* invalid */
49
50 if (pollfd->entry.tqe_prev != NULL)
51 return; /* already queued */
52
53 if (fd < 0)
54 return; /* invalid */
55
56 pollfd->pdev = pdev;
57 pollfd->pollfd.fd = fd;
58 pollfd->pollfd.events = events;
59
60 CTX_LOCK(ctx);
61 TAILQ_INSERT_TAIL(&ctx->pollfds, pollfd, entry);
62 CTX_UNLOCK(ctx);
63
64 if (ctx->fd_added_cb)
65 ctx->fd_added_cb(fd, events, ctx->fd_cb_user_data);
66}
67
68UNEXPORTED void
69libusb10_remove_pollfd(libusb_context *ctx, struct libusb_super_pollfd *pollfd)
70{
71 if (ctx == NULL)
72 return; /* invalid */
73
74 if (pollfd->entry.tqe_prev == NULL)
75 return; /* already dequeued */
76
77 CTX_LOCK(ctx);
78 TAILQ_REMOVE(&ctx->pollfds, pollfd, entry);
79 pollfd->entry.tqe_prev = NULL;
80 CTX_UNLOCK(ctx);
81
82 if (ctx->fd_removed_cb)
83 ctx->fd_removed_cb(pollfd->pollfd.fd, ctx->fd_cb_user_data);
84}
85
86/* This function must be called locked */
87
88static int
89libusb10_handle_events_sub(struct libusb_context *ctx, struct timeval *tv)
90{
91 struct libusb_device *dev;
92 struct libusb20_device **ppdev;
93 struct libusb_super_pollfd *pfd;
94 struct pollfd *fds;
95 struct libusb_super_transfer *sxfer;
96 struct libusb_transfer *uxfer;
97 nfds_t nfds;
98 int timeout;
99 int i;
100 int err;
101
102 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb10_handle_events_sub enter");
103
104 nfds = 0;
105 i = 0;
106 TAILQ_FOREACH(pfd, &ctx->pollfds, entry)
107 nfds++;
108
109 fds = alloca(sizeof(*fds) * nfds);
110 if (fds == NULL)
111 return (LIBUSB_ERROR_NO_MEM);
112
113 ppdev = alloca(sizeof(*ppdev) * nfds);
114 if (ppdev == NULL)
115 return (LIBUSB_ERROR_NO_MEM);
116
117 TAILQ_FOREACH(pfd, &ctx->pollfds, entry) {
118 fds[i].fd = pfd->pollfd.fd;
119 fds[i].events = pfd->pollfd.events;
120 fds[i].revents = 0;
121 ppdev[i] = pfd->pdev;
122 if (pfd->pdev != NULL)
123 libusb_get_device(pfd->pdev)->refcnt++;
124 i++;
125 }
126
127 if (tv == NULL)
128 timeout = -1;
129 else
130 timeout = (tv->tv_sec * 1000) + ((tv->tv_usec + 999) / 1000);
131
132 CTX_UNLOCK(ctx);
133 err = poll(fds, nfds, timeout);
134 CTX_LOCK(ctx);
135
136 if ((err == -1) && (errno == EINTR))
137 err = LIBUSB_ERROR_INTERRUPTED;
138 else if (err < 0)
139 err = LIBUSB_ERROR_IO;
140
141 if (err < 1) {
142 for (i = 0; i != nfds; i++) {
143 if (ppdev[i] != NULL) {
144 CTX_UNLOCK(ctx);
145 libusb_unref_device(libusb_get_device(ppdev[i]));
146 CTX_LOCK(ctx);
147 }
148 }
149 goto do_done;
150 }
151 for (i = 0; i != nfds; i++) {
152 if (ppdev[i] != NULL) {
153 dev = libusb_get_device(ppdev[i]);
154
155 if (fds[i].revents == 0)
156 err = 0; /* nothing to do */
157 else
158 err = libusb20_dev_process(ppdev[i]);
159
160 if (err) {
161 /* cancel all transfers - device is gone */
162 libusb10_cancel_all_transfer(dev);
163
164 /* remove USB device from polling loop */
165 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
166 }
167 CTX_UNLOCK(ctx);
168 libusb_unref_device(dev);
169 CTX_LOCK(ctx);
170
171 } else {
172 uint8_t dummy;
173
174 while (1) {
175 if (read(fds[i].fd, &dummy, 1) != 1)
176 break;
177 }
178 }
179 }
180
181 err = 0;
182
183do_done:
184
185 /* Do all done callbacks */
186
187 while ((sxfer = TAILQ_FIRST(&ctx->tr_done))) {
188 TAILQ_REMOVE(&ctx->tr_done, sxfer, entry);
189 sxfer->entry.tqe_prev = NULL;
190
191 ctx->tr_done_ref++;
192
193 CTX_UNLOCK(ctx);
194
195 uxfer = (struct libusb_transfer *)(
196 ((uint8_t *)sxfer) + sizeof(*sxfer));
197
198 if (uxfer->callback != NULL)
199 (uxfer->callback) (uxfer);
200
201 if (uxfer->flags & LIBUSB_TRANSFER_FREE_BUFFER)
202 free(uxfer->buffer);
203
204 if (uxfer->flags & LIBUSB_TRANSFER_FREE_TRANSFER)
205 libusb_free_transfer(uxfer);
206
207 CTX_LOCK(ctx);
208
209 ctx->tr_done_ref--;
210 ctx->tr_done_gen++;
211 }
212
213 /* Wakeup other waiters */
214 pthread_cond_broadcast(&ctx->ctx_cond);
215
216 return (err);
217}
218
219/* Polling and timing */
220
221int
222libusb_try_lock_events(libusb_context *ctx)
223{
224 int err;
225
226 ctx = GET_CONTEXT(ctx);
227 if (ctx == NULL)
228 return (1);
229
230 err = CTX_TRYLOCK(ctx);
231 if (err)
232 return (1);
233
234 err = (ctx->ctx_handler != NO_THREAD);
235 if (err)
236 CTX_UNLOCK(ctx);
237 else
238 ctx->ctx_handler = pthread_self();
239
240 return (err);
241}
242
243void
244libusb_lock_events(libusb_context *ctx)
245{
246 ctx = GET_CONTEXT(ctx);
247 CTX_LOCK(ctx);
248 if (ctx->ctx_handler == NO_THREAD)
249 ctx->ctx_handler = pthread_self();
250}
251
252void
253libusb_unlock_events(libusb_context *ctx)
254{
255 ctx = GET_CONTEXT(ctx);
256 if (ctx->ctx_handler == pthread_self()) {
257 ctx->ctx_handler = NO_THREAD;
258 pthread_cond_broadcast(&ctx->ctx_cond);
259 }
260 CTX_UNLOCK(ctx);
261}
262
263int
264libusb_event_handling_ok(libusb_context *ctx)
265{
266 ctx = GET_CONTEXT(ctx);
267 return (ctx->ctx_handler == pthread_self());
268}
269
270int
271libusb_event_handler_active(libusb_context *ctx)
272{
273 ctx = GET_CONTEXT(ctx);
274 return (ctx->ctx_handler != NO_THREAD);
275}
276
277void
278libusb_lock_event_waiters(libusb_context *ctx)
279{
280 ctx = GET_CONTEXT(ctx);
281 CTX_LOCK(ctx);
282}
283
284void
285libusb_unlock_event_waiters(libusb_context *ctx)
286{
287 ctx = GET_CONTEXT(ctx);
288 CTX_UNLOCK(ctx);
289}
290
291int
292libusb_wait_for_event(libusb_context *ctx, struct timeval *tv)
293{
294 struct timespec ts;
295 int err;
296
297 ctx = GET_CONTEXT(ctx);
298 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_wait_for_event enter");
299
300 if (tv == NULL) {
301 pthread_cond_wait(&ctx->ctx_cond,
302 &ctx->ctx_lock);
303 return (0);
304 }
305 err = clock_gettime(CLOCK_REALTIME, &ts);
306 if (err < 0)
307 return (LIBUSB_ERROR_OTHER);
308
309 ts.tv_sec = tv->tv_sec;
310 ts.tv_nsec = tv->tv_usec * 1000;
311 if (ts.tv_nsec >= 1000000000) {
312 ts.tv_nsec -= 1000000000;
313 ts.tv_sec++;
314 }
315 err = pthread_cond_timedwait(&ctx->ctx_cond,
316 &ctx->ctx_lock, &ts);
317
318 if (err == ETIMEDOUT)
319 return (1);
320
321 return (0);
322}
323
324int
325libusb_handle_events_timeout(libusb_context *ctx, struct timeval *tv)
326{
327 int err;
328
329 ctx = GET_CONTEXT(ctx);
330
331 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_timeout enter");
332
333 libusb_lock_events(ctx);
334
335 err = libusb_handle_events_locked(ctx, tv);
336
337 libusb_unlock_events(ctx);
338
339 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_timeout leave");
340
341 return (err);
342}
343
344int
345libusb_handle_events(libusb_context *ctx)
346{
347 return (libusb_handle_events_timeout(ctx, NULL));
348}
349
350int
351libusb_handle_events_locked(libusb_context *ctx, struct timeval *tv)
352{
353 int err;
354
355 ctx = GET_CONTEXT(ctx);
356
357 if (libusb_event_handling_ok(ctx)) {
358 err = libusb10_handle_events_sub(ctx, tv);
359 } else {
360 libusb_wait_for_event(ctx, tv);
361 err = 0;
362 }
363 return (err);
364}
365
366int
367libusb_get_next_timeout(libusb_context *ctx, struct timeval *tv)
368{
369 /* all timeouts are currently being done by the kernel */
370 timerclear(tv);
371 return (0);
372}
373
374void
375libusb_set_pollfd_notifiers(libusb_context *ctx,
376 libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb,
377 void *user_data)
378{
379 ctx = GET_CONTEXT(ctx);
380
381 ctx->fd_added_cb = added_cb;
382 ctx->fd_removed_cb = removed_cb;
383 ctx->fd_cb_user_data = user_data;
384}
385
386struct libusb_pollfd **
387libusb_get_pollfds(libusb_context *ctx)
388{
389 struct libusb_super_pollfd *pollfd;
390 libusb_pollfd **ret;
391 int i;
392
393 ctx = GET_CONTEXT(ctx);
394
395 CTX_LOCK(ctx);
396
397 i = 0;
398 TAILQ_FOREACH(pollfd, &ctx->pollfds, entry)
399 i++;
400
401 ret = calloc(i + 1, sizeof(struct libusb_pollfd *));
402 if (ret == NULL)
403 goto done;
404
405 i = 0;
406 TAILQ_FOREACH(pollfd, &ctx->pollfds, entry)
407 ret[i++] = &pollfd->pollfd;
408 ret[i] = NULL;
409
410done:
411 CTX_UNLOCK(ctx);
412 return (ret);
413}
414
415
416/* Synchronous device I/O */
417
418int
419libusb_control_transfer(libusb_device_handle *devh,
420 uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
421 uint8_t *data, uint16_t wLength, unsigned int timeout)
422{
423 struct LIBUSB20_CONTROL_SETUP_DECODED req;
424 int err;
425 uint16_t actlen;
426
427 if (devh == NULL)
428 return (LIBUSB_ERROR_INVALID_PARAM);
429
430 if ((wLength != 0) && (data == NULL))
431 return (LIBUSB_ERROR_INVALID_PARAM);
432
433 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req);
434
435 req.bmRequestType = bmRequestType;
436 req.bRequest = bRequest;
437 req.wValue = wValue;
438 req.wIndex = wIndex;
439 req.wLength = wLength;
440
441 err = libusb20_dev_request_sync(devh, &req, data,
442 &actlen, timeout, 0);
443
444 if (err == LIBUSB20_ERROR_PIPE)
445 return (LIBUSB_ERROR_PIPE);
446 else if (err == LIBUSB20_ERROR_TIMEOUT)
447 return (LIBUSB_ERROR_TIMEOUT);
448 else if (err)
449 return (LIBUSB_ERROR_NO_DEVICE);
450
451 return (actlen);
452}
453
454static void
455libusb10_do_transfer_cb(struct libusb_transfer *transfer)
456{
457 libusb_context *ctx;
458 int *pdone;
459
460 ctx = GET_CONTEXT(NULL);
461
462 DPRINTF(ctx, LIBUSB_DEBUG_TRANSFER, "sync I/O done");
463
464 pdone = transfer->user_data;
465 *pdone = 1;
466}
467
468/*
469 * TODO: Replace the following function. Allocating and freeing on a
470 * per-transfer basis is slow. --HPS
471 */
472static int
473libusb10_do_transfer(libusb_device_handle *devh,
474 uint8_t endpoint, uint8_t *data, int length,
475 int *transferred, unsigned int timeout, int type)
476{
477 libusb_context *ctx;
478 struct libusb_transfer *xfer;
479 volatile int complet;
480 int ret;
481
482 if (devh == NULL)
483 return (LIBUSB_ERROR_INVALID_PARAM);
484
485 if ((length != 0) && (data == NULL))
486 return (LIBUSB_ERROR_INVALID_PARAM);
487
488 xfer = libusb_alloc_transfer(0);
489 if (xfer == NULL)
490 return (LIBUSB_ERROR_NO_MEM);
491
492 ctx = libusb_get_device(devh)->ctx;
493
494 xfer->dev_handle = devh;
495 xfer->endpoint = endpoint;
496 xfer->type = type;
497 xfer->timeout = timeout;
498 xfer->buffer = data;
499 xfer->length = length;
500 xfer->user_data = (void *)&complet;
501 xfer->callback = libusb10_do_transfer_cb;
502 complet = 0;
503
504 if ((ret = libusb_submit_transfer(xfer)) < 0) {
505 libusb_free_transfer(xfer);
506 return (ret);
507 }
508 while (complet == 0) {
509 if ((ret = libusb_handle_events(ctx)) < 0) {
510 libusb_cancel_transfer(xfer);
511 usleep(1000); /* nice it */
512 }
513 }
514
515 *transferred = xfer->actual_length;
516
517 switch (xfer->status) {
518 case LIBUSB_TRANSFER_COMPLETED:
519 ret = 0;
520 break;
521 case LIBUSB_TRANSFER_TIMED_OUT:
522 ret = LIBUSB_ERROR_TIMEOUT;
523 break;
524 case LIBUSB_TRANSFER_OVERFLOW:
525 ret = LIBUSB_ERROR_OVERFLOW;
526 break;
527 case LIBUSB_TRANSFER_STALL:
528 ret = LIBUSB_ERROR_PIPE;
529 break;
530 case LIBUSB_TRANSFER_NO_DEVICE:
531 ret = LIBUSB_ERROR_NO_DEVICE;
532 break;
533 default:
534 ret = LIBUSB_ERROR_OTHER;
535 break;
536 }
537
538 libusb_free_transfer(xfer);
539 return (ret);
540}
541
542int
543libusb_bulk_transfer(libusb_device_handle *devh,
544 uint8_t endpoint, uint8_t *data, int length,
545 int *transferred, unsigned int timeout)
546{
547 libusb_context *ctx;
548 int ret;
549
550 ctx = GET_CONTEXT(NULL);
551 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_bulk_transfer enter");
552
553 ret = libusb10_do_transfer(devh, endpoint, data, length, transferred,
554 timeout, LIBUSB_TRANSFER_TYPE_BULK);
555
556 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_bulk_transfer leave");
557 return (ret);
558}
559
560int
561libusb_interrupt_transfer(libusb_device_handle *devh,
562 uint8_t endpoint, uint8_t *data, int length,
563 int *transferred, unsigned int timeout)
564{
565 libusb_context *ctx;
566 int ret;
567
568 ctx = GET_CONTEXT(NULL);
569 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_interrupt_transfer enter");
570
571 ret = libusb10_do_transfer(devh, endpoint, data, length, transferred,
572 timeout, LIBUSB_TRANSFER_TYPE_INTERRUPT);
573
574 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_interrupt_transfer leave");
575 return (ret);
576}
577
578uint8_t *
579libusb_get_iso_packet_buffer(struct libusb_transfer *transfer, uint32_t index)
580{
581 uint8_t *ptr;
582 uint32_t n;
583
584 if (transfer->num_iso_packets < 0)
585 return (NULL);
586
587 if (index >= (uint32_t)transfer->num_iso_packets)
588 return (NULL);
589
590 ptr = transfer->buffer;
591 if (ptr == NULL)
592 return (NULL);
593
594 for (n = 0; n != index; n++) {
595 ptr += transfer->iso_packet_desc[n].length;
596 }
597 return (ptr);
598}
599
600uint8_t *
601libusb_get_iso_packet_buffer_simple(struct libusb_transfer *transfer, uint32_t index)
602{
603 uint8_t *ptr;
604
605 if (transfer->num_iso_packets < 0)
606 return (NULL);
607
608 if (index >= (uint32_t)transfer->num_iso_packets)
609 return (NULL);
610
611 ptr = transfer->buffer;
612 if (ptr == NULL)
613 return (NULL);
614
615 ptr += transfer->iso_packet_desc[0].length * index;
616
617 return (ptr);
618}
619
620void
621libusb_set_iso_packet_lengths(struct libusb_transfer *transfer, uint32_t length)
622{
623 int n;
624
625 if (transfer->num_iso_packets < 0)
626 return;
627
628 for (n = 0; n != transfer->num_iso_packets; n++)
629 transfer->iso_packet_desc[n].length = length;
630}
631
632uint8_t *
633libusb_control_transfer_get_data(struct libusb_transfer *transfer)
634{
635 if (transfer->buffer == NULL)
636 return (NULL);
637
638 return (transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE);
639}
640
641struct libusb_control_setup *
642libusb_control_transfer_get_setup(struct libusb_transfer *transfer)
643{
644 return ((struct libusb_control_setup *)transfer->buffer);
645}
646
647void
648libusb_fill_control_setup(uint8_t *buf, uint8_t bmRequestType,
649 uint8_t bRequest, uint16_t wValue,
650 uint16_t wIndex, uint16_t wLength)
651{
652 struct libusb_control_setup *req = (struct libusb_control_setup *)buf;
653
654 /* The alignment is OK for all fields below. */
655 req->bmRequestType = bmRequestType;
656 req->bRequest = bRequest;
657 req->wValue = htole16(wValue);
658 req->wIndex = htole16(wIndex);
659 req->wLength = htole16(wLength);
660}
661
662void
663libusb_fill_control_transfer(struct libusb_transfer *transfer,
664 libusb_device_handle *devh, uint8_t *buf,
665 libusb_transfer_cb_fn callback, void *user_data,
666 uint32_t timeout)
667{
668 struct libusb_control_setup *setup = (struct libusb_control_setup *)buf;
669
670 transfer->dev_handle = devh;
671 transfer->endpoint = 0;
672 transfer->type = LIBUSB_TRANSFER_TYPE_CONTROL;
673 transfer->timeout = timeout;
674 transfer->buffer = buf;
675 if (setup != NULL)
676 transfer->length = LIBUSB_CONTROL_SETUP_SIZE
677 + le16toh(setup->wLength);
678 else
679 transfer->length = 0;
680 transfer->user_data = user_data;
681 transfer->callback = callback;
682
683}
684
685void
686libusb_fill_bulk_transfer(struct libusb_transfer *transfer,
687 libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf,
688 int length, libusb_transfer_cb_fn callback, void *user_data,
689 uint32_t timeout)
690{
691 transfer->dev_handle = devh;
692 transfer->endpoint = endpoint;
693 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
694 transfer->timeout = timeout;
695 transfer->buffer = buf;
696 transfer->length = length;
697 transfer->user_data = user_data;
698 transfer->callback = callback;
699}
700
701void
702libusb_fill_interrupt_transfer(struct libusb_transfer *transfer,
703 libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf,
704 int length, libusb_transfer_cb_fn callback, void *user_data,
705 uint32_t timeout)
706{
707 transfer->dev_handle = devh;
708 transfer->endpoint = endpoint;
709 transfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT;
710 transfer->timeout = timeout;
711 transfer->buffer = buf;
712 transfer->length = length;
713 transfer->user_data = user_data;
714 transfer->callback = callback;
715}
716
717void
718libusb_fill_iso_transfer(struct libusb_transfer *transfer,
719 libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf,
720 int length, int npacket, libusb_transfer_cb_fn callback,
721 void *user_data, uint32_t timeout)
722{
723 transfer->dev_handle = devh;
724 transfer->endpoint = endpoint;
725 transfer->type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS;
726 transfer->timeout = timeout;
727 transfer->buffer = buf;
728 transfer->length = length;
729 transfer->num_iso_packets = npacket;
730 transfer->user_data = user_data;
731 transfer->callback = callback;
732}
733
2/*-
3 * Copyright (c) 2009 Sylvestre Gallon. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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
24 * SUCH DAMAGE.
25 */
26
27#include <stdlib.h>
28#include <unistd.h>
29#include <stdio.h>
30#include <poll.h>
31#include <pthread.h>
32#include <time.h>
33#include <errno.h>
34#include <sys/queue.h>
35#include <sys/endian.h>
36
37#include "libusb20.h"
38#include "libusb20_desc.h"
39#include "libusb20_int.h"
40#include "libusb.h"
41#include "libusb10.h"
42
43UNEXPORTED void
44libusb10_add_pollfd(libusb_context *ctx, struct libusb_super_pollfd *pollfd,
45 struct libusb20_device *pdev, int fd, short events)
46{
47 if (ctx == NULL)
48 return; /* invalid */
49
50 if (pollfd->entry.tqe_prev != NULL)
51 return; /* already queued */
52
53 if (fd < 0)
54 return; /* invalid */
55
56 pollfd->pdev = pdev;
57 pollfd->pollfd.fd = fd;
58 pollfd->pollfd.events = events;
59
60 CTX_LOCK(ctx);
61 TAILQ_INSERT_TAIL(&ctx->pollfds, pollfd, entry);
62 CTX_UNLOCK(ctx);
63
64 if (ctx->fd_added_cb)
65 ctx->fd_added_cb(fd, events, ctx->fd_cb_user_data);
66}
67
68UNEXPORTED void
69libusb10_remove_pollfd(libusb_context *ctx, struct libusb_super_pollfd *pollfd)
70{
71 if (ctx == NULL)
72 return; /* invalid */
73
74 if (pollfd->entry.tqe_prev == NULL)
75 return; /* already dequeued */
76
77 CTX_LOCK(ctx);
78 TAILQ_REMOVE(&ctx->pollfds, pollfd, entry);
79 pollfd->entry.tqe_prev = NULL;
80 CTX_UNLOCK(ctx);
81
82 if (ctx->fd_removed_cb)
83 ctx->fd_removed_cb(pollfd->pollfd.fd, ctx->fd_cb_user_data);
84}
85
86/* This function must be called locked */
87
88static int
89libusb10_handle_events_sub(struct libusb_context *ctx, struct timeval *tv)
90{
91 struct libusb_device *dev;
92 struct libusb20_device **ppdev;
93 struct libusb_super_pollfd *pfd;
94 struct pollfd *fds;
95 struct libusb_super_transfer *sxfer;
96 struct libusb_transfer *uxfer;
97 nfds_t nfds;
98 int timeout;
99 int i;
100 int err;
101
102 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb10_handle_events_sub enter");
103
104 nfds = 0;
105 i = 0;
106 TAILQ_FOREACH(pfd, &ctx->pollfds, entry)
107 nfds++;
108
109 fds = alloca(sizeof(*fds) * nfds);
110 if (fds == NULL)
111 return (LIBUSB_ERROR_NO_MEM);
112
113 ppdev = alloca(sizeof(*ppdev) * nfds);
114 if (ppdev == NULL)
115 return (LIBUSB_ERROR_NO_MEM);
116
117 TAILQ_FOREACH(pfd, &ctx->pollfds, entry) {
118 fds[i].fd = pfd->pollfd.fd;
119 fds[i].events = pfd->pollfd.events;
120 fds[i].revents = 0;
121 ppdev[i] = pfd->pdev;
122 if (pfd->pdev != NULL)
123 libusb_get_device(pfd->pdev)->refcnt++;
124 i++;
125 }
126
127 if (tv == NULL)
128 timeout = -1;
129 else
130 timeout = (tv->tv_sec * 1000) + ((tv->tv_usec + 999) / 1000);
131
132 CTX_UNLOCK(ctx);
133 err = poll(fds, nfds, timeout);
134 CTX_LOCK(ctx);
135
136 if ((err == -1) && (errno == EINTR))
137 err = LIBUSB_ERROR_INTERRUPTED;
138 else if (err < 0)
139 err = LIBUSB_ERROR_IO;
140
141 if (err < 1) {
142 for (i = 0; i != nfds; i++) {
143 if (ppdev[i] != NULL) {
144 CTX_UNLOCK(ctx);
145 libusb_unref_device(libusb_get_device(ppdev[i]));
146 CTX_LOCK(ctx);
147 }
148 }
149 goto do_done;
150 }
151 for (i = 0; i != nfds; i++) {
152 if (ppdev[i] != NULL) {
153 dev = libusb_get_device(ppdev[i]);
154
155 if (fds[i].revents == 0)
156 err = 0; /* nothing to do */
157 else
158 err = libusb20_dev_process(ppdev[i]);
159
160 if (err) {
161 /* cancel all transfers - device is gone */
162 libusb10_cancel_all_transfer(dev);
163
164 /* remove USB device from polling loop */
165 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
166 }
167 CTX_UNLOCK(ctx);
168 libusb_unref_device(dev);
169 CTX_LOCK(ctx);
170
171 } else {
172 uint8_t dummy;
173
174 while (1) {
175 if (read(fds[i].fd, &dummy, 1) != 1)
176 break;
177 }
178 }
179 }
180
181 err = 0;
182
183do_done:
184
185 /* Do all done callbacks */
186
187 while ((sxfer = TAILQ_FIRST(&ctx->tr_done))) {
188 TAILQ_REMOVE(&ctx->tr_done, sxfer, entry);
189 sxfer->entry.tqe_prev = NULL;
190
191 ctx->tr_done_ref++;
192
193 CTX_UNLOCK(ctx);
194
195 uxfer = (struct libusb_transfer *)(
196 ((uint8_t *)sxfer) + sizeof(*sxfer));
197
198 if (uxfer->callback != NULL)
199 (uxfer->callback) (uxfer);
200
201 if (uxfer->flags & LIBUSB_TRANSFER_FREE_BUFFER)
202 free(uxfer->buffer);
203
204 if (uxfer->flags & LIBUSB_TRANSFER_FREE_TRANSFER)
205 libusb_free_transfer(uxfer);
206
207 CTX_LOCK(ctx);
208
209 ctx->tr_done_ref--;
210 ctx->tr_done_gen++;
211 }
212
213 /* Wakeup other waiters */
214 pthread_cond_broadcast(&ctx->ctx_cond);
215
216 return (err);
217}
218
219/* Polling and timing */
220
221int
222libusb_try_lock_events(libusb_context *ctx)
223{
224 int err;
225
226 ctx = GET_CONTEXT(ctx);
227 if (ctx == NULL)
228 return (1);
229
230 err = CTX_TRYLOCK(ctx);
231 if (err)
232 return (1);
233
234 err = (ctx->ctx_handler != NO_THREAD);
235 if (err)
236 CTX_UNLOCK(ctx);
237 else
238 ctx->ctx_handler = pthread_self();
239
240 return (err);
241}
242
243void
244libusb_lock_events(libusb_context *ctx)
245{
246 ctx = GET_CONTEXT(ctx);
247 CTX_LOCK(ctx);
248 if (ctx->ctx_handler == NO_THREAD)
249 ctx->ctx_handler = pthread_self();
250}
251
252void
253libusb_unlock_events(libusb_context *ctx)
254{
255 ctx = GET_CONTEXT(ctx);
256 if (ctx->ctx_handler == pthread_self()) {
257 ctx->ctx_handler = NO_THREAD;
258 pthread_cond_broadcast(&ctx->ctx_cond);
259 }
260 CTX_UNLOCK(ctx);
261}
262
263int
264libusb_event_handling_ok(libusb_context *ctx)
265{
266 ctx = GET_CONTEXT(ctx);
267 return (ctx->ctx_handler == pthread_self());
268}
269
270int
271libusb_event_handler_active(libusb_context *ctx)
272{
273 ctx = GET_CONTEXT(ctx);
274 return (ctx->ctx_handler != NO_THREAD);
275}
276
277void
278libusb_lock_event_waiters(libusb_context *ctx)
279{
280 ctx = GET_CONTEXT(ctx);
281 CTX_LOCK(ctx);
282}
283
284void
285libusb_unlock_event_waiters(libusb_context *ctx)
286{
287 ctx = GET_CONTEXT(ctx);
288 CTX_UNLOCK(ctx);
289}
290
291int
292libusb_wait_for_event(libusb_context *ctx, struct timeval *tv)
293{
294 struct timespec ts;
295 int err;
296
297 ctx = GET_CONTEXT(ctx);
298 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_wait_for_event enter");
299
300 if (tv == NULL) {
301 pthread_cond_wait(&ctx->ctx_cond,
302 &ctx->ctx_lock);
303 return (0);
304 }
305 err = clock_gettime(CLOCK_REALTIME, &ts);
306 if (err < 0)
307 return (LIBUSB_ERROR_OTHER);
308
309 ts.tv_sec = tv->tv_sec;
310 ts.tv_nsec = tv->tv_usec * 1000;
311 if (ts.tv_nsec >= 1000000000) {
312 ts.tv_nsec -= 1000000000;
313 ts.tv_sec++;
314 }
315 err = pthread_cond_timedwait(&ctx->ctx_cond,
316 &ctx->ctx_lock, &ts);
317
318 if (err == ETIMEDOUT)
319 return (1);
320
321 return (0);
322}
323
324int
325libusb_handle_events_timeout(libusb_context *ctx, struct timeval *tv)
326{
327 int err;
328
329 ctx = GET_CONTEXT(ctx);
330
331 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_timeout enter");
332
333 libusb_lock_events(ctx);
334
335 err = libusb_handle_events_locked(ctx, tv);
336
337 libusb_unlock_events(ctx);
338
339 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_timeout leave");
340
341 return (err);
342}
343
344int
345libusb_handle_events(libusb_context *ctx)
346{
347 return (libusb_handle_events_timeout(ctx, NULL));
348}
349
350int
351libusb_handle_events_locked(libusb_context *ctx, struct timeval *tv)
352{
353 int err;
354
355 ctx = GET_CONTEXT(ctx);
356
357 if (libusb_event_handling_ok(ctx)) {
358 err = libusb10_handle_events_sub(ctx, tv);
359 } else {
360 libusb_wait_for_event(ctx, tv);
361 err = 0;
362 }
363 return (err);
364}
365
366int
367libusb_get_next_timeout(libusb_context *ctx, struct timeval *tv)
368{
369 /* all timeouts are currently being done by the kernel */
370 timerclear(tv);
371 return (0);
372}
373
374void
375libusb_set_pollfd_notifiers(libusb_context *ctx,
376 libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb,
377 void *user_data)
378{
379 ctx = GET_CONTEXT(ctx);
380
381 ctx->fd_added_cb = added_cb;
382 ctx->fd_removed_cb = removed_cb;
383 ctx->fd_cb_user_data = user_data;
384}
385
386struct libusb_pollfd **
387libusb_get_pollfds(libusb_context *ctx)
388{
389 struct libusb_super_pollfd *pollfd;
390 libusb_pollfd **ret;
391 int i;
392
393 ctx = GET_CONTEXT(ctx);
394
395 CTX_LOCK(ctx);
396
397 i = 0;
398 TAILQ_FOREACH(pollfd, &ctx->pollfds, entry)
399 i++;
400
401 ret = calloc(i + 1, sizeof(struct libusb_pollfd *));
402 if (ret == NULL)
403 goto done;
404
405 i = 0;
406 TAILQ_FOREACH(pollfd, &ctx->pollfds, entry)
407 ret[i++] = &pollfd->pollfd;
408 ret[i] = NULL;
409
410done:
411 CTX_UNLOCK(ctx);
412 return (ret);
413}
414
415
416/* Synchronous device I/O */
417
418int
419libusb_control_transfer(libusb_device_handle *devh,
420 uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
421 uint8_t *data, uint16_t wLength, unsigned int timeout)
422{
423 struct LIBUSB20_CONTROL_SETUP_DECODED req;
424 int err;
425 uint16_t actlen;
426
427 if (devh == NULL)
428 return (LIBUSB_ERROR_INVALID_PARAM);
429
430 if ((wLength != 0) && (data == NULL))
431 return (LIBUSB_ERROR_INVALID_PARAM);
432
433 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req);
434
435 req.bmRequestType = bmRequestType;
436 req.bRequest = bRequest;
437 req.wValue = wValue;
438 req.wIndex = wIndex;
439 req.wLength = wLength;
440
441 err = libusb20_dev_request_sync(devh, &req, data,
442 &actlen, timeout, 0);
443
444 if (err == LIBUSB20_ERROR_PIPE)
445 return (LIBUSB_ERROR_PIPE);
446 else if (err == LIBUSB20_ERROR_TIMEOUT)
447 return (LIBUSB_ERROR_TIMEOUT);
448 else if (err)
449 return (LIBUSB_ERROR_NO_DEVICE);
450
451 return (actlen);
452}
453
454static void
455libusb10_do_transfer_cb(struct libusb_transfer *transfer)
456{
457 libusb_context *ctx;
458 int *pdone;
459
460 ctx = GET_CONTEXT(NULL);
461
462 DPRINTF(ctx, LIBUSB_DEBUG_TRANSFER, "sync I/O done");
463
464 pdone = transfer->user_data;
465 *pdone = 1;
466}
467
468/*
469 * TODO: Replace the following function. Allocating and freeing on a
470 * per-transfer basis is slow. --HPS
471 */
472static int
473libusb10_do_transfer(libusb_device_handle *devh,
474 uint8_t endpoint, uint8_t *data, int length,
475 int *transferred, unsigned int timeout, int type)
476{
477 libusb_context *ctx;
478 struct libusb_transfer *xfer;
479 volatile int complet;
480 int ret;
481
482 if (devh == NULL)
483 return (LIBUSB_ERROR_INVALID_PARAM);
484
485 if ((length != 0) && (data == NULL))
486 return (LIBUSB_ERROR_INVALID_PARAM);
487
488 xfer = libusb_alloc_transfer(0);
489 if (xfer == NULL)
490 return (LIBUSB_ERROR_NO_MEM);
491
492 ctx = libusb_get_device(devh)->ctx;
493
494 xfer->dev_handle = devh;
495 xfer->endpoint = endpoint;
496 xfer->type = type;
497 xfer->timeout = timeout;
498 xfer->buffer = data;
499 xfer->length = length;
500 xfer->user_data = (void *)&complet;
501 xfer->callback = libusb10_do_transfer_cb;
502 complet = 0;
503
504 if ((ret = libusb_submit_transfer(xfer)) < 0) {
505 libusb_free_transfer(xfer);
506 return (ret);
507 }
508 while (complet == 0) {
509 if ((ret = libusb_handle_events(ctx)) < 0) {
510 libusb_cancel_transfer(xfer);
511 usleep(1000); /* nice it */
512 }
513 }
514
515 *transferred = xfer->actual_length;
516
517 switch (xfer->status) {
518 case LIBUSB_TRANSFER_COMPLETED:
519 ret = 0;
520 break;
521 case LIBUSB_TRANSFER_TIMED_OUT:
522 ret = LIBUSB_ERROR_TIMEOUT;
523 break;
524 case LIBUSB_TRANSFER_OVERFLOW:
525 ret = LIBUSB_ERROR_OVERFLOW;
526 break;
527 case LIBUSB_TRANSFER_STALL:
528 ret = LIBUSB_ERROR_PIPE;
529 break;
530 case LIBUSB_TRANSFER_NO_DEVICE:
531 ret = LIBUSB_ERROR_NO_DEVICE;
532 break;
533 default:
534 ret = LIBUSB_ERROR_OTHER;
535 break;
536 }
537
538 libusb_free_transfer(xfer);
539 return (ret);
540}
541
542int
543libusb_bulk_transfer(libusb_device_handle *devh,
544 uint8_t endpoint, uint8_t *data, int length,
545 int *transferred, unsigned int timeout)
546{
547 libusb_context *ctx;
548 int ret;
549
550 ctx = GET_CONTEXT(NULL);
551 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_bulk_transfer enter");
552
553 ret = libusb10_do_transfer(devh, endpoint, data, length, transferred,
554 timeout, LIBUSB_TRANSFER_TYPE_BULK);
555
556 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_bulk_transfer leave");
557 return (ret);
558}
559
560int
561libusb_interrupt_transfer(libusb_device_handle *devh,
562 uint8_t endpoint, uint8_t *data, int length,
563 int *transferred, unsigned int timeout)
564{
565 libusb_context *ctx;
566 int ret;
567
568 ctx = GET_CONTEXT(NULL);
569 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_interrupt_transfer enter");
570
571 ret = libusb10_do_transfer(devh, endpoint, data, length, transferred,
572 timeout, LIBUSB_TRANSFER_TYPE_INTERRUPT);
573
574 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_interrupt_transfer leave");
575 return (ret);
576}
577
578uint8_t *
579libusb_get_iso_packet_buffer(struct libusb_transfer *transfer, uint32_t index)
580{
581 uint8_t *ptr;
582 uint32_t n;
583
584 if (transfer->num_iso_packets < 0)
585 return (NULL);
586
587 if (index >= (uint32_t)transfer->num_iso_packets)
588 return (NULL);
589
590 ptr = transfer->buffer;
591 if (ptr == NULL)
592 return (NULL);
593
594 for (n = 0; n != index; n++) {
595 ptr += transfer->iso_packet_desc[n].length;
596 }
597 return (ptr);
598}
599
600uint8_t *
601libusb_get_iso_packet_buffer_simple(struct libusb_transfer *transfer, uint32_t index)
602{
603 uint8_t *ptr;
604
605 if (transfer->num_iso_packets < 0)
606 return (NULL);
607
608 if (index >= (uint32_t)transfer->num_iso_packets)
609 return (NULL);
610
611 ptr = transfer->buffer;
612 if (ptr == NULL)
613 return (NULL);
614
615 ptr += transfer->iso_packet_desc[0].length * index;
616
617 return (ptr);
618}
619
620void
621libusb_set_iso_packet_lengths(struct libusb_transfer *transfer, uint32_t length)
622{
623 int n;
624
625 if (transfer->num_iso_packets < 0)
626 return;
627
628 for (n = 0; n != transfer->num_iso_packets; n++)
629 transfer->iso_packet_desc[n].length = length;
630}
631
632uint8_t *
633libusb_control_transfer_get_data(struct libusb_transfer *transfer)
634{
635 if (transfer->buffer == NULL)
636 return (NULL);
637
638 return (transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE);
639}
640
641struct libusb_control_setup *
642libusb_control_transfer_get_setup(struct libusb_transfer *transfer)
643{
644 return ((struct libusb_control_setup *)transfer->buffer);
645}
646
647void
648libusb_fill_control_setup(uint8_t *buf, uint8_t bmRequestType,
649 uint8_t bRequest, uint16_t wValue,
650 uint16_t wIndex, uint16_t wLength)
651{
652 struct libusb_control_setup *req = (struct libusb_control_setup *)buf;
653
654 /* The alignment is OK for all fields below. */
655 req->bmRequestType = bmRequestType;
656 req->bRequest = bRequest;
657 req->wValue = htole16(wValue);
658 req->wIndex = htole16(wIndex);
659 req->wLength = htole16(wLength);
660}
661
662void
663libusb_fill_control_transfer(struct libusb_transfer *transfer,
664 libusb_device_handle *devh, uint8_t *buf,
665 libusb_transfer_cb_fn callback, void *user_data,
666 uint32_t timeout)
667{
668 struct libusb_control_setup *setup = (struct libusb_control_setup *)buf;
669
670 transfer->dev_handle = devh;
671 transfer->endpoint = 0;
672 transfer->type = LIBUSB_TRANSFER_TYPE_CONTROL;
673 transfer->timeout = timeout;
674 transfer->buffer = buf;
675 if (setup != NULL)
676 transfer->length = LIBUSB_CONTROL_SETUP_SIZE
677 + le16toh(setup->wLength);
678 else
679 transfer->length = 0;
680 transfer->user_data = user_data;
681 transfer->callback = callback;
682
683}
684
685void
686libusb_fill_bulk_transfer(struct libusb_transfer *transfer,
687 libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf,
688 int length, libusb_transfer_cb_fn callback, void *user_data,
689 uint32_t timeout)
690{
691 transfer->dev_handle = devh;
692 transfer->endpoint = endpoint;
693 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
694 transfer->timeout = timeout;
695 transfer->buffer = buf;
696 transfer->length = length;
697 transfer->user_data = user_data;
698 transfer->callback = callback;
699}
700
701void
702libusb_fill_interrupt_transfer(struct libusb_transfer *transfer,
703 libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf,
704 int length, libusb_transfer_cb_fn callback, void *user_data,
705 uint32_t timeout)
706{
707 transfer->dev_handle = devh;
708 transfer->endpoint = endpoint;
709 transfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT;
710 transfer->timeout = timeout;
711 transfer->buffer = buf;
712 transfer->length = length;
713 transfer->user_data = user_data;
714 transfer->callback = callback;
715}
716
717void
718libusb_fill_iso_transfer(struct libusb_transfer *transfer,
719 libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf,
720 int length, int npacket, libusb_transfer_cb_fn callback,
721 void *user_data, uint32_t timeout)
722{
723 transfer->dev_handle = devh;
724 transfer->endpoint = endpoint;
725 transfer->type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS;
726 transfer->timeout = timeout;
727 transfer->buffer = buf;
728 transfer->length = length;
729 transfer->num_iso_packets = npacket;
730 transfer->user_data = user_data;
731 transfer->callback = callback;
732}
733