Patchwork [16/21] qom-devices: add a Pin class

login
register
mail settings
Submitter Anthony Liguori
Date July 25, 2011, 1:44 a.m.
Message ID <1311558293-5855-17-git-send-email-aliguori@us.ibm.com>
Download mbox | patch
Permalink /patch/106597/
State New
Headers show

Comments

Anthony Liguori - July 25, 2011, 1:44 a.m.
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
---
 devices/Makefile   |    2 +-
 devices/pin.c      |   69 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 include/qemu/pin.h |   28 +++++++++++++++++++++
 3 files changed, 98 insertions(+), 1 deletions(-)
 create mode 100644 devices/pin.c
 create mode 100644 include/qemu/pin.h

Patch

diff --git a/devices/Makefile b/devices/Makefile
index fbb0b82..fa52689 100644
--- a/devices/Makefile
+++ b/devices/Makefile
@@ -1 +1 @@ 
-devices-obj-$(CONFIG_DEVICE) := device.o
+devices-obj-$(CONFIG_DEVICE) := device.o pin.o
diff --git a/devices/pin.c b/devices/pin.c
new file mode 100644
index 0000000..27812d8
--- /dev/null
+++ b/devices/pin.c
@@ -0,0 +1,69 @@ 
+#include "qemu/pin.h"
+
+void pin_initialize(Pin *pin, const char *id)
+{
+    type_initialize(pin, TYPE_PIN, id);
+}
+
+void pin_finalize(Pin *pin)
+{
+    type_finalize(pin);
+}
+
+void pin_visit(Pin *obj, Visitor *v, const char *name, Error **errp)
+{
+    visit_start_struct(v, (void **)&obj, "Pin", name, sizeof(Pin), errp);
+    device_visit(DEVICE(obj), v, "super", errp);
+    visit_type_bool(v, &obj->level, "level", errp);
+    visit_end_struct(v, errp);
+}
+
+void pin_set_level(Pin *pin, bool value, Error **errp)
+{
+    bool old_level = pin->level;
+
+    pin->level = value;
+
+    if (old_level != value) {
+        notifier_list_notify(&pin->level_changed, NULL);
+    }
+}
+
+bool pin_get_level(Pin *pin, Error **errp)
+{
+    return pin->level;
+}
+
+static void pin_initfn(TypeInstance *inst)
+{
+    Pin *obj = PIN(inst);
+
+    notifier_list_init(&obj->level_changed);
+
+    plug_add_property_bool(PLUG(obj), "level",
+                           (PlugPropertyGetterBool *)pin_get_level, 
+                           (PlugPropertySetterBool *)pin_set_level,
+                           PROP_F_READWRITE);
+}
+
+static void pin_class_initfn(TypeClass *class)
+{
+    DeviceClass *device_class = DEVICE_CLASS(class);
+
+    device_class->visit = (DeviceVisitor *)pin_visit;
+}
+
+static const TypeInfo pin_type_info = {
+    .name = TYPE_PIN,
+    .parent = TYPE_DEVICE,
+    .instance_size = sizeof(Pin),
+    .instance_init = pin_initfn,
+    .class_init = pin_class_initfn,
+};
+
+static void register_devices(void)
+{
+    type_register_static(&pin_type_info);
+}
+
+device_init(register_devices);
diff --git a/include/qemu/pin.h b/include/qemu/pin.h
new file mode 100644
index 0000000..e8f2582
--- /dev/null
+++ b/include/qemu/pin.h
@@ -0,0 +1,28 @@ 
+#ifndef PIN_H
+#define PIN_H
+
+#include "qemu/device.h"
+#include "notify.h"
+
+typedef struct Pin
+{
+    Device parent;
+
+    /* private */
+    bool level;
+
+    /* FIXME */
+    NotifierList level_changed;
+} Pin;
+
+#define TYPE_PIN "pin"
+#define PIN(obj) TYPE_CHECK(Pin, obj, TYPE_PIN)
+
+void pin_initialize(Pin *pin, const char *id);
+void pin_finalize(Pin *pin);
+void pin_visit(Pin *pin, Visitor *v, const char *name, Error **errp);
+
+void pin_set_level(Pin *pin, bool value, Error **errp);
+bool pin_get_level(Pin *pin, Error **errp);
+
+#endif