Import 2.1.81
[davej-history.git] / drivers / char / misc.c
blob98b74551c363c6340fd0512315e29076a5db4be8
1 /*
2 * linux/drivers/char/misc.c
4 * Generic misc open routine by Johan Myreen
6 * Based on code from Linus
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.
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.
16 * Support for loadable modules. 8-Sep-95 Philip Blundell <pjb27@cam.ac.uk>
18 * Fixed a failing symbol register to free the device registration
19 * Alan Cox <alan@lxorguk.ukuu.org.uk> 21-Jan-96
21 * Dynamic minors and /proc/mice by Alessandro Rubini. 26-Mar-96
23 * Renamed to misc and miscdevice to be more accurate. Alan Cox 26-Mar-96
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>
31 #include <linux/config.h>
32 #include <linux/module.h>
34 #include <linux/fs.h>
35 #include <linux/errno.h>
36 #include <linux/miscdevice.h>
37 #include <linux/kernel.h>
38 #include <linux/major.h>
39 #include <linux/malloc.h>
40 #include <linux/proc_fs.h>
41 #include <linux/stat.h>
42 #include <linux/init.h>
43 #ifdef CONFIG_APM
44 #include <linux/apm_bios.h>
45 #endif
47 #include <linux/tty.h>
48 #include <linux/selection.h>
49 #ifdef CONFIG_KERNELD
50 #include <linux/kerneld.h>
51 #endif
54 * Head entry for the doubly linked miscdevice list
56 static struct miscdevice misc_list = { 0, "head", NULL, &misc_list, &misc_list };
59 * Assigned numbers, used for dynamic minors
61 #define DYNAMIC_MINORS 64 /* like dynamic majors */
62 static unsigned char misc_minors[DYNAMIC_MINORS / 8];
64 #ifndef MODULE
65 extern int bus_mouse_init(void);
66 extern int psaux_init(void);
67 extern int ms_bus_mouse_init(void);
68 extern int atixl_busmouse_init(void);
69 extern int amiga_mouse_init(void);
70 extern int atari_mouse_init(void);
71 extern int sun_mouse_init(void);
72 extern void watchdog_init(void);
73 extern void wdt_init(void);
74 extern void acq_init(void);
75 extern void pcwatchdog_init(void);
76 extern int rtc_init(void);
77 extern int dsp56k_init(void);
78 extern int nvram_init(void);
79 extern int radio_init(void);
80 extern void hfmodem_init(void);
81 #ifdef CONFIG_PC110_PAD
82 extern int pc110pad_init(void);
83 #endif
85 #ifdef CONFIG_PROC_FS
86 static int misc_read_proc(char *buf, char **start, off_t offset,
87 int len, int *eof, void *private)
89 struct miscdevice *p;
91 len=0;
92 for (p = misc_list.next; p != &misc_list && len < 4000; p = p->next)
93 len += sprintf(buf+len, "%3i %s\n",p->minor, p->name ?: "");
94 *start = buf + offset;
95 return len > offset ? len - offset : 0;
98 #endif /* PROC_FS */
99 #endif /* !MODULE */
101 static int misc_open(struct inode * inode, struct file * file)
103 int minor = MINOR(inode->i_rdev);
104 struct miscdevice *c = misc_list.next;
105 file->f_op = NULL;
107 while ((c != &misc_list) && (c->minor != minor))
108 c = c->next;
109 if (c == &misc_list) {
110 #ifdef CONFIG_KERNELD
111 char modname[20];
112 sprintf(modname, "char-major-%d-%d", MISC_MAJOR, minor);
113 request_module(modname);
114 c = misc_list.next;
115 while ((c != &misc_list) && (c->minor != minor))
116 c = c->next;
117 if (c == &misc_list)
118 #endif
119 return -ENODEV;
122 if ((file->f_op = c->fops) && file->f_op->open)
123 return file->f_op->open(inode,file);
124 else
125 return -ENODEV;
128 static struct file_operations misc_fops = {
129 NULL, /* seek */
130 NULL, /* read */
131 NULL, /* write */
132 NULL, /* readdir */
133 NULL, /* poll */
134 NULL, /* ioctl */
135 NULL, /* mmap */
136 misc_open,
137 NULL /* release */
140 int misc_register(struct miscdevice * misc)
142 if (misc->next || misc->prev)
143 return -EBUSY;
144 if (misc->minor == MISC_DYNAMIC_MINOR) {
145 int i = DYNAMIC_MINORS;
146 while (--i >= 0)
147 if ( (misc_minors[i>>3] & (1 << (i&7))) == 0)
148 break;
149 if (i<0) return -EBUSY;
150 misc->minor = i;
152 if (misc->minor < DYNAMIC_MINORS)
153 misc_minors[misc->minor >> 3] |= 1 << (misc->minor & 7);
154 MOD_INC_USE_COUNT;
155 misc->next = &misc_list;
156 misc->prev = misc_list.prev;
157 misc->prev->next = misc;
158 misc->next->prev = misc;
159 return 0;
162 int misc_deregister(struct miscdevice * misc)
164 int i = misc->minor;
165 if (!misc->next || !misc->prev)
166 return -EINVAL;
167 MOD_DEC_USE_COUNT;
168 misc->prev->next = misc->next;
169 misc->next->prev = misc->prev;
170 misc->next = NULL;
171 misc->prev = NULL;
172 if (i < DYNAMIC_MINORS && i>0) {
173 misc_minors[i>>3] &= ~(1 << (misc->minor & 7));
175 return 0;
178 #ifdef MODULE
180 #define misc_init init_module
182 void cleanup_module(void)
184 unregister_chrdev(MISC_MAJOR, "misc");
187 #endif
189 EXPORT_SYMBOL(misc_register);
190 EXPORT_SYMBOL(misc_deregister);
192 #if defined(CONFIG_PROC_FS) && !defined(MODULE)
193 static struct proc_dir_entry *proc_misc;
194 #endif
196 __initfunc(int misc_init(void))
198 #ifndef MODULE
199 #ifdef CONFIG_PROC_FS
200 proc_misc = create_proc_entry("misc", 0, 0);
201 if (proc_misc)
202 proc_misc->read_proc = misc_read_proc;
203 #endif /* PROC_FS */
204 #ifdef CONFIG_BUSMOUSE
205 bus_mouse_init();
206 #endif
207 #if defined CONFIG_PSMOUSE
208 psaux_init();
209 #endif
210 #ifdef CONFIG_MS_BUSMOUSE
211 ms_bus_mouse_init();
212 #endif
213 #ifdef CONFIG_ATIXL_BUSMOUSE
214 atixl_busmouse_init();
215 #endif
216 #ifdef CONFIG_AMIGAMOUSE
217 amiga_mouse_init();
218 #endif
219 #ifdef CONFIG_ATARIMOUSE
220 atari_mouse_init();
221 #endif
222 #ifdef CONFIG_SUN_MOUSE
223 sun_mouse_init();
224 #endif
225 #ifdef CONFIG_PC110_PAD
226 pc110pad_init();
227 #endif
229 * Only one watchdog can succeed. We probe the pcwatchdog first,
230 * then the wdt cards and finally the software watchdog which always
231 * works. This means if your hardware watchdog dies or is 'borrowed'
232 * for some reason the software watchdog still gives you some cover.
234 #ifdef CONFIG_PCWATCHDOG
235 pcwatchdog_init();
236 #endif
237 #ifdef CONFIG_WDT
238 wdt_init();
239 #endif
240 #ifdef CONFIG_ACQUIRE_WDT
241 acq_init();
242 #endif
243 #ifdef CONFIG_SOFT_WATCHDOG
244 watchdog_init();
245 #endif
246 #ifdef CONFIG_APM
247 apm_bios_init();
248 #endif
249 #ifdef CONFIG_H8
250 h8_init();
251 #endif
252 #ifdef CONFIG_RTC
253 rtc_init();
254 #endif
255 #ifdef CONFIG_ATARI_DSP56K
256 dsp56k_init();
257 #endif
258 #ifdef CONFIG_HFMODEM
259 hfmodem_init();
260 #endif
261 #ifdef CONFIG_NVRAM
262 nvram_init();
263 #endif
264 #ifdef CONFIG_MISC_RADIO
265 radio_init();
266 #endif
267 #ifdef CONFIG_HFMODEM
268 hfmodem_init();
269 #endif
270 #endif /* !MODULE */
271 if (register_chrdev(MISC_MAJOR,"misc",&misc_fops)) {
272 printk("unable to get major %d for misc devices\n",
273 MISC_MAJOR);
274 return -EIO;
277 return 0;