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