]> git.proxmox.com Git - qemu.git/blame - hw/usb-bus.c
usb: Proper error propagation for usb_device_attach errors
[qemu.git] / hw / usb-bus.c
CommitLineData
806b6024
GH
1#include "hw.h"
2#include "usb.h"
3#include "qdev.h"
a5d2f727
GH
4#include "sysemu.h"
5#include "monitor.h"
6
7static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent);
c7a2196a
GH
8
9static char *usb_get_dev_path(DeviceState *dev);
70d31cb2 10static char *usb_get_fw_dev_path(DeviceState *qdev);
806b6024
GH
11
12static struct BusInfo usb_bus_info = {
a5d2f727
GH
13 .name = "USB",
14 .size = sizeof(USBBus),
15 .print_dev = usb_bus_dev_print,
c7a2196a 16 .get_dev_path = usb_get_dev_path,
70d31cb2 17 .get_fw_dev_path = usb_get_fw_dev_path,
5f69076b
GH
18 .props = (Property[]) {
19 DEFINE_PROP_STRING("port", USBDevice, port_path),
20 DEFINE_PROP_END_OF_LIST()
21 },
806b6024
GH
22};
23static int next_usb_bus = 0;
72cf2d4f 24static QTAILQ_HEAD(, USBBus) busses = QTAILQ_HEAD_INITIALIZER(busses);
806b6024 25
c1ecb40a
GH
26const VMStateDescription vmstate_usb_device = {
27 .name = "USBDevice",
28 .version_id = 1,
29 .minimum_version_id = 1,
30 .fields = (VMStateField []) {
31 VMSTATE_UINT8(addr, USBDevice),
32 VMSTATE_INT32(state, USBDevice),
33 VMSTATE_INT32(remote_wakeup, USBDevice),
34 VMSTATE_INT32(setup_state, USBDevice),
35 VMSTATE_INT32(setup_len, USBDevice),
36 VMSTATE_INT32(setup_index, USBDevice),
37 VMSTATE_UINT8_ARRAY(setup_buf, USBDevice, 8),
38 VMSTATE_END_OF_LIST(),
39 }
40};
41
07771f6f 42void usb_bus_new(USBBus *bus, USBBusOps *ops, DeviceState *host)
806b6024 43{
b2317837 44 qbus_create_inplace(&bus->qbus, &usb_bus_info, host, NULL);
07771f6f 45 bus->ops = ops;
806b6024 46 bus->busnr = next_usb_bus++;
ef816d83 47 bus->qbus.allow_hotplug = 1; /* Yes, we can */
72cf2d4f
BS
48 QTAILQ_INIT(&bus->free);
49 QTAILQ_INIT(&bus->used);
50 QTAILQ_INSERT_TAIL(&busses, bus, next);
806b6024
GH
51}
52
53USBBus *usb_bus_find(int busnr)
54{
55 USBBus *bus;
56
57 if (-1 == busnr)
72cf2d4f
BS
58 return QTAILQ_FIRST(&busses);
59 QTAILQ_FOREACH(bus, &busses, next) {
806b6024
GH
60 if (bus->busnr == busnr)
61 return bus;
62 }
63 return NULL;
64}
65
66static int usb_qdev_init(DeviceState *qdev, DeviceInfo *base)
67{
68 USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev);
69 USBDeviceInfo *info = DO_UPCAST(USBDeviceInfo, qdev, base);
70 int rc;
71
06384698 72 pstrcpy(dev->product_desc, sizeof(dev->product_desc), info->product_desc);
806b6024 73 dev->info = info;
61e094c0 74 dev->auto_attach = 1;
132a3f55 75 QLIST_INIT(&dev->strings);
806b6024 76 rc = dev->info->init(dev);
61e094c0 77 if (rc == 0 && dev->auto_attach)
fa19bf83 78 rc = usb_device_attach(dev);
806b6024
GH
79 return rc;
80}
81
a8e662b5
GH
82static int usb_qdev_exit(DeviceState *qdev)
83{
84 USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev);
07771f6f 85 USBBus *bus = usb_bus_from_device(dev);
a8e662b5 86
290a5c60
HG
87 if (dev->attached) {
88 usb_device_detach(dev);
89 }
07771f6f 90 bus->ops->device_destroy(bus, dev);
a8e662b5
GH
91 if (dev->info->handle_destroy) {
92 dev->info->handle_destroy(dev);
93 }
94 return 0;
95}
96
806b6024
GH
97void usb_qdev_register(USBDeviceInfo *info)
98{
99 info->qdev.bus_info = &usb_bus_info;
100 info->qdev.init = usb_qdev_init;
ef816d83 101 info->qdev.unplug = qdev_simple_unplug_cb;
a8e662b5 102 info->qdev.exit = usb_qdev_exit;
806b6024
GH
103 qdev_register(&info->qdev);
104}
105
106void usb_qdev_register_many(USBDeviceInfo *info)
107{
108 while (info->qdev.name) {
109 usb_qdev_register(info);
110 info++;
111 }
112}
113
a5d2f727 114USBDevice *usb_create(USBBus *bus, const char *name)
806b6024
GH
115{
116 DeviceState *dev;
117
118#if 1
119 /* temporary stopgap until all usb is properly qdev-ified */
120 if (!bus) {
121 bus = usb_bus_find(-1);
122 if (!bus)
123 return NULL;
fa19bf83 124 error_report("%s: no bus specified, using \"%s\" for \"%s\"\n",
806b6024
GH
125 __FUNCTION__, bus->qbus.name, name);
126 }
127#endif
128
129 dev = qdev_create(&bus->qbus, name);
806b6024
GH
130 return DO_UPCAST(USBDevice, qdev, dev);
131}
a5d2f727
GH
132
133USBDevice *usb_create_simple(USBBus *bus, const char *name)
134{
135 USBDevice *dev = usb_create(bus, name);
d44168ff
PB
136 if (!dev) {
137 hw_error("Failed to create USB device '%s'\n", name);
138 }
e23a1b33 139 qdev_init_nofail(&dev->qdev);
a5d2f727
GH
140 return dev;
141}
142
143void usb_register_port(USBBus *bus, USBPort *port, void *opaque, int index,
ace1318b 144 USBPortOps *ops, int speedmask)
a5d2f727
GH
145{
146 port->opaque = opaque;
147 port->index = index;
0d86d2be
GH
148 port->opaque = opaque;
149 port->index = index;
150 port->ops = ops;
843d4e0c 151 port->speedmask = speedmask;
72cf2d4f 152 QTAILQ_INSERT_TAIL(&bus->free, port, next);
a5d2f727
GH
153 bus->nfree++;
154}
155
c7a2196a
GH
156void usb_port_location(USBPort *downstream, USBPort *upstream, int portnr)
157{
158 if (upstream) {
159 snprintf(downstream->path, sizeof(downstream->path), "%s.%d",
160 upstream->path, portnr);
161 } else {
162 snprintf(downstream->path, sizeof(downstream->path), "%d", portnr);
163 }
164}
165
a8e662b5
GH
166void usb_unregister_port(USBBus *bus, USBPort *port)
167{
168 if (port->dev)
169 qdev_free(&port->dev->qdev);
170 QTAILQ_REMOVE(&bus->free, port, next);
171 bus->nfree--;
172}
173
fa19bf83 174static int do_attach(USBDevice *dev)
a5d2f727
GH
175{
176 USBBus *bus = usb_bus_from_device(dev);
177 USBPort *port;
178
179 if (dev->attached) {
fa19bf83 180 error_report("Error: tried to attach usb device %s twice\n",
0fe6d12e 181 dev->product_desc);
fa19bf83 182 return -1;
a5d2f727 183 }
ee210d63 184 if (bus->nfree == 0) {
fa19bf83 185 error_report("Error: tried to attach usb device %s to a bus with no free ports\n",
ee210d63 186 dev->product_desc);
fa19bf83 187 return -1;
ee210d63 188 }
5f69076b
GH
189 if (dev->port_path) {
190 QTAILQ_FOREACH(port, &bus->free, next) {
191 if (strcmp(port->path, dev->port_path) == 0) {
192 break;
193 }
194 }
195 if (port == NULL) {
fa19bf83 196 error_report("Error: usb port %s (bus %s) not found\n",
5f69076b 197 dev->port_path, bus->qbus.name);
fa19bf83 198 return -1;
5f69076b
GH
199 }
200 } else {
201 port = QTAILQ_FIRST(&bus->free);
202 }
a5d2f727 203
5f69076b 204 dev->attached++;
72cf2d4f 205 QTAILQ_REMOVE(&bus->free, port, next);
a5d2f727
GH
206 bus->nfree--;
207
208 usb_attach(port, dev);
209
72cf2d4f 210 QTAILQ_INSERT_TAIL(&bus->used, port, next);
a5d2f727 211 bus->nused++;
fa19bf83
HG
212
213 return 0;
a5d2f727
GH
214}
215
216int usb_device_attach(USBDevice *dev)
217{
218 USBBus *bus = usb_bus_from_device(dev);
a5d2f727 219
5f69076b
GH
220 if (bus->nfree == 1 && dev->port_path == NULL) {
221 /* Create a new hub and chain it on
222 (unless a physical port location is specified). */
d4c4e6fd 223 usb_create_simple(bus, "usb-hub");
a5d2f727 224 }
fa19bf83 225 return do_attach(dev);
a5d2f727
GH
226}
227
a8e662b5
GH
228int usb_device_detach(USBDevice *dev)
229{
230 USBBus *bus = usb_bus_from_device(dev);
231 USBPort *port;
232
233 if (!dev->attached) {
fa19bf83 234 error_report("Error: tried to detach unattached usb device %s\n",
0fe6d12e 235 dev->product_desc);
a8e662b5
GH
236 return -1;
237 }
238 dev->attached--;
239
240 QTAILQ_FOREACH(port, &bus->used, next) {
241 if (port->dev == dev)
242 break;
243 }
244 assert(port != NULL);
245
246 QTAILQ_REMOVE(&bus->used, port, next);
247 bus->nused--;
248
249 usb_attach(port, NULL);
250
251 QTAILQ_INSERT_TAIL(&bus->free, port, next);
252 bus->nfree++;
253 return 0;
254}
255
a5d2f727
GH
256int usb_device_delete_addr(int busnr, int addr)
257{
258 USBBus *bus;
259 USBPort *port;
260 USBDevice *dev;
261
262 bus = usb_bus_find(busnr);
263 if (!bus)
264 return -1;
265
72cf2d4f 266 QTAILQ_FOREACH(port, &bus->used, next) {
a5d2f727
GH
267 if (port->dev->addr == addr)
268 break;
269 }
270 if (!port)
271 return -1;
a5d2f727 272 dev = port->dev;
a5d2f727 273
a8e662b5 274 qdev_free(&dev->qdev);
a5d2f727
GH
275 return 0;
276}
277
278static const char *usb_speed(unsigned int speed)
279{
280 static const char *txt[] = {
281 [ USB_SPEED_LOW ] = "1.5",
282 [ USB_SPEED_FULL ] = "12",
283 [ USB_SPEED_HIGH ] = "480",
290d26d2 284 [ USB_SPEED_SUPER ] = "5000",
a5d2f727
GH
285 };
286 if (speed >= ARRAY_SIZE(txt))
287 return "?";
288 return txt[speed];
289}
290
291static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
292{
293 USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev);
294 USBBus *bus = usb_bus_from_device(dev);
295
c7a2196a 296 monitor_printf(mon, "%*saddr %d.%d, port %s, speed %s, name %s%s\n",
66a6593a 297 indent, "", bus->busnr, dev->addr,
c7a2196a 298 dev->port ? dev->port->path : "-",
0fe6d12e 299 usb_speed(dev->speed), dev->product_desc,
66a6593a 300 dev->attached ? ", attached" : "");
a5d2f727
GH
301}
302
c7a2196a
GH
303static char *usb_get_dev_path(DeviceState *qdev)
304{
305 USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev);
306 return qemu_strdup(dev->port->path);
307}
308
70d31cb2
GH
309static char *usb_get_fw_dev_path(DeviceState *qdev)
310{
311 USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev);
312 char *fw_path, *in;
ea87e95f 313 ssize_t pos = 0, fw_len;
70d31cb2
GH
314 long nr;
315
ea87e95f
BS
316 fw_len = 32 + strlen(dev->port->path) * 6;
317 fw_path = qemu_malloc(fw_len);
70d31cb2 318 in = dev->port->path;
ea87e95f 319 while (fw_len - pos > 0) {
70d31cb2
GH
320 nr = strtol(in, &in, 10);
321 if (in[0] == '.') {
322 /* some hub between root port and device */
ea87e95f 323 pos += snprintf(fw_path + pos, fw_len - pos, "hub@%ld/", nr);
70d31cb2
GH
324 in++;
325 } else {
326 /* the device itself */
ea87e95f
BS
327 pos += snprintf(fw_path + pos, fw_len - pos, "%s@%ld",
328 qdev_fw_name(qdev), nr);
70d31cb2
GH
329 break;
330 }
331 }
332 return fw_path;
333}
334
a5d2f727
GH
335void usb_info(Monitor *mon)
336{
337 USBBus *bus;
338 USBDevice *dev;
339 USBPort *port;
340
72cf2d4f 341 if (QTAILQ_EMPTY(&busses)) {
a5d2f727
GH
342 monitor_printf(mon, "USB support not enabled\n");
343 return;
344 }
345
72cf2d4f
BS
346 QTAILQ_FOREACH(bus, &busses, next) {
347 QTAILQ_FOREACH(port, &bus->used, next) {
a5d2f727
GH
348 dev = port->dev;
349 if (!dev)
350 continue;
c7a2196a
GH
351 monitor_printf(mon, " Device %d.%d, Port %s, Speed %s Mb/s, Product %s\n",
352 bus->busnr, dev->addr, port->path, usb_speed(dev->speed),
0fe6d12e 353 dev->product_desc);
a5d2f727
GH
354 }
355 }
356}
357
0958b4cc
GH
358/* handle legacy -usbdevice cmd line option */
359USBDevice *usbdevice_create(const char *cmdline)
360{
361 USBBus *bus = usb_bus_find(-1 /* any */);
362 DeviceInfo *info;
363 USBDeviceInfo *usb;
702f3e0f
JK
364 char driver[32];
365 const char *params;
0958b4cc
GH
366 int len;
367
368 params = strchr(cmdline,':');
369 if (params) {
370 params++;
371 len = params - cmdline;
372 if (len > sizeof(driver))
373 len = sizeof(driver);
374 pstrcpy(driver, len, cmdline);
375 } else {
702f3e0f 376 params = "";
0958b4cc
GH
377 pstrcpy(driver, sizeof(driver), cmdline);
378 }
379
380 for (info = device_info_list; info != NULL; info = info->next) {
381 if (info->bus_info != &usb_bus_info)
382 continue;
383 usb = DO_UPCAST(USBDeviceInfo, qdev, info);
384 if (usb->usbdevice_name == NULL)
385 continue;
386 if (strcmp(usb->usbdevice_name, driver) != 0)
387 continue;
388 break;
389 }
390 if (info == NULL) {
391#if 0
392 /* no error because some drivers are not converted (yet) */
1ecda02b 393 error_report("usbdevice %s not found", driver);
0958b4cc
GH
394#endif
395 return NULL;
396 }
397
398 if (!usb->usbdevice_init) {
98f22dc1 399 if (*params) {
1ecda02b 400 error_report("usbdevice %s accepts no params", driver);
0958b4cc
GH
401 return NULL;
402 }
403 return usb_create_simple(bus, usb->qdev.name);
404 }
405 return usb->usbdevice_init(params);
406}