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