title: How can I blink LEDs in a Linux kernel module?
body: <p>I have a laptop with <a href="https://en.wikipedia.org/wiki/Linux_Mint" rel="nofollow noreferrer">Linux Mint</a> and 4.4.0-78-generic kernel.</p>
  <p>Also, I have some <a href="https://en.wikipedia.org/wiki/Light-emitting_diode" rel="nofollow noreferrer">LEDs</a> there in the <em>/sys/class/leds</em> directory, and I can turn them on and off in the userspace by executing &quot;echo 255 &gt; brightness&quot; in Bash.</p>
  <p>However, I want to switch them in my kernel module. Let’s say, that this module will listen a <a href="https://en.wikipedia.org/wiki/User_Datagram_Protocol" rel="nofollow noreferrer">UDP</a> socket and switch LED based on incoming packet data.</p>
  <p>I have googled and found kernel source files called &quot;leds-base.c&quot;, &quot;leds-class.c&quot; which contains functions to control LEDs. However, this functions require &quot;struct led_classdev&quot; to be passed, and I don't know where do I should get it.</p>
  <p>How can I set LED brightness using its name from /sys/class/leds directory in a kernel module?</p>
  <p>PS. I have seen a similar question, but it's about <em>keyboard</em> LEDs only, the LED I want to control is not a keyboard one, and can not be controlled by code in question mentioned before.</p>