1 #include "qemu/osdep.h" 2 #include "hw/qdev-properties.h" 3 #include "hw/usb.h" 4 #include "qapi/error.h" 5 #include "qapi/qapi-commands-machine.h" 6 #include "qapi/type-helpers.h" 7 #include "qemu/error-report.h" 8 #include "qemu/module.h" 9 #include "system/system.h" 10 #include "migration/vmstate.h" 11 #include "monitor/monitor.h" 12 #include "trace.h" 13 #include "qemu/cutils.h" 14 15 static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent); 16 17 static char *usb_get_dev_path(DeviceState *dev); 18 static char *usb_get_fw_dev_path(DeviceState *qdev); 19 static void usb_qdev_unrealize(DeviceState *qdev); 20 21 static const Property usb_props[] = { 22 DEFINE_PROP_STRING("port", USBDevice, port_path), 23 DEFINE_PROP_STRING("serial", USBDevice, serial), 24 DEFINE_PROP_BIT("msos-desc", USBDevice, flags, 25 USB_DEV_FLAG_MSOS_DESC_ENABLE, true), 26 DEFINE_PROP_STRING("pcap", USBDevice, pcap_filename), 27 }; 28 29 static void usb_bus_class_init(ObjectClass *klass, const void *data) 30 { 31 BusClass *k = BUS_CLASS(klass); 32 HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass); 33 34 k->print_dev = usb_bus_dev_print; 35 k->get_dev_path = usb_get_dev_path; 36 k->get_fw_dev_path = usb_get_fw_dev_path; 37 hc->unplug = qdev_simple_device_unplug_cb; 38 } 39 40 static const TypeInfo usb_bus_info = { 41 .name = TYPE_USB_BUS, 42 .parent = TYPE_BUS, 43 .instance_size = sizeof(USBBus), 44 .class_init = usb_bus_class_init, 45 .interfaces = (const InterfaceInfo[]) { 46 { TYPE_HOTPLUG_HANDLER }, 47 { } 48 } 49 }; 50 51 static int next_usb_bus = 0; 52 static QTAILQ_HEAD(, USBBus) busses = QTAILQ_HEAD_INITIALIZER(busses); 53 54 static int usb_device_post_load(void *opaque, int version_id) 55 { 56 USBDevice *dev = opaque; 57 58 if (dev->state == USB_STATE_NOTATTACHED) { 59 dev->attached = false; 60 } else { 61 dev->attached = true; 62 } 63 return 0; 64 } 65 66 const VMStateDescription vmstate_usb_device = { 67 .name = "USBDevice", 68 .version_id = 1, 69 .minimum_version_id = 1, 70 .post_load = usb_device_post_load, 71 .fields = (const VMStateField[]) { 72 VMSTATE_UINT8(addr, USBDevice), 73 VMSTATE_INT32(state, USBDevice), 74 VMSTATE_INT32(remote_wakeup, USBDevice), 75 VMSTATE_INT32(setup_state, USBDevice), 76 VMSTATE_INT32(setup_len, USBDevice), 77 VMSTATE_INT32(setup_index, USBDevice), 78 VMSTATE_UINT8_ARRAY(setup_buf, USBDevice, 8), 79 VMSTATE_END_OF_LIST(), 80 } 81 }; 82 83 void usb_bus_new(USBBus *bus, size_t bus_size, 84 USBBusOps *ops, DeviceState *host) 85 { 86 qbus_init(bus, bus_size, TYPE_USB_BUS, host, NULL); 87 qbus_set_bus_hotplug_handler(BUS(bus)); 88 bus->ops = ops; 89 bus->busnr = next_usb_bus++; 90 QTAILQ_INIT(&bus->free); 91 QTAILQ_INIT(&bus->used); 92 QTAILQ_INSERT_TAIL(&busses, bus, next); 93 } 94 95 void usb_bus_release(USBBus *bus) 96 { 97 assert(next_usb_bus > 0); 98 99 QTAILQ_REMOVE(&busses, bus, next); 100 } 101 102 static void usb_device_realize(USBDevice *dev, Error **errp) 103 { 104 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev); 105 106 if (klass->realize) { 107 klass->realize(dev, errp); 108 } 109 } 110 111 USBDevice *usb_device_find_device(USBDevice *dev, uint8_t addr) 112 { 113 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev); 114 if (klass->find_device) { 115 return klass->find_device(dev, addr); 116 } 117 return NULL; 118 } 119 120 static void usb_device_unrealize(USBDevice *dev) 121 { 122 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev); 123 124 if (klass->unrealize) { 125 klass->unrealize(dev); 126 } 127 } 128 129 void usb_device_cancel_packet(USBDevice *dev, USBPacket *p) 130 { 131 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev); 132 if (klass->cancel_packet) { 133 klass->cancel_packet(dev, p); 134 } 135 } 136 137 void usb_device_handle_attach(USBDevice *dev) 138 { 139 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev); 140 if (klass->handle_attach) { 141 klass->handle_attach(dev); 142 } 143 } 144 145 void usb_device_handle_reset(USBDevice *dev) 146 { 147 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev); 148 if (klass->handle_reset) { 149 klass->handle_reset(dev); 150 } 151 } 152 153 void usb_device_handle_control(USBDevice *dev, USBPacket *p, int request, 154 int value, int index, int length, uint8_t *data) 155 { 156 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev); 157 if (klass->handle_control) { 158 klass->handle_control(dev, p, request, value, index, length, data); 159 } 160 } 161 162 void usb_device_handle_data(USBDevice *dev, USBPacket *p) 163 { 164 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev); 165 if (klass->handle_data) { 166 klass->handle_data(dev, p); 167 } 168 } 169 170 const char *usb_device_get_product_desc(USBDevice *dev) 171 { 172 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev); 173 return klass->product_desc; 174 } 175 176 const USBDesc *usb_device_get_usb_desc(USBDevice *dev) 177 { 178 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev); 179 if (dev->usb_desc) { 180 return dev->usb_desc; 181 } 182 return klass->usb_desc; 183 } 184 185 void usb_device_set_interface(USBDevice *dev, int interface, 186 int alt_old, int alt_new) 187 { 188 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev); 189 if (klass->set_interface) { 190 klass->set_interface(dev, interface, alt_old, alt_new); 191 } 192 } 193 194 void usb_device_flush_ep_queue(USBDevice *dev, USBEndpoint *ep) 195 { 196 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev); 197 if (klass->flush_ep_queue) { 198 klass->flush_ep_queue(dev, ep); 199 } 200 } 201 202 void usb_device_ep_stopped(USBDevice *dev, USBEndpoint *ep) 203 { 204 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev); 205 if (klass->ep_stopped) { 206 klass->ep_stopped(dev, ep); 207 } 208 } 209 210 int usb_device_alloc_streams(USBDevice *dev, USBEndpoint **eps, int nr_eps, 211 int streams) 212 { 213 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev); 214 if (klass->alloc_streams) { 215 return klass->alloc_streams(dev, eps, nr_eps, streams); 216 } 217 return 0; 218 } 219 220 void usb_device_free_streams(USBDevice *dev, USBEndpoint **eps, int nr_eps) 221 { 222 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev); 223 if (klass->free_streams) { 224 klass->free_streams(dev, eps, nr_eps); 225 } 226 } 227 228 static void usb_qdev_realize(DeviceState *qdev, Error **errp) 229 { 230 USBDevice *dev = USB_DEVICE(qdev); 231 Error *local_err = NULL; 232 233 pstrcpy(dev->product_desc, sizeof(dev->product_desc), 234 usb_device_get_product_desc(dev)); 235 dev->auto_attach = 1; 236 QLIST_INIT(&dev->strings); 237 usb_ep_init(dev); 238 239 usb_claim_port(dev, &local_err); 240 if (local_err) { 241 error_propagate(errp, local_err); 242 return; 243 } 244 245 usb_device_realize(dev, &local_err); 246 if (local_err) { 247 usb_release_port(dev); 248 error_propagate(errp, local_err); 249 return; 250 } 251 252 if (dev->auto_attach) { 253 usb_device_attach(dev, &local_err); 254 if (local_err) { 255 usb_qdev_unrealize(qdev); 256 error_propagate(errp, local_err); 257 return; 258 } 259 } 260 261 if (dev->pcap_filename) { 262 int fd = qemu_create(dev->pcap_filename, 263 O_WRONLY | O_TRUNC | O_BINARY, 0666, errp); 264 if (fd < 0) { 265 usb_qdev_unrealize(qdev); 266 return; 267 } 268 dev->pcap = fdopen(fd, "wb"); 269 usb_pcap_init(dev->pcap); 270 } 271 } 272 273 static void usb_qdev_unrealize(DeviceState *qdev) 274 { 275 USBDevice *dev = USB_DEVICE(qdev); 276 USBDescString *s, *next; 277 278 QLIST_FOREACH_SAFE(s, &dev->strings, next, next) { 279 QLIST_REMOVE(s, next); 280 g_free(s->str); 281 g_free(s); 282 } 283 284 if (dev->pcap) { 285 fclose(dev->pcap); 286 } 287 288 if (dev->attached) { 289 usb_device_detach(dev); 290 } 291 usb_device_unrealize(dev); 292 if (dev->port) { 293 usb_release_port(dev); 294 } 295 } 296 297 typedef struct LegacyUSBFactory 298 { 299 const char *name; 300 const char *usbdevice_name; 301 USBDevice *(*usbdevice_init)(void); 302 } LegacyUSBFactory; 303 304 static GSList *legacy_usb_factory; 305 306 void usb_legacy_register(const char *typename, const char *usbdevice_name, 307 USBDevice *(*usbdevice_init)(void)) 308 { 309 if (usbdevice_name) { 310 LegacyUSBFactory *f = g_malloc0(sizeof(*f)); 311 f->name = typename; 312 f->usbdevice_name = usbdevice_name; 313 f->usbdevice_init = usbdevice_init; 314 legacy_usb_factory = g_slist_append(legacy_usb_factory, f); 315 } 316 } 317 318 static void usb_fill_port(USBPort *port, void *opaque, int index, 319 USBPortOps *ops, int speedmask) 320 { 321 port->opaque = opaque; 322 port->index = index; 323 port->ops = ops; 324 port->speedmask = speedmask; 325 usb_port_location(port, NULL, index + 1); 326 } 327 328 void usb_register_port(USBBus *bus, USBPort *port, void *opaque, int index, 329 USBPortOps *ops, int speedmask) 330 { 331 usb_fill_port(port, opaque, index, ops, speedmask); 332 QTAILQ_INSERT_TAIL(&bus->free, port, next); 333 bus->nfree++; 334 } 335 336 void usb_register_companion(const char *masterbus, USBPort *ports[], 337 uint32_t portcount, uint32_t firstport, 338 void *opaque, USBPortOps *ops, int speedmask, 339 Error **errp) 340 { 341 USBBus *bus; 342 int i; 343 344 QTAILQ_FOREACH(bus, &busses, next) { 345 if (strcmp(bus->qbus.name, masterbus) == 0) { 346 break; 347 } 348 } 349 350 if (!bus) { 351 error_setg(errp, "USB bus '%s' not found", masterbus); 352 return; 353 } 354 if (!bus->ops->register_companion) { 355 error_setg(errp, "Can't use USB bus '%s' as masterbus," 356 " it doesn't support companion controllers", 357 masterbus); 358 return; 359 } 360 361 for (i = 0; i < portcount; i++) { 362 usb_fill_port(ports[i], opaque, i, ops, speedmask); 363 } 364 365 bus->ops->register_companion(bus, ports, portcount, firstport, errp); 366 } 367 368 void usb_port_location(USBPort *downstream, USBPort *upstream, int portnr) 369 { 370 if (upstream) { 371 int l = snprintf(downstream->path, sizeof(downstream->path), "%s.%d", 372 upstream->path, portnr); 373 /* Max string is nn.nn.nn.nn.nn, which fits in 16 bytes */ 374 assert(l < sizeof(downstream->path)); 375 downstream->hubcount = upstream->hubcount + 1; 376 } else { 377 snprintf(downstream->path, sizeof(downstream->path), "%d", portnr); 378 downstream->hubcount = 0; 379 } 380 } 381 382 void usb_unregister_port(USBBus *bus, USBPort *port) 383 { 384 if (port->dev) { 385 object_unparent(OBJECT(port->dev)); 386 } 387 QTAILQ_REMOVE(&bus->free, port, next); 388 bus->nfree--; 389 } 390 391 void usb_claim_port(USBDevice *dev, Error **errp) 392 { 393 USBBus *bus = usb_bus_from_device(dev); 394 USBPort *port; 395 USBDevice *hub; 396 397 assert(dev->port == NULL); 398 399 if (dev->port_path) { 400 QTAILQ_FOREACH(port, &bus->free, next) { 401 if (strcmp(port->path, dev->port_path) == 0) { 402 break; 403 } 404 } 405 if (port == NULL) { 406 error_setg(errp, "usb port %s (bus %s) not found (in use?)", 407 dev->port_path, bus->qbus.name); 408 return; 409 } 410 } else { 411 if (bus->nfree == 1 && strcmp(object_get_typename(OBJECT(dev)), "usb-hub") != 0) { 412 /* Create a new hub and chain it on */ 413 hub = USB_DEVICE(qdev_try_new("usb-hub")); 414 if (hub) { 415 usb_realize_and_unref(hub, bus, NULL); 416 } 417 } 418 if (bus->nfree == 0) { 419 error_setg(errp, "tried to attach usb device %s to a bus " 420 "with no free ports", dev->product_desc); 421 return; 422 } 423 port = QTAILQ_FIRST(&bus->free); 424 } 425 trace_usb_port_claim(bus->busnr, port->path); 426 427 QTAILQ_REMOVE(&bus->free, port, next); 428 bus->nfree--; 429 430 dev->port = port; 431 port->dev = dev; 432 433 QTAILQ_INSERT_TAIL(&bus->used, port, next); 434 bus->nused++; 435 } 436 437 void usb_release_port(USBDevice *dev) 438 { 439 USBBus *bus = usb_bus_from_device(dev); 440 USBPort *port = dev->port; 441 442 assert(port != NULL); 443 trace_usb_port_release(bus->busnr, port->path); 444 445 QTAILQ_REMOVE(&bus->used, port, next); 446 bus->nused--; 447 448 dev->port = NULL; 449 port->dev = NULL; 450 451 QTAILQ_INSERT_TAIL(&bus->free, port, next); 452 bus->nfree++; 453 } 454 455 static void usb_mask_to_str(char *dest, size_t size, 456 unsigned int speedmask) 457 { 458 static const struct { 459 unsigned int mask; 460 const char *name; 461 } speeds[] = { 462 { .mask = USB_SPEED_MASK_FULL, .name = "full" }, 463 { .mask = USB_SPEED_MASK_HIGH, .name = "high" }, 464 { .mask = USB_SPEED_MASK_SUPER, .name = "super" }, 465 }; 466 int i, pos = 0; 467 468 for (i = 0; i < ARRAY_SIZE(speeds); i++) { 469 if (speeds[i].mask & speedmask) { 470 pos += snprintf(dest + pos, size - pos, "%s%s", 471 pos ? "+" : "", 472 speeds[i].name); 473 } 474 } 475 476 if (pos == 0) { 477 snprintf(dest, size, "unknown"); 478 } 479 } 480 481 void usb_check_attach(USBDevice *dev, Error **errp) 482 { 483 USBBus *bus = usb_bus_from_device(dev); 484 USBPort *port = dev->port; 485 char devspeed[32], portspeed[32]; 486 487 assert(port != NULL); 488 assert(!dev->attached); 489 usb_mask_to_str(devspeed, sizeof(devspeed), dev->speedmask); 490 usb_mask_to_str(portspeed, sizeof(portspeed), port->speedmask); 491 trace_usb_port_attach(bus->busnr, port->path, 492 devspeed, portspeed); 493 494 if (!(port->speedmask & dev->speedmask)) { 495 error_setg(errp, "Warning: speed mismatch trying to attach" 496 " usb device \"%s\" (%s speed)" 497 " to bus \"%s\", port \"%s\" (%s speed)", 498 dev->product_desc, devspeed, 499 bus->qbus.name, port->path, portspeed); 500 return; 501 } 502 } 503 504 void usb_device_attach(USBDevice *dev, Error **errp) 505 { 506 USBPort *port = dev->port; 507 Error *local_err = NULL; 508 509 usb_check_attach(dev, &local_err); 510 if (local_err) { 511 error_propagate(errp, local_err); 512 return; 513 } 514 515 dev->attached = true; 516 usb_attach(port); 517 } 518 519 int usb_device_detach(USBDevice *dev) 520 { 521 USBBus *bus = usb_bus_from_device(dev); 522 USBPort *port = dev->port; 523 524 assert(port != NULL); 525 assert(dev->attached); 526 trace_usb_port_detach(bus->busnr, port->path); 527 528 usb_detach(port); 529 dev->attached = false; 530 return 0; 531 } 532 533 static const char *usb_speed(unsigned int speed) 534 { 535 static const char *txt[] = { 536 [ USB_SPEED_LOW ] = "1.5", 537 [ USB_SPEED_FULL ] = "12", 538 [ USB_SPEED_HIGH ] = "480", 539 [ USB_SPEED_SUPER ] = "5000", 540 }; 541 if (speed >= ARRAY_SIZE(txt)) 542 return "?"; 543 return txt[speed]; 544 } 545 546 static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent) 547 { 548 USBDevice *dev = USB_DEVICE(qdev); 549 USBBus *bus = usb_bus_from_device(dev); 550 551 monitor_printf(mon, "%*saddr %d.%d, port %s, speed %s, name %s%s\n", 552 indent, "", bus->busnr, dev->addr, 553 dev->port ? dev->port->path : "-", 554 usb_speed(dev->speed), dev->product_desc, 555 dev->attached ? ", attached" : ""); 556 } 557 558 static char *usb_get_dev_path(DeviceState *qdev) 559 { 560 USBDevice *dev = USB_DEVICE(qdev); 561 DeviceState *hcd = qdev->parent_bus->parent; 562 char *id = qdev_get_dev_path(hcd); 563 564 if (id) { 565 char *ret = g_strdup_printf("%s/%s", id, dev->port->path); 566 g_free(id); 567 return ret; 568 } else { 569 return g_strdup(dev->port->path); 570 } 571 } 572 573 static char *usb_get_fw_dev_path(DeviceState *qdev) 574 { 575 USBDevice *dev = USB_DEVICE(qdev); 576 char *fw_path, *in; 577 ssize_t pos = 0, fw_len; 578 long nr; 579 580 fw_len = 32 + strlen(dev->port->path) * 6; 581 fw_path = g_malloc(fw_len); 582 in = dev->port->path; 583 while (fw_len - pos > 0) { 584 nr = strtol(in, &in, 10); 585 if (in[0] == '.') { 586 /* some hub between root port and device */ 587 pos += snprintf(fw_path + pos, fw_len - pos, "hub@%lx/", nr); 588 in++; 589 } else { 590 /* the device itself */ 591 snprintf(fw_path + pos, fw_len - pos, "%s@%lx", 592 qdev_fw_name(qdev), nr); 593 break; 594 } 595 } 596 return fw_path; 597 } 598 599 HumanReadableText *qmp_x_query_usb(Error **errp) 600 { 601 g_autoptr(GString) buf = g_string_new(""); 602 USBBus *bus; 603 USBDevice *dev; 604 USBPort *port; 605 606 if (QTAILQ_EMPTY(&busses)) { 607 error_setg(errp, "USB support not enabled"); 608 return NULL; 609 } 610 611 QTAILQ_FOREACH(bus, &busses, next) { 612 QTAILQ_FOREACH(port, &bus->used, next) { 613 dev = port->dev; 614 if (!dev) 615 continue; 616 g_string_append_printf(buf, 617 " Device %d.%d, Port %s, Speed %s Mb/s, " 618 "Product %s%s%s\n", 619 bus->busnr, dev->addr, port->path, 620 usb_speed(dev->speed), dev->product_desc, 621 dev->qdev.id ? ", ID: " : "", 622 dev->qdev.id ?: ""); 623 } 624 } 625 626 return human_readable_text_from_str(buf); 627 } 628 629 /* handle legacy -usbdevice cmd line option */ 630 USBDevice *usbdevice_create(const char *driver) 631 { 632 USBBus *bus = QTAILQ_FIRST(&busses); 633 LegacyUSBFactory *f = NULL; 634 Error *err = NULL; 635 GSList *i; 636 USBDevice *dev; 637 638 if (strchr(driver, ':')) { 639 error_report("usbdevice parameters are not supported anymore"); 640 return NULL; 641 } 642 643 for (i = legacy_usb_factory; i; i = i->next) { 644 f = i->data; 645 if (strcmp(f->usbdevice_name, driver) == 0) { 646 break; 647 } 648 } 649 if (i == NULL) { 650 #if 0 651 /* no error because some drivers are not converted (yet) */ 652 error_report("usbdevice %s not found", driver); 653 #endif 654 return NULL; 655 } 656 657 if (!bus) { 658 error_report("Error: no usb bus to attach usbdevice %s, " 659 "please try -machine usb=on and check that " 660 "the machine model supports USB", driver); 661 return NULL; 662 } 663 664 dev = f->usbdevice_init ? f->usbdevice_init() 665 : USB_DEVICE(qdev_new(f->name)); 666 if (!dev) { 667 error_report("Failed to create USB device '%s'", f->name); 668 return NULL; 669 } 670 if (!usb_realize_and_unref(dev, bus, &err)) { 671 error_reportf_err(err, "Failed to initialize USB device '%s': ", 672 f->name); 673 object_unparent(OBJECT(dev)); 674 return NULL; 675 } 676 return dev; 677 } 678 679 static bool usb_get_attached(Object *obj, Error **errp) 680 { 681 USBDevice *dev = USB_DEVICE(obj); 682 683 return dev->attached; 684 } 685 686 static void usb_set_attached(Object *obj, bool value, Error **errp) 687 { 688 USBDevice *dev = USB_DEVICE(obj); 689 690 if (dev->attached == value) { 691 return; 692 } 693 694 if (value) { 695 usb_device_attach(dev, errp); 696 } else { 697 usb_device_detach(dev); 698 } 699 } 700 701 static void usb_device_instance_init(Object *obj) 702 { 703 USBDevice *dev = USB_DEVICE(obj); 704 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev); 705 706 if (klass->attached_settable) { 707 object_property_add_bool(obj, "attached", 708 usb_get_attached, usb_set_attached); 709 } else { 710 object_property_add_bool(obj, "attached", 711 usb_get_attached, NULL); 712 } 713 } 714 715 static void usb_device_class_init(ObjectClass *klass, const void *data) 716 { 717 DeviceClass *k = DEVICE_CLASS(klass); 718 k->bus_type = TYPE_USB_BUS; 719 k->realize = usb_qdev_realize; 720 k->unrealize = usb_qdev_unrealize; 721 device_class_set_props(k, usb_props); 722 } 723 724 static const TypeInfo usb_device_type_info = { 725 .name = TYPE_USB_DEVICE, 726 .parent = TYPE_DEVICE, 727 .instance_size = sizeof(USBDevice), 728 .instance_init = usb_device_instance_init, 729 .abstract = true, 730 .class_size = sizeof(USBDeviceClass), 731 .class_init = usb_device_class_init, 732 }; 733 734 static void usb_register_types(void) 735 { 736 type_register_static(&usb_bus_info); 737 type_register_static(&usb_device_type_info); 738 } 739 740 type_init(usb_register_types) 741