Skip to contentSkip to conversation

add virtio input device

PATCH New Gerd Hoffmann 3 participants 26 messages Apr 10, 2014 – Apr 11, 2014

Subsystems (3)
lore
Share & tools
lore

Gerd Hoffmann

author maintainer
#0 lore raw

PATCH 1/1 add virtio input device

Hi,

Here comes a bunch of patches inplementing the virtio input device for review. One patch for the virtio specification. One patch for the linux kernel. A small patch series for qemu. Please review.

Note that the qemu patch series has dependencies on unmerged input layer patches (waiting for the tree being opened up for 2.1), so if you wanna play with please fetch everything here:
http://www.kraxel.org/cgit/qemu/log/?h=rebase/input-wip

cheers,
Gerd

author maintainer
#1 lore raw

PATCH 1/6 pci: add virtio input pci device id

Using 0x1012 because virtio id is 18 (0x12).

---
 docs/specs/pci-ids.txt | 1 +
 include/hw/pci/pci.h   | 1 +
 2 files changed, 2 insertions(+)
docs/specs/pci-ids.txt +1 -0
index 3c65e1a..3b0a448 100644
@@ -22,6 +22,7 @@ maintained as part of the virtio specification.
 1af4:1004  SCSI host bus adapter device
 1af4:1005  entropy generator device
 1af4:1009  9p filesystem device
+1af4:1012  input device
 
 1af4:10f0  Available for experimental usage without registration.  Must get
    to      official ID when the code leaves the test lab (i.e. when seeking
include/hw/pci/pci.h +1 -0
index 693dd6b..6539cbd 100644
@@ -80,6 +80,7 @@
 #define PCI_DEVICE_ID_VIRTIO_SCSI        0x1004
 #define PCI_DEVICE_ID_VIRTIO_RNG         0x1005
 #define PCI_DEVICE_ID_VIRTIO_9P          0x1009
+#define PCI_DEVICE_ID_VIRTIO_INPUT       0x1012
 
 #define PCI_VENDOR_ID_REDHAT             0x1b36
 #define PCI_DEVICE_ID_REDHAT_BRIDGE      0x0001
Signature · 1 line
1.8.3.1
author maintainer
#2 lore raw

PATCH 1/1 Add virtio-input driver.

In reply to Gerd Hoffmann

virtio-input is basically evdev-events-over-virtio, so this driver isn't much more than forwarding incoming events to the linux input layer.

---
 drivers/virtio/Kconfig            |  10 ++
 drivers/virtio/Makefile           |   1 +
 drivers/virtio/virtio_input.c     | 329 ++++++++++++++++++++++++++++++++++++++
 include/uapi/linux/virtio_ids.h   |   1 +
 include/uapi/linux/virtio_input.h |  66 ++++++++
 5 files changed, 407 insertions(+)
 create mode 100644 drivers/virtio/virtio_input.c
 create mode 100644 include/uapi/linux/virtio_input.h
drivers/virtio/Kconfig +10 -0
index c6683f2..05e2fda 100644
@@ -31,6 +31,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
index 9076635..45aeaac 100644
@@ -2,3 +2,4 @@ obj-$(CONFIG_VIRTIO) += virtio.o virtio_ring.o
 obj-$(CONFIG_VIRTIO_MMIO) += virtio_mmio.o
 obj-$(CONFIG_VIRTIO_PCI) += virtio_pci.o
 obj-$(CONFIG_VIRTIO_BALLOON) += virtio_balloon.o
+obj-$(CONFIG_VIRTIO_INPUT) += virtio_input.o
drivers/virtio/virtio_input.c new file +329 -0

329 lines not on this page · raw message

include/uapi/linux/virtio_ids.h +1 -0
index 284fc3a..5f60aa4 100644
@@ -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 +66 -0

66 lines not on this page · raw message

Signature · 1 line
1.8.3.1
author maintainer
#3 lore raw

PATCH 2/6 pci: add virtio gpu pci device id

In reply to Gerd Hoffmann

Using 0x1010 because virtio id is 16 (0x10).

---
 docs/specs/pci-ids.txt | 1 +
 include/hw/pci/pci.h   | 1 +
 2 files changed, 2 insertions(+)
docs/specs/pci-ids.txt +1 -0
index 3b0a448..69a2de9 100644
@@ -22,6 +22,7 @@ maintained as part of the virtio specification.
 1af4:1004  SCSI host bus adapter device
 1af4:1005  entropy generator device
 1af4:1009  9p filesystem device
+1af4:1010  gpu device
 1af4:1012  input device
 
 1af4:10f0  Available for experimental usage without registration.  Must get
include/hw/pci/pci.h +1 -0
index 6539cbd..6ebc5fa 100644
@@ -80,6 +80,7 @@
 #define PCI_DEVICE_ID_VIRTIO_SCSI        0x1004
 #define PCI_DEVICE_ID_VIRTIO_RNG         0x1005
 #define PCI_DEVICE_ID_VIRTIO_9P          0x1009
+#define PCI_DEVICE_ID_VIRTIO_GPU         0x1010
 #define PCI_DEVICE_ID_VIRTIO_INPUT       0x1012
 
 #define PCI_VENDOR_ID_REDHAT             0x1b36
Signature · 1 line
1.8.3.1
author maintainer
#4 lore raw

PATCH 3/6 virtio-input: core code & base class

In reply to Gerd Hoffmann

This patch adds virtio-input support to qemu. It brings a abstract base class providing core support, other classes can build on it to actually implement input devices.

virtio-input basically sends linux input layer events (evdev) over virtio.

---
 hw/input/Makefile.objs           |   4 +
 hw/input/virtio-input.c          | 253 +++++++++++++++++++++++++++++++++++++++
 hw/virtio/virtio-pci.c           |  36 ++++++
 hw/virtio/virtio-pci.h           |  14 +++
 include/hw/virtio/virtio-input.h | 105 ++++++++++++++++
 include/hw/virtio/virtio.h       |   1 +
 6 files changed, 413 insertions(+)
 create mode 100644 hw/input/virtio-input.c
 create mode 100644 include/hw/virtio/virtio-input.h
hw/input/Makefile.objs +4 -0
index e8c80b9..ee8bba9 100644
@@ -8,6 +8,10 @@ common-obj-$(CONFIG_STELLARIS_INPUT) += stellaris_input.o
 common-obj-$(CONFIG_TSC2005) += tsc2005.o
 common-obj-$(CONFIG_VMMOUSE) += vmmouse.o
 
+ifeq ($(CONFIG_LINUX),y)
+common-obj-$(CONFIG_VIRTIO) += virtio-input.o
+endif
+
 obj-$(CONFIG_MILKYMIST) += milkymist-softusb.o
 obj-$(CONFIG_PXA2XX) += pxa2xx_keypad.o
 obj-$(CONFIG_TSC210X) += tsc210x.o
hw/input/virtio-input.c new file +253 -0

253 lines not on this page · raw message

hw/virtio/virtio-pci.c +36 -0
index ce97514..5518192 100644
@@ -23,6 +23,7 @@
 #include "hw/virtio/virtio-serial.h"
 #include "hw/virtio/virtio-scsi.h"
 #include "hw/virtio/virtio-balloon.h"
+#include "hw/virtio/virtio-input.h"
 #include "hw/pci/pci.h"
 #include "qemu/error-report.h"
 #include "hw/pci/msi.h"
@@ -1531,6 +1532,40 @@ static const TypeInfo virtio_rng_pci_info = {
     .class_init    = virtio_rng_pci_class_init,
 };
 
+/* virtio-input-pci */
+
+static int virtio_input_pci_init(VirtIOPCIProxy *vpci_dev)
+{
+    VirtIOInputPCI *vinput = VIRTIO_INPUT_PCI(vpci_dev);
+    DeviceState *vdev = DEVICE(&vinput->vdev);
+
+    qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
+    return qdev_init(vdev);
+}
+
+static void virtio_input_pci_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
+    PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
+
+    k->init = virtio_input_pci_init;
+    set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
+
+    pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
+    pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_INPUT;
+    pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
+    pcidev_k->class_id = PCI_CLASS_OTHERS;
+}
+
+static const TypeInfo virtio_input_pci_info = {
+    .name          = TYPE_VIRTIO_INPUT_PCI,
+    .parent        = TYPE_VIRTIO_PCI,
+    .instance_size = sizeof(VirtIOInputPCI),
+    .class_init    = virtio_input_pci_class_init,
+    .abstract      = true,
+};
+
 /* virtio-pci-bus */
 
 static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
