xref: /linux-6.15/fs/char_dev.c (revision 1e756248)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  linux/fs/char_dev.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992  Linus Torvalds
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds #include <linux/init.h>
91da177e4SLinus Torvalds #include <linux/fs.h>
10b446b60eSAndrew Morton #include <linux/kdev_t.h>
111da177e4SLinus Torvalds #include <linux/slab.h>
121da177e4SLinus Torvalds #include <linux/string.h>
131da177e4SLinus Torvalds 
141da177e4SLinus Torvalds #include <linux/major.h>
151da177e4SLinus Torvalds #include <linux/errno.h>
161da177e4SLinus Torvalds #include <linux/module.h>
1768eef3b4SJoe Korty #include <linux/seq_file.h>
181da177e4SLinus Torvalds 
191da177e4SLinus Torvalds #include <linux/kobject.h>
201da177e4SLinus Torvalds #include <linux/kobj_map.h>
211da177e4SLinus Torvalds #include <linux/cdev.h>
2258383af6SJes Sorensen #include <linux/mutex.h>
235da6185bSDavid Howells #include <linux/backing-dev.h>
2431d1d48eSDavid Howells #include <linux/tty.h>
251da177e4SLinus Torvalds 
2607f3f05cSDavid Howells #include "internal.h"
271da177e4SLinus Torvalds 
2868279f9cSAlexey Dobriyan static struct kobj_map *cdev_map __ro_after_init;
291da177e4SLinus Torvalds 
3058383af6SJes Sorensen static DEFINE_MUTEX(chrdevs_lock);
311da177e4SLinus Torvalds 
328a932f73SLogan Gunthorpe #define CHRDEV_MAJOR_HASH_SIZE 255
338a932f73SLogan Gunthorpe 
341da177e4SLinus Torvalds static struct char_device_struct {
351da177e4SLinus Torvalds 	struct char_device_struct *next;
361da177e4SLinus Torvalds 	unsigned int major;
371da177e4SLinus Torvalds 	unsigned int baseminor;
381da177e4SLinus Torvalds 	int minorct;
397170be5fSNeil Horman 	char name[64];
401da177e4SLinus Torvalds 	struct cdev *cdev;		/* will die */
4168eef3b4SJoe Korty } *chrdevs[CHRDEV_MAJOR_HASH_SIZE];
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds /* index in the above */
major_to_index(unsigned major)44e61eb2e9SYang Zhang static inline int major_to_index(unsigned major)
451da177e4SLinus Torvalds {
4668eef3b4SJoe Korty 	return major % CHRDEV_MAJOR_HASH_SIZE;
471da177e4SLinus Torvalds }
481da177e4SLinus Torvalds 
4968eef3b4SJoe Korty #ifdef CONFIG_PROC_FS
5068eef3b4SJoe Korty 
chrdev_show(struct seq_file * f,off_t offset)5168eef3b4SJoe Korty void chrdev_show(struct seq_file *f, off_t offset)
5268eef3b4SJoe Korty {
537170be5fSNeil Horman 	struct char_device_struct *cd;
547170be5fSNeil Horman 
5558383af6SJes Sorensen 	mutex_lock(&chrdevs_lock);
568a932f73SLogan Gunthorpe 	for (cd = chrdevs[major_to_index(offset)]; cd; cd = cd->next) {
578a932f73SLogan Gunthorpe 		if (cd->major == offset)
5868eef3b4SJoe Korty 			seq_printf(f, "%3d %s\n", cd->major, cd->name);
5968eef3b4SJoe Korty 	}
608a932f73SLogan Gunthorpe 	mutex_unlock(&chrdevs_lock);
617170be5fSNeil Horman }
627170be5fSNeil Horman 
6368eef3b4SJoe Korty #endif /* CONFIG_PROC_FS */
641da177e4SLinus Torvalds 
find_dynamic_major(void)65a5d31a3fSLogan Gunthorpe static int find_dynamic_major(void)
66a5d31a3fSLogan Gunthorpe {
67a5d31a3fSLogan Gunthorpe 	int i;
68a5d31a3fSLogan Gunthorpe 	struct char_device_struct *cd;
69a5d31a3fSLogan Gunthorpe 
70652d703bSSrivatsa S. Bhat 	for (i = ARRAY_SIZE(chrdevs)-1; i >= CHRDEV_MAJOR_DYN_END; i--) {
71a5d31a3fSLogan Gunthorpe 		if (chrdevs[i] == NULL)
72a5d31a3fSLogan Gunthorpe 			return i;
73a5d31a3fSLogan Gunthorpe 	}
74a5d31a3fSLogan Gunthorpe 
75a5d31a3fSLogan Gunthorpe 	for (i = CHRDEV_MAJOR_DYN_EXT_START;
76652d703bSSrivatsa S. Bhat 	     i >= CHRDEV_MAJOR_DYN_EXT_END; i--) {
77a5d31a3fSLogan Gunthorpe 		for (cd = chrdevs[major_to_index(i)]; cd; cd = cd->next)
78a5d31a3fSLogan Gunthorpe 			if (cd->major == i)
79a5d31a3fSLogan Gunthorpe 				break;
80a5d31a3fSLogan Gunthorpe 
81652d703bSSrivatsa S. Bhat 		if (cd == NULL)
82a5d31a3fSLogan Gunthorpe 			return i;
83a5d31a3fSLogan Gunthorpe 	}
84a5d31a3fSLogan Gunthorpe 
85a5d31a3fSLogan Gunthorpe 	return -EBUSY;
86a5d31a3fSLogan Gunthorpe }
87a5d31a3fSLogan Gunthorpe 
881da177e4SLinus Torvalds /*
891da177e4SLinus Torvalds  * Register a single major with a specified minor range.
901da177e4SLinus Torvalds  *
91d358b173SChengguang Xu  * If major == 0 this function will dynamically allocate an unused major.
92d358b173SChengguang Xu  * If major > 0 this function will attempt to reserve the range of minors
93d358b173SChengguang Xu  * with given major.
941da177e4SLinus Torvalds  *
951da177e4SLinus Torvalds  */
961da177e4SLinus Torvalds static struct char_device_struct *
__register_chrdev_region(unsigned int major,unsigned int baseminor,int minorct,const char * name)971da177e4SLinus Torvalds __register_chrdev_region(unsigned int major, unsigned int baseminor,
981da177e4SLinus Torvalds 			   int minorct, const char *name)
991da177e4SLinus Torvalds {
1004b0be572SChengguang Xu 	struct char_device_struct *cd, *curr, *prev = NULL;
1017ef0b152SChengguang Xu 	int ret;
1021da177e4SLinus Torvalds 	int i;
1031da177e4SLinus Torvalds 
1044b0be572SChengguang Xu 	if (major >= CHRDEV_MAJOR_MAX) {
1054b0be572SChengguang Xu 		pr_err("CHRDEV \"%s\" major requested (%u) is greater than the maximum (%u)\n",
1064b0be572SChengguang Xu 		       name, major, CHRDEV_MAJOR_MAX-1);
1074b0be572SChengguang Xu 		return ERR_PTR(-EINVAL);
1084b0be572SChengguang Xu 	}
1094b0be572SChengguang Xu 
1104712d379SChengguang Xu 	if (minorct > MINORMASK + 1 - baseminor) {
1114712d379SChengguang Xu 		pr_err("CHRDEV \"%s\" minor range requested (%u-%u) is out of range of maximum range (%u-%u) for a single major\n",
1124712d379SChengguang Xu 			name, baseminor, baseminor + minorct - 1, 0, MINORMASK);
1134712d379SChengguang Xu 		return ERR_PTR(-EINVAL);
1144712d379SChengguang Xu 	}
1154712d379SChengguang Xu 
11611b0b5abSOliver Neukum 	cd = kzalloc(sizeof(struct char_device_struct), GFP_KERNEL);
1171da177e4SLinus Torvalds 	if (cd == NULL)
1181da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
1191da177e4SLinus Torvalds 
12058383af6SJes Sorensen 	mutex_lock(&chrdevs_lock);
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds 	if (major == 0) {
123a5d31a3fSLogan Gunthorpe 		ret = find_dynamic_major();
124a5d31a3fSLogan Gunthorpe 		if (ret < 0) {
125a5d31a3fSLogan Gunthorpe 			pr_err("CHRDEV \"%s\" dynamic allocation region is full\n",
126a5d31a3fSLogan Gunthorpe 			       name);
1271da177e4SLinus Torvalds 			goto out;
1281da177e4SLinus Torvalds 		}
129a5d31a3fSLogan Gunthorpe 		major = ret;
1301da177e4SLinus Torvalds 	}
1311da177e4SLinus Torvalds 
1327ef0b152SChengguang Xu 	ret = -EBUSY;
1334b0be572SChengguang Xu 	i = major_to_index(major);
1344b0be572SChengguang Xu 	for (curr = chrdevs[i]; curr; prev = curr, curr = curr->next) {
1354b0be572SChengguang Xu 		if (curr->major < major)
1364b0be572SChengguang Xu 			continue;
1374b0be572SChengguang Xu 
1384b0be572SChengguang Xu 		if (curr->major > major)
1394b0be572SChengguang Xu 			break;
1404b0be572SChengguang Xu 
1414b0be572SChengguang Xu 		if (curr->baseminor + curr->minorct <= baseminor)
1424b0be572SChengguang Xu 			continue;
1434b0be572SChengguang Xu 
1444b0be572SChengguang Xu 		if (curr->baseminor >= baseminor + minorct)
1454b0be572SChengguang Xu 			break;
1464b0be572SChengguang Xu 
1478a932f73SLogan Gunthorpe 		goto out;
1488a932f73SLogan Gunthorpe 	}
1498a932f73SLogan Gunthorpe 
1501da177e4SLinus Torvalds 	cd->major = major;
1511da177e4SLinus Torvalds 	cd->baseminor = baseminor;
1521da177e4SLinus Torvalds 	cd->minorct = minorct;
153c642256bSAzeem Shaikh 	strscpy(cd->name, name, sizeof(cd->name));
1541da177e4SLinus Torvalds 
1554b0be572SChengguang Xu 	if (!prev) {
1564b0be572SChengguang Xu 		cd->next = curr;
1574b0be572SChengguang Xu 		chrdevs[i] = cd;
1584b0be572SChengguang Xu 	} else {
1594b0be572SChengguang Xu 		cd->next = prev->next;
1604b0be572SChengguang Xu 		prev->next = cd;
1611da177e4SLinus Torvalds 	}
16201d553d0SAmos Waterland 
16358383af6SJes Sorensen 	mutex_unlock(&chrdevs_lock);
1641da177e4SLinus Torvalds 	return cd;
1651da177e4SLinus Torvalds out:
16658383af6SJes Sorensen 	mutex_unlock(&chrdevs_lock);
1671da177e4SLinus Torvalds 	kfree(cd);
1681da177e4SLinus Torvalds 	return ERR_PTR(ret);
1691da177e4SLinus Torvalds }
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds static struct char_device_struct *
__unregister_chrdev_region(unsigned major,unsigned baseminor,int minorct)1721da177e4SLinus Torvalds __unregister_chrdev_region(unsigned major, unsigned baseminor, int minorct)
1731da177e4SLinus Torvalds {
1741da177e4SLinus Torvalds 	struct char_device_struct *cd = NULL, **cp;
1751da177e4SLinus Torvalds 	int i = major_to_index(major);
1761da177e4SLinus Torvalds 
17758383af6SJes Sorensen 	mutex_lock(&chrdevs_lock);
1781da177e4SLinus Torvalds 	for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next)
1791da177e4SLinus Torvalds 		if ((*cp)->major == major &&
1801da177e4SLinus Torvalds 		    (*cp)->baseminor == baseminor &&
1811da177e4SLinus Torvalds 		    (*cp)->minorct == minorct)
1821da177e4SLinus Torvalds 			break;
1831da177e4SLinus Torvalds 	if (*cp) {
1841da177e4SLinus Torvalds 		cd = *cp;
1851da177e4SLinus Torvalds 		*cp = cd->next;
1861da177e4SLinus Torvalds 	}
18758383af6SJes Sorensen 	mutex_unlock(&chrdevs_lock);
1881da177e4SLinus Torvalds 	return cd;
1891da177e4SLinus Torvalds }
1901da177e4SLinus Torvalds 
191cf3e43dbSJonathan Corbet /**
192cf3e43dbSJonathan Corbet  * register_chrdev_region() - register a range of device numbers
193cf3e43dbSJonathan Corbet  * @from: the first in the desired range of device numbers; must include
194cf3e43dbSJonathan Corbet  *        the major number.
195cf3e43dbSJonathan Corbet  * @count: the number of consecutive device numbers required
196cf3e43dbSJonathan Corbet  * @name: the name of the device or driver.
197cf3e43dbSJonathan Corbet  *
198cf3e43dbSJonathan Corbet  * Return value is zero on success, a negative error code on failure.
199cf3e43dbSJonathan Corbet  */
register_chrdev_region(dev_t from,unsigned count,const char * name)2001da177e4SLinus Torvalds int register_chrdev_region(dev_t from, unsigned count, const char *name)
2011da177e4SLinus Torvalds {
2021da177e4SLinus Torvalds 	struct char_device_struct *cd;
2031da177e4SLinus Torvalds 	dev_t to = from + count;
2041da177e4SLinus Torvalds 	dev_t n, next;
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds 	for (n = from; n < to; n = next) {
2071da177e4SLinus Torvalds 		next = MKDEV(MAJOR(n)+1, 0);
2081da177e4SLinus Torvalds 		if (next > to)
2091da177e4SLinus Torvalds 			next = to;
2101da177e4SLinus Torvalds 		cd = __register_chrdev_region(MAJOR(n), MINOR(n),
2111da177e4SLinus Torvalds 			       next - n, name);
2121da177e4SLinus Torvalds 		if (IS_ERR(cd))
2131da177e4SLinus Torvalds 			goto fail;
2141da177e4SLinus Torvalds 	}
2151da177e4SLinus Torvalds 	return 0;
2161da177e4SLinus Torvalds fail:
2171da177e4SLinus Torvalds 	to = n;
2181da177e4SLinus Torvalds 	for (n = from; n < to; n = next) {
2191da177e4SLinus Torvalds 		next = MKDEV(MAJOR(n)+1, 0);
2201da177e4SLinus Torvalds 		kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
2211da177e4SLinus Torvalds 	}
2221da177e4SLinus Torvalds 	return PTR_ERR(cd);
2231da177e4SLinus Torvalds }
2241da177e4SLinus Torvalds 
225cf3e43dbSJonathan Corbet /**
226cf3e43dbSJonathan Corbet  * alloc_chrdev_region() - register a range of char device numbers
227cf3e43dbSJonathan Corbet  * @dev: output parameter for first assigned number
228cf3e43dbSJonathan Corbet  * @baseminor: first of the requested range of minor numbers
229cf3e43dbSJonathan Corbet  * @count: the number of minor numbers required
230cf3e43dbSJonathan Corbet  * @name: the name of the associated device or driver
231cf3e43dbSJonathan Corbet  *
232cf3e43dbSJonathan Corbet  * Allocates a range of char device numbers.  The major number will be
233cf3e43dbSJonathan Corbet  * chosen dynamically, and returned (along with the first minor number)
234cf3e43dbSJonathan Corbet  * in @dev.  Returns zero or a negative error code.
235cf3e43dbSJonathan Corbet  */
alloc_chrdev_region(dev_t * dev,unsigned baseminor,unsigned count,const char * name)2361da177e4SLinus Torvalds int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count,
2371da177e4SLinus Torvalds 			const char *name)
2381da177e4SLinus Torvalds {
2391da177e4SLinus Torvalds 	struct char_device_struct *cd;
2401da177e4SLinus Torvalds 	cd = __register_chrdev_region(0, baseminor, count, name);
2411da177e4SLinus Torvalds 	if (IS_ERR(cd))
2421da177e4SLinus Torvalds 		return PTR_ERR(cd);
2431da177e4SLinus Torvalds 	*dev = MKDEV(cd->major, cd->baseminor);
2441da177e4SLinus Torvalds 	return 0;
2451da177e4SLinus Torvalds }
2461da177e4SLinus Torvalds 
247d247e2c6SRolf Eike Beer /**
2481905b1bfSTejun Heo  * __register_chrdev() - create and register a cdev occupying a range of minors
249d247e2c6SRolf Eike Beer  * @major: major device number or 0 for dynamic allocation
2501905b1bfSTejun Heo  * @baseminor: first of the requested range of minor numbers
2511905b1bfSTejun Heo  * @count: the number of minor numbers required
252d247e2c6SRolf Eike Beer  * @name: name of this range of devices
253d247e2c6SRolf Eike Beer  * @fops: file operations associated with this devices
254d247e2c6SRolf Eike Beer  *
255d247e2c6SRolf Eike Beer  * If @major == 0 this functions will dynamically allocate a major and return
256d247e2c6SRolf Eike Beer  * its number.
257d247e2c6SRolf Eike Beer  *
258d247e2c6SRolf Eike Beer  * If @major > 0 this function will attempt to reserve a device with the given
259d247e2c6SRolf Eike Beer  * major number and will return zero on success.
260d247e2c6SRolf Eike Beer  *
261d247e2c6SRolf Eike Beer  * Returns a -ve errno on failure.
262d247e2c6SRolf Eike Beer  *
263d247e2c6SRolf Eike Beer  * The name of this device has nothing to do with the name of the device in
264d247e2c6SRolf Eike Beer  * /dev. It only helps to keep track of the different owners of devices. If
265d247e2c6SRolf Eike Beer  * your module name has only one type of devices it's ok to use e.g. the name
266d247e2c6SRolf Eike Beer  * of the module here.
267d247e2c6SRolf Eike Beer  */
__register_chrdev(unsigned int major,unsigned int baseminor,unsigned int count,const char * name,const struct file_operations * fops)2681905b1bfSTejun Heo int __register_chrdev(unsigned int major, unsigned int baseminor,
2691905b1bfSTejun Heo 		      unsigned int count, const char *name,
27099ac48f5SArjan van de Ven 		      const struct file_operations *fops)
2711da177e4SLinus Torvalds {
2721da177e4SLinus Torvalds 	struct char_device_struct *cd;
2731da177e4SLinus Torvalds 	struct cdev *cdev;
2741da177e4SLinus Torvalds 	int err = -ENOMEM;
2751da177e4SLinus Torvalds 
2761905b1bfSTejun Heo 	cd = __register_chrdev_region(major, baseminor, count, name);
2771da177e4SLinus Torvalds 	if (IS_ERR(cd))
2781da177e4SLinus Torvalds 		return PTR_ERR(cd);
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds 	cdev = cdev_alloc();
2811da177e4SLinus Torvalds 	if (!cdev)
2821da177e4SLinus Torvalds 		goto out2;
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds 	cdev->owner = fops->owner;
2851da177e4SLinus Torvalds 	cdev->ops = fops;
2861da177e4SLinus Torvalds 	kobject_set_name(&cdev->kobj, "%s", name);
2871da177e4SLinus Torvalds 
2881905b1bfSTejun Heo 	err = cdev_add(cdev, MKDEV(cd->major, baseminor), count);
2891da177e4SLinus Torvalds 	if (err)
2901da177e4SLinus Torvalds 		goto out;
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds 	cd->cdev = cdev;
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds 	return major ? 0 : cd->major;
2951da177e4SLinus Torvalds out:
2961da177e4SLinus Torvalds 	kobject_put(&cdev->kobj);
2971da177e4SLinus Torvalds out2:
2981905b1bfSTejun Heo 	kfree(__unregister_chrdev_region(cd->major, baseminor, count));
2991da177e4SLinus Torvalds 	return err;
3001da177e4SLinus Torvalds }
3011da177e4SLinus Torvalds 
302cf3e43dbSJonathan Corbet /**
303594069bcSPartha Pratim Mukherjee  * unregister_chrdev_region() - unregister a range of device numbers
304cf3e43dbSJonathan Corbet  * @from: the first in the range of numbers to unregister
305cf3e43dbSJonathan Corbet  * @count: the number of device numbers to unregister
306cf3e43dbSJonathan Corbet  *
307cf3e43dbSJonathan Corbet  * This function will unregister a range of @count device numbers,
308cf3e43dbSJonathan Corbet  * starting with @from.  The caller should normally be the one who
309cf3e43dbSJonathan Corbet  * allocated those numbers in the first place...
310cf3e43dbSJonathan Corbet  */
unregister_chrdev_region(dev_t from,unsigned count)3111da177e4SLinus Torvalds void unregister_chrdev_region(dev_t from, unsigned count)
3121da177e4SLinus Torvalds {
3131da177e4SLinus Torvalds 	dev_t to = from + count;
3141da177e4SLinus Torvalds 	dev_t n, next;
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds 	for (n = from; n < to; n = next) {
3171da177e4SLinus Torvalds 		next = MKDEV(MAJOR(n)+1, 0);
3181da177e4SLinus Torvalds 		if (next > to)
3191da177e4SLinus Torvalds 			next = to;
3201da177e4SLinus Torvalds 		kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
3211da177e4SLinus Torvalds 	}
3221da177e4SLinus Torvalds }
3231da177e4SLinus Torvalds 
3241905b1bfSTejun Heo /**
3251905b1bfSTejun Heo  * __unregister_chrdev - unregister and destroy a cdev
3261905b1bfSTejun Heo  * @major: major device number
3271905b1bfSTejun Heo  * @baseminor: first of the range of minor numbers
3281905b1bfSTejun Heo  * @count: the number of minor numbers this cdev is occupying
3291905b1bfSTejun Heo  * @name: name of this range of devices
3301905b1bfSTejun Heo  *
3311905b1bfSTejun Heo  * Unregister and destroy the cdev occupying the region described by
3321905b1bfSTejun Heo  * @major, @baseminor and @count.  This function undoes what
3331905b1bfSTejun Heo  * __register_chrdev() did.
3341905b1bfSTejun Heo  */
__unregister_chrdev(unsigned int major,unsigned int baseminor,unsigned int count,const char * name)3351905b1bfSTejun Heo void __unregister_chrdev(unsigned int major, unsigned int baseminor,
3361905b1bfSTejun Heo 			 unsigned int count, const char *name)
3371da177e4SLinus Torvalds {
3381da177e4SLinus Torvalds 	struct char_device_struct *cd;
3391905b1bfSTejun Heo 
3401905b1bfSTejun Heo 	cd = __unregister_chrdev_region(major, baseminor, count);
3411da177e4SLinus Torvalds 	if (cd && cd->cdev)
3421da177e4SLinus Torvalds 		cdev_del(cd->cdev);
3431da177e4SLinus Torvalds 	kfree(cd);
3441da177e4SLinus Torvalds }
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds static DEFINE_SPINLOCK(cdev_lock);
3471da177e4SLinus Torvalds 
cdev_get(struct cdev * p)3481da177e4SLinus Torvalds static struct kobject *cdev_get(struct cdev *p)
3491da177e4SLinus Torvalds {
3501da177e4SLinus Torvalds 	struct module *owner = p->owner;
3511da177e4SLinus Torvalds 	struct kobject *kobj;
3521da177e4SLinus Torvalds 
353e311ba29SUwe Kleine-König 	if (!try_module_get(owner))
3541da177e4SLinus Torvalds 		return NULL;
35568faa679SWill Deacon 	kobj = kobject_get_unless_zero(&p->kobj);
3561da177e4SLinus Torvalds 	if (!kobj)
3571da177e4SLinus Torvalds 		module_put(owner);
3581da177e4SLinus Torvalds 	return kobj;
3591da177e4SLinus Torvalds }
3601da177e4SLinus Torvalds 
cdev_put(struct cdev * p)3611da177e4SLinus Torvalds void cdev_put(struct cdev *p)
3621da177e4SLinus Torvalds {
3631da177e4SLinus Torvalds 	if (p) {
3647da6844cSBrian King 		struct module *owner = p->owner;
3651da177e4SLinus Torvalds 		kobject_put(&p->kobj);
3667da6844cSBrian King 		module_put(owner);
3671da177e4SLinus Torvalds 	}
3681da177e4SLinus Torvalds }
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds /*
3711da177e4SLinus Torvalds  * Called every time a character special file is opened
3721da177e4SLinus Torvalds  */
chrdev_open(struct inode * inode,struct file * filp)373922f9cfaSDenis Cheng static int chrdev_open(struct inode *inode, struct file *filp)
3741da177e4SLinus Torvalds {
375e84f9e57SAl Viro 	const struct file_operations *fops;
3761da177e4SLinus Torvalds 	struct cdev *p;
3771da177e4SLinus Torvalds 	struct cdev *new = NULL;
3781da177e4SLinus Torvalds 	int ret = 0;
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds 	spin_lock(&cdev_lock);
3811da177e4SLinus Torvalds 	p = inode->i_cdev;
3821da177e4SLinus Torvalds 	if (!p) {
3831da177e4SLinus Torvalds 		struct kobject *kobj;
3841da177e4SLinus Torvalds 		int idx;
3851da177e4SLinus Torvalds 		spin_unlock(&cdev_lock);
3861da177e4SLinus Torvalds 		kobj = kobj_lookup(cdev_map, inode->i_rdev, &idx);
3871da177e4SLinus Torvalds 		if (!kobj)
3881da177e4SLinus Torvalds 			return -ENXIO;
3891da177e4SLinus Torvalds 		new = container_of(kobj, struct cdev, kobj);
3901da177e4SLinus Torvalds 		spin_lock(&cdev_lock);
391a30427d9SJonathan Corbet 		/* Check i_cdev again in case somebody beat us to it while
392a30427d9SJonathan Corbet 		   we dropped the lock. */
3931da177e4SLinus Torvalds 		p = inode->i_cdev;
3941da177e4SLinus Torvalds 		if (!p) {
3951da177e4SLinus Torvalds 			inode->i_cdev = p = new;
3961da177e4SLinus Torvalds 			list_add(&inode->i_devices, &p->list);
3971da177e4SLinus Torvalds 			new = NULL;
3981da177e4SLinus Torvalds 		} else if (!cdev_get(p))
3991da177e4SLinus Torvalds 			ret = -ENXIO;
4001da177e4SLinus Torvalds 	} else if (!cdev_get(p))
4011da177e4SLinus Torvalds 		ret = -ENXIO;
4021da177e4SLinus Torvalds 	spin_unlock(&cdev_lock);
4031da177e4SLinus Torvalds 	cdev_put(new);
4041da177e4SLinus Torvalds 	if (ret)
4051da177e4SLinus Torvalds 		return ret;
406a518ab93SChristoph Hellwig 
407a518ab93SChristoph Hellwig 	ret = -ENXIO;
408e84f9e57SAl Viro 	fops = fops_get(p->ops);
409e84f9e57SAl Viro 	if (!fops)
410a518ab93SChristoph Hellwig 		goto out_cdev_put;
411a518ab93SChristoph Hellwig 
412e84f9e57SAl Viro 	replace_fops(filp, fops);
413a518ab93SChristoph Hellwig 	if (filp->f_op->open) {
4141da177e4SLinus Torvalds 		ret = filp->f_op->open(inode, filp);
4151da177e4SLinus Torvalds 		if (ret)
416a518ab93SChristoph Hellwig 			goto out_cdev_put;
417a518ab93SChristoph Hellwig 	}
418a518ab93SChristoph Hellwig 
419a518ab93SChristoph Hellwig 	return 0;
420a518ab93SChristoph Hellwig 
421a518ab93SChristoph Hellwig  out_cdev_put:
4221da177e4SLinus Torvalds 	cdev_put(p);
4231da177e4SLinus Torvalds 	return ret;
4241da177e4SLinus Torvalds }
4251da177e4SLinus Torvalds 
cd_forget(struct inode * inode)4261da177e4SLinus Torvalds void cd_forget(struct inode *inode)
4271da177e4SLinus Torvalds {
4281da177e4SLinus Torvalds 	spin_lock(&cdev_lock);
4291da177e4SLinus Torvalds 	list_del_init(&inode->i_devices);
4301da177e4SLinus Torvalds 	inode->i_cdev = NULL;
4313bc52c45SDan Williams 	inode->i_mapping = &inode->i_data;
4321da177e4SLinus Torvalds 	spin_unlock(&cdev_lock);
4331da177e4SLinus Torvalds }
4341da177e4SLinus Torvalds 
cdev_purge(struct cdev * cdev)43575c96f85SAdrian Bunk static void cdev_purge(struct cdev *cdev)
4361da177e4SLinus Torvalds {
4371da177e4SLinus Torvalds 	spin_lock(&cdev_lock);
4381da177e4SLinus Torvalds 	while (!list_empty(&cdev->list)) {
4391da177e4SLinus Torvalds 		struct inode *inode;
4401da177e4SLinus Torvalds 		inode = container_of(cdev->list.next, struct inode, i_devices);
4411da177e4SLinus Torvalds 		list_del_init(&inode->i_devices);
4421da177e4SLinus Torvalds 		inode->i_cdev = NULL;
4431da177e4SLinus Torvalds 	}
4441da177e4SLinus Torvalds 	spin_unlock(&cdev_lock);
4451da177e4SLinus Torvalds }
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds /*
4481da177e4SLinus Torvalds  * Dummy default file-operations: the only thing this does
4491da177e4SLinus Torvalds  * is contain the open that then fills in the correct operations
4501da177e4SLinus Torvalds  * depending on the special file...
4511da177e4SLinus Torvalds  */
4524b6f5d20SArjan van de Ven const struct file_operations def_chr_fops = {
4531da177e4SLinus Torvalds 	.open = chrdev_open,
4546038f373SArnd Bergmann 	.llseek = noop_llseek,
4551da177e4SLinus Torvalds };
4561da177e4SLinus Torvalds 
exact_match(dev_t dev,int * part,void * data)4571da177e4SLinus Torvalds static struct kobject *exact_match(dev_t dev, int *part, void *data)
4581da177e4SLinus Torvalds {
4591da177e4SLinus Torvalds 	struct cdev *p = data;
4601da177e4SLinus Torvalds 	return &p->kobj;
4611da177e4SLinus Torvalds }
4621da177e4SLinus Torvalds 
exact_lock(dev_t dev,void * data)4631da177e4SLinus Torvalds static int exact_lock(dev_t dev, void *data)
4641da177e4SLinus Torvalds {
4651da177e4SLinus Torvalds 	struct cdev *p = data;
4661da177e4SLinus Torvalds 	return cdev_get(p) ? 0 : -1;
4671da177e4SLinus Torvalds }
4681da177e4SLinus Torvalds 
469cf3e43dbSJonathan Corbet /**
470cf3e43dbSJonathan Corbet  * cdev_add() - add a char device to the system
471cf3e43dbSJonathan Corbet  * @p: the cdev structure for the device
472cf3e43dbSJonathan Corbet  * @dev: the first device number for which this device is responsible
473cf3e43dbSJonathan Corbet  * @count: the number of consecutive minor numbers corresponding to this
474cf3e43dbSJonathan Corbet  *         device
475cf3e43dbSJonathan Corbet  *
476cf3e43dbSJonathan Corbet  * cdev_add() adds the device represented by @p to the system, making it
477cf3e43dbSJonathan Corbet  * live immediately.  A negative error code is returned on failure.
478cf3e43dbSJonathan Corbet  */
cdev_add(struct cdev * p,dev_t dev,unsigned count)4791da177e4SLinus Torvalds int cdev_add(struct cdev *p, dev_t dev, unsigned count)
4801da177e4SLinus Torvalds {
4812f0157f1SDmitry Torokhov 	int error;
4822f0157f1SDmitry Torokhov 
4831da177e4SLinus Torvalds 	p->dev = dev;
4841da177e4SLinus Torvalds 	p->count = count;
4852f0157f1SDmitry Torokhov 
4864634c973SShang XiaoJing 	if (WARN_ON(dev == WHITEOUT_DEV)) {
4874634c973SShang XiaoJing 		error = -EBUSY;
4884634c973SShang XiaoJing 		goto err;
4894634c973SShang XiaoJing 	}
490a3c751a5SMiklos Szeredi 
4912f0157f1SDmitry Torokhov 	error = kobj_map(cdev_map, dev, count, NULL,
4922f0157f1SDmitry Torokhov 			 exact_match, exact_lock, p);
4932f0157f1SDmitry Torokhov 	if (error)
4944634c973SShang XiaoJing 		goto err;
4952f0157f1SDmitry Torokhov 
4962f0157f1SDmitry Torokhov 	kobject_get(p->kobj.parent);
4972f0157f1SDmitry Torokhov 
4982f0157f1SDmitry Torokhov 	return 0;
4994634c973SShang XiaoJing 
5004634c973SShang XiaoJing err:
5014634c973SShang XiaoJing 	kfree_const(p->kobj.name);
5024634c973SShang XiaoJing 	p->kobj.name = NULL;
5034634c973SShang XiaoJing 	return error;
5041da177e4SLinus Torvalds }
5051da177e4SLinus Torvalds 
506233ed09dSLogan Gunthorpe /**
507233ed09dSLogan Gunthorpe  * cdev_set_parent() - set the parent kobject for a char device
508233ed09dSLogan Gunthorpe  * @p: the cdev structure
509233ed09dSLogan Gunthorpe  * @kobj: the kobject to take a reference to
510233ed09dSLogan Gunthorpe  *
511233ed09dSLogan Gunthorpe  * cdev_set_parent() sets a parent kobject which will be referenced
512233ed09dSLogan Gunthorpe  * appropriately so the parent is not freed before the cdev. This
513233ed09dSLogan Gunthorpe  * should be called before cdev_add.
514233ed09dSLogan Gunthorpe  */
cdev_set_parent(struct cdev * p,struct kobject * kobj)515233ed09dSLogan Gunthorpe void cdev_set_parent(struct cdev *p, struct kobject *kobj)
516233ed09dSLogan Gunthorpe {
517233ed09dSLogan Gunthorpe 	WARN_ON(!kobj->state_initialized);
518233ed09dSLogan Gunthorpe 	p->kobj.parent = kobj;
519233ed09dSLogan Gunthorpe }
520233ed09dSLogan Gunthorpe 
521233ed09dSLogan Gunthorpe /**
522233ed09dSLogan Gunthorpe  * cdev_device_add() - add a char device and it's corresponding
523233ed09dSLogan Gunthorpe  *	struct device, linkink
524233ed09dSLogan Gunthorpe  * @dev: the device structure
525233ed09dSLogan Gunthorpe  * @cdev: the cdev structure
526233ed09dSLogan Gunthorpe  *
527233ed09dSLogan Gunthorpe  * cdev_device_add() adds the char device represented by @cdev to the system,
528233ed09dSLogan Gunthorpe  * just as cdev_add does. It then adds @dev to the system using device_add
529233ed09dSLogan Gunthorpe  * The dev_t for the char device will be taken from the struct device which
530233ed09dSLogan Gunthorpe  * needs to be initialized first. This helper function correctly takes a
531233ed09dSLogan Gunthorpe  * reference to the parent device so the parent will not get released until
532233ed09dSLogan Gunthorpe  * all references to the cdev are released.
533233ed09dSLogan Gunthorpe  *
534233ed09dSLogan Gunthorpe  * This helper uses dev->devt for the device number. If it is not set
535233ed09dSLogan Gunthorpe  * it will not add the cdev and it will be equivalent to device_add.
536233ed09dSLogan Gunthorpe  *
537233ed09dSLogan Gunthorpe  * This function should be used whenever the struct cdev and the
538233ed09dSLogan Gunthorpe  * struct device are members of the same structure whose lifetime is
539233ed09dSLogan Gunthorpe  * managed by the struct device.
540233ed09dSLogan Gunthorpe  *
541233ed09dSLogan Gunthorpe  * NOTE: Callers must assume that userspace was able to open the cdev and
542233ed09dSLogan Gunthorpe  * can call cdev fops callbacks at any time, even if this function fails.
543233ed09dSLogan Gunthorpe  */
cdev_device_add(struct cdev * cdev,struct device * dev)544233ed09dSLogan Gunthorpe int cdev_device_add(struct cdev *cdev, struct device *dev)
545233ed09dSLogan Gunthorpe {
546233ed09dSLogan Gunthorpe 	int rc = 0;
547233ed09dSLogan Gunthorpe 
548233ed09dSLogan Gunthorpe 	if (dev->devt) {
549233ed09dSLogan Gunthorpe 		cdev_set_parent(cdev, &dev->kobj);
550233ed09dSLogan Gunthorpe 
551233ed09dSLogan Gunthorpe 		rc = cdev_add(cdev, dev->devt, 1);
552233ed09dSLogan Gunthorpe 		if (rc)
553233ed09dSLogan Gunthorpe 			return rc;
554233ed09dSLogan Gunthorpe 	}
555233ed09dSLogan Gunthorpe 
556233ed09dSLogan Gunthorpe 	rc = device_add(dev);
55711fa7fefSYang Yingliang 	if (rc && dev->devt)
558233ed09dSLogan Gunthorpe 		cdev_del(cdev);
559233ed09dSLogan Gunthorpe 
560233ed09dSLogan Gunthorpe 	return rc;
561233ed09dSLogan Gunthorpe }
562233ed09dSLogan Gunthorpe 
563233ed09dSLogan Gunthorpe /**
564233ed09dSLogan Gunthorpe  * cdev_device_del() - inverse of cdev_device_add
565233ed09dSLogan Gunthorpe  * @cdev: the cdev structure
566*1e756248SJulia Lawall  * @dev: the device structure
567233ed09dSLogan Gunthorpe  *
568233ed09dSLogan Gunthorpe  * cdev_device_del() is a helper function to call cdev_del and device_del.
569233ed09dSLogan Gunthorpe  * It should be used whenever cdev_device_add is used.
570233ed09dSLogan Gunthorpe  *
571233ed09dSLogan Gunthorpe  * If dev->devt is not set it will not remove the cdev and will be equivalent
572233ed09dSLogan Gunthorpe  * to device_del.
573233ed09dSLogan Gunthorpe  *
574233ed09dSLogan Gunthorpe  * NOTE: This guarantees that associated sysfs callbacks are not running
575233ed09dSLogan Gunthorpe  * or runnable, however any cdevs already open will remain and their fops
576233ed09dSLogan Gunthorpe  * will still be callable even after this function returns.
577233ed09dSLogan Gunthorpe  */
cdev_device_del(struct cdev * cdev,struct device * dev)578233ed09dSLogan Gunthorpe void cdev_device_del(struct cdev *cdev, struct device *dev)
579233ed09dSLogan Gunthorpe {
580233ed09dSLogan Gunthorpe 	device_del(dev);
581233ed09dSLogan Gunthorpe 	if (dev->devt)
582233ed09dSLogan Gunthorpe 		cdev_del(cdev);
583233ed09dSLogan Gunthorpe }
584233ed09dSLogan Gunthorpe 
cdev_unmap(dev_t dev,unsigned count)5851da177e4SLinus Torvalds static void cdev_unmap(dev_t dev, unsigned count)
5861da177e4SLinus Torvalds {
5871da177e4SLinus Torvalds 	kobj_unmap(cdev_map, dev, count);
5881da177e4SLinus Torvalds }
5891da177e4SLinus Torvalds 
590cf3e43dbSJonathan Corbet /**
591cf3e43dbSJonathan Corbet  * cdev_del() - remove a cdev from the system
592cf3e43dbSJonathan Corbet  * @p: the cdev structure to be removed
593cf3e43dbSJonathan Corbet  *
594cf3e43dbSJonathan Corbet  * cdev_del() removes @p from the system, possibly freeing the structure
595cf3e43dbSJonathan Corbet  * itself.
596233ed09dSLogan Gunthorpe  *
597233ed09dSLogan Gunthorpe  * NOTE: This guarantees that cdev device will no longer be able to be
598233ed09dSLogan Gunthorpe  * opened, however any cdevs already open will remain and their fops will
599233ed09dSLogan Gunthorpe  * still be callable even after cdev_del returns.
600cf3e43dbSJonathan Corbet  */
cdev_del(struct cdev * p)6011da177e4SLinus Torvalds void cdev_del(struct cdev *p)
6021da177e4SLinus Torvalds {
6031da177e4SLinus Torvalds 	cdev_unmap(p->dev, p->count);
6041da177e4SLinus Torvalds 	kobject_put(&p->kobj);
6051da177e4SLinus Torvalds }
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds 
cdev_default_release(struct kobject * kobj)6081da177e4SLinus Torvalds static void cdev_default_release(struct kobject *kobj)
6091da177e4SLinus Torvalds {
6101da177e4SLinus Torvalds 	struct cdev *p = container_of(kobj, struct cdev, kobj);
6112f0157f1SDmitry Torokhov 	struct kobject *parent = kobj->parent;
6122f0157f1SDmitry Torokhov 
6131da177e4SLinus Torvalds 	cdev_purge(p);
6142f0157f1SDmitry Torokhov 	kobject_put(parent);
6151da177e4SLinus Torvalds }
6161da177e4SLinus Torvalds 
cdev_dynamic_release(struct kobject * kobj)6171da177e4SLinus Torvalds static void cdev_dynamic_release(struct kobject *kobj)
6181da177e4SLinus Torvalds {
6191da177e4SLinus Torvalds 	struct cdev *p = container_of(kobj, struct cdev, kobj);
6202f0157f1SDmitry Torokhov 	struct kobject *parent = kobj->parent;
6212f0157f1SDmitry Torokhov 
6221da177e4SLinus Torvalds 	cdev_purge(p);
6231da177e4SLinus Torvalds 	kfree(p);
6242f0157f1SDmitry Torokhov 	kobject_put(parent);
6251da177e4SLinus Torvalds }
6261da177e4SLinus Torvalds 
6271da177e4SLinus Torvalds static struct kobj_type ktype_cdev_default = {
6281da177e4SLinus Torvalds 	.release	= cdev_default_release,
6291da177e4SLinus Torvalds };
6301da177e4SLinus Torvalds 
6311da177e4SLinus Torvalds static struct kobj_type ktype_cdev_dynamic = {
6321da177e4SLinus Torvalds 	.release	= cdev_dynamic_release,
6331da177e4SLinus Torvalds };
6341da177e4SLinus Torvalds 
635cf3e43dbSJonathan Corbet /**
636cf3e43dbSJonathan Corbet  * cdev_alloc() - allocate a cdev structure
637cf3e43dbSJonathan Corbet  *
638cf3e43dbSJonathan Corbet  * Allocates and returns a cdev structure, or NULL on failure.
639cf3e43dbSJonathan Corbet  */
cdev_alloc(void)6401da177e4SLinus Torvalds struct cdev *cdev_alloc(void)
6411da177e4SLinus Torvalds {
64211b0b5abSOliver Neukum 	struct cdev *p = kzalloc(sizeof(struct cdev), GFP_KERNEL);
6431da177e4SLinus Torvalds 	if (p) {
6441da177e4SLinus Torvalds 		INIT_LIST_HEAD(&p->list);
645f9cb074bSGreg Kroah-Hartman 		kobject_init(&p->kobj, &ktype_cdev_dynamic);
6461da177e4SLinus Torvalds 	}
6471da177e4SLinus Torvalds 	return p;
6481da177e4SLinus Torvalds }
6491da177e4SLinus Torvalds 
650cf3e43dbSJonathan Corbet /**
651cf3e43dbSJonathan Corbet  * cdev_init() - initialize a cdev structure
652cf3e43dbSJonathan Corbet  * @cdev: the structure to initialize
653cf3e43dbSJonathan Corbet  * @fops: the file_operations for this device
654cf3e43dbSJonathan Corbet  *
655cf3e43dbSJonathan Corbet  * Initializes @cdev, remembering @fops, making it ready to add to the
656cf3e43dbSJonathan Corbet  * system with cdev_add().
657cf3e43dbSJonathan Corbet  */
cdev_init(struct cdev * cdev,const struct file_operations * fops)65899ac48f5SArjan van de Ven void cdev_init(struct cdev *cdev, const struct file_operations *fops)
6591da177e4SLinus Torvalds {
6601da177e4SLinus Torvalds 	memset(cdev, 0, sizeof *cdev);
6611da177e4SLinus Torvalds 	INIT_LIST_HEAD(&cdev->list);
662f9cb074bSGreg Kroah-Hartman 	kobject_init(&cdev->kobj, &ktype_cdev_default);
6631da177e4SLinus Torvalds 	cdev->ops = fops;
6641da177e4SLinus Torvalds }
6651da177e4SLinus Torvalds 
base_probe(dev_t dev,int * part,void * data)6661da177e4SLinus Torvalds static struct kobject *base_probe(dev_t dev, int *part, void *data)
6671da177e4SLinus Torvalds {
6681da177e4SLinus Torvalds 	if (request_module("char-major-%d-%d", MAJOR(dev), MINOR(dev)) > 0)
6691da177e4SLinus Torvalds 		/* Make old-style 2.4 aliases work */
6701da177e4SLinus Torvalds 		request_module("char-major-%d", MAJOR(dev));
6711da177e4SLinus Torvalds 	return NULL;
6721da177e4SLinus Torvalds }
6731da177e4SLinus Torvalds 
chrdev_init(void)6741da177e4SLinus Torvalds void __init chrdev_init(void)
6751da177e4SLinus Torvalds {
6761da177e4SLinus Torvalds 	cdev_map = kobj_map_init(base_probe, &chrdevs_lock);
6771da177e4SLinus Torvalds }
6781da177e4SLinus Torvalds 
6791da177e4SLinus Torvalds 
6801da177e4SLinus Torvalds /* Let modules do char dev stuff */
6811da177e4SLinus Torvalds EXPORT_SYMBOL(register_chrdev_region);
6821da177e4SLinus Torvalds EXPORT_SYMBOL(unregister_chrdev_region);
6831da177e4SLinus Torvalds EXPORT_SYMBOL(alloc_chrdev_region);
6841da177e4SLinus Torvalds EXPORT_SYMBOL(cdev_init);
6851da177e4SLinus Torvalds EXPORT_SYMBOL(cdev_alloc);
6861da177e4SLinus Torvalds EXPORT_SYMBOL(cdev_del);
6871da177e4SLinus Torvalds EXPORT_SYMBOL(cdev_add);
688233ed09dSLogan Gunthorpe EXPORT_SYMBOL(cdev_set_parent);
689233ed09dSLogan Gunthorpe EXPORT_SYMBOL(cdev_device_add);
690233ed09dSLogan Gunthorpe EXPORT_SYMBOL(cdev_device_del);
6911905b1bfSTejun Heo EXPORT_SYMBOL(__register_chrdev);
6921905b1bfSTejun Heo EXPORT_SYMBOL(__unregister_chrdev);
693