2 * QEMU PS/2 keyboard/mouse emulation
4 * Copyright (c) 2003 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 #include "hw/input/ps2.h"
26 #include "ui/console.h"
28 #include "sysemu/sysemu.h"
30 /* debug PC keyboard */
33 /* debug PC keyboard : only mouse */
36 /* Keyboard Commands */
37 #define KBD_CMD_SET_LEDS 0xED /* Set keyboard leds */
38 #define KBD_CMD_ECHO 0xEE
39 #define KBD_CMD_SCANCODE 0xF0 /* Get/set scancode set */
40 #define KBD_CMD_GET_ID 0xF2 /* get keyboard ID */
41 #define KBD_CMD_SET_RATE 0xF3 /* Set typematic rate */
42 #define KBD_CMD_ENABLE 0xF4 /* Enable scanning */
43 #define KBD_CMD_RESET_DISABLE 0xF5 /* reset and disable scanning */
44 #define KBD_CMD_RESET_ENABLE 0xF6 /* reset and enable scanning */
45 #define KBD_CMD_RESET 0xFF /* Reset */
47 /* Keyboard Replies */
48 #define KBD_REPLY_POR 0xAA /* Power on reset */
49 #define KBD_REPLY_ID 0xAB /* Keyboard ID */
50 #define KBD_REPLY_ACK 0xFA /* Command ACK */
51 #define KBD_REPLY_RESEND 0xFE /* Command NACK, send the cmd again */
54 #define AUX_SET_SCALE11 0xE6 /* Set 1:1 scaling */
55 #define AUX_SET_SCALE21 0xE7 /* Set 2:1 scaling */
56 #define AUX_SET_RES 0xE8 /* Set resolution */
57 #define AUX_GET_SCALE 0xE9 /* Get scaling factor */
58 #define AUX_SET_STREAM 0xEA /* Set stream mode */
59 #define AUX_POLL 0xEB /* Poll */
60 #define AUX_RESET_WRAP 0xEC /* Reset wrap mode */
61 #define AUX_SET_WRAP 0xEE /* Set wrap mode */
62 #define AUX_SET_REMOTE 0xF0 /* Set remote mode */
63 #define AUX_GET_TYPE 0xF2 /* Get type */
64 #define AUX_SET_SAMPLE 0xF3 /* Set sample rate */
65 #define AUX_ENABLE_DEV 0xF4 /* Enable aux device */
66 #define AUX_DISABLE_DEV 0xF5 /* Disable aux device */
67 #define AUX_SET_DEFAULT 0xF6
68 #define AUX_RESET 0xFF /* Reset aux device */
69 #define AUX_ACK 0xFA /* Command byte ACK. */
71 #define MOUSE_STATUS_REMOTE 0x40
72 #define MOUSE_STATUS_ENABLED 0x20
73 #define MOUSE_STATUS_SCALE21 0x10
75 #define PS2_QUEUE_SIZE 16 /* Buffer size required by PS/2 protocol */
78 /* Keep the data array 256 bytes long, which compatibility
79 with older qemu versions. */
81 int rptr, wptr, count;
87 void (*update_irq)(void *, int);
94 /* QEMU uses translated PC scancodes internally. To avoid multiple
95 conversions we do the translation (if any) in the PS/2 emulation
96 not the keyboard controller. */
98 int scancode_set; /* 1=XT, 2=AT, 3=PS/2 */
104 uint8_t mouse_status;
105 uint8_t mouse_resolution;
106 uint8_t mouse_sample_rate;
108 uint8_t mouse_type; /* 0 = PS2, 3 = IMPS/2, 4 = IMEX */
109 uint8_t mouse_detect_state;
110 int mouse_dx; /* current values, needed for 'poll' mode */
113 uint8_t mouse_buttons;
116 /* Table to convert from PC scancodes to raw scancodes. */
117 static const unsigned char ps2_raw_keycode[128] = {
118 0, 118, 22, 30, 38, 37, 46, 54, 61, 62, 70, 69, 78, 85, 102, 13,
119 21, 29, 36, 45, 44, 53, 60, 67, 68, 77, 84, 91, 90, 20, 28, 27,
120 35, 43, 52, 51, 59, 66, 75, 76, 82, 14, 18, 93, 26, 34, 33, 42,
121 50, 49, 58, 65, 73, 74, 89, 124, 17, 41, 88, 5, 6, 4, 12, 3,
122 11, 2, 10, 1, 9, 119, 126, 108, 117, 125, 123, 107, 115, 116, 121, 105,
123 114, 122, 112, 113, 127, 96, 97, 120, 7, 15, 23, 31, 39, 47, 55, 63,
124 71, 79, 86, 94, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 87, 111,
125 19, 25, 57, 81, 83, 92, 95, 98, 99, 100, 101, 103, 104, 106, 109, 110
127 static const unsigned char ps2_raw_keycode_set3[128] = {
128 0, 8, 22, 30, 38, 37, 46, 54, 61, 62, 70, 69, 78, 85, 102, 13,
129 21, 29, 36, 45, 44, 53, 60, 67, 68, 77, 84, 91, 90, 17, 28, 27,
130 35, 43, 52, 51, 59, 66, 75, 76, 82, 14, 18, 92, 26, 34, 33, 42,
131 50, 49, 58, 65, 73, 74, 89, 126, 25, 41, 20, 7, 15, 23, 31, 39,
132 47, 2, 63, 71, 79, 118, 95, 108, 117, 125, 132, 107, 115, 116, 124, 105,
133 114, 122, 112, 113, 127, 96, 97, 86, 94, 15, 23, 31, 39, 47, 55, 63,
134 71, 79, 86, 94, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 87, 111,
135 19, 25, 57, 81, 83, 92, 95, 98, 99, 100, 101, 103, 104, 106, 109, 110
138 void ps2_queue(void *opaque, int b)
140 PS2State *s = (PS2State *)opaque;
141 PS2Queue *q = &s->queue;
143 if (q->count >= PS2_QUEUE_SIZE - 1)
145 q->data[q->wptr] = b;
146 if (++q->wptr == PS2_QUEUE_SIZE)
149 s->update_irq(s->update_arg, 1);
153 keycode is expressed as follow:
154 bit 7 - 0 key pressed, 1 = key released
155 bits 6-0 - translated scancode set 2
157 static void ps2_put_keycode(void *opaque, int keycode)
159 PS2KbdState *s = opaque;
161 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
162 /* XXX: add support for scancode set 1 */
163 if (!s->translate && keycode < 0xe0 && s->scancode_set > 1) {
164 if (keycode & 0x80) {
165 ps2_queue(&s->common, 0xf0);
167 if (s->scancode_set == 2) {
168 keycode = ps2_raw_keycode[keycode & 0x7f];
169 } else if (s->scancode_set == 3) {
170 keycode = ps2_raw_keycode_set3[keycode & 0x7f];
173 ps2_queue(&s->common, keycode);
176 static void ps2_keyboard_event(DeviceState *dev, QemuConsole *src,
179 PS2KbdState *s = (PS2KbdState *)dev;
180 int scancodes[3], i, count;
182 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
183 count = qemu_input_key_value_to_scancode(evt->key->key,
186 for (i = 0; i < count; i++) {
187 ps2_put_keycode(s, scancodes[i]);
191 uint32_t ps2_read_data(void *opaque)
193 PS2State *s = (PS2State *)opaque;
199 /* NOTE: if no data left, we return the last keyboard one
200 (needed for EMM386) */
201 /* XXX: need a timer to do things correctly */
204 index = PS2_QUEUE_SIZE - 1;
205 val = q->data[index];
207 val = q->data[q->rptr];
208 if (++q->rptr == PS2_QUEUE_SIZE)
211 /* reading deasserts IRQ */
212 s->update_irq(s->update_arg, 0);
213 /* reassert IRQs if data left */
214 s->update_irq(s->update_arg, q->count != 0);
219 static void ps2_set_ledstate(PS2KbdState *s, int ledstate)
221 s->ledstate = ledstate;
222 kbd_put_ledstate(ledstate);
225 static void ps2_reset_keyboard(PS2KbdState *s)
229 ps2_set_ledstate(s, 0);
232 void ps2_write_keyboard(void *opaque, int val)
234 PS2KbdState *s = (PS2KbdState *)opaque;
236 switch(s->common.write_cmd) {
241 ps2_queue(&s->common, KBD_REPLY_ACK);
244 ps2_queue(&s->common, KBD_REPLY_RESEND);
247 ps2_queue(&s->common, KBD_REPLY_ACK);
248 /* We emulate a MF2 AT keyboard here */
249 ps2_queue(&s->common, KBD_REPLY_ID);
251 ps2_queue(&s->common, 0x41);
253 ps2_queue(&s->common, 0x83);
256 ps2_queue(&s->common, KBD_CMD_ECHO);
260 ps2_queue(&s->common, KBD_REPLY_ACK);
262 case KBD_CMD_SCANCODE:
263 case KBD_CMD_SET_LEDS:
264 case KBD_CMD_SET_RATE:
265 s->common.write_cmd = val;
266 ps2_queue(&s->common, KBD_REPLY_ACK);
268 case KBD_CMD_RESET_DISABLE:
269 ps2_reset_keyboard(s);
271 ps2_queue(&s->common, KBD_REPLY_ACK);
273 case KBD_CMD_RESET_ENABLE:
274 ps2_reset_keyboard(s);
276 ps2_queue(&s->common, KBD_REPLY_ACK);
279 ps2_reset_keyboard(s);
280 ps2_queue(&s->common, KBD_REPLY_ACK);
281 ps2_queue(&s->common, KBD_REPLY_POR);
284 ps2_queue(&s->common, KBD_REPLY_ACK);
288 case KBD_CMD_SCANCODE:
290 if (s->scancode_set == 1)
291 ps2_put_keycode(s, 0x43);
292 else if (s->scancode_set == 2)
293 ps2_put_keycode(s, 0x41);
294 else if (s->scancode_set == 3)
295 ps2_put_keycode(s, 0x3f);
297 if (val >= 1 && val <= 3)
298 s->scancode_set = val;
299 ps2_queue(&s->common, KBD_REPLY_ACK);
301 s->common.write_cmd = -1;
303 case KBD_CMD_SET_LEDS:
304 ps2_set_ledstate(s, val);
305 ps2_queue(&s->common, KBD_REPLY_ACK);
306 s->common.write_cmd = -1;
308 case KBD_CMD_SET_RATE:
309 ps2_queue(&s->common, KBD_REPLY_ACK);
310 s->common.write_cmd = -1;
315 /* Set the scancode translation mode.
317 1 = translated scancodes (used by qemu internally). */
319 void ps2_keyboard_set_translation(void *opaque, int mode)
321 PS2KbdState *s = (PS2KbdState *)opaque;
325 static void ps2_mouse_send_packet(PS2MouseState *s)
333 /* XXX: increase range to 8 bits ? */
342 b = 0x08 | ((dx1 < 0) << 4) | ((dy1 < 0) << 5) | (s->mouse_buttons & 0x07);
343 ps2_queue(&s->common, b);
344 ps2_queue(&s->common, dx1 & 0xff);
345 ps2_queue(&s->common, dy1 & 0xff);
346 /* extra byte for IMPS/2 or IMEX */
347 switch(s->mouse_type) {
355 ps2_queue(&s->common, dz1 & 0xff);
362 b = (dz1 & 0x0f) | ((s->mouse_buttons & 0x18) << 1);
363 ps2_queue(&s->common, b);
373 static void ps2_mouse_event(DeviceState *dev, QemuConsole *src,
376 static const int bmap[INPUT_BUTTON_MAX] = {
377 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
378 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
379 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
381 PS2MouseState *s = (PS2MouseState *)dev;
383 /* check if deltas are recorded when disabled */
384 if (!(s->mouse_status & MOUSE_STATUS_ENABLED))
388 case INPUT_EVENT_KIND_REL:
389 if (evt->rel->axis == INPUT_AXIS_X) {
390 s->mouse_dx += evt->rel->value;
391 } else if (evt->rel->axis == INPUT_AXIS_Y) {
392 s->mouse_dy -= evt->rel->value;
396 case INPUT_EVENT_KIND_BTN:
397 if (evt->btn->down) {
398 s->mouse_buttons |= bmap[evt->btn->button];
399 if (evt->btn->button == INPUT_BUTTON_WHEEL_UP) {
401 } else if (evt->btn->button == INPUT_BUTTON_WHEEL_DOWN) {
405 s->mouse_buttons &= ~bmap[evt->btn->button];
415 static void ps2_mouse_sync(DeviceState *dev)
417 PS2MouseState *s = (PS2MouseState *)dev;
419 if (s->mouse_buttons) {
420 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
422 if (!(s->mouse_status & MOUSE_STATUS_REMOTE)) {
423 while (s->common.queue.count < PS2_QUEUE_SIZE - 4) {
424 /* if not remote, send event. Multiple events are sent if
426 ps2_mouse_send_packet(s);
427 if (s->mouse_dx == 0 && s->mouse_dy == 0 && s->mouse_dz == 0)
433 void ps2_mouse_fake_event(void *opaque)
435 PS2MouseState *s = opaque;
437 ps2_mouse_sync(opaque);
440 void ps2_write_mouse(void *opaque, int val)
442 PS2MouseState *s = (PS2MouseState *)opaque;
444 printf("kbd: write mouse 0x%02x\n", val);
446 switch(s->common.write_cmd) {
451 if (val == AUX_RESET_WRAP) {
453 ps2_queue(&s->common, AUX_ACK);
455 } else if (val != AUX_RESET) {
456 ps2_queue(&s->common, val);
461 case AUX_SET_SCALE11:
462 s->mouse_status &= ~MOUSE_STATUS_SCALE21;
463 ps2_queue(&s->common, AUX_ACK);
465 case AUX_SET_SCALE21:
466 s->mouse_status |= MOUSE_STATUS_SCALE21;
467 ps2_queue(&s->common, AUX_ACK);
470 s->mouse_status &= ~MOUSE_STATUS_REMOTE;
471 ps2_queue(&s->common, AUX_ACK);
475 ps2_queue(&s->common, AUX_ACK);
478 s->mouse_status |= MOUSE_STATUS_REMOTE;
479 ps2_queue(&s->common, AUX_ACK);
482 ps2_queue(&s->common, AUX_ACK);
483 ps2_queue(&s->common, s->mouse_type);
487 s->common.write_cmd = val;
488 ps2_queue(&s->common, AUX_ACK);
491 ps2_queue(&s->common, AUX_ACK);
492 ps2_queue(&s->common, s->mouse_status);
493 ps2_queue(&s->common, s->mouse_resolution);
494 ps2_queue(&s->common, s->mouse_sample_rate);
497 ps2_queue(&s->common, AUX_ACK);
498 ps2_mouse_send_packet(s);
501 s->mouse_status |= MOUSE_STATUS_ENABLED;
502 ps2_queue(&s->common, AUX_ACK);
504 case AUX_DISABLE_DEV:
505 s->mouse_status &= ~MOUSE_STATUS_ENABLED;
506 ps2_queue(&s->common, AUX_ACK);
508 case AUX_SET_DEFAULT:
509 s->mouse_sample_rate = 100;
510 s->mouse_resolution = 2;
512 ps2_queue(&s->common, AUX_ACK);
515 s->mouse_sample_rate = 100;
516 s->mouse_resolution = 2;
519 ps2_queue(&s->common, AUX_ACK);
520 ps2_queue(&s->common, 0xaa);
521 ps2_queue(&s->common, s->mouse_type);
528 s->mouse_sample_rate = val;
529 /* detect IMPS/2 or IMEX */
530 switch(s->mouse_detect_state) {
534 s->mouse_detect_state = 1;
538 s->mouse_detect_state = 2;
540 s->mouse_detect_state = 3;
542 s->mouse_detect_state = 0;
546 s->mouse_type = 3; /* IMPS/2 */
547 s->mouse_detect_state = 0;
551 s->mouse_type = 4; /* IMEX */
552 s->mouse_detect_state = 0;
555 ps2_queue(&s->common, AUX_ACK);
556 s->common.write_cmd = -1;
559 s->mouse_resolution = val;
560 ps2_queue(&s->common, AUX_ACK);
561 s->common.write_cmd = -1;
566 static void ps2_common_reset(PS2State *s)
574 s->update_irq(s->update_arg, 0);
577 static void ps2_common_post_load(PS2State *s)
579 PS2Queue *q = &s->queue;
582 int tmp_data[PS2_QUEUE_SIZE];
584 /* set the useful data buffer queue size, < PS2_QUEUE_SIZE */
585 size = q->count > PS2_QUEUE_SIZE ? 0 : q->count;
587 /* move the queue elements to the start of data array */
589 for (i = 0; i < size; i++) {
590 /* move the queue elements to the temporary buffer */
591 tmp_data[i] = q->data[q->rptr];
592 if (++q->rptr == 256) {
596 memcpy(q->data, tmp_data, size);
598 /* reset rptr/wptr/count */
602 s->update_irq(s->update_arg, q->count != 0);
605 static void ps2_kbd_reset(void *opaque)
607 PS2KbdState *s = (PS2KbdState *) opaque;
609 ps2_common_reset(&s->common);
615 static void ps2_mouse_reset(void *opaque)
617 PS2MouseState *s = (PS2MouseState *) opaque;
619 ps2_common_reset(&s->common);
621 s->mouse_resolution = 0;
622 s->mouse_sample_rate = 0;
625 s->mouse_detect_state = 0;
629 s->mouse_buttons = 0;
632 static const VMStateDescription vmstate_ps2_common = {
633 .name = "PS2 Common State",
635 .minimum_version_id = 2,
636 .fields = (VMStateField[]) {
637 VMSTATE_INT32(write_cmd, PS2State),
638 VMSTATE_INT32(queue.rptr, PS2State),
639 VMSTATE_INT32(queue.wptr, PS2State),
640 VMSTATE_INT32(queue.count, PS2State),
641 VMSTATE_BUFFER(queue.data, PS2State),
642 VMSTATE_END_OF_LIST()
646 static bool ps2_keyboard_ledstate_needed(void *opaque)
648 PS2KbdState *s = opaque;
650 return s->ledstate != 0; /* 0 is default state */
653 static int ps2_kbd_ledstate_post_load(void *opaque, int version_id)
655 PS2KbdState *s = opaque;
657 kbd_put_ledstate(s->ledstate);
661 static const VMStateDescription vmstate_ps2_keyboard_ledstate = {
662 .name = "ps2kbd/ledstate",
664 .minimum_version_id = 2,
665 .post_load = ps2_kbd_ledstate_post_load,
666 .fields = (VMStateField[]) {
667 VMSTATE_INT32(ledstate, PS2KbdState),
668 VMSTATE_END_OF_LIST()
672 static int ps2_kbd_post_load(void* opaque, int version_id)
674 PS2KbdState *s = (PS2KbdState*)opaque;
675 PS2State *ps2 = &s->common;
680 ps2_common_post_load(ps2);
685 static void ps2_kbd_pre_save(void *opaque)
687 PS2KbdState *s = (PS2KbdState *)opaque;
688 PS2State *ps2 = &s->common;
690 ps2_common_post_load(ps2);
693 static const VMStateDescription vmstate_ps2_keyboard = {
696 .minimum_version_id = 2,
697 .post_load = ps2_kbd_post_load,
698 .pre_save = ps2_kbd_pre_save,
699 .fields = (VMStateField[]) {
700 VMSTATE_STRUCT(common, PS2KbdState, 0, vmstate_ps2_common, PS2State),
701 VMSTATE_INT32(scan_enabled, PS2KbdState),
702 VMSTATE_INT32(translate, PS2KbdState),
703 VMSTATE_INT32_V(scancode_set, PS2KbdState,3),
704 VMSTATE_END_OF_LIST()
706 .subsections = (VMStateSubsection []) {
708 .vmsd = &vmstate_ps2_keyboard_ledstate,
709 .needed = ps2_keyboard_ledstate_needed,
716 static int ps2_mouse_post_load(void *opaque, int version_id)
718 PS2MouseState *s = (PS2MouseState *)opaque;
719 PS2State *ps2 = &s->common;
721 ps2_common_post_load(ps2);
726 static void ps2_mouse_pre_save(void *opaque)
728 PS2MouseState *s = (PS2MouseState *)opaque;
729 PS2State *ps2 = &s->common;
731 ps2_common_post_load(ps2);
734 static const VMStateDescription vmstate_ps2_mouse = {
737 .minimum_version_id = 2,
738 .post_load = ps2_mouse_post_load,
739 .pre_save = ps2_mouse_pre_save,
740 .fields = (VMStateField[]) {
741 VMSTATE_STRUCT(common, PS2MouseState, 0, vmstate_ps2_common, PS2State),
742 VMSTATE_UINT8(mouse_status, PS2MouseState),
743 VMSTATE_UINT8(mouse_resolution, PS2MouseState),
744 VMSTATE_UINT8(mouse_sample_rate, PS2MouseState),
745 VMSTATE_UINT8(mouse_wrap, PS2MouseState),
746 VMSTATE_UINT8(mouse_type, PS2MouseState),
747 VMSTATE_UINT8(mouse_detect_state, PS2MouseState),
748 VMSTATE_INT32(mouse_dx, PS2MouseState),
749 VMSTATE_INT32(mouse_dy, PS2MouseState),
750 VMSTATE_INT32(mouse_dz, PS2MouseState),
751 VMSTATE_UINT8(mouse_buttons, PS2MouseState),
752 VMSTATE_END_OF_LIST()
756 static QemuInputHandler ps2_keyboard_handler = {
757 .name = "QEMU PS/2 Keyboard",
758 .mask = INPUT_EVENT_MASK_KEY,
759 .event = ps2_keyboard_event,
762 void *ps2_kbd_init(void (*update_irq)(void *, int), void *update_arg)
764 PS2KbdState *s = (PS2KbdState *)g_malloc0(sizeof(PS2KbdState));
766 s->common.update_irq = update_irq;
767 s->common.update_arg = update_arg;
769 vmstate_register(NULL, 0, &vmstate_ps2_keyboard, s);
770 qemu_input_handler_register((DeviceState *)s,
771 &ps2_keyboard_handler);
772 qemu_register_reset(ps2_kbd_reset, s);
776 static QemuInputHandler ps2_mouse_handler = {
777 .name = "QEMU PS/2 Mouse",
778 .mask = INPUT_EVENT_MASK_BTN | INPUT_EVENT_MASK_REL,
779 .event = ps2_mouse_event,
780 .sync = ps2_mouse_sync,
783 void *ps2_mouse_init(void (*update_irq)(void *, int), void *update_arg)
785 PS2MouseState *s = (PS2MouseState *)g_malloc0(sizeof(PS2MouseState));
787 s->common.update_irq = update_irq;
788 s->common.update_arg = update_arg;
789 vmstate_register(NULL, 0, &vmstate_ps2_mouse, s);
790 qemu_input_handler_register((DeviceState *)s,
792 qemu_register_reset(ps2_mouse_reset, s);