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