1 #include "qemu/osdep.h"
2 #include "hw/qdev-properties.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"
11 #include "qemu/cutils.h"
13 static void usb_bus_dev_print(Monitor
*mon
, DeviceState
*qdev
, int indent
);
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
);
19 static Property usb_props
[] = {
20 DEFINE_PROP_STRING("port", USBDevice
, port_path
),
21 DEFINE_PROP_STRING("serial", USBDevice
, serial
),
22 DEFINE_PROP_BIT("full-path", USBDevice
, flags
,
23 USB_DEV_FLAG_FULL_PATH
, true),
24 DEFINE_PROP_BIT("msos-desc", USBDevice
, flags
,
25 USB_DEV_FLAG_MSOS_DESC_ENABLE
, true),
26 DEFINE_PROP_END_OF_LIST()
29 static void usb_bus_class_init(ObjectClass
*klass
, void *data
)
31 BusClass
*k
= BUS_CLASS(klass
);
32 HotplugHandlerClass
*hc
= HOTPLUG_HANDLER_CLASS(klass
);
34 k
->print_dev
= usb_bus_dev_print
;
35 k
->get_dev_path
= usb_get_dev_path
;
36 k
->get_fw_dev_path
= usb_get_fw_dev_path
;
37 hc
->unplug
= qdev_simple_device_unplug_cb
;
40 static const TypeInfo usb_bus_info
= {
43 .instance_size
= sizeof(USBBus
),
44 .class_init
= usb_bus_class_init
,
45 .interfaces
= (InterfaceInfo
[]) {
46 { TYPE_HOTPLUG_HANDLER
},
51 static int next_usb_bus
= 0;
52 static QTAILQ_HEAD(, USBBus
) busses
= QTAILQ_HEAD_INITIALIZER(busses
);
54 static int usb_device_post_load(void *opaque
, int version_id
)
56 USBDevice
*dev
= opaque
;
58 if (dev
->state
== USB_STATE_NOTATTACHED
) {
59 dev
->attached
= false;
66 const VMStateDescription vmstate_usb_device
= {
69 .minimum_version_id
= 1,
70 .post_load
= usb_device_post_load
,
71 .fields
= (VMStateField
[]) {
72 VMSTATE_UINT8(addr
, USBDevice
),
73 VMSTATE_INT32(state
, USBDevice
),
74 VMSTATE_INT32(remote_wakeup
, USBDevice
),
75 VMSTATE_INT32(setup_state
, USBDevice
),
76 VMSTATE_INT32(setup_len
, USBDevice
),
77 VMSTATE_INT32(setup_index
, USBDevice
),
78 VMSTATE_UINT8_ARRAY(setup_buf
, USBDevice
, 8),
79 VMSTATE_END_OF_LIST(),
83 void usb_bus_new(USBBus
*bus
, size_t bus_size
,
84 USBBusOps
*ops
, DeviceState
*host
)
86 qbus_create_inplace(bus
, bus_size
, TYPE_USB_BUS
, host
, NULL
);
87 qbus_set_bus_hotplug_handler(BUS(bus
), &error_abort
);
89 bus
->busnr
= next_usb_bus
++;
90 QTAILQ_INIT(&bus
->free
);
91 QTAILQ_INIT(&bus
->used
);
92 QTAILQ_INSERT_TAIL(&busses
, bus
, next
);
95 void usb_bus_release(USBBus
*bus
)
97 assert(next_usb_bus
> 0);
99 QTAILQ_REMOVE(&busses
, bus
, next
);
102 USBBus
*usb_bus_find(int busnr
)
107 return QTAILQ_FIRST(&busses
);
108 QTAILQ_FOREACH(bus
, &busses
, next
) {
109 if (bus
->busnr
== busnr
)
115 static void usb_device_realize(USBDevice
*dev
, Error
**errp
)
117 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
119 if (klass
->realize
) {
120 klass
->realize(dev
, errp
);
124 USBDevice
*usb_device_find_device(USBDevice
*dev
, uint8_t addr
)
126 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
127 if (klass
->find_device
) {
128 return klass
->find_device(dev
, addr
);
133 static void usb_device_unrealize(USBDevice
*dev
)
135 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
137 if (klass
->unrealize
) {
138 klass
->unrealize(dev
);
142 void usb_device_cancel_packet(USBDevice
*dev
, USBPacket
*p
)
144 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
145 if (klass
->cancel_packet
) {
146 klass
->cancel_packet(dev
, p
);
150 void usb_device_handle_attach(USBDevice
*dev
)
152 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
153 if (klass
->handle_attach
) {
154 klass
->handle_attach(dev
);
158 void usb_device_handle_reset(USBDevice
*dev
)
160 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
161 if (klass
->handle_reset
) {
162 klass
->handle_reset(dev
);
166 void usb_device_handle_control(USBDevice
*dev
, USBPacket
*p
, int request
,
167 int value
, int index
, int length
, uint8_t *data
)
169 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
170 if (klass
->handle_control
) {
171 klass
->handle_control(dev
, p
, request
, value
, index
, length
, data
);
175 void usb_device_handle_data(USBDevice
*dev
, USBPacket
*p
)
177 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
178 if (klass
->handle_data
) {
179 klass
->handle_data(dev
, p
);
183 const char *usb_device_get_product_desc(USBDevice
*dev
)
185 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
186 return klass
->product_desc
;
189 const USBDesc
*usb_device_get_usb_desc(USBDevice
*dev
)
191 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
193 return dev
->usb_desc
;
195 return klass
->usb_desc
;
198 void usb_device_set_interface(USBDevice
*dev
, int interface
,
199 int alt_old
, int alt_new
)
201 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
202 if (klass
->set_interface
) {
203 klass
->set_interface(dev
, interface
, alt_old
, alt_new
);
207 void usb_device_flush_ep_queue(USBDevice
*dev
, USBEndpoint
*ep
)
209 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
210 if (klass
->flush_ep_queue
) {
211 klass
->flush_ep_queue(dev
, ep
);
215 void usb_device_ep_stopped(USBDevice
*dev
, USBEndpoint
*ep
)
217 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
218 if (klass
->ep_stopped
) {
219 klass
->ep_stopped(dev
, ep
);
223 int usb_device_alloc_streams(USBDevice
*dev
, USBEndpoint
**eps
, int nr_eps
,
226 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
227 if (klass
->alloc_streams
) {
228 return klass
->alloc_streams(dev
, eps
, nr_eps
, streams
);
233 void usb_device_free_streams(USBDevice
*dev
, USBEndpoint
**eps
, int nr_eps
)
235 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
236 if (klass
->free_streams
) {
237 klass
->free_streams(dev
, eps
, nr_eps
);
241 static void usb_qdev_realize(DeviceState
*qdev
, Error
**errp
)
243 USBDevice
*dev
= USB_DEVICE(qdev
);
244 Error
*local_err
= NULL
;
246 pstrcpy(dev
->product_desc
, sizeof(dev
->product_desc
),
247 usb_device_get_product_desc(dev
));
248 dev
->auto_attach
= 1;
249 QLIST_INIT(&dev
->strings
);
252 usb_claim_port(dev
, &local_err
);
254 error_propagate(errp
, local_err
);
258 usb_device_realize(dev
, &local_err
);
260 usb_release_port(dev
);
261 error_propagate(errp
, local_err
);
265 if (dev
->auto_attach
) {
266 usb_device_attach(dev
, &local_err
);
268 usb_qdev_unrealize(qdev
);
269 error_propagate(errp
, local_err
);
275 static void usb_qdev_unrealize(DeviceState
*qdev
)
277 USBDevice
*dev
= USB_DEVICE(qdev
);
278 USBDescString
*s
, *next
;
280 QLIST_FOREACH_SAFE(s
, &dev
->strings
, next
, next
) {
281 QLIST_REMOVE(s
, next
);
287 usb_device_detach(dev
);
289 usb_device_unrealize(dev
);
291 usb_release_port(dev
);
295 typedef struct LegacyUSBFactory
298 const char *usbdevice_name
;
299 USBDevice
*(*usbdevice_init
)(const char *params
);
302 static GSList
*legacy_usb_factory
;
304 void usb_legacy_register(const char *typename
, const char *usbdevice_name
,
305 USBDevice
*(*usbdevice_init
)(const char *params
))
307 if (usbdevice_name
) {
308 LegacyUSBFactory
*f
= g_malloc0(sizeof(*f
));
310 f
->usbdevice_name
= usbdevice_name
;
311 f
->usbdevice_init
= usbdevice_init
;
312 legacy_usb_factory
= g_slist_append(legacy_usb_factory
, f
);
316 USBDevice
*usb_new(const char *name
)
318 return USB_DEVICE(qdev_new(name
));
321 static USBDevice
*usb_try_new(const char *name
)
323 return USB_DEVICE(qdev_try_new(name
));
326 bool usb_realize_and_unref(USBDevice
*dev
, USBBus
*bus
, Error
**errp
)
328 return qdev_realize_and_unref(&dev
->qdev
, &bus
->qbus
, errp
);
331 USBDevice
*usb_create_simple(USBBus
*bus
, const char *name
)
333 USBDevice
*dev
= usb_new(name
);
335 usb_realize_and_unref(dev
, bus
, &error_abort
);
339 static void usb_fill_port(USBPort
*port
, void *opaque
, int index
,
340 USBPortOps
*ops
, int speedmask
)
342 port
->opaque
= opaque
;
345 port
->speedmask
= speedmask
;
346 usb_port_location(port
, NULL
, index
+ 1);
349 void usb_register_port(USBBus
*bus
, USBPort
*port
, void *opaque
, int index
,
350 USBPortOps
*ops
, int speedmask
)
352 usb_fill_port(port
, opaque
, index
, ops
, speedmask
);
353 QTAILQ_INSERT_TAIL(&bus
->free
, port
, next
);
357 void usb_register_companion(const char *masterbus
, USBPort
*ports
[],
358 uint32_t portcount
, uint32_t firstport
,
359 void *opaque
, USBPortOps
*ops
, int speedmask
,
365 QTAILQ_FOREACH(bus
, &busses
, next
) {
366 if (strcmp(bus
->qbus
.name
, masterbus
) == 0) {
372 error_setg(errp
, "USB bus '%s' not found", masterbus
);
375 if (!bus
->ops
->register_companion
) {
376 error_setg(errp
, "Can't use USB bus '%s' as masterbus,"
377 " it doesn't support companion controllers",
382 for (i
= 0; i
< portcount
; i
++) {
383 usb_fill_port(ports
[i
], opaque
, i
, ops
, speedmask
);
386 bus
->ops
->register_companion(bus
, ports
, portcount
, firstport
, errp
);
389 void usb_port_location(USBPort
*downstream
, USBPort
*upstream
, int portnr
)
392 int l
= snprintf(downstream
->path
, sizeof(downstream
->path
), "%s.%d",
393 upstream
->path
, portnr
);
394 /* Max string is nn.nn.nn.nn.nn, which fits in 16 bytes */
395 assert(l
< sizeof(downstream
->path
));
396 downstream
->hubcount
= upstream
->hubcount
+ 1;
398 snprintf(downstream
->path
, sizeof(downstream
->path
), "%d", portnr
);
399 downstream
->hubcount
= 0;
403 void usb_unregister_port(USBBus
*bus
, USBPort
*port
)
406 object_unparent(OBJECT(port
->dev
));
408 QTAILQ_REMOVE(&bus
->free
, port
, next
);
412 void usb_claim_port(USBDevice
*dev
, Error
**errp
)
414 USBBus
*bus
= usb_bus_from_device(dev
);
418 assert(dev
->port
== NULL
);
420 if (dev
->port_path
) {
421 QTAILQ_FOREACH(port
, &bus
->free
, next
) {
422 if (strcmp(port
->path
, dev
->port_path
) == 0) {
427 error_setg(errp
, "usb port %s (bus %s) not found (in use?)",
428 dev
->port_path
, bus
->qbus
.name
);
432 if (bus
->nfree
== 1 && strcmp(object_get_typename(OBJECT(dev
)), "usb-hub") != 0) {
433 /* Create a new hub and chain it on */
434 hub
= usb_try_new("usb-hub");
436 usb_realize_and_unref(hub
, bus
, NULL
);
439 if (bus
->nfree
== 0) {
440 error_setg(errp
, "tried to attach usb device %s to a bus "
441 "with no free ports", dev
->product_desc
);
444 port
= QTAILQ_FIRST(&bus
->free
);
446 trace_usb_port_claim(bus
->busnr
, port
->path
);
448 QTAILQ_REMOVE(&bus
->free
, port
, next
);
454 QTAILQ_INSERT_TAIL(&bus
->used
, port
, next
);
458 void usb_release_port(USBDevice
*dev
)
460 USBBus
*bus
= usb_bus_from_device(dev
);
461 USBPort
*port
= dev
->port
;
463 assert(port
!= NULL
);
464 trace_usb_port_release(bus
->busnr
, port
->path
);
466 QTAILQ_REMOVE(&bus
->used
, port
, next
);
472 QTAILQ_INSERT_TAIL(&bus
->free
, port
, next
);
476 static void usb_mask_to_str(char *dest
, size_t size
,
477 unsigned int speedmask
)
479 static const struct {
483 { .mask
= USB_SPEED_MASK_FULL
, .name
= "full" },
484 { .mask
= USB_SPEED_MASK_HIGH
, .name
= "high" },
485 { .mask
= USB_SPEED_MASK_SUPER
, .name
= "super" },
489 for (i
= 0; i
< ARRAY_SIZE(speeds
); i
++) {
490 if (speeds
[i
].mask
& speedmask
) {
491 pos
+= snprintf(dest
+ pos
, size
- pos
, "%s%s",
498 snprintf(dest
, size
, "unknown");
502 void usb_check_attach(USBDevice
*dev
, Error
**errp
)
504 USBBus
*bus
= usb_bus_from_device(dev
);
505 USBPort
*port
= dev
->port
;
506 char devspeed
[32], portspeed
[32];
508 assert(port
!= NULL
);
509 assert(!dev
->attached
);
510 usb_mask_to_str(devspeed
, sizeof(devspeed
), dev
->speedmask
);
511 usb_mask_to_str(portspeed
, sizeof(portspeed
), port
->speedmask
);
512 trace_usb_port_attach(bus
->busnr
, port
->path
,
513 devspeed
, portspeed
);
515 if (!(port
->speedmask
& dev
->speedmask
)) {
516 error_setg(errp
, "Warning: speed mismatch trying to attach"
517 " usb device \"%s\" (%s speed)"
518 " to bus \"%s\", port \"%s\" (%s speed)",
519 dev
->product_desc
, devspeed
,
520 bus
->qbus
.name
, port
->path
, portspeed
);
525 void usb_device_attach(USBDevice
*dev
, Error
**errp
)
527 USBPort
*port
= dev
->port
;
528 Error
*local_err
= NULL
;
530 usb_check_attach(dev
, &local_err
);
532 error_propagate(errp
, local_err
);
536 dev
->attached
= true;
540 int usb_device_detach(USBDevice
*dev
)
542 USBBus
*bus
= usb_bus_from_device(dev
);
543 USBPort
*port
= dev
->port
;
545 assert(port
!= NULL
);
546 assert(dev
->attached
);
547 trace_usb_port_detach(bus
->busnr
, port
->path
);
550 dev
->attached
= false;
554 static const char *usb_speed(unsigned int speed
)
556 static const char *txt
[] = {
557 [ USB_SPEED_LOW
] = "1.5",
558 [ USB_SPEED_FULL
] = "12",
559 [ USB_SPEED_HIGH
] = "480",
560 [ USB_SPEED_SUPER
] = "5000",
562 if (speed
>= ARRAY_SIZE(txt
))
567 static void usb_bus_dev_print(Monitor
*mon
, DeviceState
*qdev
, int indent
)
569 USBDevice
*dev
= USB_DEVICE(qdev
);
570 USBBus
*bus
= usb_bus_from_device(dev
);
572 monitor_printf(mon
, "%*saddr %d.%d, port %s, speed %s, name %s%s\n",
573 indent
, "", bus
->busnr
, dev
->addr
,
574 dev
->port
? dev
->port
->path
: "-",
575 usb_speed(dev
->speed
), dev
->product_desc
,
576 dev
->attached
? ", attached" : "");
579 static char *usb_get_dev_path(DeviceState
*qdev
)
581 USBDevice
*dev
= USB_DEVICE(qdev
);
582 DeviceState
*hcd
= qdev
->parent_bus
->parent
;
585 if (dev
->flags
& (1 << USB_DEV_FLAG_FULL_PATH
)) {
586 id
= qdev_get_dev_path(hcd
);
589 char *ret
= g_strdup_printf("%s/%s", id
, dev
->port
->path
);
593 return g_strdup(dev
->port
->path
);
597 static char *usb_get_fw_dev_path(DeviceState
*qdev
)
599 USBDevice
*dev
= USB_DEVICE(qdev
);
601 ssize_t pos
= 0, fw_len
;
604 fw_len
= 32 + strlen(dev
->port
->path
) * 6;
605 fw_path
= g_malloc(fw_len
);
606 in
= dev
->port
->path
;
607 while (fw_len
- pos
> 0) {
608 nr
= strtol(in
, &in
, 10);
610 /* some hub between root port and device */
611 pos
+= snprintf(fw_path
+ pos
, fw_len
- pos
, "hub@%lx/", nr
);
614 /* the device itself */
615 pos
+= snprintf(fw_path
+ pos
, fw_len
- pos
, "%s@%lx",
616 qdev_fw_name(qdev
), nr
);
623 void hmp_info_usb(Monitor
*mon
, const QDict
*qdict
)
629 if (QTAILQ_EMPTY(&busses
)) {
630 monitor_printf(mon
, "USB support not enabled\n");
634 QTAILQ_FOREACH(bus
, &busses
, next
) {
635 QTAILQ_FOREACH(port
, &bus
->used
, next
) {
639 monitor_printf(mon
, " Device %d.%d, Port %s, Speed %s Mb/s, "
641 bus
->busnr
, dev
->addr
, port
->path
,
642 usb_speed(dev
->speed
), dev
->product_desc
,
643 dev
->qdev
.id
? ", ID: " : "",
649 /* handle legacy -usbdevice cmd line option */
650 USBDevice
*usbdevice_create(const char *cmdline
)
652 USBBus
*bus
= usb_bus_find(-1 /* any */);
653 LegacyUSBFactory
*f
= NULL
;
661 params
= strchr(cmdline
,':');
664 len
= params
- cmdline
;
665 if (len
> sizeof(driver
))
666 len
= sizeof(driver
);
667 pstrcpy(driver
, len
, cmdline
);
670 pstrcpy(driver
, sizeof(driver
), cmdline
);
673 for (i
= legacy_usb_factory
; i
; i
= i
->next
) {
675 if (strcmp(f
->usbdevice_name
, driver
) == 0) {
681 /* no error because some drivers are not converted (yet) */
682 error_report("usbdevice %s not found", driver
);
688 error_report("Error: no usb bus to attach usbdevice %s, "
689 "please try -machine usb=on and check that "
690 "the machine model supports USB", driver
);
694 if (f
->usbdevice_init
) {
695 dev
= f
->usbdevice_init(params
);
698 error_report("usbdevice %s accepts no params", driver
);
701 dev
= usb_new(f
->name
);
704 error_report("Failed to create USB device '%s'", f
->name
);
707 usb_realize_and_unref(dev
, bus
, &err
);
709 error_reportf_err(err
, "Failed to initialize USB device '%s': ",
711 object_unparent(OBJECT(dev
));
717 static bool usb_get_attached(Object
*obj
, Error
**errp
)
719 USBDevice
*dev
= USB_DEVICE(obj
);
721 return dev
->attached
;
724 static void usb_set_attached(Object
*obj
, bool value
, Error
**errp
)
726 USBDevice
*dev
= USB_DEVICE(obj
);
729 if (dev
->attached
== value
) {
734 usb_device_attach(dev
, &err
);
735 error_propagate(errp
, err
);
737 usb_device_detach(dev
);
741 static void usb_device_instance_init(Object
*obj
)
743 USBDevice
*dev
= USB_DEVICE(obj
);
744 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
746 if (klass
->attached_settable
) {
747 object_property_add_bool(obj
, "attached",
748 usb_get_attached
, usb_set_attached
);
750 object_property_add_bool(obj
, "attached",
751 usb_get_attached
, NULL
);
755 static void usb_device_class_init(ObjectClass
*klass
, void *data
)
757 DeviceClass
*k
= DEVICE_CLASS(klass
);
758 k
->bus_type
= TYPE_USB_BUS
;
759 k
->realize
= usb_qdev_realize
;
760 k
->unrealize
= usb_qdev_unrealize
;
761 device_class_set_props(k
, usb_props
);
764 static const TypeInfo usb_device_type_info
= {
765 .name
= TYPE_USB_DEVICE
,
766 .parent
= TYPE_DEVICE
,
767 .instance_size
= sizeof(USBDevice
),
768 .instance_init
= usb_device_instance_init
,
770 .class_size
= sizeof(USBDeviceClass
),
771 .class_init
= usb_device_class_init
,
774 static void usb_register_types(void)
776 type_register_static(&usb_bus_info
);
777 type_register_static(&usb_device_type_info
);
780 type_init(usb_register_types
)