pckbd.c 17.7 KB
Newer Older
B
bellard 已提交
1 2
/*
 * QEMU PC keyboard emulation
3
 *
B
bellard 已提交
4
 * Copyright (c) 2003 Fabrice Bellard
5
 *
B
bellard 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
P
Peter Maydell 已提交
24
#include "qemu/osdep.h"
25
#include "qemu/log.h"
26
#include "hw/hw.h"
P
Paolo Bonzini 已提交
27 28 29
#include "hw/isa/isa.h"
#include "hw/i386/pc.h"
#include "hw/input/ps2.h"
30
#include "hw/input/i8042.h"
31
#include "sysemu/sysemu.h"
B
bellard 已提交
32 33 34

/* debug PC keyboard */
//#define DEBUG_KBD
B
Blue Swirl 已提交
35 36 37 38 39 40
#ifdef DEBUG_KBD
#define DPRINTF(fmt, ...)                                       \
    do { printf("KBD: " fmt , ## __VA_ARGS__); } while (0)
#else
#define DPRINTF(fmt, ...)
#endif
B
bellard 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

/*	Keyboard Controller Commands */
#define KBD_CCMD_READ_MODE	0x20	/* Read mode bits */
#define KBD_CCMD_WRITE_MODE	0x60	/* Write mode bits */
#define KBD_CCMD_GET_VERSION	0xA1	/* Get controller version */
#define KBD_CCMD_MOUSE_DISABLE	0xA7	/* Disable mouse interface */
#define KBD_CCMD_MOUSE_ENABLE	0xA8	/* Enable mouse interface */
#define KBD_CCMD_TEST_MOUSE	0xA9	/* Mouse interface test */
#define KBD_CCMD_SELF_TEST	0xAA	/* Controller self test */
#define KBD_CCMD_KBD_TEST	0xAB	/* Keyboard interface test */
#define KBD_CCMD_KBD_DISABLE	0xAD	/* Keyboard interface disable */
#define KBD_CCMD_KBD_ENABLE	0xAE	/* Keyboard interface enable */
#define KBD_CCMD_READ_INPORT    0xC0    /* read input port */
#define KBD_CCMD_READ_OUTPORT	0xD0    /* read output port */
#define KBD_CCMD_WRITE_OUTPORT	0xD1    /* write output port */
#define KBD_CCMD_WRITE_OBUF	0xD2
#define KBD_CCMD_WRITE_AUX_OBUF	0xD3    /* Write to output buffer as if
					   initiated by the auxiliary device */
#define KBD_CCMD_WRITE_MOUSE	0xD4	/* Write the following byte to the mouse */
#define KBD_CCMD_DISABLE_A20    0xDD    /* HP vectra only ? */
#define KBD_CCMD_ENABLE_A20     0xDF    /* HP vectra only ? */
62 63 64
#define KBD_CCMD_PULSE_BITS_3_0 0xF0    /* Pulse bits 3-0 of the output port P2. */
#define KBD_CCMD_RESET          0xFE    /* Pulse bit 0 of the output port P2 = CPU reset. */
#define KBD_CCMD_NO_OP          0xFF    /* Pulse no bits of the output port P2. */
B
bellard 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

/* Keyboard Commands */
#define KBD_CMD_SET_LEDS	0xED	/* Set keyboard leds */
#define KBD_CMD_ECHO     	0xEE
#define KBD_CMD_GET_ID 	        0xF2	/* get keyboard ID */
#define KBD_CMD_SET_RATE	0xF3	/* Set typematic rate */
#define KBD_CMD_ENABLE		0xF4	/* Enable scanning */
#define KBD_CMD_RESET_DISABLE	0xF5	/* reset and disable scanning */
#define KBD_CMD_RESET_ENABLE   	0xF6    /* reset and enable scanning */
#define KBD_CMD_RESET		0xFF	/* Reset */

/* Keyboard Replies */
#define KBD_REPLY_POR		0xAA	/* Power on reset */
#define KBD_REPLY_ACK		0xFA	/* Command ACK */
#define KBD_REPLY_RESEND	0xFE	/* Command NACK, send the cmd again */

/* Status Register Bits */
#define KBD_STAT_OBF 		0x01	/* Keyboard output buffer full */
#define KBD_STAT_IBF 		0x02	/* Keyboard input buffer full */
#define KBD_STAT_SELFTEST	0x04	/* Self test successful */
#define KBD_STAT_CMD		0x08	/* Last write was a command write (0=data) */
#define KBD_STAT_UNLOCKED	0x10	/* Zero if keyboard locked */
#define KBD_STAT_MOUSE_OBF	0x20	/* Mouse output buffer full */
#define KBD_STAT_GTO 		0x40	/* General receive/xmit timeout */
#define KBD_STAT_PERR 		0x80	/* Parity error */

/* Controller Mode Register Bits */
#define KBD_MODE_KBD_INT	0x01	/* Keyboard data generate IRQ1 */
#define KBD_MODE_MOUSE_INT	0x02	/* Mouse data generate IRQ12 */
#define KBD_MODE_SYS 		0x04	/* The system flag (?) */
#define KBD_MODE_NO_KEYLOCK	0x08	/* The keylock doesn't affect the keyboard if set */
#define KBD_MODE_DISABLE_KBD	0x10	/* Disable keyboard interface */
#define KBD_MODE_DISABLE_MOUSE	0x20	/* Disable mouse interface */
#define KBD_MODE_KCC 		0x40	/* Scan code conversion to PC format */
#define KBD_MODE_RFU		0x80

B
Blue Swirl 已提交
101 102 103 104 105 106
/* Output Port Bits */
#define KBD_OUT_RESET           0x01    /* 1=normal mode, 0=reset */
#define KBD_OUT_A20             0x02    /* x86 only */
#define KBD_OUT_OBF             0x10    /* Keyboard output buffer full */
#define KBD_OUT_MOUSE_OBF       0x20    /* Mouse output buffer full */

107 108 109 110 111 112
/* OSes typically write 0xdd/0xdf to turn the A20 line off and on.
 * We make the default value of the outport include these four bits,
 * so that the subsection is rarely necessary.
 */
#define KBD_OUT_ONES            0xcc

B
bellard 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
/* Mouse Commands */
#define AUX_SET_SCALE11		0xE6	/* Set 1:1 scaling */
#define AUX_SET_SCALE21		0xE7	/* Set 2:1 scaling */
#define AUX_SET_RES		0xE8	/* Set resolution */
#define AUX_GET_SCALE		0xE9	/* Get scaling factor */
#define AUX_SET_STREAM		0xEA	/* Set stream mode */
#define AUX_POLL		0xEB	/* Poll */
#define AUX_RESET_WRAP		0xEC	/* Reset wrap mode */
#define AUX_SET_WRAP		0xEE	/* Set wrap mode */
#define AUX_SET_REMOTE		0xF0	/* Set remote mode */
#define AUX_GET_TYPE		0xF2	/* Get type */
#define AUX_SET_SAMPLE		0xF3	/* Set sample rate */
#define AUX_ENABLE_DEV		0xF4	/* Enable aux device */
#define AUX_DISABLE_DEV		0xF5	/* Disable aux device */
#define AUX_SET_DEFAULT		0xF6
#define AUX_RESET		0xFF	/* Reset aux device */
#define AUX_ACK			0xFA	/* Command byte ACK. */

#define MOUSE_STATUS_REMOTE     0x40
#define MOUSE_STATUS_ENABLED    0x20
#define MOUSE_STATUS_SCALE21    0x10

135 136
#define KBD_PENDING_KBD         1
#define KBD_PENDING_AUX         2
B
bellard 已提交
137 138 139 140 141

typedef struct KBDState {
    uint8_t write_cmd; /* if non zero, write data to port 60 is expected */
    uint8_t status;
    uint8_t mode;
B
Blue Swirl 已提交
142
    uint8_t outport;
143
    bool outport_present;
144
    /* Bitmask of devices with data available.  */
P
pbrook 已提交
145
    uint8_t pending;
146 147
    void *kbd;
    void *mouse;
148

P
pbrook 已提交
149 150
    qemu_irq irq_kbd;
    qemu_irq irq_mouse;
E
Efimov Vasily 已提交
151
    qemu_irq a20_out;
A
Avi Kivity 已提交
152
    hwaddr mask;
B
bellard 已提交
153 154 155 156 157 158 159
} KBDState;

/* update irq and KBD_STAT_[MOUSE_]OBF */
/* XXX: not generating the irqs if KBD_MODE_DISABLE_KBD is set may be
   incorrect, but it avoids having to simulate exact delays */
static void kbd_update_irq(KBDState *s)
{
160
    int irq_kbd_level, irq_mouse_level;
B
bellard 已提交
161

162 163
    irq_kbd_level = 0;
    irq_mouse_level = 0;
B
bellard 已提交
164
    s->status &= ~(KBD_STAT_OBF | KBD_STAT_MOUSE_OBF);
B
Blue Swirl 已提交
165
    s->outport &= ~(KBD_OUT_OBF | KBD_OUT_MOUSE_OBF);
166
    if (s->pending) {
B
bellard 已提交
167
        s->status |= KBD_STAT_OBF;
B
Blue Swirl 已提交
168
        s->outport |= KBD_OUT_OBF;
169
        /* kbd data takes priority over aux data.  */
170
        if (s->pending == KBD_PENDING_AUX) {
B
bellard 已提交
171
            s->status |= KBD_STAT_MOUSE_OBF;
B
Blue Swirl 已提交
172
            s->outport |= KBD_OUT_MOUSE_OBF;
B
bellard 已提交
173
            if (s->mode & KBD_MODE_MOUSE_INT)
174
                irq_mouse_level = 1;
B
bellard 已提交
175
        } else {
176
            if ((s->mode & KBD_MODE_KBD_INT) &&
B
bellard 已提交
177
                !(s->mode & KBD_MODE_DISABLE_KBD))
178
                irq_kbd_level = 1;
B
bellard 已提交
179 180
        }
    }
P
pbrook 已提交
181 182
    qemu_set_irq(s->irq_kbd, irq_kbd_level);
    qemu_set_irq(s->irq_mouse, irq_mouse_level);
B
bellard 已提交
183 184
}

185
static void kbd_update_kbd_irq(void *opaque, int level)
B
bellard 已提交
186
{
187
    KBDState *s = (KBDState *)opaque;
B
bellard 已提交
188

189 190
    if (level)
        s->pending |= KBD_PENDING_KBD;
B
bellard 已提交
191
    else
192
        s->pending &= ~KBD_PENDING_KBD;
B
bellard 已提交
193 194 195
    kbd_update_irq(s);
}

196
static void kbd_update_aux_irq(void *opaque, int level)
B
bellard 已提交
197
{
198 199 200 201 202 203 204
    KBDState *s = (KBDState *)opaque;

    if (level)
        s->pending |= KBD_PENDING_AUX;
    else
        s->pending &= ~KBD_PENDING_AUX;
    kbd_update_irq(s);
B
bellard 已提交
205 206
}

207 208
static uint64_t kbd_read_status(void *opaque, hwaddr addr,
                                unsigned size)
B
bellard 已提交
209
{
B
bellard 已提交
210
    KBDState *s = opaque;
B
bellard 已提交
211 212
    int val;
    val = s->status;
B
Blue Swirl 已提交
213
    DPRINTF("kbd: read status=0x%02x\n", val);
B
bellard 已提交
214 215 216
    return val;
}

217 218 219 220 221 222 223 224
static void kbd_queue(KBDState *s, int b, int aux)
{
    if (aux)
        ps2_queue(s->mouse, b);
    else
        ps2_queue(s->kbd, b);
}

225
static void outport_write(KBDState *s, uint32_t val)
B
Blue Swirl 已提交
226
{
B
Blue Swirl 已提交
227
    DPRINTF("kbd: write outport=0x%02x\n", val);
B
Blue Swirl 已提交
228
    s->outport = val;
E
Efimov Vasily 已提交
229
    qemu_set_irq(s->a20_out, (val >> 1) & 1);
B
Blue Swirl 已提交
230
    if (!(val & 1)) {
231
        qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
B
Blue Swirl 已提交
232 233 234
    }
}

235 236
static void kbd_write_command(void *opaque, hwaddr addr,
                              uint64_t val, unsigned size)
B
bellard 已提交
237
{
B
bellard 已提交
238
    KBDState *s = opaque;
B
bellard 已提交
239

G
Gonglei 已提交
240
    DPRINTF("kbd: write cmd=0x%02" PRIx64 "\n", val);
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255

    /* Bits 3-0 of the output port P2 of the keyboard controller may be pulsed
     * low for approximately 6 micro seconds. Bits 3-0 of the KBD_CCMD_PULSE
     * command specify the output port bits to be pulsed.
     * 0: Bit should be pulsed. 1: Bit should not be modified.
     * The only useful version of this command is pulsing bit 0,
     * which does a CPU reset.
     */
    if((val & KBD_CCMD_PULSE_BITS_3_0) == KBD_CCMD_PULSE_BITS_3_0) {
        if(!(val & 1))
            val = KBD_CCMD_RESET;
        else
            val = KBD_CCMD_NO_OP;
    }

B
bellard 已提交
256 257
    switch(val) {
    case KBD_CCMD_READ_MODE:
258
        kbd_queue(s, s->mode, 0);
B
bellard 已提交
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
        break;
    case KBD_CCMD_WRITE_MODE:
    case KBD_CCMD_WRITE_OBUF:
    case KBD_CCMD_WRITE_AUX_OBUF:
    case KBD_CCMD_WRITE_MOUSE:
    case KBD_CCMD_WRITE_OUTPORT:
        s->write_cmd = val;
        break;
    case KBD_CCMD_MOUSE_DISABLE:
        s->mode |= KBD_MODE_DISABLE_MOUSE;
        break;
    case KBD_CCMD_MOUSE_ENABLE:
        s->mode &= ~KBD_MODE_DISABLE_MOUSE;
        break;
    case KBD_CCMD_TEST_MOUSE:
        kbd_queue(s, 0x00, 0);
        break;
    case KBD_CCMD_SELF_TEST:
        s->status |= KBD_STAT_SELFTEST;
        kbd_queue(s, 0x55, 0);
        break;
    case KBD_CCMD_KBD_TEST:
        kbd_queue(s, 0x00, 0);
        break;
    case KBD_CCMD_KBD_DISABLE:
        s->mode |= KBD_MODE_DISABLE_KBD;
        kbd_update_irq(s);
        break;
    case KBD_CCMD_KBD_ENABLE:
        s->mode &= ~KBD_MODE_DISABLE_KBD;
        kbd_update_irq(s);
        break;
    case KBD_CCMD_READ_INPORT:
292
        kbd_queue(s, 0x80, 0);
B
bellard 已提交
293 294
        break;
    case KBD_CCMD_READ_OUTPORT:
B
Blue Swirl 已提交
295
        kbd_queue(s, s->outport, 0);
B
bellard 已提交
296 297
        break;
    case KBD_CCMD_ENABLE_A20:
E
Efimov Vasily 已提交
298
        qemu_irq_raise(s->a20_out);
B
Blue Swirl 已提交
299
        s->outport |= KBD_OUT_A20;
B
bellard 已提交
300 301
        break;
    case KBD_CCMD_DISABLE_A20:
E
Efimov Vasily 已提交
302
        qemu_irq_lower(s->a20_out);
B
Blue Swirl 已提交
303
        s->outport &= ~KBD_OUT_A20;
B
bellard 已提交
304 305
        break;
    case KBD_CCMD_RESET:
306
        qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
B
bellard 已提交
307
        break;
308 309
    case KBD_CCMD_NO_OP:
        /* ignore that */
B
bellard 已提交
310 311
        break;
    default:
312 313
        qemu_log_mask(LOG_GUEST_ERROR,
                      "unsupported keyboard cmd=0x%02" PRIx64 "\n", val);
B
bellard 已提交
314 315 316 317
        break;
    }
}

318 319
static uint64_t kbd_read_data(void *opaque, hwaddr addr,
                              unsigned size)
B
bellard 已提交
320
{
B
bellard 已提交
321
    KBDState *s = opaque;
322
    uint32_t val;
B
bellard 已提交
323

324
    if (s->pending == KBD_PENDING_AUX)
325 326 327
        val = ps2_read_data(s->mouse);
    else
        val = ps2_read_data(s->kbd);
B
bellard 已提交
328

B
Blue Swirl 已提交
329
    DPRINTF("kbd: read data=0x%02x\n", val);
330
    return val;
B
bellard 已提交
331 332
}

333 334
static void kbd_write_data(void *opaque, hwaddr addr,
                           uint64_t val, unsigned size)
B
bellard 已提交
335
{
B
bellard 已提交
336
    KBDState *s = opaque;
B
bellard 已提交
337

G
Gonglei 已提交
338
    DPRINTF("kbd: write data=0x%02" PRIx64 "\n", val);
B
bellard 已提交
339 340 341

    switch(s->write_cmd) {
    case 0:
342
        ps2_write_keyboard(s->kbd, val);
B
bellard 已提交
343 344 345
        break;
    case KBD_CCMD_WRITE_MODE:
        s->mode = val;
346
        ps2_keyboard_set_translation(s->kbd, (s->mode & KBD_MODE_KCC) != 0);
347
        /* ??? */
B
bellard 已提交
348 349 350 351 352 353 354 355 356
        kbd_update_irq(s);
        break;
    case KBD_CCMD_WRITE_OBUF:
        kbd_queue(s, val, 0);
        break;
    case KBD_CCMD_WRITE_AUX_OBUF:
        kbd_queue(s, val, 1);
        break;
    case KBD_CCMD_WRITE_OUTPORT:
357
        outport_write(s, val);
B
bellard 已提交
358 359
        break;
    case KBD_CCMD_WRITE_MOUSE:
360
        ps2_write_mouse(s->mouse, val);
B
bellard 已提交
361 362 363 364 365 366 367
        break;
    default:
        break;
    }
    s->write_cmd = 0;
}

B
bellard 已提交
368
static void kbd_reset(void *opaque)
B
bellard 已提交
369
{
B
bellard 已提交
370
    KBDState *s = opaque;
B
bellard 已提交
371 372 373

    s->mode = KBD_MODE_KBD_INT | KBD_MODE_MOUSE_INT;
    s->status = KBD_STAT_CMD | KBD_STAT_UNLOCKED;
374
    s->outport = KBD_OUT_RESET | KBD_OUT_A20 | KBD_OUT_ONES;
375 376 377 378 379
    s->outport_present = false;
}

static uint8_t kbd_outport_default(KBDState *s)
{
380
    return KBD_OUT_RESET | KBD_OUT_A20 | KBD_OUT_ONES
381 382 383 384 385 386 387 388 389 390 391
           | (s->status & KBD_STAT_OBF ? KBD_OUT_OBF : 0)
           | (s->status & KBD_STAT_MOUSE_OBF ? KBD_OUT_MOUSE_OBF : 0);
}

static int kbd_outport_post_load(void *opaque, int version_id)
{
    KBDState *s = opaque;
    s->outport_present = true;
    return 0;
}

392 393 394 395 396 397
static bool kbd_outport_needed(void *opaque)
{
    KBDState *s = opaque;
    return s->outport != kbd_outport_default(s);
}

398 399 400 401 402
static const VMStateDescription vmstate_kbd_outport = {
    .name = "pckbd_outport",
    .version_id = 1,
    .minimum_version_id = 1,
    .post_load = kbd_outport_post_load,
403
    .needed = kbd_outport_needed,
404 405 406 407 408 409 410 411 412 413 414 415 416 417
    .fields = (VMStateField[]) {
        VMSTATE_UINT8(outport, KBDState),
        VMSTATE_END_OF_LIST()
    }
};

static int kbd_post_load(void *opaque, int version_id)
{
    KBDState *s = opaque;
    if (!s->outport_present) {
        s->outport = kbd_outport_default(s);
    }
    s->outport_present = false;
    return 0;
B
bellard 已提交
418 419
}

J
Juan Quintela 已提交
420 421 422 423
static const VMStateDescription vmstate_kbd = {
    .name = "pckbd",
    .version_id = 3,
    .minimum_version_id = 3,
424
    .post_load = kbd_post_load,
425
    .fields = (VMStateField[]) {
J
Juan Quintela 已提交
426 427 428 429 430
        VMSTATE_UINT8(write_cmd, KBDState),
        VMSTATE_UINT8(status, KBDState),
        VMSTATE_UINT8(mode, KBDState),
        VMSTATE_UINT8(pending, KBDState),
        VMSTATE_END_OF_LIST()
431
    },
432 433 434
    .subsections = (const VMStateDescription*[]) {
        &vmstate_kbd_outport,
        NULL
J
Juan Quintela 已提交
435 436
    }
};
B
bellard 已提交
437

438
/* Memory mapped interface */
439
static uint64_t kbd_mm_readfn(void *opaque, hwaddr addr, unsigned size)
440 441 442
{
    KBDState *s = opaque;

A
aurel32 已提交
443
    if (addr & s->mask)
444
        return kbd_read_status(s, 0, 1) & 0xff;
A
aurel32 已提交
445
    else
446
        return kbd_read_data(s, 0, 1) & 0xff;
447 448
}

449 450
static void kbd_mm_writefn(void *opaque, hwaddr addr,
                           uint64_t value, unsigned size)
451 452 453
{
    KBDState *s = opaque;

A
aurel32 已提交
454
    if (addr & s->mask)
455
        kbd_write_command(s, 0, value & 0xff, 1);
A
aurel32 已提交
456
    else
457
        kbd_write_data(s, 0, value & 0xff, 1);
458 459
}

460

461
static const MemoryRegionOps i8042_mmio_ops = {
462 463 464 465
    .read = kbd_mm_readfn,
    .write = kbd_mm_writefn,
    .valid.min_access_size = 1,
    .valid.max_access_size = 4,
466
    .endianness = DEVICE_NATIVE_ENDIAN,
467 468
};

469
void i8042_mm_init(qemu_irq kbd_irq, qemu_irq mouse_irq,
470
                   MemoryRegion *region, ram_addr_t size,
A
Avi Kivity 已提交
471
                   hwaddr mask)
472
{
473
    KBDState *s = g_malloc0(sizeof(KBDState));
474 475 476

    s->irq_kbd = kbd_irq;
    s->irq_mouse = mouse_irq;
A
aurel32 已提交
477
    s->mask = mask;
478

A
Alex Williamson 已提交
479
    vmstate_register(NULL, 0, &vmstate_kbd, s);
480

481
    memory_region_init_io(region, NULL, &i8042_mmio_ops, s, "i8042", size);
482 483 484

    s->kbd = ps2_kbd_init(kbd_update_kbd_irq, s);
    s->mouse = ps2_mouse_init(kbd_update_aux_irq, s);
485
    qemu_register_reset(kbd_reset, s);
486
}
487

A
Andreas Färber 已提交
488 489
#define I8042(obj) OBJECT_CHECK(ISAKBDState, (obj), TYPE_I8042)

490
typedef struct ISAKBDState {
A
Andreas Färber 已提交
491 492
    ISADevice parent_obj;

493 494
    KBDState kbd;
    MemoryRegion io[2];
495 496
} ISAKBDState;

B
Blue Swirl 已提交
497 498 499
void i8042_isa_mouse_fake_event(void *opaque)
{
    ISADevice *dev = opaque;
A
Andreas Färber 已提交
500 501
    ISAKBDState *isa = I8042(dev);
    KBDState *s = &isa->kbd;
B
Blue Swirl 已提交
502 503 504 505

    ps2_mouse_fake_event(s->mouse);
}

506
void i8042_setup_a20_line(ISADevice *dev, qemu_irq a20_out)
B
Blue Swirl 已提交
507
{
508
    qdev_connect_gpio_out_named(DEVICE(dev), I8042_A20_LINE, 0, a20_out);
B
Blue Swirl 已提交
509 510
}

B
Blue Swirl 已提交
511
static const VMStateDescription vmstate_kbd_isa = {
512 513 514
    .name = "pckbd",
    .version_id = 3,
    .minimum_version_id = 3,
515
    .fields = (VMStateField[]) {
516 517 518 519 520
        VMSTATE_STRUCT(kbd, ISAKBDState, 0, vmstate_kbd, KBDState),
        VMSTATE_END_OF_LIST()
    }
};

521
static const MemoryRegionOps i8042_data_ops = {
522 523 524 525 526 527 528
    .read = kbd_read_data,
    .write = kbd_write_data,
    .impl = {
        .min_access_size = 1,
        .max_access_size = 1,
    },
    .endianness = DEVICE_LITTLE_ENDIAN,
529 530 531
};

static const MemoryRegionOps i8042_cmd_ops = {
532 533 534 535 536 537 538
    .read = kbd_read_status,
    .write = kbd_write_command,
    .impl = {
        .min_access_size = 1,
        .max_access_size = 1,
    },
    .endianness = DEVICE_LITTLE_ENDIAN,
539 540
};

541
static void i8042_initfn(Object *obj)
542
{
543
    ISAKBDState *isa_s = I8042(obj);
544
    KBDState *s = &isa_s->kbd;
545

546 547 548 549
    memory_region_init_io(isa_s->io + 0, obj, &i8042_data_ops, s,
                          "i8042-data", 1);
    memory_region_init_io(isa_s->io + 1, obj, &i8042_cmd_ops, s,
                          "i8042-cmd", 1);
E
Efimov Vasily 已提交
550 551

    qdev_init_gpio_out_named(DEVICE(obj), &s->a20_out, I8042_A20_LINE, 1);
552 553 554 555 556 557 558 559 560 561 562 563 564
}

static void i8042_realizefn(DeviceState *dev, Error **errp)
{
    ISADevice *isadev = ISA_DEVICE(dev);
    ISAKBDState *isa_s = I8042(dev);
    KBDState *s = &isa_s->kbd;

    isa_init_irq(isadev, &s->irq_kbd, 1);
    isa_init_irq(isadev, &s->irq_mouse, 12);

    isa_register_ioport(isadev, isa_s->io + 0, 0x60);
    isa_register_ioport(isadev, isa_s->io + 1, 0x64);
565 566 567 568 569 570

    s->kbd = ps2_kbd_init(kbd_update_kbd_irq, s);
    s->mouse = ps2_mouse_init(kbd_update_aux_irq, s);
    qemu_register_reset(kbd_reset, s);
}

571 572
static void i8042_class_initfn(ObjectClass *klass, void *data)
{
573
    DeviceClass *dc = DEVICE_CLASS(klass);
574 575

    dc->realize = i8042_realizefn;
576
    dc->vmsd = &vmstate_kbd_isa;
577 578
}

579
static const TypeInfo i8042_info = {
A
Andreas Färber 已提交
580
    .name          = TYPE_I8042,
581 582
    .parent        = TYPE_ISA_DEVICE,
    .instance_size = sizeof(ISAKBDState),
583
    .instance_init = i8042_initfn,
584
    .class_init    = i8042_class_initfn,
585 586
};

A
Andreas Färber 已提交
587
static void i8042_register_types(void)
588
{
589
    type_register_static(&i8042_info);
590
}
A
Andreas Färber 已提交
591 592

type_init(i8042_register_types)