1- // UnderDevelopment: Not in functional state.
2- // It's NOT Working :(...
3- // Also keyboard is depending lib/utils which depends on drivers :(
41#include <drivers/keyboard/keyboard.h>
2+ #include <lib/ds/queue.h>
53#include <lib/utils/io.h>
64#include <lib/utils/time.h>
75#include <lib/utils/panic.h>
86
7+ #include "scancode_handler.c"
8+
99#define DRIVERS_KEYBOARD_PORT_DATA 0x60
1010#define DRIVERS_KEYBOARD_PORT_STATUS 0x64
1111#define DRIVERS_KEYBOARD_PORT_COMMAND 0x64
1616#define STATUS_SYSTEM_FLAG 0x4
1717#define STATUS_CMD_DATA 0x8 // 0:cmd 1:data
1818
19-
20- extern " C" {
21- // Using USB Legacy Support
22- extern void port_write (unsigned short port, unsigned char value);
23- extern unsigned char port_read (unsigned short port);
24- void __stack_chk_fail (void ) {
25- PANIC (0 , " Kernel stack overflow!!!" );
26- }
19+ // Using USB Legacy Support
20+ extern void port_write (unsigned short port , unsigned char value );
21+ extern unsigned char port_read (unsigned short port );
22+ void __stack_chk_fail (void ) {
23+ PANIC (0 , "Kernel stack overflow!!!" );
2724}
2825
2926void print_status (const char * message , int status ) {
@@ -50,26 +47,36 @@ void keyboard_wait() {
5047}
5148
5249const int WAIT_FOR_STATUS_TIMEOUT = 1000000 ;
50+ const int WAIT_FOR_STATUS_KEYSCANCODE_TIMEOUT = 1000 ;
5351
54- void wait_for_status_flag_timeout (unsigned char flag, unsigned char check_true, int timeout) {
52+ int wait_for_status_flag_timeout_low (unsigned char flag , unsigned char check_true , int timeout , int canpanic ) {
5553 while (timeout > 0 ) {
5654 if ((((port_read (DRIVERS_KEYBOARD_PORT_STATUS ))& flag )!= 0 )^check_true ) {
5755 // waiting
5856 } else {
5957 // poll done
60- return ;
58+ return 1 ;
6159 }
6260 timeout -- ;
6361 }
62+ if (!canpanic ) return 0 ;
6463 if (check_true ) {
6564 PANIC (flag ,"TIMEOUT: status bit set" );
6665 } else {
6766 PANIC (flag ,"TIMEOUT: status bit reset" );
6867 }
6968}
70-
71- void wait_for_status_flag (unsigned char flag, unsigned char check_true) {
72- wait_for_status_flag_timeout (flag, check_true, WAIT_FOR_STATUS_TIMEOUT);
69+ int wait_for_status_flag_timeout (unsigned char flag , unsigned char check_true , int timeout ) {
70+ return wait_for_status_flag_timeout_low (flag , check_true , timeout , 1 );
71+ }
72+ int wait_for_status_flag_nopanic (unsigned char flag , unsigned char check_true ) {
73+ return wait_for_status_flag_timeout_low (flag , check_true , WAIT_FOR_STATUS_TIMEOUT , 0 );
74+ }
75+ int wait_for_status_flag_nopanic_timeout (unsigned char flag , unsigned char check_true , int timeout ) {
76+ return wait_for_status_flag_timeout_low (flag , check_true , timeout , 0 );
77+ }
78+ int wait_for_status_flag (unsigned char flag , unsigned char check_true ) {
79+ return wait_for_status_flag_timeout_low (flag , check_true , WAIT_FOR_STATUS_TIMEOUT , 1 );
7380}
7481
7582unsigned char ps2_controller_send_command (unsigned char cmd , int want_reply ) {
@@ -129,11 +136,45 @@ unsigned char read_data_reply() {
129136 wait_for_status_flag_timeout (STATUS_OUTPUT_BUFFER , 1 , WAIT_FOR_STATUS_TIMEOUT );
130137 return port_read (DRIVERS_KEYBOARD_PORT_DATA );
131138}
132- extern " C" void enable_interrupts ();
133- extern " C" void disable_interrupts ();
134139
140+ #define KEYBOARD_BUFFER_SIZE 64
141+ int keyboard_buffer [KEYBOARD_BUFFER_SIZE + 3 ];
142+
143+ void keyboard_scanner_handle_buffer (int keyboard_buffer_queue []);
144+
145+ void keyboard_scanner_init () {
146+ ASSERT ( queue_init (keyboard_buffer , KEYBOARD_BUFFER_SIZE ) );
147+ }
148+
149+ int keyboard_scanner_step () {
150+ int state_change = 0 ;
151+ int qc = queue_capacity (keyboard_buffer );
152+ int qs = queue_size (keyboard_buffer );
153+ while (qs < qc ) {
154+ int got_response = wait_for_status_flag_nopanic_timeout (STATUS_OUTPUT_BUFFER , 1 , WAIT_FOR_STATUS_KEYSCANCODE_TIMEOUT );
155+ if (!got_response ) return ;
156+ unsigned char out = read_data_reply ();
157+ if (out == 0 ) return ;
158+ state_change = 1 ;
159+
160+ queue_push (keyboard_buffer , out );
161+ qs ++ ;
162+ }
163+ keyboard_scanner_handle_buffer (keyboard_buffer );
164+ return state_change ;
165+ }
166+
167+ int fake_getch () {
168+ while (queue_size (keyboard_buffer ) == 0 ) {
169+ // busy wait but checking on keyboard replies.
170+ keyboard_scanner_step ();
171+ }
172+ int out = queue_front (keyboard_buffer );
173+ queue_pop (keyboard_buffer );
174+ return out ;
175+ }
135176
136- extern " C " void keyboard_init () {
177+ void keyboard_init () {
137178 sleep_mini (3000000 );
138179
139180 unsigned char out ;
@@ -192,9 +233,9 @@ extern "C" void keyboard_init() {
192233 }
193234
194235 // enable interrupts
195- // out = ps2_controller_send_command(0X20, 1);
196- // out |= 0b11;
197- // ps2_controller_send_command_with_data(0x60, out, 0);
236+ out = ps2_controller_send_command (0X20 , 1 );
237+ out |= 0b11 ;
238+ ps2_controller_send_command_with_data (0x60 , out , 0 );
198239
199240
200241 // reset device
@@ -241,31 +282,28 @@ extern "C" void keyboard_init() {
241282 if (out != 0xFA ) {
242283 PANIC (out , "caps failed" );
243284 }
285+ // get scan code
286+ write_to_ps2_first_port (0xF0 , 0 );
287+ out = write_to_ps2_first_port (0 , 1 );
288+ if (out != 0xFA ) {
289+ PANIC (out , "failed to get scan code" );
290+ } else {
291+ // Not yet working
292+ // out = read_data_reply();
293+ // PANIC(out, "got get scan code");
294+ }
244295
245296 // set scan code
246297 write_to_ps2_first_port (0xF0 , 0 );
247- out = write_to_ps2_first_port (1 , 2 );
298+ out = write_to_ps2_first_port (2 , 1 );
248299 if (out != 0xFA ) {
249300 PANIC (out , "failed to set scan code" );
250301 }
251- // enable scanning
252- write_to_ps2_first_port (0xF4 , 0 );
253-
254- // get scan code
255- // write_to_ps2_first_port(0xF0, 0);
256- // out = write_to_ps2_first_port(0, 1);
257- // if (out != 0xFA) {
258- // PANIC(out, "failed to get scan code");
259- // } else {
260- // PANIC(out, "got get scan code");
261- // }
262- // write_to_ps2_first_port(0xF4, 0);
263302
303+ // enable scanning
264304 out = write_to_ps2_first_port (0xF4 , 1 );
265305 if (out != 0xFA ) {
266306 PANIC (out , "scan failed" );
267307 }
268-
269-
270- PANIC (1 , " end" );
308+ keyboard_scanner_init ();
271309}
0 commit comments