| 1 | /* |
| 2 | * control.c - Fan control daemon for MacBook |
| 3 | * |
| 4 | * Copyright(C) 2010 Mikael Strom <[email protected]> |
| 5 | * |
| 6 | * This program is free software: you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation, either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | */ |
| 16 | |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | #include <sys/types.h> |
| 21 | #include <sys/stat.h> |
| 22 | #include <fcntl.h> |
| 23 | #include <dirent.h> |
| 24 | #include <assert.h> |
| 25 | #include <unistd.h> |
| 26 | #include "config.h" |
| 27 | |
| 28 | //------------------------------------------------------------------------------ |
| 29 | |
| 30 | #define HWMON_DIR "/sys/class/hwmon" |
| 31 | #define APPLESMC_ID "applesmc" |
| 32 | |
| 33 | struct |
| 34 | { |
| 35 | char *key; |
| 36 | char *desc; |
| 37 | } |
| 38 | sensor_desc[] = |
| 39 | { |
| 40 | {"TB0T", "Battery TS_MAX Temp"}, |
| 41 | {"TB1T", "Battery TS1 Temp"}, |
| 42 | {"TB2T", "Battery TS2 Temp"}, |
| 43 | {"TB3T", "Battery Temp"}, |
| 44 | {"TC0D", "CPU 0 Die Temp"}, |
| 45 | {"TC0P", "CPU 0 Proximity Temp"}, |
| 46 | {"TG0D", "GPU Die - Digital"}, |
| 47 | {"TG0P", "GPU 0 Proximity Temp"}, |
| 48 | {"TG0T", "GPU 0 Die - Analog Temp"}, |
| 49 | {"TG0H", "Left Heat Pipe/Fin Stack Proximity Temp"}, |
| 50 | {"TG1H", "Left Heat Pipe/Fin Stack Proximity Temp"}, |
| 51 | {"TN0P", "MCP Proximity"}, |
| 52 | {"TN0D", "MCP Die"}, |
| 53 | {"Th2H", "Right Fin Stack Proximity Temp"}, |
| 54 | {"Tm0P", "Battery Charger Proximity Temp"}, |
| 55 | {"Ts0P", "Palm Rest Temp"} |
| 56 | }; |
| 57 | #define N_DESC (sizeof(sensor_desc) / sizeof(sensor_desc[0])) |
| 58 | |
| 59 | #define SENSKEY_MAXLEN 16 |
| 60 | |
| 61 | struct sensor |
| 62 | { |
| 63 | int id; |
| 64 | int excluded; |
| 65 | char name[SENSKEY_MAXLEN]; |
| 66 | char fname[PATH_MAX]; |
| 67 | float value; |
| 68 | }; |
| 69 | |
| 70 | //------------------------------------------------------------------------------ |
| 71 | |
| 72 | char base_path[PATH_MAX]; |
| 73 | char fan1_min[PATH_MAX]; |
| 74 | char fan2_min[PATH_MAX]; |
| 75 | char fan1_man[PATH_MAX]; |
| 76 | char fan2_man[PATH_MAX]; |
| 77 | |
| 78 | int sensor_count = 0; |
| 79 | int fan_count = 0; |
| 80 | float temp_avg = 0; |
| 81 | int fan_speed; |
| 82 | |
| 83 | struct sensor *sensors = NULL; |
| 84 | struct sensor *sensor_TC0P = NULL; |
| 85 | struct sensor *sensor_TG0P = NULL; |
| 86 | |
| 87 | #define CTL_NONE 0 // sensor control fan flags |
| 88 | #define CTL_AVG 1 |
| 89 | #define CTL_TC0P 2 |
| 90 | #define CTL_TG0P 3 |
| 91 | |
| 92 | int fan_ctl = 0; // which sensor controls fan |
| 93 | |
| 94 | //------------------------------------------------------------------------------ |
| 95 | |
| 96 | void find_applesmc() |
| 97 | { |
| 98 | DIR *fd_dir; |
| 99 | int ret; |
| 100 | |
| 101 | base_path[0] = 0; |
| 102 | |
| 103 | // find and verify applesmc path in /sys/devices |
| 104 | |
| 105 | fd_dir = opendir(HWMON_DIR); |
| 106 | |
| 107 | if(fd_dir != NULL) |
| 108 | { |
| 109 | struct dirent *dir_entry; |
| 110 | |
| 111 | while((dir_entry = readdir(fd_dir)) != NULL && base_path[0] == 0) |
| 112 | { |
| 113 | if(dir_entry->d_name[0] != '.') |
| 114 | { |
| 115 | char name_path[PATH_MAX]; |
| 116 | int fd_name; |
| 117 | |
| 118 | sprintf(name_path, "%s/%s/device/name", HWMON_DIR, dir_entry->d_name); |
| 119 | |
| 120 | fd_name = open(name_path, O_RDONLY); |
| 121 | |
| 122 | if(fd_name > -1) |
| 123 | { |
| 124 | char name[sizeof(APPLESMC_ID)]; |
| 125 | |
| 126 | ret = read(fd_name, name, sizeof(APPLESMC_ID) - 1); |
| 127 | |
| 128 | close(fd_name); |
| 129 | |
| 130 | if(ret == sizeof(APPLESMC_ID) - 1) |
| 131 | { |
| 132 | if(strncmp(name, APPLESMC_ID, sizeof(APPLESMC_ID) - 1) == 0) |
| 133 | { |
| 134 | char *dev_path; |
| 135 | char *last_slash = strrchr(name_path, '/'); |
| 136 | |
| 137 | if(last_slash != NULL) |
| 138 | { |
| 139 | *last_slash = 0; |
| 140 | |
| 141 | dev_path = realpath(name_path, NULL); |
| 142 | |
| 143 | if(dev_path != NULL) |
| 144 | { |
| 145 | strncpy(base_path, dev_path, sizeof(base_path) - 1); |
| 146 | base_path[sizeof(base_path) - 1] = 0; |
| 147 | free(dev_path); |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | closedir(fd_dir); |
| 156 | } |
| 157 | |
| 158 | // create paths to fan and sensor |
| 159 | |
| 160 | if(base_path[0] == 0) |
| 161 | { |
| 162 | printf("Error: Can't find a applesmc device\n"); |
| 163 | exit(-1); |
| 164 | } |
| 165 | |
| 166 | sprintf(fan1_min, "%s/fan1_min", base_path); |
| 167 | sprintf(fan2_min, "%s/fan2_min", base_path); |
| 168 | sprintf(fan1_man, "%s/fan1_manual", base_path); |
| 169 | sprintf(fan2_man, "%s/fan2_manual", base_path); |
| 170 | |
| 171 | printf("Found applesmc at %s\n", base_path); |
| 172 | } |
| 173 | |
| 174 | //------------------------------------------------------------------------------ |
| 175 | |
| 176 | void read_sensors() |
| 177 | { |
| 178 | int i; |
| 179 | for(i = 0; i < sensor_count; ++i) |
| 180 | { |
| 181 | if(! sensors[i].excluded) |
| 182 | { |
| 183 | // read temp value |
| 184 | |
| 185 | int fd = open(sensors[i].fname, O_RDONLY); |
| 186 | if(fd < 0) |
| 187 | { |
| 188 | printf("Error: Can't open %s\n", sensors[i].fname); |
| 189 | fflush(stdout); |
| 190 | } |
| 191 | else |
| 192 | { |
| 193 | char val_buf[16]; |
| 194 | int n = read(fd, val_buf, sizeof(val_buf)); |
| 195 | if(n < 1) |
| 196 | { |
| 197 | printf("Error: Can't read %s\n", sensors[i].fname); |
| 198 | } |
| 199 | else |
| 200 | { |
| 201 | sensors[i].value = (float)atoi(val_buf) / 1000.0; |
| 202 | } |
| 203 | close(fd); |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | // calc average |
| 209 | |
| 210 | temp_avg = 0.0; |
| 211 | int active_sensors = 0; |
| 212 | |
| 213 | for(i = 0; i < sensor_count; ++i) |
| 214 | { |
| 215 | if(! sensors[i].excluded) |
| 216 | { |
| 217 | temp_avg += sensors[i].value; |
| 218 | ++active_sensors; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | temp_avg = temp_avg / active_sensors; |
| 223 | } |
| 224 | |
| 225 | //------------------------------------------------------------------------------ |
| 226 | |
| 227 | void calc_fan() |
| 228 | { |
| 229 | fan_speed = fan_min; |
| 230 | fan_ctl = CTL_NONE; |
| 231 | |
| 232 | // calc fan speed on average |
| 233 | |
| 234 | float fan_window = fan_max - fan_min; |
| 235 | float temp_avg_window = temp_avg_ceiling - temp_avg_floor; |
| 236 | float normalized_temp =(temp_avg - temp_avg_floor) / temp_avg_window; |
| 237 | float fan_avg_speed =(normalized_temp * fan_window); |
| 238 | if(fan_avg_speed > fan_speed) |
| 239 | { |
| 240 | fan_speed = fan_avg_speed; |
| 241 | fan_ctl = CTL_AVG; |
| 242 | } |
| 243 | |
| 244 | // calc fan speed for TC0P |
| 245 | |
| 246 | if(sensor_TC0P != NULL) |
| 247 | { |
| 248 | float temp_window = temp_TC0P_ceiling - temp_TC0P_floor; |
| 249 | float normalized_temp =(sensor_TC0P->value - temp_TC0P_floor) / temp_window; |
| 250 | float fan_TC0P_speed =(normalized_temp * fan_window); |
| 251 | if(fan_TC0P_speed > fan_speed) |
| 252 | { |
| 253 | fan_speed = fan_TC0P_speed; |
| 254 | fan_ctl = CTL_TC0P; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | // calc fan speed for TG0P |
| 259 | |
| 260 | if(sensor_TG0P != NULL) |
| 261 | { |
| 262 | float temp_window = temp_TG0P_ceiling - temp_TG0P_floor; |
| 263 | float normalized_temp =(sensor_TG0P->value - temp_TG0P_floor) / temp_window; |
| 264 | float fan_TG0P_speed =(normalized_temp * fan_window); |
| 265 | if(fan_TG0P_speed > fan_speed) |
| 266 | { |
| 267 | fan_speed = fan_TG0P_speed; |
| 268 | fan_ctl = CTL_TG0P; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | // finally clamp |
| 273 | |
| 274 | fan_speed = min(fan_max, fan_speed); |
| 275 | } |
| 276 | |
| 277 | //------------------------------------------------------------------------------ |
| 278 | |
| 279 | void set_fan() |
| 280 | { |
| 281 | char buf[16]; |
| 282 | |
| 283 | // update fan 1 |
| 284 | |
| 285 | int fd = open(fan1_min, O_WRONLY); |
| 286 | if(fd < 0) |
| 287 | { |
| 288 | printf("Error: Can't open %s\n", fan1_min); |
| 289 | } |
| 290 | else |
| 291 | { |
| 292 | sprintf(buf, "%d", fan_speed); |
| 293 | write(fd, buf, strlen(buf)); |
| 294 | close(fd); |
| 295 | } |
| 296 | |
| 297 | // set fan 1 manual to zero |
| 298 | |
| 299 | fd = open(fan1_man, O_WRONLY); |
| 300 | if(fd < 0) |
| 301 | { |
| 302 | printf("Error: Can't open %s\n", fan1_man); |
| 303 | } |
| 304 | else |
| 305 | { |
| 306 | strcpy(buf, "0"); |
| 307 | write(fd, buf, strlen(buf)); |
| 308 | close(fd); |
| 309 | } |
| 310 | |
| 311 | // update fan 2 |
| 312 | |
| 313 | if(fan_count > 1) |
| 314 | { |
| 315 | fd = open(fan2_min, O_WRONLY); |
| 316 | if(fd < 0) |
| 317 | { |
| 318 | printf("Error: Can't open %s\n", fan2_min); |
| 319 | } |
| 320 | else |
| 321 | { |
| 322 | sprintf(buf, "%d", fan_speed); |
| 323 | write(fd, buf, strlen(buf)); |
| 324 | close(fd); |
| 325 | } |
| 326 | |
| 327 | // set fan 2 manual to zero |
| 328 | |
| 329 | fd = open(fan2_man, O_WRONLY); |
| 330 | if(fd < 0) |
| 331 | { |
| 332 | printf("Error: Can't open %s\n", fan2_man); |
| 333 | } |
| 334 | else |
| 335 | { |
| 336 | strcpy(buf, "0"); |
| 337 | write(fd, buf, strlen(buf)); |
| 338 | close(fd); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | fflush(stdout); |
| 343 | } |
| 344 | |
| 345 | //------------------------------------------------------------------------------ |
| 346 | |
| 347 | void adjust() |
| 348 | { |
| 349 | read_sensors(); |
| 350 | calc_fan(); |
| 351 | set_fan(); |
| 352 | } |
| 353 | |
| 354 | //------------------------------------------------------------------------------ |
| 355 | |
| 356 | void scan_sensors() |
| 357 | { |
| 358 | int i; |
| 359 | int j; |
| 360 | struct stat buf; |
| 361 | int result; |
| 362 | |
| 363 | sensor_TC0P = NULL; |
| 364 | sensor_TG0P = NULL; |
| 365 | |
| 366 | // get number of fans |
| 367 | |
| 368 | result = stat(fan1_min, &buf); |
| 369 | if(result != 0) |
| 370 | { |
| 371 | printf("No fans detected, terminating!\n"); |
| 372 | exit(-1); |
| 373 | } |
| 374 | else |
| 375 | { |
| 376 | fan_count = 1; |
| 377 | } |
| 378 | |
| 379 | result = stat(fan2_min, &buf); |
| 380 | if(result != 0) |
| 381 | { |
| 382 | printf("Found 1 fan.\n"); |
| 383 | } |
| 384 | else |
| 385 | { |
| 386 | fan_count = 2; |
| 387 | printf("Found 2 fans.\n"); |
| 388 | } |
| 389 | |
| 390 | // count number of sensors |
| 391 | |
| 392 | int count = 0; |
| 393 | while(count < 100) // more than 100 sensors is an error! |
| 394 | { |
| 395 | char fname[512]; |
| 396 | |
| 397 | // sensor numbering start at 1 |
| 398 | sprintf(fname, "%s/temp%d_input", base_path, count + 1); |
| 399 | result = stat(fname, &buf); |
| 400 | |
| 401 | if(result == 0) |
| 402 | { |
| 403 | ++count; |
| 404 | } |
| 405 | else |
| 406 | { |
| 407 | break; // done |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | sensor_count = count; |
| 412 | |
| 413 | if(sensor_count > 0) |
| 414 | { |
| 415 | // Get sensor id, labels and descriptions, check exclude list |
| 416 | |
| 417 | if(sensors != NULL) |
| 418 | { |
| 419 | free(sensors); |
| 420 | } |
| 421 | |
| 422 | sensors = malloc(sizeof(struct sensor) * sensor_count); |
| 423 | assert(sensors != NULL); |
| 424 | |
| 425 | printf("Found %d sensors:\n", sensor_count); |
| 426 | |
| 427 | for(i = 0; i < sensor_count; ++i) |
| 428 | { |
| 429 | char fname[512]; |
| 430 | |
| 431 | // set id, check exclude list and save file name |
| 432 | sensors[i].id = i + 1; |
| 433 | sensors[i].excluded = 0; |
| 434 | sprintf(sensors[i].fname, "%s/temp%d_input", base_path, sensors[i].id); |
| 435 | |
| 436 | for(j = 0; j < MAX_EXCLUDE && exclude[j] != 0; ++j) |
| 437 | { |
| 438 | if(exclude[j] == sensors[i].id) |
| 439 | { |
| 440 | sensors[i].excluded = 1; |
| 441 | break; |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | // read label |
| 446 | sprintf(fname, "%s/temp%d_label", base_path, sensors[i].id); |
| 447 | |
| 448 | sensors[i].name[0] = 0; // set zero length |
| 449 | |
| 450 | FILE *fp = fopen(fname, "r"); |
| 451 | if(fp == NULL) |
| 452 | { |
| 453 | printf("Error: Can't open %s\n", fname); |
| 454 | } |
| 455 | else |
| 456 | { |
| 457 | char key_buf[SENSKEY_MAXLEN]; |
| 458 | memset(key_buf, 0, SENSKEY_MAXLEN); |
| 459 | |
| 460 | int n = fread(key_buf, 1, SENSKEY_MAXLEN - 1, fp); |
| 461 | if(n < 1) |
| 462 | { |
| 463 | printf("Error: Can't read %s\n", fname); |
| 464 | } |
| 465 | else |
| 466 | { |
| 467 | char *p_endl = strrchr(key_buf, '\n'); |
| 468 | if(p_endl) |
| 469 | { |
| 470 | *p_endl = 0; // remove '\n' |
| 471 | } |
| 472 | strncpy(sensors[i].name, key_buf, SENSKEY_MAXLEN); |
| 473 | } |
| 474 | fclose(fp); |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | for(i = 0; i < sensor_count; ++i) // for each label found |
| 479 | { |
| 480 | if(! sensors[i].excluded) |
| 481 | { |
| 482 | // try to find TC0P and TG0P |
| 483 | // if found, assign sensor_TC0P and sensor_TG0P for later use |
| 484 | |
| 485 | if(strcmp(sensors[i].name, "TC0P") == 0) |
| 486 | { |
| 487 | sensor_TC0P = &sensors[i]; |
| 488 | } |
| 489 | else if(strcmp(sensors[i].name, "TG0P") == 0) |
| 490 | { |
| 491 | sensor_TG0P = &sensors[i]; |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | // print out sensor information. |
| 496 | |
| 497 | printf("\t%2d: ", sensors[i].id); |
| 498 | |
| 499 | int found = 0; |
| 500 | for(j = 0; j < N_DESC && ! found; ++j) // find in descriptions table |
| 501 | { |
| 502 | if(strcmp(sensors[i].name, sensor_desc[j].key) == 0) |
| 503 | { |
| 504 | found = 1; |
| 505 | printf("%s - %s", sensor_desc[j].key, sensor_desc[j].desc); |
| 506 | } |
| 507 | } |
| 508 | if(! found) |
| 509 | { |
| 510 | printf("%s - ?", sensors[i].name); |
| 511 | } |
| 512 | |
| 513 | printf(" %s\n", sensors[i].excluded ? " ***EXCLUDED***" : ""); |
| 514 | } |
| 515 | } |
| 516 | else |
| 517 | { |
| 518 | printf("No sensors detected, terminating!\n"); |
| 519 | exit(-1); |
| 520 | } |
| 521 | |
| 522 | fflush(stdout); |
| 523 | } |
| 524 | |
| 525 | //------------------------------------------------------------------------------ |
| 526 | |
| 527 | void logger() |
| 528 | { |
| 529 | int i; |
| 530 | |
| 531 | if(log_level > 0) |
| 532 | { |
| 533 | printf("Speed: %d, %sAVG: %.1fC" , |
| 534 | fan_speed, |
| 535 | fan_ctl == CTL_AVG ? "*" : " ", |
| 536 | temp_avg); |
| 537 | |
| 538 | if(sensor_TC0P != NULL) |
| 539 | { |
| 540 | printf(", %sTC0P: %.1fC" , |
| 541 | fan_ctl == CTL_TC0P ? "*" : " ", |
| 542 | sensor_TC0P->value); |
| 543 | } |
| 544 | |
| 545 | if(sensor_TG0P != NULL) |
| 546 | { |
| 547 | printf(", %sTG0P: %.1fC" , |
| 548 | fan_ctl == CTL_TG0P ? "*" : " ", |
| 549 | sensor_TG0P->value); |
| 550 | } |
| 551 | |
| 552 | if(log_level > 1) |
| 553 | { |
| 554 | printf(", Sensors: "); |
| 555 | for(i = 0; i < sensor_count; ++i) |
| 556 | { |
| 557 | if(! sensors[i].excluded) |
| 558 | { |
| 559 | printf("%s:%.0f ", sensors[i].name, sensors[i].value); |
| 560 | } |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | printf("\n"); |
| 565 | fflush(stdout); |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | //------------------------------------------------------------------------------ |
| 570 |