4 #include "sysemu/sysemu.h"
5 #include "monitor/monitor.h"
8 static void usb_bus_dev_print(Monitor
*mon
, DeviceState
*qdev
, int indent
);
10 static char *usb_get_dev_path(DeviceState
*dev
);
11 static char *usb_get_fw_dev_path(DeviceState
*qdev
);
12 static int usb_qdev_exit(DeviceState
*qdev
);
14 static Property usb_props
[] = {
15 DEFINE_PROP_STRING("port", USBDevice
, port_path
),
16 DEFINE_PROP_BIT("full-path", USBDevice
, flags
,
17 USB_DEV_FLAG_FULL_PATH
, true),
18 DEFINE_PROP_END_OF_LIST()
21 static void usb_bus_class_init(ObjectClass
*klass
, void *data
)
23 BusClass
*k
= BUS_CLASS(klass
);
25 k
->print_dev
= usb_bus_dev_print
;
26 k
->get_dev_path
= usb_get_dev_path
;
27 k
->get_fw_dev_path
= usb_get_fw_dev_path
;
30 static const TypeInfo usb_bus_info
= {
33 .instance_size
= sizeof(USBBus
),
34 .class_init
= usb_bus_class_init
,
37 static int next_usb_bus
= 0;
38 static QTAILQ_HEAD(, USBBus
) busses
= QTAILQ_HEAD_INITIALIZER(busses
);
40 static int usb_device_post_load(void *opaque
, int version_id
)
42 USBDevice
*dev
= opaque
;
44 if (dev
->state
== USB_STATE_NOTATTACHED
) {
52 const VMStateDescription vmstate_usb_device
= {
55 .minimum_version_id
= 1,
56 .post_load
= usb_device_post_load
,
57 .fields
= (VMStateField
[]) {
58 VMSTATE_UINT8(addr
, USBDevice
),
59 VMSTATE_INT32(state
, USBDevice
),
60 VMSTATE_INT32(remote_wakeup
, USBDevice
),
61 VMSTATE_INT32(setup_state
, USBDevice
),
62 VMSTATE_INT32(setup_len
, USBDevice
),
63 VMSTATE_INT32(setup_index
, USBDevice
),
64 VMSTATE_UINT8_ARRAY(setup_buf
, USBDevice
, 8),
65 VMSTATE_END_OF_LIST(),
69 void usb_bus_new(USBBus
*bus
, USBBusOps
*ops
, DeviceState
*host
)
71 qbus_create_inplace(&bus
->qbus
, TYPE_USB_BUS
, host
, NULL
);
73 bus
->busnr
= next_usb_bus
++;
74 bus
->qbus
.allow_hotplug
= 1; /* Yes, we can */
75 QTAILQ_INIT(&bus
->free
);
76 QTAILQ_INIT(&bus
->used
);
77 QTAILQ_INSERT_TAIL(&busses
, bus
, next
);
80 USBBus
*usb_bus_find(int busnr
)
85 return QTAILQ_FIRST(&busses
);
86 QTAILQ_FOREACH(bus
, &busses
, next
) {
87 if (bus
->busnr
== busnr
)
93 static int usb_device_init(USBDevice
*dev
)
95 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
97 return klass
->init(dev
);
102 USBDevice
*usb_device_find_device(USBDevice
*dev
, uint8_t addr
)
104 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
105 if (klass
->find_device
) {
106 return klass
->find_device(dev
, addr
);
111 static void usb_device_handle_destroy(USBDevice
*dev
)
113 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
114 if (klass
->handle_destroy
) {
115 klass
->handle_destroy(dev
);
119 void usb_device_cancel_packet(USBDevice
*dev
, USBPacket
*p
)
121 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
122 if (klass
->cancel_packet
) {
123 klass
->cancel_packet(dev
, p
);
127 void usb_device_handle_attach(USBDevice
*dev
)
129 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
130 if (klass
->handle_attach
) {
131 klass
->handle_attach(dev
);
135 void usb_device_handle_reset(USBDevice
*dev
)
137 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
138 if (klass
->handle_reset
) {
139 klass
->handle_reset(dev
);
143 void usb_device_handle_control(USBDevice
*dev
, USBPacket
*p
, int request
,
144 int value
, int index
, int length
, uint8_t *data
)
146 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
147 if (klass
->handle_control
) {
148 klass
->handle_control(dev
, p
, request
, value
, index
, length
, data
);
152 void usb_device_handle_data(USBDevice
*dev
, USBPacket
*p
)
154 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
155 if (klass
->handle_data
) {
156 klass
->handle_data(dev
, p
);
160 const char *usb_device_get_product_desc(USBDevice
*dev
)
162 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
163 return klass
->product_desc
;
166 const USBDesc
*usb_device_get_usb_desc(USBDevice
*dev
)
168 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
170 return dev
->usb_desc
;
172 return klass
->usb_desc
;
175 void usb_device_set_interface(USBDevice
*dev
, int interface
,
176 int alt_old
, int alt_new
)
178 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
179 if (klass
->set_interface
) {
180 klass
->set_interface(dev
, interface
, alt_old
, alt_new
);
184 void usb_device_flush_ep_queue(USBDevice
*dev
, USBEndpoint
*ep
)
186 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
187 if (klass
->flush_ep_queue
) {
188 klass
->flush_ep_queue(dev
, ep
);
192 void usb_device_ep_stopped(USBDevice
*dev
, USBEndpoint
*ep
)
194 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
195 if (klass
->ep_stopped
) {
196 klass
->ep_stopped(dev
, ep
);
200 static int usb_qdev_init(DeviceState
*qdev
)
202 USBDevice
*dev
= USB_DEVICE(qdev
);
205 pstrcpy(dev
->product_desc
, sizeof(dev
->product_desc
),
206 usb_device_get_product_desc(dev
));
207 dev
->auto_attach
= 1;
208 QLIST_INIT(&dev
->strings
);
210 rc
= usb_claim_port(dev
);
214 rc
= usb_device_init(dev
);
216 usb_release_port(dev
);
219 if (dev
->auto_attach
) {
220 rc
= usb_device_attach(dev
);
229 static int usb_qdev_exit(DeviceState
*qdev
)
231 USBDevice
*dev
= USB_DEVICE(qdev
);
234 usb_device_detach(dev
);
236 usb_device_handle_destroy(dev
);
238 usb_release_port(dev
);
243 typedef struct LegacyUSBFactory
246 const char *usbdevice_name
;
247 USBDevice
*(*usbdevice_init
)(USBBus
*bus
, const char *params
);
250 static GSList
*legacy_usb_factory
;
252 void usb_legacy_register(const char *typename
, const char *usbdevice_name
,
253 USBDevice
*(*usbdevice_init
)(USBBus
*bus
,
256 if (usbdevice_name
) {
257 LegacyUSBFactory
*f
= g_malloc0(sizeof(*f
));
259 f
->usbdevice_name
= usbdevice_name
;
260 f
->usbdevice_init
= usbdevice_init
;
261 legacy_usb_factory
= g_slist_append(legacy_usb_factory
, f
);
265 USBDevice
*usb_create(USBBus
*bus
, const char *name
)
269 dev
= qdev_create(&bus
->qbus
, name
);
270 return USB_DEVICE(dev
);
273 USBDevice
*usb_create_simple(USBBus
*bus
, const char *name
)
275 USBDevice
*dev
= usb_create(bus
, name
);
279 error_report("Failed to create USB device '%s'", name
);
282 rc
= qdev_init(&dev
->qdev
);
284 error_report("Failed to initialize USB device '%s'", name
);
290 static void usb_fill_port(USBPort
*port
, void *opaque
, int index
,
291 USBPortOps
*ops
, int speedmask
)
293 port
->opaque
= opaque
;
296 port
->speedmask
= speedmask
;
297 usb_port_location(port
, NULL
, index
+ 1);
300 void usb_register_port(USBBus
*bus
, USBPort
*port
, void *opaque
, int index
,
301 USBPortOps
*ops
, int speedmask
)
303 usb_fill_port(port
, opaque
, index
, ops
, speedmask
);
304 QTAILQ_INSERT_TAIL(&bus
->free
, port
, next
);
308 int usb_register_companion(const char *masterbus
, USBPort
*ports
[],
309 uint32_t portcount
, uint32_t firstport
,
310 void *opaque
, USBPortOps
*ops
, int speedmask
)
315 QTAILQ_FOREACH(bus
, &busses
, next
) {
316 if (strcmp(bus
->qbus
.name
, masterbus
) == 0) {
321 if (!bus
|| !bus
->ops
->register_companion
) {
322 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "masterbus",
325 error_printf_unless_qmp(
326 "USB bus '%s' does not allow companion controllers\n",
332 for (i
= 0; i
< portcount
; i
++) {
333 usb_fill_port(ports
[i
], opaque
, i
, ops
, speedmask
);
336 return bus
->ops
->register_companion(bus
, ports
, portcount
, firstport
);
339 void usb_port_location(USBPort
*downstream
, USBPort
*upstream
, int portnr
)
342 snprintf(downstream
->path
, sizeof(downstream
->path
), "%s.%d",
343 upstream
->path
, portnr
);
345 snprintf(downstream
->path
, sizeof(downstream
->path
), "%d", portnr
);
349 void usb_unregister_port(USBBus
*bus
, USBPort
*port
)
352 qdev_free(&port
->dev
->qdev
);
353 QTAILQ_REMOVE(&bus
->free
, port
, next
);
357 int usb_claim_port(USBDevice
*dev
)
359 USBBus
*bus
= usb_bus_from_device(dev
);
362 assert(dev
->port
== NULL
);
364 if (dev
->port_path
) {
365 QTAILQ_FOREACH(port
, &bus
->free
, next
) {
366 if (strcmp(port
->path
, dev
->port_path
) == 0) {
371 error_report("Error: usb port %s (bus %s) not found (in use?)",
372 dev
->port_path
, bus
->qbus
.name
);
376 if (bus
->nfree
== 1 && strcmp(object_get_typename(OBJECT(dev
)), "usb-hub") != 0) {
377 /* Create a new hub and chain it on */
378 usb_create_simple(bus
, "usb-hub");
380 if (bus
->nfree
== 0) {
381 error_report("Error: tried to attach usb device %s to a bus "
382 "with no free ports", dev
->product_desc
);
385 port
= QTAILQ_FIRST(&bus
->free
);
387 trace_usb_port_claim(bus
->busnr
, port
->path
);
389 QTAILQ_REMOVE(&bus
->free
, port
, next
);
395 QTAILQ_INSERT_TAIL(&bus
->used
, port
, next
);
400 void usb_release_port(USBDevice
*dev
)
402 USBBus
*bus
= usb_bus_from_device(dev
);
403 USBPort
*port
= dev
->port
;
405 assert(port
!= NULL
);
406 trace_usb_port_release(bus
->busnr
, port
->path
);
408 QTAILQ_REMOVE(&bus
->used
, port
, next
);
414 QTAILQ_INSERT_TAIL(&bus
->free
, port
, next
);
418 int usb_device_attach(USBDevice
*dev
)
420 USBBus
*bus
= usb_bus_from_device(dev
);
421 USBPort
*port
= dev
->port
;
423 assert(port
!= NULL
);
424 assert(!dev
->attached
);
425 trace_usb_port_attach(bus
->busnr
, port
->path
);
427 if (!(port
->speedmask
& dev
->speedmask
)) {
428 error_report("Warning: speed mismatch trying to attach "
429 "usb device %s to bus %s",
430 dev
->product_desc
, bus
->qbus
.name
);
440 int usb_device_detach(USBDevice
*dev
)
442 USBBus
*bus
= usb_bus_from_device(dev
);
443 USBPort
*port
= dev
->port
;
445 assert(port
!= NULL
);
446 assert(dev
->attached
);
447 trace_usb_port_detach(bus
->busnr
, port
->path
);
454 int usb_device_delete_addr(int busnr
, int addr
)
460 bus
= usb_bus_find(busnr
);
464 QTAILQ_FOREACH(port
, &bus
->used
, next
) {
465 if (port
->dev
->addr
== addr
)
472 qdev_free(&dev
->qdev
);
476 static const char *usb_speed(unsigned int speed
)
478 static const char *txt
[] = {
479 [ USB_SPEED_LOW
] = "1.5",
480 [ USB_SPEED_FULL
] = "12",
481 [ USB_SPEED_HIGH
] = "480",
482 [ USB_SPEED_SUPER
] = "5000",
484 if (speed
>= ARRAY_SIZE(txt
))
489 static void usb_bus_dev_print(Monitor
*mon
, DeviceState
*qdev
, int indent
)
491 USBDevice
*dev
= USB_DEVICE(qdev
);
492 USBBus
*bus
= usb_bus_from_device(dev
);
494 monitor_printf(mon
, "%*saddr %d.%d, port %s, speed %s, name %s%s\n",
495 indent
, "", bus
->busnr
, dev
->addr
,
496 dev
->port
? dev
->port
->path
: "-",
497 usb_speed(dev
->speed
), dev
->product_desc
,
498 dev
->attached
? ", attached" : "");
501 static char *usb_get_dev_path(DeviceState
*qdev
)
503 USBDevice
*dev
= USB_DEVICE(qdev
);
504 DeviceState
*hcd
= qdev
->parent_bus
->parent
;
507 if (dev
->flags
& (1 << USB_DEV_FLAG_FULL_PATH
)) {
508 id
= qdev_get_dev_path(hcd
);
511 char *ret
= g_strdup_printf("%s/%s", id
, dev
->port
->path
);
515 return g_strdup(dev
->port
->path
);
519 static char *usb_get_fw_dev_path(DeviceState
*qdev
)
521 USBDevice
*dev
= USB_DEVICE(qdev
);
523 ssize_t pos
= 0, fw_len
;
526 fw_len
= 32 + strlen(dev
->port
->path
) * 6;
527 fw_path
= g_malloc(fw_len
);
528 in
= dev
->port
->path
;
529 while (fw_len
- pos
> 0) {
530 nr
= strtol(in
, &in
, 10);
532 /* some hub between root port and device */
533 pos
+= snprintf(fw_path
+ pos
, fw_len
- pos
, "hub@%ld/", nr
);
536 /* the device itself */
537 pos
+= snprintf(fw_path
+ pos
, fw_len
- pos
, "%s@%ld",
538 qdev_fw_name(qdev
), nr
);
545 void usb_info(Monitor
*mon
, const QDict
*qdict
)
551 if (QTAILQ_EMPTY(&busses
)) {
552 monitor_printf(mon
, "USB support not enabled\n");
556 QTAILQ_FOREACH(bus
, &busses
, next
) {
557 QTAILQ_FOREACH(port
, &bus
->used
, next
) {
561 monitor_printf(mon
, " Device %d.%d, Port %s, Speed %s Mb/s, Product %s\n",
562 bus
->busnr
, dev
->addr
, port
->path
, usb_speed(dev
->speed
),
568 /* handle legacy -usbdevice cmd line option */
569 USBDevice
*usbdevice_create(const char *cmdline
)
571 USBBus
*bus
= usb_bus_find(-1 /* any */);
572 LegacyUSBFactory
*f
= NULL
;
578 params
= strchr(cmdline
,':');
581 len
= params
- cmdline
;
582 if (len
> sizeof(driver
))
583 len
= sizeof(driver
);
584 pstrcpy(driver
, len
, cmdline
);
587 pstrcpy(driver
, sizeof(driver
), cmdline
);
590 for (i
= legacy_usb_factory
; i
; i
= i
->next
) {
592 if (strcmp(f
->usbdevice_name
, driver
) == 0) {
598 /* no error because some drivers are not converted (yet) */
599 error_report("usbdevice %s not found", driver
);
605 error_report("Error: no usb bus to attach usbdevice %s, "
606 "please try -machine usb=on and check that "
607 "the machine model supports USB", driver
);
611 if (!f
->usbdevice_init
) {
613 error_report("usbdevice %s accepts no params", driver
);
616 return usb_create_simple(bus
, f
->name
);
618 return f
->usbdevice_init(bus
, params
);
621 static void usb_device_class_init(ObjectClass
*klass
, void *data
)
623 DeviceClass
*k
= DEVICE_CLASS(klass
);
624 k
->bus_type
= TYPE_USB_BUS
;
625 k
->init
= usb_qdev_init
;
626 k
->unplug
= qdev_simple_unplug_cb
;
627 k
->exit
= usb_qdev_exit
;
628 k
->props
= usb_props
;
631 static const TypeInfo usb_device_type_info
= {
632 .name
= TYPE_USB_DEVICE
,
633 .parent
= TYPE_DEVICE
,
634 .instance_size
= sizeof(USBDevice
),
636 .class_size
= sizeof(USBDeviceClass
),
637 .class_init
= usb_device_class_init
,
640 static void usb_register_types(void)
642 type_register_static(&usb_bus_info
);
643 type_register_static(&usb_device_type_info
);
646 type_init(usb_register_types
)