7 static void usb_bus_dev_print(Monitor
*mon
, DeviceState
*qdev
, int indent
);
9 static char *usb_get_dev_path(DeviceState
*dev
);
10 static char *usb_get_fw_dev_path(DeviceState
*qdev
);
12 static struct BusInfo usb_bus_info
= {
14 .size
= sizeof(USBBus
),
15 .print_dev
= usb_bus_dev_print
,
16 .get_dev_path
= usb_get_dev_path
,
17 .get_fw_dev_path
= usb_get_fw_dev_path
,
18 .props
= (Property
[]) {
19 DEFINE_PROP_STRING("port", USBDevice
, port_path
),
20 DEFINE_PROP_END_OF_LIST()
23 static int next_usb_bus
= 0;
24 static QTAILQ_HEAD(, USBBus
) busses
= QTAILQ_HEAD_INITIALIZER(busses
);
26 const VMStateDescription vmstate_usb_device
= {
29 .minimum_version_id
= 1,
30 .fields
= (VMStateField
[]) {
31 VMSTATE_UINT8(addr
, USBDevice
),
32 VMSTATE_INT32(state
, USBDevice
),
33 VMSTATE_INT32(remote_wakeup
, USBDevice
),
34 VMSTATE_INT32(setup_state
, USBDevice
),
35 VMSTATE_INT32(setup_len
, USBDevice
),
36 VMSTATE_INT32(setup_index
, USBDevice
),
37 VMSTATE_UINT8_ARRAY(setup_buf
, USBDevice
, 8),
38 VMSTATE_END_OF_LIST(),
42 void usb_bus_new(USBBus
*bus
, USBBusOps
*ops
, DeviceState
*host
)
44 qbus_create_inplace(&bus
->qbus
, &usb_bus_info
, host
, NULL
);
46 bus
->busnr
= next_usb_bus
++;
47 bus
->qbus
.allow_hotplug
= 1; /* Yes, we can */
48 QTAILQ_INIT(&bus
->free
);
49 QTAILQ_INIT(&bus
->used
);
50 QTAILQ_INSERT_TAIL(&busses
, bus
, next
);
53 USBBus
*usb_bus_find(int busnr
)
58 return QTAILQ_FIRST(&busses
);
59 QTAILQ_FOREACH(bus
, &busses
, next
) {
60 if (bus
->busnr
== busnr
)
66 static int usb_qdev_init(DeviceState
*qdev
, DeviceInfo
*base
)
68 USBDevice
*dev
= DO_UPCAST(USBDevice
, qdev
, qdev
);
69 USBDeviceInfo
*info
= DO_UPCAST(USBDeviceInfo
, qdev
, base
);
72 pstrcpy(dev
->product_desc
, sizeof(dev
->product_desc
), info
->product_desc
);
75 QLIST_INIT(&dev
->strings
);
76 rc
= dev
->info
->init(dev
);
77 if (rc
== 0 && dev
->auto_attach
)
78 rc
= usb_device_attach(dev
);
82 static int usb_qdev_exit(DeviceState
*qdev
)
84 USBDevice
*dev
= DO_UPCAST(USBDevice
, qdev
, qdev
);
87 usb_device_detach(dev
);
89 if (dev
->info
->handle_destroy
) {
90 dev
->info
->handle_destroy(dev
);
95 void usb_qdev_register(USBDeviceInfo
*info
)
97 info
->qdev
.bus_info
= &usb_bus_info
;
98 info
->qdev
.init
= usb_qdev_init
;
99 info
->qdev
.unplug
= qdev_simple_unplug_cb
;
100 info
->qdev
.exit
= usb_qdev_exit
;
101 qdev_register(&info
->qdev
);
104 void usb_qdev_register_many(USBDeviceInfo
*info
)
106 while (info
->qdev
.name
) {
107 usb_qdev_register(info
);
112 USBDevice
*usb_create(USBBus
*bus
, const char *name
)
117 /* temporary stopgap until all usb is properly qdev-ified */
119 bus
= usb_bus_find(-1);
122 error_report("%s: no bus specified, using \"%s\" for \"%s\"\n",
123 __FUNCTION__
, bus
->qbus
.name
, name
);
127 dev
= qdev_create(&bus
->qbus
, name
);
128 return DO_UPCAST(USBDevice
, qdev
, dev
);
131 USBDevice
*usb_create_simple(USBBus
*bus
, const char *name
)
133 USBDevice
*dev
= usb_create(bus
, name
);
135 hw_error("Failed to create USB device '%s'\n", name
);
137 qdev_init_nofail(&dev
->qdev
);
141 static void usb_fill_port(USBPort
*port
, void *opaque
, int index
,
142 USBPortOps
*ops
, int speedmask
)
144 port
->opaque
= opaque
;
147 port
->speedmask
= speedmask
;
148 usb_port_location(port
, NULL
, index
+ 1);
151 void usb_register_port(USBBus
*bus
, USBPort
*port
, void *opaque
, int index
,
152 USBPortOps
*ops
, int speedmask
)
154 usb_fill_port(port
, opaque
, index
, ops
, speedmask
);
155 QTAILQ_INSERT_TAIL(&bus
->free
, port
, next
);
159 int usb_register_companion(const char *masterbus
, USBPort
*ports
[],
160 uint32_t portcount
, uint32_t firstport
,
161 void *opaque
, USBPortOps
*ops
, int speedmask
)
166 QTAILQ_FOREACH(bus
, &busses
, next
) {
167 if (strcmp(bus
->qbus
.name
, masterbus
) == 0) {
172 if (!bus
|| !bus
->ops
->register_companion
) {
173 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "masterbus",
176 error_printf_unless_qmp(
177 "USB bus '%s' does not allow companion controllers\n",
183 for (i
= 0; i
< portcount
; i
++) {
184 usb_fill_port(ports
[i
], opaque
, i
, ops
, speedmask
);
187 return bus
->ops
->register_companion(bus
, ports
, portcount
, firstport
);
190 void usb_port_location(USBPort
*downstream
, USBPort
*upstream
, int portnr
)
193 snprintf(downstream
->path
, sizeof(downstream
->path
), "%s.%d",
194 upstream
->path
, portnr
);
196 snprintf(downstream
->path
, sizeof(downstream
->path
), "%d", portnr
);
200 void usb_unregister_port(USBBus
*bus
, USBPort
*port
)
203 qdev_free(&port
->dev
->qdev
);
204 QTAILQ_REMOVE(&bus
->free
, port
, next
);
208 static int do_attach(USBDevice
*dev
)
210 USBBus
*bus
= usb_bus_from_device(dev
);
214 error_report("Error: tried to attach usb device %s twice\n",
218 if (bus
->nfree
== 0) {
219 error_report("Error: tried to attach usb device %s to a bus with no free ports\n",
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\n",
231 dev
->port_path
, bus
->qbus
.name
);
235 port
= QTAILQ_FIRST(&bus
->free
);
237 if (!(port
->speedmask
& dev
->speedmask
)) {
238 error_report("Warning: speed mismatch trying to attach usb device %s to bus %s\n",
239 dev
->product_desc
, bus
->qbus
.name
);
244 QTAILQ_REMOVE(&bus
->free
, port
, next
);
247 usb_attach(port
, dev
);
249 QTAILQ_INSERT_TAIL(&bus
->used
, port
, next
);
255 int usb_device_attach(USBDevice
*dev
)
257 USBBus
*bus
= usb_bus_from_device(dev
);
259 if (bus
->nfree
== 1 && dev
->port_path
== NULL
) {
260 /* Create a new hub and chain it on
261 (unless a physical port location is specified). */
262 usb_create_simple(bus
, "usb-hub");
264 return do_attach(dev
);
267 int usb_device_detach(USBDevice
*dev
)
269 USBBus
*bus
= usb_bus_from_device(dev
);
272 if (!dev
->attached
) {
273 error_report("Error: tried to detach unattached usb device %s\n",
279 QTAILQ_FOREACH(port
, &bus
->used
, next
) {
280 if (port
->dev
== dev
)
283 assert(port
!= NULL
);
285 QTAILQ_REMOVE(&bus
->used
, port
, next
);
288 usb_attach(port
, NULL
);
290 QTAILQ_INSERT_TAIL(&bus
->free
, port
, next
);
295 int usb_device_delete_addr(int busnr
, int addr
)
301 bus
= usb_bus_find(busnr
);
305 QTAILQ_FOREACH(port
, &bus
->used
, next
) {
306 if (port
->dev
->addr
== addr
)
313 qdev_free(&dev
->qdev
);
317 static const char *usb_speed(unsigned int speed
)
319 static const char *txt
[] = {
320 [ USB_SPEED_LOW
] = "1.5",
321 [ USB_SPEED_FULL
] = "12",
322 [ USB_SPEED_HIGH
] = "480",
323 [ USB_SPEED_SUPER
] = "5000",
325 if (speed
>= ARRAY_SIZE(txt
))
330 static void usb_bus_dev_print(Monitor
*mon
, DeviceState
*qdev
, int indent
)
332 USBDevice
*dev
= DO_UPCAST(USBDevice
, qdev
, qdev
);
333 USBBus
*bus
= usb_bus_from_device(dev
);
335 monitor_printf(mon
, "%*saddr %d.%d, port %s, speed %s, name %s%s\n",
336 indent
, "", bus
->busnr
, dev
->addr
,
337 dev
->port
? dev
->port
->path
: "-",
338 usb_speed(dev
->speed
), dev
->product_desc
,
339 dev
->attached
? ", attached" : "");
342 static char *usb_get_dev_path(DeviceState
*qdev
)
344 USBDevice
*dev
= DO_UPCAST(USBDevice
, qdev
, qdev
);
345 return qemu_strdup(dev
->port
->path
);
348 static char *usb_get_fw_dev_path(DeviceState
*qdev
)
350 USBDevice
*dev
= DO_UPCAST(USBDevice
, qdev
, qdev
);
352 ssize_t pos
= 0, fw_len
;
355 fw_len
= 32 + strlen(dev
->port
->path
) * 6;
356 fw_path
= qemu_malloc(fw_len
);
357 in
= dev
->port
->path
;
358 while (fw_len
- pos
> 0) {
359 nr
= strtol(in
, &in
, 10);
361 /* some hub between root port and device */
362 pos
+= snprintf(fw_path
+ pos
, fw_len
- pos
, "hub@%ld/", nr
);
365 /* the device itself */
366 pos
+= snprintf(fw_path
+ pos
, fw_len
- pos
, "%s@%ld",
367 qdev_fw_name(qdev
), nr
);
374 void usb_info(Monitor
*mon
)
380 if (QTAILQ_EMPTY(&busses
)) {
381 monitor_printf(mon
, "USB support not enabled\n");
385 QTAILQ_FOREACH(bus
, &busses
, next
) {
386 QTAILQ_FOREACH(port
, &bus
->used
, next
) {
390 monitor_printf(mon
, " Device %d.%d, Port %s, Speed %s Mb/s, Product %s\n",
391 bus
->busnr
, dev
->addr
, port
->path
, usb_speed(dev
->speed
),
397 /* handle legacy -usbdevice cmd line option */
398 USBDevice
*usbdevice_create(const char *cmdline
)
400 USBBus
*bus
= usb_bus_find(-1 /* any */);
407 params
= strchr(cmdline
,':');
410 len
= params
- cmdline
;
411 if (len
> sizeof(driver
))
412 len
= sizeof(driver
);
413 pstrcpy(driver
, len
, cmdline
);
416 pstrcpy(driver
, sizeof(driver
), cmdline
);
419 for (info
= device_info_list
; info
!= NULL
; info
= info
->next
) {
420 if (info
->bus_info
!= &usb_bus_info
)
422 usb
= DO_UPCAST(USBDeviceInfo
, qdev
, info
);
423 if (usb
->usbdevice_name
== NULL
)
425 if (strcmp(usb
->usbdevice_name
, driver
) != 0)
431 /* no error because some drivers are not converted (yet) */
432 error_report("usbdevice %s not found", driver
);
437 if (!usb
->usbdevice_init
) {
439 error_report("usbdevice %s accepts no params", driver
);
442 return usb_create_simple(bus
, usb
->qdev
.name
);
444 return usb
->usbdevice_init(params
);