linux: Fix libusb_get_device_speed() not working on wrapped devices
[platform/upstream/libusb.git] / libusb / os / linux_usbfs.c
1 /* -*- Mode: C; c-basic-offset:8 ; indent-tabs-mode:t -*- */
2 /*
3  * Linux usbfs backend for libusb
4  * Copyright © 2007-2009 Daniel Drake <dsd@gentoo.org>
5  * Copyright © 2001 Johannes Erdfelt <johannes@erdfelt.com>
6  * Copyright © 2013 Nathan Hjelm <hjelmn@mac.com>
7  * Copyright © 2012-2013 Hans de Goede <hdegoede@redhat.com>
8  * Copyright © 2020 Chris Dickens <christopher.a.dickens@gmail.com>
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  */
24
25 #include "libusbi.h"
26 #include "linux_usbfs.h"
27
28 #include <alloca.h>
29 #include <ctype.h>
30 #include <dirent.h>
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <sys/ioctl.h>
36 #include <sys/mman.h>
37 #include <sys/utsname.h>
38 #include <sys/vfs.h>
39 #include <unistd.h>
40
41 /* sysfs vs usbfs:
42  * opening a usbfs node causes the device to be resumed, so we attempt to
43  * avoid this during enumeration.
44  *
45  * sysfs allows us to read the kernel's in-memory copies of device descriptors
46  * and so forth, avoiding the need to open the device:
47  *  - The binary "descriptors" file contains all config descriptors since
48  *    2.6.26, commit 217a9081d8e69026186067711131b77f0ce219ed
49  *  - The binary "descriptors" file was added in 2.6.23, commit
50  *    69d42a78f935d19384d1f6e4f94b65bb162b36df, but it only contains the
51  *    active config descriptors
52  *  - The "busnum" file was added in 2.6.22, commit
53  *    83f7d958eab2fbc6b159ee92bf1493924e1d0f72
54  *  - The "devnum" file has been present since pre-2.6.18
55  *  - the "bConfigurationValue" file has been present since pre-2.6.18
56  *
57  * If we have bConfigurationValue, busnum, and devnum, then we can determine
58  * the active configuration without having to open the usbfs node in RDWR mode.
59  * The busnum file is important as that is the only way we can relate sysfs
60  * devices to usbfs nodes.
61  *
62  * If we also have all descriptors, we can obtain the device descriptor and
63  * configuration without touching usbfs at all.
64  */
65
66 /* endianness for multi-byte fields:
67  *
68  * Descriptors exposed by usbfs have the multi-byte fields in the device
69  * descriptor as host endian. Multi-byte fields in the other descriptors are
70  * bus-endian. The kernel documentation says otherwise, but it is wrong.
71  *
72  * In sysfs all descriptors are bus-endian.
73  */
74
75 #define USBDEV_PATH             "/dev"
76 #define USB_DEVTMPFS_PATH       "/dev/bus/usb"
77
78 /* use usbdev*.* device names in /dev instead of the usbfs bus directories */
79 static int usbdev_names = 0;
80
81 /* Linux has changed the maximum length of an individual isochronous packet
82  * over time.  Initially this limit was 1,023 bytes, but Linux 2.6.18
83  * (commit 3612242e527eb47ee4756b5350f8bdf791aa5ede) increased this value to
84  * 8,192 bytes to support higher bandwidth devices.  Linux 3.10
85  * (commit e2e2f0ea1c935edcf53feb4c4c8fdb4f86d57dd9) further increased this
86  * value to 49,152 bytes to support super speed devices.  Linux 5.2
87  * (commit 8a1dbc8d91d3d1602282c7e6b4222c7759c916fa) even further increased
88  * this value to 98,304 bytes to support super speed plus devices.
89  */
90 static unsigned int max_iso_packet_len = 0;
91
92 /* is sysfs available (mounted) ? */
93 static int sysfs_available = -1;
94
95 /* how many times have we initted (and not exited) ? */
96 static int init_count = 0;
97
98 #ifdef __ANDROID__
99 /* have no authority to operate usb device directly */
100 static int weak_authority = 0;
101 #endif
102
103 /* Serialize hotplug start/stop */
104 static usbi_mutex_static_t linux_hotplug_startstop_lock = USBI_MUTEX_INITIALIZER;
105 /* Serialize scan-devices, event-thread, and poll */
106 usbi_mutex_static_t linux_hotplug_lock = USBI_MUTEX_INITIALIZER;
107
108 static int linux_scan_devices(struct libusb_context *ctx);
109 static int detach_kernel_driver_and_claim(struct libusb_device_handle *, uint8_t);
110
111 #if !defined(HAVE_LIBUDEV)
112 static int linux_default_scan_devices(struct libusb_context *ctx);
113 #endif
114
115 struct kernel_version {
116         int major;
117         int minor;
118         int sublevel;
119 };
120
121 struct config_descriptor {
122         struct usbi_configuration_descriptor *desc;
123         size_t actual_len;
124 };
125
126 struct linux_device_priv {
127         char *sysfs_dir;
128         void *descriptors;
129         size_t descriptors_len;
130         struct config_descriptor *config_descriptors;
131         uint8_t active_config; /* cache val for !sysfs_available  */
132 };
133
134 struct linux_device_handle_priv {
135         int fd;
136         int fd_removed;
137         int fd_keep;
138         uint32_t caps;
139 };
140
141 enum reap_action {
142         NORMAL = 0,
143         /* submission failed after the first URB, so await cancellation/completion
144          * of all the others */
145         SUBMIT_FAILED,
146
147         /* cancelled by user or timeout */
148         CANCELLED,
149
150         /* completed multi-URB transfer in non-final URB */
151         COMPLETED_EARLY,
152
153         /* one or more urbs encountered a low-level error */
154         ERROR,
155 };
156
157 struct linux_transfer_priv {
158         union {
159                 struct usbfs_urb *urbs;
160                 struct usbfs_urb **iso_urbs;
161         };
162
163         enum reap_action reap_action;
164         int num_urbs;
165         int num_retired;
166         enum libusb_transfer_status reap_status;
167
168         /* next iso packet in user-supplied transfer to be populated */
169         int iso_packet_offset;
170 };
171
172 static int get_usbfs_fd(struct libusb_device *dev, mode_t mode, int silent)
173 {
174         struct libusb_context *ctx = DEVICE_CTX(dev);
175         char path[24];
176         int fd;
177
178         if (usbdev_names)
179                 sprintf(path, USBDEV_PATH "/usbdev%u.%u",
180                         dev->bus_number, dev->device_address);
181         else
182                 sprintf(path, USB_DEVTMPFS_PATH "/%03u/%03u",
183                         dev->bus_number, dev->device_address);
184
185         fd = open(path, mode | O_CLOEXEC);
186         if (fd != -1)
187                 return fd; /* Success */
188
189         if (errno == ENOENT) {
190                 const long delay_ms = 10L;
191                 const struct timespec delay_ts = { 0L, delay_ms * 1000L * 1000L };
192
193                 if (!silent)
194                         usbi_err(ctx, "File doesn't exist, wait %ld ms and try again", delay_ms);
195
196                 /* Wait 10ms for USB device path creation.*/
197                 nanosleep(&delay_ts, NULL);
198
199                 fd = open(path, mode | O_CLOEXEC);
200                 if (fd != -1)
201                         return fd; /* Success */
202         }
203
204         if (!silent) {
205                 usbi_err(ctx, "libusb couldn't open USB device %s, errno=%d", path, errno);
206                 if (errno == EACCES && mode == O_RDWR)
207                         usbi_err(ctx, "libusb requires write access to USB device nodes");
208         }
209
210         if (errno == EACCES)
211                 return LIBUSB_ERROR_ACCESS;
212         if (errno == ENOENT)
213                 return LIBUSB_ERROR_NO_DEVICE;
214         return LIBUSB_ERROR_IO;
215 }
216
217 /* check dirent for a /dev/usbdev%d.%d name
218  * optionally return bus/device on success */
219 static int is_usbdev_entry(const char *name, uint8_t *bus_p, uint8_t *dev_p)
220 {
221         int busnum, devnum;
222
223         if (sscanf(name, "usbdev%d.%d", &busnum, &devnum) != 2)
224                 return 0;
225         if (busnum < 0 || busnum > UINT8_MAX || devnum < 0 || devnum > UINT8_MAX) {
226                 usbi_dbg("invalid usbdev format '%s'", name);
227                 return 0;
228         }
229
230         usbi_dbg("found: %s", name);
231         if (bus_p)
232                 *bus_p = (uint8_t)busnum;
233         if (dev_p)
234                 *dev_p = (uint8_t)devnum;
235         return 1;
236 }
237
238 static const char *find_usbfs_path(void)
239 {
240         const char *path;
241         DIR *dir;
242         struct dirent *entry;
243
244         path = USB_DEVTMPFS_PATH;
245         dir = opendir(path);
246         if (dir) {
247                 while ((entry = readdir(dir))) {
248                         if (entry->d_name[0] == '.')
249                                 continue;
250
251                         /* We assume if we find any files that it must be the right place */
252                         break;
253                 }
254
255                 closedir(dir);
256
257                 if (entry)
258                         return path;
259         }
260
261         /* look for /dev/usbdev*.* if the normal place fails */
262         path = USBDEV_PATH;
263         dir = opendir(path);
264         if (dir) {
265                 while ((entry = readdir(dir))) {
266                         if (entry->d_name[0] == '.')
267                                 continue;
268
269                         if (is_usbdev_entry(entry->d_name, NULL, NULL)) {
270                                 /* found one; that's enough */
271                                 break;
272                         }
273                 }
274
275                 closedir(dir);
276
277                 if (entry) {
278                         usbdev_names = 1;
279                         return path;
280                 }
281         }
282
283 /* On udev based systems without any usb-devices /dev/bus/usb will not
284  * exist. So if we've not found anything and we're using udev for hotplug
285  * simply assume /dev/bus/usb rather then making libusb_init fail.
286  * Make the same assumption for Android where SELinux policies might block us
287  * from reading /dev on newer devices. */
288 #if defined(HAVE_LIBUDEV) || defined(__ANDROID__)
289         return USB_DEVTMPFS_PATH;
290 #else
291         return NULL;
292 #endif
293 }
294
295 static int get_kernel_version(struct libusb_context *ctx,
296         struct kernel_version *ver)
297 {
298         struct utsname uts;
299         int atoms;
300
301         if (uname(&uts) < 0) {
302                 usbi_err(ctx, "uname failed, errno=%d", errno);
303                 return -1;
304         }
305
306         atoms = sscanf(uts.release, "%d.%d.%d", &ver->major, &ver->minor, &ver->sublevel);
307         if (atoms < 2) {
308                 usbi_err(ctx, "failed to parse uname release '%s'", uts.release);
309                 return -1;
310         }
311
312         if (atoms < 3)
313                 ver->sublevel = -1;
314
315         usbi_dbg("reported kernel version is %s", uts.release);
316
317         return 0;
318 }
319
320 static int kernel_version_ge(const struct kernel_version *ver,
321         int major, int minor, int sublevel)
322 {
323         if (ver->major > major)
324                 return 1;
325         else if (ver->major < major)
326                 return 0;
327
328         /* kmajor == major */
329         if (ver->minor > minor)
330                 return 1;
331         else if (ver->minor < minor)
332                 return 0;
333
334         /* kminor == minor */
335         if (ver->sublevel == -1)
336                 return sublevel == 0;
337
338         return ver->sublevel >= sublevel;
339 }
340
341 static int op_init(struct libusb_context *ctx)
342 {
343         struct kernel_version kversion;
344         const char *usbfs_path;
345         int r;
346
347         if (get_kernel_version(ctx, &kversion) < 0)
348                 return LIBUSB_ERROR_OTHER;
349
350         if (!kernel_version_ge(&kversion, 2, 6, 32)) {
351                 usbi_err(ctx, "kernel version is too old (reported as %d.%d.%d)",
352                          kversion.major, kversion.minor,
353                          kversion.sublevel != -1 ? kversion.sublevel : 0);
354                 return LIBUSB_ERROR_NOT_SUPPORTED;
355         }
356
357         usbfs_path = find_usbfs_path();
358         if (!usbfs_path) {
359                 usbi_err(ctx, "could not find usbfs");
360                 return LIBUSB_ERROR_OTHER;
361         }
362
363         usbi_dbg("found usbfs at %s", usbfs_path);
364
365         if (!max_iso_packet_len) {
366                 if (kernel_version_ge(&kversion, 5, 2, 0))
367                         max_iso_packet_len = 98304;
368                 else if (kernel_version_ge(&kversion, 3, 10, 0))
369                         max_iso_packet_len = 49152;
370                 else
371                         max_iso_packet_len = 8192;
372         }
373
374         usbi_dbg("max iso packet length is (likely) %u bytes", max_iso_packet_len);
375
376         if (sysfs_available == -1) {
377                 struct statfs statfsbuf;
378
379                 r = statfs(SYSFS_MOUNT_PATH, &statfsbuf);
380                 if (r == 0 && statfsbuf.f_type == SYSFS_MAGIC) {
381                         usbi_dbg("sysfs is available");
382                         sysfs_available = 1;
383                 } else {
384                         usbi_warn(ctx, "sysfs not mounted");
385                         sysfs_available = 0;
386                 }
387         }
388
389 #ifdef __ANDROID__
390         if (weak_authority) {
391                 return LIBUSB_SUCCESS;
392         }
393 #endif
394
395         usbi_mutex_static_lock(&linux_hotplug_startstop_lock);
396         r = LIBUSB_SUCCESS;
397         if (init_count == 0) {
398                 /* start up hotplug event handler */
399                 r = linux_start_event_monitor();
400         }
401         if (r == LIBUSB_SUCCESS) {
402                 r = linux_scan_devices(ctx);
403                 if (r == LIBUSB_SUCCESS)
404                         init_count++;
405                 else if (init_count == 0)
406                         linux_stop_event_monitor();
407         } else {
408                 usbi_err(ctx, "error starting hotplug event monitor");
409         }
410         usbi_mutex_static_unlock(&linux_hotplug_startstop_lock);
411
412         return r;
413 }
414
415 static void op_exit(struct libusb_context *ctx)
416 {
417         UNUSED(ctx);
418         usbi_mutex_static_lock(&linux_hotplug_startstop_lock);
419         assert(init_count != 0);
420         if (!--init_count) {
421                 /* tear down event handler */
422                 linux_stop_event_monitor();
423         }
424         usbi_mutex_static_unlock(&linux_hotplug_startstop_lock);
425 }
426
427 static int op_set_option(struct libusb_context *ctx, enum libusb_option option, va_list ap)
428 {
429         UNUSED(ctx);
430         UNUSED(ap);
431
432         switch (option) {
433 #ifdef __ANDROID__
434         case LIBUSB_OPTION_WEAK_AUTHORITY:
435                 usbi_dbg("set libusb has weak authority");
436                 weak_authority = 1;
437                 return LIBUSB_SUCCESS;
438 #endif
439         default:
440                 return LIBUSB_ERROR_NOT_SUPPORTED;
441         }
442 }
443
444 static int linux_scan_devices(struct libusb_context *ctx)
445 {
446         int ret;
447
448         usbi_mutex_static_lock(&linux_hotplug_lock);
449
450 #if defined(HAVE_LIBUDEV)
451         ret = linux_udev_scan_devices(ctx);
452 #else
453         ret = linux_default_scan_devices(ctx);
454 #endif
455
456         usbi_mutex_static_unlock(&linux_hotplug_lock);
457
458         return ret;
459 }
460
461 static void op_hotplug_poll(void)
462 {
463         linux_hotplug_poll();
464 }
465
466 static int open_sysfs_attr(struct libusb_context *ctx,
467         const char *sysfs_dir, const char *attr)
468 {
469         char filename[256];
470         int fd;
471
472         snprintf(filename, sizeof(filename), SYSFS_DEVICE_PATH "/%s/%s", sysfs_dir, attr);
473         fd = open(filename, O_RDONLY | O_CLOEXEC);
474         if (fd < 0) {
475                 if (errno == ENOENT) {
476                         /* File doesn't exist. Assume the device has been
477                            disconnected (see trac ticket #70). */
478                         return LIBUSB_ERROR_NO_DEVICE;
479                 }
480                 usbi_err(ctx, "open %s failed, errno=%d", filename, errno);
481                 return LIBUSB_ERROR_IO;
482         }
483
484         return fd;
485 }
486
487 /* Note only suitable for attributes which always read >= 0, < 0 is error */
488 static int read_sysfs_attr(struct libusb_context *ctx,
489         const char *sysfs_dir, const char *attr, int max_value, int *value_p)
490 {
491         char buf[20], *endptr;
492         long value;
493         ssize_t r;
494         int fd;
495
496         fd = open_sysfs_attr(ctx, sysfs_dir, attr);
497         if (fd < 0)
498                 return fd;
499
500         r = read(fd, buf, sizeof(buf));
501         if (r < 0) {
502                 r = errno;
503                 close(fd);
504                 if (r == ENODEV)
505                         return LIBUSB_ERROR_NO_DEVICE;
506                 usbi_err(ctx, "attribute %s read failed, errno=%zd", attr, r);
507                 return LIBUSB_ERROR_IO;
508         }
509         close(fd);
510
511         if (r == 0) {
512                 /* Certain attributes (e.g. bConfigurationValue) are not
513                  * populated if the device is not configured. */
514                 *value_p = -1;
515                 return 0;
516         }
517
518         /* The kernel does *not* NULL-terminate the string, but every attribute
519          * should be terminated with a newline character. */
520         if (!isdigit(buf[0])) {
521                 usbi_err(ctx, "attribute %s doesn't have numeric value?", attr);
522                 return LIBUSB_ERROR_IO;
523         } else if (buf[r - 1] != '\n') {
524                 usbi_err(ctx, "attribute %s doesn't end with newline?", attr);
525                 return LIBUSB_ERROR_IO;
526         }
527         buf[r - 1] = '\0';
528
529         errno = 0;
530         value = strtol(buf, &endptr, 10);
531         if (value < 0 || value > (long)max_value || errno) {
532                 usbi_err(ctx, "attribute %s contains an invalid value: '%s'", attr, buf);
533                 return LIBUSB_ERROR_INVALID_PARAM;
534         } else if (*endptr != '\0') {
535                 /* Consider the value to be valid if the remainder is a '.'
536                  * character followed by numbers.  This occurs, for example,
537                  * when reading the "speed" attribute for a low-speed device
538                  * (e.g. "1.5") */
539                 if (*endptr == '.' && isdigit(*(endptr + 1))) {
540                         endptr++;
541                         while (isdigit(*endptr))
542                                 endptr++;
543                 }
544                 if (*endptr != '\0') {
545                         usbi_err(ctx, "attribute %s contains an invalid value: '%s'", attr, buf);
546                         return LIBUSB_ERROR_INVALID_PARAM;
547                 }
548         }
549
550         *value_p = (int)value;
551         return 0;
552 }
553
554 static int sysfs_scan_device(struct libusb_context *ctx, const char *devname)
555 {
556         uint8_t busnum, devaddr;
557         int ret;
558
559         ret = linux_get_device_address(ctx, 0, &busnum, &devaddr, NULL, devname, -1);
560         if (ret != LIBUSB_SUCCESS)
561                 return ret;
562
563         return linux_enumerate_device(ctx, busnum, devaddr, devname);
564 }
565
566 /* read the bConfigurationValue for a device */
567 static int sysfs_get_active_config(struct libusb_device *dev, uint8_t *config)
568 {
569         struct linux_device_priv *priv = usbi_get_device_priv(dev);
570         int ret, tmp;
571
572         ret = read_sysfs_attr(DEVICE_CTX(dev), priv->sysfs_dir, "bConfigurationValue",
573                               UINT8_MAX, &tmp);
574         if (ret < 0)
575                 return ret;
576
577         if (tmp == -1)
578                 tmp = 0;        /* unconfigured */
579
580         *config = (uint8_t)tmp;
581
582         return 0;
583 }
584
585 int linux_get_device_address(struct libusb_context *ctx, int detached,
586         uint8_t *busnum, uint8_t *devaddr, const char *dev_node,
587         const char *sys_name, int fd)
588 {
589         int sysfs_val;
590         int r;
591
592         usbi_dbg("getting address for device: %s detached: %d", sys_name, detached);
593         /* can't use sysfs to read the bus and device number if the
594          * device has been detached */
595         if (!sysfs_available || detached || !sys_name) {
596                 if (!dev_node && fd >= 0) {
597                         char *fd_path = alloca(PATH_MAX);
598                         char proc_path[32];
599
600                         /* try to retrieve the device node from fd */
601                         sprintf(proc_path, "/proc/self/fd/%d", fd);
602                         r = readlink(proc_path, fd_path, PATH_MAX - 1);
603                         if (r > 0) {
604                                 fd_path[r] = '\0';
605                                 dev_node = fd_path;
606                         }
607                 }
608
609                 if (!dev_node)
610                         return LIBUSB_ERROR_OTHER;
611
612                 /* will this work with all supported kernel versions? */
613                 if (!strncmp(dev_node, "/dev/bus/usb", 12))
614                         sscanf(dev_node, "/dev/bus/usb/%hhu/%hhu", busnum, devaddr);
615                 else
616                         return LIBUSB_ERROR_OTHER;
617
618                 return LIBUSB_SUCCESS;
619         }
620
621         usbi_dbg("scan %s", sys_name);
622
623         r = read_sysfs_attr(ctx, sys_name, "busnum", UINT8_MAX, &sysfs_val);
624         if (r < 0)
625                 return r;
626         *busnum = (uint8_t)sysfs_val;
627
628         r = read_sysfs_attr(ctx, sys_name, "devnum", UINT8_MAX, &sysfs_val);
629         if (r < 0)
630                 return r;
631         *devaddr = (uint8_t)sysfs_val;
632
633         usbi_dbg("bus=%u dev=%u", *busnum, *devaddr);
634
635         return LIBUSB_SUCCESS;
636 }
637
638 /* Return offset of the next config descriptor */
639 static int seek_to_next_config(struct libusb_context *ctx,
640         uint8_t *buffer, size_t len)
641 {
642         struct usbi_descriptor_header *header;
643         int offset = 0;
644
645         while (len > 0) {
646                 if (len < 2) {
647                         usbi_err(ctx, "short descriptor read %zu/2", len);
648                         return LIBUSB_ERROR_IO;
649                 }
650
651                 header = (struct usbi_descriptor_header *)buffer;
652                 if (header->bDescriptorType == LIBUSB_DT_CONFIG)
653                         return offset;
654
655                 if (len < header->bLength) {
656                         usbi_err(ctx, "bLength overflow by %zu bytes",
657                                  (size_t)header->bLength - len);
658                         return LIBUSB_ERROR_IO;
659                 }
660
661                 offset += header->bLength;
662                 buffer += header->bLength;
663                 len -= header->bLength;
664         }
665
666         usbi_err(ctx, "config descriptor not found");
667         return LIBUSB_ERROR_IO;
668 }
669
670 static int parse_config_descriptors(struct libusb_device *dev)
671 {
672         struct libusb_context *ctx = DEVICE_CTX(dev);
673         struct linux_device_priv *priv = usbi_get_device_priv(dev);
674         struct usbi_device_descriptor *device_desc;
675         uint8_t idx, num_configs;
676         uint8_t *buffer;
677         size_t remaining;
678
679         device_desc = (struct usbi_device_descriptor *)priv->descriptors;
680         num_configs = device_desc->bNumConfigurations;
681
682         if (num_configs == 0)
683                 return 0;       /* no configurations? */
684
685         priv->config_descriptors = malloc(num_configs * sizeof(priv->config_descriptors[0]));
686         if (!priv->config_descriptors)
687                 return LIBUSB_ERROR_NO_MEM;
688
689         buffer = priv->descriptors + LIBUSB_DT_DEVICE_SIZE;
690         remaining = priv->descriptors_len - LIBUSB_DT_DEVICE_SIZE;
691
692         for (idx = 0; idx < num_configs; idx++) {
693                 struct usbi_configuration_descriptor *config_desc;
694                 uint16_t config_len;
695
696                 if (remaining < LIBUSB_DT_CONFIG_SIZE) {
697                         usbi_err(ctx, "short descriptor read %zu/%d",
698                                  remaining, LIBUSB_DT_CONFIG_SIZE);
699                         return LIBUSB_ERROR_IO;
700                 }
701
702                 config_desc = (struct usbi_configuration_descriptor *)buffer;
703                 if (config_desc->bDescriptorType != LIBUSB_DT_CONFIG) {
704                         usbi_err(ctx, "descriptor is not a config desc (type 0x%02x)",
705                                  config_desc->bDescriptorType);
706                         return LIBUSB_ERROR_IO;
707                 } else if (config_desc->bLength < LIBUSB_DT_CONFIG_SIZE) {
708                         usbi_err(ctx, "invalid descriptor bLength %u",
709                                  config_desc->bLength);
710                         return LIBUSB_ERROR_IO;
711                 }
712
713                 config_len = libusb_le16_to_cpu(config_desc->wTotalLength);
714                 if (config_len < LIBUSB_DT_CONFIG_SIZE) {
715                         usbi_err(ctx, "invalid wTotalLength %u", config_len);
716                         return LIBUSB_ERROR_IO;
717                 }
718
719                 if (priv->sysfs_dir) {
720                          /*
721                          * In sysfs wTotalLength is ignored, instead the kernel returns a
722                          * config descriptor with verified bLength fields, with descriptors
723                          * with an invalid bLength removed.
724                          */
725                         uint16_t sysfs_config_len;
726                         int offset;
727
728                         if (num_configs > 1 && idx < num_configs - 1) {
729                                 offset = seek_to_next_config(ctx, buffer + LIBUSB_DT_CONFIG_SIZE,
730                                                              remaining - LIBUSB_DT_CONFIG_SIZE);
731                                 if (offset < 0)
732                                         return offset;
733                                 sysfs_config_len = (uint16_t)offset;
734                         } else {
735                                 sysfs_config_len = (uint16_t)remaining;
736                         }
737
738                         if (config_len != sysfs_config_len) {
739                                 usbi_warn(ctx, "config length mismatch wTotalLength %u real %u",
740                                           config_len, sysfs_config_len);
741                                 config_len = sysfs_config_len;
742                         }
743                 } else {
744                         /*
745                          * In usbfs the config descriptors are wTotalLength bytes apart,
746                          * with any short reads from the device appearing as holes in the file.
747                          */
748                         if (config_len > remaining) {
749                                 usbi_warn(ctx, "short descriptor read %zu/%u", remaining, config_len);
750                                 config_len = (uint16_t)remaining;
751                         }
752                 }
753
754                 priv->config_descriptors[idx].desc = config_desc;
755                 priv->config_descriptors[idx].actual_len = config_len;
756
757                 buffer += config_len;
758                 remaining -= config_len;
759         }
760
761         return LIBUSB_SUCCESS;
762 }
763
764 static int op_get_config_descriptor_by_value(struct libusb_device *dev,
765         uint8_t value, void **buffer)
766 {
767         struct linux_device_priv *priv = usbi_get_device_priv(dev);
768         struct config_descriptor *config;
769         uint8_t idx;
770
771         for (idx = 0; idx < dev->device_descriptor.bNumConfigurations; idx++) {
772                 config = &priv->config_descriptors[idx];
773                 if (config->desc->bConfigurationValue == value) {
774                         *buffer = config->desc;
775                         return (int)config->actual_len;
776                 }
777         }
778
779         return LIBUSB_ERROR_NOT_FOUND;
780 }
781
782 static int op_get_active_config_descriptor(struct libusb_device *dev,
783         void *buffer, size_t len)
784 {
785         struct linux_device_priv *priv = usbi_get_device_priv(dev);
786         void *config_desc;
787         uint8_t active_config;
788         int r;
789
790         if (priv->sysfs_dir) {
791                 r = sysfs_get_active_config(dev, &active_config);
792                 if (r < 0)
793                         return r;
794         } else {
795                 /* Use cached bConfigurationValue */
796                 active_config = priv->active_config;
797         }
798
799         if (active_config == 0) {
800                 usbi_err(DEVICE_CTX(dev), "device unconfigured");
801                 return LIBUSB_ERROR_NOT_FOUND;
802         }
803
804         r = op_get_config_descriptor_by_value(dev, active_config, &config_desc);
805         if (r < 0)
806                 return r;
807
808         len = MIN(len, (size_t)r);
809         memcpy(buffer, config_desc, len);
810         return len;
811 }
812
813 static int op_get_config_descriptor(struct libusb_device *dev,
814         uint8_t config_index, void *buffer, size_t len)
815 {
816         struct linux_device_priv *priv = usbi_get_device_priv(dev);
817         struct config_descriptor *config;
818
819         if (config_index >= dev->device_descriptor.bNumConfigurations)
820                 return LIBUSB_ERROR_NOT_FOUND;
821
822         config = &priv->config_descriptors[config_index];
823         len = MIN(len, config->actual_len);
824         memcpy(buffer, config->desc, len);
825         return len;
826 }
827
828 /* send a control message to retrieve active configuration */
829 static int usbfs_get_active_config(struct libusb_device *dev, int fd)
830 {
831         struct linux_device_priv *priv = usbi_get_device_priv(dev);
832         uint8_t active_config = 0;
833         int r;
834
835         struct usbfs_ctrltransfer ctrl = {
836                 .bmRequestType = LIBUSB_ENDPOINT_IN,
837                 .bRequest = LIBUSB_REQUEST_GET_CONFIGURATION,
838                 .wValue = 0,
839                 .wIndex = 0,
840                 .wLength = 1,
841                 .timeout = 1000,
842                 .data = &active_config
843         };
844
845         r = ioctl(fd, IOCTL_USBFS_CONTROL, &ctrl);
846         if (r < 0) {
847                 if (errno == ENODEV)
848                         return LIBUSB_ERROR_NO_DEVICE;
849
850                 /* we hit this error path frequently with buggy devices :( */
851                 usbi_warn(DEVICE_CTX(dev), "get configuration failed, errno=%d", errno);
852         } else if (active_config == 0) {
853                 /* some buggy devices have a configuration 0, but we're
854                  * reaching into the corner of a corner case here, so let's
855                  * not support buggy devices in these circumstances.
856                  * stick to the specs: a configuration value of 0 means
857                  * unconfigured. */
858                 usbi_warn(DEVICE_CTX(dev), "active cfg 0? assuming unconfigured device");
859         }
860
861         priv->active_config = active_config;
862
863         return LIBUSB_SUCCESS;
864 }
865
866 static enum libusb_speed usbfs_get_speed(struct libusb_context *ctx, int fd)
867 {
868         int r;
869
870         r = ioctl(fd, IOCTL_USBFS_GET_SPEED, NULL);
871         switch (r) {
872         case USBFS_SPEED_UNKNOWN:       return LIBUSB_SPEED_UNKNOWN;
873         case USBFS_SPEED_LOW:           return LIBUSB_SPEED_LOW;
874         case USBFS_SPEED_FULL:          return LIBUSB_SPEED_FULL;
875         case USBFS_SPEED_HIGH:          return LIBUSB_SPEED_HIGH;
876         case USBFS_SPEED_WIRELESS:      return LIBUSB_SPEED_HIGH;
877         case USBFS_SPEED_SUPER:         return LIBUSB_SPEED_SUPER;
878         case USBFS_SPEED_SUPER_PLUS:    return LIBUSB_SPEED_SUPER_PLUS;
879         default:
880                 usbi_warn(ctx, "Error getting device speed: %d", r);
881         }
882
883         return LIBUSB_SPEED_UNKNOWN;
884 }
885
886 static int initialize_device(struct libusb_device *dev, uint8_t busnum,
887         uint8_t devaddr, const char *sysfs_dir, int wrapped_fd)
888 {
889         struct linux_device_priv *priv = usbi_get_device_priv(dev);
890         struct libusb_context *ctx = DEVICE_CTX(dev);
891         size_t alloc_len;
892         int fd, speed, r;
893         ssize_t nb;
894
895         dev->bus_number = busnum;
896         dev->device_address = devaddr;
897
898         if (sysfs_dir) {
899                 priv->sysfs_dir = strdup(sysfs_dir);
900                 if (!priv->sysfs_dir)
901                         return LIBUSB_ERROR_NO_MEM;
902
903                 /* Note speed can contain 1.5, in this case read_sysfs_attr()
904                    will stop parsing at the '.' and return 1 */
905                 if (read_sysfs_attr(ctx, sysfs_dir, "speed", INT_MAX, &speed) == 0) {
906                         switch (speed) {
907                         case     1: dev->speed = LIBUSB_SPEED_LOW; break;
908                         case    12: dev->speed = LIBUSB_SPEED_FULL; break;
909                         case   480: dev->speed = LIBUSB_SPEED_HIGH; break;
910                         case  5000: dev->speed = LIBUSB_SPEED_SUPER; break;
911                         case 10000: dev->speed = LIBUSB_SPEED_SUPER_PLUS; break;
912                         default:
913                                 usbi_warn(ctx, "unknown device speed: %d Mbps", speed);
914                         }
915                 }
916         } else if (wrapped_fd >= 0) {
917                 dev->speed = usbfs_get_speed(ctx, wrapped_fd);
918         }
919
920         /* cache descriptors in memory */
921         if (sysfs_dir) {
922                 fd = open_sysfs_attr(ctx, sysfs_dir, "descriptors");
923         } else if (wrapped_fd < 0) {
924                 fd = get_usbfs_fd(dev, O_RDONLY, 0);
925         } else {
926                 fd = wrapped_fd;
927                 r = lseek(fd, 0, SEEK_SET);
928                 if (r < 0) {
929                         usbi_err(ctx, "lseek failed, errno=%d", errno);
930                         return LIBUSB_ERROR_IO;
931                 }
932         }
933         if (fd < 0)
934                 return fd;
935
936         alloc_len = 0;
937         do {
938                 alloc_len += 256;
939                 priv->descriptors = usbi_reallocf(priv->descriptors, alloc_len);
940                 if (!priv->descriptors) {
941                         if (fd != wrapped_fd)
942                                 close(fd);
943                         return LIBUSB_ERROR_NO_MEM;
944                 }
945                 /* usbfs has holes in the file */
946                 if (!sysfs_dir)
947                         memset(priv->descriptors + priv->descriptors_len,
948                                0, alloc_len - priv->descriptors_len);
949                 nb = read(fd, priv->descriptors + priv->descriptors_len,
950                           alloc_len - priv->descriptors_len);
951                 if (nb < 0) {
952                         usbi_err(ctx, "read descriptor failed, errno=%d", errno);
953                         if (fd != wrapped_fd)
954                                 close(fd);
955                         return LIBUSB_ERROR_IO;
956                 }
957                 priv->descriptors_len += (size_t)nb;
958         } while (priv->descriptors_len == alloc_len);
959
960         if (fd != wrapped_fd)
961                 close(fd);
962
963         if (priv->descriptors_len < LIBUSB_DT_DEVICE_SIZE) {
964                 usbi_err(ctx, "short descriptor read (%zu)", priv->descriptors_len);
965                 return LIBUSB_ERROR_IO;
966         }
967
968         r = parse_config_descriptors(dev);
969         if (r < 0)
970                 return r;
971
972         memcpy(&dev->device_descriptor, priv->descriptors, LIBUSB_DT_DEVICE_SIZE);
973
974         if (sysfs_dir) {
975                 /* sysfs descriptors are in bus-endian format */
976                 usbi_localize_device_descriptor(&dev->device_descriptor);
977                 return LIBUSB_SUCCESS;
978         }
979
980         /* cache active config */
981         if (wrapped_fd < 0)
982                 fd = get_usbfs_fd(dev, O_RDWR, 1);
983         else
984                 fd = wrapped_fd;
985         if (fd < 0) {
986                 /* cannot send a control message to determine the active
987                  * config. just assume the first one is active. */
988                 usbi_warn(ctx, "Missing rw usbfs access; cannot determine "
989                                "active configuration descriptor");
990                 if (priv->config_descriptors)
991                         priv->active_config = priv->config_descriptors[0].desc->bConfigurationValue;
992                 else
993                         priv->active_config = 0; /* No config dt */
994
995                 return LIBUSB_SUCCESS;
996         }
997
998         r = usbfs_get_active_config(dev, fd);
999         if (fd != wrapped_fd)
1000                 close(fd);
1001
1002         return r;
1003 }
1004
1005 static int linux_get_parent_info(struct libusb_device *dev, const char *sysfs_dir)
1006 {
1007         struct libusb_context *ctx = DEVICE_CTX(dev);
1008         struct libusb_device *it;
1009         char *parent_sysfs_dir, *tmp;
1010         int ret, add_parent = 1;
1011
1012         /* XXX -- can we figure out the topology when using usbfs? */
1013         if (!sysfs_dir || !strncmp(sysfs_dir, "usb", 3)) {
1014                 /* either using usbfs or finding the parent of a root hub */
1015                 return LIBUSB_SUCCESS;
1016         }
1017
1018         parent_sysfs_dir = strdup(sysfs_dir);
1019         if (!parent_sysfs_dir)
1020                 return LIBUSB_ERROR_NO_MEM;
1021
1022         if ((tmp = strrchr(parent_sysfs_dir, '.')) ||
1023             (tmp = strrchr(parent_sysfs_dir, '-'))) {
1024                 dev->port_number = atoi(tmp + 1);
1025                 *tmp = '\0';
1026         } else {
1027                 usbi_warn(ctx, "Can not parse sysfs_dir: %s, no parent info",
1028                           parent_sysfs_dir);
1029                 free(parent_sysfs_dir);
1030                 return LIBUSB_SUCCESS;
1031         }
1032
1033         /* is the parent a root hub? */
1034         if (!strchr(parent_sysfs_dir, '-')) {
1035                 tmp = parent_sysfs_dir;
1036                 ret = asprintf(&parent_sysfs_dir, "usb%s", tmp);
1037                 free(tmp);
1038                 if (ret < 0)
1039                         return LIBUSB_ERROR_NO_MEM;
1040         }
1041
1042 retry:
1043         /* find the parent in the context */
1044         usbi_mutex_lock(&ctx->usb_devs_lock);
1045         for_each_device(ctx, it) {
1046                 struct linux_device_priv *priv = usbi_get_device_priv(it);
1047
1048                 if (priv->sysfs_dir) {
1049                         if (!strcmp(priv->sysfs_dir, parent_sysfs_dir)) {
1050                                 dev->parent_dev = libusb_ref_device(it);
1051                                 break;
1052                         }
1053                 }
1054         }
1055         usbi_mutex_unlock(&ctx->usb_devs_lock);
1056
1057         if (!dev->parent_dev && add_parent) {
1058                 usbi_dbg("parent_dev %s not enumerated yet, enumerating now",
1059                          parent_sysfs_dir);
1060                 sysfs_scan_device(ctx, parent_sysfs_dir);
1061                 add_parent = 0;
1062                 goto retry;
1063         }
1064
1065         usbi_dbg("dev %p (%s) has parent %p (%s) port %u", dev, sysfs_dir,
1066                  dev->parent_dev, parent_sysfs_dir, dev->port_number);
1067
1068         free(parent_sysfs_dir);
1069
1070         return LIBUSB_SUCCESS;
1071 }
1072
1073 int linux_enumerate_device(struct libusb_context *ctx,
1074         uint8_t busnum, uint8_t devaddr, const char *sysfs_dir)
1075 {
1076         unsigned long session_id;
1077         struct libusb_device *dev;
1078         int r;
1079
1080         /* FIXME: session ID is not guaranteed unique as addresses can wrap and
1081          * will be reused. instead we should add a simple sysfs attribute with
1082          * a session ID. */
1083         session_id = busnum << 8 | devaddr;
1084         usbi_dbg("busnum %u devaddr %u session_id %lu", busnum, devaddr, session_id);
1085
1086         dev = usbi_get_device_by_session_id(ctx, session_id);
1087         if (dev) {
1088                 /* device already exists in the context */
1089                 usbi_dbg("session_id %lu already exists", session_id);
1090                 libusb_unref_device(dev);
1091                 return LIBUSB_SUCCESS;
1092         }
1093
1094         usbi_dbg("allocating new device for %u/%u (session %lu)",
1095                  busnum, devaddr, session_id);
1096         dev = usbi_alloc_device(ctx, session_id);
1097         if (!dev)
1098                 return LIBUSB_ERROR_NO_MEM;
1099
1100         r = initialize_device(dev, busnum, devaddr, sysfs_dir, -1);
1101         if (r < 0)
1102                 goto out;
1103         r = usbi_sanitize_device(dev);
1104         if (r < 0)
1105                 goto out;
1106
1107         r = linux_get_parent_info(dev, sysfs_dir);
1108         if (r < 0)
1109                 goto out;
1110 out:
1111         if (r < 0)
1112                 libusb_unref_device(dev);
1113         else
1114                 usbi_connect_device(dev);
1115
1116         return r;
1117 }
1118
1119 void linux_hotplug_enumerate(uint8_t busnum, uint8_t devaddr, const char *sys_name)
1120 {
1121         struct libusb_context *ctx;
1122
1123         usbi_mutex_static_lock(&active_contexts_lock);
1124         for_each_context(ctx) {
1125                 linux_enumerate_device(ctx, busnum, devaddr, sys_name);
1126         }
1127         usbi_mutex_static_unlock(&active_contexts_lock);
1128 }
1129
1130 void linux_device_disconnected(uint8_t busnum, uint8_t devaddr)
1131 {
1132         struct libusb_context *ctx;
1133         struct libusb_device *dev;
1134         unsigned long session_id = busnum << 8 | devaddr;
1135
1136         usbi_mutex_static_lock(&active_contexts_lock);
1137         for_each_context(ctx) {
1138                 dev = usbi_get_device_by_session_id(ctx, session_id);
1139                 if (dev) {
1140                         usbi_disconnect_device(dev);
1141                         libusb_unref_device(dev);
1142                 } else {
1143                         usbi_dbg("device not found for session %lx", session_id);
1144                 }
1145         }
1146         usbi_mutex_static_unlock(&active_contexts_lock);
1147 }
1148
1149 #if !defined(HAVE_LIBUDEV)
1150 static int parse_u8(const char *str, uint8_t *val_p)
1151 {
1152         char *endptr;
1153         long num;
1154
1155         errno = 0;
1156         num = strtol(str, &endptr, 10);
1157         if (num < 0 || num > UINT8_MAX || errno)
1158                 return 0;
1159         if (endptr == str || *endptr != '\0')
1160                 return 0;
1161
1162         *val_p = (uint8_t)num;
1163         return 1;
1164 }
1165
1166 /* open a bus directory and adds all discovered devices to the context */
1167 static int usbfs_scan_busdir(struct libusb_context *ctx, uint8_t busnum)
1168 {
1169         DIR *dir;
1170         char dirpath[20];
1171         struct dirent *entry;
1172         int r = LIBUSB_ERROR_IO;
1173
1174         sprintf(dirpath, USB_DEVTMPFS_PATH "/%03u", busnum);
1175         usbi_dbg("%s", dirpath);
1176         dir = opendir(dirpath);
1177         if (!dir) {
1178                 usbi_err(ctx, "opendir '%s' failed, errno=%d", dirpath, errno);
1179                 /* FIXME: should handle valid race conditions like hub unplugged
1180                  * during directory iteration - this is not an error */
1181                 return r;
1182         }
1183
1184         while ((entry = readdir(dir))) {
1185                 uint8_t devaddr;
1186
1187                 if (entry->d_name[0] == '.')
1188                         continue;
1189
1190                 if (!parse_u8(entry->d_name, &devaddr)) {
1191                         usbi_dbg("unknown dir entry %s", entry->d_name);
1192                         continue;
1193                 }
1194
1195                 if (linux_enumerate_device(ctx, busnum, devaddr, NULL)) {
1196                         usbi_dbg("failed to enumerate dir entry %s", entry->d_name);
1197                         continue;
1198                 }
1199
1200                 r = 0;
1201         }
1202
1203         closedir(dir);
1204         return r;
1205 }
1206
1207 static int usbfs_get_device_list(struct libusb_context *ctx)
1208 {
1209         struct dirent *entry;
1210         DIR *buses;
1211         uint8_t busnum, devaddr;
1212         int r = 0;
1213
1214         if (usbdev_names)
1215                 buses = opendir(USBDEV_PATH);
1216         else
1217                 buses = opendir(USB_DEVTMPFS_PATH);
1218
1219         if (!buses) {
1220                 usbi_err(ctx, "opendir buses failed, errno=%d", errno);
1221                 return LIBUSB_ERROR_IO;
1222         }
1223
1224         while ((entry = readdir(buses))) {
1225                 if (entry->d_name[0] == '.')
1226                         continue;
1227
1228                 if (usbdev_names) {
1229                         if (!is_usbdev_entry(entry->d_name, &busnum, &devaddr))
1230                                 continue;
1231
1232                         r = linux_enumerate_device(ctx, busnum, devaddr, NULL);
1233                         if (r < 0) {
1234                                 usbi_dbg("failed to enumerate dir entry %s", entry->d_name);
1235                                 continue;
1236                         }
1237                 } else {
1238                         if (!parse_u8(entry->d_name, &busnum)) {
1239                                 usbi_dbg("unknown dir entry %s", entry->d_name);
1240                                 continue;
1241                         }
1242
1243                         r = usbfs_scan_busdir(ctx, busnum);
1244                         if (r < 0)
1245                                 break;
1246                 }
1247         }
1248
1249         closedir(buses);
1250         return r;
1251
1252 }
1253
1254 static int sysfs_get_device_list(struct libusb_context *ctx)
1255 {
1256         DIR *devices = opendir(SYSFS_DEVICE_PATH);
1257         struct dirent *entry;
1258         int num_devices = 0;
1259         int num_enumerated = 0;
1260
1261         if (!devices) {
1262                 usbi_err(ctx, "opendir devices failed, errno=%d", errno);
1263                 return LIBUSB_ERROR_IO;
1264         }
1265
1266         while ((entry = readdir(devices))) {
1267                 if ((!isdigit(entry->d_name[0]) && strncmp(entry->d_name, "usb", 3))
1268                     || strchr(entry->d_name, ':'))
1269                         continue;
1270
1271                 num_devices++;
1272
1273                 if (sysfs_scan_device(ctx, entry->d_name)) {
1274                         usbi_dbg("failed to enumerate dir entry %s", entry->d_name);
1275                         continue;
1276                 }
1277
1278                 num_enumerated++;
1279         }
1280
1281         closedir(devices);
1282
1283         /* successful if at least one device was enumerated or no devices were found */
1284         if (num_enumerated || !num_devices)
1285                 return LIBUSB_SUCCESS;
1286         else
1287                 return LIBUSB_ERROR_IO;
1288 }
1289
1290 static int linux_default_scan_devices(struct libusb_context *ctx)
1291 {
1292         /* we can retrieve device list and descriptors from sysfs or usbfs.
1293          * sysfs is preferable, because if we use usbfs we end up resuming
1294          * any autosuspended USB devices. however, sysfs is not available
1295          * everywhere, so we need a usbfs fallback too.
1296          */
1297         if (sysfs_available)
1298                 return sysfs_get_device_list(ctx);
1299         else
1300                 return usbfs_get_device_list(ctx);
1301 }
1302 #endif
1303
1304 static int initialize_handle(struct libusb_device_handle *handle, int fd)
1305 {
1306         struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1307         int r;
1308
1309         hpriv->fd = fd;
1310
1311         r = ioctl(fd, IOCTL_USBFS_GET_CAPABILITIES, &hpriv->caps);
1312         if (r < 0) {
1313                 if (errno == ENOTTY)
1314                         usbi_dbg("getcap not available");
1315                 else
1316                         usbi_err(HANDLE_CTX(handle), "getcap failed, errno=%d", errno);
1317                 hpriv->caps = USBFS_CAP_BULK_CONTINUATION;
1318         }
1319
1320         return usbi_add_event_source(HANDLE_CTX(handle), hpriv->fd, POLLOUT);
1321 }
1322
1323 static int op_wrap_sys_device(struct libusb_context *ctx,
1324         struct libusb_device_handle *handle, intptr_t sys_dev)
1325 {
1326         struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1327         int fd = (int)sys_dev;
1328         uint8_t busnum, devaddr;
1329         struct usbfs_connectinfo ci;
1330         struct libusb_device *dev;
1331         int r;
1332
1333         r = linux_get_device_address(ctx, 1, &busnum, &devaddr, NULL, NULL, fd);
1334         if (r < 0) {
1335                 r = ioctl(fd, IOCTL_USBFS_CONNECTINFO, &ci);
1336                 if (r < 0) {
1337                         usbi_err(ctx, "connectinfo failed, errno=%d", errno);
1338                         return LIBUSB_ERROR_IO;
1339                 }
1340                 /* There is no ioctl to get the bus number. We choose 0 here
1341                  * as linux starts numbering buses from 1. */
1342                 busnum = 0;
1343                 devaddr = ci.devnum;
1344         }
1345
1346         /* Session id is unused as we do not add the device to the list of
1347          * connected devices. */
1348         usbi_dbg("allocating new device for fd %d", fd);
1349         dev = usbi_alloc_device(ctx, 0);
1350         if (!dev)
1351                 return LIBUSB_ERROR_NO_MEM;
1352
1353         r = initialize_device(dev, busnum, devaddr, NULL, fd);
1354         if (r < 0)
1355                 goto out;
1356         r = usbi_sanitize_device(dev);
1357         if (r < 0)
1358                 goto out;
1359         /* Consider the device as connected, but do not add it to the managed
1360          * device list. */
1361         dev->attached = 1;
1362         handle->dev = dev;
1363
1364         r = initialize_handle(handle, fd);
1365         hpriv->fd_keep = 1;
1366
1367 out:
1368         if (r < 0)
1369                 libusb_unref_device(dev);
1370         return r;
1371 }
1372
1373 static int op_open(struct libusb_device_handle *handle)
1374 {
1375         int fd, r;
1376
1377         fd = get_usbfs_fd(handle->dev, O_RDWR, 0);
1378         if (fd < 0) {
1379                 if (fd == LIBUSB_ERROR_NO_DEVICE) {
1380                         /* device will still be marked as attached if hotplug monitor thread
1381                          * hasn't processed remove event yet */
1382                         usbi_mutex_static_lock(&linux_hotplug_lock);
1383                         if (handle->dev->attached) {
1384                                 usbi_dbg("open failed with no device, but device still attached");
1385                                 linux_device_disconnected(handle->dev->bus_number,
1386                                                           handle->dev->device_address);
1387                         }
1388                         usbi_mutex_static_unlock(&linux_hotplug_lock);
1389                 }
1390                 return fd;
1391         }
1392
1393         r = initialize_handle(handle, fd);
1394         if (r < 0)
1395                 close(fd);
1396
1397         return r;
1398 }
1399
1400 static void op_close(struct libusb_device_handle *dev_handle)
1401 {
1402         struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(dev_handle);
1403
1404         /* fd may have already been removed by POLLERR condition in op_handle_events() */
1405         if (!hpriv->fd_removed)
1406                 usbi_remove_event_source(HANDLE_CTX(dev_handle), hpriv->fd);
1407         if (!hpriv->fd_keep)
1408                 close(hpriv->fd);
1409 }
1410
1411 static int op_get_configuration(struct libusb_device_handle *handle,
1412         uint8_t *config)
1413 {
1414         struct linux_device_priv *priv = usbi_get_device_priv(handle->dev);
1415         int r;
1416
1417         if (priv->sysfs_dir) {
1418                 r = sysfs_get_active_config(handle->dev, config);
1419         } else {
1420                 struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1421
1422                 r = usbfs_get_active_config(handle->dev, hpriv->fd);
1423                 if (r == LIBUSB_SUCCESS)
1424                         *config = priv->active_config;
1425         }
1426         if (r < 0)
1427                 return r;
1428
1429         if (*config == 0)
1430                 usbi_err(HANDLE_CTX(handle), "device unconfigured");
1431
1432         return 0;
1433 }
1434
1435 static int op_set_configuration(struct libusb_device_handle *handle, int config)
1436 {
1437         struct linux_device_priv *priv = usbi_get_device_priv(handle->dev);
1438         struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1439         int fd = hpriv->fd;
1440         int r = ioctl(fd, IOCTL_USBFS_SETCONFIGURATION, &config);
1441
1442         if (r < 0) {
1443                 if (errno == EINVAL)
1444                         return LIBUSB_ERROR_NOT_FOUND;
1445                 else if (errno == EBUSY)
1446                         return LIBUSB_ERROR_BUSY;
1447                 else if (errno == ENODEV)
1448                         return LIBUSB_ERROR_NO_DEVICE;
1449
1450                 usbi_err(HANDLE_CTX(handle), "set configuration failed, errno=%d", errno);
1451                 return LIBUSB_ERROR_OTHER;
1452         }
1453
1454         if (config == -1)
1455                 config = 0;
1456
1457         /* update our cached active config descriptor */
1458         priv->active_config = (uint8_t)config;
1459
1460         return LIBUSB_SUCCESS;
1461 }
1462
1463 static int claim_interface(struct libusb_device_handle *handle, unsigned int iface)
1464 {
1465         struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1466         int fd = hpriv->fd;
1467         int r = ioctl(fd, IOCTL_USBFS_CLAIMINTERFACE, &iface);
1468
1469         if (r < 0) {
1470                 if (errno == ENOENT)
1471                         return LIBUSB_ERROR_NOT_FOUND;
1472                 else if (errno == EBUSY)
1473                         return LIBUSB_ERROR_BUSY;
1474                 else if (errno == ENODEV)
1475                         return LIBUSB_ERROR_NO_DEVICE;
1476
1477                 usbi_err(HANDLE_CTX(handle), "claim interface failed, errno=%d", errno);
1478                 return LIBUSB_ERROR_OTHER;
1479         }
1480         return 0;
1481 }
1482
1483 static int release_interface(struct libusb_device_handle *handle, unsigned int iface)
1484 {
1485         struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1486         int fd = hpriv->fd;
1487         int r = ioctl(fd, IOCTL_USBFS_RELEASEINTERFACE, &iface);
1488
1489         if (r < 0) {
1490                 if (errno == ENODEV)
1491                         return LIBUSB_ERROR_NO_DEVICE;
1492
1493                 usbi_err(HANDLE_CTX(handle), "release interface failed, errno=%d", errno);
1494                 return LIBUSB_ERROR_OTHER;
1495         }
1496         return 0;
1497 }
1498
1499 static int op_set_interface(struct libusb_device_handle *handle, uint8_t interface,
1500         uint8_t altsetting)
1501 {
1502         struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1503         int fd = hpriv->fd;
1504         struct usbfs_setinterface setintf;
1505         int r;
1506
1507         setintf.interface = interface;
1508         setintf.altsetting = altsetting;
1509         r = ioctl(fd, IOCTL_USBFS_SETINTERFACE, &setintf);
1510         if (r < 0) {
1511                 if (errno == EINVAL)
1512                         return LIBUSB_ERROR_NOT_FOUND;
1513                 else if (errno == ENODEV)
1514                         return LIBUSB_ERROR_NO_DEVICE;
1515
1516                 usbi_err(HANDLE_CTX(handle), "set interface failed, errno=%d", errno);
1517                 return LIBUSB_ERROR_OTHER;
1518         }
1519
1520         return 0;
1521 }
1522
1523 static int op_clear_halt(struct libusb_device_handle *handle,
1524         unsigned char endpoint)
1525 {
1526         struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1527         int fd = hpriv->fd;
1528         unsigned int _endpoint = endpoint;
1529         int r = ioctl(fd, IOCTL_USBFS_CLEAR_HALT, &_endpoint);
1530
1531         if (r < 0) {
1532                 if (errno == ENOENT)
1533                         return LIBUSB_ERROR_NOT_FOUND;
1534                 else if (errno == ENODEV)
1535                         return LIBUSB_ERROR_NO_DEVICE;
1536
1537                 usbi_err(HANDLE_CTX(handle), "clear halt failed, errno=%d", errno);
1538                 return LIBUSB_ERROR_OTHER;
1539         }
1540
1541         return 0;
1542 }
1543
1544 static int op_reset_device(struct libusb_device_handle *handle)
1545 {
1546         struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1547         int fd = hpriv->fd;
1548         int r, ret = 0;
1549         uint8_t i;
1550
1551         /* Doing a device reset will cause the usbfs driver to get unbound
1552          * from any interfaces it is bound to. By voluntarily unbinding
1553          * the usbfs driver ourself, we stop the kernel from rebinding
1554          * the interface after reset (which would end up with the interface
1555          * getting bound to the in kernel driver if any). */
1556         for (i = 0; i < USB_MAXINTERFACES; i++) {
1557                 if (handle->claimed_interfaces & (1UL << i))
1558                         release_interface(handle, i);
1559         }
1560
1561         usbi_mutex_lock(&handle->lock);
1562         r = ioctl(fd, IOCTL_USBFS_RESET, NULL);
1563         if (r < 0) {
1564                 if (errno == ENODEV) {
1565                         ret = LIBUSB_ERROR_NOT_FOUND;
1566                         goto out;
1567                 }
1568
1569                 usbi_err(HANDLE_CTX(handle), "reset failed, errno=%d", errno);
1570                 ret = LIBUSB_ERROR_OTHER;
1571                 goto out;
1572         }
1573
1574         /* And re-claim any interfaces which were claimed before the reset */
1575         for (i = 0; i < USB_MAXINTERFACES; i++) {
1576                 if (!(handle->claimed_interfaces & (1UL << i)))
1577                         continue;
1578                 /*
1579                  * A driver may have completed modprobing during
1580                  * IOCTL_USBFS_RESET, and bound itself as soon as
1581                  * IOCTL_USBFS_RESET released the device lock
1582                  */
1583                 r = detach_kernel_driver_and_claim(handle, i);
1584                 if (r) {
1585                         usbi_warn(HANDLE_CTX(handle), "failed to re-claim interface %u after reset: %s",
1586                                   i, libusb_error_name(r));
1587                         handle->claimed_interfaces &= ~(1UL << i);
1588                         ret = LIBUSB_ERROR_NOT_FOUND;
1589                 }
1590         }
1591 out:
1592         usbi_mutex_unlock(&handle->lock);
1593         return ret;
1594 }
1595
1596 static int do_streams_ioctl(struct libusb_device_handle *handle, long req,
1597         uint32_t num_streams, unsigned char *endpoints, int num_endpoints)
1598 {
1599         struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1600         int r, fd = hpriv->fd;
1601         struct usbfs_streams *streams;
1602
1603         if (num_endpoints > 30) /* Max 15 in + 15 out eps */
1604                 return LIBUSB_ERROR_INVALID_PARAM;
1605
1606         streams = malloc(sizeof(*streams) + num_endpoints);
1607         if (!streams)
1608                 return LIBUSB_ERROR_NO_MEM;
1609
1610         streams->num_streams = num_streams;
1611         streams->num_eps = num_endpoints;
1612         memcpy(streams->eps, endpoints, num_endpoints);
1613
1614         r = ioctl(fd, req, streams);
1615
1616         free(streams);
1617
1618         if (r < 0) {
1619                 if (errno == ENOTTY)
1620                         return LIBUSB_ERROR_NOT_SUPPORTED;
1621                 else if (errno == EINVAL)
1622                         return LIBUSB_ERROR_INVALID_PARAM;
1623                 else if (errno == ENODEV)
1624                         return LIBUSB_ERROR_NO_DEVICE;
1625
1626                 usbi_err(HANDLE_CTX(handle), "streams-ioctl failed, errno=%d", errno);
1627                 return LIBUSB_ERROR_OTHER;
1628         }
1629         return r;
1630 }
1631
1632 static int op_alloc_streams(struct libusb_device_handle *handle,
1633         uint32_t num_streams, unsigned char *endpoints, int num_endpoints)
1634 {
1635         return do_streams_ioctl(handle, IOCTL_USBFS_ALLOC_STREAMS,
1636                                 num_streams, endpoints, num_endpoints);
1637 }
1638
1639 static int op_free_streams(struct libusb_device_handle *handle,
1640                 unsigned char *endpoints, int num_endpoints)
1641 {
1642         return do_streams_ioctl(handle, IOCTL_USBFS_FREE_STREAMS, 0,
1643                                 endpoints, num_endpoints);
1644 }
1645
1646 static void *op_dev_mem_alloc(struct libusb_device_handle *handle, size_t len)
1647 {
1648         struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1649         void *buffer;
1650
1651         buffer = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, hpriv->fd, 0);
1652         if (buffer == MAP_FAILED) {
1653                 usbi_err(HANDLE_CTX(handle), "alloc dev mem failed, errno=%d", errno);
1654                 return NULL;
1655         }
1656         return buffer;
1657 }
1658
1659 static int op_dev_mem_free(struct libusb_device_handle *handle, void *buffer,
1660         size_t len)
1661 {
1662         if (munmap(buffer, len) != 0) {
1663                 usbi_err(HANDLE_CTX(handle), "free dev mem failed, errno=%d", errno);
1664                 return LIBUSB_ERROR_OTHER;
1665         } else {
1666                 return LIBUSB_SUCCESS;
1667         }
1668 }
1669
1670 static int op_kernel_driver_active(struct libusb_device_handle *handle,
1671         uint8_t interface)
1672 {
1673         struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1674         int fd = hpriv->fd;
1675         struct usbfs_getdriver getdrv;
1676         int r;
1677
1678         getdrv.interface = interface;
1679         r = ioctl(fd, IOCTL_USBFS_GETDRIVER, &getdrv);
1680         if (r < 0) {
1681                 if (errno == ENODATA)
1682                         return 0;
1683                 else if (errno == ENODEV)
1684                         return LIBUSB_ERROR_NO_DEVICE;
1685
1686                 usbi_err(HANDLE_CTX(handle), "get driver failed, errno=%d", errno);
1687                 return LIBUSB_ERROR_OTHER;
1688         }
1689
1690         return strcmp(getdrv.driver, "usbfs") != 0;
1691 }
1692
1693 static int op_detach_kernel_driver(struct libusb_device_handle *handle,
1694         uint8_t interface)
1695 {
1696         struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1697         int fd = hpriv->fd;
1698         struct usbfs_ioctl command;
1699         struct usbfs_getdriver getdrv;
1700         int r;
1701
1702         command.ifno = interface;
1703         command.ioctl_code = IOCTL_USBFS_DISCONNECT;
1704         command.data = NULL;
1705
1706         getdrv.interface = interface;
1707         r = ioctl(fd, IOCTL_USBFS_GETDRIVER, &getdrv);
1708         if (r == 0 && !strcmp(getdrv.driver, "usbfs"))
1709                 return LIBUSB_ERROR_NOT_FOUND;
1710
1711         r = ioctl(fd, IOCTL_USBFS_IOCTL, &command);
1712         if (r < 0) {
1713                 if (errno == ENODATA)
1714                         return LIBUSB_ERROR_NOT_FOUND;
1715                 else if (errno == EINVAL)
1716                         return LIBUSB_ERROR_INVALID_PARAM;
1717                 else if (errno == ENODEV)
1718                         return LIBUSB_ERROR_NO_DEVICE;
1719
1720                 usbi_err(HANDLE_CTX(handle), "detach failed, errno=%d", errno);
1721                 return LIBUSB_ERROR_OTHER;
1722         }
1723
1724         return 0;
1725 }
1726
1727 static int op_attach_kernel_driver(struct libusb_device_handle *handle,
1728         uint8_t interface)
1729 {
1730         struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1731         int fd = hpriv->fd;
1732         struct usbfs_ioctl command;
1733         int r;
1734
1735         command.ifno = interface;
1736         command.ioctl_code = IOCTL_USBFS_CONNECT;
1737         command.data = NULL;
1738
1739         r = ioctl(fd, IOCTL_USBFS_IOCTL, &command);
1740         if (r < 0) {
1741                 if (errno == ENODATA)
1742                         return LIBUSB_ERROR_NOT_FOUND;
1743                 else if (errno == EINVAL)
1744                         return LIBUSB_ERROR_INVALID_PARAM;
1745                 else if (errno == ENODEV)
1746                         return LIBUSB_ERROR_NO_DEVICE;
1747                 else if (errno == EBUSY)
1748                         return LIBUSB_ERROR_BUSY;
1749
1750                 usbi_err(HANDLE_CTX(handle), "attach failed, errno=%d", errno);
1751                 return LIBUSB_ERROR_OTHER;
1752         } else if (r == 0) {
1753                 return LIBUSB_ERROR_NOT_FOUND;
1754         }
1755
1756         return 0;
1757 }
1758
1759 static int detach_kernel_driver_and_claim(struct libusb_device_handle *handle,
1760         uint8_t interface)
1761 {
1762         struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
1763         struct usbfs_disconnect_claim dc;
1764         int r, fd = hpriv->fd;
1765
1766         dc.interface = interface;
1767         strcpy(dc.driver, "usbfs");
1768         dc.flags = USBFS_DISCONNECT_CLAIM_EXCEPT_DRIVER;
1769         r = ioctl(fd, IOCTL_USBFS_DISCONNECT_CLAIM, &dc);
1770         if (r == 0)
1771                 return 0;
1772         switch (errno) {
1773         case ENOTTY:
1774                 break;
1775         case EBUSY:
1776                 return LIBUSB_ERROR_BUSY;
1777         case EINVAL:
1778                 return LIBUSB_ERROR_INVALID_PARAM;
1779         case ENODEV:
1780                 return LIBUSB_ERROR_NO_DEVICE;
1781         default:
1782                 usbi_err(HANDLE_CTX(handle), "disconnect-and-claim failed, errno=%d", errno);
1783                 return LIBUSB_ERROR_OTHER;
1784         }
1785
1786         /* Fallback code for kernels which don't support the
1787            disconnect-and-claim ioctl */
1788         r = op_detach_kernel_driver(handle, interface);
1789         if (r != 0 && r != LIBUSB_ERROR_NOT_FOUND)
1790                 return r;
1791
1792         return claim_interface(handle, interface);
1793 }
1794
1795 static int op_claim_interface(struct libusb_device_handle *handle, uint8_t interface)
1796 {
1797         if (handle->auto_detach_kernel_driver)
1798                 return detach_kernel_driver_and_claim(handle, interface);
1799         else
1800                 return claim_interface(handle, interface);
1801 }
1802
1803 static int op_release_interface(struct libusb_device_handle *handle, uint8_t interface)
1804 {
1805         int r;
1806
1807         r = release_interface(handle, interface);
1808         if (r)
1809                 return r;
1810
1811         if (handle->auto_detach_kernel_driver)
1812                 op_attach_kernel_driver(handle, interface);
1813
1814         return 0;
1815 }
1816
1817 static void op_destroy_device(struct libusb_device *dev)
1818 {
1819         struct linux_device_priv *priv = usbi_get_device_priv(dev);
1820
1821         free(priv->config_descriptors);
1822         free(priv->descriptors);
1823         free(priv->sysfs_dir);
1824 }
1825
1826 /* URBs are discarded in reverse order of submission to avoid races. */
1827 static int discard_urbs(struct usbi_transfer *itransfer, int first, int last_plus_one)
1828 {
1829         struct libusb_transfer *transfer =
1830                 USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
1831         struct linux_transfer_priv *tpriv = usbi_get_transfer_priv(itransfer);
1832         struct linux_device_handle_priv *hpriv =
1833                 usbi_get_device_handle_priv(transfer->dev_handle);
1834         int i, ret = 0;
1835         struct usbfs_urb *urb;
1836
1837         for (i = last_plus_one - 1; i >= first; i--) {
1838                 if (transfer->type == LIBUSB_TRANSFER_TYPE_ISOCHRONOUS)
1839                         urb = tpriv->iso_urbs[i];
1840                 else
1841                         urb = &tpriv->urbs[i];
1842
1843                 if (ioctl(hpriv->fd, IOCTL_USBFS_DISCARDURB, urb) == 0)
1844                         continue;
1845
1846                 if (errno == EINVAL) {
1847                         usbi_dbg("URB not found --> assuming ready to be reaped");
1848                         if (i == (last_plus_one - 1))
1849                                 ret = LIBUSB_ERROR_NOT_FOUND;
1850                 } else if (errno == ENODEV) {
1851                         usbi_dbg("Device not found for URB --> assuming ready to be reaped");
1852                         ret = LIBUSB_ERROR_NO_DEVICE;
1853                 } else {
1854                         usbi_warn(TRANSFER_CTX(transfer), "unrecognised discard errno %d", errno);
1855                         ret = LIBUSB_ERROR_OTHER;
1856                 }
1857         }
1858         return ret;
1859 }
1860
1861 static void free_iso_urbs(struct linux_transfer_priv *tpriv)
1862 {
1863         int i;
1864
1865         for (i = 0; i < tpriv->num_urbs; i++) {
1866                 struct usbfs_urb *urb = tpriv->iso_urbs[i];
1867
1868                 if (!urb)
1869                         break;
1870                 free(urb);
1871         }
1872
1873         free(tpriv->iso_urbs);
1874         tpriv->iso_urbs = NULL;
1875 }
1876
1877 static int submit_bulk_transfer(struct usbi_transfer *itransfer)
1878 {
1879         struct libusb_transfer *transfer =
1880                 USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
1881         struct linux_transfer_priv *tpriv = usbi_get_transfer_priv(itransfer);
1882         struct linux_device_handle_priv *hpriv =
1883                 usbi_get_device_handle_priv(transfer->dev_handle);
1884         struct usbfs_urb *urbs;
1885         int is_out = IS_XFEROUT(transfer);
1886         int bulk_buffer_len, use_bulk_continuation;
1887         int num_urbs;
1888         int last_urb_partial = 0;
1889         int r;
1890         int i;
1891
1892         /*
1893          * Older versions of usbfs place a 16kb limit on bulk URBs. We work
1894          * around this by splitting large transfers into 16k blocks, and then
1895          * submit all urbs at once. it would be simpler to submit one urb at
1896          * a time, but there is a big performance gain doing it this way.
1897          *
1898          * Newer versions lift the 16k limit (USBFS_CAP_NO_PACKET_SIZE_LIM),
1899          * using arbitrary large transfers can still be a bad idea though, as
1900          * the kernel needs to allocate physical contiguous memory for this,
1901          * which may fail for large buffers.
1902          *
1903          * The kernel solves this problem by splitting the transfer into
1904          * blocks itself when the host-controller is scatter-gather capable
1905          * (USBFS_CAP_BULK_SCATTER_GATHER), which most controllers are.
1906          *
1907          * Last, there is the issue of short-transfers when splitting, for
1908          * short split-transfers to work reliable USBFS_CAP_BULK_CONTINUATION
1909          * is needed, but this is not always available.
1910          */
1911         if (hpriv->caps & USBFS_CAP_BULK_SCATTER_GATHER) {
1912                 /* Good! Just submit everything in one go */
1913                 bulk_buffer_len = transfer->length ? transfer->length : 1;
1914                 use_bulk_continuation = 0;
1915         } else if (hpriv->caps & USBFS_CAP_BULK_CONTINUATION) {
1916                 /* Split the transfers and use bulk-continuation to
1917                    avoid issues with short-transfers */
1918                 bulk_buffer_len = MAX_BULK_BUFFER_LENGTH;
1919                 use_bulk_continuation = 1;
1920         } else if (hpriv->caps & USBFS_CAP_NO_PACKET_SIZE_LIM) {
1921                 /* Don't split, assume the kernel can alloc the buffer
1922                    (otherwise the submit will fail with -ENOMEM) */
1923                 bulk_buffer_len = transfer->length ? transfer->length : 1;
1924                 use_bulk_continuation = 0;
1925         } else {
1926                 /* Bad, splitting without bulk-continuation, short transfers
1927                    which end before the last urb will not work reliable! */
1928                 /* Note we don't warn here as this is "normal" on kernels <
1929                    2.6.32 and not a problem for most applications */
1930                 bulk_buffer_len = MAX_BULK_BUFFER_LENGTH;
1931                 use_bulk_continuation = 0;
1932         }
1933
1934         num_urbs = transfer->length / bulk_buffer_len;
1935
1936         if (transfer->length == 0) {
1937                 num_urbs = 1;
1938         } else if ((transfer->length % bulk_buffer_len) > 0) {
1939                 last_urb_partial = 1;
1940                 num_urbs++;
1941         }
1942         usbi_dbg("need %d urbs for new transfer with length %d", num_urbs, transfer->length);
1943         urbs = calloc(num_urbs, sizeof(*urbs));
1944         if (!urbs)
1945                 return LIBUSB_ERROR_NO_MEM;
1946         tpriv->urbs = urbs;
1947         tpriv->num_urbs = num_urbs;
1948         tpriv->num_retired = 0;
1949         tpriv->reap_action = NORMAL;
1950         tpriv->reap_status = LIBUSB_TRANSFER_COMPLETED;
1951
1952         for (i = 0; i < num_urbs; i++) {
1953                 struct usbfs_urb *urb = &urbs[i];
1954
1955                 urb->usercontext = itransfer;
1956                 switch (transfer->type) {
1957                 case LIBUSB_TRANSFER_TYPE_BULK:
1958                         urb->type = USBFS_URB_TYPE_BULK;
1959                         urb->stream_id = 0;
1960                         break;
1961                 case LIBUSB_TRANSFER_TYPE_BULK_STREAM:
1962                         urb->type = USBFS_URB_TYPE_BULK;
1963                         urb->stream_id = itransfer->stream_id;
1964                         break;
1965                 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1966                         urb->type = USBFS_URB_TYPE_INTERRUPT;
1967                         break;
1968                 }
1969                 urb->endpoint = transfer->endpoint;
1970                 urb->buffer = transfer->buffer + (i * bulk_buffer_len);
1971
1972                 /* don't set the short not ok flag for the last URB */
1973                 if (use_bulk_continuation && !is_out && (i < num_urbs - 1))
1974                         urb->flags = USBFS_URB_SHORT_NOT_OK;
1975
1976                 if (i == num_urbs - 1 && last_urb_partial)
1977                         urb->buffer_length = transfer->length % bulk_buffer_len;
1978                 else if (transfer->length == 0)
1979                         urb->buffer_length = 0;
1980                 else
1981                         urb->buffer_length = bulk_buffer_len;
1982
1983                 if (i > 0 && use_bulk_continuation)
1984                         urb->flags |= USBFS_URB_BULK_CONTINUATION;
1985
1986                 /* we have already checked that the flag is supported */
1987                 if (is_out && i == num_urbs - 1 &&
1988                     (transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET))
1989                         urb->flags |= USBFS_URB_ZERO_PACKET;
1990
1991                 r = ioctl(hpriv->fd, IOCTL_USBFS_SUBMITURB, urb);
1992                 if (r == 0)
1993                         continue;
1994
1995                 if (errno == ENODEV) {
1996                         r = LIBUSB_ERROR_NO_DEVICE;
1997                 } else if (errno == ENOMEM) {
1998                         r = LIBUSB_ERROR_NO_MEM;
1999                 } else {
2000                         usbi_err(TRANSFER_CTX(transfer), "submiturb failed, errno=%d", errno);
2001                         r = LIBUSB_ERROR_IO;
2002                 }
2003
2004                 /* if the first URB submission fails, we can simply free up and
2005                  * return failure immediately. */
2006                 if (i == 0) {
2007                         usbi_dbg("first URB failed, easy peasy");
2008                         free(urbs);
2009                         tpriv->urbs = NULL;
2010                         return r;
2011                 }
2012
2013                 /* if it's not the first URB that failed, the situation is a bit
2014                  * tricky. we may need to discard all previous URBs. there are
2015                  * complications:
2016                  *  - discarding is asynchronous - discarded urbs will be reaped
2017                  *    later. the user must not have freed the transfer when the
2018                  *    discarded URBs are reaped, otherwise libusb will be using
2019                  *    freed memory.
2020                  *  - the earlier URBs may have completed successfully and we do
2021                  *    not want to throw away any data.
2022                  *  - this URB failing may be no error; EREMOTEIO means that
2023                  *    this transfer simply didn't need all the URBs we submitted
2024                  * so, we report that the transfer was submitted successfully and
2025                  * in case of error we discard all previous URBs. later when
2026                  * the final reap completes we can report error to the user,
2027                  * or success if an earlier URB was completed successfully.
2028                  */
2029                 tpriv->reap_action = errno == EREMOTEIO ? COMPLETED_EARLY : SUBMIT_FAILED;
2030
2031                 /* The URBs we haven't submitted yet we count as already
2032                  * retired. */
2033                 tpriv->num_retired += num_urbs - i;
2034
2035                 /* If we completed short then don't try to discard. */
2036                 if (tpriv->reap_action == COMPLETED_EARLY)
2037                         return 0;
2038
2039                 discard_urbs(itransfer, 0, i);
2040
2041                 usbi_dbg("reporting successful submission but waiting for %d "
2042                          "discards before reporting error", i);
2043                 return 0;
2044         }
2045
2046         return 0;
2047 }
2048
2049 static int submit_iso_transfer(struct usbi_transfer *itransfer)
2050 {
2051         struct libusb_transfer *transfer =
2052                 USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
2053         struct linux_transfer_priv *tpriv = usbi_get_transfer_priv(itransfer);
2054         struct linux_device_handle_priv *hpriv =
2055                 usbi_get_device_handle_priv(transfer->dev_handle);
2056         struct usbfs_urb **urbs;
2057         int num_packets = transfer->num_iso_packets;
2058         int num_packets_remaining;
2059         int i, j;
2060         int num_urbs;
2061         unsigned int packet_len;
2062         unsigned int total_len = 0;
2063         unsigned char *urb_buffer = transfer->buffer;
2064
2065         if (num_packets < 1)
2066                 return LIBUSB_ERROR_INVALID_PARAM;
2067
2068         /* usbfs places arbitrary limits on iso URBs. this limit has changed
2069          * at least three times, but we attempt to detect this limit during
2070          * init and check it here. if the kernel rejects the request due to
2071          * its size, we return an error indicating such to the user.
2072          */
2073         for (i = 0; i < num_packets; i++) {
2074                 packet_len = transfer->iso_packet_desc[i].length;
2075
2076                 if (packet_len > max_iso_packet_len) {
2077                         usbi_warn(TRANSFER_CTX(transfer),
2078                                   "iso packet length of %u bytes exceeds maximum of %u bytes",
2079                                   packet_len, max_iso_packet_len);
2080                         return LIBUSB_ERROR_INVALID_PARAM;
2081                 }
2082
2083                 total_len += packet_len;
2084         }
2085
2086         if (transfer->length < (int)total_len)
2087                 return LIBUSB_ERROR_INVALID_PARAM;
2088
2089         /* usbfs limits the number of iso packets per URB */
2090         num_urbs = (num_packets + (MAX_ISO_PACKETS_PER_URB - 1)) / MAX_ISO_PACKETS_PER_URB;
2091
2092         usbi_dbg("need %d urbs for new transfer with length %d", num_urbs, transfer->length);
2093
2094         urbs = calloc(num_urbs, sizeof(*urbs));
2095         if (!urbs)
2096                 return LIBUSB_ERROR_NO_MEM;
2097
2098         tpriv->iso_urbs = urbs;
2099         tpriv->num_urbs = num_urbs;
2100         tpriv->num_retired = 0;
2101         tpriv->reap_action = NORMAL;
2102         tpriv->iso_packet_offset = 0;
2103
2104         /* allocate + initialize each URB with the correct number of packets */
2105         num_packets_remaining = num_packets;
2106         for (i = 0, j = 0; i < num_urbs; i++) {
2107                 int num_packets_in_urb = MIN(num_packets_remaining, MAX_ISO_PACKETS_PER_URB);
2108                 struct usbfs_urb *urb;
2109                 size_t alloc_size;
2110                 int k;
2111
2112                 alloc_size = sizeof(*urb)
2113                         + (num_packets_in_urb * sizeof(struct usbfs_iso_packet_desc));
2114                 urb = calloc(1, alloc_size);
2115                 if (!urb) {
2116                         free_iso_urbs(tpriv);
2117                         return LIBUSB_ERROR_NO_MEM;
2118                 }
2119                 urbs[i] = urb;
2120
2121                 /* populate packet lengths */
2122                 for (k = 0; k < num_packets_in_urb; j++, k++) {
2123                         packet_len = transfer->iso_packet_desc[j].length;
2124                         urb->buffer_length += packet_len;
2125                         urb->iso_frame_desc[k].length = packet_len;
2126                 }
2127
2128                 urb->usercontext = itransfer;
2129                 urb->type = USBFS_URB_TYPE_ISO;
2130                 /* FIXME: interface for non-ASAP data? */
2131                 urb->flags = USBFS_URB_ISO_ASAP;
2132                 urb->endpoint = transfer->endpoint;
2133                 urb->number_of_packets = num_packets_in_urb;
2134                 urb->buffer = urb_buffer;
2135
2136                 urb_buffer += urb->buffer_length;
2137                 num_packets_remaining -= num_packets_in_urb;
2138         }
2139
2140         /* submit URBs */
2141         for (i = 0; i < num_urbs; i++) {
2142                 int r = ioctl(hpriv->fd, IOCTL_USBFS_SUBMITURB, urbs[i]);
2143
2144                 if (r == 0)
2145                         continue;
2146
2147                 if (errno == ENODEV) {
2148                         r = LIBUSB_ERROR_NO_DEVICE;
2149                 } else if (errno == EINVAL) {
2150                         usbi_warn(TRANSFER_CTX(transfer), "submiturb failed, transfer too large");
2151                         r = LIBUSB_ERROR_INVALID_PARAM;
2152                 } else if (errno == EMSGSIZE) {
2153                         usbi_warn(TRANSFER_CTX(transfer), "submiturb failed, iso packet length too large");
2154                         r = LIBUSB_ERROR_INVALID_PARAM;
2155                 } else {
2156                         usbi_err(TRANSFER_CTX(transfer), "submiturb failed, errno=%d", errno);
2157                         r = LIBUSB_ERROR_IO;
2158                 }
2159
2160                 /* if the first URB submission fails, we can simply free up and
2161                  * return failure immediately. */
2162                 if (i == 0) {
2163                         usbi_dbg("first URB failed, easy peasy");
2164                         free_iso_urbs(tpriv);
2165                         return r;
2166                 }
2167
2168                 /* if it's not the first URB that failed, the situation is a bit
2169                  * tricky. we must discard all previous URBs. there are
2170                  * complications:
2171                  *  - discarding is asynchronous - discarded urbs will be reaped
2172                  *    later. the user must not have freed the transfer when the
2173                  *    discarded URBs are reaped, otherwise libusb will be using
2174                  *    freed memory.
2175                  *  - the earlier URBs may have completed successfully and we do
2176                  *    not want to throw away any data.
2177                  * so, in this case we discard all the previous URBs BUT we report
2178                  * that the transfer was submitted successfully. then later when
2179                  * the final discard completes we can report error to the user.
2180                  */
2181                 tpriv->reap_action = SUBMIT_FAILED;
2182
2183                 /* The URBs we haven't submitted yet we count as already
2184                  * retired. */
2185                 tpriv->num_retired = num_urbs - i;
2186                 discard_urbs(itransfer, 0, i);
2187
2188                 usbi_dbg("reporting successful submission but waiting for %d "
2189                          "discards before reporting error", i);
2190                 return 0;
2191         }
2192
2193         return 0;
2194 }
2195
2196 static int submit_control_transfer(struct usbi_transfer *itransfer)
2197 {
2198         struct linux_transfer_priv *tpriv = usbi_get_transfer_priv(itransfer);
2199         struct libusb_transfer *transfer =
2200                 USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
2201         struct linux_device_handle_priv *hpriv =
2202                 usbi_get_device_handle_priv(transfer->dev_handle);
2203         struct usbfs_urb *urb;
2204         int r;
2205
2206         if (transfer->length - LIBUSB_CONTROL_SETUP_SIZE > MAX_CTRL_BUFFER_LENGTH)
2207                 return LIBUSB_ERROR_INVALID_PARAM;
2208
2209         urb = calloc(1, sizeof(*urb));
2210         if (!urb)
2211                 return LIBUSB_ERROR_NO_MEM;
2212         tpriv->urbs = urb;
2213         tpriv->num_urbs = 1;
2214         tpriv->reap_action = NORMAL;
2215
2216         urb->usercontext = itransfer;
2217         urb->type = USBFS_URB_TYPE_CONTROL;
2218         urb->endpoint = transfer->endpoint;
2219         urb->buffer = transfer->buffer;
2220         urb->buffer_length = transfer->length;
2221
2222         r = ioctl(hpriv->fd, IOCTL_USBFS_SUBMITURB, urb);
2223         if (r < 0) {
2224                 free(urb);
2225                 tpriv->urbs = NULL;
2226                 if (errno == ENODEV)
2227                         return LIBUSB_ERROR_NO_DEVICE;
2228
2229                 usbi_err(TRANSFER_CTX(transfer), "submiturb failed, errno=%d", errno);
2230                 return LIBUSB_ERROR_IO;
2231         }
2232         return 0;
2233 }
2234
2235 static int op_submit_transfer(struct usbi_transfer *itransfer)
2236 {
2237         struct libusb_transfer *transfer =
2238                 USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
2239
2240         switch (transfer->type) {
2241         case LIBUSB_TRANSFER_TYPE_CONTROL:
2242                 return submit_control_transfer(itransfer);
2243         case LIBUSB_TRANSFER_TYPE_BULK:
2244         case LIBUSB_TRANSFER_TYPE_BULK_STREAM:
2245                 return submit_bulk_transfer(itransfer);
2246         case LIBUSB_TRANSFER_TYPE_INTERRUPT:
2247                 return submit_bulk_transfer(itransfer);
2248         case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
2249                 return submit_iso_transfer(itransfer);
2250         default:
2251                 usbi_err(TRANSFER_CTX(transfer), "unknown transfer type %u", transfer->type);
2252                 return LIBUSB_ERROR_INVALID_PARAM;
2253         }
2254 }
2255
2256 static int op_cancel_transfer(struct usbi_transfer *itransfer)
2257 {
2258         struct linux_transfer_priv *tpriv = usbi_get_transfer_priv(itransfer);
2259         struct libusb_transfer *transfer =
2260                 USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
2261         int r;
2262
2263         if (!tpriv->urbs)
2264                 return LIBUSB_ERROR_NOT_FOUND;
2265
2266         r = discard_urbs(itransfer, 0, tpriv->num_urbs);
2267         if (r != 0)
2268                 return r;
2269
2270         switch (transfer->type) {
2271         case LIBUSB_TRANSFER_TYPE_BULK:
2272         case LIBUSB_TRANSFER_TYPE_BULK_STREAM:
2273                 if (tpriv->reap_action == ERROR)
2274                         break;
2275                 /* else, fall through */
2276         default:
2277                 tpriv->reap_action = CANCELLED;
2278         }
2279
2280         return 0;
2281 }
2282
2283 static void op_clear_transfer_priv(struct usbi_transfer *itransfer)
2284 {
2285         struct libusb_transfer *transfer =
2286                 USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
2287         struct linux_transfer_priv *tpriv = usbi_get_transfer_priv(itransfer);
2288
2289         switch (transfer->type) {
2290         case LIBUSB_TRANSFER_TYPE_CONTROL:
2291         case LIBUSB_TRANSFER_TYPE_BULK:
2292         case LIBUSB_TRANSFER_TYPE_BULK_STREAM:
2293         case LIBUSB_TRANSFER_TYPE_INTERRUPT:
2294                 if (tpriv->urbs) {
2295                         free(tpriv->urbs);
2296                         tpriv->urbs = NULL;
2297                 }
2298                 break;
2299         case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
2300                 if (tpriv->iso_urbs) {
2301                         free_iso_urbs(tpriv);
2302                         tpriv->iso_urbs = NULL;
2303                 }
2304                 break;
2305         default:
2306                 usbi_err(TRANSFER_CTX(transfer), "unknown transfer type %u", transfer->type);
2307         }
2308 }
2309
2310 static int handle_bulk_completion(struct usbi_transfer *itransfer,
2311         struct usbfs_urb *urb)
2312 {
2313         struct linux_transfer_priv *tpriv = usbi_get_transfer_priv(itransfer);
2314         struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
2315         int urb_idx = urb - tpriv->urbs;
2316
2317         usbi_mutex_lock(&itransfer->lock);
2318         usbi_dbg("handling completion status %d of bulk urb %d/%d", urb->status,
2319                  urb_idx + 1, tpriv->num_urbs);
2320
2321         tpriv->num_retired++;
2322
2323         if (tpriv->reap_action != NORMAL) {
2324                 /* cancelled, submit_fail, or completed early */
2325                 usbi_dbg("abnormal reap: urb status %d", urb->status);
2326
2327                 /* even though we're in the process of cancelling, it's possible that
2328                  * we may receive some data in these URBs that we don't want to lose.
2329                  * examples:
2330                  * 1. while the kernel is cancelling all the packets that make up an
2331                  *    URB, a few of them might complete. so we get back a successful
2332                  *    cancellation *and* some data.
2333                  * 2. we receive a short URB which marks the early completion condition,
2334                  *    so we start cancelling the remaining URBs. however, we're too
2335                  *    slow and another URB completes (or at least completes partially).
2336                  *    (this can't happen since we always use BULK_CONTINUATION.)
2337                  *
2338                  * When this happens, our objectives are not to lose any "surplus" data,
2339                  * and also to stick it at the end of the previously-received data
2340                  * (closing any holes), so that libusb reports the total amount of
2341                  * transferred data and presents it in a contiguous chunk.
2342                  */
2343                 if (urb->actual_length > 0) {
2344                         unsigned char *target = transfer->buffer + itransfer->transferred;
2345
2346                         usbi_dbg("received %d bytes of surplus data", urb->actual_length);
2347                         if (urb->buffer != target) {
2348                                 usbi_dbg("moving surplus data from offset %zu to offset %zu",
2349                                          (unsigned char *)urb->buffer - transfer->buffer,
2350                                          target - transfer->buffer);
2351                                 memmove(target, urb->buffer, urb->actual_length);
2352                         }
2353                         itransfer->transferred += urb->actual_length;
2354                 }
2355
2356                 if (tpriv->num_retired == tpriv->num_urbs) {
2357                         usbi_dbg("abnormal reap: last URB handled, reporting");
2358                         if (tpriv->reap_action != COMPLETED_EARLY &&
2359                             tpriv->reap_status == LIBUSB_TRANSFER_COMPLETED)
2360                                 tpriv->reap_status = LIBUSB_TRANSFER_ERROR;
2361                         goto completed;
2362                 }
2363                 goto out_unlock;
2364         }
2365
2366         itransfer->transferred += urb->actual_length;
2367
2368         /* Many of these errors can occur on *any* urb of a multi-urb
2369          * transfer.  When they do, we tear down the rest of the transfer.
2370          */
2371         switch (urb->status) {
2372         case 0:
2373                 break;
2374         case -EREMOTEIO: /* short transfer */
2375                 break;
2376         case -ENOENT: /* cancelled */
2377         case -ECONNRESET:
2378                 break;
2379         case -ENODEV:
2380         case -ESHUTDOWN:
2381                 usbi_dbg("device removed");
2382                 tpriv->reap_status = LIBUSB_TRANSFER_NO_DEVICE;
2383                 goto cancel_remaining;
2384         case -EPIPE:
2385                 usbi_dbg("detected endpoint stall");
2386                 if (tpriv->reap_status == LIBUSB_TRANSFER_COMPLETED)
2387                         tpriv->reap_status = LIBUSB_TRANSFER_STALL;
2388                 goto cancel_remaining;
2389         case -EOVERFLOW:
2390                 /* overflow can only ever occur in the last urb */
2391                 usbi_dbg("overflow, actual_length=%d", urb->actual_length);
2392                 if (tpriv->reap_status == LIBUSB_TRANSFER_COMPLETED)
2393                         tpriv->reap_status = LIBUSB_TRANSFER_OVERFLOW;
2394                 goto completed;
2395         case -ETIME:
2396         case -EPROTO:
2397         case -EILSEQ:
2398         case -ECOMM:
2399         case -ENOSR:
2400                 usbi_dbg("low-level bus error %d", urb->status);
2401                 tpriv->reap_action = ERROR;
2402                 goto cancel_remaining;
2403         default:
2404                 usbi_warn(ITRANSFER_CTX(itransfer), "unrecognised urb status %d", urb->status);
2405                 tpriv->reap_action = ERROR;
2406                 goto cancel_remaining;
2407         }
2408
2409         /* if we've reaped all urbs or we got less data than requested then we're
2410          * done */
2411         if (tpriv->num_retired == tpriv->num_urbs) {
2412                 usbi_dbg("all URBs in transfer reaped --> complete!");
2413                 goto completed;
2414         } else if (urb->actual_length < urb->buffer_length) {
2415                 usbi_dbg("short transfer %d/%d --> complete!",
2416                          urb->actual_length, urb->buffer_length);
2417                 if (tpriv->reap_action == NORMAL)
2418                         tpriv->reap_action = COMPLETED_EARLY;
2419         } else {
2420                 goto out_unlock;
2421         }
2422
2423 cancel_remaining:
2424         if (tpriv->reap_action == ERROR && tpriv->reap_status == LIBUSB_TRANSFER_COMPLETED)
2425                 tpriv->reap_status = LIBUSB_TRANSFER_ERROR;
2426
2427         if (tpriv->num_retired == tpriv->num_urbs) /* nothing to cancel */
2428                 goto completed;
2429
2430         /* cancel remaining urbs and wait for their completion before
2431          * reporting results */
2432         discard_urbs(itransfer, urb_idx + 1, tpriv->num_urbs);
2433
2434 out_unlock:
2435         usbi_mutex_unlock(&itransfer->lock);
2436         return 0;
2437
2438 completed:
2439         free(tpriv->urbs);
2440         tpriv->urbs = NULL;
2441         usbi_mutex_unlock(&itransfer->lock);
2442         return tpriv->reap_action == CANCELLED ?
2443                 usbi_handle_transfer_cancellation(itransfer) :
2444                 usbi_handle_transfer_completion(itransfer, tpriv->reap_status);
2445 }
2446
2447 static int handle_iso_completion(struct usbi_transfer *itransfer,
2448         struct usbfs_urb *urb)
2449 {
2450         struct libusb_transfer *transfer =
2451                 USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
2452         struct linux_transfer_priv *tpriv = usbi_get_transfer_priv(itransfer);
2453         int num_urbs = tpriv->num_urbs;
2454         int urb_idx = 0;
2455         int i;
2456         enum libusb_transfer_status status = LIBUSB_TRANSFER_COMPLETED;
2457
2458         usbi_mutex_lock(&itransfer->lock);
2459         for (i = 0; i < num_urbs; i++) {
2460                 if (urb == tpriv->iso_urbs[i]) {
2461                         urb_idx = i + 1;
2462                         break;
2463                 }
2464         }
2465         if (urb_idx == 0) {
2466                 usbi_err(TRANSFER_CTX(transfer), "could not locate urb!");
2467                 usbi_mutex_unlock(&itransfer->lock);
2468                 return LIBUSB_ERROR_NOT_FOUND;
2469         }
2470
2471         usbi_dbg("handling completion status %d of iso urb %d/%d", urb->status,
2472                  urb_idx, num_urbs);
2473
2474         /* copy isochronous results back in */
2475
2476         for (i = 0; i < urb->number_of_packets; i++) {
2477                 struct usbfs_iso_packet_desc *urb_desc = &urb->iso_frame_desc[i];
2478                 struct libusb_iso_packet_descriptor *lib_desc =
2479                         &transfer->iso_packet_desc[tpriv->iso_packet_offset++];
2480
2481                 lib_desc->status = LIBUSB_TRANSFER_COMPLETED;
2482                 switch (urb_desc->status) {
2483                 case 0:
2484                         break;
2485                 case -ENOENT: /* cancelled */
2486                 case -ECONNRESET:
2487                         break;
2488                 case -ENODEV:
2489                 case -ESHUTDOWN:
2490                         usbi_dbg("packet %d - device removed", i);
2491                         lib_desc->status = LIBUSB_TRANSFER_NO_DEVICE;
2492                         break;
2493                 case -EPIPE:
2494                         usbi_dbg("packet %d - detected endpoint stall", i);
2495                         lib_desc->status = LIBUSB_TRANSFER_STALL;
2496                         break;
2497                 case -EOVERFLOW:
2498                         usbi_dbg("packet %d - overflow error", i);
2499                         lib_desc->status = LIBUSB_TRANSFER_OVERFLOW;
2500                         break;
2501                 case -ETIME:
2502                 case -EPROTO:
2503                 case -EILSEQ:
2504                 case -ECOMM:
2505                 case -ENOSR:
2506                 case -EXDEV:
2507                         usbi_dbg("packet %d - low-level USB error %d", i, urb_desc->status);
2508                         lib_desc->status = LIBUSB_TRANSFER_ERROR;
2509                         break;
2510                 default:
2511                         usbi_warn(TRANSFER_CTX(transfer), "packet %d - unrecognised urb status %d",
2512                                   i, urb_desc->status);
2513                         lib_desc->status = LIBUSB_TRANSFER_ERROR;
2514                         break;
2515                 }
2516                 lib_desc->actual_length = urb_desc->actual_length;
2517         }
2518
2519         tpriv->num_retired++;
2520
2521         if (tpriv->reap_action != NORMAL) { /* cancelled or submit_fail */
2522                 usbi_dbg("CANCEL: urb status %d", urb->status);
2523
2524                 if (tpriv->num_retired == num_urbs) {
2525                         usbi_dbg("CANCEL: last URB handled, reporting");
2526                         free_iso_urbs(tpriv);
2527                         if (tpriv->reap_action == CANCELLED) {
2528                                 usbi_mutex_unlock(&itransfer->lock);
2529                                 return usbi_handle_transfer_cancellation(itransfer);
2530                         } else {
2531                                 usbi_mutex_unlock(&itransfer->lock);
2532                                 return usbi_handle_transfer_completion(itransfer, LIBUSB_TRANSFER_ERROR);
2533                         }
2534                 }
2535                 goto out;
2536         }
2537
2538         switch (urb->status) {
2539         case 0:
2540                 break;
2541         case -ENOENT: /* cancelled */
2542         case -ECONNRESET:
2543                 break;
2544         case -ESHUTDOWN:
2545                 usbi_dbg("device removed");
2546                 status = LIBUSB_TRANSFER_NO_DEVICE;
2547                 break;
2548         default:
2549                 usbi_warn(TRANSFER_CTX(transfer), "unrecognised urb status %d", urb->status);
2550                 status = LIBUSB_TRANSFER_ERROR;
2551                 break;
2552         }
2553
2554         /* if we've reaped all urbs then we're done */
2555         if (tpriv->num_retired == num_urbs) {
2556                 usbi_dbg("all URBs in transfer reaped --> complete!");
2557                 free_iso_urbs(tpriv);
2558                 usbi_mutex_unlock(&itransfer->lock);
2559                 return usbi_handle_transfer_completion(itransfer, status);
2560         }
2561
2562 out:
2563         usbi_mutex_unlock(&itransfer->lock);
2564         return 0;
2565 }
2566
2567 static int handle_control_completion(struct usbi_transfer *itransfer,
2568         struct usbfs_urb *urb)
2569 {
2570         struct linux_transfer_priv *tpriv = usbi_get_transfer_priv(itransfer);
2571         int status;
2572
2573         usbi_mutex_lock(&itransfer->lock);
2574         usbi_dbg("handling completion status %d", urb->status);
2575
2576         itransfer->transferred += urb->actual_length;
2577
2578         if (tpriv->reap_action == CANCELLED) {
2579                 if (urb->status && urb->status != -ENOENT)
2580                         usbi_warn(ITRANSFER_CTX(itransfer), "cancel: unrecognised urb status %d",
2581                                   urb->status);
2582                 free(tpriv->urbs);
2583                 tpriv->urbs = NULL;
2584                 usbi_mutex_unlock(&itransfer->lock);
2585                 return usbi_handle_transfer_cancellation(itransfer);
2586         }
2587
2588         switch (urb->status) {
2589         case 0:
2590                 status = LIBUSB_TRANSFER_COMPLETED;
2591                 break;
2592         case -ENOENT: /* cancelled */
2593                 status = LIBUSB_TRANSFER_CANCELLED;
2594                 break;
2595         case -ENODEV:
2596         case -ESHUTDOWN:
2597                 usbi_dbg("device removed");
2598                 status = LIBUSB_TRANSFER_NO_DEVICE;
2599                 break;
2600         case -EPIPE:
2601                 usbi_dbg("unsupported control request");
2602                 status = LIBUSB_TRANSFER_STALL;
2603                 break;
2604         case -EOVERFLOW:
2605                 usbi_dbg("overflow, actual_length=%d", urb->actual_length);
2606                 status = LIBUSB_TRANSFER_OVERFLOW;
2607                 break;
2608         case -ETIME:
2609         case -EPROTO:
2610         case -EILSEQ:
2611         case -ECOMM:
2612         case -ENOSR:
2613                 usbi_dbg("low-level bus error %d", urb->status);
2614                 status = LIBUSB_TRANSFER_ERROR;
2615                 break;
2616         default:
2617                 usbi_warn(ITRANSFER_CTX(itransfer), "unrecognised urb status %d", urb->status);
2618                 status = LIBUSB_TRANSFER_ERROR;
2619                 break;
2620         }
2621
2622         free(tpriv->urbs);
2623         tpriv->urbs = NULL;
2624         usbi_mutex_unlock(&itransfer->lock);
2625         return usbi_handle_transfer_completion(itransfer, status);
2626 }
2627
2628 static int reap_for_handle(struct libusb_device_handle *handle)
2629 {
2630         struct linux_device_handle_priv *hpriv = usbi_get_device_handle_priv(handle);
2631         int r;
2632         struct usbfs_urb *urb = NULL;
2633         struct usbi_transfer *itransfer;
2634         struct libusb_transfer *transfer;
2635
2636         r = ioctl(hpriv->fd, IOCTL_USBFS_REAPURBNDELAY, &urb);
2637         if (r < 0) {
2638                 if (errno == EAGAIN)
2639                         return 1;
2640                 if (errno == ENODEV)
2641                         return LIBUSB_ERROR_NO_DEVICE;
2642
2643                 usbi_err(HANDLE_CTX(handle), "reap failed, errno=%d", errno);
2644                 return LIBUSB_ERROR_IO;
2645         }
2646
2647         itransfer = urb->usercontext;
2648         transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
2649
2650         usbi_dbg("urb type=%u status=%d transferred=%d", urb->type, urb->status, urb->actual_length);
2651
2652         switch (transfer->type) {
2653         case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
2654                 return handle_iso_completion(itransfer, urb);
2655         case LIBUSB_TRANSFER_TYPE_BULK:
2656         case LIBUSB_TRANSFER_TYPE_BULK_STREAM:
2657         case LIBUSB_TRANSFER_TYPE_INTERRUPT:
2658                 return handle_bulk_completion(itransfer, urb);
2659         case LIBUSB_TRANSFER_TYPE_CONTROL:
2660                 return handle_control_completion(itransfer, urb);
2661         default:
2662                 usbi_err(HANDLE_CTX(handle), "unrecognised transfer type %u", transfer->type);
2663                 return LIBUSB_ERROR_OTHER;
2664         }
2665 }
2666
2667 static int op_handle_events(struct libusb_context *ctx,
2668         void *event_data, unsigned int count, unsigned int num_ready)
2669 {
2670         struct pollfd *fds = event_data;
2671         unsigned int n;
2672         int r;
2673
2674         usbi_mutex_lock(&ctx->open_devs_lock);
2675         for (n = 0; n < count && num_ready > 0; n++) {
2676                 struct pollfd *pollfd = &fds[n];
2677                 struct libusb_device_handle *handle;
2678                 struct linux_device_handle_priv *hpriv = NULL;
2679                 int reap_count;
2680
2681                 if (!pollfd->revents)
2682                         continue;
2683
2684                 num_ready--;
2685                 for_each_open_device(ctx, handle) {
2686                         hpriv = usbi_get_device_handle_priv(handle);
2687                         if (hpriv->fd == pollfd->fd)
2688                                 break;
2689                 }
2690
2691                 if (!hpriv || hpriv->fd != pollfd->fd) {
2692                         usbi_err(ctx, "cannot find handle for fd %d",
2693                                  pollfd->fd);
2694                         continue;
2695                 }
2696
2697                 if (pollfd->revents & POLLERR) {
2698                         /* remove the fd from the pollfd set so that it doesn't continuously
2699                          * trigger an event, and flag that it has been removed so op_close()
2700                          * doesn't try to remove it a second time */
2701                         usbi_remove_event_source(HANDLE_CTX(handle), hpriv->fd);
2702                         hpriv->fd_removed = 1;
2703
2704                         /* device will still be marked as attached if hotplug monitor thread
2705                          * hasn't processed remove event yet */
2706                         usbi_mutex_static_lock(&linux_hotplug_lock);
2707                         if (handle->dev->attached)
2708                                 linux_device_disconnected(handle->dev->bus_number,
2709                                                           handle->dev->device_address);
2710                         usbi_mutex_static_unlock(&linux_hotplug_lock);
2711
2712                         if (hpriv->caps & USBFS_CAP_REAP_AFTER_DISCONNECT) {
2713                                 do {
2714                                         r = reap_for_handle(handle);
2715                                 } while (r == 0);
2716                         }
2717
2718                         usbi_handle_disconnect(handle);
2719                         continue;
2720                 }
2721
2722                 reap_count = 0;
2723                 do {
2724                         r = reap_for_handle(handle);
2725                 } while (r == 0 && ++reap_count <= 25);
2726
2727                 if (r == 1 || r == LIBUSB_ERROR_NO_DEVICE)
2728                         continue;
2729                 else if (r < 0)
2730                         goto out;
2731         }
2732
2733         r = 0;
2734 out:
2735         usbi_mutex_unlock(&ctx->open_devs_lock);
2736         return r;
2737 }
2738
2739 const struct usbi_os_backend usbi_backend = {
2740         .name = "Linux usbfs",
2741         .caps = USBI_CAP_HAS_HID_ACCESS|USBI_CAP_SUPPORTS_DETACH_KERNEL_DRIVER,
2742         .init = op_init,
2743         .exit = op_exit,
2744         .set_option = op_set_option,
2745         .hotplug_poll = op_hotplug_poll,
2746         .get_active_config_descriptor = op_get_active_config_descriptor,
2747         .get_config_descriptor = op_get_config_descriptor,
2748         .get_config_descriptor_by_value = op_get_config_descriptor_by_value,
2749
2750         .wrap_sys_device = op_wrap_sys_device,
2751         .open = op_open,
2752         .close = op_close,
2753         .get_configuration = op_get_configuration,
2754         .set_configuration = op_set_configuration,
2755         .claim_interface = op_claim_interface,
2756         .release_interface = op_release_interface,
2757
2758         .set_interface_altsetting = op_set_interface,
2759         .clear_halt = op_clear_halt,
2760         .reset_device = op_reset_device,
2761
2762         .alloc_streams = op_alloc_streams,
2763         .free_streams = op_free_streams,
2764
2765         .dev_mem_alloc = op_dev_mem_alloc,
2766         .dev_mem_free = op_dev_mem_free,
2767
2768         .kernel_driver_active = op_kernel_driver_active,
2769         .detach_kernel_driver = op_detach_kernel_driver,
2770         .attach_kernel_driver = op_attach_kernel_driver,
2771
2772         .destroy_device = op_destroy_device,
2773
2774         .submit_transfer = op_submit_transfer,
2775         .cancel_transfer = op_cancel_transfer,
2776         .clear_transfer_priv = op_clear_transfer_priv,
2777
2778         .handle_events = op_handle_events,
2779
2780         .device_priv_size = sizeof(struct linux_device_priv),
2781         .device_handle_priv_size = sizeof(struct linux_device_handle_priv),
2782         .transfer_priv_size = sizeof(struct linux_transfer_priv),
2783 };