Merge with Linux 2.4.0-test5-pre6.
[linux-2.6/linux-mips.git] / fs / devices.c
blobdd7db77306dec194a3324d8ef4a124cdd97a1cab
1 /*
2 * linux/fs/devices.c
4 * (C) 1993 Matthias Urlichs -- collected common code and tables.
5 *
6 * Copyright (C) 1991, 1992 Linus Torvalds
8 * Added kerneld support: Jacques Gelinas and Bjorn Ekwall
9 * (changed to kmod)
12 #include <linux/config.h>
13 #include <linux/fs.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>
22 #ifdef CONFIG_KMOD
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)
31 #endif
33 struct device_struct {
34 const char * name;
35 struct file_operations * fops;
38 static rwlock_t chrdevs_lock = RW_LOCK_UNLOCKED;
39 static struct device_struct chrdevs[MAX_CHRDEV] = {
40 { NULL, NULL },
43 extern int get_blkdev_list(char *);
45 int get_device_list(char * page)
47 int i;
48 int len;
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);
59 return 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)
72 return NULL;
74 read_lock(&chrdevs_lock);
75 ret = fops_get(chrdevs[major].fops);
76 read_unlock(&chrdevs_lock);
77 #ifdef CONFIG_KMOD
78 if (ret && isa_tty_dev(major)) {
79 lock_kernel();
80 if (need_serial(major,minor)) {
81 /* Force request_module anyway, but what for? */
82 fops_put(ret);
83 ret = NULL;
85 unlock_kernel();
87 if (!ret) {
88 char name[20];
89 sprintf(name, "char-major-%d", major);
90 request_module(name);
92 read_lock(&chrdevs_lock);
93 ret = fops_get(chrdevs[major].fops);
94 read_unlock(&chrdevs_lock);
96 #endif
97 return ret;
100 int register_chrdev(unsigned int major, const char * name, struct file_operations *fops)
102 if (major == 0) {
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);
109 return major;
112 write_unlock(&chrdevs_lock);
113 return -EBUSY;
115 if (major >= MAX_CHRDEV)
116 return -EINVAL;
117 write_lock(&chrdevs_lock);
118 if (chrdevs[major].fops && chrdevs[major].fops != fops) {
119 write_unlock(&chrdevs_lock);
120 return -EBUSY;
122 chrdevs[major].name = name;
123 chrdevs[major].fops = fops;
124 write_unlock(&chrdevs_lock);
125 return 0;
128 int unregister_chrdev(unsigned int major, const char * name)
130 if (major >= MAX_CHRDEV)
131 return -EINVAL;
132 write_lock(&chrdevs_lock);
133 if (!chrdevs[major].fops || strcmp(chrdevs[major].name, name)) {
134 write_unlock(&chrdevs_lock);
135 return -EINVAL;
137 chrdevs[major].name = NULL;
138 chrdevs[major].fops = NULL;
139 write_unlock(&chrdevs_lock);
140 return 0;
144 * Called every time a character special file is opened
146 int chrdev_open(struct inode * inode, struct file * filp)
148 int ret = -ENODEV;
150 filp->f_op = get_chrfops(MAJOR(inode->i_rdev), MINOR(inode->i_rdev));
151 if (filp->f_op) {
152 ret = 0;
153 if (filp->f_op->open != NULL) {
154 lock_kernel();
155 ret = filp->f_op->open(inode,filp);
156 unlock_kernel();
159 return ret;
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 = {
168 open: chrdev_open,
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));
179 return buffer;
182 const char * cdevname(kdev_t dev)
184 static char buffer[32];
185 const char * name = chrdevs[MAJOR(dev)].name;
187 if (!name)
188 name = "unknown-char";
189 sprintf(buffer, "%s(%d,%d)", name, MAJOR(dev), MINOR(dev));
190 return buffer;
193 static int sock_no_open(struct inode *irrelevant, struct file *dontcare)
195 return -ENXIO;
198 static struct file_operations bad_sock_fops = {
199 open: sock_no_open
202 void init_special_inode(struct inode *inode, umode_t mode, int rdev)
204 inode->i_mode = mode;
205 if (S_ISCHR(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;
216 else
217 printk(KERN_DEBUG "init_special_inode: bogus imode (%o)\n", mode);