2 * Linux usbfs backend for libusb
3 * Copyright (C) 2007-2008 Daniel Drake <dsd@gentoo.org>
4 * Copyright (c) 2001 Johannes Erdfelt <johannes@erdfelt.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
31 #include <sys/ioctl.h>
33 #include <sys/types.h>
38 #include "linux_usbfs.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 was added in 2.6.23.
47 * - The "busnum" file was added in 2.6.22
48 * - The "devnum" file has been present since pre-2.6.18
49 * - the "bConfigurationValue" file has been present since pre-2.6.18
51 * If we have bConfigurationValue, busnum, and devnum, then we can determine
52 * the active configuration without having to open the usbfs node in RDWR mode.
53 * We assume this is the case if we see the busnum file (indicates 2.6.22+).
54 * The busnum file is important as that is the only way we can relate sysfs
55 * devices to usbfs nodes.
57 * If we also have descriptors, we can obtain the device descriptor and active
58 * configuration without touching usbfs at all.
61 static const char *usbfs_path = NULL;
63 /* do we have a busnum to relate devices? this also implies that we can read
64 * the active configuration through bConfigurationValue */
65 static int sysfs_can_relate_devices = -1;
67 /* do we have a descriptors file? */
68 static int sysfs_has_descriptors = -1;
70 struct linux_device_priv {
71 char sysfs_dir[SYSFS_DIR_LENGTH];
72 unsigned char *dev_descriptor;
73 unsigned char *config_descriptor;
76 struct linux_device_handle_priv {
82 /* submission failed after the first URB, so await cancellation/completion
83 * of all the others */
86 /* cancelled by user or timeout */
89 /* completed multi-URB transfer in non-final URB */
93 struct linux_transfer_priv {
95 struct usbfs_urb *urbs;
96 struct usbfs_urb **iso_urbs;
99 enum reap_action reap_action;
101 unsigned int awaiting_reap;
102 unsigned int awaiting_discard;
104 /* next iso packet in user-supplied transfer to be populated */
105 int iso_packet_offset;
108 static void __get_usbfs_path(struct libusb_device *dev, char *path)
110 snprintf(path, PATH_MAX, "%s/%03d/%03d", usbfs_path, dev->bus_number,
111 dev->device_address);
114 static struct linux_device_priv *__device_priv(struct libusb_device *dev)
116 return (struct linux_device_priv *) dev->os_priv;
119 static struct linux_device_handle_priv *__device_handle_priv(
120 struct libusb_device_handle *handle)
122 return (struct linux_device_handle_priv *) handle->os_priv;
125 static int check_usb_vfs(const char *dirname)
128 struct dirent *entry;
131 dir = opendir(dirname);
135 while ((entry = readdir(dir)) != NULL) {
136 if (entry->d_name[0] == '.')
139 /* We assume if we find any files that it must be the right place */
148 static const char *find_usbfs_path(void)
150 const char *path = "/dev/bus/usb";
151 const char *ret = NULL;
153 if (check_usb_vfs(path)) {
156 path = "/proc/bus/usb";
157 if (check_usb_vfs(path))
161 usbi_dbg("found usbfs at %s", ret);
165 static int op_init(void)
170 usbfs_path = find_usbfs_path();
172 usbi_err("could not find usbfs");
173 return LIBUSB_ERROR_OTHER;
176 r = stat(SYSFS_DEVICE_PATH, &statbuf);
177 if (r == 0 && S_ISDIR(statbuf.st_mode)) {
178 usbi_dbg("found usb devices in sysfs");
180 usbi_dbg("sysfs usb info not available");
181 sysfs_has_descriptors = 0;
182 sysfs_can_relate_devices = 0;
188 static int usbfs_get_device_descriptor(struct libusb_device *dev,
189 unsigned char *buffer)
191 struct linux_device_priv *priv = __device_priv(dev);
193 /* return cached copy */
194 memcpy(buffer, priv->dev_descriptor, DEVICE_DESC_LENGTH);
198 static int __open_sysfs_attr(struct libusb_device *dev, const char *attr)
200 struct linux_device_priv *priv = __device_priv(dev);
201 char filename[PATH_MAX + 1];
204 snprintf(filename, PATH_MAX, "%s/%s/%s",
205 SYSFS_DEVICE_PATH, priv->sysfs_dir, attr);
206 fd = open(filename, O_RDONLY);
208 usbi_err("open %s failed ret=%d errno=%d", filename, fd, errno);
209 return LIBUSB_ERROR_IO;
215 static int sysfs_get_device_descriptor(struct libusb_device *dev,
216 unsigned char *buffer)
221 /* sysfs provides access to an in-memory copy of the device descriptor,
222 * so we use that rather than keeping our own copy */
224 fd = __open_sysfs_attr(dev, "descriptors");
228 r = read(fd, buffer, DEVICE_DESC_LENGTH);;
231 usbi_err("read failed, ret=%d errno=%d", fd, errno);
232 return LIBUSB_ERROR_IO;
233 } else if (r < DEVICE_DESC_LENGTH) {
234 usbi_err("short read %d/%d", r, DEVICE_DESC_LENGTH);
235 return LIBUSB_ERROR_IO;
241 static int op_get_device_descriptor(struct libusb_device *dev,
242 unsigned char *buffer, int *host_endian)
244 if (sysfs_has_descriptors) {
245 return sysfs_get_device_descriptor(dev, buffer);
248 return usbfs_get_device_descriptor(dev, buffer);
252 static int usbfs_get_active_config_descriptor(struct libusb_device *dev,
253 unsigned char *buffer, size_t len)
255 struct linux_device_priv *priv = __device_priv(dev);
256 if (!priv->config_descriptor)
257 return LIBUSB_ERROR_NOT_FOUND; /* device is unconfigured */
259 /* retrieve cached copy */
260 memcpy(buffer, priv->config_descriptor, len);
264 static int sysfs_get_active_config_descriptor(struct libusb_device *dev,
265 unsigned char *buffer, size_t len)
271 /* sysfs provides access to an in-memory copy of the device descriptor,
272 * so we use that rather than keeping our own copy */
274 fd = __open_sysfs_attr(dev, "descriptors");
278 off = lseek(fd, DEVICE_DESC_LENGTH, SEEK_SET);
280 usbi_err("seek failed, ret=%d errno=%d", off, errno);
282 return LIBUSB_ERROR_IO;
285 r = read(fd, buffer, len);
288 usbi_err("read failed, ret=%d errno=%d", fd, errno);
289 return LIBUSB_ERROR_IO;
291 usbi_dbg("device is unconfigured");
292 return LIBUSB_ERROR_NOT_FOUND;
293 } else if (r < len) {
294 usbi_err("short read %d/%d", r, len);
295 return LIBUSB_ERROR_IO;
301 static int op_get_active_config_descriptor(struct libusb_device *dev,
302 unsigned char *buffer, size_t len, int *host_endian)
304 if (sysfs_has_descriptors) {
305 return sysfs_get_active_config_descriptor(dev, buffer, len);
308 return usbfs_get_active_config_descriptor(dev, buffer, len);
313 /* takes a usbfs fd, attempts to find the requested config and copy a certain
314 * amount of it into an output buffer. a bConfigurationValue of -1 indicates
315 * that the first config should be retreived. */
316 static int get_config_descriptor(int fd, uint8_t config_index,
317 unsigned char *buffer, size_t len)
319 unsigned char tmp[8];
323 off = lseek(fd, DEVICE_DESC_LENGTH, SEEK_SET);
325 usbi_err("seek failed ret=%d errno=%d", off, errno);
326 return LIBUSB_ERROR_IO;
329 /* might need to skip some configuration descriptors to reach the
330 * requested configuration */
331 while (config_index) {
332 struct libusb_config_descriptor config;
334 /* read first 8 bytes of descriptor */
335 r = read(fd, tmp, sizeof(tmp));
337 usbi_err("read failed ret=%d errno=%d", r, errno);
338 return LIBUSB_ERROR_IO;
339 } else if (r < sizeof(tmp)) {
340 usbi_err("short descriptor read %d/%d", r, sizeof(tmp));
341 return LIBUSB_ERROR_IO;
344 usbi_parse_descriptor(tmp, "bbwbb", &config, 1);
346 /* seek forward to end of config */
347 off = lseek(fd, config.wTotalLength - sizeof(tmp), SEEK_CUR);
349 usbi_err("seek failed ret=%d errno=%d", off, errno);
350 return LIBUSB_ERROR_IO;
356 /* read the rest of the descriptor */
357 r = read(fd, buffer, len);
359 usbi_err("read failed ret=%d errno=%d", r, errno);
360 return LIBUSB_ERROR_IO;
361 } else if (r < len) {
362 usbi_err("short output read %d/%d", r, len);
363 return LIBUSB_ERROR_IO;
369 static int op_get_config_descriptor(struct libusb_device *dev,
370 uint8_t config_index, unsigned char *buffer, size_t len, int *host_endian)
372 char filename[PATH_MAX + 1];
376 /* always read from usbfs: sysfs only has the active descriptor
377 * this will involve waking the device up, but oh well! */
379 __get_usbfs_path(dev, filename);
380 fd = open(filename, O_RDONLY);
382 usbi_err("open '%s' failed, ret=%d errno=%d", filename, fd, errno);
383 return LIBUSB_ERROR_IO;
386 r = get_config_descriptor(fd, config_index, buffer, len);
392 /* cache the active config descriptor in memory. a value of -1 means that
393 * we aren't sure which one is active, so just assume the first one.
395 static int cache_active_config(struct libusb_device *dev, int fd,
398 struct linux_device_priv *priv = __device_priv(dev);
399 struct libusb_config_descriptor config;
400 unsigned char tmp[8];
405 if (active_config == -1) {
408 r = usbi_get_config_index_by_value(dev, active_config, &idx);
412 return LIBUSB_ERROR_NOT_FOUND;
415 r = get_config_descriptor(fd, idx, tmp, sizeof(tmp));
417 usbi_err("first read error %d", r);
421 usbi_parse_descriptor(tmp, "bbw", &config, 1);
422 buf = malloc(config.wTotalLength);
424 return LIBUSB_ERROR_NO_MEM;
426 r = get_config_descriptor(fd, idx, buf, config.wTotalLength);
432 if (priv->config_descriptor)
433 free(priv->config_descriptor);
434 priv->config_descriptor = buf;
438 /* read the bConfigurationValue for a device */
439 static int sysfs_get_active_config(struct libusb_device *dev, int *config)
442 char tmp[4] = {0, 0, 0, 0};
447 fd = __open_sysfs_attr(dev, "bConfigurationValue");
451 r = read(fd, tmp, sizeof(tmp));
454 usbi_err("read bConfigurationValue failed ret=%d errno=%d",
456 return LIBUSB_ERROR_IO;
458 usbi_err("device unconfigured");
463 if (tmp[sizeof(tmp) - 1] != 0) {
464 usbi_err("not null-terminated?");
465 return LIBUSB_ERROR_IO;
466 } else if (tmp[0] == 0) {
467 usbi_err("no configuration value?");
468 return LIBUSB_ERROR_IO;
471 num = strtol(tmp, &endptr, 10);
473 usbi_err("error converting '%s' to integer", tmp);
474 return LIBUSB_ERROR_IO;
481 /* send a control message to retrieve active configuration */
482 static int usbfs_get_active_config(struct libusb_device *dev, int fd)
487 struct usbfs_ctrltransfer ctrl = {
488 .bmRequestType = LIBUSB_ENDPOINT_IN,
489 .bRequest = LIBUSB_REQUEST_GET_CONFIGURATION,
494 .data = &active_config
497 r = ioctl(fd, IOCTL_USBFS_CONTROL, &ctrl);
500 return LIBUSB_ERROR_NO_DEVICE;
502 usbi_err("get_configuration failed ret=%d errno=%d", r, errno);
503 return LIBUSB_ERROR_IO;
506 return active_config;
509 static int initialize_device(struct libusb_device *dev, uint8_t busnum,
510 uint8_t devaddr, const char *sysfs_dir)
512 struct linux_device_priv *priv = __device_priv(dev);
513 unsigned char *dev_buf;
514 char path[PATH_MAX + 1];
516 int active_config = 0;
517 int device_configured = 1;
520 dev->bus_number = busnum;
521 dev->device_address = devaddr;
524 strncpy(priv->sysfs_dir, sysfs_dir, SYSFS_DIR_LENGTH);
526 if (sysfs_has_descriptors)
529 /* cache device descriptor in memory so that we can retrieve it later
530 * without waking the device up (op_get_device_descriptor) */
532 priv->dev_descriptor = NULL;
533 priv->config_descriptor = NULL;
535 if (sysfs_can_relate_devices) {
536 int tmp = sysfs_get_active_config(dev, &active_config);
539 if (active_config == -1)
540 device_configured = 0;
543 __get_usbfs_path(dev, path);
544 fd = open(path, O_RDWR);
545 if (fd < 0 && errno == EACCES) {
546 fd = open(path, O_RDONLY);
547 /* if we only have read-only access to the device, we cannot
548 * send a control message to determine the active config. just
549 * assume the first one is active. */
554 usbi_err("open failed, ret=%d errno=%d", fd, errno);
555 return LIBUSB_ERROR_IO;
558 if (!sysfs_can_relate_devices) {
559 if (active_config == -1) {
560 /* if we only have read-only access to the device, we cannot
561 * send a control message to determine the active config. just
562 * assume the first one is active. */
563 usbi_warn("access to %s is read-only; cannot determine "
564 "active configuration descriptor", path);
566 active_config = usbfs_get_active_config(dev, fd);
567 if (active_config < 0) {
569 return active_config;
570 } else if (active_config == 0) {
571 /* some buggy devices have a configuration 0, but we're
572 * reaching into the corner of a corner case here, so let's
573 * not support buggy devices in these circumstances.
574 * stick to the specs: a configuration value of 0 means
576 usbi_dbg("assuming unconfigured device");
577 device_configured = 0;
582 dev_buf = malloc(DEVICE_DESC_LENGTH);
585 return LIBUSB_ERROR_NO_MEM;
588 r = read(fd, dev_buf, DEVICE_DESC_LENGTH);
590 usbi_err("read descriptor failed ret=%d errno=%d", fd, errno);
593 return LIBUSB_ERROR_IO;
594 } else if (r < DEVICE_DESC_LENGTH) {
595 usbi_err("short descriptor read (%d)", r);
598 return LIBUSB_ERROR_IO;
601 /* bit of a hack: set num_configurations now because cache_active_config()
602 * calls usbi_get_config_index_by_value() which uses it */
603 dev->num_configurations = dev_buf[DEVICE_DESC_LENGTH - 1];
605 if (device_configured) {
606 r = cache_active_config(dev, fd, active_config);
615 priv->dev_descriptor = dev_buf;
619 static int enumerate_device(struct discovered_devs **_discdevs,
620 uint8_t busnum, uint8_t devaddr, const char *sysfs_dir)
622 struct discovered_devs *discdevs;
623 unsigned long session_id;
625 struct libusb_device *dev;
628 /* FIXME: session ID is not guaranteed unique as addresses can wrap and
629 * will be reused. instead we should add a simple sysfs attribute with
631 session_id = busnum << 8 | devaddr;
632 usbi_dbg("busnum %d devaddr %d session_id %ld", busnum, devaddr,
635 dev = usbi_get_device_by_session_id(session_id);
637 usbi_dbg("using existing device for %d/%d (session %ld)",
638 busnum, devaddr, session_id);
640 usbi_dbg("allocating new device for %d/%d (session %ld)",
641 busnum, devaddr, session_id);
642 dev = usbi_alloc_device(session_id);
644 return LIBUSB_ERROR_NO_MEM;
646 r = initialize_device(dev, busnum, devaddr, sysfs_dir);
649 r = usbi_sanitize_device(dev);
654 discdevs = discovered_devs_append(*_discdevs, dev);
656 r = LIBUSB_ERROR_NO_MEM;
658 *_discdevs = discdevs;
662 libusb_unref_device(dev);
666 /* open a bus directory and adds all discovered devices to discdevs. on
667 * failure (non-zero return) the pre-existing discdevs should be destroyed
668 * (and devices freed). on success, the new discdevs pointer should be used
669 * as it may have been moved. */
670 static int usbfs_scan_busdir(struct discovered_devs **_discdevs, uint8_t busnum)
673 char dirpath[PATH_MAX + 1];
674 struct dirent *entry;
675 struct discovered_devs *discdevs = *_discdevs;
678 snprintf(dirpath, PATH_MAX, "%s/%03d", usbfs_path, busnum);
679 usbi_dbg("%s", dirpath);
680 dir = opendir(dirpath);
682 usbi_err("opendir '%s' failed, errno=%d", dirpath, errno);
683 /* FIXME: should handle valid race conditions like hub unplugged
684 * during directory iteration - this is not an error */
685 return LIBUSB_ERROR_IO;
688 while ((entry = readdir(dir))) {
691 if (entry->d_name[0] == '.')
694 devaddr = atoi(entry->d_name);
696 usbi_dbg("unknown dir entry %s", entry->d_name);
700 r = enumerate_device(&discdevs, busnum, (uint8_t) devaddr, NULL);
705 *_discdevs = discdevs;
711 static int usbfs_get_device_list(struct discovered_devs **_discdevs)
713 struct dirent *entry;
714 DIR *buses = opendir(usbfs_path);
715 struct discovered_devs *discdevs = *_discdevs;
719 usbi_err("opendir buses failed errno=%d", errno);
720 return LIBUSB_ERROR_IO;
723 while ((entry = readdir(buses))) {
724 struct discovered_devs *discdevs_new = discdevs;
727 if (entry->d_name[0] == '.')
730 busnum = atoi(entry->d_name);
732 usbi_dbg("unknown dir entry %s", entry->d_name);
736 r = usbfs_scan_busdir(&discdevs_new, busnum);
739 discdevs = discdevs_new;
744 *_discdevs = discdevs;
749 static int sysfs_scan_device(struct discovered_devs **_discdevs,
750 const char *devname, int *usbfs_fallback)
754 char filename[PATH_MAX + 1];
758 usbi_dbg("scan %s", devname);
760 /* determine descriptors presence ahead of time, we need to know this
761 * when we reach initialize_device */
762 if (sysfs_has_descriptors == -1) {
765 snprintf(filename, PATH_MAX, "%s/%s/descriptors", SYSFS_DEVICE_PATH,
767 r = stat(filename, &statbuf);
768 if (r == 0 && S_ISREG(statbuf.st_mode)) {
769 usbi_dbg("sysfs descriptors available");
770 sysfs_has_descriptors = 1;
772 usbi_dbg("sysfs descriptors not available");
773 sysfs_has_descriptors = 0;
777 snprintf(filename, PATH_MAX, "%s/%s/busnum", SYSFS_DEVICE_PATH, devname);
778 fd = fopen(filename, "r");
780 if (errno == ENOENT) {
781 usbi_dbg("busnum not found, cannot relate sysfs to usbfs, "
782 "falling back on pure usbfs");
783 sysfs_can_relate_devices = 0;
785 return LIBUSB_ERROR_OTHER;
787 usbi_err("open busnum failed, errno=%d", errno);
788 return LIBUSB_ERROR_IO;
791 sysfs_can_relate_devices = 1;
793 r = fscanf(fd, "%d", &busnum);
796 usbi_err("fscanf busnum returned %d, errno=%d", r, errno);
797 return LIBUSB_ERROR_IO;
800 snprintf(filename, PATH_MAX, "%s/%s/devnum", SYSFS_DEVICE_PATH, devname);
801 fd = fopen(filename, "r");
803 usbi_err("open devnum failed, errno=%d", errno);
804 return LIBUSB_ERROR_IO;
807 r = fscanf(fd, "%d", &devaddr);
810 usbi_err("fscanf devnum returned %d, errno=%d", r, errno);
811 return LIBUSB_ERROR_IO;
814 usbi_dbg("bus=%d dev=%d", busnum, devaddr);
815 if (busnum > 255 || devaddr > 255)
816 return LIBUSB_ERROR_INVALID_PARAM;
818 return enumerate_device(_discdevs, busnum & 0xff, devaddr & 0xff, devname);
821 static int sysfs_get_device_list(struct discovered_devs **_discdevs,
824 struct discovered_devs *discdevs = *_discdevs;
825 DIR *devices = opendir(SYSFS_DEVICE_PATH);
826 struct dirent *entry;
830 usbi_err("opendir devices failed errno=%d", errno);
831 return LIBUSB_ERROR_IO;
834 while ((entry = readdir(devices))) {
835 struct discovered_devs *discdevs_new = discdevs;
837 if ((!isdigit(entry->d_name[0]) && strncmp(entry->d_name, "usb", 3))
838 || strchr(entry->d_name, ':'))
841 r = sysfs_scan_device(&discdevs_new, entry->d_name, usbfs_fallback);
844 discdevs = discdevs_new;
849 *_discdevs = discdevs;
853 static int op_get_device_list(struct discovered_devs **_discdevs)
855 /* we can retrieve device list and descriptors from sysfs or usbfs.
856 * sysfs is preferable, because if we use usbfs we end up resuming
857 * any autosuspended USB devices. however, sysfs is not available
858 * everywhere, so we need a usbfs fallback too.
860 * as described in the "sysfs vs usbfs" comment, sometimes we have
861 * sysfs but not enough information to relate sysfs devices to usbfs
862 * nodes. the usbfs_fallback variable is used to indicate that we should
863 * fall back on usbfs.
865 if (sysfs_can_relate_devices != 0) {
866 int usbfs_fallback = 0;
867 int r = sysfs_get_device_list(_discdevs, &usbfs_fallback);
872 return usbfs_get_device_list(_discdevs);
875 static int op_open(struct libusb_device_handle *handle)
877 struct linux_device_handle_priv *hpriv = __device_handle_priv(handle);
878 char filename[PATH_MAX + 1];
880 __get_usbfs_path(handle->dev, filename);
881 hpriv->fd = open(filename, O_RDWR);
883 if (errno == EACCES) {
884 fprintf(stderr, "libusb couldn't open USB device %s: "
885 "Permission denied.\n"
886 "libusb requires write access to USB device nodes.\n",
888 return LIBUSB_ERROR_ACCESS;
889 } else if (errno == ENOENT) {
890 return LIBUSB_ERROR_NO_DEVICE;
892 usbi_err("open failed, code %d errno %d", hpriv->fd, errno);
893 return LIBUSB_ERROR_IO;
897 return usbi_add_pollfd(hpriv->fd, POLLOUT);
900 static void op_close(struct libusb_device_handle *dev_handle)
902 int fd = __device_handle_priv(dev_handle)->fd;
903 usbi_remove_pollfd(fd);
907 static int op_get_configuration(struct libusb_device_handle *handle,
911 if (sysfs_can_relate_devices != 1)
912 return LIBUSB_ERROR_NOT_SUPPORTED;
914 r = sysfs_get_active_config(handle->dev, config);
921 static int op_set_configuration(struct libusb_device_handle *handle, int config)
923 struct linux_device_priv *priv = __device_priv(handle->dev);
924 int fd = __device_handle_priv(handle)->fd;
925 int r = ioctl(fd, IOCTL_USBFS_SETCONFIG, &config);
928 return LIBUSB_ERROR_NOT_FOUND;
929 else if (errno == EBUSY)
930 return LIBUSB_ERROR_BUSY;
931 else if (errno == ENODEV)
932 return LIBUSB_ERROR_NO_DEVICE;
934 usbi_err("failed, error %d errno %d", r, errno);
935 return LIBUSB_ERROR_OTHER;
938 if (!sysfs_has_descriptors) {
939 /* update our cached active config descriptor */
941 if (priv->config_descriptor) {
942 free(priv->config_descriptor);
943 priv->config_descriptor = NULL;
946 r = cache_active_config(handle->dev, fd, config);
948 usbi_warn("failed to update cached config descriptor, "
956 static int op_claim_interface(struct libusb_device_handle *handle, int iface)
958 int fd = __device_handle_priv(handle)->fd;
959 int r = ioctl(fd, IOCTL_USBFS_CLAIMINTF, &iface);
962 return LIBUSB_ERROR_NOT_FOUND;
963 else if (errno == EBUSY)
964 return LIBUSB_ERROR_BUSY;
965 else if (errno == ENODEV)
966 return LIBUSB_ERROR_NO_DEVICE;
968 usbi_err("claim interface failed, error %d errno %d", r, errno);
969 return LIBUSB_ERROR_OTHER;
974 static int op_release_interface(struct libusb_device_handle *handle, int iface)
976 int fd = __device_handle_priv(handle)->fd;
977 int r = ioctl(fd, IOCTL_USBFS_RELEASEINTF, &iface);
980 return LIBUSB_ERROR_NO_DEVICE;
982 usbi_err("release interface failed, error %d errno %d", r, errno);
983 return LIBUSB_ERROR_OTHER;
988 static int op_set_interface(struct libusb_device_handle *handle, int iface,
991 int fd = __device_handle_priv(handle)->fd;
992 struct usbfs_setinterface setintf;
995 setintf.interface = iface;
996 setintf.altsetting = altsetting;
997 r = ioctl(fd, IOCTL_USBFS_SETINTF, &setintf);
1000 return LIBUSB_ERROR_NOT_FOUND;
1001 else if (errno == ENODEV)
1002 return LIBUSB_ERROR_NO_DEVICE;
1004 usbi_err("setintf failed error %d errno %d", r, errno);
1005 return LIBUSB_ERROR_OTHER;
1011 static int op_clear_halt(struct libusb_device_handle *handle,
1012 unsigned char endpoint)
1014 int fd = __device_handle_priv(handle)->fd;
1015 unsigned int _endpoint = endpoint;
1016 int r = ioctl(fd, IOCTL_USBFS_CLEAR_HALT, &_endpoint);
1018 if (errno == ENOENT)
1019 return LIBUSB_ERROR_NOT_FOUND;
1020 else if (errno == ENODEV)
1021 return LIBUSB_ERROR_NO_DEVICE;
1023 usbi_err("clear_halt failed error %d errno %d", r, errno);
1024 return LIBUSB_ERROR_OTHER;
1030 static int op_reset_device(struct libusb_device_handle *handle)
1032 int fd = __device_handle_priv(handle)->fd;
1033 int r = ioctl(fd, IOCTL_USBFS_RESET, NULL);
1035 if (errno == ENODEV)
1036 return LIBUSB_ERROR_NOT_FOUND;
1038 usbi_err("reset failed error %d errno %d", r, errno);
1039 return LIBUSB_ERROR_OTHER;
1045 static int op_kernel_driver_active(struct libusb_device_handle *handle,
1048 int fd = __device_handle_priv(handle)->fd;
1049 struct usbfs_getdriver getdrv;
1052 getdrv.interface = interface;
1053 r = ioctl(fd, IOCTL_USBFS_GETDRIVER, &getdrv);
1055 if (errno == ENODATA)
1057 else if (errno == ENODEV)
1058 return LIBUSB_ERROR_NO_DEVICE;
1060 usbi_err("get driver failed error %d errno %d", r, errno);
1061 return LIBUSB_ERROR_OTHER;
1067 static int op_detach_kernel_driver(struct libusb_device_handle *handle,
1070 int fd = __device_handle_priv(handle)->fd;
1071 struct usbfs_ioctl command;
1074 command.ifno = interface;
1075 command.ioctl_code = IOCTL_USBFS_DISCONNECT;
1076 command.data = NULL;
1078 r = ioctl(fd, IOCTL_USBFS_IOCTL, &command);
1080 if (errno == ENODATA)
1081 return LIBUSB_ERROR_NOT_FOUND;
1082 else if (errno == EINVAL)
1083 return LIBUSB_ERROR_INVALID_PARAM;
1084 else if (errno == ENODEV)
1085 return LIBUSB_ERROR_NO_DEVICE;
1087 usbi_err("detach failed error %d errno %d", r, errno);
1088 return LIBUSB_ERROR_OTHER;
1094 static void op_destroy_device(struct libusb_device *dev)
1096 struct linux_device_priv *priv = __device_priv(dev);
1097 if (!sysfs_has_descriptors) {
1098 if (priv->dev_descriptor)
1099 free(priv->dev_descriptor);
1100 if (priv->config_descriptor)
1101 free(priv->config_descriptor);
1105 static void free_iso_urbs(struct linux_transfer_priv *tpriv)
1108 for (i = 0; i < tpriv->num_urbs; i++) {
1109 struct usbfs_urb *urb = tpriv->iso_urbs[i];
1115 free(tpriv->iso_urbs);
1118 static int submit_bulk_transfer(struct usbi_transfer *itransfer,
1119 unsigned char urb_type)
1121 struct libusb_transfer *transfer =
1122 __USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
1123 struct linux_transfer_priv *tpriv = usbi_transfer_get_os_priv(itransfer);
1124 struct linux_device_handle_priv *dpriv =
1125 __device_handle_priv(transfer->dev_handle);
1126 struct usbfs_urb *urbs;
1131 /* usbfs places a 16kb limit on bulk URBs. we divide up larger requests
1132 * into smaller units to meet such restriction, then fire off all the
1133 * units at once. it would be simpler if we just fired one unit at a time,
1134 * but there is a big performance gain through doing it this way. */
1135 int num_urbs = transfer->length / MAX_BULK_BUFFER_LENGTH;
1136 int last_urb_partial = 0;
1138 if ((transfer->length % MAX_BULK_BUFFER_LENGTH) > 0) {
1139 last_urb_partial = 1;
1142 usbi_dbg("need %d urbs for new transfer with length %d", num_urbs,
1144 alloc_size = num_urbs * sizeof(struct usbfs_urb);
1145 urbs = malloc(alloc_size);
1147 return LIBUSB_ERROR_NO_MEM;
1148 memset(urbs, 0, alloc_size);
1150 tpriv->num_urbs = num_urbs;
1151 tpriv->awaiting_discard = 0;
1152 tpriv->awaiting_reap = 0;
1153 tpriv->reap_action = NORMAL;
1155 for (i = 0; i < num_urbs; i++) {
1156 struct usbfs_urb *urb = &urbs[i];
1157 urb->usercontext = itransfer;
1158 urb->type = urb_type;
1159 urb->endpoint = transfer->endpoint;
1160 urb->buffer = transfer->buffer + (i * MAX_BULK_BUFFER_LENGTH);
1161 if (i == num_urbs - 1 && last_urb_partial)
1162 urb->buffer_length = transfer->length % MAX_BULK_BUFFER_LENGTH;
1164 urb->buffer_length = MAX_BULK_BUFFER_LENGTH;
1166 r = ioctl(dpriv->fd, IOCTL_USBFS_SUBMITURB, urb);
1170 if (errno == ENODEV) {
1171 r = LIBUSB_ERROR_NO_DEVICE;
1173 usbi_err("submiturb failed error %d errno=%d", r, errno);
1174 r = LIBUSB_ERROR_IO;
1177 /* if the first URB submission fails, we can simply free up and
1178 * return failure immediately. */
1180 usbi_dbg("first URB failed, easy peasy");
1185 /* if it's not the first URB that failed, the situation is a bit
1186 * tricky. we must discard all previous URBs. there are
1188 * - discarding is asynchronous - discarded urbs will be reaped
1189 * later. the user must not have freed the transfer when the
1190 * discarded URBs are reaped, otherwise libusb will be using
1192 * - the earlier URBs may have completed successfully and we do
1193 * not want to throw away any data.
1194 * so, in this case we discard all the previous URBs BUT we report
1195 * that the transfer was submitted successfully. then later when
1196 * the final discard completes we can report error to the user.
1198 tpriv->reap_action = SUBMIT_FAILED;
1199 for (j = 0; j < i; j++) {
1200 int tmp = ioctl(dpriv->fd, IOCTL_USBFS_DISCARDURB, &urbs[j]);
1202 tpriv->awaiting_discard++;
1203 else if (errno == EINVAL)
1204 tpriv->awaiting_reap++;
1206 usbi_warn("unrecognised discard return %d", tmp);
1209 usbi_dbg("reporting successful submission but waiting for %d "
1210 "discards and %d reaps before reporting error",
1211 tpriv->awaiting_discard, tpriv->awaiting_reap);
1219 static int submit_iso_transfer(struct usbi_transfer *itransfer)
1221 struct libusb_transfer *transfer =
1222 __USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
1223 struct linux_transfer_priv *tpriv = usbi_transfer_get_os_priv(itransfer);
1224 struct linux_device_handle_priv *dpriv =
1225 __device_handle_priv(transfer->dev_handle);
1226 struct usbfs_urb **urbs;
1228 int num_packets = transfer->num_iso_packets;
1230 int this_urb_len = 0;
1232 int packet_offset = 0;
1233 unsigned int packet_len;
1234 unsigned char *urb_buffer = transfer->buffer;
1236 /* usbfs places a 32kb limit on iso URBs. we divide up larger requests
1237 * into smaller units to meet such restriction, then fire off all the
1238 * units at once. it would be simpler if we just fired one unit at a time,
1239 * but there is a big performance gain through doing it this way. */
1241 /* calculate how many URBs we need */
1242 for (i = 0; i < num_packets; i++) {
1243 int space_remaining = MAX_ISO_BUFFER_LENGTH - this_urb_len;
1244 packet_len = transfer->iso_packet_desc[i].length;
1246 if (packet_len > space_remaining) {
1248 this_urb_len = packet_len;
1250 this_urb_len += packet_len;
1253 usbi_dbg("need %d 32k URBs for transfer", num_urbs);
1255 alloc_size = num_urbs * sizeof(*urbs);
1256 urbs = malloc(alloc_size);
1258 return LIBUSB_ERROR_NO_MEM;
1259 memset(urbs, 0, alloc_size);
1261 tpriv->iso_urbs = urbs;
1262 tpriv->num_urbs = num_urbs;
1263 tpriv->awaiting_discard = 0;
1264 tpriv->awaiting_reap = 0;
1265 tpriv->reap_action = NORMAL;
1266 tpriv->iso_packet_offset = 0;
1268 /* allocate + initialize each URB with the correct number of packets */
1269 for (i = 0; i < num_urbs; i++) {
1270 struct usbfs_urb *urb;
1271 int space_remaining_in_urb = MAX_ISO_BUFFER_LENGTH;
1272 int urb_packet_offset = 0;
1273 unsigned char *urb_buffer_orig = urb_buffer;
1277 /* swallow up all the packets we can fit into this URB */
1278 while (packet_offset < transfer->num_iso_packets) {
1279 packet_len = transfer->iso_packet_desc[packet_offset].length;
1280 if (packet_len <= space_remaining_in_urb) {
1282 urb_packet_offset++;
1284 space_remaining_in_urb -= packet_len;
1285 urb_buffer += packet_len;
1287 /* it can't fit, save it for the next URB */
1292 alloc_size = sizeof(*urb)
1293 + (urb_packet_offset * sizeof(struct usbfs_iso_packet_desc));
1294 urb = malloc(alloc_size);
1296 free_iso_urbs(tpriv);
1297 return LIBUSB_ERROR_NO_MEM;
1299 memset(urb, 0, alloc_size);
1302 /* populate packet lengths */
1303 for (j = 0, k = packet_offset - urb_packet_offset;
1304 k < packet_offset; k++, j++) {
1305 packet_len = transfer->iso_packet_desc[k].length;
1306 urb->iso_frame_desc[j].length = packet_len;
1309 urb->usercontext = itransfer;
1310 urb->type = USBFS_URB_TYPE_ISO;
1311 /* FIXME: interface for non-ASAP data? */
1312 urb->flags = USBFS_URB_ISO_ASAP;
1313 urb->endpoint = transfer->endpoint;
1314 urb->number_of_packets = urb_packet_offset;
1315 urb->buffer = urb_buffer_orig;
1319 for (i = 0; i < num_urbs; i++) {
1320 int r = ioctl(dpriv->fd, IOCTL_USBFS_SUBMITURB, urbs[i]);
1324 if (errno == ENODEV) {
1325 r = LIBUSB_ERROR_NO_DEVICE;
1327 usbi_err("submiturb failed error %d errno=%d", r, errno);
1328 r = LIBUSB_ERROR_IO;
1331 /* if the first URB submission fails, we can simply free up and
1332 * return failure immediately. */
1334 usbi_dbg("first URB failed, easy peasy");
1335 free_iso_urbs(tpriv);
1339 /* if it's not the first URB that failed, the situation is a bit
1340 * tricky. we must discard all previous URBs. there are
1342 * - discarding is asynchronous - discarded urbs will be reaped
1343 * later. the user must not have freed the transfer when the
1344 * discarded URBs are reaped, otherwise libusb will be using
1346 * - the earlier URBs may have completed successfully and we do
1347 * not want to throw away any data.
1348 * so, in this case we discard all the previous URBs BUT we report
1349 * that the transfer was submitted successfully. then later when
1350 * the final discard completes we can report error to the user.
1352 tpriv->reap_action = SUBMIT_FAILED;
1353 for (j = 0; j < i; j++) {
1354 int tmp = ioctl(dpriv->fd, IOCTL_USBFS_DISCARDURB, urbs[j]);
1356 tpriv->awaiting_discard++;
1357 else if (errno == EINVAL)
1358 tpriv->awaiting_reap++;
1360 usbi_warn("unrecognised discard return %d", tmp);
1363 usbi_dbg("reporting successful submission but waiting for %d "
1364 "discards and %d reaps before reporting error",
1365 tpriv->awaiting_discard, tpriv->awaiting_reap);
1373 static int submit_control_transfer(struct usbi_transfer *itransfer)
1375 struct linux_transfer_priv *tpriv = usbi_transfer_get_os_priv(itransfer);
1376 struct libusb_transfer *transfer =
1377 __USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
1378 struct linux_device_handle_priv *dpriv =
1379 __device_handle_priv(transfer->dev_handle);
1380 struct usbfs_urb *urb;
1383 if (transfer->length - LIBUSB_CONTROL_SETUP_SIZE > MAX_CTRL_BUFFER_LENGTH)
1384 return LIBUSB_ERROR_INVALID_PARAM;
1386 urb = malloc(sizeof(struct usbfs_urb));
1388 return LIBUSB_ERROR_NO_MEM;
1389 memset(urb, 0, sizeof(struct usbfs_urb));
1391 tpriv->reap_action = NORMAL;
1393 urb->usercontext = itransfer;
1394 urb->type = USBFS_URB_TYPE_CONTROL;
1395 urb->endpoint = transfer->endpoint;
1396 urb->buffer = transfer->buffer;
1397 urb->buffer_length = transfer->length;
1399 r = ioctl(dpriv->fd, IOCTL_USBFS_SUBMITURB, urb);
1402 if (errno == ENODEV)
1403 return LIBUSB_ERROR_NO_DEVICE;
1405 usbi_err("submiturb failed error %d errno=%d", r, errno);
1406 return LIBUSB_ERROR_IO;
1411 static int op_submit_transfer(struct usbi_transfer *itransfer)
1413 struct libusb_transfer *transfer =
1414 __USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
1416 switch (transfer->type) {
1417 case LIBUSB_TRANSFER_TYPE_CONTROL:
1418 return submit_control_transfer(itransfer);
1419 case LIBUSB_TRANSFER_TYPE_BULK:
1420 return submit_bulk_transfer(itransfer, USBFS_URB_TYPE_BULK);
1421 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1422 return submit_bulk_transfer(itransfer, USBFS_URB_TYPE_INTERRUPT);
1423 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1424 return submit_iso_transfer(itransfer);
1426 usbi_err("unknown endpoint type %d", transfer->type);
1427 return LIBUSB_ERROR_INVALID_PARAM;
1431 static int cancel_control_transfer(struct usbi_transfer *itransfer)
1433 struct linux_transfer_priv *tpriv = usbi_transfer_get_os_priv(itransfer);
1434 struct libusb_transfer *transfer =
1435 __USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
1436 struct linux_device_handle_priv *dpriv =
1437 __device_handle_priv(transfer->dev_handle);
1440 tpriv->reap_action = CANCELLED;
1441 r = ioctl(dpriv->fd, IOCTL_USBFS_DISCARDURB, tpriv->urbs);
1443 if (errno == EINVAL) {
1444 usbi_dbg("URB not found --> assuming ready to be reaped");
1447 usbi_err("unrecognised DISCARD code %d", errno);
1448 return LIBUSB_ERROR_OTHER;
1455 static void cancel_bulk_transfer(struct usbi_transfer *itransfer)
1457 struct linux_transfer_priv *tpriv = usbi_transfer_get_os_priv(itransfer);
1458 struct libusb_transfer *transfer =
1459 __USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
1460 struct linux_device_handle_priv *dpriv =
1461 __device_handle_priv(transfer->dev_handle);
1464 tpriv->reap_action = CANCELLED;
1465 for (i = 0; i < tpriv->num_urbs; i++) {
1466 int tmp = ioctl(dpriv->fd, IOCTL_USBFS_DISCARDURB, &tpriv->urbs[i]);
1468 tpriv->awaiting_discard++;
1469 else if (errno == EINVAL)
1470 tpriv->awaiting_reap++;
1472 usbi_warn("unrecognised discard return %d", errno);
1476 static void cancel_iso_transfer(struct usbi_transfer *itransfer)
1478 struct linux_transfer_priv *tpriv = usbi_transfer_get_os_priv(itransfer);
1479 struct libusb_transfer *transfer =
1480 __USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
1481 struct linux_device_handle_priv *dpriv =
1482 __device_handle_priv(transfer->dev_handle);
1485 tpriv->reap_action = CANCELLED;
1486 for (i = 0; i < tpriv->num_urbs; i++) {
1487 int tmp = ioctl(dpriv->fd, IOCTL_USBFS_DISCARDURB, tpriv->iso_urbs[i]);
1489 tpriv->awaiting_discard++;
1490 else if (errno == EINVAL)
1491 tpriv->awaiting_reap++;
1493 usbi_warn("unrecognised discard return %d", errno);
1497 static int op_cancel_transfer(struct usbi_transfer *itransfer)
1499 struct libusb_transfer *transfer =
1500 __USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
1502 switch (transfer->type) {
1503 case LIBUSB_TRANSFER_TYPE_CONTROL:
1504 return cancel_control_transfer(itransfer);
1505 case LIBUSB_TRANSFER_TYPE_BULK:
1506 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1507 cancel_bulk_transfer(itransfer);
1509 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1510 cancel_iso_transfer(itransfer);
1513 usbi_err("unknown endpoint type %d", transfer->type);
1514 return LIBUSB_ERROR_INVALID_PARAM;
1518 static void op_clear_transfer_priv(struct usbi_transfer *itransfer)
1520 struct libusb_transfer *transfer =
1521 __USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
1522 struct linux_transfer_priv *tpriv = usbi_transfer_get_os_priv(itransfer);
1524 switch (transfer->type) {
1525 case LIBUSB_TRANSFER_TYPE_CONTROL:
1526 case LIBUSB_TRANSFER_TYPE_BULK:
1527 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1530 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1531 free_iso_urbs(tpriv);
1534 usbi_err("unknown endpoint type %d", transfer->type);
1538 static int handle_bulk_completion(struct usbi_transfer *itransfer,
1539 struct usbfs_urb *urb)
1541 struct linux_transfer_priv *tpriv = usbi_transfer_get_os_priv(itransfer);
1542 int num_urbs = tpriv->num_urbs;
1543 int urb_idx = urb - tpriv->urbs;
1544 enum libusb_transfer_status status = LIBUSB_TRANSFER_COMPLETED;
1546 usbi_dbg("handling completion status %d of bulk urb %d/%d", urb->status,
1547 urb_idx + 1, num_urbs);
1549 if (urb->status == 0)
1550 itransfer->transferred += urb->actual_length;
1552 if (tpriv->reap_action != NORMAL) {
1553 /* cancelled, submit_fail, or completed early */
1554 if (urb->status == -ENOENT) {
1555 usbi_dbg("CANCEL: detected a cancelled URB");
1556 if (tpriv->awaiting_discard == 0)
1557 usbi_err("CANCEL: cancelled URB but not awaiting discards?");
1559 tpriv->awaiting_discard--;
1560 } else if (urb->status == 0) {
1561 usbi_dbg("CANCEL: detected a completed URB");
1563 /* FIXME we could solve this extreme corner case with a memmove
1565 if (tpriv->reap_action == COMPLETED_EARLY)
1566 usbi_warn("SOME DATA LOST! (completed early but remaining "
1569 if (tpriv->awaiting_reap == 0)
1570 usbi_err("CANCEL: completed URB not awaiting reap?");
1572 tpriv->awaiting_reap--;
1574 usbi_warn("unhandled CANCEL urb status %d", urb->status);
1577 if (tpriv->awaiting_reap == 0 && tpriv->awaiting_discard == 0) {
1578 usbi_dbg("CANCEL: last URB handled, reporting");
1579 if (tpriv->reap_action == CANCELLED) {
1581 usbi_handle_transfer_cancellation(itransfer);
1583 } else if (tpriv->reap_action == COMPLETED_EARLY) {
1586 status = LIBUSB_TRANSFER_ERROR;
1593 if (urb->status == -EPIPE) {
1594 usbi_dbg("detected endpoint stall");
1595 status = LIBUSB_TRANSFER_STALL;
1597 } else if (urb->status != 0) {
1598 usbi_warn("unrecognised urb status %d", urb->status);
1601 /* if we're the last urb or we got less data than requested then we're
1603 if (urb_idx == num_urbs - 1) {
1604 usbi_dbg("last URB in transfer --> complete!");
1605 } else if (urb->actual_length < urb->buffer_length) {
1606 struct libusb_transfer *transfer =
1607 __USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
1608 struct linux_device_handle_priv *dpriv =
1609 __device_handle_priv(transfer->dev_handle);
1612 usbi_dbg("short transfer %d/%d --> complete!",
1613 urb->actual_length, urb->buffer_length);
1615 /* we have to cancel the remaining urbs and wait for their completion
1616 * before reporting results */
1617 tpriv->reap_action = COMPLETED_EARLY;
1618 for (i = urb_idx + 1; i < tpriv->num_urbs; i++) {
1619 int r = ioctl(dpriv->fd, IOCTL_USBFS_DISCARDURB, &tpriv->urbs[i]);
1621 tpriv->awaiting_discard++;
1622 else if (errno == EINVAL)
1623 tpriv->awaiting_reap++;
1625 usbi_warn("unrecognised discard return %d", errno);
1634 usbi_handle_transfer_completion(itransfer, status);
1638 static int handle_iso_completion(struct usbi_transfer *itransfer,
1639 struct usbfs_urb *urb)
1641 struct libusb_transfer *transfer =
1642 __USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
1643 struct linux_transfer_priv *tpriv = usbi_transfer_get_os_priv(itransfer);
1644 int num_urbs = tpriv->num_urbs;
1648 for (i = 0; i < num_urbs; i++) {
1649 if (urb == tpriv->iso_urbs[i]) {
1655 usbi_err("could not locate urb!");
1656 return LIBUSB_ERROR_NOT_FOUND;
1659 usbi_dbg("handling completion status %d of iso urb %d/%d", urb->status,
1662 if (urb->status == 0) {
1663 /* copy isochronous results back in */
1665 for (i = 0; i < urb->number_of_packets; i++) {
1666 struct usbfs_iso_packet_desc *urb_desc = &urb->iso_frame_desc[i];
1667 struct libusb_iso_packet_descriptor *lib_desc =
1668 &transfer->iso_packet_desc[tpriv->iso_packet_offset++];
1669 lib_desc->status = urb_desc->status;
1670 lib_desc->actual_length = urb_desc->actual_length;
1674 if (tpriv->reap_action != NORMAL) { /* cancelled or submit_fail */
1675 if (urb->status == -ENOENT) {
1676 usbi_dbg("CANCEL: detected a cancelled URB");
1677 if (tpriv->awaiting_discard == 0)
1678 usbi_err("CANCEL: cancelled URB but not awaiting discards?");
1680 tpriv->awaiting_discard--;
1681 } else if (urb->status == 0) {
1682 usbi_dbg("CANCEL: detected a completed URB");
1683 if (tpriv->awaiting_reap == 0)
1684 usbi_err("CANCEL: completed URB not awaiting reap?");
1686 tpriv->awaiting_reap--;
1688 usbi_warn("unhandled CANCEL urb status %d", urb->status);
1691 if (tpriv->awaiting_reap == 0 && tpriv->awaiting_discard == 0) {
1692 usbi_dbg("CANCEL: last URB handled, reporting");
1693 free_iso_urbs(tpriv);
1694 if (tpriv->reap_action == CANCELLED)
1695 usbi_handle_transfer_cancellation(itransfer);
1697 usbi_handle_transfer_completion(itransfer,
1698 LIBUSB_TRANSFER_ERROR);
1703 if (urb->status != 0)
1704 usbi_warn("unrecognised urb status %d", urb->status);
1706 /* if we're the last urb or we got less data than requested then we're
1708 if (urb_idx == num_urbs) {
1709 usbi_dbg("last URB in transfer --> complete!");
1710 free_iso_urbs(tpriv);
1711 usbi_handle_transfer_completion(itransfer, LIBUSB_TRANSFER_COMPLETED);
1717 static int handle_control_completion(struct usbi_transfer *itransfer,
1718 struct usbfs_urb *urb)
1720 struct linux_transfer_priv *tpriv = usbi_transfer_get_os_priv(itransfer);
1723 usbi_dbg("handling completion status %d", urb->status);
1725 if (urb->status == 0)
1726 itransfer->transferred += urb->actual_length;
1728 if (tpriv->reap_action == CANCELLED) {
1729 if (urb->status != 0 && urb->status != -ENOENT)
1730 usbi_warn("cancel: unrecognised urb status %d", urb->status);
1732 usbi_handle_transfer_cancellation(itransfer);
1736 if (urb->status == -EPIPE) {
1737 usbi_dbg("unsupported control request");
1738 status = LIBUSB_TRANSFER_STALL;
1740 } else if (urb->status != 0) {
1741 usbi_warn("unrecognised urb status %d", urb->status);
1742 status = LIBUSB_TRANSFER_ERROR;
1746 itransfer->transferred = urb->actual_length;
1747 status = LIBUSB_TRANSFER_COMPLETED;
1750 usbi_handle_transfer_completion(itransfer, status);
1754 static int reap_for_handle(struct libusb_device_handle *handle)
1756 struct linux_device_handle_priv *hpriv = __device_handle_priv(handle);
1758 struct usbfs_urb *urb;
1759 struct usbi_transfer *itransfer;
1760 struct libusb_transfer *transfer;
1762 r = ioctl(hpriv->fd, IOCTL_USBFS_REAPURBNDELAY, &urb);
1763 if (r == -1 && errno == EAGAIN)
1766 if (errno == ENODEV)
1767 return LIBUSB_ERROR_NO_DEVICE;
1769 usbi_err("reap failed error %d errno=%d", r, errno);
1770 return LIBUSB_ERROR_IO;
1773 itransfer = urb->usercontext;
1774 transfer = __USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
1776 usbi_dbg("urb type=%d status=%d transferred=%d", urb->type, urb->status,
1777 urb->actual_length);
1779 switch (transfer->type) {
1780 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1781 return handle_iso_completion(itransfer, urb);
1782 case LIBUSB_TRANSFER_TYPE_BULK:
1783 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1784 return handle_bulk_completion(itransfer, urb);
1785 case LIBUSB_TRANSFER_TYPE_CONTROL:
1786 return handle_control_completion(itransfer, urb);
1788 usbi_err("unrecognised endpoint type %x", transfer->type);
1789 return LIBUSB_ERROR_OTHER;
1793 static int op_handle_events(struct pollfd *fds, nfds_t nfds, int num_ready)
1798 pthread_mutex_lock(&usbi_open_devs_lock);
1799 for (i = 0; i < nfds && num_ready > 0; i++) {
1800 struct pollfd *pollfd = &fds[i];
1801 struct libusb_device_handle *handle;
1802 struct linux_device_handle_priv *hpriv = NULL;
1804 if (!pollfd->revents)
1808 list_for_each_entry(handle, &usbi_open_devs, list) {
1809 hpriv = __device_handle_priv(handle);
1810 if (hpriv->fd == pollfd->fd)
1814 if (pollfd->revents & POLLERR) {
1815 usbi_remove_pollfd(hpriv->fd);
1816 usbi_handle_disconnect(handle);
1820 r = reap_for_handle(handle);
1821 if (r == 1 || r == LIBUSB_ERROR_NO_DEVICE)
1829 pthread_mutex_unlock(&usbi_open_devs_lock);
1833 const struct usbi_os_backend linux_usbfs_backend = {
1834 .name = "Linux usbfs",
1837 .get_device_list = op_get_device_list,
1838 .get_device_descriptor = op_get_device_descriptor,
1839 .get_active_config_descriptor = op_get_active_config_descriptor,
1840 .get_config_descriptor = op_get_config_descriptor,
1844 .get_configuration = op_get_configuration,
1845 .set_configuration = op_set_configuration,
1846 .claim_interface = op_claim_interface,
1847 .release_interface = op_release_interface,
1849 .set_interface_altsetting = op_set_interface,
1850 .clear_halt = op_clear_halt,
1851 .reset_device = op_reset_device,
1853 .kernel_driver_active = op_kernel_driver_active,
1854 .detach_kernel_driver = op_detach_kernel_driver,
1856 .destroy_device = op_destroy_device,
1858 .submit_transfer = op_submit_transfer,
1859 .cancel_transfer = op_cancel_transfer,
1860 .clear_transfer_priv = op_clear_transfer_priv,
1862 .handle_events = op_handle_events,
1864 .device_priv_size = sizeof(struct linux_device_priv),
1865 .device_handle_priv_size = sizeof(struct linux_device_handle_priv),
1866 .transfer_priv_size = sizeof(struct linux_transfer_priv),
1867 .add_iso_packet_size = 0,