1 // All or parts of this file are from CHAOS (http://www.se.chaosdev.org/).
2 // CHAOS is also under the GNU General Public License.
6 #include "controller.h"
10 /* This reads the controller status port, and does the appropriate
11 action. It requires that we hold the keyboard controller spinlock. */
13 int controller_read_data(void);
14 unsigned handle_event(void)
16 unsigned status
= controller_read_status();
19 for(work
= 0; (work
< 10000) && ((status
& CONTROLLER_STATUS_OUTPUT_BUFFER_FULL
) != 0); work
++)
23 scancode
= controller_read_input();
26 /* Ignore error bytes. */
28 if((status
&(CONTROLLER_STATUS_GENERAL_TIMEOUT
|
29 CONTROLLER_STATUS_PARITY_ERROR
)) == 0)
32 if((status
& CONTROLLER_STATUS_MOUSE_OUTPUT_BUFFER_FULL
) != 0)
34 // mouse_handle_event(scancode); we just use the mouse handler directly..
38 // keyboard_handle_event(scancode);
42 status
= controller_read_status();
48 DbgPrint("PSAUX: Keyboard controller jammed\n");
54 /* Wait for keyboard controller input buffer to drain.
55 Quote from PS/2 System Reference Manual:
56 "Address hex 0060 and address hex 0064 should be written only
57 when the input-buffer-full bit and output-buffer-full bit in the
58 Controller Status register are set 0." */
60 void controller_wait(void)
62 unsigned long timeout
;
63 LARGE_INTEGER Millisecond_Timeout
;
65 Millisecond_Timeout
.QuadPart
= -10000L;
67 for(timeout
= 0; timeout
< CONTROLLER_TIMEOUT
; timeout
++)
69 // "handle_keyboard_event()" will handle any incoming events
70 // while we wait -- keypresses or mouse movement
72 unsigned char status
= handle_event();
74 if((status
& CONTROLLER_STATUS_INPUT_BUFFER_FULL
) == 0) return;
76 // Sleep for one millisecond
77 KeDelayExecutionThread (KernelMode
, FALSE
, &Millisecond_Timeout
);
80 DbgPrint("PSAUX: Keyboard timed out\n");
83 /* Wait for input from the keyboard controller. */
85 int controller_wait_for_input(void)
88 LARGE_INTEGER Millisecond_Timeout
;
90 Millisecond_Timeout
.QuadPart
= -10000L;
92 for(timeout
= KEYBOARD_INIT_TIMEOUT
; timeout
> 0; timeout
--)
94 int return_value
= controller_read_data();
96 if(return_value
>= 0) return return_value
;
98 // Sleep for one millisecond
99 KeDelayExecutionThread (KernelMode
, FALSE
, &Millisecond_Timeout
);
102 DbgPrint("PSAUX: Timed out on waiting for input from controller\n");
106 /* Write a command word to the keyboard controller. */
108 void controller_write_command_word(unsigned data
)
111 controller_write_command(data
);
114 /* Write an output word to the keyboard controller. */
116 void controller_write_output_word(unsigned data
)
119 controller_write_output(data
);
122 /* Empty the keyboard input buffer. */
124 void keyboard_clear_input(void)
128 for(max_read
= 0; max_read
< 100; max_read
++)
130 if(controller_read_data() == KEYBOARD_NO_DATA
)
137 /* Read data from the keyboard controller. */
139 int controller_read_data(void)
141 int return_value
= KEYBOARD_NO_DATA
;
144 status
= controller_read_status();
145 if(status
& CONTROLLER_STATUS_OUTPUT_BUFFER_FULL
)
147 unsigned data
= controller_read_input();
150 if(status
&(CONTROLLER_STATUS_GENERAL_TIMEOUT
|
151 CONTROLLER_STATUS_PARITY_ERROR
))
153 return_value
= KEYBOARD_BAD_DATA
;