Remove bogus error message from qemu_opts_set()
[qemu/aliguori-queue.git] / hw / qdev.c
blobb2d11cebba64341faa0fdbaabf98f2a01981a5bc
1 /*
2 * Dynamic device configuration and creation.
4 * Copyright (c) 2009 CodeSourcery
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 /* The theory here is that it should be possible to create a machine without
21 knowledge of specific devices. Historically board init routines have
22 passed a bunch of arguments to each device, requiring the board know
23 exactly which device it is dealing with. This file provides an abstract
24 API for device configuration and initialization. Devices will generally
25 inherit from a particular bus (e.g. PCI or I2C) rather than
26 this API directly. */
28 #include "net.h"
29 #include "qdev.h"
30 #include "sysemu.h"
31 #include "monitor.h"
33 /* This is a nasty hack to allow passing a NULL bus to qdev_create. */
34 static int qdev_hotplug = 0;
36 static BusState *main_system_bus;
38 static DeviceInfo *device_info_list;
40 static BusState *qbus_find_recursive(BusState *bus, const char *name,
41 const BusInfo *info);
42 static BusState *qbus_find(const char *path);
44 /* Register a new device type. */
45 void qdev_register(DeviceInfo *info)
47 assert(info->size >= sizeof(DeviceState));
48 assert(!info->next);
50 info->next = device_info_list;
51 device_info_list = info;
54 static DeviceInfo *qdev_find_info(BusInfo *bus_info, const char *name)
56 DeviceInfo *info;
58 /* first check device names */
59 for (info = device_info_list; info != NULL; info = info->next) {
60 if (bus_info && info->bus_info != bus_info)
61 continue;
62 if (strcmp(info->name, name) != 0)
63 continue;
64 return info;
67 /* failing that check the aliases */
68 for (info = device_info_list; info != NULL; info = info->next) {
69 if (bus_info && info->bus_info != bus_info)
70 continue;
71 if (!info->alias)
72 continue;
73 if (strcmp(info->alias, name) != 0)
74 continue;
75 return info;
77 return NULL;
80 /* Create a new device. This only initializes the device state structure
81 and allows properties to be set. qdev_init should be called to
82 initialize the actual device emulation. */
83 DeviceState *qdev_create(BusState *bus, const char *name)
85 DeviceInfo *info;
86 DeviceState *dev;
88 if (!bus) {
89 if (!main_system_bus) {
90 main_system_bus = qbus_create(&system_bus_info, NULL, "main-system-bus");
92 bus = main_system_bus;
95 info = qdev_find_info(bus->info, name);
96 if (!info) {
97 hw_error("Unknown device '%s' for bus '%s'\n", name, bus->info->name);
100 dev = qemu_mallocz(info->size);
101 dev->info = info;
102 dev->parent_bus = bus;
103 qdev_prop_set_defaults(dev, dev->info->props);
104 qdev_prop_set_defaults(dev, dev->parent_bus->info->props);
105 qdev_prop_set_compat(dev);
106 QLIST_INSERT_HEAD(&bus->children, dev, sibling);
107 if (qdev_hotplug) {
108 assert(bus->allow_hotplug);
109 dev->hotplugged = 1;
111 dev->state = DEV_STATE_CREATED;
112 return dev;
115 static int qdev_print_devinfo(DeviceInfo *info, char *dest, int len)
117 int pos = 0;
118 int ret;
120 ret = snprintf(dest+pos, len-pos, "name \"%s\", bus %s",
121 info->name, info->bus_info->name);
122 pos += MIN(len-pos,ret);
123 if (info->alias) {
124 ret = snprintf(dest+pos, len-pos, ", alias \"%s\"", info->alias);
125 pos += MIN(len-pos,ret);
127 if (info->desc) {
128 ret = snprintf(dest+pos, len-pos, ", desc \"%s\"", info->desc);
129 pos += MIN(len-pos,ret);
131 if (info->no_user) {
132 ret = snprintf(dest+pos, len-pos, ", no-user");
133 pos += MIN(len-pos,ret);
135 return pos;
138 static int set_property(const char *name, const char *value, void *opaque)
140 DeviceState *dev = opaque;
142 if (strcmp(name, "driver") == 0)
143 return 0;
144 if (strcmp(name, "bus") == 0)
145 return 0;
147 if (qdev_prop_parse(dev, name, value) == -1) {
148 qemu_error("can't set property \"%s\" to \"%s\" for \"%s\"\n",
149 name, value, dev->info->name);
150 return -1;
152 return 0;
155 DeviceState *qdev_device_add(QemuOpts *opts)
157 const char *driver, *path, *id;
158 DeviceInfo *info;
159 DeviceState *qdev;
160 BusState *bus;
162 driver = qemu_opt_get(opts, "driver");
163 if (!driver) {
164 qemu_error("-device: no driver specified\n");
165 return NULL;
167 if (strcmp(driver, "?") == 0) {
168 char msg[256];
169 for (info = device_info_list; info != NULL; info = info->next) {
170 qdev_print_devinfo(info, msg, sizeof(msg));
171 qemu_error("%s\n", msg);
173 return NULL;
176 /* find driver */
177 info = qdev_find_info(NULL, driver);
178 if (!info) {
179 qemu_error("Device \"%s\" not found. Try -device '?' for a list.\n",
180 driver);
181 return NULL;
183 if (info->no_user) {
184 qemu_error("device \"%s\" can't be added via command line\n",
185 info->name);
186 return NULL;
189 /* find bus */
190 path = qemu_opt_get(opts, "bus");
191 if (path != NULL) {
192 bus = qbus_find(path);
193 } else {
194 bus = qbus_find_recursive(main_system_bus, NULL, info->bus_info);
196 if (!bus) {
197 qemu_error("Did not find %s bus for %s\n",
198 path ? path : info->bus_info->name, info->name);
199 return NULL;
201 if (qdev_hotplug && !bus->allow_hotplug) {
202 qemu_error("Bus %s does not support hotplugging\n",
203 bus->name);
204 return NULL;
207 /* create device, set properties */
208 qdev = qdev_create(bus, driver);
209 id = qemu_opts_id(opts);
210 if (id) {
211 qdev->id = id;
213 if (qemu_opt_foreach(opts, set_property, qdev, 1) != 0) {
214 qdev_free(qdev);
215 return NULL;
217 if (qdev_init(qdev) != 0) {
218 qemu_error("Error initializing device %s\n", driver);
219 qdev_free(qdev);
220 return NULL;
222 qdev->opts = opts;
223 return qdev;
226 static void qdev_reset(void *opaque)
228 DeviceState *dev = opaque;
229 if (dev->info->reset)
230 dev->info->reset(dev);
233 /* Initialize a device. Device properties should be set before calling
234 this function. IRQs and MMIO regions should be connected/mapped after
235 calling this function. */
236 int qdev_init(DeviceState *dev)
238 int rc;
240 assert(dev->state == DEV_STATE_CREATED);
241 rc = dev->info->init(dev, dev->info);
242 if (rc < 0)
243 return rc;
244 qemu_register_reset(qdev_reset, dev);
245 if (dev->info->vmsd)
246 vmstate_register(-1, dev->info->vmsd, dev);
247 dev->state = DEV_STATE_INITIALIZED;
248 return 0;
251 int qdev_unplug(DeviceState *dev)
253 if (!dev->parent_bus->allow_hotplug) {
254 qemu_error("Bus %s does not support hotplugging\n",
255 dev->parent_bus->name);
256 return -1;
258 return dev->info->unplug(dev);
261 /* can be used as ->unplug() callback for the simple cases */
262 int qdev_simple_unplug_cb(DeviceState *dev)
264 /* just zap it */
265 qdev_free(dev);
266 return 0;
269 /* Unlink device from bus and free the structure. */
270 void qdev_free(DeviceState *dev)
272 BusState *bus;
274 if (dev->state == DEV_STATE_INITIALIZED) {
275 while (dev->num_child_bus) {
276 bus = QLIST_FIRST(&dev->child_bus);
277 qbus_free(bus);
279 #if 0 /* FIXME: need sane vmstate_unregister function */
280 if (dev->info->vmsd)
281 vmstate_unregister(dev->info->vmsd, dev);
282 #endif
283 if (dev->info->exit)
284 dev->info->exit(dev);
285 if (dev->opts)
286 qemu_opts_del(dev->opts);
288 qemu_unregister_reset(qdev_reset, dev);
289 QLIST_REMOVE(dev, sibling);
290 qemu_free(dev);
293 void qdev_machine_creation_done(void)
296 * ok, initial machine setup is done, starting from now we can
297 * only create hotpluggable devices
299 qdev_hotplug = 1;
302 /* Get a character (serial) device interface. */
303 CharDriverState *qdev_init_chardev(DeviceState *dev)
305 static int next_serial;
306 static int next_virtconsole;
307 /* FIXME: This is a nasty hack that needs to go away. */
308 if (strncmp(dev->info->name, "virtio", 6) == 0) {
309 return virtcon_hds[next_virtconsole++];
310 } else {
311 return serial_hds[next_serial++];
315 BusState *qdev_get_parent_bus(DeviceState *dev)
317 return dev->parent_bus;
320 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
322 assert(dev->num_gpio_in == 0);
323 dev->num_gpio_in = n;
324 dev->gpio_in = qemu_allocate_irqs(handler, dev, n);
327 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
329 assert(dev->num_gpio_out == 0);
330 dev->num_gpio_out = n;
331 dev->gpio_out = pins;
334 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
336 assert(n >= 0 && n < dev->num_gpio_in);
337 return dev->gpio_in[n];
340 void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
342 assert(n >= 0 && n < dev->num_gpio_out);
343 dev->gpio_out[n] = pin;
346 VLANClientState *qdev_get_vlan_client(DeviceState *dev,
347 NetCanReceive *can_receive,
348 NetReceive *receive,
349 NetReceiveIOV *receive_iov,
350 NetCleanup *cleanup,
351 void *opaque)
353 NICInfo *nd = dev->nd;
354 assert(nd);
355 nd->vc = qemu_new_vlan_client(nd->vlan, nd->model, nd->name, can_receive,
356 receive, receive_iov, cleanup, opaque);
357 return nd->vc;
361 void qdev_get_macaddr(DeviceState *dev, uint8_t *macaddr)
363 memcpy(macaddr, dev->nd->macaddr, 6);
366 static int next_block_unit[IF_COUNT];
368 /* Get a block device. This should only be used for single-drive devices
369 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
370 appropriate bus. */
371 BlockDriverState *qdev_init_bdrv(DeviceState *dev, BlockInterfaceType type)
373 int unit = next_block_unit[type]++;
374 DriveInfo *dinfo;
376 dinfo = drive_get(type, 0, unit);
377 return dinfo ? dinfo->bdrv : NULL;
380 BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
382 BusState *bus;
384 QLIST_FOREACH(bus, &dev->child_bus, sibling) {
385 if (strcmp(name, bus->name) == 0) {
386 return bus;
389 return NULL;
392 static BusState *qbus_find_recursive(BusState *bus, const char *name,
393 const BusInfo *info)
395 DeviceState *dev;
396 BusState *child, *ret;
397 int match = 1;
399 if (name && (strcmp(bus->name, name) != 0)) {
400 match = 0;
402 if (info && (bus->info != info)) {
403 match = 0;
405 if (match) {
406 return bus;
409 QLIST_FOREACH(dev, &bus->children, sibling) {
410 QLIST_FOREACH(child, &dev->child_bus, sibling) {
411 ret = qbus_find_recursive(child, name, info);
412 if (ret) {
413 return ret;
417 return NULL;
420 static DeviceState *qdev_find_recursive(BusState *bus, const char *id)
422 DeviceState *dev, *ret;
423 BusState *child;
425 QLIST_FOREACH(dev, &bus->children, sibling) {
426 if (dev->id && strcmp(dev->id, id) == 0)
427 return dev;
428 QLIST_FOREACH(child, &dev->child_bus, sibling) {
429 ret = qdev_find_recursive(child, id);
430 if (ret) {
431 return ret;
435 return NULL;
438 static void qbus_list_bus(DeviceState *dev, char *dest, int len)
440 BusState *child;
441 const char *sep = " ";
442 int pos = 0;
444 pos += snprintf(dest+pos, len-pos,"child busses at \"%s\":",
445 dev->id ? dev->id : dev->info->name);
446 QLIST_FOREACH(child, &dev->child_bus, sibling) {
447 pos += snprintf(dest+pos, len-pos, "%s\"%s\"", sep, child->name);
448 sep = ", ";
452 static void qbus_list_dev(BusState *bus, char *dest, int len)
454 DeviceState *dev;
455 const char *sep = " ";
456 int pos = 0;
458 pos += snprintf(dest+pos, len-pos, "devices at \"%s\":",
459 bus->name);
460 QLIST_FOREACH(dev, &bus->children, sibling) {
461 pos += snprintf(dest+pos, len-pos, "%s\"%s\"",
462 sep, dev->info->name);
463 if (dev->id)
464 pos += snprintf(dest+pos, len-pos, "/\"%s\"", dev->id);
465 sep = ", ";
469 static BusState *qbus_find_bus(DeviceState *dev, char *elem)
471 BusState *child;
473 QLIST_FOREACH(child, &dev->child_bus, sibling) {
474 if (strcmp(child->name, elem) == 0) {
475 return child;
478 return NULL;
481 static DeviceState *qbus_find_dev(BusState *bus, char *elem)
483 DeviceState *dev;
486 * try to match in order:
487 * (1) instance id, if present
488 * (2) driver name
489 * (3) driver alias, if present
491 QLIST_FOREACH(dev, &bus->children, sibling) {
492 if (dev->id && strcmp(dev->id, elem) == 0) {
493 return dev;
496 QLIST_FOREACH(dev, &bus->children, sibling) {
497 if (strcmp(dev->info->name, elem) == 0) {
498 return dev;
501 QLIST_FOREACH(dev, &bus->children, sibling) {
502 if (dev->info->alias && strcmp(dev->info->alias, elem) == 0) {
503 return dev;
506 return NULL;
509 static BusState *qbus_find(const char *path)
511 DeviceState *dev;
512 BusState *bus;
513 char elem[128], msg[256];
514 int pos, len;
516 /* find start element */
517 if (path[0] == '/') {
518 bus = main_system_bus;
519 pos = 0;
520 } else {
521 if (sscanf(path, "%127[^/]%n", elem, &len) != 1) {
522 qemu_error("path parse error (\"%s\")\n", path);
523 return NULL;
525 bus = qbus_find_recursive(main_system_bus, elem, NULL);
526 if (!bus) {
527 qemu_error("bus \"%s\" not found\n", elem);
528 return NULL;
530 pos = len;
533 for (;;) {
534 if (path[pos] == '\0') {
535 /* we are done */
536 return bus;
539 /* find device */
540 if (sscanf(path+pos, "/%127[^/]%n", elem, &len) != 1) {
541 qemu_error("path parse error (\"%s\" pos %d)\n", path, pos);
542 return NULL;
544 pos += len;
545 dev = qbus_find_dev(bus, elem);
546 if (!dev) {
547 qbus_list_dev(bus, msg, sizeof(msg));
548 qemu_error("device \"%s\" not found\n%s\n", elem, msg);
549 return NULL;
551 if (path[pos] == '\0') {
552 /* last specified element is a device. If it has exactly
553 * one child bus accept it nevertheless */
554 switch (dev->num_child_bus) {
555 case 0:
556 qemu_error("device has no child bus (%s)\n", path);
557 return NULL;
558 case 1:
559 return QLIST_FIRST(&dev->child_bus);
560 default:
561 qbus_list_bus(dev, msg, sizeof(msg));
562 qemu_error("device has multiple child busses (%s)\n%s\n",
563 path, msg);
564 return NULL;
568 /* find bus */
569 if (sscanf(path+pos, "/%127[^/]%n", elem, &len) != 1) {
570 qemu_error("path parse error (\"%s\" pos %d)\n", path, pos);
571 return NULL;
573 pos += len;
574 bus = qbus_find_bus(dev, elem);
575 if (!bus) {
576 qbus_list_bus(dev, msg, sizeof(msg));
577 qemu_error("child bus \"%s\" not found\n%s\n", elem, msg);
578 return NULL;
583 void qbus_create_inplace(BusState *bus, BusInfo *info,
584 DeviceState *parent, const char *name)
586 char *buf;
587 int i,len;
589 bus->info = info;
590 bus->parent = parent;
592 if (name) {
593 /* use supplied name */
594 bus->name = qemu_strdup(name);
595 } else if (parent && parent->id) {
596 /* parent device has id -> use it for bus name */
597 len = strlen(parent->id) + 16;
598 buf = qemu_malloc(len);
599 snprintf(buf, len, "%s.%d", parent->id, parent->num_child_bus);
600 bus->name = buf;
601 } else {
602 /* no id -> use lowercase bus type for bus name */
603 len = strlen(info->name) + 16;
604 buf = qemu_malloc(len);
605 len = snprintf(buf, len, "%s.%d", info->name,
606 parent ? parent->num_child_bus : 0);
607 for (i = 0; i < len; i++)
608 buf[i] = qemu_tolower(buf[i]);
609 bus->name = buf;
612 QLIST_INIT(&bus->children);
613 if (parent) {
614 QLIST_INSERT_HEAD(&parent->child_bus, bus, sibling);
615 parent->num_child_bus++;
620 BusState *qbus_create(BusInfo *info, DeviceState *parent, const char *name)
622 BusState *bus;
624 bus = qemu_mallocz(info->size);
625 bus->qdev_allocated = 1;
626 qbus_create_inplace(bus, info, parent, name);
627 return bus;
630 void qbus_free(BusState *bus)
632 DeviceState *dev;
634 while ((dev = QLIST_FIRST(&bus->children)) != NULL) {
635 qdev_free(dev);
637 if (bus->parent) {
638 QLIST_REMOVE(bus, sibling);
639 bus->parent->num_child_bus--;
641 if (bus->qdev_allocated) {
642 qemu_free(bus);
646 #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
647 static void qbus_print(Monitor *mon, BusState *bus, int indent);
649 static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props,
650 const char *prefix, int indent)
652 char buf[64];
654 if (!props)
655 return;
656 while (props->name) {
657 if (props->info->print) {
658 props->info->print(dev, props, buf, sizeof(buf));
659 qdev_printf("%s-prop: %s = %s\n", prefix, props->name, buf);
661 props++;
665 static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
667 BusState *child;
668 qdev_printf("dev: %s, id \"%s\"\n", dev->info->name,
669 dev->id ? dev->id : "");
670 indent += 2;
671 if (dev->num_gpio_in) {
672 qdev_printf("gpio-in %d\n", dev->num_gpio_in);
674 if (dev->num_gpio_out) {
675 qdev_printf("gpio-out %d\n", dev->num_gpio_out);
677 qdev_print_props(mon, dev, dev->info->props, "dev", indent);
678 qdev_print_props(mon, dev, dev->parent_bus->info->props, "bus", indent);
679 if (dev->parent_bus->info->print_dev)
680 dev->parent_bus->info->print_dev(mon, dev, indent);
681 QLIST_FOREACH(child, &dev->child_bus, sibling) {
682 qbus_print(mon, child, indent);
686 static void qbus_print(Monitor *mon, BusState *bus, int indent)
688 struct DeviceState *dev;
690 qdev_printf("bus: %s\n", bus->name);
691 indent += 2;
692 qdev_printf("type %s\n", bus->info->name);
693 QLIST_FOREACH(dev, &bus->children, sibling) {
694 qdev_print(mon, dev, indent);
697 #undef qdev_printf
699 void do_info_qtree(Monitor *mon)
701 if (main_system_bus)
702 qbus_print(mon, main_system_bus, 0);
705 void do_info_qdm(Monitor *mon)
707 DeviceInfo *info;
708 char msg[256];
710 for (info = device_info_list; info != NULL; info = info->next) {
711 qdev_print_devinfo(info, msg, sizeof(msg));
712 monitor_printf(mon, "%s\n", msg);
716 void do_device_add(Monitor *mon, const QDict *qdict)
718 QemuOpts *opts;
720 opts = qemu_opts_parse(&qemu_device_opts,
721 qdict_get_str(qdict, "config"), "driver");
722 if (opts)
723 qdev_device_add(opts);
726 void do_device_del(Monitor *mon, const QDict *qdict)
728 const char *id = qdict_get_str(qdict, "id");
729 DeviceState *dev;
731 dev = qdev_find_recursive(main_system_bus, id);
732 if (NULL == dev) {
733 qemu_error("Device '%s' not found\n", id);
734 return;
736 qdev_unplug(dev);