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