xref: /qemu/hw/input/pckbd.c (revision d13c0404092eb46e548754a47a808da1bb8d4fd0)
180cabfadSbellard /*
280cabfadSbellard  * QEMU PC keyboard emulation
380cabfadSbellard  *
480cabfadSbellard  * Copyright (c) 2003 Fabrice Bellard
580cabfadSbellard  *
680cabfadSbellard  * Permission is hereby granted, free of charge, to any person obtaining a copy
780cabfadSbellard  * of this software and associated documentation files (the "Software"), to deal
880cabfadSbellard  * in the Software without restriction, including without limitation the rights
980cabfadSbellard  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1080cabfadSbellard  * copies of the Software, and to permit persons to whom the Software is
1180cabfadSbellard  * furnished to do so, subject to the following conditions:
1280cabfadSbellard  *
1380cabfadSbellard  * The above copyright notice and this permission notice shall be included in
1480cabfadSbellard  * all copies or substantial portions of the Software.
1580cabfadSbellard  *
1680cabfadSbellard  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1780cabfadSbellard  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1880cabfadSbellard  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
1980cabfadSbellard  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2080cabfadSbellard  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2180cabfadSbellard  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2280cabfadSbellard  * THE SOFTWARE.
2380cabfadSbellard  */
2483c9f4caSPaolo Bonzini #include "hw/hw.h"
250d09e41aSPaolo Bonzini #include "hw/isa/isa.h"
260d09e41aSPaolo Bonzini #include "hw/i386/pc.h"
270d09e41aSPaolo Bonzini #include "hw/input/ps2.h"
289c17d615SPaolo Bonzini #include "sysemu/sysemu.h"
2980cabfadSbellard 
3080cabfadSbellard /* debug PC keyboard */
3180cabfadSbellard //#define DEBUG_KBD
32c86d2c23SBlue Swirl #ifdef DEBUG_KBD
33c86d2c23SBlue Swirl #define DPRINTF(fmt, ...)                                       \
34c86d2c23SBlue Swirl     do { printf("KBD: " fmt , ## __VA_ARGS__); } while (0)
35c86d2c23SBlue Swirl #else
36c86d2c23SBlue Swirl #define DPRINTF(fmt, ...)
37c86d2c23SBlue Swirl #endif
3880cabfadSbellard 
3980cabfadSbellard /*	Keyboard Controller Commands */
4080cabfadSbellard #define KBD_CCMD_READ_MODE	0x20	/* Read mode bits */
4180cabfadSbellard #define KBD_CCMD_WRITE_MODE	0x60	/* Write mode bits */
4280cabfadSbellard #define KBD_CCMD_GET_VERSION	0xA1	/* Get controller version */
4380cabfadSbellard #define KBD_CCMD_MOUSE_DISABLE	0xA7	/* Disable mouse interface */
4480cabfadSbellard #define KBD_CCMD_MOUSE_ENABLE	0xA8	/* Enable mouse interface */
4580cabfadSbellard #define KBD_CCMD_TEST_MOUSE	0xA9	/* Mouse interface test */
4680cabfadSbellard #define KBD_CCMD_SELF_TEST	0xAA	/* Controller self test */
4780cabfadSbellard #define KBD_CCMD_KBD_TEST	0xAB	/* Keyboard interface test */
4880cabfadSbellard #define KBD_CCMD_KBD_DISABLE	0xAD	/* Keyboard interface disable */
4980cabfadSbellard #define KBD_CCMD_KBD_ENABLE	0xAE	/* Keyboard interface enable */
5080cabfadSbellard #define KBD_CCMD_READ_INPORT    0xC0    /* read input port */
5180cabfadSbellard #define KBD_CCMD_READ_OUTPORT	0xD0    /* read output port */
5280cabfadSbellard #define KBD_CCMD_WRITE_OUTPORT	0xD1    /* write output port */
5380cabfadSbellard #define KBD_CCMD_WRITE_OBUF	0xD2
5480cabfadSbellard #define KBD_CCMD_WRITE_AUX_OBUF	0xD3    /* Write to output buffer as if
5580cabfadSbellard 					   initiated by the auxiliary device */
5680cabfadSbellard #define KBD_CCMD_WRITE_MOUSE	0xD4	/* Write the following byte to the mouse */
5780cabfadSbellard #define KBD_CCMD_DISABLE_A20    0xDD    /* HP vectra only ? */
5880cabfadSbellard #define KBD_CCMD_ENABLE_A20     0xDF    /* HP vectra only ? */
595ccaa4ceSBernhard Kohl #define KBD_CCMD_PULSE_BITS_3_0 0xF0    /* Pulse bits 3-0 of the output port P2. */
605ccaa4ceSBernhard Kohl #define KBD_CCMD_RESET          0xFE    /* Pulse bit 0 of the output port P2 = CPU reset. */
615ccaa4ceSBernhard Kohl #define KBD_CCMD_NO_OP          0xFF    /* Pulse no bits of the output port P2. */
6280cabfadSbellard 
6380cabfadSbellard /* Keyboard Commands */
6480cabfadSbellard #define KBD_CMD_SET_LEDS	0xED	/* Set keyboard leds */
6580cabfadSbellard #define KBD_CMD_ECHO     	0xEE
6680cabfadSbellard #define KBD_CMD_GET_ID 	        0xF2	/* get keyboard ID */
6780cabfadSbellard #define KBD_CMD_SET_RATE	0xF3	/* Set typematic rate */
6880cabfadSbellard #define KBD_CMD_ENABLE		0xF4	/* Enable scanning */
6980cabfadSbellard #define KBD_CMD_RESET_DISABLE	0xF5	/* reset and disable scanning */
7080cabfadSbellard #define KBD_CMD_RESET_ENABLE   	0xF6    /* reset and enable scanning */
7180cabfadSbellard #define KBD_CMD_RESET		0xFF	/* Reset */
7280cabfadSbellard 
7380cabfadSbellard /* Keyboard Replies */
7480cabfadSbellard #define KBD_REPLY_POR		0xAA	/* Power on reset */
7580cabfadSbellard #define KBD_REPLY_ACK		0xFA	/* Command ACK */
7680cabfadSbellard #define KBD_REPLY_RESEND	0xFE	/* Command NACK, send the cmd again */
7780cabfadSbellard 
7880cabfadSbellard /* Status Register Bits */
7980cabfadSbellard #define KBD_STAT_OBF 		0x01	/* Keyboard output buffer full */
8080cabfadSbellard #define KBD_STAT_IBF 		0x02	/* Keyboard input buffer full */
8180cabfadSbellard #define KBD_STAT_SELFTEST	0x04	/* Self test successful */
8280cabfadSbellard #define KBD_STAT_CMD		0x08	/* Last write was a command write (0=data) */
8380cabfadSbellard #define KBD_STAT_UNLOCKED	0x10	/* Zero if keyboard locked */
8480cabfadSbellard #define KBD_STAT_MOUSE_OBF	0x20	/* Mouse output buffer full */
8580cabfadSbellard #define KBD_STAT_GTO 		0x40	/* General receive/xmit timeout */
8680cabfadSbellard #define KBD_STAT_PERR 		0x80	/* Parity error */
8780cabfadSbellard 
8880cabfadSbellard /* Controller Mode Register Bits */
8980cabfadSbellard #define KBD_MODE_KBD_INT	0x01	/* Keyboard data generate IRQ1 */
9080cabfadSbellard #define KBD_MODE_MOUSE_INT	0x02	/* Mouse data generate IRQ12 */
9180cabfadSbellard #define KBD_MODE_SYS 		0x04	/* The system flag (?) */
9280cabfadSbellard #define KBD_MODE_NO_KEYLOCK	0x08	/* The keylock doesn't affect the keyboard if set */
9380cabfadSbellard #define KBD_MODE_DISABLE_KBD	0x10	/* Disable keyboard interface */
9480cabfadSbellard #define KBD_MODE_DISABLE_MOUSE	0x20	/* Disable mouse interface */
9580cabfadSbellard #define KBD_MODE_KCC 		0x40	/* Scan code conversion to PC format */
9680cabfadSbellard #define KBD_MODE_RFU		0x80
9780cabfadSbellard 
98956a3e6bSBlue Swirl /* Output Port Bits */
99956a3e6bSBlue Swirl #define KBD_OUT_RESET           0x01    /* 1=normal mode, 0=reset */
100956a3e6bSBlue Swirl #define KBD_OUT_A20             0x02    /* x86 only */
101956a3e6bSBlue Swirl #define KBD_OUT_OBF             0x10    /* Keyboard output buffer full */
102956a3e6bSBlue Swirl #define KBD_OUT_MOUSE_OBF       0x20    /* Mouse output buffer full */
103956a3e6bSBlue Swirl 
104*d13c0404SPaolo Bonzini /* OSes typically write 0xdd/0xdf to turn the A20 line off and on.
105*d13c0404SPaolo Bonzini  * We make the default value of the outport include these four bits,
106*d13c0404SPaolo Bonzini  * so that the subsection is rarely necessary.
107*d13c0404SPaolo Bonzini  */
108*d13c0404SPaolo Bonzini #define KBD_OUT_ONES            0xcc
109*d13c0404SPaolo Bonzini 
11080cabfadSbellard /* Mouse Commands */
11180cabfadSbellard #define AUX_SET_SCALE11		0xE6	/* Set 1:1 scaling */
11280cabfadSbellard #define AUX_SET_SCALE21		0xE7	/* Set 2:1 scaling */
11380cabfadSbellard #define AUX_SET_RES		0xE8	/* Set resolution */
11480cabfadSbellard #define AUX_GET_SCALE		0xE9	/* Get scaling factor */
11580cabfadSbellard #define AUX_SET_STREAM		0xEA	/* Set stream mode */
11680cabfadSbellard #define AUX_POLL		0xEB	/* Poll */
11780cabfadSbellard #define AUX_RESET_WRAP		0xEC	/* Reset wrap mode */
11880cabfadSbellard #define AUX_SET_WRAP		0xEE	/* Set wrap mode */
11980cabfadSbellard #define AUX_SET_REMOTE		0xF0	/* Set remote mode */
12080cabfadSbellard #define AUX_GET_TYPE		0xF2	/* Get type */
12180cabfadSbellard #define AUX_SET_SAMPLE		0xF3	/* Set sample rate */
12280cabfadSbellard #define AUX_ENABLE_DEV		0xF4	/* Enable aux device */
12380cabfadSbellard #define AUX_DISABLE_DEV		0xF5	/* Disable aux device */
12480cabfadSbellard #define AUX_SET_DEFAULT		0xF6
12580cabfadSbellard #define AUX_RESET		0xFF	/* Reset aux device */
12680cabfadSbellard #define AUX_ACK			0xFA	/* Command byte ACK. */
12780cabfadSbellard 
12880cabfadSbellard #define MOUSE_STATUS_REMOTE     0x40
12980cabfadSbellard #define MOUSE_STATUS_ENABLED    0x20
13080cabfadSbellard #define MOUSE_STATUS_SCALE21    0x10
13180cabfadSbellard 
132daa57963Sbellard #define KBD_PENDING_KBD         1
133daa57963Sbellard #define KBD_PENDING_AUX         2
13480cabfadSbellard 
13580cabfadSbellard typedef struct KBDState {
13680cabfadSbellard     uint8_t write_cmd; /* if non zero, write data to port 60 is expected */
13780cabfadSbellard     uint8_t status;
13880cabfadSbellard     uint8_t mode;
139956a3e6bSBlue Swirl     uint8_t outport;
140a28fe7e3SPavel Dovgalyuk     bool outport_present;
141daa57963Sbellard     /* Bitmask of devices with data available.  */
1427783e9f0Spbrook     uint8_t pending;
143daa57963Sbellard     void *kbd;
144daa57963Sbellard     void *mouse;
145b7678d96Sths 
146d537cf6cSpbrook     qemu_irq irq_kbd;
147d537cf6cSpbrook     qemu_irq irq_mouse;
148956a3e6bSBlue Swirl     qemu_irq *a20_out;
149a8170e5eSAvi Kivity     hwaddr mask;
15080cabfadSbellard } KBDState;
15180cabfadSbellard 
15280cabfadSbellard /* update irq and KBD_STAT_[MOUSE_]OBF */
15380cabfadSbellard /* XXX: not generating the irqs if KBD_MODE_DISABLE_KBD is set may be
15480cabfadSbellard    incorrect, but it avoids having to simulate exact delays */
15580cabfadSbellard static void kbd_update_irq(KBDState *s)
15680cabfadSbellard {
157b7678d96Sths     int irq_kbd_level, irq_mouse_level;
15880cabfadSbellard 
159b7678d96Sths     irq_kbd_level = 0;
160b7678d96Sths     irq_mouse_level = 0;
16180cabfadSbellard     s->status &= ~(KBD_STAT_OBF | KBD_STAT_MOUSE_OBF);
162956a3e6bSBlue Swirl     s->outport &= ~(KBD_OUT_OBF | KBD_OUT_MOUSE_OBF);
163daa57963Sbellard     if (s->pending) {
16480cabfadSbellard         s->status |= KBD_STAT_OBF;
165956a3e6bSBlue Swirl         s->outport |= KBD_OUT_OBF;
166b92bb99bSths         /* kbd data takes priority over aux data.  */
167daa57963Sbellard         if (s->pending == KBD_PENDING_AUX) {
16880cabfadSbellard             s->status |= KBD_STAT_MOUSE_OBF;
169956a3e6bSBlue Swirl             s->outport |= KBD_OUT_MOUSE_OBF;
17080cabfadSbellard             if (s->mode & KBD_MODE_MOUSE_INT)
171b7678d96Sths                 irq_mouse_level = 1;
17280cabfadSbellard         } else {
17380cabfadSbellard             if ((s->mode & KBD_MODE_KBD_INT) &&
17480cabfadSbellard                 !(s->mode & KBD_MODE_DISABLE_KBD))
175b7678d96Sths                 irq_kbd_level = 1;
17680cabfadSbellard         }
17780cabfadSbellard     }
178d537cf6cSpbrook     qemu_set_irq(s->irq_kbd, irq_kbd_level);
179d537cf6cSpbrook     qemu_set_irq(s->irq_mouse, irq_mouse_level);
18080cabfadSbellard }
18180cabfadSbellard 
182daa57963Sbellard static void kbd_update_kbd_irq(void *opaque, int level)
18380cabfadSbellard {
184daa57963Sbellard     KBDState *s = (KBDState *)opaque;
18580cabfadSbellard 
186daa57963Sbellard     if (level)
187daa57963Sbellard         s->pending |= KBD_PENDING_KBD;
18880cabfadSbellard     else
189daa57963Sbellard         s->pending &= ~KBD_PENDING_KBD;
19080cabfadSbellard     kbd_update_irq(s);
19180cabfadSbellard }
19280cabfadSbellard 
193daa57963Sbellard static void kbd_update_aux_irq(void *opaque, int level)
19480cabfadSbellard {
195daa57963Sbellard     KBDState *s = (KBDState *)opaque;
196daa57963Sbellard 
197daa57963Sbellard     if (level)
198daa57963Sbellard         s->pending |= KBD_PENDING_AUX;
199daa57963Sbellard     else
200daa57963Sbellard         s->pending &= ~KBD_PENDING_AUX;
201daa57963Sbellard     kbd_update_irq(s);
20280cabfadSbellard }
20380cabfadSbellard 
204d540bfe0SAlexander Graf static uint64_t kbd_read_status(void *opaque, hwaddr addr,
205d540bfe0SAlexander Graf                                 unsigned size)
20680cabfadSbellard {
207b41a2cd1Sbellard     KBDState *s = opaque;
20880cabfadSbellard     int val;
20980cabfadSbellard     val = s->status;
210c86d2c23SBlue Swirl     DPRINTF("kbd: read status=0x%02x\n", val);
21180cabfadSbellard     return val;
21280cabfadSbellard }
21380cabfadSbellard 
214daa57963Sbellard static void kbd_queue(KBDState *s, int b, int aux)
215daa57963Sbellard {
216daa57963Sbellard     if (aux)
217daa57963Sbellard         ps2_queue(s->mouse, b);
218daa57963Sbellard     else
219daa57963Sbellard         ps2_queue(s->kbd, b);
220daa57963Sbellard }
221daa57963Sbellard 
2224b78a802SBlue Swirl static void outport_write(KBDState *s, uint32_t val)
223956a3e6bSBlue Swirl {
224c86d2c23SBlue Swirl     DPRINTF("kbd: write outport=0x%02x\n", val);
225956a3e6bSBlue Swirl     s->outport = val;
226956a3e6bSBlue Swirl     if (s->a20_out) {
227956a3e6bSBlue Swirl         qemu_set_irq(*s->a20_out, (val >> 1) & 1);
228956a3e6bSBlue Swirl     }
229956a3e6bSBlue Swirl     if (!(val & 1)) {
230956a3e6bSBlue Swirl         qemu_system_reset_request();
231956a3e6bSBlue Swirl     }
232956a3e6bSBlue Swirl }
233956a3e6bSBlue Swirl 
234d540bfe0SAlexander Graf static void kbd_write_command(void *opaque, hwaddr addr,
235d540bfe0SAlexander Graf                               uint64_t val, unsigned size)
23680cabfadSbellard {
237b41a2cd1Sbellard     KBDState *s = opaque;
23880cabfadSbellard 
239c5539cb4SGonglei     DPRINTF("kbd: write cmd=0x%02" PRIx64 "\n", val);
2405ccaa4ceSBernhard Kohl 
2415ccaa4ceSBernhard Kohl     /* Bits 3-0 of the output port P2 of the keyboard controller may be pulsed
2425ccaa4ceSBernhard Kohl      * low for approximately 6 micro seconds. Bits 3-0 of the KBD_CCMD_PULSE
2435ccaa4ceSBernhard Kohl      * command specify the output port bits to be pulsed.
2445ccaa4ceSBernhard Kohl      * 0: Bit should be pulsed. 1: Bit should not be modified.
2455ccaa4ceSBernhard Kohl      * The only useful version of this command is pulsing bit 0,
2465ccaa4ceSBernhard Kohl      * which does a CPU reset.
2475ccaa4ceSBernhard Kohl      */
2485ccaa4ceSBernhard Kohl     if((val & KBD_CCMD_PULSE_BITS_3_0) == KBD_CCMD_PULSE_BITS_3_0) {
2495ccaa4ceSBernhard Kohl         if(!(val & 1))
2505ccaa4ceSBernhard Kohl             val = KBD_CCMD_RESET;
2515ccaa4ceSBernhard Kohl         else
2525ccaa4ceSBernhard Kohl             val = KBD_CCMD_NO_OP;
2535ccaa4ceSBernhard Kohl     }
2545ccaa4ceSBernhard Kohl 
25580cabfadSbellard     switch(val) {
25680cabfadSbellard     case KBD_CCMD_READ_MODE:
257889bec69Sbalrog         kbd_queue(s, s->mode, 0);
25880cabfadSbellard         break;
25980cabfadSbellard     case KBD_CCMD_WRITE_MODE:
26080cabfadSbellard     case KBD_CCMD_WRITE_OBUF:
26180cabfadSbellard     case KBD_CCMD_WRITE_AUX_OBUF:
26280cabfadSbellard     case KBD_CCMD_WRITE_MOUSE:
26380cabfadSbellard     case KBD_CCMD_WRITE_OUTPORT:
26480cabfadSbellard         s->write_cmd = val;
26580cabfadSbellard         break;
26680cabfadSbellard     case KBD_CCMD_MOUSE_DISABLE:
26780cabfadSbellard         s->mode |= KBD_MODE_DISABLE_MOUSE;
26880cabfadSbellard         break;
26980cabfadSbellard     case KBD_CCMD_MOUSE_ENABLE:
27080cabfadSbellard         s->mode &= ~KBD_MODE_DISABLE_MOUSE;
27180cabfadSbellard         break;
27280cabfadSbellard     case KBD_CCMD_TEST_MOUSE:
27380cabfadSbellard         kbd_queue(s, 0x00, 0);
27480cabfadSbellard         break;
27580cabfadSbellard     case KBD_CCMD_SELF_TEST:
27680cabfadSbellard         s->status |= KBD_STAT_SELFTEST;
27780cabfadSbellard         kbd_queue(s, 0x55, 0);
27880cabfadSbellard         break;
27980cabfadSbellard     case KBD_CCMD_KBD_TEST:
28080cabfadSbellard         kbd_queue(s, 0x00, 0);
28180cabfadSbellard         break;
28280cabfadSbellard     case KBD_CCMD_KBD_DISABLE:
28380cabfadSbellard         s->mode |= KBD_MODE_DISABLE_KBD;
28480cabfadSbellard         kbd_update_irq(s);
28580cabfadSbellard         break;
28680cabfadSbellard     case KBD_CCMD_KBD_ENABLE:
28780cabfadSbellard         s->mode &= ~KBD_MODE_DISABLE_KBD;
28880cabfadSbellard         kbd_update_irq(s);
28980cabfadSbellard         break;
29080cabfadSbellard     case KBD_CCMD_READ_INPORT:
291f1b7e0e4SHervé Poussineau         kbd_queue(s, 0x80, 0);
29280cabfadSbellard         break;
29380cabfadSbellard     case KBD_CCMD_READ_OUTPORT:
294956a3e6bSBlue Swirl         kbd_queue(s, s->outport, 0);
29580cabfadSbellard         break;
29680cabfadSbellard     case KBD_CCMD_ENABLE_A20:
297956a3e6bSBlue Swirl         if (s->a20_out) {
298956a3e6bSBlue Swirl             qemu_irq_raise(*s->a20_out);
299956a3e6bSBlue Swirl         }
300956a3e6bSBlue Swirl         s->outport |= KBD_OUT_A20;
30180cabfadSbellard         break;
30280cabfadSbellard     case KBD_CCMD_DISABLE_A20:
303956a3e6bSBlue Swirl         if (s->a20_out) {
304956a3e6bSBlue Swirl             qemu_irq_lower(*s->a20_out);
305956a3e6bSBlue Swirl         }
306956a3e6bSBlue Swirl         s->outport &= ~KBD_OUT_A20;
30780cabfadSbellard         break;
30880cabfadSbellard     case KBD_CCMD_RESET:
309d7d02e3cSbellard         qemu_system_reset_request();
31080cabfadSbellard         break;
3115ccaa4ceSBernhard Kohl     case KBD_CCMD_NO_OP:
3125ccaa4ceSBernhard Kohl         /* ignore that */
31380cabfadSbellard         break;
31480cabfadSbellard     default:
315d540bfe0SAlexander Graf         fprintf(stderr, "qemu: unsupported keyboard cmd=0x%02x\n", (int)val);
31680cabfadSbellard         break;
31780cabfadSbellard     }
31880cabfadSbellard }
31980cabfadSbellard 
320d540bfe0SAlexander Graf static uint64_t kbd_read_data(void *opaque, hwaddr addr,
321d540bfe0SAlexander Graf                               unsigned size)
32280cabfadSbellard {
323b41a2cd1Sbellard     KBDState *s = opaque;
324e41c0f26Sbalrog     uint32_t val;
32580cabfadSbellard 
326daa57963Sbellard     if (s->pending == KBD_PENDING_AUX)
327e41c0f26Sbalrog         val = ps2_read_data(s->mouse);
328e41c0f26Sbalrog     else
329e41c0f26Sbalrog         val = ps2_read_data(s->kbd);
33080cabfadSbellard 
331c86d2c23SBlue Swirl     DPRINTF("kbd: read data=0x%02x\n", val);
332e41c0f26Sbalrog     return val;
33380cabfadSbellard }
33480cabfadSbellard 
335d540bfe0SAlexander Graf static void kbd_write_data(void *opaque, hwaddr addr,
336d540bfe0SAlexander Graf                            uint64_t val, unsigned size)
33780cabfadSbellard {
338b41a2cd1Sbellard     KBDState *s = opaque;
33980cabfadSbellard 
340c5539cb4SGonglei     DPRINTF("kbd: write data=0x%02" PRIx64 "\n", val);
34180cabfadSbellard 
34280cabfadSbellard     switch(s->write_cmd) {
34380cabfadSbellard     case 0:
344daa57963Sbellard         ps2_write_keyboard(s->kbd, val);
34580cabfadSbellard         break;
34680cabfadSbellard     case KBD_CCMD_WRITE_MODE:
34780cabfadSbellard         s->mode = val;
348f94f5d71Spbrook         ps2_keyboard_set_translation(s->kbd, (s->mode & KBD_MODE_KCC) != 0);
349daa57963Sbellard         /* ??? */
35080cabfadSbellard         kbd_update_irq(s);
35180cabfadSbellard         break;
35280cabfadSbellard     case KBD_CCMD_WRITE_OBUF:
35380cabfadSbellard         kbd_queue(s, val, 0);
35480cabfadSbellard         break;
35580cabfadSbellard     case KBD_CCMD_WRITE_AUX_OBUF:
35680cabfadSbellard         kbd_queue(s, val, 1);
35780cabfadSbellard         break;
35880cabfadSbellard     case KBD_CCMD_WRITE_OUTPORT:
3594b78a802SBlue Swirl         outport_write(s, val);
36080cabfadSbellard         break;
36180cabfadSbellard     case KBD_CCMD_WRITE_MOUSE:
362daa57963Sbellard         ps2_write_mouse(s->mouse, val);
36380cabfadSbellard         break;
36480cabfadSbellard     default:
36580cabfadSbellard         break;
36680cabfadSbellard     }
36780cabfadSbellard     s->write_cmd = 0;
36880cabfadSbellard }
36980cabfadSbellard 
370d7d02e3cSbellard static void kbd_reset(void *opaque)
37180cabfadSbellard {
372d7d02e3cSbellard     KBDState *s = opaque;
37380cabfadSbellard 
37480cabfadSbellard     s->mode = KBD_MODE_KBD_INT | KBD_MODE_MOUSE_INT;
37580cabfadSbellard     s->status = KBD_STAT_CMD | KBD_STAT_UNLOCKED;
376*d13c0404SPaolo Bonzini     s->outport = KBD_OUT_RESET | KBD_OUT_A20 | KBD_OUT_ONES;
377a28fe7e3SPavel Dovgalyuk     s->outport_present = false;
378a28fe7e3SPavel Dovgalyuk }
379a28fe7e3SPavel Dovgalyuk 
380a28fe7e3SPavel Dovgalyuk static uint8_t kbd_outport_default(KBDState *s)
381a28fe7e3SPavel Dovgalyuk {
382*d13c0404SPaolo Bonzini     return KBD_OUT_RESET | KBD_OUT_A20 | KBD_OUT_ONES
383a28fe7e3SPavel Dovgalyuk            | (s->status & KBD_STAT_OBF ? KBD_OUT_OBF : 0)
384a28fe7e3SPavel Dovgalyuk            | (s->status & KBD_STAT_MOUSE_OBF ? KBD_OUT_MOUSE_OBF : 0);
385a28fe7e3SPavel Dovgalyuk }
386a28fe7e3SPavel Dovgalyuk 
387a28fe7e3SPavel Dovgalyuk static int kbd_outport_post_load(void *opaque, int version_id)
388a28fe7e3SPavel Dovgalyuk {
389a28fe7e3SPavel Dovgalyuk     KBDState *s = opaque;
390a28fe7e3SPavel Dovgalyuk     s->outport_present = true;
391a28fe7e3SPavel Dovgalyuk     return 0;
392a28fe7e3SPavel Dovgalyuk }
393a28fe7e3SPavel Dovgalyuk 
394a28fe7e3SPavel Dovgalyuk static const VMStateDescription vmstate_kbd_outport = {
395a28fe7e3SPavel Dovgalyuk     .name = "pckbd_outport",
396a28fe7e3SPavel Dovgalyuk     .version_id = 1,
397a28fe7e3SPavel Dovgalyuk     .minimum_version_id = 1,
398a28fe7e3SPavel Dovgalyuk     .post_load = kbd_outport_post_load,
399a28fe7e3SPavel Dovgalyuk     .fields = (VMStateField[]) {
400a28fe7e3SPavel Dovgalyuk         VMSTATE_UINT8(outport, KBDState),
401a28fe7e3SPavel Dovgalyuk         VMSTATE_END_OF_LIST()
402a28fe7e3SPavel Dovgalyuk     }
403a28fe7e3SPavel Dovgalyuk };
404a28fe7e3SPavel Dovgalyuk 
405a28fe7e3SPavel Dovgalyuk static bool kbd_outport_needed(void *opaque)
406a28fe7e3SPavel Dovgalyuk {
407a28fe7e3SPavel Dovgalyuk     KBDState *s = opaque;
408a28fe7e3SPavel Dovgalyuk     return s->outport != kbd_outport_default(s);
409a28fe7e3SPavel Dovgalyuk }
410a28fe7e3SPavel Dovgalyuk 
411a28fe7e3SPavel Dovgalyuk static int kbd_post_load(void *opaque, int version_id)
412a28fe7e3SPavel Dovgalyuk {
413a28fe7e3SPavel Dovgalyuk     KBDState *s = opaque;
414a28fe7e3SPavel Dovgalyuk     if (!s->outport_present) {
415a28fe7e3SPavel Dovgalyuk         s->outport = kbd_outport_default(s);
416a28fe7e3SPavel Dovgalyuk     }
417a28fe7e3SPavel Dovgalyuk     s->outport_present = false;
418a28fe7e3SPavel Dovgalyuk     return 0;
41980cabfadSbellard }
42080cabfadSbellard 
4213c619b59SJuan Quintela static const VMStateDescription vmstate_kbd = {
4223c619b59SJuan Quintela     .name = "pckbd",
4233c619b59SJuan Quintela     .version_id = 3,
4243c619b59SJuan Quintela     .minimum_version_id = 3,
425a28fe7e3SPavel Dovgalyuk     .post_load = kbd_post_load,
4263c619b59SJuan Quintela     .fields = (VMStateField[]) {
4273c619b59SJuan Quintela         VMSTATE_UINT8(write_cmd, KBDState),
4283c619b59SJuan Quintela         VMSTATE_UINT8(status, KBDState),
4293c619b59SJuan Quintela         VMSTATE_UINT8(mode, KBDState),
4303c619b59SJuan Quintela         VMSTATE_UINT8(pending, KBDState),
4313c619b59SJuan Quintela         VMSTATE_END_OF_LIST()
432a28fe7e3SPavel Dovgalyuk     },
433a28fe7e3SPavel Dovgalyuk     .subsections = (VMStateSubsection[]) {
434a28fe7e3SPavel Dovgalyuk         {
435a28fe7e3SPavel Dovgalyuk             .vmsd = &vmstate_kbd_outport,
436a28fe7e3SPavel Dovgalyuk             .needed = kbd_outport_needed,
437a28fe7e3SPavel Dovgalyuk         },
438a28fe7e3SPavel Dovgalyuk         VMSTATE_END_OF_LIST()
439675376f2Sbellard     }
4403c619b59SJuan Quintela };
441675376f2Sbellard 
442b92bb99bSths /* Memory mapped interface */
443a8170e5eSAvi Kivity static uint32_t kbd_mm_readb (void *opaque, hwaddr addr)
444b92bb99bSths {
445b92bb99bSths     KBDState *s = opaque;
446b92bb99bSths 
4474efbe58fSaurel32     if (addr & s->mask)
448d540bfe0SAlexander Graf         return kbd_read_status(s, 0, 1) & 0xff;
4494efbe58fSaurel32     else
450d540bfe0SAlexander Graf         return kbd_read_data(s, 0, 1) & 0xff;
451b92bb99bSths }
452b92bb99bSths 
453a8170e5eSAvi Kivity static void kbd_mm_writeb (void *opaque, hwaddr addr, uint32_t value)
454b92bb99bSths {
455b92bb99bSths     KBDState *s = opaque;
456b92bb99bSths 
4574efbe58fSaurel32     if (addr & s->mask)
458d540bfe0SAlexander Graf         kbd_write_command(s, 0, value & 0xff, 1);
4594efbe58fSaurel32     else
460d540bfe0SAlexander Graf         kbd_write_data(s, 0, value & 0xff, 1);
461b92bb99bSths }
462b92bb99bSths 
463dbff76acSRichard Henderson static const MemoryRegionOps i8042_mmio_ops = {
464dbff76acSRichard Henderson     .endianness = DEVICE_NATIVE_ENDIAN,
465dbff76acSRichard Henderson     .old_mmio = {
466dbff76acSRichard Henderson         .read = { kbd_mm_readb, kbd_mm_readb, kbd_mm_readb },
467dbff76acSRichard Henderson         .write = { kbd_mm_writeb, kbd_mm_writeb, kbd_mm_writeb },
468dbff76acSRichard Henderson     },
469b92bb99bSths };
470b92bb99bSths 
47171db710fSblueswir1 void i8042_mm_init(qemu_irq kbd_irq, qemu_irq mouse_irq,
472dbff76acSRichard Henderson                    MemoryRegion *region, ram_addr_t size,
473a8170e5eSAvi Kivity                    hwaddr mask)
474b92bb99bSths {
4757267c094SAnthony Liguori     KBDState *s = g_malloc0(sizeof(KBDState));
476b92bb99bSths 
477b92bb99bSths     s->irq_kbd = kbd_irq;
478b92bb99bSths     s->irq_mouse = mouse_irq;
4794efbe58fSaurel32     s->mask = mask;
480b92bb99bSths 
4810be71e32SAlex Williamson     vmstate_register(NULL, 0, &vmstate_kbd, s);
482dbff76acSRichard Henderson 
4832c9b15caSPaolo Bonzini     memory_region_init_io(region, NULL, &i8042_mmio_ops, s, "i8042", size);
484b92bb99bSths 
485b92bb99bSths     s->kbd = ps2_kbd_init(kbd_update_kbd_irq, s);
486b92bb99bSths     s->mouse = ps2_mouse_init(kbd_update_aux_irq, s);
487a08d4367SJan Kiszka     qemu_register_reset(kbd_reset, s);
488b92bb99bSths }
489da85ccfbSGerd Hoffmann 
490a2e0b863SAndreas Färber #define TYPE_I8042 "i8042"
491a2e0b863SAndreas Färber #define I8042(obj) OBJECT_CHECK(ISAKBDState, (obj), TYPE_I8042)
492a2e0b863SAndreas Färber 
493da85ccfbSGerd Hoffmann typedef struct ISAKBDState {
494a2e0b863SAndreas Färber     ISADevice parent_obj;
495a2e0b863SAndreas Färber 
496da85ccfbSGerd Hoffmann     KBDState kbd;
497dbff76acSRichard Henderson     MemoryRegion io[2];
498da85ccfbSGerd Hoffmann } ISAKBDState;
499da85ccfbSGerd Hoffmann 
500956a3e6bSBlue Swirl void i8042_isa_mouse_fake_event(void *opaque)
501956a3e6bSBlue Swirl {
502956a3e6bSBlue Swirl     ISADevice *dev = opaque;
503a2e0b863SAndreas Färber     ISAKBDState *isa = I8042(dev);
504a2e0b863SAndreas Färber     KBDState *s = &isa->kbd;
505956a3e6bSBlue Swirl 
506956a3e6bSBlue Swirl     ps2_mouse_fake_event(s->mouse);
507956a3e6bSBlue Swirl }
508956a3e6bSBlue Swirl 
509956a3e6bSBlue Swirl void i8042_setup_a20_line(ISADevice *dev, qemu_irq *a20_out)
510956a3e6bSBlue Swirl {
511a2e0b863SAndreas Färber     ISAKBDState *isa = I8042(dev);
512a2e0b863SAndreas Färber     KBDState *s = &isa->kbd;
513956a3e6bSBlue Swirl 
514956a3e6bSBlue Swirl     s->a20_out = a20_out;
515956a3e6bSBlue Swirl }
516956a3e6bSBlue Swirl 
517d05ac8faSBlue Swirl static const VMStateDescription vmstate_kbd_isa = {
518be73cfe2SJuan Quintela     .name = "pckbd",
519be73cfe2SJuan Quintela     .version_id = 3,
520be73cfe2SJuan Quintela     .minimum_version_id = 3,
521be73cfe2SJuan Quintela     .fields = (VMStateField[]) {
522be73cfe2SJuan Quintela         VMSTATE_STRUCT(kbd, ISAKBDState, 0, vmstate_kbd, KBDState),
523be73cfe2SJuan Quintela         VMSTATE_END_OF_LIST()
524be73cfe2SJuan Quintela     }
525be73cfe2SJuan Quintela };
526be73cfe2SJuan Quintela 
527dbff76acSRichard Henderson static const MemoryRegionOps i8042_data_ops = {
528d540bfe0SAlexander Graf     .read = kbd_read_data,
529d540bfe0SAlexander Graf     .write = kbd_write_data,
530d540bfe0SAlexander Graf     .impl = {
531d540bfe0SAlexander Graf         .min_access_size = 1,
532d540bfe0SAlexander Graf         .max_access_size = 1,
533d540bfe0SAlexander Graf     },
534d540bfe0SAlexander Graf     .endianness = DEVICE_LITTLE_ENDIAN,
535dbff76acSRichard Henderson };
536dbff76acSRichard Henderson 
537dbff76acSRichard Henderson static const MemoryRegionOps i8042_cmd_ops = {
538d540bfe0SAlexander Graf     .read = kbd_read_status,
539d540bfe0SAlexander Graf     .write = kbd_write_command,
540d540bfe0SAlexander Graf     .impl = {
541d540bfe0SAlexander Graf         .min_access_size = 1,
542d540bfe0SAlexander Graf         .max_access_size = 1,
543d540bfe0SAlexander Graf     },
544d540bfe0SAlexander Graf     .endianness = DEVICE_LITTLE_ENDIAN,
545dbff76acSRichard Henderson };
546dbff76acSRichard Henderson 
547db895a1eSAndreas Färber static void i8042_initfn(Object *obj)
548da85ccfbSGerd Hoffmann {
549db895a1eSAndreas Färber     ISAKBDState *isa_s = I8042(obj);
550db895a1eSAndreas Färber     KBDState *s = &isa_s->kbd;
551db895a1eSAndreas Färber 
5521437c94bSPaolo Bonzini     memory_region_init_io(isa_s->io + 0, obj, &i8042_data_ops, s,
5531437c94bSPaolo Bonzini                           "i8042-data", 1);
5541437c94bSPaolo Bonzini     memory_region_init_io(isa_s->io + 1, obj, &i8042_cmd_ops, s,
5551437c94bSPaolo Bonzini                           "i8042-cmd", 1);
556db895a1eSAndreas Färber }
557db895a1eSAndreas Färber 
558db895a1eSAndreas Färber static void i8042_realizefn(DeviceState *dev, Error **errp)
559db895a1eSAndreas Färber {
560db895a1eSAndreas Färber     ISADevice *isadev = ISA_DEVICE(dev);
561a2e0b863SAndreas Färber     ISAKBDState *isa_s = I8042(dev);
562dbff76acSRichard Henderson     KBDState *s = &isa_s->kbd;
563da85ccfbSGerd Hoffmann 
564db895a1eSAndreas Färber     isa_init_irq(isadev, &s->irq_kbd, 1);
565db895a1eSAndreas Färber     isa_init_irq(isadev, &s->irq_mouse, 12);
566da85ccfbSGerd Hoffmann 
567db895a1eSAndreas Färber     isa_register_ioport(isadev, isa_s->io + 0, 0x60);
568db895a1eSAndreas Färber     isa_register_ioport(isadev, isa_s->io + 1, 0x64);
569da85ccfbSGerd Hoffmann 
570da85ccfbSGerd Hoffmann     s->kbd = ps2_kbd_init(kbd_update_kbd_irq, s);
571da85ccfbSGerd Hoffmann     s->mouse = ps2_mouse_init(kbd_update_aux_irq, s);
572da85ccfbSGerd Hoffmann     qemu_register_reset(kbd_reset, s);
573da85ccfbSGerd Hoffmann }
574da85ccfbSGerd Hoffmann 
5758f04ee08SAnthony Liguori static void i8042_class_initfn(ObjectClass *klass, void *data)
5768f04ee08SAnthony Liguori {
57739bffca2SAnthony Liguori     DeviceClass *dc = DEVICE_CLASS(klass);
578db895a1eSAndreas Färber 
579db895a1eSAndreas Färber     dc->realize = i8042_realizefn;
58039bffca2SAnthony Liguori     dc->vmsd = &vmstate_kbd_isa;
5818f04ee08SAnthony Liguori }
5828f04ee08SAnthony Liguori 
5838c43a6f0SAndreas Färber static const TypeInfo i8042_info = {
584a2e0b863SAndreas Färber     .name          = TYPE_I8042,
58539bffca2SAnthony Liguori     .parent        = TYPE_ISA_DEVICE,
58639bffca2SAnthony Liguori     .instance_size = sizeof(ISAKBDState),
587db895a1eSAndreas Färber     .instance_init = i8042_initfn,
5888f04ee08SAnthony Liguori     .class_init    = i8042_class_initfn,
589da85ccfbSGerd Hoffmann };
590da85ccfbSGerd Hoffmann 
59183f7d43aSAndreas Färber static void i8042_register_types(void)
592da85ccfbSGerd Hoffmann {
59339bffca2SAnthony Liguori     type_register_static(&i8042_info);
594da85ccfbSGerd Hoffmann }
59583f7d43aSAndreas Färber 
59683f7d43aSAndreas Färber type_init(i8042_register_types)
597