Skip to content

Commit 8653ebc

Browse files
committed
freebsd: Extract ioctl hidraw to separate module
Signed-off-by: Daniel Schaefer <[email protected]>
1 parent b495286 commit 8653ebc

File tree

3 files changed

+95
-63
lines changed

3 files changed

+95
-63
lines changed

Diff for: framework_lib/src/freebsd_hid.rs

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
use nix::{ioctl_read, ioctl_read_buf, ioctl_readwrite, ioctl_readwrite_buf, ioctl_write_buf};
2+
use std::fs::OpenOptions;
3+
use std::io::{Read, Write};
4+
use std::os::fd::AsRawFd;
5+
use std::os::unix::fs::OpenOptionsExt;
6+
7+
#[repr(C)]
8+
pub struct HidIocGrInfo {
9+
pub bustype: u32,
10+
pub vendor: u16,
11+
pub product: u16,
12+
}
13+
14+
//ioctl_readwrite!(hidraw_get_report_desc, b'U', 21, HidrawGetReportDesc);
15+
//ioctl_readwrite!(hidraw_get_report, b'U', 23, HidrawGetReport);
16+
//ioctl_write!(hidraw_set_report, b'U', 24, HidrawSetReport);
17+
ioctl_read!(hidiocgrawninfo, b'U', 32, HidIocGrInfo);
18+
//ioctl_readwrite!(hidiocgrawnname, b'U', 33, HidIocGrName);
19+
ioctl_read_buf!(hid_raw_name, b'U', 33, u8);
20+
ioctl_write_buf!(hid_set_feature, b'U', 35, u8);
21+
ioctl_readwrite_buf!(hid_get_feature, b'U', 36, u8);
22+
23+
pub fn hidraw_open(vid: u16, pid: u16) -> Option<std::fs::File> {
24+
// TODO: List files in the directory
25+
for i in 0..32 {
26+
let path = format!("/dev/hidraw{}", i);
27+
let file = if let Ok(f) = OpenOptions::new()
28+
.read(true)
29+
.write(true)
30+
.custom_flags(libc::O_NONBLOCK)
31+
.open(&path)
32+
{
33+
f
34+
} else {
35+
debug!("{} not found", path);
36+
continue;
37+
};
38+
39+
let mut desc = HidIocGrInfo {
40+
bustype: 0,
41+
vendor: 0,
42+
product: 0,
43+
};
44+
unsafe {
45+
let fd = file.as_raw_fd();
46+
if let Err(err) = hidiocgrawninfo(fd, &mut desc) {
47+
error!("Failed to access hidraw at {}: {:?}", path, err);
48+
return None;
49+
}
50+
debug!(
51+
"Found {:04X}:{:04X} Bustype: {:04X}",
52+
desc.vendor, desc.product, desc.bustype
53+
);
54+
if desc.vendor == vid && desc.product == pid {
55+
return Some(file);
56+
}
57+
}
58+
}
59+
error!("No matching hidraw found. Is the hidraw kernel module loaded?");
60+
None
61+
}

Diff for: framework_lib/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ extern crate log;
1616
pub mod audio_card;
1717
#[cfg(feature = "rusb")]
1818
pub mod camera;
19+
#[cfg(target_os = "freebsd")]
20+
pub mod freebsd_hid;
1921
#[cfg(feature = "hidapi")]
2022
pub mod touchpad;
2123
#[cfg(feature = "hidapi")]

Diff for: framework_lib/src/touchpad.rs

+32-63
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,47 @@
1+
#[cfg(not(target_os = "freebsd"))]
12
use hidapi::{HidApi, HidDevice, HidError};
2-
#[cfg(target_os = "freebsd")]
3-
use nix::{ioctl_read, ioctl_read_buf, ioctl_readwrite, ioctl_readwrite_buf, ioctl_write_buf};
4-
#[cfg(target_os = "freebsd")]
5-
use std::fs::OpenOptions;
6-
use std::io::{Read, Write};
7-
#[cfg(target_os = "freebsd")]
8-
use std::os::fd::AsRawFd;
9-
#[cfg(target_os = "freebsd")]
10-
use std::os::unix::fs::OpenOptionsExt;
113

