1 // SPDX-License-Identifier: GPL-2.0-only
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>
8 #include <uapi/linux/virtio_ids.h>
9 #include <uapi/linux/virtio_input.h>
12 struct virtio_device *vdev;
13 struct input_dev *idev;
17 struct virtqueue *evt, *sts;
18 struct virtio_input_event evts[64];
23 static void virtinput_queue_evtbuf(struct virtio_input *vi,
24 struct virtio_input_event *evtbuf)
26 struct scatterlist sg[1];
28 sg_init_one(sg, evtbuf, sizeof(*evtbuf));
29 virtqueue_add_inbuf(vi->evt, sg, 1, evtbuf, GFP_ATOMIC);
32 static void virtinput_recv_events(struct virtqueue *vq)
34 struct virtio_input *vi = vq->vdev->priv;
35 struct virtio_input_event *event;
39 spin_lock_irqsave(&vi->lock, flags);
41 while ((event = virtqueue_get_buf(vi->evt, &len)) != NULL) {
42 spin_unlock_irqrestore(&vi->lock, flags);
44 le16_to_cpu(event->type),
45 le16_to_cpu(event->code),
46 le32_to_cpu(event->value));
47 spin_lock_irqsave(&vi->lock, flags);
48 virtinput_queue_evtbuf(vi, event);
52 spin_unlock_irqrestore(&vi->lock, flags);
56 * On error we are losing the status update, which isn't critical as
57 * this is typically used for stuff like keyboard leds.
59 static int virtinput_send_status(struct virtio_input *vi,
60 u16 type, u16 code, s32 value)
62 struct virtio_input_event *stsbuf;
63 struct scatterlist sg[1];
68 * Since 29cc309d8bf1 (HID: hid-multitouch: forward MSC_TIMESTAMP),
69 * EV_MSC/MSC_TIMESTAMP is added to each before EV_SYN event.
70 * EV_MSC is configured as INPUT_PASS_TO_ALL.
71 * In case of touch device:
72 * BE pass EV_MSC/MSC_TIMESTAMP to FE on receiving event from evdev.
73 * FE pass EV_MSC/MSC_TIMESTAMP back to BE.
74 * BE writes EV_MSC/MSC_TIMESTAMP to evdev due to INPUT_PASS_TO_ALL.
75 * BE receives extra EV_MSC/MSC_TIMESTAMP and pass to FE.
76 * >>> Each new frame becomes larger and larger.
77 * Disable EV_MSC/MSC_TIMESTAMP forwarding for MT.
79 if (vi->idev->mt && type == EV_MSC && code == MSC_TIMESTAMP)
82 stsbuf = kzalloc(sizeof(*stsbuf), GFP_ATOMIC);
86 stsbuf->type = cpu_to_le16(type);
87 stsbuf->code = cpu_to_le16(code);
88 stsbuf->value = cpu_to_le32(value);
89 sg_init_one(sg, stsbuf, sizeof(*stsbuf));
91 spin_lock_irqsave(&vi->lock, flags);
93 rc = virtqueue_add_outbuf(vi->sts, sg, 1, stsbuf, GFP_ATOMIC);
94 virtqueue_kick(vi->sts);
98 spin_unlock_irqrestore(&vi->lock, flags);
105 static void virtinput_recv_status(struct virtqueue *vq)
107 struct virtio_input *vi = vq->vdev->priv;
108 struct virtio_input_event *stsbuf;
112 spin_lock_irqsave(&vi->lock, flags);
113 while ((stsbuf = virtqueue_get_buf(vi->sts, &len)) != NULL)
115 spin_unlock_irqrestore(&vi->lock, flags);
118 static int virtinput_status(struct input_dev *idev, unsigned int type,
119 unsigned int code, int value)
121 struct virtio_input *vi = input_get_drvdata(idev);
123 return virtinput_send_status(vi, type, code, value);
126 static u8 virtinput_cfg_select(struct virtio_input *vi,
127 u8 select, u8 subsel)
131 virtio_cwrite_le(vi->vdev, struct virtio_input_config, select, &select);
132 virtio_cwrite_le(vi->vdev, struct virtio_input_config, subsel, &subsel);
133 virtio_cread_le(vi->vdev, struct virtio_input_config, size, &size);
137 static void virtinput_cfg_bits(struct virtio_input *vi, int select, int subsel,
138 unsigned long *bits, unsigned int bitcount)
144 bytes = virtinput_cfg_select(vi, select, subsel);
147 if (bitcount > bytes * 8)
148 bitcount = bytes * 8;
151 * Bitmap in virtio config space is a simple stream of bytes,
152 * with the first byte carrying bits 0-7, second bits 8-15 and
155 virtio_bits = kzalloc(bytes, GFP_KERNEL);
158 virtio_cread_bytes(vi->vdev, offsetof(struct virtio_input_config,
161 for (bit = 0; bit < bitcount; bit++) {
162 if (virtio_bits[bit / 8] & (1 << (bit % 8)))
163 __set_bit(bit, bits);
167 if (select == VIRTIO_INPUT_CFG_EV_BITS)
168 __set_bit(subsel, vi->idev->evbit);
171 static void virtinput_cfg_abs(struct virtio_input *vi, int abs)
173 u32 mi, ma, re, fu, fl;
175 virtinput_cfg_select(vi, VIRTIO_INPUT_CFG_ABS_INFO, abs);
176 virtio_cread_le(vi->vdev, struct virtio_input_config, u.abs.min, &mi);
177 virtio_cread_le(vi->vdev, struct virtio_input_config, u.abs.max, &ma);
178 virtio_cread_le(vi->vdev, struct virtio_input_config, u.abs.res, &re);
179 virtio_cread_le(vi->vdev, struct virtio_input_config, u.abs.fuzz, &fu);
180 virtio_cread_le(vi->vdev, struct virtio_input_config, u.abs.flat, &fl);
181 input_set_abs_params(vi->idev, abs, mi, ma, fu, fl);
182 input_abs_set_res(vi->idev, abs, re);
185 static int virtinput_init_vqs(struct virtio_input *vi)
187 struct virtqueue *vqs[2];
188 vq_callback_t *cbs[] = { virtinput_recv_events,
189 virtinput_recv_status };
190 static const char * const names[] = { "events", "status" };
193 err = virtio_find_vqs(vi->vdev, 2, vqs, cbs, names, NULL);
202 static void virtinput_fill_evt(struct virtio_input *vi)
207 spin_lock_irqsave(&vi->lock, flags);
208 size = virtqueue_get_vring_size(vi->evt);
209 if (size > ARRAY_SIZE(vi->evts))
210 size = ARRAY_SIZE(vi->evts);
211 for (i = 0; i < size; i++)
212 virtinput_queue_evtbuf(vi, &vi->evts[i]);
213 virtqueue_kick(vi->evt);
214 spin_unlock_irqrestore(&vi->lock, flags);
217 static int virtinput_probe(struct virtio_device *vdev)
219 struct virtio_input *vi;
224 if (!virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
227 vi = kzalloc(sizeof(*vi), GFP_KERNEL);
233 spin_lock_init(&vi->lock);
235 err = virtinput_init_vqs(vi);
239 vi->idev = input_allocate_device();
242 goto err_input_alloc;
244 input_set_drvdata(vi->idev, vi);
246 size = virtinput_cfg_select(vi, VIRTIO_INPUT_CFG_ID_NAME, 0);
247 virtio_cread_bytes(vi->vdev, offsetof(struct virtio_input_config,
249 vi->name, min(size, sizeof(vi->name)));
250 size = virtinput_cfg_select(vi, VIRTIO_INPUT_CFG_ID_SERIAL, 0);
251 virtio_cread_bytes(vi->vdev, offsetof(struct virtio_input_config,
253 vi->serial, min(size, sizeof(vi->serial)));
254 snprintf(vi->phys, sizeof(vi->phys),
255 "virtio%d/input0", vdev->index);
256 vi->idev->name = vi->name;
257 vi->idev->phys = vi->phys;
258 vi->idev->uniq = vi->serial;
260 size = virtinput_cfg_select(vi, VIRTIO_INPUT_CFG_ID_DEVIDS, 0);
261 if (size >= sizeof(struct virtio_input_devids)) {
262 virtio_cread_le(vi->vdev, struct virtio_input_config,
263 u.ids.bustype, &vi->idev->id.bustype);
264 virtio_cread_le(vi->vdev, struct virtio_input_config,
265 u.ids.vendor, &vi->idev->id.vendor);
266 virtio_cread_le(vi->vdev, struct virtio_input_config,
267 u.ids.product, &vi->idev->id.product);
268 virtio_cread_le(vi->vdev, struct virtio_input_config,
269 u.ids.version, &vi->idev->id.version);
271 vi->idev->id.bustype = BUS_VIRTUAL;
274 virtinput_cfg_bits(vi, VIRTIO_INPUT_CFG_PROP_BITS, 0,
275 vi->idev->propbit, INPUT_PROP_CNT);
276 size = virtinput_cfg_select(vi, VIRTIO_INPUT_CFG_EV_BITS, EV_REP);
278 __set_bit(EV_REP, vi->idev->evbit);
280 vi->idev->dev.parent = &vdev->dev;
281 vi->idev->event = virtinput_status;
283 /* device -> kernel */
284 virtinput_cfg_bits(vi, VIRTIO_INPUT_CFG_EV_BITS, EV_KEY,
285 vi->idev->keybit, KEY_CNT);
286 virtinput_cfg_bits(vi, VIRTIO_INPUT_CFG_EV_BITS, EV_REL,
287 vi->idev->relbit, REL_CNT);
288 virtinput_cfg_bits(vi, VIRTIO_INPUT_CFG_EV_BITS, EV_ABS,
289 vi->idev->absbit, ABS_CNT);
290 virtinput_cfg_bits(vi, VIRTIO_INPUT_CFG_EV_BITS, EV_MSC,
291 vi->idev->mscbit, MSC_CNT);
292 virtinput_cfg_bits(vi, VIRTIO_INPUT_CFG_EV_BITS, EV_SW,
293 vi->idev->swbit, SW_CNT);
295 /* kernel -> device */
296 virtinput_cfg_bits(vi, VIRTIO_INPUT_CFG_EV_BITS, EV_LED,
297 vi->idev->ledbit, LED_CNT);
298 virtinput_cfg_bits(vi, VIRTIO_INPUT_CFG_EV_BITS, EV_SND,
299 vi->idev->sndbit, SND_CNT);
301 if (test_bit(EV_ABS, vi->idev->evbit)) {
302 for (abs = 0; abs < ABS_CNT; abs++) {
303 if (!test_bit(abs, vi->idev->absbit))
305 virtinput_cfg_abs(vi, abs);
309 virtio_device_ready(vdev);
311 err = input_register_device(vi->idev);
313 goto err_input_register;
315 virtinput_fill_evt(vi);
319 spin_lock_irqsave(&vi->lock, flags);
321 spin_unlock_irqrestore(&vi->lock, flags);
322 input_free_device(vi->idev);
324 vdev->config->del_vqs(vdev);
330 static void virtinput_remove(struct virtio_device *vdev)
332 struct virtio_input *vi = vdev->priv;
336 spin_lock_irqsave(&vi->lock, flags);
338 spin_unlock_irqrestore(&vi->lock, flags);
340 input_unregister_device(vi->idev);
341 vdev->config->reset(vdev);
342 while ((buf = virtqueue_detach_unused_buf(vi->sts)) != NULL)
344 vdev->config->del_vqs(vdev);
348 #ifdef CONFIG_PM_SLEEP
349 static int virtinput_freeze(struct virtio_device *vdev)
351 struct virtio_input *vi = vdev->priv;
354 spin_lock_irqsave(&vi->lock, flags);
356 spin_unlock_irqrestore(&vi->lock, flags);
358 vdev->config->del_vqs(vdev);
362 static int virtinput_restore(struct virtio_device *vdev)
364 struct virtio_input *vi = vdev->priv;
367 err = virtinput_init_vqs(vi);
371 virtio_device_ready(vdev);
373 virtinput_fill_evt(vi);
378 static unsigned int features[] = {
381 static const struct virtio_device_id id_table[] = {
382 { VIRTIO_ID_INPUT, VIRTIO_DEV_ANY_ID },
386 static struct virtio_driver virtio_input_driver = {
387 .driver.name = KBUILD_MODNAME,
388 .driver.owner = THIS_MODULE,
389 .feature_table = features,
390 .feature_table_size = ARRAY_SIZE(features),
391 .id_table = id_table,
392 .probe = virtinput_probe,
393 .remove = virtinput_remove,
394 #ifdef CONFIG_PM_SLEEP
395 .freeze = virtinput_freeze,
396 .restore = virtinput_restore,
400 module_virtio_driver(virtio_input_driver);
401 MODULE_DEVICE_TABLE(virtio, id_table);
403 MODULE_LICENSE("GPL");
404 MODULE_DESCRIPTION("Virtio input device driver");