xref: /linux/fs/char_dev.c (revision 14b42963f64b98ab61fa9723c03d71aa5ef4f862)
1 /*
2  *  linux/fs/char_dev.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  */
6 
7 #include <linux/init.h>
8 #include <linux/fs.h>
9 #include <linux/slab.h>
10 #include <linux/string.h>
11 
12 #include <linux/major.h>
13 #include <linux/errno.h>
14 #include <linux/module.h>
15 #include <linux/smp_lock.h>
16 #include <linux/seq_file.h>
17 
18 #include <linux/kobject.h>
19 #include <linux/kobj_map.h>
20 #include <linux/cdev.h>
21 #include <linux/mutex.h>
22 
23 #ifdef CONFIG_KMOD
24 #include <linux/kmod.h>
25 #endif
26 
27 static struct kobj_map *cdev_map;
28 
29 static DEFINE_MUTEX(chrdevs_lock);
30 
31 static struct char_device_struct {
32 	struct char_device_struct *next;
33 	unsigned int major;
34 	unsigned int baseminor;
35 	int minorct;
36 	char name[64];
37 	struct file_operations *fops;
38 	struct cdev *cdev;		/* will die */
39 } *chrdevs[CHRDEV_MAJOR_HASH_SIZE];
40 
41 /* index in the above */
42 static inline int major_to_index(int major)
43 {
44 	return major % CHRDEV_MAJOR_HASH_SIZE;
45 }
46 
47 #ifdef CONFIG_PROC_FS
48 
49 void chrdev_show(struct seq_file *f, off_t offset)
50 {
51 	struct char_device_struct *cd;
52 
53 	if (offset < CHRDEV_MAJOR_HASH_SIZE) {
54 		mutex_lock(&chrdevs_lock);
55 		for (cd = chrdevs[offset]; cd; cd = cd->next)
56 			seq_printf(f, "%3d %s\n", cd->major, cd->name);
57 		mutex_unlock(&chrdevs_lock);
58 	}
59 }
60 
61 #endif /* CONFIG_PROC_FS */
62 
63 /*
64  * Register a single major with a specified minor range.
65  *
66  * If major == 0 this functions will dynamically allocate a major and return
67  * its number.
68  *
69  * If major > 0 this function will attempt to reserve the passed range of
70  * minors and will return zero on success.
71  *
72  * Returns a -ve errno on failure.
73  */
74 static struct char_device_struct *
75 __register_chrdev_region(unsigned int major, unsigned int baseminor,
76 			   int minorct, const char *name)
77 {
78 	struct char_device_struct *cd, **cp;
79 	int ret = 0;
80 	int i;
81 
82 	cd = kzalloc(sizeof(struct char_device_struct), GFP_KERNEL);
83 	if (cd == NULL)
84 		return ERR_PTR(-ENOMEM);
85 
86 	mutex_lock(&chrdevs_lock);
87 
88 	/* temporary */
89 	if (major == 0) {
90 		for (i = ARRAY_SIZE(chrdevs)-1; i > 0; i--) {
91 			if (chrdevs[i] == NULL)
92 				break;
93 		}
94 
95 		if (i == 0) {
96 			ret = -EBUSY;
97 			goto out;
98 		}
99 		major = i;
100 		ret = major;
101 	}
102 
103 	cd->major = major;
104 	cd->baseminor = baseminor;
105 	cd->minorct = minorct;
106 	strncpy(cd->name,name, 64);
107 
108 	i = major_to_index(major);
109 
110 	for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next)
111 		if ((*cp)->major > major ||
112 		    ((*cp)->major == major && (*cp)->baseminor >= baseminor))
113 			break;
114 	if (*cp && (*cp)->major == major &&
115 	    (*cp)->baseminor < baseminor + minorct) {
116 		ret = -EBUSY;
117 		goto out;
118 	}
119 	cd->next = *cp;
120 	*cp = cd;
121 	mutex_unlock(&chrdevs_lock);
122 	return cd;
123 out:
124 	mutex_unlock(&chrdevs_lock);
125 	kfree(cd);
126 	return ERR_PTR(ret);
127 }
128 
129 static struct char_device_struct *
130 __unregister_chrdev_region(unsigned major, unsigned baseminor, int minorct)
131 {
132 	struct char_device_struct *cd = NULL, **cp;
133 	int i = major_to_index(major);
134 
135 	mutex_lock(&chrdevs_lock);
136 	for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next)
137 		if ((*cp)->major == major &&
138 		    (*cp)->baseminor == baseminor &&
139 		    (*cp)->minorct == minorct)
140 			break;
141 	if (*cp) {
142 		cd = *cp;
143 		*cp = cd->next;
144 	}
145 	mutex_unlock(&chrdevs_lock);
146 	return cd;
147 }
148 
149 int register_chrdev_region(dev_t from, unsigned count, const char *name)
150 {
151 	struct char_device_struct *cd;
152 	dev_t to = from + count;
153 	dev_t n, next;
154 
155 	for (n = from; n < to; n = next) {
156 		next = MKDEV(MAJOR(n)+1, 0);
157 		if (next > to)
158 			next = to;
159 		cd = __register_chrdev_region(MAJOR(n), MINOR(n),
160 			       next - n, name);
161 		if (IS_ERR(cd))
162 			goto fail;
163 	}
164 	return 0;
165 fail:
166 	to = n;
167 	for (n = from; n < to; n = next) {
168 		next = MKDEV(MAJOR(n)+1, 0);
169 		kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
170 	}
171 	return PTR_ERR(cd);
172 }
173 
174 int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count,
175 			const char *name)
176 {
177 	struct char_device_struct *cd;
178 	cd = __register_chrdev_region(0, baseminor, count, name);
179 	if (IS_ERR(cd))
180 		return PTR_ERR(cd);
181 	*dev = MKDEV(cd->major, cd->baseminor);
182 	return 0;
183 }
184 
185 int register_chrdev(unsigned int major, const char *name,
186 		    const struct file_operations *fops)
187 {
188 	struct char_device_struct *cd;
189 	struct cdev *cdev;
190 	char *s;
191 	int err = -ENOMEM;
192 
193 	cd = __register_chrdev_region(major, 0, 256, name);
194 	if (IS_ERR(cd))
195 		return PTR_ERR(cd);
196 
197 	cdev = cdev_alloc();
198 	if (!cdev)
199 		goto out2;
200 
201 	cdev->owner = fops->owner;
202 	cdev->ops = fops;
203 	kobject_set_name(&cdev->kobj, "%s", name);
204 	for (s = strchr(kobject_name(&cdev->kobj),'/'); s; s = strchr(s, '/'))
205 		*s = '!';
206 
207 	err = cdev_add(cdev, MKDEV(cd->major, 0), 256);
208 	if (err)
209 		goto out;
210 
211 	cd->cdev = cdev;
212 
213 	return major ? 0 : cd->major;
214 out:
215 	kobject_put(&cdev->kobj);
216 out2:
217 	kfree(__unregister_chrdev_region(cd->major, 0, 256));
218 	return err;
219 }
220 
221 void unregister_chrdev_region(dev_t from, unsigned count)
222 {
223 	dev_t to = from + count;
224 	dev_t n, next;
225 
226 	for (n = from; n < to; n = next) {
227 		next = MKDEV(MAJOR(n)+1, 0);
228 		if (next > to)
229 			next = to;
230 		kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
231 	}
232 }
233 
234 int unregister_chrdev(unsigned int major, const char *name)
235 {
236 	struct char_device_struct *cd;
237 	cd = __unregister_chrdev_region(major, 0, 256);
238 	if (cd && cd->cdev)
239 		cdev_del(cd->cdev);
240 	kfree(cd);
241 	return 0;
242 }
243 
244 static DEFINE_SPINLOCK(cdev_lock);
245 
246 static struct kobject *cdev_get(struct cdev *p)
247 {
248 	struct module *owner = p->owner;
249 	struct kobject *kobj;
250 
251 	if (owner && !try_module_get(owner))
252 		return NULL;
253 	kobj = kobject_get(&p->kobj);
254 	if (!kobj)
255 		module_put(owner);
256 	return kobj;
257 }
258 
259 void cdev_put(struct cdev *p)
260 {
261 	if (p) {
262 		struct module *owner = p->owner;
263 		kobject_put(&p->kobj);
264 		module_put(owner);
265 	}
266 }
267 
268 /*
269  * Called every time a character special file is opened
270  */
271 int chrdev_open(struct inode * inode, struct file * filp)
272 {
273 	struct cdev *p;
274 	struct cdev *new = NULL;
275 	int ret = 0;
276 
277 	spin_lock(&cdev_lock);
278 	p = inode->i_cdev;
279 	if (!p) {
280 		struct kobject *kobj;
281 		int idx;
282 		spin_unlock(&cdev_lock);
283 		kobj = kobj_lookup(cdev_map, inode->i_rdev, &idx);
284 		if (!kobj)
285 			return -ENXIO;
286 		new = container_of(kobj, struct cdev, kobj);
287 		spin_lock(&cdev_lock);
288 		p = inode->i_cdev;
289 		if (!p) {
290 			inode->i_cdev = p = new;
291 			inode->i_cindex = idx;
292 			list_add(&inode->i_devices, &p->list);
293 			new = NULL;
294 		} else if (!cdev_get(p))
295 			ret = -ENXIO;
296 	} else if (!cdev_get(p))
297 		ret = -ENXIO;
298 	spin_unlock(&cdev_lock);
299 	cdev_put(new);
300 	if (ret)
301 		return ret;
302 	filp->f_op = fops_get(p->ops);
303 	if (!filp->f_op) {
304 		cdev_put(p);
305 		return -ENXIO;
306 	}
307 	if (filp->f_op->open) {
308 		lock_kernel();
309 		ret = filp->f_op->open(inode,filp);
310 		unlock_kernel();
311 	}
312 	if (ret)
313 		cdev_put(p);
314 	return ret;
315 }
316 
317 void cd_forget(struct inode *inode)
318 {
319 	spin_lock(&cdev_lock);
320 	list_del_init(&inode->i_devices);
321 	inode->i_cdev = NULL;
322 	spin_unlock(&cdev_lock);
323 }
324 
325 static void cdev_purge(struct cdev *cdev)
326 {
327 	spin_lock(&cdev_lock);
328 	while (!list_empty(&cdev->list)) {
329 		struct inode *inode;
330 		inode = container_of(cdev->list.next, struct inode, i_devices);
331 		list_del_init(&inode->i_devices);
332 		inode->i_cdev = NULL;
333 	}
334 	spin_unlock(&cdev_lock);
335 }
336 
337 /*
338  * Dummy default file-operations: the only thing this does
339  * is contain the open that then fills in the correct operations
340  * depending on the special file...
341  */
342 const struct file_operations def_chr_fops = {
343 	.open = chrdev_open,
344 };
345 
346 static struct kobject *exact_match(dev_t dev, int *part, void *data)
347 {
348 	struct cdev *p = data;
349 	return &p->kobj;
350 }
351 
352 static int exact_lock(dev_t dev, void *data)
353 {
354 	struct cdev *p = data;
355 	return cdev_get(p) ? 0 : -1;
356 }
357 
358 int cdev_add(struct cdev *p, dev_t dev, unsigned count)
359 {
360 	p->dev = dev;
361 	p->count = count;
362 	return kobj_map(cdev_map, dev, count, NULL, exact_match, exact_lock, p);
363 }
364 
365 static void cdev_unmap(dev_t dev, unsigned count)
366 {
367 	kobj_unmap(cdev_map, dev, count);
368 }
369 
370 void cdev_del(struct cdev *p)
371 {
372 	cdev_unmap(p->dev, p->count);
373 	kobject_put(&p->kobj);
374 }
375 
376 
377 static void cdev_default_release(struct kobject *kobj)
378 {
379 	struct cdev *p = container_of(kobj, struct cdev, kobj);
380 	cdev_purge(p);
381 }
382 
383 static void cdev_dynamic_release(struct kobject *kobj)
384 {
385 	struct cdev *p = container_of(kobj, struct cdev, kobj);
386 	cdev_purge(p);
387 	kfree(p);
388 }
389 
390 static struct kobj_type ktype_cdev_default = {
391 	.release	= cdev_default_release,
392 };
393 
394 static struct kobj_type ktype_cdev_dynamic = {
395 	.release	= cdev_dynamic_release,
396 };
397 
398 struct cdev *cdev_alloc(void)
399 {
400 	struct cdev *p = kzalloc(sizeof(struct cdev), GFP_KERNEL);
401 	if (p) {
402 		p->kobj.ktype = &ktype_cdev_dynamic;
403 		INIT_LIST_HEAD(&p->list);
404 		kobject_init(&p->kobj);
405 	}
406 	return p;
407 }
408 
409 void cdev_init(struct cdev *cdev, const struct file_operations *fops)
410 {
411 	memset(cdev, 0, sizeof *cdev);
412 	INIT_LIST_HEAD(&cdev->list);
413 	cdev->kobj.ktype = &ktype_cdev_default;
414 	kobject_init(&cdev->kobj);
415 	cdev->ops = fops;
416 }
417 
418 static struct kobject *base_probe(dev_t dev, int *part, void *data)
419 {
420 	if (request_module("char-major-%d-%d", MAJOR(dev), MINOR(dev)) > 0)
421 		/* Make old-style 2.4 aliases work */
422 		request_module("char-major-%d", MAJOR(dev));
423 	return NULL;
424 }
425 
426 void __init chrdev_init(void)
427 {
428 	cdev_map = kobj_map_init(base_probe, &chrdevs_lock);
429 }
430 
431 
432 /* Let modules do char dev stuff */
433 EXPORT_SYMBOL(register_chrdev_region);
434 EXPORT_SYMBOL(unregister_chrdev_region);
435 EXPORT_SYMBOL(alloc_chrdev_region);
436 EXPORT_SYMBOL(cdev_init);
437 EXPORT_SYMBOL(cdev_alloc);
438 EXPORT_SYMBOL(cdev_del);
439 EXPORT_SYMBOL(cdev_add);
440 EXPORT_SYMBOL(register_chrdev);
441 EXPORT_SYMBOL(unregister_chrdev);
442