blob: 8c6eb04d31e2ab9033a067fc42393d5fa8c38a60 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/char_dev.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
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>
12
13#include <linux/major.h>
14#include <linux/errno.h>
15#include <linux/module.h>
16#include <linux/smp_lock.h>
17#include <linux/devfs_fs_kernel.h>
18
19#include <linux/kobject.h>
20#include <linux/kobj_map.h>
21#include <linux/cdev.h>
Jes Sorensen58383af2006-02-06 14:12:43 -080022#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#ifdef CONFIG_KMOD
25#include <linux/kmod.h>
26#endif
27
28static struct kobj_map *cdev_map;
29
David Brownell1d4d5b32005-04-17 10:57:20 -070030#define MAX_PROBE_HASH 255 /* random */
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Jes Sorensen58383af2006-02-06 14:12:43 -080032static DEFINE_MUTEX(chrdevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34static struct char_device_struct {
35 struct char_device_struct *next;
36 unsigned int major;
37 unsigned int baseminor;
38 int minorct;
Neil Horman7170be52006-01-14 13:20:38 -080039 char name[64];
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 struct file_operations *fops;
41 struct cdev *cdev; /* will die */
42} *chrdevs[MAX_PROBE_HASH];
43
44/* index in the above */
45static inline int major_to_index(int major)
46{
47 return major % MAX_PROBE_HASH;
48}
49
Neil Horman7170be52006-01-14 13:20:38 -080050struct chrdev_info {
51 int index;
52 struct char_device_struct *cd;
53};
54
55void *get_next_chrdev(void *dev)
56{
57 struct chrdev_info *info;
58
59 if (dev == NULL) {
60 info = kmalloc(sizeof(*info), GFP_KERNEL);
61 if (!info)
62 goto out;
63 info->index=0;
64 info->cd = chrdevs[info->index];
65 if (info->cd)
66 goto out;
67 } else {
68 info = dev;
69 }
70
71 while (info->index < ARRAY_SIZE(chrdevs)) {
72 if (info->cd)
73 info->cd = info->cd->next;
74 if (info->cd)
75 goto out;
76 /*
77 * No devices on this chain, move to the next
78 */
79 info->index++;
80 info->cd = (info->index < ARRAY_SIZE(chrdevs)) ?
81 chrdevs[info->index] : NULL;
82 if (info->cd)
83 goto out;
84 }
85
86out:
87 return info;
88}
89
90void *acquire_chrdev_list(void)
91{
Jes Sorensen58383af2006-02-06 14:12:43 -080092 mutex_lock(&chrdevs_lock);
Neil Horman7170be52006-01-14 13:20:38 -080093 return get_next_chrdev(NULL);
94}
95
96void release_chrdev_list(void *dev)
97{
Jes Sorensen58383af2006-02-06 14:12:43 -080098 mutex_unlock(&chrdevs_lock);
Neil Horman7170be52006-01-14 13:20:38 -080099 kfree(dev);
100}
101
102
103int count_chrdev_list(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
105 struct char_device_struct *cd;
Neil Horman7170be52006-01-14 13:20:38 -0800106 int i, count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Neil Horman7170be52006-01-14 13:20:38 -0800108 count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 for (i = 0; i < ARRAY_SIZE(chrdevs) ; i++) {
Neil Horman7170be52006-01-14 13:20:38 -0800111 for (cd = chrdevs[i]; cd; cd = cd->next)
112 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Neil Horman7170be52006-01-14 13:20:38 -0800115 return count;
116}
117
118int get_chrdev_info(void *dev, int *major, char **name)
119{
120 struct chrdev_info *info = dev;
121
122 if (info->cd == NULL)
123 return 1;
124
125 *major = info->cd->major;
126 *name = info->cd->name;
127 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
130/*
131 * Register a single major with a specified minor range.
132 *
133 * If major == 0 this functions will dynamically allocate a major and return
134 * its number.
135 *
136 * If major > 0 this function will attempt to reserve the passed range of
137 * minors and will return zero on success.
138 *
139 * Returns a -ve errno on failure.
140 */
141static struct char_device_struct *
142__register_chrdev_region(unsigned int major, unsigned int baseminor,
143 int minorct, const char *name)
144{
145 struct char_device_struct *cd, **cp;
146 int ret = 0;
147 int i;
148
Oliver Neukum11b0b5a2006-03-25 03:08:13 -0800149 cd = kzalloc(sizeof(struct char_device_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 if (cd == NULL)
151 return ERR_PTR(-ENOMEM);
152
Jes Sorensen58383af2006-02-06 14:12:43 -0800153 mutex_lock(&chrdevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 /* temporary */
156 if (major == 0) {
157 for (i = ARRAY_SIZE(chrdevs)-1; i > 0; i--) {
158 if (chrdevs[i] == NULL)
159 break;
160 }
161
162 if (i == 0) {
163 ret = -EBUSY;
164 goto out;
165 }
166 major = i;
167 ret = major;
168 }
169
170 cd->major = major;
171 cd->baseminor = baseminor;
172 cd->minorct = minorct;
Neil Horman7170be52006-01-14 13:20:38 -0800173 strncpy(cd->name,name, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 i = major_to_index(major);
176
177 for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next)
178 if ((*cp)->major > major ||
179 ((*cp)->major == major && (*cp)->baseminor >= baseminor))
180 break;
181 if (*cp && (*cp)->major == major &&
182 (*cp)->baseminor < baseminor + minorct) {
183 ret = -EBUSY;
184 goto out;
185 }
186 cd->next = *cp;
187 *cp = cd;
Jes Sorensen58383af2006-02-06 14:12:43 -0800188 mutex_unlock(&chrdevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return cd;
190out:
Jes Sorensen58383af2006-02-06 14:12:43 -0800191 mutex_unlock(&chrdevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 kfree(cd);
193 return ERR_PTR(ret);
194}
195
196static struct char_device_struct *
197__unregister_chrdev_region(unsigned major, unsigned baseminor, int minorct)
198{
199 struct char_device_struct *cd = NULL, **cp;
200 int i = major_to_index(major);
201
Jes Sorensen58383af2006-02-06 14:12:43 -0800202 mutex_lock(&chrdevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next)
204 if ((*cp)->major == major &&
205 (*cp)->baseminor == baseminor &&
206 (*cp)->minorct == minorct)
207 break;
208 if (*cp) {
209 cd = *cp;
210 *cp = cd->next;
211 }
Jes Sorensen58383af2006-02-06 14:12:43 -0800212 mutex_unlock(&chrdevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 return cd;
214}
215
216int register_chrdev_region(dev_t from, unsigned count, const char *name)
217{
218 struct char_device_struct *cd;
219 dev_t to = from + count;
220 dev_t n, next;
221
222 for (n = from; n < to; n = next) {
223 next = MKDEV(MAJOR(n)+1, 0);
224 if (next > to)
225 next = to;
226 cd = __register_chrdev_region(MAJOR(n), MINOR(n),
227 next - n, name);
228 if (IS_ERR(cd))
229 goto fail;
230 }
231 return 0;
232fail:
233 to = n;
234 for (n = from; n < to; n = next) {
235 next = MKDEV(MAJOR(n)+1, 0);
236 kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
237 }
238 return PTR_ERR(cd);
239}
240
241int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count,
242 const char *name)
243{
244 struct char_device_struct *cd;
245 cd = __register_chrdev_region(0, baseminor, count, name);
246 if (IS_ERR(cd))
247 return PTR_ERR(cd);
248 *dev = MKDEV(cd->major, cd->baseminor);
249 return 0;
250}
251
252int register_chrdev(unsigned int major, const char *name,
253 struct file_operations *fops)
254{
255 struct char_device_struct *cd;
256 struct cdev *cdev;
257 char *s;
258 int err = -ENOMEM;
259
260 cd = __register_chrdev_region(major, 0, 256, name);
261 if (IS_ERR(cd))
262 return PTR_ERR(cd);
263
264 cdev = cdev_alloc();
265 if (!cdev)
266 goto out2;
267
268 cdev->owner = fops->owner;
269 cdev->ops = fops;
270 kobject_set_name(&cdev->kobj, "%s", name);
271 for (s = strchr(kobject_name(&cdev->kobj),'/'); s; s = strchr(s, '/'))
272 *s = '!';
273
274 err = cdev_add(cdev, MKDEV(cd->major, 0), 256);
275 if (err)
276 goto out;
277
278 cd->cdev = cdev;
279
280 return major ? 0 : cd->major;
281out:
282 kobject_put(&cdev->kobj);
283out2:
284 kfree(__unregister_chrdev_region(cd->major, 0, 256));
285 return err;
286}
287
288void unregister_chrdev_region(dev_t from, unsigned count)
289{
290 dev_t to = from + count;
291 dev_t n, next;
292
293 for (n = from; n < to; n = next) {
294 next = MKDEV(MAJOR(n)+1, 0);
295 if (next > to)
296 next = to;
297 kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
298 }
299}
300
301int unregister_chrdev(unsigned int major, const char *name)
302{
303 struct char_device_struct *cd;
304 cd = __unregister_chrdev_region(major, 0, 256);
305 if (cd && cd->cdev)
306 cdev_del(cd->cdev);
307 kfree(cd);
308 return 0;
309}
310
311static DEFINE_SPINLOCK(cdev_lock);
312
313static struct kobject *cdev_get(struct cdev *p)
314{
315 struct module *owner = p->owner;
316 struct kobject *kobj;
317
318 if (owner && !try_module_get(owner))
319 return NULL;
320 kobj = kobject_get(&p->kobj);
321 if (!kobj)
322 module_put(owner);
323 return kobj;
324}
325
326void cdev_put(struct cdev *p)
327{
328 if (p) {
Brian King7da68442005-07-12 13:58:30 -0700329 struct module *owner = p->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 kobject_put(&p->kobj);
Brian King7da68442005-07-12 13:58:30 -0700331 module_put(owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 }
333}
334
335/*
336 * Called every time a character special file is opened
337 */
338int chrdev_open(struct inode * inode, struct file * filp)
339{
340 struct cdev *p;
341 struct cdev *new = NULL;
342 int ret = 0;
343
344 spin_lock(&cdev_lock);
345 p = inode->i_cdev;
346 if (!p) {
347 struct kobject *kobj;
348 int idx;
349 spin_unlock(&cdev_lock);
350 kobj = kobj_lookup(cdev_map, inode->i_rdev, &idx);
351 if (!kobj)
352 return -ENXIO;
353 new = container_of(kobj, struct cdev, kobj);
354 spin_lock(&cdev_lock);
355 p = inode->i_cdev;
356 if (!p) {
357 inode->i_cdev = p = new;
358 inode->i_cindex = idx;
359 list_add(&inode->i_devices, &p->list);
360 new = NULL;
361 } else if (!cdev_get(p))
362 ret = -ENXIO;
363 } else if (!cdev_get(p))
364 ret = -ENXIO;
365 spin_unlock(&cdev_lock);
366 cdev_put(new);
367 if (ret)
368 return ret;
369 filp->f_op = fops_get(p->ops);
370 if (!filp->f_op) {
371 cdev_put(p);
372 return -ENXIO;
373 }
374 if (filp->f_op->open) {
375 lock_kernel();
376 ret = filp->f_op->open(inode,filp);
377 unlock_kernel();
378 }
379 if (ret)
380 cdev_put(p);
381 return ret;
382}
383
384void cd_forget(struct inode *inode)
385{
386 spin_lock(&cdev_lock);
387 list_del_init(&inode->i_devices);
388 inode->i_cdev = NULL;
389 spin_unlock(&cdev_lock);
390}
391
Adrian Bunk75c96f82005-05-05 16:16:09 -0700392static void cdev_purge(struct cdev *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
394 spin_lock(&cdev_lock);
395 while (!list_empty(&cdev->list)) {
396 struct inode *inode;
397 inode = container_of(cdev->list.next, struct inode, i_devices);
398 list_del_init(&inode->i_devices);
399 inode->i_cdev = NULL;
400 }
401 spin_unlock(&cdev_lock);
402}
403
404/*
405 * Dummy default file-operations: the only thing this does
406 * is contain the open that then fills in the correct operations
407 * depending on the special file...
408 */
409struct file_operations def_chr_fops = {
410 .open = chrdev_open,
411};
412
413static struct kobject *exact_match(dev_t dev, int *part, void *data)
414{
415 struct cdev *p = data;
416 return &p->kobj;
417}
418
419static int exact_lock(dev_t dev, void *data)
420{
421 struct cdev *p = data;
422 return cdev_get(p) ? 0 : -1;
423}
424
425int cdev_add(struct cdev *p, dev_t dev, unsigned count)
426{
427 p->dev = dev;
428 p->count = count;
429 return kobj_map(cdev_map, dev, count, NULL, exact_match, exact_lock, p);
430}
431
432static void cdev_unmap(dev_t dev, unsigned count)
433{
434 kobj_unmap(cdev_map, dev, count);
435}
436
437void cdev_del(struct cdev *p)
438{
439 cdev_unmap(p->dev, p->count);
440 kobject_put(&p->kobj);
441}
442
443
444static void cdev_default_release(struct kobject *kobj)
445{
446 struct cdev *p = container_of(kobj, struct cdev, kobj);
447 cdev_purge(p);
448}
449
450static void cdev_dynamic_release(struct kobject *kobj)
451{
452 struct cdev *p = container_of(kobj, struct cdev, kobj);
453 cdev_purge(p);
454 kfree(p);
455}
456
457static struct kobj_type ktype_cdev_default = {
458 .release = cdev_default_release,
459};
460
461static struct kobj_type ktype_cdev_dynamic = {
462 .release = cdev_dynamic_release,
463};
464
465struct cdev *cdev_alloc(void)
466{
Oliver Neukum11b0b5a2006-03-25 03:08:13 -0800467 struct cdev *p = kzalloc(sizeof(struct cdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 p->kobj.ktype = &ktype_cdev_dynamic;
470 INIT_LIST_HEAD(&p->list);
471 kobject_init(&p->kobj);
472 }
473 return p;
474}
475
476void cdev_init(struct cdev *cdev, struct file_operations *fops)
477{
478 memset(cdev, 0, sizeof *cdev);
479 INIT_LIST_HEAD(&cdev->list);
480 cdev->kobj.ktype = &ktype_cdev_default;
481 kobject_init(&cdev->kobj);
482 cdev->ops = fops;
483}
484
485static struct kobject *base_probe(dev_t dev, int *part, void *data)
486{
487 if (request_module("char-major-%d-%d", MAJOR(dev), MINOR(dev)) > 0)
488 /* Make old-style 2.4 aliases work */
489 request_module("char-major-%d", MAJOR(dev));
490 return NULL;
491}
492
493void __init chrdev_init(void)
494{
495 cdev_map = kobj_map_init(base_probe, &chrdevs_lock);
496}
497
498
499/* Let modules do char dev stuff */
500EXPORT_SYMBOL(register_chrdev_region);
501EXPORT_SYMBOL(unregister_chrdev_region);
502EXPORT_SYMBOL(alloc_chrdev_region);
503EXPORT_SYMBOL(cdev_init);
504EXPORT_SYMBOL(cdev_alloc);
505EXPORT_SYMBOL(cdev_del);
506EXPORT_SYMBOL(cdev_add);
507EXPORT_SYMBOL(register_chrdev);
508EXPORT_SYMBOL(unregister_chrdev);