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