blob: ebcc8fb3fa6641363e302082aacd92812142e6b1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/char_dev.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/init.h>
8#include <linux/fs.h>
Andrew Mortonb446b602007-02-20 13:57:48 -08009#include <linux/kdev_t.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#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>
Joe Korty68eef3b2006-03-31 02:30:32 -080016#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18#include <linux/kobject.h>
19#include <linux/kobj_map.h>
20#include <linux/cdev.h>
Jes Sorensen58383af2006-02-06 14:12:43 -080021#include <linux/mutex.h>
David Howells5da61852006-09-27 01:50:16 -070022#include <linux/backing-dev.h>
David Howells31d1d482010-08-06 16:34:43 +010023#include <linux/tty.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
David Howells07f3f05c2006-09-30 20:52:18 +020025#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27static struct kobj_map *cdev_map;
28
Jes Sorensen58383af2006-02-06 14:12:43 -080029static DEFINE_MUTEX(chrdevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Logan Gunthorpe8a932f72017-06-15 14:05:21 -060031#define CHRDEV_MAJOR_HASH_SIZE 255
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033static struct char_device_struct {
34 struct char_device_struct *next;
35 unsigned int major;
36 unsigned int baseminor;
37 int minorct;
Neil Horman7170be52006-01-14 13:20:38 -080038 char name[64];
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 struct cdev *cdev; /* will die */
Joe Korty68eef3b2006-03-31 02:30:32 -080040} *chrdevs[CHRDEV_MAJOR_HASH_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42/* index in the above */
Yang Zhange61eb2e2010-12-17 09:00:18 +010043static inline int major_to_index(unsigned major)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
Joe Korty68eef3b2006-03-31 02:30:32 -080045 return major % CHRDEV_MAJOR_HASH_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046}
47
Joe Korty68eef3b2006-03-31 02:30:32 -080048#ifdef CONFIG_PROC_FS
Neil Horman7170be52006-01-14 13:20:38 -080049
Joe Korty68eef3b2006-03-31 02:30:32 -080050void chrdev_show(struct seq_file *f, off_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
52 struct char_device_struct *cd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Logan Gunthorpe8a932f72017-06-15 14:05:21 -060054 mutex_lock(&chrdevs_lock);
55 for (cd = chrdevs[major_to_index(offset)]; cd; cd = cd->next) {
56 if (cd->major == offset)
Joe Korty68eef3b2006-03-31 02:30:32 -080057 seq_printf(f, "%3d %s\n", cd->major, cd->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 }
Logan Gunthorpe8a932f72017-06-15 14:05:21 -060059 mutex_unlock(&chrdevs_lock);
Neil Horman7170be52006-01-14 13:20:38 -080060}
61
Joe Korty68eef3b2006-03-31 02:30:32 -080062#endif /* CONFIG_PROC_FS */
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Logan Gunthorpea5d31a32017-06-15 14:05:20 -060064static int find_dynamic_major(void)
65{
66 int i;
67 struct char_device_struct *cd;
68
69 for (i = ARRAY_SIZE(chrdevs)-1; i > CHRDEV_MAJOR_DYN_END; i--) {
70 if (chrdevs[i] == NULL)
71 return i;
72 }
73
74 for (i = CHRDEV_MAJOR_DYN_EXT_START;
75 i > CHRDEV_MAJOR_DYN_EXT_END; i--) {
76 for (cd = chrdevs[major_to_index(i)]; cd; cd = cd->next)
77 if (cd->major == i)
78 break;
79
80 if (cd == NULL || cd->major != i)
81 return i;
82 }
83
84 return -EBUSY;
85}
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087/*
88 * Register a single major with a specified minor range.
89 *
90 * If major == 0 this functions will dynamically allocate a major and return
91 * its number.
92 *
93 * If major > 0 this function will attempt to reserve the passed range of
94 * minors and will return zero on success.
95 *
96 * Returns a -ve errno on failure.
97 */
98static struct char_device_struct *
99__register_chrdev_region(unsigned int major, unsigned int baseminor,
100 int minorct, const char *name)
101{
102 struct char_device_struct *cd, **cp;
103 int ret = 0;
104 int i;
105
Oliver Neukum11b0b5a2006-03-25 03:08:13 -0800106 cd = kzalloc(sizeof(struct char_device_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 if (cd == NULL)
108 return ERR_PTR(-ENOMEM);
109
Jes Sorensen58383af2006-02-06 14:12:43 -0800110 mutex_lock(&chrdevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 if (major == 0) {
Logan Gunthorpea5d31a32017-06-15 14:05:20 -0600113 ret = find_dynamic_major();
114 if (ret < 0) {
115 pr_err("CHRDEV \"%s\" dynamic allocation region is full\n",
116 name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 goto out;
118 }
Logan Gunthorpea5d31a32017-06-15 14:05:20 -0600119 major = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 }
121
Logan Gunthorpe8a932f72017-06-15 14:05:21 -0600122 if (major >= CHRDEV_MAJOR_MAX) {
123 pr_err("CHRDEV \"%s\" major requested (%d) is greater than the maximum (%d)\n",
124 name, major, CHRDEV_MAJOR_MAX);
125 ret = -EINVAL;
126 goto out;
127 }
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 cd->major = major;
130 cd->baseminor = baseminor;
131 cd->minorct = minorct;
Cyrill Gorcunov8c4018882009-01-06 14:41:08 -0800132 strlcpy(cd->name, name, sizeof(cd->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 i = major_to_index(major);
135
136 for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next)
137 if ((*cp)->major > major ||
Amos Waterland01d553d2006-09-29 02:00:08 -0700138 ((*cp)->major == major &&
139 (((*cp)->baseminor >= baseminor) ||
140 ((*cp)->baseminor + (*cp)->minorct > baseminor))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 break;
Amos Waterland01d553d2006-09-29 02:00:08 -0700142
143 /* Check for overlapping minor ranges. */
144 if (*cp && (*cp)->major == major) {
145 int old_min = (*cp)->baseminor;
146 int old_max = (*cp)->baseminor + (*cp)->minorct - 1;
147 int new_min = baseminor;
148 int new_max = baseminor + minorct - 1;
149
150 /* New driver overlaps from the left. */
151 if (new_max >= old_min && new_max <= old_max) {
152 ret = -EBUSY;
153 goto out;
154 }
155
156 /* New driver overlaps from the right. */
157 if (new_min <= old_max && new_min >= old_min) {
158 ret = -EBUSY;
159 goto out;
160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 }
Amos Waterland01d553d2006-09-29 02:00:08 -0700162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 cd->next = *cp;
164 *cp = cd;
Jes Sorensen58383af2006-02-06 14:12:43 -0800165 mutex_unlock(&chrdevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 return cd;
167out:
Jes Sorensen58383af2006-02-06 14:12:43 -0800168 mutex_unlock(&chrdevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 kfree(cd);
170 return ERR_PTR(ret);
171}
172
173static struct char_device_struct *
174__unregister_chrdev_region(unsigned major, unsigned baseminor, int minorct)
175{
176 struct char_device_struct *cd = NULL, **cp;
177 int i = major_to_index(major);
178
Jes Sorensen58383af2006-02-06 14:12:43 -0800179 mutex_lock(&chrdevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next)
181 if ((*cp)->major == major &&
182 (*cp)->baseminor == baseminor &&
183 (*cp)->minorct == minorct)
184 break;
185 if (*cp) {
186 cd = *cp;
187 *cp = cd->next;
188 }
Jes Sorensen58383af2006-02-06 14:12:43 -0800189 mutex_unlock(&chrdevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return cd;
191}
192
Jonathan Corbetcf3e43d2006-09-29 02:00:44 -0700193/**
194 * register_chrdev_region() - register a range of device numbers
195 * @from: the first in the desired range of device numbers; must include
196 * the major number.
197 * @count: the number of consecutive device numbers required
198 * @name: the name of the device or driver.
199 *
200 * Return value is zero on success, a negative error code on failure.
201 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202int register_chrdev_region(dev_t from, unsigned count, const char *name)
203{
204 struct char_device_struct *cd;
205 dev_t to = from + count;
206 dev_t n, next;
207
208 for (n = from; n < to; n = next) {
209 next = MKDEV(MAJOR(n)+1, 0);
210 if (next > to)
211 next = to;
212 cd = __register_chrdev_region(MAJOR(n), MINOR(n),
213 next - n, name);
214 if (IS_ERR(cd))
215 goto fail;
216 }
217 return 0;
218fail:
219 to = n;
220 for (n = from; n < to; n = next) {
221 next = MKDEV(MAJOR(n)+1, 0);
222 kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
223 }
224 return PTR_ERR(cd);
225}
226
Jonathan Corbetcf3e43d2006-09-29 02:00:44 -0700227/**
228 * alloc_chrdev_region() - register a range of char device numbers
229 * @dev: output parameter for first assigned number
230 * @baseminor: first of the requested range of minor numbers
231 * @count: the number of minor numbers required
232 * @name: the name of the associated device or driver
233 *
234 * Allocates a range of char device numbers. The major number will be
235 * chosen dynamically, and returned (along with the first minor number)
236 * in @dev. Returns zero or a negative error code.
237 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count,
239 const char *name)
240{
241 struct char_device_struct *cd;
242 cd = __register_chrdev_region(0, baseminor, count, name);
243 if (IS_ERR(cd))
244 return PTR_ERR(cd);
245 *dev = MKDEV(cd->major, cd->baseminor);
246 return 0;
247}
248
Rolf Eike Beerd247e2c2006-07-14 00:24:23 -0700249/**
Tejun Heo1905b1b2009-08-06 18:13:23 +0900250 * __register_chrdev() - create and register a cdev occupying a range of minors
Rolf Eike Beerd247e2c2006-07-14 00:24:23 -0700251 * @major: major device number or 0 for dynamic allocation
Tejun Heo1905b1b2009-08-06 18:13:23 +0900252 * @baseminor: first of the requested range of minor numbers
253 * @count: the number of minor numbers required
Rolf Eike Beerd247e2c2006-07-14 00:24:23 -0700254 * @name: name of this range of devices
255 * @fops: file operations associated with this devices
256 *
257 * If @major == 0 this functions will dynamically allocate a major and return
258 * its number.
259 *
260 * If @major > 0 this function will attempt to reserve a device with the given
261 * major number and will return zero on success.
262 *
263 * Returns a -ve errno on failure.
264 *
265 * The name of this device has nothing to do with the name of the device in
266 * /dev. It only helps to keep track of the different owners of devices. If
267 * your module name has only one type of devices it's ok to use e.g. the name
268 * of the module here.
Rolf Eike Beerd247e2c2006-07-14 00:24:23 -0700269 */
Tejun Heo1905b1b2009-08-06 18:13:23 +0900270int __register_chrdev(unsigned int major, unsigned int baseminor,
271 unsigned int count, const char *name,
272 const struct file_operations *fops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
274 struct char_device_struct *cd;
275 struct cdev *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 int err = -ENOMEM;
277
Tejun Heo1905b1b2009-08-06 18:13:23 +0900278 cd = __register_chrdev_region(major, baseminor, count, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 if (IS_ERR(cd))
280 return PTR_ERR(cd);
Greg Kroah-Hartman1ff97642011-12-13 11:06:49 -0800281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 cdev = cdev_alloc();
283 if (!cdev)
284 goto out2;
285
286 cdev->owner = fops->owner;
287 cdev->ops = fops;
288 kobject_set_name(&cdev->kobj, "%s", name);
Greg Kroah-Hartman1ff97642011-12-13 11:06:49 -0800289
Tejun Heo1905b1b2009-08-06 18:13:23 +0900290 err = cdev_add(cdev, MKDEV(cd->major, baseminor), count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 if (err)
292 goto out;
293
294 cd->cdev = cdev;
295
296 return major ? 0 : cd->major;
297out:
298 kobject_put(&cdev->kobj);
299out2:
Tejun Heo1905b1b2009-08-06 18:13:23 +0900300 kfree(__unregister_chrdev_region(cd->major, baseminor, count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 return err;
302}
303
Jonathan Corbetcf3e43d2006-09-29 02:00:44 -0700304/**
Partha Pratim Mukherjee594069b2015-07-27 16:15:19 -0700305 * unregister_chrdev_region() - unregister a range of device numbers
Jonathan Corbetcf3e43d2006-09-29 02:00:44 -0700306 * @from: the first in the range of numbers to unregister
307 * @count: the number of device numbers to unregister
308 *
309 * This function will unregister a range of @count device numbers,
310 * starting with @from. The caller should normally be the one who
311 * allocated those numbers in the first place...
312 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313void unregister_chrdev_region(dev_t from, unsigned count)
314{
315 dev_t to = from + count;
316 dev_t n, next;
317
318 for (n = from; n < to; n = next) {
319 next = MKDEV(MAJOR(n)+1, 0);
320 if (next > to)
321 next = to;
322 kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
323 }
324}
325
Tejun Heo1905b1b2009-08-06 18:13:23 +0900326/**
327 * __unregister_chrdev - unregister and destroy a cdev
328 * @major: major device number
329 * @baseminor: first of the range of minor numbers
330 * @count: the number of minor numbers this cdev is occupying
331 * @name: name of this range of devices
332 *
333 * Unregister and destroy the cdev occupying the region described by
334 * @major, @baseminor and @count. This function undoes what
335 * __register_chrdev() did.
336 */
337void __unregister_chrdev(unsigned int major, unsigned int baseminor,
338 unsigned int count, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
340 struct char_device_struct *cd;
Tejun Heo1905b1b2009-08-06 18:13:23 +0900341
342 cd = __unregister_chrdev_region(major, baseminor, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (cd && cd->cdev)
344 cdev_del(cd->cdev);
345 kfree(cd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
348static DEFINE_SPINLOCK(cdev_lock);
349
350static struct kobject *cdev_get(struct cdev *p)
351{
352 struct module *owner = p->owner;
353 struct kobject *kobj;
354
355 if (owner && !try_module_get(owner))
356 return NULL;
357 kobj = kobject_get(&p->kobj);
358 if (!kobj)
359 module_put(owner);
360 return kobj;
361}
362
363void cdev_put(struct cdev *p)
364{
365 if (p) {
Brian King7da68442005-07-12 13:58:30 -0700366 struct module *owner = p->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 kobject_put(&p->kobj);
Brian King7da68442005-07-12 13:58:30 -0700368 module_put(owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
370}
371
372/*
373 * Called every time a character special file is opened
374 */
Denis Cheng922f9cf2008-02-08 04:22:00 -0800375static int chrdev_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
Al Viroe84f9e52013-09-22 14:17:15 -0400377 const struct file_operations *fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 struct cdev *p;
379 struct cdev *new = NULL;
380 int ret = 0;
381
382 spin_lock(&cdev_lock);
383 p = inode->i_cdev;
384 if (!p) {
385 struct kobject *kobj;
386 int idx;
387 spin_unlock(&cdev_lock);
388 kobj = kobj_lookup(cdev_map, inode->i_rdev, &idx);
389 if (!kobj)
390 return -ENXIO;
391 new = container_of(kobj, struct cdev, kobj);
392 spin_lock(&cdev_lock);
Jonathan Corbeta30427d2008-05-18 15:39:11 -0600393 /* Check i_cdev again in case somebody beat us to it while
394 we dropped the lock. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 p = inode->i_cdev;
396 if (!p) {
397 inode->i_cdev = p = new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 list_add(&inode->i_devices, &p->list);
399 new = NULL;
400 } else if (!cdev_get(p))
401 ret = -ENXIO;
402 } else if (!cdev_get(p))
403 ret = -ENXIO;
404 spin_unlock(&cdev_lock);
405 cdev_put(new);
406 if (ret)
407 return ret;
Christoph Hellwiga518ab92008-08-11 15:34:22 +0200408
409 ret = -ENXIO;
Al Viroe84f9e52013-09-22 14:17:15 -0400410 fops = fops_get(p->ops);
411 if (!fops)
Christoph Hellwiga518ab92008-08-11 15:34:22 +0200412 goto out_cdev_put;
413
Al Viroe84f9e52013-09-22 14:17:15 -0400414 replace_fops(filp, fops);
Christoph Hellwiga518ab92008-08-11 15:34:22 +0200415 if (filp->f_op->open) {
Greg Kroah-Hartman1ff97642011-12-13 11:06:49 -0800416 ret = filp->f_op->open(inode, filp);
Christoph Hellwiga518ab92008-08-11 15:34:22 +0200417 if (ret)
418 goto out_cdev_put;
419 }
420
421 return 0;
422
423 out_cdev_put:
424 cdev_put(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return ret;
426}
427
428void cd_forget(struct inode *inode)
429{
430 spin_lock(&cdev_lock);
431 list_del_init(&inode->i_devices);
432 inode->i_cdev = NULL;
Dan Williams3bc52c42016-07-24 21:55:45 -0700433 inode->i_mapping = &inode->i_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 spin_unlock(&cdev_lock);
435}
436
Adrian Bunk75c96f82005-05-05 16:16:09 -0700437static void cdev_purge(struct cdev *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
439 spin_lock(&cdev_lock);
440 while (!list_empty(&cdev->list)) {
441 struct inode *inode;
442 inode = container_of(cdev->list.next, struct inode, i_devices);
443 list_del_init(&inode->i_devices);
444 inode->i_cdev = NULL;
445 }
446 spin_unlock(&cdev_lock);
447}
448
449/*
450 * Dummy default file-operations: the only thing this does
451 * is contain the open that then fills in the correct operations
452 * depending on the special file...
453 */
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800454const struct file_operations def_chr_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 .open = chrdev_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200456 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457};
458
459static struct kobject *exact_match(dev_t dev, int *part, void *data)
460{
461 struct cdev *p = data;
462 return &p->kobj;
463}
464
465static int exact_lock(dev_t dev, void *data)
466{
467 struct cdev *p = data;
468 return cdev_get(p) ? 0 : -1;
469}
470
Jonathan Corbetcf3e43d2006-09-29 02:00:44 -0700471/**
472 * cdev_add() - add a char device to the system
473 * @p: the cdev structure for the device
474 * @dev: the first device number for which this device is responsible
475 * @count: the number of consecutive minor numbers corresponding to this
476 * device
477 *
478 * cdev_add() adds the device represented by @p to the system, making it
479 * live immediately. A negative error code is returned on failure.
480 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481int cdev_add(struct cdev *p, dev_t dev, unsigned count)
482{
Dmitry Torokhov2f0157f2012-10-21 17:57:19 -0700483 int error;
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 p->dev = dev;
486 p->count = count;
Dmitry Torokhov2f0157f2012-10-21 17:57:19 -0700487
488 error = kobj_map(cdev_map, dev, count, NULL,
489 exact_match, exact_lock, p);
490 if (error)
491 return error;
492
493 kobject_get(p->kobj.parent);
494
495 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
Logan Gunthorpe233ed092017-03-17 12:48:08 -0600498/**
499 * cdev_set_parent() - set the parent kobject for a char device
500 * @p: the cdev structure
501 * @kobj: the kobject to take a reference to
502 *
503 * cdev_set_parent() sets a parent kobject which will be referenced
504 * appropriately so the parent is not freed before the cdev. This
505 * should be called before cdev_add.
506 */
507void cdev_set_parent(struct cdev *p, struct kobject *kobj)
508{
509 WARN_ON(!kobj->state_initialized);
510 p->kobj.parent = kobj;
511}
512
513/**
514 * cdev_device_add() - add a char device and it's corresponding
515 * struct device, linkink
516 * @dev: the device structure
517 * @cdev: the cdev structure
518 *
519 * cdev_device_add() adds the char device represented by @cdev to the system,
520 * just as cdev_add does. It then adds @dev to the system using device_add
521 * The dev_t for the char device will be taken from the struct device which
522 * needs to be initialized first. This helper function correctly takes a
523 * reference to the parent device so the parent will not get released until
524 * all references to the cdev are released.
525 *
526 * This helper uses dev->devt for the device number. If it is not set
527 * it will not add the cdev and it will be equivalent to device_add.
528 *
529 * This function should be used whenever the struct cdev and the
530 * struct device are members of the same structure whose lifetime is
531 * managed by the struct device.
532 *
533 * NOTE: Callers must assume that userspace was able to open the cdev and
534 * can call cdev fops callbacks at any time, even if this function fails.
535 */
536int cdev_device_add(struct cdev *cdev, struct device *dev)
537{
538 int rc = 0;
539
540 if (dev->devt) {
541 cdev_set_parent(cdev, &dev->kobj);
542
543 rc = cdev_add(cdev, dev->devt, 1);
544 if (rc)
545 return rc;
546 }
547
548 rc = device_add(dev);
549 if (rc)
550 cdev_del(cdev);
551
552 return rc;
553}
554
555/**
556 * cdev_device_del() - inverse of cdev_device_add
557 * @dev: the device structure
558 * @cdev: the cdev structure
559 *
560 * cdev_device_del() is a helper function to call cdev_del and device_del.
561 * It should be used whenever cdev_device_add is used.
562 *
563 * If dev->devt is not set it will not remove the cdev and will be equivalent
564 * to device_del.
565 *
566 * NOTE: This guarantees that associated sysfs callbacks are not running
567 * or runnable, however any cdevs already open will remain and their fops
568 * will still be callable even after this function returns.
569 */
570void cdev_device_del(struct cdev *cdev, struct device *dev)
571{
572 device_del(dev);
573 if (dev->devt)
574 cdev_del(cdev);
575}
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577static void cdev_unmap(dev_t dev, unsigned count)
578{
579 kobj_unmap(cdev_map, dev, count);
580}
581
Jonathan Corbetcf3e43d2006-09-29 02:00:44 -0700582/**
583 * cdev_del() - remove a cdev from the system
584 * @p: the cdev structure to be removed
585 *
586 * cdev_del() removes @p from the system, possibly freeing the structure
587 * itself.
Logan Gunthorpe233ed092017-03-17 12:48:08 -0600588 *
589 * NOTE: This guarantees that cdev device will no longer be able to be
590 * opened, however any cdevs already open will remain and their fops will
591 * still be callable even after cdev_del returns.
Jonathan Corbetcf3e43d2006-09-29 02:00:44 -0700592 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593void cdev_del(struct cdev *p)
594{
595 cdev_unmap(p->dev, p->count);
596 kobject_put(&p->kobj);
597}
598
599
600static void cdev_default_release(struct kobject *kobj)
601{
602 struct cdev *p = container_of(kobj, struct cdev, kobj);
Dmitry Torokhov2f0157f2012-10-21 17:57:19 -0700603 struct kobject *parent = kobj->parent;
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 cdev_purge(p);
Dmitry Torokhov2f0157f2012-10-21 17:57:19 -0700606 kobject_put(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607}
608
609static void cdev_dynamic_release(struct kobject *kobj)
610{
611 struct cdev *p = container_of(kobj, struct cdev, kobj);
Dmitry Torokhov2f0157f2012-10-21 17:57:19 -0700612 struct kobject *parent = kobj->parent;
613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 cdev_purge(p);
615 kfree(p);
Dmitry Torokhov2f0157f2012-10-21 17:57:19 -0700616 kobject_put(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617}
618
619static struct kobj_type ktype_cdev_default = {
620 .release = cdev_default_release,
621};
622
623static struct kobj_type ktype_cdev_dynamic = {
624 .release = cdev_dynamic_release,
625};
626
Jonathan Corbetcf3e43d2006-09-29 02:00:44 -0700627/**
628 * cdev_alloc() - allocate a cdev structure
629 *
630 * Allocates and returns a cdev structure, or NULL on failure.
631 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632struct cdev *cdev_alloc(void)
633{
Oliver Neukum11b0b5a2006-03-25 03:08:13 -0800634 struct cdev *p = kzalloc(sizeof(struct cdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 if (p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 INIT_LIST_HEAD(&p->list);
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700637 kobject_init(&p->kobj, &ktype_cdev_dynamic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
639 return p;
640}
641
Jonathan Corbetcf3e43d2006-09-29 02:00:44 -0700642/**
643 * cdev_init() - initialize a cdev structure
644 * @cdev: the structure to initialize
645 * @fops: the file_operations for this device
646 *
647 * Initializes @cdev, remembering @fops, making it ready to add to the
648 * system with cdev_add().
649 */
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800650void cdev_init(struct cdev *cdev, const struct file_operations *fops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
652 memset(cdev, 0, sizeof *cdev);
653 INIT_LIST_HEAD(&cdev->list);
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -0700654 kobject_init(&cdev->kobj, &ktype_cdev_default);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 cdev->ops = fops;
656}
657
658static struct kobject *base_probe(dev_t dev, int *part, void *data)
659{
660 if (request_module("char-major-%d-%d", MAJOR(dev), MINOR(dev)) > 0)
661 /* Make old-style 2.4 aliases work */
662 request_module("char-major-%d", MAJOR(dev));
663 return NULL;
664}
665
666void __init chrdev_init(void)
667{
668 cdev_map = kobj_map_init(base_probe, &chrdevs_lock);
669}
670
671
672/* Let modules do char dev stuff */
673EXPORT_SYMBOL(register_chrdev_region);
674EXPORT_SYMBOL(unregister_chrdev_region);
675EXPORT_SYMBOL(alloc_chrdev_region);
676EXPORT_SYMBOL(cdev_init);
677EXPORT_SYMBOL(cdev_alloc);
678EXPORT_SYMBOL(cdev_del);
679EXPORT_SYMBOL(cdev_add);
Logan Gunthorpe233ed092017-03-17 12:48:08 -0600680EXPORT_SYMBOL(cdev_set_parent);
681EXPORT_SYMBOL(cdev_device_add);
682EXPORT_SYMBOL(cdev_device_del);
Tejun Heo1905b1b2009-08-06 18:13:23 +0900683EXPORT_SYMBOL(__register_chrdev);
684EXPORT_SYMBOL(__unregister_chrdev);