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