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;
98 /* Serialize hotplug start/stop */
99 static usbi_mutex_static_t linux_hotplug_startstop_lock = USBI_MUTEX_INITIALIZER;
100 /* Serialize scan-devices, event-thread, and poll */
101 usbi_mutex_static_t linux_hotplug_lock = USBI_MUTEX_INITIALIZER;
103 static int linux_scan_devices(struct libusb_context *ctx);
104 static int detach_kernel_driver_and_claim(struct libusb_device_handle *, uint8_t);
106 #if !defined(HAVE_LIBUDEV)
107 static int linux_default_scan_devices(struct libusb_context *ctx);
110 struct kernel_version {
116 struct config_descriptor {
117 struct usbi_configuration_descriptor *desc;
121 struct linux_device_priv {
124 size_t descriptors_len;
125 struct config_descriptor *config_descriptors;
126 uint8_t active_config; /* cache val for !sysfs_available */
129 struct linux_device_handle_priv {
138 /* submission failed after the first URB, so await cancellation/completion
139 * of all the others */
142 /* cancelled by user or timeout */
145 /* completed multi-URB transfer in non-final URB */
148 /* one or more urbs encountered a low-level error */
152 struct linux_transfer_priv {
154 struct usbfs_urb *urbs;
155 struct usbfs_urb **iso_urbs;
158 enum reap_action reap_action;
161 enum libusb_transfer_status reap_status;
163 /* next iso packet in user-supplied transfer to be populated */
164 int iso_packet_offset;
167 static int get_usbfs_fd(struct libusb_device *dev, mode_t mode, int silent)
169 struct libusb_context *ctx = DEVICE_CTX(dev);
174 sprintf(path, USBDEV_PATH "/usbdev%u.%u",
175 dev->bus_number, dev->device_address);
177 sprintf(path, USB_DEVTMPFS_PATH "/%03u/%03u",
178 dev->bus_number, dev->device_address);
180 fd = open(path, mode | O_CLOEXEC);
182 return fd; /* Success */
184 if (errno == ENOENT) {
185 const long delay_ms = 10L;
186 const struct timespec delay_ts = { 0L, delay_ms * 1000L * 1000L };
189 usbi_err(ctx, "File doesn't exist, wait %ld ms and try again", delay_ms);
191 /* Wait 10ms for USB device path creation.*/
192 nanosleep(&delay_ts, NULL);
194 fd = open(path, mode | O_CLOEXEC);
196 return fd; /* Success */
200 usbi_err(ctx, "libusb couldn't open USB device %s, errno=%d", path, errno);
201 if (errno == EACCES && mode == O_RDWR)
202 usbi_err(ctx, "libusb requires write access to USB device nodes");
206 return LIBUSB_ERROR_ACCESS;
208 return LIBUSB_ERROR_NO_DEVICE;
209 return LIBUSB_ERROR_IO;
212 /* check dirent for a /dev/usbdev%d.%d name
213 * optionally return bus/device on success */
214 static int is_usbdev_entry(const char *name, uint8_t *bus_p, uint8_t *dev_p)
218 if (sscanf(name, "usbdev%d.%d", &busnum, &devnum) != 2)
220 if (busnum < 0 || busnum > UINT8_MAX || devnum < 0 || devnum > UINT8_MAX) {
221 usbi_dbg("invalid usbdev format '%s'", name);
225 usbi_dbg("found: %s", name);
227 *bus_p = (uint8_t)busnum;
229 *dev_p = (uint8_t)devnum;
233 static const char *find_usbfs_path(void)
237 struct dirent *entry;
239 path = USB_DEVTMPFS_PATH;
242 while ((entry = readdir(dir))) {
243 if (entry->d_name[0] == '.')
246 /* We assume if we find any files that it must be the right place */
256 /* look for /dev/usbdev*.* if the normal place fails */
260 while ((entry = readdir(dir))) {
261 if (entry->d_name[0] == '.')
264 if (is_usbdev_entry(entry->d_name, NULL, NULL)) {
265 /* found one; that's enough */
278 /* On udev based systems without any usb-devices /dev/bus/usb will not
279 * exist. So if we've not found anything and we're using udev for hotplug
280 * simply assume /dev/bus/usb rather then making libusb_init fail.
281 * Make the same assumption for Android where SELinux policies might block us
282 * from reading /dev on newer devices. */
283 #if defined(HAVE_LIBUDEV) || defined(__ANDROID__)
284 return USB_DEVTMPFS_PATH;
290 static int get_kernel_version(struct libusb_context *ctx,
291 struct kernel_version *ver)
296 if (uname(&uts) < 0) {
297 usbi_err(ctx, "uname failed, errno=%d", errno);
301 atoms = sscanf(uts.release, "%d.%d.%d", &ver->major, &ver->minor, &ver->sublevel);
303 usbi_err(ctx, "failed to parse uname release '%s'", uts.release);
310 usbi_dbg("reported kernel version is %s", uts.release);
315 static int kernel_version_ge(const struct kernel_version *ver,
316 int major, int minor, int sublevel)
318 if (ver->major > major)
320 else if (ver->major < major)
323 /* kmajor == major */
324 if (ver->minor > minor)
326 else if (ver->minor < minor)
329 /* kminor == minor */
330 if (ver->sublevel == -1)
331 return sublevel == 0;
333 return ver->sublevel >= sublevel;
336 static int op_init(struct libusb_context *ctx)
338 struct kernel_version kversion;
339 const char *usbfs_path;
342 if (get_kernel_version(ctx, &kversion) < 0)
343 return LIBUSB_ERROR_OTHER;
345 if (!kernel_version_ge(&kversion, 2, 6, 32)) {
346 usbi_err(ctx, "kernel version is too old (reported as %d.%d.%d)",
347 kversion.major, kversion.minor,
348 kversion.sublevel != -1 ? kversion.sublevel : 0);
349 return LIBUSB_ERROR_NOT_SUPPORTED;
352 usbfs_path = find_usbfs_path();
354 usbi_err(ctx, "could not find usbfs");
355 return LIBUSB_ERROR_OTHER;
358 usbi_dbg("found usbfs at %s", usbfs_path);
360 if (!max_iso_packet_len) {
361 if (kernel_version_ge(&kversion, 5, 2, 0))
362 max_iso_packet_len = 98304;
363 else if (kernel_version_ge(&kversion, 3, 10, 0))
364 max_iso_packet_len = 49152;
366 max_iso_packet_len = 8192;
369 usbi_dbg("max iso packet length is (likely) %u bytes", max_iso_packet_len);
371 if (sysfs_available == -1) {
372 struct statfs statfsbuf;
374 r = statfs(SYSFS_MOUNT_PATH, &statfsbuf);
375 if (r == 0 && statfsbuf.f_type == SYSFS_MAGIC) {
376 usbi_dbg("sysfs is available");
379 usbi_warn(ctx, "sysfs not mounted");
384 usbi_mutex_static_lock(&linux_hotplug_startstop_lock);
386 if (init_count == 0) {
387 /* start up hotplug event handler */
388 r = linux_start_event_monitor();
390 if (r == LIBUSB_SUCCESS) {
391 r = linux_scan_devices(ctx);
392 if (r == LIBUSB_SUCCESS)
394 else if (init_count == 0)
395 linux_stop_event_monitor();
397 usbi_err(ctx, "error starting hotplug event monitor");
399 usbi_mutex_static_unlock(&linux_hotplug_startstop_lock);
404 static void op_exit(struct libusb_context *ctx)
407 usbi_mutex_static_lock(&linux_hotplug_startstop_lock);
408 assert(init_count != 0);
410 /* tear down event handler */
411 linux_stop_event_monitor();
413 usbi_mutex_static_unlock(&linux_hotplug_startstop_lock);
416 static int linux_scan_devices(struct libusb_context *ctx)
420 usbi_mutex_static_lock(&linux_hotplug_lock);
422 #if defined(HAVE_LIBUDEV)
423 ret = linux_udev_scan_devices(ctx);
425 ret = linux_default_scan_devices(ctx);
428 usbi_mutex_static_unlock(&linux_hotplug_lock);
433 static void op_hotplug_poll(void)
435 linux_hotplug_poll();
438 static int open_sysfs_attr(struct libusb_context *ctx,
439 const char *sysfs_dir, const char *attr)
444 snprintf(filename, sizeof(filename), SYSFS_DEVICE_PATH "/%s/%s", sysfs_dir, attr);
445 fd = open(filename, O_RDONLY | O_CLOEXEC);
447 if (errno == ENOENT) {
448 /* File doesn't exist. Assume the device has been
449 disconnected (see trac ticket #70). */
450 return LIBUSB_ERROR_NO_DEVICE;
452 usbi_err(ctx, "open %s failed, errno=%d", filename, errno);
453 return LIBUSB_ERROR_IO;
459 /* Note only suitable for attributes which always read >= 0, < 0 is error */
460 static int read_sysfs_attr(struct libusb_context *ctx,
461 const char *sysfs_dir, const char *attr, int max_value, int *value_p)
463 char buf[20], *endptr;
468 fd = open_sysfs_attr(ctx, sysfs_dir, attr);
472 r = read(fd, buf, sizeof(buf));
477 return LIBUSB_ERROR_NO_DEVICE;
478 usbi_err(ctx, "attribute %s read failed, errno=%zd", attr, r);
479 return LIBUSB_ERROR_IO;
484 /* Certain attributes (e.g. bConfigurationValue) are not
485 * populated if the device is not configured. */
490 /* The kernel does *not* NULL-terminate the string, but every attribute
491 * should be terminated with a newline character. */
492 if (!isdigit(buf[0])) {
493 usbi_err(ctx, "attribute %s doesn't have numeric value?", attr);
494 return LIBUSB_ERROR_IO;
495 } else if (buf[r - 1] != '\n') {
496 usbi_err(ctx, "attribute %s doesn't end with newline?", attr);
497 return LIBUSB_ERROR_IO;
502 value = strtol(buf, &endptr, 10);
503 if (value < 0 || value > (long)max_value || errno) {
504 usbi_err(ctx, "attribute %s contains an invalid value: '%s'", attr, buf);
505 return LIBUSB_ERROR_INVALID_PARAM;
506 } else if (*endptr != '\0') {
507 /* Consider the value to be valid if the remainder is a '.'
508 * character followed by numbers. This occurs, for example,
509 * when reading the "speed" attribute for a low-speed device
511 if (*endptr == '.' && isdigit(*(endptr + 1))) {
513 while (isdigit(*endptr))
516 if (*endptr != '\0') {
517 usbi_err(ctx, "attribute %s contains an invalid value: '%s'", attr, buf);
518 return LIBUSB_ERROR_INVALID_PARAM;
522 *value_p = (int)value;
526 static int sysfs_scan_device(struct libusb_context *ctx, const char *devname)
528 uint8_t busnum, devaddr;
531 ret = linux_get_device_address(ctx, 0, &busnum, &devaddr, NULL, devname, -1);
532 if (ret != LIBUSB_SUCCESS)
535 return linux_enumerate_device(ctx, busnum, devaddr, devname);
538 /* read the bConfigurationValue for a device */
539 static int sysfs_get_active_config(struct libusb_device *dev, uint8_t *config)
541 struct linux_device_priv *priv = usbi_get_device_priv(dev);
544 ret = read_sysfs_attr(DEVICE_CTX(dev), priv->sysfs_dir, "bConfigurationValue",
550 tmp = 0; /* unconfigured */
552 *config = (uint8_t)tmp;
557 int linux_get_device_address(struct libusb_context *ctx, int detached,
558 uint8_t *busnum, uint8_t *devaddr, const char *dev_node,
559 const char *sys_name, int fd)
564 usbi_dbg("getting address for device: %s detached: %d", sys_name, detached);
565 /* can't use sysfs to read the bus and device number if the
566 * device has been detached */
567 if (!sysfs_available || detached || !sys_name) {
568 if (!dev_node && fd >= 0) {
569 char *fd_path = alloca(PATH_MAX);
572 /* try to retrieve the device node from fd */
573 sprintf(proc_path, "/proc/self/fd/%d", fd);
574 r = readlink(proc_path, fd_path, PATH_MAX - 1);
582 return LIBUSB_ERROR_OTHER;
584 /* will this work with all supported kernel versions? */
585 if (!strncmp(dev_node, "/dev/bus/usb", 12))
586 sscanf(dev_node, "/dev/bus/usb/%hhu/%hhu", busnum, devaddr);
588 return LIBUSB_ERROR_OTHER;
590 return LIBUSB_SUCCESS;
593 usbi_dbg("scan %s", sys_name);
595 r = read_sysfs_attr(ctx, sys_name, "busnum", UINT8_MAX, &sysfs_val);
598 *busnum = (uint8_t)sysfs_val;
600 r = read_sysfs_attr(ctx, sys_name, "devnum", UINT8_MAX, &sysfs_val);
603 *devaddr = (uint8_t)sysfs_val;
605 usbi_dbg("bus=%u dev=%u", *busnum, *devaddr);
607 return LIBUSB_SUCCESS;
610 /* Return offset of the next config descriptor */
611 static int seek_to_next_config(struct libusb_context *ctx,
612 uint8_t *buffer, size_t len)
614 struct usbi_descriptor_header *header;
619 usbi_err(ctx, "short descriptor read %zu/2", len);
620 return LIBUSB_ERROR_IO;
623 header = (struct usbi_descriptor_header *)buffer;
624 if (header->bDescriptorType == LIBUSB_DT_CONFIG)
627 if (len < header->bLength) {
628 usbi_err(ctx, "bLength overflow by %zu bytes",
629 (size_t)header->bLength - len);
630 return LIBUSB_ERROR_IO;
633 offset += header->bLength;
634 buffer += header->bLength;
635 len -= header->bLength;
638 usbi_err(ctx, "config descriptor not found");
639 return LIBUSB_ERROR_IO;
642 static int parse_config_descriptors(struct libusb_device *dev)
644 struct libusb_context *ctx = DEVICE_CTX(dev);
645 struct linux_device_priv *priv = usbi_get_device_priv(dev);
646 struct usbi_device_descriptor *device_desc;
647 uint8_t idx, num_configs;
651 device_desc = (struct usbi_device_descriptor *)priv->descriptors;
652 num_configs = device_desc->bNumConfigurations;
654 if (num_configs == 0)
655 return 0; /* no configurations? */
657 priv->config_descriptors = malloc(num_configs * sizeof(priv->config_descriptors[0]));
658 if (!priv->config_descriptors)
659 return LIBUSB_ERROR_NO_MEM;
661 buffer = priv->descriptors + LIBUSB_DT_DEVICE_SIZE;
662 remaining = priv->descriptors_len - LIBUSB_DT_DEVICE_SIZE;
664 for (idx = 0; idx < num_configs; idx++) {
665 struct usbi_configuration_descriptor *config_desc;
668 if (remaining < LIBUSB_DT_CONFIG_SIZE) {
669 usbi_err(ctx, "short descriptor read %zu/%d",
670 remaining, LIBUSB_DT_CONFIG_SIZE);
671 return LIBUSB_ERROR_IO;
674 config_desc = (struct usbi_configuration_descriptor *)buffer;
675 if (config_desc->bDescriptorType != LIBUSB_DT_CONFIG) {
676 usbi_err(ctx, "descriptor is not a config desc (type 0x%02x)",
677 config_desc->bDescriptorType);
678 return LIBUSB_ERROR_IO;
679 } else if (config_desc->bLength < LIBUSB_DT_CONFIG_SIZE) {
680 usbi_err(ctx, "invalid descriptor bLength %u",
681 config_desc->bLength);
682 return LIBUSB_ERROR_IO;
685 config_len = libusb_le16_to_cpu(config_desc->wTotalLength);
686 if (config_len < LIBUSB_DT_CONFIG_SIZE) {
687 usbi_err(ctx, "invalid wTotalLength %u", config_len);
688 return LIBUSB_ERROR_IO;
691 if (priv->sysfs_dir) {
693 * In sysfs wTotalLength is ignored, instead the kernel returns a
694 * config descriptor with verified bLength fields, with descriptors
695 * with an invalid bLength removed.
697 uint16_t sysfs_config_len;
700 if (num_configs > 1 && idx < num_configs - 1) {
701 offset = seek_to_next_config(ctx, buffer + LIBUSB_DT_CONFIG_SIZE,
702 remaining - LIBUSB_DT_CONFIG_SIZE);
705 sysfs_config_len = (uint16_t)offset;
707 sysfs_config_len = (uint16_t)remaining;
710 if (config_len != sysfs_config_len) {
711 usbi_warn(ctx, "config length mismatch wTotalLength %u real %u",
712 config_len, sysfs_config_len);
713 config_len = sysfs_config_len;
717 * In usbfs the config descriptors are wTotalLength bytes apart,
718 * with any short reads from the device appearing as holes in the file.
720 if (config_len > remaining) {
721 usbi_warn(ctx, "short descriptor read %zu/%u", remaining, config_len);
722 config_len = (uint16_t)remaining;
726 priv->config_descriptors[idx].desc = config_desc;
727 priv->config_descriptors[idx].actual_len = config_len;
729 buffer += config_len;
730 remaining -= config_len;
733 return LIBUSB_SUCCESS;
736 static int op_get_config_descriptor_by_value(struct libusb_device *dev,
737 uint8_t value, void **buffer)
739 struct linux_device_priv *priv = usbi_get_device_priv(dev);
740 struct config_descriptor *config;
743 for (idx = 0; idx < dev->device_descriptor.bNumConfigurations; idx++) {
744 config = &priv->config_descriptors[idx];
745 if (config->desc->bConfigurationValue == value) {
746 *buffer = config->desc;
747 return (int)config->actual_len;
751 return LIBUSB_ERROR_NOT_FOUND;
754 static int op_get_active_config_descriptor(struct libusb_device *dev,
755 void *buffer, size_t len)
757 struct linux_device_priv *priv = usbi_get_device_priv(dev);
759 uint8_t active_config;
762 if (priv->sysfs_dir) {
763 r = sysfs_get_active_config(dev, &active_config);
767 /* Use cached bConfigurationValue */
768 active_config = priv->active_config;
771 if (active_config == 0) {
772 usbi_err(DEVICE_CTX(dev), "device unconfigured");
773 return LIBUSB_ERROR_NOT_FOUND;
776 r = op_get_config_descriptor_by_value(dev, active_config, &config_desc);
780 len = MIN(len, (size_t)r);
781 memcpy(buffer, config_desc, len);
785 static int op_get_config_descriptor(struct libusb_device *dev,
786 uint8_t config_index, void *buffer, size_t len)
788 struct linux_device_priv *priv = usbi_get_device_priv(dev);
789 struct config_descriptor *config;
791 if (config_index >= dev->device_descriptor.bNumConfigurations)
792 return LIBUSB_ERROR_NOT_FOUND;
794 config = &priv->config_descriptors[config_index];
795 len = MIN(len, config->actual_len);
796 memcpy(buffer, config->desc, len);
800 /* send a control message to retrieve active configuration */
801 static int usbfs_get_active_config(struct libusb_device *dev, int fd)
803 struct linux_device_priv *priv = usbi_get_device_priv(dev);
804 uint8_t active_config = 0;
807 struct usbfs_ctrltransfer ctrl = {
808 .bmRequestType = LIBUSB_ENDPOINT_IN,
809 .bRequest = LIBUSB_REQUEST_GET_CONFIGURATION,
814 .data = &active_config
817 r = ioctl(fd, IOCTL_USBFS_CONTROL, &ctrl);
820 return LIBUSB_ERROR_NO_DEVICE;
822 /* we hit this error path frequently with buggy devices :( */
823 usbi_warn(DEVICE_CTX(dev), "get configuration failed, errno=%d", errno);
824 } else if (active_config == 0) {
825 /* some buggy devices have a configuration 0, but we're
826 * reaching into the corner of a corner case here, so let's
827 * not support buggy devices in these circumstances.
828 * stick to the specs: a configuration value of 0 means
830 usbi_warn(DEVICE_CTX(dev), "active cfg 0? assuming unconfigured device");
833 priv->active_config = active_config;
835 return LIBUSB_SUCCESS;
838 static int initialize_device(struct libusb_device *dev, uint8_t busnum,
839 uint8_t devaddr, const char *sysfs_dir, int wrapped_fd)
841 struct linux_device_priv *priv = usbi_get_device_priv(dev);
842 struct libusb_context *ctx = DEVICE_CTX(dev);
847 dev->bus_number = busnum;
848 dev->device_address = devaddr;
851 priv->sysfs_dir = strdup(sysfs_dir);
852 if (!priv->sysfs_dir)
853 return LIBUSB_ERROR_NO_MEM;
855 /* Note speed can contain 1.5, in this case read_sysfs_attr()
856 will stop parsing at the '.' and return 1 */
857 if (read_sysfs_attr(ctx, sysfs_dir, "speed", INT_MAX, &speed) == 0) {
859 case 1: dev->speed = LIBUSB_SPEED_LOW; break;
860 case 12: dev->speed = LIBUSB_SPEED_FULL; break;
861 case 480: dev->speed = LIBUSB_SPEED_HIGH; break;
862 case 5000: dev->speed = LIBUSB_SPEED_SUPER; break;
863 case 10000: dev->speed = LIBUSB_SPEED_SUPER_PLUS; break;
865 usbi_warn(ctx, "unknown device speed: %d Mbps", speed);
870 /* cache descriptors in memory */
872 fd = open_sysfs_attr(ctx, sysfs_dir, "descriptors");
873 } else if (wrapped_fd < 0) {
874 fd = get_usbfs_fd(dev, O_RDONLY, 0);
877 r = lseek(fd, 0, SEEK_SET);
879 usbi_err(ctx, "lseek failed, errno=%d", errno);
880 return LIBUSB_ERROR_IO;
889 priv->descriptors = usbi_reallocf(priv->descriptors, alloc_len);
890 if (!priv->descriptors) {
891 if (fd != wrapped_fd)
893 return LIBUSB_ERROR_NO_MEM;
895 /* usbfs has holes in the file */
897 memset(priv->descriptors + priv->descriptors_len,
898 0, alloc_len - priv->descriptors_len);
899 nb = read(fd, priv->descriptors + priv->descriptors_len,
900 alloc_len - priv->descriptors_len);
902 usbi_err(ctx, "read descriptor failed, errno=%d", errno);
903 if (fd != wrapped_fd)
905 return LIBUSB_ERROR_IO;
907 priv->descriptors_len += (size_t)nb;
908 } while (priv->descriptors_len == alloc_len);
910 if (fd != wrapped_fd)
913 if (priv->descriptors_len < LIBUSB_DT_DEVICE_SIZE) {
914 usbi_err(ctx, "short descriptor read (%zu)", priv->descriptors_len);
915 return LIBUSB_ERROR_IO;
918 r = parse_config_descriptors(dev);
922 memcpy(&dev->device_descriptor, priv->descriptors, LIBUSB_DT_DEVICE_SIZE);
925 /* sysfs descriptors are in bus-endian format */
926 usbi_localize_device_descriptor(&dev->device_descriptor);
927 return LIBUSB_SUCCESS;
930 /* cache active config */
932 fd = get_usbfs_fd(dev, O_RDWR, 1);
936 /* cannot send a control message to determine the active
937 * config. just assume the first one is active. */
938 usbi_warn(ctx, "Missing rw usbfs access; cannot determine "
939 "active configuration descriptor");
940 if (priv->config_descriptors)
941 priv->active_config = priv->config_descriptors[0].desc->bConfigurationValue;
943 priv->active_config = 0; /* No config dt */
945 return LIBUSB_SUCCESS;
948 r = usbfs_get_active_config(dev, fd);
949 if (fd != wrapped_fd)
955 static int linux_get_parent_info(struct libusb_device *dev, const char *sysfs_dir)
957 struct libusb_context *ctx = DEVICE_CTX(dev);
958 struct libusb_device *it;
959 char *parent_sysfs_dir, *tmp;
960 int ret, add_parent = 1;
962 /* XXX -- can we figure out the topology when using usbfs? */
963 if (!sysfs_dir || !strncmp(sysfs_dir, "usb", 3)) {
964 /* either using usbfs or finding the parent of a root hub */
965 return LIBUSB_SUCCESS;
968 parent_sysfs_dir = strdup(sysfs_dir);
969 if (!parent_sysfs_dir)
970 return LIBUSB_ERROR_NO_MEM;
972 if ((tmp = strrchr(parent_sysfs_dir, '.')) ||
973 (tmp = strrchr(parent_sysfs_dir, '-'))) {
974 dev->port_number = atoi(tmp + 1);
977 usbi_warn(ctx, "Can not parse sysfs_dir: %s, no parent info",
979 free(parent_sysfs_dir);
980 return LIBUSB_SUCCESS;
983 /* is the parent a root hub? */
984 if (!strchr(parent_sysfs_dir, '-')) {
985 tmp = parent_sysfs_dir;
986 ret = asprintf(&parent_sysfs_dir, "usb%s", tmp);
989 return LIBUSB_ERROR_NO_MEM;
993 /* find the parent in the context */
994 usbi_mutex_lock(&ctx->usb_devs_lock);
995 for_each_device(ctx, it) {
996 struct linux_device_priv *priv = usbi_get_device_priv(it);
998 if (priv->sysfs_dir) {
999 if (!strcmp(priv->sysfs_dir, parent_sysfs_dir)) {
1000 dev->parent_dev = libusb_ref_device(it);
1005 usbi_mutex_unlock(&ctx->usb_devs_lock);
1007 if (!dev->parent_dev && add_parent) {
1008 usbi_dbg("parent_dev %s not enumerated yet, enumerating now",
1010 sysfs_scan_device(ctx, parent_sysfs_dir);
1015 usbi_dbg("dev %p (%s) has parent %p (%s) port %u", dev, sysfs_dir,
1016 dev->parent_dev, parent_sysfs_dir, dev->port_number);
1018 free(parent_sysfs_dir);
1020 return LIBUSB_SUCCESS;
1023 int linux_enumerate_device(struct libusb_context *ctx,
1024 uint8_t busnum, uint8_t devaddr, const char *sysfs_dir)
1026 unsigned long session_id;
1027 struct libusb_device *dev;
1030 /* FIXME: session ID is not guaranteed unique as addresses can wrap and
1031 * will be reused. instead we should add a simple sysfs attribute with
1033 session_id = busnum << 8 | devaddr;
1034 usbi_dbg("busnum %u devaddr %u session_id %lu", busnum, devaddr, session_id);
1036 dev = usbi_get_device_by_session_id(ctx, session_id);
1038 /* device already exists in the context */
1039 usbi_dbg("session_id %lu already exists", session_id);
1040 libusb_unref_device(dev);
1041 return LIBUSB_SUCCESS;
1044 usbi_dbg("allocating new device for %u/%u (session %lu)",
1045 busnum, devaddr, session_id);
1046 dev = usbi_alloc_device(ctx, session_id);
1048 return LIBUSB_ERROR_NO_MEM;
1050 r = initialize_device(dev, busnum, devaddr, sysfs_dir, -1);
1053 r = usbi_sanitize_device(dev);
1057 r = linux_get_parent_info(dev, sysfs_dir);
1062 libusb_unref_device(dev);
1064 usbi_connect_device(dev);
1069 void linux_hotplug_enumerate(uint8_t busnum, uint8_t devaddr, const char *sys_name)
1071 struct libusb_context *ctx;
1073 usbi_mutex_static_lock(&active_contexts_lock);
1074 for_each_context(ctx) {
1075 linux_enumerate_device(ctx, busnum, devaddr, sys_name);
1077 usbi_mutex_static_unlock(&active_contexts_lock);
1080 void linux_device_disconnected(uint8_t busnum, uint8_t devaddr)
1082 struct libusb_context *ctx;
1083 struct libusb_device *dev;
1084 unsigned long session_id = busnum << 8 | devaddr;
1086 usbi_mutex_static_lock(&active_contexts_lock);
1087 for_each_context(ctx) {
1088 dev = usbi_get_device_by_session_id(ctx, session_id);
1090 usbi_disconnect_device(dev);
1091 libusb_unref_device(dev);
1093 usbi_dbg("device not found for session %lx", session_id);
1096 usbi_mutex_static_unlock(&active_contexts_lock);
1099 #if !defined(HAVE_LIBUDEV)
1100 static int parse_u8(const char *str, uint8_t *val_p)
1106 num = strtol(str, &endptr, 10);
1107 if (num < 0 || num > UINT8_MAX || errno)
1109 if (endptr == str || *endptr != '\0')
1112 *val_p = (uint8_t)num;
1116 /* open a bus directory and adds all discovered devices to the context */
1117 static int usbfs_scan_busdir(struct libusb_context *ctx, uint8_t busnum)
1121 struct dirent *entry;
1122 int r = LIBUSB_ERROR_IO;
1124 sprintf(dirpath, USB_DEVTMPFS_PATH "/%03u", busnum);
1125 usbi_dbg("%s", dirpath);
1126 dir = opendir(dirpath);
1128 usbi_err(ctx, "opendir '%s' failed, errno=%d", dirpath, errno);
1129 /* FIXME: should handle valid race conditions like hub unplugged
1130 * during directory iteration - this is not an error */
1134 while ((entry = readdir(dir))) {
1137 if (entry->d_name[0] == '.')
1140 if (!parse_u8(entry->d_name, &devaddr)) {
1141 usbi_dbg("unknown dir entry %s", entry->d_name);
1145 if (linux_enumerate_device(ctx, busnum, devaddr, NULL)) {
1146 usbi_dbg("failed to enumerate dir entry %s", entry->d_name);
1157 static int usbfs_get_device_list(struct libusb_context *ctx)
1159 struct dirent *entry;
1161 uint8_t busnum, devaddr;
1165 buses = opendir(USBDEV_PATH);
1167 buses = opendir(USB_DEVTMPFS_PATH);
1170 usbi_err(ctx, "opendir buses failed, errno=%d", errno);
1171 return LIBUSB_ERROR_IO;
1174 while ((entry = readdir(buses))) {
1175 if (entry->d_name[0] == '.')
1179 if (!is_usbdev_entry(entry->d_name, &busnum, &devaddr))
1182 r = linux_enumerate_device(ctx, busnum, devaddr, NULL);
1184 usbi_dbg("failed to enumerate dir entry %s", entry->d_name);
1188 if (!parse_u8(entry->d_name, &busnum)) {
1189 usbi_dbg("unknown dir entry %s", entry->d_name);
1193 r = usbfs_scan_busdir(ctx, busnum);
1204 static int sysfs_get_device_list(struct libusb_context *ctx)
1206 DIR *devices = opendir(SYSFS_DEVICE_PATH);
1207 struct dirent *entry;
1208 int num_devices = 0;
1209 int num_enumerated = 0;
1212 usbi_err(ctx, "opendir devices failed, errno=%d", errno);
1213 return LIBUSB_ERROR_IO;
1216 while ((entry = readdir(devices))) {
1217 if ((!isdigit(entry->d_name[0]) && strncmp(entry->d_name, "usb", 3))
1218 || strchr(entry->d_name, ':'))
1223 if (sysfs_scan_device(ctx, entry->d_name)) {
1224 usbi_dbg("failed to enumerate dir entry %s", entry->d_name);
1233 /* successful if at least one device was enumerated or no devices were found */
1234 if (num_enumerated || !num_devices)
1235 return LIBUSB_SUCCESS;
1237 return LIBUSB_ERROR_IO;
1240 static int linux_default_scan_devices(struct libusb_context *ctx)
1242 /* we can retrieve device list and descriptors from sysfs or usbfs.
1243 * sysfs is preferable, because if we use usbfs we end up resuming
1244 * any autosuspended USB devices. however, sysfs is not available
1245 * everywhere, so we need a usbfs fallback too.
1247 if (sysfs_available)
1248 return sysfs_get_device_list(ctx);
1250 return usbfs_get_device_list(ctx);
1254 static int initialize_handle(struct libusb_device_handle *handle, int fd)
1256 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1261 r = ioctl(fd, IOCTL_USBFS_GET_CAPABILITIES, &hpriv->caps);
1263 if (errno == ENOTTY)
1264 usbi_dbg("getcap not available");
1266 usbi_err(HANDLE_CTX(handle), "getcap failed, errno=%d", errno);
1267 hpriv->caps = USBFS_CAP_BULK_CONTINUATION;
1270 return usbi_add_event_source(HANDLE_CTX(handle), hpriv->fd, POLLOUT);
1273 static int op_wrap_sys_device(struct libusb_context *ctx,
1274 struct libusb_device_handle *handle, intptr_t sys_dev)
1276 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1277 int fd = (int)sys_dev;
1278 uint8_t busnum, devaddr;
1279 struct usbfs_connectinfo ci;
1280 struct libusb_device *dev;
1283 r = linux_get_device_address(ctx, 1, &busnum, &devaddr, NULL, NULL, fd);
1285 r = ioctl(fd, IOCTL_USBFS_CONNECTINFO, &ci);
1287 usbi_err(ctx, "connectinfo failed, errno=%d", errno);
1288 return LIBUSB_ERROR_IO;
1290 /* There is no ioctl to get the bus number. We choose 0 here
1291 * as linux starts numbering buses from 1. */
1293 devaddr = ci.devnum;
1296 /* Session id is unused as we do not add the device to the list of
1297 * connected devices. */
1298 usbi_dbg("allocating new device for fd %d", fd);
1299 dev = usbi_alloc_device(ctx, 0);
1301 return LIBUSB_ERROR_NO_MEM;
1303 r = initialize_device(dev, busnum, devaddr, NULL, fd);
1306 r = usbi_sanitize_device(dev);
1309 /* Consider the device as connected, but do not add it to the managed
1314 r = initialize_handle(handle, fd);
1319 libusb_unref_device(dev);
1323 static int op_open(struct libusb_device_handle *handle)
1327 fd = get_usbfs_fd(handle->dev, O_RDWR, 0);
1329 if (fd == LIBUSB_ERROR_NO_DEVICE) {
1330 /* device will still be marked as attached if hotplug monitor thread
1331 * hasn't processed remove event yet */
1332 usbi_mutex_static_lock(&linux_hotplug_lock);
1333 if (handle->dev->attached) {
1334 usbi_dbg("open failed with no device, but device still attached");
1335 linux_device_disconnected(handle->dev->bus_number,
1336 handle->dev->device_address);
1338 usbi_mutex_static_unlock(&linux_hotplug_lock);
1343 r = initialize_handle(handle, fd);
1350 static void op_close(struct libusb_device_handle *dev_handle)
1352 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(dev_handle);
1354 /* fd may have already been removed by POLLERR condition in op_handle_events() */
1355 if (!hpriv->fd_removed)
1356 usbi_remove_event_source(HANDLE_CTX(dev_handle), hpriv->fd);
1357 if (!hpriv->fd_keep)
1361 static int op_get_configuration(struct libusb_device_handle *handle,
1364 struct linux_device_priv *priv = usbi_get_device_priv(handle->dev);
1367 if (priv->sysfs_dir) {
1368 r = sysfs_get_active_config(handle->dev, config);
1370 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1372 r = usbfs_get_active_config(handle->dev, hpriv->fd);
1373 if (r == LIBUSB_SUCCESS)
1374 *config = priv->active_config;
1380 usbi_err(HANDLE_CTX(handle), "device unconfigured");
1385 static int op_set_configuration(struct libusb_device_handle *handle, int config)
1387 struct linux_device_priv *priv = usbi_get_device_priv(handle->dev);
1388 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1390 int r = ioctl(fd, IOCTL_USBFS_SETCONFIGURATION, &config);
1393 if (errno == EINVAL)
1394 return LIBUSB_ERROR_NOT_FOUND;
1395 else if (errno == EBUSY)
1396 return LIBUSB_ERROR_BUSY;
1397 else if (errno == ENODEV)
1398 return LIBUSB_ERROR_NO_DEVICE;
1400 usbi_err(HANDLE_CTX(handle), "set configuration failed, errno=%d", errno);
1401 return LIBUSB_ERROR_OTHER;
1407 /* update our cached active config descriptor */
1408 priv->active_config = (uint8_t)config;
1410 return LIBUSB_SUCCESS;
1413 static int claim_interface(struct libusb_device_handle *handle, unsigned int iface)
1415 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1417 int r = ioctl(fd, IOCTL_USBFS_CLAIMINTERFACE, &iface);
1420 if (errno == ENOENT)
1421 return LIBUSB_ERROR_NOT_FOUND;
1422 else if (errno == EBUSY)
1423 return LIBUSB_ERROR_BUSY;
1424 else if (errno == ENODEV)
1425 return LIBUSB_ERROR_NO_DEVICE;
1427 usbi_err(HANDLE_CTX(handle), "claim interface failed, errno=%d", errno);
1428 return LIBUSB_ERROR_OTHER;
1433 static int release_interface(struct libusb_device_handle *handle, unsigned int iface)
1435 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1437 int r = ioctl(fd, IOCTL_USBFS_RELEASEINTERFACE, &iface);
1440 if (errno == ENODEV)
1441 return LIBUSB_ERROR_NO_DEVICE;
1443 usbi_err(HANDLE_CTX(handle), "release interface failed, errno=%d", errno);
1444 return LIBUSB_ERROR_OTHER;
1449 static int op_set_interface(struct libusb_device_handle *handle, uint8_t interface,
1452 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1454 struct usbfs_setinterface setintf;
1457 setintf.interface = interface;
1458 setintf.altsetting = altsetting;
1459 r = ioctl(fd, IOCTL_USBFS_SETINTERFACE, &setintf);
1461 if (errno == EINVAL)
1462 return LIBUSB_ERROR_NOT_FOUND;
1463 else if (errno == ENODEV)
1464 return LIBUSB_ERROR_NO_DEVICE;
1466 usbi_err(HANDLE_CTX(handle), "set interface failed, errno=%d", errno);
1467 return LIBUSB_ERROR_OTHER;
1473 static int op_clear_halt(struct libusb_device_handle *handle,
1474 unsigned char endpoint)
1476 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1478 unsigned int _endpoint = endpoint;
1479 int r = ioctl(fd, IOCTL_USBFS_CLEAR_HALT, &_endpoint);
1482 if (errno == ENOENT)
1483 return LIBUSB_ERROR_NOT_FOUND;
1484 else if (errno == ENODEV)
1485 return LIBUSB_ERROR_NO_DEVICE;
1487 usbi_err(HANDLE_CTX(handle), "clear halt failed, errno=%d", errno);
1488 return LIBUSB_ERROR_OTHER;
1494 static int op_reset_device(struct libusb_device_handle *handle)
1496 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1501 /* Doing a device reset will cause the usbfs driver to get unbound
1502 * from any interfaces it is bound to. By voluntarily unbinding
1503 * the usbfs driver ourself, we stop the kernel from rebinding
1504 * the interface after reset (which would end up with the interface
1505 * getting bound to the in kernel driver if any). */
1506 for (i = 0; i < USB_MAXINTERFACES; i++) {
1507 if (handle->claimed_interfaces & (1UL << i))
1508 release_interface(handle, i);
1511 usbi_mutex_lock(&handle->lock);
1512 r = ioctl(fd, IOCTL_USBFS_RESET, NULL);
1514 if (errno == ENODEV) {
1515 ret = LIBUSB_ERROR_NOT_FOUND;
1519 usbi_err(HANDLE_CTX(handle), "reset failed, errno=%d", errno);
1520 ret = LIBUSB_ERROR_OTHER;
1524 /* And re-claim any interfaces which were claimed before the reset */
1525 for (i = 0; i < USB_MAXINTERFACES; i++) {
1526 if (!(handle->claimed_interfaces & (1UL << i)))
1529 * A driver may have completed modprobing during
1530 * IOCTL_USBFS_RESET, and bound itself as soon as
1531 * IOCTL_USBFS_RESET released the device lock
1533 r = detach_kernel_driver_and_claim(handle, i);
1535 usbi_warn(HANDLE_CTX(handle), "failed to re-claim interface %u after reset: %s",
1536 i, libusb_error_name(r));
1537 handle->claimed_interfaces &= ~(1UL << i);
1538 ret = LIBUSB_ERROR_NOT_FOUND;
1542 usbi_mutex_unlock(&handle->lock);
1546 static int do_streams_ioctl(struct libusb_device_handle *handle, long req,
1547 uint32_t num_streams, unsigned char *endpoints, int num_endpoints)
1549 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1550 int r, fd = hpriv->fd;
1551 struct usbfs_streams *streams;
1553 if (num_endpoints > 30) /* Max 15 in + 15 out eps */
1554 return LIBUSB_ERROR_INVALID_PARAM;
1556 streams = malloc(sizeof(*streams) + num_endpoints);
1558 return LIBUSB_ERROR_NO_MEM;
1560 streams->num_streams = num_streams;
1561 streams->num_eps = num_endpoints;
1562 memcpy(streams->eps, endpoints, num_endpoints);
1564 r = ioctl(fd, req, streams);
1569 if (errno == ENOTTY)
1570 return LIBUSB_ERROR_NOT_SUPPORTED;
1571 else if (errno == EINVAL)
1572 return LIBUSB_ERROR_INVALID_PARAM;
1573 else if (errno == ENODEV)
1574 return LIBUSB_ERROR_NO_DEVICE;
1576 usbi_err(HANDLE_CTX(handle), "streams-ioctl failed, errno=%d", errno);
1577 return LIBUSB_ERROR_OTHER;
1582 static int op_alloc_streams(struct libusb_device_handle *handle,
1583 uint32_t num_streams, unsigned char *endpoints, int num_endpoints)
1585 return do_streams_ioctl(handle, IOCTL_USBFS_ALLOC_STREAMS,
1586 num_streams, endpoints, num_endpoints);
1589 static int op_free_streams(struct libusb_device_handle *handle,
1590 unsigned char *endpoints, int num_endpoints)
1592 return do_streams_ioctl(handle, IOCTL_USBFS_FREE_STREAMS, 0,
1593 endpoints, num_endpoints);
1596 static void *op_dev_mem_alloc(struct libusb_device_handle *handle, size_t len)
1598 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1601 buffer = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, hpriv->fd, 0);
1602 if (buffer == MAP_FAILED) {
1603 usbi_err(HANDLE_CTX(handle), "alloc dev mem failed, errno=%d", errno);
1609 static int op_dev_mem_free(struct libusb_device_handle *handle, void *buffer,
1612 if (munmap(buffer, len) != 0) {
1613 usbi_err(HANDLE_CTX(handle), "free dev mem failed, errno=%d", errno);
1614 return LIBUSB_ERROR_OTHER;
1616 return LIBUSB_SUCCESS;
1620 static int op_kernel_driver_active(struct libusb_device_handle *handle,
1623 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1625 struct usbfs_getdriver getdrv;
1628 getdrv.interface = interface;
1629 r = ioctl(fd, IOCTL_USBFS_GETDRIVER, &getdrv);
1631 if (errno == ENODATA)
1633 else if (errno == ENODEV)
1634 return LIBUSB_ERROR_NO_DEVICE;
1636 usbi_err(HANDLE_CTX(handle), "get driver failed, errno=%d", errno);
1637 return LIBUSB_ERROR_OTHER;
1640 return strcmp(getdrv.driver, "usbfs") != 0;
1643 static int op_detach_kernel_driver(struct libusb_device_handle *handle,
1646 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1648 struct usbfs_ioctl command;
1649 struct usbfs_getdriver getdrv;
1652 command.ifno = interface;
1653 command.ioctl_code = IOCTL_USBFS_DISCONNECT;
1654 command.data = NULL;
1656 getdrv.interface = interface;
1657 r = ioctl(fd, IOCTL_USBFS_GETDRIVER, &getdrv);
1658 if (r == 0 && !strcmp(getdrv.driver, "usbfs"))
1659 return LIBUSB_ERROR_NOT_FOUND;
1661 r = ioctl(fd, IOCTL_USBFS_IOCTL, &command);
1663 if (errno == ENODATA)
1664 return LIBUSB_ERROR_NOT_FOUND;
1665 else if (errno == EINVAL)
1666 return LIBUSB_ERROR_INVALID_PARAM;
1667 else if (errno == ENODEV)
1668 return LIBUSB_ERROR_NO_DEVICE;
1670 usbi_err(HANDLE_CTX(handle), "detach failed, errno=%d", errno);
1671 return LIBUSB_ERROR_OTHER;
1677 static int op_attach_kernel_driver(struct libusb_device_handle *handle,
1680 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1682 struct usbfs_ioctl command;
1685 command.ifno = interface;
1686 command.ioctl_code = IOCTL_USBFS_CONNECT;
1687 command.data = NULL;
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;
1697 else if (errno == EBUSY)
1698 return LIBUSB_ERROR_BUSY;
1700 usbi_err(HANDLE_CTX(handle), "attach failed, errno=%d", errno);
1701 return LIBUSB_ERROR_OTHER;
1702 } else if (r == 0) {
1703 return LIBUSB_ERROR_NOT_FOUND;
1709 static int detach_kernel_driver_and_claim(struct libusb_device_handle *handle,
1712 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1713 struct usbfs_disconnect_claim dc;
1714 int r, fd = hpriv->fd;
1716 dc.interface = interface;
1717 strcpy(dc.driver, "usbfs");
1718 dc.flags = USBFS_DISCONNECT_CLAIM_EXCEPT_DRIVER;
1719 r = ioctl(fd, IOCTL_USBFS_DISCONNECT_CLAIM, &dc);
1726 return LIBUSB_ERROR_BUSY;
1728 return LIBUSB_ERROR_INVALID_PARAM;
1730 return LIBUSB_ERROR_NO_DEVICE;
1732 usbi_err(HANDLE_CTX(handle), "disconnect-and-claim failed, errno=%d", errno);
1733 return LIBUSB_ERROR_OTHER;
1736 /* Fallback code for kernels which don't support the
1737 disconnect-and-claim ioctl */
1738 r = op_detach_kernel_driver(handle, interface);
1739 if (r != 0 && r != LIBUSB_ERROR_NOT_FOUND)
1742 return claim_interface(handle, interface);
1745 static int op_claim_interface(struct libusb_device_handle *handle, uint8_t interface)
1747 if (handle->auto_detach_kernel_driver)
1748 return detach_kernel_driver_and_claim(handle, interface);
1750 return claim_interface(handle, interface);
1753 static int op_release_interface(struct libusb_device_handle *handle, uint8_t interface)
1757 r = release_interface(handle, interface);
1761 if (handle->auto_detach_kernel_driver)
1762 op_attach_kernel_driver(handle, interface);
1767 static void op_destroy_device(struct libusb_device *dev)
1769 struct linux_device_priv *priv = usbi_get_device_priv(dev);
1771 free(priv->config_descriptors);
1772 free(priv->descriptors);
1773 free(priv->sysfs_dir);
1776 /* URBs are discarded in reverse order of submission to avoid races. */
1777 static int discard_urbs(struct usbi_transfer *itransfer, int first, int last_plus_one)
1779 struct libusb_transfer *transfer =
1780 USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
1781 struct linux_transfer_priv *tpriv = usbi_get_transfer_priv(itransfer);
1782 struct linux_device_handle_priv *hpriv =
1783 usbi_get_device_handle_priv(transfer->dev_handle);
1785 struct usbfs_urb *urb;
1787 for (i = last_plus_one - 1; i >= first; i--) {
1788 if (transfer->type == LIBUSB_TRANSFER_TYPE_ISOCHRONOUS)
1789 urb = tpriv->iso_urbs[i];
1791 urb = &tpriv->urbs[i];
1793 if (ioctl(hpriv->fd, IOCTL_USBFS_DISCARDURB, urb) == 0)
1796 if (errno == EINVAL) {
1797 usbi_dbg("URB not found --> assuming ready to be reaped");
1798 if (i == (last_plus_one - 1))
1799 ret = LIBUSB_ERROR_NOT_FOUND;
1800 } else if (errno == ENODEV) {
1801 usbi_dbg("Device not found for URB --> assuming ready to be reaped");
1802 ret = LIBUSB_ERROR_NO_DEVICE;
1804 usbi_warn(TRANSFER_CTX(transfer), "unrecognised discard errno %d", errno);
1805 ret = LIBUSB_ERROR_OTHER;
1811 static void free_iso_urbs(struct linux_transfer_priv *tpriv)
1815 for (i = 0; i < tpriv->num_urbs; i++) {
1816 struct usbfs_urb *urb = tpriv->iso_urbs[i];
1823 free(tpriv->iso_urbs);
1824 tpriv->iso_urbs = NULL;
1827 static int submit_bulk_transfer(struct usbi_transfer *itransfer)
1829 struct libusb_transfer *transfer =
1830 USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
1831 struct linux_transfer_priv *tpriv = usbi_get_transfer_priv(itransfer);
1832 struct linux_device_handle_priv *hpriv =
1833 usbi_get_device_handle_priv(transfer->dev_handle);
1834 struct usbfs_urb *urbs;
1835 int is_out = IS_XFEROUT(transfer);
1836 int bulk_buffer_len, use_bulk_continuation;
1838 int last_urb_partial = 0;
1843 * Older versions of usbfs place a 16kb limit on bulk URBs. We work
1844 * around this by splitting large transfers into 16k blocks, and then
1845 * submit all urbs at once. it would be simpler to submit one urb at
1846 * a time, but there is a big performance gain doing it this way.
1848 * Newer versions lift the 16k limit (USBFS_CAP_NO_PACKET_SIZE_LIM),
1849 * using arbitrary large transfers can still be a bad idea though, as
1850 * the kernel needs to allocate physical contiguous memory for this,
1851 * which may fail for large buffers.
1853 * The kernel solves this problem by splitting the transfer into
1854 * blocks itself when the host-controller is scatter-gather capable
1855 * (USBFS_CAP_BULK_SCATTER_GATHER), which most controllers are.
1857 * Last, there is the issue of short-transfers when splitting, for
1858 * short split-transfers to work reliable USBFS_CAP_BULK_CONTINUATION
1859 * is needed, but this is not always available.
1861 if (hpriv->caps & USBFS_CAP_BULK_SCATTER_GATHER) {
1862 /* Good! Just submit everything in one go */
1863 bulk_buffer_len = transfer->length ? transfer->length : 1;
1864 use_bulk_continuation = 0;
1865 } else if (hpriv->caps & USBFS_CAP_BULK_CONTINUATION) {
1866 /* Split the transfers and use bulk-continuation to
1867 avoid issues with short-transfers */
1868 bulk_buffer_len = MAX_BULK_BUFFER_LENGTH;
1869 use_bulk_continuation = 1;
1870 } else if (hpriv->caps & USBFS_CAP_NO_PACKET_SIZE_LIM) {
1871 /* Don't split, assume the kernel can alloc the buffer
1872 (otherwise the submit will fail with -ENOMEM) */
1873 bulk_buffer_len = transfer->length ? transfer->length : 1;
1874 use_bulk_continuation = 0;
1876 /* Bad, splitting without bulk-continuation, short transfers
1877 which end before the last urb will not work reliable! */
1878 /* Note we don't warn here as this is "normal" on kernels <
1879 2.6.32 and not a problem for most applications */
1880 bulk_buffer_len = MAX_BULK_BUFFER_LENGTH;
1881 use_bulk_continuation = 0;
1884 num_urbs = transfer->length / bulk_buffer_len;
1886 if (transfer->length == 0) {
1888 } else if ((transfer->length % bulk_buffer_len) > 0) {
1889 last_urb_partial = 1;
1892 usbi_dbg("need %d urbs for new transfer with length %d", num_urbs, transfer->length);
1893 urbs = calloc(num_urbs, sizeof(*urbs));
1895 return LIBUSB_ERROR_NO_MEM;
1897 tpriv->num_urbs = num_urbs;
1898 tpriv->num_retired = 0;
1899 tpriv->reap_action = NORMAL;
1900 tpriv->reap_status = LIBUSB_TRANSFER_COMPLETED;
1902 for (i = 0; i < num_urbs; i++) {
1903 struct usbfs_urb *urb = &urbs[i];
1905 urb->usercontext = itransfer;
1906 switch (transfer->type) {
1907 case LIBUSB_TRANSFER_TYPE_BULK:
1908 urb->type = USBFS_URB_TYPE_BULK;
1911 case LIBUSB_TRANSFER_TYPE_BULK_STREAM:
1912 urb->type = USBFS_URB_TYPE_BULK;
1913 urb->stream_id = itransfer->stream_id;
1915 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1916 urb->type = USBFS_URB_TYPE_INTERRUPT;
1919 urb->endpoint = transfer->endpoint;
1920 urb->buffer = transfer->buffer + (i * bulk_buffer_len);
1922 /* don't set the short not ok flag for the last URB */
1923 if (use_bulk_continuation && !is_out && (i < num_urbs - 1))
1924 urb->flags = USBFS_URB_SHORT_NOT_OK;
1926 if (i == num_urbs - 1 && last_urb_partial)
1927 urb->buffer_length = transfer->length % bulk_buffer_len;
1928 else if (transfer->length == 0)
1929 urb->buffer_length = 0;
1931 urb->buffer_length = bulk_buffer_len;
1933 if (i > 0 && use_bulk_continuation)
1934 urb->flags |= USBFS_URB_BULK_CONTINUATION;
1936 /* we have already checked that the flag is supported */
1937 if (is_out && i == num_urbs - 1 &&
1938 (transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET))
1939 urb->flags |= USBFS_URB_ZERO_PACKET;
1941 r = ioctl(hpriv->fd, IOCTL_USBFS_SUBMITURB, urb);
1945 if (errno == ENODEV) {
1946 r = LIBUSB_ERROR_NO_DEVICE;
1947 } else if (errno == ENOMEM) {
1948 r = LIBUSB_ERROR_NO_MEM;
1950 usbi_err(TRANSFER_CTX(transfer), "submiturb failed, errno=%d", errno);
1951 r = LIBUSB_ERROR_IO;
1954 /* if the first URB submission fails, we can simply free up and
1955 * return failure immediately. */
1957 usbi_dbg("first URB failed, easy peasy");
1963 /* if it's not the first URB that failed, the situation is a bit
1964 * tricky. we may need to discard all previous URBs. there are
1966 * - discarding is asynchronous - discarded urbs will be reaped
1967 * later. the user must not have freed the transfer when the
1968 * discarded URBs are reaped, otherwise libusb will be using
1970 * - the earlier URBs may have completed successfully and we do
1971 * not want to throw away any data.
1972 * - this URB failing may be no error; EREMOTEIO means that
1973 * this transfer simply didn't need all the URBs we submitted
1974 * so, we report that the transfer was submitted successfully and
1975 * in case of error we discard all previous URBs. later when
1976 * the final reap completes we can report error to the user,
1977 * or success if an earlier URB was completed successfully.
1979 tpriv->reap_action = errno == EREMOTEIO ? COMPLETED_EARLY : SUBMIT_FAILED;
1981 /* The URBs we haven't submitted yet we count as already
1983 tpriv->num_retired += num_urbs - i;
1985 /* If we completed short then don't try to discard. */
1986 if (tpriv->reap_action == COMPLETED_EARLY)
1989 discard_urbs(itransfer, 0, i);
1991 usbi_dbg("reporting successful submission but waiting for %d "
1992 "discards before reporting error", i);
1999 static int submit_iso_transfer(struct usbi_transfer *itransfer)
2001 struct libusb_transfer *transfer =
2002 USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
2003 struct linux_transfer_priv *tpriv = usbi_get_transfer_priv(itransfer);
2004 struct linux_device_handle_priv *hpriv =
2005 usbi_get_device_handle_priv(transfer->dev_handle);
2006 struct usbfs_urb **urbs;
2007 int num_packets = transfer->num_iso_packets;
2008 int num_packets_remaining;
2011 unsigned int packet_len;
2012 unsigned int total_len = 0;
2013 unsigned char *urb_buffer = transfer->buffer;
2015 if (num_packets < 1)
2016 return LIBUSB_ERROR_INVALID_PARAM;
2018 /* usbfs places arbitrary limits on iso URBs. this limit has changed
2019 * at least three times, but we attempt to detect this limit during
2020 * init and check it here. if the kernel rejects the request due to
2021 * its size, we return an error indicating such to the user.
2023 for (i = 0; i < num_packets; i++) {
2024 packet_len = transfer->iso_packet_desc[i].length;
2026 if (packet_len > max_iso_packet_len) {
2027 usbi_warn(TRANSFER_CTX(transfer),
2028 "iso packet length of %u bytes exceeds maximum of %u bytes",
2029 packet_len, max_iso_packet_len);
2030 return LIBUSB_ERROR_INVALID_PARAM;
2033 total_len += packet_len;
2036 if (transfer->length < (int)total_len)
2037 return LIBUSB_ERROR_INVALID_PARAM;
2039 /* usbfs limits the number of iso packets per URB */
2040 num_urbs = (num_packets + (MAX_ISO_PACKETS_PER_URB - 1)) / MAX_ISO_PACKETS_PER_URB;
2042 usbi_dbg("need %d urbs for new transfer with length %d", num_urbs, transfer->length);
2044 urbs = calloc(num_urbs, sizeof(*urbs));
2046 return LIBUSB_ERROR_NO_MEM;
2048 tpriv->iso_urbs = urbs;
2049 tpriv->num_urbs = num_urbs;
2050 tpriv->num_retired = 0;
2051 tpriv->reap_action = NORMAL;
2052 tpriv->iso_packet_offset = 0;
2054 /* allocate + initialize each URB with the correct number of packets */
2055 num_packets_remaining = num_packets;
2056 for (i = 0, j = 0; i < num_urbs; i++) {
2057 int num_packets_in_urb = MIN(num_packets_remaining, MAX_ISO_PACKETS_PER_URB);
2058 struct usbfs_urb *urb;
2062 alloc_size = sizeof(*urb)
2063 + (num_packets_in_urb * sizeof(struct usbfs_iso_packet_desc));
2064 urb = calloc(1, alloc_size);
2066 free_iso_urbs(tpriv);
2067 return LIBUSB_ERROR_NO_MEM;
2071 /* populate packet lengths */
2072 for (k = 0; k < num_packets_in_urb; j++, k++) {
2073 packet_len = transfer->iso_packet_desc[j].length;
2074 urb->buffer_length += packet_len;
2075 urb->iso_frame_desc[k].length = packet_len;
2078 urb->usercontext = itransfer;
2079 urb->type = USBFS_URB_TYPE_ISO;
2080 /* FIXME: interface for non-ASAP data? */
2081 urb->flags = USBFS_URB_ISO_ASAP;
2082 urb->endpoint = transfer->endpoint;
2083 urb->number_of_packets = num_packets_in_urb;
2084 urb->buffer = urb_buffer;
2086 urb_buffer += urb->buffer_length;
2087 num_packets_remaining -= num_packets_in_urb;
2091 for (i = 0; i < num_urbs; i++) {
2092 int r = ioctl(hpriv->fd, IOCTL_USBFS_SUBMITURB, urbs[i]);
2097 if (errno == ENODEV) {
2098 r = LIBUSB_ERROR_NO_DEVICE;
2099 } else if (errno == EINVAL) {
2100 usbi_warn(TRANSFER_CTX(transfer), "submiturb failed, transfer too large");
2101 r = LIBUSB_ERROR_INVALID_PARAM;
2102 } else if (errno == EMSGSIZE) {
2103 usbi_warn(TRANSFER_CTX(transfer), "submiturb failed, iso packet length too large");
2104 r = LIBUSB_ERROR_INVALID_PARAM;
2106 usbi_err(TRANSFER_CTX(transfer), "submiturb failed, errno=%d", errno);
2107 r = LIBUSB_ERROR_IO;
2110 /* if the first URB submission fails, we can simply free up and
2111 * return failure immediately. */
2113 usbi_dbg("first URB failed, easy peasy");
2114 free_iso_urbs(tpriv);
2118 /* if it's not the first URB that failed, the situation is a bit
2119 * tricky. we must discard all previous URBs. there are
2121 * - discarding is asynchronous - discarded urbs will be reaped
2122 * later. the user must not have freed the transfer when the
2123 * discarded URBs are reaped, otherwise libusb will be using
2125 * - the earlier URBs may have completed successfully and we do
2126 * not want to throw away any data.
2127 * so, in this case we discard all the previous URBs BUT we report
2128 * that the transfer was submitted successfully. then later when
2129 * the final discard completes we can report error to the user.
2131 tpriv->reap_action = SUBMIT_FAILED;
2133 /* The URBs we haven't submitted yet we count as already
2135 tpriv->num_retired = num_urbs - i;
2136 discard_urbs(itransfer, 0, i);
2138 usbi_dbg("reporting successful submission but waiting for %d "
2139 "discards before reporting error", i);
2146 static int submit_control_transfer(struct usbi_transfer *itransfer)
2148 struct linux_transfer_priv *tpriv = usbi_get_transfer_priv(itransfer);
2149 struct libusb_transfer *transfer =
2150 USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
2151 struct linux_device_handle_priv *hpriv =
2152 usbi_get_device_handle_priv(transfer->dev_handle);
2153 struct usbfs_urb *urb;
2156 if (transfer->length - LIBUSB_CONTROL_SETUP_SIZE > MAX_CTRL_BUFFER_LENGTH)
2157 return LIBUSB_ERROR_INVALID_PARAM;
2159 urb = calloc(1, sizeof(*urb));
2161 return LIBUSB_ERROR_NO_MEM;
2163 tpriv->num_urbs = 1;
2164 tpriv->reap_action = NORMAL;
2166 urb->usercontext = itransfer;
2167 urb->type = USBFS_URB_TYPE_CONTROL;
2168 urb->endpoint = transfer->endpoint;
2169 urb->buffer = transfer->buffer;
2170 urb->buffer_length = transfer->length;
2172 r = ioctl(hpriv->fd, IOCTL_USBFS_SUBMITURB, urb);
2176 if (errno == ENODEV)
2177 return LIBUSB_ERROR_NO_DEVICE;
2179 usbi_err(TRANSFER_CTX(transfer), "submiturb failed, errno=%d", errno);
2180 return LIBUSB_ERROR_IO;
2185 static int op_submit_transfer(struct usbi_transfer *itransfer)
2187 struct libusb_transfer *transfer =
2188 USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
2190 switch (transfer->type) {
2191 case LIBUSB_TRANSFER_TYPE_CONTROL:
2192 return submit_control_transfer(itransfer);
2193 case LIBUSB_TRANSFER_TYPE_BULK:
2194 case LIBUSB_TRANSFER_TYPE_BULK_STREAM:
2195 return submit_bulk_transfer(itransfer);
2196 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
2197 return submit_bulk_transfer(itransfer);
2198 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
2199 return submit_iso_transfer(itransfer);
2201 usbi_err(TRANSFER_CTX(transfer), "unknown transfer type %u", transfer->type);
2202 return LIBUSB_ERROR_INVALID_PARAM;
2206 static int op_cancel_transfer(struct usbi_transfer *itransfer)
2208 struct linux_transfer_priv *tpriv = usbi_get_transfer_priv(itransfer);
2209 struct libusb_transfer *transfer =
2210 USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
2214 return LIBUSB_ERROR_NOT_FOUND;
2216 r = discard_urbs(itransfer, 0, tpriv->num_urbs);
2220 switch (transfer->type) {
2221 case LIBUSB_TRANSFER_TYPE_BULK:
2222 case LIBUSB_TRANSFER_TYPE_BULK_STREAM:
2223 if (tpriv->reap_action == ERROR)
2225 /* else, fall through */
2227 tpriv->reap_action = CANCELLED;
2233 static void op_clear_transfer_priv(struct usbi_transfer *itransfer)
2235 struct libusb_transfer *transfer =
2236 USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
2237 struct linux_transfer_priv *tpriv = usbi_get_transfer_priv(itransfer);
2239 switch (transfer->type) {
2240 case LIBUSB_TRANSFER_TYPE_CONTROL:
2241 case LIBUSB_TRANSFER_TYPE_BULK:
2242 case LIBUSB_TRANSFER_TYPE_BULK_STREAM:
2243 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
2249 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
2250 if (tpriv->iso_urbs) {
2251 free_iso_urbs(tpriv);
2252 tpriv->iso_urbs = NULL;
2256 usbi_err(TRANSFER_CTX(transfer), "unknown transfer type %u", transfer->type);
2260 static int handle_bulk_completion(struct usbi_transfer *itransfer,
2261 struct usbfs_urb *urb)
2263 struct linux_transfer_priv *tpriv = usbi_get_transfer_priv(itransfer);
2264 struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
2265 int urb_idx = urb - tpriv->urbs;
2267 usbi_mutex_lock(&itransfer->lock);
2268 usbi_dbg("handling completion status %d of bulk urb %d/%d", urb->status,
2269 urb_idx + 1, tpriv->num_urbs);
2271 tpriv->num_retired++;
2273 if (tpriv->reap_action != NORMAL) {
2274 /* cancelled, submit_fail, or completed early */
2275 usbi_dbg("abnormal reap: urb status %d", urb->status);
2277 /* even though we're in the process of cancelling, it's possible that
2278 * we may receive some data in these URBs that we don't want to lose.
2280 * 1. while the kernel is cancelling all the packets that make up an
2281 * URB, a few of them might complete. so we get back a successful
2282 * cancellation *and* some data.
2283 * 2. we receive a short URB which marks the early completion condition,
2284 * so we start cancelling the remaining URBs. however, we're too
2285 * slow and another URB completes (or at least completes partially).
2286 * (this can't happen since we always use BULK_CONTINUATION.)
2288 * When this happens, our objectives are not to lose any "surplus" data,
2289 * and also to stick it at the end of the previously-received data
2290 * (closing any holes), so that libusb reports the total amount of
2291 * transferred data and presents it in a contiguous chunk.
2293 if (urb->actual_length > 0) {
2294 unsigned char *target = transfer->buffer + itransfer->transferred;
2296 usbi_dbg("received %d bytes of surplus data", urb->actual_length);
2297 if (urb->buffer != target) {
2298 usbi_dbg("moving surplus data from offset %zu to offset %zu",
2299 (unsigned char *)urb->buffer - transfer->buffer,
2300 target - transfer->buffer);
2301 memmove(target, urb->buffer, urb->actual_length);
2303 itransfer->transferred += urb->actual_length;
2306 if (tpriv->num_retired == tpriv->num_urbs) {
2307 usbi_dbg("abnormal reap: last URB handled, reporting");
2308 if (tpriv->reap_action != COMPLETED_EARLY &&
2309 tpriv->reap_status == LIBUSB_TRANSFER_COMPLETED)
2310 tpriv->reap_status = LIBUSB_TRANSFER_ERROR;
2316 itransfer->transferred += urb->actual_length;
2318 /* Many of these errors can occur on *any* urb of a multi-urb
2319 * transfer. When they do, we tear down the rest of the transfer.
2321 switch (urb->status) {
2324 case -EREMOTEIO: /* short transfer */
2326 case -ENOENT: /* cancelled */
2331 usbi_dbg("device removed");
2332 tpriv->reap_status = LIBUSB_TRANSFER_NO_DEVICE;
2333 goto cancel_remaining;
2335 usbi_dbg("detected endpoint stall");
2336 if (tpriv->reap_status == LIBUSB_TRANSFER_COMPLETED)
2337 tpriv->reap_status = LIBUSB_TRANSFER_STALL;
2338 goto cancel_remaining;
2340 /* overflow can only ever occur in the last urb */
2341 usbi_dbg("overflow, actual_length=%d", urb->actual_length);
2342 if (tpriv->reap_status == LIBUSB_TRANSFER_COMPLETED)
2343 tpriv->reap_status = LIBUSB_TRANSFER_OVERFLOW;
2350 usbi_dbg("low-level bus error %d", urb->status);
2351 tpriv->reap_action = ERROR;
2352 goto cancel_remaining;
2354 usbi_warn(ITRANSFER_CTX(itransfer), "unrecognised urb status %d", urb->status);
2355 tpriv->reap_action = ERROR;
2356 goto cancel_remaining;
2359 /* if we've reaped all urbs or we got less data than requested then we're
2361 if (tpriv->num_retired == tpriv->num_urbs) {
2362 usbi_dbg("all URBs in transfer reaped --> complete!");
2364 } else if (urb->actual_length < urb->buffer_length) {
2365 usbi_dbg("short transfer %d/%d --> complete!",
2366 urb->actual_length, urb->buffer_length);
2367 if (tpriv->reap_action == NORMAL)
2368 tpriv->reap_action = COMPLETED_EARLY;
2374 if (tpriv->reap_action == ERROR && tpriv->reap_status == LIBUSB_TRANSFER_COMPLETED)
2375 tpriv->reap_status = LIBUSB_TRANSFER_ERROR;
2377 if (tpriv->num_retired == tpriv->num_urbs) /* nothing to cancel */
2380 /* cancel remaining urbs and wait for their completion before
2381 * reporting results */
2382 discard_urbs(itransfer, urb_idx + 1, tpriv->num_urbs);
2385 usbi_mutex_unlock(&itransfer->lock);
2391 usbi_mutex_unlock(&itransfer->lock);
2392 return tpriv->reap_action == CANCELLED ?
2393 usbi_handle_transfer_cancellation(itransfer) :
2394 usbi_handle_transfer_completion(itransfer, tpriv->reap_status);
2397 static int handle_iso_completion(struct usbi_transfer *itransfer,
2398 struct usbfs_urb *urb)
2400 struct libusb_transfer *transfer =
2401 USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
2402 struct linux_transfer_priv *tpriv = usbi_get_transfer_priv(itransfer);
2403 int num_urbs = tpriv->num_urbs;
2406 enum libusb_transfer_status status = LIBUSB_TRANSFER_COMPLETED;
2408 usbi_mutex_lock(&itransfer->lock);
2409 for (i = 0; i < num_urbs; i++) {
2410 if (urb == tpriv->iso_urbs[i]) {
2416 usbi_err(TRANSFER_CTX(transfer), "could not locate urb!");
2417 usbi_mutex_unlock(&itransfer->lock);
2418 return LIBUSB_ERROR_NOT_FOUND;
2421 usbi_dbg("handling completion status %d of iso urb %d/%d", urb->status,
2424 /* copy isochronous results back in */
2426 for (i = 0; i < urb->number_of_packets; i++) {
2427 struct usbfs_iso_packet_desc *urb_desc = &urb->iso_frame_desc[i];
2428 struct libusb_iso_packet_descriptor *lib_desc =
2429 &transfer->iso_packet_desc[tpriv->iso_packet_offset++];
2431 lib_desc->status = LIBUSB_TRANSFER_COMPLETED;
2432 switch (urb_desc->status) {
2435 case -ENOENT: /* cancelled */
2440 usbi_dbg("packet %d - device removed", i);
2441 lib_desc->status = LIBUSB_TRANSFER_NO_DEVICE;
2444 usbi_dbg("packet %d - detected endpoint stall", i);
2445 lib_desc->status = LIBUSB_TRANSFER_STALL;
2448 usbi_dbg("packet %d - overflow error", i);
2449 lib_desc->status = LIBUSB_TRANSFER_OVERFLOW;
2457 usbi_dbg("packet %d - low-level USB error %d", i, urb_desc->status);
2458 lib_desc->status = LIBUSB_TRANSFER_ERROR;
2461 usbi_warn(TRANSFER_CTX(transfer), "packet %d - unrecognised urb status %d",
2462 i, urb_desc->status);
2463 lib_desc->status = LIBUSB_TRANSFER_ERROR;
2466 lib_desc->actual_length = urb_desc->actual_length;
2469 tpriv->num_retired++;
2471 if (tpriv->reap_action != NORMAL) { /* cancelled or submit_fail */
2472 usbi_dbg("CANCEL: urb status %d", urb->status);
2474 if (tpriv->num_retired == num_urbs) {
2475 usbi_dbg("CANCEL: last URB handled, reporting");
2476 free_iso_urbs(tpriv);
2477 if (tpriv->reap_action == CANCELLED) {
2478 usbi_mutex_unlock(&itransfer->lock);
2479 return usbi_handle_transfer_cancellation(itransfer);
2481 usbi_mutex_unlock(&itransfer->lock);
2482 return usbi_handle_transfer_completion(itransfer, LIBUSB_TRANSFER_ERROR);
2488 switch (urb->status) {
2491 case -ENOENT: /* cancelled */
2495 usbi_dbg("device removed");
2496 status = LIBUSB_TRANSFER_NO_DEVICE;
2499 usbi_warn(TRANSFER_CTX(transfer), "unrecognised urb status %d", urb->status);
2500 status = LIBUSB_TRANSFER_ERROR;
2504 /* if we've reaped all urbs then we're done */
2505 if (tpriv->num_retired == num_urbs) {
2506 usbi_dbg("all URBs in transfer reaped --> complete!");
2507 free_iso_urbs(tpriv);
2508 usbi_mutex_unlock(&itransfer->lock);
2509 return usbi_handle_transfer_completion(itransfer, status);
2513 usbi_mutex_unlock(&itransfer->lock);
2517 static int handle_control_completion(struct usbi_transfer *itransfer,
2518 struct usbfs_urb *urb)
2520 struct linux_transfer_priv *tpriv = usbi_get_transfer_priv(itransfer);
2523 usbi_mutex_lock(&itransfer->lock);
2524 usbi_dbg("handling completion status %d", urb->status);
2526 itransfer->transferred += urb->actual_length;
2528 if (tpriv->reap_action == CANCELLED) {
2529 if (urb->status && urb->status != -ENOENT)
2530 usbi_warn(ITRANSFER_CTX(itransfer), "cancel: unrecognised urb status %d",
2534 usbi_mutex_unlock(&itransfer->lock);
2535 return usbi_handle_transfer_cancellation(itransfer);
2538 switch (urb->status) {
2540 status = LIBUSB_TRANSFER_COMPLETED;
2542 case -ENOENT: /* cancelled */
2543 status = LIBUSB_TRANSFER_CANCELLED;
2547 usbi_dbg("device removed");
2548 status = LIBUSB_TRANSFER_NO_DEVICE;
2551 usbi_dbg("unsupported control request");
2552 status = LIBUSB_TRANSFER_STALL;
2555 usbi_dbg("overflow, actual_length=%d", urb->actual_length);
2556 status = LIBUSB_TRANSFER_OVERFLOW;
2563 usbi_dbg("low-level bus error %d", urb->status);
2564 status = LIBUSB_TRANSFER_ERROR;
2567 usbi_warn(ITRANSFER_CTX(itransfer), "unrecognised urb status %d", urb->status);
2568 status = LIBUSB_TRANSFER_ERROR;
2574 usbi_mutex_unlock(&itransfer->lock);
2575 return usbi_handle_transfer_completion(itransfer, status);
2578 static int reap_for_handle(struct libusb_device_handle *handle)
2580 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
2582 struct usbfs_urb *urb = NULL;
2583 struct usbi_transfer *itransfer;
2584 struct libusb_transfer *transfer;
2586 r = ioctl(hpriv->fd, IOCTL_USBFS_REAPURBNDELAY, &urb);
2588 if (errno == EAGAIN)
2590 if (errno == ENODEV)
2591 return LIBUSB_ERROR_NO_DEVICE;
2593 usbi_err(HANDLE_CTX(handle), "reap failed, errno=%d", errno);
2594 return LIBUSB_ERROR_IO;
2597 itransfer = urb->usercontext;
2598 transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
2600 usbi_dbg("urb type=%u status=%d transferred=%d", urb->type, urb->status, urb->actual_length);
2602 switch (transfer->type) {
2603 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
2604 return handle_iso_completion(itransfer, urb);
2605 case LIBUSB_TRANSFER_TYPE_BULK:
2606 case LIBUSB_TRANSFER_TYPE_BULK_STREAM:
2607 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
2608 return handle_bulk_completion(itransfer, urb);
2609 case LIBUSB_TRANSFER_TYPE_CONTROL:
2610 return handle_control_completion(itransfer, urb);
2612 usbi_err(HANDLE_CTX(handle), "unrecognised transfer type %u", transfer->type);
2613 return LIBUSB_ERROR_OTHER;
2617 static int op_handle_events(struct libusb_context *ctx,
2618 void *event_data, unsigned int count, unsigned int num_ready)
2620 struct pollfd *fds = event_data;
2624 usbi_mutex_lock(&ctx->open_devs_lock);
2625 for (n = 0; n < count && num_ready > 0; n++) {
2626 struct pollfd *pollfd = &fds[n];
2627 struct libusb_device_handle *handle;
2628 struct linux_device_handle_priv *hpriv = NULL;
2630 if (!pollfd->revents)
2634 for_each_open_device(ctx, handle) {
2635 hpriv = usbi_get_device_handle_priv(handle);
2636 if (hpriv->fd == pollfd->fd)
2640 if (!hpriv || hpriv->fd != pollfd->fd) {
2641 usbi_err(ctx, "cannot find handle for fd %d",
2646 if (pollfd->revents & POLLERR) {
2647 /* remove the fd from the pollfd set so that it doesn't continuously
2648 * trigger an event, and flag that it has been removed so op_close()
2649 * doesn't try to remove it a second time */
2650 usbi_remove_event_source(HANDLE_CTX(handle), hpriv->fd);
2651 hpriv->fd_removed = 1;
2653 /* device will still be marked as attached if hotplug monitor thread
2654 * hasn't processed remove event yet */
2655 usbi_mutex_static_lock(&linux_hotplug_lock);
2656 if (handle->dev->attached)
2657 linux_device_disconnected(handle->dev->bus_number,
2658 handle->dev->device_address);
2659 usbi_mutex_static_unlock(&linux_hotplug_lock);
2661 if (hpriv->caps & USBFS_CAP_REAP_AFTER_DISCONNECT) {
2663 r = reap_for_handle(handle);
2667 usbi_handle_disconnect(handle);
2672 r = reap_for_handle(handle);
2674 if (r == 1 || r == LIBUSB_ERROR_NO_DEVICE)
2682 usbi_mutex_unlock(&ctx->open_devs_lock);
2686 const struct usbi_os_backend usbi_backend = {
2687 .name = "Linux usbfs",
2688 .caps = USBI_CAP_HAS_HID_ACCESS|USBI_CAP_SUPPORTS_DETACH_KERNEL_DRIVER,
2691 .hotplug_poll = op_hotplug_poll,
2692 .get_active_config_descriptor = op_get_active_config_descriptor,
2693 .get_config_descriptor = op_get_config_descriptor,
2694 .get_config_descriptor_by_value = op_get_config_descriptor_by_value,
2696 .wrap_sys_device = op_wrap_sys_device,
2699 .get_configuration = op_get_configuration,
2700 .set_configuration = op_set_configuration,
2701 .claim_interface = op_claim_interface,
2702 .release_interface = op_release_interface,
2704 .set_interface_altsetting = op_set_interface,
2705 .clear_halt = op_clear_halt,
2706 .reset_device = op_reset_device,
2708 .alloc_streams = op_alloc_streams,
2709 .free_streams = op_free_streams,
2711 .dev_mem_alloc = op_dev_mem_alloc,
2712 .dev_mem_free = op_dev_mem_free,
2714 .kernel_driver_active = op_kernel_driver_active,
2715 .detach_kernel_driver = op_detach_kernel_driver,
2716 .attach_kernel_driver = op_attach_kernel_driver,
2718 .destroy_device = op_destroy_device,
2720 .submit_transfer = op_submit_transfer,
2721 .cancel_transfer = op_cancel_transfer,
2722 .clear_transfer_priv = op_clear_transfer_priv,
2724 .handle_events = op_handle_events,
2726 .device_priv_size = sizeof(struct linux_device_priv),
2727 .device_handle_priv_size = sizeof(struct linux_device_handle_priv),
2728 .transfer_priv_size = sizeof(struct linux_transfer_priv),