Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1MPEFQ-0002N4-2J for qemu-devel@nongnu.org; Fri, 10 Jul 2009 07:26:36 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1MPEFK-0002Dd-8e for qemu-devel@nongnu.org; Fri, 10 Jul 2009 07:26:34 -0400 Received: from [199.232.76.173] (port=37940 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MPEFK-0002DI-08 for qemu-devel@nongnu.org; Fri, 10 Jul 2009 07:26:30 -0400 Received: from mx2.redhat.com ([66.187.237.31]:38361) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1MPEFJ-0005ad-Dx for qemu-devel@nongnu.org; Fri, 10 Jul 2009 07:26:29 -0400 Received: from int-mx2.corp.redhat.com (int-mx2.corp.redhat.com [172.16.27.26]) by mx2.redhat.com (8.13.8/8.13.8) with ESMTP id n6ABQSwp029999 for ; Fri, 10 Jul 2009 07:26:28 -0400 From: Gerd Hoffmann Date: Fri, 10 Jul 2009 13:26:10 +0200 Message-Id: <1247225179-5495-5-git-send-email-kraxel@redhat.com> In-Reply-To: <1247225179-5495-1-git-send-email-kraxel@redhat.com> References: <1247225179-5495-1-git-send-email-kraxel@redhat.com> Subject: [Qemu-devel] [PATCH 04/13] qdev: add generic qdev_device_add() List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Gerd Hoffmann Will be used for -device command line. Signed-off-by: Gerd Hoffmann --- hw/pci.c | 1 + hw/qdev.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ hw/qdev.h | 3 +++ 3 files changed, 55 insertions(+), 0 deletions(-) diff --git a/hw/pci.c b/hw/pci.c index a52fe3a..a8e7c78 100644 --- a/hw/pci.c +++ b/hw/pci.c @@ -54,6 +54,7 @@ static struct BusInfo pci_bus_info = { .name = "PCI", .size = sizeof(PCIBus), .print_dev = pcibus_dev_print, + .add_dev = pci_create, .props = (Property[]) { { .name = "devfn", diff --git a/hw/qdev.c b/hw/qdev.c index 644a5be..e86c896 100644 --- a/hw/qdev.c +++ b/hw/qdev.c @@ -90,6 +90,57 @@ DeviceState *qdev_create(BusState *bus, const char *name) return dev; } +DeviceState *qdev_device_add(const char *cmdline) +{ + DeviceInfo *info; + DeviceState *qdev; + char driver[32], addr[32] = ""; + char tag[32], value[256]; + const char *params = NULL; + int n = 0; + + if (1 != sscanf(cmdline, "%32[^,],%n", driver, &n)) { + fprintf(stderr, "device parse error: \"%s\"\n", cmdline); + return NULL; + } + if (strcmp(driver, "?") == 0) { + for (info = device_info_list; info != NULL; info = info->next) { + fprintf(stderr, "name \"%s\", bus %s\n", info->name, info->bus_info->name); + } + return NULL; + } + if (n) { + params = cmdline + n; + get_param_value(addr, sizeof(addr), "addr", params); + } + info = qdev_find_info(NULL, driver); + + if (!info->bus_info->add_dev) { + fprintf(stderr, "bus \"%s\" can't add devices.\n", + info->bus_info->name); + return NULL; + } + + qdev = info->bus_info->add_dev(driver, strlen(addr) ? addr : NULL); + + if (params) { + while (params[0]) { + if (2 != sscanf(params, "%31[^=]=%255[^,]%n", tag, value, &n)) + break; + params += n; + if (strcmp(tag, "addr") == 0) + continue; + if (-1 == qdev_prop_parse(qdev, tag, value)) { + fprintf(stderr, "can't set property \"%s\" to \"%s\" for \"%s\"\n", + tag, value, driver); + } + } + } + + qdev_init(qdev); + return qdev; +} + /* Initialize a device. Device properties should be set before calling this function. IRQs and MMIO regions should be connected/mapped after calling this function. */ diff --git a/hw/qdev.h b/hw/qdev.h index 64e9b75..98c11a0 100644 --- a/hw/qdev.h +++ b/hw/qdev.h @@ -29,10 +29,12 @@ struct DeviceState { }; typedef void (*bus_dev_printfn)(Monitor *mon, DeviceState *dev, int indent); +typedef DeviceState* (*bus_dev_addfn)(const char *name, const char *addr); struct BusInfo { const char *name; size_t size; bus_dev_printfn print_dev; + bus_dev_addfn add_dev; Property *props; }; @@ -61,6 +63,7 @@ struct PropertyInfo { /*** Board API. This should go away once we have a machine config file. ***/ DeviceState *qdev_create(BusState *bus, const char *name); +DeviceState *qdev_device_add(const char *cmdline); void qdev_init(DeviceState *dev); void qdev_free(DeviceState *dev); -- 1.6.2.5