2 * I/O functions for libusb
3 * Copyright (C) 2007 Daniel Drake <dsd@gentoo.org>
4 * Copyright (c) 2001 Johannes Erdfelt <johannes@erdfelt.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 #include <sys/select.h>
33 /* signalfd() support is present in glibc-2.7 onwards, but glibc-2.7 contains
34 * a bug where the header is neither installed or compilable. This will be
35 * fixed for glibc-2.8. */
36 #if __GLIBC_PREREQ(2, 8)
37 #include <sys/signalfd.h>
47 /* this is a list of in-flight fp_urb_handles, sorted by timeout expiration.
48 * URBs to timeout the soonest are placed at the beginning of the list, URBs
49 * that will time out later are placed after, and urbs with infinite timeout
50 * are always placed at the very end. */
51 static struct list_head flying_urbs;
53 static int setup_signalfd(int _signum)
58 fp_dbg("signal %d", _signum);
61 sigaddset(&sigset, _signum);
62 sigfd = signalfd(-1, &sigset, 0);
64 fp_err("signalfd failed, code=%d errno=%d", sigfd, errno);
67 fp_dbg("got signalfd %d", sigfd);
71 sigaddset(&sigset, _signum);
72 return sigprocmask(SIG_BLOCK, &sigset, NULL);
75 int usbi_io_init(int _signum)
77 list_init(&flying_urbs);
78 return setup_signalfd(signum);
81 void usbi_io_exit(void)
86 static int calculate_timeout(struct libusb_urb_handle *urbh,
90 struct timespec current_time;
91 struct sigevent sigevt = {
92 .sigev_notify = SIGEV_SIGNAL,
93 .sigev_signo = signum,
95 struct itimerspec itspec;
96 struct timespec *it_value = &itspec.it_value;
101 r = clock_gettime(CLOCK_MONOTONIC, ¤t_time);
103 fp_err("failed to read monotonic clock, errno=%d", errno);
107 r = timer_create(CLOCK_MONOTONIC, &sigevt, &urbh->timer);
109 fp_err("failed to create monotonic timer");
113 memset(&itspec, 0, sizeof(itspec));
114 it_value->tv_sec = current_time.tv_sec + (timeout / 1000);
115 it_value->tv_nsec = current_time.tv_nsec +
116 ((timeout % 1000) * 1000000);
118 if (it_value->tv_nsec > 1000000000) {
119 it_value->tv_nsec -= 1000000000;
123 r = timer_settime(&urbh->timer, TIMER_ABSTIME, &itspec, NULL);
125 fp_err("failed to arm monotonic timer");
129 urbh->timeout = itspec.it_value;
134 static void add_to_flying_list(struct libusb_urb_handle *urbh)
136 struct libusb_urb_handle *cur;
137 struct timespec *timeout = &urbh->timeout;
139 /* if we have no other flying urbs, start the list with this one */
140 if (list_empty(&flying_urbs)) {
141 list_add(&urbh->list, &flying_urbs);
145 /* if we have infinite timeout, append to end of list */
146 if (!TIMESPEC_IS_SET(timeout)) {
147 list_add_tail(&urbh->list, &flying_urbs);
151 /* otherwise, find appropriate place in list */
152 list_for_each_entry(cur, &flying_urbs, list) {
153 /* find first timeout that occurs after the urbh in question */
154 struct timespec *cur_ts = &cur->timeout;
156 if (!TIMESPEC_IS_SET(cur_ts) || (cur_ts->tv_sec > timeout->tv_sec) ||
157 (cur_ts->tv_sec == timeout->tv_sec &&
158 cur_ts->tv_nsec > timeout->tv_nsec)) {
159 list_add_tail(&urbh->list, &cur->list);
164 /* otherwise we need to be inserted at the end */
165 list_add_tail(&urbh->list, &flying_urbs);
168 static int submit_urb(struct libusb_dev_handle *devh,
169 struct libusb_urb_handle *urbh)
172 struct usb_urb *urb = &urbh->urb;
173 int to_be_transferred = urbh->transfer_len - urbh->transferred;
175 urb->type = urbh->urb_type;
176 urb->endpoint = urbh->endpoint;
177 urb->buffer = urbh->buffer + urbh->transferred;
178 urb->buffer_length = MIN(to_be_transferred, MAX_URB_BUFFER_LENGTH);
181 /* FIXME: for requests that we have to split into multiple URBs, we should
182 * submit all the URBs instantly: submit, submit, submit, reap, reap, reap
183 * rather than: submit, reap, submit, reap, submit, reap
184 * this will improve performance and fix bugs concerning behaviour when
185 * the user submits two similar multiple-urb requests */
186 fp_dbg("transferring %d from %d bytes", urb->buffer_length,
189 r = ioctl(devh->fd, IOCTL_USB_SUBMITURB, &urbh->urb);
191 fp_err("submiturb failed error %d errno=%d", r, errno);
195 add_to_flying_list(urbh);
199 API_EXPORTED struct libusb_urb_handle *libusb_async_control_transfer(
200 struct libusb_dev_handle *devh, struct libusb_control_transfer *transfer,
201 libusb_ctrl_cb_fn callback, void *user_data, unsigned int timeout)
203 struct libusb_urb_handle *urbh = malloc(sizeof(*urbh));
204 struct libusb_ctrl_setup *setup;
205 unsigned char *urbdata;
206 int urbdata_length = sizeof(struct libusb_ctrl_setup) + transfer->length;
211 memset(urbh, 0, sizeof(*urbh));
213 urbh->callback = callback;
214 urbh->user_data = user_data;
215 r = calculate_timeout(urbh, timeout);
221 urbdata = malloc(urbdata_length);
227 fp_dbg("RQT=%02x RQ=%02x VAL=%04x IDX=%04x length=%d",
228 transfer->requesttype, transfer->request, transfer->value,
229 transfer->index, transfer->length);
231 setup = (struct libusb_ctrl_setup *) urbdata;
232 setup->bRequestType = transfer->requesttype;
233 setup->bRequest = transfer->request;
234 setup->wValue = cpu_to_le16(transfer->value);
235 setup->wIndex = cpu_to_le16(transfer->index);
236 setup->wLength = cpu_to_le16(transfer->length);
238 if ((transfer->requesttype & 0x80) == LIBUSB_ENDPOINT_OUT)
239 memcpy(urbdata + sizeof(struct libusb_ctrl_setup), transfer->data,
242 urbh->urb_type = USB_URB_TYPE_CONTROL;
243 urbh->buffer = urbdata;
244 urbh->transfer_len = urbdata_length;
246 r = submit_urb(devh, urbh);
256 static struct libusb_urb_handle *submit_bulk_transfer(
257 struct libusb_dev_handle *devh, struct libusb_bulk_transfer *transfer,
258 libusb_bulk_cb_fn callback, void *user_data, unsigned int timeout,
259 unsigned char urbtype)
261 struct libusb_urb_handle *urbh = malloc(sizeof(*urbh));
264 fp_dbg("length %d timeout %d", transfer->length, timeout);
268 memset(urbh, 0, sizeof(*urbh));
269 r = calculate_timeout(urbh, timeout);
275 urbh->callback = callback;
276 urbh->user_data = user_data;
277 urbh->flags |= LIBUSB_URBH_DATA_BELONGS_TO_USER;
278 urbh->endpoint = transfer->endpoint;
279 urbh->urb_type = urbtype;
280 urbh->buffer = transfer->data;
281 urbh->transfer_len = transfer->length;
283 r = submit_urb(devh, urbh);
292 API_EXPORTED struct libusb_urb_handle *libusb_async_bulk_transfer(
293 struct libusb_dev_handle *devh, struct libusb_bulk_transfer *transfer,
294 libusb_bulk_cb_fn callback, void *user_data, unsigned int timeout)
296 return submit_bulk_transfer(devh, transfer, callback, user_data, timeout,
300 API_EXPORTED struct libusb_urb_handle *libusb_async_interrupt_transfer(
301 struct libusb_dev_handle *devh, struct libusb_bulk_transfer *transfer,
302 libusb_bulk_cb_fn callback, void *user_data, unsigned int timeout)
304 return submit_bulk_transfer(devh, transfer, callback, user_data, timeout,
305 USB_URB_TYPE_INTERRUPT);
308 API_EXPORTED int libusb_urb_handle_cancel(struct libusb_dev_handle *devh,
309 struct libusb_urb_handle *urbh)
313 r = ioctl(devh->fd, IOCTL_USB_DISCARDURB, &urbh->urb);
315 fp_err("cancel urb failed error %d", r);
319 API_EXPORTED int libusb_urb_handle_cancel_sync(struct libusb_dev_handle *devh,
320 struct libusb_urb_handle *urbh)
324 r = ioctl(devh->fd, IOCTL_USB_DISCARDURB, &urbh->urb);
326 fp_err("cancel urb failed error %d", r);
330 urbh->flags |= LIBUSB_URBH_SYNC_CANCELLED;
331 while (urbh->flags & LIBUSB_URBH_SYNC_CANCELLED) {
340 int handle_transfer_completion(struct libusb_dev_handle *devh,
341 struct libusb_urb_handle *urbh, enum fp_urb_cb_status status)
343 struct usb_urb *urb = &urbh->urb;
345 if (TIMESPEC_IS_SET(&urbh->timeout))
346 timer_delete(urbh->timer);
348 if (status == FP_URB_SILENT_COMPLETION)
351 if (urb->type == USB_URB_TYPE_CONTROL) {
352 libusb_ctrl_cb_fn callback = urbh->callback;
354 callback(devh, urbh, status, urb->buffer,
355 urb->buffer + sizeof(struct libusb_ctrl_setup), urbh->transferred,
357 } else if (urb->type == USB_URB_TYPE_BULK ||
358 urb->type == USB_URB_TYPE_INTERRUPT) {
359 libusb_bulk_cb_fn callback = urbh->callback;
361 callback(devh, urbh, status, urbh->endpoint, urbh->transfer_len,
362 urbh->buffer, urbh->transferred, urbh->user_data);
367 static int handle_transfer_cancellation(struct libusb_dev_handle *devh,
368 struct libusb_urb_handle *urbh)
370 /* if the URB is being cancelled synchronously, raise cancellation
371 * completion event by unsetting flag, and ensure that user callback does
374 if (urbh->flags & LIBUSB_URBH_SYNC_CANCELLED) {
375 urbh->flags &= ~LIBUSB_URBH_SYNC_CANCELLED;
376 fp_dbg("detected sync. cancel");
377 return handle_transfer_completion(devh, urbh, FP_URB_SILENT_COMPLETION);
380 /* if the URB was cancelled due to timeout, report timeout to the user */
381 if (urbh->flags & LIBUSB_URBH_TIMED_OUT) {
382 fp_dbg("detected timeout cancellation");
383 return handle_transfer_completion(devh, urbh, FP_URB_TIMEOUT);
386 /* otherwise its a normal async cancel */
387 return handle_transfer_completion(devh, urbh, FP_URB_CANCELLED);
390 static int reap_for_devh(struct libusb_dev_handle *devh)
394 struct libusb_urb_handle *urbh;
397 r = ioctl(devh->fd, IOCTL_USB_REAPURBNDELAY, &urb);
398 if (r == -1 && errno == EAGAIN)
401 fp_err("reap failed error %d errno=%d", r, errno);
405 urbh = container_of(urb, struct libusb_urb_handle, urb);
407 fp_dbg("urb type=%d status=%d transferred=%d", urb->type, urb->status,
409 list_del(&urbh->list);
411 if (urb->status == -2)
412 return handle_transfer_cancellation(devh, urbh);
413 /* FIXME: research what other status codes may exist */
414 if (urb->status != 0)
415 fp_warn("unrecognised urb status %d", urb->status);
417 /* determine how much data was asked for */
418 trf_requested = MIN(urbh->transfer_len - urbh->transferred,
419 MAX_URB_BUFFER_LENGTH);
421 urbh->transferred += urb->actual_length;
423 /* if we were provided less data than requested, then our transfer is
425 if (urb->actual_length < trf_requested) {
426 fp_dbg("less data than requested (%d/%d) --> all done",
427 urb->actual_length, trf_requested);
428 return handle_transfer_completion(devh, urbh, FP_URB_COMPLETED);
431 /* if we've transferred all data, we're done */
432 if (urbh->transferred == urbh->transfer_len) {
433 fp_dbg("transfer complete --> all done");
434 return handle_transfer_completion(devh, urbh, FP_URB_COMPLETED);
437 /* otherwise, we have more data to transfer */
438 fp_dbg("more data to transfer...");
439 memset(urb, 0, sizeof(*urb));
440 return submit_urb(devh, urbh);
443 static void handle_timeout(struct libusb_urb_handle *urbh)
445 /* handling timeouts is tricky, as we may race with the kernel: we may
446 * detect a timeout racing with the condition that the urb has actually
447 * completed. we asynchronously cancel the URB and report timeout
448 * to the user when the URB cancellation completes (or not at all if the
449 * URB actually gets delivered as per this race) */
453 urbh->flags |= LIBUSB_URBH_TIMED_OUT;
454 r = libusb_urb_handle_cancel(urbh->devh, urbh);
456 fp_warn("async cancel failed %d errno=%d", r, errno);
459 static int handle_timeouts(void)
461 struct timespec systime;
462 struct libusb_urb_handle *urbh;
465 if (list_empty(&flying_urbs))
468 /* get current time */
469 r = clock_gettime(CLOCK_MONOTONIC, &systime);
473 /* iterate through flying urbs list, finding all urbs that have expired
475 list_for_each_entry(urbh, &flying_urbs, list) {
476 struct timespec *cur_ts = &urbh->timeout;
478 /* if we've reached urbs of infinite timeout, we're all done */
479 if (!TIMESPEC_IS_SET(cur_ts))
482 /* if urb has non-expired timeout, nothing more to do */
483 if ((cur_ts->tv_sec > systime.tv_sec) ||
484 (cur_ts->tv_sec == systime.tv_sec &&
485 cur_ts->tv_nsec > systime.tv_nsec))
488 /* otherwise, we've got an expired timeout to handle */
489 handle_timeout(urbh);
495 static int reap(void)
497 struct libusb_dev_handle *devh;
500 list_for_each_entry(devh, &open_devs, list) {
501 r = reap_for_devh(devh);
502 if (r == -1 && errno == EAGAIN)
508 r = handle_timeouts();
513 static int flush_sigfd(void)
516 struct signalfd_siginfo siginfo;
517 r = read(sigfd, &siginfo, sizeof(siginfo));
519 fp_err("sigfd read failed %d %d", r, errno);
522 if ((unsigned int) r < sizeof(siginfo)) {
523 fp_err("sigfd short read (%d/%d)", r, sizeof(siginfo));
529 static int poll_io(struct timeval *tv)
536 r = select(sigfd + 1, &fds, NULL, NULL, tv);
537 if (r == -1 && errno == EINTR)
540 fp_err("select failed %d err=%d\n", r, errno);
552 API_EXPORTED int libusb_poll_timeout(struct timeval *tv)
557 API_EXPORTED int libusb_poll(void)
565 struct sync_ctrl_handle {
566 enum fp_urb_cb_status status;
571 static void ctrl_transfer_cb(struct libusb_dev_handle *devh,
572 struct libusb_urb_handle *urbh, enum fp_urb_cb_status status,
573 struct libusb_ctrl_setup *setup, unsigned char *data, int actual_length,
576 struct sync_ctrl_handle *ctrlh = (struct sync_ctrl_handle *) user_data;
577 fp_dbg("actual_length=%d", actual_length);
579 if (status == FP_URB_COMPLETED) {
580 /* copy results into user-defined buffer */
581 if (setup->bRequestType & LIBUSB_ENDPOINT_IN)
582 memcpy(ctrlh->data, data, actual_length);
585 ctrlh->status = status;
586 ctrlh->actual_length = actual_length;
587 /* caller frees urbh */
590 API_EXPORTED int libusb_control_transfer(struct libusb_dev_handle *devh,
591 struct libusb_control_transfer *transfer, unsigned int timeout)
593 struct libusb_urb_handle *urbh;
594 struct sync_ctrl_handle ctrlh;
596 memset(&ctrlh, 0, sizeof(ctrlh));
597 ctrlh.data = transfer->data;
599 urbh = libusb_async_control_transfer(devh, transfer, ctrl_transfer_cb,
604 while (!ctrlh.status) {
605 int r = libusb_poll();
607 libusb_urb_handle_cancel_sync(devh, urbh);
608 libusb_urb_handle_free(urbh);
613 libusb_urb_handle_free(urbh);
614 switch (ctrlh.status) {
615 case FP_URB_COMPLETED:
616 return ctrlh.actual_length;
620 fp_warn("unrecognised status code %d", ctrlh.status);
625 struct sync_bulk_handle {
626 enum fp_urb_cb_status status;
630 static void bulk_transfer_cb(struct libusb_dev_handle *devh,
631 struct libusb_urb_handle *urbh, enum fp_urb_cb_status status,
632 unsigned char endpoint, int rqlength, unsigned char *data,
633 int actual_length, void *user_data)
635 struct sync_bulk_handle *bulkh = (struct sync_bulk_handle *) user_data;
637 bulkh->status = status;
638 bulkh->actual_length = actual_length;
639 /* caller frees urbh */
642 static int do_sync_bulk_transfer(struct libusb_dev_handle *devh,
643 struct libusb_bulk_transfer *transfer, int *transferred,
644 unsigned int timeout, unsigned char urbtype)
646 struct libusb_urb_handle *urbh;
647 struct sync_bulk_handle bulkh;
649 memset(&bulkh, 0, sizeof(bulkh));
651 urbh = submit_bulk_transfer(devh, transfer, bulk_transfer_cb, &bulkh,
656 while (!bulkh.status) {
657 int r = libusb_poll();
659 libusb_urb_handle_cancel_sync(devh, urbh);
660 libusb_urb_handle_free(urbh);
665 *transferred = bulkh.actual_length;
666 libusb_urb_handle_free(urbh);
668 switch (bulkh.status) {
669 case FP_URB_COMPLETED:
674 fp_warn("unrecognised status code %d", bulkh.status);
679 API_EXPORTED int libusb_interrupt_transfer(struct libusb_dev_handle *devh,
680 struct libusb_bulk_transfer *transfer, int *transferred,
681 unsigned int timeout)
683 return do_sync_bulk_transfer(devh, transfer, transferred, timeout,
684 USB_URB_TYPE_INTERRUPT);
687 API_EXPORTED int libusb_bulk_transfer(struct libusb_dev_handle *devh,
688 struct libusb_bulk_transfer *transfer, int *transferred,
689 unsigned int timeout)
691 return do_sync_bulk_transfer(devh, transfer, transferred, timeout,
695 API_EXPORTED void libusb_urb_handle_free(struct libusb_urb_handle *urbh)
700 if (!(urbh->flags & LIBUSB_URBH_DATA_BELONGS_TO_USER))
701 free(urbh->urb.buffer);
705 API_EXPORTED int libusb_get_pollfd(void)