1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include "qemu/osdep.h"
26#include "hw/irq.h"
27#include "hw/sysbus.h"
28#include "migration/vmstate.h"
29#include "qemu/module.h"
30#include "qemu/timer.h"
31
32#define TYPE_GPIOKEY "gpio-key"
33#define GPIOKEY(obj) OBJECT_CHECK(GPIOKEYState, (obj), TYPE_GPIOKEY)
34#define GPIO_KEY_LATENCY 100
35
36typedef struct GPIOKEYState {
37 SysBusDevice parent_obj;
38
39 QEMUTimer *timer;
40 qemu_irq irq;
41} GPIOKEYState;
42
43static const VMStateDescription vmstate_gpio_key = {
44 .name = "gpio-key",
45 .version_id = 1,
46 .minimum_version_id = 1,
47 .fields = (VMStateField[]) {
48 VMSTATE_TIMER_PTR(timer, GPIOKEYState),
49 VMSTATE_END_OF_LIST()
50 }
51};
52
53static void gpio_key_reset(DeviceState *dev)
54{
55 GPIOKEYState *s = GPIOKEY(dev);
56
57 timer_del(s->timer);
58}
59
60static void gpio_key_timer_expired(void *opaque)
61{
62 GPIOKEYState *s = (GPIOKEYState *)opaque;
63
64 qemu_set_irq(s->irq, 0);
65 timer_del(s->timer);
66}
67
68static void gpio_key_set_irq(void *opaque, int irq, int level)
69{
70 GPIOKEYState *s = (GPIOKEYState *)opaque;
71
72 qemu_set_irq(s->irq, 1);
73 timer_mod(s->timer,
74 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + GPIO_KEY_LATENCY);
75}
76
77static void gpio_key_realize(DeviceState *dev, Error **errp)
78{
79 GPIOKEYState *s = GPIOKEY(dev);
80 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
81
82 sysbus_init_irq(sbd, &s->irq);
83 qdev_init_gpio_in(dev, gpio_key_set_irq, 1);
84 s->timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, gpio_key_timer_expired, s);
85}
86
87static void gpio_key_class_init(ObjectClass *klass, void *data)
88{
89 DeviceClass *dc = DEVICE_CLASS(klass);
90
91 dc->realize = gpio_key_realize;
92 dc->vmsd = &vmstate_gpio_key;
93 dc->reset = &gpio_key_reset;
94}
95
96static const TypeInfo gpio_key_info = {
97 .name = TYPE_GPIOKEY,
98 .parent = TYPE_SYS_BUS_DEVICE,
99 .instance_size = sizeof(GPIOKEYState),
100 .class_init = gpio_key_class_init,
101};
102
103static void gpio_key_register_types(void)
104{
105 type_register_static(&gpio_key_info);
106}
107
108type_init(gpio_key_register_types)
109