]> git.proxmox.com Git - mirror_qemu.git/blame - hw/input/pckbd.c
Merge remote-tracking branch 'remotes/kraxel/tags/pull-vga-20150615-1' into staging
[mirror_qemu.git] / hw / input / pckbd.c
CommitLineData
80cabfad
FB
1/*
2 * QEMU PC keyboard emulation
5fafdf24 3 *
80cabfad 4 * Copyright (c) 2003 Fabrice Bellard
5fafdf24 5 *
80cabfad
FB
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:
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 */
83c9f4ca 24#include "hw/hw.h"
0d09e41a
PB
25#include "hw/isa/isa.h"
26#include "hw/i386/pc.h"
27#include "hw/input/ps2.h"
9c17d615 28#include "sysemu/sysemu.h"
80cabfad
FB
29
30/* debug PC keyboard */
31//#define DEBUG_KBD
c86d2c23
BS
32#ifdef DEBUG_KBD
33#define DPRINTF(fmt, ...) \
34 do { printf("KBD: " fmt , ## __VA_ARGS__); } while (0)
35#else
36#define DPRINTF(fmt, ...)
37#endif
80cabfad 38
80cabfad
FB
39/* 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 ? */
5ccaa4ce
BK
59#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. */
80cabfad
FB
62
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
956a3e6b
BS
98/* 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
d13c0404
PB
104/* OSes typically write 0xdd/0xdf to turn the A20 line off and on.
105 * We make the default value of the outport include these four bits,
106 * so that the subsection is rarely necessary.
107 */
108#define KBD_OUT_ONES 0xcc
109
80cabfad
FB
110/* Mouse Commands */
111#define AUX_SET_SCALE11 0xE6 /* Set 1:1 scaling */
112#define AUX_SET_SCALE21 0xE7 /* Set 2:1 scaling */
113#define AUX_SET_RES 0xE8 /* Set resolution */
114#define AUX_GET_SCALE 0xE9 /* Get scaling factor */
115#define AUX_SET_STREAM 0xEA /* Set stream mode */
116#define AUX_POLL 0xEB /* Poll */
117#define AUX_RESET_WRAP 0xEC /* Reset wrap mode */
118#define AUX_SET_WRAP 0xEE /* Set wrap mode */
119#define AUX_SET_REMOTE 0xF0 /* Set remote mode */
120#define AUX_GET_TYPE 0xF2 /* Get type */
121#define AUX_SET_SAMPLE 0xF3 /* Set sample rate */
122#define AUX_ENABLE_DEV 0xF4 /* Enable aux device */
123#define AUX_DISABLE_DEV 0xF5 /* Disable aux device */
124#define AUX_SET_DEFAULT 0xF6
125#define AUX_RESET 0xFF /* Reset aux device */
126#define AUX_ACK 0xFA /* Command byte ACK. */
127
128#define MOUSE_STATUS_REMOTE 0x40
129#define MOUSE_STATUS_ENABLED 0x20
130#define MOUSE_STATUS_SCALE21 0x10
131
daa57963
FB
132#define KBD_PENDING_KBD 1
133#define KBD_PENDING_AUX 2
80cabfad
FB
134
135typedef struct KBDState {
80cabfad
FB
136 uint8_t write_cmd; /* if non zero, write data to port 60 is expected */
137 uint8_t status;
138 uint8_t mode;
956a3e6b 139 uint8_t outport;
a28fe7e3 140 bool outport_present;
daa57963 141 /* Bitmask of devices with data available. */
7783e9f0 142 uint8_t pending;
daa57963
FB
143 void *kbd;
144 void *mouse;
b7678d96 145
d537cf6c
PB
146 qemu_irq irq_kbd;
147 qemu_irq irq_mouse;
956a3e6b 148 qemu_irq *a20_out;
a8170e5e 149 hwaddr mask;
80cabfad
FB
150} KBDState;
151
80cabfad
FB
152/* update irq and KBD_STAT_[MOUSE_]OBF */
153/* XXX: not generating the irqs if KBD_MODE_DISABLE_KBD is set may be
154 incorrect, but it avoids having to simulate exact delays */
155static void kbd_update_irq(KBDState *s)
156{
b7678d96 157 int irq_kbd_level, irq_mouse_level;
80cabfad 158
b7678d96
TS
159 irq_kbd_level = 0;
160 irq_mouse_level = 0;
80cabfad 161 s->status &= ~(KBD_STAT_OBF | KBD_STAT_MOUSE_OBF);
956a3e6b 162 s->outport &= ~(KBD_OUT_OBF | KBD_OUT_MOUSE_OBF);
daa57963 163 if (s->pending) {
80cabfad 164 s->status |= KBD_STAT_OBF;
956a3e6b 165 s->outport |= KBD_OUT_OBF;
b92bb99b 166 /* kbd data takes priority over aux data. */
daa57963 167 if (s->pending == KBD_PENDING_AUX) {
80cabfad 168 s->status |= KBD_STAT_MOUSE_OBF;
956a3e6b 169 s->outport |= KBD_OUT_MOUSE_OBF;
80cabfad 170 if (s->mode & KBD_MODE_MOUSE_INT)
b7678d96 171 irq_mouse_level = 1;
80cabfad 172 } else {
5fafdf24 173 if ((s->mode & KBD_MODE_KBD_INT) &&
80cabfad 174 !(s->mode & KBD_MODE_DISABLE_KBD))
b7678d96 175 irq_kbd_level = 1;
80cabfad
FB
176 }
177 }
d537cf6c
PB
178 qemu_set_irq(s->irq_kbd, irq_kbd_level);
179 qemu_set_irq(s->irq_mouse, irq_mouse_level);
80cabfad
FB
180}
181
daa57963 182static void kbd_update_kbd_irq(void *opaque, int level)
80cabfad 183{
daa57963 184 KBDState *s = (KBDState *)opaque;
80cabfad 185
daa57963
FB
186 if (level)
187 s->pending |= KBD_PENDING_KBD;
80cabfad 188 else
daa57963 189 s->pending &= ~KBD_PENDING_KBD;
80cabfad
FB
190 kbd_update_irq(s);
191}
192
daa57963 193static void kbd_update_aux_irq(void *opaque, int level)
80cabfad 194{
daa57963
FB
195 KBDState *s = (KBDState *)opaque;
196
197 if (level)
198 s->pending |= KBD_PENDING_AUX;
199 else
200 s->pending &= ~KBD_PENDING_AUX;
201 kbd_update_irq(s);
80cabfad
FB
202}
203
d540bfe0
AG
204static uint64_t kbd_read_status(void *opaque, hwaddr addr,
205 unsigned size)
80cabfad 206{
b41a2cd1 207 KBDState *s = opaque;
80cabfad
FB
208 int val;
209 val = s->status;
c86d2c23 210 DPRINTF("kbd: read status=0x%02x\n", val);
80cabfad
FB
211 return val;
212}
213
daa57963
FB
214static void kbd_queue(KBDState *s, int b, int aux)
215{
216 if (aux)
217 ps2_queue(s->mouse, b);
218 else
219 ps2_queue(s->kbd, b);
220}
221
4b78a802 222static void outport_write(KBDState *s, uint32_t val)
956a3e6b 223{
c86d2c23 224 DPRINTF("kbd: write outport=0x%02x\n", val);
956a3e6b
BS
225 s->outport = val;
226 if (s->a20_out) {
227 qemu_set_irq(*s->a20_out, (val >> 1) & 1);
228 }
229 if (!(val & 1)) {
230 qemu_system_reset_request();
231 }
232}
233
d540bfe0
AG
234static void kbd_write_command(void *opaque, hwaddr addr,
235 uint64_t val, unsigned size)
80cabfad 236{
b41a2cd1 237 KBDState *s = opaque;
80cabfad 238
c5539cb4 239 DPRINTF("kbd: write cmd=0x%02" PRIx64 "\n", val);
5ccaa4ce
BK
240
241 /* Bits 3-0 of the output port P2 of the keyboard controller may be pulsed
242 * low for approximately 6 micro seconds. Bits 3-0 of the KBD_CCMD_PULSE
243 * command specify the output port bits to be pulsed.
244 * 0: Bit should be pulsed. 1: Bit should not be modified.
245 * The only useful version of this command is pulsing bit 0,
246 * which does a CPU reset.
247 */
248 if((val & KBD_CCMD_PULSE_BITS_3_0) == KBD_CCMD_PULSE_BITS_3_0) {
249 if(!(val & 1))
250 val = KBD_CCMD_RESET;
251 else
252 val = KBD_CCMD_NO_OP;
253 }
254
80cabfad
FB
255 switch(val) {
256 case KBD_CCMD_READ_MODE:
889bec69 257 kbd_queue(s, s->mode, 0);
80cabfad
FB
258 break;
259 case KBD_CCMD_WRITE_MODE:
260 case KBD_CCMD_WRITE_OBUF:
261 case KBD_CCMD_WRITE_AUX_OBUF:
262 case KBD_CCMD_WRITE_MOUSE:
263 case KBD_CCMD_WRITE_OUTPORT:
264 s->write_cmd = val;
265 break;
266 case KBD_CCMD_MOUSE_DISABLE:
267 s->mode |= KBD_MODE_DISABLE_MOUSE;
268 break;
269 case KBD_CCMD_MOUSE_ENABLE:
270 s->mode &= ~KBD_MODE_DISABLE_MOUSE;
271 break;
272 case KBD_CCMD_TEST_MOUSE:
273 kbd_queue(s, 0x00, 0);
274 break;
275 case KBD_CCMD_SELF_TEST:
276 s->status |= KBD_STAT_SELFTEST;
277 kbd_queue(s, 0x55, 0);
278 break;
279 case KBD_CCMD_KBD_TEST:
280 kbd_queue(s, 0x00, 0);
281 break;
282 case KBD_CCMD_KBD_DISABLE:
283 s->mode |= KBD_MODE_DISABLE_KBD;
284 kbd_update_irq(s);
285 break;
286 case KBD_CCMD_KBD_ENABLE:
287 s->mode &= ~KBD_MODE_DISABLE_KBD;
288 kbd_update_irq(s);
289 break;
290 case KBD_CCMD_READ_INPORT:
f1b7e0e4 291 kbd_queue(s, 0x80, 0);
80cabfad
FB
292 break;
293 case KBD_CCMD_READ_OUTPORT:
956a3e6b 294 kbd_queue(s, s->outport, 0);
80cabfad 295 break;
80cabfad 296 case KBD_CCMD_ENABLE_A20:
956a3e6b
BS
297 if (s->a20_out) {
298 qemu_irq_raise(*s->a20_out);
299 }
300 s->outport |= KBD_OUT_A20;
80cabfad
FB
301 break;
302 case KBD_CCMD_DISABLE_A20:
956a3e6b
BS
303 if (s->a20_out) {
304 qemu_irq_lower(*s->a20_out);
305 }
306 s->outport &= ~KBD_OUT_A20;
80cabfad 307 break;
80cabfad 308 case KBD_CCMD_RESET:
d7d02e3c 309 qemu_system_reset_request();
80cabfad 310 break;
5ccaa4ce
BK
311 case KBD_CCMD_NO_OP:
312 /* ignore that */
80cabfad
FB
313 break;
314 default:
d540bfe0 315 fprintf(stderr, "qemu: unsupported keyboard cmd=0x%02x\n", (int)val);
80cabfad
FB
316 break;
317 }
318}
319
d540bfe0
AG
320static uint64_t kbd_read_data(void *opaque, hwaddr addr,
321 unsigned size)
80cabfad 322{
63066f4f 323 KBDState *s = opaque;
e41c0f26 324 uint32_t val;
80cabfad 325
daa57963 326 if (s->pending == KBD_PENDING_AUX)
e41c0f26
AZ
327 val = ps2_read_data(s->mouse);
328 else
329 val = ps2_read_data(s->kbd);
80cabfad 330
c86d2c23 331 DPRINTF("kbd: read data=0x%02x\n", val);
e41c0f26 332 return val;
80cabfad
FB
333}
334
d540bfe0
AG
335static void kbd_write_data(void *opaque, hwaddr addr,
336 uint64_t val, unsigned size)
80cabfad 337{
b41a2cd1 338 KBDState *s = opaque;
80cabfad 339
c5539cb4 340 DPRINTF("kbd: write data=0x%02" PRIx64 "\n", val);
80cabfad
FB
341
342 switch(s->write_cmd) {
343 case 0:
daa57963 344 ps2_write_keyboard(s->kbd, val);
80cabfad
FB
345 break;
346 case KBD_CCMD_WRITE_MODE:
347 s->mode = val;
f94f5d71 348 ps2_keyboard_set_translation(s->kbd, (s->mode & KBD_MODE_KCC) != 0);
daa57963 349 /* ??? */
80cabfad
FB
350 kbd_update_irq(s);
351 break;
352 case KBD_CCMD_WRITE_OBUF:
353 kbd_queue(s, val, 0);
354 break;
355 case KBD_CCMD_WRITE_AUX_OBUF:
356 kbd_queue(s, val, 1);
357 break;
358 case KBD_CCMD_WRITE_OUTPORT:
4b78a802 359 outport_write(s, val);
80cabfad
FB
360 break;
361 case KBD_CCMD_WRITE_MOUSE:
daa57963 362 ps2_write_mouse(s->mouse, val);
80cabfad
FB
363 break;
364 default:
365 break;
366 }
367 s->write_cmd = 0;
368}
369
d7d02e3c 370static void kbd_reset(void *opaque)
80cabfad 371{
d7d02e3c 372 KBDState *s = opaque;
80cabfad 373
80cabfad
FB
374 s->mode = KBD_MODE_KBD_INT | KBD_MODE_MOUSE_INT;
375 s->status = KBD_STAT_CMD | KBD_STAT_UNLOCKED;
d13c0404 376 s->outport = KBD_OUT_RESET | KBD_OUT_A20 | KBD_OUT_ONES;
a28fe7e3
PD
377 s->outport_present = false;
378}
379
380static uint8_t kbd_outport_default(KBDState *s)
381{
d13c0404 382 return KBD_OUT_RESET | KBD_OUT_A20 | KBD_OUT_ONES
a28fe7e3
PD
383 | (s->status & KBD_STAT_OBF ? KBD_OUT_OBF : 0)
384 | (s->status & KBD_STAT_MOUSE_OBF ? KBD_OUT_MOUSE_OBF : 0);
385}
386
387static int kbd_outport_post_load(void *opaque, int version_id)
388{
389 KBDState *s = opaque;
390 s->outport_present = true;
391 return 0;
392}
393
5cd8cada
JQ
394static bool kbd_outport_needed(void *opaque)
395{
396 KBDState *s = opaque;
397 return s->outport != kbd_outport_default(s);
398}
399
a28fe7e3
PD
400static const VMStateDescription vmstate_kbd_outport = {
401 .name = "pckbd_outport",
402 .version_id = 1,
403 .minimum_version_id = 1,
404 .post_load = kbd_outport_post_load,
5cd8cada 405 .needed = kbd_outport_needed,
a28fe7e3
PD
406 .fields = (VMStateField[]) {
407 VMSTATE_UINT8(outport, KBDState),
408 VMSTATE_END_OF_LIST()
409 }
410};
411
a28fe7e3
PD
412static int kbd_post_load(void *opaque, int version_id)
413{
414 KBDState *s = opaque;
415 if (!s->outport_present) {
416 s->outport = kbd_outport_default(s);
417 }
418 s->outport_present = false;
419 return 0;
80cabfad
FB
420}
421
3c619b59
JQ
422static const VMStateDescription vmstate_kbd = {
423 .name = "pckbd",
424 .version_id = 3,
425 .minimum_version_id = 3,
a28fe7e3 426 .post_load = kbd_post_load,
d49805ae 427 .fields = (VMStateField[]) {
3c619b59
JQ
428 VMSTATE_UINT8(write_cmd, KBDState),
429 VMSTATE_UINT8(status, KBDState),
430 VMSTATE_UINT8(mode, KBDState),
431 VMSTATE_UINT8(pending, KBDState),
432 VMSTATE_END_OF_LIST()
a28fe7e3 433 },
5cd8cada
JQ
434 .subsections = (const VMStateDescription*[]) {
435 &vmstate_kbd_outport,
436 NULL
3c619b59
JQ
437 }
438};
675376f2 439
b92bb99b 440/* Memory mapped interface */
a8170e5e 441static uint32_t kbd_mm_readb (void *opaque, hwaddr addr)
b92bb99b
TS
442{
443 KBDState *s = opaque;
444
4efbe58f 445 if (addr & s->mask)
d540bfe0 446 return kbd_read_status(s, 0, 1) & 0xff;
4efbe58f 447 else
d540bfe0 448 return kbd_read_data(s, 0, 1) & 0xff;
b92bb99b
TS
449}
450
a8170e5e 451static void kbd_mm_writeb (void *opaque, hwaddr addr, uint32_t value)
b92bb99b
TS
452{
453 KBDState *s = opaque;
454
4efbe58f 455 if (addr & s->mask)
d540bfe0 456 kbd_write_command(s, 0, value & 0xff, 1);
4efbe58f 457 else
d540bfe0 458 kbd_write_data(s, 0, value & 0xff, 1);
b92bb99b
TS
459}
460
dbff76ac
RH
461static const MemoryRegionOps i8042_mmio_ops = {
462 .endianness = DEVICE_NATIVE_ENDIAN,
463 .old_mmio = {
464 .read = { kbd_mm_readb, kbd_mm_readb, kbd_mm_readb },
465 .write = { kbd_mm_writeb, kbd_mm_writeb, kbd_mm_writeb },
466 },
b92bb99b
TS
467};
468
71db710f 469void i8042_mm_init(qemu_irq kbd_irq, qemu_irq mouse_irq,
dbff76ac 470 MemoryRegion *region, ram_addr_t size,
a8170e5e 471 hwaddr mask)
b92bb99b 472{
7267c094 473 KBDState *s = g_malloc0(sizeof(KBDState));
b92bb99b
TS
474
475 s->irq_kbd = kbd_irq;
476 s->irq_mouse = mouse_irq;
4efbe58f 477 s->mask = mask;
b92bb99b 478
0be71e32 479 vmstate_register(NULL, 0, &vmstate_kbd, s);
dbff76ac 480
2c9b15ca 481 memory_region_init_io(region, NULL, &i8042_mmio_ops, s, "i8042", size);
b92bb99b
TS
482
483 s->kbd = ps2_kbd_init(kbd_update_kbd_irq, s);
484 s->mouse = ps2_mouse_init(kbd_update_aux_irq, s);
a08d4367 485 qemu_register_reset(kbd_reset, s);
b92bb99b 486}
da85ccfb 487
a2e0b863
AF
488#define TYPE_I8042 "i8042"
489#define I8042(obj) OBJECT_CHECK(ISAKBDState, (obj), TYPE_I8042)
490
da85ccfb 491typedef struct ISAKBDState {
a2e0b863
AF
492 ISADevice parent_obj;
493
dbff76ac
RH
494 KBDState kbd;
495 MemoryRegion io[2];
da85ccfb
GH
496} ISAKBDState;
497
956a3e6b
BS
498void i8042_isa_mouse_fake_event(void *opaque)
499{
500 ISADevice *dev = opaque;
a2e0b863
AF
501 ISAKBDState *isa = I8042(dev);
502 KBDState *s = &isa->kbd;
956a3e6b
BS
503
504 ps2_mouse_fake_event(s->mouse);
505}
506
507void i8042_setup_a20_line(ISADevice *dev, qemu_irq *a20_out)
508{
a2e0b863
AF
509 ISAKBDState *isa = I8042(dev);
510 KBDState *s = &isa->kbd;
956a3e6b
BS
511
512 s->a20_out = a20_out;
513}
514
d05ac8fa 515static const VMStateDescription vmstate_kbd_isa = {
be73cfe2
JQ
516 .name = "pckbd",
517 .version_id = 3,
518 .minimum_version_id = 3,
d49805ae 519 .fields = (VMStateField[]) {
be73cfe2
JQ
520 VMSTATE_STRUCT(kbd, ISAKBDState, 0, vmstate_kbd, KBDState),
521 VMSTATE_END_OF_LIST()
522 }
523};
524
dbff76ac 525static const MemoryRegionOps i8042_data_ops = {
d540bfe0
AG
526 .read = kbd_read_data,
527 .write = kbd_write_data,
528 .impl = {
529 .min_access_size = 1,
530 .max_access_size = 1,
531 },
532 .endianness = DEVICE_LITTLE_ENDIAN,
dbff76ac
RH
533};
534
535static const MemoryRegionOps i8042_cmd_ops = {
d540bfe0
AG
536 .read = kbd_read_status,
537 .write = kbd_write_command,
538 .impl = {
539 .min_access_size = 1,
540 .max_access_size = 1,
541 },
542 .endianness = DEVICE_LITTLE_ENDIAN,
dbff76ac
RH
543};
544
db895a1e 545static void i8042_initfn(Object *obj)
da85ccfb 546{
db895a1e 547 ISAKBDState *isa_s = I8042(obj);
dbff76ac 548 KBDState *s = &isa_s->kbd;
da85ccfb 549
1437c94b
PB
550 memory_region_init_io(isa_s->io + 0, obj, &i8042_data_ops, s,
551 "i8042-data", 1);
552 memory_region_init_io(isa_s->io + 1, obj, &i8042_cmd_ops, s,
553 "i8042-cmd", 1);
db895a1e
AF
554}
555
556static void i8042_realizefn(DeviceState *dev, Error **errp)
557{
558 ISADevice *isadev = ISA_DEVICE(dev);
559 ISAKBDState *isa_s = I8042(dev);
560 KBDState *s = &isa_s->kbd;
561
562 isa_init_irq(isadev, &s->irq_kbd, 1);
563 isa_init_irq(isadev, &s->irq_mouse, 12);
564
565 isa_register_ioport(isadev, isa_s->io + 0, 0x60);
566 isa_register_ioport(isadev, isa_s->io + 1, 0x64);
da85ccfb
GH
567
568 s->kbd = ps2_kbd_init(kbd_update_kbd_irq, s);
569 s->mouse = ps2_mouse_init(kbd_update_aux_irq, s);
da85ccfb
GH
570 qemu_register_reset(kbd_reset, s);
571}
572
8f04ee08
AL
573static void i8042_class_initfn(ObjectClass *klass, void *data)
574{
39bffca2 575 DeviceClass *dc = DEVICE_CLASS(klass);
db895a1e
AF
576
577 dc->realize = i8042_realizefn;
39bffca2 578 dc->vmsd = &vmstate_kbd_isa;
8f04ee08
AL
579}
580
8c43a6f0 581static const TypeInfo i8042_info = {
a2e0b863 582 .name = TYPE_I8042,
39bffca2
AL
583 .parent = TYPE_ISA_DEVICE,
584 .instance_size = sizeof(ISAKBDState),
db895a1e 585 .instance_init = i8042_initfn,
39bffca2 586 .class_init = i8042_class_initfn,
da85ccfb
GH
587};
588
83f7d43a 589static void i8042_register_types(void)
da85ccfb 590{
39bffca2 591 type_register_static(&i8042_info);
da85ccfb 592}
83f7d43a
AF
593
594type_init(i8042_register_types)