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-common.h" 9 #include "qemu/sockets.h" 10 11 #include "hw/qdev.h" 12 #include "hw/virtio/virtio.h" 13 #include "hw/virtio/virtio-input.h" 14 15 #include <sys/ioctl.h> 16 #include "standard-headers/linux/input.h" 17 18 /* ----------------------------------------------------------------- */ 19 20 static struct virtio_input_config virtio_input_host_config[] = { 21 { /* empty list */ }, 22 }; 23 24 static void virtio_input_host_event(void *opaque) 25 { 26 VirtIOInputHost *vih = opaque; 27 VirtIOInput *vinput = VIRTIO_INPUT(vih); 28 struct virtio_input_event virtio; 29 struct input_event evdev; 30 int rc; 31 32 for (;;) { 33 rc = read(vih->fd, &evdev, sizeof(evdev)); 34 if (rc != sizeof(evdev)) { 35 break; 36 } 37 38 virtio.type = cpu_to_le16(evdev.type); 39 virtio.code = cpu_to_le16(evdev.code); 40 virtio.value = cpu_to_le32(evdev.value); 41 virtio_input_send(vinput, &virtio); 42 } 43 } 44 45 static void virtio_input_bits_config(VirtIOInputHost *vih, 46 int type, int count) 47 { 48 virtio_input_config bits; 49 int rc, i, size = 0; 50 51 memset(&bits, 0, sizeof(bits)); 52 rc = ioctl(vih->fd, EVIOCGBIT(type, count/8), bits.u.bitmap); 53 if (rc < 0) { 54 return; 55 } 56 57 for (i = 0; i < count/8; i++) { 58 if (bits.u.bitmap[i]) { 59 size = i+1; 60 } 61 } 62 if (size == 0) { 63 return; 64 } 65 66 bits.select = VIRTIO_INPUT_CFG_EV_BITS; 67 bits.subsel = type; 68 bits.size = size; 69 virtio_input_add_config(VIRTIO_INPUT(vih), &bits); 70 } 71 72 static void virtio_input_host_realize(DeviceState *dev, Error **errp) 73 { 74 VirtIOInputHost *vih = VIRTIO_INPUT_HOST(dev); 75 VirtIOInput *vinput = VIRTIO_INPUT(dev); 76 virtio_input_config id; 77 struct input_id ids; 78 int rc, ver; 79 80 if (!vih->evdev) { 81 error_setg(errp, "evdev property is required"); 82 return; 83 } 84 85 vih->fd = open(vih->evdev, O_RDWR); 86 if (vih->fd < 0) { 87 error_setg_file_open(errp, errno, vih->evdev); 88 return; 89 } 90 qemu_set_nonblock(vih->fd); 91 92 rc = ioctl(vih->fd, EVIOCGVERSION, &ver); 93 if (rc < 0) { 94 error_setg(errp, "%s: is not an evdev device", vih->evdev); 95 goto err_close; 96 } 97 98 rc = ioctl(vih->fd, EVIOCGRAB, 1); 99 if (rc < 0) { 100 error_setg_errno(errp, errno, "%s: failed to get exclusive access", 101 vih->evdev); 102 goto err_close; 103 } 104 105 memset(&id, 0, sizeof(id)); 106 ioctl(vih->fd, EVIOCGNAME(sizeof(id.u.string)-1), id.u.string); 107 id.select = VIRTIO_INPUT_CFG_ID_NAME; 108 id.size = strlen(id.u.string); 109 virtio_input_add_config(vinput, &id); 110 111 if (ioctl(vih->fd, EVIOCGID, &ids) == 0) { 112 memset(&id, 0, sizeof(id)); 113 id.select = VIRTIO_INPUT_CFG_ID_DEVIDS; 114 id.size = sizeof(struct virtio_input_devids); 115 id.u.ids.bustype = cpu_to_le16(ids.bustype); 116 id.u.ids.vendor = cpu_to_le16(ids.vendor); 117 id.u.ids.product = cpu_to_le16(ids.product); 118 id.u.ids.version = cpu_to_le16(ids.version); 119 virtio_input_add_config(vinput, &id); 120 } 121 122 virtio_input_bits_config(vih, EV_KEY, KEY_CNT); 123 virtio_input_bits_config(vih, EV_REL, REL_CNT); 124 virtio_input_bits_config(vih, EV_ABS, ABS_CNT); 125 virtio_input_bits_config(vih, EV_MSC, MSC_CNT); 126 virtio_input_bits_config(vih, EV_SW, SW_CNT); 127 128 qemu_set_fd_handler(vih->fd, virtio_input_host_event, NULL, vih); 129 return; 130 131 err_close: 132 close(vih->fd); 133 vih->fd = -1; 134 return; 135 } 136 137 static void virtio_input_host_unrealize(DeviceState *dev, Error **errp) 138 { 139 VirtIOInputHost *vih = VIRTIO_INPUT_HOST(dev); 140 141 if (vih->fd > 0) { 142 qemu_set_fd_handler(vih->fd, NULL, NULL, NULL); 143 close(vih->fd); 144 } 145 } 146 147 static const VMStateDescription vmstate_virtio_input_host = { 148 .name = "virtio-input-host", 149 .unmigratable = 1, 150 }; 151 152 static Property virtio_input_host_properties[] = { 153 DEFINE_PROP_STRING("evdev", VirtIOInputHost, evdev), 154 DEFINE_PROP_END_OF_LIST(), 155 }; 156 157 static void virtio_input_host_class_init(ObjectClass *klass, void *data) 158 { 159 VirtIOInputClass *vic = VIRTIO_INPUT_CLASS(klass); 160 DeviceClass *dc = DEVICE_CLASS(klass); 161 162 dc->vmsd = &vmstate_virtio_input_host; 163 dc->props = virtio_input_host_properties; 164 vic->realize = virtio_input_host_realize; 165 vic->unrealize = virtio_input_host_unrealize; 166 } 167 168 static void virtio_input_host_init(Object *obj) 169 { 170 VirtIOInput *vinput = VIRTIO_INPUT(obj); 171 172 virtio_input_init_config(vinput, virtio_input_host_config); 173 } 174 175 static const TypeInfo virtio_input_host_info = { 176 .name = TYPE_VIRTIO_INPUT_HOST, 177 .parent = TYPE_VIRTIO_INPUT, 178 .instance_size = sizeof(VirtIOInputHost), 179 .instance_init = virtio_input_host_init, 180 .class_init = virtio_input_host_class_init, 181 }; 182 183 /* ----------------------------------------------------------------- */ 184 185 static void virtio_register_types(void) 186 { 187 type_register_static(&virtio_input_host_info); 188 } 189 190 type_init(virtio_register_types) 191