Coverage Report

Created: 2026-11-20 08:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/hw/input/virtio-input.c
Line
Count
Source
1
/*
2
 * This work is licensed under the terms of the GNU GPL, version 2 or
3
 * (at your option) any later version.  See the COPYING file in the
4
 * top-level directory.
5
 */
6
7
#include "qemu/osdep.h"
8
#include "qapi/error.h"
9
#include "qemu/iov.h"
10
#include "qemu/module.h"
11
#include "trace.h"
12
13
#include "hw/virtio/virtio.h"
14
#include "hw/core/qdev-properties.h"
15
#include "hw/virtio/virtio-input.h"
16
17
#include "standard-headers/linux/input.h"
18
19
#define VIRTIO_INPUT_VM_VERSION 1
20
21
/* ----------------------------------------------------------------- */
22
23
void virtio_input_send(VirtIOInput *vinput, virtio_input_event *event)
24
0
{
25
0
    VirtQueueElement *elem;
26
0
    int i, len;
27
28
0
    if (!vinput->active) {
29
0
        return;
30
0
    }
31
32
    /* queue up events ... */
33
0
    if (vinput->qindex == vinput->qsize) {
34
0
        vinput->qsize++;
35
0
        vinput->queue = g_realloc(vinput->queue, vinput->qsize *
36
0
                                  sizeof(vinput->queue[0]));
37
0
    }
38
0
    vinput->queue[vinput->qindex++].event = *event;
39
40
    /* ... until we see a report sync ... */
41
0
    if (event->type != cpu_to_le16(EV_SYN) ||
42
0
        event->code != cpu_to_le16(SYN_REPORT)) {
43
0
        return;
44
0
    }
45
46
    /* ... then check available space ... */
47
0
    for (i = 0; i < vinput->qindex; i++) {
48
0
        elem = virtqueue_pop(vinput->evt, sizeof(VirtQueueElement));
49
0
        if (!elem) {
50
0
            while (--i >= 0) {
51
0
                virtqueue_unpop(vinput->evt, vinput->queue[i].elem, 0);
52
0
            }
53
0
            vinput->qindex = 0;
54
0
            trace_virtio_input_queue_full();
55
0
            return;
56
0
        }
57
0
        vinput->queue[i].elem = elem;
58
0
    }
59
60
    /* ... and finally pass them to the guest */
61
0
    for (i = 0; i < vinput->qindex; i++) {
62
0
        elem = vinput->queue[i].elem;
63
0
        len = iov_from_buf(elem->in_sg, elem->in_num,
64
0
                           0, &vinput->queue[i].event, sizeof(virtio_input_event));
65
0
        virtqueue_push(vinput->evt, elem, len);
66
0
        g_free(elem);
67
0
    }
68
0
    virtio_notify(VIRTIO_DEVICE(vinput), vinput->evt);
69
0
    vinput->qindex = 0;
70
0
}
71
72
static void virtio_input_handle_evt(VirtIODevice *vdev, VirtQueue *vq)
73
1.34k
{
74
    /* nothing */
75
1.34k
}
76
77
static void virtio_input_handle_sts(VirtIODevice *vdev, VirtQueue *vq)
78
26.9k
{
79
26.9k
    VirtIOInputClass *vic = VIRTIO_INPUT_GET_CLASS(vdev);
80
26.9k
    VirtIOInput *vinput = VIRTIO_INPUT(vdev);
81
26.9k
    virtio_input_event event;
82
26.9k
    VirtQueueElement *elem;
83
26.9k
    int len;
84
85
317k
    for (;;) {
86
317k
        elem = virtqueue_pop(vinput->sts, sizeof(VirtQueueElement));
87
317k
        if (!elem) {
88
26.9k
            break;
89
26.9k
        }
90
91
290k
        memset(&event, 0, sizeof(event));
92
290k
        len = iov_to_buf(elem->out_sg, elem->out_num,
93
290k
                         0, &event, sizeof(event));
94
290k
        if (vic->handle_status) {
95
290k
            vic->handle_status(vinput, &event);
96
290k
        }
97
290k
        virtqueue_push(vinput->sts, elem, len);
98
290k
        g_free(elem);
99
290k
    }
100
26.9k
    virtio_notify(vdev, vinput->sts);
101
26.9k
}
102
103
virtio_input_config *virtio_input_find_config(VirtIOInput *vinput,
104
                                              uint8_t select,
105
                                              uint8_t subsel)
106
10.2k
{
107
10.2k
    VirtIOInputConfig *cfg;
108
109
40.7k
    QTAILQ_FOREACH(cfg, &vinput->cfg_list, node) {
110
40.7k
        if (select == cfg->config.select &&
111
222
            subsel == cfg->config.subsel) {
112
33
            return &cfg->config;
113
33
        }
114
40.7k
    }
115
10.1k
    return NULL;
116
10.2k
}
117
118
void virtio_input_add_config(VirtIOInput *vinput,
119
                             virtio_input_config *config)
