What’s the problem

If you have any sort of ASUS notebook and run any Linux distro on it using wayland, you may have encountered several problems. Maybe not the most severe, yet definitely one of the most annoying problems for me was the fact, that I couldn’t turn off my keyboard LEDs. I did not really care about the color, because most of the time I don’t have them running anyways. So I didn’t even care about any advanced driver functionality, and yet was left in the RGB by the firmware. So I looked for solutions for my ASUS Vivobook S15 with an IT5570E and quickly found VRGB. The tool offers the perfect functionality for my purpose and using it, my problem was solved. At least till the reboot, which forced me to put the tool into OpenRC (or manually call it every time). Mostly due to that, it came with two problems

  1. It is a python project
  2. It offers way more functionality than I need

The first point became a problem, because I try to avoid starting up a python environment in the boot as far as avoidable. Yet, I either start VRGB every time manually or I accept some sort of bloat in my init. Then the second problem arises, as any sort of bloat (yet useful features nonetheless, just not for me, who only wants to fix the problem) will result in more security concerns. So, I have to give an outside tool udev permissions and put it in my init. For that I naturally check out the code for any sort of security concerns in the code. Every feature there is just more work for me in ensuring safety. That is too much work for the simple problem I had, so I decided to write my own userspace program in C++ to make the LED brightness controllable again and additionally (as the only accepted feature) also set it to a given color.

The approach

I wrote a CLI which can be put in your init system and if you have the same device you will be well off. Otherwise I will propose the strategy, which worked for me and may work for you too, no matter of the device, if you encounter the same problem. If so, please also feel free to add your device (and of course the fix) to the repo. As I highly value simplicity, I will also propose the minimal code to achieve it for my device, so in the end there is not even need for my CLI and you can just execute the minimal code. This was particularly what I was looking for initially and couldn’t find, so I hope this blog shortens the search for many. Also I will give the approach from a kernel space angle for anyone that is interested. Yet, doing that simple job in kernel space is like shooting a rabbit with a nuke and is highly discouraged.

HID

Your device needs to expose itself via HID (HID - Kernel Docs). For my device that happens in a sufficient manner when the asus_nb_wmi module is loaded. You can check your HID devices (This literally translates to Human Interface Device devices, but the kernel refers to it like that, and I will oblige) investigating the /sys/class/hidraw/ directory. So, with

ls -l /sys/class/hidraw/

To then find your specific device, you need to parse

cat /sys/class/hidraw/hidrawX/device/uevent

for each device individually. Once found, you can send ioctl requests to /dev/hidrawX.

The minimal example

With that information we also get directly to the minimum working example for my device, and use it for further explanations. For your device that can be totally different and you should always confront it yourself. If you have an IT5570E in an ASUS Vivobook, that could work out of the box for you. Please note, that the order of the HID devices is not guaranteed the same for every boot, so if you run it as an init service, you need to extend this in order to automatically find your device before applying this.

#include <fcntl.h>
#include <linux/hidraw.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <unistd.h>

int main() {
  // replace this with your device
  int fd = open("/dev/hidrawX", O_RDWR);
  if (fd < 0) {
    fprintf(stderr, "Could not open device\n");
    goto error;
  }

  uint8_t autonomous_on[2] = {0x0b, 0x01};
  if (ioctl(fd, HIDIOCSFEATURE(sizeof(autonomous_on)), autonomous_on) < 0) {
    perror("Error sending autonomous on");
    goto error;
  }

  uint8_t autonomous_off[2] = {0x0b, 0x00};
  if (ioctl(fd, HIDIOCSFEATURE(sizeof(autonomous_off)), autonomous_off) < 0) {
    perror("Error sending autonomous off");
    goto error;
  }

  // red with full intensity for the example (0xff0000ff)
  uint8_t lamp_range_update_report[10] = {0x05, 0x01, 0x00, 0x00, 0x00,
                                          0x00, 0xff, 0x00, 0x00, 0xff};

  if (ioctl(fd, HIDIOCSFEATURE(sizeof(lamp_range_update_report)),
            lamp_range_update_report) < 0) {
    perror("Error sending lamp update report");
    goto error;
  }
  close(fd);
  return 0;

error:
  if (fd >= 0) {
    close(fd);
  }
  return 1;
}

