Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 LT |
2 | /* |
3 | * linux/fs/char_dev.c | |
4 | * | |
5 | * Copyright (C) 1991, 1992 Linus Torvalds | |
6 | */ | |
7 | ||
1da177e4 LT |
8 | #include <linux/init.h> |
9 | #include <linux/fs.h> | |
b446b60e | 10 | #include <linux/kdev_t.h> |
1da177e4 LT |
11 | #include <linux/slab.h> |
12 | #include <linux/string.h> | |
13 | ||
14 | #include <linux/major.h> | |
15 | #include <linux/errno.h> | |
16 | #include <linux/module.h> | |
68eef3b4 | 17 | #include <linux/seq_file.h> |
1da177e4 LT |
18 | |
19 | #include <linux/kobject.h> | |
20 | #include <linux/kobj_map.h> | |
21 | #include <linux/cdev.h> | |
58383af6 | 22 | #include <linux/mutex.h> |
5da6185b | 23 | #include <linux/backing-dev.h> |
31d1d48e | 24 | #include <linux/tty.h> |
1da177e4 | 25 | |
07f3f05c | 26 | #include "internal.h" |
1da177e4 LT |
27 | |
28 | static struct kobj_map *cdev_map; | |
29 | ||
58383af6 | 30 | static DEFINE_MUTEX(chrdevs_lock); |
1da177e4 | 31 | |
8a932f73 LG |
32 | #define CHRDEV_MAJOR_HASH_SIZE 255 |
33 | ||
1da177e4 LT |
34 | static struct char_device_struct { |
35 | struct char_device_struct *next; | |
36 | unsigned int major; | |
37 | unsigned int baseminor; | |
38 | int minorct; | |
7170be5f | 39 | char name[64]; |
1da177e4 | 40 | struct cdev *cdev; /* will die */ |
68eef3b4 | 41 | } *chrdevs[CHRDEV_MAJOR_HASH_SIZE]; |
1da177e4 LT |
42 | |
43 | /* index in the above */ | |
e61eb2e9 | 44 | static inline int major_to_index(unsigned major) |
1da177e4 | 45 | { |
68eef3b4 | 46 | return major % CHRDEV_MAJOR_HASH_SIZE; |
7170be5f NH |
47 | } |
48 | ||
68eef3b4 | 49 | #ifdef CONFIG_PROC_FS |
7170be5f | 50 | |
68eef3b4 | 51 | void chrdev_show(struct seq_file *f, off_t offset) |
7170be5f NH |
52 | { |
53 | struct char_device_struct *cd; | |
7170be5f | 54 | |
8a932f73 LG |
55 | mutex_lock(&chrdevs_lock); |
56 | for (cd = chrdevs[major_to_index(offset)]; cd; cd = cd->next) { | |
57 | if (cd->major == offset) | |
68eef3b4 | 58 | seq_printf(f, "%3d %s\n", cd->major, cd->name); |
1da177e4 | 59 | } |
8a932f73 | 60 | mutex_unlock(&chrdevs_lock); |
7170be5f NH |
61 | } |
62 | ||
68eef3b4 | 63 | #endif /* CONFIG_PROC_FS */ |
1da177e4 | 64 | |
a5d31a3f LG |
65 | static int find_dynamic_major(void) |
66 | { | |
67 | int i; | |
68 | struct char_device_struct *cd; | |
69 | ||
652d703b | 70 | for (i = ARRAY_SIZE(chrdevs)-1; i >= CHRDEV_MAJOR_DYN_END; i--) { |
a5d31a3f LG |
71 | if (chrdevs[i] == NULL) |
72 | return i; | |
73 | } | |
74 | ||
75 | for (i = CHRDEV_MAJOR_DYN_EXT_START; | |
652d703b | 76 | i >= CHRDEV_MAJOR_DYN_EXT_END; i--) { |
a5d31a3f LG |
77 | for (cd = chrdevs[major_to_index(i)]; cd; cd = cd->next) |
78 | if (cd->major == i) | |
79 | break; | |
80 | ||
652d703b | 81 | if (cd == NULL) |
a5d31a3f LG |
82 | return i; |
83 | } | |
84 | ||
85 | return -EBUSY; | |
86 | } | |
87 | ||
1da177e4 LT |
88 | /* |
89 | * Register a single major with a specified minor range. | |
90 | * | |
91 | * If major == 0 this functions will dynamically allocate a major and return | |
92 | * its number. | |
93 | * | |
94 | * If major > 0 this function will attempt to reserve the passed range of | |
95 | * minors and will return zero on success. | |
96 | * | |
97 | * Returns a -ve errno on failure. | |
98 | */ | |
99 | static struct char_device_struct * | |
100 | __register_chrdev_region(unsigned int major, unsigned int baseminor, | |
101 | int minorct, const char *name) | |
102 | { | |
103 | struct char_device_struct *cd, **cp; | |
104 | int ret = 0; | |
105 | int i; | |
106 | ||
4712d379 CX |
107 | if (minorct > MINORMASK + 1 - baseminor) { |
108 | pr_err("CHRDEV \"%s\" minor range requested (%u-%u) is out of range of maximum range (%u-%u) for a single major\n", | |
109 | name, baseminor, baseminor + minorct - 1, 0, MINORMASK); | |
110 | return ERR_PTR(-EINVAL); | |
111 | } | |
112 | ||
11b0b5ab | 113 | cd = kzalloc(sizeof(struct char_device_struct), GFP_KERNEL); |
1da177e4 LT |
114 | if (cd == NULL) |
115 | return ERR_PTR(-ENOMEM); | |
116 | ||
58383af6 | 117 | mutex_lock(&chrdevs_lock); |
1da177e4 | 118 | |
1da177e4 | 119 | if (major == 0) { |
a5d31a3f LG |
120 | ret = find_dynamic_major(); |
121 | if (ret < 0) { | |
122 | pr_err("CHRDEV \"%s\" dynamic allocation region is full\n", | |
123 | name); | |
1da177e4 LT |
124 | goto out; |
125 | } | |
a5d31a3f | 126 | major = ret; |
1da177e4 LT |
127 | } |
128 | ||
8a932f73 | 129 | if (major >= CHRDEV_MAJOR_MAX) { |
f33ff110 SB |
130 | pr_err("CHRDEV \"%s\" major requested (%u) is greater than the maximum (%u)\n", |
131 | name, major, CHRDEV_MAJOR_MAX-1); | |
8a932f73 LG |
132 | ret = -EINVAL; |
133 | goto out; | |
134 | } | |
135 | ||
1da177e4 LT |
136 | cd->major = major; |
137 | cd->baseminor = baseminor; | |
138 | cd->minorct = minorct; | |
8c401888 | 139 | strlcpy(cd->name, name, sizeof(cd->name)); |
1da177e4 LT |
140 | |
141 | i = major_to_index(major); | |
142 | ||
143 | for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next) | |
144 | if ((*cp)->major > major || | |
01d553d0 AW |
145 | ((*cp)->major == major && |
146 | (((*cp)->baseminor >= baseminor) || | |
147 | ((*cp)->baseminor + (*cp)->minorct > baseminor)))) | |
1da177e4 | 148 | break; |
01d553d0 AW |
149 | |
150 | /* Check for overlapping minor ranges. */ | |
151 | if (*cp && (*cp)->major == major) { | |
152 | int old_min = (*cp)->baseminor; | |
153 | int old_max = (*cp)->baseminor + (*cp)->minorct - 1; | |
154 | int new_min = baseminor; | |
155 | int new_max = baseminor + minorct - 1; | |
156 | ||
157 | /* New driver overlaps from the left. */ | |
158 | if (new_max >= old_min && new_max <= old_max) { | |
159 | ret = -EBUSY; | |
160 | goto out; | |
161 | } | |
162 | ||
163 | /* New driver overlaps from the right. */ | |
164 | if (new_min <= old_max && new_min >= old_min) { | |
165 | ret = -EBUSY; | |
166 | goto out; | |
167 | } | |
de36e16d CX |
168 | |
169 | if (new_min < old_min && new_max > old_max) { | |
170 | ret = -EBUSY; | |
171 | goto out; | |
172 | } | |
173 | ||
1da177e4 | 174 | } |
01d553d0 | 175 | |
1da177e4 LT |
176 | cd->next = *cp; |
177 | *cp = cd; | |
58383af6 | 178 | mutex_unlock(&chrdevs_lock); |
1da177e4 LT |
179 | return cd; |
180 | out: | |
58383af6 | 181 | mutex_unlock(&chrdevs_lock); |
1da177e4 LT |
182 | kfree(cd); |
183 | return ERR_PTR(ret); | |
184 | } | |
185 | ||
186 | static struct char_device_struct * | |
187 | __unregister_chrdev_region(unsigned major, unsigned baseminor, int minorct) | |
188 | { | |
189 | struct char_device_struct *cd = NULL, **cp; | |
190 | int i = major_to_index(major); | |
191 | ||
58383af6 | 192 | mutex_lock(&chrdevs_lock); |
1da177e4 LT |
193 | for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next) |
194 | if ((*cp)->major == major && | |
195 | (*cp)->baseminor == baseminor && | |
196 | (*cp)->minorct == minorct) | |
197 | break; | |
198 | if (*cp) { | |
199 | cd = *cp; | |
200 | *cp = cd->next; | |
201 | } | |
58383af6 | 202 | mutex_unlock(&chrdevs_lock); |
1da177e4 LT |
203 | return cd; |
204 | } | |
205 | ||
cf3e43db JC |
206 | /** |
207 | * register_chrdev_region() - register a range of device numbers | |
208 | * @from: the first in the desired range of device numbers; must include | |
209 | * the major number. | |
210 | * @count: the number of consecutive device numbers required | |
211 | * @name: the name of the device or driver. | |
212 | * | |
213 | * Return value is zero on success, a negative error code on failure. | |
214 | */ | |
1da177e4 LT |
215 | int register_chrdev_region(dev_t from, unsigned count, const char *name) |
216 | { | |
217 | struct char_device_struct *cd; | |
218 | dev_t to = from + count; | |
219 | dev_t n, next; | |
220 | ||
221 | for (n = from; n < to; n = next) { | |
222 | next = MKDEV(MAJOR(n)+1, 0); | |
223 | if (next > to) | |
224 | next = to; | |
225 | cd = __register_chrdev_region(MAJOR(n), MINOR(n), | |
226 | next - n, name); | |
227 | if (IS_ERR(cd)) | |
228 | goto fail; | |
229 | } | |
230 | return 0; | |
231 | fail: | |
232 | to = n; | |
233 | for (n = from; n < to; n = next) { | |
234 | next = MKDEV(MAJOR(n)+1, 0); | |
235 | kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n)); | |
236 | } | |
237 | return PTR_ERR(cd); | |
238 | } | |
239 | ||
cf3e43db JC |
240 | /** |
241 | * alloc_chrdev_region() - register a range of char device numbers | |
242 | * @dev: output parameter for first assigned number | |
243 | * @baseminor: first of the requested range of minor numbers | |
244 | * @count: the number of minor numbers required | |
245 | * @name: the name of the associated device or driver | |
246 | * | |
247 | * Allocates a range of char device numbers. The major number will be | |
248 | * chosen dynamically, and returned (along with the first minor number) | |
249 | * in @dev. Returns zero or a negative error code. | |
250 | */ | |
1da177e4 LT |
251 | int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count, |
252 | const char *name) | |
253 | { | |
254 | struct char_device_struct *cd; | |
255 | cd = __register_chrdev_region(0, baseminor, count, name); | |
256 | if (IS_ERR(cd)) | |
257 | return PTR_ERR(cd); | |
258 | *dev = MKDEV(cd->major, cd->baseminor); | |
259 | return 0; | |
260 | } | |
261 | ||
d247e2c6 | 262 | /** |
1905b1bf | 263 | * __register_chrdev() - create and register a cdev occupying a range of minors |
d247e2c6 | 264 | * @major: major device number or 0 for dynamic allocation |
1905b1bf TH |
265 | * @baseminor: first of the requested range of minor numbers |
266 | * @count: the number of minor numbers required | |
d247e2c6 REB |
267 | * @name: name of this range of devices |
268 | * @fops: file operations associated with this devices | |
269 | * | |
270 | * If @major == 0 this functions will dynamically allocate a major and return | |
271 | * its number. | |
272 | * | |
273 | * If @major > 0 this function will attempt to reserve a device with the given | |
274 | * major number and will return zero on success. | |
275 | * | |
276 | * Returns a -ve errno on failure. | |
277 | * | |
278 | * The name of this device has nothing to do with the name of the device in | |
279 | * /dev. It only helps to keep track of the different owners of devices. If | |
280 | * your module name has only one type of devices it's ok to use e.g. the name | |
281 | * of the module here. | |
d247e2c6 | 282 | */ |
1905b1bf TH |
283 | int __register_chrdev(unsigned int major, unsigned int baseminor, |
284 | unsigned int count, const char *name, | |
285 | const struct file_operations *fops) | |
1da177e4 LT |
286 | { |
287 | struct char_device_struct *cd; | |
288 | struct cdev *cdev; | |
1da177e4 LT |
289 | int err = -ENOMEM; |
290 | ||
1905b1bf | 291 | cd = __register_chrdev_region(major, baseminor, count, name); |
1da177e4 LT |
292 | if (IS_ERR(cd)) |
293 | return PTR_ERR(cd); | |
1ff97647 | 294 | |
1da177e4 LT |
295 | cdev = cdev_alloc(); |
296 | if (!cdev) | |
297 | goto out2; | |
298 | ||
299 | cdev->owner = fops->owner; | |
300 | cdev->ops = fops; | |
301 | kobject_set_name(&cdev->kobj, "%s", name); | |
1ff97647 | 302 | |
1905b1bf | 303 | err = cdev_add(cdev, MKDEV(cd->major, baseminor), count); |
1da177e4 LT |
304 | if (err) |
305 | goto out; | |
306 | ||
307 | cd->cdev = cdev; | |
308 | ||
309 | return major ? 0 : cd->major; | |
310 | out: | |
311 | kobject_put(&cdev->kobj); | |
312 | out2: | |
1905b1bf | 313 | kfree(__unregister_chrdev_region(cd->major, baseminor, count)); |
1da177e4 LT |
314 | return err; |
315 | } | |
316 | ||
cf3e43db | 317 | /** |
594069bc | 318 | * unregister_chrdev_region() - unregister a range of device numbers |
cf3e43db JC |
319 | * @from: the first in the range of numbers to unregister |
320 | * @count: the number of device numbers to unregister | |
321 | * | |
322 | * This function will unregister a range of @count device numbers, | |
323 | * starting with @from. The caller should normally be the one who | |
324 | * allocated those numbers in the first place... | |
325 | */ | |
1da177e4 LT |
326 | void unregister_chrdev_region(dev_t from, unsigned count) |
327 | { | |
328 | dev_t to = from + count; | |
329 | dev_t n, next; | |
330 | ||
331 | for (n = from; n < to; n = next) { | |
332 | next = MKDEV(MAJOR(n)+1, 0); | |
333 | if (next > to) | |
334 | next = to; | |
335 | kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n)); | |
336 | } | |
337 | } | |
338 | ||
1905b1bf TH |
339 | /** |
340 | * __unregister_chrdev - unregister and destroy a cdev | |
341 | * @major: major device number | |
342 | * @baseminor: first of the range of minor numbers | |
343 | * @count: the number of minor numbers this cdev is occupying | |
344 | * @name: name of this range of devices | |
345 | * | |
346 | * Unregister and destroy the cdev occupying the region described by | |
347 | * @major, @baseminor and @count. This function undoes what | |
348 | * __register_chrdev() did. | |
349 | */ | |
350 | void __unregister_chrdev(unsigned int major, unsigned int baseminor, | |
351 | unsigned int count, const char *name) | |
1da177e4 LT |
352 | { |
353 | struct char_device_struct *cd; | |
1905b1bf TH |
354 | |
355 | cd = __unregister_chrdev_region(major, baseminor, count); | |
1da177e4 LT |
356 | if (cd && cd->cdev) |
357 | cdev_del(cd->cdev); | |
358 | kfree(cd); | |
1da177e4 LT |
359 | } |
360 | ||
361 | static DEFINE_SPINLOCK(cdev_lock); | |
362 | ||
363 | static struct kobject *cdev_get(struct cdev *p) | |
364 | { | |
365 | struct module *owner = p->owner; | |
366 | struct kobject *kobj; | |
367 | ||
368 | if (owner && !try_module_get(owner)) | |
369 | return NULL; | |
370 | kobj = kobject_get(&p->kobj); | |
371 | if (!kobj) | |
372 | module_put(owner); | |
373 | return kobj; | |
374 | } | |
375 | ||
376 | void cdev_put(struct cdev *p) | |
377 | { | |
378 | if (p) { | |
7da6844c | 379 | struct module *owner = p->owner; |
1da177e4 | 380 | kobject_put(&p->kobj); |
7da6844c | 381 | module_put(owner); |
1da177e4 LT |
382 | } |
383 | } | |
384 | ||
385 | /* | |
386 | * Called every time a character special file is opened | |
387 | */ | |
922f9cfa | 388 | static int chrdev_open(struct inode *inode, struct file *filp) |
1da177e4 | 389 | { |
e84f9e57 | 390 | const struct file_operations *fops; |
1da177e4 LT |
391 | struct cdev *p; |
392 | struct cdev *new = NULL; | |
393 | int ret = 0; | |
394 | ||
395 | spin_lock(&cdev_lock); | |
396 | p = inode->i_cdev; | |
397 | if (!p) { | |
398 | struct kobject *kobj; | |
399 | int idx; | |
400 | spin_unlock(&cdev_lock); | |
401 | kobj = kobj_lookup(cdev_map, inode->i_rdev, &idx); | |
402 | if (!kobj) | |
403 | return -ENXIO; | |
404 | new = container_of(kobj, struct cdev, kobj); | |
405 | spin_lock(&cdev_lock); | |
a30427d9 JC |
406 | /* Check i_cdev again in case somebody beat us to it while |
407 | we dropped the lock. */ | |
1da177e4 LT |
408 | p = inode->i_cdev; |
409 | if (!p) { | |
410 | inode->i_cdev = p = new; | |
1da177e4 LT |
411 | list_add(&inode->i_devices, &p->list); |
412 | new = NULL; | |
413 | } else if (!cdev_get(p)) | |
414 | ret = -ENXIO; | |
415 | } else if (!cdev_get(p)) | |
416 | ret = -ENXIO; | |
417 | spin_unlock(&cdev_lock); | |
418 | cdev_put(new); | |
419 | if (ret) | |
420 | return ret; | |
a518ab93 CH |
421 | |
422 | ret = -ENXIO; | |
e84f9e57 AV |
423 | fops = fops_get(p->ops); |
424 | if (!fops) | |
a518ab93 CH |
425 | goto out_cdev_put; |
426 | ||
e84f9e57 | 427 | replace_fops(filp, fops); |
a518ab93 | 428 | if (filp->f_op->open) { |
1ff97647 | 429 | ret = filp->f_op->open(inode, filp); |
a518ab93 CH |
430 | if (ret) |
431 | goto out_cdev_put; | |
432 | } | |
433 | ||
434 | return 0; | |
435 | ||
436 | out_cdev_put: | |
437 | cdev_put(p); | |
1da177e4 LT |
438 | return ret; |
439 | } | |
440 | ||
441 | void cd_forget(struct inode *inode) | |
442 | { | |
443 | spin_lock(&cdev_lock); | |
444 | list_del_init(&inode->i_devices); | |
445 | inode->i_cdev = NULL; | |
3bc52c45 | 446 | inode->i_mapping = &inode->i_data; |
1da177e4 LT |
447 | spin_unlock(&cdev_lock); |
448 | } | |
449 | ||
75c96f85 | 450 | static void cdev_purge(struct cdev *cdev) |
1da177e4 LT |
451 | { |
452 | spin_lock(&cdev_lock); | |
453 | while (!list_empty(&cdev->list)) { | |
454 | struct inode *inode; | |
455 | inode = container_of(cdev->list.next, struct inode, i_devices); | |
456 | list_del_init(&inode->i_devices); | |
457 | inode->i_cdev = NULL; | |
458 | } | |
459 | spin_unlock(&cdev_lock); | |
460 | } | |
461 | ||
462 | /* | |
463 | * Dummy default file-operations: the only thing this does | |
464 | * is contain the open that then fills in the correct operations | |
465 | * depending on the special file... | |
466 | */ | |
4b6f5d20 | 467 | const struct file_operations def_chr_fops = { |
1da177e4 | 468 | .open = chrdev_open, |
6038f373 | 469 | .llseek = noop_llseek, |
1da177e4 LT |
470 | }; |
471 | ||
472 | static struct kobject *exact_match(dev_t dev, int *part, void *data) | |
473 | { | |
474 | struct cdev *p = data; | |
475 | return &p->kobj; | |
476 | } | |
477 | ||
478 | static int exact_lock(dev_t dev, void *data) | |
479 | { | |
480 | struct cdev *p = data; | |
481 | return cdev_get(p) ? 0 : -1; | |
482 | } | |
483 | ||
cf3e43db JC |
484 | /** |
485 | * cdev_add() - add a char device to the system | |
486 | * @p: the cdev structure for the device | |
487 | * @dev: the first device number for which this device is responsible | |
488 | * @count: the number of consecutive minor numbers corresponding to this | |
489 | * device | |
490 | * | |
491 | * cdev_add() adds the device represented by @p to the system, making it | |
492 | * live immediately. A negative error code is returned on failure. | |
493 | */ | |
1da177e4 LT |
494 | int cdev_add(struct cdev *p, dev_t dev, unsigned count) |
495 | { | |
2f0157f1 DT |
496 | int error; |
497 | ||
1da177e4 LT |
498 | p->dev = dev; |
499 | p->count = count; | |
2f0157f1 DT |
500 | |
501 | error = kobj_map(cdev_map, dev, count, NULL, | |
502 | exact_match, exact_lock, p); | |
503 | if (error) | |
504 | return error; | |
505 | ||
506 | kobject_get(p->kobj.parent); | |
507 | ||
508 | return 0; | |
1da177e4 LT |
509 | } |
510 | ||
233ed09d LG |
511 | /** |
512 | * cdev_set_parent() - set the parent kobject for a char device | |
513 | * @p: the cdev structure | |
514 | * @kobj: the kobject to take a reference to | |
515 | * | |
516 | * cdev_set_parent() sets a parent kobject which will be referenced | |
517 | * appropriately so the parent is not freed before the cdev. This | |
518 | * should be called before cdev_add. | |
519 | */ | |
520 | void cdev_set_parent(struct cdev *p, struct kobject *kobj) | |
521 | { | |
522 | WARN_ON(!kobj->state_initialized); | |
523 | p->kobj.parent = kobj; | |
524 | } | |
525 | ||
526 | /** | |
527 | * cdev_device_add() - add a char device and it's corresponding | |
528 | * struct device, linkink | |
529 | * @dev: the device structure | |
530 | * @cdev: the cdev structure | |
531 | * | |
532 | * cdev_device_add() adds the char device represented by @cdev to the system, | |
533 | * just as cdev_add does. It then adds @dev to the system using device_add | |
534 | * The dev_t for the char device will be taken from the struct device which | |
535 | * needs to be initialized first. This helper function correctly takes a | |
536 | * reference to the parent device so the parent will not get released until | |
537 | * all references to the cdev are released. | |
538 | * | |
539 | * This helper uses dev->devt for the device number. If it is not set | |
540 | * it will not add the cdev and it will be equivalent to device_add. | |
541 | * | |
542 | * This function should be used whenever the struct cdev and the | |
543 | * struct device are members of the same structure whose lifetime is | |
544 | * managed by the struct device. | |
545 | * | |
546 | * NOTE: Callers must assume that userspace was able to open the cdev and | |
547 | * can call cdev fops callbacks at any time, even if this function fails. | |
548 | */ | |
549 | int cdev_device_add(struct cdev *cdev, struct device *dev) | |
550 | { | |
551 | int rc = 0; | |
552 | ||
553 | if (dev->devt) { | |
554 | cdev_set_parent(cdev, &dev->kobj); | |
555 | ||
556 | rc = cdev_add(cdev, dev->devt, 1); | |
557 | if (rc) | |
558 | return rc; | |
559 | } | |
560 | ||
561 | rc = device_add(dev); | |
562 | if (rc) | |
563 | cdev_del(cdev); | |
564 | ||
565 | return rc; | |
566 | } | |
567 | ||
568 | /** | |
569 | * cdev_device_del() - inverse of cdev_device_add | |
570 | * @dev: the device structure | |
571 | * @cdev: the cdev structure | |
572 | * | |
573 | * cdev_device_del() is a helper function to call cdev_del and device_del. | |
574 | * It should be used whenever cdev_device_add is used. | |
575 | * | |
576 | * If dev->devt is not set it will not remove the cdev and will be equivalent | |
577 | * to device_del. | |
578 | * | |
579 | * NOTE: This guarantees that associated sysfs callbacks are not running | |
580 | * or runnable, however any cdevs already open will remain and their fops | |
581 | * will still be callable even after this function returns. | |
582 | */ | |
583 | void cdev_device_del(struct cdev *cdev, struct device *dev) | |
584 | { | |
585 | device_del(dev); | |
586 | if (dev->devt) | |
587 | cdev_del(cdev); | |
588 | } | |
589 | ||
1da177e4 LT |
590 | static void cdev_unmap(dev_t dev, unsigned count) |
591 | { | |
592 | kobj_unmap(cdev_map, dev, count); | |
593 | } | |
594 | ||
cf3e43db JC |
595 | /** |
596 | * cdev_del() - remove a cdev from the system | |
597 | * @p: the cdev structure to be removed | |
598 | * | |
599 | * cdev_del() removes @p from the system, possibly freeing the structure | |
600 | * itself. | |
233ed09d LG |
601 | * |
602 | * NOTE: This guarantees that cdev device will no longer be able to be | |
603 | * opened, however any cdevs already open will remain and their fops will | |
604 | * still be callable even after cdev_del returns. | |
cf3e43db | 605 | */ |
1da177e4 LT |
606 | void cdev_del(struct cdev *p) |
607 | { | |
608 | cdev_unmap(p->dev, p->count); | |
609 | kobject_put(&p->kobj); | |
610 | } | |
611 | ||
612 | ||
613 | static void cdev_default_release(struct kobject *kobj) | |
614 | { | |
615 | struct cdev *p = container_of(kobj, struct cdev, kobj); | |
2f0157f1 DT |
616 | struct kobject *parent = kobj->parent; |
617 | ||
1da177e4 | 618 | cdev_purge(p); |
2f0157f1 | 619 | kobject_put(parent); |
1da177e4 LT |
620 | } |
621 | ||
622 | static void cdev_dynamic_release(struct kobject *kobj) | |
623 | { | |
624 | struct cdev *p = container_of(kobj, struct cdev, kobj); | |
2f0157f1 DT |
625 | struct kobject *parent = kobj->parent; |
626 | ||
1da177e4 LT |
627 | cdev_purge(p); |
628 | kfree(p); | |
2f0157f1 | 629 | kobject_put(parent); |
1da177e4 LT |
630 | } |
631 | ||
632 | static struct kobj_type ktype_cdev_default = { | |
633 | .release = cdev_default_release, | |
634 | }; | |
635 | ||
636 | static struct kobj_type ktype_cdev_dynamic = { | |
637 | .release = cdev_dynamic_release, | |
638 | }; | |
639 | ||
cf3e43db JC |
640 | /** |
641 | * cdev_alloc() - allocate a cdev structure | |
642 | * | |
643 | * Allocates and returns a cdev structure, or NULL on failure. | |
644 | */ | |
1da177e4 LT |
645 | struct cdev *cdev_alloc(void) |
646 | { | |
11b0b5ab | 647 | struct cdev *p = kzalloc(sizeof(struct cdev), GFP_KERNEL); |
1da177e4 | 648 | if (p) { |
1da177e4 | 649 | INIT_LIST_HEAD(&p->list); |
f9cb074b | 650 | kobject_init(&p->kobj, &ktype_cdev_dynamic); |
1da177e4 LT |
651 | } |
652 | return p; | |
653 | } | |
654 | ||
cf3e43db JC |
655 | /** |
656 | * cdev_init() - initialize a cdev structure | |
657 | * @cdev: the structure to initialize | |
658 | * @fops: the file_operations for this device | |
659 | * | |
660 | * Initializes @cdev, remembering @fops, making it ready to add to the | |
661 | * system with cdev_add(). | |
662 | */ | |
99ac48f5 | 663 | void cdev_init(struct cdev *cdev, const struct file_operations *fops) |
1da177e4 LT |
664 | { |
665 | memset(cdev, 0, sizeof *cdev); | |
666 | INIT_LIST_HEAD(&cdev->list); | |
f9cb074b | 667 | kobject_init(&cdev->kobj, &ktype_cdev_default); |
1da177e4 LT |
668 | cdev->ops = fops; |
669 | } | |
670 | ||
671 | static struct kobject *base_probe(dev_t dev, int *part, void *data) | |
672 | { | |
673 | if (request_module("char-major-%d-%d", MAJOR(dev), MINOR(dev)) > 0) | |
674 | /* Make old-style 2.4 aliases work */ | |
675 | request_module("char-major-%d", MAJOR(dev)); | |
676 | return NULL; | |
677 | } | |
678 | ||
679 | void __init chrdev_init(void) | |
680 | { | |
681 | cdev_map = kobj_map_init(base_probe, &chrdevs_lock); | |
682 | } | |
683 | ||
684 | ||
685 | /* Let modules do char dev stuff */ | |
686 | EXPORT_SYMBOL(register_chrdev_region); | |
687 | EXPORT_SYMBOL(unregister_chrdev_region); | |
688 | EXPORT_SYMBOL(alloc_chrdev_region); | |
689 | EXPORT_SYMBOL(cdev_init); | |
690 | EXPORT_SYMBOL(cdev_alloc); | |
691 | EXPORT_SYMBOL(cdev_del); | |
692 | EXPORT_SYMBOL(cdev_add); | |
233ed09d LG |
693 | EXPORT_SYMBOL(cdev_set_parent); |
694 | EXPORT_SYMBOL(cdev_device_add); | |
695 | EXPORT_SYMBOL(cdev_device_del); | |
1905b1bf TH |
696 | EXPORT_SYMBOL(__register_chrdev); |
697 | EXPORT_SYMBOL(__unregister_chrdev); |