1 // Support for handling the PS/2 mouse/keyboard ports.
3 // Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4 // Based on code Copyright (c) 1999-2004 Vojtech Pavlik
6 // This file may be distributed under the terms of the GNU GPLv3 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
27 for (i=0; i<I8042_CTL_TIMEOUT; i++) {
28 u8 status = inb(PORT_PS2_STATUS);
29 if (status & I8042_STR_OBF)
33 dprintf(1, "i8042 timeout on wait read\n");
38 i8042_wait_write(void)
41 for (i=0; i<I8042_CTL_TIMEOUT; i++) {
42 u8 status = inb(PORT_PS2_STATUS);
43 if (! (status & I8042_STR_IBF))
47 dprintf(1, "i8042 timeout on wait write\n");
54 unsigned long flags = irq_save();
57 for (i=0; i<I8042_BUFFER_SIZE; i++) {
58 u8 status = inb(PORT_PS2_STATUS);
59 if (! (status & I8042_STR_OBF)) {
68 dprintf(1, "i8042 timeout on flush\n");
73 __i8042_command(int command, u8 *param)
75 int receive = (command >> 8) & 0xf;
76 int send = (command >> 12) & 0xf;
79 int ret = i8042_wait_write();
82 outb(command, PORT_PS2_STATUS);
84 // Send parameters (if any).
86 for (i = 0; i < send; i++) {
87 ret = i8042_wait_write();
90 outb(param[i], PORT_PS2_DATA);
93 // Receive parameters (if any).
94 for (i = 0; i < receive; i++) {
95 ret = i8042_wait_read();
98 param[i] = inb(PORT_PS2_DATA);
105 i8042_command(int command, u8 *param)
107 unsigned long flags = irq_save();
108 int ret = __i8042_command(command, param);
111 dprintf(2, "i8042 command %x failed\n", command);
116 i8042_kbd_write(u8 c)
118 unsigned long flags = irq_save();
120 int ret = i8042_wait_write();
122 outb(c, PORT_PS2_DATA);
130 i8042_aux_write(u8 c)
132 return i8042_command(I8042_CMD_AUX_SEND, &c);
136 /****************************************************************
138 ****************************************************************/
140 #define PS2_RET_ACK 0xfa
141 #define PS2_RET_NAK 0xfe
144 ps2_sendbyte(int aux, u8 command)
148 ret = i8042_aux_write(command);
150 ret = i8042_kbd_write(command);
155 ret = i8042_wait_read();
158 u8 ack = inb(PORT_PS2_DATA);
159 if (ack != PS2_RET_ACK) {
160 dprintf(1, "Missing ack (got %x not %x)\n", ack, PS2_RET_ACK);
168 ps2_command(int aux, int command, u8 *param)
171 int receive = (command >> 8) & 0xf;
172 int send = (command >> 12) & 0xf;
174 // Disable interrupts and keyboard/mouse.
175 u8 ps2ctr = GET_EBDA(ps2ctr);
178 newctr |= I8042_CTR_KBDDIS;
180 newctr |= I8042_CTR_AUXDIS;
181 newctr &= ~(I8042_CTR_KBDINT|I8042_CTR_AUXINT);
182 dprintf(6, "i8042 ctr old=%x new=%x\n", ps2ctr, newctr);
183 int ret = i8042_command(I8042_CMD_CTL_WCTR, &newctr);
188 ret = ps2_sendbyte(aux, command);
192 // Send parameters (if any).
194 for (i = 0; i < send; i++) {
195 ret = ps2_sendbyte(aux, command);
200 // Receive parameters (if any).
201 for (i = 0; i < receive; i++) {
202 ret = i8042_wait_read();
204 // On a receive timeout, return the item number that the
205 // transfer failed on.
209 param[i] = inb(PORT_PS2_DATA);
213 // Restore interrupts and keyboard/mouse.
214 ret2 = i8042_command(I8042_CMD_CTL_WCTR, &ps2ctr);
222 kbd_command(int command, u8 *param)
224 int ret = ps2_command(0, command, param);
226 dprintf(2, "keyboard command %x failed (ret=%d)\n", command, ret);
231 aux_command(int command, u8 *param)
233 int ret = ps2_command(1, command, param);
235 dprintf(2, "mouse command %x failed (ret=%d)\n", command, ret);