Skip to contentSkip to conversation

input: add virtio input devices

PATCH New Gerd Hoffmann 1 participant 5 messages Jun 3, 2014

lore
Share & tools
lore
#0 lore raw

cover input: add virtio input devices

Hi,

This series adds virtio input support to qemu. virtio input basically sends linux evdev events over virtio.

Patch #1 is just the pci id. Patch #2 adds the core code as abstract qom class. Patch #3 adds emulated devices on top of that, and patch #4 adds evdev passthrough.

The corresponding linux kernel driver is here:
https://www.kraxel.org/cgit/linux/log/?h=virtio-input

cheers,
Gerd

Gerd Hoffmann (4):
pci: add virtio input pci device id
virtio-input: core code & base class
virtio-input: emulated devices
virtio-input: evdev passthrough

 docs/specs/pci-ids.txt           |   1 +
 hw/input/Makefile.objs           |   6 +
 hw/input/virtio-input-hid.c      | 484 +++++++++++++++++++++++++++++++++++++++
 hw/input/virtio-input-host.c     | 172 ++++++++++++++
 hw/input/virtio-input.c          | 284 +++++++++++++++++++++++
 hw/virtio/virtio-pci.c           | 135 +++++++++++
 hw/virtio/virtio-pci.h           |  37 +++
 include/hw/pci/pci.h             |   1 +
 include/hw/virtio/virtio-input.h | 140 +++++++++++
 include/hw/virtio/virtio.h       |   1 +
 10 files changed, 1261 insertions(+)
 create mode 100644 hw/input/virtio-input-hid.c
 create mode 100644 hw/input/virtio-input-host.c
 create mode 100644 hw/input/virtio-input.c
 create mode 100644 include/hw/virtio/virtio-input.h
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH 1/4 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 8c25ae5..ef75f94 100644
@@ -81,6 +81,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
#2 lore raw

PATCH 2/4 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          | 284 +++++++++++++++++++++++++++++++++++++++
 hw/virtio/virtio-pci.c           |  36 +++++
 hw/virtio/virtio-pci.h           |  14 ++
 include/hw/virtio/virtio-input.h | 106 +++++++++++++++
 include/hw/virtio/virtio.h       |   1 +
 6 files changed, 445 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 +284 -0

284 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 +106 -0

106 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
#3 lore raw

PATCH 3/4 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      | 484 +++++++++++++++++++++++++++++++++++++++
 hw/virtio/virtio-pci.c           |  68 ++++++
 hw/virtio/virtio-pci.h           |  13 ++
 include/hw/virtio/virtio-input.h |  21 ++
 5 files changed, 587 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 +484 -0

484 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 +21 -0
index 718ebfd..ca5bcee 100644
@@ -56,9 +56,23 @@ 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)
+
 typedef struct VirtIOInput VirtIOInput;
 typedef struct VirtIOInputClass VirtIOInputClass;
 typedef struct VirtIOInputConfig VirtIOInputConfig;
+typedef struct VirtIOInputHID VirtIOInputHID;
 
 struct virtio_input_conf {
     char *serial;
@@ -95,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
#4 lore raw

PATCH 4/4 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     | 172 +++++++++++++++++++++++++++++++++++++++
 hw/virtio/virtio-pci.c           |  31 +++++++
 hw/virtio/virtio-pci.h           |  10 +++
 include/hw/virtio/virtio-input.h |  13 +++
 5 files changed, 227 insertions(+)
 create mode 100644 hw/input/virtio-input-host.c
hw/input/Makefile.objs +1 -0
index 0dae710..624ba7e 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-host.o
 endif
 
 obj-$(CONFIG_MILKYMIST) += milkymist-softusb.o
hw/input/virtio-input-host.c new file +172 -0

172 lines not on this page · raw message

hw/virtio/virtio-pci.c +31 -0
index b421c01..b6be1f7 100644
@@ -1541,6 +1541,14 @@ static Property virtio_input_hid_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);
@@ -1572,6 +1580,13 @@ static void virtio_input_hid_pci_class_init(ObjectClass *klass, void *data)
     dc->props = virtio_input_hid_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);
@@ -1593,6 +1608,13 @@ static void virtio_tablet_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,
@@ -1630,6 +1652,14 @@ static const TypeInfo virtio_tablet_pci_info = {
     .instance_init = virtio_tablet_initfn,
 };
 
+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,
@@ -1679,6 +1709,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_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 f615ae6..d0b7f10 100644
@@ -42,6 +42,7 @@ typedef struct VHostSCSIPCI VHostSCSIPCI;
 typedef struct VirtIORngPCI VirtIORngPCI;
 typedef struct VirtIOInputPCI VirtIOInputPCI;
 typedef struct VirtIOInputHIDPCI VirtIOInputHIDPCI;
+typedef struct VirtIOInputHostPCI VirtIOInputHostPCI;
 
 /* virtio-pci-bus */
 
@@ -226,6 +227,15 @@ struct VirtIOInputHIDPCI {
     VirtIOInputHID 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 ca5bcee..eb870bb 100644
@@ -66,6 +66,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_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)
 
@@ -73,6 +79,7 @@ typedef struct VirtIOInput VirtIOInput;
 typedef struct VirtIOInputClass VirtIOInputClass;
 typedef struct VirtIOInputConfig VirtIOInputConfig;
 typedef struct VirtIOInputHID VirtIOInputHID;
+typedef struct VirtIOInputHost VirtIOInputHost;
 
 struct virtio_input_conf {
     char *serial;
@@ -116,6 +123,12 @@ struct VirtIOInputHID {
     int                               ledstate;
 };
 
+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