Skip to content

Commit 84a79f9

Browse files
Merge pull request #97 from FrameworkComputer/touchpad-framework12
framework12: Support reading touchpad version
2 parents 6824174 + 6e1d411 commit 84a79f9

File tree

1 file changed

+64
-8
lines changed

1 file changed

+64
-8
lines changed

framework_lib/src/touchpad.rs

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,31 @@
11
use hidapi::{HidApi, HidDevice, HidError};
2+
use log::Level;
23

34
pub const PIX_VID: u16 = 0x093A;
4-
pub const PIX_REPORT_ID: u8 = 0x43;
5+
pub const P274_REPORT_ID: u8 = 0x43;
6+
pub const P239_REPORT_ID: u8 = 0x42;
57

6-
fn read_byte(device: &HidDevice, addr: u8) -> Result<u8, HidError> {
7-
device.send_feature_report(&[PIX_REPORT_ID, addr, 0x10, 0])?;
8+
fn read_byte(device: &HidDevice, report_id: u8, addr: u8) -> Result<u8, HidError> {
9+
device.send_feature_report(&[report_id, addr, 0x10, 0])?;
810

911
let mut buf = [0u8; 4];
10-
buf[0] = PIX_REPORT_ID;
12+
buf[0] = report_id;
1113

1214
device.get_feature_report(&mut buf)?;
1315
Ok(buf[3])
1416
}
1517

16-
fn read_ver(device: &HidDevice) -> Result<u16, HidError> {
18+
fn read_239_ver(device: &HidDevice) -> Result<u16, HidError> {
1719
Ok(u16::from_le_bytes([
18-
read_byte(device, 0xb2)?,
19-
read_byte(device, 0xb3)?,
20+
read_byte(device, P239_REPORT_ID, 0x16)?,
21+
read_byte(device, P239_REPORT_ID, 0x18)?,
22+
]))
23+
}
24+
25+
fn read_274_ver(device: &HidDevice) -> Result<u16, HidError> {
26+
Ok(u16::from_le_bytes([
27+
read_byte(device, P274_REPORT_ID, 0xb2)?,
28+
read_byte(device, P274_REPORT_ID, 0xb3)?,
2029
]))
2130
}
2231

@@ -28,6 +37,7 @@ pub fn print_touchpad_fw_ver() -> Result<(), HidError> {
2837
let vid = dev_info.vendor_id();
2938
let pid = dev_info.product_id();
3039
let usage_page = dev_info.usage_page();
40+
let hid_ver = dev_info.release_number();
3141

3242
debug!(
3343
" Found {:04X}:{:04X} (Usage Page {:04X})",
@@ -50,7 +60,53 @@ pub fn print_touchpad_fw_ver() -> Result<(), HidError> {
5060

5161
println!("Touchpad");
5262
println!(" IC Type: {:04X}", pid);
53-
println!(" Firmware Version: v{:04X}", read_ver(&device)?);
63+
64+
let ver = match pid {
65+
0x0239 => format!("{:04X}", read_239_ver(&device)?),
66+
0x0274 => format!("{:04X}", read_274_ver(&device)?),
67+
_ => "Unsupported".to_string(),
68+
};
69+
println!(" Firmware Version: v{}", ver);
70+
71+
if log_enabled!(Level::Debug) {
72+
println!(" Config space 1");
73+
print!(" ");
74+
for x in 0..16 {
75+
print!("0{:X} ", x);
76+
}
77+
println!();
78+
for y in 0..16 {
79+
print!("{:X}0 ", y);
80+
for x in 0..16 {
81+
print!("{:02X} ", read_byte(&device, 0x42, x + 16 * y)?);
82+
}
83+
println!();
84+
}
85+
println!(" Config space 2");
86+
print!(" ");
87+
for x in 0..16 {
88+
print!("0{:X} ", x);
89+
}
90+
println!();
91+
for y in 0..16 {
92+
print!("{:X}0 ", y);
93+
for x in 0..16 {
94+
print!("{:02X} ", read_byte(&device, 0x43, x + 16 * y)?);
95+
}
96+
println!();
97+
}
98+
}
99+
100+
// Linux does not expose a useful version number for I2C HID devices
101+
#[cfg(target_os = "linux")]
102+
debug!(" HID Version {:04X}", hid_ver);
103+
#[cfg(not(target_os = "linux"))]
104+
if ver != format!("{:04X}", hid_ver) {
105+
println!(" HID Version v{:04X}", hid_ver);
106+
} else if log_enabled!(Level::Debug) {
107+
println!(" HID Version v{:04X}", hid_ver);
108+
}
109+
54110
// If we found one, there's no need to look for more
55111
return Ok(());
56112
}

0 commit comments

Comments
 (0)