xref: /openbmc/qemu/hw/usb/bus.c (revision f101c9fe)
1 #include "qemu/osdep.h"
2 #include "hw/qdev-properties.h"
3 #include "hw/usb.h"
4 #include "qapi/error.h"
5 #include "qemu/error-report.h"
6 #include "qemu/module.h"
7 #include "sysemu/sysemu.h"
8 #include "migration/vmstate.h"
9 #include "monitor/monitor.h"
10 #include "trace.h"
11 #include "qemu/cutils.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);
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("msos-desc", USBDevice, flags,
23                     USB_DEV_FLAG_MSOS_DESC_ENABLE, true),
24     DEFINE_PROP_STRING("pcap", USBDevice, pcap_filename),
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));
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)
133 {
134     USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
135 
136     if (klass->unrealize) {
137         klass->unrealize(dev);
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);
268             error_propagate(errp, local_err);
269             return;
270         }
271     }
272 
273     if (dev->pcap_filename) {
274         int fd = qemu_open_old(dev->pcap_filename, O_CREAT | O_WRONLY | O_TRUNC, 0666);
275         if (fd < 0) {
276             error_setg(errp, "open %s failed", dev->pcap_filename);
277             usb_qdev_unrealize(qdev);
278             return;
279         }
280         dev->pcap = fdopen(fd, "w");
281         usb_pcap_init(dev->pcap);
282     }
283 }
284 
285 static void usb_qdev_unrealize(DeviceState *qdev)
286 {
287     USBDevice *dev = USB_DEVICE(qdev);
288     USBDescString *s, *next;
289 
290     QLIST_FOREACH_SAFE(s, &dev->strings, next, next) {
291         QLIST_REMOVE(s, next);
292         g_free(s->str);
293         g_free(s);
294     }
295 
296     if (dev->pcap) {
297         fclose(dev->pcap);
298     }
299 
300     if (dev->attached) {
301         usb_device_detach(dev);
302     }
303     usb_device_unrealize(dev);
304     if (dev->port) {
305         usb_release_port(dev);
306     }
307 }
308 
309 typedef struct LegacyUSBFactory
310 {
311     const char *name;
312     const char *usbdevice_name;
313     USBDevice *(*usbdevice_init)(void);
314 } LegacyUSBFactory;
315 
316 static GSList *legacy_usb_factory;
317 
318 void usb_legacy_register(const char *typename, const char *usbdevice_name,
319                          USBDevice *(*usbdevice_init)(void))
320 {
321     if (usbdevice_name) {
322         LegacyUSBFactory *f = g_malloc0(sizeof(*f));
323         f->name = typename;
324         f->usbdevice_name = usbdevice_name;
325         f->usbdevice_init = usbdevice_init;
326         legacy_usb_factory = g_slist_append(legacy_usb_factory, f);
327     }
328 }
329 
330 USBDevice *usb_new(const char *name)
331 {
332     return USB_DEVICE(qdev_new(name));
333 }
334 
335 static USBDevice *usb_try_new(const char *name)
336 {
337     return USB_DEVICE(qdev_try_new(name));
338 }
339 
340 bool usb_realize_and_unref(USBDevice *dev, USBBus *bus, Error **errp)
341 {
342     return qdev_realize_and_unref(&dev->qdev, &bus->qbus, errp);
343 }
344 
345 USBDevice *usb_create_simple(USBBus *bus, const char *name)
346 {
347     USBDevice *dev = usb_new(name);
348 
349     usb_realize_and_unref(dev, bus, &error_abort);
350     return dev;
351 }
352 
353 static void usb_fill_port(USBPort *port, void *opaque, int index,
354                           USBPortOps *ops, int speedmask)
355 {
356     port->opaque = opaque;
357     port->index = index;
358     port->ops = ops;
359     port->speedmask = speedmask;
360     usb_port_location(port, NULL, index + 1);
361 }
362 
363 void usb_register_port(USBBus *bus, USBPort *port, void *opaque, int index,
364                        USBPortOps *ops, int speedmask)
365 {
366     usb_fill_port(port, opaque, index, ops, speedmask);
367     QTAILQ_INSERT_TAIL(&bus->free, port, next);
368     bus->nfree++;
369 }
370 
371 void usb_register_companion(const char *masterbus, USBPort *ports[],
372                             uint32_t portcount, uint32_t firstport,
373                             void *opaque, USBPortOps *ops, int speedmask,
374                             Error **errp)
375 {
376     USBBus *bus;
377     int i;
378 
379     QTAILQ_FOREACH(bus, &busses, next) {
380         if (strcmp(bus->qbus.name, masterbus) == 0) {
381             break;
382         }
383     }
384 
385     if (!bus) {
386         error_setg(errp, "USB bus '%s' not found", masterbus);
387         return;
388     }
389     if (!bus->ops->register_companion) {
390         error_setg(errp, "Can't use USB bus '%s' as masterbus,"
391                    " it doesn't support companion controllers",
392                    masterbus);
393         return;
394     }
395 
396     for (i = 0; i < portcount; i++) {
397         usb_fill_port(ports[i], opaque, i, ops, speedmask);
398     }
399 
400     bus->ops->register_companion(bus, ports, portcount, firstport, errp);
401 }
402 
403 void usb_port_location(USBPort *downstream, USBPort *upstream, int portnr)
404 {
405     if (upstream) {
406         int l = snprintf(downstream->path, sizeof(downstream->path), "%s.%d",
407                          upstream->path, portnr);
408         /* Max string is nn.nn.nn.nn.nn, which fits in 16 bytes */
409         assert(l < sizeof(downstream->path));
410         downstream->hubcount = upstream->hubcount + 1;
411     } else {
412         snprintf(downstream->path, sizeof(downstream->path), "%d", portnr);
413         downstream->hubcount = 0;
414     }
415 }
416 
417 void usb_unregister_port(USBBus *bus, USBPort *port)
418 {
419     if (port->dev) {
420         object_unparent(OBJECT(port->dev));
421     }
422     QTAILQ_REMOVE(&bus->free, port, next);
423     bus->nfree--;
424 }
425 
426 void usb_claim_port(USBDevice *dev, Error **errp)
427 {
428     USBBus *bus = usb_bus_from_device(dev);
429     USBPort *port;
430     USBDevice *hub;
431 
432     assert(dev->port == NULL);
433 
434     if (dev->port_path) {
435         QTAILQ_FOREACH(port, &bus->free, next) {
436             if (strcmp(port->path, dev->port_path) == 0) {
437                 break;
438             }
439         }
440         if (port == NULL) {
441             error_setg(errp, "usb port %s (bus %s) not found (in use?)",
442                        dev->port_path, bus->qbus.name);
443             return;
444         }
445     } else {
446         if (bus->nfree == 1 && strcmp(object_get_typename(OBJECT(dev)), "usb-hub") != 0) {
447             /* Create a new hub and chain it on */
448             hub = usb_try_new("usb-hub");
449             if (hub) {
450                 usb_realize_and_unref(hub, bus, NULL);
451             }
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     if (pos == 0) {
512         snprintf(dest, size, "unknown");
513     }
514 }
515 
516 void usb_check_attach(USBDevice *dev, Error **errp)
517 {
518     USBBus *bus = usb_bus_from_device(dev);
519     USBPort *port = dev->port;
520     char devspeed[32], portspeed[32];
521 
522     assert(port != NULL);
523     assert(!dev->attached);
524     usb_mask_to_str(devspeed, sizeof(devspeed), dev->speedmask);
525     usb_mask_to_str(portspeed, sizeof(portspeed), port->speedmask);
526     trace_usb_port_attach(bus->busnr, port->path,
527                           devspeed, portspeed);
528 
529     if (!(port->speedmask & dev->speedmask)) {
530         error_setg(errp, "Warning: speed mismatch trying to attach"
531                    " usb device \"%s\" (%s speed)"
532                    " to bus \"%s\", port \"%s\" (%s speed)",
533                    dev->product_desc, devspeed,
534                    bus->qbus.name, port->path, portspeed);
535         return;
536     }
537 }
538 
539 void usb_device_attach(USBDevice *dev, Error **errp)
540 {
541     USBPort *port = dev->port;
542     Error *local_err = NULL;
543 
544     usb_check_attach(dev, &local_err);
545     if (local_err) {
546         error_propagate(errp, local_err);
547         return;
548     }
549 
550     dev->attached = true;
551     usb_attach(port);
552 }
553 
554 int usb_device_detach(USBDevice *dev)
555 {
556     USBBus *bus = usb_bus_from_device(dev);
557     USBPort *port = dev->port;
558 
559     assert(port != NULL);
560     assert(dev->attached);
561     trace_usb_port_detach(bus->busnr, port->path);
562 
563     usb_detach(port);
564     dev->attached = false;
565     return 0;
566 }
567 
568 static const char *usb_speed(unsigned int speed)
569 {
570     static const char *txt[] = {
571         [ USB_SPEED_LOW  ] = "1.5",
572         [ USB_SPEED_FULL ] = "12",
573         [ USB_SPEED_HIGH ] = "480",
574         [ USB_SPEED_SUPER ] = "5000",
575     };
576     if (speed >= ARRAY_SIZE(txt))
577         return "?";
578     return txt[speed];
579 }
580 
581 static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
582 {
583     USBDevice *dev = USB_DEVICE(qdev);
584     USBBus *bus = usb_bus_from_device(dev);
585 
586     monitor_printf(mon, "%*saddr %d.%d, port %s, speed %s, name %s%s\n",
587                    indent, "", bus->busnr, dev->addr,
588                    dev->port ? dev->port->path : "-",
589                    usb_speed(dev->speed), dev->product_desc,
590                    dev->attached ? ", attached" : "");
591 }
592 
593 static char *usb_get_dev_path(DeviceState *qdev)
594 {
595     USBDevice *dev = USB_DEVICE(qdev);
596     DeviceState *hcd = qdev->parent_bus->parent;
597     char *id = qdev_get_dev_path(hcd);
598 
599     if (id) {
600         char *ret = g_strdup_printf("%s/%s", id, dev->port->path);
601         g_free(id);
602         return ret;
603     } else {
604         return g_strdup(dev->port->path);
605     }
606 }
607 
608 static char *usb_get_fw_dev_path(DeviceState *qdev)
609 {
610     USBDevice *dev = USB_DEVICE(qdev);
611     char *fw_path, *in;
612     ssize_t pos = 0, fw_len;
613     long nr;
614 
615     fw_len = 32 + strlen(dev->port->path) * 6;
616     fw_path = g_malloc(fw_len);
617     in = dev->port->path;
618     while (fw_len - pos > 0) {
619         nr = strtol(in, &in, 10);
620         if (in[0] == '.') {
621             /* some hub between root port and device */
622             pos += snprintf(fw_path + pos, fw_len - pos, "hub@%lx/", nr);
623             in++;
624         } else {
625             /* the device itself */
626             snprintf(fw_path + pos, fw_len - pos, "%s@%lx",
627                      qdev_fw_name(qdev), nr);
628             break;
629         }
630     }
631     return fw_path;
632 }
633 
634 void hmp_info_usb(Monitor *mon, const QDict *qdict)
635 {
636     USBBus *bus;
637     USBDevice *dev;
638     USBPort *port;
639 
640     if (QTAILQ_EMPTY(&busses)) {
641         monitor_printf(mon, "USB support not enabled\n");
642         return;
643     }
644 
645     QTAILQ_FOREACH(bus, &busses, next) {
646         QTAILQ_FOREACH(port, &bus->used, next) {
647             dev = port->dev;
648             if (!dev)
649                 continue;
650             monitor_printf(mon, "  Device %d.%d, Port %s, Speed %s Mb/s, "
651                            "Product %s%s%s\n",
652                            bus->busnr, dev->addr, port->path,
653                            usb_speed(dev->speed), dev->product_desc,
654                            dev->qdev.id ? ", ID: " : "",
655                            dev->qdev.id ?: "");
656         }
657     }
658 }
659 
660 /* handle legacy -usbdevice cmd line option */
661 USBDevice *usbdevice_create(const char *driver)
662 {
663     USBBus *bus = usb_bus_find(-1 /* any */);
664     LegacyUSBFactory *f = NULL;
665     Error *err = NULL;
666     GSList *i;
667     USBDevice *dev;
668 
669     if (strchr(driver, ':')) {
670         error_report("usbdevice parameters are not supported anymore");
671         return NULL;
672     }
673 
674     for (i = legacy_usb_factory; i; i = i->next) {
675         f = i->data;
676         if (strcmp(f->usbdevice_name, driver) == 0) {
677             break;
678         }
679     }
680     if (i == NULL) {
681 #if 0
682         /* no error because some drivers are not converted (yet) */
683         error_report("usbdevice %s not found", driver);
684 #endif
685         return NULL;
686     }
687 
688     if (!bus) {
689         error_report("Error: no usb bus to attach usbdevice %s, "
690                      "please try -machine usb=on and check that "
691                      "the machine model supports USB", driver);
692         return NULL;
693     }
694 
695     dev = f->usbdevice_init ? f->usbdevice_init() : usb_new(f->name);
696     if (!dev) {
697         error_report("Failed to create USB device '%s'", f->name);
698         return NULL;
699     }
700     if (!usb_realize_and_unref(dev, bus, &err)) {
701         error_reportf_err(err, "Failed to initialize USB device '%s': ",
702                           f->name);
703         object_unparent(OBJECT(dev));
704         return NULL;
705     }
706     return dev;
707 }
708 
709 static bool usb_get_attached(Object *obj, Error **errp)
710 {
711     USBDevice *dev = USB_DEVICE(obj);
712 
713     return dev->attached;
714 }
715 
716 static void usb_set_attached(Object *obj, bool value, Error **errp)
717 {
718     USBDevice *dev = USB_DEVICE(obj);
719 
720     if (dev->attached == value) {
721         return;
722     }
723 
724     if (value) {
725         usb_device_attach(dev, errp);
726     } else {
727         usb_device_detach(dev);
728     }
729 }
730 
731 static void usb_device_instance_init(Object *obj)
732 {
733     USBDevice *dev = USB_DEVICE(obj);
734     USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
735 
736     if (klass->attached_settable) {
737         object_property_add_bool(obj, "attached",
738                                  usb_get_attached, usb_set_attached);
739     } else {
740         object_property_add_bool(obj, "attached",
741                                  usb_get_attached, NULL);
742     }
743 }
744 
745 static void usb_device_class_init(ObjectClass *klass, void *data)
746 {
747     DeviceClass *k = DEVICE_CLASS(klass);
748     k->bus_type = TYPE_USB_BUS;
749     k->realize  = usb_qdev_realize;
750     k->unrealize = usb_qdev_unrealize;
751     device_class_set_props(k, usb_props);
752 }
753 
754 static const TypeInfo usb_device_type_info = {
755     .name = TYPE_USB_DEVICE,
756     .parent = TYPE_DEVICE,
757     .instance_size = sizeof(USBDevice),
758     .instance_init = usb_device_instance_init,
759     .abstract = true,
760     .class_size = sizeof(USBDeviceClass),
761     .class_init = usb_device_class_init,
762 };
763 
764 static void usb_register_types(void)
765 {
766     type_register_static(&usb_bus_info);
767     type_register_static(&usb_device_type_info);
768 }
769 
770 type_init(usb_register_types)
771