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 /* degrade to linked list for small systems */
30 #define MAX_PROBE_HASH (CONFIG_BASE_SMALL ? 1 : 255)
32 static DECLARE_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
;
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");
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",
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 memset(cd
, 0, sizeof(struct char_device_struct
));
98 for (i
= ARRAY_SIZE(chrdevs
)-1; i
> 0; i
--) {
99 if (chrdevs
[i
] == NULL
)
112 cd
->baseminor
= baseminor
;
113 cd
->minorct
= minorct
;
116 i
= major_to_index(major
);
118 for (cp
= &chrdevs
[i
]; *cp
; cp
= &(*cp
)->next
)
119 if ((*cp
)->major
> major
||
120 ((*cp
)->major
== major
&& (*cp
)->baseminor
>= baseminor
))
122 if (*cp
&& (*cp
)->major
== major
&&
123 (*cp
)->baseminor
< baseminor
+ minorct
) {
137 static struct char_device_struct
*
138 __unregister_chrdev_region(unsigned major
, unsigned baseminor
, int minorct
)
140 struct char_device_struct
*cd
= NULL
, **cp
;
141 int i
= major_to_index(major
);
144 for (cp
= &chrdevs
[i
]; *cp
; cp
= &(*cp
)->next
)
145 if ((*cp
)->major
== major
&&
146 (*cp
)->baseminor
== baseminor
&&
147 (*cp
)->minorct
== minorct
)
157 int register_chrdev_region(dev_t from
, unsigned count
, const char *name
)
159 struct char_device_struct
*cd
;
160 dev_t to
= from
+ count
;
163 for (n
= from
; n
< to
; n
= next
) {
164 next
= MKDEV(MAJOR(n
)+1, 0);
167 cd
= __register_chrdev_region(MAJOR(n
), MINOR(n
),
175 for (n
= from
; n
< to
; n
= next
) {
176 next
= MKDEV(MAJOR(n
)+1, 0);
177 kfree(__unregister_chrdev_region(MAJOR(n
), MINOR(n
), next
- n
));
182 int alloc_chrdev_region(dev_t
*dev
, unsigned baseminor
, unsigned count
,
185 struct char_device_struct
*cd
;
186 cd
= __register_chrdev_region(0, baseminor
, count
, name
);
189 *dev
= MKDEV(cd
->major
, cd
->baseminor
);
193 int register_chrdev(unsigned int major
, const char *name
,
194 struct file_operations
*fops
)
196 struct char_device_struct
*cd
;
201 cd
= __register_chrdev_region(major
, 0, 256, name
);
209 cdev
->owner
= fops
->owner
;
211 kobject_set_name(&cdev
->kobj
, "%s", name
);
212 for (s
= strchr(kobject_name(&cdev
->kobj
),'/'); s
; s
= strchr(s
, '/'))
215 err
= cdev_add(cdev
, MKDEV(cd
->major
, 0), 256);
221 return major
? 0 : cd
->major
;
223 kobject_put(&cdev
->kobj
);
225 kfree(__unregister_chrdev_region(cd
->major
, 0, 256));
229 void unregister_chrdev_region(dev_t from
, unsigned count
)
231 dev_t to
= from
+ count
;
234 for (n
= from
; n
< to
; n
= next
) {
235 next
= MKDEV(MAJOR(n
)+1, 0);
238 kfree(__unregister_chrdev_region(MAJOR(n
), MINOR(n
), next
- n
));
242 int unregister_chrdev(unsigned int major
, const char *name
)
244 struct char_device_struct
*cd
;
245 cd
= __unregister_chrdev_region(major
, 0, 256);
252 static DEFINE_SPINLOCK(cdev_lock
);
254 static struct kobject
*cdev_get(struct cdev
*p
)
256 struct module
*owner
= p
->owner
;
257 struct kobject
*kobj
;
259 if (owner
&& !try_module_get(owner
))
261 kobj
= kobject_get(&p
->kobj
);
267 void cdev_put(struct cdev
*p
)
270 kobject_put(&p
->kobj
);
271 module_put(p
->owner
);
276 * Called every time a character special file is opened
278 int chrdev_open(struct inode
* inode
, struct file
* filp
)
281 struct cdev
*new = NULL
;
284 spin_lock(&cdev_lock
);
287 struct kobject
*kobj
;
289 spin_unlock(&cdev_lock
);
290 kobj
= kobj_lookup(cdev_map
, inode
->i_rdev
, &idx
);
293 new = container_of(kobj
, struct cdev
, kobj
);
294 spin_lock(&cdev_lock
);
297 inode
->i_cdev
= p
= new;
298 inode
->i_cindex
= idx
;
299 list_add(&inode
->i_devices
, &p
->list
);
301 } else if (!cdev_get(p
))
303 } else if (!cdev_get(p
))
305 spin_unlock(&cdev_lock
);
309 filp
->f_op
= fops_get(p
->ops
);
314 if (filp
->f_op
->open
) {
316 ret
= filp
->f_op
->open(inode
,filp
);
324 void cd_forget(struct inode
*inode
)
326 spin_lock(&cdev_lock
);
327 list_del_init(&inode
->i_devices
);
328 inode
->i_cdev
= NULL
;
329 spin_unlock(&cdev_lock
);
332 void cdev_purge(struct cdev
*cdev
)
334 spin_lock(&cdev_lock
);
335 while (!list_empty(&cdev
->list
)) {
337 inode
= container_of(cdev
->list
.next
, struct inode
, i_devices
);
338 list_del_init(&inode
->i_devices
);
339 inode
->i_cdev
= NULL
;
341 spin_unlock(&cdev_lock
);
345 * Dummy default file-operations: the only thing this does
346 * is contain the open that then fills in the correct operations
347 * depending on the special file...
349 struct file_operations def_chr_fops
= {
353 static struct kobject
*exact_match(dev_t dev
, int *part
, void *data
)
355 struct cdev
*p
= data
;
359 static int exact_lock(dev_t dev
, void *data
)
361 struct cdev
*p
= data
;
362 return cdev_get(p
) ? 0 : -1;
365 int cdev_add(struct cdev
*p
, dev_t dev
, unsigned count
)
369 return kobj_map(cdev_map
, dev
, count
, NULL
, exact_match
, exact_lock
, p
);
372 static void cdev_unmap(dev_t dev
, unsigned count
)
374 kobj_unmap(cdev_map
, dev
, count
);
377 void cdev_del(struct cdev
*p
)
379 cdev_unmap(p
->dev
, p
->count
);
380 kobject_put(&p
->kobj
);
384 static void cdev_default_release(struct kobject
*kobj
)
386 struct cdev
*p
= container_of(kobj
, struct cdev
, kobj
);
390 static void cdev_dynamic_release(struct kobject
*kobj
)
392 struct cdev
*p
= container_of(kobj
, struct cdev
, kobj
);
397 static struct kobj_type ktype_cdev_default
= {
398 .release
= cdev_default_release
,
401 static struct kobj_type ktype_cdev_dynamic
= {
402 .release
= cdev_dynamic_release
,
405 struct cdev
*cdev_alloc(void)
407 struct cdev
*p
= kmalloc(sizeof(struct cdev
), GFP_KERNEL
);
409 memset(p
, 0, sizeof(struct cdev
));
410 p
->kobj
.ktype
= &ktype_cdev_dynamic
;
411 INIT_LIST_HEAD(&p
->list
);
412 kobject_init(&p
->kobj
);
417 void cdev_init(struct cdev
*cdev
, struct file_operations
*fops
)
419 memset(cdev
, 0, sizeof *cdev
);
420 INIT_LIST_HEAD(&cdev
->list
);
421 cdev
->kobj
.ktype
= &ktype_cdev_default
;
422 kobject_init(&cdev
->kobj
);
426 static struct kobject
*base_probe(dev_t dev
, int *part
, void *data
)
428 if (request_module("char-major-%d-%d", MAJOR(dev
), MINOR(dev
)) > 0)
429 /* Make old-style 2.4 aliases work */
430 request_module("char-major-%d", MAJOR(dev
));
434 void __init
chrdev_init(void)
436 cdev_map
= kobj_map_init(base_probe
, &chrdevs_lock
);
440 /* Let modules do char dev stuff */
441 EXPORT_SYMBOL(register_chrdev_region
);
442 EXPORT_SYMBOL(unregister_chrdev_region
);
443 EXPORT_SYMBOL(alloc_chrdev_region
);
444 EXPORT_SYMBOL(cdev_init
);
445 EXPORT_SYMBOL(cdev_alloc
);
446 EXPORT_SYMBOL(cdev_del
);
447 EXPORT_SYMBOL(cdev_add
);
448 EXPORT_SYMBOL(register_chrdev
);
449 EXPORT_SYMBOL(unregister_chrdev
);