Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(262)

Side by Side Diff: third_party/libusb/src/libusb/os/linux_usbfs.c

Issue 19490008: Recommit: Update libusb 1.0.9 to libusbx 1.0.16 (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Linux usbfs backend for libusb 2 * Linux usbfs backend for libusbx
3 * Copyright (C) 2007-2009 Daniel Drake <dsd@gentoo.org> 3 * Copyright © 2007-2009 Daniel Drake <dsd@gentoo.org>
4 * Copyright (c) 2001 Johannes Erdfelt <johannes@erdfelt.com> 4 * Copyright © 2001 Johannes Erdfelt <johannes@erdfelt.com>
5 * Copyright © 2013 Nathan Hjelm <hjelmn@mac.com>
6 * Copyright © 2012-2013 Hans de Goede <hdegoede@redhat.com>
5 * 7 *
6 * This library is free software; you can redistribute it and/or 8 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public 9 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either 10 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version. 11 * version 2.1 of the License, or (at your option) any later version.
10 * 12 *
11 * This library is distributed in the hope that it will be useful, 13 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details. 16 * Lesser General Public License for more details.
15 * 17 *
16 * You should have received a copy of the GNU Lesser General Public 18 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software 19 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */ 21 */
20 22
21 #include <config.h> 23 #include "config.h"
24
25 #include <assert.h>
22 #include <ctype.h> 26 #include <ctype.h>
23 #include <dirent.h> 27 #include <dirent.h>
24 #include <errno.h> 28 #include <errno.h>
25 #include <fcntl.h> 29 #include <fcntl.h>
26 #include <poll.h> 30 #include <poll.h>
27 #include <stdio.h> 31 #include <stdio.h>
28 #include <stdlib.h> 32 #include <stdlib.h>
29 #include <string.h> 33 #include <string.h>
30 #include <sys/ioctl.h> 34 #include <sys/ioctl.h>
31 #include <sys/stat.h> 35 #include <sys/stat.h>
32 #include <sys/types.h> 36 #include <sys/types.h>
33 #include <sys/utsname.h> 37 #include <sys/utsname.h>
34 #include <unistd.h> 38 #include <unistd.h>
35 39
36 #include "libusb.h" 40 #include "libusb.h"
37 #include "libusbi.h" 41 #include "libusbi.h"
38 #include "linux_usbfs.h" 42 #include "linux_usbfs.h"
39 43
40 /* sysfs vs usbfs: 44 /* sysfs vs usbfs:
41 * opening a usbfs node causes the device to be resumed, so we attempt to 45 * opening a usbfs node causes the device to be resumed, so we attempt to
42 * avoid this during enumeration. 46 * avoid this during enumeration.
43 * 47 *
44 * sysfs allows us to read the kernel's in-memory copies of device descriptors 48 * 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: 49 * and so forth, avoiding the need to open the device:
46 * - The binary "descriptors" file was added in 2.6.23. 50 * - The binary "descriptors" file contains all config descriptors since
47 * - The "busnum" file was added in 2.6.22 51 * 2.6.26, commit 217a9081d8e69026186067711131b77f0ce219ed
52 * - The binary "descriptors" file was added in 2.6.23, commit
53 * 69d42a78f935d19384d1f6e4f94b65bb162b36df, but it only contains the
54 * active config descriptors
55 * - The "busnum" file was added in 2.6.22, commit
56 * 83f7d958eab2fbc6b159ee92bf1493924e1d0f72
48 * - The "devnum" file has been present since pre-2.6.18 57 * - The "devnum" file has been present since pre-2.6.18
49 * - the "bConfigurationValue" file has been present since pre-2.6.18 58 * - the "bConfigurationValue" file has been present since pre-2.6.18
50 * 59 *
51 * If we have bConfigurationValue, busnum, and devnum, then we can determine 60 * If we have bConfigurationValue, busnum, and devnum, then we can determine
52 * the active configuration without having to open the usbfs node in RDWR mode. 61 * the active configuration without having to open the usbfs node in RDWR mode.
53 * We assume this is the case if we see the busnum file (indicates 2.6.22+).
54 * The busnum file is important as that is the only way we can relate sysfs 62 * The busnum file is important as that is the only way we can relate sysfs
55 * devices to usbfs nodes. 63 * devices to usbfs nodes.
56 * 64 *
57 * If we also have descriptors, we can obtain the device descriptor and active 65 * If we also have all descriptors, we can obtain the device descriptor and
58 * configuration without touching usbfs at all. 66 * configuration without touching usbfs at all.
59 *
60 * The descriptors file originally only contained the active configuration
61 * descriptor alongside the device descriptor, but all configurations are
62 * included as of Linux 2.6.26.
63 */ 67 */
64 68
65 /* endianness for multi-byte fields: 69 /* endianness for multi-byte fields:
66 * 70 *
67 * Descriptors exposed by usbfs have the multi-byte fields in the device 71 * 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 72 * descriptor as host endian. Multi-byte fields in the other descriptors are
69 * bus-endian. The kernel documentation says otherwise, but it is wrong. 73 * bus-endian. The kernel documentation says otherwise, but it is wrong.
74 *
75 * In sysfs all descriptors are bus-endian.
70 */ 76 */
71 77
72 static const char *usbfs_path = NULL; 78 static const char *usbfs_path = NULL;
73 79
74 /* use usbdev*.* device names in /dev instead of the usbfs bus directories */ 80 /* use usbdev*.* device names in /dev instead of the usbfs bus directories */
75 static int usbdev_names = 0; 81 static int usbdev_names = 0;
76 82
77 /* Linux 2.6.32 adds support for a bulk continuation URB flag. this basically 83 /* Linux 2.6.32 adds support for a bulk continuation URB flag. this basically
78 * allows us to mark URBs as being part of a specific logical transfer when 84 * allows us to mark URBs as being part of a specific logical transfer when
79 * we submit them to the kernel. then, on any error except a cancellation, all 85 * we submit them to the kernel. then, on any error except a cancellation, all
(...skipping 14 matching lines...) Expand all
94 /* Linux 2.6.31 fixes support for the zero length packet URB flag. This 100 /* Linux 2.6.31 fixes support for the zero length packet URB flag. This
95 * allows us to mark URBs that should be followed by a zero length data 101 * allows us to mark URBs that should be followed by a zero length data
96 * packet, which can be required by device- or class-specific protocols. 102 * packet, which can be required by device- or class-specific protocols.
97 */ 103 */
98 static int supports_flag_zero_packet = -1; 104 static int supports_flag_zero_packet = -1;
99 105
100 /* clock ID for monotonic clock, as not all clock sources are available on all 106 /* clock ID for monotonic clock, as not all clock sources are available on all
101 * systems. appropriate choice made at initialization time. */ 107 * systems. appropriate choice made at initialization time. */
102 static clockid_t monotonic_clkid = -1; 108 static clockid_t monotonic_clkid = -1;
103 109
104 /* do we have a busnum to relate devices? this also implies that we can read 110 /* Linux 2.6.22 (commit 83f7d958eab2fbc6b159ee92bf1493924e1d0f72) adds a busnum
111 * to sysfs, so we can relate devices. This also implies that we can read
105 * the active configuration through bConfigurationValue */ 112 * the active configuration through bConfigurationValue */
106 static int sysfs_can_relate_devices = 0; 113 static int sysfs_can_relate_devices = -1;
107 114
108 /* do we have a descriptors file? */ 115 /* Linux 2.6.26 (commit 217a9081d8e69026186067711131b77f0ce219ed) adds all
109 static int sysfs_has_descriptors = 0; 116 * config descriptors (rather then just the active config) to the sysfs
117 * descriptors file, so from then on we can use them. */
118 static int sysfs_has_descriptors = -1;
119
120 /* how many times have we initted (and not exited) ? */
121 static volatile int init_count = 0;
122
123 /* Serialize hotplug start/stop, scan-devices, event-thread, and poll */
124 usbi_mutex_static_t linux_hotplug_lock = USBI_MUTEX_INITIALIZER;
125
126 static int linux_start_event_monitor(void);
127 static int linux_stop_event_monitor(void);
128 static int linux_scan_devices(struct libusb_context *ctx);
129 static int sysfs_scan_device(struct libusb_context *ctx, const char *devname);
130 static int detach_kernel_driver_and_claim(struct libusb_device_handle *, int);
131
132 #if !defined(USE_UDEV)
133 static int linux_default_scan_devices (struct libusb_context *ctx);
134 #endif
110 135
111 struct linux_device_priv { 136 struct linux_device_priv {
112 char *sysfs_dir; 137 char *sysfs_dir;
113 » unsigned char *dev_descriptor; 138 » unsigned char *descriptors;
114 » unsigned char *config_descriptor; 139 » int descriptors_len;
140 » int active_config; /* cache val for !sysfs_can_relate_devices */
115 }; 141 };
116 142
117 struct linux_device_handle_priv { 143 struct linux_device_handle_priv {
118 int fd; 144 int fd;
145 uint32_t caps;
119 }; 146 };
120 147
121 enum reap_action { 148 enum reap_action {
122 NORMAL = 0, 149 NORMAL = 0,
123 /* submission failed after the first URB, so await cancellation/completi on 150 /* submission failed after the first URB, so await cancellation/completi on
124 * of all the others */ 151 * of all the others */
125 SUBMIT_FAILED, 152 SUBMIT_FAILED,
126 153
127 /* cancelled by user or timeout */ 154 /* cancelled by user or timeout */
128 CANCELLED, 155 CANCELLED,
129 156
130 /* completed multi-URB transfer in non-final URB */ 157 /* completed multi-URB transfer in non-final URB */
131 COMPLETED_EARLY, 158 COMPLETED_EARLY,
132 159
133 /* one or more urbs encountered a low-level error */ 160 /* one or more urbs encountered a low-level error */
134 ERROR, 161 ERROR,
135 }; 162 };
136 163
137 struct linux_transfer_priv { 164 struct linux_transfer_priv {
138 union { 165 union {
139 struct usbfs_urb *urbs; 166 struct usbfs_urb *urbs;
140 struct usbfs_urb **iso_urbs; 167 struct usbfs_urb **iso_urbs;
141 }; 168 };
142 169
143 enum reap_action reap_action; 170 enum reap_action reap_action;
144 int num_urbs; 171 int num_urbs;
145 » unsigned int num_retired; 172 » int num_retired;
146 enum libusb_transfer_status reap_status; 173 enum libusb_transfer_status reap_status;
147 174
148 /* next iso packet in user-supplied transfer to be populated */ 175 /* next iso packet in user-supplied transfer to be populated */
149 int iso_packet_offset; 176 int iso_packet_offset;
150 }; 177 };
151 178
152 static void _get_usbfs_path(struct libusb_device *dev, char *path) 179 static int _get_usbfs_fd(struct libusb_device *dev, mode_t mode, int silent)
153 { 180 {
181 struct libusb_context *ctx = DEVICE_CTX(dev);
182 char path[PATH_MAX];
183 int fd;
184
154 if (usbdev_names) 185 if (usbdev_names)
155 snprintf(path, PATH_MAX, "%s/usbdev%d.%d", 186 snprintf(path, PATH_MAX, "%s/usbdev%d.%d",
156 usbfs_path, dev->bus_number, dev->device_address); 187 usbfs_path, dev->bus_number, dev->device_address);
157 else 188 else
158 snprintf(path, PATH_MAX, "%s/%03d/%03d", 189 snprintf(path, PATH_MAX, "%s/%03d/%03d",
159 usbfs_path, dev->bus_number, dev->device_address); 190 usbfs_path, dev->bus_number, dev->device_address);
191
192 fd = open(path, mode);
193 if (fd != -1)
194 return fd; /* Success */
195
196 if (!silent) {
197 usbi_err(ctx, "libusbx couldn't open USB device %s: %s",
198 path, strerror(errno));
199 if (errno == EACCES && mode == O_RDWR)
200 usbi_err(ctx, "libusbx requires write access to USB "
201 "device nodes.");
202 }
203
204 if (errno == EACCES)
205 return LIBUSB_ERROR_ACCESS;
206 if (errno == ENOENT)
207 return LIBUSB_ERROR_NO_DEVICE;
208 return LIBUSB_ERROR_IO;
160 } 209 }
161 210
162 static struct linux_device_priv *_device_priv(struct libusb_device *dev) 211 static struct linux_device_priv *_device_priv(struct libusb_device *dev)
163 { 212 {
164 return (struct linux_device_priv *) dev->os_priv; 213 return (struct linux_device_priv *) dev->os_priv;
165 } 214 }
166 215
167 static struct linux_device_handle_priv *_device_handle_priv( 216 static struct linux_device_handle_priv *_device_handle_priv(
168 struct libusb_device_handle *handle) 217 struct libusb_device_handle *handle)
169 { 218 {
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
292 if (kminor < minor) 341 if (kminor < minor)
293 return 0; 342 return 0;
294 343
295 /* kminor == minor */ 344 /* kminor == minor */
296 if (atoms < 3) 345 if (atoms < 3)
297 return 0 == sublevel; 346 return 0 == sublevel;
298 347
299 return ksublevel >= sublevel; 348 return ksublevel >= sublevel;
300 } 349 }
301 350
302 /* Return 1 if filename exists inside dirname in sysfs.
303 SYSFS_DEVICE_PATH is assumed to be the beginning of the path. */
304 static int sysfs_has_file(const char *dirname, const char *filename)
305 {
306 struct stat statbuf;
307 char path[PATH_MAX];
308 int r;
309
310 snprintf(path, PATH_MAX, "%s/%s/%s", SYSFS_DEVICE_PATH, dirname, filenam e);
311 r = stat(path, &statbuf);
312 if (r == 0 && S_ISREG(statbuf.st_mode))
313 return 1;
314
315 return 0;
316 }
317
318 static int op_init(struct libusb_context *ctx) 351 static int op_init(struct libusb_context *ctx)
319 { 352 {
320 struct stat statbuf; 353 struct stat statbuf;
321 int r; 354 int r;
322 355
323 usbfs_path = find_usbfs_path(); 356 usbfs_path = find_usbfs_path();
324 if (!usbfs_path) { 357 if (!usbfs_path) {
325 usbi_err(ctx, "could not find usbfs"); 358 usbi_err(ctx, "could not find usbfs");
326 return LIBUSB_ERROR_OTHER; 359 return LIBUSB_ERROR_OTHER;
327 } 360 }
(...skipping 18 matching lines...) Expand all
346 supports_flag_zero_packet = kernel_version_ge(2,6,31); 379 supports_flag_zero_packet = kernel_version_ge(2,6,31);
347 if (-1 == supports_flag_zero_packet) { 380 if (-1 == supports_flag_zero_packet) {
348 usbi_err(ctx, "error checking for zero length packet sup port"); 381 usbi_err(ctx, "error checking for zero length packet sup port");
349 return LIBUSB_ERROR_OTHER; 382 return LIBUSB_ERROR_OTHER;
350 } 383 }
351 } 384 }
352 385
353 if (supports_flag_zero_packet) 386 if (supports_flag_zero_packet)
354 usbi_dbg("zero length packet flag supported"); 387 usbi_dbg("zero length packet flag supported");
355 388
356 » r = stat(SYSFS_DEVICE_PATH, &statbuf); 389 » if (-1 == sysfs_has_descriptors) {
357 » if (r == 0 && S_ISDIR(statbuf.st_mode)) { 390 » » /* sysfs descriptors has all descriptors since Linux 2.6.26 */
358 » » DIR *devices = opendir(SYSFS_DEVICE_PATH); 391 » » sysfs_has_descriptors = kernel_version_ge(2,6,26);
359 » » struct dirent *entry; 392 » » if (-1 == sysfs_has_descriptors) {
360 393 » » » usbi_err(ctx, "error checking for sysfs descriptors");
361 » » usbi_dbg("found usb devices in sysfs"); 394 » » » return LIBUSB_ERROR_OTHER;
362
363 » » if (!devices) {
364 » » » usbi_err(ctx, "opendir devices failed errno=%d", errno);
365 » » » return LIBUSB_ERROR_IO;
366 } 395 }
367
368 /* Make sure sysfs supports all the required files. If it
369 * does not, then usbfs will be used instead. Determine
370 * this by looping through the directories in
371 * SYSFS_DEVICE_PATH. With the assumption that there will
372 * always be subdirectories of the name usbN (usb1, usb2,
373 * etc) representing the root hubs, check the usbN
374 * subdirectories to see if they have all the needed files.
375 * This algorithm uses the usbN subdirectories (root hubs)
376 * because a device disconnection will cause a race
377 * condition regarding which files are available, sometimes
378 * causing an incorrect result. The root hubs are used
379 * because it is assumed that they will always be present.
380 * See the "sysfs vs usbfs" comment at the top of this file
381 * for more details. */
382 while ((entry = readdir(devices))) {
383 int has_busnum=0, has_devnum=0, has_descriptors=0;
384 int has_configuration_value=0;
385
386 /* Only check the usbN directories. */
387 if (strncmp(entry->d_name, "usb", 3) != 0)
388 continue;
389
390 /* Check for the files libusb needs from sysfs. */
391 has_busnum = sysfs_has_file(entry->d_name, "busnum");
392 has_devnum = sysfs_has_file(entry->d_name, "devnum");
393 has_descriptors = sysfs_has_file(entry->d_name, "descrip tors");
394 has_configuration_value = sysfs_has_file(entry->d_name, "bConfigurationValue");
395
396 if (has_busnum && has_devnum && has_configuration_value)
397 sysfs_can_relate_devices = 1;
398 if (has_descriptors)
399 sysfs_has_descriptors = 1;
400
401 /* Only need to check until we've found ONE device which
402 has all the attributes. */
403 if (sysfs_has_descriptors && sysfs_can_relate_devices)
404 break;
405 }
406 closedir(devices);
407
408 /* Only use sysfs descriptors if the rest of
409 sysfs will work for libusb. */
410 if (!sysfs_can_relate_devices)
411 sysfs_has_descriptors = 0;
412 } else {
413 usbi_dbg("sysfs usb info not available");
414 sysfs_has_descriptors = 0;
415 sysfs_can_relate_devices = 0;
416 } 396 }
417 397
418 » return 0; 398 » if (-1 == sysfs_can_relate_devices) {
399 » » /* sysfs has busnum since Linux 2.6.22 */
400 » » sysfs_can_relate_devices = kernel_version_ge(2,6,22);
401 » » if (-1 == sysfs_can_relate_devices) {
402 » » » usbi_err(ctx, "error checking for sysfs busnum");
403 » » » return LIBUSB_ERROR_OTHER;
404 » » }
405 » }
406
407 » if (sysfs_can_relate_devices || sysfs_has_descriptors) {
408 » » r = stat(SYSFS_DEVICE_PATH, &statbuf);
409 » » if (r != 0 || !S_ISDIR(statbuf.st_mode)) {
410 » » » usbi_warn(ctx, "sysfs not mounted");
411 » » » sysfs_can_relate_devices = 0;
412 » » » sysfs_has_descriptors = 0;
413 » » }
414 » }
415
416 » if (sysfs_can_relate_devices)
417 » » usbi_dbg("sysfs can relate devices");
418
419 » if (sysfs_has_descriptors)
420 » » usbi_dbg("sysfs has complete descriptors");
421
422 » usbi_mutex_static_lock(&linux_hotplug_lock);
423 » r = LIBUSB_SUCCESS;
424 » if (init_count == 0) {
425 » » /* start up hotplug event handler */
426 » » r = linux_start_event_monitor();
427 » }
428 » if (r == LIBUSB_SUCCESS) {
429 » » r = linux_scan_devices(ctx);
430 » » if (r == LIBUSB_SUCCESS)
431 » » » init_count++;
432 » » else if (init_count == 0)
433 » » » linux_stop_event_monitor();
434 » } else
435 » » usbi_err(ctx, "error starting hotplug event monitor");
436 » usbi_mutex_static_unlock(&linux_hotplug_lock);
437
438 » return r;
419 } 439 }
420 440
421 static int usbfs_get_device_descriptor(struct libusb_device *dev, 441 static void op_exit(void)
422 » unsigned char *buffer)
423 { 442 {
424 » struct linux_device_priv *priv = _device_priv(dev); 443 » usbi_mutex_static_lock(&linux_hotplug_lock);
444 » assert(init_count != 0);
445 » if (!--init_count) {
446 » » /* tear down event handler */
447 » » (void)linux_stop_event_monitor();
448 » }
449 » usbi_mutex_static_unlock(&linux_hotplug_lock);
450 }
425 451
426 » /* return cached copy */ 452 static int linux_start_event_monitor(void)
427 » memcpy(buffer, priv->dev_descriptor, DEVICE_DESC_LENGTH); 453 {
428 » return 0; 454 #if defined(USE_UDEV)
455 » return linux_udev_start_event_monitor();
456 #else
457 » return linux_netlink_start_event_monitor();
458 #endif
459 }
460
461 static int linux_stop_event_monitor(void)
462 {
463 #if defined(USE_UDEV)
464 » return linux_udev_stop_event_monitor();
465 #else
466 » return linux_netlink_stop_event_monitor();
467 #endif
468 }
469
470 static int linux_scan_devices(struct libusb_context *ctx)
471 {
472 #if defined(USE_UDEV)
473 » return linux_udev_scan_devices(ctx);
474 #else
475 » return linux_default_scan_devices(ctx);
476 #endif
477 }
478
479 static void op_hotplug_poll(void)
480 {
481 #if defined(USE_UDEV)
482 » linux_udev_hotplug_poll();
483 #else
484 » linux_netlink_hotplug_poll();
485 #endif
429 } 486 }
430 487
431 static int _open_sysfs_attr(struct libusb_device *dev, const char *attr) 488 static int _open_sysfs_attr(struct libusb_device *dev, const char *attr)
432 { 489 {
433 struct linux_device_priv *priv = _device_priv(dev); 490 struct linux_device_priv *priv = _device_priv(dev);
434 char filename[PATH_MAX]; 491 char filename[PATH_MAX];
435 int fd; 492 int fd;
436 493
437 snprintf(filename, PATH_MAX, "%s/%s/%s", 494 snprintf(filename, PATH_MAX, "%s/%s/%s",
438 SYSFS_DEVICE_PATH, priv->sysfs_dir, attr); 495 SYSFS_DEVICE_PATH, priv->sysfs_dir, attr);
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
474 return LIBUSB_ERROR_NO_DEVICE; /* For unplug race (trac #70) */ 531 return LIBUSB_ERROR_NO_DEVICE; /* For unplug race (trac #70) */
475 } 532 }
476 if (value < 0) { 533 if (value < 0) {
477 usbi_err(ctx, "%s contains a negative value", filename); 534 usbi_err(ctx, "%s contains a negative value", filename);
478 return LIBUSB_ERROR_IO; 535 return LIBUSB_ERROR_IO;
479 } 536 }
480 537
481 return value; 538 return value;
482 } 539 }
483 540
484 static int sysfs_get_device_descriptor(struct libusb_device *dev,
485 unsigned char *buffer)
486 {
487 int fd;
488 ssize_t r;
489
490 /* sysfs provides access to an in-memory copy of the device descriptor,
491 * so we use that rather than keeping our own copy */
492
493 fd = _open_sysfs_attr(dev, "descriptors");
494 if (fd < 0)
495 return fd;
496
497 r = read(fd, buffer, DEVICE_DESC_LENGTH);;
498 close(fd);
499 if (r < 0) {
500 usbi_err(DEVICE_CTX(dev), "read failed, ret=%d errno=%d", fd, er rno);
501 return LIBUSB_ERROR_IO;
502 } else if (r < DEVICE_DESC_LENGTH) {
503 usbi_err(DEVICE_CTX(dev), "short read %d/%d", r, DEVICE_DESC_LEN GTH);
504 return LIBUSB_ERROR_IO;
505 }
506
507 return 0;
508 }
509
510 static int op_get_device_descriptor(struct libusb_device *dev, 541 static int op_get_device_descriptor(struct libusb_device *dev,
511 unsigned char *buffer, int *host_endian) 542 unsigned char *buffer, int *host_endian)
512 { 543 {
513 » if (sysfs_has_descriptors) { 544 » struct linux_device_priv *priv = _device_priv(dev);
514 » » return sysfs_get_device_descriptor(dev, buffer);
515 » } else {
516 » » *host_endian = 1;
517 » » return usbfs_get_device_descriptor(dev, buffer);
518 » }
519 }
520 545
521 static int usbfs_get_active_config_descriptor(struct libusb_device *dev, 546 » *host_endian = sysfs_has_descriptors ? 0 : 1;
522 » unsigned char *buffer, size_t len) 547 » memcpy(buffer, priv->descriptors, DEVICE_DESC_LENGTH);
523 {
524 » struct linux_device_priv *priv = _device_priv(dev);
525 » if (!priv->config_descriptor)
526 » » return LIBUSB_ERROR_NOT_FOUND; /* device is unconfigured */
527 548
528 /* retrieve cached copy */
529 memcpy(buffer, priv->config_descriptor, len);
530 return 0; 549 return 0;
531 } 550 }
532 551
533 /* read the bConfigurationValue for a device */ 552 /* read the bConfigurationValue for a device */
534 static int sysfs_get_active_config(struct libusb_device *dev, int *config) 553 static int sysfs_get_active_config(struct libusb_device *dev, int *config)
535 { 554 {
536 char *endptr; 555 char *endptr;
537 char tmp[4] = {0, 0, 0, 0}; 556 char tmp[4] = {0, 0, 0, 0};
538 long num; 557 long num;
539 int fd; 558 int fd;
(...skipping 26 matching lines...) Expand all
566 num = strtol(tmp, &endptr, 10); 585 num = strtol(tmp, &endptr, 10);
567 if (endptr == tmp) { 586 if (endptr == tmp) {
568 usbi_err(DEVICE_CTX(dev), "error converting '%s' to integer", tm p); 587 usbi_err(DEVICE_CTX(dev), "error converting '%s' to integer", tm p);
569 return LIBUSB_ERROR_IO; 588 return LIBUSB_ERROR_IO;
570 } 589 }
571 590
572 *config = (int) num; 591 *config = (int) num;
573 return 0; 592 return 0;
574 } 593 }
575 594
576 /* takes a usbfs/descriptors fd seeked to the start of a configuration, and 595 int linux_get_device_address (struct libusb_context *ctx, int detached,
577 * seeks to the next one. */ 596 » uint8_t *busnum, uint8_t *devaddr,const char *dev_node,
578 static int seek_to_next_config(struct libusb_context *ctx, int fd, 597 » const char *sys_name)
579 » int host_endian) 598 {
599 » usbi_dbg("getting address for device: %s detached: %d", sys_name, detach ed);
600 » /* can't use sysfs to read the bus and device number if the
601 » * device has been detached */
602 » if (!sysfs_can_relate_devices || detached || NULL == sys_name) {
603 » » if (NULL == dev_node) {
604 » » » return LIBUSB_ERROR_OTHER;
605 » » }
606
607 » » /* will this work with all supported kernel versions? */
608 » » if (!strncmp(dev_node, "/dev/bus/usb", 12)) {
609 » » » sscanf (dev_node, "/dev/bus/usb/%hhd/%hhd", busnum, deva ddr);
610 » » } else if (!strncmp(dev_node, "/proc/bus/usb", 13)) {
611 » » » sscanf (dev_node, "/proc/bus/usb/%hhd/%hhd", busnum, dev addr);
612 » » }
613
614 » » return LIBUSB_SUCCESS;
615 » }
616
617 » usbi_dbg("scan %s", sys_name);
618
619 » *busnum = __read_sysfs_attr(ctx, sys_name, "busnum");
620 » if (0 > *busnum)
621 » » return *busnum;
622
623 » *devaddr = __read_sysfs_attr(ctx, sys_name, "devnum");
624 » if (0 > *devaddr)
625 » » return *devaddr;
626
627 » usbi_dbg("bus=%d dev=%d", *busnum, *devaddr);
628 » if (*busnum > 255 || *devaddr > 255)
629 » » return LIBUSB_ERROR_INVALID_PARAM;
630
631 » return LIBUSB_SUCCESS;
632 }
633
634 /* Return offset of the next descriptor with the given type */
635 static int seek_to_next_descriptor(struct libusb_context *ctx,
636 » uint8_t descriptor_type, unsigned char *buffer, int size)
637 {
638 » struct usb_descriptor_header header;
639 » int i;
640
641 » for (i = 0; size >= 0; i += header.bLength, size -= header.bLength) {
642 » » if (size == 0)
643 » » » return LIBUSB_ERROR_NOT_FOUND;
644
645 » » if (size < 2) {
646 » » » usbi_err(ctx, "short descriptor read %d/2", size);
647 » » » return LIBUSB_ERROR_IO;
648 » » }
649 » » usbi_parse_descriptor(buffer + i, "bb", &header, 0);
650
651 » » if (i && header.bDescriptorType == descriptor_type)
652 » » » return i;
653 » }
654 » usbi_err(ctx, "bLength overflow by %d bytes", -size);
655 » return LIBUSB_ERROR_IO;
656 }
657
658 /* Return offset to next config */
659 static int seek_to_next_config(struct libusb_context *ctx,
660 » unsigned char *buffer, int size)
580 { 661 {
581 struct libusb_config_descriptor config; 662 struct libusb_config_descriptor config;
582 » unsigned char tmp[6]; 663
583 » off_t off; 664 » if (size == 0)
584 » ssize_t r; 665 » » return LIBUSB_ERROR_NOT_FOUND;
585 666
586 » /* read first 6 bytes of descriptor */ 667 » if (size < LIBUSB_DT_CONFIG_SIZE) {
587 » r = read(fd, tmp, sizeof(tmp)); 668 » » usbi_err(ctx, "short descriptor read %d/%d",
588 » if (r < 0) { 669 » » » size, LIBUSB_DT_CONFIG_SIZE);
589 » » usbi_err(ctx, "read failed ret=%d errno=%d", r, errno);
590 return LIBUSB_ERROR_IO; 670 return LIBUSB_ERROR_IO;
591 » } else if (r < sizeof(tmp)) { 671 » }
592 » » usbi_err(ctx, "short descriptor read %d/%d", r, sizeof(tmp)); 672
673 » usbi_parse_descriptor(buffer, "bbwbbbbb", &config, 0);
674 » if (config.bDescriptorType != LIBUSB_DT_CONFIG) {
675 » » usbi_err(ctx, "descriptor is not a config desc (type 0x%02x)",
676 » » » config.bDescriptorType);
593 return LIBUSB_ERROR_IO; 677 return LIBUSB_ERROR_IO;
594 } 678 }
595 679
596 » /* seek forward to end of config */ 680 » /*
597 » usbi_parse_descriptor(tmp, "bbwbb", &config, host_endian); 681 » * In usbfs the config descriptors are config.wTotalLength bytes apart,
598 » off = lseek(fd, config.wTotalLength - sizeof(tmp), SEEK_CUR); 682 » * with any short reads from the device appearing as holes in the file.
599 » if (off < 0) { 683 » *
600 » » usbi_err(ctx, "seek failed ret=%d errno=%d", off, errno); 684 » * In sysfs wTotalLength is ignored, instead the kernel returns a
601 » » return LIBUSB_ERROR_IO; 685 » * config descriptor with verified bLength fields, with descriptors
602 » } 686 » * with an invalid bLength removed.
603 687 » */
604 » return 0; 688 » if (sysfs_has_descriptors) {
605 } 689 » » int next = seek_to_next_descriptor(ctx, LIBUSB_DT_CONFIG,
606 690 » » » » » » buffer, size);
607 static int sysfs_get_active_config_descriptor(struct libusb_device *dev, 691 » » if (next == LIBUSB_ERROR_NOT_FOUND)
608 » unsigned char *buffer, size_t len) 692 » » » next = size;
609 { 693 » » if (next < 0)
610 » int fd; 694 » » » return next;
611 » ssize_t r; 695
612 » off_t off; 696 » » if (next != config.wTotalLength)
613 » int to_copy; 697 » » » usbi_warn(ctx, "config length mismatch wTotalLength "
614 » int config; 698 » » » » "%d real %d", config.wTotalLength, next);
615 » unsigned char tmp[6]; 699 » » return next;
616 700 » } else {
617 » r = sysfs_get_active_config(dev, &config); 701 » » if (config.wTotalLength < LIBUSB_DT_CONFIG_SIZE) {
702 » » » usbi_err(ctx, "invalid wTotalLength %d",
703 » » » » config.wTotalLength);
704 » » » return LIBUSB_ERROR_IO;
705 » » } else if (config.wTotalLength > size) {
706 » » » usbi_warn(ctx, "short descriptor read %d/%d",
707 » » » » size, config.wTotalLength);
708 » » » return size;
709 » » } else
710 » » » return config.wTotalLength;
711 » }
712 }
713
714 static int op_get_config_descriptor_by_value(struct libusb_device *dev,
715 » uint8_t value, unsigned char **buffer, int *host_endian)
716 {
717 » struct libusb_context *ctx = DEVICE_CTX(dev);
718 » struct linux_device_priv *priv = _device_priv(dev);
719 » unsigned char *descriptors = priv->descriptors;
720 » int size = priv->descriptors_len;
721 » struct libusb_config_descriptor *config;
722
723 » *buffer = NULL;
724 » /* Unlike the device desc. config descs. are always in raw format */
725 » *host_endian = 0;
726
727 » /* Skip device header */
728 » descriptors += DEVICE_DESC_LENGTH;
729 » size -= DEVICE_DESC_LENGTH;
730
731 » /* Seek till the config is found, or till "EOF" */
732 » while (1) {
733 » » int next = seek_to_next_config(ctx, descriptors, size);
734 » » if (next < 0)
735 » » » return next;
736 » » config = (struct libusb_config_descriptor *)descriptors;
737 » » if (config->bConfigurationValue == value) {
738 » » » *buffer = descriptors;
739 » » » return next;
740 » » }
741 » » size -= next;
742 » » descriptors += next;
743 » }
744 }
745
746 static int op_get_active_config_descriptor(struct libusb_device *dev,
747 » unsigned char *buffer, size_t len, int *host_endian)
748 {
749 » int r, config;
750 » unsigned char *config_desc;
751
752 » if (sysfs_can_relate_devices) {
753 » » r = sysfs_get_active_config(dev, &config);
754 » » if (r < 0)
755 » » » return r;
756 » } else {
757 » » /* Use cached bConfigurationValue */
758 » » struct linux_device_priv *priv = _device_priv(dev);
759 » » config = priv->active_config;
760 » }
761 » if (config == -1)
762 » » return LIBUSB_ERROR_NOT_FOUND;
763
764 » r = op_get_config_descriptor_by_value(dev, config, &config_desc,
765 » » » » » host_endian);
618 if (r < 0) 766 if (r < 0)
619 return r; 767 return r;
620 » if (config == -1) 768
621 » » return LIBUSB_ERROR_NOT_FOUND; 769 » len = MIN(len, r);
622 770 » memcpy(buffer, config_desc, len);
623 » usbi_dbg("active configuration %d", config); 771 » return len;
624 772 }
625 » /* sysfs provides access to an in-memory copy of the device descriptor, 773
626 » * so we use that rather than keeping our own copy */ 774 static int op_get_config_descriptor(struct libusb_device *dev,
627 775 » uint8_t config_index, unsigned char *buffer, size_t len, int *host_endia n)
628 » fd = _open_sysfs_attr(dev, "descriptors"); 776 {
629 » if (fd < 0) 777 » struct linux_device_priv *priv = _device_priv(dev);
630 » » return fd; 778 » unsigned char *descriptors = priv->descriptors;
631 779 » int i, r, size = priv->descriptors_len;
632 » /* device might have been unconfigured since we read bConfigurationValue , 780
633 » * so first check that there is any config descriptor data at all... */ 781 » /* Unlike the device desc. config descs. are always in raw format */
634 » off = lseek(fd, 0, SEEK_END); 782 » *host_endian = 0;
635 » if (off < 1) { 783
636 » » usbi_err(DEVICE_CTX(dev), "end seek failed, ret=%d errno=%d", 784 » /* Skip device header */
637 » » » off, errno); 785 » descriptors += DEVICE_DESC_LENGTH;
638 » » close(fd); 786 » size -= DEVICE_DESC_LENGTH;
639 » » return LIBUSB_ERROR_IO; 787
640 » } else if (off == DEVICE_DESC_LENGTH) { 788 » /* Seek till the config is found, or till "EOF" */
641 » » close(fd); 789 » for (i = 0; ; i++) {
642 » » return LIBUSB_ERROR_NOT_FOUND; 790 » » r = seek_to_next_config(DEVICE_CTX(dev), descriptors, size);
643 » }
644
645 » off = lseek(fd, DEVICE_DESC_LENGTH, SEEK_SET);
646 » if (off < 0) {
647 » » usbi_err(DEVICE_CTX(dev), "seek failed, ret=%d errno=%d", off, e rrno);
648 » » close(fd);
649 » » return LIBUSB_ERROR_IO;
650 » }
651
652 » /* unbounded loop: we expect the descriptor to be present under all
653 » * circumstances */
654 » while (1) {
655 » » r = read(fd, tmp, sizeof(tmp));
656 » » if (r < 0) {
657 » » » usbi_err(DEVICE_CTX(dev), "read failed, ret=%d errno=%d" ,
658 » » » » fd, errno);
659 » » » return LIBUSB_ERROR_IO;
660 » » } else if (r < sizeof(tmp)) {
661 » » » usbi_err(DEVICE_CTX(dev), "short read %d/%d", r, sizeof( tmp));
662 » » » return LIBUSB_ERROR_IO;
663 » » }
664
665 » » /* check bConfigurationValue */
666 » » if (tmp[5] == config)
667 » » » break;
668
669 » » /* try the next descriptor */
670 » » off = lseek(fd, 0 - sizeof(tmp), SEEK_CUR);
671 » » if (off < 0)
672 » » » return LIBUSB_ERROR_IO;
673
674 » » r = seek_to_next_config(DEVICE_CTX(dev), fd, 0);
675 if (r < 0) 791 if (r < 0)
676 return r; 792 return r;
677 » } 793 » » if (i == config_index)
678 794 » » » break;
679 » to_copy = (len < sizeof(tmp)) ? len : sizeof(tmp); 795 » » size -= r;
680 » memcpy(buffer, tmp, to_copy); 796 » » descriptors += r;
681 » if (len > sizeof(tmp)) { 797 » }
682 » » r = read(fd, buffer + sizeof(tmp), len - sizeof(tmp)); 798
683 » » if (r < 0) { 799 » len = MIN(len, r);
684 » » » usbi_err(DEVICE_CTX(dev), "read failed, ret=%d errno=%d" , 800 » memcpy(buffer, descriptors, len);
685 » » » » fd, errno); 801 » return len;
686 » » » r = LIBUSB_ERROR_IO;
687 » » } else if (r == 0) {
688 » » » usbi_dbg("device is unconfigured");
689 » » » r = LIBUSB_ERROR_NOT_FOUND;
690 » » } else if (r < len - sizeof(tmp)) {
691 » » » usbi_err(DEVICE_CTX(dev), "short read %d/%d", r, len);
692 » » » r = LIBUSB_ERROR_IO;
693 » » }
694 » } else {
695 » » r = 0;
696 » }
697
698 » close(fd);
699 » return r;
700 }
701
702 static int op_get_active_config_descriptor(struct libusb_device *dev,
703 » unsigned char *buffer, size_t len, int *host_endian)
704 {
705 » if (sysfs_has_descriptors) {
706 » » return sysfs_get_active_config_descriptor(dev, buffer, len);
707 » } else {
708 » » return usbfs_get_active_config_descriptor(dev, buffer, len);
709 » }
710 }
711
712 /* takes a usbfs fd, attempts to find the requested config and copy a certain
713 * amount of it into an output buffer. */
714 static int get_config_descriptor(struct libusb_context *ctx, int fd,
715 » uint8_t config_index, unsigned char *buffer, size_t len)
716 {
717 » off_t off;
718 » ssize_t r;
719
720 » off = lseek(fd, DEVICE_DESC_LENGTH, SEEK_SET);
721 » if (off < 0) {
722 » » usbi_err(ctx, "seek failed ret=%d errno=%d", off, errno);
723 » » return LIBUSB_ERROR_IO;
724 » }
725
726 » /* might need to skip some configuration descriptors to reach the
727 » * requested configuration */
728 » while (config_index > 0) {
729 » » r = seek_to_next_config(ctx, fd, 1);
730 » » if (r < 0)
731 » » » return r;
732 » » config_index--;
733 » }
734
735 » /* read the rest of the descriptor */
736 » r = read(fd, buffer, len);
737 » if (r < 0) {
738 » » usbi_err(ctx, "read failed ret=%d errno=%d", r, errno);
739 » » return LIBUSB_ERROR_IO;
740 » } else if (r < len) {
741 » » usbi_err(ctx, "short output read %d/%d", r, len);
742 » » return LIBUSB_ERROR_IO;
743 » }
744
745 » return 0;
746 }
747
748 static int op_get_config_descriptor(struct libusb_device *dev,
749 » uint8_t config_index, unsigned char *buffer, size_t len, int *host_endia n)
750 {
751 » char filename[PATH_MAX];
752 » int fd;
753 » int r;
754
755 » /* always read from usbfs: sysfs only has the active descriptor
756 » * this will involve waking the device up, but oh well! */
757
758 » /* FIXME: the above is no longer true, new kernels have all descriptors
759 » * in the descriptors file. but its kinda hard to detect if the kernel
760 » * is sufficiently new. */
761
762 » _get_usbfs_path(dev, filename);
763 » fd = open(filename, O_RDONLY);
764 » if (fd < 0) {
765 » » usbi_err(DEVICE_CTX(dev),
766 » » » "open '%s' failed, ret=%d errno=%d", filename, fd, errno );
767 » » return LIBUSB_ERROR_IO;
768 » }
769
770 » r = get_config_descriptor(DEVICE_CTX(dev), fd, config_index, buffer, len );
771 » close(fd);
772 » return r;
773 }
774
775 /* cache the active config descriptor in memory. a value of -1 means that
776 * we aren't sure which one is active, so just assume the first one.
777 * only for usbfs. */
778 static int cache_active_config(struct libusb_device *dev, int fd,
779 » int active_config)
780 {
781 » struct linux_device_priv *priv = _device_priv(dev);
782 » struct libusb_config_descriptor config;
783 » unsigned char tmp[8];
784 » unsigned char *buf;
785 » int idx;
786 » int r;
787
788 » if (active_config == -1) {
789 » » idx = 0;
790 » } else {
791 » » r = usbi_get_config_index_by_value(dev, active_config, &idx);
792 » » if (r < 0)
793 » » » return r;
794 » » if (idx == -1)
795 » » » return LIBUSB_ERROR_NOT_FOUND;
796 » }
797
798 » r = get_config_descriptor(DEVICE_CTX(dev), fd, idx, tmp, sizeof(tmp));
799 » if (r < 0) {
800 » » usbi_err(DEVICE_CTX(dev), "first read error %d", r);
801 » » return r;
802 » }
803
804 » usbi_parse_descriptor(tmp, "bbw", &config, 0);
805 » buf = malloc(config.wTotalLength);
806 » if (!buf)
807 » » return LIBUSB_ERROR_NO_MEM;
808
809 » r = get_config_descriptor(DEVICE_CTX(dev), fd, idx, buf,
810 » » config.wTotalLength);
811 » if (r < 0) {
812 » » free(buf);
813 » » return r;
814 » }
815
816 » if (priv->config_descriptor)
817 » » free(priv->config_descriptor);
818 » priv->config_descriptor = buf;
819 » return 0;
820 } 802 }
821 803
822 /* send a control message to retrieve active configuration */ 804 /* send a control message to retrieve active configuration */
823 static int usbfs_get_active_config(struct libusb_device *dev, int fd) 805 static int usbfs_get_active_config(struct libusb_device *dev, int fd)
824 { 806 {
825 unsigned char active_config = 0; 807 unsigned char active_config = 0;
826 int r; 808 int r;
827 809
828 struct usbfs_ctrltransfer ctrl = { 810 struct usbfs_ctrltransfer ctrl = {
829 .bmRequestType = LIBUSB_ENDPOINT_IN, 811 .bmRequestType = LIBUSB_ENDPOINT_IN,
(...skipping 16 matching lines...) Expand all
846 return LIBUSB_ERROR_IO; 828 return LIBUSB_ERROR_IO;
847 } 829 }
848 830
849 return active_config; 831 return active_config;
850 } 832 }
851 833
852 static int initialize_device(struct libusb_device *dev, uint8_t busnum, 834 static int initialize_device(struct libusb_device *dev, uint8_t busnum,
853 uint8_t devaddr, const char *sysfs_dir) 835 uint8_t devaddr, const char *sysfs_dir)
854 { 836 {
855 struct linux_device_priv *priv = _device_priv(dev); 837 struct linux_device_priv *priv = _device_priv(dev);
856 » unsigned char *dev_buf; 838 » struct libusb_context *ctx = DEVICE_CTX(dev);
857 » char path[PATH_MAX]; 839 » int descriptors_size = 512; /* Begin with a 1024 byte alloc */
858 int fd, speed; 840 int fd, speed;
859 int active_config = 0;
860 int device_configured = 1;
861 ssize_t r; 841 ssize_t r;
862 842
863 dev->bus_number = busnum; 843 dev->bus_number = busnum;
864 dev->device_address = devaddr; 844 dev->device_address = devaddr;
865 845
866 if (sysfs_dir) { 846 if (sysfs_dir) {
867 priv->sysfs_dir = malloc(strlen(sysfs_dir) + 1); 847 priv->sysfs_dir = malloc(strlen(sysfs_dir) + 1);
868 if (!priv->sysfs_dir) 848 if (!priv->sysfs_dir)
869 return LIBUSB_ERROR_NO_MEM; 849 return LIBUSB_ERROR_NO_MEM;
870 strcpy(priv->sysfs_dir, sysfs_dir); 850 strcpy(priv->sysfs_dir, sysfs_dir);
871 851
872 /* Note speed can contain 1.5, in this case __read_sysfs_attr 852 /* Note speed can contain 1.5, in this case __read_sysfs_attr
873 will stop parsing at the '.' and return 1 */ 853 will stop parsing at the '.' and return 1 */
874 speed = __read_sysfs_attr(DEVICE_CTX(dev), sysfs_dir, "speed"); 854 speed = __read_sysfs_attr(DEVICE_CTX(dev), sysfs_dir, "speed");
875 if (speed >= 0) { 855 if (speed >= 0) {
876 switch (speed) { 856 switch (speed) {
877 case 1: dev->speed = LIBUSB_SPEED_LOW; break; 857 case 1: dev->speed = LIBUSB_SPEED_LOW; break;
878 case 12: dev->speed = LIBUSB_SPEED_FULL; break; 858 case 12: dev->speed = LIBUSB_SPEED_FULL; break;
879 case 480: dev->speed = LIBUSB_SPEED_HIGH; break; 859 case 480: dev->speed = LIBUSB_SPEED_HIGH; break;
880 case 5000: dev->speed = LIBUSB_SPEED_SUPER; break; 860 case 5000: dev->speed = LIBUSB_SPEED_SUPER; break;
881 default: 861 default:
882 usbi_warn(DEVICE_CTX(dev), "Unknown device speed : %d Mbps", speed); 862 usbi_warn(DEVICE_CTX(dev), "Unknown device speed : %d Mbps", speed);
883 } 863 }
884 } 864 }
885 } 865 }
886 866
867 /* cache descriptors in memory */
887 if (sysfs_has_descriptors) 868 if (sysfs_has_descriptors)
888 » » return 0; 869 » » fd = _open_sysfs_attr(dev, "descriptors");
870 » else
871 » » fd = _get_usbfs_fd(dev, O_RDONLY, 0);
872 » if (fd < 0)
873 » » return fd;
889 874
890 » /* cache device descriptor in memory so that we can retrieve it later 875 » do {
891 » * without waking the device up (op_get_device_descriptor) */ 876 » » descriptors_size *= 2;
877 » » priv->descriptors = usbi_reallocf(priv->descriptors,
878 » » » » » » descriptors_size);
879 » » if (!priv->descriptors) {
880 » » » close(fd);
881 » » » return LIBUSB_ERROR_NO_MEM;
882 » » }
883 » » /* usbfs has holes in the file */
884 » » if (!sysfs_has_descriptors) {
885 » » » memset(priv->descriptors + priv->descriptors_len,
886 » » » 0, descriptors_size - priv->descriptors_len);
887 » » }
888 » » r = read(fd, priv->descriptors + priv->descriptors_len,
889 » » » descriptors_size - priv->descriptors_len);
890 » » if (r < 0) {
891 » » » usbi_err(ctx, "read descriptor failed ret=%d errno=%d",
892 » » » » fd, errno);
893 » » » close(fd);
894 » » » return LIBUSB_ERROR_IO;
895 » » }
896 » » priv->descriptors_len += r;
897 » } while (priv->descriptors_len == descriptors_size);
898 »
899 » close(fd);
892 900
893 » priv->dev_descriptor = NULL; 901 » if (priv->descriptors_len < DEVICE_DESC_LENGTH) {
894 » priv->config_descriptor = NULL; 902 » » usbi_err(ctx, "short descriptor read (%d)",
895 903 » » » priv->descriptors_len);
896 » if (sysfs_can_relate_devices) {
897 » » int tmp = sysfs_get_active_config(dev, &active_config);
898 » » if (tmp < 0)
899 » » » return tmp;
900 » » if (active_config == -1)
901 » » » device_configured = 0;
902 » }
903
904 » _get_usbfs_path(dev, path);
905 » fd = open(path, O_RDWR);
906 » if (fd < 0 && errno == EACCES) {
907 » » fd = open(path, O_RDONLY);
908 » » /* if we only have read-only access to the device, we cannot
909 » » * send a control message to determine the active config. just
910 » » * assume the first one is active. */
911 » » active_config = -1;
912 » }
913
914 » if (fd < 0) {
915 » » usbi_err(DEVICE_CTX(dev), "open failed, ret=%d errno=%d", fd, er rno);
916 return LIBUSB_ERROR_IO; 904 return LIBUSB_ERROR_IO;
917 } 905 }
918 906
919 » if (!sysfs_can_relate_devices) { 907 » if (sysfs_can_relate_devices)
920 » » if (active_config == -1) { 908 » » return LIBUSB_SUCCESS;
921 » » » /* if we only have read-only access to the device, we ca nnot 909
922 » » » * send a control message to determine the active config . just 910 » /* cache active config */
923 » » » * assume the first one is active. */ 911 » fd = _get_usbfs_fd(dev, O_RDWR, 1);
924 » » » usbi_warn(DEVICE_CTX(dev), "access to %s is read-only; c annot " 912 » if (fd < 0) {
925 » » » » "determine active configuration descriptor", pat h); 913 » » /* cannot send a control message to determine the active
926 » » } else { 914 » » * config. just assume the first one is active. */
927 » » » active_config = usbfs_get_active_config(dev, fd); 915 » » usbi_warn(ctx, "Missing rw usbfs access; cannot determine "
928 » » » if (active_config == LIBUSB_ERROR_IO) { 916 » » » "active configuration descriptor");
929 » » » » /* buggy devices sometimes fail to report their active config. 917 » » if (priv->descriptors_len >=
930 » » » » * assume unconfigured and continue the probing */ 918 » » » » (DEVICE_DESC_LENGTH + LIBUSB_DT_CONFIG_SIZE)) {
931 » » » » usbi_warn(DEVICE_CTX(dev), "couldn't query activ e " 919 » » » struct libusb_config_descriptor config;
932 » » » » » "configuration, assumung unconfigured"); 920 » » » usbi_parse_descriptor(
933 » » » » device_configured = 0; 921 » » » » priv->descriptors + DEVICE_DESC_LENGTH,
934 » » » } else if (active_config < 0) { 922 » » » » "bbwbbbbb", &config, 0);
935 » » » » close(fd); 923 » » » priv->active_config = config.bConfigurationValue;
936 » » » » return active_config; 924 » » } else
937 » » » } else if (active_config == 0) { 925 » » » priv->active_config = -1; /* No config dt */
938 » » » » /* some buggy devices have a configuration 0, bu t we're 926
939 » » » » * reaching into the corner of a corner case her e, so let's 927 » » return LIBUSB_SUCCESS;
940 » » » » * not support buggy devices in these circumstan ces. 928 » }
941 » » » » * stick to the specs: a configuration value of 0 means 929
942 » » » » * unconfigured. */ 930 » r = usbfs_get_active_config(dev, fd);
943 » » » » usbi_dbg("active cfg 0? assuming unconfigured de vice"); 931 » if (r > 0) {
944 » » » » device_configured = 0; 932 » » priv->active_config = r;
945 » » » } 933 » » r = LIBUSB_SUCCESS;
934 » } else if (r == 0) {
935 » » /* some buggy devices have a configuration 0, but we're
936 » » * reaching into the corner of a corner case here, so let's
937 » » * not support buggy devices in these circumstances.
938 » » * stick to the specs: a configuration value of 0 means
939 » » * unconfigured. */
940 » » usbi_dbg("active cfg 0? assuming unconfigured device");
941 » » priv->active_config = -1;
942 » » r = LIBUSB_SUCCESS;
943 » } else if (r == LIBUSB_ERROR_IO) {
944 » » /* buggy devices sometimes fail to report their active config.
945 » » * assume unconfigured and continue the probing */
946 » » usbi_warn(ctx, "couldn't query active configuration, assuming"
947 » » » " unconfigured");
948 » » priv->active_config = -1;
949 » » r = LIBUSB_SUCCESS;
950 » } /* else r < 0, just return the error code */
951
952 » close(fd);
953 » return r;
954 }
955
956 static int linux_get_parent_info(struct libusb_device *dev, const char *sysfs_di r)
957 {
958 » struct libusb_context *ctx = DEVICE_CTX(dev);
959 » struct libusb_device *it;
960 » char *parent_sysfs_dir, *tmp;
961 » int ret, add_parent = 1;
962
963 » /* XXX -- can we figure out the topology when using usbfs? */
964 » if (NULL == sysfs_dir || 0 == strncmp(sysfs_dir, "usb", 3)) {
965 » » /* either using usbfs or finding the parent of a root hub */
966 » » return LIBUSB_SUCCESS;
967 » }
968
969 » parent_sysfs_dir = strdup(sysfs_dir);
970 » if (NULL != (tmp = strrchr(parent_sysfs_dir, '.')) ||
971 » NULL != (tmp = strrchr(parent_sysfs_dir, '-'))) {
972 » dev->port_number = atoi(tmp + 1);
973 » » *tmp = '\0';
974 » } else {
975 » » usbi_warn(ctx, "Can not parse sysfs_dir: %s, no parent info",
976 » » » parent_sysfs_dir);
977 » » free (parent_sysfs_dir);
978 » » return LIBUSB_SUCCESS;
979 » }
980
981 » /* is the parent a root hub? */
982 » if (NULL == strchr(parent_sysfs_dir, '-')) {
983 » » tmp = parent_sysfs_dir;
984 » » ret = asprintf (&parent_sysfs_dir, "usb%s", tmp);
985 » » free (tmp);
986 » » if (0 > ret) {
987 » » » return LIBUSB_ERROR_NO_MEM;
946 } 988 }
947 } 989 }
948 990
949 » dev_buf = malloc(DEVICE_DESC_LENGTH); 991 retry:
950 » if (!dev_buf) { 992 » /* find the parent in the context */
951 » » close(fd); 993 » usbi_mutex_lock(&ctx->usb_devs_lock);
952 » » return LIBUSB_ERROR_NO_MEM; 994 » list_for_each_entry(it, &ctx->usb_devs, list, struct libusb_device) {
995 » » struct linux_device_priv *priv = _device_priv(it);
996 » » if (0 == strcmp (priv->sysfs_dir, parent_sysfs_dir)) {
997 » » » dev->parent_dev = libusb_ref_device(it);
998 » » » break;
999 » » }
1000 » }
1001 » usbi_mutex_unlock(&ctx->usb_devs_lock);
1002
1003 » if (!dev->parent_dev && add_parent) {
1004 » » usbi_dbg("parent_dev %s not enumerated yet, enumerating now",
1005 » » » parent_sysfs_dir);
1006 » » sysfs_scan_device(ctx, parent_sysfs_dir);
1007 » » add_parent = 0;
1008 » » goto retry;
953 } 1009 }
954 1010
955 » r = read(fd, dev_buf, DEVICE_DESC_LENGTH); 1011 » usbi_dbg("Dev %p (%s) has parent %p (%s) port %d", dev, sysfs_dir,
956 » if (r < 0) { 1012 » » dev->parent_dev, parent_sysfs_dir, dev->port_number);
957 » » usbi_err(DEVICE_CTX(dev),
958 » » » "read descriptor failed ret=%d errno=%d", fd, errno);
959 » » free(dev_buf);
960 » » close(fd);
961 » » return LIBUSB_ERROR_IO;
962 » } else if (r < DEVICE_DESC_LENGTH) {
963 » » usbi_err(DEVICE_CTX(dev), "short descriptor read (%d)", r);
964 » » free(dev_buf);
965 » » close(fd);
966 » » return LIBUSB_ERROR_IO;
967 » }
968 1013
969 » /* bit of a hack: set num_configurations now because cache_active_config () 1014 » free (parent_sysfs_dir);
970 » * calls usbi_get_config_index_by_value() which uses it */
971 » dev->num_configurations = dev_buf[DEVICE_DESC_LENGTH - 1];
972 1015
973 » if (device_configured) { 1016 » return LIBUSB_SUCCESS;
974 » » r = cache_active_config(dev, fd, active_config);
975 » » if (r < 0) {
976 » » » close(fd);
977 » » » free(dev_buf);
978 » » » return r;
979 » » }
980 » }
981
982 » close(fd);
983 » priv->dev_descriptor = dev_buf;
984 » return 0;
985 } 1017 }
986 1018
987 static int enumerate_device(struct libusb_context *ctx, 1019 int linux_enumerate_device(struct libusb_context *ctx,
988 » struct discovered_devs **_discdevs, uint8_t busnum, uint8_t devaddr, 1020 » uint8_t busnum, uint8_t devaddr, const char *sysfs_dir)
989 » const char *sysfs_dir)
990 { 1021 {
991 struct discovered_devs *discdevs;
992 unsigned long session_id; 1022 unsigned long session_id;
993 int need_unref = 0;
994 struct libusb_device *dev; 1023 struct libusb_device *dev;
995 int r = 0; 1024 int r = 0;
996 1025
997 /* FIXME: session ID is not guaranteed unique as addresses can wrap and 1026 /* FIXME: session ID is not guaranteed unique as addresses can wrap and
998 * will be reused. instead we should add a simple sysfs attribute with 1027 * will be reused. instead we should add a simple sysfs attribute with
999 * a session ID. */ 1028 * a session ID. */
1000 session_id = busnum << 8 | devaddr; 1029 session_id = busnum << 8 | devaddr;
1001 usbi_dbg("busnum %d devaddr %d session_id %ld", busnum, devaddr, 1030 usbi_dbg("busnum %d devaddr %d session_id %ld", busnum, devaddr,
1002 session_id); 1031 session_id);
1003 1032
1004 » dev = usbi_get_device_by_session_id(ctx, session_id); 1033 » if (usbi_get_device_by_session_id(ctx, session_id)) {
1005 » if (dev) { 1034 » » /* device already exists in the context */
1006 » » usbi_dbg("using existing device for %d/%d (session %ld)", 1035 » » usbi_dbg("session_id %ld already exists", session_id);
1007 » » » busnum, devaddr, session_id); 1036 » » return LIBUSB_SUCCESS;
1008 » } else {
1009 » » usbi_dbg("allocating new device for %d/%d (session %ld)",
1010 » » » busnum, devaddr, session_id);
1011 » » dev = usbi_alloc_device(ctx, session_id);
1012 » » if (!dev)
1013 » » » return LIBUSB_ERROR_NO_MEM;
1014 » » need_unref = 1;
1015 » » r = initialize_device(dev, busnum, devaddr, sysfs_dir);
1016 » » if (r < 0)
1017 » » » goto out;
1018 » » r = usbi_sanitize_device(dev);
1019 » » if (r < 0)
1020 » » » goto out;
1021 } 1037 }
1022 1038
1023 » discdevs = discovered_devs_append(*_discdevs, dev); 1039 » usbi_dbg("allocating new device for %d/%d (session %ld)",
1024 » if (!discdevs) 1040 » » busnum, devaddr, session_id);
1025 » » r = LIBUSB_ERROR_NO_MEM; 1041 » dev = usbi_alloc_device(ctx, session_id);
1042 » if (!dev)
1043 » » return LIBUSB_ERROR_NO_MEM;
1044
1045 » r = initialize_device(dev, busnum, devaddr, sysfs_dir);
1046 » if (r < 0)
1047 » » goto out;
1048 » r = usbi_sanitize_device(dev);
1049 » if (r < 0)
1050 » » goto out;
1051
1052 » r = linux_get_parent_info(dev, sysfs_dir);
1053 » if (r < 0)
1054 » » goto out;
1055 out:
1056 » if (r < 0)
1057 » » libusb_unref_device(dev);
1026 else 1058 else
1027 » » *_discdevs = discdevs; 1059 » » usbi_connect_device(dev);
1028 1060
1029 out:
1030 if (need_unref)
1031 libusb_unref_device(dev);
1032 return r; 1061 return r;
1033 } 1062 }
1034 1063
1035 /* open a bus directory and adds all discovered devices to discdevs. on 1064 void linux_hotplug_enumerate(uint8_t busnum, uint8_t devaddr, const char *sys_na me)
1036 * failure (non-zero return) the pre-existing discdevs should be destroyed 1065 {
1037 * (and devices freed). on success, the new discdevs pointer should be used 1066 » struct libusb_context *ctx;
1038 * as it may have been moved. */ 1067
1039 static int usbfs_scan_busdir(struct libusb_context *ctx, 1068 » usbi_mutex_static_lock(&active_contexts_lock);
1040 » struct discovered_devs **_discdevs, uint8_t busnum) 1069 » list_for_each_entry(ctx, &active_contexts_list, list, struct libusb_cont ext) {
1070 » » linux_enumerate_device(ctx, busnum, devaddr, sys_name);
1071 » }
1072 » usbi_mutex_static_unlock(&active_contexts_lock);
1073 }
1074
1075 void linux_hotplug_disconnected(uint8_t busnum, uint8_t devaddr, const char *sys _name)
1076 {
1077 » struct libusb_context *ctx;
1078 » struct libusb_device *dev;
1079 » unsigned long session_id = busnum << 8 | devaddr;
1080
1081 » usbi_mutex_static_lock(&active_contexts_lock);
1082 » list_for_each_entry(ctx, &active_contexts_list, list, struct libusb_cont ext) {
1083 » » dev = usbi_get_device_by_session_id (ctx, session_id);
1084 » » if (NULL != dev) {
1085 » » » usbi_disconnect_device (dev);
1086 » » } else {
1087 » » » usbi_dbg("device not found for session %x", session_id);
1088 » » }
1089 » }
1090 » usbi_mutex_static_unlock(&active_contexts_lock);
1091 }
1092
1093 #if !defined(USE_UDEV)
1094 /* open a bus directory and adds all discovered devices to the context */
1095 static int usbfs_scan_busdir(struct libusb_context *ctx, uint8_t busnum)
1041 { 1096 {
1042 DIR *dir; 1097 DIR *dir;
1043 char dirpath[PATH_MAX]; 1098 char dirpath[PATH_MAX];
1044 struct dirent *entry; 1099 struct dirent *entry;
1045 struct discovered_devs *discdevs = *_discdevs;
1046 int r = LIBUSB_ERROR_IO; 1100 int r = LIBUSB_ERROR_IO;
1047 1101
1048 snprintf(dirpath, PATH_MAX, "%s/%03d", usbfs_path, busnum); 1102 snprintf(dirpath, PATH_MAX, "%s/%03d", usbfs_path, busnum);
1049 usbi_dbg("%s", dirpath); 1103 usbi_dbg("%s", dirpath);
1050 dir = opendir(dirpath); 1104 dir = opendir(dirpath);
1051 if (!dir) { 1105 if (!dir) {
1052 usbi_err(ctx, "opendir '%s' failed, errno=%d", dirpath, errno); 1106 usbi_err(ctx, "opendir '%s' failed, errno=%d", dirpath, errno);
1053 /* FIXME: should handle valid race conditions like hub unplugged 1107 /* FIXME: should handle valid race conditions like hub unplugged
1054 * during directory iteration - this is not an error */ 1108 * during directory iteration - this is not an error */
1055 return r; 1109 return r;
1056 } 1110 }
1057 1111
1058 while ((entry = readdir(dir))) { 1112 while ((entry = readdir(dir))) {
1059 int devaddr; 1113 int devaddr;
1060 1114
1061 if (entry->d_name[0] == '.') 1115 if (entry->d_name[0] == '.')
1062 continue; 1116 continue;
1063 1117
1064 devaddr = atoi(entry->d_name); 1118 devaddr = atoi(entry->d_name);
1065 if (devaddr == 0) { 1119 if (devaddr == 0) {
1066 usbi_dbg("unknown dir entry %s", entry->d_name); 1120 usbi_dbg("unknown dir entry %s", entry->d_name);
1067 continue; 1121 continue;
1068 } 1122 }
1069 1123
1070 » » if (enumerate_device(ctx, &discdevs, busnum, (uint8_t) devaddr, NULL)) { 1124 » » if (linux_enumerate_device(ctx, busnum, (uint8_t) devaddr, NULL) ) {
1071 usbi_dbg("failed to enumerate dir entry %s", entry->d_na me); 1125 usbi_dbg("failed to enumerate dir entry %s", entry->d_na me);
1072 continue; 1126 continue;
1073 } 1127 }
1074 1128
1075 r = 0; 1129 r = 0;
1076 } 1130 }
1077 1131
1078 if (!r)
1079 *_discdevs = discdevs;
1080 closedir(dir); 1132 closedir(dir);
1081 return r; 1133 return r;
1082 } 1134 }
1083 1135
1084 static int usbfs_get_device_list(struct libusb_context *ctx, 1136 static int usbfs_get_device_list(struct libusb_context *ctx)
1085 » struct discovered_devs **_discdevs)
1086 { 1137 {
1087 struct dirent *entry; 1138 struct dirent *entry;
1088 DIR *buses = opendir(usbfs_path); 1139 DIR *buses = opendir(usbfs_path);
1089 struct discovered_devs *discdevs = *_discdevs;
1090 int r = 0; 1140 int r = 0;
1091 1141
1092 if (!buses) { 1142 if (!buses) {
1093 usbi_err(ctx, "opendir buses failed errno=%d", errno); 1143 usbi_err(ctx, "opendir buses failed errno=%d", errno);
1094 return LIBUSB_ERROR_IO; 1144 return LIBUSB_ERROR_IO;
1095 } 1145 }
1096 1146
1097 while ((entry = readdir(buses))) { 1147 while ((entry = readdir(buses))) {
1098 struct discovered_devs *discdevs_new = discdevs;
1099 int busnum; 1148 int busnum;
1100 1149
1101 if (entry->d_name[0] == '.') 1150 if (entry->d_name[0] == '.')
1102 continue; 1151 continue;
1103 1152
1104 if (usbdev_names) { 1153 if (usbdev_names) {
1105 int devaddr; 1154 int devaddr;
1106 if (!_is_usbdev_entry(entry, &busnum, &devaddr)) 1155 if (!_is_usbdev_entry(entry, &busnum, &devaddr))
1107 continue; 1156 continue;
1108 1157
1109 » » » r = enumerate_device(ctx, &discdevs_new, busnum, 1158 » » » r = linux_enumerate_device(ctx, busnum, (uint8_t) devadd r, NULL);
1110 » » » » (uint8_t) devaddr, NULL);
1111 if (r < 0) { 1159 if (r < 0) {
1112 usbi_dbg("failed to enumerate dir entry %s", ent ry->d_name); 1160 usbi_dbg("failed to enumerate dir entry %s", ent ry->d_name);
1113 continue; 1161 continue;
1114 } 1162 }
1115 } else { 1163 } else {
1116 busnum = atoi(entry->d_name); 1164 busnum = atoi(entry->d_name);
1117 if (busnum == 0) { 1165 if (busnum == 0) {
1118 usbi_dbg("unknown dir entry %s", entry->d_name); 1166 usbi_dbg("unknown dir entry %s", entry->d_name);
1119 continue; 1167 continue;
1120 } 1168 }
1121 1169
1122 » » » r = usbfs_scan_busdir(ctx, &discdevs_new, busnum); 1170 » » » r = usbfs_scan_busdir(ctx, busnum);
1123 if (r < 0) 1171 if (r < 0)
1124 » » » » goto out; 1172 » » » » break;
1125 } 1173 }
1126 discdevs = discdevs_new;
1127 } 1174 }
1128 1175
1129 out:
1130 closedir(buses); 1176 closedir(buses);
1131 *_discdevs = discdevs;
1132 return r; 1177 return r;
1133 1178
1134 } 1179 }
1180 #endif
1135 1181
1136 static int sysfs_scan_device(struct libusb_context *ctx, 1182 static int sysfs_scan_device(struct libusb_context *ctx, const char *devname)
1137 » struct discovered_devs **_discdevs, const char *devname)
1138 { 1183 {
1139 » int busnum; 1184 » uint8_t busnum, devaddr;
1140 » int devaddr; 1185 » int ret;
1141 1186
1142 » usbi_dbg("scan %s", devname); 1187 » ret = linux_get_device_address (ctx, 0, &busnum, &devaddr, NULL, devname );
1188 » if (LIBUSB_SUCCESS != ret) {
1189 » » return ret;
1190 » }
1143 1191
1144 » busnum = __read_sysfs_attr(ctx, devname, "busnum"); 1192 » return linux_enumerate_device(ctx, busnum & 0xff, devaddr & 0xff,
1145 » if (busnum < 0)
1146 » » return busnum;
1147
1148 » devaddr = __read_sysfs_attr(ctx, devname, "devnum");
1149 » if (devaddr < 0)
1150 » » return devaddr;
1151
1152 » usbi_dbg("bus=%d dev=%d", busnum, devaddr);
1153 » if (busnum > 255 || devaddr > 255)
1154 » » return LIBUSB_ERROR_INVALID_PARAM;
1155
1156 » return enumerate_device(ctx, _discdevs, busnum & 0xff, devaddr & 0xff,
1157 devname); 1193 devname);
1158 } 1194 }
1159 1195
1160 static int sysfs_get_device_list(struct libusb_context *ctx, 1196 #if !defined(USE_UDEV)
1161 » struct discovered_devs **_discdevs) 1197 static int sysfs_get_device_list(struct libusb_context *ctx)
1162 { 1198 {
1163 struct discovered_devs *discdevs = *_discdevs;
1164 DIR *devices = opendir(SYSFS_DEVICE_PATH); 1199 DIR *devices = opendir(SYSFS_DEVICE_PATH);
1165 struct dirent *entry; 1200 struct dirent *entry;
1166 int r = LIBUSB_ERROR_IO; 1201 int r = LIBUSB_ERROR_IO;
1167 1202
1168 if (!devices) { 1203 if (!devices) {
1169 usbi_err(ctx, "opendir devices failed errno=%d", errno); 1204 usbi_err(ctx, "opendir devices failed errno=%d", errno);
1170 return r; 1205 return r;
1171 } 1206 }
1172 1207
1173 while ((entry = readdir(devices))) { 1208 while ((entry = readdir(devices))) {
1174 struct discovered_devs *discdevs_new = discdevs;
1175
1176 if ((!isdigit(entry->d_name[0]) && strncmp(entry->d_name, "usb", 3)) 1209 if ((!isdigit(entry->d_name[0]) && strncmp(entry->d_name, "usb", 3))
1177 || strchr(entry->d_name, ':')) 1210 || strchr(entry->d_name, ':'))
1178 continue; 1211 continue;
1179 1212
1180 » » if (sysfs_scan_device(ctx, &discdevs_new, entry->d_name)) { 1213 » » if (sysfs_scan_device(ctx, entry->d_name)) {
1181 usbi_dbg("failed to enumerate dir entry %s", entry->d_na me); 1214 usbi_dbg("failed to enumerate dir entry %s", entry->d_na me);
1182 continue; 1215 continue;
1183 } 1216 }
1184 1217
1185 r = 0; 1218 r = 0;
1186 discdevs = discdevs_new;
1187 } 1219 }
1188 1220
1189 if (!r)
1190 *_discdevs = discdevs;
1191 closedir(devices); 1221 closedir(devices);
1192 return r; 1222 return r;
1193 } 1223 }
1194 1224
1195 static int op_get_device_list(struct libusb_context *ctx, 1225 static int linux_default_scan_devices (struct libusb_context *ctx)
1196 » struct discovered_devs **_discdevs)
1197 { 1226 {
1198 /* we can retrieve device list and descriptors from sysfs or usbfs. 1227 /* we can retrieve device list and descriptors from sysfs or usbfs.
1199 * sysfs is preferable, because if we use usbfs we end up resuming 1228 * sysfs is preferable, because if we use usbfs we end up resuming
1200 * any autosuspended USB devices. however, sysfs is not available 1229 * any autosuspended USB devices. however, sysfs is not available
1201 * everywhere, so we need a usbfs fallback too. 1230 * everywhere, so we need a usbfs fallback too.
1202 * 1231 *
1203 * as described in the "sysfs vs usbfs" comment at the top of this 1232 * as described in the "sysfs vs usbfs" comment at the top of this
1204 * file, sometimes we have sysfs but not enough information to 1233 * file, sometimes we have sysfs but not enough information to
1205 * relate sysfs devices to usbfs nodes. op_init() determines the 1234 * relate sysfs devices to usbfs nodes. op_init() determines the
1206 * adequacy of sysfs and sets sysfs_can_relate_devices. 1235 * adequacy of sysfs and sets sysfs_can_relate_devices.
1207 */ 1236 */
1208 if (sysfs_can_relate_devices != 0) 1237 if (sysfs_can_relate_devices != 0)
1209 » » return sysfs_get_device_list(ctx, _discdevs); 1238 » » return sysfs_get_device_list(ctx);
1210 else 1239 else
1211 » » return usbfs_get_device_list(ctx, _discdevs); 1240 » » return usbfs_get_device_list(ctx);
1212 } 1241 }
1242 #endif
1213 1243
1214 static int op_open(struct libusb_device_handle *handle) 1244 static int op_open(struct libusb_device_handle *handle)
1215 { 1245 {
1216 struct linux_device_handle_priv *hpriv = _device_handle_priv(handle); 1246 struct linux_device_handle_priv *hpriv = _device_handle_priv(handle);
1217 » char filename[PATH_MAX]; 1247 » int r;
1218 1248
1219 » _get_usbfs_path(handle->dev, filename); 1249 » hpriv->fd = _get_usbfs_fd(handle->dev, O_RDWR, 0);
1220 » usbi_dbg("opening %s", filename); 1250 » if (hpriv->fd < 0)
1221 » hpriv->fd = open(filename, O_RDWR); 1251 » » return hpriv->fd;
1222 » if (hpriv->fd < 0) { 1252
1223 » » if (errno == EACCES) { 1253 » r = ioctl(hpriv->fd, IOCTL_USBFS_GET_CAPABILITIES, &hpriv->caps);
1224 » » » usbi_err(HANDLE_CTX(handle), "libusb couldn't open USB d evice %s: " 1254 » if (r < 0) {
1225 » » » » "Permission denied.", filename); 1255 » » if (errno == ENOTTY)
1226 » » » usbi_err(HANDLE_CTX(handle), 1256 » » » usbi_dbg("getcap not available");
1227 » » » » "libusb requires write access to USB device node s."); 1257 » » else
1228 » » » return LIBUSB_ERROR_ACCESS; 1258 » » » usbi_err(HANDLE_CTX(handle), "getcap failed (%d)", errno );
1229 » » } else if (errno == ENOENT) { 1259 » » hpriv->caps = 0;
1230 » » » usbi_err(HANDLE_CTX(handle), "libusb couldn't open USB d evice %s: " 1260 » » if (supports_flag_zero_packet)
1231 » » » » "No such file or directory.", filename); 1261 » » » hpriv->caps |= USBFS_CAP_ZERO_PACKET;
1232 » » » return LIBUSB_ERROR_NO_DEVICE; 1262 » » if (supports_flag_bulk_continuation)
1233 » » } else { 1263 » » » hpriv->caps |= USBFS_CAP_BULK_CONTINUATION;
1234 » » » usbi_err(HANDLE_CTX(handle),
1235 » » » » "open failed, code %d errno %d", hpriv->fd, errn o);
1236 » » » return LIBUSB_ERROR_IO;
1237 » » }
1238 } 1264 }
1239 1265
1240 return usbi_add_pollfd(HANDLE_CTX(handle), hpriv->fd, POLLOUT); 1266 return usbi_add_pollfd(HANDLE_CTX(handle), hpriv->fd, POLLOUT);
1241 } 1267 }
1242 1268
1243 static void op_close(struct libusb_device_handle *dev_handle) 1269 static void op_close(struct libusb_device_handle *dev_handle)
1244 { 1270 {
1245 int fd = _device_handle_priv(dev_handle)->fd; 1271 int fd = _device_handle_priv(dev_handle)->fd;
1246 usbi_remove_pollfd(HANDLE_CTX(dev_handle), fd); 1272 usbi_remove_pollfd(HANDLE_CTX(dev_handle), fd);
1247 close(fd); 1273 close(fd);
1248 } 1274 }
1249 1275
1250 static int op_get_configuration(struct libusb_device_handle *handle, 1276 static int op_get_configuration(struct libusb_device_handle *handle,
1251 int *config) 1277 int *config)
1252 { 1278 {
1253 int r; 1279 int r;
1254 if (sysfs_can_relate_devices != 1)
1255 return LIBUSB_ERROR_NOT_SUPPORTED;
1256 1280
1257 » r = sysfs_get_active_config(handle->dev, config); 1281 » if (sysfs_can_relate_devices) {
1282 » » r = sysfs_get_active_config(handle->dev, config);
1283 » } else {
1284 » » r = usbfs_get_active_config(handle->dev,
1285 » » » » » _device_handle_priv(handle)->fd);
1286 » }
1258 if (r < 0) 1287 if (r < 0)
1259 return r; 1288 return r;
1260 1289
1261 if (*config == -1) { 1290 if (*config == -1) {
1262 usbi_err(HANDLE_CTX(handle), "device unconfigured"); 1291 usbi_err(HANDLE_CTX(handle), "device unconfigured");
1263 *config = 0; 1292 *config = 0;
1264 } 1293 }
1265 1294
1266 return 0; 1295 return 0;
1267 } 1296 }
1268 1297
1269 static int op_set_configuration(struct libusb_device_handle *handle, int config) 1298 static int op_set_configuration(struct libusb_device_handle *handle, int config)
1270 { 1299 {
1271 struct linux_device_priv *priv = _device_priv(handle->dev); 1300 struct linux_device_priv *priv = _device_priv(handle->dev);
1272 int fd = _device_handle_priv(handle)->fd; 1301 int fd = _device_handle_priv(handle)->fd;
1273 int r = ioctl(fd, IOCTL_USBFS_SETCONFIG, &config); 1302 int r = ioctl(fd, IOCTL_USBFS_SETCONFIG, &config);
1274 if (r) { 1303 if (r) {
1275 if (errno == EINVAL) 1304 if (errno == EINVAL)
1276 return LIBUSB_ERROR_NOT_FOUND; 1305 return LIBUSB_ERROR_NOT_FOUND;
1277 else if (errno == EBUSY) 1306 else if (errno == EBUSY)
1278 return LIBUSB_ERROR_BUSY; 1307 return LIBUSB_ERROR_BUSY;
1279 else if (errno == ENODEV) 1308 else if (errno == ENODEV)
1280 return LIBUSB_ERROR_NO_DEVICE; 1309 return LIBUSB_ERROR_NO_DEVICE;
1281 1310
1282 usbi_err(HANDLE_CTX(handle), "failed, error %d errno %d", r, err no); 1311 usbi_err(HANDLE_CTX(handle), "failed, error %d errno %d", r, err no);
1283 return LIBUSB_ERROR_OTHER; 1312 return LIBUSB_ERROR_OTHER;
1284 } 1313 }
1285 1314
1286 » if (!sysfs_has_descriptors) { 1315 » /* update our cached active config descriptor */
1287 » » /* update our cached active config descriptor */ 1316 » priv->active_config = config;
1288 » » if (config == -1) {
1289 » » » if (priv->config_descriptor) {
1290 » » » » free(priv->config_descriptor);
1291 » » » » priv->config_descriptor = NULL;
1292 » » » }
1293 » » } else {
1294 » » » r = cache_active_config(handle->dev, fd, config);
1295 » » » if (r < 0)
1296 » » » » usbi_warn(HANDLE_CTX(handle),
1297 » » » » » "failed to update cached config descript or, error %d", r);
1298 » » }
1299 » }
1300 1317
1301 » return 0; 1318 » return LIBUSB_SUCCESS;
1302 } 1319 }
1303 1320
1304 static int op_claim_interface(struct libusb_device_handle *handle, int iface) 1321 static int claim_interface(struct libusb_device_handle *handle, int iface)
1305 { 1322 {
1306 int fd = _device_handle_priv(handle)->fd; 1323 int fd = _device_handle_priv(handle)->fd;
1307 int r = ioctl(fd, IOCTL_USBFS_CLAIMINTF, &iface); 1324 int r = ioctl(fd, IOCTL_USBFS_CLAIMINTF, &iface);
1308 if (r) { 1325 if (r) {
1309 if (errno == ENOENT) 1326 if (errno == ENOENT)
1310 return LIBUSB_ERROR_NOT_FOUND; 1327 return LIBUSB_ERROR_NOT_FOUND;
1311 else if (errno == EBUSY) 1328 else if (errno == EBUSY)
1312 return LIBUSB_ERROR_BUSY; 1329 return LIBUSB_ERROR_BUSY;
1313 else if (errno == ENODEV) 1330 else if (errno == ENODEV)
1314 return LIBUSB_ERROR_NO_DEVICE; 1331 return LIBUSB_ERROR_NO_DEVICE;
1315 1332
1316 usbi_err(HANDLE_CTX(handle), 1333 usbi_err(HANDLE_CTX(handle),
1317 "claim interface failed, error %d errno %d", r, errno); 1334 "claim interface failed, error %d errno %d", r, errno);
1318 return LIBUSB_ERROR_OTHER; 1335 return LIBUSB_ERROR_OTHER;
1319 } 1336 }
1320 return 0; 1337 return 0;
1321 } 1338 }
1322 1339
1323 static int op_release_interface(struct libusb_device_handle *handle, int iface) 1340 static int release_interface(struct libusb_device_handle *handle, int iface)
1324 { 1341 {
1325 int fd = _device_handle_priv(handle)->fd; 1342 int fd = _device_handle_priv(handle)->fd;
1326 int r = ioctl(fd, IOCTL_USBFS_RELEASEINTF, &iface); 1343 int r = ioctl(fd, IOCTL_USBFS_RELEASEINTF, &iface);
1327 if (r) { 1344 if (r) {
1328 if (errno == ENODEV) 1345 if (errno == ENODEV)
1329 return LIBUSB_ERROR_NO_DEVICE; 1346 return LIBUSB_ERROR_NO_DEVICE;
1330 1347
1331 usbi_err(HANDLE_CTX(handle), 1348 usbi_err(HANDLE_CTX(handle),
1332 "release interface failed, error %d errno %d", r, errno) ; 1349 "release interface failed, error %d errno %d", r, errno) ;
1333 return LIBUSB_ERROR_OTHER; 1350 return LIBUSB_ERROR_OTHER;
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
1384 int fd = _device_handle_priv(handle)->fd; 1401 int fd = _device_handle_priv(handle)->fd;
1385 int i, r, ret = 0; 1402 int i, r, ret = 0;
1386 1403
1387 /* Doing a device reset will cause the usbfs driver to get unbound 1404 /* Doing a device reset will cause the usbfs driver to get unbound
1388 from any interfaces it is bound to. By voluntarily unbinding 1405 from any interfaces it is bound to. By voluntarily unbinding
1389 the usbfs driver ourself, we stop the kernel from rebinding 1406 the usbfs driver ourself, we stop the kernel from rebinding
1390 the interface after reset (which would end up with the interface 1407 the interface after reset (which would end up with the interface
1391 getting bound to the in kernel driver if any). */ 1408 getting bound to the in kernel driver if any). */
1392 for (i = 0; i < USB_MAXINTERFACES; i++) { 1409 for (i = 0; i < USB_MAXINTERFACES; i++) {
1393 if (handle->claimed_interfaces & (1L << i)) { 1410 if (handle->claimed_interfaces & (1L << i)) {
1394 » » » op_release_interface(handle, i); 1411 » » » release_interface(handle, i);
1395 } 1412 }
1396 } 1413 }
1397 1414
1398 usbi_mutex_lock(&handle->lock); 1415 usbi_mutex_lock(&handle->lock);
1399 r = ioctl(fd, IOCTL_USBFS_RESET, NULL); 1416 r = ioctl(fd, IOCTL_USBFS_RESET, NULL);
1400 if (r) { 1417 if (r) {
1401 if (errno == ENODEV) { 1418 if (errno == ENODEV) {
1402 ret = LIBUSB_ERROR_NOT_FOUND; 1419 ret = LIBUSB_ERROR_NOT_FOUND;
1403 goto out; 1420 goto out;
1404 } 1421 }
1405 1422
1406 usbi_err(HANDLE_CTX(handle), 1423 usbi_err(HANDLE_CTX(handle),
1407 "reset failed error %d errno %d", r, errno); 1424 "reset failed error %d errno %d", r, errno);
1408 ret = LIBUSB_ERROR_OTHER; 1425 ret = LIBUSB_ERROR_OTHER;
1409 goto out; 1426 goto out;
1410 } 1427 }
1411 1428
1412 /* And re-claim any interfaces which were claimed before the reset */ 1429 /* And re-claim any interfaces which were claimed before the reset */
1413 for (i = 0; i < USB_MAXINTERFACES; i++) { 1430 for (i = 0; i < USB_MAXINTERFACES; i++) {
1414 if (handle->claimed_interfaces & (1L << i)) { 1431 if (handle->claimed_interfaces & (1L << i)) {
1415 » » » r = op_claim_interface(handle, i); 1432 » » » /*
1433 » » » * A driver may have completed modprobing during
1434 » » » * IOCTL_USBFS_RESET, and bound itself as soon as
1435 » » » * IOCTL_USBFS_RESET released the device lock
1436 » » » */
1437 » » » r = detach_kernel_driver_and_claim(handle, i);
1416 if (r) { 1438 if (r) {
1417 usbi_warn(HANDLE_CTX(handle), 1439 usbi_warn(HANDLE_CTX(handle),
1418 » » » » » "failed to re-claim interface %d after r eset", i); 1440 » » » » » "failed to re-claim interface %d after r eset: %s",
1441 » » » » » i, libusb_error_name(r));
1419 handle->claimed_interfaces &= ~(1L << i); 1442 handle->claimed_interfaces &= ~(1L << i);
1443 ret = LIBUSB_ERROR_NOT_FOUND;
1420 } 1444 }
1421 } 1445 }
1422 } 1446 }
1423 out: 1447 out:
1424 usbi_mutex_unlock(&handle->lock); 1448 usbi_mutex_unlock(&handle->lock);
1425 return ret; 1449 return ret;
1426 } 1450 }
1427 1451
1428 static int op_kernel_driver_active(struct libusb_device_handle *handle, 1452 static int op_kernel_driver_active(struct libusb_device_handle *handle,
1429 int interface) 1453 int interface)
1430 { 1454 {
1431 int fd = _device_handle_priv(handle)->fd; 1455 int fd = _device_handle_priv(handle)->fd;
1432 struct usbfs_getdriver getdrv; 1456 struct usbfs_getdriver getdrv;
1433 int r; 1457 int r;
1434 1458
1435 getdrv.interface = interface; 1459 getdrv.interface = interface;
1436 r = ioctl(fd, IOCTL_USBFS_GETDRIVER, &getdrv); 1460 r = ioctl(fd, IOCTL_USBFS_GETDRIVER, &getdrv);
1437 if (r) { 1461 if (r) {
1438 if (errno == ENODATA) 1462 if (errno == ENODATA)
1439 return 0; 1463 return 0;
1440 else if (errno == ENODEV) 1464 else if (errno == ENODEV)
1441 return LIBUSB_ERROR_NO_DEVICE; 1465 return LIBUSB_ERROR_NO_DEVICE;
1442 1466
1443 usbi_err(HANDLE_CTX(handle), 1467 usbi_err(HANDLE_CTX(handle),
1444 "get driver failed error %d errno %d", r, errno); 1468 "get driver failed error %d errno %d", r, errno);
1445 return LIBUSB_ERROR_OTHER; 1469 return LIBUSB_ERROR_OTHER;
1446 } 1470 }
1447 1471
1448 » return 1; 1472 » return (strcmp(getdrv.driver, "usbfs") == 0) ? 0 : 1;
1449 } 1473 }
1450 1474
1451 static int op_detach_kernel_driver(struct libusb_device_handle *handle, 1475 static int op_detach_kernel_driver(struct libusb_device_handle *handle,
1452 int interface) 1476 int interface)
1453 { 1477 {
1454 int fd = _device_handle_priv(handle)->fd; 1478 int fd = _device_handle_priv(handle)->fd;
1455 struct usbfs_ioctl command; 1479 struct usbfs_ioctl command;
1480 struct usbfs_getdriver getdrv;
1456 int r; 1481 int r;
1457 1482
1458 command.ifno = interface; 1483 command.ifno = interface;
1459 command.ioctl_code = IOCTL_USBFS_DISCONNECT; 1484 command.ioctl_code = IOCTL_USBFS_DISCONNECT;
1460 command.data = NULL; 1485 command.data = NULL;
1461 1486
1487 getdrv.interface = interface;
1488 r = ioctl(fd, IOCTL_USBFS_GETDRIVER, &getdrv);
1489 if (r == 0 && strcmp(getdrv.driver, "usbfs") == 0)
1490 return LIBUSB_ERROR_NOT_FOUND;
1491
1462 r = ioctl(fd, IOCTL_USBFS_IOCTL, &command); 1492 r = ioctl(fd, IOCTL_USBFS_IOCTL, &command);
1463 if (r) { 1493 if (r) {
1464 if (errno == ENODATA) 1494 if (errno == ENODATA)
1465 return LIBUSB_ERROR_NOT_FOUND; 1495 return LIBUSB_ERROR_NOT_FOUND;
1466 else if (errno == EINVAL) 1496 else if (errno == EINVAL)
1467 return LIBUSB_ERROR_INVALID_PARAM; 1497 return LIBUSB_ERROR_INVALID_PARAM;
1468 else if (errno == ENODEV) 1498 else if (errno == ENODEV)
1469 return LIBUSB_ERROR_NO_DEVICE; 1499 return LIBUSB_ERROR_NO_DEVICE;
1470 1500
1471 usbi_err(HANDLE_CTX(handle), 1501 usbi_err(HANDLE_CTX(handle),
(...skipping 29 matching lines...) Expand all
1501 usbi_err(HANDLE_CTX(handle), 1531 usbi_err(HANDLE_CTX(handle),
1502 "attach failed error %d errno %d", r, errno); 1532 "attach failed error %d errno %d", r, errno);
1503 return LIBUSB_ERROR_OTHER; 1533 return LIBUSB_ERROR_OTHER;
1504 } else if (r == 0) { 1534 } else if (r == 0) {
1505 return LIBUSB_ERROR_NOT_FOUND; 1535 return LIBUSB_ERROR_NOT_FOUND;
1506 } 1536 }
1507 1537
1508 return 0; 1538 return 0;
1509 } 1539 }
1510 1540
1541 static int detach_kernel_driver_and_claim(struct libusb_device_handle *handle,
1542 int interface)
1543 {
1544 struct usbfs_disconnect_claim dc;
1545 int r, fd = _device_handle_priv(handle)->fd;
1546
1547 dc.interface = interface;
1548 strcpy(dc.driver, "usbfs");
1549 dc.flags = USBFS_DISCONNECT_CLAIM_EXCEPT_DRIVER;
1550 r = ioctl(fd, IOCTL_USBFS_DISCONNECT_CLAIM, &dc);
1551 if (r == 0 || (r != 0 && errno != ENOTTY)) {
1552 if (r == 0)
1553 return 0;
1554
1555 switch (errno) {
1556 case EBUSY:
1557 return LIBUSB_ERROR_BUSY;
1558 case EINVAL:
1559 return LIBUSB_ERROR_INVALID_PARAM;
1560 case ENODEV:
1561 return LIBUSB_ERROR_NO_DEVICE;
1562 }
1563 usbi_err(HANDLE_CTX(handle),
1564 "disconnect-and-claim failed errno %d", errno);
1565 return LIBUSB_ERROR_OTHER;
1566 }
1567
1568 /* Fallback code for kernels which don't support the
1569 disconnect-and-claim ioctl */
1570 r = op_detach_kernel_driver(handle, interface);
1571 if (r != 0 && r != LIBUSB_ERROR_NOT_FOUND)
1572 return r;
1573
1574 return claim_interface(handle, interface);
1575 }
1576
1577 static int op_claim_interface(struct libusb_device_handle *handle, int iface)
1578 {
1579 if (handle->auto_detach_kernel_driver)
1580 return detach_kernel_driver_and_claim(handle, iface);
1581 else
1582 return claim_interface(handle, iface);
1583 }
1584
1585 static int op_release_interface(struct libusb_device_handle *handle, int iface)
1586 {
1587 int r;
1588
1589 r = release_interface(handle, iface);
1590 if (r)
1591 return r;
1592
1593 if (handle->auto_detach_kernel_driver)
1594 op_attach_kernel_driver(handle, iface);
1595
1596 return 0;
1597 }
1598
1511 static void op_destroy_device(struct libusb_device *dev) 1599 static void op_destroy_device(struct libusb_device *dev)
1512 { 1600 {
1513 struct linux_device_priv *priv = _device_priv(dev); 1601 struct linux_device_priv *priv = _device_priv(dev);
1514 » if (!sysfs_has_descriptors) { 1602 » if (priv->descriptors)
1515 » » if (priv->dev_descriptor) 1603 » » free(priv->descriptors);
1516 » » » free(priv->dev_descriptor);
1517 » » if (priv->config_descriptor)
1518 » » » free(priv->config_descriptor);
1519 » }
1520 if (priv->sysfs_dir) 1604 if (priv->sysfs_dir)
1521 free(priv->sysfs_dir); 1605 free(priv->sysfs_dir);
1522 } 1606 }
1523 1607
1524 /* URBs are discarded in reverse order of submission to avoid races. */ 1608 /* URBs are discarded in reverse order of submission to avoid races. */
1525 static int discard_urbs(struct usbi_transfer *itransfer, int first, int last_plu s_one) 1609 static int discard_urbs(struct usbi_transfer *itransfer, int first, int last_plu s_one)
1526 { 1610 {
1527 struct libusb_transfer *transfer = 1611 struct libusb_transfer *transfer =
1528 USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer); 1612 USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
1529 struct linux_transfer_priv *tpriv = 1613 struct linux_transfer_priv *tpriv =
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
1576 unsigned char urb_type) 1660 unsigned char urb_type)
1577 { 1661 {
1578 struct libusb_transfer *transfer = 1662 struct libusb_transfer *transfer =
1579 USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer); 1663 USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
1580 struct linux_transfer_priv *tpriv = usbi_transfer_get_os_priv(itransfer) ; 1664 struct linux_transfer_priv *tpriv = usbi_transfer_get_os_priv(itransfer) ;
1581 struct linux_device_handle_priv *dpriv = 1665 struct linux_device_handle_priv *dpriv =
1582 _device_handle_priv(transfer->dev_handle); 1666 _device_handle_priv(transfer->dev_handle);
1583 struct usbfs_urb *urbs; 1667 struct usbfs_urb *urbs;
1584 int is_out = (transfer->endpoint & LIBUSB_ENDPOINT_DIR_MASK) 1668 int is_out = (transfer->endpoint & LIBUSB_ENDPOINT_DIR_MASK)
1585 == LIBUSB_ENDPOINT_OUT; 1669 == LIBUSB_ENDPOINT_OUT;
1670 int bulk_buffer_len, use_bulk_continuation;
1586 int r; 1671 int r;
1587 int i; 1672 int i;
1588 size_t alloc_size; 1673 size_t alloc_size;
1589 1674
1590 if (tpriv->urbs) 1675 if (tpriv->urbs)
1591 return LIBUSB_ERROR_BUSY; 1676 return LIBUSB_ERROR_BUSY;
1592 1677
1593 » if (is_out && transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET && 1678 » if (is_out && (transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET) &&
1594 » !supports_flag_zero_packet) 1679 » » » !(dpriv->caps & USBFS_CAP_ZERO_PACKET))
1595 return LIBUSB_ERROR_NOT_SUPPORTED; 1680 return LIBUSB_ERROR_NOT_SUPPORTED;
1596 1681
1597 » /* usbfs places a 16kb limit on bulk URBs. we divide up larger requests 1682 » /*
1598 » * into smaller units to meet such restriction, then fire off all the 1683 » * Older versions of usbfs place a 16kb limit on bulk URBs. We work
1599 » * units at once. it would be simpler if we just fired one unit at a tim e, 1684 » * around this by splitting large transfers into 16k blocks, and then
1600 » * but there is a big performance gain through doing it this way. */ 1685 » * submit all urbs at once. it would be simpler to submit one urb at
1601 » int num_urbs = transfer->length / MAX_BULK_BUFFER_LENGTH; 1686 » * a time, but there is a big performance gain doing it this way.
1687 » *
1688 » * Newer versions lift the 16k limit (USBFS_CAP_NO_PACKET_SIZE_LIM),
1689 » * using arbritary large transfers can still be a bad idea though, as
1690 » * the kernel needs to allocate physical contiguous memory for this,
1691 » * which may fail for large buffers.
1692 » *
1693 » * The kernel solves this problem by splitting the transfer into
1694 » * blocks itself when the host-controller is scatter-gather capable
1695 » * (USBFS_CAP_BULK_SCATTER_GATHER), which most controllers are.
1696 » *
1697 » * Last, there is the issue of short-transfers when splitting, for
1698 » * short split-transfers to work reliable USBFS_CAP_BULK_CONTINUATION
1699 » * is needed, but this is not always available.
1700 » */
1701 » if (dpriv->caps & USBFS_CAP_BULK_SCATTER_GATHER) {
1702 » » /* Good! Just submit everything in one go */
1703 » » bulk_buffer_len = transfer->length ? transfer->length : 1;
1704 » » use_bulk_continuation = 0;
1705 » } else if (dpriv->caps & USBFS_CAP_BULK_CONTINUATION) {
1706 » » /* Split the transfers and use bulk-continuation to
1707 » » avoid issues with short-transfers */
1708 » » bulk_buffer_len = MAX_BULK_BUFFER_LENGTH;
1709 » » use_bulk_continuation = 1;
1710 » } else if (dpriv->caps & USBFS_CAP_NO_PACKET_SIZE_LIM) {
1711 » » /* Don't split, assume the kernel can alloc the buffer
1712 » » (otherwise the submit will fail with -ENOMEM) */
1713 » » bulk_buffer_len = transfer->length ? transfer->length : 1;
1714 » » use_bulk_continuation = 0;
1715 » } else {
1716 » » /* Bad, splitting without bulk-continuation, short transfers
1717 » » which end before the last urb will not work reliable! */
1718 » » /* Note we don't warn here as this is "normal" on kernels <
1719 » » 2.6.32 and not a problem for most applications */
1720 » » bulk_buffer_len = MAX_BULK_BUFFER_LENGTH;
1721 » » use_bulk_continuation = 0;
1722 » }
1723
1724 » int num_urbs = transfer->length / bulk_buffer_len;
1602 int last_urb_partial = 0; 1725 int last_urb_partial = 0;
1603 1726
1604 if (transfer->length == 0) { 1727 if (transfer->length == 0) {
1605 num_urbs = 1; 1728 num_urbs = 1;
1606 » } else if ((transfer->length % MAX_BULK_BUFFER_LENGTH) > 0) { 1729 » } else if ((transfer->length % bulk_buffer_len) > 0) {
1607 last_urb_partial = 1; 1730 last_urb_partial = 1;
1608 num_urbs++; 1731 num_urbs++;
1609 } 1732 }
1610 usbi_dbg("need %d urbs for new transfer with length %d", num_urbs, 1733 usbi_dbg("need %d urbs for new transfer with length %d", num_urbs,
1611 transfer->length); 1734 transfer->length);
1612 alloc_size = num_urbs * sizeof(struct usbfs_urb); 1735 alloc_size = num_urbs * sizeof(struct usbfs_urb);
1613 » urbs = malloc(alloc_size); 1736 » urbs = calloc(1, alloc_size);
1614 if (!urbs) 1737 if (!urbs)
1615 return LIBUSB_ERROR_NO_MEM; 1738 return LIBUSB_ERROR_NO_MEM;
1616 memset(urbs, 0, alloc_size);
1617 tpriv->urbs = urbs; 1739 tpriv->urbs = urbs;
1618 tpriv->num_urbs = num_urbs; 1740 tpriv->num_urbs = num_urbs;
1619 tpriv->num_retired = 0; 1741 tpriv->num_retired = 0;
1620 tpriv->reap_action = NORMAL; 1742 tpriv->reap_action = NORMAL;
1621 tpriv->reap_status = LIBUSB_TRANSFER_COMPLETED; 1743 tpriv->reap_status = LIBUSB_TRANSFER_COMPLETED;
1622 1744
1623 for (i = 0; i < num_urbs; i++) { 1745 for (i = 0; i < num_urbs; i++) {
1624 struct usbfs_urb *urb = &urbs[i]; 1746 struct usbfs_urb *urb = &urbs[i];
1625 urb->usercontext = itransfer; 1747 urb->usercontext = itransfer;
1626 urb->type = urb_type; 1748 urb->type = urb_type;
1627 urb->endpoint = transfer->endpoint; 1749 urb->endpoint = transfer->endpoint;
1628 » » urb->buffer = transfer->buffer + (i * MAX_BULK_BUFFER_LENGTH); 1750 » » urb->buffer = transfer->buffer + (i * bulk_buffer_len);
1629 » » if (supports_flag_bulk_continuation && !is_out) 1751 » » /* don't set the short not ok flag for the last URB */
1752 » » if (use_bulk_continuation && !is_out && (i < num_urbs - 1))
1630 urb->flags = USBFS_URB_SHORT_NOT_OK; 1753 urb->flags = USBFS_URB_SHORT_NOT_OK;
1631 if (i == num_urbs - 1 && last_urb_partial) 1754 if (i == num_urbs - 1 && last_urb_partial)
1632 » » » urb->buffer_length = transfer->length % MAX_BULK_BUFFER_ LENGTH; 1755 » » » urb->buffer_length = transfer->length % bulk_buffer_len;
1633 else if (transfer->length == 0) 1756 else if (transfer->length == 0)
1634 urb->buffer_length = 0; 1757 urb->buffer_length = 0;
1635 else 1758 else
1636 » » » urb->buffer_length = MAX_BULK_BUFFER_LENGTH; 1759 » » » urb->buffer_length = bulk_buffer_len;
1637 1760
1638 » » if (i > 0 && supports_flag_bulk_continuation) 1761 » » if (i > 0 && use_bulk_continuation)
1639 urb->flags |= USBFS_URB_BULK_CONTINUATION; 1762 urb->flags |= USBFS_URB_BULK_CONTINUATION;
1640 1763
1641 /* we have already checked that the flag is supported */ 1764 /* we have already checked that the flag is supported */
1642 if (is_out && i == num_urbs - 1 && 1765 if (is_out && i == num_urbs - 1 &&
1643 transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET) 1766 transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET)
1644 urb->flags |= USBFS_URB_ZERO_PACKET; 1767 urb->flags |= USBFS_URB_ZERO_PACKET;
1645 1768
1646 r = ioctl(dpriv->fd, IOCTL_USBFS_SUBMITURB, urb); 1769 r = ioctl(dpriv->fd, IOCTL_USBFS_SUBMITURB, urb);
1647 if (r < 0) { 1770 if (r < 0) {
1648 if (errno == ENODEV) { 1771 if (errno == ENODEV) {
(...skipping 11 matching lines...) Expand all
1660 free(urbs); 1783 free(urbs);
1661 tpriv->urbs = NULL; 1784 tpriv->urbs = NULL;
1662 return r; 1785 return r;
1663 } 1786 }
1664 1787
1665 /* if it's not the first URB that failed, the situation is a bit 1788 /* if it's not the first URB that failed, the situation is a bit
1666 * tricky. we may need to discard all previous URBs. the re are 1789 * tricky. we may need to discard all previous URBs. the re are
1667 * complications: 1790 * complications:
1668 * - discarding is asynchronous - discarded urbs will b e reaped 1791 * - discarding is asynchronous - discarded urbs will b e reaped
1669 * later. the user must not have freed the transfer w hen the 1792 * later. the user must not have freed the transfer w hen the
1670 » » » * discarded URBs are reaped, otherwise libusb will b e using 1793 » » » * discarded URBs are reaped, otherwise libusbx will be using
1671 * freed memory. 1794 * freed memory.
1672 * - the earlier URBs may have completed successfully a nd we do 1795 * - the earlier URBs may have completed successfully a nd we do
1673 * not want to throw away any data. 1796 * not want to throw away any data.
1674 * - this URB failing may be no error; EREMOTEIO means that 1797 * - this URB failing may be no error; EREMOTEIO means that
1675 * this transfer simply didn't need all the URBs we s ubmitted 1798 * this transfer simply didn't need all the URBs we s ubmitted
1676 * so, we report that the transfer was submitted success fully and 1799 * so, we report that the transfer was submitted success fully and
1677 * in case of error we discard all previous URBs. later when 1800 * in case of error we discard all previous URBs. later when
1678 * the final reap completes we can report error to the u ser, 1801 * the final reap completes we can report error to the u ser,
1679 * or success if an earlier URB was completed successful ly. 1802 * or success if an earlier URB was completed successful ly.
1680 */ 1803 */
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
1715 int packet_offset = 0; 1838 int packet_offset = 0;
1716 unsigned int packet_len; 1839 unsigned int packet_len;
1717 unsigned char *urb_buffer = transfer->buffer; 1840 unsigned char *urb_buffer = transfer->buffer;
1718 1841
1719 if (tpriv->iso_urbs) 1842 if (tpriv->iso_urbs)
1720 return LIBUSB_ERROR_BUSY; 1843 return LIBUSB_ERROR_BUSY;
1721 1844
1722 /* usbfs places a 32kb limit on iso URBs. we divide up larger requests 1845 /* usbfs places a 32kb limit on iso URBs. we divide up larger requests
1723 * into smaller units to meet such restriction, then fire off all the 1846 * into smaller units to meet such restriction, then fire off all the
1724 * units at once. it would be simpler if we just fired one unit at a tim e, 1847 * units at once. it would be simpler if we just fired one unit at a tim e,
1725 » * but there is a big performance gain through doing it this way. */ 1848 » * but there is a big performance gain through doing it this way.
1849 » *
1850 » * Newer kernels lift the 32k limit (USBFS_CAP_NO_PACKET_SIZE_LIM),
1851 » * using arbritary large transfers is still be a bad idea though, as
1852 » * the kernel needs to allocate physical contiguous memory for this,
1853 » * which may fail for large buffers.
1854 » */
1726 1855
1727 /* calculate how many URBs we need */ 1856 /* calculate how many URBs we need */
1728 for (i = 0; i < num_packets; i++) { 1857 for (i = 0; i < num_packets; i++) {
1729 » » int space_remaining = MAX_ISO_BUFFER_LENGTH - this_urb_len; 1858 » » unsigned int space_remaining = MAX_ISO_BUFFER_LENGTH - this_urb_ len;
1730 packet_len = transfer->iso_packet_desc[i].length; 1859 packet_len = transfer->iso_packet_desc[i].length;
1731 1860
1732 if (packet_len > space_remaining) { 1861 if (packet_len > space_remaining) {
1733 num_urbs++; 1862 num_urbs++;
1734 this_urb_len = packet_len; 1863 this_urb_len = packet_len;
1735 } else { 1864 } else {
1736 this_urb_len += packet_len; 1865 this_urb_len += packet_len;
1737 } 1866 }
1738 } 1867 }
1739 usbi_dbg("need %d 32k URBs for transfer", num_urbs); 1868 usbi_dbg("need %d 32k URBs for transfer", num_urbs);
1740 1869
1741 alloc_size = num_urbs * sizeof(*urbs); 1870 alloc_size = num_urbs * sizeof(*urbs);
1742 » urbs = malloc(alloc_size); 1871 » urbs = calloc(1, alloc_size);
1743 if (!urbs) 1872 if (!urbs)
1744 return LIBUSB_ERROR_NO_MEM; 1873 return LIBUSB_ERROR_NO_MEM;
1745 memset(urbs, 0, alloc_size);
1746 1874
1747 tpriv->iso_urbs = urbs; 1875 tpriv->iso_urbs = urbs;
1748 tpriv->num_urbs = num_urbs; 1876 tpriv->num_urbs = num_urbs;
1749 tpriv->num_retired = 0; 1877 tpriv->num_retired = 0;
1750 tpriv->reap_action = NORMAL; 1878 tpriv->reap_action = NORMAL;
1751 tpriv->iso_packet_offset = 0; 1879 tpriv->iso_packet_offset = 0;
1752 1880
1753 /* allocate + initialize each URB with the correct number of packets */ 1881 /* allocate + initialize each URB with the correct number of packets */
1754 for (i = 0; i < num_urbs; i++) { 1882 for (i = 0; i < num_urbs; i++) {
1755 struct usbfs_urb *urb; 1883 struct usbfs_urb *urb;
1756 » » int space_remaining_in_urb = MAX_ISO_BUFFER_LENGTH; 1884 » » unsigned int space_remaining_in_urb = MAX_ISO_BUFFER_LENGTH;
1757 int urb_packet_offset = 0; 1885 int urb_packet_offset = 0;
1758 unsigned char *urb_buffer_orig = urb_buffer; 1886 unsigned char *urb_buffer_orig = urb_buffer;
1759 int j; 1887 int j;
1760 int k; 1888 int k;
1761 1889
1762 /* swallow up all the packets we can fit into this URB */ 1890 /* swallow up all the packets we can fit into this URB */
1763 while (packet_offset < transfer->num_iso_packets) { 1891 while (packet_offset < transfer->num_iso_packets) {
1764 packet_len = transfer->iso_packet_desc[packet_offset].le ngth; 1892 packet_len = transfer->iso_packet_desc[packet_offset].le ngth;
1765 if (packet_len <= space_remaining_in_urb) { 1893 if (packet_len <= space_remaining_in_urb) {
1766 /* throw it in */ 1894 /* throw it in */
1767 urb_packet_offset++; 1895 urb_packet_offset++;
1768 packet_offset++; 1896 packet_offset++;
1769 space_remaining_in_urb -= packet_len; 1897 space_remaining_in_urb -= packet_len;
1770 urb_buffer += packet_len; 1898 urb_buffer += packet_len;
1771 } else { 1899 } else {
1772 /* it can't fit, save it for the next URB */ 1900 /* it can't fit, save it for the next URB */
1773 break; 1901 break;
1774 } 1902 }
1775 } 1903 }
1776 1904
1777 alloc_size = sizeof(*urb) 1905 alloc_size = sizeof(*urb)
1778 + (urb_packet_offset * sizeof(struct usbfs_iso_packet_de sc)); 1906 + (urb_packet_offset * sizeof(struct usbfs_iso_packet_de sc));
1779 » » urb = malloc(alloc_size); 1907 » » urb = calloc(1, alloc_size);
1780 if (!urb) { 1908 if (!urb) {
1781 free_iso_urbs(tpriv); 1909 free_iso_urbs(tpriv);
1782 return LIBUSB_ERROR_NO_MEM; 1910 return LIBUSB_ERROR_NO_MEM;
1783 } 1911 }
1784 memset(urb, 0, alloc_size);
1785 urbs[i] = urb; 1912 urbs[i] = urb;
1786 1913
1787 /* populate packet lengths */ 1914 /* populate packet lengths */
1788 for (j = 0, k = packet_offset - urb_packet_offset; 1915 for (j = 0, k = packet_offset - urb_packet_offset;
1789 k < packet_offset; k++, j++) { 1916 k < packet_offset; k++, j++) {
1790 packet_len = transfer->iso_packet_desc[k].length; 1917 packet_len = transfer->iso_packet_desc[k].length;
1791 urb->iso_frame_desc[j].length = packet_len; 1918 urb->iso_frame_desc[j].length = packet_len;
1792 } 1919 }
1793 1920
1794 urb->usercontext = itransfer; 1921 urb->usercontext = itransfer;
(...skipping 23 matching lines...) Expand all
1818 usbi_dbg("first URB failed, easy peasy"); 1945 usbi_dbg("first URB failed, easy peasy");
1819 free_iso_urbs(tpriv); 1946 free_iso_urbs(tpriv);
1820 return r; 1947 return r;
1821 } 1948 }
1822 1949
1823 /* if it's not the first URB that failed, the situation is a bit 1950 /* if it's not the first URB that failed, the situation is a bit
1824 * tricky. we must discard all previous URBs. there are 1951 * tricky. we must discard all previous URBs. there are
1825 * complications: 1952 * complications:
1826 * - discarding is asynchronous - discarded urbs will b e reaped 1953 * - discarding is asynchronous - discarded urbs will b e reaped
1827 * later. the user must not have freed the transfer w hen the 1954 * later. the user must not have freed the transfer w hen the
1828 » » » * discarded URBs are reaped, otherwise libusb will b e using 1955 » » » * discarded URBs are reaped, otherwise libusbx will be using
1829 * freed memory. 1956 * freed memory.
1830 * - the earlier URBs may have completed successfully a nd we do 1957 * - the earlier URBs may have completed successfully a nd we do
1831 * not want to throw away any data. 1958 * not want to throw away any data.
1832 * so, in this case we discard all the previous URBs BUT we report 1959 * so, in this case we discard all the previous URBs BUT we report
1833 * that the transfer was submitted successfully. then la ter when 1960 * that the transfer was submitted successfully. then la ter when
1834 * the final discard completes we can report error to th e user. 1961 * the final discard completes we can report error to th e user.
1835 */ 1962 */
1836 tpriv->reap_action = SUBMIT_FAILED; 1963 tpriv->reap_action = SUBMIT_FAILED;
1837 1964
1838 /* The URBs we haven't submitted yet we count as already 1965 /* The URBs we haven't submitted yet we count as already
(...skipping 19 matching lines...) Expand all
1858 _device_handle_priv(transfer->dev_handle); 1985 _device_handle_priv(transfer->dev_handle);
1859 struct usbfs_urb *urb; 1986 struct usbfs_urb *urb;
1860 int r; 1987 int r;
1861 1988
1862 if (tpriv->urbs) 1989 if (tpriv->urbs)
1863 return LIBUSB_ERROR_BUSY; 1990 return LIBUSB_ERROR_BUSY;
1864 1991
1865 if (transfer->length - LIBUSB_CONTROL_SETUP_SIZE > MAX_CTRL_BUFFER_LENGT H) 1992 if (transfer->length - LIBUSB_CONTROL_SETUP_SIZE > MAX_CTRL_BUFFER_LENGT H)
1866 return LIBUSB_ERROR_INVALID_PARAM; 1993 return LIBUSB_ERROR_INVALID_PARAM;
1867 1994
1868 » urb = malloc(sizeof(struct usbfs_urb)); 1995 » urb = calloc(1, sizeof(struct usbfs_urb));
1869 if (!urb) 1996 if (!urb)
1870 return LIBUSB_ERROR_NO_MEM; 1997 return LIBUSB_ERROR_NO_MEM;
1871 memset(urb, 0, sizeof(struct usbfs_urb));
1872 tpriv->urbs = urb; 1998 tpriv->urbs = urb;
1873 tpriv->num_urbs = 1; 1999 tpriv->num_urbs = 1;
1874 tpriv->reap_action = NORMAL; 2000 tpriv->reap_action = NORMAL;
1875 2001
1876 urb->usercontext = itransfer; 2002 urb->usercontext = itransfer;
1877 urb->type = USBFS_URB_TYPE_CONTROL; 2003 urb->type = USBFS_URB_TYPE_CONTROL;
1878 urb->endpoint = transfer->endpoint; 2004 urb->endpoint = transfer->endpoint;
1879 urb->buffer = transfer->buffer; 2005 urb->buffer = transfer->buffer;
1880 urb->buffer_length = transfer->length; 2006 urb->buffer_length = transfer->length;
1881 2007
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
1994 * 1. while the kernel is cancelling all the packets that make u p an 2120 * 1. while the kernel is cancelling all the packets that make u p an
1995 * URB, a few of them might complete. so we get back a succes sful 2121 * URB, a few of them might complete. so we get back a succes sful
1996 * cancellation *and* some data. 2122 * cancellation *and* some data.
1997 * 2. we receive a short URB which marks the early completion co ndition, 2123 * 2. we receive a short URB which marks the early completion co ndition,
1998 * so we start cancelling the remaining URBs. however, we're too 2124 * so we start cancelling the remaining URBs. however, we're too
1999 * slow and another URB completes (or at least completes part ially). 2125 * slow and another URB completes (or at least completes part ially).
2000 * (this can't happen since we always use BULK_CONTINUATION.) 2126 * (this can't happen since we always use BULK_CONTINUATION.)
2001 * 2127 *
2002 * When this happens, our objectives are not to lose any "surplu s" data, 2128 * When this happens, our objectives are not to lose any "surplu s" data,
2003 * and also to stick it at the end of the previously-received da ta 2129 * and also to stick it at the end of the previously-received da ta
2004 » » * (closing any holes), so that libusb reports the total amount of 2130 » » * (closing any holes), so that libusbx reports the total amount of
2005 * transferred data and presents it in a contiguous chunk. 2131 * transferred data and presents it in a contiguous chunk.
2006 */ 2132 */
2007 if (urb->actual_length > 0) { 2133 if (urb->actual_length > 0) {
2008 unsigned char *target = transfer->buffer + itransfer->tr ansferred; 2134 unsigned char *target = transfer->buffer + itransfer->tr ansferred;
2009 usbi_dbg("received %d bytes of surplus data", urb->actua l_length); 2135 usbi_dbg("received %d bytes of surplus data", urb->actua l_length);
2010 if (urb->buffer != target) { 2136 if (urb->buffer != target) {
2011 usbi_dbg("moving surplus data from offset %d to offset %d", 2137 usbi_dbg("moving surplus data from offset %d to offset %d",
2012 (unsigned char *) urb->buffer - transfer ->buffer, 2138 (unsigned char *) urb->buffer - transfer ->buffer,
2013 target - transfer->buffer); 2139 target - transfer->buffer);
2014 memmove(target, urb->buffer, urb->actual_length) ; 2140 memmove(target, urb->buffer, urb->actual_length) ;
(...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after
2328 usbi_err(HANDLE_CTX(handle), "unrecognised endpoint type %x", 2454 usbi_err(HANDLE_CTX(handle), "unrecognised endpoint type %x",
2329 transfer->type); 2455 transfer->type);
2330 return LIBUSB_ERROR_OTHER; 2456 return LIBUSB_ERROR_OTHER;
2331 } 2457 }
2332 } 2458 }
2333 2459
2334 static int op_handle_events(struct libusb_context *ctx, 2460 static int op_handle_events(struct libusb_context *ctx,
2335 struct pollfd *fds, POLL_NFDS_TYPE nfds, int num_ready) 2461 struct pollfd *fds, POLL_NFDS_TYPE nfds, int num_ready)
2336 { 2462 {
2337 int r; 2463 int r;
2338 » int i = 0; 2464 » unsigned int i = 0;
2339 2465
2340 usbi_mutex_lock(&ctx->open_devs_lock); 2466 usbi_mutex_lock(&ctx->open_devs_lock);
2341 for (i = 0; i < nfds && num_ready > 0; i++) { 2467 for (i = 0; i < nfds && num_ready > 0; i++) {
2342 struct pollfd *pollfd = &fds[i]; 2468 struct pollfd *pollfd = &fds[i];
2343 struct libusb_device_handle *handle; 2469 struct libusb_device_handle *handle;
2344 struct linux_device_handle_priv *hpriv = NULL; 2470 struct linux_device_handle_priv *hpriv = NULL;
2345 2471
2346 if (!pollfd->revents) 2472 if (!pollfd->revents)
2347 continue; 2473 continue;
2348 2474
2349 num_ready--; 2475 num_ready--;
2350 list_for_each_entry(handle, &ctx->open_devs, list, struct libusb _device_handle) { 2476 list_for_each_entry(handle, &ctx->open_devs, list, struct libusb _device_handle) {
2351 hpriv = _device_handle_priv(handle); 2477 hpriv = _device_handle_priv(handle);
2352 if (hpriv->fd == pollfd->fd) 2478 if (hpriv->fd == pollfd->fd)
2353 break; 2479 break;
2354 } 2480 }
2355 2481
2356 if (pollfd->revents & POLLERR) { 2482 if (pollfd->revents & POLLERR) {
2357 usbi_remove_pollfd(HANDLE_CTX(handle), hpriv->fd); 2483 usbi_remove_pollfd(HANDLE_CTX(handle), hpriv->fd);
2358 usbi_handle_disconnect(handle); 2484 usbi_handle_disconnect(handle);
2359 continue; 2485 continue;
2360 } 2486 }
2361 2487
2362 » » r = reap_for_handle(handle); 2488 » » do {
2489 » » » r = reap_for_handle(handle);
2490 » » } while (r == 0);
2363 if (r == 1 || r == LIBUSB_ERROR_NO_DEVICE) 2491 if (r == 1 || r == LIBUSB_ERROR_NO_DEVICE)
2364 continue; 2492 continue;
2365 else if (r < 0) 2493 else if (r < 0)
2366 goto out; 2494 goto out;
2367 } 2495 }
2368 2496
2369 r = 0; 2497 r = 0;
2370 out: 2498 out:
2371 usbi_mutex_unlock(&ctx->open_devs_lock); 2499 usbi_mutex_unlock(&ctx->open_devs_lock);
2372 return r; 2500 return r;
(...skipping 14 matching lines...) Expand all
2387 #ifdef USBI_TIMERFD_AVAILABLE 2515 #ifdef USBI_TIMERFD_AVAILABLE
2388 static clockid_t op_get_timerfd_clockid(void) 2516 static clockid_t op_get_timerfd_clockid(void)
2389 { 2517 {
2390 return monotonic_clkid; 2518 return monotonic_clkid;
2391 2519
2392 } 2520 }
2393 #endif 2521 #endif
2394 2522
2395 const struct usbi_os_backend linux_usbfs_backend = { 2523 const struct usbi_os_backend linux_usbfs_backend = {
2396 .name = "Linux usbfs", 2524 .name = "Linux usbfs",
2525 .caps = USBI_CAP_HAS_HID_ACCESS|USBI_CAP_SUPPORTS_DETACH_KERNEL_DRIVER,
2397 .init = op_init, 2526 .init = op_init,
2398 » .exit = NULL, 2527 » .exit = op_exit,
2399 » .get_device_list = op_get_device_list, 2528 » .get_device_list = NULL,
2529 » .hotplug_poll = op_hotplug_poll,
2400 .get_device_descriptor = op_get_device_descriptor, 2530 .get_device_descriptor = op_get_device_descriptor,
2401 .get_active_config_descriptor = op_get_active_config_descriptor, 2531 .get_active_config_descriptor = op_get_active_config_descriptor,
2402 .get_config_descriptor = op_get_config_descriptor, 2532 .get_config_descriptor = op_get_config_descriptor,
2533 .get_config_descriptor_by_value = op_get_config_descriptor_by_value,
2403 2534
2404 .open = op_open, 2535 .open = op_open,
2405 .close = op_close, 2536 .close = op_close,
2406 .get_configuration = op_get_configuration, 2537 .get_configuration = op_get_configuration,
2407 .set_configuration = op_set_configuration, 2538 .set_configuration = op_set_configuration,
2408 .claim_interface = op_claim_interface, 2539 .claim_interface = op_claim_interface,
2409 .release_interface = op_release_interface, 2540 .release_interface = op_release_interface,
2410 2541
2411 .set_interface_altsetting = op_set_interface, 2542 .set_interface_altsetting = op_set_interface,
2412 .clear_halt = op_clear_halt, 2543 .clear_halt = op_clear_halt,
(...skipping 15 matching lines...) Expand all
2428 2559
2429 #ifdef USBI_TIMERFD_AVAILABLE 2560 #ifdef USBI_TIMERFD_AVAILABLE
2430 .get_timerfd_clockid = op_get_timerfd_clockid, 2561 .get_timerfd_clockid = op_get_timerfd_clockid,
2431 #endif 2562 #endif
2432 2563
2433 .device_priv_size = sizeof(struct linux_device_priv), 2564 .device_priv_size = sizeof(struct linux_device_priv),
2434 .device_handle_priv_size = sizeof(struct linux_device_handle_priv), 2565 .device_handle_priv_size = sizeof(struct linux_device_handle_priv),
2435 .transfer_priv_size = sizeof(struct linux_transfer_priv), 2566 .transfer_priv_size = sizeof(struct linux_transfer_priv),
2436 .add_iso_packet_size = 0, 2567 .add_iso_packet_size = 0,
2437 }; 2568 };
2438
OLDNEW
« no previous file with comments | « third_party/libusb/src/libusb/os/linux_usbfs.h ('k') | third_party/libusb/src/libusb/os/openbsd_usb.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698