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 void usb_bus_release(USBBus
*bus
)
92 assert(next_usb_bus
> 0);
94 QTAILQ_REMOVE(&busses
, bus
, next
);
97 USBBus
*usb_bus_find(int busnr
)
102 return QTAILQ_FIRST(&busses
);
103 QTAILQ_FOREACH(bus
, &busses
, next
) {
104 if (bus
->busnr
== busnr
)
110 static int usb_device_init(USBDevice
*dev
)
112 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
114 return klass
->init(dev
);
119 USBDevice
*usb_device_find_device(USBDevice
*dev
, uint8_t addr
)
121 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
122 if (klass
->find_device
) {
123 return klass
->find_device(dev
, addr
);
128 static void usb_device_handle_destroy(USBDevice
*dev
)
130 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
131 if (klass
->handle_destroy
) {
132 klass
->handle_destroy(dev
);
136 void usb_device_cancel_packet(USBDevice
*dev
, USBPacket
*p
)
138 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
139 if (klass
->cancel_packet
) {
140 klass
->cancel_packet(dev
, p
);
144 void usb_device_handle_attach(USBDevice
*dev
)
146 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
147 if (klass
->handle_attach
) {
148 klass
->handle_attach(dev
);
152 void usb_device_handle_reset(USBDevice
*dev
)
154 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
155 if (klass
->handle_reset
) {
156 klass
->handle_reset(dev
);
160 void usb_device_handle_control(USBDevice
*dev
, USBPacket
*p
, int request
,
161 int value
, int index
, int length
, uint8_t *data
)
163 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
164 if (klass
->handle_control
) {
165 klass
->handle_control(dev
, p
, request
, value
, index
, length
, data
);
169 void usb_device_handle_data(USBDevice
*dev
, USBPacket
*p
)
171 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
172 if (klass
->handle_data
) {
173 klass
->handle_data(dev
, p
);
177 const char *usb_device_get_product_desc(USBDevice
*dev
)
179 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
180 return klass
->product_desc
;
183 const USBDesc
*usb_device_get_usb_desc(USBDevice
*dev
)
185 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
187 return dev
->usb_desc
;
189 return klass
->usb_desc
;
192 void usb_device_set_interface(USBDevice
*dev
, int interface
,
193 int alt_old
, int alt_new
)
195 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
196 if (klass
->set_interface
) {
197 klass
->set_interface(dev
, interface
, alt_old
, alt_new
);
201 void usb_device_flush_ep_queue(USBDevice
*dev
, USBEndpoint
*ep
)
203 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
204 if (klass
->flush_ep_queue
) {
205 klass
->flush_ep_queue(dev
, ep
);
209 void usb_device_ep_stopped(USBDevice
*dev
, USBEndpoint
*ep
)
211 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
212 if (klass
->ep_stopped
) {
213 klass
->ep_stopped(dev
, ep
);
217 int usb_device_alloc_streams(USBDevice
*dev
, USBEndpoint
**eps
, int nr_eps
,
220 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
221 if (klass
->alloc_streams
) {
222 return klass
->alloc_streams(dev
, eps
, nr_eps
, streams
);
227 void usb_device_free_streams(USBDevice
*dev
, USBEndpoint
**eps
, int nr_eps
)
229 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
230 if (klass
->free_streams
) {
231 klass
->free_streams(dev
, eps
, nr_eps
);
235 static int usb_qdev_init(DeviceState
*qdev
)
237 USBDevice
*dev
= USB_DEVICE(qdev
);
240 pstrcpy(dev
->product_desc
, sizeof(dev
->product_desc
),
241 usb_device_get_product_desc(dev
));
242 dev
->auto_attach
= 1;
243 QLIST_INIT(&dev
->strings
);
245 rc
= usb_claim_port(dev
);
249 rc
= usb_device_init(dev
);
251 usb_release_port(dev
);
254 if (dev
->auto_attach
) {
255 rc
= usb_device_attach(dev
);
264 static int usb_qdev_exit(DeviceState
*qdev
)
266 USBDevice
*dev
= USB_DEVICE(qdev
);
269 usb_device_detach(dev
);
271 usb_device_handle_destroy(dev
);
273 usb_release_port(dev
);
278 typedef struct LegacyUSBFactory
281 const char *usbdevice_name
;
282 USBDevice
*(*usbdevice_init
)(USBBus
*bus
, const char *params
);
285 static GSList
*legacy_usb_factory
;
287 void usb_legacy_register(const char *typename
, const char *usbdevice_name
,
288 USBDevice
*(*usbdevice_init
)(USBBus
*bus
,
291 if (usbdevice_name
) {
292 LegacyUSBFactory
*f
= g_malloc0(sizeof(*f
));
294 f
->usbdevice_name
= usbdevice_name
;
295 f
->usbdevice_init
= usbdevice_init
;
296 legacy_usb_factory
= g_slist_append(legacy_usb_factory
, f
);
300 USBDevice
*usb_create(USBBus
*bus
, const char *name
)
304 dev
= qdev_create(&bus
->qbus
, name
);
305 return USB_DEVICE(dev
);
308 USBDevice
*usb_create_simple(USBBus
*bus
, const char *name
)
310 USBDevice
*dev
= usb_create(bus
, name
);
314 error_report("Failed to create USB device '%s'", name
);
317 rc
= qdev_init(&dev
->qdev
);
319 error_report("Failed to initialize USB device '%s'", name
);
325 static void usb_fill_port(USBPort
*port
, void *opaque
, int index
,
326 USBPortOps
*ops
, int speedmask
)
328 port
->opaque
= opaque
;
331 port
->speedmask
= speedmask
;
332 usb_port_location(port
, NULL
, index
+ 1);
335 void usb_register_port(USBBus
*bus
, USBPort
*port
, void *opaque
, int index
,
336 USBPortOps
*ops
, int speedmask
)
338 usb_fill_port(port
, opaque
, index
, ops
, speedmask
);
339 QTAILQ_INSERT_TAIL(&bus
->free
, port
, next
);
343 int usb_register_companion(const char *masterbus
, USBPort
*ports
[],
344 uint32_t portcount
, uint32_t firstport
,
345 void *opaque
, USBPortOps
*ops
, int speedmask
)
350 QTAILQ_FOREACH(bus
, &busses
, next
) {
351 if (strcmp(bus
->qbus
.name
, masterbus
) == 0) {
356 if (!bus
|| !bus
->ops
->register_companion
) {
357 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "masterbus",
360 error_printf_unless_qmp(
361 "USB bus '%s' does not allow companion controllers\n",
367 for (i
= 0; i
< portcount
; i
++) {
368 usb_fill_port(ports
[i
], opaque
, i
, ops
, speedmask
);
371 return bus
->ops
->register_companion(bus
, ports
, portcount
, firstport
);
374 void usb_port_location(USBPort
*downstream
, USBPort
*upstream
, int portnr
)
377 snprintf(downstream
->path
, sizeof(downstream
->path
), "%s.%d",
378 upstream
->path
, portnr
);
379 downstream
->hubcount
= upstream
->hubcount
+ 1;
381 snprintf(downstream
->path
, sizeof(downstream
->path
), "%d", portnr
);
382 downstream
->hubcount
= 0;
386 void usb_unregister_port(USBBus
*bus
, USBPort
*port
)
389 object_unparent(OBJECT(port
->dev
));
391 QTAILQ_REMOVE(&bus
->free
, port
, next
);
395 int usb_claim_port(USBDevice
*dev
)
397 USBBus
*bus
= usb_bus_from_device(dev
);
400 assert(dev
->port
== NULL
);
402 if (dev
->port_path
) {
403 QTAILQ_FOREACH(port
, &bus
->free
, next
) {
404 if (strcmp(port
->path
, dev
->port_path
) == 0) {
409 error_report("Error: usb port %s (bus %s) not found (in use?)",
410 dev
->port_path
, bus
->qbus
.name
);
414 if (bus
->nfree
== 1 && strcmp(object_get_typename(OBJECT(dev
)), "usb-hub") != 0) {
415 /* Create a new hub and chain it on */
416 usb_create_simple(bus
, "usb-hub");
418 if (bus
->nfree
== 0) {
419 error_report("Error: tried to attach usb device %s to a bus "
420 "with no free ports", dev
->product_desc
);
423 port
= QTAILQ_FIRST(&bus
->free
);
425 trace_usb_port_claim(bus
->busnr
, port
->path
);
427 QTAILQ_REMOVE(&bus
->free
, port
, next
);
433 QTAILQ_INSERT_TAIL(&bus
->used
, port
, next
);
438 void usb_release_port(USBDevice
*dev
)
440 USBBus
*bus
= usb_bus_from_device(dev
);
441 USBPort
*port
= dev
->port
;
443 assert(port
!= NULL
);
444 trace_usb_port_release(bus
->busnr
, port
->path
);
446 QTAILQ_REMOVE(&bus
->used
, port
, next
);
452 QTAILQ_INSERT_TAIL(&bus
->free
, port
, next
);
456 static void usb_mask_to_str(char *dest
, size_t size
,
457 unsigned int speedmask
)
459 static const struct {
463 { .mask
= USB_SPEED_MASK_FULL
, .name
= "full" },
464 { .mask
= USB_SPEED_MASK_HIGH
, .name
= "high" },
465 { .mask
= USB_SPEED_MASK_SUPER
, .name
= "super" },
469 for (i
= 0; i
< ARRAY_SIZE(speeds
); i
++) {
470 if (speeds
[i
].mask
& speedmask
) {
471 pos
+= snprintf(dest
+ pos
, size
- pos
, "%s%s",
478 int usb_device_attach(USBDevice
*dev
)
480 USBBus
*bus
= usb_bus_from_device(dev
);
481 USBPort
*port
= dev
->port
;
482 char devspeed
[32], portspeed
[32];
484 assert(port
!= NULL
);
485 assert(!dev
->attached
);
486 usb_mask_to_str(devspeed
, sizeof(devspeed
), dev
->speedmask
);
487 usb_mask_to_str(portspeed
, sizeof(portspeed
), port
->speedmask
);
488 trace_usb_port_attach(bus
->busnr
, port
->path
,
489 devspeed
, portspeed
);
491 if (!(port
->speedmask
& dev
->speedmask
)) {
492 error_report("Warning: speed mismatch trying to attach"
493 " usb device \"%s\" (%s speed)"
494 " to bus \"%s\", port \"%s\" (%s speed)",
495 dev
->product_desc
, devspeed
,
496 bus
->qbus
.name
, port
->path
, portspeed
);
506 int usb_device_detach(USBDevice
*dev
)
508 USBBus
*bus
= usb_bus_from_device(dev
);
509 USBPort
*port
= dev
->port
;
511 assert(port
!= NULL
);
512 assert(dev
->attached
);
513 trace_usb_port_detach(bus
->busnr
, port
->path
);
520 int usb_device_delete_addr(int busnr
, int addr
)
526 bus
= usb_bus_find(busnr
);
530 QTAILQ_FOREACH(port
, &bus
->used
, next
) {
531 if (port
->dev
->addr
== addr
)
538 object_unparent(OBJECT(dev
));
542 static const char *usb_speed(unsigned int speed
)
544 static const char *txt
[] = {
545 [ USB_SPEED_LOW
] = "1.5",
546 [ USB_SPEED_FULL
] = "12",
547 [ USB_SPEED_HIGH
] = "480",
548 [ USB_SPEED_SUPER
] = "5000",
550 if (speed
>= ARRAY_SIZE(txt
))
555 static void usb_bus_dev_print(Monitor
*mon
, DeviceState
*qdev
, int indent
)
557 USBDevice
*dev
= USB_DEVICE(qdev
);
558 USBBus
*bus
= usb_bus_from_device(dev
);
560 monitor_printf(mon
, "%*saddr %d.%d, port %s, speed %s, name %s%s\n",
561 indent
, "", bus
->busnr
, dev
->addr
,
562 dev
->port
? dev
->port
->path
: "-",
563 usb_speed(dev
->speed
), dev
->product_desc
,
564 dev
->attached
? ", attached" : "");
567 static char *usb_get_dev_path(DeviceState
*qdev
)
569 USBDevice
*dev
= USB_DEVICE(qdev
);
570 DeviceState
*hcd
= qdev
->parent_bus
->parent
;
573 if (dev
->flags
& (1 << USB_DEV_FLAG_FULL_PATH
)) {
574 id
= qdev_get_dev_path(hcd
);
577 char *ret
= g_strdup_printf("%s/%s", id
, dev
->port
->path
);
581 return g_strdup(dev
->port
->path
);
585 static char *usb_get_fw_dev_path(DeviceState
*qdev
)
587 USBDevice
*dev
= USB_DEVICE(qdev
);
589 ssize_t pos
= 0, fw_len
;
592 fw_len
= 32 + strlen(dev
->port
->path
) * 6;
593 fw_path
= g_malloc(fw_len
);
594 in
= dev
->port
->path
;
595 while (fw_len
- pos
> 0) {
596 nr
= strtol(in
, &in
, 10);
598 /* some hub between root port and device */
599 pos
+= snprintf(fw_path
+ pos
, fw_len
- pos
, "hub@%lx/", nr
);
602 /* the device itself */
603 pos
+= snprintf(fw_path
+ pos
, fw_len
- pos
, "%s@%lx",
604 qdev_fw_name(qdev
), nr
);
611 void usb_info(Monitor
*mon
, const QDict
*qdict
)
617 if (QTAILQ_EMPTY(&busses
)) {
618 monitor_printf(mon
, "USB support not enabled\n");
622 QTAILQ_FOREACH(bus
, &busses
, next
) {
623 QTAILQ_FOREACH(port
, &bus
->used
, next
) {
627 monitor_printf(mon
, " Device %d.%d, Port %s, Speed %s Mb/s, Product %s\n",
628 bus
->busnr
, dev
->addr
, port
->path
, usb_speed(dev
->speed
),
634 /* handle legacy -usbdevice cmd line option */
635 USBDevice
*usbdevice_create(const char *cmdline
)
637 USBBus
*bus
= usb_bus_find(-1 /* any */);
638 LegacyUSBFactory
*f
= NULL
;
644 params
= strchr(cmdline
,':');
647 len
= params
- cmdline
;
648 if (len
> sizeof(driver
))
649 len
= sizeof(driver
);
650 pstrcpy(driver
, len
, cmdline
);
653 pstrcpy(driver
, sizeof(driver
), cmdline
);
656 for (i
= legacy_usb_factory
; i
; i
= i
->next
) {
658 if (strcmp(f
->usbdevice_name
, driver
) == 0) {
664 /* no error because some drivers are not converted (yet) */
665 error_report("usbdevice %s not found", driver
);
671 error_report("Error: no usb bus to attach usbdevice %s, "
672 "please try -machine usb=on and check that "
673 "the machine model supports USB", driver
);
677 if (!f
->usbdevice_init
) {
679 error_report("usbdevice %s accepts no params", driver
);
682 return usb_create_simple(bus
, f
->name
);
684 return f
->usbdevice_init(bus
, params
);
687 static void usb_device_class_init(ObjectClass
*klass
, void *data
)
689 DeviceClass
*k
= DEVICE_CLASS(klass
);
690 k
->bus_type
= TYPE_USB_BUS
;
691 k
->init
= usb_qdev_init
;
692 k
->unplug
= qdev_simple_unplug_cb
;
693 k
->exit
= usb_qdev_exit
;
694 k
->props
= usb_props
;
697 static const TypeInfo usb_device_type_info
= {
698 .name
= TYPE_USB_DEVICE
,
699 .parent
= TYPE_DEVICE
,
700 .instance_size
= sizeof(USBDevice
),
702 .class_size
= sizeof(USBDeviceClass
),
703 .class_init
= usb_device_class_init
,
706 static void usb_register_types(void)
708 type_register_static(&usb_bus_info
);
709 type_register_static(&usb_device_type_info
);
712 type_init(usb_register_types
)