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_END_OF_LIST()
25 static int next_usb_bus
= 0;
26 static QTAILQ_HEAD(, USBBus
) busses
= QTAILQ_HEAD_INITIALIZER(busses
);
28 const VMStateDescription vmstate_usb_device
= {
31 .minimum_version_id
= 1,
32 .fields
= (VMStateField
[]) {
33 VMSTATE_UINT8(addr
, USBDevice
),
34 VMSTATE_INT32(state
, USBDevice
),
35 VMSTATE_INT32(remote_wakeup
, USBDevice
),
36 VMSTATE_INT32(setup_state
, USBDevice
),
37 VMSTATE_INT32(setup_len
, USBDevice
),
38 VMSTATE_INT32(setup_index
, USBDevice
),
39 VMSTATE_UINT8_ARRAY(setup_buf
, USBDevice
, 8),
40 VMSTATE_END_OF_LIST(),
44 void usb_bus_new(USBBus
*bus
, USBBusOps
*ops
, DeviceState
*host
)
46 qbus_create_inplace(&bus
->qbus
, &usb_bus_info
, host
, NULL
);
48 bus
->busnr
= next_usb_bus
++;
49 bus
->qbus
.allow_hotplug
= 1; /* Yes, we can */
50 QTAILQ_INIT(&bus
->free
);
51 QTAILQ_INIT(&bus
->used
);
52 QTAILQ_INSERT_TAIL(&busses
, bus
, next
);
55 USBBus
*usb_bus_find(int busnr
)
60 return QTAILQ_FIRST(&busses
);
61 QTAILQ_FOREACH(bus
, &busses
, next
) {
62 if (bus
->busnr
== busnr
)
68 static int usb_qdev_init(DeviceState
*qdev
, DeviceInfo
*base
)
70 USBDevice
*dev
= DO_UPCAST(USBDevice
, qdev
, qdev
);
71 USBDeviceInfo
*info
= DO_UPCAST(USBDeviceInfo
, qdev
, base
);
74 pstrcpy(dev
->product_desc
, sizeof(dev
->product_desc
), info
->product_desc
);
77 QLIST_INIT(&dev
->strings
);
78 rc
= usb_claim_port(dev
);
82 rc
= dev
->info
->init(dev
);
86 if (dev
->auto_attach
) {
87 rc
= usb_device_attach(dev
);
99 static int usb_qdev_exit(DeviceState
*qdev
)
101 USBDevice
*dev
= DO_UPCAST(USBDevice
, qdev
, qdev
);
104 usb_device_detach(dev
);
106 if (dev
->info
->handle_destroy
) {
107 dev
->info
->handle_destroy(dev
);
110 usb_release_port(dev
);
115 void usb_qdev_register(USBDeviceInfo
*info
)
117 info
->qdev
.bus_info
= &usb_bus_info
;
118 info
->qdev
.init
= usb_qdev_init
;
119 info
->qdev
.unplug
= qdev_simple_unplug_cb
;
120 info
->qdev
.exit
= usb_qdev_exit
;
121 qdev_register(&info
->qdev
);
124 void usb_qdev_register_many(USBDeviceInfo
*info
)
126 while (info
->qdev
.name
) {
127 usb_qdev_register(info
);
132 USBDevice
*usb_create(USBBus
*bus
, const char *name
)
137 /* temporary stopgap until all usb is properly qdev-ified */
139 bus
= usb_bus_find(-1);
142 error_report("%s: no bus specified, using \"%s\" for \"%s\"\n",
143 __FUNCTION__
, bus
->qbus
.name
, name
);
147 dev
= qdev_create(&bus
->qbus
, name
);
148 return DO_UPCAST(USBDevice
, qdev
, dev
);
151 USBDevice
*usb_create_simple(USBBus
*bus
, const char *name
)
153 USBDevice
*dev
= usb_create(bus
, name
);
157 error_report("Failed to create USB device '%s'\n", name
);
160 rc
= qdev_init(&dev
->qdev
);
162 error_report("Failed to initialize USB device '%s'\n", name
);
168 static void usb_fill_port(USBPort
*port
, void *opaque
, int index
,
169 USBPortOps
*ops
, int speedmask
)
171 port
->opaque
= opaque
;
174 port
->speedmask
= speedmask
;
175 usb_port_location(port
, NULL
, index
+ 1);
178 void usb_register_port(USBBus
*bus
, USBPort
*port
, void *opaque
, int index
,
179 USBPortOps
*ops
, int speedmask
)
181 usb_fill_port(port
, opaque
, index
, ops
, speedmask
);
182 QTAILQ_INSERT_TAIL(&bus
->free
, port
, next
);
186 int usb_register_companion(const char *masterbus
, USBPort
*ports
[],
187 uint32_t portcount
, uint32_t firstport
,
188 void *opaque
, USBPortOps
*ops
, int speedmask
)
193 QTAILQ_FOREACH(bus
, &busses
, next
) {
194 if (strcmp(bus
->qbus
.name
, masterbus
) == 0) {
199 if (!bus
|| !bus
->ops
->register_companion
) {
200 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "masterbus",
203 error_printf_unless_qmp(
204 "USB bus '%s' does not allow companion controllers\n",
210 for (i
= 0; i
< portcount
; i
++) {
211 usb_fill_port(ports
[i
], opaque
, i
, ops
, speedmask
);
214 return bus
->ops
->register_companion(bus
, ports
, portcount
, firstport
);
217 void usb_port_location(USBPort
*downstream
, USBPort
*upstream
, int portnr
)
220 snprintf(downstream
->path
, sizeof(downstream
->path
), "%s.%d",
221 upstream
->path
, portnr
);
223 snprintf(downstream
->path
, sizeof(downstream
->path
), "%d", portnr
);
227 void usb_unregister_port(USBBus
*bus
, USBPort
*port
)
230 qdev_free(&port
->dev
->qdev
);
231 QTAILQ_REMOVE(&bus
->free
, port
, next
);
235 int usb_claim_port(USBDevice
*dev
)
237 USBBus
*bus
= usb_bus_from_device(dev
);
240 assert(dev
->port
== NULL
);
242 if (dev
->port_path
) {
243 QTAILQ_FOREACH(port
, &bus
->free
, next
) {
244 if (strcmp(port
->path
, dev
->port_path
) == 0) {
249 error_report("Error: usb port %s (bus %s) not found (in use?)\n",
250 dev
->port_path
, bus
->qbus
.name
);
254 if (bus
->nfree
== 1 && strcmp(dev
->qdev
.info
->name
, "usb-hub") != 0) {
255 /* Create a new hub and chain it on */
256 usb_create_simple(bus
, "usb-hub");
258 if (bus
->nfree
== 0) {
259 error_report("Error: tried to attach usb device %s to a bus "
260 "with no free ports\n", dev
->product_desc
);
263 port
= QTAILQ_FIRST(&bus
->free
);
265 trace_usb_port_claim(bus
->busnr
, port
->path
);
267 QTAILQ_REMOVE(&bus
->free
, port
, next
);
273 QTAILQ_INSERT_TAIL(&bus
->used
, port
, next
);
278 void usb_release_port(USBDevice
*dev
)
280 USBBus
*bus
= usb_bus_from_device(dev
);
281 USBPort
*port
= dev
->port
;
283 assert(port
!= NULL
);
284 trace_usb_port_release(bus
->busnr
, port
->path
);
286 QTAILQ_REMOVE(&bus
->used
, port
, next
);
292 QTAILQ_INSERT_TAIL(&bus
->free
, port
, next
);
296 int usb_device_attach(USBDevice
*dev
)
298 USBBus
*bus
= usb_bus_from_device(dev
);
299 USBPort
*port
= dev
->port
;
301 assert(port
!= NULL
);
302 assert(!dev
->attached
);
303 trace_usb_port_attach(bus
->busnr
, port
->path
);
305 if (!(port
->speedmask
& dev
->speedmask
)) {
306 error_report("Warning: speed mismatch trying to attach "
307 "usb device %s to bus %s\n",
308 dev
->product_desc
, bus
->qbus
.name
);
318 int usb_device_detach(USBDevice
*dev
)
320 USBBus
*bus
= usb_bus_from_device(dev
);
321 USBPort
*port
= dev
->port
;
323 assert(port
!= NULL
);
324 assert(dev
->attached
);
325 trace_usb_port_detach(bus
->busnr
, port
->path
);
332 int usb_device_delete_addr(int busnr
, int addr
)
338 bus
= usb_bus_find(busnr
);
342 QTAILQ_FOREACH(port
, &bus
->used
, next
) {
343 if (port
->dev
->addr
== addr
)
350 qdev_free(&dev
->qdev
);
354 static const char *usb_speed(unsigned int speed
)
356 static const char *txt
[] = {
357 [ USB_SPEED_LOW
] = "1.5",
358 [ USB_SPEED_FULL
] = "12",
359 [ USB_SPEED_HIGH
] = "480",
360 [ USB_SPEED_SUPER
] = "5000",
362 if (speed
>= ARRAY_SIZE(txt
))
367 static void usb_bus_dev_print(Monitor
*mon
, DeviceState
*qdev
, int indent
)
369 USBDevice
*dev
= DO_UPCAST(USBDevice
, qdev
, qdev
);
370 USBBus
*bus
= usb_bus_from_device(dev
);
372 monitor_printf(mon
, "%*saddr %d.%d, port %s, speed %s, name %s%s\n",
373 indent
, "", bus
->busnr
, dev
->addr
,
374 dev
->port
? dev
->port
->path
: "-",
375 usb_speed(dev
->speed
), dev
->product_desc
,
376 dev
->attached
? ", attached" : "");
379 static char *usb_get_dev_path(DeviceState
*qdev
)
381 USBDevice
*dev
= DO_UPCAST(USBDevice
, qdev
, qdev
);
382 return g_strdup(dev
->port
->path
);
385 static char *usb_get_fw_dev_path(DeviceState
*qdev
)
387 USBDevice
*dev
= DO_UPCAST(USBDevice
, qdev
, qdev
);
389 ssize_t pos
= 0, fw_len
;
392 fw_len
= 32 + strlen(dev
->port
->path
) * 6;
393 fw_path
= g_malloc(fw_len
);
394 in
= dev
->port
->path
;
395 while (fw_len
- pos
> 0) {
396 nr
= strtol(in
, &in
, 10);
398 /* some hub between root port and device */
399 pos
+= snprintf(fw_path
+ pos
, fw_len
- pos
, "hub@%ld/", nr
);
402 /* the device itself */
403 pos
+= snprintf(fw_path
+ pos
, fw_len
- pos
, "%s@%ld",
404 qdev_fw_name(qdev
), nr
);
411 void usb_info(Monitor
*mon
)
417 if (QTAILQ_EMPTY(&busses
)) {
418 monitor_printf(mon
, "USB support not enabled\n");
422 QTAILQ_FOREACH(bus
, &busses
, next
) {
423 QTAILQ_FOREACH(port
, &bus
->used
, next
) {
427 monitor_printf(mon
, " Device %d.%d, Port %s, Speed %s Mb/s, Product %s\n",
428 bus
->busnr
, dev
->addr
, port
->path
, usb_speed(dev
->speed
),
434 /* handle legacy -usbdevice cmd line option */
435 USBDevice
*usbdevice_create(const char *cmdline
)
437 USBBus
*bus
= usb_bus_find(-1 /* any */);
444 params
= strchr(cmdline
,':');
447 len
= params
- cmdline
;
448 if (len
> sizeof(driver
))
449 len
= sizeof(driver
);
450 pstrcpy(driver
, len
, cmdline
);
453 pstrcpy(driver
, sizeof(driver
), cmdline
);
456 for (info
= device_info_list
; info
!= NULL
; info
= info
->next
) {
457 if (info
->bus_info
!= &usb_bus_info
)
459 usb
= DO_UPCAST(USBDeviceInfo
, qdev
, info
);
460 if (usb
->usbdevice_name
== NULL
)
462 if (strcmp(usb
->usbdevice_name
, driver
) != 0)
468 /* no error because some drivers are not converted (yet) */
469 error_report("usbdevice %s not found", driver
);
474 if (!usb
->usbdevice_init
) {
476 error_report("usbdevice %s accepts no params", driver
);
479 return usb_create_simple(bus
, usb
->qdev
.name
);
481 return usb
->usbdevice_init(params
);