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"
27 #include "sysemu/sysemu.h"
29 /* debug PC keyboard */
32 /* debug PC keyboard : only mouse */
35 /* Keyboard Commands */
36 #define KBD_CMD_SET_LEDS 0xED /* Set keyboard leds */
37 #define KBD_CMD_ECHO 0xEE
38 #define KBD_CMD_SCANCODE 0xF0 /* Get/set scancode set */
39 #define KBD_CMD_GET_ID 0xF2 /* get keyboard ID */
40 #define KBD_CMD_SET_RATE 0xF3 /* Set typematic rate */
41 #define KBD_CMD_ENABLE 0xF4 /* Enable scanning */
42 #define KBD_CMD_RESET_DISABLE 0xF5 /* reset and disable scanning */
43 #define KBD_CMD_RESET_ENABLE 0xF6 /* reset and enable scanning */
44 #define KBD_CMD_RESET 0xFF /* Reset */
46 /* Keyboard Replies */
47 #define KBD_REPLY_POR 0xAA /* Power on reset */
48 #define KBD_REPLY_ID 0xAB /* Keyboard ID */
49 #define KBD_REPLY_ACK 0xFA /* Command ACK */
50 #define KBD_REPLY_RESEND 0xFE /* Command NACK, send the cmd again */
53 #define AUX_SET_SCALE11 0xE6 /* Set 1:1 scaling */
54 #define AUX_SET_SCALE21 0xE7 /* Set 2:1 scaling */
55 #define AUX_SET_RES 0xE8 /* Set resolution */
56 #define AUX_GET_SCALE 0xE9 /* Get scaling factor */
57 #define AUX_SET_STREAM 0xEA /* Set stream mode */
58 #define AUX_POLL 0xEB /* Poll */
59 #define AUX_RESET_WRAP 0xEC /* Reset wrap mode */
60 #define AUX_SET_WRAP 0xEE /* Set wrap mode */
61 #define AUX_SET_REMOTE 0xF0 /* Set remote mode */
62 #define AUX_GET_TYPE 0xF2 /* Get type */
63 #define AUX_SET_SAMPLE 0xF3 /* Set sample rate */
64 #define AUX_ENABLE_DEV 0xF4 /* Enable aux device */
65 #define AUX_DISABLE_DEV 0xF5 /* Disable aux device */
66 #define AUX_SET_DEFAULT 0xF6
67 #define AUX_RESET 0xFF /* Reset aux device */
68 #define AUX_ACK 0xFA /* Command byte ACK. */
70 #define MOUSE_STATUS_REMOTE 0x40
71 #define MOUSE_STATUS_ENABLED 0x20
72 #define MOUSE_STATUS_SCALE21 0x10
74 #define PS2_QUEUE_SIZE 16 /* Buffer size required by PS/2 protocol */
77 /* Keep the data array 256 bytes long, which compatibility
78 with older qemu versions. */
80 int rptr
, wptr
, count
;
86 void (*update_irq
)(void *, int);
93 /* QEMU uses translated PC scancodes internally. To avoid multiple
94 conversions we do the translation (if any) in the PS/2 emulation
95 not the keyboard controller. */
97 int scancode_set
; /* 1=XT, 2=AT, 3=PS/2 */
103 uint8_t mouse_status
;
104 uint8_t mouse_resolution
;
105 uint8_t mouse_sample_rate
;
107 uint8_t mouse_type
; /* 0 = PS2, 3 = IMPS/2, 4 = IMEX */
108 uint8_t mouse_detect_state
;
109 int mouse_dx
; /* current values, needed for 'poll' mode */
112 uint8_t mouse_buttons
;
115 /* Table to convert from PC scancodes to raw scancodes. */
116 static const unsigned char ps2_raw_keycode
[128] = {
117 0, 118, 22, 30, 38, 37, 46, 54, 61, 62, 70, 69, 78, 85, 102, 13,
118 21, 29, 36, 45, 44, 53, 60, 67, 68, 77, 84, 91, 90, 20, 28, 27,
119 35, 43, 52, 51, 59, 66, 75, 76, 82, 14, 18, 93, 26, 34, 33, 42,
120 50, 49, 58, 65, 73, 74, 89, 124, 17, 41, 88, 5, 6, 4, 12, 3,
121 11, 2, 10, 1, 9, 119, 126, 108, 117, 125, 123, 107, 115, 116, 121, 105,
122 114, 122, 112, 113, 127, 96, 97, 120, 7, 15, 23, 31, 39, 47, 55, 63,
123 71, 79, 86, 94, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 87, 111,
124 19, 25, 57, 81, 83, 92, 95, 98, 99, 100, 101, 103, 104, 106, 109, 110
126 static const unsigned char ps2_raw_keycode_set3
[128] = {
127 0, 8, 22, 30, 38, 37, 46, 54, 61, 62, 70, 69, 78, 85, 102, 13,
128 21, 29, 36, 45, 44, 53, 60, 67, 68, 77, 84, 91, 90, 17, 28, 27,
129 35, 43, 52, 51, 59, 66, 75, 76, 82, 14, 18, 92, 26, 34, 33, 42,
130 50, 49, 58, 65, 73, 74, 89, 126, 25, 41, 20, 7, 15, 23, 31, 39,
131 47, 2, 63, 71, 79, 118, 95, 108, 117, 125, 132, 107, 115, 116, 124, 105,
132 114, 122, 112, 113, 127, 96, 97, 86, 94, 15, 23, 31, 39, 47, 55, 63,
133 71, 79, 86, 94, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 87, 111,
134 19, 25, 57, 81, 83, 92, 95, 98, 99, 100, 101, 103, 104, 106, 109, 110
137 void ps2_queue(void *opaque
, int b
)
139 PS2State
*s
= (PS2State
*)opaque
;
140 PS2Queue
*q
= &s
->queue
;
142 if (q
->count
>= PS2_QUEUE_SIZE
- 1)
144 q
->data
[q
->wptr
] = b
;
145 if (++q
->wptr
== PS2_QUEUE_SIZE
)
148 s
->update_irq(s
->update_arg
, 1);
152 keycode is expressed as follow:
153 bit 7 - 0 key pressed, 1 = key released
154 bits 6-0 - translated scancode set 2
156 static void ps2_put_keycode(void *opaque
, int keycode
)
158 PS2KbdState
*s
= opaque
;
160 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
);
161 /* XXX: add support for scancode set 1 */
162 if (!s
->translate
&& keycode
< 0xe0 && s
->scancode_set
> 1) {
163 if (keycode
& 0x80) {
164 ps2_queue(&s
->common
, 0xf0);
166 if (s
->scancode_set
== 2) {
167 keycode
= ps2_raw_keycode
[keycode
& 0x7f];
168 } else if (s
->scancode_set
== 3) {
169 keycode
= ps2_raw_keycode_set3
[keycode
& 0x7f];
172 ps2_queue(&s
->common
, keycode
);
175 uint32_t ps2_read_data(void *opaque
)
177 PS2State
*s
= (PS2State
*)opaque
;
183 /* NOTE: if no data left, we return the last keyboard one
184 (needed for EMM386) */
185 /* XXX: need a timer to do things correctly */
188 index
= PS2_QUEUE_SIZE
- 1;
189 val
= q
->data
[index
];
191 val
= q
->data
[q
->rptr
];
192 if (++q
->rptr
== PS2_QUEUE_SIZE
)
195 /* reading deasserts IRQ */
196 s
->update_irq(s
->update_arg
, 0);
197 /* reassert IRQs if data left */
198 s
->update_irq(s
->update_arg
, q
->count
!= 0);
203 static void ps2_set_ledstate(PS2KbdState
*s
, int ledstate
)
205 s
->ledstate
= ledstate
;
206 kbd_put_ledstate(ledstate
);
209 static void ps2_reset_keyboard(PS2KbdState
*s
)
213 ps2_set_ledstate(s
, 0);
216 void ps2_write_keyboard(void *opaque
, int val
)
218 PS2KbdState
*s
= (PS2KbdState
*)opaque
;
220 switch(s
->common
.write_cmd
) {
225 ps2_queue(&s
->common
, KBD_REPLY_ACK
);
228 ps2_queue(&s
->common
, KBD_REPLY_RESEND
);
231 ps2_queue(&s
->common
, KBD_REPLY_ACK
);
232 /* We emulate a MF2 AT keyboard here */
233 ps2_queue(&s
->common
, KBD_REPLY_ID
);
235 ps2_queue(&s
->common
, 0x41);
237 ps2_queue(&s
->common
, 0x83);
240 ps2_queue(&s
->common
, KBD_CMD_ECHO
);
244 ps2_queue(&s
->common
, KBD_REPLY_ACK
);
246 case KBD_CMD_SCANCODE
:
247 case KBD_CMD_SET_LEDS
:
248 case KBD_CMD_SET_RATE
:
249 s
->common
.write_cmd
= val
;
250 ps2_queue(&s
->common
, KBD_REPLY_ACK
);
252 case KBD_CMD_RESET_DISABLE
:
253 ps2_reset_keyboard(s
);
255 ps2_queue(&s
->common
, KBD_REPLY_ACK
);
257 case KBD_CMD_RESET_ENABLE
:
258 ps2_reset_keyboard(s
);
260 ps2_queue(&s
->common
, KBD_REPLY_ACK
);
263 ps2_reset_keyboard(s
);
264 ps2_queue(&s
->common
, KBD_REPLY_ACK
);
265 ps2_queue(&s
->common
, KBD_REPLY_POR
);
268 ps2_queue(&s
->common
, KBD_REPLY_ACK
);
272 case KBD_CMD_SCANCODE
:
274 if (s
->scancode_set
== 1)
275 ps2_put_keycode(s
, 0x43);
276 else if (s
->scancode_set
== 2)
277 ps2_put_keycode(s
, 0x41);
278 else if (s
->scancode_set
== 3)
279 ps2_put_keycode(s
, 0x3f);
281 if (val
>= 1 && val
<= 3)
282 s
->scancode_set
= val
;
283 ps2_queue(&s
->common
, KBD_REPLY_ACK
);
285 s
->common
.write_cmd
= -1;
287 case KBD_CMD_SET_LEDS
:
288 ps2_set_ledstate(s
, val
);
289 ps2_queue(&s
->common
, KBD_REPLY_ACK
);
290 s
->common
.write_cmd
= -1;
292 case KBD_CMD_SET_RATE
:
293 ps2_queue(&s
->common
, KBD_REPLY_ACK
);
294 s
->common
.write_cmd
= -1;
299 /* Set the scancode translation mode.
301 1 = translated scancodes (used by qemu internally). */
303 void ps2_keyboard_set_translation(void *opaque
, int mode
)
305 PS2KbdState
*s
= (PS2KbdState
*)opaque
;
309 static void ps2_mouse_send_packet(PS2MouseState
*s
)
317 /* XXX: increase range to 8 bits ? */
326 b
= 0x08 | ((dx1
< 0) << 4) | ((dy1
< 0) << 5) | (s
->mouse_buttons
& 0x07);
327 ps2_queue(&s
->common
, b
);
328 ps2_queue(&s
->common
, dx1
& 0xff);
329 ps2_queue(&s
->common
, dy1
& 0xff);
330 /* extra byte for IMPS/2 or IMEX */
331 switch(s
->mouse_type
) {
339 ps2_queue(&s
->common
, dz1
& 0xff);
346 b
= (dz1
& 0x0f) | ((s
->mouse_buttons
& 0x18) << 1);
347 ps2_queue(&s
->common
, b
);
357 static void ps2_mouse_event(void *opaque
,
358 int dx
, int dy
, int dz
, int buttons_state
)
360 PS2MouseState
*s
= opaque
;
362 /* check if deltas are recorded when disabled */
363 if (!(s
->mouse_status
& MOUSE_STATUS_ENABLED
))
369 /* XXX: SDL sometimes generates nul events: we delete them */
370 if (s
->mouse_dx
== 0 && s
->mouse_dy
== 0 && s
->mouse_dz
== 0 &&
371 s
->mouse_buttons
== buttons_state
)
373 s
->mouse_buttons
= buttons_state
;
376 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
);
379 if (!(s
->mouse_status
& MOUSE_STATUS_REMOTE
)) {
380 while (s
->common
.queue
.count
< PS2_QUEUE_SIZE
- 4) {
381 /* if not remote, send event. Multiple events are sent if
383 ps2_mouse_send_packet(s
);
384 if (s
->mouse_dx
== 0 && s
->mouse_dy
== 0 && s
->mouse_dz
== 0)
390 void ps2_mouse_fake_event(void *opaque
)
392 ps2_mouse_event(opaque
, 1, 0, 0, 0);
395 void ps2_write_mouse(void *opaque
, int val
)
397 PS2MouseState
*s
= (PS2MouseState
*)opaque
;
399 printf("kbd: write mouse 0x%02x\n", val
);
401 switch(s
->common
.write_cmd
) {
406 if (val
== AUX_RESET_WRAP
) {
408 ps2_queue(&s
->common
, AUX_ACK
);
410 } else if (val
!= AUX_RESET
) {
411 ps2_queue(&s
->common
, val
);
416 case AUX_SET_SCALE11
:
417 s
->mouse_status
&= ~MOUSE_STATUS_SCALE21
;
418 ps2_queue(&s
->common
, AUX_ACK
);
420 case AUX_SET_SCALE21
:
421 s
->mouse_status
|= MOUSE_STATUS_SCALE21
;
422 ps2_queue(&s
->common
, AUX_ACK
);
425 s
->mouse_status
&= ~MOUSE_STATUS_REMOTE
;
426 ps2_queue(&s
->common
, AUX_ACK
);
430 ps2_queue(&s
->common
, AUX_ACK
);
433 s
->mouse_status
|= MOUSE_STATUS_REMOTE
;
434 ps2_queue(&s
->common
, AUX_ACK
);
437 ps2_queue(&s
->common
, AUX_ACK
);
438 ps2_queue(&s
->common
, s
->mouse_type
);
442 s
->common
.write_cmd
= val
;
443 ps2_queue(&s
->common
, AUX_ACK
);
446 ps2_queue(&s
->common
, AUX_ACK
);
447 ps2_queue(&s
->common
, s
->mouse_status
);
448 ps2_queue(&s
->common
, s
->mouse_resolution
);
449 ps2_queue(&s
->common
, s
->mouse_sample_rate
);
452 ps2_queue(&s
->common
, AUX_ACK
);
453 ps2_mouse_send_packet(s
);
456 s
->mouse_status
|= MOUSE_STATUS_ENABLED
;
457 ps2_queue(&s
->common
, AUX_ACK
);
459 case AUX_DISABLE_DEV
:
460 s
->mouse_status
&= ~MOUSE_STATUS_ENABLED
;
461 ps2_queue(&s
->common
, AUX_ACK
);
463 case AUX_SET_DEFAULT
:
464 s
->mouse_sample_rate
= 100;
465 s
->mouse_resolution
= 2;
467 ps2_queue(&s
->common
, AUX_ACK
);
470 s
->mouse_sample_rate
= 100;
471 s
->mouse_resolution
= 2;
474 ps2_queue(&s
->common
, AUX_ACK
);
475 ps2_queue(&s
->common
, 0xaa);
476 ps2_queue(&s
->common
, s
->mouse_type
);
483 s
->mouse_sample_rate
= val
;
484 /* detect IMPS/2 or IMEX */
485 switch(s
->mouse_detect_state
) {
489 s
->mouse_detect_state
= 1;
493 s
->mouse_detect_state
= 2;
495 s
->mouse_detect_state
= 3;
497 s
->mouse_detect_state
= 0;
501 s
->mouse_type
= 3; /* IMPS/2 */
502 s
->mouse_detect_state
= 0;
506 s
->mouse_type
= 4; /* IMEX */
507 s
->mouse_detect_state
= 0;
510 ps2_queue(&s
->common
, AUX_ACK
);
511 s
->common
.write_cmd
= -1;
514 s
->mouse_resolution
= val
;
515 ps2_queue(&s
->common
, AUX_ACK
);
516 s
->common
.write_cmd
= -1;
521 static void ps2_common_reset(PS2State
*s
)
529 s
->update_irq(s
->update_arg
, 0);
532 static void ps2_common_post_load(PS2State
*s
)
534 PS2Queue
*q
= &s
->queue
;
537 int tmp_data
[PS2_QUEUE_SIZE
];
539 /* set the useful data buffer queue size, < PS2_QUEUE_SIZE */
540 size
= q
->count
> PS2_QUEUE_SIZE
? 0 : q
->count
;
542 /* move the queue elements to the start of data array */
544 for (i
= 0; i
< size
; i
++) {
545 /* move the queue elements to the temporary buffer */
546 tmp_data
[i
] = q
->data
[q
->rptr
];
547 if (++q
->rptr
== 256) {
551 memcpy(q
->data
, tmp_data
, size
);
553 /* reset rptr/wptr/count */
557 s
->update_irq(s
->update_arg
, q
->count
!= 0);
560 static void ps2_kbd_reset(void *opaque
)
562 PS2KbdState
*s
= (PS2KbdState
*) opaque
;
564 ps2_common_reset(&s
->common
);
570 static void ps2_mouse_reset(void *opaque
)
572 PS2MouseState
*s
= (PS2MouseState
*) opaque
;
574 ps2_common_reset(&s
->common
);
576 s
->mouse_resolution
= 0;
577 s
->mouse_sample_rate
= 0;
580 s
->mouse_detect_state
= 0;
584 s
->mouse_buttons
= 0;
587 static const VMStateDescription vmstate_ps2_common
= {
588 .name
= "PS2 Common State",
590 .minimum_version_id
= 2,
591 .minimum_version_id_old
= 2,
592 .fields
= (VMStateField
[]) {
593 VMSTATE_INT32(write_cmd
, PS2State
),
594 VMSTATE_INT32(queue
.rptr
, PS2State
),
595 VMSTATE_INT32(queue
.wptr
, PS2State
),
596 VMSTATE_INT32(queue
.count
, PS2State
),
597 VMSTATE_BUFFER(queue
.data
, PS2State
),
598 VMSTATE_END_OF_LIST()
602 static bool ps2_keyboard_ledstate_needed(void *opaque
)
604 PS2KbdState
*s
= opaque
;
606 return s
->ledstate
!= 0; /* 0 is default state */
609 static int ps2_kbd_ledstate_post_load(void *opaque
, int version_id
)
611 PS2KbdState
*s
= opaque
;
613 kbd_put_ledstate(s
->ledstate
);
617 static const VMStateDescription vmstate_ps2_keyboard_ledstate
= {
618 .name
= "ps2kbd/ledstate",
620 .minimum_version_id
= 2,
621 .minimum_version_id_old
= 2,
622 .post_load
= ps2_kbd_ledstate_post_load
,
623 .fields
= (VMStateField
[]) {
624 VMSTATE_INT32(ledstate
, PS2KbdState
),
625 VMSTATE_END_OF_LIST()
629 static int ps2_kbd_post_load(void* opaque
, int version_id
)
631 PS2KbdState
*s
= (PS2KbdState
*)opaque
;
632 PS2State
*ps2
= &s
->common
;
637 ps2_common_post_load(ps2
);
642 static void ps2_kbd_pre_save(void *opaque
)
644 PS2KbdState
*s
= (PS2KbdState
*)opaque
;
645 PS2State
*ps2
= &s
->common
;
647 ps2_common_post_load(ps2
);
650 static const VMStateDescription vmstate_ps2_keyboard
= {
653 .minimum_version_id
= 2,
654 .minimum_version_id_old
= 2,
655 .post_load
= ps2_kbd_post_load
,
656 .pre_save
= ps2_kbd_pre_save
,
657 .fields
= (VMStateField
[]) {
658 VMSTATE_STRUCT(common
, PS2KbdState
, 0, vmstate_ps2_common
, PS2State
),
659 VMSTATE_INT32(scan_enabled
, PS2KbdState
),
660 VMSTATE_INT32(translate
, PS2KbdState
),
661 VMSTATE_INT32_V(scancode_set
, PS2KbdState
,3),
662 VMSTATE_END_OF_LIST()
664 .subsections
= (VMStateSubsection
[]) {
666 .vmsd
= &vmstate_ps2_keyboard_ledstate
,
667 .needed
= ps2_keyboard_ledstate_needed
,
674 static int ps2_mouse_post_load(void *opaque
, int version_id
)
676 PS2MouseState
*s
= (PS2MouseState
*)opaque
;
677 PS2State
*ps2
= &s
->common
;
679 ps2_common_post_load(ps2
);
684 static void ps2_mouse_pre_save(void *opaque
)
686 PS2MouseState
*s
= (PS2MouseState
*)opaque
;
687 PS2State
*ps2
= &s
->common
;
689 ps2_common_post_load(ps2
);
692 static const VMStateDescription vmstate_ps2_mouse
= {
695 .minimum_version_id
= 2,
696 .minimum_version_id_old
= 2,
697 .post_load
= ps2_mouse_post_load
,
698 .pre_save
= ps2_mouse_pre_save
,
699 .fields
= (VMStateField
[]) {
700 VMSTATE_STRUCT(common
, PS2MouseState
, 0, vmstate_ps2_common
, PS2State
),
701 VMSTATE_UINT8(mouse_status
, PS2MouseState
),
702 VMSTATE_UINT8(mouse_resolution
, PS2MouseState
),
703 VMSTATE_UINT8(mouse_sample_rate
, PS2MouseState
),
704 VMSTATE_UINT8(mouse_wrap
, PS2MouseState
),
705 VMSTATE_UINT8(mouse_type
, PS2MouseState
),
706 VMSTATE_UINT8(mouse_detect_state
, PS2MouseState
),
707 VMSTATE_INT32(mouse_dx
, PS2MouseState
),
708 VMSTATE_INT32(mouse_dy
, PS2MouseState
),
709 VMSTATE_INT32(mouse_dz
, PS2MouseState
),
710 VMSTATE_UINT8(mouse_buttons
, PS2MouseState
),
711 VMSTATE_END_OF_LIST()
715 void *ps2_kbd_init(void (*update_irq
)(void *, int), void *update_arg
)
717 PS2KbdState
*s
= (PS2KbdState
*)g_malloc0(sizeof(PS2KbdState
));
719 s
->common
.update_irq
= update_irq
;
720 s
->common
.update_arg
= update_arg
;
722 vmstate_register(NULL
, 0, &vmstate_ps2_keyboard
, s
);
723 qemu_add_kbd_event_handler(ps2_put_keycode
, s
);
724 qemu_register_reset(ps2_kbd_reset
, s
);
728 void *ps2_mouse_init(void (*update_irq
)(void *, int), void *update_arg
)
730 PS2MouseState
*s
= (PS2MouseState
*)g_malloc0(sizeof(PS2MouseState
));
732 s
->common
.update_irq
= update_irq
;
733 s
->common
.update_arg
= update_arg
;
734 vmstate_register(NULL
, 0, &vmstate_ps2_mouse
, s
);
735 qemu_add_mouse_event_handler(ps2_mouse_event
, s
, 0, "QEMU PS/2 Mouse");
736 qemu_register_reset(ps2_mouse_reset
, s
);