Single Linux Device Driver code for multiple Linux versions using LINUX_VERSION

The arguments of the kernel functions changes with new releases.

For example, Kernel versions > 3.10 use the below function to create a proc entry

  proc_file_entry = proc_create("proc_file_name", 0, NULL, &proc_file_fops);

For kernel versions < 3.10, this API was

  proc_file_entry = create_proc_entry("proc_file_name", 0, NULL);
If you want to have a Linux Device Driver which supports multiple Linux Versions, use the macro LINUX_VERSION_CODE

This macro expands to the binary representation of the kernel version
For example for Linux version 2.6.10, the value is 132618(0x02060a)

There is another macro KERNEL_VERSION(major, minor, release). This builds up the version number from the individual numbers. For example KERNEL_VERSION(2,6,10) will return 132618

Sample Code:

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <asm/current.h>
#include <linux/sched.h>
#include <generated/utsrelease.h>
#include <linux/version.h>
MODULE_LICENSE("GPL");
static int __init kernel_version_init(void)
{
//Now depends on our Kernel version we could log a diffrent comment in the Kernel log
#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 10)
printk(KERN_INFO "KERNELVERSION: Hello OLD Kernel %s\n", UTS_RELEASE);
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(4, 10, 0)
printk(KERN_INFO "KERNELVERSION: Hello NEW Kernel %s\n", UTS_RELEASE);
#else
printk(KERN_INFO "KERNELVERSION: Hello Moderate Kernel %s\n", UTS_RELEASE);
#endif
return 0;
}
static void __exit kernel_version_exit(void)
{
printk(KERN_INFO "KERNELVERSION: GoodBye.\n");
}
module_init(kernel_version_init);
module_exit(kernel_version_exit);
view raw kernelversion.c hosted with ❤ by GitHub

Output:




Comments

Post a Comment

Popular posts from this blog

bb.utils.contains yocto

Difference between RDEPENDS and DEPENDS in Yocto

PR, PN and PV Variable in Yocto