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