@@ -1575,6 +1610,7 @@ static const TypeInfo virtio_pci_bus_info = {
 static void virtio_pci_register_types(void)
 {
     type_register_static(&virtio_rng_pci_info);
+    type_register_static(&virtio_input_pci_info);
     type_register_static(&virtio_pci_bus_info);
     type_register_static(&virtio_pci_info);
 #ifdef CONFIG_VIRTFS
hw/virtio/virtio-pci.h +14 -0
index dc332ae..f1e75ad 100644
@@ -24,6 +24,7 @@
 #include "hw/virtio/virtio-balloon.h"
 #include "hw/virtio/virtio-bus.h"
 #include "hw/virtio/virtio-9p.h"
+#include "hw/virtio/virtio-input.h"
 #ifdef CONFIG_VIRTFS
 #include "hw/9pfs/virtio-9p.h"
 #endif
@@ -39,6 +40,7 @@ typedef struct VirtIOSerialPCI VirtIOSerialPCI;
 typedef struct VirtIONetPCI VirtIONetPCI;
 typedef struct VHostSCSIPCI VHostSCSIPCI;
 typedef struct VirtIORngPCI VirtIORngPCI;
+typedef struct VirtIOInputPCI VirtIOInputPCI;
 
 /* virtio-pci-bus */
 
@@ -199,6 +201,18 @@ struct VirtIORngPCI {
     VirtIORNG vdev;
 };
 
+/*
+ * virtio-input-pci: This extends VirtioPCIProxy.
+ */
+#define TYPE_VIRTIO_INPUT_PCI "virtio-input-pci"
+#define VIRTIO_INPUT_PCI(obj) \
+        OBJECT_CHECK(VirtIOInputPCI, (obj), TYPE_VIRTIO_INPUT_PCI)
+
+struct VirtIOInputPCI {
+    VirtIOPCIProxy parent_obj;
+    VirtIOInput vdev;
+};
+
 /* Virtio ABI version, if we increment this, we break the guest driver. */
 #define VIRTIO_PCI_ABI_VERSION          0
 
include/hw/virtio/virtio-input.h new file +105 -0

105 lines not on this page · raw message

include/hw/virtio/virtio.h +1 -0
index 3e54e90..93d1607 100644
@@ -222,6 +222,7 @@ VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf,
                               struct virtio_net_conf *net,
                               uint32_t host_features);
 typedef struct virtio_serial_conf virtio_serial_conf;
+typedef struct virtio_input_conf virtio_input_conf;
 typedef struct VirtIOSCSIConf VirtIOSCSIConf;
 typedef struct VirtIORNGConf VirtIORNGConf;
 
Signature · 1 line
1.8.3.1
#5 lore raw

Re: [Qemu-devel] [PATCH qemu 3/6] virtio-input: core code & base class

Quoted 56 lines · Gerd Hoffmann

This patch adds virtio-input support to qemu. It brings a abstract base class providing core support, other classes can build on it to actually implement input devices.

virtio-input basically sends linux input layer events (evdev) over virtio.

---
 hw/input/Makefile.objs           |   4 +
 hw/input/virtio-input.c          | 253 +++++++++++++++++++++++++++++++++++++++
 hw/virtio/virtio-pci.c           |  36 ++++++
 hw/virtio/virtio-pci.h           |  14 +++
 include/hw/virtio/virtio-input.h | 105 ++++++++++++++++
 include/hw/virtio/virtio.h       |   1 +
 6 files changed, 413 insertions(+)
 create mode 100644 hw/input/virtio-input.c
 create mode 100644 include/hw/virtio/virtio-input.h
hw/input/Makefile.objs +4 -0
index e8c80b9..ee8bba9 100644
@@ -8,6 +8,10 @@ common-obj-$(CONFIG_STELLARIS_INPUT) += stellaris_input.o
 common-obj-$(CONFIG_TSC2005) += tsc2005.o
 common-obj-$(CONFIG_VMMOUSE) += vmmouse.o
 
+ifeq ($(CONFIG_LINUX),y)
+common-obj-$(CONFIG_VIRTIO) += virtio-input.o
+endif
+
 obj-$(CONFIG_MILKYMIST) += milkymist-softusb.o
 obj-$(CONFIG_PXA2XX) += pxa2xx_keypad.o
 obj-$(CONFIG_TSC210X) += tsc210x.o
hw/input/virtio-input.c new file +16 -0
new file mode 100644
index 0000000..35f0cfc
@@ -0,0 +1,253 @@
+/*
+ * 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/iov.h"
+
+#include "hw/qdev.h"
+#include "hw/virtio/virtio.h"
+#include "hw/virtio/virtio-input.h"
+
+#include "ui/console.h"
+
+#include <linux/input.h>
+

hw/input/virtio-input.c line 26

Quoted 10 lines
hw/input/virtio-input.c new file +10 -0
+/* ----------------------------------------------------------------- */
+
+void virtio_input_send(VirtIOInput *vinput, virtio_input_event *event)
+{
+    VirtQueueElement elem;
+    int len;
+
+    if (!virtqueue_pop(vinput->evt, &elem)) {
+        fprintf(stderr, "%s: virtqueue empty, dropping event\n", __func__);
+        return;

Looks scary.

Quoted 452 lines
hw/input/virtio-input.c new file +227 -0
+    }
+    len = iov_from_buf(elem.in_sg, elem.in_num,
+                       0, event, sizeof(*event));
+    virtqueue_push(vinput->evt, &elem, len);
+}
+
+static void virtio_input_handle_evt(VirtIODevice *vdev, VirtQueue *vq)
+{
+    /* nothing */
+}
+
+static void virtio_input_handle_sts(VirtIODevice *vdev, VirtQueue *vq)
+{
+    VirtIOInputClass *vic = VIRTIO_INPUT_GET_CLASS(vdev);
+    VirtIOInput *vinput = VIRTIO_INPUT(vdev);
+    virtio_input_event event;
+    VirtQueueElement elem;
+    int len;
+
+    while (virtqueue_pop(vinput->sts, &elem)) {
+        memset(&event, 0, sizeof(event));
+        len = iov_to_buf(elem.out_sg, elem.out_num,
+                         0, &event, sizeof(event));
+        if (vic->handle_status) {
+            vic->handle_status(vinput, &event);
+        }
+        virtqueue_push(vinput->sts, &elem, len);
+    }
+    virtio_notify(vdev, vinput->sts);
+}
+
+static virtio_input_config *virtio_input_find_config(VirtIOInput *vinput,
+                                                     uint8_t select,
+                                                     uint8_t subsel)
+{
+    VirtIOInputConfig *cfg;
+
+    QTAILQ_FOREACH(cfg, &vinput->cfg_list, node) {
+        if (select == cfg->config.select &&
+            subsel == cfg->config.subsel) {
+            return &cfg->config;
+        }
+    }
+    return NULL;
+}
+
+void virtio_input_add_config(VirtIOInput *vinput,
+                             virtio_input_config *config)
+{
+    VirtIOInputConfig *cfg;
+
+    if (virtio_input_find_config(vinput, config->select, config->subsel)) {
+        /* should not happen */
+        fprintf(stderr, "%s: duplicate config: %d/%d\n",
+                __func__, config->select, config->subsel);
+        abort();
+    }
+
+    cfg = g_new0(VirtIOInputConfig, 1);
+    cfg->config = *config;
+    QTAILQ_INSERT_TAIL(&vinput->cfg_list, cfg, node);
+}
+
+void virtio_input_init_config(VirtIOInput *vinput,
+                              virtio_input_config *config)
+{
+    int i = 0;
+
+    QTAILQ_INIT(&vinput->cfg_list);
+    while (config[i].select) {
+        virtio_input_add_config(vinput, config + i);
+        i++;
+    }
+}
+
+void virtio_input_idstr_config(VirtIOInput *vinput,
+                               uint8_t select, const char *string)
+{
+    virtio_input_config id;
+
+    if (!string) {
+        return;
+    }
+    memset(&id, 0, sizeof(id));
+    id.select = select;
+    id.size = snprintf(id.u.string, sizeof(id.u.string), "%s", string);
+    virtio_input_add_config(vinput, &id);
+}
+
+static void virtio_input_get_config(VirtIODevice *vdev, uint8_t *config_data)
+{
+    VirtIOInput *vinput = VIRTIO_INPUT(vdev);
+    virtio_input_config *config;
+
+    config = virtio_input_find_config(vinput, vinput->cfg_select,
+                                      vinput->cfg_subsel);
+    if (config) {
+        memcpy(config_data, config, vinput->cfg_size);
+    } else {
+        memset(config_data, 0, vinput->cfg_size);
+    }
+}
+
+static void virtio_input_set_config(VirtIODevice *vdev,
+                                    const uint8_t *config_data)
+{
+    VirtIOInput *vinput = VIRTIO_INPUT(vdev);
+    virtio_input_config *config = (virtio_input_config *)config_data;
+
+    vinput->cfg_select = config->select;
+    vinput->cfg_subsel = config->subsel;
+    virtio_notify_config(vdev);
+}
+
+static uint32_t virtio_input_get_features(VirtIODevice *vdev, uint32_t f)
+{
+    return f;
+}
+
+static void virtio_input_set_status(VirtIODevice *vdev, uint8_t val)
+{
+    VirtIOInputClass *vic = VIRTIO_INPUT_GET_CLASS(vdev);
+    VirtIOInput *vinput = VIRTIO_INPUT(vdev);
+
+    if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
+        if (!vinput->active) {
+            vinput->active = true;
+            if (vic->change_active) {
+                vic->change_active(vinput);
+            }
+        }
+    }
+}
+
+static void virtio_input_reset(VirtIODevice *vdev)
+{
+    VirtIOInputClass *vic = VIRTIO_INPUT_GET_CLASS(vdev);
+    VirtIOInput *vinput = VIRTIO_INPUT(vdev);
+
+    if (vinput->active) {
+        vinput->active = false;
+        if (vic->change_active) {
+            vic->change_active(vinput);
+        }
+    }
+}
+
+static void virtio_input_device_realize(DeviceState *dev, Error **errp)
+{
+    VirtIOInputClass *vic = VIRTIO_INPUT_GET_CLASS(dev);
+    VirtIODevice *vdev = VIRTIO_DEVICE(dev);
+    VirtIOInput *vinput = VIRTIO_INPUT(dev);
+    VirtIOInputConfig *cfg;
+
+
+    if (vic->unrealize) {
+        vic->realize(dev, errp);
+        if (error_is_set(errp)) {
+            return;
+        }
+    }
+
+    virtio_input_idstr_config(vinput, VIRTIO_INPUT_CFG_ID_SERIAL,
+                              vinput->input.serial);
+    virtio_input_idstr_config(vinput, VIRTIO_INPUT_CFG_ID_SEAT,
+                              vinput->input.seat);
+
+    QTAILQ_FOREACH(cfg, &vinput->cfg_list, node) {
+        if (vinput->cfg_size < cfg->config.size) {
+            vinput->cfg_size = cfg->config.size;
+        }
+    }
+    vinput->cfg_size += 4;
+    assert(vinput->cfg_size <= sizeof(virtio_input_config));
+
+    virtio_init(vdev, "virtio-input", VIRTIO_ID_INPUT,
+                vinput->cfg_size);
+    vinput->evt = virtio_add_queue(vdev, 64, virtio_input_handle_evt);
+    vinput->sts = virtio_add_queue(vdev, 64, virtio_input_handle_sts);
+}
+
+static void virtio_input_device_unrealize(DeviceState *dev, Error **errp)
+{
+    VirtIOInputClass *vic = VIRTIO_INPUT_GET_CLASS(dev);
+    VirtIODevice *vdev = VIRTIO_DEVICE(dev);
+
+    if (vic->unrealize) {
+        vic->unrealize(dev, errp);
+        if (error_is_set(errp)) {
+            return;
+        }
+    }
+    virtio_cleanup(vdev);
+}
+
+static void virtio_input_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
+
+    set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
+    vdc->realize      = virtio_input_device_realize;
+    vdc->unrealize    = virtio_input_device_unrealize;
+    vdc->get_config   = virtio_input_get_config;
+    vdc->set_config   = virtio_input_set_config;
+    vdc->get_features = virtio_input_get_features;
+    vdc->set_status   = virtio_input_set_status;
+    vdc->reset        = virtio_input_reset;
+}
+
+static const TypeInfo virtio_input_info = {
+    .name          = TYPE_VIRTIO_INPUT,
+    .parent        = TYPE_VIRTIO_DEVICE,
+    .instance_size = sizeof(VirtIOInput),
+    .class_size    = sizeof(VirtIOInputClass),
+    .class_init    = virtio_input_class_init,
+    .abstract      = true,
+};
+
+/* ----------------------------------------------------------------- */
+
+static void virtio_register_types(void)
+{
+    type_register_static(&virtio_input_info);
+}
+
+type_init(virtio_register_types)
hw/virtio/virtio-pci.c +36 -0
index ce97514..5518192 100644
@@ -23,6 +23,7 @@
 #include "hw/virtio/virtio-serial.h"
 #include "hw/virtio/virtio-scsi.h"
 #include "hw/virtio/virtio-balloon.h"
+#include "hw/virtio/virtio-input.h"
 #include "hw/pci/pci.h"
 #include "qemu/error-report.h"
 #include "hw/pci/msi.h"
@@ -1531,6 +1532,40 @@ static const TypeInfo virtio_rng_pci_info = {
     .class_init    = virtio_rng_pci_class_init,
 };
 
+/* virtio-input-pci */
+
+static int virtio_input_pci_init(VirtIOPCIProxy *vpci_dev)
+{
+    VirtIOInputPCI *vinput = VIRTIO_INPUT_PCI(vpci_dev);
+    DeviceState *vdev = DEVICE(&vinput->vdev);
+
+    qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
+    return qdev_init(vdev);
+}
+
+static void virtio_input_pci_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
+    PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
+
+    k->init = virtio_input_pci_init;
+    set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
+
+    pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
+    pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_INPUT;
+    pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
+    pcidev_k->class_id = PCI_CLASS_OTHERS;
+}
+
+static const TypeInfo virtio_input_pci_info = {
+    .name          = TYPE_VIRTIO_INPUT_PCI,
+    .parent        = TYPE_VIRTIO_PCI,
+    .instance_size = sizeof(VirtIOInputPCI),
+    .class_init    = virtio_input_pci_class_init,
+    .abstract      = true,
+};
+
 /* virtio-pci-bus */
 
 static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
@@ -1575,6 +1610,7 @@ static const TypeInfo virtio_pci_bus_info = {
 static void virtio_pci_register_types(void)
 {
     type_register_static(&virtio_rng_pci_info);
+    type_register_static(&virtio_input_pci_info);
     type_register_static(&virtio_pci_bus_info);
     type_register_static(&virtio_pci_info);
 #ifdef CONFIG_VIRTFS
hw/virtio/virtio-pci.h +14 -0
index dc332ae..f1e75ad 100644
@@ -24,6 +24,7 @@
 #include "hw/virtio/virtio-balloon.h"
 #include "hw/virtio/virtio-bus.h"
 #include "hw/virtio/virtio-9p.h"
+#include "hw/virtio/virtio-input.h"
 #ifdef CONFIG_VIRTFS
 #include "hw/9pfs/virtio-9p.h"
 #endif
@@ -39,6 +40,7 @@ typedef struct VirtIOSerialPCI VirtIOSerialPCI;
 typedef struct VirtIONetPCI VirtIONetPCI;
 typedef struct VHostSCSIPCI VHostSCSIPCI;
 typedef struct VirtIORngPCI VirtIORngPCI;
+typedef struct VirtIOInputPCI VirtIOInputPCI;
 
 /* virtio-pci-bus */
 
@@ -199,6 +201,18 @@ struct VirtIORngPCI {
     VirtIORNG vdev;
 };
 
+/*
+ * virtio-input-pci: This extends VirtioPCIProxy.
+ */
+#define TYPE_VIRTIO_INPUT_PCI "virtio-input-pci"
+#define VIRTIO_INPUT_PCI(obj) \
+        OBJECT_CHECK(VirtIOInputPCI, (obj), TYPE_VIRTIO_INPUT_PCI)
+
+struct VirtIOInputPCI {
+    VirtIOPCIProxy parent_obj;
+    VirtIOInput vdev;
+};
+
 /* Virtio ABI version, if we increment this, we break the guest driver. */
 #define VIRTIO_PCI_ABI_VERSION          0
 
include/hw/virtio/virtio-input.h new file +105 -0
new file mode 100644
index 0000000..5d37a70
@@ -0,0 +1,105 @@
+#ifndef _QEMU_VIRTIO_INPUT_H
+#define _QEMU_VIRTIO_INPUT_H
+
+#include "ui/input.h"
+
+/* ----------------------------------------------------------------- */
+/* virtio input protocol                                             */
+
+/* The Virtio ID for the virtio input device */
+#define VIRTIO_ID_INPUT 18
+
+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_SEAT    = 0x03,
+    VIRTIO_INPUT_CFG_PROP_BITS  = 0x10,
+    VIRTIO_INPUT_CFG_EV_BITS    = 0x11,
+    VIRTIO_INPUT_CFG_ABS_INFO   = 0x12,
+};
+
+typedef struct virtio_input_absinfo {
+    uint32_t       min;
+    uint32_t       max;
+    uint32_t       fuzz;
+    uint32_t       flat;
+} virtio_input_absinfo;
+
+typedef struct virtio_input_config {
+    uint8_t        select;
+    uint8_t        subsel;
+    uint8_t        size;
+    uint8_t        reserved;
+    union {
+        char       string[128];
+        uint8_t    bitmap[128];
+        virtio_input_absinfo abs;
+    } u;
+} virtio_input_config;
+
+typedef struct virtio_input_event {
+    uint16_t       type;
+    uint16_t       code;
+    int32_t        value;
+} virtio_input_event;
+
+/* ----------------------------------------------------------------- */
+/* qemu internals                                                    */
+
+#define TYPE_VIRTIO_INPUT "virtio-input-device"
+#define VIRTIO_INPUT(obj) \
+        OBJECT_CHECK(VirtIOInput, (obj), TYPE_VIRTIO_INPUT)
+#define VIRTIO_INPUT_GET_PARENT_CLASS(obj) \
+        OBJECT_GET_PARENT_CLASS(obj, TYPE_VIRTIO_INPUT)
+#define VIRTIO_INPUT_GET_CLASS(obj) \
+        OBJECT_GET_CLASS(VirtIOInputClass, obj, TYPE_VIRTIO_INPUT)
+#define VIRTIO_INPUT_CLASS(klass) \
+        OBJECT_CLASS_CHECK(VirtIOInputClass, klass, TYPE_VIRTIO_INPUT)
+
+typedef struct VirtIOInput VirtIOInput;
+typedef struct VirtIOInputClass VirtIOInputClass;
+typedef struct VirtIOInputConfig VirtIOInputConfig;
+
+struct virtio_input_conf {
+    char *serial;
+    char *seat;
+};
+
+struct VirtIOInputConfig {
+    virtio_input_config               config;
+    QTAILQ_ENTRY(VirtIOInputConfig)   node;
+};
+
+struct VirtIOInput {
+    VirtIODevice                      parent_obj;
+    uint8_t                           cfg_select;
+    uint8_t                           cfg_subsel;
+    uint32_t                          cfg_size;
+    QTAILQ_HEAD(, VirtIOInputConfig)  cfg_list;
+    VirtQueue                         *evt, *sts;
+    virtio_input_conf                 input;
+
+    bool                              active;
+};
+
+struct VirtIOInputClass {
+    /*< private >*/
+    VirtioDeviceClass parent;
+    /*< public >*/
+
+    DeviceRealize realize;
+    DeviceUnrealize unrealize;
+    void (*change_active)(VirtIOInput *vinput);
+    void (*handle_status)(VirtIOInput *vinput, virtio_input_event *event);
+};
+
+void virtio_input_send(VirtIOInput *vinput, virtio_input_event *event);
+void virtio_input_init_config(VirtIOInput *vinput,
+                              virtio_input_config *config);
+void virtio_input_add_config(VirtIOInput *vinput,
+                             virtio_input_config *config);
+void virtio_input_idstr_config(VirtIOInput *vinput,
+                               uint8_t select, const char *string);
+
+#endif /* _QEMU_VIRTIO_INPUT_H */
include/hw/virtio/virtio.h +1 -0
index 3e54e90..93d1607 100644
@@ -222,6 +222,7 @@ VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf,
                               struct virtio_net_conf *net,
                               uint32_t host_features);
 typedef struct virtio_serial_conf virtio_serial_conf;
