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