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