1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 // #define DEBUG 1 18 #if DEBUG 19 20 #ifdef USE_LIBLOG 21 #define LOG_TAG "usbhost" 22 #include "utils/Log.h" 23 #define D ALOGD 24 #else 25 #define D printf 26 #endif 27 28 #else 29 #define D(...) 30 #endif 31 32 #include <stdio.h> 33 #include <stdlib.h> 34 #include <unistd.h> 35 #include <string.h> 36 #include <stddef.h> 37 38 #include <sys/ioctl.h> 39 #include <sys/types.h> 40 #include <sys/time.h> 41 #include <sys/inotify.h> 42 #include <dirent.h> 43 #include <fcntl.h> 44 #include <errno.h> 45 #include <ctype.h> 46 #include <pthread.h> 47 48 #include <linux/usbdevice_fs.h> 49 #include <asm/byteorder.h> 50 51 #include "usbhost/usbhost.h" 52 53 #define DEV_DIR "/dev" 54 #define USB_FS_DIR DEV_DIR "/bus/usb" 55 #define USB_FS_ID_SCANNER USB_FS_DIR "/%d/%d" 56 #define USB_FS_ID_FORMAT USB_FS_DIR "/%03d/%03d" 57 58 // From drivers/usb/core/devio.c 59 // I don't know why this isn't in a kernel header 60 #define MAX_USBFS_BUFFER_SIZE 16384 61 62 #define MAX_USBFS_WD_COUNT 10 63 64 struct usb_host_context { 65 int fd; 66 usb_device_added_cb cb_added; 67 usb_device_removed_cb cb_removed; 68 void *data; 69 int wds[MAX_USBFS_WD_COUNT]; 70 int wdd; 71 }; 72 73 struct usb_device { 74 char dev_name[64]; 75 unsigned char desc[4096]; 76 int desc_length; 77 int fd; 78 int writeable; 79 }; 80 81 static inline int badname(const char *name) 82 { 83 while(*name) { 84 if(!isdigit(*name++)) return 1; 85 } 86 return 0; 87 } 88 89 static int find_existing_devices_bus(char *busname, 90 usb_device_added_cb added_cb, 91 void *client_data) 92 { 93 char devname[32]; 94 DIR *devdir; 95 struct dirent *de; 96 int done = 0; 97 98 devdir = opendir(busname); 99 if(devdir == 0) return 0; 100 101 while ((de = readdir(devdir)) && !done) { 102 if(badname(de->d_name)) continue; 103 104 snprintf(devname, sizeof(devname), "%s/%s", busname, de->d_name); 105 done = added_cb(devname, client_data); 106 } // end of devdir while 107 closedir(devdir); 108 109 return done; 110 } 111 112 /* returns true if one of the callbacks indicates we are done */ 113 static int find_existing_devices(usb_device_added_cb added_cb, 114 void *client_data) 115 { 116 char busname[32]; 117 DIR *busdir; 118 struct dirent *de; 119 int done = 0; 120 121 busdir = opendir(USB_FS_DIR); 122 if(busdir == 0) return 0; 123 124 while ((de = readdir(busdir)) != 0 && !done) { 125 if(badname(de->d_name)) continue; 126 127 snprintf(busname, sizeof(busname), USB_FS_DIR "/%s", de->d_name); 128 done = find_existing_devices_bus(busname, added_cb, 129 client_data); 130 } //end of busdir while 131 closedir(busdir); 132 133 return done; 134 } 135 136 static void watch_existing_subdirs(struct usb_host_context *context, 137 int *wds, int wd_count) 138 { 139 char path[100]; 140 int i, ret; 141 142 wds[0] = inotify_add_watch(context->fd, USB_FS_DIR, IN_CREATE | IN_DELETE); 143 if (wds[0] < 0) 144 return; 145 146 /* watch existing subdirectories of USB_FS_DIR */ 147 for (i = 1; i < wd_count; i++) { 148 snprintf(path, sizeof(path), USB_FS_DIR "/%03d", i); 149 ret = inotify_add_watch(context->fd, path, IN_CREATE | IN_DELETE); 150 if (ret >= 0) 151 wds[i] = ret; 152 } 153 } 154 155 struct usb_host_context *usb_host_init() 156 { 157 struct usb_host_context *context = calloc(1, sizeof(struct usb_host_context)); 158 if (!context) { 159 fprintf(stderr, "out of memory in usb_host_context\n"); 160 return NULL; 161 } 162 context->fd = inotify_init(); 163 if (context->fd < 0) { 164 fprintf(stderr, "inotify_init failed\n"); 165 free(context); 166 return NULL; 167 } 168 return context; 169 } 170 171 void usb_host_cleanup(struct usb_host_context *context) 172 { 173 close(context->fd); 174 free(context); 175 } 176 177 int usb_host_get_fd(struct usb_host_context *context) 178 { 179 return context->fd; 180 } /* usb_host_get_fd() */ 181 182 int usb_host_load(struct usb_host_context *context, 183 usb_device_added_cb added_cb, 184 usb_device_removed_cb removed_cb, 185 usb_discovery_done_cb discovery_done_cb, 186 void *client_data) 187 { 188 int done = 0; 189 int i; 190 191 context->cb_added = added_cb; 192 context->cb_removed = removed_cb; 193 context->data = client_data; 194 195 D("Created device discovery thread\n"); 196 197 /* watch for files added and deleted within USB_FS_DIR */ 198 for (i = 0; i < MAX_USBFS_WD_COUNT; i++) 199 context->wds[i] = -1; 200 201 /* watch the root for new subdirectories */ 202 context->wdd = inotify_add_watch(context->fd, DEV_DIR, IN_CREATE | IN_DELETE); 203 if (context->wdd < 0) { 204 fprintf(stderr, "inotify_add_watch failed\n"); 205 if (discovery_done_cb) 206 discovery_done_cb(client_data); 207 return done; 208 } 209 210 watch_existing_subdirs(context, context->wds, MAX_USBFS_WD_COUNT); 211 212 /* check for existing devices first, after we have inotify set up */ 213 done = find_existing_devices(added_cb, client_data); 214 if (discovery_done_cb) 215 done |= discovery_done_cb(client_data); 216 217 return done; 218 } /* usb_host_load() */ 219 220 int usb_host_read_event(struct usb_host_context *context) 221 { 222 struct inotify_event* event; 223 char event_buf[512]; 224 char path[100]; 225 int i, ret, done = 0; 226 int offset = 0; 227 int wd; 228 229 ret = read(context->fd, event_buf, sizeof(event_buf)); 230 if (ret >= (int)sizeof(struct inotify_event)) { 231 while (offset < ret) { 232 event = (struct inotify_event*)&event_buf[offset]; 233 done = 0; 234 wd = event->wd; 235 if (wd == context->wdd) { 236 if ((event->mask & IN_CREATE) && !strcmp(event->name, "bus")) { 237 watch_existing_subdirs(context, context->wds, MAX_USBFS_WD_COUNT); 238 done = find_existing_devices(context->cb_added, context->data); 239 } else if ((event->mask & IN_DELETE) && !strcmp(event->name, "bus")) { 240 for (i = 0; i < MAX_USBFS_WD_COUNT; i++) { 241 if (context->wds[i] >= 0) { 242 inotify_rm_watch(context->fd, context->wds[i]); 243 context->wds[i] = -1; 244 } 245 } 246 } 247 } else if (wd == context->wds[0]) { 248 i = atoi(event->name); 249 snprintf(path, sizeof(path), USB_FS_DIR "/%s", event->name); 250 D("%s subdirectory %s: index: %d\n", (event->mask & IN_CREATE) ? 251 "new" : "gone", path, i); 252 if (i > 0 && i < MAX_USBFS_WD_COUNT) { 253 if (event->mask & IN_CREATE) { 254 ret = inotify_add_watch(context->fd, path, 255 IN_CREATE | IN_DELETE); 256 if (ret >= 0) 257 context->wds[i] = ret; 258 done = find_existing_devices_bus(path, context->cb_added, 259 context->data); 260 } else if (event->mask & IN_DELETE) { 261 inotify_rm_watch(context->fd, context->wds[i]); 262 context->wds[i] = -1; 263 } 264 } 265 } else { 266 for (i = 1; (i < MAX_USBFS_WD_COUNT) && !done; i++) { 267 if (wd == context->wds[i]) { 268 snprintf(path, sizeof(path), USB_FS_DIR "/%03d/%s", i, event->name); 269 if (event->mask == IN_CREATE) { 270 D("new device %s\n", path); 271 done = context->cb_added(path, context->data); 272 } else if (event->mask == IN_DELETE) { 273 D("gone device %s\n", path); 274 done = context->cb_removed(path, context->data); 275 } 276 } 277 } 278 } 279 280 offset += sizeof(struct inotify_event) + event->len; 281 } 282 } 283 284 return done; 285 } /* usb_host_read_event() */ 286 287 void usb_host_run(struct usb_host_context *context, 288 usb_device_added_cb added_cb, 289 usb_device_removed_cb removed_cb, 290 usb_discovery_done_cb discovery_done_cb, 291 void *client_data) 292 { 293 int done; 294 295 done = usb_host_load(context, added_cb, removed_cb, discovery_done_cb, client_data); 296 297 while (!done) { 298 299 done = usb_host_read_event(context); 300 } 301 } /* usb_host_run() */ 302 303 struct usb_device *usb_device_open(const char *dev_name) 304 { 305 int fd, did_retry = 0, writeable = 1; 306 307 D("usb_device_open %s\n", dev_name); 308 309 retry: 310 fd = open(dev_name, O_RDWR); 311 if (fd < 0) { 312 /* if we fail, see if have read-only access */ 313 fd = open(dev_name, O_RDONLY); 314 D("usb_device_open open returned %d errno %d\n", fd, errno); 315 if (fd < 0 && (errno == EACCES || errno == ENOENT) && !did_retry) { 316 /* work around race condition between inotify and permissions management */ 317 sleep(1); 318 did_retry = 1; 319 goto retry; 320 } 321 322 if (fd < 0) 323 return NULL; 324 writeable = 0; 325 D("[ usb open read-only %s fd = %d]\n", dev_name, fd); 326 } 327 328 struct usb_device* result = usb_device_new(dev_name, fd); 329 if (result) 330 result->writeable = writeable; 331 return result; 332 } 333 334 void usb_device_close(struct usb_device *device) 335 { 336 close(device->fd); 337 free(device); 338 } 339 340 struct usb_device *usb_device_new(const char *dev_name, int fd) 341 { 342 struct usb_device *device = calloc(1, sizeof(struct usb_device)); 343 int length; 344 345 D("usb_device_new %s fd: %d\n", dev_name, fd); 346 347 if (lseek(fd, 0, SEEK_SET) != 0) 348 goto failed; 349 length = read(fd, device->desc, sizeof(device->desc)); 350 D("usb_device_new read returned %d errno %d\n", length, errno); 351 if (length < 0) 352 goto failed; 353 354 strncpy(device->dev_name, dev_name, sizeof(device->dev_name) - 1); 355 device->fd = fd; 356 device->desc_length = length; 357 // assume we are writeable, since usb_device_get_fd will only return writeable fds 358 device->writeable = 1; 359 return device; 360 361 failed: 362 close(fd); 363 free(device); 364 return NULL; 365 } 366 367 static int usb_device_reopen_writeable(struct usb_device *device) 368 { 369 if (device->writeable) 370 return 1; 371 372 int fd = open(device->dev_name, O_RDWR); 373 if (fd >= 0) { 374 close(device->fd); 375 device->fd = fd; 376 device->writeable = 1; 377 return 1; 378 } 379 D("usb_device_reopen_writeable failed errno %d\n", errno); 380 return 0; 381 } 382 383 int usb_device_get_fd(struct usb_device *device) 384 { 385 if (!usb_device_reopen_writeable(device)) 386 return -1; 387 return device->fd; 388 } 389 390 const char* usb_device_get_name(struct usb_device *device) 391 { 392 return device->dev_name; 393 } 394 395 int usb_device_get_unique_id(struct usb_device *device) 396 { 397 int bus = 0, dev = 0; 398 sscanf(device->dev_name, USB_FS_ID_SCANNER, &bus, &dev); 399 return bus * 1000 + dev; 400 } 401 402 int usb_device_get_unique_id_from_name(const char* name) 403 { 404 int bus = 0, dev = 0; 405 sscanf(name, USB_FS_ID_SCANNER, &bus, &dev); 406 return bus * 1000 + dev; 407 } 408 409 char* usb_device_get_name_from_unique_id(int id) 410 { 411 int bus = id / 1000; 412 int dev = id % 1000; 413 char* result = (char *)calloc(1, strlen(USB_FS_ID_FORMAT)); 414 snprintf(result, strlen(USB_FS_ID_FORMAT) - 1, USB_FS_ID_FORMAT, bus, dev); 415 return result; 416 } 417 418 uint16_t usb_device_get_vendor_id(struct usb_device *device) 419 { 420 struct usb_device_descriptor* desc = (struct usb_device_descriptor*)device->desc; 421 return __le16_to_cpu(desc->idVendor); 422 } 423 424 uint16_t usb_device_get_product_id(struct usb_device *device) 425 { 426 struct usb_device_descriptor* desc = (struct usb_device_descriptor*)device->desc; 427 return __le16_to_cpu(desc->idProduct); 428 } 429 430 const struct usb_device_descriptor* usb_device_get_device_descriptor(struct usb_device *device) 431 { 432 return (struct usb_device_descriptor*)device->desc; 433 } 434 435 char* usb_device_get_string(struct usb_device *device, int id) 436 { 437 char string[256]; 438 __u16 buffer[128]; 439 __u16 languages[128]; 440 int i, result; 441 int languageCount = 0; 442 443 string[0] = 0; 444 memset(languages, 0, sizeof(languages)); 445 446 // read list of supported languages 447 result = usb_device_control_transfer(device, 448 USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE, USB_REQ_GET_DESCRIPTOR, 449 (USB_DT_STRING << 8) | 0, 0, languages, sizeof(languages), 0); 450 if (result > 0) 451 languageCount = (result - 2) / 2; 452 453 for (i = 1; i <= languageCount; i++) { 454 memset(buffer, 0, sizeof(buffer)); 455 456 result = usb_device_control_transfer(device, 457 USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE, USB_REQ_GET_DESCRIPTOR, 458 (USB_DT_STRING << 8) | id, languages[i], buffer, sizeof(buffer), 0); 459 if (result > 0) { 460 int i; 461 // skip first word, and copy the rest to the string, changing shorts to bytes. 462 result /= 2; 463 for (i = 1; i < result; i++) 464 string[i - 1] = buffer[i]; 465 string[i - 1] = 0; 466 return strdup(string); 467 } 468 } 469 470 return NULL; 471 } 472 473 char* usb_device_get_manufacturer_name(struct usb_device *device) 474 { 475 struct usb_device_descriptor *desc = (struct usb_device_descriptor *)device->desc; 476 477 if (desc->iManufacturer) 478 return usb_device_get_string(device, desc->iManufacturer); 479 else 480 return NULL; 481 } 482 483 char* usb_device_get_product_name(struct usb_device *device) 484 { 485 struct usb_device_descriptor *desc = (struct usb_device_descriptor *)device->desc; 486 487 if (desc->iProduct) 488 return usb_device_get_string(device, desc->iProduct); 489 else 490 return NULL; 491 } 492 493 char* usb_device_get_serial(struct usb_device *device) 494 { 495 struct usb_device_descriptor *desc = (struct usb_device_descriptor *)device->desc; 496 497 if (desc->iSerialNumber) 498 return usb_device_get_string(device, desc->iSerialNumber); 499 else 500 return NULL; 501 } 502 503 int usb_device_is_writeable(struct usb_device *device) 504 { 505 return device->writeable; 506 } 507 508 void usb_descriptor_iter_init(struct usb_device *device, struct usb_descriptor_iter *iter) 509 { 510 iter->config = device->desc; 511 iter->config_end = device->desc + device->desc_length; 512 iter->curr_desc = device->desc; 513 } 514 515 struct usb_descriptor_header *usb_descriptor_iter_next(struct usb_descriptor_iter *iter) 516 { 517 struct usb_descriptor_header* next; 518 if (iter->curr_desc >= iter->config_end) 519 return NULL; 520 next = (struct usb_descriptor_header*)iter->curr_desc; 521 iter->curr_desc += next->bLength; 522 return next; 523 } 524 525 int usb_device_claim_interface(struct usb_device *device, unsigned int interface) 526 { 527 return ioctl(device->fd, USBDEVFS_CLAIMINTERFACE, &interface); 528 } 529 530 int usb_device_release_interface(struct usb_device *device, unsigned int interface) 531 { 532 return ioctl(device->fd, USBDEVFS_RELEASEINTERFACE, &interface); 533 } 534 535 int usb_device_connect_kernel_driver(struct usb_device *device, 536 unsigned int interface, int connect) 537 { 538 struct usbdevfs_ioctl ctl; 539 540 ctl.ifno = interface; 541 ctl.ioctl_code = (connect ? USBDEVFS_CONNECT : USBDEVFS_DISCONNECT); 542 ctl.data = NULL; 543 return ioctl(device->fd, USBDEVFS_IOCTL, &ctl); 544 } 545 546 int usb_device_control_transfer(struct usb_device *device, 547 int requestType, 548 int request, 549 int value, 550 int index, 551 void* buffer, 552 int length, 553 unsigned int timeout) 554 { 555 struct usbdevfs_ctrltransfer ctrl; 556 557 // this usually requires read/write permission 558 if (!usb_device_reopen_writeable(device)) 559 return -1; 560 561 memset(&ctrl, 0, sizeof(ctrl)); 562 ctrl.bRequestType = requestType; 563 ctrl.bRequest = request; 564 ctrl.wValue = value; 565 ctrl.wIndex = index; 566 ctrl.wLength = length; 567 ctrl.data = buffer; 568 ctrl.timeout = timeout; 569 return ioctl(device->fd, USBDEVFS_CONTROL, &ctrl); 570 } 571 572 int usb_device_bulk_transfer(struct usb_device *device, 573 int endpoint, 574 void* buffer, 575 int length, 576 unsigned int timeout) 577 { 578 struct usbdevfs_bulktransfer ctrl; 579 580 // need to limit request size to avoid EINVAL 581 if (length > MAX_USBFS_BUFFER_SIZE) 582 length = MAX_USBFS_BUFFER_SIZE; 583 584 memset(&ctrl, 0, sizeof(ctrl)); 585 ctrl.ep = endpoint; 586 ctrl.len = length; 587 ctrl.data = buffer; 588 ctrl.timeout = timeout; 589 return ioctl(device->fd, USBDEVFS_BULK, &ctrl); 590 } 591 592 struct usb_request *usb_request_new(struct usb_device *dev, 593 const struct usb_endpoint_descriptor *ep_desc) 594 { 595 struct usbdevfs_urb *urb = calloc(1, sizeof(struct usbdevfs_urb)); 596 if (!urb) 597 return NULL; 598 599 if ((ep_desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK) 600 urb->type = USBDEVFS_URB_TYPE_BULK; 601 else if ((ep_desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT) 602 urb->type = USBDEVFS_URB_TYPE_INTERRUPT; 603 else { 604 D("Unsupported endpoint type %d", ep_desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK); 605 free(urb); 606 return NULL; 607 } 608 urb->endpoint = ep_desc->bEndpointAddress; 609 610 struct usb_request *req = calloc(1, sizeof(struct usb_request)); 611 if (!req) { 612 free(urb); 613 return NULL; 614 } 615 616 req->dev = dev; 617 req->max_packet_size = __le16_to_cpu(ep_desc->wMaxPacketSize); 618 req->private_data = urb; 619 req->endpoint = urb->endpoint; 620 urb->usercontext = req; 621 622 return req; 623 } 624 625 void usb_request_free(struct usb_request *req) 626 { 627 free(req->private_data); 628 free(req); 629 } 630 631 int usb_request_queue(struct usb_request *req) 632 { 633 struct usbdevfs_urb *urb = (struct usbdevfs_urb*)req->private_data; 634 int res; 635 636 urb->status = -1; 637 urb->buffer = req->buffer; 638 // need to limit request size to avoid EINVAL 639 if (req->buffer_length > MAX_USBFS_BUFFER_SIZE) 640 urb->buffer_length = MAX_USBFS_BUFFER_SIZE; 641 else 642 urb->buffer_length = req->buffer_length; 643 644 do { 645 res = ioctl(req->dev->fd, USBDEVFS_SUBMITURB, urb); 646 } while((res < 0) && (errno == EINTR)); 647 648 return res; 649 } 650 651 struct usb_request *usb_request_wait(struct usb_device *dev) 652 { 653 struct usbdevfs_urb *urb = NULL; 654 struct usb_request *req = NULL; 655 656 while (1) { 657 int res = ioctl(dev->fd, USBDEVFS_REAPURB, &urb); 658 D("USBDEVFS_REAPURB returned %d\n", res); 659 if (res < 0) { 660 if(errno == EINTR) { 661 continue; 662 } 663 D("[ reap urb - error ]\n"); 664 return NULL; 665 } else { 666 D("[ urb @%p status = %d, actual = %d ]\n", 667 urb, urb->status, urb->actual_length); 668 req = (struct usb_request*)urb->usercontext; 669 req->actual_length = urb->actual_length; 670 } 671 break; 672 } 673 return req; 674 } 675 676 int usb_request_cancel(struct usb_request *req) 677 { 678 struct usbdevfs_urb *urb = ((struct usbdevfs_urb*)req->private_data); 679 return ioctl(req->dev->fd, USBDEVFS_DISCARDURB, &urb); 680 } 681 682