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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
|
/*
* linux/fs/devices.c
*
* (C) 1993 Matthias Urlichs -- collected common code and tables.
*
* Copyright (C) 1991, 1992 Linus Torvalds
*
* Added kerneld support: Jacques Gelinas and Bjorn Ekwall
* (changed to kmod)
*/
#include <linux/config.h>
#include <linux/fs.h>
#include <linux/major.h>
#include <linux/string.h>
#include <linux/sched.h>
#include <linux/stat.h>
#include <linux/fcntl.h>
#include <linux/errno.h>
#ifdef CONFIG_KMOD
#include <linux/kmod.h>
#include <linux/tty.h>
/* serial module kmod load support */
struct tty_driver *get_tty_driver(kdev_t device);
#define isa_tty_dev(ma) (ma == TTY_MAJOR || ma == TTYAUX_MAJOR)
#define need_serial(ma,mi) (get_tty_driver(MKDEV(ma,mi)) == NULL)
#endif
struct device_struct {
const char * name;
struct file_operations * fops;
};
static struct device_struct chrdevs[MAX_CHRDEV] = {
{ NULL, NULL },
};
static struct device_struct blkdevs[MAX_BLKDEV] = {
{ NULL, NULL },
};
int get_device_list(char * page)
{
int i;
int len;
len = sprintf(page, "Character devices:\n");
for (i = 0; i < MAX_CHRDEV ; i++) {
if (chrdevs[i].fops) {
len += sprintf(page+len, "%3d %s\n", i, chrdevs[i].name);
}
}
len += sprintf(page+len, "\nBlock devices:\n");
for (i = 0; i < MAX_BLKDEV ; i++) {
if (blkdevs[i].fops) {
len += sprintf(page+len, "%3d %s\n", i, blkdevs[i].name);
}
}
return len;
}
/*
Return the function table of a device.
Load the driver if needed.
*/
static struct file_operations * get_fops(
unsigned int major,
unsigned int minor,
unsigned int maxdev,
const char *mangle, /* String to use to build the module name */
struct device_struct tb[])
{
struct file_operations *ret = NULL;
if (major < maxdev){
#ifdef CONFIG_KMOD
/*
* I do get request for device 0. I have no idea why. It happen
* at shutdown time for one. Without the following test, the
* kernel will happily trigger a request_module() which will
* trigger kmod and modprobe for nothing (since there
* is no device with major number == 0. And furthermore
* it locks the reboot process :-(
*
* Jacques Gelinas (jacques@solucorp.qc.ca)
*
* A. Haritsis <ah@doc.ic.ac.uk>: fix for serial module
* though we need the minor here to check if serial dev,
* we pass only the normal major char dev to kmod
* as there is no other loadable dev on these majors
*/
if ((isa_tty_dev(major) && need_serial(major,minor)) ||
(major != 0 && !tb[major].fops)) {
char name[20];
sprintf(name, mangle, major);
request_module(name);
}
#endif
ret = tb[major].fops;
}
return ret;
}
/*
Return the function table of a device.
Load the driver if needed.
*/
struct file_operations * get_blkfops(unsigned int major)
{
return get_fops (major,0,MAX_BLKDEV,"block-major-%d",blkdevs);
}
struct file_operations * get_chrfops(unsigned int major, unsigned int minor)
{
return get_fops (major,minor,MAX_CHRDEV,"char-major-%d",chrdevs);
}
int register_chrdev(unsigned int major, const char * name, struct file_operations *fops)
{
if (major == 0) {
for (major = MAX_CHRDEV-1; major > 0; major--) {
if (chrdevs[major].fops == NULL) {
chrdevs[major].name = name;
chrdevs[major].fops = fops;
return major;
}
}
return -EBUSY;
}
if (major >= MAX_CHRDEV)
return -EINVAL;
if (chrdevs[major].fops && chrdevs[major].fops != fops)
return -EBUSY;
chrdevs[major].name = name;
chrdevs[major].fops = fops;
return 0;
}
int register_blkdev(unsigned int major, const char * name, struct file_operations *fops)
{
if (major == 0) {
for (major = MAX_BLKDEV-1; major > 0; major--) {
if (blkdevs[major].fops == NULL) {
blkdevs[major].name = name;
blkdevs[major].fops = fops;
return major;
}
}
return -EBUSY;
}
if (major >= MAX_BLKDEV)
return -EINVAL;
if (blkdevs[major].fops && blkdevs[major].fops != fops)
return -EBUSY;
blkdevs[major].name = name;
blkdevs[major].fops = fops;
return 0;
}
int unregister_chrdev(unsigned int major, const char * name)
{
if (major >= MAX_CHRDEV)
return -EINVAL;
if (!chrdevs[major].fops)
return -EINVAL;
if (strcmp(chrdevs[major].name, name))
return -EINVAL;
chrdevs[major].name = NULL;
chrdevs[major].fops = NULL;
return 0;
}
int unregister_blkdev(unsigned int major, const char * name)
{
if (major >= MAX_BLKDEV)
return -EINVAL;
if (!blkdevs[major].fops)
return -EINVAL;
if (strcmp(blkdevs[major].name, name))
return -EINVAL;
blkdevs[major].name = NULL;
blkdevs[major].fops = NULL;
return 0;
}
/*
* This routine checks whether a removable media has been changed,
* and invalidates all buffer-cache-entries in that case. This
* is a relatively slow routine, so we have to try to minimize using
* it. Thus it is called only upon a 'mount' or 'open'. This
* is the best way of combining speed and utility, I think.
* People changing diskettes in the middle of an operation deserve
* to lose :-)
*/
int check_disk_change(kdev_t dev)
{
int i;
struct file_operations * fops;
struct super_block * sb;
i = MAJOR(dev);
if (i >= MAX_BLKDEV || (fops = blkdevs[i].fops) == NULL)
return 0;
if (fops->check_media_change == NULL)
return 0;
if (!fops->check_media_change(dev))
return 0;
printk(KERN_DEBUG "VFS: Disk change detected on device %s\n",
bdevname(dev));
sb = get_super(dev);
if (sb && invalidate_inodes(sb))
printk("VFS: busy inodes on changed media.\n");
invalidate_buffers(dev);
if (fops->revalidate)
fops->revalidate(dev);
return 1;
}
/*
* Called every time a block special file is opened
*/
int blkdev_open(struct inode * inode, struct file * filp)
{
int ret = -ENODEV;
filp->f_op = get_blkfops(MAJOR(inode->i_rdev));
if (filp->f_op != NULL){
ret = 0;
if (filp->f_op->open != NULL)
ret = filp->f_op->open(inode,filp);
}
return ret;
}
int blkdev_release(struct inode * inode)
{
struct file_operations *fops = get_blkfops(MAJOR(inode->i_rdev));
if (fops && fops->release)
return fops->release(inode,NULL);
return 0;
}
/*
* Dummy default file-operations: the only thing this does
* is contain the open that then fills in the correct operations
* depending on the special file...
*/
struct file_operations def_blk_fops = {
NULL, /* lseek */
NULL, /* read */
NULL, /* write */
NULL, /* readdir */
NULL, /* poll */
NULL, /* ioctl */
NULL, /* mmap */
blkdev_open, /* open */
NULL, /* flush */
NULL, /* release */
};
struct inode_operations blkdev_inode_operations = {
&def_blk_fops, /* default file operations */
NULL, /* create */
NULL, /* lookup */
NULL, /* link */
NULL, /* unlink */
NULL, /* symlink */
NULL, /* mkdir */
NULL, /* rmdir */
NULL, /* mknod */
NULL, /* rename */
NULL, /* readlink */
NULL, /* follow_link */
NULL, /* get_block */
NULL, /* readpage */
NULL, /* writepage */
NULL, /* flushpage */
NULL, /* truncate */
NULL, /* permission */
NULL, /* smap */
NULL /* revalidate */
};
/*
* Called every time a character special file is opened
*/
int chrdev_open(struct inode * inode, struct file * filp)
{
int ret = -ENODEV;
filp->f_op = get_chrfops(MAJOR(inode->i_rdev), MINOR(inode->i_rdev));
if (filp->f_op != NULL){
ret = 0;
if (filp->f_op->open != NULL)
ret = filp->f_op->open(inode,filp);
}
return ret;
}
/*
* Dummy default file-operations: the only thing this does
* is contain the open that then fills in the correct operations
* depending on the special file...
*/
struct file_operations def_chr_fops = {
NULL, /* lseek */
NULL, /* read */
NULL, /* write */
NULL, /* readdir */
NULL, /* poll */
NULL, /* ioctl */
NULL, /* mmap */
chrdev_open, /* open */
NULL, /* flush */
NULL, /* release */
};
struct inode_operations chrdev_inode_operations = {
&def_chr_fops, /* default file operations */
NULL, /* create */
NULL, /* lookup */
NULL, /* link */
NULL, /* unlink */
NULL, /* symlink */
NULL, /* mkdir */
NULL, /* rmdir */
NULL, /* mknod */
NULL, /* rename */
NULL, /* readlink */
NULL, /* follow_link */
NULL, /* get_block */
NULL, /* readpage */
NULL, /* writepage */
NULL, /* flushpage */
NULL, /* truncate */
NULL, /* permission */
NULL, /* smap */
NULL /* revalidate */
};
/*
* Print device name (in decimal, hexadecimal or symbolic)
* Note: returns pointer to static data!
*/
char * kdevname(kdev_t dev)
{
static char buffer[32];
sprintf(buffer, "%02x:%02x", MAJOR(dev), MINOR(dev));
return buffer;
}
char * bdevname(kdev_t dev)
{
static char buffer[32];
const char * name = blkdevs[MAJOR(dev)].name;
if (!name)
name = "unknown-block";
sprintf(buffer, "%s(%d,%d)", name, MAJOR(dev), MINOR(dev));
return buffer;
}
char * cdevname(kdev_t dev)
{
static char buffer[32];
const char * name = chrdevs[MAJOR(dev)].name;
if (!name)
name = "unknown-char";
sprintf(buffer, "%s(%d,%d)", name, MAJOR(dev), MINOR(dev));
return buffer;
}
void init_special_inode(struct inode *inode, umode_t mode, int rdev)
{
inode->i_mode = mode;
inode->i_op = NULL;
if (S_ISCHR(mode)) {
inode->i_op = &chrdev_inode_operations;
inode->i_rdev = to_kdev_t(rdev);
} else if (S_ISBLK(mode)) {
inode->i_op = &blkdev_inode_operations;
inode->i_rdev = to_kdev_t(rdev);
} else if (S_ISFIFO(mode))
init_fifo(inode);
else if (S_ISSOCK(mode))
;
else
printk(KERN_DEBUG "init_special_inode: bogus imode (%o)\n", mode);
}
|