xref: /openbmc/qemu/hw/usb/bus.c (revision 709395f8)
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 
505 void usb_check_attach(USBDevice *dev, Error **errp)
506 {
507     USBBus *bus = usb_bus_from_device(dev);
508     USBPort *port = dev->port;
509     char devspeed[32], portspeed[32];
510 
511     assert(port != NULL);
512     assert(!dev->attached);
513     usb_mask_to_str(devspeed, sizeof(devspeed), dev->speedmask);
514     usb_mask_to_str(portspeed, sizeof(portspeed), port->speedmask);
515     trace_usb_port_attach(bus->busnr, port->path,
516                           devspeed, portspeed);
517 
518     if (!(port->speedmask & dev->speedmask)) {
519         error_setg(errp, "Warning: speed mismatch trying to attach"
520                    " usb device \"%s\" (%s speed)"
521                    " to bus \"%s\", port \"%s\" (%s speed)",
522                    dev->product_desc, devspeed,
523                    bus->qbus.name, port->path, portspeed);
524         return;
525     }
526 }
527 
528 void usb_device_attach(USBDevice *dev, Error **errp)
529 {
530     USBPort *port = dev->port;
531     Error *local_err = NULL;
532 
533     usb_check_attach(dev, &local_err);
534     if (local_err) {
535         error_propagate(errp, local_err);
536         return;
537     }
538 
539     dev->attached = true;
540     usb_attach(port);
541 }
542 
543 int usb_device_detach(USBDevice *dev)
544 {
545     USBBus *bus = usb_bus_from_device(dev);
546     USBPort *port = dev->port;
547 
548     assert(port != NULL);
549     assert(dev->attached);
550     trace_usb_port_detach(bus->busnr, port->path);
551 
552     usb_detach(port);
553     dev->attached = false;
554     return 0;
555 }
556 
557 static const char *usb_speed(unsigned int speed)
558 {
559     static const char *txt[] = {
560         [ USB_SPEED_LOW  ] = "1.5",
561         [ USB_SPEED_FULL ] = "12",
562         [ USB_SPEED_HIGH ] = "480",
563         [ USB_SPEED_SUPER ] = "5000",
564     };
565     if (speed >= ARRAY_SIZE(txt))
566         return "?";
567     return txt[speed];
568 }
569 
570 static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
571 {
572     USBDevice *dev = USB_DEVICE(qdev);
573     USBBus *bus = usb_bus_from_device(dev);
574 
575     monitor_printf(mon, "%*saddr %d.%d, port %s, speed %s, name %s%s\n",
576                    indent, "", bus->busnr, dev->addr,
577                    dev->port ? dev->port->path : "-",
578                    usb_speed(dev->speed), dev->product_desc,
579                    dev->attached ? ", attached" : "");
580 }
581 
582 static char *usb_get_dev_path(DeviceState *qdev)
583 {
584     USBDevice *dev = USB_DEVICE(qdev);
585     DeviceState *hcd = qdev->parent_bus->parent;
586     char *id = NULL;
587 
588     if (dev->flags & (1 << USB_DEV_FLAG_FULL_PATH)) {
589         id = qdev_get_dev_path(hcd);
590     }
591     if (id) {
592         char *ret = g_strdup_printf("%s/%s", id, dev->port->path);
593         g_free(id);
594         return ret;
595     } else {
596         return g_strdup(dev->port->path);
597     }
598 }
599 
600 static char *usb_get_fw_dev_path(DeviceState *qdev)
601 {
602     USBDevice *dev = USB_DEVICE(qdev);
603     char *fw_path, *in;
604     ssize_t pos = 0, fw_len;
605     long nr;
606 
607     fw_len = 32 + strlen(dev->port->path) * 6;
608     fw_path = g_malloc(fw_len);
609     in = dev->port->path;
610     while (fw_len - pos > 0) {
611         nr = strtol(in, &in, 10);
612         if (in[0] == '.') {
613             /* some hub between root port and device */
614             pos += snprintf(fw_path + pos, fw_len - pos, "hub@%lx/", nr);
615             in++;
616         } else {
617             /* the device itself */
618             pos += snprintf(fw_path + pos, fw_len - pos, "%s@%lx",
619                             qdev_fw_name(qdev), nr);
620             break;
621         }
622     }
623     return fw_path;
624 }
625 
626 void hmp_info_usb(Monitor *mon, const QDict *qdict)
627 {
628     USBBus *bus;
629     USBDevice *dev;
630     USBPort *port;
631 
632     if (QTAILQ_EMPTY(&busses)) {
633         monitor_printf(mon, "USB support not enabled\n");
634         return;
635     }
636 
637     QTAILQ_FOREACH(bus, &busses, next) {
638         QTAILQ_FOREACH(port, &bus->used, next) {
639             dev = port->dev;
640             if (!dev)
641                 continue;
642             monitor_printf(mon, "  Device %d.%d, Port %s, Speed %s Mb/s, "
643                            "Product %s%s%s\n",
644                            bus->busnr, dev->addr, port->path,
645                            usb_speed(dev->speed), dev->product_desc,
646                            dev->qdev.id ? ", ID: " : "",
647                            dev->qdev.id ?: "");
648         }
649     }
650 }
651 
652 /* handle legacy -usbdevice cmd line option */
653 USBDevice *usbdevice_create(const char *cmdline)
654 {
655     USBBus *bus = usb_bus_find(-1 /* any */);
656     LegacyUSBFactory *f = NULL;
657     Error *err = NULL;
658     GSList *i;
659     char driver[32];
660     const char *params;
661     int len;
662     USBDevice *dev;
663 
664     params = strchr(cmdline,':');
665     if (params) {
666         params++;
667         len = params - cmdline;
668         if (len > sizeof(driver))
669             len = sizeof(driver);
670         pstrcpy(driver, len, cmdline);
671     } else {
672         params = "";
673         pstrcpy(driver, sizeof(driver), cmdline);
674     }
675 
676     for (i = legacy_usb_factory; i; i = i->next) {
677         f = i->data;
678         if (strcmp(f->usbdevice_name, driver) == 0) {
679             break;
680         }
681     }
682     if (i == NULL) {
683 #if 0
684         /* no error because some drivers are not converted (yet) */
685         error_report("usbdevice %s not found", driver);
686 #endif
687         return NULL;
688     }
689 
690     if (!bus) {
691         error_report("Error: no usb bus to attach usbdevice %s, "
692                      "please try -machine usb=on and check that "
693                      "the machine model supports USB", driver);
694         return NULL;
695     }
696 
697     if (f->usbdevice_init) {
698         dev = f->usbdevice_init(bus, params);
699     } else {
700         if (*params) {
701             error_report("usbdevice %s accepts no params", driver);
702             return NULL;
703         }
704         dev = usb_create(bus, f->name);
705     }
706     if (!dev) {
707         error_report("Failed to create USB device '%s'", f->name);
708         return NULL;
709     }
710     object_property_set_bool(OBJECT(dev), true, "realized", &err);
711     if (err) {
712         error_reportf_err(err, "Failed to initialize USB device '%s': ",
713                           f->name);
714         object_unparent(OBJECT(dev));
715         return NULL;
716     }
717     return dev;
718 }
719 
720 static bool usb_get_attached(Object *obj, Error **errp)
721 {
722     USBDevice *dev = USB_DEVICE(obj);
723 
724     return dev->attached;
725 }
726 
727 static void usb_set_attached(Object *obj, bool value, Error **errp)
728 {
729     USBDevice *dev = USB_DEVICE(obj);
730     Error *err = NULL;
731 
732     if (dev->attached == value) {
733         return;
734     }
735 
736     if (value) {
737         usb_device_attach(dev, &err);
738         error_propagate(errp, err);
739     } else {
740         usb_device_detach(dev);
741     }
742 }
743 
744 static void usb_device_instance_init(Object *obj)
745 {
746     USBDevice *dev = USB_DEVICE(obj);
747     USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
748 
749     if (klass->attached_settable) {
750         object_property_add_bool(obj, "attached",
751                                  usb_get_attached, usb_set_attached,
752                                  NULL);
753     } else {
754         object_property_add_bool(obj, "attached",
755                                  usb_get_attached, NULL,
756                                  NULL);
757     }
758 }
759 
760 static void usb_device_class_init(ObjectClass *klass, void *data)
761 {
762     DeviceClass *k = DEVICE_CLASS(klass);
763     k->bus_type = TYPE_USB_BUS;
764     k->realize  = usb_qdev_realize;
765     k->unrealize = usb_qdev_unrealize;
766     k->props    = usb_props;
767 }
768 
769 static const TypeInfo usb_device_type_info = {
770     .name = TYPE_USB_DEVICE,
771     .parent = TYPE_DEVICE,
772     .instance_size = sizeof(USBDevice),
773     .instance_init = usb_device_instance_init,
774     .abstract = true,
775     .class_size = sizeof(USBDeviceClass),
776     .class_init = usb_device_class_init,
777 };
778 
779 static void usb_register_types(void)
780 {
781     type_register_static(&usb_bus_info);
782     type_register_static(&usb_device_type_info);
783 }
784 
785 type_init(usb_register_types)
786