bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 1 | /* |
| 2 | * QEMU PC keyboard emulation |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3 | * |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 4 | * Copyright (c) 2003 Fabrice Bellard |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5 | * |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 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 | */ |
Markus Armbruster | 71e8a91 | 2019-08-12 07:23:38 +0200 | [diff] [blame] | 24 | |
Peter Maydell | 0430891 | 2016-01-26 18:17:30 +0000 | [diff] [blame] | 25 | #include "qemu/osdep.h" |
Volker Rümelin | d1e4566 | 2021-05-25 20:14:35 +0200 | [diff] [blame] | 26 | #include "qemu/error-report.h" |
Philippe Mathieu-Daudé | c2e846b | 2018-06-26 17:50:40 +0100 | [diff] [blame] | 27 | #include "qemu/log.h" |
Volker Rümelin | d1e4566 | 2021-05-25 20:14:35 +0200 | [diff] [blame] | 28 | #include "qemu/timer.h" |
Bernhard Beschow | b86ce7a | 2022-03-01 23:00:33 +0100 | [diff] [blame] | 29 | #include "qapi/error.h" |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 30 | #include "hw/isa/isa.h" |
Markus Armbruster | d645427 | 2019-08-12 07:23:45 +0200 | [diff] [blame] | 31 | #include "migration/vmstate.h" |
Igor Mammedov | 68f0131 | 2022-06-08 09:53:12 -0400 | [diff] [blame] | 32 | #include "hw/acpi/acpi_aml_interface.h" |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 33 | #include "hw/input/ps2.h" |
Markus Armbruster | 64552b6 | 2019-08-12 07:23:42 +0200 | [diff] [blame] | 34 | #include "hw/irq.h" |
Philippe Mathieu-Daudé | 47973a2 | 2018-03-08 23:39:24 +0100 | [diff] [blame] | 35 | #include "hw/input/i8042.h" |
Volker Rümelin | ff6e162 | 2021-05-25 20:14:34 +0200 | [diff] [blame] | 36 | #include "hw/qdev-properties.h" |
Markus Armbruster | 71e8a91 | 2019-08-12 07:23:38 +0200 | [diff] [blame] | 37 | #include "sysemu/reset.h" |
Markus Armbruster | 54d3123 | 2019-08-12 07:23:59 +0200 | [diff] [blame] | 38 | #include "sysemu/runstate.h" |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 39 | |
Dr. David Alan Gilbert | 65b182c | 2018-10-16 12:22:32 +0100 | [diff] [blame] | 40 | #include "trace.h" |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 41 | |
Mark Cave-Ayland | 32be015 | 2022-06-24 14:40:33 +0100 | [diff] [blame] | 42 | /* Keyboard Controller Commands */ |
| 43 | |
| 44 | /* Read mode bits */ |
| 45 | #define KBD_CCMD_READ_MODE 0x20 |
| 46 | /* Write mode bits */ |
| 47 | #define KBD_CCMD_WRITE_MODE 0x60 |
| 48 | /* Get controller version */ |
| 49 | #define KBD_CCMD_GET_VERSION 0xA1 |
| 50 | /* Disable mouse interface */ |
| 51 | #define KBD_CCMD_MOUSE_DISABLE 0xA7 |
| 52 | /* Enable mouse interface */ |
| 53 | #define KBD_CCMD_MOUSE_ENABLE 0xA8 |
| 54 | /* Mouse interface test */ |
| 55 | #define KBD_CCMD_TEST_MOUSE 0xA9 |
| 56 | /* Controller self test */ |
| 57 | #define KBD_CCMD_SELF_TEST 0xAA |
| 58 | /* Keyboard interface test */ |
| 59 | #define KBD_CCMD_KBD_TEST 0xAB |
| 60 | /* Keyboard interface disable */ |
| 61 | #define KBD_CCMD_KBD_DISABLE 0xAD |
| 62 | /* Keyboard interface enable */ |
| 63 | #define KBD_CCMD_KBD_ENABLE 0xAE |
| 64 | /* read input port */ |
| 65 | #define KBD_CCMD_READ_INPORT 0xC0 |
| 66 | /* read output port */ |
| 67 | #define KBD_CCMD_READ_OUTPORT 0xD0 |
| 68 | /* write output port */ |
| 69 | #define KBD_CCMD_WRITE_OUTPORT 0xD1 |
| 70 | #define KBD_CCMD_WRITE_OBUF 0xD2 |
| 71 | /* Write to output buffer as if initiated by the auxiliary device */ |
| 72 | #define KBD_CCMD_WRITE_AUX_OBUF 0xD3 |
| 73 | /* Write the following byte to the mouse */ |
| 74 | #define KBD_CCMD_WRITE_MOUSE 0xD4 |
| 75 | /* HP vectra only ? */ |
| 76 | #define KBD_CCMD_DISABLE_A20 0xDD |
| 77 | /* HP vectra only ? */ |
| 78 | #define KBD_CCMD_ENABLE_A20 0xDF |
| 79 | /* Pulse bits 3-0 of the output port P2. */ |
| 80 | #define KBD_CCMD_PULSE_BITS_3_0 0xF0 |
| 81 | /* Pulse bit 0 of the output port P2 = CPU reset. */ |
| 82 | #define KBD_CCMD_RESET 0xFE |
| 83 | /* Pulse no bits of the output port P2. */ |
| 84 | #define KBD_CCMD_NO_OP 0xFF |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 85 | |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 86 | /* Status Register Bits */ |
Mark Cave-Ayland | 32be015 | 2022-06-24 14:40:33 +0100 | [diff] [blame] | 87 | |
| 88 | /* Keyboard output buffer full */ |
| 89 | #define KBD_STAT_OBF 0x01 |
| 90 | /* Keyboard input buffer full */ |
| 91 | #define KBD_STAT_IBF 0x02 |
| 92 | /* Self test successful */ |
| 93 | #define KBD_STAT_SELFTEST 0x04 |
| 94 | /* Last write was a command write (0=data) */ |
| 95 | #define KBD_STAT_CMD 0x08 |
| 96 | /* Zero if keyboard locked */ |
| 97 | #define KBD_STAT_UNLOCKED 0x10 |
| 98 | /* Mouse output buffer full */ |
| 99 | #define KBD_STAT_MOUSE_OBF 0x20 |
| 100 | /* General receive/xmit timeout */ |
| 101 | #define KBD_STAT_GTO 0x40 |
| 102 | /* Parity error */ |
| 103 | #define KBD_STAT_PERR 0x80 |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 104 | |
| 105 | /* Controller Mode Register Bits */ |
Mark Cave-Ayland | 32be015 | 2022-06-24 14:40:33 +0100 | [diff] [blame] | 106 | |
| 107 | /* Keyboard data generate IRQ1 */ |
| 108 | #define KBD_MODE_KBD_INT 0x01 |
| 109 | /* Mouse data generate IRQ12 */ |
| 110 | #define KBD_MODE_MOUSE_INT 0x02 |
| 111 | /* The system flag (?) */ |
| 112 | #define KBD_MODE_SYS 0x04 |
| 113 | /* The keylock doesn't affect the keyboard if set */ |
| 114 | #define KBD_MODE_NO_KEYLOCK 0x08 |
| 115 | /* Disable keyboard interface */ |
| 116 | #define KBD_MODE_DISABLE_KBD 0x10 |
| 117 | /* Disable mouse interface */ |
| 118 | #define KBD_MODE_DISABLE_MOUSE 0x20 |
| 119 | /* Scan code conversion to PC format */ |
| 120 | #define KBD_MODE_KCC 0x40 |
| 121 | #define KBD_MODE_RFU 0x80 |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 122 | |
Blue Swirl | 956a3e6 | 2010-05-22 07:59:01 +0000 | [diff] [blame] | 123 | /* Output Port Bits */ |
| 124 | #define KBD_OUT_RESET 0x01 /* 1=normal mode, 0=reset */ |
| 125 | #define KBD_OUT_A20 0x02 /* x86 only */ |
| 126 | #define KBD_OUT_OBF 0x10 /* Keyboard output buffer full */ |
| 127 | #define KBD_OUT_MOUSE_OBF 0x20 /* Mouse output buffer full */ |
| 128 | |
Mark Cave-Ayland | 32be015 | 2022-06-24 14:40:33 +0100 | [diff] [blame] | 129 | /* |
| 130 | * OSes typically write 0xdd/0xdf to turn the A20 line off and on. |
Paolo Bonzini | d13c040 | 2014-12-22 08:55:19 +0100 | [diff] [blame] | 131 | * We make the default value of the outport include these four bits, |
| 132 | * so that the subsection is rarely necessary. |
| 133 | */ |
| 134 | #define KBD_OUT_ONES 0xcc |
| 135 | |
Volker Rümelin | f6f57a8 | 2021-05-25 20:14:39 +0200 | [diff] [blame] | 136 | #define KBD_PENDING_KBD_COMPAT 0x01 |
| 137 | #define KBD_PENDING_AUX_COMPAT 0x02 |
Volker Rümelin | aa67a42 | 2021-05-25 20:14:37 +0200 | [diff] [blame] | 138 | #define KBD_PENDING_CTRL_KBD 0x04 |
| 139 | #define KBD_PENDING_CTRL_AUX 0x08 |
Volker Rümelin | f6f57a8 | 2021-05-25 20:14:39 +0200 | [diff] [blame] | 140 | #define KBD_PENDING_KBD KBD_MODE_DISABLE_KBD /* 0x10 */ |
| 141 | #define KBD_PENDING_AUX KBD_MODE_DISABLE_MOUSE /* 0x20 */ |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 142 | |
Volker Rümelin | d1e4566 | 2021-05-25 20:14:35 +0200 | [diff] [blame] | 143 | #define KBD_MIGR_TIMER_PENDING 0x1 |
| 144 | |
Volker Rümelin | ac9192b | 2021-05-25 20:14:36 +0200 | [diff] [blame] | 145 | #define KBD_OBSRC_KBD 0x01 |
| 146 | #define KBD_OBSRC_MOUSE 0x02 |
Volker Rümelin | aa67a42 | 2021-05-25 20:14:37 +0200 | [diff] [blame] | 147 | #define KBD_OBSRC_CTRL 0x04 |
Volker Rümelin | ac9192b | 2021-05-25 20:14:36 +0200 | [diff] [blame] | 148 | |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 149 | |
Mark Cave-Ayland | 32be015 | 2022-06-24 14:40:33 +0100 | [diff] [blame] | 150 | /* |
| 151 | * XXX: not generating the irqs if KBD_MODE_DISABLE_KBD is set may be |
| 152 | * incorrect, but it avoids having to simulate exact delays |
| 153 | */ |
Volker Rümelin | c3c4a96 | 2021-05-25 20:14:33 +0200 | [diff] [blame] | 154 | static void kbd_update_irq_lines(KBDState *s) |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 155 | { |
ths | b7678d9 | 2007-02-18 00:08:44 +0000 | [diff] [blame] | 156 | int irq_kbd_level, irq_mouse_level; |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 157 | |
ths | b7678d9 | 2007-02-18 00:08:44 +0000 | [diff] [blame] | 158 | irq_kbd_level = 0; |
| 159 | irq_mouse_level = 0; |
Volker Rümelin | c3c4a96 | 2021-05-25 20:14:33 +0200 | [diff] [blame] | 160 | |
| 161 | if (s->status & KBD_STAT_OBF) { |
| 162 | if (s->status & KBD_STAT_MOUSE_OBF) { |
| 163 | if (s->mode & KBD_MODE_MOUSE_INT) { |
| 164 | irq_mouse_level = 1; |
| 165 | } |
| 166 | } else { |
| 167 | if ((s->mode & KBD_MODE_KBD_INT) && |
| 168 | !(s->mode & KBD_MODE_DISABLE_KBD)) { |
| 169 | irq_kbd_level = 1; |
| 170 | } |
| 171 | } |
| 172 | } |
Mark Cave-Ayland | c2b1747 | 2022-06-24 14:40:50 +0100 | [diff] [blame] | 173 | qemu_set_irq(s->irqs[I8042_KBD_IRQ], irq_kbd_level); |
| 174 | qemu_set_irq(s->irqs[I8042_MOUSE_IRQ], irq_mouse_level); |
Volker Rümelin | c3c4a96 | 2021-05-25 20:14:33 +0200 | [diff] [blame] | 175 | } |
| 176 | |
Volker Rümelin | ff6e162 | 2021-05-25 20:14:34 +0200 | [diff] [blame] | 177 | static void kbd_deassert_irq(KBDState *s) |
| 178 | { |
| 179 | s->status &= ~(KBD_STAT_OBF | KBD_STAT_MOUSE_OBF); |
| 180 | s->outport &= ~(KBD_OUT_OBF | KBD_OUT_MOUSE_OBF); |
| 181 | kbd_update_irq_lines(s); |
| 182 | } |
| 183 | |
Volker Rümelin | e4697fa | 2021-05-25 20:14:38 +0200 | [diff] [blame] | 184 | static uint8_t kbd_pending(KBDState *s) |
| 185 | { |
Volker Rümelin | f6f57a8 | 2021-05-25 20:14:39 +0200 | [diff] [blame] | 186 | if (s->extended_state) { |
| 187 | return s->pending & (~s->mode | ~(KBD_PENDING_KBD | KBD_PENDING_AUX)); |
| 188 | } else { |
| 189 | return s->pending; |
| 190 | } |
Volker Rümelin | e4697fa | 2021-05-25 20:14:38 +0200 | [diff] [blame] | 191 | } |
| 192 | |
Volker Rümelin | c3c4a96 | 2021-05-25 20:14:33 +0200 | [diff] [blame] | 193 | /* update irq and KBD_STAT_[MOUSE_]OBF */ |
| 194 | static void kbd_update_irq(KBDState *s) |
| 195 | { |
Volker Rümelin | e4697fa | 2021-05-25 20:14:38 +0200 | [diff] [blame] | 196 | uint8_t pending = kbd_pending(s); |
| 197 | |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 198 | s->status &= ~(KBD_STAT_OBF | KBD_STAT_MOUSE_OBF); |
Blue Swirl | 956a3e6 | 2010-05-22 07:59:01 +0000 | [diff] [blame] | 199 | s->outport &= ~(KBD_OUT_OBF | KBD_OUT_MOUSE_OBF); |
Volker Rümelin | e4697fa | 2021-05-25 20:14:38 +0200 | [diff] [blame] | 200 | if (pending) { |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 201 | s->status |= KBD_STAT_OBF; |
Blue Swirl | 956a3e6 | 2010-05-22 07:59:01 +0000 | [diff] [blame] | 202 | s->outport |= KBD_OUT_OBF; |
Volker Rümelin | e4697fa | 2021-05-25 20:14:38 +0200 | [diff] [blame] | 203 | if (pending & KBD_PENDING_CTRL_KBD) { |
Volker Rümelin | aa67a42 | 2021-05-25 20:14:37 +0200 | [diff] [blame] | 204 | s->obsrc = KBD_OBSRC_CTRL; |
Volker Rümelin | e4697fa | 2021-05-25 20:14:38 +0200 | [diff] [blame] | 205 | } else if (pending & KBD_PENDING_CTRL_AUX) { |
Volker Rümelin | aa67a42 | 2021-05-25 20:14:37 +0200 | [diff] [blame] | 206 | s->status |= KBD_STAT_MOUSE_OBF; |
| 207 | s->outport |= KBD_OUT_MOUSE_OBF; |
| 208 | s->obsrc = KBD_OBSRC_CTRL; |
Volker Rümelin | e4697fa | 2021-05-25 20:14:38 +0200 | [diff] [blame] | 209 | } else if (pending & KBD_PENDING_KBD) { |
Volker Rümelin | aa67a42 | 2021-05-25 20:14:37 +0200 | [diff] [blame] | 210 | s->obsrc = KBD_OBSRC_KBD; |
| 211 | } else { |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 212 | s->status |= KBD_STAT_MOUSE_OBF; |
Blue Swirl | 956a3e6 | 2010-05-22 07:59:01 +0000 | [diff] [blame] | 213 | s->outport |= KBD_OUT_MOUSE_OBF; |
Volker Rümelin | ac9192b | 2021-05-25 20:14:36 +0200 | [diff] [blame] | 214 | s->obsrc = KBD_OBSRC_MOUSE; |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 215 | } |
| 216 | } |
Volker Rümelin | c3c4a96 | 2021-05-25 20:14:33 +0200 | [diff] [blame] | 217 | kbd_update_irq_lines(s); |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Volker Rümelin | ff6e162 | 2021-05-25 20:14:34 +0200 | [diff] [blame] | 220 | static void kbd_safe_update_irq(KBDState *s) |
| 221 | { |
| 222 | /* |
| 223 | * with KBD_STAT_OBF set, a call to kbd_read_data() will eventually call |
| 224 | * kbd_update_irq() |
| 225 | */ |
| 226 | if (s->status & KBD_STAT_OBF) { |
| 227 | return; |
| 228 | } |
Volker Rümelin | d1e4566 | 2021-05-25 20:14:35 +0200 | [diff] [blame] | 229 | /* the throttle timer is pending and will call kbd_update_irq() */ |
| 230 | if (s->throttle_timer && timer_pending(s->throttle_timer)) { |
| 231 | return; |
| 232 | } |
Volker Rümelin | e4697fa | 2021-05-25 20:14:38 +0200 | [diff] [blame] | 233 | if (kbd_pending(s)) { |
Volker Rümelin | ff6e162 | 2021-05-25 20:14:34 +0200 | [diff] [blame] | 234 | kbd_update_irq(s); |
| 235 | } |
| 236 | } |
| 237 | |
bellard | daa5796 | 2005-11-26 10:14:03 +0000 | [diff] [blame] | 238 | static void kbd_update_kbd_irq(void *opaque, int level) |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 239 | { |
Volker Rümelin | ff6e162 | 2021-05-25 20:14:34 +0200 | [diff] [blame] | 240 | KBDState *s = opaque; |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 241 | |
Volker Rümelin | ff6e162 | 2021-05-25 20:14:34 +0200 | [diff] [blame] | 242 | if (level) { |
bellard | daa5796 | 2005-11-26 10:14:03 +0000 | [diff] [blame] | 243 | s->pending |= KBD_PENDING_KBD; |
Volker Rümelin | ff6e162 | 2021-05-25 20:14:34 +0200 | [diff] [blame] | 244 | } else { |
bellard | daa5796 | 2005-11-26 10:14:03 +0000 | [diff] [blame] | 245 | s->pending &= ~KBD_PENDING_KBD; |
Volker Rümelin | ff6e162 | 2021-05-25 20:14:34 +0200 | [diff] [blame] | 246 | } |
| 247 | kbd_safe_update_irq(s); |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 248 | } |
| 249 | |
bellard | daa5796 | 2005-11-26 10:14:03 +0000 | [diff] [blame] | 250 | static void kbd_update_aux_irq(void *opaque, int level) |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 251 | { |
Volker Rümelin | ff6e162 | 2021-05-25 20:14:34 +0200 | [diff] [blame] | 252 | KBDState *s = opaque; |
bellard | daa5796 | 2005-11-26 10:14:03 +0000 | [diff] [blame] | 253 | |
Volker Rümelin | ff6e162 | 2021-05-25 20:14:34 +0200 | [diff] [blame] | 254 | if (level) { |
bellard | daa5796 | 2005-11-26 10:14:03 +0000 | [diff] [blame] | 255 | s->pending |= KBD_PENDING_AUX; |
Volker Rümelin | ff6e162 | 2021-05-25 20:14:34 +0200 | [diff] [blame] | 256 | } else { |
bellard | daa5796 | 2005-11-26 10:14:03 +0000 | [diff] [blame] | 257 | s->pending &= ~KBD_PENDING_AUX; |
Volker Rümelin | ff6e162 | 2021-05-25 20:14:34 +0200 | [diff] [blame] | 258 | } |
| 259 | kbd_safe_update_irq(s); |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Volker Rümelin | d1e4566 | 2021-05-25 20:14:35 +0200 | [diff] [blame] | 262 | static void kbd_throttle_timeout(void *opaque) |
| 263 | { |
| 264 | KBDState *s = opaque; |
| 265 | |
Volker Rümelin | e4697fa | 2021-05-25 20:14:38 +0200 | [diff] [blame] | 266 | if (kbd_pending(s)) { |
Volker Rümelin | d1e4566 | 2021-05-25 20:14:35 +0200 | [diff] [blame] | 267 | kbd_update_irq(s); |
| 268 | } |
| 269 | } |
| 270 | |
Alexander Graf | d540bfe | 2012-10-08 13:30:08 +0200 | [diff] [blame] | 271 | static uint64_t kbd_read_status(void *opaque, hwaddr addr, |
| 272 | unsigned size) |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 273 | { |
bellard | b41a2cd | 2004-03-14 21:46:48 +0000 | [diff] [blame] | 274 | KBDState *s = opaque; |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 275 | int val; |
| 276 | val = s->status; |
Dr. David Alan Gilbert | 65b182c | 2018-10-16 12:22:32 +0100 | [diff] [blame] | 277 | trace_pckbd_kbd_read_status(val); |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 278 | return val; |
| 279 | } |
| 280 | |
bellard | daa5796 | 2005-11-26 10:14:03 +0000 | [diff] [blame] | 281 | static void kbd_queue(KBDState *s, int b, int aux) |
| 282 | { |
Volker Rümelin | aa67a42 | 2021-05-25 20:14:37 +0200 | [diff] [blame] | 283 | if (s->extended_state) { |
| 284 | s->cbdata = b; |
| 285 | s->pending &= ~KBD_PENDING_CTRL_KBD & ~KBD_PENDING_CTRL_AUX; |
| 286 | s->pending |= aux ? KBD_PENDING_CTRL_AUX : KBD_PENDING_CTRL_KBD; |
| 287 | kbd_safe_update_irq(s); |
| 288 | } else { |
Mark Cave-Ayland | 9d1a425 | 2022-07-12 22:52:49 +0100 | [diff] [blame] | 289 | ps2_queue(aux ? PS2_DEVICE(&s->ps2mouse) : PS2_DEVICE(&s->ps2kbd), b); |
Volker Rümelin | aa67a42 | 2021-05-25 20:14:37 +0200 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
| 293 | static uint8_t kbd_dequeue(KBDState *s) |
| 294 | { |
| 295 | uint8_t b = s->cbdata; |
| 296 | |
| 297 | s->pending &= ~KBD_PENDING_CTRL_KBD & ~KBD_PENDING_CTRL_AUX; |
Volker Rümelin | e4697fa | 2021-05-25 20:14:38 +0200 | [diff] [blame] | 298 | if (kbd_pending(s)) { |
Volker Rümelin | aa67a42 | 2021-05-25 20:14:37 +0200 | [diff] [blame] | 299 | kbd_update_irq(s); |
| 300 | } |
| 301 | return b; |
bellard | daa5796 | 2005-11-26 10:14:03 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Blue Swirl | 4b78a80 | 2011-01-06 18:24:35 +0000 | [diff] [blame] | 304 | static void outport_write(KBDState *s, uint32_t val) |
Blue Swirl | 956a3e6 | 2010-05-22 07:59:01 +0000 | [diff] [blame] | 305 | { |
Dr. David Alan Gilbert | 65b182c | 2018-10-16 12:22:32 +0100 | [diff] [blame] | 306 | trace_pckbd_outport_write(val); |
Blue Swirl | 956a3e6 | 2010-05-22 07:59:01 +0000 | [diff] [blame] | 307 | s->outport = val; |
Efimov Vasily | 3115b9e | 2016-06-22 15:24:51 +0300 | [diff] [blame] | 308 | qemu_set_irq(s->a20_out, (val >> 1) & 1); |
Blue Swirl | 956a3e6 | 2010-05-22 07:59:01 +0000 | [diff] [blame] | 309 | if (!(val & 1)) { |
Eric Blake | cf83f14 | 2017-05-15 16:41:13 -0500 | [diff] [blame] | 310 | qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET); |
Blue Swirl | 956a3e6 | 2010-05-22 07:59:01 +0000 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
Alexander Graf | d540bfe | 2012-10-08 13:30:08 +0200 | [diff] [blame] | 314 | static void kbd_write_command(void *opaque, hwaddr addr, |
| 315 | uint64_t val, unsigned size) |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 316 | { |
bellard | b41a2cd | 2004-03-14 21:46:48 +0000 | [diff] [blame] | 317 | KBDState *s = opaque; |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 318 | |
Dr. David Alan Gilbert | 65b182c | 2018-10-16 12:22:32 +0100 | [diff] [blame] | 319 | trace_pckbd_kbd_write_command(val); |
Bernhard Kohl | 5ccaa4c | 2010-08-19 14:52:12 +0200 | [diff] [blame] | 320 | |
Mark Cave-Ayland | 32be015 | 2022-06-24 14:40:33 +0100 | [diff] [blame] | 321 | /* |
| 322 | * Bits 3-0 of the output port P2 of the keyboard controller may be pulsed |
Bernhard Kohl | 5ccaa4c | 2010-08-19 14:52:12 +0200 | [diff] [blame] | 323 | * low for approximately 6 micro seconds. Bits 3-0 of the KBD_CCMD_PULSE |
| 324 | * command specify the output port bits to be pulsed. |
| 325 | * 0: Bit should be pulsed. 1: Bit should not be modified. |
| 326 | * The only useful version of this command is pulsing bit 0, |
| 327 | * which does a CPU reset. |
| 328 | */ |
Mark Cave-Ayland | 32be015 | 2022-06-24 14:40:33 +0100 | [diff] [blame] | 329 | if ((val & KBD_CCMD_PULSE_BITS_3_0) == KBD_CCMD_PULSE_BITS_3_0) { |
| 330 | if (!(val & 1)) { |
Bernhard Kohl | 5ccaa4c | 2010-08-19 14:52:12 +0200 | [diff] [blame] | 331 | val = KBD_CCMD_RESET; |
Mark Cave-Ayland | 32be015 | 2022-06-24 14:40:33 +0100 | [diff] [blame] | 332 | } else { |
Bernhard Kohl | 5ccaa4c | 2010-08-19 14:52:12 +0200 | [diff] [blame] | 333 | val = KBD_CCMD_NO_OP; |
Mark Cave-Ayland | 32be015 | 2022-06-24 14:40:33 +0100 | [diff] [blame] | 334 | } |
Bernhard Kohl | 5ccaa4c | 2010-08-19 14:52:12 +0200 | [diff] [blame] | 335 | } |
| 336 | |
Mark Cave-Ayland | 32be015 | 2022-06-24 14:40:33 +0100 | [diff] [blame] | 337 | switch (val) { |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 338 | case KBD_CCMD_READ_MODE: |
balrog | 889bec6 | 2008-07-19 14:16:20 +0000 | [diff] [blame] | 339 | kbd_queue(s, s->mode, 0); |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 340 | break; |
| 341 | case KBD_CCMD_WRITE_MODE: |
| 342 | case KBD_CCMD_WRITE_OBUF: |
| 343 | case KBD_CCMD_WRITE_AUX_OBUF: |
| 344 | case KBD_CCMD_WRITE_MOUSE: |
| 345 | case KBD_CCMD_WRITE_OUTPORT: |
| 346 | s->write_cmd = val; |
| 347 | break; |
| 348 | case KBD_CCMD_MOUSE_DISABLE: |
| 349 | s->mode |= KBD_MODE_DISABLE_MOUSE; |
| 350 | break; |
| 351 | case KBD_CCMD_MOUSE_ENABLE: |
| 352 | s->mode &= ~KBD_MODE_DISABLE_MOUSE; |
Volker Rümelin | f6f57a8 | 2021-05-25 20:14:39 +0200 | [diff] [blame] | 353 | kbd_safe_update_irq(s); |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 354 | break; |
| 355 | case KBD_CCMD_TEST_MOUSE: |
| 356 | kbd_queue(s, 0x00, 0); |
| 357 | break; |
| 358 | case KBD_CCMD_SELF_TEST: |
| 359 | s->status |= KBD_STAT_SELFTEST; |
| 360 | kbd_queue(s, 0x55, 0); |
| 361 | break; |
| 362 | case KBD_CCMD_KBD_TEST: |
| 363 | kbd_queue(s, 0x00, 0); |
| 364 | break; |
| 365 | case KBD_CCMD_KBD_DISABLE: |
| 366 | s->mode |= KBD_MODE_DISABLE_KBD; |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 367 | break; |
| 368 | case KBD_CCMD_KBD_ENABLE: |
| 369 | s->mode &= ~KBD_MODE_DISABLE_KBD; |
Volker Rümelin | ff6e162 | 2021-05-25 20:14:34 +0200 | [diff] [blame] | 370 | kbd_safe_update_irq(s); |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 371 | break; |
| 372 | case KBD_CCMD_READ_INPORT: |
Hervé Poussineau | f1b7e0e | 2014-02-11 23:46:03 +0100 | [diff] [blame] | 373 | kbd_queue(s, 0x80, 0); |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 374 | break; |
| 375 | case KBD_CCMD_READ_OUTPORT: |
Blue Swirl | 956a3e6 | 2010-05-22 07:59:01 +0000 | [diff] [blame] | 376 | kbd_queue(s, s->outport, 0); |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 377 | break; |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 378 | case KBD_CCMD_ENABLE_A20: |
Efimov Vasily | 3115b9e | 2016-06-22 15:24:51 +0300 | [diff] [blame] | 379 | qemu_irq_raise(s->a20_out); |
Blue Swirl | 956a3e6 | 2010-05-22 07:59:01 +0000 | [diff] [blame] | 380 | s->outport |= KBD_OUT_A20; |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 381 | break; |
| 382 | case KBD_CCMD_DISABLE_A20: |
Efimov Vasily | 3115b9e | 2016-06-22 15:24:51 +0300 | [diff] [blame] | 383 | qemu_irq_lower(s->a20_out); |
Blue Swirl | 956a3e6 | 2010-05-22 07:59:01 +0000 | [diff] [blame] | 384 | s->outport &= ~KBD_OUT_A20; |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 385 | break; |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 386 | case KBD_CCMD_RESET: |
Eric Blake | cf83f14 | 2017-05-15 16:41:13 -0500 | [diff] [blame] | 387 | qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET); |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 388 | break; |
Bernhard Kohl | 5ccaa4c | 2010-08-19 14:52:12 +0200 | [diff] [blame] | 389 | case KBD_CCMD_NO_OP: |
| 390 | /* ignore that */ |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 391 | break; |
| 392 | default: |
Philippe Mathieu-Daudé | c2e846b | 2018-06-26 17:50:40 +0100 | [diff] [blame] | 393 | qemu_log_mask(LOG_GUEST_ERROR, |
| 394 | "unsupported keyboard cmd=0x%02" PRIx64 "\n", val); |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 395 | break; |
| 396 | } |
| 397 | } |
| 398 | |
Alexander Graf | d540bfe | 2012-10-08 13:30:08 +0200 | [diff] [blame] | 399 | static uint64_t kbd_read_data(void *opaque, hwaddr addr, |
| 400 | unsigned size) |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 401 | { |
bellard | b41a2cd | 2004-03-14 21:46:48 +0000 | [diff] [blame] | 402 | KBDState *s = opaque; |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 403 | |
Volker Rümelin | ac9192b | 2021-05-25 20:14:36 +0200 | [diff] [blame] | 404 | if (s->status & KBD_STAT_OBF) { |
Volker Rümelin | ff6e162 | 2021-05-25 20:14:34 +0200 | [diff] [blame] | 405 | kbd_deassert_irq(s); |
Volker Rümelin | ac9192b | 2021-05-25 20:14:36 +0200 | [diff] [blame] | 406 | if (s->obsrc & KBD_OBSRC_KBD) { |
Volker Rümelin | d1e4566 | 2021-05-25 20:14:35 +0200 | [diff] [blame] | 407 | if (s->throttle_timer) { |
| 408 | timer_mod(s->throttle_timer, |
| 409 | qemu_clock_get_us(QEMU_CLOCK_VIRTUAL) + 1000); |
| 410 | } |
Mark Cave-Ayland | 652fbff | 2022-07-12 22:52:47 +0100 | [diff] [blame] | 411 | s->obdata = ps2_read_data(PS2_DEVICE(&s->ps2kbd)); |
Volker Rümelin | ac9192b | 2021-05-25 20:14:36 +0200 | [diff] [blame] | 412 | } else if (s->obsrc & KBD_OBSRC_MOUSE) { |
Mark Cave-Ayland | 9d1a425 | 2022-07-12 22:52:49 +0100 | [diff] [blame] | 413 | s->obdata = ps2_read_data(PS2_DEVICE(&s->ps2mouse)); |
Volker Rümelin | aa67a42 | 2021-05-25 20:14:37 +0200 | [diff] [blame] | 414 | } else if (s->obsrc & KBD_OBSRC_CTRL) { |
| 415 | s->obdata = kbd_dequeue(s); |
Volker Rümelin | ff6e162 | 2021-05-25 20:14:34 +0200 | [diff] [blame] | 416 | } |
| 417 | } |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 418 | |
Volker Rümelin | ff6e162 | 2021-05-25 20:14:34 +0200 | [diff] [blame] | 419 | trace_pckbd_kbd_read_data(s->obdata); |
| 420 | return s->obdata; |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 421 | } |
| 422 | |
Alexander Graf | d540bfe | 2012-10-08 13:30:08 +0200 | [diff] [blame] | 423 | static void kbd_write_data(void *opaque, hwaddr addr, |
| 424 | uint64_t val, unsigned size) |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 425 | { |
bellard | b41a2cd | 2004-03-14 21:46:48 +0000 | [diff] [blame] | 426 | KBDState *s = opaque; |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 427 | |
Dr. David Alan Gilbert | 65b182c | 2018-10-16 12:22:32 +0100 | [diff] [blame] | 428 | trace_pckbd_kbd_write_data(val); |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 429 | |
Mark Cave-Ayland | 32be015 | 2022-06-24 14:40:33 +0100 | [diff] [blame] | 430 | switch (s->write_cmd) { |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 431 | case 0: |
Mark Cave-Ayland | 652fbff | 2022-07-12 22:52:47 +0100 | [diff] [blame] | 432 | ps2_write_keyboard(&s->ps2kbd, val); |
Volker Rümelin | f6f57a8 | 2021-05-25 20:14:39 +0200 | [diff] [blame] | 433 | /* sending data to the keyboard reenables PS/2 communication */ |
| 434 | s->mode &= ~KBD_MODE_DISABLE_KBD; |
| 435 | kbd_safe_update_irq(s); |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 436 | break; |
| 437 | case KBD_CCMD_WRITE_MODE: |
| 438 | s->mode = val; |
Mark Cave-Ayland | 652fbff | 2022-07-12 22:52:47 +0100 | [diff] [blame] | 439 | ps2_keyboard_set_translation(&s->ps2kbd, |
| 440 | (s->mode & KBD_MODE_KCC) != 0); |
Volker Rümelin | ff6e162 | 2021-05-25 20:14:34 +0200 | [diff] [blame] | 441 | /* |
| 442 | * a write to the mode byte interrupt enable flags directly updates |
| 443 | * the irq lines |
| 444 | */ |
| 445 | kbd_update_irq_lines(s); |
| 446 | /* |
| 447 | * a write to the mode byte disable interface flags may raise |
| 448 | * an irq if there is pending data in the PS/2 queues. |
| 449 | */ |
| 450 | kbd_safe_update_irq(s); |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 451 | break; |
| 452 | case KBD_CCMD_WRITE_OBUF: |
| 453 | kbd_queue(s, val, 0); |
| 454 | break; |
| 455 | case KBD_CCMD_WRITE_AUX_OBUF: |
| 456 | kbd_queue(s, val, 1); |
| 457 | break; |
| 458 | case KBD_CCMD_WRITE_OUTPORT: |
Blue Swirl | 4b78a80 | 2011-01-06 18:24:35 +0000 | [diff] [blame] | 459 | outport_write(s, val); |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 460 | break; |
| 461 | case KBD_CCMD_WRITE_MOUSE: |
Mark Cave-Ayland | 9d1a425 | 2022-07-12 22:52:49 +0100 | [diff] [blame] | 462 | ps2_write_mouse(&s->ps2mouse, val); |
Volker Rümelin | f6f57a8 | 2021-05-25 20:14:39 +0200 | [diff] [blame] | 463 | /* sending data to the mouse reenables PS/2 communication */ |
| 464 | s->mode &= ~KBD_MODE_DISABLE_MOUSE; |
| 465 | kbd_safe_update_irq(s); |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 466 | break; |
| 467 | default: |
| 468 | break; |
| 469 | } |
| 470 | s->write_cmd = 0; |
| 471 | } |
| 472 | |
bellard | d7d02e3 | 2004-06-20 12:58:36 +0000 | [diff] [blame] | 473 | static void kbd_reset(void *opaque) |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 474 | { |
bellard | d7d02e3 | 2004-06-20 12:58:36 +0000 | [diff] [blame] | 475 | KBDState *s = opaque; |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 476 | |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 477 | s->mode = KBD_MODE_KBD_INT | KBD_MODE_MOUSE_INT; |
| 478 | s->status = KBD_STAT_CMD | KBD_STAT_UNLOCKED; |
Paolo Bonzini | d13c040 | 2014-12-22 08:55:19 +0100 | [diff] [blame] | 479 | s->outport = KBD_OUT_RESET | KBD_OUT_A20 | KBD_OUT_ONES; |
Volker Rümelin | ff6e162 | 2021-05-25 20:14:34 +0200 | [diff] [blame] | 480 | s->pending = 0; |
| 481 | kbd_deassert_irq(s); |
Volker Rümelin | d1e4566 | 2021-05-25 20:14:35 +0200 | [diff] [blame] | 482 | if (s->throttle_timer) { |
| 483 | timer_del(s->throttle_timer); |
| 484 | } |
Pavel Dovgalyuk | a28fe7e | 2014-08-28 15:19:14 +0400 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | static uint8_t kbd_outport_default(KBDState *s) |
| 488 | { |
Paolo Bonzini | d13c040 | 2014-12-22 08:55:19 +0100 | [diff] [blame] | 489 | return KBD_OUT_RESET | KBD_OUT_A20 | KBD_OUT_ONES |
Pavel Dovgalyuk | a28fe7e | 2014-08-28 15:19:14 +0400 | [diff] [blame] | 490 | | (s->status & KBD_STAT_OBF ? KBD_OUT_OBF : 0) |
| 491 | | (s->status & KBD_STAT_MOUSE_OBF ? KBD_OUT_MOUSE_OBF : 0); |
| 492 | } |
| 493 | |
| 494 | static int kbd_outport_post_load(void *opaque, int version_id) |
| 495 | { |
| 496 | KBDState *s = opaque; |
| 497 | s->outport_present = true; |
| 498 | return 0; |
| 499 | } |
| 500 | |
Pavel Dovgalyuk | a28fe7e | 2014-08-28 15:19:14 +0400 | [diff] [blame] | 501 | static bool kbd_outport_needed(void *opaque) |
| 502 | { |
| 503 | KBDState *s = opaque; |
| 504 | return s->outport != kbd_outport_default(s); |
| 505 | } |
| 506 | |
Juan Quintela | 5cd8cad | 2014-09-23 14:09:54 +0200 | [diff] [blame] | 507 | static const VMStateDescription vmstate_kbd_outport = { |
| 508 | .name = "pckbd_outport", |
| 509 | .version_id = 1, |
| 510 | .minimum_version_id = 1, |
| 511 | .post_load = kbd_outport_post_load, |
| 512 | .needed = kbd_outport_needed, |
Richard Henderson | af0f07d | 2023-12-21 14:16:14 +1100 | [diff] [blame] | 513 | .fields = (const VMStateField[]) { |
Juan Quintela | 5cd8cad | 2014-09-23 14:09:54 +0200 | [diff] [blame] | 514 | VMSTATE_UINT8(outport, KBDState), |
| 515 | VMSTATE_END_OF_LIST() |
| 516 | } |
| 517 | }; |
| 518 | |
Volker Rümelin | d1e4566 | 2021-05-25 20:14:35 +0200 | [diff] [blame] | 519 | static int kbd_extended_state_pre_save(void *opaque) |
| 520 | { |
| 521 | KBDState *s = opaque; |
| 522 | |
| 523 | s->migration_flags = 0; |
| 524 | if (s->throttle_timer && timer_pending(s->throttle_timer)) { |
| 525 | s->migration_flags |= KBD_MIGR_TIMER_PENDING; |
| 526 | } |
| 527 | |
| 528 | return 0; |
| 529 | } |
| 530 | |
| 531 | static int kbd_extended_state_post_load(void *opaque, int version_id) |
| 532 | { |
| 533 | KBDState *s = opaque; |
| 534 | |
| 535 | if (s->migration_flags & KBD_MIGR_TIMER_PENDING) { |
| 536 | kbd_throttle_timeout(s); |
| 537 | } |
Volker Rümelin | ac9192b | 2021-05-25 20:14:36 +0200 | [diff] [blame] | 538 | s->extended_state_loaded = true; |
Volker Rümelin | d1e4566 | 2021-05-25 20:14:35 +0200 | [diff] [blame] | 539 | |
| 540 | return 0; |
| 541 | } |
| 542 | |
Volker Rümelin | ff6e162 | 2021-05-25 20:14:34 +0200 | [diff] [blame] | 543 | static bool kbd_extended_state_needed(void *opaque) |
| 544 | { |
| 545 | KBDState *s = opaque; |
| 546 | |
| 547 | return s->extended_state; |
| 548 | } |
| 549 | |
| 550 | static const VMStateDescription vmstate_kbd_extended_state = { |
| 551 | .name = "pckbd/extended_state", |
Volker Rümelin | d1e4566 | 2021-05-25 20:14:35 +0200 | [diff] [blame] | 552 | .post_load = kbd_extended_state_post_load, |
| 553 | .pre_save = kbd_extended_state_pre_save, |
Volker Rümelin | ff6e162 | 2021-05-25 20:14:34 +0200 | [diff] [blame] | 554 | .needed = kbd_extended_state_needed, |
Richard Henderson | af0f07d | 2023-12-21 14:16:14 +1100 | [diff] [blame] | 555 | .fields = (const VMStateField[]) { |
Volker Rümelin | d1e4566 | 2021-05-25 20:14:35 +0200 | [diff] [blame] | 556 | VMSTATE_UINT32(migration_flags, KBDState), |
Volker Rümelin | ac9192b | 2021-05-25 20:14:36 +0200 | [diff] [blame] | 557 | VMSTATE_UINT32(obsrc, KBDState), |
Volker Rümelin | ff6e162 | 2021-05-25 20:14:34 +0200 | [diff] [blame] | 558 | VMSTATE_UINT8(obdata, KBDState), |
Volker Rümelin | aa67a42 | 2021-05-25 20:14:37 +0200 | [diff] [blame] | 559 | VMSTATE_UINT8(cbdata, KBDState), |
Volker Rümelin | ff6e162 | 2021-05-25 20:14:34 +0200 | [diff] [blame] | 560 | VMSTATE_END_OF_LIST() |
| 561 | } |
| 562 | }; |
| 563 | |
Volker Rümelin | f6f57a8 | 2021-05-25 20:14:39 +0200 | [diff] [blame] | 564 | static int kbd_pre_save(void *opaque) |
| 565 | { |
| 566 | KBDState *s = opaque; |
| 567 | |
| 568 | if (s->extended_state) { |
| 569 | s->pending_tmp = s->pending; |
| 570 | } else { |
| 571 | s->pending_tmp = 0; |
| 572 | if (s->pending & KBD_PENDING_KBD) { |
| 573 | s->pending_tmp |= KBD_PENDING_KBD_COMPAT; |
| 574 | } |
| 575 | if (s->pending & KBD_PENDING_AUX) { |
| 576 | s->pending_tmp |= KBD_PENDING_AUX_COMPAT; |
| 577 | } |
| 578 | } |
| 579 | return 0; |
| 580 | } |
| 581 | |
Volker Rümelin | ac9192b | 2021-05-25 20:14:36 +0200 | [diff] [blame] | 582 | static int kbd_pre_load(void *opaque) |
| 583 | { |
| 584 | KBDState *s = opaque; |
| 585 | |
Volker Rümelin | 9d74e6c | 2021-05-25 20:14:41 +0200 | [diff] [blame] | 586 | s->outport_present = false; |
Volker Rümelin | ac9192b | 2021-05-25 20:14:36 +0200 | [diff] [blame] | 587 | s->extended_state_loaded = false; |
| 588 | return 0; |
| 589 | } |
| 590 | |
Pavel Dovgalyuk | a28fe7e | 2014-08-28 15:19:14 +0400 | [diff] [blame] | 591 | static int kbd_post_load(void *opaque, int version_id) |
| 592 | { |
| 593 | KBDState *s = opaque; |
| 594 | if (!s->outport_present) { |
| 595 | s->outport = kbd_outport_default(s); |
| 596 | } |
Volker Rümelin | f6f57a8 | 2021-05-25 20:14:39 +0200 | [diff] [blame] | 597 | s->pending = s->pending_tmp; |
Volker Rümelin | ac9192b | 2021-05-25 20:14:36 +0200 | [diff] [blame] | 598 | if (!s->extended_state_loaded) { |
| 599 | s->obsrc = s->status & KBD_STAT_OBF ? |
| 600 | (s->status & KBD_STAT_MOUSE_OBF ? KBD_OBSRC_MOUSE : KBD_OBSRC_KBD) : |
| 601 | 0; |
Volker Rümelin | f6f57a8 | 2021-05-25 20:14:39 +0200 | [diff] [blame] | 602 | if (s->pending & KBD_PENDING_KBD_COMPAT) { |
| 603 | s->pending |= KBD_PENDING_KBD; |
| 604 | } |
| 605 | if (s->pending & KBD_PENDING_AUX_COMPAT) { |
| 606 | s->pending |= KBD_PENDING_AUX; |
| 607 | } |
Volker Rümelin | ac9192b | 2021-05-25 20:14:36 +0200 | [diff] [blame] | 608 | } |
Volker Rümelin | f6f57a8 | 2021-05-25 20:14:39 +0200 | [diff] [blame] | 609 | /* clear all unused flags */ |
| 610 | s->pending &= KBD_PENDING_CTRL_KBD | KBD_PENDING_CTRL_AUX | |
| 611 | KBD_PENDING_KBD | KBD_PENDING_AUX; |
Pavel Dovgalyuk | a28fe7e | 2014-08-28 15:19:14 +0400 | [diff] [blame] | 612 | return 0; |
bellard | 80cabfa | 2004-03-14 12:20:30 +0000 | [diff] [blame] | 613 | } |
| 614 | |
Juan Quintela | 3c619b5 | 2009-09-10 03:04:41 +0200 | [diff] [blame] | 615 | static const VMStateDescription vmstate_kbd = { |
| 616 | .name = "pckbd", |
| 617 | .version_id = 3, |
| 618 | .minimum_version_id = 3, |
Volker Rümelin | ac9192b | 2021-05-25 20:14:36 +0200 | [diff] [blame] | 619 | .pre_load = kbd_pre_load, |
Pavel Dovgalyuk | a28fe7e | 2014-08-28 15:19:14 +0400 | [diff] [blame] | 620 | .post_load = kbd_post_load, |
Volker Rümelin | f6f57a8 | 2021-05-25 20:14:39 +0200 | [diff] [blame] | 621 | .pre_save = kbd_pre_save, |
Richard Henderson | af0f07d | 2023-12-21 14:16:14 +1100 | [diff] [blame] | 622 | .fields = (const VMStateField[]) { |
Juan Quintela | 3c619b5 | 2009-09-10 03:04:41 +0200 | [diff] [blame] | 623 | VMSTATE_UINT8(write_cmd, KBDState), |
| 624 | VMSTATE_UINT8(status, KBDState), |
| 625 | VMSTATE_UINT8(mode, KBDState), |
Volker Rümelin | f6f57a8 | 2021-05-25 20:14:39 +0200 | [diff] [blame] | 626 | VMSTATE_UINT8(pending_tmp, KBDState), |
Juan Quintela | 3c619b5 | 2009-09-10 03:04:41 +0200 | [diff] [blame] | 627 | VMSTATE_END_OF_LIST() |
Pavel Dovgalyuk | a28fe7e | 2014-08-28 15:19:14 +0400 | [diff] [blame] | 628 | }, |
Richard Henderson | af0f07d | 2023-12-21 14:16:14 +1100 | [diff] [blame] | 629 | .subsections = (const VMStateDescription * const []) { |
Juan Quintela | 5cd8cad | 2014-09-23 14:09:54 +0200 | [diff] [blame] | 630 | &vmstate_kbd_outport, |
Volker Rümelin | ff6e162 | 2021-05-25 20:14:34 +0200 | [diff] [blame] | 631 | &vmstate_kbd_extended_state, |
Juan Quintela | 5cd8cad | 2014-09-23 14:09:54 +0200 | [diff] [blame] | 632 | NULL |
Juan Quintela | 3c619b5 | 2009-09-10 03:04:41 +0200 | [diff] [blame] | 633 | } |
| 634 | }; |
bellard | 675376f | 2004-07-10 13:39:53 +0000 | [diff] [blame] | 635 | |
ths | b92bb99 | 2007-04-16 17:20:48 +0000 | [diff] [blame] | 636 | /* Memory mapped interface */ |
Peter Maydell | 5876503 | 2018-06-15 14:57:13 +0100 | [diff] [blame] | 637 | static uint64_t kbd_mm_readfn(void *opaque, hwaddr addr, unsigned size) |
ths | b92bb99 | 2007-04-16 17:20:48 +0000 | [diff] [blame] | 638 | { |
| 639 | KBDState *s = opaque; |
| 640 | |
Mark Cave-Ayland | 32be015 | 2022-06-24 14:40:33 +0100 | [diff] [blame] | 641 | if (addr & s->mask) { |
Alexander Graf | d540bfe | 2012-10-08 13:30:08 +0200 | [diff] [blame] | 642 | return kbd_read_status(s, 0, 1) & 0xff; |
Mark Cave-Ayland | 32be015 | 2022-06-24 14:40:33 +0100 | [diff] [blame] | 643 | } else { |
Alexander Graf | d540bfe | 2012-10-08 13:30:08 +0200 | [diff] [blame] | 644 | return kbd_read_data(s, 0, 1) & 0xff; |
Mark Cave-Ayland | 32be015 | 2022-06-24 14:40:33 +0100 | [diff] [blame] | 645 | } |
ths | b92bb99 | 2007-04-16 17:20:48 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Peter Maydell | 5876503 | 2018-06-15 14:57:13 +0100 | [diff] [blame] | 648 | static void kbd_mm_writefn(void *opaque, hwaddr addr, |
| 649 | uint64_t value, unsigned size) |
ths | b92bb99 | 2007-04-16 17:20:48 +0000 | [diff] [blame] | 650 | { |
| 651 | KBDState *s = opaque; |
| 652 | |
Mark Cave-Ayland | 32be015 | 2022-06-24 14:40:33 +0100 | [diff] [blame] | 653 | if (addr & s->mask) { |
Alexander Graf | d540bfe | 2012-10-08 13:30:08 +0200 | [diff] [blame] | 654 | kbd_write_command(s, 0, value & 0xff, 1); |
Mark Cave-Ayland | 32be015 | 2022-06-24 14:40:33 +0100 | [diff] [blame] | 655 | } else { |
Alexander Graf | d540bfe | 2012-10-08 13:30:08 +0200 | [diff] [blame] | 656 | kbd_write_data(s, 0, value & 0xff, 1); |
Mark Cave-Ayland | 32be015 | 2022-06-24 14:40:33 +0100 | [diff] [blame] | 657 | } |
ths | b92bb99 | 2007-04-16 17:20:48 +0000 | [diff] [blame] | 658 | } |
| 659 | |
Peter Maydell | 5876503 | 2018-06-15 14:57:13 +0100 | [diff] [blame] | 660 | |
Richard Henderson | dbff76a | 2011-08-10 15:28:17 -0700 | [diff] [blame] | 661 | static const MemoryRegionOps i8042_mmio_ops = { |
Peter Maydell | 5876503 | 2018-06-15 14:57:13 +0100 | [diff] [blame] | 662 | .read = kbd_mm_readfn, |
| 663 | .write = kbd_mm_writefn, |
| 664 | .valid.min_access_size = 1, |
| 665 | .valid.max_access_size = 4, |
Richard Henderson | dbff76a | 2011-08-10 15:28:17 -0700 | [diff] [blame] | 666 | .endianness = DEVICE_NATIVE_ENDIAN, |
ths | b92bb99 | 2007-04-16 17:20:48 +0000 | [diff] [blame] | 667 | }; |
| 668 | |
Mark Cave-Ayland | cb663a8 | 2022-06-24 14:41:04 +0100 | [diff] [blame] | 669 | static void i8042_mmio_set_kbd_irq(void *opaque, int n, int level) |
| 670 | { |
| 671 | MMIOKBDState *s = I8042_MMIO(opaque); |
| 672 | KBDState *ks = &s->kbd; |
| 673 | |
| 674 | kbd_update_kbd_irq(ks, level); |
| 675 | } |
| 676 | |
| 677 | static void i8042_mmio_set_mouse_irq(void *opaque, int n, int level) |
| 678 | { |
| 679 | MMIOKBDState *s = I8042_MMIO(opaque); |
| 680 | KBDState *ks = &s->kbd; |
| 681 | |
| 682 | kbd_update_aux_irq(ks, level); |
| 683 | } |
| 684 | |
Mark Cave-Ayland | 57f6c3a | 2022-06-24 14:40:37 +0100 | [diff] [blame] | 685 | static void i8042_mmio_reset(DeviceState *dev) |
| 686 | { |
| 687 | MMIOKBDState *s = I8042_MMIO(dev); |
| 688 | KBDState *ks = &s->kbd; |
| 689 | |
| 690 | kbd_reset(ks); |
| 691 | } |
| 692 | |
Mark Cave-Ayland | f4de68d | 2022-06-24 14:40:40 +0100 | [diff] [blame] | 693 | static void i8042_mmio_realize(DeviceState *dev, Error **errp) |
| 694 | { |
| 695 | MMIOKBDState *s = I8042_MMIO(dev); |
| 696 | KBDState *ks = &s->kbd; |
| 697 | |
| 698 | memory_region_init_io(&s->region, OBJECT(dev), &i8042_mmio_ops, ks, |
| 699 | "i8042", s->size); |
| 700 | |
| 701 | sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->region); |
Mark Cave-Ayland | 75877e9 | 2022-06-24 14:40:44 +0100 | [diff] [blame] | 702 | |
Mark Cave-Ayland | 652fbff | 2022-07-12 22:52:47 +0100 | [diff] [blame] | 703 | if (!sysbus_realize(SYS_BUS_DEVICE(&ks->ps2kbd), errp)) { |
| 704 | return; |
| 705 | } |
| 706 | |
Mark Cave-Ayland | 9d1a425 | 2022-07-12 22:52:49 +0100 | [diff] [blame] | 707 | if (!sysbus_realize(SYS_BUS_DEVICE(&ks->ps2mouse), errp)) { |
| 708 | return; |
| 709 | } |
| 710 | |
Mark Cave-Ayland | 652fbff | 2022-07-12 22:52:47 +0100 | [diff] [blame] | 711 | qdev_connect_gpio_out(DEVICE(&ks->ps2kbd), PS2_DEVICE_IRQ, |
Mark Cave-Ayland | cb663a8 | 2022-06-24 14:41:04 +0100 | [diff] [blame] | 712 | qdev_get_gpio_in_named(dev, "ps2-kbd-input-irq", |
| 713 | 0)); |
Mark Cave-Ayland | 652fbff | 2022-07-12 22:52:47 +0100 | [diff] [blame] | 714 | |
Mark Cave-Ayland | 9d1a425 | 2022-07-12 22:52:49 +0100 | [diff] [blame] | 715 | qdev_connect_gpio_out(DEVICE(&ks->ps2mouse), PS2_DEVICE_IRQ, |
Mark Cave-Ayland | cb663a8 | 2022-06-24 14:41:04 +0100 | [diff] [blame] | 716 | qdev_get_gpio_in_named(dev, "ps2-mouse-input-irq", |
| 717 | 0)); |
Mark Cave-Ayland | f4de68d | 2022-06-24 14:40:40 +0100 | [diff] [blame] | 718 | } |
| 719 | |
Mark Cave-Ayland | 47fc741 | 2022-06-24 14:40:41 +0100 | [diff] [blame] | 720 | static void i8042_mmio_init(Object *obj) |
| 721 | { |
| 722 | MMIOKBDState *s = I8042_MMIO(obj); |
| 723 | KBDState *ks = &s->kbd; |
| 724 | |
| 725 | ks->extended_state = true; |
Mark Cave-Ayland | cb663a8 | 2022-06-24 14:41:04 +0100 | [diff] [blame] | 726 | |
Mark Cave-Ayland | 652fbff | 2022-07-12 22:52:47 +0100 | [diff] [blame] | 727 | object_initialize_child(obj, "ps2kbd", &ks->ps2kbd, TYPE_PS2_KBD_DEVICE); |
Mark Cave-Ayland | 9d1a425 | 2022-07-12 22:52:49 +0100 | [diff] [blame] | 728 | object_initialize_child(obj, "ps2mouse", &ks->ps2mouse, |
| 729 | TYPE_PS2_MOUSE_DEVICE); |
Mark Cave-Ayland | 652fbff | 2022-07-12 22:52:47 +0100 | [diff] [blame] | 730 | |
Mark Cave-Ayland | cb663a8 | 2022-06-24 14:41:04 +0100 | [diff] [blame] | 731 | qdev_init_gpio_out(DEVICE(obj), ks->irqs, 2); |
| 732 | qdev_init_gpio_in_named(DEVICE(obj), i8042_mmio_set_kbd_irq, |
| 733 | "ps2-kbd-input-irq", 1); |
| 734 | qdev_init_gpio_in_named(DEVICE(obj), i8042_mmio_set_mouse_irq, |
| 735 | "ps2-mouse-input-irq", 1); |
Mark Cave-Ayland | 47fc741 | 2022-06-24 14:40:41 +0100 | [diff] [blame] | 736 | } |
| 737 | |
Mark Cave-Ayland | d4f5b4d | 2022-06-24 14:40:38 +0100 | [diff] [blame] | 738 | static Property i8042_mmio_properties[] = { |
| 739 | DEFINE_PROP_UINT64("mask", MMIOKBDState, kbd.mask, UINT64_MAX), |
Mark Cave-Ayland | 7b9fff2 | 2022-06-24 14:40:39 +0100 | [diff] [blame] | 740 | DEFINE_PROP_UINT32("size", MMIOKBDState, size, -1), |
Mark Cave-Ayland | d4f5b4d | 2022-06-24 14:40:38 +0100 | [diff] [blame] | 741 | DEFINE_PROP_END_OF_LIST(), |
| 742 | }; |
| 743 | |
Mark Cave-Ayland | abcacb2 | 2022-07-12 22:52:46 +0100 | [diff] [blame] | 744 | static const VMStateDescription vmstate_kbd_mmio = { |
| 745 | .name = "pckbd-mmio", |
| 746 | .version_id = 1, |
| 747 | .minimum_version_id = 1, |
Richard Henderson | af0f07d | 2023-12-21 14:16:14 +1100 | [diff] [blame] | 748 | .fields = (const VMStateField[]) { |
Mark Cave-Ayland | abcacb2 | 2022-07-12 22:52:46 +0100 | [diff] [blame] | 749 | VMSTATE_STRUCT(kbd, MMIOKBDState, 0, vmstate_kbd, KBDState), |
| 750 | VMSTATE_END_OF_LIST() |
| 751 | } |
| 752 | }; |
| 753 | |
Mark Cave-Ayland | 150ee01 | 2022-06-24 14:40:36 +0100 | [diff] [blame] | 754 | static void i8042_mmio_class_init(ObjectClass *klass, void *data) |
| 755 | { |
| 756 | DeviceClass *dc = DEVICE_CLASS(klass); |
| 757 | |
Mark Cave-Ayland | f4de68d | 2022-06-24 14:40:40 +0100 | [diff] [blame] | 758 | dc->realize = i8042_mmio_realize; |
Peter Maydell | e3d0814 | 2024-09-13 15:31:44 +0100 | [diff] [blame] | 759 | device_class_set_legacy_reset(dc, i8042_mmio_reset); |
Mark Cave-Ayland | abcacb2 | 2022-07-12 22:52:46 +0100 | [diff] [blame] | 760 | dc->vmsd = &vmstate_kbd_mmio; |
Mark Cave-Ayland | d4f5b4d | 2022-06-24 14:40:38 +0100 | [diff] [blame] | 761 | device_class_set_props(dc, i8042_mmio_properties); |
Mark Cave-Ayland | 150ee01 | 2022-06-24 14:40:36 +0100 | [diff] [blame] | 762 | set_bit(DEVICE_CATEGORY_INPUT, dc->categories); |
| 763 | } |
| 764 | |
Mark Cave-Ayland | 150ee01 | 2022-06-24 14:40:36 +0100 | [diff] [blame] | 765 | static const TypeInfo i8042_mmio_info = { |
| 766 | .name = TYPE_I8042_MMIO, |
| 767 | .parent = TYPE_SYS_BUS_DEVICE, |
Mark Cave-Ayland | 47fc741 | 2022-06-24 14:40:41 +0100 | [diff] [blame] | 768 | .instance_init = i8042_mmio_init, |
Mark Cave-Ayland | 150ee01 | 2022-06-24 14:40:36 +0100 | [diff] [blame] | 769 | .instance_size = sizeof(MMIOKBDState), |
| 770 | .class_init = i8042_mmio_class_init |
| 771 | }; |
| 772 | |
Marc-André Lureau | 0fe4bb3 | 2019-10-17 17:06:05 +0200 | [diff] [blame] | 773 | void i8042_isa_mouse_fake_event(ISAKBDState *isa) |
Blue Swirl | 956a3e6 | 2010-05-22 07:59:01 +0000 | [diff] [blame] | 774 | { |
Andreas Färber | a2e0b86 | 2013-04-27 22:18:47 +0200 | [diff] [blame] | 775 | KBDState *s = &isa->kbd; |
Blue Swirl | 956a3e6 | 2010-05-22 07:59:01 +0000 | [diff] [blame] | 776 | |
Mark Cave-Ayland | 9d1a425 | 2022-07-12 22:52:49 +0100 | [diff] [blame] | 777 | ps2_mouse_fake_event(&s->ps2mouse); |
Blue Swirl | 956a3e6 | 2010-05-22 07:59:01 +0000 | [diff] [blame] | 778 | } |
| 779 | |
Blue Swirl | d05ac8f | 2009-12-04 20:44:44 +0000 | [diff] [blame] | 780 | static const VMStateDescription vmstate_kbd_isa = { |
Juan Quintela | be73cfe | 2009-12-02 12:36:46 +0100 | [diff] [blame] | 781 | .name = "pckbd", |
| 782 | .version_id = 3, |
| 783 | .minimum_version_id = 3, |
Richard Henderson | af0f07d | 2023-12-21 14:16:14 +1100 | [diff] [blame] | 784 | .fields = (const VMStateField[]) { |
Juan Quintela | be73cfe | 2009-12-02 12:36:46 +0100 | [diff] [blame] | 785 | VMSTATE_STRUCT(kbd, ISAKBDState, 0, vmstate_kbd, KBDState), |
| 786 | VMSTATE_END_OF_LIST() |
| 787 | } |
| 788 | }; |
| 789 | |
Richard Henderson | dbff76a | 2011-08-10 15:28:17 -0700 | [diff] [blame] | 790 | static const MemoryRegionOps i8042_data_ops = { |
Alexander Graf | d540bfe | 2012-10-08 13:30:08 +0200 | [diff] [blame] | 791 | .read = kbd_read_data, |
| 792 | .write = kbd_write_data, |
| 793 | .impl = { |
| 794 | .min_access_size = 1, |
| 795 | .max_access_size = 1, |
| 796 | }, |
| 797 | .endianness = DEVICE_LITTLE_ENDIAN, |
Richard Henderson | dbff76a | 2011-08-10 15:28:17 -0700 | [diff] [blame] | 798 | }; |
| 799 | |
| 800 | static const MemoryRegionOps i8042_cmd_ops = { |
Alexander Graf | d540bfe | 2012-10-08 13:30:08 +0200 | [diff] [blame] | 801 | .read = kbd_read_status, |
| 802 | .write = kbd_write_command, |
| 803 | .impl = { |
| 804 | .min_access_size = 1, |
| 805 | .max_access_size = 1, |
| 806 | }, |
| 807 | .endianness = DEVICE_LITTLE_ENDIAN, |
Richard Henderson | dbff76a | 2011-08-10 15:28:17 -0700 | [diff] [blame] | 808 | }; |
| 809 | |
Mark Cave-Ayland | 6eb252d | 2022-06-24 14:41:07 +0100 | [diff] [blame] | 810 | static void i8042_set_kbd_irq(void *opaque, int n, int level) |
| 811 | { |
| 812 | ISAKBDState *s = I8042(opaque); |
| 813 | KBDState *ks = &s->kbd; |
| 814 | |
| 815 | kbd_update_kbd_irq(ks, level); |
| 816 | } |
| 817 | |
| 818 | static void i8042_set_mouse_irq(void *opaque, int n, int level) |
| 819 | { |
| 820 | ISAKBDState *s = I8042(opaque); |
| 821 | KBDState *ks = &s->kbd; |
| 822 | |
| 823 | kbd_update_aux_irq(ks, level); |
| 824 | } |
| 825 | |
| 826 | |
Mark Cave-Ayland | 55870d6 | 2022-06-24 14:41:06 +0100 | [diff] [blame] | 827 | static void i8042_reset(DeviceState *dev) |
| 828 | { |
| 829 | ISAKBDState *s = I8042(dev); |
| 830 | KBDState *ks = &s->kbd; |
| 831 | |
| 832 | kbd_reset(ks); |
| 833 | } |
| 834 | |
Andreas Färber | db895a1 | 2012-11-25 02:37:14 +0100 | [diff] [blame] | 835 | static void i8042_initfn(Object *obj) |
Gerd Hoffmann | da85ccf | 2009-07-31 12:30:15 +0200 | [diff] [blame] | 836 | { |
Andreas Färber | db895a1 | 2012-11-25 02:37:14 +0100 | [diff] [blame] | 837 | ISAKBDState *isa_s = I8042(obj); |
| 838 | KBDState *s = &isa_s->kbd; |
| 839 | |
Paolo Bonzini | 1437c94 | 2013-06-06 21:25:08 -0400 | [diff] [blame] | 840 | memory_region_init_io(isa_s->io + 0, obj, &i8042_data_ops, s, |
| 841 | "i8042-data", 1); |
| 842 | memory_region_init_io(isa_s->io + 1, obj, &i8042_cmd_ops, s, |
| 843 | "i8042-cmd", 1); |
Efimov Vasily | 3115b9e | 2016-06-22 15:24:51 +0300 | [diff] [blame] | 844 | |
Mark Cave-Ayland | 652fbff | 2022-07-12 22:52:47 +0100 | [diff] [blame] | 845 | object_initialize_child(obj, "ps2kbd", &s->ps2kbd, TYPE_PS2_KBD_DEVICE); |
Mark Cave-Ayland | 9d1a425 | 2022-07-12 22:52:49 +0100 | [diff] [blame] | 846 | object_initialize_child(obj, "ps2mouse", &s->ps2mouse, |
| 847 | TYPE_PS2_MOUSE_DEVICE); |
Mark Cave-Ayland | 652fbff | 2022-07-12 22:52:47 +0100 | [diff] [blame] | 848 | |
Efimov Vasily | 3115b9e | 2016-06-22 15:24:51 +0300 | [diff] [blame] | 849 | qdev_init_gpio_out_named(DEVICE(obj), &s->a20_out, I8042_A20_LINE, 1); |
Mark Cave-Ayland | 6eb252d | 2022-06-24 14:41:07 +0100 | [diff] [blame] | 850 | |
| 851 | qdev_init_gpio_out(DEVICE(obj), s->irqs, 2); |
| 852 | qdev_init_gpio_in_named(DEVICE(obj), i8042_set_kbd_irq, |
| 853 | "ps2-kbd-input-irq", 1); |
| 854 | qdev_init_gpio_in_named(DEVICE(obj), i8042_set_mouse_irq, |
| 855 | "ps2-mouse-input-irq", 1); |
Andreas Färber | db895a1 | 2012-11-25 02:37:14 +0100 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | static void i8042_realizefn(DeviceState *dev, Error **errp) |
| 859 | { |
| 860 | ISADevice *isadev = ISA_DEVICE(dev); |
Andreas Färber | a2e0b86 | 2013-04-27 22:18:47 +0200 | [diff] [blame] | 861 | ISAKBDState *isa_s = I8042(dev); |
Richard Henderson | dbff76a | 2011-08-10 15:28:17 -0700 | [diff] [blame] | 862 | KBDState *s = &isa_s->kbd; |
Gerd Hoffmann | da85ccf | 2009-07-31 12:30:15 +0200 | [diff] [blame] | 863 | |
Bernhard Beschow | b86ce7a | 2022-03-01 23:00:33 +0100 | [diff] [blame] | 864 | if (isa_s->kbd_irq >= ISA_NUM_IRQS) { |
| 865 | error_setg(errp, "Maximum value for \"kbd-irq\" is: %u", |
| 866 | ISA_NUM_IRQS - 1); |
| 867 | return; |
| 868 | } |
| 869 | |
| 870 | if (isa_s->mouse_irq >= ISA_NUM_IRQS) { |
| 871 | error_setg(errp, "Maximum value for \"mouse-irq\" is: %u", |
| 872 | ISA_NUM_IRQS - 1); |
| 873 | return; |
| 874 | } |
| 875 | |
Mark Cave-Ayland | 6eb252d | 2022-06-24 14:41:07 +0100 | [diff] [blame] | 876 | isa_connect_gpio_out(isadev, I8042_KBD_IRQ, isa_s->kbd_irq); |
| 877 | isa_connect_gpio_out(isadev, I8042_MOUSE_IRQ, isa_s->mouse_irq); |
Gerd Hoffmann | da85ccf | 2009-07-31 12:30:15 +0200 | [diff] [blame] | 878 | |
Andreas Färber | db895a1 | 2012-11-25 02:37:14 +0100 | [diff] [blame] | 879 | isa_register_ioport(isadev, isa_s->io + 0, 0x60); |
| 880 | isa_register_ioport(isadev, isa_s->io + 1, 0x64); |
Gerd Hoffmann | da85ccf | 2009-07-31 12:30:15 +0200 | [diff] [blame] | 881 | |
Mark Cave-Ayland | 652fbff | 2022-07-12 22:52:47 +0100 | [diff] [blame] | 882 | if (!sysbus_realize(SYS_BUS_DEVICE(&s->ps2kbd), errp)) { |
| 883 | return; |
| 884 | } |
| 885 | |
| 886 | qdev_connect_gpio_out(DEVICE(&s->ps2kbd), PS2_DEVICE_IRQ, |
Mark Cave-Ayland | 6eb252d | 2022-06-24 14:41:07 +0100 | [diff] [blame] | 887 | qdev_get_gpio_in_named(dev, "ps2-kbd-input-irq", |
| 888 | 0)); |
Mark Cave-Ayland | 652fbff | 2022-07-12 22:52:47 +0100 | [diff] [blame] | 889 | |
Mark Cave-Ayland | 9d1a425 | 2022-07-12 22:52:49 +0100 | [diff] [blame] | 890 | if (!sysbus_realize(SYS_BUS_DEVICE(&s->ps2mouse), errp)) { |
| 891 | return; |
| 892 | } |
| 893 | |
| 894 | qdev_connect_gpio_out(DEVICE(&s->ps2mouse), PS2_DEVICE_IRQ, |
Mark Cave-Ayland | 6eb252d | 2022-06-24 14:41:07 +0100 | [diff] [blame] | 895 | qdev_get_gpio_in_named(dev, "ps2-mouse-input-irq", |
| 896 | 0)); |
Mark Cave-Ayland | 9d1a425 | 2022-07-12 22:52:49 +0100 | [diff] [blame] | 897 | |
Volker Rümelin | d1e4566 | 2021-05-25 20:14:35 +0200 | [diff] [blame] | 898 | if (isa_s->kbd_throttle && !isa_s->kbd.extended_state) { |
| 899 | warn_report(TYPE_I8042 ": can't enable kbd-throttle without" |
| 900 | " extended-state, disabling kbd-throttle"); |
| 901 | } else if (isa_s->kbd_throttle) { |
| 902 | s->throttle_timer = timer_new_us(QEMU_CLOCK_VIRTUAL, |
| 903 | kbd_throttle_timeout, s); |
| 904 | } |
Gerd Hoffmann | da85ccf | 2009-07-31 12:30:15 +0200 | [diff] [blame] | 905 | } |
| 906 | |
Igor Mammedov | 68f0131 | 2022-06-08 09:53:12 -0400 | [diff] [blame] | 907 | static void i8042_build_aml(AcpiDevAmlIf *adev, Aml *scope) |
Gerd Hoffmann | df0f3d1 | 2020-06-19 11:18:59 +0200 | [diff] [blame] | 908 | { |
Igor Mammedov | 68f0131 | 2022-06-08 09:53:12 -0400 | [diff] [blame] | 909 | ISAKBDState *isa_s = I8042(adev); |
Gerd Hoffmann | df0f3d1 | 2020-06-19 11:18:59 +0200 | [diff] [blame] | 910 | Aml *kbd; |
| 911 | Aml *mou; |
| 912 | Aml *crs; |
| 913 | |
| 914 | crs = aml_resource_template(); |
| 915 | aml_append(crs, aml_io(AML_DECODE16, 0x0060, 0x0060, 0x01, 0x01)); |
| 916 | aml_append(crs, aml_io(AML_DECODE16, 0x0064, 0x0064, 0x01, 0x01)); |
Bernhard Beschow | b86ce7a | 2022-03-01 23:00:33 +0100 | [diff] [blame] | 917 | aml_append(crs, aml_irq_no_flags(isa_s->kbd_irq)); |
Gerd Hoffmann | df0f3d1 | 2020-06-19 11:18:59 +0200 | [diff] [blame] | 918 | |
| 919 | kbd = aml_device("KBD"); |
| 920 | aml_append(kbd, aml_name_decl("_HID", aml_eisaid("PNP0303"))); |
| 921 | aml_append(kbd, aml_name_decl("_STA", aml_int(0xf))); |
| 922 | aml_append(kbd, aml_name_decl("_CRS", crs)); |
| 923 | |
| 924 | crs = aml_resource_template(); |
Bernhard Beschow | b86ce7a | 2022-03-01 23:00:33 +0100 | [diff] [blame] | 925 | aml_append(crs, aml_irq_no_flags(isa_s->mouse_irq)); |
Gerd Hoffmann | df0f3d1 | 2020-06-19 11:18:59 +0200 | [diff] [blame] | 926 | |
| 927 | mou = aml_device("MOU"); |
| 928 | aml_append(mou, aml_name_decl("_HID", aml_eisaid("PNP0F13"))); |
| 929 | aml_append(mou, aml_name_decl("_STA", aml_int(0xf))); |
| 930 | aml_append(mou, aml_name_decl("_CRS", crs)); |
| 931 | |
| 932 | aml_append(scope, kbd); |
| 933 | aml_append(scope, mou); |
| 934 | } |
| 935 | |
Volker Rümelin | ff6e162 | 2021-05-25 20:14:34 +0200 | [diff] [blame] | 936 | static Property i8042_properties[] = { |
| 937 | DEFINE_PROP_BOOL("extended-state", ISAKBDState, kbd.extended_state, true), |
Volker Rümelin | d1e4566 | 2021-05-25 20:14:35 +0200 | [diff] [blame] | 938 | DEFINE_PROP_BOOL("kbd-throttle", ISAKBDState, kbd_throttle, false), |
Bernhard Beschow | b86ce7a | 2022-03-01 23:00:33 +0100 | [diff] [blame] | 939 | DEFINE_PROP_UINT8("kbd-irq", ISAKBDState, kbd_irq, 1), |
| 940 | DEFINE_PROP_UINT8("mouse-irq", ISAKBDState, mouse_irq, 12), |
Volker Rümelin | ff6e162 | 2021-05-25 20:14:34 +0200 | [diff] [blame] | 941 | DEFINE_PROP_END_OF_LIST(), |
| 942 | }; |
| 943 | |
Anthony Liguori | 8f04ee0 | 2011-12-04 11:52:49 -0600 | [diff] [blame] | 944 | static void i8042_class_initfn(ObjectClass *klass, void *data) |
| 945 | { |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 946 | DeviceClass *dc = DEVICE_CLASS(klass); |
Igor Mammedov | 68f0131 | 2022-06-08 09:53:12 -0400 | [diff] [blame] | 947 | AcpiDevAmlIfClass *adevc = ACPI_DEV_AML_IF_CLASS(klass); |
Andreas Färber | db895a1 | 2012-11-25 02:37:14 +0100 | [diff] [blame] | 948 | |
Volker Rümelin | ff6e162 | 2021-05-25 20:14:34 +0200 | [diff] [blame] | 949 | device_class_set_props(dc, i8042_properties); |
Peter Maydell | e3d0814 | 2024-09-13 15:31:44 +0100 | [diff] [blame] | 950 | device_class_set_legacy_reset(dc, i8042_reset); |
Andreas Färber | db895a1 | 2012-11-25 02:37:14 +0100 | [diff] [blame] | 951 | dc->realize = i8042_realizefn; |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 952 | dc->vmsd = &vmstate_kbd_isa; |
Igor Mammedov | 68f0131 | 2022-06-08 09:53:12 -0400 | [diff] [blame] | 953 | adevc->build_dev_aml = i8042_build_aml; |
kumar sourav | cbe9ed7 | 2019-01-25 20:44:40 +0530 | [diff] [blame] | 954 | set_bit(DEVICE_CATEGORY_INPUT, dc->categories); |
Anthony Liguori | 8f04ee0 | 2011-12-04 11:52:49 -0600 | [diff] [blame] | 955 | } |
| 956 | |
Andreas Färber | 8c43a6f | 2013-01-10 16:19:07 +0100 | [diff] [blame] | 957 | static const TypeInfo i8042_info = { |
Andreas Färber | a2e0b86 | 2013-04-27 22:18:47 +0200 | [diff] [blame] | 958 | .name = TYPE_I8042, |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 959 | .parent = TYPE_ISA_DEVICE, |
| 960 | .instance_size = sizeof(ISAKBDState), |
Andreas Färber | db895a1 | 2012-11-25 02:37:14 +0100 | [diff] [blame] | 961 | .instance_init = i8042_initfn, |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 962 | .class_init = i8042_class_initfn, |
Igor Mammedov | 68f0131 | 2022-06-08 09:53:12 -0400 | [diff] [blame] | 963 | .interfaces = (InterfaceInfo[]) { |
| 964 | { TYPE_ACPI_DEV_AML_IF }, |
| 965 | { }, |
| 966 | }, |
Gerd Hoffmann | da85ccf | 2009-07-31 12:30:15 +0200 | [diff] [blame] | 967 | }; |
| 968 | |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 969 | static void i8042_register_types(void) |
Gerd Hoffmann | da85ccf | 2009-07-31 12:30:15 +0200 | [diff] [blame] | 970 | { |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 971 | type_register_static(&i8042_info); |
Mark Cave-Ayland | 150ee01 | 2022-06-24 14:40:36 +0100 | [diff] [blame] | 972 | type_register_static(&i8042_mmio_info); |
Gerd Hoffmann | da85ccf | 2009-07-31 12:30:15 +0200 | [diff] [blame] | 973 | } |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 974 | |
| 975 | type_init(i8042_register_types) |