Code for creating IOCTL command in linux kernel 5.3 . The details of creation and usage of kerenl is given in the post here
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
/* The code has been tested on kerenl 5.3 | |
The steps to use the icotl in user space is given in | |
http://tuxthink.blogspot.com/2011/01/creating-ioctl-command.html | |
*/ | |
#include <linux/module.h> | |
#include <linux/kernel.h> | |
#include <linux/fs.h> // required for various structures related to files liked fops. | |
#include <linux/semaphore.h> | |
#include <linux/cdev.h> | |
#include "ioctl_basic.h" | |
#include <linux/version.h> | |
static int Major; | |
#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,36) | |
#define UNLOCKED 1 | |
#endif | |
int open(struct inode *inode, struct file *filp) | |
{ | |
printk(KERN_INFO "Inside open \n"); | |
return 0; | |
} | |
int release(struct inode *inode, struct file *filp) { | |
printk (KERN_INFO "Inside close \n"); | |
return 0; | |
} | |
#ifdef UNLOCKED | |
long int ioctl_funcs(struct file *filp,unsigned int cmd, unsigned long arg) | |
{ | |
int ret=0; | |
switch(cmd) { | |
case IOCTL_HELLO: | |
printk(KERN_INFO "Hello ioctl world"); | |
break; | |
} | |
return ret; | |
} | |
struct file_operations fops = { | |
open: open, | |
unlocked_ioctl: ioctl_funcs, | |
release: release | |
}; | |
#else | |
int ioctl_funcs(struct inode *inode, struct file *filp, | |
unsigned int cmd, unsigned long arg) | |
{ | |
int data=10,ret; | |
switch(cmd) { | |
case IOCTL_HELLO: | |
printk(KERN_INFO "Hello ioctl world"); | |
break; | |
} | |
return ret; | |
} | |
struct file_operations fops = { | |
open: open, | |
ioctl: ioctl_funcs, | |
release: release | |
}; | |
#endif | |
struct cdev *kernel_cdev; | |
int char_arr_init (void) { | |
int ret; | |
dev_t dev_no,dev; | |
kernel_cdev = cdev_alloc(); | |
kernel_cdev->ops = &fops; | |
kernel_cdev->owner = THIS_MODULE; | |
printk (" Inside init module\n"); | |
ret = alloc_chrdev_region( &dev_no , 0, 1,"char_arr_dev"); | |
if (ret < 0) { | |
printk("Major number allocation is failed\n"); | |
return ret; | |
} | |
Major = MAJOR(dev_no); | |
dev = MKDEV(Major,0); | |
printk (" The major number for your device is %d\n", Major); | |
ret = cdev_add( kernel_cdev,dev,1); | |
if(ret < 0 ) | |
{ | |
printk(KERN_INFO "Unable to allocate cdev"); | |
return ret; | |
} | |
return 0; | |
} | |
void char_arr_cleanup(void) { | |
printk(KERN_INFO " Inside cleanup_module\n"); | |
cdev_del(kernel_cdev); | |
unregister_chrdev_region(Major, 1); | |
} | |
MODULE_LICENSE("GPL"); | |
module_init(char_arr_init); | |
module_exit(char_arr_cleanup); |