Fix prototype of SMP version of synchronize_irq.
[linux-2.6/linux-mips.git] / fs / char_dev.c
blob5b340702bdd2d20473e852b8fca01c8e2149bdbd
1 /*
2 * linux/fs/char_dev.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 #include <linux/config.h>
8 #include <linux/init.h>
9 #include <linux/fs.h>
10 #include <linux/slab.h>
11 #include <linux/string.h>
13 #include <linux/major.h>
14 #include <linux/string.h>
15 #include <linux/errno.h>
16 #include <linux/module.h>
17 #include <linux/smp_lock.h>
18 #include <linux/devfs_fs_kernel.h>
20 #include <linux/kobject.h>
21 #include <linux/kobj_map.h>
22 #include <linux/cdev.h>
24 #ifdef CONFIG_KMOD
25 #include <linux/kmod.h>
26 #endif
28 static struct kobj_map *cdev_map;
30 #define MAX_PROBE_HASH 255 /* random */
32 static rwlock_t chrdevs_lock = RW_LOCK_UNLOCKED;
34 static struct char_device_struct {
35 struct char_device_struct *next;
36 unsigned int major;
37 unsigned int baseminor;
38 int minorct;
39 const char *name;
40 struct file_operations *fops;
41 struct cdev *cdev; /* will die */
42 } *chrdevs[MAX_PROBE_HASH];
44 /* index in the above */
45 static inline int major_to_index(int major)
47 return major % MAX_PROBE_HASH;
50 /* get char device names in somewhat random order */
51 int get_chrdev_list(char *page)
53 struct char_device_struct *cd;
54 int i, len;
56 len = sprintf(page, "Character devices:\n");
58 read_lock(&chrdevs_lock);
59 for (i = 0; i < ARRAY_SIZE(chrdevs) ; i++) {
60 for (cd = chrdevs[i]; cd; cd = cd->next)
61 len += sprintf(page+len, "%3d %s\n",
62 cd->major, cd->name);
64 read_unlock(&chrdevs_lock);
66 return len;
70 * Register a single major with a specified minor range.
72 * If major == 0 this functions will dynamically allocate a major and return
73 * its number.
75 * If major > 0 this function will attempt to reserve the passed range of
76 * minors and will return zero on success.
78 * Returns a -ve errno on failure.
80 static struct char_device_struct *
81 __register_chrdev_region(unsigned int major, unsigned int baseminor,
82 int minorct, const char *name)
84 struct char_device_struct *cd, **cp;
85 int ret = 0;
86 int i;
88 cd = kmalloc(sizeof(struct char_device_struct), GFP_KERNEL);
89 if (cd == NULL)
90 return ERR_PTR(-ENOMEM);
92 write_lock_irq(&chrdevs_lock);
94 /* temporary */
95 if (major == 0) {
96 for (i = ARRAY_SIZE(chrdevs)-1; i > 0; i--) {
97 if (chrdevs[i] == NULL)
98 break;
101 if (i == 0) {
102 ret = -EBUSY;
103 goto out;
105 major = i;
106 ret = major;
109 cd->major = major;
110 cd->baseminor = baseminor;
111 cd->minorct = minorct;
112 cd->name = name;
114 i = major_to_index(major);
116 for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next)
117 if ((*cp)->major > major ||
118 ((*cp)->major == major && (*cp)->baseminor >= baseminor))
119 break;
120 if (*cp && (*cp)->major == major &&
121 (*cp)->baseminor < baseminor + minorct) {
122 ret = -EBUSY;
123 goto out;
125 cd->next = *cp;
126 *cp = cd;
127 write_unlock_irq(&chrdevs_lock);
128 return cd;
129 out:
130 write_unlock_irq(&chrdevs_lock);
131 kfree(cd);
132 return ERR_PTR(ret);
135 static struct char_device_struct *
136 __unregister_chrdev_region(unsigned major, unsigned baseminor, int minorct)
138 struct char_device_struct *cd = NULL, **cp;
139 int i = major_to_index(major);
141 write_lock_irq(&chrdevs_lock);
142 for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next)
143 if ((*cp)->major == major &&
144 (*cp)->baseminor == baseminor &&
145 (*cp)->minorct == minorct)
146 break;
147 if (*cp) {
148 cd = *cp;
149 *cp = cd->next;
151 write_unlock_irq(&chrdevs_lock);
152 return cd;
155 int register_chrdev_region(dev_t from, unsigned count, char *name)
157 struct char_device_struct *cd;
158 dev_t to = from + count;
159 dev_t n, next;
161 for (n = from; n < to; n = next) {
162 next = MKDEV(MAJOR(n)+1, 0);
163 if (next > to)
164 next = to;
165 cd = __register_chrdev_region(MAJOR(n), MINOR(n),
166 next - n, name);
167 if (IS_ERR(cd))
168 goto fail;
170 return 0;
171 fail:
172 to = n;
173 for (n = from; n < to; n = next) {
174 next = MKDEV(MAJOR(n)+1, 0);
175 kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
177 return PTR_ERR(cd);
180 int alloc_chrdev_region(dev_t *dev, unsigned count, char *name)
182 struct char_device_struct *cd;
183 cd = __register_chrdev_region(0, 0, count, name);
184 if (IS_ERR(cd))
185 return PTR_ERR(cd);
186 *dev = MKDEV(cd->major, cd->baseminor);
187 return 0;
190 int register_chrdev(unsigned int major, const char *name,
191 struct file_operations *fops)
193 struct char_device_struct *cd;
194 struct cdev *cdev;
195 char *s;
196 int err = -ENOMEM;
198 cd = __register_chrdev_region(major, 0, 256, name);
199 if (IS_ERR(cd))
200 return PTR_ERR(cd);
202 cdev = cdev_alloc();
203 if (!cdev)
204 goto out2;
206 cdev->owner = fops->owner;
207 cdev->ops = fops;
208 strcpy(cdev->kobj.name, name);
209 for (s = strchr(cdev->kobj.name, '/'); s; s = strchr(s, '/'))
210 *s = '!';
212 err = cdev_add(cdev, MKDEV(cd->major, 0), 256);
213 if (err)
214 goto out;
216 cd->cdev = cdev;
218 return major ? 0 : cd->major;
219 out:
220 kobject_put(&cdev->kobj);
221 out2:
222 __unregister_chrdev_region(cd->major, 0, 256);
223 return err;
226 void unregister_chrdev_region(dev_t from, unsigned count)
228 dev_t to = from + count;
229 dev_t n, next;
231 for (n = from; n < to; n = next) {
232 next = MKDEV(MAJOR(n)+1, 0);
233 if (next > to)
234 next = to;
235 kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
239 int unregister_chrdev(unsigned int major, const char *name)
241 struct char_device_struct *cd;
242 cdev_unmap(MKDEV(major, 0), 256);
243 cd = __unregister_chrdev_region(major, 0, 256);
244 if (cd && cd->cdev)
245 cdev_del(cd->cdev);
246 kfree(cd);
247 return 0;
250 static spinlock_t cdev_lock = SPIN_LOCK_UNLOCKED;
252 * Called every time a character special file is opened
254 int chrdev_open(struct inode * inode, struct file * filp)
256 struct cdev *p;
257 struct cdev *new = NULL;
258 int ret = 0;
260 spin_lock(&cdev_lock);
261 p = inode->i_cdev;
262 if (!p) {
263 struct kobject *kobj;
264 int idx;
265 spin_unlock(&cdev_lock);
266 kobj = kobj_lookup(cdev_map, kdev_t_to_nr(inode->i_rdev), &idx);
267 if (!kobj)
268 return -ENODEV;
269 new = container_of(kobj, struct cdev, kobj);
270 spin_lock(&cdev_lock);
271 p = inode->i_cdev;
272 if (!p) {
273 inode->i_cdev = p = new;
274 inode->i_cindex = idx;
275 list_add(&inode->i_devices, &p->list);
276 new = NULL;
277 } else if (!cdev_get(p))
278 ret = -ENODEV;
279 } else if (!cdev_get(p))
280 ret = -ENODEV;
281 spin_unlock(&cdev_lock);
282 cdev_put(new);
283 if (ret)
284 return ret;
285 filp->f_op = fops_get(p->ops);
286 if (!filp->f_op) {
287 cdev_put(p);
288 return -ENODEV;
290 if (filp->f_op->open) {
291 lock_kernel();
292 ret = filp->f_op->open(inode,filp);
293 unlock_kernel();
295 if (ret)
296 cdev_put(p);
297 return ret;
300 void cd_forget(struct inode *inode)
302 spin_lock(&cdev_lock);
303 list_del_init(&inode->i_devices);
304 inode->i_cdev = NULL;
305 spin_unlock(&cdev_lock);
308 void cdev_purge(struct cdev *cdev)
310 spin_lock(&cdev_lock);
311 while (!list_empty(&cdev->list)) {
312 struct inode *inode;
313 inode = container_of(cdev->list.next, struct inode, i_devices);
314 list_del_init(&inode->i_devices);
315 inode->i_cdev = NULL;
317 spin_unlock(&cdev_lock);
321 * Dummy default file-operations: the only thing this does
322 * is contain the open that then fills in the correct operations
323 * depending on the special file...
325 struct file_operations def_chr_fops = {
326 .open = chrdev_open,
329 const char *cdevname(kdev_t dev)
331 static char buffer[40];
332 const char *name = "unknown-char";
333 unsigned int major = major(dev);
334 unsigned int minor = minor(dev);
335 int i = major_to_index(major);
336 struct char_device_struct *cd;
338 read_lock(&chrdevs_lock);
339 for (cd = chrdevs[i]; cd; cd = cd->next)
340 if (cd->major == major)
341 break;
342 if (cd)
343 name = cd->name;
344 sprintf(buffer, "%s(%d,%d)", name, major, minor);
345 read_unlock(&chrdevs_lock);
347 return buffer;
350 static struct kobject *exact_match(dev_t dev, int *part, void *data)
352 struct cdev *p = data;
353 return &p->kobj;
356 static int exact_lock(dev_t dev, void *data)
358 struct cdev *p = data;
359 return cdev_get(p) ? 0 : -1;
362 int cdev_add(struct cdev *p, dev_t dev, unsigned count)
364 int err = kobject_add(&p->kobj);
365 if (err)
366 return err;
367 return kobj_map(cdev_map, dev, count, NULL, exact_match, exact_lock, p);
370 void cdev_unmap(dev_t dev, unsigned count)
372 kobj_unmap(cdev_map, dev, count);
375 void cdev_del(struct cdev *p)
377 kobject_del(&p->kobj);
378 kobject_put(&p->kobj);
381 struct kobject *cdev_get(struct cdev *p)
383 struct module *owner = p->owner;
384 struct kobject *kobj;
386 if (owner && !try_module_get(owner))
387 return NULL;
388 kobj = kobject_get(&p->kobj);
389 if (!kobj)
390 module_put(owner);
391 return kobj;
394 void cdev_put(struct cdev *p)
396 if (p) {
397 kobject_put(&p->kobj);
398 module_put(p->owner);
402 static decl_subsys(cdev, NULL, NULL);
404 static void cdev_default_release(struct kobject *kobj)
406 struct cdev *p = container_of(kobj, struct cdev, kobj);
407 cdev_purge(p);
410 static void cdev_dynamic_release(struct kobject *kobj)
412 struct cdev *p = container_of(kobj, struct cdev, kobj);
413 cdev_purge(p);
414 kfree(p);
417 static struct kobj_type ktype_cdev_default = {
418 .release = cdev_default_release,
421 static struct kobj_type ktype_cdev_dynamic = {
422 .release = cdev_dynamic_release,
425 static struct kset kset_dynamic = {
426 .subsys = &cdev_subsys,
427 .kobj = {.name = "major",},
428 .ktype = &ktype_cdev_dynamic,
431 struct cdev *cdev_alloc(void)
433 struct cdev *p = kmalloc(sizeof(struct cdev), GFP_KERNEL);
434 if (p) {
435 memset(p, 0, sizeof(struct cdev));
436 p->kobj.kset = &kset_dynamic;
437 INIT_LIST_HEAD(&p->list);
438 kobject_init(&p->kobj);
440 return p;
443 void cdev_init(struct cdev *cdev, struct file_operations *fops)
445 INIT_LIST_HEAD(&cdev->list);
446 kobj_set_kset_s(cdev, cdev_subsys);
447 cdev->kobj.ktype = &ktype_cdev_default;
448 kobject_init(&cdev->kobj);
449 cdev->ops = fops;
452 static struct kobject *base_probe(dev_t dev, int *part, void *data)
454 char name[30];
455 sprintf(name, "char-major-%d", MAJOR(dev));
456 request_module(name);
457 return NULL;
460 void __init chrdev_init(void)
462 subsystem_register(&cdev_subsys);
463 kset_register(&kset_dynamic);
464 cdev_map = kobj_map_init(base_probe, &cdev_subsys);