mbpfan-josie

Support the applesmc hwmon interface from Linux 7.3

Linux 7.3 (commit 94f5081d3592) moves applesmc to hwmon_device_register_with_info(). Probe the legacy fanN_output path first, then fall back to the hwmon fanN_target and pwmN_enable attributes. Record each fan's manual on/off values so the exit path writes 2 (automatic) on the new driver, which rejects 0.

Patch authored by Charles Menser; tested by the author on a MacBookPro10,1 with 7.2.8 carrying 94f5081d3592.

10c974d801fff99b56be1f5b959b8a9c530c36e8
cjosie <administrator@josie-c.com> · 2026-11-10T21:22 · browse files at this commit

parents: 41989f2

diff --git a/src/global.h b/src/global.h
index 65a22a5..478860f 100644
--- a/src/global.h
+++ b/src/global.h
@@ -23,6 +23,8 @@ struct s_fans {
     char *label;
     char *fan_output_path;
     char *fan_manual_path;
+    int fan_manual_on;  // value written to fan_manual_path to take control
+    int fan_manual_off; // value written to fan_manual_path to hand back to the SMC
     int step_up;
     int step_down;
     int fan_id;
diff --git a/src/mbpfan.c b/src/mbpfan.c
index 645b009..5685287 100644
--- a/src/mbpfan.c
+++ b/src/mbpfan.c
@@ -275,6 +275,36 @@ static void trim_trailing_whitespace(char *str)
     }
 }
 
+/*
+ * Newer applesmc drivers register their attributes through the standard hwmon
+ * ABI instead of on the platform device: fan<N>_target replaces fan<N>_output,
+ * and pwm<N>_enable (1 = manual, 2 = automatic) replaces fan<N>_manual (1/0).
+ * Those files live in <applesmc device>/hwmon/hwmon<M>/, not in the device dir.
+ * Returns that directory, or NULL if the device has none.
+ */
+static char *find_applesmc_hwmon_dir()
+{
+    char *hwmon_parent = smprintf("%s/hwmon", applesmc_path);
+    DIR *dir = opendir(hwmon_parent);
+    char *hwmon_dir = NULL;
+
+    if (dir != NULL) {
+        struct dirent *ent;
+
+        while ((ent = readdir(dir)) != NULL) {
+            if (strncmp(ent->d_name, "hwmon", 5) == 0) {
+                hwmon_dir = smprintf("%s/%s", hwmon_parent, ent->d_name);
+                break;
+            }
+        }
+
+        closedir(dir);
+    }
+
+    free(hwmon_parent);
+    return hwmon_dir;
+}
+
 t_fans *retrieve_fans()
 {
     t_fans *fans_head = NULL;
@@ -296,6 +326,8 @@ t_fans *retrieve_fans()
     int counter = 0;
     int fans_found = 0;
 
+    char *hwmon_dir = find_applesmc_hwmon_dir();
+
     for (counter = 0; counter < NUM_FANS; counter++) {
 
         path_output = smprintf("%s%d%s", path_begin, counter, path_output_end);
@@ -305,11 +337,30 @@ t_fans *retrieve_fans()
         path_fan_max = smprintf("%s%d%s", path_begin, counter, path_max_speed);
 
         FILE *file = fopen(path_output, "w");
+        int manual_on = 1;
+        int manual_off = 0;
+
+        if (file == NULL && hwmon_dir != NULL) {
+            free(path_output);
+            free(path_label);
+            free(path_manual);
+            free(path_fan_min);
+            free(path_fan_max);
+            path_output = smprintf("%s/fan%d_target", hwmon_dir, counter);
+            path_label = smprintf("%s/fan%d%s", hwmon_dir, counter, path_label_end);
+            path_manual = smprintf("%s/pwm%d_enable", hwmon_dir, counter);
+            path_fan_min = smprintf("%s/fan%d%s", hwmon_dir, counter, path_min_speed);
+            path_fan_max = smprintf("%s/fan%d%s", hwmon_dir, counter, path_max_speed);
+            manual_off = 2;
+            file = fopen(path_output, "w");
+        }
 
         if (file != NULL) {
             fan = (t_fans *)malloc(sizeof(t_fans));
             fan->fan_output_path = strdup(path_output);
             fan->fan_manual_path = strdup(path_manual);
+            fan->fan_manual_on = manual_on;
+            fan->fan_manual_off = manual_off;
             fan->fan_id = counter;
 
             int fan_speed = read_value(path_fan_min);
@@ -361,6 +412,8 @@ t_fans *retrieve_fans()
         path_manual = NULL;
     }
 
+    free(hwmon_dir);
+
     if (verbose) {
         mbp_log(LOG_INFO, "Found %d fans", fans_found);
     }
@@ -373,7 +426,7 @@ t_fans *retrieve_fans()
     return fans_head;
 }
 
-static void set_fans_mode(t_fans *fans, int mode)
+static void set_fans_mode(t_fans *fans, bool manual)
 {
     t_fans *tmp = fans;
     FILE *file;
@@ -382,7 +435,7 @@ static void set_fans_mode(t_fans *fans, int mode)
         file = fopen(tmp->fan_manual_path, "rw+");
 
         if (file != NULL) {
-            fprintf(file, "%d", mode);
+            fprintf(file, "%d", manual ? tmp->fan_manual_on : tmp->fan_manual_off);
             fclose(file);
         }
 
@@ -393,13 +446,13 @@ static void set_fans_mode(t_fans *fans, int mode)
 void set_fans_man(t_fans *fans)
 {
 
-    set_fans_mode(fans, 1);
+    set_fans_mode(fans, true);
 }
 
 void set_fans_auto(t_fans *fans)
 {
 
-    set_fans_mode(fans, 0);
+    set_fans_mode(fans, false);
 }
 
 t_sensors *refresh_sensors(t_sensors *sensors)