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 pmu_device_init(void);
77 extern int tosh_init(void);
78 extern int i8k_init(void);
79 extern int lcd_init(void);
81 static int misc_read_proc(char *buf, char **start, off_t offset,
82 int len, int *eof, void *private)
88 for (p = misc_list.next; p != &misc_list && written < len; p = p->next) {
89 written += sprintf(buf+written, "%3i %s\n",p->minor, p->name ?: "");
90 if (written < offset) {
95 *start = buf + offset;
102 return (written<0) ? 0 : written;
106 static int misc_open(struct inode * inode, struct file * file)
108 int minor = MINOR(inode->i_rdev);
109 struct miscdevice *c;
111 struct file_operations *old_fops, *new_fops = NULL;
117 while ((c != &misc_list) && (c->minor != minor))
120 new_fops = fops_get(c->fops);
124 sprintf(modname, "char-major-%d-%d", MISC_MAJOR, minor);
125 request_module(modname);
128 while ((c != &misc_list) && (c->minor != minor))
130 if (c == &misc_list || (new_fops = fops_get(c->fops)) == NULL)
135 old_fops = file->f_op;
136 file->f_op = new_fops;
137 if (file->f_op->open) {
138 err=file->f_op->open(inode,file);
140 fops_put(file->f_op);
141 file->f_op = fops_get(old_fops);
150 static struct file_operations misc_fops = {
157 * misc_register - register a miscellaneous device
158 * @misc: device structure
160 * Register a miscellaneous device with the kernel. If the minor
161 * number is set to %MISC_DYNAMIC_MINOR a minor number is assigned
162 * and placed in the minor field of the structure. For other cases
163 * the minor number requested is used.
165 * The structure passed is linked into the kernel and may not be
166 * destroyed until it has been unregistered.
168 * A zero is returned on success and a negative errno code for
172 int misc_register(struct miscdevice * misc)
174 static devfs_handle_t devfs_handle, dir;
175 struct miscdevice *c;
177 if (misc->next || misc->prev)
182 while ((c != &misc_list) && (c->minor != misc->minor))
184 if (c != &misc_list) {
189 if (misc->minor == MISC_DYNAMIC_MINOR) {
190 int i = DYNAMIC_MINORS;
192 if ( (misc_minors[i>>3] & (1 << (i&7))) == 0)
201 if (misc->minor < DYNAMIC_MINORS)
202 misc_minors[misc->minor >> 3] |= 1 << (misc->minor & 7);
204 devfs_handle = devfs_mk_dir (NULL, "misc", NULL);
205 dir = strchr (misc->name, '/') ? NULL : devfs_handle;
207 devfs_register (dir, misc->name, DEVFS_FL_NONE,
208 MISC_MAJOR, misc->minor,
209 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP,
213 * Add it to the front, so that later devices can "override"
216 misc->prev = &misc_list;
217 misc->next = misc_list.next;
218 misc->prev->next = misc;
219 misc->next->prev = misc;
225 * misc_deregister - unregister a miscellaneous device
226 * @misc: device to unregister
228 * Unregister a miscellaneous device that was previously
229 * successfully registered with misc_register(). Success
230 * is indicated by a zero return, a negative errno code
231 * indicates an error.
234 int misc_deregister(struct miscdevice * misc)
237 if (!misc->next || !misc->prev)
240 misc->prev->next = misc->next;
241 misc->next->prev = misc->prev;
244 devfs_unregister (misc->devfs_handle);
245 if (i < DYNAMIC_MINORS && i>0) {
246 misc_minors[i>>3] &= ~(1 << (misc->minor & 7));
252 EXPORT_SYMBOL(misc_register);
253 EXPORT_SYMBOL(misc_deregister);
255 int __init misc_init(void)
257 create_proc_read_entry("misc", 0, 0, misc_read_proc, NULL);
258 #ifdef CONFIG_MVME16x
261 #ifdef CONFIG_BVME6000
264 #ifdef CONFIG_SGI_DS1286
267 #ifdef CONFIG_PMAC_PBOOK
270 #ifdef CONFIG_SGI_NEWPORT_GFX
273 #ifdef CONFIG_SGI_IP22
276 #ifdef CONFIG_SGI_NEWPORT_GFX
279 #ifdef CONFIG_TOSHIBA
282 #ifdef CONFIG_COBALT_LCD
288 if (devfs_register_chrdev(MISC_MAJOR,"misc",&misc_fops)) {
289 printk("unable to get major %d for misc devices\n",