Patchwork [V4,12/12] hw/sd.c: introduce SD card "drive" property

login
register
mail settings
Submitter Mitsyanko Igor
Date July 27, 2012, 7:29 p.m.
Message ID <1343417387-13953-13-git-send-email-i.mitsyanko@samsung.com>
Download mbox | patch
Permalink /patch/173781/
State New
Headers show

Comments

Mitsyanko Igor - July 27, 2012, 7:29 p.m.
Setting "drive" SD card property ties SD card with BlockDriverState of the
same name. This property can be set dynamically, allowing for SD card hot-insert.

With "drive" property we no longer need SDClass::init method, all work is
done in property setter.

Signed-off-by: Igor Mitsyanko <i.mitsyanko@samsung.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Markus Armbruster <armbru@redhat.com>
---
 hw/sd.c |   74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++------
 hw/sd.h |    7 ++++-
 2 files changed, 71 insertions(+), 10 deletions(-)

Patch

diff --git a/hw/sd.c b/hw/sd.c
index 611e1f3..fabb046 100644
--- a/hw/sd.c
+++ b/hw/sd.c
@@ -491,15 +491,24 @@  static const VMStateDescription sd_vmstate = {
    whether card should be in SSI or MMC/SD mode.  It is also up to the
    board to ensure that ssi transfers only occur when the chip select
    is asserted.  */
-static void sd_init_card(SDState *sd, BlockDriverState *bs)
+static void sd_init_card(SDState *sd, BlockDriverState *bdrv, Error **errp)
 {
-    sd->buf = qemu_blockalign(bs, 512);
-    sd->enable = true;
-    sd_reset(sd, bs);
-    if (sd->bdrv) {
-        bdrv_attach_dev_nofail(sd->bdrv, sd);
-        bdrv_set_dev_ops(sd->bdrv, &sd_block_ops, sd);
+    if (!bdrv) {
+        error_set(errp, QERR_MISSING_PARAMETER, "BlockDriverState");
+        return;
+    }
+
+    if (bdrv_attach_dev(bdrv, sd) < 0) {
+        error_set(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bdrv));
+        return;
     }
+
+    bdrv_set_dev_ops(bdrv, &sd_block_ops, sd);
+    sd->buf = qemu_blockalign(bdrv, 512);
+    sd->enable = true;
+    sd_reset(sd, bdrv);
+    qemu_set_irq(sd->inserted_cb, bdrv_is_inserted(sd->bdrv));
+    qemu_set_irq(sd->readonly_cb, sd->wp_switch);
     vmstate_register(NULL, -1, &sd_vmstate, sd);
 }
 
@@ -1756,7 +1765,6 @@  static void sd_class_init(ObjectClass *klass, void *data)
 {
     SDClass *k = SD_CLASS(klass);
 
-    k->init = sd_init_card;
     k->set_cb = sd_set_callbacks;
     k->do_command = sd_send_command;
     k->data_ready = sd_is_data_ready;
@@ -1785,6 +1793,54 @@  static void sd_set_spimode(Object *obj, Visitor *v, void *opaque,
     }
 }
 
+static char *sd_drive_get(Object *obj, Error **errp)
+{
+    SDState *sd = SD_CARD(obj);
+    char *ret = NULL;
+
+    if (sd->bdrv) {
+        ret = g_strndup(bdrv_get_device_name(sd->bdrv), 32);
+    } else {
+        error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, "SD card");
+    }
+
+    return ret;
+}
+
+static void sd_drive_set(Object *obj, const char *value, Error **errp)
+{
+    SDState *sd = SD_CARD(obj);
+    BlockDriverState *new_bdrv = bdrv_find(value);
+
+    if (!new_bdrv) {
+        error_set(errp, QERR_DEVICE_NOT_FOUND, value);
+        return;
+    }
+
+    if (sd->bdrv) {
+        if (!strncmp(bdrv_get_device_name(sd->bdrv), value, 32)) {
+            return;
+        }
+
+        if (bdrv_in_use(sd->bdrv)) {
+            error_set(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(sd->bdrv));
+            return;
+        }
+
+        bdrv_close(sd->bdrv);
+        sd->enable = false;
+        bdrv_detach_dev(sd->bdrv, sd);
+        vmstate_unregister(NULL, &sd_vmstate, sd);
+        if (sd->buf) {
+            qemu_vfree(sd->buf);
+            sd->buf = NULL;
+        }
+        sd_reset(sd, NULL);
+    }
+
+    sd_init_card(sd, new_bdrv, errp);
+}
+
 static void sd_initfn(Object *obj)
 {
     SDState *sd = SD_CARD(obj);
@@ -1792,6 +1848,8 @@  static void sd_initfn(Object *obj)
     sd->spi = false;
     object_property_add(obj, "spi", "boolean", sd_is_spi, sd_set_spimode,
             NULL, NULL, NULL);
+    object_property_add_str(OBJECT(sd), "drive", sd_drive_get, sd_drive_set,
+        NULL);
 }
 
 static const TypeInfo sd_type_info = {
diff --git a/hw/sd.h b/hw/sd.h
index c78eaa1..acf5d94 100644
--- a/hw/sd.h
+++ b/hw/sd.h
@@ -32,6 +32,7 @@ 
 #include "qemu-common.h"
 #include "qemu/object.h"
 #include "qapi/qapi-visit-core.h"
+#include "block.h"
 
 #define OUT_OF_RANGE		(1 << 31)
 #define ADDRESS_ERROR		(1 << 30)
@@ -74,7 +75,6 @@  typedef struct SDState SDState;
 typedef struct SDClass {
     ObjectClass parent_class;
 
-    void (*init)(SDState *sd, BlockDriverState *bdrv);
     int (*do_command)(SDState *sd, SDRequest *req, uint8_t *response);
     void (*write_data)(SDState *sd, uint8_t value);
     uint8_t (*read_data)(SDState *sd);
@@ -96,8 +96,11 @@  static inline SDState *sd_init(BlockDriverState *bs, bool is_spi)
     SDState *sd = SD_CARD(object_new(TYPE_SD_CARD));
     Error *errp = NULL;
     object_property_set_bool(OBJECT(sd), is_spi, "spi", &errp);
+    if (bs) {
+        object_property_set_str(OBJECT(sd), bdrv_get_device_name(bs),
+                "drive", &errp);
+    }
     assert_no_error(errp);
-    SD_GET_CLASS(sd)->init(sd, bs);
     return sd;
 }