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#include "qom/object.h"
32
33#define TYPE_GPIOKEY "gpio-key"
34OBJECT_DECLARE_SIMPLE_TYPE(GPIOKEYState, GPIOKEY)
35#define GPIO_KEY_LATENCY 100
36
37struct GPIOKEYState {
38 SysBusDevice parent_obj;
39
40 QEMUTimer *timer;
41 qemu_irq irq;
42};
43
44static const VMStateDescription vmstate_gpio_key = {
45 .name = "gpio-key",
46 .version_id = 1,
47 .minimum_version_id = 1,
48 .fields = (VMStateField[]) {
49 VMSTATE_TIMER_PTR(timer, GPIOKEYState),
50 VMSTATE_END_OF_LIST()
51 }
52};
53
54static void gpio_key_reset(DeviceState *dev)
55{
56 GPIOKEYState *s = GPIOKEY(dev);
57
58 timer_del(s->timer);
59}
60
61static void gpio_key_timer_expired(void *opaque)
62{
63 GPIOKEYState *s = (GPIOKEYState *)opaque;
64
65 qemu_set_irq(s->irq, 0);
66 timer_del(s->timer);
67}
68
69static void gpio_key_set_irq(void *opaque, int irq, int level)
70{
71 GPIOKEYState *s = (GPIOKEYState *)opaque;
72
73 qemu_set_irq(s->irq, 1);
74 timer_mod(s->timer,
75 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + GPIO_KEY_LATENCY);
76}
77
78static void gpio_key_realize(DeviceState *dev, Error **errp)
79{
80 GPIOKEYState *s = GPIOKEY(dev);
81 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
82
83 sysbus_init_irq(sbd, &s->irq);
84 qdev_init_gpio_in(dev, gpio_key_set_irq, 1);
85 s->timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, gpio_key_timer_expired, s);
86}
87
88static void gpio_key_class_init(ObjectClass *klass, void *data)
89{
90 DeviceClass *dc = DEVICE_CLASS(klass);
91
92 dc->realize = gpio_key_realize;
93 dc->vmsd = &vmstate_gpio_key;
94 dc->reset = &gpio_key_reset;
95}
96
97static const TypeInfo gpio_key_info = {
98 .name = TYPE_GPIOKEY,
99 .parent = TYPE_SYS_BUS_DEVICE,
100 .instance_size = sizeof(GPIOKEYState),
101 .class_init = gpio_key_class_init,
102};
103
104static void gpio_key_register_types(void)
105{
106 type_register_static(&gpio_key_info);
107}
108
109type_init(gpio_key_register_types)
110