import of ftp.dlink.com/GPL/DSMG-600_reB/ppclinux.tar.gz
[linux-2.4.21-pre4.git] / drivers / char / misc.c
1 /*
2  * linux/drivers/char/misc.c
3  *
4  * Generic misc open routine by Johan Myreen
5  *
6  * Based on code from Linus
7  *
8  * Teemu Rantanen's Microsoft Busmouse support and Derrick Cole's
9  *   changes incorporated into 0.97pl4
10  *   by Peter Cervasio (pete%q106fm.uucp@wupost.wustl.edu) (08SEP92)
11  *   See busmouse.c for particulars.
12  *
13  * Made things a lot mode modular - easy to compile in just one or two
14  * of the misc drivers, as they are now completely independent. Linus.
15  *
16  * Support for loadable modules. 8-Sep-95 Philip Blundell <pjb27@cam.ac.uk>
17  *
18  * Fixed a failing symbol register to free the device registration
19  *              Alan Cox <alan@lxorguk.ukuu.org.uk> 21-Jan-96
20  *
21  * Dynamic minors and /proc/mice by Alessandro Rubini. 26-Mar-96
22  *
23  * Renamed to misc and miscdevice to be more accurate. Alan Cox 26-Mar-96
24  *
25  * Handling of mouse minor numbers for kerneld:
26  *  Idea by Jacques Gelinas <jack@solucorp.qc.ca>,
27  *  adapted by Bjorn Ekwall <bj0rn@blox.se>
28  *  corrected by Alan Cox <alan@lxorguk.ukuu.org.uk>
29  *
30  * Changes for kmod (from kerneld):
31  *      Cyrus Durgin <cider@speakeasy.org>
32  *
33  * Added devfs support. Richard Gooch <rgooch@atnf.csiro.au>  10-Jan-1998
34  */
35
36 #include <linux/module.h>
37 #include <linux/config.h>
38
39 #include <linux/fs.h>
40 #include <linux/errno.h>
41 #include <linux/miscdevice.h>
42 #include <linux/kernel.h>
43 #include <linux/major.h>
44 #include <linux/slab.h>
45 #include <linux/proc_fs.h>
46 #include <linux/devfs_fs_kernel.h>
47 #include <linux/stat.h>
48 #include <linux/init.h>
49
50 #include <linux/tty.h>
51 #include <linux/selection.h>
52 #include <linux/kmod.h>
53
54 #include "busmouse.h"
55
56 /*
57  * Head entry for the doubly linked miscdevice list
58  */
59 static struct miscdevice misc_list = { 0, "head", NULL, &misc_list, &misc_list };
60 static DECLARE_MUTEX(misc_sem);
61
62 /*
63  * Assigned numbers, used for dynamic minors
64  */
65 #define DYNAMIC_MINORS 64 /* like dynamic majors */
66 static unsigned char misc_minors[DYNAMIC_MINORS / 8];
67
68 extern int psaux_init(void);
69 #ifdef CONFIG_SGI_NEWPORT_GFX
70 extern void gfx_register(void);
71 #endif
72 extern void streamable_init(void);
73 extern int rtc_DP8570A_init(void);
74 extern int rtc_MK48T08_init(void);
75 extern int ds1286_init(void);
76 extern int pmu_device_init(void);
77 extern int tosh_init(void);
78 extern int i8k_init(void);
79 extern int lcd_init(void);
80
81 static int misc_read_proc(char *buf, char **start, off_t offset,
82                           int len, int *eof, void *private)
83 {
84         struct miscdevice *p;
85         int written;
86
87         written=0;
88         for (p = misc_list.next; p != &misc_list && written < len; p = p->next) {
89                 written += sprintf(buf+written, "%3i %s\n",p->minor, p->name ?: "");
90                 if (written < offset) {
91                         offset -= written;
92                         written = 0;
93                 }
94         }
95         *start = buf + offset;
96         written -= offset;
97         if(written > len) {
98                 *eof = 0;
99                 return len;
100         }
101         *eof = 1;
102         return (written<0) ? 0 : written;
103 }
104
105
106 static int misc_open(struct inode * inode, struct file * file)
107 {
108         int minor = MINOR(inode->i_rdev);
109         struct miscdevice *c;
110         int err = -ENODEV;
111         struct file_operations *old_fops, *new_fops = NULL;
112         
113         down(&misc_sem);
114         
115         c = misc_list.next;
116
117         while ((c != &misc_list) && (c->minor != minor))
118                 c = c->next;
119         if (c != &misc_list)
120                 new_fops = fops_get(c->fops);
121         if (!new_fops) {
122                 char modname[20];
123                 up(&misc_sem);
124                 sprintf(modname, "char-major-%d-%d", MISC_MAJOR, minor);
125                 request_module(modname);
126                 down(&misc_sem);
127                 c = misc_list.next;
128                 while ((c != &misc_list) && (c->minor != minor))
129                         c = c->next;
130                 if (c == &misc_list || (new_fops = fops_get(c->fops)) == NULL)
131                         goto fail;
132         }
133
134         err = 0;
135         old_fops = file->f_op;
136         file->f_op = new_fops;
137         if (file->f_op->open) {
138                 err=file->f_op->open(inode,file);
139                 if (err) {
140                         fops_put(file->f_op);
141                         file->f_op = fops_get(old_fops);
142                 }
143         }
144         fops_put(old_fops);
145 fail:
146         up(&misc_sem);
147         return err;
148 }
149
150 static struct file_operations misc_fops = {
151         owner:          THIS_MODULE,
152         open:           misc_open,
153 };
154
155
156 /**
157  *      misc_register   -       register a miscellaneous device
158  *      @misc: device structure
159  *      
160  *      Register a miscellaneous device with the kernel. If the minor
161  *      number is set to %MISC_DYNAMIC_MINOR a minor number is assigned
162  *      and placed in the minor field of the structure. For other cases
163  *      the minor number requested is used.
164  *
165  *      The structure passed is linked into the kernel and may not be
166  *      destroyed until it has been unregistered.
167  *
168  *      A zero is returned on success and a negative errno code for
169  *      failure.
170  */
171  
172 int misc_register(struct miscdevice * misc)
173 {
174         static devfs_handle_t devfs_handle, dir;
175         struct miscdevice *c;
176         
177         if (misc->next || misc->prev)
178                 return -EBUSY;
179         down(&misc_sem);
180         c = misc_list.next;
181
182         while ((c != &misc_list) && (c->minor != misc->minor))
183                 c = c->next;
184         if (c != &misc_list) {
185                 up(&misc_sem);
186                 return -EBUSY;
187         }
188
189         if (misc->minor == MISC_DYNAMIC_MINOR) {
190                 int i = DYNAMIC_MINORS;
191                 while (--i >= 0)
192                         if ( (misc_minors[i>>3] & (1 << (i&7))) == 0)
193                                 break;
194                 if (i<0)
195                 {
196                         up(&misc_sem);
197                         return -EBUSY;
198                 }
199                 misc->minor = i;
200         }
201         if (misc->minor < DYNAMIC_MINORS)
202                 misc_minors[misc->minor >> 3] |= 1 << (misc->minor & 7);
203         if (!devfs_handle)
204                 devfs_handle = devfs_mk_dir (NULL, "misc", NULL);
205         dir = strchr (misc->name, '/') ? NULL : devfs_handle;
206         misc->devfs_handle =
207                 devfs_register (dir, misc->name, DEVFS_FL_NONE,
208                                 MISC_MAJOR, misc->minor,
209                                 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP,
210                                 misc->fops, NULL);
211
212         /*
213          * Add it to the front, so that later devices can "override"
214          * earlier defaults
215          */
216         misc->prev = &misc_list;
217         misc->next = misc_list.next;
218         misc->prev->next = misc;
219         misc->next->prev = misc;
220         up(&misc_sem);
221         return 0;
222 }
223
224 /**
225  *      misc_deregister - unregister a miscellaneous device
226  *      @misc: device to unregister
227  *
228  *      Unregister a miscellaneous device that was previously
229  *      successfully registered with misc_register(). Success
230  *      is indicated by a zero return, a negative errno code
231  *      indicates an error.
232  */
233
234 int misc_deregister(struct miscdevice * misc)
235 {
236         int i = misc->minor;
237         if (!misc->next || !misc->prev)
238                 return -EINVAL;
239         down(&misc_sem);
240         misc->prev->next = misc->next;
241         misc->next->prev = misc->prev;
242         misc->next = NULL;
243         misc->prev = NULL;
244         devfs_unregister (misc->devfs_handle);
245         if (i < DYNAMIC_MINORS && i>0) {
246                 misc_minors[i>>3] &= ~(1 << (misc->minor & 7));
247         }
248         up(&misc_sem);
249         return 0;
250 }
251
252 EXPORT_SYMBOL(misc_register);
253 EXPORT_SYMBOL(misc_deregister);
254
255 int __init misc_init(void)
256 {
257         create_proc_read_entry("misc", 0, 0, misc_read_proc, NULL);
258 #ifdef CONFIG_MVME16x
259         rtc_MK48T08_init();
260 #endif
261 #ifdef CONFIG_BVME6000
262         rtc_DP8570A_init();
263 #endif
264 #ifdef CONFIG_SGI_DS1286
265         ds1286_init();
266 #endif
267 #ifdef CONFIG_PMAC_PBOOK
268         pmu_device_init();
269 #endif
270 #ifdef CONFIG_SGI_NEWPORT_GFX
271         gfx_register ();
272 #endif
273 #ifdef CONFIG_SGI_IP22
274         streamable_init ();
275 #endif
276 #ifdef CONFIG_SGI_NEWPORT_GFX
277         gfx_register ();
278 #endif
279 #ifdef CONFIG_TOSHIBA
280         tosh_init();
281 #endif
282 #ifdef CONFIG_COBALT_LCD
283         lcd_init();
284 #endif
285 #ifdef CONFIG_I8K
286         i8k_init();
287 #endif
288         if (devfs_register_chrdev(MISC_MAJOR,"misc",&misc_fops)) {
289                 printk("unable to get major %d for misc devices\n",
290                        MISC_MAJOR);
291                 return -EIO;
292         }
293         return 0;
294 }