xref: /openbmc/qemu/hw/usb/bus.c (revision 39164c13)
1 #include "qemu/osdep.h"
2 #include "hw/hw.h"
3 #include "hw/usb.h"
4 #include "hw/qdev.h"
5 #include "qapi/error.h"
6 #include "qemu/error-report.h"
7 #include "sysemu/sysemu.h"
8 #include "monitor/monitor.h"
9 #include "trace.h"
10 #include "qemu/cutils.h"
11 #include "migration/migration.h"
12 
13 static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent);
14 
15 static char *usb_get_dev_path(DeviceState *dev);
16 static char *usb_get_fw_dev_path(DeviceState *qdev);
17 static void usb_qdev_unrealize(DeviceState *qdev, Error **errp);
18 
19 static Property usb_props[] = {
20     DEFINE_PROP_STRING("port", USBDevice, port_path),
21     DEFINE_PROP_STRING("serial", USBDevice, serial),
22     DEFINE_PROP_BIT("full-path", USBDevice, flags,
23                     USB_DEV_FLAG_FULL_PATH, true),
24     DEFINE_PROP_BIT("msos-desc", USBDevice, flags,
25                     USB_DEV_FLAG_MSOS_DESC_ENABLE, true),
26     DEFINE_PROP_END_OF_LIST()
27 };
28 
29 static void usb_bus_class_init(ObjectClass *klass, void *data)
30 {
31     BusClass *k = BUS_CLASS(klass);
32     HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
33 
34     k->print_dev = usb_bus_dev_print;
35     k->get_dev_path = usb_get_dev_path;
36     k->get_fw_dev_path = usb_get_fw_dev_path;
37     hc->unplug = qdev_simple_device_unplug_cb;
38 }
39 
40 static const TypeInfo usb_bus_info = {
41     .name = TYPE_USB_BUS,
42     .parent = TYPE_BUS,
43     .instance_size = sizeof(USBBus),
44     .class_init = usb_bus_class_init,
45     .interfaces = (InterfaceInfo[]) {
46         { TYPE_HOTPLUG_HANDLER },
47         { }
48     }
49 };
50 
51 static int next_usb_bus = 0;
52 static QTAILQ_HEAD(, USBBus) busses = QTAILQ_HEAD_INITIALIZER(busses);
53 
54 static int usb_device_post_load(void *opaque, int version_id)
55 {
56     USBDevice *dev = opaque;
57 
58     if (dev->state == USB_STATE_NOTATTACHED) {
59         dev->attached = false;
60     } else {
61         dev->attached = true;
62     }
63     if (dev->setup_index < 0 ||
64         dev->setup_len < 0 ||
65         dev->setup_index > dev->setup_len ||
66         dev->setup_len > sizeof(dev->data_buf)) {
67         return -EINVAL;
68     }
69     return 0;
70 }
71 
72 const VMStateDescription vmstate_usb_device = {
73     .name = "USBDevice",
74     .version_id = 1,
75     .minimum_version_id = 1,
76     .post_load = usb_device_post_load,
77     .fields = (VMStateField[]) {
78         VMSTATE_UINT8(addr, USBDevice),
79         VMSTATE_INT32(state, USBDevice),
80         VMSTATE_INT32(remote_wakeup, USBDevice),
81         VMSTATE_INT32(setup_state, USBDevice),
82         VMSTATE_INT32(setup_len, USBDevice),
83         VMSTATE_INT32(setup_index, USBDevice),
84         VMSTATE_UINT8_ARRAY(setup_buf, USBDevice, 8),
85         VMSTATE_END_OF_LIST(),
86     }
87 };
88 
89 void usb_bus_new(USBBus *bus, size_t bus_size,
90                  USBBusOps *ops, DeviceState *host)
91 {
92     qbus_create_inplace(bus, bus_size, TYPE_USB_BUS, host, NULL);
93     qbus_set_bus_hotplug_handler(BUS(bus), &error_abort);
94     bus->ops = ops;
95     bus->busnr = next_usb_bus++;
96     QTAILQ_INIT(&bus->free);
97     QTAILQ_INIT(&bus->used);
98     QTAILQ_INSERT_TAIL(&busses, bus, next);
99 }
100 
101 void usb_bus_release(USBBus *bus)
102 {
103     assert(next_usb_bus > 0);
104 
105     QTAILQ_REMOVE(&busses, bus, next);
106 }
107 
108 USBBus *usb_bus_find(int busnr)
109 {
110     USBBus *bus;
111 
112     if (-1 == busnr)
113         return QTAILQ_FIRST(&busses);
114     QTAILQ_FOREACH(bus, &busses, next) {
115         if (bus->busnr == busnr)
116             return bus;
117     }
118     return NULL;
119 }
120 
121 static void usb_device_realize(USBDevice *dev, Error **errp)
122 {
123     USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
124 
125     if (klass->realize) {
126         klass->realize(dev, errp);
127     }
128 }
129 
130 USBDevice *usb_device_find_device(USBDevice *dev, uint8_t addr)
131 {
132     USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
133     if (klass->find_device) {
134         return klass->find_device(dev, addr);
135     }
136     return NULL;
137 }
138 
139 static void usb_device_unrealize(USBDevice *dev, Error **errp)
140 {
141     USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
142 
143     if (klass->unrealize) {
144         klass->unrealize(dev, errp);
145     }
146 }
147 
148 void usb_device_cancel_packet(USBDevice *dev, USBPacket *p)
149 {
150     USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
151     if (klass->cancel_packet) {
152         klass->cancel_packet(dev, p);
153     }
154 }
155 
156 void usb_device_handle_attach(USBDevice *dev)
157 {
158     USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
159     if (klass->handle_attach) {
160         klass->handle_attach(dev);
161     }
162 }
163 
164 void usb_device_handle_reset(USBDevice *dev)
165 {
166     USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
167     if (klass->handle_reset) {
168         klass->handle_reset(dev);
169     }
170 }
171 
172 void usb_device_handle_control(USBDevice *dev, USBPacket *p, int request,
173                                int value, int index, int length, uint8_t *data)
174 {
175     USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
176     if (klass->handle_control) {
177         klass->handle_control(dev, p, request, value, index, length, data);
178     }
179 }
180 
181 void usb_device_handle_data(USBDevice *dev, USBPacket *p)
182 {
183     USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
184     if (klass->handle_data) {
185         klass->handle_data(dev, p);
186     }
187 }
188 
189 const char *usb_device_get_product_desc(USBDevice *dev)
190 {
191     USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
192     return klass->product_desc;
193 }
194 
195 const USBDesc *usb_device_get_usb_desc(USBDevice *dev)
196 {
197     USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
198     if (dev->usb_desc) {
199         return dev->usb_desc;
200     }
201     return klass->usb_desc;
202 }
203 
204 void usb_device_set_interface(USBDevice *dev, int interface,
205                               int alt_old, int alt_new)
206 {
207     USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
208     if (klass->set_interface) {
209         klass->set_interface(dev, interface, alt_old, alt_new);
210     }
211 }
212 
213 void usb_device_flush_ep_queue(USBDevice *dev, USBEndpoint *ep)
214 {
215     USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
216     if (klass->flush_ep_queue) {
217         klass->flush_ep_queue(dev, ep);
218     }
219 }
220 
221 void usb_device_ep_stopped(USBDevice *dev, USBEndpoint *ep)
222 {
223     USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
224     if (klass->ep_stopped) {
225         klass->ep_stopped(dev, ep);
226     }
227 }
228 
229 int usb_device_alloc_streams(USBDevice *dev, USBEndpoint **eps, int nr_eps,
230                              int streams)
231 {
232     USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
233     if (klass->alloc_streams) {
234         return klass->alloc_streams(dev, eps, nr_eps, streams);
235     }
236     return 0;
237 }
238 
239 void usb_device_free_streams(USBDevice *dev, USBEndpoint **eps, int nr_eps)
240 {
241     USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
242     if (klass->free_streams) {
243         klass->free_streams(dev, eps, nr_eps);
244     }
245 }
246 
247 static void usb_qdev_realize(DeviceState *qdev, Error **errp)
248 {
249     USBDevice *dev = USB_DEVICE(qdev);
250     Error *local_err = NULL;
251 
252     pstrcpy(dev->product_desc, sizeof(dev->product_desc),
253             usb_device_get_product_desc(dev));
254     dev->auto_attach = 1;
255     QLIST_INIT(&dev->strings);
256     usb_ep_init(dev);
257 
258     usb_claim_port(dev, &local_err);
259     if (local_err) {
260         error_propagate(errp, local_err);
261         return;
262     }
263 
264     usb_device_realize(dev, &local_err);
265     if (local_err) {
266         usb_release_port(dev);
267         error_propagate(errp, local_err);
268         return;
269     }
270 
271     if (dev->auto_attach) {
272         usb_device_attach(dev, &local_err);
273         if (local_err) {
274             usb_qdev_unrealize(qdev, NULL);
275             error_propagate(errp, local_err);
276             return;
277         }
278     }
279 }
280 
281 static void usb_qdev_unrealize(DeviceState *qdev, Error **errp)
282 {
283     USBDevice *dev = USB_DEVICE(qdev);
284     USBDescString *s, *next;
285 
286     QLIST_FOREACH_SAFE(s, &dev->strings, next, next) {
287         QLIST_REMOVE(s, next);
288         g_free(s->str);
289         g_free(s);
290     }
291 
292     if (dev->attached) {
293         usb_device_detach(dev);
294     }
295     usb_device_unrealize(dev, errp);
296     if (dev->port) {
297         usb_release_port(dev);
298     }
299 }
300 
301 typedef struct LegacyUSBFactory
302 {
303     const char *name;
304     const char *usbdevice_name;
305     USBDevice *(*usbdevice_init)(USBBus *bus, const char *params);
306 } LegacyUSBFactory;
307 
308 static GSList *legacy_usb_factory;
309 
310 void usb_legacy_register(const char *typename, const char *usbdevice_name,
311                          USBDevice *(*usbdevice_init)(USBBus *bus,
312                                                       const char *params))
313 {
314     if (usbdevice_name) {
315         LegacyUSBFactory *f = g_malloc0(sizeof(*f));
316         f->name = typename;
317         f->usbdevice_name = usbdevice_name;
318         f->usbdevice_init = usbdevice_init;
319         legacy_usb_factory = g_slist_append(legacy_usb_factory, f);
320     }
321 }
322 
323 USBDevice *usb_create(USBBus *bus, const char *name)
324 {
325     DeviceState *dev;
326 
327     dev = qdev_create(&bus->qbus, name);
328     return USB_DEVICE(dev);
329 }
330 
331 static USBDevice *usb_try_create_simple(USBBus *bus, const char *name,
332                                         Error **errp)
333 {
334     Error *err = NULL;
335     USBDevice *dev;
336 
337     dev = USB_DEVICE(qdev_try_create(&bus->qbus, name));
338     if (!dev) {
339         error_setg(errp, "Failed to create USB device '%s'", name);
340         return NULL;
341     }
342     object_property_set_bool(OBJECT(dev), true, "realized", &err);
343     if (err) {
344         error_propagate(errp, err);
345         error_prepend(errp, "Failed to initialize USB device '%s': ",
346                       name);
347         object_unparent(OBJECT(dev));
348         return NULL;
349     }
350     return dev;
351 }
352 
353 USBDevice *usb_create_simple(USBBus *bus, const char *name)
354 {
355     return usb_try_create_simple(bus, name, &error_abort);
356 }
357 
358 static void usb_fill_port(USBPort *port, void *opaque, int index,
359                           USBPortOps *ops, int speedmask)
360 {
361     port->opaque = opaque;
362     port->index = index;
363     port->ops = ops;
364     port->speedmask = speedmask;
365     usb_port_location(port, NULL, index + 1);
366 }
367 
368 void usb_register_port(USBBus *bus, USBPort *port, void *opaque, int index,
369                        USBPortOps *ops, int speedmask)
370 {
371     usb_fill_port(port, opaque, index, ops, speedmask);
372     QTAILQ_INSERT_TAIL(&bus->free, port, next);
373     bus->nfree++;
374 }
375 
376 void usb_register_companion(const char *masterbus, USBPort *ports[],
377                             uint32_t portcount, uint32_t firstport,
378                             void *opaque, USBPortOps *ops, int speedmask,
379                             Error **errp)
380 {
381     USBBus *bus;
382     int i;
383 
384     QTAILQ_FOREACH(bus, &busses, next) {
385         if (strcmp(bus->qbus.name, masterbus) == 0) {
386             break;
387         }
388     }
389 
390     if (!bus) {
391         error_setg(errp, "USB bus '%s' not found", masterbus);
392         return;
393     }
394     if (!bus->ops->register_companion) {
395         error_setg(errp, "Can't use USB bus '%s' as masterbus,"
396                    " it doesn't support companion controllers",
397                    masterbus);
398         return;
399     }
400 
401     for (i = 0; i < portcount; i++) {
402         usb_fill_port(ports[i], opaque, i, ops, speedmask);
403     }
404 
405     bus->ops->register_companion(bus, ports, portcount, firstport, errp);
406 }
407 
408 void usb_port_location(USBPort *downstream, USBPort *upstream, int portnr)
409 {
410     if (upstream) {
411         snprintf(downstream->path, sizeof(downstream->path), "%s.%d",
412                  upstream->path, portnr);
413         downstream->hubcount = upstream->hubcount + 1;
414     } else {
415         snprintf(downstream->path, sizeof(downstream->path), "%d", portnr);
416         downstream->hubcount = 0;
417     }
418 }
419 
420 void usb_unregister_port(USBBus *bus, USBPort *port)
421 {
422     if (port->dev) {
423         object_unparent(OBJECT(port->dev));
424     }
425     QTAILQ_REMOVE(&bus->free, port, next);
426     bus->nfree--;
427 }
428 
429 void usb_claim_port(USBDevice *dev, Error **errp)
430 {
431     USBBus *bus = usb_bus_from_device(dev);
432     USBPort *port;
433 
434     assert(dev->port == NULL);
435 
436     if (dev->port_path) {
437         QTAILQ_FOREACH(port, &bus->free, next) {
438             if (strcmp(port->path, dev->port_path) == 0) {
439                 break;
440             }
441         }
442         if (port == NULL) {
443             error_setg(errp, "usb port %s (bus %s) not found (in use?)",
444                        dev->port_path, bus->qbus.name);
445             return;
446         }
447     } else {
448         if (bus->nfree == 1 && strcmp(object_get_typename(OBJECT(dev)), "usb-hub") != 0) {
449             /* Create a new hub and chain it on */
450             usb_try_create_simple(bus, "usb-hub", NULL);
451         }
452         if (bus->nfree == 0) {
453             error_setg(errp, "tried to attach usb device %s to a bus "
454                        "with no free ports", dev->product_desc);
455             return;
456         }
457         port = QTAILQ_FIRST(&bus->free);
458     }
459     trace_usb_port_claim(bus->busnr, port->path);
460 
461     QTAILQ_REMOVE(&bus->free, port, next);
462     bus->nfree--;
463 
464     dev->port = port;
465     port->dev = dev;
466 
467     QTAILQ_INSERT_TAIL(&bus->used, port, next);
468     bus->nused++;
469 }
470 
471 void usb_release_port(USBDevice *dev)
472 {
473     USBBus *bus = usb_bus_from_device(dev);
474     USBPort *port = dev->port;
475 
476     assert(port != NULL);
477     trace_usb_port_release(bus->busnr, port->path);
478 
479     QTAILQ_REMOVE(&bus->used, port, next);
480     bus->nused--;
481 
482     dev->port = NULL;
483     port->dev = NULL;
484 
485     QTAILQ_INSERT_TAIL(&bus->free, port, next);
486     bus->nfree++;
487 }
488 
489 static void usb_mask_to_str(char *dest, size_t size,
490                             unsigned int speedmask)
491 {
492     static const struct {
493         unsigned int mask;
494         const char *name;
495     } speeds[] = {
496         { .mask = USB_SPEED_MASK_FULL,  .name = "full"  },
497         { .mask = USB_SPEED_MASK_HIGH,  .name = "high"  },
498         { .mask = USB_SPEED_MASK_SUPER, .name = "super" },
499     };
500     int i, pos = 0;
501 
502     for (i = 0; i < ARRAY_SIZE(speeds); i++) {
503         if (speeds[i].mask & speedmask) {
504             pos += snprintf(dest + pos, size - pos, "%s%s",
505                             pos ? "+" : "",
506                             speeds[i].name);
507         }
508     }
509 }
510 
511 void usb_check_attach(USBDevice *dev, Error **errp)
512 {
513     USBBus *bus = usb_bus_from_device(dev);
514     USBPort *port = dev->port;
515     char devspeed[32], portspeed[32];
516 
517     assert(port != NULL);
518     assert(!dev->attached);
519     usb_mask_to_str(devspeed, sizeof(devspeed), dev->speedmask);
520     usb_mask_to_str(portspeed, sizeof(portspeed), port->speedmask);
521     trace_usb_port_attach(bus->busnr, port->path,
522                           devspeed, portspeed);
523 
524     if (!(port->speedmask & dev->speedmask)) {
525         error_setg(errp, "Warning: speed mismatch trying to attach"
526                    " usb device \"%s\" (%s speed)"
527                    " to bus \"%s\", port \"%s\" (%s speed)",
528                    dev->product_desc, devspeed,
529                    bus->qbus.name, port->path, portspeed);
530         return;
531     }
532 }
533 
534 void usb_device_attach(USBDevice *dev, Error **errp)
535 {
536     USBPort *port = dev->port;
537     Error *local_err = NULL;
538 
539     usb_check_attach(dev, &local_err);
540     if (local_err) {
541         error_propagate(errp, local_err);
542         return;
543     }
544 
545     dev->attached = true;
546     usb_attach(port);
547 }
548 
549 int usb_device_detach(USBDevice *dev)
550 {
551     USBBus *bus = usb_bus_from_device(dev);
552     USBPort *port = dev->port;
553 
554     assert(port != NULL);
555     assert(dev->attached);
556     trace_usb_port_detach(bus->busnr, port->path);
557 
558     usb_detach(port);
559     dev->attached = false;
560     return 0;
561 }
562 
563 int usb_device_delete_addr(int busnr, int addr)
564 {
565     USBBus *bus;
566     USBPort *port;
567     USBDevice *dev;
568 
569     bus = usb_bus_find(busnr);
570     if (!bus)
571         return -1;
572 
573     QTAILQ_FOREACH(port, &bus->used, next) {
574         if (port->dev->addr == addr)
575             break;
576     }
577     if (!port)
578         return -1;
579     dev = port->dev;
580 
581     object_unparent(OBJECT(dev));
582     return 0;
583 }
584 
585 static const char *usb_speed(unsigned int speed)
586 {
587     static const char *txt[] = {
588         [ USB_SPEED_LOW  ] = "1.5",
589         [ USB_SPEED_FULL ] = "12",
590         [ USB_SPEED_HIGH ] = "480",
591         [ USB_SPEED_SUPER ] = "5000",
592     };
593     if (speed >= ARRAY_SIZE(txt))
594         return "?";
595     return txt[speed];
596 }
597 
598 static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
599 {
600     USBDevice *dev = USB_DEVICE(qdev);
601     USBBus *bus = usb_bus_from_device(dev);
602 
603     monitor_printf(mon, "%*saddr %d.%d, port %s, speed %s, name %s%s\n",
604                    indent, "", bus->busnr, dev->addr,
605                    dev->port ? dev->port->path : "-",
606                    usb_speed(dev->speed), dev->product_desc,
607                    dev->attached ? ", attached" : "");
608 }
609 
610 static char *usb_get_dev_path(DeviceState *qdev)
611 {
612     USBDevice *dev = USB_DEVICE(qdev);
613     DeviceState *hcd = qdev->parent_bus->parent;
614     char *id = NULL;
615 
616     if (dev->flags & (1 << USB_DEV_FLAG_FULL_PATH)) {
617         id = qdev_get_dev_path(hcd);
618     }
619     if (id) {
620         char *ret = g_strdup_printf("%s/%s", id, dev->port->path);
621         g_free(id);
622         return ret;
623     } else {
624         return g_strdup(dev->port->path);
625     }
626 }
627 
628 static char *usb_get_fw_dev_path(DeviceState *qdev)
629 {
630     USBDevice *dev = USB_DEVICE(qdev);
631     char *fw_path, *in;
632     ssize_t pos = 0, fw_len;
633     long nr;
634 
635     fw_len = 32 + strlen(dev->port->path) * 6;
636     fw_path = g_malloc(fw_len);
637     in = dev->port->path;
638     while (fw_len - pos > 0) {
639         nr = strtol(in, &in, 10);
640         if (in[0] == '.') {
641             /* some hub between root port and device */
642             pos += snprintf(fw_path + pos, fw_len - pos, "hub@%lx/", nr);
643             in++;
644         } else {
645             /* the device itself */
646             pos += snprintf(fw_path + pos, fw_len - pos, "%s@%lx",
647                             qdev_fw_name(qdev), nr);
648             break;
649         }
650     }
651     return fw_path;
652 }
653 
654 void hmp_info_usb(Monitor *mon, const QDict *qdict)
655 {
656     USBBus *bus;
657     USBDevice *dev;
658     USBPort *port;
659 
660     if (QTAILQ_EMPTY(&busses)) {
661         monitor_printf(mon, "USB support not enabled\n");
662         return;
663     }
664 
665     QTAILQ_FOREACH(bus, &busses, next) {
666         QTAILQ_FOREACH(port, &bus->used, next) {
667             dev = port->dev;
668             if (!dev)
669                 continue;
670             monitor_printf(mon, "  Device %d.%d, Port %s, Speed %s Mb/s, "
671                            "Product %s%s%s\n",
672                            bus->busnr, dev->addr, port->path,
673                            usb_speed(dev->speed), dev->product_desc,
674                            dev->qdev.id ? ", ID: " : "",
675                            dev->qdev.id ?: "");
676         }
677     }
678 }
679 
680 /* handle legacy -usbdevice cmd line option */
681 USBDevice *usbdevice_create(const char *cmdline)
682 {
683     USBBus *bus = usb_bus_find(-1 /* any */);
684     LegacyUSBFactory *f = NULL;
685     Error *err = NULL;
686     GSList *i;
687     char driver[32];
688     const char *params;
689     int len;
690     USBDevice *dev;
691     ObjectClass *klass;
692     DeviceClass *dc;
693 
694     params = strchr(cmdline,':');
695     if (params) {
696         params++;
697         len = params - cmdline;
698         if (len > sizeof(driver))
699             len = sizeof(driver);
700         pstrcpy(driver, len, cmdline);
701     } else {
702         params = "";
703         pstrcpy(driver, sizeof(driver), cmdline);
704     }
705 
706     for (i = legacy_usb_factory; i; i = i->next) {
707         f = i->data;
708         if (strcmp(f->usbdevice_name, driver) == 0) {
709             break;
710         }
711     }
712     if (i == NULL) {
713 #if 0
714         /* no error because some drivers are not converted (yet) */
715         error_report("usbdevice %s not found", driver);
716 #endif
717         return NULL;
718     }
719 
720     if (!bus) {
721         error_report("Error: no usb bus to attach usbdevice %s, "
722                      "please try -machine usb=on and check that "
723                      "the machine model supports USB", driver);
724         return NULL;
725     }
726 
727     klass = object_class_by_name(f->name);
728     if (klass == NULL) {
729         error_report("Device '%s' not found", f->name);
730         return NULL;
731     }
732 
733     dc = DEVICE_CLASS(klass);
734 
735     if (only_migratable) {
736         if (dc->vmsd->unmigratable) {
737             error_report("Device %s is not migratable, but --only-migratable "
738                          "was specified", f->name);
739             return NULL;
740         }
741     }
742 
743     if (f->usbdevice_init) {
744         dev = f->usbdevice_init(bus, params);
745     } else {
746         if (*params) {
747             error_report("usbdevice %s accepts no params", driver);
748             return NULL;
749         }
750         dev = usb_create(bus, f->name);
751     }
752     if (!dev) {
753         error_report("Failed to create USB device '%s'", f->name);
754         return NULL;
755     }
756     object_property_set_bool(OBJECT(dev), true, "realized", &err);
757     if (err) {
758         error_reportf_err(err, "Failed to initialize USB device '%s': ",
759                           f->name);
760         object_unparent(OBJECT(dev));
761         return NULL;
762     }
763     return dev;
764 }
765 
766 static bool usb_get_attached(Object *obj, Error **errp)
767 {
768     USBDevice *dev = USB_DEVICE(obj);
769 
770     return dev->attached;
771 }
772 
773 static void usb_set_attached(Object *obj, bool value, Error **errp)
774 {
775     USBDevice *dev = USB_DEVICE(obj);
776     Error *err = NULL;
777 
778     if (dev->attached == value) {
779         return;
780     }
781 
782     if (value) {
783         usb_device_attach(dev, &err);
784         if (err) {
785             error_propagate(errp, err);
786         }
787     } else {
788         usb_device_detach(dev);
789     }
790 }
791 
792 static void usb_device_instance_init(Object *obj)
793 {
794     USBDevice *dev = USB_DEVICE(obj);
795     USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
796 
797     if (klass->attached_settable) {
798         object_property_add_bool(obj, "attached",
799                                  usb_get_attached, usb_set_attached,
800                                  NULL);
801     } else {
802         object_property_add_bool(obj, "attached",
803                                  usb_get_attached, NULL,
804                                  NULL);
805     }
806 }
807 
808 static void usb_device_class_init(ObjectClass *klass, void *data)
809 {
810     DeviceClass *k = DEVICE_CLASS(klass);
811     k->bus_type = TYPE_USB_BUS;
812     k->realize  = usb_qdev_realize;
813     k->unrealize = usb_qdev_unrealize;
814     k->props    = usb_props;
815 }
816 
817 static const TypeInfo usb_device_type_info = {
818     .name = TYPE_USB_DEVICE,
819     .parent = TYPE_DEVICE,
820     .instance_size = sizeof(USBDevice),
821     .instance_init = usb_device_instance_init,
822     .abstract = true,
823     .class_size = sizeof(USBDeviceClass),
824     .class_init = usb_device_class_init,
825 };
826 
827 static void usb_register_types(void)
828 {
829     type_register_static(&usb_bus_info);
830     type_register_static(&usb_device_type_info);
831 }
832 
833 type_init(usb_register_types)
834