/
simple_char_driver.c
113 lines (68 loc) · 1.8 KB
/
simple_char_driver.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include<linux/init.h>
#include<linux/module.h>
#include<linux/cdev.h>
#include<linux/sched.h>
#include<linux/uaccess.h>
#include<linux/types.h>
#include<linux/kernel.h>
#include<linux/fs.h>
#include<linux/device.h>
dev_t dev;
struct cdev my_char_driver;
static int ela_open(struct inode *i, struct file *f)
{
printk("opened the device file \n");
return 0;
}
static int ela_read(struct file *f, char __user *ubuff, size_t len, loff_t *off)
{
printk("reading the device file \n");
return 0;
}
static int ela_write(struct file *f, size_t len, loff_t *off)
{
printk("writing the device file \n");
return 0;
}
static int ela_release(struct inode *i, struct file *f)
{
printk("closing the device file \n");
return 0;
}
static struct file_operations fopz=
{
.open=ela_open,
.write=ela_write,
.read=ela_read,
.release=ela_release,
};
struct class *class_var;
static int ela_init(void)
{
int ret_val;
ret_val=alloc_chrdev_region(&dev,7,3,"Char_dvr"); //dynamic allocation of major number
if(ret_val<0)
{
printk("error in allocating character driver..\n");
}
cdev_init(&my_char_driver,&fopz); //connect file operations with cdev
cdev_add(&my_char_driver,dev,3); //connect major&minor number with cdev
class_var=class_create(THIS_MODULE,"Char_dvr"); //populate sysfs entries
device_create(class_var,NULL,dev,NULL,"Char_dvr"); //send uevents to udev // dev/char_dvr will be created
printk("ELA: In init function ... \n");
return 0;
}
static int ela_exit(void)
{
printk("ELA: In exit function ... \n");
cdev_del(&my_char_driver);
device_destroy(class_var,dev);
class_destroy(class_var);
unregister_chrdev_region(dev,3);
return 0;
}
module_init(ela_init);
module_exit(ela_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Elango");
MODULE_DESCRIPTION("Tiny character driver");