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 unsigned handle_event(void)
15 unsigned status = controller_read_status();
18 for(work = 0; (work < 10000) && ((status & CONTROLLER_STATUS_OUTPUT_BUFFER_FULL) != 0); work++)
22 scancode = controller_read_input();
25 /* Ignore error bytes. */
27 if((status &(CONTROLLER_STATUS_GENERAL_TIMEOUT |
28 CONTROLLER_STATUS_PARITY_ERROR)) == 0)
31 if((status & CONTROLLER_STATUS_MOUSE_OUTPUT_BUFFER_FULL) != 0)
33 // mouse_handle_event(scancode); we just use the mouse handler directly..
37 // keyboard_handle_event(scancode);
41 status = controller_read_status();
47 DbgPrint("PSAUX: Keyboard controller jammed\n");
53 /* Wait for keyboard controller input buffer to drain.
54 Quote from PS/2 System Reference Manual:
55 "Address hex 0060 and address hex 0064 should be written only
56 when the input-buffer-full bit and output-buffer-full bit in the
57 Controller Status register are set 0." */
59 void controller_wait(void)
61 unsigned long timeout;
62 LARGE_INTEGER Millisecond_Timeout;
64 Millisecond_Timeout.QuadPart = 1;
66 for(timeout = 0; timeout < CONTROLLER_TIMEOUT; timeout++)
68 // "handle_keyboard_event()" will handle any incoming events
69 // while we wait -- keypresses or mouse movement
71 unsigned char status = handle_event();
73 if((status & CONTROLLER_STATUS_INPUT_BUFFER_FULL) == 0) return;
75 // Sleep for one millisecond
76 KeDelayExecutionThread (KernelMode, FALSE, &Millisecond_Timeout);
79 DbgPrint("PSAUX: Keyboard timed out\n");
82 /* Wait for input from the keyboard controller. */
84 int controller_wait_for_input(void)
87 LARGE_INTEGER Millisecond_Timeout;
89 Millisecond_Timeout.QuadPart = 1;
91 for(timeout = KEYBOARD_INIT_TIMEOUT; timeout > 0; timeout--)
93 int return_value = controller_read_data();
95 if(return_value >= 0) return return_value;
97 // Sleep for one millisecond
98 KeDelayExecutionThread (KernelMode, FALSE, &Millisecond_Timeout);
101 DbgPrint("PSAUX: Timed out on waiting for input from controller\n");
105 /* Write a command word to the keyboard controller. */
107 void controller_write_command_word(unsigned data)
110 controller_write_command(data);
113 /* Write an output word to the keyboard controller. */
115 void controller_write_output_word(unsigned data)
118 controller_write_output(data);
121 /* Empty the keyboard input buffer. */
123 void keyboard_clear_input(void)
127 for(max_read = 0; max_read < 100; max_read++)
129 if(controller_read_data() == KEYBOARD_NO_DATA)
136 /* Read data from the keyboard controller. */
138 int controller_read_data(void)
140 int return_value = KEYBOARD_NO_DATA;
143 status = controller_read_status();
144 if(status & CONTROLLER_STATUS_OUTPUT_BUFFER_FULL)
146 unsigned data = controller_read_input();
149 if(status &(CONTROLLER_STATUS_GENERAL_TIMEOUT |
150 CONTROLLER_STATUS_PARITY_ERROR))
152 return_value = KEYBOARD_BAD_DATA;