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/iov.h" 8 9 #include "hw/qdev.h" 10 #include "hw/virtio/virtio.h" 11 #include "hw/virtio/virtio-input.h" 12 13 #undef CONFIG_CURSES 14 #include "ui/console.h" 15 16 #include "standard-headers/linux/input.h" 17 18 #define VIRTIO_ID_NAME_KEYBOARD "QEMU Virtio Keyboard" 19 #define VIRTIO_ID_NAME_MOUSE "QEMU Virtio Mouse" 20 #define VIRTIO_ID_NAME_TABLET "QEMU Virtio Tablet" 21 22 /* ----------------------------------------------------------------- */ 23 24 static const unsigned int keymap_qcode[Q_KEY_CODE__MAX] = { 25 [Q_KEY_CODE_ESC] = KEY_ESC, 26 [Q_KEY_CODE_1] = KEY_1, 27 [Q_KEY_CODE_2] = KEY_2, 28 [Q_KEY_CODE_3] = KEY_3, 29 [Q_KEY_CODE_4] = KEY_4, 30 [Q_KEY_CODE_5] = KEY_5, 31 [Q_KEY_CODE_6] = KEY_6, 32 [Q_KEY_CODE_7] = KEY_7, 33 [Q_KEY_CODE_8] = KEY_8, 34 [Q_KEY_CODE_9] = KEY_9, 35 [Q_KEY_CODE_0] = KEY_0, 36 [Q_KEY_CODE_MINUS] = KEY_MINUS, 37 [Q_KEY_CODE_EQUAL] = KEY_EQUAL, 38 [Q_KEY_CODE_BACKSPACE] = KEY_BACKSPACE, 39 40 [Q_KEY_CODE_TAB] = KEY_TAB, 41 [Q_KEY_CODE_Q] = KEY_Q, 42 [Q_KEY_CODE_W] = KEY_W, 43 [Q_KEY_CODE_E] = KEY_E, 44 [Q_KEY_CODE_R] = KEY_R, 45 [Q_KEY_CODE_T] = KEY_T, 46 [Q_KEY_CODE_Y] = KEY_Y, 47 [Q_KEY_CODE_U] = KEY_U, 48 [Q_KEY_CODE_I] = KEY_I, 49 [Q_KEY_CODE_O] = KEY_O, 50 [Q_KEY_CODE_P] = KEY_P, 51 [Q_KEY_CODE_BRACKET_LEFT] = KEY_LEFTBRACE, 52 [Q_KEY_CODE_BRACKET_RIGHT] = KEY_RIGHTBRACE, 53 [Q_KEY_CODE_RET] = KEY_ENTER, 54 55 [Q_KEY_CODE_CTRL] = KEY_LEFTCTRL, 56 [Q_KEY_CODE_A] = KEY_A, 57 [Q_KEY_CODE_S] = KEY_S, 58 [Q_KEY_CODE_D] = KEY_D, 59 [Q_KEY_CODE_F] = KEY_F, 60 [Q_KEY_CODE_G] = KEY_G, 61 [Q_KEY_CODE_H] = KEY_H, 62 [Q_KEY_CODE_J] = KEY_J, 63 [Q_KEY_CODE_K] = KEY_K, 64 [Q_KEY_CODE_L] = KEY_L, 65 [Q_KEY_CODE_SEMICOLON] = KEY_SEMICOLON, 66 [Q_KEY_CODE_APOSTROPHE] = KEY_APOSTROPHE, 67 [Q_KEY_CODE_GRAVE_ACCENT] = KEY_GRAVE, 68 69 [Q_KEY_CODE_SHIFT] = KEY_LEFTSHIFT, 70 [Q_KEY_CODE_BACKSLASH] = KEY_BACKSLASH, 71 [Q_KEY_CODE_LESS] = KEY_102ND, 72 [Q_KEY_CODE_Z] = KEY_Z, 73 [Q_KEY_CODE_X] = KEY_X, 74 [Q_KEY_CODE_C] = KEY_C, 75 [Q_KEY_CODE_V] = KEY_V, 76 [Q_KEY_CODE_B] = KEY_B, 77 [Q_KEY_CODE_N] = KEY_N, 78 [Q_KEY_CODE_M] = KEY_M, 79 [Q_KEY_CODE_COMMA] = KEY_COMMA, 80 [Q_KEY_CODE_DOT] = KEY_DOT, 81 [Q_KEY_CODE_SLASH] = KEY_SLASH, 82 [Q_KEY_CODE_SHIFT_R] = KEY_RIGHTSHIFT, 83 84 [Q_KEY_CODE_ALT] = KEY_LEFTALT, 85 [Q_KEY_CODE_SPC] = KEY_SPACE, 86 [Q_KEY_CODE_CAPS_LOCK] = KEY_CAPSLOCK, 87 88 [Q_KEY_CODE_F1] = KEY_F1, 89 [Q_KEY_CODE_F2] = KEY_F2, 90 [Q_KEY_CODE_F3] = KEY_F3, 91 [Q_KEY_CODE_F4] = KEY_F4, 92 [Q_KEY_CODE_F5] = KEY_F5, 93 [Q_KEY_CODE_F6] = KEY_F6, 94 [Q_KEY_CODE_F7] = KEY_F7, 95 [Q_KEY_CODE_F8] = KEY_F8, 96 [Q_KEY_CODE_F9] = KEY_F9, 97 [Q_KEY_CODE_F10] = KEY_F10, 98 [Q_KEY_CODE_NUM_LOCK] = KEY_NUMLOCK, 99 [Q_KEY_CODE_SCROLL_LOCK] = KEY_SCROLLLOCK, 100 101 [Q_KEY_CODE_KP_0] = KEY_KP0, 102 [Q_KEY_CODE_KP_1] = KEY_KP1, 103 [Q_KEY_CODE_KP_2] = KEY_KP2, 104 [Q_KEY_CODE_KP_3] = KEY_KP3, 105 [Q_KEY_CODE_KP_4] = KEY_KP4, 106 [Q_KEY_CODE_KP_5] = KEY_KP5, 107 [Q_KEY_CODE_KP_6] = KEY_KP6, 108 [Q_KEY_CODE_KP_7] = KEY_KP7, 109 [Q_KEY_CODE_KP_8] = KEY_KP8, 110 [Q_KEY_CODE_KP_9] = KEY_KP9, 111 [Q_KEY_CODE_KP_SUBTRACT] = KEY_KPMINUS, 112 [Q_KEY_CODE_KP_ADD] = KEY_KPPLUS, 113 [Q_KEY_CODE_KP_DECIMAL] = KEY_KPDOT, 114 [Q_KEY_CODE_KP_ENTER] = KEY_KPENTER, 115 [Q_KEY_CODE_KP_DIVIDE] = KEY_KPSLASH, 116 [Q_KEY_CODE_KP_MULTIPLY] = KEY_KPASTERISK, 117 118 [Q_KEY_CODE_F11] = KEY_F11, 119 [Q_KEY_CODE_F12] = KEY_F12, 120 121 [Q_KEY_CODE_CTRL_R] = KEY_RIGHTCTRL, 122 [Q_KEY_CODE_SYSRQ] = KEY_SYSRQ, 123 [Q_KEY_CODE_ALT_R] = KEY_RIGHTALT, 124 125 [Q_KEY_CODE_HOME] = KEY_HOME, 126 [Q_KEY_CODE_UP] = KEY_UP, 127 [Q_KEY_CODE_PGUP] = KEY_PAGEUP, 128 [Q_KEY_CODE_LEFT] = KEY_LEFT, 129 [Q_KEY_CODE_RIGHT] = KEY_RIGHT, 130 [Q_KEY_CODE_END] = KEY_END, 131 [Q_KEY_CODE_DOWN] = KEY_DOWN, 132 [Q_KEY_CODE_PGDN] = KEY_PAGEDOWN, 133 [Q_KEY_CODE_INSERT] = KEY_INSERT, 134 [Q_KEY_CODE_DELETE] = KEY_DELETE, 135 136 [Q_KEY_CODE_META_L] = KEY_LEFTMETA, 137 [Q_KEY_CODE_META_R] = KEY_RIGHTMETA, 138 [Q_KEY_CODE_MENU] = KEY_MENU, 139 }; 140 141 static const unsigned int keymap_button[INPUT_BUTTON__MAX] = { 142 [INPUT_BUTTON_LEFT] = BTN_LEFT, 143 [INPUT_BUTTON_RIGHT] = BTN_RIGHT, 144 [INPUT_BUTTON_MIDDLE] = BTN_MIDDLE, 145 [INPUT_BUTTON_WHEELUP] = BTN_GEAR_UP, 146 [INPUT_BUTTON_WHEELDOWN] = BTN_GEAR_DOWN, 147 }; 148 149 static const unsigned int axismap_rel[INPUT_AXIS__MAX] = { 150 [INPUT_AXIS_X] = REL_X, 151 [INPUT_AXIS_Y] = REL_Y, 152 }; 153 154 static const unsigned int axismap_abs[INPUT_AXIS__MAX] = { 155 [INPUT_AXIS_X] = ABS_X, 156 [INPUT_AXIS_Y] = ABS_Y, 157 }; 158 159 /* ----------------------------------------------------------------- */ 160 161 static void virtio_input_key_config(VirtIOInput *vinput, 162 const unsigned int *keymap, 163 size_t mapsize) 164 { 165 virtio_input_config keys; 166 int i, bit, byte, bmax = 0; 167 168 memset(&keys, 0, sizeof(keys)); 169 for (i = 0; i < mapsize; i++) { 170 bit = keymap[i]; 171 if (!bit) { 172 continue; 173 } 174 byte = bit / 8; 175 bit = bit % 8; 176 keys.u.bitmap[byte] |= (1 << bit); 177 if (bmax < byte+1) { 178 bmax = byte+1; 179 } 180 } 181 keys.select = VIRTIO_INPUT_CFG_EV_BITS; 182 keys.subsel = EV_KEY; 183 keys.size = bmax; 184 virtio_input_add_config(vinput, &keys); 185 } 186 187 static void virtio_input_handle_event(DeviceState *dev, QemuConsole *src, 188 InputEvent *evt) 189 { 190 VirtIOInput *vinput = VIRTIO_INPUT(dev); 191 virtio_input_event event; 192 int qcode; 193 194 switch (evt->type) { 195 case INPUT_EVENT_KIND_KEY: 196 qcode = qemu_input_key_value_to_qcode(evt->u.key->key); 197 if (qcode && keymap_qcode[qcode]) { 198 event.type = cpu_to_le16(EV_KEY); 199 event.code = cpu_to_le16(keymap_qcode[qcode]); 200 event.value = cpu_to_le32(evt->u.key->down ? 1 : 0); 201 virtio_input_send(vinput, &event); 202 } else { 203 if (evt->u.key->down) { 204 fprintf(stderr, "%s: unmapped key: %d [%s]\n", __func__, 205 qcode, QKeyCode_lookup[qcode]); 206 } 207 } 208 break; 209 case INPUT_EVENT_KIND_BTN: 210 if (keymap_button[evt->u.btn->button]) { 211 event.type = cpu_to_le16(EV_KEY); 212 event.code = cpu_to_le16(keymap_button[evt->u.btn->button]); 213 event.value = cpu_to_le32(evt->u.btn->down ? 1 : 0); 214 virtio_input_send(vinput, &event); 215 } else { 216 if (evt->u.btn->down) { 217 fprintf(stderr, "%s: unmapped button: %d [%s]\n", __func__, 218 evt->u.btn->button, 219 InputButton_lookup[evt->u.btn->button]); 220 } 221 } 222 break; 223 case INPUT_EVENT_KIND_REL: 224 event.type = cpu_to_le16(EV_REL); 225 event.code = cpu_to_le16(axismap_rel[evt->u.rel->axis]); 226 event.value = cpu_to_le32(evt->u.rel->value); 227 virtio_input_send(vinput, &event); 228 break; 229 case INPUT_EVENT_KIND_ABS: 230 event.type = cpu_to_le16(EV_ABS); 231 event.code = cpu_to_le16(axismap_abs[evt->u.abs->axis]); 232 event.value = cpu_to_le32(evt->u.abs->value); 233 virtio_input_send(vinput, &event); 234 break; 235 default: 236 /* keep gcc happy */ 237 break; 238 } 239 } 240 241 static void virtio_input_handle_sync(DeviceState *dev) 242 { 243 VirtIOInput *vinput = VIRTIO_INPUT(dev); 244 virtio_input_event event = { 245 .type = cpu_to_le16(EV_SYN), 246 .code = cpu_to_le16(SYN_REPORT), 247 .value = 0, 248 }; 249 250 virtio_input_send(vinput, &event); 251 } 252 253 static void virtio_input_hid_realize(DeviceState *dev, Error **errp) 254 { 255 VirtIOInputHID *vhid = VIRTIO_INPUT_HID(dev); 256 257 vhid->hs = qemu_input_handler_register(dev, vhid->handler); 258 if (vhid->display && vhid->hs) { 259 qemu_input_handler_bind(vhid->hs, vhid->display, vhid->head, NULL); 260 } 261 } 262 263 static void virtio_input_hid_unrealize(DeviceState *dev, Error **errp) 264 { 265 VirtIOInputHID *vhid = VIRTIO_INPUT_HID(dev); 266 qemu_input_handler_unregister(vhid->hs); 267 } 268 269 static void virtio_input_hid_change_active(VirtIOInput *vinput) 270 { 271 VirtIOInputHID *vhid = VIRTIO_INPUT_HID(vinput); 272 273 if (vinput->active) { 274 qemu_input_handler_activate(vhid->hs); 275 } else { 276 qemu_input_handler_deactivate(vhid->hs); 277 } 278 } 279 280 static void virtio_input_hid_handle_status(VirtIOInput *vinput, 281 virtio_input_event *event) 282 { 283 VirtIOInputHID *vhid = VIRTIO_INPUT_HID(vinput); 284 int ledbit = 0; 285 286 switch (le16_to_cpu(event->type)) { 287 case EV_LED: 288 if (event->code == LED_NUML) { 289 ledbit = QEMU_NUM_LOCK_LED; 290 } else if (event->code == LED_CAPSL) { 291 ledbit = QEMU_CAPS_LOCK_LED; 292 } else if (event->code == LED_SCROLLL) { 293 ledbit = QEMU_SCROLL_LOCK_LED; 294 } 295 if (event->value) { 296 vhid->ledstate |= ledbit; 297 } else { 298 vhid->ledstate &= ~ledbit; 299 } 300 kbd_put_ledstate(vhid->ledstate); 301 break; 302 default: 303 fprintf(stderr, "%s: unknown type %d\n", __func__, 304 le16_to_cpu(event->type)); 305 break; 306 } 307 } 308 309 static Property virtio_input_hid_properties[] = { 310 DEFINE_PROP_STRING("display", VirtIOInputHID, display), 311 DEFINE_PROP_UINT32("head", VirtIOInputHID, head, 0), 312 DEFINE_PROP_END_OF_LIST(), 313 }; 314 315 static void virtio_input_hid_class_init(ObjectClass *klass, void *data) 316 { 317 DeviceClass *dc = DEVICE_CLASS(klass); 318 VirtIOInputClass *vic = VIRTIO_INPUT_CLASS(klass); 319 320 dc->props = virtio_input_hid_properties; 321 vic->realize = virtio_input_hid_realize; 322 vic->unrealize = virtio_input_hid_unrealize; 323 vic->change_active = virtio_input_hid_change_active; 324 vic->handle_status = virtio_input_hid_handle_status; 325 } 326 327 static const TypeInfo virtio_input_hid_info = { 328 .name = TYPE_VIRTIO_INPUT_HID, 329 .parent = TYPE_VIRTIO_INPUT, 330 .instance_size = sizeof(VirtIOInputHID), 331 .class_init = virtio_input_hid_class_init, 332 .abstract = true, 333 }; 334 335 /* ----------------------------------------------------------------- */ 336 337 static QemuInputHandler virtio_keyboard_handler = { 338 .name = VIRTIO_ID_NAME_KEYBOARD, 339 .mask = INPUT_EVENT_MASK_KEY, 340 .event = virtio_input_handle_event, 341 .sync = virtio_input_handle_sync, 342 }; 343 344 static struct virtio_input_config virtio_keyboard_config[] = { 345 { 346 .select = VIRTIO_INPUT_CFG_ID_NAME, 347 .size = sizeof(VIRTIO_ID_NAME_KEYBOARD), 348 .u.string = VIRTIO_ID_NAME_KEYBOARD, 349 },{ 350 .select = VIRTIO_INPUT_CFG_ID_DEVIDS, 351 .size = sizeof(struct virtio_input_devids), 352 .u.ids = { 353 .bustype = const_le16(BUS_VIRTUAL), 354 .vendor = const_le16(0x0627), /* same we use for usb hid devices */ 355 .product = const_le16(0x0001), 356 .version = const_le16(0x0001), 357 }, 358 },{ 359 .select = VIRTIO_INPUT_CFG_EV_BITS, 360 .subsel = EV_REP, 361 .size = 1, 362 },{ 363 .select = VIRTIO_INPUT_CFG_EV_BITS, 364 .subsel = EV_LED, 365 .size = 1, 366 .u.bitmap = { 367 (1 << LED_NUML) | (1 << LED_CAPSL) | (1 << LED_SCROLLL), 368 }, 369 }, 370 { /* end of list */ }, 371 }; 372 373 static void virtio_keyboard_init(Object *obj) 374 { 375 VirtIOInputHID *vhid = VIRTIO_INPUT_HID(obj); 376 VirtIOInput *vinput = VIRTIO_INPUT(obj); 377 378 vhid->handler = &virtio_keyboard_handler; 379 virtio_input_init_config(vinput, virtio_keyboard_config); 380 virtio_input_key_config(vinput, keymap_qcode, 381 ARRAY_SIZE(keymap_qcode)); 382 } 383 384 static const TypeInfo virtio_keyboard_info = { 385 .name = TYPE_VIRTIO_KEYBOARD, 386 .parent = TYPE_VIRTIO_INPUT_HID, 387 .instance_size = sizeof(VirtIOInputHID), 388 .instance_init = virtio_keyboard_init, 389 }; 390 391 /* ----------------------------------------------------------------- */ 392 393 static QemuInputHandler virtio_mouse_handler = { 394 .name = VIRTIO_ID_NAME_MOUSE, 395 .mask = INPUT_EVENT_MASK_BTN | INPUT_EVENT_MASK_REL, 396 .event = virtio_input_handle_event, 397 .sync = virtio_input_handle_sync, 398 }; 399 400 static struct virtio_input_config virtio_mouse_config[] = { 401 { 402 .select = VIRTIO_INPUT_CFG_ID_NAME, 403 .size = sizeof(VIRTIO_ID_NAME_MOUSE), 404 .u.string = VIRTIO_ID_NAME_MOUSE, 405 },{ 406 .select = VIRTIO_INPUT_CFG_ID_DEVIDS, 407 .size = sizeof(struct virtio_input_devids), 408 .u.ids = { 409 .bustype = const_le16(BUS_VIRTUAL), 410 .vendor = const_le16(0x0627), /* same we use for usb hid devices */ 411 .product = const_le16(0x0002), 412 .version = const_le16(0x0001), 413 }, 414 },{ 415 .select = VIRTIO_INPUT_CFG_EV_BITS, 416 .subsel = EV_REL, 417 .size = 1, 418 .u.bitmap = { 419 (1 << REL_X) | (1 << REL_Y), 420 }, 421 }, 422 { /* end of list */ }, 423 }; 424 425 static void virtio_mouse_init(Object *obj) 426 { 427 VirtIOInputHID *vhid = VIRTIO_INPUT_HID(obj); 428 VirtIOInput *vinput = VIRTIO_INPUT(obj); 429 430 vhid->handler = &virtio_mouse_handler; 431 virtio_input_init_config(vinput, virtio_mouse_config); 432 virtio_input_key_config(vinput, keymap_button, 433 ARRAY_SIZE(keymap_button)); 434 } 435 436 static const TypeInfo virtio_mouse_info = { 437 .name = TYPE_VIRTIO_MOUSE, 438 .parent = TYPE_VIRTIO_INPUT_HID, 439 .instance_size = sizeof(VirtIOInputHID), 440 .instance_init = virtio_mouse_init, 441 }; 442 443 /* ----------------------------------------------------------------- */ 444 445 static QemuInputHandler virtio_tablet_handler = { 446 .name = VIRTIO_ID_NAME_TABLET, 447 .mask = INPUT_EVENT_MASK_BTN | INPUT_EVENT_MASK_ABS, 448 .event = virtio_input_handle_event, 449 .sync = virtio_input_handle_sync, 450 }; 451 452 static struct virtio_input_config virtio_tablet_config[] = { 453 { 454 .select = VIRTIO_INPUT_CFG_ID_NAME, 455 .size = sizeof(VIRTIO_ID_NAME_TABLET), 456 .u.string = VIRTIO_ID_NAME_TABLET, 457 },{ 458 .select = VIRTIO_INPUT_CFG_ID_DEVIDS, 459 .size = sizeof(struct virtio_input_devids), 460 .u.ids = { 461 .bustype = const_le16(BUS_VIRTUAL), 462 .vendor = const_le16(0x0627), /* same we use for usb hid devices */ 463 .product = const_le16(0x0003), 464 .version = const_le16(0x0001), 465 }, 466 },{ 467 .select = VIRTIO_INPUT_CFG_EV_BITS, 468 .subsel = EV_ABS, 469 .size = 1, 470 .u.bitmap = { 471 (1 << ABS_X) | (1 << ABS_Y), 472 }, 473 },{ 474 .select = VIRTIO_INPUT_CFG_ABS_INFO, 475 .subsel = ABS_X, 476 .size = sizeof(virtio_input_absinfo), 477 .u.abs.max = const_le32(INPUT_EVENT_ABS_SIZE), 478 },{ 479 .select = VIRTIO_INPUT_CFG_ABS_INFO, 480 .subsel = ABS_Y, 481 .size = sizeof(virtio_input_absinfo), 482 .u.abs.max = const_le32(INPUT_EVENT_ABS_SIZE), 483 }, 484 { /* end of list */ }, 485 }; 486 487 static void virtio_tablet_init(Object *obj) 488 { 489 VirtIOInputHID *vhid = VIRTIO_INPUT_HID(obj); 490 VirtIOInput *vinput = VIRTIO_INPUT(obj); 491 492 vhid->handler = &virtio_tablet_handler; 493 virtio_input_init_config(vinput, virtio_tablet_config); 494 virtio_input_key_config(vinput, keymap_button, 495 ARRAY_SIZE(keymap_button)); 496 } 497 498 static const TypeInfo virtio_tablet_info = { 499 .name = TYPE_VIRTIO_TABLET, 500 .parent = TYPE_VIRTIO_INPUT_HID, 501 .instance_size = sizeof(VirtIOInputHID), 502 .instance_init = virtio_tablet_init, 503 }; 504 505 /* ----------------------------------------------------------------- */ 506 507 static void virtio_register_types(void) 508 { 509 type_register_static(&virtio_input_hid_info); 510 type_register_static(&virtio_keyboard_info); 511 type_register_static(&virtio_mouse_info); 512 type_register_static(&virtio_tablet_info); 513 } 514 515 type_init(virtio_register_types) 516