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
25 #include "qemu/osdep.h"
26 #include "qemu/error-report.h"
28 #include "qemu/timer.h"
29 #include "hw/isa/isa.h"
30 #include "migration/vmstate.h"
31 #include "hw/acpi/aml-build.h"
32 #include "hw/input/ps2.h"
34 #include "hw/input/i8042.h"
35 #include "hw/qdev-properties.h"
36 #include "sysemu/reset.h"
37 #include "sysemu/runstate.h"
41 /* Keyboard Controller Commands */
42 #define KBD_CCMD_READ_MODE 0x20 /* Read mode bits */
43 #define KBD_CCMD_WRITE_MODE 0x60 /* Write mode bits */
44 #define KBD_CCMD_GET_VERSION 0xA1 /* Get controller version */
45 #define KBD_CCMD_MOUSE_DISABLE 0xA7 /* Disable mouse interface */
46 #define KBD_CCMD_MOUSE_ENABLE 0xA8 /* Enable mouse interface */
47 #define KBD_CCMD_TEST_MOUSE 0xA9 /* Mouse interface test */
48 #define KBD_CCMD_SELF_TEST 0xAA /* Controller self test */
49 #define KBD_CCMD_KBD_TEST 0xAB /* Keyboard interface test */
50 #define KBD_CCMD_KBD_DISABLE 0xAD /* Keyboard interface disable */
51 #define KBD_CCMD_KBD_ENABLE 0xAE /* Keyboard interface enable */
52 #define KBD_CCMD_READ_INPORT 0xC0 /* read input port */
53 #define KBD_CCMD_READ_OUTPORT 0xD0 /* read output port */
54 #define KBD_CCMD_WRITE_OUTPORT 0xD1 /* write output port */
55 #define KBD_CCMD_WRITE_OBUF 0xD2
56 #define KBD_CCMD_WRITE_AUX_OBUF 0xD3 /* Write to output buffer as if
57 initiated by the auxiliary device */
58 #define KBD_CCMD_WRITE_MOUSE 0xD4 /* Write the following byte to the mouse */
59 #define KBD_CCMD_DISABLE_A20 0xDD /* HP vectra only ? */
60 #define KBD_CCMD_ENABLE_A20 0xDF /* HP vectra only ? */
61 #define KBD_CCMD_PULSE_BITS_3_0 0xF0 /* Pulse bits 3-0 of the output port P2. */
62 #define KBD_CCMD_RESET 0xFE /* Pulse bit 0 of the output port P2 = CPU reset. */
63 #define KBD_CCMD_NO_OP 0xFF /* Pulse no bits of the output port P2. */
65 /* Status Register Bits */
66 #define KBD_STAT_OBF 0x01 /* Keyboard output buffer full */
67 #define KBD_STAT_IBF 0x02 /* Keyboard input buffer full */
68 #define KBD_STAT_SELFTEST 0x04 /* Self test successful */
69 #define KBD_STAT_CMD 0x08 /* Last write was a command write (0=data) */
70 #define KBD_STAT_UNLOCKED 0x10 /* Zero if keyboard locked */
71 #define KBD_STAT_MOUSE_OBF 0x20 /* Mouse output buffer full */
72 #define KBD_STAT_GTO 0x40 /* General receive/xmit timeout */
73 #define KBD_STAT_PERR 0x80 /* Parity error */
75 /* Controller Mode Register Bits */
76 #define KBD_MODE_KBD_INT 0x01 /* Keyboard data generate IRQ1 */
77 #define KBD_MODE_MOUSE_INT 0x02 /* Mouse data generate IRQ12 */
78 #define KBD_MODE_SYS 0x04 /* The system flag (?) */
79 #define KBD_MODE_NO_KEYLOCK 0x08 /* The keylock doesn't affect the keyboard if set */
80 #define KBD_MODE_DISABLE_KBD 0x10 /* Disable keyboard interface */
81 #define KBD_MODE_DISABLE_MOUSE 0x20 /* Disable mouse interface */
82 #define KBD_MODE_KCC 0x40 /* Scan code conversion to PC format */
83 #define KBD_MODE_RFU 0x80
85 /* Output Port Bits */
86 #define KBD_OUT_RESET 0x01 /* 1=normal mode, 0=reset */
87 #define KBD_OUT_A20 0x02 /* x86 only */
88 #define KBD_OUT_OBF 0x10 /* Keyboard output buffer full */
89 #define KBD_OUT_MOUSE_OBF 0x20 /* Mouse output buffer full */
91 /* OSes typically write 0xdd/0xdf to turn the A20 line off and on.
92 * We make the default value of the outport include these four bits,
93 * so that the subsection is rarely necessary.
95 #define KBD_OUT_ONES 0xcc
97 #define KBD_PENDING_KBD_COMPAT 0x01
98 #define KBD_PENDING_AUX_COMPAT 0x02
99 #define KBD_PENDING_CTRL_KBD 0x04
100 #define KBD_PENDING_CTRL_AUX 0x08
101 #define KBD_PENDING_KBD KBD_MODE_DISABLE_KBD /* 0x10 */
102 #define KBD_PENDING_AUX KBD_MODE_DISABLE_MOUSE /* 0x20 */
104 #define KBD_MIGR_TIMER_PENDING 0x1
106 #define KBD_OBSRC_KBD 0x01
107 #define KBD_OBSRC_MOUSE 0x02
108 #define KBD_OBSRC_CTRL 0x04
110 typedef struct KBDState
{
111 uint8_t write_cmd
; /* if non zero, write data to port 60 is expected */
115 uint32_t migration_flags
;
117 bool outport_present
;
119 bool extended_state_loaded
;
120 /* Bitmask of devices with data available. */
127 QEMUTimer
*throttle_timer
;
135 /* XXX: not generating the irqs if KBD_MODE_DISABLE_KBD is set may be
136 incorrect, but it avoids having to simulate exact delays */
137 static void kbd_update_irq_lines(KBDState
*s
)
139 int irq_kbd_level
, irq_mouse_level
;
144 if (s
->status
& KBD_STAT_OBF
) {
145 if (s
->status
& KBD_STAT_MOUSE_OBF
) {
146 if (s
->mode
& KBD_MODE_MOUSE_INT
) {
150 if ((s
->mode
& KBD_MODE_KBD_INT
) &&
151 !(s
->mode
& KBD_MODE_DISABLE_KBD
)) {
156 qemu_set_irq(s
->irq_kbd
, irq_kbd_level
);
157 qemu_set_irq(s
->irq_mouse
, irq_mouse_level
);
160 static void kbd_deassert_irq(KBDState
*s
)
162 s
->status
&= ~(KBD_STAT_OBF
| KBD_STAT_MOUSE_OBF
);
163 s
->outport
&= ~(KBD_OUT_OBF
| KBD_OUT_MOUSE_OBF
);
164 kbd_update_irq_lines(s
);
167 static uint8_t kbd_pending(KBDState
*s
)
169 if (s
->extended_state
) {
170 return s
->pending
& (~s
->mode
| ~(KBD_PENDING_KBD
| KBD_PENDING_AUX
));
176 /* update irq and KBD_STAT_[MOUSE_]OBF */
177 static void kbd_update_irq(KBDState
*s
)
179 uint8_t pending
= kbd_pending(s
);
181 s
->status
&= ~(KBD_STAT_OBF
| KBD_STAT_MOUSE_OBF
);
182 s
->outport
&= ~(KBD_OUT_OBF
| KBD_OUT_MOUSE_OBF
);
184 s
->status
|= KBD_STAT_OBF
;
185 s
->outport
|= KBD_OUT_OBF
;
186 if (pending
& KBD_PENDING_CTRL_KBD
) {
187 s
->obsrc
= KBD_OBSRC_CTRL
;
188 } else if (pending
& KBD_PENDING_CTRL_AUX
) {
189 s
->status
|= KBD_STAT_MOUSE_OBF
;
190 s
->outport
|= KBD_OUT_MOUSE_OBF
;
191 s
->obsrc
= KBD_OBSRC_CTRL
;
192 } else if (pending
& KBD_PENDING_KBD
) {
193 s
->obsrc
= KBD_OBSRC_KBD
;
195 s
->status
|= KBD_STAT_MOUSE_OBF
;
196 s
->outport
|= KBD_OUT_MOUSE_OBF
;
197 s
->obsrc
= KBD_OBSRC_MOUSE
;
200 kbd_update_irq_lines(s
);
203 static void kbd_safe_update_irq(KBDState
*s
)
206 * with KBD_STAT_OBF set, a call to kbd_read_data() will eventually call
209 if (s
->status
& KBD_STAT_OBF
) {
212 /* the throttle timer is pending and will call kbd_update_irq() */
213 if (s
->throttle_timer
&& timer_pending(s
->throttle_timer
)) {
216 if (kbd_pending(s
)) {
221 static void kbd_update_kbd_irq(void *opaque
, int level
)
223 KBDState
*s
= opaque
;
226 s
->pending
|= KBD_PENDING_KBD
;
228 s
->pending
&= ~KBD_PENDING_KBD
;
230 kbd_safe_update_irq(s
);
233 static void kbd_update_aux_irq(void *opaque
, int level
)
235 KBDState
*s
= opaque
;
238 s
->pending
|= KBD_PENDING_AUX
;
240 s
->pending
&= ~KBD_PENDING_AUX
;
242 kbd_safe_update_irq(s
);
245 static void kbd_throttle_timeout(void *opaque
)
247 KBDState
*s
= opaque
;
249 if (kbd_pending(s
)) {
254 static uint64_t kbd_read_status(void *opaque
, hwaddr addr
,
257 KBDState
*s
= opaque
;
260 trace_pckbd_kbd_read_status(val
);
264 static void kbd_queue(KBDState
*s
, int b
, int aux
)
266 if (s
->extended_state
) {
268 s
->pending
&= ~KBD_PENDING_CTRL_KBD
& ~KBD_PENDING_CTRL_AUX
;
269 s
->pending
|= aux
? KBD_PENDING_CTRL_AUX
: KBD_PENDING_CTRL_KBD
;
270 kbd_safe_update_irq(s
);
272 ps2_queue(aux
? s
->mouse
: s
->kbd
, b
);
276 static uint8_t kbd_dequeue(KBDState
*s
)
278 uint8_t b
= s
->cbdata
;
280 s
->pending
&= ~KBD_PENDING_CTRL_KBD
& ~KBD_PENDING_CTRL_AUX
;
281 if (kbd_pending(s
)) {
287 static void outport_write(KBDState
*s
, uint32_t val
)
289 trace_pckbd_outport_write(val
);
291 qemu_set_irq(s
->a20_out
, (val
>> 1) & 1);
293 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET
);
297 static void kbd_write_command(void *opaque
, hwaddr addr
,
298 uint64_t val
, unsigned size
)
300 KBDState
*s
= opaque
;
302 trace_pckbd_kbd_write_command(val
);
304 /* Bits 3-0 of the output port P2 of the keyboard controller may be pulsed
305 * low for approximately 6 micro seconds. Bits 3-0 of the KBD_CCMD_PULSE
306 * command specify the output port bits to be pulsed.
307 * 0: Bit should be pulsed. 1: Bit should not be modified.
308 * The only useful version of this command is pulsing bit 0,
309 * which does a CPU reset.
311 if((val
& KBD_CCMD_PULSE_BITS_3_0
) == KBD_CCMD_PULSE_BITS_3_0
) {
313 val
= KBD_CCMD_RESET
;
315 val
= KBD_CCMD_NO_OP
;
319 case KBD_CCMD_READ_MODE
:
320 kbd_queue(s
, s
->mode
, 0);
322 case KBD_CCMD_WRITE_MODE
:
323 case KBD_CCMD_WRITE_OBUF
:
324 case KBD_CCMD_WRITE_AUX_OBUF
:
325 case KBD_CCMD_WRITE_MOUSE
:
326 case KBD_CCMD_WRITE_OUTPORT
:
329 case KBD_CCMD_MOUSE_DISABLE
:
330 s
->mode
|= KBD_MODE_DISABLE_MOUSE
;
332 case KBD_CCMD_MOUSE_ENABLE
:
333 s
->mode
&= ~KBD_MODE_DISABLE_MOUSE
;
334 kbd_safe_update_irq(s
);
336 case KBD_CCMD_TEST_MOUSE
:
337 kbd_queue(s
, 0x00, 0);
339 case KBD_CCMD_SELF_TEST
:
340 s
->status
|= KBD_STAT_SELFTEST
;
341 kbd_queue(s
, 0x55, 0);
343 case KBD_CCMD_KBD_TEST
:
344 kbd_queue(s
, 0x00, 0);
346 case KBD_CCMD_KBD_DISABLE
:
347 s
->mode
|= KBD_MODE_DISABLE_KBD
;
349 case KBD_CCMD_KBD_ENABLE
:
350 s
->mode
&= ~KBD_MODE_DISABLE_KBD
;
351 kbd_safe_update_irq(s
);
353 case KBD_CCMD_READ_INPORT
:
354 kbd_queue(s
, 0x80, 0);
356 case KBD_CCMD_READ_OUTPORT
:
357 kbd_queue(s
, s
->outport
, 0);
359 case KBD_CCMD_ENABLE_A20
:
360 qemu_irq_raise(s
->a20_out
);
361 s
->outport
|= KBD_OUT_A20
;
363 case KBD_CCMD_DISABLE_A20
:
364 qemu_irq_lower(s
->a20_out
);
365 s
->outport
&= ~KBD_OUT_A20
;
368 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET
);
374 qemu_log_mask(LOG_GUEST_ERROR
,
375 "unsupported keyboard cmd=0x%02" PRIx64
"\n", val
);
380 static uint64_t kbd_read_data(void *opaque
, hwaddr addr
,
383 KBDState
*s
= opaque
;
385 if (s
->status
& KBD_STAT_OBF
) {
387 if (s
->obsrc
& KBD_OBSRC_KBD
) {
388 if (s
->throttle_timer
) {
389 timer_mod(s
->throttle_timer
,
390 qemu_clock_get_us(QEMU_CLOCK_VIRTUAL
) + 1000);
392 s
->obdata
= ps2_read_data(s
->kbd
);
393 } else if (s
->obsrc
& KBD_OBSRC_MOUSE
) {
394 s
->obdata
= ps2_read_data(s
->mouse
);
395 } else if (s
->obsrc
& KBD_OBSRC_CTRL
) {
396 s
->obdata
= kbd_dequeue(s
);
400 trace_pckbd_kbd_read_data(s
->obdata
);
404 static void kbd_write_data(void *opaque
, hwaddr addr
,
405 uint64_t val
, unsigned size
)
407 KBDState
*s
= opaque
;
409 trace_pckbd_kbd_write_data(val
);
411 switch(s
->write_cmd
) {
413 ps2_write_keyboard(s
->kbd
, val
);
414 /* sending data to the keyboard reenables PS/2 communication */
415 s
->mode
&= ~KBD_MODE_DISABLE_KBD
;
416 kbd_safe_update_irq(s
);
418 case KBD_CCMD_WRITE_MODE
:
420 ps2_keyboard_set_translation(s
->kbd
, (s
->mode
& KBD_MODE_KCC
) != 0);
422 * a write to the mode byte interrupt enable flags directly updates
425 kbd_update_irq_lines(s
);
427 * a write to the mode byte disable interface flags may raise
428 * an irq if there is pending data in the PS/2 queues.
430 kbd_safe_update_irq(s
);
432 case KBD_CCMD_WRITE_OBUF
:
433 kbd_queue(s
, val
, 0);
435 case KBD_CCMD_WRITE_AUX_OBUF
:
436 kbd_queue(s
, val
, 1);
438 case KBD_CCMD_WRITE_OUTPORT
:
439 outport_write(s
, val
);
441 case KBD_CCMD_WRITE_MOUSE
:
442 ps2_write_mouse(s
->mouse
, val
);
443 /* sending data to the mouse reenables PS/2 communication */
444 s
->mode
&= ~KBD_MODE_DISABLE_MOUSE
;
445 kbd_safe_update_irq(s
);
453 static void kbd_reset(void *opaque
)
455 KBDState
*s
= opaque
;
457 s
->mode
= KBD_MODE_KBD_INT
| KBD_MODE_MOUSE_INT
;
458 s
->status
= KBD_STAT_CMD
| KBD_STAT_UNLOCKED
;
459 s
->outport
= KBD_OUT_RESET
| KBD_OUT_A20
| KBD_OUT_ONES
;
462 if (s
->throttle_timer
) {
463 timer_del(s
->throttle_timer
);
467 static uint8_t kbd_outport_default(KBDState
*s
)
469 return KBD_OUT_RESET
| KBD_OUT_A20
| KBD_OUT_ONES
470 | (s
->status
& KBD_STAT_OBF
? KBD_OUT_OBF
: 0)
471 | (s
->status
& KBD_STAT_MOUSE_OBF
? KBD_OUT_MOUSE_OBF
: 0);
474 static int kbd_outport_post_load(void *opaque
, int version_id
)
476 KBDState
*s
= opaque
;
477 s
->outport_present
= true;
481 static bool kbd_outport_needed(void *opaque
)
483 KBDState
*s
= opaque
;
484 return s
->outport
!= kbd_outport_default(s
);
487 static const VMStateDescription vmstate_kbd_outport
= {
488 .name
= "pckbd_outport",
490 .minimum_version_id
= 1,
491 .post_load
= kbd_outport_post_load
,
492 .needed
= kbd_outport_needed
,
493 .fields
= (VMStateField
[]) {
494 VMSTATE_UINT8(outport
, KBDState
),
495 VMSTATE_END_OF_LIST()
499 static int kbd_extended_state_pre_save(void *opaque
)
501 KBDState
*s
= opaque
;
503 s
->migration_flags
= 0;
504 if (s
->throttle_timer
&& timer_pending(s
->throttle_timer
)) {
505 s
->migration_flags
|= KBD_MIGR_TIMER_PENDING
;
511 static int kbd_extended_state_post_load(void *opaque
, int version_id
)
513 KBDState
*s
= opaque
;
515 if (s
->migration_flags
& KBD_MIGR_TIMER_PENDING
) {
516 kbd_throttle_timeout(s
);
518 s
->extended_state_loaded
= true;
523 static bool kbd_extended_state_needed(void *opaque
)
525 KBDState
*s
= opaque
;
527 return s
->extended_state
;
530 static const VMStateDescription vmstate_kbd_extended_state
= {
531 .name
= "pckbd/extended_state",
532 .post_load
= kbd_extended_state_post_load
,
533 .pre_save
= kbd_extended_state_pre_save
,
534 .needed
= kbd_extended_state_needed
,
535 .fields
= (VMStateField
[]) {
536 VMSTATE_UINT32(migration_flags
, KBDState
),
537 VMSTATE_UINT32(obsrc
, KBDState
),
538 VMSTATE_UINT8(obdata
, KBDState
),
539 VMSTATE_UINT8(cbdata
, KBDState
),
540 VMSTATE_END_OF_LIST()
544 static int kbd_pre_save(void *opaque
)
546 KBDState
*s
= opaque
;
548 if (s
->extended_state
) {
549 s
->pending_tmp
= s
->pending
;
552 if (s
->pending
& KBD_PENDING_KBD
) {
553 s
->pending_tmp
|= KBD_PENDING_KBD_COMPAT
;
555 if (s
->pending
& KBD_PENDING_AUX
) {
556 s
->pending_tmp
|= KBD_PENDING_AUX_COMPAT
;
562 static int kbd_pre_load(void *opaque
)
564 KBDState
*s
= opaque
;
566 s
->outport_present
= false;
567 s
->extended_state_loaded
= false;
571 static int kbd_post_load(void *opaque
, int version_id
)
573 KBDState
*s
= opaque
;
574 if (!s
->outport_present
) {
575 s
->outport
= kbd_outport_default(s
);
577 s
->pending
= s
->pending_tmp
;
578 if (!s
->extended_state_loaded
) {
579 s
->obsrc
= s
->status
& KBD_STAT_OBF
?
580 (s
->status
& KBD_STAT_MOUSE_OBF
? KBD_OBSRC_MOUSE
: KBD_OBSRC_KBD
) :
582 if (s
->pending
& KBD_PENDING_KBD_COMPAT
) {
583 s
->pending
|= KBD_PENDING_KBD
;
585 if (s
->pending
& KBD_PENDING_AUX_COMPAT
) {
586 s
->pending
|= KBD_PENDING_AUX
;
589 /* clear all unused flags */
590 s
->pending
&= KBD_PENDING_CTRL_KBD
| KBD_PENDING_CTRL_AUX
|
591 KBD_PENDING_KBD
| KBD_PENDING_AUX
;
595 static const VMStateDescription vmstate_kbd
= {
598 .minimum_version_id
= 3,
599 .pre_load
= kbd_pre_load
,
600 .post_load
= kbd_post_load
,
601 .pre_save
= kbd_pre_save
,
602 .fields
= (VMStateField
[]) {
603 VMSTATE_UINT8(write_cmd
, KBDState
),
604 VMSTATE_UINT8(status
, KBDState
),
605 VMSTATE_UINT8(mode
, KBDState
),
606 VMSTATE_UINT8(pending_tmp
, KBDState
),
607 VMSTATE_END_OF_LIST()
609 .subsections
= (const VMStateDescription
*[]) {
610 &vmstate_kbd_outport
,
611 &vmstate_kbd_extended_state
,
616 /* Memory mapped interface */
617 static uint64_t kbd_mm_readfn(void *opaque
, hwaddr addr
, unsigned size
)
619 KBDState
*s
= opaque
;
622 return kbd_read_status(s
, 0, 1) & 0xff;
624 return kbd_read_data(s
, 0, 1) & 0xff;
627 static void kbd_mm_writefn(void *opaque
, hwaddr addr
,
628 uint64_t value
, unsigned size
)
630 KBDState
*s
= opaque
;
633 kbd_write_command(s
, 0, value
& 0xff, 1);
635 kbd_write_data(s
, 0, value
& 0xff, 1);
639 static const MemoryRegionOps i8042_mmio_ops
= {
640 .read
= kbd_mm_readfn
,
641 .write
= kbd_mm_writefn
,
642 .valid
.min_access_size
= 1,
643 .valid
.max_access_size
= 4,
644 .endianness
= DEVICE_NATIVE_ENDIAN
,
647 void i8042_mm_init(qemu_irq kbd_irq
, qemu_irq mouse_irq
,
648 MemoryRegion
*region
, ram_addr_t size
,
651 KBDState
*s
= g_malloc0(sizeof(KBDState
));
653 s
->irq_kbd
= kbd_irq
;
654 s
->irq_mouse
= mouse_irq
;
657 s
->extended_state
= true;
659 vmstate_register(NULL
, 0, &vmstate_kbd
, s
);
661 memory_region_init_io(region
, NULL
, &i8042_mmio_ops
, s
, "i8042", size
);
663 s
->kbd
= ps2_kbd_init(kbd_update_kbd_irq
, s
);
664 s
->mouse
= ps2_mouse_init(kbd_update_aux_irq
, s
);
665 qemu_register_reset(kbd_reset
, s
);
669 ISADevice parent_obj
;
676 void i8042_isa_mouse_fake_event(ISAKBDState
*isa
)
678 KBDState
*s
= &isa
->kbd
;
680 ps2_mouse_fake_event(s
->mouse
);
683 void i8042_setup_a20_line(ISADevice
*dev
, qemu_irq a20_out
)
685 qdev_connect_gpio_out_named(DEVICE(dev
), I8042_A20_LINE
, 0, a20_out
);
688 static const VMStateDescription vmstate_kbd_isa
= {
691 .minimum_version_id
= 3,
692 .fields
= (VMStateField
[]) {
693 VMSTATE_STRUCT(kbd
, ISAKBDState
, 0, vmstate_kbd
, KBDState
),
694 VMSTATE_END_OF_LIST()
698 static const MemoryRegionOps i8042_data_ops
= {
699 .read
= kbd_read_data
,
700 .write
= kbd_write_data
,
702 .min_access_size
= 1,
703 .max_access_size
= 1,
705 .endianness
= DEVICE_LITTLE_ENDIAN
,
708 static const MemoryRegionOps i8042_cmd_ops
= {
709 .read
= kbd_read_status
,
710 .write
= kbd_write_command
,
712 .min_access_size
= 1,
713 .max_access_size
= 1,
715 .endianness
= DEVICE_LITTLE_ENDIAN
,
718 static void i8042_initfn(Object
*obj
)
720 ISAKBDState
*isa_s
= I8042(obj
);
721 KBDState
*s
= &isa_s
->kbd
;
723 memory_region_init_io(isa_s
->io
+ 0, obj
, &i8042_data_ops
, s
,
725 memory_region_init_io(isa_s
->io
+ 1, obj
, &i8042_cmd_ops
, s
,
728 qdev_init_gpio_out_named(DEVICE(obj
), &s
->a20_out
, I8042_A20_LINE
, 1);
731 static void i8042_realizefn(DeviceState
*dev
, Error
**errp
)
733 ISADevice
*isadev
= ISA_DEVICE(dev
);
734 ISAKBDState
*isa_s
= I8042(dev
);
735 KBDState
*s
= &isa_s
->kbd
;
737 isa_init_irq(isadev
, &s
->irq_kbd
, 1);
738 isa_init_irq(isadev
, &s
->irq_mouse
, 12);
740 isa_register_ioport(isadev
, isa_s
->io
+ 0, 0x60);
741 isa_register_ioport(isadev
, isa_s
->io
+ 1, 0x64);
743 s
->kbd
= ps2_kbd_init(kbd_update_kbd_irq
, s
);
744 s
->mouse
= ps2_mouse_init(kbd_update_aux_irq
, s
);
745 if (isa_s
->kbd_throttle
&& !isa_s
->kbd
.extended_state
) {
746 warn_report(TYPE_I8042
": can't enable kbd-throttle without"
747 " extended-state, disabling kbd-throttle");
748 } else if (isa_s
->kbd_throttle
) {
749 s
->throttle_timer
= timer_new_us(QEMU_CLOCK_VIRTUAL
,
750 kbd_throttle_timeout
, s
);
752 qemu_register_reset(kbd_reset
, s
);
755 static void i8042_build_aml(ISADevice
*isadev
, Aml
*scope
)
761 crs
= aml_resource_template();
762 aml_append(crs
, aml_io(AML_DECODE16
, 0x0060, 0x0060, 0x01, 0x01));
763 aml_append(crs
, aml_io(AML_DECODE16
, 0x0064, 0x0064, 0x01, 0x01));
764 aml_append(crs
, aml_irq_no_flags(1));
766 kbd
= aml_device("KBD");
767 aml_append(kbd
, aml_name_decl("_HID", aml_eisaid("PNP0303")));
768 aml_append(kbd
, aml_name_decl("_STA", aml_int(0xf)));
769 aml_append(kbd
, aml_name_decl("_CRS", crs
));
771 crs
= aml_resource_template();
772 aml_append(crs
, aml_irq_no_flags(12));
774 mou
= aml_device("MOU");
775 aml_append(mou
, aml_name_decl("_HID", aml_eisaid("PNP0F13")));
776 aml_append(mou
, aml_name_decl("_STA", aml_int(0xf)));
777 aml_append(mou
, aml_name_decl("_CRS", crs
));
779 aml_append(scope
, kbd
);
780 aml_append(scope
, mou
);
783 static Property i8042_properties
[] = {
784 DEFINE_PROP_BOOL("extended-state", ISAKBDState
, kbd
.extended_state
, true),
785 DEFINE_PROP_BOOL("kbd-throttle", ISAKBDState
, kbd_throttle
, false),
786 DEFINE_PROP_END_OF_LIST(),
789 static void i8042_class_initfn(ObjectClass
*klass
, void *data
)
791 DeviceClass
*dc
= DEVICE_CLASS(klass
);
792 ISADeviceClass
*isa
= ISA_DEVICE_CLASS(klass
);
794 device_class_set_props(dc
, i8042_properties
);
795 dc
->realize
= i8042_realizefn
;
796 dc
->vmsd
= &vmstate_kbd_isa
;
797 isa
->build_aml
= i8042_build_aml
;
798 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
801 static const TypeInfo i8042_info
= {
803 .parent
= TYPE_ISA_DEVICE
,
804 .instance_size
= sizeof(ISAKBDState
),
805 .instance_init
= i8042_initfn
,
806 .class_init
= i8042_class_initfn
,
809 static void i8042_register_types(void)
811 type_register_static(&i8042_info
);
814 type_init(i8042_register_types
)