From a6550e2654a3ad54e0cd1fa9900ac81b8fd95414 Mon Sep 17 00:00:00 2001 From: DrFedarov <26868588+DrFedarov@users.noreply.github.com> Date: Thu, 12 Nov 2026 13:19:58 -0600 Subject: [PATCH 1/6] Support macsmc_hwmon (Linux 7.3+ T2 Macs) Find the hwmon named macsmc_hwmon when applesmc is absent, drive fans through fanN_target (value in [min,max] = manual, 0 = SMC auto), parse sensor keys from 'Description (KEY)' labels, exclude unreadable or out-of-range sensors from averages, clamp to hardware min/max, and hand fans back to the SMC on exit. Co-Authored-By: Claude Opus 5.5 --- include/applesmc.h | 5 ++ include/config.h | 1 + src/applesmc.c | 157 +++++++++++++++++++++++++++++++++++++++++---- src/control.c | 21 ++++-- src/macfan.c | 2 + 5 files changed, 171 insertions(+), 15 deletions(-) diff --git a/include/applesmc.h b/include/applesmc.h index dd62177..40cf04d 100644 --- a/include/applesmc.h +++ b/include/applesmc.h @@ -20,6 +20,7 @@ struct sensor char name[50]; char fname[PATH_MAX]; float value; + bool valid; }; struct fan @@ -28,6 +29,8 @@ struct fan char out_path[PATH_MAX]; char manual_path[PATH_MAX]; int speed; + int hw_min; /* fanN_min from sysfs, 0 if unknown */ + int hw_max; /* fanN_max from sysfs, 0 if unknown */ float sensor_avg; }; @@ -38,6 +41,7 @@ struct applesmc int fan_cnt; float temp_avg; int active_sensors; + bool is_macsmc; /* macsmc_hwmon (kernel >= 7.3) instead of applesmc */ struct sensor *sensors; struct fan *fans; @@ -48,5 +52,6 @@ int countFans(char *basePath); void find_applesmc(struct applesmc *smc); void scan_sensors(struct applesmc *smc, struct mfdconfig cfg); void read_sensors(struct applesmc *smc, struct mfdconfig cfg); +void release_fans(struct applesmc *smc); #endif \ No newline at end of file diff --git a/include/config.h b/include/config.h index a103a6e..2f76811 100644 --- a/include/config.h +++ b/include/config.h @@ -14,6 +14,7 @@ #define HWMON_DIR "/sys/class/hwmon" #define APPLESMC_ID "applesmc" +#define MACSMC_HWMON_ID "macsmc_hwmon" #define SENSKEY_MAXLEN 16 #define MAXFANS 18 #define MAXSENSORS 100 diff --git a/src/applesmc.c b/src/applesmc.c index affd8e3..0454347 100644 --- a/src/applesmc.c +++ b/src/applesmc.c @@ -69,6 +69,65 @@ int countFans(char *basePath) return fancnt; } +static int read_sysfs_int(const char *path, int *out) +{ + char buf[32]; + int fd = open(path, O_RDONLY); + if (fd < 0) + return -1; + int n = read(fd, buf, sizeof(buf) - 1); + close(fd); + if (n < 1) + return -1; + buf[n] = 0; + *out = atoi(buf); + return 0; +} + +/* Kernel >= 7.3 (T2 Macs): macsmc_hwmon. Attributes live directly in the + * hwmon directory and the hwmon "name" file identifies it. */ +static void find_macsmc_hwmon(struct applesmc *smc) +{ + DIR *d = opendir(HWMON_DIR); + struct dirent *e; + char p[PATH_MAX]; + char name[64]; + + if (d == NULL) + return; + + while ((e = readdir(d)) != NULL && smc->path[0] == 0) + { + if (e->d_name[0] == '.') + continue; + + snprintf(p, sizeof(p), "%s/%s/name", HWMON_DIR, e->d_name); + int fd = open(p, O_RDONLY); + if (fd < 0) + continue; + int n = read(fd, name, sizeof(name) - 1); + close(fd); + if (n < 1) + continue; + name[n] = 0; + char *nl = strchr(name, '\n'); + if (nl) *nl = 0; + + if (strcmp(name, MACSMC_HWMON_ID) == 0) + { + snprintf(p, sizeof(p), "%s/%s", HWMON_DIR, e->d_name); + char *real = realpath(p, NULL); + if (real != NULL) + { + strlcpy(smc->path, real, PATH_MAX); + free(real); + smc->is_macsmc = true; + } + } + } + closedir(d); +} + void find_applesmc(struct applesmc *smc) { int i, ret, fd_name; @@ -125,9 +184,13 @@ void find_applesmc(struct applesmc *smc) closedir(fd_dir); } + smc->is_macsmc = false; + if (smc->path[0] == 0) + find_macsmc_hwmon(smc); + if (smc->path[0] == 0) { - printf("Error: Cannot find applesmc device.\n"); + printf("Error: Cannot find applesmc or macsmc_hwmon device.\n"); exit(-1); } @@ -139,13 +202,34 @@ void find_applesmc(struct applesmc *smc) smc->fans[i].id = i + 1; - sprintf(path, "%s/fan%d_output", smc->path, i+1); - strlcpy(smc->fans[i].out_path, path, PATH_MAX); + if (smc->is_macsmc) + { + /* macsmc_hwmon: writing a value in [min,max] to fanN_target + * switches the fan to manual; writing 0 returns it to the SMC. */ + sprintf(path, "%s/fan%d_target", smc->path, i+1); + strlcpy(smc->fans[i].out_path, path, PATH_MAX); + smc->fans[i].manual_path[0] = 0; + } + else + { + sprintf(path, "%s/fan%d_output", smc->path, i+1); + strlcpy(smc->fans[i].out_path, path, PATH_MAX); + + sprintf(path, "%s/fan%d_manual", smc->path, i+1); + strlcpy(smc->fans[i].manual_path, path, PATH_MAX); + } - sprintf(path, "%s/fan%d_manual", smc->path, i+1); - strlcpy(smc->fans[i].manual_path, path, PATH_MAX); + smc->fans[i].hw_min = 0; + smc->fans[i].hw_max = 0; + sprintf(path, "%s/fan%d_min", smc->path, i+1); + read_sysfs_int(path, &smc->fans[i].hw_min); + sprintf(path, "%s/fan%d_max", smc->path, i+1); + read_sysfs_int(path, &smc->fans[i].hw_max); } + printf("Using %s at %s (%d fans)\n", + smc->is_macsmc ? MACSMC_HWMON_ID : APPLESMC_ID, smc->path, smc->fan_cnt); + free(name_path); } @@ -194,6 +278,25 @@ void scan_sensors(struct applesmc *smc, struct mfdconfig cfg) { printf("Error: cannot open %s\n", fname); } + else if (smc->is_macsmc) + { + char label[256] = {0}; + int n = fread(label, 1, sizeof(label) - 1, fp); + char *open_p = (n > 0) ? strrchr(label, '(') : NULL; + char *close_p = open_p ? strchr(open_p, ')') : NULL; + + memset(smc->sensors[i].key, 0, SENSKEY_MAXLEN); + if (open_p && close_p && close_p > open_p + 1) + { + int klen = min((int)(close_p - open_p - 1), SENSKEY_MAXLEN - 1); + memcpy(smc->sensors[i].key, open_p + 1, klen); + } + else + { + printf("Error: cannot parse label in %s\n", fname); + } + fclose(fp); + } else { int n = fread(key_buf, 1, SENSKEY_MAXLEN - 1, fp); @@ -290,6 +393,7 @@ void read_sensors(struct applesmc *smc, struct mfdconfig cfg) strlcpy(smc->sensors[i].name, cfg.profile->sensordesc[k].desc, strlen(cfg.profile->sensordesc[k].desc)+1); } + smc->sensors[i].valid = false; if (smc->sensors[i].blacklisted != true) { int fd = open(smc->sensors[i].fname, O_RDONLY); @@ -309,7 +413,13 @@ void read_sensors(struct applesmc *smc, struct mfdconfig cfg) } else { + val_buf[min(n, (int)sizeof(val_buf) - 1)] = 0; + smc->sensors[i].valid = true; smc->sensors[i].value = (float)atoi(val_buf) / 1000.0; + /* macsmc exposes PECI-relative, trend and error keys that + * are <= 0 or nonsense; never let them into averages. */ + if (smc->is_macsmc && (smc->sensors[i].value <= 0.0 || smc->sensors[i].value > 125.0)) + smc->sensors[i].valid = false; if (smc->sensors[i].value <= 0.0 && smc->sensors[i].value != 0.0) smc->sensors[i].value *= -1; } @@ -324,14 +434,15 @@ void read_sensors(struct applesmc *smc, struct mfdconfig cfg) for (i = 0; i < smc->sensor_cnt; i++) { - if (smc->sensors[i].blacklisted != true) + if (smc->sensors[i].blacklisted != true && smc->sensors[i].valid) { smc->temp_avg += smc->sensors[i].value; smc->active_sensors++; } } - smc->temp_avg = smc->temp_avg / smc->active_sensors; + if (smc->active_sensors > 0) + smc->temp_avg = smc->temp_avg / smc->active_sensors; for (i = 0; i < smc->fan_cnt; i++) { @@ -346,7 +457,7 @@ void read_sensors(struct applesmc *smc, struct mfdconfig cfg) { for (k = 0; k < MAXSENSORS; k++) { - if (strcmp(cfg.fanctrl[i].sensors[j], smc->sensors[k].key) == 0 && smc->sensors[k].blacklisted != true) + if (k < smc->sensor_cnt && strcmp(cfg.fanctrl[i].sensors[j], smc->sensors[k].key) == 0 && smc->sensors[k].blacklisted != true && smc->sensors[k].valid) { tempavg += smc->sensors[k].value; avgcnt++; @@ -357,7 +468,7 @@ void read_sensors(struct applesmc *smc, struct mfdconfig cfg) { for (k = 0; k < MAXSENSORS; k++) { - if (strcmp(cfg.profile->fanctrl[i].sensors[j], smc->sensors[k].key) == 0 && smc->sensors[k].blacklisted != true) + if (k < smc->sensor_cnt && strcmp(cfg.profile->fanctrl[i].sensors[j], smc->sensors[k].key) == 0 && smc->sensors[k].blacklisted != true && smc->sensors[k].valid) { tempavg += smc->sensors[k].value; avgcnt++; @@ -366,11 +477,35 @@ void read_sensors(struct applesmc *smc, struct mfdconfig cfg) } } - tempavg = tempavg / avgcnt; + if (avgcnt > 0) + tempavg = tempavg / avgcnt; if (tempavg <= 0.0 && tempavg != 0) tempavg *= -1; smc->fans[i].sensor_avg = floor(tempavg); } -} \ No newline at end of file +} + +/* Hand the fans back to the SMC's own control (called on shutdown). */ +void release_fans(struct applesmc *smc) +{ + int i, fd; + + for (i = 0; i < smc->fan_cnt; i++) + { + const char *p = smc->is_macsmc ? smc->fans[i].out_path : smc->fans[i].manual_path; + if (p[0] == 0) + continue; + fd = open(p, O_WRONLY); + if (fd < 0) + { + printf("Error: cannot release fan: %s\n", p); + continue; + } + if (write(fd, "0", 1) < 0) + printf("Error: cannot release fan: %s\n", p); + close(fd); + } + fflush(stdout); +} diff --git a/src/control.c b/src/control.c index 51a1ba8..e3fbee8 100644 --- a/src/control.c +++ b/src/control.c @@ -82,9 +82,21 @@ void set_fan(struct applesmc *smc, struct mfdconfig cfg) for (i = 0; i < smc->fan_cnt; i++) { - buf = calloc(10, sizeof(char)); - fd = open(smc->fans[i].manual_path, O_WRONLY); - if (fd < 0) + buf = calloc(16, sizeof(char)); + + /* The driver rejects (EINVAL) anything outside the hardware range, + * e.g. a configured max of 2763 when the SMC reports 2762. */ + if (smc->fans[i].hw_max > 0 && smc->fans[i].speed > smc->fans[i].hw_max) + smc->fans[i].speed = smc->fans[i].hw_max; + if (smc->fans[i].hw_min > 0 && smc->fans[i].speed < smc->fans[i].hw_min) + smc->fans[i].speed = smc->fans[i].hw_min; + + fd = (smc->fans[i].manual_path[0] == 0) ? -2 : open(smc->fans[i].manual_path, O_WRONLY); + if (fd == -2) + { + /* macsmc_hwmon: no manual file, writing the target enables manual */ + } + else if (fd < 0) { printf("Error: cannot open fan: %s\n", smc->fans[i].manual_path); } @@ -103,7 +115,8 @@ void set_fan(struct applesmc *smc, struct mfdconfig cfg) else { sprintf(buf, "%d", smc->fans[i].speed); - write(fd, buf, strlen(buf)); + if (write(fd, buf, strlen(buf)) < 0) + printf("Error: write %s to %s failed\n", buf, smc->fans[i].out_path); close(fd); } diff --git a/src/macfan.c b/src/macfan.c index 1a421fc..c9bf3df 100644 --- a/src/macfan.c +++ b/src/macfan.c @@ -154,6 +154,8 @@ int main(int argc, char *argv[]) sleep(cf.pollingInterval); } + release_fans(smc); + if (lock_fd != -1) { close(lock_fd); From 0c3592a607bd0657e93c950dc25041450fe48bda Mon Sep 17 00:00:00 2001 From: DrFedarov <26868588+DrFedarov@users.noreply.github.com> Date: Thu, 12 Nov 2026 13:19:58 -0600 Subject: [PATCH 2/6] Fix fan curve, per-fan config, and add external hwmon sensors - calc_fan: ramp linearly from min_speed at floor to max_speed at ceiling. The old |normalized| * window ran fans faster as temps fell below the floor and never reached max_speed. - read_cfg: allocate per-fan fan_sensors lists (were NULL -> segfault) and let /etc/macfand.conf use_avg override the machine profile. - Per-fan control follows the hottest listed sensor, not the average. - fan_sensors may name any hwmon sensor as ":