xref: /openbmc/qemu/hw/input/virtio-input.c (revision fafa4d50)
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/iov.h"
8 
9 #include "hw/qdev.h"
10 #include "hw/virtio/virtio.h"
11 #include "hw/virtio/virtio-input.h"
12 
13 #include "standard-headers/linux/input.h"
14 
15 /* ----------------------------------------------------------------- */
16 
17 void virtio_input_send(VirtIOInput *vinput, virtio_input_event *event)
18 {
19     VirtQueueElement elem;
20     unsigned have, need;
21     int i, len;
22 
23     /* queue up events ... */
24     if (vinput->qindex == vinput->qsize) {
25         vinput->qsize++;
26         vinput->queue = realloc(vinput->queue, vinput->qsize *
27                                 sizeof(virtio_input_event));
28     }
29     vinput->queue[vinput->qindex++] = *event;
30 
31     /* ... until we see a report sync ... */
32     if (event->type != cpu_to_le16(EV_SYN) ||
33         event->code != cpu_to_le16(SYN_REPORT)) {
34         return;
35     }
36 
37     /* ... then check available space ... */
38     need = sizeof(virtio_input_event) * vinput->qindex;
39     virtqueue_get_avail_bytes(vinput->evt, &have, NULL, need, 0);
40     if (have < need) {
41         vinput->qindex = 0;
42         fprintf(stderr, "%s: ENOSPC in vq, dropping events\n", __func__);
43         return;
44     }
45 
46     /* ... and finally pass them to the guest */
47     for (i = 0; i < vinput->qindex; i++) {
48         if (!virtqueue_pop(vinput->evt, &elem)) {
49             /* should not happen, we've checked for space beforehand */
50             fprintf(stderr, "%s: Huh?  No vq elem available ...\n", __func__);
51             return;
52         }
53         len = iov_from_buf(elem.in_sg, elem.in_num,
54                            0, vinput->queue+i, sizeof(virtio_input_event));
55         virtqueue_push(vinput->evt, &elem, len);
56     }
57     virtio_notify(VIRTIO_DEVICE(vinput), vinput->evt);
58     vinput->qindex = 0;
59 }
60 
61 static void virtio_input_handle_evt(VirtIODevice *vdev, VirtQueue *vq)
62 {
63     /* nothing */
64 }
65 
66 static void virtio_input_handle_sts(VirtIODevice *vdev, VirtQueue *vq)
67 {
68     VirtIOInputClass *vic = VIRTIO_INPUT_GET_CLASS(vdev);
69     VirtIOInput *vinput = VIRTIO_INPUT(vdev);
70     virtio_input_event event;
71     VirtQueueElement elem;
72     int len;
73 
74     while (virtqueue_pop(vinput->sts, &elem)) {
75         memset(&event, 0, sizeof(event));
76         len = iov_to_buf(elem.out_sg, elem.out_num,
77                          0, &event, sizeof(event));
78         if (vic->handle_status) {
79             vic->handle_status(vinput, &event);
80         }
81         virtqueue_push(vinput->sts, &elem, len);
82     }
83     virtio_notify(vdev, vinput->sts);
84 }
85 
86 static virtio_input_config *virtio_input_find_config(VirtIOInput *vinput,
87                                                      uint8_t select,
88                                                      uint8_t subsel)
89 {
90     VirtIOInputConfig *cfg;
91 
92     QTAILQ_FOREACH(cfg, &vinput->cfg_list, node) {
93         if (select == cfg->config.select &&
94             subsel == cfg->config.subsel) {
95             return &cfg->config;
96         }
97     }
98     return NULL;
99 }
100 
101 void virtio_input_add_config(VirtIOInput *vinput,
102                              virtio_input_config *config)
103 {
104     VirtIOInputConfig *cfg;
105 
106     if (virtio_input_find_config(vinput, config->select, config->subsel)) {
107         /* should not happen */
108         fprintf(stderr, "%s: duplicate config: %d/%d\n",
109                 __func__, config->select, config->subsel);
110         abort();
111     }
112 
113     cfg = g_new0(VirtIOInputConfig, 1);
114     cfg->config = *config;
115     QTAILQ_INSERT_TAIL(&vinput->cfg_list, cfg, node);
116 }
117 
118 void virtio_input_init_config(VirtIOInput *vinput,
119                               virtio_input_config *config)
120 {
121     int i = 0;
122 
123     QTAILQ_INIT(&vinput->cfg_list);
124     while (config[i].select) {
125         virtio_input_add_config(vinput, config + i);
126         i++;
127     }
128 }
129 
130 void virtio_input_idstr_config(VirtIOInput *vinput,
131                                uint8_t select, const char *string)
132 {
133     virtio_input_config id;
134 
135     if (!string) {
136         return;
137     }
138     memset(&id, 0, sizeof(id));
139     id.select = select;
140     id.size = snprintf(id.u.string, sizeof(id.u.string), "%s", string);
141     virtio_input_add_config(vinput, &id);
142 }
143 
144 static void virtio_input_get_config(VirtIODevice *vdev, uint8_t *config_data)
145 {
146     VirtIOInput *vinput = VIRTIO_INPUT(vdev);
147     virtio_input_config *config;
148 
149     config = virtio_input_find_config(vinput, vinput->cfg_select,
150                                       vinput->cfg_subsel);
151     if (config) {
152         memcpy(config_data, config, vinput->cfg_size);
153     } else {
154         memset(config_data, 0, vinput->cfg_size);
155     }
156 }
157 
158 static void virtio_input_set_config(VirtIODevice *vdev,
159                                     const uint8_t *config_data)
160 {
161     VirtIOInput *vinput = VIRTIO_INPUT(vdev);
162     virtio_input_config *config = (virtio_input_config *)config_data;
163 
164     vinput->cfg_select = config->select;
165     vinput->cfg_subsel = config->subsel;
166     virtio_notify_config(vdev);
167 }
168 
169 static uint64_t virtio_input_get_features(VirtIODevice *vdev, uint64_t f)
170 {
171     return f;
172 }
173 
174 static void virtio_input_set_status(VirtIODevice *vdev, uint8_t val)
175 {
176     VirtIOInputClass *vic = VIRTIO_INPUT_GET_CLASS(vdev);
177     VirtIOInput *vinput = VIRTIO_INPUT(vdev);
178 
179     if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
180         if (!vinput->active) {
181             vinput->active = true;
182             if (vic->change_active) {
183                 vic->change_active(vinput);
184             }
185         }
186     }
187 }
188 
189 static void virtio_input_reset(VirtIODevice *vdev)
190 {
191     VirtIOInputClass *vic = VIRTIO_INPUT_GET_CLASS(vdev);
192     VirtIOInput *vinput = VIRTIO_INPUT(vdev);
193 
194     if (vinput->active) {
195         vinput->active = false;
196         if (vic->change_active) {
197             vic->change_active(vinput);
198         }
199     }
200 }
201 
202 static void virtio_input_device_realize(DeviceState *dev, Error **errp)
203 {
204     VirtIOInputClass *vic = VIRTIO_INPUT_GET_CLASS(dev);
205     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
206     VirtIOInput *vinput = VIRTIO_INPUT(dev);
207     VirtIOInputConfig *cfg;
208     Error *local_err = NULL;
209 
210     if (vic->realize) {
211         vic->realize(dev, &local_err);
212         if (local_err) {
213             error_propagate(errp, local_err);
214             return;
215         }
216     }
217 
218     virtio_input_idstr_config(vinput, VIRTIO_INPUT_CFG_ID_SERIAL,
219                               vinput->input.serial);
220 
221     QTAILQ_FOREACH(cfg, &vinput->cfg_list, node) {
222         if (vinput->cfg_size < cfg->config.size) {
223             vinput->cfg_size = cfg->config.size;
224         }
225     }
226     vinput->cfg_size += 8;
227     assert(vinput->cfg_size <= sizeof(virtio_input_config));
228 
229     virtio_init(vdev, "virtio-input", VIRTIO_ID_INPUT,
230                 vinput->cfg_size);
231     vinput->evt = virtio_add_queue(vdev, 64, virtio_input_handle_evt);
232     vinput->sts = virtio_add_queue(vdev, 64, virtio_input_handle_sts);
233 }
234 
235 static void virtio_input_device_unrealize(DeviceState *dev, Error **errp)
236 {
237     VirtIOInputClass *vic = VIRTIO_INPUT_GET_CLASS(dev);
238     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
239     Error *local_err = NULL;
240 
241     if (vic->unrealize) {
242         vic->unrealize(dev, &local_err);
243         if (local_err) {
244             error_propagate(errp, local_err);
245             return;
246         }
247     }
248     virtio_cleanup(vdev);
249 }
250 
251 static void virtio_input_class_init(ObjectClass *klass, void *data)
252 {
253     DeviceClass *dc = DEVICE_CLASS(klass);
254     VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
255 
256     set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
257     vdc->realize      = virtio_input_device_realize;
258     vdc->unrealize    = virtio_input_device_unrealize;
259     vdc->get_config   = virtio_input_get_config;
260     vdc->set_config   = virtio_input_set_config;
261     vdc->get_features = virtio_input_get_features;
262     vdc->set_status   = virtio_input_set_status;
263     vdc->reset        = virtio_input_reset;
264 }
265 
266 static const TypeInfo virtio_input_info = {
267     .name          = TYPE_VIRTIO_INPUT,
268     .parent        = TYPE_VIRTIO_DEVICE,
269     .instance_size = sizeof(VirtIOInput),
270     .class_size    = sizeof(VirtIOInputClass),
271     .class_init    = virtio_input_class_init,
272     .abstract      = true,
273 };
274 
275 /* ----------------------------------------------------------------- */
276 
277 static void virtio_register_types(void)
278 {
279     type_register_static(&virtio_input_info);
280 }
281 
282 type_init(virtio_register_types)
283