4 #include "qemu/error-report.h"
5 #include "sysemu/sysemu.h"
6 #include "monitor/monitor.h"
9 static void usb_bus_dev_print(Monitor
*mon
, DeviceState
*qdev
, int indent
);
11 static char *usb_get_dev_path(DeviceState
*dev
);
12 static char *usb_get_fw_dev_path(DeviceState
*qdev
);
13 static void usb_qdev_unrealize(DeviceState
*qdev
, Error
**errp
);
15 static Property usb_props
[] = {
16 DEFINE_PROP_STRING("port", USBDevice
, port_path
),
17 DEFINE_PROP_STRING("serial", USBDevice
, serial
),
18 DEFINE_PROP_BIT("full-path", USBDevice
, flags
,
19 USB_DEV_FLAG_FULL_PATH
, true),
20 DEFINE_PROP_BIT("msos-desc", USBDevice
, flags
,
21 USB_DEV_FLAG_MSOS_DESC_ENABLE
, true),
22 DEFINE_PROP_END_OF_LIST()
25 static void usb_bus_class_init(ObjectClass
*klass
, void *data
)
27 BusClass
*k
= BUS_CLASS(klass
);
28 HotplugHandlerClass
*hc
= HOTPLUG_HANDLER_CLASS(klass
);
30 k
->print_dev
= usb_bus_dev_print
;
31 k
->get_dev_path
= usb_get_dev_path
;
32 k
->get_fw_dev_path
= usb_get_fw_dev_path
;
33 hc
->unplug
= qdev_simple_device_unplug_cb
;
36 static const TypeInfo usb_bus_info
= {
39 .instance_size
= sizeof(USBBus
),
40 .class_init
= usb_bus_class_init
,
41 .interfaces
= (InterfaceInfo
[]) {
42 { TYPE_HOTPLUG_HANDLER
},
47 static int next_usb_bus
= 0;
48 static QTAILQ_HEAD(, USBBus
) busses
= QTAILQ_HEAD_INITIALIZER(busses
);
50 static int usb_device_post_load(void *opaque
, int version_id
)
52 USBDevice
*dev
= opaque
;
54 if (dev
->state
== USB_STATE_NOTATTACHED
) {
59 if (dev
->setup_index
< 0 ||
61 dev
->setup_index
> dev
->setup_len
||
62 dev
->setup_len
> sizeof(dev
->data_buf
)) {
68 const VMStateDescription vmstate_usb_device
= {
71 .minimum_version_id
= 1,
72 .post_load
= usb_device_post_load
,
73 .fields
= (VMStateField
[]) {
74 VMSTATE_UINT8(addr
, USBDevice
),
75 VMSTATE_INT32(state
, USBDevice
),
76 VMSTATE_INT32(remote_wakeup
, USBDevice
),
77 VMSTATE_INT32(setup_state
, USBDevice
),
78 VMSTATE_INT32(setup_len
, USBDevice
),
79 VMSTATE_INT32(setup_index
, USBDevice
),
80 VMSTATE_UINT8_ARRAY(setup_buf
, USBDevice
, 8),
81 VMSTATE_END_OF_LIST(),
85 void usb_bus_new(USBBus
*bus
, size_t bus_size
,
86 USBBusOps
*ops
, DeviceState
*host
)
88 qbus_create_inplace(bus
, bus_size
, TYPE_USB_BUS
, host
, NULL
);
89 qbus_set_bus_hotplug_handler(BUS(bus
), &error_abort
);
91 bus
->busnr
= next_usb_bus
++;
92 QTAILQ_INIT(&bus
->free
);
93 QTAILQ_INIT(&bus
->used
);
94 QTAILQ_INSERT_TAIL(&busses
, bus
, next
);
97 void usb_bus_release(USBBus
*bus
)
99 assert(next_usb_bus
> 0);
101 QTAILQ_REMOVE(&busses
, bus
, next
);
104 USBBus
*usb_bus_find(int busnr
)
109 return QTAILQ_FIRST(&busses
);
110 QTAILQ_FOREACH(bus
, &busses
, next
) {
111 if (bus
->busnr
== busnr
)
117 static void usb_device_realize(USBDevice
*dev
, Error
**errp
)
119 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
121 if (klass
->realize
) {
122 klass
->realize(dev
, errp
);
126 USBDevice
*usb_device_find_device(USBDevice
*dev
, uint8_t addr
)
128 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
129 if (klass
->find_device
) {
130 return klass
->find_device(dev
, addr
);
135 static void usb_device_handle_destroy(USBDevice
*dev
)
137 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
138 if (klass
->handle_destroy
) {
139 klass
->handle_destroy(dev
);
143 void usb_device_cancel_packet(USBDevice
*dev
, USBPacket
*p
)
145 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
146 if (klass
->cancel_packet
) {
147 klass
->cancel_packet(dev
, p
);
151 void usb_device_handle_attach(USBDevice
*dev
)
153 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
154 if (klass
->handle_attach
) {
155 klass
->handle_attach(dev
);
159 void usb_device_handle_reset(USBDevice
*dev
)
161 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
162 if (klass
->handle_reset
) {
163 klass
->handle_reset(dev
);
167 void usb_device_handle_control(USBDevice
*dev
, USBPacket
*p
, int request
,
168 int value
, int index
, int length
, uint8_t *data
)
170 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
171 if (klass
->handle_control
) {
172 klass
->handle_control(dev
, p
, request
, value
, index
, length
, data
);
176 void usb_device_handle_data(USBDevice
*dev
, USBPacket
*p
)
178 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
179 if (klass
->handle_data
) {
180 klass
->handle_data(dev
, p
);
184 const char *usb_device_get_product_desc(USBDevice
*dev
)
186 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
187 return klass
->product_desc
;
190 const USBDesc
*usb_device_get_usb_desc(USBDevice
*dev
)
192 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
194 return dev
->usb_desc
;
196 return klass
->usb_desc
;
199 void usb_device_set_interface(USBDevice
*dev
, int interface
,
200 int alt_old
, int alt_new
)
202 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
203 if (klass
->set_interface
) {
204 klass
->set_interface(dev
, interface
, alt_old
, alt_new
);
208 void usb_device_flush_ep_queue(USBDevice
*dev
, USBEndpoint
*ep
)
210 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
211 if (klass
->flush_ep_queue
) {
212 klass
->flush_ep_queue(dev
, ep
);
216 void usb_device_ep_stopped(USBDevice
*dev
, USBEndpoint
*ep
)
218 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
219 if (klass
->ep_stopped
) {
220 klass
->ep_stopped(dev
, ep
);
224 int usb_device_alloc_streams(USBDevice
*dev
, USBEndpoint
**eps
, int nr_eps
,
227 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
228 if (klass
->alloc_streams
) {
229 return klass
->alloc_streams(dev
, eps
, nr_eps
, streams
);
234 void usb_device_free_streams(USBDevice
*dev
, USBEndpoint
**eps
, int nr_eps
)
236 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
237 if (klass
->free_streams
) {
238 klass
->free_streams(dev
, eps
, nr_eps
);
242 static void usb_qdev_realize(DeviceState
*qdev
, Error
**errp
)
244 USBDevice
*dev
= USB_DEVICE(qdev
);
245 Error
*local_err
= NULL
;
247 pstrcpy(dev
->product_desc
, sizeof(dev
->product_desc
),
248 usb_device_get_product_desc(dev
));
249 dev
->auto_attach
= 1;
250 QLIST_INIT(&dev
->strings
);
253 usb_claim_port(dev
, &local_err
);
255 error_propagate(errp
, local_err
);
259 usb_device_realize(dev
, &local_err
);
261 usb_release_port(dev
);
262 error_propagate(errp
, local_err
);
266 if (dev
->auto_attach
) {
267 usb_device_attach(dev
, &local_err
);
269 usb_qdev_unrealize(qdev
, NULL
);
270 error_propagate(errp
, local_err
);
276 static void usb_qdev_unrealize(DeviceState
*qdev
, Error
**errp
)
278 USBDevice
*dev
= USB_DEVICE(qdev
);
281 usb_device_detach(dev
);
283 usb_device_handle_destroy(dev
);
285 usb_release_port(dev
);
289 typedef struct LegacyUSBFactory
292 const char *usbdevice_name
;
293 USBDevice
*(*usbdevice_init
)(USBBus
*bus
, const char *params
);
296 static GSList
*legacy_usb_factory
;
298 void usb_legacy_register(const char *typename
, const char *usbdevice_name
,
299 USBDevice
*(*usbdevice_init
)(USBBus
*bus
,
302 if (usbdevice_name
) {
303 LegacyUSBFactory
*f
= g_malloc0(sizeof(*f
));
305 f
->usbdevice_name
= usbdevice_name
;
306 f
->usbdevice_init
= usbdevice_init
;
307 legacy_usb_factory
= g_slist_append(legacy_usb_factory
, f
);
311 USBDevice
*usb_create(USBBus
*bus
, const char *name
)
315 dev
= qdev_create(&bus
->qbus
, name
);
316 return USB_DEVICE(dev
);
319 static USBDevice
*usb_try_create_simple(USBBus
*bus
, const char *name
,
325 dev
= USB_DEVICE(qdev_try_create(&bus
->qbus
, name
));
327 error_setg(errp
, "Failed to create USB device '%s'", name
);
330 object_property_set_bool(OBJECT(dev
), true, "realized", &err
);
332 error_setg(errp
, "Failed to initialize USB device '%s': %s",
333 name
, error_get_pretty(err
));
335 object_unparent(OBJECT(dev
));
341 USBDevice
*usb_create_simple(USBBus
*bus
, const char *name
)
343 return usb_try_create_simple(bus
, name
, &error_abort
);
346 static void usb_fill_port(USBPort
*port
, void *opaque
, int index
,
347 USBPortOps
*ops
, int speedmask
)
349 port
->opaque
= opaque
;
352 port
->speedmask
= speedmask
;
353 usb_port_location(port
, NULL
, index
+ 1);
356 void usb_register_port(USBBus
*bus
, USBPort
*port
, void *opaque
, int index
,
357 USBPortOps
*ops
, int speedmask
)
359 usb_fill_port(port
, opaque
, index
, ops
, speedmask
);
360 QTAILQ_INSERT_TAIL(&bus
->free
, port
, next
);
364 void usb_register_companion(const char *masterbus
, USBPort
*ports
[],
365 uint32_t portcount
, uint32_t firstport
,
366 void *opaque
, USBPortOps
*ops
, int speedmask
,
372 QTAILQ_FOREACH(bus
, &busses
, next
) {
373 if (strcmp(bus
->qbus
.name
, masterbus
) == 0) {
379 error_setg(errp
, "USB bus '%s' not found", masterbus
);
382 if (!bus
->ops
->register_companion
) {
383 error_setg(errp
, "Can't use USB bus '%s' as masterbus,"
384 " it doesn't support companion controllers",
389 for (i
= 0; i
< portcount
; i
++) {
390 usb_fill_port(ports
[i
], opaque
, i
, ops
, speedmask
);
393 bus
->ops
->register_companion(bus
, ports
, portcount
, firstport
, errp
);
396 void usb_port_location(USBPort
*downstream
, USBPort
*upstream
, int portnr
)
399 snprintf(downstream
->path
, sizeof(downstream
->path
), "%s.%d",
400 upstream
->path
, portnr
);
401 downstream
->hubcount
= upstream
->hubcount
+ 1;
403 snprintf(downstream
->path
, sizeof(downstream
->path
), "%d", portnr
);
404 downstream
->hubcount
= 0;
408 void usb_unregister_port(USBBus
*bus
, USBPort
*port
)
411 object_unparent(OBJECT(port
->dev
));
413 QTAILQ_REMOVE(&bus
->free
, port
, next
);
417 void usb_claim_port(USBDevice
*dev
, Error
**errp
)
419 USBBus
*bus
= usb_bus_from_device(dev
);
422 assert(dev
->port
== NULL
);
424 if (dev
->port_path
) {
425 QTAILQ_FOREACH(port
, &bus
->free
, next
) {
426 if (strcmp(port
->path
, dev
->port_path
) == 0) {
431 error_setg(errp
, "usb port %s (bus %s) not found (in use?)",
432 dev
->port_path
, bus
->qbus
.name
);
436 if (bus
->nfree
== 1 && strcmp(object_get_typename(OBJECT(dev
)), "usb-hub") != 0) {
437 /* Create a new hub and chain it on */
438 usb_try_create_simple(bus
, "usb-hub", NULL
);
440 if (bus
->nfree
== 0) {
441 error_setg(errp
, "tried to attach usb device %s to a bus "
442 "with no free ports", dev
->product_desc
);
445 port
= QTAILQ_FIRST(&bus
->free
);
447 trace_usb_port_claim(bus
->busnr
, port
->path
);
449 QTAILQ_REMOVE(&bus
->free
, port
, next
);
455 QTAILQ_INSERT_TAIL(&bus
->used
, port
, next
);
459 void usb_release_port(USBDevice
*dev
)
461 USBBus
*bus
= usb_bus_from_device(dev
);
462 USBPort
*port
= dev
->port
;
464 assert(port
!= NULL
);
465 trace_usb_port_release(bus
->busnr
, port
->path
);
467 QTAILQ_REMOVE(&bus
->used
, port
, next
);
473 QTAILQ_INSERT_TAIL(&bus
->free
, port
, next
);
477 static void usb_mask_to_str(char *dest
, size_t size
,
478 unsigned int speedmask
)
480 static const struct {
484 { .mask
= USB_SPEED_MASK_FULL
, .name
= "full" },
485 { .mask
= USB_SPEED_MASK_HIGH
, .name
= "high" },
486 { .mask
= USB_SPEED_MASK_SUPER
, .name
= "super" },
490 for (i
= 0; i
< ARRAY_SIZE(speeds
); i
++) {
491 if (speeds
[i
].mask
& speedmask
) {
492 pos
+= snprintf(dest
+ pos
, size
- pos
, "%s%s",
499 void usb_check_attach(USBDevice
*dev
, Error
**errp
)
501 USBBus
*bus
= usb_bus_from_device(dev
);
502 USBPort
*port
= dev
->port
;
503 char devspeed
[32], portspeed
[32];
505 assert(port
!= NULL
);
506 assert(!dev
->attached
);
507 usb_mask_to_str(devspeed
, sizeof(devspeed
), dev
->speedmask
);
508 usb_mask_to_str(portspeed
, sizeof(portspeed
), port
->speedmask
);
509 trace_usb_port_attach(bus
->busnr
, port
->path
,
510 devspeed
, portspeed
);
512 if (!(port
->speedmask
& dev
->speedmask
)) {
513 error_setg(errp
, "Warning: speed mismatch trying to attach"
514 " usb device \"%s\" (%s speed)"
515 " to bus \"%s\", port \"%s\" (%s speed)",
516 dev
->product_desc
, devspeed
,
517 bus
->qbus
.name
, port
->path
, portspeed
);
522 void usb_device_attach(USBDevice
*dev
, Error
**errp
)
524 USBPort
*port
= dev
->port
;
525 Error
*local_err
= NULL
;
527 usb_check_attach(dev
, &local_err
);
529 error_propagate(errp
, local_err
);
537 int usb_device_detach(USBDevice
*dev
)
539 USBBus
*bus
= usb_bus_from_device(dev
);
540 USBPort
*port
= dev
->port
;
542 assert(port
!= NULL
);
543 assert(dev
->attached
);
544 trace_usb_port_detach(bus
->busnr
, port
->path
);
551 int usb_device_delete_addr(int busnr
, int addr
)
557 bus
= usb_bus_find(busnr
);
561 QTAILQ_FOREACH(port
, &bus
->used
, next
) {
562 if (port
->dev
->addr
== addr
)
569 object_unparent(OBJECT(dev
));
573 static const char *usb_speed(unsigned int speed
)
575 static const char *txt
[] = {
576 [ USB_SPEED_LOW
] = "1.5",
577 [ USB_SPEED_FULL
] = "12",
578 [ USB_SPEED_HIGH
] = "480",
579 [ USB_SPEED_SUPER
] = "5000",
581 if (speed
>= ARRAY_SIZE(txt
))
586 static void usb_bus_dev_print(Monitor
*mon
, DeviceState
*qdev
, int indent
)
588 USBDevice
*dev
= USB_DEVICE(qdev
);
589 USBBus
*bus
= usb_bus_from_device(dev
);
591 monitor_printf(mon
, "%*saddr %d.%d, port %s, speed %s, name %s%s\n",
592 indent
, "", bus
->busnr
, dev
->addr
,
593 dev
->port
? dev
->port
->path
: "-",
594 usb_speed(dev
->speed
), dev
->product_desc
,
595 dev
->attached
? ", attached" : "");
598 static char *usb_get_dev_path(DeviceState
*qdev
)
600 USBDevice
*dev
= USB_DEVICE(qdev
);
601 DeviceState
*hcd
= qdev
->parent_bus
->parent
;
604 if (dev
->flags
& (1 << USB_DEV_FLAG_FULL_PATH
)) {
605 id
= qdev_get_dev_path(hcd
);
608 char *ret
= g_strdup_printf("%s/%s", id
, dev
->port
->path
);
612 return g_strdup(dev
->port
->path
);
616 static char *usb_get_fw_dev_path(DeviceState
*qdev
)
618 USBDevice
*dev
= USB_DEVICE(qdev
);
620 ssize_t pos
= 0, fw_len
;
623 fw_len
= 32 + strlen(dev
->port
->path
) * 6;
624 fw_path
= g_malloc(fw_len
);
625 in
= dev
->port
->path
;
626 while (fw_len
- pos
> 0) {
627 nr
= strtol(in
, &in
, 10);
629 /* some hub between root port and device */
630 pos
+= snprintf(fw_path
+ pos
, fw_len
- pos
, "hub@%lx/", nr
);
633 /* the device itself */
634 pos
+= snprintf(fw_path
+ pos
, fw_len
- pos
, "%s@%lx",
635 qdev_fw_name(qdev
), nr
);
642 void hmp_info_usb(Monitor
*mon
, const QDict
*qdict
)
648 if (QTAILQ_EMPTY(&busses
)) {
649 monitor_printf(mon
, "USB support not enabled\n");
653 QTAILQ_FOREACH(bus
, &busses
, next
) {
654 QTAILQ_FOREACH(port
, &bus
->used
, next
) {
658 monitor_printf(mon
, " Device %d.%d, Port %s, Speed %s Mb/s, Product %s\n",
659 bus
->busnr
, dev
->addr
, port
->path
, usb_speed(dev
->speed
),
665 /* handle legacy -usbdevice cmd line option */
666 USBDevice
*usbdevice_create(const char *cmdline
)
668 USBBus
*bus
= usb_bus_find(-1 /* any */);
669 LegacyUSBFactory
*f
= NULL
;
677 params
= strchr(cmdline
,':');
680 len
= params
- cmdline
;
681 if (len
> sizeof(driver
))
682 len
= sizeof(driver
);
683 pstrcpy(driver
, len
, cmdline
);
686 pstrcpy(driver
, sizeof(driver
), cmdline
);
689 for (i
= legacy_usb_factory
; i
; i
= i
->next
) {
691 if (strcmp(f
->usbdevice_name
, driver
) == 0) {
697 /* no error because some drivers are not converted (yet) */
698 error_report("usbdevice %s not found", driver
);
704 error_report("Error: no usb bus to attach usbdevice %s, "
705 "please try -machine usb=on and check that "
706 "the machine model supports USB", driver
);
710 if (f
->usbdevice_init
) {
711 dev
= f
->usbdevice_init(bus
, params
);
714 error_report("usbdevice %s accepts no params", driver
);
717 dev
= usb_create(bus
, f
->name
);
720 error_report("Failed to create USB device '%s'", f
->name
);
723 object_property_set_bool(OBJECT(dev
), true, "realized", &err
);
725 error_report("Failed to initialize USB device '%s': %s",
726 f
->name
, error_get_pretty(err
));
728 object_unparent(OBJECT(dev
));
734 static void usb_device_class_init(ObjectClass
*klass
, void *data
)
736 DeviceClass
*k
= DEVICE_CLASS(klass
);
737 k
->bus_type
= TYPE_USB_BUS
;
738 k
->realize
= usb_qdev_realize
;
739 k
->unrealize
= usb_qdev_unrealize
;
740 k
->props
= usb_props
;
743 static const TypeInfo usb_device_type_info
= {
744 .name
= TYPE_USB_DEVICE
,
745 .parent
= TYPE_DEVICE
,
746 .instance_size
= sizeof(USBDevice
),
748 .class_size
= sizeof(USBDeviceClass
),
749 .class_init
= usb_device_class_init
,
752 static void usb_register_types(void)
754 type_register_static(&usb_bus_info
);
755 type_register_static(&usb_device_type_info
);
758 type_init(usb_register_types
)