| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Linux usbfs backend for libusb | |
| 3 * Copyright (C) 2007-2009 Daniel Drake <dsd@gentoo.org> | |
| 4 * Copyright (c) 2001 Johannes Erdfelt <johannes@erdfelt.com> | |
| 5 * | |
| 6 * This library is free software; you can redistribute it and/or | |
| 7 * modify it under the terms of the GNU Lesser General Public | |
| 8 * License as published by the Free Software Foundation; either | |
| 9 * version 2.1 of the License, or (at your option) any later version. | |
| 10 * | |
| 11 * This library is distributed in the hope that it will be useful, | |
| 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 14 * Lesser General Public License for more details. | |
| 15 * | |
| 16 * You should have received a copy of the GNU Lesser General Public | |
| 17 * License along with this library; if not, write to the Free Software | |
| 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
| 19 */ | |
| 20 | |
| 21 #include <config.h> | |
| 22 #include <ctype.h> | |
| 23 #include <dirent.h> | |
| 24 #include <errno.h> | |
| 25 #include <fcntl.h> | |
| 26 #include <poll.h> | |
| 27 #include <stdio.h> | |
| 28 #include <stdlib.h> | |
| 29 #include <string.h> | |
| 30 #include <sys/ioctl.h> | |
| 31 #include <sys/stat.h> | |
| 32 #include <sys/types.h> | |
| 33 #include <sys/utsname.h> | |
| 34 #include <unistd.h> | |
| 35 | |
| 36 #include "libusb.h" | |
| 37 #include "libusbi.h" | |
| 38 #include "linux_usbfs.h" | |
| 39 | |
| 40 /* sysfs vs usbfs: | |
| 41 * opening a usbfs node causes the device to be resumed, so we attempt to | |
| 42 * avoid this during enumeration. | |
| 43 * | |
| 44 * sysfs allows us to read the kernel's in-memory copies of device descriptors | |
| 45 * and so forth, avoiding the need to open the device: | |
| 46 * - The binary "descriptors" file was added in 2.6.23. | |
| 47 * - The "busnum" file was added in 2.6.22 | |
| 48 * - The "devnum" file has been present since pre-2.6.18 | |
| 49 * - the "bConfigurationValue" file has been present since pre-2.6.18 | |
| 50 * | |
| 51 * If we have bConfigurationValue, busnum, and devnum, then we can determine | |
| 52 * the active configuration without having to open the usbfs node in RDWR mode. | |
| 53 * We assume this is the case if we see the busnum file (indicates 2.6.22+). | |
| 54 * The busnum file is important as that is the only way we can relate sysfs | |
| 55 * devices to usbfs nodes. | |
| 56 * | |
| 57 * If we also have descriptors, we can obtain the device descriptor and active | |
| 58 * configuration without touching usbfs at all. | |
| 59 * | |
| 60 * The descriptors file originally only contained the active configuration | |
| 61 * descriptor alongside the device descriptor, but all configurations are | |
| 62 * included as of Linux 2.6.26. | |
| 63 */ | |
| 64 | |
| 65 /* endianness for multi-byte fields: | |
| 66 * | |
| 67 * Descriptors exposed by usbfs have the multi-byte fields in the device | |
| 68 * descriptor as host endian. Multi-byte fields in the other descriptors are | |
| 69 * bus-endian. The kernel documentation says otherwise, but it is wrong. | |
| 70 */ | |
| 71 | |
| 72 static const char *usbfs_path = NULL; | |
| 73 | |
| 74 /* Linux 2.6.32 adds support for a bulk continuation URB flag. this basically | |
| 75 * allows us to mark URBs as being part of a specific logical transfer when | |
| 76 * we submit them to the kernel. then, on any error except a cancellation, all | |
| 77 * URBs within that transfer will be cancelled and no more URBs will be | |
| 78 * accepted for the transfer, meaning that no more data can creep in. | |
| 79 * | |
| 80 * The BULK_CONTINUATION flag must be set on all URBs within a bulk transfer | |
| 81 * (in either direction) except the first. | |
| 82 * For IN transfers, we must also set SHORT_NOT_OK on all URBs except the | |
| 83 * last; it means that the kernel should treat a short reply as an error. | |
| 84 * For OUT transfers, SHORT_NOT_OK must not be set. it isn't needed (OUT | |
| 85 * transfers can't be short unless there's already some sort of error), and | |
| 86 * setting this flag is disallowed (a kernel with USB debugging enabled will | |
| 87 * reject such URBs). | |
| 88 */ | |
| 89 static int supports_flag_bulk_continuation = -1; | |
| 90 | |
| 91 /* clock ID for monotonic clock, as not all clock sources are available on all | |
| 92 * systems. appropriate choice made at initialization time. */ | |
| 93 static clockid_t monotonic_clkid = -1; | |
| 94 | |
| 95 /* do we have a busnum to relate devices? this also implies that we can read | |
| 96 * the active configuration through bConfigurationValue */ | |
| 97 static int sysfs_can_relate_devices = 0; | |
| 98 | |
| 99 /* do we have a descriptors file? */ | |
| 100 static int sysfs_has_descriptors = 0; | |
| 101 | |
| 102 struct linux_device_priv { | |
| 103 char *sysfs_dir; | |
| 104 unsigned char *dev_descriptor; | |
| 105 unsigned char *config_descriptor; | |
| 106 }; | |
| 107 | |
| 108 struct linux_device_handle_priv { | |
| 109 int fd; | |
| 110 }; | |
| 111 | |
| 112 enum reap_action { | |
| 113 NORMAL = 0, | |
| 114 /* submission failed after the first URB, so await cancellation/completi
on | |
| 115 * of all the others */ | |
| 116 SUBMIT_FAILED, | |
| 117 | |
| 118 /* cancelled by user or timeout */ | |
| 119 CANCELLED, | |
| 120 | |
| 121 /* completed multi-URB transfer in non-final URB */ | |
| 122 COMPLETED_EARLY, | |
| 123 | |
| 124 /* one or more urbs encountered a low-level error */ | |
| 125 ERROR, | |
| 126 }; | |
| 127 | |
| 128 struct linux_transfer_priv { | |
| 129 union { | |
| 130 struct usbfs_urb *urbs; | |
| 131 struct usbfs_urb **iso_urbs; | |
| 132 }; | |
| 133 | |
| 134 enum reap_action reap_action; | |
| 135 int num_urbs; | |
| 136 unsigned int num_retired; | |
| 137 enum libusb_transfer_status reap_status; | |
| 138 | |
| 139 /* next iso packet in user-supplied transfer to be populated */ | |
| 140 int iso_packet_offset; | |
| 141 }; | |
| 142 | |
| 143 static void _get_usbfs_path(struct libusb_device *dev, char *path) | |
| 144 { | |
| 145 snprintf(path, PATH_MAX, "%s/%03d/%03d", usbfs_path, dev->bus_number, | |
| 146 dev->device_address); | |
| 147 } | |
| 148 | |
| 149 static struct linux_device_priv *_device_priv(struct libusb_device *dev) | |
| 150 { | |
| 151 return (struct linux_device_priv *) dev->os_priv; | |
| 152 } | |
| 153 | |
| 154 static struct linux_device_handle_priv *_device_handle_priv( | |
| 155 struct libusb_device_handle *handle) | |
| 156 { | |
| 157 return (struct linux_device_handle_priv *) handle->os_priv; | |
| 158 } | |
| 159 | |
| 160 static int check_usb_vfs(const char *dirname) | |
| 161 { | |
| 162 DIR *dir; | |
| 163 struct dirent *entry; | |
| 164 int found = 0; | |
| 165 | |
| 166 dir = opendir(dirname); | |
| 167 if (!dir) | |
| 168 return 0; | |
| 169 | |
| 170 while ((entry = readdir(dir)) != NULL) { | |
| 171 if (entry->d_name[0] == '.') | |
| 172 continue; | |
| 173 | |
| 174 /* We assume if we find any files that it must be the right plac
e */ | |
| 175 found = 1; | |
| 176 break; | |
| 177 } | |
| 178 | |
| 179 closedir(dir); | |
| 180 return found; | |
| 181 } | |
| 182 | |
| 183 static const char *find_usbfs_path(void) | |
| 184 { | |
| 185 const char *path = "/dev/bus/usb"; | |
| 186 const char *ret = NULL; | |
| 187 | |
| 188 if (check_usb_vfs(path)) { | |
| 189 ret = path; | |
| 190 } else { | |
| 191 path = "/proc/bus/usb"; | |
| 192 if (check_usb_vfs(path)) | |
| 193 ret = path; | |
| 194 } | |
| 195 | |
| 196 usbi_dbg("found usbfs at %s", ret); | |
| 197 return ret; | |
| 198 } | |
| 199 | |
| 200 /* the monotonic clock is not usable on all systems (e.g. embedded ones often | |
| 201 * seem to lack it). fall back to REALTIME if we have to. */ | |
| 202 static clockid_t find_monotonic_clock(void) | |
| 203 { | |
| 204 struct timespec ts; | |
| 205 int r; | |
| 206 | |
| 207 #ifdef CLOCK_MONOTONIC | |
| 208 /* Linux 2.6.28 adds CLOCK_MONOTONIC_RAW but we don't use it | |
| 209 * because it's not available through timerfd */ | |
| 210 r = clock_gettime(CLOCK_MONOTONIC, &ts); | |
| 211 if (r == 0) | |
| 212 return CLOCK_MONOTONIC; | |
| 213 usbi_dbg("monotonic clock doesn't work, errno %d", errno); | |
| 214 #endif | |
| 215 | |
| 216 return CLOCK_REALTIME; | |
| 217 } | |
| 218 | |
| 219 /* bulk continuation URB flag available from Linux 2.6.32 */ | |
| 220 static int check_flag_bulk_continuation(void) | |
| 221 { | |
| 222 struct utsname uts; | |
| 223 int atoms, major, minor, sublevel; | |
| 224 | |
| 225 if (uname(&uts) < 0) | |
| 226 return -1; | |
| 227 atoms = sscanf(uts.release, "%d.%d.%d", &major, &minor, &sublevel); | |
| 228 if (atoms < 1) | |
| 229 return -1; | |
| 230 | |
| 231 if (major > 2) | |
| 232 return 1; | |
| 233 if (major < 2) | |
| 234 return 0; | |
| 235 | |
| 236 if (atoms < 2) | |
| 237 return 0; | |
| 238 | |
| 239 /* major == 2 */ | |
| 240 if (minor < 6) | |
| 241 return 0; | |
| 242 if (minor > 6) /* Does not exist, just here for correctness */ | |
| 243 return 1; | |
| 244 | |
| 245 /* 2.6.x */ | |
| 246 if (3 == atoms && sublevel >= 32) | |
| 247 return 1; | |
| 248 | |
| 249 return 0; | |
| 250 } | |
| 251 | |
| 252 /* Return 1 if filename exists inside dirname in sysfs. | |
| 253 SYSFS_DEVICE_PATH is assumed to be the beginning of the path. */ | |
| 254 static int sysfs_has_file(const char *dirname, const char *filename) | |
| 255 { | |
| 256 struct stat statbuf; | |
| 257 char path[PATH_MAX]; | |
| 258 int r; | |
| 259 | |
| 260 snprintf(path, PATH_MAX, "%s/%s/%s", SYSFS_DEVICE_PATH, dirname, filenam
e); | |
| 261 r = stat(path, &statbuf); | |
| 262 if (r == 0 && S_ISREG(statbuf.st_mode)) | |
| 263 return 1; | |
| 264 | |
| 265 return 0; | |
| 266 } | |
| 267 | |
| 268 static int op_init(struct libusb_context *ctx) | |
| 269 { | |
| 270 struct stat statbuf; | |
| 271 int r; | |
| 272 | |
| 273 usbfs_path = find_usbfs_path(); | |
| 274 if (!usbfs_path) { | |
| 275 usbi_err(ctx, "could not find usbfs"); | |
| 276 return LIBUSB_ERROR_OTHER; | |
| 277 } | |
| 278 | |
| 279 if (monotonic_clkid == -1) | |
| 280 monotonic_clkid = find_monotonic_clock(); | |
| 281 | |
| 282 if (supports_flag_bulk_continuation == -1) { | |
| 283 supports_flag_bulk_continuation = check_flag_bulk_continuation()
; | |
| 284 if (supports_flag_bulk_continuation == -1) { | |
| 285 usbi_err(ctx, "error checking for bulk continuation supp
ort"); | |
| 286 return LIBUSB_ERROR_OTHER; | |
| 287 } | |
| 288 } | |
| 289 | |
| 290 if (supports_flag_bulk_continuation) | |
| 291 usbi_dbg("bulk continuation flag supported"); | |
| 292 | |
| 293 r = stat(SYSFS_DEVICE_PATH, &statbuf); | |
| 294 if (r == 0 && S_ISDIR(statbuf.st_mode)) { | |
| 295 DIR *devices = opendir(SYSFS_DEVICE_PATH); | |
| 296 struct dirent *entry; | |
| 297 | |
| 298 usbi_dbg("found usb devices in sysfs"); | |
| 299 | |
| 300 if (!devices) { | |
| 301 usbi_err(ctx, "opendir devices failed errno=%d", errno); | |
| 302 return LIBUSB_ERROR_IO; | |
| 303 } | |
| 304 | |
| 305 /* Make sure sysfs supports all the required files. If it | |
| 306 * does not, then usbfs will be used instead. Determine | |
| 307 * this by looping through the directories in | |
| 308 * SYSFS_DEVICE_PATH. With the assumption that there will | |
| 309 * always be subdirectories of the name usbN (usb1, usb2, | |
| 310 * etc) representing the root hubs, check the usbN | |
| 311 * subdirectories to see if they have all the needed files. | |
| 312 * This algorithm uses the usbN subdirectories (root hubs) | |
| 313 * because a device disconnection will cause a race | |
| 314 * condition regarding which files are available, sometimes | |
| 315 * causing an incorrect result. The root hubs are used | |
| 316 * because it is assumed that they will always be present. | |
| 317 * See the "sysfs vs usbfs" comment at the top of this file | |
| 318 * for more details. */ | |
| 319 while ((entry = readdir(devices))) { | |
| 320 int has_busnum=0, has_devnum=0, has_descriptors=0; | |
| 321 int has_configuration_value=0; | |
| 322 | |
| 323 /* Only check the usbN directories. */ | |
| 324 if (strncmp(entry->d_name, "usb", 3) != 0) | |
| 325 continue; | |
| 326 | |
| 327 /* Check for the files libusb needs from sysfs. */ | |
| 328 has_busnum = sysfs_has_file(entry->d_name, "busnum"); | |
| 329 has_devnum = sysfs_has_file(entry->d_name, "devnum"); | |
| 330 has_descriptors = sysfs_has_file(entry->d_name, "descrip
tors"); | |
| 331 has_configuration_value = sysfs_has_file(entry->d_name,
"bConfigurationValue"); | |
| 332 | |
| 333 if (has_busnum && has_devnum && has_configuration_value) | |
| 334 sysfs_can_relate_devices = 1; | |
| 335 if (has_descriptors) | |
| 336 sysfs_has_descriptors = 1; | |
| 337 | |
| 338 /* Only need to check until we've found ONE device which | |
| 339 has all the attributes. */ | |
| 340 if (sysfs_has_descriptors && sysfs_can_relate_devices) | |
| 341 break; | |
| 342 } | |
| 343 closedir(devices); | |
| 344 | |
| 345 /* Only use sysfs descriptors if the rest of | |
| 346 sysfs will work for libusb. */ | |
| 347 if (!sysfs_can_relate_devices) | |
| 348 sysfs_has_descriptors = 0; | |
| 349 } else { | |
| 350 usbi_dbg("sysfs usb info not available"); | |
| 351 sysfs_has_descriptors = 0; | |
| 352 sysfs_can_relate_devices = 0; | |
| 353 } | |
| 354 | |
| 355 return 0; | |
| 356 } | |
| 357 | |
| 358 static int usbfs_get_device_descriptor(struct libusb_device *dev, | |
| 359 unsigned char *buffer) | |
| 360 { | |
| 361 struct linux_device_priv *priv = _device_priv(dev); | |
| 362 | |
| 363 /* return cached copy */ | |
| 364 memcpy(buffer, priv->dev_descriptor, DEVICE_DESC_LENGTH); | |
| 365 return 0; | |
| 366 } | |
| 367 | |
| 368 static int _open_sysfs_attr(struct libusb_device *dev, const char *attr) | |
| 369 { | |
| 370 struct linux_device_priv *priv = _device_priv(dev); | |
| 371 char filename[PATH_MAX]; | |
| 372 int fd; | |
| 373 | |
| 374 snprintf(filename, PATH_MAX, "%s/%s/%s", | |
| 375 SYSFS_DEVICE_PATH, priv->sysfs_dir, attr); | |
| 376 fd = open(filename, O_RDONLY); | |
| 377 if (fd < 0) { | |
| 378 usbi_err(DEVICE_CTX(dev), | |
| 379 "open %s failed ret=%d errno=%d", filename, fd, errno); | |
| 380 return LIBUSB_ERROR_IO; | |
| 381 } | |
| 382 | |
| 383 return fd; | |
| 384 } | |
| 385 | |
| 386 /* Note only suitable for attributes which always read >= 0, < 0 is error */ | |
| 387 static int __read_sysfs_attr(struct libusb_context *ctx, | |
| 388 const char *devname, const char *attr) | |
| 389 { | |
| 390 char filename[PATH_MAX]; | |
| 391 FILE *f; | |
| 392 int r, value; | |
| 393 | |
| 394 snprintf(filename, PATH_MAX, "%s/%s/%s", SYSFS_DEVICE_PATH, | |
| 395 devname, attr); | |
| 396 f = fopen(filename, "r"); | |
| 397 if (f == NULL) { | |
| 398 if (errno == ENOENT) { | |
| 399 /* File doesn't exist. Assume the device has been | |
| 400 disconnected (see trac ticket #70). */ | |
| 401 return LIBUSB_ERROR_NO_DEVICE; | |
| 402 } | |
| 403 usbi_err(ctx, "open %s failed errno=%d", filename, errno); | |
| 404 return LIBUSB_ERROR_IO; | |
| 405 } | |
| 406 | |
| 407 r = fscanf(f, "%d", &value); | |
| 408 fclose(f); | |
| 409 if (r != 1) { | |
| 410 usbi_err(ctx, "fscanf %s returned %d, errno=%d", attr, r, errno)
; | |
| 411 return LIBUSB_ERROR_NO_DEVICE; /* For unplug race (trac #70) */ | |
| 412 } | |
| 413 if (value < 0) { | |
| 414 usbi_err(ctx, "%s contains a negative value", filename); | |
| 415 return LIBUSB_ERROR_IO; | |
| 416 } | |
| 417 | |
| 418 return value; | |
| 419 } | |
| 420 | |
| 421 static int sysfs_get_device_descriptor(struct libusb_device *dev, | |
| 422 unsigned char *buffer) | |
| 423 { | |
| 424 int fd; | |
| 425 ssize_t r; | |
| 426 | |
| 427 /* sysfs provides access to an in-memory copy of the device descriptor, | |
| 428 * so we use that rather than keeping our own copy */ | |
| 429 | |
| 430 fd = _open_sysfs_attr(dev, "descriptors"); | |
| 431 if (fd < 0) | |
| 432 return fd; | |
| 433 | |
| 434 r = read(fd, buffer, DEVICE_DESC_LENGTH);; | |
| 435 close(fd); | |
| 436 if (r < 0) { | |
| 437 usbi_err(DEVICE_CTX(dev), "read failed, ret=%d errno=%d", fd, er
rno); | |
| 438 return LIBUSB_ERROR_IO; | |
| 439 } else if (r < DEVICE_DESC_LENGTH) { | |
| 440 usbi_err(DEVICE_CTX(dev), "short read %d/%d", r, DEVICE_DESC_LEN
GTH); | |
| 441 return LIBUSB_ERROR_IO; | |
| 442 } | |
| 443 | |
| 444 return 0; | |
| 445 } | |
| 446 | |
| 447 static int op_get_device_descriptor(struct libusb_device *dev, | |
| 448 unsigned char *buffer, int *host_endian) | |
| 449 { | |
| 450 if (sysfs_has_descriptors) { | |
| 451 return sysfs_get_device_descriptor(dev, buffer); | |
| 452 } else { | |
| 453 *host_endian = 1; | |
| 454 return usbfs_get_device_descriptor(dev, buffer); | |
| 455 } | |
| 456 } | |
| 457 | |
| 458 static int usbfs_get_active_config_descriptor(struct libusb_device *dev, | |
| 459 unsigned char *buffer, size_t len) | |
| 460 { | |
| 461 struct linux_device_priv *priv = _device_priv(dev); | |
| 462 if (!priv->config_descriptor) | |
| 463 return LIBUSB_ERROR_NOT_FOUND; /* device is unconfigured */ | |
| 464 | |
| 465 /* retrieve cached copy */ | |
| 466 memcpy(buffer, priv->config_descriptor, len); | |
| 467 return 0; | |
| 468 } | |
| 469 | |
| 470 /* read the bConfigurationValue for a device */ | |
| 471 static int sysfs_get_active_config(struct libusb_device *dev, int *config) | |
| 472 { | |
| 473 char *endptr; | |
| 474 char tmp[4] = {0, 0, 0, 0}; | |
| 475 long num; | |
| 476 int fd; | |
| 477 ssize_t r; | |
| 478 | |
| 479 fd = _open_sysfs_attr(dev, "bConfigurationValue"); | |
| 480 if (fd < 0) | |
| 481 return fd; | |
| 482 | |
| 483 r = read(fd, tmp, sizeof(tmp)); | |
| 484 close(fd); | |
| 485 if (r < 0) { | |
| 486 usbi_err(DEVICE_CTX(dev), | |
| 487 "read bConfigurationValue failed ret=%d errno=%d", r, er
rno); | |
| 488 return LIBUSB_ERROR_IO; | |
| 489 } else if (r == 0) { | |
| 490 usbi_err(DEVICE_CTX(dev), "device unconfigured"); | |
| 491 *config = -1; | |
| 492 return 0; | |
| 493 } | |
| 494 | |
| 495 if (tmp[sizeof(tmp) - 1] != 0) { | |
| 496 usbi_err(DEVICE_CTX(dev), "not null-terminated?"); | |
| 497 return LIBUSB_ERROR_IO; | |
| 498 } else if (tmp[0] == 0) { | |
| 499 usbi_err(DEVICE_CTX(dev), "no configuration value?"); | |
| 500 return LIBUSB_ERROR_IO; | |
| 501 } | |
| 502 | |
| 503 num = strtol(tmp, &endptr, 10); | |
| 504 if (endptr == tmp) { | |
| 505 usbi_err(DEVICE_CTX(dev), "error converting '%s' to integer", tm
p); | |
| 506 return LIBUSB_ERROR_IO; | |
| 507 } | |
| 508 | |
| 509 *config = (int) num; | |
| 510 return 0; | |
| 511 } | |
| 512 | |
| 513 /* takes a usbfs/descriptors fd seeked to the start of a configuration, and | |
| 514 * seeks to the next one. */ | |
| 515 static int seek_to_next_config(struct libusb_context *ctx, int fd, | |
| 516 int host_endian) | |
| 517 { | |
| 518 struct libusb_config_descriptor config; | |
| 519 unsigned char tmp[6]; | |
| 520 off_t off; | |
| 521 ssize_t r; | |
| 522 | |
| 523 /* read first 6 bytes of descriptor */ | |
| 524 r = read(fd, tmp, sizeof(tmp)); | |
| 525 if (r < 0) { | |
| 526 usbi_err(ctx, "read failed ret=%d errno=%d", r, errno); | |
| 527 return LIBUSB_ERROR_IO; | |
| 528 } else if (r < sizeof(tmp)) { | |
| 529 usbi_err(ctx, "short descriptor read %d/%d", r, sizeof(tmp)); | |
| 530 return LIBUSB_ERROR_IO; | |
| 531 } | |
| 532 | |
| 533 /* seek forward to end of config */ | |
| 534 usbi_parse_descriptor(tmp, "bbwbb", &config, host_endian); | |
| 535 off = lseek(fd, config.wTotalLength - sizeof(tmp), SEEK_CUR); | |
| 536 if (off < 0) { | |
| 537 usbi_err(ctx, "seek failed ret=%d errno=%d", off, errno); | |
| 538 return LIBUSB_ERROR_IO; | |
| 539 } | |
| 540 | |
| 541 return 0; | |
| 542 } | |
| 543 | |
| 544 static int sysfs_get_active_config_descriptor(struct libusb_device *dev, | |
| 545 unsigned char *buffer, size_t len) | |
| 546 { | |
| 547 int fd; | |
| 548 ssize_t r; | |
| 549 off_t off; | |
| 550 int to_copy; | |
| 551 int config; | |
| 552 unsigned char tmp[6]; | |
| 553 | |
| 554 r = sysfs_get_active_config(dev, &config); | |
| 555 if (r < 0) | |
| 556 return r; | |
| 557 if (config == -1) | |
| 558 return LIBUSB_ERROR_NOT_FOUND; | |
| 559 | |
| 560 usbi_dbg("active configuration %d", config); | |
| 561 | |
| 562 /* sysfs provides access to an in-memory copy of the device descriptor, | |
| 563 * so we use that rather than keeping our own copy */ | |
| 564 | |
| 565 fd = _open_sysfs_attr(dev, "descriptors"); | |
| 566 if (fd < 0) | |
| 567 return fd; | |
| 568 | |
| 569 /* device might have been unconfigured since we read bConfigurationValue
, | |
| 570 * so first check that there is any config descriptor data at all... */ | |
| 571 off = lseek(fd, 0, SEEK_END); | |
| 572 if (off < 1) { | |
| 573 usbi_err(DEVICE_CTX(dev), "end seek failed, ret=%d errno=%d", | |
| 574 off, errno); | |
| 575 close(fd); | |
| 576 return LIBUSB_ERROR_IO; | |
| 577 } else if (off == DEVICE_DESC_LENGTH) { | |
| 578 close(fd); | |
| 579 return LIBUSB_ERROR_NOT_FOUND; | |
| 580 } | |
| 581 | |
| 582 off = lseek(fd, DEVICE_DESC_LENGTH, SEEK_SET); | |
| 583 if (off < 0) { | |
| 584 usbi_err(DEVICE_CTX(dev), "seek failed, ret=%d errno=%d", off, e
rrno); | |
| 585 close(fd); | |
| 586 return LIBUSB_ERROR_IO; | |
| 587 } | |
| 588 | |
| 589 /* unbounded loop: we expect the descriptor to be present under all | |
| 590 * circumstances */ | |
| 591 while (1) { | |
| 592 r = read(fd, tmp, sizeof(tmp)); | |
| 593 if (r < 0) { | |
| 594 usbi_err(DEVICE_CTX(dev), "read failed, ret=%d errno=%d"
, | |
| 595 fd, errno); | |
| 596 return LIBUSB_ERROR_IO; | |
| 597 } else if (r < sizeof(tmp)) { | |
| 598 usbi_err(DEVICE_CTX(dev), "short read %d/%d", r, sizeof(
tmp)); | |
| 599 return LIBUSB_ERROR_IO; | |
| 600 } | |
| 601 | |
| 602 /* check bConfigurationValue */ | |
| 603 if (tmp[5] == config) | |
| 604 break; | |
| 605 | |
| 606 /* try the next descriptor */ | |
| 607 off = lseek(fd, 0 - sizeof(tmp), SEEK_CUR); | |
| 608 if (off < 0) | |
| 609 return LIBUSB_ERROR_IO; | |
| 610 | |
| 611 r = seek_to_next_config(DEVICE_CTX(dev), fd, 0); | |
| 612 if (r < 0) | |
| 613 return r; | |
| 614 } | |
| 615 | |
| 616 to_copy = (len < sizeof(tmp)) ? len : sizeof(tmp); | |
| 617 memcpy(buffer, tmp, to_copy); | |
| 618 if (len > sizeof(tmp)) { | |
| 619 r = read(fd, buffer + sizeof(tmp), len - sizeof(tmp)); | |
| 620 if (r < 0) { | |
| 621 usbi_err(DEVICE_CTX(dev), "read failed, ret=%d errno=%d"
, | |
| 622 fd, errno); | |
| 623 r = LIBUSB_ERROR_IO; | |
| 624 } else if (r == 0) { | |
| 625 usbi_dbg("device is unconfigured"); | |
| 626 r = LIBUSB_ERROR_NOT_FOUND; | |
| 627 } else if (r < len - sizeof(tmp)) { | |
| 628 usbi_err(DEVICE_CTX(dev), "short read %d/%d", r, len); | |
| 629 r = LIBUSB_ERROR_IO; | |
| 630 } | |
| 631 } else { | |
| 632 r = 0; | |
| 633 } | |
| 634 | |
| 635 close(fd); | |
| 636 return r; | |
| 637 } | |
| 638 | |
| 639 static int op_get_active_config_descriptor(struct libusb_device *dev, | |
| 640 unsigned char *buffer, size_t len, int *host_endian) | |
| 641 { | |
| 642 if (sysfs_has_descriptors) { | |
| 643 return sysfs_get_active_config_descriptor(dev, buffer, len); | |
| 644 } else { | |
| 645 return usbfs_get_active_config_descriptor(dev, buffer, len); | |
| 646 } | |
| 647 } | |
| 648 | |
| 649 /* takes a usbfs fd, attempts to find the requested config and copy a certain | |
| 650 * amount of it into an output buffer. */ | |
| 651 static int get_config_descriptor(struct libusb_context *ctx, int fd, | |
| 652 uint8_t config_index, unsigned char *buffer, size_t len) | |
| 653 { | |
| 654 off_t off; | |
| 655 ssize_t r; | |
| 656 | |
| 657 off = lseek(fd, DEVICE_DESC_LENGTH, SEEK_SET); | |
| 658 if (off < 0) { | |
| 659 usbi_err(ctx, "seek failed ret=%d errno=%d", off, errno); | |
| 660 return LIBUSB_ERROR_IO; | |
| 661 } | |
| 662 | |
| 663 /* might need to skip some configuration descriptors to reach the | |
| 664 * requested configuration */ | |
| 665 while (config_index > 0) { | |
| 666 r = seek_to_next_config(ctx, fd, 1); | |
| 667 if (r < 0) | |
| 668 return r; | |
| 669 config_index--; | |
| 670 } | |
| 671 | |
| 672 /* read the rest of the descriptor */ | |
| 673 r = read(fd, buffer, len); | |
| 674 if (r < 0) { | |
| 675 usbi_err(ctx, "read failed ret=%d errno=%d", r, errno); | |
| 676 return LIBUSB_ERROR_IO; | |
| 677 } else if (r < len) { | |
| 678 usbi_err(ctx, "short output read %d/%d", r, len); | |
| 679 return LIBUSB_ERROR_IO; | |
| 680 } | |
| 681 | |
| 682 return 0; | |
| 683 } | |
| 684 | |
| 685 static int op_get_config_descriptor(struct libusb_device *dev, | |
| 686 uint8_t config_index, unsigned char *buffer, size_t len, int *host_endia
n) | |
| 687 { | |
| 688 char filename[PATH_MAX]; | |
| 689 int fd; | |
| 690 int r; | |
| 691 | |
| 692 /* always read from usbfs: sysfs only has the active descriptor | |
| 693 * this will involve waking the device up, but oh well! */ | |
| 694 | |
| 695 /* FIXME: the above is no longer true, new kernels have all descriptors | |
| 696 * in the descriptors file. but its kinda hard to detect if the kernel | |
| 697 * is sufficiently new. */ | |
| 698 | |
| 699 _get_usbfs_path(dev, filename); | |
| 700 fd = open(filename, O_RDONLY); | |
| 701 if (fd < 0) { | |
| 702 usbi_err(DEVICE_CTX(dev), | |
| 703 "open '%s' failed, ret=%d errno=%d", filename, fd, errno
); | |
| 704 return LIBUSB_ERROR_IO; | |
| 705 } | |
| 706 | |
| 707 r = get_config_descriptor(DEVICE_CTX(dev), fd, config_index, buffer, len
); | |
| 708 close(fd); | |
| 709 return r; | |
| 710 } | |
| 711 | |
| 712 /* cache the active config descriptor in memory. a value of -1 means that | |
| 713 * we aren't sure which one is active, so just assume the first one. | |
| 714 * only for usbfs. */ | |
| 715 static int cache_active_config(struct libusb_device *dev, int fd, | |
| 716 int active_config) | |
| 717 { | |
| 718 struct linux_device_priv *priv = _device_priv(dev); | |
| 719 struct libusb_config_descriptor config; | |
| 720 unsigned char tmp[8]; | |
| 721 unsigned char *buf; | |
| 722 int idx; | |
| 723 int r; | |
| 724 | |
| 725 if (active_config == -1) { | |
| 726 idx = 0; | |
| 727 } else { | |
| 728 r = usbi_get_config_index_by_value(dev, active_config, &idx); | |
| 729 if (r < 0) | |
| 730 return r; | |
| 731 if (idx == -1) | |
| 732 return LIBUSB_ERROR_NOT_FOUND; | |
| 733 } | |
| 734 | |
| 735 r = get_config_descriptor(DEVICE_CTX(dev), fd, idx, tmp, sizeof(tmp)); | |
| 736 if (r < 0) { | |
| 737 usbi_err(DEVICE_CTX(dev), "first read error %d", r); | |
| 738 return r; | |
| 739 } | |
| 740 | |
| 741 usbi_parse_descriptor(tmp, "bbw", &config, 0); | |
| 742 buf = malloc(config.wTotalLength); | |
| 743 if (!buf) | |
| 744 return LIBUSB_ERROR_NO_MEM; | |
| 745 | |
| 746 r = get_config_descriptor(DEVICE_CTX(dev), fd, idx, buf, | |
| 747 config.wTotalLength); | |
| 748 if (r < 0) { | |
| 749 free(buf); | |
| 750 return r; | |
| 751 } | |
| 752 | |
| 753 if (priv->config_descriptor) | |
| 754 free(priv->config_descriptor); | |
| 755 priv->config_descriptor = buf; | |
| 756 return 0; | |
| 757 } | |
| 758 | |
| 759 /* send a control message to retrieve active configuration */ | |
| 760 static int usbfs_get_active_config(struct libusb_device *dev, int fd) | |
| 761 { | |
| 762 unsigned char active_config = 0; | |
| 763 int r; | |
| 764 | |
| 765 struct usbfs_ctrltransfer ctrl = { | |
| 766 .bmRequestType = LIBUSB_ENDPOINT_IN, | |
| 767 .bRequest = LIBUSB_REQUEST_GET_CONFIGURATION, | |
| 768 .wValue = 0, | |
| 769 .wIndex = 0, | |
| 770 .wLength = 1, | |
| 771 .timeout = 1000, | |
| 772 .data = &active_config | |
| 773 }; | |
| 774 | |
| 775 r = ioctl(fd, IOCTL_USBFS_CONTROL, &ctrl); | |
| 776 if (r < 0) { | |
| 777 if (errno == ENODEV) | |
| 778 return LIBUSB_ERROR_NO_DEVICE; | |
| 779 | |
| 780 /* we hit this error path frequently with buggy devices :( */ | |
| 781 usbi_warn(DEVICE_CTX(dev), | |
| 782 "get_configuration failed ret=%d errno=%d", r, errno); | |
| 783 return LIBUSB_ERROR_IO; | |
| 784 } | |
| 785 | |
| 786 return active_config; | |
| 787 } | |
| 788 | |
| 789 static int initialize_device(struct libusb_device *dev, uint8_t busnum, | |
| 790 uint8_t devaddr, const char *sysfs_dir) | |
| 791 { | |
| 792 struct linux_device_priv *priv = _device_priv(dev); | |
| 793 unsigned char *dev_buf; | |
| 794 char path[PATH_MAX]; | |
| 795 int fd, speed; | |
| 796 int active_config = 0; | |
| 797 int device_configured = 1; | |
| 798 ssize_t r; | |
| 799 | |
| 800 dev->bus_number = busnum; | |
| 801 dev->device_address = devaddr; | |
| 802 | |
| 803 if (sysfs_dir) { | |
| 804 priv->sysfs_dir = malloc(strlen(sysfs_dir) + 1); | |
| 805 if (!priv->sysfs_dir) | |
| 806 return LIBUSB_ERROR_NO_MEM; | |
| 807 strcpy(priv->sysfs_dir, sysfs_dir); | |
| 808 | |
| 809 /* Note speed can contain 1.5, in this case __read_sysfs_attr | |
| 810 will stop parsing at the '.' and return 1 */ | |
| 811 speed = __read_sysfs_attr(DEVICE_CTX(dev), sysfs_dir, "speed"); | |
| 812 if (speed >= 0) { | |
| 813 switch (speed) { | |
| 814 case 1: dev->speed = LIBUSB_SPEED_LOW; break; | |
| 815 case 12: dev->speed = LIBUSB_SPEED_FULL; break; | |
| 816 case 480: dev->speed = LIBUSB_SPEED_HIGH; break; | |
| 817 case 5000: dev->speed = LIBUSB_SPEED_SUPER; break; | |
| 818 default: | |
| 819 usbi_warn(DEVICE_CTX(dev), "Unknown device speed
: %d Mbps", speed); | |
| 820 } | |
| 821 } | |
| 822 } | |
| 823 | |
| 824 if (sysfs_has_descriptors) | |
| 825 return 0; | |
| 826 | |
| 827 /* cache device descriptor in memory so that we can retrieve it later | |
| 828 * without waking the device up (op_get_device_descriptor) */ | |
| 829 | |
| 830 priv->dev_descriptor = NULL; | |
| 831 priv->config_descriptor = NULL; | |
| 832 | |
| 833 if (sysfs_can_relate_devices) { | |
| 834 int tmp = sysfs_get_active_config(dev, &active_config); | |
| 835 if (tmp < 0) | |
| 836 return tmp; | |
| 837 if (active_config == -1) | |
| 838 device_configured = 0; | |
| 839 } | |
| 840 | |
| 841 _get_usbfs_path(dev, path); | |
| 842 fd = open(path, O_RDWR); | |
| 843 if (fd < 0 && errno == EACCES) { | |
| 844 fd = open(path, O_RDONLY); | |
| 845 /* if we only have read-only access to the device, we cannot | |
| 846 * send a control message to determine the active config. just | |
| 847 * assume the first one is active. */ | |
| 848 active_config = -1; | |
| 849 } | |
| 850 | |
| 851 if (fd < 0) { | |
| 852 usbi_err(DEVICE_CTX(dev), "open failed, ret=%d errno=%d", fd, er
rno); | |
| 853 return LIBUSB_ERROR_IO; | |
| 854 } | |
| 855 | |
| 856 if (!sysfs_can_relate_devices) { | |
| 857 if (active_config == -1) { | |
| 858 /* if we only have read-only access to the device, we ca
nnot | |
| 859 * send a control message to determine the active config
. just | |
| 860 * assume the first one is active. */ | |
| 861 usbi_warn(DEVICE_CTX(dev), "access to %s is read-only; c
annot " | |
| 862 "determine active configuration descriptor", pat
h); | |
| 863 } else { | |
| 864 active_config = usbfs_get_active_config(dev, fd); | |
| 865 if (active_config == LIBUSB_ERROR_IO) { | |
| 866 /* buggy devices sometimes fail to report their
active config. | |
| 867 * assume unconfigured and continue the probing
*/ | |
| 868 usbi_warn(DEVICE_CTX(dev), "couldn't query activ
e " | |
| 869 "configuration, assumung unconfigured"); | |
| 870 device_configured = 0; | |
| 871 } else if (active_config < 0) { | |
| 872 close(fd); | |
| 873 return active_config; | |
| 874 } else if (active_config == 0) { | |
| 875 /* some buggy devices have a configuration 0, bu
t we're | |
| 876 * reaching into the corner of a corner case her
e, so let's | |
| 877 * not support buggy devices in these circumstan
ces. | |
| 878 * stick to the specs: a configuration value of
0 means | |
| 879 * unconfigured. */ | |
| 880 usbi_dbg("active cfg 0? assuming unconfigured de
vice"); | |
| 881 device_configured = 0; | |
| 882 } | |
| 883 } | |
| 884 } | |
| 885 | |
| 886 dev_buf = malloc(DEVICE_DESC_LENGTH); | |
| 887 if (!dev_buf) { | |
| 888 close(fd); | |
| 889 return LIBUSB_ERROR_NO_MEM; | |
| 890 } | |
| 891 | |
| 892 r = read(fd, dev_buf, DEVICE_DESC_LENGTH); | |
| 893 if (r < 0) { | |
| 894 usbi_err(DEVICE_CTX(dev), | |
| 895 "read descriptor failed ret=%d errno=%d", fd, errno); | |
| 896 free(dev_buf); | |
| 897 close(fd); | |
| 898 return LIBUSB_ERROR_IO; | |
| 899 } else if (r < DEVICE_DESC_LENGTH) { | |
| 900 usbi_err(DEVICE_CTX(dev), "short descriptor read (%d)", r); | |
| 901 free(dev_buf); | |
| 902 close(fd); | |
| 903 return LIBUSB_ERROR_IO; | |
| 904 } | |
| 905 | |
| 906 /* bit of a hack: set num_configurations now because cache_active_config
() | |
| 907 * calls usbi_get_config_index_by_value() which uses it */ | |
| 908 dev->num_configurations = dev_buf[DEVICE_DESC_LENGTH - 1]; | |
| 909 | |
| 910 if (device_configured) { | |
| 911 r = cache_active_config(dev, fd, active_config); | |
| 912 if (r < 0) { | |
| 913 close(fd); | |
| 914 free(dev_buf); | |
| 915 return r; | |
| 916 } | |
| 917 } | |
| 918 | |
| 919 close(fd); | |
| 920 priv->dev_descriptor = dev_buf; | |
| 921 return 0; | |
| 922 } | |
| 923 | |
| 924 static int enumerate_device(struct libusb_context *ctx, | |
| 925 struct discovered_devs **_discdevs, uint8_t busnum, uint8_t devaddr, | |
| 926 const char *sysfs_dir) | |
| 927 { | |
| 928 struct discovered_devs *discdevs; | |
| 929 unsigned long session_id; | |
| 930 int need_unref = 0; | |
| 931 struct libusb_device *dev; | |
| 932 int r = 0; | |
| 933 | |
| 934 /* FIXME: session ID is not guaranteed unique as addresses can wrap and | |
| 935 * will be reused. instead we should add a simple sysfs attribute with | |
| 936 * a session ID. */ | |
| 937 session_id = busnum << 8 | devaddr; | |
| 938 usbi_dbg("busnum %d devaddr %d session_id %ld", busnum, devaddr, | |
| 939 session_id); | |
| 940 | |
| 941 dev = usbi_get_device_by_session_id(ctx, session_id); | |
| 942 if (dev) { | |
| 943 usbi_dbg("using existing device for %d/%d (session %ld)", | |
| 944 busnum, devaddr, session_id); | |
| 945 } else { | |
| 946 usbi_dbg("allocating new device for %d/%d (session %ld)", | |
| 947 busnum, devaddr, session_id); | |
| 948 dev = usbi_alloc_device(ctx, session_id); | |
| 949 if (!dev) | |
| 950 return LIBUSB_ERROR_NO_MEM; | |
| 951 need_unref = 1; | |
| 952 r = initialize_device(dev, busnum, devaddr, sysfs_dir); | |
| 953 if (r < 0) | |
| 954 goto out; | |
| 955 r = usbi_sanitize_device(dev); | |
| 956 if (r < 0) | |
| 957 goto out; | |
| 958 } | |
| 959 | |
| 960 discdevs = discovered_devs_append(*_discdevs, dev); | |
| 961 if (!discdevs) | |
| 962 r = LIBUSB_ERROR_NO_MEM; | |
| 963 else | |
| 964 *_discdevs = discdevs; | |
| 965 | |
| 966 out: | |
| 967 if (need_unref) | |
| 968 libusb_unref_device(dev); | |
| 969 return r; | |
| 970 } | |
| 971 | |
| 972 /* open a bus directory and adds all discovered devices to discdevs. on | |
| 973 * failure (non-zero return) the pre-existing discdevs should be destroyed | |
| 974 * (and devices freed). on success, the new discdevs pointer should be used | |
| 975 * as it may have been moved. */ | |
| 976 static int usbfs_scan_busdir(struct libusb_context *ctx, | |
| 977 struct discovered_devs **_discdevs, uint8_t busnum) | |
| 978 { | |
| 979 DIR *dir; | |
| 980 char dirpath[PATH_MAX]; | |
| 981 struct dirent *entry; | |
| 982 struct discovered_devs *discdevs = *_discdevs; | |
| 983 int r = LIBUSB_ERROR_IO; | |
| 984 | |
| 985 snprintf(dirpath, PATH_MAX, "%s/%03d", usbfs_path, busnum); | |
| 986 usbi_dbg("%s", dirpath); | |
| 987 dir = opendir(dirpath); | |
| 988 if (!dir) { | |
| 989 usbi_err(ctx, "opendir '%s' failed, errno=%d", dirpath, errno); | |
| 990 /* FIXME: should handle valid race conditions like hub unplugged | |
| 991 * during directory iteration - this is not an error */ | |
| 992 return r; | |
| 993 } | |
| 994 | |
| 995 while ((entry = readdir(dir))) { | |
| 996 int devaddr; | |
| 997 | |
| 998 if (entry->d_name[0] == '.') | |
| 999 continue; | |
| 1000 | |
| 1001 devaddr = atoi(entry->d_name); | |
| 1002 if (devaddr == 0) { | |
| 1003 usbi_dbg("unknown dir entry %s", entry->d_name); | |
| 1004 continue; | |
| 1005 } | |
| 1006 | |
| 1007 if (enumerate_device(ctx, &discdevs, busnum, (uint8_t) devaddr,
NULL)) { | |
| 1008 usbi_dbg("failed to enumerate dir entry %s", entry->d_na
me); | |
| 1009 continue; | |
| 1010 } | |
| 1011 | |
| 1012 r = 0; | |
| 1013 } | |
| 1014 | |
| 1015 if (!r) | |
| 1016 *_discdevs = discdevs; | |
| 1017 closedir(dir); | |
| 1018 return r; | |
| 1019 } | |
| 1020 | |
| 1021 static int usbfs_get_device_list(struct libusb_context *ctx, | |
| 1022 struct discovered_devs **_discdevs) | |
| 1023 { | |
| 1024 struct dirent *entry; | |
| 1025 DIR *buses = opendir(usbfs_path); | |
| 1026 struct discovered_devs *discdevs = *_discdevs; | |
| 1027 int r = 0; | |
| 1028 | |
| 1029 if (!buses) { | |
| 1030 usbi_err(ctx, "opendir buses failed errno=%d", errno); | |
| 1031 return LIBUSB_ERROR_IO; | |
| 1032 } | |
| 1033 | |
| 1034 while ((entry = readdir(buses))) { | |
| 1035 struct discovered_devs *discdevs_new = discdevs; | |
| 1036 int busnum; | |
| 1037 | |
| 1038 if (entry->d_name[0] == '.') | |
| 1039 continue; | |
| 1040 | |
| 1041 busnum = atoi(entry->d_name); | |
| 1042 if (busnum == 0) { | |
| 1043 usbi_dbg("unknown dir entry %s", entry->d_name); | |
| 1044 continue; | |
| 1045 } | |
| 1046 | |
| 1047 r = usbfs_scan_busdir(ctx, &discdevs_new, busnum); | |
| 1048 if (r < 0) | |
| 1049 goto out; | |
| 1050 discdevs = discdevs_new; | |
| 1051 } | |
| 1052 | |
| 1053 out: | |
| 1054 closedir(buses); | |
| 1055 *_discdevs = discdevs; | |
| 1056 return r; | |
| 1057 | |
| 1058 } | |
| 1059 | |
| 1060 static int sysfs_scan_device(struct libusb_context *ctx, | |
| 1061 struct discovered_devs **_discdevs, const char *devname) | |
| 1062 { | |
| 1063 int busnum; | |
| 1064 int devaddr; | |
| 1065 | |
| 1066 usbi_dbg("scan %s", devname); | |
| 1067 | |
| 1068 busnum = __read_sysfs_attr(ctx, devname, "busnum"); | |
| 1069 if (busnum < 0) | |
| 1070 return busnum; | |
| 1071 | |
| 1072 devaddr = __read_sysfs_attr(ctx, devname, "devnum"); | |
| 1073 if (devaddr < 0) | |
| 1074 return devaddr; | |
| 1075 | |
| 1076 usbi_dbg("bus=%d dev=%d", busnum, devaddr); | |
| 1077 if (busnum > 255 || devaddr > 255) | |
| 1078 return LIBUSB_ERROR_INVALID_PARAM; | |
| 1079 | |
| 1080 return enumerate_device(ctx, _discdevs, busnum & 0xff, devaddr & 0xff, | |
| 1081 devname); | |
| 1082 } | |
| 1083 | |
| 1084 static int sysfs_get_device_list(struct libusb_context *ctx, | |
| 1085 struct discovered_devs **_discdevs) | |
| 1086 { | |
| 1087 struct discovered_devs *discdevs = *_discdevs; | |
| 1088 DIR *devices = opendir(SYSFS_DEVICE_PATH); | |
| 1089 struct dirent *entry; | |
| 1090 int r = LIBUSB_ERROR_IO; | |
| 1091 | |
| 1092 if (!devices) { | |
| 1093 usbi_err(ctx, "opendir devices failed errno=%d", errno); | |
| 1094 return r; | |
| 1095 } | |
| 1096 | |
| 1097 while ((entry = readdir(devices))) { | |
| 1098 struct discovered_devs *discdevs_new = discdevs; | |
| 1099 | |
| 1100 if ((!isdigit(entry->d_name[0]) && strncmp(entry->d_name, "usb",
3)) | |
| 1101 || strchr(entry->d_name, ':')) | |
| 1102 continue; | |
| 1103 | |
| 1104 if (sysfs_scan_device(ctx, &discdevs_new, entry->d_name)) { | |
| 1105 usbi_dbg("failed to enumerate dir entry %s", entry->d_na
me); | |
| 1106 continue; | |
| 1107 } | |
| 1108 | |
| 1109 r = 0; | |
| 1110 discdevs = discdevs_new; | |
| 1111 } | |
| 1112 | |
| 1113 if (!r) | |
| 1114 *_discdevs = discdevs; | |
| 1115 closedir(devices); | |
| 1116 return r; | |
| 1117 } | |
| 1118 | |
| 1119 static int op_get_device_list(struct libusb_context *ctx, | |
| 1120 struct discovered_devs **_discdevs) | |
| 1121 { | |
| 1122 /* we can retrieve device list and descriptors from sysfs or usbfs. | |
| 1123 * sysfs is preferable, because if we use usbfs we end up resuming | |
| 1124 * any autosuspended USB devices. however, sysfs is not available | |
| 1125 * everywhere, so we need a usbfs fallback too. | |
| 1126 * | |
| 1127 * as described in the "sysfs vs usbfs" comment at the top of this | |
| 1128 * file, sometimes we have sysfs but not enough information to | |
| 1129 * relate sysfs devices to usbfs nodes. op_init() determines the | |
| 1130 * adequacy of sysfs and sets sysfs_can_relate_devices. | |
| 1131 */ | |
| 1132 if (sysfs_can_relate_devices != 0) | |
| 1133 return sysfs_get_device_list(ctx, _discdevs); | |
| 1134 else | |
| 1135 return usbfs_get_device_list(ctx, _discdevs); | |
| 1136 } | |
| 1137 | |
| 1138 static int op_open(struct libusb_device_handle *handle) | |
| 1139 { | |
| 1140 struct linux_device_handle_priv *hpriv = _device_handle_priv(handle); | |
| 1141 char filename[PATH_MAX]; | |
| 1142 | |
| 1143 _get_usbfs_path(handle->dev, filename); | |
| 1144 usbi_dbg("opening %s", filename); | |
| 1145 hpriv->fd = open(filename, O_RDWR); | |
| 1146 if (hpriv->fd < 0) { | |
| 1147 if (errno == EACCES) { | |
| 1148 usbi_err(HANDLE_CTX(handle), "libusb couldn't open USB d
evice %s: " | |
| 1149 "Permission denied.", filename); | |
| 1150 usbi_err(HANDLE_CTX(handle), | |
| 1151 "libusb requires write access to USB device node
s."); | |
| 1152 return LIBUSB_ERROR_ACCESS; | |
| 1153 } else if (errno == ENOENT) { | |
| 1154 usbi_err(HANDLE_CTX(handle), "libusb couldn't open USB d
evice %s: " | |
| 1155 "No such file or directory.", filename); | |
| 1156 return LIBUSB_ERROR_NO_DEVICE; | |
| 1157 } else { | |
| 1158 usbi_err(HANDLE_CTX(handle), | |
| 1159 "open failed, code %d errno %d", hpriv->fd, errn
o); | |
| 1160 return LIBUSB_ERROR_IO; | |
| 1161 } | |
| 1162 } | |
| 1163 | |
| 1164 return usbi_add_pollfd(HANDLE_CTX(handle), hpriv->fd, POLLOUT); | |
| 1165 } | |
| 1166 | |
| 1167 static void op_close(struct libusb_device_handle *dev_handle) | |
| 1168 { | |
| 1169 int fd = _device_handle_priv(dev_handle)->fd; | |
| 1170 usbi_remove_pollfd(HANDLE_CTX(dev_handle), fd); | |
| 1171 close(fd); | |
| 1172 } | |
| 1173 | |
| 1174 static int op_get_configuration(struct libusb_device_handle *handle, | |
| 1175 int *config) | |
| 1176 { | |
| 1177 int r; | |
| 1178 if (sysfs_can_relate_devices != 1) | |
| 1179 return LIBUSB_ERROR_NOT_SUPPORTED; | |
| 1180 | |
| 1181 r = sysfs_get_active_config(handle->dev, config); | |
| 1182 if (r < 0) | |
| 1183 return r; | |
| 1184 | |
| 1185 if (*config == -1) | |
| 1186 *config = 0; | |
| 1187 | |
| 1188 return 0; | |
| 1189 } | |
| 1190 | |
| 1191 static int op_set_configuration(struct libusb_device_handle *handle, int config) | |
| 1192 { | |
| 1193 struct linux_device_priv *priv = _device_priv(handle->dev); | |
| 1194 int fd = _device_handle_priv(handle)->fd; | |
| 1195 int r = ioctl(fd, IOCTL_USBFS_SETCONFIG, &config); | |
| 1196 if (r) { | |
| 1197 if (errno == EINVAL) | |
| 1198 return LIBUSB_ERROR_NOT_FOUND; | |
| 1199 else if (errno == EBUSY) | |
| 1200 return LIBUSB_ERROR_BUSY; | |
| 1201 else if (errno == ENODEV) | |
| 1202 return LIBUSB_ERROR_NO_DEVICE; | |
| 1203 | |
| 1204 usbi_err(HANDLE_CTX(handle), "failed, error %d errno %d", r, err
no); | |
| 1205 return LIBUSB_ERROR_OTHER; | |
| 1206 } | |
| 1207 | |
| 1208 if (!sysfs_has_descriptors) { | |
| 1209 /* update our cached active config descriptor */ | |
| 1210 if (config == -1) { | |
| 1211 if (priv->config_descriptor) { | |
| 1212 free(priv->config_descriptor); | |
| 1213 priv->config_descriptor = NULL; | |
| 1214 } | |
| 1215 } else { | |
| 1216 r = cache_active_config(handle->dev, fd, config); | |
| 1217 if (r < 0) | |
| 1218 usbi_warn(HANDLE_CTX(handle), | |
| 1219 "failed to update cached config descript
or, error %d", r); | |
| 1220 } | |
| 1221 } | |
| 1222 | |
| 1223 return 0; | |
| 1224 } | |
| 1225 | |
| 1226 static int op_claim_interface(struct libusb_device_handle *handle, int iface) | |
| 1227 { | |
| 1228 int fd = _device_handle_priv(handle)->fd; | |
| 1229 int r = ioctl(fd, IOCTL_USBFS_CLAIMINTF, &iface); | |
| 1230 if (r) { | |
| 1231 if (errno == ENOENT) | |
| 1232 return LIBUSB_ERROR_NOT_FOUND; | |
| 1233 else if (errno == EBUSY) | |
| 1234 return LIBUSB_ERROR_BUSY; | |
| 1235 else if (errno == ENODEV) | |
| 1236 return LIBUSB_ERROR_NO_DEVICE; | |
| 1237 | |
| 1238 usbi_err(HANDLE_CTX(handle), | |
| 1239 "claim interface failed, error %d errno %d", r, errno); | |
| 1240 return LIBUSB_ERROR_OTHER; | |
| 1241 } | |
| 1242 return 0; | |
| 1243 } | |
| 1244 | |
| 1245 static int op_release_interface(struct libusb_device_handle *handle, int iface) | |
| 1246 { | |
| 1247 int fd = _device_handle_priv(handle)->fd; | |
| 1248 int r = ioctl(fd, IOCTL_USBFS_RELEASEINTF, &iface); | |
| 1249 if (r) { | |
| 1250 if (errno == ENODEV) | |
| 1251 return LIBUSB_ERROR_NO_DEVICE; | |
| 1252 | |
| 1253 usbi_err(HANDLE_CTX(handle), | |
| 1254 "release interface failed, error %d errno %d", r, errno)
; | |
| 1255 return LIBUSB_ERROR_OTHER; | |
| 1256 } | |
| 1257 return 0; | |
| 1258 } | |
| 1259 | |
| 1260 static int op_set_interface(struct libusb_device_handle *handle, int iface, | |
| 1261 int altsetting) | |
| 1262 { | |
| 1263 int fd = _device_handle_priv(handle)->fd; | |
| 1264 struct usbfs_setinterface setintf; | |
| 1265 int r; | |
| 1266 | |
| 1267 setintf.interface = iface; | |
| 1268 setintf.altsetting = altsetting; | |
| 1269 r = ioctl(fd, IOCTL_USBFS_SETINTF, &setintf); | |
| 1270 if (r) { | |
| 1271 if (errno == EINVAL) | |
| 1272 return LIBUSB_ERROR_NOT_FOUND; | |
| 1273 else if (errno == ENODEV) | |
| 1274 return LIBUSB_ERROR_NO_DEVICE; | |
| 1275 | |
| 1276 usbi_err(HANDLE_CTX(handle), | |
| 1277 "setintf failed error %d errno %d", r, errno); | |
| 1278 return LIBUSB_ERROR_OTHER; | |
| 1279 } | |
| 1280 | |
| 1281 return 0; | |
| 1282 } | |
| 1283 | |
| 1284 static int op_clear_halt(struct libusb_device_handle *handle, | |
| 1285 unsigned char endpoint) | |
| 1286 { | |
| 1287 int fd = _device_handle_priv(handle)->fd; | |
| 1288 unsigned int _endpoint = endpoint; | |
| 1289 int r = ioctl(fd, IOCTL_USBFS_CLEAR_HALT, &_endpoint); | |
| 1290 if (r) { | |
| 1291 if (errno == ENOENT) | |
| 1292 return LIBUSB_ERROR_NOT_FOUND; | |
| 1293 else if (errno == ENODEV) | |
| 1294 return LIBUSB_ERROR_NO_DEVICE; | |
| 1295 | |
| 1296 usbi_err(HANDLE_CTX(handle), | |
| 1297 "clear_halt failed error %d errno %d", r, errno); | |
| 1298 return LIBUSB_ERROR_OTHER; | |
| 1299 } | |
| 1300 | |
| 1301 return 0; | |
| 1302 } | |
| 1303 | |
| 1304 static int op_reset_device(struct libusb_device_handle *handle) | |
| 1305 { | |
| 1306 int fd = _device_handle_priv(handle)->fd; | |
| 1307 int i, r, ret = 0; | |
| 1308 | |
| 1309 /* Doing a device reset will cause the usbfs driver to get unbound | |
| 1310 from any interfaces it is bound to. By voluntarily unbinding | |
| 1311 the usbfs driver ourself, we stop the kernel from rebinding | |
| 1312 the interface after reset (which would end up with the interface | |
| 1313 getting bound to the in kernel driver if any). */ | |
| 1314 for (i = 0; i < USB_MAXINTERFACES; i++) { | |
| 1315 if (handle->claimed_interfaces & (1L << i)) { | |
| 1316 op_release_interface(handle, i); | |
| 1317 } | |
| 1318 } | |
| 1319 | |
| 1320 usbi_mutex_lock(&handle->lock); | |
| 1321 r = ioctl(fd, IOCTL_USBFS_RESET, NULL); | |
| 1322 if (r) { | |
| 1323 if (errno == ENODEV) { | |
| 1324 ret = LIBUSB_ERROR_NOT_FOUND; | |
| 1325 goto out; | |
| 1326 } | |
| 1327 | |
| 1328 usbi_err(HANDLE_CTX(handle), | |
| 1329 "reset failed error %d errno %d", r, errno); | |
| 1330 ret = LIBUSB_ERROR_OTHER; | |
| 1331 goto out; | |
| 1332 } | |
| 1333 | |
| 1334 /* And re-claim any interfaces which were claimed before the reset */ | |
| 1335 for (i = 0; i < USB_MAXINTERFACES; i++) { | |
| 1336 if (handle->claimed_interfaces & (1L << i)) { | |
| 1337 r = op_claim_interface(handle, i); | |
| 1338 if (r) { | |
| 1339 usbi_warn(HANDLE_CTX(handle), | |
| 1340 "failed to re-claim interface %d after r
eset", i); | |
| 1341 handle->claimed_interfaces &= ~(1L << i); | |
| 1342 } | |
| 1343 } | |
| 1344 } | |
| 1345 out: | |
| 1346 usbi_mutex_unlock(&handle->lock); | |
| 1347 return ret; | |
| 1348 } | |
| 1349 | |
| 1350 static int op_kernel_driver_active(struct libusb_device_handle *handle, | |
| 1351 int interface) | |
| 1352 { | |
| 1353 int fd = _device_handle_priv(handle)->fd; | |
| 1354 struct usbfs_getdriver getdrv; | |
| 1355 int r; | |
| 1356 | |
| 1357 getdrv.interface = interface; | |
| 1358 r = ioctl(fd, IOCTL_USBFS_GETDRIVER, &getdrv); | |
| 1359 if (r) { | |
| 1360 if (errno == ENODATA) | |
| 1361 return 0; | |
| 1362 else if (errno == ENODEV) | |
| 1363 return LIBUSB_ERROR_NO_DEVICE; | |
| 1364 | |
| 1365 usbi_err(HANDLE_CTX(handle), | |
| 1366 "get driver failed error %d errno %d", r, errno); | |
| 1367 return LIBUSB_ERROR_OTHER; | |
| 1368 } | |
| 1369 | |
| 1370 return 1; | |
| 1371 } | |
| 1372 | |
| 1373 static int op_detach_kernel_driver(struct libusb_device_handle *handle, | |
| 1374 int interface) | |
| 1375 { | |
| 1376 int fd = _device_handle_priv(handle)->fd; | |
| 1377 struct usbfs_ioctl command; | |
| 1378 int r; | |
| 1379 | |
| 1380 command.ifno = interface; | |
| 1381 command.ioctl_code = IOCTL_USBFS_DISCONNECT; | |
| 1382 command.data = NULL; | |
| 1383 | |
| 1384 r = ioctl(fd, IOCTL_USBFS_IOCTL, &command); | |
| 1385 if (r) { | |
| 1386 if (errno == ENODATA) | |
| 1387 return LIBUSB_ERROR_NOT_FOUND; | |
| 1388 else if (errno == EINVAL) | |
| 1389 return LIBUSB_ERROR_INVALID_PARAM; | |
| 1390 else if (errno == ENODEV) | |
| 1391 return LIBUSB_ERROR_NO_DEVICE; | |
| 1392 | |
| 1393 usbi_err(HANDLE_CTX(handle), | |
| 1394 "detach failed error %d errno %d", r, errno); | |
| 1395 return LIBUSB_ERROR_OTHER; | |
| 1396 } | |
| 1397 | |
| 1398 return 0; | |
| 1399 } | |
| 1400 | |
| 1401 static int op_attach_kernel_driver(struct libusb_device_handle *handle, | |
| 1402 int interface) | |
| 1403 { | |
| 1404 int fd = _device_handle_priv(handle)->fd; | |
| 1405 struct usbfs_ioctl command; | |
| 1406 int r; | |
| 1407 | |
| 1408 command.ifno = interface; | |
| 1409 command.ioctl_code = IOCTL_USBFS_CONNECT; | |
| 1410 command.data = NULL; | |
| 1411 | |
| 1412 r = ioctl(fd, IOCTL_USBFS_IOCTL, &command); | |
| 1413 if (r < 0) { | |
| 1414 if (errno == ENODATA) | |
| 1415 return LIBUSB_ERROR_NOT_FOUND; | |
| 1416 else if (errno == EINVAL) | |
| 1417 return LIBUSB_ERROR_INVALID_PARAM; | |
| 1418 else if (errno == ENODEV) | |
| 1419 return LIBUSB_ERROR_NO_DEVICE; | |
| 1420 else if (errno == EBUSY) | |
| 1421 return LIBUSB_ERROR_BUSY; | |
| 1422 | |
| 1423 usbi_err(HANDLE_CTX(handle), | |
| 1424 "attach failed error %d errno %d", r, errno); | |
| 1425 return LIBUSB_ERROR_OTHER; | |
| 1426 } else if (r == 0) { | |
| 1427 return LIBUSB_ERROR_NOT_FOUND; | |
| 1428 } | |
| 1429 | |
| 1430 return 0; | |
| 1431 } | |
| 1432 | |
| 1433 static void op_destroy_device(struct libusb_device *dev) | |
| 1434 { | |
| 1435 struct linux_device_priv *priv = _device_priv(dev); | |
| 1436 if (!sysfs_has_descriptors) { | |
| 1437 if (priv->dev_descriptor) | |
| 1438 free(priv->dev_descriptor); | |
| 1439 if (priv->config_descriptor) | |
| 1440 free(priv->config_descriptor); | |
| 1441 } | |
| 1442 if (priv->sysfs_dir) | |
| 1443 free(priv->sysfs_dir); | |
| 1444 } | |
| 1445 | |
| 1446 /* URBs are discarded in reverse order of submission to avoid races. */ | |
| 1447 static int discard_urbs(struct usbi_transfer *itransfer, int first, int last_plu
s_one) | |
| 1448 { | |
| 1449 struct libusb_transfer *transfer = | |
| 1450 USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer); | |
| 1451 struct linux_transfer_priv *tpriv = | |
| 1452 usbi_transfer_get_os_priv(itransfer); | |
| 1453 struct linux_device_handle_priv *dpriv = | |
| 1454 _device_handle_priv(transfer->dev_handle); | |
| 1455 int i, ret = 0; | |
| 1456 struct usbfs_urb *urb; | |
| 1457 | |
| 1458 for (i = last_plus_one - 1; i >= first; i--) { | |
| 1459 if (LIBUSB_TRANSFER_TYPE_ISOCHRONOUS == transfer->type) | |
| 1460 urb = tpriv->iso_urbs[i]; | |
| 1461 else | |
| 1462 urb = &tpriv->urbs[i]; | |
| 1463 | |
| 1464 if (0 == ioctl(dpriv->fd, IOCTL_USBFS_DISCARDURB, urb)) | |
| 1465 continue; | |
| 1466 | |
| 1467 if (EINVAL == errno) { | |
| 1468 usbi_dbg("URB not found --> assuming ready to be reaped"
); | |
| 1469 ret = LIBUSB_ERROR_NOT_FOUND; | |
| 1470 } else if (ENODEV == errno) { | |
| 1471 usbi_dbg("Device not found for URB --> assuming ready to
be reaped"); | |
| 1472 ret = LIBUSB_ERROR_NO_DEVICE; | |
| 1473 } else { | |
| 1474 usbi_warn(TRANSFER_CTX(transfer), | |
| 1475 "unrecognised discard errno %d", errno); | |
| 1476 ret = LIBUSB_ERROR_OTHER; | |
| 1477 } | |
| 1478 } | |
| 1479 return ret; | |
| 1480 } | |
| 1481 | |
| 1482 static void free_iso_urbs(struct linux_transfer_priv *tpriv) | |
| 1483 { | |
| 1484 int i; | |
| 1485 for (i = 0; i < tpriv->num_urbs; i++) { | |
| 1486 struct usbfs_urb *urb = tpriv->iso_urbs[i]; | |
| 1487 if (!urb) | |
| 1488 break; | |
| 1489 free(urb); | |
| 1490 } | |
| 1491 | |
| 1492 free(tpriv->iso_urbs); | |
| 1493 tpriv->iso_urbs = NULL; | |
| 1494 } | |
| 1495 | |
| 1496 static int submit_bulk_transfer(struct usbi_transfer *itransfer, | |
| 1497 unsigned char urb_type) | |
| 1498 { | |
| 1499 struct libusb_transfer *transfer = | |
| 1500 USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer); | |
| 1501 struct linux_transfer_priv *tpriv = usbi_transfer_get_os_priv(itransfer)
; | |
| 1502 struct linux_device_handle_priv *dpriv = | |
| 1503 _device_handle_priv(transfer->dev_handle); | |
| 1504 struct usbfs_urb *urbs; | |
| 1505 int is_out = (transfer->endpoint & LIBUSB_ENDPOINT_DIR_MASK) | |
| 1506 == LIBUSB_ENDPOINT_OUT; | |
| 1507 int r; | |
| 1508 int i; | |
| 1509 size_t alloc_size; | |
| 1510 | |
| 1511 if (tpriv->urbs) | |
| 1512 return LIBUSB_ERROR_BUSY; | |
| 1513 | |
| 1514 /* usbfs places a 16kb limit on bulk URBs. we divide up larger requests | |
| 1515 * into smaller units to meet such restriction, then fire off all the | |
| 1516 * units at once. it would be simpler if we just fired one unit at a tim
e, | |
| 1517 * but there is a big performance gain through doing it this way. */ | |
| 1518 int num_urbs = transfer->length / MAX_BULK_BUFFER_LENGTH; | |
| 1519 int last_urb_partial = 0; | |
| 1520 | |
| 1521 if (transfer->length == 0) { | |
| 1522 num_urbs = 1; | |
| 1523 } else if ((transfer->length % MAX_BULK_BUFFER_LENGTH) > 0) { | |
| 1524 last_urb_partial = 1; | |
| 1525 num_urbs++; | |
| 1526 } | |
| 1527 usbi_dbg("need %d urbs for new transfer with length %d", num_urbs, | |
| 1528 transfer->length); | |
| 1529 alloc_size = num_urbs * sizeof(struct usbfs_urb); | |
| 1530 urbs = malloc(alloc_size); | |
| 1531 if (!urbs) | |
| 1532 return LIBUSB_ERROR_NO_MEM; | |
| 1533 memset(urbs, 0, alloc_size); | |
| 1534 tpriv->urbs = urbs; | |
| 1535 tpriv->num_urbs = num_urbs; | |
| 1536 tpriv->num_retired = 0; | |
| 1537 tpriv->reap_action = NORMAL; | |
| 1538 tpriv->reap_status = LIBUSB_TRANSFER_COMPLETED; | |
| 1539 | |
| 1540 for (i = 0; i < num_urbs; i++) { | |
| 1541 struct usbfs_urb *urb = &urbs[i]; | |
| 1542 urb->usercontext = itransfer; | |
| 1543 urb->type = urb_type; | |
| 1544 urb->endpoint = transfer->endpoint; | |
| 1545 urb->buffer = transfer->buffer + (i * MAX_BULK_BUFFER_LENGTH); | |
| 1546 if (supports_flag_bulk_continuation && !is_out) | |
| 1547 urb->flags = USBFS_URB_SHORT_NOT_OK; | |
| 1548 if (i == num_urbs - 1 && last_urb_partial) | |
| 1549 urb->buffer_length = transfer->length % MAX_BULK_BUFFER_
LENGTH; | |
| 1550 else if (transfer->length == 0) | |
| 1551 urb->buffer_length = 0; | |
| 1552 else | |
| 1553 urb->buffer_length = MAX_BULK_BUFFER_LENGTH; | |
| 1554 | |
| 1555 if (i > 0 && supports_flag_bulk_continuation) | |
| 1556 urb->flags |= USBFS_URB_BULK_CONTINUATION; | |
| 1557 | |
| 1558 r = ioctl(dpriv->fd, IOCTL_USBFS_SUBMITURB, urb); | |
| 1559 if (r < 0) { | |
| 1560 if (errno == ENODEV) { | |
| 1561 r = LIBUSB_ERROR_NO_DEVICE; | |
| 1562 } else { | |
| 1563 usbi_err(TRANSFER_CTX(transfer), | |
| 1564 "submiturb failed error %d errno=%d", r,
errno); | |
| 1565 r = LIBUSB_ERROR_IO; | |
| 1566 } | |
| 1567 | |
| 1568 /* if the first URB submission fails, we can simply free
up and | |
| 1569 * return failure immediately. */ | |
| 1570 if (i == 0) { | |
| 1571 usbi_dbg("first URB failed, easy peasy"); | |
| 1572 free(urbs); | |
| 1573 tpriv->urbs = NULL; | |
| 1574 return r; | |
| 1575 } | |
| 1576 | |
| 1577 /* if it's not the first URB that failed, the situation
is a bit | |
| 1578 * tricky. we may need to discard all previous URBs. the
re are | |
| 1579 * complications: | |
| 1580 * - discarding is asynchronous - discarded urbs will b
e reaped | |
| 1581 * later. the user must not have freed the transfer w
hen the | |
| 1582 * discarded URBs are reaped, otherwise libusb will b
e using | |
| 1583 * freed memory. | |
| 1584 * - the earlier URBs may have completed successfully a
nd we do | |
| 1585 * not want to throw away any data. | |
| 1586 * - this URB failing may be no error; EREMOTEIO means
that | |
| 1587 * this transfer simply didn't need all the URBs we s
ubmitted | |
| 1588 * so, we report that the transfer was submitted success
fully and | |
| 1589 * in case of error we discard all previous URBs. later
when | |
| 1590 * the final reap completes we can report error to the u
ser, | |
| 1591 * or success if an earlier URB was completed successful
ly. | |
| 1592 */ | |
| 1593 tpriv->reap_action = EREMOTEIO == errno ? COMPLETED_EARL
Y : SUBMIT_FAILED; | |
| 1594 | |
| 1595 /* The URBs we haven't submitted yet we count as already | |
| 1596 * retired. */ | |
| 1597 tpriv->num_retired += num_urbs - i; | |
| 1598 | |
| 1599 /* If we completed short then don't try to discard. */ | |
| 1600 if (COMPLETED_EARLY == tpriv->reap_action) | |
| 1601 return 0; | |
| 1602 | |
| 1603 discard_urbs(itransfer, 0, i); | |
| 1604 | |
| 1605 usbi_dbg("reporting successful submission but waiting fo
r %d " | |
| 1606 "discards before reporting error", i); | |
| 1607 return 0; | |
| 1608 } | |
| 1609 } | |
| 1610 | |
| 1611 return 0; | |
| 1612 } | |
| 1613 | |
| 1614 static int submit_iso_transfer(struct usbi_transfer *itransfer) | |
| 1615 { | |
| 1616 struct libusb_transfer *transfer = | |
| 1617 USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer); | |
| 1618 struct linux_transfer_priv *tpriv = usbi_transfer_get_os_priv(itransfer)
; | |
| 1619 struct linux_device_handle_priv *dpriv = | |
| 1620 _device_handle_priv(transfer->dev_handle); | |
| 1621 struct usbfs_urb **urbs; | |
| 1622 size_t alloc_size; | |
| 1623 int num_packets = transfer->num_iso_packets; | |
| 1624 int i; | |
| 1625 int this_urb_len = 0; | |
| 1626 int num_urbs = 1; | |
| 1627 int packet_offset = 0; | |
| 1628 unsigned int packet_len; | |
| 1629 unsigned char *urb_buffer = transfer->buffer; | |
| 1630 | |
| 1631 if (tpriv->iso_urbs) | |
| 1632 return LIBUSB_ERROR_BUSY; | |
| 1633 | |
| 1634 /* usbfs places a 32kb limit on iso URBs. we divide up larger requests | |
| 1635 * into smaller units to meet such restriction, then fire off all the | |
| 1636 * units at once. it would be simpler if we just fired one unit at a tim
e, | |
| 1637 * but there is a big performance gain through doing it this way. */ | |
| 1638 | |
| 1639 /* calculate how many URBs we need */ | |
| 1640 for (i = 0; i < num_packets; i++) { | |
| 1641 int space_remaining = MAX_ISO_BUFFER_LENGTH - this_urb_len; | |
| 1642 packet_len = transfer->iso_packet_desc[i].length; | |
| 1643 | |
| 1644 if (packet_len > space_remaining) { | |
| 1645 num_urbs++; | |
| 1646 this_urb_len = packet_len; | |
| 1647 } else { | |
| 1648 this_urb_len += packet_len; | |
| 1649 } | |
| 1650 } | |
| 1651 usbi_dbg("need %d 32k URBs for transfer", num_urbs); | |
| 1652 | |
| 1653 alloc_size = num_urbs * sizeof(*urbs); | |
| 1654 urbs = malloc(alloc_size); | |
| 1655 if (!urbs) | |
| 1656 return LIBUSB_ERROR_NO_MEM; | |
| 1657 memset(urbs, 0, alloc_size); | |
| 1658 | |
| 1659 tpriv->iso_urbs = urbs; | |
| 1660 tpriv->num_urbs = num_urbs; | |
| 1661 tpriv->num_retired = 0; | |
| 1662 tpriv->reap_action = NORMAL; | |
| 1663 tpriv->iso_packet_offset = 0; | |
| 1664 | |
| 1665 /* allocate + initialize each URB with the correct number of packets */ | |
| 1666 for (i = 0; i < num_urbs; i++) { | |
| 1667 struct usbfs_urb *urb; | |
| 1668 int space_remaining_in_urb = MAX_ISO_BUFFER_LENGTH; | |
| 1669 int urb_packet_offset = 0; | |
| 1670 unsigned char *urb_buffer_orig = urb_buffer; | |
| 1671 int j; | |
| 1672 int k; | |
| 1673 | |
| 1674 /* swallow up all the packets we can fit into this URB */ | |
| 1675 while (packet_offset < transfer->num_iso_packets) { | |
| 1676 packet_len = transfer->iso_packet_desc[packet_offset].le
ngth; | |
| 1677 if (packet_len <= space_remaining_in_urb) { | |
| 1678 /* throw it in */ | |
| 1679 urb_packet_offset++; | |
| 1680 packet_offset++; | |
| 1681 space_remaining_in_urb -= packet_len; | |
| 1682 urb_buffer += packet_len; | |
| 1683 } else { | |
| 1684 /* it can't fit, save it for the next URB */ | |
| 1685 break; | |
| 1686 } | |
| 1687 } | |
| 1688 | |
| 1689 alloc_size = sizeof(*urb) | |
| 1690 + (urb_packet_offset * sizeof(struct usbfs_iso_packet_de
sc)); | |
| 1691 urb = malloc(alloc_size); | |
| 1692 if (!urb) { | |
| 1693 free_iso_urbs(tpriv); | |
| 1694 return LIBUSB_ERROR_NO_MEM; | |
| 1695 } | |
| 1696 memset(urb, 0, alloc_size); | |
| 1697 urbs[i] = urb; | |
| 1698 | |
| 1699 /* populate packet lengths */ | |
| 1700 for (j = 0, k = packet_offset - urb_packet_offset; | |
| 1701 k < packet_offset; k++, j++) { | |
| 1702 packet_len = transfer->iso_packet_desc[k].length; | |
| 1703 urb->iso_frame_desc[j].length = packet_len; | |
| 1704 } | |
| 1705 | |
| 1706 urb->usercontext = itransfer; | |
| 1707 urb->type = USBFS_URB_TYPE_ISO; | |
| 1708 /* FIXME: interface for non-ASAP data? */ | |
| 1709 urb->flags = USBFS_URB_ISO_ASAP; | |
| 1710 urb->endpoint = transfer->endpoint; | |
| 1711 urb->number_of_packets = urb_packet_offset; | |
| 1712 urb->buffer = urb_buffer_orig; | |
| 1713 } | |
| 1714 | |
| 1715 /* submit URBs */ | |
| 1716 for (i = 0; i < num_urbs; i++) { | |
| 1717 int r = ioctl(dpriv->fd, IOCTL_USBFS_SUBMITURB, urbs[i]); | |
| 1718 if (r < 0) { | |
| 1719 if (errno == ENODEV) { | |
| 1720 r = LIBUSB_ERROR_NO_DEVICE; | |
| 1721 } else { | |
| 1722 usbi_err(TRANSFER_CTX(transfer), | |
| 1723 "submiturb failed error %d errno=%d", r,
errno); | |
| 1724 r = LIBUSB_ERROR_IO; | |
| 1725 } | |
| 1726 | |
| 1727 /* if the first URB submission fails, we can simply free
up and | |
| 1728 * return failure immediately. */ | |
| 1729 if (i == 0) { | |
| 1730 usbi_dbg("first URB failed, easy peasy"); | |
| 1731 free_iso_urbs(tpriv); | |
| 1732 return r; | |
| 1733 } | |
| 1734 | |
| 1735 /* if it's not the first URB that failed, the situation
is a bit | |
| 1736 * tricky. we must discard all previous URBs. there are | |
| 1737 * complications: | |
| 1738 * - discarding is asynchronous - discarded urbs will b
e reaped | |
| 1739 * later. the user must not have freed the transfer w
hen the | |
| 1740 * discarded URBs are reaped, otherwise libusb will b
e using | |
| 1741 * freed memory. | |
| 1742 * - the earlier URBs may have completed successfully a
nd we do | |
| 1743 * not want to throw away any data. | |
| 1744 * so, in this case we discard all the previous URBs BUT
we report | |
| 1745 * that the transfer was submitted successfully. then la
ter when | |
| 1746 * the final discard completes we can report error to th
e user. | |
| 1747 */ | |
| 1748 tpriv->reap_action = SUBMIT_FAILED; | |
| 1749 | |
| 1750 /* The URBs we haven't submitted yet we count as already | |
| 1751 * retired. */ | |
| 1752 tpriv->num_retired = num_urbs - i; | |
| 1753 discard_urbs(itransfer, 0, i); | |
| 1754 | |
| 1755 usbi_dbg("reporting successful submission but waiting fo
r %d " | |
| 1756 "discards before reporting error", i); | |
| 1757 return 0; | |
| 1758 } | |
| 1759 } | |
| 1760 | |
| 1761 return 0; | |
| 1762 } | |
| 1763 | |
| 1764 static int submit_control_transfer(struct usbi_transfer *itransfer) | |
| 1765 { | |
| 1766 struct linux_transfer_priv *tpriv = usbi_transfer_get_os_priv(itransfer)
; | |
| 1767 struct libusb_transfer *transfer = | |
| 1768 USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer); | |
| 1769 struct linux_device_handle_priv *dpriv = | |
| 1770 _device_handle_priv(transfer->dev_handle); | |
| 1771 struct usbfs_urb *urb; | |
| 1772 int r; | |
| 1773 | |
| 1774 if (tpriv->urbs) | |
| 1775 return LIBUSB_ERROR_BUSY; | |
| 1776 | |
| 1777 if (transfer->length - LIBUSB_CONTROL_SETUP_SIZE > MAX_CTRL_BUFFER_LENGT
H) | |
| 1778 return LIBUSB_ERROR_INVALID_PARAM; | |
| 1779 | |
| 1780 urb = malloc(sizeof(struct usbfs_urb)); | |
| 1781 if (!urb) | |
| 1782 return LIBUSB_ERROR_NO_MEM; | |
| 1783 memset(urb, 0, sizeof(struct usbfs_urb)); | |
| 1784 tpriv->urbs = urb; | |
| 1785 tpriv->num_urbs = 1; | |
| 1786 tpriv->reap_action = NORMAL; | |
| 1787 | |
| 1788 urb->usercontext = itransfer; | |
| 1789 urb->type = USBFS_URB_TYPE_CONTROL; | |
| 1790 urb->endpoint = transfer->endpoint; | |
| 1791 urb->buffer = transfer->buffer; | |
| 1792 urb->buffer_length = transfer->length; | |
| 1793 | |
| 1794 r = ioctl(dpriv->fd, IOCTL_USBFS_SUBMITURB, urb); | |
| 1795 if (r < 0) { | |
| 1796 free(urb); | |
| 1797 tpriv->urbs = NULL; | |
| 1798 if (errno == ENODEV) | |
| 1799 return LIBUSB_ERROR_NO_DEVICE; | |
| 1800 | |
| 1801 usbi_err(TRANSFER_CTX(transfer), | |
| 1802 "submiturb failed error %d errno=%d", r, errno); | |
| 1803 return LIBUSB_ERROR_IO; | |
| 1804 } | |
| 1805 return 0; | |
| 1806 } | |
| 1807 | |
| 1808 static int op_submit_transfer(struct usbi_transfer *itransfer) | |
| 1809 { | |
| 1810 struct libusb_transfer *transfer = | |
| 1811 USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer); | |
| 1812 | |
| 1813 switch (transfer->type) { | |
| 1814 case LIBUSB_TRANSFER_TYPE_CONTROL: | |
| 1815 return submit_control_transfer(itransfer); | |
| 1816 case LIBUSB_TRANSFER_TYPE_BULK: | |
| 1817 return submit_bulk_transfer(itransfer, USBFS_URB_TYPE_BULK); | |
| 1818 case LIBUSB_TRANSFER_TYPE_INTERRUPT: | |
| 1819 return submit_bulk_transfer(itransfer, USBFS_URB_TYPE_INTERRUPT)
; | |
| 1820 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS: | |
| 1821 return submit_iso_transfer(itransfer); | |
| 1822 default: | |
| 1823 usbi_err(TRANSFER_CTX(transfer), | |
| 1824 "unknown endpoint type %d", transfer->type); | |
| 1825 return LIBUSB_ERROR_INVALID_PARAM; | |
| 1826 } | |
| 1827 } | |
| 1828 | |
| 1829 static int op_cancel_transfer(struct usbi_transfer *itransfer) | |
| 1830 { | |
| 1831 struct linux_transfer_priv *tpriv = usbi_transfer_get_os_priv(itransfer)
; | |
| 1832 struct libusb_transfer *transfer = | |
| 1833 USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer); | |
| 1834 | |
| 1835 switch (transfer->type) { | |
| 1836 case LIBUSB_TRANSFER_TYPE_BULK: | |
| 1837 if (tpriv->reap_action == ERROR) | |
| 1838 break; | |
| 1839 /* else, fall through */ | |
| 1840 case LIBUSB_TRANSFER_TYPE_CONTROL: | |
| 1841 case LIBUSB_TRANSFER_TYPE_INTERRUPT: | |
| 1842 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS: | |
| 1843 tpriv->reap_action = CANCELLED; | |
| 1844 break; | |
| 1845 default: | |
| 1846 usbi_err(TRANSFER_CTX(transfer), | |
| 1847 "unknown endpoint type %d", transfer->type); | |
| 1848 return LIBUSB_ERROR_INVALID_PARAM; | |
| 1849 } | |
| 1850 | |
| 1851 if (!tpriv->urbs) | |
| 1852 return LIBUSB_ERROR_NOT_FOUND; | |
| 1853 | |
| 1854 return discard_urbs(itransfer, 0, tpriv->num_urbs); | |
| 1855 } | |
| 1856 | |
| 1857 static void op_clear_transfer_priv(struct usbi_transfer *itransfer) | |
| 1858 { | |
| 1859 struct libusb_transfer *transfer = | |
| 1860 USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer); | |
| 1861 struct linux_transfer_priv *tpriv = usbi_transfer_get_os_priv(itransfer)
; | |
| 1862 | |
| 1863 /* urbs can be freed also in submit_transfer so lock mutex first */ | |
| 1864 switch (transfer->type) { | |
| 1865 case LIBUSB_TRANSFER_TYPE_CONTROL: | |
| 1866 case LIBUSB_TRANSFER_TYPE_BULK: | |
| 1867 case LIBUSB_TRANSFER_TYPE_INTERRUPT: | |
| 1868 usbi_mutex_lock(&itransfer->lock); | |
| 1869 if (tpriv->urbs) | |
| 1870 free(tpriv->urbs); | |
| 1871 tpriv->urbs = NULL; | |
| 1872 usbi_mutex_unlock(&itransfer->lock); | |
| 1873 break; | |
| 1874 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS: | |
| 1875 usbi_mutex_lock(&itransfer->lock); | |
| 1876 if (tpriv->iso_urbs) | |
| 1877 free_iso_urbs(tpriv); | |
| 1878 usbi_mutex_unlock(&itransfer->lock); | |
| 1879 break; | |
| 1880 default: | |
| 1881 usbi_err(TRANSFER_CTX(transfer), | |
| 1882 "unknown endpoint type %d", transfer->type); | |
| 1883 } | |
| 1884 } | |
| 1885 | |
| 1886 static int handle_bulk_completion(struct usbi_transfer *itransfer, | |
| 1887 struct usbfs_urb *urb) | |
| 1888 { | |
| 1889 struct linux_transfer_priv *tpriv = usbi_transfer_get_os_priv(itransfer)
; | |
| 1890 struct libusb_transfer *transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itra
nsfer); | |
| 1891 int urb_idx = urb - tpriv->urbs; | |
| 1892 | |
| 1893 usbi_mutex_lock(&itransfer->lock); | |
| 1894 usbi_dbg("handling completion status %d of bulk urb %d/%d", urb->status, | |
| 1895 urb_idx + 1, tpriv->num_urbs); | |
| 1896 | |
| 1897 tpriv->num_retired++; | |
| 1898 | |
| 1899 if (tpriv->reap_action != NORMAL) { | |
| 1900 /* cancelled, submit_fail, or completed early */ | |
| 1901 usbi_dbg("abnormal reap: urb status %d", urb->status); | |
| 1902 | |
| 1903 /* even though we're in the process of cancelling, it's possible
that | |
| 1904 * we may receive some data in these URBs that we don't want to
lose. | |
| 1905 * examples: | |
| 1906 * 1. while the kernel is cancelling all the packets that make u
p an | |
| 1907 * URB, a few of them might complete. so we get back a succes
sful | |
| 1908 * cancellation *and* some data. | |
| 1909 * 2. we receive a short URB which marks the early completion co
ndition, | |
| 1910 * so we start cancelling the remaining URBs. however, we're
too | |
| 1911 * slow and another URB completes (or at least completes part
ially). | |
| 1912 * (this can't happen since we always use BULK_CONTINUATION.) | |
| 1913 * | |
| 1914 * When this happens, our objectives are not to lose any "surplu
s" data, | |
| 1915 * and also to stick it at the end of the previously-received da
ta | |
| 1916 * (closing any holes), so that libusb reports the total amount
of | |
| 1917 * transferred data and presents it in a contiguous chunk. | |
| 1918 */ | |
| 1919 if (urb->actual_length > 0) { | |
| 1920 unsigned char *target = transfer->buffer + itransfer->tr
ansferred; | |
| 1921 usbi_dbg("received %d bytes of surplus data", urb->actua
l_length); | |
| 1922 if (urb->buffer != target) { | |
| 1923 usbi_dbg("moving surplus data from offset %d to
offset %d", | |
| 1924 (unsigned char *) urb->buffer - transfer
->buffer, | |
| 1925 target - transfer->buffer); | |
| 1926 memmove(target, urb->buffer, urb->actual_length)
; | |
| 1927 } | |
| 1928 itransfer->transferred += urb->actual_length; | |
| 1929 } | |
| 1930 | |
| 1931 if (tpriv->num_retired == tpriv->num_urbs) { | |
| 1932 usbi_dbg("abnormal reap: last URB handled, reporting"); | |
| 1933 if (tpriv->reap_action != COMPLETED_EARLY && | |
| 1934 tpriv->reap_status == LIBUSB_TRANSFER_COMPLETED) | |
| 1935 tpriv->reap_status = LIBUSB_TRANSFER_ERROR; | |
| 1936 goto completed; | |
| 1937 } | |
| 1938 goto out_unlock; | |
| 1939 } | |
| 1940 | |
| 1941 itransfer->transferred += urb->actual_length; | |
| 1942 | |
| 1943 /* Many of these errors can occur on *any* urb of a multi-urb | |
| 1944 * transfer. When they do, we tear down the rest of the transfer. | |
| 1945 */ | |
| 1946 switch (urb->status) { | |
| 1947 case 0: | |
| 1948 break; | |
| 1949 case -EREMOTEIO: /* short transfer */ | |
| 1950 break; | |
| 1951 case -ENOENT: /* cancelled */ | |
| 1952 case -ECONNRESET: | |
| 1953 break; | |
| 1954 case -ESHUTDOWN: | |
| 1955 usbi_dbg("device removed"); | |
| 1956 tpriv->reap_status = LIBUSB_TRANSFER_NO_DEVICE; | |
| 1957 goto cancel_remaining; | |
| 1958 case -EPIPE: | |
| 1959 usbi_dbg("detected endpoint stall"); | |
| 1960 if (tpriv->reap_status == LIBUSB_TRANSFER_COMPLETED) | |
| 1961 tpriv->reap_status = LIBUSB_TRANSFER_STALL; | |
| 1962 goto cancel_remaining; | |
| 1963 case -EOVERFLOW: | |
| 1964 /* overflow can only ever occur in the last urb */ | |
| 1965 usbi_dbg("overflow, actual_length=%d", urb->actual_length); | |
| 1966 if (tpriv->reap_status == LIBUSB_TRANSFER_COMPLETED) | |
| 1967 tpriv->reap_status = LIBUSB_TRANSFER_OVERFLOW; | |
| 1968 goto completed; | |
| 1969 case -ETIME: | |
| 1970 case -EPROTO: | |
| 1971 case -EILSEQ: | |
| 1972 usbi_dbg("low level error %d", urb->status); | |
| 1973 tpriv->reap_action = ERROR; | |
| 1974 goto cancel_remaining; | |
| 1975 default: | |
| 1976 usbi_warn(ITRANSFER_CTX(itransfer), | |
| 1977 "unrecognised urb status %d", urb->status); | |
| 1978 tpriv->reap_action = ERROR; | |
| 1979 goto cancel_remaining; | |
| 1980 } | |
| 1981 | |
| 1982 /* if we're the last urb or we got less data than requested then we're | |
| 1983 * done */ | |
| 1984 if (urb_idx == tpriv->num_urbs - 1) { | |
| 1985 usbi_dbg("last URB in transfer --> complete!"); | |
| 1986 goto completed; | |
| 1987 } else if (urb->actual_length < urb->buffer_length) { | |
| 1988 usbi_dbg("short transfer %d/%d --> complete!", | |
| 1989 urb->actual_length, urb->buffer_length); | |
| 1990 if (tpriv->reap_action == NORMAL) | |
| 1991 tpriv->reap_action = COMPLETED_EARLY; | |
| 1992 } else | |
| 1993 goto out_unlock; | |
| 1994 | |
| 1995 cancel_remaining: | |
| 1996 if (ERROR == tpriv->reap_action && LIBUSB_TRANSFER_COMPLETED == tpriv->r
eap_status) | |
| 1997 tpriv->reap_status = LIBUSB_TRANSFER_ERROR; | |
| 1998 | |
| 1999 if (tpriv->num_retired == tpriv->num_urbs) /* nothing to cancel */ | |
| 2000 goto completed; | |
| 2001 | |
| 2002 /* cancel remaining urbs and wait for their completion before | |
| 2003 * reporting results */ | |
| 2004 discard_urbs(itransfer, urb_idx + 1, tpriv->num_urbs); | |
| 2005 | |
| 2006 out_unlock: | |
| 2007 usbi_mutex_unlock(&itransfer->lock); | |
| 2008 return 0; | |
| 2009 | |
| 2010 completed: | |
| 2011 free(tpriv->urbs); | |
| 2012 tpriv->urbs = NULL; | |
| 2013 usbi_mutex_unlock(&itransfer->lock); | |
| 2014 return CANCELLED == tpriv->reap_action ? | |
| 2015 usbi_handle_transfer_cancellation(itransfer) : | |
| 2016 usbi_handle_transfer_completion(itransfer, tpriv->reap_status); | |
| 2017 } | |
| 2018 | |
| 2019 static int handle_iso_completion(struct usbi_transfer *itransfer, | |
| 2020 struct usbfs_urb *urb) | |
| 2021 { | |
| 2022 struct libusb_transfer *transfer = | |
| 2023 USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer); | |
| 2024 struct linux_transfer_priv *tpriv = usbi_transfer_get_os_priv(itransfer)
; | |
| 2025 int num_urbs = tpriv->num_urbs; | |
| 2026 int urb_idx = 0; | |
| 2027 int i; | |
| 2028 enum libusb_transfer_status status = LIBUSB_TRANSFER_COMPLETED; | |
| 2029 | |
| 2030 usbi_mutex_lock(&itransfer->lock); | |
| 2031 for (i = 0; i < num_urbs; i++) { | |
| 2032 if (urb == tpriv->iso_urbs[i]) { | |
| 2033 urb_idx = i + 1; | |
| 2034 break; | |
| 2035 } | |
| 2036 } | |
| 2037 if (urb_idx == 0) { | |
| 2038 usbi_err(TRANSFER_CTX(transfer), "could not locate urb!"); | |
| 2039 usbi_mutex_unlock(&itransfer->lock); | |
| 2040 return LIBUSB_ERROR_NOT_FOUND; | |
| 2041 } | |
| 2042 | |
| 2043 usbi_dbg("handling completion status %d of iso urb %d/%d", urb->status, | |
| 2044 urb_idx, num_urbs); | |
| 2045 | |
| 2046 /* copy isochronous results back in */ | |
| 2047 | |
| 2048 for (i = 0; i < urb->number_of_packets; i++) { | |
| 2049 struct usbfs_iso_packet_desc *urb_desc = &urb->iso_frame_desc[i]
; | |
| 2050 struct libusb_iso_packet_descriptor *lib_desc = | |
| 2051 &transfer->iso_packet_desc[tpriv->iso_packet_offset++]; | |
| 2052 lib_desc->status = urb_desc->status; | |
| 2053 lib_desc->actual_length = urb_desc->actual_length; | |
| 2054 } | |
| 2055 | |
| 2056 tpriv->num_retired++; | |
| 2057 | |
| 2058 if (tpriv->reap_action != NORMAL) { /* cancelled or submit_fail */ | |
| 2059 usbi_dbg("CANCEL: urb status %d", urb->status); | |
| 2060 | |
| 2061 if (status == LIBUSB_TRANSFER_COMPLETED) | |
| 2062 status = LIBUSB_TRANSFER_ERROR; | |
| 2063 | |
| 2064 if (tpriv->num_retired == num_urbs) { | |
| 2065 usbi_dbg("CANCEL: last URB handled, reporting"); | |
| 2066 free_iso_urbs(tpriv); | |
| 2067 if (tpriv->reap_action == CANCELLED) { | |
| 2068 usbi_mutex_unlock(&itransfer->lock); | |
| 2069 return usbi_handle_transfer_cancellation(itransf
er); | |
| 2070 } else { | |
| 2071 usbi_mutex_unlock(&itransfer->lock); | |
| 2072 return usbi_handle_transfer_completion(itransfer
, | |
| 2073 LIBUSB_TRANSFER_ERROR); | |
| 2074 } | |
| 2075 } | |
| 2076 goto out; | |
| 2077 } | |
| 2078 | |
| 2079 switch (urb->status) { | |
| 2080 case 0: | |
| 2081 break; | |
| 2082 case -ENOENT: /* cancelled */ | |
| 2083 break; | |
| 2084 case -ESHUTDOWN: | |
| 2085 usbi_dbg("device removed"); | |
| 2086 status = LIBUSB_TRANSFER_NO_DEVICE; | |
| 2087 break; | |
| 2088 case -ETIME: | |
| 2089 case -EPROTO: | |
| 2090 case -EILSEQ: | |
| 2091 usbi_dbg("low-level USB error %d", urb->status); | |
| 2092 break; | |
| 2093 default: | |
| 2094 usbi_warn(TRANSFER_CTX(transfer), | |
| 2095 "unrecognised urb status %d", urb->status); | |
| 2096 break; | |
| 2097 } | |
| 2098 | |
| 2099 /* if we're the last urb or we got less data than requested then we're | |
| 2100 * done */ | |
| 2101 if (urb_idx == num_urbs) { | |
| 2102 usbi_dbg("last URB in transfer --> complete!"); | |
| 2103 free_iso_urbs(tpriv); | |
| 2104 usbi_mutex_unlock(&itransfer->lock); | |
| 2105 return usbi_handle_transfer_completion(itransfer, status); | |
| 2106 } | |
| 2107 | |
| 2108 out: | |
| 2109 usbi_mutex_unlock(&itransfer->lock); | |
| 2110 return 0; | |
| 2111 } | |
| 2112 | |
| 2113 static int handle_control_completion(struct usbi_transfer *itransfer, | |
| 2114 struct usbfs_urb *urb) | |
| 2115 { | |
| 2116 struct linux_transfer_priv *tpriv = usbi_transfer_get_os_priv(itransfer)
; | |
| 2117 int status; | |
| 2118 | |
| 2119 usbi_mutex_lock(&itransfer->lock); | |
| 2120 usbi_dbg("handling completion status %d", urb->status); | |
| 2121 | |
| 2122 itransfer->transferred += urb->actual_length; | |
| 2123 | |
| 2124 if (tpriv->reap_action == CANCELLED) { | |
| 2125 if (urb->status != 0 && urb->status != -ENOENT) | |
| 2126 usbi_warn(ITRANSFER_CTX(itransfer), | |
| 2127 "cancel: unrecognised urb status %d", urb->statu
s); | |
| 2128 free(tpriv->urbs); | |
| 2129 tpriv->urbs = NULL; | |
| 2130 usbi_mutex_unlock(&itransfer->lock); | |
| 2131 return usbi_handle_transfer_cancellation(itransfer); | |
| 2132 } | |
| 2133 | |
| 2134 switch (urb->status) { | |
| 2135 case 0: | |
| 2136 status = LIBUSB_TRANSFER_COMPLETED; | |
| 2137 break; | |
| 2138 case -ENOENT: /* cancelled */ | |
| 2139 status = LIBUSB_TRANSFER_CANCELLED; | |
| 2140 break; | |
| 2141 case -ESHUTDOWN: | |
| 2142 usbi_dbg("device removed"); | |
| 2143 status = LIBUSB_TRANSFER_NO_DEVICE; | |
| 2144 break; | |
| 2145 case -EPIPE: | |
| 2146 usbi_dbg("unsupported control request"); | |
| 2147 status = LIBUSB_TRANSFER_STALL; | |
| 2148 break; | |
| 2149 case -ETIME: | |
| 2150 case -EPROTO: | |
| 2151 case -EILSEQ: | |
| 2152 usbi_dbg("low-level bus error occurred"); | |
| 2153 status = LIBUSB_TRANSFER_ERROR; | |
| 2154 break; | |
| 2155 default: | |
| 2156 usbi_warn(ITRANSFER_CTX(itransfer), | |
| 2157 "unrecognised urb status %d", urb->status); | |
| 2158 status = LIBUSB_TRANSFER_ERROR; | |
| 2159 break; | |
| 2160 } | |
| 2161 | |
| 2162 free(tpriv->urbs); | |
| 2163 tpriv->urbs = NULL; | |
| 2164 usbi_mutex_unlock(&itransfer->lock); | |
| 2165 return usbi_handle_transfer_completion(itransfer, status); | |
| 2166 } | |
| 2167 | |
| 2168 static int reap_for_handle(struct libusb_device_handle *handle) | |
| 2169 { | |
| 2170 struct linux_device_handle_priv *hpriv = _device_handle_priv(handle); | |
| 2171 int r; | |
| 2172 struct usbfs_urb *urb; | |
| 2173 struct usbi_transfer *itransfer; | |
| 2174 struct libusb_transfer *transfer; | |
| 2175 | |
| 2176 r = ioctl(hpriv->fd, IOCTL_USBFS_REAPURBNDELAY, &urb); | |
| 2177 if (r == -1 && errno == EAGAIN) | |
| 2178 return 1; | |
| 2179 if (r < 0) { | |
| 2180 if (errno == ENODEV) | |
| 2181 return LIBUSB_ERROR_NO_DEVICE; | |
| 2182 | |
| 2183 usbi_err(HANDLE_CTX(handle), "reap failed error %d errno=%d", | |
| 2184 r, errno); | |
| 2185 return LIBUSB_ERROR_IO; | |
| 2186 } | |
| 2187 | |
| 2188 itransfer = urb->usercontext; | |
| 2189 transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer); | |
| 2190 | |
| 2191 usbi_dbg("urb type=%d status=%d transferred=%d", urb->type, urb->status, | |
| 2192 urb->actual_length); | |
| 2193 | |
| 2194 switch (transfer->type) { | |
| 2195 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS: | |
| 2196 return handle_iso_completion(itransfer, urb); | |
| 2197 case LIBUSB_TRANSFER_TYPE_BULK: | |
| 2198 case LIBUSB_TRANSFER_TYPE_INTERRUPT: | |
| 2199 return handle_bulk_completion(itransfer, urb); | |
| 2200 case LIBUSB_TRANSFER_TYPE_CONTROL: | |
| 2201 return handle_control_completion(itransfer, urb); | |
| 2202 default: | |
| 2203 usbi_err(HANDLE_CTX(handle), "unrecognised endpoint type %x", | |
| 2204 transfer->type); | |
| 2205 return LIBUSB_ERROR_OTHER; | |
| 2206 } | |
| 2207 } | |
| 2208 | |
| 2209 static int op_handle_events(struct libusb_context *ctx, | |
| 2210 struct pollfd *fds, POLL_NFDS_TYPE nfds, int num_ready) | |
| 2211 { | |
| 2212 int r; | |
| 2213 int i = 0; | |
| 2214 | |
| 2215 usbi_mutex_lock(&ctx->open_devs_lock); | |
| 2216 for (i = 0; i < nfds && num_ready > 0; i++) { | |
| 2217 struct pollfd *pollfd = &fds[i]; | |
| 2218 struct libusb_device_handle *handle; | |
| 2219 struct linux_device_handle_priv *hpriv = NULL; | |
| 2220 | |
| 2221 if (!pollfd->revents) | |
| 2222 continue; | |
| 2223 | |
| 2224 num_ready--; | |
| 2225 list_for_each_entry(handle, &ctx->open_devs, list, struct libusb
_device_handle) { | |
| 2226 hpriv = _device_handle_priv(handle); | |
| 2227 if (hpriv->fd == pollfd->fd) | |
| 2228 break; | |
| 2229 } | |
| 2230 | |
| 2231 if (pollfd->revents & POLLERR) { | |
| 2232 usbi_remove_pollfd(HANDLE_CTX(handle), hpriv->fd); | |
| 2233 usbi_handle_disconnect(handle); | |
| 2234 continue; | |
| 2235 } | |
| 2236 | |
| 2237 r = reap_for_handle(handle); | |
| 2238 if (r == 1 || r == LIBUSB_ERROR_NO_DEVICE) | |
| 2239 continue; | |
| 2240 else if (r < 0) | |
| 2241 goto out; | |
| 2242 } | |
| 2243 | |
| 2244 r = 0; | |
| 2245 out: | |
| 2246 usbi_mutex_unlock(&ctx->open_devs_lock); | |
| 2247 return r; | |
| 2248 } | |
| 2249 | |
| 2250 static int op_clock_gettime(int clk_id, struct timespec *tp) | |
| 2251 { | |
| 2252 switch (clk_id) { | |
| 2253 case USBI_CLOCK_MONOTONIC: | |
| 2254 return clock_gettime(monotonic_clkid, tp); | |
| 2255 case USBI_CLOCK_REALTIME: | |
| 2256 return clock_gettime(CLOCK_REALTIME, tp); | |
| 2257 default: | |
| 2258 return LIBUSB_ERROR_INVALID_PARAM; | |
| 2259 } | |
| 2260 } | |
| 2261 | |
| 2262 #ifdef USBI_TIMERFD_AVAILABLE | |
| 2263 static clockid_t op_get_timerfd_clockid(void) | |
| 2264 { | |
| 2265 return monotonic_clkid; | |
| 2266 | |
| 2267 } | |
| 2268 #endif | |
| 2269 | |
| 2270 const struct usbi_os_backend linux_usbfs_backend = { | |
| 2271 .name = "Linux usbfs", | |
| 2272 .init = op_init, | |
| 2273 .exit = NULL, | |
| 2274 .get_device_list = op_get_device_list, | |
| 2275 .get_device_descriptor = op_get_device_descriptor, | |
| 2276 .get_active_config_descriptor = op_get_active_config_descriptor, | |
| 2277 .get_config_descriptor = op_get_config_descriptor, | |
| 2278 | |
| 2279 .open = op_open, | |
| 2280 .close = op_close, | |
| 2281 .get_configuration = op_get_configuration, | |
| 2282 .set_configuration = op_set_configuration, | |
| 2283 .claim_interface = op_claim_interface, | |
| 2284 .release_interface = op_release_interface, | |
| 2285 | |
| 2286 .set_interface_altsetting = op_set_interface, | |
| 2287 .clear_halt = op_clear_halt, | |
| 2288 .reset_device = op_reset_device, | |
| 2289 | |
| 2290 .kernel_driver_active = op_kernel_driver_active, | |
| 2291 .detach_kernel_driver = op_detach_kernel_driver, | |
| 2292 .attach_kernel_driver = op_attach_kernel_driver, | |
| 2293 | |
| 2294 .destroy_device = op_destroy_device, | |
| 2295 | |
| 2296 .submit_transfer = op_submit_transfer, | |
| 2297 .cancel_transfer = op_cancel_transfer, | |
| 2298 .clear_transfer_priv = op_clear_transfer_priv, | |
| 2299 | |
| 2300 .handle_events = op_handle_events, | |
| 2301 | |
| 2302 .clock_gettime = op_clock_gettime, | |
| 2303 | |
| 2304 #ifdef USBI_TIMERFD_AVAILABLE | |
| 2305 .get_timerfd_clockid = op_get_timerfd_clockid, | |
| 2306 #endif | |
| 2307 | |
| 2308 .device_priv_size = sizeof(struct linux_device_priv), | |
| 2309 .device_handle_priv_size = sizeof(struct linux_device_handle_priv), | |
| 2310 .transfer_priv_size = sizeof(struct linux_transfer_priv), | |
| 2311 .add_iso_packet_size = 0, | |
| 2312 }; | |
| 2313 | |
| OLD | NEW |