From: "Michael S. Tsirkin" <mst@redhat.com>
To: Gerd Hoffmann <kraxel@redhat.com>
Cc: virtio-dev@lists.oasis-open.org, qemu-devel@nongnu.org,
Anthony Liguori <aliguori@amazon.com>
Subject: Re: [Qemu-devel] [PATCH qemu 6/6] virtio-input: evdev passthrough
Date: Thu, 10 Apr 2014 14:05:02 +0300 [thread overview]
Message-ID: <20140410110502.GB19546@redhat.com> (raw)
In-Reply-To: <1397120874-17166-9-git-send-email-kraxel@redhat.com>
On Thu, Apr 10, 2014 at 11:07:54AM +0200, Gerd Hoffmann wrote:
> This allows to assign host input devices to the guest:
>
> qemu -device virto-input-host-pci,evdev=/dev/input/event<nr>
>
> The guest gets exclusive access to the input device, so be careful
> with assigning the keyboard if you have only one connected to your
> machine.
>
> Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
> ---
> hw/input/Makefile.objs | 1 +
> hw/input/virtio-input-host.c | 174 +++++++++++++++++++++++++++++++++++++++
> hw/virtio/virtio-pci.c | 31 +++++++
> hw/virtio/virtio-pci.h | 10 +++
> include/hw/virtio/virtio-input.h | 13 +++
> 5 files changed, 229 insertions(+)
> create mode 100644 hw/input/virtio-input-host.c
>
> diff --git a/hw/input/Makefile.objs b/hw/input/Makefile.objs
> index 0179154..9461b37 100644
> --- a/hw/input/Makefile.objs
> +++ b/hw/input/Makefile.objs
> @@ -12,6 +12,7 @@ ifeq ($(CONFIG_LINUX),y)
> common-obj-$(CONFIG_VIRTIO) += virtio-input.o
> common-obj-$(CONFIG_VIRTIO) += virtio-input-hid.o
> common-obj-$(CONFIG_VIRTIO) += virtio-input-control.o
> +common-obj-$(CONFIG_VIRTIO) += virtio-input-host.o
> endif
>
> obj-$(CONFIG_MILKYMIST) += milkymist-softusb.o
> diff --git a/hw/input/virtio-input-host.c b/hw/input/virtio-input-host.c
> new file mode 100644
> index 0000000..663d967
> --- /dev/null
> +++ b/hw/input/virtio-input-host.c
> @@ -0,0 +1,174 @@
> +/*
> + * This work is licensed under the terms of the GNU GPL, version 2 or
> + * (at your option) any later version. See the COPYING file in the
> + * top-level directory.
> + */
> +
> +#include "qemu-common.h"
> +#include "qemu/sockets.h"
> +
> +#include "hw/qdev.h"
> +#include "hw/virtio/virtio.h"
> +#include "hw/virtio/virtio-input.h"
> +
> +#include "ui/console.h"
> +
> +#include <linux/input.h>
> +
> +/* ----------------------------------------------------------------- */
> +
> +static struct virtio_input_config virtio_input_host_config[] = {
> + { /* empty list */ },
> +};
> +
> +static void virtio_input_host_event(void *opaque)
> +{
> + VirtIOInputHost *vhost = opaque;
I'd prefer a name that does not imply
vhost infrastructure - which is an out of process QEMU backend
(kernel or another userspace process) accessing guest
memory directly.
This one is completely in-process.
> + VirtIOInput *vinput = VIRTIO_INPUT(vhost);
> + struct virtio_input_event virtio;
> + struct input_event evdev;
> + bool notify = false;
> + int rc;
> +
> + for (;;) {
You might want to limit this, requeue if there's
a storm of events.
> + rc = read(vhost->fd, &evdev, sizeof(evdev));
> + if (rc != sizeof(evdev)) {
> + break;
> + }
> +
> + virtio.type = cpu_to_le16(evdev.type);
> + virtio.code = cpu_to_le16(evdev.code);
> + virtio.value = cpu_to_le32(evdev.value);
> + virtio_input_send(vinput, &virtio);
> +
> + if (evdev.type == EV_SYN) {
> + notify = true;
> + }
> + }
> +
> + if (notify) {
> + virtio_notify(VIRTIO_DEVICE(vhost), vinput->evt);
> + }
> +}
> +
> +static void virtio_input_bits_config(VirtIOInputHost *vhost,
> + int type, int count)
> +{
> + virtio_input_config bits;
> + int rc, i, size = 0;
> +
> + memset(&bits, 0, sizeof(bits));
> + rc = ioctl(vhost->fd, EVIOCGBIT(type, count/8), bits.u.bitmap);
> + if (rc < 0) {
> + return;
> + }
> +
> + for (i = 0; i < count/8; i++) {
> + if (bits.u.bitmap[i]) {
> + size = i+1;
> + }
> + }
> + if (size == 0) {
> + return;
> + }
> +
> + bits.select = VIRTIO_INPUT_CFG_EV_BITS;
> + bits.subsel = type;
> + bits.size = size;
> + virtio_input_add_config(VIRTIO_INPUT(vhost), &bits);
> +}
> +
> +static void virtio_input_host_realize(DeviceState *dev, Error **errp)
> +{
> + VirtIOInputHost *vhost = VIRTIO_INPUT_HOST(dev);
> + VirtIOInput *vinput = VIRTIO_INPUT(dev);
> + virtio_input_config id;
> + int rc, ver;
> +
> + if (!vhost->evdev) {
> + error_setg(errp, "evdev property is required");
> + return;
> + }
> +
> + vhost->fd = open(vhost->evdev, O_RDWR);
> + if (vhost->fd < 0) {
> + error_setg_file_open(errp, errno, vhost->evdev);
> + return;
> + }
> + qemu_set_nonblock(vhost->fd);
> +
> + rc = ioctl(vhost->fd, EVIOCGVERSION, &ver);
> + if (rc < 0) {
> + error_setg(errp, "%s: is not an evdev device", vhost->evdev);
> + goto err_close;
> + }
Hmm is that all?
Don't we want to report versioning info to guests?
> +
> + rc = ioctl(vhost->fd, EVIOCGRAB, 1);
> + if (rc < 0) {
> + error_setg_errno(errp, errno, "%s: failed to get exclusive access",
> + vhost->evdev);
> + goto err_close;
> + }
> +
> + memset(&id, 0, sizeof(id));
> + ioctl(vhost->fd, EVIOCGNAME(sizeof(id.u.string)-1), id.u.string);
> + id.select = VIRTIO_INPUT_CFG_ID_NAME;
> + id.size = strlen(id.u.string);
> + virtio_input_add_config(vinput, &id);
> +
> + virtio_input_bits_config(vhost, EV_KEY, KEY_CNT);
> + virtio_input_bits_config(vhost, EV_REL, REL_CNT);
> + virtio_input_bits_config(vhost, EV_ABS, ABS_CNT);
> + virtio_input_bits_config(vhost, EV_MSC, MSC_CNT);
> + virtio_input_bits_config(vhost, EV_SW, SW_CNT);
> +
> + qemu_set_fd_handler(vhost->fd, virtio_input_host_event, NULL, vhost);
> + return;
> +
> +err_close:
> + close(vhost->fd);
> + vhost->fd = -1;
> + return;
> +}
> +
> +static void virtio_input_host_unrealize(DeviceState *dev, Error **errp)
> +{
> + VirtIOInputHost *vhost = VIRTIO_INPUT_HOST(dev);
> +
> + if (vhost->fd > 0) {
> + qemu_set_fd_handler(vhost->fd, NULL, NULL, NULL);
> + close(vhost->fd);
> + }
> +}
> +
> +static void virtio_input_host_class_init(ObjectClass *klass, void *data)
> +{
> + VirtIOInputClass *vic = VIRTIO_INPUT_CLASS(klass);
> +
> + vic->realize = virtio_input_host_realize;
> + vic->unrealize = virtio_input_host_unrealize;
> +}
> +
> +static void virtio_input_host_init(Object *obj)
> +{
> + VirtIOInput *vinput = VIRTIO_INPUT(obj);
> +
> + virtio_input_init_config(vinput, virtio_input_host_config);
> +}
> +
> +static const TypeInfo virtio_input_host_info = {
> + .name = TYPE_VIRTIO_INPUT_HOST,
> + .parent = TYPE_VIRTIO_INPUT,
> + .instance_size = sizeof(VirtIOInputHost),
> + .instance_init = virtio_input_host_init,
> + .class_init = virtio_input_host_class_init,
> +};
> +
> +/* ----------------------------------------------------------------- */
> +
> +static void virtio_register_types(void)
> +{
> + type_register_static(&virtio_input_host_info);
> +}
> +
> +type_init(virtio_register_types)
> diff --git a/hw/virtio/virtio-pci.c b/hw/virtio/virtio-pci.c
> index 9446d45..9f81fc0 100644
> --- a/hw/virtio/virtio-pci.c
> +++ b/hw/virtio/virtio-pci.c
> @@ -1547,6 +1547,14 @@ static Property virtio_input_ctrl_pci_properties[] = {
> DEFINE_PROP_END_OF_LIST(),
> };
>
> +static Property virtio_input_host_pci_properties[] = {
> + DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
> + DEFINE_VIRTIO_INPUT_PROPERTIES(VirtIOInputPCI, vdev.input),
> + DEFINE_PROP_STRING("evdev", VirtIOInputHostPCI, vdev.evdev),
> + DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
> + DEFINE_PROP_END_OF_LIST(),
> +};
> +
> static int virtio_input_pci_init(VirtIOPCIProxy *vpci_dev)
> {
> VirtIOInputPCI *vinput = VIRTIO_INPUT_PCI(vpci_dev);
> @@ -1585,6 +1593,13 @@ static void virtio_input_ctrl_pci_class_init(ObjectClass *klass, void *data)
> dc->props = virtio_input_ctrl_pci_properties;
> }
>
> +static void virtio_input_host_pci_class_init(ObjectClass *klass, void *data)
> +{
> + DeviceClass *dc = DEVICE_CLASS(klass);
> +
> + dc->props = virtio_input_host_pci_properties;
> +}
> +
> static void virtio_keyboard_initfn(Object *obj)
> {
> VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj);
> @@ -1613,6 +1628,13 @@ static void virtio_ctrl_initfn(Object *obj)
> object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
> }
>
> +static void virtio_host_initfn(Object *obj)
> +{
> + VirtIOInputHostPCI *dev = VIRTIO_INPUT_HOST_PCI(obj);
> + object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_INPUT_HOST);
> + object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
> +}
> +
> static const TypeInfo virtio_input_pci_info = {
> .name = TYPE_VIRTIO_INPUT_PCI,
> .parent = TYPE_VIRTIO_PCI,
> @@ -1658,6 +1680,14 @@ static const TypeInfo virtio_ctrl_pci_info = {
> .class_init = virtio_input_ctrl_pci_class_init,
> };
>
> +static const TypeInfo virtio_host_pci_info = {
> + .name = TYPE_VIRTIO_INPUT_HOST_PCI,
> + .parent = TYPE_VIRTIO_INPUT_PCI,
> + .instance_size = sizeof(VirtIOInputHostPCI),
> + .instance_init = virtio_host_initfn,
> + .class_init = virtio_input_host_pci_class_init,
> +};
> +
> /* virtio-pci-bus */
>
> static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
> @@ -1708,6 +1738,7 @@ static void virtio_pci_register_types(void)
> type_register_static(&virtio_mouse_pci_info);
> type_register_static(&virtio_tablet_pci_info);
> type_register_static(&virtio_ctrl_pci_info);
> + type_register_static(&virtio_host_pci_info);
> type_register_static(&virtio_pci_bus_info);
> type_register_static(&virtio_pci_info);
> #ifdef CONFIG_VIRTFS
> diff --git a/hw/virtio/virtio-pci.h b/hw/virtio/virtio-pci.h
> index 0c26e64..4c12770 100644
> --- a/hw/virtio/virtio-pci.h
> +++ b/hw/virtio/virtio-pci.h
> @@ -43,6 +43,7 @@ typedef struct VirtIORngPCI VirtIORngPCI;
> typedef struct VirtIOInputPCI VirtIOInputPCI;
> typedef struct VirtIOInputHIDPCI VirtIOInputHIDPCI;
> typedef struct VirtIOInputCtrlPCI VirtIOInputCtrlPCI;
> +typedef struct VirtIOInputHostPCI VirtIOInputHostPCI;
>
> /* virtio-pci-bus */
>
> @@ -236,6 +237,15 @@ struct VirtIOInputCtrlPCI {
> VirtIOInputCtrl vdev;
> };
>
> +#define TYPE_VIRTIO_INPUT_HOST_PCI "virtio-input-host-pci"
> +#define VIRTIO_INPUT_HOST_PCI(obj) \
> + OBJECT_CHECK(VirtIOInputHostPCI, (obj), TYPE_VIRTIO_INPUT_HOST_PCI)
> +
> +struct VirtIOInputHostPCI {
> + VirtIOPCIProxy parent_obj;
> + VirtIOInputHost vdev;
> +};
> +
> /* Virtio ABI version, if we increment this, we break the guest driver. */
> #define VIRTIO_PCI_ABI_VERSION 0
>
> diff --git a/include/hw/virtio/virtio-input.h b/include/hw/virtio/virtio-input.h
> index d753834..94ea405 100644
> --- a/include/hw/virtio/virtio-input.h
> +++ b/include/hw/virtio/virtio-input.h
> @@ -73,6 +73,12 @@ typedef struct virtio_input_event {
> #define VIRTIO_INPUT_CTRL_GET_PARENT_CLASS(obj) \
> OBJECT_GET_PARENT_CLASS(obj, TYPE_VIRTIO_INPUT_CTRL)
>
> +#define TYPE_VIRTIO_INPUT_HOST "virtio-input-host"
> +#define VIRTIO_INPUT_HOST(obj) \
> + OBJECT_CHECK(VirtIOInputHost, (obj), TYPE_VIRTIO_INPUT_HOST)
> +#define VIRTIO_INPUT_HOST_GET_PARENT_CLASS(obj) \
> + OBJECT_GET_PARENT_CLASS(obj, TYPE_VIRTIO_INPUT_HOST)
> +
> #define DEFINE_VIRTIO_INPUT_PROPERTIES(_state, _field) \
> DEFINE_PROP_STRING("serial", _state, _field.serial), \
> DEFINE_PROP_STRING("seat", _state, _field.seat)
> @@ -82,6 +88,7 @@ typedef struct VirtIOInputClass VirtIOInputClass;
> typedef struct VirtIOInputConfig VirtIOInputConfig;
> typedef struct VirtIOInputHID VirtIOInputHID;
> typedef struct VirtIOInputCtrl VirtIOInputCtrl;
> +typedef struct VirtIOInputHost VirtIOInputHost;
>
> struct virtio_input_conf {
> char *serial;
> @@ -128,6 +135,12 @@ struct VirtIOInputCtrl {
> Notifier powerdown;
> };
>
> +struct VirtIOInputHost {
> + VirtIOInput parent_obj;
> + char *evdev;
> + int fd;
> +};
> +
> void virtio_input_send(VirtIOInput *vinput, virtio_input_event *event);
> void virtio_input_init_config(VirtIOInput *vinput,
> virtio_input_config *config);
> --
> 1.8.3.1
next prev parent reply other threads:[~2014-04-10 11:05 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-04-10 9:07 [Qemu-devel] [PATCHES] add virtio input device Gerd Hoffmann
2014-04-10 9:07 ` [Qemu-devel] [PATCH spec] Add virtio input device specification Gerd Hoffmann
2014-04-10 15:36 ` Christopher Covington
2014-04-10 9:07 ` [Qemu-devel] [PATCH linux] Add virtio-input driver Gerd Hoffmann
2014-04-10 9:07 ` [Qemu-devel] [PATCH qemu 1/6] pci: add virtio input pci device id Gerd Hoffmann
2014-04-10 9:07 ` [Qemu-devel] [PATCH qemu 2/6] pci: add virtio gpu " Gerd Hoffmann
2014-04-10 9:07 ` [Qemu-devel] [PATCH qemu 3/6] virtio-input: core code & base class Gerd Hoffmann
2014-04-10 11:06 ` Michael S. Tsirkin
2014-04-10 12:22 ` Gerd Hoffmann
2014-04-10 14:56 ` Michael S. Tsirkin
2014-04-11 7:17 ` Gerd Hoffmann
2014-04-10 9:07 ` [Qemu-devel] [PATCH qemu 4/6] virtio-input: emulated devices Gerd Hoffmann
2014-04-10 10:55 ` Michael S. Tsirkin
2014-04-10 11:47 ` Gerd Hoffmann
2014-04-10 15:14 ` Michael S. Tsirkin
2014-04-11 8:01 ` Gerd Hoffmann
2014-04-10 9:07 ` [Qemu-devel] [PATCH qemu 5/6] virtio-input: control device Gerd Hoffmann
2014-04-10 11:05 ` Michael S. Tsirkin
2014-04-10 12:10 ` Gerd Hoffmann
2014-04-10 15:20 ` Michael S. Tsirkin
2014-04-11 8:25 ` Gerd Hoffmann
2014-04-10 9:07 ` [Qemu-devel] [PATCH qemu 6/6] virtio-input: evdev passthrough Gerd Hoffmann
2014-04-10 11:05 ` Michael S. Tsirkin [this message]
2014-04-10 11:57 ` Gerd Hoffmann
2014-04-10 15:16 ` Michael S. Tsirkin
2014-04-11 8:02 ` Gerd Hoffmann
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20140410110502.GB19546@redhat.com \
--to=mst@redhat.com \
--cc=aliguori@amazon.com \
--cc=kraxel@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=virtio-dev@lists.oasis-open.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).