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
12 #include "pic.h" // eoi_pic1
15 /****************************************************************
16 * Low level i8042 commands.
17 ****************************************************************/
20 #define I8042_CTL_TIMEOUT 10000
22 #define I8042_BUFFER_SIZE 16
27 dprintf(7, "i8042_wait_read\n");
29 for (i=0; i<I8042_CTL_TIMEOUT; i++) {
30 u8 status = inb(PORT_PS2_STATUS);
31 if (status & I8042_STR_OBF)
35 dprintf(1, "i8042 timeout on wait read\n");
40 i8042_wait_write(void)
42 dprintf(7, "i8042_wait_write\n");
44 for (i=0; i<I8042_CTL_TIMEOUT; i++) {
45 u8 status = inb(PORT_PS2_STATUS);
46 if (! (status & I8042_STR_IBF))
50 dprintf(1, "i8042 timeout on wait write\n");
57 dprintf(7, "i8042_flush\n");
59 for (i=0; i<I8042_BUFFER_SIZE; i++) {
60 u8 status = inb(PORT_PS2_STATUS);
61 if (! (status & I8042_STR_OBF))
64 u8 data = inb(PORT_PS2_DATA);
65 dprintf(7, "i8042 flushed %x (status=%x)\n", data, status);
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);
99 dprintf(7, "i8042 param=%x\n", param[i]);
106 i8042_command(int command, u8 *param)
108 dprintf(7, "i8042_command cmd=%x\n", command);
109 int ret = __i8042_command(command, param);
111 dprintf(2, "i8042 command %x failed\n", command);
116 i8042_kbd_write(u8 c)
118 dprintf(7, "i8042_kbd_write c=%d\n", c);
119 int ret = i8042_wait_write();
121 outb(c, PORT_PS2_DATA);
126 i8042_aux_write(u8 c)
128 return i8042_command(I8042_CMD_AUX_SEND, &c);
132 /****************************************************************
134 ****************************************************************/
136 #define PS2_RET_ACK 0xfa
137 #define PS2_RET_NAK 0xfe
140 process_ps2byte(u8 status, u8 data)
143 // Don't pull in all of keyboard/mouse code into 32bit code -
144 // just discard the data.
145 dprintf(1, "Discarding ps2 data %x (status=%x)\n", data, status);
148 if (status & I8042_STR_AUXDATA)
155 ps2_recvbyte(int aux, int needack, int timeout)
157 u64 end = calc_future_tsc(timeout);
159 u8 status = inb(PORT_PS2_STATUS);
160 if (status & I8042_STR_OBF) {
161 u8 data = inb(PORT_PS2_DATA);
162 dprintf(7, "ps2 read %x\n", data);
164 if (!!(status & I8042_STR_AUXDATA) == aux) {
167 if (data == PS2_RET_ACK)
169 if (data == PS2_RET_NAK) {
170 dprintf(1, "Got ps2 nak (status=%x)\n", status);
175 // Data not part of this command.
176 process_ps2byte(status, data);
179 if (check_time(end)) {
180 dprintf(1, "ps2_recvbyte timeout\n");
188 ps2_sendbyte(int aux, u8 command, int timeout)
190 dprintf(7, "ps2_sendbyte aux=%d cmd=%x\n", aux, command);
193 ret = i8042_aux_write(command);
195 ret = i8042_kbd_write(command);
200 ret = ps2_recvbyte(aux, 1, timeout);
203 if (ret != PS2_RET_ACK)
210 ps2_command(int aux, int command, u8 *param)
213 int receive = (command >> 8) & 0xf;
214 int send = (command >> 12) & 0xf;
216 // Disable interrupts and keyboard/mouse.
217 u8 ps2ctr = GET_EBDA(ps2ctr);
220 newctr |= I8042_CTR_KBDDIS;
222 newctr |= I8042_CTR_AUXDIS;
223 newctr &= ~(I8042_CTR_KBDINT|I8042_CTR_AUXINT);
224 dprintf(6, "i8042 ctr old=%x new=%x\n", ps2ctr, newctr);
225 int ret = i8042_command(I8042_CMD_CTL_WCTR, &newctr);
229 if (command == ATKBD_CMD_RESET_BAT) {
230 // Reset is special wrt timeouts.
233 ret = ps2_sendbyte(aux, command, 1000);
237 // Receive parameters.
238 ret = ps2_recvbyte(aux, 0, 4000);
242 ret = ps2_recvbyte(aux, 0, 100);
244 // Some devices only respond with one byte on reset.
249 ret = ps2_sendbyte(aux, command, 200);
253 // Send parameters (if any).
255 for (i = 0; i < send; i++) {
256 ret = ps2_sendbyte(aux, param[i], 200);
261 // Receive parameters (if any).
262 for (i = 0; i < receive; i++) {
263 ret = ps2_recvbyte(aux, 0, 500);
273 // Restore interrupts and keyboard/mouse.
274 ret2 = i8042_command(I8042_CMD_CTL_WCTR, &ps2ctr);
282 kbd_command(int command, u8 *param)
284 dprintf(7, "kbd_command cmd=%x\n", command);
285 int ret = ps2_command(0, command, param);
287 dprintf(2, "keyboard command %x failed\n", command);
292 aux_command(int command, u8 *param)
294 dprintf(7, "aux_command cmd=%x\n", command);
295 int ret = ps2_command(1, command, param);
297 dprintf(2, "mouse command %x failed\n", command);
302 /****************************************************************
304 ****************************************************************/
309 u8 status = inb(PORT_PS2_STATUS);
310 if (!(status & I8042_STR_OBF)) {
311 dprintf(1, "ps2 irq but no data.\n");
314 u8 data = inb(PORT_PS2_DATA);
316 process_ps2byte(status, data);
319 // INT74h : PS/2 mouse hardware interrupt
323 if (! CONFIG_PS2PORT)
326 debug_isr(DEBUG_ISR_74);
331 // INT09h : Keyboard Hardware Service Entry Point
335 if (! CONFIG_PS2PORT)
338 debug_isr(DEBUG_ISR_09);
344 /****************************************************************
346 ****************************************************************/
351 /* flush incoming keys */
352 int ret = i8042_flush();
356 // Controller self-test.
358 ret = i8042_command(I8042_CMD_CTL_TEST, param);
361 if (param[0] != 0x55) {
362 dprintf(1, "i8042 self test failed (got %x not 0x55)\n", param[0]);
366 // Controller keyboard test.
367 ret = i8042_command(I8042_CMD_KBD_TEST, param);
370 if (param[0] != 0x00) {
371 dprintf(1, "i8042 keyboard test failed (got %x not 0x00)\n", param[0]);
375 // Enable keyboard and mouse ports.
376 ret = i8042_command(I8042_CMD_KBD_ENABLE, NULL);
379 ret = i8042_command(I8042_CMD_AUX_ENABLE, NULL);
384 /* ------------------- keyboard side ------------------------*/
385 /* reset keyboard and self test (keyboard side) */
386 ret = kbd_command(ATKBD_CMD_RESET_BAT, param);
389 if (param[0] != 0xaa) {
390 dprintf(1, "keyboard self test failed (got %x not 0xaa)\n", param[0]);
394 /* Disable keyboard */
395 ret = kbd_command(ATKBD_CMD_RESET_DIS, NULL);
399 // Set scancode command (mode 2)
401 ret = kbd_command(ATKBD_CMD_SSCANSET, param);
405 // Keyboard Mode: scan code convert, disable mouse, enable IRQ 1
406 SET_EBDA(ps2ctr, I8042_CTR_AUXDIS | I8042_CTR_XLATE | I8042_CTR_KBDINT);
408 /* Enable keyboard */
409 ret = kbd_command(ATKBD_CMD_ENABLE, NULL);
413 dprintf(1, "keyboard initialized\n");
419 if (! CONFIG_PS2PORT)
421 dprintf(3, "init ps2port\n");
423 enable_hwirq(1, entry_09);
424 enable_hwirq(12, entry_74);
426 run_thread(keyboard_init, NULL);