xref: /openbmc/qemu/hw/input/stellaris_gamepad.c (revision 806f71ee)
1 /*
2  * Gamepad style buttons connected to IRQ/GPIO lines
3  *
4  * Copyright (c) 2007 CodeSourcery.
5  * Written by Paul Brook
6  *
7  * This code is licensed under the GPL.
8  */
9 
10 #include "qemu/osdep.h"
11 #include "qapi/error.h"
12 #include "hw/input/stellaris_gamepad.h"
13 #include "hw/irq.h"
14 #include "hw/qdev-properties.h"
15 #include "migration/vmstate.h"
16 #include "ui/console.h"
17 
18 static void stellaris_gamepad_event(DeviceState *dev, QemuConsole *src,
19                                     InputEvent *evt)
20 {
21     StellarisGamepad *s = STELLARIS_GAMEPAD(dev);
22     InputKeyEvent *key = evt->u.key.data;
23     int qcode = qemu_input_key_value_to_qcode(key->key);
24     int i;
25 
26     for (i = 0; i < s->num_buttons; i++) {
27         if (s->keycodes[i] == qcode && s->pressed[i] != key->down) {
28             s->pressed[i] = key->down;
29             qemu_set_irq(s->irqs[i], key->down);
30         }
31     }
32 }
33 
34 static const VMStateDescription vmstate_stellaris_gamepad = {
35     .name = "stellaris_gamepad",
36     .version_id = 4,
37     .minimum_version_id = 4,
38     .fields = (VMStateField[]) {
39         VMSTATE_VARRAY_UINT32(pressed, StellarisGamepad, num_buttons,
40                               0, vmstate_info_uint8, uint8_t),
41         VMSTATE_END_OF_LIST()
42     }
43 };
44 
45 static const QemuInputHandler stellaris_gamepad_handler = {
46     .name = "Stellaris Gamepad",
47     .mask = INPUT_EVENT_MASK_KEY,
48     .event = stellaris_gamepad_event,
49 };
50 
51 static void stellaris_gamepad_realize(DeviceState *dev, Error **errp)
52 {
53     StellarisGamepad *s = STELLARIS_GAMEPAD(dev);
54 
55     if (s->num_buttons == 0) {
56         error_setg(errp, "keycodes property array must be set");
57         return;
58     }
59 
60     s->irqs = g_new0(qemu_irq, s->num_buttons);
61     s->pressed = g_new0(uint8_t, s->num_buttons);
62     qdev_init_gpio_out(dev, s->irqs, s->num_buttons);
63     qemu_input_handler_register(dev, &stellaris_gamepad_handler);
64 }
65 
66 static void stellaris_gamepad_reset_enter(Object *obj, ResetType type)
67 {
68     StellarisGamepad *s = STELLARIS_GAMEPAD(obj);
69 
70     memset(s->pressed, 0, s->num_buttons * sizeof(uint8_t));
71 }
72 
73 static Property stellaris_gamepad_properties[] = {
74     DEFINE_PROP_ARRAY("keycodes", StellarisGamepad, num_buttons,
75                       keycodes, qdev_prop_uint32, uint32_t),
76     DEFINE_PROP_END_OF_LIST(),
77 };
78 
79 static void stellaris_gamepad_class_init(ObjectClass *klass, void *data)
80 {
81     DeviceClass *dc = DEVICE_CLASS(klass);
82     ResettableClass *rc = RESETTABLE_CLASS(klass);
83 
84     rc->phases.enter = stellaris_gamepad_reset_enter;
85     dc->realize = stellaris_gamepad_realize;
86     dc->vmsd = &vmstate_stellaris_gamepad;
87     device_class_set_props(dc, stellaris_gamepad_properties);
88 }
89 
90 static const TypeInfo stellaris_gamepad_info[] = {
91     {
92         .name = TYPE_STELLARIS_GAMEPAD,
93         .parent = TYPE_SYS_BUS_DEVICE,
94         .instance_size = sizeof(StellarisGamepad),
95         .class_init = stellaris_gamepad_class_init,
96     },
97 };
98 
99 DEFINE_TYPES(stellaris_gamepad_info);
100