usb: add usb_bus_release function
[qemu.git] / hw / usb / bus.c
blobc7c4dadedd2b1c256cbb875fe6ca4a6e61a5bd29
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"
8 static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent);
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);
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()
24 static void usb_bus_class_init(ObjectClass *klass, void *data)
26 BusClass *k = BUS_CLASS(klass);
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;
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,
40 static int next_usb_bus = 0;
41 static QTAILQ_HEAD(, USBBus) busses = QTAILQ_HEAD_INITIALIZER(busses);
43 static int usb_device_post_load(void *opaque, int version_id)
45 USBDevice *dev = opaque;
47 if (dev->state == USB_STATE_NOTATTACHED) {
48 dev->attached = 0;
49 } else {
50 dev->attached = 1;
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;
58 return 0;
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(),
78 void usb_bus_new(USBBus *bus, size_t bus_size,
79 USBBusOps *ops, DeviceState *host)
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);
90 void usb_bus_release(USBBus *bus)
92 assert(next_usb_bus > 0);
94 QTAILQ_REMOVE(&busses, bus, next);
97 USBBus *usb_bus_find(int busnr)
99 USBBus *bus;
101 if (-1 == busnr)
102 return QTAILQ_FIRST(&busses);
103 QTAILQ_FOREACH(bus, &busses, next) {
104 if (bus->busnr == busnr)
105 return bus;
107 return NULL;
110 static int usb_device_init(USBDevice *dev)
112 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
113 if (klass->init) {
114 return klass->init(dev);
116 return 0;
119 USBDevice *usb_device_find_device(USBDevice *dev, uint8_t addr)
121 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
122 if (klass->find_device) {
123 return klass->find_device(dev, addr);
125 return NULL;
128 static void usb_device_handle_destroy(USBDevice *dev)
130 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
131 if (klass->handle_destroy) {
132 klass->handle_destroy(dev);
136 void usb_device_cancel_packet(USBDevice *dev, USBPacket *p)
138 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
139 if (klass->cancel_packet) {
140 klass->cancel_packet(dev, p);
144 void usb_device_handle_attach(USBDevice *dev)
146 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
147 if (klass->handle_attach) {
148 klass->handle_attach(dev);
152 void usb_device_handle_reset(USBDevice *dev)
154 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
155 if (klass->handle_reset) {
156 klass->handle_reset(dev);
160 void usb_device_handle_control(USBDevice *dev, USBPacket *p, int request,
161 int value, int index, int length, uint8_t *data)
163 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
164 if (klass->handle_control) {
165 klass->handle_control(dev, p, request, value, index, length, data);
169 void usb_device_handle_data(USBDevice *dev, USBPacket *p)
171 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
172 if (klass->handle_data) {
173 klass->handle_data(dev, p);
177 const char *usb_device_get_product_desc(USBDevice *dev)
179 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
180 return klass->product_desc;
183 const USBDesc *usb_device_get_usb_desc(USBDevice *dev)
185 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
186 if (dev->usb_desc) {
187 return dev->usb_desc;
189 return klass->usb_desc;
192 void usb_device_set_interface(USBDevice *dev, int interface,
193 int alt_old, int alt_new)
195 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
196 if (klass->set_interface) {
197 klass->set_interface(dev, interface, alt_old, alt_new);
201 void usb_device_flush_ep_queue(USBDevice *dev, USBEndpoint *ep)
203 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
204 if (klass->flush_ep_queue) {
205 klass->flush_ep_queue(dev, ep);
209 void usb_device_ep_stopped(USBDevice *dev, USBEndpoint *ep)
211 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
212 if (klass->ep_stopped) {
213 klass->ep_stopped(dev, ep);
217 int usb_device_alloc_streams(USBDevice *dev, USBEndpoint **eps, int nr_eps,
218 int streams)
220 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
221 if (klass->alloc_streams) {
222 return klass->alloc_streams(dev, eps, nr_eps, streams);
224 return 0;
227 void usb_device_free_streams(USBDevice *dev, USBEndpoint **eps, int nr_eps)
229 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
230 if (klass->free_streams) {
231 klass->free_streams(dev, eps, nr_eps);
235 static int usb_qdev_init(DeviceState *qdev)
237 USBDevice *dev = USB_DEVICE(qdev);
238 int rc;
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;
249 rc = usb_device_init(dev);
250 if (rc != 0) {
251 usb_release_port(dev);
252 return rc;
254 if (dev->auto_attach) {
255 rc = usb_device_attach(dev);
256 if (rc != 0) {
257 usb_qdev_exit(qdev);
258 return rc;
261 return 0;
264 static int usb_qdev_exit(DeviceState *qdev)
266 USBDevice *dev = USB_DEVICE(qdev);
268 if (dev->attached) {
269 usb_device_detach(dev);
271 usb_device_handle_destroy(dev);
272 if (dev->port) {
273 usb_release_port(dev);
275 return 0;
278 typedef struct LegacyUSBFactory
280 const char *name;
281 const char *usbdevice_name;
282 USBDevice *(*usbdevice_init)(USBBus *bus, const char *params);
283 } LegacyUSBFactory;
285 static GSList *legacy_usb_factory;
287 void usb_legacy_register(const char *typename, const char *usbdevice_name,
288 USBDevice *(*usbdevice_init)(USBBus *bus,
289 const char *params))
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);
300 USBDevice *usb_create(USBBus *bus, const char *name)
302 DeviceState *dev;
304 dev = qdev_create(&bus->qbus, name);
305 return USB_DEVICE(dev);
308 USBDevice *usb_create_simple(USBBus *bus, const char *name)
310 USBDevice *dev = usb_create(bus, name);
311 int rc;
313 if (!dev) {
314 error_report("Failed to create USB device '%s'", name);
315 return NULL;
317 rc = qdev_init(&dev->qdev);
318 if (rc < 0) {
319 error_report("Failed to initialize USB device '%s'", name);
320 return NULL;
322 return dev;
325 static void usb_fill_port(USBPort *port, void *opaque, int index,
326 USBPortOps *ops, int speedmask)
328 port->opaque = opaque;
329 port->index = index;
330 port->ops = ops;
331 port->speedmask = speedmask;
332 usb_port_location(port, NULL, index + 1);
335 void usb_register_port(USBBus *bus, USBPort *port, void *opaque, int index,
336 USBPortOps *ops, int speedmask)
338 usb_fill_port(port, opaque, index, ops, speedmask);
339 QTAILQ_INSERT_TAIL(&bus->free, port, next);
340 bus->nfree++;
343 int usb_register_companion(const char *masterbus, USBPort *ports[],
344 uint32_t portcount, uint32_t firstport,
345 void *opaque, USBPortOps *ops, int speedmask)
347 USBBus *bus;
348 int i;
350 QTAILQ_FOREACH(bus, &busses, next) {
351 if (strcmp(bus->qbus.name, masterbus) == 0) {
352 break;
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);
364 return -1;
367 for (i = 0; i < portcount; i++) {
368 usb_fill_port(ports[i], opaque, i, ops, speedmask);
371 return bus->ops->register_companion(bus, ports, portcount, firstport);
374 void usb_port_location(USBPort *downstream, USBPort *upstream, int portnr)
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;
386 void usb_unregister_port(USBBus *bus, USBPort *port)
388 if (port->dev) {
389 object_unparent(OBJECT(port->dev));
391 QTAILQ_REMOVE(&bus->free, port, next);
392 bus->nfree--;
395 int usb_claim_port(USBDevice *dev)
397 USBBus *bus = usb_bus_from_device(dev);
398 USBPort *port;
400 assert(dev->port == NULL);
402 if (dev->port_path) {
403 QTAILQ_FOREACH(port, &bus->free, next) {
404 if (strcmp(port->path, dev->port_path) == 0) {
405 break;
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;
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");
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;
423 port = QTAILQ_FIRST(&bus->free);
425 trace_usb_port_claim(bus->busnr, port->path);
427 QTAILQ_REMOVE(&bus->free, port, next);
428 bus->nfree--;
430 dev->port = port;
431 port->dev = dev;
433 QTAILQ_INSERT_TAIL(&bus->used, port, next);
434 bus->nused++;
435 return 0;
438 void usb_release_port(USBDevice *dev)
440 USBBus *bus = usb_bus_from_device(dev);
441 USBPort *port = dev->port;
443 assert(port != NULL);
444 trace_usb_port_release(bus->busnr, port->path);
446 QTAILQ_REMOVE(&bus->used, port, next);
447 bus->nused--;
449 dev->port = NULL;
450 port->dev = NULL;
452 QTAILQ_INSERT_TAIL(&bus->free, port, next);
453 bus->nfree++;
456 static void usb_mask_to_str(char *dest, size_t size,
457 unsigned int speedmask)
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" },
467 int i, pos = 0;
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);
478 int usb_device_attach(USBDevice *dev)
480 USBBus *bus = usb_bus_from_device(dev);
481 USBPort *port = dev->port;
482 char devspeed[32], portspeed[32];
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);
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;
500 dev->attached++;
501 usb_attach(port);
503 return 0;
506 int usb_device_detach(USBDevice *dev)
508 USBBus *bus = usb_bus_from_device(dev);
509 USBPort *port = dev->port;
511 assert(port != NULL);
512 assert(dev->attached);
513 trace_usb_port_detach(bus->busnr, port->path);
515 usb_detach(port);
516 dev->attached--;
517 return 0;
520 int usb_device_delete_addr(int busnr, int addr)
522 USBBus *bus;
523 USBPort *port;
524 USBDevice *dev;
526 bus = usb_bus_find(busnr);
527 if (!bus)
528 return -1;
530 QTAILQ_FOREACH(port, &bus->used, next) {
531 if (port->dev->addr == addr)
532 break;
534 if (!port)
535 return -1;
536 dev = port->dev;
538 object_unparent(OBJECT(dev));
539 return 0;
542 static const char *usb_speed(unsigned int speed)
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",
550 if (speed >= ARRAY_SIZE(txt))
551 return "?";
552 return txt[speed];
555 static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
557 USBDevice *dev = USB_DEVICE(qdev);
558 USBBus *bus = usb_bus_from_device(dev);
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" : "");
567 static char *usb_get_dev_path(DeviceState *qdev)
569 USBDevice *dev = USB_DEVICE(qdev);
570 DeviceState *hcd = qdev->parent_bus->parent;
571 char *id = NULL;
573 if (dev->flags & (1 << USB_DEV_FLAG_FULL_PATH)) {
574 id = qdev_get_dev_path(hcd);
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);
585 static char *usb_get_fw_dev_path(DeviceState *qdev)
587 USBDevice *dev = USB_DEVICE(qdev);
588 char *fw_path, *in;
589 ssize_t pos = 0, fw_len;
590 long nr;
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;
608 return fw_path;
611 void usb_info(Monitor *mon, const QDict *qdict)
613 USBBus *bus;
614 USBDevice *dev;
615 USBPort *port;
617 if (QTAILQ_EMPTY(&busses)) {
618 monitor_printf(mon, "USB support not enabled\n");
619 return;
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);
634 /* handle legacy -usbdevice cmd line option */
635 USBDevice *usbdevice_create(const char *cmdline)
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;
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);
656 for (i = legacy_usb_factory; i; i = i->next) {
657 f = i->data;
658 if (strcmp(f->usbdevice_name, driver) == 0) {
659 break;
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;
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;
677 if (!f->usbdevice_init) {
678 if (*params) {
679 error_report("usbdevice %s accepts no params", driver);
680 return NULL;
682 return usb_create_simple(bus, f->name);
684 return f->usbdevice_init(bus, params);
687 static void usb_device_class_init(ObjectClass *klass, void *data)
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;
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,
706 static void usb_register_types(void)
708 type_register_static(&usb_bus_info);
709 type_register_static(&usb_device_type_info);
712 type_init(usb_register_types)