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