This source file includes following definitions.
- register_chrdev
- register_blkdev
- blkdev_open
- chrdev_open
1
2
3
4
5
6
7
8
9 #include <linux/fs.h>
10 #include <linux/string.h>
11 #include <linux/sched.h>
12 #include <linux/ext_fs.h>
13 #include <linux/tty.h>
14 #include <linux/stat.h>
15 #include <linux/fcntl.h>
16 #include <linux/errno.h>
17
18 struct file_operations * chrdev_fops[MAX_CHRDEV] = {
19 NULL,
20 };
21
22 struct file_operations * blkdev_fops[MAX_BLKDEV] = {
23 NULL,
24 };
25
26 int register_chrdev(unsigned int major, const char * name, struct file_operations *fops)
27 {
28 if (major >= MAX_CHRDEV)
29 return -EINVAL;
30 if (chrdev_fops[major])
31 return -EBUSY;
32 chrdev_fops[major] = fops;
33 return 0;
34 }
35
36 int register_blkdev(unsigned int major, const char * name, struct file_operations *fops)
37 {
38 if (major >= MAX_BLKDEV)
39 return -EINVAL;
40 if (blkdev_fops[major])
41 return -EBUSY;
42 blkdev_fops[major] = fops;
43 return 0;
44 }
45
46
47
48
49 int blkdev_open(struct inode * inode, struct file * filp)
50 {
51 int i;
52
53 i = MAJOR(inode->i_rdev);
54 if (i >= MAX_BLKDEV || !blkdev_fops[i])
55 return -ENODEV;
56 filp->f_op = blkdev_fops[i];
57 if (filp->f_op->open)
58 return filp->f_op->open(inode,filp);
59 return 0;
60 }
61
62
63
64
65
66
67 struct file_operations def_blk_fops = {
68 NULL,
69 NULL,
70 NULL,
71 NULL,
72 NULL,
73 NULL,
74 NULL,
75 blkdev_open,
76 NULL,
77 };
78
79 struct inode_operations blkdev_inode_operations = {
80 &def_blk_fops,
81 NULL,
82 NULL,
83 NULL,
84 NULL,
85 NULL,
86 NULL,
87 NULL,
88 NULL,
89 NULL,
90 NULL,
91 NULL,
92 NULL,
93 NULL,
94 NULL
95 };
96
97
98
99
100 int chrdev_open(struct inode * inode, struct file * filp)
101 {
102 int i;
103
104 i = MAJOR(inode->i_rdev);
105 if (i >= MAX_CHRDEV || !chrdev_fops[i])
106 return -ENODEV;
107 filp->f_op = chrdev_fops[i];
108 if (filp->f_op->open)
109 return filp->f_op->open(inode,filp);
110 return 0;
111 }
112
113
114
115
116
117
118 struct file_operations def_chr_fops = {
119 NULL,
120 NULL,
121 NULL,
122 NULL,
123 NULL,
124 NULL,
125 NULL,
126 chrdev_open,
127 NULL,
128 };
129
130 struct inode_operations chrdev_inode_operations = {
131 &def_chr_fops,
132 NULL,
133 NULL,
134 NULL,
135 NULL,
136 NULL,
137 NULL,
138 NULL,
139 NULL,
140 NULL,
141 NULL,
142 NULL,
143 NULL,
144 NULL,
145 NULL
146 };