Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bonw15-b: TBT quirks, KBLED handling #436

Merged
merged 5 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/daemon/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::{
hid_backlight,
hotplug::{mux, Detect, HotPlugDetect},
kernel_parameters::{KernelParameter, NmiWatchdog},
runtime_pm::runtime_pm_quirks,
runtime_pm::{runtime_pm_quirks, thunderbolt_hotplug_wakeup},
DBUS_NAME, DBUS_PATH,
};

Expand Down Expand Up @@ -539,7 +539,9 @@ pub async fn daemon() -> anyhow::Result<()> {
}
}

match runtime_pm_quirks() {
let vendor = fs::read_to_string("/sys/class/dmi/id/sys_vendor")?;
let model = fs::read_to_string("/sys/class/dmi/id/product_version")?;
match runtime_pm_quirks(&model, &vendor) {
Ok(()) => (),
Err(err) => {
log::warn!("Failed to set runtime power management quirks: {}", err);
Expand Down Expand Up @@ -617,6 +619,15 @@ pub async fn daemon() -> anyhow::Result<()> {

fan_daemon.step();

// HACK: As of Linux 6.9.3, TBT5 controller must be active for HPD
// to work on USB-C ports.
match thunderbolt_hotplug_wakeup(&vendor, &model) {
Ok(()) => (),
Err(err) => {
log::warn!("Failed to wakeup thunderbolt on hotplug: {}", err);
}
}

let hpd = hpd();
for i in 0..hpd.len() {
if hpd[i] != last[i] && hpd[i] {
Expand Down
64 changes: 45 additions & 19 deletions src/hid_backlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use hidapi::{HidApi, HidDevice, HidResult};
use inotify::{Inotify, WatchMask};
use std::{fs, path::Path};

const USB_VID_ITE: u16 = 0x048d;

fn keyboard(device: &HidDevice, brightness: u8, color: u32) -> HidResult<()> {
// TODO: reset
let raw_brightness = ((u16::from(brightness) * 10 + 254) / 255) as u8;
Expand Down Expand Up @@ -73,43 +75,67 @@ pub fn daemon() {
}
};

let dir = Path::new("/sys/class/leds/system76_acpi::kbd_backlight");
let mut dir = Path::new("/sys/class/leds/system76_acpi::kbd_backlight");
if !dir.is_dir() {
// Check path from system76-dkms
dir = Path::new("/sys/class/leds/system76::kbd_backlight");
}
if !dir.is_dir() {
log::error!("hid_backlight: no system76_acpi::kbd_backlight led");
log::error!("hid_backlight: no kbd_backlight control");
return;
}

// TODO: check for existence of files
let mut inotify = Inotify::init().unwrap();
let mut watches = inotify.watches();

let brightness_file = dir.join("brightness");
if brightness_file.exists() {
watches.add(&brightness_file, WatchMask::MODIFY).unwrap();
}

let brightness_hw_changed_file = dir.join("brightness_hw_changed");
if brightness_hw_changed_file.exists() {
if let Err(e) = watches.add(&brightness_hw_changed_file, WatchMask::MODIFY) {
log::warn!("hid_backlight: failed to watch hardware changed file: {}", e);
}
}

let color_file = dir.join("color");
let color_left_file = dir.join("color_left");

let mut inotify = Inotify::init().unwrap();
let mut watches = inotify.watches();
watches.add(&brightness_file, WatchMask::MODIFY).unwrap();
if let Err(e) = watches.add(&brightness_hw_changed_file, WatchMask::MODIFY) {
log::warn!("hid_backlight: failed to watch hardware changed file: {}", e);
}
if let Err(e) = watches.add(&color_file, WatchMask::MODIFY) {
log::warn!("hid_backlight: failed to watch keyboard color: {}", e);
if color_file.exists() {
if let Err(e) = watches.add(&color_file, WatchMask::MODIFY) {
log::warn!("hid_backlight: failed to watch keyboard color: {}", e);
}
} else if color_left_file.exists() {
// XXX: Watch other zones?
watches.add(&color_left_file, WatchMask::MODIFY).unwrap();
}

let mut buffer = [0; 1024];
loop {
let brightness_string = fs::read_to_string(&brightness_file).unwrap();
let brightness = brightness_string.trim().parse::<u8>().unwrap();

let color_string = fs::read_to_string(&color_file)
// Fallback for non-colored keyboards
.unwrap_or_else(|_| String::from("FFFFFF"));
let brightness = if brightness_file.exists() {
let brightness_string = fs::read_to_string(&brightness_file).unwrap();
brightness_string.trim().parse::<u8>().unwrap()
} else {
0
};

let color_string = if color_file.exists() {
fs::read_to_string(&color_file).unwrap()
} else if color_left_file.exists() {
fs::read_to_string(&color_left_file).unwrap()
} else {
String::from("FFFFFF")
};
let color = u32::from_str_radix(color_string.trim(), 16).unwrap();

let mut devices = 0;

for info in api.device_list() {
let f = match (info.vendor_id(), info.product_id()) {
(0x048d, 0x8297) => lightguide,
(0x048d, 0x8910) => keyboard,
(USB_VID_ITE, 0x8297) => lightguide,
(USB_VID_ITE, 0x8910) => keyboard,
_ => continue,
};

Expand Down
32 changes: 27 additions & 5 deletions src/runtime_pm.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
use std::{fs, io};
use sysfs_class::{PciDevice, RuntimePM, RuntimePowerManagement, SysClass};

pub fn runtime_pm_quirks() -> io::Result<()> {
let vendor = fs::read_to_string("/sys/class/dmi/id/sys_vendor")?;
let model = fs::read_to_string("/sys/class/dmi/id/product_version")?;

pub fn runtime_pm_quirks(vendor: &str, model: &str) -> io::Result<()> {
match (vendor.trim(), model.trim()) {
("System76", "bonw15") | ("System76", "bonw15-b") => {
("System76", "bonw15") => {
for dev in PciDevice::all()? {
match (dev.vendor()?, dev.device()?) {
(0x8086, 0x1138) => {
Expand All @@ -20,8 +17,33 @@ pub fn runtime_pm_quirks() -> io::Result<()> {
}
}
}
("System76", "bonw15-b") => {
for dev in PciDevice::all()? {
match (dev.vendor()?, dev.device()?) {
(0x8086, 0x5782) => {
log::info!(
"Disabling runtime power management on Thunderbolt XHCI device at {:?}",
dev.path()
);
dev.set_runtime_pm(RuntimePowerManagement::Off)?;
}
_ => (),
}
}
}
_ => (),
}

Ok(())
}

pub fn thunderbolt_hotplug_wakeup(vendor: &str, model: &str) -> io::Result<()> {
match (vendor.trim(), model.trim()) {
("System76", "bonw15-b") => {
fs::read("/sys/kernel/debug/thunderbolt/0-0/regs")?;
}
(..) => {}
}

Ok(())
}
Loading