| 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 "qapi/error.h" |
| 9 | #include "qemu/module.h" |
| 10 | #include "qemu/sockets.h" |
| 11 | |
| 12 | #include "hw/virtio/virtio.h" |
| 13 | #include "hw/core/qdev-properties.h" |
| 14 | #include "hw/virtio/virtio-input.h" |
| 15 | |
| 16 | #include <sys/ioctl.h> |
| 17 | #include "standard-headers/linux/input.h" |
| 18 | |
| 19 | /* ----------------------------------------------------------------- */ |
| 20 | |
| 21 | static struct virtio_input_config virtio_input_host_config[] = { |
| 22 | { /* empty list */ }, |
| 23 | }; |
| 24 | |
| 25 | static void virtio_input_host_event(void *opaque) |
| 26 | { |
| 27 | VirtIOInputHost *vih = opaque; |
| 28 | VirtIOInput *vinput = VIRTIO_INPUT(vih); |
| 29 | struct virtio_input_event virtio; |
| 30 | struct input_event evdev; |
| 31 | int rc; |
| 32 | |
| 33 | for (;;) { |
| 34 | rc = read(vih->fd, &evdev, sizeof(evdev)); |
| 35 | if (rc != sizeof(evdev)) { |
| 36 | break; |
| 37 | } |
| 38 | |
| 39 | virtio.type = cpu_to_le16(evdev.type); |
| 40 | virtio.code = cpu_to_le16(evdev.code); |
| 41 | virtio.value = cpu_to_le32(evdev.value); |
| 42 | virtio_input_send(vinput, &virtio); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | static void virtio_input_bits_config(VirtIOInputHost *vih, |
| 47 | int type, int count) |
| 48 | { |
| 49 | virtio_input_config bits; |
| 50 | int rc, i, size = 0; |
| 51 | |
| 52 | memset(&bits, 0, sizeof(bits)); |
| 53 | rc = ioctl(vih->fd, EVIOCGBIT(type, count/8), bits.u.bitmap); |
| 54 | if (rc < 0) { |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | for (i = 0; i < count/8; i++) { |
| 59 | if (bits.u.bitmap[i]) { |
| 60 | size = i+1; |
| 61 | } |
| 62 | } |
| 63 | if (size == 0) { |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | bits.select = VIRTIO_INPUT_CFG_EV_BITS; |
| 68 | bits.subsel = type; |
| 69 | bits.size = size; |
| 70 | virtio_input_add_config(VIRTIO_INPUT(vih), &bits); |
| 71 | } |
| 72 | |
| 73 | static void virtio_input_abs_config(VirtIOInputHost *vih, int axis) |
| 74 | { |
| 75 | virtio_input_config config; |
| 76 | struct input_absinfo absinfo; |
| 77 | int rc; |
| 78 | |
| 79 | rc = ioctl(vih->fd, EVIOCGABS(axis), &absinfo); |
| 80 | if (rc < 0) { |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | memset(&config, 0, sizeof(config)); |
| 85 | config.select = VIRTIO_INPUT_CFG_ABS_INFO; |
| 86 | config.subsel = axis; |
| 87 | config.size = sizeof(virtio_input_absinfo); |
| 88 | |
| 89 | config.u.abs.min = cpu_to_le32(absinfo.minimum); |
| 90 | config.u.abs.max = cpu_to_le32(absinfo.maximum); |
| 91 | config.u.abs.fuzz = cpu_to_le32(absinfo.fuzz); |
| 92 | config.u.abs.flat = cpu_to_le32(absinfo.flat); |
| 93 | config.u.abs.res = cpu_to_le32(absinfo.resolution); |
| 94 | |
| 95 | virtio_input_add_config(VIRTIO_INPUT(vih), &config); |
| 96 | } |
| 97 | |
| 98 | static void virtio_input_host_realize(DeviceState *dev, Error **errp) |
| 99 | { |
| 100 | VirtIOInputHost *vih = VIRTIO_INPUT_HOST(dev); |
| 101 | VirtIOInput *vinput = VIRTIO_INPUT(dev); |
| 102 | virtio_input_config id, *abs; |
| 103 | struct input_id ids; |
| 104 | int rc, ver, i, axis; |
| 105 | uint8_t byte; |
| 106 | |
| 107 | if (!vih->evdev) { |
| 108 | error_setg(errp, "evdev property is required"); |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | vih->fd = open(vih->evdev, O_RDWR); |
| 113 | if (vih->fd < 0) { |
| 114 | error_setg_file_open(errp, errno, vih->evdev); |
| 115 | return; |
| 116 | } |
| 117 | if (!qemu_set_blocking(vih->fd, false, errp)) { |
| 118 | goto err_close; |
| 119 | } |
| 120 | |
| 121 | rc = ioctl(vih->fd, EVIOCGVERSION, &ver); |
| 122 | if (rc < 0) { |
| 123 | error_setg(errp, "%s: is not an evdev device", vih->evdev); |
| 124 | goto err_close; |
| 125 | } |
| 126 | |
| 127 | rc = ioctl(vih->fd, EVIOCGRAB, 1); |
| 128 | if (rc < 0) { |
| 129 | error_setg_errno(errp, errno, "%s: failed to get exclusive access", |
| 130 | vih->evdev); |
| 131 | goto err_close; |
| 132 | } |
| 133 | |
| 134 | memset(&id, 0, sizeof(id)); |
| 135 | ioctl(vih->fd, EVIOCGNAME(sizeof(id.u.string)-1), id.u.string); |
| 136 | id.select = VIRTIO_INPUT_CFG_ID_NAME; |
| 137 | id.size = strlen(id.u.string); |
| 138 | virtio_input_add_config(vinput, &id); |
| 139 | |
| 140 | if (ioctl(vih->fd, EVIOCGID, &ids) == 0) { |
| 141 | memset(&id, 0, sizeof(id)); |
| 142 | id.select = VIRTIO_INPUT_CFG_ID_DEVIDS; |
| 143 | id.size = sizeof(struct virtio_input_devids); |
| 144 | id.u.ids.bustype = cpu_to_le16(ids.bustype); |
| 145 | id.u.ids.vendor = cpu_to_le16(ids.vendor); |
| 146 | id.u.ids.product = cpu_to_le16(ids.product); |
| 147 | id.u.ids.version = cpu_to_le16(ids.version); |
| 148 | virtio_input_add_config(vinput, &id); |
| 149 | } |
| 150 | |
| 151 | virtio_input_bits_config(vih, EV_KEY, KEY_CNT); |
| 152 | virtio_input_bits_config(vih, EV_REL, REL_CNT); |
| 153 | virtio_input_bits_config(vih, EV_ABS, ABS_CNT); |
| 154 | virtio_input_bits_config(vih, EV_MSC, MSC_CNT); |
| 155 | virtio_input_bits_config(vih, EV_SW, SW_CNT); |
| 156 | virtio_input_bits_config(vih, EV_LED, LED_CNT); |
| 157 | |
| 158 | abs = virtio_input_find_config(VIRTIO_INPUT(vih), |
| 159 | VIRTIO_INPUT_CFG_EV_BITS, EV_ABS); |
| 160 | if (abs) { |
| 161 | for (i = 0; i < abs->size; i++) { |
| 162 | byte = abs->u.bitmap[i]; |
| 163 | axis = 8 * i; |
| 164 | while (byte) { |
| 165 | if (byte & 1) { |
| 166 | virtio_input_abs_config(vih, axis); |
| 167 | } |
| 168 | axis++; |
| 169 | byte >>= 1; |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | qemu_set_fd_handler(vih->fd, virtio_input_host_event, NULL, vih); |
| 175 | return; |
| 176 | |
| 177 | err_close: |
| 178 | close(vih->fd); |
| 179 | vih->fd = -1; |
| 180 | } |
| 181 | |
| 182 | static void virtio_input_host_unrealize(DeviceState *dev) |
| 183 | { |
| 184 | VirtIOInputHost *vih = VIRTIO_INPUT_HOST(dev); |
| 185 | |
| 186 | if (vih->fd > 0) { |
| 187 | qemu_set_fd_handler(vih->fd, NULL, NULL, NULL); |
| 188 | close(vih->fd); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | static void virtio_input_host_handle_status(VirtIOInput *vinput, |
| 193 | virtio_input_event *event) |
| 194 | { |
| 195 | VirtIOInputHost *vih = VIRTIO_INPUT_HOST(vinput); |
| 196 | struct input_event evdev; |
| 197 | struct timeval tval; |
| 198 | int rc; |
| 199 | |
| 200 | if (gettimeofday(&tval, NULL)) { |
| 201 | perror("virtio_input_host_handle_status: gettimeofday"); |
| 202 | return; |
| 203 | } |
| 204 | |
| 205 | evdev.input_event_sec = tval.tv_sec; |
| 206 | evdev.input_event_usec = tval.tv_usec; |
| 207 | evdev.type = le16_to_cpu(event->type); |
| 208 | evdev.code = le16_to_cpu(event->code); |
| 209 | evdev.value = le32_to_cpu(event->value); |
| 210 | |
| 211 | rc = write(vih->fd, &evdev, sizeof(evdev)); |
| 212 | if (rc == -1) { |
| 213 | perror("virtio_input_host_handle_status: write"); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | static const VMStateDescription vmstate_virtio_input_host = { |
| 218 | .name = "virtio-input-host", |
| 219 | .unmigratable = 1, |
| 220 | }; |
| 221 | |
| 222 | static const Property virtio_input_host_properties[] = { |
| 223 | DEFINE_PROP_STRING("evdev", VirtIOInputHost, evdev), |
| 224 | }; |
| 225 | |
| 226 | static void virtio_input_host_class_init(ObjectClass *klass, const void *data) |
| 227 | { |
| 228 | VirtIOInputClass *vic = VIRTIO_INPUT_CLASS(klass); |
| 229 | DeviceClass *dc = DEVICE_CLASS(klass); |
| 230 | |
| 231 | dc->vmsd = &vmstate_virtio_input_host; |
| 232 | device_class_set_props(dc, virtio_input_host_properties); |
| 233 | vic->realize = virtio_input_host_realize; |
| 234 | vic->unrealize = virtio_input_host_unrealize; |
| 235 | vic->handle_status = virtio_input_host_handle_status; |
| 236 | } |
| 237 | |
| 238 | static void virtio_input_host_init(Object *obj) |
| 239 | { |
| 240 | VirtIOInput *vinput = VIRTIO_INPUT(obj); |
| 241 | |
| 242 | virtio_input_init_config(vinput, virtio_input_host_config); |
| 243 | } |
| 244 | |
| 245 | static const TypeInfo virtio_input_host_info = { |
| 246 | .name = TYPE_VIRTIO_INPUT_HOST, |
| 247 | .parent = TYPE_VIRTIO_INPUT, |
| 248 | .instance_size = sizeof(VirtIOInputHost), |
| 249 | .instance_init = virtio_input_host_init, |
| 250 | .class_init = virtio_input_host_class_init, |
| 251 | }; |
| 252 | |
| 253 | /* ----------------------------------------------------------------- */ |
| 254 | |
| 255 | static void virtio_register_types(void) |
| 256 | { |
| 257 | type_register_static(&virtio_input_host_info); |
| 258 | } |
| 259 | |
| 260 | type_init(virtio_register_types) |