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