And with this it becomes clear, that you really do not need any full blown library with all sort of color support, just to get your keyboard leds working in a suitable way.

Takeaways

You may have spotted that we turn the autonomous mode on first, before we turn it off. This does not make any sense in regular HID handling. For that we confront HUT 1_7 (p. 338). This states, that after the interrogation of the necessary attributes (which we have done implicitly in the minimal example, because we know the device), you turn off the autonomous mode, perform your update and turn it on again. I experienced, that when I try that my keyboard doesn’t react to it. It simply ignores the autonomous off and the following color request. This weird behavior is probably also the reason for the problem. It seems like the controller starts up in a different state (maybe even error state) and turning on autonomous mode resets that. We can rule out, that it was in autonomous mode before because then turning it on again should be a no-op (HUT 1_7, p. 346 ) and we can rule out is turned off, because then we could switch the color and the current color would persist till turned on again (also on p.346). Yet, all this of course only holds, given the firmware holds to the standard. In any way, the fix works. Now, let’s look at the commands. You can see, that those consist of an opcode in the first position. You can retrieve them by asking HIDIOCGRDESC. For a program running in your init it may still be best practice to look up the opcodes from there, yet it is quite wasteful, because they will not change without a firmware update (and most likely not even then). Also you would need to have a parser for that which is also more work and we focus here on a simple solution. The LampRangeUpdate offers (like the name already tells) a range in which the update should be applied. That can also be retrieved via the HIDIOCGRDESC and is for my device 1. So the range starts at index 0 and ends at index 0.

The CLI

This section is there for convenience, as the most important concepts are discussed already. So, if you want to have a convenient tool, which takes care of what we discussed here and your device is the IT5570E (or any other device if supported, like the Roccat Vulcan TKL), you can check out Github KBD LED Ctl. It still holds to the philosophy, that investigating the whole code is fairly fast, because of a small code base. With the given command line arguments you can also parse your hidraw devices and look for your device and proceed from there. Note that the device is hardcoded and the HIDIOCGRDESC is never parsed. This may be a problem for some, but was an explicit design choice to keep the program as small as possible. Another explicit design choice is that you can compile in only the devices you need. That then differentiates it from tools like OpenRGB which forces you to have all handlers in your binary, while you will never need 99% of them. Also, the cli is completely dependency-free.

Bonus: LKM performing the request

This is of course still not recommended in any way, yet it may be interesting to see, that the functionality can also be implemented in kernel space only. Note, that there is explicitly no error handling, to make it more readable.

#include <linux/hid.h>
#include <linux/kernel.h>
#include <linux/module.h>

static int led_probe(struct hid_device *hdev, const struct hid_device_id *id) {
  printk("Probe is running\n");
  (void)hid_parse(hdev);
  (void)hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  (void)hid_hw_open(hdev);

  u8 autonomous_on[2] = {0x0B, 0x01};
  (void)hid_hw_raw_request(hdev, autonomous_on[0], autonomous_on,
                           sizeof(autonomous_on), HID_FEATURE_REPORT,
                           HID_REQ_SET_REPORT);
  u8 autonomous_off[2] = {0x0B, 0x00};
  (void)hid_hw_raw_request(hdev, autonomous_off[0], autonomous_off,
                           sizeof(autonomous_off), HID_FEATURE_REPORT,
                           HID_REQ_SET_REPORT);

  u8 lamp_range_update[10] = {0x05, 0x01, 0x00, 0x00,
                              0x00, 0x00, 
                              0xff, 0x00, 0x00, 0xff};
  (void)hid_hw_raw_request(hdev, lamp_range_update[0], lamp_range_update,
                           sizeof(lamp_range_update), HID_FEATURE_REPORT,
                           HID_REQ_SET_REPORT);
  return 0;
}

static void led_remove(struct hid_device *hdev) {
  hid_hw_close(hdev);
  hid_hw_stop(hdev);
}

static const struct hid_device_id ite57709e_device_id[] = {
    {HID_DEVICE(HID_BUS_ANY, HID_GROUP_ANY, 0x0b05, 0x19b6)}, {}};

static struct hid_driver ite57709e_led_driver = {.name = "ite57709e_led_driver",
                                                 .id_table =
                                                     ite57709e_device_id,
                                                 .probe = led_probe,
                                                 .remove = led_remove};

module_hid_driver(ite57709e_led_driver)