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" // ps2_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)
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))
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);
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);
135 for (i=0; i<10; i++) {
138 outb(0xfe, PORT_PS2_STATUS); /* pulse reset low */
144 /****************************************************************
146 ****************************************************************/
148 #define PS2_RET_ACK 0xfa
149 #define PS2_RET_NAK 0xfe
152 ps2_recvbyte(int aux, int needack, int timeout)
154 u64 end = calc_future_tsc(timeout);
156 u8 status = inb(PORT_PS2_STATUS);
157 if (status & I8042_STR_OBF) {
158 u8 data = inb(PORT_PS2_DATA);
159 dprintf(7, "ps2 read %x\n", data);
161 if (!!(status & I8042_STR_AUXDATA) == aux) {
164 if (data == PS2_RET_ACK)
166 if (data == PS2_RET_NAK) {
167 dprintf(1, "Got ps2 nak (status=%x)\n", status);
172 // This data not part of command - just discard it.
173 dprintf(1, "Discarding ps2 data %02x (status=%02x)\n", data, status);
176 if (check_tsc(end)) {
177 // Don't warn on second byte of a reset
187 ps2_sendbyte(int aux, u8 command, int timeout)
189 dprintf(7, "ps2_sendbyte aux=%d cmd=%x\n", aux, command);
192 ret = i8042_aux_write(command);
194 ret = i8042_kbd_write(command);
199 ret = ps2_recvbyte(aux, 1, timeout);
202 if (ret != PS2_RET_ACK)
209 __ps2_command(int aux, int command, u8 *param)
212 int receive = (command >> 8) & 0xf;
213 int send = (command >> 12) & 0xf;
215 // Disable interrupts and keyboard/mouse.
216 u8 ps2ctr = GET_EBDA(ps2ctr);
217 u8 newctr = ((ps2ctr | I8042_CTR_AUXDIS | I8042_CTR_KBDDIS)
218 & ~(I8042_CTR_KBDINT|I8042_CTR_AUXINT));
219 dprintf(6, "i8042 ctr old=%x new=%x\n", ps2ctr, newctr);
220 int ret = i8042_command(I8042_CMD_CTL_WCTR, &newctr);
224 // Flush any interrupts already pending.
227 // Enable port command is being sent to.
229 newctr &= ~I8042_CTR_AUXDIS;
231 newctr &= ~I8042_CTR_KBDDIS;
232 ret = i8042_command(I8042_CMD_CTL_WCTR, &newctr);
236 if (command == ATKBD_CMD_RESET_BAT) {
237 // Reset is special wrt timeouts and bytes received.
240 ret = ps2_sendbyte(aux, command, 1000);
244 // Receive parameters.
245 ret = ps2_recvbyte(aux, 0, 4000);
249 ret = ps2_recvbyte(aux, 0, 100);
251 // Some devices only respond with one byte on reset.
254 } else if (command == ATKBD_CMD_GETID) {
255 // Getid is special wrt bytes received.
258 ret = ps2_sendbyte(aux, command, 200);
262 // Receive parameters.
263 ret = ps2_recvbyte(aux, 0, 500);
267 if (ret == 0xab || ret == 0xac || ret == 0x2b || ret == 0x5d
268 || ret == 0x60 || ret == 0x47) {
269 // These ids (keyboards) return two bytes.
270 ret = ps2_recvbyte(aux, 0, 500);
279 ret = ps2_sendbyte(aux, command, 200);
283 // Send parameters (if any).
285 for (i = 0; i < send; i++) {
286 ret = ps2_sendbyte(aux, param[i], 200);
291 // Receive parameters (if any).
292 for (i = 0; i < receive; i++) {
293 ret = ps2_recvbyte(aux, 0, 500);
303 // Restore interrupts and keyboard/mouse.
304 ret2 = i8042_command(I8042_CMD_CTL_WCTR, &ps2ctr);
312 ps2_command(int aux, int command, u8 *param)
314 dprintf(7, "ps2_command aux=%d cmd=%x\n", aux, command);
315 int ret = __ps2_command(aux, command, param);
317 dprintf(2, "ps2 command %x failed (aux=%d)\n", command, aux);
322 ps2_kbd_command(int command, u8 *param)
324 return ps2_command(0, command, param);
328 ps2_mouse_command(int command, u8 *param)
330 // Update ps2ctr for mouse enable/disable.
331 if (command == PSMOUSE_CMD_ENABLE || command == PSMOUSE_CMD_DISABLE) {
332 u16 ebda_seg = get_ebda_seg();
333 u8 ps2ctr = GET_EBDA2(ebda_seg, ps2ctr);
334 if (command == PSMOUSE_CMD_ENABLE)
335 ps2ctr = (ps2ctr | I8042_CTR_AUXINT) & ~I8042_CTR_AUXDIS;
337 ps2ctr = (ps2ctr | I8042_CTR_AUXDIS) & ~I8042_CTR_AUXINT;
338 SET_EBDA2(ebda_seg, ps2ctr, ps2ctr);
341 return ps2_command(1, command, param);
345 /****************************************************************
347 ****************************************************************/
349 // INT74h : PS/2 mouse hardware interrupt
353 if (! CONFIG_PS2PORT)
356 debug_isr(DEBUG_ISR_74);
358 u8 v = inb(PORT_PS2_STATUS);
359 if ((v & (I8042_STR_OBF|I8042_STR_AUXDATA))
360 != (I8042_STR_OBF|I8042_STR_AUXDATA)) {
361 dprintf(1, "ps2 mouse irq but no mouse data.\n");
364 v = inb(PORT_PS2_DATA);
366 if (!(GET_EBDA(ps2ctr) & I8042_CTR_AUXINT))
367 // Interrupts not enabled.
376 // INT09h : Keyboard Hardware Service Entry Point
380 if (! CONFIG_PS2PORT)
383 debug_isr(DEBUG_ISR_09);
385 // read key from keyboard controller
386 u8 v = inb(PORT_PS2_STATUS);
387 if (v & I8042_STR_AUXDATA) {
388 dprintf(1, "ps2 keyboard irq but found mouse data?!\n");
391 v = inb(PORT_PS2_DATA);
393 if (!(GET_EBDA(ps2ctr) & I8042_CTR_KBDINT))
394 // Interrupts not enabled.
404 /****************************************************************
406 ****************************************************************/
409 keyboard_init(void *data)
411 /* flush incoming keys */
412 int ret = i8042_flush();
416 // Controller self-test.
418 ret = i8042_command(I8042_CMD_CTL_TEST, param);
421 if (param[0] != 0x55) {
422 dprintf(1, "i8042 self test failed (got %x not 0x55)\n", param[0]);
426 // Controller keyboard test.
427 ret = i8042_command(I8042_CMD_KBD_TEST, param);
430 if (param[0] != 0x00) {
431 dprintf(1, "i8042 keyboard test failed (got %x not 0x00)\n", param[0]);
435 // Disable keyboard and mouse events.
436 SET_EBDA(ps2ctr, I8042_CTR_KBDDIS | I8042_CTR_AUXDIS);
439 /* ------------------- keyboard side ------------------------*/
440 /* reset keyboard and self test (keyboard side) */
441 ret = ps2_kbd_command(ATKBD_CMD_RESET_BAT, param);
444 if (param[0] != 0xaa) {
445 dprintf(1, "keyboard self test failed (got %x not 0xaa)\n", param[0]);
449 /* Disable keyboard */
450 ret = ps2_kbd_command(ATKBD_CMD_RESET_DIS, NULL);
454 // Set scancode command (mode 2)
456 ret = ps2_kbd_command(ATKBD_CMD_SSCANSET, param);
460 // Keyboard Mode: disable mouse, scan code convert, enable kbd IRQ
461 SET_EBDA(ps2ctr, I8042_CTR_AUXDIS | I8042_CTR_XLATE | I8042_CTR_KBDINT);
463 /* Enable keyboard */
464 ret = ps2_kbd_command(ATKBD_CMD_ENABLE, NULL);
468 dprintf(1, "PS2 keyboard initialized\n");
475 if (! CONFIG_PS2PORT)
477 dprintf(3, "init ps2port\n");
479 enable_hwirq(1, FUNC16(entry_09));
480 enable_hwirq(12, FUNC16(entry_74));
482 run_thread(keyboard_init, NULL);