-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathclevo-indicator.c
More file actions
617 lines (560 loc) · 19.5 KB
/
Copy pathclevo-indicator.c
File metadata and controls
617 lines (560 loc) · 19.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
/*
============================================================================
Name : clevo-indicator.c
Author : AqD <iiiaqd@gmail.com>
Version :
Description : Ubuntu fan control indicator for Clevo laptops
Based on http://www.association-apml.fr/upload/fanctrl.c by Jonas Diemer
(diemer@gmx.de)
============================================================================
TEST:
gcc clevo-indicator.c -o clevo-indicator `pkg-config --cflags --libs appindicator3-0.1` -lm
sudo chown root clevo-indicator
sudo chmod u+s clevo-indicator
Run as effective uid = root, but uid = desktop user (in order to use indicator).
============================================================================
Auto fan control algorithm:
The algorithm is to replace the builtin auto fan-control algorithm in Clevo
laptops which is apparently broken in recent models such as W350SSQ, where the
fan doesn't get kicked until both of GPU and CPU are really hot (and GPU
cannot be hot anymore thanks to nVIDIA's Maxwell chips). It's far more
aggressive than the builtin algorithm in order to keep the temperatures below
60°C all the time, for maximized performance with Intel turbo boost enabled.
============================================================================
*/
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <math.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/io.h>
#include <sys/mman.h>
#include <sys/prctl.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <libayatana-appindicator3-0.1/libayatana-appindicator/app-indicator.h>
#define NAME "clevo-indicator"
#define EC_SC 0x66
#define EC_DATA 0x62
#define IBF 1
#define OBF 0
#define EC_SC_READ_CMD 0x80
/* EC registers can be read by EC_SC_READ_CMD or /sys/kernel/debug/ec/ec0/io:
*
* 1. modprobe ec_sys
* 2. od -Ax -t x1 /sys/kernel/debug/ec/ec0/io
*/
#define EC_REG_SIZE 0x100
#define EC_REG_CPU_TEMP 0x07
#define EC_REG_GPU_TEMP 0xCD
#define EC_REG_FAN_DUTY 0xCE
#define EC_REG_FAN_RPMS_HI 0xD0
#define EC_REG_FAN_RPMS_LO 0xD1
#define MAX_FAN_RPM 4400.0
typedef enum {
NA = 0, AUTO = 1, MANUAL = 2
} MenuItemType;
static void main_init_share(void);
static int main_ec_worker(void);
static void main_ui_worker(int argc, char** argv);
static void main_on_sigchld(int signum);
static void main_on_sigterm(int signum);
static int main_dump_fan(void);
static int main_test_fan(int duty_percentage);
static gboolean ui_update(gpointer user_data);
static void ui_command_set_fan(long fan_duty);
static void ui_command_quit(gchar* command);
static void ui_toggle_menuitems(int fan_duty);
static void ec_on_sigterm(int signum);
static int ec_init(void);
static int ec_auto_duty_adjust(void);
static int ec_query_cpu_temp(void);
static int ec_query_gpu_temp(void);
static int ec_query_fan_duty(void);
static int ec_query_fan_rpms(void);
static int ec_write_fan_duty(int duty_percentage);
static int ec_io_wait(const uint32_t port, const uint32_t flag,
const char value);
static uint8_t ec_io_read(const uint32_t port);
static int ec_io_do(const uint32_t cmd, const uint32_t port,
const uint8_t value);
static int calculate_fan_duty(int raw_duty);
static int calculate_fan_rpms(int raw_rpm_high, int raw_rpm_low);
static int check_proc_instances(const char* proc_name);
static void get_time_string(char* buffer, size_t max, const char* format);
static void signal_term(__sighandler_t handler);
static AppIndicator* indicator = NULL;
struct {
char label[256];
GCallback callback;
long option;
MenuItemType type;
GtkWidget* widget;
}static menuitems[] = {
{ "Set FAN to AUTO", G_CALLBACK(ui_command_set_fan), 0, AUTO, NULL },
{ "", NULL, 0L, NA, NULL },
{ "Set FAN to 60%", G_CALLBACK(ui_command_set_fan), 60, MANUAL, NULL },
{ "Set FAN to 70%", G_CALLBACK(ui_command_set_fan), 70, MANUAL, NULL },
{ "Set FAN to 80%", G_CALLBACK(ui_command_set_fan), 80, MANUAL, NULL },
{ "Set FAN to 90%", G_CALLBACK(ui_command_set_fan), 90, MANUAL, NULL },
{ "Set FAN to 100%", G_CALLBACK(ui_command_set_fan), 100, MANUAL, NULL },
{ "", NULL, 0L, NA, NULL },
{ "Quit", G_CALLBACK(ui_command_quit), 0L, NA, NULL }
};
static int menuitem_count = (sizeof(menuitems) / sizeof(menuitems[0]));
struct {
volatile int exit;
volatile int cpu_temp;
volatile int gpu_temp;
volatile int fan_duty;
volatile int fan_rpms;
volatile int auto_duty;
volatile int auto_duty_val;
volatile int manual_next_fan_duty;
volatile int manual_prev_fan_duty;
}static *share_info = NULL;
static pid_t parent_pid = 0;
int main(int argc, char* argv[]) {
printf("Simple fan control utility for Clevo laptops\n");
if (check_proc_instances(NAME) > 1) {
printf("Multiple running instances!\n");
char* display = getenv("DISPLAY");
if (display != NULL && strlen(display) > 0) {
int desktop_uid = getuid();
setuid(desktop_uid);
//
gtk_init(&argc, &argv);
GtkWidget* dialog = gtk_message_dialog_new(NULL, 0,
GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
"Multiple running instances of %s!", NAME);
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
}
return EXIT_FAILURE;
}
if (ec_init() != EXIT_SUCCESS) {
printf("unable to control EC: %s\n", strerror(errno));
return EXIT_FAILURE;
}
if (argc <= 1) {
char* display = getenv("DISPLAY");
if (display == NULL || strlen(display) == 0) {
return main_dump_fan();
} else {
parent_pid = getpid();
main_init_share();
signal(SIGCHLD, &main_on_sigchld);
signal_term(&main_on_sigterm);
pid_t worker_pid = fork();
if (worker_pid == 0) {
signal(SIGCHLD, SIG_DFL);
signal_term(&ec_on_sigterm);
return main_ec_worker();
} else if (worker_pid > 0) {
main_ui_worker(argc, argv);
share_info->exit = 1;
waitpid(worker_pid, NULL, 0);
} else {
printf("unable to create worker: %s\n", strerror(errno));
return EXIT_FAILURE;
}
}
} else {
if (argv[1][0] == '-') {
printf(
"\n\
Usage: clevo-indicator [fan-duty-percentage]\n\
\n\
Dump/Control fan duty on Clevo laptops. Display indicator by default.\n\
\n\
Arguments:\n\
[fan-duty-percentage]\t\tTarget fan duty in percentage, from 40 to 100\n\
-?\t\t\t\tDisplay this help and exit\n\
\n\
Without arguments this program should attempt to display an indicator in\n\
the Ubuntu tray area for fan information display and control. The indicator\n\
requires this program to have setuid=root flag but run from the desktop user\n\
, because a root user is not allowed to display a desktop indicator while a\n\
non-root user is not allowed to control Clevo EC (Embedded Controller that's\n\
responsible of the fan). Fix permissions of this executable if it fails to\n\
run:\n\
sudo chown root clevo-indicator\n\
sudo chmod u+s clevo-indicator\n\
\n\
Note any fan duty change should take 1-2 seconds to come into effect - you\n\
can verify by the fan speed displayed on indicator icon and also louder fan\n\
noise.\n\
\n\
In the indicator mode, this program would always attempt to load kernel\n\
module 'ec_sys', in order to query EC information from\n\
'/sys/kernel/debug/ec/ec0/io' instead of polling EC ports for readings,\n\
which may be more risky if interrupted or concurrently operated during the\n\
process.\n\
\n\
DO NOT MANIPULATE OR QUERY EC I/O PORTS WHILE THIS PROGRAM IS RUNNING.\n\
\n");
return main_dump_fan();
} else {
int val = atoi(argv[1]);
if (val < 40 || val > 100)
{
printf("invalid fan duty %d!\n", val);
return EXIT_FAILURE;
}
return main_test_fan(val);
}
}
return EXIT_SUCCESS;
}
static void main_init_share(void) {
void* shm = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED,
-1, 0);
share_info = shm;
share_info->exit = 0;
share_info->cpu_temp = 0;
share_info->gpu_temp = 0;
share_info->fan_duty = 0;
share_info->fan_rpms = 0;
share_info->auto_duty = 1;
share_info->auto_duty_val = 0;
share_info->manual_next_fan_duty = 0;
share_info->manual_prev_fan_duty = 0;
}
static int main_ec_worker(void) {
setuid(0);
system("modprobe ec_sys");
while (share_info->exit == 0) {
// check parent
if (parent_pid != 0 && kill(parent_pid, 0) == -1) {
printf("worker on parent death\n");
break;
}
// write EC
int new_fan_duty = share_info->manual_next_fan_duty;
if (new_fan_duty != 0
&& new_fan_duty != share_info->manual_prev_fan_duty) {
ec_write_fan_duty(new_fan_duty);
share_info->manual_prev_fan_duty = new_fan_duty;
}
// read EC
int io_fd = open("/sys/kernel/debug/ec/ec0/io", O_RDONLY, 0);
if (io_fd < 0) {
printf("unable to read EC from sysfs: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
unsigned char buf[EC_REG_SIZE];
ssize_t len = read(io_fd, buf, EC_REG_SIZE);
switch (len) {
case -1:
printf("unable to read EC from sysfs: %s\n", strerror(errno));
break;
case 0x100:
share_info->cpu_temp = buf[EC_REG_CPU_TEMP];
share_info->gpu_temp = buf[EC_REG_GPU_TEMP];
share_info->fan_duty = calculate_fan_duty(buf[EC_REG_FAN_DUTY]);
share_info->fan_rpms = calculate_fan_rpms(buf[EC_REG_FAN_RPMS_HI],
buf[EC_REG_FAN_RPMS_LO]);
/*
printf("temp=%d, duty=%d, rpms=%d\n", share_info->cpu_temp,
share_info->fan_duty, share_info->fan_rpms);
*/
break;
default:
printf("wrong EC size from sysfs: %ld\n", len);
}
close(io_fd);
// auto EC
if (share_info->auto_duty == 1) {
int next_duty = ec_auto_duty_adjust();
if (next_duty != 0 && next_duty != share_info->auto_duty_val) {
char s_time[256];
get_time_string(s_time, 256, "%m/%d %H:%M:%S");
printf("%s CPU=%d°C, GPU=%d°C, auto fan duty to %d%%\n", s_time,
share_info->cpu_temp, share_info->gpu_temp, next_duty);
ec_write_fan_duty(next_duty);
share_info->auto_duty_val = next_duty;
}
}
//
usleep(200 * 1000);
}
printf("worker quit\n");
return EXIT_SUCCESS;
}
static void main_ui_worker(int argc, char** argv) {
printf("Indicator...\n");
int desktop_uid = getuid();
setuid(desktop_uid);
//
gtk_init(&argc, &argv);
//
GtkWidget* indicator_menu = gtk_menu_new();
for (int i = 0; i < menuitem_count; i++) {
GtkWidget* item;
if (strlen(menuitems[i].label) == 0) {
item = gtk_separator_menu_item_new();
} else {
item = gtk_menu_item_new_with_label(menuitems[i].label);
g_signal_connect_swapped(item, "activate",
G_CALLBACK(menuitems[i].callback),
(void* ) menuitems[i].option);
}
gtk_menu_shell_append(GTK_MENU_SHELL(indicator_menu), item);
menuitems[i].widget = item;
}
gtk_widget_show_all(indicator_menu);
//
indicator = app_indicator_new(NAME, "brasero",
APP_INDICATOR_CATEGORY_HARDWARE);
g_assert(IS_APP_INDICATOR(indicator));
app_indicator_set_label(indicator, "Init..", "XX");
app_indicator_set_status(indicator, APP_INDICATOR_STATUS_ATTENTION);
app_indicator_set_ordering_index(indicator, -2);
app_indicator_set_title(indicator, "Clevo");
app_indicator_set_menu(indicator, GTK_MENU(indicator_menu));
g_timeout_add(500, &ui_update, NULL);
ui_toggle_menuitems(share_info->fan_duty);
gtk_main();
printf("main on UI quit\n");
}
static void main_on_sigchld(int signum) {
printf("main on worker quit signal\n");
exit(EXIT_SUCCESS);
}
static void main_on_sigterm(int signum) {
printf("main on signal: %s\n", strsignal(signum));
if (share_info != NULL)
share_info->exit = 1;
exit(EXIT_SUCCESS);
}
static int main_dump_fan(void) {
printf("Dump fan information\n");
printf(" FAN Duty: %d%%\n", ec_query_fan_duty());
printf(" FAN RPMs: %d RPM\n", ec_query_fan_rpms());
printf(" CPU Temp: %d°C\n", ec_query_cpu_temp());
printf(" GPU Temp: %d°C\n", ec_query_gpu_temp());
return EXIT_SUCCESS;
}
static int main_test_fan(int duty_percentage) {
printf("Change fan duty to %d%%\n", duty_percentage);
ec_write_fan_duty(duty_percentage);
printf("\n");
main_dump_fan();
return EXIT_SUCCESS;
}
static gboolean ui_update(gpointer user_data) {
char label[256];
sprintf(label, "%d℃ %d℃", share_info->cpu_temp, share_info->gpu_temp);
app_indicator_set_label(indicator, label, "XXXXXX");
char icon_name[256];
double load = ((double) share_info->fan_rpms) / MAX_FAN_RPM * 100.0;
double load_r = round(load / 5.0) * 5.0;
sprintf(icon_name, "brasero-disc-%02d", (int) load_r);
app_indicator_set_icon(indicator, icon_name);
return G_SOURCE_CONTINUE;
}
static void ui_command_set_fan(long fan_duty) {
int fan_duty_val = (int) fan_duty;
if (fan_duty_val == 0) {
printf("clicked on fan duty auto\n");
share_info->auto_duty = 1;
share_info->auto_duty_val = 0;
share_info->manual_next_fan_duty = 0;
} else {
printf("clicked on fan duty: %d\n", fan_duty_val);
share_info->auto_duty = 0;
share_info->auto_duty_val = 0;
share_info->manual_next_fan_duty = fan_duty_val;
}
ui_toggle_menuitems(fan_duty_val);
}
static void ui_command_quit(gchar* command) {
printf("clicked on quit\n");
gtk_main_quit();
}
static void ui_toggle_menuitems(int fan_duty) {
for (int i = 0; i < menuitem_count; i++) {
if (menuitems[i].widget == NULL)
continue;
if (fan_duty == 0)
gtk_widget_set_sensitive(menuitems[i].widget,
menuitems[i].type != AUTO);
else
gtk_widget_set_sensitive(menuitems[i].widget,
menuitems[i].type != MANUAL
|| (int) menuitems[i].option != fan_duty);
}
}
static int ec_init(void) {
if (ioperm(EC_DATA, 1, 1) != 0)
return EXIT_FAILURE;
if (ioperm(EC_SC, 1, 1) != 0)
return EXIT_FAILURE;
return EXIT_SUCCESS;
}
static void ec_on_sigterm(int signum) {
printf("ec on signal: %s\n", strsignal(signum));
if (share_info != NULL)
share_info->exit = 1;
}
static int ec_auto_duty_adjust(void) {
int temp = MAX(share_info->cpu_temp, share_info->gpu_temp);
int duty = share_info->fan_duty;
//
if (temp >= 80 && duty < 100)
return 100;
if (temp >= 70 && duty < 90)
return 90;
if (temp >= 60 && duty < 80)
return 80;
if (temp >= 50 && duty < 70)
return 70;
if (temp >= 40 && duty < 60)
return 60;
if (temp >= 30 && duty < 50)
return 50;
if (temp >= 20 && duty < 40)
return 40;
if (temp >= 10 && duty < 30)
return 30;
//
if (temp <= 15 && duty > 30)
return 30;
if (temp <= 25 && duty > 40)
return 40;
if (temp <= 35 && duty > 50)
return 50;
if (temp <= 45 && duty > 60)
return 60;
if (temp <= 55 && duty > 70)
return 70;
if (temp <= 65 && duty > 80)
return 80;
if (temp <= 75 && duty > 90)
return 90;
//
return 0;
}
static int ec_query_cpu_temp(void) {
return ec_io_read(EC_REG_CPU_TEMP);
}
static int ec_query_gpu_temp(void) {
return ec_io_read(EC_REG_GPU_TEMP);
}
static int ec_query_fan_duty(void) {
int raw_duty = ec_io_read(EC_REG_FAN_DUTY);
return calculate_fan_duty(raw_duty);
}
static int ec_query_fan_rpms(void) {
int raw_rpm_hi = ec_io_read(EC_REG_FAN_RPMS_HI);
int raw_rpm_lo = ec_io_read(EC_REG_FAN_RPMS_LO);
return calculate_fan_rpms(raw_rpm_hi, raw_rpm_lo);
}
static int ec_write_fan_duty(int duty_percentage) {
if (duty_percentage < 60 || duty_percentage > 100) {
printf("Wrong fan duty to write: %d\n", duty_percentage);
return EXIT_FAILURE;
}
double v_d = ((double) duty_percentage) / 100.0 * 255.0;
int v_i = (int) v_d;
return ec_io_do(0x99, 0x01, v_i);
}
static int ec_io_wait(const uint32_t port, const uint32_t flag,
const char value) {
uint8_t data = inb(port);
int i = 0;
while ((((data >> flag) & 0x1) != value) && (i++ < 100)) {
usleep(1000);
data = inb(port);
}
if (i >= 100) {
printf("wait_ec error on port 0x%x, data=0x%x, flag=0x%x, value=0x%x\n",
port, data, flag, value);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
static uint8_t ec_io_read(const uint32_t port) {
ec_io_wait(EC_SC, IBF, 0);
outb(EC_SC_READ_CMD, EC_SC);
ec_io_wait(EC_SC, IBF, 0);
outb(port, EC_DATA);
//wait_ec(EC_SC, EC_SC_IBF_FREE);
ec_io_wait(EC_SC, OBF, 1);
uint8_t value = inb(EC_DATA);
return value;
}
static int ec_io_do(const uint32_t cmd, const uint32_t port,
const uint8_t value) {
ec_io_wait(EC_SC, IBF, 0);
outb(cmd, EC_SC);
ec_io_wait(EC_SC, IBF, 0);
outb(port, EC_DATA);
ec_io_wait(EC_SC, IBF, 0);
outb(value, EC_DATA);
return ec_io_wait(EC_SC, IBF, 0);
}
static int calculate_fan_duty(int raw_duty) {
return (int) ((double) raw_duty / 255.0 * 100.0);
}
static int calculate_fan_rpms(int raw_rpm_high, int raw_rpm_low) {
int raw_rpm = (raw_rpm_high << 8) + raw_rpm_low;
return raw_rpm > 0 ? (2156220 / raw_rpm) : 0;
}
static int check_proc_instances(const char* proc_name) {
int proc_name_len = strlen(proc_name);
pid_t this_pid = getpid();
DIR* dir;
if (!(dir = opendir("/proc"))) {
perror("can't open /proc");
return -1;
}
int instance_count = 0;
struct dirent* ent;
while ((ent = readdir(dir)) != NULL) {
char* endptr;
long lpid = strtol(ent->d_name, &endptr, 10);
if (*endptr != '\0')
continue;
if (lpid == this_pid)
continue;
char buf[512];
snprintf(buf, sizeof(buf), "/proc/%ld/comm", lpid);
FILE* fp = fopen(buf, "r");
if (fp) {
if (fgets(buf, sizeof(buf), fp) != NULL) {
if ((buf[proc_name_len] == '\n' || buf[proc_name_len] == '\0')
&& strncmp(buf, proc_name, proc_name_len) == 0) {
fprintf(stderr, "Process: %ld\n", lpid);
instance_count += 1;
}
}
fclose(fp);
}
}
closedir(dir);
return instance_count;
}
static void get_time_string(char* buffer, size_t max, const char* format) {
time_t timer;
struct tm tm_info;
time(&timer);
localtime_r(&timer, &tm_info);
strftime(buffer, max, format, &tm_info);
}
static void signal_term(__sighandler_t handler) {
signal(SIGHUP, handler);
signal(SIGINT, handler);
signal(SIGQUIT, handler);
signal(SIGPIPE, handler);
signal(SIGALRM, handler);
signal(SIGTERM, handler);
signal(SIGUSR1, handler);
signal(SIGUSR2, handler);
}