colo: Consolidate the duplicate code chunk into a routine
[qemu/ar7.git] / hw / usb / bus.c
blobe56dc3348a23641731fdc6462c407ccf40aa4d72
1 #include "qemu/osdep.h"
2 #include "hw/hw.h"
3 #include "hw/usb.h"
4 #include "hw/qdev.h"
5 #include "qapi/error.h"
6 #include "qemu/error-report.h"
7 #include "sysemu/sysemu.h"
8 #include "monitor/monitor.h"
9 #include "trace.h"
10 #include "qemu/cutils.h"
12 static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent);
14 static char *usb_get_dev_path(DeviceState *dev);
15 static char *usb_get_fw_dev_path(DeviceState *qdev);
16 static void usb_qdev_unrealize(DeviceState *qdev, Error **errp);
18 static Property usb_props[] = {
19 DEFINE_PROP_STRING("port", USBDevice, port_path),
20 DEFINE_PROP_STRING("serial", USBDevice, serial),
21 DEFINE_PROP_BIT("full-path", USBDevice, flags,
22 USB_DEV_FLAG_FULL_PATH, true),
23 DEFINE_PROP_BIT("msos-desc", USBDevice, flags,
24 USB_DEV_FLAG_MSOS_DESC_ENABLE, true),
25 DEFINE_PROP_END_OF_LIST()
28 static void usb_bus_class_init(ObjectClass *klass, void *data)
30 BusClass *k = BUS_CLASS(klass);
31 HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
33 k->print_dev = usb_bus_dev_print;
34 k->get_dev_path = usb_get_dev_path;
35 k->get_fw_dev_path = usb_get_fw_dev_path;
36 hc->unplug = qdev_simple_device_unplug_cb;
39 static const TypeInfo usb_bus_info = {
40 .name = TYPE_USB_BUS,
41 .parent = TYPE_BUS,
42 .instance_size = sizeof(USBBus),
43 .class_init = usb_bus_class_init,
44 .interfaces = (InterfaceInfo[]) {
45 { TYPE_HOTPLUG_HANDLER },
46 { }
50 static int next_usb_bus = 0;
51 static QTAILQ_HEAD(, USBBus) busses = QTAILQ_HEAD_INITIALIZER(busses);
53 static int usb_device_post_load(void *opaque, int version_id)
55 USBDevice *dev = opaque;
57 if (dev->state == USB_STATE_NOTATTACHED) {
58 dev->attached = false;
59 } else {
60 dev->attached = true;
62 if (dev->setup_index < 0 ||
63 dev->setup_len < 0 ||
64 dev->setup_index > dev->setup_len ||
65 dev->setup_len > sizeof(dev->data_buf)) {
66 return -EINVAL;
68 return 0;
71 const VMStateDescription vmstate_usb_device = {
72 .name = "USBDevice",
73 .version_id = 1,
74 .minimum_version_id = 1,
75 .post_load = usb_device_post_load,
76 .fields = (VMStateField[]) {
77 VMSTATE_UINT8(addr, USBDevice),
78 VMSTATE_INT32(state, USBDevice),
79 VMSTATE_INT32(remote_wakeup, USBDevice),
80 VMSTATE_INT32(setup_state, USBDevice),
81 VMSTATE_INT32(setup_len, USBDevice),
82 VMSTATE_INT32(setup_index, USBDevice),
83 VMSTATE_UINT8_ARRAY(setup_buf, USBDevice, 8),
84 VMSTATE_END_OF_LIST(),
88 void usb_bus_new(USBBus *bus, size_t bus_size,
89 USBBusOps *ops, DeviceState *host)
91 qbus_create_inplace(bus, bus_size, TYPE_USB_BUS, host, NULL);
92 qbus_set_bus_hotplug_handler(BUS(bus), &error_abort);
93 bus->ops = ops;
94 bus->busnr = next_usb_bus++;
95 QTAILQ_INIT(&bus->free);
96 QTAILQ_INIT(&bus->used);
97 QTAILQ_INSERT_TAIL(&busses, bus, next);
100 void usb_bus_release(USBBus *bus)
102 assert(next_usb_bus > 0);
104 QTAILQ_REMOVE(&busses, bus, next);
107 USBBus *usb_bus_find(int busnr)
109 USBBus *bus;
111 if (-1 == busnr)
112 return QTAILQ_FIRST(&busses);
113 QTAILQ_FOREACH(bus, &busses, next) {
114 if (bus->busnr == busnr)
115 return bus;
117 return NULL;
120 static void usb_device_realize(USBDevice *dev, Error **errp)
122 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
124 if (klass->realize) {
125 klass->realize(dev, errp);
129 USBDevice *usb_device_find_device(USBDevice *dev, uint8_t addr)
131 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
132 if (klass->find_device) {
133 return klass->find_device(dev, addr);
135 return NULL;
138 static void usb_device_unrealize(USBDevice *dev, Error **errp)
140 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
142 if (klass->unrealize) {
143 klass->unrealize(dev, errp);
147 void usb_device_cancel_packet(USBDevice *dev, USBPacket *p)
149 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
150 if (klass->cancel_packet) {
151 klass->cancel_packet(dev, p);
155 void usb_device_handle_attach(USBDevice *dev)
157 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
158 if (klass->handle_attach) {
159 klass->handle_attach(dev);
163 void usb_device_handle_reset(USBDevice *dev)
165 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
166 if (klass->handle_reset) {
167 klass->handle_reset(dev);
171 void usb_device_handle_control(USBDevice *dev, USBPacket *p, int request,
172 int value, int index, int length, uint8_t *data)
174 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
175 if (klass->handle_control) {
176 klass->handle_control(dev, p, request, value, index, length, data);
180 void usb_device_handle_data(USBDevice *dev, USBPacket *p)
182 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
183 if (klass->handle_data) {
184 klass->handle_data(dev, p);
188 const char *usb_device_get_product_desc(USBDevice *dev)
190 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
191 return klass->product_desc;
194 const USBDesc *usb_device_get_usb_desc(USBDevice *dev)
196 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
197 if (dev->usb_desc) {
198 return dev->usb_desc;
200 return klass->usb_desc;
203 void usb_device_set_interface(USBDevice *dev, int interface,
204 int alt_old, int alt_new)
206 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
207 if (klass->set_interface) {
208 klass->set_interface(dev, interface, alt_old, alt_new);
212 void usb_device_flush_ep_queue(USBDevice *dev, USBEndpoint *ep)
214 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
215 if (klass->flush_ep_queue) {
216 klass->flush_ep_queue(dev, ep);
220 void usb_device_ep_stopped(USBDevice *dev, USBEndpoint *ep)
222 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
223 if (klass->ep_stopped) {
224 klass->ep_stopped(dev, ep);
228 int usb_device_alloc_streams(USBDevice *dev, USBEndpoint **eps, int nr_eps,
229 int streams)
231 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
232 if (klass->alloc_streams) {
233 return klass->alloc_streams(dev, eps, nr_eps, streams);
235 return 0;
238 void usb_device_free_streams(USBDevice *dev, USBEndpoint **eps, int nr_eps)
240 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
241 if (klass->free_streams) {
242 klass->free_streams(dev, eps, nr_eps);
246 static void usb_qdev_realize(DeviceState *qdev, Error **errp)
248 USBDevice *dev = USB_DEVICE(qdev);
249 Error *local_err = NULL;
251 pstrcpy(dev->product_desc, sizeof(dev->product_desc),
252 usb_device_get_product_desc(dev));
253 dev->auto_attach = 1;
254 QLIST_INIT(&dev->strings);
255 usb_ep_init(dev);
257 usb_claim_port(dev, &local_err);
258 if (local_err) {
259 error_propagate(errp, local_err);
260 return;
263 usb_device_realize(dev, &local_err);
264 if (local_err) {
265 usb_release_port(dev);
266 error_propagate(errp, local_err);
267 return;
270 if (dev->auto_attach) {
271 usb_device_attach(dev, &local_err);
272 if (local_err) {
273 usb_qdev_unrealize(qdev, NULL);
274 error_propagate(errp, local_err);
275 return;
280 static void usb_qdev_unrealize(DeviceState *qdev, Error **errp)
282 USBDevice *dev = USB_DEVICE(qdev);
283 USBDescString *s, *next;
285 QLIST_FOREACH_SAFE(s, &dev->strings, next, next) {
286 QLIST_REMOVE(s, next);
287 g_free(s->str);
288 g_free(s);
291 if (dev->attached) {
292 usb_device_detach(dev);
294 usb_device_unrealize(dev, errp);
295 if (dev->port) {
296 usb_release_port(dev);
300 typedef struct LegacyUSBFactory
302 const char *name;
303 const char *usbdevice_name;
304 USBDevice *(*usbdevice_init)(USBBus *bus, const char *params);
305 } LegacyUSBFactory;
307 static GSList *legacy_usb_factory;
309 void usb_legacy_register(const char *typename, const char *usbdevice_name,
310 USBDevice *(*usbdevice_init)(USBBus *bus,
311 const char *params))
313 if (usbdevice_name) {
314 LegacyUSBFactory *f = g_malloc0(sizeof(*f));
315 f->name = typename;
316 f->usbdevice_name = usbdevice_name;
317 f->usbdevice_init = usbdevice_init;
318 legacy_usb_factory = g_slist_append(legacy_usb_factory, f);
322 USBDevice *usb_create(USBBus *bus, const char *name)
324 DeviceState *dev;
326 dev = qdev_create(&bus->qbus, name);
327 return USB_DEVICE(dev);
330 static USBDevice *usb_try_create_simple(USBBus *bus, const char *name,
331 Error **errp)
333 Error *err = NULL;
334 USBDevice *dev;
336 dev = USB_DEVICE(qdev_try_create(&bus->qbus, name));
337 if (!dev) {
338 error_setg(errp, "Failed to create USB device '%s'", name);
339 return NULL;
341 object_property_set_bool(OBJECT(dev), true, "realized", &err);
342 if (err) {
343 error_propagate(errp, err);
344 error_prepend(errp, "Failed to initialize USB device '%s': ", name);
345 return NULL;
347 return dev;
350 USBDevice *usb_create_simple(USBBus *bus, const char *name)
352 return usb_try_create_simple(bus, name, &error_abort);
355 static void usb_fill_port(USBPort *port, void *opaque, int index,
356 USBPortOps *ops, int speedmask)
358 port->opaque = opaque;
359 port->index = index;
360 port->ops = ops;
361 port->speedmask = speedmask;
362 usb_port_location(port, NULL, index + 1);
365 void usb_register_port(USBBus *bus, USBPort *port, void *opaque, int index,
366 USBPortOps *ops, int speedmask)
368 usb_fill_port(port, opaque, index, ops, speedmask);
369 QTAILQ_INSERT_TAIL(&bus->free, port, next);
370 bus->nfree++;
373 void usb_register_companion(const char *masterbus, USBPort *ports[],
374 uint32_t portcount, uint32_t firstport,
375 void *opaque, USBPortOps *ops, int speedmask,
376 Error **errp)
378 USBBus *bus;
379 int i;
381 QTAILQ_FOREACH(bus, &busses, next) {
382 if (strcmp(bus->qbus.name, masterbus) == 0) {
383 break;
387 if (!bus) {
388 error_setg(errp, "USB bus '%s' not found", masterbus);
389 return;
391 if (!bus->ops->register_companion) {
392 error_setg(errp, "Can't use USB bus '%s' as masterbus,"
393 " it doesn't support companion controllers",
394 masterbus);
395 return;
398 for (i = 0; i < portcount; i++) {
399 usb_fill_port(ports[i], opaque, i, ops, speedmask);
402 bus->ops->register_companion(bus, ports, portcount, firstport, errp);
405 void usb_port_location(USBPort *downstream, USBPort *upstream, int portnr)
407 if (upstream) {
408 int l = snprintf(downstream->path, sizeof(downstream->path), "%s.%d",
409 upstream->path, portnr);
410 /* Max string is nn.nn.nn.nn.nn, which fits in 16 bytes */
411 assert(l < sizeof(downstream->path));
412 downstream->hubcount = upstream->hubcount + 1;
413 } else {
414 snprintf(downstream->path, sizeof(downstream->path), "%d", portnr);
415 downstream->hubcount = 0;
419 void usb_unregister_port(USBBus *bus, USBPort *port)
421 if (port->dev) {
422 object_unparent(OBJECT(port->dev));
424 QTAILQ_REMOVE(&bus->free, port, next);
425 bus->nfree--;
428 void usb_claim_port(USBDevice *dev, Error **errp)
430 USBBus *bus = usb_bus_from_device(dev);
431 USBPort *port;
433 assert(dev->port == NULL);
435 if (dev->port_path) {
436 QTAILQ_FOREACH(port, &bus->free, next) {
437 if (strcmp(port->path, dev->port_path) == 0) {
438 break;
441 if (port == NULL) {
442 error_setg(errp, "usb port %s (bus %s) not found (in use?)",
443 dev->port_path, bus->qbus.name);
444 return;
446 } else {
447 if (bus->nfree == 1 && strcmp(object_get_typename(OBJECT(dev)), "usb-hub") != 0) {
448 /* Create a new hub and chain it on */
449 usb_try_create_simple(bus, "usb-hub", NULL);
451 if (bus->nfree == 0) {
452 error_setg(errp, "tried to attach usb device %s to a bus "
453 "with no free ports", dev->product_desc);
454 return;
456 port = QTAILQ_FIRST(&bus->free);
458 trace_usb_port_claim(bus->busnr, port->path);
460 QTAILQ_REMOVE(&bus->free, port, next);
461 bus->nfree--;
463 dev->port = port;
464 port->dev = dev;
466 QTAILQ_INSERT_TAIL(&bus->used, port, next);
467 bus->nused++;
470 void usb_release_port(USBDevice *dev)
472 USBBus *bus = usb_bus_from_device(dev);
473 USBPort *port = dev->port;
475 assert(port != NULL);
476 trace_usb_port_release(bus->busnr, port->path);
478 QTAILQ_REMOVE(&bus->used, port, next);
479 bus->nused--;
481 dev->port = NULL;
482 port->dev = NULL;
484 QTAILQ_INSERT_TAIL(&bus->free, port, next);
485 bus->nfree++;
488 static void usb_mask_to_str(char *dest, size_t size,
489 unsigned int speedmask)
491 static const struct {
492 unsigned int mask;
493 const char *name;
494 } speeds[] = {
495 { .mask = USB_SPEED_MASK_FULL, .name = "full" },
496 { .mask = USB_SPEED_MASK_HIGH, .name = "high" },
497 { .mask = USB_SPEED_MASK_SUPER, .name = "super" },
499 int i, pos = 0;
501 for (i = 0; i < ARRAY_SIZE(speeds); i++) {
502 if (speeds[i].mask & speedmask) {
503 pos += snprintf(dest + pos, size - pos, "%s%s",
504 pos ? "+" : "",
505 speeds[i].name);
510 void usb_check_attach(USBDevice *dev, Error **errp)
512 USBBus *bus = usb_bus_from_device(dev);
513 USBPort *port = dev->port;
514 char devspeed[32], portspeed[32];
516 assert(port != NULL);
517 assert(!dev->attached);
518 usb_mask_to_str(devspeed, sizeof(devspeed), dev->speedmask);
519 usb_mask_to_str(portspeed, sizeof(portspeed), port->speedmask);
520 trace_usb_port_attach(bus->busnr, port->path,
521 devspeed, portspeed);
523 if (!(port->speedmask & dev->speedmask)) {
524 error_setg(errp, "Warning: speed mismatch trying to attach"
525 " usb device \"%s\" (%s speed)"
526 " to bus \"%s\", port \"%s\" (%s speed)",
527 dev->product_desc, devspeed,
528 bus->qbus.name, port->path, portspeed);
529 return;
533 void usb_device_attach(USBDevice *dev, Error **errp)
535 USBPort *port = dev->port;
536 Error *local_err = NULL;
538 usb_check_attach(dev, &local_err);
539 if (local_err) {
540 error_propagate(errp, local_err);
541 return;
544 dev->attached = true;
545 usb_attach(port);
548 int usb_device_detach(USBDevice *dev)
550 USBBus *bus = usb_bus_from_device(dev);
551 USBPort *port = dev->port;
553 assert(port != NULL);
554 assert(dev->attached);
555 trace_usb_port_detach(bus->busnr, port->path);
557 usb_detach(port);
558 dev->attached = false;
559 return 0;
562 int usb_device_delete_addr(int busnr, int addr)
564 USBBus *bus;
565 USBPort *port;
566 USBDevice *dev;
568 bus = usb_bus_find(busnr);
569 if (!bus)
570 return -1;
572 QTAILQ_FOREACH(port, &bus->used, next) {
573 if (port->dev->addr == addr)
574 break;
576 if (!port)
577 return -1;
578 dev = port->dev;
580 object_unparent(OBJECT(dev));
581 return 0;
584 static const char *usb_speed(unsigned int speed)
586 static const char *txt[] = {
587 [ USB_SPEED_LOW ] = "1.5",
588 [ USB_SPEED_FULL ] = "12",
589 [ USB_SPEED_HIGH ] = "480",
590 [ USB_SPEED_SUPER ] = "5000",
592 if (speed >= ARRAY_SIZE(txt))
593 return "?";
594 return txt[speed];
597 static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
599 USBDevice *dev = USB_DEVICE(qdev);
600 USBBus *bus = usb_bus_from_device(dev);
602 monitor_printf(mon, "%*saddr %d.%d, port %s, speed %s, name %s%s\n",
603 indent, "", bus->busnr, dev->addr,
604 dev->port ? dev->port->path : "-",
605 usb_speed(dev->speed), dev->product_desc,
606 dev->attached ? ", attached" : "");
609 static char *usb_get_dev_path(DeviceState *qdev)
611 USBDevice *dev = USB_DEVICE(qdev);
612 DeviceState *hcd = qdev->parent_bus->parent;
613 char *id = NULL;
615 if (dev->flags & (1 << USB_DEV_FLAG_FULL_PATH)) {
616 id = qdev_get_dev_path(hcd);
618 if (id) {
619 char *ret = g_strdup_printf("%s/%s", id, dev->port->path);
620 g_free(id);
621 return ret;
622 } else {
623 return g_strdup(dev->port->path);
627 static char *usb_get_fw_dev_path(DeviceState *qdev)
629 USBDevice *dev = USB_DEVICE(qdev);
630 char *fw_path, *in;
631 ssize_t pos = 0, fw_len;
632 long nr;
634 fw_len = 32 + strlen(dev->port->path) * 6;
635 fw_path = g_malloc(fw_len);
636 in = dev->port->path;
637 while (fw_len - pos > 0) {
638 nr = strtol(in, &in, 10);
639 if (in[0] == '.') {
640 /* some hub between root port and device */
641 pos += snprintf(fw_path + pos, fw_len - pos, "hub@%lx/", nr);
642 in++;
643 } else {
644 /* the device itself */
645 pos += snprintf(fw_path + pos, fw_len - pos, "%s@%lx",
646 qdev_fw_name(qdev), nr);
647 break;
650 return fw_path;
653 void hmp_info_usb(Monitor *mon, const QDict *qdict)
655 USBBus *bus;
656 USBDevice *dev;
657 USBPort *port;
659 if (QTAILQ_EMPTY(&busses)) {
660 monitor_printf(mon, "USB support not enabled\n");
661 return;
664 QTAILQ_FOREACH(bus, &busses, next) {
665 QTAILQ_FOREACH(port, &bus->used, next) {
666 dev = port->dev;
667 if (!dev)
668 continue;
669 monitor_printf(mon, " Device %d.%d, Port %s, Speed %s Mb/s, "
670 "Product %s%s%s\n",
671 bus->busnr, dev->addr, port->path,
672 usb_speed(dev->speed), dev->product_desc,
673 dev->qdev.id ? ", ID: " : "",
674 dev->qdev.id ?: "");
679 /* handle legacy -usbdevice cmd line option */
680 USBDevice *usbdevice_create(const char *cmdline)
682 USBBus *bus = usb_bus_find(-1 /* any */);
683 LegacyUSBFactory *f = NULL;
684 Error *err = NULL;
685 GSList *i;
686 char driver[32];
687 const char *params;
688 int len;
689 USBDevice *dev;
691 params = strchr(cmdline,':');
692 if (params) {
693 params++;
694 len = params - cmdline;
695 if (len > sizeof(driver))
696 len = sizeof(driver);
697 pstrcpy(driver, len, cmdline);
698 } else {
699 params = "";
700 pstrcpy(driver, sizeof(driver), cmdline);
703 for (i = legacy_usb_factory; i; i = i->next) {
704 f = i->data;
705 if (strcmp(f->usbdevice_name, driver) == 0) {
706 break;
709 if (i == NULL) {
710 #if 0
711 /* no error because some drivers are not converted (yet) */
712 error_report("usbdevice %s not found", driver);
713 #endif
714 return NULL;
717 if (!bus) {
718 error_report("Error: no usb bus to attach usbdevice %s, "
719 "please try -machine usb=on and check that "
720 "the machine model supports USB", driver);
721 return NULL;
724 if (f->usbdevice_init) {
725 dev = f->usbdevice_init(bus, params);
726 } else {
727 if (*params) {
728 error_report("usbdevice %s accepts no params", driver);
729 return NULL;
731 dev = usb_create(bus, f->name);
733 if (!dev) {
734 error_report("Failed to create USB device '%s'", f->name);
735 return NULL;
737 object_property_set_bool(OBJECT(dev), true, "realized", &err);
738 if (err) {
739 error_reportf_err(err, "Failed to initialize USB device '%s': ",
740 f->name);
741 object_unparent(OBJECT(dev));
742 return NULL;
744 return dev;
747 static bool usb_get_attached(Object *obj, Error **errp)
749 USBDevice *dev = USB_DEVICE(obj);
751 return dev->attached;
754 static void usb_set_attached(Object *obj, bool value, Error **errp)
756 USBDevice *dev = USB_DEVICE(obj);
757 Error *err = NULL;
759 if (dev->attached == value) {
760 return;
763 if (value) {
764 usb_device_attach(dev, &err);
765 error_propagate(errp, err);
766 } else {
767 usb_device_detach(dev);
771 static void usb_device_instance_init(Object *obj)
773 USBDevice *dev = USB_DEVICE(obj);
774 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
776 if (klass->attached_settable) {
777 object_property_add_bool(obj, "attached",
778 usb_get_attached, usb_set_attached,
779 NULL);
780 } else {
781 object_property_add_bool(obj, "attached",
782 usb_get_attached, NULL,
783 NULL);
787 static void usb_device_class_init(ObjectClass *klass, void *data)
789 DeviceClass *k = DEVICE_CLASS(klass);
790 k->bus_type = TYPE_USB_BUS;
791 k->realize = usb_qdev_realize;
792 k->unrealize = usb_qdev_unrealize;
793 k->props = usb_props;
796 static const TypeInfo usb_device_type_info = {
797 .name = TYPE_USB_DEVICE,
798 .parent = TYPE_DEVICE,
799 .instance_size = sizeof(USBDevice),
800 .instance_init = usb_device_instance_init,
801 .abstract = true,
802 .class_size = sizeof(USBDeviceClass),
803 .class_init = usb_device_class_init,
806 static void usb_register_types(void)
808 type_register_static(&usb_bus_info);
809 type_register_static(&usb_device_type_info);
812 type_init(usb_register_types)