forked from ljli1990/github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.c
More file actions
110 lines (86 loc) · 2.11 KB
/
Copy pathtest.c
File metadata and controls
110 lines (86 loc) · 2.11 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <linux/init.h>
#include <linux/module.h>
MODULE_AUTHOR("ming.li");
//MODULE_DESCRIPTION(description);
//MODULE_VERSION(version_string);
//MODULE_DEVICE_TABLE(table_info);
//MODULE_ALIAS(alternate_name);
MODULE_LICENSE("Dual BSD/GPL");
//#include <linux/moduleparam.h>
//module_param(variable, type, perm);
//module_param_array(name, type, nump, perm)
//#include <linux/sched.h>
//struct task_struct *current;
//current->pid
//current->comm
#include <linux/types.h> //dev_t
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/slab.h>
static dev_t dev = 0;
static struct cdev * mydev = NULL;
static char *buf = NULL;
int my_open(struct inode *inode, struct file *filp)
{
dev_t i_rdev = inode->i_rdev;
printk(KERN_DEBUG "my_open cdev = %p,major = %d minor = %d\n",imajor(inode),iminor(inode),inode->i_cdev);
if (buf)
{
printk(KERN_DEBUG "err buf is not NULL\n");
return -1;
}
buf = kmalloc(2048,GFP_KERNEL);
printk(KERN_DEBUG "buf = %x\n", buf);
return 0;
}
int my_release(struct inode *inode, struct file *filp)
{
dev_t i_rdev = inode->i_rdev;
printk(KERN_DEBUG "my_open cdev = %p,major = %d minor = %d\n",imajor(inode),iminor(inode),inode->i_cdev);
kfree(buf);
buf = NULL;
return 0;
}
int my_read()
{
}
struct file_operations my_opt = {
.owner = THIS_MODULE;
.open = my_open;
.read = my_read;
.write = my_Write;
.unlocked_ioctl = my_ioctl;
.llseek = my_seek;
.release = my_release;
}
static __init int test_init(void)
{
int res = 0;
printk(KERN_DEBUG "Hello,test!\n");
//res = alloc_chrdev_region(&dev, 0, 1,"test_cdev");
dev = MKDEV(248,0);
res = register_chrdev_region(dev,1,"test_cdev");
if (res < 0)
{
printk(KERN_DEBUG "err");
goto FAIL;
}
printk(KERN_DEBUG "major = %d minor = %d \n",MAJOR(dev),MINOR(dev));
mydev = cdev_alloc();
mydev->ops = &my_opt;
mydev->owner = THIS_MODULE;
cdev->add(mydev, 0, 1);
FAIL:
return 0;
}
static __exit void test_exit(void)
{
printk(KERN_DEBUG "Bye, test!\n");
cdev->del(mydev);
unregister_chrdev_region(dev,1);
}
//EXPORT_SYMBOL (symbol);
//EXPORT_SYMBOL_GPL (symbol);
//
module_init(test_init);
module_exit(test_exit);