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");
57 unsigned long flags = irq_save();
60 for (i=0; i<I8042_BUFFER_SIZE; i++) {
61 u8 status = inb(PORT_PS2_STATUS);
62 if (! (status & I8042_STR_OBF)) {
67 u8 data = inb(PORT_PS2_DATA);
68 dprintf(7, "i8042 flushed %x\n", data);
72 dprintf(1, "i8042 timeout on flush\n");
77 __i8042_command(int command, u8 *param)
79 int receive = (command >> 8) & 0xf;
80 int send = (command >> 12) & 0xf;
83 int ret = i8042_wait_write();
86 outb(command, PORT_PS2_STATUS);
88 // Send parameters (if any).
90 for (i = 0; i < send; i++) {
91 ret = i8042_wait_write();
94 outb(param[i], PORT_PS2_DATA);
97 // Receive parameters (if any).
98 for (i = 0; i < receive; i++) {
99 ret = i8042_wait_read();
102 param[i] = inb(PORT_PS2_DATA);
103 dprintf(7, "i8042 param=%x\n", param[i]);
110 i8042_command(int command, u8 *param)
112 dprintf(7, "i8042_command cmd=%x\n", command);
113 unsigned long flags = irq_save();
114 int ret = __i8042_command(command, param);
117 dprintf(2, "i8042 command %x failed\n", command);
122 i8042_kbd_write(u8 c)
124 dprintf(7, "i8042_kbd_write c=%d\n", c);
125 unsigned long flags = irq_save();
127 int ret = i8042_wait_write();
129 outb(c, PORT_PS2_DATA);
137 i8042_aux_write(u8 c)
139 return i8042_command(I8042_CMD_AUX_SEND, &c);
143 /****************************************************************
145 ****************************************************************/
147 #define PS2_RET_ACK 0xfa
148 #define PS2_RET_NAK 0xfe
151 ps2_recvbyte(int aux, int needack, int timeout)
153 u64 end = calc_future_tsc(timeout);
155 if (rdtscll() >= end) {
156 dprintf(1, "ps2_recvbyte timeout\n");
160 u8 status = inb(PORT_PS2_STATUS);
161 if (! (status & I8042_STR_OBF))
163 u8 data = inb(PORT_PS2_DATA);
164 dprintf(7, "ps2 read %x\n", data);
166 if ((!!(status & I8042_STR_AUXDATA) != aux)
167 || (needack && data != PS2_RET_ACK)) {
168 // This data not for us - XXX - just discard it for now.
169 dprintf(1, "Discarding ps2 data %x\n", data);
178 ps2_sendbyte(int aux, u8 command)
180 dprintf(7, "ps2_sendbyte aux=%d cmd=%x\n", aux, command);
183 ret = i8042_aux_write(command);
185 ret = i8042_kbd_write(command);
190 ret = ps2_recvbyte(aux, 1, 200);
198 ps2_command(int aux, int command, u8 *param)
201 int receive = (command >> 8) & 0xf;
202 int send = (command >> 12) & 0xf;
204 // Disable interrupts and keyboard/mouse.
205 u8 ps2ctr = GET_EBDA(ps2ctr);
208 newctr |= I8042_CTR_KBDDIS;
210 newctr |= I8042_CTR_AUXDIS;
211 newctr &= ~(I8042_CTR_KBDINT|I8042_CTR_AUXINT);
212 dprintf(6, "i8042 ctr old=%x new=%x\n", ps2ctr, newctr);
213 int ret = i8042_command(I8042_CMD_CTL_WCTR, &newctr);
218 ret = ps2_sendbyte(aux, command);
222 // Send parameters (if any).
224 for (i = 0; i < send; i++) {
225 ret = ps2_sendbyte(aux, param[i]);
230 // Receive parameters (if any).
231 for (i = 0; i < receive; i++) {
232 u8 data = ps2_recvbyte(aux, 0, 200);
234 // On a receive timeout, return the item number that the
235 // transfer failed on.
243 // Restore interrupts and keyboard/mouse.
244 ret2 = i8042_command(I8042_CMD_CTL_WCTR, &ps2ctr);
252 kbd_command(int command, u8 *param)
254 dprintf(7, "kbd_command cmd=%x\n", command);
255 int ret = ps2_command(0, command, param);
257 dprintf(2, "keyboard command %x failed (ret=%d)\n", command, ret);
262 aux_command(int command, u8 *param)
264 dprintf(7, "aux_command cmd=%x\n", command);
265 int ret = ps2_command(1, command, param);
267 dprintf(2, "mouse command %x failed (ret=%d)\n", command, ret);