]>
Commit | Line | Data |
---|---|---|
806b6024 GH |
1 | #include "hw.h" |
2 | #include "usb.h" | |
3 | #include "qdev.h" | |
a5d2f727 GH |
4 | #include "sysemu.h" |
5 | #include "monitor.h" | |
891fb2cd | 6 | #include "trace.h" |
a5d2f727 GH |
7 | |
8 | static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent); | |
c7a2196a GH |
9 | |
10 | static char *usb_get_dev_path(DeviceState *dev); | |
70d31cb2 | 11 | static char *usb_get_fw_dev_path(DeviceState *qdev); |
f462141f | 12 | static int usb_qdev_exit(DeviceState *qdev); |
806b6024 GH |
13 | |
14 | static struct BusInfo usb_bus_info = { | |
a5d2f727 GH |
15 | .name = "USB", |
16 | .size = sizeof(USBBus), | |
17 | .print_dev = usb_bus_dev_print, | |
c7a2196a | 18 | .get_dev_path = usb_get_dev_path, |
70d31cb2 | 19 | .get_fw_dev_path = usb_get_fw_dev_path, |
5f69076b GH |
20 | .props = (Property[]) { |
21 | DEFINE_PROP_STRING("port", USBDevice, port_path), | |
22 | DEFINE_PROP_END_OF_LIST() | |
23 | }, | |
806b6024 GH |
24 | }; |
25 | static int next_usb_bus = 0; | |
72cf2d4f | 26 | static QTAILQ_HEAD(, USBBus) busses = QTAILQ_HEAD_INITIALIZER(busses); |
806b6024 | 27 | |
c1ecb40a GH |
28 | const VMStateDescription vmstate_usb_device = { |
29 | .name = "USBDevice", | |
30 | .version_id = 1, | |
31 | .minimum_version_id = 1, | |
32 | .fields = (VMStateField []) { | |
33 | VMSTATE_UINT8(addr, USBDevice), | |
34 | VMSTATE_INT32(state, USBDevice), | |
35 | VMSTATE_INT32(remote_wakeup, USBDevice), | |
36 | VMSTATE_INT32(setup_state, USBDevice), | |
37 | VMSTATE_INT32(setup_len, USBDevice), | |
38 | VMSTATE_INT32(setup_index, USBDevice), | |
39 | VMSTATE_UINT8_ARRAY(setup_buf, USBDevice, 8), | |
40 | VMSTATE_END_OF_LIST(), | |
41 | } | |
42 | }; | |
43 | ||
07771f6f | 44 | void usb_bus_new(USBBus *bus, USBBusOps *ops, DeviceState *host) |
806b6024 | 45 | { |
b2317837 | 46 | qbus_create_inplace(&bus->qbus, &usb_bus_info, host, NULL); |
07771f6f | 47 | bus->ops = ops; |
806b6024 | 48 | bus->busnr = next_usb_bus++; |
ef816d83 | 49 | bus->qbus.allow_hotplug = 1; /* Yes, we can */ |
72cf2d4f BS |
50 | QTAILQ_INIT(&bus->free); |
51 | QTAILQ_INIT(&bus->used); | |
52 | QTAILQ_INSERT_TAIL(&busses, bus, next); | |
806b6024 GH |
53 | } |
54 | ||
55 | USBBus *usb_bus_find(int busnr) | |
56 | { | |
57 | USBBus *bus; | |
58 | ||
59 | if (-1 == busnr) | |
72cf2d4f BS |
60 | return QTAILQ_FIRST(&busses); |
61 | QTAILQ_FOREACH(bus, &busses, next) { | |
806b6024 GH |
62 | if (bus->busnr == busnr) |
63 | return bus; | |
64 | } | |
65 | return NULL; | |
66 | } | |
67 | ||
68 | static int usb_qdev_init(DeviceState *qdev, DeviceInfo *base) | |
69 | { | |
70 | USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev); | |
71 | USBDeviceInfo *info = DO_UPCAST(USBDeviceInfo, qdev, base); | |
72 | int rc; | |
73 | ||
06384698 | 74 | pstrcpy(dev->product_desc, sizeof(dev->product_desc), info->product_desc); |
806b6024 | 75 | dev->info = info; |
61e094c0 | 76 | dev->auto_attach = 1; |
132a3f55 | 77 | QLIST_INIT(&dev->strings); |
891fb2cd | 78 | rc = usb_claim_port(dev); |
f462141f | 79 | if (rc != 0) { |
db3a5ed7 | 80 | return rc; |
891fb2cd | 81 | } |
f462141f GH |
82 | rc = dev->info->init(dev); |
83 | if (rc != 0) { | |
db3a5ed7 SH |
84 | usb_release_port(dev); |
85 | return rc; | |
f462141f GH |
86 | } |
87 | if (dev->auto_attach) { | |
fa19bf83 | 88 | rc = usb_device_attach(dev); |
f462141f | 89 | if (rc != 0) { |
db3a5ed7 SH |
90 | usb_qdev_exit(qdev); |
91 | return rc; | |
f462141f | 92 | } |
891fb2cd | 93 | } |
f462141f | 94 | return 0; |
806b6024 GH |
95 | } |
96 | ||
a8e662b5 GH |
97 | static int usb_qdev_exit(DeviceState *qdev) |
98 | { | |
99 | USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev); | |
100 | ||
290a5c60 HG |
101 | if (dev->attached) { |
102 | usb_device_detach(dev); | |
103 | } | |
a8e662b5 GH |
104 | if (dev->info->handle_destroy) { |
105 | dev->info->handle_destroy(dev); | |
106 | } | |
891fb2cd GH |
107 | if (dev->port) { |
108 | usb_release_port(dev); | |
109 | } | |
a8e662b5 GH |
110 | return 0; |
111 | } | |
112 | ||
806b6024 GH |
113 | void usb_qdev_register(USBDeviceInfo *info) |
114 | { | |
115 | info->qdev.bus_info = &usb_bus_info; | |
116 | info->qdev.init = usb_qdev_init; | |
ef816d83 | 117 | info->qdev.unplug = qdev_simple_unplug_cb; |
a8e662b5 | 118 | info->qdev.exit = usb_qdev_exit; |
806b6024 GH |
119 | qdev_register(&info->qdev); |
120 | } | |
121 | ||
122 | void usb_qdev_register_many(USBDeviceInfo *info) | |
123 | { | |
124 | while (info->qdev.name) { | |
125 | usb_qdev_register(info); | |
126 | info++; | |
127 | } | |
128 | } | |
129 | ||
a5d2f727 | 130 | USBDevice *usb_create(USBBus *bus, const char *name) |
806b6024 GH |
131 | { |
132 | DeviceState *dev; | |
133 | ||
134 | #if 1 | |
135 | /* temporary stopgap until all usb is properly qdev-ified */ | |
136 | if (!bus) { | |
137 | bus = usb_bus_find(-1); | |
138 | if (!bus) | |
139 | return NULL; | |
be62a2eb | 140 | error_report("%s: no bus specified, using \"%s\" for \"%s\"", |
806b6024 GH |
141 | __FUNCTION__, bus->qbus.name, name); |
142 | } | |
143 | #endif | |
144 | ||
145 | dev = qdev_create(&bus->qbus, name); | |
806b6024 GH |
146 | return DO_UPCAST(USBDevice, qdev, dev); |
147 | } | |
a5d2f727 GH |
148 | |
149 | USBDevice *usb_create_simple(USBBus *bus, const char *name) | |
150 | { | |
151 | USBDevice *dev = usb_create(bus, name); | |
2af2a1b8 GH |
152 | int rc; |
153 | ||
d44168ff | 154 | if (!dev) { |
be62a2eb | 155 | error_report("Failed to create USB device '%s'", name); |
2af2a1b8 GH |
156 | return NULL; |
157 | } | |
158 | rc = qdev_init(&dev->qdev); | |
159 | if (rc < 0) { | |
be62a2eb | 160 | error_report("Failed to initialize USB device '%s'", name); |
2af2a1b8 | 161 | return NULL; |
d44168ff | 162 | } |
a5d2f727 GH |
163 | return dev; |
164 | } | |
165 | ||
090ac642 HG |
166 | static void usb_fill_port(USBPort *port, void *opaque, int index, |
167 | USBPortOps *ops, int speedmask) | |
a5d2f727 | 168 | { |
0d86d2be GH |
169 | port->opaque = opaque; |
170 | port->index = index; | |
171 | port->ops = ops; | |
843d4e0c | 172 | port->speedmask = speedmask; |
3631e6c8 | 173 | usb_port_location(port, NULL, index + 1); |
090ac642 HG |
174 | } |
175 | ||
176 | void usb_register_port(USBBus *bus, USBPort *port, void *opaque, int index, | |
177 | USBPortOps *ops, int speedmask) | |
178 | { | |
179 | usb_fill_port(port, opaque, index, ops, speedmask); | |
72cf2d4f | 180 | QTAILQ_INSERT_TAIL(&bus->free, port, next); |
a5d2f727 GH |
181 | bus->nfree++; |
182 | } | |
183 | ||
ae60fea9 HG |
184 | int usb_register_companion(const char *masterbus, USBPort *ports[], |
185 | uint32_t portcount, uint32_t firstport, | |
186 | void *opaque, USBPortOps *ops, int speedmask) | |
187 | { | |
188 | USBBus *bus; | |
189 | int i; | |
190 | ||
191 | QTAILQ_FOREACH(bus, &busses, next) { | |
192 | if (strcmp(bus->qbus.name, masterbus) == 0) { | |
193 | break; | |
194 | } | |
195 | } | |
196 | ||
197 | if (!bus || !bus->ops->register_companion) { | |
198 | qerror_report(QERR_INVALID_PARAMETER_VALUE, "masterbus", | |
199 | "an USB masterbus"); | |
200 | if (bus) { | |
201 | error_printf_unless_qmp( | |
202 | "USB bus '%s' does not allow companion controllers\n", | |
203 | masterbus); | |
204 | } | |
205 | return -1; | |
206 | } | |
207 | ||
208 | for (i = 0; i < portcount; i++) { | |
209 | usb_fill_port(ports[i], opaque, i, ops, speedmask); | |
210 | } | |
211 | ||
212 | return bus->ops->register_companion(bus, ports, portcount, firstport); | |
213 | } | |
214 | ||
c7a2196a GH |
215 | void usb_port_location(USBPort *downstream, USBPort *upstream, int portnr) |
216 | { | |
217 | if (upstream) { | |
218 | snprintf(downstream->path, sizeof(downstream->path), "%s.%d", | |
219 | upstream->path, portnr); | |
220 | } else { | |
221 | snprintf(downstream->path, sizeof(downstream->path), "%d", portnr); | |
222 | } | |
223 | } | |
224 | ||
a8e662b5 GH |
225 | void usb_unregister_port(USBBus *bus, USBPort *port) |
226 | { | |
227 | if (port->dev) | |
228 | qdev_free(&port->dev->qdev); | |
229 | QTAILQ_REMOVE(&bus->free, port, next); | |
230 | bus->nfree--; | |
231 | } | |
232 | ||
891fb2cd | 233 | int usb_claim_port(USBDevice *dev) |
a5d2f727 GH |
234 | { |
235 | USBBus *bus = usb_bus_from_device(dev); | |
236 | USBPort *port; | |
237 | ||
891fb2cd GH |
238 | assert(dev->port == NULL); |
239 | ||
5f69076b GH |
240 | if (dev->port_path) { |
241 | QTAILQ_FOREACH(port, &bus->free, next) { | |
242 | if (strcmp(port->path, dev->port_path) == 0) { | |
243 | break; | |
244 | } | |
245 | } | |
246 | if (port == NULL) { | |
be62a2eb | 247 | error_report("Error: usb port %s (bus %s) not found (in use?)", |
891fb2cd | 248 | dev->port_path, bus->qbus.name); |
fa19bf83 | 249 | return -1; |
5f69076b GH |
250 | } |
251 | } else { | |
891fb2cd GH |
252 | if (bus->nfree == 1 && strcmp(dev->qdev.info->name, "usb-hub") != 0) { |
253 | /* Create a new hub and chain it on */ | |
254 | usb_create_simple(bus, "usb-hub"); | |
255 | } | |
256 | if (bus->nfree == 0) { | |
257 | error_report("Error: tried to attach usb device %s to a bus " | |
be62a2eb | 258 | "with no free ports", dev->product_desc); |
891fb2cd GH |
259 | return -1; |
260 | } | |
5f69076b GH |
261 | port = QTAILQ_FIRST(&bus->free); |
262 | } | |
891fb2cd | 263 | trace_usb_port_claim(bus->busnr, port->path); |
a5d2f727 | 264 | |
72cf2d4f | 265 | QTAILQ_REMOVE(&bus->free, port, next); |
a5d2f727 GH |
266 | bus->nfree--; |
267 | ||
891fb2cd GH |
268 | dev->port = port; |
269 | port->dev = dev; | |
a5d2f727 | 270 | |
72cf2d4f | 271 | QTAILQ_INSERT_TAIL(&bus->used, port, next); |
a5d2f727 | 272 | bus->nused++; |
fa19bf83 | 273 | return 0; |
a5d2f727 GH |
274 | } |
275 | ||
891fb2cd | 276 | void usb_release_port(USBDevice *dev) |
a5d2f727 GH |
277 | { |
278 | USBBus *bus = usb_bus_from_device(dev); | |
891fb2cd | 279 | USBPort *port = dev->port; |
a5d2f727 | 280 | |
891fb2cd GH |
281 | assert(port != NULL); |
282 | trace_usb_port_release(bus->busnr, port->path); | |
283 | ||
284 | QTAILQ_REMOVE(&bus->used, port, next); | |
285 | bus->nused--; | |
286 | ||
287 | dev->port = NULL; | |
288 | port->dev = NULL; | |
289 | ||
290 | QTAILQ_INSERT_TAIL(&bus->free, port, next); | |
291 | bus->nfree++; | |
a5d2f727 GH |
292 | } |
293 | ||
891fb2cd | 294 | int usb_device_attach(USBDevice *dev) |
a8e662b5 GH |
295 | { |
296 | USBBus *bus = usb_bus_from_device(dev); | |
891fb2cd | 297 | USBPort *port = dev->port; |
a8e662b5 | 298 | |
891fb2cd GH |
299 | assert(port != NULL); |
300 | assert(!dev->attached); | |
301 | trace_usb_port_attach(bus->busnr, port->path); | |
302 | ||
303 | if (!(port->speedmask & dev->speedmask)) { | |
304 | error_report("Warning: speed mismatch trying to attach " | |
be62a2eb | 305 | "usb device %s to bus %s", |
891fb2cd | 306 | dev->product_desc, bus->qbus.name); |
a8e662b5 GH |
307 | return -1; |
308 | } | |
a8e662b5 | 309 | |
891fb2cd GH |
310 | dev->attached++; |
311 | usb_attach(port); | |
a8e662b5 | 312 | |
891fb2cd GH |
313 | return 0; |
314 | } | |
315 | ||
316 | int usb_device_detach(USBDevice *dev) | |
317 | { | |
318 | USBBus *bus = usb_bus_from_device(dev); | |
319 | USBPort *port = dev->port; | |
a8e662b5 | 320 | |
891fb2cd GH |
321 | assert(port != NULL); |
322 | assert(dev->attached); | |
323 | trace_usb_port_detach(bus->busnr, port->path); | |
a8e662b5 | 324 | |
891fb2cd GH |
325 | usb_detach(port); |
326 | dev->attached--; | |
a8e662b5 GH |
327 | return 0; |
328 | } | |
329 | ||
a5d2f727 GH |
330 | int usb_device_delete_addr(int busnr, int addr) |
331 | { | |
332 | USBBus *bus; | |
333 | USBPort *port; | |
334 | USBDevice *dev; | |
335 | ||
336 | bus = usb_bus_find(busnr); | |
337 | if (!bus) | |
338 | return -1; | |
339 | ||
72cf2d4f | 340 | QTAILQ_FOREACH(port, &bus->used, next) { |
a5d2f727 GH |
341 | if (port->dev->addr == addr) |
342 | break; | |
343 | } | |
344 | if (!port) | |
345 | return -1; | |
a5d2f727 | 346 | dev = port->dev; |
a5d2f727 | 347 | |
a8e662b5 | 348 | qdev_free(&dev->qdev); |
a5d2f727 GH |
349 | return 0; |
350 | } | |
351 | ||
352 | static const char *usb_speed(unsigned int speed) | |
353 | { | |
354 | static const char *txt[] = { | |
355 | [ USB_SPEED_LOW ] = "1.5", | |
356 | [ USB_SPEED_FULL ] = "12", | |
357 | [ USB_SPEED_HIGH ] = "480", | |
290d26d2 | 358 | [ USB_SPEED_SUPER ] = "5000", |
a5d2f727 GH |
359 | }; |
360 | if (speed >= ARRAY_SIZE(txt)) | |
361 | return "?"; | |
362 | return txt[speed]; | |
363 | } | |
364 | ||
365 | static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent) | |
366 | { | |
367 | USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev); | |
368 | USBBus *bus = usb_bus_from_device(dev); | |
369 | ||
c7a2196a | 370 | monitor_printf(mon, "%*saddr %d.%d, port %s, speed %s, name %s%s\n", |
66a6593a | 371 | indent, "", bus->busnr, dev->addr, |
c7a2196a | 372 | dev->port ? dev->port->path : "-", |
0fe6d12e | 373 | usb_speed(dev->speed), dev->product_desc, |
66a6593a | 374 | dev->attached ? ", attached" : ""); |
a5d2f727 GH |
375 | } |
376 | ||
c7a2196a GH |
377 | static char *usb_get_dev_path(DeviceState *qdev) |
378 | { | |
379 | USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev); | |
7267c094 | 380 | return g_strdup(dev->port->path); |
c7a2196a GH |
381 | } |
382 | ||
70d31cb2 GH |
383 | static char *usb_get_fw_dev_path(DeviceState *qdev) |
384 | { | |
385 | USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev); | |
386 | char *fw_path, *in; | |
ea87e95f | 387 | ssize_t pos = 0, fw_len; |
70d31cb2 GH |
388 | long nr; |
389 | ||
ea87e95f | 390 | fw_len = 32 + strlen(dev->port->path) * 6; |
7267c094 | 391 | fw_path = g_malloc(fw_len); |
70d31cb2 | 392 | in = dev->port->path; |
ea87e95f | 393 | while (fw_len - pos > 0) { |
70d31cb2 GH |
394 | nr = strtol(in, &in, 10); |
395 | if (in[0] == '.') { | |
396 | /* some hub between root port and device */ | |
ea87e95f | 397 | pos += snprintf(fw_path + pos, fw_len - pos, "hub@%ld/", nr); |
70d31cb2 GH |
398 | in++; |
399 | } else { | |
400 | /* the device itself */ | |
ea87e95f BS |
401 | pos += snprintf(fw_path + pos, fw_len - pos, "%s@%ld", |
402 | qdev_fw_name(qdev), nr); | |
70d31cb2 GH |
403 | break; |
404 | } | |
405 | } | |
406 | return fw_path; | |
407 | } | |
408 | ||
a5d2f727 GH |
409 | void usb_info(Monitor *mon) |
410 | { | |
411 | USBBus *bus; | |
412 | USBDevice *dev; | |
413 | USBPort *port; | |
414 | ||
72cf2d4f | 415 | if (QTAILQ_EMPTY(&busses)) { |
a5d2f727 GH |
416 | monitor_printf(mon, "USB support not enabled\n"); |
417 | return; | |
418 | } | |
419 | ||
72cf2d4f BS |
420 | QTAILQ_FOREACH(bus, &busses, next) { |
421 | QTAILQ_FOREACH(port, &bus->used, next) { | |
a5d2f727 GH |
422 | dev = port->dev; |
423 | if (!dev) | |
424 | continue; | |
c7a2196a GH |
425 | monitor_printf(mon, " Device %d.%d, Port %s, Speed %s Mb/s, Product %s\n", |
426 | bus->busnr, dev->addr, port->path, usb_speed(dev->speed), | |
0fe6d12e | 427 | dev->product_desc); |
a5d2f727 GH |
428 | } |
429 | } | |
430 | } | |
431 | ||
0958b4cc GH |
432 | /* handle legacy -usbdevice cmd line option */ |
433 | USBDevice *usbdevice_create(const char *cmdline) | |
434 | { | |
435 | USBBus *bus = usb_bus_find(-1 /* any */); | |
436 | DeviceInfo *info; | |
437 | USBDeviceInfo *usb; | |
702f3e0f JK |
438 | char driver[32]; |
439 | const char *params; | |
0958b4cc GH |
440 | int len; |
441 | ||
442 | params = strchr(cmdline,':'); | |
443 | if (params) { | |
444 | params++; | |
445 | len = params - cmdline; | |
446 | if (len > sizeof(driver)) | |
447 | len = sizeof(driver); | |
448 | pstrcpy(driver, len, cmdline); | |
449 | } else { | |
702f3e0f | 450 | params = ""; |
0958b4cc GH |
451 | pstrcpy(driver, sizeof(driver), cmdline); |
452 | } | |
453 | ||
454 | for (info = device_info_list; info != NULL; info = info->next) { | |
455 | if (info->bus_info != &usb_bus_info) | |
456 | continue; | |
457 | usb = DO_UPCAST(USBDeviceInfo, qdev, info); | |
458 | if (usb->usbdevice_name == NULL) | |
459 | continue; | |
460 | if (strcmp(usb->usbdevice_name, driver) != 0) | |
461 | continue; | |
462 | break; | |
463 | } | |
464 | if (info == NULL) { | |
465 | #if 0 | |
466 | /* no error because some drivers are not converted (yet) */ | |
1ecda02b | 467 | error_report("usbdevice %s not found", driver); |
0958b4cc GH |
468 | #endif |
469 | return NULL; | |
470 | } | |
471 | ||
472 | if (!usb->usbdevice_init) { | |
98f22dc1 | 473 | if (*params) { |
1ecda02b | 474 | error_report("usbdevice %s accepts no params", driver); |
0958b4cc GH |
475 | return NULL; |
476 | } | |
477 | return usb_create_simple(bus, usb->qdev.name); | |
478 | } | |
479 | return usb->usbdevice_init(params); | |
480 | } |