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 struct BusInfo usb_bus_info
= {
16 .size
= sizeof(USBBus
),
17 .print_dev
= usb_bus_dev_print
,
18 .get_dev_path
= usb_get_dev_path
,
19 .get_fw_dev_path
= usb_get_fw_dev_path
,
20 .props
= (Property
[]) {
21 DEFINE_PROP_STRING("port", USBDevice
, port_path
),
22 DEFINE_PROP_BIT("full-path", USBDevice
, flags
,
23 USB_DEV_FLAG_FULL_PATH
, true),
24 DEFINE_PROP_END_OF_LIST()
27 static int next_usb_bus
= 0;
28 static QTAILQ_HEAD(, USBBus
) busses
= QTAILQ_HEAD_INITIALIZER(busses
);
30 const VMStateDescription vmstate_usb_device
= {
33 .minimum_version_id
= 1,
34 .fields
= (VMStateField
[]) {
35 VMSTATE_UINT8(addr
, USBDevice
),
36 VMSTATE_INT32(state
, USBDevice
),
37 VMSTATE_INT32(remote_wakeup
, USBDevice
),
38 VMSTATE_INT32(setup_state
, USBDevice
),
39 VMSTATE_INT32(setup_len
, USBDevice
),
40 VMSTATE_INT32(setup_index
, USBDevice
),
41 VMSTATE_UINT8_ARRAY(setup_buf
, USBDevice
, 8),
42 VMSTATE_END_OF_LIST(),
46 void usb_bus_new(USBBus
*bus
, USBBusOps
*ops
, DeviceState
*host
)
48 qbus_create_inplace(&bus
->qbus
, &usb_bus_info
, host
, NULL
);
50 bus
->busnr
= next_usb_bus
++;
51 bus
->qbus
.allow_hotplug
= 1; /* Yes, we can */
52 QTAILQ_INIT(&bus
->free
);
53 QTAILQ_INIT(&bus
->used
);
54 QTAILQ_INSERT_TAIL(&busses
, bus
, next
);
57 USBBus
*usb_bus_find(int busnr
)
62 return QTAILQ_FIRST(&busses
);
63 QTAILQ_FOREACH(bus
, &busses
, next
) {
64 if (bus
->busnr
== busnr
)
70 static int usb_device_init(USBDevice
*dev
)
72 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
74 return klass
->init(dev
);
79 USBDevice
*usb_device_find_device(USBDevice
*dev
, uint8_t addr
)
81 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
82 if (klass
->find_device
) {
83 return klass
->find_device(dev
, addr
);
88 static void usb_device_handle_destroy(USBDevice
*dev
)
90 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
91 if (klass
->handle_destroy
) {
92 klass
->handle_destroy(dev
);
96 void usb_device_cancel_packet(USBDevice
*dev
, USBPacket
*p
)
98 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
99 if (klass
->cancel_packet
) {
100 klass
->cancel_packet(dev
, p
);
104 void usb_device_handle_attach(USBDevice
*dev
)
106 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
107 if (klass
->handle_attach
) {
108 klass
->handle_attach(dev
);
112 void usb_device_handle_reset(USBDevice
*dev
)
114 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
115 if (klass
->handle_reset
) {
116 klass
->handle_reset(dev
);
120 int usb_device_handle_control(USBDevice
*dev
, USBPacket
*p
, int request
,
121 int value
, int index
, int length
, uint8_t *data
)
123 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
124 if (klass
->handle_control
) {
125 return klass
->handle_control(dev
, p
, request
, value
, index
, length
,
131 int usb_device_handle_data(USBDevice
*dev
, USBPacket
*p
)
133 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
134 if (klass
->handle_data
) {
135 return klass
->handle_data(dev
, p
);
140 const char *usb_device_get_product_desc(USBDevice
*dev
)
142 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
143 return klass
->product_desc
;
146 const USBDesc
*usb_device_get_usb_desc(USBDevice
*dev
)
148 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
149 return klass
->usb_desc
;
152 void usb_device_set_interface(USBDevice
*dev
, int interface
,
153 int alt_old
, int alt_new
)
155 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
156 if (klass
->set_interface
) {
157 klass
->set_interface(dev
, interface
, alt_old
, alt_new
);
161 static int usb_qdev_init(DeviceState
*qdev
)
163 USBDevice
*dev
= USB_DEVICE(qdev
);
166 pstrcpy(dev
->product_desc
, sizeof(dev
->product_desc
),
167 usb_device_get_product_desc(dev
));
168 dev
->auto_attach
= 1;
169 QLIST_INIT(&dev
->strings
);
171 rc
= usb_claim_port(dev
);
175 rc
= usb_device_init(dev
);
177 usb_release_port(dev
);
180 if (dev
->auto_attach
) {
181 rc
= usb_device_attach(dev
);
190 static int usb_qdev_exit(DeviceState
*qdev
)
192 USBDevice
*dev
= USB_DEVICE(qdev
);
195 usb_device_detach(dev
);
197 usb_device_handle_destroy(dev
);
199 usb_release_port(dev
);
204 typedef struct LegacyUSBFactory
207 const char *usbdevice_name
;
208 USBDevice
*(*usbdevice_init
)(USBBus
*bus
, const char *params
);
211 static GSList
*legacy_usb_factory
;
213 void usb_legacy_register(const char *typename
, const char *usbdevice_name
,
214 USBDevice
*(*usbdevice_init
)(USBBus
*bus
,
217 if (usbdevice_name
) {
218 LegacyUSBFactory
*f
= g_malloc0(sizeof(*f
));
220 f
->usbdevice_name
= usbdevice_name
;
221 f
->usbdevice_init
= usbdevice_init
;
222 legacy_usb_factory
= g_slist_append(legacy_usb_factory
, f
);
226 USBDevice
*usb_create(USBBus
*bus
, const char *name
)
230 dev
= qdev_create(&bus
->qbus
, name
);
231 return USB_DEVICE(dev
);
234 USBDevice
*usb_create_simple(USBBus
*bus
, const char *name
)
236 USBDevice
*dev
= usb_create(bus
, name
);
240 error_report("Failed to create USB device '%s'", name
);
243 rc
= qdev_init(&dev
->qdev
);
245 error_report("Failed to initialize USB device '%s'", name
);
251 static void usb_fill_port(USBPort
*port
, void *opaque
, int index
,
252 USBPortOps
*ops
, int speedmask
)
254 port
->opaque
= opaque
;
257 port
->speedmask
= speedmask
;
258 usb_port_location(port
, NULL
, index
+ 1);
261 void usb_register_port(USBBus
*bus
, USBPort
*port
, void *opaque
, int index
,
262 USBPortOps
*ops
, int speedmask
)
264 usb_fill_port(port
, opaque
, index
, ops
, speedmask
);
265 QTAILQ_INSERT_TAIL(&bus
->free
, port
, next
);
269 int usb_register_companion(const char *masterbus
, USBPort
*ports
[],
270 uint32_t portcount
, uint32_t firstport
,
271 void *opaque
, USBPortOps
*ops
, int speedmask
)
276 QTAILQ_FOREACH(bus
, &busses
, next
) {
277 if (strcmp(bus
->qbus
.name
, masterbus
) == 0) {
282 if (!bus
|| !bus
->ops
->register_companion
) {
283 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "masterbus",
286 error_printf_unless_qmp(
287 "USB bus '%s' does not allow companion controllers\n",
293 for (i
= 0; i
< portcount
; i
++) {
294 usb_fill_port(ports
[i
], opaque
, i
, ops
, speedmask
);
297 return bus
->ops
->register_companion(bus
, ports
, portcount
, firstport
);
300 void usb_port_location(USBPort
*downstream
, USBPort
*upstream
, int portnr
)
303 snprintf(downstream
->path
, sizeof(downstream
->path
), "%s.%d",
304 upstream
->path
, portnr
);
306 snprintf(downstream
->path
, sizeof(downstream
->path
), "%d", portnr
);
310 void usb_unregister_port(USBBus
*bus
, USBPort
*port
)
313 qdev_free(&port
->dev
->qdev
);
314 QTAILQ_REMOVE(&bus
->free
, port
, next
);
318 int usb_claim_port(USBDevice
*dev
)
320 USBBus
*bus
= usb_bus_from_device(dev
);
323 assert(dev
->port
== NULL
);
325 if (dev
->port_path
) {
326 QTAILQ_FOREACH(port
, &bus
->free
, next
) {
327 if (strcmp(port
->path
, dev
->port_path
) == 0) {
332 error_report("Error: usb port %s (bus %s) not found (in use?)",
333 dev
->port_path
, bus
->qbus
.name
);
337 if (bus
->nfree
== 1 && strcmp(object_get_typename(OBJECT(dev
)), "usb-hub") != 0) {
338 /* Create a new hub and chain it on */
339 usb_create_simple(bus
, "usb-hub");
341 if (bus
->nfree
== 0) {
342 error_report("Error: tried to attach usb device %s to a bus "
343 "with no free ports", dev
->product_desc
);
346 port
= QTAILQ_FIRST(&bus
->free
);
348 trace_usb_port_claim(bus
->busnr
, port
->path
);
350 QTAILQ_REMOVE(&bus
->free
, port
, next
);
356 QTAILQ_INSERT_TAIL(&bus
->used
, port
, next
);
361 void usb_release_port(USBDevice
*dev
)
363 USBBus
*bus
= usb_bus_from_device(dev
);
364 USBPort
*port
= dev
->port
;
366 assert(port
!= NULL
);
367 trace_usb_port_release(bus
->busnr
, port
->path
);
369 QTAILQ_REMOVE(&bus
->used
, port
, next
);
375 QTAILQ_INSERT_TAIL(&bus
->free
, port
, next
);
379 int usb_device_attach(USBDevice
*dev
)
381 USBBus
*bus
= usb_bus_from_device(dev
);
382 USBPort
*port
= dev
->port
;
384 assert(port
!= NULL
);
385 assert(!dev
->attached
);
386 trace_usb_port_attach(bus
->busnr
, port
->path
);
388 if (!(port
->speedmask
& dev
->speedmask
)) {
389 error_report("Warning: speed mismatch trying to attach "
390 "usb device %s to bus %s",
391 dev
->product_desc
, bus
->qbus
.name
);
401 int usb_device_detach(USBDevice
*dev
)
403 USBBus
*bus
= usb_bus_from_device(dev
);
404 USBPort
*port
= dev
->port
;
406 assert(port
!= NULL
);
407 assert(dev
->attached
);
408 trace_usb_port_detach(bus
->busnr
, port
->path
);
415 int usb_device_delete_addr(int busnr
, int addr
)
421 bus
= usb_bus_find(busnr
);
425 QTAILQ_FOREACH(port
, &bus
->used
, next
) {
426 if (port
->dev
->addr
== addr
)
433 qdev_free(&dev
->qdev
);
437 static const char *usb_speed(unsigned int speed
)
439 static const char *txt
[] = {
440 [ USB_SPEED_LOW
] = "1.5",
441 [ USB_SPEED_FULL
] = "12",
442 [ USB_SPEED_HIGH
] = "480",
443 [ USB_SPEED_SUPER
] = "5000",
445 if (speed
>= ARRAY_SIZE(txt
))
450 static void usb_bus_dev_print(Monitor
*mon
, DeviceState
*qdev
, int indent
)
452 USBDevice
*dev
= USB_DEVICE(qdev
);
453 USBBus
*bus
= usb_bus_from_device(dev
);
455 monitor_printf(mon
, "%*saddr %d.%d, port %s, speed %s, name %s%s\n",
456 indent
, "", bus
->busnr
, dev
->addr
,
457 dev
->port
? dev
->port
->path
: "-",
458 usb_speed(dev
->speed
), dev
->product_desc
,
459 dev
->attached
? ", attached" : "");
462 static char *usb_get_dev_path(DeviceState
*qdev
)
464 USBDevice
*dev
= USB_DEVICE(qdev
);
465 DeviceState
*hcd
= qdev
->parent_bus
->parent
;
468 if ((dev
->flags
& (1 << USB_DEV_FLAG_FULL_PATH
)) &&
469 hcd
&& hcd
->parent_bus
&& hcd
->parent_bus
->info
->get_dev_path
) {
470 id
= hcd
->parent_bus
->info
->get_dev_path(hcd
);
473 char *ret
= g_strdup_printf("%s/%s", id
, dev
->port
->path
);
477 return g_strdup(dev
->port
->path
);
481 static char *usb_get_fw_dev_path(DeviceState
*qdev
)
483 USBDevice
*dev
= USB_DEVICE(qdev
);
485 ssize_t pos
= 0, fw_len
;
488 fw_len
= 32 + strlen(dev
->port
->path
) * 6;
489 fw_path
= g_malloc(fw_len
);
490 in
= dev
->port
->path
;
491 while (fw_len
- pos
> 0) {
492 nr
= strtol(in
, &in
, 10);
494 /* some hub between root port and device */
495 pos
+= snprintf(fw_path
+ pos
, fw_len
- pos
, "hub@%ld/", nr
);
498 /* the device itself */
499 pos
+= snprintf(fw_path
+ pos
, fw_len
- pos
, "%s@%ld",
500 qdev_fw_name(qdev
), nr
);
507 void usb_info(Monitor
*mon
)
513 if (QTAILQ_EMPTY(&busses
)) {
514 monitor_printf(mon
, "USB support not enabled\n");
518 QTAILQ_FOREACH(bus
, &busses
, next
) {
519 QTAILQ_FOREACH(port
, &bus
->used
, next
) {
523 monitor_printf(mon
, " Device %d.%d, Port %s, Speed %s Mb/s, Product %s\n",
524 bus
->busnr
, dev
->addr
, port
->path
, usb_speed(dev
->speed
),
530 /* handle legacy -usbdevice cmd line option */
531 USBDevice
*usbdevice_create(const char *cmdline
)
533 USBBus
*bus
= usb_bus_find(-1 /* any */);
534 LegacyUSBFactory
*f
= NULL
;
540 params
= strchr(cmdline
,':');
543 len
= params
- cmdline
;
544 if (len
> sizeof(driver
))
545 len
= sizeof(driver
);
546 pstrcpy(driver
, len
, cmdline
);
549 pstrcpy(driver
, sizeof(driver
), cmdline
);
552 for (i
= legacy_usb_factory
; i
; i
= i
->next
) {
554 if (strcmp(f
->usbdevice_name
, driver
) == 0) {
560 /* no error because some drivers are not converted (yet) */
561 error_report("usbdevice %s not found", driver
);
566 if (!f
->usbdevice_init
) {
568 error_report("usbdevice %s accepts no params", driver
);
571 return usb_create_simple(bus
, f
->name
);
573 return f
->usbdevice_init(bus
, params
);
576 static void usb_device_class_init(ObjectClass
*klass
, void *data
)
578 DeviceClass
*k
= DEVICE_CLASS(klass
);
579 k
->bus_info
= &usb_bus_info
;
580 k
->init
= usb_qdev_init
;
581 k
->unplug
= qdev_simple_unplug_cb
;
582 k
->exit
= usb_qdev_exit
;
585 static TypeInfo usb_device_type_info
= {
586 .name
= TYPE_USB_DEVICE
,
587 .parent
= TYPE_DEVICE
,
588 .instance_size
= sizeof(USBDevice
),
590 .class_size
= sizeof(USBDeviceClass
),
591 .class_init
= usb_device_class_init
,
594 static void usb_register_types(void)
596 type_register_static(&usb_device_type_info
);
599 type_init(usb_register_types
)