qdev/scsi: add scsi bus support to qdev, convert drivers.
[qemu.git] / hw / qdev.c
blob0e9732b1ca22d828fbc48df6c522878bc56632ae
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 BusState *main_system_bus;
36 static DeviceInfo *device_info_list;
38 static BusState *qbus_find_recursive(BusState *bus, const char *name,
39 const BusInfo *info);
40 static BusState *qbus_find(const char *path);
42 /* Register a new device type. */
43 void qdev_register(DeviceInfo *info)
45 assert(info->size >= sizeof(DeviceState));
46 assert(!info->next);
48 info->next = device_info_list;
49 device_info_list = info;
52 static DeviceInfo *qdev_find_info(BusInfo *bus_info, const char *name)
54 DeviceInfo *info;
56 /* first check device names */
57 for (info = device_info_list; info != NULL; info = info->next) {
58 if (bus_info && info->bus_info != bus_info)
59 continue;
60 if (strcmp(info->name, name) != 0)
61 continue;
62 return info;
65 /* failing that check the aliases */
66 for (info = device_info_list; info != NULL; info = info->next) {
67 if (bus_info && info->bus_info != bus_info)
68 continue;
69 if (!info->alias)
70 continue;
71 if (strcmp(info->alias, name) != 0)
72 continue;
73 return info;
75 return NULL;
78 /* Create a new device. This only initializes the device state structure
79 and allows properties to be set. qdev_init should be called to
80 initialize the actual device emulation. */
81 DeviceState *qdev_create(BusState *bus, const char *name)
83 DeviceInfo *info;
84 DeviceState *dev;
86 if (!bus) {
87 if (!main_system_bus) {
88 main_system_bus = qbus_create(&system_bus_info, NULL, "main-system-bus");
90 bus = main_system_bus;
93 info = qdev_find_info(bus->info, name);
94 if (!info) {
95 hw_error("Unknown device '%s' for bus '%s'\n", name, bus->info->name);
98 dev = qemu_mallocz(info->size);
99 dev->info = info;
100 dev->parent_bus = bus;
101 qdev_prop_set_defaults(dev, dev->info->props);
102 qdev_prop_set_defaults(dev, dev->parent_bus->info->props);
103 qdev_prop_set_compat(dev);
104 LIST_INSERT_HEAD(&bus->children, dev, sibling);
105 return dev;
108 static int qdev_print_devinfo(DeviceInfo *info, char *dest, int len)
110 int pos = 0;
111 int ret;
113 ret = snprintf(dest+pos, len-pos, "name \"%s\", bus %s",
114 info->name, info->bus_info->name);
115 pos += MIN(len-pos,ret);
116 if (info->alias) {
117 ret = snprintf(dest+pos, len-pos, ", alias \"%s\"", info->alias);
118 pos += MIN(len-pos,ret);
120 if (info->desc) {
121 ret = snprintf(dest+pos, len-pos, ", desc \"%s\"", info->desc);
122 pos += MIN(len-pos,ret);
124 if (info->no_user) {
125 ret = snprintf(dest+pos, len-pos, ", no-user");
126 pos += MIN(len-pos,ret);
128 return pos;
131 static int set_property(const char *name, const char *value, void *opaque)
133 DeviceState *dev = opaque;
135 if (strcmp(name, "driver") == 0)
136 return 0;
137 if (strcmp(name, "bus") == 0)
138 return 0;
140 if (-1 == qdev_prop_parse(dev, name, value)) {
141 qemu_error("can't set property \"%s\" to \"%s\" for \"%s\"\n",
142 name, value, dev->info->name);
143 return -1;
145 return 0;
148 DeviceState *qdev_device_add(QemuOpts *opts)
150 const char *driver, *path, *id;
151 DeviceInfo *info;
152 DeviceState *qdev;
153 BusState *bus;
155 driver = qemu_opt_get(opts, "driver");
156 if (!driver) {
157 qemu_error("-device: no driver specified\n");
158 return NULL;
160 if (strcmp(driver, "?") == 0) {
161 char msg[256];
162 for (info = device_info_list; info != NULL; info = info->next) {
163 qdev_print_devinfo(info, msg, sizeof(msg));
164 qemu_error("%s\n", msg);
166 return NULL;
169 /* find driver */
170 info = qdev_find_info(NULL, driver);
171 if (!info) {
172 qemu_error("Device \"%s\" not found. Try -device '?' for a list.\n",
173 driver);
174 return NULL;
176 if (info->no_user) {
177 qemu_error("device \"%s\" can't be added via command line\n",
178 info->name);
179 return NULL;
182 /* find bus */
183 path = qemu_opt_get(opts, "bus");
184 if (path != NULL) {
185 bus = qbus_find(path);
186 } else {
187 bus = qbus_find_recursive(main_system_bus, NULL, info->bus_info);
189 if (!bus) {
190 qemu_error("Did not find %s bus for %s\n",
191 path ? path : info->bus_info->name, info->name);
192 return NULL;
195 /* create device, set properties */
196 qdev = qdev_create(bus, driver);
197 id = qemu_opts_id(opts);
198 if (id) {
199 qdev->id = id;
201 if (qemu_opt_foreach(opts, set_property, qdev, 1) != 0) {
202 qdev_free(qdev);
203 return NULL;
205 if (qdev_init(qdev) != 0) {
206 qdev_free(qdev);
207 return NULL;
209 return qdev;
212 /* Initialize a device. Device properties should be set before calling
213 this function. IRQs and MMIO regions should be connected/mapped after
214 calling this function. */
215 int qdev_init(DeviceState *dev)
217 return dev->info->init(dev, dev->info);
220 /* Unlink device from bus and free the structure. */
221 void qdev_free(DeviceState *dev)
223 LIST_REMOVE(dev, sibling);
224 qemu_free(dev);
227 /* Get a character (serial) device interface. */
228 CharDriverState *qdev_init_chardev(DeviceState *dev)
230 static int next_serial;
231 static int next_virtconsole;
232 /* FIXME: This is a nasty hack that needs to go away. */
233 if (strncmp(dev->info->name, "virtio", 6) == 0) {
234 return virtcon_hds[next_virtconsole++];
235 } else {
236 return serial_hds[next_serial++];
240 BusState *qdev_get_parent_bus(DeviceState *dev)
242 return dev->parent_bus;
245 void qdev_init_gpio_in(DeviceState *dev, qemu_irq_handler handler, int n)
247 assert(dev->num_gpio_in == 0);
248 dev->num_gpio_in = n;
249 dev->gpio_in = qemu_allocate_irqs(handler, dev, n);
252 void qdev_init_gpio_out(DeviceState *dev, qemu_irq *pins, int n)
254 assert(dev->num_gpio_out == 0);
255 dev->num_gpio_out = n;
256 dev->gpio_out = pins;
259 qemu_irq qdev_get_gpio_in(DeviceState *dev, int n)
261 assert(n >= 0 && n < dev->num_gpio_in);
262 return dev->gpio_in[n];
265 void qdev_connect_gpio_out(DeviceState * dev, int n, qemu_irq pin)
267 assert(n >= 0 && n < dev->num_gpio_out);
268 dev->gpio_out[n] = pin;
271 VLANClientState *qdev_get_vlan_client(DeviceState *dev,
272 NetCanReceive *can_receive,
273 NetReceive *receive,
274 NetReceiveIOV *receive_iov,
275 NetCleanup *cleanup,
276 void *opaque)
278 NICInfo *nd = dev->nd;
279 assert(nd);
280 nd->vc = qemu_new_vlan_client(nd->vlan, nd->model, nd->name, can_receive,
281 receive, receive_iov, cleanup, opaque);
282 return nd->vc;
286 void qdev_get_macaddr(DeviceState *dev, uint8_t *macaddr)
288 memcpy(macaddr, dev->nd->macaddr, 6);
291 static int next_block_unit[IF_COUNT];
293 /* Get a block device. This should only be used for single-drive devices
294 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
295 appropriate bus. */
296 BlockDriverState *qdev_init_bdrv(DeviceState *dev, BlockInterfaceType type)
298 int unit = next_block_unit[type]++;
299 DriveInfo *dinfo;
301 dinfo = drive_get(type, 0, unit);
302 return dinfo ? dinfo->bdrv : NULL;
305 BusState *qdev_get_child_bus(DeviceState *dev, const char *name)
307 BusState *bus;
309 LIST_FOREACH(bus, &dev->child_bus, sibling) {
310 if (strcmp(name, bus->name) == 0) {
311 return bus;
314 return NULL;
317 static BusState *qbus_find_recursive(BusState *bus, const char *name,
318 const BusInfo *info)
320 DeviceState *dev;
321 BusState *child, *ret;
322 int match = 1;
324 if (name && (strcmp(bus->name, name) != 0)) {
325 match = 0;
327 if (info && (bus->info != info)) {
328 match = 0;
330 if (match) {
331 return bus;
334 LIST_FOREACH(dev, &bus->children, sibling) {
335 LIST_FOREACH(child, &dev->child_bus, sibling) {
336 ret = qbus_find_recursive(child, name, info);
337 if (ret) {
338 return ret;
342 return NULL;
345 static void qbus_list_bus(DeviceState *dev, char *dest, int len)
347 BusState *child;
348 const char *sep = " ";
349 int pos = 0;
351 pos += snprintf(dest+pos, len-pos,"child busses at \"%s\":",
352 dev->id ? dev->id : dev->info->name);
353 LIST_FOREACH(child, &dev->child_bus, sibling) {
354 pos += snprintf(dest+pos, len-pos, "%s\"%s\"", sep, child->name);
355 sep = ", ";
359 static void qbus_list_dev(BusState *bus, char *dest, int len)
361 DeviceState *dev;
362 const char *sep = " ";
363 int pos = 0;
365 pos += snprintf(dest+pos, len-pos, "devices at \"%s\":",
366 bus->name);
367 LIST_FOREACH(dev, &bus->children, sibling) {
368 pos += snprintf(dest+pos, len-pos, "%s\"%s\"",
369 sep, dev->info->name);
370 if (dev->id)
371 pos += snprintf(dest+pos, len-pos, "/\"%s\"", dev->id);
372 sep = ", ";
376 static BusState *qbus_find_bus(DeviceState *dev, char *elem)
378 BusState *child;
380 LIST_FOREACH(child, &dev->child_bus, sibling) {
381 if (strcmp(child->name, elem) == 0) {
382 return child;
385 return NULL;
388 static DeviceState *qbus_find_dev(BusState *bus, char *elem)
390 DeviceState *dev;
393 * try to match in order:
394 * (1) instance id, if present
395 * (2) driver name
396 * (3) driver alias, if present
398 LIST_FOREACH(dev, &bus->children, sibling) {
399 if (dev->id && strcmp(dev->id, elem) == 0) {
400 return dev;
403 LIST_FOREACH(dev, &bus->children, sibling) {
404 if (strcmp(dev->info->name, elem) == 0) {
405 return dev;
408 LIST_FOREACH(dev, &bus->children, sibling) {
409 if (dev->info->alias && strcmp(dev->info->alias, elem) == 0) {
410 return dev;
413 return NULL;
416 static BusState *qbus_find(const char *path)
418 DeviceState *dev;
419 BusState *bus;
420 char elem[128], msg[256];
421 int pos, len;
423 /* find start element */
424 if (path[0] == '/') {
425 bus = main_system_bus;
426 pos = 0;
427 } else {
428 if (sscanf(path, "%127[^/]%n", elem, &len) != 1) {
429 qemu_error("path parse error (\"%s\")\n", path);
430 return NULL;
432 bus = qbus_find_recursive(main_system_bus, elem, NULL);
433 if (!bus) {
434 qemu_error("bus \"%s\" not found\n", elem);
435 return NULL;
437 pos = len;
440 for (;;) {
441 if (path[pos] == '\0') {
442 /* we are done */
443 return bus;
446 /* find device */
447 if (sscanf(path+pos, "/%127[^/]%n", elem, &len) != 1) {
448 qemu_error("path parse error (\"%s\" pos %d)\n", path, pos);
449 return NULL;
451 pos += len;
452 dev = qbus_find_dev(bus, elem);
453 if (!dev) {
454 qbus_list_dev(bus, msg, sizeof(msg));
455 qemu_error("device \"%s\" not found\n%s\n", elem, msg);
456 return NULL;
458 if (path[pos] == '\0') {
459 /* last specified element is a device. If it has exactly
460 * one child bus accept it nevertheless */
461 switch (dev->num_child_bus) {
462 case 0:
463 qemu_error("device has no child bus (%s)\n", path);
464 return NULL;
465 case 1:
466 return LIST_FIRST(&dev->child_bus);
467 default:
468 qbus_list_bus(dev, msg, sizeof(msg));
469 qemu_error("device has multiple child busses (%s)\n%s\n",
470 path, msg);
471 return NULL;
475 /* find bus */
476 if (sscanf(path+pos, "/%127[^/]%n", elem, &len) != 1) {
477 qemu_error("path parse error (\"%s\" pos %d)\n", path, pos);
478 return NULL;
480 pos += len;
481 bus = qbus_find_bus(dev, elem);
482 if (!bus) {
483 qbus_list_bus(dev, msg, sizeof(msg));
484 qemu_error("child bus \"%s\" not found\n%s\n", elem, msg);
485 return NULL;
490 BusState *qbus_create(BusInfo *info, DeviceState *parent, const char *name)
492 BusState *bus;
493 char *buf;
494 int i,len;
496 bus = qemu_mallocz(info->size);
497 bus->info = info;
498 bus->parent = parent;
500 if (name) {
501 /* use supplied name */
502 bus->name = qemu_strdup(name);
503 } else if (parent && parent->id) {
504 /* parent device has id -> use it for bus name */
505 len = strlen(parent->id) + 16;
506 buf = qemu_malloc(len);
507 snprintf(buf, len, "%s.%d", parent->id, parent->num_child_bus);
508 bus->name = buf;
509 } else {
510 /* no id -> use lowercase bus type for bus name */
511 len = strlen(info->name) + 16;
512 buf = qemu_malloc(len);
513 len = snprintf(buf, len, "%s.%d", info->name,
514 parent ? parent->num_child_bus : 0);
515 for (i = 0; i < len; i++)
516 buf[i] = qemu_tolower(buf[i]);
517 bus->name = buf;
520 LIST_INIT(&bus->children);
521 if (parent) {
522 LIST_INSERT_HEAD(&parent->child_bus, bus, sibling);
523 parent->num_child_bus++;
525 return bus;
528 #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
529 static void qbus_print(Monitor *mon, BusState *bus, int indent);
531 static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props,
532 const char *prefix, int indent)
534 char buf[64];
536 if (!props)
537 return;
538 while (props->name) {
539 if (props->info->print) {
540 props->info->print(dev, props, buf, sizeof(buf));
541 qdev_printf("%s-prop: %s = %s\n", prefix, props->name, buf);
543 props++;
547 static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
549 BusState *child;
550 qdev_printf("dev: %s, id \"%s\"\n", dev->info->name,
551 dev->id ? dev->id : "");
552 indent += 2;
553 if (dev->num_gpio_in) {
554 qdev_printf("gpio-in %d\n", dev->num_gpio_in);
556 if (dev->num_gpio_out) {
557 qdev_printf("gpio-out %d\n", dev->num_gpio_out);
559 qdev_print_props(mon, dev, dev->info->props, "dev", indent);
560 qdev_print_props(mon, dev, dev->parent_bus->info->props, "bus", indent);
561 if (dev->parent_bus->info->print_dev)
562 dev->parent_bus->info->print_dev(mon, dev, indent);
563 LIST_FOREACH(child, &dev->child_bus, sibling) {
564 qbus_print(mon, child, indent);
568 static void qbus_print(Monitor *mon, BusState *bus, int indent)
570 struct DeviceState *dev;
572 qdev_printf("bus: %s\n", bus->name);
573 indent += 2;
574 qdev_printf("type %s\n", bus->info->name);
575 LIST_FOREACH(dev, &bus->children, sibling) {
576 qdev_print(mon, dev, indent);
579 #undef qdev_printf
581 void do_info_qtree(Monitor *mon)
583 if (main_system_bus)
584 qbus_print(mon, main_system_bus, 0);
587 void do_info_qdm(Monitor *mon)
589 DeviceInfo *info;
590 char msg[256];
592 for (info = device_info_list; info != NULL; info = info->next) {
593 qdev_print_devinfo(info, msg, sizeof(msg));
594 monitor_printf(mon, "%s\n", msg);