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 <kra...@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 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