This source file includes following definitions.
- get_blkfops
- get_chrfops
- register_chrdev
- register_blkdev
- unregister_chrdev
- unregister_blkdev
- blkdev_open
- chrdev_open
1
2
3
4
5
6
7
8
9 #include <linux/fs.h>
10 #include <linux/major.h>
11 #include <linux/string.h>
12 #include <linux/sched.h>
13 #include <linux/ext_fs.h>
14 #include <linux/stat.h>
15 #include <linux/fcntl.h>
16 #include <linux/errno.h>
17
18 struct device_struct {
19 const char * name;
20 struct file_operations * fops;
21 };
22
23 static struct device_struct chrdevs[MAX_CHRDEV] = {
24 { NULL, NULL },
25 };
26
27 static struct device_struct blkdevs[MAX_BLKDEV] = {
28 { NULL, NULL },
29 };
30
31 struct file_operations * get_blkfops(unsigned int major)
32 {
33 if (major >= MAX_BLKDEV)
34 return NULL;
35 return blkdevs[major].fops;
36 }
37
38 struct file_operations * get_chrfops(unsigned int major)
39 {
40 if (major >= MAX_CHRDEV)
41 return NULL;
42 return chrdevs[major].fops;
43 }
44
45 int register_chrdev(unsigned int major, const char * name, struct file_operations *fops)
46 {
47 if (major >= MAX_CHRDEV)
48 return -EINVAL;
49 if (chrdevs[major].fops)
50 return -EBUSY;
51 chrdevs[major].name = name;
52 chrdevs[major].fops = fops;
53 return 0;
54 }
55
56 int register_blkdev(unsigned int major, const char * name, struct file_operations *fops)
57 {
58 if (major >= MAX_BLKDEV)
59 return -EINVAL;
60 if (blkdevs[major].fops)
61 return -EBUSY;
62 blkdevs[major].name = name;
63 blkdevs[major].fops = fops;
64 return 0;
65 }
66
67 int unregister_chrdev(unsigned int major, const char * name)
68 {
69 if (major >= MAX_CHRDEV)
70 return -EINVAL;
71 if (!chrdevs[major].fops)
72 return -EINVAL;
73 if (strcmp(chrdevs[major].name, name))
74 return -EINVAL;
75 chrdevs[major].name = NULL;
76 chrdevs[major].fops = NULL;
77 return 0;
78 }
79
80 int unregister_blkdev(unsigned int major, const char * name)
81 {
82 if (major >= MAX_BLKDEV)
83 return -EINVAL;
84 if (!blkdevs[major].fops)
85 return -EINVAL;
86 if (strcmp(blkdevs[major].name, name))
87 return -EINVAL;
88 blkdevs[major].name = NULL;
89 blkdevs[major].fops = NULL;
90 return 0;
91 }
92
93
94
95
96 int blkdev_open(struct inode * inode, struct file * filp)
97 {
98 int i;
99
100 i = MAJOR(inode->i_rdev);
101 if (i >= MAX_BLKDEV || !blkdevs[i].fops)
102 return -ENODEV;
103 filp->f_op = blkdevs[i].fops;
104 if (filp->f_op->open)
105 return filp->f_op->open(inode,filp);
106 return 0;
107 }
108
109
110
111
112
113
114 struct file_operations def_blk_fops = {
115 NULL,
116 NULL,
117 NULL,
118 NULL,
119 NULL,
120 NULL,
121 NULL,
122 blkdev_open,
123 NULL,
124 };
125
126 struct inode_operations blkdev_inode_operations = {
127 &def_blk_fops,
128 NULL,
129 NULL,
130 NULL,
131 NULL,
132 NULL,
133 NULL,
134 NULL,
135 NULL,
136 NULL,
137 NULL,
138 NULL,
139 NULL,
140 NULL,
141 NULL
142 };
143
144
145
146
147 int chrdev_open(struct inode * inode, struct file * filp)
148 {
149 int i;
150
151 i = MAJOR(inode->i_rdev);
152 if (i >= MAX_CHRDEV || !chrdevs[i].fops)
153 return -ENODEV;
154 filp->f_op = chrdevs[i].fops;
155 if (filp->f_op->open)
156 return filp->f_op->open(inode,filp);
157 return 0;
158 }
159
160
161
162
163
164
165 struct file_operations def_chr_fops = {
166 NULL,
167 NULL,
168 NULL,
169 NULL,
170 NULL,
171 NULL,
172 NULL,
173 chrdev_open,
174 NULL,
175 };
176
177 struct inode_operations chrdev_inode_operations = {
178 &def_chr_fops,
179 NULL,
180 NULL,
181 NULL,
182 NULL,
183 NULL,
184 NULL,
185 NULL,
186 NULL,
187 NULL,
188 NULL,
189 NULL,
190 NULL,
191 NULL,
192 NULL
193 };