1 /*
2  * This work is licensed under the terms of the GNU GPL, version 2 or
3  * (at your option) any later version.  See the COPYING file in the
4  * top-level directory.
5  */
6 
7 #include "qemu/osdep.h"
8 #include "qemu/error-report.h"
9 #include "qapi/error.h"
10 #include "qemu-common.h"
11 
12 #include "hw/virtio/virtio-input.h"
13 
vhost_input_config_change(struct vhost_dev * dev)14 static int vhost_input_config_change(struct vhost_dev *dev)
15 {
16     error_report("vhost-user-input: unhandled backend config change");
17     return -1;
18 }
19 
20 static const VhostDevConfigOps config_ops = {
21     .vhost_dev_config_notifier = vhost_input_config_change,
22 };
23 
vhost_input_realize(DeviceState * dev,Error ** errp)24 static void vhost_input_realize(DeviceState *dev, Error **errp)
25 {
26     VHostUserInput *vhi = VHOST_USER_INPUT(dev);
27     VirtIOInput *vinput = VIRTIO_INPUT(dev);
28     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
29 
30     vhost_dev_set_config_notifier(&vhi->vhost->dev, &config_ops);
31     vinput->cfg_size = sizeof_field(virtio_input_config, u);
32     if (vhost_user_backend_dev_init(vhi->vhost, vdev, 2, errp) == -1) {
33         return;
34     }
35 }
36 
vhost_input_change_active(VirtIOInput * vinput)37 static void vhost_input_change_active(VirtIOInput *vinput)
38 {
39     VHostUserInput *vhi = VHOST_USER_INPUT(vinput);
40 
41     if (vinput->active) {
42         vhost_user_backend_start(vhi->vhost);
43     } else {
44         vhost_user_backend_stop(vhi->vhost);
45     }
46 }
47 
vhost_input_get_config(VirtIODevice * vdev,uint8_t * config_data)48 static void vhost_input_get_config(VirtIODevice *vdev, uint8_t *config_data)
49 {
50     VirtIOInput *vinput = VIRTIO_INPUT(vdev);
51     VHostUserInput *vhi = VHOST_USER_INPUT(vdev);
52     int ret;
53 
54     memset(config_data, 0, vinput->cfg_size);
55 
56     ret = vhost_dev_get_config(&vhi->vhost->dev, config_data, vinput->cfg_size);
57     if (ret) {
58         error_report("vhost-user-input: get device config space failed");
59         return;
60     }
61 }
62 
vhost_input_set_config(VirtIODevice * vdev,const uint8_t * config_data)63 static void vhost_input_set_config(VirtIODevice *vdev,
64                                    const uint8_t *config_data)
65 {
66     VHostUserInput *vhi = VHOST_USER_INPUT(vdev);
67     int ret;
68 
69     ret = vhost_dev_set_config(&vhi->vhost->dev, config_data,
70                                0, sizeof(virtio_input_config),
71                                VHOST_SET_CONFIG_TYPE_MASTER);
72     if (ret) {
73         error_report("vhost-user-input: set device config space failed");
74         return;
75     }
76 
77     virtio_notify_config(vdev);
78 }
79 
80 static const VMStateDescription vmstate_vhost_input = {
81     .name = "vhost-user-input",
82     .unmigratable = 1,
83 };
84 
vhost_input_class_init(ObjectClass * klass,void * data)85 static void vhost_input_class_init(ObjectClass *klass, void *data)
86 {
87     VirtIOInputClass *vic = VIRTIO_INPUT_CLASS(klass);
88     VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
89     DeviceClass *dc = DEVICE_CLASS(klass);
90 
91     dc->vmsd = &vmstate_vhost_input;
92     vdc->get_config = vhost_input_get_config;
93     vdc->set_config = vhost_input_set_config;
94     vic->realize = vhost_input_realize;
95     vic->change_active = vhost_input_change_active;
96 }
97 
vhost_input_init(Object * obj)98 static void vhost_input_init(Object *obj)
99 {
100     VHostUserInput *vhi = VHOST_USER_INPUT(obj);
101 
102     vhi->vhost = VHOST_USER_BACKEND(object_new(TYPE_VHOST_USER_BACKEND));
103     object_property_add_alias(obj, "chardev",
104                               OBJECT(vhi->vhost), "chardev", &error_abort);
105 }
106 
vhost_input_finalize(Object * obj)107 static void vhost_input_finalize(Object *obj)
108 {
109     VHostUserInput *vhi = VHOST_USER_INPUT(obj);
110 
111     object_unref(OBJECT(vhi->vhost));
112 }
113 
114 static const TypeInfo vhost_input_info = {
115     .name          = TYPE_VHOST_USER_INPUT,
116     .parent        = TYPE_VIRTIO_INPUT,
117     .instance_size = sizeof(VHostUserInput),
118     .instance_init = vhost_input_init,
119     .instance_finalize = vhost_input_finalize,
120     .class_init    = vhost_input_class_init,
121 };
122 
vhost_input_register_types(void)123 static void vhost_input_register_types(void)
124 {
125     type_register_static(&vhost_input_info);
126 }
127 
128 type_init(vhost_input_register_types)
129