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