xref: /openbmc/qemu/hw/usb/bus.c (revision 5e02a4fd)
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"
14 
15 static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent);
16 
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);
20 
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()
28 };
29 
usb_bus_class_init(ObjectClass * klass,void * data)30 static void usb_bus_class_init(ObjectClass *klass, void *data)
31 {
32     BusClass *k = BUS_CLASS(klass);
33     HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
34 
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;
39 }
40 
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         { }
49     }
50 };
51 
52 static int next_usb_bus = 0;
53 static QTAILQ_HEAD(, USBBus) busses = QTAILQ_HEAD_INITIALIZER(busses);
54 
usb_device_post_load(void * opaque,int version_id)55 static int usb_device_post_load(void *opaque, int version_id)
56 {
57     USBDevice *dev = opaque;
58 
59     if (dev->state == USB_STATE_NOTATTACHED) {
60         dev->attached = false;
61     } else {
62         dev->attached = true;
63     }
64     return 0;
65 }
66 
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(),
81     }
82 };
83 
usb_bus_new(USBBus * bus,size_t bus_size,USBBusOps * ops,DeviceState * host)84 void usb_bus_new(USBBus *bus, size_t bus_size,
85                  USBBusOps *ops, DeviceState *host)
86 {
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);
94 }
95 
usb_bus_release(USBBus * bus)96 void usb_bus_release(USBBus *bus)
97 {
98     assert(next_usb_bus > 0);
99 
100     QTAILQ_REMOVE(&busses, bus, next);
101 }
102 
usb_device_realize(USBDevice * dev,Error ** errp)103 static void usb_device_realize(USBDevice *dev, Error **errp)
104 {
105     USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
106 
107     if (klass->realize) {
108         klass->realize(dev, errp);
109     }
110 }
111 
usb_device_find_device(USBDevice * dev,uint8_t addr)112 USBDevice *usb_device_find_device(USBDevice *dev, uint8_t addr)
113 {
114     USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
115     if (klass->find_device) {
116         return klass->find_device(dev, addr);
117     }
118     return NULL;
119 }
120 
usb_device_unrealize(USBDevice * dev)121 static void usb_device_unrealize(USBDevice *dev)
122 {
123     USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
124 
125     if (klass->unrealize) {
126         klass->unrealize(dev);
127     }
128 }
129 
usb_device_cancel_packet(USBDevice * dev,USBPacket * p)130 void usb_device_cancel_packet(USBDevice *dev, USBPacket *p)
131 {
132     USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
133     if (klass->cancel_packet) {
134         klass->cancel_packet(dev, p);
135     }
136 }
137 
usb_device_handle_attach(USBDevice * dev)138 void usb_device_handle_attach(USBDevice *dev)
139 {
140     USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
141     if (klass->handle_attach) {
142         klass->handle_attach(dev);
143     }
144 }
145 
usb_device_handle_reset(USBDevice * dev)146 void usb_device_handle_reset(USBDevice *dev)
147 {
148     USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
149     if (klass->handle_reset) {
150         klass->handle_reset(dev);
151     }
152 }
153 
usb_device_handle_control(USBDevice * dev,USBPacket * p,int request,int value,int index,int length,uint8_t * data)154 void usb_device_handle_control(USBDevice *dev, USBPacket *p, int request,
155                                int value, int index, int length, uint8_t *data)
156 {
157     USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
158     if (klass->handle_control) {
159         klass->handle_control(dev, p, request, value, index, length, data);
160     }
161 }
162 
usb_device_handle_data(USBDevice * dev,USBPacket * p)163 void usb_device_handle_data(USBDevice *dev, USBPacket *p)
164 {
165     USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
166     if (klass->handle_data) {
167         klass->handle_data(dev, p);
168     }
169 }
170 
usb_device_get_product_desc(USBDevice * dev)171 const char *usb_device_get_product_desc(USBDevice *dev)
172 {
173     USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
174     return klass->product_desc;
175 }
176 
usb_device_get_usb_desc(USBDevice * dev)177 const USBDesc *usb_device_get_usb_desc(USBDevice *dev)
178 {
179     USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
180     if (dev->usb_desc) {
181         return dev->usb_desc;
182     }
183     return klass->usb_desc;
184 }
185 
usb_device_set_interface(USBDevice * dev,int interface,int alt_old,int alt_new)186 void usb_device_set_interface(USBDevice *dev, int interface,
187                               int alt_old, int alt_new)
188 {
189     USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
190     if (klass->set_interface) {
191         klass->set_interface(dev, interface, alt_old, alt_new);
192     }
193 }
194 
usb_device_flush_ep_queue(USBDevice * dev,USBEndpoint * ep)195 void usb_device_flush_ep_queue(USBDevice *dev, USBEndpoint *ep)
196 {
197     USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
198     if (klass->flush_ep_queue) {
199         klass->flush_ep_queue(dev, ep);
200     }
201 }
202 
usb_device_ep_stopped(USBDevice * dev,USBEndpoint * ep)203 void usb_device_ep_stopped(USBDevice *dev, USBEndpoint *ep)
204 {
205     USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
206     if (klass->ep_stopped) {
207         klass->ep_stopped(dev, ep);
208     }
209 }
210 
usb_device_alloc_streams(USBDevice * dev,USBEndpoint ** eps,int nr_eps,int streams)211 int usb_device_alloc_streams(USBDevice *dev, USBEndpoint **eps, int nr_eps,
212                              int streams)
213 {
214     USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
215     if (klass->alloc_streams) {
216         return klass->alloc_streams(dev, eps, nr_eps, streams);
217     }
218     return 0;
219 }
220 
usb_device_free_streams(USBDevice * dev,USBEndpoint ** eps,int nr_eps)221 void usb_device_free_streams(USBDevice *dev, USBEndpoint **eps, int nr_eps)
222 {
223     USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
224     if (klass->free_streams) {
225         klass->free_streams(dev, eps, nr_eps);
226     }
227 }
228 
usb_qdev_realize(DeviceState * qdev,Error ** errp)229 static void usb_qdev_realize(DeviceState *qdev, Error **errp)
230 {
231     USBDevice *dev = USB_DEVICE(qdev);
232     Error *local_err = NULL;
233 
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);
239 
240     usb_claim_port(dev, &local_err);
241     if (local_err) {
242         error_propagate(errp, local_err);
243         return;
244     }
245 
246     usb_device_realize(dev, &local_err);
247     if (local_err) {
248         usb_release_port(dev);
249         error_propagate(errp, local_err);
250         return;
251     }
252 
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;
259         }
260     }
261 
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;
269         }
270         dev->pcap = fdopen(fd, "wb");
271         usb_pcap_init(dev->pcap);
272     }
273 }
274 
usb_qdev_unrealize(DeviceState * qdev)275 static void usb_qdev_unrealize(DeviceState *qdev)
276 {
277     USBDevice *dev = USB_DEVICE(qdev);
278     USBDescString *s, *next;
279 
280     QLIST_FOREACH_SAFE(s, &dev->strings, next, next) {
281         QLIST_REMOVE(s, next);
282         g_free(s->str);
283         g_free(s);
284     }
285 
286     if (dev->pcap) {
287         fclose(dev->pcap);
288     }
289 
290     if (dev->attached) {
291         usb_device_detach(dev);
292     }
293     usb_device_unrealize(dev);
294     if (dev->port) {
295         usb_release_port(dev);
296     }
297 }
298 
299 typedef struct LegacyUSBFactory
300 {
301     const char *name;
302     const char *usbdevice_name;
303     USBDevice *(*usbdevice_init)(void);
304 } LegacyUSBFactory;
305 
306 static GSList *legacy_usb_factory;
307 
usb_legacy_register(const char * typename,const char * usbdevice_name,USBDevice * (* usbdevice_init)(void))308 void usb_legacy_register(const char *typename, const char *usbdevice_name,
309                          USBDevice *(*usbdevice_init)(void))
310 {
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);
317     }
318 }
319 
usb_fill_port(USBPort * port,void * opaque,int index,USBPortOps * ops,int speedmask)320 static void usb_fill_port(USBPort *port, void *opaque, int index,
321                           USBPortOps *ops, int speedmask)
322 {
323     port->opaque = opaque;
324     port->index = index;
325     port->ops = ops;
326     port->speedmask = speedmask;
327     usb_port_location(port, NULL, index + 1);
328 }
329 
usb_register_port(USBBus * bus,USBPort * port,void * opaque,int index,USBPortOps * ops,int speedmask)330 void usb_register_port(USBBus *bus, USBPort *port, void *opaque, int index,
331                        USBPortOps *ops, int speedmask)
332 {
333     usb_fill_port(port, opaque, index, ops, speedmask);
334     QTAILQ_INSERT_TAIL(&bus->free, port, next);
335     bus->nfree++;
336 }
337 
usb_register_companion(const char * masterbus,USBPort * ports[],uint32_t portcount,uint32_t firstport,void * opaque,USBPortOps * ops,int speedmask,Error ** errp)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)
342 {
343     USBBus *bus;
344     int i;
345 
346     QTAILQ_FOREACH(bus, &busses, next) {
347         if (strcmp(bus->qbus.name, masterbus) == 0) {
348             break;
349         }
350     }
351 
352     if (!bus) {
353         error_setg(errp, "USB bus '%s' not found", masterbus);
354         return;
355     }
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;
361     }
362 
363     for (i = 0; i < portcount; i++) {
364         usb_fill_port(ports[i], opaque, i, ops, speedmask);
365     }
366 
367     bus->ops->register_companion(bus, ports, portcount, firstport, errp);
368 }
369 
usb_port_location(USBPort * downstream,USBPort * upstream,int portnr)370 void usb_port_location(USBPort *downstream, USBPort *upstream, int portnr)
371 {
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;
381     }
382 }
383 
usb_unregister_port(USBBus * bus,USBPort * port)384 void usb_unregister_port(USBBus *bus, USBPort *port)
385 {
386     if (port->dev) {
387         object_unparent(OBJECT(port->dev));
388     }
389     QTAILQ_REMOVE(&bus->free, port, next);
390     bus->nfree--;
391 }
392 
usb_claim_port(USBDevice * dev,Error ** errp)393 void usb_claim_port(USBDevice *dev, Error **errp)
394 {
395     USBBus *bus = usb_bus_from_device(dev);
396     USBPort *port;
397     USBDevice *hub;
398 
399     assert(dev->port == NULL);
400 
401     if (dev->port_path) {
402         QTAILQ_FOREACH(port, &bus->free, next) {
403             if (strcmp(port->path, dev->port_path) == 0) {
404                 break;
405             }
406         }
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;
411         }
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);
418             }
419         }
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;
424         }
425         port = QTAILQ_FIRST(&bus->free);
426     }
427     trace_usb_port_claim(bus->busnr, port->path);
428 
429     QTAILQ_REMOVE(&bus->free, port, next);
430     bus->nfree--;
431 
432     dev->port = port;
433     port->dev = dev;
434 
435     QTAILQ_INSERT_TAIL(&bus->used, port, next);
436     bus->nused++;
437 }
438 
usb_release_port(USBDevice * dev)439 void usb_release_port(USBDevice *dev)
440 {
441     USBBus *bus = usb_bus_from_device(dev);
442     USBPort *port = dev->port;
443 
444     assert(port != NULL);
445     trace_usb_port_release(bus->busnr, port->path);
446 
447     QTAILQ_REMOVE(&bus->used, port, next);
448     bus->nused--;
449 
450     dev->port = NULL;
451     port->dev = NULL;
452 
453     QTAILQ_INSERT_TAIL(&bus->free, port, next);
454     bus->nfree++;
455 }
456 
usb_mask_to_str(char * dest,size_t size,unsigned int speedmask)457 static void usb_mask_to_str(char *dest, size_t size,
458                             unsigned int speedmask)
459 {
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" },
467     };
468     int i, pos = 0;
469 
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);
475         }
476     }
477 
478     if (pos == 0) {
479         snprintf(dest, size, "unknown");
480     }
481 }
482 
usb_check_attach(USBDevice * dev,Error ** errp)483 void usb_check_attach(USBDevice *dev, Error **errp)
484 {
485     USBBus *bus = usb_bus_from_device(dev);
486     USBPort *port = dev->port;
487     char devspeed[32], portspeed[32];
488 
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);
495 
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;
503     }
504 }
505 
usb_device_attach(USBDevice * dev,Error ** errp)506 void usb_device_attach(USBDevice *dev, Error **errp)
507 {
508     USBPort *port = dev->port;
509     Error *local_err = NULL;
510 
511     usb_check_attach(dev, &local_err);
512     if (local_err) {
513         error_propagate(errp, local_err);
514         return;
515     }
516 
517     dev->attached = true;
518     usb_attach(port);
519 }
520 
usb_device_detach(USBDevice * dev)521 int usb_device_detach(USBDevice *dev)
522 {
523     USBBus *bus = usb_bus_from_device(dev);
524     USBPort *port = dev->port;
525 
526     assert(port != NULL);
527     assert(dev->attached);
528     trace_usb_port_detach(bus->busnr, port->path);
529 
530     usb_detach(port);
531     dev->attached = false;
532     return 0;
533 }
534 
usb_speed(unsigned int speed)535 static const char *usb_speed(unsigned int speed)
536 {
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",
542     };
543     if (speed >= ARRAY_SIZE(txt))
544         return "?";
545     return txt[speed];
546 }
547 
usb_bus_dev_print(Monitor * mon,DeviceState * qdev,int indent)548 static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
549 {
550     USBDevice *dev = USB_DEVICE(qdev);
551     USBBus *bus = usb_bus_from_device(dev);
552 
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" : "");
558 }
559 
usb_get_dev_path(DeviceState * qdev)560 static char *usb_get_dev_path(DeviceState *qdev)
561 {
562     USBDevice *dev = USB_DEVICE(qdev);
563     DeviceState *hcd = qdev->parent_bus->parent;
564     char *id = qdev_get_dev_path(hcd);
565 
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);
572     }
573 }
574 
usb_get_fw_dev_path(DeviceState * qdev)575 static char *usb_get_fw_dev_path(DeviceState *qdev)
576 {
577     USBDevice *dev = USB_DEVICE(qdev);
578     char *fw_path, *in;
579     ssize_t pos = 0, fw_len;
580     long nr;
581 
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;
596         }
597     }
598     return fw_path;
599 }
600 
qmp_x_query_usb(Error ** errp)601 HumanReadableText *qmp_x_query_usb(Error **errp)
602 {
603     g_autoptr(GString) buf = g_string_new("");
604     USBBus *bus;
605     USBDevice *dev;
606     USBPort *port;
607 
608     if (QTAILQ_EMPTY(&busses)) {
609         error_setg(errp, "USB support not enabled");
610         return NULL;
611     }
612 
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 ?: "");
625         }
626     }
627 
628     return human_readable_text_from_str(buf);
629 }
630 
631 /* handle legacy -usbdevice cmd line option */
usbdevice_create(const char * driver)632 USBDevice *usbdevice_create(const char *driver)
633 {
634     USBBus *bus = QTAILQ_FIRST(&busses);
635     LegacyUSBFactory *f = NULL;
636     Error *err = NULL;
637     GSList *i;
638     USBDevice *dev;
639 
640     if (strchr(driver, ':')) {
641         error_report("usbdevice parameters are not supported anymore");
642         return NULL;
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     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;
670     }
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;
676     }
677     return dev;
678 }
679 
usb_get_attached(Object * obj,Error ** errp)680 static bool usb_get_attached(Object *obj, Error **errp)
681 {
682     USBDevice *dev = USB_DEVICE(obj);
683 
684     return dev->attached;
685 }
686 
usb_set_attached(Object * obj,bool value,Error ** errp)687 static void usb_set_attached(Object *obj, bool value, Error **errp)
688 {
689     USBDevice *dev = USB_DEVICE(obj);
690 
691     if (dev->attached == value) {
692         return;
693     }
694 
695     if (value) {
696         usb_device_attach(dev, errp);
697     } else {
698         usb_device_detach(dev);
699     }
700 }
701 
usb_device_instance_init(Object * obj)702 static void usb_device_instance_init(Object *obj)
703 {
704     USBDevice *dev = USB_DEVICE(obj);
705     USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
706 
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);
713     }
714 }
715 
usb_device_class_init(ObjectClass * klass,void * data)716 static void usb_device_class_init(ObjectClass *klass, void *data)
717 {
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);
723 }
724 
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,
733 };
734 
usb_register_types(void)735 static void usb_register_types(void)
736 {
737     type_register_static(&usb_bus_info);
738     type_register_static(&usb_device_type_info);
739 }
740 
741 type_init(usb_register_types)
742