]> git.proxmox.com Git - mirror_qemu.git/blame - hw/usb-bus.c
usb: kill usb_send_msg
[mirror_qemu.git] / hw / usb-bus.c
CommitLineData
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
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
77static void usb_device_handle_destroy(USBDevice *dev)
78{
79 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
80 if (klass->handle_destroy) {
81 klass->handle_destroy(dev);
82 }
83}
84
85int usb_device_handle_packet(USBDevice *dev, USBPacket *p)
86{
87 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
88 if (klass->handle_packet) {
89 return klass->handle_packet(dev, p);
90 }
91 return -ENOSYS;
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;
206 USBDevice *(*usbdevice_init)(const char *params);
207} LegacyUSBFactory;
806b6024 208
62aed765
AL
209static GSList *legacy_usb_factory;
210
ba02430f
AL
211void usb_legacy_register(const char *typename, const char *usbdevice_name,
212 USBDevice *(*usbdevice_init)(const char *params))
806b6024 213{
62aed765
AL
214 if (usbdevice_name) {
215 LegacyUSBFactory *f = g_malloc0(sizeof(*f));
ba02430f 216 f->name = typename;
62aed765
AL
217 f->usbdevice_name = usbdevice_name;
218 f->usbdevice_init = usbdevice_init;
219 legacy_usb_factory = g_slist_append(legacy_usb_factory, f);
806b6024
GH
220 }
221}
222
a5d2f727 223USBDevice *usb_create(USBBus *bus, const char *name)
806b6024
GH
224{
225 DeviceState *dev;
226
227#if 1
228 /* temporary stopgap until all usb is properly qdev-ified */
229 if (!bus) {
230 bus = usb_bus_find(-1);
231 if (!bus)
232 return NULL;
be62a2eb 233 error_report("%s: no bus specified, using \"%s\" for \"%s\"",
806b6024
GH
234 __FUNCTION__, bus->qbus.name, name);
235 }
236#endif
237
238 dev = qdev_create(&bus->qbus, name);
62aed765 239 return USB_DEVICE(dev);
806b6024 240}
a5d2f727
GH
241
242USBDevice *usb_create_simple(USBBus *bus, const char *name)
243{
244 USBDevice *dev = usb_create(bus, name);
2af2a1b8
GH
245 int rc;
246
d44168ff 247 if (!dev) {
be62a2eb 248 error_report("Failed to create USB device '%s'", name);
2af2a1b8
GH
249 return NULL;
250 }
251 rc = qdev_init(&dev->qdev);
252 if (rc < 0) {
be62a2eb 253 error_report("Failed to initialize USB device '%s'", name);
2af2a1b8 254 return NULL;
d44168ff 255 }
a5d2f727
GH
256 return dev;
257}
258
090ac642
HG
259static void usb_fill_port(USBPort *port, void *opaque, int index,
260 USBPortOps *ops, int speedmask)
a5d2f727 261{
0d86d2be
GH
262 port->opaque = opaque;
263 port->index = index;
264 port->ops = ops;
843d4e0c 265 port->speedmask = speedmask;
3631e6c8 266 usb_port_location(port, NULL, index + 1);
090ac642
HG
267}
268
269void usb_register_port(USBBus *bus, USBPort *port, void *opaque, int index,
270 USBPortOps *ops, int speedmask)
271{
272 usb_fill_port(port, opaque, index, ops, speedmask);
72cf2d4f 273 QTAILQ_INSERT_TAIL(&bus->free, port, next);
a5d2f727
GH
274 bus->nfree++;
275}
276
ae60fea9
HG
277int usb_register_companion(const char *masterbus, USBPort *ports[],
278 uint32_t portcount, uint32_t firstport,
279 void *opaque, USBPortOps *ops, int speedmask)
280{
281 USBBus *bus;
282 int i;
283
284 QTAILQ_FOREACH(bus, &busses, next) {
285 if (strcmp(bus->qbus.name, masterbus) == 0) {
286 break;
287 }
288 }
289
290 if (!bus || !bus->ops->register_companion) {
291 qerror_report(QERR_INVALID_PARAMETER_VALUE, "masterbus",
292 "an USB masterbus");
293 if (bus) {
294 error_printf_unless_qmp(
295 "USB bus '%s' does not allow companion controllers\n",
296 masterbus);
297 }
298 return -1;
299 }
300
301 for (i = 0; i < portcount; i++) {
302 usb_fill_port(ports[i], opaque, i, ops, speedmask);
303 }
304
305 return bus->ops->register_companion(bus, ports, portcount, firstport);
306}
307
c7a2196a
GH
308void usb_port_location(USBPort *downstream, USBPort *upstream, int portnr)
309{
310 if (upstream) {
311 snprintf(downstream->path, sizeof(downstream->path), "%s.%d",
312 upstream->path, portnr);
313 } else {
314 snprintf(downstream->path, sizeof(downstream->path), "%d", portnr);
315 }
316}
317
a8e662b5
GH
318void usb_unregister_port(USBBus *bus, USBPort *port)
319{
320 if (port->dev)
321 qdev_free(&port->dev->qdev);
322 QTAILQ_REMOVE(&bus->free, port, next);
323 bus->nfree--;
324}
325
891fb2cd 326int usb_claim_port(USBDevice *dev)
a5d2f727
GH
327{
328 USBBus *bus = usb_bus_from_device(dev);
329 USBPort *port;
330
891fb2cd
GH
331 assert(dev->port == NULL);
332
5f69076b
GH
333 if (dev->port_path) {
334 QTAILQ_FOREACH(port, &bus->free, next) {
335 if (strcmp(port->path, dev->port_path) == 0) {
336 break;
337 }
338 }
339 if (port == NULL) {
be62a2eb 340 error_report("Error: usb port %s (bus %s) not found (in use?)",
891fb2cd 341 dev->port_path, bus->qbus.name);
fa19bf83 342 return -1;
5f69076b
GH
343 }
344 } else {
f79f2bfc 345 if (bus->nfree == 1 && strcmp(object_get_typename(OBJECT(dev)), "usb-hub") != 0) {
891fb2cd
GH
346 /* Create a new hub and chain it on */
347 usb_create_simple(bus, "usb-hub");
348 }
349 if (bus->nfree == 0) {
350 error_report("Error: tried to attach usb device %s to a bus "
be62a2eb 351 "with no free ports", dev->product_desc);
891fb2cd
GH
352 return -1;
353 }
5f69076b
GH
354 port = QTAILQ_FIRST(&bus->free);
355 }
891fb2cd 356 trace_usb_port_claim(bus->busnr, port->path);
a5d2f727 357
72cf2d4f 358 QTAILQ_REMOVE(&bus->free, port, next);
a5d2f727
GH
359 bus->nfree--;
360
891fb2cd
GH
361 dev->port = port;
362 port->dev = dev;
a5d2f727 363
72cf2d4f 364 QTAILQ_INSERT_TAIL(&bus->used, port, next);
a5d2f727 365 bus->nused++;
fa19bf83 366 return 0;
a5d2f727
GH
367}
368
891fb2cd 369void usb_release_port(USBDevice *dev)
a5d2f727
GH
370{
371 USBBus *bus = usb_bus_from_device(dev);
891fb2cd 372 USBPort *port = dev->port;
a5d2f727 373
891fb2cd
GH
374 assert(port != NULL);
375 trace_usb_port_release(bus->busnr, port->path);
376
377 QTAILQ_REMOVE(&bus->used, port, next);
378 bus->nused--;
379
380 dev->port = NULL;
381 port->dev = NULL;
382
383 QTAILQ_INSERT_TAIL(&bus->free, port, next);
384 bus->nfree++;
a5d2f727
GH
385}
386
891fb2cd 387int usb_device_attach(USBDevice *dev)
a8e662b5
GH
388{
389 USBBus *bus = usb_bus_from_device(dev);
891fb2cd 390 USBPort *port = dev->port;
a8e662b5 391
891fb2cd
GH
392 assert(port != NULL);
393 assert(!dev->attached);
394 trace_usb_port_attach(bus->busnr, port->path);
395
396 if (!(port->speedmask & dev->speedmask)) {
397 error_report("Warning: speed mismatch trying to attach "
be62a2eb 398 "usb device %s to bus %s",
891fb2cd 399 dev->product_desc, bus->qbus.name);
a8e662b5
GH
400 return -1;
401 }
a8e662b5 402
891fb2cd
GH
403 dev->attached++;
404 usb_attach(port);
a8e662b5 405
891fb2cd
GH
406 return 0;
407}
408
409int usb_device_detach(USBDevice *dev)
410{
411 USBBus *bus = usb_bus_from_device(dev);
412 USBPort *port = dev->port;
a8e662b5 413
891fb2cd
GH
414 assert(port != NULL);
415 assert(dev->attached);
416 trace_usb_port_detach(bus->busnr, port->path);
a8e662b5 417
891fb2cd
GH
418 usb_detach(port);
419 dev->attached--;
a8e662b5
GH
420 return 0;
421}
422
a5d2f727
GH
423int usb_device_delete_addr(int busnr, int addr)
424{
425 USBBus *bus;
426 USBPort *port;
427 USBDevice *dev;
428
429 bus = usb_bus_find(busnr);
430 if (!bus)
431 return -1;
432
72cf2d4f 433 QTAILQ_FOREACH(port, &bus->used, next) {
a5d2f727
GH
434 if (port->dev->addr == addr)
435 break;
436 }
437 if (!port)
438 return -1;
a5d2f727 439 dev = port->dev;
a5d2f727 440
a8e662b5 441 qdev_free(&dev->qdev);
a5d2f727
GH
442 return 0;
443}
444
445static const char *usb_speed(unsigned int speed)
446{
447 static const char *txt[] = {
448 [ USB_SPEED_LOW ] = "1.5",
449 [ USB_SPEED_FULL ] = "12",
450 [ USB_SPEED_HIGH ] = "480",
290d26d2 451 [ USB_SPEED_SUPER ] = "5000",
a5d2f727
GH
452 };
453 if (speed >= ARRAY_SIZE(txt))
454 return "?";
455 return txt[speed];
456}
457
458static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
459{
62aed765 460 USBDevice *dev = USB_DEVICE(qdev);
a5d2f727
GH
461 USBBus *bus = usb_bus_from_device(dev);
462
c7a2196a 463 monitor_printf(mon, "%*saddr %d.%d, port %s, speed %s, name %s%s\n",
66a6593a 464 indent, "", bus->busnr, dev->addr,
c7a2196a 465 dev->port ? dev->port->path : "-",
0fe6d12e 466 usb_speed(dev->speed), dev->product_desc,
66a6593a 467 dev->attached ? ", attached" : "");
a5d2f727
GH
468}
469
c7a2196a
GH
470static char *usb_get_dev_path(DeviceState *qdev)
471{
62aed765 472 USBDevice *dev = USB_DEVICE(qdev);
7267c094 473 return g_strdup(dev->port->path);
c7a2196a
GH
474}
475
70d31cb2
GH
476static char *usb_get_fw_dev_path(DeviceState *qdev)
477{
62aed765 478 USBDevice *dev = USB_DEVICE(qdev);
70d31cb2 479 char *fw_path, *in;
ea87e95f 480 ssize_t pos = 0, fw_len;
70d31cb2
GH
481 long nr;
482
ea87e95f 483 fw_len = 32 + strlen(dev->port->path) * 6;
7267c094 484 fw_path = g_malloc(fw_len);
70d31cb2 485 in = dev->port->path;
ea87e95f 486 while (fw_len - pos > 0) {
70d31cb2
GH
487 nr = strtol(in, &in, 10);
488 if (in[0] == '.') {
489 /* some hub between root port and device */
ea87e95f 490 pos += snprintf(fw_path + pos, fw_len - pos, "hub@%ld/", nr);
70d31cb2
GH
491 in++;
492 } else {
493 /* the device itself */
ea87e95f
BS
494 pos += snprintf(fw_path + pos, fw_len - pos, "%s@%ld",
495 qdev_fw_name(qdev), nr);
70d31cb2
GH
496 break;
497 }
498 }
499 return fw_path;
500}
501
a5d2f727
GH
502void usb_info(Monitor *mon)
503{
504 USBBus *bus;
505 USBDevice *dev;
506 USBPort *port;
507
72cf2d4f 508 if (QTAILQ_EMPTY(&busses)) {
a5d2f727
GH
509 monitor_printf(mon, "USB support not enabled\n");
510 return;
511 }
512
72cf2d4f
BS
513 QTAILQ_FOREACH(bus, &busses, next) {
514 QTAILQ_FOREACH(port, &bus->used, next) {
a5d2f727
GH
515 dev = port->dev;
516 if (!dev)
517 continue;
c7a2196a
GH
518 monitor_printf(mon, " Device %d.%d, Port %s, Speed %s Mb/s, Product %s\n",
519 bus->busnr, dev->addr, port->path, usb_speed(dev->speed),
0fe6d12e 520 dev->product_desc);
a5d2f727
GH
521 }
522 }
523}
524
0958b4cc
GH
525/* handle legacy -usbdevice cmd line option */
526USBDevice *usbdevice_create(const char *cmdline)
527{
528 USBBus *bus = usb_bus_find(-1 /* any */);
62aed765
AL
529 LegacyUSBFactory *f = NULL;
530 GSList *i;
702f3e0f
JK
531 char driver[32];
532 const char *params;
0958b4cc
GH
533 int len;
534
535 params = strchr(cmdline,':');
536 if (params) {
537 params++;
538 len = params - cmdline;
539 if (len > sizeof(driver))
540 len = sizeof(driver);
541 pstrcpy(driver, len, cmdline);
542 } else {
702f3e0f 543 params = "";
0958b4cc
GH
544 pstrcpy(driver, sizeof(driver), cmdline);
545 }
546
62aed765
AL
547 for (i = legacy_usb_factory; i; i = i->next) {
548 f = i->data;
549 if (strcmp(f->usbdevice_name, driver) == 0) {
550 break;
551 }
0958b4cc 552 }
62aed765 553 if (i == NULL) {
0958b4cc
GH
554#if 0
555 /* no error because some drivers are not converted (yet) */
1ecda02b 556 error_report("usbdevice %s not found", driver);
0958b4cc
GH
557#endif
558 return NULL;
559 }
560
62aed765 561 if (!f->usbdevice_init) {
98f22dc1 562 if (*params) {
1ecda02b 563 error_report("usbdevice %s accepts no params", driver);
0958b4cc
GH
564 return NULL;
565 }
62aed765 566 return usb_create_simple(bus, f->name);
0958b4cc 567 }
62aed765 568 return f->usbdevice_init(params);
0958b4cc 569}
62aed765 570
39bffca2
AL
571static void usb_device_class_init(ObjectClass *klass, void *data)
572{
573 DeviceClass *k = DEVICE_CLASS(klass);
574 k->bus_info = &usb_bus_info;
575 k->init = usb_qdev_init;
576 k->unplug = qdev_simple_unplug_cb;
577 k->exit = usb_qdev_exit;
578}
579
62aed765
AL
580static TypeInfo usb_device_type_info = {
581 .name = TYPE_USB_DEVICE,
582 .parent = TYPE_DEVICE,
583 .instance_size = sizeof(USBDevice),
584 .abstract = true,
585 .class_size = sizeof(USBDeviceClass),
39bffca2 586 .class_init = usb_device_class_init,
62aed765
AL
587};
588
589static void usb_register_devices(void)
590{
591 type_register_static(&usb_device_type_info);
592}
593
594device_init(usb_register_devices);