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_STRING("serial", USBDevice
, serial
),
17 DEFINE_PROP_BIT("full-path", USBDevice
, flags
,
18 USB_DEV_FLAG_FULL_PATH
, true),
19 DEFINE_PROP_BIT("msos-desc", USBDevice
, flags
,
20 USB_DEV_FLAG_MSOS_DESC_ENABLE
, true),
21 DEFINE_PROP_END_OF_LIST()
24 static void usb_bus_class_init(ObjectClass
*klass
, void *data
)
26 BusClass
*k
= BUS_CLASS(klass
);
28 k
->print_dev
= usb_bus_dev_print
;
29 k
->get_dev_path
= usb_get_dev_path
;
30 k
->get_fw_dev_path
= usb_get_fw_dev_path
;
33 static const TypeInfo usb_bus_info
= {
36 .instance_size
= sizeof(USBBus
),
37 .class_init
= usb_bus_class_init
,
40 static int next_usb_bus
= 0;
41 static QTAILQ_HEAD(, USBBus
) busses
= QTAILQ_HEAD_INITIALIZER(busses
);
43 static int usb_device_post_load(void *opaque
, int version_id
)
45 USBDevice
*dev
= opaque
;
47 if (dev
->state
== USB_STATE_NOTATTACHED
) {
52 if (dev
->setup_index
< 0 ||
54 dev
->setup_index
> dev
->setup_len
||
55 dev
->setup_len
> sizeof(dev
->data_buf
)) {
61 const VMStateDescription vmstate_usb_device
= {
64 .minimum_version_id
= 1,
65 .post_load
= usb_device_post_load
,
66 .fields
= (VMStateField
[]) {
67 VMSTATE_UINT8(addr
, USBDevice
),
68 VMSTATE_INT32(state
, USBDevice
),
69 VMSTATE_INT32(remote_wakeup
, USBDevice
),
70 VMSTATE_INT32(setup_state
, USBDevice
),
71 VMSTATE_INT32(setup_len
, USBDevice
),
72 VMSTATE_INT32(setup_index
, USBDevice
),
73 VMSTATE_UINT8_ARRAY(setup_buf
, USBDevice
, 8),
74 VMSTATE_END_OF_LIST(),
78 void usb_bus_new(USBBus
*bus
, size_t bus_size
,
79 USBBusOps
*ops
, DeviceState
*host
)
81 qbus_create_inplace(bus
, bus_size
, TYPE_USB_BUS
, host
, NULL
);
83 bus
->busnr
= next_usb_bus
++;
84 bus
->qbus
.allow_hotplug
= 1; /* Yes, we can */
85 QTAILQ_INIT(&bus
->free
);
86 QTAILQ_INIT(&bus
->used
);
87 QTAILQ_INSERT_TAIL(&busses
, bus
, next
);
90 USBBus
*usb_bus_find(int busnr
)
95 return QTAILQ_FIRST(&busses
);
96 QTAILQ_FOREACH(bus
, &busses
, next
) {
97 if (bus
->busnr
== busnr
)
103 static int usb_device_init(USBDevice
*dev
)
105 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
107 return klass
->init(dev
);
112 USBDevice
*usb_device_find_device(USBDevice
*dev
, uint8_t addr
)
114 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
115 if (klass
->find_device
) {
116 return klass
->find_device(dev
, addr
);
121 static void usb_device_handle_destroy(USBDevice
*dev
)
123 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
124 if (klass
->handle_destroy
) {
125 klass
->handle_destroy(dev
);
129 void usb_device_cancel_packet(USBDevice
*dev
, USBPacket
*p
)
131 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
132 if (klass
->cancel_packet
) {
133 klass
->cancel_packet(dev
, p
);
137 void usb_device_handle_attach(USBDevice
*dev
)
139 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
140 if (klass
->handle_attach
) {
141 klass
->handle_attach(dev
);
145 void usb_device_handle_reset(USBDevice
*dev
)
147 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
148 if (klass
->handle_reset
) {
149 klass
->handle_reset(dev
);
153 void usb_device_handle_control(USBDevice
*dev
, USBPacket
*p
, int request
,
154 int value
, int index
, int length
, uint8_t *data
)
156 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
157 if (klass
->handle_control
) {
158 klass
->handle_control(dev
, p
, request
, value
, index
, length
, data
);
162 void usb_device_handle_data(USBDevice
*dev
, USBPacket
*p
)
164 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
165 if (klass
->handle_data
) {
166 klass
->handle_data(dev
, p
);
170 const char *usb_device_get_product_desc(USBDevice
*dev
)
172 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
173 return klass
->product_desc
;
176 const USBDesc
*usb_device_get_usb_desc(USBDevice
*dev
)
178 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
180 return dev
->usb_desc
;
182 return klass
->usb_desc
;
185 void usb_device_set_interface(USBDevice
*dev
, int interface
,
186 int alt_old
, int alt_new
)
188 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
189 if (klass
->set_interface
) {
190 klass
->set_interface(dev
, interface
, alt_old
, alt_new
);
194 void usb_device_flush_ep_queue(USBDevice
*dev
, USBEndpoint
*ep
)
196 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
197 if (klass
->flush_ep_queue
) {
198 klass
->flush_ep_queue(dev
, ep
);
202 void usb_device_ep_stopped(USBDevice
*dev
, USBEndpoint
*ep
)
204 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
205 if (klass
->ep_stopped
) {
206 klass
->ep_stopped(dev
, ep
);
210 int usb_device_alloc_streams(USBDevice
*dev
, USBEndpoint
**eps
, int nr_eps
,
213 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
214 if (klass
->alloc_streams
) {
215 return klass
->alloc_streams(dev
, eps
, nr_eps
, streams
);
220 void usb_device_free_streams(USBDevice
*dev
, USBEndpoint
**eps
, int nr_eps
)
222 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
223 if (klass
->free_streams
) {
224 klass
->free_streams(dev
, eps
, nr_eps
);
228 static int usb_qdev_init(DeviceState
*qdev
)
230 USBDevice
*dev
= USB_DEVICE(qdev
);
233 pstrcpy(dev
->product_desc
, sizeof(dev
->product_desc
),
234 usb_device_get_product_desc(dev
));
235 dev
->auto_attach
= 1;
236 QLIST_INIT(&dev
->strings
);
238 rc
= usb_claim_port(dev
);
242 rc
= usb_device_init(dev
);
244 usb_release_port(dev
);
247 if (dev
->auto_attach
) {
248 rc
= usb_device_attach(dev
);
257 static int usb_qdev_exit(DeviceState
*qdev
)
259 USBDevice
*dev
= USB_DEVICE(qdev
);
262 usb_device_detach(dev
);
264 usb_device_handle_destroy(dev
);
266 usb_release_port(dev
);
271 typedef struct LegacyUSBFactory
274 const char *usbdevice_name
;
275 USBDevice
*(*usbdevice_init
)(USBBus
*bus
, const char *params
);
278 static GSList
*legacy_usb_factory
;
280 void usb_legacy_register(const char *typename
, const char *usbdevice_name
,
281 USBDevice
*(*usbdevice_init
)(USBBus
*bus
,
284 if (usbdevice_name
) {
285 LegacyUSBFactory
*f
= g_malloc0(sizeof(*f
));
287 f
->usbdevice_name
= usbdevice_name
;
288 f
->usbdevice_init
= usbdevice_init
;
289 legacy_usb_factory
= g_slist_append(legacy_usb_factory
, f
);
293 USBDevice
*usb_create(USBBus
*bus
, const char *name
)
297 dev
= qdev_create(&bus
->qbus
, name
);
298 return USB_DEVICE(dev
);
301 USBDevice
*usb_create_simple(USBBus
*bus
, const char *name
)
303 USBDevice
*dev
= usb_create(bus
, name
);
307 error_report("Failed to create USB device '%s'", name
);
310 rc
= qdev_init(&dev
->qdev
);
312 error_report("Failed to initialize USB device '%s'", name
);
318 static void usb_fill_port(USBPort
*port
, void *opaque
, int index
,
319 USBPortOps
*ops
, int speedmask
)
321 port
->opaque
= opaque
;
324 port
->speedmask
= speedmask
;
325 usb_port_location(port
, NULL
, index
+ 1);
328 void usb_register_port(USBBus
*bus
, USBPort
*port
, void *opaque
, int index
,
329 USBPortOps
*ops
, int speedmask
)
331 usb_fill_port(port
, opaque
, index
, ops
, speedmask
);
332 QTAILQ_INSERT_TAIL(&bus
->free
, port
, next
);
336 int usb_register_companion(const char *masterbus
, USBPort
*ports
[],
337 uint32_t portcount
, uint32_t firstport
,
338 void *opaque
, USBPortOps
*ops
, int speedmask
)
343 QTAILQ_FOREACH(bus
, &busses
, next
) {
344 if (strcmp(bus
->qbus
.name
, masterbus
) == 0) {
349 if (!bus
|| !bus
->ops
->register_companion
) {
350 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "masterbus",
353 error_printf_unless_qmp(
354 "USB bus '%s' does not allow companion controllers\n",
360 for (i
= 0; i
< portcount
; i
++) {
361 usb_fill_port(ports
[i
], opaque
, i
, ops
, speedmask
);
364 return bus
->ops
->register_companion(bus
, ports
, portcount
, firstport
);
367 void usb_port_location(USBPort
*downstream
, USBPort
*upstream
, int portnr
)
370 snprintf(downstream
->path
, sizeof(downstream
->path
), "%s.%d",
371 upstream
->path
, portnr
);
372 downstream
->hubcount
= upstream
->hubcount
+ 1;
374 snprintf(downstream
->path
, sizeof(downstream
->path
), "%d", portnr
);
375 downstream
->hubcount
= 0;
379 void usb_unregister_port(USBBus
*bus
, USBPort
*port
)
382 object_unparent(OBJECT(port
->dev
));
384 QTAILQ_REMOVE(&bus
->free
, port
, next
);
388 int usb_claim_port(USBDevice
*dev
)
390 USBBus
*bus
= usb_bus_from_device(dev
);
393 assert(dev
->port
== NULL
);
395 if (dev
->port_path
) {
396 QTAILQ_FOREACH(port
, &bus
->free
, next
) {
397 if (strcmp(port
->path
, dev
->port_path
) == 0) {
402 error_report("Error: usb port %s (bus %s) not found (in use?)",
403 dev
->port_path
, bus
->qbus
.name
);
407 if (bus
->nfree
== 1 && strcmp(object_get_typename(OBJECT(dev
)), "usb-hub") != 0) {
408 /* Create a new hub and chain it on */
409 usb_create_simple(bus
, "usb-hub");
411 if (bus
->nfree
== 0) {
412 error_report("Error: tried to attach usb device %s to a bus "
413 "with no free ports", dev
->product_desc
);
416 port
= QTAILQ_FIRST(&bus
->free
);
418 trace_usb_port_claim(bus
->busnr
, port
->path
);
420 QTAILQ_REMOVE(&bus
->free
, port
, next
);
426 QTAILQ_INSERT_TAIL(&bus
->used
, port
, next
);
431 void usb_release_port(USBDevice
*dev
)
433 USBBus
*bus
= usb_bus_from_device(dev
);
434 USBPort
*port
= dev
->port
;
436 assert(port
!= NULL
);
437 trace_usb_port_release(bus
->busnr
, port
->path
);
439 QTAILQ_REMOVE(&bus
->used
, port
, next
);
445 QTAILQ_INSERT_TAIL(&bus
->free
, port
, next
);
449 static void usb_mask_to_str(char *dest
, size_t size
,
450 unsigned int speedmask
)
452 static const struct {
456 { .mask
= USB_SPEED_MASK_FULL
, .name
= "full" },
457 { .mask
= USB_SPEED_MASK_HIGH
, .name
= "high" },
458 { .mask
= USB_SPEED_MASK_SUPER
, .name
= "super" },
462 for (i
= 0; i
< ARRAY_SIZE(speeds
); i
++) {
463 if (speeds
[i
].mask
& speedmask
) {
464 pos
+= snprintf(dest
+ pos
, size
- pos
, "%s%s",
471 int usb_device_attach(USBDevice
*dev
)
473 USBBus
*bus
= usb_bus_from_device(dev
);
474 USBPort
*port
= dev
->port
;
475 char devspeed
[32], portspeed
[32];
477 assert(port
!= NULL
);
478 assert(!dev
->attached
);
479 usb_mask_to_str(devspeed
, sizeof(devspeed
), dev
->speedmask
);
480 usb_mask_to_str(portspeed
, sizeof(portspeed
), port
->speedmask
);
481 trace_usb_port_attach(bus
->busnr
, port
->path
,
482 devspeed
, portspeed
);
484 if (!(port
->speedmask
& dev
->speedmask
)) {
485 error_report("Warning: speed mismatch trying to attach"
486 " usb device \"%s\" (%s speed)"
487 " to bus \"%s\", port \"%s\" (%s speed)",
488 dev
->product_desc
, devspeed
,
489 bus
->qbus
.name
, port
->path
, portspeed
);
499 int usb_device_detach(USBDevice
*dev
)
501 USBBus
*bus
= usb_bus_from_device(dev
);
502 USBPort
*port
= dev
->port
;
504 assert(port
!= NULL
);
505 assert(dev
->attached
);
506 trace_usb_port_detach(bus
->busnr
, port
->path
);
513 int usb_device_delete_addr(int busnr
, int addr
)
519 bus
= usb_bus_find(busnr
);
523 QTAILQ_FOREACH(port
, &bus
->used
, next
) {
524 if (port
->dev
->addr
== addr
)
531 object_unparent(OBJECT(dev
));
535 static const char *usb_speed(unsigned int speed
)
537 static const char *txt
[] = {
538 [ USB_SPEED_LOW
] = "1.5",
539 [ USB_SPEED_FULL
] = "12",
540 [ USB_SPEED_HIGH
] = "480",
541 [ USB_SPEED_SUPER
] = "5000",
543 if (speed
>= ARRAY_SIZE(txt
))
548 static void usb_bus_dev_print(Monitor
*mon
, DeviceState
*qdev
, int indent
)
550 USBDevice
*dev
= USB_DEVICE(qdev
);
551 USBBus
*bus
= usb_bus_from_device(dev
);
553 monitor_printf(mon
, "%*saddr %d.%d, port %s, speed %s, name %s%s\n",
554 indent
, "", bus
->busnr
, dev
->addr
,
555 dev
->port
? dev
->port
->path
: "-",
556 usb_speed(dev
->speed
), dev
->product_desc
,
557 dev
->attached
? ", attached" : "");
560 static char *usb_get_dev_path(DeviceState
*qdev
)
562 USBDevice
*dev
= USB_DEVICE(qdev
);
563 DeviceState
*hcd
= qdev
->parent_bus
->parent
;
566 if (dev
->flags
& (1 << USB_DEV_FLAG_FULL_PATH
)) {
567 id
= qdev_get_dev_path(hcd
);
570 char *ret
= g_strdup_printf("%s/%s", id
, dev
->port
->path
);
574 return g_strdup(dev
->port
->path
);
578 static char *usb_get_fw_dev_path(DeviceState
*qdev
)
580 USBDevice
*dev
= USB_DEVICE(qdev
);
582 ssize_t pos
= 0, fw_len
;
585 fw_len
= 32 + strlen(dev
->port
->path
) * 6;
586 fw_path
= g_malloc(fw_len
);
587 in
= dev
->port
->path
;
588 while (fw_len
- pos
> 0) {
589 nr
= strtol(in
, &in
, 10);
591 /* some hub between root port and device */
592 pos
+= snprintf(fw_path
+ pos
, fw_len
- pos
, "hub@%ld/", nr
);
595 /* the device itself */
596 pos
+= snprintf(fw_path
+ pos
, fw_len
- pos
, "%s@%ld",
597 qdev_fw_name(qdev
), nr
);
604 void usb_info(Monitor
*mon
, const QDict
*qdict
)
610 if (QTAILQ_EMPTY(&busses
)) {
611 monitor_printf(mon
, "USB support not enabled\n");
615 QTAILQ_FOREACH(bus
, &busses
, next
) {
616 QTAILQ_FOREACH(port
, &bus
->used
, next
) {
620 monitor_printf(mon
, " Device %d.%d, Port %s, Speed %s Mb/s, Product %s\n",
621 bus
->busnr
, dev
->addr
, port
->path
, usb_speed(dev
->speed
),
627 /* handle legacy -usbdevice cmd line option */
628 USBDevice
*usbdevice_create(const char *cmdline
)
630 USBBus
*bus
= usb_bus_find(-1 /* any */);
631 LegacyUSBFactory
*f
= NULL
;
637 params
= strchr(cmdline
,':');
640 len
= params
- cmdline
;
641 if (len
> sizeof(driver
))
642 len
= sizeof(driver
);
643 pstrcpy(driver
, len
, cmdline
);
646 pstrcpy(driver
, sizeof(driver
), cmdline
);
649 for (i
= legacy_usb_factory
; i
; i
= i
->next
) {
651 if (strcmp(f
->usbdevice_name
, driver
) == 0) {
657 /* no error because some drivers are not converted (yet) */
658 error_report("usbdevice %s not found", driver
);
664 error_report("Error: no usb bus to attach usbdevice %s, "
665 "please try -machine usb=on and check that "
666 "the machine model supports USB", driver
);
670 if (!f
->usbdevice_init
) {
672 error_report("usbdevice %s accepts no params", driver
);
675 return usb_create_simple(bus
, f
->name
);
677 return f
->usbdevice_init(bus
, params
);
680 static void usb_device_class_init(ObjectClass
*klass
, void *data
)
682 DeviceClass
*k
= DEVICE_CLASS(klass
);
683 k
->bus_type
= TYPE_USB_BUS
;
684 k
->init
= usb_qdev_init
;
685 k
->unplug
= qdev_simple_unplug_cb
;
686 k
->exit
= usb_qdev_exit
;
687 k
->props
= usb_props
;
690 static const TypeInfo usb_device_type_info
= {
691 .name
= TYPE_USB_DEVICE
,
692 .parent
= TYPE_DEVICE
,
693 .instance_size
= sizeof(USBDevice
),
695 .class_size
= sizeof(USBDeviceClass
),
696 .class_init
= usb_device_class_init
,
699 static void usb_register_types(void)
701 type_register_static(&usb_bus_info
);
702 type_register_static(&usb_device_type_info
);
705 type_init(usb_register_types
)