forked from wuxinxinggg/linux_kernel_driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.c
More file actions
65 lines (52 loc) · 1.33 KB
/
Copy pathdemo.c
File metadata and controls
65 lines (52 loc) · 1.33 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
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
static ssize_t drv_read(struct file* filp, char* buf, size_t count, loff_t* ppos) {
printk("device read\n");
return count;
}
static ssize_t drv_write(struct file* filp, const char* buf, size_t count, loff_t* ppos) {
printk("device write\n");
return count;
}
static int drv_open(struct inode* inode, struct file* filp) {
printk("device open\n");
return 0;
}
static long drv_ioctl(struct file* filp, unsigned int cmd, unsigned long value) {
printk("device ioctl\n");
return 0;
}
static int drv_release(struct inode* inode, struct file* filp) {
printk("device close\n");
return 0;
}
struct file_operations drv_fops = {
read:
drv_read,
write:
drv_write,
unlocked_ioctl:
drv_ioctl,
open:
drv_open,
release:
drv_release,
};
#define MAJOR_NUM 60
#define MODULE_NAME "DEMO"
static int demo_init(void) {
if (register_chrdev(MAJOR_NUM, "demo", &drv_fops) < 0) {
printk("<1>%s: can't get major %d\n", MODULE_NAME, MAJOR_NUM);
return (-EBUSY);
}
printk("<1>%s: started\n", MODULE_NAME);
return 0;
}
static void demo_exit(void) {
unregister_chrdev(MAJOR_NUM, "demo");
printk("<1>%s: removed\n", MODULE_NAME);
}
module_init(demo_init);
module_exit(demo_exit);