projects/linux·blob master · refs

691 lines · 16.7 KiB

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