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 rwlock_t chrdevs_lock
= RW_LOCK_UNLOCKED
;
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");
57 read_lock(&chrdevs_lock
);
58 for (i
= 0; i
< ARRAY_SIZE(chrdevs
) ; i
++) {
59 for (cd
= chrdevs
[i
]; cd
; cd
= cd
->next
)
60 len
+= sprintf(page
+len
, "%3d %s\n",
63 read_unlock(&chrdevs_lock
);
69 * Register a single major with a specified minor range.
71 * If major == 0 this functions will dynamically allocate a major and return
74 * If major > 0 this function will attempt to reserve the passed range of
75 * minors and will return zero on success.
77 * Returns a -ve errno on failure.
79 static struct char_device_struct
*
80 __register_chrdev_region(unsigned int major
, unsigned int baseminor
,
81 int minorct
, const char *name
)
83 struct char_device_struct
*cd
, **cp
;
87 cd
= kmalloc(sizeof(struct char_device_struct
), GFP_KERNEL
);
89 return ERR_PTR(-ENOMEM
);
91 memset(cd
, 0, sizeof(struct char_device_struct
));
93 write_lock_irq(&chrdevs_lock
);
97 for (i
= ARRAY_SIZE(chrdevs
)-1; i
> 0; i
--) {
98 if (chrdevs
[i
] == NULL
)
111 cd
->baseminor
= baseminor
;
112 cd
->minorct
= minorct
;
115 i
= major_to_index(major
);
117 for (cp
= &chrdevs
[i
]; *cp
; cp
= &(*cp
)->next
)
118 if ((*cp
)->major
> major
||
119 ((*cp
)->major
== major
&& (*cp
)->baseminor
>= baseminor
))
121 if (*cp
&& (*cp
)->major
== major
&&
122 (*cp
)->baseminor
< baseminor
+ minorct
) {
128 write_unlock_irq(&chrdevs_lock
);
131 write_unlock_irq(&chrdevs_lock
);
136 static struct char_device_struct
*
137 __unregister_chrdev_region(unsigned major
, unsigned baseminor
, int minorct
)
139 struct char_device_struct
*cd
= NULL
, **cp
;
140 int i
= major_to_index(major
);
142 write_lock_irq(&chrdevs_lock
);
143 for (cp
= &chrdevs
[i
]; *cp
; cp
= &(*cp
)->next
)
144 if ((*cp
)->major
== major
&&
145 (*cp
)->baseminor
== baseminor
&&
146 (*cp
)->minorct
== minorct
)
152 write_unlock_irq(&chrdevs_lock
);
156 int register_chrdev_region(dev_t from
, unsigned count
, char *name
)
158 struct char_device_struct
*cd
;
159 dev_t to
= from
+ count
;
162 for (n
= from
; n
< to
; n
= next
) {
163 next
= MKDEV(MAJOR(n
)+1, 0);
166 cd
= __register_chrdev_region(MAJOR(n
), MINOR(n
),
174 for (n
= from
; n
< to
; n
= next
) {
175 next
= MKDEV(MAJOR(n
)+1, 0);
176 kfree(__unregister_chrdev_region(MAJOR(n
), MINOR(n
), next
- n
));
181 int alloc_chrdev_region(dev_t
*dev
, unsigned baseminor
, unsigned count
, char *name
)
183 struct char_device_struct
*cd
;
184 cd
= __register_chrdev_region(0, baseminor
, count
, name
);
187 *dev
= MKDEV(cd
->major
, cd
->baseminor
);
191 int register_chrdev(unsigned int major
, const char *name
,
192 struct file_operations
*fops
)
194 struct char_device_struct
*cd
;
199 cd
= __register_chrdev_region(major
, 0, 256, name
);
207 cdev
->owner
= fops
->owner
;
209 strcpy(cdev
->kobj
.name
, name
);
210 for (s
= strchr(cdev
->kobj
.name
, '/'); s
; s
= strchr(s
, '/'))
213 err
= cdev_add(cdev
, MKDEV(cd
->major
, 0), 256);
219 return major
? 0 : cd
->major
;
221 kobject_put(&cdev
->kobj
);
223 kfree(__unregister_chrdev_region(cd
->major
, 0, 256));
227 void unregister_chrdev_region(dev_t from
, unsigned count
)
229 dev_t to
= from
+ count
;
232 for (n
= from
; n
< to
; n
= next
) {
233 next
= MKDEV(MAJOR(n
)+1, 0);
236 kfree(__unregister_chrdev_region(MAJOR(n
), MINOR(n
), next
- n
));
240 int unregister_chrdev(unsigned int major
, const char *name
)
242 struct char_device_struct
*cd
;
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
, 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 static struct kobject
*exact_match(dev_t dev
, int *part
, void *data
)
331 struct cdev
*p
= data
;
335 static int exact_lock(dev_t dev
, void *data
)
337 struct cdev
*p
= data
;
338 return cdev_get(p
) ? 0 : -1;
341 int cdev_add(struct cdev
*p
, dev_t dev
, unsigned count
)
345 return kobj_map(cdev_map
, dev
, count
, NULL
, exact_match
, exact_lock
, p
);
348 static void cdev_unmap(dev_t dev
, unsigned count
)
350 kobj_unmap(cdev_map
, dev
, count
);
353 void cdev_del(struct cdev
*p
)
355 cdev_unmap(p
->dev
, p
->count
);
356 kobject_put(&p
->kobj
);
359 struct kobject
*cdev_get(struct cdev
*p
)
361 struct module
*owner
= p
->owner
;
362 struct kobject
*kobj
;
364 if (owner
&& !try_module_get(owner
))
366 kobj
= kobject_get(&p
->kobj
);
372 void cdev_put(struct cdev
*p
)
375 kobject_put(&p
->kobj
);
376 module_put(p
->owner
);
380 static decl_subsys(cdev
, NULL
, NULL
);
382 static void cdev_default_release(struct kobject
*kobj
)
384 struct cdev
*p
= container_of(kobj
, struct cdev
, kobj
);
388 static void cdev_dynamic_release(struct kobject
*kobj
)
390 struct cdev
*p
= container_of(kobj
, struct cdev
, kobj
);
395 static struct kobj_type ktype_cdev_default
= {
396 .release
= cdev_default_release
,
399 static struct kobj_type ktype_cdev_dynamic
= {
400 .release
= cdev_dynamic_release
,
403 struct cdev
*cdev_alloc(void)
405 struct cdev
*p
= kmalloc(sizeof(struct cdev
), GFP_KERNEL
);
407 memset(p
, 0, sizeof(struct cdev
));
408 p
->kobj
.ktype
= &ktype_cdev_dynamic
;
409 INIT_LIST_HEAD(&p
->list
);
410 kobject_init(&p
->kobj
);
415 void cdev_init(struct cdev
*cdev
, struct file_operations
*fops
)
417 INIT_LIST_HEAD(&cdev
->list
);
418 cdev
->kobj
.ktype
= &ktype_cdev_default
;
419 kobject_init(&cdev
->kobj
);
423 static struct kobject
*base_probe(dev_t dev
, int *part
, void *data
)
425 if (request_module("char-major-%d-%d", MAJOR(dev
), MINOR(dev
)) > 0)
426 /* Make old-style 2.4 aliases work */
427 request_module("char-major-%d", MAJOR(dev
));
431 void __init
chrdev_init(void)
434 * Keep cdev_subsys around because (and only because) the kobj_map code
435 * depends on the rwsem it contains. We don't make it public in sysfs,
438 subsystem_init(&cdev_subsys
);
439 cdev_map
= kobj_map_init(base_probe
, &cdev_subsys
);
443 /* Let modules do char dev stuff */
444 EXPORT_SYMBOL(register_chrdev_region
);
445 EXPORT_SYMBOL(unregister_chrdev_region
);
446 EXPORT_SYMBOL(alloc_chrdev_region
);
447 EXPORT_SYMBOL(cdev_init
);
448 EXPORT_SYMBOL(cdev_alloc
);
449 EXPORT_SYMBOL(cdev_get
);
450 EXPORT_SYMBOL(cdev_put
);
451 EXPORT_SYMBOL(cdev_del
);
452 EXPORT_SYMBOL(cdev_add
);
453 EXPORT_SYMBOL(register_chrdev
);
454 EXPORT_SYMBOL(unregister_chrdev
);