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