diff mbox

[RFC,v5,16/23] fdc: Implement ISA state callbacks

Message ID 1308019077-61957-17-git-send-email-andreas.faerber@web.de
State New
Headers show

Commit Message

Andreas Färber June 14, 2011, 2:37 a.m. UTC
Add "enabled" qdev property, and implement enable and disable callbacks.

Incorporate ISA VMState as well as I/O base and IRQ as subsection,
and implement pre_load and post_load callbacks.

Cc: Gerd Hoffmann <kraxel@redhat.com>
Cc: Markus Armbruster <armbru@redhat.com>
Cc: Juan Quintela <quintela@redhat.com>
Signed-off-by: Andreas Färber <andreas.faerber@web.de>
---
 hw/fdc.c |   91 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 88 insertions(+), 3 deletions(-)
diff mbox

Patch

diff --git a/hw/fdc.c b/hw/fdc.c
index f4e3e0d..778fb58 100644
--- a/hw/fdc.c
+++ b/hw/fdc.c
@@ -431,6 +431,8 @@  typedef struct FDCtrlISABus {
     struct FDCtrl state;
     int32_t bootindexA;
     int32_t bootindexB;
+    uint32_t initial_iobase;
+    uint32_t initial_irq;
 } FDCtrlISABus;
 
 static uint32_t fdctrl_read (void *opaque, uint32_t reg)
@@ -1894,11 +1896,10 @@  static int fdctrl_init_common(FDCtrl *fdctrl)
     return fdctrl_connect_drives(fdctrl);
 }
 
-static int isabus_fdc_init1(ISADevice *dev)
+static int isabus_fdc_enable(ISADevice *dev)
 {
     FDCtrlISABus *isa = DO_UPCAST(FDCtrlISABus, busdev, dev);
     FDCtrl *fdctrl = &isa->state;
-    int ret;
 
     register_ioport_read(isa->iobase + 0x01, 5, 1,
                          &fdctrl_read_port, fdctrl);
@@ -1912,7 +1913,58 @@  static int isabus_fdc_init1(ISADevice *dev)
     isa_init_ioport(dev, isa->iobase + 7);
 
     isa_init_irq(&isa->busdev, &fdctrl->irq, isa->irq);
+
+    return 0;
+}
+
+static int isabus_fdc_disable(ISADevice *dev)
+{
+    FDCtrlISABus *isa = DO_UPCAST(FDCtrlISABus, busdev, dev);
+    FDCtrl *fdctrl = &isa->state;
+
+    isa_discard_ioport_range(dev, isa->iobase + 0x07, 1);
+    isa_discard_ioport_range(dev, isa->iobase + 0x01, 5);
+    isa_unassign_ioport(isa->iobase + 7, 1);
+    isa_unassign_ioport(isa->iobase, 6);
+
+    isa_uninit_irq(&isa->busdev, &fdctrl->irq, isa->irq);
+    fdctrl->irq = NULL;
+
+    return 0;
+}
+
+static int isabus_fdc_pre_load(void *opaque)
+{
+    FDCtrlISABus *s = opaque;
+
+    isa_set_state(&s->busdev, false);
+    return 0;
+}
+
+static int isabus_fdc_post_load(void *opaque, int version_id)
+{
+    FDCtrlISABus *s = opaque;
+    ISADevice *dev = &s->busdev;
+
+    if (dev->enabled) {
+        isabus_fdc_enable(dev);
+    }
+    return 0;
+}
+
+static int isabus_fdc_init1(ISADevice *dev)
+{
+    FDCtrlISABus *isa = DO_UPCAST(FDCtrlISABus, busdev, dev);
+    FDCtrl *fdctrl = &isa->state;
+    int ret;
+
+    isa->initial_iobase = isa->iobase;
+    isa->initial_irq = isa->irq;
+
     fdctrl->dma_chann = isa->dma;
+    if (dev->enabled) {
+        isabus_fdc_enable(dev);
+    }
 
     qdev_set_legacy_instance_id(&dev->qdev, isa->iobase, 2);
     ret = fdctrl_init_common(fdctrl);
@@ -1960,18 +2012,50 @@  static int sun4m_fdc_init1(SysBusDevice *dev)
     return fdctrl_init_common(fdctrl);
 }
 
+static bool isabus_fdc_isaconfig_needed(void *opaque)
+{
+    FDCtrlISABus *s = opaque;
+
+    return isa_vmstate_needed(&s->busdev) ||
+        s->initial_iobase != s->iobase ||
+        s->initial_irq != s->irq;
+}
+
+static const VMStateDescription vmstate_isa_fdc_isaconfig = {
+    .name = "fdc/isa-config",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .fields = (VMStateField []) {
+        VMSTATE_ISA_DEVICE(busdev, FDCtrlISABus),
+        VMSTATE_UINT32(iobase, FDCtrlISABus),
+        VMSTATE_UINT32(irq, FDCtrlISABus),
+        VMSTATE_END_OF_LIST()
+    },
+};
+
 static const VMStateDescription vmstate_isa_fdc ={
     .name = "fdc",
     .version_id = 2,
     .minimum_version_id = 2,
+    .pre_load  = isabus_fdc_pre_load,
+    .post_load = isabus_fdc_post_load,
     .fields = (VMStateField []) {
         VMSTATE_STRUCT(state, FDCtrlISABus, 0, vmstate_fdc, FDCtrl),
         VMSTATE_END_OF_LIST()
-    }
+    },
+    .subsections = (VMStateSubsection []) {
+        {
+            .vmsd = &vmstate_isa_fdc_isaconfig,
+            .needed = isabus_fdc_isaconfig_needed,
+        }, {
+        }
+    },
 };
 
 static ISADeviceInfo isa_fdc_info = {
     .init = isabus_fdc_init1,
+    .enable = isabus_fdc_enable,
+    .disable = isabus_fdc_disable,
     .qdev.name  = "isa-fdc",
     .qdev.fw_name  = "fdc",
     .qdev.size  = sizeof(FDCtrlISABus),
@@ -1986,6 +2070,7 @@  static ISADeviceInfo isa_fdc_info = {
         DEFINE_PROP_DRIVE("driveB", FDCtrlISABus, state.drives[1].bs),
         DEFINE_PROP_INT32("bootindexA", FDCtrlISABus, bootindexA, -1),
         DEFINE_PROP_INT32("bootindexB", FDCtrlISABus, bootindexB, -1),
+        DEFINE_PROP_BOOL("enabled", FDCtrlISABus, busdev.enabled, true),
         DEFINE_PROP_END_OF_LIST(),
     },
 };