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