root / hw / usb-bus.c @ 3631e6c8
History | View | Annotate | Download (10.8 kB)
1 |
#include "hw.h" |
---|---|
2 |
#include "usb.h" |
3 |
#include "qdev.h" |
4 |
#include "sysemu.h" |
5 |
#include "monitor.h" |
6 |
|
7 |
static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent); |
8 |
|
9 |
static char *usb_get_dev_path(DeviceState *dev); |
10 |
static char *usb_get_fw_dev_path(DeviceState *qdev); |
11 |
|
12 |
static struct BusInfo usb_bus_info = { |
13 |
.name = "USB",
|
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() |
21 |
}, |
22 |
}; |
23 |
static int next_usb_bus = 0; |
24 |
static QTAILQ_HEAD(, USBBus) busses = QTAILQ_HEAD_INITIALIZER(busses);
|
25 |
|
26 |
const VMStateDescription vmstate_usb_device = {
|
27 |
.name = "USBDevice",
|
28 |
.version_id = 1,
|
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(), |
39 |
} |
40 |
}; |
41 |
|
42 |
void usb_bus_new(USBBus *bus, USBBusOps *ops, DeviceState *host)
|
43 |
{ |
44 |
qbus_create_inplace(&bus->qbus, &usb_bus_info, host, NULL);
|
45 |
bus->ops = ops; |
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); |
51 |
} |
52 |
|
53 |
USBBus *usb_bus_find(int busnr)
|
54 |
{ |
55 |
USBBus *bus; |
56 |
|
57 |
if (-1 == busnr) |
58 |
return QTAILQ_FIRST(&busses);
|
59 |
QTAILQ_FOREACH(bus, &busses, next) { |
60 |
if (bus->busnr == busnr)
|
61 |
return bus;
|
62 |
} |
63 |
return NULL; |
64 |
} |
65 |
|
66 |
static int usb_qdev_init(DeviceState *qdev, DeviceInfo *base) |
67 |
{ |
68 |
USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev); |
69 |
USBDeviceInfo *info = DO_UPCAST(USBDeviceInfo, qdev, base); |
70 |
int rc;
|
71 |
|
72 |
pstrcpy(dev->product_desc, sizeof(dev->product_desc), info->product_desc);
|
73 |
dev->info = info; |
74 |
dev->auto_attach = 1;
|
75 |
QLIST_INIT(&dev->strings); |
76 |
rc = dev->info->init(dev); |
77 |
if (rc == 0 && dev->auto_attach) |
78 |
rc = usb_device_attach(dev); |
79 |
return rc;
|
80 |
} |
81 |
|
82 |
static int usb_qdev_exit(DeviceState *qdev) |
83 |
{ |
84 |
USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev); |
85 |
USBBus *bus = usb_bus_from_device(dev); |
86 |
|
87 |
if (dev->attached) {
|
88 |
usb_device_detach(dev); |
89 |
} |
90 |
bus->ops->device_destroy(bus, dev); |
91 |
if (dev->info->handle_destroy) {
|
92 |
dev->info->handle_destroy(dev); |
93 |
} |
94 |
return 0; |
95 |
} |
96 |
|
97 |
void usb_qdev_register(USBDeviceInfo *info)
|
98 |
{ |
99 |
info->qdev.bus_info = &usb_bus_info; |
100 |
info->qdev.init = usb_qdev_init; |
101 |
info->qdev.unplug = qdev_simple_unplug_cb; |
102 |
info->qdev.exit = usb_qdev_exit; |
103 |
qdev_register(&info->qdev); |
104 |
} |
105 |
|
106 |
void usb_qdev_register_many(USBDeviceInfo *info)
|
107 |
{ |
108 |
while (info->qdev.name) {
|
109 |
usb_qdev_register(info); |
110 |
info++; |
111 |
} |
112 |
} |
113 |
|
114 |
USBDevice *usb_create(USBBus *bus, const char *name) |
115 |
{ |
116 |
DeviceState *dev; |
117 |
|
118 |
#if 1 |
119 |
/* temporary stopgap until all usb is properly qdev-ified */
|
120 |
if (!bus) {
|
121 |
bus = usb_bus_find(-1);
|
122 |
if (!bus)
|
123 |
return NULL; |
124 |
error_report("%s: no bus specified, using \"%s\" for \"%s\"\n",
|
125 |
__FUNCTION__, bus->qbus.name, name); |
126 |
} |
127 |
#endif
|
128 |
|
129 |
dev = qdev_create(&bus->qbus, name); |
130 |
return DO_UPCAST(USBDevice, qdev, dev);
|
131 |
} |
132 |
|
133 |
USBDevice *usb_create_simple(USBBus *bus, const char *name) |
134 |
{ |
135 |
USBDevice *dev = usb_create(bus, name); |
136 |
if (!dev) {
|
137 |
hw_error("Failed to create USB device '%s'\n", name);
|
138 |
} |
139 |
qdev_init_nofail(&dev->qdev); |
140 |
return dev;
|
141 |
} |
142 |
|
143 |
static void usb_fill_port(USBPort *port, void *opaque, int index, |
144 |
USBPortOps *ops, int speedmask)
|
145 |
{ |
146 |
port->opaque = opaque; |
147 |
port->index = index; |
148 |
port->opaque = opaque; |
149 |
port->index = index; |
150 |
port->ops = ops; |
151 |
port->speedmask = speedmask; |
152 |
usb_port_location(port, NULL, index + 1); |
153 |
} |
154 |
|
155 |
void usb_register_port(USBBus *bus, USBPort *port, void *opaque, int index, |
156 |
USBPortOps *ops, int speedmask)
|
157 |
{ |
158 |
usb_fill_port(port, opaque, index, ops, speedmask); |
159 |
QTAILQ_INSERT_TAIL(&bus->free, port, next); |
160 |
bus->nfree++; |
161 |
} |
162 |
|
163 |
void usb_port_location(USBPort *downstream, USBPort *upstream, int portnr) |
164 |
{ |
165 |
if (upstream) {
|
166 |
snprintf(downstream->path, sizeof(downstream->path), "%s.%d", |
167 |
upstream->path, portnr); |
168 |
} else {
|
169 |
snprintf(downstream->path, sizeof(downstream->path), "%d", portnr); |
170 |
} |
171 |
} |
172 |
|
173 |
void usb_unregister_port(USBBus *bus, USBPort *port)
|
174 |
{ |
175 |
if (port->dev)
|
176 |
qdev_free(&port->dev->qdev); |
177 |
QTAILQ_REMOVE(&bus->free, port, next); |
178 |
bus->nfree--; |
179 |
} |
180 |
|
181 |
static int do_attach(USBDevice *dev) |
182 |
{ |
183 |
USBBus *bus = usb_bus_from_device(dev); |
184 |
USBPort *port; |
185 |
|
186 |
if (dev->attached) {
|
187 |
error_report("Error: tried to attach usb device %s twice\n",
|
188 |
dev->product_desc); |
189 |
return -1; |
190 |
} |
191 |
if (bus->nfree == 0) { |
192 |
error_report("Error: tried to attach usb device %s to a bus with no free ports\n",
|
193 |
dev->product_desc); |
194 |
return -1; |
195 |
} |
196 |
if (dev->port_path) {
|
197 |
QTAILQ_FOREACH(port, &bus->free, next) { |
198 |
if (strcmp(port->path, dev->port_path) == 0) { |
199 |
break;
|
200 |
} |
201 |
} |
202 |
if (port == NULL) { |
203 |
error_report("Error: usb port %s (bus %s) not found\n",
|
204 |
dev->port_path, bus->qbus.name); |
205 |
return -1; |
206 |
} |
207 |
} else {
|
208 |
port = QTAILQ_FIRST(&bus->free); |
209 |
} |
210 |
if (!(port->speedmask & dev->speedmask)) {
|
211 |
error_report("Warning: speed mismatch trying to attach usb device %s to bus %s\n",
|
212 |
dev->product_desc, bus->qbus.name); |
213 |
return -1; |
214 |
} |
215 |
|
216 |
dev->attached++; |
217 |
QTAILQ_REMOVE(&bus->free, port, next); |
218 |
bus->nfree--; |
219 |
|
220 |
usb_attach(port, dev); |
221 |
|
222 |
QTAILQ_INSERT_TAIL(&bus->used, port, next); |
223 |
bus->nused++; |
224 |
|
225 |
return 0; |
226 |
} |
227 |
|
228 |
int usb_device_attach(USBDevice *dev)
|
229 |
{ |
230 |
USBBus *bus = usb_bus_from_device(dev); |
231 |
|
232 |
if (bus->nfree == 1 && dev->port_path == NULL) { |
233 |
/* Create a new hub and chain it on
|
234 |
(unless a physical port location is specified). */
|
235 |
usb_create_simple(bus, "usb-hub");
|
236 |
} |
237 |
return do_attach(dev);
|
238 |
} |
239 |
|
240 |
int usb_device_detach(USBDevice *dev)
|
241 |
{ |
242 |
USBBus *bus = usb_bus_from_device(dev); |
243 |
USBPort *port; |
244 |
|
245 |
if (!dev->attached) {
|
246 |
error_report("Error: tried to detach unattached usb device %s\n",
|
247 |
dev->product_desc); |
248 |
return -1; |
249 |
} |
250 |
dev->attached--; |
251 |
|
252 |
QTAILQ_FOREACH(port, &bus->used, next) { |
253 |
if (port->dev == dev)
|
254 |
break;
|
255 |
} |
256 |
assert(port != NULL);
|
257 |
|
258 |
QTAILQ_REMOVE(&bus->used, port, next); |
259 |
bus->nused--; |
260 |
|
261 |
usb_attach(port, NULL);
|
262 |
|
263 |
QTAILQ_INSERT_TAIL(&bus->free, port, next); |
264 |
bus->nfree++; |
265 |
return 0; |
266 |
} |
267 |
|
268 |
int usb_device_delete_addr(int busnr, int addr) |
269 |
{ |
270 |
USBBus *bus; |
271 |
USBPort *port; |
272 |
USBDevice *dev; |
273 |
|
274 |
bus = usb_bus_find(busnr); |
275 |
if (!bus)
|
276 |
return -1; |
277 |
|
278 |
QTAILQ_FOREACH(port, &bus->used, next) { |
279 |
if (port->dev->addr == addr)
|
280 |
break;
|
281 |
} |
282 |
if (!port)
|
283 |
return -1; |
284 |
dev = port->dev; |
285 |
|
286 |
qdev_free(&dev->qdev); |
287 |
return 0; |
288 |
} |
289 |
|
290 |
static const char *usb_speed(unsigned int speed) |
291 |
{ |
292 |
static const char *txt[] = { |
293 |
[ USB_SPEED_LOW ] = "1.5",
|
294 |
[ USB_SPEED_FULL ] = "12",
|
295 |
[ USB_SPEED_HIGH ] = "480",
|
296 |
[ USB_SPEED_SUPER ] = "5000",
|
297 |
}; |
298 |
if (speed >= ARRAY_SIZE(txt))
|
299 |
return "?"; |
300 |
return txt[speed];
|
301 |
} |
302 |
|
303 |
static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent) |
304 |
{ |
305 |
USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev); |
306 |
USBBus *bus = usb_bus_from_device(dev); |
307 |
|
308 |
monitor_printf(mon, "%*saddr %d.%d, port %s, speed %s, name %s%s\n",
|
309 |
indent, "", bus->busnr, dev->addr,
|
310 |
dev->port ? dev->port->path : "-",
|
311 |
usb_speed(dev->speed), dev->product_desc, |
312 |
dev->attached ? ", attached" : ""); |
313 |
} |
314 |
|
315 |
static char *usb_get_dev_path(DeviceState *qdev) |
316 |
{ |
317 |
USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev); |
318 |
return qemu_strdup(dev->port->path);
|
319 |
} |
320 |
|
321 |
static char *usb_get_fw_dev_path(DeviceState *qdev) |
322 |
{ |
323 |
USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev); |
324 |
char *fw_path, *in;
|
325 |
ssize_t pos = 0, fw_len;
|
326 |
long nr;
|
327 |
|
328 |
fw_len = 32 + strlen(dev->port->path) * 6; |
329 |
fw_path = qemu_malloc(fw_len); |
330 |
in = dev->port->path; |
331 |
while (fw_len - pos > 0) { |
332 |
nr = strtol(in, &in, 10);
|
333 |
if (in[0] == '.') { |
334 |
/* some hub between root port and device */
|
335 |
pos += snprintf(fw_path + pos, fw_len - pos, "hub@%ld/", nr);
|
336 |
in++; |
337 |
} else {
|
338 |
/* the device itself */
|
339 |
pos += snprintf(fw_path + pos, fw_len - pos, "%s@%ld",
|
340 |
qdev_fw_name(qdev), nr); |
341 |
break;
|
342 |
} |
343 |
} |
344 |
return fw_path;
|
345 |
} |
346 |
|
347 |
void usb_info(Monitor *mon)
|
348 |
{ |
349 |
USBBus *bus; |
350 |
USBDevice *dev; |
351 |
USBPort *port; |
352 |
|
353 |
if (QTAILQ_EMPTY(&busses)) {
|
354 |
monitor_printf(mon, "USB support not enabled\n");
|
355 |
return;
|
356 |
} |
357 |
|
358 |
QTAILQ_FOREACH(bus, &busses, next) { |
359 |
QTAILQ_FOREACH(port, &bus->used, next) { |
360 |
dev = port->dev; |
361 |
if (!dev)
|
362 |
continue;
|
363 |
monitor_printf(mon, " Device %d.%d, Port %s, Speed %s Mb/s, Product %s\n",
|
364 |
bus->busnr, dev->addr, port->path, usb_speed(dev->speed), |
365 |
dev->product_desc); |
366 |
} |
367 |
} |
368 |
} |
369 |
|
370 |
/* handle legacy -usbdevice cmd line option */
|
371 |
USBDevice *usbdevice_create(const char *cmdline) |
372 |
{ |
373 |
USBBus *bus = usb_bus_find(-1 /* any */); |
374 |
DeviceInfo *info; |
375 |
USBDeviceInfo *usb; |
376 |
char driver[32]; |
377 |
const char *params; |
378 |
int len;
|
379 |
|
380 |
params = strchr(cmdline,':');
|
381 |
if (params) {
|
382 |
params++; |
383 |
len = params - cmdline; |
384 |
if (len > sizeof(driver)) |
385 |
len = sizeof(driver);
|
386 |
pstrcpy(driver, len, cmdline); |
387 |
} else {
|
388 |
params = "";
|
389 |
pstrcpy(driver, sizeof(driver), cmdline);
|
390 |
} |
391 |
|
392 |
for (info = device_info_list; info != NULL; info = info->next) { |
393 |
if (info->bus_info != &usb_bus_info)
|
394 |
continue;
|
395 |
usb = DO_UPCAST(USBDeviceInfo, qdev, info); |
396 |
if (usb->usbdevice_name == NULL) |
397 |
continue;
|
398 |
if (strcmp(usb->usbdevice_name, driver) != 0) |
399 |
continue;
|
400 |
break;
|
401 |
} |
402 |
if (info == NULL) { |
403 |
#if 0
|
404 |
/* no error because some drivers are not converted (yet) */
|
405 |
error_report("usbdevice %s not found", driver);
|
406 |
#endif
|
407 |
return NULL; |
408 |
} |
409 |
|
410 |
if (!usb->usbdevice_init) {
|
411 |
if (*params) {
|
412 |
error_report("usbdevice %s accepts no params", driver);
|
413 |
return NULL; |
414 |
} |
415 |
return usb_create_simple(bus, usb->qdev.name);
|
416 |
} |
417 |
return usb->usbdevice_init(params);
|
418 |
} |