Skip to content
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
37 changes: 19 additions & 18 deletions src/usr/local/emhttp/plugins/fanctrlplus2/include/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,9 @@ function normalize_chip_name(string $chip): string {

function build_pwm_map(): array {
$map = [];
foreach (glob("/sys/class/hwmon/hwmon*") as $dir) {
$name_file = "$dir/name";
if (!is_file($name_file)) continue;

$chip = normalize_chip_name(trim(file_get_contents($name_file)));

foreach (glob("$dir/pwm[0-9]") as $pwm_path) {
$pwmN = basename($pwm_path);
$real = realpath($pwm_path) ?: $pwm_path;
$map["$chip:$pwmN"] = $real;
}
foreach (list_pwm() as $pwm) {
if ($pwm['chip'] === '') continue;
$map[normalize_chip_name($pwm['chip']).':'.$pwm['name']] = $pwm['sensor'];
}
return $map;
}
Expand Down Expand Up @@ -232,17 +224,26 @@ function migrate_cfg_and_labels(string $plugin): void {
// END: Migrate hwmonX (cfg+labels)
// ================================

function list_pwm() {
// The sensor is the resolved /sys/devices path: saved labels and controller
// settings use it, and the /sys/class/hwmon/hwmonN link follows probe order.
// Older drivers keep their attributes on the parent device instead.
// "pwm[0-9]*" would also match pwm1_enable and friends, hence the regex.
function list_pwm(string $hwmon_glob = '/sys/class/hwmon/hwmon*') {
$out = [];
exec("find /sys/devices -type f -iname 'pwm[0-9]' -exec dirname \"{}\" + | uniq", $chips);
Comment thread
andrebrait marked this conversation as resolved.
foreach ($chips as $chip) {
$name = is_file("$chip/name") ? trim(file_get_contents("$chip/name")) : '';
foreach (glob("$chip/pwm[0-9]") as $pwm) {
$out[] = ['chip' => $name, 'name' => basename($pwm), 'sensor' => $pwm];
foreach (glob($hwmon_glob) ?: [] as $hwmon) {
$name = '';
foreach ([$hwmon, "$hwmon/device"] as $dir) {
if (is_file("$dir/name")) $name = trim(file_get_contents("$dir/name"));
foreach (glob("$dir/pwm*") ?: [] as $pwm) {
if (!preg_match('/^pwm\d+$/', basename($pwm)) || !is_file($pwm)) continue;
$sensor = realpath($pwm) ?: $pwm;
$out[$sensor] = ['chip' => $name, 'name' => basename($pwm), 'sensor' => $sensor];
}
}
}

usort($out, fn($a, $b) => strcmp($a['name'], $b['name']));
$out = array_values($out);
usort($out, fn($a, $b) => strnatcmp($a['name'], $b['name']) ?: strcmp($a['sensor'], $b['sensor']));
return $out;
}

Expand Down
57 changes: 57 additions & 0 deletions tests/pwm_discovery_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

require_once __DIR__ . '/../src/usr/local/emhttp/plugins/fanctrlplus2/include/Common.php';

$failures = [];
$root = realpath(sys_get_temp_dir()) . '/fcp_pwm_discovery_' . getmypid();

$chip = function (string $hwmon, string $device, string $name, array $files) use ($root): void {
@mkdir("$root/devices/$device", 0777, true);
@mkdir("$root/class/hwmon", 0777, true);
symlink("$root/devices/$device", "$root/class/hwmon/$hwmon");
file_put_contents("$root/devices/$device/name", "$name\n");
foreach ($files as $f) touch("$root/devices/$device/$f");
};
$usb = 'usb1/1-14/1-14:1.0/0003:3904:F001.0005/hwmon/hwmon5';
$chip('hwmon5', $usb, 'arctic_fan_controller',
['pwm1', 'pwm2', 'pwm10', 'pwm1_enable', 'pwm10_enable', 'pwm1_auto_point1_pwm']);

// Older drivers put the attributes on the parent device; hwmonN is a stub
// that may or may not carry the name.
$legacy = function (string $hwmon, string $device, string $name, bool $nameOnDevice) use ($root, $chip): void {
@mkdir("$root/devices/$device/hwmon/$hwmon", 0777, true);
symlink("$root/devices/$device", "$root/devices/$device/hwmon/$hwmon/device");
$chip($hwmon, "$device/hwmon/$hwmon", $name, []);
if ($nameOnDevice) {
unlink("$root/devices/$device/hwmon/$hwmon/name");
file_put_contents("$root/devices/$device/name", "$name\n");
}
touch("$root/devices/$device/pwm1");
};
$legacy('hwmon2', 'platform/w83627hf.656', 'w83627hf', true);
$legacy('hwmon3', 'platform/it87.552', 'it8728', false);

$pwms = list_pwm("$root/class/hwmon/hwmon*");

$expected = [
['chip' => 'it8728', 'name' => 'pwm1', 'sensor' => "$root/devices/platform/it87.552/pwm1"],
['chip' => 'w83627hf', 'name' => 'pwm1', 'sensor' => "$root/devices/platform/w83627hf.656/pwm1"],
['chip' => 'arctic_fan_controller', 'name' => 'pwm1', 'sensor' => "$root/devices/$usb/pwm1"],
['chip' => 'arctic_fan_controller', 'name' => 'pwm2', 'sensor' => "$root/devices/$usb/pwm2"],
['chip' => 'arctic_fan_controller', 'name' => 'pwm10', 'sensor' => "$root/devices/$usb/pwm10"],
];
// Labels and controller settings are keyed by the /sys/devices path, so a
// /sys/class/hwmon/hwmonN path would orphan every saved one.
if ($pwms !== $expected) {
$failures[] = "PWM channels must include pwm10 and legacy-layout channels, exclude per-channel attributes,\n"
. "sort numerically and use the resolved device path. Got:\n" . var_export($pwms, true);
}

exec('rm -rf ' . escapeshellarg($root));

if ($failures) {
fwrite(STDERR, implode("\n", $failures) . "\n");
exit(1);
}

echo "pwm discovery tests passed\n";
Loading