4 * Copyright (C) 1991, 1992 Linus Torvalds
7 #include <linux/config.h>
8 #include <linux/init.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/smp_lock.h>
17 #include <linux/devfs_fs_kernel.h>
18 #include <linux/seq_file.h>
20 #include <linux/kobject.h>
21 #include <linux/kobj_map.h>
22 #include <linux/cdev.h>
25 #include <linux/kmod.h>
28 static struct kobj_map
*cdev_map
;
30 static DECLARE_MUTEX(chrdevs_lock
);
32 static struct char_device_struct
{
33 struct char_device_struct
*next
;
35 unsigned int baseminor
;
38 struct file_operations
*fops
;
39 struct cdev
*cdev
; /* will die */
40 } *chrdevs
[CHRDEV_MAJOR_HASH_SIZE
];
42 /* index in the above */
43 static inline int major_to_index(int 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 if (offset
< CHRDEV_MAJOR_HASH_SIZE
) {
56 for (cd
= chrdevs
[offset
]; cd
; cd
= cd
->next
)
57 seq_printf(f
, "%3d %s\n", cd
->major
, cd
->name
);
62 #endif /* CONFIG_PROC_FS */
65 * Register a single major with a specified minor range.
67 * If major == 0 this functions will dynamically allocate a major and return
70 * If major > 0 this function will attempt to reserve the passed range of
71 * minors and will return zero on success.
73 * Returns a -ve errno on failure.
75 static struct char_device_struct
*
76 __register_chrdev_region(unsigned int major
, unsigned int baseminor
,
77 int minorct
, const char *name
)
79 struct char_device_struct
*cd
, **cp
;
83 cd
= kmalloc(sizeof(struct char_device_struct
), GFP_KERNEL
);
85 return ERR_PTR(-ENOMEM
);
87 memset(cd
, 0, sizeof(struct char_device_struct
));
93 for (i
= ARRAY_SIZE(chrdevs
)-1; i
> 0; i
--) {
94 if (chrdevs
[i
] == NULL
)
107 cd
->baseminor
= baseminor
;
108 cd
->minorct
= minorct
;
109 strncpy(cd
->name
,name
, 64);
111 i
= major_to_index(major
);
113 for (cp
= &chrdevs
[i
]; *cp
; cp
= &(*cp
)->next
)
114 if ((*cp
)->major
> major
||
115 ((*cp
)->major
== major
&& (*cp
)->baseminor
>= baseminor
))
117 if (*cp
&& (*cp
)->major
== major
&&
118 (*cp
)->baseminor
< baseminor
+ minorct
) {
132 static struct char_device_struct
*
133 __unregister_chrdev_region(unsigned major
, unsigned baseminor
, int minorct
)
135 struct char_device_struct
*cd
= NULL
, **cp
;
136 int i
= major_to_index(major
);
139 for (cp
= &chrdevs
[i
]; *cp
; cp
= &(*cp
)->next
)
140 if ((*cp
)->major
== major
&&
141 (*cp
)->baseminor
== baseminor
&&
142 (*cp
)->minorct
== minorct
)
152 int register_chrdev_region(dev_t from
, unsigned count
, const char *name
)
154 struct char_device_struct
*cd
;
155 dev_t to
= from
+ count
;
158 for (n
= from
; n
< to
; n
= next
) {
159 next
= MKDEV(MAJOR(n
)+1, 0);
162 cd
= __register_chrdev_region(MAJOR(n
), MINOR(n
),
170 for (n
= from
; n
< to
; n
= next
) {
171 next
= MKDEV(MAJOR(n
)+1, 0);
172 kfree(__unregister_chrdev_region(MAJOR(n
), MINOR(n
), next
- n
));
177 int alloc_chrdev_region(dev_t
*dev
, unsigned baseminor
, unsigned count
,
180 struct char_device_struct
*cd
;
181 cd
= __register_chrdev_region(0, baseminor
, count
, name
);
184 *dev
= MKDEV(cd
->major
, cd
->baseminor
);
188 int register_chrdev(unsigned int major
, const char *name
,
189 struct file_operations
*fops
)
191 struct char_device_struct
*cd
;
196 cd
= __register_chrdev_region(major
, 0, 256, name
);
204 cdev
->owner
= fops
->owner
;
206 kobject_set_name(&cdev
->kobj
, "%s", name
);
207 for (s
= strchr(kobject_name(&cdev
->kobj
),'/'); s
; s
= strchr(s
, '/'))
210 err
= cdev_add(cdev
, MKDEV(cd
->major
, 0), 256);
216 return major
? 0 : cd
->major
;
218 kobject_put(&cdev
->kobj
);
220 kfree(__unregister_chrdev_region(cd
->major
, 0, 256));
224 void unregister_chrdev_region(dev_t from
, unsigned count
)
226 dev_t to
= from
+ count
;
229 for (n
= from
; n
< to
; n
= next
) {
230 next
= MKDEV(MAJOR(n
)+1, 0);
233 kfree(__unregister_chrdev_region(MAJOR(n
), MINOR(n
), next
- n
));
237 int unregister_chrdev(unsigned int major
, const char *name
)
239 struct char_device_struct
*cd
;
240 cd
= __unregister_chrdev_region(major
, 0, 256);
247 static DEFINE_SPINLOCK(cdev_lock
);
249 static struct kobject
*cdev_get(struct cdev
*p
)
251 struct module
*owner
= p
->owner
;
252 struct kobject
*kobj
;
254 if (owner
&& !try_module_get(owner
))
256 kobj
= kobject_get(&p
->kobj
);
262 void cdev_put(struct cdev
*p
)
265 struct module
*owner
= p
->owner
;
266 kobject_put(&p
->kobj
);
272 * Called every time a character special file is opened
274 int chrdev_open(struct inode
* inode
, struct file
* filp
)
277 struct cdev
*new = NULL
;
280 spin_lock(&cdev_lock
);
283 struct kobject
*kobj
;
285 spin_unlock(&cdev_lock
);
286 kobj
= kobj_lookup(cdev_map
, inode
->i_rdev
, &idx
);
289 new = container_of(kobj
, struct cdev
, kobj
);
290 spin_lock(&cdev_lock
);
293 inode
->i_cdev
= p
= new;
294 inode
->i_cindex
= idx
;
295 list_add(&inode
->i_devices
, &p
->list
);
297 } else if (!cdev_get(p
))
299 } else if (!cdev_get(p
))
301 spin_unlock(&cdev_lock
);
305 filp
->f_op
= fops_get(p
->ops
);
310 if (filp
->f_op
->open
) {
312 ret
= filp
->f_op
->open(inode
,filp
);
320 void cd_forget(struct inode
*inode
)
322 spin_lock(&cdev_lock
);
323 list_del_init(&inode
->i_devices
);
324 inode
->i_cdev
= NULL
;
325 spin_unlock(&cdev_lock
);
328 static void cdev_purge(struct cdev
*cdev
)
330 spin_lock(&cdev_lock
);
331 while (!list_empty(&cdev
->list
)) {
333 inode
= container_of(cdev
->list
.next
, struct inode
, i_devices
);
334 list_del_init(&inode
->i_devices
);
335 inode
->i_cdev
= NULL
;
337 spin_unlock(&cdev_lock
);
341 * Dummy default file-operations: the only thing this does
342 * is contain the open that then fills in the correct operations
343 * depending on the special file...
345 struct file_operations def_chr_fops
= {
349 static struct kobject
*exact_match(dev_t dev
, int *part
, void *data
)
351 struct cdev
*p
= data
;
355 static int exact_lock(dev_t dev
, void *data
)
357 struct cdev
*p
= data
;
358 return cdev_get(p
) ? 0 : -1;
361 int cdev_add(struct cdev
*p
, dev_t dev
, unsigned count
)
365 return kobj_map(cdev_map
, dev
, count
, NULL
, exact_match
, exact_lock
, p
);
368 static void cdev_unmap(dev_t dev
, unsigned count
)
370 kobj_unmap(cdev_map
, dev
, count
);
373 void cdev_del(struct cdev
*p
)
375 cdev_unmap(p
->dev
, p
->count
);
376 kobject_put(&p
->kobj
);
380 static void cdev_default_release(struct kobject
*kobj
)
382 struct cdev
*p
= container_of(kobj
, struct cdev
, kobj
);
386 static void cdev_dynamic_release(struct kobject
*kobj
)
388 struct cdev
*p
= container_of(kobj
, struct cdev
, kobj
);
393 static struct kobj_type ktype_cdev_default
= {
394 .release
= cdev_default_release
,
397 static struct kobj_type ktype_cdev_dynamic
= {
398 .release
= cdev_dynamic_release
,
401 struct cdev
*cdev_alloc(void)
403 struct cdev
*p
= kmalloc(sizeof(struct cdev
), GFP_KERNEL
);
405 memset(p
, 0, sizeof(struct cdev
));
406 p
->kobj
.ktype
= &ktype_cdev_dynamic
;
407 INIT_LIST_HEAD(&p
->list
);
408 kobject_init(&p
->kobj
);
413 void cdev_init(struct cdev
*cdev
, struct file_operations
*fops
)
415 memset(cdev
, 0, sizeof *cdev
);
416 INIT_LIST_HEAD(&cdev
->list
);
417 cdev
->kobj
.ktype
= &ktype_cdev_default
;
418 kobject_init(&cdev
->kobj
);
422 static struct kobject
*base_probe(dev_t dev
, int *part
, void *data
)
424 if (request_module("char-major-%d-%d", MAJOR(dev
), MINOR(dev
)) > 0)
425 /* Make old-style 2.4 aliases work */
426 request_module("char-major-%d", MAJOR(dev
));
430 void __init
chrdev_init(void)
432 cdev_map
= kobj_map_init(base_probe
, &chrdevs_lock
);
436 /* Let modules do char dev stuff */
437 EXPORT_SYMBOL(register_chrdev_region
);
438 EXPORT_SYMBOL(unregister_chrdev_region
);
439 EXPORT_SYMBOL(alloc_chrdev_region
);
440 EXPORT_SYMBOL(cdev_init
);
441 EXPORT_SYMBOL(cdev_alloc
);
442 EXPORT_SYMBOL(cdev_del
);
443 EXPORT_SYMBOL(cdev_add
);
444 EXPORT_SYMBOL(register_chrdev
);
445 EXPORT_SYMBOL(unregister_chrdev
);