4 * Copyright (C) 1991, 1992 Linus Torvalds
7 #include <linux/init.h>
9 #include <linux/slab.h>
10 #include <linux/string.h>
12 #include <linux/major.h>
13 #include <linux/errno.h>
14 #include <linux/module.h>
15 #include <linux/smp_lock.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>
25 #include <linux/kmod.h>
30 * capabilities for /dev/mem, /dev/kmem and similar directly mappable character
32 * - permits shared-mmap for read, write and/or exec
33 * - does not permit private mmap in NOMMU mode (can't do COW)
34 * - no readahead or I/O queue unplugging required
36 struct backing_dev_info directly_mappable_cdev_bdi
= {
39 /* permit private copies of the data to be taken */
42 /* permit direct mmap, for read, write or exec */
44 BDI_CAP_READ_MAP
| BDI_CAP_WRITE_MAP
| BDI_CAP_EXEC_MAP
),
47 static struct kobj_map
*cdev_map
;
49 static DEFINE_MUTEX(chrdevs_lock
);
51 static struct char_device_struct
{
52 struct char_device_struct
*next
;
54 unsigned int baseminor
;
57 struct file_operations
*fops
;
58 struct cdev
*cdev
; /* will die */
59 } *chrdevs
[CHRDEV_MAJOR_HASH_SIZE
];
61 /* index in the above */
62 static inline int major_to_index(int major
)
64 return major
% CHRDEV_MAJOR_HASH_SIZE
;
69 void chrdev_show(struct seq_file
*f
, off_t offset
)
71 struct char_device_struct
*cd
;
73 if (offset
< CHRDEV_MAJOR_HASH_SIZE
) {
74 mutex_lock(&chrdevs_lock
);
75 for (cd
= chrdevs
[offset
]; cd
; cd
= cd
->next
)
76 seq_printf(f
, "%3d %s\n", cd
->major
, cd
->name
);
77 mutex_unlock(&chrdevs_lock
);
81 #endif /* CONFIG_PROC_FS */
84 * Register a single major with a specified minor range.
86 * If major == 0 this functions will dynamically allocate a major and return
89 * If major > 0 this function will attempt to reserve the passed range of
90 * minors and will return zero on success.
92 * Returns a -ve errno on failure.
94 static struct char_device_struct
*
95 __register_chrdev_region(unsigned int major
, unsigned int baseminor
,
96 int minorct
, const char *name
)
98 struct char_device_struct
*cd
, **cp
;
102 cd
= kzalloc(sizeof(struct char_device_struct
), GFP_KERNEL
);
104 return ERR_PTR(-ENOMEM
);
106 mutex_lock(&chrdevs_lock
);
110 for (i
= ARRAY_SIZE(chrdevs
)-1; i
> 0; i
--) {
112 * Disallow the LANANA-assigned LOCAL/EXPERIMENTAL
115 if ((60 <= i
&& i
<= 63) || (120 <= i
&& i
<= 127) ||
116 (240 <= i
&& i
<= 254))
118 if (chrdevs
[i
] == NULL
)
131 cd
->baseminor
= baseminor
;
132 cd
->minorct
= minorct
;
133 strncpy(cd
->name
,name
, 64);
135 i
= major_to_index(major
);
137 for (cp
= &chrdevs
[i
]; *cp
; cp
= &(*cp
)->next
)
138 if ((*cp
)->major
> major
||
139 ((*cp
)->major
== major
&&
140 (((*cp
)->baseminor
>= baseminor
) ||
141 ((*cp
)->baseminor
+ (*cp
)->minorct
> baseminor
))))
144 /* Check for overlapping minor ranges. */
145 if (*cp
&& (*cp
)->major
== major
) {
146 int old_min
= (*cp
)->baseminor
;
147 int old_max
= (*cp
)->baseminor
+ (*cp
)->minorct
- 1;
148 int new_min
= baseminor
;
149 int new_max
= baseminor
+ minorct
- 1;
151 /* New driver overlaps from the left. */
152 if (new_max
>= old_min
&& new_max
<= old_max
) {
157 /* New driver overlaps from the right. */
158 if (new_min
<= old_max
&& new_min
>= old_min
) {
166 mutex_unlock(&chrdevs_lock
);
169 mutex_unlock(&chrdevs_lock
);
174 static struct char_device_struct
*
175 __unregister_chrdev_region(unsigned major
, unsigned baseminor
, int minorct
)
177 struct char_device_struct
*cd
= NULL
, **cp
;
178 int i
= major_to_index(major
);
180 mutex_lock(&chrdevs_lock
);
181 for (cp
= &chrdevs
[i
]; *cp
; cp
= &(*cp
)->next
)
182 if ((*cp
)->major
== major
&&
183 (*cp
)->baseminor
== baseminor
&&
184 (*cp
)->minorct
== minorct
)
190 mutex_unlock(&chrdevs_lock
);
195 * register_chrdev_region() - register a range of device numbers
196 * @from: the first in the desired range of device numbers; must include
198 * @count: the number of consecutive device numbers required
199 * @name: the name of the device or driver.
201 * Return value is zero on success, a negative error code on failure.
203 int register_chrdev_region(dev_t from
, unsigned count
, const char *name
)
205 struct char_device_struct
*cd
;
206 dev_t to
= from
+ count
;
209 for (n
= from
; n
< to
; n
= next
) {
210 next
= MKDEV(MAJOR(n
)+1, 0);
213 cd
= __register_chrdev_region(MAJOR(n
), MINOR(n
),
221 for (n
= from
; n
< to
; n
= next
) {
222 next
= MKDEV(MAJOR(n
)+1, 0);
223 kfree(__unregister_chrdev_region(MAJOR(n
), MINOR(n
), next
- n
));
229 * alloc_chrdev_region() - register a range of char device numbers
230 * @dev: output parameter for first assigned number
231 * @baseminor: first of the requested range of minor numbers
232 * @count: the number of minor numbers required
233 * @name: the name of the associated device or driver
235 * Allocates a range of char device numbers. The major number will be
236 * chosen dynamically, and returned (along with the first minor number)
237 * in @dev. Returns zero or a negative error code.
239 int alloc_chrdev_region(dev_t
*dev
, unsigned baseminor
, unsigned count
,
242 struct char_device_struct
*cd
;
243 cd
= __register_chrdev_region(0, baseminor
, count
, name
);
246 *dev
= MKDEV(cd
->major
, cd
->baseminor
);
251 * register_chrdev() - Register a major number for character devices.
252 * @major: major device number or 0 for dynamic allocation
253 * @name: name of this range of devices
254 * @fops: file operations associated with this devices
256 * If @major == 0 this functions will dynamically allocate a major and return
259 * If @major > 0 this function will attempt to reserve a device with the given
260 * major number and will return zero on success.
262 * Returns a -ve errno on failure.
264 * The name of this device has nothing to do with the name of the device in
265 * /dev. It only helps to keep track of the different owners of devices. If
266 * your module name has only one type of devices it's ok to use e.g. the name
267 * of the module here.
269 * This function registers a range of 256 minor numbers. The first minor number
272 int register_chrdev(unsigned int major
, const char *name
,
273 const struct file_operations
*fops
)
275 struct char_device_struct
*cd
;
280 cd
= __register_chrdev_region(major
, 0, 256, name
);
288 cdev
->owner
= fops
->owner
;
290 kobject_set_name(&cdev
->kobj
, "%s", name
);
291 for (s
= strchr(kobject_name(&cdev
->kobj
),'/'); s
; s
= strchr(s
, '/'))
294 err
= cdev_add(cdev
, MKDEV(cd
->major
, 0), 256);
300 return major
? 0 : cd
->major
;
302 kobject_put(&cdev
->kobj
);
304 kfree(__unregister_chrdev_region(cd
->major
, 0, 256));
309 * unregister_chrdev_region() - return a range of device numbers
310 * @from: the first in the range of numbers to unregister
311 * @count: the number of device numbers to unregister
313 * This function will unregister a range of @count device numbers,
314 * starting with @from. The caller should normally be the one who
315 * allocated those numbers in the first place...
317 void unregister_chrdev_region(dev_t from
, unsigned count
)
319 dev_t to
= from
+ count
;
322 for (n
= from
; n
< to
; n
= next
) {
323 next
= MKDEV(MAJOR(n
)+1, 0);
326 kfree(__unregister_chrdev_region(MAJOR(n
), MINOR(n
), next
- n
));
330 int unregister_chrdev(unsigned int major
, const char *name
)
332 struct char_device_struct
*cd
;
333 cd
= __unregister_chrdev_region(major
, 0, 256);
340 static DEFINE_SPINLOCK(cdev_lock
);
342 static struct kobject
*cdev_get(struct cdev
*p
)
344 struct module
*owner
= p
->owner
;
345 struct kobject
*kobj
;
347 if (owner
&& !try_module_get(owner
))
349 kobj
= kobject_get(&p
->kobj
);
355 void cdev_put(struct cdev
*p
)
358 struct module
*owner
= p
->owner
;
359 kobject_put(&p
->kobj
);
365 * Called every time a character special file is opened
367 int chrdev_open(struct inode
* inode
, struct file
* filp
)
370 struct cdev
*new = NULL
;
373 spin_lock(&cdev_lock
);
376 struct kobject
*kobj
;
378 spin_unlock(&cdev_lock
);
379 kobj
= kobj_lookup(cdev_map
, inode
->i_rdev
, &idx
);
382 new = container_of(kobj
, struct cdev
, kobj
);
383 spin_lock(&cdev_lock
);
386 inode
->i_cdev
= p
= new;
387 inode
->i_cindex
= idx
;
388 list_add(&inode
->i_devices
, &p
->list
);
390 } else if (!cdev_get(p
))
392 } else if (!cdev_get(p
))
394 spin_unlock(&cdev_lock
);
398 filp
->f_op
= fops_get(p
->ops
);
403 if (filp
->f_op
->open
) {
405 ret
= filp
->f_op
->open(inode
,filp
);
413 void cd_forget(struct inode
*inode
)
415 spin_lock(&cdev_lock
);
416 list_del_init(&inode
->i_devices
);
417 inode
->i_cdev
= NULL
;
418 spin_unlock(&cdev_lock
);
421 static void cdev_purge(struct cdev
*cdev
)
423 spin_lock(&cdev_lock
);
424 while (!list_empty(&cdev
->list
)) {
426 inode
= container_of(cdev
->list
.next
, struct inode
, i_devices
);
427 list_del_init(&inode
->i_devices
);
428 inode
->i_cdev
= NULL
;
430 spin_unlock(&cdev_lock
);
434 * Dummy default file-operations: the only thing this does
435 * is contain the open that then fills in the correct operations
436 * depending on the special file...
438 const struct file_operations def_chr_fops
= {
442 static struct kobject
*exact_match(dev_t dev
, int *part
, void *data
)
444 struct cdev
*p
= data
;
448 static int exact_lock(dev_t dev
, void *data
)
450 struct cdev
*p
= data
;
451 return cdev_get(p
) ? 0 : -1;
455 * cdev_add() - add a char device to the system
456 * @p: the cdev structure for the device
457 * @dev: the first device number for which this device is responsible
458 * @count: the number of consecutive minor numbers corresponding to this
461 * cdev_add() adds the device represented by @p to the system, making it
462 * live immediately. A negative error code is returned on failure.
464 int cdev_add(struct cdev
*p
, dev_t dev
, unsigned count
)
468 return kobj_map(cdev_map
, dev
, count
, NULL
, exact_match
, exact_lock
, p
);
471 static void cdev_unmap(dev_t dev
, unsigned count
)
473 kobj_unmap(cdev_map
, dev
, count
);
477 * cdev_del() - remove a cdev from the system
478 * @p: the cdev structure to be removed
480 * cdev_del() removes @p from the system, possibly freeing the structure
483 void cdev_del(struct cdev
*p
)
485 cdev_unmap(p
->dev
, p
->count
);
486 kobject_put(&p
->kobj
);
490 static void cdev_default_release(struct kobject
*kobj
)
492 struct cdev
*p
= container_of(kobj
, struct cdev
, kobj
);
496 static void cdev_dynamic_release(struct kobject
*kobj
)
498 struct cdev
*p
= container_of(kobj
, struct cdev
, kobj
);
503 static struct kobj_type ktype_cdev_default
= {
504 .release
= cdev_default_release
,
507 static struct kobj_type ktype_cdev_dynamic
= {
508 .release
= cdev_dynamic_release
,
512 * cdev_alloc() - allocate a cdev structure
514 * Allocates and returns a cdev structure, or NULL on failure.
516 struct cdev
*cdev_alloc(void)
518 struct cdev
*p
= kzalloc(sizeof(struct cdev
), GFP_KERNEL
);
520 p
->kobj
.ktype
= &ktype_cdev_dynamic
;
521 INIT_LIST_HEAD(&p
->list
);
522 kobject_init(&p
->kobj
);
528 * cdev_init() - initialize a cdev structure
529 * @cdev: the structure to initialize
530 * @fops: the file_operations for this device
532 * Initializes @cdev, remembering @fops, making it ready to add to the
533 * system with cdev_add().
535 void cdev_init(struct cdev
*cdev
, const struct file_operations
*fops
)
537 memset(cdev
, 0, sizeof *cdev
);
538 INIT_LIST_HEAD(&cdev
->list
);
539 cdev
->kobj
.ktype
= &ktype_cdev_default
;
540 kobject_init(&cdev
->kobj
);
544 static struct kobject
*base_probe(dev_t dev
, int *part
, void *data
)
546 if (request_module("char-major-%d-%d", MAJOR(dev
), MINOR(dev
)) > 0)
547 /* Make old-style 2.4 aliases work */
548 request_module("char-major-%d", MAJOR(dev
));
552 void __init
chrdev_init(void)
554 cdev_map
= kobj_map_init(base_probe
, &chrdevs_lock
);
558 /* Let modules do char dev stuff */
559 EXPORT_SYMBOL(register_chrdev_region
);
560 EXPORT_SYMBOL(unregister_chrdev_region
);
561 EXPORT_SYMBOL(alloc_chrdev_region
);
562 EXPORT_SYMBOL(cdev_init
);
563 EXPORT_SYMBOL(cdev_alloc
);
564 EXPORT_SYMBOL(cdev_del
);
565 EXPORT_SYMBOL(cdev_add
);
566 EXPORT_SYMBOL(register_chrdev
);
567 EXPORT_SYMBOL(unregister_chrdev
);
568 EXPORT_SYMBOL(directly_mappable_cdev_bdi
);