blob: 33b95af89da4471482816ff97b701e7c5743761c [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
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/init.h>
8#include <linux/fs.h>
9#include <linux/slab.h>
10#include <linux/string.h>
11
12#include <linux/major.h>
13#include <linux/errno.h>
14#include <linux/module.h>
15#include <linux/smp_lock.h>
Joe Korty68eef3b2006-03-31 02:30:32 -080016#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18#include <linux/kobject.h>
19#include <linux/kobj_map.h>
20#include <linux/cdev.h>
Jes Sorensen58383af2006-02-06 14:12:43 -080021#include <linux/mutex.h>
David Howells5da61852006-09-27 01:50:16 -070022#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#ifdef CONFIG_KMOD
25#include <linux/kmod.h>
26#endif
27
David Howells5da61852006-09-27 01:50:16 -070028/*
29 * capabilities for /dev/mem, /dev/kmem and similar directly mappable character
30 * devices
31 * - permits shared-mmap for read, write and/or exec
32 * - does not permit private mmap in NOMMU mode (can't do COW)
33 * - no readahead or I/O queue unplugging required
34 */
35struct backing_dev_info directly_mappable_cdev_bdi = {
36 .capabilities = (
37#ifdef CONFIG_MMU
38 /* permit private copies of the data to be taken */
39 BDI_CAP_MAP_COPY |
40#endif
41 /* permit direct mmap, for read, write or exec */
42 BDI_CAP_MAP_DIRECT |
43 BDI_CAP_READ_MAP | BDI_CAP_WRITE_MAP | BDI_CAP_EXEC_MAP),
44};
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046static struct kobj_map *cdev_map;
47
Jes Sorensen58383af2006-02-06 14:12:43 -080048static DEFINE_MUTEX(chrdevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50static struct char_device_struct {
51 struct char_device_struct *next;
52 unsigned int major;
53 unsigned int baseminor;
54 int minorct;
Neil Horman7170be52006-01-14 13:20:38 -080055 char name[64];
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 struct file_operations *fops;
57 struct cdev *cdev; /* will die */
Joe Korty68eef3b2006-03-31 02:30:32 -080058} *chrdevs[CHRDEV_MAJOR_HASH_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60/* index in the above */
61static inline int major_to_index(int major)
62{
Joe Korty68eef3b2006-03-31 02:30:32 -080063 return major % CHRDEV_MAJOR_HASH_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064}
65
Joe Korty68eef3b2006-03-31 02:30:32 -080066#ifdef CONFIG_PROC_FS
Neil Horman7170be52006-01-14 13:20:38 -080067
Joe Korty68eef3b2006-03-31 02:30:32 -080068void chrdev_show(struct seq_file *f, off_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
70 struct char_device_struct *cd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Joe Korty68eef3b2006-03-31 02:30:32 -080072 if (offset < CHRDEV_MAJOR_HASH_SIZE) {
73 mutex_lock(&chrdevs_lock);
74 for (cd = chrdevs[offset]; cd; cd = cd->next)
75 seq_printf(f, "%3d %s\n", cd->major, cd->name);
76 mutex_unlock(&chrdevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 }
Neil Horman7170be52006-01-14 13:20:38 -080078}
79
Joe Korty68eef3b2006-03-31 02:30:32 -080080#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82/*
83 * Register a single major with a specified minor range.
84 *
85 * If major == 0 this functions will dynamically allocate a major and return
86 * its number.
87 *
88 * If major > 0 this function will attempt to reserve the passed range of
89 * minors and will return zero on success.
90 *
91 * Returns a -ve errno on failure.
92 */
93static struct char_device_struct *
94__register_chrdev_region(unsigned int major, unsigned int baseminor,
95 int minorct, const char *name)
96{
97 struct char_device_struct *cd, **cp;
98 int ret = 0;
99 int i;
100
Oliver Neukum11b0b5a2006-03-25 03:08:13 -0800101 cd = kzalloc(sizeof(struct char_device_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 if (cd == NULL)
103 return ERR_PTR(-ENOMEM);
104
Jes Sorensen58383af2006-02-06 14:12:43 -0800105 mutex_lock(&chrdevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 /* temporary */
108 if (major == 0) {
109 for (i = ARRAY_SIZE(chrdevs)-1; i > 0; i--) {
110 if (chrdevs[i] == NULL)
111 break;
112 }
113
114 if (i == 0) {
115 ret = -EBUSY;
116 goto out;
117 }
118 major = i;
119 ret = major;
120 }
121
122 cd->major = major;
123 cd->baseminor = baseminor;
124 cd->minorct = minorct;
Neil Horman7170be52006-01-14 13:20:38 -0800125 strncpy(cd->name,name, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 i = major_to_index(major);
128
129 for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next)
130 if ((*cp)->major > major ||
Amos Waterland01d553d2006-09-29 02:00:08 -0700131 ((*cp)->major == major &&
132 (((*cp)->baseminor >= baseminor) ||
133 ((*cp)->baseminor + (*cp)->minorct > baseminor))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 break;
Amos Waterland01d553d2006-09-29 02:00:08 -0700135
136 /* Check for overlapping minor ranges. */
137 if (*cp && (*cp)->major == major) {
138 int old_min = (*cp)->baseminor;
139 int old_max = (*cp)->baseminor + (*cp)->minorct - 1;
140 int new_min = baseminor;
141 int new_max = baseminor + minorct - 1;
142
143 /* New driver overlaps from the left. */
144 if (new_max >= old_min && new_max <= old_max) {
145 ret = -EBUSY;
146 goto out;
147 }
148
149 /* New driver overlaps from the right. */
150 if (new_min <= old_max && new_min >= old_min) {
151 ret = -EBUSY;
152 goto out;
153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 }
Amos Waterland01d553d2006-09-29 02:00:08 -0700155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 cd->next = *cp;
157 *cp = cd;
Jes Sorensen58383af2006-02-06 14:12:43 -0800158 mutex_unlock(&chrdevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 return cd;
160out:
Jes Sorensen58383af2006-02-06 14:12:43 -0800161 mutex_unlock(&chrdevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 kfree(cd);
163 return ERR_PTR(ret);
164}
165
166static struct char_device_struct *
167__unregister_chrdev_region(unsigned major, unsigned baseminor, int minorct)
168{
169 struct char_device_struct *cd = NULL, **cp;
170 int i = major_to_index(major);
171
Jes Sorensen58383af2006-02-06 14:12:43 -0800172 mutex_lock(&chrdevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next)
174 if ((*cp)->major == major &&
175 (*cp)->baseminor == baseminor &&
176 (*cp)->minorct == minorct)
177 break;
178 if (*cp) {
179 cd = *cp;
180 *cp = cd->next;
181 }
Jes Sorensen58383af2006-02-06 14:12:43 -0800182 mutex_unlock(&chrdevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 return cd;
184}
185
186int register_chrdev_region(dev_t from, unsigned count, const char *name)
187{
188 struct char_device_struct *cd;
189 dev_t to = from + count;
190 dev_t n, next;
191
192 for (n = from; n < to; n = next) {
193 next = MKDEV(MAJOR(n)+1, 0);
194 if (next > to)
195 next = to;
196 cd = __register_chrdev_region(MAJOR(n), MINOR(n),
197 next - n, name);
198 if (IS_ERR(cd))
199 goto fail;
200 }
201 return 0;
202fail:
203 to = n;
204 for (n = from; n < to; n = next) {
205 next = MKDEV(MAJOR(n)+1, 0);
206 kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
207 }
208 return PTR_ERR(cd);
209}
210
211int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count,
212 const char *name)
213{
214 struct char_device_struct *cd;
215 cd = __register_chrdev_region(0, baseminor, count, name);
216 if (IS_ERR(cd))
217 return PTR_ERR(cd);
218 *dev = MKDEV(cd->major, cd->baseminor);
219 return 0;
220}
221
Rolf Eike Beerd247e2c2006-07-14 00:24:23 -0700222/**
223 * register_chrdev() - Register a major number for character devices.
224 * @major: major device number or 0 for dynamic allocation
225 * @name: name of this range of devices
226 * @fops: file operations associated with this devices
227 *
228 * If @major == 0 this functions will dynamically allocate a major and return
229 * its number.
230 *
231 * If @major > 0 this function will attempt to reserve a device with the given
232 * major number and will return zero on success.
233 *
234 * Returns a -ve errno on failure.
235 *
236 * The name of this device has nothing to do with the name of the device in
237 * /dev. It only helps to keep track of the different owners of devices. If
238 * your module name has only one type of devices it's ok to use e.g. the name
239 * of the module here.
240 *
241 * This function registers a range of 256 minor numbers. The first minor number
242 * is 0.
243 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244int register_chrdev(unsigned int major, const char *name,
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800245 const struct file_operations *fops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
247 struct char_device_struct *cd;
248 struct cdev *cdev;
249 char *s;
250 int err = -ENOMEM;
251
252 cd = __register_chrdev_region(major, 0, 256, name);
253 if (IS_ERR(cd))
254 return PTR_ERR(cd);
255
256 cdev = cdev_alloc();
257 if (!cdev)
258 goto out2;
259
260 cdev->owner = fops->owner;
261 cdev->ops = fops;
262 kobject_set_name(&cdev->kobj, "%s", name);
263 for (s = strchr(kobject_name(&cdev->kobj),'/'); s; s = strchr(s, '/'))
264 *s = '!';
265
266 err = cdev_add(cdev, MKDEV(cd->major, 0), 256);
267 if (err)
268 goto out;
269
270 cd->cdev = cdev;
271
272 return major ? 0 : cd->major;
273out:
274 kobject_put(&cdev->kobj);
275out2:
276 kfree(__unregister_chrdev_region(cd->major, 0, 256));
277 return err;
278}
279
280void unregister_chrdev_region(dev_t from, unsigned count)
281{
282 dev_t to = from + count;
283 dev_t n, next;
284
285 for (n = from; n < to; n = next) {
286 next = MKDEV(MAJOR(n)+1, 0);
287 if (next > to)
288 next = to;
289 kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
290 }
291}
292
293int unregister_chrdev(unsigned int major, const char *name)
294{
295 struct char_device_struct *cd;
296 cd = __unregister_chrdev_region(major, 0, 256);
297 if (cd && cd->cdev)
298 cdev_del(cd->cdev);
299 kfree(cd);
300 return 0;
301}
302
303static DEFINE_SPINLOCK(cdev_lock);
304
305static struct kobject *cdev_get(struct cdev *p)
306{
307 struct module *owner = p->owner;
308 struct kobject *kobj;
309
310 if (owner && !try_module_get(owner))
311 return NULL;
312 kobj = kobject_get(&p->kobj);
313 if (!kobj)
314 module_put(owner);
315 return kobj;
316}
317
318void cdev_put(struct cdev *p)
319{
320 if (p) {
Brian King7da68442005-07-12 13:58:30 -0700321 struct module *owner = p->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 kobject_put(&p->kobj);
Brian King7da68442005-07-12 13:58:30 -0700323 module_put(owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
325}
326
327/*
328 * Called every time a character special file is opened
329 */
330int chrdev_open(struct inode * inode, struct file * filp)
331{
332 struct cdev *p;
333 struct cdev *new = NULL;
334 int ret = 0;
335
336 spin_lock(&cdev_lock);
337 p = inode->i_cdev;
338 if (!p) {
339 struct kobject *kobj;
340 int idx;
341 spin_unlock(&cdev_lock);
342 kobj = kobj_lookup(cdev_map, inode->i_rdev, &idx);
343 if (!kobj)
344 return -ENXIO;
345 new = container_of(kobj, struct cdev, kobj);
346 spin_lock(&cdev_lock);
347 p = inode->i_cdev;
348 if (!p) {
349 inode->i_cdev = p = new;
350 inode->i_cindex = idx;
351 list_add(&inode->i_devices, &p->list);
352 new = NULL;
353 } else if (!cdev_get(p))
354 ret = -ENXIO;
355 } else if (!cdev_get(p))
356 ret = -ENXIO;
357 spin_unlock(&cdev_lock);
358 cdev_put(new);
359 if (ret)
360 return ret;
361 filp->f_op = fops_get(p->ops);
362 if (!filp->f_op) {
363 cdev_put(p);
364 return -ENXIO;
365 }
366 if (filp->f_op->open) {
367 lock_kernel();
368 ret = filp->f_op->open(inode,filp);
369 unlock_kernel();
370 }
371 if (ret)
372 cdev_put(p);
373 return ret;
374}
375
376void cd_forget(struct inode *inode)
377{
378 spin_lock(&cdev_lock);
379 list_del_init(&inode->i_devices);
380 inode->i_cdev = NULL;
381 spin_unlock(&cdev_lock);
382}
383
Adrian Bunk75c96f82005-05-05 16:16:09 -0700384static void cdev_purge(struct cdev *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
386 spin_lock(&cdev_lock);
387 while (!list_empty(&cdev->list)) {
388 struct inode *inode;
389 inode = container_of(cdev->list.next, struct inode, i_devices);
390 list_del_init(&inode->i_devices);
391 inode->i_cdev = NULL;
392 }
393 spin_unlock(&cdev_lock);
394}
395
396/*
397 * Dummy default file-operations: the only thing this does
398 * is contain the open that then fills in the correct operations
399 * depending on the special file...
400 */
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800401const struct file_operations def_chr_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 .open = chrdev_open,
403};
404
405static struct kobject *exact_match(dev_t dev, int *part, void *data)
406{
407 struct cdev *p = data;
408 return &p->kobj;
409}
410
411static int exact_lock(dev_t dev, void *data)
412{
413 struct cdev *p = data;
414 return cdev_get(p) ? 0 : -1;
415}
416
417int cdev_add(struct cdev *p, dev_t dev, unsigned count)
418{
419 p->dev = dev;
420 p->count = count;
421 return kobj_map(cdev_map, dev, count, NULL, exact_match, exact_lock, p);
422}
423
424static void cdev_unmap(dev_t dev, unsigned count)
425{
426 kobj_unmap(cdev_map, dev, count);
427}
428
429void cdev_del(struct cdev *p)
430{
431 cdev_unmap(p->dev, p->count);
432 kobject_put(&p->kobj);
433}
434
435
436static void cdev_default_release(struct kobject *kobj)
437{
438 struct cdev *p = container_of(kobj, struct cdev, kobj);
439 cdev_purge(p);
440}
441
442static void cdev_dynamic_release(struct kobject *kobj)
443{
444 struct cdev *p = container_of(kobj, struct cdev, kobj);
445 cdev_purge(p);
446 kfree(p);
447}
448
449static struct kobj_type ktype_cdev_default = {
450 .release = cdev_default_release,
451};
452
453static struct kobj_type ktype_cdev_dynamic = {
454 .release = cdev_dynamic_release,
455};
456
457struct cdev *cdev_alloc(void)
458{
Oliver Neukum11b0b5a2006-03-25 03:08:13 -0800459 struct cdev *p = kzalloc(sizeof(struct cdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 p->kobj.ktype = &ktype_cdev_dynamic;
462 INIT_LIST_HEAD(&p->list);
463 kobject_init(&p->kobj);
464 }
465 return p;
466}
467
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800468void cdev_init(struct cdev *cdev, const struct file_operations *fops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
470 memset(cdev, 0, sizeof *cdev);
471 INIT_LIST_HEAD(&cdev->list);
472 cdev->kobj.ktype = &ktype_cdev_default;
473 kobject_init(&cdev->kobj);
474 cdev->ops = fops;
475}
476
477static struct kobject *base_probe(dev_t dev, int *part, void *data)
478{
479 if (request_module("char-major-%d-%d", MAJOR(dev), MINOR(dev)) > 0)
480 /* Make old-style 2.4 aliases work */
481 request_module("char-major-%d", MAJOR(dev));
482 return NULL;
483}
484
485void __init chrdev_init(void)
486{
487 cdev_map = kobj_map_init(base_probe, &chrdevs_lock);
488}
489
490
491/* Let modules do char dev stuff */
492EXPORT_SYMBOL(register_chrdev_region);
493EXPORT_SYMBOL(unregister_chrdev_region);
494EXPORT_SYMBOL(alloc_chrdev_region);
495EXPORT_SYMBOL(cdev_init);
496EXPORT_SYMBOL(cdev_alloc);
497EXPORT_SYMBOL(cdev_del);
498EXPORT_SYMBOL(cdev_add);
499EXPORT_SYMBOL(register_chrdev);
500EXPORT_SYMBOL(unregister_chrdev);
David Howells5da61852006-09-27 01:50:16 -0700501EXPORT_SYMBOL(directly_mappable_cdev_bdi);