• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #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 
10 struct 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 
virtinput_queue_evtbuf(struct virtio_input * vi,struct virtio_input_event * evtbuf)22 static 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 
virtinput_recv_events(struct virtqueue * vq)31 static 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  */
virtinput_send_status(struct virtio_input * vi,u16 type,u16 code,s32 value)58 static 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 
virtinput_recv_status(struct virtqueue * vq)89 static 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 
virtinput_status(struct input_dev * idev,unsigned int type,unsigned int code,int value)102 static 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 
virtinput_cfg_select(struct virtio_input * vi,u8 select,u8 subsel)110 static 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 
virtinput_cfg_bits(struct virtio_input * vi,int select,int subsel,unsigned long * bits,unsigned int bitcount)121 static 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 
virtinput_cfg_abs(struct virtio_input * vi,int abs)155 static 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 		int input_max_finger = ma;
169 		// Set the VirtIO Input Device to be multi-touch.
170 		// In the Ranchu kernel, there is multi-touch-specific
171 		// code for handling ABS_MT_SLOT events.
172 		// See drivers/input/input.c:input_handle_abs_event.
173 		// If we do not issue input_mt_init_slots,
174 		// the kernel will filter out needed ABS_MT_SLOT
175 		// events when we touch the screen in more than one place,
176 		// preventing multi-touch with more than one finger from
177 		// working.
178 		input_mt_init_slots(vi->idev, input_max_finger, 0);
179 	}
180 }
181 
virtinput_init_vqs(struct virtio_input * vi)182 static int virtinput_init_vqs(struct virtio_input *vi)
183 {
184 	struct virtqueue *vqs[2];
185 	vq_callback_t *cbs[] = { virtinput_recv_events,
186 				 virtinput_recv_status };
187 	static const char * const names[] = { "events", "status" };
188 	int err;
189 
190 	err = virtio_find_vqs(vi->vdev, 2, vqs, cbs, names, NULL);
191 	if (err)
192 		return err;
193 	vi->evt = vqs[0];
194 	vi->sts = vqs[1];
195 
196 	return 0;
197 }
198 
virtinput_fill_evt(struct virtio_input * vi)199 static void virtinput_fill_evt(struct virtio_input *vi)
200 {
201 	unsigned long flags;
202 	int i, size;
203 
204 	spin_lock_irqsave(&vi->lock, flags);
205 	size = virtqueue_get_vring_size(vi->evt);
206 	if (size > ARRAY_SIZE(vi->evts))
207 		size = ARRAY_SIZE(vi->evts);
208 	for (i = 0; i < size; i++)
209 		virtinput_queue_evtbuf(vi, &vi->evts[i]);
210 	virtqueue_kick(vi->evt);
211 	spin_unlock_irqrestore(&vi->lock, flags);
212 }
213 
virtinput_probe(struct virtio_device * vdev)214 static int virtinput_probe(struct virtio_device *vdev)
215 {
216 	struct virtio_input *vi;
217 	unsigned long flags;
218 	size_t size;
219 	int abs, err;
220 
221 	if (!virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
222 		return -ENODEV;
223 
224 	vi = kzalloc(sizeof(*vi), GFP_KERNEL);
225 	if (!vi)
226 		return -ENOMEM;
227 
228 	vdev->priv = vi;
229 	vi->vdev = vdev;
230 	spin_lock_init(&vi->lock);
231 
232 	err = virtinput_init_vqs(vi);
233 	if (err)
234 		goto err_init_vq;
235 
236 	vi->idev = input_allocate_device();
237 	if (!vi->idev) {
238 		err = -ENOMEM;
239 		goto err_input_alloc;
240 	}
241 	input_set_drvdata(vi->idev, vi);
242 
243 	size = virtinput_cfg_select(vi, VIRTIO_INPUT_CFG_ID_NAME, 0);
244 	virtio_cread_bytes(vi->vdev, offsetof(struct virtio_input_config,
245 					      u.string),
246 			   vi->name, min(size, sizeof(vi->name)));
247 	size = virtinput_cfg_select(vi, VIRTIO_INPUT_CFG_ID_SERIAL, 0);
248 	virtio_cread_bytes(vi->vdev, offsetof(struct virtio_input_config,
249 					      u.string),
250 			   vi->serial, min(size, sizeof(vi->serial)));
251 	snprintf(vi->phys, sizeof(vi->phys),
252 		 "virtio%d/input0", vdev->index);
253 	vi->idev->name = vi->name;
254 	vi->idev->phys = vi->phys;
255 	vi->idev->uniq = vi->serial;
256 
257 	size = virtinput_cfg_select(vi, VIRTIO_INPUT_CFG_ID_DEVIDS, 0);
258 	if (size >= sizeof(struct virtio_input_devids)) {
259 		virtio_cread(vi->vdev, struct virtio_input_config,
260 			     u.ids.bustype, &vi->idev->id.bustype);
261 		virtio_cread(vi->vdev, struct virtio_input_config,
262 			     u.ids.vendor, &vi->idev->id.vendor);
263 		virtio_cread(vi->vdev, struct virtio_input_config,
264 			     u.ids.product, &vi->idev->id.product);
265 		virtio_cread(vi->vdev, struct virtio_input_config,
266 			     u.ids.version, &vi->idev->id.version);
267 	} else {
268 		vi->idev->id.bustype = BUS_VIRTUAL;
269 	}
270 
271 	virtinput_cfg_bits(vi, VIRTIO_INPUT_CFG_PROP_BITS, 0,
272 			   vi->idev->propbit, INPUT_PROP_CNT);
273 	size = virtinput_cfg_select(vi, VIRTIO_INPUT_CFG_EV_BITS, EV_REP);
274 	if (size)
275 		__set_bit(EV_REP, vi->idev->evbit);
276 
277 	vi->idev->dev.parent = &vdev->dev;
278 	vi->idev->event = virtinput_status;
279 
280 	/* device -> kernel */
281 	virtinput_cfg_bits(vi, VIRTIO_INPUT_CFG_EV_BITS, EV_KEY,
282 			   vi->idev->keybit, KEY_CNT);
283 	virtinput_cfg_bits(vi, VIRTIO_INPUT_CFG_EV_BITS, EV_REL,
284 			   vi->idev->relbit, REL_CNT);
285 	virtinput_cfg_bits(vi, VIRTIO_INPUT_CFG_EV_BITS, EV_ABS,
286 			   vi->idev->absbit, ABS_CNT);
287 	virtinput_cfg_bits(vi, VIRTIO_INPUT_CFG_EV_BITS, EV_MSC,
288 			   vi->idev->mscbit, MSC_CNT);
289 	virtinput_cfg_bits(vi, VIRTIO_INPUT_CFG_EV_BITS, EV_SW,
290 			   vi->idev->swbit,  SW_CNT);
291 
292 	/* kernel -> device */
293 	virtinput_cfg_bits(vi, VIRTIO_INPUT_CFG_EV_BITS, EV_LED,
294 			   vi->idev->ledbit, LED_CNT);
295 	virtinput_cfg_bits(vi, VIRTIO_INPUT_CFG_EV_BITS, EV_SND,
296 			   vi->idev->sndbit, SND_CNT);
297 
298 	if (test_bit(EV_ABS, vi->idev->evbit)) {
299 		for (abs = 0; abs < ABS_CNT; abs++) {
300 			if (!test_bit(abs, vi->idev->absbit))
301 				continue;
302 			virtinput_cfg_abs(vi, abs);
303 		}
304 	}
305 
306 	virtio_device_ready(vdev);
307 	vi->ready = true;
308 	err = input_register_device(vi->idev);
309 	if (err)
310 		goto err_input_register;
311 
312 	virtinput_fill_evt(vi);
313 	return 0;
314 
315 err_input_register:
316 	spin_lock_irqsave(&vi->lock, flags);
317 	vi->ready = false;
318 	spin_unlock_irqrestore(&vi->lock, flags);
319 	input_free_device(vi->idev);
320 err_input_alloc:
321 	vdev->config->del_vqs(vdev);
322 err_init_vq:
323 	kfree(vi);
324 	return err;
325 }
326 
virtinput_remove(struct virtio_device * vdev)327 static void virtinput_remove(struct virtio_device *vdev)
328 {
329 	struct virtio_input *vi = vdev->priv;
330 	void *buf;
331 	unsigned long flags;
332 
333 	spin_lock_irqsave(&vi->lock, flags);
334 	vi->ready = false;
335 	spin_unlock_irqrestore(&vi->lock, flags);
336 
337 	input_unregister_device(vi->idev);
338 	vdev->config->reset(vdev);
339 	while ((buf = virtqueue_detach_unused_buf(vi->sts)) != NULL)
340 		kfree(buf);
341 	vdev->config->del_vqs(vdev);
342 	kfree(vi);
343 }
344 
345 #ifdef CONFIG_PM_SLEEP
virtinput_freeze(struct virtio_device * vdev)346 static int virtinput_freeze(struct virtio_device *vdev)
347 {
348 	struct virtio_input *vi = vdev->priv;
349 	unsigned long flags;
350 
351 	spin_lock_irqsave(&vi->lock, flags);
352 	vi->ready = false;
353 	spin_unlock_irqrestore(&vi->lock, flags);
354 
355 	vdev->config->del_vqs(vdev);
356 	return 0;
357 }
358 
virtinput_restore(struct virtio_device * vdev)359 static int virtinput_restore(struct virtio_device *vdev)
360 {
361 	struct virtio_input *vi = vdev->priv;
362 	int err;
363 
364 	err = virtinput_init_vqs(vi);
365 	if (err)
366 		return err;
367 
368 	virtio_device_ready(vdev);
369 	vi->ready = true;
370 	virtinput_fill_evt(vi);
371 	return 0;
372 }
373 #endif
374 
375 static unsigned int features[] = {
376 	/* none */
377 };
378 static struct virtio_device_id id_table[] = {
379 	{ VIRTIO_ID_INPUT, VIRTIO_DEV_ANY_ID },
380 	{ 0 },
381 };
382 
383 static struct virtio_driver virtio_input_driver = {
384 	.driver.name         = KBUILD_MODNAME,
385 	.driver.owner        = THIS_MODULE,
386 	.feature_table       = features,
387 	.feature_table_size  = ARRAY_SIZE(features),
388 	.id_table            = id_table,
389 	.probe               = virtinput_probe,
390 	.remove              = virtinput_remove,
391 #ifdef CONFIG_PM_SLEEP
392 	.freeze	             = virtinput_freeze,
393 	.restore             = virtinput_restore,
394 #endif
395 };
396 
397 module_virtio_driver(virtio_input_driver);
398 MODULE_DEVICE_TABLE(virtio, id_table);
399 
400 MODULE_LICENSE("GPL");
401 MODULE_DESCRIPTION("Virtio input device driver");
402 MODULE_AUTHOR("Gerd Hoffmann <kraxel@redhat.com>");
403