+typedef struct virtio_input_conf virtio_input_conf;
 typedef struct VirtIOSCSIConf VirtIOSCSIConf;
 typedef struct VirtIORNGConf VirtIORNGConf;
 
Signature · 1 line
1.8.3.1
author maintainer
#6 lore raw

Re: [Qemu-devel] [PATCH qemu 3/6] virtio-input: core code & base class

Quoted 9 lines · Michael S. Tsirkin
Quoted 1 line

+void virtio_input_send(VirtIOInput *vinput, virtio_input_event

*event)

hw/input/virtio-input.c

Quoted 6 lines
hw/input/virtio-input.c new file +6 -0
+{
+    VirtQueueElement elem;
+    int len;
+
+    if (!virtqueue_pop(vinput->evt, &elem)) {
+        fprintf(stderr, "%s: virtqueue empty, dropping event\n",

__func__);

Quoted 4 lines
Quoted 1 line

+ return;

Looks scary.

It's not different from other input devices. No buffer space -> drop event. What else do you think should happen? We could signal "you lost events" to the guest, but I suspect that buys us nothing. Other input devices don't have that capability, so guests are likely not prepared to handle the situation. Also, there isn't much they can actually do about it.

cheers,
Gerd

#7 lore raw

Re: [Qemu-devel] [PATCH qemu 3/6] virtio-input: core code & base class

Quoted 12 lines · Gerd Hoffmann
Quoted 11 lines · Michael S. Tsirkin
Quoted 1 line

+void virtio_input_send(VirtIOInput *vinput, virtio_input_event

*event)

hw/input/virtio-input.c

Quoted 6 lines
hw/input/virtio-input.c new file +6 -0
+{
+    VirtQueueElement elem;
+    int len;
+
+    if (!virtqueue_pop(vinput->evt, &elem)) {
+        fprintf(stderr, "%s: virtqueue empty, dropping event\n",

__func__);

Quoted 1 line

+ return;

Quoted 8 lines
Quoted 2 lines

Looks scary.

It's not different from other input devices. No buffer space -> drop event. What else do you think should happen? We could signal "you lost events" to the guest, but I suspect that buys us nothing. Other input devices don't have that capability, so guests are likely not prepared to handle the situation.

For assigned device input events, how about we don't read events off the input device file if there's nowhere to put them?

For things like sync that qemu generates, I suspect it's a good idea to buffer them in QEMU otherwise guest will get out of sync, right?

I'm also pretty sure whoever's running the hypervisor does not want to see the fprintf.

Quoted 5 lines

Also, there isn't much they can actually do about
it.

cheers,
Gerd

Signature · 1 line
MST
author maintainer
#8 lore raw

Re: [Qemu-devel] [PATCH qemu 3/6] virtio-input: core code & base class

Hi,

Quoted 8 lines
Quoted 5 lines

It's not different from other input devices. No buffer space -> drop event. What else do you think should happen? We could signal "you lost events" to the guest, but I suspect that buys us nothing. Other input devices don't have that capability, so guests are likely not prepared to handle the situation.

For assigned device input events, how about we don't read events off the input device file if there's nowhere to put them?

That'll just offload the problem to the kernel.

Quoted 2 lines

For things like sync that qemu generates, I suspect it's a good idea to buffer them in QEMU otherwise guest will get out of sync, right?

Grouping things makes sense indeed. Mouse movements for example are reported as three events: one for the x axis, one for the y axis, and the sync. We should report either all three or none of them to the guest to avoid confusion. I'll think about how to do that best.

Quoted 2 lines

I'm also pretty sure whoever's running the hypervisor does not want to see the fprintf.

I'll drop it. Or maybe better make it a tracepoint.

cheers,
Gerd

author maintainer
#9 lore raw

PATCH 4/6 virtio-input: emulated devices

In reply to Gerd Hoffmann

This patch adds the virtio-input-hid base class and virtio-{keyboard,mouse,tablet} subclasses building on the base class. They are hooked up to the qemu input core and deliver input events to the guest like all other hid devices (ps/2 kbd, usb tablet, ...).

Using them is as simple as adding "-device virtio-tablet-pci" to your command line. If you want add multiple devices but don't want waste a pci slot for each you can compose a multifunction device this way:

qemu -device virtio-keyboard-pci,addr=0d.0,multifunction=on \
-device virtio-tablet-pci,addr=0d.1,multifunction=on

---
 hw/input/Makefile.objs           |   1 +
 hw/input/virtio-input-hid.c      | 486 +++++++++++++++++++++++++++++++++++++++
 hw/virtio/virtio-pci.c           |  68 ++++++
 hw/virtio/virtio-pci.h           |  13 ++
 include/hw/virtio/virtio-input.h |  22 ++
 5 files changed, 590 insertions(+)
 create mode 100644 hw/input/virtio-input-hid.c
hw/input/Makefile.objs +1 -0
index ee8bba9..0dae710 100644
@@ -10,6 +10,7 @@ common-obj-$(CONFIG_VMMOUSE) += vmmouse.o
 
 ifeq ($(CONFIG_LINUX),y)
 common-obj-$(CONFIG_VIRTIO) += virtio-input.o
+common-obj-$(CONFIG_VIRTIO) += virtio-input-hid.o
 endif
 
 obj-$(CONFIG_MILKYMIST) += milkymist-softusb.o
hw/input/virtio-input-hid.c new file +486 -0

486 lines not on this page · raw message

hw/virtio/virtio-pci.c +68 -0

92 lines not on this page · raw message

hw/virtio/virtio-pci.h +13 -0
index f1e75ad..f615ae6 100644
@@ -41,6 +41,7 @@ typedef struct VirtIONetPCI VirtIONetPCI;
 typedef struct VHostSCSIPCI VHostSCSIPCI;
 typedef struct VirtIORngPCI VirtIORngPCI;
 typedef struct VirtIOInputPCI VirtIOInputPCI;
+typedef struct VirtIOInputHIDPCI VirtIOInputHIDPCI;
 
 /* virtio-pci-bus */
 
@@ -213,6 +214,18 @@ struct VirtIOInputPCI {
     VirtIOInput vdev;
 };
 
+#define TYPE_VIRTIO_INPUT_HID_PCI "virtio-input-hid-pci"
+#define TYPE_VIRTIO_KEYBOARD_PCI  "virtio-keyboard-pci"
+#define TYPE_VIRTIO_MOUSE_PCI     "virtio-mouse-pci"
+#define TYPE_VIRTIO_TABLET_PCI    "virtio-tablet-pci"
+#define VIRTIO_INPUT_HID_PCI(obj) \
+        OBJECT_CHECK(VirtIOInputHIDPCI, (obj), TYPE_VIRTIO_INPUT_HID_PCI)
+
+struct VirtIOInputHIDPCI {
+    VirtIOPCIProxy parent_obj;
+    VirtIOInputHID vdev;
+};
+
 /* Virtio ABI version, if we increment this, we break the guest driver. */
 #define VIRTIO_PCI_ABI_VERSION          0
 
include/hw/virtio/virtio-input.h +22 -0
index 5d37a70..4b819c3 100644
@@ -57,9 +57,24 @@ typedef struct virtio_input_event {
 #define VIRTIO_INPUT_CLASS(klass) \
         OBJECT_CLASS_CHECK(VirtIOInputClass, klass, TYPE_VIRTIO_INPUT)
 
+#define TYPE_VIRTIO_INPUT_HID "virtio-input-hid"
+#define TYPE_VIRTIO_KEYBOARD  "virtio-keyboard"
+#define TYPE_VIRTIO_MOUSE     "virtio-mouse"
+#define TYPE_VIRTIO_TABLET    "virtio-tablet"
+
+#define VIRTIO_INPUT_HID(obj) \
+        OBJECT_CHECK(VirtIOInputHID, (obj), TYPE_VIRTIO_INPUT_HID)
+#define VIRTIO_INPUT_HID_GET_PARENT_CLASS(obj) \
+        OBJECT_GET_PARENT_CLASS(obj, TYPE_VIRTIO_INPUT_HID)
+
+#define DEFINE_VIRTIO_INPUT_PROPERTIES(_state, _field)       \
+        DEFINE_PROP_STRING("serial", _state, _field.serial), \
+        DEFINE_PROP_STRING("seat", _state, _field.seat)
+
 typedef struct VirtIOInput VirtIOInput;
 typedef struct VirtIOInputClass VirtIOInputClass;
 typedef struct VirtIOInputConfig VirtIOInputConfig;
+typedef struct VirtIOInputHID VirtIOInputHID;
 
 struct virtio_input_conf {
     char *serial;
@@ -94,6 +109,13 @@ struct VirtIOInputClass {
     void (*handle_status)(VirtIOInput *vinput, virtio_input_event *event);
 };
 
+struct VirtIOInputHID {
+    VirtIOInput                       parent_obj;
+    QemuInputHandler                  *handler;
+    QemuInputHandlerState             *hs;
+    int                               ledstate;
+};
+
 void virtio_input_send(VirtIOInput *vinput, virtio_input_event *event);
 void virtio_input_init_config(VirtIOInput *vinput,
                               virtio_input_config *config);
Signature · 1 line
1.8.3.1
#10 lore raw

Re: [Qemu-devel] [PATCH qemu 4/6] virtio-input: emulated devices

Quoted 5 lines · Gerd Hoffmann

This patch adds the virtio-input-hid base class and virtio-{keyboard,mouse,tablet} subclasses building on the base class. They are hooked up to the qemu input core and deliver input events to the guest like all other hid devices (ps/2 kbd, usb tablet, ...).

Quoted 8 lines

Using them is as simple as adding "-device virtio-tablet-pci" to your command line. If you want add multiple devices but don't want waste a pci slot for each you can compose a multifunction device this way:

qemu -device virtio-keyboard-pci,addr=0d.0,multifunction=on \
-device virtio-tablet-pci,addr=0d.1,multifunction=on

Hmm - that's interesting.
I was under the impression that a single pci function can be a keyboard, mouse and tablet at the same time.

If they aren't why don't we assign distinct device IDs to them after all?

Quoted 155 lines
---
 hw/input/Makefile.objs           |   1 +
 hw/input/virtio-input-hid.c      | 486 +++++++++++++++++++++++++++++++++++++++
 hw/virtio/virtio-pci.c           |  68 ++++++
 hw/virtio/virtio-pci.h           |  13 ++
 include/hw/virtio/virtio-input.h |  22 ++
 5 files changed, 590 insertions(+)
 create mode 100644 hw/input/virtio-input-hid.c
hw/input/Makefile.objs +1 -0
index ee8bba9..0dae710 100644
@@ -10,6 +10,7 @@ common-obj-$(CONFIG_VMMOUSE) += vmmouse.o
 
 ifeq ($(CONFIG_LINUX),y)
 common-obj-$(CONFIG_VIRTIO) += virtio-input.o
+common-obj-$(CONFIG_VIRTIO) += virtio-input-hid.o
 endif
 
 obj-$(CONFIG_MILKYMIST) += milkymist-softusb.o
hw/input/virtio-input-hid.c new file +128 -0
new file mode 100644
index 0000000..2db993e
@@ -0,0 +1,486 @@
+/*
+ * 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/iov.h"
+
+#include "hw/qdev.h"
+#include "hw/virtio/virtio.h"
+#include "hw/virtio/virtio-input.h"
+
+#include "ui/console.h"
+
+#include <linux/input.h>
+
+#define VIRTIO_ID_NAME_KEYBOARD "QEMU Virtio Keyboard"
+#define VIRTIO_ID_NAME_MOUSE    "QEMU Virtio Mouse"
+#define VIRTIO_ID_NAME_TABLET   "QEMU Virtio Tablet"
+
+/* ----------------------------------------------------------------- */
+
+static const unsigned int keymap_qcode[Q_KEY_CODE_MAX] = {
+    [Q_KEY_CODE_ESC]                 = KEY_ESC,
+    [Q_KEY_CODE_1]                   = KEY_1,
+    [Q_KEY_CODE_2]                   = KEY_2,
+    [Q_KEY_CODE_3]                   = KEY_3,
+    [Q_KEY_CODE_4]                   = KEY_4,
+    [Q_KEY_CODE_5]                   = KEY_5,
+    [Q_KEY_CODE_6]                   = KEY_6,
+    [Q_KEY_CODE_7]                   = KEY_7,
+    [Q_KEY_CODE_8]                   = KEY_8,
+    [Q_KEY_CODE_9]                   = KEY_9,
+    [Q_KEY_CODE_0]                   = KEY_0,
+    [Q_KEY_CODE_MINUS]               = KEY_MINUS,
+    [Q_KEY_CODE_EQUAL]               = KEY_EQUAL,
+    [Q_KEY_CODE_BACKSPACE]           = KEY_BACKSPACE,
+
+    [Q_KEY_CODE_TAB]                 = KEY_TAB,
+    [Q_KEY_CODE_Q]                   = KEY_Q,
+    [Q_KEY_CODE_W]                   = KEY_W,
+    [Q_KEY_CODE_E]                   = KEY_E,
+    [Q_KEY_CODE_R]                   = KEY_R,
+    [Q_KEY_CODE_T]                   = KEY_T,
+    [Q_KEY_CODE_Y]                   = KEY_Y,
+    [Q_KEY_CODE_U]                   = KEY_U,
+    [Q_KEY_CODE_I]                   = KEY_I,
+    [Q_KEY_CODE_O]                   = KEY_O,
+    [Q_KEY_CODE_P]                   = KEY_P,
+    [Q_KEY_CODE_BRACKET_LEFT]        = KEY_LEFTBRACE,
+    [Q_KEY_CODE_BRACKET_RIGHT]       = KEY_RIGHTBRACE,
+    [Q_KEY_CODE_RET]                 = KEY_ENTER,
+
+    [Q_KEY_CODE_CTRL]                = KEY_LEFTCTRL,
+    [Q_KEY_CODE_A]                   = KEY_A,
+    [Q_KEY_CODE_S]                   = KEY_S,
+    [Q_KEY_CODE_D]                   = KEY_D,
+    [Q_KEY_CODE_F]                   = KEY_F,
+    [Q_KEY_CODE_G]                   = KEY_G,
+    [Q_KEY_CODE_H]                   = KEY_H,
+    [Q_KEY_CODE_J]                   = KEY_J,
+    [Q_KEY_CODE_K]                   = KEY_K,
+    [Q_KEY_CODE_L]                   = KEY_L,
+    [Q_KEY_CODE_SEMICOLON]           = KEY_SEMICOLON,
+    [Q_KEY_CODE_APOSTROPHE]          = KEY_APOSTROPHE,
+    [Q_KEY_CODE_GRAVE_ACCENT]        = KEY_GRAVE,
+
+    [Q_KEY_CODE_SHIFT]               = KEY_LEFTSHIFT,
+    [Q_KEY_CODE_BACKSLASH]           = KEY_BACKSLASH,
+    [Q_KEY_CODE_LESS]                = KEY_102ND,
+    [Q_KEY_CODE_Z]                   = KEY_Z,
+    [Q_KEY_CODE_X]                   = KEY_X,
+    [Q_KEY_CODE_C]                   = KEY_C,
+    [Q_KEY_CODE_V]                   = KEY_V,
+    [Q_KEY_CODE_B]                   = KEY_B,
+    [Q_KEY_CODE_N]                   = KEY_N,
+    [Q_KEY_CODE_M]                   = KEY_M,
+    [Q_KEY_CODE_COMMA]               = KEY_COMMA,
+    [Q_KEY_CODE_DOT]                 = KEY_DOT,
+    [Q_KEY_CODE_SLASH]               = KEY_SLASH,
+    [Q_KEY_CODE_SHIFT_R]             = KEY_RIGHTSHIFT,
+
+    [Q_KEY_CODE_ALT]                 = KEY_LEFTALT,
+    [Q_KEY_CODE_SPC]                 = KEY_SPACE,
+    [Q_KEY_CODE_CAPS_LOCK]           = KEY_CAPSLOCK,
+
+    [Q_KEY_CODE_F1]                  = KEY_F1,
+    [Q_KEY_CODE_F2]                  = KEY_F2,
+    [Q_KEY_CODE_F3]                  = KEY_F3,
+    [Q_KEY_CODE_F4]                  = KEY_F4,
+    [Q_KEY_CODE_F5]                  = KEY_F5,
+    [Q_KEY_CODE_F6]                  = KEY_F6,
+    [Q_KEY_CODE_F7]                  = KEY_F7,
+    [Q_KEY_CODE_F8]                  = KEY_F8,
+    [Q_KEY_CODE_F9]                  = KEY_F9,
+    [Q_KEY_CODE_F10]                 = KEY_F10,
+    [Q_KEY_CODE_NUM_LOCK]            = KEY_NUMLOCK,
+    [Q_KEY_CODE_SCROLL_LOCK]         = KEY_SCROLLLOCK,
+
+    [Q_KEY_CODE_KP_0]                = KEY_KP0,
+    [Q_KEY_CODE_KP_1]                = KEY_KP1,
+    [Q_KEY_CODE_KP_2]                = KEY_KP2,
+    [Q_KEY_CODE_KP_3]                = KEY_KP3,
+    [Q_KEY_CODE_KP_4]                = KEY_KP4,
+    [Q_KEY_CODE_KP_5]                = KEY_KP5,
+    [Q_KEY_CODE_KP_6]                = KEY_KP6,
+    [Q_KEY_CODE_KP_7]                = KEY_KP7,
+    [Q_KEY_CODE_KP_8]                = KEY_KP8,
+    [Q_KEY_CODE_KP_9]                = KEY_KP9,
+    [Q_KEY_CODE_KP_SUBTRACT]         = KEY_KPMINUS,
+    [Q_KEY_CODE_KP_ADD]              = KEY_KPPLUS,
+    [Q_KEY_CODE_KP_DECIMAL]          = KEY_KPDOT,
+    [Q_KEY_CODE_KP_ENTER]            = KEY_KPENTER,
+    [Q_KEY_CODE_KP_DIVIDE]           = KEY_KPSLASH,
+    [Q_KEY_CODE_KP_MULTIPLY]         = KEY_KPASTERISK,
+
+    [Q_KEY_CODE_F11]                 = KEY_F11,
+    [Q_KEY_CODE_F12]                 = KEY_F12,
+
+    [Q_KEY_CODE_CTRL_R]              = KEY_RIGHTCTRL,
+    [Q_KEY_CODE_SYSRQ]               = KEY_SYSRQ,
+    [Q_KEY_CODE_ALT_R]               = KEY_RIGHTALT,
+
+    [Q_KEY_CODE_HOME]                = KEY_HOME,
+    [Q_KEY_CODE_UP]                  = KEY_UP,
+    [Q_KEY_CODE_PGUP]                = KEY_PAGEUP,
+    [Q_KEY_CODE_LEFT]                = KEY_LEFT,
+    [Q_KEY_CODE_RIGHT]               = KEY_RIGHT,

hw/input/virtio-input-hid.c line 138

Quoted 10 lines
hw/input/virtio-input-hid.c new file +10 -0
+    [Q_KEY_CODE_END]                 = KEY_END,
+    [Q_KEY_CODE_DOWN]                = KEY_DOWN,
+    [Q_KEY_CODE_PGDN]                = KEY_PAGEDOWN,
+    [Q_KEY_CODE_INSERT]              = KEY_INSERT,
+    [Q_KEY_CODE_DELETE]              = KEY_DELETE,
+
+    [Q_KEY_CODE_META_L]              = KEY_LEFTMETA,
+    [Q_KEY_CODE_META_R]              = KEY_RIGHTMETA,
+    [Q_KEY_CODE_MENU]                = KEY_MENU,
+};

OK these are values send to guest, right?
And they are from linux/input.h, right? But are these reasonable in a cross-platform device?

E.g. Linux is pretty good at backwards compatibility but less good at versioning.

That header says "Most of the keys/buttons are modeled after USB HUT 1.12" but as far as I could see the codes are not from HUT, correct?

Would it be a good idea to use codes from HUT directly? This way we could extend functionality without adding lots of text to the spec, simply by referring to HUT.

Also what defines the subset selected?
I'm a bit worried that we'll get into an endless cycle of spec updates with each new button missing above.

Quoted 9 lines
hw/input/virtio-input-hid.c new file +9 -0
+
+static const unsigned int keymap_button[INPUT_BUTTON_MAX] = {
+    [INPUT_BUTTON_LEFT]              = BTN_LEFT,
+    [INPUT_BUTTON_RIGHT]             = BTN_RIGHT,
+    [INPUT_BUTTON_MIDDLE]            = BTN_MIDDLE,
+    [INPUT_BUTTON_WHEEL_UP]          = BTN_GEAR_UP,
+    [INPUT_BUTTON_WHEEL_DOWN]        = BTN_GEAR_DOWN,
+};
+

hw/input/virtio-input-hid.c line 157

Quoted 10 lines
hw/input/virtio-input-hid.c new file +10 -0
+static const unsigned int axismap_rel[INPUT_AXIS_MAX] = {
+    [INPUT_AXIS_X]                   = REL_X,
+    [INPUT_AXIS_Y]                   = REL_Y,
+};
+
+static const unsigned int axismap_abs[INPUT_AXIS_MAX] = {
+    [INPUT_AXIS_X]                   = ABS_X,
+    [INPUT_AXIS_Y]                   = ABS_Y,
+};
+

In the future, it seems like a good idea to report raw multi-touch events to guests - this would need a different interface along the lines of Documentation/input/multi-touch-protocol.txt

How would we handle such an extension?
Do MT devices generate ST events as well so it's ok to just filter out everything we don't recognize?

Quoted 129 lines
hw/input/virtio-input-hid.c new file +129 -0
+/* ----------------------------------------------------------------- */
+
+static void virtio_input_key_config(VirtIOInput *vinput,
+                                    const unsigned int *keymap,
+                                    size_t mapsize)
+{
+    virtio_input_config keys;
+    int i, bit, byte, bmax = 0;
+
+    memset(&keys, 0, sizeof(keys));
+    for (i = 0; i < mapsize; i++) {
+        bit = keymap[i];
+        if (!bit) {
+            continue;
+        }
+        byte = bit / 8;
+        bit  = bit % 8;
+        keys.u.bitmap[byte] |= (1 << bit);
+        if (bmax < byte+1) {
+            bmax = byte+1;
+        }
+    }
+    keys.select = VIRTIO_INPUT_CFG_EV_BITS;
+    keys.subsel = EV_KEY;
+    keys.size   = bmax;
+    virtio_input_add_config(vinput, &keys);
+}
+
+static void virtio_input_handle_event(DeviceState *dev, QemuConsole *src,
+                                      InputEvent *evt)
+{
+    VirtIOInput *vinput = VIRTIO_INPUT(dev);
+    virtio_input_event event;
+    int qcode;
+
+    switch (evt->kind) {
+    case INPUT_EVENT_KIND_KEY:
+        qcode = qemu_input_key_value_to_qcode(evt->key->key);
+        if (qcode && keymap_qcode[qcode]) {
+            event.type  = cpu_to_le16(EV_KEY);
+            event.code  = cpu_to_le16(keymap_qcode[qcode]);
+            event.value = cpu_to_le32(evt->key->down ? 1 : 0);
+            virtio_input_send(vinput, &event);
+        } else {
+            if (evt->key->down) {
+                fprintf(stderr, "%s: unmapped key: %d [%s]\n", __func__,
+                        qcode, QKeyCode_lookup[qcode]);
+            }
+        }
+        break;
+    case INPUT_EVENT_KIND_BTN:
+        if (keymap_button[evt->btn->button]) {
+            event.type  = cpu_to_le16(EV_KEY);
+            event.code  = cpu_to_le16(keymap_button[evt->btn->button]);
+            event.value = cpu_to_le32(evt->btn->down ? 1 : 0);
+            virtio_input_send(vinput, &event);
+        } else {
+            if (evt->btn->down) {
+                fprintf(stderr, "%s: unmapped button: %d [%s]\n", __func__,
+                        evt->btn->button, InputButton_lookup[evt->btn->button]);
+            }
+        }
+        break;
+    case INPUT_EVENT_KIND_REL:
+        event.type  = cpu_to_le16(EV_REL);
+        event.code  = cpu_to_le16(axismap_rel[evt->rel->axis]);
+        event.value = cpu_to_le32(evt->rel->value);
+        virtio_input_send(vinput, &event);
+        break;
+    case INPUT_EVENT_KIND_ABS:
+        event.type  = cpu_to_le16(EV_ABS);
+        event.code  = cpu_to_le16(axismap_abs[evt->abs->axis]);
+        event.value = cpu_to_le32(evt->abs->value);
+        virtio_input_send(vinput, &event);
+        break;
+    default:
+        /* keep gcc happy */
+        break;
+    }
+}
+
+static void virtio_input_handle_sync(DeviceState *dev)
+{
+    VirtIODevice *vdev = VIRTIO_DEVICE(dev);
+    VirtIOInput *vinput = VIRTIO_INPUT(dev);
+    virtio_input_event event = {
+        .type  = cpu_to_le16(EV_SYN),
+        .code  = cpu_to_le16(SYN_REPORT),
+        .value = 0,
+    };
+
+    virtio_input_send(vinput, &event);
+    virtio_notify(vdev, vinput->evt);
+}
+
+static void virtio_input_hid_realize(DeviceState *dev, Error **errp)
+{
+    VirtIOInputHID *vhid = VIRTIO_INPUT_HID(dev);
+    vhid->hs = qemu_input_handler_register(dev, vhid->handler);
+}
+
+static void virtio_input_hid_unrealize(DeviceState *dev, Error **errp)
+{
+    VirtIOInputHID *vhid = VIRTIO_INPUT_HID(dev);
+    qemu_input_handler_unregister(vhid->hs);
+}
+
+static void virtio_input_hid_change_active(VirtIOInput *vinput)
+{
+    VirtIOInputHID *vhid = VIRTIO_INPUT_HID(vinput);
+
+    if (vinput->active) {
+        qemu_input_handler_activate(vhid->hs);
+    } else {
+        qemu_input_handler_deactivate(vhid->hs);
+    }
+}
+
+static void virtio_input_hid_handle_status(VirtIOInput *vinput,
+                                           virtio_input_event *event)
+{
+    VirtIOInputHID *vhid = VIRTIO_INPUT_HID(vinput);
+    int ledbit = 0;
+
+    switch (le16_to_cpu(event->type)) {
+    case EV_LED:
+        if (event->code == LED_NUML) {
+            ledbit = QEMU_NUM_LOCK_LED;
+        } else if (event->code == LED_CAPSL) {

hw/input/virtio-input-hid.c line 296

Quoted 10 lines
hw/input/virtio-input-hid.c new file +10 -0
+            ledbit = QEMU_CAPS_LOCK_LED;
+        } else if (event->code == LED_SCROLLL) {
+            ledbit = QEMU_SCROLL_LOCK_LED;
+        }
+        if (event->value) {
+            vhid->ledstate |= ledbit;
+        } else {
+            vhid->ledstate &= ~ledbit;
+        }
+        kbd_put_ledstate(vhid->ledstate);

What does this do? notice led light up on one keyboard and propagate state to all keyboards?

Quoted 366 lines
hw/input/virtio-input-hid.c new file +190 -0
+        break;
+    default:
+        fprintf(stderr, "%s: unknown type %d\n", __func__,
+                le16_to_cpu(event->type));
+        break;
+    }
+}
+
+static void virtio_input_hid_class_init(ObjectClass *klass, void *data)
+{
+    VirtIOInputClass *vic = VIRTIO_INPUT_CLASS(klass);
+
+    vic->realize       = virtio_input_hid_realize;
+    vic->unrealize     = virtio_input_hid_unrealize;
+    vic->change_active = virtio_input_hid_change_active;
+    vic->handle_status = virtio_input_hid_handle_status;
+}
+
+static const TypeInfo virtio_input_hid_info = {
+    .name          = TYPE_VIRTIO_INPUT_HID,
+    .parent        = TYPE_VIRTIO_INPUT,
+    .instance_size = sizeof(VirtIOInputHID),
+    .class_init    = virtio_input_hid_class_init,
+    .abstract      = true,
+};
+
+/* ----------------------------------------------------------------- */
+
+static QemuInputHandler virtio_keyboard_handler = {
+    .name  = VIRTIO_ID_NAME_KEYBOARD,
+    .mask  = INPUT_EVENT_MASK_KEY,
+    .event = virtio_input_handle_event,
+    .sync  = virtio_input_handle_sync,
+};
+
+static struct virtio_input_config virtio_keyboard_config[] = {
+    {
+        .select    = VIRTIO_INPUT_CFG_ID_NAME,
+        .size      = sizeof(VIRTIO_ID_NAME_KEYBOARD),
+        .u.string  = VIRTIO_ID_NAME_KEYBOARD,
+    },{
+        .select    = VIRTIO_INPUT_CFG_EV_BITS,
+        .subsel    = EV_REP,
+        .size      = 1,
+    },{
+        .select    = VIRTIO_INPUT_CFG_EV_BITS,
+        .subsel    = EV_LED,
+        .size      = 1,
+        .u.bitmap  = {
+            (1 << LED_NUML) | (1 << LED_CAPSL) | (1 << LED_SCROLLL),
+        },
+    },
+    { /* end of list */ },
+};
+
+static void virtio_keyboard_init(Object *obj)
+{
+    VirtIOInputHID *vhid = VIRTIO_INPUT_HID(obj);
+    VirtIOInput *vinput = VIRTIO_INPUT(obj);
+
+    vhid->handler = &virtio_keyboard_handler;
+    virtio_input_init_config(vinput, virtio_keyboard_config);
+    virtio_input_key_config(vinput, keymap_qcode,
+                            ARRAY_SIZE(keymap_qcode));
+}
+
+static const TypeInfo virtio_keyboard_info = {
+    .name          = TYPE_VIRTIO_KEYBOARD,
+    .parent        = TYPE_VIRTIO_INPUT_HID,
+    .instance_size = sizeof(VirtIOInputHID),
+    .instance_init = virtio_keyboard_init,
+};
+
+/* ----------------------------------------------------------------- */
+
+static QemuInputHandler virtio_mouse_handler = {
+    .name  = VIRTIO_ID_NAME_MOUSE,
+    .mask  = INPUT_EVENT_MASK_BTN | INPUT_EVENT_MASK_REL,
+    .event = virtio_input_handle_event,
+    .sync  = virtio_input_handle_sync,
+};
+
+static struct virtio_input_config virtio_mouse_config[] = {
+    {
+        .select    = VIRTIO_INPUT_CFG_ID_NAME,
+        .size      = sizeof(VIRTIO_ID_NAME_MOUSE),
+        .u.string  = VIRTIO_ID_NAME_MOUSE,
+    },{
+        .select    = VIRTIO_INPUT_CFG_EV_BITS,
+        .subsel    = EV_REL,
+        .size      = 1,
+        .u.bitmap  = {
+            (1 << REL_X) | (1 << REL_Y),
+        },
+    },
+    { /* end of list */ },
+};
+
+static void virtio_mouse_init(Object *obj)
+{
+    VirtIOInputHID *vhid = VIRTIO_INPUT_HID(obj);
+    VirtIOInput *vinput = VIRTIO_INPUT(obj);
+
+    vhid->handler = &virtio_mouse_handler;
+    virtio_input_init_config(vinput, virtio_mouse_config);
+    virtio_input_key_config(vinput, keymap_button,
+                            ARRAY_SIZE(keymap_button));
+}
+
+static const TypeInfo virtio_mouse_info = {
+    .name          = TYPE_VIRTIO_MOUSE,
+    .parent        = TYPE_VIRTIO_INPUT_HID,
+    .instance_size = sizeof(VirtIOInputHID),
+    .instance_init = virtio_mouse_init,
+};
+
+/* ----------------------------------------------------------------- */
+
+static QemuInputHandler virtio_tablet_handler = {
+    .name  = VIRTIO_ID_NAME_TABLET,
+    .mask  = INPUT_EVENT_MASK_BTN | INPUT_EVENT_MASK_ABS,
+    .event = virtio_input_handle_event,
+    .sync  = virtio_input_handle_sync,
+};
+
+static struct virtio_input_config virtio_tablet_config[] = {
+    {
+        .select    = VIRTIO_INPUT_CFG_ID_NAME,
+        .size      = sizeof(VIRTIO_ID_NAME_TABLET),
+        .u.string  = VIRTIO_ID_NAME_TABLET,
+    },{
+        .select    = VIRTIO_INPUT_CFG_EV_BITS,
+        .subsel    = EV_ABS,
+        .size      = 1,
+        .u.bitmap  = {
+            (1 << ABS_X) | (1 << ABS_Y),
+        },
+    },{
+        .select    = VIRTIO_INPUT_CFG_ABS_INFO,
+        .subsel    = ABS_X,
+        .size      = sizeof(virtio_input_absinfo),
+#if 0
+        /* FIXME */
+        .u.abs.max = cpu_to_le32(INPUT_EVENT_ABS_SIZE),
+#else
+        .u.abs.max = INPUT_EVENT_ABS_SIZE,
+#endif
+    },{
+        .select    = VIRTIO_INPUT_CFG_ABS_INFO,
+        .subsel    = ABS_Y,
+        .size      = sizeof(virtio_input_absinfo),
+#if 0
+        /* FIXME */
+        .u.abs.max = cpu_to_le32(INPUT_EVENT_ABS_SIZE),
+#else
+        .u.abs.max = INPUT_EVENT_ABS_SIZE,
+#endif
+    },
+    { /* end of list */ },
+};
+
+static void virtio_tablet_init(Object *obj)
+{
+    VirtIOInputHID *vhid = VIRTIO_INPUT_HID(obj);
+    VirtIOInput *vinput = VIRTIO_INPUT(obj);
+
+    vhid->handler = &virtio_tablet_handler;
+    virtio_input_init_config(vinput, virtio_tablet_config);
+    virtio_input_key_config(vinput, keymap_button,
+                            ARRAY_SIZE(keymap_button));
+}
+
+static const TypeInfo virtio_tablet_info = {
+    .name          = TYPE_VIRTIO_TABLET,
+    .parent        = TYPE_VIRTIO_INPUT_HID,
+    .instance_size = sizeof(VirtIOInputHID),
+    .instance_init = virtio_tablet_init,
+};
+
+/* ----------------------------------------------------------------- */
+
+static void virtio_register_types(void)
+{
+    type_register_static(&virtio_input_hid_info);
+    type_register_static(&virtio_keyboard_info);
+    type_register_static(&virtio_mouse_info);
+    type_register_static(&virtio_tablet_info);
+}
+
+type_init(virtio_register_types)
hw/virtio/virtio-pci.c +68 -0
index 5518192..b421c01 100644
@@ -1534,6 +1534,13 @@ static const TypeInfo virtio_rng_pci_info = {
 
 /* virtio-input-pci */
 
+static Property virtio_input_hid_pci_properties[] = {
+    DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
+    DEFINE_VIRTIO_INPUT_PROPERTIES(VirtIOInputPCI, vdev.input),
+    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);
@@ -1558,6 +1565,34 @@ static void virtio_input_pci_class_init(ObjectClass *klass, void *data)
     pcidev_k->class_id = PCI_CLASS_OTHERS;
 }
 
+static void virtio_input_hid_pci_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+
+    dc->props = virtio_input_hid_pci_properties;
+}
+
+static void virtio_keyboard_initfn(Object *obj)
+{
+    VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj);
+    object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_KEYBOARD);
+    object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
+}
+
+static void virtio_mouse_initfn(Object *obj)
+{
+    VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj);
+    object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_MOUSE);
+    object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
+}
+
+static void virtio_tablet_initfn(Object *obj)
+{
+    VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj);
+    object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_TABLET);
+    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,
@@ -1566,6 +1601,35 @@ static const TypeInfo virtio_input_pci_info = {
     .abstract      = true,
 };
 
