Skip to contentSkip to conversation

arm/stellaris: convert gamepad input device to qdev

PATCH Superseded Peter Maydell 2 participants 19 messages Oct 17, 2023 – Oct 30, 2023 Arm/Amlogic Meson SoC support

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover arm/stellaris: convert gamepad input device to qdev

(I had this all ready to go before I went off on holiday two weeks ago, except I forgot to actually *send* the emails...)

This patchseries converts the stellaris board's gamepad input device to qdev. This isn't a very important bit of conversion (I was just looking for a small tail-end-of-the-week task), but it does reduce by one the number of users of a couple of legacy APIs: vmstate_register() and qemu_add_kbd_event_handler().

I've included Kevin's qdev_prop_set_array() patch here, because I wanted an array property and it doesn't seem sensible to write it the old way and have another thing that needs converting. I'm assuming that by the time this patchset gets reviewed and committed that one will already be upstream.

thanks
-- PMM

Kevin Wolf (1):
qdev: Add qdev_prop_set_array()

Peter Maydell (5):
hw/input/stellaris_input: Rename to stellaris_gamepad hw/input/stellaris_gamepad: Rename structs to our usual convention hw/input/stellaris_gamepad: Remove StellarisGamepadButton struct hw/input/stellaris_input: Convert to qdev hw/input/stellaris_gamepad: Convert to qemu_input_handler_register()

 include/hw/input/gamepad.h           |  18 -----
 include/hw/input/stellaris_gamepad.h |  39 ++++++++++
 include/hw/qdev-properties.h         |   3 +
 hw/arm/stellaris.c                   |  34 ++++++---
 hw/core/qdev-properties.c            |  21 ++++++
 hw/input/stellaris_gamepad.c         | 102 +++++++++++++++++++++++++++
 hw/input/stellaris_input.c           |  93 ------------------------
 hw/arm/Kconfig                       |   2 +-
 hw/input/Kconfig                     |   2 +-
 hw/input/meson.build                 |   2 +-
 10 files changed, 193 insertions(+), 123 deletions(-)
 delete mode 100644 include/hw/input/gamepad.h
 create mode 100644 include/hw/input/stellaris_gamepad.h
 create mode 100644 hw/input/stellaris_gamepad.c
 delete mode 100644 hw/input/stellaris_input.c
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/6 hw/input/stellaris_input: Rename to stellaris_gamepad

This source file implements a stellaris gamepad device; rename it so that it is a closer match to the device name.

---
 include/hw/input/{gamepad.h => stellaris_gamepad.h} | 6 +++---
 hw/arm/stellaris.c                                  | 2 +-
 hw/input/{stellaris_input.c => stellaris_gamepad.c} | 2 +-
 hw/arm/Kconfig                                      | 2 +-
 hw/input/Kconfig                                    | 2 +-
 hw/input/meson.build                                | 2 +-
 6 files changed, 8 insertions(+), 8 deletions(-)
 rename include/hw/input/{gamepad.h => stellaris_gamepad.h} (77%)
 rename hw/input/{stellaris_input.c => stellaris_gamepad.c} (98%)
include/hw/input/stellaris_gamepad.h renamed from include/hw/input/gamepad.h +3 -3
similarity index 77%
rename from include/hw/input/gamepad.h
rename to include/hw/input/stellaris_gamepad.h
index 6f6aa2406aa..23cfd3c95f3 100644
@@ -8,11 +8,11 @@
  * See the COPYING file in the top-level directory.
  */
 
-#ifndef HW_INPUT_GAMEPAD_H
-#define HW_INPUT_GAMEPAD_H
+#ifndef HW_INPUT_STELLARIS_GAMEPAD_H
+#define HW_INPUT_STELLARIS_GAMEPAD_H
 
 
-/* stellaris_input.c */
+/* stellaris_gamepad.c */
 void stellaris_gamepad_init(int n, qemu_irq *irq, const int *keycode);
 
 #endif
hw/arm/stellaris.c +1 -1
index aa5b0ddfaa5..96585dd7106 100644
@@ -23,7 +23,7 @@
 #include "sysemu/sysemu.h"
 #include "hw/arm/armv7m.h"
 #include "hw/char/pl011.h"
-#include "hw/input/gamepad.h"
+#include "hw/input/stellaris_gamepad.h"
 #include "hw/irq.h"
 #include "hw/watchdog/cmsdk-apb-watchdog.h"
 #include "migration/vmstate.h"
hw/input/stellaris_gamepad.c renamed from hw/input/stellaris_input.c +1 -1
similarity index 98%
rename from hw/input/stellaris_input.c
rename to hw/input/stellaris_gamepad.c
index e6ee5e11f1b..3bab557cab3 100644
@@ -8,7 +8,7 @@
  */
 
 #include "qemu/osdep.h"
-#include "hw/input/gamepad.h"
+#include "hw/input/stellaris_gamepad.h"
 #include "hw/irq.h"
 #include "migration/vmstate.h"
 #include "ui/console.h"
hw/arm/Kconfig +1 -1
index 7e683484405..841f3131ea5 100644
@@ -291,7 +291,7 @@ config STELLARIS
     select SSD0303 # OLED display
     select SSD0323 # OLED display
     select SSI_SD
-    select STELLARIS_INPUT
+    select STELLARIS_GAMEPAD
     select STELLARIS_ENET # ethernet
     select STELLARIS_GPTM # general purpose timer module
     select UNIMP
hw/input/Kconfig +1 -1
index 55865bb3869..f86e98c8293 100644
@@ -20,7 +20,7 @@ config PL050
 config PS2
     bool
 
