summaryrefslogtreecommitdiffstats
path: root/drivers/input/virtio_input.c
blob: 655a9051726a59d7218f3bb36da237a4773cdf46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// SPDX-License-Identifier: GPL-2.0-only

#include <common.h>
#include <poller.h>
#include <linux/virtio.h>
#include <linux/virtio_config.h>
#include <linux/virtio_ring.h>
#include <input/input.h>
#include <sound.h>
#include <dt-bindings/input/linux-event-codes.h>

#include <uapi/linux/virtio_ids.h>
#include <uapi/linux/virtio_input.h>

struct virtio_input {
	struct input_device        idev;
	struct virtio_device       *vdev;
	struct virtqueue           *evt, *sts;
	struct virtio_input_event  evts[64];
	struct poller_struct       poller;
	struct sound_card          beeper;
	unsigned long              sndbit[BITS_TO_LONGS(SND_CNT)];
};

static void virtinput_queue_evtbuf(struct virtio_input *vi,
				   struct virtio_input_event *evtbuf)
{
	struct virtio_sg sg[1];
	virtio_sg_init_one(sg, evtbuf, sizeof(*evtbuf));
	virtqueue_add_inbuf(vi->evt, sg, 1);
}

static int virtinput_recv_events(struct virtio_input *vi)
{
	struct device *dev = &vi->vdev->dev;
	struct virtio_input_event *event;
	unsigned int len;
	int i = 0;

	while ((event = virtqueue_get_buf(vi->evt, &len)) != NULL) {
		if (le16_to_cpu(event->type) == EV_KEY)
			input_report_key_event(&vi->idev, le16_to_cpu(event->code),
					       le32_to_cpu(event->value));

		pr_debug("\n%s: input event #%td received (type=%u, code=%u, value=%u)\n",
			 dev_name(dev),
			 event - &vi->evts[0],
			 le16_to_cpu(event->type), le16_to_cpu(event->code),
			 le32_to_cpu(event->value));

		virtinput_queue_evtbuf(vi, event);
		i++;
	}

	return i;
}

/*
 * On error we are losing the status update, which isn't critical as
 * this is used for the bell.
 */
static int virtinput_send_status(struct sound_card *beeper, unsigned freq, unsigned duration)
{
	struct virtio_input *vi = container_of(beeper, struct virtio_input, beeper);
	struct virtio_input_event *stsbuf;
	struct virtio_sg sg[1];
	u16 code;
	int rc;

	stsbuf = kzalloc(sizeof(*stsbuf), 0);
	if (!stsbuf)
		return -ENOMEM;

	code = vi->sndbit[0] & BIT_MASK(SND_TONE) ? SND_TONE : SND_BELL;

	stsbuf->type  = cpu_to_le16(EV_SND);
	stsbuf->code  = cpu_to_le16(code);
	stsbuf->value = cpu_to_le32(freq);
	virtio_sg_init_one(sg, stsbuf, sizeof(*stsbuf));

	rc = virtqueue_add_outbuf(vi->sts, sg, 1);
	virtqueue_kick(vi->sts);

	if (rc != 0)
		kfree(stsbuf);
	return rc;
}

static int virtinput_recv_status(struct virtio_input *vi)
{
	struct virtio_input_event *stsbuf;
	unsigned int len;
	int i = 0;

	while ((stsbuf = virtqueue_get_buf(vi->sts, &len)) != NULL) {
		kfree(stsbuf);
		i++;
	}

	return i;
}

static void virtinput_poll_vqs(struct poller_struct *poller)
{
	struct virtio_input *vi = container_of(poller, struct virtio_input, poller);

	int bufs = 0;

	bufs += virtinput_recv_events(vi);
	bufs += virtinput_recv_status(vi);

	if (bufs)
		virtqueue_kick(vi->evt);
}

static u8 virtinput_cfg_select(struct virtio_input *vi,
			       u8 select, u8 subsel)
{
	u8 size;

	virtio_cwrite_le(vi->vdev, struct virtio_input_config, select, &select);
	virtio_cwrite_le(vi->vdev, struct virtio_input_config, subsel, &subsel);
	virtio_cread_le(vi->vdev, struct virtio_input_config, size, &size);
	return size;
}

