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