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