-config STELLARIS_INPUT
+config STELLARIS_GAMEPAD
     bool
 
 config TSC2005
hw/input/meson.build +1 -1
index c0d44821800..640556bbbcc 100644
@@ -5,7 +5,7 @@ system_ss.add(when: 'CONFIG_LM832X', if_true: files('lm832x.c'))
 system_ss.add(when: 'CONFIG_PCKBD', if_true: files('pckbd.c'))
 system_ss.add(when: 'CONFIG_PL050', if_true: files('pl050.c'))
 system_ss.add(when: 'CONFIG_PS2', if_true: files('ps2.c'))
-system_ss.add(when: 'CONFIG_STELLARIS_INPUT', if_true: files('stellaris_input.c'))
+system_ss.add(when: 'CONFIG_STELLARIS_GAMEPAD', if_true: files('stellaris_gamepad.c'))
 system_ss.add(when: 'CONFIG_TSC2005', if_true: files('tsc2005.c'))
 
 system_ss.add(when: 'CONFIG_VIRTIO_INPUT', if_true: files('virtio-input.c'))
Signature · 1 line
2.34.1
Reviewed-by on v1 1/6
#2 lore raw

Re: [PATCH 1/6] hw/input/stellaris_input: Rename to stellaris_gamepad

Quoted 12 lines · Peter Maydell

This source file implements a stellaris gamepad device; rename it so that it is a closer match to the device name.

---
  include/hw/input/{gamepad.h => stellaris_gamepad.h} | 6 +++---
  hw/arm/stellaris.c                                  | 2 +-
  hw/input/{stellaris_input.c => stellaris_gamepad.c} | 2 +-
  hw/arm/Kconfig                                      | 2 +-
  hw/input/Kconfig                                    | 2 +-
  hw/input/meson.build                                | 2 +-
  6 files changed, 8 insertions(+), 8 deletions(-)
Quoted 2 lines

rename include/hw/input/{gamepad.h => stellaris_gamepad.h} (77%) rename hw/input/{stellaris_input.c => stellaris_gamepad.c} (98%)

#3 lore raw

PATCH 2/6 hw/input/stellaris_gamepad: Rename structs to our usual convention

In reply to Peter Maydell

Rename the structs in stellaris_gamepad.c to our now-standard CamelCase convention.

---
 hw/input/stellaris_gamepad.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)
