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