1da352fc4d6f19a7c721541e78a2e1f7331e6674
[linux-flexiantxendom0-3.2.10.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 dsp56k_init(void);
77 extern int radio_init(void);
78 extern int pc110pad_init(void);
79 extern int pmu_device_init(void);
80 extern int qpmouse_init(void);
81 extern int tosh_init(void);
82
83 static int misc_read_proc(char *buf, char **start, off_t offset,
84                           int len, int *eof, void *private)
85 {
86         struct miscdevice *p;
87         int written;
88
89         written=0;
90         for (p = misc_list.next; p != &misc_list && written < len; p = p->next) {
91                 written += sprintf(buf+written, "%3i %s\n",p->minor, p->name ?: "");
92                 if (written < offset) {
93                         offset -= written;
94                         written = 0;
95                 }
96         }
97         *start = buf + offset;
98         written -= offset;
99         if(written > len) {
100                 *eof = 0;
101                 return len;
102         }
103         *eof = 1;
104         return (written<0) ? 0 : written;
105 }
106
107
108 static int misc_open(struct inode * inode, struct file * file)
109 {
110         int minor = MINOR(inode->i_rdev);
111         struct miscdevice *c;
112         int err = -ENODEV;
113         struct file_operations *old_fops, *new_fops = NULL;
114         
115         down(&misc_sem);
116         
117         c = misc_list.next;
118
119         while ((c != &misc_list) && (c->minor != minor))
120                 c = c->next;
121         if (c != &misc_list)
122                 new_fops = fops_get(c->fops);
123         if (!new_fops) {
124                 char modname[20];
125                 up(&misc_sem);
126                 sprintf(modname, "char-major-%d-%d", MISC_MAJOR, minor);
127                 request_module(modname);
128                 down(&misc_sem);
129                 c = misc_list.next;
130                 while ((c != &misc_list) && (c->minor != minor))
131                         c = c->next;
132                 if (c == &misc_list || (new_fops = fops_get(c->fops)) == NULL)
133                         goto fail;
134         }
135
136         err = 0;
137         old_fops = file->f_op;
138         file->f_op = new_fops;
139         if (file->f_op->open) {
140                 err=file->f_op->open(inode,file);
141                 if (err) {
142                         fops_put(file->f_op);
143                         file->f_op = fops_get(old_fops);
144                 }
145         }
146         fops_put(old_fops);
147 fail:
148         up(&misc_sem);
149         return err;
150 }
151
152 static struct file_operations misc_fops = {
153         owner:          THIS_MODULE,
154         open:           misc_open,
155 };
156
157
158 /**
159  *      misc_register   -       register a miscellaneous device
160  *      @misc: device structure
161  *      
162  *      Register a miscellaneous device with the kernel. If the minor
163  *      number is set to %MISC_DYNAMIC_MINOR a minor number is assigned
164  *      and placed in the minor field of the structure. For other cases
165  *      the minor number requested is used.
166  *
167  *      The structure passed is linked into the kernel and may not be
168  *      destroyed until it has been unregistered.
169  *
170  *      A zero is returned on success and a negative errno code for
171  *      failure.
172  */
173  
174 int misc_register(struct miscdevice * misc)
175 {
176         static devfs_handle_t devfs_handle;
177
178         if (misc->next || misc->prev)
179                 return -EBUSY;
180         down(&misc_sem);
181         if (misc->minor == MISC_DYNAMIC_MINOR) {
182                 int i = DYNAMIC_MINORS;
183                 while (--i >= 0)
184                         if ( (misc_minors[i>>3] & (1 << (i&7))) == 0)
185                                 break;
186                 if (i<0)
187                 {
188                         up(&misc_sem);
189                         return -EBUSY;
190                 }
191                 misc->minor = i;
192         }
193         if (misc->minor < DYNAMIC_MINORS)
194                 misc_minors[misc->minor >> 3] |= 1 << (misc->minor & 7);
195         if (!devfs_handle)
196                 devfs_handle = devfs_mk_dir (NULL, "misc", NULL);
197         misc->devfs_handle =
198             devfs_register (devfs_handle, misc->name, DEVFS_FL_NONE,
199                             MISC_MAJOR, misc->minor,
200                             S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP,
201                             misc->fops, NULL);
202
203         /*
204          * Add it to the front, so that later devices can "override"
205          * earlier defaults
206          */
207         misc->prev = &misc_list;
208         misc->next = misc_list.next;
209         misc->prev->next = misc;
210         misc->next->prev = misc;
211         up(&misc_sem);
212         return 0;
213 }
214
215 /**
216  *      misc_deregister - unregister a miscellaneous device
217  *      @misc: device to unregister
218  *
219  *      Unregister a miscellaneous device that was previously
220  *      successfully registered with misc_register(). Success
221  *      is indicated by a zero return, a negative errno code
222  *      indicates an error.
223  */
224
225 int misc_deregister(struct miscdevice * misc)
226 {
227         int i = misc->minor;
228         if (!misc->next || !misc->prev)
229                 return -EINVAL;
230         down(&misc_sem);
231         misc->prev->next = misc->next;
232         misc->next->prev = misc->prev;
233         misc->next = NULL;
234         misc->prev = NULL;
235         devfs_unregister (misc->devfs_handle);
236         if (i < DYNAMIC_MINORS && i>0) {
237                 misc_minors[i>>3] &= ~(1 << (misc->minor & 7));
238         }
239         up(&misc_sem);
240         return 0;
241 }
242
243 EXPORT_SYMBOL(misc_register);
244 EXPORT_SYMBOL(misc_deregister);
245
246 int __init misc_init(void)
247 {
248         create_proc_read_entry("misc", 0, 0, misc_read_proc, NULL);
249 #if defined CONFIG_82C710_MOUSE
250         qpmouse_init();
251 #endif
252 #ifdef CONFIG_PC110_PAD
253         pc110pad_init();
254 #endif
255 #ifdef CONFIG_MVME16x
256         rtc_MK48T08_init();
257 #endif
258 #ifdef CONFIG_BVME6000
259         rtc_DP8570A_init();
260 #endif
261 #ifdef CONFIG_SGI_DS1286
262         ds1286_init();
263 #endif
264 #ifdef CONFIG_ATARI_DSP56K
265         dsp56k_init();
266 #endif
267 #ifdef CONFIG_MISC_RADIO
268         radio_init();
269 #endif
270 #ifdef CONFIG_PMAC_PBOOK
271         pmu_device_init();
272 #endif
273 #ifdef CONFIG_SGI_NEWPORT_GFX
274         gfx_register ();
275 #endif
276 #ifdef CONFIG_SGI_IP22
277         streamable_init ();
278 #endif
279 #ifdef CONFIG_SGI_NEWPORT_GFX
280         gfx_register ();
281 #endif
282 #ifdef CONFIG_SGI
283         streamable_init ();
284 #endif
285 #ifdef CONFIG_TOSHIBA
286         tosh_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 }