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>
41 * opening a usbfs node causes the device to be resumed, so we attempt to
42 * avoid this during enumeration.
44 * sysfs allows us to read the kernel's in-memory copies of device descriptors
45 * and so forth, avoiding the need to open the device:
46 * - The binary "descriptors" file contains all config descriptors since
47 * 2.6.26, commit 217a9081d8e69026186067711131b77f0ce219ed
48 * - The binary "descriptors" file was added in 2.6.23, commit
49 * 69d42a78f935d19384d1f6e4f94b65bb162b36df, but it only contains the
50 * active config descriptors
51 * - The "busnum" file was added in 2.6.22, commit
52 * 83f7d958eab2fbc6b159ee92bf1493924e1d0f72
53 * - The "devnum" file has been present since pre-2.6.18
54 * - the "bConfigurationValue" file has been present since pre-2.6.18
56 * If we have bConfigurationValue, busnum, and devnum, then we can determine
57 * the active configuration without having to open the usbfs node in RDWR mode.
58 * The busnum file is important as that is the only way we can relate sysfs
59 * devices to usbfs nodes.
61 * If we also have all descriptors, we can obtain the device descriptor and
62 * configuration without touching usbfs at all.
65 /* endianness for multi-byte fields:
67 * Descriptors exposed by usbfs have the multi-byte fields in the device
68 * descriptor as host endian. Multi-byte fields in the other descriptors are
69 * bus-endian. The kernel documentation says otherwise, but it is wrong.
71 * In sysfs all descriptors are bus-endian.
74 #define USBDEV_PATH "/dev"
75 #define USB_DEVTMPFS_PATH "/dev/bus/usb"
77 /* use usbdev*.* device names in /dev instead of the usbfs bus directories */
78 static int usbdev_names = 0;
80 /* Linux has changed the maximum length of an individual isochronous packet
81 * over time. Initially this limit was 1,023 bytes, but Linux 2.6.18
82 * (commit 3612242e527eb47ee4756b5350f8bdf791aa5ede) increased this value to
83 * 8,192 bytes to support higher bandwidth devices. Linux 3.10
84 * (commit e2e2f0ea1c935edcf53feb4c4c8fdb4f86d57dd9) further increased this
85 * value to 49,152 bytes to support super speed devices. Linux 5.2
86 * (commit 8a1dbc8d91d3d1602282c7e6b4222c7759c916fa) even further increased
87 * this value to 98,304 bytes to support super speed plus devices.
89 static unsigned int max_iso_packet_len = 0;
91 /* is sysfs available (mounted) ? */
92 static int sysfs_available = -1;
94 /* how many times have we initted (and not exited) ? */
95 static int init_count = 0;
97 /* Serialize hotplug start/stop */
98 static usbi_mutex_static_t linux_hotplug_startstop_lock = USBI_MUTEX_INITIALIZER;
99 /* Serialize scan-devices, event-thread, and poll */
100 usbi_mutex_static_t linux_hotplug_lock = USBI_MUTEX_INITIALIZER;
102 static int linux_scan_devices(struct libusb_context *ctx);
103 static int detach_kernel_driver_and_claim(struct libusb_device_handle *, int);
105 #if !defined(HAVE_LIBUDEV)
106 static int linux_default_scan_devices(struct libusb_context *ctx);
109 struct kernel_version {
115 struct linux_device_priv {
117 unsigned char *descriptors;
119 int active_config; /* cache val for !sysfs_available */
122 struct linux_device_handle_priv {
131 /* submission failed after the first URB, so await cancellation/completion
132 * of all the others */
135 /* cancelled by user or timeout */
138 /* completed multi-URB transfer in non-final URB */
141 /* one or more urbs encountered a low-level error */
145 struct linux_transfer_priv {
147 struct usbfs_urb *urbs;
148 struct usbfs_urb **iso_urbs;
151 enum reap_action reap_action;
154 enum libusb_transfer_status reap_status;
156 /* next iso packet in user-supplied transfer to be populated */
157 int iso_packet_offset;
160 static int get_usbfs_fd(struct libusb_device *dev, mode_t mode, int silent)
162 struct libusb_context *ctx = DEVICE_CTX(dev);
167 sprintf(path, USBDEV_PATH "/usbdev%u.%u",
168 dev->bus_number, dev->device_address);
170 sprintf(path, USB_DEVTMPFS_PATH "/%03u/%03u",
171 dev->bus_number, dev->device_address);
173 fd = open(path, mode | O_CLOEXEC);
175 return fd; /* Success */
177 if (errno == ENOENT) {
178 const long delay_ms = 10L;
179 const struct timespec delay_ts = { 0L, delay_ms * 1000L * 1000L };
182 usbi_err(ctx, "File doesn't exist, wait %ld ms and try again", delay_ms);
184 /* Wait 10ms for USB device path creation.*/
185 nanosleep(&delay_ts, NULL);
187 fd = open(path, mode | O_CLOEXEC);
189 return fd; /* Success */
193 usbi_err(ctx, "libusb couldn't open USB device %s, errno=%d", path, errno);
194 if (errno == EACCES && mode == O_RDWR)
195 usbi_err(ctx, "libusb requires write access to USB device nodes");
199 return LIBUSB_ERROR_ACCESS;
201 return LIBUSB_ERROR_NO_DEVICE;
202 return LIBUSB_ERROR_IO;
205 /* check dirent for a /dev/usbdev%d.%d name
206 * optionally return bus/device on success */
207 static int is_usbdev_entry(const char *name, uint8_t *bus_p, uint8_t *dev_p)
211 if (sscanf(name, "usbdev%d.%d", &busnum, &devnum) != 2)
213 if (busnum < 0 || busnum > UINT8_MAX || devnum < 0 || devnum > UINT8_MAX) {
214 usbi_dbg("invalid usbdev format '%s'", name);
218 usbi_dbg("found: %s", name);
220 *bus_p = (uint8_t)busnum;
222 *dev_p = (uint8_t)devnum;
226 static const char *find_usbfs_path(void)
230 struct dirent *entry;
232 path = USB_DEVTMPFS_PATH;
235 while ((entry = readdir(dir))) {
236 if (entry->d_name[0] == '.')
239 /* We assume if we find any files that it must be the right place */
249 /* look for /dev/usbdev*.* if the normal place fails */
253 while ((entry = readdir(dir))) {
254 if (entry->d_name[0] == '.')
257 if (is_usbdev_entry(entry->d_name, NULL, NULL)) {
258 /* found one; that's enough */
271 /* On udev based systems without any usb-devices /dev/bus/usb will not
272 * exist. So if we've not found anything and we're using udev for hotplug
273 * simply assume /dev/bus/usb rather then making libusb_init fail.
274 * Make the same assumption for Android where SELinux policies might block us
275 * from reading /dev on newer devices. */
276 #if defined(HAVE_LIBUDEV) || defined(__ANDROID__)
277 return USB_DEVTMPFS_PATH;
283 static int get_kernel_version(struct libusb_context *ctx,
284 struct kernel_version *ver)
289 if (uname(&uts) < 0) {
290 usbi_err(ctx, "uname failed, errno=%d", errno);
294 atoms = sscanf(uts.release, "%d.%d.%d", &ver->major, &ver->minor, &ver->sublevel);
296 usbi_err(ctx, "failed to parse uname release '%s'", uts.release);
303 usbi_dbg("reported kernel version is %s", uts.release);
308 static int kernel_version_ge(const struct kernel_version *ver,
309 int major, int minor, int sublevel)
311 if (ver->major > major)
313 else if (ver->major < major)
316 /* kmajor == major */
317 if (ver->minor > minor)
319 else if (ver->minor < minor)
322 /* kminor == minor */
323 if (ver->sublevel == -1)
324 return sublevel == 0;
326 return ver->sublevel >= sublevel;
329 static int op_init(struct libusb_context *ctx)
331 struct kernel_version kversion;
332 const char *usbfs_path;
335 if (get_kernel_version(ctx, &kversion) < 0)
336 return LIBUSB_ERROR_OTHER;
338 if (!kernel_version_ge(&kversion, 2, 6, 32)) {
339 usbi_err(ctx, "kernel version is too old (reported as %d.%d.%d)",
340 kversion.major, kversion.minor,
341 kversion.sublevel != -1 ? kversion.sublevel : 0);
342 return LIBUSB_ERROR_NOT_SUPPORTED;
345 usbfs_path = find_usbfs_path();
347 usbi_err(ctx, "could not find usbfs");
348 return LIBUSB_ERROR_OTHER;
351 usbi_dbg("found usbfs at %s", usbfs_path);
353 if (!max_iso_packet_len) {
354 if (kernel_version_ge(&kversion, 5, 2, 0))
355 max_iso_packet_len = 98304;
356 else if (kernel_version_ge(&kversion, 3, 10, 0))
357 max_iso_packet_len = 49152;
359 max_iso_packet_len = 8192;
362 usbi_dbg("max iso packet length is (likely) %u bytes", max_iso_packet_len);
364 if (sysfs_available == -1) {
365 struct statfs statfsbuf;
367 r = statfs(SYSFS_MOUNT_PATH, &statfsbuf);
368 if (r == 0 && statfsbuf.f_type == SYSFS_MAGIC) {
369 usbi_dbg("sysfs is available");
372 usbi_warn(ctx, "sysfs not mounted");
377 usbi_mutex_static_lock(&linux_hotplug_startstop_lock);
379 if (init_count == 0) {
380 /* start up hotplug event handler */
381 r = linux_start_event_monitor();
383 if (r == LIBUSB_SUCCESS) {
384 r = linux_scan_devices(ctx);
385 if (r == LIBUSB_SUCCESS)
387 else if (init_count == 0)
388 linux_stop_event_monitor();
390 usbi_err(ctx, "error starting hotplug event monitor");
392 usbi_mutex_static_unlock(&linux_hotplug_startstop_lock);
397 static void op_exit(struct libusb_context *ctx)
400 usbi_mutex_static_lock(&linux_hotplug_startstop_lock);
401 assert(init_count != 0);
403 /* tear down event handler */
404 linux_stop_event_monitor();
406 usbi_mutex_static_unlock(&linux_hotplug_startstop_lock);
409 static int linux_scan_devices(struct libusb_context *ctx)
413 usbi_mutex_static_lock(&linux_hotplug_lock);
415 #if defined(HAVE_LIBUDEV)
416 ret = linux_udev_scan_devices(ctx);
418 ret = linux_default_scan_devices(ctx);
421 usbi_mutex_static_unlock(&linux_hotplug_lock);
426 static void op_hotplug_poll(void)
428 linux_hotplug_poll();
431 static int open_sysfs_attr(struct libusb_context *ctx,
432 const char *sysfs_dir, const char *attr)
437 snprintf(filename, sizeof(filename), SYSFS_DEVICE_PATH "/%s/%s", sysfs_dir, attr);
438 fd = open(filename, O_RDONLY | O_CLOEXEC);
440 if (errno == ENOENT) {
441 /* File doesn't exist. Assume the device has been
442 disconnected (see trac ticket #70). */
443 return LIBUSB_ERROR_NO_DEVICE;
445 usbi_err(ctx, "open %s failed, errno=%d", filename, errno);
446 return LIBUSB_ERROR_IO;
452 /* Note only suitable for attributes which always read >= 0, < 0 is error */
453 static int read_sysfs_attr(struct libusb_context *ctx,
454 const char *sysfs_dir, const char *attr, int max_value, int *value_p)
456 char buf[20], *endptr;
461 fd = open_sysfs_attr(ctx, sysfs_dir, attr);
465 r = read(fd, buf, sizeof(buf));
470 return LIBUSB_ERROR_NO_DEVICE;
471 usbi_err(ctx, "attribute %s read failed, errno=%zd", attr, r);
472 return LIBUSB_ERROR_IO;
477 /* Certain attributes (e.g. bConfigurationValue) are not
478 * populated if the device is not configured. */
483 /* The kernel does *not* NULL-terminate the string, but every attribute
484 * should be terminated with a newline character. */
485 if (!isdigit(buf[0])) {
486 usbi_err(ctx, "attribute %s doesn't have numeric value?", attr);
487 return LIBUSB_ERROR_IO;
488 } else if (buf[r - 1] != '\n') {
489 usbi_err(ctx, "attribute %s doesn't end with newline?", attr);
490 return LIBUSB_ERROR_IO;
495 value = strtol(buf, &endptr, 10);
496 if (value < 0 || value > (long)max_value || errno) {
497 usbi_err(ctx, "attribute %s contains an invalid value: '%s'", attr, buf);
498 return LIBUSB_ERROR_INVALID_PARAM;
499 } else if (*endptr != '\0') {
500 /* Consider the value to be valid if the remainder is a '.'
501 * character followed by numbers. This occurs, for example,
502 * when reading the "speed" attribute for a low-speed device
504 if (*endptr == '.' && isdigit(*(endptr + 1))) {
506 while (isdigit(*endptr))
509 if (*endptr != '\0') {
510 usbi_err(ctx, "attribute %s contains an invalid value: '%s'", attr, buf);
511 return LIBUSB_ERROR_INVALID_PARAM;
515 *value_p = (int)value;
519 static int op_get_device_descriptor(struct libusb_device *dev,
520 unsigned char *buffer, int *host_endian)
522 struct linux_device_priv *priv = usbi_get_device_priv(dev);
524 *host_endian = priv->sysfs_dir != NULL;
525 memcpy(buffer, priv->descriptors, DEVICE_DESC_LENGTH);
530 static int sysfs_scan_device(struct libusb_context *ctx, const char *devname)
532 uint8_t busnum, devaddr;
535 ret = linux_get_device_address(ctx, 0, &busnum, &devaddr, NULL, devname, -1);
536 if (ret != LIBUSB_SUCCESS)
539 return linux_enumerate_device(ctx, busnum, devaddr, devname);
542 /* read the bConfigurationValue for a device */
543 static int sysfs_get_active_config(struct libusb_device *dev, int *config)
545 struct linux_device_priv *priv = usbi_get_device_priv(dev);
548 ret = read_sysfs_attr(DEVICE_CTX(dev), priv->sysfs_dir, "bConfigurationValue",
554 usbi_dbg("device unconfigured");
559 int linux_get_device_address(struct libusb_context *ctx, int detached,
560 uint8_t *busnum, uint8_t *devaddr, const char *dev_node,
561 const char *sys_name, int fd)
566 usbi_dbg("getting address for device: %s detached: %d", sys_name, detached);
567 /* can't use sysfs to read the bus and device number if the
568 * device has been detached */
569 if (!sysfs_available || detached || !sys_name) {
570 if (!dev_node && fd >= 0) {
571 char *fd_path = alloca(PATH_MAX);
574 /* try to retrieve the device node from fd */
575 sprintf(proc_path, "/proc/self/fd/%d", fd);
576 r = readlink(proc_path, fd_path, PATH_MAX - 1);
584 return LIBUSB_ERROR_OTHER;
586 /* will this work with all supported kernel versions? */
587 if (!strncmp(dev_node, "/dev/bus/usb", 12))
588 sscanf(dev_node, "/dev/bus/usb/%hhu/%hhu", busnum, devaddr);
590 return LIBUSB_ERROR_OTHER;
592 return LIBUSB_SUCCESS;
595 usbi_dbg("scan %s", sys_name);
597 r = read_sysfs_attr(ctx, sys_name, "busnum", UINT8_MAX, &sysfs_val);
600 *busnum = (uint8_t)sysfs_val;
602 r = read_sysfs_attr(ctx, sys_name, "devnum", UINT8_MAX, &sysfs_val);
605 *devaddr = (uint8_t)sysfs_val;
607 usbi_dbg("bus=%u dev=%u", *busnum, *devaddr);
609 return LIBUSB_SUCCESS;
612 /* Return offset of the next descriptor with the given type */
613 static int seek_to_next_descriptor(struct libusb_context *ctx,
614 uint8_t descriptor_type, unsigned char *buffer, int size)
616 struct usbi_descriptor_header header;
619 for (i = 0; size >= 0; i += header.bLength, size -= header.bLength) {
621 return LIBUSB_ERROR_NOT_FOUND;
624 usbi_err(ctx, "short descriptor read %d/2", size);
625 return LIBUSB_ERROR_IO;
627 usbi_parse_descriptor(buffer + i, "bb", &header, 0);
629 if (i && header.bDescriptorType == descriptor_type)
632 usbi_err(ctx, "bLength overflow by %d bytes", -size);
633 return LIBUSB_ERROR_IO;
636 /* Return offset to next config */
637 static int seek_to_next_config(struct libusb_device *dev,
638 unsigned char *buffer, int size)
640 struct libusb_context *ctx = DEVICE_CTX(dev);
641 struct linux_device_priv *priv = usbi_get_device_priv(dev);
642 struct libusb_config_descriptor config;
645 return LIBUSB_ERROR_NOT_FOUND;
647 if (size < LIBUSB_DT_CONFIG_SIZE) {
648 usbi_err(ctx, "short descriptor read %d/%d",
649 size, LIBUSB_DT_CONFIG_SIZE);
650 return LIBUSB_ERROR_IO;
653 usbi_parse_descriptor(buffer, "bbwbbbbb", &config, 0);
654 if (config.bDescriptorType != LIBUSB_DT_CONFIG) {
655 usbi_err(ctx, "descriptor is not a config desc (type 0x%02x)",
656 config.bDescriptorType);
657 return LIBUSB_ERROR_IO;
661 * In usbfs the config descriptors are config.wTotalLength bytes apart,
662 * with any short reads from the device appearing as holes in the file.
664 * In sysfs wTotalLength is ignored, instead the kernel returns a
665 * config descriptor with verified bLength fields, with descriptors
666 * with an invalid bLength removed.
668 if (priv->sysfs_dir) {
669 int next = seek_to_next_descriptor(ctx, LIBUSB_DT_CONFIG, buffer, size);
671 if (next == LIBUSB_ERROR_NOT_FOUND)
676 if (next != config.wTotalLength)
677 usbi_warn(ctx, "config length mismatch wTotalLength %u real %d",
678 config.wTotalLength, next);
681 if (config.wTotalLength < LIBUSB_DT_CONFIG_SIZE) {
682 usbi_err(ctx, "invalid wTotalLength %u", config.wTotalLength);
683 return LIBUSB_ERROR_IO;
684 } else if (config.wTotalLength > size) {
685 usbi_warn(ctx, "short descriptor read %d/%u",
686 size, config.wTotalLength);
689 return config.wTotalLength;
694 static int op_get_config_descriptor_by_value(struct libusb_device *dev,
695 uint8_t value, unsigned char **buffer, int *host_endian)
697 struct linux_device_priv *priv = usbi_get_device_priv(dev);
698 unsigned char *descriptors = priv->descriptors;
699 int size = priv->descriptors_len;
700 struct libusb_config_descriptor *config;
703 /* Unlike the device desc. config descs. are always in raw format */
706 /* Skip device header */
707 descriptors += DEVICE_DESC_LENGTH;
708 size -= DEVICE_DESC_LENGTH;
710 /* Seek till the config is found, or till "EOF" */
712 int next = seek_to_next_config(dev, descriptors, size);
716 config = (struct libusb_config_descriptor *)descriptors;
717 if (config->bConfigurationValue == value) {
718 *buffer = descriptors;
726 static int op_get_active_config_descriptor(struct libusb_device *dev,
727 unsigned char *buffer, size_t len, int *host_endian)
729 struct linux_device_priv *priv = usbi_get_device_priv(dev);
731 unsigned char *config_desc;
733 if (priv->sysfs_dir) {
734 r = sysfs_get_active_config(dev, &config);
738 /* Use cached bConfigurationValue */
739 config = priv->active_config;
742 return LIBUSB_ERROR_NOT_FOUND;
744 r = op_get_config_descriptor_by_value(dev, config, &config_desc,
749 len = MIN(len, (size_t)r);
750 memcpy(buffer, config_desc, len);
754 static int op_get_config_descriptor(struct libusb_device *dev,
755 uint8_t config_index, unsigned char *buffer, size_t len, int *host_endian)
757 struct linux_device_priv *priv = usbi_get_device_priv(dev);
758 unsigned char *descriptors = priv->descriptors;
759 int i, r, size = priv->descriptors_len;
761 /* Unlike the device desc. config descs. are always in raw format */
764 /* Skip device header */
765 descriptors += DEVICE_DESC_LENGTH;
766 size -= DEVICE_DESC_LENGTH;
768 /* Seek till the config is found, or till "EOF" */
770 r = seek_to_next_config(dev, descriptors, size);
773 if (i == config_index)
779 len = MIN(len, (size_t)r);
780 memcpy(buffer, descriptors, len);
784 /* send a control message to retrieve active configuration */
785 static int usbfs_get_active_config(struct libusb_device *dev, int fd)
787 struct linux_device_priv *priv = usbi_get_device_priv(dev);
788 unsigned char active_config = 0;
791 struct usbfs_ctrltransfer ctrl = {
792 .bmRequestType = LIBUSB_ENDPOINT_IN,
793 .bRequest = LIBUSB_REQUEST_GET_CONFIGURATION,
798 .data = &active_config
801 r = ioctl(fd, IOCTL_USBFS_CONTROL, &ctrl);
804 return LIBUSB_ERROR_NO_DEVICE;
806 /* we hit this error path frequently with buggy devices :( */
807 usbi_warn(DEVICE_CTX(dev), "get configuration failed, errno=%d", errno);
808 priv->active_config = -1;
810 if (active_config > 0) {
811 priv->active_config = active_config;
813 /* some buggy devices have a configuration 0, but we're
814 * reaching into the corner of a corner case here, so let's
815 * not support buggy devices in these circumstances.
816 * stick to the specs: a configuration value of 0 means
818 usbi_warn(DEVICE_CTX(dev), "active cfg 0? assuming unconfigured device");
819 priv->active_config = -1;
823 return LIBUSB_SUCCESS;
826 static int initialize_device(struct libusb_device *dev, uint8_t busnum,
827 uint8_t devaddr, const char *sysfs_dir, int wrapped_fd)
829 struct linux_device_priv *priv = usbi_get_device_priv(dev);
830 struct libusb_context *ctx = DEVICE_CTX(dev);
831 int descriptors_size = 0;
835 dev->bus_number = busnum;
836 dev->device_address = devaddr;
839 priv->sysfs_dir = strdup(sysfs_dir);
840 if (!priv->sysfs_dir)
841 return LIBUSB_ERROR_NO_MEM;
843 /* Note speed can contain 1.5, in this case read_sysfs_attr()
844 will stop parsing at the '.' and return 1 */
845 if (read_sysfs_attr(ctx, sysfs_dir, "speed", INT_MAX, &speed) == 0) {
847 case 1: dev->speed = LIBUSB_SPEED_LOW; break;
848 case 12: dev->speed = LIBUSB_SPEED_FULL; break;
849 case 480: dev->speed = LIBUSB_SPEED_HIGH; break;
850 case 5000: dev->speed = LIBUSB_SPEED_SUPER; break;
851 case 10000: dev->speed = LIBUSB_SPEED_SUPER_PLUS; break;
853 usbi_warn(ctx, "unknown device speed: %d Mbps", speed);
858 /* cache descriptors in memory */
860 fd = open_sysfs_attr(ctx, sysfs_dir, "descriptors");
861 } else if (wrapped_fd < 0) {
862 fd = get_usbfs_fd(dev, O_RDONLY, 0);
865 r = lseek(fd, 0, SEEK_SET);
867 usbi_err(ctx, "lseek failed, errno=%d", errno);
868 return LIBUSB_ERROR_IO;
875 descriptors_size += 256;
876 priv->descriptors = usbi_reallocf(priv->descriptors, descriptors_size);
877 if (!priv->descriptors) {
878 if (fd != wrapped_fd)
880 return LIBUSB_ERROR_NO_MEM;
882 /* usbfs has holes in the file */
884 memset(priv->descriptors + priv->descriptors_len,
885 0, descriptors_size - priv->descriptors_len);
887 r = read(fd, priv->descriptors + priv->descriptors_len,
888 descriptors_size - priv->descriptors_len);
890 usbi_err(ctx, "read descriptor failed, errno=%d", errno);
891 if (fd != wrapped_fd)
893 return LIBUSB_ERROR_IO;
895 priv->descriptors_len += r;
896 } while (priv->descriptors_len == descriptors_size);
898 if (fd != wrapped_fd)
901 if (priv->descriptors_len < DEVICE_DESC_LENGTH) {
902 usbi_err(ctx, "short descriptor read (%d)", priv->descriptors_len);
903 return LIBUSB_ERROR_IO;
907 return LIBUSB_SUCCESS;
909 /* cache active config */
911 fd = get_usbfs_fd(dev, O_RDWR, 1);
915 /* cannot send a control message to determine the active
916 * config. just assume the first one is active. */
917 usbi_warn(ctx, "Missing rw usbfs access; cannot determine "
918 "active configuration descriptor");
919 if (priv->descriptors_len >= (DEVICE_DESC_LENGTH + LIBUSB_DT_CONFIG_SIZE)) {
920 struct libusb_config_descriptor config;
922 usbi_parse_descriptor(priv->descriptors + DEVICE_DESC_LENGTH,
923 "bbwbbbbb", &config, 0);
924 priv->active_config = config.bConfigurationValue;
926 priv->active_config = -1; /* No config dt */
929 return LIBUSB_SUCCESS;
932 r = usbfs_get_active_config(dev, fd);
933 if (fd != wrapped_fd)
939 static int linux_get_parent_info(struct libusb_device *dev, const char *sysfs_dir)
941 struct libusb_context *ctx = DEVICE_CTX(dev);
942 struct libusb_device *it;
943 char *parent_sysfs_dir, *tmp;
944 int ret, add_parent = 1;
946 /* XXX -- can we figure out the topology when using usbfs? */
947 if (!sysfs_dir || !strncmp(sysfs_dir, "usb", 3)) {
948 /* either using usbfs or finding the parent of a root hub */
949 return LIBUSB_SUCCESS;
952 parent_sysfs_dir = strdup(sysfs_dir);
953 if (!parent_sysfs_dir)
954 return LIBUSB_ERROR_NO_MEM;
956 if ((tmp = strrchr(parent_sysfs_dir, '.')) ||
957 (tmp = strrchr(parent_sysfs_dir, '-'))) {
958 dev->port_number = atoi(tmp + 1);
961 usbi_warn(ctx, "Can not parse sysfs_dir: %s, no parent info",
963 free(parent_sysfs_dir);
964 return LIBUSB_SUCCESS;
967 /* is the parent a root hub? */
968 if (!strchr(parent_sysfs_dir, '-')) {
969 tmp = parent_sysfs_dir;
970 ret = asprintf(&parent_sysfs_dir, "usb%s", tmp);
973 return LIBUSB_ERROR_NO_MEM;
977 /* find the parent in the context */
978 usbi_mutex_lock(&ctx->usb_devs_lock);
979 list_for_each_entry(it, &ctx->usb_devs, list, struct libusb_device) {
980 struct linux_device_priv *priv = usbi_get_device_priv(it);
982 if (priv->sysfs_dir) {
983 if (!strcmp(priv->sysfs_dir, parent_sysfs_dir)) {
984 dev->parent_dev = libusb_ref_device(it);
989 usbi_mutex_unlock(&ctx->usb_devs_lock);
991 if (!dev->parent_dev && add_parent) {
992 usbi_dbg("parent_dev %s not enumerated yet, enumerating now",
994 sysfs_scan_device(ctx, parent_sysfs_dir);
999 usbi_dbg("dev %p (%s) has parent %p (%s) port %u", dev, sysfs_dir,
1000 dev->parent_dev, parent_sysfs_dir, dev->port_number);
1002 free(parent_sysfs_dir);
1004 return LIBUSB_SUCCESS;
1007 int linux_enumerate_device(struct libusb_context *ctx,
1008 uint8_t busnum, uint8_t devaddr, const char *sysfs_dir)
1010 unsigned long session_id;
1011 struct libusb_device *dev;
1014 /* FIXME: session ID is not guaranteed unique as addresses can wrap and
1015 * will be reused. instead we should add a simple sysfs attribute with
1017 session_id = busnum << 8 | devaddr;
1018 usbi_dbg("busnum %u devaddr %u session_id %lu", busnum, devaddr, session_id);
1020 dev = usbi_get_device_by_session_id(ctx, session_id);
1022 /* device already exists in the context */
1023 usbi_dbg("session_id %lu already exists", session_id);
1024 libusb_unref_device(dev);
1025 return LIBUSB_SUCCESS;
1028 usbi_dbg("allocating new device for %u/%u (session %lu)",
1029 busnum, devaddr, session_id);
1030 dev = usbi_alloc_device(ctx, session_id);
1032 return LIBUSB_ERROR_NO_MEM;
1034 r = initialize_device(dev, busnum, devaddr, sysfs_dir, -1);
1037 r = usbi_sanitize_device(dev);
1041 r = linux_get_parent_info(dev, sysfs_dir);
1046 libusb_unref_device(dev);
1048 usbi_connect_device(dev);
1053 void linux_hotplug_enumerate(uint8_t busnum, uint8_t devaddr, const char *sys_name)
1055 struct libusb_context *ctx;
1057 usbi_mutex_static_lock(&active_contexts_lock);
1058 list_for_each_entry(ctx, &active_contexts_list, list, struct libusb_context) {
1059 linux_enumerate_device(ctx, busnum, devaddr, sys_name);
1061 usbi_mutex_static_unlock(&active_contexts_lock);
1064 void linux_device_disconnected(uint8_t busnum, uint8_t devaddr)
1066 struct libusb_context *ctx;
1067 struct libusb_device *dev;
1068 unsigned long session_id = busnum << 8 | devaddr;
1070 usbi_mutex_static_lock(&active_contexts_lock);
1071 list_for_each_entry(ctx, &active_contexts_list, list, struct libusb_context) {
1072 dev = usbi_get_device_by_session_id (ctx, session_id);
1074 usbi_disconnect_device(dev);
1075 libusb_unref_device(dev);
1077 usbi_dbg("device not found for session %lx", session_id);
1080 usbi_mutex_static_unlock(&active_contexts_lock);
1083 #if !defined(HAVE_LIBUDEV)
1084 static int parse_u8(const char *str, uint8_t *val_p)
1090 num = strtol(str, &endptr, 10);
1091 if (num < 0 || num > UINT8_MAX || errno)
1093 if (endptr == str || *endptr != '\0')
1096 *val_p = (uint8_t)num;
1100 /* open a bus directory and adds all discovered devices to the context */
1101 static int usbfs_scan_busdir(struct libusb_context *ctx, uint8_t busnum)
1105 struct dirent *entry;
1106 int r = LIBUSB_ERROR_IO;
1108 sprintf(dirpath, USB_DEVTMPFS_PATH "/%03u", busnum);
1109 usbi_dbg("%s", dirpath);
1110 dir = opendir(dirpath);
1112 usbi_err(ctx, "opendir '%s' failed, errno=%d", dirpath, errno);
1113 /* FIXME: should handle valid race conditions like hub unplugged
1114 * during directory iteration - this is not an error */
1118 while ((entry = readdir(dir))) {
1121 if (entry->d_name[0] == '.')
1124 if (!parse_u8(entry->d_name, &devaddr)) {
1125 usbi_dbg("unknown dir entry %s", entry->d_name);
1129 if (linux_enumerate_device(ctx, busnum, devaddr, NULL)) {
1130 usbi_dbg("failed to enumerate dir entry %s", entry->d_name);
1141 static int usbfs_get_device_list(struct libusb_context *ctx)
1143 struct dirent *entry;
1145 uint8_t busnum, devaddr;
1149 buses = opendir(USBDEV_PATH);
1151 buses = opendir(USB_DEVTMPFS_PATH);
1154 usbi_err(ctx, "opendir buses failed, errno=%d", errno);
1155 return LIBUSB_ERROR_IO;
1158 while ((entry = readdir(buses))) {
1159 if (entry->d_name[0] == '.')
1163 if (!is_usbdev_entry(entry->d_name, &busnum, &devaddr))
1166 r = linux_enumerate_device(ctx, busnum, devaddr, NULL);
1168 usbi_dbg("failed to enumerate dir entry %s", entry->d_name);
1172 if (!parse_u8(entry->d_name, &busnum)) {
1173 usbi_dbg("unknown dir entry %s", entry->d_name);
1177 r = usbfs_scan_busdir(ctx, busnum);
1188 static int sysfs_get_device_list(struct libusb_context *ctx)
1190 DIR *devices = opendir(SYSFS_DEVICE_PATH);
1191 struct dirent *entry;
1192 int num_devices = 0;
1193 int num_enumerated = 0;
1196 usbi_err(ctx, "opendir devices failed, errno=%d", errno);
1197 return LIBUSB_ERROR_IO;
1200 while ((entry = readdir(devices))) {
1201 if ((!isdigit(entry->d_name[0]) && strncmp(entry->d_name, "usb", 3))
1202 || strchr(entry->d_name, ':'))
1207 if (sysfs_scan_device(ctx, entry->d_name)) {
1208 usbi_dbg("failed to enumerate dir entry %s", entry->d_name);
1217 /* successful if at least one device was enumerated or no devices were found */
1218 if (num_enumerated || !num_devices)
1219 return LIBUSB_SUCCESS;
1221 return LIBUSB_ERROR_IO;
1224 static int linux_default_scan_devices(struct libusb_context *ctx)
1226 /* we can retrieve device list and descriptors from sysfs or usbfs.
1227 * sysfs is preferable, because if we use usbfs we end up resuming
1228 * any autosuspended USB devices. however, sysfs is not available
1229 * everywhere, so we need a usbfs fallback too.
1231 if (sysfs_available)
1232 return sysfs_get_device_list(ctx);
1234 return usbfs_get_device_list(ctx);
1238 static int initialize_handle(struct libusb_device_handle *handle, int fd)
1240 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1245 r = ioctl(fd, IOCTL_USBFS_GET_CAPABILITIES, &hpriv->caps);
1247 if (errno == ENOTTY)
1248 usbi_dbg("getcap not available");
1250 usbi_err(HANDLE_CTX(handle), "getcap failed, errno=%d", errno);
1251 hpriv->caps = USBFS_CAP_BULK_CONTINUATION;
1254 return usbi_add_pollfd(HANDLE_CTX(handle), hpriv->fd, POLLOUT);
1257 static int op_wrap_sys_device(struct libusb_context *ctx,
1258 struct libusb_device_handle *handle, intptr_t sys_dev)
1260 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1261 int fd = (int)sys_dev;
1262 uint8_t busnum, devaddr;
1263 struct usbfs_connectinfo ci;
1264 struct libusb_device *dev;
1267 r = linux_get_device_address(ctx, 1, &busnum, &devaddr, NULL, NULL, fd);
1269 r = ioctl(fd, IOCTL_USBFS_CONNECTINFO, &ci);
1271 usbi_err(ctx, "connectinfo failed, errno=%d", errno);
1272 return LIBUSB_ERROR_IO;
1274 /* There is no ioctl to get the bus number. We choose 0 here
1275 * as linux starts numbering buses from 1. */
1277 devaddr = ci.devnum;
1280 /* Session id is unused as we do not add the device to the list of
1281 * connected devices. */
1282 usbi_dbg("allocating new device for fd %d", fd);
1283 dev = usbi_alloc_device(ctx, 0);
1285 return LIBUSB_ERROR_NO_MEM;
1287 r = initialize_device(dev, busnum, devaddr, NULL, fd);
1290 r = usbi_sanitize_device(dev);
1293 /* Consider the device as connected, but do not add it to the managed
1298 r = initialize_handle(handle, fd);
1303 libusb_unref_device(dev);
1307 static int op_open(struct libusb_device_handle *handle)
1311 fd = get_usbfs_fd(handle->dev, O_RDWR, 0);
1313 if (fd == LIBUSB_ERROR_NO_DEVICE) {
1314 /* device will still be marked as attached if hotplug monitor thread
1315 * hasn't processed remove event yet */
1316 usbi_mutex_static_lock(&linux_hotplug_lock);
1317 if (handle->dev->attached) {
1318 usbi_dbg("open failed with no device, but device still attached");
1319 linux_device_disconnected(handle->dev->bus_number,
1320 handle->dev->device_address);
1322 usbi_mutex_static_unlock(&linux_hotplug_lock);
1327 r = initialize_handle(handle, fd);
1334 static void op_close(struct libusb_device_handle *dev_handle)
1336 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(dev_handle);
1338 /* fd may have already been removed by POLLERR condition in op_handle_events() */
1339 if (!hpriv->fd_removed)
1340 usbi_remove_pollfd(HANDLE_CTX(dev_handle), hpriv->fd);
1341 if (!hpriv->fd_keep)
1345 static int op_get_configuration(struct libusb_device_handle *handle,
1348 struct linux_device_priv *priv = usbi_get_device_priv(handle->dev);
1351 if (priv->sysfs_dir) {
1352 r = sysfs_get_active_config(handle->dev, config);
1354 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1356 r = usbfs_get_active_config(handle->dev, hpriv->fd);
1357 if (r == LIBUSB_SUCCESS)
1358 *config = priv->active_config;
1363 if (*config == -1) {
1364 usbi_err(HANDLE_CTX(handle), "device unconfigured");
1371 static int op_set_configuration(struct libusb_device_handle *handle, int config)
1373 struct linux_device_priv *priv = usbi_get_device_priv(handle->dev);
1374 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1376 int r = ioctl(fd, IOCTL_USBFS_SETCONFIGURATION, &config);
1379 if (errno == EINVAL)
1380 return LIBUSB_ERROR_NOT_FOUND;
1381 else if (errno == EBUSY)
1382 return LIBUSB_ERROR_BUSY;
1383 else if (errno == ENODEV)
1384 return LIBUSB_ERROR_NO_DEVICE;
1386 usbi_err(HANDLE_CTX(handle), "set configuration failed, errno=%d", errno);
1387 return LIBUSB_ERROR_OTHER;
1390 /* update our cached active config descriptor */
1391 priv->active_config = config;
1393 return LIBUSB_SUCCESS;
1396 static int claim_interface(struct libusb_device_handle *handle, int iface)
1398 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1400 int r = ioctl(fd, IOCTL_USBFS_CLAIMINTERFACE, &iface);
1403 if (errno == ENOENT)
1404 return LIBUSB_ERROR_NOT_FOUND;
1405 else if (errno == EBUSY)
1406 return LIBUSB_ERROR_BUSY;
1407 else if (errno == ENODEV)
1408 return LIBUSB_ERROR_NO_DEVICE;
1410 usbi_err(HANDLE_CTX(handle), "claim interface failed, errno=%d", errno);
1411 return LIBUSB_ERROR_OTHER;
1416 static int release_interface(struct libusb_device_handle *handle, int iface)
1418 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1420 int r = ioctl(fd, IOCTL_USBFS_RELEASEINTERFACE, &iface);
1423 if (errno == ENODEV)
1424 return LIBUSB_ERROR_NO_DEVICE;
1426 usbi_err(HANDLE_CTX(handle), "release interface failed, errno=%d", errno);
1427 return LIBUSB_ERROR_OTHER;
1432 static int op_set_interface(struct libusb_device_handle *handle, int iface,
1435 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1437 struct usbfs_setinterface setintf;
1440 setintf.interface = iface;
1441 setintf.altsetting = altsetting;
1442 r = ioctl(fd, IOCTL_USBFS_SETINTERFACE, &setintf);
1444 if (errno == EINVAL)
1445 return LIBUSB_ERROR_NOT_FOUND;
1446 else if (errno == ENODEV)
1447 return LIBUSB_ERROR_NO_DEVICE;
1449 usbi_err(HANDLE_CTX(handle), "set interface failed, errno=%d", errno);
1450 return LIBUSB_ERROR_OTHER;
1456 static int op_clear_halt(struct libusb_device_handle *handle,
1457 unsigned char endpoint)
1459 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1461 unsigned int _endpoint = endpoint;
1462 int r = ioctl(fd, IOCTL_USBFS_CLEAR_HALT, &_endpoint);
1465 if (errno == ENOENT)
1466 return LIBUSB_ERROR_NOT_FOUND;
1467 else if (errno == ENODEV)
1468 return LIBUSB_ERROR_NO_DEVICE;
1470 usbi_err(HANDLE_CTX(handle), "clear halt failed, errno=%d", errno);
1471 return LIBUSB_ERROR_OTHER;
1477 static int op_reset_device(struct libusb_device_handle *handle)
1479 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1483 /* Doing a device reset will cause the usbfs driver to get unbound
1484 * from any interfaces it is bound to. By voluntarily unbinding
1485 * the usbfs driver ourself, we stop the kernel from rebinding
1486 * the interface after reset (which would end up with the interface
1487 * getting bound to the in kernel driver if any). */
1488 for (i = 0; i < USB_MAXINTERFACES; i++) {
1489 if (handle->claimed_interfaces & (1UL << i))
1490 release_interface(handle, i);
1493 usbi_mutex_lock(&handle->lock);
1494 r = ioctl(fd, IOCTL_USBFS_RESET, NULL);
1496 if (errno == ENODEV) {
1497 ret = LIBUSB_ERROR_NOT_FOUND;
1501 usbi_err(HANDLE_CTX(handle), "reset failed, errno=%d", errno);
1502 ret = LIBUSB_ERROR_OTHER;
1506 /* And re-claim any interfaces which were claimed before the reset */
1507 for (i = 0; i < USB_MAXINTERFACES; i++) {
1508 if (!(handle->claimed_interfaces & (1UL << i)))
1511 * A driver may have completed modprobing during
1512 * IOCTL_USBFS_RESET, and bound itself as soon as
1513 * IOCTL_USBFS_RESET released the device lock
1515 r = detach_kernel_driver_and_claim(handle, i);
1517 usbi_warn(HANDLE_CTX(handle), "failed to re-claim interface %d after reset: %s",
1518 i, libusb_error_name(r));
1519 handle->claimed_interfaces &= ~(1UL << i);
1520 ret = LIBUSB_ERROR_NOT_FOUND;
1524 usbi_mutex_unlock(&handle->lock);
1528 static int do_streams_ioctl(struct libusb_device_handle *handle, long req,
1529 uint32_t num_streams, unsigned char *endpoints, int num_endpoints)
1531 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1532 int r, fd = hpriv->fd;
1533 struct usbfs_streams *streams;
1535 if (num_endpoints > 30) /* Max 15 in + 15 out eps */
1536 return LIBUSB_ERROR_INVALID_PARAM;
1538 streams = malloc(sizeof(*streams) + num_endpoints);
1540 return LIBUSB_ERROR_NO_MEM;
1542 streams->num_streams = num_streams;
1543 streams->num_eps = num_endpoints;
1544 memcpy(streams->eps, endpoints, num_endpoints);
1546 r = ioctl(fd, req, streams);
1551 if (errno == ENOTTY)
1552 return LIBUSB_ERROR_NOT_SUPPORTED;
1553 else if (errno == EINVAL)
1554 return LIBUSB_ERROR_INVALID_PARAM;
1555 else if (errno == ENODEV)
1556 return LIBUSB_ERROR_NO_DEVICE;
1558 usbi_err(HANDLE_CTX(handle), "streams-ioctl failed, errno=%d", errno);
1559 return LIBUSB_ERROR_OTHER;
1564 static int op_alloc_streams(struct libusb_device_handle *handle,
1565 uint32_t num_streams, unsigned char *endpoints, int num_endpoints)
1567 return do_streams_ioctl(handle, IOCTL_USBFS_ALLOC_STREAMS,
1568 num_streams, endpoints, num_endpoints);
1571 static int op_free_streams(struct libusb_device_handle *handle,
1572 unsigned char *endpoints, int num_endpoints)
1574 return do_streams_ioctl(handle, IOCTL_USBFS_FREE_STREAMS, 0,
1575 endpoints, num_endpoints);
1578 static unsigned char *op_dev_mem_alloc(struct libusb_device_handle *handle,
1581 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1582 unsigned char *buffer;
1584 buffer = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, hpriv->fd, 0);
1585 if (buffer == MAP_FAILED) {
1586 usbi_err(HANDLE_CTX(handle), "alloc dev mem failed, errno=%d", errno);
1592 static int op_dev_mem_free(struct libusb_device_handle *handle,
1593 unsigned char *buffer, size_t len)
1595 if (munmap(buffer, len) != 0) {
1596 usbi_err(HANDLE_CTX(handle), "free dev mem failed, errno=%d", errno);
1597 return LIBUSB_ERROR_OTHER;
1599 return LIBUSB_SUCCESS;
1603 static int op_kernel_driver_active(struct libusb_device_handle *handle,
1606 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1608 struct usbfs_getdriver getdrv;
1611 getdrv.interface = interface;
1612 r = ioctl(fd, IOCTL_USBFS_GETDRIVER, &getdrv);
1614 if (errno == ENODATA)
1616 else if (errno == ENODEV)
1617 return LIBUSB_ERROR_NO_DEVICE;
1619 usbi_err(HANDLE_CTX(handle), "get driver failed, errno=%d", errno);
1620 return LIBUSB_ERROR_OTHER;
1623 return strcmp(getdrv.driver, "usbfs") != 0;
1626 static int op_detach_kernel_driver(struct libusb_device_handle *handle,
1629 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1631 struct usbfs_ioctl command;
1632 struct usbfs_getdriver getdrv;
1635 command.ifno = interface;
1636 command.ioctl_code = IOCTL_USBFS_DISCONNECT;
1637 command.data = NULL;
1639 getdrv.interface = interface;
1640 r = ioctl(fd, IOCTL_USBFS_GETDRIVER, &getdrv);
1641 if (r == 0 && !strcmp(getdrv.driver, "usbfs"))
1642 return LIBUSB_ERROR_NOT_FOUND;
1644 r = ioctl(fd, IOCTL_USBFS_IOCTL, &command);
1646 if (errno == ENODATA)
1647 return LIBUSB_ERROR_NOT_FOUND;
1648 else if (errno == EINVAL)
1649 return LIBUSB_ERROR_INVALID_PARAM;
1650 else if (errno == ENODEV)
1651 return LIBUSB_ERROR_NO_DEVICE;
1653 usbi_err(HANDLE_CTX(handle), "detach failed, errno=%d", errno);
1654 return LIBUSB_ERROR_OTHER;
1660 static int op_attach_kernel_driver(struct libusb_device_handle *handle,
1663 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1665 struct usbfs_ioctl command;
1668 command.ifno = interface;
1669 command.ioctl_code = IOCTL_USBFS_CONNECT;
1670 command.data = NULL;
1672 r = ioctl(fd, IOCTL_USBFS_IOCTL, &command);
1674 if (errno == ENODATA)
1675 return LIBUSB_ERROR_NOT_FOUND;
1676 else if (errno == EINVAL)
1677 return LIBUSB_ERROR_INVALID_PARAM;
1678 else if (errno == ENODEV)
1679 return LIBUSB_ERROR_NO_DEVICE;
1680 else if (errno == EBUSY)
1681 return LIBUSB_ERROR_BUSY;
1683 usbi_err(HANDLE_CTX(handle), "attach failed, errno=%d", errno);
1684 return LIBUSB_ERROR_OTHER;
1685 } else if (r == 0) {
1686 return LIBUSB_ERROR_NOT_FOUND;
1692 static int detach_kernel_driver_and_claim(struct libusb_device_handle *handle,
1695 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1696 struct usbfs_disconnect_claim dc;
1697 int r, fd = hpriv->fd;
1699 dc.interface = interface;
1700 strcpy(dc.driver, "usbfs");
1701 dc.flags = USBFS_DISCONNECT_CLAIM_EXCEPT_DRIVER;
1702 r = ioctl(fd, IOCTL_USBFS_DISCONNECT_CLAIM, &dc);
1709 return LIBUSB_ERROR_BUSY;
1711 return LIBUSB_ERROR_INVALID_PARAM;
1713 return LIBUSB_ERROR_NO_DEVICE;
1715 usbi_err(HANDLE_CTX(handle), "disconnect-and-claim failed, errno=%d", errno);
1716 return LIBUSB_ERROR_OTHER;
1719 /* Fallback code for kernels which don't support the
1720 disconnect-and-claim ioctl */
1721 r = op_detach_kernel_driver(handle, interface);
1722 if (r != 0 && r != LIBUSB_ERROR_NOT_FOUND)
1725 return claim_interface(handle, interface);
1728 static int op_claim_interface(struct libusb_device_handle *handle, int iface)
1730 if (handle->auto_detach_kernel_driver)
1731 return detach_kernel_driver_and_claim(handle, iface);
1733 return claim_interface(handle, iface);
1736 static int op_release_interface(struct libusb_device_handle *handle, int iface)
1740 r = release_interface(handle, iface);
1744 if (handle->auto_detach_kernel_driver)
1745 op_attach_kernel_driver(handle, iface);
1750 static void op_destroy_device(struct libusb_device *dev)
1752 struct linux_device_priv *priv = usbi_get_device_priv(dev);
1754 free(priv->descriptors);
1755 free(priv->sysfs_dir);
1758 /* URBs are discarded in reverse order of submission to avoid races. */
1759 static int discard_urbs(struct usbi_transfer *itransfer, int first, int last_plus_one)
1761 struct libusb_transfer *transfer =
1762 USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
1763 struct linux_transfer_priv *tpriv = usbi_get_transfer_priv(itransfer);
1764 struct linux_device_handle_priv *hpriv =
1765 usbi_get_device_handle_priv(transfer->dev_handle);
1767 struct usbfs_urb *urb;
1769 for (i = last_plus_one - 1; i >= first; i--) {
1770 if (transfer->type == LIBUSB_TRANSFER_TYPE_ISOCHRONOUS)
1771 urb = tpriv->iso_urbs[i];
1773 urb = &tpriv->urbs[i];
1775 if (ioctl(hpriv->fd, IOCTL_USBFS_DISCARDURB, urb) == 0)
1778 if (errno == EINVAL) {
1779 usbi_dbg("URB not found --> assuming ready to be reaped");
1780 if (i == (last_plus_one - 1))
1781 ret = LIBUSB_ERROR_NOT_FOUND;
1782 } else if (errno == ENODEV) {
1783 usbi_dbg("Device not found for URB --> assuming ready to be reaped");
1784 ret = LIBUSB_ERROR_NO_DEVICE;
1786 usbi_warn(TRANSFER_CTX(transfer), "unrecognised discard errno %d", errno);
1787 ret = LIBUSB_ERROR_OTHER;
1793 static void free_iso_urbs(struct linux_transfer_priv *tpriv)
1797 for (i = 0; i < tpriv->num_urbs; i++) {
1798 struct usbfs_urb *urb = tpriv->iso_urbs[i];
1805 free(tpriv->iso_urbs);
1806 tpriv->iso_urbs = NULL;
1809 static int submit_bulk_transfer(struct usbi_transfer *itransfer)
1811 struct libusb_transfer *transfer =
1812 USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
1813 struct linux_transfer_priv *tpriv = usbi_get_transfer_priv(itransfer);
1814 struct linux_device_handle_priv *hpriv =
1815 usbi_get_device_handle_priv(transfer->dev_handle);
1816 struct usbfs_urb *urbs;
1817 int is_out = IS_XFEROUT(transfer);
1818 int bulk_buffer_len, use_bulk_continuation;
1820 int last_urb_partial = 0;
1825 * Older versions of usbfs place a 16kb limit on bulk URBs. We work
1826 * around this by splitting large transfers into 16k blocks, and then
1827 * submit all urbs at once. it would be simpler to submit one urb at
1828 * a time, but there is a big performance gain doing it this way.
1830 * Newer versions lift the 16k limit (USBFS_CAP_NO_PACKET_SIZE_LIM),
1831 * using arbritary large transfers can still be a bad idea though, as
1832 * the kernel needs to allocate physical contiguous memory for this,
1833 * which may fail for large buffers.
1835 * The kernel solves this problem by splitting the transfer into
1836 * blocks itself when the host-controller is scatter-gather capable
1837 * (USBFS_CAP_BULK_SCATTER_GATHER), which most controllers are.
1839 * Last, there is the issue of short-transfers when splitting, for
1840 * short split-transfers to work reliable USBFS_CAP_BULK_CONTINUATION
1841 * is needed, but this is not always available.
1843 if (hpriv->caps & USBFS_CAP_BULK_SCATTER_GATHER) {
1844 /* Good! Just submit everything in one go */
1845 bulk_buffer_len = transfer->length ? transfer->length : 1;
1846 use_bulk_continuation = 0;
1847 } else if (hpriv->caps & USBFS_CAP_BULK_CONTINUATION) {
1848 /* Split the transfers and use bulk-continuation to
1849 avoid issues with short-transfers */
1850 bulk_buffer_len = MAX_BULK_BUFFER_LENGTH;
1851 use_bulk_continuation = 1;
1852 } else if (hpriv->caps & USBFS_CAP_NO_PACKET_SIZE_LIM) {
1853 /* Don't split, assume the kernel can alloc the buffer
1854 (otherwise the submit will fail with -ENOMEM) */
1855 bulk_buffer_len = transfer->length ? transfer->length : 1;
1856 use_bulk_continuation = 0;
1858 /* Bad, splitting without bulk-continuation, short transfers
1859 which end before the last urb will not work reliable! */
1860 /* Note we don't warn here as this is "normal" on kernels <
1861 2.6.32 and not a problem for most applications */
1862 bulk_buffer_len = MAX_BULK_BUFFER_LENGTH;
1863 use_bulk_continuation = 0;
1866 num_urbs = transfer->length / bulk_buffer_len;
1868 if (transfer->length == 0) {
1870 } else if ((transfer->length % bulk_buffer_len) > 0) {
1871 last_urb_partial = 1;
1874 usbi_dbg("need %d urbs for new transfer with length %d", num_urbs, transfer->length);
1875 urbs = calloc(num_urbs, sizeof(*urbs));
1877 return LIBUSB_ERROR_NO_MEM;
1879 tpriv->num_urbs = num_urbs;
1880 tpriv->num_retired = 0;
1881 tpriv->reap_action = NORMAL;
1882 tpriv->reap_status = LIBUSB_TRANSFER_COMPLETED;
1884 for (i = 0; i < num_urbs; i++) {
1885 struct usbfs_urb *urb = &urbs[i];
1887 urb->usercontext = itransfer;
1888 switch (transfer->type) {
1889 case LIBUSB_TRANSFER_TYPE_BULK:
1890 urb->type = USBFS_URB_TYPE_BULK;
1893 case LIBUSB_TRANSFER_TYPE_BULK_STREAM:
1894 urb->type = USBFS_URB_TYPE_BULK;
1895 urb->stream_id = itransfer->stream_id;
1897 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1898 urb->type = USBFS_URB_TYPE_INTERRUPT;
1901 urb->endpoint = transfer->endpoint;
1902 urb->buffer = transfer->buffer + (i * bulk_buffer_len);
1904 /* don't set the short not ok flag for the last URB */
1905 if (use_bulk_continuation && !is_out && (i < num_urbs - 1))
1906 urb->flags = USBFS_URB_SHORT_NOT_OK;
1908 if (i == num_urbs - 1 && last_urb_partial)
1909 urb->buffer_length = transfer->length % bulk_buffer_len;
1910 else if (transfer->length == 0)
1911 urb->buffer_length = 0;
1913 urb->buffer_length = bulk_buffer_len;
1915 if (i > 0 && use_bulk_continuation)
1916 urb->flags |= USBFS_URB_BULK_CONTINUATION;
1918 /* we have already checked that the flag is supported */
1919 if (is_out && i == num_urbs - 1 &&
1920 (transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET))
1921 urb->flags |= USBFS_URB_ZERO_PACKET;
1923 r = ioctl(hpriv->fd, IOCTL_USBFS_SUBMITURB, urb);
1927 if (errno == ENODEV) {
1928 r = LIBUSB_ERROR_NO_DEVICE;
1929 } else if (errno == ENOMEM) {
1930 r = LIBUSB_ERROR_NO_MEM;
1932 usbi_err(TRANSFER_CTX(transfer), "submiturb failed, errno=%d", errno);
1933 r = LIBUSB_ERROR_IO;
1936 /* if the first URB submission fails, we can simply free up and
1937 * return failure immediately. */
1939 usbi_dbg("first URB failed, easy peasy");
1945 /* if it's not the first URB that failed, the situation is a bit
1946 * tricky. we may need to discard all previous URBs. there are
1948 * - discarding is asynchronous - discarded urbs will be reaped
1949 * later. the user must not have freed the transfer when the
1950 * discarded URBs are reaped, otherwise libusb will be using
1952 * - the earlier URBs may have completed successfully and we do
1953 * not want to throw away any data.
1954 * - this URB failing may be no error; EREMOTEIO means that
1955 * this transfer simply didn't need all the URBs we submitted
1956 * so, we report that the transfer was submitted successfully and
1957 * in case of error we discard all previous URBs. later when
1958 * the final reap completes we can report error to the user,
1959 * or success if an earlier URB was completed successfully.
1961 tpriv->reap_action = errno == EREMOTEIO ? COMPLETED_EARLY : SUBMIT_FAILED;
1963 /* The URBs we haven't submitted yet we count as already
1965 tpriv->num_retired += num_urbs - i;
1967 /* If we completed short then don't try to discard. */
1968 if (tpriv->reap_action == COMPLETED_EARLY)
1971 discard_urbs(itransfer, 0, i);
1973 usbi_dbg("reporting successful submission but waiting for %d "
1974 "discards before reporting error", i);
1981 static int submit_iso_transfer(struct usbi_transfer *itransfer)
1983 struct libusb_transfer *transfer =
1984 USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
1985 struct linux_transfer_priv *tpriv = usbi_get_transfer_priv(itransfer);
1986 struct linux_device_handle_priv *hpriv =
1987 usbi_get_device_handle_priv(transfer->dev_handle);
1988 struct usbfs_urb **urbs;
1989 int num_packets = transfer->num_iso_packets;
1990 int num_packets_remaining;
1993 unsigned int packet_len;
1994 unsigned int total_len = 0;
1995 unsigned char *urb_buffer = transfer->buffer;
1997 if (num_packets < 1)
1998 return LIBUSB_ERROR_INVALID_PARAM;
2000 /* usbfs places arbitrary limits on iso URBs. this limit has changed
2001 * at least three times, but we attempt to detect this limit during
2002 * init and check it here. if the kernel rejects the request due to
2003 * its size, we return an error indicating such to the user.
2005 for (i = 0; i < num_packets; i++) {
2006 packet_len = transfer->iso_packet_desc[i].length;
2008 if (packet_len > max_iso_packet_len) {
2009 usbi_warn(TRANSFER_CTX(transfer),
2010 "iso packet length of %u bytes exceeds maximum of %u bytes",
2011 packet_len, max_iso_packet_len);
2012 return LIBUSB_ERROR_INVALID_PARAM;
2015 total_len += packet_len;
2018 if (transfer->length < (int)total_len)
2019 return LIBUSB_ERROR_INVALID_PARAM;
2021 /* usbfs limits the number of iso packets per URB */
2022 num_urbs = (num_packets + (MAX_ISO_PACKETS_PER_URB - 1)) / MAX_ISO_PACKETS_PER_URB;
2024 usbi_dbg("need %d urbs for new transfer with length %d", num_urbs, transfer->length);
2026 urbs = calloc(num_urbs, sizeof(*urbs));
2028 return LIBUSB_ERROR_NO_MEM;
2030 tpriv->iso_urbs = urbs;
2031 tpriv->num_urbs = num_urbs;
2032 tpriv->num_retired = 0;
2033 tpriv->reap_action = NORMAL;
2034 tpriv->iso_packet_offset = 0;
2036 /* allocate + initialize each URB with the correct number of packets */
2037 num_packets_remaining = num_packets;
2038 for (i = 0, j = 0; i < num_urbs; i++) {
2039 int num_packets_in_urb = MIN(num_packets_remaining, MAX_ISO_PACKETS_PER_URB);
2040 struct usbfs_urb *urb;
2044 alloc_size = sizeof(*urb)
2045 + (num_packets_in_urb * sizeof(struct usbfs_iso_packet_desc));
2046 urb = calloc(1, alloc_size);
2048 free_iso_urbs(tpriv);
2049 return LIBUSB_ERROR_NO_MEM;
2053 /* populate packet lengths */
2054 for (k = 0; k < num_packets_in_urb; j++, k++) {
2055 packet_len = transfer->iso_packet_desc[j].length;
2056 urb->buffer_length += packet_len;
2057 urb->iso_frame_desc[k].length = packet_len;
2060 urb->usercontext = itransfer;
2061 urb->type = USBFS_URB_TYPE_ISO;
2062 /* FIXME: interface for non-ASAP data? */
2063 urb->flags = USBFS_URB_ISO_ASAP;
2064 urb->endpoint = transfer->endpoint;
2065 urb->number_of_packets = num_packets_in_urb;
2066 urb->buffer = urb_buffer;
2068 urb_buffer += urb->buffer_length;
2069 num_packets_remaining -= num_packets_in_urb;
2073 for (i = 0; i < num_urbs; i++) {
2074 int r = ioctl(hpriv->fd, IOCTL_USBFS_SUBMITURB, urbs[i]);
2079 if (errno == ENODEV) {
2080 r = LIBUSB_ERROR_NO_DEVICE;
2081 } else if (errno == EINVAL) {
2082 usbi_warn(TRANSFER_CTX(transfer), "submiturb failed, transfer too large");
2083 r = LIBUSB_ERROR_INVALID_PARAM;
2084 } else if (errno == EMSGSIZE) {
2085 usbi_warn(TRANSFER_CTX(transfer), "submiturb failed, iso packet length too large");
2086 r = LIBUSB_ERROR_INVALID_PARAM;
2088 usbi_err(TRANSFER_CTX(transfer), "submiturb failed, errno=%d", errno);
2089 r = LIBUSB_ERROR_IO;
2092 /* if the first URB submission fails, we can simply free up and
2093 * return failure immediately. */
2095 usbi_dbg("first URB failed, easy peasy");
2096 free_iso_urbs(tpriv);
2100 /* if it's not the first URB that failed, the situation is a bit
2101 * tricky. we must discard all previous URBs. there are
2103 * - discarding is asynchronous - discarded urbs will be reaped
2104 * later. the user must not have freed the transfer when the
2105 * discarded URBs are reaped, otherwise libusb will be using
2107 * - the earlier URBs may have completed successfully and we do
2108 * not want to throw away any data.
2109 * so, in this case we discard all the previous URBs BUT we report
2110 * that the transfer was submitted successfully. then later when
2111 * the final discard completes we can report error to the user.
2113 tpriv->reap_action = SUBMIT_FAILED;
2115 /* The URBs we haven't submitted yet we count as already
2117 tpriv->num_retired = num_urbs - i;
2118 discard_urbs(itransfer, 0, i);
2120 usbi_dbg("reporting successful submission but waiting for %d "
2121 "discards before reporting error", i);
2128 static int submit_control_transfer(struct usbi_transfer *itransfer)
2130 struct linux_transfer_priv *tpriv = usbi_get_transfer_priv(itransfer);
2131 struct libusb_transfer *transfer =
2132 USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
2133 struct linux_device_handle_priv *hpriv =
2134 usbi_get_device_handle_priv(transfer->dev_handle);
2135 struct usbfs_urb *urb;
2138 if (transfer->length - LIBUSB_CONTROL_SETUP_SIZE > MAX_CTRL_BUFFER_LENGTH)
2139 return LIBUSB_ERROR_INVALID_PARAM;
2141 urb = calloc(1, sizeof(*urb));
2143 return LIBUSB_ERROR_NO_MEM;
2145 tpriv->num_urbs = 1;
2146 tpriv->reap_action = NORMAL;
2148 urb->usercontext = itransfer;
2149 urb->type = USBFS_URB_TYPE_CONTROL;
2150 urb->endpoint = transfer->endpoint;
2151 urb->buffer = transfer->buffer;
2152 urb->buffer_length = transfer->length;
2154 r = ioctl(hpriv->fd, IOCTL_USBFS_SUBMITURB, urb);
2158 if (errno == ENODEV)
2159 return LIBUSB_ERROR_NO_DEVICE;
2161 usbi_err(TRANSFER_CTX(transfer), "submiturb failed, errno=%d", errno);
2162 return LIBUSB_ERROR_IO;
2167 static int op_submit_transfer(struct usbi_transfer *itransfer)
2169 struct libusb_transfer *transfer =
2170 USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
2172 switch (transfer->type) {
2173 case LIBUSB_TRANSFER_TYPE_CONTROL:
2174 return submit_control_transfer(itransfer);
2175 case LIBUSB_TRANSFER_TYPE_BULK:
2176 case LIBUSB_TRANSFER_TYPE_BULK_STREAM:
2177 return submit_bulk_transfer(itransfer);
2178 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
2179 return submit_bulk_transfer(itransfer);
2180 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
2181 return submit_iso_transfer(itransfer);
2183 usbi_err(TRANSFER_CTX(transfer), "unknown transfer type %u", transfer->type);
2184 return LIBUSB_ERROR_INVALID_PARAM;
2188 static int op_cancel_transfer(struct usbi_transfer *itransfer)
2190 struct linux_transfer_priv *tpriv = usbi_get_transfer_priv(itransfer);
2191 struct libusb_transfer *transfer =
2192 USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
2196 return LIBUSB_ERROR_NOT_FOUND;
2198 r = discard_urbs(itransfer, 0, tpriv->num_urbs);
2202 switch (transfer->type) {
2203 case LIBUSB_TRANSFER_TYPE_BULK:
2204 case LIBUSB_TRANSFER_TYPE_BULK_STREAM:
2205 if (tpriv->reap_action == ERROR)
2207 /* else, fall through */
2209 tpriv->reap_action = CANCELLED;
2215 static void op_clear_transfer_priv(struct usbi_transfer *itransfer)
2217 struct libusb_transfer *transfer =
2218 USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
2219 struct linux_transfer_priv *tpriv = usbi_get_transfer_priv(itransfer);
2221 switch (transfer->type) {
2222 case LIBUSB_TRANSFER_TYPE_CONTROL:
2223 case LIBUSB_TRANSFER_TYPE_BULK:
2224 case LIBUSB_TRANSFER_TYPE_BULK_STREAM:
2225 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
2231 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
2232 if (tpriv->iso_urbs) {
2233 free_iso_urbs(tpriv);
2234 tpriv->iso_urbs = NULL;
2238 usbi_err(TRANSFER_CTX(transfer), "unknown transfer type %u", transfer->type);
2242 static int handle_bulk_completion(struct usbi_transfer *itransfer,
2243 struct usbfs_urb *urb)
2245 struct linux_transfer_priv *tpriv = usbi_get_transfer_priv(itransfer);
2246 struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
2247 int urb_idx = urb - tpriv->urbs;
2249 usbi_mutex_lock(&itransfer->lock);
2250 usbi_dbg("handling completion status %d of bulk urb %d/%d", urb->status,
2251 urb_idx + 1, tpriv->num_urbs);
2253 tpriv->num_retired++;
2255 if (tpriv->reap_action != NORMAL) {
2256 /* cancelled, submit_fail, or completed early */
2257 usbi_dbg("abnormal reap: urb status %d", urb->status);
2259 /* even though we're in the process of cancelling, it's possible that
2260 * we may receive some data in these URBs that we don't want to lose.
2262 * 1. while the kernel is cancelling all the packets that make up an
2263 * URB, a few of them might complete. so we get back a successful
2264 * cancellation *and* some data.
2265 * 2. we receive a short URB which marks the early completion condition,
2266 * so we start cancelling the remaining URBs. however, we're too
2267 * slow and another URB completes (or at least completes partially).
2268 * (this can't happen since we always use BULK_CONTINUATION.)
2270 * When this happens, our objectives are not to lose any "surplus" data,
2271 * and also to stick it at the end of the previously-received data
2272 * (closing any holes), so that libusb reports the total amount of
2273 * transferred data and presents it in a contiguous chunk.
2275 if (urb->actual_length > 0) {
2276 unsigned char *target = transfer->buffer + itransfer->transferred;
2278 usbi_dbg("received %d bytes of surplus data", urb->actual_length);
2279 if (urb->buffer != target) {
2280 usbi_dbg("moving surplus data from offset %zu to offset %zu",
2281 (unsigned char *) urb->buffer - transfer->buffer,
2282 target - transfer->buffer);
2283 memmove(target, urb->buffer, urb->actual_length);
2285 itransfer->transferred += urb->actual_length;
2288 if (tpriv->num_retired == tpriv->num_urbs) {
2289 usbi_dbg("abnormal reap: last URB handled, reporting");
2290 if (tpriv->reap_action != COMPLETED_EARLY &&
2291 tpriv->reap_status == LIBUSB_TRANSFER_COMPLETED)
2292 tpriv->reap_status = LIBUSB_TRANSFER_ERROR;
2298 itransfer->transferred += urb->actual_length;
2300 /* Many of these errors can occur on *any* urb of a multi-urb
2301 * transfer. When they do, we tear down the rest of the transfer.
2303 switch (urb->status) {
2306 case -EREMOTEIO: /* short transfer */
2308 case -ENOENT: /* cancelled */
2313 usbi_dbg("device removed");
2314 tpriv->reap_status = LIBUSB_TRANSFER_NO_DEVICE;
2315 goto cancel_remaining;
2317 usbi_dbg("detected endpoint stall");
2318 if (tpriv->reap_status == LIBUSB_TRANSFER_COMPLETED)
2319 tpriv->reap_status = LIBUSB_TRANSFER_STALL;
2320 goto cancel_remaining;
2322 /* overflow can only ever occur in the last urb */
2323 usbi_dbg("overflow, actual_length=%d", urb->actual_length);
2324 if (tpriv->reap_status == LIBUSB_TRANSFER_COMPLETED)
2325 tpriv->reap_status = LIBUSB_TRANSFER_OVERFLOW;
2332 usbi_dbg("low-level bus error %d", urb->status);
2333 tpriv->reap_action = ERROR;
2334 goto cancel_remaining;
2336 usbi_warn(ITRANSFER_CTX(itransfer), "unrecognised urb status %d", urb->status);
2337 tpriv->reap_action = ERROR;
2338 goto cancel_remaining;
2341 /* if we've reaped all urbs or we got less data than requested then we're
2343 if (tpriv->num_retired == tpriv->num_urbs) {
2344 usbi_dbg("all URBs in transfer reaped --> complete!");
2346 } else if (urb->actual_length < urb->buffer_length) {
2347 usbi_dbg("short transfer %d/%d --> complete!",
2348 urb->actual_length, urb->buffer_length);
2349 if (tpriv->reap_action == NORMAL)
2350 tpriv->reap_action = COMPLETED_EARLY;
2356 if (tpriv->reap_action == ERROR && tpriv->reap_status == LIBUSB_TRANSFER_COMPLETED)
2357 tpriv->reap_status = LIBUSB_TRANSFER_ERROR;
2359 if (tpriv->num_retired == tpriv->num_urbs) /* nothing to cancel */
2362 /* cancel remaining urbs and wait for their completion before
2363 * reporting results */
2364 discard_urbs(itransfer, urb_idx + 1, tpriv->num_urbs);
2367 usbi_mutex_unlock(&itransfer->lock);
2373 usbi_mutex_unlock(&itransfer->lock);
2374 return tpriv->reap_action == CANCELLED ?
2375 usbi_handle_transfer_cancellation(itransfer) :
2376 usbi_handle_transfer_completion(itransfer, tpriv->reap_status);
2379 static int handle_iso_completion(struct usbi_transfer *itransfer,
2380 struct usbfs_urb *urb)
2382 struct libusb_transfer *transfer =
2383 USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
2384 struct linux_transfer_priv *tpriv = usbi_get_transfer_priv(itransfer);
2385 int num_urbs = tpriv->num_urbs;
2388 enum libusb_transfer_status status = LIBUSB_TRANSFER_COMPLETED;
2390 usbi_mutex_lock(&itransfer->lock);
2391 for (i = 0; i < num_urbs; i++) {
2392 if (urb == tpriv->iso_urbs[i]) {
2398 usbi_err(TRANSFER_CTX(transfer), "could not locate urb!");
2399 usbi_mutex_unlock(&itransfer->lock);
2400 return LIBUSB_ERROR_NOT_FOUND;
2403 usbi_dbg("handling completion status %d of iso urb %d/%d", urb->status,
2406 /* copy isochronous results back in */
2408 for (i = 0; i < urb->number_of_packets; i++) {
2409 struct usbfs_iso_packet_desc *urb_desc = &urb->iso_frame_desc[i];
2410 struct libusb_iso_packet_descriptor *lib_desc =
2411 &transfer->iso_packet_desc[tpriv->iso_packet_offset++];
2413 lib_desc->status = LIBUSB_TRANSFER_COMPLETED;
2414 switch (urb_desc->status) {
2417 case -ENOENT: /* cancelled */
2422 usbi_dbg("packet %d - device removed", i);
2423 lib_desc->status = LIBUSB_TRANSFER_NO_DEVICE;
2426 usbi_dbg("packet %d - detected endpoint stall", i);
2427 lib_desc->status = LIBUSB_TRANSFER_STALL;
2430 usbi_dbg("packet %d - overflow error", i);
2431 lib_desc->status = LIBUSB_TRANSFER_OVERFLOW;
2439 usbi_dbg("packet %d - low-level USB error %d", i, urb_desc->status);
2440 lib_desc->status = LIBUSB_TRANSFER_ERROR;
2443 usbi_warn(TRANSFER_CTX(transfer), "packet %d - unrecognised urb status %d",
2444 i, urb_desc->status);
2445 lib_desc->status = LIBUSB_TRANSFER_ERROR;
2448 lib_desc->actual_length = urb_desc->actual_length;
2451 tpriv->num_retired++;
2453 if (tpriv->reap_action != NORMAL) { /* cancelled or submit_fail */
2454 usbi_dbg("CANCEL: urb status %d", urb->status);
2456 if (tpriv->num_retired == num_urbs) {
2457 usbi_dbg("CANCEL: last URB handled, reporting");
2458 free_iso_urbs(tpriv);
2459 if (tpriv->reap_action == CANCELLED) {
2460 usbi_mutex_unlock(&itransfer->lock);
2461 return usbi_handle_transfer_cancellation(itransfer);
2463 usbi_mutex_unlock(&itransfer->lock);
2464 return usbi_handle_transfer_completion(itransfer, LIBUSB_TRANSFER_ERROR);
2470 switch (urb->status) {
2473 case -ENOENT: /* cancelled */
2477 usbi_dbg("device removed");
2478 status = LIBUSB_TRANSFER_NO_DEVICE;
2481 usbi_warn(TRANSFER_CTX(transfer), "unrecognised urb status %d", urb->status);
2482 status = LIBUSB_TRANSFER_ERROR;
2486 /* if we've reaped all urbs then we're done */
2487 if (tpriv->num_retired == num_urbs) {
2488 usbi_dbg("all URBs in transfer reaped --> complete!");
2489 free_iso_urbs(tpriv);
2490 usbi_mutex_unlock(&itransfer->lock);
2491 return usbi_handle_transfer_completion(itransfer, status);
2495 usbi_mutex_unlock(&itransfer->lock);
2499 static int handle_control_completion(struct usbi_transfer *itransfer,
2500 struct usbfs_urb *urb)
2502 struct linux_transfer_priv *tpriv = usbi_get_transfer_priv(itransfer);
2505 usbi_mutex_lock(&itransfer->lock);
2506 usbi_dbg("handling completion status %d", urb->status);
2508 itransfer->transferred += urb->actual_length;
2510 if (tpriv->reap_action == CANCELLED) {
2511 if (urb->status && urb->status != -ENOENT)
2512 usbi_warn(ITRANSFER_CTX(itransfer), "cancel: unrecognised urb status %d",
2516 usbi_mutex_unlock(&itransfer->lock);
2517 return usbi_handle_transfer_cancellation(itransfer);
2520 switch (urb->status) {
2522 status = LIBUSB_TRANSFER_COMPLETED;
2524 case -ENOENT: /* cancelled */
2525 status = LIBUSB_TRANSFER_CANCELLED;
2529 usbi_dbg("device removed");
2530 status = LIBUSB_TRANSFER_NO_DEVICE;
2533 usbi_dbg("unsupported control request");
2534 status = LIBUSB_TRANSFER_STALL;
2537 usbi_dbg("overflow, actual_length=%d", urb->actual_length);
2538 status = LIBUSB_TRANSFER_OVERFLOW;
2545 usbi_dbg("low-level bus error %d", urb->status);
2546 status = LIBUSB_TRANSFER_ERROR;
2549 usbi_warn(ITRANSFER_CTX(itransfer), "unrecognised urb status %d", urb->status);
2550 status = LIBUSB_TRANSFER_ERROR;
2556 usbi_mutex_unlock(&itransfer->lock);
2557 return usbi_handle_transfer_completion(itransfer, status);
2560 static int reap_for_handle(struct libusb_device_handle *handle)
2562 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
2564 struct usbfs_urb *urb = NULL;
2565 struct usbi_transfer *itransfer;
2566 struct libusb_transfer *transfer;
2568 r = ioctl(hpriv->fd, IOCTL_USBFS_REAPURBNDELAY, &urb);
2570 if (errno == EAGAIN)
2572 if (errno == ENODEV)
2573 return LIBUSB_ERROR_NO_DEVICE;
2575 usbi_err(HANDLE_CTX(handle), "reap failed, errno=%d", errno);
2576 return LIBUSB_ERROR_IO;
2579 itransfer = urb->usercontext;
2580 transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
2582 usbi_dbg("urb type=%u status=%d transferred=%d", urb->type, urb->status, urb->actual_length);
2584 switch (transfer->type) {
2585 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
2586 return handle_iso_completion(itransfer, urb);
2587 case LIBUSB_TRANSFER_TYPE_BULK:
2588 case LIBUSB_TRANSFER_TYPE_BULK_STREAM:
2589 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
2590 return handle_bulk_completion(itransfer, urb);
2591 case LIBUSB_TRANSFER_TYPE_CONTROL:
2592 return handle_control_completion(itransfer, urb);
2594 usbi_err(HANDLE_CTX(handle), "unrecognised transfer type %u", transfer->type);
2595 return LIBUSB_ERROR_OTHER;
2599 static int op_handle_events(struct libusb_context *ctx,
2600 struct pollfd *fds, usbi_nfds_t nfds, int num_ready)
2605 usbi_mutex_lock(&ctx->open_devs_lock);
2606 for (n = 0; n < nfds && num_ready > 0; n++) {
2607 struct pollfd *pollfd = &fds[n];
2608 struct libusb_device_handle *handle;
2609 struct linux_device_handle_priv *hpriv = NULL;
2611 if (!pollfd->revents)
2615 list_for_each_entry(handle, &ctx->open_devs, list, struct libusb_device_handle) {
2616 hpriv = usbi_get_device_handle_priv(handle);
2617 if (hpriv->fd == pollfd->fd)
2621 if (!hpriv || hpriv->fd != pollfd->fd) {
2622 usbi_err(ctx, "cannot find handle for fd %d",
2627 if (pollfd->revents & POLLERR) {
2628 /* remove the fd from the pollfd set so that it doesn't continuously
2629 * trigger an event, and flag that it has been removed so op_close()
2630 * doesn't try to remove it a second time */
2631 usbi_remove_pollfd(HANDLE_CTX(handle), hpriv->fd);
2632 hpriv->fd_removed = 1;
2634 /* device will still be marked as attached if hotplug monitor thread
2635 * hasn't processed remove event yet */
2636 usbi_mutex_static_lock(&linux_hotplug_lock);
2637 if (handle->dev->attached)
2638 linux_device_disconnected(handle->dev->bus_number,
2639 handle->dev->device_address);
2640 usbi_mutex_static_unlock(&linux_hotplug_lock);
2642 if (hpriv->caps & USBFS_CAP_REAP_AFTER_DISCONNECT) {
2644 r = reap_for_handle(handle);
2648 usbi_handle_disconnect(handle);
2653 r = reap_for_handle(handle);
2655 if (r == 1 || r == LIBUSB_ERROR_NO_DEVICE)
2663 usbi_mutex_unlock(&ctx->open_devs_lock);
2667 const struct usbi_os_backend usbi_backend = {
2668 .name = "Linux usbfs",
2669 .caps = USBI_CAP_HAS_HID_ACCESS|USBI_CAP_SUPPORTS_DETACH_KERNEL_DRIVER,
2672 .hotplug_poll = op_hotplug_poll,
2673 .get_device_descriptor = op_get_device_descriptor,
2674 .get_active_config_descriptor = op_get_active_config_descriptor,
2675 .get_config_descriptor = op_get_config_descriptor,
2676 .get_config_descriptor_by_value = op_get_config_descriptor_by_value,
2678 .wrap_sys_device = op_wrap_sys_device,
2681 .get_configuration = op_get_configuration,
2682 .set_configuration = op_set_configuration,
2683 .claim_interface = op_claim_interface,
2684 .release_interface = op_release_interface,
2686 .set_interface_altsetting = op_set_interface,
2687 .clear_halt = op_clear_halt,
2688 .reset_device = op_reset_device,
2690 .alloc_streams = op_alloc_streams,
2691 .free_streams = op_free_streams,
2693 .dev_mem_alloc = op_dev_mem_alloc,
2694 .dev_mem_free = op_dev_mem_free,
2696 .kernel_driver_active = op_kernel_driver_active,
2697 .detach_kernel_driver = op_detach_kernel_driver,
2698 .attach_kernel_driver = op_attach_kernel_driver,
2700 .destroy_device = op_destroy_device,
2702 .submit_transfer = op_submit_transfer,
2703 .cancel_transfer = op_cancel_transfer,
2704 .clear_transfer_priv = op_clear_transfer_priv,
2706 .handle_events = op_handle_events,
2708 .device_priv_size = sizeof(struct linux_device_priv),
2709 .device_handle_priv_size = sizeof(struct linux_device_handle_priv),
2710 .transfer_priv_size = sizeof(struct linux_transfer_priv),