usb: fix usb_qdev_init error handling.
[qemu/opensuse.git] / hw / usb-bus.c
blob8cafb76fff5d44a5e9ea150bd5084b7471eee7ad
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_qdev_init(DeviceState *qdev, DeviceInfo *base)
70 USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev);
71 USBDeviceInfo *info = DO_UPCAST(USBDeviceInfo, qdev, base);
72 int rc;
74 pstrcpy(dev->product_desc, sizeof(dev->product_desc), info->product_desc);
75 dev->info = info;
76 dev->auto_attach = 1;
77 QLIST_INIT(&dev->strings);
78 rc = usb_claim_port(dev);
79 if (rc != 0) {
80 goto err;
82 rc = dev->info->init(dev);
83 if (rc != 0) {
84 goto err;
86 if (dev->auto_attach) {
87 rc = usb_device_attach(dev);
88 if (rc != 0) {
89 goto err;
92 return 0;
94 err:
95 usb_qdev_exit(qdev);
96 return rc;
99 static int usb_qdev_exit(DeviceState *qdev)
101 USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev);
103 if (dev->attached) {
104 usb_device_detach(dev);
106 if (dev->info->handle_destroy) {
107 dev->info->handle_destroy(dev);
109 if (dev->port) {
110 usb_release_port(dev);
112 return 0;
115 void usb_qdev_register(USBDeviceInfo *info)
117 info->qdev.bus_info = &usb_bus_info;
118 info->qdev.init = usb_qdev_init;
119 info->qdev.unplug = qdev_simple_unplug_cb;
120 info->qdev.exit = usb_qdev_exit;
121 qdev_register(&info->qdev);
124 void usb_qdev_register_many(USBDeviceInfo *info)
126 while (info->qdev.name) {
127 usb_qdev_register(info);
128 info++;
132 USBDevice *usb_create(USBBus *bus, const char *name)
134 DeviceState *dev;
136 #if 1
137 /* temporary stopgap until all usb is properly qdev-ified */
138 if (!bus) {
139 bus = usb_bus_find(-1);
140 if (!bus)
141 return NULL;
142 error_report("%s: no bus specified, using \"%s\" for \"%s\"\n",
143 __FUNCTION__, bus->qbus.name, name);
145 #endif
147 dev = qdev_create(&bus->qbus, name);
148 return DO_UPCAST(USBDevice, qdev, dev);
151 USBDevice *usb_create_simple(USBBus *bus, const char *name)
153 USBDevice *dev = usb_create(bus, name);
154 int rc;
156 if (!dev) {
157 error_report("Failed to create USB device '%s'\n", name);
158 return NULL;
160 rc = qdev_init(&dev->qdev);
161 if (rc < 0) {
162 error_report("Failed to initialize USB device '%s'\n", name);
163 return NULL;
165 return dev;
168 static void usb_fill_port(USBPort *port, void *opaque, int index,
169 USBPortOps *ops, int speedmask)
171 port->opaque = opaque;
172 port->index = index;
173 port->ops = ops;
174 port->speedmask = speedmask;
175 usb_port_location(port, NULL, index + 1);
178 void usb_register_port(USBBus *bus, USBPort *port, void *opaque, int index,
179 USBPortOps *ops, int speedmask)
181 usb_fill_port(port, opaque, index, ops, speedmask);
182 QTAILQ_INSERT_TAIL(&bus->free, port, next);
183 bus->nfree++;
186 int usb_register_companion(const char *masterbus, USBPort *ports[],
187 uint32_t portcount, uint32_t firstport,
188 void *opaque, USBPortOps *ops, int speedmask)
190 USBBus *bus;
191 int i;
193 QTAILQ_FOREACH(bus, &busses, next) {
194 if (strcmp(bus->qbus.name, masterbus) == 0) {
195 break;
199 if (!bus || !bus->ops->register_companion) {
200 qerror_report(QERR_INVALID_PARAMETER_VALUE, "masterbus",
201 "an USB masterbus");
202 if (bus) {
203 error_printf_unless_qmp(
204 "USB bus '%s' does not allow companion controllers\n",
205 masterbus);
207 return -1;
210 for (i = 0; i < portcount; i++) {
211 usb_fill_port(ports[i], opaque, i, ops, speedmask);
214 return bus->ops->register_companion(bus, ports, portcount, firstport);
217 void usb_port_location(USBPort *downstream, USBPort *upstream, int portnr)
219 if (upstream) {
220 snprintf(downstream->path, sizeof(downstream->path), "%s.%d",
221 upstream->path, portnr);
222 } else {
223 snprintf(downstream->path, sizeof(downstream->path), "%d", portnr);
227 void usb_unregister_port(USBBus *bus, USBPort *port)
229 if (port->dev)
230 qdev_free(&port->dev->qdev);
231 QTAILQ_REMOVE(&bus->free, port, next);
232 bus->nfree--;
235 int usb_claim_port(USBDevice *dev)
237 USBBus *bus = usb_bus_from_device(dev);
238 USBPort *port;
240 assert(dev->port == NULL);
242 if (dev->port_path) {
243 QTAILQ_FOREACH(port, &bus->free, next) {
244 if (strcmp(port->path, dev->port_path) == 0) {
245 break;
248 if (port == NULL) {
249 error_report("Error: usb port %s (bus %s) not found (in use?)\n",
250 dev->port_path, bus->qbus.name);
251 return -1;
253 } else {
254 if (bus->nfree == 1 && strcmp(dev->qdev.info->name, "usb-hub") != 0) {
255 /* Create a new hub and chain it on */
256 usb_create_simple(bus, "usb-hub");
258 if (bus->nfree == 0) {
259 error_report("Error: tried to attach usb device %s to a bus "
260 "with no free ports\n", dev->product_desc);
261 return -1;
263 port = QTAILQ_FIRST(&bus->free);
265 trace_usb_port_claim(bus->busnr, port->path);
267 QTAILQ_REMOVE(&bus->free, port, next);
268 bus->nfree--;
270 dev->port = port;
271 port->dev = dev;
273 QTAILQ_INSERT_TAIL(&bus->used, port, next);
274 bus->nused++;
275 return 0;
278 void usb_release_port(USBDevice *dev)
280 USBBus *bus = usb_bus_from_device(dev);
281 USBPort *port = dev->port;
283 assert(port != NULL);
284 trace_usb_port_release(bus->busnr, port->path);
286 QTAILQ_REMOVE(&bus->used, port, next);
287 bus->nused--;
289 dev->port = NULL;
290 port->dev = NULL;
292 QTAILQ_INSERT_TAIL(&bus->free, port, next);
293 bus->nfree++;
296 int usb_device_attach(USBDevice *dev)
298 USBBus *bus = usb_bus_from_device(dev);
299 USBPort *port = dev->port;
301 assert(port != NULL);
302 assert(!dev->attached);
303 trace_usb_port_attach(bus->busnr, port->path);
305 if (!(port->speedmask & dev->speedmask)) {
306 error_report("Warning: speed mismatch trying to attach "
307 "usb device %s to bus %s\n",
308 dev->product_desc, bus->qbus.name);
309 return -1;
312 dev->attached++;
313 usb_attach(port);
315 return 0;
318 int usb_device_detach(USBDevice *dev)
320 USBBus *bus = usb_bus_from_device(dev);
321 USBPort *port = dev->port;
323 assert(port != NULL);
324 assert(dev->attached);
325 trace_usb_port_detach(bus->busnr, port->path);
327 usb_detach(port);
328 dev->attached--;
329 return 0;
332 int usb_device_delete_addr(int busnr, int addr)
334 USBBus *bus;
335 USBPort *port;
336 USBDevice *dev;
338 bus = usb_bus_find(busnr);
339 if (!bus)
340 return -1;
342 QTAILQ_FOREACH(port, &bus->used, next) {
343 if (port->dev->addr == addr)
344 break;
346 if (!port)
347 return -1;
348 dev = port->dev;
350 qdev_free(&dev->qdev);
351 return 0;
354 static const char *usb_speed(unsigned int speed)
356 static const char *txt[] = {
357 [ USB_SPEED_LOW ] = "1.5",
358 [ USB_SPEED_FULL ] = "12",
359 [ USB_SPEED_HIGH ] = "480",
360 [ USB_SPEED_SUPER ] = "5000",
362 if (speed >= ARRAY_SIZE(txt))
363 return "?";
364 return txt[speed];
367 static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
369 USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev);
370 USBBus *bus = usb_bus_from_device(dev);
372 monitor_printf(mon, "%*saddr %d.%d, port %s, speed %s, name %s%s\n",
373 indent, "", bus->busnr, dev->addr,
374 dev->port ? dev->port->path : "-",
375 usb_speed(dev->speed), dev->product_desc,
376 dev->attached ? ", attached" : "");
379 static char *usb_get_dev_path(DeviceState *qdev)
381 USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev);
382 return g_strdup(dev->port->path);
385 static char *usb_get_fw_dev_path(DeviceState *qdev)
387 USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev);
388 char *fw_path, *in;
389 ssize_t pos = 0, fw_len;
390 long nr;
392 fw_len = 32 + strlen(dev->port->path) * 6;
393 fw_path = g_malloc(fw_len);
394 in = dev->port->path;
395 while (fw_len - pos > 0) {
396 nr = strtol(in, &in, 10);
397 if (in[0] == '.') {
398 /* some hub between root port and device */
399 pos += snprintf(fw_path + pos, fw_len - pos, "hub@%ld/", nr);
400 in++;
401 } else {
402 /* the device itself */
403 pos += snprintf(fw_path + pos, fw_len - pos, "%s@%ld",
404 qdev_fw_name(qdev), nr);
405 break;
408 return fw_path;
411 void usb_info(Monitor *mon)
413 USBBus *bus;
414 USBDevice *dev;
415 USBPort *port;
417 if (QTAILQ_EMPTY(&busses)) {
418 monitor_printf(mon, "USB support not enabled\n");
419 return;
422 QTAILQ_FOREACH(bus, &busses, next) {
423 QTAILQ_FOREACH(port, &bus->used, next) {
424 dev = port->dev;
425 if (!dev)
426 continue;
427 monitor_printf(mon, " Device %d.%d, Port %s, Speed %s Mb/s, Product %s\n",
428 bus->busnr, dev->addr, port->path, usb_speed(dev->speed),
429 dev->product_desc);
434 /* handle legacy -usbdevice cmd line option */
435 USBDevice *usbdevice_create(const char *cmdline)
437 USBBus *bus = usb_bus_find(-1 /* any */);
438 DeviceInfo *info;
439 USBDeviceInfo *usb;
440 char driver[32];
441 const char *params;
442 int len;
444 params = strchr(cmdline,':');
445 if (params) {
446 params++;
447 len = params - cmdline;
448 if (len > sizeof(driver))
449 len = sizeof(driver);
450 pstrcpy(driver, len, cmdline);
451 } else {
452 params = "";
453 pstrcpy(driver, sizeof(driver), cmdline);
456 for (info = device_info_list; info != NULL; info = info->next) {
457 if (info->bus_info != &usb_bus_info)
458 continue;
459 usb = DO_UPCAST(USBDeviceInfo, qdev, info);
460 if (usb->usbdevice_name == NULL)
461 continue;
462 if (strcmp(usb->usbdevice_name, driver) != 0)
463 continue;
464 break;
466 if (info == NULL) {
467 #if 0
468 /* no error because some drivers are not converted (yet) */
469 error_report("usbdevice %s not found", driver);
470 #endif
471 return NULL;
474 if (!usb->usbdevice_init) {
475 if (*params) {
476 error_report("usbdevice %s accepts no params", driver);
477 return NULL;
479 return usb_create_simple(bus, usb->qdev.name);
481 return usb->usbdevice_init(params);