Skip to content

Instantly share code, notes, and snippets.

@hadess
Created October 5, 2013 23:19
Show Gist options
  • Save hadess/6847281 to your computer and use it in GitHub Desktop.
Save hadess/6847281 to your computer and use it in GitHub Desktop.
Hack to control the keyboard backlight level on a Thinkpad Lenovo X1 Carbon Touch
/* gcc -o tmp `pkg-config --libs --cflags glib-2.0` tmp.c
*
* # modprobe ec_sys
*
* # watch -n 0.1 hexdump -C /sys/kernel/debug/ec/ec0/io
*
* 00000000 a7 05 a0 e2 00 86 05 00 00 00 47 00 00 03 00 10 |..........G.....|
* 00000000 a7 05 a0 e2 00 86 05 00 00 00 47 00 00 43 00 10 |..........G..C..|
* 00000000 a7 05 a0 e2 00 86 05 00 00 00 47 00 00 83 00 10 |..........G.....|
*
* # ./tmp 0
* # ./tmp 1
* # ./tmp 2
*
*/
#include <glib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
char levels[] = {
0x03,
0x43,
0x83
};
static void
usage (char **argv)
{
g_print ("%s [level]\n", argv[0]);
g_print ("Where level is between 1 and 3\n");
}
int main (int argc, char **argv)
{
int fd;
int level;
if (argc < 2) {
usage (argv);
return 1;
}
level = atoi(argv[1]);
if (level < 0 || level > 3) {
usage (argv);
return 1;
}
fd = open ("/sys/kernel/debug/ec/ec0/io", O_RDWR);
if (fd < 0) {
g_print ("open: %s\n", g_strerror (errno));
return 1;
}
if (lseek (fd, 0xd, SEEK_CUR) < 0) {
g_print ("seek: %s\n", g_strerror (errno));
return 1;
}
if (write (fd, &levels[level], 1) < 0) {
g_print ("write: %s\n", g_strerror (errno));
return 1;
}
return 0;
}
@bruno066
Copy link

I am experiencing the same issue as @maltinho, with the same laptop. By the way, even if I expect no difference, my thinkpad is not a touch one.

Hopping this will be solved, thanks in advance

@epi
Copy link

epi commented Sep 28, 2015

Works on ThinkPad T430s with Ubuntu 14.04, thanks! It's really helpful since my Fn key stopped working.

@chriswareham
Copy link

Here's a version that doesn't need libglib:

/* gcc -Wall -Werror -o backlight backlight.c
 *
 * # modprobe ec_sys
 *
 * # watch -n 0.1 hexdump -C /sys/kernel/debug/ec/ec0/io
 *
 * 00000000  a7 05 a0 e2 00 86 05 00  00 00 47 00 00 03 00 10  |..........G.....|
 * 00000000  a7 05 a0 e2 00 86 05 00  00 00 47 00 00 43 00 10  |..........G..C..|
 * 00000000  a7 05 a0 e2 00 86 05 00  00 00 47 00 00 83 00 10  |..........G.....|
 *
 * # ./backlight 0
 * # ./backlight 1
 * # ./backlight 2
 *
 */

#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

static char levels[] = {
    0x03,
    0x43,
    0x83
};

static void
usage(char **argv)
{
    printf("%s [level]\n", argv[0]);
    printf("Where level is between 1 and 3\n");
}

int
main(int argc, char **argv)
{
    int fd;
    int level;

    if (argc < 2) {
        usage(argv);
        return 1;
    }

    level = atoi(argv[1]);

    if (level < 0 || level > 3) {
        usage(argv);
        return 1;
    }

    fd = open("/sys/kernel/debug/ec/ec0/io", O_RDWR);

    if (fd < 0) {
        printf("open: %s\n", strerror(errno));
        return 1;
    }

    if (lseek(fd, 0xd, SEEK_CUR) < 0) {
        printf("seek: %s\n", strerror(errno));
        return 1;
    }

    if (write(fd, &levels[level], 1) < 0) {
        printf("write: %s\n", strerror(errno));
        return 1;
    }

    return 0;
}

@3v1n0
Copy link

3v1n0 commented May 5, 2016

@maltinho, bruno066, just sudo modprobe ec_sys to get that (if your kernel is configured to ship it as a module)

@vzaliva
Copy link

vzaliva commented May 22, 2016

Here is a bash script version which does not require compilation. Works on my X260.

https://gist.github.com/vzaliva/0adba7bc40e2f31a0b5f802af2a63267

Thanks for figuring out registers!

@tokyovigilante
Copy link

On Fedora at least, compiled versions need chown root:root <filename>; chmod +s <filename> to gain root permissions.

The ec_sys kernel module is not loaded automatically on my system (X230 Tablet) so needs # echo ec_sys > /etc/modules_load.d to load, and #echo options ec_sys write-support=1 > /etc/modprobe.d/ec_sys.conf to be able to write to the relevant device persistently after reboot.

Once that's done, works like a charm!

@thusfar7
Copy link

thusfar7 commented Oct 4, 2020

Can someone tell me how to set this up on X1 Ubuntu 20 so that backlight is persistent? Not sure what steps to take so if someone can write step by step guide.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment