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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
/*
* This work is licensed under the terms of the GNU GPL, version 2 or
* (at your option) any later version. See the COPYING file in the
* top-level directory.
*/
#include "qemu/osdep.h"
#include "qapi/error.h"
#include "qemu/module.h"
#include "qemu/sockets.h"
#include "hw/virtio/virtio.h"
#include "hw/qdev-properties.h"
#include "hw/virtio/virtio-input.h"
#include <sys/ioctl.h>
#include "standard-headers/linux/input.h"
/* ----------------------------------------------------------------- */
static struct virtio_input_config virtio_input_host_config[] = {
{ /* empty list */ },
};
static void virtio_input_host_event(void *opaque)
{
VirtIOInputHost *vih = opaque;
VirtIOInput *vinput = VIRTIO_INPUT(vih);
struct virtio_input_event virtio;
struct input_event evdev;
int rc;
for (;;) {
rc = read(vih->fd, &evdev, sizeof(evdev));
if (rc != sizeof(evdev)) {
break;
}
virtio.type = cpu_to_le16(evdev.type);
virtio.code = cpu_to_le16(evdev.code);
virtio.value = cpu_to_le32(evdev.value);
virtio_input_send(vinput, &virtio);
}
}
static void virtio_input_bits_config(VirtIOInputHost *vih,
int type, int count)
{
virtio_input_config bits;
int rc, i, size = 0;
memset(&bits, 0, sizeof(bits));
rc = ioctl(vih->fd, EVIOCGBIT(type, count/8), bits.u.bitmap);
if (rc < 0) {
return;
}
for (i = 0; i < count/8; i++) {
if (bits.u.bitmap[i]) {
size = i+1;
}
}
if (size == 0) {
return;
}
bits.select = VIRTIO_INPUT_CFG_EV_BITS;
bits.subsel = type;
bits.size = size;
virtio_input_add_config(VIRTIO_INPUT(vih), &bits);
}
static void virtio_input_abs_config(VirtIOInputHost *vih, int axis)
{
virtio_input_config config;
struct input_absinfo absinfo;
int rc;
rc = ioctl(vih->fd, EVIOCGABS(axis), &absinfo);
if (rc < 0) {
return;
}
memset(&config, 0, sizeof(config));
config.select = VIRTIO_INPUT_CFG_ABS_INFO;
config.subsel = axis;
config.size = sizeof(virtio_input_absinfo);
config.u.abs.min = cpu_to_le32(absinfo.minimum);
config.u.abs.max = cpu_to_le32(absinfo.maximum);
config.u.abs.fuzz = cpu_to_le32(absinfo.fuzz);
config.u.abs.flat = cpu_to_le32(absinfo.flat);
config.u.abs.res = cpu_to_le32(absinfo.resolution);
virtio_input_add_config(VIRTIO_INPUT(vih), &config);
}
static void virtio_input_host_realize(DeviceState *dev, Error **errp)
{
VirtIOInputHost *vih = VIRTIO_INPUT_HOST(dev);
VirtIOInput *vinput = VIRTIO_INPUT(dev);
virtio_input_config id, *abs;
struct input_id ids;
int rc, ver, i, axis;
uint8_t byte;
if (!vih->evdev) {
error_setg(errp, "evdev property is required");
return;
}
vih->fd = open(vih->evdev, O_RDWR);
if (vih->fd < 0) {
error_setg_file_open(errp, errno, vih->evdev);
return;
}
qemu_set_nonblock(vih->fd);
rc = ioctl(vih->fd, EVIOCGVERSION, &ver);
if (rc < 0) {
error_setg(errp, "%s: is not an evdev device", vih->evdev);
goto err_close;
}
rc = ioctl(vih->fd, EVIOCGRAB, 1);
if (rc < 0) {
error_setg_errno(errp, errno, "%s: failed to get exclusive access",
vih->evdev);
goto err_close;
}
memset(&id, 0, sizeof(id));
ioctl(vih->fd, EVIOCGNAME(sizeof(id.u.string)-1), id.u.string);
id.select = VIRTIO_INPUT_CFG_ID_NAME;
id.size = strlen(id.u.string);
virtio_input_add_config(vinput, &id);
if (ioctl(vih->fd, EVIOCGID, &ids) == 0) {
memset(&id, 0, sizeof(id));
id.select = VIRTIO_INPUT_CFG_ID_DEVIDS;
id.size = sizeof(struct virtio_input_devids);
id.u.ids.bustype = cpu_to_le16(ids.bustype);
id.u.ids.vendor = cpu_to_le16(ids.vendor);
id.u.ids.product = cpu_to_le16(ids.product);
id.u.ids.version = cpu_to_le16(ids.version);
virtio_input_add_config(vinput, &id);
}
virtio_input_bits_config(vih, EV_KEY, KEY_CNT);
virtio_input_bits_config(vih, EV_REL, REL_CNT);
virtio_input_bits_config(vih, EV_ABS, ABS_CNT);
virtio_input_bits_config(vih, EV_MSC, MSC_CNT);
virtio_input_bits_config(vih, EV_SW, SW_CNT);
virtio_input_bits_config(vih, EV_LED, LED_CNT);
abs = virtio_input_find_config(VIRTIO_INPUT(vih),
VIRTIO_INPUT_CFG_EV_BITS, EV_ABS);
if (abs) {
for (i = 0; i < abs->size; i++) {
byte = abs->u.bitmap[i];
axis = 8 * i;
while (byte) {
if (byte & 1) {
virtio_input_abs_config(vih, axis);
}
axis++;
byte >>= 1;
}
}
}
qemu_set_fd_handler(vih->fd, virtio_input_host_event, NULL, vih);
return;
err_close:
close(vih->fd);
vih->fd = -1;
return;
}
static void virtio_input_host_unrealize(DeviceState *dev, Error **errp)
{
VirtIOInputHost *vih = VIRTIO_INPUT_HOST(dev);
if (vih->fd > 0) {
qemu_set_fd_handler(vih->fd, NULL, NULL, NULL);
close(vih->fd);
}
}
static void virtio_input_host_handle_status(VirtIOInput *vinput,
virtio_input_event *event)
{
VirtIOInputHost *vih = VIRTIO_INPUT_HOST(vinput);
struct input_event evdev;
int rc;
if (gettimeofday(&evdev.time, NULL)) {
perror("virtio_input_host_handle_status: gettimeofday");
return;
}
evdev.type = le16_to_cpu(event->type);
evdev.code = le16_to_cpu(event->code);
evdev.value = le32_to_cpu(event->value);
rc = write(vih->fd, &evdev, sizeof(evdev));
if (rc == -1) {
perror("virtio_input_host_handle_status: write");
}
}
static const VMStateDescription vmstate_virtio_input_host = {
.name = "virtio-input-host",
.unmigratable = 1,
};
static Property virtio_input_host_properties[] = {
DEFINE_PROP_STRING("evdev", VirtIOInputHost, evdev),
DEFINE_PROP_END_OF_LIST(),
};
static void virtio_input_host_class_init(ObjectClass *klass, void *data)
{
VirtIOInputClass *vic = VIRTIO_INPUT_CLASS(klass);
DeviceClass *dc = DEVICE_CLASS(klass);
dc->vmsd = &vmstate_virtio_input_host;
device_class_set_props(dc, virtio_input_host_properties);
vic->realize = virtio_input_host_realize;
vic->unrealize = virtio_input_host_unrealize;
vic->handle_status = virtio_input_host_handle_status;
}
static void virtio_input_host_init(Object *obj)
{
VirtIOInput *vinput = VIRTIO_INPUT(obj);
virtio_input_init_config(vinput, virtio_input_host_config);
}
static const TypeInfo virtio_input_host_info = {
.name = TYPE_VIRTIO_INPUT_HOST,
.parent = TYPE_VIRTIO_INPUT,
.instance_size = sizeof(VirtIOInputHost),
.instance_init = virtio_input_host_init,
.class_init = virtio_input_host_class_init,
};
/* ----------------------------------------------------------------- */
static void virtio_register_types(void)
{
type_register_static(&virtio_input_host_info);
}
type_init(virtio_register_types)
|