1 #include "qemu/osdep.h"
5 #include "qapi/error.h"
6 #include "qemu/error-report.h"
7 #include "sysemu/sysemu.h"
8 #include "monitor/monitor.h"
10 #include "qemu/cutils.h"
12 static void usb_bus_dev_print(Monitor
*mon
, DeviceState
*qdev
, int indent
);
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
);
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()
28 static void usb_bus_class_init(ObjectClass
*klass
, void *data
)
30 BusClass
*k
= BUS_CLASS(klass
);
31 HotplugHandlerClass
*hc
= HOTPLUG_HANDLER_CLASS(klass
);
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
;
39 static const TypeInfo usb_bus_info
= {
42 .instance_size
= sizeof(USBBus
),
43 .class_init
= usb_bus_class_init
,
44 .interfaces
= (InterfaceInfo
[]) {
45 { TYPE_HOTPLUG_HANDLER
},
50 static int next_usb_bus
= 0;
51 static QTAILQ_HEAD(, USBBus
) busses
= QTAILQ_HEAD_INITIALIZER(busses
);
53 static int usb_device_post_load(void *opaque
, int version_id
)
55 USBDevice
*dev
= opaque
;
57 if (dev
->state
== USB_STATE_NOTATTACHED
) {
58 dev
->attached
= false;
65 const VMStateDescription vmstate_usb_device
= {
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(),
82 void usb_bus_new(USBBus
*bus
, size_t bus_size
,
83 USBBusOps
*ops
, DeviceState
*host
)
85 qbus_create_inplace(bus
, bus_size
, TYPE_USB_BUS
, host
, NULL
);
86 qbus_set_bus_hotplug_handler(BUS(bus
), &error_abort
);
88 bus
->busnr
= next_usb_bus
++;
89 QTAILQ_INIT(&bus
->free
);
90 QTAILQ_INIT(&bus
->used
);
91 QTAILQ_INSERT_TAIL(&busses
, bus
, next
);
94 void usb_bus_release(USBBus
*bus
)
96 assert(next_usb_bus
> 0);
98 QTAILQ_REMOVE(&busses
, bus
, next
);
101 USBBus
*usb_bus_find(int busnr
)
106 return QTAILQ_FIRST(&busses
);
107 QTAILQ_FOREACH(bus
, &busses
, next
) {
108 if (bus
->busnr
== busnr
)
114 static void usb_device_realize(USBDevice
*dev
, Error
**errp
)
116 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
118 if (klass
->realize
) {
119 klass
->realize(dev
, errp
);
123 USBDevice
*usb_device_find_device(USBDevice
*dev
, uint8_t addr
)
125 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
126 if (klass
->find_device
) {
127 return klass
->find_device(dev
, addr
);
132 static void usb_device_unrealize(USBDevice
*dev
, Error
**errp
)
134 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
136 if (klass
->unrealize
) {
137 klass
->unrealize(dev
, errp
);
141 void usb_device_cancel_packet(USBDevice
*dev
, USBPacket
*p
)
143 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
144 if (klass
->cancel_packet
) {
145 klass
->cancel_packet(dev
, p
);
149 void usb_device_handle_attach(USBDevice
*dev
)
151 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
152 if (klass
->handle_attach
) {
153 klass
->handle_attach(dev
);
157 void usb_device_handle_reset(USBDevice
*dev
)
159 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
160 if (klass
->handle_reset
) {
161 klass
->handle_reset(dev
);
165 void usb_device_handle_control(USBDevice
*dev
, USBPacket
*p
, int request
,
166 int value
, int index
, int length
, uint8_t *data
)
168 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
169 if (klass
->handle_control
) {
170 klass
->handle_control(dev
, p
, request
, value
, index
, length
, data
);
174 void usb_device_handle_data(USBDevice
*dev
, USBPacket
*p
)
176 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
177 if (klass
->handle_data
) {
178 klass
->handle_data(dev
, p
);
182 const char *usb_device_get_product_desc(USBDevice
*dev
)
184 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
185 return klass
->product_desc
;
188 const USBDesc
*usb_device_get_usb_desc(USBDevice
*dev
)
190 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
192 return dev
->usb_desc
;
194 return klass
->usb_desc
;
197 void usb_device_set_interface(USBDevice
*dev
, int interface
,
198 int alt_old
, int alt_new
)
200 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
201 if (klass
->set_interface
) {
202 klass
->set_interface(dev
, interface
, alt_old
, alt_new
);
206 void usb_device_flush_ep_queue(USBDevice
*dev
, USBEndpoint
*ep
)
208 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
209 if (klass
->flush_ep_queue
) {
210 klass
->flush_ep_queue(dev
, ep
);
214 void usb_device_ep_stopped(USBDevice
*dev
, USBEndpoint
*ep
)
216 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
217 if (klass
->ep_stopped
) {
218 klass
->ep_stopped(dev
, ep
);
222 int usb_device_alloc_streams(USBDevice
*dev
, USBEndpoint
**eps
, int nr_eps
,
225 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
226 if (klass
->alloc_streams
) {
227 return klass
->alloc_streams(dev
, eps
, nr_eps
, streams
);
232 void usb_device_free_streams(USBDevice
*dev
, USBEndpoint
**eps
, int nr_eps
)
234 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
235 if (klass
->free_streams
) {
236 klass
->free_streams(dev
, eps
, nr_eps
);
240 static void usb_qdev_realize(DeviceState
*qdev
, Error
**errp
)
242 USBDevice
*dev
= USB_DEVICE(qdev
);
243 Error
*local_err
= NULL
;
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
);
251 usb_claim_port(dev
, &local_err
);
253 error_propagate(errp
, local_err
);
257 usb_device_realize(dev
, &local_err
);
259 usb_release_port(dev
);
260 error_propagate(errp
, local_err
);
264 if (dev
->auto_attach
) {
265 usb_device_attach(dev
, &local_err
);
267 usb_qdev_unrealize(qdev
, NULL
);
268 error_propagate(errp
, local_err
);
274 static void usb_qdev_unrealize(DeviceState
*qdev
, Error
**errp
)
276 USBDevice
*dev
= USB_DEVICE(qdev
);
277 USBDescString
*s
, *next
;
279 QLIST_FOREACH_SAFE(s
, &dev
->strings
, next
, next
) {
280 QLIST_REMOVE(s
, next
);
286 usb_device_detach(dev
);
288 usb_device_unrealize(dev
, errp
);
290 usb_release_port(dev
);
294 typedef struct LegacyUSBFactory
297 const char *usbdevice_name
;
298 USBDevice
*(*usbdevice_init
)(USBBus
*bus
, const char *params
);
301 static GSList
*legacy_usb_factory
;
303 void usb_legacy_register(const char *typename
, const char *usbdevice_name
,
304 USBDevice
*(*usbdevice_init
)(USBBus
*bus
,
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_create(USBBus
*bus
, const char *name
)
320 dev
= qdev_create(&bus
->qbus
, name
);
321 return USB_DEVICE(dev
);
324 static USBDevice
*usb_try_create_simple(USBBus
*bus
, const char *name
,
330 dev
= USB_DEVICE(qdev_try_create(&bus
->qbus
, name
));
332 error_setg(errp
, "Failed to create USB device '%s'", name
);
335 object_property_set_bool(OBJECT(dev
), true, "realized", &err
);
337 error_propagate_prepend(errp
, err
,
338 "Failed to initialize USB device '%s': ",
345 USBDevice
*usb_create_simple(USBBus
*bus
, const char *name
)
347 return usb_try_create_simple(bus
, name
, &error_abort
);
350 static void usb_fill_port(USBPort
*port
, void *opaque
, int index
,
351 USBPortOps
*ops
, int speedmask
)
353 port
->opaque
= opaque
;
356 port
->speedmask
= speedmask
;
357 usb_port_location(port
, NULL
, index
+ 1);
360 void usb_register_port(USBBus
*bus
, USBPort
*port
, void *opaque
, int index
,
361 USBPortOps
*ops
, int speedmask
)
363 usb_fill_port(port
, opaque
, index
, ops
, speedmask
);
364 QTAILQ_INSERT_TAIL(&bus
->free
, port
, next
);
368 void usb_register_companion(const char *masterbus
, USBPort
*ports
[],
369 uint32_t portcount
, uint32_t firstport
,
370 void *opaque
, USBPortOps
*ops
, int speedmask
,
376 QTAILQ_FOREACH(bus
, &busses
, next
) {
377 if (strcmp(bus
->qbus
.name
, masterbus
) == 0) {
383 error_setg(errp
, "USB bus '%s' not found", masterbus
);
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",
393 for (i
= 0; i
< portcount
; i
++) {
394 usb_fill_port(ports
[i
], opaque
, i
, ops
, speedmask
);
397 bus
->ops
->register_companion(bus
, ports
, portcount
, firstport
, errp
);
400 void usb_port_location(USBPort
*downstream
, USBPort
*upstream
, int portnr
)
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;
409 snprintf(downstream
->path
, sizeof(downstream
->path
), "%d", portnr
);
410 downstream
->hubcount
= 0;
414 void usb_unregister_port(USBBus
*bus
, USBPort
*port
)
417 object_unparent(OBJECT(port
->dev
));
419 QTAILQ_REMOVE(&bus
->free
, port
, next
);
423 void usb_claim_port(USBDevice
*dev
, Error
**errp
)
425 USBBus
*bus
= usb_bus_from_device(dev
);
428 assert(dev
->port
== NULL
);
430 if (dev
->port_path
) {
431 QTAILQ_FOREACH(port
, &bus
->free
, next
) {
432 if (strcmp(port
->path
, dev
->port_path
) == 0) {
437 error_setg(errp
, "usb port %s (bus %s) not found (in use?)",
438 dev
->port_path
, bus
->qbus
.name
);
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
);
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
);
451 port
= QTAILQ_FIRST(&bus
->free
);
453 trace_usb_port_claim(bus
->busnr
, port
->path
);
455 QTAILQ_REMOVE(&bus
->free
, port
, next
);
461 QTAILQ_INSERT_TAIL(&bus
->used
, port
, next
);
465 void usb_release_port(USBDevice
*dev
)
467 USBBus
*bus
= usb_bus_from_device(dev
);
468 USBPort
*port
= dev
->port
;
470 assert(port
!= NULL
);
471 trace_usb_port_release(bus
->busnr
, port
->path
);
473 QTAILQ_REMOVE(&bus
->used
, port
, next
);
479 QTAILQ_INSERT_TAIL(&bus
->free
, port
, next
);
483 static void usb_mask_to_str(char *dest
, size_t size
,
484 unsigned int speedmask
)
486 static const struct {
490 { .mask
= USB_SPEED_MASK_FULL
, .name
= "full" },
491 { .mask
= USB_SPEED_MASK_HIGH
, .name
= "high" },
492 { .mask
= USB_SPEED_MASK_SUPER
, .name
= "super" },
496 for (i
= 0; i
< ARRAY_SIZE(speeds
); i
++) {
497 if (speeds
[i
].mask
& speedmask
) {
498 pos
+= snprintf(dest
+ pos
, size
- pos
, "%s%s",
505 void usb_check_attach(USBDevice
*dev
, Error
**errp
)
507 USBBus
*bus
= usb_bus_from_device(dev
);
508 USBPort
*port
= dev
->port
;
509 char devspeed
[32], portspeed
[32];
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
);
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
);
528 void usb_device_attach(USBDevice
*dev
, Error
**errp
)
530 USBPort
*port
= dev
->port
;
531 Error
*local_err
= NULL
;
533 usb_check_attach(dev
, &local_err
);
535 error_propagate(errp
, local_err
);
539 dev
->attached
= true;
543 int usb_device_detach(USBDevice
*dev
)
545 USBBus
*bus
= usb_bus_from_device(dev
);
546 USBPort
*port
= dev
->port
;
548 assert(port
!= NULL
);
549 assert(dev
->attached
);
550 trace_usb_port_detach(bus
->busnr
, port
->path
);
553 dev
->attached
= false;
557 static const char *usb_speed(unsigned int speed
)
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",
565 if (speed
>= ARRAY_SIZE(txt
))
570 static void usb_bus_dev_print(Monitor
*mon
, DeviceState
*qdev
, int indent
)
572 USBDevice
*dev
= USB_DEVICE(qdev
);
573 USBBus
*bus
= usb_bus_from_device(dev
);
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" : "");
582 static char *usb_get_dev_path(DeviceState
*qdev
)
584 USBDevice
*dev
= USB_DEVICE(qdev
);
585 DeviceState
*hcd
= qdev
->parent_bus
->parent
;
588 if (dev
->flags
& (1 << USB_DEV_FLAG_FULL_PATH
)) {
589 id
= qdev_get_dev_path(hcd
);
592 char *ret
= g_strdup_printf("%s/%s", id
, dev
->port
->path
);
596 return g_strdup(dev
->port
->path
);
600 static char *usb_get_fw_dev_path(DeviceState
*qdev
)
602 USBDevice
*dev
= USB_DEVICE(qdev
);
604 ssize_t pos
= 0, fw_len
;
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);
613 /* some hub between root port and device */
614 pos
+= snprintf(fw_path
+ pos
, fw_len
- pos
, "hub@%lx/", nr
);
617 /* the device itself */
618 pos
+= snprintf(fw_path
+ pos
, fw_len
- pos
, "%s@%lx",
619 qdev_fw_name(qdev
), nr
);
626 void hmp_info_usb(Monitor
*mon
, const QDict
*qdict
)
632 if (QTAILQ_EMPTY(&busses
)) {
633 monitor_printf(mon
, "USB support not enabled\n");
637 QTAILQ_FOREACH(bus
, &busses
, next
) {
638 QTAILQ_FOREACH(port
, &bus
->used
, next
) {
642 monitor_printf(mon
, " Device %d.%d, Port %s, Speed %s Mb/s, "
644 bus
->busnr
, dev
->addr
, port
->path
,
645 usb_speed(dev
->speed
), dev
->product_desc
,
646 dev
->qdev
.id
? ", ID: " : "",
652 /* handle legacy -usbdevice cmd line option */
653 USBDevice
*usbdevice_create(const char *cmdline
)
655 USBBus
*bus
= usb_bus_find(-1 /* any */);
656 LegacyUSBFactory
*f
= NULL
;
664 params
= strchr(cmdline
,':');
667 len
= params
- cmdline
;
668 if (len
> sizeof(driver
))
669 len
= sizeof(driver
);
670 pstrcpy(driver
, len
, cmdline
);
673 pstrcpy(driver
, sizeof(driver
), cmdline
);
676 for (i
= legacy_usb_factory
; i
; i
= i
->next
) {
678 if (strcmp(f
->usbdevice_name
, driver
) == 0) {
684 /* no error because some drivers are not converted (yet) */
685 error_report("usbdevice %s not found", driver
);
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
);
697 if (f
->usbdevice_init
) {
698 dev
= f
->usbdevice_init(bus
, params
);
701 error_report("usbdevice %s accepts no params", driver
);
704 dev
= usb_create(bus
, f
->name
);
707 error_report("Failed to create USB device '%s'", f
->name
);
710 object_property_set_bool(OBJECT(dev
), true, "realized", &err
);
712 error_reportf_err(err
, "Failed to initialize USB device '%s': ",
714 object_unparent(OBJECT(dev
));
720 static bool usb_get_attached(Object
*obj
, Error
**errp
)
722 USBDevice
*dev
= USB_DEVICE(obj
);
724 return dev
->attached
;
727 static void usb_set_attached(Object
*obj
, bool value
, Error
**errp
)
729 USBDevice
*dev
= USB_DEVICE(obj
);
732 if (dev
->attached
== value
) {
737 usb_device_attach(dev
, &err
);
738 error_propagate(errp
, err
);
740 usb_device_detach(dev
);
744 static void usb_device_instance_init(Object
*obj
)
746 USBDevice
*dev
= USB_DEVICE(obj
);
747 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
749 if (klass
->attached_settable
) {
750 object_property_add_bool(obj
, "attached",
751 usb_get_attached
, usb_set_attached
,
754 object_property_add_bool(obj
, "attached",
755 usb_get_attached
, NULL
,
760 static void usb_device_class_init(ObjectClass
*klass
, void *data
)
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
;
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
,
775 .class_size
= sizeof(USBDeviceClass
),
776 .class_init
= usb_device_class_init
,
779 static void usb_register_types(void)
781 type_register_static(&usb_bus_info
);
782 type_register_static(&usb_device_type_info
);
785 type_init(usb_register_types
)