usb: make usb_create_simple catch and pass up errors.
[qemu.git] / hw / usb-bus.c
blobf8b98072328e414997e293a1e40995d0ac041367
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);
13 static struct BusInfo usb_bus_info = {
14 .name = "USB",
15 .size = sizeof(USBBus),
16 .print_dev = usb_bus_dev_print,
17 .get_dev_path = usb_get_dev_path,
18 .get_fw_dev_path = usb_get_fw_dev_path,
19 .props = (Property[]) {
20 DEFINE_PROP_STRING("port", USBDevice, port_path),
21 DEFINE_PROP_END_OF_LIST()
24 static int next_usb_bus = 0;
25 static QTAILQ_HEAD(, USBBus) busses = QTAILQ_HEAD_INITIALIZER(busses);
27 const VMStateDescription vmstate_usb_device = {
28 .name = "USBDevice",
29 .version_id = 1,
30 .minimum_version_id = 1,
31 .fields = (VMStateField []) {
32 VMSTATE_UINT8(addr, USBDevice),
33 VMSTATE_INT32(state, USBDevice),
34 VMSTATE_INT32(remote_wakeup, USBDevice),
35 VMSTATE_INT32(setup_state, USBDevice),
36 VMSTATE_INT32(setup_len, USBDevice),
37 VMSTATE_INT32(setup_index, USBDevice),
38 VMSTATE_UINT8_ARRAY(setup_buf, USBDevice, 8),
39 VMSTATE_END_OF_LIST(),
43 void usb_bus_new(USBBus *bus, USBBusOps *ops, DeviceState *host)
45 qbus_create_inplace(&bus->qbus, &usb_bus_info, host, NULL);
46 bus->ops = ops;
47 bus->busnr = next_usb_bus++;
48 bus->qbus.allow_hotplug = 1; /* Yes, we can */
49 QTAILQ_INIT(&bus->free);
50 QTAILQ_INIT(&bus->used);
51 QTAILQ_INSERT_TAIL(&busses, bus, next);
54 USBBus *usb_bus_find(int busnr)
56 USBBus *bus;
58 if (-1 == busnr)
59 return QTAILQ_FIRST(&busses);
60 QTAILQ_FOREACH(bus, &busses, next) {
61 if (bus->busnr == busnr)
62 return bus;
64 return NULL;
67 static int usb_qdev_init(DeviceState *qdev, DeviceInfo *base)
69 USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev);
70 USBDeviceInfo *info = DO_UPCAST(USBDeviceInfo, qdev, base);
71 int rc;
73 pstrcpy(dev->product_desc, sizeof(dev->product_desc), info->product_desc);
74 dev->info = info;
75 dev->auto_attach = 1;
76 QLIST_INIT(&dev->strings);
77 rc = usb_claim_port(dev);
78 if (rc == 0) {
79 rc = dev->info->init(dev);
81 if (rc == 0 && dev->auto_attach) {
82 rc = usb_device_attach(dev);
84 return rc;
87 static int usb_qdev_exit(DeviceState *qdev)
89 USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev);
91 if (dev->attached) {
92 usb_device_detach(dev);
94 if (dev->info->handle_destroy) {
95 dev->info->handle_destroy(dev);
97 if (dev->port) {
98 usb_release_port(dev);
100 return 0;
103 void usb_qdev_register(USBDeviceInfo *info)
105 info->qdev.bus_info = &usb_bus_info;
106 info->qdev.init = usb_qdev_init;
107 info->qdev.unplug = qdev_simple_unplug_cb;
108 info->qdev.exit = usb_qdev_exit;
109 qdev_register(&info->qdev);
112 void usb_qdev_register_many(USBDeviceInfo *info)
114 while (info->qdev.name) {
115 usb_qdev_register(info);
116 info++;
120 USBDevice *usb_create(USBBus *bus, const char *name)
122 DeviceState *dev;
124 #if 1
125 /* temporary stopgap until all usb is properly qdev-ified */
126 if (!bus) {
127 bus = usb_bus_find(-1);
128 if (!bus)
129 return NULL;
130 error_report("%s: no bus specified, using \"%s\" for \"%s\"\n",
131 __FUNCTION__, bus->qbus.name, name);
133 #endif
135 dev = qdev_create(&bus->qbus, name);
136 return DO_UPCAST(USBDevice, qdev, dev);
139 USBDevice *usb_create_simple(USBBus *bus, const char *name)
141 USBDevice *dev = usb_create(bus, name);
142 int rc;
144 if (!dev) {
145 error_report("Failed to create USB device '%s'\n", name);
146 return NULL;
148 rc = qdev_init(&dev->qdev);
149 if (rc < 0) {
150 error_report("Failed to initialize USB device '%s'\n", name);
151 return NULL;
153 return dev;
156 static void usb_fill_port(USBPort *port, void *opaque, int index,
157 USBPortOps *ops, int speedmask)
159 port->opaque = opaque;
160 port->index = index;
161 port->ops = ops;
162 port->speedmask = speedmask;
163 usb_port_location(port, NULL, index + 1);
166 void usb_register_port(USBBus *bus, USBPort *port, void *opaque, int index,
167 USBPortOps *ops, int speedmask)
169 usb_fill_port(port, opaque, index, ops, speedmask);
170 QTAILQ_INSERT_TAIL(&bus->free, port, next);
171 bus->nfree++;
174 int usb_register_companion(const char *masterbus, USBPort *ports[],
175 uint32_t portcount, uint32_t firstport,
176 void *opaque, USBPortOps *ops, int speedmask)
178 USBBus *bus;
179 int i;
181 QTAILQ_FOREACH(bus, &busses, next) {
182 if (strcmp(bus->qbus.name, masterbus) == 0) {
183 break;
187 if (!bus || !bus->ops->register_companion) {
188 qerror_report(QERR_INVALID_PARAMETER_VALUE, "masterbus",
189 "an USB masterbus");
190 if (bus) {
191 error_printf_unless_qmp(
192 "USB bus '%s' does not allow companion controllers\n",
193 masterbus);
195 return -1;
198 for (i = 0; i < portcount; i++) {
199 usb_fill_port(ports[i], opaque, i, ops, speedmask);
202 return bus->ops->register_companion(bus, ports, portcount, firstport);
205 void usb_port_location(USBPort *downstream, USBPort *upstream, int portnr)
207 if (upstream) {
208 snprintf(downstream->path, sizeof(downstream->path), "%s.%d",
209 upstream->path, portnr);
210 } else {
211 snprintf(downstream->path, sizeof(downstream->path), "%d", portnr);
215 void usb_unregister_port(USBBus *bus, USBPort *port)
217 if (port->dev)
218 qdev_free(&port->dev->qdev);
219 QTAILQ_REMOVE(&bus->free, port, next);
220 bus->nfree--;
223 int usb_claim_port(USBDevice *dev)
225 USBBus *bus = usb_bus_from_device(dev);
226 USBPort *port;
228 assert(dev->port == NULL);
230 if (dev->port_path) {
231 QTAILQ_FOREACH(port, &bus->free, next) {
232 if (strcmp(port->path, dev->port_path) == 0) {
233 break;
236 if (port == NULL) {
237 error_report("Error: usb port %s (bus %s) not found (in use?)\n",
238 dev->port_path, bus->qbus.name);
239 return -1;
241 } else {
242 if (bus->nfree == 1 && strcmp(dev->qdev.info->name, "usb-hub") != 0) {
243 /* Create a new hub and chain it on */
244 usb_create_simple(bus, "usb-hub");
246 if (bus->nfree == 0) {
247 error_report("Error: tried to attach usb device %s to a bus "
248 "with no free ports\n", dev->product_desc);
249 return -1;
251 port = QTAILQ_FIRST(&bus->free);
253 trace_usb_port_claim(bus->busnr, port->path);
255 QTAILQ_REMOVE(&bus->free, port, next);
256 bus->nfree--;
258 dev->port = port;
259 port->dev = dev;
261 QTAILQ_INSERT_TAIL(&bus->used, port, next);
262 bus->nused++;
263 return 0;
266 void usb_release_port(USBDevice *dev)
268 USBBus *bus = usb_bus_from_device(dev);
269 USBPort *port = dev->port;
271 assert(port != NULL);
272 trace_usb_port_release(bus->busnr, port->path);
274 QTAILQ_REMOVE(&bus->used, port, next);
275 bus->nused--;
277 dev->port = NULL;
278 port->dev = NULL;
280 QTAILQ_INSERT_TAIL(&bus->free, port, next);
281 bus->nfree++;
284 int usb_device_attach(USBDevice *dev)
286 USBBus *bus = usb_bus_from_device(dev);
287 USBPort *port = dev->port;
289 assert(port != NULL);
290 assert(!dev->attached);
291 trace_usb_port_attach(bus->busnr, port->path);
293 if (!(port->speedmask & dev->speedmask)) {
294 error_report("Warning: speed mismatch trying to attach "
295 "usb device %s to bus %s\n",
296 dev->product_desc, bus->qbus.name);
297 return -1;
300 dev->attached++;
301 usb_attach(port);
303 return 0;
306 int usb_device_detach(USBDevice *dev)
308 USBBus *bus = usb_bus_from_device(dev);
309 USBPort *port = dev->port;
311 assert(port != NULL);
312 assert(dev->attached);
313 trace_usb_port_detach(bus->busnr, port->path);
315 usb_detach(port);
316 dev->attached--;
317 return 0;
320 int usb_device_delete_addr(int busnr, int addr)
322 USBBus *bus;
323 USBPort *port;
324 USBDevice *dev;
326 bus = usb_bus_find(busnr);
327 if (!bus)
328 return -1;
330 QTAILQ_FOREACH(port, &bus->used, next) {
331 if (port->dev->addr == addr)
332 break;
334 if (!port)
335 return -1;
336 dev = port->dev;
338 qdev_free(&dev->qdev);
339 return 0;
342 static const char *usb_speed(unsigned int speed)
344 static const char *txt[] = {
345 [ USB_SPEED_LOW ] = "1.5",
346 [ USB_SPEED_FULL ] = "12",
347 [ USB_SPEED_HIGH ] = "480",
348 [ USB_SPEED_SUPER ] = "5000",
350 if (speed >= ARRAY_SIZE(txt))
351 return "?";
352 return txt[speed];
355 static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
357 USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev);
358 USBBus *bus = usb_bus_from_device(dev);
360 monitor_printf(mon, "%*saddr %d.%d, port %s, speed %s, name %s%s\n",
361 indent, "", bus->busnr, dev->addr,
362 dev->port ? dev->port->path : "-",
363 usb_speed(dev->speed), dev->product_desc,
364 dev->attached ? ", attached" : "");
367 static char *usb_get_dev_path(DeviceState *qdev)
369 USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev);
370 return g_strdup(dev->port->path);
373 static char *usb_get_fw_dev_path(DeviceState *qdev)
375 USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev);
376 char *fw_path, *in;
377 ssize_t pos = 0, fw_len;
378 long nr;
380 fw_len = 32 + strlen(dev->port->path) * 6;
381 fw_path = g_malloc(fw_len);
382 in = dev->port->path;
383 while (fw_len - pos > 0) {
384 nr = strtol(in, &in, 10);
385 if (in[0] == '.') {
386 /* some hub between root port and device */
387 pos += snprintf(fw_path + pos, fw_len - pos, "hub@%ld/", nr);
388 in++;
389 } else {
390 /* the device itself */
391 pos += snprintf(fw_path + pos, fw_len - pos, "%s@%ld",
392 qdev_fw_name(qdev), nr);
393 break;
396 return fw_path;
399 void usb_info(Monitor *mon)
401 USBBus *bus;
402 USBDevice *dev;
403 USBPort *port;
405 if (QTAILQ_EMPTY(&busses)) {
406 monitor_printf(mon, "USB support not enabled\n");
407 return;
410 QTAILQ_FOREACH(bus, &busses, next) {
411 QTAILQ_FOREACH(port, &bus->used, next) {
412 dev = port->dev;
413 if (!dev)
414 continue;
415 monitor_printf(mon, " Device %d.%d, Port %s, Speed %s Mb/s, Product %s\n",
416 bus->busnr, dev->addr, port->path, usb_speed(dev->speed),
417 dev->product_desc);
422 /* handle legacy -usbdevice cmd line option */
423 USBDevice *usbdevice_create(const char *cmdline)
425 USBBus *bus = usb_bus_find(-1 /* any */);
426 DeviceInfo *info;
427 USBDeviceInfo *usb;
428 char driver[32];
429 const char *params;
430 int len;
432 params = strchr(cmdline,':');
433 if (params) {
434 params++;
435 len = params - cmdline;
436 if (len > sizeof(driver))
437 len = sizeof(driver);
438 pstrcpy(driver, len, cmdline);
439 } else {
440 params = "";
441 pstrcpy(driver, sizeof(driver), cmdline);
444 for (info = device_info_list; info != NULL; info = info->next) {
445 if (info->bus_info != &usb_bus_info)
446 continue;
447 usb = DO_UPCAST(USBDeviceInfo, qdev, info);
448 if (usb->usbdevice_name == NULL)
449 continue;
450 if (strcmp(usb->usbdevice_name, driver) != 0)
451 continue;
452 break;
454 if (info == NULL) {
455 #if 0
456 /* no error because some drivers are not converted (yet) */
457 error_report("usbdevice %s not found", driver);
458 #endif
459 return NULL;
462 if (!usb->usbdevice_init) {
463 if (*params) {
464 error_report("usbdevice %s accepts no params", driver);
465 return NULL;
467 return usb_create_simple(bus, usb->qdev.name);
469 return usb->usbdevice_init(params);