Linux Audio

Check our new training course

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