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>
8 #include <uapi/linux/virtio_ids.h>
9 #include <uapi/linux/virtio_input.h>
12 struct virtio_device
*vdev
;
13 struct input_dev
*idev
;
17 struct virtqueue
*evt
, *sts
;
18 struct virtio_input_event evts
[64];
23 static void virtinput_queue_evtbuf(struct virtio_input
*vi
,
24 struct virtio_input_event
*evtbuf
)
26 struct scatterlist sg
[1];
28 sg_init_one(sg
, evtbuf
, sizeof(*evtbuf
));
29 virtqueue_add_inbuf(vi
->evt
, sg
, 1, evtbuf
, GFP_ATOMIC
);
32 static void virtinput_recv_events(struct virtqueue
*vq
)
34 struct virtio_input
*vi
= vq
->vdev
->priv
;
35 struct virtio_input_event
*event
;
39 spin_lock_irqsave(&vi
->lock
, flags
);
41 while ((event
= virtqueue_get_buf(vi
->evt
, &len
)) != NULL
) {
42 spin_unlock_irqrestore(&vi
->lock
, flags
);
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
);
52 spin_unlock_irqrestore(&vi
->lock
, flags
);
56 * On error we are losing the status update, which isn't critical as
57 * this is typically used for stuff like keyboard leds.
59 static int virtinput_send_status(struct virtio_input
*vi
,
60 u16 type
, u16 code
, s32 value
)
62 struct virtio_input_event
*stsbuf
;
63 struct scatterlist sg
[1];
67 stsbuf
= kzalloc(sizeof(*stsbuf
), GFP_ATOMIC
);
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
));
76 spin_lock_irqsave(&vi
->lock
, flags
);
78 rc
= virtqueue_add_outbuf(vi
->sts
, sg
, 1, stsbuf
, GFP_ATOMIC
);
79 virtqueue_kick(vi
->sts
);
83 spin_unlock_irqrestore(&vi
->lock
, flags
);
90 static void virtinput_recv_status(struct virtqueue
*vq
)
92 struct virtio_input
*vi
= vq
->vdev
->priv
;
93 struct virtio_input_event
*stsbuf
;
97 spin_lock_irqsave(&vi
->lock
, flags
);
98 while ((stsbuf
= virtqueue_get_buf(vi
->sts
, &len
)) != NULL
)
100 spin_unlock_irqrestore(&vi
->lock
, flags
);
103 static int virtinput_status(struct input_dev
*idev
, unsigned int type
,
104 unsigned int code
, int value
)
106 struct virtio_input
*vi
= input_get_drvdata(idev
);
108 return virtinput_send_status(vi
, type
, code
, value
);
111 static u8
virtinput_cfg_select(struct virtio_input
*vi
,
112 u8 select
, u8 subsel
)
116 virtio_cwrite(vi
->vdev
, struct virtio_input_config
, select
, &select
);
117 virtio_cwrite(vi
->vdev
, struct virtio_input_config
, subsel
, &subsel
);
118 virtio_cread(vi
->vdev
, struct virtio_input_config
, size
, &size
);
122 static void virtinput_cfg_bits(struct virtio_input
*vi
, int select
, int subsel
,
123 unsigned long *bits
, unsigned int bitcount
)
129 bytes
= virtinput_cfg_select(vi
, select
, subsel
);
132 if (bitcount
> bytes
* 8)
133 bitcount
= bytes
* 8;
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
140 virtio_bits
= kzalloc(bytes
, GFP_KERNEL
);
143 virtio_cread_bytes(vi
->vdev
, offsetof(struct virtio_input_config
,
146 for (bit
= 0; bit
< bitcount
; bit
++) {
147 if (virtio_bits
[bit
/ 8] & (1 << (bit
% 8)))
148 __set_bit(bit
, bits
);
152 if (select
== VIRTIO_INPUT_CFG_EV_BITS
)
153 __set_bit(subsel
, vi
->idev
->evbit
);
156 static void virtinput_cfg_abs(struct virtio_input
*vi
, int abs
)
158 u32 mi
, ma
, re
, fu
, fl
;
160 virtinput_cfg_select(vi
, VIRTIO_INPUT_CFG_ABS_INFO
, abs
);
161 virtio_cread(vi
->vdev
, struct virtio_input_config
, u
.abs
.min
, &mi
);
162 virtio_cread(vi
->vdev
, struct virtio_input_config
, u
.abs
.max
, &ma
);
163 virtio_cread(vi
->vdev
, struct virtio_input_config
, u
.abs
.res
, &re
);
164 virtio_cread(vi
->vdev
, struct virtio_input_config
, u
.abs
.fuzz
, &fu
);
165 virtio_cread(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
);
170 static int virtinput_init_vqs(struct virtio_input
*vi
)
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" };
178 err
= virtio_find_vqs(vi
->vdev
, 2, vqs
, cbs
, names
, NULL
);
187 static void virtinput_fill_evt(struct virtio_input
*vi
)
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
);
202 static int virtinput_probe(struct virtio_device
*vdev
)
204 struct virtio_input
*vi
;
209 if (!virtio_has_feature(vdev
, VIRTIO_F_VERSION_1
))
212 vi
= kzalloc(sizeof(*vi
), GFP_KERNEL
);
218 spin_lock_init(&vi
->lock
);
220 err
= virtinput_init_vqs(vi
);
224 vi
->idev
= input_allocate_device();
227 goto err_input_alloc
;
229 input_set_drvdata(vi
->idev
, vi
);
231 size
= virtinput_cfg_select(vi
, VIRTIO_INPUT_CFG_ID_NAME
, 0);
232 virtio_cread_bytes(vi
->vdev
, offsetof(struct virtio_input_config
,
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
,
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
;
245 size
= virtinput_cfg_select(vi
, VIRTIO_INPUT_CFG_ID_DEVIDS
, 0);
246 if (size
>= sizeof(struct virtio_input_devids
)) {
247 virtio_cread(vi
->vdev
, struct virtio_input_config
,
248 u
.ids
.bustype
, &vi
->idev
->id
.bustype
);
249 virtio_cread(vi
->vdev
, struct virtio_input_config
,
250 u
.ids
.vendor
, &vi
->idev
->id
.vendor
);
251 virtio_cread(vi
->vdev
, struct virtio_input_config
,
252 u
.ids
.product
, &vi
->idev
->id
.product
);
253 virtio_cread(vi
->vdev
, struct virtio_input_config
,
254 u
.ids
.version
, &vi
->idev
->id
.version
);
256 vi
->idev
->id
.bustype
= BUS_VIRTUAL
;
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
);
263 __set_bit(EV_REP
, vi
->idev
->evbit
);
265 vi
->idev
->dev
.parent
= &vdev
->dev
;
266 vi
->idev
->event
= virtinput_status
;
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
);
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
);
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
))
290 virtinput_cfg_abs(vi
, abs
);
294 virtio_device_ready(vdev
);
296 err
= input_register_device(vi
->idev
);
298 goto err_input_register
;
300 virtinput_fill_evt(vi
);
304 spin_lock_irqsave(&vi
->lock
, flags
);
306 spin_unlock_irqrestore(&vi
->lock
, flags
);
307 input_free_device(vi
->idev
);
309 vdev
->config
->del_vqs(vdev
);
315 static void virtinput_remove(struct virtio_device
*vdev
)
317 struct virtio_input
*vi
= vdev
->priv
;
321 spin_lock_irqsave(&vi
->lock
, flags
);
323 spin_unlock_irqrestore(&vi
->lock
, flags
);
325 input_unregister_device(vi
->idev
);
326 vdev
->config
->reset(vdev
);
327 while ((buf
= virtqueue_detach_unused_buf(vi
->sts
)) != NULL
)
329 vdev
->config
->del_vqs(vdev
);
333 #ifdef CONFIG_PM_SLEEP
334 static int virtinput_freeze(struct virtio_device
*vdev
)
336 struct virtio_input
*vi
= vdev
->priv
;
339 spin_lock_irqsave(&vi
->lock
, flags
);
341 spin_unlock_irqrestore(&vi
->lock
, flags
);
343 vdev
->config
->del_vqs(vdev
);
347 static int virtinput_restore(struct virtio_device
*vdev
)
349 struct virtio_input
*vi
= vdev
->priv
;
352 err
= virtinput_init_vqs(vi
);
356 virtio_device_ready(vdev
);
358 virtinput_fill_evt(vi
);
363 static unsigned int features
[] = {
366 static struct virtio_device_id id_table
[] = {
367 { VIRTIO_ID_INPUT
, VIRTIO_DEV_ANY_ID
},
371 static 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
,
385 module_virtio_driver(virtio_input_driver
);
386 MODULE_DEVICE_TABLE(virtio
, id_table
);
388 MODULE_LICENSE("GPL");
389 MODULE_DESCRIPTION("Virtio input device driver");
390 MODULE_AUTHOR("Gerd Hoffmann <kraxel@redhat.com>");