virtio: make features 64bit wide
[qemu.git] / hw / input / virtio-input.c
blobc4f4b3c150e02ecd4dc577d3cc23f75a3fa8a0ac
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 */
7 #include "qemu/iov.h"
9 #include "hw/qdev.h"
10 #include "hw/virtio/virtio.h"
11 #include "hw/virtio/virtio-input.h"
13 #include "standard-headers/linux/input.h"
15 /* ----------------------------------------------------------------- */
17 void virtio_input_send(VirtIOInput *vinput, virtio_input_event *event)
19 VirtQueueElement elem;
20 unsigned have, need;
21 int i, len;
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));
29 vinput->queue[vinput->qindex++] = *event;
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;
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;
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;
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);
57 virtio_notify(VIRTIO_DEVICE(vinput), vinput->evt);
58 vinput->qindex = 0;
61 static void virtio_input_handle_evt(VirtIODevice *vdev, VirtQueue *vq)
63 /* nothing */
66 static void virtio_input_handle_sts(VirtIODevice *vdev, VirtQueue *vq)
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;
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);
81 virtqueue_push(vinput->sts, &elem, len);
83 virtio_notify(vdev, vinput->sts);
86 static virtio_input_config *virtio_input_find_config(VirtIOInput *vinput,
87 uint8_t select,
88 uint8_t subsel)
90 VirtIOInputConfig *cfg;
92 QTAILQ_FOREACH(cfg, &vinput->cfg_list, node) {
93 if (select == cfg->config.select &&
94 subsel == cfg->config.subsel) {
95 return &cfg->config;
98 return NULL;
101 void virtio_input_add_config(VirtIOInput *vinput,
102 virtio_input_config *config)
104 VirtIOInputConfig *cfg;
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();
113 cfg = g_new0(VirtIOInputConfig, 1);
114 cfg->config = *config;
115 QTAILQ_INSERT_TAIL(&vinput->cfg_list, cfg, node);
118 void virtio_input_init_config(VirtIOInput *vinput,
119 virtio_input_config *config)
121 int i = 0;
123 QTAILQ_INIT(&vinput->cfg_list);
124 while (config[i].select) {
125 virtio_input_add_config(vinput, config + i);
126 i++;
130 void virtio_input_idstr_config(VirtIOInput *vinput,
131 uint8_t select, const char *string)
133 virtio_input_config id;
135 if (!string) {
136 return;
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);
144 static void virtio_input_get_config(VirtIODevice *vdev, uint8_t *config_data)
146 VirtIOInput *vinput = VIRTIO_INPUT(vdev);
147 virtio_input_config *config;
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);
158 static void virtio_input_set_config(VirtIODevice *vdev,
159 const uint8_t *config_data)
161 VirtIOInput *vinput = VIRTIO_INPUT(vdev);
162 virtio_input_config *config = (virtio_input_config *)config_data;
164 vinput->cfg_select = config->select;
165 vinput->cfg_subsel = config->subsel;
166 virtio_notify_config(vdev);
169 static uint64_t virtio_input_get_features(VirtIODevice *vdev, uint64_t f)
171 return f;
174 static void virtio_input_set_status(VirtIODevice *vdev, uint8_t val)
176 VirtIOInputClass *vic = VIRTIO_INPUT_GET_CLASS(vdev);
177 VirtIOInput *vinput = VIRTIO_INPUT(vdev);
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);
189 static void virtio_input_reset(VirtIODevice *vdev)
191 VirtIOInputClass *vic = VIRTIO_INPUT_GET_CLASS(vdev);
192 VirtIOInput *vinput = VIRTIO_INPUT(vdev);
194 if (vinput->active) {
195 vinput->active = false;
196 if (vic->change_active) {
197 vic->change_active(vinput);
202 static void virtio_input_device_realize(DeviceState *dev, Error **errp)
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;
210 if (vic->realize) {
211 vic->realize(dev, &local_err);
212 if (local_err) {
213 error_propagate(errp, local_err);
214 return;
218 virtio_input_idstr_config(vinput, VIRTIO_INPUT_CFG_ID_SERIAL,
219 vinput->input.serial);
221 QTAILQ_FOREACH(cfg, &vinput->cfg_list, node) {
222 if (vinput->cfg_size < cfg->config.size) {
223 vinput->cfg_size = cfg->config.size;
226 vinput->cfg_size += 8;
227 assert(vinput->cfg_size <= sizeof(virtio_input_config));
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);
235 static void virtio_input_device_unrealize(DeviceState *dev, Error **errp)
237 VirtIOInputClass *vic = VIRTIO_INPUT_GET_CLASS(dev);
238 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
239 Error *local_err = NULL;
241 if (vic->unrealize) {
242 vic->unrealize(dev, &local_err);
243 if (local_err) {
244 error_propagate(errp, local_err);
245 return;
248 virtio_cleanup(vdev);
251 static void virtio_input_class_init(ObjectClass *klass, void *data)
253 DeviceClass *dc = DEVICE_CLASS(klass);
254 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
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;
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,
275 /* ----------------------------------------------------------------- */
277 static void virtio_register_types(void)
279 type_register_static(&virtio_input_info);
282 type_init(virtio_register_types)