]> Git Repo - linux.git/blob - drivers/virtio/virtio_input.c
244965c20d9bc522ee88d0e73edd4c1d7f3f033c
[linux.git] / drivers / virtio / virtio_input.c
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
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
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
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  */
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         /*
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.
78          */
79         if (vi->idev->mt && type == EV_MSC && code == MSC_TIMESTAMP)
80                 return 0;
81
82         stsbuf = kzalloc(sizeof(*stsbuf), GFP_ATOMIC);
83         if (!stsbuf)
84                 return -ENOMEM;
85
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));
90
91         spin_lock_irqsave(&vi->lock, flags);
92         if (vi->ready) {
93                 rc = virtqueue_add_outbuf(vi->sts, sg, 1, stsbuf, GFP_ATOMIC);
94                 virtqueue_kick(vi->sts);
95         } else {
96                 rc = -ENODEV;
97         }
98         spin_unlock_irqrestore(&vi->lock, flags);
99
100         if (rc != 0)
101                 kfree(stsbuf);
102         return rc;
103 }
104
105 static void virtinput_recv_status(struct virtqueue *vq)
106 {
107         struct virtio_input *vi = vq->vdev->priv;
108         struct virtio_input_event *stsbuf;
109         unsigned long flags;
110         unsigned int len;
111
112         spin_lock_irqsave(&vi->lock, flags);
113         while ((stsbuf = virtqueue_get_buf(vi->sts, &len)) != NULL)
114                 kfree(stsbuf);
115         spin_unlock_irqrestore(&vi->lock, flags);
116 }
117
118 static int virtinput_status(struct input_dev *idev, unsigned int type,
119                             unsigned int code, int value)
120 {
121         struct virtio_input *vi = input_get_drvdata(idev);
122
123         return virtinput_send_status(vi, type, code, value);
124 }
125
126 static u8 virtinput_cfg_select(struct virtio_input *vi,
127                                u8 select, u8 subsel)
128 {
129         u8 size;
130
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);
134         return size;
135 }
136
137 static void virtinput_cfg_bits(struct virtio_input *vi, int select, int subsel,
138                                unsigned long *bits, unsigned int bitcount)
139 {
140         unsigned int bit;
141         u8 *virtio_bits;
142         u8 bytes;
143
144         bytes = virtinput_cfg_select(vi, select, subsel);
145         if (!bytes)
146                 return;
147         if (bitcount > bytes * 8)
148                 bitcount = bytes * 8;
149
150         /*
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
153          * so on.
154          */
155         virtio_bits = kzalloc(bytes, GFP_KERNEL);
156         if (!virtio_bits)
157                 return;
158         virtio_cread_bytes(vi->vdev, offsetof(struct virtio_input_config,
159                                               u.bitmap),
160                            virtio_bits, bytes);
161         for (bit = 0; bit < bitcount; bit++) {
162                 if (virtio_bits[bit / 8] & (1 << (bit % 8)))
163                         __set_bit(bit, bits);
164         }
165         kfree(virtio_bits);
166
167         if (select == VIRTIO_INPUT_CFG_EV_BITS)
168                 __set_bit(subsel, vi->idev->evbit);
169 }
170
171 static void virtinput_cfg_abs(struct virtio_input *vi, int abs)
172 {
173         u32 mi, ma, re, fu, fl;
174
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);
183 }
184
185 static int virtinput_init_vqs(struct virtio_input *vi)
186 {
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" };
191         int err;
192
193         err = virtio_find_vqs(vi->vdev, 2, vqs, cbs, names, NULL);
194         if (err)
195                 return err;
196         vi->evt = vqs[0];
197         vi->sts = vqs[1];
198
199         return 0;
200 }
201
202 static void virtinput_fill_evt(struct virtio_input *vi)
203 {
204         unsigned long flags;
205         int i, size;
206
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);
215 }
216
217 static int virtinput_probe(struct virtio_device *vdev)
218 {
219         struct virtio_input *vi;
220         unsigned long flags;
221         size_t size;
222         int abs, err;
223
224         if (!virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
225                 return -ENODEV;
226
227         vi = kzalloc(sizeof(*vi), GFP_KERNEL);
228         if (!vi)
229                 return -ENOMEM;
230
231         vdev->priv = vi;
232         vi->vdev = vdev;
233         spin_lock_init(&vi->lock);
234
235         err = virtinput_init_vqs(vi);
236         if (err)
237                 goto err_init_vq;
238
239         vi->idev = input_allocate_device();
240         if (!vi->idev) {
241                 err = -ENOMEM;
242                 goto err_input_alloc;
243         }
244         input_set_drvdata(vi->idev, vi);
245
246         size = virtinput_cfg_select(vi, VIRTIO_INPUT_CFG_ID_NAME, 0);
247         virtio_cread_bytes(vi->vdev, offsetof(struct virtio_input_config,
248                                               u.string),
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,
252                                               u.string),
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;
259
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);
270         } else {
271                 vi->idev->id.bustype = BUS_VIRTUAL;
272         }
273
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);
277         if (size)
278                 __set_bit(EV_REP, vi->idev->evbit);
279
280         vi->idev->dev.parent = &vdev->dev;
281         vi->idev->event = virtinput_status;
282
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);
294
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);
300
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))
304                                 continue;
305                         virtinput_cfg_abs(vi, abs);
306                 }
307         }
308
309         virtio_device_ready(vdev);
310         vi->ready = true;
311         err = input_register_device(vi->idev);
312         if (err)
313                 goto err_input_register;
314
315         virtinput_fill_evt(vi);
316         return 0;
317
318 err_input_register:
319         spin_lock_irqsave(&vi->lock, flags);
320         vi->ready = false;
321         spin_unlock_irqrestore(&vi->lock, flags);
322         input_free_device(vi->idev);
323 err_input_alloc:
324         vdev->config->del_vqs(vdev);
325 err_init_vq:
326         kfree(vi);
327         return err;
328 }
329
330 static void virtinput_remove(struct virtio_device *vdev)
331 {
332         struct virtio_input *vi = vdev->priv;
333         void *buf;
334         unsigned long flags;
335
336         spin_lock_irqsave(&vi->lock, flags);
337         vi->ready = false;
338         spin_unlock_irqrestore(&vi->lock, flags);
339
340         input_unregister_device(vi->idev);
341         vdev->config->reset(vdev);
342         while ((buf = virtqueue_detach_unused_buf(vi->sts)) != NULL)
343                 kfree(buf);
344         vdev->config->del_vqs(vdev);
345         kfree(vi);
346 }
347
348 #ifdef CONFIG_PM_SLEEP
349 static int virtinput_freeze(struct virtio_device *vdev)
350 {
351         struct virtio_input *vi = vdev->priv;
352         unsigned long flags;
353
354         spin_lock_irqsave(&vi->lock, flags);
355         vi->ready = false;
356         spin_unlock_irqrestore(&vi->lock, flags);
357
358         vdev->config->del_vqs(vdev);
359         return 0;
360 }
361
362 static int virtinput_restore(struct virtio_device *vdev)
363 {
364         struct virtio_input *vi = vdev->priv;
365         int err;
366
367         err = virtinput_init_vqs(vi);
368         if (err)
369                 return err;
370
371         virtio_device_ready(vdev);
372         vi->ready = true;
373         virtinput_fill_evt(vi);
374         return 0;
375 }
376 #endif
377
378 static unsigned int features[] = {
379         /* none */
380 };
381 static const struct virtio_device_id id_table[] = {
382         { VIRTIO_ID_INPUT, VIRTIO_DEV_ANY_ID },
383         { 0 },
384 };
385
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,
397 #endif
398 };
399
400 module_virtio_driver(virtio_input_driver);
401 MODULE_DEVICE_TABLE(virtio, id_table);
402
403 MODULE_LICENSE("GPL");
404 MODULE_DESCRIPTION("Virtio input device driver");
405 MODULE_AUTHOR("Gerd Hoffmann <[email protected]>");
This page took 0.040688 seconds and 2 git commands to generate.