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