Add virtio-input driver.
Subsystems (3)
Gerd Hoffmann
maintainerPATCH v4 1/1 Add virtio-input driver.
virtio-input is basically evdev-events-over-virtio, so this driver isn't much more than reading configuration from config space and forwarding incoming events to the linux input layer.
--- MAINTAINERS | 6 + drivers/virtio/Kconfig | 10 ++ drivers/virtio/Makefile | 1 + drivers/virtio/virtio_input.c | 363 ++++++++++++++++++++++++++++++++++++++ include/uapi/linux/Kbuild | 1 + include/uapi/linux/virtio_ids.h | 1 + include/uapi/linux/virtio_input.h | 76 ++++++++ 7 files changed, 458 insertions(+) create mode 100644 drivers/virtio/virtio_input.c create mode 100644 include/uapi/linux/virtio_input.h
MAINTAINERS +6 -0
@@ -10442,6 +10442,12 @@ S: Maintained F: drivers/vhost/ F: include/uapi/linux/vhost.h +VIRTIO INPUT DRIVER +M: Gerd Hoffmann <[email protected]> +S: Maintained +F: drivers/virtio/virtio_input.c +F: include/uapi/linux/virtio_input.h + VIA RHINE NETWORK DRIVER M: Roger Luethi <[email protected]> S: Maintained
drivers/virtio/Kconfig +10 -0
@@ -48,6 +48,16 @@ config VIRTIO_BALLOON If unsure, say M. +config VIRTIO_INPUT + tristate "Virtio input driver" + depends on VIRTIO + depends on INPUT + ---help--- + This driver supports virtio input devices such as + keyboards, mice and tablets. + + If unsure, say M. + config VIRTIO_MMIO tristate "Platform bus driver for memory mapped virtio devices" depends on HAS_IOMEM
drivers/virtio/Makefile +1 -0
@@ -4,3 +4,4 @@ obj-$(CONFIG_VIRTIO_PCI) += virtio_pci.o virtio_pci-y := virtio_pci_modern.o virtio_pci_common.o virtio_pci-$(CONFIG_VIRTIO_PCI_LEGACY) += virtio_pci_legacy.o obj-$(CONFIG_VIRTIO_BALLOON) += virtio_balloon.o +obj-$(CONFIG_VIRTIO_INPUT) += virtio_input.o
drivers/virtio/virtio_input.c new file +363 -0
363 lines not on this page · raw message
include/uapi/linux/Kbuild +1 -0
@@ -430,6 +430,7 @@ header-y += virtio_blk.h header-y += virtio_config.h header-y += virtio_console.h header-y += virtio_ids.h +header-y += virtio_input.h header-y += virtio_net.h header-y += virtio_pci.h header-y += virtio_ring.h
include/uapi/linux/virtio_ids.h +1 -0
@@ -39,5 +39,6 @@#define VIRTIO_ID_9P 9/* 9p virtio console */ #define VIRTIO_ID_RPROC_SERIAL 11/* virtio remoteproc serial link */ #define VIRTIO_ID_CAIF 12/* Virtio caif */ +#define VIRTIO_ID_INPUT 18/* virtio input */ #endif /* _LINUX_VIRTIO_IDS_H */
include/uapi/linux/virtio_input.h new file +76 -0
76 lines not on this page · raw message
Signature · 1 line
1.8.3.1
Quoted 5 lines · Gerd Hoffmann
virtio-input is basically evdev-events-over-virtio, so this driver isn't much more than reading configuration from config space and forwarding incoming events to the linux input layer.
Signed-off-by Gerd Hoffmann <[email protected]>
Is the input layer sane? I've never dealt with it, so I don't know.
What's it used for?
Imagine a future virtio standard which incorporates this. And a Windows or FreeBSD implementation of the device and or driver. How ugly would they be?
Thanks,
Rusty.
Quoted 8 lines · Rusty Russell
Quoted 5 lines · Gerd Hoffmann
virtio-input is basically evdev-events-over-virtio, so this driver isn't much more than reading configuration from config space and forwarding incoming events to the linux input layer.
Signed-off-by Gerd Hoffmann <[email protected]>Is the input layer sane? I've never dealt with it, so I don't know.
I'm rather biased having designed it, but I'd say it's reasonable. It certainly has limitations and design mistakes, but none are too bad. One testimony to that Android has based its own Input API around it.
Quoted 1 line
What's it used for?
For all human input devices, like keyboards, mice, touchscreens, etc.
Quoted 3 lines
Imagine a future virtio standard which incorporates this. And a Windows or FreeBSD implementation of the device and or driver. How ugly would they be?
A windows translation layer is fairly easy, people porting software from Windows to Linux told me numerous times that adapting isn't hard. I also believe that at least one of the BSD's has a compatible implementation these days based on the fact that I was asked to allow copying the headers in the past.
Signature · 2 lines
Vojtech Pavlik Director SUSE Labs
Quoted 10 lines · Vojtech Pavlik
Quoted 3 lines · Rusty Russell
Imagine a future virtio standard which incorporates this. And a Windows or FreeBSD implementation of the device and or driver. How ugly would they be?
A windows translation layer is fairly easy, people porting software from Windows to Linux told me numerous times that adapting isn't hard. I also believe that at least one of the BSD's has a compatible implementation these days based on the fact that I was asked to allow copying the headers in the past.
Thanks Vojtech, that answers my questions.
I figure we apply this and see where it leads...
Cheers,
Rusty.
Quoted 10 lines · Vojtech Pavlik
Quoted 3 lines · Rusty Russell
Imagine a future virtio standard which incorporates this. And a Windows or FreeBSD implementation of the device and or driver. How ugly would they be?
A windows translation layer is fairly easy, people porting software from Windows to Linux told me numerous times that adapting isn't hard. I also believe that at least one of the BSD's has a compatible implementation these days based on the fact that I was asked to allow copying the headers in the past.
Thanks Vojtech, that answers my questions.
I figure we apply this and see where it leads...
Cheers,
Rusty.
Quoted 8 lines · Rusty Russell
Quoted 5 lines · Gerd Hoffmann
virtio-input is basically evdev-events-over-virtio, so this driver isn't much more than reading configuration from config space and forwarding incoming events to the linux input layer.
Signed-off-by Gerd Hoffmann <[email protected]>Is the input layer sane? I've never dealt with it, so I don't know.
I'm rather biased having designed it, but I'd say it's reasonable. It certainly has limitations and design mistakes, but none are too bad. One testimony to that Android has based its own Input API around it.
Quoted 1 line
What's it used for?
For all human input devices, like keyboards, mice, touchscreens, etc.
Quoted 3 lines
Imagine a future virtio standard which incorporates this. And a Windows or FreeBSD implementation of the device and or driver. How ugly would they be?
A windows translation layer is fairly easy, people porting software from Windows to Linux told me numerous times that adapting isn't hard. I also believe that at least one of the BSD's has a compatible implementation these days based on the fact that I was asked to allow copying the headers in the past.
Signature · 2 lines
Vojtech Pavlik Director SUSE Labs
Quoted 5 lines · Gerd Hoffmann
virtio-input is basically evdev-events-over-virtio, so this driver isn't much more than reading configuration from config space and forwarding incoming events to the linux input layer.
Signed-off-by Gerd Hoffmann <[email protected]>
Is the input layer sane? I've never dealt with it, so I don't know.
What's it used for?
Imagine a future virtio standard which incorporates this. And a Windows or FreeBSD implementation of the device and or driver. How ugly would they be?
Thanks,
Rusty.
Michael S. Tsirkin
maintainerQuoted 5 lines · Gerd Hoffmann
virtio-input is basically evdev-events-over-virtio, so this driver isn't much more than reading configuration from config space and forwarding incoming events to the linux input layer.
Signed-off-by Gerd Hoffmann <[email protected]>
Looks good overallm though I think I still see a couple of minor issues.
Quoted 340 lines
--- MAINTAINERS | 6 + drivers/virtio/Kconfig | 10 ++ drivers/virtio/Makefile | 1 + drivers/virtio/virtio_input.c | 363 ++++++++++++++++++++++++++++++++++++++ include/uapi/linux/Kbuild | 1 + include/uapi/linux/virtio_ids.h | 1 + include/uapi/linux/virtio_input.h | 76 ++++++++ 7 files changed, 458 insertions(+) create mode 100644 drivers/virtio/virtio_input.c create mode 100644 include/uapi/linux/virtio_input.hMAINTAINERS +6
-0@@ -10442,6 +10442,12 @@ S: Maintained F: drivers/vhost/ F: include/uapi/linux/vhost.h +VIRTIO INPUT DRIVER +M: Gerd Hoffmann <[email protected]> +S: Maintained +F: drivers/virtio/virtio_input.c +F: include/uapi/linux/virtio_input.h + VIA RHINE NETWORK DRIVER M: Roger Luethi <[email protected]> S: Maintaineddrivers/virtio/Kconfig +10
-0@@ -48,6 +48,16 @@ config VIRTIO_BALLOON If unsure, say M. +config VIRTIO_INPUT + tristate "Virtio input driver" + depends on VIRTIO + depends on INPUT + ---help--- + This driver supports virtio input devices such as + keyboards, mice and tablets. + + If unsure, say M. + config VIRTIO_MMIO tristate "Platform bus driver for memory mapped virtio devices" depends on HAS_IOMEMdrivers/virtio/Makefile +1
-0@@ -4,3 +4,4 @@ obj-$(CONFIG_VIRTIO_PCI) += virtio_pci.o virtio_pci-y := virtio_pci_modern.o virtio_pci_common.o virtio_pci-$(CONFIG_VIRTIO_PCI_LEGACY) += virtio_pci_legacy.o obj-$(CONFIG_VIRTIO_BALLOON) += virtio_balloon.o +obj-$(CONFIG_VIRTIO_INPUT) += virtio_input.odrivers/virtio/virtio_input.c new file +275
-0@@ -0,0 +1,363 @@ +#include <linux/module.h> +#include <linux/virtio.h> +#include <linux/input.h> + +#include <uapi/linux/virtio_ids.h> +#include <uapi/linux/virtio_input.h> + +struct virtio_input { +struct virtio_device *vdev; +struct input_dev *idev; +char name[64 ]; +char serial[64 ]; +char phys[64 ]; +struct virtqueue *evt, *sts; +struct virtio_input_event evts[64 ]; +spinlock_t lock; +bool ready; +}; + +static void virtinput_queue_evtbuf (struct virtio_input *vi, +struct virtio_input_event *evtbuf) +{ +struct scatterlist sg[1 ]; + +sg_init_one (sg, evtbuf,sizeof (*evtbuf)); +virtqueue_add_inbuf (vi->evt, sg,1 , evtbuf,GFP_ATOMIC ); +} + +static void virtinput_recv_events (struct virtqueue *vq) +{ +struct virtio_input *vi = vq->vdev->priv; +struct virtio_input_event *event; +unsigned long flags; +unsigned int len; + +spin_lock_irqsave (&vi->lock, flags); +if (vi->ready) { +while ((event =virtqueue_get_buf (vi->evt, &len)) !=NULL ) { +input_event (vi->idev, +le16_to_cpu (event->type), +le16_to_cpu (event->code), +le32_to_cpu (event->value)); +virtinput_queue_evtbuf (vi, event); + } +virtqueue_kick (vq); + } +spin_unlock_irqrestore (&vi->lock, flags); +} + +static int virtinput_send_status (struct virtio_input *vi, +u16 type,u16 code,s32 value) +{ +struct virtio_input_event *stsbuf; +struct scatterlist sg[1 ]; +unsigned long flags; +int rc; + + stsbuf =kzalloc (sizeof (*stsbuf),GFP_ATOMIC ); +if (!stsbuf) +return -ENOMEM ; + + stsbuf->type =cpu_to_le16 (type); + stsbuf->code =cpu_to_le16 (code); + stsbuf->value =cpu_to_le32 (value); +sg_init_one (sg, stsbuf,sizeof (*stsbuf)); + +spin_lock_irqsave (&vi->lock, flags); +if (vi->ready) { + rc =virtqueue_add_outbuf (vi->sts, sg,1 , stsbuf,GFP_ATOMIC ); +virtqueue_kick (vi->sts); + }else { + rc = -ENODEV ; + } +spin_unlock_irqrestore (&vi->lock, flags); + +if (rc !=0 ) +kfree (stsbuf); +return rc; +} + +static void virtinput_recv_status (struct virtqueue *vq) +{ +struct virtio_input *vi = vq->vdev->priv; +struct virtio_input_event *stsbuf; +unsigned long flags; +unsigned int len; + +spin_lock_irqsave (&vi->lock, flags); +while ((stsbuf =virtqueue_get_buf (vi->sts, &len)) !=NULL ) +kfree (stsbuf); +spin_unlock_irqrestore (&vi->lock, flags); +} + +static int virtinput_status (struct input_dev *idev,unsigned int type, +unsigned int code,int value) +{ +struct virtio_input *vi =input_get_drvdata (idev); + +return virtinput_send_status (vi, type, code, value); +} + +static size_t virtinput_cfg_select (struct virtio_input *vi, +u8 select,u8 subsel) +{ +u8 size; + + virtio_cwrite(vi->vdev, struct virtio_input_config, select, &select); + virtio_cwrite(vi->vdev, struct virtio_input_config, subsel, &subsel); + virtio_cread(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; +size_t bytes; +u8 *virtio_bits; + + 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); + +if (select ==VIRTIO_INPUT_CFG_EV_BITS ) +__set_bit (subsel, vi->idev->evbit); +} + +static void virtinput_cfg_abs (struct virtio_input *vi,int abs) +{ +u32 mi, ma, re, fu, fl; + +virtinput_cfg_select (vi,VIRTIO_INPUT_CFG_ABS_INFO , abs); + virtio_cread(vi->vdev, struct virtio_input_config, u.abs.min, &mi); + virtio_cread(vi->vdev, struct virtio_input_config, u.abs.max, &ma); + virtio_cread(vi->vdev, struct virtio_input_config, u.abs.res, &re); + virtio_cread(vi->vdev, struct virtio_input_config, u.abs.fuzz, &fu); + virtio_cread(vi->vdev, struct virtio_input_config, u.abs.flat, &fl); +input_set_abs_params (vi->idev, abs, mi, ma, fu, fl); +input_abs_set_res (vi->idev, abs, re); +} + +static int virtinput_init_vqs (struct virtio_input *vi) +{ +struct virtqueue *vqs[2 ]; +vq_callback_t *cbs[] = { virtinput_recv_events, + virtinput_recv_status }; +static const char *names[] = {"events" ,"status" }; +int err; + + err = vi->vdev->config->find_vqs (vi->vdev,2 , vqs, cbs, names); +if (err) +return err; + vi->evt = vqs[0 ]; + vi->sts = vqs[1 ]; + +return 0 ; +} + +static void virtinput_fill_evt (struct virtio_input *vi) +{ +unsigned long flags; +int i, size; + +spin_lock_irqsave (&vi->lock, flags); + 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); +spin_unlock_irqrestore (&vi->lock, flags); +} + +static int virtinput_probe (struct virtio_device *vdev) +{ +struct virtio_input *vi; +size_t size; +int abs, 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; +spin_lock_init (&vi->lock); + + err =virtinput_init_vqs (vi); +if (err) + goto err_init_vq; + + vi->idev =input_allocate_device (); +if (!vi->idev) { + err = -ENOMEM ; + goto err_input_alloc; + } +input_set_drvdata (vi->idev, vi); + + size =virtinput_cfg_select (vi,VIRTIO_INPUT_CFG_ID_NAME ,0 ); +virtio_cread_bytes (vi->vdev, offsetof(struct virtio_input_config , + u.string), + vi->name, min(size, sizeof(vi->name))); + size =virtinput_cfg_select (vi,VIRTIO_INPUT_CFG_ID_SERIAL ,0 ); +virtio_cread_bytes (vi->vdev, offsetof(struct virtio_input_config , + u.string), + vi->serial, min(size, sizeof(vi->serial))); +snprintf (vi->phys,sizeof (vi->phys), +"virtio%d/input0" , vdev->index); + vi->idev->name = vi->name; + vi->idev->phys = vi->phys; + vi->idev->uniq = vi->serial; + + size =virtinput_cfg_select (vi,VIRTIO_INPUT_CFG_ID_DEVIDS ,0 ); +if (size >=sizeof (struct virtio_input_devids )) { + virtio_cread(vi->vdev, struct virtio_input_config, + u.ids.bustype, &vi->idev->id.bustype); + virtio_cread(vi->vdev, struct virtio_input_config, + u.ids.vendor, &vi->idev->id.vendor); + virtio_cread(vi->vdev, struct virtio_input_config, + u.ids.product, &vi->idev->id.product); + virtio_cread(vi->vdev, struct virtio_input_config, + u.ids.version, &vi->idev->id.version); + }else { + vi->idev->id.bustype =BUS_VIRTUAL ; + } + +virtinput_cfg_bits (vi,VIRTIO_INPUT_CFG_PROP_BITS ,0 , + vi->idev->propbit,INPUT_PROP_CNT ); + size =virtinput_cfg_select (vi,VIRTIO_INPUT_CFG_EV_BITS ,EV_REP ); +if (size) +__set_bit (EV_REP , vi->idev->evbit); + + vi->idev->dev.parent = &vdev->dev; + vi->idev->event = virtinput_status; + +/* device -> kernel */ +virtinput_cfg_bits (vi,VIRTIO_INPUT_CFG_EV_BITS ,EV_KEY , + vi->idev->keybit,KEY_CNT ); +virtinput_cfg_bits (vi,VIRTIO_INPUT_CFG_EV_BITS ,EV_REL , + vi->idev->relbit,REL_CNT ); +virtinput_cfg_bits (vi,VIRTIO_INPUT_CFG_EV_BITS ,EV_ABS , + vi->idev->absbit,ABS_CNT ); +virtinput_cfg_bits (vi,VIRTIO_INPUT_CFG_EV_BITS ,EV_MSC , + vi->idev->mscbit,MSC_CNT ); +virtinput_cfg_bits (vi,VIRTIO_INPUT_CFG_EV_BITS ,EV_SW , + vi->idev->swbit,SW_CNT ); + +/* kernel -> device */ +virtinput_cfg_bits (vi,VIRTIO_INPUT_CFG_EV_BITS ,EV_LED , + vi->idev->ledbit,LED_CNT ); +virtinput_cfg_bits (vi,VIRTIO_INPUT_CFG_EV_BITS ,EV_SND , + vi->idev->sndbit,SND_CNT ); +
drivers/virtio/virtio_input.c line 285
Quoted 10 lines
drivers/virtio/virtio_input.c new file +10
-0+if (test_bit (EV_ABS , vi->idev->evbit)) { +for (abs =0 ; abs <ABS_CNT ; abs++) { +if (!test_bit (abs, vi->idev->absbit)) +continue ; +virtinput_cfg_abs (vi, abs); + } + } + err =input_register_device (vi->idev); +if (err) + goto err_input_register;
vi->ready is false here so you might lose status updates. Losing updates isn't too terrible, but seems easy to fix:
virtio_device_ready(vdev); vi->ready = true; err = input_register_device(vi->idev);
...
err_input_register:
vi->ready = false;
it's up to you whether to do this.
Quoted 10 lines
drivers/virtio/virtio_input.c new file +10
-0+ +virtio_device_ready (vdev); + vi->ready = true; +virtinput_fill_evt (vi); +return 0 ; + +err_input_register: +input_free_device (vi->idev); +err_input_alloc: + vdev->config->del_vqs (vdev);
drivers/virtio/virtio_input.c line 305
Quoted 10 lines
drivers/virtio/virtio_input.c new file +10
-0+err_init_vq: +kfree (vi); +return err; +} + +static void virtinput_remove (struct virtio_device *vdev) +{ +struct virtio_input *vi = vdev->priv; + + vi->ready = false;
This one needs to be done under a lock, otherwise we can't be sure no callbacks are running here.
drivers/virtio/virtio_input.c line 316
Quoted 11 lines
drivers/virtio/virtio_input.c new file +11
-0+input_unregister_device (vi->idev); + vdev->config->del_vqs (vdev); +kfree (vi); +} + +#ifdef CONFIG_PM_SLEEP +static int virtinput_freeze (struct virtio_device *vdev) +{ +struct virtio_input *vi = vdev->priv; + + vi->ready = false;
Same here I think.
Quoted 12 lines
drivers/virtio/virtio_input.c new file +12
-0+ vdev->config->del_vqs (vdev); +return 0 ; +} + +static int virtinput_restore (struct virtio_device *vdev) +{ +struct virtio_input *vi = vdev->priv; +int err; + + err =virtinput_init_vqs (vi); +if (err) +return err;
drivers/virtio/virtio_input.c line 338
Quoted 10 lines
drivers/virtio/virtio_input.c new file +10
-0+ +virtio_device_ready (vdev); + vi->ready = true; +virtinput_fill_evt (vi); + return 0; +} +#endif + +static unsigned int features[] = { +};
an emoty line won't hurt here.
Quoted 72 lines
drivers/virtio/virtio_input.c new file +25
-0+static struct virtio_device_id id_table[] = { + {VIRTIO_ID_INPUT ,VIRTIO_DEV_ANY_ID }, + {0 }, +}; + +static struct virtio_driver virtio_input_driver = { + .driver.name =KBUILD_MODNAME , + .driver.owner =THIS_MODULE , + .feature_table = features, + .feature_table_size =ARRAY_SIZE (features), + .id_table = id_table, + .probe = virtinput_probe, + .remove = virtinput_remove, +#ifdef CONFIG_PM_SLEEP + .freeze = virtinput_freeze, + .restore = virtinput_restore, +#endif +}; + +module_virtio_driver (virtio_input_driver); +MODULE_DEVICE_TABLE (virtio, id_table); + +MODULE_LICENSE ("GPL" ); +MODULE_DESCRIPTION ("Virtio input device driver" ); +MODULE_AUTHOR ("Gerd Hoffmann <[email protected]>" );include/uapi/linux/Kbuild +1
-0@@ -430,6 +430,7 @@ header-y += virtio_blk.h header-y += virtio_config.h header-y += virtio_console.h header-y += virtio_ids.h +header-y += virtio_input.h header-y += virtio_net.h header-y += virtio_pci.h header-y += virtio_ring.hinclude/uapi/linux/virtio_ids.h +1
-0@@ -39,5 +39,6 @@#define VIRTIO_ID_9P 9/* 9p virtio console */ #define VIRTIO_ID_RPROC_SERIAL 11/* virtio remoteproc serial link */ #define VIRTIO_ID_CAIF 12/* Virtio caif */ +#define VIRTIO_ID_INPUT 18/* virtio input */ #endif /* _LINUX_VIRTIO_IDS_H */ include/uapi/linux/virtio_input.h new file +18
-0@@ -0,0 +1,76 @@ +#ifndef _LINUX_VIRTIO_INPUT_H +#define _LINUX_VIRTIO_INPUT_H +/* This header is BSD licensed so anyone can use the definitions to implement +* compatible drivers/servers. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* 1. Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* 2. Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in the +* documentation and/or other materials provided with the distribution. +* 3. Neither the name of IBM nor the names of its contributors +* may be used to endorse or promote products derived from this software +* without specific prior written permission. +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
include/uapi/linux/virtio_input.h line 28
Quoted 10 lines
include/uapi/linux/virtio_input.h new file +10
-0+* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL IBM OR +* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT +* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +* SUCH DAMAGE. */
You must include linux/types.h for __le types I think.
include/uapi/linux/virtio_input.h line 29
Quoted 1 line
include/uapi/linux/virtio_input.h new file +1
-0+#include <linux/virtio_ids.h>
This one is there in all other headers, so ok, though I don't know why.
include/uapi/linux/virtio_input.h line 30
Quoted 1 line
include/uapi/linux/virtio_input.h new file +1
-0+#include <linux/virtio_config.h>
I'm sure you don't need this one.
Quoted 21 lines
include/uapi/linux/virtio_input.h new file +21
-0+ +enum virtio_input_config_select { +VIRTIO_INPUT_CFG_UNSET =0x00 , +VIRTIO_INPUT_CFG_ID_NAME =0x01 , +VIRTIO_INPUT_CFG_ID_SERIAL =0x02 , +VIRTIO_INPUT_CFG_ID_DEVIDS =0x03 , +VIRTIO_INPUT_CFG_PROP_BITS =0x10 , +VIRTIO_INPUT_CFG_EV_BITS =0x11 , +VIRTIO_INPUT_CFG_ABS_INFO =0x12 , +}; + +struct virtio_input_absinfo { +__u32 min; +__u32 max; +__u32 fuzz; +__u32 flat; +__u32 res; +}; + +struct virtio_input_devids { +__u16 bustype;
include/uapi/linux/virtio_input.h line 61
Quoted 10 lines
include/uapi/linux/virtio_input.h new file +10
-0+__u16 vendor; +__u16 product; +__u16 version; +}; + +struct virtio_input_config { +__u8 select; +__u8 subsel; +__u8 size; +__u8 reserved;
Thinking more about it, might it be better to make this reserved[5] so the union is 8 byte aligned? Will be helpful if you later want a 64 bit field in the union.
Quoted 17 lines
include/uapi/linux/virtio_input.h new file +15
-0+union { +char string[128 ]; +__u8 bitmap[128 ]; +struct virtio_input_absinfo abs; +struct virtio_input_devids ids; + } u; +}; + +struct virtio_input_event { +__le16 type; +__le16 code; +__le32 value; +}; + +#endif /* _LINUX_VIRTIO_INPUT_H */ Signature · 1 line
1.8.3.1
Michael S. Tsirkin
maintainerQuoted 5 lines · Gerd Hoffmann
virtio-input is basically evdev-events-over-virtio, so this driver isn't much more than reading configuration from config space and forwarding incoming events to the linux input layer.
Signed-off-by Gerd Hoffmann <[email protected]>
Looks good overallm though I think I still see a couple of minor issues.
Quoted 340 lines
--- MAINTAINERS | 6 + drivers/virtio/Kconfig | 10 ++ drivers/virtio/Makefile | 1 + drivers/virtio/virtio_input.c | 363 ++++++++++++++++++++++++++++++++++++++ include/uapi/linux/Kbuild | 1 + include/uapi/linux/virtio_ids.h | 1 + include/uapi/linux/virtio_input.h | 76 ++++++++ 7 files changed, 458 insertions(+) create mode 100644 drivers/virtio/virtio_input.c create mode 100644 include/uapi/linux/virtio_input.hMAINTAINERS +6
-0@@ -10442,6 +10442,12 @@ S: Maintained F: drivers/vhost/ F: include/uapi/linux/vhost.h +VIRTIO INPUT DRIVER +M: Gerd Hoffmann <[email protected]> +S: Maintained +F: drivers/virtio/virtio_input.c +F: include/uapi/linux/virtio_input.h + VIA RHINE NETWORK DRIVER M: Roger Luethi <[email protected]> S: Maintaineddrivers/virtio/Kconfig +10
-0@@ -48,6 +48,16 @@ config VIRTIO_BALLOON If unsure, say M. +config VIRTIO_INPUT + tristate "Virtio input driver" + depends on VIRTIO + depends on INPUT + ---help--- + This driver supports virtio input devices such as + keyboards, mice and tablets. + + If unsure, say M. + config VIRTIO_MMIO tristate "Platform bus driver for memory mapped virtio devices" depends on HAS_IOMEMdrivers/virtio/Makefile +1
-0@@ -4,3 +4,4 @@ obj-$(CONFIG_VIRTIO_PCI) += virtio_pci.o virtio_pci-y := virtio_pci_modern.o virtio_pci_common.o virtio_pci-$(CONFIG_VIRTIO_PCI_LEGACY) += virtio_pci_legacy.o obj-$(CONFIG_VIRTIO_BALLOON) += virtio_balloon.o +obj-$(CONFIG_VIRTIO_INPUT) += virtio_input.odrivers/virtio/virtio_input.c new file +275
-0@@ -0,0 +1,363 @@ +#include <linux/module.h> +#include <linux/virtio.h> +#include <linux/input.h> + +#include <uapi/linux/virtio_ids.h> +#include <uapi/linux/virtio_input.h> + +struct virtio_input { +struct virtio_device *vdev; +struct input_dev *idev; +char name[64 ]; +char serial[64 ]; +char phys[64 ]; +struct virtqueue *evt, *sts; +struct virtio_input_event evts[64 ]; +spinlock_t lock; +bool ready; +}; + +static void virtinput_queue_evtbuf (struct virtio_input *vi, +struct virtio_input_event *evtbuf) +{ +struct scatterlist sg[1 ]; + +sg_init_one (sg, evtbuf,sizeof (*evtbuf)); +virtqueue_add_inbuf (vi->evt, sg,1 , evtbuf,GFP_ATOMIC ); +} + +static void virtinput_recv_events (struct virtqueue *vq) +{ +struct virtio_input *vi = vq->vdev->priv; +struct virtio_input_event *event; +unsigned long flags; +unsigned int len; + +spin_lock_irqsave (&vi->lock, flags); +if (vi->ready) { +while ((event =virtqueue_get_buf (vi->evt, &len)) !=NULL ) { +input_event (vi->idev, +le16_to_cpu (event->type), +le16_to_cpu (event->code), +le32_to_cpu (event->value)); +virtinput_queue_evtbuf (vi, event); + } +virtqueue_kick (vq); + } +spin_unlock_irqrestore (&vi->lock, flags); +} + +static int virtinput_send_status (struct virtio_input *vi, +u16 type,u16 code,s32 value) +{ +struct virtio_input_event *stsbuf; +struct scatterlist sg[1 ]; +unsigned long flags; +int rc; + + stsbuf =kzalloc (sizeof (*stsbuf),GFP_ATOMIC ); +if (!stsbuf) +return -ENOMEM ; + + stsbuf->type =cpu_to_le16 (type); + stsbuf->code =cpu_to_le16 (code); + stsbuf->value =cpu_to_le32 (value); +sg_init_one (sg, stsbuf,sizeof (*stsbuf)); + +spin_lock_irqsave (&vi->lock, flags); +if (vi->ready) { + rc =virtqueue_add_outbuf (vi->sts, sg,1 , stsbuf,GFP_ATOMIC ); +virtqueue_kick (vi->sts); + }else { + rc = -ENODEV ; + } +spin_unlock_irqrestore (&vi->lock, flags); + +if (rc !=0 ) +kfree (stsbuf); +return rc; +} + +static void virtinput_recv_status (struct virtqueue *vq) +{ +struct virtio_input *vi = vq->vdev->priv; +struct virtio_input_event *stsbuf; +unsigned long flags; +unsigned int len; + +spin_lock_irqsave (&vi->lock, flags); +while ((stsbuf =virtqueue_get_buf (vi->sts, &len)) !=NULL ) +kfree (stsbuf); +spin_unlock_irqrestore (&vi->lock, flags); +} + +static int virtinput_status (struct input_dev *idev,unsigned int type, +unsigned int code,int value) +{ +struct virtio_input *vi =input_get_drvdata (idev); + +return virtinput_send_status (vi, type, code, value); +} + +static size_t virtinput_cfg_select (struct virtio_input *vi, +u8 select,u8 subsel) +{ +u8 size; + + virtio_cwrite(vi->vdev, struct virtio_input_config, select, &select); + virtio_cwrite(vi->vdev, struct virtio_input_config, subsel, &subsel); + virtio_cread(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; +size_t bytes; +u8 *virtio_bits; + + 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); + +if (select ==VIRTIO_INPUT_CFG_EV_BITS ) +__set_bit (subsel, vi->idev->evbit); +} + +static void virtinput_cfg_abs (struct virtio_input *vi,int abs) +{ +u32 mi, ma, re, fu, fl; + +virtinput_cfg_select (vi,VIRTIO_INPUT_CFG_ABS_INFO , abs); + virtio_cread(vi->vdev, struct virtio_input_config, u.abs.min, &mi); + virtio_cread(vi->vdev, struct virtio_input_config, u.abs.max, &ma); + virtio_cread(vi->vdev, struct virtio_input_config, u.abs.res, &re); + virtio_cread(vi->vdev, struct virtio_input_config, u.abs.fuzz, &fu); + virtio_cread(vi->vdev, struct virtio_input_config, u.abs.flat, &fl); +input_set_abs_params (vi->idev, abs, mi, ma, fu, fl); +input_abs_set_res (vi->idev, abs, re); +} + +static int virtinput_init_vqs (struct virtio_input *vi) +{ +struct virtqueue *vqs[2 ]; +vq_callback_t *cbs[] = { virtinput_recv_events, + virtinput_recv_status }; +static const char *names[] = {"events" ,"status" }; +int err; + + err = vi->vdev->config->find_vqs (vi->vdev,2 , vqs, cbs, names); +if (err) +return err; + vi->evt = vqs[0 ]; + vi->sts = vqs[1 ]; + +return 0 ; +} + +static void virtinput_fill_evt (struct virtio_input *vi) +{ +unsigned long flags; +int i, size; + +spin_lock_irqsave (&vi->lock, flags); + 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); +spin_unlock_irqrestore (&vi->lock, flags); +} + +static int virtinput_probe (struct virtio_device *vdev) +{ +struct virtio_input *vi; +size_t size; +int abs, 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; +spin_lock_init (&vi->lock); + + err =virtinput_init_vqs (vi); +if (err) + goto err_init_vq; + + vi->idev =input_allocate_device (); +if (!vi->idev) { + err = -ENOMEM ; + goto err_input_alloc; + } +input_set_drvdata (vi->idev, vi); + + size =virtinput_cfg_select (vi,VIRTIO_INPUT_CFG_ID_NAME ,0 ); +virtio_cread_bytes (vi->vdev, offsetof(struct virtio_input_config , + u.string), + vi->name, min(size, sizeof(vi->name))); + size =virtinput_cfg_select (vi,VIRTIO_INPUT_CFG_ID_SERIAL ,0 ); +virtio_cread_bytes (vi->vdev, offsetof(struct virtio_input_config , + u.string), + vi->serial, min(size, sizeof(vi->serial))); +snprintf (vi->phys,sizeof (vi->phys), +"virtio%d/input0" , vdev->index); + vi->idev->name = vi->name; + vi->idev->phys = vi->phys; + vi->idev->uniq = vi->serial; + + size =virtinput_cfg_select (vi,VIRTIO_INPUT_CFG_ID_DEVIDS ,0 ); +if (size >=sizeof (struct virtio_input_devids )) { + virtio_cread(vi->vdev, struct virtio_input_config, + u.ids.bustype, &vi->idev->id.bustype); + virtio_cread(vi->vdev, struct virtio_input_config, + u.ids.vendor, &vi->idev->id.vendor); + virtio_cread(vi->vdev, struct virtio_input_config, + u.ids.product, &vi->idev->id.product); + virtio_cread(vi->vdev, struct virtio_input_config, + u.ids.version, &vi->idev->id.version); + }else { + vi->idev->id.bustype =BUS_VIRTUAL ; + } + +virtinput_cfg_bits (vi,VIRTIO_INPUT_CFG_PROP_BITS ,0 , + vi->idev->propbit,INPUT_PROP_CNT ); + size =virtinput_cfg_select (vi,VIRTIO_INPUT_CFG_EV_BITS ,EV_REP ); +if (size) +__set_bit (EV_REP , vi->idev->evbit); + + vi->idev->dev.parent = &vdev->dev; + vi->idev->event = virtinput_status; + +/* device -> kernel */ +virtinput_cfg_bits (vi,VIRTIO_INPUT_CFG_EV_BITS ,EV_KEY , + vi->idev->keybit,KEY_CNT ); +virtinput_cfg_bits (vi,VIRTIO_INPUT_CFG_EV_BITS ,EV_REL , + vi->idev->relbit,REL_CNT ); +virtinput_cfg_bits (vi,VIRTIO_INPUT_CFG_EV_BITS ,EV_ABS , + vi->idev->absbit,ABS_CNT ); +virtinput_cfg_bits (vi,VIRTIO_INPUT_CFG_EV_BITS ,EV_MSC , + vi->idev->mscbit,MSC_CNT ); +virtinput_cfg_bits (vi,VIRTIO_INPUT_CFG_EV_BITS ,EV_SW , + vi->idev->swbit,SW_CNT ); + +/* kernel -> device */ +virtinput_cfg_bits (vi,VIRTIO_INPUT_CFG_EV_BITS ,EV_LED , + vi->idev->ledbit,LED_CNT ); +virtinput_cfg_bits (vi,VIRTIO_INPUT_CFG_EV_BITS ,EV_SND , + vi->idev->sndbit,SND_CNT ); +
drivers/virtio/virtio_input.c line 285
Quoted 10 lines
drivers/virtio/virtio_input.c new file +10
-0+if (test_bit (EV_ABS , vi->idev->evbit)) { +for (abs =0 ; abs <ABS_CNT ; abs++) { +if (!test_bit (abs, vi->idev->absbit)) +continue ; +virtinput_cfg_abs (vi, abs); + } + } + err =input_register_device (vi->idev); +if (err) + goto err_input_register;
vi->ready is false here so you might lose status updates. Losing updates isn't too terrible, but seems easy to fix:
virtio_device_ready(vdev); vi->ready = true; err = input_register_device(vi->idev);
...
err_input_register:
vi->ready = false;
it's up to you whether to do this.
Quoted 10 lines
drivers/virtio/virtio_input.c new file +10
-0+ +virtio_device_ready (vdev); + vi->ready = true; +virtinput_fill_evt (vi); +return 0 ; + +err_input_register: +input_free_device (vi->idev); +err_input_alloc: + vdev->config->del_vqs (vdev);
drivers/virtio/virtio_input.c line 305
Quoted 10 lines
drivers/virtio/virtio_input.c new file +10
-0+err_init_vq: +kfree (vi); +return err; +} + +static void virtinput_remove (struct virtio_device *vdev) +{ +struct virtio_input *vi = vdev->priv; + + vi->ready = false;
This one needs to be done under a lock, otherwise we can't be sure no callbacks are running here.
drivers/virtio/virtio_input.c line 316
Quoted 11 lines
drivers/virtio/virtio_input.c new file +11
-0+input_unregister_device (vi->idev); + vdev->config->del_vqs (vdev); +kfree (vi); +} + +#ifdef CONFIG_PM_SLEEP +static int virtinput_freeze (struct virtio_device *vdev) +{ +struct virtio_input *vi = vdev->priv; + + vi->ready = false;
Same here I think.
Quoted 12 lines
drivers/virtio/virtio_input.c new file +12
-0+ vdev->config->del_vqs (vdev); +return 0 ; +} + +static int virtinput_restore (struct virtio_device *vdev) +{ +struct virtio_input *vi = vdev->priv; +int err; + + err =virtinput_init_vqs (vi); +if (err) +return err;
drivers/virtio/virtio_input.c line 338
Quoted 10 lines
drivers/virtio/virtio_input.c new file +10
-0+ +virtio_device_ready (vdev); + vi->ready = true; +virtinput_fill_evt (vi); + return 0; +} +#endif + +static unsigned int features[] = { +};
an emoty line won't hurt here.
Quoted 72 lines
drivers/virtio/virtio_input.c new file +25
-0+static struct virtio_device_id id_table[] = { + {VIRTIO_ID_INPUT ,VIRTIO_DEV_ANY_ID }, + {0 }, +}; + +static struct virtio_driver virtio_input_driver = { + .driver.name =KBUILD_MODNAME , + .driver.owner =THIS_MODULE , + .feature_table = features, + .feature_table_size =ARRAY_SIZE (features), + .id_table = id_table, + .probe = virtinput_probe, + .remove = virtinput_remove, +#ifdef CONFIG_PM_SLEEP + .freeze = virtinput_freeze, + .restore = virtinput_restore, +#endif +}; + +module_virtio_driver (virtio_input_driver); +MODULE_DEVICE_TABLE (virtio, id_table); + +MODULE_LICENSE ("GPL" ); +MODULE_DESCRIPTION ("Virtio input device driver" ); +MODULE_AUTHOR ("Gerd Hoffmann <[email protected]>" );include/uapi/linux/Kbuild +1
-0@@ -430,6 +430,7 @@ header-y += virtio_blk.h header-y += virtio_config.h header-y += virtio_console.h header-y += virtio_ids.h +header-y += virtio_input.h header-y += virtio_net.h header-y += virtio_pci.h header-y += virtio_ring.hinclude/uapi/linux/virtio_ids.h +1
-0@@ -39,5 +39,6 @@#define VIRTIO_ID_9P 9/* 9p virtio console */ #define VIRTIO_ID_RPROC_SERIAL 11/* virtio remoteproc serial link */ #define VIRTIO_ID_CAIF 12/* Virtio caif */ +#define VIRTIO_ID_INPUT 18/* virtio input */ #endif /* _LINUX_VIRTIO_IDS_H */ include/uapi/linux/virtio_input.h new file +18
-0@@ -0,0 +1,76 @@ +#ifndef _LINUX_VIRTIO_INPUT_H +#define _LINUX_VIRTIO_INPUT_H +/* This header is BSD licensed so anyone can use the definitions to implement +* compatible drivers/servers. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions +* are met: +* 1. Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* 2. Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in the +* documentation and/or other materials provided with the distribution. +* 3. Neither the name of IBM nor the names of its contributors +* may be used to endorse or promote products derived from this software +* without specific prior written permission. +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
include/uapi/linux/virtio_input.h line 28
Quoted 10 lines
include/uapi/linux/virtio_input.h new file +10
-0+* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL IBM OR +* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF +* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT +* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +* SUCH DAMAGE. */
You must include linux/types.h for __le types I think.
include/uapi/linux/virtio_input.h line 29
Quoted 1 line
include/uapi/linux/virtio_input.h new file +1
-0+#include <linux/virtio_ids.h>
This one is there in all other headers, so ok, though I don't know why.
include/uapi/linux/virtio_input.h line 30
Quoted 1 line
include/uapi/linux/virtio_input.h new file +1
-0+#include <linux/virtio_config.h>
I'm sure you don't need this one.
Quoted 21 lines
include/uapi/linux/virtio_input.h new file +21
-0+ +enum virtio_input_config_select { +VIRTIO_INPUT_CFG_UNSET =0x00 , +VIRTIO_INPUT_CFG_ID_NAME =0x01 , +VIRTIO_INPUT_CFG_ID_SERIAL =0x02 , +VIRTIO_INPUT_CFG_ID_DEVIDS =0x03 , +VIRTIO_INPUT_CFG_PROP_BITS =0x10 , +VIRTIO_INPUT_CFG_EV_BITS =0x11 , +VIRTIO_INPUT_CFG_ABS_INFO =0x12 , +}; + +struct virtio_input_absinfo { +__u32 min; +__u32 max; +__u32 fuzz; +__u32 flat; +__u32 res; +}; + +struct virtio_input_devids { +__u16 bustype;
include/uapi/linux/virtio_input.h line 61
Quoted 10 lines
include/uapi/linux/virtio_input.h new file +10
-0+__u16 vendor; +__u16 product; +__u16 version; +}; + +struct virtio_input_config { +__u8 select; +__u8 subsel; +__u8 size; +__u8 reserved;
Thinking more about it, might it be better to make this reserved[5] so the union is 8 byte aligned? Will be helpful if you later want a 64 bit field in the union.
Quoted 17 lines
include/uapi/linux/virtio_input.h new file +15
-0+union { +char string[128 ]; +__u8 bitmap[128 ]; +struct virtio_input_absinfo abs; +struct virtio_input_devids ids; + } u; +}; + +struct virtio_input_event { +__le16 type; +__le16 code; +__le32 value; +}; + +#endif /* _LINUX_VIRTIO_INPUT_H */ Signature · 1 line
1.8.3.1