+static const TypeInfo virtio_input_hid_pci_info = {
+    .name          = TYPE_VIRTIO_INPUT_HID_PCI,
+    .parent        = TYPE_VIRTIO_INPUT_PCI,
+    .instance_size = sizeof(VirtIOInputHIDPCI),
+    .class_init    = virtio_input_hid_pci_class_init,
+    .abstract      = true,
+};
+
+static const TypeInfo virtio_keyboard_pci_info = {
+    .name          = TYPE_VIRTIO_KEYBOARD_PCI,
+    .parent        = TYPE_VIRTIO_INPUT_HID_PCI,
+    .instance_size = sizeof(VirtIOInputHIDPCI),
+    .instance_init = virtio_keyboard_initfn,
+};
+
+static const TypeInfo virtio_mouse_pci_info = {
+    .name          = TYPE_VIRTIO_MOUSE_PCI,
+    .parent        = TYPE_VIRTIO_INPUT_HID_PCI,
+    .instance_size = sizeof(VirtIOInputHIDPCI),
+    .instance_init = virtio_mouse_initfn,
+};
+
+static const TypeInfo virtio_tablet_pci_info = {
+    .name          = TYPE_VIRTIO_TABLET_PCI,
+    .parent        = TYPE_VIRTIO_INPUT_HID_PCI,
+    .instance_size = sizeof(VirtIOInputHIDPCI),
+    .instance_init = virtio_tablet_initfn,
+};
+
 /* virtio-pci-bus */
 
 static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
