9 #include <linux/kdev_t.h>
10 #include <linux/slab.h>
11 #include <linux/string.h>
14 #include <linux/errno.h>
15 #include <linux/module.h>
23 #include <linux/tty.h>
52 static struct char_device_struct {
53 struct char_device_struct *
next;
55 unsigned int baseminor;
62 static inline int major_to_index(
unsigned major)
71 struct char_device_struct *
cd;
75 for (cd = chrdevs[offset];
cd; cd = cd->next)
76 seq_printf(f,
"%3d %s\n", cd->major, cd->name);
94 static struct char_device_struct *
95 __register_chrdev_region(
unsigned int major,
unsigned int baseminor,
96 int minorct,
const char *
name)
98 struct char_device_struct *
cd, **
cp;
102 cd = kzalloc(
sizeof(
struct char_device_struct),
GFP_KERNEL);
111 if (chrdevs[i] ==
NULL)
124 cd->baseminor = baseminor;
125 cd->minorct = minorct;
126 strlcpy(cd->name, name,
sizeof(cd->name));
128 i = major_to_index(major);
130 for (cp = &chrdevs[i]; *
cp; cp = &(*cp)->next)
131 if ((*cp)->major > major ||
132 ((*cp)->major == major &&
133 (((*cp)->baseminor >= baseminor) ||
134 ((*cp)->baseminor + (*cp)->minorct > baseminor))))
138 if (*cp && (*cp)->major == major) {
139 int old_min = (*cp)->baseminor;
140 int old_max = (*cp)->baseminor + (*cp)->minorct - 1;
141 int new_min = baseminor;
142 int new_max = baseminor + minorct - 1;
145 if (new_max >= old_min && new_max <= old_max) {
151 if (new_min <= old_max && new_min >= old_min) {
167 static struct char_device_struct *
168 __unregister_chrdev_region(
unsigned major,
unsigned baseminor,
int minorct)
170 struct char_device_struct *
cd =
NULL, **
cp;
171 int i = major_to_index(major);
174 for (cp = &chrdevs[i]; *
cp; cp = &(*cp)->next)
175 if ((*cp)->major == major &&
176 (*cp)->baseminor == baseminor &&
177 (*cp)->minorct == minorct)
198 struct char_device_struct *
cd;
202 for (n = from; n < to; n =
next) {
206 cd = __register_chrdev_region(
MAJOR(n),
MINOR(n),
214 for (n = from; n < to; n =
next) {
235 struct char_device_struct *
cd;
236 cd = __register_chrdev_region(0, baseminor, count, name);
239 *dev =
MKDEV(cd->major, cd->baseminor);
265 unsigned int count,
const char *name,
268 struct char_device_struct *
cd;
272 cd = __register_chrdev_region(major, baseminor, count, name);
290 return major ? 0 : cd->major;
294 kfree(__unregister_chrdev_region(cd->major, baseminor, count));
312 for (n = from; n < to; n =
next) {
332 unsigned int count,
const char *name)
334 struct char_device_struct *
cd;
336 cd = __unregister_chrdev_region(major, baseminor, count);
349 if (owner && !try_module_get(owner))
375 spin_lock(&cdev_lock);
380 spin_unlock(&cdev_lock);
385 spin_lock(&cdev_lock);
393 }
else if (!cdev_get(p))
395 }
else if (!cdev_get(p))
397 spin_unlock(&cdev_lock);
407 if (filp->
f_op->open) {
408 ret = filp->
f_op->open(inode, filp);
422 spin_lock(&cdev_lock);
425 spin_unlock(&cdev_lock);
428 static void cdev_purge(
struct cdev *
cdev)
430 spin_lock(&cdev_lock);
431 while (!list_empty(&cdev->
list)) {
437 spin_unlock(&cdev_lock);
452 struct cdev *p =
data;
458 struct cdev *p =
data;
459 return cdev_get(p) ? 0 : -1;
480 exact_match, exact_lock, p);
508 static void cdev_default_release(
struct kobject *kobj)
517 static void cdev_dynamic_release(
struct kobject *kobj)
527 static struct kobj_type ktype_cdev_default = {
528 .release = cdev_default_release,
531 static struct kobj_type ktype_cdev_dynamic = {
532 .release = cdev_dynamic_release,
542 struct cdev *p = kzalloc(
sizeof(
struct cdev),
GFP_KERNEL);
544 INIT_LIST_HEAD(&p->
list);
560 memset(cdev, 0,
sizeof *cdev);
561 INIT_LIST_HEAD(&cdev->
list);
568 if (request_module(
"char-major-%d-%d",
MAJOR(dev),
MINOR(dev)) > 0)
570 request_module(
"char-major-%d",
MAJOR(dev));
577 bdi_init(&directly_mappable_cdev_bdi);