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 "paravirt.h" // romfile_loadint
11 #include "biosvar.h" // GET_EBDA
12 #include "ps2port.h" // ps2_kbd_command
13 #include "pic.h" // eoi_pic1
16 /****************************************************************
17 * Low level i8042 commands.
18 ****************************************************************/
21 #define I8042_CTL_TIMEOUT 10000
23 #define I8042_BUFFER_SIZE 16
28 dprintf(7, "i8042_wait_read\n");
30 for (i=0; i<I8042_CTL_TIMEOUT; i++) {
31 u8 status = inb(PORT_PS2_STATUS);
32 if (status & I8042_STR_OBF)
41 i8042_wait_write(void)
43 dprintf(7, "i8042_wait_write\n");
45 for (i=0; i<I8042_CTL_TIMEOUT; i++) {
46 u8 status = inb(PORT_PS2_STATUS);
47 if (! (status & I8042_STR_IBF))
58 dprintf(7, "i8042_flush\n");
60 for (i=0; i<I8042_BUFFER_SIZE; i++) {
61 u8 status = inb(PORT_PS2_STATUS);
62 if (! (status & I8042_STR_OBF))
65 u8 data = inb(PORT_PS2_DATA);
66 dprintf(7, "i8042 flushed %x (status=%x)\n", data, status);
74 __i8042_command(int command, u8 *param)
76 int receive = (command >> 8) & 0xf;
77 int send = (command >> 12) & 0xf;
80 int ret = i8042_wait_write();
83 outb(command, PORT_PS2_STATUS);
85 // Send parameters (if any).
87 for (i = 0; i < send; i++) {
88 ret = i8042_wait_write();
91 outb(param[i], PORT_PS2_DATA);
94 // Receive parameters (if any).
95 for (i = 0; i < receive; i++) {
96 ret = i8042_wait_read();
99 param[i] = inb(PORT_PS2_DATA);
100 dprintf(7, "i8042 param=%x\n", param[i]);
107 i8042_command(int command, u8 *param)
109 dprintf(7, "i8042_command cmd=%x\n", command);
110 int ret = __i8042_command(command, param);
112 dprintf(2, "i8042 command %x failed\n", command);
117 i8042_kbd_write(u8 c)
119 dprintf(7, "i8042_kbd_write c=%d\n", c);
120 int ret = i8042_wait_write();
122 outb(c, PORT_PS2_DATA);
127 i8042_aux_write(u8 c)
129 return i8042_command(I8042_CMD_AUX_SEND, &c);
135 if (! CONFIG_PS2PORT)
138 for (i=0; i<10; i++) {
141 outb(0xfe, PORT_PS2_STATUS); /* pulse reset low */
147 /****************************************************************
149 ****************************************************************/
151 #define PS2_RET_ACK 0xfa
152 #define PS2_RET_NAK 0xfe
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 // This data not part of command - just discard it.
176 dprintf(1, "Discarding ps2 data %02x (status=%02x)\n", data, status);
179 if (check_tsc(end)) {
180 // Don't warn on second byte of a reset
190 ps2_sendbyte(int aux, u8 command, int timeout)
192 dprintf(7, "ps2_sendbyte aux=%d cmd=%x\n", aux, command);
195 ret = i8042_aux_write(command);
197 ret = i8042_kbd_write(command);
202 ret = ps2_recvbyte(aux, 1, timeout);
205 if (ret != PS2_RET_ACK)
212 __ps2_command(int aux, int command, u8 *param)
215 int receive = (command >> 8) & 0xf;
216 int send = (command >> 12) & 0xf;
218 // Disable interrupts and keyboard/mouse.
219 u8 ps2ctr = GET_EBDA(ps2ctr);
220 u8 newctr = ((ps2ctr | I8042_CTR_AUXDIS | I8042_CTR_KBDDIS)
221 & ~(I8042_CTR_KBDINT|I8042_CTR_AUXINT));
222 dprintf(6, "i8042 ctr old=%x new=%x\n", ps2ctr, newctr);
223 int ret = i8042_command(I8042_CMD_CTL_WCTR, &newctr);
227 // Flush any interrupts already pending.
230 // Enable port command is being sent to.
232 newctr &= ~I8042_CTR_AUXDIS;
234 newctr &= ~I8042_CTR_KBDDIS;
235 ret = i8042_command(I8042_CMD_CTL_WCTR, &newctr);
239 if (command == ATKBD_CMD_RESET_BAT) {
240 // Reset is special wrt timeouts and bytes received.
243 ret = ps2_sendbyte(aux, command, 1000);
247 // Receive parameters.
248 ret = ps2_recvbyte(aux, 0, 4000);
252 ret = ps2_recvbyte(aux, 0, 100);
254 // Some devices only respond with one byte on reset.
257 } else if (command == ATKBD_CMD_GETID) {
258 // Getid is special wrt bytes received.
261 ret = ps2_sendbyte(aux, command, 200);
265 // Receive parameters.
266 ret = ps2_recvbyte(aux, 0, 500);
270 if (ret == 0xab || ret == 0xac || ret == 0x2b || ret == 0x5d
271 || ret == 0x60 || ret == 0x47) {
272 // These ids (keyboards) return two bytes.
273 ret = ps2_recvbyte(aux, 0, 500);
282 ret = ps2_sendbyte(aux, command, 200);
286 // Send parameters (if any).
288 for (i = 0; i < send; i++) {
289 ret = ps2_sendbyte(aux, param[i], 200);
294 // Receive parameters (if any).
295 for (i = 0; i < receive; i++) {
296 ret = ps2_recvbyte(aux, 0, 500);
306 // Restore interrupts and keyboard/mouse.
307 ret2 = i8042_command(I8042_CMD_CTL_WCTR, &ps2ctr);
315 ps2_command(int aux, int command, u8 *param)
317 dprintf(7, "ps2_command aux=%d cmd=%x\n", aux, command);
318 int ret = __ps2_command(aux, command, param);
320 dprintf(2, "ps2 command %x failed (aux=%d)\n", command, aux);
325 ps2_kbd_command(int command, u8 *param)
327 if (! CONFIG_PS2PORT)
329 return ps2_command(0, command, param);
333 ps2_mouse_command(int command, u8 *param)
335 if (! CONFIG_PS2PORT)
338 // Update ps2ctr for mouse enable/disable.
339 if (command == PSMOUSE_CMD_ENABLE || command == PSMOUSE_CMD_DISABLE) {
340 u16 ebda_seg = get_ebda_seg();
341 u8 ps2ctr = GET_EBDA2(ebda_seg, ps2ctr);
342 if (command == PSMOUSE_CMD_ENABLE)
343 ps2ctr = (ps2ctr | I8042_CTR_AUXINT) & ~I8042_CTR_AUXDIS;
345 ps2ctr = (ps2ctr | I8042_CTR_AUXDIS) & ~I8042_CTR_AUXINT;
346 SET_EBDA2(ebda_seg, ps2ctr, ps2ctr);
349 return ps2_command(1, command, param);
353 /****************************************************************
355 ****************************************************************/
357 // INT74h : PS/2 mouse hardware interrupt
361 if (! CONFIG_PS2PORT)
364 debug_isr(DEBUG_ISR_74);
366 u8 v = inb(PORT_PS2_STATUS);
367 if ((v & (I8042_STR_OBF|I8042_STR_AUXDATA))
368 != (I8042_STR_OBF|I8042_STR_AUXDATA)) {
369 dprintf(1, "ps2 mouse irq but no mouse data.\n");
372 v = inb(PORT_PS2_DATA);
374 if (!(GET_EBDA(ps2ctr) & I8042_CTR_AUXINT))
375 // Interrupts not enabled.
384 // INT09h : Keyboard Hardware Service Entry Point
388 if (! CONFIG_PS2PORT)
391 debug_isr(DEBUG_ISR_09);
393 // read key from keyboard controller
394 u8 v = inb(PORT_PS2_STATUS);
395 if (v & I8042_STR_AUXDATA) {
396 dprintf(1, "ps2 keyboard irq but found mouse data?!\n");
399 v = inb(PORT_PS2_DATA);
401 if (!(GET_EBDA(ps2ctr) & I8042_CTR_KBDINT))
402 // Interrupts not enabled.
412 /****************************************************************
414 ****************************************************************/
417 keyboard_init(void *data)
419 /* flush incoming keys */
420 int ret = i8042_flush();
424 // Controller self-test.
426 ret = i8042_command(I8042_CMD_CTL_TEST, param);
429 if (param[0] != 0x55) {
430 dprintf(1, "i8042 self test failed (got %x not 0x55)\n", param[0]);
434 // Controller keyboard test.
435 ret = i8042_command(I8042_CMD_KBD_TEST, param);
438 if (param[0] != 0x00) {
439 dprintf(1, "i8042 keyboard test failed (got %x not 0x00)\n", param[0]);
443 // Disable keyboard and mouse events.
444 SET_EBDA(ps2ctr, I8042_CTR_KBDDIS | I8042_CTR_AUXDIS);
447 /* ------------------- keyboard side ------------------------*/
448 /* reset keyboard and self test (keyboard side) */
449 int spinupdelay = romfile_loadint("etc/ps2-keyboard-spinup", 0);
450 u64 end = calc_future_tsc(spinupdelay);
452 ret = ps2_kbd_command(ATKBD_CMD_RESET_BAT, param);
455 if (check_tsc(end)) {
462 if (param[0] != 0xaa) {
463 dprintf(1, "keyboard self test failed (got %x not 0xaa)\n", param[0]);
467 /* Disable keyboard */
468 ret = ps2_kbd_command(ATKBD_CMD_RESET_DIS, NULL);
472 // Set scancode command (mode 2)
474 ret = ps2_kbd_command(ATKBD_CMD_SSCANSET, param);
478 // Keyboard Mode: disable mouse, scan code convert, enable kbd IRQ
479 SET_EBDA(ps2ctr, I8042_CTR_AUXDIS | I8042_CTR_XLATE | I8042_CTR_KBDINT);
481 /* Enable keyboard */
482 ret = ps2_kbd_command(ATKBD_CMD_ENABLE, NULL);
486 dprintf(1, "PS2 keyboard initialized\n");
493 if (! CONFIG_PS2PORT)
495 dprintf(3, "init ps2port\n");
497 enable_hwirq(1, FUNC16(entry_09));
498 enable_hwirq(12, FUNC16(entry_74));
500 run_thread(keyboard_init, NULL);