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 const VMStateDescription vmstate_usb_device
= {
43 .minimum_version_id
= 1,
44 .fields
= (VMStateField
[]) {
45 VMSTATE_UINT8(addr
, USBDevice
),
46 VMSTATE_INT32(state
, USBDevice
),
47 VMSTATE_INT32(remote_wakeup
, USBDevice
),
48 VMSTATE_INT32(setup_state
, USBDevice
),
49 VMSTATE_INT32(setup_len
, USBDevice
),
50 VMSTATE_INT32(setup_index
, USBDevice
),
51 VMSTATE_UINT8_ARRAY(setup_buf
, USBDevice
, 8),
52 VMSTATE_END_OF_LIST(),
56 void usb_bus_new(USBBus
*bus
, USBBusOps
*ops
, DeviceState
*host
)
58 qbus_create_inplace(&bus
->qbus
, TYPE_USB_BUS
, host
, NULL
);
60 bus
->busnr
= next_usb_bus
++;
61 bus
->qbus
.allow_hotplug
= 1; /* Yes, we can */
62 QTAILQ_INIT(&bus
->free
);
63 QTAILQ_INIT(&bus
->used
);
64 QTAILQ_INSERT_TAIL(&busses
, bus
, next
);
67 USBBus
*usb_bus_find(int busnr
)
72 return QTAILQ_FIRST(&busses
);
73 QTAILQ_FOREACH(bus
, &busses
, next
) {
74 if (bus
->busnr
== busnr
)
80 static int usb_device_init(USBDevice
*dev
)
82 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
84 return klass
->init(dev
);
89 USBDevice
*usb_device_find_device(USBDevice
*dev
, uint8_t addr
)
91 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
92 if (klass
->find_device
) {
93 return klass
->find_device(dev
, addr
);
98 static void usb_device_handle_destroy(USBDevice
*dev
)
100 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
101 if (klass
->handle_destroy
) {
102 klass
->handle_destroy(dev
);
106 void usb_device_cancel_packet(USBDevice
*dev
, USBPacket
*p
)
108 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
109 if (klass
->cancel_packet
) {
110 klass
->cancel_packet(dev
, p
);
114 void usb_device_handle_attach(USBDevice
*dev
)
116 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
117 if (klass
->handle_attach
) {
118 klass
->handle_attach(dev
);
122 void usb_device_handle_reset(USBDevice
*dev
)
124 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
125 if (klass
->handle_reset
) {
126 klass
->handle_reset(dev
);
130 int usb_device_handle_control(USBDevice
*dev
, USBPacket
*p
, int request
,
131 int value
, int index
, int length
, uint8_t *data
)
133 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
134 if (klass
->handle_control
) {
135 return klass
->handle_control(dev
, p
, request
, value
, index
, length
,
141 int usb_device_handle_data(USBDevice
*dev
, USBPacket
*p
)
143 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
144 if (klass
->handle_data
) {
145 return klass
->handle_data(dev
, p
);
150 const char *usb_device_get_product_desc(USBDevice
*dev
)
152 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
153 return klass
->product_desc
;
156 const USBDesc
*usb_device_get_usb_desc(USBDevice
*dev
)
158 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
159 return klass
->usb_desc
;
162 void usb_device_set_interface(USBDevice
*dev
, int interface
,
163 int alt_old
, int alt_new
)
165 USBDeviceClass
*klass
= USB_DEVICE_GET_CLASS(dev
);
166 if (klass
->set_interface
) {
167 klass
->set_interface(dev
, interface
, alt_old
, alt_new
);
171 static int usb_qdev_init(DeviceState
*qdev
)
173 USBDevice
*dev
= USB_DEVICE(qdev
);
176 pstrcpy(dev
->product_desc
, sizeof(dev
->product_desc
),
177 usb_device_get_product_desc(dev
));
178 dev
->auto_attach
= 1;
179 QLIST_INIT(&dev
->strings
);
181 rc
= usb_claim_port(dev
);
185 rc
= usb_device_init(dev
);
187 usb_release_port(dev
);
190 if (dev
->auto_attach
) {
191 rc
= usb_device_attach(dev
);
200 static int usb_qdev_exit(DeviceState
*qdev
)
202 USBDevice
*dev
= USB_DEVICE(qdev
);
205 usb_device_detach(dev
);
207 usb_device_handle_destroy(dev
);
209 usb_release_port(dev
);
214 typedef struct LegacyUSBFactory
217 const char *usbdevice_name
;
218 USBDevice
*(*usbdevice_init
)(USBBus
*bus
, const char *params
);
221 static GSList
*legacy_usb_factory
;
223 void usb_legacy_register(const char *typename
, const char *usbdevice_name
,
224 USBDevice
*(*usbdevice_init
)(USBBus
*bus
,
227 if (usbdevice_name
) {
228 LegacyUSBFactory
*f
= g_malloc0(sizeof(*f
));
230 f
->usbdevice_name
= usbdevice_name
;
231 f
->usbdevice_init
= usbdevice_init
;
232 legacy_usb_factory
= g_slist_append(legacy_usb_factory
, f
);
236 USBDevice
*usb_create(USBBus
*bus
, const char *name
)
240 dev
= qdev_create(&bus
->qbus
, name
);
241 return USB_DEVICE(dev
);
244 USBDevice
*usb_create_simple(USBBus
*bus
, const char *name
)
246 USBDevice
*dev
= usb_create(bus
, name
);
250 error_report("Failed to create USB device '%s'", name
);
253 rc
= qdev_init(&dev
->qdev
);
255 error_report("Failed to initialize USB device '%s'", name
);
261 static void usb_fill_port(USBPort
*port
, void *opaque
, int index
,
262 USBPortOps
*ops
, int speedmask
)
264 port
->opaque
= opaque
;
267 port
->speedmask
= speedmask
;
268 usb_port_location(port
, NULL
, index
+ 1);
271 void usb_register_port(USBBus
*bus
, USBPort
*port
, void *opaque
, int index
,
272 USBPortOps
*ops
, int speedmask
)
274 usb_fill_port(port
, opaque
, index
, ops
, speedmask
);
275 QTAILQ_INSERT_TAIL(&bus
->free
, port
, next
);
279 int usb_register_companion(const char *masterbus
, USBPort
*ports
[],
280 uint32_t portcount
, uint32_t firstport
,
281 void *opaque
, USBPortOps
*ops
, int speedmask
)
286 QTAILQ_FOREACH(bus
, &busses
, next
) {
287 if (strcmp(bus
->qbus
.name
, masterbus
) == 0) {
292 if (!bus
|| !bus
->ops
->register_companion
) {
293 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "masterbus",
296 error_printf_unless_qmp(
297 "USB bus '%s' does not allow companion controllers\n",
303 for (i
= 0; i
< portcount
; i
++) {
304 usb_fill_port(ports
[i
], opaque
, i
, ops
, speedmask
);
307 return bus
->ops
->register_companion(bus
, ports
, portcount
, firstport
);
310 void usb_port_location(USBPort
*downstream
, USBPort
*upstream
, int portnr
)
313 snprintf(downstream
->path
, sizeof(downstream
->path
), "%s.%d",
314 upstream
->path
, portnr
);
316 snprintf(downstream
->path
, sizeof(downstream
->path
), "%d", portnr
);
320 void usb_unregister_port(USBBus
*bus
, USBPort
*port
)
323 qdev_free(&port
->dev
->qdev
);
324 QTAILQ_REMOVE(&bus
->free
, port
, next
);
328 int usb_claim_port(USBDevice
*dev
)
330 USBBus
*bus
= usb_bus_from_device(dev
);
333 assert(dev
->port
== NULL
);
335 if (dev
->port_path
) {
336 QTAILQ_FOREACH(port
, &bus
->free
, next
) {
337 if (strcmp(port
->path
, dev
->port_path
) == 0) {
342 error_report("Error: usb port %s (bus %s) not found (in use?)",
343 dev
->port_path
, bus
->qbus
.name
);
347 if (bus
->nfree
== 1 && strcmp(object_get_typename(OBJECT(dev
)), "usb-hub") != 0) {
348 /* Create a new hub and chain it on */
349 usb_create_simple(bus
, "usb-hub");
351 if (bus
->nfree
== 0) {
352 error_report("Error: tried to attach usb device %s to a bus "
353 "with no free ports", dev
->product_desc
);
356 port
= QTAILQ_FIRST(&bus
->free
);
358 trace_usb_port_claim(bus
->busnr
, port
->path
);
360 QTAILQ_REMOVE(&bus
->free
, port
, next
);
366 QTAILQ_INSERT_TAIL(&bus
->used
, port
, next
);
371 void usb_release_port(USBDevice
*dev
)
373 USBBus
*bus
= usb_bus_from_device(dev
);
374 USBPort
*port
= dev
->port
;
376 assert(port
!= NULL
);
377 trace_usb_port_release(bus
->busnr
, port
->path
);
379 QTAILQ_REMOVE(&bus
->used
, port
, next
);
385 QTAILQ_INSERT_TAIL(&bus
->free
, port
, next
);
389 int usb_device_attach(USBDevice
*dev
)
391 USBBus
*bus
= usb_bus_from_device(dev
);
392 USBPort
*port
= dev
->port
;
394 assert(port
!= NULL
);
395 assert(!dev
->attached
);
396 trace_usb_port_attach(bus
->busnr
, port
->path
);
398 if (!(port
->speedmask
& dev
->speedmask
)) {
399 error_report("Warning: speed mismatch trying to attach "
400 "usb device %s to bus %s",
401 dev
->product_desc
, bus
->qbus
.name
);
411 int usb_device_detach(USBDevice
*dev
)
413 USBBus
*bus
= usb_bus_from_device(dev
);
414 USBPort
*port
= dev
->port
;
416 assert(port
!= NULL
);
417 assert(dev
->attached
);
418 trace_usb_port_detach(bus
->busnr
, port
->path
);
425 int usb_device_delete_addr(int busnr
, int addr
)
431 bus
= usb_bus_find(busnr
);
435 QTAILQ_FOREACH(port
, &bus
->used
, next
) {
436 if (port
->dev
->addr
== addr
)
443 qdev_free(&dev
->qdev
);
447 static const char *usb_speed(unsigned int speed
)
449 static const char *txt
[] = {
450 [ USB_SPEED_LOW
] = "1.5",
451 [ USB_SPEED_FULL
] = "12",
452 [ USB_SPEED_HIGH
] = "480",
453 [ USB_SPEED_SUPER
] = "5000",
455 if (speed
>= ARRAY_SIZE(txt
))
460 static void usb_bus_dev_print(Monitor
*mon
, DeviceState
*qdev
, int indent
)
462 USBDevice
*dev
= USB_DEVICE(qdev
);
463 USBBus
*bus
= usb_bus_from_device(dev
);
465 monitor_printf(mon
, "%*saddr %d.%d, port %s, speed %s, name %s%s\n",
466 indent
, "", bus
->busnr
, dev
->addr
,
467 dev
->port
? dev
->port
->path
: "-",
468 usb_speed(dev
->speed
), dev
->product_desc
,
469 dev
->attached
? ", attached" : "");
472 static char *usb_get_dev_path(DeviceState
*qdev
)
474 USBDevice
*dev
= USB_DEVICE(qdev
);
475 DeviceState
*hcd
= qdev
->parent_bus
->parent
;
478 if (dev
->flags
& (1 << USB_DEV_FLAG_FULL_PATH
)) {
479 id
= qdev_get_dev_path(hcd
);
482 char *ret
= g_strdup_printf("%s/%s", id
, dev
->port
->path
);
486 return g_strdup(dev
->port
->path
);
490 static char *usb_get_fw_dev_path(DeviceState
*qdev
)
492 USBDevice
*dev
= USB_DEVICE(qdev
);
494 ssize_t pos
= 0, fw_len
;
497 fw_len
= 32 + strlen(dev
->port
->path
) * 6;
498 fw_path
= g_malloc(fw_len
);
499 in
= dev
->port
->path
;
500 while (fw_len
- pos
> 0) {
501 nr
= strtol(in
, &in
, 10);
503 /* some hub between root port and device */
504 pos
+= snprintf(fw_path
+ pos
, fw_len
- pos
, "hub@%ld/", nr
);
507 /* the device itself */
508 pos
+= snprintf(fw_path
+ pos
, fw_len
- pos
, "%s@%ld",
509 qdev_fw_name(qdev
), nr
);
516 void usb_info(Monitor
*mon
)
522 if (QTAILQ_EMPTY(&busses
)) {
523 monitor_printf(mon
, "USB support not enabled\n");
527 QTAILQ_FOREACH(bus
, &busses
, next
) {
528 QTAILQ_FOREACH(port
, &bus
->used
, next
) {
532 monitor_printf(mon
, " Device %d.%d, Port %s, Speed %s Mb/s, Product %s\n",
533 bus
->busnr
, dev
->addr
, port
->path
, usb_speed(dev
->speed
),
539 /* handle legacy -usbdevice cmd line option */
540 USBDevice
*usbdevice_create(const char *cmdline
)
542 USBBus
*bus
= usb_bus_find(-1 /* any */);
543 LegacyUSBFactory
*f
= NULL
;
549 params
= strchr(cmdline
,':');
552 len
= params
- cmdline
;
553 if (len
> sizeof(driver
))
554 len
= sizeof(driver
);
555 pstrcpy(driver
, len
, cmdline
);
558 pstrcpy(driver
, sizeof(driver
), cmdline
);
561 for (i
= legacy_usb_factory
; i
; i
= i
->next
) {
563 if (strcmp(f
->usbdevice_name
, driver
) == 0) {
569 /* no error because some drivers are not converted (yet) */
570 error_report("usbdevice %s not found", driver
);
575 if (!f
->usbdevice_init
) {
577 error_report("usbdevice %s accepts no params", driver
);
580 return usb_create_simple(bus
, f
->name
);
582 return f
->usbdevice_init(bus
, params
);
585 static void usb_device_class_init(ObjectClass
*klass
, void *data
)
587 DeviceClass
*k
= DEVICE_CLASS(klass
);
588 k
->bus_type
= TYPE_USB_BUS
;
589 k
->init
= usb_qdev_init
;
590 k
->unplug
= qdev_simple_unplug_cb
;
591 k
->exit
= usb_qdev_exit
;
592 k
->props
= usb_props
;
595 static TypeInfo usb_device_type_info
= {
596 .name
= TYPE_USB_DEVICE
,
597 .parent
= TYPE_DEVICE
,
598 .instance_size
= sizeof(USBDevice
),
600 .class_size
= sizeof(USBDeviceClass
),
601 .class_init
= usb_device_class_init
,
604 static void usb_register_types(void)
606 type_register_static(&usb_bus_info
);
607 type_register_static(&usb_device_type_info
);
610 type_init(usb_register_types
)