xref: /qemu/hw/input/pckbd.c (revision d80fe99d)
149ab747fSPaolo Bonzini /*
249ab747fSPaolo Bonzini  * QEMU PC keyboard emulation
349ab747fSPaolo Bonzini  *
449ab747fSPaolo Bonzini  * Copyright (c) 2003 Fabrice Bellard
549ab747fSPaolo Bonzini  *
649ab747fSPaolo Bonzini  * Permission is hereby granted, free of charge, to any person obtaining a copy
749ab747fSPaolo Bonzini  * of this software and associated documentation files (the "Software"), to deal
849ab747fSPaolo Bonzini  * in the Software without restriction, including without limitation the rights
949ab747fSPaolo Bonzini  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1049ab747fSPaolo Bonzini  * copies of the Software, and to permit persons to whom the Software is
1149ab747fSPaolo Bonzini  * furnished to do so, subject to the following conditions:
1249ab747fSPaolo Bonzini  *
1349ab747fSPaolo Bonzini  * The above copyright notice and this permission notice shall be included in
1449ab747fSPaolo Bonzini  * all copies or substantial portions of the Software.
1549ab747fSPaolo Bonzini  *
1649ab747fSPaolo Bonzini  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1749ab747fSPaolo Bonzini  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1849ab747fSPaolo Bonzini  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
1949ab747fSPaolo Bonzini  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2049ab747fSPaolo Bonzini  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2149ab747fSPaolo Bonzini  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2249ab747fSPaolo Bonzini  * THE SOFTWARE.
2349ab747fSPaolo Bonzini  */
240430891cSPeter Maydell #include "qemu/osdep.h"
2549ab747fSPaolo Bonzini #include "hw/hw.h"
2649ab747fSPaolo Bonzini #include "hw/isa/isa.h"
2749ab747fSPaolo Bonzini #include "hw/i386/pc.h"
2849ab747fSPaolo Bonzini #include "hw/input/ps2.h"
2949ab747fSPaolo Bonzini #include "sysemu/sysemu.h"
3049ab747fSPaolo Bonzini 
3149ab747fSPaolo Bonzini /* debug PC keyboard */
3249ab747fSPaolo Bonzini //#define DEBUG_KBD
3349ab747fSPaolo Bonzini #ifdef DEBUG_KBD
3449ab747fSPaolo Bonzini #define DPRINTF(fmt, ...)                                       \
3549ab747fSPaolo Bonzini     do { printf("KBD: " fmt , ## __VA_ARGS__); } while (0)
3649ab747fSPaolo Bonzini #else
3749ab747fSPaolo Bonzini #define DPRINTF(fmt, ...)
3849ab747fSPaolo Bonzini #endif
3949ab747fSPaolo Bonzini 
4049ab747fSPaolo Bonzini /*	Keyboard Controller Commands */
4149ab747fSPaolo Bonzini #define KBD_CCMD_READ_MODE	0x20	/* Read mode bits */
4249ab747fSPaolo Bonzini #define KBD_CCMD_WRITE_MODE	0x60	/* Write mode bits */
4349ab747fSPaolo Bonzini #define KBD_CCMD_GET_VERSION	0xA1	/* Get controller version */
4449ab747fSPaolo Bonzini #define KBD_CCMD_MOUSE_DISABLE	0xA7	/* Disable mouse interface */
4549ab747fSPaolo Bonzini #define KBD_CCMD_MOUSE_ENABLE	0xA8	/* Enable mouse interface */
4649ab747fSPaolo Bonzini #define KBD_CCMD_TEST_MOUSE	0xA9	/* Mouse interface test */
4749ab747fSPaolo Bonzini #define KBD_CCMD_SELF_TEST	0xAA	/* Controller self test */
4849ab747fSPaolo Bonzini #define KBD_CCMD_KBD_TEST	0xAB	/* Keyboard interface test */
4949ab747fSPaolo Bonzini #define KBD_CCMD_KBD_DISABLE	0xAD	/* Keyboard interface disable */
5049ab747fSPaolo Bonzini #define KBD_CCMD_KBD_ENABLE	0xAE	/* Keyboard interface enable */
5149ab747fSPaolo Bonzini #define KBD_CCMD_READ_INPORT    0xC0    /* read input port */
5249ab747fSPaolo Bonzini #define KBD_CCMD_READ_OUTPORT	0xD0    /* read output port */
5349ab747fSPaolo Bonzini #define KBD_CCMD_WRITE_OUTPORT	0xD1    /* write output port */
5449ab747fSPaolo Bonzini #define KBD_CCMD_WRITE_OBUF	0xD2
5549ab747fSPaolo Bonzini #define KBD_CCMD_WRITE_AUX_OBUF	0xD3    /* Write to output buffer as if
5649ab747fSPaolo Bonzini 					   initiated by the auxiliary device */
5749ab747fSPaolo Bonzini #define KBD_CCMD_WRITE_MOUSE	0xD4	/* Write the following byte to the mouse */
5849ab747fSPaolo Bonzini #define KBD_CCMD_DISABLE_A20    0xDD    /* HP vectra only ? */
5949ab747fSPaolo Bonzini #define KBD_CCMD_ENABLE_A20     0xDF    /* HP vectra only ? */
6049ab747fSPaolo Bonzini #define KBD_CCMD_PULSE_BITS_3_0 0xF0    /* Pulse bits 3-0 of the output port P2. */
6149ab747fSPaolo Bonzini #define KBD_CCMD_RESET          0xFE    /* Pulse bit 0 of the output port P2 = CPU reset. */
6249ab747fSPaolo Bonzini #define KBD_CCMD_NO_OP          0xFF    /* Pulse no bits of the output port P2. */
6349ab747fSPaolo Bonzini 
6449ab747fSPaolo Bonzini /* Keyboard Commands */
6549ab747fSPaolo Bonzini #define KBD_CMD_SET_LEDS	0xED	/* Set keyboard leds */
6649ab747fSPaolo Bonzini #define KBD_CMD_ECHO     	0xEE
6749ab747fSPaolo Bonzini #define KBD_CMD_GET_ID 	        0xF2	/* get keyboard ID */
6849ab747fSPaolo Bonzini #define KBD_CMD_SET_RATE	0xF3	/* Set typematic rate */
6949ab747fSPaolo Bonzini #define KBD_CMD_ENABLE		0xF4	/* Enable scanning */
7049ab747fSPaolo Bonzini #define KBD_CMD_RESET_DISABLE	0xF5	/* reset and disable scanning */
7149ab747fSPaolo Bonzini #define KBD_CMD_RESET_ENABLE   	0xF6    /* reset and enable scanning */
7249ab747fSPaolo Bonzini #define KBD_CMD_RESET		0xFF	/* Reset */
7349ab747fSPaolo Bonzini 
7449ab747fSPaolo Bonzini /* Keyboard Replies */
7549ab747fSPaolo Bonzini #define KBD_REPLY_POR		0xAA	/* Power on reset */
7649ab747fSPaolo Bonzini #define KBD_REPLY_ACK		0xFA	/* Command ACK */
7749ab747fSPaolo Bonzini #define KBD_REPLY_RESEND	0xFE	/* Command NACK, send the cmd again */
7849ab747fSPaolo Bonzini 
7949ab747fSPaolo Bonzini /* Status Register Bits */
8049ab747fSPaolo Bonzini #define KBD_STAT_OBF 		0x01	/* Keyboard output buffer full */
8149ab747fSPaolo Bonzini #define KBD_STAT_IBF 		0x02	/* Keyboard input buffer full */
8249ab747fSPaolo Bonzini #define KBD_STAT_SELFTEST	0x04	/* Self test successful */
8349ab747fSPaolo Bonzini #define KBD_STAT_CMD		0x08	/* Last write was a command write (0=data) */
8449ab747fSPaolo Bonzini #define KBD_STAT_UNLOCKED	0x10	/* Zero if keyboard locked */
8549ab747fSPaolo Bonzini #define KBD_STAT_MOUSE_OBF	0x20	/* Mouse output buffer full */
8649ab747fSPaolo Bonzini #define KBD_STAT_GTO 		0x40	/* General receive/xmit timeout */
8749ab747fSPaolo Bonzini #define KBD_STAT_PERR 		0x80	/* Parity error */
8849ab747fSPaolo Bonzini 
8949ab747fSPaolo Bonzini /* Controller Mode Register Bits */
9049ab747fSPaolo Bonzini #define KBD_MODE_KBD_INT	0x01	/* Keyboard data generate IRQ1 */
9149ab747fSPaolo Bonzini #define KBD_MODE_MOUSE_INT	0x02	/* Mouse data generate IRQ12 */
9249ab747fSPaolo Bonzini #define KBD_MODE_SYS 		0x04	/* The system flag (?) */
9349ab747fSPaolo Bonzini #define KBD_MODE_NO_KEYLOCK	0x08	/* The keylock doesn't affect the keyboard if set */
9449ab747fSPaolo Bonzini #define KBD_MODE_DISABLE_KBD	0x10	/* Disable keyboard interface */
9549ab747fSPaolo Bonzini #define KBD_MODE_DISABLE_MOUSE	0x20	/* Disable mouse interface */
9649ab747fSPaolo Bonzini #define KBD_MODE_KCC 		0x40	/* Scan code conversion to PC format */
9749ab747fSPaolo Bonzini #define KBD_MODE_RFU		0x80
9849ab747fSPaolo Bonzini 
9949ab747fSPaolo Bonzini /* Output Port Bits */
10049ab747fSPaolo Bonzini #define KBD_OUT_RESET           0x01    /* 1=normal mode, 0=reset */
10149ab747fSPaolo Bonzini #define KBD_OUT_A20             0x02    /* x86 only */
10249ab747fSPaolo Bonzini #define KBD_OUT_OBF             0x10    /* Keyboard output buffer full */
10349ab747fSPaolo Bonzini #define KBD_OUT_MOUSE_OBF       0x20    /* Mouse output buffer full */
10449ab747fSPaolo Bonzini 
105d13c0404SPaolo Bonzini /* OSes typically write 0xdd/0xdf to turn the A20 line off and on.
106d13c0404SPaolo Bonzini  * We make the default value of the outport include these four bits,
107d13c0404SPaolo Bonzini  * so that the subsection is rarely necessary.
108d13c0404SPaolo Bonzini  */
109d13c0404SPaolo Bonzini #define KBD_OUT_ONES            0xcc
110d13c0404SPaolo Bonzini 
11149ab747fSPaolo Bonzini /* Mouse Commands */
11249ab747fSPaolo Bonzini #define AUX_SET_SCALE11		0xE6	/* Set 1:1 scaling */
11349ab747fSPaolo Bonzini #define AUX_SET_SCALE21		0xE7	/* Set 2:1 scaling */
11449ab747fSPaolo Bonzini #define AUX_SET_RES		0xE8	/* Set resolution */
11549ab747fSPaolo Bonzini #define AUX_GET_SCALE		0xE9	/* Get scaling factor */
11649ab747fSPaolo Bonzini #define AUX_SET_STREAM		0xEA	/* Set stream mode */
11749ab747fSPaolo Bonzini #define AUX_POLL		0xEB	/* Poll */
11849ab747fSPaolo Bonzini #define AUX_RESET_WRAP		0xEC	/* Reset wrap mode */
11949ab747fSPaolo Bonzini #define AUX_SET_WRAP		0xEE	/* Set wrap mode */
12049ab747fSPaolo Bonzini #define AUX_SET_REMOTE		0xF0	/* Set remote mode */
12149ab747fSPaolo Bonzini #define AUX_GET_TYPE		0xF2	/* Get type */
12249ab747fSPaolo Bonzini #define AUX_SET_SAMPLE		0xF3	/* Set sample rate */
12349ab747fSPaolo Bonzini #define AUX_ENABLE_DEV		0xF4	/* Enable aux device */
12449ab747fSPaolo Bonzini #define AUX_DISABLE_DEV		0xF5	/* Disable aux device */
12549ab747fSPaolo Bonzini #define AUX_SET_DEFAULT		0xF6
12649ab747fSPaolo Bonzini #define AUX_RESET		0xFF	/* Reset aux device */
12749ab747fSPaolo Bonzini #define AUX_ACK			0xFA	/* Command byte ACK. */
12849ab747fSPaolo Bonzini 
12949ab747fSPaolo Bonzini #define MOUSE_STATUS_REMOTE     0x40
13049ab747fSPaolo Bonzini #define MOUSE_STATUS_ENABLED    0x20
13149ab747fSPaolo Bonzini #define MOUSE_STATUS_SCALE21    0x10
13249ab747fSPaolo Bonzini 
13349ab747fSPaolo Bonzini #define KBD_PENDING_KBD         1
13449ab747fSPaolo Bonzini #define KBD_PENDING_AUX         2
13549ab747fSPaolo Bonzini 
13649ab747fSPaolo Bonzini typedef struct KBDState {
13749ab747fSPaolo Bonzini     uint8_t write_cmd; /* if non zero, write data to port 60 is expected */
13849ab747fSPaolo Bonzini     uint8_t status;
13949ab747fSPaolo Bonzini     uint8_t mode;
14049ab747fSPaolo Bonzini     uint8_t outport;
141a28fe7e3SPavel Dovgalyuk     bool outport_present;
14249ab747fSPaolo Bonzini     /* Bitmask of devices with data available.  */
14349ab747fSPaolo Bonzini     uint8_t pending;
14449ab747fSPaolo Bonzini     void *kbd;
14549ab747fSPaolo Bonzini     void *mouse;
14649ab747fSPaolo Bonzini 
14749ab747fSPaolo Bonzini     qemu_irq irq_kbd;
14849ab747fSPaolo Bonzini     qemu_irq irq_mouse;
1493115b9e2SEfimov Vasily     qemu_irq a20_out;
15049ab747fSPaolo Bonzini     hwaddr mask;
15149ab747fSPaolo Bonzini } KBDState;
15249ab747fSPaolo Bonzini 
15349ab747fSPaolo Bonzini /* update irq and KBD_STAT_[MOUSE_]OBF */
15449ab747fSPaolo Bonzini /* XXX: not generating the irqs if KBD_MODE_DISABLE_KBD is set may be
15549ab747fSPaolo Bonzini    incorrect, but it avoids having to simulate exact delays */
15649ab747fSPaolo Bonzini static void kbd_update_irq(KBDState *s)
15749ab747fSPaolo Bonzini {
15849ab747fSPaolo Bonzini     int irq_kbd_level, irq_mouse_level;
15949ab747fSPaolo Bonzini 
16049ab747fSPaolo Bonzini     irq_kbd_level = 0;
16149ab747fSPaolo Bonzini     irq_mouse_level = 0;
16249ab747fSPaolo Bonzini     s->status &= ~(KBD_STAT_OBF | KBD_STAT_MOUSE_OBF);
16349ab747fSPaolo Bonzini     s->outport &= ~(KBD_OUT_OBF | KBD_OUT_MOUSE_OBF);
16449ab747fSPaolo Bonzini     if (s->pending) {
16549ab747fSPaolo Bonzini         s->status |= KBD_STAT_OBF;
16649ab747fSPaolo Bonzini         s->outport |= KBD_OUT_OBF;
16749ab747fSPaolo Bonzini         /* kbd data takes priority over aux data.  */
16849ab747fSPaolo Bonzini         if (s->pending == KBD_PENDING_AUX) {
16949ab747fSPaolo Bonzini             s->status |= KBD_STAT_MOUSE_OBF;
17049ab747fSPaolo Bonzini             s->outport |= KBD_OUT_MOUSE_OBF;
17149ab747fSPaolo Bonzini             if (s->mode & KBD_MODE_MOUSE_INT)
17249ab747fSPaolo Bonzini                 irq_mouse_level = 1;
17349ab747fSPaolo Bonzini         } else {
17449ab747fSPaolo Bonzini             if ((s->mode & KBD_MODE_KBD_INT) &&
17549ab747fSPaolo Bonzini                 !(s->mode & KBD_MODE_DISABLE_KBD))
17649ab747fSPaolo Bonzini                 irq_kbd_level = 1;
17749ab747fSPaolo Bonzini         }
17849ab747fSPaolo Bonzini     }
17949ab747fSPaolo Bonzini     qemu_set_irq(s->irq_kbd, irq_kbd_level);
18049ab747fSPaolo Bonzini     qemu_set_irq(s->irq_mouse, irq_mouse_level);
18149ab747fSPaolo Bonzini }
18249ab747fSPaolo Bonzini 
18349ab747fSPaolo Bonzini static void kbd_update_kbd_irq(void *opaque, int level)
18449ab747fSPaolo Bonzini {
18549ab747fSPaolo Bonzini     KBDState *s = (KBDState *)opaque;
18649ab747fSPaolo Bonzini 
18749ab747fSPaolo Bonzini     if (level)
18849ab747fSPaolo Bonzini         s->pending |= KBD_PENDING_KBD;
18949ab747fSPaolo Bonzini     else
19049ab747fSPaolo Bonzini         s->pending &= ~KBD_PENDING_KBD;
19149ab747fSPaolo Bonzini     kbd_update_irq(s);
19249ab747fSPaolo Bonzini }
19349ab747fSPaolo Bonzini 
19449ab747fSPaolo Bonzini static void kbd_update_aux_irq(void *opaque, int level)
19549ab747fSPaolo Bonzini {
19649ab747fSPaolo Bonzini     KBDState *s = (KBDState *)opaque;
19749ab747fSPaolo Bonzini 
19849ab747fSPaolo Bonzini     if (level)
19949ab747fSPaolo Bonzini         s->pending |= KBD_PENDING_AUX;
20049ab747fSPaolo Bonzini     else
20149ab747fSPaolo Bonzini         s->pending &= ~KBD_PENDING_AUX;
20249ab747fSPaolo Bonzini     kbd_update_irq(s);
20349ab747fSPaolo Bonzini }
20449ab747fSPaolo Bonzini 
20549ab747fSPaolo Bonzini static uint64_t kbd_read_status(void *opaque, hwaddr addr,
20649ab747fSPaolo Bonzini                                 unsigned size)
20749ab747fSPaolo Bonzini {
20849ab747fSPaolo Bonzini     KBDState *s = opaque;
20949ab747fSPaolo Bonzini     int val;
21049ab747fSPaolo Bonzini     val = s->status;
21149ab747fSPaolo Bonzini     DPRINTF("kbd: read status=0x%02x\n", val);
21249ab747fSPaolo Bonzini     return val;
21349ab747fSPaolo Bonzini }
21449ab747fSPaolo Bonzini 
21549ab747fSPaolo Bonzini static void kbd_queue(KBDState *s, int b, int aux)
21649ab747fSPaolo Bonzini {
21749ab747fSPaolo Bonzini     if (aux)
21849ab747fSPaolo Bonzini         ps2_queue(s->mouse, b);
21949ab747fSPaolo Bonzini     else
22049ab747fSPaolo Bonzini         ps2_queue(s->kbd, b);
22149ab747fSPaolo Bonzini }
22249ab747fSPaolo Bonzini 
22349ab747fSPaolo Bonzini static void outport_write(KBDState *s, uint32_t val)
22449ab747fSPaolo Bonzini {
22549ab747fSPaolo Bonzini     DPRINTF("kbd: write outport=0x%02x\n", val);
22649ab747fSPaolo Bonzini     s->outport = val;
2273115b9e2SEfimov Vasily     qemu_set_irq(s->a20_out, (val >> 1) & 1);
22849ab747fSPaolo Bonzini     if (!(val & 1)) {
22949ab747fSPaolo Bonzini         qemu_system_reset_request();
23049ab747fSPaolo Bonzini     }
23149ab747fSPaolo Bonzini }
23249ab747fSPaolo Bonzini 
23349ab747fSPaolo Bonzini static void kbd_write_command(void *opaque, hwaddr addr,
23449ab747fSPaolo Bonzini                               uint64_t val, unsigned size)
23549ab747fSPaolo Bonzini {
23649ab747fSPaolo Bonzini     KBDState *s = opaque;
23749ab747fSPaolo Bonzini 
238c5539cb4SGonglei     DPRINTF("kbd: write cmd=0x%02" PRIx64 "\n", val);
23949ab747fSPaolo Bonzini 
24049ab747fSPaolo Bonzini     /* Bits 3-0 of the output port P2 of the keyboard controller may be pulsed
24149ab747fSPaolo Bonzini      * low for approximately 6 micro seconds. Bits 3-0 of the KBD_CCMD_PULSE
24249ab747fSPaolo Bonzini      * command specify the output port bits to be pulsed.
24349ab747fSPaolo Bonzini      * 0: Bit should be pulsed. 1: Bit should not be modified.
24449ab747fSPaolo Bonzini      * The only useful version of this command is pulsing bit 0,
24549ab747fSPaolo Bonzini      * which does a CPU reset.
24649ab747fSPaolo Bonzini      */
24749ab747fSPaolo Bonzini     if((val & KBD_CCMD_PULSE_BITS_3_0) == KBD_CCMD_PULSE_BITS_3_0) {
24849ab747fSPaolo Bonzini         if(!(val & 1))
24949ab747fSPaolo Bonzini             val = KBD_CCMD_RESET;
25049ab747fSPaolo Bonzini         else
25149ab747fSPaolo Bonzini             val = KBD_CCMD_NO_OP;
25249ab747fSPaolo Bonzini     }
25349ab747fSPaolo Bonzini 
25449ab747fSPaolo Bonzini     switch(val) {
25549ab747fSPaolo Bonzini     case KBD_CCMD_READ_MODE:
25649ab747fSPaolo Bonzini         kbd_queue(s, s->mode, 0);
25749ab747fSPaolo Bonzini         break;
25849ab747fSPaolo Bonzini     case KBD_CCMD_WRITE_MODE:
25949ab747fSPaolo Bonzini     case KBD_CCMD_WRITE_OBUF:
26049ab747fSPaolo Bonzini     case KBD_CCMD_WRITE_AUX_OBUF:
26149ab747fSPaolo Bonzini     case KBD_CCMD_WRITE_MOUSE:
26249ab747fSPaolo Bonzini     case KBD_CCMD_WRITE_OUTPORT:
26349ab747fSPaolo Bonzini         s->write_cmd = val;
26449ab747fSPaolo Bonzini         break;
26549ab747fSPaolo Bonzini     case KBD_CCMD_MOUSE_DISABLE:
26649ab747fSPaolo Bonzini         s->mode |= KBD_MODE_DISABLE_MOUSE;
26749ab747fSPaolo Bonzini         break;
26849ab747fSPaolo Bonzini     case KBD_CCMD_MOUSE_ENABLE:
26949ab747fSPaolo Bonzini         s->mode &= ~KBD_MODE_DISABLE_MOUSE;
27049ab747fSPaolo Bonzini         break;
27149ab747fSPaolo Bonzini     case KBD_CCMD_TEST_MOUSE:
27249ab747fSPaolo Bonzini         kbd_queue(s, 0x00, 0);
27349ab747fSPaolo Bonzini         break;
27449ab747fSPaolo Bonzini     case KBD_CCMD_SELF_TEST:
27549ab747fSPaolo Bonzini         s->status |= KBD_STAT_SELFTEST;
27649ab747fSPaolo Bonzini         kbd_queue(s, 0x55, 0);
27749ab747fSPaolo Bonzini         break;
27849ab747fSPaolo Bonzini     case KBD_CCMD_KBD_TEST:
27949ab747fSPaolo Bonzini         kbd_queue(s, 0x00, 0);
28049ab747fSPaolo Bonzini         break;
28149ab747fSPaolo Bonzini     case KBD_CCMD_KBD_DISABLE:
28249ab747fSPaolo Bonzini         s->mode |= KBD_MODE_DISABLE_KBD;
28349ab747fSPaolo Bonzini         kbd_update_irq(s);
28449ab747fSPaolo Bonzini         break;
28549ab747fSPaolo Bonzini     case KBD_CCMD_KBD_ENABLE:
28649ab747fSPaolo Bonzini         s->mode &= ~KBD_MODE_DISABLE_KBD;
28749ab747fSPaolo Bonzini         kbd_update_irq(s);
28849ab747fSPaolo Bonzini         break;
28949ab747fSPaolo Bonzini     case KBD_CCMD_READ_INPORT:
290f1b7e0e4SHervé Poussineau         kbd_queue(s, 0x80, 0);
29149ab747fSPaolo Bonzini         break;
29249ab747fSPaolo Bonzini     case KBD_CCMD_READ_OUTPORT:
29349ab747fSPaolo Bonzini         kbd_queue(s, s->outport, 0);
29449ab747fSPaolo Bonzini         break;
29549ab747fSPaolo Bonzini     case KBD_CCMD_ENABLE_A20:
2963115b9e2SEfimov Vasily         qemu_irq_raise(s->a20_out);
29749ab747fSPaolo Bonzini         s->outport |= KBD_OUT_A20;
29849ab747fSPaolo Bonzini         break;
29949ab747fSPaolo Bonzini     case KBD_CCMD_DISABLE_A20:
3003115b9e2SEfimov Vasily         qemu_irq_lower(s->a20_out);
30149ab747fSPaolo Bonzini         s->outport &= ~KBD_OUT_A20;
30249ab747fSPaolo Bonzini         break;
30349ab747fSPaolo Bonzini     case KBD_CCMD_RESET:
30449ab747fSPaolo Bonzini         qemu_system_reset_request();
30549ab747fSPaolo Bonzini         break;
30649ab747fSPaolo Bonzini     case KBD_CCMD_NO_OP:
30749ab747fSPaolo Bonzini         /* ignore that */
30849ab747fSPaolo Bonzini         break;
30949ab747fSPaolo Bonzini     default:
31049ab747fSPaolo Bonzini         fprintf(stderr, "qemu: unsupported keyboard cmd=0x%02x\n", (int)val);
31149ab747fSPaolo Bonzini         break;
31249ab747fSPaolo Bonzini     }
31349ab747fSPaolo Bonzini }
31449ab747fSPaolo Bonzini 
31549ab747fSPaolo Bonzini static uint64_t kbd_read_data(void *opaque, hwaddr addr,
31649ab747fSPaolo Bonzini                               unsigned size)
31749ab747fSPaolo Bonzini {
31849ab747fSPaolo Bonzini     KBDState *s = opaque;
31949ab747fSPaolo Bonzini     uint32_t val;
32049ab747fSPaolo Bonzini 
32149ab747fSPaolo Bonzini     if (s->pending == KBD_PENDING_AUX)
32249ab747fSPaolo Bonzini         val = ps2_read_data(s->mouse);
32349ab747fSPaolo Bonzini     else
32449ab747fSPaolo Bonzini         val = ps2_read_data(s->kbd);
32549ab747fSPaolo Bonzini 
32649ab747fSPaolo Bonzini     DPRINTF("kbd: read data=0x%02x\n", val);
32749ab747fSPaolo Bonzini     return val;
32849ab747fSPaolo Bonzini }
32949ab747fSPaolo Bonzini 
33049ab747fSPaolo Bonzini static void kbd_write_data(void *opaque, hwaddr addr,
33149ab747fSPaolo Bonzini                            uint64_t val, unsigned size)
33249ab747fSPaolo Bonzini {
33349ab747fSPaolo Bonzini     KBDState *s = opaque;
33449ab747fSPaolo Bonzini 
335c5539cb4SGonglei     DPRINTF("kbd: write data=0x%02" PRIx64 "\n", val);
33649ab747fSPaolo Bonzini 
33749ab747fSPaolo Bonzini     switch(s->write_cmd) {
33849ab747fSPaolo Bonzini     case 0:
33949ab747fSPaolo Bonzini         ps2_write_keyboard(s->kbd, val);
34049ab747fSPaolo Bonzini         break;
34149ab747fSPaolo Bonzini     case KBD_CCMD_WRITE_MODE:
34249ab747fSPaolo Bonzini         s->mode = val;
34349ab747fSPaolo Bonzini         ps2_keyboard_set_translation(s->kbd, (s->mode & KBD_MODE_KCC) != 0);
34449ab747fSPaolo Bonzini         /* ??? */
34549ab747fSPaolo Bonzini         kbd_update_irq(s);
34649ab747fSPaolo Bonzini         break;
34749ab747fSPaolo Bonzini     case KBD_CCMD_WRITE_OBUF:
34849ab747fSPaolo Bonzini         kbd_queue(s, val, 0);
34949ab747fSPaolo Bonzini         break;
35049ab747fSPaolo Bonzini     case KBD_CCMD_WRITE_AUX_OBUF:
35149ab747fSPaolo Bonzini         kbd_queue(s, val, 1);
35249ab747fSPaolo Bonzini         break;
35349ab747fSPaolo Bonzini     case KBD_CCMD_WRITE_OUTPORT:
35449ab747fSPaolo Bonzini         outport_write(s, val);
35549ab747fSPaolo Bonzini         break;
35649ab747fSPaolo Bonzini     case KBD_CCMD_WRITE_MOUSE:
35749ab747fSPaolo Bonzini         ps2_write_mouse(s->mouse, val);
35849ab747fSPaolo Bonzini         break;
35949ab747fSPaolo Bonzini     default:
36049ab747fSPaolo Bonzini         break;
36149ab747fSPaolo Bonzini     }
36249ab747fSPaolo Bonzini     s->write_cmd = 0;
36349ab747fSPaolo Bonzini }
36449ab747fSPaolo Bonzini 
36549ab747fSPaolo Bonzini static void kbd_reset(void *opaque)
36649ab747fSPaolo Bonzini {
36749ab747fSPaolo Bonzini     KBDState *s = opaque;
36849ab747fSPaolo Bonzini 
36949ab747fSPaolo Bonzini     s->mode = KBD_MODE_KBD_INT | KBD_MODE_MOUSE_INT;
37049ab747fSPaolo Bonzini     s->status = KBD_STAT_CMD | KBD_STAT_UNLOCKED;
371d13c0404SPaolo Bonzini     s->outport = KBD_OUT_RESET | KBD_OUT_A20 | KBD_OUT_ONES;
372a28fe7e3SPavel Dovgalyuk     s->outport_present = false;
373a28fe7e3SPavel Dovgalyuk }
374a28fe7e3SPavel Dovgalyuk 
375a28fe7e3SPavel Dovgalyuk static uint8_t kbd_outport_default(KBDState *s)
376a28fe7e3SPavel Dovgalyuk {
377d13c0404SPaolo Bonzini     return KBD_OUT_RESET | KBD_OUT_A20 | KBD_OUT_ONES
378a28fe7e3SPavel Dovgalyuk            | (s->status & KBD_STAT_OBF ? KBD_OUT_OBF : 0)
379a28fe7e3SPavel Dovgalyuk            | (s->status & KBD_STAT_MOUSE_OBF ? KBD_OUT_MOUSE_OBF : 0);
380a28fe7e3SPavel Dovgalyuk }
381a28fe7e3SPavel Dovgalyuk 
382a28fe7e3SPavel Dovgalyuk static int kbd_outport_post_load(void *opaque, int version_id)
383a28fe7e3SPavel Dovgalyuk {
384a28fe7e3SPavel Dovgalyuk     KBDState *s = opaque;
385a28fe7e3SPavel Dovgalyuk     s->outport_present = true;
386a28fe7e3SPavel Dovgalyuk     return 0;
387a28fe7e3SPavel Dovgalyuk }
388a28fe7e3SPavel Dovgalyuk 
389a28fe7e3SPavel Dovgalyuk static bool kbd_outport_needed(void *opaque)
390a28fe7e3SPavel Dovgalyuk {
391a28fe7e3SPavel Dovgalyuk     KBDState *s = opaque;
392a28fe7e3SPavel Dovgalyuk     return s->outport != kbd_outport_default(s);
393a28fe7e3SPavel Dovgalyuk }
394a28fe7e3SPavel Dovgalyuk 
3955cd8cadaSJuan Quintela static const VMStateDescription vmstate_kbd_outport = {
3965cd8cadaSJuan Quintela     .name = "pckbd_outport",
3975cd8cadaSJuan Quintela     .version_id = 1,
3985cd8cadaSJuan Quintela     .minimum_version_id = 1,
3995cd8cadaSJuan Quintela     .post_load = kbd_outport_post_load,
4005cd8cadaSJuan Quintela     .needed = kbd_outport_needed,
4015cd8cadaSJuan Quintela     .fields = (VMStateField[]) {
4025cd8cadaSJuan Quintela         VMSTATE_UINT8(outport, KBDState),
4035cd8cadaSJuan Quintela         VMSTATE_END_OF_LIST()
4045cd8cadaSJuan Quintela     }
4055cd8cadaSJuan Quintela };
4065cd8cadaSJuan Quintela 
407a28fe7e3SPavel Dovgalyuk static int kbd_post_load(void *opaque, int version_id)
408a28fe7e3SPavel Dovgalyuk {
409a28fe7e3SPavel Dovgalyuk     KBDState *s = opaque;
410a28fe7e3SPavel Dovgalyuk     if (!s->outport_present) {
411a28fe7e3SPavel Dovgalyuk         s->outport = kbd_outport_default(s);
412a28fe7e3SPavel Dovgalyuk     }
413a28fe7e3SPavel Dovgalyuk     s->outport_present = false;
414a28fe7e3SPavel Dovgalyuk     return 0;
41549ab747fSPaolo Bonzini }
41649ab747fSPaolo Bonzini 
41749ab747fSPaolo Bonzini static const VMStateDescription vmstate_kbd = {
41849ab747fSPaolo Bonzini     .name = "pckbd",
41949ab747fSPaolo Bonzini     .version_id = 3,
42049ab747fSPaolo Bonzini     .minimum_version_id = 3,
421a28fe7e3SPavel Dovgalyuk     .post_load = kbd_post_load,
42249ab747fSPaolo Bonzini     .fields = (VMStateField[]) {
42349ab747fSPaolo Bonzini         VMSTATE_UINT8(write_cmd, KBDState),
42449ab747fSPaolo Bonzini         VMSTATE_UINT8(status, KBDState),
42549ab747fSPaolo Bonzini         VMSTATE_UINT8(mode, KBDState),
42649ab747fSPaolo Bonzini         VMSTATE_UINT8(pending, KBDState),
42749ab747fSPaolo Bonzini         VMSTATE_END_OF_LIST()
428a28fe7e3SPavel Dovgalyuk     },
4295cd8cadaSJuan Quintela     .subsections = (const VMStateDescription*[]) {
4305cd8cadaSJuan Quintela         &vmstate_kbd_outport,
4315cd8cadaSJuan Quintela         NULL
43249ab747fSPaolo Bonzini     }
43349ab747fSPaolo Bonzini };
43449ab747fSPaolo Bonzini 
43549ab747fSPaolo Bonzini /* Memory mapped interface */
43649ab747fSPaolo Bonzini static uint32_t kbd_mm_readb (void *opaque, hwaddr addr)
43749ab747fSPaolo Bonzini {
43849ab747fSPaolo Bonzini     KBDState *s = opaque;
43949ab747fSPaolo Bonzini 
44049ab747fSPaolo Bonzini     if (addr & s->mask)
44149ab747fSPaolo Bonzini         return kbd_read_status(s, 0, 1) & 0xff;
44249ab747fSPaolo Bonzini     else
44349ab747fSPaolo Bonzini         return kbd_read_data(s, 0, 1) & 0xff;
44449ab747fSPaolo Bonzini }
44549ab747fSPaolo Bonzini 
44649ab747fSPaolo Bonzini static void kbd_mm_writeb (void *opaque, hwaddr addr, uint32_t value)
44749ab747fSPaolo Bonzini {
44849ab747fSPaolo Bonzini     KBDState *s = opaque;
44949ab747fSPaolo Bonzini 
45049ab747fSPaolo Bonzini     if (addr & s->mask)
45149ab747fSPaolo Bonzini         kbd_write_command(s, 0, value & 0xff, 1);
45249ab747fSPaolo Bonzini     else
45349ab747fSPaolo Bonzini         kbd_write_data(s, 0, value & 0xff, 1);
45449ab747fSPaolo Bonzini }
45549ab747fSPaolo Bonzini 
45649ab747fSPaolo Bonzini static const MemoryRegionOps i8042_mmio_ops = {
45749ab747fSPaolo Bonzini     .endianness = DEVICE_NATIVE_ENDIAN,
45849ab747fSPaolo Bonzini     .old_mmio = {
45949ab747fSPaolo Bonzini         .read = { kbd_mm_readb, kbd_mm_readb, kbd_mm_readb },
46049ab747fSPaolo Bonzini         .write = { kbd_mm_writeb, kbd_mm_writeb, kbd_mm_writeb },
46149ab747fSPaolo Bonzini     },
46249ab747fSPaolo Bonzini };
46349ab747fSPaolo Bonzini 
46449ab747fSPaolo Bonzini void i8042_mm_init(qemu_irq kbd_irq, qemu_irq mouse_irq,
46549ab747fSPaolo Bonzini                    MemoryRegion *region, ram_addr_t size,
46649ab747fSPaolo Bonzini                    hwaddr mask)
46749ab747fSPaolo Bonzini {
46849ab747fSPaolo Bonzini     KBDState *s = g_malloc0(sizeof(KBDState));
46949ab747fSPaolo Bonzini 
47049ab747fSPaolo Bonzini     s->irq_kbd = kbd_irq;
47149ab747fSPaolo Bonzini     s->irq_mouse = mouse_irq;
47249ab747fSPaolo Bonzini     s->mask = mask;
47349ab747fSPaolo Bonzini 
47449ab747fSPaolo Bonzini     vmstate_register(NULL, 0, &vmstate_kbd, s);
47549ab747fSPaolo Bonzini 
4762c9b15caSPaolo Bonzini     memory_region_init_io(region, NULL, &i8042_mmio_ops, s, "i8042", size);
47749ab747fSPaolo Bonzini 
47849ab747fSPaolo Bonzini     s->kbd = ps2_kbd_init(kbd_update_kbd_irq, s);
47949ab747fSPaolo Bonzini     s->mouse = ps2_mouse_init(kbd_update_aux_irq, s);
48049ab747fSPaolo Bonzini     qemu_register_reset(kbd_reset, s);
48149ab747fSPaolo Bonzini }
48249ab747fSPaolo Bonzini 
483a2e0b863SAndreas Färber #define TYPE_I8042 "i8042"
484a2e0b863SAndreas Färber #define I8042(obj) OBJECT_CHECK(ISAKBDState, (obj), TYPE_I8042)
485a2e0b863SAndreas Färber 
48649ab747fSPaolo Bonzini typedef struct ISAKBDState {
487a2e0b863SAndreas Färber     ISADevice parent_obj;
488a2e0b863SAndreas Färber 
48949ab747fSPaolo Bonzini     KBDState kbd;
49049ab747fSPaolo Bonzini     MemoryRegion io[2];
49149ab747fSPaolo Bonzini } ISAKBDState;
49249ab747fSPaolo Bonzini 
49349ab747fSPaolo Bonzini void i8042_isa_mouse_fake_event(void *opaque)
49449ab747fSPaolo Bonzini {
49549ab747fSPaolo Bonzini     ISADevice *dev = opaque;
496a2e0b863SAndreas Färber     ISAKBDState *isa = I8042(dev);
497a2e0b863SAndreas Färber     KBDState *s = &isa->kbd;
49849ab747fSPaolo Bonzini 
49949ab747fSPaolo Bonzini     ps2_mouse_fake_event(s->mouse);
50049ab747fSPaolo Bonzini }
50149ab747fSPaolo Bonzini 
502*d80fe99dSMarc-André Lureau void i8042_setup_a20_line(ISADevice *dev, qemu_irq a20_out)
50349ab747fSPaolo Bonzini {
504*d80fe99dSMarc-André Lureau     qdev_connect_gpio_out_named(DEVICE(dev), I8042_A20_LINE, 0, a20_out);
50549ab747fSPaolo Bonzini }
50649ab747fSPaolo Bonzini 
50749ab747fSPaolo Bonzini static const VMStateDescription vmstate_kbd_isa = {
50849ab747fSPaolo Bonzini     .name = "pckbd",
50949ab747fSPaolo Bonzini     .version_id = 3,
51049ab747fSPaolo Bonzini     .minimum_version_id = 3,
51149ab747fSPaolo Bonzini     .fields = (VMStateField[]) {
51249ab747fSPaolo Bonzini         VMSTATE_STRUCT(kbd, ISAKBDState, 0, vmstate_kbd, KBDState),
51349ab747fSPaolo Bonzini         VMSTATE_END_OF_LIST()
51449ab747fSPaolo Bonzini     }
51549ab747fSPaolo Bonzini };
51649ab747fSPaolo Bonzini 
51749ab747fSPaolo Bonzini static const MemoryRegionOps i8042_data_ops = {
51849ab747fSPaolo Bonzini     .read = kbd_read_data,
51949ab747fSPaolo Bonzini     .write = kbd_write_data,
52049ab747fSPaolo Bonzini     .impl = {
52149ab747fSPaolo Bonzini         .min_access_size = 1,
52249ab747fSPaolo Bonzini         .max_access_size = 1,
52349ab747fSPaolo Bonzini     },
52449ab747fSPaolo Bonzini     .endianness = DEVICE_LITTLE_ENDIAN,
52549ab747fSPaolo Bonzini };
52649ab747fSPaolo Bonzini 
52749ab747fSPaolo Bonzini static const MemoryRegionOps i8042_cmd_ops = {
52849ab747fSPaolo Bonzini     .read = kbd_read_status,
52949ab747fSPaolo Bonzini     .write = kbd_write_command,
53049ab747fSPaolo Bonzini     .impl = {
53149ab747fSPaolo Bonzini         .min_access_size = 1,
53249ab747fSPaolo Bonzini         .max_access_size = 1,
53349ab747fSPaolo Bonzini     },
53449ab747fSPaolo Bonzini     .endianness = DEVICE_LITTLE_ENDIAN,
53549ab747fSPaolo Bonzini };
53649ab747fSPaolo Bonzini 
537db895a1eSAndreas Färber static void i8042_initfn(Object *obj)
53849ab747fSPaolo Bonzini {
539db895a1eSAndreas Färber     ISAKBDState *isa_s = I8042(obj);
540db895a1eSAndreas Färber     KBDState *s = &isa_s->kbd;
541db895a1eSAndreas Färber 
5421437c94bSPaolo Bonzini     memory_region_init_io(isa_s->io + 0, obj, &i8042_data_ops, s,
5431437c94bSPaolo Bonzini                           "i8042-data", 1);
5441437c94bSPaolo Bonzini     memory_region_init_io(isa_s->io + 1, obj, &i8042_cmd_ops, s,
5451437c94bSPaolo Bonzini                           "i8042-cmd", 1);
5463115b9e2SEfimov Vasily 
5473115b9e2SEfimov Vasily     qdev_init_gpio_out_named(DEVICE(obj), &s->a20_out, I8042_A20_LINE, 1);
548db895a1eSAndreas Färber }
549db895a1eSAndreas Färber 
550db895a1eSAndreas Färber static void i8042_realizefn(DeviceState *dev, Error **errp)
551db895a1eSAndreas Färber {
552db895a1eSAndreas Färber     ISADevice *isadev = ISA_DEVICE(dev);
553a2e0b863SAndreas Färber     ISAKBDState *isa_s = I8042(dev);
55449ab747fSPaolo Bonzini     KBDState *s = &isa_s->kbd;
55549ab747fSPaolo Bonzini 
556db895a1eSAndreas Färber     isa_init_irq(isadev, &s->irq_kbd, 1);
557db895a1eSAndreas Färber     isa_init_irq(isadev, &s->irq_mouse, 12);
55849ab747fSPaolo Bonzini 
559db895a1eSAndreas Färber     isa_register_ioport(isadev, isa_s->io + 0, 0x60);
560db895a1eSAndreas Färber     isa_register_ioport(isadev, isa_s->io + 1, 0x64);
56149ab747fSPaolo Bonzini 
56249ab747fSPaolo Bonzini     s->kbd = ps2_kbd_init(kbd_update_kbd_irq, s);
56349ab747fSPaolo Bonzini     s->mouse = ps2_mouse_init(kbd_update_aux_irq, s);
56449ab747fSPaolo Bonzini     qemu_register_reset(kbd_reset, s);
56549ab747fSPaolo Bonzini }
56649ab747fSPaolo Bonzini 
56749ab747fSPaolo Bonzini static void i8042_class_initfn(ObjectClass *klass, void *data)
56849ab747fSPaolo Bonzini {
56949ab747fSPaolo Bonzini     DeviceClass *dc = DEVICE_CLASS(klass);
570db895a1eSAndreas Färber 
571db895a1eSAndreas Färber     dc->realize = i8042_realizefn;
57249ab747fSPaolo Bonzini     dc->vmsd = &vmstate_kbd_isa;
57349ab747fSPaolo Bonzini }
57449ab747fSPaolo Bonzini 
57549ab747fSPaolo Bonzini static const TypeInfo i8042_info = {
576a2e0b863SAndreas Färber     .name          = TYPE_I8042,
57749ab747fSPaolo Bonzini     .parent        = TYPE_ISA_DEVICE,
57849ab747fSPaolo Bonzini     .instance_size = sizeof(ISAKBDState),
579db895a1eSAndreas Färber     .instance_init = i8042_initfn,
58049ab747fSPaolo Bonzini     .class_init    = i8042_class_initfn,
58149ab747fSPaolo Bonzini };
58249ab747fSPaolo Bonzini 
58349ab747fSPaolo Bonzini static void i8042_register_types(void)
58449ab747fSPaolo Bonzini {
58549ab747fSPaolo Bonzini     type_register_static(&i8042_info);
58649ab747fSPaolo Bonzini }
58749ab747fSPaolo Bonzini 
58849ab747fSPaolo Bonzini type_init(i8042_register_types)
589