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 Error **errp) 171 { 172 return f; 173 } 174 175 static void virtio_input_set_status(VirtIODevice *vdev, uint8_t val) 176 { 177 VirtIOInputClass *vic = VIRTIO_INPUT_GET_CLASS(vdev); 178 VirtIOInput *vinput = VIRTIO_INPUT(vdev); 179 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); 185 } 186 } 187 } 188 } 189 190 static void virtio_input_reset(VirtIODevice *vdev) 191 { 192 VirtIOInputClass *vic = VIRTIO_INPUT_GET_CLASS(vdev); 193 VirtIOInput *vinput = VIRTIO_INPUT(vdev); 194 195 if (vinput->active) { 196 vinput->active = false; 197 if (vic->change_active) { 198 vic->change_active(vinput); 199 } 200 } 201 } 202 203 static void virtio_input_device_realize(DeviceState *dev, Error **errp) 204 { 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; 210 211 if (vic->realize) { 212 vic->realize(dev, &local_err); 213 if (local_err) { 214 error_propagate(errp, local_err); 215 return; 216 } 217 } 218 219 virtio_input_idstr_config(vinput, VIRTIO_INPUT_CFG_ID_SERIAL, 220 vinput->serial); 221 222 QTAILQ_FOREACH(cfg, &vinput->cfg_list, node) { 223 if (vinput->cfg_size < cfg->config.size) { 224 vinput->cfg_size = cfg->config.size; 225 } 226 } 227 vinput->cfg_size += 8; 228 assert(vinput->cfg_size <= sizeof(virtio_input_config)); 229 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); 234 } 235 236 static void virtio_input_device_unrealize(DeviceState *dev, Error **errp) 237 { 238 VirtIOInputClass *vic = VIRTIO_INPUT_GET_CLASS(dev); 239 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 240 Error *local_err = NULL; 241 242 if (vic->unrealize) { 243 vic->unrealize(dev, &local_err); 244 if (local_err) { 245 error_propagate(errp, local_err); 246 return; 247 } 248 } 249 virtio_cleanup(vdev); 250 } 251 252 static Property virtio_input_properties[] = { 253 DEFINE_PROP_STRING("serial", VirtIOInput, serial), 254 DEFINE_PROP_END_OF_LIST(), 255 }; 256 257 static void virtio_input_class_init(ObjectClass *klass, void *data) 258 { 259 DeviceClass *dc = DEVICE_CLASS(klass); 260 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); 261 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; 271 } 272 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, 280 }; 281 282 /* ----------------------------------------------------------------- */ 283 284 static void virtio_register_types(void) 285 { 286 type_register_static(&virtio_input_info); 287 } 288 289 type_init(virtio_register_types) 290