1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
use crate::{
controller::Controller,
error::{ControllerError, KeyboardError},
flags::KeyboardLedFlags,
COMMAND_ACKNOWLEDGED, RESEND, SELF_TEST_FAILED, SELF_TEST_PASSED,
};
pub use self::keyboard_type::KeyboardType;
mod keyboard_type;
const BUFFER_OVERRUN: u8 = 0x00;
const ECHO: u8 = 0xee;
const KEY_DETECTION_ERROR: u8 = 0xff;
type Result<T> = core::result::Result<T, KeyboardError>;
#[repr(u8)]
enum Command {
SetLeds = 0xed,
Echo = 0xee,
GetOrSetScancode = 0xf0,
IdentifyKeyboard = 0xf2,
SetTypematicRateAndDelay = 0xf3,
EnableScanning = 0xf4,
DisableScanning = 0xf5,
SetDefaults = 0xf6,
SetAllKeysTypematic = 0xf7,
SetAllKeysMakeBreak = 0xf8,
SetAllKeysMakeOnly = 0xf9,
SetAllKeysTypematicAndMakeBreak = 0xfa,
SetKeyTypematic = 0xfb,
SetKeyMakeBreak = 0xfc,
SetKeyMakeOnly = 0xfd,
ResendLastByte = 0xfe,
ResetAndSelfTest = 0xff,
}
#[derive(Debug)]
pub struct Keyboard<'c> {
controller: &'c mut Controller,
}
impl<'c> Keyboard<'c> {
pub(crate) const fn new(controller: &'c mut Controller) -> Self {
Self { controller }
}
fn check_response(&mut self) -> Result<()> {
match self.controller.read_data()? {
BUFFER_OVERRUN => Err(KeyboardError::KeyDetectionError),
COMMAND_ACKNOWLEDGED => Ok(()),
RESEND => Err(KeyboardError::Resend),
KEY_DETECTION_ERROR => Err(KeyboardError::KeyDetectionError),
other => Err(KeyboardError::InvalidResponse(other)),
}
}
fn write_command(&mut self, command: Command, data: Option<u8>) -> Result<()> {
self.controller.write_data(command as u8)?;
self.check_response()?;
if let Some(data) = data {
self.controller.write_data(data as u8)?;
self.check_response()?;
}
Ok(())
}
pub fn set_leds(&mut self, leds: KeyboardLedFlags) -> Result<()> {
self.write_command(Command::SetLeds, Some(leds.bits()))
}
pub fn echo(&mut self) -> Result<()> {
self.controller.write_data(Command::Echo as u8)?;
match self.controller.read_data()? {
ECHO => Ok(()),
RESEND => Err(KeyboardError::Resend),
other => Err(KeyboardError::InvalidResponse(other)),
}
}
pub fn get_scancode_set(&mut self) -> Result<u8> {
self.write_command(Command::GetOrSetScancode, Some(0))?;
Ok(self.controller.read_data()?)
}
pub fn set_scancode_set(&mut self, scancode_set: u8) -> Result<()> {
self.write_command(Command::GetOrSetScancode, Some(scancode_set))?;
Ok(())
}
pub fn get_keyboard_type(&mut self) -> Result<KeyboardType> {
match self.write_command(Command::IdentifyKeyboard, None) {
Ok(()) => {}
Err(KeyboardError::Resend) => return Ok(KeyboardType::XT),
Err(other) => return Err(other.into()),
}
match self.controller.read_data() {
Ok(first_byte) => {
let second_byte = self.controller.read_data()?;
Ok(KeyboardType::from((first_byte, second_byte)))
}
Err(ControllerError::Timeout) => Ok(KeyboardType::ATWithTranslation),
Err(other) => return Err(other.into()),
}
}
pub fn set_typematic_rate_and_delay(&mut self, typematic_config: u8) -> Result<()> {
self.write_command(
Command::SetTypematicRateAndDelay,
Some(typematic_config & 0b01111111),
)
}
pub fn enable_scanning(&mut self) -> Result<()> {
self.write_command(Command::EnableScanning, None)
}
pub fn disable_scanning(&mut self) -> Result<()> {
self.write_command(Command::DisableScanning, None)
}
pub fn set_defaults(&mut self) -> Result<()> {
self.write_command(Command::SetDefaults, None)
}
pub fn set_all_keys_typematic(&mut self) -> Result<()> {
self.write_command(Command::SetAllKeysTypematic, None)
}
pub fn set_all_keys_make_break(&mut self) -> Result<()> {
self.write_command(Command::SetAllKeysMakeBreak, None)
}
pub fn set_all_keys_make_only(&mut self) -> Result<()> {
self.write_command(Command::SetAllKeysMakeOnly, None)
}
pub fn set_all_keys_typematic_make_break(&mut self) -> Result<()> {
self.write_command(Command::SetAllKeysTypematicAndMakeBreak, None)
}
pub fn set_key_typematic(&mut self, scancode: u8) -> Result<()> {
self.write_command(Command::SetKeyTypematic, Some(scancode))
}
pub fn set_key_make_break(&mut self, scancode: u8) -> Result<()> {
self.write_command(Command::SetKeyMakeBreak, Some(scancode))
}
pub fn set_key_make_only(&mut self, scancode: u8) -> Result<()> {
self.write_command(Command::SetKeyMakeOnly, Some(scancode))
}
pub fn resend_last_byte(&mut self) -> Result<u8> {
self.controller.write_data(Command::ResendLastByte as u8)?;
match self.controller.read_data()? {
RESEND => Err(KeyboardError::Resend),
byte => Ok(byte),
}
}
pub fn reset_and_self_test(&mut self) -> Result<()> {
self.write_command(Command::ResetAndSelfTest, None)?;
match self.controller.read_data()? {
SELF_TEST_PASSED => Ok(()),
SELF_TEST_FAILED => Err(KeyboardError::SelfTestFailed),
RESEND => Err(KeyboardError::Resend),
other => Err(KeyboardError::InvalidResponse(other)),
}
}
}