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>
19 #include <linux/kobject.h>
20 #include <linux/kobj_map.h>
21 #include <linux/cdev.h>
24 #include <linux/kmod.h>
27 static struct kobj_map
*cdev_map
;
29 #define MAX_PROBE_HASH 255 /* random */
31 static DECLARE_MUTEX(chrdevs_lock
);
33 static struct char_device_struct
{
34 struct char_device_struct
*next
;
36 unsigned int baseminor
;
39 struct file_operations
*fops
;
40 struct cdev
*cdev
; /* will die */
41 } *chrdevs
[MAX_PROBE_HASH
];
43 /* index in the above */
44 static inline int major_to_index(int major
)
46 return major
% MAX_PROBE_HASH
;
49 /* get char device names in somewhat random order */
50 int get_chrdev_list(char *page
)
52 struct char_device_struct
*cd
;
55 len
= sprintf(page
, "Character devices:\n");
58 for (i
= 0; i
< ARRAY_SIZE(chrdevs
) ; i
++) {
59 for (cd
= chrdevs
[i
]; cd
; cd
= cd
->next
) {
61 * if the current name, plus the 5 extra characters
62 * in the device line for this entry
63 * would run us off the page, we're done
65 if ((len
+strlen(cd
->name
) + 5) >= PAGE_SIZE
)
69 len
+= sprintf(page
+len
, "%3d %s\n",
80 * Register a single major with a specified minor range.
82 * If major == 0 this functions will dynamically allocate a major and return
85 * If major > 0 this function will attempt to reserve the passed range of
86 * minors and will return zero on success.
88 * Returns a -ve errno on failure.
90 static struct char_device_struct
*
91 __register_chrdev_region(unsigned int major
, unsigned int baseminor
,
92 int minorct
, const char *name
)
94 struct char_device_struct
*cd
, **cp
;
98 cd
= kmalloc(sizeof(struct char_device_struct
), GFP_KERNEL
);
100 return ERR_PTR(-ENOMEM
);
102 memset(cd
, 0, sizeof(struct char_device_struct
));
108 for (i
= ARRAY_SIZE(chrdevs
)-1; i
> 0; i
--) {
109 if (chrdevs
[i
] == NULL
)
122 cd
->baseminor
= baseminor
;
123 cd
->minorct
= minorct
;
126 i
= major_to_index(major
);
128 for (cp
= &chrdevs
[i
]; *cp
; cp
= &(*cp
)->next
)
129 if ((*cp
)->major
> major
||
130 ((*cp
)->major
== major
&& (*cp
)->baseminor
>= baseminor
))
132 if (*cp
&& (*cp
)->major
== major
&&
133 (*cp
)->baseminor
< baseminor
+ minorct
) {
147 static struct char_device_struct
*
148 __unregister_chrdev_region(unsigned major
, unsigned baseminor
, int minorct
)
150 struct char_device_struct
*cd
= NULL
, **cp
;
151 int i
= major_to_index(major
);
154 for (cp
= &chrdevs
[i
]; *cp
; cp
= &(*cp
)->next
)
155 if ((*cp
)->major
== major
&&
156 (*cp
)->baseminor
== baseminor
&&
157 (*cp
)->minorct
== minorct
)
167 int register_chrdev_region(dev_t from
, unsigned count
, const char *name
)
169 struct char_device_struct
*cd
;
170 dev_t to
= from
+ count
;
173 for (n
= from
; n
< to
; n
= next
) {
174 next
= MKDEV(MAJOR(n
)+1, 0);
177 cd
= __register_chrdev_region(MAJOR(n
), MINOR(n
),
185 for (n
= from
; n
< to
; n
= next
) {
186 next
= MKDEV(MAJOR(n
)+1, 0);
187 kfree(__unregister_chrdev_region(MAJOR(n
), MINOR(n
), next
- n
));
192 int alloc_chrdev_region(dev_t
*dev
, unsigned baseminor
, unsigned count
,
195 struct char_device_struct
*cd
;
196 cd
= __register_chrdev_region(0, baseminor
, count
, name
);
199 *dev
= MKDEV(cd
->major
, cd
->baseminor
);
203 int register_chrdev(unsigned int major
, const char *name
,
204 struct file_operations
*fops
)
206 struct char_device_struct
*cd
;
211 cd
= __register_chrdev_region(major
, 0, 256, name
);
219 cdev
->owner
= fops
->owner
;
221 kobject_set_name(&cdev
->kobj
, "%s", name
);
222 for (s
= strchr(kobject_name(&cdev
->kobj
),'/'); s
; s
= strchr(s
, '/'))
225 err
= cdev_add(cdev
, MKDEV(cd
->major
, 0), 256);
231 return major
? 0 : cd
->major
;
233 kobject_put(&cdev
->kobj
);
235 kfree(__unregister_chrdev_region(cd
->major
, 0, 256));
239 void unregister_chrdev_region(dev_t from
, unsigned count
)
241 dev_t to
= from
+ count
;
244 for (n
= from
; n
< to
; n
= next
) {
245 next
= MKDEV(MAJOR(n
)+1, 0);
248 kfree(__unregister_chrdev_region(MAJOR(n
), MINOR(n
), next
- n
));
252 int unregister_chrdev(unsigned int major
, const char *name
)
254 struct char_device_struct
*cd
;
255 cd
= __unregister_chrdev_region(major
, 0, 256);
262 static DEFINE_SPINLOCK(cdev_lock
);
264 static struct kobject
*cdev_get(struct cdev
*p
)
266 struct module
*owner
= p
->owner
;
267 struct kobject
*kobj
;
269 if (owner
&& !try_module_get(owner
))
271 kobj
= kobject_get(&p
->kobj
);
277 void cdev_put(struct cdev
*p
)
280 struct module
*owner
= p
->owner
;
281 kobject_put(&p
->kobj
);
287 * Called every time a character special file is opened
289 int chrdev_open(struct inode
* inode
, struct file
* filp
)
292 struct cdev
*new = NULL
;
295 spin_lock(&cdev_lock
);
298 struct kobject
*kobj
;
300 spin_unlock(&cdev_lock
);
301 kobj
= kobj_lookup(cdev_map
, inode
->i_rdev
, &idx
);
304 new = container_of(kobj
, struct cdev
, kobj
);
305 spin_lock(&cdev_lock
);
308 inode
->i_cdev
= p
= new;
309 inode
->i_cindex
= idx
;
310 list_add(&inode
->i_devices
, &p
->list
);
312 } else if (!cdev_get(p
))
314 } else if (!cdev_get(p
))
316 spin_unlock(&cdev_lock
);
320 filp
->f_op
= fops_get(p
->ops
);
325 if (filp
->f_op
->open
) {
327 ret
= filp
->f_op
->open(inode
,filp
);
335 void cd_forget(struct inode
*inode
)
337 spin_lock(&cdev_lock
);
338 list_del_init(&inode
->i_devices
);
339 inode
->i_cdev
= NULL
;
340 spin_unlock(&cdev_lock
);
343 static void cdev_purge(struct cdev
*cdev
)
345 spin_lock(&cdev_lock
);
346 while (!list_empty(&cdev
->list
)) {
348 inode
= container_of(cdev
->list
.next
, struct inode
, i_devices
);
349 list_del_init(&inode
->i_devices
);
350 inode
->i_cdev
= NULL
;
352 spin_unlock(&cdev_lock
);
356 * Dummy default file-operations: the only thing this does
357 * is contain the open that then fills in the correct operations
358 * depending on the special file...
360 struct file_operations def_chr_fops
= {
364 static struct kobject
*exact_match(dev_t dev
, int *part
, void *data
)
366 struct cdev
*p
= data
;
370 static int exact_lock(dev_t dev
, void *data
)
372 struct cdev
*p
= data
;
373 return cdev_get(p
) ? 0 : -1;
376 int cdev_add(struct cdev
*p
, dev_t dev
, unsigned count
)
380 return kobj_map(cdev_map
, dev
, count
, NULL
, exact_match
, exact_lock
, p
);
383 static void cdev_unmap(dev_t dev
, unsigned count
)
385 kobj_unmap(cdev_map
, dev
, count
);
388 void cdev_del(struct cdev
*p
)
390 cdev_unmap(p
->dev
, p
->count
);
391 kobject_put(&p
->kobj
);
395 static void cdev_default_release(struct kobject
*kobj
)
397 struct cdev
*p
= container_of(kobj
, struct cdev
, kobj
);
401 static void cdev_dynamic_release(struct kobject
*kobj
)
403 struct cdev
*p
= container_of(kobj
, struct cdev
, kobj
);
408 static struct kobj_type ktype_cdev_default
= {
409 .release
= cdev_default_release
,
412 static struct kobj_type ktype_cdev_dynamic
= {
413 .release
= cdev_dynamic_release
,
416 struct cdev
*cdev_alloc(void)
418 struct cdev
*p
= kmalloc(sizeof(struct cdev
), GFP_KERNEL
);
420 memset(p
, 0, sizeof(struct cdev
));
421 p
->kobj
.ktype
= &ktype_cdev_dynamic
;
422 INIT_LIST_HEAD(&p
->list
);
423 kobject_init(&p
->kobj
);
428 void cdev_init(struct cdev
*cdev
, struct file_operations
*fops
)
430 memset(cdev
, 0, sizeof *cdev
);
431 INIT_LIST_HEAD(&cdev
->list
);
432 cdev
->kobj
.ktype
= &ktype_cdev_default
;
433 kobject_init(&cdev
->kobj
);
437 static struct kobject
*base_probe(dev_t dev
, int *part
, void *data
)
439 if (request_module("char-major-%d-%d", MAJOR(dev
), MINOR(dev
)) > 0)
440 /* Make old-style 2.4 aliases work */
441 request_module("char-major-%d", MAJOR(dev
));
445 void __init
chrdev_init(void)
447 cdev_map
= kobj_map_init(base_probe
, &chrdevs_lock
);
451 /* Let modules do char dev stuff */
452 EXPORT_SYMBOL(register_chrdev_region
);
453 EXPORT_SYMBOL(unregister_chrdev_region
);
454 EXPORT_SYMBOL(alloc_chrdev_region
);
455 EXPORT_SYMBOL(cdev_init
);
456 EXPORT_SYMBOL(cdev_alloc
);
457 EXPORT_SYMBOL(cdev_del
);
458 EXPORT_SYMBOL(cdev_add
);
459 EXPORT_SYMBOL(register_chrdev
);
460 EXPORT_SYMBOL(unregister_chrdev
);