]> git.proxmox.com Git - mirror_qemu.git/blame - hw/usb/bus.c
qemu-timer.c: Remove 250us timeouts
[mirror_qemu.git] / hw / usb / bus.c
CommitLineData
f1ae32a1
GH
1#include "hw/hw.h"
2#include "hw/usb.h"
3#include "hw/qdev.h"
a5d2f727
GH
4#include "sysemu.h"
5#include "monitor.h"
891fb2cd 6#include "trace.h"
a5d2f727
GH
7
8static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent);
c7a2196a
GH
9
10static char *usb_get_dev_path(DeviceState *dev);
70d31cb2 11static char *usb_get_fw_dev_path(DeviceState *qdev);
f462141f 12static int usb_qdev_exit(DeviceState *qdev);
806b6024
GH
13
14static 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};
25static int next_usb_bus = 0;
72cf2d4f 26static QTAILQ_HEAD(, USBBus) busses = QTAILQ_HEAD_INITIALIZER(busses);
806b6024 27
c1ecb40a
GH
28const 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 44void 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
55USBBus *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
62aed765
AL
68static int usb_device_init(USBDevice *dev)
69{
70 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
71 if (klass->init) {
72 return klass->init(dev);
73 }
74 return 0;
75}
76
73796fe6 77USBDevice *usb_device_find_device(USBDevice *dev, uint8_t addr)
62aed765
AL
78{
79 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
73796fe6
GH
80 if (klass->find_device) {
81 return klass->find_device(dev, addr);
62aed765 82 }
73796fe6 83 return NULL;
62aed765
AL
84}
85
62aed765 86static void usb_device_handle_destroy(USBDevice *dev)
62aed765
AL
87{
88 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
62aed765
AL
89 if (klass->handle_destroy) {
90 klass->handle_destroy(dev);
62aed765 91 }
62aed765
AL
92}
93
94void usb_device_cancel_packet(USBDevice *dev, USBPacket *p)
95{
96 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
97 if (klass->cancel_packet) {
98 klass->cancel_packet(dev, p);
99 }
100}
101
102void usb_device_handle_attach(USBDevice *dev)
103{
104 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
105 if (klass->handle_attach) {
106 klass->handle_attach(dev);
107 }
108}
109
110void usb_device_handle_reset(USBDevice *dev)
111{
112 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
113 if (klass->handle_reset) {
114 klass->handle_reset(dev);
115 }
116}
117
118int usb_device_handle_control(USBDevice *dev, USBPacket *p, int request,
119 int value, int index, int length, uint8_t *data)
120{
121 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
122 if (klass->handle_control) {
123 return klass->handle_control(dev, p, request, value, index, length,
124 data);
125 }
126 return -ENOSYS;
127}
128
129int usb_device_handle_data(USBDevice *dev, USBPacket *p)
130{
131 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
132 if (klass->handle_data) {
133 return klass->handle_data(dev, p);
134 }
135 return -ENOSYS;
136}
137
138const char *usb_device_get_product_desc(USBDevice *dev)
139{
140 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
141 return klass->product_desc;
142}
143
144const USBDesc *usb_device_get_usb_desc(USBDevice *dev)
145{
146 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
147 return klass->usb_desc;
148}
149
150void usb_device_set_interface(USBDevice *dev, int interface,
151 int alt_old, int alt_new)
152{
153 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
154 if (klass->set_interface) {
155 klass->set_interface(dev, interface, alt_old, alt_new);
156 }
157}
158
d307af79 159static int usb_qdev_init(DeviceState *qdev)
806b6024 160{
62aed765 161 USBDevice *dev = USB_DEVICE(qdev);
806b6024
GH
162 int rc;
163
62aed765
AL
164 pstrcpy(dev->product_desc, sizeof(dev->product_desc),
165 usb_device_get_product_desc(dev));
61e094c0 166 dev->auto_attach = 1;
132a3f55 167 QLIST_INIT(&dev->strings);
d8e17efd 168 usb_ep_init(dev);
891fb2cd 169 rc = usb_claim_port(dev);
f462141f 170 if (rc != 0) {
db3a5ed7 171 return rc;
891fb2cd 172 }
62aed765 173 rc = usb_device_init(dev);
f462141f 174 if (rc != 0) {
db3a5ed7
SH
175 usb_release_port(dev);
176 return rc;
f462141f
GH
177 }
178 if (dev->auto_attach) {
fa19bf83 179 rc = usb_device_attach(dev);
f462141f 180 if (rc != 0) {
db3a5ed7
SH
181 usb_qdev_exit(qdev);
182 return rc;
f462141f 183 }
891fb2cd 184 }
f462141f 185 return 0;
806b6024
GH
186}
187
a8e662b5
GH
188static int usb_qdev_exit(DeviceState *qdev)
189{
62aed765 190 USBDevice *dev = USB_DEVICE(qdev);
a8e662b5 191
290a5c60
HG
192 if (dev->attached) {
193 usb_device_detach(dev);
194 }
62aed765 195 usb_device_handle_destroy(dev);
891fb2cd
GH
196 if (dev->port) {
197 usb_release_port(dev);
198 }
a8e662b5
GH
199 return 0;
200}
201
62aed765 202typedef struct LegacyUSBFactory
806b6024 203{
62aed765
AL
204 const char *name;
205 const char *usbdevice_name;
3741715c 206 USBDevice *(*usbdevice_init)(USBBus *bus, const char *params);
62aed765 207} LegacyUSBFactory;
806b6024 208
62aed765
AL
209static GSList *legacy_usb_factory;
210
ba02430f 211void usb_legacy_register(const char *typename, const char *usbdevice_name,
3741715c
JK
212 USBDevice *(*usbdevice_init)(USBBus *bus,
213 const char *params))
806b6024 214{
62aed765
AL
215 if (usbdevice_name) {
216 LegacyUSBFactory *f = g_malloc0(sizeof(*f));
ba02430f 217 f->name = typename;
62aed765
AL
218 f->usbdevice_name = usbdevice_name;
219 f->usbdevice_init = usbdevice_init;
220 legacy_usb_factory = g_slist_append(legacy_usb_factory, f);
806b6024
GH
221 }
222}
223
a5d2f727 224USBDevice *usb_create(USBBus *bus, const char *name)
806b6024
GH
225{
226 DeviceState *dev;
227
806b6024 228 dev = qdev_create(&bus->qbus, name);
62aed765 229 return USB_DEVICE(dev);
806b6024 230}
a5d2f727
GH
231
232USBDevice *usb_create_simple(USBBus *bus, const char *name)
233{
234 USBDevice *dev = usb_create(bus, name);
2af2a1b8
GH
235 int rc;
236
d44168ff 237 if (!dev) {
be62a2eb 238 error_report("Failed to create USB device '%s'", name);
2af2a1b8
GH
239 return NULL;
240 }
241 rc = qdev_init(&dev->qdev);
242 if (rc < 0) {
be62a2eb 243 error_report("Failed to initialize USB device '%s'", name);
2af2a1b8 244 return NULL;
d44168ff 245 }
a5d2f727
GH
246 return dev;
247}
248
090ac642
HG
249static void usb_fill_port(USBPort *port, void *opaque, int index,
250 USBPortOps *ops, int speedmask)
a5d2f727 251{
0d86d2be
GH
252 port->opaque = opaque;
253 port->index = index;
254 port->ops = ops;
843d4e0c 255 port->speedmask = speedmask;
3631e6c8 256 usb_port_location(port, NULL, index + 1);
090ac642
HG
257}
258
259void usb_register_port(USBBus *bus, USBPort *port, void *opaque, int index,
260 USBPortOps *ops, int speedmask)
261{
262 usb_fill_port(port, opaque, index, ops, speedmask);
72cf2d4f 263 QTAILQ_INSERT_TAIL(&bus->free, port, next);
a5d2f727
GH
264 bus->nfree++;
265}
266
ae60fea9
HG
267int usb_register_companion(const char *masterbus, USBPort *ports[],
268 uint32_t portcount, uint32_t firstport,
269 void *opaque, USBPortOps *ops, int speedmask)
270{
271 USBBus *bus;
272 int i;
273
274 QTAILQ_FOREACH(bus, &busses, next) {
275 if (strcmp(bus->qbus.name, masterbus) == 0) {
276 break;
277 }
278 }
279
280 if (!bus || !bus->ops->register_companion) {
281 qerror_report(QERR_INVALID_PARAMETER_VALUE, "masterbus",
282 "an USB masterbus");
283 if (bus) {
284 error_printf_unless_qmp(
285 "USB bus '%s' does not allow companion controllers\n",
286 masterbus);
287 }
288 return -1;
289 }
290
291 for (i = 0; i < portcount; i++) {
292 usb_fill_port(ports[i], opaque, i, ops, speedmask);
293 }
294
295 return bus->ops->register_companion(bus, ports, portcount, firstport);
296}
297
c7a2196a
GH
298void usb_port_location(USBPort *downstream, USBPort *upstream, int portnr)
299{
300 if (upstream) {
301 snprintf(downstream->path, sizeof(downstream->path), "%s.%d",
302 upstream->path, portnr);
303 } else {
304 snprintf(downstream->path, sizeof(downstream->path), "%d", portnr);
305 }
306}
307
a8e662b5
GH
308void usb_unregister_port(USBBus *bus, USBPort *port)
309{
310 if (port->dev)
311 qdev_free(&port->dev->qdev);
312 QTAILQ_REMOVE(&bus->free, port, next);
313 bus->nfree--;
314}
315
891fb2cd 316int usb_claim_port(USBDevice *dev)
a5d2f727
GH
317{
318 USBBus *bus = usb_bus_from_device(dev);
319 USBPort *port;
320
891fb2cd
GH
321 assert(dev->port == NULL);
322
5f69076b
GH
323 if (dev->port_path) {
324 QTAILQ_FOREACH(port, &bus->free, next) {
325 if (strcmp(port->path, dev->port_path) == 0) {
326 break;
327 }
328 }
329 if (port == NULL) {
be62a2eb 330 error_report("Error: usb port %s (bus %s) not found (in use?)",
891fb2cd 331 dev->port_path, bus->qbus.name);
fa19bf83 332 return -1;
5f69076b
GH
333 }
334 } else {
f79f2bfc 335 if (bus->nfree == 1 && strcmp(object_get_typename(OBJECT(dev)), "usb-hub") != 0) {
891fb2cd
GH
336 /* Create a new hub and chain it on */
337 usb_create_simple(bus, "usb-hub");
338 }
339 if (bus->nfree == 0) {
340 error_report("Error: tried to attach usb device %s to a bus "
be62a2eb 341 "with no free ports", dev->product_desc);
891fb2cd
GH
342 return -1;
343 }
5f69076b
GH
344 port = QTAILQ_FIRST(&bus->free);
345 }
891fb2cd 346 trace_usb_port_claim(bus->busnr, port->path);
a5d2f727 347
72cf2d4f 348 QTAILQ_REMOVE(&bus->free, port, next);
a5d2f727
GH
349 bus->nfree--;
350
891fb2cd
GH
351 dev->port = port;
352 port->dev = dev;
a5d2f727 353
72cf2d4f 354 QTAILQ_INSERT_TAIL(&bus->used, port, next);
a5d2f727 355 bus->nused++;
fa19bf83 356 return 0;
a5d2f727
GH
357}
358
891fb2cd 359void usb_release_port(USBDevice *dev)
a5d2f727
GH
360{
361 USBBus *bus = usb_bus_from_device(dev);
891fb2cd 362 USBPort *port = dev->port;
a5d2f727 363
891fb2cd
GH
364 assert(port != NULL);
365 trace_usb_port_release(bus->busnr, port->path);
366
367 QTAILQ_REMOVE(&bus->used, port, next);
368 bus->nused--;
369
370 dev->port = NULL;
371 port->dev = NULL;
372
373 QTAILQ_INSERT_TAIL(&bus->free, port, next);
374 bus->nfree++;
a5d2f727
GH
375}
376
891fb2cd 377int usb_device_attach(USBDevice *dev)
a8e662b5
GH
378{
379 USBBus *bus = usb_bus_from_device(dev);
891fb2cd 380 USBPort *port = dev->port;
a8e662b5 381
891fb2cd
GH
382 assert(port != NULL);
383 assert(!dev->attached);
384 trace_usb_port_attach(bus->busnr, port->path);
385
386 if (!(port->speedmask & dev->speedmask)) {
387 error_report("Warning: speed mismatch trying to attach "
be62a2eb 388 "usb device %s to bus %s",
891fb2cd 389 dev->product_desc, bus->qbus.name);
a8e662b5
GH
390 return -1;
391 }
a8e662b5 392
891fb2cd
GH
393 dev->attached++;
394 usb_attach(port);
a8e662b5 395
891fb2cd
GH
396 return 0;
397}
398
399int usb_device_detach(USBDevice *dev)
400{
401 USBBus *bus = usb_bus_from_device(dev);
402 USBPort *port = dev->port;
a8e662b5 403
891fb2cd
GH
404 assert(port != NULL);
405 assert(dev->attached);
406 trace_usb_port_detach(bus->busnr, port->path);
a8e662b5 407
891fb2cd
GH
408 usb_detach(port);
409 dev->attached--;
a8e662b5
GH
410 return 0;
411}
412
a5d2f727
GH
413int usb_device_delete_addr(int busnr, int addr)
414{
415 USBBus *bus;
416 USBPort *port;
417 USBDevice *dev;
418
419 bus = usb_bus_find(busnr);
420 if (!bus)
421 return -1;
422
72cf2d4f 423 QTAILQ_FOREACH(port, &bus->used, next) {
a5d2f727
GH
424 if (port->dev->addr == addr)
425 break;
426 }
427 if (!port)
428 return -1;
a5d2f727 429 dev = port->dev;
a5d2f727 430
a8e662b5 431 qdev_free(&dev->qdev);
a5d2f727
GH
432 return 0;
433}
434
435static const char *usb_speed(unsigned int speed)
436{
437 static const char *txt[] = {
438 [ USB_SPEED_LOW ] = "1.5",
439 [ USB_SPEED_FULL ] = "12",
440 [ USB_SPEED_HIGH ] = "480",
290d26d2 441 [ USB_SPEED_SUPER ] = "5000",
a5d2f727
GH
442 };
443 if (speed >= ARRAY_SIZE(txt))
444 return "?";
445 return txt[speed];
446}
447
448static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
449{
62aed765 450 USBDevice *dev = USB_DEVICE(qdev);
a5d2f727
GH
451 USBBus *bus = usb_bus_from_device(dev);
452
c7a2196a 453 monitor_printf(mon, "%*saddr %d.%d, port %s, speed %s, name %s%s\n",
66a6593a 454 indent, "", bus->busnr, dev->addr,
c7a2196a 455 dev->port ? dev->port->path : "-",
0fe6d12e 456 usb_speed(dev->speed), dev->product_desc,
66a6593a 457 dev->attached ? ", attached" : "");
a5d2f727
GH
458}
459
c7a2196a
GH
460static char *usb_get_dev_path(DeviceState *qdev)
461{
62aed765 462 USBDevice *dev = USB_DEVICE(qdev);
7267c094 463 return g_strdup(dev->port->path);
c7a2196a
GH
464}
465
70d31cb2
GH
466static char *usb_get_fw_dev_path(DeviceState *qdev)
467{
62aed765 468 USBDevice *dev = USB_DEVICE(qdev);
70d31cb2 469 char *fw_path, *in;
ea87e95f 470 ssize_t pos = 0, fw_len;
70d31cb2
GH
471 long nr;
472
ea87e95f 473 fw_len = 32 + strlen(dev->port->path) * 6;
7267c094 474 fw_path = g_malloc(fw_len);
70d31cb2 475 in = dev->port->path;
ea87e95f 476 while (fw_len - pos > 0) {
70d31cb2
GH
477 nr = strtol(in, &in, 10);
478 if (in[0] == '.') {
479 /* some hub between root port and device */
ea87e95f 480 pos += snprintf(fw_path + pos, fw_len - pos, "hub@%ld/", nr);
70d31cb2
GH
481 in++;
482 } else {
483 /* the device itself */
ea87e95f
BS
484 pos += snprintf(fw_path + pos, fw_len - pos, "%s@%ld",
485 qdev_fw_name(qdev), nr);
70d31cb2
GH
486 break;
487 }
488 }
489 return fw_path;
490}
491
a5d2f727
GH
492void usb_info(Monitor *mon)
493{
494 USBBus *bus;
495 USBDevice *dev;
496 USBPort *port;
497
72cf2d4f 498 if (QTAILQ_EMPTY(&busses)) {
a5d2f727
GH
499 monitor_printf(mon, "USB support not enabled\n");
500 return;
501 }
502
72cf2d4f
BS
503 QTAILQ_FOREACH(bus, &busses, next) {
504 QTAILQ_FOREACH(port, &bus->used, next) {
a5d2f727
GH
505 dev = port->dev;
506 if (!dev)
507 continue;
c7a2196a
GH
508 monitor_printf(mon, " Device %d.%d, Port %s, Speed %s Mb/s, Product %s\n",
509 bus->busnr, dev->addr, port->path, usb_speed(dev->speed),
0fe6d12e 510 dev->product_desc);
a5d2f727
GH
511 }
512 }
513}
514
0958b4cc
GH
515/* handle legacy -usbdevice cmd line option */
516USBDevice *usbdevice_create(const char *cmdline)
517{
518 USBBus *bus = usb_bus_find(-1 /* any */);
62aed765
AL
519 LegacyUSBFactory *f = NULL;
520 GSList *i;
702f3e0f
JK
521 char driver[32];
522 const char *params;
0958b4cc
GH
523 int len;
524
525 params = strchr(cmdline,':');
526 if (params) {
527 params++;
528 len = params - cmdline;
529 if (len > sizeof(driver))
530 len = sizeof(driver);
531 pstrcpy(driver, len, cmdline);
532 } else {
702f3e0f 533 params = "";
0958b4cc
GH
534 pstrcpy(driver, sizeof(driver), cmdline);
535 }
536
62aed765
AL
537 for (i = legacy_usb_factory; i; i = i->next) {
538 f = i->data;
539 if (strcmp(f->usbdevice_name, driver) == 0) {
540 break;
541 }
0958b4cc 542 }
62aed765 543 if (i == NULL) {
0958b4cc
GH
544#if 0
545 /* no error because some drivers are not converted (yet) */
1ecda02b 546 error_report("usbdevice %s not found", driver);
0958b4cc
GH
547#endif
548 return NULL;
549 }
550
62aed765 551 if (!f->usbdevice_init) {
98f22dc1 552 if (*params) {
1ecda02b 553 error_report("usbdevice %s accepts no params", driver);
0958b4cc
GH
554 return NULL;
555 }
62aed765 556 return usb_create_simple(bus, f->name);
0958b4cc 557 }
3741715c 558 return f->usbdevice_init(bus, params);
0958b4cc 559}
62aed765 560
39bffca2
AL
561static void usb_device_class_init(ObjectClass *klass, void *data)
562{
563 DeviceClass *k = DEVICE_CLASS(klass);
564 k->bus_info = &usb_bus_info;
565 k->init = usb_qdev_init;
566 k->unplug = qdev_simple_unplug_cb;
567 k->exit = usb_qdev_exit;
568}
569
62aed765
AL
570static TypeInfo usb_device_type_info = {
571 .name = TYPE_USB_DEVICE,
572 .parent = TYPE_DEVICE,
573 .instance_size = sizeof(USBDevice),
574 .abstract = true,
575 .class_size = sizeof(USBDeviceClass),
39bffca2 576 .class_init = usb_device_class_init,
62aed765
AL
577};
578
83f7d43a 579static void usb_register_types(void)
62aed765
AL
580{
581 type_register_static(&usb_device_type_info);
582}
583
83f7d43a 584type_init(usb_register_types)