From Geert: Quickfix to get DDB compile.
[linux-2.6/linux-mips.git] / fs / devices.c
blobd119b1c75f620e714980ea7d63ed8866b7a2bfbe
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 #ifdef CONFIG_KMOD
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)
30 #endif
32 struct device_struct {
33 const char * name;
34 struct file_operations * fops;
37 static struct device_struct chrdevs[MAX_CHRDEV] = {
38 { NULL, NULL },
41 extern int get_blkdev_list(char *);
43 int get_device_list(char * page)
45 int i;
46 int len;
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);
55 return len;
59 Return the function table of a device.
60 Load the driver if needed.
62 static struct file_operations * get_fops(
63 unsigned int major,
64 unsigned int minor,
65 unsigned int maxdev,
66 const char *mangle, /* String to use to build the module name */
67 struct device_struct tb[])
69 struct file_operations *ret = NULL;
71 if (major < maxdev){
72 #ifdef CONFIG_KMOD
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)) {
90 char name[20];
91 sprintf(name, mangle, major);
92 request_module(name);
94 #endif
95 ret = tb[major].fops;
97 return ret;
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)
107 if (major == 0) {
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;
112 return major;
115 return -EBUSY;
117 if (major >= MAX_CHRDEV)
118 return -EINVAL;
119 if (chrdevs[major].fops && chrdevs[major].fops != fops)
120 return -EBUSY;
121 chrdevs[major].name = name;
122 chrdevs[major].fops = fops;
123 return 0;
126 int unregister_chrdev(unsigned int major, const char * name)
128 if (major >= MAX_CHRDEV)
129 return -EINVAL;
130 if (!chrdevs[major].fops)
131 return -EINVAL;
132 if (strcmp(chrdevs[major].name, name))
133 return -EINVAL;
134 chrdevs[major].name = NULL;
135 chrdevs[major].fops = NULL;
136 return 0;
140 * Called every time a character special file is opened
142 int chrdev_open(struct inode * inode, struct file * filp)
144 int ret = -ENODEV;
146 filp->f_op = fops_get(get_chrfops(MAJOR(inode->i_rdev),
147 MINOR(inode->i_rdev)));
148 if (filp->f_op) {
149 ret = 0;
150 if (filp->f_op->open != NULL)
151 ret = filp->f_op->open(inode,filp);
153 return ret;
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 = {
162 open: chrdev_open,
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));
173 return buffer;
176 const char * cdevname(kdev_t dev)
178 static char buffer[32];
179 const char * name = chrdevs[MAJOR(dev)].name;
181 if (!name)
182 name = "unknown-char";
183 sprintf(buffer, "%s(%d,%d)", name, MAJOR(dev), MINOR(dev));
184 return buffer;
187 void init_special_inode(struct inode *inode, umode_t mode, int rdev)
189 inode->i_mode = mode;
190 if (S_ISCHR(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))
201 else
202 printk(KERN_DEBUG "init_special_inode: bogus imode (%o)\n", mode);