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>
33 * Added devfs support. Richard Gooch <rgooch@atnf.csiro.au> 10-Jan-1998
36 #include <linux/module.h>
37 #include <linux/config.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>
50 #include <linux/tty.h>
51 #include <linux/selection.h>
52 #include <linux/kmod.h>
57 * Head entry for the doubly linked miscdevice list
59 static struct miscdevice misc_list = { 0, "head", NULL, &misc_list, &misc_list };
60 static DECLARE_MUTEX(misc_sem);
63 * Assigned numbers, used for dynamic minors
65 #define DYNAMIC_MINORS 64 /* like dynamic majors */
66 static unsigned char misc_minors[DYNAMIC_MINORS / 8];
68 extern int psaux_init(void);
69 #ifdef CONFIG_SGI_NEWPORT_GFX
70 extern void gfx_register(void);
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);
83 static int misc_read_proc(char *buf, char **start, off_t offset,
84 int len, int *eof, void *private)
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) {
97 *start = buf + offset;
104 return (written<0) ? 0 : written;
108 static int misc_open(struct inode * inode, struct file * file)
110 int minor = MINOR(inode->i_rdev);
111 struct miscdevice *c;
113 struct file_operations *old_fops, *new_fops = NULL;
119 while ((c != &misc_list) && (c->minor != minor))
122 new_fops = fops_get(c->fops);
126 sprintf(modname, "char-major-%d-%d", MISC_MAJOR, minor);
127 request_module(modname);
130 while ((c != &misc_list) && (c->minor != minor))
132 if (c == &misc_list || (new_fops = fops_get(c->fops)) == NULL)
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);
142 fops_put(file->f_op);
143 file->f_op = fops_get(old_fops);
152 static struct file_operations misc_fops = {
159 * misc_register - register a miscellaneous device
160 * @misc: device structure
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.
167 * The structure passed is linked into the kernel and may not be
168 * destroyed until it has been unregistered.
170 * A zero is returned on success and a negative errno code for
174 int misc_register(struct miscdevice * misc)
176 static devfs_handle_t devfs_handle;
178 if (misc->next || misc->prev)
181 if (misc->minor == MISC_DYNAMIC_MINOR) {
182 int i = DYNAMIC_MINORS;
184 if ( (misc_minors[i>>3] & (1 << (i&7))) == 0)
193 if (misc->minor < DYNAMIC_MINORS)
194 misc_minors[misc->minor >> 3] |= 1 << (misc->minor & 7);
196 devfs_handle = devfs_mk_dir (NULL, "misc", NULL);
198 devfs_register (devfs_handle, misc->name, DEVFS_FL_NONE,
199 MISC_MAJOR, misc->minor,
200 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP,
204 * Add it to the front, so that later devices can "override"
207 misc->prev = &misc_list;
208 misc->next = misc_list.next;
209 misc->prev->next = misc;
210 misc->next->prev = misc;
216 * misc_deregister - unregister a miscellaneous device
217 * @misc: device to unregister
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.
225 int misc_deregister(struct miscdevice * misc)
228 if (!misc->next || !misc->prev)
231 misc->prev->next = misc->next;
232 misc->next->prev = misc->prev;
235 devfs_unregister (misc->devfs_handle);
236 if (i < DYNAMIC_MINORS && i>0) {
237 misc_minors[i>>3] &= ~(1 << (misc->minor & 7));
243 EXPORT_SYMBOL(misc_register);
244 EXPORT_SYMBOL(misc_deregister);
246 int __init misc_init(void)
248 create_proc_read_entry("misc", 0, 0, misc_read_proc, NULL);
249 #if defined CONFIG_82C710_MOUSE
252 #ifdef CONFIG_PC110_PAD
255 #ifdef CONFIG_MVME16x
258 #ifdef CONFIG_BVME6000
261 #ifdef CONFIG_SGI_DS1286
264 #ifdef CONFIG_ATARI_DSP56K
267 #ifdef CONFIG_MISC_RADIO
270 #ifdef CONFIG_PMAC_PBOOK
273 #ifdef CONFIG_SGI_NEWPORT_GFX
276 #ifdef CONFIG_SGI_IP22
279 #ifdef CONFIG_SGI_NEWPORT_GFX
285 #ifdef CONFIG_TOSHIBA
288 if (devfs_register_chrdev(MISC_MAJOR,"misc",&misc_fops)) {
289 printk("unable to get major %d for misc devices\n",