linuxlinux-kernelled

How can I blink LEDs in a Linux kernel module?


I have a laptop with Linux Mint and 4.4.0-78-generic kernel.

Also, I have some LEDs there in the /sys/class/leds directory, and I can turn them on and off in the userspace by executing "echo 255 > brightness" in Bash.

However, I want to switch them in my kernel module. Let’s say, that this module will listen a UDP socket and switch LED based on incoming packet data.

I have googled and found kernel source files called "leds-base.c", "leds-class.c" which contains functions to control LEDs. However, this functions require "struct led_classdev" to be passed, and I don't know where do I should get it.

How can I set LED brightness using its name from /sys/class/leds directory in a kernel module?

PS. I have seen a similar question, but it's about keyboard LEDs only, the LED I want to control is not a keyboard one, and can not be controlled by code in question mentioned before.


Solution

  • Implement an led_trigger class and call

    led_trigger_event(led, LED_FULL);
    

    and friends in your driver. You can bind your trigger to any LED by

    echo my-trigger-name > /sys/class/leds/.../trigger