1 /* 2 * QEMU PS/2 keyboard/mouse emulation 3 * 4 * Copyright (c) 2003 Fabrice Bellard 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a copy 7 * of this software and associated documentation files (the "Software"), to deal 8 * in the Software without restriction, including without limitation the rights 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 * copies of the Software, and to permit persons to whom the Software is 11 * furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 * THE SOFTWARE. 23 */ 24 25 #include "qemu/osdep.h" 26 #include "qemu/log.h" 27 #include "hw/sysbus.h" 28 #include "hw/input/ps2.h" 29 #include "migration/vmstate.h" 30 #include "ui/console.h" 31 #include "ui/input.h" 32 #include "sysemu/reset.h" 33 #include "sysemu/runstate.h" 34 #include "qapi/error.h" 35 36 #include "trace.h" 37 38 /* Keyboard Commands */ 39 #define KBD_CMD_SET_LEDS 0xED /* Set keyboard leds */ 40 #define KBD_CMD_ECHO 0xEE 41 #define KBD_CMD_SCANCODE 0xF0 /* Get/set scancode set */ 42 #define KBD_CMD_GET_ID 0xF2 /* get keyboard ID */ 43 #define KBD_CMD_SET_RATE 0xF3 /* Set typematic rate */ 44 #define KBD_CMD_ENABLE 0xF4 /* Enable scanning */ 45 #define KBD_CMD_RESET_DISABLE 0xF5 /* reset and disable scanning */ 46 #define KBD_CMD_RESET_ENABLE 0xF6 /* reset and enable scanning */ 47 #define KBD_CMD_RESET 0xFF /* Reset */ 48 #define KBD_CMD_SET_MAKE_BREAK 0xFC /* Set Make and Break mode */ 49 #define KBD_CMD_SET_TYPEMATIC 0xFA /* Set Typematic Make and Break mode */ 50 51 /* Keyboard Replies */ 52 #define KBD_REPLY_POR 0xAA /* Power on reset */ 53 #define KBD_REPLY_ID 0xAB /* Keyboard ID */ 54 #define KBD_REPLY_ACK 0xFA /* Command ACK */ 55 #define KBD_REPLY_RESEND 0xFE /* Command NACK, send the cmd again */ 56 57 /* Mouse Commands */ 58 #define AUX_SET_SCALE11 0xE6 /* Set 1:1 scaling */ 59 #define AUX_SET_SCALE21 0xE7 /* Set 2:1 scaling */ 60 #define AUX_SET_RES 0xE8 /* Set resolution */ 61 #define AUX_GET_SCALE 0xE9 /* Get scaling factor */ 62 #define AUX_SET_STREAM 0xEA /* Set stream mode */ 63 #define AUX_POLL 0xEB /* Poll */ 64 #define AUX_RESET_WRAP 0xEC /* Reset wrap mode */ 65 #define AUX_SET_WRAP 0xEE /* Set wrap mode */ 66 #define AUX_SET_REMOTE 0xF0 /* Set remote mode */ 67 #define AUX_GET_TYPE 0xF2 /* Get type */ 68 #define AUX_SET_SAMPLE 0xF3 /* Set sample rate */ 69 #define AUX_ENABLE_DEV 0xF4 /* Enable aux device */ 70 #define AUX_DISABLE_DEV 0xF5 /* Disable aux device */ 71 #define AUX_SET_DEFAULT 0xF6 72 #define AUX_RESET 0xFF /* Reset aux device */ 73 #define AUX_ACK 0xFA /* Command byte ACK. */ 74 75 #define MOUSE_STATUS_REMOTE 0x40 76 #define MOUSE_STATUS_ENABLED 0x20 77 #define MOUSE_STATUS_SCALE21 0x10 78 79 /* 80 * PS/2 buffer size. Keep 256 bytes for compatibility with 81 * older QEMU versions. 82 */ 83 #define PS2_BUFFER_SIZE 256 84 #define PS2_QUEUE_SIZE 16 /* Queue size required by PS/2 protocol */ 85 #define PS2_QUEUE_HEADROOM 8 /* Queue size for keyboard command replies */ 86 87 /* Bits for 'modifiers' field in PS2KbdState */ 88 #define MOD_CTRL_L (1 << 0) 89 #define MOD_SHIFT_L (1 << 1) 90 #define MOD_ALT_L (1 << 2) 91 #define MOD_CTRL_R (1 << 3) 92 #define MOD_SHIFT_R (1 << 4) 93 #define MOD_ALT_R (1 << 5) 94 95 typedef struct { 96 uint8_t data[PS2_BUFFER_SIZE]; 97 int rptr, wptr, cwptr, count; 98 } PS2Queue; 99 100 struct PS2State { 101 SysBusDevice parent_obj; 102 103 PS2Queue queue; 104 int32_t write_cmd; 105 void (*update_irq)(void *, int); 106 void *update_arg; 107 }; 108 109 #define TYPE_PS2_DEVICE "ps2-device" 110 OBJECT_DECLARE_SIMPLE_TYPE(PS2State, PS2_DEVICE) 111 112 struct PS2KbdState { 113 PS2State parent_obj; 114 115 int scan_enabled; 116 int translate; 117 int scancode_set; /* 1=XT, 2=AT, 3=PS/2 */ 118 int ledstate; 119 bool need_high_bit; 120 unsigned int modifiers; /* bitmask of MOD_* constants above */ 121 }; 122 123 #define TYPE_PS2_KBD_DEVICE "ps2-kbd" 124 OBJECT_DECLARE_SIMPLE_TYPE(PS2KbdState, PS2_KBD_DEVICE) 125 126 typedef struct { 127 PS2State common; 128 uint8_t mouse_status; 129 uint8_t mouse_resolution; 130 uint8_t mouse_sample_rate; 131 uint8_t mouse_wrap; 132 uint8_t mouse_type; /* 0 = PS2, 3 = IMPS/2, 4 = IMEX */ 133 uint8_t mouse_detect_state; 134 int mouse_dx; /* current values, needed for 'poll' mode */ 135 int mouse_dy; 136 int mouse_dz; 137 int mouse_dw; 138 uint8_t mouse_buttons; 139 } PS2MouseState; 140 141 static uint8_t translate_table[256] = { 142 0xff, 0x43, 0x41, 0x3f, 0x3d, 0x3b, 0x3c, 0x58, 143 0x64, 0x44, 0x42, 0x40, 0x3e, 0x0f, 0x29, 0x59, 144 0x65, 0x38, 0x2a, 0x70, 0x1d, 0x10, 0x02, 0x5a, 145 0x66, 0x71, 0x2c, 0x1f, 0x1e, 0x11, 0x03, 0x5b, 146 0x67, 0x2e, 0x2d, 0x20, 0x12, 0x05, 0x04, 0x5c, 147 0x68, 0x39, 0x2f, 0x21, 0x14, 0x13, 0x06, 0x5d, 148 0x69, 0x31, 0x30, 0x23, 0x22, 0x15, 0x07, 0x5e, 149 0x6a, 0x72, 0x32, 0x24, 0x16, 0x08, 0x09, 0x5f, 150 0x6b, 0x33, 0x25, 0x17, 0x18, 0x0b, 0x0a, 0x60, 151 0x6c, 0x34, 0x35, 0x26, 0x27, 0x19, 0x0c, 0x61, 152 0x6d, 0x73, 0x28, 0x74, 0x1a, 0x0d, 0x62, 0x6e, 153 0x3a, 0x36, 0x1c, 0x1b, 0x75, 0x2b, 0x63, 0x76, 154 0x55, 0x56, 0x77, 0x78, 0x79, 0x7a, 0x0e, 0x7b, 155 0x7c, 0x4f, 0x7d, 0x4b, 0x47, 0x7e, 0x7f, 0x6f, 156 0x52, 0x53, 0x50, 0x4c, 0x4d, 0x48, 0x01, 0x45, 157 0x57, 0x4e, 0x51, 0x4a, 0x37, 0x49, 0x46, 0x54, 158 0x80, 0x81, 0x82, 0x41, 0x54, 0x85, 0x86, 0x87, 159 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 160 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 161 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 162 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 163 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 164 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 165 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 166 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 167 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 168 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 169 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 170 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 171 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 172 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 173 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, 174 }; 175 176 static unsigned int ps2_modifier_bit(QKeyCode key) 177 { 178 switch (key) { 179 case Q_KEY_CODE_CTRL: 180 return MOD_CTRL_L; 181 case Q_KEY_CODE_CTRL_R: 182 return MOD_CTRL_R; 183 case Q_KEY_CODE_SHIFT: 184 return MOD_SHIFT_L; 185 case Q_KEY_CODE_SHIFT_R: 186 return MOD_SHIFT_R; 187 case Q_KEY_CODE_ALT: 188 return MOD_ALT_L; 189 case Q_KEY_CODE_ALT_R: 190 return MOD_ALT_R; 191 default: 192 return 0; 193 } 194 } 195 196 static void ps2_reset_queue(PS2State *s) 197 { 198 PS2Queue *q = &s->queue; 199 200 q->rptr = 0; 201 q->wptr = 0; 202 q->cwptr = -1; 203 q->count = 0; 204 } 205 206 int ps2_queue_empty(PS2State *s) 207 { 208 return s->queue.count == 0; 209 } 210 211 void ps2_queue_noirq(PS2State *s, int b) 212 { 213 PS2Queue *q = &s->queue; 214 215 if (q->count >= PS2_QUEUE_SIZE) { 216 return; 217 } 218 219 q->data[q->wptr] = b; 220 if (++q->wptr == PS2_BUFFER_SIZE) { 221 q->wptr = 0; 222 } 223 q->count++; 224 } 225 226 void ps2_raise_irq(PS2State *s) 227 { 228 s->update_irq(s->update_arg, 1); 229 } 230 231 void ps2_queue(PS2State *s, int b) 232 { 233 if (PS2_QUEUE_SIZE - s->queue.count < 1) { 234 return; 235 } 236 237 ps2_queue_noirq(s, b); 238 ps2_raise_irq(s); 239 } 240 241 void ps2_queue_2(PS2State *s, int b1, int b2) 242 { 243 if (PS2_QUEUE_SIZE - s->queue.count < 2) { 244 return; 245 } 246 247 ps2_queue_noirq(s, b1); 248 ps2_queue_noirq(s, b2); 249 ps2_raise_irq(s); 250 } 251 252 void ps2_queue_3(PS2State *s, int b1, int b2, int b3) 253 { 254 if (PS2_QUEUE_SIZE - s->queue.count < 3) { 255 return; 256 } 257 258 ps2_queue_noirq(s, b1); 259 ps2_queue_noirq(s, b2); 260 ps2_queue_noirq(s, b3); 261 ps2_raise_irq(s); 262 } 263 264 void ps2_queue_4(PS2State *s, int b1, int b2, int b3, int b4) 265 { 266 if (PS2_QUEUE_SIZE - s->queue.count < 4) { 267 return; 268 } 269 270 ps2_queue_noirq(s, b1); 271 ps2_queue_noirq(s, b2); 272 ps2_queue_noirq(s, b3); 273 ps2_queue_noirq(s, b4); 274 ps2_raise_irq(s); 275 } 276 277 static void ps2_cqueue_data(PS2Queue *q, int b) 278 { 279 q->data[q->cwptr] = b; 280 if (++q->cwptr >= PS2_BUFFER_SIZE) { 281 q->cwptr = 0; 282 } 283 q->count++; 284 } 285 286 static void ps2_cqueue_1(PS2State *s, int b1) 287 { 288 PS2Queue *q = &s->queue; 289 290 q->rptr = (q->rptr - 1) & (PS2_BUFFER_SIZE - 1); 291 q->cwptr = q->rptr; 292 ps2_cqueue_data(q, b1); 293 ps2_raise_irq(s); 294 } 295 296 static void ps2_cqueue_2(PS2State *s, int b1, int b2) 297 { 298 PS2Queue *q = &s->queue; 299 300 q->rptr = (q->rptr - 2) & (PS2_BUFFER_SIZE - 1); 301 q->cwptr = q->rptr; 302 ps2_cqueue_data(q, b1); 303 ps2_cqueue_data(q, b2); 304 ps2_raise_irq(s); 305 } 306 307 static void ps2_cqueue_3(PS2State *s, int b1, int b2, int b3) 308 { 309 PS2Queue *q = &s->queue; 310 311 q->rptr = (q->rptr - 3) & (PS2_BUFFER_SIZE - 1); 312 q->cwptr = q->rptr; 313 ps2_cqueue_data(q, b1); 314 ps2_cqueue_data(q, b2); 315 ps2_cqueue_data(q, b3); 316 ps2_raise_irq(s); 317 } 318 319 static void ps2_cqueue_reset(PS2State *s) 320 { 321 PS2Queue *q = &s->queue; 322 int ccount; 323 324 if (q->cwptr == -1) { 325 return; 326 } 327 328 ccount = (q->cwptr - q->rptr) & (PS2_BUFFER_SIZE - 1); 329 q->count -= ccount; 330 q->rptr = q->cwptr; 331 q->cwptr = -1; 332 } 333 334 /* keycode is the untranslated scancode in the current scancode set. */ 335 static void ps2_put_keycode(void *opaque, int keycode) 336 { 337 PS2KbdState *s = opaque; 338 PS2State *ps = PS2_DEVICE(s); 339 340 trace_ps2_put_keycode(opaque, keycode); 341 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL); 342 343 if (s->translate) { 344 if (keycode == 0xf0) { 345 s->need_high_bit = true; 346 } else if (s->need_high_bit) { 347 ps2_queue(ps, translate_table[keycode] | 0x80); 348 s->need_high_bit = false; 349 } else { 350 ps2_queue(ps, translate_table[keycode]); 351 } 352 } else { 353 ps2_queue(ps, keycode); 354 } 355 } 356 357 static void ps2_keyboard_event(DeviceState *dev, QemuConsole *src, 358 InputEvent *evt) 359 { 360 PS2KbdState *s = (PS2KbdState *)dev; 361 InputKeyEvent *key = evt->u.key.data; 362 int qcode; 363 uint16_t keycode = 0; 364 int mod; 365 366 /* do not process events while disabled to prevent stream corruption */ 367 if (!s->scan_enabled) { 368 return; 369 } 370 371 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL); 372 assert(evt->type == INPUT_EVENT_KIND_KEY); 373 qcode = qemu_input_key_value_to_qcode(key->key); 374 375 mod = ps2_modifier_bit(qcode); 376 trace_ps2_keyboard_event(s, qcode, key->down, mod, 377 s->modifiers, s->scancode_set, s->translate); 378 if (key->down) { 379 s->modifiers |= mod; 380 } else { 381 s->modifiers &= ~mod; 382 } 383 384 if (s->scancode_set == 1) { 385 if (qcode == Q_KEY_CODE_PAUSE) { 386 if (s->modifiers & (MOD_CTRL_L | MOD_CTRL_R)) { 387 if (key->down) { 388 ps2_put_keycode(s, 0xe0); 389 ps2_put_keycode(s, 0x46); 390 ps2_put_keycode(s, 0xe0); 391 ps2_put_keycode(s, 0xc6); 392 } 393 } else { 394 if (key->down) { 395 ps2_put_keycode(s, 0xe1); 396 ps2_put_keycode(s, 0x1d); 397 ps2_put_keycode(s, 0x45); 398 ps2_put_keycode(s, 0xe1); 399 ps2_put_keycode(s, 0x9d); 400 ps2_put_keycode(s, 0xc5); 401 } 402 } 403 } else if (qcode == Q_KEY_CODE_PRINT) { 404 if (s->modifiers & MOD_ALT_L) { 405 if (key->down) { 406 ps2_put_keycode(s, 0xb8); 407 ps2_put_keycode(s, 0x38); 408 ps2_put_keycode(s, 0x54); 409 } else { 410 ps2_put_keycode(s, 0xd4); 411 ps2_put_keycode(s, 0xb8); 412 ps2_put_keycode(s, 0x38); 413 } 414 } else if (s->modifiers & MOD_ALT_R) { 415 if (key->down) { 416 ps2_put_keycode(s, 0xe0); 417 ps2_put_keycode(s, 0xb8); 418 ps2_put_keycode(s, 0xe0); 419 ps2_put_keycode(s, 0x38); 420 ps2_put_keycode(s, 0x54); 421 } else { 422 ps2_put_keycode(s, 0xd4); 423 ps2_put_keycode(s, 0xe0); 424 ps2_put_keycode(s, 0xb8); 425 ps2_put_keycode(s, 0xe0); 426 ps2_put_keycode(s, 0x38); 427 } 428 } else if (s->modifiers & (MOD_SHIFT_L | MOD_CTRL_L | 429 MOD_SHIFT_R | MOD_CTRL_R)) { 430 if (key->down) { 431 ps2_put_keycode(s, 0xe0); 432 ps2_put_keycode(s, 0x37); 433 } else { 434 ps2_put_keycode(s, 0xe0); 435 ps2_put_keycode(s, 0xb7); 436 } 437 } else { 438 if (key->down) { 439 ps2_put_keycode(s, 0xe0); 440 ps2_put_keycode(s, 0x2a); 441 ps2_put_keycode(s, 0xe0); 442 ps2_put_keycode(s, 0x37); 443 } else { 444 ps2_put_keycode(s, 0xe0); 445 ps2_put_keycode(s, 0xb7); 446 ps2_put_keycode(s, 0xe0); 447 ps2_put_keycode(s, 0xaa); 448 } 449 } 450 } else { 451 if (qcode < qemu_input_map_qcode_to_atset1_len) { 452 keycode = qemu_input_map_qcode_to_atset1[qcode]; 453 } 454 if (keycode) { 455 if (keycode & 0xff00) { 456 ps2_put_keycode(s, keycode >> 8); 457 } 458 if (!key->down) { 459 keycode |= 0x80; 460 } 461 ps2_put_keycode(s, keycode & 0xff); 462 } else { 463 qemu_log_mask(LOG_UNIMP, 464 "ps2: ignoring key with qcode %d\n", qcode); 465 } 466 } 467 } else if (s->scancode_set == 2) { 468 if (qcode == Q_KEY_CODE_PAUSE) { 469 if (s->modifiers & (MOD_CTRL_L | MOD_CTRL_R)) { 470 if (key->down) { 471 ps2_put_keycode(s, 0xe0); 472 ps2_put_keycode(s, 0x7e); 473 ps2_put_keycode(s, 0xe0); 474 ps2_put_keycode(s, 0xf0); 475 ps2_put_keycode(s, 0x7e); 476 } 477 } else { 478 if (key->down) { 479 ps2_put_keycode(s, 0xe1); 480 ps2_put_keycode(s, 0x14); 481 ps2_put_keycode(s, 0x77); 482 ps2_put_keycode(s, 0xe1); 483 ps2_put_keycode(s, 0xf0); 484 ps2_put_keycode(s, 0x14); 485 ps2_put_keycode(s, 0xf0); 486 ps2_put_keycode(s, 0x77); 487 } 488 } 489 } else if (qcode == Q_KEY_CODE_PRINT) { 490 if (s->modifiers & MOD_ALT_L) { 491 if (key->down) { 492 ps2_put_keycode(s, 0xf0); 493 ps2_put_keycode(s, 0x11); 494 ps2_put_keycode(s, 0x11); 495 ps2_put_keycode(s, 0x84); 496 } else { 497 ps2_put_keycode(s, 0xf0); 498 ps2_put_keycode(s, 0x84); 499 ps2_put_keycode(s, 0xf0); 500 ps2_put_keycode(s, 0x11); 501 ps2_put_keycode(s, 0x11); 502 } 503 } else if (s->modifiers & MOD_ALT_R) { 504 if (key->down) { 505 ps2_put_keycode(s, 0xe0); 506 ps2_put_keycode(s, 0xf0); 507 ps2_put_keycode(s, 0x11); 508 ps2_put_keycode(s, 0xe0); 509 ps2_put_keycode(s, 0x11); 510 ps2_put_keycode(s, 0x84); 511 } else { 512 ps2_put_keycode(s, 0xf0); 513 ps2_put_keycode(s, 0x84); 514 ps2_put_keycode(s, 0xe0); 515 ps2_put_keycode(s, 0xf0); 516 ps2_put_keycode(s, 0x11); 517 ps2_put_keycode(s, 0xe0); 518 ps2_put_keycode(s, 0x11); 519 } 520 } else if (s->modifiers & (MOD_SHIFT_L | MOD_CTRL_L | 521 MOD_SHIFT_R | MOD_CTRL_R)) { 522 if (key->down) { 523 ps2_put_keycode(s, 0xe0); 524 ps2_put_keycode(s, 0x7c); 525 } else { 526 ps2_put_keycode(s, 0xe0); 527 ps2_put_keycode(s, 0xf0); 528 ps2_put_keycode(s, 0x7c); 529 } 530 } else { 531 if (key->down) { 532 ps2_put_keycode(s, 0xe0); 533 ps2_put_keycode(s, 0x12); 534 ps2_put_keycode(s, 0xe0); 535 ps2_put_keycode(s, 0x7c); 536 } else { 537 ps2_put_keycode(s, 0xe0); 538 ps2_put_keycode(s, 0xf0); 539 ps2_put_keycode(s, 0x7c); 540 ps2_put_keycode(s, 0xe0); 541 ps2_put_keycode(s, 0xf0); 542 ps2_put_keycode(s, 0x12); 543 } 544 } 545 } else { 546 if (qcode < qemu_input_map_qcode_to_atset2_len) { 547 keycode = qemu_input_map_qcode_to_atset2[qcode]; 548 } 549 if (keycode) { 550 if (keycode & 0xff00) { 551 ps2_put_keycode(s, keycode >> 8); 552 } 553 if (!key->down) { 554 ps2_put_keycode(s, 0xf0); 555 } 556 ps2_put_keycode(s, keycode & 0xff); 557 } else { 558 qemu_log_mask(LOG_UNIMP, 559 "ps2: ignoring key with qcode %d\n", qcode); 560 } 561 } 562 } else if (s->scancode_set == 3) { 563 if (qcode < qemu_input_map_qcode_to_atset3_len) { 564 keycode = qemu_input_map_qcode_to_atset3[qcode]; 565 } 566 if (keycode) { 567 /* FIXME: break code should be configured on a key by key basis */ 568 if (!key->down) { 569 ps2_put_keycode(s, 0xf0); 570 } 571 ps2_put_keycode(s, keycode); 572 } else { 573 qemu_log_mask(LOG_UNIMP, 574 "ps2: ignoring key with qcode %d\n", qcode); 575 } 576 } 577 } 578 579 uint32_t ps2_read_data(PS2State *s) 580 { 581 PS2Queue *q; 582 int val, index; 583 584 trace_ps2_read_data(s); 585 q = &s->queue; 586 if (q->count == 0) { 587 /* 588 * NOTE: if no data left, we return the last keyboard one 589 * (needed for EMM386) 590 */ 591 /* XXX: need a timer to do things correctly */ 592 index = q->rptr - 1; 593 if (index < 0) { 594 index = PS2_BUFFER_SIZE - 1; 595 } 596 val = q->data[index]; 597 } else { 598 val = q->data[q->rptr]; 599 if (++q->rptr == PS2_BUFFER_SIZE) { 600 q->rptr = 0; 601 } 602 q->count--; 603 if (q->rptr == q->cwptr) { 604 /* command reply queue is empty */ 605 q->cwptr = -1; 606 } 607 /* reading deasserts IRQ */ 608 s->update_irq(s->update_arg, 0); 609 /* reassert IRQs if data left */ 610 if (q->count) { 611 s->update_irq(s->update_arg, 1); 612 } 613 } 614 return val; 615 } 616 617 static void ps2_set_ledstate(PS2KbdState *s, int ledstate) 618 { 619 trace_ps2_set_ledstate(s, ledstate); 620 s->ledstate = ledstate; 621 kbd_put_ledstate(ledstate); 622 } 623 624 static void ps2_reset_keyboard(PS2KbdState *s) 625 { 626 PS2State *ps2 = PS2_DEVICE(s); 627 628 trace_ps2_reset_keyboard(s); 629 s->scan_enabled = 1; 630 s->scancode_set = 2; 631 ps2_reset_queue(ps2); 632 ps2_set_ledstate(s, 0); 633 } 634 635 void ps2_write_keyboard(void *opaque, int val) 636 { 637 PS2KbdState *s = (PS2KbdState *)opaque; 638 PS2State *ps2 = PS2_DEVICE(s); 639 640 trace_ps2_write_keyboard(opaque, val); 641 ps2_cqueue_reset(ps2); 642 switch (ps2->write_cmd) { 643 default: 644 case -1: 645 switch (val) { 646 case 0x00: 647 ps2_cqueue_1(ps2, KBD_REPLY_ACK); 648 break; 649 case 0x05: 650 ps2_cqueue_1(ps2, KBD_REPLY_RESEND); 651 break; 652 case KBD_CMD_GET_ID: 653 /* We emulate a MF2 AT keyboard here */ 654 ps2_cqueue_3(ps2, KBD_REPLY_ACK, KBD_REPLY_ID, 655 s->translate ? 0x41 : 0x83); 656 break; 657 case KBD_CMD_ECHO: 658 ps2_cqueue_1(ps2, KBD_CMD_ECHO); 659 break; 660 case KBD_CMD_ENABLE: 661 s->scan_enabled = 1; 662 ps2_cqueue_1(ps2, KBD_REPLY_ACK); 663 break; 664 case KBD_CMD_SCANCODE: 665 case KBD_CMD_SET_LEDS: 666 case KBD_CMD_SET_RATE: 667 case KBD_CMD_SET_MAKE_BREAK: 668 ps2->write_cmd = val; 669 ps2_cqueue_1(ps2, KBD_REPLY_ACK); 670 break; 671 case KBD_CMD_RESET_DISABLE: 672 ps2_reset_keyboard(s); 673 s->scan_enabled = 0; 674 ps2_cqueue_1(ps2, KBD_REPLY_ACK); 675 break; 676 case KBD_CMD_RESET_ENABLE: 677 ps2_reset_keyboard(s); 678 s->scan_enabled = 1; 679 ps2_cqueue_1(ps2, KBD_REPLY_ACK); 680 break; 681 case KBD_CMD_RESET: 682 ps2_reset_keyboard(s); 683 ps2_cqueue_2(ps2, 684 KBD_REPLY_ACK, 685 KBD_REPLY_POR); 686 break; 687 case KBD_CMD_SET_TYPEMATIC: 688 ps2_cqueue_1(ps2, KBD_REPLY_ACK); 689 break; 690 default: 691 ps2_cqueue_1(ps2, KBD_REPLY_RESEND); 692 break; 693 } 694 break; 695 case KBD_CMD_SET_MAKE_BREAK: 696 ps2_cqueue_1(ps2, KBD_REPLY_ACK); 697 ps2->write_cmd = -1; 698 break; 699 case KBD_CMD_SCANCODE: 700 if (val == 0) { 701 ps2_cqueue_2(ps2, KBD_REPLY_ACK, s->translate ? 702 translate_table[s->scancode_set] : s->scancode_set); 703 } else if (val >= 1 && val <= 3) { 704 s->scancode_set = val; 705 ps2_cqueue_1(ps2, KBD_REPLY_ACK); 706 } else { 707 ps2_cqueue_1(ps2, KBD_REPLY_RESEND); 708 } 709 ps2->write_cmd = -1; 710 break; 711 case KBD_CMD_SET_LEDS: 712 ps2_set_ledstate(s, val); 713 ps2_cqueue_1(ps2, KBD_REPLY_ACK); 714 ps2->write_cmd = -1; 715 break; 716 case KBD_CMD_SET_RATE: 717 ps2_cqueue_1(ps2, KBD_REPLY_ACK); 718 ps2->write_cmd = -1; 719 break; 720 } 721 } 722 723 /* 724 * Set the scancode translation mode. 725 * 0 = raw scancodes. 726 * 1 = translated scancodes (used by qemu internally). 727 */ 728 729 void ps2_keyboard_set_translation(void *opaque, int mode) 730 { 731 PS2KbdState *s = (PS2KbdState *)opaque; 732 trace_ps2_keyboard_set_translation(opaque, mode); 733 s->translate = mode; 734 } 735 736 static int ps2_mouse_send_packet(PS2MouseState *s) 737 { 738 /* IMPS/2 and IMEX send 4 bytes, PS2 sends 3 bytes */ 739 const int needed = s->mouse_type ? 4 : 3; 740 unsigned int b; 741 int dx1, dy1, dz1, dw1; 742 743 if (PS2_QUEUE_SIZE - s->common.queue.count < needed) { 744 return 0; 745 } 746 747 dx1 = s->mouse_dx; 748 dy1 = s->mouse_dy; 749 dz1 = s->mouse_dz; 750 dw1 = s->mouse_dw; 751 /* XXX: increase range to 8 bits ? */ 752 if (dx1 > 127) { 753 dx1 = 127; 754 } else if (dx1 < -127) { 755 dx1 = -127; 756 } 757 if (dy1 > 127) { 758 dy1 = 127; 759 } else if (dy1 < -127) { 760 dy1 = -127; 761 } 762 b = 0x08 | ((dx1 < 0) << 4) | ((dy1 < 0) << 5) | (s->mouse_buttons & 0x07); 763 ps2_queue_noirq(&s->common, b); 764 ps2_queue_noirq(&s->common, dx1 & 0xff); 765 ps2_queue_noirq(&s->common, dy1 & 0xff); 766 /* extra byte for IMPS/2 or IMEX */ 767 switch (s->mouse_type) { 768 default: 769 /* Just ignore the wheels if not supported */ 770 s->mouse_dz = 0; 771 s->mouse_dw = 0; 772 break; 773 case 3: 774 if (dz1 > 127) { 775 dz1 = 127; 776 } else if (dz1 < -127) { 777 dz1 = -127; 778 } 779 ps2_queue_noirq(&s->common, dz1 & 0xff); 780 s->mouse_dz -= dz1; 781 s->mouse_dw = 0; 782 break; 783 case 4: 784 /* 785 * This matches what the Linux kernel expects for exps/2 in 786 * drivers/input/mouse/psmouse-base.c. Note, if you happen to 787 * press/release the 4th or 5th buttons at the same moment as a 788 * horizontal wheel scroll, those button presses will get lost. I'm not 789 * sure what to do about that, since by this point we don't know 790 * whether those buttons actually changed state. 791 */ 792 if (dw1 != 0) { 793 if (dw1 > 31) { 794 dw1 = 31; 795 } else if (dw1 < -31) { 796 dw1 = -31; 797 } 798 799 /* 800 * linux kernel expects first 6 bits to represent the value 801 * for horizontal scroll 802 */ 803 b = (dw1 & 0x3f) | 0x40; 804 s->mouse_dw -= dw1; 805 } else { 806 if (dz1 > 7) { 807 dz1 = 7; 808 } else if (dz1 < -7) { 809 dz1 = -7; 810 } 811 812 b = (dz1 & 0x0f) | ((s->mouse_buttons & 0x18) << 1); 813 s->mouse_dz -= dz1; 814 } 815 ps2_queue_noirq(&s->common, b); 816 break; 817 } 818 819 ps2_raise_irq(&s->common); 820 821 trace_ps2_mouse_send_packet(s, dx1, dy1, dz1, b); 822 /* update deltas */ 823 s->mouse_dx -= dx1; 824 s->mouse_dy -= dy1; 825 826 return 1; 827 } 828 829 static void ps2_mouse_event(DeviceState *dev, QemuConsole *src, 830 InputEvent *evt) 831 { 832 static const int bmap[INPUT_BUTTON__MAX] = { 833 [INPUT_BUTTON_LEFT] = PS2_MOUSE_BUTTON_LEFT, 834 [INPUT_BUTTON_MIDDLE] = PS2_MOUSE_BUTTON_MIDDLE, 835 [INPUT_BUTTON_RIGHT] = PS2_MOUSE_BUTTON_RIGHT, 836 [INPUT_BUTTON_SIDE] = PS2_MOUSE_BUTTON_SIDE, 837 [INPUT_BUTTON_EXTRA] = PS2_MOUSE_BUTTON_EXTRA, 838 }; 839 PS2MouseState *s = (PS2MouseState *)dev; 840 InputMoveEvent *move; 841 InputBtnEvent *btn; 842 843 /* check if deltas are recorded when disabled */ 844 if (!(s->mouse_status & MOUSE_STATUS_ENABLED)) { 845 return; 846 } 847 848 switch (evt->type) { 849 case INPUT_EVENT_KIND_REL: 850 move = evt->u.rel.data; 851 if (move->axis == INPUT_AXIS_X) { 852 s->mouse_dx += move->value; 853 } else if (move->axis == INPUT_AXIS_Y) { 854 s->mouse_dy -= move->value; 855 } 856 break; 857 858 case INPUT_EVENT_KIND_BTN: 859 btn = evt->u.btn.data; 860 if (btn->down) { 861 s->mouse_buttons |= bmap[btn->button]; 862 if (btn->button == INPUT_BUTTON_WHEEL_UP) { 863 s->mouse_dz--; 864 } else if (btn->button == INPUT_BUTTON_WHEEL_DOWN) { 865 s->mouse_dz++; 866 } 867 868 if (btn->button == INPUT_BUTTON_WHEEL_RIGHT) { 869 s->mouse_dw--; 870 } else if (btn->button == INPUT_BUTTON_WHEEL_LEFT) { 871 s->mouse_dw++; 872 } 873 } else { 874 s->mouse_buttons &= ~bmap[btn->button]; 875 } 876 break; 877 878 default: 879 /* keep gcc happy */ 880 break; 881 } 882 } 883 884 static void ps2_mouse_sync(DeviceState *dev) 885 { 886 PS2MouseState *s = (PS2MouseState *)dev; 887 888 /* do not sync while disabled to prevent stream corruption */ 889 if (!(s->mouse_status & MOUSE_STATUS_ENABLED)) { 890 return; 891 } 892 893 if (s->mouse_buttons) { 894 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL); 895 } 896 if (!(s->mouse_status & MOUSE_STATUS_REMOTE)) { 897 /* 898 * if not remote, send event. Multiple events are sent if 899 * too big deltas 900 */ 901 while (ps2_mouse_send_packet(s)) { 902 if (s->mouse_dx == 0 && s->mouse_dy == 0 903 && s->mouse_dz == 0 && s->mouse_dw == 0) { 904 break; 905 } 906 } 907 } 908 } 909 910 void ps2_mouse_fake_event(void *opaque) 911 { 912 PS2MouseState *s = opaque; 913 trace_ps2_mouse_fake_event(opaque); 914 s->mouse_dx++; 915 ps2_mouse_sync(opaque); 916 } 917 918 void ps2_write_mouse(void *opaque, int val) 919 { 920 PS2MouseState *s = (PS2MouseState *)opaque; 921 922 trace_ps2_write_mouse(opaque, val); 923 switch (s->common.write_cmd) { 924 default: 925 case -1: 926 /* mouse command */ 927 if (s->mouse_wrap) { 928 if (val == AUX_RESET_WRAP) { 929 s->mouse_wrap = 0; 930 ps2_queue(&s->common, AUX_ACK); 931 return; 932 } else if (val != AUX_RESET) { 933 ps2_queue(&s->common, val); 934 return; 935 } 936 } 937 switch (val) { 938 case AUX_SET_SCALE11: 939 s->mouse_status &= ~MOUSE_STATUS_SCALE21; 940 ps2_queue(&s->common, AUX_ACK); 941 break; 942 case AUX_SET_SCALE21: 943 s->mouse_status |= MOUSE_STATUS_SCALE21; 944 ps2_queue(&s->common, AUX_ACK); 945 break; 946 case AUX_SET_STREAM: 947 s->mouse_status &= ~MOUSE_STATUS_REMOTE; 948 ps2_queue(&s->common, AUX_ACK); 949 break; 950 case AUX_SET_WRAP: 951 s->mouse_wrap = 1; 952 ps2_queue(&s->common, AUX_ACK); 953 break; 954 case AUX_SET_REMOTE: 955 s->mouse_status |= MOUSE_STATUS_REMOTE; 956 ps2_queue(&s->common, AUX_ACK); 957 break; 958 case AUX_GET_TYPE: 959 ps2_queue_2(&s->common, 960 AUX_ACK, 961 s->mouse_type); 962 break; 963 case AUX_SET_RES: 964 case AUX_SET_SAMPLE: 965 s->common.write_cmd = val; 966 ps2_queue(&s->common, AUX_ACK); 967 break; 968 case AUX_GET_SCALE: 969 ps2_queue_4(&s->common, 970 AUX_ACK, 971 s->mouse_status, 972 s->mouse_resolution, 973 s->mouse_sample_rate); 974 break; 975 case AUX_POLL: 976 ps2_queue(&s->common, AUX_ACK); 977 ps2_mouse_send_packet(s); 978 break; 979 case AUX_ENABLE_DEV: 980 s->mouse_status |= MOUSE_STATUS_ENABLED; 981 ps2_queue(&s->common, AUX_ACK); 982 break; 983 case AUX_DISABLE_DEV: 984 s->mouse_status &= ~MOUSE_STATUS_ENABLED; 985 ps2_queue(&s->common, AUX_ACK); 986 break; 987 case AUX_SET_DEFAULT: 988 s->mouse_sample_rate = 100; 989 s->mouse_resolution = 2; 990 s->mouse_status = 0; 991 ps2_queue(&s->common, AUX_ACK); 992 break; 993 case AUX_RESET: 994 s->mouse_sample_rate = 100; 995 s->mouse_resolution = 2; 996 s->mouse_status = 0; 997 s->mouse_type = 0; 998 ps2_reset_queue(&s->common); 999 ps2_queue_3(&s->common, 1000 AUX_ACK, 1001 0xaa, 1002 s->mouse_type); 1003 break; 1004 default: 1005 break; 1006 } 1007 break; 1008 case AUX_SET_SAMPLE: 1009 s->mouse_sample_rate = val; 1010 /* detect IMPS/2 or IMEX */ 1011 switch (s->mouse_detect_state) { 1012 default: 1013 case 0: 1014 if (val == 200) { 1015 s->mouse_detect_state = 1; 1016 } 1017 break; 1018 case 1: 1019 if (val == 100) { 1020 s->mouse_detect_state = 2; 1021 } else if (val == 200) { 1022 s->mouse_detect_state = 3; 1023 } else { 1024 s->mouse_detect_state = 0; 1025 } 1026 break; 1027 case 2: 1028 if (val == 80) { 1029 s->mouse_type = 3; /* IMPS/2 */ 1030 } 1031 s->mouse_detect_state = 0; 1032 break; 1033 case 3: 1034 if (val == 80) { 1035 s->mouse_type = 4; /* IMEX */ 1036 } 1037 s->mouse_detect_state = 0; 1038 break; 1039 } 1040 ps2_queue(&s->common, AUX_ACK); 1041 s->common.write_cmd = -1; 1042 break; 1043 case AUX_SET_RES: 1044 s->mouse_resolution = val; 1045 ps2_queue(&s->common, AUX_ACK); 1046 s->common.write_cmd = -1; 1047 break; 1048 } 1049 } 1050 1051 static void ps2_common_reset(PS2State *s) 1052 { 1053 s->write_cmd = -1; 1054 ps2_reset_queue(s); 1055 s->update_irq(s->update_arg, 0); 1056 } 1057 1058 static void ps2_common_post_load(PS2State *s) 1059 { 1060 PS2Queue *q = &s->queue; 1061 int ccount = 0; 1062 1063 /* limit the number of queued command replies to PS2_QUEUE_HEADROOM */ 1064 if (q->cwptr != -1) { 1065 ccount = (q->cwptr - q->rptr) & (PS2_BUFFER_SIZE - 1); 1066 if (ccount > PS2_QUEUE_HEADROOM) { 1067 ccount = PS2_QUEUE_HEADROOM; 1068 } 1069 } 1070 1071 /* limit the scancode queue size to PS2_QUEUE_SIZE */ 1072 if (q->count < ccount) { 1073 q->count = ccount; 1074 } else if (q->count > ccount + PS2_QUEUE_SIZE) { 1075 q->count = ccount + PS2_QUEUE_SIZE; 1076 } 1077 1078 /* sanitize rptr and recalculate wptr and cwptr */ 1079 q->rptr = q->rptr & (PS2_BUFFER_SIZE - 1); 1080 q->wptr = (q->rptr + q->count) & (PS2_BUFFER_SIZE - 1); 1081 q->cwptr = ccount ? (q->rptr + ccount) & (PS2_BUFFER_SIZE - 1) : -1; 1082 } 1083 1084 static void ps2_kbd_reset(void *opaque) 1085 { 1086 PS2KbdState *s = (PS2KbdState *) opaque; 1087 PS2State *ps2 = PS2_DEVICE(s); 1088 1089 trace_ps2_kbd_reset(opaque); 1090 ps2_common_reset(ps2); 1091 s->scan_enabled = 1; 1092 s->translate = 0; 1093 s->scancode_set = 2; 1094 s->modifiers = 0; 1095 } 1096 1097 static void ps2_mouse_reset(void *opaque) 1098 { 1099 PS2MouseState *s = (PS2MouseState *) opaque; 1100 1101 trace_ps2_mouse_reset(opaque); 1102 ps2_common_reset(&s->common); 1103 s->mouse_status = 0; 1104 s->mouse_resolution = 0; 1105 s->mouse_sample_rate = 0; 1106 s->mouse_wrap = 0; 1107 s->mouse_type = 0; 1108 s->mouse_detect_state = 0; 1109 s->mouse_dx = 0; 1110 s->mouse_dy = 0; 1111 s->mouse_dz = 0; 1112 s->mouse_dw = 0; 1113 s->mouse_buttons = 0; 1114 } 1115 1116 static const VMStateDescription vmstate_ps2_common = { 1117 .name = "PS2 Common State", 1118 .version_id = 3, 1119 .minimum_version_id = 2, 1120 .fields = (VMStateField[]) { 1121 VMSTATE_INT32(write_cmd, PS2State), 1122 VMSTATE_INT32(queue.rptr, PS2State), 1123 VMSTATE_INT32(queue.wptr, PS2State), 1124 VMSTATE_INT32(queue.count, PS2State), 1125 VMSTATE_BUFFER(queue.data, PS2State), 1126 VMSTATE_END_OF_LIST() 1127 } 1128 }; 1129 1130 static bool ps2_keyboard_ledstate_needed(void *opaque) 1131 { 1132 PS2KbdState *s = opaque; 1133 1134 return s->ledstate != 0; /* 0 is default state */ 1135 } 1136 1137 static int ps2_kbd_ledstate_post_load(void *opaque, int version_id) 1138 { 1139 PS2KbdState *s = opaque; 1140 1141 kbd_put_ledstate(s->ledstate); 1142 return 0; 1143 } 1144 1145 static const VMStateDescription vmstate_ps2_keyboard_ledstate = { 1146 .name = "ps2kbd/ledstate", 1147 .version_id = 3, 1148 .minimum_version_id = 2, 1149 .post_load = ps2_kbd_ledstate_post_load, 1150 .needed = ps2_keyboard_ledstate_needed, 1151 .fields = (VMStateField[]) { 1152 VMSTATE_INT32(ledstate, PS2KbdState), 1153 VMSTATE_END_OF_LIST() 1154 } 1155 }; 1156 1157 static bool ps2_keyboard_need_high_bit_needed(void *opaque) 1158 { 1159 PS2KbdState *s = opaque; 1160 return s->need_high_bit != 0; /* 0 is the usual state */ 1161 } 1162 1163 static const VMStateDescription vmstate_ps2_keyboard_need_high_bit = { 1164 .name = "ps2kbd/need_high_bit", 1165 .version_id = 1, 1166 .minimum_version_id = 1, 1167 .needed = ps2_keyboard_need_high_bit_needed, 1168 .fields = (VMStateField[]) { 1169 VMSTATE_BOOL(need_high_bit, PS2KbdState), 1170 VMSTATE_END_OF_LIST() 1171 } 1172 }; 1173 1174 static bool ps2_keyboard_cqueue_needed(void *opaque) 1175 { 1176 PS2KbdState *s = opaque; 1177 PS2State *ps2 = PS2_DEVICE(s); 1178 1179 return ps2->queue.cwptr != -1; /* the queue is mostly empty */ 1180 } 1181 1182 static const VMStateDescription vmstate_ps2_keyboard_cqueue = { 1183 .name = "ps2kbd/command_reply_queue", 1184 .needed = ps2_keyboard_cqueue_needed, 1185 .fields = (VMStateField[]) { 1186 VMSTATE_INT32(parent_obj.queue.cwptr, PS2KbdState), 1187 VMSTATE_END_OF_LIST() 1188 } 1189 }; 1190 1191 static int ps2_kbd_post_load(void *opaque, int version_id) 1192 { 1193 PS2KbdState *s = (PS2KbdState *)opaque; 1194 PS2State *ps2 = PS2_DEVICE(s); 1195 1196 if (version_id == 2) { 1197 s->scancode_set = 2; 1198 } 1199 1200 ps2_common_post_load(ps2); 1201 1202 return 0; 1203 } 1204 1205 static const VMStateDescription vmstate_ps2_keyboard = { 1206 .name = "ps2kbd", 1207 .version_id = 3, 1208 .minimum_version_id = 2, 1209 .post_load = ps2_kbd_post_load, 1210 .fields = (VMStateField[]) { 1211 VMSTATE_STRUCT(parent_obj, PS2KbdState, 0, vmstate_ps2_common, 1212 PS2State), 1213 VMSTATE_INT32(scan_enabled, PS2KbdState), 1214 VMSTATE_INT32(translate, PS2KbdState), 1215 VMSTATE_INT32_V(scancode_set, PS2KbdState, 3), 1216 VMSTATE_END_OF_LIST() 1217 }, 1218 .subsections = (const VMStateDescription * []) { 1219 &vmstate_ps2_keyboard_ledstate, 1220 &vmstate_ps2_keyboard_need_high_bit, 1221 &vmstate_ps2_keyboard_cqueue, 1222 NULL 1223 } 1224 }; 1225 1226 static int ps2_mouse_post_load(void *opaque, int version_id) 1227 { 1228 PS2MouseState *s = (PS2MouseState *)opaque; 1229 PS2State *ps2 = &s->common; 1230 1231 ps2_common_post_load(ps2); 1232 1233 return 0; 1234 } 1235 1236 static const VMStateDescription vmstate_ps2_mouse = { 1237 .name = "ps2mouse", 1238 .version_id = 2, 1239 .minimum_version_id = 2, 1240 .post_load = ps2_mouse_post_load, 1241 .fields = (VMStateField[]) { 1242 VMSTATE_STRUCT(common, PS2MouseState, 0, vmstate_ps2_common, PS2State), 1243 VMSTATE_UINT8(mouse_status, PS2MouseState), 1244 VMSTATE_UINT8(mouse_resolution, PS2MouseState), 1245 VMSTATE_UINT8(mouse_sample_rate, PS2MouseState), 1246 VMSTATE_UINT8(mouse_wrap, PS2MouseState), 1247 VMSTATE_UINT8(mouse_type, PS2MouseState), 1248 VMSTATE_UINT8(mouse_detect_state, PS2MouseState), 1249 VMSTATE_INT32(mouse_dx, PS2MouseState), 1250 VMSTATE_INT32(mouse_dy, PS2MouseState), 1251 VMSTATE_INT32(mouse_dz, PS2MouseState), 1252 VMSTATE_UINT8(mouse_buttons, PS2MouseState), 1253 VMSTATE_END_OF_LIST() 1254 } 1255 }; 1256 1257 static QemuInputHandler ps2_keyboard_handler = { 1258 .name = "QEMU PS/2 Keyboard", 1259 .mask = INPUT_EVENT_MASK_KEY, 1260 .event = ps2_keyboard_event, 1261 }; 1262 1263 void *ps2_kbd_init(void (*update_irq)(void *, int), void *update_arg) 1264 { 1265 DeviceState *dev; 1266 PS2KbdState *s; 1267 PS2State *ps2; 1268 1269 dev = qdev_new(TYPE_PS2_KBD_DEVICE); 1270 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal); 1271 s = PS2_KBD_DEVICE(dev); 1272 ps2 = PS2_DEVICE(s); 1273 1274 trace_ps2_kbd_init(s); 1275 ps2->update_irq = update_irq; 1276 ps2->update_arg = update_arg; 1277 s->scancode_set = 2; 1278 vmstate_register(NULL, 0, &vmstate_ps2_keyboard, s); 1279 qemu_input_handler_register((DeviceState *)s, 1280 &ps2_keyboard_handler); 1281 qemu_register_reset(ps2_kbd_reset, s); 1282 return s; 1283 } 1284 1285 static QemuInputHandler ps2_mouse_handler = { 1286 .name = "QEMU PS/2 Mouse", 1287 .mask = INPUT_EVENT_MASK_BTN | INPUT_EVENT_MASK_REL, 1288 .event = ps2_mouse_event, 1289 .sync = ps2_mouse_sync, 1290 }; 1291 1292 void *ps2_mouse_init(void (*update_irq)(void *, int), void *update_arg) 1293 { 1294 PS2MouseState *s = g_new0(PS2MouseState, 1); 1295 1296 trace_ps2_mouse_init(s); 1297 s->common.update_irq = update_irq; 1298 s->common.update_arg = update_arg; 1299 vmstate_register(NULL, 0, &vmstate_ps2_mouse, s); 1300 qemu_input_handler_register((DeviceState *)s, 1301 &ps2_mouse_handler); 1302 qemu_register_reset(ps2_mouse_reset, s); 1303 return s; 1304 } 1305 1306 static const TypeInfo ps2_kbd_info = { 1307 .name = TYPE_PS2_KBD_DEVICE, 1308 .parent = TYPE_PS2_DEVICE, 1309 .instance_size = sizeof(PS2KbdState), 1310 }; 1311 1312 static void ps2_class_init(ObjectClass *klass, void *data) 1313 { 1314 DeviceClass *dc = DEVICE_CLASS(klass); 1315 1316 set_bit(DEVICE_CATEGORY_INPUT, dc->categories); 1317 } 1318 1319 static const TypeInfo ps2_info = { 1320 .name = TYPE_PS2_DEVICE, 1321 .parent = TYPE_SYS_BUS_DEVICE, 1322 .instance_size = sizeof(PS2State), 1323 .class_init = ps2_class_init, 1324 .abstract = true 1325 }; 1326 1327 static void ps2_register_types(void) 1328 { 1329 type_register_static(&ps2_info); 1330 type_register_static(&ps2_kbd_info); 1331 } 1332 1333 type_init(ps2_register_types) 1334