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 process_ps2bytes(void)
158 u8 status = inb(PORT_PS2_STATUS);
159 if (!(status & I8042_STR_OBF))
161 u8 data = inb(PORT_PS2_DATA);
162 process_ps2byte(status, data);
167 ps2_recvbyte(int aux, int needack, int timeout)
169 u64 end = calc_future_tsc(timeout);
171 u8 status = inb(PORT_PS2_STATUS);
172 if (status & I8042_STR_OBF) {
173 u8 data = inb(PORT_PS2_DATA);
174 dprintf(7, "ps2 read %x\n", data);
176 if (!!(status & I8042_STR_AUXDATA) == aux) {
179 if (data == PS2_RET_ACK)
181 if (data == PS2_RET_NAK) {
182 dprintf(1, "Got ps2 nak (status=%x)\n", status);
187 // Data not part of this command.
188 process_ps2byte(status, data);
191 if (check_time(end)) {
192 dprintf(1, "ps2_recvbyte timeout\n");
200 ps2_sendbyte(int aux, u8 command, int timeout)
202 dprintf(7, "ps2_sendbyte aux=%d cmd=%x\n", aux, command);
205 ret = i8042_aux_write(command);
207 ret = i8042_kbd_write(command);
212 ret = ps2_recvbyte(aux, 1, timeout);
215 if (ret != PS2_RET_ACK)
222 ps2_command(int aux, int command, u8 *param)
225 int receive = (command >> 8) & 0xf;
226 int send = (command >> 12) & 0xf;
228 // Disable processing of interrupts.
229 u8 kbdflag = GET_BDA(kbd_flag3);
230 SET_BDA(kbd_flag3, kbdflag | KF3_CMD_PENDING);
232 if (command == ATKBD_CMD_RESET_BAT) {
233 // Reset is special wrt timeouts.
236 ret = ps2_sendbyte(aux, command, 1000);
240 // Receive parameters.
241 ret = ps2_recvbyte(aux, 0, 4000);
245 ret = ps2_recvbyte(aux, 0, 100);
247 // Some devices only respond with one byte on reset.
252 ret = ps2_sendbyte(aux, command, 200);
256 // Send parameters (if any).
258 for (i = 0; i < send; i++) {
259 ret = ps2_sendbyte(aux, param[i], 200);
264 // Receive parameters (if any).
265 for (i = 0; i < receive; i++) {
266 ret = ps2_recvbyte(aux, 0, 500);
276 // Restore processing of interrupts.
277 if (!(kbdflag & KF3_CMD_PENDING))
279 SET_BDA(kbd_flag3, kbdflag);
285 kbd_command(int command, u8 *param)
287 dprintf(7, "kbd_command cmd=%x\n", command);
288 int ret = ps2_command(0, command, param);
290 dprintf(2, "keyboard command %x failed\n", command);
295 aux_command(int command, u8 *param)
297 dprintf(7, "aux_command cmd=%x\n", command);
298 int ret = ps2_command(1, command, param);
300 dprintf(2, "mouse command %x failed\n", command);
305 /****************************************************************
307 ****************************************************************/
312 if (GET_BDA(kbd_flag3) & KF3_CMD_PENDING)
313 // PS/2 command in progress - it will handle this event.
318 // INT74h : PS/2 mouse hardware interrupt
322 if (! CONFIG_PS2PORT)
325 debug_isr(DEBUG_ISR_74);
330 // INT09h : Keyboard Hardware Service Entry Point
334 if (! CONFIG_PS2PORT)
337 debug_isr(DEBUG_ISR_09);
343 /****************************************************************
345 ****************************************************************/
348 keyboard_init(void *data)
350 /* flush incoming keys */
351 int ret = i8042_flush();
355 // Controller self-test.
357 ret = i8042_command(I8042_CMD_CTL_TEST, param);
360 if (param[0] != 0x55) {
361 dprintf(1, "i8042 self test failed (got %x not 0x55)\n", param[0]);
365 // Controller keyboard test.
366 ret = i8042_command(I8042_CMD_KBD_TEST, param);
369 if (param[0] != 0x00) {
370 dprintf(1, "i8042 keyboard test failed (got %x not 0x00)\n", param[0]);
374 // Enable keyboard and mouse ports.
375 ret = i8042_command(I8042_CMD_KBD_ENABLE, NULL);
378 ret = i8042_command(I8042_CMD_AUX_ENABLE, NULL);
383 /* ------------------- keyboard side ------------------------*/
384 /* reset keyboard and self test (keyboard side) */
385 ret = kbd_command(ATKBD_CMD_RESET_BAT, param);
388 if (param[0] != 0xaa) {
389 dprintf(1, "keyboard self test failed (got %x not 0xaa)\n", param[0]);
393 /* Disable keyboard */
394 ret = kbd_command(ATKBD_CMD_RESET_DIS, NULL);
398 // Set scancode command (mode 2)
400 ret = kbd_command(ATKBD_CMD_SSCANSET, param);
404 // Mode: scan code convert, enable IRQ 1, enable IRQ 12
405 param[0] = I8042_CTR_XLATE | I8042_CTR_KBDINT | I8042_CTR_AUXINT;
406 ret = i8042_command(I8042_CMD_CTL_WCTR, param);
409 CLEARBITS_BDA(kbd_flag3, KF3_CMD_PENDING);
411 /* Enable keyboard */
412 ret = kbd_command(ATKBD_CMD_ENABLE, NULL);
416 dprintf(1, "keyboard initialized\n");
422 if (! CONFIG_PS2PORT)
424 dprintf(3, "init ps2port\n");
426 // Setup irqs, but disable them until init complete.
427 SETBITS_BDA(kbd_flag3, KF3_CMD_PENDING);
428 enable_hwirq(1, entry_09);
429 enable_hwirq(12, entry_74);
431 run_thread(keyboard_init, NULL);