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>
21 #include <linux/smp_lock.h>
23 #include <linux/kmod.h>
25 #include <linux/tty.h>
27 /* serial module kmod load support */
28 struct tty_driver
*get_tty_driver(kdev_t device
);
29 #define isa_tty_dev(ma) (ma == TTY_MAJOR || ma == TTYAUX_MAJOR)
30 #define need_serial(ma,mi) (get_tty_driver(MKDEV(ma,mi)) == NULL)
33 struct device_struct
{
35 struct file_operations
* fops
;
38 static rwlock_t chrdevs_lock
= RW_LOCK_UNLOCKED
;
39 static struct device_struct chrdevs
[MAX_CHRDEV
] = {
43 extern int get_blkdev_list(char *);
45 int get_device_list(char * page
)
50 len
= sprintf(page
, "Character devices:\n");
51 read_lock(&chrdevs_lock
);
52 for (i
= 0; i
< MAX_CHRDEV
; i
++) {
53 if (chrdevs
[i
].fops
) {
54 len
+= sprintf(page
+len
, "%3d %s\n", i
, chrdevs
[i
].name
);
57 read_unlock(&chrdevs_lock
);
58 len
+= get_blkdev_list(page
+len
);
63 Return the function table of a device.
64 Load the driver if needed.
65 Increment the reference count of module in question.
67 static struct file_operations
* get_chrfops(unsigned int major
, unsigned int minor
)
69 struct file_operations
*ret
= NULL
;
71 if (!major
|| major
>= MAX_CHRDEV
)
74 read_lock(&chrdevs_lock
);
75 ret
= fops_get(chrdevs
[major
].fops
);
76 read_unlock(&chrdevs_lock
);
78 if (ret
&& isa_tty_dev(major
)) {
80 if (need_serial(major
,minor
)) {
81 /* Force request_module anyway, but what for? */
89 sprintf(name
, "char-major-%d", major
);
92 read_lock(&chrdevs_lock
);
93 ret
= fops_get(chrdevs
[major
].fops
);
94 read_unlock(&chrdevs_lock
);
100 int register_chrdev(unsigned int major
, const char * name
, struct file_operations
*fops
)
103 write_lock(&chrdevs_lock
);
104 for (major
= MAX_CHRDEV
-1; major
> 0; major
--) {
105 if (chrdevs
[major
].fops
== NULL
) {
106 chrdevs
[major
].name
= name
;
107 chrdevs
[major
].fops
= fops
;
108 write_unlock(&chrdevs_lock
);
112 write_unlock(&chrdevs_lock
);
115 if (major
>= MAX_CHRDEV
)
117 write_lock(&chrdevs_lock
);
118 if (chrdevs
[major
].fops
&& chrdevs
[major
].fops
!= fops
) {
119 write_unlock(&chrdevs_lock
);
122 chrdevs
[major
].name
= name
;
123 chrdevs
[major
].fops
= fops
;
124 write_unlock(&chrdevs_lock
);
128 int unregister_chrdev(unsigned int major
, const char * name
)
130 if (major
>= MAX_CHRDEV
)
132 write_lock(&chrdevs_lock
);
133 if (!chrdevs
[major
].fops
|| strcmp(chrdevs
[major
].name
, name
)) {
134 write_unlock(&chrdevs_lock
);
137 chrdevs
[major
].name
= NULL
;
138 chrdevs
[major
].fops
= NULL
;
139 write_unlock(&chrdevs_lock
);
144 * Called every time a character special file is opened
146 int chrdev_open(struct inode
* inode
, struct file
* filp
)
150 filp
->f_op
= get_chrfops(MAJOR(inode
->i_rdev
), MINOR(inode
->i_rdev
));
153 if (filp
->f_op
->open
!= NULL
) {
155 ret
= filp
->f_op
->open(inode
,filp
);
163 * Dummy default file-operations: the only thing this does
164 * is contain the open that then fills in the correct operations
165 * depending on the special file...
167 static struct file_operations def_chr_fops
= {
172 * Print device name (in decimal, hexadecimal or symbolic)
173 * Note: returns pointer to static data!
175 const char * kdevname(kdev_t dev
)
177 static char buffer
[32];
178 sprintf(buffer
, "%02x:%02x", MAJOR(dev
), MINOR(dev
));
182 const char * cdevname(kdev_t dev
)
184 static char buffer
[32];
185 const char * name
= chrdevs
[MAJOR(dev
)].name
;
188 name
= "unknown-char";
189 sprintf(buffer
, "%s(%d,%d)", name
, MAJOR(dev
), MINOR(dev
));
193 static int sock_no_open(struct inode
*irrelevant
, struct file
*dontcare
)
198 static struct file_operations bad_sock_fops
= {
202 void init_special_inode(struct inode
*inode
, umode_t mode
, int rdev
)
204 inode
->i_mode
= mode
;
206 inode
->i_fop
= &def_chr_fops
;
207 inode
->i_rdev
= to_kdev_t(rdev
);
208 } else if (S_ISBLK(mode
)) {
209 inode
->i_fop
= &def_blk_fops
;
210 inode
->i_rdev
= to_kdev_t(rdev
);
211 inode
->i_bdev
= bdget(rdev
);
212 } else if (S_ISFIFO(mode
))
213 inode
->i_fop
= &def_fifo_fops
;
214 else if (S_ISSOCK(mode
))
215 inode
->i_fop
= &bad_sock_fops
;
217 printk(KERN_DEBUG
"init_special_inode: bogus imode (%o)\n", mode
);