-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtouchpad.rs
More file actions
140 lines (119 loc) · 4.14 KB
/
touchpad.rs
File metadata and controls
140 lines (119 loc) · 4.14 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
#[cfg(not(target_os = "freebsd"))]
use hidapi::{HidApi, HidDevice, HidError};
#[cfg(target_os = "freebsd")]
use crate::freebsd_hid::*;
#[cfg(target_os = "freebsd")]
use std::os::fd::AsRawFd;
pub const PIX_VID: u16 = 0x093A;
pub const TP_PID: u16 = 0x0274;
pub const PIX_REPORT_ID: u8 = 0x43;
#[cfg(target_os = "freebsd")]
pub fn print_touchpad_fw_ver() -> Option<()> {
if let Some(file) = hidraw_open(PIX_VID, TP_PID) {
println!("Touchpad");
unsafe {
let fd = file.as_raw_fd();
let mut desc = HidIocGrInfo {
bustype: 0,
vendor: 0,
product: 0,
};
if let Err(err) = hidiocgrawninfo(fd, &mut desc) {
error!("Failed to call hidiocgrawninfo: {}", err);
return None;
}
println!(" IC Type: {:04X}", desc.product);
let mut buf = [0u8; 255];
if let Err(err) = hid_raw_name(fd, &mut buf) {
error!("Failed to call hid_raw_name: {}", err);
return None;
}
let name = std::str::from_utf8(&buf)
.unwrap()
.trim_end_matches(char::from(0));
debug!(" Name: {}", name);
println!(" Firmware Version: v{:04X}", read_ver(fd)?);
read_byte(fd, 0x2b);
}
}
Some(())
}
fn read_byte(fd: i32, addr: u8) -> Option<u8> {
unsafe {
let mut buf: [u8; 4] = [PIX_REPORT_ID, addr, 0x10, 0];
if let Err(err) = hid_set_feature(fd, &mut buf) {
error!("Failed to hid_set_feature: {:?}", err);
return None;
}
//device.send_feature_report(&[PIX_REPORT_ID, addr, 0x10, 0])?;
let mut buf = [0u8; 4];
buf[0] = PIX_REPORT_ID;
if let Err(err) = hid_get_feature(fd, &mut buf) {
error!("Failed to hid_get_feature: {:?}", err);
return None;
}
Some(buf[3])
}
}
#[cfg(target_os = "freebsd")]
fn read_ver(device: i32) -> Option<u16> {
Some(u16::from_le_bytes([
read_byte(device, 0xb2)?,
read_byte(device, 0xb3)?,
]))
}
#[cfg(not(target_os = "freebsd"))]
fn read_byte(device: &HidDevice, addr: u8) -> Result<u8, HidError> {
device.send_feature_report(&[PIX_REPORT_ID, addr, 0x10, 0])?;
let mut buf = [0u8; 4];
buf[0] = PIX_REPORT_ID;
device.get_feature_report(&mut buf)?;
Ok(buf[3])
}
#[cfg(not(target_os = "freebsd"))]
fn read_ver(device: &HidDevice) -> Result<u16, HidError> {
Ok(u16::from_le_bytes([
read_byte(device, 0xb2)?,
read_byte(device, 0xb3)?,
]))
}
#[cfg(not(target_os = "freebsd"))]
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();
debug!(
" Found {:04X}:{:04X} (Usage Page {:04X})",
vid, pid, usage_page
);
if vid != PIX_VID || (pid != 0x0274 && pid != 0x0239) {
debug!(
" Skipping VID:PID. Expected {:04X}:{:04X}/{:04X}",
PIX_VID, 0x0274, 0x0239
);
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");
println!(" IC Type: {:04X}", pid);
println!(" Firmware Version: v{:04X}", read_ver(&device)?);
// 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(())
}