Skip to content

Commit 5be2f94

Browse files
Handle strange PS/2 hardware better and more leniently (#884)
* Be a little less strict about adhering to PS/2 specs, as some hardware doesn't abide by typical behavior. * The PS/2 keyboard should now work on all real hardware, but the mouse support still isn't 100% correct on all systems.
1 parent db6f3a4 commit 5be2f94

File tree

3 files changed

+202
-105
lines changed

3 files changed

+202
-105
lines changed

kernel/keyboard/src/lib.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,13 @@ pub fn init(keyboard: PS2Keyboard<'static>, keyboard_queue_producer: Queue<Event
4747
}
4848

4949
// TODO: figure out what we should do, for now using set 1
50-
keyboard.keyboard_scancode_set(ScancodeSet::Set1)?;
50+
// Acer Aspire 7745G:
51+
// - allows setting Set 1 and also returns Set 1 on ScancodeSet::Get, but keyboard interrupts don't fire
52+
// - allows setting Set 2 and works
53+
// HP Pavilion 17(-ab)-f050ng:
54+
// - TODO
55+
// -> we can only try setting the default Set 2 and then try ScancodeSet::Get
56+
keyboard.set_keyboard_scancode_set(ScancodeSet::Set1)?;
5157

5258
// Register the interrupt handler
5359
interrupts::register_interrupt(PS2_KEYBOARD_IRQ, ps2_keyboard_handler).map_err(|e| {

kernel/mouse/src/lib.rs

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
#![no_std]
44
#![feature(abi_x86_interrupt)]
55

6-
use log::{error, warn, debug};
6+
use log::{error, warn};
77
use spin::Once;
88
use mpmc::Queue;
99
use event_types::Event;
1010
use x86_64::structures::idt::InterruptStackFrame;
1111
use mouse_data::{MouseButtons, MouseEvent, MouseMovementRelative};
12-
use ps2::{PS2Mouse, MouseId, MousePacket};
12+
use ps2::{PS2Mouse, MousePacket};
1313

1414
/// The first PS/2 port for the mouse is connected directly to IRQ 0xC.
1515
/// Because we perform the typical PIC remapping, the remapped IRQ vector number is 0x2C.
@@ -29,26 +29,10 @@ struct MouseInterruptParams {
2929
/// * `mouse_queue_producer`: the queue onto which the mouse interrupt handler
3030
/// will push new mouse events when a mouse action occurs.
3131
pub fn init(mut mouse: PS2Mouse<'static>, mouse_queue_producer: Queue<Event>) -> Result<(), &'static str> {
32-
//TODO: set to 3, failed? id = 0, otherwise set to 4, failed? id = 3, otherwise id = 4
33-
//the current code beneath just tries to set id = 4, so is not final
34-
// Set Mouse ID to 4.
35-
if let Err(e) = mouse.set_mouse_id(MouseId::Four) {
36-
error!("Failed to set the mouse id to four: {e}");
37-
return Err("Failed to set the mouse id to four");
38-
}
39-
// Read it back to check that it worked.
40-
match mouse.mouse_id() {
41-
Ok(id) => {
42-
debug!("The PS/2 mouse ID is: {id:?}");
43-
if !matches!(id, MouseId::Four) {
44-
error!("Failed to set the mouse id to four");
45-
return Err("Failed to set the mouse id to four");
46-
}
47-
}
48-
Err(e) => {
49-
error!("Failed to read the PS/2 mouse ID: {e}");
50-
return Err("Failed to read the PS/2 mouse ID");
51-
}
32+
// Set MouseId to the highest possible one
33+
if let Err(e) = mouse.set_mouse_id() {
34+
error!("Failed to set the mouse id: {e}");
35+
return Err("Failed to set the mouse id");
5236
}
5337

5438
// Register the interrupt handler
@@ -78,13 +62,12 @@ extern "x86-interrupt" fn ps2_mouse_handler(_stack_frame: InterruptStackFrame) {
7862
// try to redesign this to only get one byte per interrupt instead of the 3-4 bytes we
7963
// currently get in read_mouse_packet and merge them afterwards
8064
let mouse_packet = mouse.read_mouse_packet();
81-
// warn!("read_mouse_packet: {mouse_packet:?}");
8265

8366
if mouse_packet.always_one() != 1 {
8467
// this could signal a hardware error or a mouse which doesn't conform to the rule
85-
warn!("Discarding mouse data packet since its third bit should always be 1.");
68+
warn!("ps2_mouse_handler(): Discarding mouse data packet since its third bit should always be 1.");
8669
} else if let Err(e) = handle_mouse_input(mouse_packet, queue) {
87-
error!("ps2_mouse_handler: {e:?}");
70+
error!("ps2_mouse_handler(): {e:?}");
8871
}
8972
}
9073
} else {

0 commit comments

Comments
 (0)