Skip to content

Commit 74bdc2e

Browse files
committed
[driver][ps2] optinally wait for all bytes during device detect
1 parent fa8d51d commit 74bdc2e

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

src/drivers/ps2/ps2.c

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,20 @@ void ps2_controller_wait_for_full_output() {
4747
// output buffer should not be empty now.
4848
}
4949

50+
int ps2_controller_wait_for_full_output_optional() {
51+
int try = 5;
52+
while(try--) {
53+
if(((inputb(PORT_PS2_STATUS)>>STATUS_OUTPUT_BUFFER_INDEX) & 1 ) == EMPTY) {
54+
// waiting
55+
} else {
56+
// wait over
57+
return 1;
58+
}
59+
}
60+
// timeout
61+
return 0;
62+
}
63+
5064
static void ps2_cmd(uint8_t cmd) {
5165
ps2_controller_wait_for_empty_input();
5266
outb(PORT_PS2_CMD, cmd);
@@ -70,6 +84,14 @@ uint8_t ps2_read_data() {
7084
return inputb(PORT_PS2_DATA);
7185
}
7286

87+
uint8_t ps2_read_data_optional() {
88+
int found = ps2_controller_wait_for_full_output_optional();
89+
if(found) {
90+
return inputb(PORT_PS2_DATA);
91+
}
92+
return 0;
93+
}
94+
7395
void ps2_init() {
7496
uint8_t out;
7597

@@ -184,12 +206,16 @@ void ps2_init() {
184206
if (out!=0xFA) {
185207
PANIC(out, LOG_PREFIX "[port1] identify device failed");
186208
}
187-
out = ps2_read_data(); // device type first byte
209+
// device type
210+
int dev_type0 = ps2_read_data();
211+
int dev_type1 = ps2_read_data_optional();
212+
int dev_type2 = ps2_read_data_optional();
188213
// ignoring second byte data, it should get cleared soon.
189-
if(out != 0xAB) {
214+
if(dev_type0 != 0xAB) {
190215
PANIC(out, "[port1] expected device keyboard not found.");
191216
}
192-
print_log(LOG_PREFIX "[port1] keyboard found");
217+
218+
print_log(LOG_PREFIX "[port1] keyboard found %x, %x, %x", dev_type0, dev_type1, dev_type2);
193219
return;
194220
}
195221

0 commit comments

Comments
 (0)