Linux 2.3.7pre1
[davej-history.git] / drivers / char / misc.c
blobebe17535117c5b6c3efd111c27ea2f21ffb6e7a4
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 extern void watchdog_init(void);
74 extern void wdt_init(void);
75 extern void acq_init(void);
76 extern void dtlk_init(void);
77 extern void pcwatchdog_init(void);
78 extern int rtc_init(void);
79 extern int rtc_sun_init(void); /* Combines MK48T02 and MK48T08 */
80 extern int rtc_DP8570A_init(void);
81 extern int rtc_MK48T08_init(void);
82 extern int dsp56k_init(void);
83 extern int nvram_init(void);
84 extern int radio_init(void);
85 extern void hfmodem_init(void);
86 extern int pc110pad_init(void);
87 extern int pmu_device_init(void);
89 static int misc_read_proc(char *buf, char **start, off_t offset,
90 int len, int *eof, void *private)
92 struct miscdevice *p;
94 len=0;
95 for (p = misc_list.next; p != &misc_list && len < 4000; p = p->next)
96 len += sprintf(buf+len, "%3i %s\n",p->minor, p->name ?: "");
97 *start = buf + offset;
98 return len > offset ? len - offset : 0;
102 static int misc_open(struct inode * inode, struct file * file)
104 int minor = MINOR(inode->i_rdev);
105 struct miscdevice *c = misc_list.next;
106 file->f_op = NULL;
108 while ((c != &misc_list) && (c->minor != minor))
109 c = c->next;
110 if (c == &misc_list) {
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 return -ENODEV;
121 if ((file->f_op = c->fops) && file->f_op->open)
122 return file->f_op->open(inode,file);
123 else
124 return -ENODEV;
127 static struct file_operations misc_fops = {
128 NULL, /* seek */
129 NULL, /* read */
130 NULL, /* write */
131 NULL, /* readdir */
132 NULL, /* poll */
133 NULL, /* ioctl */
134 NULL, /* mmap */
135 misc_open,
136 NULL, /* flush */
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);
156 * Add it to the front, so that later devices can "override"
157 * earlier defaults
159 misc->prev = &misc_list;
160 misc->next = misc_list.next;
161 misc->prev->next = misc;
162 misc->next->prev = misc;
163 return 0;
166 int misc_deregister(struct miscdevice * misc)
168 int i = misc->minor;
169 if (!misc->next || !misc->prev)
170 return -EINVAL;
171 misc->prev->next = misc->next;
172 misc->next->prev = misc->prev;
173 misc->next = NULL;
174 misc->prev = NULL;
175 if (i < DYNAMIC_MINORS && i>0) {
176 misc_minors[i>>3] &= ~(1 << (misc->minor & 7));
178 return 0;
181 EXPORT_SYMBOL(misc_register);
182 EXPORT_SYMBOL(misc_deregister);
184 static struct proc_dir_entry *proc_misc;
186 int __init misc_init(void)
188 proc_misc = create_proc_entry("misc", 0, 0);
189 if (proc_misc)
190 proc_misc->read_proc = misc_read_proc;
191 #ifdef CONFIG_BUSMOUSE
192 bus_mouse_init();
193 #endif
194 #if defined CONFIG_82C710_MOUSE
195 qpmouse_init();
196 #endif
197 #ifdef CONFIG_MS_BUSMOUSE
198 ms_bus_mouse_init();
199 #endif
200 #ifdef CONFIG_ATIXL_BUSMOUSE
201 atixl_busmouse_init();
202 #endif
203 #ifdef CONFIG_AMIGAMOUSE
204 amiga_mouse_init();
205 #endif
206 #ifdef CONFIG_ATARIMOUSE
207 atari_mouse_init();
208 #endif
209 #ifdef CONFIG_SUN_MOUSE
210 sun_mouse_init();
211 #endif
212 #ifdef CONFIG_ADBMOUSE
213 adb_mouse_init();
214 #endif
215 #ifdef CONFIG_PC110_PAD
216 pc110pad_init();
217 #endif
219 * Only one watchdog can succeed. We probe the pcwatchdog first,
220 * then the wdt cards and finally the software watchdog which always
221 * works. This means if your hardware watchdog dies or is 'borrowed'
222 * for some reason the software watchdog still gives you some cover.
224 #ifdef CONFIG_PCWATCHDOG
225 pcwatchdog_init();
226 #endif
227 #ifdef CONFIG_WDT
228 wdt_init();
229 #endif
230 #ifdef CONFIG_ACQUIRE_WDT
231 acq_init();
232 #endif
233 #ifdef CONFIG_SOFT_WATCHDOG
234 watchdog_init();
235 #endif
236 #ifdef CONFIG_DTLK
237 dtlk_init();
238 #endif
239 #ifdef CONFIG_APM
240 apm_bios_init();
241 #endif
242 #ifdef CONFIG_H8
243 h8_init();
244 #endif
245 #ifdef CONFIG_MVME16x
246 rtc_MK48T08_init();
247 #endif
248 #ifdef CONFIG_BVME6000
249 rtc_DP8570A_init();
250 #endif
251 #if defined(CONFIG_SUN_MOSTEK_RTC)
252 rtc_sun_init();
253 #endif
254 #if defined(CONFIG_RTC)
255 rtc_init();
256 #endif
257 #ifdef CONFIG_ATARI_DSP56K
258 dsp56k_init();
259 #endif
260 #ifdef CONFIG_HFMODEM
261 hfmodem_init();
262 #endif
263 #ifdef CONFIG_NVRAM
264 nvram_init();
265 #endif
266 #ifdef CONFIG_MISC_RADIO
267 radio_init();
268 #endif
269 #ifdef CONFIG_HFMODEM
270 hfmodem_init();
271 #endif
272 #ifdef CONFIG_PMAC_PBOOK
273 pmu_device_init();
274 #endif
275 if (register_chrdev(MISC_MAJOR,"misc",&misc_fops)) {
276 printk("unable to get major %d for misc devices\n",
277 MISC_MAJOR);
278 return -EIO;
281 return 0;