Gerd Hoffmann | f73ddba | 2014-03-14 14:39:20 +0100 | [diff] [blame] | 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 | |
Peter Maydell | 9b8bfe2 | 2016-01-26 18:17:07 +0000 | [diff] [blame] | 7 | #include "qemu/osdep.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 8 | #include "qapi/error.h" |
Gerd Hoffmann | f73ddba | 2014-03-14 14:39:20 +0100 | [diff] [blame] | 9 | #include "qemu/iov.h" |
Markus Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 10 | #include "qemu/module.h" |
Gerd Hoffmann | c80276b | 2016-06-23 11:51:35 +0200 | [diff] [blame] | 11 | #include "trace.h" |
Gerd Hoffmann | f73ddba | 2014-03-14 14:39:20 +0100 | [diff] [blame] | 12 | |
Gerd Hoffmann | f73ddba | 2014-03-14 14:39:20 +0100 | [diff] [blame] | 13 | #include "hw/virtio/virtio.h" |
Markus Armbruster | a27bd6c | 2019-08-12 07:23:51 +0200 | [diff] [blame] | 14 | #include "hw/qdev-properties.h" |
Gerd Hoffmann | f73ddba | 2014-03-14 14:39:20 +0100 | [diff] [blame] | 15 | #include "hw/virtio/virtio-input.h" |
| 16 | |
| 17 | #include "standard-headers/linux/input.h" |
| 18 | |
Gerd Hoffmann | 2d73837 | 2016-04-05 14:31:41 +0200 | [diff] [blame] | 19 | #define VIRTIO_INPUT_VM_VERSION 1 |
| 20 | |
Gerd Hoffmann | f73ddba | 2014-03-14 14:39:20 +0100 | [diff] [blame] | 21 | /* ----------------------------------------------------------------- */ |
| 22 | |
| 23 | void virtio_input_send(VirtIOInput *vinput, virtio_input_event *event) |
| 24 | { |
Paolo Bonzini | 51b19eb | 2016-02-04 16:26:51 +0200 | [diff] [blame] | 25 | VirtQueueElement *elem; |
Gerd Hoffmann | f73ddba | 2014-03-14 14:39:20 +0100 | [diff] [blame] | 26 | int i, len; |
| 27 | |
Gerd Hoffmann | d9460a7 | 2015-10-16 13:33:07 +0200 | [diff] [blame] | 28 | if (!vinput->active) { |
| 29 | return; |
| 30 | } |
| 31 | |
Gerd Hoffmann | f73ddba | 2014-03-14 14:39:20 +0100 | [diff] [blame] | 32 | /* queue up events ... */ |
| 33 | if (vinput->qindex == vinput->qsize) { |
| 34 | vinput->qsize++; |
Ladi Prosek | 0f5a15e | 2017-03-24 15:24:49 +0100 | [diff] [blame] | 35 | vinput->queue = g_realloc(vinput->queue, vinput->qsize * |
Ladi Prosek | 5709454 | 2017-03-24 15:24:50 +0100 | [diff] [blame] | 36 | sizeof(vinput->queue[0])); |
Gerd Hoffmann | f73ddba | 2014-03-14 14:39:20 +0100 | [diff] [blame] | 37 | } |
Ladi Prosek | 5709454 | 2017-03-24 15:24:50 +0100 | [diff] [blame] | 38 | vinput->queue[vinput->qindex++].event = *event; |
Gerd Hoffmann | f73ddba | 2014-03-14 14:39:20 +0100 | [diff] [blame] | 39 | |
| 40 | /* ... until we see a report sync ... */ |
| 41 | if (event->type != cpu_to_le16(EV_SYN) || |
| 42 | event->code != cpu_to_le16(SYN_REPORT)) { |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | /* ... then check available space ... */ |
Ladi Prosek | 5709454 | 2017-03-24 15:24:50 +0100 | [diff] [blame] | 47 | for (i = 0; i < vinput->qindex; i++) { |
| 48 | elem = virtqueue_pop(vinput->evt, sizeof(VirtQueueElement)); |
| 49 | if (!elem) { |
| 50 | while (--i >= 0) { |
| 51 | virtqueue_unpop(vinput->evt, vinput->queue[i].elem, 0); |
| 52 | } |
| 53 | vinput->qindex = 0; |
| 54 | trace_virtio_input_queue_full(); |
| 55 | return; |
| 56 | } |
| 57 | vinput->queue[i].elem = elem; |
Gerd Hoffmann | f73ddba | 2014-03-14 14:39:20 +0100 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /* ... and finally pass them to the guest */ |
| 61 | for (i = 0; i < vinput->qindex; i++) { |
Ladi Prosek | 5709454 | 2017-03-24 15:24:50 +0100 | [diff] [blame] | 62 | elem = vinput->queue[i].elem; |
Paolo Bonzini | 51b19eb | 2016-02-04 16:26:51 +0200 | [diff] [blame] | 63 | len = iov_from_buf(elem->in_sg, elem->in_num, |
Ladi Prosek | 5709454 | 2017-03-24 15:24:50 +0100 | [diff] [blame] | 64 | 0, &vinput->queue[i].event, sizeof(virtio_input_event)); |
Paolo Bonzini | 51b19eb | 2016-02-04 16:26:51 +0200 | [diff] [blame] | 65 | virtqueue_push(vinput->evt, elem, len); |
| 66 | g_free(elem); |
Gerd Hoffmann | f73ddba | 2014-03-14 14:39:20 +0100 | [diff] [blame] | 67 | } |
| 68 | virtio_notify(VIRTIO_DEVICE(vinput), vinput->evt); |
| 69 | vinput->qindex = 0; |
| 70 | } |
| 71 | |
| 72 | static void virtio_input_handle_evt(VirtIODevice *vdev, VirtQueue *vq) |
| 73 | { |
| 74 | /* nothing */ |
| 75 | } |
| 76 | |
| 77 | static void virtio_input_handle_sts(VirtIODevice *vdev, VirtQueue *vq) |
| 78 | { |
| 79 | VirtIOInputClass *vic = VIRTIO_INPUT_GET_CLASS(vdev); |
| 80 | VirtIOInput *vinput = VIRTIO_INPUT(vdev); |
| 81 | virtio_input_event event; |
Paolo Bonzini | 51b19eb | 2016-02-04 16:26:51 +0200 | [diff] [blame] | 82 | VirtQueueElement *elem; |
Gerd Hoffmann | f73ddba | 2014-03-14 14:39:20 +0100 | [diff] [blame] | 83 | int len; |
| 84 | |
Paolo Bonzini | 51b19eb | 2016-02-04 16:26:51 +0200 | [diff] [blame] | 85 | for (;;) { |
| 86 | elem = virtqueue_pop(vinput->sts, sizeof(VirtQueueElement)); |
| 87 | if (!elem) { |
| 88 | break; |
| 89 | } |
| 90 | |
Gerd Hoffmann | f73ddba | 2014-03-14 14:39:20 +0100 | [diff] [blame] | 91 | memset(&event, 0, sizeof(event)); |
Paolo Bonzini | 51b19eb | 2016-02-04 16:26:51 +0200 | [diff] [blame] | 92 | len = iov_to_buf(elem->out_sg, elem->out_num, |
Gerd Hoffmann | f73ddba | 2014-03-14 14:39:20 +0100 | [diff] [blame] | 93 | 0, &event, sizeof(event)); |
| 94 | if (vic->handle_status) { |
| 95 | vic->handle_status(vinput, &event); |
| 96 | } |
Paolo Bonzini | 51b19eb | 2016-02-04 16:26:51 +0200 | [diff] [blame] | 97 | virtqueue_push(vinput->sts, elem, len); |
| 98 | g_free(elem); |
Gerd Hoffmann | f73ddba | 2014-03-14 14:39:20 +0100 | [diff] [blame] | 99 | } |
| 100 | virtio_notify(vdev, vinput->sts); |
| 101 | } |
| 102 | |
Ladi Prosek | b065e27 | 2016-04-13 16:43:23 +0200 | [diff] [blame] | 103 | virtio_input_config *virtio_input_find_config(VirtIOInput *vinput, |
| 104 | uint8_t select, |
| 105 | uint8_t subsel) |
Gerd Hoffmann | f73ddba | 2014-03-14 14:39:20 +0100 | [diff] [blame] | 106 | { |
| 107 | VirtIOInputConfig *cfg; |
| 108 | |
| 109 | QTAILQ_FOREACH(cfg, &vinput->cfg_list, node) { |
| 110 | if (select == cfg->config.select && |
| 111 | subsel == cfg->config.subsel) { |
| 112 | return &cfg->config; |
| 113 | } |
| 114 | } |
| 115 | return NULL; |
| 116 | } |
| 117 | |
| 118 | void virtio_input_add_config(VirtIOInput *vinput, |
| 119 | virtio_input_config *config) |
| 120 | { |
| 121 | VirtIOInputConfig *cfg; |
| 122 | |
| 123 | if (virtio_input_find_config(vinput, config->select, config->subsel)) { |
| 124 | /* should not happen */ |
| 125 | fprintf(stderr, "%s: duplicate config: %d/%d\n", |
| 126 | __func__, config->select, config->subsel); |
| 127 | abort(); |
| 128 | } |
| 129 | |
| 130 | cfg = g_new0(VirtIOInputConfig, 1); |
| 131 | cfg->config = *config; |
| 132 | QTAILQ_INSERT_TAIL(&vinput->cfg_list, cfg, node); |
| 133 | } |
| 134 | |
| 135 | void virtio_input_init_config(VirtIOInput *vinput, |
| 136 | virtio_input_config *config) |
| 137 | { |
| 138 | int i = 0; |
| 139 | |
| 140 | QTAILQ_INIT(&vinput->cfg_list); |
| 141 | while (config[i].select) { |
| 142 | virtio_input_add_config(vinput, config + i); |
| 143 | i++; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | void virtio_input_idstr_config(VirtIOInput *vinput, |
| 148 | uint8_t select, const char *string) |
| 149 | { |
| 150 | virtio_input_config id; |
| 151 | |
| 152 | if (!string) { |
| 153 | return; |
| 154 | } |
| 155 | memset(&id, 0, sizeof(id)); |
| 156 | id.select = select; |
| 157 | id.size = snprintf(id.u.string, sizeof(id.u.string), "%s", string); |
| 158 | virtio_input_add_config(vinput, &id); |
| 159 | } |
| 160 | |
| 161 | static void virtio_input_get_config(VirtIODevice *vdev, uint8_t *config_data) |
| 162 | { |
| 163 | VirtIOInput *vinput = VIRTIO_INPUT(vdev); |
| 164 | virtio_input_config *config; |
| 165 | |
| 166 | config = virtio_input_find_config(vinput, vinput->cfg_select, |
| 167 | vinput->cfg_subsel); |
| 168 | if (config) { |
| 169 | memcpy(config_data, config, vinput->cfg_size); |
| 170 | } else { |
| 171 | memset(config_data, 0, vinput->cfg_size); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | static void virtio_input_set_config(VirtIODevice *vdev, |
| 176 | const uint8_t *config_data) |
| 177 | { |
| 178 | VirtIOInput *vinput = VIRTIO_INPUT(vdev); |
| 179 | virtio_input_config *config = (virtio_input_config *)config_data; |
| 180 | |
| 181 | vinput->cfg_select = config->select; |
| 182 | vinput->cfg_subsel = config->subsel; |
| 183 | virtio_notify_config(vdev); |
| 184 | } |
| 185 | |
Jason Wang | 9d5b731 | 2015-07-27 17:49:19 +0800 | [diff] [blame] | 186 | static uint64_t virtio_input_get_features(VirtIODevice *vdev, uint64_t f, |
| 187 | Error **errp) |
Gerd Hoffmann | f73ddba | 2014-03-14 14:39:20 +0100 | [diff] [blame] | 188 | { |
| 189 | return f; |
| 190 | } |
| 191 | |
| 192 | static void virtio_input_set_status(VirtIODevice *vdev, uint8_t val) |
| 193 | { |
| 194 | VirtIOInputClass *vic = VIRTIO_INPUT_GET_CLASS(vdev); |
| 195 | VirtIOInput *vinput = VIRTIO_INPUT(vdev); |
| 196 | |
| 197 | if (val & VIRTIO_CONFIG_S_DRIVER_OK) { |
| 198 | if (!vinput->active) { |
| 199 | vinput->active = true; |
| 200 | if (vic->change_active) { |
| 201 | vic->change_active(vinput); |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | static void virtio_input_reset(VirtIODevice *vdev) |
| 208 | { |
| 209 | VirtIOInputClass *vic = VIRTIO_INPUT_GET_CLASS(vdev); |
| 210 | VirtIOInput *vinput = VIRTIO_INPUT(vdev); |
| 211 | |
| 212 | if (vinput->active) { |
| 213 | vinput->active = false; |
| 214 | if (vic->change_active) { |
| 215 | vic->change_active(vinput); |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
Halil Pasic | 73a1734 | 2016-10-06 14:55:45 +0200 | [diff] [blame] | 220 | static int virtio_input_post_load(void *opaque, int version_id) |
Gerd Hoffmann | 2d73837 | 2016-04-05 14:31:41 +0200 | [diff] [blame] | 221 | { |
| 222 | VirtIOInput *vinput = opaque; |
| 223 | VirtIOInputClass *vic = VIRTIO_INPUT_GET_CLASS(vinput); |
| 224 | VirtIODevice *vdev = VIRTIO_DEVICE(vinput); |
Gerd Hoffmann | 2d73837 | 2016-04-05 14:31:41 +0200 | [diff] [blame] | 225 | |
Gerd Hoffmann | 2d73837 | 2016-04-05 14:31:41 +0200 | [diff] [blame] | 226 | vinput->active = vdev->status & VIRTIO_CONFIG_S_DRIVER_OK; |
| 227 | if (vic->change_active) { |
| 228 | vic->change_active(vinput); |
| 229 | } |
| 230 | return 0; |
| 231 | } |
| 232 | |
Gerd Hoffmann | f73ddba | 2014-03-14 14:39:20 +0100 | [diff] [blame] | 233 | static void virtio_input_device_realize(DeviceState *dev, Error **errp) |
| 234 | { |
| 235 | VirtIOInputClass *vic = VIRTIO_INPUT_GET_CLASS(dev); |
| 236 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); |
| 237 | VirtIOInput *vinput = VIRTIO_INPUT(dev); |
| 238 | VirtIOInputConfig *cfg; |
| 239 | Error *local_err = NULL; |
| 240 | |
| 241 | if (vic->realize) { |
| 242 | vic->realize(dev, &local_err); |
| 243 | if (local_err) { |
| 244 | error_propagate(errp, local_err); |
| 245 | return; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | virtio_input_idstr_config(vinput, VIRTIO_INPUT_CFG_ID_SERIAL, |
Gerd Hoffmann | 6f2b9a5 | 2015-06-18 17:45:47 +0200 | [diff] [blame] | 250 | vinput->serial); |
Gerd Hoffmann | f73ddba | 2014-03-14 14:39:20 +0100 | [diff] [blame] | 251 | |
| 252 | QTAILQ_FOREACH(cfg, &vinput->cfg_list, node) { |
| 253 | if (vinput->cfg_size < cfg->config.size) { |
| 254 | vinput->cfg_size = cfg->config.size; |
| 255 | } |
| 256 | } |
| 257 | vinput->cfg_size += 8; |
| 258 | assert(vinput->cfg_size <= sizeof(virtio_input_config)); |
| 259 | |
Jonah Palmer | 3857cd5 | 2022-04-01 09:23:18 -0400 | [diff] [blame] | 260 | virtio_init(vdev, VIRTIO_ID_INPUT, vinput->cfg_size); |
Gerd Hoffmann | f73ddba | 2014-03-14 14:39:20 +0100 | [diff] [blame] | 261 | vinput->evt = virtio_add_queue(vdev, 64, virtio_input_handle_evt); |
| 262 | vinput->sts = virtio_add_queue(vdev, 64, virtio_input_handle_sts); |
| 263 | } |
| 264 | |
Marc-André Lureau | 0137a55 | 2016-07-15 10:55:12 +0200 | [diff] [blame] | 265 | static void virtio_input_finalize(Object *obj) |
| 266 | { |
| 267 | VirtIOInput *vinput = VIRTIO_INPUT(obj); |
| 268 | VirtIOInputConfig *cfg, *next; |
| 269 | |
| 270 | QTAILQ_FOREACH_SAFE(cfg, &vinput->cfg_list, node, next) { |
| 271 | QTAILQ_REMOVE(&vinput->cfg_list, cfg, node); |
| 272 | g_free(cfg); |
| 273 | } |
Ladi Prosek | 0f5a15e | 2017-03-24 15:24:49 +0100 | [diff] [blame] | 274 | |
| 275 | g_free(vinput->queue); |
Marc-André Lureau | 0137a55 | 2016-07-15 10:55:12 +0200 | [diff] [blame] | 276 | } |
Marc-André Lureau | 509ec36 | 2019-11-21 13:56:49 +0400 | [diff] [blame] | 277 | |
Markus Armbruster | b69c3c2 | 2020-05-05 17:29:24 +0200 | [diff] [blame] | 278 | static void virtio_input_device_unrealize(DeviceState *dev) |
Gerd Hoffmann | f73ddba | 2014-03-14 14:39:20 +0100 | [diff] [blame] | 279 | { |
| 280 | VirtIOInputClass *vic = VIRTIO_INPUT_GET_CLASS(dev); |
| 281 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); |
Michael S. Tsirkin | 087ed8a | 2019-12-09 11:55:10 -0500 | [diff] [blame] | 282 | VirtIOInput *vinput = VIRTIO_INPUT(dev); |
Gerd Hoffmann | f73ddba | 2014-03-14 14:39:20 +0100 | [diff] [blame] | 283 | |
| 284 | if (vic->unrealize) { |
Markus Armbruster | b69c3c2 | 2020-05-05 17:29:24 +0200 | [diff] [blame] | 285 | vic->unrealize(dev); |
Gerd Hoffmann | f73ddba | 2014-03-14 14:39:20 +0100 | [diff] [blame] | 286 | } |
Michael S. Tsirkin | 087ed8a | 2019-12-09 11:55:10 -0500 | [diff] [blame] | 287 | virtio_delete_queue(vinput->evt); |
| 288 | virtio_delete_queue(vinput->sts); |
Gerd Hoffmann | f73ddba | 2014-03-14 14:39:20 +0100 | [diff] [blame] | 289 | virtio_cleanup(vdev); |
| 290 | } |
| 291 | |
Halil Pasic | 73a1734 | 2016-10-06 14:55:45 +0200 | [diff] [blame] | 292 | static const VMStateDescription vmstate_virtio_input = { |
| 293 | .name = "virtio-input", |
| 294 | .minimum_version_id = VIRTIO_INPUT_VM_VERSION, |
| 295 | .version_id = VIRTIO_INPUT_VM_VERSION, |
Richard Henderson | af0f07d | 2023-12-21 14:16:14 +1100 | [diff] [blame] | 296 | .fields = (const VMStateField[]) { |
Halil Pasic | 73a1734 | 2016-10-06 14:55:45 +0200 | [diff] [blame] | 297 | VMSTATE_VIRTIO_DEVICE, |
| 298 | VMSTATE_END_OF_LIST() |
| 299 | }, |
| 300 | .post_load = virtio_input_post_load, |
| 301 | }; |
Dr. David Alan Gilbert | 428d2ed | 2016-07-14 18:22:53 +0100 | [diff] [blame] | 302 | |
Gerd Hoffmann | 6f2b9a5 | 2015-06-18 17:45:47 +0200 | [diff] [blame] | 303 | static Property virtio_input_properties[] = { |
| 304 | DEFINE_PROP_STRING("serial", VirtIOInput, serial), |
| 305 | DEFINE_PROP_END_OF_LIST(), |
| 306 | }; |
| 307 | |
Gerd Hoffmann | f73ddba | 2014-03-14 14:39:20 +0100 | [diff] [blame] | 308 | static void virtio_input_class_init(ObjectClass *klass, void *data) |
| 309 | { |
| 310 | DeviceClass *dc = DEVICE_CLASS(klass); |
| 311 | VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); |
| 312 | |
Marc-André Lureau | 4f67d30 | 2020-01-10 19:30:32 +0400 | [diff] [blame] | 313 | device_class_set_props(dc, virtio_input_properties); |
Dr. David Alan Gilbert | 428d2ed | 2016-07-14 18:22:53 +0100 | [diff] [blame] | 314 | dc->vmsd = &vmstate_virtio_input; |
Gerd Hoffmann | f73ddba | 2014-03-14 14:39:20 +0100 | [diff] [blame] | 315 | set_bit(DEVICE_CATEGORY_INPUT, dc->categories); |
| 316 | vdc->realize = virtio_input_device_realize; |
| 317 | vdc->unrealize = virtio_input_device_unrealize; |
| 318 | vdc->get_config = virtio_input_get_config; |
| 319 | vdc->set_config = virtio_input_set_config; |
| 320 | vdc->get_features = virtio_input_get_features; |
| 321 | vdc->set_status = virtio_input_set_status; |
| 322 | vdc->reset = virtio_input_reset; |
| 323 | } |
| 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, |
Marc-André Lureau | 0137a55 | 2016-07-15 10:55:12 +0200 | [diff] [blame] | 332 | .instance_finalize = virtio_input_finalize, |
Gerd Hoffmann | f73ddba | 2014-03-14 14:39:20 +0100 | [diff] [blame] | 333 | }; |
| 334 | |
| 335 | /* ----------------------------------------------------------------- */ |
| 336 | |
| 337 | static void virtio_register_types(void) |
| 338 | { |
| 339 | type_register_static(&virtio_input_info); |
| 340 | } |
| 341 | |
| 342 | type_init(virtio_register_types) |