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