Merge remote-tracking branch 'remotes/bonzini/tags/for-upstream' into staging
[qemu/ar7.git] / hw / input / virtio-input.c
blob7b25d27693189c292c5547e8d6af15392b81e73f
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,
170 Error **errp)
172 return f;
175 static void virtio_input_set_status(VirtIODevice *vdev, uint8_t val)
177 VirtIOInputClass *vic = VIRTIO_INPUT_GET_CLASS(vdev);
178 VirtIOInput *vinput = VIRTIO_INPUT(vdev);
180 if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
181 if (!vinput->active) {
182 vinput->active = true;
183 if (vic->change_active) {
184 vic->change_active(vinput);
190 static void virtio_input_reset(VirtIODevice *vdev)
192 VirtIOInputClass *vic = VIRTIO_INPUT_GET_CLASS(vdev);
193 VirtIOInput *vinput = VIRTIO_INPUT(vdev);
195 if (vinput->active) {
196 vinput->active = false;
197 if (vic->change_active) {
198 vic->change_active(vinput);
203 static void virtio_input_device_realize(DeviceState *dev, Error **errp)
205 VirtIOInputClass *vic = VIRTIO_INPUT_GET_CLASS(dev);
206 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
207 VirtIOInput *vinput = VIRTIO_INPUT(dev);
208 VirtIOInputConfig *cfg;
209 Error *local_err = NULL;
211 if (vic->realize) {
212 vic->realize(dev, &local_err);
213 if (local_err) {
214 error_propagate(errp, local_err);
215 return;
219 virtio_input_idstr_config(vinput, VIRTIO_INPUT_CFG_ID_SERIAL,
220 vinput->serial);
222 QTAILQ_FOREACH(cfg, &vinput->cfg_list, node) {
223 if (vinput->cfg_size < cfg->config.size) {
224 vinput->cfg_size = cfg->config.size;
227 vinput->cfg_size += 8;
228 assert(vinput->cfg_size <= sizeof(virtio_input_config));
230 virtio_init(vdev, "virtio-input", VIRTIO_ID_INPUT,
231 vinput->cfg_size);
232 vinput->evt = virtio_add_queue(vdev, 64, virtio_input_handle_evt);
233 vinput->sts = virtio_add_queue(vdev, 64, virtio_input_handle_sts);
236 static void virtio_input_device_unrealize(DeviceState *dev, Error **errp)
238 VirtIOInputClass *vic = VIRTIO_INPUT_GET_CLASS(dev);
239 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
240 Error *local_err = NULL;
242 if (vic->unrealize) {
243 vic->unrealize(dev, &local_err);
244 if (local_err) {
245 error_propagate(errp, local_err);
246 return;
249 virtio_cleanup(vdev);
252 static Property virtio_input_properties[] = {
253 DEFINE_PROP_STRING("serial", VirtIOInput, serial),
254 DEFINE_PROP_END_OF_LIST(),
257 static void virtio_input_class_init(ObjectClass *klass, void *data)
259 DeviceClass *dc = DEVICE_CLASS(klass);
260 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
262 dc->props = virtio_input_properties;
263 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
264 vdc->realize = virtio_input_device_realize;
265 vdc->unrealize = virtio_input_device_unrealize;
266 vdc->get_config = virtio_input_get_config;
267 vdc->set_config = virtio_input_set_config;
268 vdc->get_features = virtio_input_get_features;
269 vdc->set_status = virtio_input_set_status;
270 vdc->reset = virtio_input_reset;
273 static const TypeInfo virtio_input_info = {
274 .name = TYPE_VIRTIO_INPUT,
275 .parent = TYPE_VIRTIO_DEVICE,
276 .instance_size = sizeof(VirtIOInput),
277 .class_size = sizeof(VirtIOInputClass),
278 .class_init = virtio_input_class_init,
279 .abstract = true,
282 /* ----------------------------------------------------------------- */
284 static void virtio_register_types(void)
286 type_register_static(&virtio_input_info);
289 type_init(virtio_register_types)