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
, DeviceState
*host
)
44 qbus_create_inplace(&bus
->qbus
, &usb_bus_info
, host
, NULL
);
45 bus
->busnr
= next_usb_bus
++;
46 bus
->qbus
.allow_hotplug
= 1; /* Yes, we can */
47 QTAILQ_INIT(&bus
->free
);
48 QTAILQ_INIT(&bus
->used
);
49 QTAILQ_INSERT_TAIL(&busses
, bus
, next
);
52 USBBus
*usb_bus_find(int busnr
)
57 return QTAILQ_FIRST(&busses
);
58 QTAILQ_FOREACH(bus
, &busses
, next
) {
59 if (bus
->busnr
== busnr
)
65 static int usb_qdev_init(DeviceState
*qdev
, DeviceInfo
*base
)
67 USBDevice
*dev
= DO_UPCAST(USBDevice
, qdev
, qdev
);
68 USBDeviceInfo
*info
= DO_UPCAST(USBDeviceInfo
, qdev
, base
);
71 pstrcpy(dev
->product_desc
, sizeof(dev
->product_desc
), info
->product_desc
);
74 QLIST_INIT(&dev
->strings
);
75 rc
= dev
->info
->init(dev
);
76 if (rc
== 0 && dev
->auto_attach
)
77 usb_device_attach(dev
);
81 static int usb_qdev_exit(DeviceState
*qdev
)
83 USBDevice
*dev
= DO_UPCAST(USBDevice
, qdev
, qdev
);
85 usb_device_detach(dev
);
86 if (dev
->info
->handle_destroy
) {
87 dev
->info
->handle_destroy(dev
);
92 void usb_qdev_register(USBDeviceInfo
*info
)
94 info
->qdev
.bus_info
= &usb_bus_info
;
95 info
->qdev
.init
= usb_qdev_init
;
96 info
->qdev
.unplug
= qdev_simple_unplug_cb
;
97 info
->qdev
.exit
= usb_qdev_exit
;
98 qdev_register(&info
->qdev
);
101 void usb_qdev_register_many(USBDeviceInfo
*info
)
103 while (info
->qdev
.name
) {
104 usb_qdev_register(info
);
109 USBDevice
*usb_create(USBBus
*bus
, const char *name
)
114 /* temporary stopgap until all usb is properly qdev-ified */
116 bus
= usb_bus_find(-1);
119 fprintf(stderr
, "%s: no bus specified, using \"%s\" for \"%s\"\n",
120 __FUNCTION__
, bus
->qbus
.name
, name
);
124 dev
= qdev_create(&bus
->qbus
, name
);
125 return DO_UPCAST(USBDevice
, qdev
, dev
);
128 USBDevice
*usb_create_simple(USBBus
*bus
, const char *name
)
130 USBDevice
*dev
= usb_create(bus
, name
);
132 hw_error("Failed to create USB device '%s'\n", name
);
134 qdev_init_nofail(&dev
->qdev
);
138 void usb_register_port(USBBus
*bus
, USBPort
*port
, void *opaque
, int index
,
139 USBPortOps
*ops
, int speedmask
)
141 port
->opaque
= opaque
;
143 port
->opaque
= opaque
;
146 port
->speedmask
= speedmask
;
147 QTAILQ_INSERT_TAIL(&bus
->free
, port
, next
);
151 void usb_port_location(USBPort
*downstream
, USBPort
*upstream
, int portnr
)
154 snprintf(downstream
->path
, sizeof(downstream
->path
), "%s.%d",
155 upstream
->path
, portnr
);
157 snprintf(downstream
->path
, sizeof(downstream
->path
), "%d", portnr
);
161 void usb_unregister_port(USBBus
*bus
, USBPort
*port
)
164 qdev_free(&port
->dev
->qdev
);
165 QTAILQ_REMOVE(&bus
->free
, port
, next
);
169 static void do_attach(USBDevice
*dev
)
171 USBBus
*bus
= usb_bus_from_device(dev
);
175 fprintf(stderr
, "Warning: tried to attach usb device %s twice\n",
179 if (dev
->port_path
) {
180 QTAILQ_FOREACH(port
, &bus
->free
, next
) {
181 if (strcmp(port
->path
, dev
->port_path
) == 0) {
186 fprintf(stderr
, "Warning: usb port %s (bus %s) not found\n",
187 dev
->port_path
, bus
->qbus
.name
);
191 port
= QTAILQ_FIRST(&bus
->free
);
195 QTAILQ_REMOVE(&bus
->free
, port
, next
);
198 usb_attach(port
, dev
);
200 QTAILQ_INSERT_TAIL(&bus
->used
, port
, next
);
204 int usb_device_attach(USBDevice
*dev
)
206 USBBus
*bus
= usb_bus_from_device(dev
);
208 if (bus
->nfree
== 1 && dev
->port_path
== NULL
) {
209 /* Create a new hub and chain it on
210 (unless a physical port location is specified). */
211 usb_create_simple(bus
, "usb-hub");
217 int usb_device_detach(USBDevice
*dev
)
219 USBBus
*bus
= usb_bus_from_device(dev
);
222 if (!dev
->attached
) {
223 fprintf(stderr
, "Warning: tried to detach unattached usb device %s\n",
229 QTAILQ_FOREACH(port
, &bus
->used
, next
) {
230 if (port
->dev
== dev
)
233 assert(port
!= NULL
);
235 QTAILQ_REMOVE(&bus
->used
, port
, next
);
238 usb_attach(port
, NULL
);
240 QTAILQ_INSERT_TAIL(&bus
->free
, port
, next
);
245 int usb_device_delete_addr(int busnr
, int addr
)
251 bus
= usb_bus_find(busnr
);
255 QTAILQ_FOREACH(port
, &bus
->used
, next
) {
256 if (port
->dev
->addr
== addr
)
263 qdev_free(&dev
->qdev
);
267 static const char *usb_speed(unsigned int speed
)
269 static const char *txt
[] = {
270 [ USB_SPEED_LOW
] = "1.5",
271 [ USB_SPEED_FULL
] = "12",
272 [ USB_SPEED_HIGH
] = "480",
274 if (speed
>= ARRAY_SIZE(txt
))
279 static void usb_bus_dev_print(Monitor
*mon
, DeviceState
*qdev
, int indent
)
281 USBDevice
*dev
= DO_UPCAST(USBDevice
, qdev
, qdev
);
282 USBBus
*bus
= usb_bus_from_device(dev
);
284 monitor_printf(mon
, "%*saddr %d.%d, port %s, speed %s, name %s%s\n",
285 indent
, "", bus
->busnr
, dev
->addr
,
286 dev
->port
? dev
->port
->path
: "-",
287 usb_speed(dev
->speed
), dev
->product_desc
,
288 dev
->attached
? ", attached" : "");
291 static char *usb_get_dev_path(DeviceState
*qdev
)
293 USBDevice
*dev
= DO_UPCAST(USBDevice
, qdev
, qdev
);
294 return qemu_strdup(dev
->port
->path
);
297 static char *usb_get_fw_dev_path(DeviceState
*qdev
)
299 USBDevice
*dev
= DO_UPCAST(USBDevice
, qdev
, qdev
);
301 ssize_t pos
= 0, fw_len
;
304 fw_len
= 32 + strlen(dev
->port
->path
) * 6;
305 fw_path
= qemu_malloc(fw_len
);
306 in
= dev
->port
->path
;
307 while (fw_len
- pos
> 0) {
308 nr
= strtol(in
, &in
, 10);
310 /* some hub between root port and device */
311 pos
+= snprintf(fw_path
+ pos
, fw_len
- pos
, "hub@%ld/", nr
);
314 /* the device itself */
315 pos
+= snprintf(fw_path
+ pos
, fw_len
- pos
, "%s@%ld",
316 qdev_fw_name(qdev
), nr
);
323 void usb_info(Monitor
*mon
)
329 if (QTAILQ_EMPTY(&busses
)) {
330 monitor_printf(mon
, "USB support not enabled\n");
334 QTAILQ_FOREACH(bus
, &busses
, next
) {
335 QTAILQ_FOREACH(port
, &bus
->used
, next
) {
339 monitor_printf(mon
, " Device %d.%d, Port %s, Speed %s Mb/s, Product %s\n",
340 bus
->busnr
, dev
->addr
, port
->path
, usb_speed(dev
->speed
),
346 /* handle legacy -usbdevice cmd line option */
347 USBDevice
*usbdevice_create(const char *cmdline
)
349 USBBus
*bus
= usb_bus_find(-1 /* any */);
356 params
= strchr(cmdline
,':');
359 len
= params
- cmdline
;
360 if (len
> sizeof(driver
))
361 len
= sizeof(driver
);
362 pstrcpy(driver
, len
, cmdline
);
365 pstrcpy(driver
, sizeof(driver
), cmdline
);
368 for (info
= device_info_list
; info
!= NULL
; info
= info
->next
) {
369 if (info
->bus_info
!= &usb_bus_info
)
371 usb
= DO_UPCAST(USBDeviceInfo
, qdev
, info
);
372 if (usb
->usbdevice_name
== NULL
)
374 if (strcmp(usb
->usbdevice_name
, driver
) != 0)
380 /* no error because some drivers are not converted (yet) */
381 error_report("usbdevice %s not found", driver
);
386 if (!usb
->usbdevice_init
) {
388 error_report("usbdevice %s accepts no params", driver
);
391 return usb_create_simple(bus
, usb
->qdev
.name
);
393 return usb
->usbdevice_init(params
);