1 /* -*- Mode: C; c-basic-offset:8 ; indent-tabs-mode:t -*- */
3 * Linux usbfs backend for libusb
4 * Copyright © 2007-2009 Daniel Drake <dsd@gentoo.org>
5 * Copyright © 2001 Johannes Erdfelt <johannes@erdfelt.com>
6 * Copyright © 2013 Nathan Hjelm <hjelmn@mac.com>
7 * Copyright © 2012-2013 Hans de Goede <hdegoede@redhat.com>
8 * Copyright © 2020 Chris Dickens <christopher.a.dickens@gmail.com>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "linux_usbfs.h"
35 #include <sys/ioctl.h>
37 #include <sys/utsname.h>
42 * opening a usbfs node causes the device to be resumed, so we attempt to
43 * avoid this during enumeration.
45 * sysfs allows us to read the kernel's in-memory copies of device descriptors
46 * and so forth, avoiding the need to open the device:
47 * - The binary "descriptors" file contains all config descriptors since
48 * 2.6.26, commit 217a9081d8e69026186067711131b77f0ce219ed
49 * - The binary "descriptors" file was added in 2.6.23, commit
50 * 69d42a78f935d19384d1f6e4f94b65bb162b36df, but it only contains the
51 * active config descriptors
52 * - The "busnum" file was added in 2.6.22, commit
53 * 83f7d958eab2fbc6b159ee92bf1493924e1d0f72
54 * - The "devnum" file has been present since pre-2.6.18
55 * - the "bConfigurationValue" file has been present since pre-2.6.18
57 * If we have bConfigurationValue, busnum, and devnum, then we can determine
58 * the active configuration without having to open the usbfs node in RDWR mode.
59 * The busnum file is important as that is the only way we can relate sysfs
60 * devices to usbfs nodes.
62 * If we also have all descriptors, we can obtain the device descriptor and
63 * configuration without touching usbfs at all.
66 /* endianness for multi-byte fields:
68 * Descriptors exposed by usbfs have the multi-byte fields in the device
69 * descriptor as host endian. Multi-byte fields in the other descriptors are
70 * bus-endian. The kernel documentation says otherwise, but it is wrong.
72 * In sysfs all descriptors are bus-endian.
75 #define USBDEV_PATH "/dev"
76 #define USB_DEVTMPFS_PATH "/dev/bus/usb"
78 /* use usbdev*.* device names in /dev instead of the usbfs bus directories */
79 static int usbdev_names = 0;
81 /* Linux has changed the maximum length of an individual isochronous packet
82 * over time. Initially this limit was 1,023 bytes, but Linux 2.6.18
83 * (commit 3612242e527eb47ee4756b5350f8bdf791aa5ede) increased this value to
84 * 8,192 bytes to support higher bandwidth devices. Linux 3.10
85 * (commit e2e2f0ea1c935edcf53feb4c4c8fdb4f86d57dd9) further increased this
86 * value to 49,152 bytes to support super speed devices. Linux 5.2
87 * (commit 8a1dbc8d91d3d1602282c7e6b4222c7759c916fa) even further increased
88 * this value to 98,304 bytes to support super speed plus devices.
90 static unsigned int max_iso_packet_len = 0;
92 /* is sysfs available (mounted) ? */
93 static int sysfs_available = -1;
95 /* how many times have we initted (and not exited) ? */
96 static int init_count = 0;
99 /* have no authority to operate usb device directly */
100 static int weak_authority = 0;
103 /* Serialize hotplug start/stop */
104 static usbi_mutex_static_t linux_hotplug_startstop_lock = USBI_MUTEX_INITIALIZER;
105 /* Serialize scan-devices, event-thread, and poll */
106 usbi_mutex_static_t linux_hotplug_lock = USBI_MUTEX_INITIALIZER;
108 static int linux_scan_devices(struct libusb_context *ctx);
109 static int detach_kernel_driver_and_claim(struct libusb_device_handle *, uint8_t);
111 #if !defined(HAVE_LIBUDEV)
112 static int linux_default_scan_devices(struct libusb_context *ctx);
115 struct kernel_version {
121 struct config_descriptor {
122 struct usbi_configuration_descriptor *desc;
126 struct linux_device_priv {
129 size_t descriptors_len;
130 struct config_descriptor *config_descriptors;
131 uint8_t active_config; /* cache val for !sysfs_available */
134 struct linux_device_handle_priv {
143 /* submission failed after the first URB, so await cancellation/completion
144 * of all the others */
147 /* cancelled by user or timeout */
150 /* completed multi-URB transfer in non-final URB */
153 /* one or more urbs encountered a low-level error */
157 struct linux_transfer_priv {
159 struct usbfs_urb *urbs;
160 struct usbfs_urb **iso_urbs;
163 enum reap_action reap_action;
166 enum libusb_transfer_status reap_status;
168 /* next iso packet in user-supplied transfer to be populated */
169 int iso_packet_offset;
172 static int get_usbfs_fd(struct libusb_device *dev, mode_t mode, int silent)
174 struct libusb_context *ctx = DEVICE_CTX(dev);
179 sprintf(path, USBDEV_PATH "/usbdev%u.%u",
180 dev->bus_number, dev->device_address);
182 sprintf(path, USB_DEVTMPFS_PATH "/%03u/%03u",
183 dev->bus_number, dev->device_address);
185 fd = open(path, mode | O_CLOEXEC);
187 return fd; /* Success */
189 if (errno == ENOENT) {
190 const long delay_ms = 10L;
191 const struct timespec delay_ts = { 0L, delay_ms * 1000L * 1000L };
194 usbi_err(ctx, "File doesn't exist, wait %ld ms and try again", delay_ms);
196 /* Wait 10ms for USB device path creation.*/
197 nanosleep(&delay_ts, NULL);
199 fd = open(path, mode | O_CLOEXEC);
201 return fd; /* Success */
205 usbi_err(ctx, "libusb couldn't open USB device %s, errno=%d", path, errno);
206 if (errno == EACCES && mode == O_RDWR)
207 usbi_err(ctx, "libusb requires write access to USB device nodes");
211 return LIBUSB_ERROR_ACCESS;
213 return LIBUSB_ERROR_NO_DEVICE;
214 return LIBUSB_ERROR_IO;
217 /* check dirent for a /dev/usbdev%d.%d name
218 * optionally return bus/device on success */
219 static int is_usbdev_entry(const char *name, uint8_t *bus_p, uint8_t *dev_p)
223 if (sscanf(name, "usbdev%d.%d", &busnum, &devnum) != 2)
225 if (busnum < 0 || busnum > UINT8_MAX || devnum < 0 || devnum > UINT8_MAX) {
226 usbi_dbg("invalid usbdev format '%s'", name);
230 usbi_dbg("found: %s", name);
232 *bus_p = (uint8_t)busnum;
234 *dev_p = (uint8_t)devnum;
238 static const char *find_usbfs_path(void)
242 struct dirent *entry;
244 path = USB_DEVTMPFS_PATH;
247 while ((entry = readdir(dir))) {
248 if (entry->d_name[0] == '.')
251 /* We assume if we find any files that it must be the right place */
261 /* look for /dev/usbdev*.* if the normal place fails */
265 while ((entry = readdir(dir))) {
266 if (entry->d_name[0] == '.')
269 if (is_usbdev_entry(entry->d_name, NULL, NULL)) {
270 /* found one; that's enough */
283 /* On udev based systems without any usb-devices /dev/bus/usb will not
284 * exist. So if we've not found anything and we're using udev for hotplug
285 * simply assume /dev/bus/usb rather then making libusb_init fail.
286 * Make the same assumption for Android where SELinux policies might block us
287 * from reading /dev on newer devices. */
288 #if defined(HAVE_LIBUDEV) || defined(__ANDROID__)
289 return USB_DEVTMPFS_PATH;
295 static int get_kernel_version(struct libusb_context *ctx,
296 struct kernel_version *ver)
301 if (uname(&uts) < 0) {
302 usbi_err(ctx, "uname failed, errno=%d", errno);
306 atoms = sscanf(uts.release, "%d.%d.%d", &ver->major, &ver->minor, &ver->sublevel);
308 usbi_err(ctx, "failed to parse uname release '%s'", uts.release);
315 usbi_dbg("reported kernel version is %s", uts.release);
320 static int kernel_version_ge(const struct kernel_version *ver,
321 int major, int minor, int sublevel)
323 if (ver->major > major)
325 else if (ver->major < major)
328 /* kmajor == major */
329 if (ver->minor > minor)
331 else if (ver->minor < minor)
334 /* kminor == minor */
335 if (ver->sublevel == -1)
336 return sublevel == 0;
338 return ver->sublevel >= sublevel;
341 static int op_init(struct libusb_context *ctx)
343 struct kernel_version kversion;
344 const char *usbfs_path;
347 if (get_kernel_version(ctx, &kversion) < 0)
348 return LIBUSB_ERROR_OTHER;
350 if (!kernel_version_ge(&kversion, 2, 6, 32)) {
351 usbi_err(ctx, "kernel version is too old (reported as %d.%d.%d)",
352 kversion.major, kversion.minor,
353 kversion.sublevel != -1 ? kversion.sublevel : 0);
354 return LIBUSB_ERROR_NOT_SUPPORTED;
357 usbfs_path = find_usbfs_path();
359 usbi_err(ctx, "could not find usbfs");
360 return LIBUSB_ERROR_OTHER;
363 usbi_dbg("found usbfs at %s", usbfs_path);
365 if (!max_iso_packet_len) {
366 if (kernel_version_ge(&kversion, 5, 2, 0))
367 max_iso_packet_len = 98304;
368 else if (kernel_version_ge(&kversion, 3, 10, 0))
369 max_iso_packet_len = 49152;
371 max_iso_packet_len = 8192;
374 usbi_dbg("max iso packet length is (likely) %u bytes", max_iso_packet_len);
376 if (sysfs_available == -1) {
377 struct statfs statfsbuf;
379 r = statfs(SYSFS_MOUNT_PATH, &statfsbuf);
380 if (r == 0 && statfsbuf.f_type == SYSFS_MAGIC) {
381 usbi_dbg("sysfs is available");
384 usbi_warn(ctx, "sysfs not mounted");
390 if (weak_authority) {
391 return LIBUSB_SUCCESS;
395 usbi_mutex_static_lock(&linux_hotplug_startstop_lock);
397 if (init_count == 0) {
398 /* start up hotplug event handler */
399 r = linux_start_event_monitor();
401 if (r == LIBUSB_SUCCESS) {
402 r = linux_scan_devices(ctx);
403 if (r == LIBUSB_SUCCESS)
405 else if (init_count == 0)
406 linux_stop_event_monitor();
408 usbi_err(ctx, "error starting hotplug event monitor");
410 usbi_mutex_static_unlock(&linux_hotplug_startstop_lock);
415 static void op_exit(struct libusb_context *ctx)
418 usbi_mutex_static_lock(&linux_hotplug_startstop_lock);
419 assert(init_count != 0);
421 /* tear down event handler */
422 linux_stop_event_monitor();
424 usbi_mutex_static_unlock(&linux_hotplug_startstop_lock);
427 static int op_set_option(struct libusb_context *ctx, enum libusb_option option, va_list ap)
434 case LIBUSB_OPTION_WEAK_AUTHORITY:
435 usbi_dbg("set libusb has weak authority");
437 return LIBUSB_SUCCESS;
440 return LIBUSB_ERROR_NOT_SUPPORTED;
444 static int linux_scan_devices(struct libusb_context *ctx)
448 usbi_mutex_static_lock(&linux_hotplug_lock);
450 #if defined(HAVE_LIBUDEV)
451 ret = linux_udev_scan_devices(ctx);
453 ret = linux_default_scan_devices(ctx);
456 usbi_mutex_static_unlock(&linux_hotplug_lock);
461 static void op_hotplug_poll(void)
463 linux_hotplug_poll();
466 static int open_sysfs_attr(struct libusb_context *ctx,
467 const char *sysfs_dir, const char *attr)
472 snprintf(filename, sizeof(filename), SYSFS_DEVICE_PATH "/%s/%s", sysfs_dir, attr);
473 fd = open(filename, O_RDONLY | O_CLOEXEC);
475 if (errno == ENOENT) {
476 /* File doesn't exist. Assume the device has been
477 disconnected (see trac ticket #70). */
478 return LIBUSB_ERROR_NO_DEVICE;
480 usbi_err(ctx, "open %s failed, errno=%d", filename, errno);
481 return LIBUSB_ERROR_IO;
487 /* Note only suitable for attributes which always read >= 0, < 0 is error */
488 static int read_sysfs_attr(struct libusb_context *ctx,
489 const char *sysfs_dir, const char *attr, int max_value, int *value_p)
491 char buf[20], *endptr;
496 fd = open_sysfs_attr(ctx, sysfs_dir, attr);
500 r = read(fd, buf, sizeof(buf));
505 return LIBUSB_ERROR_NO_DEVICE;
506 usbi_err(ctx, "attribute %s read failed, errno=%zd", attr, r);
507 return LIBUSB_ERROR_IO;
512 /* Certain attributes (e.g. bConfigurationValue) are not
513 * populated if the device is not configured. */
518 /* The kernel does *not* NULL-terminate the string, but every attribute
519 * should be terminated with a newline character. */
520 if (!isdigit(buf[0])) {
521 usbi_err(ctx, "attribute %s doesn't have numeric value?", attr);
522 return LIBUSB_ERROR_IO;
523 } else if (buf[r - 1] != '\n') {
524 usbi_err(ctx, "attribute %s doesn't end with newline?", attr);
525 return LIBUSB_ERROR_IO;
530 value = strtol(buf, &endptr, 10);
531 if (value < 0 || value > (long)max_value || errno) {
532 usbi_err(ctx, "attribute %s contains an invalid value: '%s'", attr, buf);
533 return LIBUSB_ERROR_INVALID_PARAM;
534 } else if (*endptr != '\0') {
535 /* Consider the value to be valid if the remainder is a '.'
536 * character followed by numbers. This occurs, for example,
537 * when reading the "speed" attribute for a low-speed device
539 if (*endptr == '.' && isdigit(*(endptr + 1))) {
541 while (isdigit(*endptr))
544 if (*endptr != '\0') {
545 usbi_err(ctx, "attribute %s contains an invalid value: '%s'", attr, buf);
546 return LIBUSB_ERROR_INVALID_PARAM;
550 *value_p = (int)value;
554 static int sysfs_scan_device(struct libusb_context *ctx, const char *devname)
556 uint8_t busnum, devaddr;
559 ret = linux_get_device_address(ctx, 0, &busnum, &devaddr, NULL, devname, -1);
560 if (ret != LIBUSB_SUCCESS)
563 return linux_enumerate_device(ctx, busnum, devaddr, devname);
566 /* read the bConfigurationValue for a device */
567 static int sysfs_get_active_config(struct libusb_device *dev, uint8_t *config)
569 struct linux_device_priv *priv = usbi_get_device_priv(dev);
572 ret = read_sysfs_attr(DEVICE_CTX(dev), priv->sysfs_dir, "bConfigurationValue",
578 tmp = 0; /* unconfigured */
580 *config = (uint8_t)tmp;
585 int linux_get_device_address(struct libusb_context *ctx, int detached,
586 uint8_t *busnum, uint8_t *devaddr, const char *dev_node,
587 const char *sys_name, int fd)
592 usbi_dbg("getting address for device: %s detached: %d", sys_name, detached);
593 /* can't use sysfs to read the bus and device number if the
594 * device has been detached */
595 if (!sysfs_available || detached || !sys_name) {
596 if (!dev_node && fd >= 0) {
597 char *fd_path = alloca(PATH_MAX);
600 /* try to retrieve the device node from fd */
601 sprintf(proc_path, "/proc/self/fd/%d", fd);
602 r = readlink(proc_path, fd_path, PATH_MAX - 1);
610 return LIBUSB_ERROR_OTHER;
612 /* will this work with all supported kernel versions? */
613 if (!strncmp(dev_node, "/dev/bus/usb", 12))
614 sscanf(dev_node, "/dev/bus/usb/%hhu/%hhu", busnum, devaddr);
616 return LIBUSB_ERROR_OTHER;
618 return LIBUSB_SUCCESS;
621 usbi_dbg("scan %s", sys_name);
623 r = read_sysfs_attr(ctx, sys_name, "busnum", UINT8_MAX, &sysfs_val);
626 *busnum = (uint8_t)sysfs_val;
628 r = read_sysfs_attr(ctx, sys_name, "devnum", UINT8_MAX, &sysfs_val);
631 *devaddr = (uint8_t)sysfs_val;
633 usbi_dbg("bus=%u dev=%u", *busnum, *devaddr);
635 return LIBUSB_SUCCESS;
638 /* Return offset of the next config descriptor */
639 static int seek_to_next_config(struct libusb_context *ctx,
640 uint8_t *buffer, size_t len)
642 struct usbi_descriptor_header *header;
647 usbi_err(ctx, "short descriptor read %zu/2", len);
648 return LIBUSB_ERROR_IO;
651 header = (struct usbi_descriptor_header *)buffer;
652 if (header->bDescriptorType == LIBUSB_DT_CONFIG)
655 if (len < header->bLength) {
656 usbi_err(ctx, "bLength overflow by %zu bytes",
657 (size_t)header->bLength - len);
658 return LIBUSB_ERROR_IO;
661 offset += header->bLength;
662 buffer += header->bLength;
663 len -= header->bLength;
666 usbi_err(ctx, "config descriptor not found");
667 return LIBUSB_ERROR_IO;
670 static int parse_config_descriptors(struct libusb_device *dev)
672 struct libusb_context *ctx = DEVICE_CTX(dev);
673 struct linux_device_priv *priv = usbi_get_device_priv(dev);
674 struct usbi_device_descriptor *device_desc;
675 uint8_t idx, num_configs;
679 device_desc = (struct usbi_device_descriptor *)priv->descriptors;
680 num_configs = device_desc->bNumConfigurations;
682 if (num_configs == 0)
683 return 0; /* no configurations? */
685 priv->config_descriptors = malloc(num_configs * sizeof(priv->config_descriptors[0]));
686 if (!priv->config_descriptors)
687 return LIBUSB_ERROR_NO_MEM;
689 buffer = priv->descriptors + LIBUSB_DT_DEVICE_SIZE;
690 remaining = priv->descriptors_len - LIBUSB_DT_DEVICE_SIZE;
692 for (idx = 0; idx < num_configs; idx++) {
693 struct usbi_configuration_descriptor *config_desc;
696 if (remaining < LIBUSB_DT_CONFIG_SIZE) {
697 usbi_err(ctx, "short descriptor read %zu/%d",
698 remaining, LIBUSB_DT_CONFIG_SIZE);
699 return LIBUSB_ERROR_IO;
702 config_desc = (struct usbi_configuration_descriptor *)buffer;
703 if (config_desc->bDescriptorType != LIBUSB_DT_CONFIG) {
704 usbi_err(ctx, "descriptor is not a config desc (type 0x%02x)",
705 config_desc->bDescriptorType);
706 return LIBUSB_ERROR_IO;
707 } else if (config_desc->bLength < LIBUSB_DT_CONFIG_SIZE) {
708 usbi_err(ctx, "invalid descriptor bLength %u",
709 config_desc->bLength);
710 return LIBUSB_ERROR_IO;
713 config_len = libusb_le16_to_cpu(config_desc->wTotalLength);
714 if (config_len < LIBUSB_DT_CONFIG_SIZE) {
715 usbi_err(ctx, "invalid wTotalLength %u", config_len);
716 return LIBUSB_ERROR_IO;
719 if (priv->sysfs_dir) {
721 * In sysfs wTotalLength is ignored, instead the kernel returns a
722 * config descriptor with verified bLength fields, with descriptors
723 * with an invalid bLength removed.
725 uint16_t sysfs_config_len;
728 if (num_configs > 1 && idx < num_configs - 1) {
729 offset = seek_to_next_config(ctx, buffer + LIBUSB_DT_CONFIG_SIZE,
730 remaining - LIBUSB_DT_CONFIG_SIZE);
733 sysfs_config_len = (uint16_t)offset;
735 sysfs_config_len = (uint16_t)remaining;
738 if (config_len != sysfs_config_len) {
739 usbi_warn(ctx, "config length mismatch wTotalLength %u real %u",
740 config_len, sysfs_config_len);
741 config_len = sysfs_config_len;
745 * In usbfs the config descriptors are wTotalLength bytes apart,
746 * with any short reads from the device appearing as holes in the file.
748 if (config_len > remaining) {
749 usbi_warn(ctx, "short descriptor read %zu/%u", remaining, config_len);
750 config_len = (uint16_t)remaining;
754 priv->config_descriptors[idx].desc = config_desc;
755 priv->config_descriptors[idx].actual_len = config_len;
757 buffer += config_len;
758 remaining -= config_len;
761 return LIBUSB_SUCCESS;
764 static int op_get_config_descriptor_by_value(struct libusb_device *dev,
765 uint8_t value, void **buffer)
767 struct linux_device_priv *priv = usbi_get_device_priv(dev);
768 struct config_descriptor *config;
771 for (idx = 0; idx < dev->device_descriptor.bNumConfigurations; idx++) {
772 config = &priv->config_descriptors[idx];
773 if (config->desc->bConfigurationValue == value) {
774 *buffer = config->desc;
775 return (int)config->actual_len;
779 return LIBUSB_ERROR_NOT_FOUND;
782 static int op_get_active_config_descriptor(struct libusb_device *dev,
783 void *buffer, size_t len)
785 struct linux_device_priv *priv = usbi_get_device_priv(dev);
787 uint8_t active_config;
790 if (priv->sysfs_dir) {
791 r = sysfs_get_active_config(dev, &active_config);
795 /* Use cached bConfigurationValue */
796 active_config = priv->active_config;
799 if (active_config == 0) {
800 usbi_err(DEVICE_CTX(dev), "device unconfigured");
801 return LIBUSB_ERROR_NOT_FOUND;
804 r = op_get_config_descriptor_by_value(dev, active_config, &config_desc);
808 len = MIN(len, (size_t)r);
809 memcpy(buffer, config_desc, len);
813 static int op_get_config_descriptor(struct libusb_device *dev,
814 uint8_t config_index, void *buffer, size_t len)
816 struct linux_device_priv *priv = usbi_get_device_priv(dev);
817 struct config_descriptor *config;
819 if (config_index >= dev->device_descriptor.bNumConfigurations)
820 return LIBUSB_ERROR_NOT_FOUND;
822 config = &priv->config_descriptors[config_index];
823 len = MIN(len, config->actual_len);
824 memcpy(buffer, config->desc, len);
828 /* send a control message to retrieve active configuration */
829 static int usbfs_get_active_config(struct libusb_device *dev, int fd)
831 struct linux_device_priv *priv = usbi_get_device_priv(dev);
832 uint8_t active_config = 0;
835 struct usbfs_ctrltransfer ctrl = {
836 .bmRequestType = LIBUSB_ENDPOINT_IN,
837 .bRequest = LIBUSB_REQUEST_GET_CONFIGURATION,
842 .data = &active_config
845 r = ioctl(fd, IOCTL_USBFS_CONTROL, &ctrl);
848 return LIBUSB_ERROR_NO_DEVICE;
850 /* we hit this error path frequently with buggy devices :( */
851 usbi_warn(DEVICE_CTX(dev), "get configuration failed, errno=%d", errno);
852 } else if (active_config == 0) {
853 /* some buggy devices have a configuration 0, but we're
854 * reaching into the corner of a corner case here, so let's
855 * not support buggy devices in these circumstances.
856 * stick to the specs: a configuration value of 0 means
858 usbi_warn(DEVICE_CTX(dev), "active cfg 0? assuming unconfigured device");
861 priv->active_config = active_config;
863 return LIBUSB_SUCCESS;
866 static int initialize_device(struct libusb_device *dev, uint8_t busnum,
867 uint8_t devaddr, const char *sysfs_dir, int wrapped_fd)
869 struct linux_device_priv *priv = usbi_get_device_priv(dev);
870 struct libusb_context *ctx = DEVICE_CTX(dev);
875 dev->bus_number = busnum;
876 dev->device_address = devaddr;
879 priv->sysfs_dir = strdup(sysfs_dir);
880 if (!priv->sysfs_dir)
881 return LIBUSB_ERROR_NO_MEM;
883 /* Note speed can contain 1.5, in this case read_sysfs_attr()
884 will stop parsing at the '.' and return 1 */
885 if (read_sysfs_attr(ctx, sysfs_dir, "speed", INT_MAX, &speed) == 0) {
887 case 1: dev->speed = LIBUSB_SPEED_LOW; break;
888 case 12: dev->speed = LIBUSB_SPEED_FULL; break;
889 case 480: dev->speed = LIBUSB_SPEED_HIGH; break;
890 case 5000: dev->speed = LIBUSB_SPEED_SUPER; break;
891 case 10000: dev->speed = LIBUSB_SPEED_SUPER_PLUS; break;
893 usbi_warn(ctx, "unknown device speed: %d Mbps", speed);
898 /* cache descriptors in memory */
900 fd = open_sysfs_attr(ctx, sysfs_dir, "descriptors");
901 } else if (wrapped_fd < 0) {
902 fd = get_usbfs_fd(dev, O_RDONLY, 0);
905 r = lseek(fd, 0, SEEK_SET);
907 usbi_err(ctx, "lseek failed, errno=%d", errno);
908 return LIBUSB_ERROR_IO;
917 priv->descriptors = usbi_reallocf(priv->descriptors, alloc_len);
918 if (!priv->descriptors) {
919 if (fd != wrapped_fd)
921 return LIBUSB_ERROR_NO_MEM;
923 /* usbfs has holes in the file */
925 memset(priv->descriptors + priv->descriptors_len,
926 0, alloc_len - priv->descriptors_len);
927 nb = read(fd, priv->descriptors + priv->descriptors_len,
928 alloc_len - priv->descriptors_len);
930 usbi_err(ctx, "read descriptor failed, errno=%d", errno);
931 if (fd != wrapped_fd)
933 return LIBUSB_ERROR_IO;
935 priv->descriptors_len += (size_t)nb;
936 } while (priv->descriptors_len == alloc_len);
938 if (fd != wrapped_fd)
941 if (priv->descriptors_len < LIBUSB_DT_DEVICE_SIZE) {
942 usbi_err(ctx, "short descriptor read (%zu)", priv->descriptors_len);
943 return LIBUSB_ERROR_IO;
946 r = parse_config_descriptors(dev);
950 memcpy(&dev->device_descriptor, priv->descriptors, LIBUSB_DT_DEVICE_SIZE);
953 /* sysfs descriptors are in bus-endian format */
954 usbi_localize_device_descriptor(&dev->device_descriptor);
955 return LIBUSB_SUCCESS;
958 /* cache active config */
960 fd = get_usbfs_fd(dev, O_RDWR, 1);
964 /* cannot send a control message to determine the active
965 * config. just assume the first one is active. */
966 usbi_warn(ctx, "Missing rw usbfs access; cannot determine "
967 "active configuration descriptor");
968 if (priv->config_descriptors)
969 priv->active_config = priv->config_descriptors[0].desc->bConfigurationValue;
971 priv->active_config = 0; /* No config dt */
973 return LIBUSB_SUCCESS;
976 r = usbfs_get_active_config(dev, fd);
977 if (fd != wrapped_fd)
983 static int linux_get_parent_info(struct libusb_device *dev, const char *sysfs_dir)
985 struct libusb_context *ctx = DEVICE_CTX(dev);
986 struct libusb_device *it;
987 char *parent_sysfs_dir, *tmp;
988 int ret, add_parent = 1;
990 /* XXX -- can we figure out the topology when using usbfs? */
991 if (!sysfs_dir || !strncmp(sysfs_dir, "usb", 3)) {
992 /* either using usbfs or finding the parent of a root hub */
993 return LIBUSB_SUCCESS;
996 parent_sysfs_dir = strdup(sysfs_dir);
997 if (!parent_sysfs_dir)
998 return LIBUSB_ERROR_NO_MEM;
1000 if ((tmp = strrchr(parent_sysfs_dir, '.')) ||
1001 (tmp = strrchr(parent_sysfs_dir, '-'))) {
1002 dev->port_number = atoi(tmp + 1);
1005 usbi_warn(ctx, "Can not parse sysfs_dir: %s, no parent info",
1007 free(parent_sysfs_dir);
1008 return LIBUSB_SUCCESS;
1011 /* is the parent a root hub? */
1012 if (!strchr(parent_sysfs_dir, '-')) {
1013 tmp = parent_sysfs_dir;
1014 ret = asprintf(&parent_sysfs_dir, "usb%s", tmp);
1017 return LIBUSB_ERROR_NO_MEM;
1021 /* find the parent in the context */
1022 usbi_mutex_lock(&ctx->usb_devs_lock);
1023 for_each_device(ctx, it) {
1024 struct linux_device_priv *priv = usbi_get_device_priv(it);
1026 if (priv->sysfs_dir) {
1027 if (!strcmp(priv->sysfs_dir, parent_sysfs_dir)) {
1028 dev->parent_dev = libusb_ref_device(it);
1033 usbi_mutex_unlock(&ctx->usb_devs_lock);
1035 if (!dev->parent_dev && add_parent) {
1036 usbi_dbg("parent_dev %s not enumerated yet, enumerating now",
1038 sysfs_scan_device(ctx, parent_sysfs_dir);
1043 usbi_dbg("dev %p (%s) has parent %p (%s) port %u", dev, sysfs_dir,
1044 dev->parent_dev, parent_sysfs_dir, dev->port_number);
1046 free(parent_sysfs_dir);
1048 return LIBUSB_SUCCESS;
1051 int linux_enumerate_device(struct libusb_context *ctx,
1052 uint8_t busnum, uint8_t devaddr, const char *sysfs_dir)
1054 unsigned long session_id;
1055 struct libusb_device *dev;
1058 /* FIXME: session ID is not guaranteed unique as addresses can wrap and
1059 * will be reused. instead we should add a simple sysfs attribute with
1061 session_id = busnum << 8 | devaddr;
1062 usbi_dbg("busnum %u devaddr %u session_id %lu", busnum, devaddr, session_id);
1064 dev = usbi_get_device_by_session_id(ctx, session_id);
1066 /* device already exists in the context */
1067 usbi_dbg("session_id %lu already exists", session_id);
1068 libusb_unref_device(dev);
1069 return LIBUSB_SUCCESS;
1072 usbi_dbg("allocating new device for %u/%u (session %lu)",
1073 busnum, devaddr, session_id);
1074 dev = usbi_alloc_device(ctx, session_id);
1076 return LIBUSB_ERROR_NO_MEM;
1078 r = initialize_device(dev, busnum, devaddr, sysfs_dir, -1);
1081 r = usbi_sanitize_device(dev);
1085 r = linux_get_parent_info(dev, sysfs_dir);
1090 libusb_unref_device(dev);
1092 usbi_connect_device(dev);
1097 void linux_hotplug_enumerate(uint8_t busnum, uint8_t devaddr, const char *sys_name)
1099 struct libusb_context *ctx;
1101 usbi_mutex_static_lock(&active_contexts_lock);
1102 for_each_context(ctx) {
1103 linux_enumerate_device(ctx, busnum, devaddr, sys_name);
1105 usbi_mutex_static_unlock(&active_contexts_lock);
1108 void linux_device_disconnected(uint8_t busnum, uint8_t devaddr)
1110 struct libusb_context *ctx;
1111 struct libusb_device *dev;
1112 unsigned long session_id = busnum << 8 | devaddr;
1114 usbi_mutex_static_lock(&active_contexts_lock);
1115 for_each_context(ctx) {
1116 dev = usbi_get_device_by_session_id(ctx, session_id);
1118 usbi_disconnect_device(dev);
1119 libusb_unref_device(dev);
1121 usbi_dbg("device not found for session %lx", session_id);
1124 usbi_mutex_static_unlock(&active_contexts_lock);
1127 #if !defined(HAVE_LIBUDEV)
1128 static int parse_u8(const char *str, uint8_t *val_p)
1134 num = strtol(str, &endptr, 10);
1135 if (num < 0 || num > UINT8_MAX || errno)
1137 if (endptr == str || *endptr != '\0')
1140 *val_p = (uint8_t)num;
1144 /* open a bus directory and adds all discovered devices to the context */
1145 static int usbfs_scan_busdir(struct libusb_context *ctx, uint8_t busnum)
1149 struct dirent *entry;
1150 int r = LIBUSB_ERROR_IO;
1152 sprintf(dirpath, USB_DEVTMPFS_PATH "/%03u", busnum);
1153 usbi_dbg("%s", dirpath);
1154 dir = opendir(dirpath);
1156 usbi_err(ctx, "opendir '%s' failed, errno=%d", dirpath, errno);
1157 /* FIXME: should handle valid race conditions like hub unplugged
1158 * during directory iteration - this is not an error */
1162 while ((entry = readdir(dir))) {
1165 if (entry->d_name[0] == '.')
1168 if (!parse_u8(entry->d_name, &devaddr)) {
1169 usbi_dbg("unknown dir entry %s", entry->d_name);
1173 if (linux_enumerate_device(ctx, busnum, devaddr, NULL)) {
1174 usbi_dbg("failed to enumerate dir entry %s", entry->d_name);
1185 static int usbfs_get_device_list(struct libusb_context *ctx)
1187 struct dirent *entry;
1189 uint8_t busnum, devaddr;
1193 buses = opendir(USBDEV_PATH);
1195 buses = opendir(USB_DEVTMPFS_PATH);
1198 usbi_err(ctx, "opendir buses failed, errno=%d", errno);
1199 return LIBUSB_ERROR_IO;
1202 while ((entry = readdir(buses))) {
1203 if (entry->d_name[0] == '.')
1207 if (!is_usbdev_entry(entry->d_name, &busnum, &devaddr))
1210 r = linux_enumerate_device(ctx, busnum, devaddr, NULL);
1212 usbi_dbg("failed to enumerate dir entry %s", entry->d_name);
1216 if (!parse_u8(entry->d_name, &busnum)) {
1217 usbi_dbg("unknown dir entry %s", entry->d_name);
1221 r = usbfs_scan_busdir(ctx, busnum);
1232 static int sysfs_get_device_list(struct libusb_context *ctx)
1234 DIR *devices = opendir(SYSFS_DEVICE_PATH);
1235 struct dirent *entry;
1236 int num_devices = 0;
1237 int num_enumerated = 0;
1240 usbi_err(ctx, "opendir devices failed, errno=%d", errno);
1241 return LIBUSB_ERROR_IO;
1244 while ((entry = readdir(devices))) {
1245 if ((!isdigit(entry->d_name[0]) && strncmp(entry->d_name, "usb", 3))
1246 || strchr(entry->d_name, ':'))
1251 if (sysfs_scan_device(ctx, entry->d_name)) {
1252 usbi_dbg("failed to enumerate dir entry %s", entry->d_name);
1261 /* successful if at least one device was enumerated or no devices were found */
1262 if (num_enumerated || !num_devices)
1263 return LIBUSB_SUCCESS;
1265 return LIBUSB_ERROR_IO;
1268 static int linux_default_scan_devices(struct libusb_context *ctx)
1270 /* we can retrieve device list and descriptors from sysfs or usbfs.
1271 * sysfs is preferable, because if we use usbfs we end up resuming
1272 * any autosuspended USB devices. however, sysfs is not available
1273 * everywhere, so we need a usbfs fallback too.
1275 if (sysfs_available)
1276 return sysfs_get_device_list(ctx);
1278 return usbfs_get_device_list(ctx);
1282 static int initialize_handle(struct libusb_device_handle *handle, int fd)
1284 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1289 r = ioctl(fd, IOCTL_USBFS_GET_CAPABILITIES, &hpriv->caps);
1291 if (errno == ENOTTY)
1292 usbi_dbg("getcap not available");
1294 usbi_err(HANDLE_CTX(handle), "getcap failed, errno=%d", errno);
1295 hpriv->caps = USBFS_CAP_BULK_CONTINUATION;
1298 return usbi_add_event_source(HANDLE_CTX(handle), hpriv->fd, POLLOUT);
1301 static int op_wrap_sys_device(struct libusb_context *ctx,
1302 struct libusb_device_handle *handle, intptr_t sys_dev)
1304 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1305 int fd = (int)sys_dev;
1306 uint8_t busnum, devaddr;
1307 struct usbfs_connectinfo ci;
1308 struct libusb_device *dev;
1311 r = linux_get_device_address(ctx, 1, &busnum, &devaddr, NULL, NULL, fd);
1313 r = ioctl(fd, IOCTL_USBFS_CONNECTINFO, &ci);
1315 usbi_err(ctx, "connectinfo failed, errno=%d", errno);
1316 return LIBUSB_ERROR_IO;
1318 /* There is no ioctl to get the bus number. We choose 0 here
1319 * as linux starts numbering buses from 1. */
1321 devaddr = ci.devnum;
1324 /* Session id is unused as we do not add the device to the list of
1325 * connected devices. */
1326 usbi_dbg("allocating new device for fd %d", fd);
1327 dev = usbi_alloc_device(ctx, 0);
1329 return LIBUSB_ERROR_NO_MEM;
1331 r = initialize_device(dev, busnum, devaddr, NULL, fd);
1334 r = usbi_sanitize_device(dev);
1337 /* Consider the device as connected, but do not add it to the managed
1342 r = initialize_handle(handle, fd);
1347 libusb_unref_device(dev);
1351 static int op_open(struct libusb_device_handle *handle)
1355 fd = get_usbfs_fd(handle->dev, O_RDWR, 0);
1357 if (fd == LIBUSB_ERROR_NO_DEVICE) {
1358 /* device will still be marked as attached if hotplug monitor thread
1359 * hasn't processed remove event yet */
1360 usbi_mutex_static_lock(&linux_hotplug_lock);
1361 if (handle->dev->attached) {
1362 usbi_dbg("open failed with no device, but device still attached");
1363 linux_device_disconnected(handle->dev->bus_number,
1364 handle->dev->device_address);
1366 usbi_mutex_static_unlock(&linux_hotplug_lock);
1371 r = initialize_handle(handle, fd);
1378 static void op_close(struct libusb_device_handle *dev_handle)
1380 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(dev_handle);
1382 /* fd may have already been removed by POLLERR condition in op_handle_events() */
1383 if (!hpriv->fd_removed)
1384 usbi_remove_event_source(HANDLE_CTX(dev_handle), hpriv->fd);
1385 if (!hpriv->fd_keep)
1389 static int op_get_configuration(struct libusb_device_handle *handle,
1392 struct linux_device_priv *priv = usbi_get_device_priv(handle->dev);
1395 if (priv->sysfs_dir) {
1396 r = sysfs_get_active_config(handle->dev, config);
1398 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1400 r = usbfs_get_active_config(handle->dev, hpriv->fd);
1401 if (r == LIBUSB_SUCCESS)
1402 *config = priv->active_config;
1408 usbi_err(HANDLE_CTX(handle), "device unconfigured");
1413 static int op_set_configuration(struct libusb_device_handle *handle, int config)
1415 struct linux_device_priv *priv = usbi_get_device_priv(handle->dev);
1416 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1418 int r = ioctl(fd, IOCTL_USBFS_SETCONFIGURATION, &config);
1421 if (errno == EINVAL)
1422 return LIBUSB_ERROR_NOT_FOUND;
1423 else if (errno == EBUSY)
1424 return LIBUSB_ERROR_BUSY;
1425 else if (errno == ENODEV)
1426 return LIBUSB_ERROR_NO_DEVICE;
1428 usbi_err(HANDLE_CTX(handle), "set configuration failed, errno=%d", errno);
1429 return LIBUSB_ERROR_OTHER;
1435 /* update our cached active config descriptor */
1436 priv->active_config = (uint8_t)config;
1438 return LIBUSB_SUCCESS;
1441 static int claim_interface(struct libusb_device_handle *handle, unsigned int iface)
1443 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1445 int r = ioctl(fd, IOCTL_USBFS_CLAIMINTERFACE, &iface);
1448 if (errno == ENOENT)
1449 return LIBUSB_ERROR_NOT_FOUND;
1450 else if (errno == EBUSY)
1451 return LIBUSB_ERROR_BUSY;
1452 else if (errno == ENODEV)
1453 return LIBUSB_ERROR_NO_DEVICE;
1455 usbi_err(HANDLE_CTX(handle), "claim interface failed, errno=%d", errno);
1456 return LIBUSB_ERROR_OTHER;
1461 static int release_interface(struct libusb_device_handle *handle, unsigned int iface)
1463 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1465 int r = ioctl(fd, IOCTL_USBFS_RELEASEINTERFACE, &iface);
1468 if (errno == ENODEV)
1469 return LIBUSB_ERROR_NO_DEVICE;
1471 usbi_err(HANDLE_CTX(handle), "release interface failed, errno=%d", errno);
1472 return LIBUSB_ERROR_OTHER;
1477 static int op_set_interface(struct libusb_device_handle *handle, uint8_t interface,
1480 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1482 struct usbfs_setinterface setintf;
1485 setintf.interface = interface;
1486 setintf.altsetting = altsetting;
1487 r = ioctl(fd, IOCTL_USBFS_SETINTERFACE, &setintf);
1489 if (errno == EINVAL)
1490 return LIBUSB_ERROR_NOT_FOUND;
1491 else if (errno == ENODEV)
1492 return LIBUSB_ERROR_NO_DEVICE;
1494 usbi_err(HANDLE_CTX(handle), "set interface failed, errno=%d", errno);
1495 return LIBUSB_ERROR_OTHER;
1501 static int op_clear_halt(struct libusb_device_handle *handle,
1502 unsigned char endpoint)
1504 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1506 unsigned int _endpoint = endpoint;
1507 int r = ioctl(fd, IOCTL_USBFS_CLEAR_HALT, &_endpoint);
1510 if (errno == ENOENT)
1511 return LIBUSB_ERROR_NOT_FOUND;
1512 else if (errno == ENODEV)
1513 return LIBUSB_ERROR_NO_DEVICE;
1515 usbi_err(HANDLE_CTX(handle), "clear halt failed, errno=%d", errno);
1516 return LIBUSB_ERROR_OTHER;
1522 static int op_reset_device(struct libusb_device_handle *handle)
1524 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1529 /* Doing a device reset will cause the usbfs driver to get unbound
1530 * from any interfaces it is bound to. By voluntarily unbinding
1531 * the usbfs driver ourself, we stop the kernel from rebinding
1532 * the interface after reset (which would end up with the interface
1533 * getting bound to the in kernel driver if any). */
1534 for (i = 0; i < USB_MAXINTERFACES; i++) {
1535 if (handle->claimed_interfaces & (1UL << i))
1536 release_interface(handle, i);
1539 usbi_mutex_lock(&handle->lock);
1540 r = ioctl(fd, IOCTL_USBFS_RESET, NULL);
1542 if (errno == ENODEV) {
1543 ret = LIBUSB_ERROR_NOT_FOUND;
1547 usbi_err(HANDLE_CTX(handle), "reset failed, errno=%d", errno);
1548 ret = LIBUSB_ERROR_OTHER;
1552 /* And re-claim any interfaces which were claimed before the reset */
1553 for (i = 0; i < USB_MAXINTERFACES; i++) {
1554 if (!(handle->claimed_interfaces & (1UL << i)))
1557 * A driver may have completed modprobing during
1558 * IOCTL_USBFS_RESET, and bound itself as soon as
1559 * IOCTL_USBFS_RESET released the device lock
1561 r = detach_kernel_driver_and_claim(handle, i);
1563 usbi_warn(HANDLE_CTX(handle), "failed to re-claim interface %u after reset: %s",
1564 i, libusb_error_name(r));
1565 handle->claimed_interfaces &= ~(1UL << i);
1566 ret = LIBUSB_ERROR_NOT_FOUND;
1570 usbi_mutex_unlock(&handle->lock);
1574 static int do_streams_ioctl(struct libusb_device_handle *handle, long req,
1575 uint32_t num_streams, unsigned char *endpoints, int num_endpoints)
1577 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1578 int r, fd = hpriv->fd;
1579 struct usbfs_streams *streams;
1581 if (num_endpoints > 30) /* Max 15 in + 15 out eps */
1582 return LIBUSB_ERROR_INVALID_PARAM;
1584 streams = malloc(sizeof(*streams) + num_endpoints);
1586 return LIBUSB_ERROR_NO_MEM;
1588 streams->num_streams = num_streams;
1589 streams->num_eps = num_endpoints;
1590 memcpy(streams->eps, endpoints, num_endpoints);
1592 r = ioctl(fd, req, streams);
1597 if (errno == ENOTTY)
1598 return LIBUSB_ERROR_NOT_SUPPORTED;
1599 else if (errno == EINVAL)
1600 return LIBUSB_ERROR_INVALID_PARAM;
1601 else if (errno == ENODEV)
1602 return LIBUSB_ERROR_NO_DEVICE;
1604 usbi_err(HANDLE_CTX(handle), "streams-ioctl failed, errno=%d", errno);
1605 return LIBUSB_ERROR_OTHER;
1610 static int op_alloc_streams(struct libusb_device_handle *handle,
1611 uint32_t num_streams, unsigned char *endpoints, int num_endpoints)
1613 return do_streams_ioctl(handle, IOCTL_USBFS_ALLOC_STREAMS,
1614 num_streams, endpoints, num_endpoints);
1617 static int op_free_streams(struct libusb_device_handle *handle,
1618 unsigned char *endpoints, int num_endpoints)
1620 return do_streams_ioctl(handle, IOCTL_USBFS_FREE_STREAMS, 0,
1621 endpoints, num_endpoints);
1624 static void *op_dev_mem_alloc(struct libusb_device_handle *handle, size_t len)
1626 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1629 buffer = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, hpriv->fd, 0);
1630 if (buffer == MAP_FAILED) {
1631 usbi_err(HANDLE_CTX(handle), "alloc dev mem failed, errno=%d", errno);
1637 static int op_dev_mem_free(struct libusb_device_handle *handle, void *buffer,
1640 if (munmap(buffer, len) != 0) {
1641 usbi_err(HANDLE_CTX(handle), "free dev mem failed, errno=%d", errno);
1642 return LIBUSB_ERROR_OTHER;
1644 return LIBUSB_SUCCESS;
1648 static int op_kernel_driver_active(struct libusb_device_handle *handle,
1651 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1653 struct usbfs_getdriver getdrv;
1656 getdrv.interface = interface;
1657 r = ioctl(fd, IOCTL_USBFS_GETDRIVER, &getdrv);
1659 if (errno == ENODATA)
1661 else if (errno == ENODEV)
1662 return LIBUSB_ERROR_NO_DEVICE;
1664 usbi_err(HANDLE_CTX(handle), "get driver failed, errno=%d", errno);
1665 return LIBUSB_ERROR_OTHER;
1668 return strcmp(getdrv.driver, "usbfs") != 0;
1671 static int op_detach_kernel_driver(struct libusb_device_handle *handle,
1674 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1676 struct usbfs_ioctl command;
1677 struct usbfs_getdriver getdrv;
1680 command.ifno = interface;
1681 command.ioctl_code = IOCTL_USBFS_DISCONNECT;
1682 command.data = NULL;
1684 getdrv.interface = interface;
1685 r = ioctl(fd, IOCTL_USBFS_GETDRIVER, &getdrv);
1686 if (r == 0 && !strcmp(getdrv.driver, "usbfs"))
1687 return LIBUSB_ERROR_NOT_FOUND;
1689 r = ioctl(fd, IOCTL_USBFS_IOCTL, &command);
1691 if (errno == ENODATA)
1692 return LIBUSB_ERROR_NOT_FOUND;
1693 else if (errno == EINVAL)
1694 return LIBUSB_ERROR_INVALID_PARAM;
1695 else if (errno == ENODEV)
1696 return LIBUSB_ERROR_NO_DEVICE;
1698 usbi_err(HANDLE_CTX(handle), "detach failed, errno=%d", errno);
1699 return LIBUSB_ERROR_OTHER;
1705 static int op_attach_kernel_driver(struct libusb_device_handle *handle,
1708 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1710 struct usbfs_ioctl command;
1713 command.ifno = interface;
1714 command.ioctl_code = IOCTL_USBFS_CONNECT;
1715 command.data = NULL;
1717 r = ioctl(fd, IOCTL_USBFS_IOCTL, &command);
1719 if (errno == ENODATA)
1720 return LIBUSB_ERROR_NOT_FOUND;
1721 else if (errno == EINVAL)
1722 return LIBUSB_ERROR_INVALID_PARAM;
1723 else if (errno == ENODEV)
1724 return LIBUSB_ERROR_NO_DEVICE;
1725 else if (errno == EBUSY)
1726 return LIBUSB_ERROR_BUSY;
1728 usbi_err(HANDLE_CTX(handle), "attach failed, errno=%d", errno);
1729 return LIBUSB_ERROR_OTHER;
1730 } else if (r == 0) {
1731 return LIBUSB_ERROR_NOT_FOUND;
1737 static int detach_kernel_driver_and_claim(struct libusb_device_handle *handle,
1740 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1741 struct usbfs_disconnect_claim dc;
1742 int r, fd = hpriv->fd;
1744 dc.interface = interface;
1745 strcpy(dc.driver, "usbfs");
1746 dc.flags = USBFS_DISCONNECT_CLAIM_EXCEPT_DRIVER;
1747 r = ioctl(fd, IOCTL_USBFS_DISCONNECT_CLAIM, &dc);
1754 return LIBUSB_ERROR_BUSY;
1756 return LIBUSB_ERROR_INVALID_PARAM;
1758 return LIBUSB_ERROR_NO_DEVICE;
1760 usbi_err(HANDLE_CTX(handle), "disconnect-and-claim failed, errno=%d", errno);
1761 return LIBUSB_ERROR_OTHER;
1764 /* Fallback code for kernels which don't support the
1765 disconnect-and-claim ioctl */
1766 r = op_detach_kernel_driver(handle, interface);
1767 if (r != 0 && r != LIBUSB_ERROR_NOT_FOUND)
1770 return claim_interface(handle, interface);
1773 static int op_claim_interface(struct libusb_device_handle *handle, uint8_t interface)
1775 if (handle->auto_detach_kernel_driver)
1776 return detach_kernel_driver_and_claim(handle, interface);
1778 return claim_interface(handle, interface);
1781 static int op_release_interface(struct libusb_device_handle *handle, uint8_t interface)
1785 r = release_interface(handle, interface);
1789 if (handle->auto_detach_kernel_driver)
1790 op_attach_kernel_driver(handle, interface);
1795 static void op_destroy_device(struct libusb_device *dev)
1797 struct linux_device_priv *priv = usbi_get_device_priv(dev);
1799 free(priv->config_descriptors);
1800 free(priv->descriptors);
1801 free(priv->sysfs_dir);
1804 /* URBs are discarded in reverse order of submission to avoid races. */
1805 static int discard_urbs(struct usbi_transfer *itransfer, int first, int last_plus_one)
1807 struct libusb_transfer *transfer =
1808 USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
1809 struct linux_transfer_priv *tpriv = usbi_get_transfer_priv(itransfer);
1810 struct linux_device_handle_priv *hpriv =
1811 usbi_get_device_handle_priv(transfer->dev_handle);
1813 struct usbfs_urb *urb;
1815 for (i = last_plus_one - 1; i >= first; i--) {
1816 if (transfer->type == LIBUSB_TRANSFER_TYPE_ISOCHRONOUS)
1817 urb = tpriv->iso_urbs[i];
1819 urb = &tpriv->urbs[i];
1821 if (ioctl(hpriv->fd, IOCTL_USBFS_DISCARDURB, urb) == 0)
1824 if (errno == EINVAL) {
1825 usbi_dbg("URB not found --> assuming ready to be reaped");
1826 if (i == (last_plus_one - 1))
1827 ret = LIBUSB_ERROR_NOT_FOUND;
1828 } else if (errno == ENODEV) {
1829 usbi_dbg("Device not found for URB --> assuming ready to be reaped");
1830 ret = LIBUSB_ERROR_NO_DEVICE;
1832 usbi_warn(TRANSFER_CTX(transfer), "unrecognised discard errno %d", errno);
1833 ret = LIBUSB_ERROR_OTHER;
1839 static void free_iso_urbs(struct linux_transfer_priv *tpriv)
1843 for (i = 0; i < tpriv->num_urbs; i++) {
1844 struct usbfs_urb *urb = tpriv->iso_urbs[i];
1851 free(tpriv->iso_urbs);
1852 tpriv->iso_urbs = NULL;
1855 static int submit_bulk_transfer(struct usbi_transfer *itransfer)
1857 struct libusb_transfer *transfer =
1858 USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
1859 struct linux_transfer_priv *tpriv = usbi_get_transfer_priv(itransfer);
1860 struct linux_device_handle_priv *hpriv =
1861 usbi_get_device_handle_priv(transfer->dev_handle);
1862 struct usbfs_urb *urbs;
1863 int is_out = IS_XFEROUT(transfer);
1864 int bulk_buffer_len, use_bulk_continuation;
1866 int last_urb_partial = 0;
1871 * Older versions of usbfs place a 16kb limit on bulk URBs. We work
1872 * around this by splitting large transfers into 16k blocks, and then
1873 * submit all urbs at once. it would be simpler to submit one urb at
1874 * a time, but there is a big performance gain doing it this way.
1876 * Newer versions lift the 16k limit (USBFS_CAP_NO_PACKET_SIZE_LIM),
1877 * using arbitrary large transfers can still be a bad idea though, as
1878 * the kernel needs to allocate physical contiguous memory for this,
1879 * which may fail for large buffers.
1881 * The kernel solves this problem by splitting the transfer into
1882 * blocks itself when the host-controller is scatter-gather capable
1883 * (USBFS_CAP_BULK_SCATTER_GATHER), which most controllers are.
1885 * Last, there is the issue of short-transfers when splitting, for
1886 * short split-transfers to work reliable USBFS_CAP_BULK_CONTINUATION
1887 * is needed, but this is not always available.
1889 if (hpriv->caps & USBFS_CAP_BULK_SCATTER_GATHER) {
1890 /* Good! Just submit everything in one go */
1891 bulk_buffer_len = transfer->length ? transfer->length : 1;
1892 use_bulk_continuation = 0;
1893 } else if (hpriv->caps & USBFS_CAP_BULK_CONTINUATION) {
1894 /* Split the transfers and use bulk-continuation to
1895 avoid issues with short-transfers */
1896 bulk_buffer_len = MAX_BULK_BUFFER_LENGTH;
1897 use_bulk_continuation = 1;
1898 } else if (hpriv->caps & USBFS_CAP_NO_PACKET_SIZE_LIM) {
1899 /* Don't split, assume the kernel can alloc the buffer
1900 (otherwise the submit will fail with -ENOMEM) */
1901 bulk_buffer_len = transfer->length ? transfer->length : 1;
1902 use_bulk_continuation = 0;
1904 /* Bad, splitting without bulk-continuation, short transfers
1905 which end before the last urb will not work reliable! */
1906 /* Note we don't warn here as this is "normal" on kernels <
1907 2.6.32 and not a problem for most applications */
1908 bulk_buffer_len = MAX_BULK_BUFFER_LENGTH;
1909 use_bulk_continuation = 0;
1912 num_urbs = transfer->length / bulk_buffer_len;
1914 if (transfer->length == 0) {
1916 } else if ((transfer->length % bulk_buffer_len) > 0) {
1917 last_urb_partial = 1;
1920 usbi_dbg("need %d urbs for new transfer with length %d", num_urbs, transfer->length);
1921 urbs = calloc(num_urbs, sizeof(*urbs));
1923 return LIBUSB_ERROR_NO_MEM;
1925 tpriv->num_urbs = num_urbs;
1926 tpriv->num_retired = 0;
1927 tpriv->reap_action = NORMAL;
1928 tpriv->reap_status = LIBUSB_TRANSFER_COMPLETED;
1930 for (i = 0; i < num_urbs; i++) {
1931 struct usbfs_urb *urb = &urbs[i];
1933 urb->usercontext = itransfer;
1934 switch (transfer->type) {
1935 case LIBUSB_TRANSFER_TYPE_BULK:
1936 urb->type = USBFS_URB_TYPE_BULK;
1939 case LIBUSB_TRANSFER_TYPE_BULK_STREAM:
1940 urb->type = USBFS_URB_TYPE_BULK;
1941 urb->stream_id = itransfer->stream_id;
1943 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1944 urb->type = USBFS_URB_TYPE_INTERRUPT;
1947 urb->endpoint = transfer->endpoint;
1948 urb->buffer = transfer->buffer + (i * bulk_buffer_len);
1950 /* don't set the short not ok flag for the last URB */
1951 if (use_bulk_continuation && !is_out && (i < num_urbs - 1))
1952 urb->flags = USBFS_URB_SHORT_NOT_OK;
1954 if (i == num_urbs - 1 && last_urb_partial)
1955 urb->buffer_length = transfer->length % bulk_buffer_len;
1956 else if (transfer->length == 0)
1957 urb->buffer_length = 0;
1959 urb->buffer_length = bulk_buffer_len;
1961 if (i > 0 && use_bulk_continuation)
1962 urb->flags |= USBFS_URB_BULK_CONTINUATION;
1964 /* we have already checked that the flag is supported */
1965 if (is_out && i == num_urbs - 1 &&
1966 (transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET))
1967 urb->flags |= USBFS_URB_ZERO_PACKET;
1969 r = ioctl(hpriv->fd, IOCTL_USBFS_SUBMITURB, urb);
1973 if (errno == ENODEV) {
1974 r = LIBUSB_ERROR_NO_DEVICE;
1975 } else if (errno == ENOMEM) {
1976 r = LIBUSB_ERROR_NO_MEM;
1978 usbi_err(TRANSFER_CTX(transfer), "submiturb failed, errno=%d", errno);
1979 r = LIBUSB_ERROR_IO;
1982 /* if the first URB submission fails, we can simply free up and
1983 * return failure immediately. */
1985 usbi_dbg("first URB failed, easy peasy");
1991 /* if it's not the first URB that failed, the situation is a bit
1992 * tricky. we may need to discard all previous URBs. there are
1994 * - discarding is asynchronous - discarded urbs will be reaped
1995 * later. the user must not have freed the transfer when the
1996 * discarded URBs are reaped, otherwise libusb will be using
1998 * - the earlier URBs may have completed successfully and we do
1999 * not want to throw away any data.
2000 * - this URB failing may be no error; EREMOTEIO means that
2001 * this transfer simply didn't need all the URBs we submitted
2002 * so, we report that the transfer was submitted successfully and
2003 * in case of error we discard all previous URBs. later when
2004 * the final reap completes we can report error to the user,
2005 * or success if an earlier URB was completed successfully.
2007 tpriv->reap_action = errno == EREMOTEIO ? COMPLETED_EARLY : SUBMIT_FAILED;
2009 /* The URBs we haven't submitted yet we count as already
2011 tpriv->num_retired += num_urbs - i;
2013 /* If we completed short then don't try to discard. */
2014 if (tpriv->reap_action == COMPLETED_EARLY)
2017 discard_urbs(itransfer, 0, i);
2019 usbi_dbg("reporting successful submission but waiting for %d "
2020 "discards before reporting error", i);
2027 static int submit_iso_transfer(struct usbi_transfer *itransfer)
2029 struct libusb_transfer *transfer =
2030 USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
2031 struct linux_transfer_priv *tpriv = usbi_get_transfer_priv(itransfer);
2032 struct linux_device_handle_priv *hpriv =
2033 usbi_get_device_handle_priv(transfer->dev_handle);
2034 struct usbfs_urb **urbs;
2035 int num_packets = transfer->num_iso_packets;
2036 int num_packets_remaining;
2039 unsigned int packet_len;
2040 unsigned int total_len = 0;
2041 unsigned char *urb_buffer = transfer->buffer;
2043 if (num_packets < 1)
2044 return LIBUSB_ERROR_INVALID_PARAM;
2046 /* usbfs places arbitrary limits on iso URBs. this limit has changed
2047 * at least three times, but we attempt to detect this limit during
2048 * init and check it here. if the kernel rejects the request due to
2049 * its size, we return an error indicating such to the user.
2051 for (i = 0; i < num_packets; i++) {
2052 packet_len = transfer->iso_packet_desc[i].length;
2054 if (packet_len > max_iso_packet_len) {
2055 usbi_warn(TRANSFER_CTX(transfer),
2056 "iso packet length of %u bytes exceeds maximum of %u bytes",
2057 packet_len, max_iso_packet_len);
2058 return LIBUSB_ERROR_INVALID_PARAM;
2061 total_len += packet_len;
2064 if (transfer->length < (int)total_len)
2065 return LIBUSB_ERROR_INVALID_PARAM;
2067 /* usbfs limits the number of iso packets per URB */
2068 num_urbs = (num_packets + (MAX_ISO_PACKETS_PER_URB - 1)) / MAX_ISO_PACKETS_PER_URB;
2070 usbi_dbg("need %d urbs for new transfer with length %d", num_urbs, transfer->length);
2072 urbs = calloc(num_urbs, sizeof(*urbs));
2074 return LIBUSB_ERROR_NO_MEM;
2076 tpriv->iso_urbs = urbs;
2077 tpriv->num_urbs = num_urbs;
2078 tpriv->num_retired = 0;
2079 tpriv->reap_action = NORMAL;
2080 tpriv->iso_packet_offset = 0;
2082 /* allocate + initialize each URB with the correct number of packets */
2083 num_packets_remaining = num_packets;
2084 for (i = 0, j = 0; i < num_urbs; i++) {
2085 int num_packets_in_urb = MIN(num_packets_remaining, MAX_ISO_PACKETS_PER_URB);
2086 struct usbfs_urb *urb;
2090 alloc_size = sizeof(*urb)
2091 + (num_packets_in_urb * sizeof(struct usbfs_iso_packet_desc));
2092 urb = calloc(1, alloc_size);
2094 free_iso_urbs(tpriv);
2095 return LIBUSB_ERROR_NO_MEM;
2099 /* populate packet lengths */
2100 for (k = 0; k < num_packets_in_urb; j++, k++) {
2101 packet_len = transfer->iso_packet_desc[j].length;
2102 urb->buffer_length += packet_len;
2103 urb->iso_frame_desc[k].length = packet_len;
2106 urb->usercontext = itransfer;
2107 urb->type = USBFS_URB_TYPE_ISO;
2108 /* FIXME: interface for non-ASAP data? */
2109 urb->flags = USBFS_URB_ISO_ASAP;
2110 urb->endpoint = transfer->endpoint;
2111 urb->number_of_packets = num_packets_in_urb;
2112 urb->buffer = urb_buffer;
2114 urb_buffer += urb->buffer_length;
2115 num_packets_remaining -= num_packets_in_urb;
2119 for (i = 0; i < num_urbs; i++) {
2120 int r = ioctl(hpriv->fd, IOCTL_USBFS_SUBMITURB, urbs[i]);
2125 if (errno == ENODEV) {
2126 r = LIBUSB_ERROR_NO_DEVICE;
2127 } else if (errno == EINVAL) {
2128 usbi_warn(TRANSFER_CTX(transfer), "submiturb failed, transfer too large");
2129 r = LIBUSB_ERROR_INVALID_PARAM;
2130 } else if (errno == EMSGSIZE) {
2131 usbi_warn(TRANSFER_CTX(transfer), "submiturb failed, iso packet length too large");
2132 r = LIBUSB_ERROR_INVALID_PARAM;
2134 usbi_err(TRANSFER_CTX(transfer), "submiturb failed, errno=%d", errno);
2135 r = LIBUSB_ERROR_IO;
2138 /* if the first URB submission fails, we can simply free up and
2139 * return failure immediately. */
2141 usbi_dbg("first URB failed, easy peasy");
2142 free_iso_urbs(tpriv);
2146 /* if it's not the first URB that failed, the situation is a bit
2147 * tricky. we must discard all previous URBs. there are
2149 * - discarding is asynchronous - discarded urbs will be reaped
2150 * later. the user must not have freed the transfer when the
2151 * discarded URBs are reaped, otherwise libusb will be using
2153 * - the earlier URBs may have completed successfully and we do
2154 * not want to throw away any data.
2155 * so, in this case we discard all the previous URBs BUT we report
2156 * that the transfer was submitted successfully. then later when
2157 * the final discard completes we can report error to the user.
2159 tpriv->reap_action = SUBMIT_FAILED;
2161 /* The URBs we haven't submitted yet we count as already
2163 tpriv->num_retired = num_urbs - i;
2164 discard_urbs(itransfer, 0, i);
2166 usbi_dbg("reporting successful submission but waiting for %d "
2167 "discards before reporting error", i);
2174 static int submit_control_transfer(struct usbi_transfer *itransfer)
2176 struct linux_transfer_priv *tpriv = usbi_get_transfer_priv(itransfer);
2177 struct libusb_transfer *transfer =
2178 USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
2179 struct linux_device_handle_priv *hpriv =
2180 usbi_get_device_handle_priv(transfer->dev_handle);
2181 struct usbfs_urb *urb;
2184 if (transfer->length - LIBUSB_CONTROL_SETUP_SIZE > MAX_CTRL_BUFFER_LENGTH)
2185 return LIBUSB_ERROR_INVALID_PARAM;
2187 urb = calloc(1, sizeof(*urb));
2189 return LIBUSB_ERROR_NO_MEM;
2191 tpriv->num_urbs = 1;
2192 tpriv->reap_action = NORMAL;
2194 urb->usercontext = itransfer;
2195 urb->type = USBFS_URB_TYPE_CONTROL;
2196 urb->endpoint = transfer->endpoint;
2197 urb->buffer = transfer->buffer;
2198 urb->buffer_length = transfer->length;
2200 r = ioctl(hpriv->fd, IOCTL_USBFS_SUBMITURB, urb);
2204 if (errno == ENODEV)
2205 return LIBUSB_ERROR_NO_DEVICE;
2207 usbi_err(TRANSFER_CTX(transfer), "submiturb failed, errno=%d", errno);
2208 return LIBUSB_ERROR_IO;
2213 static int op_submit_transfer(struct usbi_transfer *itransfer)
2215 struct libusb_transfer *transfer =
2216 USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
2218 switch (transfer->type) {
2219 case LIBUSB_TRANSFER_TYPE_CONTROL:
2220 return submit_control_transfer(itransfer);
2221 case LIBUSB_TRANSFER_TYPE_BULK:
2222 case LIBUSB_TRANSFER_TYPE_BULK_STREAM:
2223 return submit_bulk_transfer(itransfer);
2224 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
2225 return submit_bulk_transfer(itransfer);
2226 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
2227 return submit_iso_transfer(itransfer);
2229 usbi_err(TRANSFER_CTX(transfer), "unknown transfer type %u", transfer->type);
2230 return LIBUSB_ERROR_INVALID_PARAM;
2234 static int op_cancel_transfer(struct usbi_transfer *itransfer)
2236 struct linux_transfer_priv *tpriv = usbi_get_transfer_priv(itransfer);
2237 struct libusb_transfer *transfer =
2238 USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
2242 return LIBUSB_ERROR_NOT_FOUND;
2244 r = discard_urbs(itransfer, 0, tpriv->num_urbs);
2248 switch (transfer->type) {
2249 case LIBUSB_TRANSFER_TYPE_BULK:
2250 case LIBUSB_TRANSFER_TYPE_BULK_STREAM:
2251 if (tpriv->reap_action == ERROR)
2253 /* else, fall through */
2255 tpriv->reap_action = CANCELLED;
2261 static void op_clear_transfer_priv(struct usbi_transfer *itransfer)
2263 struct libusb_transfer *transfer =
2264 USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
2265 struct linux_transfer_priv *tpriv = usbi_get_transfer_priv(itransfer);
2267 switch (transfer->type) {
2268 case LIBUSB_TRANSFER_TYPE_CONTROL:
2269 case LIBUSB_TRANSFER_TYPE_BULK:
2270 case LIBUSB_TRANSFER_TYPE_BULK_STREAM:
2271 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
2277 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
2278 if (tpriv->iso_urbs) {
2279 free_iso_urbs(tpriv);
2280 tpriv->iso_urbs = NULL;
2284 usbi_err(TRANSFER_CTX(transfer), "unknown transfer type %u", transfer->type);
2288 static int handle_bulk_completion(struct usbi_transfer *itransfer,
2289 struct usbfs_urb *urb)
2291 struct linux_transfer_priv *tpriv = usbi_get_transfer_priv(itransfer);
2292 struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
2293 int urb_idx = urb - tpriv->urbs;
2295 usbi_mutex_lock(&itransfer->lock);
2296 usbi_dbg("handling completion status %d of bulk urb %d/%d", urb->status,
2297 urb_idx + 1, tpriv->num_urbs);
2299 tpriv->num_retired++;
2301 if (tpriv->reap_action != NORMAL) {
2302 /* cancelled, submit_fail, or completed early */
2303 usbi_dbg("abnormal reap: urb status %d", urb->status);
2305 /* even though we're in the process of cancelling, it's possible that
2306 * we may receive some data in these URBs that we don't want to lose.
2308 * 1. while the kernel is cancelling all the packets that make up an
2309 * URB, a few of them might complete. so we get back a successful
2310 * cancellation *and* some data.
2311 * 2. we receive a short URB which marks the early completion condition,
2312 * so we start cancelling the remaining URBs. however, we're too
2313 * slow and another URB completes (or at least completes partially).
2314 * (this can't happen since we always use BULK_CONTINUATION.)
2316 * When this happens, our objectives are not to lose any "surplus" data,
2317 * and also to stick it at the end of the previously-received data
2318 * (closing any holes), so that libusb reports the total amount of
2319 * transferred data and presents it in a contiguous chunk.
2321 if (urb->actual_length > 0) {
2322 unsigned char *target = transfer->buffer + itransfer->transferred;
2324 usbi_dbg("received %d bytes of surplus data", urb->actual_length);
2325 if (urb->buffer != target) {
2326 usbi_dbg("moving surplus data from offset %zu to offset %zu",
2327 (unsigned char *)urb->buffer - transfer->buffer,
2328 target - transfer->buffer);
2329 memmove(target, urb->buffer, urb->actual_length);
2331 itransfer->transferred += urb->actual_length;
2334 if (tpriv->num_retired == tpriv->num_urbs) {
2335 usbi_dbg("abnormal reap: last URB handled, reporting");
2336 if (tpriv->reap_action != COMPLETED_EARLY &&
2337 tpriv->reap_status == LIBUSB_TRANSFER_COMPLETED)
2338 tpriv->reap_status = LIBUSB_TRANSFER_ERROR;
2344 itransfer->transferred += urb->actual_length;
2346 /* Many of these errors can occur on *any* urb of a multi-urb
2347 * transfer. When they do, we tear down the rest of the transfer.
2349 switch (urb->status) {
2352 case -EREMOTEIO: /* short transfer */
2354 case -ENOENT: /* cancelled */
2359 usbi_dbg("device removed");
2360 tpriv->reap_status = LIBUSB_TRANSFER_NO_DEVICE;
2361 goto cancel_remaining;
2363 usbi_dbg("detected endpoint stall");
2364 if (tpriv->reap_status == LIBUSB_TRANSFER_COMPLETED)
2365 tpriv->reap_status = LIBUSB_TRANSFER_STALL;
2366 goto cancel_remaining;
2368 /* overflow can only ever occur in the last urb */
2369 usbi_dbg("overflow, actual_length=%d", urb->actual_length);
2370 if (tpriv->reap_status == LIBUSB_TRANSFER_COMPLETED)
2371 tpriv->reap_status = LIBUSB_TRANSFER_OVERFLOW;
2378 usbi_dbg("low-level bus error %d", urb->status);
2379 tpriv->reap_action = ERROR;
2380 goto cancel_remaining;
2382 usbi_warn(ITRANSFER_CTX(itransfer), "unrecognised urb status %d", urb->status);
2383 tpriv->reap_action = ERROR;
2384 goto cancel_remaining;
2387 /* if we've reaped all urbs or we got less data than requested then we're
2389 if (tpriv->num_retired == tpriv->num_urbs) {
2390 usbi_dbg("all URBs in transfer reaped --> complete!");
2392 } else if (urb->actual_length < urb->buffer_length) {
2393 usbi_dbg("short transfer %d/%d --> complete!",
2394 urb->actual_length, urb->buffer_length);
2395 if (tpriv->reap_action == NORMAL)
2396 tpriv->reap_action = COMPLETED_EARLY;
2402 if (tpriv->reap_action == ERROR && tpriv->reap_status == LIBUSB_TRANSFER_COMPLETED)
2403 tpriv->reap_status = LIBUSB_TRANSFER_ERROR;
2405 if (tpriv->num_retired == tpriv->num_urbs) /* nothing to cancel */
2408 /* cancel remaining urbs and wait for their completion before
2409 * reporting results */
2410 discard_urbs(itransfer, urb_idx + 1, tpriv->num_urbs);
2413 usbi_mutex_unlock(&itransfer->lock);
2419 usbi_mutex_unlock(&itransfer->lock);
2420 return tpriv->reap_action == CANCELLED ?
2421 usbi_handle_transfer_cancellation(itransfer) :
2422 usbi_handle_transfer_completion(itransfer, tpriv->reap_status);
2425 static int handle_iso_completion(struct usbi_transfer *itransfer,
2426 struct usbfs_urb *urb)
2428 struct libusb_transfer *transfer =
2429 USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
2430 struct linux_transfer_priv *tpriv = usbi_get_transfer_priv(itransfer);
2431 int num_urbs = tpriv->num_urbs;
2434 enum libusb_transfer_status status = LIBUSB_TRANSFER_COMPLETED;
2436 usbi_mutex_lock(&itransfer->lock);
2437 for (i = 0; i < num_urbs; i++) {
2438 if (urb == tpriv->iso_urbs[i]) {
2444 usbi_err(TRANSFER_CTX(transfer), "could not locate urb!");
2445 usbi_mutex_unlock(&itransfer->lock);
2446 return LIBUSB_ERROR_NOT_FOUND;
2449 usbi_dbg("handling completion status %d of iso urb %d/%d", urb->status,
2452 /* copy isochronous results back in */
2454 for (i = 0; i < urb->number_of_packets; i++) {
2455 struct usbfs_iso_packet_desc *urb_desc = &urb->iso_frame_desc[i];
2456 struct libusb_iso_packet_descriptor *lib_desc =
2457 &transfer->iso_packet_desc[tpriv->iso_packet_offset++];
2459 lib_desc->status = LIBUSB_TRANSFER_COMPLETED;
2460 switch (urb_desc->status) {
2463 case -ENOENT: /* cancelled */
2468 usbi_dbg("packet %d - device removed", i);
2469 lib_desc->status = LIBUSB_TRANSFER_NO_DEVICE;
2472 usbi_dbg("packet %d - detected endpoint stall", i);
2473 lib_desc->status = LIBUSB_TRANSFER_STALL;
2476 usbi_dbg("packet %d - overflow error", i);
2477 lib_desc->status = LIBUSB_TRANSFER_OVERFLOW;
2485 usbi_dbg("packet %d - low-level USB error %d", i, urb_desc->status);
2486 lib_desc->status = LIBUSB_TRANSFER_ERROR;
2489 usbi_warn(TRANSFER_CTX(transfer), "packet %d - unrecognised urb status %d",
2490 i, urb_desc->status);
2491 lib_desc->status = LIBUSB_TRANSFER_ERROR;
2494 lib_desc->actual_length = urb_desc->actual_length;
2497 tpriv->num_retired++;
2499 if (tpriv->reap_action != NORMAL) { /* cancelled or submit_fail */
2500 usbi_dbg("CANCEL: urb status %d", urb->status);
2502 if (tpriv->num_retired == num_urbs) {
2503 usbi_dbg("CANCEL: last URB handled, reporting");
2504 free_iso_urbs(tpriv);
2505 if (tpriv->reap_action == CANCELLED) {
2506 usbi_mutex_unlock(&itransfer->lock);
2507 return usbi_handle_transfer_cancellation(itransfer);
2509 usbi_mutex_unlock(&itransfer->lock);
2510 return usbi_handle_transfer_completion(itransfer, LIBUSB_TRANSFER_ERROR);
2516 switch (urb->status) {
2519 case -ENOENT: /* cancelled */
2523 usbi_dbg("device removed");
2524 status = LIBUSB_TRANSFER_NO_DEVICE;
2527 usbi_warn(TRANSFER_CTX(transfer), "unrecognised urb status %d", urb->status);
2528 status = LIBUSB_TRANSFER_ERROR;
2532 /* if we've reaped all urbs then we're done */
2533 if (tpriv->num_retired == num_urbs) {
2534 usbi_dbg("all URBs in transfer reaped --> complete!");
2535 free_iso_urbs(tpriv);
2536 usbi_mutex_unlock(&itransfer->lock);
2537 return usbi_handle_transfer_completion(itransfer, status);
2541 usbi_mutex_unlock(&itransfer->lock);
2545 static int handle_control_completion(struct usbi_transfer *itransfer,
2546 struct usbfs_urb *urb)
2548 struct linux_transfer_priv *tpriv = usbi_get_transfer_priv(itransfer);
2551 usbi_mutex_lock(&itransfer->lock);
2552 usbi_dbg("handling completion status %d", urb->status);
2554 itransfer->transferred += urb->actual_length;
2556 if (tpriv->reap_action == CANCELLED) {
2557 if (urb->status && urb->status != -ENOENT)
2558 usbi_warn(ITRANSFER_CTX(itransfer), "cancel: unrecognised urb status %d",
2562 usbi_mutex_unlock(&itransfer->lock);
2563 return usbi_handle_transfer_cancellation(itransfer);
2566 switch (urb->status) {
2568 status = LIBUSB_TRANSFER_COMPLETED;
2570 case -ENOENT: /* cancelled */
2571 status = LIBUSB_TRANSFER_CANCELLED;
2575 usbi_dbg("device removed");
2576 status = LIBUSB_TRANSFER_NO_DEVICE;
2579 usbi_dbg("unsupported control request");
2580 status = LIBUSB_TRANSFER_STALL;
2583 usbi_dbg("overflow, actual_length=%d", urb->actual_length);
2584 status = LIBUSB_TRANSFER_OVERFLOW;
2591 usbi_dbg("low-level bus error %d", urb->status);
2592 status = LIBUSB_TRANSFER_ERROR;
2595 usbi_warn(ITRANSFER_CTX(itransfer), "unrecognised urb status %d", urb->status);
2596 status = LIBUSB_TRANSFER_ERROR;
2602 usbi_mutex_unlock(&itransfer->lock);
2603 return usbi_handle_transfer_completion(itransfer, status);
2606 static int reap_for_handle(struct libusb_device_handle *handle)
2608 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
2610 struct usbfs_urb *urb = NULL;
2611 struct usbi_transfer *itransfer;
2612 struct libusb_transfer *transfer;
2614 r = ioctl(hpriv->fd, IOCTL_USBFS_REAPURBNDELAY, &urb);
2616 if (errno == EAGAIN)
2618 if (errno == ENODEV)
2619 return LIBUSB_ERROR_NO_DEVICE;
2621 usbi_err(HANDLE_CTX(handle), "reap failed, errno=%d", errno);
2622 return LIBUSB_ERROR_IO;
2625 itransfer = urb->usercontext;
2626 transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
2628 usbi_dbg("urb type=%u status=%d transferred=%d", urb->type, urb->status, urb->actual_length);
2630 switch (transfer->type) {
2631 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
2632 return handle_iso_completion(itransfer, urb);
2633 case LIBUSB_TRANSFER_TYPE_BULK:
2634 case LIBUSB_TRANSFER_TYPE_BULK_STREAM:
2635 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
2636 return handle_bulk_completion(itransfer, urb);
2637 case LIBUSB_TRANSFER_TYPE_CONTROL:
2638 return handle_control_completion(itransfer, urb);
2640 usbi_err(HANDLE_CTX(handle), "unrecognised transfer type %u", transfer->type);
2641 return LIBUSB_ERROR_OTHER;
2645 static int op_handle_events(struct libusb_context *ctx,
2646 void *event_data, unsigned int count, unsigned int num_ready)
2648 struct pollfd *fds = event_data;
2652 usbi_mutex_lock(&ctx->open_devs_lock);
2653 for (n = 0; n < count && num_ready > 0; n++) {
2654 struct pollfd *pollfd = &fds[n];
2655 struct libusb_device_handle *handle;
2656 struct linux_device_handle_priv *hpriv = NULL;
2659 if (!pollfd->revents)
2663 for_each_open_device(ctx, handle) {
2664 hpriv = usbi_get_device_handle_priv(handle);
2665 if (hpriv->fd == pollfd->fd)
2669 if (!hpriv || hpriv->fd != pollfd->fd) {
2670 usbi_err(ctx, "cannot find handle for fd %d",
2675 if (pollfd->revents & POLLERR) {
2676 /* remove the fd from the pollfd set so that it doesn't continuously
2677 * trigger an event, and flag that it has been removed so op_close()
2678 * doesn't try to remove it a second time */
2679 usbi_remove_event_source(HANDLE_CTX(handle), hpriv->fd);
2680 hpriv->fd_removed = 1;
2682 /* device will still be marked as attached if hotplug monitor thread
2683 * hasn't processed remove event yet */
2684 usbi_mutex_static_lock(&linux_hotplug_lock);
2685 if (handle->dev->attached)
2686 linux_device_disconnected(handle->dev->bus_number,
2687 handle->dev->device_address);
2688 usbi_mutex_static_unlock(&linux_hotplug_lock);
2690 if (hpriv->caps & USBFS_CAP_REAP_AFTER_DISCONNECT) {
2692 r = reap_for_handle(handle);
2696 usbi_handle_disconnect(handle);
2702 r = reap_for_handle(handle);
2703 } while (r == 0 && ++reap_count <= 25);
2705 if (r == 1 || r == LIBUSB_ERROR_NO_DEVICE)
2713 usbi_mutex_unlock(&ctx->open_devs_lock);
2717 const struct usbi_os_backend usbi_backend = {
2718 .name = "Linux usbfs",
2719 .caps = USBI_CAP_HAS_HID_ACCESS|USBI_CAP_SUPPORTS_DETACH_KERNEL_DRIVER,
2722 .set_option = op_set_option,
2723 .hotplug_poll = op_hotplug_poll,
2724 .get_active_config_descriptor = op_get_active_config_descriptor,
2725 .get_config_descriptor = op_get_config_descriptor,
2726 .get_config_descriptor_by_value = op_get_config_descriptor_by_value,
2728 .wrap_sys_device = op_wrap_sys_device,
2731 .get_configuration = op_get_configuration,
2732 .set_configuration = op_set_configuration,
2733 .claim_interface = op_claim_interface,
2734 .release_interface = op_release_interface,
2736 .set_interface_altsetting = op_set_interface,
2737 .clear_halt = op_clear_halt,
2738 .reset_device = op_reset_device,
2740 .alloc_streams = op_alloc_streams,
2741 .free_streams = op_free_streams,
2743 .dev_mem_alloc = op_dev_mem_alloc,
2744 .dev_mem_free = op_dev_mem_free,
2746 .kernel_driver_active = op_kernel_driver_active,
2747 .detach_kernel_driver = op_detach_kernel_driver,
2748 .attach_kernel_driver = op_attach_kernel_driver,
2750 .destroy_device = op_destroy_device,
2752 .submit_transfer = op_submit_transfer,
2753 .cancel_transfer = op_cancel_transfer,
2754 .clear_transfer_priv = op_clear_transfer_priv,
2756 .handle_events = op_handle_events,
2758 .device_priv_size = sizeof(struct linux_device_priv),
2759 .device_handle_priv_size = sizeof(struct linux_device_handle_priv),
2760 .transfer_priv_size = sizeof(struct linux_transfer_priv),