Statistics
| Branch: | Revision:

root / hw / pckbd.c @ e4c7d2ae

History | View | Annotate | Download (14.7 kB)

1 80cabfad bellard
/*
2 80cabfad bellard
 * QEMU PC keyboard emulation
3 5fafdf24 ths
 *
4 80cabfad bellard
 * Copyright (c) 2003 Fabrice Bellard
5 5fafdf24 ths
 *
6 80cabfad bellard
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 80cabfad bellard
 * of this software and associated documentation files (the "Software"), to deal
8 80cabfad bellard
 * in the Software without restriction, including without limitation the rights
9 80cabfad bellard
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 80cabfad bellard
 * copies of the Software, and to permit persons to whom the Software is
11 80cabfad bellard
 * furnished to do so, subject to the following conditions:
12 80cabfad bellard
 *
13 80cabfad bellard
 * The above copyright notice and this permission notice shall be included in
14 80cabfad bellard
 * all copies or substantial portions of the Software.
15 80cabfad bellard
 *
16 80cabfad bellard
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 80cabfad bellard
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 80cabfad bellard
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 80cabfad bellard
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 80cabfad bellard
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 80cabfad bellard
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 80cabfad bellard
 * THE SOFTWARE.
23 80cabfad bellard
 */
24 87ecb68b pbrook
#include "hw.h"
25 87ecb68b pbrook
#include "isa.h"
26 87ecb68b pbrook
#include "pc.h"
27 87ecb68b pbrook
#include "ps2.h"
28 87ecb68b pbrook
#include "sysemu.h"
29 80cabfad bellard
30 80cabfad bellard
/* debug PC keyboard */
31 80cabfad bellard
//#define DEBUG_KBD
32 c86d2c23 Blue Swirl
#ifdef DEBUG_KBD
33 c86d2c23 Blue Swirl
#define DPRINTF(fmt, ...)                                       \
34 c86d2c23 Blue Swirl
    do { printf("KBD: " fmt , ## __VA_ARGS__); } while (0)
35 c86d2c23 Blue Swirl
#else
36 c86d2c23 Blue Swirl
#define DPRINTF(fmt, ...)
37 c86d2c23 Blue Swirl
#endif
38 80cabfad bellard
39 80cabfad bellard
/*        Keyboard Controller Commands */
40 80cabfad bellard
#define KBD_CCMD_READ_MODE        0x20        /* Read mode bits */
41 80cabfad bellard
#define KBD_CCMD_WRITE_MODE        0x60        /* Write mode bits */
42 80cabfad bellard
#define KBD_CCMD_GET_VERSION        0xA1        /* Get controller version */
43 80cabfad bellard
#define KBD_CCMD_MOUSE_DISABLE        0xA7        /* Disable mouse interface */
44 80cabfad bellard
#define KBD_CCMD_MOUSE_ENABLE        0xA8        /* Enable mouse interface */
45 80cabfad bellard
#define KBD_CCMD_TEST_MOUSE        0xA9        /* Mouse interface test */
46 80cabfad bellard
#define KBD_CCMD_SELF_TEST        0xAA        /* Controller self test */
47 80cabfad bellard
#define KBD_CCMD_KBD_TEST        0xAB        /* Keyboard interface test */
48 80cabfad bellard
#define KBD_CCMD_KBD_DISABLE        0xAD        /* Keyboard interface disable */
49 80cabfad bellard
#define KBD_CCMD_KBD_ENABLE        0xAE        /* Keyboard interface enable */
50 80cabfad bellard
#define KBD_CCMD_READ_INPORT    0xC0    /* read input port */
51 80cabfad bellard
#define KBD_CCMD_READ_OUTPORT        0xD0    /* read output port */
52 80cabfad bellard
#define KBD_CCMD_WRITE_OUTPORT        0xD1    /* write output port */
53 80cabfad bellard
#define KBD_CCMD_WRITE_OBUF        0xD2
54 80cabfad bellard
#define KBD_CCMD_WRITE_AUX_OBUF        0xD3    /* Write to output buffer as if
55 80cabfad bellard
                                           initiated by the auxiliary device */
56 80cabfad bellard
#define KBD_CCMD_WRITE_MOUSE        0xD4        /* Write the following byte to the mouse */
57 80cabfad bellard
#define KBD_CCMD_DISABLE_A20    0xDD    /* HP vectra only ? */
58 80cabfad bellard
#define KBD_CCMD_ENABLE_A20     0xDF    /* HP vectra only ? */
59 80cabfad bellard
#define KBD_CCMD_RESET                0xFE
60 80cabfad bellard
61 80cabfad bellard
/* Keyboard Commands */
62 80cabfad bellard
#define KBD_CMD_SET_LEDS        0xED        /* Set keyboard leds */
63 80cabfad bellard
#define KBD_CMD_ECHO             0xEE
64 80cabfad bellard
#define KBD_CMD_GET_ID                 0xF2        /* get keyboard ID */
65 80cabfad bellard
#define KBD_CMD_SET_RATE        0xF3        /* Set typematic rate */
66 80cabfad bellard
#define KBD_CMD_ENABLE                0xF4        /* Enable scanning */
67 80cabfad bellard
#define KBD_CMD_RESET_DISABLE        0xF5        /* reset and disable scanning */
68 80cabfad bellard
#define KBD_CMD_RESET_ENABLE           0xF6    /* reset and enable scanning */
69 80cabfad bellard
#define KBD_CMD_RESET                0xFF        /* Reset */
70 80cabfad bellard
71 80cabfad bellard
/* Keyboard Replies */
72 80cabfad bellard
#define KBD_REPLY_POR                0xAA        /* Power on reset */
73 80cabfad bellard
#define KBD_REPLY_ACK                0xFA        /* Command ACK */
74 80cabfad bellard
#define KBD_REPLY_RESEND        0xFE        /* Command NACK, send the cmd again */
75 80cabfad bellard
76 80cabfad bellard
/* Status Register Bits */
77 80cabfad bellard
#define KBD_STAT_OBF                 0x01        /* Keyboard output buffer full */
78 80cabfad bellard
#define KBD_STAT_IBF                 0x02        /* Keyboard input buffer full */
79 80cabfad bellard
#define KBD_STAT_SELFTEST        0x04        /* Self test successful */
80 80cabfad bellard
#define KBD_STAT_CMD                0x08        /* Last write was a command write (0=data) */
81 80cabfad bellard
#define KBD_STAT_UNLOCKED        0x10        /* Zero if keyboard locked */
82 80cabfad bellard
#define KBD_STAT_MOUSE_OBF        0x20        /* Mouse output buffer full */
83 80cabfad bellard
#define KBD_STAT_GTO                 0x40        /* General receive/xmit timeout */
84 80cabfad bellard
#define KBD_STAT_PERR                 0x80        /* Parity error */
85 80cabfad bellard
86 80cabfad bellard
/* Controller Mode Register Bits */
87 80cabfad bellard
#define KBD_MODE_KBD_INT        0x01        /* Keyboard data generate IRQ1 */
88 80cabfad bellard
#define KBD_MODE_MOUSE_INT        0x02        /* Mouse data generate IRQ12 */
89 80cabfad bellard
#define KBD_MODE_SYS                 0x04        /* The system flag (?) */
90 80cabfad bellard
#define KBD_MODE_NO_KEYLOCK        0x08        /* The keylock doesn't affect the keyboard if set */
91 80cabfad bellard
#define KBD_MODE_DISABLE_KBD        0x10        /* Disable keyboard interface */
92 80cabfad bellard
#define KBD_MODE_DISABLE_MOUSE        0x20        /* Disable mouse interface */
93 80cabfad bellard
#define KBD_MODE_KCC                 0x40        /* Scan code conversion to PC format */
94 80cabfad bellard
#define KBD_MODE_RFU                0x80
95 80cabfad bellard
96 956a3e6b Blue Swirl
/* Output Port Bits */
97 956a3e6b Blue Swirl
#define KBD_OUT_RESET           0x01    /* 1=normal mode, 0=reset */
98 956a3e6b Blue Swirl
#define KBD_OUT_A20             0x02    /* x86 only */
99 956a3e6b Blue Swirl
#define KBD_OUT_OBF             0x10    /* Keyboard output buffer full */
100 956a3e6b Blue Swirl
#define KBD_OUT_MOUSE_OBF       0x20    /* Mouse output buffer full */
101 956a3e6b Blue Swirl
102 80cabfad bellard
/* Mouse Commands */
103 80cabfad bellard
#define AUX_SET_SCALE11                0xE6        /* Set 1:1 scaling */
104 80cabfad bellard
#define AUX_SET_SCALE21                0xE7        /* Set 2:1 scaling */
105 80cabfad bellard
#define AUX_SET_RES                0xE8        /* Set resolution */
106 80cabfad bellard
#define AUX_GET_SCALE                0xE9        /* Get scaling factor */
107 80cabfad bellard
#define AUX_SET_STREAM                0xEA        /* Set stream mode */
108 80cabfad bellard
#define AUX_POLL                0xEB        /* Poll */
109 80cabfad bellard
#define AUX_RESET_WRAP                0xEC        /* Reset wrap mode */
110 80cabfad bellard
#define AUX_SET_WRAP                0xEE        /* Set wrap mode */
111 80cabfad bellard
#define AUX_SET_REMOTE                0xF0        /* Set remote mode */
112 80cabfad bellard
#define AUX_GET_TYPE                0xF2        /* Get type */
113 80cabfad bellard
#define AUX_SET_SAMPLE                0xF3        /* Set sample rate */
114 80cabfad bellard
#define AUX_ENABLE_DEV                0xF4        /* Enable aux device */
115 80cabfad bellard
#define AUX_DISABLE_DEV                0xF5        /* Disable aux device */
116 80cabfad bellard
#define AUX_SET_DEFAULT                0xF6
117 80cabfad bellard
#define AUX_RESET                0xFF        /* Reset aux device */
118 80cabfad bellard
#define AUX_ACK                        0xFA        /* Command byte ACK. */
119 80cabfad bellard
120 80cabfad bellard
#define MOUSE_STATUS_REMOTE     0x40
121 80cabfad bellard
#define MOUSE_STATUS_ENABLED    0x20
122 80cabfad bellard
#define MOUSE_STATUS_SCALE21    0x10
123 80cabfad bellard
124 daa57963 bellard
#define KBD_PENDING_KBD         1
125 daa57963 bellard
#define KBD_PENDING_AUX         2
126 80cabfad bellard
127 80cabfad bellard
typedef struct KBDState {
128 80cabfad bellard
    uint8_t write_cmd; /* if non zero, write data to port 60 is expected */
129 80cabfad bellard
    uint8_t status;
130 80cabfad bellard
    uint8_t mode;
131 956a3e6b Blue Swirl
    uint8_t outport;
132 daa57963 bellard
    /* Bitmask of devices with data available.  */
133 7783e9f0 pbrook
    uint8_t pending;
134 daa57963 bellard
    void *kbd;
135 daa57963 bellard
    void *mouse;
136 b7678d96 ths
137 d537cf6c pbrook
    qemu_irq irq_kbd;
138 d537cf6c pbrook
    qemu_irq irq_mouse;
139 956a3e6b Blue Swirl
    qemu_irq *a20_out;
140 c227f099 Anthony Liguori
    target_phys_addr_t mask;
141 80cabfad bellard
} KBDState;
142 80cabfad bellard
143 80cabfad bellard
/* update irq and KBD_STAT_[MOUSE_]OBF */
144 80cabfad bellard
/* XXX: not generating the irqs if KBD_MODE_DISABLE_KBD is set may be
145 80cabfad bellard
   incorrect, but it avoids having to simulate exact delays */
146 80cabfad bellard
static void kbd_update_irq(KBDState *s)
147 80cabfad bellard
{
148 b7678d96 ths
    int irq_kbd_level, irq_mouse_level;
149 80cabfad bellard
150 b7678d96 ths
    irq_kbd_level = 0;
151 b7678d96 ths
    irq_mouse_level = 0;
152 80cabfad bellard
    s->status &= ~(KBD_STAT_OBF | KBD_STAT_MOUSE_OBF);
153 956a3e6b Blue Swirl
    s->outport &= ~(KBD_OUT_OBF | KBD_OUT_MOUSE_OBF);
154 daa57963 bellard
    if (s->pending) {
155 80cabfad bellard
        s->status |= KBD_STAT_OBF;
156 956a3e6b Blue Swirl
        s->outport |= KBD_OUT_OBF;
157 b92bb99b ths
        /* kbd data takes priority over aux data.  */
158 daa57963 bellard
        if (s->pending == KBD_PENDING_AUX) {
159 80cabfad bellard
            s->status |= KBD_STAT_MOUSE_OBF;
160 956a3e6b Blue Swirl
            s->outport |= KBD_OUT_MOUSE_OBF;
161 80cabfad bellard
            if (s->mode & KBD_MODE_MOUSE_INT)
162 b7678d96 ths
                irq_mouse_level = 1;
163 80cabfad bellard
        } else {
164 5fafdf24 ths
            if ((s->mode & KBD_MODE_KBD_INT) &&
165 80cabfad bellard
                !(s->mode & KBD_MODE_DISABLE_KBD))
166 b7678d96 ths
                irq_kbd_level = 1;
167 80cabfad bellard
        }
168 80cabfad bellard
    }
169 d537cf6c pbrook
    qemu_set_irq(s->irq_kbd, irq_kbd_level);
170 d537cf6c pbrook
    qemu_set_irq(s->irq_mouse, irq_mouse_level);
171 80cabfad bellard
}
172 80cabfad bellard
173 daa57963 bellard
static void kbd_update_kbd_irq(void *opaque, int level)
174 80cabfad bellard
{
175 daa57963 bellard
    KBDState *s = (KBDState *)opaque;
176 80cabfad bellard
177 daa57963 bellard
    if (level)
178 daa57963 bellard
        s->pending |= KBD_PENDING_KBD;
179 80cabfad bellard
    else
180 daa57963 bellard
        s->pending &= ~KBD_PENDING_KBD;
181 80cabfad bellard
    kbd_update_irq(s);
182 80cabfad bellard
}
183 80cabfad bellard
184 daa57963 bellard
static void kbd_update_aux_irq(void *opaque, int level)
185 80cabfad bellard
{
186 daa57963 bellard
    KBDState *s = (KBDState *)opaque;
187 daa57963 bellard
188 daa57963 bellard
    if (level)
189 daa57963 bellard
        s->pending |= KBD_PENDING_AUX;
190 daa57963 bellard
    else
191 daa57963 bellard
        s->pending &= ~KBD_PENDING_AUX;
192 daa57963 bellard
    kbd_update_irq(s);
193 80cabfad bellard
}
194 80cabfad bellard
195 b41a2cd1 bellard
static uint32_t kbd_read_status(void *opaque, uint32_t addr)
196 80cabfad bellard
{
197 b41a2cd1 bellard
    KBDState *s = opaque;
198 80cabfad bellard
    int val;
199 80cabfad bellard
    val = s->status;
200 c86d2c23 Blue Swirl
    DPRINTF("kbd: read status=0x%02x\n", val);
201 80cabfad bellard
    return val;
202 80cabfad bellard
}
203 80cabfad bellard
204 daa57963 bellard
static void kbd_queue(KBDState *s, int b, int aux)
205 daa57963 bellard
{
206 daa57963 bellard
    if (aux)
207 daa57963 bellard
        ps2_queue(s->mouse, b);
208 daa57963 bellard
    else
209 daa57963 bellard
        ps2_queue(s->kbd, b);
210 daa57963 bellard
}
211 daa57963 bellard
212 956a3e6b Blue Swirl
static void ioport92_write(void *opaque, uint32_t addr, uint32_t val)
213 956a3e6b Blue Swirl
{
214 956a3e6b Blue Swirl
    KBDState *s = opaque;
215 956a3e6b Blue Swirl
216 c86d2c23 Blue Swirl
    DPRINTF("kbd: write outport=0x%02x\n", val);
217 956a3e6b Blue Swirl
    s->outport = val;
218 956a3e6b Blue Swirl
    if (s->a20_out) {
219 956a3e6b Blue Swirl
        qemu_set_irq(*s->a20_out, (val >> 1) & 1);
220 956a3e6b Blue Swirl
    }
221 956a3e6b Blue Swirl
    if (!(val & 1)) {
222 956a3e6b Blue Swirl
        qemu_system_reset_request();
223 956a3e6b Blue Swirl
    }
224 956a3e6b Blue Swirl
}
225 956a3e6b Blue Swirl
226 956a3e6b Blue Swirl
static uint32_t ioport92_read(void *opaque, uint32_t addr)
227 956a3e6b Blue Swirl
{
228 956a3e6b Blue Swirl
    KBDState *s = opaque;
229 c86d2c23 Blue Swirl
    uint32_t ret;
230 956a3e6b Blue Swirl
231 c86d2c23 Blue Swirl
    ret = s->outport;
232 c86d2c23 Blue Swirl
    DPRINTF("kbd: read outport=0x%02x\n", ret);
233 c86d2c23 Blue Swirl
    return ret;
234 956a3e6b Blue Swirl
}
235 956a3e6b Blue Swirl
236 b41a2cd1 bellard
static void kbd_write_command(void *opaque, uint32_t addr, uint32_t val)
237 80cabfad bellard
{
238 b41a2cd1 bellard
    KBDState *s = opaque;
239 80cabfad bellard
240 c86d2c23 Blue Swirl
    DPRINTF("kbd: write cmd=0x%02x\n", val);
241 80cabfad bellard
    switch(val) {
242 80cabfad bellard
    case KBD_CCMD_READ_MODE:
243 889bec69 balrog
        kbd_queue(s, s->mode, 0);
244 80cabfad bellard
        break;
245 80cabfad bellard
    case KBD_CCMD_WRITE_MODE:
246 80cabfad bellard
    case KBD_CCMD_WRITE_OBUF:
247 80cabfad bellard
    case KBD_CCMD_WRITE_AUX_OBUF:
248 80cabfad bellard
    case KBD_CCMD_WRITE_MOUSE:
249 80cabfad bellard
    case KBD_CCMD_WRITE_OUTPORT:
250 80cabfad bellard
        s->write_cmd = val;
251 80cabfad bellard
        break;
252 80cabfad bellard
    case KBD_CCMD_MOUSE_DISABLE:
253 80cabfad bellard
        s->mode |= KBD_MODE_DISABLE_MOUSE;
254 80cabfad bellard
        break;
255 80cabfad bellard
    case KBD_CCMD_MOUSE_ENABLE:
256 80cabfad bellard
        s->mode &= ~KBD_MODE_DISABLE_MOUSE;
257 80cabfad bellard
        break;
258 80cabfad bellard
    case KBD_CCMD_TEST_MOUSE:
259 80cabfad bellard
        kbd_queue(s, 0x00, 0);
260 80cabfad bellard
        break;
261 80cabfad bellard
    case KBD_CCMD_SELF_TEST:
262 80cabfad bellard
        s->status |= KBD_STAT_SELFTEST;
263 80cabfad bellard
        kbd_queue(s, 0x55, 0);
264 80cabfad bellard
        break;
265 80cabfad bellard
    case KBD_CCMD_KBD_TEST:
266 80cabfad bellard
        kbd_queue(s, 0x00, 0);
267 80cabfad bellard
        break;
268 80cabfad bellard
    case KBD_CCMD_KBD_DISABLE:
269 80cabfad bellard
        s->mode |= KBD_MODE_DISABLE_KBD;
270 80cabfad bellard
        kbd_update_irq(s);
271 80cabfad bellard
        break;
272 80cabfad bellard
    case KBD_CCMD_KBD_ENABLE:
273 80cabfad bellard
        s->mode &= ~KBD_MODE_DISABLE_KBD;
274 80cabfad bellard
        kbd_update_irq(s);
275 80cabfad bellard
        break;
276 80cabfad bellard
    case KBD_CCMD_READ_INPORT:
277 80cabfad bellard
        kbd_queue(s, 0x00, 0);
278 80cabfad bellard
        break;
279 80cabfad bellard
    case KBD_CCMD_READ_OUTPORT:
280 956a3e6b Blue Swirl
        kbd_queue(s, s->outport, 0);
281 80cabfad bellard
        break;
282 80cabfad bellard
    case KBD_CCMD_ENABLE_A20:
283 956a3e6b Blue Swirl
        if (s->a20_out) {
284 956a3e6b Blue Swirl
            qemu_irq_raise(*s->a20_out);
285 956a3e6b Blue Swirl
        }
286 956a3e6b Blue Swirl
        s->outport |= KBD_OUT_A20;
287 80cabfad bellard
        break;
288 80cabfad bellard
    case KBD_CCMD_DISABLE_A20:
289 956a3e6b Blue Swirl
        if (s->a20_out) {
290 956a3e6b Blue Swirl
            qemu_irq_lower(*s->a20_out);
291 956a3e6b Blue Swirl
        }
292 956a3e6b Blue Swirl
        s->outport &= ~KBD_OUT_A20;
293 80cabfad bellard
        break;
294 80cabfad bellard
    case KBD_CCMD_RESET:
295 d7d02e3c bellard
        qemu_system_reset_request();
296 80cabfad bellard
        break;
297 80cabfad bellard
    case 0xff:
298 80cabfad bellard
        /* ignore that - I don't know what is its use */
299 80cabfad bellard
        break;
300 80cabfad bellard
    default:
301 80cabfad bellard
        fprintf(stderr, "qemu: unsupported keyboard cmd=0x%02x\n", val);
302 80cabfad bellard
        break;
303 80cabfad bellard
    }
304 80cabfad bellard
}
305 80cabfad bellard
306 b41a2cd1 bellard
static uint32_t kbd_read_data(void *opaque, uint32_t addr)
307 80cabfad bellard
{
308 b41a2cd1 bellard
    KBDState *s = opaque;
309 e41c0f26 balrog
    uint32_t val;
310 80cabfad bellard
311 daa57963 bellard
    if (s->pending == KBD_PENDING_AUX)
312 e41c0f26 balrog
        val = ps2_read_data(s->mouse);
313 e41c0f26 balrog
    else
314 e41c0f26 balrog
        val = ps2_read_data(s->kbd);
315 80cabfad bellard
316 c86d2c23 Blue Swirl
    DPRINTF("kbd: read data=0x%02x\n", val);
317 e41c0f26 balrog
    return val;
318 80cabfad bellard
}
319 80cabfad bellard
320 9596ebb7 pbrook
static void kbd_write_data(void *opaque, uint32_t addr, uint32_t val)
321 80cabfad bellard
{
322 b41a2cd1 bellard
    KBDState *s = opaque;
323 80cabfad bellard
324 c86d2c23 Blue Swirl
    DPRINTF("kbd: write data=0x%02x\n", val);
325 80cabfad bellard
326 80cabfad bellard
    switch(s->write_cmd) {
327 80cabfad bellard
    case 0:
328 daa57963 bellard
        ps2_write_keyboard(s->kbd, val);
329 80cabfad bellard
        break;
330 80cabfad bellard
    case KBD_CCMD_WRITE_MODE:
331 80cabfad bellard
        s->mode = val;
332 f94f5d71 pbrook
        ps2_keyboard_set_translation(s->kbd, (s->mode & KBD_MODE_KCC) != 0);
333 daa57963 bellard
        /* ??? */
334 80cabfad bellard
        kbd_update_irq(s);
335 80cabfad bellard
        break;
336 80cabfad bellard
    case KBD_CCMD_WRITE_OBUF:
337 80cabfad bellard
        kbd_queue(s, val, 0);
338 80cabfad bellard
        break;
339 80cabfad bellard
    case KBD_CCMD_WRITE_AUX_OBUF:
340 80cabfad bellard
        kbd_queue(s, val, 1);
341 80cabfad bellard
        break;
342 80cabfad bellard
    case KBD_CCMD_WRITE_OUTPORT:
343 956a3e6b Blue Swirl
        ioport92_write(s, 0, val);
344 80cabfad bellard
        break;
345 80cabfad bellard
    case KBD_CCMD_WRITE_MOUSE:
346 daa57963 bellard
        ps2_write_mouse(s->mouse, val);
347 80cabfad bellard
        break;
348 80cabfad bellard
    default:
349 80cabfad bellard
        break;
350 80cabfad bellard
    }
351 80cabfad bellard
    s->write_cmd = 0;
352 80cabfad bellard
}
353 80cabfad bellard
354 d7d02e3c bellard
static void kbd_reset(void *opaque)
355 80cabfad bellard
{
356 d7d02e3c bellard
    KBDState *s = opaque;
357 80cabfad bellard
358 80cabfad bellard
    s->mode = KBD_MODE_KBD_INT | KBD_MODE_MOUSE_INT;
359 80cabfad bellard
    s->status = KBD_STAT_CMD | KBD_STAT_UNLOCKED;
360 956a3e6b Blue Swirl
    s->outport = KBD_OUT_RESET | KBD_OUT_A20;
361 80cabfad bellard
}
362 80cabfad bellard
363 3c619b59 Juan Quintela
static const VMStateDescription vmstate_kbd = {
364 3c619b59 Juan Quintela
    .name = "pckbd",
365 3c619b59 Juan Quintela
    .version_id = 3,
366 3c619b59 Juan Quintela
    .minimum_version_id = 3,
367 3c619b59 Juan Quintela
    .minimum_version_id_old = 3,
368 3c619b59 Juan Quintela
    .fields      = (VMStateField []) {
369 3c619b59 Juan Quintela
        VMSTATE_UINT8(write_cmd, KBDState),
370 3c619b59 Juan Quintela
        VMSTATE_UINT8(status, KBDState),
371 3c619b59 Juan Quintela
        VMSTATE_UINT8(mode, KBDState),
372 3c619b59 Juan Quintela
        VMSTATE_UINT8(pending, KBDState),
373 3c619b59 Juan Quintela
        VMSTATE_END_OF_LIST()
374 3c619b59 Juan Quintela
    }
375 3c619b59 Juan Quintela
};
376 675376f2 bellard
377 b92bb99b ths
/* Memory mapped interface */
378 c227f099 Anthony Liguori
static uint32_t kbd_mm_readb (void *opaque, target_phys_addr_t addr)
379 b92bb99b ths
{
380 b92bb99b ths
    KBDState *s = opaque;
381 b92bb99b ths
382 4efbe58f aurel32
    if (addr & s->mask)
383 80355292 ths
        return kbd_read_status(s, 0) & 0xff;
384 4efbe58f aurel32
    else
385 4efbe58f aurel32
        return kbd_read_data(s, 0) & 0xff;
386 b92bb99b ths
}
387 b92bb99b ths
388 c227f099 Anthony Liguori
static void kbd_mm_writeb (void *opaque, target_phys_addr_t addr, uint32_t value)
389 b92bb99b ths
{
390 b92bb99b ths
    KBDState *s = opaque;
391 b92bb99b ths
392 4efbe58f aurel32
    if (addr & s->mask)
393 80355292 ths
        kbd_write_command(s, 0, value & 0xff);
394 4efbe58f aurel32
    else
395 4efbe58f aurel32
        kbd_write_data(s, 0, value & 0xff);
396 b92bb99b ths
}
397 b92bb99b ths
398 d60efc6b Blue Swirl
static CPUReadMemoryFunc * const kbd_mm_read[] = {
399 b92bb99b ths
    &kbd_mm_readb,
400 b92bb99b ths
    &kbd_mm_readb,
401 b92bb99b ths
    &kbd_mm_readb,
402 b92bb99b ths
};
403 b92bb99b ths
404 d60efc6b Blue Swirl
static CPUWriteMemoryFunc * const kbd_mm_write[] = {
405 b92bb99b ths
    &kbd_mm_writeb,
406 b92bb99b ths
    &kbd_mm_writeb,
407 b92bb99b ths
    &kbd_mm_writeb,
408 b92bb99b ths
};
409 b92bb99b ths
410 71db710f blueswir1
void i8042_mm_init(qemu_irq kbd_irq, qemu_irq mouse_irq,
411 c227f099 Anthony Liguori
                   target_phys_addr_t base, ram_addr_t size,
412 c227f099 Anthony Liguori
                   target_phys_addr_t mask)
413 b92bb99b ths
{
414 5acd0646 Blue Swirl
    KBDState *s = qemu_mallocz(sizeof(KBDState));
415 b92bb99b ths
    int s_io_memory;
416 b92bb99b ths
417 b92bb99b ths
    s->irq_kbd = kbd_irq;
418 b92bb99b ths
    s->irq_mouse = mouse_irq;
419 4efbe58f aurel32
    s->mask = mask;
420 b92bb99b ths
421 0be71e32 Alex Williamson
    vmstate_register(NULL, 0, &vmstate_kbd, s);
422 1eed09cb Avi Kivity
    s_io_memory = cpu_register_io_memory(kbd_mm_read, kbd_mm_write, s);
423 4efbe58f aurel32
    cpu_register_physical_memory(base, size, s_io_memory);
424 b92bb99b ths
425 b92bb99b ths
    s->kbd = ps2_kbd_init(kbd_update_kbd_irq, s);
426 b92bb99b ths
    s->mouse = ps2_mouse_init(kbd_update_aux_irq, s);
427 a08d4367 Jan Kiszka
    qemu_register_reset(kbd_reset, s);
428 b92bb99b ths
}
429 da85ccfb Gerd Hoffmann
430 da85ccfb Gerd Hoffmann
typedef struct ISAKBDState {
431 da85ccfb Gerd Hoffmann
    ISADevice dev;
432 da85ccfb Gerd Hoffmann
    KBDState  kbd;
433 da85ccfb Gerd Hoffmann
} ISAKBDState;
434 da85ccfb Gerd Hoffmann
435 956a3e6b Blue Swirl
void i8042_isa_mouse_fake_event(void *opaque)
436 956a3e6b Blue Swirl
{
437 956a3e6b Blue Swirl
    ISADevice *dev = opaque;
438 956a3e6b Blue Swirl
    KBDState *s = &(DO_UPCAST(ISAKBDState, dev, dev)->kbd);
439 956a3e6b Blue Swirl
440 956a3e6b Blue Swirl
    ps2_mouse_fake_event(s->mouse);
441 956a3e6b Blue Swirl
}
442 956a3e6b Blue Swirl
443 956a3e6b Blue Swirl
void i8042_setup_a20_line(ISADevice *dev, qemu_irq *a20_out)
444 956a3e6b Blue Swirl
{
445 956a3e6b Blue Swirl
    KBDState *s = &(DO_UPCAST(ISAKBDState, dev, dev)->kbd);
446 956a3e6b Blue Swirl
447 956a3e6b Blue Swirl
    s->a20_out = a20_out;
448 956a3e6b Blue Swirl
}
449 956a3e6b Blue Swirl
450 d05ac8fa Blue Swirl
static const VMStateDescription vmstate_kbd_isa = {
451 be73cfe2 Juan Quintela
    .name = "pckbd",
452 be73cfe2 Juan Quintela
    .version_id = 3,
453 be73cfe2 Juan Quintela
    .minimum_version_id = 3,
454 be73cfe2 Juan Quintela
    .minimum_version_id_old = 3,
455 be73cfe2 Juan Quintela
    .fields      = (VMStateField []) {
456 be73cfe2 Juan Quintela
        VMSTATE_STRUCT(kbd, ISAKBDState, 0, vmstate_kbd, KBDState),
457 be73cfe2 Juan Quintela
        VMSTATE_END_OF_LIST()
458 be73cfe2 Juan Quintela
    }
459 be73cfe2 Juan Quintela
};
460 be73cfe2 Juan Quintela
461 81a322d4 Gerd Hoffmann
static int i8042_initfn(ISADevice *dev)
462 da85ccfb Gerd Hoffmann
{
463 da85ccfb Gerd Hoffmann
    KBDState *s = &(DO_UPCAST(ISAKBDState, dev, dev)->kbd);
464 da85ccfb Gerd Hoffmann
465 2e15e23b Gerd Hoffmann
    isa_init_irq(dev, &s->irq_kbd, 1);
466 2e15e23b Gerd Hoffmann
    isa_init_irq(dev, &s->irq_mouse, 12);
467 da85ccfb Gerd Hoffmann
468 86c86157 Gerd Hoffmann
    register_ioport_read(0x60, 1, 1, kbd_read_data, s);
469 86c86157 Gerd Hoffmann
    register_ioport_write(0x60, 1, 1, kbd_write_data, s);
470 86c86157 Gerd Hoffmann
    register_ioport_read(0x64, 1, 1, kbd_read_status, s);
471 86c86157 Gerd Hoffmann
    register_ioport_write(0x64, 1, 1, kbd_write_command, s);
472 956a3e6b Blue Swirl
    register_ioport_read(0x92, 1, 1, ioport92_read, s);
473 956a3e6b Blue Swirl
    register_ioport_write(0x92, 1, 1, ioport92_write, s);
474 da85ccfb Gerd Hoffmann
475 da85ccfb Gerd Hoffmann
    s->kbd = ps2_kbd_init(kbd_update_kbd_irq, s);
476 da85ccfb Gerd Hoffmann
    s->mouse = ps2_mouse_init(kbd_update_aux_irq, s);
477 da85ccfb Gerd Hoffmann
    qemu_register_reset(kbd_reset, s);
478 81a322d4 Gerd Hoffmann
    return 0;
479 da85ccfb Gerd Hoffmann
}
480 da85ccfb Gerd Hoffmann
481 da85ccfb Gerd Hoffmann
static ISADeviceInfo i8042_info = {
482 da85ccfb Gerd Hoffmann
    .qdev.name     = "i8042",
483 da85ccfb Gerd Hoffmann
    .qdev.size     = sizeof(ISAKBDState),
484 be73cfe2 Juan Quintela
    .qdev.vmsd     = &vmstate_kbd_isa,
485 da85ccfb Gerd Hoffmann
    .qdev.no_user  = 1,
486 da85ccfb Gerd Hoffmann
    .init          = i8042_initfn,
487 da85ccfb Gerd Hoffmann
};
488 da85ccfb Gerd Hoffmann
489 da85ccfb Gerd Hoffmann
static void i8042_register(void)
490 da85ccfb Gerd Hoffmann
{
491 da85ccfb Gerd Hoffmann
    isa_qdev_register(&i8042_info);
492 da85ccfb Gerd Hoffmann
}
493 da85ccfb Gerd Hoffmann
device_init(i8042_register)