qdev: integrate with QEMU Object Model (v2)
[qemu.git] / hw / usb-bus.c
1 #include "hw.h"
2 #include "usb.h"
3 #include "qdev.h"
4 #include "sysemu.h"
5 #include "monitor.h"
6 #include "trace.h"
7
8 static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent);
9
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);
13
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()
23 },
24 };
25 static int next_usb_bus = 0;
26 static QTAILQ_HEAD(, USBBus) busses = QTAILQ_HEAD_INITIALIZER(busses);
27
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(),
41 }
42 };
43
44 void usb_bus_new(USBBus *bus, USBBusOps *ops, DeviceState *host)
45 {
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);
53 }
54
55 USBBus *usb_bus_find(int busnr)
56 {
57 USBBus *bus;
58
59 if (-1 == busnr)
60 return QTAILQ_FIRST(&busses);
61 QTAILQ_FOREACH(bus, &busses, next) {
62 if (bus->busnr == busnr)
63 return bus;
64 }
65 return NULL;
66 }
67
68 static int usb_qdev_init(DeviceState *qdev, DeviceInfo *base)
69 {
70 USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev);
71 USBDeviceInfo *info = DO_UPCAST(USBDeviceInfo, qdev, base);
72 int rc;
73
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 usb_ep_init(dev);
79 rc = usb_claim_port(dev);
80 if (rc != 0) {
81 return rc;
82 }
83 rc = dev->info->init(dev);
84 if (rc != 0) {
85 usb_release_port(dev);
86 return rc;
87 }
88 if (dev->auto_attach) {
89 rc = usb_device_attach(dev);
90 if (rc != 0) {
91 usb_qdev_exit(qdev);
92 return rc;
93 }
94 }
95 return 0;
96 }
97
98 static int usb_qdev_exit(DeviceState *qdev)
99 {
100 USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev);
101
102 if (dev->attached) {
103 usb_device_detach(dev);
104 }
105 if (dev->info->handle_destroy) {
106 dev->info->handle_destroy(dev);
107 }
108 if (dev->port) {
109 usb_release_port(dev);
110 }
111 return 0;
112 }
113
114 void usb_qdev_register(USBDeviceInfo *info)
115 {
116 info->qdev.bus_info = &usb_bus_info;
117 info->qdev.init = usb_qdev_init;
118 info->qdev.unplug = qdev_simple_unplug_cb;
119 info->qdev.exit = usb_qdev_exit;
120 qdev_register(&info->qdev);
121 }
122
123 void usb_qdev_register_many(USBDeviceInfo *info)
124 {
125 while (info->qdev.name) {
126 usb_qdev_register(info);
127 info++;
128 }
129 }
130
131 USBDevice *usb_create(USBBus *bus, const char *name)
132 {
133 DeviceState *dev;
134
135 #if 1
136 /* temporary stopgap until all usb is properly qdev-ified */
137 if (!bus) {
138 bus = usb_bus_find(-1);
139 if (!bus)
140 return NULL;
141 error_report("%s: no bus specified, using \"%s\" for \"%s\"",
142 __FUNCTION__, bus->qbus.name, name);
143 }
144 #endif
145
146 dev = qdev_create(&bus->qbus, name);
147 return DO_UPCAST(USBDevice, qdev, dev);
148 }
149
150 USBDevice *usb_create_simple(USBBus *bus, const char *name)
151 {
152 USBDevice *dev = usb_create(bus, name);
153 int rc;
154
155 if (!dev) {
156 error_report("Failed to create USB device '%s'", name);
157 return NULL;
158 }
159 rc = qdev_init(&dev->qdev);
160 if (rc < 0) {
161 error_report("Failed to initialize USB device '%s'", name);
162 return NULL;
163 }
164 return dev;
165 }
166
167 static void usb_fill_port(USBPort *port, void *opaque, int index,
168 USBPortOps *ops, int speedmask)
169 {
170 port->opaque = opaque;
171 port->index = index;
172 port->ops = ops;
173 port->speedmask = speedmask;
174 usb_port_location(port, NULL, index + 1);
175 }
176
177 void usb_register_port(USBBus *bus, USBPort *port, void *opaque, int index,
178 USBPortOps *ops, int speedmask)
179 {
180 usb_fill_port(port, opaque, index, ops, speedmask);
181 QTAILQ_INSERT_TAIL(&bus->free, port, next);
182 bus->nfree++;
183 }
184
185 int usb_register_companion(const char *masterbus, USBPort *ports[],
186 uint32_t portcount, uint32_t firstport,
187 void *opaque, USBPortOps *ops, int speedmask)
188 {
189 USBBus *bus;
190 int i;
191
192 QTAILQ_FOREACH(bus, &busses, next) {
193 if (strcmp(bus->qbus.name, masterbus) == 0) {
194 break;
195 }
196 }
197
198 if (!bus || !bus->ops->register_companion) {
199 qerror_report(QERR_INVALID_PARAMETER_VALUE, "masterbus",
200 "an USB masterbus");
201 if (bus) {
202 error_printf_unless_qmp(
203 "USB bus '%s' does not allow companion controllers\n",
204 masterbus);
205 }
206 return -1;
207 }
208
209 for (i = 0; i < portcount; i++) {
210 usb_fill_port(ports[i], opaque, i, ops, speedmask);
211 }
212
213 return bus->ops->register_companion(bus, ports, portcount, firstport);
214 }
215
216 void usb_port_location(USBPort *downstream, USBPort *upstream, int portnr)
217 {
218 if (upstream) {
219 snprintf(downstream->path, sizeof(downstream->path), "%s.%d",
220 upstream->path, portnr);
221 } else {
222 snprintf(downstream->path, sizeof(downstream->path), "%d", portnr);
223 }
224 }
225
226 void usb_unregister_port(USBBus *bus, USBPort *port)
227 {
228 if (port->dev)
229 qdev_free(&port->dev->qdev);
230 QTAILQ_REMOVE(&bus->free, port, next);
231 bus->nfree--;
232 }
233
234 int usb_claim_port(USBDevice *dev)
235 {
236 USBBus *bus = usb_bus_from_device(dev);
237 USBPort *port;
238
239 assert(dev->port == NULL);
240
241 if (dev->port_path) {
242 QTAILQ_FOREACH(port, &bus->free, next) {
243 if (strcmp(port->path, dev->port_path) == 0) {
244 break;
245 }
246 }
247 if (port == NULL) {
248 error_report("Error: usb port %s (bus %s) not found (in use?)",
249 dev->port_path, bus->qbus.name);
250 return -1;
251 }
252 } else {
253 if (bus->nfree == 1 && strcmp(dev->qdev.info->name, "usb-hub") != 0) {
254 /* Create a new hub and chain it on */
255 usb_create_simple(bus, "usb-hub");
256 }
257 if (bus->nfree == 0) {
258 error_report("Error: tried to attach usb device %s to a bus "
259 "with no free ports", dev->product_desc);
260 return -1;
261 }
262 port = QTAILQ_FIRST(&bus->free);
263 }
264 trace_usb_port_claim(bus->busnr, port->path);
265
266 QTAILQ_REMOVE(&bus->free, port, next);
267 bus->nfree--;
268
269 dev->port = port;
270 port->dev = dev;
271
272 QTAILQ_INSERT_TAIL(&bus->used, port, next);
273 bus->nused++;
274 return 0;
275 }
276
277 void usb_release_port(USBDevice *dev)
278 {
279 USBBus *bus = usb_bus_from_device(dev);
280 USBPort *port = dev->port;
281
282 assert(port != NULL);
283 trace_usb_port_release(bus->busnr, port->path);
284
285 QTAILQ_REMOVE(&bus->used, port, next);
286 bus->nused--;
287
288 dev->port = NULL;
289 port->dev = NULL;
290
291 QTAILQ_INSERT_TAIL(&bus->free, port, next);
292 bus->nfree++;
293 }
294
295 int usb_device_attach(USBDevice *dev)
296 {
297 USBBus *bus = usb_bus_from_device(dev);
298 USBPort *port = dev->port;
299
300 assert(port != NULL);
301 assert(!dev->attached);
302 trace_usb_port_attach(bus->busnr, port->path);
303
304 if (!(port->speedmask & dev->speedmask)) {
305 error_report("Warning: speed mismatch trying to attach "
306 "usb device %s to bus %s",
307 dev->product_desc, bus->qbus.name);
308 return -1;
309 }
310
311 dev->attached++;
312 usb_attach(port);
313
314 return 0;
315 }
316
317 int usb_device_detach(USBDevice *dev)
318 {
319 USBBus *bus = usb_bus_from_device(dev);
320 USBPort *port = dev->port;
321
322 assert(port != NULL);
323 assert(dev->attached);
324 trace_usb_port_detach(bus->busnr, port->path);
325
326 usb_detach(port);
327 dev->attached--;
328 return 0;
329 }
330
331 int usb_device_delete_addr(int busnr, int addr)
332 {
333 USBBus *bus;
334 USBPort *port;
335 USBDevice *dev;
336
337 bus = usb_bus_find(busnr);
338 if (!bus)
339 return -1;
340
341 QTAILQ_FOREACH(port, &bus->used, next) {
342 if (port->dev->addr == addr)
343 break;
344 }
345 if (!port)
346 return -1;
347 dev = port->dev;
348
349 qdev_free(&dev->qdev);
350 return 0;
351 }
352
353 static const char *usb_speed(unsigned int speed)
354 {
355 static const char *txt[] = {
356 [ USB_SPEED_LOW ] = "1.5",
357 [ USB_SPEED_FULL ] = "12",
358 [ USB_SPEED_HIGH ] = "480",
359 [ USB_SPEED_SUPER ] = "5000",
360 };
361 if (speed >= ARRAY_SIZE(txt))
362 return "?";
363 return txt[speed];
364 }
365
366 static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
367 {
368 USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev);
369 USBBus *bus = usb_bus_from_device(dev);
370
371 monitor_printf(mon, "%*saddr %d.%d, port %s, speed %s, name %s%s\n",
372 indent, "", bus->busnr, dev->addr,
373 dev->port ? dev->port->path : "-",
374 usb_speed(dev->speed), dev->product_desc,
375 dev->attached ? ", attached" : "");
376 }
377
378 static char *usb_get_dev_path(DeviceState *qdev)
379 {
380 USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev);
381 return g_strdup(dev->port->path);
382 }
383
384 static char *usb_get_fw_dev_path(DeviceState *qdev)
385 {
386 USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev);
387 char *fw_path, *in;
388 ssize_t pos = 0, fw_len;
389 long nr;
390
391 fw_len = 32 + strlen(dev->port->path) * 6;
392 fw_path = g_malloc(fw_len);
393 in = dev->port->path;
394 while (fw_len - pos > 0) {
395 nr = strtol(in, &in, 10);
396 if (in[0] == '.') {
397 /* some hub between root port and device */
398 pos += snprintf(fw_path + pos, fw_len - pos, "hub@%ld/", nr);
399 in++;
400 } else {
401 /* the device itself */
402 pos += snprintf(fw_path + pos, fw_len - pos, "%s@%ld",
403 qdev_fw_name(qdev), nr);
404 break;
405 }
406 }
407 return fw_path;
408 }
409
410 void usb_info(Monitor *mon)
411 {
412 USBBus *bus;
413 USBDevice *dev;
414 USBPort *port;
415
416 if (QTAILQ_EMPTY(&busses)) {
417 monitor_printf(mon, "USB support not enabled\n");
418 return;
419 }
420
421 QTAILQ_FOREACH(bus, &busses, next) {
422 QTAILQ_FOREACH(port, &bus->used, next) {
423 dev = port->dev;
424 if (!dev)
425 continue;
426 monitor_printf(mon, " Device %d.%d, Port %s, Speed %s Mb/s, Product %s\n",
427 bus->busnr, dev->addr, port->path, usb_speed(dev->speed),
428 dev->product_desc);
429 }
430 }
431 }
432
433 /* handle legacy -usbdevice cmd line option */
434 USBDevice *usbdevice_create(const char *cmdline)
435 {
436 USBBus *bus = usb_bus_find(-1 /* any */);
437 DeviceInfo *info;
438 USBDeviceInfo *usb;
439 char driver[32];
440 const char *params;
441 int len;
442
443 params = strchr(cmdline,':');
444 if (params) {
445 params++;
446 len = params - cmdline;
447 if (len > sizeof(driver))
448 len = sizeof(driver);
449 pstrcpy(driver, len, cmdline);
450 } else {
451 params = "";
452 pstrcpy(driver, sizeof(driver), cmdline);
453 }
454
455 for (info = device_info_list; info != NULL; info = info->next) {
456 if (info->bus_info != &usb_bus_info)
457 continue;
458 usb = DO_UPCAST(USBDeviceInfo, qdev, info);
459 if (usb->usbdevice_name == NULL)
460 continue;
461 if (strcmp(usb->usbdevice_name, driver) != 0)
462 continue;
463 break;
464 }
465 if (info == NULL) {
466 #if 0
467 /* no error because some drivers are not converted (yet) */
468 error_report("usbdevice %s not found", driver);
469 #endif
470 return NULL;
471 }
472
473 if (!usb->usbdevice_init) {
474 if (*params) {
475 error_report("usbdevice %s accepts no params", driver);
476 return NULL;
477 }
478 return usb_create_simple(bus, usb->qdev.name);
479 }
480 return usb->usbdevice_init(params);
481 }