qemu-ga: move channel/transport functionality into wrapper class
[qemu.git] / hw / usb-bus.c
blobae79a4527b0a0e3c2a12127b12371ab237473976
1 #include "hw.h"
2 #include "usb.h"
3 #include "qdev.h"
4 #include "sysemu.h"
5 #include "monitor.h"
6 #include "trace.h"
8 static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent);
10 static char *usb_get_dev_path(DeviceState *dev);
11 static char *usb_get_fw_dev_path(DeviceState *qdev);
12 static int usb_qdev_exit(DeviceState *qdev);
14 static struct BusInfo usb_bus_info = {
15 .name = "USB",
16 .size = sizeof(USBBus),
17 .print_dev = usb_bus_dev_print,
18 .get_dev_path = usb_get_dev_path,
19 .get_fw_dev_path = usb_get_fw_dev_path,
20 .props = (Property[]) {
21 DEFINE_PROP_STRING("port", USBDevice, port_path),
22 DEFINE_PROP_END_OF_LIST()
25 static int next_usb_bus = 0;
26 static QTAILQ_HEAD(, USBBus) busses = QTAILQ_HEAD_INITIALIZER(busses);
28 const 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(),
44 void usb_bus_new(USBBus *bus, USBBusOps *ops, DeviceState *host)
46 qbus_create_inplace(&bus->qbus, &usb_bus_info, host, NULL);
47 bus->ops = ops;
48 bus->busnr = next_usb_bus++;
49 bus->qbus.allow_hotplug = 1; /* Yes, we can */
50 QTAILQ_INIT(&bus->free);
51 QTAILQ_INIT(&bus->used);
52 QTAILQ_INSERT_TAIL(&busses, bus, next);
55 USBBus *usb_bus_find(int busnr)
57 USBBus *bus;
59 if (-1 == busnr)
60 return QTAILQ_FIRST(&busses);
61 QTAILQ_FOREACH(bus, &busses, next) {
62 if (bus->busnr == busnr)
63 return bus;
65 return NULL;
68 static int usb_device_init(USBDevice *dev)
70 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
71 if (klass->init) {
72 return klass->init(dev);
74 return 0;
77 USBDevice *usb_device_find_device(USBDevice *dev, uint8_t addr)
79 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
80 if (klass->find_device) {
81 return klass->find_device(dev, addr);
83 return NULL;
86 static void usb_device_handle_destroy(USBDevice *dev)
88 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
89 if (klass->handle_destroy) {
90 klass->handle_destroy(dev);
94 void usb_device_cancel_packet(USBDevice *dev, USBPacket *p)
96 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
97 if (klass->cancel_packet) {
98 klass->cancel_packet(dev, p);
102 void usb_device_handle_attach(USBDevice *dev)
104 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
105 if (klass->handle_attach) {
106 klass->handle_attach(dev);
110 void usb_device_handle_reset(USBDevice *dev)
112 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
113 if (klass->handle_reset) {
114 klass->handle_reset(dev);
118 int usb_device_handle_control(USBDevice *dev, USBPacket *p, int request,
119 int value, int index, int length, uint8_t *data)
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);
126 return -ENOSYS;
129 int usb_device_handle_data(USBDevice *dev, USBPacket *p)
131 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
132 if (klass->handle_data) {
133 return klass->handle_data(dev, p);
135 return -ENOSYS;
138 const char *usb_device_get_product_desc(USBDevice *dev)
140 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
141 return klass->product_desc;
144 const USBDesc *usb_device_get_usb_desc(USBDevice *dev)
146 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
147 return klass->usb_desc;
150 void usb_device_set_interface(USBDevice *dev, int interface,
151 int alt_old, int alt_new)
153 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
154 if (klass->set_interface) {
155 klass->set_interface(dev, interface, alt_old, alt_new);
159 static int usb_qdev_init(DeviceState *qdev)
161 USBDevice *dev = USB_DEVICE(qdev);
162 int rc;
164 pstrcpy(dev->product_desc, sizeof(dev->product_desc),
165 usb_device_get_product_desc(dev));
166 dev->auto_attach = 1;
167 QLIST_INIT(&dev->strings);
168 usb_ep_init(dev);
169 rc = usb_claim_port(dev);
170 if (rc != 0) {
171 return rc;
173 rc = usb_device_init(dev);
174 if (rc != 0) {
175 usb_release_port(dev);
176 return rc;
178 if (dev->auto_attach) {
179 rc = usb_device_attach(dev);
180 if (rc != 0) {
181 usb_qdev_exit(qdev);
182 return rc;
185 return 0;
188 static int usb_qdev_exit(DeviceState *qdev)
190 USBDevice *dev = USB_DEVICE(qdev);
192 if (dev->attached) {
193 usb_device_detach(dev);
195 usb_device_handle_destroy(dev);
196 if (dev->port) {
197 usb_release_port(dev);
199 return 0;
202 typedef struct LegacyUSBFactory
204 const char *name;
205 const char *usbdevice_name;
206 USBDevice *(*usbdevice_init)(const char *params);
207 } LegacyUSBFactory;
209 static GSList *legacy_usb_factory;
211 void usb_legacy_register(const char *typename, const char *usbdevice_name,
212 USBDevice *(*usbdevice_init)(const char *params))
214 if (usbdevice_name) {
215 LegacyUSBFactory *f = g_malloc0(sizeof(*f));
216 f->name = typename;
217 f->usbdevice_name = usbdevice_name;
218 f->usbdevice_init = usbdevice_init;
219 legacy_usb_factory = g_slist_append(legacy_usb_factory, f);
223 USBDevice *usb_create(USBBus *bus, const char *name)
225 DeviceState *dev;
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;
233 error_report("%s: no bus specified, using \"%s\" for \"%s\"",
234 __FUNCTION__, bus->qbus.name, name);
236 #endif
238 dev = qdev_create(&bus->qbus, name);
239 return USB_DEVICE(dev);
242 USBDevice *usb_create_simple(USBBus *bus, const char *name)
244 USBDevice *dev = usb_create(bus, name);
245 int rc;
247 if (!dev) {
248 error_report("Failed to create USB device '%s'", name);
249 return NULL;
251 rc = qdev_init(&dev->qdev);
252 if (rc < 0) {
253 error_report("Failed to initialize USB device '%s'", name);
254 return NULL;
256 return dev;
259 static void usb_fill_port(USBPort *port, void *opaque, int index,
260 USBPortOps *ops, int speedmask)
262 port->opaque = opaque;
263 port->index = index;
264 port->ops = ops;
265 port->speedmask = speedmask;
266 usb_port_location(port, NULL, index + 1);
269 void usb_register_port(USBBus *bus, USBPort *port, void *opaque, int index,
270 USBPortOps *ops, int speedmask)
272 usb_fill_port(port, opaque, index, ops, speedmask);
273 QTAILQ_INSERT_TAIL(&bus->free, port, next);
274 bus->nfree++;
277 int usb_register_companion(const char *masterbus, USBPort *ports[],
278 uint32_t portcount, uint32_t firstport,
279 void *opaque, USBPortOps *ops, int speedmask)
281 USBBus *bus;
282 int i;
284 QTAILQ_FOREACH(bus, &busses, next) {
285 if (strcmp(bus->qbus.name, masterbus) == 0) {
286 break;
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);
298 return -1;
301 for (i = 0; i < portcount; i++) {
302 usb_fill_port(ports[i], opaque, i, ops, speedmask);
305 return bus->ops->register_companion(bus, ports, portcount, firstport);
308 void usb_port_location(USBPort *downstream, USBPort *upstream, int portnr)
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);
318 void usb_unregister_port(USBBus *bus, USBPort *port)
320 if (port->dev)
321 qdev_free(&port->dev->qdev);
322 QTAILQ_REMOVE(&bus->free, port, next);
323 bus->nfree--;
326 int usb_claim_port(USBDevice *dev)
328 USBBus *bus = usb_bus_from_device(dev);
329 USBPort *port;
331 assert(dev->port == NULL);
333 if (dev->port_path) {
334 QTAILQ_FOREACH(port, &bus->free, next) {
335 if (strcmp(port->path, dev->port_path) == 0) {
336 break;
339 if (port == NULL) {
340 error_report("Error: usb port %s (bus %s) not found (in use?)",
341 dev->port_path, bus->qbus.name);
342 return -1;
344 } else {
345 if (bus->nfree == 1 && strcmp(object_get_typename(OBJECT(dev)), "usb-hub") != 0) {
346 /* Create a new hub and chain it on */
347 usb_create_simple(bus, "usb-hub");
349 if (bus->nfree == 0) {
350 error_report("Error: tried to attach usb device %s to a bus "
351 "with no free ports", dev->product_desc);
352 return -1;
354 port = QTAILQ_FIRST(&bus->free);
356 trace_usb_port_claim(bus->busnr, port->path);
358 QTAILQ_REMOVE(&bus->free, port, next);
359 bus->nfree--;
361 dev->port = port;
362 port->dev = dev;
364 QTAILQ_INSERT_TAIL(&bus->used, port, next);
365 bus->nused++;
366 return 0;
369 void usb_release_port(USBDevice *dev)
371 USBBus *bus = usb_bus_from_device(dev);
372 USBPort *port = dev->port;
374 assert(port != NULL);
375 trace_usb_port_release(bus->busnr, port->path);
377 QTAILQ_REMOVE(&bus->used, port, next);
378 bus->nused--;
380 dev->port = NULL;
381 port->dev = NULL;
383 QTAILQ_INSERT_TAIL(&bus->free, port, next);
384 bus->nfree++;
387 int usb_device_attach(USBDevice *dev)
389 USBBus *bus = usb_bus_from_device(dev);
390 USBPort *port = dev->port;
392 assert(port != NULL);
393 assert(!dev->attached);
394 trace_usb_port_attach(bus->busnr, port->path);
396 if (!(port->speedmask & dev->speedmask)) {
397 error_report("Warning: speed mismatch trying to attach "
398 "usb device %s to bus %s",
399 dev->product_desc, bus->qbus.name);
400 return -1;
403 dev->attached++;
404 usb_attach(port);
406 return 0;
409 int usb_device_detach(USBDevice *dev)
411 USBBus *bus = usb_bus_from_device(dev);
412 USBPort *port = dev->port;
414 assert(port != NULL);
415 assert(dev->attached);
416 trace_usb_port_detach(bus->busnr, port->path);
418 usb_detach(port);
419 dev->attached--;
420 return 0;
423 int usb_device_delete_addr(int busnr, int addr)
425 USBBus *bus;
426 USBPort *port;
427 USBDevice *dev;
429 bus = usb_bus_find(busnr);
430 if (!bus)
431 return -1;
433 QTAILQ_FOREACH(port, &bus->used, next) {
434 if (port->dev->addr == addr)
435 break;
437 if (!port)
438 return -1;
439 dev = port->dev;
441 qdev_free(&dev->qdev);
442 return 0;
445 static const char *usb_speed(unsigned int speed)
447 static const char *txt[] = {
448 [ USB_SPEED_LOW ] = "1.5",
449 [ USB_SPEED_FULL ] = "12",
450 [ USB_SPEED_HIGH ] = "480",
451 [ USB_SPEED_SUPER ] = "5000",
453 if (speed >= ARRAY_SIZE(txt))
454 return "?";
455 return txt[speed];
458 static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
460 USBDevice *dev = USB_DEVICE(qdev);
461 USBBus *bus = usb_bus_from_device(dev);
463 monitor_printf(mon, "%*saddr %d.%d, port %s, speed %s, name %s%s\n",
464 indent, "", bus->busnr, dev->addr,
465 dev->port ? dev->port->path : "-",
466 usb_speed(dev->speed), dev->product_desc,
467 dev->attached ? ", attached" : "");
470 static char *usb_get_dev_path(DeviceState *qdev)
472 USBDevice *dev = USB_DEVICE(qdev);
473 return g_strdup(dev->port->path);
476 static char *usb_get_fw_dev_path(DeviceState *qdev)
478 USBDevice *dev = USB_DEVICE(qdev);
479 char *fw_path, *in;
480 ssize_t pos = 0, fw_len;
481 long nr;
483 fw_len = 32 + strlen(dev->port->path) * 6;
484 fw_path = g_malloc(fw_len);
485 in = dev->port->path;
486 while (fw_len - pos > 0) {
487 nr = strtol(in, &in, 10);
488 if (in[0] == '.') {
489 /* some hub between root port and device */
490 pos += snprintf(fw_path + pos, fw_len - pos, "hub@%ld/", nr);
491 in++;
492 } else {
493 /* the device itself */
494 pos += snprintf(fw_path + pos, fw_len - pos, "%s@%ld",
495 qdev_fw_name(qdev), nr);
496 break;
499 return fw_path;
502 void usb_info(Monitor *mon)
504 USBBus *bus;
505 USBDevice *dev;
506 USBPort *port;
508 if (QTAILQ_EMPTY(&busses)) {
509 monitor_printf(mon, "USB support not enabled\n");
510 return;
513 QTAILQ_FOREACH(bus, &busses, next) {
514 QTAILQ_FOREACH(port, &bus->used, next) {
515 dev = port->dev;
516 if (!dev)
517 continue;
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),
520 dev->product_desc);
525 /* handle legacy -usbdevice cmd line option */
526 USBDevice *usbdevice_create(const char *cmdline)
528 USBBus *bus = usb_bus_find(-1 /* any */);
529 LegacyUSBFactory *f = NULL;
530 GSList *i;
531 char driver[32];
532 const char *params;
533 int len;
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 {
543 params = "";
544 pstrcpy(driver, sizeof(driver), cmdline);
547 for (i = legacy_usb_factory; i; i = i->next) {
548 f = i->data;
549 if (strcmp(f->usbdevice_name, driver) == 0) {
550 break;
553 if (i == NULL) {
554 #if 0
555 /* no error because some drivers are not converted (yet) */
556 error_report("usbdevice %s not found", driver);
557 #endif
558 return NULL;
561 if (!f->usbdevice_init) {
562 if (*params) {
563 error_report("usbdevice %s accepts no params", driver);
564 return NULL;
566 return usb_create_simple(bus, f->name);
568 return f->usbdevice_init(params);
571 static void usb_device_class_init(ObjectClass *klass, void *data)
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;
580 static 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),
586 .class_init = usb_device_class_init,
589 static void usb_register_types(void)
591 type_register_static(&usb_device_type_info);
594 type_init(usb_register_types)