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
= kmalloc(sizeof(struct char_device_struct
), GFP_KERNEL
);
151 return ERR_PTR(-ENOMEM
);
153 memset(cd
, 0, sizeof(struct char_device_struct
));
155 mutex_lock(&chrdevs_lock
);
159 for (i
= ARRAY_SIZE(chrdevs
)-1; i
> 0; i
--) {
160 if (chrdevs
[i
] == NULL
)
173 cd
->baseminor
= baseminor
;
174 cd
->minorct
= minorct
;
175 strncpy(cd
->name
,name
, 64);
177 i
= major_to_index(major
);
179 for (cp
= &chrdevs
[i
]; *cp
; cp
= &(*cp
)->next
)
180 if ((*cp
)->major
> major
||
181 ((*cp
)->major
== major
&& (*cp
)->baseminor
>= baseminor
))
183 if (*cp
&& (*cp
)->major
== major
&&
184 (*cp
)->baseminor
< baseminor
+ minorct
) {
190 mutex_unlock(&chrdevs_lock
);
193 mutex_unlock(&chrdevs_lock
);
198 static struct char_device_struct
*
199 __unregister_chrdev_region(unsigned major
, unsigned baseminor
, int minorct
)
201 struct char_device_struct
*cd
= NULL
, **cp
;
202 int i
= major_to_index(major
);
204 mutex_lock(&chrdevs_lock
);
205 for (cp
= &chrdevs
[i
]; *cp
; cp
= &(*cp
)->next
)
206 if ((*cp
)->major
== major
&&
207 (*cp
)->baseminor
== baseminor
&&
208 (*cp
)->minorct
== minorct
)
214 mutex_unlock(&chrdevs_lock
);
218 int register_chrdev_region(dev_t from
, unsigned count
, const char *name
)
220 struct char_device_struct
*cd
;
221 dev_t to
= from
+ count
;
224 for (n
= from
; n
< to
; n
= next
) {
225 next
= MKDEV(MAJOR(n
)+1, 0);
228 cd
= __register_chrdev_region(MAJOR(n
), MINOR(n
),
236 for (n
= from
; n
< to
; n
= next
) {
237 next
= MKDEV(MAJOR(n
)+1, 0);
238 kfree(__unregister_chrdev_region(MAJOR(n
), MINOR(n
), next
- n
));
243 int alloc_chrdev_region(dev_t
*dev
, unsigned baseminor
, unsigned count
,
246 struct char_device_struct
*cd
;
247 cd
= __register_chrdev_region(0, baseminor
, count
, name
);
250 *dev
= MKDEV(cd
->major
, cd
->baseminor
);
254 int register_chrdev(unsigned int major
, const char *name
,
255 struct file_operations
*fops
)
257 struct char_device_struct
*cd
;
262 cd
= __register_chrdev_region(major
, 0, 256, name
);
270 cdev
->owner
= fops
->owner
;
272 kobject_set_name(&cdev
->kobj
, "%s", name
);
273 for (s
= strchr(kobject_name(&cdev
->kobj
),'/'); s
; s
= strchr(s
, '/'))
276 err
= cdev_add(cdev
, MKDEV(cd
->major
, 0), 256);
282 return major
? 0 : cd
->major
;
284 kobject_put(&cdev
->kobj
);
286 kfree(__unregister_chrdev_region(cd
->major
, 0, 256));
290 void unregister_chrdev_region(dev_t from
, unsigned count
)
292 dev_t to
= from
+ count
;
295 for (n
= from
; n
< to
; n
= next
) {
296 next
= MKDEV(MAJOR(n
)+1, 0);
299 kfree(__unregister_chrdev_region(MAJOR(n
), MINOR(n
), next
- n
));
303 int unregister_chrdev(unsigned int major
, const char *name
)
305 struct char_device_struct
*cd
;
306 cd
= __unregister_chrdev_region(major
, 0, 256);
313 static DEFINE_SPINLOCK(cdev_lock
);
315 static struct kobject
*cdev_get(struct cdev
*p
)
317 struct module
*owner
= p
->owner
;
318 struct kobject
*kobj
;
320 if (owner
&& !try_module_get(owner
))
322 kobj
= kobject_get(&p
->kobj
);
328 void cdev_put(struct cdev
*p
)
331 struct module
*owner
= p
->owner
;
332 kobject_put(&p
->kobj
);
338 * Called every time a character special file is opened
340 int chrdev_open(struct inode
* inode
, struct file
* filp
)
343 struct cdev
*new = NULL
;
346 spin_lock(&cdev_lock
);
349 struct kobject
*kobj
;
351 spin_unlock(&cdev_lock
);
352 kobj
= kobj_lookup(cdev_map
, inode
->i_rdev
, &idx
);
355 new = container_of(kobj
, struct cdev
, kobj
);
356 spin_lock(&cdev_lock
);
359 inode
->i_cdev
= p
= new;
360 inode
->i_cindex
= idx
;
361 list_add(&inode
->i_devices
, &p
->list
);
363 } else if (!cdev_get(p
))
365 } else if (!cdev_get(p
))
367 spin_unlock(&cdev_lock
);
371 filp
->f_op
= fops_get(p
->ops
);
376 if (filp
->f_op
->open
) {
378 ret
= filp
->f_op
->open(inode
,filp
);
386 void cd_forget(struct inode
*inode
)
388 spin_lock(&cdev_lock
);
389 list_del_init(&inode
->i_devices
);
390 inode
->i_cdev
= NULL
;
391 spin_unlock(&cdev_lock
);
394 static void cdev_purge(struct cdev
*cdev
)
396 spin_lock(&cdev_lock
);
397 while (!list_empty(&cdev
->list
)) {
399 inode
= container_of(cdev
->list
.next
, struct inode
, i_devices
);
400 list_del_init(&inode
->i_devices
);
401 inode
->i_cdev
= NULL
;
403 spin_unlock(&cdev_lock
);
407 * Dummy default file-operations: the only thing this does
408 * is contain the open that then fills in the correct operations
409 * depending on the special file...
411 struct file_operations def_chr_fops
= {
415 static struct kobject
*exact_match(dev_t dev
, int *part
, void *data
)
417 struct cdev
*p
= data
;
421 static int exact_lock(dev_t dev
, void *data
)
423 struct cdev
*p
= data
;
424 return cdev_get(p
) ? 0 : -1;
427 int cdev_add(struct cdev
*p
, dev_t dev
, unsigned count
)
431 return kobj_map(cdev_map
, dev
, count
, NULL
, exact_match
, exact_lock
, p
);
434 static void cdev_unmap(dev_t dev
, unsigned count
)
436 kobj_unmap(cdev_map
, dev
, count
);
439 void cdev_del(struct cdev
*p
)
441 cdev_unmap(p
->dev
, p
->count
);
442 kobject_put(&p
->kobj
);
446 static void cdev_default_release(struct kobject
*kobj
)
448 struct cdev
*p
= container_of(kobj
, struct cdev
, kobj
);
452 static void cdev_dynamic_release(struct kobject
*kobj
)
454 struct cdev
*p
= container_of(kobj
, struct cdev
, kobj
);
459 static struct kobj_type ktype_cdev_default
= {
460 .release
= cdev_default_release
,
463 static struct kobj_type ktype_cdev_dynamic
= {
464 .release
= cdev_dynamic_release
,
467 struct cdev
*cdev_alloc(void)
469 struct cdev
*p
= kmalloc(sizeof(struct cdev
), GFP_KERNEL
);
471 memset(p
, 0, sizeof(struct cdev
));
472 p
->kobj
.ktype
= &ktype_cdev_dynamic
;
473 INIT_LIST_HEAD(&p
->list
);
474 kobject_init(&p
->kobj
);
479 void cdev_init(struct cdev
*cdev
, struct file_operations
*fops
)
481 memset(cdev
, 0, sizeof *cdev
);
482 INIT_LIST_HEAD(&cdev
->list
);
483 cdev
->kobj
.ktype
= &ktype_cdev_default
;
484 kobject_init(&cdev
->kobj
);
488 static struct kobject
*base_probe(dev_t dev
, int *part
, void *data
)
490 if (request_module("char-major-%d-%d", MAJOR(dev
), MINOR(dev
)) > 0)
491 /* Make old-style 2.4 aliases work */
492 request_module("char-major-%d", MAJOR(dev
));
496 void __init
chrdev_init(void)
498 cdev_map
= kobj_map_init(base_probe
, &chrdevs_lock
);
502 /* Let modules do char dev stuff */
503 EXPORT_SYMBOL(register_chrdev_region
);
504 EXPORT_SYMBOL(unregister_chrdev_region
);
505 EXPORT_SYMBOL(alloc_chrdev_region
);
506 EXPORT_SYMBOL(cdev_init
);
507 EXPORT_SYMBOL(cdev_alloc
);
508 EXPORT_SYMBOL(cdev_del
);
509 EXPORT_SYMBOL(cdev_add
);
510 EXPORT_SYMBOL(register_chrdev
);
511 EXPORT_SYMBOL(unregister_chrdev
);