PPC: e500mc: add missing IVORs to bitmap
[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_device_init(USBDevice *dev)
69 {
70 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
71 if (klass->init) {
72 return klass->init(dev);
73 }
74 return 0;
75 }
76
77 static void usb_device_handle_destroy(USBDevice *dev)
78 {
79 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
80 if (klass->handle_destroy) {
81 klass->handle_destroy(dev);
82 }
83 }
84
85 int usb_device_handle_packet(USBDevice *dev, USBPacket *p)
86 {
87 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
88 if (klass->handle_packet) {
89 return klass->handle_packet(dev, p);
90 }
91 return -ENOSYS;
92 }
93
94 void usb_device_cancel_packet(USBDevice *dev, USBPacket *p)
95 {
96 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
97 if (klass->cancel_packet) {
98 klass->cancel_packet(dev, p);
99 }
100 }
101
102 void usb_device_handle_attach(USBDevice *dev)
103 {
104 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
105 if (klass->handle_attach) {
106 klass->handle_attach(dev);
107 }
108 }
109
110 void usb_device_handle_reset(USBDevice *dev)
111 {
112 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
113 if (klass->handle_reset) {
114 klass->handle_reset(dev);
115 }
116 }
117
118 int usb_device_handle_control(USBDevice *dev, USBPacket *p, int request,
119 int value, int index, int length, uint8_t *data)
120 {
121 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
122 if (klass->handle_control) {
123 return klass->handle_control(dev, p, request, value, index, length,
124 data);
125 }
126 return -ENOSYS;
127 }
128
129 int usb_device_handle_data(USBDevice *dev, USBPacket *p)
130 {
131 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
132 if (klass->handle_data) {
133 return klass->handle_data(dev, p);
134 }
135 return -ENOSYS;
136 }
137
138 const char *usb_device_get_product_desc(USBDevice *dev)
139 {
140 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
141 return klass->product_desc;
142 }
143
144 const USBDesc *usb_device_get_usb_desc(USBDevice *dev)
145 {
146 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
147 return klass->usb_desc;
148 }
149
150 void usb_device_set_interface(USBDevice *dev, int interface,
151 int alt_old, int alt_new)
152 {
153 USBDeviceClass *klass = USB_DEVICE_GET_CLASS(dev);
154 if (klass->set_interface) {
155 klass->set_interface(dev, interface, alt_old, alt_new);
156 }
157 }
158
159 static int usb_qdev_init(DeviceState *qdev, DeviceInfo *base)
160 {
161 USBDevice *dev = USB_DEVICE(qdev);
162 int rc;
163
164 pstrcpy(dev->product_desc, sizeof(dev->product_desc),
165 usb_device_get_product_desc(dev));
166 dev->auto_attach = 1;
167 QLIST_INIT(&dev->strings);
168 usb_ep_init(dev);
169 rc = usb_claim_port(dev);
170 if (rc != 0) {
171 return rc;
172 }
173 rc = usb_device_init(dev);
174 if (rc != 0) {
175 usb_release_port(dev);
176 return rc;
177 }
178 if (dev->auto_attach) {
179 rc = usb_device_attach(dev);
180 if (rc != 0) {
181 usb_qdev_exit(qdev);
182 return rc;
183 }
184 }
185 return 0;
186 }
187
188 static int usb_qdev_exit(DeviceState *qdev)
189 {
190 USBDevice *dev = USB_DEVICE(qdev);
191
192 if (dev->attached) {
193 usb_device_detach(dev);
194 }
195 usb_device_handle_destroy(dev);
196 if (dev->port) {
197 usb_release_port(dev);
198 }
199 return 0;
200 }
201
202 typedef struct LegacyUSBFactory
203 {
204 const char *name;
205 const char *usbdevice_name;
206 USBDevice *(*usbdevice_init)(const char *params);
207 } LegacyUSBFactory;
208
209 static GSList *legacy_usb_factory;
210
211 void usb_qdev_register(DeviceInfo *info,
212 const char *usbdevice_name,
213 USBDevice *(*usbdevice_init)(const char *params))
214 {
215 info->bus_info = &usb_bus_info;
216 info->init = usb_qdev_init;
217 info->unplug = qdev_simple_unplug_cb;
218 info->exit = usb_qdev_exit;
219 qdev_register_subclass(info, TYPE_USB_DEVICE);
220
221 if (usbdevice_name) {
222 LegacyUSBFactory *f = g_malloc0(sizeof(*f));
223 f->name = info->name;
224 f->usbdevice_name = usbdevice_name;
225 f->usbdevice_init = usbdevice_init;
226 legacy_usb_factory = g_slist_append(legacy_usb_factory, f);
227 }
228 }
229
230 USBDevice *usb_create(USBBus *bus, const char *name)
231 {
232 DeviceState *dev;
233
234 #if 1
235 /* temporary stopgap until all usb is properly qdev-ified */
236 if (!bus) {
237 bus = usb_bus_find(-1);
238 if (!bus)
239 return NULL;
240 error_report("%s: no bus specified, using \"%s\" for \"%s\"",
241 __FUNCTION__, bus->qbus.name, name);
242 }
243 #endif
244
245 dev = qdev_create(&bus->qbus, name);
246 return USB_DEVICE(dev);
247 }
248
249 USBDevice *usb_create_simple(USBBus *bus, const char *name)
250 {
251 USBDevice *dev = usb_create(bus, name);
252 int rc;
253
254 if (!dev) {
255 error_report("Failed to create USB device '%s'", name);
256 return NULL;
257 }
258 rc = qdev_init(&dev->qdev);
259 if (rc < 0) {
260 error_report("Failed to initialize USB device '%s'", name);
261 return NULL;
262 }
263 return dev;
264 }
265
266 static void usb_fill_port(USBPort *port, void *opaque, int index,
267 USBPortOps *ops, int speedmask)
268 {
269 port->opaque = opaque;
270 port->index = index;
271 port->ops = ops;
272 port->speedmask = speedmask;
273 usb_port_location(port, NULL, index + 1);
274 }
275
276 void usb_register_port(USBBus *bus, USBPort *port, void *opaque, int index,
277 USBPortOps *ops, int speedmask)
278 {
279 usb_fill_port(port, opaque, index, ops, speedmask);
280 QTAILQ_INSERT_TAIL(&bus->free, port, next);
281 bus->nfree++;
282 }
283
284 int usb_register_companion(const char *masterbus, USBPort *ports[],
285 uint32_t portcount, uint32_t firstport,
286 void *opaque, USBPortOps *ops, int speedmask)
287 {
288 USBBus *bus;
289 int i;
290
291 QTAILQ_FOREACH(bus, &busses, next) {
292 if (strcmp(bus->qbus.name, masterbus) == 0) {
293 break;
294 }
295 }
296
297 if (!bus || !bus->ops->register_companion) {
298 qerror_report(QERR_INVALID_PARAMETER_VALUE, "masterbus",
299 "an USB masterbus");
300 if (bus) {
301 error_printf_unless_qmp(
302 "USB bus '%s' does not allow companion controllers\n",
303 masterbus);
304 }
305 return -1;
306 }
307
308 for (i = 0; i < portcount; i++) {
309 usb_fill_port(ports[i], opaque, i, ops, speedmask);
310 }
311
312 return bus->ops->register_companion(bus, ports, portcount, firstport);
313 }
314
315 void usb_port_location(USBPort *downstream, USBPort *upstream, int portnr)
316 {
317 if (upstream) {
318 snprintf(downstream->path, sizeof(downstream->path), "%s.%d",
319 upstream->path, portnr);
320 } else {
321 snprintf(downstream->path, sizeof(downstream->path), "%d", portnr);
322 }
323 }
324
325 void usb_unregister_port(USBBus *bus, USBPort *port)
326 {
327 if (port->dev)
328 qdev_free(&port->dev->qdev);
329 QTAILQ_REMOVE(&bus->free, port, next);
330 bus->nfree--;
331 }
332
333 int usb_claim_port(USBDevice *dev)
334 {
335 USBBus *bus = usb_bus_from_device(dev);
336 USBPort *port;
337
338 assert(dev->port == NULL);
339
340 if (dev->port_path) {
341 QTAILQ_FOREACH(port, &bus->free, next) {
342 if (strcmp(port->path, dev->port_path) == 0) {
343 break;
344 }
345 }
346 if (port == NULL) {
347 error_report("Error: usb port %s (bus %s) not found (in use?)",
348 dev->port_path, bus->qbus.name);
349 return -1;
350 }
351 } else {
352 if (bus->nfree == 1 && strcmp(object_get_typename(OBJECT(dev)), "usb-hub") != 0) {
353 /* Create a new hub and chain it on */
354 usb_create_simple(bus, "usb-hub");
355 }
356 if (bus->nfree == 0) {
357 error_report("Error: tried to attach usb device %s to a bus "
358 "with no free ports", dev->product_desc);
359 return -1;
360 }
361 port = QTAILQ_FIRST(&bus->free);
362 }
363 trace_usb_port_claim(bus->busnr, port->path);
364
365 QTAILQ_REMOVE(&bus->free, port, next);
366 bus->nfree--;
367
368 dev->port = port;
369 port->dev = dev;
370
371 QTAILQ_INSERT_TAIL(&bus->used, port, next);
372 bus->nused++;
373 return 0;
374 }
375
376 void usb_release_port(USBDevice *dev)
377 {
378 USBBus *bus = usb_bus_from_device(dev);
379 USBPort *port = dev->port;
380
381 assert(port != NULL);
382 trace_usb_port_release(bus->busnr, port->path);
383
384 QTAILQ_REMOVE(&bus->used, port, next);
385 bus->nused--;
386
387 dev->port = NULL;
388 port->dev = NULL;
389
390 QTAILQ_INSERT_TAIL(&bus->free, port, next);
391 bus->nfree++;
392 }
393
394 int usb_device_attach(USBDevice *dev)
395 {
396 USBBus *bus = usb_bus_from_device(dev);
397 USBPort *port = dev->port;
398
399 assert(port != NULL);
400 assert(!dev->attached);
401 trace_usb_port_attach(bus->busnr, port->path);
402
403 if (!(port->speedmask & dev->speedmask)) {
404 error_report("Warning: speed mismatch trying to attach "
405 "usb device %s to bus %s",
406 dev->product_desc, bus->qbus.name);
407 return -1;
408 }
409
410 dev->attached++;
411 usb_attach(port);
412
413 return 0;
414 }
415
416 int usb_device_detach(USBDevice *dev)
417 {
418 USBBus *bus = usb_bus_from_device(dev);
419 USBPort *port = dev->port;
420
421 assert(port != NULL);
422 assert(dev->attached);
423 trace_usb_port_detach(bus->busnr, port->path);
424
425 usb_detach(port);
426 dev->attached--;
427 return 0;
428 }
429
430 int usb_device_delete_addr(int busnr, int addr)
431 {
432 USBBus *bus;
433 USBPort *port;
434 USBDevice *dev;
435
436 bus = usb_bus_find(busnr);
437 if (!bus)
438 return -1;
439
440 QTAILQ_FOREACH(port, &bus->used, next) {
441 if (port->dev->addr == addr)
442 break;
443 }
444 if (!port)
445 return -1;
446 dev = port->dev;
447
448 qdev_free(&dev->qdev);
449 return 0;
450 }
451
452 static const char *usb_speed(unsigned int speed)
453 {
454 static const char *txt[] = {
455 [ USB_SPEED_LOW ] = "1.5",
456 [ USB_SPEED_FULL ] = "12",
457 [ USB_SPEED_HIGH ] = "480",
458 [ USB_SPEED_SUPER ] = "5000",
459 };
460 if (speed >= ARRAY_SIZE(txt))
461 return "?";
462 return txt[speed];
463 }
464
465 static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
466 {
467 USBDevice *dev = USB_DEVICE(qdev);
468 USBBus *bus = usb_bus_from_device(dev);
469
470 monitor_printf(mon, "%*saddr %d.%d, port %s, speed %s, name %s%s\n",
471 indent, "", bus->busnr, dev->addr,
472 dev->port ? dev->port->path : "-",
473 usb_speed(dev->speed), dev->product_desc,
474 dev->attached ? ", attached" : "");
475 }
476
477 static char *usb_get_dev_path(DeviceState *qdev)
478 {
479 USBDevice *dev = USB_DEVICE(qdev);
480 return g_strdup(dev->port->path);
481 }
482
483 static char *usb_get_fw_dev_path(DeviceState *qdev)
484 {
485 USBDevice *dev = USB_DEVICE(qdev);
486 char *fw_path, *in;
487 ssize_t pos = 0, fw_len;
488 long nr;
489
490 fw_len = 32 + strlen(dev->port->path) * 6;
491 fw_path = g_malloc(fw_len);
492 in = dev->port->path;
493 while (fw_len - pos > 0) {
494 nr = strtol(in, &in, 10);
495 if (in[0] == '.') {
496 /* some hub between root port and device */
497 pos += snprintf(fw_path + pos, fw_len - pos, "hub@%ld/", nr);
498 in++;
499 } else {
500 /* the device itself */
501 pos += snprintf(fw_path + pos, fw_len - pos, "%s@%ld",
502 qdev_fw_name(qdev), nr);
503 break;
504 }
505 }
506 return fw_path;
507 }
508
509 void usb_info(Monitor *mon)
510 {
511 USBBus *bus;
512 USBDevice *dev;
513 USBPort *port;
514
515 if (QTAILQ_EMPTY(&busses)) {
516 monitor_printf(mon, "USB support not enabled\n");
517 return;
518 }
519
520 QTAILQ_FOREACH(bus, &busses, next) {
521 QTAILQ_FOREACH(port, &bus->used, next) {
522 dev = port->dev;
523 if (!dev)
524 continue;
525 monitor_printf(mon, " Device %d.%d, Port %s, Speed %s Mb/s, Product %s\n",
526 bus->busnr, dev->addr, port->path, usb_speed(dev->speed),
527 dev->product_desc);
528 }
529 }
530 }
531
532 /* handle legacy -usbdevice cmd line option */
533 USBDevice *usbdevice_create(const char *cmdline)
534 {
535 USBBus *bus = usb_bus_find(-1 /* any */);
536 LegacyUSBFactory *f = NULL;
537 GSList *i;
538 char driver[32];
539 const char *params;
540 int len;
541
542 params = strchr(cmdline,':');
543 if (params) {
544 params++;
545 len = params - cmdline;
546 if (len > sizeof(driver))
547 len = sizeof(driver);
548 pstrcpy(driver, len, cmdline);
549 } else {
550 params = "";
551 pstrcpy(driver, sizeof(driver), cmdline);
552 }
553
554 for (i = legacy_usb_factory; i; i = i->next) {
555 f = i->data;
556 if (strcmp(f->usbdevice_name, driver) == 0) {
557 break;
558 }
559 }
560 if (i == NULL) {
561 #if 0
562 /* no error because some drivers are not converted (yet) */
563 error_report("usbdevice %s not found", driver);
564 #endif
565 return NULL;
566 }
567
568 if (!f->usbdevice_init) {
569 if (*params) {
570 error_report("usbdevice %s accepts no params", driver);
571 return NULL;
572 }
573 return usb_create_simple(bus, f->name);
574 }
575 return f->usbdevice_init(params);
576 }
577
578 static TypeInfo usb_device_type_info = {
579 .name = TYPE_USB_DEVICE,
580 .parent = TYPE_DEVICE,
581 .instance_size = sizeof(USBDevice),
582 .abstract = true,
583 .class_size = sizeof(USBDeviceClass),
584 };
585
586 static void usb_register_devices(void)
587 {
588 type_register_static(&usb_device_type_info);
589 }
590
591 device_init(usb_register_devices);