xref: /openbmc/qemu/hw/usb/bus.c (revision d901eff3)
1 #include "hw/hw.h"
2 #include "hw/usb.h"
3 #include "hw/qdev.h"
4 #include "sysemu.h"
5 #include "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 int 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         return klass->handle_control(dev, p, request, value, index, length,
149                                          data);
150     }
151     return -ENOSYS;
152 }
153 
154 int usb_device_handle_data(USBDevice *dev, USBPacket *p)
155 {
156     USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
157     if (klass->handle_data) {
158         return klass->handle_data(dev, p);
159     }
160     return -ENOSYS;
161 }
162 
163 const char *usb_device_get_product_desc(USBDevice *dev)
164 {
165     USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
166     return klass->product_desc;
167 }
168 
169 const USBDesc *usb_device_get_usb_desc(USBDevice *dev)
170 {
171     USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
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 static int usb_qdev_init(DeviceState *qdev)
185 {
186     USBDevice *dev = USB_DEVICE(qdev);
187     int rc;
188 
189     pstrcpy(dev->product_desc, sizeof(dev->product_desc),
190             usb_device_get_product_desc(dev));
191     dev->auto_attach = 1;
192     QLIST_INIT(&dev->strings);
193     usb_ep_init(dev);
194     rc = usb_claim_port(dev);
195     if (rc != 0) {
196         return rc;
197     }
198     rc = usb_device_init(dev);
199     if (rc != 0) {
200         usb_release_port(dev);
201         return rc;
202     }
203     if (dev->auto_attach) {
204         rc = usb_device_attach(dev);
205         if (rc != 0) {
206             usb_qdev_exit(qdev);
207             return rc;
208         }
209     }
210     return 0;
211 }
212 
213 static int usb_qdev_exit(DeviceState *qdev)
214 {
215     USBDevice *dev = USB_DEVICE(qdev);
216 
217     if (dev->attached) {
218         usb_device_detach(dev);
219     }
220     usb_device_handle_destroy(dev);
221     if (dev->port) {
222         usb_release_port(dev);
223     }
224     return 0;
225 }
226 
227 typedef struct LegacyUSBFactory
228 {
229     const char *name;
230     const char *usbdevice_name;
231     USBDevice *(*usbdevice_init)(USBBus *bus, const char *params);
232 } LegacyUSBFactory;
233 
234 static GSList *legacy_usb_factory;
235 
236 void usb_legacy_register(const char *typename, const char *usbdevice_name,
237                          USBDevice *(*usbdevice_init)(USBBus *bus,
238                                                       const char *params))
239 {
240     if (usbdevice_name) {
241         LegacyUSBFactory *f = g_malloc0(sizeof(*f));
242         f->name = typename;
243         f->usbdevice_name = usbdevice_name;
244         f->usbdevice_init = usbdevice_init;
245         legacy_usb_factory = g_slist_append(legacy_usb_factory, f);
246     }
247 }
248 
249 USBDevice *usb_create(USBBus *bus, const char *name)
250 {
251     DeviceState *dev;
252 
253     dev = qdev_create(&bus->qbus, name);
254     return USB_DEVICE(dev);
255 }
256 
257 USBDevice *usb_create_simple(USBBus *bus, const char *name)
258 {
259     USBDevice *dev = usb_create(bus, name);
260     int rc;
261 
262     if (!dev) {
263         error_report("Failed to create USB device '%s'", name);
264         return NULL;
265     }
266     rc = qdev_init(&dev->qdev);
267     if (rc < 0) {
268         error_report("Failed to initialize USB device '%s'", name);
269         return NULL;
270     }
271     return dev;
272 }
273 
274 static void usb_fill_port(USBPort *port, void *opaque, int index,
275                           USBPortOps *ops, int speedmask)
276 {
277     port->opaque = opaque;
278     port->index = index;
279     port->ops = ops;
280     port->speedmask = speedmask;
281     usb_port_location(port, NULL, index + 1);
282 }
283 
284 void usb_register_port(USBBus *bus, USBPort *port, void *opaque, int index,
285                        USBPortOps *ops, int speedmask)
286 {
287     usb_fill_port(port, opaque, index, ops, speedmask);
288     QTAILQ_INSERT_TAIL(&bus->free, port, next);
289     bus->nfree++;
290 }
291 
292 int usb_register_companion(const char *masterbus, USBPort *ports[],
293                            uint32_t portcount, uint32_t firstport,
294                            void *opaque, USBPortOps *ops, int speedmask)
295 {
296     USBBus *bus;
297     int i;
298 
299     QTAILQ_FOREACH(bus, &busses, next) {
300         if (strcmp(bus->qbus.name, masterbus) == 0) {
301             break;
302         }
303     }
304 
305     if (!bus || !bus->ops->register_companion) {
306         qerror_report(QERR_INVALID_PARAMETER_VALUE, "masterbus",
307                       "an USB masterbus");
308         if (bus) {
309             error_printf_unless_qmp(
310                 "USB bus '%s' does not allow companion controllers\n",
311                 masterbus);
312         }
313         return -1;
314     }
315 
316     for (i = 0; i < portcount; i++) {
317         usb_fill_port(ports[i], opaque, i, ops, speedmask);
318     }
319 
320     return bus->ops->register_companion(bus, ports, portcount, firstport);
321 }
322 
323 void usb_port_location(USBPort *downstream, USBPort *upstream, int portnr)
324 {
325     if (upstream) {
326         snprintf(downstream->path, sizeof(downstream->path), "%s.%d",
327                  upstream->path, portnr);
328     } else {
329         snprintf(downstream->path, sizeof(downstream->path), "%d", portnr);
330     }
331 }
332 
333 void usb_unregister_port(USBBus *bus, USBPort *port)
334 {
335     if (port->dev)
336         qdev_free(&port->dev->qdev);
337     QTAILQ_REMOVE(&bus->free, port, next);
338     bus->nfree--;
339 }
340 
341 int usb_claim_port(USBDevice *dev)
342 {
343     USBBus *bus = usb_bus_from_device(dev);
344     USBPort *port;
345 
346     assert(dev->port == NULL);
347 
348     if (dev->port_path) {
349         QTAILQ_FOREACH(port, &bus->free, next) {
350             if (strcmp(port->path, dev->port_path) == 0) {
351                 break;
352             }
353         }
354         if (port == NULL) {
355             error_report("Error: usb port %s (bus %s) not found (in use?)",
356                          dev->port_path, bus->qbus.name);
357             return -1;
358         }
359     } else {
360         if (bus->nfree == 1 && strcmp(object_get_typename(OBJECT(dev)), "usb-hub") != 0) {
361             /* Create a new hub and chain it on */
362             usb_create_simple(bus, "usb-hub");
363         }
364         if (bus->nfree == 0) {
365             error_report("Error: tried to attach usb device %s to a bus "
366                          "with no free ports", dev->product_desc);
367             return -1;
368         }
369         port = QTAILQ_FIRST(&bus->free);
370     }
371     trace_usb_port_claim(bus->busnr, port->path);
372 
373     QTAILQ_REMOVE(&bus->free, port, next);
374     bus->nfree--;
375 
376     dev->port = port;
377     port->dev = dev;
378 
379     QTAILQ_INSERT_TAIL(&bus->used, port, next);
380     bus->nused++;
381     return 0;
382 }
383 
384 void usb_release_port(USBDevice *dev)
385 {
386     USBBus *bus = usb_bus_from_device(dev);
387     USBPort *port = dev->port;
388 
389     assert(port != NULL);
390     trace_usb_port_release(bus->busnr, port->path);
391 
392     QTAILQ_REMOVE(&bus->used, port, next);
393     bus->nused--;
394 
395     dev->port = NULL;
396     port->dev = NULL;
397 
398     QTAILQ_INSERT_TAIL(&bus->free, port, next);
399     bus->nfree++;
400 }
401 
402 int usb_device_attach(USBDevice *dev)
403 {
404     USBBus *bus = usb_bus_from_device(dev);
405     USBPort *port = dev->port;
406 
407     assert(port != NULL);
408     assert(!dev->attached);
409     trace_usb_port_attach(bus->busnr, port->path);
410 
411     if (!(port->speedmask & dev->speedmask)) {
412         error_report("Warning: speed mismatch trying to attach "
413                      "usb device %s to bus %s",
414                      dev->product_desc, bus->qbus.name);
415         return -1;
416     }
417 
418     dev->attached++;
419     usb_attach(port);
420 
421     return 0;
422 }
423 
424 int usb_device_detach(USBDevice *dev)
425 {
426     USBBus *bus = usb_bus_from_device(dev);
427     USBPort *port = dev->port;
428 
429     assert(port != NULL);
430     assert(dev->attached);
431     trace_usb_port_detach(bus->busnr, port->path);
432 
433     usb_detach(port);
434     dev->attached--;
435     return 0;
436 }
437 
438 int usb_device_delete_addr(int busnr, int addr)
439 {
440     USBBus *bus;
441     USBPort *port;
442     USBDevice *dev;
443 
444     bus = usb_bus_find(busnr);
445     if (!bus)
446         return -1;
447 
448     QTAILQ_FOREACH(port, &bus->used, next) {
449         if (port->dev->addr == addr)
450             break;
451     }
452     if (!port)
453         return -1;
454     dev = port->dev;
455 
456     qdev_free(&dev->qdev);
457     return 0;
458 }
459 
460 static const char *usb_speed(unsigned int speed)
461 {
462     static const char *txt[] = {
463         [ USB_SPEED_LOW  ] = "1.5",
464         [ USB_SPEED_FULL ] = "12",
465         [ USB_SPEED_HIGH ] = "480",
466         [ USB_SPEED_SUPER ] = "5000",
467     };
468     if (speed >= ARRAY_SIZE(txt))
469         return "?";
470     return txt[speed];
471 }
472 
473 static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
474 {
475     USBDevice *dev = USB_DEVICE(qdev);
476     USBBus *bus = usb_bus_from_device(dev);
477 
478     monitor_printf(mon, "%*saddr %d.%d, port %s, speed %s, name %s%s\n",
479                    indent, "", bus->busnr, dev->addr,
480                    dev->port ? dev->port->path : "-",
481                    usb_speed(dev->speed), dev->product_desc,
482                    dev->attached ? ", attached" : "");
483 }
484 
485 static char *usb_get_dev_path(DeviceState *qdev)
486 {
487     USBDevice *dev = USB_DEVICE(qdev);
488     DeviceState *hcd = qdev->parent_bus->parent;
489     char *id = NULL;
490 
491     if (dev->flags & (1 << USB_DEV_FLAG_FULL_PATH)) {
492         id = qdev_get_dev_path(hcd);
493     }
494     if (id) {
495         char *ret = g_strdup_printf("%s/%s", id, dev->port->path);
496         g_free(id);
497         return ret;
498     } else {
499         return g_strdup(dev->port->path);
500     }
501 }
502 
503 static char *usb_get_fw_dev_path(DeviceState *qdev)
504 {
505     USBDevice *dev = USB_DEVICE(qdev);
506     char *fw_path, *in;
507     ssize_t pos = 0, fw_len;
508     long nr;
509 
510     fw_len = 32 + strlen(dev->port->path) * 6;
511     fw_path = g_malloc(fw_len);
512     in = dev->port->path;
513     while (fw_len - pos > 0) {
514         nr = strtol(in, &in, 10);
515         if (in[0] == '.') {
516             /* some hub between root port and device */
517             pos += snprintf(fw_path + pos, fw_len - pos, "hub@%ld/", nr);
518             in++;
519         } else {
520             /* the device itself */
521             pos += snprintf(fw_path + pos, fw_len - pos, "%s@%ld",
522                             qdev_fw_name(qdev), nr);
523             break;
524         }
525     }
526     return fw_path;
527 }
528 
529 void usb_info(Monitor *mon)
530 {
531     USBBus *bus;
532     USBDevice *dev;
533     USBPort *port;
534 
535     if (QTAILQ_EMPTY(&busses)) {
536         monitor_printf(mon, "USB support not enabled\n");
537         return;
538     }
539 
540     QTAILQ_FOREACH(bus, &busses, next) {
541         QTAILQ_FOREACH(port, &bus->used, next) {
542             dev = port->dev;
543             if (!dev)
544                 continue;
545             monitor_printf(mon, "  Device %d.%d, Port %s, Speed %s Mb/s, Product %s\n",
546                            bus->busnr, dev->addr, port->path, usb_speed(dev->speed),
547                            dev->product_desc);
548         }
549     }
550 }
551 
552 /* handle legacy -usbdevice cmd line option */
553 USBDevice *usbdevice_create(const char *cmdline)
554 {
555     USBBus *bus = usb_bus_find(-1 /* any */);
556     LegacyUSBFactory *f = NULL;
557     GSList *i;
558     char driver[32];
559     const char *params;
560     int len;
561 
562     params = strchr(cmdline,':');
563     if (params) {
564         params++;
565         len = params - cmdline;
566         if (len > sizeof(driver))
567             len = sizeof(driver);
568         pstrcpy(driver, len, cmdline);
569     } else {
570         params = "";
571         pstrcpy(driver, sizeof(driver), cmdline);
572     }
573 
574     for (i = legacy_usb_factory; i; i = i->next) {
575         f = i->data;
576         if (strcmp(f->usbdevice_name, driver) == 0) {
577             break;
578         }
579     }
580     if (i == NULL) {
581 #if 0
582         /* no error because some drivers are not converted (yet) */
583         error_report("usbdevice %s not found", driver);
584 #endif
585         return NULL;
586     }
587 
588     if (!f->usbdevice_init) {
589         if (*params) {
590             error_report("usbdevice %s accepts no params", driver);
591             return NULL;
592         }
593         return usb_create_simple(bus, f->name);
594     }
595     return f->usbdevice_init(bus, params);
596 }
597 
598 static void usb_device_class_init(ObjectClass *klass, void *data)
599 {
600     DeviceClass *k = DEVICE_CLASS(klass);
601     k->bus_type = TYPE_USB_BUS;
602     k->init     = usb_qdev_init;
603     k->unplug   = qdev_simple_unplug_cb;
604     k->exit     = usb_qdev_exit;
605     k->props    = usb_props;
606 }
607 
608 static TypeInfo usb_device_type_info = {
609     .name = TYPE_USB_DEVICE,
610     .parent = TYPE_DEVICE,
611     .instance_size = sizeof(USBDevice),
612     .abstract = true,
613     .class_size = sizeof(USBDeviceClass),
614     .class_init = usb_device_class_init,
615 };
616 
617 static void usb_register_types(void)
618 {
619     type_register_static(&usb_bus_info);
620     type_register_static(&usb_device_type_info);
621 }
622 
623 type_init(usb_register_types)
624