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