Patchwork [qemu,5/6] virtio-input: control device

login
register
mail settings
Submitter Gerd Hoffmann
Date April 10, 2014, 9:07 a.m.
Message ID <1397120874-17166-8-git-send-email-kraxel@redhat.com>
Download mbox | patch
Permalink /patch/338065/
State New
Headers show

Comments

Gerd Hoffmann - April 10, 2014, 9:07 a.m.
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.

Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
---
 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
Michael S. Tsirkin - April 10, 2014, 11:05 a.m.
On Thu, Apr 10, 2014 at 11:07:53AM +0200, Gerd Hoffmann wrote:
> 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.
> 
> Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>

Why not use a keyboard device for this?

> ---
>  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
> 
> diff --git a/hw/input/Makefile.objs b/hw/input/Makefile.objs
> index 0dae710..0179154 100644
> --- a/hw/input/Makefile.objs
> +++ b/hw/input/Makefile.objs
> @@ -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
> diff --git a/hw/input/virtio-input-control.c b/hw/input/virtio-input-control.c
> new file mode 100644
> index 0000000..3e439e6
> --- /dev/null
> +++ b/hw/input/virtio-input-control.c
> @@ -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)
> diff --git a/hw/virtio/virtio-pci.c b/hw/virtio/virtio-pci.c
> index b421c01..9446d45 100644
> --- a/hw/virtio/virtio-pci.c
> +++ b/hw/virtio/virtio-pci.c
> @@ -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
> diff --git a/hw/virtio/virtio-pci.h b/hw/virtio/virtio-pci.h
> index f615ae6..0c26e64 100644
> --- a/hw/virtio/virtio-pci.h
> +++ b/hw/virtio/virtio-pci.h
> @@ -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
>  
> diff --git a/include/hw/virtio/virtio-input.h b/include/hw/virtio/virtio-input.h
> index 4b819c3..d753834 100644
> --- a/include/hw/virtio/virtio-input.h
> +++ b/include/hw/virtio/virtio-input.h
> @@ -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);
> -- 
> 1.8.3.1
Gerd Hoffmann - April 10, 2014, 12:10 p.m.
On Do, 2014-04-10 at 14:05 +0300, Michael S. Tsirkin wrote:
> On Thu, Apr 10, 2014 at 11:07:53AM +0200, Gerd Hoffmann wrote:
> > 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.
> > 
> > Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
> 
> 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
Michael S. Tsirkin - April 10, 2014, 3:20 p.m.
On Thu, Apr 10, 2014 at 02:10:20PM +0200, Gerd Hoffmann wrote:
> On Do, 2014-04-10 at 14:05 +0300, Michael S. Tsirkin wrote:
> > On Thu, Apr 10, 2014 at 11:07:53AM +0200, Gerd Hoffmann wrote:
> > > 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.
> > > 
> > > Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
> > 
> > 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
>

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?
Gerd Hoffmann - April 11, 2014, 8:25 a.m.
On Do, 2014-04-10 at 18:20 +0300, Michael S. Tsirkin wrote:
> On Thu, Apr 10, 2014 at 02:10:20PM +0200, Gerd Hoffmann wrote:
> > On Do, 2014-04-10 at 14:05 +0300, Michael S. Tsirkin wrote:
> > > On Thu, Apr 10, 2014 at 11:07:53AM +0200, Gerd Hoffmann wrote:
> > > > 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.
> > > > 
> > > > Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
> > > 
> > > 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
> >
> 
> 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
Michael S. Tsirkin - April 13, 2014, 5:14 p.m.
On Fri, Apr 11, 2014 at 10:25:59AM +0200, Gerd Hoffmann wrote:
> On Do, 2014-04-10 at 18:20 +0300, Michael S. Tsirkin wrote:
> > On Thu, Apr 10, 2014 at 02:10:20PM +0200, Gerd Hoffmann wrote:
> > > On Do, 2014-04-10 at 14:05 +0300, Michael S. Tsirkin wrote:
> > > > On Thu, Apr 10, 2014 at 11:07:53AM +0200, Gerd Hoffmann wrote:
> > > > > 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.
> > > > > 
> > > > > Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
> > > > 
> > > > 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
> > >
> > 
> > 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

Or forward volume keys to guest through the virtio keyboard - then we
don't need to emulate a volume wheel.
Gerd Hoffmann - April 14, 2014, 8:51 a.m.
Hi,

> > 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).

> Or forward volume keys to guest through the virtio keyboard - then we
> don't need to emulate a volume wheel.

Isn't going to fly.  Using volume up/down keys we can't says "volume is
at 42% now" to the guest.

cheers,
  Gerd

Patch

diff --git a/hw/input/Makefile.objs b/hw/input/Makefile.objs
index 0dae710..0179154 100644
--- a/hw/input/Makefile.objs
+++ b/hw/input/Makefile.objs
@@ -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
diff --git a/hw/input/virtio-input-control.c b/hw/input/virtio-input-control.c
new file mode 100644
index 0000000..3e439e6
--- /dev/null
+++ b/hw/input/virtio-input-control.c
@@ -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)
diff --git a/hw/virtio/virtio-pci.c b/hw/virtio/virtio-pci.c
index b421c01..9446d45 100644
--- a/hw/virtio/virtio-pci.c
+++ b/hw/virtio/virtio-pci.c
@@ -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
diff --git a/hw/virtio/virtio-pci.h b/hw/virtio/virtio-pci.h
index f615ae6..0c26e64 100644
--- a/hw/virtio/virtio-pci.h
+++ b/hw/virtio/virtio-pci.h
@@ -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
 
diff --git a/include/hw/virtio/virtio-input.h b/include/hw/virtio/virtio-input.h
index 4b819c3..d753834 100644
--- a/include/hw/virtio/virtio-input.h
+++ b/include/hw/virtio/virtio-input.h
@@ -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);