Repository navigation
Expand file tree
/
Copy pathtouchpad.rs
More file actions
209 lines (184 loc) · 7.35 KB
/
Copy pathtouchpad.rs
File metadata and controls
209 lines (184 loc) · 7.35 KB
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
use hidapi::{HidApi, HidDevice, HidError};
use log::Level;
pub const PIX_VID: u16 = 0x093A;
pub const P274_REPORT_ID: u8 = 0x43;
pub const P239_REPORT_ID: u8 = 0x42;
// Standard HID Precision Touchpad (PTP) interface — every PTP-compliant touchpad
// reports on this usage. Only haptic touchpads expose the feature reports below.
const TOUCHPAD_USAGE_PAGE: u16 = 0x000D; // Digitizers
const TOUCHPAD_USAGE: u16 = 0x0005; // Touch Pad
// Haptic feedback intensity (HID Haptic page 0x0E, Usage 0x23 Intensity).
// Descriptor says logical range 0..100, but the Boreas haptic firmware
// only implements five steps: 0%, 25%, 50%, 75%, 100%.
const HAPTIC_INTENSITY_REPORT_ID: u8 = 0x09;
pub const HAPTIC_INTENSITY_LEVELS: [u8; 5] = [0, 25, 50, 75, 100];
// Button press threshold / click force (HID Digitizer page 0x0D, Usage 0xB0).
// Added in HUTRR11, included in HUT 1.4
// 2-bit field, firmware accepts 1=Low, 2=Medium, 3=High.
const CLICK_FORCE_REPORT_ID: u8 = 0x08;
#[derive(Clone, Copy, Debug, PartialEq)]
#[repr(u8)]
pub enum ClickForce {
Low = 1,
Medium = 2,
High = 3,
}
/// Open the PTP HID interface of the touchpad. Note: every modern touchpad
/// exposes this interface; only haptic touchpads respond to the feature
/// reports used by `set_haptic_intensity` / `set_click_force`.
fn open_haptic_touchpad() -> Option<HidDevice> {
let api = HidApi::new().ok()?;
for dev_info in api.device_list() {
if dev_info.usage_page() != TOUCHPAD_USAGE_PAGE || dev_info.usage() != TOUCHPAD_USAGE {
continue;
}
debug!(
" Touchpad candidate {:04X}:{:04X} (Usage Page {:04X}, Usage {:04X})",
dev_info.vendor_id(),
dev_info.product_id(),
dev_info.usage_page(),
dev_info.usage()
);
if let Ok(device) = dev_info.open_device(&api) {
return Some(device);
}
}
None
}
// The firmware accepts SET_FEATURE for these reports but doesn't reply
// to GET_FEATURE, so both controls are write-only.
fn hid_err(message: impl Into<String>) -> HidError {
HidError::HidApiError {
message: message.into(),
}
}
pub fn set_haptic_intensity(value: u8) -> Result<(), HidError> {
if !HAPTIC_INTENSITY_LEVELS.contains(&value) {
return Err(hid_err(format!(
"Haptic intensity must be one of: {:?}",
HAPTIC_INTENSITY_LEVELS
)));
}
let device =
open_haptic_touchpad().ok_or_else(|| hid_err("Could not find a haptic touchpad"))?;
let buf = [HAPTIC_INTENSITY_REPORT_ID, value];
debug!(" send_feature_report (haptic intensity) {:X?}", buf);
device.send_feature_report(&buf)
}
pub fn set_click_force(force: ClickForce) -> Result<(), HidError> {
let device =
open_haptic_touchpad().ok_or_else(|| hid_err("Could not find a haptic touchpad"))?;
// Field is 2 bits at the bottom of the report payload
let buf = [CLICK_FORCE_REPORT_ID, force as u8];
debug!(" send_feature_report (click force) {:X?}", buf);
device.send_feature_report(&buf)
}
fn read_byte(device: &HidDevice, report_id: u8, addr: u8) -> Result<u8, HidError> {
device.send_feature_report(&[report_id, addr, 0x10, 0])?;
let mut buf = [0u8; 4];
buf[0] = report_id;
device.get_feature_report(&mut buf)?;
Ok(buf[3])
}
fn read_239_ver(device: &HidDevice) -> Result<u16, HidError> {
Ok(u16::from_le_bytes([
read_byte(device, P239_REPORT_ID, 0x16)?,
read_byte(device, P239_REPORT_ID, 0x18)?,
]))
}
fn read_274_ver(device: &HidDevice) -> Result<u16, HidError> {
Ok(u16::from_le_bytes([
read_byte(device, P274_REPORT_ID, 0xb2)?,
read_byte(device, P274_REPORT_ID, 0xb3)?,
]))
}
fn read_360_ver(device: &HidDevice) -> Result<u16, HidError> {
Ok(u16::from_le_bytes([
read_byte(device, P274_REPORT_ID, 0x7e)?,
read_byte(device, P274_REPORT_ID, 0x7f)?,
]))
}
pub fn print_touchpad_fw_ver() -> Result<(), HidError> {
debug!("Looking for touchpad HID device");
match HidApi::new() {
Ok(api) => {
for dev_info in api.device_list() {
let vid = dev_info.vendor_id();
let pid = dev_info.product_id();
let usage_page = dev_info.usage_page();
let hid_ver = dev_info.release_number();
debug!(
" Found {:04X}:{:04X} (Usage Page {:04X})",
vid, pid, usage_page
);
if vid != PIX_VID
|| (pid != 0x0274 && pid != 0x0239 && pid != 0x0360 && pid != 0x0343)
{
debug!(
" Skipping VID:PID. Expected {:04X}:{:04X}/{:04X}/{:04X}",
PIX_VID, 0x0274, 0x0239, 0x0343
);
continue;
}
if usage_page != 0xFF00 {
debug!(" Skipping usage page. Expected {:04X}", 0xFF00);
continue;
}
debug!(" Found matching touchpad HID device");
let device = dev_info.open_device(&api).unwrap();
println!("Touchpad");
info!(" IC Type: {:04X}", pid);
let ver = match pid {
0x0239 => format!("{:04X}", read_239_ver(&device)?),
0x0274 => format!("{:04X}", read_274_ver(&device)?),
0x0343 => format!("{:04X}", read_274_ver(&device)?),
0x0360 => format!("{:04X}", read_360_ver(&device)?),
_ => "Unsupported".to_string(),
};
println!(" Firmware Version: v{}", ver);
if log_enabled!(Level::Debug) {
println!(" Config space 1");
print!(" ");
for x in 0..16 {
print!("0{:X} ", x);
}
println!();
for y in 0..16 {
print!("{:X}0 ", y);
for x in 0..16 {
print!("{:02X} ", read_byte(&device, 0x42, x + 16 * y)?);
}
println!();
}
println!(" Config space 2");
print!(" ");
for x in 0..16 {
print!("0{:X} ", x);
}
println!();
for y in 0..16 {
print!("{:X}0 ", y);
for x in 0..16 {
print!("{:02X} ", read_byte(&device, 0x43, x + 16 * y)?);
}
println!();
}
}
// Linux does not expose a useful version number for I2C HID devices
#[cfg(target_os = "linux")]
debug!(" HID Version {:04X}", hid_ver);
#[cfg(not(target_os = "linux"))]
if ver != format!("{:04X}", hid_ver) || log_enabled!(Level::Debug) {
println!(" HID Version v{:04X}", hid_ver);
}
// If we found one, there's no need to look for more
return Ok(());
}
}
Err(e) => {
eprintln!("Failed to open hidapi. Error: {e}");
return Err(e);
}
};
Ok(())
}