static void virtinput_cfg_bits(struct virtio_input *vi, int select, int subsel,
			       unsigned long *bits, unsigned int bitcount)
{
	unsigned int bit;
	u8 *virtio_bits;
	u8 bytes;

	bytes = virtinput_cfg_select(vi, select, subsel);
	if (!bytes)
		return;
	if (bitcount > bytes * 8)
		bitcount = bytes * 8;

	/*
	 * Bitmap in virtio config space is a simple stream of bytes,
	 * with the first byte carrying bits 0-7, second bits 8-15 and
	 * so on.
	 */
	virtio_bits = kzalloc(bytes, GFP_KERNEL);
	if (!virtio_bits)
		return;
	virtio_cread_bytes(vi->vdev, offsetof(struct virtio_input_config,
					      u.bitmap),
			   virtio_bits, bytes);
	for (bit = 0; bit < bitcount; bit++) {
		if (virtio_bits[bit / 8] & (1 << (bit % 8)))
			__set_bit(bit, bits);
	}
	kfree(virtio_bits);
}

static void virtinput_fill_evt(struct virtio_input *vi)
{
	int i, size;

	size = virtqueue_get_vring_size(vi->evt);
	if (size > ARRAY_SIZE(vi->evts))
		size = ARRAY_SIZE(vi->evts);
	for (i = 0; i < size; i++)
		virtinput_queue_evtbuf(vi, &vi->evts[i]);
	virtqueue_kick(vi->evt);
}

static int virtinput_init_vqs(struct virtio_input *vi)
{
	struct virtqueue *vqs[2];
	int err;


	err = virtio_find_vqs(vi->vdev, 2, vqs);
	if (err)
		return err;

	vi->evt = vqs[0];
	vi->sts = vqs[1];

	return 0;
}

static int virtinput_probe(struct virtio_device *vdev)
{
	struct virtio_input *vi;
	char name[64];
	size_t size;
	int err;

	if (!virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
		return -ENODEV;

	vi = kzalloc(sizeof(*vi), GFP_KERNEL);
	if (!vi)
		return -ENOMEM;

	vdev->priv = vi;
	vi->vdev = vdev;

	err = virtinput_init_vqs(vi);
	if (err)
		goto err_init_vq;

	size = virtinput_cfg_select(vi, VIRTIO_INPUT_CFG_ID_NAME, 0);
	virtio_cread_bytes(vi->vdev, offsetof(struct virtio_input_config, u.string),
			   name, min(size, sizeof(name)));
	name[size] = '\0';

	dev_info(&vdev->dev, "'%s' detected\n", name);

	virtinput_cfg_bits(vi, VIRTIO_INPUT_CFG_EV_BITS, EV_SND,
			   vi->sndbit, SND_CNT);

	virtio_device_ready(vdev);

	vi->idev.parent = &vdev->dev;
	err = input_device_register(&vi->idev);
	if (err)
		goto err_input_register;

	virtinput_fill_evt(vi);

	vi->poller.func = virtinput_poll_vqs;
	snprintf(name, sizeof(name), "%s/input0", dev_name(&vdev->dev));

	err = poller_register(&vi->poller, name);
	if (err)
		goto err_poller_register;

	if (IS_ENABLED(CONFIG_SOUND) &&
	    (vi->sndbit[0] & (BIT_MASK(SND_BELL) | BIT_MASK(SND_TONE)))) {
		struct sound_card *beeper;

		beeper = &vi->beeper;
		beeper->name = basprintf("%s/beeper0", dev_name(&vdev->dev));
		beeper->beep = virtinput_send_status;

		err = sound_card_register(&vi->beeper);
		if (err)
			dev_warn(&vdev->dev, "bell registration failed: %pe\n", ERR_PTR(err));
		else
			dev_info(&vdev->dev, "bell registered\n");
	}

	return 0;

err_poller_register:
	input_device_unregister(&vi->idev);
err_input_register:
	vdev->config->del_vqs(vdev);
err_init_vq:
	kfree(vi);
	return err;
}

static void virtinput_remove(struct virtio_device *vdev)
{
	struct virtio_input *vi = vdev->priv;

	vdev->config->reset(vdev);
	poller_unregister(&vi->poller);
	input_device_unregister(&vi->idev);
	vdev->config->del_vqs(vdev);

	kfree(vi);
}

static const struct virtio_device_id id_table[] = {
	{ VIRTIO_ID_INPUT, VIRTIO_DEV_ANY_ID },
	{ 0 },
};

static struct virtio_driver virtio_input_driver = {
	.driver.name         = "virtio_input",
	.id_table            = id_table,
	.probe               = virtinput_probe,
	.remove              = virtinput_remove,
};
device_virtio_driver(virtio_input_driver);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Virtio input device driver");
MODULE_AUTHOR("Gerd Hoffmann <kraxel@redhat.com>");
MODULE_AUTHOR("Ahmad Fatoum <a.fatoum@pengutronix.de>");