target-i386: Add API to get note's size
[qemu/ar7.git] / hw / usb / bus.c
blob2068640a58bcdf57d9d0f47f411d720c06863fad
1 #include "hw/hw.h"
2 #include "hw/usb.h"
3 #include "hw/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_BIT("full-path", USBDevice, flags,
23 USB_DEV_FLAG_FULL_PATH, true),
24 DEFINE_PROP_END_OF_LIST()
27 static int next_usb_bus = 0;
28 static QTAILQ_HEAD(, USBBus) busses = QTAILQ_HEAD_INITIALIZER(busses);
30 const VMStateDescription vmstate_usb_device = {
31 .name = "USBDevice",
32 .version_id = 1,
33 .minimum_version_id = 1,
34 .fields = (VMStateField []) {
35 VMSTATE_UINT8(addr, USBDevice),
36 VMSTATE_INT32(state, USBDevice),
37 VMSTATE_INT32(remote_wakeup, USBDevice),
38 VMSTATE_INT32(setup_state, USBDevice),
39 VMSTATE_INT32(setup_len, USBDevice),
40 VMSTATE_INT32(setup_index, USBDevice),
41 VMSTATE_UINT8_ARRAY(setup_buf, USBDevice, 8),
42 VMSTATE_END_OF_LIST(),
46 void usb_bus_new(USBBus *bus, USBBusOps *ops, DeviceState *host)
48 qbus_create_inplace(&bus->qbus, &usb_bus_info, host, NULL);
49 bus->ops = ops;
50 bus->busnr = next_usb_bus++;
51 bus->qbus.allow_hotplug = 1; /* Yes, we can */
52 QTAILQ_INIT(&bus->free);
53 QTAILQ_INIT(&bus->used);
54 QTAILQ_INSERT_TAIL(&busses, bus, next);
57 USBBus *usb_bus_find(int busnr)
59 USBBus *bus;
61 if (-1 == busnr)
62 return QTAILQ_FIRST(&busses);
63 QTAILQ_FOREACH(bus, &busses, next) {
64 if (bus->busnr == busnr)
65 return bus;
67 return NULL;
70 static int usb_device_init(USBDevice *dev)
72 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
73 if (klass->init) {
74 return klass->init(dev);
76 return 0;
79 USBDevice *usb_device_find_device(USBDevice *dev, uint8_t addr)
81 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
82 if (klass->find_device) {
83 return klass->find_device(dev, addr);
85 return NULL;
88 static void usb_device_handle_destroy(USBDevice *dev)
90 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
91 if (klass->handle_destroy) {
92 klass->handle_destroy(dev);
96 void usb_device_cancel_packet(USBDevice *dev, USBPacket *p)
98 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
99 if (klass->cancel_packet) {
100 klass->cancel_packet(dev, p);
104 void usb_device_handle_attach(USBDevice *dev)
106 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
107 if (klass->handle_attach) {
108 klass->handle_attach(dev);
112 void usb_device_handle_reset(USBDevice *dev)
114 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
115 if (klass->handle_reset) {
116 klass->handle_reset(dev);
120 int usb_device_handle_control(USBDevice *dev, USBPacket *p, int request,
121 int value, int index, int length, uint8_t *data)
123 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
124 if (klass->handle_control) {
125 return klass->handle_control(dev, p, request, value, index, length,
126 data);
128 return -ENOSYS;
131 int usb_device_handle_data(USBDevice *dev, USBPacket *p)
133 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
134 if (klass->handle_data) {
135 return klass->handle_data(dev, p);
137 return -ENOSYS;
140 const char *usb_device_get_product_desc(USBDevice *dev)
142 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
143 return klass->product_desc;
146 const USBDesc *usb_device_get_usb_desc(USBDevice *dev)
148 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
149 return klass->usb_desc;
152 void usb_device_set_interface(USBDevice *dev, int interface,
153 int alt_old, int alt_new)
155 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
156 if (klass->set_interface) {
157 klass->set_interface(dev, interface, alt_old, alt_new);
161 static int usb_qdev_init(DeviceState *qdev)
163 USBDevice *dev = USB_DEVICE(qdev);
164 int rc;
166 pstrcpy(dev->product_desc, sizeof(dev->product_desc),
167 usb_device_get_product_desc(dev));
168 dev->auto_attach = 1;
169 QLIST_INIT(&dev->strings);
170 usb_ep_init(dev);
171 rc = usb_claim_port(dev);
172 if (rc != 0) {
173 return rc;
175 rc = usb_device_init(dev);
176 if (rc != 0) {
177 usb_release_port(dev);
178 return rc;
180 if (dev->auto_attach) {
181 rc = usb_device_attach(dev);
182 if (rc != 0) {
183 usb_qdev_exit(qdev);
184 return rc;
187 return 0;
190 static int usb_qdev_exit(DeviceState *qdev)
192 USBDevice *dev = USB_DEVICE(qdev);
194 if (dev->attached) {
195 usb_device_detach(dev);
197 usb_device_handle_destroy(dev);
198 if (dev->port) {
199 usb_release_port(dev);
201 return 0;
204 typedef struct LegacyUSBFactory
206 const char *name;
207 const char *usbdevice_name;
208 USBDevice *(*usbdevice_init)(USBBus *bus, const char *params);
209 } LegacyUSBFactory;
211 static GSList *legacy_usb_factory;
213 void usb_legacy_register(const char *typename, const char *usbdevice_name,
214 USBDevice *(*usbdevice_init)(USBBus *bus,
215 const char *params))
217 if (usbdevice_name) {
218 LegacyUSBFactory *f = g_malloc0(sizeof(*f));
219 f->name = typename;
220 f->usbdevice_name = usbdevice_name;
221 f->usbdevice_init = usbdevice_init;
222 legacy_usb_factory = g_slist_append(legacy_usb_factory, f);
226 USBDevice *usb_create(USBBus *bus, const char *name)
228 DeviceState *dev;
230 dev = qdev_create(&bus->qbus, name);
231 return USB_DEVICE(dev);
234 USBDevice *usb_create_simple(USBBus *bus, const char *name)
236 USBDevice *dev = usb_create(bus, name);
237 int rc;
239 if (!dev) {
240 error_report("Failed to create USB device '%s'", name);
241 return NULL;
243 rc = qdev_init(&dev->qdev);
244 if (rc < 0) {
245 error_report("Failed to initialize USB device '%s'", name);
246 return NULL;
248 return dev;
251 static void usb_fill_port(USBPort *port, void *opaque, int index,
252 USBPortOps *ops, int speedmask)
254 port->opaque = opaque;
255 port->index = index;
256 port->ops = ops;
257 port->speedmask = speedmask;
258 usb_port_location(port, NULL, index + 1);
261 void usb_register_port(USBBus *bus, USBPort *port, void *opaque, int index,
262 USBPortOps *ops, int speedmask)
264 usb_fill_port(port, opaque, index, ops, speedmask);
265 QTAILQ_INSERT_TAIL(&bus->free, port, next);
266 bus->nfree++;
269 int usb_register_companion(const char *masterbus, USBPort *ports[],
270 uint32_t portcount, uint32_t firstport,
271 void *opaque, USBPortOps *ops, int speedmask)
273 USBBus *bus;
274 int i;
276 QTAILQ_FOREACH(bus, &busses, next) {
277 if (strcmp(bus->qbus.name, masterbus) == 0) {
278 break;
282 if (!bus || !bus->ops->register_companion) {
283 qerror_report(QERR_INVALID_PARAMETER_VALUE, "masterbus",
284 "an USB masterbus");
285 if (bus) {
286 error_printf_unless_qmp(
287 "USB bus '%s' does not allow companion controllers\n",
288 masterbus);
290 return -1;
293 for (i = 0; i < portcount; i++) {
294 usb_fill_port(ports[i], opaque, i, ops, speedmask);
297 return bus->ops->register_companion(bus, ports, portcount, firstport);
300 void usb_port_location(USBPort *downstream, USBPort *upstream, int portnr)
302 if (upstream) {
303 snprintf(downstream->path, sizeof(downstream->path), "%s.%d",
304 upstream->path, portnr);
305 } else {
306 snprintf(downstream->path, sizeof(downstream->path), "%d", portnr);
310 void usb_unregister_port(USBBus *bus, USBPort *port)
312 if (port->dev)
313 qdev_free(&port->dev->qdev);
314 QTAILQ_REMOVE(&bus->free, port, next);
315 bus->nfree--;
318 int usb_claim_port(USBDevice *dev)
320 USBBus *bus = usb_bus_from_device(dev);
321 USBPort *port;
323 assert(dev->port == NULL);
325 if (dev->port_path) {
326 QTAILQ_FOREACH(port, &bus->free, next) {
327 if (strcmp(port->path, dev->port_path) == 0) {
328 break;
331 if (port == NULL) {
332 error_report("Error: usb port %s (bus %s) not found (in use?)",
333 dev->port_path, bus->qbus.name);
334 return -1;
336 } else {
337 if (bus->nfree == 1 && strcmp(object_get_typename(OBJECT(dev)), "usb-hub") != 0) {
338 /* Create a new hub and chain it on */
339 usb_create_simple(bus, "usb-hub");
341 if (bus->nfree == 0) {
342 error_report("Error: tried to attach usb device %s to a bus "
343 "with no free ports", dev->product_desc);
344 return -1;
346 port = QTAILQ_FIRST(&bus->free);
348 trace_usb_port_claim(bus->busnr, port->path);
350 QTAILQ_REMOVE(&bus->free, port, next);
351 bus->nfree--;
353 dev->port = port;
354 port->dev = dev;
356 QTAILQ_INSERT_TAIL(&bus->used, port, next);
357 bus->nused++;
358 return 0;
361 void usb_release_port(USBDevice *dev)
363 USBBus *bus = usb_bus_from_device(dev);
364 USBPort *port = dev->port;
366 assert(port != NULL);
367 trace_usb_port_release(bus->busnr, port->path);
369 QTAILQ_REMOVE(&bus->used, port, next);
370 bus->nused--;
372 dev->port = NULL;
373 port->dev = NULL;
375 QTAILQ_INSERT_TAIL(&bus->free, port, next);
376 bus->nfree++;
379 int usb_device_attach(USBDevice *dev)
381 USBBus *bus = usb_bus_from_device(dev);
382 USBPort *port = dev->port;
384 assert(port != NULL);
385 assert(!dev->attached);
386 trace_usb_port_attach(bus->busnr, port->path);
388 if (!(port->speedmask & dev->speedmask)) {
389 error_report("Warning: speed mismatch trying to attach "
390 "usb device %s to bus %s",
391 dev->product_desc, bus->qbus.name);
392 return -1;
395 dev->attached++;
396 usb_attach(port);
398 return 0;
401 int usb_device_detach(USBDevice *dev)
403 USBBus *bus = usb_bus_from_device(dev);
404 USBPort *port = dev->port;
406 assert(port != NULL);
407 assert(dev->attached);
408 trace_usb_port_detach(bus->busnr, port->path);
410 usb_detach(port);
411 dev->attached--;
412 return 0;
415 int usb_device_delete_addr(int busnr, int addr)
417 USBBus *bus;
418 USBPort *port;
419 USBDevice *dev;
421 bus = usb_bus_find(busnr);
422 if (!bus)
423 return -1;
425 QTAILQ_FOREACH(port, &bus->used, next) {
426 if (port->dev->addr == addr)
427 break;
429 if (!port)
430 return -1;
431 dev = port->dev;
433 qdev_free(&dev->qdev);
434 return 0;
437 static const char *usb_speed(unsigned int speed)
439 static const char *txt[] = {
440 [ USB_SPEED_LOW ] = "1.5",
441 [ USB_SPEED_FULL ] = "12",
442 [ USB_SPEED_HIGH ] = "480",
443 [ USB_SPEED_SUPER ] = "5000",
445 if (speed >= ARRAY_SIZE(txt))
446 return "?";
447 return txt[speed];
450 static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
452 USBDevice *dev = USB_DEVICE(qdev);
453 USBBus *bus = usb_bus_from_device(dev);
455 monitor_printf(mon, "%*saddr %d.%d, port %s, speed %s, name %s%s\n",
456 indent, "", bus->busnr, dev->addr,
457 dev->port ? dev->port->path : "-",
458 usb_speed(dev->speed), dev->product_desc,
459 dev->attached ? ", attached" : "");
462 static char *usb_get_dev_path(DeviceState *qdev)
464 USBDevice *dev = USB_DEVICE(qdev);
465 DeviceState *hcd = qdev->parent_bus->parent;
466 char *id = NULL;
468 if ((dev->flags & (1 << USB_DEV_FLAG_FULL_PATH)) &&
469 hcd && hcd->parent_bus && hcd->parent_bus->info->get_dev_path) {
470 id = hcd->parent_bus->info->get_dev_path(hcd);
472 if (id) {
473 char *ret = g_strdup_printf("%s/%s", id, dev->port->path);
474 g_free(id);
475 return ret;
476 } else {
477 return g_strdup(dev->port->path);
481 static char *usb_get_fw_dev_path(DeviceState *qdev)
483 USBDevice *dev = USB_DEVICE(qdev);
484 char *fw_path, *in;
485 ssize_t pos = 0, fw_len;
486 long nr;
488 fw_len = 32 + strlen(dev->port->path) * 6;
489 fw_path = g_malloc(fw_len);
490 in = dev->port->path;
491 while (fw_len - pos > 0) {
492 nr = strtol(in, &in, 10);
493 if (in[0] == '.') {
494 /* some hub between root port and device */
495 pos += snprintf(fw_path + pos, fw_len - pos, "hub@%ld/", nr);
496 in++;
497 } else {
498 /* the device itself */
499 pos += snprintf(fw_path + pos, fw_len - pos, "%s@%ld",
500 qdev_fw_name(qdev), nr);
501 break;
504 return fw_path;
507 void usb_info(Monitor *mon)
509 USBBus *bus;
510 USBDevice *dev;
511 USBPort *port;
513 if (QTAILQ_EMPTY(&busses)) {
514 monitor_printf(mon, "USB support not enabled\n");
515 return;
518 QTAILQ_FOREACH(bus, &busses, next) {
519 QTAILQ_FOREACH(port, &bus->used, next) {
520 dev = port->dev;
521 if (!dev)
522 continue;
523 monitor_printf(mon, " Device %d.%d, Port %s, Speed %s Mb/s, Product %s\n",
524 bus->busnr, dev->addr, port->path, usb_speed(dev->speed),
525 dev->product_desc);
530 /* handle legacy -usbdevice cmd line option */
531 USBDevice *usbdevice_create(const char *cmdline)
533 USBBus *bus = usb_bus_find(-1 /* any */);
534 LegacyUSBFactory *f = NULL;
535 GSList *i;
536 char driver[32];
537 const char *params;
538 int len;
540 params = strchr(cmdline,':');
541 if (params) {
542 params++;
543 len = params - cmdline;
544 if (len > sizeof(driver))
545 len = sizeof(driver);
546 pstrcpy(driver, len, cmdline);
547 } else {
548 params = "";
549 pstrcpy(driver, sizeof(driver), cmdline);
552 for (i = legacy_usb_factory; i; i = i->next) {
553 f = i->data;
554 if (strcmp(f->usbdevice_name, driver) == 0) {
555 break;
558 if (i == NULL) {
559 #if 0
560 /* no error because some drivers are not converted (yet) */
561 error_report("usbdevice %s not found", driver);
562 #endif
563 return NULL;
566 if (!f->usbdevice_init) {
567 if (*params) {
568 error_report("usbdevice %s accepts no params", driver);
569 return NULL;
571 return usb_create_simple(bus, f->name);
573 return f->usbdevice_init(bus, params);
576 static void usb_device_class_init(ObjectClass *klass, void *data)
578 DeviceClass *k = DEVICE_CLASS(klass);
579 k->bus_info = &usb_bus_info;
580 k->init = usb_qdev_init;
581 k->unplug = qdev_simple_unplug_cb;
582 k->exit = usb_qdev_exit;
585 static TypeInfo usb_device_type_info = {
586 .name = TYPE_USB_DEVICE,
587 .parent = TYPE_DEVICE,
588 .instance_size = sizeof(USBDevice),
589 .abstract = true,
590 .class_size = sizeof(USBDeviceClass),
591 .class_init = usb_device_class_init,
594 static void usb_register_types(void)
596 type_register_static(&usb_device_type_info);
599 type_init(usb_register_types)