hw/usb/bus.c: PCAP adding 0xA in Windows version
[qemu/ar7.git] / hw / usb / bus.c
blobbfab2807d75073b982252ab778dea9569ea7a1a1
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 "sysemu/sysemu.h"
10 #include "migration/vmstate.h"
11 #include "monitor/monitor.h"
12 #include "trace.h"
13 #include "qemu/cutils.h"
15 static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent);
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);
21 static 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 DEFINE_PROP_END_OF_LIST()
30 static void usb_bus_class_init(ObjectClass *klass, void *data)
32 BusClass *k = BUS_CLASS(klass);
33 HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
35 k->print_dev = usb_bus_dev_print;
36 k->get_dev_path = usb_get_dev_path;
37 k->get_fw_dev_path = usb_get_fw_dev_path;
38 hc->unplug = qdev_simple_device_unplug_cb;
41 static const TypeInfo usb_bus_info = {
42 .name = TYPE_USB_BUS,
43 .parent = TYPE_BUS,
44 .instance_size = sizeof(USBBus),
45 .class_init = usb_bus_class_init,
46 .interfaces = (InterfaceInfo[]) {
47 { TYPE_HOTPLUG_HANDLER },
48 { }
52 static int next_usb_bus = 0;
53 static QTAILQ_HEAD(, USBBus) busses = QTAILQ_HEAD_INITIALIZER(busses);
55 static int usb_device_post_load(void *opaque, int version_id)
57 USBDevice *dev = opaque;
59 if (dev->state == USB_STATE_NOTATTACHED) {
60 dev->attached = false;
61 } else {
62 dev->attached = true;
64 return 0;
67 const VMStateDescription vmstate_usb_device = {
68 .name = "USBDevice",
69 .version_id = 1,
70 .minimum_version_id = 1,
71 .post_load = usb_device_post_load,
72 .fields = (const VMStateField[]) {
73 VMSTATE_UINT8(addr, USBDevice),
74 VMSTATE_INT32(state, USBDevice),
75 VMSTATE_INT32(remote_wakeup, USBDevice),
76 VMSTATE_INT32(setup_state, USBDevice),
77 VMSTATE_INT32(setup_len, USBDevice),
78 VMSTATE_INT32(setup_index, USBDevice),
79 VMSTATE_UINT8_ARRAY(setup_buf, USBDevice, 8),
80 VMSTATE_END_OF_LIST(),
84 void usb_bus_new(USBBus *bus, size_t bus_size,
85 USBBusOps *ops, DeviceState *host)
87 qbus_init(bus, bus_size, TYPE_USB_BUS, host, NULL);
88 qbus_set_bus_hotplug_handler(BUS(bus));
89 bus->ops = ops;
90 bus->busnr = next_usb_bus++;
91 QTAILQ_INIT(&bus->free);
92 QTAILQ_INIT(&bus->used);
93 QTAILQ_INSERT_TAIL(&busses, bus, next);
96 void usb_bus_release(USBBus *bus)
98 assert(next_usb_bus > 0);
100 QTAILQ_REMOVE(&busses, bus, next);
103 static void usb_device_realize(USBDevice *dev, Error **errp)
105 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
107 if (klass->realize) {
108 klass->realize(dev, errp);
112 USBDevice *usb_device_find_device(USBDevice *dev, uint8_t addr)
114 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
115 if (klass->find_device) {
116 return klass->find_device(dev, addr);
118 return NULL;
121 static void usb_device_unrealize(USBDevice *dev)
123 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
125 if (klass->unrealize) {
126 klass->unrealize(dev);
130 void usb_device_cancel_packet(USBDevice *dev, USBPacket *p)
132 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
133 if (klass->cancel_packet) {
134 klass->cancel_packet(dev, p);
138 void usb_device_handle_attach(USBDevice *dev)
140 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
141 if (klass->handle_attach) {
142 klass->handle_attach(dev);
146 void usb_device_handle_reset(USBDevice *dev)
148 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
149 if (klass->handle_reset) {
150 klass->handle_reset(dev);
154 void usb_device_handle_control(USBDevice *dev, USBPacket *p, int request,
155 int value, int index, int length, uint8_t *data)
157 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
158 if (klass->handle_control) {
159 klass->handle_control(dev, p, request, value, index, length, data);
163 void usb_device_handle_data(USBDevice *dev, USBPacket *p)
165 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
166 if (klass->handle_data) {
167 klass->handle_data(dev, p);
171 const char *usb_device_get_product_desc(USBDevice *dev)
173 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
174 return klass->product_desc;
177 const USBDesc *usb_device_get_usb_desc(USBDevice *dev)
179 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
180 if (dev->usb_desc) {
181 return dev->usb_desc;
183 return klass->usb_desc;
186 void usb_device_set_interface(USBDevice *dev, int interface,
187 int alt_old, int alt_new)
189 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
190 if (klass->set_interface) {
191 klass->set_interface(dev, interface, alt_old, alt_new);
195 void usb_device_flush_ep_queue(USBDevice *dev, USBEndpoint *ep)
197 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
198 if (klass->flush_ep_queue) {
199 klass->flush_ep_queue(dev, ep);
203 void usb_device_ep_stopped(USBDevice *dev, USBEndpoint *ep)
205 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
206 if (klass->ep_stopped) {
207 klass->ep_stopped(dev, ep);
211 int usb_device_alloc_streams(USBDevice *dev, USBEndpoint **eps, int nr_eps,
212 int streams)
214 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
215 if (klass->alloc_streams) {
216 return klass->alloc_streams(dev, eps, nr_eps, streams);
218 return 0;
221 void usb_device_free_streams(USBDevice *dev, USBEndpoint **eps, int nr_eps)
223 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
224 if (klass->free_streams) {
225 klass->free_streams(dev, eps, nr_eps);
229 static void usb_qdev_realize(DeviceState *qdev, Error **errp)
231 USBDevice *dev = USB_DEVICE(qdev);
232 Error *local_err = NULL;
234 pstrcpy(dev->product_desc, sizeof(dev->product_desc),
235 usb_device_get_product_desc(dev));
236 dev->auto_attach = 1;
237 QLIST_INIT(&dev->strings);
238 usb_ep_init(dev);
240 usb_claim_port(dev, &local_err);
241 if (local_err) {
242 error_propagate(errp, local_err);
243 return;
246 usb_device_realize(dev, &local_err);
247 if (local_err) {
248 usb_release_port(dev);
249 error_propagate(errp, local_err);
250 return;
253 if (dev->auto_attach) {
254 usb_device_attach(dev, &local_err);
255 if (local_err) {
256 usb_qdev_unrealize(qdev);
257 error_propagate(errp, local_err);
258 return;
262 if (dev->pcap_filename) {
263 int fd = qemu_open_old(dev->pcap_filename,
264 O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, 0666);
265 if (fd < 0) {
266 error_setg(errp, "open %s failed", dev->pcap_filename);
267 usb_qdev_unrealize(qdev);
268 return;
270 dev->pcap = fdopen(fd, "wb");
271 usb_pcap_init(dev->pcap);
275 static void usb_qdev_unrealize(DeviceState *qdev)
277 USBDevice *dev = USB_DEVICE(qdev);
278 USBDescString *s, *next;
280 QLIST_FOREACH_SAFE(s, &dev->strings, next, next) {
281 QLIST_REMOVE(s, next);
282 g_free(s->str);
283 g_free(s);
286 if (dev->pcap) {
287 fclose(dev->pcap);
290 if (dev->attached) {
291 usb_device_detach(dev);
293 usb_device_unrealize(dev);
294 if (dev->port) {
295 usb_release_port(dev);
299 typedef struct LegacyUSBFactory
301 const char *name;
302 const char *usbdevice_name;
303 USBDevice *(*usbdevice_init)(void);
304 } LegacyUSBFactory;
306 static GSList *legacy_usb_factory;
308 void usb_legacy_register(const char *typename, const char *usbdevice_name,
309 USBDevice *(*usbdevice_init)(void))
311 if (usbdevice_name) {
312 LegacyUSBFactory *f = g_malloc0(sizeof(*f));
313 f->name = typename;
314 f->usbdevice_name = usbdevice_name;
315 f->usbdevice_init = usbdevice_init;
316 legacy_usb_factory = g_slist_append(legacy_usb_factory, f);
320 static void usb_fill_port(USBPort *port, void *opaque, int index,
321 USBPortOps *ops, int speedmask)
323 port->opaque = opaque;
324 port->index = index;
325 port->ops = ops;
326 port->speedmask = speedmask;
327 usb_port_location(port, NULL, index + 1);
330 void usb_register_port(USBBus *bus, USBPort *port, void *opaque, int index,
331 USBPortOps *ops, int speedmask)
333 usb_fill_port(port, opaque, index, ops, speedmask);
334 QTAILQ_INSERT_TAIL(&bus->free, port, next);
335 bus->nfree++;
338 void usb_register_companion(const char *masterbus, USBPort *ports[],
339 uint32_t portcount, uint32_t firstport,
340 void *opaque, USBPortOps *ops, int speedmask,
341 Error **errp)
343 USBBus *bus;
344 int i;
346 QTAILQ_FOREACH(bus, &busses, next) {
347 if (strcmp(bus->qbus.name, masterbus) == 0) {
348 break;
352 if (!bus) {
353 error_setg(errp, "USB bus '%s' not found", masterbus);
354 return;
356 if (!bus->ops->register_companion) {
357 error_setg(errp, "Can't use USB bus '%s' as masterbus,"
358 " it doesn't support companion controllers",
359 masterbus);
360 return;
363 for (i = 0; i < portcount; i++) {
364 usb_fill_port(ports[i], opaque, i, ops, speedmask);
367 bus->ops->register_companion(bus, ports, portcount, firstport, errp);
370 void usb_port_location(USBPort *downstream, USBPort *upstream, int portnr)
372 if (upstream) {
373 int l = snprintf(downstream->path, sizeof(downstream->path), "%s.%d",
374 upstream->path, portnr);
375 /* Max string is nn.nn.nn.nn.nn, which fits in 16 bytes */
376 assert(l < sizeof(downstream->path));
377 downstream->hubcount = upstream->hubcount + 1;
378 } else {
379 snprintf(downstream->path, sizeof(downstream->path), "%d", portnr);
380 downstream->hubcount = 0;
384 void usb_unregister_port(USBBus *bus, USBPort *port)
386 if (port->dev) {
387 object_unparent(OBJECT(port->dev));
389 QTAILQ_REMOVE(&bus->free, port, next);
390 bus->nfree--;
393 void usb_claim_port(USBDevice *dev, Error **errp)
395 USBBus *bus = usb_bus_from_device(dev);
396 USBPort *port;
397 USBDevice *hub;
399 assert(dev->port == NULL);
401 if (dev->port_path) {
402 QTAILQ_FOREACH(port, &bus->free, next) {
403 if (strcmp(port->path, dev->port_path) == 0) {
404 break;
407 if (port == NULL) {
408 error_setg(errp, "usb port %s (bus %s) not found (in use?)",
409 dev->port_path, bus->qbus.name);
410 return;
412 } else {
413 if (bus->nfree == 1 && strcmp(object_get_typename(OBJECT(dev)), "usb-hub") != 0) {
414 /* Create a new hub and chain it on */
415 hub = usb_try_new("usb-hub");
416 if (hub) {
417 usb_realize_and_unref(hub, bus, NULL);
420 if (bus->nfree == 0) {
421 error_setg(errp, "tried to attach usb device %s to a bus "
422 "with no free ports", dev->product_desc);
423 return;
425 port = QTAILQ_FIRST(&bus->free);
427 trace_usb_port_claim(bus->busnr, port->path);
429 QTAILQ_REMOVE(&bus->free, port, next);
430 bus->nfree--;
432 dev->port = port;
433 port->dev = dev;
435 QTAILQ_INSERT_TAIL(&bus->used, port, next);
436 bus->nused++;
439 void usb_release_port(USBDevice *dev)
441 USBBus *bus = usb_bus_from_device(dev);
442 USBPort *port = dev->port;
444 assert(port != NULL);
445 trace_usb_port_release(bus->busnr, port->path);
447 QTAILQ_REMOVE(&bus->used, port, next);
448 bus->nused--;
450 dev->port = NULL;
451 port->dev = NULL;
453 QTAILQ_INSERT_TAIL(&bus->free, port, next);
454 bus->nfree++;
457 static void usb_mask_to_str(char *dest, size_t size,
458 unsigned int speedmask)
460 static const struct {
461 unsigned int mask;
462 const char *name;
463 } speeds[] = {
464 { .mask = USB_SPEED_MASK_FULL, .name = "full" },
465 { .mask = USB_SPEED_MASK_HIGH, .name = "high" },
466 { .mask = USB_SPEED_MASK_SUPER, .name = "super" },
468 int i, pos = 0;
470 for (i = 0; i < ARRAY_SIZE(speeds); i++) {
471 if (speeds[i].mask & speedmask) {
472 pos += snprintf(dest + pos, size - pos, "%s%s",
473 pos ? "+" : "",
474 speeds[i].name);
478 if (pos == 0) {
479 snprintf(dest, size, "unknown");
483 void usb_check_attach(USBDevice *dev, Error **errp)
485 USBBus *bus = usb_bus_from_device(dev);
486 USBPort *port = dev->port;
487 char devspeed[32], portspeed[32];
489 assert(port != NULL);
490 assert(!dev->attached);
491 usb_mask_to_str(devspeed, sizeof(devspeed), dev->speedmask);
492 usb_mask_to_str(portspeed, sizeof(portspeed), port->speedmask);
493 trace_usb_port_attach(bus->busnr, port->path,
494 devspeed, portspeed);
496 if (!(port->speedmask & dev->speedmask)) {
497 error_setg(errp, "Warning: speed mismatch trying to attach"
498 " usb device \"%s\" (%s speed)"
499 " to bus \"%s\", port \"%s\" (%s speed)",
500 dev->product_desc, devspeed,
501 bus->qbus.name, port->path, portspeed);
502 return;
506 void usb_device_attach(USBDevice *dev, Error **errp)
508 USBPort *port = dev->port;
509 Error *local_err = NULL;
511 usb_check_attach(dev, &local_err);
512 if (local_err) {
513 error_propagate(errp, local_err);
514 return;
517 dev->attached = true;
518 usb_attach(port);
521 int usb_device_detach(USBDevice *dev)
523 USBBus *bus = usb_bus_from_device(dev);
524 USBPort *port = dev->port;
526 assert(port != NULL);
527 assert(dev->attached);
528 trace_usb_port_detach(bus->busnr, port->path);
530 usb_detach(port);
531 dev->attached = false;
532 return 0;
535 static const char *usb_speed(unsigned int speed)
537 static const char *txt[] = {
538 [ USB_SPEED_LOW ] = "1.5",
539 [ USB_SPEED_FULL ] = "12",
540 [ USB_SPEED_HIGH ] = "480",
541 [ USB_SPEED_SUPER ] = "5000",
543 if (speed >= ARRAY_SIZE(txt))
544 return "?";
545 return txt[speed];
548 static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
550 USBDevice *dev = USB_DEVICE(qdev);
551 USBBus *bus = usb_bus_from_device(dev);
553 monitor_printf(mon, "%*saddr %d.%d, port %s, speed %s, name %s%s\n",
554 indent, "", bus->busnr, dev->addr,
555 dev->port ? dev->port->path : "-",
556 usb_speed(dev->speed), dev->product_desc,
557 dev->attached ? ", attached" : "");
560 static char *usb_get_dev_path(DeviceState *qdev)
562 USBDevice *dev = USB_DEVICE(qdev);
563 DeviceState *hcd = qdev->parent_bus->parent;
564 char *id = qdev_get_dev_path(hcd);
566 if (id) {
567 char *ret = g_strdup_printf("%s/%s", id, dev->port->path);
568 g_free(id);
569 return ret;
570 } else {
571 return g_strdup(dev->port->path);
575 static char *usb_get_fw_dev_path(DeviceState *qdev)
577 USBDevice *dev = USB_DEVICE(qdev);
578 char *fw_path, *in;
579 ssize_t pos = 0, fw_len;
580 long nr;
582 fw_len = 32 + strlen(dev->port->path) * 6;
583 fw_path = g_malloc(fw_len);
584 in = dev->port->path;
585 while (fw_len - pos > 0) {
586 nr = strtol(in, &in, 10);
587 if (in[0] == '.') {
588 /* some hub between root port and device */
589 pos += snprintf(fw_path + pos, fw_len - pos, "hub@%lx/", nr);
590 in++;
591 } else {
592 /* the device itself */
593 snprintf(fw_path + pos, fw_len - pos, "%s@%lx",
594 qdev_fw_name(qdev), nr);
595 break;
598 return fw_path;
601 HumanReadableText *qmp_x_query_usb(Error **errp)
603 g_autoptr(GString) buf = g_string_new("");
604 USBBus *bus;
605 USBDevice *dev;
606 USBPort *port;
608 if (QTAILQ_EMPTY(&busses)) {
609 error_setg(errp, "USB support not enabled");
610 return NULL;
613 QTAILQ_FOREACH(bus, &busses, next) {
614 QTAILQ_FOREACH(port, &bus->used, next) {
615 dev = port->dev;
616 if (!dev)
617 continue;
618 g_string_append_printf(buf,
619 " Device %d.%d, Port %s, Speed %s Mb/s, "
620 "Product %s%s%s\n",
621 bus->busnr, dev->addr, port->path,
622 usb_speed(dev->speed), dev->product_desc,
623 dev->qdev.id ? ", ID: " : "",
624 dev->qdev.id ?: "");
628 return human_readable_text_from_str(buf);
631 /* handle legacy -usbdevice cmd line option */
632 USBDevice *usbdevice_create(const char *driver)
634 USBBus *bus = QTAILQ_FIRST(&busses);
635 LegacyUSBFactory *f = NULL;
636 Error *err = NULL;
637 GSList *i;
638 USBDevice *dev;
640 if (strchr(driver, ':')) {
641 error_report("usbdevice parameters are not supported anymore");
642 return NULL;
645 for (i = legacy_usb_factory; i; i = i->next) {
646 f = i->data;
647 if (strcmp(f->usbdevice_name, driver) == 0) {
648 break;
651 if (i == NULL) {
652 #if 0
653 /* no error because some drivers are not converted (yet) */
654 error_report("usbdevice %s not found", driver);
655 #endif
656 return NULL;
659 if (!bus) {
660 error_report("Error: no usb bus to attach usbdevice %s, "
661 "please try -machine usb=on and check that "
662 "the machine model supports USB", driver);
663 return NULL;
666 dev = f->usbdevice_init ? f->usbdevice_init() : usb_new(f->name);
667 if (!dev) {
668 error_report("Failed to create USB device '%s'", f->name);
669 return NULL;
671 if (!usb_realize_and_unref(dev, bus, &err)) {
672 error_reportf_err(err, "Failed to initialize USB device '%s': ",
673 f->name);
674 object_unparent(OBJECT(dev));
675 return NULL;
677 return dev;
680 static bool usb_get_attached(Object *obj, Error **errp)
682 USBDevice *dev = USB_DEVICE(obj);
684 return dev->attached;
687 static void usb_set_attached(Object *obj, bool value, Error **errp)
689 USBDevice *dev = USB_DEVICE(obj);
691 if (dev->attached == value) {
692 return;
695 if (value) {
696 usb_device_attach(dev, errp);
697 } else {
698 usb_device_detach(dev);
702 static void usb_device_instance_init(Object *obj)
704 USBDevice *dev = USB_DEVICE(obj);
705 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
707 if (klass->attached_settable) {
708 object_property_add_bool(obj, "attached",
709 usb_get_attached, usb_set_attached);
710 } else {
711 object_property_add_bool(obj, "attached",
712 usb_get_attached, NULL);
716 static void usb_device_class_init(ObjectClass *klass, void *data)
718 DeviceClass *k = DEVICE_CLASS(klass);
719 k->bus_type = TYPE_USB_BUS;
720 k->realize = usb_qdev_realize;
721 k->unrealize = usb_qdev_unrealize;
722 device_class_set_props(k, usb_props);
725 static const TypeInfo usb_device_type_info = {
726 .name = TYPE_USB_DEVICE,
727 .parent = TYPE_DEVICE,
728 .instance_size = sizeof(USBDevice),
729 .instance_init = usb_device_instance_init,
730 .abstract = true,
731 .class_size = sizeof(USBDeviceClass),
732 .class_init = usb_device_class_init,
735 static void usb_register_types(void)
737 type_register_static(&usb_bus_info);
738 type_register_static(&usb_device_type_info);
741 type_init(usb_register_types)