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