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
) {
62 if (dev
->setup_index
< 0 ||
64 dev
->setup_index
> dev
->setup_len
||
65 dev
->setup_len
> sizeof(dev
->data_buf
)) {
71 const VMStateDescription vmstate_usb_device
= {
74 .minimum_version_id
= 1,
75 .post_load
= usb_device_post_load
,
76 .fields
= (VMStateField
[]) {
77 VMSTATE_UINT8(addr
, USBDevice
),
78 VMSTATE_INT32(state
, USBDevice
),
79 VMSTATE_INT32(remote_wakeup
, USBDevice
),
80 VMSTATE_INT32(setup_state
, USBDevice
),
81 VMSTATE_INT32(setup_len
, USBDevice
),
82 VMSTATE_INT32(setup_index
, USBDevice
),
83 VMSTATE_UINT8_ARRAY(setup_buf
, USBDevice
, 8),
84 VMSTATE_END_OF_LIST(),
88 void usb_bus_new(USBBus
*bus
, size_t bus_size
,
89 USBBusOps
*ops
, DeviceState
*host
)
91 qbus_create_inplace(bus
, bus_size
, TYPE_USB_BUS
, host
, NULL
);
92 qbus_set_bus_hotplug_handler(BUS(bus
), &error_abort
);
94 bus
->busnr
= next_usb_bus
++;
95 QTAILQ_INIT(&bus
->free
);
96 QTAILQ_INIT(&bus
->used
);
97 QTAILQ_INSERT_TAIL(&busses
, bus
, next
);
100 void usb_bus_release(USBBus
*bus
)
102 assert(next_usb_bus
> 0);
104 QTAILQ_REMOVE(&busses
, bus
, next
);
107 USBBus
*usb_bus_find(int busnr
)
112 return QTAILQ_FIRST(&busses
);
113 QTAILQ_FOREACH(bus
, &busses
, next
) {
114 if (bus
->busnr
== busnr
)
120 static void usb_device_realize(USBDevice
*dev
, Error
**errp
)
122 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
124 if (klass
->realize
) {
125 klass
->realize(dev
, errp
);
129 USBDevice
*usb_device_find_device(USBDevice
*dev
, uint8_t addr
)
131 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
132 if (klass
->find_device
) {
133 return klass
->find_device(dev
, addr
);
138 static void usb_device_handle_destroy(USBDevice
*dev
)
140 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
141 if (klass
->handle_destroy
) {
142 klass
->handle_destroy(dev
);
146 void usb_device_cancel_packet(USBDevice
*dev
, USBPacket
*p
)
148 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
149 if (klass
->cancel_packet
) {
150 klass
->cancel_packet(dev
, p
);
154 void usb_device_handle_attach(USBDevice
*dev
)
156 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
157 if (klass
->handle_attach
) {
158 klass
->handle_attach(dev
);
162 void usb_device_handle_reset(USBDevice
*dev
)
164 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
165 if (klass
->handle_reset
) {
166 klass
->handle_reset(dev
);
170 void usb_device_handle_control(USBDevice
*dev
, USBPacket
*p
, int request
,
171 int value
, int index
, int length
, uint8_t *data
)
173 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
174 if (klass
->handle_control
) {
175 klass
->handle_control(dev
, p
, request
, value
, index
, length
, data
);
179 void usb_device_handle_data(USBDevice
*dev
, USBPacket
*p
)
181 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
182 if (klass
->handle_data
) {
183 klass
->handle_data(dev
, p
);
187 const char *usb_device_get_product_desc(USBDevice
*dev
)
189 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
190 return klass
->product_desc
;
193 const USBDesc
*usb_device_get_usb_desc(USBDevice
*dev
)
195 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
197 return dev
->usb_desc
;
199 return klass
->usb_desc
;
202 void usb_device_set_interface(USBDevice
*dev
, int interface
,
203 int alt_old
, int alt_new
)
205 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
206 if (klass
->set_interface
) {
207 klass
->set_interface(dev
, interface
, alt_old
, alt_new
);
211 void usb_device_flush_ep_queue(USBDevice
*dev
, USBEndpoint
*ep
)
213 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
214 if (klass
->flush_ep_queue
) {
215 klass
->flush_ep_queue(dev
, ep
);
219 void usb_device_ep_stopped(USBDevice
*dev
, USBEndpoint
*ep
)
221 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
222 if (klass
->ep_stopped
) {
223 klass
->ep_stopped(dev
, ep
);
227 int usb_device_alloc_streams(USBDevice
*dev
, USBEndpoint
**eps
, int nr_eps
,
230 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
231 if (klass
->alloc_streams
) {
232 return klass
->alloc_streams(dev
, eps
, nr_eps
, streams
);
237 void usb_device_free_streams(USBDevice
*dev
, USBEndpoint
**eps
, int nr_eps
)
239 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
240 if (klass
->free_streams
) {
241 klass
->free_streams(dev
, eps
, nr_eps
);
245 static void usb_qdev_realize(DeviceState
*qdev
, Error
**errp
)
247 USBDevice
*dev
= USB_DEVICE(qdev
);
248 Error
*local_err
= NULL
;
250 pstrcpy(dev
->product_desc
, sizeof(dev
->product_desc
),
251 usb_device_get_product_desc(dev
));
252 dev
->auto_attach
= 1;
253 QLIST_INIT(&dev
->strings
);
256 usb_claim_port(dev
, &local_err
);
258 error_propagate(errp
, local_err
);
262 usb_device_realize(dev
, &local_err
);
264 usb_release_port(dev
);
265 error_propagate(errp
, local_err
);
269 if (dev
->auto_attach
) {
270 usb_device_attach(dev
, &local_err
);
272 usb_qdev_unrealize(qdev
, NULL
);
273 error_propagate(errp
, local_err
);
279 static void usb_qdev_unrealize(DeviceState
*qdev
, Error
**errp
)
281 USBDevice
*dev
= USB_DEVICE(qdev
);
284 usb_device_detach(dev
);
286 usb_device_handle_destroy(dev
);
288 usb_release_port(dev
);
292 typedef struct LegacyUSBFactory
295 const char *usbdevice_name
;
296 USBDevice
*(*usbdevice_init
)(USBBus
*bus
, const char *params
);
299 static GSList
*legacy_usb_factory
;
301 void usb_legacy_register(const char *typename
, const char *usbdevice_name
,
302 USBDevice
*(*usbdevice_init
)(USBBus
*bus
,
305 if (usbdevice_name
) {
306 LegacyUSBFactory
*f
= g_malloc0(sizeof(*f
));
308 f
->usbdevice_name
= usbdevice_name
;
309 f
->usbdevice_init
= usbdevice_init
;
310 legacy_usb_factory
= g_slist_append(legacy_usb_factory
, f
);
314 USBDevice
*usb_create(USBBus
*bus
, const char *name
)
318 dev
= qdev_create(&bus
->qbus
, name
);
319 return USB_DEVICE(dev
);
322 static USBDevice
*usb_try_create_simple(USBBus
*bus
, const char *name
,
328 dev
= USB_DEVICE(qdev_try_create(&bus
->qbus
, name
));
330 error_setg(errp
, "Failed to create USB device '%s'", name
);
333 object_property_set_bool(OBJECT(dev
), true, "realized", &err
);
335 error_propagate(errp
, err
);
336 error_prepend(errp
, "Failed to initialize USB device '%s': ",
338 object_unparent(OBJECT(dev
));
344 USBDevice
*usb_create_simple(USBBus
*bus
, const char *name
)
346 return usb_try_create_simple(bus
, name
, &error_abort
);
349 static void usb_fill_port(USBPort
*port
, void *opaque
, int index
,
350 USBPortOps
*ops
, int speedmask
)
352 port
->opaque
= opaque
;
355 port
->speedmask
= speedmask
;
356 usb_port_location(port
, NULL
, index
+ 1);
359 void usb_register_port(USBBus
*bus
, USBPort
*port
, void *opaque
, int index
,
360 USBPortOps
*ops
, int speedmask
)
362 usb_fill_port(port
, opaque
, index
, ops
, speedmask
);
363 QTAILQ_INSERT_TAIL(&bus
->free
, port
, next
);
367 void usb_register_companion(const char *masterbus
, USBPort
*ports
[],
368 uint32_t portcount
, uint32_t firstport
,
369 void *opaque
, USBPortOps
*ops
, int speedmask
,
375 QTAILQ_FOREACH(bus
, &busses
, next
) {
376 if (strcmp(bus
->qbus
.name
, masterbus
) == 0) {
382 error_setg(errp
, "USB bus '%s' not found", masterbus
);
385 if (!bus
->ops
->register_companion
) {
386 error_setg(errp
, "Can't use USB bus '%s' as masterbus,"
387 " it doesn't support companion controllers",
392 for (i
= 0; i
< portcount
; i
++) {
393 usb_fill_port(ports
[i
], opaque
, i
, ops
, speedmask
);
396 bus
->ops
->register_companion(bus
, ports
, portcount
, firstport
, errp
);
399 void usb_port_location(USBPort
*downstream
, USBPort
*upstream
, int portnr
)
402 snprintf(downstream
->path
, sizeof(downstream
->path
), "%s.%d",
403 upstream
->path
, portnr
);
404 downstream
->hubcount
= upstream
->hubcount
+ 1;
406 snprintf(downstream
->path
, sizeof(downstream
->path
), "%d", portnr
);
407 downstream
->hubcount
= 0;
411 void usb_unregister_port(USBBus
*bus
, USBPort
*port
)
414 object_unparent(OBJECT(port
->dev
));
416 QTAILQ_REMOVE(&bus
->free
, port
, next
);
420 void usb_claim_port(USBDevice
*dev
, Error
**errp
)
422 USBBus
*bus
= usb_bus_from_device(dev
);
425 assert(dev
->port
== NULL
);
427 if (dev
->port_path
) {
428 QTAILQ_FOREACH(port
, &bus
->free
, next
) {
429 if (strcmp(port
->path
, dev
->port_path
) == 0) {
434 error_setg(errp
, "usb port %s (bus %s) not found (in use?)",
435 dev
->port_path
, bus
->qbus
.name
);
439 if (bus
->nfree
== 1 && strcmp(object_get_typename(OBJECT(dev
)), "usb-hub") != 0) {
440 /* Create a new hub and chain it on */
441 usb_try_create_simple(bus
, "usb-hub", NULL
);
443 if (bus
->nfree
== 0) {
444 error_setg(errp
, "tried to attach usb device %s to a bus "
445 "with no free ports", dev
->product_desc
);
448 port
= QTAILQ_FIRST(&bus
->free
);
450 trace_usb_port_claim(bus
->busnr
, port
->path
);
452 QTAILQ_REMOVE(&bus
->free
, port
, next
);
458 QTAILQ_INSERT_TAIL(&bus
->used
, port
, next
);
462 void usb_release_port(USBDevice
*dev
)
464 USBBus
*bus
= usb_bus_from_device(dev
);
465 USBPort
*port
= dev
->port
;
467 assert(port
!= NULL
);
468 trace_usb_port_release(bus
->busnr
, port
->path
);
470 QTAILQ_REMOVE(&bus
->used
, port
, next
);
476 QTAILQ_INSERT_TAIL(&bus
->free
, port
, next
);
480 static void usb_mask_to_str(char *dest
, size_t size
,
481 unsigned int speedmask
)
483 static const struct {
487 { .mask
= USB_SPEED_MASK_FULL
, .name
= "full" },
488 { .mask
= USB_SPEED_MASK_HIGH
, .name
= "high" },
489 { .mask
= USB_SPEED_MASK_SUPER
, .name
= "super" },
493 for (i
= 0; i
< ARRAY_SIZE(speeds
); i
++) {
494 if (speeds
[i
].mask
& speedmask
) {
495 pos
+= snprintf(dest
+ pos
, size
- pos
, "%s%s",
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
);
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
);
554 int usb_device_delete_addr(int busnr
, int addr
)
560 bus
= usb_bus_find(busnr
);
564 QTAILQ_FOREACH(port
, &bus
->used
, next
) {
565 if (port
->dev
->addr
== addr
)
572 object_unparent(OBJECT(dev
));
576 static const char *usb_speed(unsigned int speed
)
578 static const char *txt
[] = {
579 [ USB_SPEED_LOW
] = "1.5",
580 [ USB_SPEED_FULL
] = "12",
581 [ USB_SPEED_HIGH
] = "480",
582 [ USB_SPEED_SUPER
] = "5000",
584 if (speed
>= ARRAY_SIZE(txt
))
589 static void usb_bus_dev_print(Monitor
*mon
, DeviceState
*qdev
, int indent
)
591 USBDevice
*dev
= USB_DEVICE(qdev
);
592 USBBus
*bus
= usb_bus_from_device(dev
);
594 monitor_printf(mon
, "%*saddr %d.%d, port %s, speed %s, name %s%s\n",
595 indent
, "", bus
->busnr
, dev
->addr
,
596 dev
->port
? dev
->port
->path
: "-",
597 usb_speed(dev
->speed
), dev
->product_desc
,
598 dev
->attached
? ", attached" : "");
601 static char *usb_get_dev_path(DeviceState
*qdev
)
603 USBDevice
*dev
= USB_DEVICE(qdev
);
604 DeviceState
*hcd
= qdev
->parent_bus
->parent
;
607 if (dev
->flags
& (1 << USB_DEV_FLAG_FULL_PATH
)) {
608 id
= qdev_get_dev_path(hcd
);
611 char *ret
= g_strdup_printf("%s/%s", id
, dev
->port
->path
);
615 return g_strdup(dev
->port
->path
);
619 static char *usb_get_fw_dev_path(DeviceState
*qdev
)
621 USBDevice
*dev
= USB_DEVICE(qdev
);
623 ssize_t pos
= 0, fw_len
;
626 fw_len
= 32 + strlen(dev
->port
->path
) * 6;
627 fw_path
= g_malloc(fw_len
);
628 in
= dev
->port
->path
;
629 while (fw_len
- pos
> 0) {
630 nr
= strtol(in
, &in
, 10);
632 /* some hub between root port and device */
633 pos
+= snprintf(fw_path
+ pos
, fw_len
- pos
, "hub@%lx/", nr
);
636 /* the device itself */
637 pos
+= snprintf(fw_path
+ pos
, fw_len
- pos
, "%s@%lx",
638 qdev_fw_name(qdev
), nr
);
645 void hmp_info_usb(Monitor
*mon
, const QDict
*qdict
)
651 if (QTAILQ_EMPTY(&busses
)) {
652 monitor_printf(mon
, "USB support not enabled\n");
656 QTAILQ_FOREACH(bus
, &busses
, next
) {
657 QTAILQ_FOREACH(port
, &bus
->used
, next
) {
661 monitor_printf(mon
, " Device %d.%d, Port %s, Speed %s Mb/s, "
663 bus
->busnr
, dev
->addr
, port
->path
,
664 usb_speed(dev
->speed
), dev
->product_desc
,
665 dev
->qdev
.id
? ", ID: " : "",
671 /* handle legacy -usbdevice cmd line option */
672 USBDevice
*usbdevice_create(const char *cmdline
)
674 USBBus
*bus
= usb_bus_find(-1 /* any */);
675 LegacyUSBFactory
*f
= NULL
;
683 params
= strchr(cmdline
,':');
686 len
= params
- cmdline
;
687 if (len
> sizeof(driver
))
688 len
= sizeof(driver
);
689 pstrcpy(driver
, len
, cmdline
);
692 pstrcpy(driver
, sizeof(driver
), cmdline
);
695 for (i
= legacy_usb_factory
; i
; i
= i
->next
) {
697 if (strcmp(f
->usbdevice_name
, driver
) == 0) {
703 /* no error because some drivers are not converted (yet) */
704 error_report("usbdevice %s not found", driver
);
710 error_report("Error: no usb bus to attach usbdevice %s, "
711 "please try -machine usb=on and check that "
712 "the machine model supports USB", driver
);
716 if (f
->usbdevice_init
) {
717 dev
= f
->usbdevice_init(bus
, params
);
720 error_report("usbdevice %s accepts no params", driver
);
723 dev
= usb_create(bus
, f
->name
);
726 error_report("Failed to create USB device '%s'", f
->name
);
729 object_property_set_bool(OBJECT(dev
), true, "realized", &err
);
731 error_reportf_err(err
, "Failed to initialize USB device '%s': ",
733 object_unparent(OBJECT(dev
));
739 static void usb_device_class_init(ObjectClass
*klass
, void *data
)
741 DeviceClass
*k
= DEVICE_CLASS(klass
);
742 k
->bus_type
= TYPE_USB_BUS
;
743 k
->realize
= usb_qdev_realize
;
744 k
->unrealize
= usb_qdev_unrealize
;
745 k
->props
= usb_props
;
748 static const TypeInfo usb_device_type_info
= {
749 .name
= TYPE_USB_DEVICE
,
750 .parent
= TYPE_DEVICE
,
751 .instance_size
= sizeof(USBDevice
),
753 .class_size
= sizeof(USBDeviceClass
),
754 .class_init
= usb_device_class_init
,
757 static void usb_register_types(void)
759 type_register_static(&usb_bus_info
);
760 type_register_static(&usb_device_type_info
);
763 type_init(usb_register_types
)