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 void usb_bus_new(USBBus
*bus
, DeviceState
*host
)
28 qbus_create_inplace(&bus
->qbus
, &usb_bus_info
, host
, NULL
);
29 bus
->busnr
= next_usb_bus
++;
30 bus
->qbus
.allow_hotplug
= 1; /* Yes, we can */
31 QTAILQ_INIT(&bus
->free
);
32 QTAILQ_INIT(&bus
->used
);
33 QTAILQ_INSERT_TAIL(&busses
, bus
, next
);
36 USBBus
*usb_bus_find(int busnr
)
41 return QTAILQ_FIRST(&busses
);
42 QTAILQ_FOREACH(bus
, &busses
, next
) {
43 if (bus
->busnr
== busnr
)
49 static int usb_qdev_init(DeviceState
*qdev
, DeviceInfo
*base
)
51 USBDevice
*dev
= DO_UPCAST(USBDevice
, qdev
, qdev
);
52 USBDeviceInfo
*info
= DO_UPCAST(USBDeviceInfo
, qdev
, base
);
55 pstrcpy(dev
->product_desc
, sizeof(dev
->product_desc
), info
->product_desc
);
58 QLIST_INIT(&dev
->strings
);
59 rc
= dev
->info
->init(dev
);
60 if (rc
== 0 && dev
->auto_attach
)
61 usb_device_attach(dev
);
65 static int usb_qdev_exit(DeviceState
*qdev
)
67 USBDevice
*dev
= DO_UPCAST(USBDevice
, qdev
, qdev
);
69 usb_device_detach(dev
);
70 if (dev
->info
->handle_destroy
) {
71 dev
->info
->handle_destroy(dev
);
76 void usb_qdev_register(USBDeviceInfo
*info
)
78 info
->qdev
.bus_info
= &usb_bus_info
;
79 info
->qdev
.init
= usb_qdev_init
;
80 info
->qdev
.unplug
= qdev_simple_unplug_cb
;
81 info
->qdev
.exit
= usb_qdev_exit
;
82 qdev_register(&info
->qdev
);
85 void usb_qdev_register_many(USBDeviceInfo
*info
)
87 while (info
->qdev
.name
) {
88 usb_qdev_register(info
);
93 USBDevice
*usb_create(USBBus
*bus
, const char *name
)
98 /* temporary stopgap until all usb is properly qdev-ified */
100 bus
= usb_bus_find(-1);
103 fprintf(stderr
, "%s: no bus specified, using \"%s\" for \"%s\"\n",
104 __FUNCTION__
, bus
->qbus
.name
, name
);
108 dev
= qdev_create(&bus
->qbus
, name
);
109 return DO_UPCAST(USBDevice
, qdev
, dev
);
112 USBDevice
*usb_create_simple(USBBus
*bus
, const char *name
)
114 USBDevice
*dev
= usb_create(bus
, name
);
116 hw_error("Failed to create USB device '%s'\n", name
);
118 qdev_init_nofail(&dev
->qdev
);
122 void usb_register_port(USBBus
*bus
, USBPort
*port
, void *opaque
, int index
,
123 USBPortOps
*ops
, int speedmask
)
125 port
->opaque
= opaque
;
127 port
->opaque
= opaque
;
130 port
->speedmask
= speedmask
;
131 QTAILQ_INSERT_TAIL(&bus
->free
, port
, next
);
135 void usb_port_location(USBPort
*downstream
, USBPort
*upstream
, int portnr
)
138 snprintf(downstream
->path
, sizeof(downstream
->path
), "%s.%d",
139 upstream
->path
, portnr
);
141 snprintf(downstream
->path
, sizeof(downstream
->path
), "%d", portnr
);
145 void usb_unregister_port(USBBus
*bus
, USBPort
*port
)
148 qdev_free(&port
->dev
->qdev
);
149 QTAILQ_REMOVE(&bus
->free
, port
, next
);
153 static void do_attach(USBDevice
*dev
)
155 USBBus
*bus
= usb_bus_from_device(dev
);
159 fprintf(stderr
, "Warning: tried to attach usb device %s twice\n",
163 if (dev
->port_path
) {
164 QTAILQ_FOREACH(port
, &bus
->free
, next
) {
165 if (strcmp(port
->path
, dev
->port_path
) == 0) {
170 fprintf(stderr
, "Warning: usb port %s (bus %s) not found\n",
171 dev
->port_path
, bus
->qbus
.name
);
175 port
= QTAILQ_FIRST(&bus
->free
);
179 QTAILQ_REMOVE(&bus
->free
, port
, next
);
182 usb_attach(port
, dev
);
184 QTAILQ_INSERT_TAIL(&bus
->used
, port
, next
);
188 int usb_device_attach(USBDevice
*dev
)
190 USBBus
*bus
= usb_bus_from_device(dev
);
192 if (bus
->nfree
== 1 && dev
->port_path
== NULL
) {
193 /* Create a new hub and chain it on
194 (unless a physical port location is specified). */
195 usb_create_simple(bus
, "usb-hub");
201 int usb_device_detach(USBDevice
*dev
)
203 USBBus
*bus
= usb_bus_from_device(dev
);
206 if (!dev
->attached
) {
207 fprintf(stderr
, "Warning: tried to detach unattached usb device %s\n",
213 QTAILQ_FOREACH(port
, &bus
->used
, next
) {
214 if (port
->dev
== dev
)
217 assert(port
!= NULL
);
219 QTAILQ_REMOVE(&bus
->used
, port
, next
);
222 usb_attach(port
, NULL
);
224 QTAILQ_INSERT_TAIL(&bus
->free
, port
, next
);
229 int usb_device_delete_addr(int busnr
, int addr
)
235 bus
= usb_bus_find(busnr
);
239 QTAILQ_FOREACH(port
, &bus
->used
, next
) {
240 if (port
->dev
->addr
== addr
)
247 qdev_free(&dev
->qdev
);
251 static const char *usb_speed(unsigned int speed
)
253 static const char *txt
[] = {
254 [ USB_SPEED_LOW
] = "1.5",
255 [ USB_SPEED_FULL
] = "12",
256 [ USB_SPEED_HIGH
] = "480",
258 if (speed
>= ARRAY_SIZE(txt
))
263 static void usb_bus_dev_print(Monitor
*mon
, DeviceState
*qdev
, int indent
)
265 USBDevice
*dev
= DO_UPCAST(USBDevice
, qdev
, qdev
);
266 USBBus
*bus
= usb_bus_from_device(dev
);
268 monitor_printf(mon
, "%*saddr %d.%d, port %s, speed %s, name %s%s\n",
269 indent
, "", bus
->busnr
, dev
->addr
,
270 dev
->port
? dev
->port
->path
: "-",
271 usb_speed(dev
->speed
), dev
->product_desc
,
272 dev
->attached
? ", attached" : "");
275 static char *usb_get_dev_path(DeviceState
*qdev
)
277 USBDevice
*dev
= DO_UPCAST(USBDevice
, qdev
, qdev
);
278 return qemu_strdup(dev
->port
->path
);
281 static char *usb_get_fw_dev_path(DeviceState
*qdev
)
283 USBDevice
*dev
= DO_UPCAST(USBDevice
, qdev
, qdev
);
288 fw_path
= qemu_malloc(32 + strlen(dev
->port
->path
) * 6);
289 in
= dev
->port
->path
;
291 nr
= strtol(in
, &in
, 10);
293 /* some hub between root port and device */
294 pos
+= sprintf(fw_path
+ pos
, "hub@%ld/", nr
);
297 /* the device itself */
298 pos
+= sprintf(fw_path
+ pos
, "%s@%ld", qdev_fw_name(qdev
), nr
);
305 void usb_info(Monitor
*mon
)
311 if (QTAILQ_EMPTY(&busses
)) {
312 monitor_printf(mon
, "USB support not enabled\n");
316 QTAILQ_FOREACH(bus
, &busses
, next
) {
317 QTAILQ_FOREACH(port
, &bus
->used
, next
) {
321 monitor_printf(mon
, " Device %d.%d, Port %s, Speed %s Mb/s, Product %s\n",
322 bus
->busnr
, dev
->addr
, port
->path
, usb_speed(dev
->speed
),
328 /* handle legacy -usbdevice cmd line option */
329 USBDevice
*usbdevice_create(const char *cmdline
)
331 USBBus
*bus
= usb_bus_find(-1 /* any */);
338 params
= strchr(cmdline
,':');
341 len
= params
- cmdline
;
342 if (len
> sizeof(driver
))
343 len
= sizeof(driver
);
344 pstrcpy(driver
, len
, cmdline
);
347 pstrcpy(driver
, sizeof(driver
), cmdline
);
350 for (info
= device_info_list
; info
!= NULL
; info
= info
->next
) {
351 if (info
->bus_info
!= &usb_bus_info
)
353 usb
= DO_UPCAST(USBDeviceInfo
, qdev
, info
);
354 if (usb
->usbdevice_name
== NULL
)
356 if (strcmp(usb
->usbdevice_name
, driver
) != 0)
362 /* no error because some drivers are not converted (yet) */
363 error_report("usbdevice %s not found", driver
);
368 if (!usb
->usbdevice_init
) {
370 error_report("usbdevice %s accepts no params", driver
);
373 return usb_create_simple(bus
, usb
->qdev
.name
);
375 return usb
->usbdevice_init(params
);