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>
22 #include <linux/mutex.h>
25 #include <linux/kmod.h>
28 static struct kobj_map
*cdev_map
;
30 #define MAX_PROBE_HASH 255 /* random */
32 static DEFINE_MUTEX(chrdevs_lock
);
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
;
52 struct char_device_struct
*cd
;
55 void *get_next_chrdev(void *dev
)
57 struct chrdev_info
*info
;
60 info
= kmalloc(sizeof(*info
), GFP_KERNEL
);
64 info
->cd
= chrdevs
[info
->index
];
71 while (info
->index
< ARRAY_SIZE(chrdevs
)) {
73 info
->cd
= info
->cd
->next
;
77 * No devices on this chain, move to the next
80 info
->cd
= (info
->index
< ARRAY_SIZE(chrdevs
)) ?
81 chrdevs
[info
->index
] : NULL
;
90 void *acquire_chrdev_list(void)
92 mutex_lock(&chrdevs_lock
);
93 return get_next_chrdev(NULL
);
96 void release_chrdev_list(void *dev
)
98 mutex_unlock(&chrdevs_lock
);
103 int count_chrdev_list(void)
105 struct char_device_struct
*cd
;
110 for (i
= 0; i
< ARRAY_SIZE(chrdevs
) ; i
++) {
111 for (cd
= chrdevs
[i
]; cd
; cd
= cd
->next
)
118 int get_chrdev_info(void *dev
, int *major
, char **name
)
120 struct chrdev_info
*info
= dev
;
122 if (info
->cd
== NULL
)
125 *major
= info
->cd
->major
;
126 *name
= info
->cd
->name
;
131 * Register a single major with a specified minor range.
133 * If major == 0 this functions will dynamically allocate a major and return
136 * If major > 0 this function will attempt to reserve the passed range of
137 * minors and will return zero on success.
139 * Returns a -ve errno on failure.
141 static struct char_device_struct
*
142 __register_chrdev_region(unsigned int major
, unsigned int baseminor
,
143 int minorct
, const char *name
)
145 struct char_device_struct
*cd
, **cp
;
149 cd
= kzalloc(sizeof(struct char_device_struct
), GFP_KERNEL
);
151 return ERR_PTR(-ENOMEM
);
153 mutex_lock(&chrdevs_lock
);
157 for (i
= ARRAY_SIZE(chrdevs
)-1; i
> 0; i
--) {
158 if (chrdevs
[i
] == NULL
)
171 cd
->baseminor
= baseminor
;
172 cd
->minorct
= minorct
;
173 strncpy(cd
->name
,name
, 64);
175 i
= major_to_index(major
);
177 for (cp
= &chrdevs
[i
]; *cp
; cp
= &(*cp
)->next
)
178 if ((*cp
)->major
> major
||
179 ((*cp
)->major
== major
&& (*cp
)->baseminor
>= baseminor
))
181 if (*cp
&& (*cp
)->major
== major
&&
182 (*cp
)->baseminor
< baseminor
+ minorct
) {
188 mutex_unlock(&chrdevs_lock
);
191 mutex_unlock(&chrdevs_lock
);
196 static struct char_device_struct
*
197 __unregister_chrdev_region(unsigned major
, unsigned baseminor
, int minorct
)
199 struct char_device_struct
*cd
= NULL
, **cp
;
200 int i
= major_to_index(major
);
202 mutex_lock(&chrdevs_lock
);
203 for (cp
= &chrdevs
[i
]; *cp
; cp
= &(*cp
)->next
)
204 if ((*cp
)->major
== major
&&
205 (*cp
)->baseminor
== baseminor
&&
206 (*cp
)->minorct
== minorct
)
212 mutex_unlock(&chrdevs_lock
);
216 int register_chrdev_region(dev_t from
, unsigned count
, const char *name
)
218 struct char_device_struct
*cd
;
219 dev_t to
= from
+ count
;
222 for (n
= from
; n
< to
; n
= next
) {
223 next
= MKDEV(MAJOR(n
)+1, 0);
226 cd
= __register_chrdev_region(MAJOR(n
), MINOR(n
),
234 for (n
= from
; n
< to
; n
= next
) {
235 next
= MKDEV(MAJOR(n
)+1, 0);
236 kfree(__unregister_chrdev_region(MAJOR(n
), MINOR(n
), next
- n
));
241 int alloc_chrdev_region(dev_t
*dev
, unsigned baseminor
, unsigned count
,
244 struct char_device_struct
*cd
;
245 cd
= __register_chrdev_region(0, baseminor
, count
, name
);
248 *dev
= MKDEV(cd
->major
, cd
->baseminor
);
252 int register_chrdev(unsigned int major
, const char *name
,
253 struct file_operations
*fops
)
255 struct char_device_struct
*cd
;
260 cd
= __register_chrdev_region(major
, 0, 256, name
);
268 cdev
->owner
= fops
->owner
;
270 kobject_set_name(&cdev
->kobj
, "%s", name
);
271 for (s
= strchr(kobject_name(&cdev
->kobj
),'/'); s
; s
= strchr(s
, '/'))
274 err
= cdev_add(cdev
, MKDEV(cd
->major
, 0), 256);
280 return major
? 0 : cd
->major
;
282 kobject_put(&cdev
->kobj
);
284 kfree(__unregister_chrdev_region(cd
->major
, 0, 256));
288 void unregister_chrdev_region(dev_t from
, unsigned count
)
290 dev_t to
= from
+ count
;
293 for (n
= from
; n
< to
; n
= next
) {
294 next
= MKDEV(MAJOR(n
)+1, 0);
297 kfree(__unregister_chrdev_region(MAJOR(n
), MINOR(n
), next
- n
));
301 int unregister_chrdev(unsigned int major
, const char *name
)
303 struct char_device_struct
*cd
;
304 cd
= __unregister_chrdev_region(major
, 0, 256);
311 static DEFINE_SPINLOCK(cdev_lock
);
313 static struct kobject
*cdev_get(struct cdev
*p
)
315 struct module
*owner
= p
->owner
;
316 struct kobject
*kobj
;
318 if (owner
&& !try_module_get(owner
))
320 kobj
= kobject_get(&p
->kobj
);
326 void cdev_put(struct cdev
*p
)
329 struct module
*owner
= p
->owner
;
330 kobject_put(&p
->kobj
);
336 * Called every time a character special file is opened
338 int chrdev_open(struct inode
* inode
, struct file
* filp
)
341 struct cdev
*new = NULL
;
344 spin_lock(&cdev_lock
);
347 struct kobject
*kobj
;
349 spin_unlock(&cdev_lock
);
350 kobj
= kobj_lookup(cdev_map
, inode
->i_rdev
, &idx
);
353 new = container_of(kobj
, struct cdev
, kobj
);
354 spin_lock(&cdev_lock
);
357 inode
->i_cdev
= p
= new;
358 inode
->i_cindex
= idx
;
359 list_add(&inode
->i_devices
, &p
->list
);
361 } else if (!cdev_get(p
))
363 } else if (!cdev_get(p
))
365 spin_unlock(&cdev_lock
);
369 filp
->f_op
= fops_get(p
->ops
);
374 if (filp
->f_op
->open
) {
376 ret
= filp
->f_op
->open(inode
,filp
);
384 void cd_forget(struct inode
*inode
)
386 spin_lock(&cdev_lock
);
387 list_del_init(&inode
->i_devices
);
388 inode
->i_cdev
= NULL
;
389 spin_unlock(&cdev_lock
);
392 static void cdev_purge(struct cdev
*cdev
)
394 spin_lock(&cdev_lock
);
395 while (!list_empty(&cdev
->list
)) {
397 inode
= container_of(cdev
->list
.next
, struct inode
, i_devices
);
398 list_del_init(&inode
->i_devices
);
399 inode
->i_cdev
= NULL
;
401 spin_unlock(&cdev_lock
);
405 * Dummy default file-operations: the only thing this does
406 * is contain the open that then fills in the correct operations
407 * depending on the special file...
409 struct file_operations def_chr_fops
= {
413 static struct kobject
*exact_match(dev_t dev
, int *part
, void *data
)
415 struct cdev
*p
= data
;
419 static int exact_lock(dev_t dev
, void *data
)
421 struct cdev
*p
= data
;
422 return cdev_get(p
) ? 0 : -1;
425 int cdev_add(struct cdev
*p
, dev_t dev
, unsigned count
)
429 return kobj_map(cdev_map
, dev
, count
, NULL
, exact_match
, exact_lock
, p
);
432 static void cdev_unmap(dev_t dev
, unsigned count
)
434 kobj_unmap(cdev_map
, dev
, count
);
437 void cdev_del(struct cdev
*p
)
439 cdev_unmap(p
->dev
, p
->count
);
440 kobject_put(&p
->kobj
);
444 static void cdev_default_release(struct kobject
*kobj
)
446 struct cdev
*p
= container_of(kobj
, struct cdev
, kobj
);
450 static void cdev_dynamic_release(struct kobject
*kobj
)
452 struct cdev
*p
= container_of(kobj
, struct cdev
, kobj
);
457 static struct kobj_type ktype_cdev_default
= {
458 .release
= cdev_default_release
,
461 static struct kobj_type ktype_cdev_dynamic
= {
462 .release
= cdev_dynamic_release
,
465 struct cdev
*cdev_alloc(void)
467 struct cdev
*p
= kzalloc(sizeof(struct cdev
), GFP_KERNEL
);
469 p
->kobj
.ktype
= &ktype_cdev_dynamic
;
470 INIT_LIST_HEAD(&p
->list
);
471 kobject_init(&p
->kobj
);
476 void cdev_init(struct cdev
*cdev
, struct file_operations
*fops
)
478 memset(cdev
, 0, sizeof *cdev
);
479 INIT_LIST_HEAD(&cdev
->list
);
480 cdev
->kobj
.ktype
= &ktype_cdev_default
;
481 kobject_init(&cdev
->kobj
);
485 static struct kobject
*base_probe(dev_t dev
, int *part
, void *data
)
487 if (request_module("char-major-%d-%d", MAJOR(dev
), MINOR(dev
)) > 0)
488 /* Make old-style 2.4 aliases work */
489 request_module("char-major-%d", MAJOR(dev
));
493 void __init
chrdev_init(void)
495 cdev_map
= kobj_map_init(base_probe
, &chrdevs_lock
);
499 /* Let modules do char dev stuff */
500 EXPORT_SYMBOL(register_chrdev_region
);
501 EXPORT_SYMBOL(unregister_chrdev_region
);
502 EXPORT_SYMBOL(alloc_chrdev_region
);
503 EXPORT_SYMBOL(cdev_init
);
504 EXPORT_SYMBOL(cdev_alloc
);
505 EXPORT_SYMBOL(cdev_del
);
506 EXPORT_SYMBOL(cdev_add
);
507 EXPORT_SYMBOL(register_chrdev
);
508 EXPORT_SYMBOL(unregister_chrdev
);