124
#[cfg(target_os = "freebsd")]
13-
#[repr(C)]
14-
pub struct HidIocGrInfo {
15-
bustype: u32,
16-
vendor: u16,
17-
product: u16,
18-
}
19-
5+
use crate::freebsd_hid::*;
206
#[cfg(target_os = "freebsd")]
21-
//ioctl_readwrite!(hidraw_get_report_desc, b'U', 21, HidrawGetReportDesc);
22-
//ioctl_readwrite!(hidraw_get_report, b'U', 23, HidrawGetReport);
23-
//ioctl_write!(hidraw_set_report, b'U', 24, HidrawSetReport);
24-
ioctl_read!(hidiocgrawninfo, b'U', 32, HidIocGrInfo);
25-
//ioctl_readwrite!(hidiocgrawnname, b'U', 33, HidIocGrName);
26-
ioctl_read_buf!(hid_raw_name, b'U', 33, u8);
27-
ioctl_write_buf!(hid_set_feature, b'U', 35, u8);
28-
ioctl_readwrite_buf!(hid_get_feature, b'U', 36, u8);
7+
use std::os::fd::AsRawFd;
298

309
pub const PIX_VID: u16 = 0x093A;
10+
pub const TP_PID: u16 = 0x0274;
3111
pub const PIX_REPORT_ID: u8 = 0x43;
3212

3313
#[cfg(target_os = "freebsd")]
3414
pub fn print_touchpad_fw_ver() -> Option<()> {
35-
println!("Touchpad");
36-
let path = "/dev/hidraw2";
37-
let mut file = OpenOptions::new()
38-
.read(true)
39-
.write(true)
40-
.custom_flags(libc::O_NONBLOCK)
41-
.open(path)
42-
.unwrap();
43-
44-
let mut desc = HidIocGrInfo {
45-
bustype: 0,
46-
vendor: 0,
47-
product: 0,
48-
};
49-
unsafe {
50-
let fd = file.as_raw_fd();
51-
if let Err(err) = hidiocgrawninfo(fd, &mut desc) {
52-
error!("Failed to access hidraw at {}: {:?}", path, err);
53-
return None;
54-
}
55-
debug!(
56-
"Found {:04X}:{:04X} Bustype: {:04X}",
57-
desc.vendor, desc.product, desc.bustype
58-
);
59-
// TODO: Iterate over all /dev/hidraw* devices and find the right one
60-
// if vid != PIX_VID || (pid != 0x0274 && pid != 0x0239) {
61-
println!(" IC Type: {:04X}", desc.product);
62-
63-
let mut buf = [0u8; 255];
64-
if let Err(err) = hid_raw_name(fd, &mut buf) {
65-
error!("Failed to access hidraw at {}: {:?}", path, err);
66-
return None;
67-
}
68-
let name = std::str::from_utf8(&buf)
69-
.unwrap()
70-
.trim_end_matches(char::from(0));
71-
debug!(" Name: {}", name);
15+
if let Some(file) = hidraw_open(PIX_VID, TP_PID) {
16+
println!("Touchpad");
17+
unsafe {
18+
let fd = file.as_raw_fd();
19+
20+
let mut desc = HidIocGrInfo {
21+
bustype: 0,
22+
vendor: 0,
23+
product: 0,
24+
};
25+
if let Err(err) = hidiocgrawninfo(fd, &mut desc) {
26+
error!("Failed to call hidiocgrawninfo: {}", err);
27+
return None;
28+
}
29+
println!(" IC Type: {:04X}", desc.product);
30+
31+
let mut buf = [0u8; 255];
32+
if let Err(err) = hid_raw_name(fd, &mut buf) {
33+
error!("Failed to call hid_raw_name: {}", err);
34+
return None;
35+
}
36+
let name = std::str::from_utf8(&buf)
37+
.unwrap()
38+
.trim_end_matches(char::from(0));
39+
debug!(" Name: {}", name);
7240

73-
println!(" Firmware Version: v{:04X}", read_ver(fd)?);
41+
println!(" Firmware Version: v{:04X}", read_ver(fd)?);
7442

75-
read_byte(fd, 0x2b);
43+
read_byte(fd, 0x2b);
44+
}
7645
}
7746

7847
Some(())

0 commit comments

Comments
 (0)