#include<linux/init.h>
#include<linux/module.h>
#include<linux/version.h>
#include<linux/types.h>
#include<linux/fs.h>
#include<linux/device.h>
#include<linux/cdev.h>
#include<linux/kdev_t.h>
#include<linux/kernel.h>
#include<asm/uaccess.h>
MODULE_LICENSE("GPL");
MODULE_VERSION("0.1");
static dev_t first=MKDEV(0,0);
static struct cdev c_dev;
static struct class *cl;
static int my_open(struct inode *inode,struct file *filp)
{
printk(KERN_INFO "driver is now open");
return 0;
}
static int my_close(struct inode *inode,struct file *filp)
{
printk(KERN_INFO "the driver is closed");
return 0;
}
static ssize_t my_read(struct file *filp,char __user *buf,size_t len,loff_t *off)
{
printk(KERN_INFO "the driver is reading");
return 0;
}
static ssize_t my_write(struct file *filp,const char __user *buff ,size_t len,loff_t *off)
{
printk(KERN_INFO "the driver is writing");
return len;
}
static struct file_operations fops={
.owner= THIS_MODULE,
.open=my_open,
.read=my_read,
.write=my_write,
.release=my_close,
};
static int __init my_init(void)
{
printk(KERN_ALERT "Device Init running");
if(alloc_chrdev_region(&first,0,1,"Simple_char_driver")<0)
{
return -1;
}
if((cl=class_create(THIS_MODULE,"Simple_char_driver"))==NULL)
{
unregister_chrdev_region(first,1);
return -1;
}
if(device_create(cl,NULL,first,NULL,"Simple_char_driver")==NULL)
{
class_destroy(cl);
unregister_chrdev_region(first,1);
return -1;
}
cdev_init(&c_dev,&fops);
c_dev.owner=THIS_MODULE;
if(cdev_add(&c_dev,first,1)==-1)
{
device_destroy(cl,first);
class_destroy(cl);
unregister_chrdev_region(first,1);
return -1;
}
return 0;
}
static void __exit my_exit(void)
{
cdev_del(&c_dev);
device_destroy(cl,first);
class_destroy(cl);
unregister_chrdev_region(first,1);
printk(KERN_INFO " driver running __exit");
}
module_init(my_init);
module_exit(my_exit);
MODULE_AUTHOR("ADITHYA");
MODULE_DESCRIPTION("Simple char driver");