120
8
{
121
8
    VirtIOInputConfig *cfg;
122
123
8
    if (virtio_input_find_config(vinput, config->select, config->subsel)) {
124
        /* should not happen */
125
0
        fprintf(stderr, "%s: duplicate config: %d/%d\n",
126
0
                __func__, config->select, config->subsel);
127
0
        abort();
128
0
    }
129
130
8
    cfg = g_new0(VirtIOInputConfig, 1);
131
8
    cfg->config = *config;
132
8
    QTAILQ_INSERT_TAIL(&vinput->cfg_list, cfg, node);
133
8
}
134
135
void virtio_input_init_config(VirtIOInput *vinput,
136
                              virtio_input_config *config)
137
2
{
138
2
    int i = 0;
139
140
2
    QTAILQ_INIT(&vinput->cfg_list);
141
8
    while (config[i].select) {
142
6
        virtio_input_add_config(vinput, config + i);
143
6
        i++;
144
6
    }
145
2
}
146
147
void virtio_input_idstr_config(VirtIOInput *vinput,
148
                               uint8_t select, const char *string)
149
2
{
150
2
    virtio_input_config id;
151
152
2
    if (!string) {
153
2
        return;
154
2
    }
155
0
    memset(&id, 0, sizeof(id));
156
0
    id.select = select;
157
0
    id.size = snprintf(id.u.string, sizeof(id.u.string), "%s", string);
158
0
    virtio_input_add_config(vinput, &id);
159
0
}
160
161
static void virtio_input_get_config(VirtIODevice *vdev, uint8_t *config_data)
162
10.2k
{
163
10.2k
    VirtIOInput *vinput = VIRTIO_INPUT(vdev);
164
10.2k
    virtio_input_config *config;
165
166
10.2k
    config = virtio_input_find_config(vinput, vinput->cfg_select,
167
10.2k
                                      vinput->cfg_subsel);
168
10.2k
    if (config) {
169
33
        memcpy(config_data, config, vinput->cfg_size);
170
10.1k
    } else {
171
10.1k
        memset(config_data, 0, vinput->cfg_size);
172
10.1k
    }
173
10.2k
}
174
175
static void virtio_input_set_config(VirtIODevice *vdev,
176
                                    const uint8_t *config_data)
177
70.8k
{
178
70.8k
    VirtIOInput *vinput = VIRTIO_INPUT(vdev);
179
70.8k
    virtio_input_config *config = (virtio_input_config *)config_data;
180
181
70.8k
    vinput->cfg_select = config->select;
182
70.8k
    vinput->cfg_subsel = config->subsel;
183
70.8k
    virtio_notify_config(vdev);
184
70.8k
}
185
186
static uint64_t virtio_input_get_features(VirtIODevice *vdev, uint64_t f,
187
                                          Error **errp)
188
2
{
189
2
    return f;
190
2
}
191
192
static int virtio_input_set_status(VirtIODevice *vdev, uint8_t val)
193
100k
{
194
100k
    VirtIOInputClass *vic = VIRTIO_INPUT_GET_CLASS(vdev);
195
100k
    VirtIOInput *vinput = VIRTIO_INPUT(vdev);
196
197
100k
    if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
198
24.2k
        if (!vinput->active) {
199
11.3k
            vinput->active = true;
200
11.3k
            if (vic->change_active) {
201
11.3k
                vic->change_active(vinput);
202
11.3k
            }
203
11.3k
        }
204
24.2k
    }
205
100k
    return 0;
