1 /* 2 * Linux host USB redirector 3 * 4 * Copyright (c) 2005 Fabrice Bellard 5 * 6 * Copyright (c) 2008 Max Krasnyansky 7 * Support for host device auto connect & disconnect 8 * Major rewrite to support fully async operation 9 * 10 * Copyright 2008 TJ <linux (at) tjworld.net> 11 * Added flexible support for /dev/bus/usb /sys/bus/usb/devices in addition 12 * to the legacy /proc/bus/usb USB device discovery and handling 13 * 14 * Permission is hereby granted, free of charge, to any person obtaining a copy 15 * of this software and associated documentation files (the "Software"), to deal 16 * in the Software without restriction, including without limitation the rights 17 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 18 * copies of the Software, and to permit persons to whom the Software is 19 * furnished to do so, subject to the following conditions: 20 * 21 * The above copyright notice and this permission notice shall be included in 22 * all copies or substantial portions of the Software. 23 * 24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 27 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 29 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 30 * THE SOFTWARE. 31 */ 32 33 #include "qemu-common.h" 34 #include "qemu-timer.h" 35 #include "monitor.h" 36 37 #include <dirent.h> 38 #include <sys/ioctl.h> 39 #include <signal.h> 40 41 #include <linux/usbdevice_fs.h> 42 #include <linux/version.h> 43 #include "hw/usb.h" 44 45 /* We redefine it to avoid version problems */ 46 struct usb_ctrltransfer { 47 uint8_t bRequestType; 48 uint8_t bRequest; 49 uint16_t wValue; 50 uint16_t wIndex; 51 uint16_t wLength; 52 uint32_t timeout; 53 void *data; 54 }; 55 56 struct usb_ctrlrequest { 57 uint8_t bRequestType; 58 uint8_t bRequest; 59 uint16_t wValue; 60 uint16_t wIndex; 61 uint16_t wLength; 62 }; 63 64 typedef int USBScanFunc(void *opaque, int bus_num, int addr, int class_id, 65 int vendor_id, int product_id, 66 const char *product_name, int speed); 67 static int usb_host_find_device(int *pbus_num, int *paddr, 68 char *product_name, int product_name_size, 69 const char *devname); 70 //#define DEBUG 71 72 #ifdef DEBUG 73 #define dprintf printf 74 #else 75 #define dprintf(...) 76 #endif 77 78 #define USBDBG_DEVOPENED "husb: opened %s/devices\n" 79 80 #define USBPROCBUS_PATH "/proc/bus/usb" 81 #define PRODUCT_NAME_SZ 32 82 #define MAX_ENDPOINTS 16 83 #define USBDEVBUS_PATH "/dev/bus/usb" 84 #define USBSYSBUS_PATH "/sys/bus/usb" 85 86 static char *usb_host_device_path; 87 88 #define USB_FS_NONE 0 89 #define USB_FS_PROC 1 90 #define USB_FS_DEV 2 91 #define USB_FS_SYS 3 92 93 static int usb_fs_type; 94 95 /* endpoint association data */ 96 struct endp_data { 97 uint8_t type; 98 uint8_t halted; 99 }; 100 101 enum { 102 CTRL_STATE_IDLE = 0, 103 CTRL_STATE_SETUP, 104 CTRL_STATE_DATA, 105 CTRL_STATE_ACK 106 }; 107 108 /* 109 * Control transfer state. 110 * Note that 'buffer' _must_ follow 'req' field because 111 * we need contigious buffer when we submit control URB. 112 */ 113 struct ctrl_struct { 114 uint16_t len; 115 uint16_t offset; 116 uint8_t state; 117 struct usb_ctrlrequest req; 118 uint8_t buffer[1024]; 119 }; 120 121 typedef struct USBHostDevice { 122 USBDevice dev; 123 int fd; 124 125 uint8_t descr[1024]; 126 int descr_len; 127 int configuration; 128 int ninterfaces; 129 int closing; 130 131 struct ctrl_struct ctrl; 132 struct endp_data endp_table[MAX_ENDPOINTS]; 133 134 /* Host side address */ 135 int bus_num; 136 int addr; 137 138 struct USBHostDevice *next; 139 } USBHostDevice; 140 141 static int is_isoc(USBHostDevice *s, int ep) 142 { 143 return s->endp_table[ep - 1].type == USBDEVFS_URB_TYPE_ISO; 144 } 145 146 static int is_halted(USBHostDevice *s, int ep) 147 { 148 return s->endp_table[ep - 1].halted; 149 } 150 151 static void clear_halt(USBHostDevice *s, int ep) 152 { 153 s->endp_table[ep - 1].halted = 0; 154 } 155 156 static void set_halt(USBHostDevice *s, int ep) 157 { 158 s->endp_table[ep - 1].halted = 1; 159 } 160 161 static USBHostDevice *hostdev_list; 162 163 static void hostdev_link(USBHostDevice *dev) 164 { 165 dev->next = hostdev_list; 166 hostdev_list = dev; 167 } 168 169 static void hostdev_unlink(USBHostDevice *dev) 170 { 171 USBHostDevice *pdev = hostdev_list; 172 USBHostDevice **prev = &hostdev_list; 173 174 while (pdev) { 175 if (pdev == dev) { 176 *prev = dev->next; 177 return; 178 } 179 180 prev = &pdev->next; 181 pdev = pdev->next; 182 } 183 } 184 185 static USBHostDevice *hostdev_find(int bus_num, int addr) 186 { 187 USBHostDevice *s = hostdev_list; 188 while (s) { 189 if (s->bus_num == bus_num && s->addr == addr) 190 return s; 191 s = s->next; 192 } 193 return NULL; 194 } 195 196 /* 197 * Async URB state. 198 * We always allocate one isoc descriptor even for bulk transfers 199 * to simplify allocation and casts. 200 */ 201 typedef struct AsyncURB 202 { 203 struct usbdevfs_urb urb; 204 struct usbdevfs_iso_packet_desc isocpd; 205 206 USBPacket *packet; 207 USBHostDevice *hdev; 208 } AsyncURB; 209 210 static AsyncURB *async_alloc(void) 211 { 212 return (AsyncURB *) qemu_mallocz(sizeof(AsyncURB)); 213 } 214 215 static void async_free(AsyncURB *aurb) 216 { 217 qemu_free(aurb); 218 } 219 220 static void async_complete_ctrl(USBHostDevice *s, USBPacket *p) 221 { 222 switch(s->ctrl.state) { 223 case CTRL_STATE_SETUP: 224 if (p->len < s->ctrl.len) 225 s->ctrl.len = p->len; 226 s->ctrl.state = CTRL_STATE_DATA; 227 p->len = 8; 228 break; 229 230 case CTRL_STATE_ACK: 231 s->ctrl.state = CTRL_STATE_IDLE; 232 p->len = 0; 233 break; 234 235 default: 236 break; 237 } 238 } 239 240 static void async_complete(void *opaque) 241 { 242 USBHostDevice *s = opaque; 243 AsyncURB *aurb; 244 245 while (1) { 246 USBPacket *p; 247 248 int r = ioctl(s->fd, USBDEVFS_REAPURBNDELAY, &aurb); 249 if (r < 0) { 250 if (errno == EAGAIN) 251 return; 252 253 if (errno == ENODEV && !s->closing) { 254 printf("husb: device %d.%d disconnected\n", s->bus_num, s->addr); 255 usb_device_del_addr(0, s->dev.addr); 256 return; 257 } 258 259 dprintf("husb: async. reap urb failed errno %d\n", errno); 260 return; 261 } 262 263 p = aurb->packet; 264 265 dprintf("husb: async completed. aurb %p status %d alen %d\n", 266 aurb, aurb->urb.status, aurb->urb.actual_length); 267 268 if (p) { 269 switch (aurb->urb.status) { 270 case 0: 271 p->len = aurb->urb.actual_length; 272 if (aurb->urb.type == USBDEVFS_URB_TYPE_CONTROL) 273 async_complete_ctrl(s, p); 274 break; 275 276 case -EPIPE: 277 set_halt(s, p->devep); 278 /* fall through */ 279 default: 280 p->len = USB_RET_NAK; 281 break; 282 } 283 284 usb_packet_complete(p); 285 } 286 287 async_free(aurb); 288 } 289 } 290 291 static void async_cancel(USBPacket *unused, void *opaque) 292 { 293 AsyncURB *aurb = opaque; 294 USBHostDevice *s = aurb->hdev; 295 296 dprintf("husb: async cancel. aurb %p\n", aurb); 297 298 /* Mark it as dead (see async_complete above) */ 299 aurb->packet = NULL; 300 301 int r = ioctl(s->fd, USBDEVFS_DISCARDURB, aurb); 302 if (r < 0) { 303 dprintf("husb: async. discard urb failed errno %d\n", errno); 304 } 305 } 306 307 static int usb_host_claim_interfaces(USBHostDevice *dev, int configuration) 308 { 309 int dev_descr_len, config_descr_len; 310 int interface, nb_interfaces, nb_configurations; 311 int ret, i; 312 313 if (configuration == 0) /* address state - ignore */ 314 return 1; 315 316 dprintf("husb: claiming interfaces. config %d\n", configuration); 317 318 i = 0; 319 dev_descr_len = dev->descr[0]; 320 if (dev_descr_len > dev->descr_len) 321 goto fail; 322 nb_configurations = dev->descr[17]; 323 324 i += dev_descr_len; 325 while (i < dev->descr_len) { 326 dprintf("husb: i is %d, descr_len is %d, dl %d, dt %d\n", i, dev->descr_len, 327 dev->descr[i], dev->descr[i+1]); 328 329 if (dev->descr[i+1] != USB_DT_CONFIG) { 330 i += dev->descr[i]; 331 continue; 332 } 333 config_descr_len = dev->descr[i]; 334 335 printf("husb: config #%d need %d\n", dev->descr[i + 5], configuration); 336 337 if (configuration < 0 || configuration == dev->descr[i + 5]) { 338 configuration = dev->descr[i + 5]; 339 break; 340 } 341 342 i += config_descr_len; 343 } 344 345 if (i >= dev->descr_len) { 346 fprintf(stderr, "husb: update iface failed. no matching configuration\n"); 347 goto fail; 348 } 349 nb_interfaces = dev->descr[i + 4]; 350 351 #ifdef USBDEVFS_DISCONNECT 352 /* earlier Linux 2.4 do not support that */ 353 { 354 struct usbdevfs_ioctl ctrl; 355 for (interface = 0; interface < nb_interfaces; interface++) { 356 ctrl.ioctl_code = USBDEVFS_DISCONNECT; 357 ctrl.ifno = interface; 358 ctrl.data = NULL; 359 ret = ioctl(dev->fd, USBDEVFS_IOCTL, &ctrl); 360 if (ret < 0 && errno != ENODATA) { 361 perror("USBDEVFS_DISCONNECT"); 362 goto fail; 363 } 364 } 365 } 366 #endif 367 368 /* XXX: only grab if all interfaces are free */ 369 for (interface = 0; interface < nb_interfaces; interface++) { 370 ret = ioctl(dev->fd, USBDEVFS_CLAIMINTERFACE, &interface); 371 if (ret < 0) { 372 if (errno == EBUSY) { 373 printf("husb: update iface. device already grabbed\n"); 374 } else { 375 perror("husb: failed to claim interface"); 376 } 377 fail: 378 return 0; 379 } 380 } 381 382 printf("husb: %d interfaces claimed for configuration %d\n", 383 nb_interfaces, configuration); 384 385 dev->ninterfaces = nb_interfaces; 386 dev->configuration = configuration; 387 return 1; 388 } 389 390 static int usb_host_release_interfaces(USBHostDevice *s) 391 { 392 int ret, i; 393 394 dprintf("husb: releasing interfaces\n"); 395 396 for (i = 0; i < s->ninterfaces; i++) { 397 ret = ioctl(s->fd, USBDEVFS_RELEASEINTERFACE, &i); 398 if (ret < 0) { 399 perror("husb: failed to release interface"); 400 return 0; 401 } 402 } 403 404 return 1; 405 } 406 407 static void usb_host_handle_reset(USBDevice *dev) 408 { 409 USBHostDevice *s = (USBHostDevice *) dev; 410 411 dprintf("husb: reset device %u.%u\n", s->bus_num, s->addr); 412 413 ioctl(s->fd, USBDEVFS_RESET); 414 415 usb_host_claim_interfaces(s, s->configuration); 416 } 417 418 static void usb_host_handle_destroy(USBDevice *dev) 419 { 420 USBHostDevice *s = (USBHostDevice *)dev; 421 422 s->closing = 1; 423 424 qemu_set_fd_handler(s->fd, NULL, NULL, NULL); 425 426 hostdev_unlink(s); 427 428 async_complete(s); 429 430 if (s->fd >= 0) 431 close(s->fd); 432 433 qemu_free(s); 434 } 435 436 static int usb_linux_update_endp_table(USBHostDevice *s); 437 438 static int usb_host_handle_data(USBHostDevice *s, USBPacket *p) 439 { 440 struct usbdevfs_urb *urb; 441 AsyncURB *aurb; 442 int ret; 443 444 aurb = async_alloc(); 445 aurb->hdev = s; 446 aurb->packet = p; 447 448 urb = &aurb->urb; 449 450 if (p->pid == USB_TOKEN_IN) 451 urb->endpoint = p->devep | 0x80; 452 else 453 urb->endpoint = p->devep; 454 455 if (is_halted(s, p->devep)) { 456 ret = ioctl(s->fd, USBDEVFS_CLEAR_HALT, &urb->endpoint); 457 if (ret < 0) { 458 dprintf("husb: failed to clear halt. ep 0x%x errno %d\n", 459 urb->endpoint, errno); 460 return USB_RET_NAK; 461 } 462 clear_halt(s, p->devep); 463 } 464 465 urb->buffer = p->data; 466 urb->buffer_length = p->len; 467 468 if (is_isoc(s, p->devep)) { 469 /* Setup ISOC transfer */ 470 urb->type = USBDEVFS_URB_TYPE_ISO; 471 urb->flags = USBDEVFS_URB_ISO_ASAP; 472 urb->number_of_packets = 1; 473 urb->iso_frame_desc[0].length = p->len; 474 } else { 475 /* Setup bulk transfer */ 476 urb->type = USBDEVFS_URB_TYPE_BULK; 477 } 478 479 urb->usercontext = s; 480 481 ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb); 482 483 dprintf("husb: data submit. ep 0x%x len %u aurb %p\n", urb->endpoint, p->len, aurb); 484 485 if (ret < 0) { 486 dprintf("husb: submit failed. errno %d\n", errno); 487 async_free(aurb); 488 489 switch(errno) { 490 case ETIMEDOUT: 491 return USB_RET_NAK; 492 case EPIPE: 493 default: 494 return USB_RET_STALL; 495 } 496 } 497 498 usb_defer_packet(p, async_cancel, aurb); 499 return USB_RET_ASYNC; 500 } 501 502 static int ctrl_error(void) 503 { 504 if (errno == ETIMEDOUT) 505 return USB_RET_NAK; 506 else 507 return USB_RET_STALL; 508 } 509 510 static int usb_host_set_address(USBHostDevice *s, int addr) 511 { 512 dprintf("husb: ctrl set addr %u\n", addr); 513 s->dev.addr = addr; 514 return 0; 515 } 516 517 static int usb_host_set_config(USBHostDevice *s, int config) 518 { 519 usb_host_release_interfaces(s); 520 521 int ret = ioctl(s->fd, USBDEVFS_SETCONFIGURATION, &config); 522 523 dprintf("husb: ctrl set config %d ret %d errno %d\n", config, ret, errno); 524 525 if (ret < 0) 526 return ctrl_error(); 527 528 usb_host_claim_interfaces(s, config); 529 return 0; 530 } 531 532 static int usb_host_set_interface(USBHostDevice *s, int iface, int alt) 533 { 534 struct usbdevfs_setinterface si; 535 int ret; 536 537 si.interface = iface; 538 si.altsetting = alt; 539 ret = ioctl(s->fd, USBDEVFS_SETINTERFACE, &si); 540 541 dprintf("husb: ctrl set iface %d altset %d ret %d errno %d\n", 542 iface, alt, ret, errno); 543 544 if (ret < 0) 545 return ctrl_error(); 546 547 usb_linux_update_endp_table(s); 548 return 0; 549 } 550 551 static int usb_host_handle_control(USBHostDevice *s, USBPacket *p) 552 { 553 struct usbdevfs_urb *urb; 554 AsyncURB *aurb; 555 int ret, value, index; 556 557 /* 558 * Process certain standard device requests. 559 * These are infrequent and are processed synchronously. 560 */ 561 value = le16_to_cpu(s->ctrl.req.wValue); 562 index = le16_to_cpu(s->ctrl.req.wIndex); 563 564 dprintf("husb: ctrl type 0x%x req 0x%x val 0x%x index %u len %u\n", 565 s->ctrl.req.bRequestType, s->ctrl.req.bRequest, value, index, 566 s->ctrl.len); 567 568 if (s->ctrl.req.bRequestType == 0) { 569 switch (s->ctrl.req.bRequest) { 570 case USB_REQ_SET_ADDRESS: 571 return usb_host_set_address(s, value); 572 573 case USB_REQ_SET_CONFIGURATION: 574 return usb_host_set_config(s, value & 0xff); 575 } 576 } 577 578 if (s->ctrl.req.bRequestType == 1 && 579 s->ctrl.req.bRequest == USB_REQ_SET_INTERFACE) 580 return usb_host_set_interface(s, index, value); 581 582 /* The rest are asynchronous */ 583 584 aurb = async_alloc(); 585 aurb->hdev = s; 586 aurb->packet = p; 587 588 /* 589 * Setup ctrl transfer. 590 * 591 * s->ctrl is layed out such that data buffer immediately follows 592 * 'req' struct which is exactly what usbdevfs expects. 593 */ 594 urb = &aurb->urb; 595 596 urb->type = USBDEVFS_URB_TYPE_CONTROL; 597 urb->endpoint = p->devep; 598 599 urb->buffer = &s->ctrl.req; 600 urb->buffer_length = 8 + s->ctrl.len; 601 602 urb->usercontext = s; 603 604 ret = ioctl(s->fd, USBDEVFS_SUBMITURB, urb); 605 606 dprintf("husb: submit ctrl. len %u aurb %p\n", urb->buffer_length, aurb); 607 608 if (ret < 0) { 609 dprintf("husb: submit failed. errno %d\n", errno); 610 async_free(aurb); 611 612 switch(errno) { 613 case ETIMEDOUT: 614 return USB_RET_NAK; 615 case EPIPE: 616 default: 617 return USB_RET_STALL; 618 } 619 } 620 621 usb_defer_packet(p, async_cancel, aurb); 622 return USB_RET_ASYNC; 623 } 624 625 static int do_token_setup(USBDevice *dev, USBPacket *p) 626 { 627 USBHostDevice *s = (USBHostDevice *) dev; 628 int ret = 0; 629 630 if (p->len != 8) 631 return USB_RET_STALL; 632 633 memcpy(&s->ctrl.req, p->data, 8); 634 s->ctrl.len = le16_to_cpu(s->ctrl.req.wLength); 635 s->ctrl.offset = 0; 636 s->ctrl.state = CTRL_STATE_SETUP; 637 638 if (s->ctrl.req.bRequestType & USB_DIR_IN) { 639 ret = usb_host_handle_control(s, p); 640 if (ret < 0) 641 return ret; 642 643 if (ret < s->ctrl.len) 644 s->ctrl.len = ret; 645 s->ctrl.state = CTRL_STATE_DATA; 646 } else { 647 if (s->ctrl.len == 0) 648 s->ctrl.state = CTRL_STATE_ACK; 649 else 650 s->ctrl.state = CTRL_STATE_DATA; 651 } 652 653 return ret; 654 } 655 656 static int do_token_in(USBDevice *dev, USBPacket *p) 657 { 658 USBHostDevice *s = (USBHostDevice *) dev; 659 int ret = 0; 660 661 if (p->devep != 0) 662 return usb_host_handle_data(s, p); 663 664 switch(s->ctrl.state) { 665 case CTRL_STATE_ACK: 666 if (!(s->ctrl.req.bRequestType & USB_DIR_IN)) { 667 ret = usb_host_handle_control(s, p); 668 if (ret == USB_RET_ASYNC) 669 return USB_RET_ASYNC; 670 671 s->ctrl.state = CTRL_STATE_IDLE; 672 return ret > 0 ? 0 : ret; 673 } 674 675 return 0; 676 677 case CTRL_STATE_DATA: 678 if (s->ctrl.req.bRequestType & USB_DIR_IN) { 679 int len = s->ctrl.len - s->ctrl.offset; 680 if (len > p->len) 681 len = p->len; 682 memcpy(p->data, s->ctrl.buffer + s->ctrl.offset, len); 683 s->ctrl.offset += len; 684 if (s->ctrl.offset >= s->ctrl.len) 685 s->ctrl.state = CTRL_STATE_ACK; 686 return len; 687 } 688 689 s->ctrl.state = CTRL_STATE_IDLE; 690 return USB_RET_STALL; 691 692 default: 693 return USB_RET_STALL; 694 } 695 } 696 697 static int do_token_out(USBDevice *dev, USBPacket *p) 698 { 699 USBHostDevice *s = (USBHostDevice *) dev; 700 701 if (p->devep != 0) 702 return usb_host_handle_data(s, p); 703 704 switch(s->ctrl.state) { 705 case CTRL_STATE_ACK: 706 if (s->ctrl.req.bRequestType & USB_DIR_IN) { 707 s->ctrl.state = CTRL_STATE_IDLE; 708 /* transfer OK */ 709 } else { 710 /* ignore additional output */ 711 } 712 return 0; 713 714 case CTRL_STATE_DATA: 715 if (!(s->ctrl.req.bRequestType & USB_DIR_IN)) { 716 int len = s->ctrl.len - s->ctrl.offset; 717 if (len > p->len) 718 len = p->len; 719 memcpy(s->ctrl.buffer + s->ctrl.offset, p->data, len); 720 s->ctrl.offset += len; 721 if (s->ctrl.offset >= s->ctrl.len) 722 s->ctrl.state = CTRL_STATE_ACK; 723 return len; 724 } 725 726 s->ctrl.state = CTRL_STATE_IDLE; 727 return USB_RET_STALL; 728 729 default: 730 return USB_RET_STALL; 731 } 732 } 733 734 /* 735 * Packet handler. 736 * Called by the HC (host controller). 737 * 738 * Returns length of the transaction or one of the USB_RET_XXX codes. 739 */ 740 static int usb_host_handle_packet(USBDevice *s, USBPacket *p) 741 { 742 switch(p->pid) { 743 case USB_MSG_ATTACH: 744 s->state = USB_STATE_ATTACHED; 745 return 0; 746 747 case USB_MSG_DETACH: 748 s->state = USB_STATE_NOTATTACHED; 749 return 0; 750 751 case USB_MSG_RESET: 752 s->remote_wakeup = 0; 753 s->addr = 0; 754 s->state = USB_STATE_DEFAULT; 755 s->handle_reset(s); 756 return 0; 757 } 758 759 /* Rest of the PIDs must match our address */ 760 if (s->state < USB_STATE_DEFAULT || p->devaddr != s->addr) 761 return USB_RET_NODEV; 762 763 switch (p->pid) { 764 case USB_TOKEN_SETUP: 765 return do_token_setup(s, p); 766 767 case USB_TOKEN_IN: 768 return do_token_in(s, p); 769 770 case USB_TOKEN_OUT: 771 return do_token_out(s, p); 772 773 default: 774 return USB_RET_STALL; 775 } 776 } 777 778 /* returns 1 on problem encountered or 0 for success */ 779 static int usb_linux_update_endp_table(USBHostDevice *s) 780 { 781 uint8_t *descriptors; 782 uint8_t devep, type, configuration, alt_interface; 783 struct usb_ctrltransfer ct; 784 int interface, ret, length, i; 785 786 ct.bRequestType = USB_DIR_IN; 787 ct.bRequest = USB_REQ_GET_CONFIGURATION; 788 ct.wValue = 0; 789 ct.wIndex = 0; 790 ct.wLength = 1; 791 ct.data = &configuration; 792 ct.timeout = 50; 793 794 ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct); 795 if (ret < 0) { 796 perror("usb_linux_update_endp_table"); 797 return 1; 798 } 799 800 /* in address state */ 801 if (configuration == 0) 802 return 1; 803 804 /* get the desired configuration, interface, and endpoint descriptors 805 * from device description */ 806 descriptors = &s->descr[18]; 807 length = s->descr_len - 18; 808 i = 0; 809 810 if (descriptors[i + 1] != USB_DT_CONFIG || 811 descriptors[i + 5] != configuration) { 812 dprintf("invalid descriptor data - configuration\n"); 813 return 1; 814 } 815 i += descriptors[i]; 816 817 while (i < length) { 818 if (descriptors[i + 1] != USB_DT_INTERFACE || 819 (descriptors[i + 1] == USB_DT_INTERFACE && 820 descriptors[i + 4] == 0)) { 821 i += descriptors[i]; 822 continue; 823 } 824 825 interface = descriptors[i + 2]; 826 827 ct.bRequestType = USB_DIR_IN | USB_RECIP_INTERFACE; 828 ct.bRequest = USB_REQ_GET_INTERFACE; 829 ct.wValue = 0; 830 ct.wIndex = interface; 831 ct.wLength = 1; 832 ct.data = &alt_interface; 833 ct.timeout = 50; 834 835 ret = ioctl(s->fd, USBDEVFS_CONTROL, &ct); 836 if (ret < 0) { 837 alt_interface = interface; 838 } 839 840 /* the current interface descriptor is the active interface 841 * and has endpoints */ 842 if (descriptors[i + 3] != alt_interface) { 843 i += descriptors[i]; 844 continue; 845 } 846 847 /* advance to the endpoints */ 848 while (i < length && descriptors[i +1] != USB_DT_ENDPOINT) 849 i += descriptors[i]; 850 851 if (i >= length) 852 break; 853 854 while (i < length) { 855 if (descriptors[i + 1] != USB_DT_ENDPOINT) 856 break; 857 858 devep = descriptors[i + 2]; 859 switch (descriptors[i + 3] & 0x3) { 860 case 0x00: 861 type = USBDEVFS_URB_TYPE_CONTROL; 862 break; 863 case 0x01: 864 type = USBDEVFS_URB_TYPE_ISO; 865 break; 866 case 0x02: 867 type = USBDEVFS_URB_TYPE_BULK; 868 break; 869 case 0x03: 870 type = USBDEVFS_URB_TYPE_INTERRUPT; 871 break; 872 default: 873 dprintf("usb_host: malformed endpoint type\n"); 874 type = USBDEVFS_URB_TYPE_BULK; 875 } 876 s->endp_table[(devep & 0xf) - 1].type = type; 877 s->endp_table[(devep & 0xf) - 1].halted = 0; 878 879 i += descriptors[i]; 880 } 881 } 882 return 0; 883 } 884 885 static USBDevice *usb_host_device_open_addr(int bus_num, int addr, const char *prod_name) 886 { 887 int fd = -1, ret; 888 USBHostDevice *dev = NULL; 889 struct usbdevfs_connectinfo ci; 890 char buf[1024]; 891 892 dev = qemu_mallocz(sizeof(USBHostDevice)); 893 894 dev->bus_num = bus_num; 895 dev->addr = addr; 896 897 printf("husb: open device %d.%d\n", bus_num, addr); 898 899 if (!usb_host_device_path) { 900 perror("husb: USB Host Device Path not set"); 901 goto fail; 902 } 903 snprintf(buf, sizeof(buf), "%s/%03d/%03d", usb_host_device_path, 904 bus_num, addr); 905 fd = open(buf, O_RDWR | O_NONBLOCK); 906 if (fd < 0) { 907 perror(buf); 908 goto fail; 909 } 910 dprintf("husb: opened %s\n", buf); 911 912 /* read the device description */ 913 dev->descr_len = read(fd, dev->descr, sizeof(dev->descr)); 914 if (dev->descr_len <= 0) { 915 perror("husb: reading device data failed"); 916 goto fail; 917 } 918 919 #ifdef DEBUG 920 { 921 int x; 922 printf("=== begin dumping device descriptor data ===\n"); 923 for (x = 0; x < dev->descr_len; x++) 924 printf("%02x ", dev->descr[x]); 925 printf("\n=== end dumping device descriptor data ===\n"); 926 } 927 #endif 928 929 dev->fd = fd; 930 931 /* 932 * Initial configuration is -1 which makes us claim first 933 * available config. We used to start with 1, which does not 934 * always work. I've seen devices where first config starts 935 * with 2. 936 */ 937 if (!usb_host_claim_interfaces(dev, -1)) 938 goto fail; 939 940 ret = ioctl(fd, USBDEVFS_CONNECTINFO, &ci); 941 if (ret < 0) { 942 perror("usb_host_device_open: USBDEVFS_CONNECTINFO"); 943 goto fail; 944 } 945 946 printf("husb: grabbed usb device %d.%d\n", bus_num, addr); 947 948 ret = usb_linux_update_endp_table(dev); 949 if (ret) 950 goto fail; 951 952 if (ci.slow) 953 dev->dev.speed = USB_SPEED_LOW; 954 else 955 dev->dev.speed = USB_SPEED_HIGH; 956 957 dev->dev.handle_packet = usb_host_handle_packet; 958 dev->dev.handle_reset = usb_host_handle_reset; 959 dev->dev.handle_destroy = usb_host_handle_destroy; 960 961 if (!prod_name || prod_name[0] == '\0') 962 snprintf(dev->dev.devname, sizeof(dev->dev.devname), 963 "host:%d.%d", bus_num, addr); 964 else 965 pstrcpy(dev->dev.devname, sizeof(dev->dev.devname), 966 prod_name); 967 968 /* USB devio uses 'write' flag to check for async completions */ 969 qemu_set_fd_handler(dev->fd, NULL, async_complete, dev); 970 971 hostdev_link(dev); 972 973 return (USBDevice *) dev; 974 975 fail: 976 if (dev) 977 qemu_free(dev); 978 979 close(fd); 980 return NULL; 981 } 982 983 static int usb_host_auto_add(const char *spec); 984 static int usb_host_auto_del(const char *spec); 985 986 USBDevice *usb_host_device_open(const char *devname) 987 { 988 Monitor *mon = cur_mon; 989 int bus_num, addr; 990 char product_name[PRODUCT_NAME_SZ]; 991 992 if (strstr(devname, "auto:")) { 993 usb_host_auto_add(devname); 994 return NULL; 995 } 996 997 if (usb_host_find_device(&bus_num, &addr, product_name, sizeof(product_name), 998 devname) < 0) 999 return NULL; 1000 1001 if (hostdev_find(bus_num, addr)) { 1002 monitor_printf(mon, "husb: host usb device %d.%d is already open\n", 1003 bus_num, addr); 1004 return NULL; 1005 } 1006 1007 return usb_host_device_open_addr(bus_num, addr, product_name); 1008 } 1009 1010 int usb_host_device_close(const char *devname) 1011 { 1012 char product_name[PRODUCT_NAME_SZ]; 1013 int bus_num, addr; 1014 USBHostDevice *s; 1015 1016 if (strstr(devname, "auto:")) 1017 return usb_host_auto_del(devname); 1018 1019 if (usb_host_find_device(&bus_num, &addr, product_name, sizeof(product_name), 1020 devname) < 0) 1021 return -1; 1022 1023 s = hostdev_find(bus_num, addr); 1024 if (s) { 1025 usb_device_del_addr(0, s->dev.addr); 1026 return 0; 1027 } 1028 1029 return -1; 1030 } 1031 1032 static int get_tag_value(char *buf, int buf_size, 1033 const char *str, const char *tag, 1034 const char *stopchars) 1035 { 1036 const char *p; 1037 char *q; 1038 p = strstr(str, tag); 1039 if (!p) 1040 return -1; 1041 p += strlen(tag); 1042 while (qemu_isspace(*p)) 1043 p++; 1044 q = buf; 1045 while (*p != '\0' && !strchr(stopchars, *p)) { 1046 if ((q - buf) < (buf_size - 1)) 1047 *q++ = *p; 1048 p++; 1049 } 1050 *q = '\0'; 1051 return q - buf; 1052 } 1053 1054 /* 1055 * Use /proc/bus/usb/devices or /dev/bus/usb/devices file to determine 1056 * host's USB devices. This is legacy support since many distributions 1057 * are moving to /sys/bus/usb 1058 */ 1059 static int usb_host_scan_dev(void *opaque, USBScanFunc *func) 1060 { 1061 FILE *f = 0; 1062 char line[1024]; 1063 char buf[1024]; 1064 int bus_num, addr, speed, device_count, class_id, product_id, vendor_id; 1065 char product_name[512]; 1066 int ret = 0; 1067 1068 if (!usb_host_device_path) { 1069 perror("husb: USB Host Device Path not set"); 1070 goto the_end; 1071 } 1072 snprintf(line, sizeof(line), "%s/devices", usb_host_device_path); 1073 f = fopen(line, "r"); 1074 if (!f) { 1075 perror("husb: cannot open devices file"); 1076 goto the_end; 1077 } 1078 1079 device_count = 0; 1080 bus_num = addr = speed = class_id = product_id = vendor_id = 0; 1081 for(;;) { 1082 if (fgets(line, sizeof(line), f) == NULL) 1083 break; 1084 if (strlen(line) > 0) 1085 line[strlen(line) - 1] = '\0'; 1086 if (line[0] == 'T' && line[1] == ':') { 1087 if (device_count && (vendor_id || product_id)) { 1088 /* New device. Add the previously discovered device. */ 1089 ret = func(opaque, bus_num, addr, class_id, vendor_id, 1090 product_id, product_name, speed); 1091 if (ret) 1092 goto the_end; 1093 } 1094 if (get_tag_value(buf, sizeof(buf), line, "Bus=", " ") < 0) 1095 goto fail; 1096 bus_num = atoi(buf); 1097 if (get_tag_value(buf, sizeof(buf), line, "Dev#=", " ") < 0) 1098 goto fail; 1099 addr = atoi(buf); 1100 if (get_tag_value(buf, sizeof(buf), line, "Spd=", " ") < 0) 1101 goto fail; 1102 if (!strcmp(buf, "480")) 1103 speed = USB_SPEED_HIGH; 1104 else if (!strcmp(buf, "1.5")) 1105 speed = USB_SPEED_LOW; 1106 else 1107 speed = USB_SPEED_FULL; 1108 product_name[0] = '\0'; 1109 class_id = 0xff; 1110 device_count++; 1111 product_id = 0; 1112 vendor_id = 0; 1113 } else if (line[0] == 'P' && line[1] == ':') { 1114 if (get_tag_value(buf, sizeof(buf), line, "Vendor=", " ") < 0) 1115 goto fail; 1116 vendor_id = strtoul(buf, NULL, 16); 1117 if (get_tag_value(buf, sizeof(buf), line, "ProdID=", " ") < 0) 1118 goto fail; 1119 product_id = strtoul(buf, NULL, 16); 1120 } else if (line[0] == 'S' && line[1] == ':') { 1121 if (get_tag_value(buf, sizeof(buf), line, "Product=", "") < 0) 1122 goto fail; 1123 pstrcpy(product_name, sizeof(product_name), buf); 1124 } else if (line[0] == 'D' && line[1] == ':') { 1125 if (get_tag_value(buf, sizeof(buf), line, "Cls=", " (") < 0) 1126 goto fail; 1127 class_id = strtoul(buf, NULL, 16); 1128 } 1129 fail: ; 1130 } 1131 if (device_count && (vendor_id || product_id)) { 1132 /* Add the last device. */ 1133 ret = func(opaque, bus_num, addr, class_id, vendor_id, 1134 product_id, product_name, speed); 1135 } 1136 the_end: 1137 if (f) 1138 fclose(f); 1139 return ret; 1140 } 1141 1142 /* 1143 * Read sys file-system device file 1144 * 1145 * @line address of buffer to put file contents in 1146 * @line_size size of line 1147 * @device_file path to device file (printf format string) 1148 * @device_name device being opened (inserted into device_file) 1149 * 1150 * @return 0 failed, 1 succeeded ('line' contains data) 1151 */ 1152 static int usb_host_read_file(char *line, size_t line_size, const char *device_file, const char *device_name) 1153 { 1154 Monitor *mon = cur_mon; 1155 FILE *f; 1156 int ret = 0; 1157 char filename[PATH_MAX]; 1158 1159 snprintf(filename, PATH_MAX, USBSYSBUS_PATH "/devices/%s/%s", device_name, 1160 device_file); 1161 f = fopen(filename, "r"); 1162 if (f) { 1163 ret = (fgets(line, line_size, f) != NULL); 1164 fclose(f); 1165 } else { 1166 monitor_printf(mon, "husb: could not open %s\n", filename); 1167 } 1168 1169 return ret; 1170 } 1171 1172 /* 1173 * Use /sys/bus/usb/devices/ directory to determine host's USB 1174 * devices. 1175 * 1176 * This code is based on Robert Schiele's original patches posted to 1177 * the Novell bug-tracker https://bugzilla.novell.com/show_bug.cgi?id=241950 1178 */ 1179 static int usb_host_scan_sys(void *opaque, USBScanFunc *func) 1180 { 1181 DIR *dir = 0; 1182 char line[1024]; 1183 int bus_num, addr, speed, class_id, product_id, vendor_id; 1184 int ret = 0; 1185 char product_name[512]; 1186 struct dirent *de; 1187 1188 dir = opendir(USBSYSBUS_PATH "/devices"); 1189 if (!dir) { 1190 perror("husb: cannot open devices directory"); 1191 goto the_end; 1192 } 1193 1194 while ((de = readdir(dir))) { 1195 if (de->d_name[0] != '.' && !strchr(de->d_name, ':')) { 1196 char *tmpstr = de->d_name; 1197 if (!strncmp(de->d_name, "usb", 3)) 1198 tmpstr += 3; 1199 bus_num = atoi(tmpstr); 1200 1201 if (!usb_host_read_file(line, sizeof(line), "devnum", de->d_name)) 1202 goto the_end; 1203 if (sscanf(line, "%d", &addr) != 1) 1204 goto the_end; 1205 1206 if (!usb_host_read_file(line, sizeof(line), "bDeviceClass", 1207 de->d_name)) 1208 goto the_end; 1209 if (sscanf(line, "%x", &class_id) != 1) 1210 goto the_end; 1211 1212 if (!usb_host_read_file(line, sizeof(line), "idVendor", de->d_name)) 1213 goto the_end; 1214 if (sscanf(line, "%x", &vendor_id) != 1) 1215 goto the_end; 1216 1217 if (!usb_host_read_file(line, sizeof(line), "idProduct", 1218 de->d_name)) 1219 goto the_end; 1220 if (sscanf(line, "%x", &product_id) != 1) 1221 goto the_end; 1222 1223 if (!usb_host_read_file(line, sizeof(line), "product", 1224 de->d_name)) { 1225 *product_name = 0; 1226 } else { 1227 if (strlen(line) > 0) 1228 line[strlen(line) - 1] = '\0'; 1229 pstrcpy(product_name, sizeof(product_name), line); 1230 } 1231 1232 if (!usb_host_read_file(line, sizeof(line), "speed", de->d_name)) 1233 goto the_end; 1234 if (!strcmp(line, "480\n")) 1235 speed = USB_SPEED_HIGH; 1236 else if (!strcmp(line, "1.5\n")) 1237 speed = USB_SPEED_LOW; 1238 else 1239 speed = USB_SPEED_FULL; 1240 1241 ret = func(opaque, bus_num, addr, class_id, vendor_id, 1242 product_id, product_name, speed); 1243 if (ret) 1244 goto the_end; 1245 } 1246 } 1247 the_end: 1248 if (dir) 1249 closedir(dir); 1250 return ret; 1251 } 1252 1253 /* 1254 * Determine how to access the host's USB devices and call the 1255 * specific support function. 1256 */ 1257 static int usb_host_scan(void *opaque, USBScanFunc *func) 1258 { 1259 Monitor *mon = cur_mon; 1260 FILE *f = 0; 1261 DIR *dir = 0; 1262 int ret = 0; 1263 const char *fs_type[] = {"unknown", "proc", "dev", "sys"}; 1264 char devpath[PATH_MAX]; 1265 1266 /* only check the host once */ 1267 if (!usb_fs_type) { 1268 f = fopen(USBPROCBUS_PATH "/devices", "r"); 1269 if (f) { 1270 /* devices found in /proc/bus/usb/ */ 1271 strcpy(devpath, USBPROCBUS_PATH); 1272 usb_fs_type = USB_FS_PROC; 1273 fclose(f); 1274 dprintf(USBDBG_DEVOPENED, USBPROCBUS_PATH); 1275 goto found_devices; 1276 } 1277 /* try additional methods if an access method hasn't been found yet */ 1278 f = fopen(USBDEVBUS_PATH "/devices", "r"); 1279 if (f) { 1280 /* devices found in /dev/bus/usb/ */ 1281 strcpy(devpath, USBDEVBUS_PATH); 1282 usb_fs_type = USB_FS_DEV; 1283 fclose(f); 1284 dprintf(USBDBG_DEVOPENED, USBDEVBUS_PATH); 1285 goto found_devices; 1286 } 1287 dir = opendir(USBSYSBUS_PATH "/devices"); 1288 if (dir) { 1289 /* devices found in /dev/bus/usb/ (yes - not a mistake!) */ 1290 strcpy(devpath, USBDEVBUS_PATH); 1291 usb_fs_type = USB_FS_SYS; 1292 closedir(dir); 1293 dprintf(USBDBG_DEVOPENED, USBSYSBUS_PATH); 1294 goto found_devices; 1295 } 1296 found_devices: 1297 if (!usb_fs_type) { 1298 monitor_printf(mon, "husb: unable to access USB devices\n"); 1299 return -ENOENT; 1300 } 1301 1302 /* the module setting (used later for opening devices) */ 1303 usb_host_device_path = qemu_mallocz(strlen(devpath)+1); 1304 strcpy(usb_host_device_path, devpath); 1305 monitor_printf(mon, "husb: using %s file-system with %s\n", 1306 fs_type[usb_fs_type], usb_host_device_path); 1307 } 1308 1309 switch (usb_fs_type) { 1310 case USB_FS_PROC: 1311 case USB_FS_DEV: 1312 ret = usb_host_scan_dev(opaque, func); 1313 break; 1314 case USB_FS_SYS: 1315 ret = usb_host_scan_sys(opaque, func); 1316 break; 1317 default: 1318 ret = -EINVAL; 1319 break; 1320 } 1321 return ret; 1322 } 1323 1324 struct USBAutoFilter { 1325 struct USBAutoFilter *next; 1326 int bus_num; 1327 int addr; 1328 int vendor_id; 1329 int product_id; 1330 }; 1331 1332 static QEMUTimer *usb_auto_timer; 1333 static struct USBAutoFilter *usb_auto_filter; 1334 1335 static int usb_host_auto_scan(void *opaque, int bus_num, int addr, 1336 int class_id, int vendor_id, int product_id, 1337 const char *product_name, int speed) 1338 { 1339 struct USBAutoFilter *f; 1340 struct USBDevice *dev; 1341 1342 /* Ignore hubs */ 1343 if (class_id == 9) 1344 return 0; 1345 1346 for (f = usb_auto_filter; f; f = f->next) { 1347 if (f->bus_num >= 0 && f->bus_num != bus_num) 1348 continue; 1349 1350 if (f->addr >= 0 && f->addr != addr) 1351 continue; 1352 1353 if (f->vendor_id >= 0 && f->vendor_id != vendor_id) 1354 continue; 1355 1356 if (f->product_id >= 0 && f->product_id != product_id) 1357 continue; 1358 1359 /* We got a match */ 1360 1361 /* Allredy attached ? */ 1362 if (hostdev_find(bus_num, addr)) 1363 return 0; 1364 1365 dprintf("husb: auto open: bus_num %d addr %d\n", bus_num, addr); 1366 1367 dev = usb_host_device_open_addr(bus_num, addr, product_name); 1368 if (dev) 1369 usb_device_add_dev(dev); 1370 } 1371 1372 return 0; 1373 } 1374 1375 static void usb_host_auto_timer(void *unused) 1376 { 1377 usb_host_scan(NULL, usb_host_auto_scan); 1378 qemu_mod_timer(usb_auto_timer, qemu_get_clock_ms(rt_clock) + 2000); 1379 } 1380 1381 /* 1382 * Autoconnect filter 1383 * Format: 1384 * auto:bus:dev[:vid:pid] 1385 * auto:bus.dev[:vid:pid] 1386 * 1387 * bus - bus number (dec, * means any) 1388 * dev - device number (dec, * means any) 1389 * vid - vendor id (hex, * means any) 1390 * pid - product id (hex, * means any) 1391 * 1392 * See 'lsusb' output. 1393 */ 1394 static int parse_filter(const char *spec, struct USBAutoFilter *f) 1395 { 1396 enum { BUS, DEV, VID, PID, DONE }; 1397 const char *p = spec; 1398 int i; 1399 1400 f->bus_num = -1; 1401 f->addr = -1; 1402 f->vendor_id = -1; 1403 f->product_id = -1; 1404 1405 for (i = BUS; i < DONE; i++) { 1406 p = strpbrk(p, ":."); 1407 if (!p) break; 1408 p++; 1409 1410 if (*p == '*') 1411 continue; 1412 1413 switch(i) { 1414 case BUS: f->bus_num = strtol(p, NULL, 10); break; 1415 case DEV: f->addr = strtol(p, NULL, 10); break; 1416 case VID: f->vendor_id = strtol(p, NULL, 16); break; 1417 case PID: f->product_id = strtol(p, NULL, 16); break; 1418 } 1419 } 1420 1421 if (i < DEV) { 1422 fprintf(stderr, "husb: invalid auto filter spec %s\n", spec); 1423 return -1; 1424 } 1425 1426 return 0; 1427 } 1428 1429 static int match_filter(const struct USBAutoFilter *f1, 1430 const struct USBAutoFilter *f2) 1431 { 1432 return f1->bus_num == f2->bus_num && 1433 f1->addr == f2->addr && 1434 f1->vendor_id == f2->vendor_id && 1435 f1->product_id == f2->product_id; 1436 } 1437 1438 static int usb_host_auto_add(const char *spec) 1439 { 1440 struct USBAutoFilter filter, *f; 1441 1442 if (parse_filter(spec, &filter) < 0) 1443 return -1; 1444 1445 f = qemu_mallocz(sizeof(*f)); 1446 1447 *f = filter; 1448 1449 if (!usb_auto_filter) { 1450 /* 1451 * First entry. Init and start the monitor. 1452 * Right now we're using timer to check for new devices. 1453 * If this turns out to be too expensive we can move that into a 1454 * separate thread. 1455 */ 1456 usb_auto_timer = qemu_new_timer_ms(rt_clock, usb_host_auto_timer, NULL); 1457 if (!usb_auto_timer) { 1458 fprintf(stderr, "husb: failed to allocate auto scan timer\n"); 1459 qemu_free(f); 1460 return -1; 1461 } 1462 1463 /* Check for new devices every two seconds */ 1464 qemu_mod_timer(usb_auto_timer, qemu_get_clock_ms(rt_clock) + 2000); 1465 } 1466 1467 dprintf("husb: added auto filter: bus_num %d addr %d vid %d pid %d\n", 1468 f->bus_num, f->addr, f->vendor_id, f->product_id); 1469 1470 f->next = usb_auto_filter; 1471 usb_auto_filter = f; 1472 1473 return 0; 1474 } 1475 1476 static int usb_host_auto_del(const char *spec) 1477 { 1478 struct USBAutoFilter *pf = usb_auto_filter; 1479 struct USBAutoFilter **prev = &usb_auto_filter; 1480 struct USBAutoFilter filter; 1481 1482 if (parse_filter(spec, &filter) < 0) 1483 return -1; 1484 1485 while (pf) { 1486 if (match_filter(pf, &filter)) { 1487 dprintf("husb: removed auto filter: bus_num %d addr %d vid %d pid %d\n", 1488 pf->bus_num, pf->addr, pf->vendor_id, pf->product_id); 1489 1490 *prev = pf->next; 1491 1492 if (!usb_auto_filter) { 1493 /* No more filters. Stop scanning. */ 1494 qemu_del_timer(usb_auto_timer); 1495 qemu_free_timer(usb_auto_timer); 1496 } 1497 1498 return 0; 1499 } 1500 1501 prev = &pf->next; 1502 pf = pf->next; 1503 } 1504 1505 return -1; 1506 } 1507 1508 typedef struct FindDeviceState { 1509 int vendor_id; 1510 int product_id; 1511 int bus_num; 1512 int addr; 1513 char product_name[PRODUCT_NAME_SZ]; 1514 } FindDeviceState; 1515 1516 static int usb_host_find_device_scan(void *opaque, int bus_num, int addr, 1517 int class_id, 1518 int vendor_id, int product_id, 1519 const char *product_name, int speed) 1520 { 1521 FindDeviceState *s = opaque; 1522 if ((vendor_id == s->vendor_id && 1523 product_id == s->product_id) || 1524 (bus_num == s->bus_num && 1525 addr == s->addr)) { 1526 pstrcpy(s->product_name, PRODUCT_NAME_SZ, product_name); 1527 s->bus_num = bus_num; 1528 s->addr = addr; 1529 return 1; 1530 } else { 1531 return 0; 1532 } 1533 } 1534 1535 /* the syntax is : 1536 'bus.addr' (decimal numbers) or 1537 'vendor_id:product_id' (hexa numbers) */ 1538 static int usb_host_find_device(int *pbus_num, int *paddr, 1539 char *product_name, int product_name_size, 1540 const char *devname) 1541 { 1542 const char *p; 1543 int ret; 1544 FindDeviceState fs; 1545 1546 p = strchr(devname, '.'); 1547 if (p) { 1548 *pbus_num = strtoul(devname, NULL, 0); 1549 *paddr = strtoul(p + 1, NULL, 0); 1550 fs.bus_num = *pbus_num; 1551 fs.addr = *paddr; 1552 fs.vendor_id = -1; 1553 fs.product_id = -1; 1554 ret = usb_host_scan(&fs, usb_host_find_device_scan); 1555 if (ret) 1556 pstrcpy(product_name, product_name_size, fs.product_name); 1557 return 0; 1558 } 1559 1560 p = strchr(devname, ':'); 1561 if (p) { 1562 fs.vendor_id = strtoul(devname, NULL, 16); 1563 fs.product_id = strtoul(p + 1, NULL, 16); 1564 fs.bus_num = -1; 1565 fs.addr = -1; 1566 ret = usb_host_scan(&fs, usb_host_find_device_scan); 1567 if (ret) { 1568 *pbus_num = fs.bus_num; 1569 *paddr = fs.addr; 1570 pstrcpy(product_name, product_name_size, fs.product_name); 1571 return 0; 1572 } 1573 } 1574 return -1; 1575 } 1576 1577 /**********************/ 1578 /* USB host device info */ 1579 1580 struct usb_class_info { 1581 int class; 1582 const char *class_name; 1583 }; 1584 1585 static const struct usb_class_info usb_class_info[] = { 1586 { USB_CLASS_AUDIO, "Audio"}, 1587 { USB_CLASS_COMM, "Communication"}, 1588 { USB_CLASS_HID, "HID"}, 1589 { USB_CLASS_HUB, "Hub" }, 1590 { USB_CLASS_PHYSICAL, "Physical" }, 1591 { USB_CLASS_PRINTER, "Printer" }, 1592 { USB_CLASS_MASS_STORAGE, "Storage" }, 1593 { USB_CLASS_CDC_DATA, "Data" }, 1594 { USB_CLASS_APP_SPEC, "Application Specific" }, 1595 { USB_CLASS_VENDOR_SPEC, "Vendor Specific" }, 1596 { USB_CLASS_STILL_IMAGE, "Still Image" }, 1597 { USB_CLASS_CSCID, "Smart Card" }, 1598 { USB_CLASS_CONTENT_SEC, "Content Security" }, 1599 { -1, NULL } 1600 }; 1601 1602 static const char *usb_class_str(uint8_t class) 1603 { 1604 const struct usb_class_info *p; 1605 for(p = usb_class_info; p->class != -1; p++) { 1606 if (p->class == class) 1607 break; 1608 } 1609 return p->class_name; 1610 } 1611 1612 static void usb_info_device(int bus_num, int addr, int class_id, 1613 int vendor_id, int product_id, 1614 const char *product_name, 1615 int speed) 1616 { 1617 Monitor *mon = cur_mon; 1618 const char *class_str, *speed_str; 1619 1620 switch(speed) { 1621 case USB_SPEED_LOW: 1622 speed_str = "1.5"; 1623 break; 1624 case USB_SPEED_FULL: 1625 speed_str = "12"; 1626 break; 1627 case USB_SPEED_HIGH: 1628 speed_str = "480"; 1629 break; 1630 default: 1631 speed_str = "?"; 1632 break; 1633 } 1634 1635 monitor_printf(mon, " Device %d.%d, speed %s Mb/s\n", 1636 bus_num, addr, speed_str); 1637 class_str = usb_class_str(class_id); 1638 if (class_str) 1639 monitor_printf(mon, " %s:", class_str); 1640 else 1641 monitor_printf(mon, " Class %02x:", class_id); 1642 monitor_printf(mon, " USB device %04x:%04x", vendor_id, product_id); 1643 if (product_name[0] != '\0') 1644 monitor_printf(mon, ", %s", product_name); 1645 monitor_printf(mon, "\n"); 1646 } 1647 1648 static int usb_host_info_device(void *opaque, int bus_num, int addr, 1649 int class_id, 1650 int vendor_id, int product_id, 1651 const char *product_name, 1652 int speed) 1653 { 1654 usb_info_device(bus_num, addr, class_id, vendor_id, product_id, 1655 product_name, speed); 1656 return 0; 1657 } 1658 1659 static void dec2str(int val, char *str, size_t size) 1660 { 1661 if (val == -1) 1662 snprintf(str, size, "*"); 1663 else 1664 snprintf(str, size, "%d", val); 1665 } 1666 1667 static void hex2str(int val, char *str, size_t size) 1668 { 1669 if (val == -1) 1670 snprintf(str, size, "*"); 1671 else 1672 snprintf(str, size, "%x", val); 1673 } 1674 1675 void usb_host_info(Monitor *mon) 1676 { 1677 struct USBAutoFilter *f; 1678 1679 usb_host_scan(NULL, usb_host_info_device); 1680 1681 if (usb_auto_filter) 1682 monitor_printf(mon, " Auto filters:\n"); 1683 for (f = usb_auto_filter; f; f = f->next) { 1684 char bus[10], addr[10], vid[10], pid[10]; 1685 dec2str(f->bus_num, bus, sizeof(bus)); 1686 dec2str(f->addr, addr, sizeof(addr)); 1687 hex2str(f->vendor_id, vid, sizeof(vid)); 1688 hex2str(f->product_id, pid, sizeof(pid)); 1689 monitor_printf(mon, " Device %s.%s ID %s:%s\n", 1690 bus, addr, vid, pid); 1691 } 1692 } 1693