11// UnderDevelopment: Not in functional state.
2+ // It's NOT Working :(...
23// Also keyboard is depending lib/utils which depends on drivers :(
4+ #include <drivers/keyboard/keyboard.h>
35#include <lib/utils/io.h>
6+ #include <lib/utils/time.h>
47#include <lib/utils/panic.h>
58
69#define DRIVERS_KEYBOARD_PORT_DATA 0x60
710#define DRIVERS_KEYBOARD_PORT_STATUS 0x64
811#define DRIVERS_KEYBOARD_PORT_COMMAND 0x64
912
13+
14+ #define STATUS_OUTPUT_BUFFER 0x1
15+ #define STATUS_INPUT_BUFFER 0x2
16+ #define STATUS_SYSTEM_FLAG 0x4
17+ #define STATUS_CMD_DATA 0x8 // 0:cmd 1:data
18+
19+
1020// Using USB Legacy Support
1121extern void port_write (unsigned short port , unsigned char value );
1222extern unsigned char port_read (unsigned short port );
@@ -24,36 +34,195 @@ void print_status(const char *message, int status) {
2434 }
2535}
2636
37+ void sleep_mini (int s ) {
38+ while (s > 0 ) {
39+ s -- ;
40+ }
41+ }
42+ void wait_for_status_flag (unsigned char flag , unsigned char check_true ) {
43+ while ((((port_read (DRIVERS_KEYBOARD_PORT_STATUS ))& flag )!= 0 )^check_true );
44+ }
45+ void wait_for_status_flag_timeout (unsigned char flag , unsigned char check_true , int timeout ) {
46+ while (timeout > 0 ) {
47+ if ((((port_read (DRIVERS_KEYBOARD_PORT_STATUS ))& flag )!= 0 )^check_true ) {
48+ // waiting
49+ } else {
50+ // poll done
51+ return ;
52+ }
53+ timeout -- ;
54+ }
55+ if (check_true ) {
56+ PANIC (flag ,"TIMEOUT: status bit set" );
57+ } else {
58+ PANIC (flag ,"TIMEOUT: status bit reset" );
59+ }
60+ }
61+
62+ unsigned char send_command (unsigned char cmd , int want_reply ) {
63+ unsigned char out ;
64+ sleep_mini (100000 );
65+ port_write (DRIVERS_KEYBOARD_PORT_COMMAND , cmd );
66+ sleep_mini (100000 );
67+ if (!want_reply ) {
68+ return 0 ;
69+ }
70+ wait_for_status_flag (STATUS_OUTPUT_BUFFER , 1 );
71+ // wait_for_status_flag(STATUS_INPUT_BUFFER, 0);
72+ sleep_mini (100000 );
73+ unsigned char data = port_read (DRIVERS_KEYBOARD_PORT_DATA );
74+ return data ;
75+ }
76+
77+ unsigned char send_command_with_data (unsigned char cmd , unsigned char data , int want_reply ) {
78+ unsigned char out ;
79+
80+ port_write (DRIVERS_KEYBOARD_PORT_COMMAND , cmd );
81+ sleep_mini (1000000 );
82+ wait_for_status_flag (STATUS_INPUT_BUFFER , 0 );
83+ port_write (DRIVERS_KEYBOARD_PORT_DATA , data );
84+ sleep_mini (1000000 );
85+ if (!want_reply ) {
86+ return 0 ;
87+ }
88+ sleep_mini (1000000 );
89+ wait_for_status_flag (STATUS_OUTPUT_BUFFER , 1 );
90+ data = port_read (DRIVERS_KEYBOARD_PORT_DATA );
91+ return data ;
92+ }
93+ unsigned char read_reply () {
94+ sleep_mini (1000000 );
95+ wait_for_status_flag_timeout (STATUS_OUTPUT_BUFFER , 1 , 1000000 );
96+ return port_read (DRIVERS_KEYBOARD_PORT_DATA );
97+ }
98+
99+ unsigned char write_to_ps2_first_port (unsigned char byte , int want_reply ) {
100+ unsigned char out ;
101+ for (int i = 0 ;i < 3 ;i ++ ) {
102+ wait_for_status_flag_timeout (STATUS_INPUT_BUFFER , 0 , 1000000 );
103+ port_write (DRIVERS_KEYBOARD_PORT_DATA , byte );
104+ sleep_mini (1000000 );
105+ if (!want_reply ) {
106+ return 0 ;
107+ }
108+ wait_for_status_flag_timeout (STATUS_OUTPUT_BUFFER , 1 , 1000000 );
109+ out = port_read (DRIVERS_KEYBOARD_PORT_DATA );
110+ if (out != 0xFE ) {
111+ break ;
112+ }
113+ }
114+ return out ;
115+ }
116+ void write_to_ps2_second_port (unsigned char byte ) {
117+ port_write (DRIVERS_KEYBOARD_PORT_COMMAND , 0x64 );
118+ sleep_mini (1000000 );
119+ wait_for_status_flag_timeout (STATUS_INPUT_BUFFER , 0 , 1000000 );
120+ port_write (DRIVERS_KEYBOARD_PORT_DATA , byte );
121+ sleep_mini (1000000 );
122+ }
123+
27124void keyboard_init () {
28- print_status ( "Loading Keyboard Drivers..." , 0 );
125+ sleep_mini ( 30000000 );
29126 unsigned char out ;
30- port_write ( DRIVERS_KEYBOARD_PORT_COMMAND , 0xAD );
31- port_write ( DRIVERS_KEYBOARD_PORT_COMMAND , 0xA7 );
32- while ( 1 ) {
33- // clear the buffer
34- out = port_read ( DRIVERS_KEYBOARD_PORT_STATUS );
35- if (!( out & 1 )) break ;
36- }
37- port_write ( DRIVERS_KEYBOARD_PORT_COMMAND , 0x20 );
38- port_write ( DRIVERS_KEYBOARD_PORT_COMMAND , 0x60 );
39- out = port_read ( DRIVERS_KEYBOARD_PORT_STATUS );
127+
128+ // disable PS/2 first port
129+ send_command ( 0XAD , 0 );
130+ // disable PS/2 second port
131+ send_command ( 0XA7 , 0 );
132+ // flush output buffer
133+ wait_for_status_flag ( STATUS_OUTPUT_BUFFER , 0 );
134+
135+ out = send_command ( 0X20 , 1 );
136+ out = out & (~ 0b1000011 );
40137 int second_port_exists = 0 ;
41- if (out & (0b10000 )) {
138+ if (out & (0b10000 )) {
42139 second_port_exists = 1 ;
43- print_status ("Loading Keyboard Drivers: Second Port Exists" , 0 );
44140 }
45- // clear 0, 1 and 6.
46- // test bit 5
47- port_write (DRIVERS_KEYBOARD_PORT_COMMAND , 0xAA );
48- out = port_read (DRIVERS_KEYBOARD_PORT_STATUS );
141+ print_status ("PS2 Port Count is " , 1 + second_port_exists );
142+ send_command_with_data (0x60 , out , 0 );
143+ out = send_command (0XAA , 1 );
49144 if (out != 0x55 ) {
50145 PANIC (out , "PS2 self test failed." );
51146 }
52- // enable PS2 first port.
53- port_write (DRIVERS_KEYBOARD_PORT_COMMAND , 0xAE );
54- port_write (DRIVERS_KEYBOARD_PORT_COMMAND , 0xFF );
55- out = port_read (DRIVERS_KEYBOARD_PORT_STATUS );
147+
148+ if (second_port_exists ) {
149+ send_command (0XA8 , 0 );
150+ out = send_command (0X20 , 1 );
151+ if (!(out & (0b10000 ))) {
152+ // should be clear
153+ second_port_exists = 0 ;
154+ send_command (0xA7 , 0 );
155+ print_status ("PS2 second port disabled DNWAIS." , 0 );
156+ } else {
157+ // can't have dual channel
158+ print_status ("PS2 can't have dual channel." , 0 );
159+ }
160+ }
161+ out = send_command (0XAB , 1 );
162+ if (out != 0x00 ) {
163+ PANIC (out , "PS2 first port test failed." );
164+ }
165+ if (second_port_exists ) {
166+ out = send_command (0XA9 , 1 );
167+ if (out != 0x00 ) {
168+ PANIC (out , "PS2 second port test failed." );
169+ }
170+ }
171+ // enable first port
172+ write_to_ps2_first_port (0xAE , 0 );
173+ if (second_port_exists ) {
174+ // enable second port
175+ write_to_ps2_second_port (0xA8 );
176+ }
177+ // reset device
178+ out = write_to_ps2_first_port (0xFF , 1 );
56179 if (out != 0xFA ) {
57- PANIC (out , "PS2 reset failed. " );
180+ PANIC (out , "reset ps/2 first failed" );
58181 }
182+ // if(second_port_exists) {
183+ // // reset second port
184+ // write_to_ps2_first_port(0xFF);
185+ // out = read_reply();
186+ // if (out != 0xFA) {
187+ // PANIC(out, "reset ps/2 second failed");
188+ // }
189+ // }
190+
191+ // scan code
192+ // write_to_ps2_first_port(0xF0, 0);
193+ // out = write_to_ps2_first_port(0, 1);
194+ // if (out != 0xFA) {
195+ // PANIC(out, "failed to get scan code");
196+ // } else {
197+ // PANIC(out, "got get scan code");
198+ // }
199+ // write_to_ps2_first_port(0xF4);
200+
201+ // Caps
202+ write_to_ps2_first_port (0xED , 0 );
203+ out = write_to_ps2_first_port (7 , 1 );
204+ if (out != 0xFA ) {
205+ PANIC (out , "caps failed" );
206+ }
207+ // write_to_ps2_first_port(0xF4);
208+ // out = read_reply();
209+ // if (out != 0xFA) {
210+ // PANIC(out, "scan failed");
211+ // }
212+
213+ // // detect device type
214+ out = write_to_ps2_first_port (0xF5 , 1 );
215+ if (out != 0xFA ) {
216+ PANIC (out , "disable scanning failed." );
217+ }
218+ out = write_to_ps2_first_port (0xF2 , 1 );
219+ if (out != 0xFA ) {
220+ PANIC (out , "device identity failed." );
221+ }
222+ // 0xab
223+ unsigned char dev_d0 = read_reply ();
224+ unsigned char dev_d1 = read_reply ();
225+ PANIC (dev_d0 , "device id" );
226+
227+ PANIC (1 , "end" );
59228}
0 commit comments