1 /*
2 * linux/drivers/char/misc.c
3 *
4 * Generic misc open routine by Johan Myreen
5 *
6 * Based on code from Linus
7 *
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.
12 *
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.
15 *
16 * Support for loadable modules. 8-Sep-95 Philip Blundell <pjb27@cam.ac.uk>
17 *
18 * Fixed a failing symbol register to free the device registration
19 * Alan Cox <alan@lxorguk.ukuu.org.uk> 21-Jan-96
20 *
21 * Dynamic minors and /proc/mice by Alessandro Rubini. 26-Mar-96
22 *
23 * Renamed to misc and miscdevice to be more accurate. Alan Cox 26-Mar-96
24 *
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>
29 *
30 * Changes for kmod (from kerneld):
31 * Cyrus Durgin <cider@speakeasy.org>
32 *
33 * Added devfs support. Richard Gooch <rgooch@atnf.csiro.au> 10-Jan-1998
34 */
35
36 #include <linux/module.h>
37 #include <linux/config.h>
38
39 #include <linux/fs.h>
40 #include <linux/errno.h>
41 #include <linux/miscdevice.h>
42 #include <linux/kernel.h>
43 #include <linux/major.h>
44 #include <linux/malloc.h>
45 #include <linux/proc_fs.h>
46 #include <linux/devfs_fs_kernel.h>
47 #include <linux/stat.h>
48 #include <linux/init.h>
49
50 #include <linux/tty.h>
51 #include <linux/selection.h>
52 #include <linux/kmod.h>
53
54 #include "busmouse.h"
55
56 /*
57 * Head entry for the doubly linked miscdevice list
58 */
59 static struct miscdevice misc_list = { 0, "head", NULL, &misc_list, &misc_list };
60 static DECLARE_MUTEX(misc_sem);
61
62 /*
63 * Assigned numbers, used for dynamic minors
64 */
65 #define DYNAMIC_MINORS 64 /* like dynamic majors */
66 static unsigned char misc_minors[DYNAMIC_MINORS / 8];
67
68 extern int psaux_init(void);
69 #ifdef CONFIG_SGI_NEWPORT_GFX
70 extern void gfx_register(void);
71 #endif
72 extern void streamable_init(void);
73 extern int rtc_sun_init(void); /* Combines MK48T02 and MK48T08 */
74 extern int rtc_DP8570A_init(void);
75 extern int rtc_MK48T08_init(void);
76 extern int ds1286_init(void);
77 extern int dsp56k_init(void);
78 extern int radio_init(void);
79 extern int pc110pad_init(void);
80 extern int pmu_device_init(void);
81 extern int qpmouse_init(void);
82 extern int tosh_init(void);
83
84 static int misc_read_proc(char *buf, char **start, off_t offset,
85 int len, int *eof, void *private)
86 {
87 struct miscdevice *p;
88 int written;
89
90 written=0;
91 for (p = misc_list.next; p != &misc_list && written < len; p = p->next) {
92 written += sprintf(buf+written, "%3i %s\n",p->minor, p->name ?: "");
93 if (written < offset) {
94 offset -= written;
95 written = 0;
96 }
97 }
98 *start = buf + offset;
99 written -= offset;
100 if(written > len) {
101 *eof = 0;
102 return len;
103 }
104 *eof = 1;
105 return (written<0) ? 0 : written;
106 }
107
108
109 static int misc_open(struct inode * inode, struct file * file)
110 {
111 int minor = MINOR(inode->i_rdev);
112 struct miscdevice *c;
113 int err = -ENODEV;
114 struct file_operations *old_fops, *new_fops = NULL;
115
116 down(&misc_sem);
117
118 c = misc_list.next;
119
120 while ((c != &misc_list) && (c->minor != minor))
121 c = c->next;
122 if (c != &misc_list)
123 new_fops = fops_get(c->fops);
124 if (!new_fops) {
125 char modname[20];
126 up(&misc_sem);
127 sprintf(modname, "char-major-%d-%d", MISC_MAJOR, minor);
128 request_module(modname);
129 down(&misc_sem);
130 c = misc_list.next;
131 while ((c != &misc_list) && (c->minor != minor))
132 c = c->next;
133 if (c == &misc_list || (new_fops = fops_get(c->fops)) == NULL)
134 goto fail;
135 }
136
137 err = 0;
138 old_fops = file->f_op;
139 file->f_op = new_fops;
140 if (file->f_op->open) {
141 err=file->f_op->open(inode,file);
142 if (err) {
143 fops_put(file->f_op);
144 file->f_op = fops_get(old_fops);
145 }
146 }
147 fops_put(old_fops);
148 fail:
149 up(&misc_sem);
150 return err;
151 }
152
153 static struct file_operations misc_fops = {
154 owner: THIS_MODULE,
155 open: misc_open,
156 };
157
158
159 /**
160 * misc_register - register a miscellaneous device
161 * @misc: device structure
162 *
163 * Register a miscellaneous device with the kernel. If the minor
164 * number is set to %MISC_DYNAMIC_MINOR a minor number is assigned
165 * and placed in the minor field of the structure. For other cases
166 * the minor number requested is used.
167 *
168 * The structure passed is linked into the kernel and may not be
169 * destroyed until it has been unregistered.
170 *
171 * A zero is returned on success and a negative errno code for
172 * failure.
173 */
174
175 int misc_register(struct miscdevice * misc)
176 {
177 static devfs_handle_t devfs_handle;
178
179 if (misc->next || misc->prev)
180 return -EBUSY;
181 down(&misc_sem);
182 if (misc->minor == MISC_DYNAMIC_MINOR) {
183 int i = DYNAMIC_MINORS;
184 while (--i >= 0)
185 if ( (misc_minors[i>>3] & (1 << (i&7))) == 0)
186 break;
187 if (i<0)
188 {
189 up(&misc_sem);
190 return -EBUSY;
191 }
192 misc->minor = i;
193 }
194 if (misc->minor < DYNAMIC_MINORS)
195 misc_minors[misc->minor >> 3] |= 1 << (misc->minor & 7);
196 if (!devfs_handle)
197 devfs_handle = devfs_mk_dir (NULL, "misc", NULL);
198 misc->devfs_handle =
199 devfs_register (devfs_handle, misc->name, DEVFS_FL_NONE,
200 MISC_MAJOR, misc->minor,
201 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP,
202 misc->fops, NULL);
203
204 /*
205 * Add it to the front, so that later devices can "override"
206 * earlier defaults
207 */
208 misc->prev = &misc_list;
209 misc->next = misc_list.next;
210 misc->prev->next = misc;
211 misc->next->prev = misc;
212 up(&misc_sem);
213 return 0;
214 }
215
216 /**
217 * misc_deregister - unregister a miscellaneous device
218 * @misc: device to unregister
219 *
220 * Unregister a miscellaneous device that was previously
221 * successfully registered with misc_register(). Success
222 * is indicated by a zero return, a negative errno code
223 * indicates an error.
224 */
225
226 int misc_deregister(struct miscdevice * misc)
227 {
228 int i = misc->minor;
229 if (!misc->next || !misc->prev)
230 return -EINVAL;
231 down(&misc_sem);
232 misc->prev->next = misc->next;
233 misc->next->prev = misc->prev;
234 misc->next = NULL;
235 misc->prev = NULL;
236 devfs_unregister (misc->devfs_handle);
237 if (i < DYNAMIC_MINORS && i>0) {
238 misc_minors[i>>3] &= ~(1 << (misc->minor & 7));
239 }
240 up(&misc_sem);
241 return 0;
242 }
243
244 EXPORT_SYMBOL(misc_register);
245 EXPORT_SYMBOL(misc_deregister);
246
247 int __init misc_init(void)
248 {
249 create_proc_read_entry("misc", 0, 0, misc_read_proc, NULL);
250 #if defined CONFIG_82C710_MOUSE
251 qpmouse_init();
252 #endif
253 #ifdef CONFIG_PC110_PAD
254 pc110pad_init();
255 #endif
256 #ifdef CONFIG_MVME16x
257 rtc_MK48T08_init();
258 #endif
259 #ifdef CONFIG_BVME6000
260 rtc_DP8570A_init();
261 #endif
262 #if defined(CONFIG_SUN_MOSTEK_RTC)
263 rtc_sun_init();
264 #endif
265 #ifdef CONFIG_SGI_DS1286
266 ds1286_init();
267 #endif
268 #ifdef CONFIG_ATARI_DSP56K
269 dsp56k_init();
270 #endif
271 #ifdef CONFIG_MISC_RADIO
272 radio_init();
273 #endif
274 #ifdef CONFIG_PMAC_PBOOK
275 pmu_device_init();
276 #endif
277 #ifdef CONFIG_SGI_NEWPORT_GFX
278 gfx_register ();
279 #endif
280 #ifdef CONFIG_SGI_IP22
281 streamable_init ();
282 #endif
283 #ifdef CONFIG_SGI_NEWPORT_GFX
284 gfx_register ();
285 #endif
286 #ifdef CONFIG_SGI
287 streamable_init ();
288 #endif
289 #ifdef CONFIG_TOSHIBA
290 tosh_init();
291 #endif
292 if (devfs_register_chrdev(MISC_MAJOR,"misc",&misc_fops)) {
293 printk("unable to get major %d for misc devices\n",
294 MISC_MAJOR);
295 return -EIO;
296 }
297 return 0;
298 }
299
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.