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
];
41 extern int get_blkdev_list(char *);
43 int get_device_list(char * page
)
48 len
= sprintf(page
, "Character devices:\n");
49 read_lock(&chrdevs_lock
);
50 for (i
= 0; i
< MAX_CHRDEV
; i
++) {
51 if (chrdevs
[i
].fops
) {
52 len
+= sprintf(page
+len
, "%3d %s\n", i
, chrdevs
[i
].name
);
55 read_unlock(&chrdevs_lock
);
56 len
+= get_blkdev_list(page
+len
);
61 Return the function table of a device.
62 Load the driver if needed.
63 Increment the reference count of module in question.
65 static struct file_operations
* get_chrfops(unsigned int major
, unsigned int minor
)
67 struct file_operations
*ret
= NULL
;
69 if (!major
|| major
>= MAX_CHRDEV
)
72 read_lock(&chrdevs_lock
);
73 ret
= fops_get(chrdevs
[major
].fops
);
74 read_unlock(&chrdevs_lock
);
76 if (ret
&& isa_tty_dev(major
)) {
78 if (need_serial(major
,minor
)) {
79 /* Force request_module anyway, but what for? */
87 sprintf(name
, "char-major-%d", major
);
90 read_lock(&chrdevs_lock
);
91 ret
= fops_get(chrdevs
[major
].fops
);
92 read_unlock(&chrdevs_lock
);
98 int register_chrdev(unsigned int major
, const char * name
, struct file_operations
*fops
)
101 write_lock(&chrdevs_lock
);
102 for (major
= MAX_CHRDEV
-1; major
> 0; major
--) {
103 if (chrdevs
[major
].fops
== NULL
) {
104 chrdevs
[major
].name
= name
;
105 chrdevs
[major
].fops
= fops
;
106 write_unlock(&chrdevs_lock
);
110 write_unlock(&chrdevs_lock
);
113 if (major
>= MAX_CHRDEV
)
115 write_lock(&chrdevs_lock
);
116 if (chrdevs
[major
].fops
&& chrdevs
[major
].fops
!= fops
) {
117 write_unlock(&chrdevs_lock
);
120 chrdevs
[major
].name
= name
;
121 chrdevs
[major
].fops
= fops
;
122 write_unlock(&chrdevs_lock
);
126 int unregister_chrdev(unsigned int major
, const char * name
)
128 if (major
>= MAX_CHRDEV
)
130 write_lock(&chrdevs_lock
);
131 if (!chrdevs
[major
].fops
|| strcmp(chrdevs
[major
].name
, name
)) {
132 write_unlock(&chrdevs_lock
);
135 chrdevs
[major
].name
= NULL
;
136 chrdevs
[major
].fops
= NULL
;
137 write_unlock(&chrdevs_lock
);
142 * Called every time a character special file is opened
144 int chrdev_open(struct inode
* inode
, struct file
* filp
)
148 filp
->f_op
= get_chrfops(MAJOR(inode
->i_rdev
), MINOR(inode
->i_rdev
));
151 if (filp
->f_op
->open
!= NULL
) {
153 ret
= filp
->f_op
->open(inode
,filp
);
161 * Dummy default file-operations: the only thing this does
162 * is contain the open that then fills in the correct operations
163 * depending on the special file...
165 static struct file_operations def_chr_fops
= {
170 * Print device name (in decimal, hexadecimal or symbolic)
171 * Note: returns pointer to static data!
173 const char * kdevname(kdev_t dev
)
175 static char buffer
[32];
176 sprintf(buffer
, "%02x:%02x", MAJOR(dev
), MINOR(dev
));
180 const char * cdevname(kdev_t dev
)
182 static char buffer
[32];
183 const char * name
= chrdevs
[MAJOR(dev
)].name
;
186 name
= "unknown-char";
187 sprintf(buffer
, "%s(%d,%d)", name
, MAJOR(dev
), MINOR(dev
));
191 static int sock_no_open(struct inode
*irrelevant
, struct file
*dontcare
)
196 static struct file_operations bad_sock_fops
= {
200 void init_special_inode(struct inode
*inode
, umode_t mode
, int rdev
)
202 inode
->i_mode
= mode
;
204 inode
->i_fop
= &def_chr_fops
;
205 inode
->i_rdev
= to_kdev_t(rdev
);
206 } else if (S_ISBLK(mode
)) {
207 inode
->i_fop
= &def_blk_fops
;
208 inode
->i_rdev
= to_kdev_t(rdev
);
209 inode
->i_bdev
= bdget(rdev
);
210 } else if (S_ISFIFO(mode
))
211 inode
->i_fop
= &def_fifo_fops
;
212 else if (S_ISSOCK(mode
))
213 inode
->i_fop
= &bad_sock_fops
;
215 printk(KERN_DEBUG
"init_special_inode: bogus imode (%o)\n", mode
);