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