-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiscdevice.c
More file actions
91 lines (76 loc) · 1.5 KB
/
miscdevice.c
File metadata and controls
91 lines (76 loc) · 1.5 KB
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
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/miscdevice.h>
#include <linux/fs.h>
static int o_count = 0;
int sample_open(struct inode *inode, struct file *file)
{
if (o_count)
return -EBUSY; //Only open one time
else
{
o_count++;
return 0;
}
}
int sample_release(struct inode *inode, struct file *file)
{
o_count--;
return 0;
}
ssize_t sample_read(struct file *file, char __user *data, size_t len, loff_t *offset)
{
//sys_read call to do
return 0;
}
ssize_t sample_write(struct file *file, const char __user *data, size_t len, loff_t *offset)
{
//sys_write call to do
return 0;
}
long sample_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
switch (cmd)
{
//sys_ioctl call to do
default:
return -EIOCTLCMD;
break;
}
return 0;
}
static struct file_operations sample_fops = {
.owner = THIS_MODULE,
.open = sample_open,
.release = sample_release,
.read = sample_read,
.write = sample_write,
.unlocked_ioctl = sample_ioctl,
};
static struct miscdevice sample_dev = {
.minor = MISC_DYNAMIC_MINOR,
.name = "sample_dev",
.fops = &sample_fops,
};
static int __init sample_init(void)
{
int ret = -EBUSY;
ret = misc_register(&sample_dev);
if (ret != 0)
{
goto exit1;
}
return 0;
exit1:
return ret;
}
static void __exit sample_exit(void)
{
misc_deregister(&sample_dev);
}
module_init(sample_init);
module_exit(sample_exit);
MODULE_DESCRIPTION("Sample of Misc Device Driver");
MODULE_AUTHOR("Mr.Chowe");
MODULE_LICENSE("GPL v3");