4

I am using sama5d27-som1-ek1 embedded board which i build for it Linux image operating system and a cross compiler with YOCTO project.

I wanted to test a C code on my board. This code creates a new userspace LED class device and monitors it. A timestamp and brightness value is printed each time the brightness changes. I compiled it with the corss compiler but when i tried to run it, it tells me:

Failed to open /dev/uleds: No such file or directory

When i check /dev directory I can't find uleds. I think that this is the problem. Do you have any suggestions ?

This is the code:

#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#include <linux/uleds.h>

int main(int argc, char const *argv[])
{
    struct uleds_user_dev uleds_dev;
    int fd, ret;
    int brightness;
    struct timespec ts;

    if (argc != 2) {
        fprintf(stderr, "Requires <device-name> argument\n");
        return 1;
    }

    strncpy(uleds_dev.name, argv[1], LED_MAX_NAME_SIZE);
    uleds_dev.max_brightness = 100;

    fd = open("/dev/uleds", O_RDWR);
    if (fd == -1) {
        perror("Failed to open /dev/uleds");
        return 1;
    }

    ret = write(fd, &uleds_dev, sizeof(uleds_dev));
    if (ret == -1) {
        perror("Failed to write to /dev/uleds");
        close(fd);
        return 1;
    }

    while (1) {
        ret = read(fd, &brightness, sizeof(brightness));
        if (ret == -1) {
            perror("Failed to read from /dev/uleds");
            close(fd);
            return 1;
        }
        clock_gettime(CLOCK_MONOTONIC, &ts);
        printf("[%ld.%09ld] %u\n", ts.tv_sec, ts.tv_nsec, brightness);
    }

    close(fd);

    return 0;
}
gaston
  • 405
  • 5
  • 22

1 Answers1

1

First of all check your kernel config for CONFIG_LEDS_USER it should be like built-in (compiled in) module y or loadable module m. You can check it on already compiled and running kernel

  1. zcat /proc/config.gz | grep LEDS_USER
  2. cat /boot/config | grep LEDS_USER
  3. cat /boot/config-$(uname -r) | grep LEDS_USER

Enable this option in config and rebuild your kernel. If I not mistaken you can add this line to the kernel config and YOCTO should use it as is. Or another way is to make it like the patch for config and add this patch to .bb kernel rule and YOCTO applies it during the building of a project.

Then use insmod to insert your module if it configures like loadable module m. If you choose y option /dev/uleds should be present by default

After these steps /dev/uleds should appear.

Nick S
  • 1,299
  • 1
  • 11
  • 23
  • I checked the MENU CONFIG of yocto with tapping _make menuconfig_ to change or make some modifications for .config of my linux kernel. I found under /device drivers/Leds support an option that was not selected which is **user space leds** so i selected it with "y" then i saved and rebuild the kernel. But same problem ! can't find uleds ! – gaston Aug 24 '19 at 23:49
  • @gaston are you sure that you recompile kernel with this option? If yes, try to do it with ‘m’ and load module manually. – Nick S Aug 26 '19 at 04:48
  • I followed the steps of Configure and build Linux kernel from here : https://www.at91.com/linux4sam/bin/view/Linux4SAM/Sama5d27Som1EKMainPage .. And after making modifications and recompiling I verified again the menuconfig and i found that my modifications still exist in it. I will try with 'm' now – gaston Aug 26 '19 at 07:39
  • Now i re-compiled the kernel with **user-space-leds** selected as 'm' and i can find these files : **uleds.ko /uleds.mod.c/uleds.o/uleds.mod.o** under _at91-linux_ directory under my Host machine. How can I load the desired file on my target machine ( SAMA5D27 ) with insmod ? – gaston Aug 26 '19 at 09:23
  • @gaston if you already find module use `insmod`. like this `insmod uleds.ko` from the folder with module. And probably you will need `root` rights to load new module to the kernel so use `sudo` if you not under root. – Nick S Aug 27 '19 at 10:47
  • In fact I copied the uleds.ko module found under my Yocto host machine drivers to _/lib/modules_ folder in my target machine using `CAT` command ( I did not find any other solution to bring it there! ). And as you said I'm trying to load it to the kernel using `insmod` command but I have a problem of versions! the version of the module is not the same version of my linux kernel! I posted a new question here : https://stackoverflow.com/questions/57670786/yocto-cant-insert-linux-module-to-the-kernel-versions-are-different – gaston Aug 27 '19 at 10:55