4 * (C) 1993 Matthias Urlichs -- collected common code and tables.
6 * Copyright (C) 1991, 1992 Linus Torvalds
8 * Added kerneld support: Jacques Gelinas and Bjorn Ekwall
12 #include <linux/config.h>
14 #include <linux/major.h>
15 #include <linux/string.h>
16 #include <linux/sched.h>
17 #include <linux/stat.h>
18 #include <linux/fcntl.h>
19 #include <linux/errno.h>
20 #include <linux/module.h>
22 #include <linux/kmod.h>
24 #include <linux/tty.h>
26 /* serial module kmod load support */
27 struct tty_driver
*get_tty_driver(kdev_t device
);
28 #define isa_tty_dev(ma) (ma == TTY_MAJOR || ma == TTYAUX_MAJOR)
29 #define need_serial(ma,mi) (get_tty_driver(MKDEV(ma,mi)) == NULL)
32 struct device_struct
{
34 struct file_operations
* fops
;
37 static struct device_struct chrdevs
[MAX_CHRDEV
] = {
41 extern int get_blkdev_list(char *);
43 int get_device_list(char * page
)
48 len
= sprintf(page
, "Character devices:\n");
49 for (i
= 0; i
< MAX_CHRDEV
; i
++) {
50 if (chrdevs
[i
].fops
) {
51 len
+= sprintf(page
+len
, "%3d %s\n", i
, chrdevs
[i
].name
);
54 len
+= get_blkdev_list(page
+len
);
59 Return the function table of a device.
60 Load the driver if needed.
62 static struct file_operations
* get_fops(
66 const char *mangle
, /* String to use to build the module name */
67 struct device_struct tb
[])
69 struct file_operations
*ret
= NULL
;
74 * I do get request for device 0. I have no idea why. It happen
75 * at shutdown time for one. Without the following test, the
76 * kernel will happily trigger a request_module() which will
77 * trigger kmod and modprobe for nothing (since there
78 * is no device with major number == 0. And furthermore
79 * it locks the reboot process :-(
81 * Jacques Gelinas (jacques@solucorp.qc.ca)
83 * A. Haritsis <ah@doc.ic.ac.uk>: fix for serial module
84 * though we need the minor here to check if serial dev,
85 * we pass only the normal major char dev to kmod
86 * as there is no other loadable dev on these majors
88 if ((isa_tty_dev(major
) && need_serial(major
,minor
)) ||
89 (major
!= 0 && !tb
[major
].fops
)) {
91 sprintf(name
, mangle
, major
);
100 struct file_operations
* get_chrfops(unsigned int major
, unsigned int minor
)
102 return get_fops (major
,minor
,MAX_CHRDEV
,"char-major-%d",chrdevs
);
105 int register_chrdev(unsigned int major
, const char * name
, struct file_operations
*fops
)
108 for (major
= MAX_CHRDEV
-1; major
> 0; major
--) {
109 if (chrdevs
[major
].fops
== NULL
) {
110 chrdevs
[major
].name
= name
;
111 chrdevs
[major
].fops
= fops
;
117 if (major
>= MAX_CHRDEV
)
119 if (chrdevs
[major
].fops
&& chrdevs
[major
].fops
!= fops
)
121 chrdevs
[major
].name
= name
;
122 chrdevs
[major
].fops
= fops
;
126 int unregister_chrdev(unsigned int major
, const char * name
)
128 if (major
>= MAX_CHRDEV
)
130 if (!chrdevs
[major
].fops
)
132 if (strcmp(chrdevs
[major
].name
, name
))
134 chrdevs
[major
].name
= NULL
;
135 chrdevs
[major
].fops
= NULL
;
140 * Called every time a character special file is opened
142 int chrdev_open(struct inode
* inode
, struct file
* filp
)
146 filp
->f_op
= fops_get(get_chrfops(MAJOR(inode
->i_rdev
),
147 MINOR(inode
->i_rdev
)));
150 if (filp
->f_op
->open
!= NULL
)
151 ret
= filp
->f_op
->open(inode
,filp
);
157 * Dummy default file-operations: the only thing this does
158 * is contain the open that then fills in the correct operations
159 * depending on the special file...
161 static struct file_operations def_chr_fops
= {
166 * Print device name (in decimal, hexadecimal or symbolic)
167 * Note: returns pointer to static data!
169 const char * kdevname(kdev_t dev
)
171 static char buffer
[32];
172 sprintf(buffer
, "%02x:%02x", MAJOR(dev
), MINOR(dev
));
176 const char * cdevname(kdev_t dev
)
178 static char buffer
[32];
179 const char * name
= chrdevs
[MAJOR(dev
)].name
;
182 name
= "unknown-char";
183 sprintf(buffer
, "%s(%d,%d)", name
, MAJOR(dev
), MINOR(dev
));
187 void init_special_inode(struct inode
*inode
, umode_t mode
, int rdev
)
189 inode
->i_mode
= mode
;
191 inode
->i_fop
= &def_chr_fops
;
192 inode
->i_rdev
= to_kdev_t(rdev
);
193 } else if (S_ISBLK(mode
)) {
194 inode
->i_fop
= &def_blk_fops
;
195 inode
->i_rdev
= to_kdev_t(rdev
);
196 inode
->i_bdev
= bdget(rdev
);
197 } else if (S_ISFIFO(mode
))
198 inode
->i_fop
= &def_fifo_fops
;
199 else if (S_ISSOCK(mode
))
202 printk(KERN_DEBUG
"init_special_inode: bogus imode (%o)\n", mode
);