@@ -1611,6 +1675,10 @@ static void virtio_pci_register_types(void)
 {
     type_register_static(&virtio_rng_pci_info);
     type_register_static(&virtio_input_pci_info);
+    type_register_static(&virtio_input_hid_pci_info);
+    type_register_static(&virtio_keyboard_pci_info);
+    type_register_static(&virtio_mouse_pci_info);
+    type_register_static(&virtio_tablet_pci_info);
     type_register_static(&virtio_pci_bus_info);
     type_register_static(&virtio_pci_info);
 #ifdef CONFIG_VIRTFS
hw/virtio/virtio-pci.h +13 -0
index f1e75ad..f615ae6 100644
@@ -41,6 +41,7 @@ typedef struct VirtIONetPCI VirtIONetPCI;
 typedef struct VHostSCSIPCI VHostSCSIPCI;
 typedef struct VirtIORngPCI VirtIORngPCI;
 typedef struct VirtIOInputPCI VirtIOInputPCI;
+typedef struct VirtIOInputHIDPCI VirtIOInputHIDPCI;
 
 /* virtio-pci-bus */
 
@@ -213,6 +214,18 @@ struct VirtIOInputPCI {
     VirtIOInput vdev;
 };
 
+#define TYPE_VIRTIO_INPUT_HID_PCI "virtio-input-hid-pci"
+#define TYPE_VIRTIO_KEYBOARD_PCI  "virtio-keyboard-pci"
+#define TYPE_VIRTIO_MOUSE_PCI     "virtio-mouse-pci"
+#define TYPE_VIRTIO_TABLET_PCI    "virtio-tablet-pci"
+#define VIRTIO_INPUT_HID_PCI(obj) \
+        OBJECT_CHECK(VirtIOInputHIDPCI, (obj), TYPE_VIRTIO_INPUT_HID_PCI)
+
+struct VirtIOInputHIDPCI {
+    VirtIOPCIProxy parent_obj;
+    VirtIOInputHID vdev;
+};
+
 /* Virtio ABI version, if we increment this, we break the guest driver. */
 #define VIRTIO_PCI_ABI_VERSION          0
 
include/hw/virtio/virtio-input.h +22 -0
index 5d37a70..4b819c3 100644
@@ -57,9 +57,24 @@ typedef struct virtio_input_event {
 #define VIRTIO_INPUT_CLASS(klass) \
         OBJECT_CLASS_CHECK(VirtIOInputClass, klass, TYPE_VIRTIO_INPUT)
 
+#define TYPE_VIRTIO_INPUT_HID "virtio-input-hid"
+#define TYPE_VIRTIO_KEYBOARD  "virtio-keyboard"
+#define TYPE_VIRTIO_MOUSE     "virtio-mouse"
+#define TYPE_VIRTIO_TABLET    "virtio-tablet"
+
+#define VIRTIO_INPUT_HID(obj) \
+        OBJECT_CHECK(VirtIOInputHID, (obj), TYPE_VIRTIO_INPUT_HID)
+#define VIRTIO_INPUT_HID_GET_PARENT_CLASS(obj) \
+        OBJECT_GET_PARENT_CLASS(obj, TYPE_VIRTIO_INPUT_HID)
+
+#define DEFINE_VIRTIO_INPUT_PROPERTIES(_state, _field)       \
+        DEFINE_PROP_STRING("serial", _state, _field.serial), \
+        DEFINE_PROP_STRING("seat", _state, _field.seat)
+
 typedef struct VirtIOInput VirtIOInput;
 typedef struct VirtIOInputClass VirtIOInputClass;
 typedef struct VirtIOInputConfig VirtIOInputConfig;
+typedef struct VirtIOInputHID VirtIOInputHID;
 
 struct virtio_input_conf {
     char *serial;
@@ -94,6 +109,13 @@ struct VirtIOInputClass {
     void (*handle_status)(VirtIOInput *vinput, virtio_input_event *event);
 };
 
+struct VirtIOInputHID {
+    VirtIOInput                       parent_obj;
+    QemuInputHandler                  *handler;
+    QemuInputHandlerState             *hs;
+    int                               ledstate;
+};
+
 void virtio_input_send(VirtIOInput *vinput, virtio_input_event *event);
 void virtio_input_init_config(VirtIOInput *vinput,
                               virtio_input_config *config);
Signature · 1 line
1.8.3.1
author maintainer
#11 lore raw

Re: [Qemu-devel] [PATCH qemu 4/6] virtio-input: emulated devices

Quoted 10 lines · Michael S. Tsirkin
Quoted 9 lines · Gerd Hoffmann

This patch adds the virtio-input-hid base class and virtio-{keyboard,mouse,tablet} subclasses building on the base class. They are hooked up to the qemu input core and deliver input events to the guest like all other hid devices (ps/2 kbd, usb tablet, ...).

Using them is as simple as adding "-device virtio-tablet-pci" to your command line. If you want add multiple devices but don't want waste a pci slot for each you can compose a multifunction device this way:

Quoted 8 lines
Quoted 4 lines

qemu -device virtio-keyboard-pci,addr=0d.0,multifunction=on \
-device virtio-tablet-pci,addr=0d.1,multifunction=on

Hmm - that's interesting.
I was under the impression that a single pci function can be a keyboard, mouse and tablet at the same time.

It is possible to create a device supporting both keyboard and mouse/tablet events. Which will also show up as single input device in the guest then. People and software tends to not expect that though, so I think it is better to keep them separate.

Quoted 2 lines

If they aren't why don't we assign distinct device IDs to them after all?

pci device ids I assume? Sure, we can do that. Will make lspci output a bit more informative (no need to check /proc/bus/input/devices to figure what kind of input device it is).

Quoted 6 lines

hw/input/virtio-input-hid.c line 138

Quoted 4 lines
hw/input/virtio-input-hid.c new file +4 -0
+    [Q_KEY_CODE_META_L]              = KEY_LEFTMETA,
+    [Q_KEY_CODE_META_R]              = KEY_RIGHTMETA,
+    [Q_KEY_CODE_MENU]                = KEY_MENU,
+};

OK these are values send to guest, right?

Yes.

Quoted 2 lines

And they are from linux/input.h, right? But are these reasonable in a cross-platform device?

Can't see strong reasons speaking against it. It's kernel/userspace API, therefore stable. There are keycodes defined for pretty much anything you can think of.

linux guest code is dead simple. For other guests supporting it shouldn't be that hard too, they basically need a mapping table to map the linux KEY_* codes into their internal representation.

Quoted 2 lines

E.g. Linux is pretty good at backwards compatibility but less good at versioning.

--verbose please.

Quoted 2 lines

That header says "Most of the keys/buttons are modeled after USB HUT 1.12" but as far as I could see the codes are not from HUT, correct?

No, the codes are different.

Quoted 3 lines

Would it be a good idea to use codes from HUT directly? This way we could extend functionality without adding lots of text to the spec, simply by referring to HUT.

I want to simply refer to linux/input.h in the spec.

Quoted 1 line

Also what defines the subset selected?

All keys in linux/input.h are supported by the virtio input protocol.

The current qemu kbd emulation covers all keys qemu knows (see QKeyCode in qapi-schema.json).

Quoted 10 lines

hw/input/virtio-input-hid.c line 157

Quoted 5 lines
hw/input/virtio-input-hid.c new file +5 -0
+static const unsigned int axismap_abs[INPUT_AXIS_MAX] = {
+    [INPUT_AXIS_X]                   = ABS_X,
+    [INPUT_AXIS_Y]                   = ABS_Y,
+};
+

In the future, it seems like a good idea to report raw multi-touch events to guests - this would need a different interface along the lines of Documentation/input/multi-touch-protocol.txt

Should be no big deal. Not looked at that deeply yet due to lack of test hardware, but I think all we need is mapping the info from EVIOCGMTSLOTS into config space, simliar to how it is done for EVIOCGABS.

Quoted 2 lines

Do MT devices generate ST events as well so it's ok to just filter out everything we don't recognize?

Yes, as far I know both mt and st events are generated.

Quoted 16 lines
Quoted 16 lines
hw/input/virtio-input-hid.c new file +16 -0
+static void virtio_input_hid_handle_status(VirtIOInput *vinput,
+                                           virtio_input_event *event)
+{
+    VirtIOInputHID *vhid = VIRTIO_INPUT_HID(vinput);
+    int ledbit = 0;
+
+    switch (le16_to_cpu(event->type)) {
+    case EV_LED:
+        if (event->code == LED_NUML) {
+            ledbit = QEMU_NUM_LOCK_LED;
+        } else if (event->code == LED_CAPSL) {
+            ledbit = QEMU_CAPS_LOCK_LED;
+        } else if (event->code == LED_SCROLLL) {
+            ledbit = QEMU_SCROLL_LOCK_LED;
+        }
+        if (event->value) {
Quoted 8 lines

hw/input/virtio-input-hid.c line 296

Quoted 5 lines
hw/input/virtio-input-hid.c new file +5 -0
+            vhid->ledstate |= ledbit;
+        } else {
+            vhid->ledstate &= ~ledbit;
+        }
+        kbd_put_ledstate(vhid->ledstate);

What does this do? notice led light up on one keyboard and propagate state to all keyboards?

Notify everybody interested in about kbd led changes. ps/2+usb kbd emulations do the same.

It is used by vnc for example, to make sure capslock/numlock state between guest and host stay in sync.

cheers,
Gerd

#12 lore raw

Re: [Qemu-devel] [PATCH qemu 4/6] virtio-input: emulated devices

Quoted 25 lines · Gerd Hoffmann
Quoted 10 lines · Michael S. Tsirkin
Quoted 9 lines · Gerd Hoffmann

This patch adds the virtio-input-hid base class and virtio-{keyboard,mouse,tablet} subclasses building on the base class. They are hooked up to the qemu input core and deliver input events to the guest like all other hid devices (ps/2 kbd, usb tablet, ...).

Using them is as simple as adding "-device virtio-tablet-pci" to your command line. If you want add multiple devices but don't want waste a pci slot for each you can compose a multifunction device this way:

Quoted 8 lines
Quoted 4 lines

qemu -device virtio-keyboard-pci,addr=0d.0,multifunction=on \
-device virtio-tablet-pci,addr=0d.1,multifunction=on

Hmm - that's interesting.
I was under the impression that a single pci function can be a keyboard, mouse and tablet at the same time.

It is possible to create a device supporting both keyboard and mouse/tablet events. Which will also show up as single input device in the guest then. People and software tends to not expect that though, so I think it is better to keep them separate.

Quoted 6 lines
Quoted 2 lines

If they aren't why don't we assign distinct device IDs to them after all?

pci device ids I assume? Sure, we can do that. Will make lspci output a bit more informative (no need to check /proc/bus/input/devices to figure what kind of input device it is).

It's up to you I am just asking.

If drivers don't expect a mix of functionality, then separate IDs seem better.
OTOH an advantage to using a custom identification scheme would be if you want to allow a single device to change type dynamically.

E.g. if guest knows about MT, act as an MT device otherwise act as a mouse.

Or more interestingly, as we migrate from host with a mouse to an MT one, switch from a mouse to a touch device?

Quoted 16 lines
Quoted 6 lines

hw/input/virtio-input-hid.c line 138

Quoted 4 lines
hw/input/virtio-input-hid.c new file +4 -0
+    [Q_KEY_CODE_META_L]              = KEY_LEFTMETA,
+    [Q_KEY_CODE_META_R]              = KEY_RIGHTMETA,
+    [Q_KEY_CODE_MENU]                = KEY_MENU,
+};

OK these are values send to guest, right?

Yes.

Quoted 2 lines

And they are from linux/input.h, right? But are these reasonable in a cross-platform device?

Can't see strong reasons speaking against it. It's kernel/userspace API, therefore stable. There are keycodes defined for pretty much anything you can think of.

Quoted 7 lines

linux guest code is dead simple. For other guests supporting it shouldn't be that hard too, they basically need a mapping table to map the linux KEY_* codes into their internal representation.

Quoted 2 lines

E.g. Linux is pretty good at backwards compatibility but less good at versioning.

--verbose please.

See below.

Quoted 10 lines
Quoted 2 lines

That header says "Most of the keys/buttons are modeled after USB HUT 1.12" but as far as I could see the codes are not from HUT, correct?

No, the codes are different.

Quoted 3 lines

Would it be a good idea to use codes from HUT directly? This way we could extend functionality without adding lots of text to the spec, simply by referring to HUT.

I want to simply refer to linux/input.h in the spec.

That's exactly the question. Which version of linux/input.h?

Quoted 3 lines
Quoted 1 line

Also what defines the subset selected?

All keys in linux/input.h are supported by the virtio input protocol.

I'm asking how is this versioned: assume that linux adds a new keycode, existing guests don't expect it. Don't we need any negotiation? Is it really always safe to ignore keypresses?

Quoted 2 lines

The current qemu kbd emulation covers all keys qemu knows (see QKeyCode in qapi-schema.json).

Hmm SDL can give us a bunch of codes like volume up/down that you don't seem to include.

Quoted 54 lines
Quoted 10 lines

hw/input/virtio-input-hid.c line 157

Quoted 5 lines
hw/input/virtio-input-hid.c new file +5 -0
+static const unsigned int axismap_abs[INPUT_AXIS_MAX] = {
+    [INPUT_AXIS_X]                   = ABS_X,
+    [INPUT_AXIS_Y]                   = ABS_Y,
+};
+

In the future, it seems like a good idea to report raw multi-touch events to guests - this would need a different interface along the lines of Documentation/input/multi-touch-protocol.txt

Should be no big deal. Not looked at that deeply yet due to lack of test hardware, but I think all we need is mapping the info from EVIOCGMTSLOTS into config space, simliar to how it is done for EVIOCGABS.

Quoted 2 lines

Do MT devices generate ST events as well so it's ok to just filter out everything we don't recognize?

Yes, as far I know both mt and st events are generated.

Quoted 16 lines
Quoted 16 lines
hw/input/virtio-input-hid.c new file +16 -0
+static void virtio_input_hid_handle_status(VirtIOInput *vinput,
+                                           virtio_input_event *event)
+{
+    VirtIOInputHID *vhid = VIRTIO_INPUT_HID(vinput);
+    int ledbit = 0;
+
+    switch (le16_to_cpu(event->type)) {
+    case EV_LED:
+        if (event->code == LED_NUML) {
+            ledbit = QEMU_NUM_LOCK_LED;
+        } else if (event->code == LED_CAPSL) {
+            ledbit = QEMU_CAPS_LOCK_LED;
+        } else if (event->code == LED_SCROLLL) {
+            ledbit = QEMU_SCROLL_LOCK_LED;
+        }
+        if (event->value) {
Quoted 8 lines

hw/input/virtio-input-hid.c line 296

Quoted 5 lines
hw/input/virtio-input-hid.c new file +5 -0
+            vhid->ledstate |= ledbit;
+        } else {
+            vhid->ledstate &= ~ledbit;
+        }
+        kbd_put_ledstate(vhid->ledstate);

What does this do? notice led light up on one keyboard and propagate state to all keyboards?

Notify everybody interested in about kbd led changes. ps/2+usb kbd emulations do the same.

It is used by vnc for example, to make sure capslock/numlock state between guest and host stay in sync.

cheers,
Gerd

author maintainer
#13 lore raw

Re: [Qemu-devel] [PATCH qemu 4/6] virtio-input: emulated devices

Hi,

Quoted 2 lines

If drivers don't expect a mix of functionality, then separate IDs seem better.

The linux kernel can deal with it just fine. It's just that humans are more used to have separate devices. And I remember back in the dark xen days the input channel for xenfb was a unified input device which delivered both mouse and keyboard events, and it caused some grief with the X11 configuration.

While thinking about it: We could certainly make this user-configurable. So instead of adding three new devices we just add one with config options. Then we'll have

-device virtio-input-emulated-pci,kbd=on

instead of

-device virtio-keyboard-pci

Likewise for mouse and tablet. And it'll be possible then to just do

-device virtio-input-emulated-pci,kbd=on,tablet=on

to get a unified device.

Quoted 3 lines

OTOH an advantage to using a custom identification scheme would be if you want to allow a single device to change type dynamically.

Changing dynamically isn't going to happen.

Quoted 2 lines

Or more interestingly, as we migrate from host with a mouse to an MT one, switch from a mouse to a touch device?

Multitouch is tricky, especially the input side. gtk+sdl ui could work without too much trouble. spice+vnc need protocol extensions for that. Not investigated yet in detail. Guess it's a good excuse to expense a new, shiny and multi-touch capable monitor ;)

Singletouch is easy. We'll basically send BTN_TOUCH instead of BTN_LEFT. And with some additional tweaks (such as filtering out any move events with mousebutton/finger lifted) we can even use normal mouse input to fake a touch screen.

Quoted 6 lines
Quoted 1 line

All keys in linux/input.h are supported by the virtio input protocol.

I'm asking how is this versioned: assume that linux adds a new keycode, existing guests don't expect it. Don't we need any negotiation? Is it really always safe to ignore keypresses?

Worst case is a non-working key. I also don't think you'll run into this in practice. All fundamental stuff is there. Stuff which gets added now is pretty exotic, like this:

=================== cut here ======================

commit 5a1bbf21325bd4f2641f6141fb8c47f6095578dd

Author: Dmitry Torokhov <[email protected]>
Date:   Sat Feb 2 11:53:47 2013 -0800

Input: add new keycodes for passenger control units

Entertainment systems used in aircraft need additional keycodes for their Passenger Control Units, so let's add them.

include/uapi/linux/input.h +5 -0
index 5588285..6e4e3c6 100644
@@ -702,6 +702,11 @@ struct input_keymap_entry {
 #define KEY_CAMERA_LEFT                0x219
 #define KEY_CAMERA_RIGHT       0x21a
 
+#define KEY_ATTENDANT_ON       0x21b
+#define KEY_ATTENDANT_OFF      0x21c
+#define KEY_ATTENDANT_TOGGLE   0x21d   /* Attendant call on or off */
+#define KEY_LIGHTS_TOGGLE      0x21e   /* Reading light on or off */
+
 #define BTN_TRIGGER_HAPPY              0x2c0
 #define BTN_TRIGGER_HAPPY1             0x2c0
 #define BTN_TRIGGER_HAPPY2             0x2c1

=================== cut here ======================

And if you really care about this you'll probably make sure your guests do know what these key codes are for anyway ;)

Quoted 5 lines
Quoted 2 lines

The current qemu kbd emulation covers all keys qemu knows (see QKeyCode in qapi-schema.json).

Hmm SDL can give us a bunch of codes like volume up/down that you don't seem to include.

Disclaimer: I havn't added QkeyCode, it was there already.

I expect QKeyCode learn more keys in the future. The input layer rewrite in the 2.0 devel cycle moved QKeyCode into the focus as it is used more widely in qemu now. Until recently it was used only for the sendkey monitor command.

cheers,
Gerd

author maintainer
#14 lore raw

PATCH 5/6 virtio-input: control device

In reply to Gerd Hoffmann

Device for sending non-input control messages to the guest. For now this is only a single event: shutdown requests are sent as power button press to the guest.

Possible other use is signaling sound volume changes to the guest (via EV_ABS / ABS_VOLUME). I expect we'll find more over time.

---
 hw/input/Makefile.objs           |   1 +
 hw/input/virtio-input-control.c  | 112 +++++++++++++++++++++++++++++++++++++++
 hw/virtio/virtio-pci.c           |  29 ++++++++++
 hw/virtio/virtio-pci.h           |  10 ++++
 include/hw/virtio/virtio-input.h |  12 +++++
 5 files changed, 164 insertions(+)
 create mode 100644 hw/input/virtio-input-control.c
hw/input/Makefile.objs +1 -0
index 0dae710..0179154 100644
@@ -11,6 +11,7 @@ common-obj-$(CONFIG_VMMOUSE) += vmmouse.o
 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
 endif
 
 obj-$(CONFIG_MILKYMIST) += milkymist-softusb.o
hw/input/virtio-input-control.c new file +112 -0

112 lines not on this page · raw message

hw/virtio/virtio-pci.c +29 -0
index b421c01..9446d45 100644
@@ -1541,6 +1541,12 @@ static Property virtio_input_hid_pci_properties[] = {
     DEFINE_PROP_END_OF_LIST(),
 };
 
+static Property virtio_input_ctrl_pci_properties[] = {
+    DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
+    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);
@@ -1572,6 +1578,13 @@ static void virtio_input_hid_pci_class_init(ObjectClass *klass, void *data)
     dc->props = virtio_input_hid_pci_properties;
 }
 
+static void virtio_input_ctrl_pci_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+
+    dc->props = virtio_input_ctrl_pci_properties;
+}
+
 static void virtio_keyboard_initfn(Object *obj)
 {
     VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj);
@@ -1593,6 +1606,13 @@ static void virtio_tablet_initfn(Object *obj)
     object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
 }
 
+static void virtio_ctrl_initfn(Object *obj)
+{
+    VirtIOInputCtrlPCI *dev = VIRTIO_INPUT_CTRL_PCI(obj);
+    object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_INPUT_CTRL);
+    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,
@@ -1630,6 +1650,14 @@ static const TypeInfo virtio_tablet_pci_info = {
     .instance_init = virtio_tablet_initfn,
 };
 
+static const TypeInfo virtio_ctrl_pci_info = {
+    .name          = TYPE_VIRTIO_INPUT_CTRL_PCI,
+    .parent        = TYPE_VIRTIO_INPUT_PCI,
+    .instance_size = sizeof(VirtIOInputCtrlPCI),
+    .instance_init = virtio_ctrl_initfn,
+    .class_init    = virtio_input_ctrl_pci_class_init,
+};
+
 /* virtio-pci-bus */
 
 static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
@@ -1679,6 +1707,7 @@ static void virtio_pci_register_types(void)
     type_register_static(&virtio_keyboard_pci_info);
     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_pci_bus_info);
     type_register_static(&virtio_pci_info);
 #ifdef CONFIG_VIRTFS
hw/virtio/virtio-pci.h +10 -0
index f615ae6..0c26e64 100644
@@ -42,6 +42,7 @@ typedef struct VHostSCSIPCI VHostSCSIPCI;
 typedef struct VirtIORngPCI VirtIORngPCI;
 typedef struct VirtIOInputPCI VirtIOInputPCI;
 typedef struct VirtIOInputHIDPCI VirtIOInputHIDPCI;
+typedef struct VirtIOInputCtrlPCI VirtIOInputCtrlPCI;
 
 /* virtio-pci-bus */
 
@@ -226,6 +227,15 @@ struct VirtIOInputHIDPCI {
     VirtIOInputHID vdev;
 };
 
+#define TYPE_VIRTIO_INPUT_CTRL_PCI "virtio-input-control-pci"
+#define VIRTIO_INPUT_CTRL_PCI(obj) \
+        OBJECT_CHECK(VirtIOInputCtrlPCI, (obj), TYPE_VIRTIO_INPUT_CTRL_PCI)
+
+struct VirtIOInputCtrlPCI {
+    VirtIOPCIProxy parent_obj;
+    VirtIOInputCtrl vdev;
+};
+
 /* Virtio ABI version, if we increment this, we break the guest driver. */
 #define VIRTIO_PCI_ABI_VERSION          0
 
include/hw/virtio/virtio-input.h +12 -0
index 4b819c3..d753834 100644
@@ -67,6 +67,12 @@ typedef struct virtio_input_event {
 #define VIRTIO_INPUT_HID_GET_PARENT_CLASS(obj) \
         OBJECT_GET_PARENT_CLASS(obj, TYPE_VIRTIO_INPUT_HID)
 
+#define TYPE_VIRTIO_INPUT_CTRL   "virtio-input-control"
+#define VIRTIO_INPUT_CTRL(obj) \
+        OBJECT_CHECK(VirtIOInputCtrl, (obj), TYPE_VIRTIO_INPUT_CTRL)
+#define VIRTIO_INPUT_CTRL_GET_PARENT_CLASS(obj) \
+        OBJECT_GET_PARENT_CLASS(obj, TYPE_VIRTIO_INPUT_CTRL)
+
 #define DEFINE_VIRTIO_INPUT_PROPERTIES(_state, _field)       \
         DEFINE_PROP_STRING("serial", _state, _field.serial), \
         DEFINE_PROP_STRING("seat", _state, _field.seat)
@@ -75,6 +81,7 @@ typedef struct VirtIOInput VirtIOInput;
 typedef struct VirtIOInputClass VirtIOInputClass;
 typedef struct VirtIOInputConfig VirtIOInputConfig;
 typedef struct VirtIOInputHID VirtIOInputHID;
+typedef struct VirtIOInputCtrl VirtIOInputCtrl;
 
 struct virtio_input_conf {
     char *serial;
@@ -116,6 +123,11 @@ struct VirtIOInputHID {
     int                               ledstate;
 };
 
+struct VirtIOInputCtrl {
+    VirtIOInput                       parent_obj;
+    Notifier                          powerdown;
+};
+
 void virtio_input_send(VirtIOInput *vinput, virtio_input_event *event);
 void virtio_input_init_config(VirtIOInput *vinput,
                               virtio_input_config *config);
Signature · 1 line
1.8.3.1
#15 lore raw

Re: [Qemu-devel] [PATCH qemu 5/6] virtio-input: control device

Quoted 8 lines · Gerd Hoffmann

Device for sending non-input control messages to the guest. For now this is only a single event: shutdown requests are sent as power button press to the guest.

Possible other use is signaling sound volume changes to the guest (via EV_ABS / ABS_VOLUME). I expect we'll find more over time.

Why not use a keyboard device for this?

Quoted 274 lines
---
 hw/input/Makefile.objs           |   1 +
 hw/input/virtio-input-control.c  | 112 +++++++++++++++++++++++++++++++++++++++
 hw/virtio/virtio-pci.c           |  29 ++++++++++
 hw/virtio/virtio-pci.h           |  10 ++++
 include/hw/virtio/virtio-input.h |  12 +++++
 5 files changed, 164 insertions(+)
 create mode 100644 hw/input/virtio-input-control.c
hw/input/Makefile.objs +1 -0
index 0dae710..0179154 100644
@@ -11,6 +11,7 @@ common-obj-$(CONFIG_VMMOUSE) += vmmouse.o
 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
 endif
 
 obj-$(CONFIG_MILKYMIST) += milkymist-softusb.o
hw/input/virtio-input-control.c new file +112 -0
new file mode 100644
index 0000000..3e439e6
@@ -0,0 +1,112 @@
+/*
+ * 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/iov.h"
+
+#include "hw/qdev.h"
+#include "hw/virtio/virtio.h"
+#include "hw/virtio/virtio-input.h"
+
+#include "ui/console.h"
+
+#include <linux/input.h>
+
+#define VIRTIO_ID_NAME_CTRL "QEMU Virtio Control Panel"
+
+/* ----------------------------------------------------------------- */
+
+static void virtio_input_key_config(VirtIOInput *vinput)
+{
+    static const int keylist[] = { KEY_POWER };
+    virtio_input_config keys;
+    int i, bit, byte, bmax = 0;
+
+    memset(&keys, 0, sizeof(keys));
+    for (i = 0; i < ARRAY_SIZE(keylist); i++) {
+        byte = keylist[i] / 8;
+        bit  = keylist[i] % 8;
+        keys.u.bitmap[byte] |= (1 << bit);
+        if (bmax < byte+1) {
+            bmax = byte+1;
+        }
+    }
+    keys.select = VIRTIO_INPUT_CFG_EV_BITS;
+    keys.subsel = EV_KEY;
+    keys.size   = bmax;
+    virtio_input_add_config(vinput, &keys);
+}
+
+static void virtio_input_ctrl_keypress(VirtIOInput *vinput, int keycode)
+{
+    virtio_input_event key_down = {
+        .type  = cpu_to_le16(EV_KEY),
+        .code  = cpu_to_le16(keycode),
+        .value = 1,
+    };
+    virtio_input_event key_up = {
+        .type  = cpu_to_le16(EV_KEY),
+        .code  = cpu_to_le16(keycode),
+        .value = 0,
+    };
+    virtio_input_event sync = {
+        .type  = cpu_to_le16(EV_SYN),
+        .code  = cpu_to_le16(SYN_REPORT),
+        .value = 0,
+    };
+
+    virtio_input_send(vinput, &key_down);
+    virtio_input_send(vinput, &sync);
+    virtio_input_send(vinput, &key_up);
+    virtio_input_send(vinput, &sync);
+    virtio_notify(VIRTIO_DEVICE(vinput), vinput->evt);
+}
+
+static void virtio_input_ctrl_powerdown(Notifier *n, void *opaque)
+{
+    VirtIOInputCtrl *vctrl =
+        container_of(n, VirtIOInputCtrl, powerdown);
+
+    virtio_input_ctrl_keypress(VIRTIO_INPUT(vctrl), KEY_POWER);
+}
+
+/* ----------------------------------------------------------------- */
+
+static struct virtio_input_config virtio_ctrl_config[] = {
+    {
+        .select    = VIRTIO_INPUT_CFG_ID_NAME,
+        .size      = sizeof(VIRTIO_ID_NAME_CTRL),
+        .u.string  = VIRTIO_ID_NAME_CTRL,
+    },
+    { /* end of list */ },
+};
+
+static void virtio_ctrl_init(Object *obj)
+{
+    VirtIOInput *vinput = VIRTIO_INPUT(obj);
+    VirtIOInputCtrl *vctrl = VIRTIO_INPUT_CTRL(obj);
+
+    virtio_input_init_config(vinput, virtio_ctrl_config);
+    virtio_input_key_config(vinput);
+
+    vctrl->powerdown.notify = virtio_input_ctrl_powerdown;
+    qemu_register_powerdown_notifier(&vctrl->powerdown);
+}
+
+static const TypeInfo virtio_ctrl_info = {
+    .name          = TYPE_VIRTIO_INPUT_CTRL,
+    .parent        = TYPE_VIRTIO_INPUT,
+    .instance_size = sizeof(VirtIOInputCtrl),
+    .instance_init = virtio_ctrl_init,
+};
+
+/* ----------------------------------------------------------------- */
+
+static void virtio_register_types(void)
+{
+    type_register_static(&virtio_ctrl_info);
+}
+
+type_init(virtio_register_types)
hw/virtio/virtio-pci.c +29 -0
index b421c01..9446d45 100644
@@ -1541,6 +1541,12 @@ static Property virtio_input_hid_pci_properties[] = {
     DEFINE_PROP_END_OF_LIST(),
 };
 
+static Property virtio_input_ctrl_pci_properties[] = {
+    DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
+    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);
@@ -1572,6 +1578,13 @@ static void virtio_input_hid_pci_class_init(ObjectClass *klass, void *data)
     dc->props = virtio_input_hid_pci_properties;
 }
 
+static void virtio_input_ctrl_pci_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+
+    dc->props = virtio_input_ctrl_pci_properties;
+}
+
 static void virtio_keyboard_initfn(Object *obj)
 {
     VirtIOInputHIDPCI *dev = VIRTIO_INPUT_HID_PCI(obj);
@@ -1593,6 +1606,13 @@ static void virtio_tablet_initfn(Object *obj)
     object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL);
 }
 
+static void virtio_ctrl_initfn(Object *obj)
+{
+    VirtIOInputCtrlPCI *dev = VIRTIO_INPUT_CTRL_PCI(obj);
+    object_initialize(&dev->vdev, sizeof(dev->vdev), TYPE_VIRTIO_INPUT_CTRL);
+    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,
@@ -1630,6 +1650,14 @@ static const TypeInfo virtio_tablet_pci_info = {
     .instance_init = virtio_tablet_initfn,
 };
 
+static const TypeInfo virtio_ctrl_pci_info = {
+    .name          = TYPE_VIRTIO_INPUT_CTRL_PCI,
+    .parent        = TYPE_VIRTIO_INPUT_PCI,
+    .instance_size = sizeof(VirtIOInputCtrlPCI),
+    .instance_init = virtio_ctrl_initfn,
+    .class_init    = virtio_input_ctrl_pci_class_init,
+};
+
 /* virtio-pci-bus */
 
 static void virtio_pci_bus_new(VirtioBusState *bus, size_t bus_size,
@@ -1679,6 +1707,7 @@ static void virtio_pci_register_types(void)
     type_register_static(&virtio_keyboard_pci_info);
     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_pci_bus_info);
     type_register_static(&virtio_pci_info);
 #ifdef CONFIG_VIRTFS
hw/virtio/virtio-pci.h +10 -0
index f615ae6..0c26e64 100644
@@ -42,6 +42,7 @@ typedef struct VHostSCSIPCI VHostSCSIPCI;
 typedef struct VirtIORngPCI VirtIORngPCI;
 typedef struct VirtIOInputPCI VirtIOInputPCI;
 typedef struct VirtIOInputHIDPCI VirtIOInputHIDPCI;
+typedef struct VirtIOInputCtrlPCI VirtIOInputCtrlPCI;
 
 /* virtio-pci-bus */
 
@@ -226,6 +227,15 @@ struct VirtIOInputHIDPCI {
     VirtIOInputHID vdev;
 };
 
+#define TYPE_VIRTIO_INPUT_CTRL_PCI "virtio-input-control-pci"
+#define VIRTIO_INPUT_CTRL_PCI(obj) \
+        OBJECT_CHECK(VirtIOInputCtrlPCI, (obj), TYPE_VIRTIO_INPUT_CTRL_PCI)
+
+struct VirtIOInputCtrlPCI {
+    VirtIOPCIProxy parent_obj;
+    VirtIOInputCtrl vdev;
+};
+
 /* Virtio ABI version, if we increment this, we break the guest driver. */
 #define VIRTIO_PCI_ABI_VERSION          0
 
include/hw/virtio/virtio-input.h +12 -0
index 4b819c3..d753834 100644
@@ -67,6 +67,12 @@ typedef struct virtio_input_event {
 #define VIRTIO_INPUT_HID_GET_PARENT_CLASS(obj) \
         OBJECT_GET_PARENT_CLASS(obj, TYPE_VIRTIO_INPUT_HID)
 
+#define TYPE_VIRTIO_INPUT_CTRL   "virtio-input-control"
+#define VIRTIO_INPUT_CTRL(obj) \
+        OBJECT_CHECK(VirtIOInputCtrl, (obj), TYPE_VIRTIO_INPUT_CTRL)
+#define VIRTIO_INPUT_CTRL_GET_PARENT_CLASS(obj) \
+        OBJECT_GET_PARENT_CLASS(obj, TYPE_VIRTIO_INPUT_CTRL)
+
 #define DEFINE_VIRTIO_INPUT_PROPERTIES(_state, _field)       \
         DEFINE_PROP_STRING("serial", _state, _field.serial), \
         DEFINE_PROP_STRING("seat", _state, _field.seat)
@@ -75,6 +81,7 @@ typedef struct VirtIOInput VirtIOInput;
 typedef struct VirtIOInputClass VirtIOInputClass;
 typedef struct VirtIOInputConfig VirtIOInputConfig;
 typedef struct VirtIOInputHID VirtIOInputHID;
+typedef struct VirtIOInputCtrl VirtIOInputCtrl;
 
 struct virtio_input_conf {
     char *serial;
@@ -116,6 +123,11 @@ struct VirtIOInputHID {
     int                               ledstate;
 };
 
+struct VirtIOInputCtrl {
+    VirtIOInput                       parent_obj;
+    Notifier                          powerdown;
+};
+
 void virtio_input_send(VirtIOInput *vinput, virtio_input_event *event);
 void virtio_input_init_config(VirtIOInput *vinput,
                               virtio_input_config *config);
Signature · 1 line
1.8.3.1
author maintainer
#16 lore raw

Re: [Qemu-devel] [PATCH qemu 5/6] virtio-input: control device

Quoted 5 lines · Michael S. Tsirkin
Quoted 4 lines · Gerd Hoffmann

Device for sending non-input control messages to the guest. For now this is only a single event: shutdown requests are sent as power button press to the guest.

Quoted 8 lines
Quoted 4 lines

Possible other use is signaling sound volume changes to the guest (via EV_ABS / ABS_VOLUME). I expect we'll find more over time.

Why not use a keyboard device for this?

From the guests point of view this is looks like a keyboard. A keyboard

with a single key: power.

I prefer a clear separation between devices being feed from user input and the control device which monitors other event sources (powerdown notifier).

There is no fundamental reason why this can't live in the emulated keyboard though.

cheers,
Gerd

#17 lore raw

Re: [Qemu-devel] [PATCH qemu 5/6] virtio-input: control device

Quoted 20 lines · Gerd Hoffmann
Quoted 5 lines · Michael S. Tsirkin
Quoted 4 lines · Gerd Hoffmann

Device for sending non-input control messages to the guest. For now this is only a single event: shutdown requests are sent as power button press to the guest.

Quoted 8 lines
Quoted 4 lines

Possible other use is signaling sound volume changes to the guest (via EV_ABS / ABS_VOLUME). I expect we'll find more over time.

Why not use a keyboard device for this?

From the guests point of view this is looks like a keyboard. A keyboard

with a single key: power.

I prefer a clear separation between devices being feed from user input and the control device which monitors other event sources (powerdown notifier).

Quoted 6 lines

There is no fundamental reason why this can't live in the emulated keyboard though.

cheers,
Gerd

Well I have a keyboard with volume keys - sleep and wakeup buttons too. Not power but that's not out of the realm of possibility.
If we want to be able to pass that through, it should work as a virtio keyboard right?

Signature · 1 line
MST
author maintainer
#18 lore raw

Re: [Qemu-devel] [PATCH qemu 5/6] virtio-input: control device

Quoted 24 lines · Michael S. Tsirkin
Quoted 23 lines · Gerd Hoffmann
Quoted 5 lines · Michael S. Tsirkin
Quoted 4 lines · Gerd Hoffmann

Device for sending non-input control messages to the guest. For now this is only a single event: shutdown requests are sent as power button press to the guest.

Quoted 8 lines
Quoted 4 lines

Possible other use is signaling sound volume changes to the guest (via EV_ABS / ABS_VOLUME). I expect we'll find more over time.

Why not use a keyboard device for this?

From the guests point of view this is looks like a keyboard. A keyboard

with a single key: power.

I prefer a clear separation between devices being feed from user input and the control device which monitors other event sources (powerdown notifier).

There is no fundamental reason why this can't live in the emulated keyboard though.

Quoted 8 lines
Quoted 3 lines

cheers,
Gerd

Well I have a keyboard with volume keys - sleep and wakeup buttons too. Not power but that's not out of the realm of possibility.
If we want to be able to pass that through, it should work as a virtio keyboard right?

Sure. That is another story though. QKeyCode needs to learn those keys, they need to be added to the mappings, and then they'll be handled like any other key.

What I was thinking about is sending feedback about volume changes to the guest is something completely different, let me explain in detail:

What we have in spice today is that the guests volume change request is passed to the spice client, which in turn forwards it to pulseaudio, which then actually applies the volume setting to the audio stream. And you can see the guests volume changes in the hosts mixer UI (sound settings -> applications): change the volume in the guest and watch the slider move.

Today this is a one-way ticket. If you fiddle with the volume in the hosts mixer ui the guest doesn't notice. A emulated volume wheel would be a possible way to feedback volume changes on the host to the guest. Note that it isn't that simple to implement as it need a spice protocol extension too (and support in spice server + client of course).

cheers,
Gerd

author maintainer
#19 lore raw

PATCH 6/6 virtio-input: evdev passthrough

In reply to Gerd Hoffmann

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.

---
 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
hw/input/Makefile.objs +1 -0
index 0179154..9461b37 100644
@@ -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
hw/input/virtio-input-host.c new file +174 -0

174 lines not on this page · raw message

hw/virtio/virtio-pci.c +31 -0
index 9446d45..9f81fc0 100644
@@ -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
hw/virtio/virtio-pci.h +10 -0
index 0c26e64..4c12770 100644
@@ -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
 
include/hw/virtio/virtio-input.h +13 -0
index d753834..94ea405 100644
@@ -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);
Signature · 1 line
1.8.3.1
#20 lore raw

Re: [Qemu-devel] [PATCH qemu 6/6] virtio-input: evdev passthrough

Quoted 52 lines · Gerd Hoffmann

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.

---
 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
hw/input/Makefile.objs +1 -0
index 0179154..9461b37 100644
@@ -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
hw/input/virtio-input-host.c new file +16 -0
new file mode 100644
index 0000000..663d967
@@ -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>

hw/input/virtio-input-host.c line 26

Quoted 10 lines
hw/input/virtio-input-host.c new file +10 -0
+
+/* ----------------------------------------------------------------- */
+
+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.

hw/input/virtio-input-host.c line 33

Quoted 7 lines
hw/input/virtio-input-host.c new file +7 -0
+    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.

Quoted 61 lines
hw/input/virtio-input-host.c new file +61 -0
+        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)  {

hw/input/virtio-input-host.c line 104

Quoted 10 lines
hw/input/virtio-input-host.c new file +10 -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?

Quoted 208 lines
hw/input/virtio-input-host.c new file +70 -0
+
+    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)
hw/virtio/virtio-pci.c +31 -0
index 9446d45..9f81fc0 100644
@@ -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
hw/virtio/virtio-pci.h +10 -0
index 0c26e64..4c12770 100644
@@ -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
 
include/hw/virtio/virtio-input.h +13 -0
index d753834..94ea405 100644
@@ -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);
Signature · 1 line
1.8.3.1
author maintainer
#21 lore raw

Re: [Qemu-devel] [PATCH qemu 6/6] virtio-input: evdev passthrough

Hi,

Quoted 6 lines

hw/input/virtio-input-host.c line 26

Quoted 3 lines
hw/input/virtio-input-host.c new file +3 -0
+static void virtio_input_host_event(void *opaque)
+{
+    VirtIOInputHost *vhost = opaque;

I'd prefer a name that does not imply
vhost infrastructure

ok.

Quoted 8 lines

hw/input/virtio-input-host.c line 104

Quoted 5 lines
hw/input/virtio-input-host.c new file +5 -0
+    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?

I guess the only thing we might want to here is bail out in case the major version is != 1 (which implies a incompatible change). The major version didn't change so far though, and I think it is highly unlikely that it'll ever happen.

cheers,
Gerd

#22 lore raw

Re: [Qemu-devel] [PATCH qemu 6/6] virtio-input: evdev passthrough

Quoted 20 lines · Gerd Hoffmann

Hi,

Quoted 6 lines

hw/input/virtio-input-host.c line 26

Quoted 3 lines
hw/input/virtio-input-host.c new file +3 -0
+static void virtio_input_host_event(void *opaque)
+{
+    VirtIOInputHost *vhost = opaque;

I'd prefer a name that does not imply
vhost infrastructure

ok.

Quoted 8 lines

hw/input/virtio-input-host.c line 104

Quoted 5 lines
hw/input/virtio-input-host.c new file +5 -0
+    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?

Quoted 7 lines

I guess the only thing we might want to here is bail out in case the major version is != 1 (which implies a incompatible change). The major version didn't change so far though, and I think it is highly unlikely that it'll ever happen.

cheers,
Gerd

BTW this device should block migration, right?

author maintainer
#23 lore raw

Re: [Qemu-devel] [PATCH qemu 6/6] virtio-input: evdev passthrough

Hi,

Quoted 9 lines
Quoted 7 lines

I guess the only thing we might want to here is bail out in case the major version is != 1 (which implies a incompatible change). The major version didn't change so far though, and I think it is highly unlikely that it'll ever happen.

cheers,
Gerd

BTW this device should block migration, right?

Yes. I'll add it.

cheers,
Gerd

author maintainer
#24 lore raw

PATCH 1/1 Add virtio input device specification

In reply to Gerd Hoffmann
---
 content.tex      |   2 +
 virtio-input.tex | 135 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 137 insertions(+)
 create mode 100644 virtio-input.tex
content.tex +2 -0
index c31a99e..196950d 100644
@@ -4887,6 +4887,8 @@ descriptor for the \field{sense_len}, \field{residual},
 \field{status_qualifier}, \field{status}, \field{response} and
 \field{sense} fields.
 
+\input{virtio-input.tex}
+
 \chapter{Reserved Feature Bits}\label{sec:Reserved Feature Bits}
 
 Currently there are three device-independent feature bits defined:
virtio-input.tex new file +135 -0

135 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#25 lore raw

Re: [Qemu-devel] [PATCH spec] Add virtio input device specification

Hi Gerd,

Quoted 26 lines · Gerd Hoffmann
---
 content.tex      |   2 +
 virtio-input.tex | 135 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 137 insertions(+)
 create mode 100644 virtio-input.tex
content.tex +2 -0
index c31a99e..196950d 100644
@@ -4887,6 +4887,8 @@ descriptor for the \field{sense_len}, \field{residual},
 \field{status_qualifier}, \field{status}, \field{response} and
 \field{sense} fields.
 
+\input{virtio-input.tex}
+
 \chapter{Reserved Feature Bits}\label{sec:Reserved Feature Bits}
 
 Currently there are three device-independent feature bits defined:
virtio-input.tex new file +0 -0
new file mode 100644
index 0000000..3c34a52

virtio-input.tex line 4

Quoted 4 lines
virtio-input.tex new file +4 -0
@@ -0,0 +1,135 @@
+\section{Input Device}\label{sec:Device Types / Input Device}
+
+The virtio input device can be used to create virtual human interface
+devices such as keyboards, mice and tables.  It basically sends linux

tablets

Christopher

Signature · 3 lines
Employee of Qualcomm Innovation Center, Inc.
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
hosted by the Linux Foundation.