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
30 /* debug PC keyboard */
33 /* Keyboard Controller Commands */
34 #define KBD_CCMD_READ_MODE 0x20 /* Read mode bits */
35 #define KBD_CCMD_WRITE_MODE 0x60 /* Write mode bits */
36 #define KBD_CCMD_GET_VERSION 0xA1 /* Get controller version */
37 #define KBD_CCMD_MOUSE_DISABLE 0xA7 /* Disable mouse interface */
38 #define KBD_CCMD_MOUSE_ENABLE 0xA8 /* Enable mouse interface */
39 #define KBD_CCMD_TEST_MOUSE 0xA9 /* Mouse interface test */
40 #define KBD_CCMD_SELF_TEST 0xAA /* Controller self test */
41 #define KBD_CCMD_KBD_TEST 0xAB /* Keyboard interface test */
42 #define KBD_CCMD_KBD_DISABLE 0xAD /* Keyboard interface disable */
43 #define KBD_CCMD_KBD_ENABLE 0xAE /* Keyboard interface enable */
44 #define KBD_CCMD_READ_INPORT 0xC0 /* read input port */
45 #define KBD_CCMD_READ_OUTPORT 0xD0 /* read output port */
46 #define KBD_CCMD_WRITE_OUTPORT 0xD1 /* write output port */
47 #define KBD_CCMD_WRITE_OBUF 0xD2
48 #define KBD_CCMD_WRITE_AUX_OBUF 0xD3 /* Write to output buffer as if
49 initiated by the auxiliary device */
50 #define KBD_CCMD_WRITE_MOUSE 0xD4 /* Write the following byte to the mouse */
51 #define KBD_CCMD_DISABLE_A20 0xDD /* HP vectra only ? */
52 #define KBD_CCMD_ENABLE_A20 0xDF /* HP vectra only ? */
53 #define KBD_CCMD_RESET 0xFE
55 /* Keyboard Commands */
56 #define KBD_CMD_SET_LEDS 0xED /* Set keyboard leds */
57 #define KBD_CMD_ECHO 0xEE
58 #define KBD_CMD_GET_ID 0xF2 /* get keyboard ID */
59 #define KBD_CMD_SET_RATE 0xF3 /* Set typematic rate */
60 #define KBD_CMD_ENABLE 0xF4 /* Enable scanning */
61 #define KBD_CMD_RESET_DISABLE 0xF5 /* reset and disable scanning */
62 #define KBD_CMD_RESET_ENABLE 0xF6 /* reset and enable scanning */
63 #define KBD_CMD_RESET 0xFF /* Reset */
65 /* Keyboard Replies */
66 #define KBD_REPLY_POR 0xAA /* Power on reset */
67 #define KBD_REPLY_ACK 0xFA /* Command ACK */
68 #define KBD_REPLY_RESEND 0xFE /* Command NACK, send the cmd again */
70 /* Status Register Bits */
71 #define KBD_STAT_OBF 0x01 /* Keyboard output buffer full */
72 #define KBD_STAT_IBF 0x02 /* Keyboard input buffer full */
73 #define KBD_STAT_SELFTEST 0x04 /* Self test successful */
74 #define KBD_STAT_CMD 0x08 /* Last write was a command write (0=data) */
75 #define KBD_STAT_UNLOCKED 0x10 /* Zero if keyboard locked */
76 #define KBD_STAT_MOUSE_OBF 0x20 /* Mouse output buffer full */
77 #define KBD_STAT_GTO 0x40 /* General receive/xmit timeout */
78 #define KBD_STAT_PERR 0x80 /* Parity error */
80 /* Controller Mode Register Bits */
81 #define KBD_MODE_KBD_INT 0x01 /* Keyboard data generate IRQ1 */
82 #define KBD_MODE_MOUSE_INT 0x02 /* Mouse data generate IRQ12 */
83 #define KBD_MODE_SYS 0x04 /* The system flag (?) */
84 #define KBD_MODE_NO_KEYLOCK 0x08 /* The keylock doesn't affect the keyboard if set */
85 #define KBD_MODE_DISABLE_KBD 0x10 /* Disable keyboard interface */
86 #define KBD_MODE_DISABLE_MOUSE 0x20 /* Disable mouse interface */
87 #define KBD_MODE_KCC 0x40 /* Scan code conversion to PC format */
88 #define KBD_MODE_RFU 0x80
90 /* Output Port Bits */
91 #define KBD_OUT_RESET 0x01 /* 1=normal mode, 0=reset */
92 #define KBD_OUT_A20 0x02 /* x86 only */
93 #define KBD_OUT_OBF 0x10 /* Keyboard output buffer full */
94 #define KBD_OUT_MOUSE_OBF 0x20 /* Mouse output buffer full */
97 #define AUX_SET_SCALE11 0xE6 /* Set 1:1 scaling */
98 #define AUX_SET_SCALE21 0xE7 /* Set 2:1 scaling */
99 #define AUX_SET_RES 0xE8 /* Set resolution */
100 #define AUX_GET_SCALE 0xE9 /* Get scaling factor */
101 #define AUX_SET_STREAM 0xEA /* Set stream mode */
102 #define AUX_POLL 0xEB /* Poll */
103 #define AUX_RESET_WRAP 0xEC /* Reset wrap mode */
104 #define AUX_SET_WRAP 0xEE /* Set wrap mode */
105 #define AUX_SET_REMOTE 0xF0 /* Set remote mode */
106 #define AUX_GET_TYPE 0xF2 /* Get type */
107 #define AUX_SET_SAMPLE 0xF3 /* Set sample rate */
108 #define AUX_ENABLE_DEV 0xF4 /* Enable aux device */
109 #define AUX_DISABLE_DEV 0xF5 /* Disable aux device */
110 #define AUX_SET_DEFAULT 0xF6
111 #define AUX_RESET 0xFF /* Reset aux device */
112 #define AUX_ACK 0xFA /* Command byte ACK. */
114 #define MOUSE_STATUS_REMOTE 0x40
115 #define MOUSE_STATUS_ENABLED 0x20
116 #define MOUSE_STATUS_SCALE21 0x10
118 #define KBD_PENDING_KBD 1
119 #define KBD_PENDING_AUX 2
121 typedef struct KBDState
{
122 uint8_t write_cmd
; /* if non zero, write data to port 60 is expected */
126 /* Bitmask of devices with data available. */
134 target_phys_addr_t mask
;
137 /* update irq and KBD_STAT_[MOUSE_]OBF */
138 /* XXX: not generating the irqs if KBD_MODE_DISABLE_KBD is set may be
139 incorrect, but it avoids having to simulate exact delays */
140 static void kbd_update_irq(KBDState
*s
)
142 int irq_kbd_level
, irq_mouse_level
;
146 s
->status
&= ~(KBD_STAT_OBF
| KBD_STAT_MOUSE_OBF
);
147 s
->outport
&= ~(KBD_OUT_OBF
| KBD_OUT_MOUSE_OBF
);
149 s
->status
|= KBD_STAT_OBF
;
150 s
->outport
|= KBD_OUT_OBF
;
151 /* kbd data takes priority over aux data. */
152 if (s
->pending
== KBD_PENDING_AUX
) {
153 s
->status
|= KBD_STAT_MOUSE_OBF
;
154 s
->outport
|= KBD_OUT_MOUSE_OBF
;
155 if (s
->mode
& KBD_MODE_MOUSE_INT
)
158 if ((s
->mode
& KBD_MODE_KBD_INT
) &&
159 !(s
->mode
& KBD_MODE_DISABLE_KBD
))
163 qemu_set_irq(s
->irq_kbd
, irq_kbd_level
);
164 qemu_set_irq(s
->irq_mouse
, irq_mouse_level
);
167 static void kbd_update_kbd_irq(void *opaque
, int level
)
169 KBDState
*s
= (KBDState
*)opaque
;
172 s
->pending
|= KBD_PENDING_KBD
;
174 s
->pending
&= ~KBD_PENDING_KBD
;
178 static void kbd_update_aux_irq(void *opaque
, int level
)
180 KBDState
*s
= (KBDState
*)opaque
;
183 s
->pending
|= KBD_PENDING_AUX
;
185 s
->pending
&= ~KBD_PENDING_AUX
;
189 static uint32_t kbd_read_status(void *opaque
, uint32_t addr
)
191 KBDState
*s
= opaque
;
194 #if defined(DEBUG_KBD)
195 printf("kbd: read status=0x%02x\n", val
);
200 static void kbd_queue(KBDState
*s
, int b
, int aux
)
203 ps2_queue(s
->mouse
, b
);
205 ps2_queue(s
->kbd
, b
);
208 static void ioport92_write(void *opaque
, uint32_t addr
, uint32_t val
)
210 KBDState
*s
= opaque
;
214 qemu_set_irq(*s
->a20_out
, (val
>> 1) & 1);
217 qemu_system_reset_request();
221 static uint32_t ioport92_read(void *opaque
, uint32_t addr
)
223 KBDState
*s
= opaque
;
228 static void kbd_write_command(void *opaque
, uint32_t addr
, uint32_t val
)
230 KBDState
*s
= opaque
;
233 printf("kbd: write cmd=0x%02x\n", val
);
236 case KBD_CCMD_READ_MODE
:
237 kbd_queue(s
, s
->mode
, 0);
239 case KBD_CCMD_WRITE_MODE
:
240 case KBD_CCMD_WRITE_OBUF
:
241 case KBD_CCMD_WRITE_AUX_OBUF
:
242 case KBD_CCMD_WRITE_MOUSE
:
243 case KBD_CCMD_WRITE_OUTPORT
:
246 case KBD_CCMD_MOUSE_DISABLE
:
247 s
->mode
|= KBD_MODE_DISABLE_MOUSE
;
249 case KBD_CCMD_MOUSE_ENABLE
:
250 s
->mode
&= ~KBD_MODE_DISABLE_MOUSE
;
252 case KBD_CCMD_TEST_MOUSE
:
253 kbd_queue(s
, 0x00, 0);
255 case KBD_CCMD_SELF_TEST
:
256 s
->status
|= KBD_STAT_SELFTEST
;
257 kbd_queue(s
, 0x55, 0);
259 case KBD_CCMD_KBD_TEST
:
260 kbd_queue(s
, 0x00, 0);
262 case KBD_CCMD_KBD_DISABLE
:
263 s
->mode
|= KBD_MODE_DISABLE_KBD
;
266 case KBD_CCMD_KBD_ENABLE
:
267 s
->mode
&= ~KBD_MODE_DISABLE_KBD
;
270 case KBD_CCMD_READ_INPORT
:
271 kbd_queue(s
, 0x00, 0);
273 case KBD_CCMD_READ_OUTPORT
:
274 kbd_queue(s
, s
->outport
, 0);
276 case KBD_CCMD_ENABLE_A20
:
278 qemu_irq_raise(*s
->a20_out
);
280 s
->outport
|= KBD_OUT_A20
;
282 case KBD_CCMD_DISABLE_A20
:
284 qemu_irq_lower(*s
->a20_out
);
286 s
->outport
&= ~KBD_OUT_A20
;
289 qemu_system_reset_request();
292 /* ignore that - I don't know what is its use */
295 fprintf(stderr
, "qemu: unsupported keyboard cmd=0x%02x\n", val
);
300 static uint32_t kbd_read_data(void *opaque
, uint32_t addr
)
302 KBDState
*s
= opaque
;
305 if (s
->pending
== KBD_PENDING_AUX
)
306 val
= ps2_read_data(s
->mouse
);
308 val
= ps2_read_data(s
->kbd
);
310 #if defined(DEBUG_KBD)
311 printf("kbd: read data=0x%02x\n", val
);
316 static void kbd_write_data(void *opaque
, uint32_t addr
, uint32_t val
)
318 KBDState
*s
= opaque
;
321 printf("kbd: write data=0x%02x\n", val
);
324 switch(s
->write_cmd
) {
326 ps2_write_keyboard(s
->kbd
, val
);
328 case KBD_CCMD_WRITE_MODE
:
330 ps2_keyboard_set_translation(s
->kbd
, (s
->mode
& KBD_MODE_KCC
) != 0);
334 case KBD_CCMD_WRITE_OBUF
:
335 kbd_queue(s
, val
, 0);
337 case KBD_CCMD_WRITE_AUX_OBUF
:
338 kbd_queue(s
, val
, 1);
340 case KBD_CCMD_WRITE_OUTPORT
:
341 ioport92_write(s
, 0, val
);
343 case KBD_CCMD_WRITE_MOUSE
:
344 ps2_write_mouse(s
->mouse
, val
);
352 static void kbd_reset(void *opaque
)
354 KBDState
*s
= opaque
;
356 s
->mode
= KBD_MODE_KBD_INT
| KBD_MODE_MOUSE_INT
;
357 s
->status
= KBD_STAT_CMD
| KBD_STAT_UNLOCKED
;
358 s
->outport
= KBD_OUT_RESET
| KBD_OUT_A20
;
361 static const VMStateDescription vmstate_kbd
= {
364 .minimum_version_id
= 3,
365 .minimum_version_id_old
= 3,
366 .fields
= (VMStateField
[]) {
367 VMSTATE_UINT8(write_cmd
, KBDState
),
368 VMSTATE_UINT8(status
, KBDState
),
369 VMSTATE_UINT8(mode
, KBDState
),
370 VMSTATE_UINT8(pending
, KBDState
),
371 VMSTATE_END_OF_LIST()
375 /* Memory mapped interface */
376 static uint32_t kbd_mm_readb (void *opaque
, target_phys_addr_t addr
)
378 KBDState
*s
= opaque
;
381 return kbd_read_status(s
, 0) & 0xff;
383 return kbd_read_data(s
, 0) & 0xff;
386 static void kbd_mm_writeb (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
388 KBDState
*s
= opaque
;
391 kbd_write_command(s
, 0, value
& 0xff);
393 kbd_write_data(s
, 0, value
& 0xff);
396 static CPUReadMemoryFunc
* const kbd_mm_read
[] = {
402 static CPUWriteMemoryFunc
* const kbd_mm_write
[] = {
408 void i8042_mm_init(qemu_irq kbd_irq
, qemu_irq mouse_irq
,
409 target_phys_addr_t base
, ram_addr_t size
,
410 target_phys_addr_t mask
)
412 KBDState
*s
= qemu_mallocz(sizeof(KBDState
));
415 s
->irq_kbd
= kbd_irq
;
416 s
->irq_mouse
= mouse_irq
;
419 vmstate_register(0, &vmstate_kbd
, s
);
420 s_io_memory
= cpu_register_io_memory(kbd_mm_read
, kbd_mm_write
, s
);
421 cpu_register_physical_memory(base
, size
, s_io_memory
);
423 s
->kbd
= ps2_kbd_init(kbd_update_kbd_irq
, s
);
424 s
->mouse
= ps2_mouse_init(kbd_update_aux_irq
, s
);
425 qemu_register_reset(kbd_reset
, s
);
428 typedef struct ISAKBDState
{
433 void i8042_isa_mouse_fake_event(void *opaque
)
435 ISADevice
*dev
= opaque
;
436 KBDState
*s
= &(DO_UPCAST(ISAKBDState
, dev
, dev
)->kbd
);
438 ps2_mouse_fake_event(s
->mouse
);
441 void i8042_setup_a20_line(ISADevice
*dev
, qemu_irq
*a20_out
)
443 KBDState
*s
= &(DO_UPCAST(ISAKBDState
, dev
, dev
)->kbd
);
445 s
->a20_out
= a20_out
;
448 static const VMStateDescription vmstate_kbd_isa
= {
451 .minimum_version_id
= 3,
452 .minimum_version_id_old
= 3,
453 .fields
= (VMStateField
[]) {
454 VMSTATE_STRUCT(kbd
, ISAKBDState
, 0, vmstate_kbd
, KBDState
),
455 VMSTATE_END_OF_LIST()
459 static int i8042_initfn(ISADevice
*dev
)
461 KBDState
*s
= &(DO_UPCAST(ISAKBDState
, dev
, dev
)->kbd
);
463 isa_init_irq(dev
, &s
->irq_kbd
, 1);
464 isa_init_irq(dev
, &s
->irq_mouse
, 12);
466 register_ioport_read(0x60, 1, 1, kbd_read_data
, s
);
467 register_ioport_write(0x60, 1, 1, kbd_write_data
, s
);
468 register_ioport_read(0x64, 1, 1, kbd_read_status
, s
);
469 register_ioport_write(0x64, 1, 1, kbd_write_command
, s
);
470 register_ioport_read(0x92, 1, 1, ioport92_read
, s
);
471 register_ioport_write(0x92, 1, 1, ioport92_write
, s
);
473 s
->kbd
= ps2_kbd_init(kbd_update_kbd_irq
, s
);
474 s
->mouse
= ps2_mouse_init(kbd_update_aux_irq
, s
);
475 qemu_register_reset(kbd_reset
, s
);
479 static ISADeviceInfo i8042_info
= {
480 .qdev
.name
= "i8042",
481 .qdev
.size
= sizeof(ISAKBDState
),
482 .qdev
.vmsd
= &vmstate_kbd_isa
,
484 .init
= i8042_initfn
,
487 static void i8042_register(void)
489 isa_qdev_register(&i8042_info
);
491 device_init(i8042_register
)