Merge remote-tracking branch 'remotes/mst/tags/for_upstream' into staging
[qemu/ar7.git] / hw / usb / bus.c
blob07083349f51b5fde78ecf20f356ffd908432c78e
1 #include "qemu/osdep.h"
2 #include "hw/qdev-properties.h"
3 #include "hw/usb.h"
4 #include "qapi/error.h"
5 #include "qemu/error-report.h"
6 #include "qemu/module.h"
7 #include "sysemu/sysemu.h"
8 #include "migration/vmstate.h"
9 #include "monitor/monitor.h"
10 #include "trace.h"
11 #include "qemu/cutils.h"
13 static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent);
15 static char *usb_get_dev_path(DeviceState *dev);
16 static char *usb_get_fw_dev_path(DeviceState *qdev);
17 static void usb_qdev_unrealize(DeviceState *qdev);
19 static Property usb_props[] = {
20 DEFINE_PROP_STRING("port", USBDevice, port_path),
21 DEFINE_PROP_STRING("serial", USBDevice, serial),
22 DEFINE_PROP_BIT("msos-desc", USBDevice, flags,
23 USB_DEV_FLAG_MSOS_DESC_ENABLE, true),
24 DEFINE_PROP_STRING("pcap", USBDevice, pcap_filename),
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 return 0;
65 const VMStateDescription vmstate_usb_device = {
66 .name = "USBDevice",
67 .version_id = 1,
68 .minimum_version_id = 1,
69 .post_load = usb_device_post_load,
70 .fields = (VMStateField[]) {
71 VMSTATE_UINT8(addr, USBDevice),
72 VMSTATE_INT32(state, USBDevice),
73 VMSTATE_INT32(remote_wakeup, USBDevice),
74 VMSTATE_INT32(setup_state, USBDevice),
75 VMSTATE_INT32(setup_len, USBDevice),
76 VMSTATE_INT32(setup_index, USBDevice),
77 VMSTATE_UINT8_ARRAY(setup_buf, USBDevice, 8),
78 VMSTATE_END_OF_LIST(),
82 void usb_bus_new(USBBus *bus, size_t bus_size,
83 USBBusOps *ops, DeviceState *host)
85 qbus_create_inplace(bus, bus_size, TYPE_USB_BUS, host, NULL);
86 qbus_set_bus_hotplug_handler(BUS(bus));
87 bus->ops = ops;
88 bus->busnr = next_usb_bus++;
89 QTAILQ_INIT(&bus->free);
90 QTAILQ_INIT(&bus->used);
91 QTAILQ_INSERT_TAIL(&busses, bus, next);
94 void usb_bus_release(USBBus *bus)
96 assert(next_usb_bus > 0);
98 QTAILQ_REMOVE(&busses, bus, next);
101 USBBus *usb_bus_find(int busnr)
103 USBBus *bus;
105 if (-1 == busnr)
106 return QTAILQ_FIRST(&busses);
107 QTAILQ_FOREACH(bus, &busses, next) {
108 if (bus->busnr == busnr)
109 return bus;
111 return NULL;
114 static void usb_device_realize(USBDevice *dev, Error **errp)
116 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
118 if (klass->realize) {
119 klass->realize(dev, errp);
123 USBDevice *usb_device_find_device(USBDevice *dev, uint8_t addr)
125 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
126 if (klass->find_device) {
127 return klass->find_device(dev, addr);
129 return NULL;
132 static void usb_device_unrealize(USBDevice *dev)
134 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
136 if (klass->unrealize) {
137 klass->unrealize(dev);
141 void usb_device_cancel_packet(USBDevice *dev, USBPacket *p)
143 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
144 if (klass->cancel_packet) {
145 klass->cancel_packet(dev, p);
149 void usb_device_handle_attach(USBDevice *dev)
151 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
152 if (klass->handle_attach) {
153 klass->handle_attach(dev);
157 void usb_device_handle_reset(USBDevice *dev)
159 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
160 if (klass->handle_reset) {
161 klass->handle_reset(dev);
165 void usb_device_handle_control(USBDevice *dev, USBPacket *p, int request,
166 int value, int index, int length, uint8_t *data)
168 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
169 if (klass->handle_control) {
170 klass->handle_control(dev, p, request, value, index, length, data);
174 void usb_device_handle_data(USBDevice *dev, USBPacket *p)
176 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
177 if (klass->handle_data) {
178 klass->handle_data(dev, p);
182 const char *usb_device_get_product_desc(USBDevice *dev)
184 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
185 return klass->product_desc;
188 const USBDesc *usb_device_get_usb_desc(USBDevice *dev)
190 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
191 if (dev->usb_desc) {
192 return dev->usb_desc;
194 return klass->usb_desc;
197 void usb_device_set_interface(USBDevice *dev, int interface,
198 int alt_old, int alt_new)
200 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
201 if (klass->set_interface) {
202 klass->set_interface(dev, interface, alt_old, alt_new);
206 void usb_device_flush_ep_queue(USBDevice *dev, USBEndpoint *ep)
208 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
209 if (klass->flush_ep_queue) {
210 klass->flush_ep_queue(dev, ep);
214 void usb_device_ep_stopped(USBDevice *dev, USBEndpoint *ep)
216 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
217 if (klass->ep_stopped) {
218 klass->ep_stopped(dev, ep);
222 int usb_device_alloc_streams(USBDevice *dev, USBEndpoint **eps, int nr_eps,
223 int streams)
225 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
226 if (klass->alloc_streams) {
227 return klass->alloc_streams(dev, eps, nr_eps, streams);
229 return 0;
232 void usb_device_free_streams(USBDevice *dev, USBEndpoint **eps, int nr_eps)
234 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
235 if (klass->free_streams) {
236 klass->free_streams(dev, eps, nr_eps);
240 static void usb_qdev_realize(DeviceState *qdev, Error **errp)
242 USBDevice *dev = USB_DEVICE(qdev);
243 Error *local_err = NULL;
245 pstrcpy(dev->product_desc, sizeof(dev->product_desc),
246 usb_device_get_product_desc(dev));
247 dev->auto_attach = 1;
248 QLIST_INIT(&dev->strings);
249 usb_ep_init(dev);
251 usb_claim_port(dev, &local_err);
252 if (local_err) {
253 error_propagate(errp, local_err);
254 return;
257 usb_device_realize(dev, &local_err);
258 if (local_err) {
259 usb_release_port(dev);
260 error_propagate(errp, local_err);
261 return;
264 if (dev->auto_attach) {
265 usb_device_attach(dev, &local_err);
266 if (local_err) {
267 usb_qdev_unrealize(qdev);
268 error_propagate(errp, local_err);
269 return;
273 if (dev->pcap_filename) {
274 int fd = qemu_open_old(dev->pcap_filename, O_CREAT | O_WRONLY | O_TRUNC, 0666);
275 if (fd < 0) {
276 error_setg(errp, "open %s failed", dev->pcap_filename);
277 usb_qdev_unrealize(qdev);
278 return;
280 dev->pcap = fdopen(fd, "w");
281 usb_pcap_init(dev->pcap);
285 static void usb_qdev_unrealize(DeviceState *qdev)
287 USBDevice *dev = USB_DEVICE(qdev);
288 USBDescString *s, *next;
290 QLIST_FOREACH_SAFE(s, &dev->strings, next, next) {
291 QLIST_REMOVE(s, next);
292 g_free(s->str);
293 g_free(s);
296 if (dev->pcap) {
297 fclose(dev->pcap);
300 if (dev->attached) {
301 usb_device_detach(dev);
303 usb_device_unrealize(dev);
304 if (dev->port) {
305 usb_release_port(dev);
309 typedef struct LegacyUSBFactory
311 const char *name;
312 const char *usbdevice_name;
313 USBDevice *(*usbdevice_init)(void);
314 } LegacyUSBFactory;
316 static GSList *legacy_usb_factory;
318 void usb_legacy_register(const char *typename, const char *usbdevice_name,
319 USBDevice *(*usbdevice_init)(void))
321 if (usbdevice_name) {
322 LegacyUSBFactory *f = g_malloc0(sizeof(*f));
323 f->name = typename;
324 f->usbdevice_name = usbdevice_name;
325 f->usbdevice_init = usbdevice_init;
326 legacy_usb_factory = g_slist_append(legacy_usb_factory, f);
330 USBDevice *usb_new(const char *name)
332 return USB_DEVICE(qdev_new(name));
335 static USBDevice *usb_try_new(const char *name)
337 return USB_DEVICE(qdev_try_new(name));
340 bool usb_realize_and_unref(USBDevice *dev, USBBus *bus, Error **errp)
342 return qdev_realize_and_unref(&dev->qdev, &bus->qbus, errp);
345 USBDevice *usb_create_simple(USBBus *bus, const char *name)
347 USBDevice *dev = usb_new(name);
349 usb_realize_and_unref(dev, bus, &error_abort);
350 return dev;
353 static void usb_fill_port(USBPort *port, void *opaque, int index,
354 USBPortOps *ops, int speedmask)
356 port->opaque = opaque;
357 port->index = index;
358 port->ops = ops;
359 port->speedmask = speedmask;
360 usb_port_location(port, NULL, index + 1);
363 void usb_register_port(USBBus *bus, USBPort *port, void *opaque, int index,
364 USBPortOps *ops, int speedmask)
366 usb_fill_port(port, opaque, index, ops, speedmask);
367 QTAILQ_INSERT_TAIL(&bus->free, port, next);
368 bus->nfree++;
371 void usb_register_companion(const char *masterbus, USBPort *ports[],
372 uint32_t portcount, uint32_t firstport,
373 void *opaque, USBPortOps *ops, int speedmask,
374 Error **errp)
376 USBBus *bus;
377 int i;
379 QTAILQ_FOREACH(bus, &busses, next) {
380 if (strcmp(bus->qbus.name, masterbus) == 0) {
381 break;
385 if (!bus) {
386 error_setg(errp, "USB bus '%s' not found", masterbus);
387 return;
389 if (!bus->ops->register_companion) {
390 error_setg(errp, "Can't use USB bus '%s' as masterbus,"
391 " it doesn't support companion controllers",
392 masterbus);
393 return;
396 for (i = 0; i < portcount; i++) {
397 usb_fill_port(ports[i], opaque, i, ops, speedmask);
400 bus->ops->register_companion(bus, ports, portcount, firstport, errp);
403 void usb_port_location(USBPort *downstream, USBPort *upstream, int portnr)
405 if (upstream) {
406 int l = snprintf(downstream->path, sizeof(downstream->path), "%s.%d",
407 upstream->path, portnr);
408 /* Max string is nn.nn.nn.nn.nn, which fits in 16 bytes */
409 assert(l < sizeof(downstream->path));
410 downstream->hubcount = upstream->hubcount + 1;
411 } else {
412 snprintf(downstream->path, sizeof(downstream->path), "%d", portnr);
413 downstream->hubcount = 0;
417 void usb_unregister_port(USBBus *bus, USBPort *port)
419 if (port->dev) {
420 object_unparent(OBJECT(port->dev));
422 QTAILQ_REMOVE(&bus->free, port, next);
423 bus->nfree--;
426 void usb_claim_port(USBDevice *dev, Error **errp)
428 USBBus *bus = usb_bus_from_device(dev);
429 USBPort *port;
430 USBDevice *hub;
432 assert(dev->port == NULL);
434 if (dev->port_path) {
435 QTAILQ_FOREACH(port, &bus->free, next) {
436 if (strcmp(port->path, dev->port_path) == 0) {
437 break;
440 if (port == NULL) {
441 error_setg(errp, "usb port %s (bus %s) not found (in use?)",
442 dev->port_path, bus->qbus.name);
443 return;
445 } else {
446 if (bus->nfree == 1 && strcmp(object_get_typename(OBJECT(dev)), "usb-hub") != 0) {
447 /* Create a new hub and chain it on */
448 hub = usb_try_new("usb-hub");
449 if (hub) {
450 usb_realize_and_unref(hub, bus, NULL);
453 if (bus->nfree == 0) {
454 error_setg(errp, "tried to attach usb device %s to a bus "
455 "with no free ports", dev->product_desc);
456 return;
458 port = QTAILQ_FIRST(&bus->free);
460 trace_usb_port_claim(bus->busnr, port->path);
462 QTAILQ_REMOVE(&bus->free, port, next);
463 bus->nfree--;
465 dev->port = port;
466 port->dev = dev;
468 QTAILQ_INSERT_TAIL(&bus->used, port, next);
469 bus->nused++;
472 void usb_release_port(USBDevice *dev)
474 USBBus *bus = usb_bus_from_device(dev);
475 USBPort *port = dev->port;
477 assert(port != NULL);
478 trace_usb_port_release(bus->busnr, port->path);
480 QTAILQ_REMOVE(&bus->used, port, next);
481 bus->nused--;
483 dev->port = NULL;
484 port->dev = NULL;
486 QTAILQ_INSERT_TAIL(&bus->free, port, next);
487 bus->nfree++;
490 static void usb_mask_to_str(char *dest, size_t size,
491 unsigned int speedmask)
493 static const struct {
494 unsigned int mask;
495 const char *name;
496 } speeds[] = {
497 { .mask = USB_SPEED_MASK_FULL, .name = "full" },
498 { .mask = USB_SPEED_MASK_HIGH, .name = "high" },
499 { .mask = USB_SPEED_MASK_SUPER, .name = "super" },
501 int i, pos = 0;
503 for (i = 0; i < ARRAY_SIZE(speeds); i++) {
504 if (speeds[i].mask & speedmask) {
505 pos += snprintf(dest + pos, size - pos, "%s%s",
506 pos ? "+" : "",
507 speeds[i].name);
511 if (pos == 0) {
512 snprintf(dest, size, "unknown");
516 void usb_check_attach(USBDevice *dev, Error **errp)
518 USBBus *bus = usb_bus_from_device(dev);
519 USBPort *port = dev->port;
520 char devspeed[32], portspeed[32];
522 assert(port != NULL);
523 assert(!dev->attached);
524 usb_mask_to_str(devspeed, sizeof(devspeed), dev->speedmask);
525 usb_mask_to_str(portspeed, sizeof(portspeed), port->speedmask);
526 trace_usb_port_attach(bus->busnr, port->path,
527 devspeed, portspeed);
529 if (!(port->speedmask & dev->speedmask)) {
530 error_setg(errp, "Warning: speed mismatch trying to attach"
531 " usb device \"%s\" (%s speed)"
532 " to bus \"%s\", port \"%s\" (%s speed)",
533 dev->product_desc, devspeed,
534 bus->qbus.name, port->path, portspeed);
535 return;
539 void usb_device_attach(USBDevice *dev, Error **errp)
541 USBPort *port = dev->port;
542 Error *local_err = NULL;
544 usb_check_attach(dev, &local_err);
545 if (local_err) {
546 error_propagate(errp, local_err);
547 return;
550 dev->attached = true;
551 usb_attach(port);
554 int usb_device_detach(USBDevice *dev)
556 USBBus *bus = usb_bus_from_device(dev);
557 USBPort *port = dev->port;
559 assert(port != NULL);
560 assert(dev->attached);
561 trace_usb_port_detach(bus->busnr, port->path);
563 usb_detach(port);
564 dev->attached = false;
565 return 0;
568 static const char *usb_speed(unsigned int speed)
570 static const char *txt[] = {
571 [ USB_SPEED_LOW ] = "1.5",
572 [ USB_SPEED_FULL ] = "12",
573 [ USB_SPEED_HIGH ] = "480",
574 [ USB_SPEED_SUPER ] = "5000",
576 if (speed >= ARRAY_SIZE(txt))
577 return "?";
578 return txt[speed];
581 static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
583 USBDevice *dev = USB_DEVICE(qdev);
584 USBBus *bus = usb_bus_from_device(dev);
586 monitor_printf(mon, "%*saddr %d.%d, port %s, speed %s, name %s%s\n",
587 indent, "", bus->busnr, dev->addr,
588 dev->port ? dev->port->path : "-",
589 usb_speed(dev->speed), dev->product_desc,
590 dev->attached ? ", attached" : "");
593 static char *usb_get_dev_path(DeviceState *qdev)
595 USBDevice *dev = USB_DEVICE(qdev);
596 DeviceState *hcd = qdev->parent_bus->parent;
597 char *id = qdev_get_dev_path(hcd);
599 if (id) {
600 char *ret = g_strdup_printf("%s/%s", id, dev->port->path);
601 g_free(id);
602 return ret;
603 } else {
604 return g_strdup(dev->port->path);
608 static char *usb_get_fw_dev_path(DeviceState *qdev)
610 USBDevice *dev = USB_DEVICE(qdev);
611 char *fw_path, *in;
612 ssize_t pos = 0, fw_len;
613 long nr;
615 fw_len = 32 + strlen(dev->port->path) * 6;
616 fw_path = g_malloc(fw_len);
617 in = dev->port->path;
618 while (fw_len - pos > 0) {
619 nr = strtol(in, &in, 10);
620 if (in[0] == '.') {
621 /* some hub between root port and device */
622 pos += snprintf(fw_path + pos, fw_len - pos, "hub@%lx/", nr);
623 in++;
624 } else {
625 /* the device itself */
626 snprintf(fw_path + pos, fw_len - pos, "%s@%lx",
627 qdev_fw_name(qdev), nr);
628 break;
631 return fw_path;
634 void hmp_info_usb(Monitor *mon, const QDict *qdict)
636 USBBus *bus;
637 USBDevice *dev;
638 USBPort *port;
640 if (QTAILQ_EMPTY(&busses)) {
641 monitor_printf(mon, "USB support not enabled\n");
642 return;
645 QTAILQ_FOREACH(bus, &busses, next) {
646 QTAILQ_FOREACH(port, &bus->used, next) {
647 dev = port->dev;
648 if (!dev)
649 continue;
650 monitor_printf(mon, " Device %d.%d, Port %s, Speed %s Mb/s, "
651 "Product %s%s%s\n",
652 bus->busnr, dev->addr, port->path,
653 usb_speed(dev->speed), dev->product_desc,
654 dev->qdev.id ? ", ID: " : "",
655 dev->qdev.id ?: "");
660 /* handle legacy -usbdevice cmd line option */
661 USBDevice *usbdevice_create(const char *driver)
663 USBBus *bus = usb_bus_find(-1 /* any */);
664 LegacyUSBFactory *f = NULL;
665 Error *err = NULL;
666 GSList *i;
667 USBDevice *dev;
669 if (strchr(driver, ':')) {
670 error_report("usbdevice parameters are not supported anymore");
671 return NULL;
674 for (i = legacy_usb_factory; i; i = i->next) {
675 f = i->data;
676 if (strcmp(f->usbdevice_name, driver) == 0) {
677 break;
680 if (i == NULL) {
681 #if 0
682 /* no error because some drivers are not converted (yet) */
683 error_report("usbdevice %s not found", driver);
684 #endif
685 return NULL;
688 if (!bus) {
689 error_report("Error: no usb bus to attach usbdevice %s, "
690 "please try -machine usb=on and check that "
691 "the machine model supports USB", driver);
692 return NULL;
695 dev = f->usbdevice_init ? f->usbdevice_init() : usb_new(f->name);
696 if (!dev) {
697 error_report("Failed to create USB device '%s'", f->name);
698 return NULL;
700 if (!usb_realize_and_unref(dev, bus, &err)) {
701 error_reportf_err(err, "Failed to initialize USB device '%s': ",
702 f->name);
703 object_unparent(OBJECT(dev));
704 return NULL;
706 return dev;
709 static bool usb_get_attached(Object *obj, Error **errp)
711 USBDevice *dev = USB_DEVICE(obj);
713 return dev->attached;
716 static void usb_set_attached(Object *obj, bool value, Error **errp)
718 USBDevice *dev = USB_DEVICE(obj);
720 if (dev->attached == value) {
721 return;
724 if (value) {
725 usb_device_attach(dev, errp);
726 } else {
727 usb_device_detach(dev);
731 static void usb_device_instance_init(Object *obj)
733 USBDevice *dev = USB_DEVICE(obj);
734 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
736 if (klass->attached_settable) {
737 object_property_add_bool(obj, "attached",
738 usb_get_attached, usb_set_attached);
739 } else {
740 object_property_add_bool(obj, "attached",
741 usb_get_attached, NULL);
745 static void usb_device_class_init(ObjectClass *klass, void *data)
747 DeviceClass *k = DEVICE_CLASS(klass);
748 k->bus_type = TYPE_USB_BUS;
749 k->realize = usb_qdev_realize;
750 k->unrealize = usb_qdev_unrealize;
751 device_class_set_props(k, usb_props);
754 static const TypeInfo usb_device_type_info = {
755 .name = TYPE_USB_DEVICE,
756 .parent = TYPE_DEVICE,
757 .instance_size = sizeof(USBDevice),
758 .instance_init = usb_device_instance_init,
759 .abstract = true,
760 .class_size = sizeof(USBDeviceClass),
761 .class_init = usb_device_class_init,
764 static void usb_register_types(void)
766 type_register_static(&usb_bus_info);
767 type_register_static(&usb_device_type_info);
770 type_init(usb_register_types)