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