Skip to content

Commit c649b4f

Browse files
committed
i8042の入力経路を調整
1 parent b4e657f commit c649b4f

1 file changed

Lines changed: 15 additions & 46 deletions

File tree

ps2/i8042-driver/src/lib.rs

Lines changed: 15 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,6 @@ const STATUS_OUTPUT_FULL: u8 = 1 << 0;
66
const STATUS_INPUT_FULL: u8 = 1 << 1;
77
const STATUS_AUX_DATA: u8 = 1 << 5;
88

9-
fn sign_extend(delta: u8, sign: bool) -> i16 {
10-
if sign {
11-
(delta as i16) - 256
12-
} else {
13-
delta as i16
14-
}
15-
}
16-
179
fn wait_input_clear(mut budget: usize) -> bool {
1810
while budget > 0 {
1911
match platform::port::in_u8(0x64) {
@@ -74,6 +66,10 @@ fn send_mouse_command(cmd: u8) -> bool {
7466
&& matches!(read_data_with_timeout(100_000), Some(0xFA))
7567
}
7668

69+
fn send_keyboard_command(cmd: u8) -> bool {
70+
write_data(cmd) && matches!(read_data_with_timeout(100_000), Some(0xFA))
71+
}
72+
7773
fn init_i8042() {
7874
flush_output_buffer();
7975

@@ -86,12 +82,15 @@ fn init_i8042() {
8682

8783
if write_command(0x20) {
8884
if let Some(config) = read_data_with_timeout(100_000) {
89-
let updated = (config | 0x03) & !0x30;
85+
// Enable IRQ1/IRQ12 and keyboard translation so the first port
86+
// produces set-1 scancodes expected by input.service.
87+
let updated = (config | 0x43) & !0x30;
9088
let _ = write_command(0x60);
9189
let _ = write_data(updated);
9290
}
9391
}
9492

93+
let _ = send_keyboard_command(0xF4);
9594
let _ = send_mouse_command(0xF6);
9695
let _ = send_mouse_command(0xF4);
9796
}
@@ -141,10 +140,7 @@ pub fn run(sp: *const usize) -> ! {
141140
platform::println!("i8042: start");
142141
init_i8042();
143142
let input_endpoint = unsafe { parse_endpoint_arg(sp) }.unwrap_or(0);
144-
145-
let mut mouse_buttons = 0u8;
146-
let mut mouse_packet = [0u8; 3];
147-
let mut mouse_packet_len = 0usize;
143+
let mut keyboard_log_budget = 8usize;
148144

149145
loop {
150146
let Ok(status) = platform::port::in_u8(0x64) else {
@@ -163,42 +159,15 @@ pub fn run(sp: *const usize) -> ! {
163159
};
164160

165161
if (status & STATUS_AUX_DATA) == 0 {
162+
if keyboard_log_budget > 0 {
163+
platform::println!("i8042: key byte=0x{:02x}", byte);
164+
keyboard_log_budget -= 1;
165+
}
166166
if input_endpoint != 0 {
167-
let _ = platform::ipc::send(input_endpoint, &[byte]);
167+
let packet = [platform::input::RAW_KIND_KEYBOARD, 0, 0, 0, byte, 0, 0, 0];
168+
let _ = platform::ipc::send(input_endpoint, &packet);
168169
}
169170
continue;
170171
}
171-
172-
if mouse_packet_len == 0 && (byte & 0x08) == 0 {
173-
continue;
174-
}
175-
mouse_packet[mouse_packet_len] = byte;
176-
mouse_packet_len += 1;
177-
if mouse_packet_len < 3 {
178-
continue;
179-
}
180-
mouse_packet_len = 0;
181-
182-
let b0 = mouse_packet[0];
183-
let b1 = mouse_packet[1];
184-
let b2 = mouse_packet[2];
185-
if (b0 & 0xC0) != 0 {
186-
continue;
187-
}
188-
189-
let buttons = b0 & 0x07;
190-
let x = sign_extend(b1, (b0 & 0x10) != 0);
191-
let dy = -sign_extend(b2, (b0 & 0x20) != 0);
192-
if x != 0 || dy != 0 {
193-
platform::println!("i8042: mouse moved dx={} dy={}", x, dy);
194-
}
195-
if buttons != mouse_buttons {
196-
platform::println!(
197-
"i8042: mouse buttons 0x{:02x} -> 0x{:02x}",
198-
mouse_buttons,
199-
buttons
200-
);
201-
mouse_buttons = buttons;
202-
}
203172
}
204173
}

0 commit comments

Comments
 (0)