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