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