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