Skip to content

Commit 1ca8a5d

Browse files
committed
Patched to work on newer systems and added sanity check on input args
1 parent 93e1fe4 commit 1ca8a5d

File tree

3 files changed

+60
-11
lines changed

3 files changed

+60
-11
lines changed

intel_backlight.c

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@
2929
#include <stdlib.h>
3030
#include <stdio.h>
3131
#include <string.h>
32+
#include <errno.h>
33+
#include <limits.h>
3234

3335
#include "intel_gpu_tools.h"
36+
3437
#define NUM_ELEMENTS(array) (sizeof(array) / sizeof(array[0]))
3538

3639
/* XXX PCH only today */
@@ -44,7 +47,7 @@ static void reg_write(uint32_t reg, uint32_t val)
4447
{
4548
*(volatile uint32_t *)((volatile char*)mmio + reg) = val;
4649
}
47-
50+
4851
static int brightness_levels[] = {1, 2, 4, 6, 9, 12, 16, 20, 25, 30, 36, 43,
4952
51, 60, 70, 80, 90, 100};
5053

@@ -66,33 +69,69 @@ static int brightness_decr(int curr)
6669
return brightness_levels[i];
6770
}
6871

72+
void print_usage() {
73+
printf("Usage: intel_backlight [incr|decr|n] where n is brightness in percent\n");
74+
}
75+
6976
int main(int argc, char** argv)
7077
{
7178
uint32_t current, max, min;
79+
uint32_t currentReg, maxReg;
7280
int result;
7381

7482
intel_get_mmio(intel_get_pci_device());
7583

7684
current = reg_read(BLC_PWM_CPU_CTL) & BACKLIGHT_DUTY_CYCLE_MASK;
7785
max = reg_read(BLC_PWM_PCH_CTL2) >> 16;
7886

87+
intel_check_pch();
88+
89+
switch( pch )
90+
{
91+
case PCH_LPT:
92+
case PCH_SPT:
93+
case PCH_KBP:
94+
currentReg = 0xc8254;
95+
maxReg = 0xc8254;
96+
break;
97+
98+
default:
99+
currentReg = BLC_PWM_CPU_CTL;
100+
maxReg = BLC_PWM_PCH_CTL2;
101+
break;
102+
}
103+
104+
current = reg_read(currentReg) & BACKLIGHT_DUTY_CYCLE_MASK;
105+
max = reg_read(maxReg) >> 16;
106+
79107
min = 0.5 + 0.5 * max / 100.0; // 0.5%
80108
/*
81-
printf ("min: %d, NUM_ELEMENTS(brightness_levels): %d\n", min,
109+
printf ("min: %d, NUM_ELEMENTS(brightness_levels): %d\n", min,
82110
NUM_ELEMENTS(brightness_levels));
83111
*/
84112
result = 0.5 + current * 100.0 / max;
85-
printf ("current backlight value: %d%% (%d/%d)\n", result, current, max);
86-
113+
printf ("Current backlight value: %d%% (%d/%d)\n", result, current, max);
114+
87115
if (argc > 1) {
88116
uint32_t v;
89-
if (0 == strcmp(argv[1], "incr"))
117+
if (0 == strcmp(argv[1], "incr"))
90118
v = 0.5 + brightness_incr(result) * max / 100.0;
91119
else if (0 == strcmp(argv[1], "decr"))
92120
v = 0.5 + brightness_decr(result) * max / 100.0;
93-
else
94-
v = 0.5 + atoi (argv[1]) * max / 100.0;
95-
/*
121+
else {
122+
errno = 0;
123+
char *endptr;
124+
long val = strtol(argv[1], &endptr, 10);
125+
126+
if((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
127+
|| (errno != 0 && val == 0) || endptr == argv[1]) {
128+
print_usage();
129+
return result;
130+
}
131+
132+
v = 0.5 + val * max / 100.0;
133+
}
134+
/*
96135
printf("v: %d\n", v);
97136
*/
98137
if (v < min)
@@ -102,10 +141,12 @@ int main(int argc, char** argv)
102141
reg_write(BLC_PWM_CPU_CTL,
103142
(reg_read(BLC_PWM_CPU_CTL) &~ BACKLIGHT_DUTY_CYCLE_MASK) | v);
104143
(void) reg_read(BLC_PWM_CPU_CTL);
144+
145+
reg_write(currentReg,
146+
(reg_read(currentReg) &~ BACKLIGHT_DUTY_CYCLE_MASK) | v);
147+
(void) reg_read(currentReg);
105148
result = 0.5 + v * 100.0 / max;
106149
printf ("set backlight to %d%% (%d/%d)\n", result, v, max);
107150
}
108-
109151
return result;
110152
}
111-

intel_gpu_tools.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ enum pch_type {
103103
PCH_IBX,
104104
PCH_CPT,
105105
PCH_LPT,
106+
PCH_SPT,
107+
PCH_KBP,
106108
};
107109

108110
extern enum pch_type pch;

intel_pci.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,15 @@ intel_check_pch(void)
115115
case 0x9c00:
116116
pch = PCH_LPT;
117117
break;
118+
case 0xA100:
119+
case 0x9D00:
120+
pch = PCH_SPT;
121+
break;
122+
case 0xA200:
123+
pch = PCH_KBP;
124+
break;
118125
default:
119126
pch = PCH_NONE;
120127
return;
121128
}
122129
}
123-

0 commit comments

Comments
 (0)