]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/char_dev.c
char_dev: extend dynamic allocation of majors into a higher range
[mirror_ubuntu-bionic-kernel.git] / fs / char_dev.c
CommitLineData
1da177e4
LT
1/*
2 * linux/fs/char_dev.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
1da177e4
LT
7#include <linux/init.h>
8#include <linux/fs.h>
b446b60e 9#include <linux/kdev_t.h>
1da177e4
LT
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>
68eef3b4 16#include <linux/seq_file.h>
1da177e4
LT
17
18#include <linux/kobject.h>
19#include <linux/kobj_map.h>
20#include <linux/cdev.h>
58383af6 21#include <linux/mutex.h>
5da6185b 22#include <linux/backing-dev.h>
31d1d48e 23#include <linux/tty.h>
1da177e4 24
07f3f05c 25#include "internal.h"
1da177e4
LT
26
27static struct kobj_map *cdev_map;
28
58383af6 29static DEFINE_MUTEX(chrdevs_lock);
1da177e4
LT
30
31static struct char_device_struct {
32 struct char_device_struct *next;
33 unsigned int major;
34 unsigned int baseminor;
35 int minorct;
7170be5f 36 char name[64];
1da177e4 37 struct cdev *cdev; /* will die */
68eef3b4 38} *chrdevs[CHRDEV_MAJOR_HASH_SIZE];
1da177e4
LT
39
40/* index in the above */
e61eb2e9 41static inline int major_to_index(unsigned major)
1da177e4 42{
68eef3b4 43 return major % CHRDEV_MAJOR_HASH_SIZE;
7170be5f
NH
44}
45
68eef3b4 46#ifdef CONFIG_PROC_FS
7170be5f 47
68eef3b4 48void chrdev_show(struct seq_file *f, off_t offset)
7170be5f
NH
49{
50 struct char_device_struct *cd;
7170be5f 51
68eef3b4
JK
52 if (offset < CHRDEV_MAJOR_HASH_SIZE) {
53 mutex_lock(&chrdevs_lock);
54 for (cd = chrdevs[offset]; cd; cd = cd->next)
55 seq_printf(f, "%3d %s\n", cd->major, cd->name);
56 mutex_unlock(&chrdevs_lock);
1da177e4 57 }
7170be5f
NH
58}
59
68eef3b4 60#endif /* CONFIG_PROC_FS */
1da177e4 61
a5d31a3f
LG
62static int find_dynamic_major(void)
63{
64 int i;
65 struct char_device_struct *cd;
66
67 for (i = ARRAY_SIZE(chrdevs)-1; i > CHRDEV_MAJOR_DYN_END; i--) {
68 if (chrdevs[i] == NULL)
69 return i;
70 }
71
72 for (i = CHRDEV_MAJOR_DYN_EXT_START;
73 i > CHRDEV_MAJOR_DYN_EXT_END; i--) {
74 for (cd = chrdevs[major_to_index(i)]; cd; cd = cd->next)
75 if (cd->major == i)
76 break;
77
78 if (cd == NULL || cd->major != i)
79 return i;
80 }
81
82 return -EBUSY;
83}
84
1da177e4
LT
85/*
86 * Register a single major with a specified minor range.
87 *
88 * If major == 0 this functions will dynamically allocate a major and return
89 * its number.
90 *
91 * If major > 0 this function will attempt to reserve the passed range of
92 * minors and will return zero on success.
93 *
94 * Returns a -ve errno on failure.
95 */
96static struct char_device_struct *
97__register_chrdev_region(unsigned int major, unsigned int baseminor,
98 int minorct, const char *name)
99{
100 struct char_device_struct *cd, **cp;
101 int ret = 0;
102 int i;
103
11b0b5ab 104 cd = kzalloc(sizeof(struct char_device_struct), GFP_KERNEL);
1da177e4
LT
105 if (cd == NULL)
106 return ERR_PTR(-ENOMEM);
107
58383af6 108 mutex_lock(&chrdevs_lock);
1da177e4 109
1da177e4 110 if (major == 0) {
a5d31a3f
LG
111 ret = find_dynamic_major();
112 if (ret < 0) {
113 pr_err("CHRDEV \"%s\" dynamic allocation region is full\n",
114 name);
1da177e4
LT
115 goto out;
116 }
a5d31a3f 117 major = ret;
1da177e4
LT
118 }
119
120 cd->major = major;
121 cd->baseminor = baseminor;
122 cd->minorct = minorct;
8c401888 123 strlcpy(cd->name, name, sizeof(cd->name));
1da177e4
LT
124
125 i = major_to_index(major);
126
127 for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next)
128 if ((*cp)->major > major ||
01d553d0
AW
129 ((*cp)->major == major &&
130 (((*cp)->baseminor >= baseminor) ||
131 ((*cp)->baseminor + (*cp)->minorct > baseminor))))
1da177e4 132 break;
01d553d0
AW
133
134 /* Check for overlapping minor ranges. */
135 if (*cp && (*cp)->major == major) {
136 int old_min = (*cp)->baseminor;
137 int old_max = (*cp)->baseminor + (*cp)->minorct - 1;
138 int new_min = baseminor;
139 int new_max = baseminor + minorct - 1;
140
141 /* New driver overlaps from the left. */
142 if (new_max >= old_min && new_max <= old_max) {
143 ret = -EBUSY;
144 goto out;
145 }
146
147 /* New driver overlaps from the right. */
148 if (new_min <= old_max && new_min >= old_min) {
149 ret = -EBUSY;
150 goto out;
151 }
1da177e4 152 }
01d553d0 153
1da177e4
LT
154 cd->next = *cp;
155 *cp = cd;
58383af6 156 mutex_unlock(&chrdevs_lock);
1da177e4
LT
157 return cd;
158out:
58383af6 159 mutex_unlock(&chrdevs_lock);
1da177e4
LT
160 kfree(cd);
161 return ERR_PTR(ret);
162}
163
164static struct char_device_struct *
165__unregister_chrdev_region(unsigned major, unsigned baseminor, int minorct)
166{
167 struct char_device_struct *cd = NULL, **cp;
168 int i = major_to_index(major);
169
58383af6 170 mutex_lock(&chrdevs_lock);
1da177e4
LT
171 for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next)
172 if ((*cp)->major == major &&
173 (*cp)->baseminor == baseminor &&
174 (*cp)->minorct == minorct)
175 break;
176 if (*cp) {
177 cd = *cp;
178 *cp = cd->next;
179 }
58383af6 180 mutex_unlock(&chrdevs_lock);
1da177e4
LT
181 return cd;
182}
183
cf3e43db
JC
184/**
185 * register_chrdev_region() - register a range of device numbers
186 * @from: the first in the desired range of device numbers; must include
187 * the major number.
188 * @count: the number of consecutive device numbers required
189 * @name: the name of the device or driver.
190 *
191 * Return value is zero on success, a negative error code on failure.
192 */
1da177e4
LT
193int register_chrdev_region(dev_t from, unsigned count, const char *name)
194{
195 struct char_device_struct *cd;
196 dev_t to = from + count;
197 dev_t n, next;
198
199 for (n = from; n < to; n = next) {
200 next = MKDEV(MAJOR(n)+1, 0);
201 if (next > to)
202 next = to;
203 cd = __register_chrdev_region(MAJOR(n), MINOR(n),
204 next - n, name);
205 if (IS_ERR(cd))
206 goto fail;
207 }
208 return 0;
209fail:
210 to = n;
211 for (n = from; n < to; n = next) {
212 next = MKDEV(MAJOR(n)+1, 0);
213 kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
214 }
215 return PTR_ERR(cd);
216}
217
cf3e43db
JC
218/**
219 * alloc_chrdev_region() - register a range of char device numbers
220 * @dev: output parameter for first assigned number
221 * @baseminor: first of the requested range of minor numbers
222 * @count: the number of minor numbers required
223 * @name: the name of the associated device or driver
224 *
225 * Allocates a range of char device numbers. The major number will be
226 * chosen dynamically, and returned (along with the first minor number)
227 * in @dev. Returns zero or a negative error code.
228 */
1da177e4
LT
229int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count,
230 const char *name)
231{
232 struct char_device_struct *cd;
233 cd = __register_chrdev_region(0, baseminor, count, name);
234 if (IS_ERR(cd))
235 return PTR_ERR(cd);
236 *dev = MKDEV(cd->major, cd->baseminor);
237 return 0;
238}
239
d247e2c6 240/**
1905b1bf 241 * __register_chrdev() - create and register a cdev occupying a range of minors
d247e2c6 242 * @major: major device number or 0 for dynamic allocation
1905b1bf
TH
243 * @baseminor: first of the requested range of minor numbers
244 * @count: the number of minor numbers required
d247e2c6
REB
245 * @name: name of this range of devices
246 * @fops: file operations associated with this devices
247 *
248 * If @major == 0 this functions will dynamically allocate a major and return
249 * its number.
250 *
251 * If @major > 0 this function will attempt to reserve a device with the given
252 * major number and will return zero on success.
253 *
254 * Returns a -ve errno on failure.
255 *
256 * The name of this device has nothing to do with the name of the device in
257 * /dev. It only helps to keep track of the different owners of devices. If
258 * your module name has only one type of devices it's ok to use e.g. the name
259 * of the module here.
d247e2c6 260 */
1905b1bf
TH
261int __register_chrdev(unsigned int major, unsigned int baseminor,
262 unsigned int count, const char *name,
263 const struct file_operations *fops)
1da177e4
LT
264{
265 struct char_device_struct *cd;
266 struct cdev *cdev;
1da177e4
LT
267 int err = -ENOMEM;
268
1905b1bf 269 cd = __register_chrdev_region(major, baseminor, count, name);
1da177e4
LT
270 if (IS_ERR(cd))
271 return PTR_ERR(cd);
1ff97647 272
1da177e4
LT
273 cdev = cdev_alloc();
274 if (!cdev)
275 goto out2;
276
277 cdev->owner = fops->owner;
278 cdev->ops = fops;
279 kobject_set_name(&cdev->kobj, "%s", name);
1ff97647 280
1905b1bf 281 err = cdev_add(cdev, MKDEV(cd->major, baseminor), count);
1da177e4
LT
282 if (err)
283 goto out;
284
285 cd->cdev = cdev;
286
287 return major ? 0 : cd->major;
288out:
289 kobject_put(&cdev->kobj);
290out2:
1905b1bf 291 kfree(__unregister_chrdev_region(cd->major, baseminor, count));
1da177e4
LT
292 return err;
293}
294
cf3e43db 295/**
594069bc 296 * unregister_chrdev_region() - unregister a range of device numbers
cf3e43db
JC
297 * @from: the first in the range of numbers to unregister
298 * @count: the number of device numbers to unregister
299 *
300 * This function will unregister a range of @count device numbers,
301 * starting with @from. The caller should normally be the one who
302 * allocated those numbers in the first place...
303 */
1da177e4
LT
304void unregister_chrdev_region(dev_t from, unsigned count)
305{
306 dev_t to = from + count;
307 dev_t n, next;
308
309 for (n = from; n < to; n = next) {
310 next = MKDEV(MAJOR(n)+1, 0);
311 if (next > to)
312 next = to;
313 kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
314 }
315}
316
1905b1bf
TH
317/**
318 * __unregister_chrdev - unregister and destroy a cdev
319 * @major: major device number
320 * @baseminor: first of the range of minor numbers
321 * @count: the number of minor numbers this cdev is occupying
322 * @name: name of this range of devices
323 *
324 * Unregister and destroy the cdev occupying the region described by
325 * @major, @baseminor and @count. This function undoes what
326 * __register_chrdev() did.
327 */
328void __unregister_chrdev(unsigned int major, unsigned int baseminor,
329 unsigned int count, const char *name)
1da177e4
LT
330{
331 struct char_device_struct *cd;
1905b1bf
TH
332
333 cd = __unregister_chrdev_region(major, baseminor, count);
1da177e4
LT
334 if (cd && cd->cdev)
335 cdev_del(cd->cdev);
336 kfree(cd);
1da177e4
LT
337}
338
339static DEFINE_SPINLOCK(cdev_lock);
340
341static struct kobject *cdev_get(struct cdev *p)
342{
343 struct module *owner = p->owner;
344 struct kobject *kobj;
345
346 if (owner && !try_module_get(owner))
347 return NULL;
348 kobj = kobject_get(&p->kobj);
349 if (!kobj)
350 module_put(owner);
351 return kobj;
352}
353
354void cdev_put(struct cdev *p)
355{
356 if (p) {
7da6844c 357 struct module *owner = p->owner;
1da177e4 358 kobject_put(&p->kobj);
7da6844c 359 module_put(owner);
1da177e4
LT
360 }
361}
362
363/*
364 * Called every time a character special file is opened
365 */
922f9cfa 366static int chrdev_open(struct inode *inode, struct file *filp)
1da177e4 367{
e84f9e57 368 const struct file_operations *fops;
1da177e4
LT
369 struct cdev *p;
370 struct cdev *new = NULL;
371 int ret = 0;
372
373 spin_lock(&cdev_lock);
374 p = inode->i_cdev;
375 if (!p) {
376 struct kobject *kobj;
377 int idx;
378 spin_unlock(&cdev_lock);
379 kobj = kobj_lookup(cdev_map, inode->i_rdev, &idx);
380 if (!kobj)
381 return -ENXIO;
382 new = container_of(kobj, struct cdev, kobj);
383 spin_lock(&cdev_lock);
a30427d9
JC
384 /* Check i_cdev again in case somebody beat us to it while
385 we dropped the lock. */
1da177e4
LT
386 p = inode->i_cdev;
387 if (!p) {
388 inode->i_cdev = p = new;
1da177e4
LT
389 list_add(&inode->i_devices, &p->list);
390 new = NULL;
391 } else if (!cdev_get(p))
392 ret = -ENXIO;
393 } else if (!cdev_get(p))
394 ret = -ENXIO;
395 spin_unlock(&cdev_lock);
396 cdev_put(new);
397 if (ret)
398 return ret;
a518ab93
CH
399
400 ret = -ENXIO;
e84f9e57
AV
401 fops = fops_get(p->ops);
402 if (!fops)
a518ab93
CH
403 goto out_cdev_put;
404
e84f9e57 405 replace_fops(filp, fops);
a518ab93 406 if (filp->f_op->open) {
1ff97647 407 ret = filp->f_op->open(inode, filp);
a518ab93
CH
408 if (ret)
409 goto out_cdev_put;
410 }
411
412 return 0;
413
414 out_cdev_put:
415 cdev_put(p);
1da177e4
LT
416 return ret;
417}
418
419void cd_forget(struct inode *inode)
420{
421 spin_lock(&cdev_lock);
422 list_del_init(&inode->i_devices);
423 inode->i_cdev = NULL;
3bc52c45 424 inode->i_mapping = &inode->i_data;
1da177e4
LT
425 spin_unlock(&cdev_lock);
426}
427
75c96f85 428static void cdev_purge(struct cdev *cdev)
1da177e4
LT
429{
430 spin_lock(&cdev_lock);
431 while (!list_empty(&cdev->list)) {
432 struct inode *inode;
433 inode = container_of(cdev->list.next, struct inode, i_devices);
434 list_del_init(&inode->i_devices);
435 inode->i_cdev = NULL;
436 }
437 spin_unlock(&cdev_lock);
438}
439
440/*
441 * Dummy default file-operations: the only thing this does
442 * is contain the open that then fills in the correct operations
443 * depending on the special file...
444 */
4b6f5d20 445const struct file_operations def_chr_fops = {
1da177e4 446 .open = chrdev_open,
6038f373 447 .llseek = noop_llseek,
1da177e4
LT
448};
449
450static struct kobject *exact_match(dev_t dev, int *part, void *data)
451{
452 struct cdev *p = data;
453 return &p->kobj;
454}
455
456static int exact_lock(dev_t dev, void *data)
457{
458 struct cdev *p = data;
459 return cdev_get(p) ? 0 : -1;
460}
461
cf3e43db
JC
462/**
463 * cdev_add() - add a char device to the system
464 * @p: the cdev structure for the device
465 * @dev: the first device number for which this device is responsible
466 * @count: the number of consecutive minor numbers corresponding to this
467 * device
468 *
469 * cdev_add() adds the device represented by @p to the system, making it
470 * live immediately. A negative error code is returned on failure.
471 */
1da177e4
LT
472int cdev_add(struct cdev *p, dev_t dev, unsigned count)
473{
2f0157f1
DT
474 int error;
475
1da177e4
LT
476 p->dev = dev;
477 p->count = count;
2f0157f1
DT
478
479 error = kobj_map(cdev_map, dev, count, NULL,
480 exact_match, exact_lock, p);
481 if (error)
482 return error;
483
484 kobject_get(p->kobj.parent);
485
486 return 0;
1da177e4
LT
487}
488
233ed09d
LG
489/**
490 * cdev_set_parent() - set the parent kobject for a char device
491 * @p: the cdev structure
492 * @kobj: the kobject to take a reference to
493 *
494 * cdev_set_parent() sets a parent kobject which will be referenced
495 * appropriately so the parent is not freed before the cdev. This
496 * should be called before cdev_add.
497 */
498void cdev_set_parent(struct cdev *p, struct kobject *kobj)
499{
500 WARN_ON(!kobj->state_initialized);
501 p->kobj.parent = kobj;
502}
503
504/**
505 * cdev_device_add() - add a char device and it's corresponding
506 * struct device, linkink
507 * @dev: the device structure
508 * @cdev: the cdev structure
509 *
510 * cdev_device_add() adds the char device represented by @cdev to the system,
511 * just as cdev_add does. It then adds @dev to the system using device_add
512 * The dev_t for the char device will be taken from the struct device which
513 * needs to be initialized first. This helper function correctly takes a
514 * reference to the parent device so the parent will not get released until
515 * all references to the cdev are released.
516 *
517 * This helper uses dev->devt for the device number. If it is not set
518 * it will not add the cdev and it will be equivalent to device_add.
519 *
520 * This function should be used whenever the struct cdev and the
521 * struct device are members of the same structure whose lifetime is
522 * managed by the struct device.
523 *
524 * NOTE: Callers must assume that userspace was able to open the cdev and
525 * can call cdev fops callbacks at any time, even if this function fails.
526 */
527int cdev_device_add(struct cdev *cdev, struct device *dev)
528{
529 int rc = 0;
530
531 if (dev->devt) {
532 cdev_set_parent(cdev, &dev->kobj);
533
534 rc = cdev_add(cdev, dev->devt, 1);
535 if (rc)
536 return rc;
537 }
538
539 rc = device_add(dev);
540 if (rc)
541 cdev_del(cdev);
542
543 return rc;
544}
545
546/**
547 * cdev_device_del() - inverse of cdev_device_add
548 * @dev: the device structure
549 * @cdev: the cdev structure
550 *
551 * cdev_device_del() is a helper function to call cdev_del and device_del.
552 * It should be used whenever cdev_device_add is used.
553 *
554 * If dev->devt is not set it will not remove the cdev and will be equivalent
555 * to device_del.
556 *
557 * NOTE: This guarantees that associated sysfs callbacks are not running
558 * or runnable, however any cdevs already open will remain and their fops
559 * will still be callable even after this function returns.
560 */
561void cdev_device_del(struct cdev *cdev, struct device *dev)
562{
563 device_del(dev);
564 if (dev->devt)
565 cdev_del(cdev);
566}
567
1da177e4
LT
568static void cdev_unmap(dev_t dev, unsigned count)
569{
570 kobj_unmap(cdev_map, dev, count);
571}
572
cf3e43db
JC
573/**
574 * cdev_del() - remove a cdev from the system
575 * @p: the cdev structure to be removed
576 *
577 * cdev_del() removes @p from the system, possibly freeing the structure
578 * itself.
233ed09d
LG
579 *
580 * NOTE: This guarantees that cdev device will no longer be able to be
581 * opened, however any cdevs already open will remain and their fops will
582 * still be callable even after cdev_del returns.
cf3e43db 583 */
1da177e4
LT
584void cdev_del(struct cdev *p)
585{
586 cdev_unmap(p->dev, p->count);
587 kobject_put(&p->kobj);
588}
589
590
591static void cdev_default_release(struct kobject *kobj)
592{
593 struct cdev *p = container_of(kobj, struct cdev, kobj);
2f0157f1
DT
594 struct kobject *parent = kobj->parent;
595
1da177e4 596 cdev_purge(p);
2f0157f1 597 kobject_put(parent);
1da177e4
LT
598}
599
600static void cdev_dynamic_release(struct kobject *kobj)
601{
602 struct cdev *p = container_of(kobj, struct cdev, kobj);
2f0157f1
DT
603 struct kobject *parent = kobj->parent;
604
1da177e4
LT
605 cdev_purge(p);
606 kfree(p);
2f0157f1 607 kobject_put(parent);
1da177e4
LT
608}
609
610static struct kobj_type ktype_cdev_default = {
611 .release = cdev_default_release,
612};
613
614static struct kobj_type ktype_cdev_dynamic = {
615 .release = cdev_dynamic_release,
616};
617
cf3e43db
JC
618/**
619 * cdev_alloc() - allocate a cdev structure
620 *
621 * Allocates and returns a cdev structure, or NULL on failure.
622 */
1da177e4
LT
623struct cdev *cdev_alloc(void)
624{
11b0b5ab 625 struct cdev *p = kzalloc(sizeof(struct cdev), GFP_KERNEL);
1da177e4 626 if (p) {
1da177e4 627 INIT_LIST_HEAD(&p->list);
f9cb074b 628 kobject_init(&p->kobj, &ktype_cdev_dynamic);
1da177e4
LT
629 }
630 return p;
631}
632
cf3e43db
JC
633/**
634 * cdev_init() - initialize a cdev structure
635 * @cdev: the structure to initialize
636 * @fops: the file_operations for this device
637 *
638 * Initializes @cdev, remembering @fops, making it ready to add to the
639 * system with cdev_add().
640 */
99ac48f5 641void cdev_init(struct cdev *cdev, const struct file_operations *fops)
1da177e4
LT
642{
643 memset(cdev, 0, sizeof *cdev);
644 INIT_LIST_HEAD(&cdev->list);
f9cb074b 645 kobject_init(&cdev->kobj, &ktype_cdev_default);
1da177e4
LT
646 cdev->ops = fops;
647}
648
649static struct kobject *base_probe(dev_t dev, int *part, void *data)
650{
651 if (request_module("char-major-%d-%d", MAJOR(dev), MINOR(dev)) > 0)
652 /* Make old-style 2.4 aliases work */
653 request_module("char-major-%d", MAJOR(dev));
654 return NULL;
655}
656
657void __init chrdev_init(void)
658{
659 cdev_map = kobj_map_init(base_probe, &chrdevs_lock);
660}
661
662
663/* Let modules do char dev stuff */
664EXPORT_SYMBOL(register_chrdev_region);
665EXPORT_SYMBOL(unregister_chrdev_region);
666EXPORT_SYMBOL(alloc_chrdev_region);
667EXPORT_SYMBOL(cdev_init);
668EXPORT_SYMBOL(cdev_alloc);
669EXPORT_SYMBOL(cdev_del);
670EXPORT_SYMBOL(cdev_add);
233ed09d
LG
671EXPORT_SYMBOL(cdev_set_parent);
672EXPORT_SYMBOL(cdev_device_add);
673EXPORT_SYMBOL(cdev_device_del);
1905b1bf
TH
674EXPORT_SYMBOL(__register_chrdev);
675EXPORT_SYMBOL(__unregister_chrdev);