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