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.
60 * The descriptors file originally only contained the active configuration
61 * descriptor alongside the device descriptor, but all configurations are
62 * included as of Linux 2.6.26.
65 static const char *usbfs_path = NULL;
67 /* do we have a busnum to relate devices? this also implies that we can read
68 * the active configuration through bConfigurationValue */
69 static int sysfs_can_relate_devices = -1;
71 /* do we have a descriptors file? */
72 static int sysfs_has_descriptors = -1;
74 struct linux_device_priv {
76 unsigned char *dev_descriptor;
77 unsigned char *config_descriptor;
80 struct linux_device_handle_priv {
86 /* submission failed after the first URB, so await cancellation/completion
87 * of all the others */
90 /* cancelled by user or timeout */
93 /* completed multi-URB transfer in non-final URB */
97 struct linux_transfer_priv {
99 struct usbfs_urb *urbs;
100 struct usbfs_urb **iso_urbs;
103 enum reap_action reap_action;
105 unsigned int num_retired;
107 /* next iso packet in user-supplied transfer to be populated */
108 int iso_packet_offset;
111 static void __get_usbfs_path(struct libusb_device *dev, char *path)
113 snprintf(path, PATH_MAX, "%s/%03d/%03d", usbfs_path, dev->bus_number,
114 dev->device_address);
117 static struct linux_device_priv *__device_priv(struct libusb_device *dev)
119 return (struct linux_device_priv *) dev->os_priv;
122 static struct linux_device_handle_priv *__device_handle_priv(
123 struct libusb_device_handle *handle)
125 return (struct linux_device_handle_priv *) handle->os_priv;
128 static int check_usb_vfs(const char *dirname)
131 struct dirent *entry;
134 dir = opendir(dirname);
138 while ((entry = readdir(dir)) != NULL) {
139 if (entry->d_name[0] == '.')
142 /* We assume if we find any files that it must be the right place */
151 static const char *find_usbfs_path(void)
153 const char *path = "/dev/bus/usb";
154 const char *ret = NULL;
156 if (check_usb_vfs(path)) {
159 path = "/proc/bus/usb";
160 if (check_usb_vfs(path))
164 usbi_dbg("found usbfs at %s", ret);
168 static int op_init(struct libusb_context *ctx)
173 usbfs_path = find_usbfs_path();
175 usbi_err(ctx, "could not find usbfs");
176 return LIBUSB_ERROR_OTHER;
179 r = stat(SYSFS_DEVICE_PATH, &statbuf);
180 if (r == 0 && S_ISDIR(statbuf.st_mode)) {
181 usbi_dbg("found usb devices in sysfs");
183 usbi_dbg("sysfs usb info not available");
184 sysfs_has_descriptors = 0;
185 sysfs_can_relate_devices = 0;
191 static int usbfs_get_device_descriptor(struct libusb_device *dev,
192 unsigned char *buffer)
194 struct linux_device_priv *priv = __device_priv(dev);
196 /* return cached copy */
197 memcpy(buffer, priv->dev_descriptor, DEVICE_DESC_LENGTH);
201 static int __open_sysfs_attr(struct libusb_device *dev, const char *attr)
203 struct linux_device_priv *priv = __device_priv(dev);
204 char filename[PATH_MAX];
207 snprintf(filename, PATH_MAX, "%s/%s/%s",
208 SYSFS_DEVICE_PATH, priv->sysfs_dir, attr);
209 fd = open(filename, O_RDONLY);
211 usbi_err(DEVICE_CTX(dev),
212 "open %s failed ret=%d errno=%d", filename, fd, errno);
213 return LIBUSB_ERROR_IO;
219 static int sysfs_get_device_descriptor(struct libusb_device *dev,
220 unsigned char *buffer)
225 /* sysfs provides access to an in-memory copy of the device descriptor,
226 * so we use that rather than keeping our own copy */
228 fd = __open_sysfs_attr(dev, "descriptors");
232 r = read(fd, buffer, DEVICE_DESC_LENGTH);;
235 usbi_err(DEVICE_CTX(dev), "read failed, ret=%d errno=%d", fd, errno);
236 return LIBUSB_ERROR_IO;
237 } else if (r < DEVICE_DESC_LENGTH) {
238 usbi_err(DEVICE_CTX(dev), "short read %d/%d", r, DEVICE_DESC_LENGTH);
239 return LIBUSB_ERROR_IO;
245 static int op_get_device_descriptor(struct libusb_device *dev,
246 unsigned char *buffer, int *host_endian)
248 if (sysfs_has_descriptors) {
249 return sysfs_get_device_descriptor(dev, buffer);
252 return usbfs_get_device_descriptor(dev, buffer);
256 static int usbfs_get_active_config_descriptor(struct libusb_device *dev,
257 unsigned char *buffer, size_t len)
259 struct linux_device_priv *priv = __device_priv(dev);
260 if (!priv->config_descriptor)
261 return LIBUSB_ERROR_NOT_FOUND; /* device is unconfigured */
263 /* retrieve cached copy */
264 memcpy(buffer, priv->config_descriptor, len);
268 /* read the bConfigurationValue for a device */
269 static int sysfs_get_active_config(struct libusb_device *dev, int *config)
272 char tmp[4] = {0, 0, 0, 0};
277 fd = __open_sysfs_attr(dev, "bConfigurationValue");
281 r = read(fd, tmp, sizeof(tmp));
284 usbi_err(DEVICE_CTX(dev),
285 "read bConfigurationValue failed ret=%d errno=%d", r, errno);
286 return LIBUSB_ERROR_IO;
288 usbi_err(DEVICE_CTX(dev), "device unconfigured");
293 if (tmp[sizeof(tmp) - 1] != 0) {
294 usbi_err(DEVICE_CTX(dev), "not null-terminated?");
295 return LIBUSB_ERROR_IO;
296 } else if (tmp[0] == 0) {
297 usbi_err(DEVICE_CTX(dev), "no configuration value?");
298 return LIBUSB_ERROR_IO;
301 num = strtol(tmp, &endptr, 10);
303 usbi_err(DEVICE_CTX(dev), "error converting '%s' to integer", tmp);
304 return LIBUSB_ERROR_IO;
311 /* takes a usbfs/descriptors fd seeked to the start of a configuration, and
312 * seeks to the next one. */
313 static int seek_to_next_config(struct libusb_context *ctx, int fd)
315 struct libusb_config_descriptor config;
316 unsigned char tmp[6];
320 /* read first 6 bytes of descriptor */
321 r = read(fd, tmp, sizeof(tmp));
323 usbi_err(ctx, "read failed ret=%d errno=%d", r, errno);
324 return LIBUSB_ERROR_IO;
325 } else if (r < sizeof(tmp)) {
326 usbi_err(ctx, "short descriptor read %d/%d", r, sizeof(tmp));
327 return LIBUSB_ERROR_IO;
330 /* seek forward to end of config */
331 usbi_parse_descriptor(tmp, "bbwbb", &config, 1);
332 off = lseek(fd, config.wTotalLength - sizeof(tmp), SEEK_CUR);
334 usbi_err(ctx, "seek failed ret=%d errno=%d", off, errno);
335 return LIBUSB_ERROR_IO;
341 static int sysfs_get_active_config_descriptor(struct libusb_device *dev,
342 unsigned char *buffer, size_t len)
349 unsigned char tmp[6];
351 r = sysfs_get_active_config(dev, &config);
355 return LIBUSB_ERROR_NOT_FOUND;
357 usbi_dbg("active configuration %d", config);
359 /* sysfs provides access to an in-memory copy of the device descriptor,
360 * so we use that rather than keeping our own copy */
362 fd = __open_sysfs_attr(dev, "descriptors");
366 /* device might have been unconfigured since we read bConfigurationValue,
367 * so first check that there is any config descriptor data at all... */
368 off = lseek(fd, 0, SEEK_END);
370 usbi_err(DEVICE_CTX(dev), "end seek failed, ret=%d errno=%d",
373 return LIBUSB_ERROR_IO;
374 } else if (off == DEVICE_DESC_LENGTH) {
376 return LIBUSB_ERROR_NOT_FOUND;
379 off = lseek(fd, DEVICE_DESC_LENGTH, SEEK_SET);
381 usbi_err(DEVICE_CTX(dev), "seek failed, ret=%d errno=%d", off, errno);
383 return LIBUSB_ERROR_IO;
386 /* unbounded loop: we expect the descriptor to be present under all
389 r = read(fd, tmp, sizeof(tmp));
391 usbi_err(DEVICE_CTX(dev), "read failed, ret=%d errno=%d",
393 return LIBUSB_ERROR_IO;
394 } else if (r < sizeof(tmp)) {
395 usbi_err(DEVICE_CTX(dev), "short read %d/%d", r, sizeof(tmp));
396 return LIBUSB_ERROR_IO;
399 /* check bConfigurationValue */
400 if (tmp[5] == config)
403 /* try the next descriptor */
404 off = lseek(fd, 0 - sizeof(tmp), SEEK_CUR);
406 return LIBUSB_ERROR_IO;
408 r = seek_to_next_config(DEVICE_CTX(dev), fd);
413 to_copy = (len < sizeof(tmp)) ? len : sizeof(tmp);
414 memcpy(buffer, tmp, to_copy);
415 if (len > sizeof(tmp)) {
416 r = read(fd, buffer + sizeof(tmp), len - sizeof(tmp));
418 usbi_err(DEVICE_CTX(dev), "read failed, ret=%d errno=%d",
422 usbi_dbg("device is unconfigured");
423 r = LIBUSB_ERROR_NOT_FOUND;
424 } else if (r < len - sizeof(tmp)) {
425 usbi_err(DEVICE_CTX(dev), "short read %d/%d", r, len);
436 static int op_get_active_config_descriptor(struct libusb_device *dev,
437 unsigned char *buffer, size_t len, int *host_endian)
439 if (sysfs_has_descriptors) {
440 return sysfs_get_active_config_descriptor(dev, buffer, len);
443 return usbfs_get_active_config_descriptor(dev, buffer, len);
447 /* takes a usbfs fd, attempts to find the requested config and copy a certain
448 * amount of it into an output buffer. */
449 static int get_config_descriptor(struct libusb_context *ctx, int fd,
450 uint8_t config_index, unsigned char *buffer, size_t len)
455 off = lseek(fd, DEVICE_DESC_LENGTH, SEEK_SET);
457 usbi_err(ctx, "seek failed ret=%d errno=%d", off, errno);
458 return LIBUSB_ERROR_IO;
461 /* might need to skip some configuration descriptors to reach the
462 * requested configuration */
463 while (config_index > 0) {
464 r = seek_to_next_config(ctx, fd);
470 /* read the rest of the descriptor */
471 r = read(fd, buffer, len);
473 usbi_err(ctx, "read failed ret=%d errno=%d", r, errno);
474 return LIBUSB_ERROR_IO;
475 } else if (r < len) {
476 usbi_err(ctx, "short output read %d/%d", r, len);
477 return LIBUSB_ERROR_IO;
483 static int op_get_config_descriptor(struct libusb_device *dev,
484 uint8_t config_index, unsigned char *buffer, size_t len, int *host_endian)
486 char filename[PATH_MAX];
490 /* always read from usbfs: sysfs only has the active descriptor
491 * this will involve waking the device up, but oh well! */
493 /* FIXME: the above is no longer true, new kernels have all descriptors
494 * in the descriptors file. but its kinda hard to detect if the kernel
495 * is sufficiently new. */
497 __get_usbfs_path(dev, filename);
498 fd = open(filename, O_RDONLY);
500 usbi_err(DEVICE_CTX(dev),
501 "open '%s' failed, ret=%d errno=%d", filename, fd, errno);
502 return LIBUSB_ERROR_IO;
505 r = get_config_descriptor(DEVICE_CTX(dev), fd, config_index, buffer, len);
511 /* cache the active config descriptor in memory. a value of -1 means that
512 * we aren't sure which one is active, so just assume the first one.
514 static int cache_active_config(struct libusb_device *dev, int fd,
517 struct linux_device_priv *priv = __device_priv(dev);
518 struct libusb_config_descriptor config;
519 unsigned char tmp[8];
524 if (active_config == -1) {
527 r = usbi_get_config_index_by_value(dev, active_config, &idx);
531 return LIBUSB_ERROR_NOT_FOUND;
534 r = get_config_descriptor(DEVICE_CTX(dev), fd, idx, tmp, sizeof(tmp));
536 usbi_err(DEVICE_CTX(dev), "first read error %d", r);
540 usbi_parse_descriptor(tmp, "bbw", &config, 1);
541 buf = malloc(config.wTotalLength);
543 return LIBUSB_ERROR_NO_MEM;
545 r = get_config_descriptor(DEVICE_CTX(dev), fd, idx, buf,
546 config.wTotalLength);
552 if (priv->config_descriptor)
553 free(priv->config_descriptor);
554 priv->config_descriptor = buf;
558 /* send a control message to retrieve active configuration */
559 static int usbfs_get_active_config(struct libusb_device *dev, int fd)
561 unsigned char active_config = 0;
564 struct usbfs_ctrltransfer ctrl = {
565 .bmRequestType = LIBUSB_ENDPOINT_IN,
566 .bRequest = LIBUSB_REQUEST_GET_CONFIGURATION,
571 .data = &active_config
574 r = ioctl(fd, IOCTL_USBFS_CONTROL, &ctrl);
577 return LIBUSB_ERROR_NO_DEVICE;
579 usbi_err(DEVICE_CTX(dev),
580 "get_configuration failed ret=%d errno=%d", r, errno);
581 return LIBUSB_ERROR_IO;
584 return active_config;
587 static int initialize_device(struct libusb_device *dev, uint8_t busnum,
588 uint8_t devaddr, const char *sysfs_dir)
590 struct linux_device_priv *priv = __device_priv(dev);
591 unsigned char *dev_buf;
594 int active_config = 0;
595 int device_configured = 1;
598 dev->bus_number = busnum;
599 dev->device_address = devaddr;
602 priv->sysfs_dir = malloc(strlen(sysfs_dir) + 1);
603 if (!priv->sysfs_dir)
604 return LIBUSB_ERROR_NO_MEM;
605 strcpy(priv->sysfs_dir, sysfs_dir);
608 if (sysfs_has_descriptors)
611 /* cache device descriptor in memory so that we can retrieve it later
612 * without waking the device up (op_get_device_descriptor) */
614 priv->dev_descriptor = NULL;
615 priv->config_descriptor = NULL;
617 if (sysfs_can_relate_devices) {
618 int tmp = sysfs_get_active_config(dev, &active_config);
621 if (active_config == -1)
622 device_configured = 0;
625 __get_usbfs_path(dev, path);
626 fd = open(path, O_RDWR);
627 if (fd < 0 && errno == EACCES) {
628 fd = open(path, O_RDONLY);
629 /* if we only have read-only access to the device, we cannot
630 * send a control message to determine the active config. just
631 * assume the first one is active. */
636 usbi_err(DEVICE_CTX(dev), "open failed, ret=%d errno=%d", fd, errno);
637 return LIBUSB_ERROR_IO;
640 if (!sysfs_can_relate_devices) {
641 if (active_config == -1) {
642 /* if we only have read-only access to the device, we cannot
643 * send a control message to determine the active config. just
644 * assume the first one is active. */
645 usbi_warn(DEVICE_CTX(dev), "access to %s is read-only; cannot "
646 "determine active configuration descriptor", path);
648 active_config = usbfs_get_active_config(dev, fd);
649 if (active_config < 0) {
651 return active_config;
652 } else if (active_config == 0) {
653 /* some buggy devices have a configuration 0, but we're
654 * reaching into the corner of a corner case here, so let's
655 * not support buggy devices in these circumstances.
656 * stick to the specs: a configuration value of 0 means
658 usbi_dbg("assuming unconfigured device");
659 device_configured = 0;
664 dev_buf = malloc(DEVICE_DESC_LENGTH);
667 return LIBUSB_ERROR_NO_MEM;
670 r = read(fd, dev_buf, DEVICE_DESC_LENGTH);
672 usbi_err(DEVICE_CTX(dev),
673 "read descriptor failed ret=%d errno=%d", fd, errno);
676 return LIBUSB_ERROR_IO;
677 } else if (r < DEVICE_DESC_LENGTH) {
678 usbi_err(DEVICE_CTX(dev), "short descriptor read (%d)", r);
681 return LIBUSB_ERROR_IO;
684 /* bit of a hack: set num_configurations now because cache_active_config()
685 * calls usbi_get_config_index_by_value() which uses it */
686 dev->num_configurations = dev_buf[DEVICE_DESC_LENGTH - 1];
688 if (device_configured) {
689 r = cache_active_config(dev, fd, active_config);
698 priv->dev_descriptor = dev_buf;
702 static int enumerate_device(struct libusb_context *ctx,
703 struct discovered_devs **_discdevs, uint8_t busnum, uint8_t devaddr,
704 const char *sysfs_dir)
706 struct discovered_devs *discdevs;
707 unsigned long session_id;
709 struct libusb_device *dev;
712 /* FIXME: session ID is not guaranteed unique as addresses can wrap and
713 * will be reused. instead we should add a simple sysfs attribute with
715 session_id = busnum << 8 | devaddr;
716 usbi_dbg("busnum %d devaddr %d session_id %ld", busnum, devaddr,
719 dev = usbi_get_device_by_session_id(ctx, session_id);
721 usbi_dbg("using existing device for %d/%d (session %ld)",
722 busnum, devaddr, session_id);
724 usbi_dbg("allocating new device for %d/%d (session %ld)",
725 busnum, devaddr, session_id);
726 dev = usbi_alloc_device(ctx, session_id);
728 return LIBUSB_ERROR_NO_MEM;
730 r = initialize_device(dev, busnum, devaddr, sysfs_dir);
733 r = usbi_sanitize_device(dev);
738 discdevs = discovered_devs_append(*_discdevs, dev);
740 r = LIBUSB_ERROR_NO_MEM;
742 *_discdevs = discdevs;
746 libusb_unref_device(dev);
750 /* open a bus directory and adds all discovered devices to discdevs. on
751 * failure (non-zero return) the pre-existing discdevs should be destroyed
752 * (and devices freed). on success, the new discdevs pointer should be used
753 * as it may have been moved. */
754 static int usbfs_scan_busdir(struct libusb_context *ctx,
755 struct discovered_devs **_discdevs, uint8_t busnum)
758 char dirpath[PATH_MAX];
759 struct dirent *entry;
760 struct discovered_devs *discdevs = *_discdevs;
763 snprintf(dirpath, PATH_MAX, "%s/%03d", usbfs_path, busnum);
764 usbi_dbg("%s", dirpath);
765 dir = opendir(dirpath);
767 usbi_err(ctx, "opendir '%s' failed, errno=%d", dirpath, errno);
768 /* FIXME: should handle valid race conditions like hub unplugged
769 * during directory iteration - this is not an error */
770 return LIBUSB_ERROR_IO;
773 while ((entry = readdir(dir))) {
776 if (entry->d_name[0] == '.')
779 devaddr = atoi(entry->d_name);
781 usbi_dbg("unknown dir entry %s", entry->d_name);
785 r = enumerate_device(ctx, &discdevs, busnum, (uint8_t) devaddr, NULL);
790 *_discdevs = discdevs;
796 static int usbfs_get_device_list(struct libusb_context *ctx,
797 struct discovered_devs **_discdevs)
799 struct dirent *entry;
800 DIR *buses = opendir(usbfs_path);
801 struct discovered_devs *discdevs = *_discdevs;
805 usbi_err(ctx, "opendir buses failed errno=%d", errno);
806 return LIBUSB_ERROR_IO;
809 while ((entry = readdir(buses))) {
810 struct discovered_devs *discdevs_new = discdevs;
813 if (entry->d_name[0] == '.')
816 busnum = atoi(entry->d_name);
818 usbi_dbg("unknown dir entry %s", entry->d_name);
822 r = usbfs_scan_busdir(ctx, &discdevs_new, busnum);
825 discdevs = discdevs_new;
830 *_discdevs = discdevs;
835 static int sysfs_scan_device(struct libusb_context *ctx,
836 struct discovered_devs **_discdevs, const char *devname,
841 char filename[PATH_MAX];
845 usbi_dbg("scan %s", devname);
847 /* determine descriptors presence ahead of time, we need to know this
848 * when we reach initialize_device */
849 if (sysfs_has_descriptors == -1) {
852 snprintf(filename, PATH_MAX, "%s/%s/descriptors", SYSFS_DEVICE_PATH,
854 r = stat(filename, &statbuf);
855 if (r == 0 && S_ISREG(statbuf.st_mode)) {
856 usbi_dbg("sysfs descriptors available");
857 sysfs_has_descriptors = 1;
859 usbi_dbg("sysfs descriptors not available");
860 sysfs_has_descriptors = 0;
864 snprintf(filename, PATH_MAX, "%s/%s/busnum", SYSFS_DEVICE_PATH, devname);
865 fd = fopen(filename, "r");
867 if (errno == ENOENT) {
868 usbi_dbg("busnum not found, cannot relate sysfs to usbfs, "
869 "falling back on pure usbfs");
870 sysfs_can_relate_devices = 0;
872 return LIBUSB_ERROR_OTHER;
874 usbi_err(ctx, "open busnum failed, errno=%d", errno);
875 return LIBUSB_ERROR_IO;
878 sysfs_can_relate_devices = 1;
880 r = fscanf(fd, "%d", &busnum);
883 usbi_err(ctx, "fscanf busnum returned %d, errno=%d", r, errno);
884 return LIBUSB_ERROR_IO;
887 snprintf(filename, PATH_MAX, "%s/%s/devnum", SYSFS_DEVICE_PATH, devname);
888 fd = fopen(filename, "r");
890 usbi_err(ctx, "open devnum failed, errno=%d", errno);
891 return LIBUSB_ERROR_IO;
894 r = fscanf(fd, "%d", &devaddr);
897 usbi_err(ctx, "fscanf devnum returned %d, errno=%d", r, errno);
898 return LIBUSB_ERROR_IO;
901 usbi_dbg("bus=%d dev=%d", busnum, devaddr);
902 if (busnum > 255 || devaddr > 255)
903 return LIBUSB_ERROR_INVALID_PARAM;
905 return enumerate_device(ctx, _discdevs, busnum & 0xff, devaddr & 0xff,
909 static int sysfs_get_device_list(struct libusb_context *ctx,
910 struct discovered_devs **_discdevs, int *usbfs_fallback)
912 struct discovered_devs *discdevs = *_discdevs;
913 DIR *devices = opendir(SYSFS_DEVICE_PATH);
914 struct dirent *entry;
918 usbi_err(ctx, "opendir devices failed errno=%d", errno);
919 return LIBUSB_ERROR_IO;
922 while ((entry = readdir(devices))) {
923 struct discovered_devs *discdevs_new = discdevs;
925 if ((!isdigit(entry->d_name[0]) && strncmp(entry->d_name, "usb", 3))
926 || strchr(entry->d_name, ':'))
929 r = sysfs_scan_device(ctx, &discdevs_new, entry->d_name,
933 discdevs = discdevs_new;
938 *_discdevs = discdevs;
942 static int op_get_device_list(struct libusb_context *ctx,
943 struct discovered_devs **_discdevs)
945 /* we can retrieve device list and descriptors from sysfs or usbfs.
946 * sysfs is preferable, because if we use usbfs we end up resuming
947 * any autosuspended USB devices. however, sysfs is not available
948 * everywhere, so we need a usbfs fallback too.
950 * as described in the "sysfs vs usbfs" comment, sometimes we have
951 * sysfs but not enough information to relate sysfs devices to usbfs
952 * nodes. the usbfs_fallback variable is used to indicate that we should
953 * fall back on usbfs.
955 if (sysfs_can_relate_devices != 0) {
956 int usbfs_fallback = 0;
957 int r = sysfs_get_device_list(ctx, _discdevs, &usbfs_fallback);
962 return usbfs_get_device_list(ctx, _discdevs);
965 static int op_open(struct libusb_device_handle *handle)
967 struct linux_device_handle_priv *hpriv = __device_handle_priv(handle);
968 char filename[PATH_MAX];
970 __get_usbfs_path(handle->dev, filename);
971 hpriv->fd = open(filename, O_RDWR);
973 if (errno == EACCES) {
974 fprintf(stderr, "libusb couldn't open USB device %s: "
975 "Permission denied.\n"
976 "libusb requires write access to USB device nodes.\n",
978 return LIBUSB_ERROR_ACCESS;
979 } else if (errno == ENOENT) {
980 return LIBUSB_ERROR_NO_DEVICE;
982 usbi_err(HANDLE_CTX(handle),
983 "open failed, code %d errno %d", hpriv->fd, errno);
984 return LIBUSB_ERROR_IO;
988 return usbi_add_pollfd(HANDLE_CTX(handle), hpriv->fd, POLLOUT);
991 static void op_close(struct libusb_device_handle *dev_handle)
993 int fd = __device_handle_priv(dev_handle)->fd;
994 usbi_remove_pollfd(HANDLE_CTX(dev_handle), fd);
998 static int op_get_configuration(struct libusb_device_handle *handle,
1002 if (sysfs_can_relate_devices != 1)
1003 return LIBUSB_ERROR_NOT_SUPPORTED;
1005 r = sysfs_get_active_config(handle->dev, config);
1012 static int op_set_configuration(struct libusb_device_handle *handle, int config)
1014 struct linux_device_priv *priv = __device_priv(handle->dev);
1015 int fd = __device_handle_priv(handle)->fd;
1016 int r = ioctl(fd, IOCTL_USBFS_SETCONFIG, &config);
1018 if (errno == EINVAL)
1019 return LIBUSB_ERROR_NOT_FOUND;
1020 else if (errno == EBUSY)
1021 return LIBUSB_ERROR_BUSY;
1022 else if (errno == ENODEV)
1023 return LIBUSB_ERROR_NO_DEVICE;
1025 usbi_err(HANDLE_CTX(handle), "failed, error %d errno %d", r, errno);
1026 return LIBUSB_ERROR_OTHER;
1029 if (!sysfs_has_descriptors) {
1030 /* update our cached active config descriptor */
1032 if (priv->config_descriptor) {
1033 free(priv->config_descriptor);
1034 priv->config_descriptor = NULL;
1037 r = cache_active_config(handle->dev, fd, config);
1039 usbi_warn(HANDLE_CTX(handle),
1040 "failed to update cached config descriptor, error %d", r);
1047 static int op_claim_interface(struct libusb_device_handle *handle, int iface)
1049 int fd = __device_handle_priv(handle)->fd;
1050 int r = ioctl(fd, IOCTL_USBFS_CLAIMINTF, &iface);
1052 if (errno == ENOENT)
1053 return LIBUSB_ERROR_NOT_FOUND;
1054 else if (errno == EBUSY)
1055 return LIBUSB_ERROR_BUSY;
1056 else if (errno == ENODEV)
1057 return LIBUSB_ERROR_NO_DEVICE;
1059 usbi_err(HANDLE_CTX(handle),
1060 "claim interface failed, error %d errno %d", r, errno);
1061 return LIBUSB_ERROR_OTHER;
1066 static int op_release_interface(struct libusb_device_handle *handle, int iface)
1068 int fd = __device_handle_priv(handle)->fd;
1069 int r = ioctl(fd, IOCTL_USBFS_RELEASEINTF, &iface);
1071 if (errno == ENODEV)
1072 return LIBUSB_ERROR_NO_DEVICE;
1074 usbi_err(HANDLE_CTX(handle),
1075 "release interface failed, error %d errno %d", r, errno);
1076 return LIBUSB_ERROR_OTHER;
1081 static int op_set_interface(struct libusb_device_handle *handle, int iface,
1084 int fd = __device_handle_priv(handle)->fd;
1085 struct usbfs_setinterface setintf;
1088 setintf.interface = iface;
1089 setintf.altsetting = altsetting;
1090 r = ioctl(fd, IOCTL_USBFS_SETINTF, &setintf);
1092 if (errno == EINVAL)
1093 return LIBUSB_ERROR_NOT_FOUND;
1094 else if (errno == ENODEV)
1095 return LIBUSB_ERROR_NO_DEVICE;
1097 usbi_err(HANDLE_CTX(handle),
1098 "setintf failed error %d errno %d", r, errno);
1099 return LIBUSB_ERROR_OTHER;
1105 static int op_clear_halt(struct libusb_device_handle *handle,
1106 unsigned char endpoint)
1108 int fd = __device_handle_priv(handle)->fd;
1109 unsigned int _endpoint = endpoint;
1110 int r = ioctl(fd, IOCTL_USBFS_CLEAR_HALT, &_endpoint);
1112 if (errno == ENOENT)
1113 return LIBUSB_ERROR_NOT_FOUND;
1114 else if (errno == ENODEV)
1115 return LIBUSB_ERROR_NO_DEVICE;
1117 usbi_err(HANDLE_CTX(handle),
1118 "clear_halt failed error %d errno %d", r, errno);
1119 return LIBUSB_ERROR_OTHER;
1125 static int op_reset_device(struct libusb_device_handle *handle)
1127 int fd = __device_handle_priv(handle)->fd;
1128 int r = ioctl(fd, IOCTL_USBFS_RESET, NULL);
1130 if (errno == ENODEV)
1131 return LIBUSB_ERROR_NOT_FOUND;
1133 usbi_err(HANDLE_CTX(handle),
1134 "reset failed error %d errno %d", r, errno);
1135 return LIBUSB_ERROR_OTHER;
1141 static int op_kernel_driver_active(struct libusb_device_handle *handle,
1144 int fd = __device_handle_priv(handle)->fd;
1145 struct usbfs_getdriver getdrv;
1148 getdrv.interface = interface;
1149 r = ioctl(fd, IOCTL_USBFS_GETDRIVER, &getdrv);
1151 if (errno == ENODATA)
1153 else if (errno == ENODEV)
1154 return LIBUSB_ERROR_NO_DEVICE;
1156 usbi_err(HANDLE_CTX(handle),
1157 "get driver failed error %d errno %d", r, errno);
1158 return LIBUSB_ERROR_OTHER;
1164 static int op_detach_kernel_driver(struct libusb_device_handle *handle,
1167 int fd = __device_handle_priv(handle)->fd;
1168 struct usbfs_ioctl command;
1171 command.ifno = interface;
1172 command.ioctl_code = IOCTL_USBFS_DISCONNECT;
1173 command.data = NULL;
1175 r = ioctl(fd, IOCTL_USBFS_IOCTL, &command);
1177 if (errno == ENODATA)
1178 return LIBUSB_ERROR_NOT_FOUND;
1179 else if (errno == EINVAL)
1180 return LIBUSB_ERROR_INVALID_PARAM;
1181 else if (errno == ENODEV)
1182 return LIBUSB_ERROR_NO_DEVICE;
1184 usbi_err(HANDLE_CTX(handle),
1185 "detach failed error %d errno %d", r, errno);
1186 return LIBUSB_ERROR_OTHER;
1192 static int op_attach_kernel_driver(struct libusb_device_handle *handle,
1195 int fd = __device_handle_priv(handle)->fd;
1196 struct usbfs_ioctl command;
1199 command.ifno = interface;
1200 command.ioctl_code = IOCTL_USBFS_CONNECT;
1201 command.data = NULL;
1203 r = ioctl(fd, IOCTL_USBFS_IOCTL, &command);
1205 if (errno == ENODATA)
1206 return LIBUSB_ERROR_NOT_FOUND;
1207 else if (errno == EINVAL)
1208 return LIBUSB_ERROR_INVALID_PARAM;
1209 else if (errno == ENODEV)
1210 return LIBUSB_ERROR_NO_DEVICE;
1211 else if (errno == EBUSY)
1212 return LIBUSB_ERROR_BUSY;
1214 usbi_err(HANDLE_CTX(handle),
1215 "attach failed error %d errno %d", r, errno);
1216 return LIBUSB_ERROR_OTHER;
1217 } else if (r == 0) {
1218 return LIBUSB_ERROR_NOT_FOUND;
1224 static void op_destroy_device(struct libusb_device *dev)
1226 struct linux_device_priv *priv = __device_priv(dev);
1227 if (!sysfs_has_descriptors) {
1228 if (priv->dev_descriptor)
1229 free(priv->dev_descriptor);
1230 if (priv->config_descriptor)
1231 free(priv->config_descriptor);
1233 if (priv->sysfs_dir)
1234 free(priv->sysfs_dir);
1237 static void free_iso_urbs(struct linux_transfer_priv *tpriv)
1240 for (i = 0; i < tpriv->num_urbs; i++) {
1241 struct usbfs_urb *urb = tpriv->iso_urbs[i];
1247 free(tpriv->iso_urbs);
1248 tpriv->iso_urbs = NULL;
1251 static int submit_bulk_transfer(struct usbi_transfer *itransfer,
1252 unsigned char urb_type)
1254 struct libusb_transfer *transfer =
1255 __USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
1256 struct linux_transfer_priv *tpriv = usbi_transfer_get_os_priv(itransfer);
1257 struct linux_device_handle_priv *dpriv =
1258 __device_handle_priv(transfer->dev_handle);
1259 struct usbfs_urb *urbs;
1265 return LIBUSB_ERROR_BUSY;
1267 /* usbfs places a 16kb limit on bulk URBs. we divide up larger requests
1268 * into smaller units to meet such restriction, then fire off all the
1269 * units at once. it would be simpler if we just fired one unit at a time,
1270 * but there is a big performance gain through doing it this way. */
1271 int num_urbs = transfer->length / MAX_BULK_BUFFER_LENGTH;
1272 int last_urb_partial = 0;
1274 if ((transfer->length % MAX_BULK_BUFFER_LENGTH) > 0) {
1275 last_urb_partial = 1;
1278 usbi_dbg("need %d urbs for new transfer with length %d", num_urbs,
1280 alloc_size = num_urbs * sizeof(struct usbfs_urb);
1281 urbs = malloc(alloc_size);
1283 return LIBUSB_ERROR_NO_MEM;
1284 memset(urbs, 0, alloc_size);
1286 tpriv->num_urbs = num_urbs;
1287 tpriv->num_retired = 0;
1288 tpriv->reap_action = NORMAL;
1290 for (i = 0; i < num_urbs; i++) {
1291 struct usbfs_urb *urb = &urbs[i];
1292 urb->usercontext = itransfer;
1293 urb->type = urb_type;
1294 urb->endpoint = transfer->endpoint;
1295 urb->buffer = transfer->buffer + (i * MAX_BULK_BUFFER_LENGTH);
1296 if (i == num_urbs - 1 && last_urb_partial)
1297 urb->buffer_length = transfer->length % MAX_BULK_BUFFER_LENGTH;
1299 urb->buffer_length = MAX_BULK_BUFFER_LENGTH;
1301 r = ioctl(dpriv->fd, IOCTL_USBFS_SUBMITURB, urb);
1305 if (errno == ENODEV) {
1306 r = LIBUSB_ERROR_NO_DEVICE;
1308 usbi_err(TRANSFER_CTX(transfer),
1309 "submiturb failed error %d errno=%d", r, errno);
1310 r = LIBUSB_ERROR_IO;
1313 /* if the first URB submission fails, we can simply free up and
1314 * return failure immediately. */
1316 usbi_dbg("first URB failed, easy peasy");
1322 /* if it's not the first URB that failed, the situation is a bit
1323 * tricky. we must discard all previous URBs. there are
1325 * - discarding is asynchronous - discarded urbs will be reaped
1326 * later. the user must not have freed the transfer when the
1327 * discarded URBs are reaped, otherwise libusb will be using
1329 * - the earlier URBs may have completed successfully and we do
1330 * not want to throw away any data.
1331 * so, in this case we discard all the previous URBs BUT we report
1332 * that the transfer was submitted successfully. then later when
1333 * the final discard completes we can report error to the user.
1335 tpriv->reap_action = SUBMIT_FAILED;
1337 /* The URBs we haven't submitted yet we count as already
1339 tpriv->num_retired += num_urbs - i;
1340 for (j = 0; j < i; j++) {
1341 int tmp = ioctl(dpriv->fd, IOCTL_USBFS_DISCARDURB, &urbs[j]);
1342 if (tmp && errno != EINVAL)
1343 usbi_warn(TRANSFER_CTX(transfer),
1344 "unrecognised discard errno %d", errno);
1347 usbi_dbg("reporting successful submission but waiting for %d "
1348 "discards before reporting error", i);
1356 static int submit_iso_transfer(struct usbi_transfer *itransfer)
1358 struct libusb_transfer *transfer =
1359 __USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
1360 struct linux_transfer_priv *tpriv = usbi_transfer_get_os_priv(itransfer);
1361 struct linux_device_handle_priv *dpriv =
1362 __device_handle_priv(transfer->dev_handle);
1363 struct usbfs_urb **urbs;
1365 int num_packets = transfer->num_iso_packets;
1367 int this_urb_len = 0;
1369 int packet_offset = 0;
1370 unsigned int packet_len;
1371 unsigned char *urb_buffer = transfer->buffer;
1373 if (tpriv->iso_urbs)
1374 return LIBUSB_ERROR_BUSY;
1376 /* usbfs places a 32kb limit on iso URBs. we divide up larger requests
1377 * into smaller units to meet such restriction, then fire off all the
1378 * units at once. it would be simpler if we just fired one unit at a time,
1379 * but there is a big performance gain through doing it this way. */
1381 /* calculate how many URBs we need */
1382 for (i = 0; i < num_packets; i++) {
1383 int space_remaining = MAX_ISO_BUFFER_LENGTH - this_urb_len;
1384 packet_len = transfer->iso_packet_desc[i].length;
1386 if (packet_len > space_remaining) {
1388 this_urb_len = packet_len;
1390 this_urb_len += packet_len;
1393 usbi_dbg("need %d 32k URBs for transfer", num_urbs);
1395 alloc_size = num_urbs * sizeof(*urbs);
1396 urbs = malloc(alloc_size);
1398 return LIBUSB_ERROR_NO_MEM;
1399 memset(urbs, 0, alloc_size);
1401 tpriv->iso_urbs = urbs;
1402 tpriv->num_urbs = num_urbs;
1403 tpriv->num_retired = 0;
1404 tpriv->reap_action = NORMAL;
1405 tpriv->iso_packet_offset = 0;
1407 /* allocate + initialize each URB with the correct number of packets */
1408 for (i = 0; i < num_urbs; i++) {
1409 struct usbfs_urb *urb;
1410 int space_remaining_in_urb = MAX_ISO_BUFFER_LENGTH;
1411 int urb_packet_offset = 0;
1412 unsigned char *urb_buffer_orig = urb_buffer;
1416 /* swallow up all the packets we can fit into this URB */
1417 while (packet_offset < transfer->num_iso_packets) {
1418 packet_len = transfer->iso_packet_desc[packet_offset].length;
1419 if (packet_len <= space_remaining_in_urb) {
1421 urb_packet_offset++;
1423 space_remaining_in_urb -= packet_len;
1424 urb_buffer += packet_len;
1426 /* it can't fit, save it for the next URB */
1431 alloc_size = sizeof(*urb)
1432 + (urb_packet_offset * sizeof(struct usbfs_iso_packet_desc));
1433 urb = malloc(alloc_size);
1435 free_iso_urbs(tpriv);
1436 return LIBUSB_ERROR_NO_MEM;
1438 memset(urb, 0, alloc_size);
1441 /* populate packet lengths */
1442 for (j = 0, k = packet_offset - urb_packet_offset;
1443 k < packet_offset; k++, j++) {
1444 packet_len = transfer->iso_packet_desc[k].length;
1445 urb->iso_frame_desc[j].length = packet_len;
1448 urb->usercontext = itransfer;
1449 urb->type = USBFS_URB_TYPE_ISO;
1450 /* FIXME: interface for non-ASAP data? */
1451 urb->flags = USBFS_URB_ISO_ASAP;
1452 urb->endpoint = transfer->endpoint;
1453 urb->number_of_packets = urb_packet_offset;
1454 urb->buffer = urb_buffer_orig;
1458 for (i = 0; i < num_urbs; i++) {
1459 int r = ioctl(dpriv->fd, IOCTL_USBFS_SUBMITURB, urbs[i]);
1463 if (errno == ENODEV) {
1464 r = LIBUSB_ERROR_NO_DEVICE;
1466 usbi_err(TRANSFER_CTX(transfer),
1467 "submiturb failed error %d errno=%d", r, errno);
1468 r = LIBUSB_ERROR_IO;
1471 /* if the first URB submission fails, we can simply free up and
1472 * return failure immediately. */
1474 usbi_dbg("first URB failed, easy peasy");
1475 free_iso_urbs(tpriv);
1479 /* if it's not the first URB that failed, the situation is a bit
1480 * tricky. we must discard all previous URBs. there are
1482 * - discarding is asynchronous - discarded urbs will be reaped
1483 * later. the user must not have freed the transfer when the
1484 * discarded URBs are reaped, otherwise libusb will be using
1486 * - the earlier URBs may have completed successfully and we do
1487 * not want to throw away any data.
1488 * so, in this case we discard all the previous URBs BUT we report
1489 * that the transfer was submitted successfully. then later when
1490 * the final discard completes we can report error to the user.
1492 tpriv->reap_action = SUBMIT_FAILED;
1494 /* The URBs we haven't submitted yet we count as already
1496 tpriv->num_retired = num_urbs - i;
1497 for (j = 0; j < i; j++) {
1498 int tmp = ioctl(dpriv->fd, IOCTL_USBFS_DISCARDURB, urbs[j]);
1499 if (tmp && errno != EINVAL)
1500 usbi_warn(TRANSFER_CTX(transfer),
1501 "unrecognised discard errno %d", errno);
1504 usbi_dbg("reporting successful submission but waiting for %d "
1505 "discards before reporting error", i);
1513 static int submit_control_transfer(struct usbi_transfer *itransfer)
1515 struct linux_transfer_priv *tpriv = usbi_transfer_get_os_priv(itransfer);
1516 struct libusb_transfer *transfer =
1517 __USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
1518 struct linux_device_handle_priv *dpriv =
1519 __device_handle_priv(transfer->dev_handle);
1520 struct usbfs_urb *urb;
1524 return LIBUSB_ERROR_BUSY;
1526 if (transfer->length - LIBUSB_CONTROL_SETUP_SIZE > MAX_CTRL_BUFFER_LENGTH)
1527 return LIBUSB_ERROR_INVALID_PARAM;
1529 urb = malloc(sizeof(struct usbfs_urb));
1531 return LIBUSB_ERROR_NO_MEM;
1532 memset(urb, 0, sizeof(struct usbfs_urb));
1534 tpriv->reap_action = NORMAL;
1536 urb->usercontext = itransfer;
1537 urb->type = USBFS_URB_TYPE_CONTROL;
1538 urb->endpoint = transfer->endpoint;
1539 urb->buffer = transfer->buffer;
1540 urb->buffer_length = transfer->length;
1542 r = ioctl(dpriv->fd, IOCTL_USBFS_SUBMITURB, urb);
1546 if (errno == ENODEV)
1547 return LIBUSB_ERROR_NO_DEVICE;
1549 usbi_err(TRANSFER_CTX(transfer),
1550 "submiturb failed error %d errno=%d", r, errno);
1551 return LIBUSB_ERROR_IO;
1556 static int op_submit_transfer(struct usbi_transfer *itransfer)
1558 struct libusb_transfer *transfer =
1559 __USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
1561 switch (transfer->type) {
1562 case LIBUSB_TRANSFER_TYPE_CONTROL:
1563 return submit_control_transfer(itransfer);
1564 case LIBUSB_TRANSFER_TYPE_BULK:
1565 return submit_bulk_transfer(itransfer, USBFS_URB_TYPE_BULK);
1566 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1567 return submit_bulk_transfer(itransfer, USBFS_URB_TYPE_INTERRUPT);
1568 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1569 return submit_iso_transfer(itransfer);
1571 usbi_err(TRANSFER_CTX(transfer),
1572 "unknown endpoint type %d", transfer->type);
1573 return LIBUSB_ERROR_INVALID_PARAM;
1577 static int cancel_control_transfer(struct usbi_transfer *itransfer)
1579 struct linux_transfer_priv *tpriv = usbi_transfer_get_os_priv(itransfer);
1580 struct libusb_transfer *transfer =
1581 __USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
1582 struct linux_device_handle_priv *dpriv =
1583 __device_handle_priv(transfer->dev_handle);
1587 return LIBUSB_ERROR_NOT_FOUND;
1589 tpriv->reap_action = CANCELLED;
1590 r = ioctl(dpriv->fd, IOCTL_USBFS_DISCARDURB, tpriv->urbs);
1592 if (errno == EINVAL) {
1593 usbi_dbg("URB not found --> assuming ready to be reaped");
1596 usbi_err(TRANSFER_CTX(transfer),
1597 "unrecognised DISCARD code %d", errno);
1598 return LIBUSB_ERROR_OTHER;
1605 static int cancel_bulk_transfer(struct usbi_transfer *itransfer)
1607 struct linux_transfer_priv *tpriv = usbi_transfer_get_os_priv(itransfer);
1608 struct libusb_transfer *transfer =
1609 __USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
1610 struct linux_device_handle_priv *dpriv =
1611 __device_handle_priv(transfer->dev_handle);
1615 return LIBUSB_ERROR_NOT_FOUND;
1617 tpriv->reap_action = CANCELLED;
1618 for (i = 0; i < tpriv->num_urbs; i++) {
1619 int tmp = ioctl(dpriv->fd, IOCTL_USBFS_DISCARDURB, &tpriv->urbs[i]);
1620 if (tmp && errno != EINVAL)
1621 usbi_warn(TRANSFER_CTX(transfer),
1622 "unrecognised discard errno %d", errno);
1627 static int cancel_iso_transfer(struct usbi_transfer *itransfer)
1629 struct linux_transfer_priv *tpriv = usbi_transfer_get_os_priv(itransfer);
1630 struct libusb_transfer *transfer =
1631 __USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
1632 struct linux_device_handle_priv *dpriv =
1633 __device_handle_priv(transfer->dev_handle);
1636 if (!tpriv->iso_urbs)
1637 return LIBUSB_ERROR_NOT_FOUND;
1639 tpriv->reap_action = CANCELLED;
1640 for (i = 0; i < tpriv->num_urbs; i++) {
1641 int tmp = ioctl(dpriv->fd, IOCTL_USBFS_DISCARDURB, tpriv->iso_urbs[i]);
1642 if (tmp && errno != EINVAL)
1643 usbi_warn(TRANSFER_CTX(transfer),
1644 "unrecognised discard errno %d", errno);
1649 static int op_cancel_transfer(struct usbi_transfer *itransfer)
1651 struct libusb_transfer *transfer =
1652 __USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
1654 switch (transfer->type) {
1655 case LIBUSB_TRANSFER_TYPE_CONTROL:
1656 return cancel_control_transfer(itransfer);
1657 case LIBUSB_TRANSFER_TYPE_BULK:
1658 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1659 return cancel_bulk_transfer(itransfer);
1660 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1661 return cancel_iso_transfer(itransfer);
1663 usbi_err(TRANSFER_CTX(transfer),
1664 "unknown endpoint type %d", transfer->type);
1665 return LIBUSB_ERROR_INVALID_PARAM;
1669 static void op_clear_transfer_priv(struct usbi_transfer *itransfer)
1671 struct libusb_transfer *transfer =
1672 __USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
1673 struct linux_transfer_priv *tpriv = usbi_transfer_get_os_priv(itransfer);
1675 switch (transfer->type) {
1676 case LIBUSB_TRANSFER_TYPE_CONTROL:
1677 case LIBUSB_TRANSFER_TYPE_BULK:
1678 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1682 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1683 free_iso_urbs(tpriv);
1686 usbi_err(TRANSFER_CTX(transfer),
1687 "unknown endpoint type %d", transfer->type);
1691 static int handle_bulk_completion(struct usbi_transfer *itransfer,
1692 struct usbfs_urb *urb)
1694 struct linux_transfer_priv *tpriv = usbi_transfer_get_os_priv(itransfer);
1695 int num_urbs = tpriv->num_urbs;
1696 int urb_idx = urb - tpriv->urbs;
1697 enum libusb_transfer_status status = LIBUSB_TRANSFER_COMPLETED;
1699 usbi_dbg("handling completion status %d of bulk urb %d/%d", urb->status,
1700 urb_idx + 1, num_urbs);
1702 tpriv->num_retired++;
1704 if (urb->status == 0 ||
1705 (urb->status == -EOVERFLOW && urb->actual_length > 0))
1706 itransfer->transferred += urb->actual_length;
1708 if (tpriv->reap_action != NORMAL) {
1709 /* cancelled, submit_fail, or completed early */
1710 if (urb->status == 0 && tpriv->reap_action == COMPLETED_EARLY) {
1711 /* FIXME we could solve this extreme corner case with a memmove
1713 usbi_warn(ITRANSFER_CTX(itransfer), "SOME DATA LOST! "
1714 "(completed early but remaining urb completed)");
1716 usbi_dbg("CANCEL: urb status %d", urb->status);
1718 if (tpriv->num_retired == num_urbs) {
1719 usbi_dbg("CANCEL: last URB handled, reporting");
1720 if (tpriv->reap_action == CANCELLED) {
1723 usbi_handle_transfer_cancellation(itransfer);
1726 if (tpriv->reap_action != COMPLETED_EARLY)
1727 status = LIBUSB_TRANSFER_ERROR;
1733 switch (urb->status) {
1737 usbi_dbg("detected endpoint stall");
1738 status = LIBUSB_TRANSFER_STALL;
1741 /* overflow can only ever occur in the last urb */
1742 usbi_dbg("overflow, actual_length=%d", urb->actual_length);
1743 status = LIBUSB_TRANSFER_OVERFLOW;
1748 usbi_dbg("low level error %d", urb->status);
1749 status = LIBUSB_TRANSFER_ERROR;
1752 usbi_warn(ITRANSFER_CTX(itransfer),
1753 "unrecognised urb status %d", urb->status);
1754 status = LIBUSB_TRANSFER_ERROR;
1758 /* if we're the last urb or we got less data than requested then we're
1760 if (urb_idx == num_urbs - 1) {
1761 usbi_dbg("last URB in transfer --> complete!");
1762 } else if (urb->actual_length < urb->buffer_length) {
1763 struct libusb_transfer *transfer =
1764 __USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
1765 struct linux_device_handle_priv *dpriv =
1766 __device_handle_priv(transfer->dev_handle);
1769 usbi_dbg("short transfer %d/%d --> complete!", urb->actual_length,
1770 urb->buffer_length);
1772 /* we have to cancel the remaining urbs and wait for their completion
1773 * before reporting results */
1774 tpriv->reap_action = COMPLETED_EARLY;
1775 for (i = urb_idx + 1; i < tpriv->num_urbs; i++) {
1776 int r = ioctl(dpriv->fd, IOCTL_USBFS_DISCARDURB, &tpriv->urbs[i]);
1777 if (r && errno != EINVAL)
1778 usbi_warn(TRANSFER_CTX(transfer),
1779 "unrecognised discard errno %d", errno);
1789 usbi_handle_transfer_completion(itransfer, status);
1793 static int handle_iso_completion(struct usbi_transfer *itransfer,
1794 struct usbfs_urb *urb)
1796 struct libusb_transfer *transfer =
1797 __USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
1798 struct linux_transfer_priv *tpriv = usbi_transfer_get_os_priv(itransfer);
1799 int num_urbs = tpriv->num_urbs;
1803 for (i = 0; i < num_urbs; i++) {
1804 if (urb == tpriv->iso_urbs[i]) {
1810 usbi_err(TRANSFER_CTX(transfer), "could not locate urb!");
1811 return LIBUSB_ERROR_NOT_FOUND;
1814 usbi_dbg("handling completion status %d of iso urb %d/%d", urb->status,
1817 if (urb->status == 0) {
1818 /* copy isochronous results back in */
1820 for (i = 0; i < urb->number_of_packets; i++) {
1821 struct usbfs_iso_packet_desc *urb_desc = &urb->iso_frame_desc[i];
1822 struct libusb_iso_packet_descriptor *lib_desc =
1823 &transfer->iso_packet_desc[tpriv->iso_packet_offset++];
1824 lib_desc->status = urb_desc->status;
1825 lib_desc->actual_length = urb_desc->actual_length;
1829 tpriv->num_retired++;
1831 if (tpriv->reap_action != NORMAL) { /* cancelled or submit_fail */
1832 usbi_dbg("CANCEL: urb status %d", urb->status);
1834 if (tpriv->num_retired == num_urbs) {
1835 usbi_dbg("CANCEL: last URB handled, reporting");
1836 free_iso_urbs(tpriv);
1837 if (tpriv->reap_action == CANCELLED)
1838 usbi_handle_transfer_cancellation(itransfer);
1840 usbi_handle_transfer_completion(itransfer,
1841 LIBUSB_TRANSFER_ERROR);
1846 switch (urb->status) {
1852 usbi_dbg("low-level USB error %d", urb->status);
1855 usbi_warn(TRANSFER_CTX(transfer),
1856 "unrecognised urb status %d", urb->status);
1860 /* if we're the last urb or we got less data than requested then we're
1862 if (urb_idx == num_urbs) {
1863 usbi_dbg("last URB in transfer --> complete!");
1864 free_iso_urbs(tpriv);
1865 usbi_handle_transfer_completion(itransfer, LIBUSB_TRANSFER_COMPLETED);
1871 static int handle_control_completion(struct usbi_transfer *itransfer,
1872 struct usbfs_urb *urb)
1874 struct linux_transfer_priv *tpriv = usbi_transfer_get_os_priv(itransfer);
1877 usbi_dbg("handling completion status %d", urb->status);
1879 if (urb->status == 0)
1880 itransfer->transferred += urb->actual_length;
1882 if (tpriv->reap_action == CANCELLED) {
1883 if (urb->status != 0 && urb->status != -ENOENT)
1884 usbi_warn(ITRANSFER_CTX(itransfer),
1885 "cancel: unrecognised urb status %d", urb->status);
1888 usbi_handle_transfer_cancellation(itransfer);
1892 switch (urb->status) {
1894 itransfer->transferred = urb->actual_length;
1895 status = LIBUSB_TRANSFER_COMPLETED;
1898 usbi_dbg("unsupported control request");
1899 status = LIBUSB_TRANSFER_STALL;
1904 usbi_dbg("low-level bus error occurred");
1905 status = LIBUSB_TRANSFER_ERROR;
1908 usbi_warn(ITRANSFER_CTX(itransfer),
1909 "unrecognised urb status %d", urb->status);
1910 status = LIBUSB_TRANSFER_ERROR;
1916 usbi_handle_transfer_completion(itransfer, status);
1920 static int reap_for_handle(struct libusb_device_handle *handle)
1922 struct linux_device_handle_priv *hpriv = __device_handle_priv(handle);
1924 struct usbfs_urb *urb;
1925 struct usbi_transfer *itransfer;
1926 struct libusb_transfer *transfer;
1928 r = ioctl(hpriv->fd, IOCTL_USBFS_REAPURBNDELAY, &urb);
1929 if (r == -1 && errno == EAGAIN)
1932 if (errno == ENODEV)
1933 return LIBUSB_ERROR_NO_DEVICE;
1935 usbi_err(HANDLE_CTX(handle), "reap failed error %d errno=%d",
1937 return LIBUSB_ERROR_IO;
1940 itransfer = urb->usercontext;
1941 transfer = __USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
1943 usbi_dbg("urb type=%d status=%d transferred=%d", urb->type, urb->status,
1944 urb->actual_length);
1946 switch (transfer->type) {
1947 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1948 return handle_iso_completion(itransfer, urb);
1949 case LIBUSB_TRANSFER_TYPE_BULK:
1950 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1951 return handle_bulk_completion(itransfer, urb);
1952 case LIBUSB_TRANSFER_TYPE_CONTROL:
1953 return handle_control_completion(itransfer, urb);
1955 usbi_err(HANDLE_CTX(handle), "unrecognised endpoint type %x",
1957 return LIBUSB_ERROR_OTHER;
1961 static int op_handle_events(struct libusb_context *ctx,
1962 struct pollfd *fds, nfds_t nfds, int num_ready)
1967 pthread_mutex_lock(&ctx->open_devs_lock);
1968 for (i = 0; i < nfds && num_ready > 0; i++) {
1969 struct pollfd *pollfd = &fds[i];
1970 struct libusb_device_handle *handle;
1971 struct linux_device_handle_priv *hpriv = NULL;
1973 if (!pollfd->revents)
1977 list_for_each_entry(handle, &ctx->open_devs, list) {
1978 hpriv = __device_handle_priv(handle);
1979 if (hpriv->fd == pollfd->fd)
1983 if (pollfd->revents & POLLERR) {
1984 usbi_remove_pollfd(HANDLE_CTX(handle), hpriv->fd);
1985 usbi_handle_disconnect(handle);
1989 r = reap_for_handle(handle);
1990 if (r == 1 || r == LIBUSB_ERROR_NO_DEVICE)
1998 pthread_mutex_unlock(&ctx->open_devs_lock);
2002 static int op_clock_gettime(int clk_id, struct timespec *tp)
2005 case USBI_CLOCK_MONOTONIC:
2006 return clock_gettime(CLOCK_MONOTONIC, tp);
2007 case USBI_CLOCK_REALTIME:
2008 return clock_gettime(CLOCK_REALTIME, tp);
2010 return LIBUSB_ERROR_INVALID_PARAM;
2014 const struct usbi_os_backend linux_usbfs_backend = {
2015 .name = "Linux usbfs",
2018 .get_device_list = op_get_device_list,
2019 .get_device_descriptor = op_get_device_descriptor,
2020 .get_active_config_descriptor = op_get_active_config_descriptor,
2021 .get_config_descriptor = op_get_config_descriptor,
2025 .get_configuration = op_get_configuration,
2026 .set_configuration = op_set_configuration,
2027 .claim_interface = op_claim_interface,
2028 .release_interface = op_release_interface,
2030 .set_interface_altsetting = op_set_interface,
2031 .clear_halt = op_clear_halt,
2032 .reset_device = op_reset_device,
2034 .kernel_driver_active = op_kernel_driver_active,
2035 .detach_kernel_driver = op_detach_kernel_driver,
2036 .attach_kernel_driver = op_attach_kernel_driver,
2038 .destroy_device = op_destroy_device,
2040 .submit_transfer = op_submit_transfer,
2041 .cancel_transfer = op_cancel_transfer,
2042 .clear_transfer_priv = op_clear_transfer_priv,
2044 .handle_events = op_handle_events,
2046 .clock_gettime = op_clock_gettime,
2048 .device_priv_size = sizeof(struct linux_device_priv),
2049 .device_handle_priv_size = sizeof(struct linux_device_handle_priv),
2050 .transfer_priv_size = sizeof(struct linux_transfer_priv),
2051 .add_iso_packet_size = 0,