2 * QEMU PC keyboard 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
26 /* debug PC keyboard */
29 /* debug PC keyboard : only mouse */
32 /* Keyboard Controller Commands */
33 #define KBD_CCMD_READ_MODE 0x20 /* Read mode bits */
34 #define KBD_CCMD_WRITE_MODE 0x60 /* Write mode bits */
35 #define KBD_CCMD_GET_VERSION 0xA1 /* Get controller version */
36 #define KBD_CCMD_MOUSE_DISABLE 0xA7 /* Disable mouse interface */
37 #define KBD_CCMD_MOUSE_ENABLE 0xA8 /* Enable mouse interface */
38 #define KBD_CCMD_TEST_MOUSE 0xA9 /* Mouse interface test */
39 #define KBD_CCMD_SELF_TEST 0xAA /* Controller self test */
40 #define KBD_CCMD_KBD_TEST 0xAB /* Keyboard interface test */
41 #define KBD_CCMD_KBD_DISABLE 0xAD /* Keyboard interface disable */
42 #define KBD_CCMD_KBD_ENABLE 0xAE /* Keyboard interface enable */
43 #define KBD_CCMD_READ_INPORT 0xC0 /* read input port */
44 #define KBD_CCMD_READ_OUTPORT 0xD0 /* read output port */
45 #define KBD_CCMD_WRITE_OUTPORT 0xD1 /* write output port */
46 #define KBD_CCMD_WRITE_OBUF 0xD2
47 #define KBD_CCMD_WRITE_AUX_OBUF 0xD3 /* Write to output buffer as if
48 initiated by the auxiliary device */
49 #define KBD_CCMD_WRITE_MOUSE 0xD4 /* Write the following byte to the mouse */
50 #define KBD_CCMD_DISABLE_A20 0xDD /* HP vectra only ? */
51 #define KBD_CCMD_ENABLE_A20 0xDF /* HP vectra only ? */
52 #define KBD_CCMD_RESET 0xFE
54 /* Keyboard Commands */
55 #define KBD_CMD_SET_LEDS 0xED /* Set keyboard leds */
56 #define KBD_CMD_ECHO 0xEE
57 #define KBD_CMD_GET_ID 0xF2 /* get keyboard ID */
58 #define KBD_CMD_SET_RATE 0xF3 /* Set typematic rate */
59 #define KBD_CMD_ENABLE 0xF4 /* Enable scanning */
60 #define KBD_CMD_RESET_DISABLE 0xF5 /* reset and disable scanning */
61 #define KBD_CMD_RESET_ENABLE 0xF6 /* reset and enable scanning */
62 #define KBD_CMD_RESET 0xFF /* Reset */
64 /* Keyboard Replies */
65 #define KBD_REPLY_POR 0xAA /* Power on reset */
66 #define KBD_REPLY_ACK 0xFA /* Command ACK */
67 #define KBD_REPLY_RESEND 0xFE /* Command NACK, send the cmd again */
69 /* Status Register Bits */
70 #define KBD_STAT_OBF 0x01 /* Keyboard output buffer full */
71 #define KBD_STAT_IBF 0x02 /* Keyboard input buffer full */
72 #define KBD_STAT_SELFTEST 0x04 /* Self test successful */
73 #define KBD_STAT_CMD 0x08 /* Last write was a command write (0=data) */
74 #define KBD_STAT_UNLOCKED 0x10 /* Zero if keyboard locked */
75 #define KBD_STAT_MOUSE_OBF 0x20 /* Mouse output buffer full */
76 #define KBD_STAT_GTO 0x40 /* General receive/xmit timeout */
77 #define KBD_STAT_PERR 0x80 /* Parity error */
79 /* Controller Mode Register Bits */
80 #define KBD_MODE_KBD_INT 0x01 /* Keyboard data generate IRQ1 */
81 #define KBD_MODE_MOUSE_INT 0x02 /* Mouse data generate IRQ12 */
82 #define KBD_MODE_SYS 0x04 /* The system flag (?) */
83 #define KBD_MODE_NO_KEYLOCK 0x08 /* The keylock doesn't affect the keyboard if set */
84 #define KBD_MODE_DISABLE_KBD 0x10 /* Disable keyboard interface */
85 #define KBD_MODE_DISABLE_MOUSE 0x20 /* Disable mouse interface */
86 #define KBD_MODE_KCC 0x40 /* Scan code conversion to PC format */
87 #define KBD_MODE_RFU 0x80
90 #define AUX_SET_SCALE11 0xE6 /* Set 1:1 scaling */
91 #define AUX_SET_SCALE21 0xE7 /* Set 2:1 scaling */
92 #define AUX_SET_RES 0xE8 /* Set resolution */
93 #define AUX_GET_SCALE 0xE9 /* Get scaling factor */
94 #define AUX_SET_STREAM 0xEA /* Set stream mode */
95 #define AUX_POLL 0xEB /* Poll */
96 #define AUX_RESET_WRAP 0xEC /* Reset wrap mode */
97 #define AUX_SET_WRAP 0xEE /* Set wrap mode */
98 #define AUX_SET_REMOTE 0xF0 /* Set remote mode */
99 #define AUX_GET_TYPE 0xF2 /* Get type */
100 #define AUX_SET_SAMPLE 0xF3 /* Set sample rate */
101 #define AUX_ENABLE_DEV 0xF4 /* Enable aux device */
102 #define AUX_DISABLE_DEV 0xF5 /* Disable aux device */
103 #define AUX_SET_DEFAULT 0xF6
104 #define AUX_RESET 0xFF /* Reset aux device */
105 #define AUX_ACK 0xFA /* Command byte ACK. */
107 #define MOUSE_STATUS_REMOTE 0x40
108 #define MOUSE_STATUS_ENABLED 0x20
109 #define MOUSE_STATUS_SCALE21 0x10
111 #define KBD_QUEUE_SIZE 256
114 uint8_t aux[KBD_QUEUE_SIZE];
115 uint8_t data[KBD_QUEUE_SIZE];
116 int rptr, wptr, count;
119 typedef struct KBDState {
121 uint8_t write_cmd; /* if non zero, write data to port 60 is expected */
129 uint8_t mouse_status;
130 uint8_t mouse_resolution;
131 uint8_t mouse_sample_rate;
133 uint8_t mouse_type; /* 0 = PS2, 3 = IMPS/2, 4 = IMEX */
134 uint8_t mouse_detect_state;
135 int mouse_dx; /* current values, needed for 'poll' mode */
138 uint8_t mouse_buttons;
144 /* update irq and KBD_STAT_[MOUSE_]OBF */
145 /* XXX: not generating the irqs if KBD_MODE_DISABLE_KBD is set may be
146 incorrect, but it avoids having to simulate exact delays */
147 static void kbd_update_irq(KBDState *s)
149 KBDQueue *q = &s->queue;
150 int irq12_level, irq1_level;
154 s->status &= ~(KBD_STAT_OBF | KBD_STAT_MOUSE_OBF);
156 s->status |= KBD_STAT_OBF;
157 if (q->aux[q->rptr]) {
158 s->status |= KBD_STAT_MOUSE_OBF;
159 if (s->mode & KBD_MODE_MOUSE_INT)
162 if ((s->mode & KBD_MODE_KBD_INT) &&
163 !(s->mode & KBD_MODE_DISABLE_KBD))
167 pic_set_irq(1, irq1_level);
168 pic_set_irq(12, irq12_level);
171 static void kbd_queue(KBDState *s, int b, int aux)
173 KBDQueue *q = &s->queue;
175 #if defined(DEBUG_MOUSE) || defined(DEBUG_KBD)
177 printf("mouse event: 0x%02x\n", b);
180 printf("kbd event: 0x%02x\n", b);
183 if (q->count >= KBD_QUEUE_SIZE)
185 q->aux[q->wptr] = aux;
186 q->data[q->wptr] = b;
187 if (++q->wptr == KBD_QUEUE_SIZE)
193 static void pc_kbd_put_keycode(void *opaque, int keycode)
195 KBDState *s = opaque;
196 kbd_queue(s, keycode, 0);
199 static uint32_t kbd_read_status(void *opaque, uint32_t addr)
201 KBDState *s = opaque;
204 #if defined(DEBUG_KBD)
205 printf("kbd: read status=0x%02x\n", val);
210 static void kbd_write_command(void *opaque, uint32_t addr, uint32_t val)
212 KBDState *s = opaque;
215 printf("kbd: write cmd=0x%02x\n", val);
218 case KBD_CCMD_READ_MODE:
219 kbd_queue(s, s->mode, 0);
221 case KBD_CCMD_WRITE_MODE:
222 case KBD_CCMD_WRITE_OBUF:
223 case KBD_CCMD_WRITE_AUX_OBUF:
224 case KBD_CCMD_WRITE_MOUSE:
225 case KBD_CCMD_WRITE_OUTPORT:
228 case KBD_CCMD_MOUSE_DISABLE:
229 s->mode |= KBD_MODE_DISABLE_MOUSE;
231 case KBD_CCMD_MOUSE_ENABLE:
232 s->mode &= ~KBD_MODE_DISABLE_MOUSE;
234 case KBD_CCMD_TEST_MOUSE:
235 kbd_queue(s, 0x00, 0);
237 case KBD_CCMD_SELF_TEST:
238 s->status |= KBD_STAT_SELFTEST;
239 kbd_queue(s, 0x55, 0);
241 case KBD_CCMD_KBD_TEST:
242 kbd_queue(s, 0x00, 0);
244 case KBD_CCMD_KBD_DISABLE:
245 s->mode |= KBD_MODE_DISABLE_KBD;
248 case KBD_CCMD_KBD_ENABLE:
249 s->mode &= ~KBD_MODE_DISABLE_KBD;
252 case KBD_CCMD_READ_INPORT:
253 kbd_queue(s, 0x00, 0);
255 case KBD_CCMD_READ_OUTPORT:
256 /* XXX: check that */
258 val = 0x01 | (((cpu_single_env->a20_mask >> 20) & 1) << 1);
262 if (s->status & KBD_STAT_OBF)
264 if (s->status & KBD_STAT_MOUSE_OBF)
266 kbd_queue(s, val, 0);
269 case KBD_CCMD_ENABLE_A20:
270 cpu_x86_set_a20(cpu_single_env, 1);
272 case KBD_CCMD_DISABLE_A20:
273 cpu_x86_set_a20(cpu_single_env, 0);
278 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
281 /* ignore that - I don't know what is its use */
284 fprintf(stderr, "qemu: unsupported keyboard cmd=0x%02x\n", val);
289 static uint32_t kbd_read_data(void *opaque, uint32_t addr)
291 KBDState *s = opaque;
297 /* NOTE: if no data left, we return the last keyboard one
298 (needed for EMM386) */
299 /* XXX: need a timer to do things correctly */
302 index = KBD_QUEUE_SIZE - 1;
303 val = q->data[index];
305 aux = q->aux[q->rptr];
306 val = q->data[q->rptr];
307 if (++q->rptr == KBD_QUEUE_SIZE)
310 /* reading deasserts IRQ */
316 /* reassert IRQs if data left */
319 printf("kbd: read data=0x%02x\n", val);
324 static void kbd_reset_keyboard(KBDState *s)
329 static void kbd_write_keyboard(KBDState *s, int val)
331 switch(s->kbd_write_cmd) {
336 kbd_queue(s, KBD_REPLY_ACK, 0);
339 kbd_queue(s, KBD_REPLY_RESEND, 0);
342 kbd_queue(s, KBD_REPLY_ACK, 0);
343 kbd_queue(s, 0xab, 0);
344 kbd_queue(s, 0x83, 0);
347 kbd_queue(s, KBD_CMD_ECHO, 0);
351 kbd_queue(s, KBD_REPLY_ACK, 0);
353 case KBD_CMD_SET_LEDS:
354 case KBD_CMD_SET_RATE:
355 s->kbd_write_cmd = val;
356 kbd_queue(s, KBD_REPLY_ACK, 0);
358 case KBD_CMD_RESET_DISABLE:
359 kbd_reset_keyboard(s);
361 kbd_queue(s, KBD_REPLY_ACK, 0);
363 case KBD_CMD_RESET_ENABLE:
364 kbd_reset_keyboard(s);
366 kbd_queue(s, KBD_REPLY_ACK, 0);
369 kbd_reset_keyboard(s);
370 kbd_queue(s, KBD_REPLY_ACK, 0);
371 kbd_queue(s, KBD_REPLY_POR, 0);
374 kbd_queue(s, KBD_REPLY_ACK, 0);
378 case KBD_CMD_SET_LEDS:
379 kbd_queue(s, KBD_REPLY_ACK, 0);
380 s->kbd_write_cmd = -1;
382 case KBD_CMD_SET_RATE:
383 kbd_queue(s, KBD_REPLY_ACK, 0);
384 s->kbd_write_cmd = -1;
389 static void kbd_mouse_send_packet(KBDState *s)
397 /* XXX: increase range to 8 bits ? */
406 b = 0x08 | ((dx1 < 0) << 4) | ((dy1 < 0) << 5) | (s->mouse_buttons & 0x07);
408 kbd_queue(s, dx1 & 0xff, 1);
409 kbd_queue(s, dy1 & 0xff, 1);
410 /* extra byte for IMPS/2 or IMEX */
411 switch(s->mouse_type) {
419 kbd_queue(s, dz1 & 0xff, 1);
426 b = (dz1 & 0x0f) | ((s->mouse_buttons & 0x18) << 1);
437 static void pc_kbd_mouse_event(void *opaque,
438 int dx, int dy, int dz, int buttons_state)
440 KBDState *s = opaque;
442 /* check if deltas are recorded when disabled */
443 if (!(s->mouse_status & MOUSE_STATUS_ENABLED))
449 /* XXX: SDL sometimes generates nul events: we delete them */
450 if (s->mouse_dx == 0 && s->mouse_dy == 0 && s->mouse_dz == 0 &&
451 s->mouse_buttons == buttons_state)
453 s->mouse_buttons = buttons_state;
455 if (!(s->mouse_status & MOUSE_STATUS_REMOTE) &&
456 (s->queue.count < (KBD_QUEUE_SIZE - 16))) {
458 /* if not remote, send event. Multiple events are sent if
460 kbd_mouse_send_packet(s);
461 if (s->mouse_dx == 0 && s->mouse_dy == 0 && s->mouse_dz == 0)
467 static void kbd_write_mouse(KBDState *s, int val)
470 printf("kbd: write mouse 0x%02x\n", val);
472 switch(s->mouse_write_cmd) {
477 if (val == AUX_RESET_WRAP) {
479 kbd_queue(s, AUX_ACK, 1);
481 } else if (val != AUX_RESET) {
482 kbd_queue(s, val, 1);
487 case AUX_SET_SCALE11:
488 s->mouse_status &= ~MOUSE_STATUS_SCALE21;
489 kbd_queue(s, AUX_ACK, 1);
491 case AUX_SET_SCALE21:
492 s->mouse_status |= MOUSE_STATUS_SCALE21;
493 kbd_queue(s, AUX_ACK, 1);
496 s->mouse_status &= ~MOUSE_STATUS_REMOTE;
497 kbd_queue(s, AUX_ACK, 1);
501 kbd_queue(s, AUX_ACK, 1);
504 s->mouse_status |= MOUSE_STATUS_REMOTE;
505 kbd_queue(s, AUX_ACK, 1);
508 kbd_queue(s, AUX_ACK, 1);
509 kbd_queue(s, s->mouse_type, 1);
513 s->mouse_write_cmd = val;
514 kbd_queue(s, AUX_ACK, 1);
517 kbd_queue(s, AUX_ACK, 1);
518 kbd_queue(s, s->mouse_status, 1);
519 kbd_queue(s, s->mouse_resolution, 1);
520 kbd_queue(s, s->mouse_sample_rate, 1);
523 kbd_queue(s, AUX_ACK, 1);
524 kbd_mouse_send_packet(s);
527 s->mouse_status |= MOUSE_STATUS_ENABLED;
528 kbd_queue(s, AUX_ACK, 1);
530 case AUX_DISABLE_DEV:
531 s->mouse_status &= ~MOUSE_STATUS_ENABLED;
532 kbd_queue(s, AUX_ACK, 1);
534 case AUX_SET_DEFAULT:
535 s->mouse_sample_rate = 100;
536 s->mouse_resolution = 2;
538 kbd_queue(s, AUX_ACK, 1);
541 s->mouse_sample_rate = 100;
542 s->mouse_resolution = 2;
544 kbd_queue(s, AUX_ACK, 1);
545 kbd_queue(s, 0xaa, 1);
546 kbd_queue(s, s->mouse_type, 1);
553 s->mouse_sample_rate = val;
555 /* detect IMPS/2 or IMEX */
556 switch(s->mouse_detect_state) {
560 s->mouse_detect_state = 1;
564 s->mouse_detect_state = 2;
566 s->mouse_detect_state = 3;
568 s->mouse_detect_state = 0;
572 s->mouse_type = 3; /* IMPS/2 */
573 s->mouse_detect_state = 0;
577 s->mouse_type = 4; /* IMEX */
578 s->mouse_detect_state = 0;
582 kbd_queue(s, AUX_ACK, 1);
583 s->mouse_write_cmd = -1;
586 s->mouse_resolution = val;
587 kbd_queue(s, AUX_ACK, 1);
588 s->mouse_write_cmd = -1;
593 void kbd_write_data(void *opaque, uint32_t addr, uint32_t val)
595 KBDState *s = opaque;
598 printf("kbd: write data=0x%02x\n", val);
601 switch(s->write_cmd) {
603 kbd_write_keyboard(s, val);
605 case KBD_CCMD_WRITE_MODE:
609 case KBD_CCMD_WRITE_OBUF:
610 kbd_queue(s, val, 0);
612 case KBD_CCMD_WRITE_AUX_OBUF:
613 kbd_queue(s, val, 1);
615 case KBD_CCMD_WRITE_OUTPORT:
617 cpu_x86_set_a20(cpu_single_env, (val >> 1) & 1);
621 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
624 case KBD_CCMD_WRITE_MOUSE:
625 kbd_write_mouse(s, val);
633 void kbd_reset(KBDState *s)
637 s->kbd_write_cmd = -1;
638 s->mouse_write_cmd = -1;
639 s->mode = KBD_MODE_KBD_INT | KBD_MODE_MOUSE_INT;
640 s->status = KBD_STAT_CMD | KBD_STAT_UNLOCKED;
649 KBDState *s = &kbd_state;
652 register_ioport_read(0x60, 1, 1, kbd_read_data, s);
653 register_ioport_write(0x60, 1, 1, kbd_write_data, s);
654 register_ioport_read(0x64, 1, 1, kbd_read_status, s);
655 register_ioport_write(0x64, 1, 1, kbd_write_command, s);
657 qemu_add_kbd_event_handler(pc_kbd_put_keycode, s);
658 qemu_add_mouse_event_handler(pc_kbd_mouse_event, s);