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/string.h>
15 #include <linux/errno.h>
16 #include <linux/module.h>
17 #include <linux/smp_lock.h>
18 #include <linux/devfs_fs_kernel.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 #define MAX_PROBE_HASH 255 /* random */
32 static rwlock_t chrdevs_lock
= RW_LOCK_UNLOCKED
;
34 static struct char_device_struct
{
35 struct char_device_struct
*next
;
37 unsigned int baseminor
;
40 struct file_operations
*fops
;
41 struct cdev
*cdev
; /* will die */
42 } *chrdevs
[MAX_PROBE_HASH
];
44 /* index in the above */
45 static inline int major_to_index(int major
)
47 return major
% MAX_PROBE_HASH
;
50 /* get char device names in somewhat random order */
51 int get_chrdev_list(char *page
)
53 struct char_device_struct
*cd
;
56 len
= sprintf(page
, "Character devices:\n");
58 read_lock(&chrdevs_lock
);
59 for (i
= 0; i
< ARRAY_SIZE(chrdevs
) ; i
++) {
60 for (cd
= chrdevs
[i
]; cd
; cd
= cd
->next
)
61 len
+= sprintf(page
+len
, "%3d %s\n",
64 read_unlock(&chrdevs_lock
);
70 * Register a single major with a specified minor range.
72 * If major == 0 this functions will dynamically allocate a major and return
75 * If major > 0 this function will attempt to reserve the passed range of
76 * minors and will return zero on success.
78 * Returns a -ve errno on failure.
80 static struct char_device_struct
*
81 __register_chrdev_region(unsigned int major
, unsigned int baseminor
,
82 int minorct
, const char *name
)
84 struct char_device_struct
*cd
, **cp
;
88 cd
= kmalloc(sizeof(struct char_device_struct
), GFP_KERNEL
);
90 return ERR_PTR(-ENOMEM
);
92 write_lock_irq(&chrdevs_lock
);
96 for (i
= ARRAY_SIZE(chrdevs
)-1; i
> 0; i
--) {
97 if (chrdevs
[i
] == NULL
)
110 cd
->baseminor
= baseminor
;
111 cd
->minorct
= minorct
;
114 i
= major_to_index(major
);
116 for (cp
= &chrdevs
[i
]; *cp
; cp
= &(*cp
)->next
)
117 if ((*cp
)->major
> major
||
118 ((*cp
)->major
== major
&& (*cp
)->baseminor
>= baseminor
))
120 if (*cp
&& (*cp
)->major
== major
&&
121 (*cp
)->baseminor
< baseminor
+ minorct
) {
127 write_unlock_irq(&chrdevs_lock
);
130 write_unlock_irq(&chrdevs_lock
);
135 static struct char_device_struct
*
136 __unregister_chrdev_region(unsigned major
, unsigned baseminor
, int minorct
)
138 struct char_device_struct
*cd
= NULL
, **cp
;
139 int i
= major_to_index(major
);
141 write_lock_irq(&chrdevs_lock
);
142 for (cp
= &chrdevs
[i
]; *cp
; cp
= &(*cp
)->next
)
143 if ((*cp
)->major
== major
&&
144 (*cp
)->baseminor
== baseminor
&&
145 (*cp
)->minorct
== minorct
)
151 write_unlock_irq(&chrdevs_lock
);
155 int register_chrdev_region(dev_t from
, unsigned count
, char *name
)
157 struct char_device_struct
*cd
;
158 dev_t to
= from
+ count
;
161 for (n
= from
; n
< to
; n
= next
) {
162 next
= MKDEV(MAJOR(n
)+1, 0);
165 cd
= __register_chrdev_region(MAJOR(n
), MINOR(n
),
173 for (n
= from
; n
< to
; n
= next
) {
174 next
= MKDEV(MAJOR(n
)+1, 0);
175 kfree(__unregister_chrdev_region(MAJOR(n
), MINOR(n
), next
- n
));
180 int alloc_chrdev_region(dev_t
*dev
, unsigned count
, char *name
)
182 struct char_device_struct
*cd
;
183 cd
= __register_chrdev_region(0, 0, count
, name
);
186 *dev
= MKDEV(cd
->major
, cd
->baseminor
);
190 int register_chrdev(unsigned int major
, const char *name
,
191 struct file_operations
*fops
)
193 struct char_device_struct
*cd
;
198 cd
= __register_chrdev_region(major
, 0, 256, name
);
206 cdev
->owner
= fops
->owner
;
208 strcpy(cdev
->kobj
.name
, name
);
209 for (s
= strchr(cdev
->kobj
.name
, '/'); s
; s
= strchr(s
, '/'))
212 err
= cdev_add(cdev
, MKDEV(cd
->major
, 0), 256);
218 return major
? 0 : cd
->major
;
220 kobject_put(&cdev
->kobj
);
222 __unregister_chrdev_region(cd
->major
, 0, 256);
226 void unregister_chrdev_region(dev_t from
, unsigned count
)
228 dev_t to
= from
+ count
;
231 for (n
= from
; n
< to
; n
= next
) {
232 next
= MKDEV(MAJOR(n
)+1, 0);
235 kfree(__unregister_chrdev_region(MAJOR(n
), MINOR(n
), next
- n
));
239 int unregister_chrdev(unsigned int major
, const char *name
)
241 struct char_device_struct
*cd
;
242 cdev_unmap(MKDEV(major
, 0), 256);
243 cd
= __unregister_chrdev_region(major
, 0, 256);
250 static spinlock_t cdev_lock
= SPIN_LOCK_UNLOCKED
;
252 * Called every time a character special file is opened
254 int chrdev_open(struct inode
* inode
, struct file
* filp
)
257 struct cdev
*new = NULL
;
260 spin_lock(&cdev_lock
);
263 struct kobject
*kobj
;
265 spin_unlock(&cdev_lock
);
266 kobj
= kobj_lookup(cdev_map
, kdev_t_to_nr(inode
->i_rdev
), &idx
);
269 new = container_of(kobj
, struct cdev
, kobj
);
270 spin_lock(&cdev_lock
);
273 inode
->i_cdev
= p
= new;
274 inode
->i_cindex
= idx
;
275 list_add(&inode
->i_devices
, &p
->list
);
277 } else if (!cdev_get(p
))
279 } else if (!cdev_get(p
))
281 spin_unlock(&cdev_lock
);
285 filp
->f_op
= fops_get(p
->ops
);
290 if (filp
->f_op
->open
) {
292 ret
= filp
->f_op
->open(inode
,filp
);
300 void cd_forget(struct inode
*inode
)
302 spin_lock(&cdev_lock
);
303 list_del_init(&inode
->i_devices
);
304 inode
->i_cdev
= NULL
;
305 spin_unlock(&cdev_lock
);
308 void cdev_purge(struct cdev
*cdev
)
310 spin_lock(&cdev_lock
);
311 while (!list_empty(&cdev
->list
)) {
313 inode
= container_of(cdev
->list
.next
, struct inode
, i_devices
);
314 list_del_init(&inode
->i_devices
);
315 inode
->i_cdev
= NULL
;
317 spin_unlock(&cdev_lock
);
321 * Dummy default file-operations: the only thing this does
322 * is contain the open that then fills in the correct operations
323 * depending on the special file...
325 struct file_operations def_chr_fops
= {
329 const char *cdevname(kdev_t dev
)
331 static char buffer
[40];
332 const char *name
= "unknown-char";
333 unsigned int major
= major(dev
);
334 unsigned int minor
= minor(dev
);
335 int i
= major_to_index(major
);
336 struct char_device_struct
*cd
;
338 read_lock(&chrdevs_lock
);
339 for (cd
= chrdevs
[i
]; cd
; cd
= cd
->next
)
340 if (cd
->major
== major
)
344 sprintf(buffer
, "%s(%d,%d)", name
, major
, minor
);
345 read_unlock(&chrdevs_lock
);
350 static struct kobject
*exact_match(dev_t dev
, int *part
, void *data
)
352 struct cdev
*p
= data
;
356 static int exact_lock(dev_t dev
, void *data
)
358 struct cdev
*p
= data
;
359 return cdev_get(p
) ? 0 : -1;
362 int cdev_add(struct cdev
*p
, dev_t dev
, unsigned count
)
364 int err
= kobject_add(&p
->kobj
);
367 return kobj_map(cdev_map
, dev
, count
, NULL
, exact_match
, exact_lock
, p
);
370 void cdev_unmap(dev_t dev
, unsigned count
)
372 kobj_unmap(cdev_map
, dev
, count
);
375 void cdev_del(struct cdev
*p
)
377 kobject_del(&p
->kobj
);
378 kobject_put(&p
->kobj
);
381 struct kobject
*cdev_get(struct cdev
*p
)
383 struct module
*owner
= p
->owner
;
384 struct kobject
*kobj
;
386 if (owner
&& !try_module_get(owner
))
388 kobj
= kobject_get(&p
->kobj
);
394 void cdev_put(struct cdev
*p
)
397 kobject_put(&p
->kobj
);
398 module_put(p
->owner
);
402 static decl_subsys(cdev
, NULL
, NULL
);
404 static void cdev_default_release(struct kobject
*kobj
)
406 struct cdev
*p
= container_of(kobj
, struct cdev
, kobj
);
410 static void cdev_dynamic_release(struct kobject
*kobj
)
412 struct cdev
*p
= container_of(kobj
, struct cdev
, kobj
);
417 static struct kobj_type ktype_cdev_default
= {
418 .release
= cdev_default_release
,
421 static struct kobj_type ktype_cdev_dynamic
= {
422 .release
= cdev_dynamic_release
,
425 static struct kset kset_dynamic
= {
426 .subsys
= &cdev_subsys
,
427 .kobj
= {.name
= "major",},
428 .ktype
= &ktype_cdev_dynamic
,
431 struct cdev
*cdev_alloc(void)
433 struct cdev
*p
= kmalloc(sizeof(struct cdev
), GFP_KERNEL
);
435 memset(p
, 0, sizeof(struct cdev
));
436 p
->kobj
.kset
= &kset_dynamic
;
437 INIT_LIST_HEAD(&p
->list
);
438 kobject_init(&p
->kobj
);
443 void cdev_init(struct cdev
*cdev
, struct file_operations
*fops
)
445 INIT_LIST_HEAD(&cdev
->list
);
446 kobj_set_kset_s(cdev
, cdev_subsys
);
447 cdev
->kobj
.ktype
= &ktype_cdev_default
;
448 kobject_init(&cdev
->kobj
);
452 static struct kobject
*base_probe(dev_t dev
, int *part
, void *data
)
455 sprintf(name
, "char-major-%d", MAJOR(dev
));
456 request_module(name
);
460 void __init
chrdev_init(void)
462 subsystem_register(&cdev_subsys
);
463 kset_register(&kset_dynamic
);
464 cdev_map
= kobj_map_init(base_probe
, &cdev_subsys
);