Search code examples
clinux-kernellinux-device-driverembedded-linuxdevice-tree

Linux Kernel Module never reaches ->probe() functions


Summary

I want to write a simple Linux Kernel Module, which will initialize a GPIO from information it got from device tree. But currently my module never enters the probe function. To break it down, I have created a minimal example without the GPIO part. What I want to see in this minimal kernel module, is that the probe and remove functions are called correctly.

HW & Kernel

I am running a Raspberry Pi 3 with the following kernel:

$ uname -a
Linux raspberrypi 5.10.92-v7+ #1514 SMP Mon Jan 17 17:36:39 GMT 2022 armv7l GNU/Linux

Device Tree Overlay

Here is the code of my device tree overlay:

/dts-v1/;
/plugin/;
/ {
        compatible = "raspberrypi,3-model-b-plusbrcm,bcm2837";
        fragment@0 {
        target-path = "/";
        __overlay__ {
                jo_test {
                        compatible = "j4l,testdev";
                        status="enabled";
                        label = "Test";
                        };
                };
        };
};

I compile it with the following command:

dtc -W  no-unit_address_vs_reg -I dts -O dtb -o testoverlay.dtbo testoverlay.dts

Then I load it with:

sudo dtoverlay -d . testoverlay.dtbo

Now I can see my overlay in /proc/device-tree and the values are also correctly assigned:

$ sudo ls /proc/device-tree/jo_test
compatible  label  name  status
$ for afile in $(sudo ls /proc/device-tree/jo_test); do echo Name: $afile; sudo cat /proc/device-tree/jo_test/$afile; echo ""; done
Name: compatible
j4l,testdev
Name: label
Test
Name: name
jo_test
Name: status
enabled

Kernel Module

Here is my minimal Linux Kernel Module. I used the slides from Device tree for dummies as a reference ([You can find it here][1], slides 16-18)/ [1]: https://bootlin.com/pub/conferences/2014/elc/petazzoni-device-tree-dummies/petazzoni-device-tree-dummies.pdf

#include <linux/module.h>
#include <linux/init.h>
#include <linux/proc_fs.h>
#include <linux/gpio.h>
#include <linux/mod_devicetable.h>
#include <linux/gpio/consumer.h>
#include <linux/property.h>
#include <linux/platform_device.h>
#include <linux/of_device.h>

/* Meta Information */
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Johannes 4 GNU/Linux");
MODULE_DESCRIPTION("Module creates a folder and file in procfs and implements read and write callbacks");

static int dt_probe(struct platform_device *pdev);
static int dt_remove(struct platform_device *pdev);

static struct of_device_id my_ids[] = {
    {
        .compatible = "j4l,testdev",
    }, { /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, my_ids);

static struct platform_driver my_driver = {
    .probe = dt_probe,
    .remove = dt_remove,
    .driver = {
        .name = "my_driver",
        .of_match_table = my_ids,
    },
};

static int dt_probe(struct platform_device *pdev)
{
        struct device *dev = &pdev->dev;
        const struct of_device_id *of_id = of_match_device(my_ids, dev);

        printk("dt_test - Now I am in dt_probe\n");
        if (!of_id) {
                printk("test_dt - Something went wrong...\n");
                return -1;
        }

        return 0;
}

static int dt_remove(struct platform_device *pdev)
{
        struct device *dev = &pdev->dev;
        const struct of_device_id *of_id = of_match_device(my_ids, dev);

        printk("dt_test - Now I am in dt_remove\n");
        if (!of_id) {
                printk("test_dt - Something went wrong...\n");
        } else {
                printk("dt_test - of_id->name: %s\n",of_id->name);
                printk("dt_test - of_id->compatible: %s\n",of_id->compatible);
        }
        return 0;
}

/**
 * @brief This function is called, when the module is loaded into the kernel
 */
static int __init my_init(void)
{
        int error;
        printk("dt_test - Module Init\n");

        error = platform_driver_register(&my_driver);
        printk("dt_test: error=%d\n", error);
        if (error) {
                printk("dt_test - Error probing \n");
                return error;
        }

        return 0;
}

/**
 * @brief This function is called, when the module is removed from the kernel
 */
static void __exit my_exit(void)
{
        printk("dt_test - Removing module\n");
        platform_driver_unregister(&my_driver);
}

module_init(my_init);
module_exit(my_exit);

And here is my Makefile:

obj-m += dt_test.o

all:
        make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
        make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

Run my example

After compiling the module, inserting the DT-overlay and then the Kernel Module, I see the following in the kernel's log:

$ sudo insmod dt_test.ko
$ dmesg | tail -n 5
[   33.755037] cam1-reg: disabling
[   33.755061] cam-dummy-reg: disabling
[  811.832947] dt_test: loading out-of-tree module taints kernel.
[  811.833341] dt_test - Module Init
[  811.833643] dt_test: error=0

So, my Module never enters the dt_probe() function, otherwise I should see the "Now I am in dt_probe" print. And I don't know, what the "loading out-of-tree module" message means. It seems, the module can't find the compatible section in the device tree...

On a second try, I have copied the compiled overlay to /boot/overlays and added the overlay in the /boot/config.txt with dtoverlay=testoverlay. The overlay appears in /proc/device-tree, but I have the same behaviour on loading the module...

Do you know, what I am doing wrong or do you have any tips, how I can debug this?


Solution

  • I got it working. Removing the status="enabled" with status = "okay" fixed it. The problem was, I had an earlier version of the OVerlay copied into /boot/overlay and in /boot/config/ my overlay was enabled. So, on every boot a false version of my overlay was loaded. But after removing the overlay from /boot/overlay and from /boot/config it worked. My result can be found here: https://github.com/Johannes4Linux/Linux_Driver_Tutorial/tree/main/20_dt_probe