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
For kernel versions < 3.10, this API was
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:
Output:
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);
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_CODEThis 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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); |
Output:
Very nice Post keep sharing it helps me a lot.
ReplyDeleteThank You!
mac repair adelaide, Adelaide pc repairs
computer repairs perth