From 6305d334534c608393112808a558bfd2c169f1a2 Mon Sep 17 00:00:00 2001
From: Markus Armbruster <armbru@redhat.com>
Date: Wed, 31 Mar 2010 13:25:15 -0300
Subject: [PATCH 60/66] monitor: convert do_device_add() to QObject

RH-Author: Markus Armbruster <armbru@redhat.com>
Message-id: <1270041921-28969-61-git-send-email-armbru@redhat.com>
Patchwork-id: 8225
O-Subject: [PATCH 60/66] monitor: convert do_device_add() to QObject
Bugzilla: 579470
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
RH-Acked-by: Juan Quintela <quintela@redhat.com>
RH-Acked-by: Luiz Capitulino <lcapitulino@redhat.com>

(cherry picked from commit 8bc27249f0f62524887ea355a6604722edd276a9)
---
 hw/qdev.c       |   42 ++++++++++++++++++++++++++++++++++--------
 hw/qdev.h       |    2 +-
 qemu-monitor.hx |    3 ++-
 3 files changed, 37 insertions(+), 10 deletions(-)

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
 hw/qdev.c       |   42 ++++++++++++++++++++++++++++++++++--------
 hw/qdev.h       |    2 +-
 qemu-monitor.hx |    3 ++-
 3 files changed, 37 insertions(+), 10 deletions(-)

diff --git a/hw/qdev.c b/hw/qdev.c
index a28e262..17a46a7 100644
--- a/hw/qdev.c
+++ b/hw/qdev.c
@@ -586,7 +586,9 @@ static BusState *qbus_find(const char *path)
         dev = qbus_find_dev(bus, elem);
         if (!dev) {
             qerror_report(QERR_DEVICE_NOT_FOUND, elem);
-            qbus_list_dev(bus);
+            if (!monitor_cur_is_qmp()) {
+                qbus_list_dev(bus);
+            }
             return NULL;
         }
 
@@ -605,7 +607,9 @@ static BusState *qbus_find(const char *path)
                 return QLIST_FIRST(&dev->child_bus);
             default:
                 qerror_report(QERR_DEVICE_MULTIPLE_BUSSES, elem);
-                qbus_list_bus(dev);
+                if (!monitor_cur_is_qmp()) {
+                    qbus_list_bus(dev);
+                }
                 return NULL;
             }
         }
@@ -619,7 +623,9 @@ static BusState *qbus_find(const char *path)
         bus = qbus_find_bus(dev, elem);
         if (!bus) {
             qerror_report(QERR_BUS_NOT_FOUND, elem);
-            qbus_list_bus(dev);
+            if (!monitor_cur_is_qmp()) {
+                qbus_list_bus(dev);
+            }
             return NULL;
         }
     }
@@ -762,16 +768,36 @@ void do_info_qdm(Monitor *mon)
     }
 }
 
-void do_device_add(Monitor *mon, const QDict *qdict)
+/**
+ * do_device_add(): Add a device
+ *
+ * Argument qdict contains
+ * - "driver": the name of the new device's driver
+ * - "bus": the device's parent bus (device tree path)
+ * - "id": the device's ID (must be unique)
+ * - device properties
+ *
+ * Example:
+ *
+ * { "driver": "usb-net", "id": "eth1", "netdev": "netdev1" }
+ */
+int do_device_add(Monitor *mon, const QDict *qdict, QObject **ret_data)
 {
     QemuOpts *opts;
 
     opts = qemu_opts_from_qdict(&qemu_device_opts, qdict);
-    if (opts) {
-        if (qdev_device_help(opts) || qdev_device_add(opts) == NULL) {
-            qemu_opts_del(opts);
-        }
+    if (!opts) {
+        return -1;
+    }
+    if (!monitor_cur_is_qmp() && qdev_device_help(opts)) {
+        qemu_opts_del(opts);
+        return 0;
     }
+    if (!qdev_device_add(opts)) {
+        qemu_opts_del(opts);
+        return -1;
+    }
+    return 0;
 }
 
 void do_device_del(Monitor *mon, const QDict *qdict)
diff --git a/hw/qdev.h b/hw/qdev.h
index adfcf79..9475705 100644
--- a/hw/qdev.h
+++ b/hw/qdev.h
@@ -175,7 +175,7 @@ void qbus_free(BusState *bus);
 
 void do_info_qtree(Monitor *mon);
 void do_info_qdm(Monitor *mon);
-void do_device_add(Monitor *mon, const QDict *qdict);
+int do_device_add(Monitor *mon, const QDict *qdict, QObject **ret_data);
 void do_device_del(Monitor *mon, const QDict *qdict);
 
 /*** qdev-properties.c ***/
diff --git a/qemu-monitor.hx b/qemu-monitor.hx
index 9f4af98..4d1abb2 100644
--- a/qemu-monitor.hx
+++ b/qemu-monitor.hx
@@ -545,7 +545,8 @@ ETEXI
         .args_type  = "device:O",
         .params     = "driver[,prop=value][,...]",
         .help       = "add device, like -device on the command line",
-        .mhandler.cmd = do_device_add,
+        .user_print = monitor_user_noop,
+        .mhandler.cmd_new = do_device_add,
     },
 
 STEXI
-- 
1.7.0.3

