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
);
145 error_report("Failed to create USB device '%s'\n", name
);
148 rc
= qdev_init(&dev
->qdev
);
150 error_report("Failed to initialize USB device '%s'\n", name
);
156 static void usb_fill_port(USBPort
*port
, void *opaque
, int index
,
157 USBPortOps
*ops
, int speedmask
)
159 port
->opaque
= opaque
;
162 port
->speedmask
= speedmask
;
163 usb_port_location(port
, NULL
, index
+ 1);
166 void usb_register_port(USBBus
*bus
, USBPort
*port
, void *opaque
, int index
,
167 USBPortOps
*ops
, int speedmask
)
169 usb_fill_port(port
, opaque
, index
, ops
, speedmask
);
170 QTAILQ_INSERT_TAIL(&bus
->free
, port
, next
);
174 int usb_register_companion(const char *masterbus
, USBPort
*ports
[],
175 uint32_t portcount
, uint32_t firstport
,
176 void *opaque
, USBPortOps
*ops
, int speedmask
)
181 QTAILQ_FOREACH(bus
, &busses
, next
) {
182 if (strcmp(bus
->qbus
.name
, masterbus
) == 0) {
187 if (!bus
|| !bus
->ops
->register_companion
) {
188 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "masterbus",
191 error_printf_unless_qmp(
192 "USB bus '%s' does not allow companion controllers\n",
198 for (i
= 0; i
< portcount
; i
++) {
199 usb_fill_port(ports
[i
], opaque
, i
, ops
, speedmask
);
202 return bus
->ops
->register_companion(bus
, ports
, portcount
, firstport
);
205 void usb_port_location(USBPort
*downstream
, USBPort
*upstream
, int portnr
)
208 snprintf(downstream
->path
, sizeof(downstream
->path
), "%s.%d",
209 upstream
->path
, portnr
);
211 snprintf(downstream
->path
, sizeof(downstream
->path
), "%d", portnr
);
215 void usb_unregister_port(USBBus
*bus
, USBPort
*port
)
218 qdev_free(&port
->dev
->qdev
);
219 QTAILQ_REMOVE(&bus
->free
, port
, next
);
223 int usb_claim_port(USBDevice
*dev
)
225 USBBus
*bus
= usb_bus_from_device(dev
);
228 assert(dev
->port
== NULL
);
230 if (dev
->port_path
) {
231 QTAILQ_FOREACH(port
, &bus
->free
, next
) {
232 if (strcmp(port
->path
, dev
->port_path
) == 0) {
237 error_report("Error: usb port %s (bus %s) not found (in use?)\n",
238 dev
->port_path
, bus
->qbus
.name
);
242 if (bus
->nfree
== 1 && strcmp(dev
->qdev
.info
->name
, "usb-hub") != 0) {
243 /* Create a new hub and chain it on */
244 usb_create_simple(bus
, "usb-hub");
246 if (bus
->nfree
== 0) {
247 error_report("Error: tried to attach usb device %s to a bus "
248 "with no free ports\n", dev
->product_desc
);
251 port
= QTAILQ_FIRST(&bus
->free
);
253 trace_usb_port_claim(bus
->busnr
, port
->path
);
255 QTAILQ_REMOVE(&bus
->free
, port
, next
);
261 QTAILQ_INSERT_TAIL(&bus
->used
, port
, next
);
266 void usb_release_port(USBDevice
*dev
)
268 USBBus
*bus
= usb_bus_from_device(dev
);
269 USBPort
*port
= dev
->port
;
271 assert(port
!= NULL
);
272 trace_usb_port_release(bus
->busnr
, port
->path
);
274 QTAILQ_REMOVE(&bus
->used
, port
, next
);
280 QTAILQ_INSERT_TAIL(&bus
->free
, port
, next
);
284 int usb_device_attach(USBDevice
*dev
)
286 USBBus
*bus
= usb_bus_from_device(dev
);
287 USBPort
*port
= dev
->port
;
289 assert(port
!= NULL
);
290 assert(!dev
->attached
);
291 trace_usb_port_attach(bus
->busnr
, port
->path
);
293 if (!(port
->speedmask
& dev
->speedmask
)) {
294 error_report("Warning: speed mismatch trying to attach "
295 "usb device %s to bus %s\n",
296 dev
->product_desc
, bus
->qbus
.name
);
306 int usb_device_detach(USBDevice
*dev
)
308 USBBus
*bus
= usb_bus_from_device(dev
);
309 USBPort
*port
= dev
->port
;
311 assert(port
!= NULL
);
312 assert(dev
->attached
);
313 trace_usb_port_detach(bus
->busnr
, port
->path
);
320 int usb_device_delete_addr(int busnr
, int addr
)
326 bus
= usb_bus_find(busnr
);
330 QTAILQ_FOREACH(port
, &bus
->used
, next
) {
331 if (port
->dev
->addr
== addr
)
338 qdev_free(&dev
->qdev
);
342 static const char *usb_speed(unsigned int speed
)
344 static const char *txt
[] = {
345 [ USB_SPEED_LOW
] = "1.5",
346 [ USB_SPEED_FULL
] = "12",
347 [ USB_SPEED_HIGH
] = "480",
348 [ USB_SPEED_SUPER
] = "5000",
350 if (speed
>= ARRAY_SIZE(txt
))
355 static void usb_bus_dev_print(Monitor
*mon
, DeviceState
*qdev
, int indent
)
357 USBDevice
*dev
= DO_UPCAST(USBDevice
, qdev
, qdev
);
358 USBBus
*bus
= usb_bus_from_device(dev
);
360 monitor_printf(mon
, "%*saddr %d.%d, port %s, speed %s, name %s%s\n",
361 indent
, "", bus
->busnr
, dev
->addr
,
362 dev
->port
? dev
->port
->path
: "-",
363 usb_speed(dev
->speed
), dev
->product_desc
,
364 dev
->attached
? ", attached" : "");
367 static char *usb_get_dev_path(DeviceState
*qdev
)
369 USBDevice
*dev
= DO_UPCAST(USBDevice
, qdev
, qdev
);
370 return g_strdup(dev
->port
->path
);
373 static char *usb_get_fw_dev_path(DeviceState
*qdev
)
375 USBDevice
*dev
= DO_UPCAST(USBDevice
, qdev
, qdev
);
377 ssize_t pos
= 0, fw_len
;
380 fw_len
= 32 + strlen(dev
->port
->path
) * 6;
381 fw_path
= g_malloc(fw_len
);
382 in
= dev
->port
->path
;
383 while (fw_len
- pos
> 0) {
384 nr
= strtol(in
, &in
, 10);
386 /* some hub between root port and device */
387 pos
+= snprintf(fw_path
+ pos
, fw_len
- pos
, "hub@%ld/", nr
);
390 /* the device itself */
391 pos
+= snprintf(fw_path
+ pos
, fw_len
- pos
, "%s@%ld",
392 qdev_fw_name(qdev
), nr
);
399 void usb_info(Monitor
*mon
)
405 if (QTAILQ_EMPTY(&busses
)) {
406 monitor_printf(mon
, "USB support not enabled\n");
410 QTAILQ_FOREACH(bus
, &busses
, next
) {
411 QTAILQ_FOREACH(port
, &bus
->used
, next
) {
415 monitor_printf(mon
, " Device %d.%d, Port %s, Speed %s Mb/s, Product %s\n",
416 bus
->busnr
, dev
->addr
, port
->path
, usb_speed(dev
->speed
),
422 /* handle legacy -usbdevice cmd line option */
423 USBDevice
*usbdevice_create(const char *cmdline
)
425 USBBus
*bus
= usb_bus_find(-1 /* any */);
432 params
= strchr(cmdline
,':');
435 len
= params
- cmdline
;
436 if (len
> sizeof(driver
))
437 len
= sizeof(driver
);
438 pstrcpy(driver
, len
, cmdline
);
441 pstrcpy(driver
, sizeof(driver
), cmdline
);
444 for (info
= device_info_list
; info
!= NULL
; info
= info
->next
) {
445 if (info
->bus_info
!= &usb_bus_info
)
447 usb
= DO_UPCAST(USBDeviceInfo
, qdev
, info
);
448 if (usb
->usbdevice_name
== NULL
)
450 if (strcmp(usb
->usbdevice_name
, driver
) != 0)
456 /* no error because some drivers are not converted (yet) */
457 error_report("usbdevice %s not found", driver
);
462 if (!usb
->usbdevice_init
) {
464 error_report("usbdevice %s accepts no params", driver
);
467 return usb_create_simple(bus
, usb
->qdev
.name
);
469 return usb
->usbdevice_init(params
);