7 static void usb_bus_dev_print(Monitor
*mon
, DeviceState
*qdev
, int indent
);
8 static char *usbbus_get_fw_dev_path(DeviceState
*dev
);
10 static struct BusInfo usb_bus_info
= {
12 .size
= sizeof(USBBus
),
13 .print_dev
= usb_bus_dev_print
,
14 .get_fw_dev_path
= usbbus_get_fw_dev_path
,
16 static int next_usb_bus
= 0;
17 static QTAILQ_HEAD(, USBBus
) busses
= QTAILQ_HEAD_INITIALIZER(busses
);
19 void usb_bus_new(USBBus
*bus
, DeviceState
*host
)
21 qbus_create_inplace(&bus
->qbus
, &usb_bus_info
, host
, NULL
);
22 bus
->busnr
= next_usb_bus
++;
23 bus
->qbus
.allow_hotplug
= 1; /* Yes, we can */
24 QTAILQ_INIT(&bus
->free
);
25 QTAILQ_INIT(&bus
->used
);
26 QTAILQ_INSERT_TAIL(&busses
, bus
, next
);
29 USBBus
*usb_bus_find(int busnr
)
34 return QTAILQ_FIRST(&busses
);
35 QTAILQ_FOREACH(bus
, &busses
, next
) {
36 if (bus
->busnr
== busnr
)
42 static int usb_qdev_init(DeviceState
*qdev
, DeviceInfo
*base
)
44 USBDevice
*dev
= DO_UPCAST(USBDevice
, qdev
, qdev
);
45 USBDeviceInfo
*info
= DO_UPCAST(USBDeviceInfo
, qdev
, base
);
48 pstrcpy(dev
->product_desc
, sizeof(dev
->product_desc
), info
->product_desc
);
51 QLIST_INIT(&dev
->strings
);
52 rc
= dev
->info
->init(dev
);
53 if (rc
== 0 && dev
->auto_attach
)
54 usb_device_attach(dev
);
58 static int usb_qdev_exit(DeviceState
*qdev
)
60 USBDevice
*dev
= DO_UPCAST(USBDevice
, qdev
, qdev
);
62 usb_device_detach(dev
);
63 if (dev
->info
->handle_destroy
) {
64 dev
->info
->handle_destroy(dev
);
69 void usb_qdev_register(USBDeviceInfo
*info
)
71 info
->qdev
.bus_info
= &usb_bus_info
;
72 info
->qdev
.init
= usb_qdev_init
;
73 info
->qdev
.unplug
= qdev_simple_unplug_cb
;
74 info
->qdev
.exit
= usb_qdev_exit
;
75 qdev_register(&info
->qdev
);
78 void usb_qdev_register_many(USBDeviceInfo
*info
)
80 while (info
->qdev
.name
) {
81 usb_qdev_register(info
);
86 USBDevice
*usb_create(USBBus
*bus
, const char *name
)
91 /* temporary stopgap until all usb is properly qdev-ified */
93 bus
= usb_bus_find(-1);
96 fprintf(stderr
, "%s: no bus specified, using \"%s\" for \"%s\"\n",
97 __FUNCTION__
, bus
->qbus
.name
, name
);
101 dev
= qdev_create(&bus
->qbus
, name
);
102 return DO_UPCAST(USBDevice
, qdev
, dev
);
105 USBDevice
*usb_create_simple(USBBus
*bus
, const char *name
)
107 USBDevice
*dev
= usb_create(bus
, name
);
109 hw_error("Failed to create USB device '%s'\n", name
);
111 qdev_init_nofail(&dev
->qdev
);
115 void usb_register_port(USBBus
*bus
, USBPort
*port
, void *opaque
, int index
,
116 USBDevice
*pdev
, USBPortOps
*ops
, int speedmask
)
118 port
->opaque
= opaque
;
121 port
->opaque
= opaque
;
124 port
->speedmask
= speedmask
;
125 QTAILQ_INSERT_TAIL(&bus
->free
, port
, next
);
129 void usb_unregister_port(USBBus
*bus
, USBPort
*port
)
132 qdev_free(&port
->dev
->qdev
);
133 QTAILQ_REMOVE(&bus
->free
, port
, next
);
137 static void do_attach(USBDevice
*dev
)
139 USBBus
*bus
= usb_bus_from_device(dev
);
143 fprintf(stderr
, "Warning: tried to attach usb device %s twice\n",
149 port
= QTAILQ_FIRST(&bus
->free
);
150 QTAILQ_REMOVE(&bus
->free
, port
, next
);
153 usb_attach(port
, dev
);
155 QTAILQ_INSERT_TAIL(&bus
->used
, port
, next
);
159 int usb_device_attach(USBDevice
*dev
)
161 USBBus
*bus
= usb_bus_from_device(dev
);
163 if (bus
->nfree
== 1) {
164 /* Create a new hub and chain it on. */
165 usb_create_simple(bus
, "usb-hub");
171 int usb_device_detach(USBDevice
*dev
)
173 USBBus
*bus
= usb_bus_from_device(dev
);
176 if (!dev
->attached
) {
177 fprintf(stderr
, "Warning: tried to detach unattached usb device %s\n",
183 QTAILQ_FOREACH(port
, &bus
->used
, next
) {
184 if (port
->dev
== dev
)
187 assert(port
!= NULL
);
189 QTAILQ_REMOVE(&bus
->used
, port
, next
);
192 usb_attach(port
, NULL
);
194 QTAILQ_INSERT_TAIL(&bus
->free
, port
, next
);
199 int usb_device_delete_addr(int busnr
, int addr
)
205 bus
= usb_bus_find(busnr
);
209 QTAILQ_FOREACH(port
, &bus
->used
, next
) {
210 if (port
->dev
->addr
== addr
)
217 qdev_free(&dev
->qdev
);
221 static const char *usb_speed(unsigned int speed
)
223 static const char *txt
[] = {
224 [ USB_SPEED_LOW
] = "1.5",
225 [ USB_SPEED_FULL
] = "12",
226 [ USB_SPEED_HIGH
] = "480",
228 if (speed
>= ARRAY_SIZE(txt
))
233 static void usb_bus_dev_print(Monitor
*mon
, DeviceState
*qdev
, int indent
)
235 USBDevice
*dev
= DO_UPCAST(USBDevice
, qdev
, qdev
);
236 USBBus
*bus
= usb_bus_from_device(dev
);
238 monitor_printf(mon
, "%*saddr %d.%d, speed %s, name %s%s\n",
239 indent
, "", bus
->busnr
, dev
->addr
,
240 usb_speed(dev
->speed
), dev
->product_desc
,
241 dev
->attached
? ", attached" : "");
244 void usb_info(Monitor
*mon
)
250 if (QTAILQ_EMPTY(&busses
)) {
251 monitor_printf(mon
, "USB support not enabled\n");
255 QTAILQ_FOREACH(bus
, &busses
, next
) {
256 QTAILQ_FOREACH(port
, &bus
->used
, next
) {
260 monitor_printf(mon
, " Device %d.%d, Speed %s Mb/s, Product %s\n",
261 bus
->busnr
, dev
->addr
, usb_speed(dev
->speed
),
267 /* handle legacy -usbdevice cmd line option */
268 USBDevice
*usbdevice_create(const char *cmdline
)
270 USBBus
*bus
= usb_bus_find(-1 /* any */);
277 params
= strchr(cmdline
,':');
280 len
= params
- cmdline
;
281 if (len
> sizeof(driver
))
282 len
= sizeof(driver
);
283 pstrcpy(driver
, len
, cmdline
);
286 pstrcpy(driver
, sizeof(driver
), cmdline
);
289 for (info
= device_info_list
; info
!= NULL
; info
= info
->next
) {
290 if (info
->bus_info
!= &usb_bus_info
)
292 usb
= DO_UPCAST(USBDeviceInfo
, qdev
, info
);
293 if (usb
->usbdevice_name
== NULL
)
295 if (strcmp(usb
->usbdevice_name
, driver
) != 0)
301 /* no error because some drivers are not converted (yet) */
302 error_report("usbdevice %s not found", driver
);
307 if (!usb
->usbdevice_init
) {
309 error_report("usbdevice %s accepts no params", driver
);
312 return usb_create_simple(bus
, usb
->qdev
.name
);
314 return usb
->usbdevice_init(params
);
317 static int usbbus_get_fw_dev_path_helper(USBDevice
*d
, USBBus
*bus
, char *p
,
323 QTAILQ_FOREACH(port
, &bus
->used
, next
) {
324 if (port
->dev
== d
) {
326 l
= usbbus_get_fw_dev_path_helper(port
->pdev
, bus
, p
, len
);
328 l
+= snprintf(p
+ l
, len
- l
, "%s@%x/", qdev_fw_name(&d
->qdev
),
337 static char *usbbus_get_fw_dev_path(DeviceState
*dev
)
339 USBDevice
*d
= (USBDevice
*)dev
;
340 USBBus
*bus
= usb_bus_from_device(d
);
344 assert(d
->attached
!= 0);
346 l
= usbbus_get_fw_dev_path_helper(d
, bus
, path
, sizeof(path
));