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