Import 2.3.9pre5
[davej-history.git] / drivers / char / misc.c
blob1feedb594b491b3403a4871b2c90f9971c15c3fb
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>
30 * Changes for kmod (from kerneld):
31 Cyrus Durgin <cider@speakeasy.org>
34 #include <linux/module.h>
35 #include <linux/config.h>
37 #include <linux/fs.h>
38 #include <linux/errno.h>
39 #include <linux/miscdevice.h>
40 #include <linux/kernel.h>
41 #include <linux/major.h>
42 #include <linux/malloc.h>
43 #include <linux/proc_fs.h>
44 #include <linux/stat.h>
45 #include <linux/init.h>
46 #ifdef CONFIG_APM
47 #include <linux/apm_bios.h>
48 #endif
50 #include <linux/tty.h>
51 #include <linux/selection.h>
52 #include <linux/kmod.h>
55 * Head entry for the doubly linked miscdevice list
57 static struct miscdevice misc_list = { 0, "head", NULL, &misc_list, &misc_list };
60 * Assigned numbers, used for dynamic minors
62 #define DYNAMIC_MINORS 64 /* like dynamic majors */
63 static unsigned char misc_minors[DYNAMIC_MINORS / 8];
65 extern int bus_mouse_init(void);
66 extern int qpmouse_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 int adb_mouse_init(void);
73 #ifdef CONFIG_SGI_NEWPORT_GFX
74 extern void gfx_register(void);
75 #endif
76 extern void streamable_init(void);
77 extern void watchdog_init(void);
78 extern void wdt_init(void);
79 extern void acq_init(void);
80 extern void dtlk_init(void);
81 extern void pcwatchdog_init(void);
82 extern int rtc_init(void);
83 extern int rtc_sun_init(void); /* Combines MK48T02 and MK48T08 */
84 extern int rtc_DP8570A_init(void);
85 extern int rtc_MK48T08_init(void);
86 extern int dsp56k_init(void);
87 extern int nvram_init(void);
88 extern int radio_init(void);
89 extern void hfmodem_init(void);
90 extern int pc110pad_init(void);
91 extern int pmu_device_init(void);
93 static int misc_read_proc(char *buf, char **start, off_t offset,
94 int len, int *eof, void *private)
96 struct miscdevice *p;
98 len=0;
99 for (p = misc_list.next; p != &misc_list && len < 4000; p = p->next)
100 len += sprintf(buf+len, "%3i %s\n",p->minor, p->name ?: "");
101 *start = buf + offset;
102 return len > offset ? len - offset : 0;
106 static int misc_open(struct inode * inode, struct file * file)
108 int minor = MINOR(inode->i_rdev);
109 struct miscdevice *c = misc_list.next;
110 file->f_op = NULL;
112 while ((c != &misc_list) && (c->minor != minor))
113 c = c->next;
114 if (c == &misc_list) {
115 char modname[20];
116 sprintf(modname, "char-major-%d-%d", MISC_MAJOR, minor);
117 request_module(modname);
118 c = misc_list.next;
119 while ((c != &misc_list) && (c->minor != minor))
120 c = c->next;
121 if (c == &misc_list)
122 return -ENODEV;
125 if ((file->f_op = c->fops) && file->f_op->open)
126 return file->f_op->open(inode,file);
127 else
128 return -ENODEV;
131 static struct file_operations misc_fops = {
132 NULL, /* seek */
133 NULL, /* read */
134 NULL, /* write */
135 NULL, /* readdir */
136 NULL, /* poll */
137 NULL, /* ioctl */
138 NULL, /* mmap */
139 misc_open,
140 NULL, /* flush */
141 NULL /* release */
144 int misc_register(struct miscdevice * misc)
146 if (misc->next || misc->prev)
147 return -EBUSY;
148 if (misc->minor == MISC_DYNAMIC_MINOR) {
149 int i = DYNAMIC_MINORS;
150 while (--i >= 0)
151 if ( (misc_minors[i>>3] & (1 << (i&7))) == 0)
152 break;
153 if (i<0) return -EBUSY;
154 misc->minor = i;
156 if (misc->minor < DYNAMIC_MINORS)
157 misc_minors[misc->minor >> 3] |= 1 << (misc->minor & 7);
160 * Add it to the front, so that later devices can "override"
161 * earlier defaults
163 misc->prev = &misc_list;
164 misc->next = misc_list.next;
165 misc->prev->next = misc;
166 misc->next->prev = misc;
167 return 0;
170 int misc_deregister(struct miscdevice * misc)
172 int i = misc->minor;
173 if (!misc->next || !misc->prev)
174 return -EINVAL;
175 misc->prev->next = misc->next;
176 misc->next->prev = misc->prev;
177 misc->next = NULL;
178 misc->prev = NULL;
179 if (i < DYNAMIC_MINORS && i>0) {
180 misc_minors[i>>3] &= ~(1 << (misc->minor & 7));
182 return 0;
185 EXPORT_SYMBOL(misc_register);
186 EXPORT_SYMBOL(misc_deregister);
188 static struct proc_dir_entry *proc_misc;
190 int __init misc_init(void)
192 proc_misc = create_proc_entry("misc", 0, 0);
193 if (proc_misc)
194 proc_misc->read_proc = misc_read_proc;
195 #ifdef CONFIG_BUSMOUSE
196 bus_mouse_init();
197 #endif
198 #if defined CONFIG_82C710_MOUSE
199 qpmouse_init();
200 #endif
201 #ifdef CONFIG_MS_BUSMOUSE
202 ms_bus_mouse_init();
203 #endif
204 #ifdef CONFIG_ATIXL_BUSMOUSE
205 atixl_busmouse_init();
206 #endif
207 #ifdef CONFIG_AMIGAMOUSE
208 amiga_mouse_init();
209 #endif
210 #ifdef CONFIG_ATARIMOUSE
211 atari_mouse_init();
212 #endif
213 #ifdef CONFIG_SUN_MOUSE
214 sun_mouse_init();
215 #endif
216 #ifdef CONFIG_ADBMOUSE
217 adb_mouse_init();
218 #endif
219 #ifdef CONFIG_PC110_PAD
220 pc110pad_init();
221 #endif
223 * Only one watchdog can succeed. We probe the pcwatchdog first,
224 * then the wdt cards and finally the software watchdog which always
225 * works. This means if your hardware watchdog dies or is 'borrowed'
226 * for some reason the software watchdog still gives you some cover.
228 #ifdef CONFIG_PCWATCHDOG
229 pcwatchdog_init();
230 #endif
231 #ifdef CONFIG_WDT
232 wdt_init();
233 #endif
234 #ifdef CONFIG_ACQUIRE_WDT
235 acq_init();
236 #endif
237 #ifdef CONFIG_SOFT_WATCHDOG
238 watchdog_init();
239 #endif
240 #ifdef CONFIG_DTLK
241 dtlk_init();
242 #endif
243 #ifdef CONFIG_APM
244 apm_bios_init();
245 #endif
246 #ifdef CONFIG_H8
247 h8_init();
248 #endif
249 #ifdef CONFIG_MVME16x
250 rtc_MK48T08_init();
251 #endif
252 #ifdef CONFIG_BVME6000
253 rtc_DP8570A_init();
254 #endif
255 #if defined(CONFIG_SUN_MOSTEK_RTC)
256 rtc_sun_init();
257 #endif
258 #if defined(CONFIG_RTC)
259 rtc_init();
260 #endif
261 #ifdef CONFIG_ATARI_DSP56K
262 dsp56k_init();
263 #endif
264 #ifdef CONFIG_HFMODEM
265 hfmodem_init();
266 #endif
267 #ifdef CONFIG_NVRAM
268 nvram_init();
269 #endif
270 #ifdef CONFIG_MISC_RADIO
271 radio_init();
272 #endif
273 #ifdef CONFIG_HFMODEM
274 hfmodem_init();
275 #endif
276 #ifdef CONFIG_PMAC_PBOOK
277 pmu_device_init();
278 #endif
279 #ifdef CONFIG_SGI_NEWPORT_GFX
280 gfx_register ();
281 #endif
282 #ifdef CONFIG_SGI
283 streamable_init ();
284 #endif
285 if (register_chrdev(MISC_MAJOR,"misc",&misc_fops)) {
286 printk("unable to get major %d for misc devices\n",
287 MISC_MAJOR);
288 return -EIO;
291 return 0;