1+ #ifndef __KERNEL_DRIVERS_KEYBOARD
2+ #define __KERNEL_DRIVERS_KEYBOARD
3+
4+ #include <kernel/essentials.c>
5+
6+ #define DRIVERS_KEYBOARD_PORT_DATA 0x60
7+ #define DRIVERS_KEYBOARD_PORT_STATUS 0x64
8+ #define DRIVERS_KEYBOARD_PORT_COMMAND 0x64
9+
10+ // Using USB Legacy Support
11+ extern void port_write (unsigned short port , unsigned char value );
12+ extern unsigned char port_read (unsigned short port );
13+
14+ void keyboard_init () {
15+ kernel_status ("Loading Keyboard Drivers..." , 0 );
16+ unsigned char out ;
17+ port_write (DRIVERS_KEYBOARD_PORT_COMMAND , 0xAD );
18+ port_write (DRIVERS_KEYBOARD_PORT_COMMAND , 0xA7 );
19+ while (1 ) {
20+ // clear the buffer
21+ out = port_read (DRIVERS_KEYBOARD_PORT_STATUS );
22+ if (!(out & 1 )) break ;
23+ }
24+ port_write (DRIVERS_KEYBOARD_PORT_COMMAND , 0x20 );
25+ port_write (DRIVERS_KEYBOARD_PORT_COMMAND , 0x60 );
26+ out = port_read (DRIVERS_KEYBOARD_PORT_STATUS );
27+ int second_port_exists = 0 ;
28+ if (out & (0b10000 )) {
29+ second_port_exists = 1 ;
30+ kernel_status ("Loading Keyboard Drivers: Second Port Exists" , 0 );
31+ }
32+ // clear 0, 1 and 6.
33+ // test bit 5
34+ port_write (DRIVERS_KEYBOARD_PORT_COMMAND , 0xAA );
35+ out = port_read (DRIVERS_KEYBOARD_PORT_STATUS );
36+ if (out != 0x55 ) {
37+ PANIC (out , "PS2 self test failed." );
38+ }
39+ // enable PS2 first port.
40+ port_write (DRIVERS_KEYBOARD_PORT_COMMAND , 0xAE );
41+ port_write (DRIVERS_KEYBOARD_PORT_COMMAND , 0xFF );
42+ out = port_read (DRIVERS_KEYBOARD_PORT_STATUS );
43+ if (out != 0xFA ) {
44+ PANIC (out , "PS2 reset failed." );
45+ }
46+ }
47+
48+ #endif
0 commit comments