206
100k
}
207
208
static void virtio_input_reset(VirtIODevice *vdev)
209
28.2k
{
210
28.2k
    VirtIOInputClass *vic = VIRTIO_INPUT_GET_CLASS(vdev);
211
28.2k
    VirtIOInput *vinput = VIRTIO_INPUT(vdev);
212
213
28.2k
    if (vinput->active) {
214
11.3k
        vinput->active = false;
215
11.3k
        if (vic->change_active) {
216
11.3k
            vic->change_active(vinput);
217
11.3k
        }
218
11.3k
    }
219
28.2k
}
220
221
static int virtio_input_post_load(void *opaque, int version_id)
222
0
{
223
0
    VirtIOInput *vinput = opaque;
224
0
    VirtIOInputClass *vic = VIRTIO_INPUT_GET_CLASS(vinput);
225
0
    VirtIODevice *vdev = VIRTIO_DEVICE(vinput);
226
227
0
    vinput->active = vdev->status & VIRTIO_CONFIG_S_DRIVER_OK;
228
0
    if (vic->change_active) {
229
0
        vic->change_active(vinput);
230
0
    }
231
0
    return 0;
232
0
}
233
234
static void virtio_input_device_realize(DeviceState *dev, Error **errp)
235
2
{
236
2
    VirtIOInputClass *vic = VIRTIO_INPUT_GET_CLASS(dev);
237
2
    VirtIODevice *vdev = VIRTIO_DEVICE(dev);
238
2
    VirtIOInput *vinput = VIRTIO_INPUT(dev);
239
2
    VirtIOInputConfig *cfg;
240
2
    Error *local_err = NULL;
241
242
2
    if (vic->realize) {
243
2
        vic->realize(dev, &local_err);
244
2
        if (local_err) {
245
0
            error_propagate(errp, local_err);
246
0
            return;
247
0
        }
248
2
    }
249
250
2
    virtio_input_idstr_config(vinput, VIRTIO_INPUT_CFG_ID_SERIAL,
251
2
                              vinput->serial);
252
253
8
    QTAILQ_FOREACH(cfg, &vinput->cfg_list, node) {
254
8
        if (vinput->cfg_size < cfg->config.size) {
255
4
            vinput->cfg_size = cfg->config.size;
256
4
        }
257
8
    }
258
2
    vinput->cfg_size += 8;
259
2
    assert(vinput->cfg_size <= sizeof(virtio_input_config));
260
261
2
    virtio_init(vdev, VIRTIO_ID_INPUT, vinput->cfg_size);
262
2
    vinput->evt = virtio_add_queue(vdev, 64, virtio_input_handle_evt);
263
2
    vinput->sts = virtio_add_queue(vdev, 64, virtio_input_handle_sts);
264
2
}
265
266
static void virtio_input_finalize(Object *obj)
267
0
{
268
0
    VirtIOInput *vinput = VIRTIO_INPUT(obj);
269
0
    VirtIOInputConfig *cfg, *next;
270
271
0
    QTAILQ_FOREACH_SAFE(cfg, &vinput->cfg_list, node, next) {
272
0
        QTAILQ_REMOVE(&vinput->cfg_list, cfg, node);
273
0
        g_free(cfg);
274
0
    }
275
276
0
    g_free(vinput->queue);
277
0
}
278
279
static void virtio_input_device_unrealize(DeviceState *dev)
280
0
{
281
0
    VirtIOInputClass *vic = VIRTIO_INPUT_GET_CLASS(dev);
282
0
    VirtIODevice *vdev = VIRTIO_DEVICE(dev);
283
0
    VirtIOInput *vinput = VIRTIO_INPUT(dev);
284
285
0
    if (vic->unrealize) {
286
0
        vic->unrealize(dev);
287
0
    }
288
0
    virtio_delete_queue(vinput->evt);
289
0
    virtio_delete_queue(vinput->sts);
290
0
    virtio_cleanup(vdev);
291
0
}
292
293
static const VMStateDescription vmstate_virtio_input = {
294
    .name = "virtio-input",
295
    .minimum_version_id = VIRTIO_INPUT_VM_VERSION,
296
    .version_id = VIRTIO_INPUT_VM_VERSION,
297
    .fields = (const VMStateField[]) {
298
        VMSTATE_VIRTIO_DEVICE,
299
        VMSTATE_END_OF_LIST()
300
    },
301
    .post_load = virtio_input_post_load,
302
};
303
304
static const Property virtio_input_properties[] = {
305
    DEFINE_PROP_STRING("serial", VirtIOInput, serial),
306
};
307
308
static void virtio_input_class_init(ObjectClass *klass, const void *data)
309
63
{
310
63
    DeviceClass *dc = DEVICE_CLASS(klass);
311
63
    VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
312
313
63
    device_class_set_props(dc, virtio_input_properties);
314
63
    dc->vmsd           = &vmstate_virtio_input;
315
63
    set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
316
63
    vdc->realize      = virtio_input_device_realize;
317
63
    vdc->unrealize    = virtio_input_device_unrealize;
318
63
    vdc->get_config   = virtio_input_get_config;
319
63
    vdc->set_config   = virtio_input_set_config;
320
63
    vdc->get_features = virtio_input_get_features;
321
63
    vdc->set_status   = virtio_input_set_status;
322
63
    vdc->reset        = virtio_input_reset;
323
63
}
324
325
static const TypeInfo virtio_input_info = {
326
    .name          = TYPE_VIRTIO_INPUT,
327
    .parent        = TYPE_VIRTIO_DEVICE,
328
    .instance_size = sizeof(VirtIOInput),
329
    .class_size    = sizeof(VirtIOInputClass),
330
    .class_init    = virtio_input_class_init,
331
    .abstract      = true,
332
    .instance_finalize = virtio_input_finalize,
333
};
334
335
/* ----------------------------------------------------------------- */
336
337
static void virtio_register_types(void)
338
63
{
339
63
    type_register_static(&virtio_input_info);
340
63
}
341
342
type_init(virtio_register_types)