4 * Copyright (C) 1991, 1992 Linus Torvalds
7 #include <linux/init.h>
9 #include <linux/kdev_t.h>
10 #include <linux/slab.h>
11 #include <linux/string.h>
13 #include <linux/major.h>
14 #include <linux/errno.h>
15 #include <linux/module.h>
16 #include <linux/seq_file.h>
18 #include <linux/kobject.h>
19 #include <linux/kobj_map.h>
20 #include <linux/cdev.h>
21 #include <linux/mutex.h>
22 #include <linux/backing-dev.h>
23 #include <linux/tty.h>
27 static struct kobj_map
*cdev_map
;
29 static DEFINE_MUTEX(chrdevs_lock
);
31 #define CHRDEV_MAJOR_HASH_SIZE 255
33 static struct char_device_struct
{
34 struct char_device_struct
*next
;
36 unsigned int baseminor
;
39 struct cdev
*cdev
; /* will die */
40 } *chrdevs
[CHRDEV_MAJOR_HASH_SIZE
];
42 /* index in the above */
43 static inline int major_to_index(unsigned major
)
45 return major
% CHRDEV_MAJOR_HASH_SIZE
;
50 void chrdev_show(struct seq_file
*f
, off_t offset
)
52 struct char_device_struct
*cd
;
54 mutex_lock(&chrdevs_lock
);
55 for (cd
= chrdevs
[major_to_index(offset
)]; cd
; cd
= cd
->next
) {
56 if (cd
->major
== offset
)
57 seq_printf(f
, "%3d %s\n", cd
->major
, cd
->name
);
59 mutex_unlock(&chrdevs_lock
);
62 #endif /* CONFIG_PROC_FS */
64 static int find_dynamic_major(void)
67 struct char_device_struct
*cd
;
69 for (i
= ARRAY_SIZE(chrdevs
)-1; i
> CHRDEV_MAJOR_DYN_END
; i
--) {
70 if (chrdevs
[i
] == NULL
)
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
)
80 if (cd
== NULL
|| cd
->major
!= i
)
88 * Register a single major with a specified minor range.
90 * If major == 0 this functions will dynamically allocate a major and return
93 * If major > 0 this function will attempt to reserve the passed range of
94 * minors and will return zero on success.
96 * Returns a -ve errno on failure.
98 static struct char_device_struct
*
99 __register_chrdev_region(unsigned int major
, unsigned int baseminor
,
100 int minorct
, const char *name
)
102 struct char_device_struct
*cd
, **cp
;
106 cd
= kzalloc(sizeof(struct char_device_struct
), GFP_KERNEL
);
108 return ERR_PTR(-ENOMEM
);
110 mutex_lock(&chrdevs_lock
);
113 ret
= find_dynamic_major();
115 pr_err("CHRDEV \"%s\" dynamic allocation region is full\n",
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
);
130 cd
->baseminor
= baseminor
;
131 cd
->minorct
= minorct
;
132 strlcpy(cd
->name
, name
, sizeof(cd
->name
));
134 i
= major_to_index(major
);
136 for (cp
= &chrdevs
[i
]; *cp
; cp
= &(*cp
)->next
)
137 if ((*cp
)->major
> major
||
138 ((*cp
)->major
== major
&&
139 (((*cp
)->baseminor
>= baseminor
) ||
140 ((*cp
)->baseminor
+ (*cp
)->minorct
> baseminor
))))
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;
150 /* New driver overlaps from the left. */
151 if (new_max
>= old_min
&& new_max
<= old_max
) {
156 /* New driver overlaps from the right. */
157 if (new_min
<= old_max
&& new_min
>= old_min
) {
165 mutex_unlock(&chrdevs_lock
);
168 mutex_unlock(&chrdevs_lock
);
173 static struct char_device_struct
*
174 __unregister_chrdev_region(unsigned major
, unsigned baseminor
, int minorct
)
176 struct char_device_struct
*cd
= NULL
, **cp
;
177 int i
= major_to_index(major
);
179 mutex_lock(&chrdevs_lock
);
180 for (cp
= &chrdevs
[i
]; *cp
; cp
= &(*cp
)->next
)
181 if ((*cp
)->major
== major
&&
182 (*cp
)->baseminor
== baseminor
&&
183 (*cp
)->minorct
== minorct
)
189 mutex_unlock(&chrdevs_lock
);
194 * register_chrdev_region() - register a range of device numbers
195 * @from: the first in the desired range of device numbers; must include
197 * @count: the number of consecutive device numbers required
198 * @name: the name of the device or driver.
200 * Return value is zero on success, a negative error code on failure.
202 int register_chrdev_region(dev_t from
, unsigned count
, const char *name
)
204 struct char_device_struct
*cd
;
205 dev_t to
= from
+ count
;
208 for (n
= from
; n
< to
; n
= next
) {
209 next
= MKDEV(MAJOR(n
)+1, 0);
212 cd
= __register_chrdev_region(MAJOR(n
), MINOR(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
));
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
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.
238 int alloc_chrdev_region(dev_t
*dev
, unsigned baseminor
, unsigned count
,
241 struct char_device_struct
*cd
;
242 cd
= __register_chrdev_region(0, baseminor
, count
, name
);
245 *dev
= MKDEV(cd
->major
, cd
->baseminor
);
250 * __register_chrdev() - create and register a cdev occupying a range of minors
251 * @major: major device number or 0 for dynamic allocation
252 * @baseminor: first of the requested range of minor numbers
253 * @count: the number of minor numbers required
254 * @name: name of this range of devices
255 * @fops: file operations associated with this devices
257 * If @major == 0 this functions will dynamically allocate a major and return
260 * If @major > 0 this function will attempt to reserve a device with the given
261 * major number and will return zero on success.
263 * Returns a -ve errno on failure.
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.
270 int __register_chrdev(unsigned int major
, unsigned int baseminor
,
271 unsigned int count
, const char *name
,
272 const struct file_operations
*fops
)
274 struct char_device_struct
*cd
;
278 cd
= __register_chrdev_region(major
, baseminor
, count
, name
);
286 cdev
->owner
= fops
->owner
;
288 kobject_set_name(&cdev
->kobj
, "%s", name
);
290 err
= cdev_add(cdev
, MKDEV(cd
->major
, baseminor
), count
);
296 return major
? 0 : cd
->major
;
298 kobject_put(&cdev
->kobj
);
300 kfree(__unregister_chrdev_region(cd
->major
, baseminor
, count
));
305 * unregister_chrdev_region() - unregister a range of device numbers
306 * @from: the first in the range of numbers to unregister
307 * @count: the number of device numbers to unregister
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...
313 void unregister_chrdev_region(dev_t from
, unsigned count
)
315 dev_t to
= from
+ count
;
318 for (n
= from
; n
< to
; n
= next
) {
319 next
= MKDEV(MAJOR(n
)+1, 0);
322 kfree(__unregister_chrdev_region(MAJOR(n
), MINOR(n
), next
- n
));
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
333 * Unregister and destroy the cdev occupying the region described by
334 * @major, @baseminor and @count. This function undoes what
335 * __register_chrdev() did.
337 void __unregister_chrdev(unsigned int major
, unsigned int baseminor
,
338 unsigned int count
, const char *name
)
340 struct char_device_struct
*cd
;
342 cd
= __unregister_chrdev_region(major
, baseminor
, count
);
348 static DEFINE_SPINLOCK(cdev_lock
);
350 static struct kobject
*cdev_get(struct cdev
*p
)
352 struct module
*owner
= p
->owner
;
353 struct kobject
*kobj
;
355 if (owner
&& !try_module_get(owner
))
357 kobj
= kobject_get(&p
->kobj
);
363 void cdev_put(struct cdev
*p
)
366 struct module
*owner
= p
->owner
;
367 kobject_put(&p
->kobj
);
373 * Called every time a character special file is opened
375 static int chrdev_open(struct inode
*inode
, struct file
*filp
)
377 const struct file_operations
*fops
;
379 struct cdev
*new = NULL
;
382 spin_lock(&cdev_lock
);
385 struct kobject
*kobj
;
387 spin_unlock(&cdev_lock
);
388 kobj
= kobj_lookup(cdev_map
, inode
->i_rdev
, &idx
);
391 new = container_of(kobj
, struct cdev
, kobj
);
392 spin_lock(&cdev_lock
);
393 /* Check i_cdev again in case somebody beat us to it while
394 we dropped the lock. */
397 inode
->i_cdev
= p
= new;
398 list_add(&inode
->i_devices
, &p
->list
);
400 } else if (!cdev_get(p
))
402 } else if (!cdev_get(p
))
404 spin_unlock(&cdev_lock
);
410 fops
= fops_get(p
->ops
);
414 replace_fops(filp
, fops
);
415 if (filp
->f_op
->open
) {
416 ret
= filp
->f_op
->open(inode
, filp
);
428 void cd_forget(struct inode
*inode
)
430 spin_lock(&cdev_lock
);
431 list_del_init(&inode
->i_devices
);
432 inode
->i_cdev
= NULL
;
433 inode
->i_mapping
= &inode
->i_data
;
434 spin_unlock(&cdev_lock
);
437 static void cdev_purge(struct cdev
*cdev
)
439 spin_lock(&cdev_lock
);
440 while (!list_empty(&cdev
->list
)) {
442 inode
= container_of(cdev
->list
.next
, struct inode
, i_devices
);
443 list_del_init(&inode
->i_devices
);
444 inode
->i_cdev
= NULL
;
446 spin_unlock(&cdev_lock
);
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...
454 const struct file_operations def_chr_fops
= {
456 .llseek
= noop_llseek
,
459 static struct kobject
*exact_match(dev_t dev
, int *part
, void *data
)
461 struct cdev
*p
= data
;
465 static int exact_lock(dev_t dev
, void *data
)
467 struct cdev
*p
= data
;
468 return cdev_get(p
) ? 0 : -1;
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
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.
481 int cdev_add(struct cdev
*p
, dev_t dev
, unsigned count
)
488 error
= kobj_map(cdev_map
, dev
, count
, NULL
,
489 exact_match
, exact_lock
, p
);
493 kobject_get(p
->kobj
.parent
);
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
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.
507 void cdev_set_parent(struct cdev
*p
, struct kobject
*kobj
)
509 WARN_ON(!kobj
->state_initialized
);
510 p
->kobj
.parent
= kobj
;
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
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.
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.
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.
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.
536 int cdev_device_add(struct cdev
*cdev
, struct device
*dev
)
541 cdev_set_parent(cdev
, &dev
->kobj
);
543 rc
= cdev_add(cdev
, dev
->devt
, 1);
548 rc
= device_add(dev
);
556 * cdev_device_del() - inverse of cdev_device_add
557 * @dev: the device structure
558 * @cdev: the cdev structure
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.
563 * If dev->devt is not set it will not remove the cdev and will be equivalent
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.
570 void cdev_device_del(struct cdev
*cdev
, struct device
*dev
)
577 static void cdev_unmap(dev_t dev
, unsigned count
)
579 kobj_unmap(cdev_map
, dev
, count
);
583 * cdev_del() - remove a cdev from the system
584 * @p: the cdev structure to be removed
586 * cdev_del() removes @p from the system, possibly freeing the structure
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.
593 void cdev_del(struct cdev
*p
)
595 cdev_unmap(p
->dev
, p
->count
);
596 kobject_put(&p
->kobj
);
600 static void cdev_default_release(struct kobject
*kobj
)
602 struct cdev
*p
= container_of(kobj
, struct cdev
, kobj
);
603 struct kobject
*parent
= kobj
->parent
;
609 static void cdev_dynamic_release(struct kobject
*kobj
)
611 struct cdev
*p
= container_of(kobj
, struct cdev
, kobj
);
612 struct kobject
*parent
= kobj
->parent
;
619 static struct kobj_type ktype_cdev_default
= {
620 .release
= cdev_default_release
,
623 static struct kobj_type ktype_cdev_dynamic
= {
624 .release
= cdev_dynamic_release
,
628 * cdev_alloc() - allocate a cdev structure
630 * Allocates and returns a cdev structure, or NULL on failure.
632 struct cdev
*cdev_alloc(void)
634 struct cdev
*p
= kzalloc(sizeof(struct cdev
), GFP_KERNEL
);
636 INIT_LIST_HEAD(&p
->list
);
637 kobject_init(&p
->kobj
, &ktype_cdev_dynamic
);
643 * cdev_init() - initialize a cdev structure
644 * @cdev: the structure to initialize
645 * @fops: the file_operations for this device
647 * Initializes @cdev, remembering @fops, making it ready to add to the
648 * system with cdev_add().
650 void cdev_init(struct cdev
*cdev
, const struct file_operations
*fops
)
652 memset(cdev
, 0, sizeof *cdev
);
653 INIT_LIST_HEAD(&cdev
->list
);
654 kobject_init(&cdev
->kobj
, &ktype_cdev_default
);
658 static struct kobject
*base_probe(dev_t dev
, int *part
, void *data
)
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
));
666 void __init
chrdev_init(void)
668 cdev_map
= kobj_map_init(base_probe
, &chrdevs_lock
);
672 /* Let modules do char dev stuff */
673 EXPORT_SYMBOL(register_chrdev_region
);
674 EXPORT_SYMBOL(unregister_chrdev_region
);
675 EXPORT_SYMBOL(alloc_chrdev_region
);
676 EXPORT_SYMBOL(cdev_init
);
677 EXPORT_SYMBOL(cdev_alloc
);
678 EXPORT_SYMBOL(cdev_del
);
679 EXPORT_SYMBOL(cdev_add
);
680 EXPORT_SYMBOL(cdev_set_parent
);
681 EXPORT_SYMBOL(cdev_device_add
);
682 EXPORT_SYMBOL(cdev_device_del
);
683 EXPORT_SYMBOL(__register_chrdev
);
684 EXPORT_SYMBOL(__unregister_chrdev
);