]> git.proxmox.com Git - mirror_qemu.git/blob - hw/usb/bus.c
Merge remote-tracking branch 'remotes/lalrae/tags/mips-20141015' into staging
[mirror_qemu.git] / hw / usb / bus.c
1 #include "hw/hw.h"
2 #include "hw/usb.h"
3 #include "hw/qdev.h"
4 #include "sysemu/sysemu.h"
5 #include "monitor/monitor.h"
6 #include "trace.h"
7
8 static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent);
9
10 static char *usb_get_dev_path(DeviceState *dev);
11 static char *usb_get_fw_dev_path(DeviceState *qdev);
12 static void usb_qdev_unrealize(DeviceState *qdev, Error **errp);
13
14 static Property usb_props[] = {
15 DEFINE_PROP_STRING("port", USBDevice, port_path),
16 DEFINE_PROP_STRING("serial", USBDevice, serial),
17 DEFINE_PROP_BIT("full-path", USBDevice, flags,
18 USB_DEV_FLAG_FULL_PATH, true),
19 DEFINE_PROP_BIT("msos-desc", USBDevice, flags,
20 USB_DEV_FLAG_MSOS_DESC_ENABLE, true),
21 DEFINE_PROP_END_OF_LIST()
22 };
23
24 static void usb_bus_class_init(ObjectClass *klass, void *data)
25 {
26 BusClass *k = BUS_CLASS(klass);
27 HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
28
29 k->print_dev = usb_bus_dev_print;
30 k->get_dev_path = usb_get_dev_path;
31 k->get_fw_dev_path = usb_get_fw_dev_path;
32 hc->unplug = qdev_simple_device_unplug_cb;
33 }
34
35 static const TypeInfo usb_bus_info = {
36 .name = TYPE_USB_BUS,
37 .parent = TYPE_BUS,
38 .instance_size = sizeof(USBBus),
39 .class_init = usb_bus_class_init,
40 .interfaces = (InterfaceInfo[]) {
41 { TYPE_HOTPLUG_HANDLER },
42 { }
43 }
44 };
45
46 static int next_usb_bus = 0;
47 static QTAILQ_HEAD(, USBBus) busses = QTAILQ_HEAD_INITIALIZER(busses);
48
49 static int usb_device_post_load(void *opaque, int version_id)
50 {
51 USBDevice *dev = opaque;
52
53 if (dev->state == USB_STATE_NOTATTACHED) {
54 dev->attached = 0;
55 } else {
56 dev->attached = 1;
57 }
58 if (dev->setup_index < 0 ||
59 dev->setup_len < 0 ||
60 dev->setup_index > dev->setup_len ||
61 dev->setup_len > sizeof(dev->data_buf)) {
62 return -EINVAL;
63 }
64 return 0;
65 }
66
67 const VMStateDescription vmstate_usb_device = {
68 .name = "USBDevice",
69 .version_id = 1,
70 .minimum_version_id = 1,
71 .post_load = usb_device_post_load,
72 .fields = (VMStateField[]) {
73 VMSTATE_UINT8(addr, USBDevice),
74 VMSTATE_INT32(state, USBDevice),
75 VMSTATE_INT32(remote_wakeup, USBDevice),
76 VMSTATE_INT32(setup_state, USBDevice),
77 VMSTATE_INT32(setup_len, USBDevice),
78 VMSTATE_INT32(setup_index, USBDevice),
79 VMSTATE_UINT8_ARRAY(setup_buf, USBDevice, 8),
80 VMSTATE_END_OF_LIST(),
81 }
82 };
83
84 void usb_bus_new(USBBus *bus, size_t bus_size,
85 USBBusOps *ops, DeviceState *host)
86 {
87 qbus_create_inplace(bus, bus_size, TYPE_USB_BUS, host, NULL);
88 qbus_set_bus_hotplug_handler(BUS(bus), &error_abort);
89 bus->ops = ops;
90 bus->busnr = next_usb_bus++;
91 QTAILQ_INIT(&bus->free);
92 QTAILQ_INIT(&bus->used);
93 QTAILQ_INSERT_TAIL(&busses, bus, next);
94 }
95
96 void usb_bus_release(USBBus *bus)
97 {
98 assert(next_usb_bus > 0);
99
100 QTAILQ_REMOVE(&busses, bus, next);
101 }
102
103 USBBus *usb_bus_find(int busnr)
104 {
105 USBBus *bus;
106
107 if (-1 == busnr)
108 return QTAILQ_FIRST(&busses);
109 QTAILQ_FOREACH(bus, &busses, next) {
110 if (bus->busnr == busnr)
111 return bus;
112 }
113 return NULL;
114 }
115
116 static void usb_device_realize(USBDevice *dev, Error **errp)
117 {
118 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
119
120 if (klass->realize) {
121 klass->realize(dev, errp);
122 }
123 }
124
125 USBDevice *usb_device_find_device(USBDevice *dev, uint8_t addr)
126 {
127 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
128 if (klass->find_device) {
129 return klass->find_device(dev, addr);
130 }
131 return NULL;
132 }
133
134 static void usb_device_handle_destroy(USBDevice *dev)
135 {
136 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
137 if (klass->handle_destroy) {
138 klass->handle_destroy(dev);
139 }
140 }
141
142 void usb_device_cancel_packet(USBDevice *dev, USBPacket *p)
143 {
144 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
145 if (klass->cancel_packet) {
146 klass->cancel_packet(dev, p);
147 }
148 }
149
150 void usb_device_handle_attach(USBDevice *dev)
151 {
152 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
153 if (klass->handle_attach) {
154 klass->handle_attach(dev);
155 }
156 }
157
158 void usb_device_handle_reset(USBDevice *dev)
159 {
160 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
161 if (klass->handle_reset) {
162 klass->handle_reset(dev);
163 }
164 }
165
166 void usb_device_handle_control(USBDevice *dev, USBPacket *p, int request,
167 int value, int index, int length, uint8_t *data)
168 {
169 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
170 if (klass->handle_control) {
171 klass->handle_control(dev, p, request, value, index, length, data);
172 }
173 }
174
175 void usb_device_handle_data(USBDevice *dev, USBPacket *p)
176 {
177 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
178 if (klass->handle_data) {
179 klass->handle_data(dev, p);
180 }
181 }
182
183 const char *usb_device_get_product_desc(USBDevice *dev)
184 {
185 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
186 return klass->product_desc;
187 }
188
189 const USBDesc *usb_device_get_usb_desc(USBDevice *dev)
190 {
191 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
192 if (dev->usb_desc) {
193 return dev->usb_desc;
194 }
195 return klass->usb_desc;
196 }
197
198 void usb_device_set_interface(USBDevice *dev, int interface,
199 int alt_old, int alt_new)
200 {
201 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
202 if (klass->set_interface) {
203 klass->set_interface(dev, interface, alt_old, alt_new);
204 }
205 }
206
207 void usb_device_flush_ep_queue(USBDevice *dev, USBEndpoint *ep)
208 {
209 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
210 if (klass->flush_ep_queue) {
211 klass->flush_ep_queue(dev, ep);
212 }
213 }
214
215 void usb_device_ep_stopped(USBDevice *dev, USBEndpoint *ep)
216 {
217 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
218 if (klass->ep_stopped) {
219 klass->ep_stopped(dev, ep);
220 }
221 }
222
223 int usb_device_alloc_streams(USBDevice *dev, USBEndpoint **eps, int nr_eps,
224 int streams)
225 {
226 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
227 if (klass->alloc_streams) {
228 return klass->alloc_streams(dev, eps, nr_eps, streams);
229 }
230 return 0;
231 }
232
233 void usb_device_free_streams(USBDevice *dev, USBEndpoint **eps, int nr_eps)
234 {
235 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
236 if (klass->free_streams) {
237 klass->free_streams(dev, eps, nr_eps);
238 }
239 }
240
241 static void usb_qdev_realize(DeviceState *qdev, Error **errp)
242 {
243 USBDevice *dev = USB_DEVICE(qdev);
244 Error *local_err = NULL;
245
246 pstrcpy(dev->product_desc, sizeof(dev->product_desc),
247 usb_device_get_product_desc(dev));
248 dev->auto_attach = 1;
249 QLIST_INIT(&dev->strings);
250 usb_ep_init(dev);
251
252 usb_claim_port(dev, &local_err);
253 if (local_err) {
254 error_propagate(errp, local_err);
255 return;
256 }
257
258 usb_device_realize(dev, &local_err);
259 if (local_err) {
260 usb_release_port(dev);
261 error_propagate(errp, local_err);
262 return;
263 }
264
265 if (dev->auto_attach) {
266 usb_device_attach(dev, &local_err);
267 if (local_err) {
268 usb_qdev_unrealize(qdev, NULL);
269 error_propagate(errp, local_err);
270 return;
271 }
272 }
273 }
274
275 static void usb_qdev_unrealize(DeviceState *qdev, Error **errp)
276 {
277 USBDevice *dev = USB_DEVICE(qdev);
278
279 if (dev->attached) {
280 usb_device_detach(dev);
281 }
282 usb_device_handle_destroy(dev);
283 if (dev->port) {
284 usb_release_port(dev);
285 }
286 }
287
288 typedef struct LegacyUSBFactory
289 {
290 const char *name;
291 const char *usbdevice_name;
292 USBDevice *(*usbdevice_init)(USBBus *bus, const char *params);
293 } LegacyUSBFactory;
294
295 static GSList *legacy_usb_factory;
296
297 void usb_legacy_register(const char *typename, const char *usbdevice_name,
298 USBDevice *(*usbdevice_init)(USBBus *bus,
299 const char *params))
300 {
301 if (usbdevice_name) {
302 LegacyUSBFactory *f = g_malloc0(sizeof(*f));
303 f->name = typename;
304 f->usbdevice_name = usbdevice_name;
305 f->usbdevice_init = usbdevice_init;
306 legacy_usb_factory = g_slist_append(legacy_usb_factory, f);
307 }
308 }
309
310 USBDevice *usb_create(USBBus *bus, const char *name)
311 {
312 DeviceState *dev;
313
314 dev = qdev_create(&bus->qbus, name);
315 return USB_DEVICE(dev);
316 }
317
318 USBDevice *usb_create_simple(USBBus *bus, const char *name)
319 {
320 USBDevice *dev = usb_create(bus, name);
321 int rc;
322
323 if (!dev) {
324 error_report("Failed to create USB device '%s'", name);
325 return NULL;
326 }
327 rc = qdev_init(&dev->qdev);
328 if (rc < 0) {
329 error_report("Failed to initialize USB device '%s'", name);
330 return NULL;
331 }
332 return dev;
333 }
334
335 static void usb_fill_port(USBPort *port, void *opaque, int index,
336 USBPortOps *ops, int speedmask)
337 {
338 port->opaque = opaque;
339 port->index = index;
340 port->ops = ops;
341 port->speedmask = speedmask;
342 usb_port_location(port, NULL, index + 1);
343 }
344
345 void usb_register_port(USBBus *bus, USBPort *port, void *opaque, int index,
346 USBPortOps *ops, int speedmask)
347 {
348 usb_fill_port(port, opaque, index, ops, speedmask);
349 QTAILQ_INSERT_TAIL(&bus->free, port, next);
350 bus->nfree++;
351 }
352
353 int usb_register_companion(const char *masterbus, USBPort *ports[],
354 uint32_t portcount, uint32_t firstport,
355 void *opaque, USBPortOps *ops, int speedmask)
356 {
357 USBBus *bus;
358 int i;
359
360 QTAILQ_FOREACH(bus, &busses, next) {
361 if (strcmp(bus->qbus.name, masterbus) == 0) {
362 break;
363 }
364 }
365
366 if (!bus || !bus->ops->register_companion) {
367 qerror_report(QERR_INVALID_PARAMETER_VALUE, "masterbus",
368 "an USB masterbus");
369 if (bus) {
370 error_printf_unless_qmp(
371 "USB bus '%s' does not allow companion controllers\n",
372 masterbus);
373 }
374 return -1;
375 }
376
377 for (i = 0; i < portcount; i++) {
378 usb_fill_port(ports[i], opaque, i, ops, speedmask);
379 }
380
381 return bus->ops->register_companion(bus, ports, portcount, firstport);
382 }
383
384 void usb_port_location(USBPort *downstream, USBPort *upstream, int portnr)
385 {
386 if (upstream) {
387 snprintf(downstream->path, sizeof(downstream->path), "%s.%d",
388 upstream->path, portnr);
389 downstream->hubcount = upstream->hubcount + 1;
390 } else {
391 snprintf(downstream->path, sizeof(downstream->path), "%d", portnr);
392 downstream->hubcount = 0;
393 }
394 }
395
396 void usb_unregister_port(USBBus *bus, USBPort *port)
397 {
398 if (port->dev) {
399 object_unparent(OBJECT(port->dev));
400 }
401 QTAILQ_REMOVE(&bus->free, port, next);
402 bus->nfree--;
403 }
404
405 void usb_claim_port(USBDevice *dev, Error **errp)
406 {
407 USBBus *bus = usb_bus_from_device(dev);
408 USBPort *port;
409
410 assert(dev->port == NULL);
411
412 if (dev->port_path) {
413 QTAILQ_FOREACH(port, &bus->free, next) {
414 if (strcmp(port->path, dev->port_path) == 0) {
415 break;
416 }
417 }
418 if (port == NULL) {
419 error_setg(errp, "Error: usb port %s (bus %s) not found (in use?)",
420 dev->port_path, bus->qbus.name);
421 return;
422 }
423 } else {
424 if (bus->nfree == 1 && strcmp(object_get_typename(OBJECT(dev)), "usb-hub") != 0) {
425 /* Create a new hub and chain it on */
426 usb_create_simple(bus, "usb-hub");
427 }
428 if (bus->nfree == 0) {
429 error_setg(errp, "Error: tried to attach usb device %s to a bus "
430 "with no free ports", dev->product_desc);
431 return;
432 }
433 port = QTAILQ_FIRST(&bus->free);
434 }
435 trace_usb_port_claim(bus->busnr, port->path);
436
437 QTAILQ_REMOVE(&bus->free, port, next);
438 bus->nfree--;
439
440 dev->port = port;
441 port->dev = dev;
442
443 QTAILQ_INSERT_TAIL(&bus->used, port, next);
444 bus->nused++;
445 }
446
447 void usb_release_port(USBDevice *dev)
448 {
449 USBBus *bus = usb_bus_from_device(dev);
450 USBPort *port = dev->port;
451
452 assert(port != NULL);
453 trace_usb_port_release(bus->busnr, port->path);
454
455 QTAILQ_REMOVE(&bus->used, port, next);
456 bus->nused--;
457
458 dev->port = NULL;
459 port->dev = NULL;
460
461 QTAILQ_INSERT_TAIL(&bus->free, port, next);
462 bus->nfree++;
463 }
464
465 static void usb_mask_to_str(char *dest, size_t size,
466 unsigned int speedmask)
467 {
468 static const struct {
469 unsigned int mask;
470 const char *name;
471 } speeds[] = {
472 { .mask = USB_SPEED_MASK_FULL, .name = "full" },
473 { .mask = USB_SPEED_MASK_HIGH, .name = "high" },
474 { .mask = USB_SPEED_MASK_SUPER, .name = "super" },
475 };
476 int i, pos = 0;
477
478 for (i = 0; i < ARRAY_SIZE(speeds); i++) {
479 if (speeds[i].mask & speedmask) {
480 pos += snprintf(dest + pos, size - pos, "%s%s",
481 pos ? "+" : "",
482 speeds[i].name);
483 }
484 }
485 }
486
487 void usb_check_attach(USBDevice *dev, Error **errp)
488 {
489 USBBus *bus = usb_bus_from_device(dev);
490 USBPort *port = dev->port;
491 char devspeed[32], portspeed[32];
492
493 assert(port != NULL);
494 assert(!dev->attached);
495 usb_mask_to_str(devspeed, sizeof(devspeed), dev->speedmask);
496 usb_mask_to_str(portspeed, sizeof(portspeed), port->speedmask);
497 trace_usb_port_attach(bus->busnr, port->path,
498 devspeed, portspeed);
499
500 if (!(port->speedmask & dev->speedmask)) {
501 error_setg(errp, "Warning: speed mismatch trying to attach"
502 " usb device \"%s\" (%s speed)"
503 " to bus \"%s\", port \"%s\" (%s speed)",
504 dev->product_desc, devspeed,
505 bus->qbus.name, port->path, portspeed);
506 return;
507 }
508 }
509
510 void usb_device_attach(USBDevice *dev, Error **errp)
511 {
512 USBPort *port = dev->port;
513 Error *local_err = NULL;
514
515 usb_check_attach(dev, &local_err);
516 if (local_err) {
517 error_propagate(errp, local_err);
518 return;
519 }
520
521 dev->attached++;
522 usb_attach(port);
523 }
524
525 int usb_device_detach(USBDevice *dev)
526 {
527 USBBus *bus = usb_bus_from_device(dev);
528 USBPort *port = dev->port;
529
530 assert(port != NULL);
531 assert(dev->attached);
532 trace_usb_port_detach(bus->busnr, port->path);
533
534 usb_detach(port);
535 dev->attached--;
536 return 0;
537 }
538
539 int usb_device_delete_addr(int busnr, int addr)
540 {
541 USBBus *bus;
542 USBPort *port;
543 USBDevice *dev;
544
545 bus = usb_bus_find(busnr);
546 if (!bus)
547 return -1;
548
549 QTAILQ_FOREACH(port, &bus->used, next) {
550 if (port->dev->addr == addr)
551 break;
552 }
553 if (!port)
554 return -1;
555 dev = port->dev;
556
557 object_unparent(OBJECT(dev));
558 return 0;
559 }
560
561 static const char *usb_speed(unsigned int speed)
562 {
563 static const char *txt[] = {
564 [ USB_SPEED_LOW ] = "1.5",
565 [ USB_SPEED_FULL ] = "12",
566 [ USB_SPEED_HIGH ] = "480",
567 [ USB_SPEED_SUPER ] = "5000",
568 };
569 if (speed >= ARRAY_SIZE(txt))
570 return "?";
571 return txt[speed];
572 }
573
574 static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
575 {
576 USBDevice *dev = USB_DEVICE(qdev);
577 USBBus *bus = usb_bus_from_device(dev);
578
579 monitor_printf(mon, "%*saddr %d.%d, port %s, speed %s, name %s%s\n",
580 indent, "", bus->busnr, dev->addr,
581 dev->port ? dev->port->path : "-",
582 usb_speed(dev->speed), dev->product_desc,
583 dev->attached ? ", attached" : "");
584 }
585
586 static char *usb_get_dev_path(DeviceState *qdev)
587 {
588 USBDevice *dev = USB_DEVICE(qdev);
589 DeviceState *hcd = qdev->parent_bus->parent;
590 char *id = NULL;
591
592 if (dev->flags & (1 << USB_DEV_FLAG_FULL_PATH)) {
593 id = qdev_get_dev_path(hcd);
594 }
595 if (id) {
596 char *ret = g_strdup_printf("%s/%s", id, dev->port->path);
597 g_free(id);
598 return ret;
599 } else {
600 return g_strdup(dev->port->path);
601 }
602 }
603
604 static char *usb_get_fw_dev_path(DeviceState *qdev)
605 {
606 USBDevice *dev = USB_DEVICE(qdev);
607 char *fw_path, *in;
608 ssize_t pos = 0, fw_len;
609 long nr;
610
611 fw_len = 32 + strlen(dev->port->path) * 6;
612 fw_path = g_malloc(fw_len);
613 in = dev->port->path;
614 while (fw_len - pos > 0) {
615 nr = strtol(in, &in, 10);
616 if (in[0] == '.') {
617 /* some hub between root port and device */
618 pos += snprintf(fw_path + pos, fw_len - pos, "hub@%lx/", nr);
619 in++;
620 } else {
621 /* the device itself */
622 pos += snprintf(fw_path + pos, fw_len - pos, "%s@%lx",
623 qdev_fw_name(qdev), nr);
624 break;
625 }
626 }
627 return fw_path;
628 }
629
630 void usb_info(Monitor *mon, const QDict *qdict)
631 {
632 USBBus *bus;
633 USBDevice *dev;
634 USBPort *port;
635
636 if (QTAILQ_EMPTY(&busses)) {
637 monitor_printf(mon, "USB support not enabled\n");
638 return;
639 }
640
641 QTAILQ_FOREACH(bus, &busses, next) {
642 QTAILQ_FOREACH(port, &bus->used, next) {
643 dev = port->dev;
644 if (!dev)
645 continue;
646 monitor_printf(mon, " Device %d.%d, Port %s, Speed %s Mb/s, Product %s\n",
647 bus->busnr, dev->addr, port->path, usb_speed(dev->speed),
648 dev->product_desc);
649 }
650 }
651 }
652
653 /* handle legacy -usbdevice cmd line option */
654 USBDevice *usbdevice_create(const char *cmdline)
655 {
656 USBBus *bus = usb_bus_find(-1 /* any */);
657 LegacyUSBFactory *f = NULL;
658 GSList *i;
659 char driver[32];
660 const char *params;
661 int len;
662
663 params = strchr(cmdline,':');
664 if (params) {
665 params++;
666 len = params - cmdline;
667 if (len > sizeof(driver))
668 len = sizeof(driver);
669 pstrcpy(driver, len, cmdline);
670 } else {
671 params = "";
672 pstrcpy(driver, sizeof(driver), cmdline);
673 }
674
675 for (i = legacy_usb_factory; i; i = i->next) {
676 f = i->data;
677 if (strcmp(f->usbdevice_name, driver) == 0) {
678 break;
679 }
680 }
681 if (i == NULL) {
682 #if 0
683 /* no error because some drivers are not converted (yet) */
684 error_report("usbdevice %s not found", driver);
685 #endif
686 return NULL;
687 }
688
689 if (!bus) {
690 error_report("Error: no usb bus to attach usbdevice %s, "
691 "please try -machine usb=on and check that "
692 "the machine model supports USB", driver);
693 return NULL;
694 }
695
696 if (!f->usbdevice_init) {
697 if (*params) {
698 error_report("usbdevice %s accepts no params", driver);
699 return NULL;
700 }
701 return usb_create_simple(bus, f->name);
702 }
703 return f->usbdevice_init(bus, params);
704 }
705
706 static void usb_device_class_init(ObjectClass *klass, void *data)
707 {
708 DeviceClass *k = DEVICE_CLASS(klass);
709 k->bus_type = TYPE_USB_BUS;
710 k->realize = usb_qdev_realize;
711 k->unrealize = usb_qdev_unrealize;
712 k->props = usb_props;
713 }
714
715 static const TypeInfo usb_device_type_info = {
716 .name = TYPE_USB_DEVICE,
717 .parent = TYPE_DEVICE,
718 .instance_size = sizeof(USBDevice),
719 .abstract = true,
720 .class_size = sizeof(USBDeviceClass),
721 .class_init = usb_device_class_init,
722 };
723
724 static void usb_register_types(void)
725 {
726 type_register_static(&usb_bus_info);
727 type_register_static(&usb_device_type_info);
728 }
729
730 type_init(usb_register_types)