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
);
13 static struct BusInfo usb_bus_info
= {
15 .size
= sizeof(USBBus
),
16 .print_dev
= usb_bus_dev_print
,
17 .get_dev_path
= usb_get_dev_path
,
18 .get_fw_dev_path
= usb_get_fw_dev_path
,
19 .props
= (Property
[]) {
20 DEFINE_PROP_STRING("port", USBDevice
, port_path
),
21 DEFINE_PROP_END_OF_LIST()
24 static int next_usb_bus
= 0;
25 static QTAILQ_HEAD(, USBBus
) busses
= QTAILQ_HEAD_INITIALIZER(busses
);
27 const VMStateDescription vmstate_usb_device
= {
30 .minimum_version_id
= 1,
31 .fields
= (VMStateField
[]) {
32 VMSTATE_UINT8(addr
, USBDevice
),
33 VMSTATE_INT32(state
, USBDevice
),
34 VMSTATE_INT32(remote_wakeup
, USBDevice
),
35 VMSTATE_INT32(setup_state
, USBDevice
),
36 VMSTATE_INT32(setup_len
, USBDevice
),
37 VMSTATE_INT32(setup_index
, USBDevice
),
38 VMSTATE_UINT8_ARRAY(setup_buf
, USBDevice
, 8),
39 VMSTATE_END_OF_LIST(),
43 void usb_bus_new(USBBus
*bus
, USBBusOps
*ops
, DeviceState
*host
)
45 qbus_create_inplace(&bus
->qbus
, &usb_bus_info
, host
, NULL
);
47 bus
->busnr
= next_usb_bus
++;
48 bus
->qbus
.allow_hotplug
= 1; /* Yes, we can */
49 QTAILQ_INIT(&bus
->free
);
50 QTAILQ_INIT(&bus
->used
);
51 QTAILQ_INSERT_TAIL(&busses
, bus
, next
);
54 USBBus
*usb_bus_find(int busnr
)
59 return QTAILQ_FIRST(&busses
);
60 QTAILQ_FOREACH(bus
, &busses
, next
) {
61 if (bus
->busnr
== busnr
)
67 static int usb_qdev_init(DeviceState
*qdev
, DeviceInfo
*base
)
69 USBDevice
*dev
= DO_UPCAST(USBDevice
, qdev
, qdev
);
70 USBDeviceInfo
*info
= DO_UPCAST(USBDeviceInfo
, qdev
, base
);
73 pstrcpy(dev
->product_desc
, sizeof(dev
->product_desc
), info
->product_desc
);
76 QLIST_INIT(&dev
->strings
);
77 rc
= usb_claim_port(dev
);
79 rc
= dev
->info
->init(dev
);
81 if (rc
== 0 && dev
->auto_attach
) {
82 rc
= usb_device_attach(dev
);
87 static int usb_qdev_exit(DeviceState
*qdev
)
89 USBDevice
*dev
= DO_UPCAST(USBDevice
, qdev
, qdev
);
92 usb_device_detach(dev
);
94 if (dev
->info
->handle_destroy
) {
95 dev
->info
->handle_destroy(dev
);
98 usb_release_port(dev
);
103 void usb_qdev_register(USBDeviceInfo
*info
)
105 info
->qdev
.bus_info
= &usb_bus_info
;
106 info
->qdev
.init
= usb_qdev_init
;
107 info
->qdev
.unplug
= qdev_simple_unplug_cb
;
108 info
->qdev
.exit
= usb_qdev_exit
;
109 qdev_register(&info
->qdev
);
112 void usb_qdev_register_many(USBDeviceInfo
*info
)
114 while (info
->qdev
.name
) {
115 usb_qdev_register(info
);
120 USBDevice
*usb_create(USBBus
*bus
, const char *name
)
125 /* temporary stopgap until all usb is properly qdev-ified */
127 bus
= usb_bus_find(-1);
130 error_report("%s: no bus specified, using \"%s\" for \"%s\"\n",
131 __FUNCTION__
, bus
->qbus
.name
, name
);
135 dev
= qdev_create(&bus
->qbus
, name
);
136 return DO_UPCAST(USBDevice
, qdev
, dev
);
139 USBDevice
*usb_create_simple(USBBus
*bus
, const char *name
)
141 USBDevice
*dev
= usb_create(bus
, name
);
143 hw_error("Failed to create USB device '%s'\n", name
);
145 qdev_init_nofail(&dev
->qdev
);
149 static void usb_fill_port(USBPort
*port
, void *opaque
, int index
,
150 USBPortOps
*ops
, int speedmask
)
152 port
->opaque
= opaque
;
155 port
->speedmask
= speedmask
;
156 usb_port_location(port
, NULL
, index
+ 1);
159 void usb_register_port(USBBus
*bus
, USBPort
*port
, void *opaque
, int index
,
160 USBPortOps
*ops
, int speedmask
)
162 usb_fill_port(port
, opaque
, index
, ops
, speedmask
);
163 QTAILQ_INSERT_TAIL(&bus
->free
, port
, next
);
167 int usb_register_companion(const char *masterbus
, USBPort
*ports
[],
168 uint32_t portcount
, uint32_t firstport
,
169 void *opaque
, USBPortOps
*ops
, int speedmask
)
174 QTAILQ_FOREACH(bus
, &busses
, next
) {
175 if (strcmp(bus
->qbus
.name
, masterbus
) == 0) {
180 if (!bus
|| !bus
->ops
->register_companion
) {
181 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "masterbus",
184 error_printf_unless_qmp(
185 "USB bus '%s' does not allow companion controllers\n",
191 for (i
= 0; i
< portcount
; i
++) {
192 usb_fill_port(ports
[i
], opaque
, i
, ops
, speedmask
);
195 return bus
->ops
->register_companion(bus
, ports
, portcount
, firstport
);
198 void usb_port_location(USBPort
*downstream
, USBPort
*upstream
, int portnr
)
201 snprintf(downstream
->path
, sizeof(downstream
->path
), "%s.%d",
202 upstream
->path
, portnr
);
204 snprintf(downstream
->path
, sizeof(downstream
->path
), "%d", portnr
);
208 void usb_unregister_port(USBBus
*bus
, USBPort
*port
)
211 qdev_free(&port
->dev
->qdev
);
212 QTAILQ_REMOVE(&bus
->free
, port
, next
);
216 int usb_claim_port(USBDevice
*dev
)
218 USBBus
*bus
= usb_bus_from_device(dev
);
221 assert(dev
->port
== NULL
);
223 if (dev
->port_path
) {
224 QTAILQ_FOREACH(port
, &bus
->free
, next
) {
225 if (strcmp(port
->path
, dev
->port_path
) == 0) {
230 error_report("Error: usb port %s (bus %s) not found (in use?)\n",
231 dev
->port_path
, bus
->qbus
.name
);
235 if (bus
->nfree
== 1 && strcmp(dev
->qdev
.info
->name
, "usb-hub") != 0) {
236 /* Create a new hub and chain it on */
237 usb_create_simple(bus
, "usb-hub");
239 if (bus
->nfree
== 0) {
240 error_report("Error: tried to attach usb device %s to a bus "
241 "with no free ports\n", dev
->product_desc
);
244 port
= QTAILQ_FIRST(&bus
->free
);
246 trace_usb_port_claim(bus
->busnr
, port
->path
);
248 QTAILQ_REMOVE(&bus
->free
, port
, next
);
254 QTAILQ_INSERT_TAIL(&bus
->used
, port
, next
);
259 void usb_release_port(USBDevice
*dev
)
261 USBBus
*bus
= usb_bus_from_device(dev
);
262 USBPort
*port
= dev
->port
;
264 assert(port
!= NULL
);
265 trace_usb_port_release(bus
->busnr
, port
->path
);
267 QTAILQ_REMOVE(&bus
->used
, port
, next
);
273 QTAILQ_INSERT_TAIL(&bus
->free
, port
, next
);
277 int usb_device_attach(USBDevice
*dev
)
279 USBBus
*bus
= usb_bus_from_device(dev
);
280 USBPort
*port
= dev
->port
;
282 assert(port
!= NULL
);
283 assert(!dev
->attached
);
284 trace_usb_port_attach(bus
->busnr
, port
->path
);
286 if (!(port
->speedmask
& dev
->speedmask
)) {
287 error_report("Warning: speed mismatch trying to attach "
288 "usb device %s to bus %s\n",
289 dev
->product_desc
, bus
->qbus
.name
);
299 int usb_device_detach(USBDevice
*dev
)
301 USBBus
*bus
= usb_bus_from_device(dev
);
302 USBPort
*port
= dev
->port
;
304 assert(port
!= NULL
);
305 assert(dev
->attached
);
306 trace_usb_port_detach(bus
->busnr
, port
->path
);
313 int usb_device_delete_addr(int busnr
, int addr
)
319 bus
= usb_bus_find(busnr
);
323 QTAILQ_FOREACH(port
, &bus
->used
, next
) {
324 if (port
->dev
->addr
== addr
)
331 qdev_free(&dev
->qdev
);
335 static const char *usb_speed(unsigned int speed
)
337 static const char *txt
[] = {
338 [ USB_SPEED_LOW
] = "1.5",
339 [ USB_SPEED_FULL
] = "12",
340 [ USB_SPEED_HIGH
] = "480",
341 [ USB_SPEED_SUPER
] = "5000",
343 if (speed
>= ARRAY_SIZE(txt
))
348 static void usb_bus_dev_print(Monitor
*mon
, DeviceState
*qdev
, int indent
)
350 USBDevice
*dev
= DO_UPCAST(USBDevice
, qdev
, qdev
);
351 USBBus
*bus
= usb_bus_from_device(dev
);
353 monitor_printf(mon
, "%*saddr %d.%d, port %s, speed %s, name %s%s\n",
354 indent
, "", bus
->busnr
, dev
->addr
,
355 dev
->port
? dev
->port
->path
: "-",
356 usb_speed(dev
->speed
), dev
->product_desc
,
357 dev
->attached
? ", attached" : "");
360 static char *usb_get_dev_path(DeviceState
*qdev
)
362 USBDevice
*dev
= DO_UPCAST(USBDevice
, qdev
, qdev
);
363 return g_strdup(dev
->port
->path
);
366 static char *usb_get_fw_dev_path(DeviceState
*qdev
)
368 USBDevice
*dev
= DO_UPCAST(USBDevice
, qdev
, qdev
);
370 ssize_t pos
= 0, fw_len
;
373 fw_len
= 32 + strlen(dev
->port
->path
) * 6;
374 fw_path
= g_malloc(fw_len
);
375 in
= dev
->port
->path
;
376 while (fw_len
- pos
> 0) {
377 nr
= strtol(in
, &in
, 10);
379 /* some hub between root port and device */
380 pos
+= snprintf(fw_path
+ pos
, fw_len
- pos
, "hub@%ld/", nr
);
383 /* the device itself */
384 pos
+= snprintf(fw_path
+ pos
, fw_len
- pos
, "%s@%ld",
385 qdev_fw_name(qdev
), nr
);
392 void usb_info(Monitor
*mon
)
398 if (QTAILQ_EMPTY(&busses
)) {
399 monitor_printf(mon
, "USB support not enabled\n");
403 QTAILQ_FOREACH(bus
, &busses
, next
) {
404 QTAILQ_FOREACH(port
, &bus
->used
, next
) {
408 monitor_printf(mon
, " Device %d.%d, Port %s, Speed %s Mb/s, Product %s\n",
409 bus
->busnr
, dev
->addr
, port
->path
, usb_speed(dev
->speed
),
415 /* handle legacy -usbdevice cmd line option */
416 USBDevice
*usbdevice_create(const char *cmdline
)
418 USBBus
*bus
= usb_bus_find(-1 /* any */);
425 params
= strchr(cmdline
,':');
428 len
= params
- cmdline
;
429 if (len
> sizeof(driver
))
430 len
= sizeof(driver
);
431 pstrcpy(driver
, len
, cmdline
);
434 pstrcpy(driver
, sizeof(driver
), cmdline
);
437 for (info
= device_info_list
; info
!= NULL
; info
= info
->next
) {
438 if (info
->bus_info
!= &usb_bus_info
)
440 usb
= DO_UPCAST(USBDeviceInfo
, qdev
, info
);
441 if (usb
->usbdevice_name
== NULL
)
443 if (strcmp(usb
->usbdevice_name
, driver
) != 0)
449 /* no error because some drivers are not converted (yet) */
450 error_report("usbdevice %s not found", driver
);
455 if (!usb
->usbdevice_init
) {
457 error_report("usbdevice %s accepts no params", driver
);
460 return usb_create_simple(bus
, usb
->qdev
.name
);
462 return usb
->usbdevice_init(params
);