1 // Support for handling the PS/2 mouse/keyboard ports.
3 // Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4 // Several ideas taken from code Copyright (c) 1999-2004 Vojtech Pavlik
6 // This file may be distributed under the terms of the GNU LGPLv3 license.
8 #include "ioport.h" // inb
9 #include "util.h" // dprintf
10 #include "biosvar.h" // GET_EBDA
11 #include "ps2port.h" // kbd_command
14 /****************************************************************
15 * Low level i8042 commands.
16 ****************************************************************/
19 #define I8042_CTL_TIMEOUT 10000
21 #define I8042_BUFFER_SIZE 16
26 dprintf(7, "i8042_wait_read\n");
28 for (i=0; i<I8042_CTL_TIMEOUT; i++) {
29 u8 status = inb(PORT_PS2_STATUS);
30 if (status & I8042_STR_OBF)
34 dprintf(1, "i8042 timeout on wait read\n");
39 i8042_wait_write(void)
41 dprintf(7, "i8042_wait_write\n");
43 for (i=0; i<I8042_CTL_TIMEOUT; i++) {
44 u8 status = inb(PORT_PS2_STATUS);
45 if (! (status & I8042_STR_IBF))
49 dprintf(1, "i8042 timeout on wait write\n");
56 dprintf(7, "i8042_flush\n");
58 for (i=0; i<I8042_BUFFER_SIZE; i++) {
59 u8 status = inb(PORT_PS2_STATUS);
60 if (! (status & I8042_STR_OBF))
63 u8 data = inb(PORT_PS2_DATA);
64 dprintf(7, "i8042 flushed %x (status=%x)\n", data, status);
67 dprintf(1, "i8042 timeout on flush\n");
72 __i8042_command(int command, u8 *param)
74 int receive = (command >> 8) & 0xf;
75 int send = (command >> 12) & 0xf;
78 int ret = i8042_wait_write();
81 outb(command, PORT_PS2_STATUS);
83 // Send parameters (if any).
85 for (i = 0; i < send; i++) {
86 ret = i8042_wait_write();
89 outb(param[i], PORT_PS2_DATA);
92 // Receive parameters (if any).
93 for (i = 0; i < receive; i++) {
94 ret = i8042_wait_read();
97 param[i] = inb(PORT_PS2_DATA);
98 dprintf(7, "i8042 param=%x\n", param[i]);
105 i8042_command(int command, u8 *param)
107 dprintf(7, "i8042_command cmd=%x\n", command);
108 int ret = __i8042_command(command, param);
110 dprintf(2, "i8042 command %x failed\n", command);
115 i8042_kbd_write(u8 c)
117 dprintf(7, "i8042_kbd_write c=%d\n", c);
118 int ret = i8042_wait_write();
120 outb(c, PORT_PS2_DATA);
125 i8042_aux_write(u8 c)
127 return i8042_command(I8042_CMD_AUX_SEND, &c);
131 /****************************************************************
133 ****************************************************************/
135 #define PS2_RET_ACK 0xfa
136 #define PS2_RET_NAK 0xfe
139 ps2_recvbyte(int aux, int needack, int timeout)
141 u64 end = calc_future_tsc(timeout);
143 u8 status = inb(PORT_PS2_STATUS);
144 if (status & I8042_STR_OBF) {
145 u8 data = inb(PORT_PS2_DATA);
146 dprintf(7, "ps2 read %x\n", data);
148 if (!!(status & I8042_STR_AUXDATA) == aux) {
151 if (data == PS2_RET_ACK)
153 if (data == PS2_RET_NAK) {
154 dprintf(1, "Got ps2 nak (status=%x)\n", status);
159 // This data not for us - XXX - just discard it for now.
160 dprintf(1, "Discarding ps2 data %x (status=%x)\n", data, status);
163 if (check_time(end)) {
164 dprintf(1, "ps2_recvbyte timeout\n");
172 ps2_sendbyte(int aux, u8 command, int timeout)
174 dprintf(7, "ps2_sendbyte aux=%d cmd=%x\n", aux, command);
177 ret = i8042_aux_write(command);
179 ret = i8042_kbd_write(command);
184 ret = ps2_recvbyte(aux, 1, timeout);
187 if (ret != PS2_RET_ACK)
194 ps2_command(int aux, int command, u8 *param)
197 int receive = (command >> 8) & 0xf;
198 int send = (command >> 12) & 0xf;
200 // Disable interrupts and keyboard/mouse.
201 u8 ps2ctr = GET_EBDA(ps2ctr);
204 newctr |= I8042_CTR_KBDDIS;
206 newctr |= I8042_CTR_AUXDIS;
207 newctr &= ~(I8042_CTR_KBDINT|I8042_CTR_AUXINT);
208 dprintf(6, "i8042 ctr old=%x new=%x\n", ps2ctr, newctr);
209 int ret = i8042_command(I8042_CMD_CTL_WCTR, &newctr);
213 if (command == ATKBD_CMD_RESET_BAT) {
214 // Reset is special wrt timeouts.
217 ret = ps2_sendbyte(aux, command, 1000);
221 // Receive parameters.
222 ret = ps2_recvbyte(aux, 0, 4000);
226 ret = ps2_recvbyte(aux, 0, 100);
228 // Some devices only respond with one byte on reset.
233 ret = ps2_sendbyte(aux, command, 200);
237 // Send parameters (if any).
239 for (i = 0; i < send; i++) {
240 ret = ps2_sendbyte(aux, param[i], 200);
245 // Receive parameters (if any).
246 for (i = 0; i < receive; i++) {
247 ret = ps2_recvbyte(aux, 0, 500);
257 // Restore interrupts and keyboard/mouse.
258 ret2 = i8042_command(I8042_CMD_CTL_WCTR, &ps2ctr);
266 kbd_command(int command, u8 *param)
268 dprintf(7, "kbd_command cmd=%x\n", command);
269 int ret = ps2_command(0, command, param);
271 dprintf(2, "keyboard command %x failed\n", command);
276 aux_command(int command, u8 *param)
278 dprintf(7, "aux_command cmd=%x\n", command);
279 int ret = ps2_command(1, command, param);
281 dprintf(2, "mouse command %x failed\n", command);