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>
7 #include <uapi/linux/virtio_ids.h>
8 #include <uapi/linux/virtio_input.h>
11 struct virtio_device
*vdev
;
12 struct input_dev
*idev
;
16 struct virtqueue
*evt
, *sts
;
17 struct virtio_input_event evts
[64];
22 static void virtinput_queue_evtbuf(struct virtio_input
*vi
,
23 struct virtio_input_event
*evtbuf
)
25 struct scatterlist sg
[1];
27 sg_init_one(sg
, evtbuf
, sizeof(*evtbuf
));
28 virtqueue_add_inbuf(vi
->evt
, sg
, 1, evtbuf
, GFP_ATOMIC
);
31 static void virtinput_recv_events(struct virtqueue
*vq
)
33 struct virtio_input
*vi
= vq
->vdev
->priv
;
34 struct virtio_input_event
*event
;
38 spin_lock_irqsave(&vi
->lock
, flags
);
40 while ((event
= virtqueue_get_buf(vi
->evt
, &len
)) != NULL
) {
41 spin_unlock_irqrestore(&vi
->lock
, flags
);
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
);
51 spin_unlock_irqrestore(&vi
->lock
, flags
);
55 * On error we are losing the status update, which isn't critical as
56 * this is typically used for stuff like keyboard leds.
58 static int virtinput_send_status(struct virtio_input
*vi
,
59 u16 type
, u16 code
, s32 value
)
61 struct virtio_input_event
*stsbuf
;
62 struct scatterlist sg
[1];
66 stsbuf
= kzalloc(sizeof(*stsbuf
), GFP_ATOMIC
);
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
));
75 spin_lock_irqsave(&vi
->lock
, flags
);
77 rc
= virtqueue_add_outbuf(vi
->sts
, sg
, 1, stsbuf
, GFP_ATOMIC
);
78 virtqueue_kick(vi
->sts
);
82 spin_unlock_irqrestore(&vi
->lock
, flags
);
89 static void virtinput_recv_status(struct virtqueue
*vq
)
91 struct virtio_input
*vi
= vq
->vdev
->priv
;
92 struct virtio_input_event
*stsbuf
;
96 spin_lock_irqsave(&vi
->lock
, flags
);
97 while ((stsbuf
= virtqueue_get_buf(vi
->sts
, &len
)) != NULL
)
99 spin_unlock_irqrestore(&vi
->lock
, flags
);
102 static int virtinput_status(struct input_dev
*idev
, unsigned int type
,
103 unsigned int code
, int value
)
105 struct virtio_input
*vi
= input_get_drvdata(idev
);
107 return virtinput_send_status(vi
, type
, code
, value
);
110 static u8
virtinput_cfg_select(struct virtio_input
*vi
,
111 u8 select
, u8 subsel
)
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
);
121 static void virtinput_cfg_bits(struct virtio_input
*vi
, int select
, int subsel
,
122 unsigned long *bits
, unsigned int bitcount
)
128 bytes
= virtinput_cfg_select(vi
, select
, subsel
);
131 if (bitcount
> bytes
* 8)
132 bitcount
= bytes
* 8;
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
139 virtio_bits
= kzalloc(bytes
, GFP_KERNEL
);
142 virtio_cread_bytes(vi
->vdev
, offsetof(struct virtio_input_config
,
145 for (bit
= 0; bit
< bitcount
; bit
++) {
146 if (virtio_bits
[bit
/ 8] & (1 << (bit
% 8)))
147 __set_bit(bit
, bits
);
151 if (select
== VIRTIO_INPUT_CFG_EV_BITS
)
152 __set_bit(subsel
, vi
->idev
->evbit
);
155 static void virtinput_cfg_abs(struct virtio_input
*vi
, int abs
)
157 u32 mi
, ma
, re
, fu
, fl
;
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
);
169 static int virtinput_init_vqs(struct virtio_input
*vi
)
171 struct virtqueue
*vqs
[2];
172 vq_callback_t
*cbs
[] = { virtinput_recv_events
,
173 virtinput_recv_status
};
174 static const char * const names
[] = { "events", "status" };
177 err
= virtio_find_vqs(vi
->vdev
, 2, vqs
, cbs
, names
, NULL
);
186 static void virtinput_fill_evt(struct virtio_input
*vi
)
191 spin_lock_irqsave(&vi
->lock
, flags
);
192 size
= virtqueue_get_vring_size(vi
->evt
);
193 if (size
> ARRAY_SIZE(vi
->evts
))
194 size
= ARRAY_SIZE(vi
->evts
);
195 for (i
= 0; i
< size
; i
++)
196 virtinput_queue_evtbuf(vi
, &vi
->evts
[i
]);
197 virtqueue_kick(vi
->evt
);
198 spin_unlock_irqrestore(&vi
->lock
, flags
);
201 static int virtinput_probe(struct virtio_device
*vdev
)
203 struct virtio_input
*vi
;
208 if (!virtio_has_feature(vdev
, VIRTIO_F_VERSION_1
))
211 vi
= kzalloc(sizeof(*vi
), GFP_KERNEL
);
217 spin_lock_init(&vi
->lock
);
219 err
= virtinput_init_vqs(vi
);
223 vi
->idev
= input_allocate_device();
226 goto err_input_alloc
;
228 input_set_drvdata(vi
->idev
, vi
);
230 size
= virtinput_cfg_select(vi
, VIRTIO_INPUT_CFG_ID_NAME
, 0);
231 virtio_cread_bytes(vi
->vdev
, offsetof(struct virtio_input_config
,
233 vi
->name
, min(size
, sizeof(vi
->name
)));
234 size
= virtinput_cfg_select(vi
, VIRTIO_INPUT_CFG_ID_SERIAL
, 0);
235 virtio_cread_bytes(vi
->vdev
, offsetof(struct virtio_input_config
,
237 vi
->serial
, min(size
, sizeof(vi
->serial
)));
238 snprintf(vi
->phys
, sizeof(vi
->phys
),
239 "virtio%d/input0", vdev
->index
);
240 vi
->idev
->name
= vi
->name
;
241 vi
->idev
->phys
= vi
->phys
;
242 vi
->idev
->uniq
= vi
->serial
;
244 size
= virtinput_cfg_select(vi
, VIRTIO_INPUT_CFG_ID_DEVIDS
, 0);
245 if (size
>= sizeof(struct virtio_input_devids
)) {
246 virtio_cread(vi
->vdev
, struct virtio_input_config
,
247 u
.ids
.bustype
, &vi
->idev
->id
.bustype
);
248 virtio_cread(vi
->vdev
, struct virtio_input_config
,
249 u
.ids
.vendor
, &vi
->idev
->id
.vendor
);
250 virtio_cread(vi
->vdev
, struct virtio_input_config
,
251 u
.ids
.product
, &vi
->idev
->id
.product
);
252 virtio_cread(vi
->vdev
, struct virtio_input_config
,
253 u
.ids
.version
, &vi
->idev
->id
.version
);
255 vi
->idev
->id
.bustype
= BUS_VIRTUAL
;
258 virtinput_cfg_bits(vi
, VIRTIO_INPUT_CFG_PROP_BITS
, 0,
259 vi
->idev
->propbit
, INPUT_PROP_CNT
);
260 size
= virtinput_cfg_select(vi
, VIRTIO_INPUT_CFG_EV_BITS
, EV_REP
);
262 __set_bit(EV_REP
, vi
->idev
->evbit
);
264 vi
->idev
->dev
.parent
= &vdev
->dev
;
265 vi
->idev
->event
= virtinput_status
;
267 /* device -> kernel */
268 virtinput_cfg_bits(vi
, VIRTIO_INPUT_CFG_EV_BITS
, EV_KEY
,
269 vi
->idev
->keybit
, KEY_CNT
);
270 virtinput_cfg_bits(vi
, VIRTIO_INPUT_CFG_EV_BITS
, EV_REL
,
271 vi
->idev
->relbit
, REL_CNT
);
272 virtinput_cfg_bits(vi
, VIRTIO_INPUT_CFG_EV_BITS
, EV_ABS
,
273 vi
->idev
->absbit
, ABS_CNT
);
274 virtinput_cfg_bits(vi
, VIRTIO_INPUT_CFG_EV_BITS
, EV_MSC
,
275 vi
->idev
->mscbit
, MSC_CNT
);
276 virtinput_cfg_bits(vi
, VIRTIO_INPUT_CFG_EV_BITS
, EV_SW
,
277 vi
->idev
->swbit
, SW_CNT
);
279 /* kernel -> device */
280 virtinput_cfg_bits(vi
, VIRTIO_INPUT_CFG_EV_BITS
, EV_LED
,
281 vi
->idev
->ledbit
, LED_CNT
);
282 virtinput_cfg_bits(vi
, VIRTIO_INPUT_CFG_EV_BITS
, EV_SND
,
283 vi
->idev
->sndbit
, SND_CNT
);
285 if (test_bit(EV_ABS
, vi
->idev
->evbit
)) {
286 for (abs
= 0; abs
< ABS_CNT
; abs
++) {
287 if (!test_bit(abs
, vi
->idev
->absbit
))
289 virtinput_cfg_abs(vi
, abs
);
293 virtio_device_ready(vdev
);
295 err
= input_register_device(vi
->idev
);
297 goto err_input_register
;
299 virtinput_fill_evt(vi
);
303 spin_lock_irqsave(&vi
->lock
, flags
);
305 spin_unlock_irqrestore(&vi
->lock
, flags
);
306 input_free_device(vi
->idev
);
308 vdev
->config
->del_vqs(vdev
);
314 static void virtinput_remove(struct virtio_device
*vdev
)
316 struct virtio_input
*vi
= vdev
->priv
;
320 spin_lock_irqsave(&vi
->lock
, flags
);
322 spin_unlock_irqrestore(&vi
->lock
, flags
);
324 input_unregister_device(vi
->idev
);
325 vdev
->config
->reset(vdev
);
326 while ((buf
= virtqueue_detach_unused_buf(vi
->sts
)) != NULL
)
328 vdev
->config
->del_vqs(vdev
);
332 #ifdef CONFIG_PM_SLEEP
333 static int virtinput_freeze(struct virtio_device
*vdev
)
335 struct virtio_input
*vi
= vdev
->priv
;
338 spin_lock_irqsave(&vi
->lock
, flags
);
340 spin_unlock_irqrestore(&vi
->lock
, flags
);
342 vdev
->config
->del_vqs(vdev
);
346 static int virtinput_restore(struct virtio_device
*vdev
)
348 struct virtio_input
*vi
= vdev
->priv
;
351 err
= virtinput_init_vqs(vi
);
355 virtio_device_ready(vdev
);
357 virtinput_fill_evt(vi
);
362 static unsigned int features
[] = {
365 static struct virtio_device_id id_table
[] = {
366 { VIRTIO_ID_INPUT
, VIRTIO_DEV_ANY_ID
},
370 static struct virtio_driver virtio_input_driver
= {
371 .driver
.name
= KBUILD_MODNAME
,
372 .driver
.owner
= THIS_MODULE
,
373 .feature_table
= features
,
374 .feature_table_size
= ARRAY_SIZE(features
),
375 .id_table
= id_table
,
376 .probe
= virtinput_probe
,
377 .remove
= virtinput_remove
,
378 #ifdef CONFIG_PM_SLEEP
379 .freeze
= virtinput_freeze
,
380 .restore
= virtinput_restore
,
384 module_virtio_driver(virtio_input_driver
);
385 MODULE_DEVICE_TABLE(virtio
, id_table
);
387 MODULE_LICENSE("GPL");
388 MODULE_DESCRIPTION("Virtio input device driver");
389 MODULE_AUTHOR("Gerd Hoffmann <kraxel@redhat.com>");