| 1 | |
| 2 | #include <linux/module.h>
|
| 3 | #include <linux/virtio.h>
|
| 4 | #include <linux/virtio_config.h>
|
| 5 | #include <linux/input.h>
|
| 6 | #include <linux/slab.h>
|
| 7 | #include <linux/dma-mapping.h>
|
| 8 |
|
| 9 | #include <uapi/linux/virtio_ids.h>
|
| 10 | #include <uapi/linux/virtio_input.h>
|
| 11 | #include <linux/input/mt.h>
|
| 12 |
|
| 13 | struct virtio_input {
|
| 14 | struct virtio_device *vdev;
|
| 15 | struct input_dev *idev;
|
| 16 | char name[64];
|
| 17 | char serial[64];
|
| 18 | char phys[64];
|
| 19 | struct virtqueue *evt, *sts;
|
| 20 | __dma_from_device_group_begin();
|
| 21 | struct virtio_input_event evts[64];
|
| 22 | __dma_from_device_group_end();
|
| 23 | spinlock_t lock;
|
| 24 | bool ready;
|
| 25 | };
|
| 26 |
|
| 27 | static void virtinput_queue_evtbuf(struct virtio_input *vi,
|
| 28 | struct virtio_input_event *evtbuf)
|
| 29 | {
|
| 30 | struct scatterlist sg[1];
|
| 31 |
|
| 32 | sg_init_one(sg, evtbuf, sizeof(*evtbuf));
|
| 33 | virtqueue_add_inbuf_cache_clean(vi->evt, sg, 1, evtbuf, GFP_ATOMIC);
|
| 34 | }
|
| 35 |
|
| 36 | static void virtinput_recv_events(struct virtqueue *vq)
|
| 37 | {
|
| 38 | struct virtio_input *vi = vq->vdev->priv;
|
| 39 | struct virtio_input_event *event;
|
| 40 | unsigned long flags;
|
| 41 | unsigned int len;
|
| 42 |
|
| 43 | spin_lock_irqsave(&vi->lock, flags);
|
| 44 | if (vi->ready) {
|
| 45 | while ((event = virtqueue_get_buf(vi->evt, &len)) != NULL) {
|
| 46 | spin_unlock_irqrestore(&vi->lock, flags);
|
| 47 | input_event(vi->idev,
|
| 48 | le16_to_cpu(event->type),
|
| 49 | le16_to_cpu(event->code),
|
| 50 | le32_to_cpu(event->value));
|
| 51 | spin_lock_irqsave(&vi->lock, flags);
|
| 52 | if (!vi->ready)
|
| 53 | continue;
|
| 54 | virtinput_queue_evtbuf(vi, event);
|
| 55 | }
|
| 56 | if (vi->ready)
|
| 57 | virtqueue_kick(vq);
|
| 58 | }
|
| 59 | spin_unlock_irqrestore(&vi->lock, flags);
|
| 60 | }
|
| 61 |
|
| 62 | |
| 63 | * On error we are losing the status update, which isn't critical as
|
| 64 | * this is typically used for stuff like keyboard leds.
|
| 65 |
|
| 66 | static int virtinput_send_status(struct virtio_input *vi,
|
| 67 | u16 type, u16 code, s32 value)
|
| 68 | {
|
| 69 | struct virtio_input_event *stsbuf;
|
| 70 | struct scatterlist sg[1];
|
| 71 | unsigned long flags;
|
| 72 | int rc;
|
| 73 |
|
| 74 | |
| 75 | * Since 29cc309d8bf1 (HID: hid-multitouch: forward MSC_TIMESTAMP),
|
| 76 | * EV_MSC/MSC_TIMESTAMP is added to each before EV_SYN event.
|
| 77 | * EV_MSC is configured as INPUT_PASS_TO_ALL.
|
| 78 | * In case of touch device:
|
| 79 | * BE pass EV_MSC/MSC_TIMESTAMP to FE on receiving event from evdev.
|
| 80 | * FE pass EV_MSC/MSC_TIMESTAMP back to BE.
|
| 81 | * BE writes EV_MSC/MSC_TIMESTAMP to evdev due to INPUT_PASS_TO_ALL.
|
| 82 | * BE receives extra EV_MSC/MSC_TIMESTAMP and pass to FE.
|
| 83 | * >>> Each new frame becomes larger and larger.
|
| 84 | * Disable EV_MSC/MSC_TIMESTAMP forwarding for MT.
|
| 85 |
|
| 86 | if (vi->idev->mt && type == EV_MSC && code == MSC_TIMESTAMP)
|
| 87 | return 0;
|
| 88 |
|
| 89 | stsbuf = kzalloc_obj(*stsbuf, GFP_ATOMIC);
|
| 90 | if (!stsbuf)
|
| 91 | return -ENOMEM;
|
| 92 |
|
| 93 | stsbuf->type = cpu_to_le16(type);
|
| 94 | stsbuf->code = cpu_to_le16(code);
|
| 95 | stsbuf->value = cpu_to_le32(value);
|
| 96 | sg_init_one(sg, stsbuf, sizeof(*stsbuf));
|
| 97 |
|
| 98 | spin_lock_irqsave(&vi->lock, flags);
|
| 99 | if (vi->ready) {
|
| 100 | rc = virtqueue_add_outbuf(vi->sts, sg, 1, stsbuf, GFP_ATOMIC);
|
| 101 | virtqueue_kick(vi->sts);
|
| 102 | } else {
|
| 103 | rc = -ENODEV;
|
| 104 | }
|
| 105 | spin_unlock_irqrestore(&vi->lock, flags);
|
| 106 |
|
| 107 | if (rc != 0)
|
| 108 | kfree(stsbuf);
|
| 109 | return rc;
|
| 110 | }
|
| 111 |
|
| 112 | static void virtinput_recv_status(struct virtqueue *vq)
|
| 113 | {
|
| 114 | struct virtio_input *vi = vq->vdev->priv;
|
| 115 | struct virtio_input_event *stsbuf;
|
| 116 | unsigned long flags;
|
| 117 | unsigned int len;
|
| 118 |
|
| 119 | spin_lock_irqsave(&vi->lock, flags);
|
| 120 | while ((stsbuf = virtqueue_get_buf(vi->sts, &len)) != NULL)
|
| 121 | kfree(stsbuf);
|
| 122 | spin_unlock_irqrestore(&vi->lock, flags);
|
| 123 | }
|
| 124 |
|
| 125 | static int virtinput_status(struct input_dev *idev, unsigned int type,
|
| 126 | unsigned int code, int value)
|
| 127 | {
|
| 128 | struct virtio_input *vi = input_get_drvdata(idev);
|
| 129 |
|
| 130 | return virtinput_send_status(vi, type, code, value);
|
| 131 | }
|
| 132 |
|
| 133 | static u8 virtinput_cfg_select(struct virtio_input *vi,
|
| 134 | u8 select, u8 subsel)
|
| 135 | {
|
| 136 | u8 size;
|
| 137 |
|
| 138 | virtio_cwrite_le(vi->vdev, struct virtio_input_config, select, &select);
|
| 139 | virtio_cwrite_le(vi->vdev, struct virtio_input_config, subsel, &subsel);
|
| 140 | virtio_cread_le(vi->vdev, struct virtio_input_config, size, &size);
|
| 141 | return size;
|
| 142 | }
|
| 143 |
|
| 144 | static void virtinput_cfg_bits(struct virtio_input *vi, int select, int subsel,
|
| 145 | unsigned long *bits, unsigned int bitcount)
|
| 146 | {
|
| 147 | unsigned int bit;
|
| 148 | u8 *virtio_bits;
|
| 149 | u8 bytes;
|
| 150 |
|
| 151 | bytes = virtinput_cfg_select(vi, select, subsel);
|
| 152 | if (!bytes)
|
| 153 | return;
|
| 154 | if (bitcount > bytes * 8)
|
| 155 | bitcount = bytes * 8;
|
| 156 |
|
| 157 | |
| 158 | * Bitmap in virtio config space is a simple stream of bytes,
|
| 159 | * with the first byte carrying bits 0-7, second bits 8-15 and
|
| 160 | * so on.
|
| 161 |
|
| 162 | virtio_bits = kzalloc(bytes, GFP_KERNEL);
|
| 163 | if (!virtio_bits)
|
| 164 | return;
|
| 165 | virtio_cread_bytes(vi->vdev, offsetof(struct virtio_input_config,
|
| 166 | u.bitmap),
|
| 167 | virtio_bits, bytes);
|
| 168 | for (bit = 0; bit < bitcount; bit++) {
|
| 169 | if (virtio_bits[bit / 8] & (1 << (bit % 8)))
|
| 170 | __set_bit(bit, bits);
|
| 171 | }
|
| 172 | kfree(virtio_bits);
|
| 173 |
|
| 174 | if (select == VIRTIO_INPUT_CFG_EV_BITS)
|
| 175 | __set_bit(subsel, vi->idev->evbit);
|
| 176 | }
|
| 177 |
|
| 178 | static void virtinput_cfg_abs(struct virtio_input *vi, int abs)
|
| 179 | {
|
| 180 | u32 mi, ma, re, fu, fl;
|
| 181 |
|
| 182 | virtinput_cfg_select(vi, VIRTIO_INPUT_CFG_ABS_INFO, abs);
|
| 183 | virtio_cread_le(vi->vdev, struct virtio_input_config, u.abs.min, &mi);
|
| 184 | virtio_cread_le(vi->vdev, struct virtio_input_config, u.abs.max, &ma);
|
| 185 | virtio_cread_le(vi->vdev, struct virtio_input_config, u.abs.res, &re);
|
| 186 | virtio_cread_le(vi->vdev, struct virtio_input_config, u.abs.fuzz, &fu);
|
| 187 | virtio_cread_le(vi->vdev, struct virtio_input_config, u.abs.flat, &fl);
|
| 188 | input_set_abs_params(vi->idev, abs, mi, ma, fu, fl);
|
| 189 | input_abs_set_res(vi->idev, abs, re);
|
| 190 | }
|
| 191 |
|
| 192 | static int virtinput_init_vqs(struct virtio_input *vi)
|
| 193 | {
|
| 194 | struct virtqueue_info vqs_info[] = {
|
| 195 | { "events", virtinput_recv_events },
|
| 196 | { "status", virtinput_recv_status },
|
| 197 | };
|
| 198 | struct virtqueue *vqs[2];
|
| 199 | int err;
|
| 200 |
|
| 201 | err = virtio_find_vqs(vi->vdev, 2, vqs, vqs_info, NULL);
|
| 202 | if (err)
|
| 203 | return err;
|
| 204 | vi->evt = vqs[0];
|
| 205 | vi->sts = vqs[1];
|
| 206 |
|
| 207 | return 0;
|
| 208 | }
|
| 209 |
|
| 210 | static void virtinput_fill_evt(struct virtio_input *vi)
|
| 211 | {
|
| 212 | unsigned long flags;
|
| 213 | int i, size;
|
| 214 |
|
| 215 | spin_lock_irqsave(&vi->lock, flags);
|
| 216 | size = virtqueue_get_vring_size(vi->evt);
|
| 217 | if (size > ARRAY_SIZE(vi->evts))
|
| 218 | size = ARRAY_SIZE(vi->evts);
|
| 219 | for (i = 0; i < size; i++)
|
| 220 | virtinput_queue_evtbuf(vi, &vi->evts[i]);
|
| 221 | virtqueue_kick(vi->evt);
|
| 222 | spin_unlock_irqrestore(&vi->lock, flags);
|
| 223 | }
|
| 224 |
|
| 225 | static int virtinput_probe(struct virtio_device *vdev)
|
| 226 | {
|
| 227 | struct virtio_input *vi;
|
| 228 | unsigned long flags;
|
| 229 | size_t size;
|
| 230 | int abs, err, nslots;
|
| 231 |
|
| 232 | if (!virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
|
| 233 | return -ENODEV;
|
| 234 |
|
| 235 | vi = kzalloc_obj(*vi);
|
| 236 | if (!vi)
|
| 237 | return -ENOMEM;
|
| 238 |
|
| 239 | vdev->priv = vi;
|
| 240 | vi->vdev = vdev;
|
| 241 | spin_lock_init(&vi->lock);
|
| 242 |
|
| 243 | err = virtinput_init_vqs(vi);
|
| 244 | if (err)
|
| 245 | goto err_init_vq;
|
| 246 |
|
| 247 | vi->idev = input_allocate_device();
|
| 248 | if (!vi->idev) {
|
| 249 | err = -ENOMEM;
|
| 250 | goto err_input_alloc;
|
| 251 | }
|
| 252 | input_set_drvdata(vi->idev, vi);
|
| 253 |
|
| 254 | size = virtinput_cfg_select(vi, VIRTIO_INPUT_CFG_ID_NAME, 0);
|
| 255 | virtio_cread_bytes(vi->vdev, offsetof(struct virtio_input_config,
|
| 256 | u.string),
|
| 257 | vi->name, min(size, sizeof(vi->name)));
|
| 258 | size = virtinput_cfg_select(vi, VIRTIO_INPUT_CFG_ID_SERIAL, 0);
|
| 259 | virtio_cread_bytes(vi->vdev, offsetof(struct virtio_input_config,
|
| 260 | u.string),
|
| 261 | vi->serial, min(size, sizeof(vi->serial)));
|
| 262 | snprintf(vi->phys, sizeof(vi->phys),
|
| 263 | "virtio%d/input0", vdev->index);
|
| 264 | vi->idev->name = vi->name;
|
| 265 | vi->idev->phys = vi->phys;
|
| 266 | vi->idev->uniq = vi->serial;
|
| 267 |
|
| 268 | size = virtinput_cfg_select(vi, VIRTIO_INPUT_CFG_ID_DEVIDS, 0);
|
| 269 | if (size >= sizeof(struct virtio_input_devids)) {
|
| 270 | virtio_cread_le(vi->vdev, struct virtio_input_config,
|
| 271 | u.ids.bustype, &vi->idev->id.bustype);
|
| 272 | virtio_cread_le(vi->vdev, struct virtio_input_config,
|
| 273 | u.ids.vendor, &vi->idev->id.vendor);
|
| 274 | virtio_cread_le(vi->vdev, struct virtio_input_config,
|
| 275 | u.ids.product, &vi->idev->id.product);
|
| 276 | virtio_cread_le(vi->vdev, struct virtio_input_config,
|
| 277 | u.ids.version, &vi->idev->id.version);
|
| 278 | } else {
|
| 279 | vi->idev->id.bustype = BUS_VIRTUAL;
|
| 280 | }
|
| 281 |
|
| 282 | virtinput_cfg_bits(vi, VIRTIO_INPUT_CFG_PROP_BITS, 0,
|
| 283 | vi->idev->propbit, INPUT_PROP_CNT);
|
| 284 | size = virtinput_cfg_select(vi, VIRTIO_INPUT_CFG_EV_BITS, EV_REP);
|
| 285 | if (size)
|
| 286 | __set_bit(EV_REP, vi->idev->evbit);
|
| 287 |
|
| 288 | vi->idev->dev.parent = &vdev->dev;
|
| 289 | vi->idev->event = virtinput_status;
|
| 290 |
|
| 291 |
|
| 292 | virtinput_cfg_bits(vi, VIRTIO_INPUT_CFG_EV_BITS, EV_KEY,
|
| 293 | vi->idev->keybit, KEY_CNT);
|
| 294 | virtinput_cfg_bits(vi, VIRTIO_INPUT_CFG_EV_BITS, EV_REL,
|
| 295 | vi->idev->relbit, REL_CNT);
|
| 296 | virtinput_cfg_bits(vi, VIRTIO_INPUT_CFG_EV_BITS, EV_ABS,
|
| 297 | vi->idev->absbit, ABS_CNT);
|
| 298 | virtinput_cfg_bits(vi, VIRTIO_INPUT_CFG_EV_BITS, EV_MSC,
|
| 299 | vi->idev->mscbit, MSC_CNT);
|
| 300 | virtinput_cfg_bits(vi, VIRTIO_INPUT_CFG_EV_BITS, EV_SW,
|
| 301 | vi->idev->swbit, SW_CNT);
|
| 302 |
|
| 303 |
|
| 304 | virtinput_cfg_bits(vi, VIRTIO_INPUT_CFG_EV_BITS, EV_LED,
|
| 305 | vi->idev->ledbit, LED_CNT);
|
| 306 | virtinput_cfg_bits(vi, VIRTIO_INPUT_CFG_EV_BITS, EV_SND,
|
| 307 | vi->idev->sndbit, SND_CNT);
|
| 308 |
|
| 309 | if (test_bit(EV_ABS, vi->idev->evbit)) {
|
| 310 | for (abs = 0; abs < ABS_CNT; abs++) {
|
| 311 | if (!test_bit(abs, vi->idev->absbit))
|
| 312 | continue;
|
| 313 | virtinput_cfg_abs(vi, abs);
|
| 314 | }
|
| 315 |
|
| 316 | if (test_bit(ABS_MT_SLOT, vi->idev->absbit)) {
|
| 317 | nslots = input_abs_get_max(vi->idev, ABS_MT_SLOT) + 1;
|
| 318 | err = input_mt_init_slots(vi->idev, nslots, 0);
|
| 319 | if (err)
|
| 320 | goto err_mt_init_slots;
|
| 321 | }
|
| 322 | }
|
| 323 |
|
| 324 | virtio_device_ready(vdev);
|
| 325 | vi->ready = true;
|
| 326 | err = input_register_device(vi->idev);
|
| 327 | if (err)
|
| 328 | goto err_input_register;
|
| 329 |
|
| 330 | virtinput_fill_evt(vi);
|
| 331 | return 0;
|
| 332 |
|
| 333 | err_input_register:
|
| 334 | spin_lock_irqsave(&vi->lock, flags);
|
| 335 | vi->ready = false;
|
| 336 | spin_unlock_irqrestore(&vi->lock, flags);
|
| 337 | virtio_reset_device(vdev);
|
| 338 | err_mt_init_slots:
|
| 339 | input_free_device(vi->idev);
|
| 340 | err_input_alloc:
|
| 341 | vdev->config->del_vqs(vdev);
|
| 342 | err_init_vq:
|
| 343 | kfree(vi);
|
| 344 | return err;
|
| 345 | }
|
| 346 |
|
| 347 | static void virtinput_remove(struct virtio_device *vdev)
|
| 348 | {
|
| 349 | struct virtio_input *vi = vdev->priv;
|
| 350 | void *buf;
|
| 351 | unsigned long flags;
|
| 352 |
|
| 353 | spin_lock_irqsave(&vi->lock, flags);
|
| 354 | vi->ready = false;
|
| 355 | spin_unlock_irqrestore(&vi->lock, flags);
|
| 356 |
|
| 357 |
|
| 358 | virtio_reset_device(vdev);
|
| 359 | input_unregister_device(vi->idev);
|
| 360 | while ((buf = virtqueue_detach_unused_buf(vi->sts)) != NULL)
|
| 361 | kfree(buf);
|
| 362 | vdev->config->del_vqs(vdev);
|
| 363 | kfree(vi);
|
| 364 | }
|
| 365 |
|
| 366 | #ifdef CONFIG_PM_SLEEP
|
| 367 | static int virtinput_freeze(struct virtio_device *vdev)
|
| 368 | {
|
| 369 | struct virtio_input *vi = vdev->priv;
|
| 370 | unsigned long flags;
|
| 371 | void *buf;
|
| 372 |
|
| 373 | spin_lock_irqsave(&vi->lock, flags);
|
| 374 | vi->ready = false;
|
| 375 | spin_unlock_irqrestore(&vi->lock, flags);
|
| 376 |
|
| 377 | virtio_reset_device(vdev);
|
| 378 | while ((buf = virtqueue_detach_unused_buf(vi->sts)) != NULL)
|
| 379 | kfree(buf);
|
| 380 | vdev->config->del_vqs(vdev);
|
| 381 | return 0;
|
| 382 | }
|
| 383 |
|
| 384 | static int virtinput_restore(struct virtio_device *vdev)
|
| 385 | {
|
| 386 | struct virtio_input *vi = vdev->priv;
|
| 387 | int err;
|
| 388 |
|
| 389 | err = virtinput_init_vqs(vi);
|
| 390 | if (err)
|
| 391 | return err;
|
| 392 |
|
| 393 | virtio_device_ready(vdev);
|
| 394 | vi->ready = true;
|
| 395 | virtinput_fill_evt(vi);
|
| 396 | return 0;
|
| 397 | }
|
| 398 | #endif
|
| 399 |
|
| 400 | static unsigned int features[] = {
|
| 401 |
|
| 402 | };
|
| 403 | static const struct virtio_device_id id_table[] = {
|
| 404 | { VIRTIO_ID_INPUT, VIRTIO_DEV_ANY_ID },
|
| 405 | { 0 },
|
| 406 | };
|
| 407 |
|
| 408 | static struct virtio_driver virtio_input_driver = {
|
| 409 | .driver.name = KBUILD_MODNAME,
|
| 410 | .feature_table = features,
|
| 411 | .feature_table_size = ARRAY_SIZE(features),
|
| 412 | .id_table = id_table,
|
| 413 | .probe = virtinput_probe,
|
| 414 | .remove = virtinput_remove,
|
| 415 | #ifdef CONFIG_PM_SLEEP
|
| 416 | .freeze = virtinput_freeze,
|
| 417 | .restore = virtinput_restore,
|
| 418 | #endif
|
| 419 | };
|
| 420 |
|
| 421 | module_virtio_driver(virtio_input_driver);
|
| 422 | MODULE_DEVICE_TABLE(virtio, id_table);
|
| 423 |
|
| 424 | MODULE_LICENSE("GPL");
|
| 425 | MODULE_DESCRIPTION("Virtio input device driver");
|
| 426 | MODULE_AUTHOR("Gerd Hoffmann <[email protected]>");
|