hw/input/stellaris_gamepad.c +11 -11
index 3bab557cab3..377101a4035 100644
@@ -17,17 +17,17 @@ typedef struct {
     qemu_irq irq;
     int keycode;
     uint8_t pressed;
-} gamepad_button;
+} StellarisGamepadButton;
 
 typedef struct {
-    gamepad_button *buttons;
+    StellarisGamepadButton *buttons;
     int num_buttons;
     int extension;
-} gamepad_state;
+} StellarisGamepad;
 
 static void stellaris_gamepad_put_key(void * opaque, int keycode)
 {
-    gamepad_state *s = (gamepad_state *)opaque;
+    StellarisGamepad *s = (StellarisGamepad *)opaque;
     int i;
     int down;
 
@@ -55,7 +55,7 @@ static const VMStateDescription vmstate_stellaris_button = {
     .version_id = 0,
     .minimum_version_id = 0,
     .fields = (VMStateField[]) {
-        VMSTATE_UINT8(pressed, gamepad_button),
+        VMSTATE_UINT8(pressed, StellarisGamepadButton),
         VMSTATE_END_OF_LIST()
     }
 };
@@ -65,11 +65,11 @@ static const VMStateDescription vmstate_stellaris_gamepad = {
     .version_id = 2,
     .minimum_version_id = 2,
     .fields = (VMStateField[]) {
-        VMSTATE_INT32(extension, gamepad_state),
-        VMSTATE_STRUCT_VARRAY_POINTER_INT32(buttons, gamepad_state,
+        VMSTATE_INT32(extension, StellarisGamepad),
+        VMSTATE_STRUCT_VARRAY_POINTER_INT32(buttons, StellarisGamepad,
                                             num_buttons,
                                             vmstate_stellaris_button,
-                                            gamepad_button),
+                                            StellarisGamepadButton),
         VMSTATE_END_OF_LIST()
     }
 };
@@ -77,11 +77,11 @@ static const VMStateDescription vmstate_stellaris_gamepad = {
 /* Returns an array of 5 output slots.  */
 void stellaris_gamepad_init(int n, qemu_irq *irq, const int *keycode)
 {
-    gamepad_state *s;
+    StellarisGamepad *s;
     int i;
 
-    s = g_new0(gamepad_state, 1);
-    s->buttons = g_new0(gamepad_button, n);
+    s = g_new0(StellarisGamepad, 1);
+    s->buttons = g_new0(StellarisGamepadButton, n);
     for (i = 0; i < n; i++) {
         s->buttons[i].irq = irq[i];
         s->buttons[i].keycode = keycode[i];
Signature · 1 line
2.34.1
#5 lore raw

PATCH 3/6 qdev: Add qdev_prop_set_array()

In reply to Peter Maydell

From: Kevin Wolf <[email protected]>

Instead of exposing the ugly hack of how we represent arrays in qdev (a static "foo-len" property and after it is set, dynamically created "foo[i]" properties) to boards, add an interface that allows setting the whole array at once.

Once all internal users of devices with array properties have been converted to use this function, we can change the implementation to move away from this hack.

---
 include/hw/qdev-properties.h |  3 +++
 hw/core/qdev-properties.c    | 21 +++++++++++++++++++++
 2 files changed, 24 insertions(+)
include/hw/qdev-properties.h +3 -0
index e1df08876c6..7fa2fdb7c94 100644
@@ -206,6 +206,9 @@ void qdev_prop_set_macaddr(DeviceState *dev, const char *name,
                            const uint8_t *value);
 void qdev_prop_set_enum(DeviceState *dev, const char *name, int value);
 
+/* Takes ownership of @values */
+void qdev_prop_set_array(DeviceState *dev, const char *name, QList *values);
+
 void *object_field_prop_ptr(Object *obj, Property *prop);
 
 void qdev_prop_register_global(GlobalProperty *prop);
hw/core/qdev-properties.c +21 -0
index 357b8761b54..950ef48e013 100644
@@ -3,12 +3,14 @@
 #include "qapi/error.h"
 #include "qapi/qapi-types-misc.h"
 #include "qapi/qmp/qerror.h"
+#include "qapi/qmp/qlist.h"
 #include "qemu/ctype.h"
 #include "qemu/error-report.h"
 #include "qapi/visitor.h"
 #include "qemu/units.h"
 #include "qemu/cutils.h"
 #include "qdev-prop-internal.h"
+#include "qom/qom-qobject.h"
 
 void qdev_prop_set_after_realize(DeviceState *dev, const char *name,
                                   Error **errp)
@@ -739,6 +741,25 @@ void qdev_prop_set_enum(DeviceState *dev, const char *name, int value)
                             &error_abort);
 }
 
+void qdev_prop_set_array(DeviceState *dev, const char *name, QList *values)
+{
+    const QListEntry *entry;
+    g_autofree char *prop_len = g_strdup_printf("len-%s", name);
+    uint32_t i = 0;
+
+    object_property_set_int(OBJECT(dev), prop_len, qlist_size(values),
+                            &error_abort);
+
+    QLIST_FOREACH_ENTRY(values, entry) {
+        g_autofree char *prop_idx = g_strdup_printf("%s[%u]", name, i);
+        object_property_set_qobject(OBJECT(dev), prop_idx, entry->value,
+                                    &error_abort);
+        i++;
+    }
+
+    qobject_unref(values);
+}
+
 static GPtrArray *global_props(void)
 {
     static GPtrArray *gp;
Signature · 1 line
2.34.1
#6 lore raw

PATCH 4/6 hw/input/stellaris_gamepad: Remove StellarisGamepadButton struct

In reply to Peter Maydell

Currently for each button on the device we have a StellarisGamepadButton struct which has the irq, keycode and pressed state for it. When we convert to qdev, the qdev property and GPIO APIs are going to require that we have separate arrays for the irqs and keycodes. Convert from array-of-structs to three separate arrays in preparation.

---
 hw/input/stellaris_gamepad.c | 43 ++++++++++++------------------------
 1 file changed, 14 insertions(+), 29 deletions(-)
hw/input/stellaris_gamepad.c +14 -29
index 377101a4035..da974400b59 100644
@@ -14,15 +14,11 @@
 #include "ui/console.h"
 
 typedef struct {
-    qemu_irq irq;
-    int keycode;
-    uint8_t pressed;
-} StellarisGamepadButton;
-
-typedef struct {
-    StellarisGamepadButton *buttons;
-    int num_buttons;
+    uint32_t num_buttons;
     int extension;
+    qemu_irq *irqs;
+    uint32_t *keycodes;
+    uint8_t *pressed;
 } StellarisGamepad;
 
 static void stellaris_gamepad_put_key(void * opaque, int keycode)
@@ -40,36 +36,23 @@ static void stellaris_gamepad_put_key(void * opaque, int keycode)
     keycode = (keycode & 0x7f) | s->extension;
 
     for (i = 0; i < s->num_buttons; i++) {
-        if (s->buttons[i].keycode == keycode
-                && s->buttons[i].pressed != down) {
-            s->buttons[i].pressed = down;
-            qemu_set_irq(s->buttons[i].irq, down);
+        if (s->keycodes[i] == keycode && s->pressed[i] != down) {
+            s->pressed[i] = down;
+            qemu_set_irq(s->irqs[i], down);
         }
     }
 
     s->extension = 0;
 }
 
-static const VMStateDescription vmstate_stellaris_button = {
-    .name = "stellaris_button",
-    .version_id = 0,
-    .minimum_version_id = 0,
-    .fields = (VMStateField[]) {
-        VMSTATE_UINT8(pressed, StellarisGamepadButton),
-        VMSTATE_END_OF_LIST()
-    }
-};
-
 static const VMStateDescription vmstate_stellaris_gamepad = {
     .name = "stellaris_gamepad",
     .version_id = 2,
     .minimum_version_id = 2,
     .fields = (VMStateField[]) {
         VMSTATE_INT32(extension, StellarisGamepad),
-        VMSTATE_STRUCT_VARRAY_POINTER_INT32(buttons, StellarisGamepad,
-                                            num_buttons,
-                                            vmstate_stellaris_button,
-                                            StellarisGamepadButton),
+        VMSTATE_VARRAY_UINT32(pressed, StellarisGamepad, num_buttons,
+                              0, vmstate_info_uint8, uint8_t),
         VMSTATE_END_OF_LIST()
     }
 };
@@ -81,10 +64,12 @@ void stellaris_gamepad_init(int n, qemu_irq *irq, const int *keycode)
     int i;
 
     s = g_new0(StellarisGamepad, 1);
-    s->buttons = g_new0(StellarisGamepadButton, n);
+    s->irqs = g_new0(qemu_irq, n);
+    s->keycodes = g_new0(uint32_t, n);
+    s->pressed = g_new0(uint8_t, n);
     for (i = 0; i < n; i++) {
-        s->buttons[i].irq = irq[i];
-        s->buttons[i].keycode = keycode[i];
+        s->irqs[i] = irq[i];
+        s->keycodes[i] = keycode[i];
     }
     s->num_buttons = n;
     qemu_add_kbd_event_handler(stellaris_gamepad_put_key, s);
Signature · 1 line
2.34.1
#7 lore raw

Re: [PATCH 4/6] hw/input/stellaris_gamepad: Remove StellarisGamepadButton struct

Hi Peter,

Quoted 25 lines · Peter Maydell

Currently for each button on the device we have a StellarisGamepadButton struct which has the irq, keycode and pressed state for it. When we convert to qdev, the qdev property and GPIO APIs are going to require that we have separate arrays for the irqs and keycodes. Convert from array-of-structs to three separate arrays in preparation.

---
  hw/input/stellaris_gamepad.c | 43 ++++++++++++------------------------
  1 file changed, 14 insertions(+), 29 deletions(-)
hw/input/stellaris_gamepad.c +0 -10
-static const VMStateDescription vmstate_stellaris_button = {
-    .name = "stellaris_button",
-    .version_id = 0,
-    .minimum_version_id = 0,
-    .fields = (VMStateField[]) {
-        VMSTATE_UINT8(pressed, StellarisGamepadButton),
-        VMSTATE_END_OF_LIST()
-    }
-};
-
  static const VMStateDescription vmstate_stellaris_gamepad = {
      .name = "stellaris_gamepad",

hw/input/stellaris_gamepad.c static void stellaris_gamepad_put_key(void * opaque, int keycode) · line 55

Quoted 10 lines
hw/input/stellaris_gamepad.c +2 -4
      .version_id = 2,
      .minimum_version_id = 2,
      .fields = (VMStateField[]) {
          VMSTATE_INT32(extension, StellarisGamepad),
-        VMSTATE_STRUCT_VARRAY_POINTER_INT32(buttons, StellarisGamepad,
-                                            num_buttons,
-                                            vmstate_stellaris_button,
-                                            StellarisGamepadButton),
+        VMSTATE_VARRAY_UINT32(pressed, StellarisGamepad, num_buttons,
+                              0, vmstate_info_uint8, uint8_t),

Don't this break the migration stream?

Quoted 3 lines
          VMSTATE_END_OF_LIST()
      }

};

#8 lore raw

Re: [PATCH 4/6] hw/input/stellaris_gamepad: Remove StellarisGamepadButton struct

Quoted 33 lines · Philippe Mathieu-Daudé

Hi Peter,

Quoted 29 lines · Peter Maydell

Currently for each button on the device we have a StellarisGamepadButton struct which has the irq, keycode and pressed state for it. When we convert to qdev, the qdev property and GPIO APIs are going to require that we have separate arrays for the irqs and keycodes. Convert from array-of-structs to three separate arrays in preparation.

---
  hw/input/stellaris_gamepad.c | 43 ++++++++++++------------------------
  1 file changed, 14 insertions(+), 29 deletions(-)
hw/input/stellaris_gamepad.c +0 -10
-static const VMStateDescription vmstate_stellaris_button = {
-    .name = "stellaris_button",
-    .version_id = 0,
-    .minimum_version_id = 0,
-    .fields = (VMStateField[]) {
-        VMSTATE_UINT8(pressed, StellarisGamepadButton),
-        VMSTATE_END_OF_LIST()
-    }
-};
-
  static const VMStateDescription vmstate_stellaris_gamepad = {
      .name = "stellaris_gamepad",
      .version_id = 2,
      .minimum_version_id = 2,
      .fields = (VMStateField[]) {
          VMSTATE_INT32(extension, StellarisGamepad),
Quoted 8 lines

hw/input/stellaris_gamepad.c static void stellaris_gamepad_put_key(void * opaque, int keycode) · line 55

Quoted 6 lines
hw/input/stellaris_gamepad.c +2 -4
-        VMSTATE_STRUCT_VARRAY_POINTER_INT32(buttons, StellarisGamepad,
-                                            num_buttons,
-                                            vmstate_stellaris_button,
-                                            StellarisGamepadButton),
+        VMSTATE_VARRAY_UINT32(pressed, StellarisGamepad, num_buttons,
+                              0, vmstate_info_uint8, uint8_t),

Don't this break the migration stream?

Yes; this is OK because we don't care about migration compat for this board. But I forgot to mention it in the commit message, and we should bump the version_id fields too.

thanks
-- PMM

#9 lore raw

Re: [PATCH 4/6] hw/input/stellaris_gamepad: Remove StellarisGamepadButton struct

Quoted 43 lines · Peter Maydell
Quoted 33 lines · Philippe Mathieu-Daudé

Hi Peter,

Quoted 29 lines · Peter Maydell

Currently for each button on the device we have a StellarisGamepadButton struct which has the irq, keycode and pressed state for it. When we convert to qdev, the qdev property and GPIO APIs are going to require that we have separate arrays for the irqs and keycodes. Convert from array-of-structs to three separate arrays in preparation.

---
   hw/input/stellaris_gamepad.c | 43 ++++++++++++------------------------
   1 file changed, 14 insertions(+), 29 deletions(-)
hw/input/stellaris_gamepad.c +0 -10
-static const VMStateDescription vmstate_stellaris_button = {
-    .name = "stellaris_button",
-    .version_id = 0,
-    .minimum_version_id = 0,
-    .fields = (VMStateField[]) {
-        VMSTATE_UINT8(pressed, StellarisGamepadButton),
-        VMSTATE_END_OF_LIST()
-    }
-};
-
   static const VMStateDescription vmstate_stellaris_gamepad = {
       .name = "stellaris_gamepad",
       .version_id = 2,
       .minimum_version_id = 2,
       .fields = (VMStateField[]) {
           VMSTATE_INT32(extension, StellarisGamepad),
Quoted 8 lines

hw/input/stellaris_gamepad.c static void stellaris_gamepad_put_key(void * opaque, int keycode) · line 55

Quoted 6 lines
hw/input/stellaris_gamepad.c +2 -4
-        VMSTATE_STRUCT_VARRAY_POINTER_INT32(buttons, StellarisGamepad,
-                                            num_buttons,
-                                            vmstate_stellaris_button,
-                                            StellarisGamepadButton),
+        VMSTATE_VARRAY_UINT32(pressed, StellarisGamepad, num_buttons,
+                              0, vmstate_info_uint8, uint8_t),

Don't this break the migration stream?

Quoted 3 lines

Yes; this is OK because we don't care about migration compat for this board. But I forgot to mention it in the commit message, and we should bump the version_id fields too.

OK, this is what I thought (I'm still trying to correctly understand that myself). With that updated:

#10 lore raw

PATCH 5/6 hw/input/stellaris_input: Convert to qdev

In reply to Peter Maydell

Convert the hw/input/stellaris_input device to qdev.

The interface uses an array property for the board to specify the keycodes to use, so the s->keycodes memory is now allocated by the array-property machinery.

---
 include/hw/input/stellaris_gamepad.h | 25 +++++++++-
 hw/arm/stellaris.c                   | 26 +++++++---
 hw/input/stellaris_gamepad.c         | 73 +++++++++++++++++++---------
 3 files changed, 92 insertions(+), 32 deletions(-)
include/hw/input/stellaris_gamepad.h +23 -2
index 23cfd3c95f3..50c17041121 100644
@@ -11,8 +11,29 @@
 #ifndef HW_INPUT_STELLARIS_GAMEPAD_H
 #define HW_INPUT_STELLARIS_GAMEPAD_H
 
+#include "hw/sysbus.h"
+#include "qom/object.h"
 
-/* stellaris_gamepad.c */
-void stellaris_gamepad_init(int n, qemu_irq *irq, const int *keycode);
+/*
+ * QEMU interface:
+ *  + QOM array property "keycodes": uint32_t QEMU keycodes to handle
+ *  + unnamed GPIO outputs: one per keycode, in the same order as the
+ *    "keycodes" array property entries; asserted when key is down
+ */
+
+#define TYPE_STELLARIS_GAMEPAD "stellaris-gamepad"
+OBJECT_DECLARE_SIMPLE_TYPE(StellarisGamepad, STELLARIS_GAMEPAD)
+
+struct StellarisGamepad {
+    /*< private >*/
+    SysBusDevice parent_obj;
+
+    /*< public >*/
+    uint32_t num_buttons;
+    qemu_irq *irqs;
+    uint32_t *keycodes;
+    uint8_t *pressed;
+    int extension;
+};
 
 #endif
hw/arm/stellaris.c +19 -7
index 96585dd7106..707b0dae375 100644
@@ -31,6 +31,7 @@
 #include "hw/timer/stellaris-gptm.h"
 #include "hw/qdev-clock.h"
 #include "qom/object.h"
+#include "qapi/qmp/qlist.h"
 
 #define GPIO_A 0
 #define GPIO_B 1
@@ -1274,16 +1275,27 @@ static void stellaris_init(MachineState *ms, stellaris_board_info *board)
         sysbus_connect_irq(SYS_BUS_DEVICE(enet), 0, qdev_get_gpio_in(nvic, 42));
     }
     if (board->peripherals & BP_GAMEPAD) {
-        qemu_irq gpad_irq[5];
+        QList *gpad_keycode_list = qlist_new();
         static const int gpad_keycode[5] = { 0xc8, 0xd0, 0xcb, 0xcd, 0x1d };
+        DeviceState *gpad;
 
-        gpad_irq[0] = qemu_irq_invert(gpio_in[GPIO_E][0]); /* up */
-        gpad_irq[1] = qemu_irq_invert(gpio_in[GPIO_E][1]); /* down */
-        gpad_irq[2] = qemu_irq_invert(gpio_in[GPIO_E][2]); /* left */
-        gpad_irq[3] = qemu_irq_invert(gpio_in[GPIO_E][3]); /* right */
-        gpad_irq[4] = qemu_irq_invert(gpio_in[GPIO_F][1]); /* select */
+        gpad = qdev_new(TYPE_STELLARIS_GAMEPAD);
+        for (i = 0; i < ARRAY_SIZE(gpad_keycode); i++) {
+            qlist_append_int(gpad_keycode_list, gpad_keycode[i]);
+        }
+        qdev_prop_set_array(gpad, "keycodes", gpad_keycode_list);
+        sysbus_realize_and_unref(SYS_BUS_DEVICE(gpad), &error_fatal);
 
-        stellaris_gamepad_init(5, gpad_irq, gpad_keycode);
+        qdev_connect_gpio_out(gpad, 0,
+                              qemu_irq_invert(gpio_in[GPIO_E][0])); /* up */
+        qdev_connect_gpio_out(gpad, 1,
+                              qemu_irq_invert(gpio_in[GPIO_E][1])); /* down */
+        qdev_connect_gpio_out(gpad, 2,
+                              qemu_irq_invert(gpio_in[GPIO_E][2])); /* left */
+        qdev_connect_gpio_out(gpad, 3,
+                              qemu_irq_invert(gpio_in[GPIO_E][3])); /* right */
+        qdev_connect_gpio_out(gpad, 4,
+                              qemu_irq_invert(gpio_in[GPIO_F][1])); /* select */
     }
     for (i = 0; i < 7; i++) {
         if (board->dc4 & (1 << i)) {
hw/input/stellaris_gamepad.c +50 -23

91 lines not on this page · raw message

Signature · 1 line
2.34.1
Reviewed-by on v1 5/6
#11 lore raw

Re: [PATCH 5/6] hw/input/stellaris_input: Convert to qdev

Quoted 14 lines · Peter Maydell

Convert the hw/input/stellaris_input device to qdev.

The interface uses an array property for the board to specify the keycodes to use, so the s->keycodes memory is now allocated by the array-property machinery.

---
  include/hw/input/stellaris_gamepad.h | 25 +++++++++-
  hw/arm/stellaris.c                   | 26 +++++++---
  hw/input/stellaris_gamepad.c         | 73 +++++++++++++++++++---------
  3 files changed, 92 insertions(+), 32 deletions(-)

include/hw/input/stellaris_gamepad.h line 28

Quoted 5 lines
include/hw/input/stellaris_gamepad.h +5 -0
+#define TYPE_STELLARIS_GAMEPAD "stellaris-gamepad"
+OBJECT_DECLARE_SIMPLE_TYPE(StellarisGamepad, STELLARIS_GAMEPAD)
+
+struct StellarisGamepad {
+    /*< private >*/

Since commit 067109a11c ("docs/devel: mention the spacing requirement for QOM") we don't use these private/public comments anymore.

Otherwise,

Quoted 9 lines
include/hw/input/stellaris_gamepad.h +9 -0
+    SysBusDevice parent_obj;
+
+    /*< public >*/
+    uint32_t num_buttons;
+    qemu_irq *irqs;
+    uint32_t *keycodes;
+    uint8_t *pressed;
+    int extension;
+};
#12 lore raw

Re: [PATCH 5/6] hw/input/stellaris_input: Convert to qdev

Quoted 16 lines · Philippe Mathieu-Daudé
Quoted 12 lines · Peter Maydell

Convert the hw/input/stellaris_input device to qdev.

The interface uses an array property for the board to specify the keycodes to use, so the s->keycodes memory is now allocated by the array-property machinery.

---
  include/hw/input/stellaris_gamepad.h | 25 +++++++++-
  hw/arm/stellaris.c                   | 26 +++++++---
  hw/input/stellaris_gamepad.c         | 73 +++++++++++++++++++---------
  3 files changed, 92 insertions(+), 32 deletions(-)
Quoted 8 lines

include/hw/input/stellaris_gamepad.h line 28

Quoted 5 lines
include/hw/input/stellaris_gamepad.h +5 -0
+#define TYPE_STELLARIS_GAMEPAD "stellaris-gamepad"
+OBJECT_DECLARE_SIMPLE_TYPE(StellarisGamepad, STELLARIS_GAMEPAD)
+
+struct StellarisGamepad {
+    /*< private >*/

Since commit 067109a11c ("docs/devel: mention the spacing requirement for QOM") we don't use these private/public comments anymore.

Oh, good. I never really understood the purpose of them. We still have a lot of them in the codebase, though...

thanks
-- PMM

#13 lore raw

PATCH 6/6 hw/input/stellaris_gamepad: Convert to qemu_input_handler_register()

In reply to Peter Maydell

Now that we have converted to qdev, we can use the newer qemu_input_handler_register() API rather than the legacy qemu_add_kbd_event_handler().

Since we only have one user, take the opportunity to convert from scancodes to QCodes, rather than using qemu_input_key_value_to_scancode() (which adds an 0xe0 prefix and encodes up/down indication in the scancode, which our old handler function then had to reverse). That lets us drop the old state field which was tracking whether we were halfway through a two-byte scancode.

---
 include/hw/input/stellaris_gamepad.h |  2 +-
 hw/arm/stellaris.c                   |  6 ++++-
 hw/input/stellaris_gamepad.c         | 33 +++++++++++++---------------
 3 files changed, 21 insertions(+), 20 deletions(-)
include/hw/input/stellaris_gamepad.h +1 -1
index 50c17041121..979e7243fd8 100644
@@ -17,6 +17,7 @@
 /*
  * QEMU interface:
  *  + QOM array property "keycodes": uint32_t QEMU keycodes to handle
+ *    (these are QCodes, ie the Q_KEY_* values)
  *  + unnamed GPIO outputs: one per keycode, in the same order as the
  *    "keycodes" array property entries; asserted when key is down
  */
@@ -33,7 +34,6 @@ struct StellarisGamepad {
     qemu_irq *irqs;
     uint32_t *keycodes;
     uint8_t *pressed;
-    int extension;
 };
 
 #endif
hw/arm/stellaris.c +5 -1
index 707b0dae375..dd90f686bfa 100644
@@ -32,6 +32,7 @@
 #include "hw/qdev-clock.h"
 #include "qom/object.h"
 #include "qapi/qmp/qlist.h"
+#include "ui/input.h"
 
 #define GPIO_A 0
 #define GPIO_B 1
@@ -1276,7 +1277,10 @@ static void stellaris_init(MachineState *ms, stellaris_board_info *board)
     }
     if (board->peripherals & BP_GAMEPAD) {
         QList *gpad_keycode_list = qlist_new();
-        static const int gpad_keycode[5] = { 0xc8, 0xd0, 0xcb, 0xcd, 0x1d };
+        static const int gpad_keycode[5] = {
+            Q_KEY_CODE_UP, Q_KEY_CODE_DOWN, Q_KEY_CODE_LEFT,
+            Q_KEY_CODE_RIGHT, Q_KEY_CODE_CTRL,
+        };
         DeviceState *gpad;
 
         gpad = qdev_new(TYPE_STELLARIS_GAMEPAD);
hw/input/stellaris_gamepad.c +15 -18
index 48d37bd6275..65247ecd782 100644
@@ -15,28 +15,20 @@
 #include "migration/vmstate.h"
 #include "ui/console.h"
 
-static void stellaris_gamepad_put_key(void * opaque, int keycode)
+static void stellaris_gamepad_event(DeviceState *dev, QemuConsole *src,
+                                    InputEvent *evt)
 {
-    StellarisGamepad *s = (StellarisGamepad *)opaque;
+    StellarisGamepad *s = STELLARIS_GAMEPAD(dev);
+    InputKeyEvent *key = evt->u.key.data;
+    int qcode = qemu_input_key_value_to_qcode(key->key);
     int i;
-    int down;
-
-    if (keycode == 0xe0 && !s->extension) {
-        s->extension = 0x80;
-        return;
-    }
-
-    down = (keycode & 0x80) == 0;
-    keycode = (keycode & 0x7f) | s->extension;
 
     for (i = 0; i < s->num_buttons; i++) {
-        if (s->keycodes[i] == keycode && s->pressed[i] != down) {
-            s->pressed[i] = down;
-            qemu_set_irq(s->irqs[i], down);
+        if (s->keycodes[i] == qcode && s->pressed[i] != key->down) {
+            s->pressed[i] = key->down;
+            qemu_set_irq(s->irqs[i], key->down);
         }
     }
-
-    s->extension = 0;
 }
 
 static const VMStateDescription vmstate_stellaris_gamepad = {
@@ -44,13 +36,18 @@ static const VMStateDescription vmstate_stellaris_gamepad = {
     .version_id = 2,
     .minimum_version_id = 2,
     .fields = (VMStateField[]) {
-        VMSTATE_INT32(extension, StellarisGamepad),
         VMSTATE_VARRAY_UINT32(pressed, StellarisGamepad, num_buttons,
                               0, vmstate_info_uint8, uint8_t),
         VMSTATE_END_OF_LIST()
     }
 };
 
+static QemuInputHandler stellaris_gamepad_handler = {
+    .name = "Stellaris Gamepad",
+    .mask = INPUT_EVENT_MASK_KEY,
+    .event = stellaris_gamepad_event,
+};
+
 static void stellaris_gamepad_realize(DeviceState *dev, Error **errp)
 {
     StellarisGamepad *s = STELLARIS_GAMEPAD(dev);
@@ -63,7 +60,7 @@ static void stellaris_gamepad_realize(DeviceState *dev, Error **errp)
     s->irqs = g_new0(qemu_irq, s->num_buttons);
     s->pressed = g_new0(uint8_t, s->num_buttons);
     qdev_init_gpio_out(dev, s->irqs, s->num_buttons);
-    qemu_add_kbd_event_handler(stellaris_gamepad_put_key, dev);
+    qemu_input_handler_register(dev, &stellaris_gamepad_handler);
 }
 
 static void stellaris_gamepad_reset_enter(Object *obj, ResetType type)
Signature · 1 line
2.34.1
Reviewed-by on v1 6/6
#14 lore raw

Re: [PATCH 6/6] hw/input/stellaris_gamepad: Convert to qemu_input_handler_register()

Quoted 22 lines · Peter Maydell

Now that we have converted to qdev, we can use the newer qemu_input_handler_register() API rather than the legacy qemu_add_kbd_event_handler().

Since we only have one user, take the opportunity to convert from scancodes to QCodes, rather than using qemu_input_key_value_to_scancode() (which adds an 0xe0 prefix and encodes up/down indication in the scancode, which our old handler function then had to reverse). That lets us drop the old state field which was tracking whether we were halfway through a two-byte scancode.

---
  include/hw/input/stellaris_gamepad.h |  2 +-
  hw/arm/stellaris.c                   |  6 ++++-
  hw/input/stellaris_gamepad.c         | 33 +++++++++++++---------------
  3 files changed, 21 insertions(+), 20 deletions(-)
  static const VMStateDescription vmstate_stellaris_gamepad = {

hw/input/stellaris_gamepad.c static const VMStateDescription vmstate_stellaris_gamepad = { · line 37

Quoted 2 lines
hw/input/stellaris_gamepad.c +0 -0
@@ -44,13 +36,18 @@ static const VMStateDescription vmstate_stellaris_gamepad = {
      .version_id = 2,
      .minimum_version_id = 2,

Updating the migration version:

Quoted 7 lines
hw/input/stellaris_gamepad.c +0 -1
      .fields = (VMStateField[]) {
-        VMSTATE_INT32(extension, StellarisGamepad),
          VMSTATE_VARRAY_UINT32(pressed, StellarisGamepad, num_buttons,
                                0, vmstate_info_uint8, uint8_t),
          VMSTATE_END_OF_LIST()
      }
  };
#15 lore raw

Re: [PATCH 6/6] hw/input/stellaris_gamepad: Convert to qemu_input_handler_register()

In reply to Peter Maydell

Hi Peter,

Quoted 12 lines · Peter Maydell

Now that we have converted to qdev, we can use the newer qemu_input_handler_register() API rather than the legacy qemu_add_kbd_event_handler().

Since we only have one user, take the opportunity to convert from scancodes to QCodes, rather than using qemu_input_key_value_to_scancode() (which adds an 0xe0 prefix and encodes up/down indication in the scancode, which our old handler function then had to reverse). That lets us drop the old state field which was tracking whether we were halfway through a two-byte scancode.

Quoted 6 lines
---
  include/hw/input/stellaris_gamepad.h |  2 +-
  hw/arm/stellaris.c                   |  6 ++++-
  hw/input/stellaris_gamepad.c         | 33 +++++++++++++---------------
  3 files changed, 21 insertions(+), 20 deletions(-)

If https://lore.kernel.org/qemu-devel/[email protected]/
gets merged before you respin, this structure needs to be declared const:

Quoted 5 lines
hw/input/stellaris_gamepad.c +5 -0
+static QemuInputHandler stellaris_gamepad_handler = {
+    .name = "Stellaris Gamepad",
+    .mask = INPUT_EVENT_MASK_KEY,
+    .event = stellaris_gamepad_event,
+};
Tested-by on the v1 series
#16 lore raw
In reply to Peter Maydell
Quoted 21 lines · Peter Maydell

(I had this all ready to go before I went off on holiday two weeks ago, except I forgot to actually *send* the emails...)

This patchseries converts the stellaris board's gamepad input device to qdev. This isn't a very important bit of conversion (I was just looking for a small tail-end-of-the-week task), but it does reduce by one the number of users of a couple of legacy APIs: vmstate_register() and qemu_add_kbd_event_handler().

I've included Kevin's qdev_prop_set_array() patch here, because I wanted an array property and it doesn't seem sensible to write it the old way and have another thing that needs converting. I'm assuming that by the time this patchset gets reviewed and committed that one will already be upstream.

thanks
-- PMM

Kevin Wolf (1):
qdev: Add qdev_prop_set_array()

Quoted 6 lines

Peter Maydell (5):
hw/input/stellaris_input: Rename to stellaris_gamepad hw/input/stellaris_gamepad: Rename structs to our usual convention hw/input/stellaris_gamepad: Remove StellarisGamepadButton struct hw/input/stellaris_input: Convert to qdev hw/input/stellaris_gamepad: Convert to qemu_input_handler_register()

#17 lore raw
Quoted 23 lines · Philippe Mathieu-Daudé
Quoted 21 lines · Peter Maydell

(I had this all ready to go before I went off on holiday two weeks ago, except I forgot to actually *send* the emails...)

This patchseries converts the stellaris board's gamepad input device to qdev. This isn't a very important bit of conversion (I was just looking for a small tail-end-of-the-week task), but it does reduce by one the number of users of a couple of legacy APIs: vmstate_register() and qemu_add_kbd_event_handler().

I've included Kevin's qdev_prop_set_array() patch here, because I wanted an array property and it doesn't seem sensible to write it the old way and have another thing that needs converting. I'm assuming that by the time this patchset gets reviewed and committed that one will already be upstream.

thanks
-- PMM

Kevin Wolf (1):
qdev: Add qdev_prop_set_array()

Quoted 8 lines
Quoted 6 lines

Peter Maydell (5):
hw/input/stellaris_input: Rename to stellaris_gamepad hw/input/stellaris_gamepad: Rename structs to our usual convention hw/input/stellaris_gamepad: Remove StellarisGamepadButton struct hw/input/stellaris_input: Convert to qdev hw/input/stellaris_gamepad: Convert to qemu_input_handler_register()

Thanks. I actually have a v2 of this ready but am waiting on a decision about whether the qdev_prop_set_array() API is OK before I send it.

-- PMM

#18 lore raw
Quoted 31 lines · Peter Maydell
Quoted 30 lines · Philippe Mathieu-Daudé
Quoted 27 lines · Peter Maydell

(I had this all ready to go before I went off on holiday two weeks ago, except I forgot to actually *send* the emails...)

This patchseries converts the stellaris board's gamepad input device to qdev. This isn't a very important bit of conversion (I was just looking for a small tail-end-of-the-week task), but it does reduce by one the number of users of a couple of legacy APIs: vmstate_register() and qemu_add_kbd_event_handler().

I've included Kevin's qdev_prop_set_array() patch here, because I wanted an array property and it doesn't seem sensible to write it the old way and have another thing that needs converting. I'm assuming that by the time this patchset gets reviewed and committed that one will already be upstream.

thanks
-- PMM

Kevin Wolf (1):
qdev: Add qdev_prop_set_array()

Peter Maydell (5):
hw/input/stellaris_input: Rename to stellaris_gamepad hw/input/stellaris_gamepad: Rename structs to our usual convention hw/input/stellaris_gamepad: Remove StellarisGamepadButton struct hw/input/stellaris_input: Convert to qdev hw/input/stellaris_gamepad: Convert to qemu_input_handler_register()

Quoted 4 lines
Quoted 1 line

Thanks. I actually have a v2 of this ready but am waiting on a decision about whether the qdev_prop_set_array() API is OK before I send it.

Oh we just crossed then :)

I'm happy with qdev_prop_set_array() for our needs but I don't think you are waiting for my particular input there, rather Kevin / Markus.