blob: 3bad09baf28e830e5fd11ee7b8cc709e54fdd038 [file] [log] [blame]
bellard80cabfa2004-03-14 12:20:30 +00001/*
2 * QEMU PC keyboard emulation
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard80cabfa2004-03-14 12:20:30 +00004 * Copyright (c) 2003 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard80cabfa2004-03-14 12:20:30 +00006 * 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 */
pbrook87ecb682007-11-17 17:14:51 +000024#include "hw.h"
25#include "isa.h"
26#include "pc.h"
27#include "ps2.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010028#include "sysemu/sysemu.h"
bellard80cabfa2004-03-14 12:20:30 +000029
30/* debug PC keyboard */
31//#define DEBUG_KBD
Blue Swirlc86d2c22010-05-22 07:59:06 +000032#ifdef DEBUG_KBD
33#define DPRINTF(fmt, ...) \
34 do { printf("KBD: " fmt , ## __VA_ARGS__); } while (0)
35#else
36#define DPRINTF(fmt, ...)
37#endif
bellard80cabfa2004-03-14 12:20:30 +000038
bellard80cabfa2004-03-14 12:20:30 +000039/* Keyboard Controller Commands */
40#define KBD_CCMD_READ_MODE 0x20 /* Read mode bits */
41#define KBD_CCMD_WRITE_MODE 0x60 /* Write mode bits */
42#define KBD_CCMD_GET_VERSION 0xA1 /* Get controller version */
43#define KBD_CCMD_MOUSE_DISABLE 0xA7 /* Disable mouse interface */
44#define KBD_CCMD_MOUSE_ENABLE 0xA8 /* Enable mouse interface */
45#define KBD_CCMD_TEST_MOUSE 0xA9 /* Mouse interface test */
46#define KBD_CCMD_SELF_TEST 0xAA /* Controller self test */
47#define KBD_CCMD_KBD_TEST 0xAB /* Keyboard interface test */
48#define KBD_CCMD_KBD_DISABLE 0xAD /* Keyboard interface disable */
49#define KBD_CCMD_KBD_ENABLE 0xAE /* Keyboard interface enable */
50#define KBD_CCMD_READ_INPORT 0xC0 /* read input port */
51#define KBD_CCMD_READ_OUTPORT 0xD0 /* read output port */
52#define KBD_CCMD_WRITE_OUTPORT 0xD1 /* write output port */
53#define KBD_CCMD_WRITE_OBUF 0xD2
54#define KBD_CCMD_WRITE_AUX_OBUF 0xD3 /* Write to output buffer as if
55 initiated by the auxiliary device */
56#define KBD_CCMD_WRITE_MOUSE 0xD4 /* Write the following byte to the mouse */
57#define KBD_CCMD_DISABLE_A20 0xDD /* HP vectra only ? */
58#define KBD_CCMD_ENABLE_A20 0xDF /* HP vectra only ? */
Bernhard Kohl5ccaa4c2010-08-19 14:52:12 +020059#define KBD_CCMD_PULSE_BITS_3_0 0xF0 /* Pulse bits 3-0 of the output port P2. */
60#define KBD_CCMD_RESET 0xFE /* Pulse bit 0 of the output port P2 = CPU reset. */
61#define KBD_CCMD_NO_OP 0xFF /* Pulse no bits of the output port P2. */
bellard80cabfa2004-03-14 12:20:30 +000062
63/* Keyboard Commands */
64#define KBD_CMD_SET_LEDS 0xED /* Set keyboard leds */
65#define KBD_CMD_ECHO 0xEE
66#define KBD_CMD_GET_ID 0xF2 /* get keyboard ID */
67#define KBD_CMD_SET_RATE 0xF3 /* Set typematic rate */
68#define KBD_CMD_ENABLE 0xF4 /* Enable scanning */
69#define KBD_CMD_RESET_DISABLE 0xF5 /* reset and disable scanning */
70#define KBD_CMD_RESET_ENABLE 0xF6 /* reset and enable scanning */
71#define KBD_CMD_RESET 0xFF /* Reset */
72
73/* Keyboard Replies */
74#define KBD_REPLY_POR 0xAA /* Power on reset */
75#define KBD_REPLY_ACK 0xFA /* Command ACK */
76#define KBD_REPLY_RESEND 0xFE /* Command NACK, send the cmd again */
77
78/* Status Register Bits */
79#define KBD_STAT_OBF 0x01 /* Keyboard output buffer full */
80#define KBD_STAT_IBF 0x02 /* Keyboard input buffer full */
81#define KBD_STAT_SELFTEST 0x04 /* Self test successful */
82#define KBD_STAT_CMD 0x08 /* Last write was a command write (0=data) */
83#define KBD_STAT_UNLOCKED 0x10 /* Zero if keyboard locked */
84#define KBD_STAT_MOUSE_OBF 0x20 /* Mouse output buffer full */
85#define KBD_STAT_GTO 0x40 /* General receive/xmit timeout */
86#define KBD_STAT_PERR 0x80 /* Parity error */
87
88/* Controller Mode Register Bits */
89#define KBD_MODE_KBD_INT 0x01 /* Keyboard data generate IRQ1 */
90#define KBD_MODE_MOUSE_INT 0x02 /* Mouse data generate IRQ12 */
91#define KBD_MODE_SYS 0x04 /* The system flag (?) */
92#define KBD_MODE_NO_KEYLOCK 0x08 /* The keylock doesn't affect the keyboard if set */
93#define KBD_MODE_DISABLE_KBD 0x10 /* Disable keyboard interface */
94#define KBD_MODE_DISABLE_MOUSE 0x20 /* Disable mouse interface */
95#define KBD_MODE_KCC 0x40 /* Scan code conversion to PC format */
96#define KBD_MODE_RFU 0x80
97
Blue Swirl956a3e62010-05-22 07:59:01 +000098/* Output Port Bits */
99#define KBD_OUT_RESET 0x01 /* 1=normal mode, 0=reset */
100#define KBD_OUT_A20 0x02 /* x86 only */
101#define KBD_OUT_OBF 0x10 /* Keyboard output buffer full */
102#define KBD_OUT_MOUSE_OBF 0x20 /* Mouse output buffer full */
103
bellard80cabfa2004-03-14 12:20:30 +0000104/* Mouse Commands */
105#define AUX_SET_SCALE11 0xE6 /* Set 1:1 scaling */
106#define AUX_SET_SCALE21 0xE7 /* Set 2:1 scaling */
107#define AUX_SET_RES 0xE8 /* Set resolution */
108#define AUX_GET_SCALE 0xE9 /* Get scaling factor */
109#define AUX_SET_STREAM 0xEA /* Set stream mode */
110#define AUX_POLL 0xEB /* Poll */
111#define AUX_RESET_WRAP 0xEC /* Reset wrap mode */
112#define AUX_SET_WRAP 0xEE /* Set wrap mode */
113#define AUX_SET_REMOTE 0xF0 /* Set remote mode */
114#define AUX_GET_TYPE 0xF2 /* Get type */
115#define AUX_SET_SAMPLE 0xF3 /* Set sample rate */
116#define AUX_ENABLE_DEV 0xF4 /* Enable aux device */
117#define AUX_DISABLE_DEV 0xF5 /* Disable aux device */
118#define AUX_SET_DEFAULT 0xF6
119#define AUX_RESET 0xFF /* Reset aux device */
120#define AUX_ACK 0xFA /* Command byte ACK. */
121
122#define MOUSE_STATUS_REMOTE 0x40
123#define MOUSE_STATUS_ENABLED 0x20
124#define MOUSE_STATUS_SCALE21 0x10
125
bellarddaa57962005-11-26 10:14:03 +0000126#define KBD_PENDING_KBD 1
127#define KBD_PENDING_AUX 2
bellard80cabfa2004-03-14 12:20:30 +0000128
129typedef struct KBDState {
bellard80cabfa2004-03-14 12:20:30 +0000130 uint8_t write_cmd; /* if non zero, write data to port 60 is expected */
131 uint8_t status;
132 uint8_t mode;
Blue Swirl956a3e62010-05-22 07:59:01 +0000133 uint8_t outport;
bellarddaa57962005-11-26 10:14:03 +0000134 /* Bitmask of devices with data available. */
pbrook7783e9f2006-04-08 14:12:31 +0000135 uint8_t pending;
bellarddaa57962005-11-26 10:14:03 +0000136 void *kbd;
137 void *mouse;
thsb7678d92007-02-18 00:08:44 +0000138
pbrookd537cf62007-04-07 18:14:41 +0000139 qemu_irq irq_kbd;
140 qemu_irq irq_mouse;
Blue Swirl956a3e62010-05-22 07:59:01 +0000141 qemu_irq *a20_out;
Avi Kivitya8170e52012-10-23 12:30:10 +0200142 hwaddr mask;
bellard80cabfa2004-03-14 12:20:30 +0000143} KBDState;
144
bellard80cabfa2004-03-14 12:20:30 +0000145/* update irq and KBD_STAT_[MOUSE_]OBF */
146/* XXX: not generating the irqs if KBD_MODE_DISABLE_KBD is set may be
147 incorrect, but it avoids having to simulate exact delays */
148static void kbd_update_irq(KBDState *s)
149{
thsb7678d92007-02-18 00:08:44 +0000150 int irq_kbd_level, irq_mouse_level;
bellard80cabfa2004-03-14 12:20:30 +0000151
thsb7678d92007-02-18 00:08:44 +0000152 irq_kbd_level = 0;
153 irq_mouse_level = 0;
bellard80cabfa2004-03-14 12:20:30 +0000154 s->status &= ~(KBD_STAT_OBF | KBD_STAT_MOUSE_OBF);
Blue Swirl956a3e62010-05-22 07:59:01 +0000155 s->outport &= ~(KBD_OUT_OBF | KBD_OUT_MOUSE_OBF);
bellarddaa57962005-11-26 10:14:03 +0000156 if (s->pending) {
bellard80cabfa2004-03-14 12:20:30 +0000157 s->status |= KBD_STAT_OBF;
Blue Swirl956a3e62010-05-22 07:59:01 +0000158 s->outport |= KBD_OUT_OBF;
thsb92bb992007-04-16 17:20:48 +0000159 /* kbd data takes priority over aux data. */
bellarddaa57962005-11-26 10:14:03 +0000160 if (s->pending == KBD_PENDING_AUX) {
bellard80cabfa2004-03-14 12:20:30 +0000161 s->status |= KBD_STAT_MOUSE_OBF;
Blue Swirl956a3e62010-05-22 07:59:01 +0000162 s->outport |= KBD_OUT_MOUSE_OBF;
bellard80cabfa2004-03-14 12:20:30 +0000163 if (s->mode & KBD_MODE_MOUSE_INT)
thsb7678d92007-02-18 00:08:44 +0000164 irq_mouse_level = 1;
bellard80cabfa2004-03-14 12:20:30 +0000165 } else {
ths5fafdf22007-09-16 21:08:06 +0000166 if ((s->mode & KBD_MODE_KBD_INT) &&
bellard80cabfa2004-03-14 12:20:30 +0000167 !(s->mode & KBD_MODE_DISABLE_KBD))
thsb7678d92007-02-18 00:08:44 +0000168 irq_kbd_level = 1;
bellard80cabfa2004-03-14 12:20:30 +0000169 }
170 }
pbrookd537cf62007-04-07 18:14:41 +0000171 qemu_set_irq(s->irq_kbd, irq_kbd_level);
172 qemu_set_irq(s->irq_mouse, irq_mouse_level);
bellard80cabfa2004-03-14 12:20:30 +0000173}
174
bellarddaa57962005-11-26 10:14:03 +0000175static void kbd_update_kbd_irq(void *opaque, int level)
bellard80cabfa2004-03-14 12:20:30 +0000176{
bellarddaa57962005-11-26 10:14:03 +0000177 KBDState *s = (KBDState *)opaque;
bellard80cabfa2004-03-14 12:20:30 +0000178
bellarddaa57962005-11-26 10:14:03 +0000179 if (level)
180 s->pending |= KBD_PENDING_KBD;
bellard80cabfa2004-03-14 12:20:30 +0000181 else
bellarddaa57962005-11-26 10:14:03 +0000182 s->pending &= ~KBD_PENDING_KBD;
bellard80cabfa2004-03-14 12:20:30 +0000183 kbd_update_irq(s);
184}
185
bellarddaa57962005-11-26 10:14:03 +0000186static void kbd_update_aux_irq(void *opaque, int level)
bellard80cabfa2004-03-14 12:20:30 +0000187{
bellarddaa57962005-11-26 10:14:03 +0000188 KBDState *s = (KBDState *)opaque;
189
190 if (level)
191 s->pending |= KBD_PENDING_AUX;
192 else
193 s->pending &= ~KBD_PENDING_AUX;
194 kbd_update_irq(s);
bellard80cabfa2004-03-14 12:20:30 +0000195}
196
Alexander Grafd540bfe2012-10-08 13:30:08 +0200197static uint64_t kbd_read_status(void *opaque, hwaddr addr,
198 unsigned size)
bellard80cabfa2004-03-14 12:20:30 +0000199{
bellardb41a2cd2004-03-14 21:46:48 +0000200 KBDState *s = opaque;
bellard80cabfa2004-03-14 12:20:30 +0000201 int val;
202 val = s->status;
Blue Swirlc86d2c22010-05-22 07:59:06 +0000203 DPRINTF("kbd: read status=0x%02x\n", val);
bellard80cabfa2004-03-14 12:20:30 +0000204 return val;
205}
206
bellarddaa57962005-11-26 10:14:03 +0000207static void kbd_queue(KBDState *s, int b, int aux)
208{
209 if (aux)
210 ps2_queue(s->mouse, b);
211 else
212 ps2_queue(s->kbd, b);
213}
214
Blue Swirl4b78a802011-01-06 18:24:35 +0000215static void outport_write(KBDState *s, uint32_t val)
Blue Swirl956a3e62010-05-22 07:59:01 +0000216{
Blue Swirlc86d2c22010-05-22 07:59:06 +0000217 DPRINTF("kbd: write outport=0x%02x\n", val);
Blue Swirl956a3e62010-05-22 07:59:01 +0000218 s->outport = val;
219 if (s->a20_out) {
220 qemu_set_irq(*s->a20_out, (val >> 1) & 1);
221 }
222 if (!(val & 1)) {
223 qemu_system_reset_request();
224 }
225}
226
Alexander Grafd540bfe2012-10-08 13:30:08 +0200227static void kbd_write_command(void *opaque, hwaddr addr,
228 uint64_t val, unsigned size)
bellard80cabfa2004-03-14 12:20:30 +0000229{
bellardb41a2cd2004-03-14 21:46:48 +0000230 KBDState *s = opaque;
bellard80cabfa2004-03-14 12:20:30 +0000231
Blue Swirlc86d2c22010-05-22 07:59:06 +0000232 DPRINTF("kbd: write cmd=0x%02x\n", val);
Bernhard Kohl5ccaa4c2010-08-19 14:52:12 +0200233
234 /* Bits 3-0 of the output port P2 of the keyboard controller may be pulsed
235 * low for approximately 6 micro seconds. Bits 3-0 of the KBD_CCMD_PULSE
236 * command specify the output port bits to be pulsed.
237 * 0: Bit should be pulsed. 1: Bit should not be modified.
238 * The only useful version of this command is pulsing bit 0,
239 * which does a CPU reset.
240 */
241 if((val & KBD_CCMD_PULSE_BITS_3_0) == KBD_CCMD_PULSE_BITS_3_0) {
242 if(!(val & 1))
243 val = KBD_CCMD_RESET;
244 else
245 val = KBD_CCMD_NO_OP;
246 }
247
bellard80cabfa2004-03-14 12:20:30 +0000248 switch(val) {
249 case KBD_CCMD_READ_MODE:
balrog889bec62008-07-19 14:16:20 +0000250 kbd_queue(s, s->mode, 0);
bellard80cabfa2004-03-14 12:20:30 +0000251 break;
252 case KBD_CCMD_WRITE_MODE:
253 case KBD_CCMD_WRITE_OBUF:
254 case KBD_CCMD_WRITE_AUX_OBUF:
255 case KBD_CCMD_WRITE_MOUSE:
256 case KBD_CCMD_WRITE_OUTPORT:
257 s->write_cmd = val;
258 break;
259 case KBD_CCMD_MOUSE_DISABLE:
260 s->mode |= KBD_MODE_DISABLE_MOUSE;
261 break;
262 case KBD_CCMD_MOUSE_ENABLE:
263 s->mode &= ~KBD_MODE_DISABLE_MOUSE;
264 break;
265 case KBD_CCMD_TEST_MOUSE:
266 kbd_queue(s, 0x00, 0);
267 break;
268 case KBD_CCMD_SELF_TEST:
269 s->status |= KBD_STAT_SELFTEST;
270 kbd_queue(s, 0x55, 0);
271 break;
272 case KBD_CCMD_KBD_TEST:
273 kbd_queue(s, 0x00, 0);
274 break;
275 case KBD_CCMD_KBD_DISABLE:
276 s->mode |= KBD_MODE_DISABLE_KBD;
277 kbd_update_irq(s);
278 break;
279 case KBD_CCMD_KBD_ENABLE:
280 s->mode &= ~KBD_MODE_DISABLE_KBD;
281 kbd_update_irq(s);
282 break;
283 case KBD_CCMD_READ_INPORT:
284 kbd_queue(s, 0x00, 0);
285 break;
286 case KBD_CCMD_READ_OUTPORT:
Blue Swirl956a3e62010-05-22 07:59:01 +0000287 kbd_queue(s, s->outport, 0);
bellard80cabfa2004-03-14 12:20:30 +0000288 break;
bellard80cabfa2004-03-14 12:20:30 +0000289 case KBD_CCMD_ENABLE_A20:
Blue Swirl956a3e62010-05-22 07:59:01 +0000290 if (s->a20_out) {
291 qemu_irq_raise(*s->a20_out);
292 }
293 s->outport |= KBD_OUT_A20;
bellard80cabfa2004-03-14 12:20:30 +0000294 break;
295 case KBD_CCMD_DISABLE_A20:
Blue Swirl956a3e62010-05-22 07:59:01 +0000296 if (s->a20_out) {
297 qemu_irq_lower(*s->a20_out);
298 }
299 s->outport &= ~KBD_OUT_A20;
bellard80cabfa2004-03-14 12:20:30 +0000300 break;
bellard80cabfa2004-03-14 12:20:30 +0000301 case KBD_CCMD_RESET:
bellardd7d02e32004-06-20 12:58:36 +0000302 qemu_system_reset_request();
bellard80cabfa2004-03-14 12:20:30 +0000303 break;
Bernhard Kohl5ccaa4c2010-08-19 14:52:12 +0200304 case KBD_CCMD_NO_OP:
305 /* ignore that */
bellard80cabfa2004-03-14 12:20:30 +0000306 break;
307 default:
Alexander Grafd540bfe2012-10-08 13:30:08 +0200308 fprintf(stderr, "qemu: unsupported keyboard cmd=0x%02x\n", (int)val);
bellard80cabfa2004-03-14 12:20:30 +0000309 break;
310 }
311}
312
Alexander Grafd540bfe2012-10-08 13:30:08 +0200313static uint64_t kbd_read_data(void *opaque, hwaddr addr,
314 unsigned size)
bellard80cabfa2004-03-14 12:20:30 +0000315{
bellardb41a2cd2004-03-14 21:46:48 +0000316 KBDState *s = opaque;
balroge41c0f22008-02-10 13:39:24 +0000317 uint32_t val;
bellard80cabfa2004-03-14 12:20:30 +0000318
bellarddaa57962005-11-26 10:14:03 +0000319 if (s->pending == KBD_PENDING_AUX)
balroge41c0f22008-02-10 13:39:24 +0000320 val = ps2_read_data(s->mouse);
321 else
322 val = ps2_read_data(s->kbd);
bellard80cabfa2004-03-14 12:20:30 +0000323
Blue Swirlc86d2c22010-05-22 07:59:06 +0000324 DPRINTF("kbd: read data=0x%02x\n", val);
balroge41c0f22008-02-10 13:39:24 +0000325 return val;
bellard80cabfa2004-03-14 12:20:30 +0000326}
327
Alexander Grafd540bfe2012-10-08 13:30:08 +0200328static void kbd_write_data(void *opaque, hwaddr addr,
329 uint64_t val, unsigned size)
bellard80cabfa2004-03-14 12:20:30 +0000330{
bellardb41a2cd2004-03-14 21:46:48 +0000331 KBDState *s = opaque;
bellard80cabfa2004-03-14 12:20:30 +0000332
Blue Swirlc86d2c22010-05-22 07:59:06 +0000333 DPRINTF("kbd: write data=0x%02x\n", val);
bellard80cabfa2004-03-14 12:20:30 +0000334
335 switch(s->write_cmd) {
336 case 0:
bellarddaa57962005-11-26 10:14:03 +0000337 ps2_write_keyboard(s->kbd, val);
bellard80cabfa2004-03-14 12:20:30 +0000338 break;
339 case KBD_CCMD_WRITE_MODE:
340 s->mode = val;
pbrookf94f5d72006-02-08 04:42:17 +0000341 ps2_keyboard_set_translation(s->kbd, (s->mode & KBD_MODE_KCC) != 0);
bellarddaa57962005-11-26 10:14:03 +0000342 /* ??? */
bellard80cabfa2004-03-14 12:20:30 +0000343 kbd_update_irq(s);
344 break;
345 case KBD_CCMD_WRITE_OBUF:
346 kbd_queue(s, val, 0);
347 break;
348 case KBD_CCMD_WRITE_AUX_OBUF:
349 kbd_queue(s, val, 1);
350 break;
351 case KBD_CCMD_WRITE_OUTPORT:
Blue Swirl4b78a802011-01-06 18:24:35 +0000352 outport_write(s, val);
bellard80cabfa2004-03-14 12:20:30 +0000353 break;
354 case KBD_CCMD_WRITE_MOUSE:
bellarddaa57962005-11-26 10:14:03 +0000355 ps2_write_mouse(s->mouse, val);
bellard80cabfa2004-03-14 12:20:30 +0000356 break;
357 default:
358 break;
359 }
360 s->write_cmd = 0;
361}
362
bellardd7d02e32004-06-20 12:58:36 +0000363static void kbd_reset(void *opaque)
bellard80cabfa2004-03-14 12:20:30 +0000364{
bellardd7d02e32004-06-20 12:58:36 +0000365 KBDState *s = opaque;
bellard80cabfa2004-03-14 12:20:30 +0000366
bellard80cabfa2004-03-14 12:20:30 +0000367 s->mode = KBD_MODE_KBD_INT | KBD_MODE_MOUSE_INT;
368 s->status = KBD_STAT_CMD | KBD_STAT_UNLOCKED;
Blue Swirl956a3e62010-05-22 07:59:01 +0000369 s->outport = KBD_OUT_RESET | KBD_OUT_A20;
bellard80cabfa2004-03-14 12:20:30 +0000370}
371
Juan Quintela3c619b52009-09-10 03:04:41 +0200372static const VMStateDescription vmstate_kbd = {
373 .name = "pckbd",
374 .version_id = 3,
375 .minimum_version_id = 3,
376 .minimum_version_id_old = 3,
377 .fields = (VMStateField []) {
378 VMSTATE_UINT8(write_cmd, KBDState),
379 VMSTATE_UINT8(status, KBDState),
380 VMSTATE_UINT8(mode, KBDState),
381 VMSTATE_UINT8(pending, KBDState),
382 VMSTATE_END_OF_LIST()
383 }
384};
bellard675376f2004-07-10 13:39:53 +0000385
thsb92bb992007-04-16 17:20:48 +0000386/* Memory mapped interface */
Avi Kivitya8170e52012-10-23 12:30:10 +0200387static uint32_t kbd_mm_readb (void *opaque, hwaddr addr)
thsb92bb992007-04-16 17:20:48 +0000388{
389 KBDState *s = opaque;
390
aurel324efbe582008-12-10 15:02:07 +0000391 if (addr & s->mask)
Alexander Grafd540bfe2012-10-08 13:30:08 +0200392 return kbd_read_status(s, 0, 1) & 0xff;
aurel324efbe582008-12-10 15:02:07 +0000393 else
Alexander Grafd540bfe2012-10-08 13:30:08 +0200394 return kbd_read_data(s, 0, 1) & 0xff;
thsb92bb992007-04-16 17:20:48 +0000395}
396
Avi Kivitya8170e52012-10-23 12:30:10 +0200397static void kbd_mm_writeb (void *opaque, hwaddr addr, uint32_t value)
thsb92bb992007-04-16 17:20:48 +0000398{
399 KBDState *s = opaque;
400
aurel324efbe582008-12-10 15:02:07 +0000401 if (addr & s->mask)
Alexander Grafd540bfe2012-10-08 13:30:08 +0200402 kbd_write_command(s, 0, value & 0xff, 1);
aurel324efbe582008-12-10 15:02:07 +0000403 else
Alexander Grafd540bfe2012-10-08 13:30:08 +0200404 kbd_write_data(s, 0, value & 0xff, 1);
thsb92bb992007-04-16 17:20:48 +0000405}
406
Richard Hendersondbff76a2011-08-10 15:28:17 -0700407static const MemoryRegionOps i8042_mmio_ops = {
408 .endianness = DEVICE_NATIVE_ENDIAN,
409 .old_mmio = {
410 .read = { kbd_mm_readb, kbd_mm_readb, kbd_mm_readb },
411 .write = { kbd_mm_writeb, kbd_mm_writeb, kbd_mm_writeb },
412 },
thsb92bb992007-04-16 17:20:48 +0000413};
414
blueswir171db7102007-06-08 16:45:23 +0000415void i8042_mm_init(qemu_irq kbd_irq, qemu_irq mouse_irq,
Richard Hendersondbff76a2011-08-10 15:28:17 -0700416 MemoryRegion *region, ram_addr_t size,
Avi Kivitya8170e52012-10-23 12:30:10 +0200417 hwaddr mask)
thsb92bb992007-04-16 17:20:48 +0000418{
Anthony Liguori7267c092011-08-20 22:09:37 -0500419 KBDState *s = g_malloc0(sizeof(KBDState));
thsb92bb992007-04-16 17:20:48 +0000420
421 s->irq_kbd = kbd_irq;
422 s->irq_mouse = mouse_irq;
aurel324efbe582008-12-10 15:02:07 +0000423 s->mask = mask;
thsb92bb992007-04-16 17:20:48 +0000424
Alex Williamson0be71e32010-06-25 11:09:07 -0600425 vmstate_register(NULL, 0, &vmstate_kbd, s);
Richard Hendersondbff76a2011-08-10 15:28:17 -0700426
427 memory_region_init_io(region, &i8042_mmio_ops, s, "i8042", size);
thsb92bb992007-04-16 17:20:48 +0000428
429 s->kbd = ps2_kbd_init(kbd_update_kbd_irq, s);
430 s->mouse = ps2_mouse_init(kbd_update_aux_irq, s);
Jan Kiszkaa08d4362009-06-27 09:25:07 +0200431 qemu_register_reset(kbd_reset, s);
thsb92bb992007-04-16 17:20:48 +0000432}
Gerd Hoffmannda85ccf2009-07-31 12:30:15 +0200433
434typedef struct ISAKBDState {
435 ISADevice dev;
Richard Hendersondbff76a2011-08-10 15:28:17 -0700436 KBDState kbd;
437 MemoryRegion io[2];
Gerd Hoffmannda85ccf2009-07-31 12:30:15 +0200438} ISAKBDState;
439
Blue Swirl956a3e62010-05-22 07:59:01 +0000440void i8042_isa_mouse_fake_event(void *opaque)
441{
442 ISADevice *dev = opaque;
443 KBDState *s = &(DO_UPCAST(ISAKBDState, dev, dev)->kbd);
444
445 ps2_mouse_fake_event(s->mouse);
446}
447
448void i8042_setup_a20_line(ISADevice *dev, qemu_irq *a20_out)
449{
450 KBDState *s = &(DO_UPCAST(ISAKBDState, dev, dev)->kbd);
451
452 s->a20_out = a20_out;
453}
454
Blue Swirld05ac8f2009-12-04 20:44:44 +0000455static const VMStateDescription vmstate_kbd_isa = {
Juan Quintelabe73cfe2009-12-02 12:36:46 +0100456 .name = "pckbd",
457 .version_id = 3,
458 .minimum_version_id = 3,
459 .minimum_version_id_old = 3,
460 .fields = (VMStateField []) {
461 VMSTATE_STRUCT(kbd, ISAKBDState, 0, vmstate_kbd, KBDState),
462 VMSTATE_END_OF_LIST()
463 }
464};
465
Richard Hendersondbff76a2011-08-10 15:28:17 -0700466static const MemoryRegionOps i8042_data_ops = {
Alexander Grafd540bfe2012-10-08 13:30:08 +0200467 .read = kbd_read_data,
468 .write = kbd_write_data,
469 .impl = {
470 .min_access_size = 1,
471 .max_access_size = 1,
472 },
473 .endianness = DEVICE_LITTLE_ENDIAN,
Richard Hendersondbff76a2011-08-10 15:28:17 -0700474};
475
476static const MemoryRegionOps i8042_cmd_ops = {
Alexander Grafd540bfe2012-10-08 13:30:08 +0200477 .read = kbd_read_status,
478 .write = kbd_write_command,
479 .impl = {
480 .min_access_size = 1,
481 .max_access_size = 1,
482 },
483 .endianness = DEVICE_LITTLE_ENDIAN,
Richard Hendersondbff76a2011-08-10 15:28:17 -0700484};
485
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200486static int i8042_initfn(ISADevice *dev)
Gerd Hoffmannda85ccf2009-07-31 12:30:15 +0200487{
Richard Hendersondbff76a2011-08-10 15:28:17 -0700488 ISAKBDState *isa_s = DO_UPCAST(ISAKBDState, dev, dev);
489 KBDState *s = &isa_s->kbd;
Gerd Hoffmannda85ccf2009-07-31 12:30:15 +0200490
Gerd Hoffmann2e15e232009-09-10 11:43:27 +0200491 isa_init_irq(dev, &s->irq_kbd, 1);
492 isa_init_irq(dev, &s->irq_mouse, 12);
Gerd Hoffmannda85ccf2009-07-31 12:30:15 +0200493
Richard Hendersondbff76a2011-08-10 15:28:17 -0700494 memory_region_init_io(isa_s->io + 0, &i8042_data_ops, s, "i8042-data", 1);
495 isa_register_ioport(dev, isa_s->io + 0, 0x60);
496
497 memory_region_init_io(isa_s->io + 1, &i8042_cmd_ops, s, "i8042-cmd", 1);
498 isa_register_ioport(dev, isa_s->io + 1, 0x64);
Gerd Hoffmannda85ccf2009-07-31 12:30:15 +0200499
500 s->kbd = ps2_kbd_init(kbd_update_kbd_irq, s);
501 s->mouse = ps2_mouse_init(kbd_update_aux_irq, s);
Gerd Hoffmannda85ccf2009-07-31 12:30:15 +0200502 qemu_register_reset(kbd_reset, s);
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200503 return 0;
Gerd Hoffmannda85ccf2009-07-31 12:30:15 +0200504}
505
Anthony Liguori8f04ee02011-12-04 11:52:49 -0600506static void i8042_class_initfn(ObjectClass *klass, void *data)
507{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600508 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguori8f04ee02011-12-04 11:52:49 -0600509 ISADeviceClass *ic = ISA_DEVICE_CLASS(klass);
510 ic->init = i8042_initfn;
Anthony Liguori39bffca2011-12-07 21:34:16 -0600511 dc->no_user = 1;
512 dc->vmsd = &vmstate_kbd_isa;
Anthony Liguori8f04ee02011-12-04 11:52:49 -0600513}
514
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100515static const TypeInfo i8042_info = {
Anthony Liguori39bffca2011-12-07 21:34:16 -0600516 .name = "i8042",
517 .parent = TYPE_ISA_DEVICE,
518 .instance_size = sizeof(ISAKBDState),
519 .class_init = i8042_class_initfn,
Gerd Hoffmannda85ccf2009-07-31 12:30:15 +0200520};
521
Andreas Färber83f7d432012-02-09 15:20:55 +0100522static void i8042_register_types(void)
Gerd Hoffmannda85ccf2009-07-31 12:30:15 +0200523{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600524 type_register_static(&i8042_info);
Gerd Hoffmannda85ccf2009-07-31 12:30:15 +0200525}
Andreas Färber83f7d432012-02-09 15:20:55 +0100526
527type_init(i8042_register_types)