18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  linux/fs/char_dev.c
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *  Copyright (C) 1991, 1992  Linus Torvalds
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/init.h>
98c2ecf20Sopenharmony_ci#include <linux/fs.h>
108c2ecf20Sopenharmony_ci#include <linux/kdev_t.h>
118c2ecf20Sopenharmony_ci#include <linux/slab.h>
128c2ecf20Sopenharmony_ci#include <linux/string.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include <linux/major.h>
158c2ecf20Sopenharmony_ci#include <linux/errno.h>
168c2ecf20Sopenharmony_ci#include <linux/module.h>
178c2ecf20Sopenharmony_ci#include <linux/seq_file.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#include <linux/kobject.h>
208c2ecf20Sopenharmony_ci#include <linux/kobj_map.h>
218c2ecf20Sopenharmony_ci#include <linux/cdev.h>
228c2ecf20Sopenharmony_ci#include <linux/mutex.h>
238c2ecf20Sopenharmony_ci#include <linux/backing-dev.h>
248c2ecf20Sopenharmony_ci#include <linux/tty.h>
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#include "internal.h"
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_cistatic struct kobj_map *cdev_map;
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_cistatic DEFINE_MUTEX(chrdevs_lock);
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci#define CHRDEV_MAJOR_HASH_SIZE 255
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_cistatic struct char_device_struct {
358c2ecf20Sopenharmony_ci	struct char_device_struct *next;
368c2ecf20Sopenharmony_ci	unsigned int major;
378c2ecf20Sopenharmony_ci	unsigned int baseminor;
388c2ecf20Sopenharmony_ci	int minorct;
398c2ecf20Sopenharmony_ci	char name[64];
408c2ecf20Sopenharmony_ci	struct cdev *cdev;		/* will die */
418c2ecf20Sopenharmony_ci} *chrdevs[CHRDEV_MAJOR_HASH_SIZE];
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci/* index in the above */
448c2ecf20Sopenharmony_cistatic inline int major_to_index(unsigned major)
458c2ecf20Sopenharmony_ci{
468c2ecf20Sopenharmony_ci	return major % CHRDEV_MAJOR_HASH_SIZE;
478c2ecf20Sopenharmony_ci}
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci#ifdef CONFIG_PROC_FS
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_civoid chrdev_show(struct seq_file *f, off_t offset)
528c2ecf20Sopenharmony_ci{
538c2ecf20Sopenharmony_ci	struct char_device_struct *cd;
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	mutex_lock(&chrdevs_lock);
568c2ecf20Sopenharmony_ci	for (cd = chrdevs[major_to_index(offset)]; cd; cd = cd->next) {
578c2ecf20Sopenharmony_ci		if (cd->major == offset)
588c2ecf20Sopenharmony_ci			seq_printf(f, "%3d %s\n", cd->major, cd->name);
598c2ecf20Sopenharmony_ci	}
608c2ecf20Sopenharmony_ci	mutex_unlock(&chrdevs_lock);
618c2ecf20Sopenharmony_ci}
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci#endif /* CONFIG_PROC_FS */
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_cistatic int find_dynamic_major(void)
668c2ecf20Sopenharmony_ci{
678c2ecf20Sopenharmony_ci	int i;
688c2ecf20Sopenharmony_ci	struct char_device_struct *cd;
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	for (i = ARRAY_SIZE(chrdevs)-1; i >= CHRDEV_MAJOR_DYN_END; i--) {
718c2ecf20Sopenharmony_ci		if (chrdevs[i] == NULL)
728c2ecf20Sopenharmony_ci			return i;
738c2ecf20Sopenharmony_ci	}
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	for (i = CHRDEV_MAJOR_DYN_EXT_START;
768c2ecf20Sopenharmony_ci	     i >= CHRDEV_MAJOR_DYN_EXT_END; i--) {
778c2ecf20Sopenharmony_ci		for (cd = chrdevs[major_to_index(i)]; cd; cd = cd->next)
788c2ecf20Sopenharmony_ci			if (cd->major == i)
798c2ecf20Sopenharmony_ci				break;
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci		if (cd == NULL)
828c2ecf20Sopenharmony_ci			return i;
838c2ecf20Sopenharmony_ci	}
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	return -EBUSY;
868c2ecf20Sopenharmony_ci}
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci/*
898c2ecf20Sopenharmony_ci * Register a single major with a specified minor range.
908c2ecf20Sopenharmony_ci *
918c2ecf20Sopenharmony_ci * If major == 0 this function will dynamically allocate an unused major.
928c2ecf20Sopenharmony_ci * If major > 0 this function will attempt to reserve the range of minors
938c2ecf20Sopenharmony_ci * with given major.
948c2ecf20Sopenharmony_ci *
958c2ecf20Sopenharmony_ci */
968c2ecf20Sopenharmony_cistatic struct char_device_struct *
978c2ecf20Sopenharmony_ci__register_chrdev_region(unsigned int major, unsigned int baseminor,
988c2ecf20Sopenharmony_ci			   int minorct, const char *name)
998c2ecf20Sopenharmony_ci{
1008c2ecf20Sopenharmony_ci	struct char_device_struct *cd, *curr, *prev = NULL;
1018c2ecf20Sopenharmony_ci	int ret;
1028c2ecf20Sopenharmony_ci	int i;
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	if (major >= CHRDEV_MAJOR_MAX) {
1058c2ecf20Sopenharmony_ci		pr_err("CHRDEV \"%s\" major requested (%u) is greater than the maximum (%u)\n",
1068c2ecf20Sopenharmony_ci		       name, major, CHRDEV_MAJOR_MAX-1);
1078c2ecf20Sopenharmony_ci		return ERR_PTR(-EINVAL);
1088c2ecf20Sopenharmony_ci	}
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	if (minorct > MINORMASK + 1 - baseminor) {
1118c2ecf20Sopenharmony_ci		pr_err("CHRDEV \"%s\" minor range requested (%u-%u) is out of range of maximum range (%u-%u) for a single major\n",
1128c2ecf20Sopenharmony_ci			name, baseminor, baseminor + minorct - 1, 0, MINORMASK);
1138c2ecf20Sopenharmony_ci		return ERR_PTR(-EINVAL);
1148c2ecf20Sopenharmony_ci	}
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	cd = kzalloc(sizeof(struct char_device_struct), GFP_KERNEL);
1178c2ecf20Sopenharmony_ci	if (cd == NULL)
1188c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	mutex_lock(&chrdevs_lock);
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	if (major == 0) {
1238c2ecf20Sopenharmony_ci		ret = find_dynamic_major();
1248c2ecf20Sopenharmony_ci		if (ret < 0) {
1258c2ecf20Sopenharmony_ci			pr_err("CHRDEV \"%s\" dynamic allocation region is full\n",
1268c2ecf20Sopenharmony_ci			       name);
1278c2ecf20Sopenharmony_ci			goto out;
1288c2ecf20Sopenharmony_ci		}
1298c2ecf20Sopenharmony_ci		major = ret;
1308c2ecf20Sopenharmony_ci	}
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	ret = -EBUSY;
1338c2ecf20Sopenharmony_ci	i = major_to_index(major);
1348c2ecf20Sopenharmony_ci	for (curr = chrdevs[i]; curr; prev = curr, curr = curr->next) {
1358c2ecf20Sopenharmony_ci		if (curr->major < major)
1368c2ecf20Sopenharmony_ci			continue;
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci		if (curr->major > major)
1398c2ecf20Sopenharmony_ci			break;
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci		if (curr->baseminor + curr->minorct <= baseminor)
1428c2ecf20Sopenharmony_ci			continue;
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci		if (curr->baseminor >= baseminor + minorct)
1458c2ecf20Sopenharmony_ci			break;
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci		goto out;
1488c2ecf20Sopenharmony_ci	}
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	cd->major = major;
1518c2ecf20Sopenharmony_ci	cd->baseminor = baseminor;
1528c2ecf20Sopenharmony_ci	cd->minorct = minorct;
1538c2ecf20Sopenharmony_ci	strlcpy(cd->name, name, sizeof(cd->name));
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	if (!prev) {
1568c2ecf20Sopenharmony_ci		cd->next = curr;
1578c2ecf20Sopenharmony_ci		chrdevs[i] = cd;
1588c2ecf20Sopenharmony_ci	} else {
1598c2ecf20Sopenharmony_ci		cd->next = prev->next;
1608c2ecf20Sopenharmony_ci		prev->next = cd;
1618c2ecf20Sopenharmony_ci	}
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	mutex_unlock(&chrdevs_lock);
1648c2ecf20Sopenharmony_ci	return cd;
1658c2ecf20Sopenharmony_ciout:
1668c2ecf20Sopenharmony_ci	mutex_unlock(&chrdevs_lock);
1678c2ecf20Sopenharmony_ci	kfree(cd);
1688c2ecf20Sopenharmony_ci	return ERR_PTR(ret);
1698c2ecf20Sopenharmony_ci}
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_cistatic struct char_device_struct *
1728c2ecf20Sopenharmony_ci__unregister_chrdev_region(unsigned major, unsigned baseminor, int minorct)
1738c2ecf20Sopenharmony_ci{
1748c2ecf20Sopenharmony_ci	struct char_device_struct *cd = NULL, **cp;
1758c2ecf20Sopenharmony_ci	int i = major_to_index(major);
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	mutex_lock(&chrdevs_lock);
1788c2ecf20Sopenharmony_ci	for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next)
1798c2ecf20Sopenharmony_ci		if ((*cp)->major == major &&
1808c2ecf20Sopenharmony_ci		    (*cp)->baseminor == baseminor &&
1818c2ecf20Sopenharmony_ci		    (*cp)->minorct == minorct)
1828c2ecf20Sopenharmony_ci			break;
1838c2ecf20Sopenharmony_ci	if (*cp) {
1848c2ecf20Sopenharmony_ci		cd = *cp;
1858c2ecf20Sopenharmony_ci		*cp = cd->next;
1868c2ecf20Sopenharmony_ci	}
1878c2ecf20Sopenharmony_ci	mutex_unlock(&chrdevs_lock);
1888c2ecf20Sopenharmony_ci	return cd;
1898c2ecf20Sopenharmony_ci}
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci/**
1928c2ecf20Sopenharmony_ci * register_chrdev_region() - register a range of device numbers
1938c2ecf20Sopenharmony_ci * @from: the first in the desired range of device numbers; must include
1948c2ecf20Sopenharmony_ci *        the major number.
1958c2ecf20Sopenharmony_ci * @count: the number of consecutive device numbers required
1968c2ecf20Sopenharmony_ci * @name: the name of the device or driver.
1978c2ecf20Sopenharmony_ci *
1988c2ecf20Sopenharmony_ci * Return value is zero on success, a negative error code on failure.
1998c2ecf20Sopenharmony_ci */
2008c2ecf20Sopenharmony_ciint register_chrdev_region(dev_t from, unsigned count, const char *name)
2018c2ecf20Sopenharmony_ci{
2028c2ecf20Sopenharmony_ci	struct char_device_struct *cd;
2038c2ecf20Sopenharmony_ci	dev_t to = from + count;
2048c2ecf20Sopenharmony_ci	dev_t n, next;
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	for (n = from; n < to; n = next) {
2078c2ecf20Sopenharmony_ci		next = MKDEV(MAJOR(n)+1, 0);
2088c2ecf20Sopenharmony_ci		if (next > to)
2098c2ecf20Sopenharmony_ci			next = to;
2108c2ecf20Sopenharmony_ci		cd = __register_chrdev_region(MAJOR(n), MINOR(n),
2118c2ecf20Sopenharmony_ci			       next - n, name);
2128c2ecf20Sopenharmony_ci		if (IS_ERR(cd))
2138c2ecf20Sopenharmony_ci			goto fail;
2148c2ecf20Sopenharmony_ci	}
2158c2ecf20Sopenharmony_ci	return 0;
2168c2ecf20Sopenharmony_cifail:
2178c2ecf20Sopenharmony_ci	to = n;
2188c2ecf20Sopenharmony_ci	for (n = from; n < to; n = next) {
2198c2ecf20Sopenharmony_ci		next = MKDEV(MAJOR(n)+1, 0);
2208c2ecf20Sopenharmony_ci		kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
2218c2ecf20Sopenharmony_ci	}
2228c2ecf20Sopenharmony_ci	return PTR_ERR(cd);
2238c2ecf20Sopenharmony_ci}
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci/**
2268c2ecf20Sopenharmony_ci * alloc_chrdev_region() - register a range of char device numbers
2278c2ecf20Sopenharmony_ci * @dev: output parameter for first assigned number
2288c2ecf20Sopenharmony_ci * @baseminor: first of the requested range of minor numbers
2298c2ecf20Sopenharmony_ci * @count: the number of minor numbers required
2308c2ecf20Sopenharmony_ci * @name: the name of the associated device or driver
2318c2ecf20Sopenharmony_ci *
2328c2ecf20Sopenharmony_ci * Allocates a range of char device numbers.  The major number will be
2338c2ecf20Sopenharmony_ci * chosen dynamically, and returned (along with the first minor number)
2348c2ecf20Sopenharmony_ci * in @dev.  Returns zero or a negative error code.
2358c2ecf20Sopenharmony_ci */
2368c2ecf20Sopenharmony_ciint alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count,
2378c2ecf20Sopenharmony_ci			const char *name)
2388c2ecf20Sopenharmony_ci{
2398c2ecf20Sopenharmony_ci	struct char_device_struct *cd;
2408c2ecf20Sopenharmony_ci	cd = __register_chrdev_region(0, baseminor, count, name);
2418c2ecf20Sopenharmony_ci	if (IS_ERR(cd))
2428c2ecf20Sopenharmony_ci		return PTR_ERR(cd);
2438c2ecf20Sopenharmony_ci	*dev = MKDEV(cd->major, cd->baseminor);
2448c2ecf20Sopenharmony_ci	return 0;
2458c2ecf20Sopenharmony_ci}
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci/**
2488c2ecf20Sopenharmony_ci * __register_chrdev() - create and register a cdev occupying a range of minors
2498c2ecf20Sopenharmony_ci * @major: major device number or 0 for dynamic allocation
2508c2ecf20Sopenharmony_ci * @baseminor: first of the requested range of minor numbers
2518c2ecf20Sopenharmony_ci * @count: the number of minor numbers required
2528c2ecf20Sopenharmony_ci * @name: name of this range of devices
2538c2ecf20Sopenharmony_ci * @fops: file operations associated with this devices
2548c2ecf20Sopenharmony_ci *
2558c2ecf20Sopenharmony_ci * If @major == 0 this functions will dynamically allocate a major and return
2568c2ecf20Sopenharmony_ci * its number.
2578c2ecf20Sopenharmony_ci *
2588c2ecf20Sopenharmony_ci * If @major > 0 this function will attempt to reserve a device with the given
2598c2ecf20Sopenharmony_ci * major number and will return zero on success.
2608c2ecf20Sopenharmony_ci *
2618c2ecf20Sopenharmony_ci * Returns a -ve errno on failure.
2628c2ecf20Sopenharmony_ci *
2638c2ecf20Sopenharmony_ci * The name of this device has nothing to do with the name of the device in
2648c2ecf20Sopenharmony_ci * /dev. It only helps to keep track of the different owners of devices. If
2658c2ecf20Sopenharmony_ci * your module name has only one type of devices it's ok to use e.g. the name
2668c2ecf20Sopenharmony_ci * of the module here.
2678c2ecf20Sopenharmony_ci */
2688c2ecf20Sopenharmony_ciint __register_chrdev(unsigned int major, unsigned int baseminor,
2698c2ecf20Sopenharmony_ci		      unsigned int count, const char *name,
2708c2ecf20Sopenharmony_ci		      const struct file_operations *fops)
2718c2ecf20Sopenharmony_ci{
2728c2ecf20Sopenharmony_ci	struct char_device_struct *cd;
2738c2ecf20Sopenharmony_ci	struct cdev *cdev;
2748c2ecf20Sopenharmony_ci	int err = -ENOMEM;
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	cd = __register_chrdev_region(major, baseminor, count, name);
2778c2ecf20Sopenharmony_ci	if (IS_ERR(cd))
2788c2ecf20Sopenharmony_ci		return PTR_ERR(cd);
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci	cdev = cdev_alloc();
2818c2ecf20Sopenharmony_ci	if (!cdev)
2828c2ecf20Sopenharmony_ci		goto out2;
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci	cdev->owner = fops->owner;
2858c2ecf20Sopenharmony_ci	cdev->ops = fops;
2868c2ecf20Sopenharmony_ci	kobject_set_name(&cdev->kobj, "%s", name);
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci	err = cdev_add(cdev, MKDEV(cd->major, baseminor), count);
2898c2ecf20Sopenharmony_ci	if (err)
2908c2ecf20Sopenharmony_ci		goto out;
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	cd->cdev = cdev;
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	return major ? 0 : cd->major;
2958c2ecf20Sopenharmony_ciout:
2968c2ecf20Sopenharmony_ci	kobject_put(&cdev->kobj);
2978c2ecf20Sopenharmony_ciout2:
2988c2ecf20Sopenharmony_ci	kfree(__unregister_chrdev_region(cd->major, baseminor, count));
2998c2ecf20Sopenharmony_ci	return err;
3008c2ecf20Sopenharmony_ci}
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci/**
3038c2ecf20Sopenharmony_ci * unregister_chrdev_region() - unregister a range of device numbers
3048c2ecf20Sopenharmony_ci * @from: the first in the range of numbers to unregister
3058c2ecf20Sopenharmony_ci * @count: the number of device numbers to unregister
3068c2ecf20Sopenharmony_ci *
3078c2ecf20Sopenharmony_ci * This function will unregister a range of @count device numbers,
3088c2ecf20Sopenharmony_ci * starting with @from.  The caller should normally be the one who
3098c2ecf20Sopenharmony_ci * allocated those numbers in the first place...
3108c2ecf20Sopenharmony_ci */
3118c2ecf20Sopenharmony_civoid unregister_chrdev_region(dev_t from, unsigned count)
3128c2ecf20Sopenharmony_ci{
3138c2ecf20Sopenharmony_ci	dev_t to = from + count;
3148c2ecf20Sopenharmony_ci	dev_t n, next;
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci	for (n = from; n < to; n = next) {
3178c2ecf20Sopenharmony_ci		next = MKDEV(MAJOR(n)+1, 0);
3188c2ecf20Sopenharmony_ci		if (next > to)
3198c2ecf20Sopenharmony_ci			next = to;
3208c2ecf20Sopenharmony_ci		kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
3218c2ecf20Sopenharmony_ci	}
3228c2ecf20Sopenharmony_ci}
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci/**
3258c2ecf20Sopenharmony_ci * __unregister_chrdev - unregister and destroy a cdev
3268c2ecf20Sopenharmony_ci * @major: major device number
3278c2ecf20Sopenharmony_ci * @baseminor: first of the range of minor numbers
3288c2ecf20Sopenharmony_ci * @count: the number of minor numbers this cdev is occupying
3298c2ecf20Sopenharmony_ci * @name: name of this range of devices
3308c2ecf20Sopenharmony_ci *
3318c2ecf20Sopenharmony_ci * Unregister and destroy the cdev occupying the region described by
3328c2ecf20Sopenharmony_ci * @major, @baseminor and @count.  This function undoes what
3338c2ecf20Sopenharmony_ci * __register_chrdev() did.
3348c2ecf20Sopenharmony_ci */
3358c2ecf20Sopenharmony_civoid __unregister_chrdev(unsigned int major, unsigned int baseminor,
3368c2ecf20Sopenharmony_ci			 unsigned int count, const char *name)
3378c2ecf20Sopenharmony_ci{
3388c2ecf20Sopenharmony_ci	struct char_device_struct *cd;
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	cd = __unregister_chrdev_region(major, baseminor, count);
3418c2ecf20Sopenharmony_ci	if (cd && cd->cdev)
3428c2ecf20Sopenharmony_ci		cdev_del(cd->cdev);
3438c2ecf20Sopenharmony_ci	kfree(cd);
3448c2ecf20Sopenharmony_ci}
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_cistatic DEFINE_SPINLOCK(cdev_lock);
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_cistatic struct kobject *cdev_get(struct cdev *p)
3498c2ecf20Sopenharmony_ci{
3508c2ecf20Sopenharmony_ci	struct module *owner = p->owner;
3518c2ecf20Sopenharmony_ci	struct kobject *kobj;
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	if (owner && !try_module_get(owner))
3548c2ecf20Sopenharmony_ci		return NULL;
3558c2ecf20Sopenharmony_ci	kobj = kobject_get_unless_zero(&p->kobj);
3568c2ecf20Sopenharmony_ci	if (!kobj)
3578c2ecf20Sopenharmony_ci		module_put(owner);
3588c2ecf20Sopenharmony_ci	return kobj;
3598c2ecf20Sopenharmony_ci}
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_civoid cdev_put(struct cdev *p)
3628c2ecf20Sopenharmony_ci{
3638c2ecf20Sopenharmony_ci	if (p) {
3648c2ecf20Sopenharmony_ci		struct module *owner = p->owner;
3658c2ecf20Sopenharmony_ci		kobject_put(&p->kobj);
3668c2ecf20Sopenharmony_ci		module_put(owner);
3678c2ecf20Sopenharmony_ci	}
3688c2ecf20Sopenharmony_ci}
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci/*
3718c2ecf20Sopenharmony_ci * Called every time a character special file is opened
3728c2ecf20Sopenharmony_ci */
3738c2ecf20Sopenharmony_cistatic int chrdev_open(struct inode *inode, struct file *filp)
3748c2ecf20Sopenharmony_ci{
3758c2ecf20Sopenharmony_ci	const struct file_operations *fops;
3768c2ecf20Sopenharmony_ci	struct cdev *p;
3778c2ecf20Sopenharmony_ci	struct cdev *new = NULL;
3788c2ecf20Sopenharmony_ci	int ret = 0;
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci	spin_lock(&cdev_lock);
3818c2ecf20Sopenharmony_ci	p = inode->i_cdev;
3828c2ecf20Sopenharmony_ci	if (!p) {
3838c2ecf20Sopenharmony_ci		struct kobject *kobj;
3848c2ecf20Sopenharmony_ci		int idx;
3858c2ecf20Sopenharmony_ci		spin_unlock(&cdev_lock);
3868c2ecf20Sopenharmony_ci		kobj = kobj_lookup(cdev_map, inode->i_rdev, &idx);
3878c2ecf20Sopenharmony_ci		if (!kobj)
3888c2ecf20Sopenharmony_ci			return -ENXIO;
3898c2ecf20Sopenharmony_ci		new = container_of(kobj, struct cdev, kobj);
3908c2ecf20Sopenharmony_ci		spin_lock(&cdev_lock);
3918c2ecf20Sopenharmony_ci		/* Check i_cdev again in case somebody beat us to it while
3928c2ecf20Sopenharmony_ci		   we dropped the lock. */
3938c2ecf20Sopenharmony_ci		p = inode->i_cdev;
3948c2ecf20Sopenharmony_ci		if (!p) {
3958c2ecf20Sopenharmony_ci			inode->i_cdev = p = new;
3968c2ecf20Sopenharmony_ci			list_add(&inode->i_devices, &p->list);
3978c2ecf20Sopenharmony_ci			new = NULL;
3988c2ecf20Sopenharmony_ci		} else if (!cdev_get(p))
3998c2ecf20Sopenharmony_ci			ret = -ENXIO;
4008c2ecf20Sopenharmony_ci	} else if (!cdev_get(p))
4018c2ecf20Sopenharmony_ci		ret = -ENXIO;
4028c2ecf20Sopenharmony_ci	spin_unlock(&cdev_lock);
4038c2ecf20Sopenharmony_ci	cdev_put(new);
4048c2ecf20Sopenharmony_ci	if (ret)
4058c2ecf20Sopenharmony_ci		return ret;
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_ci	ret = -ENXIO;
4088c2ecf20Sopenharmony_ci	fops = fops_get(p->ops);
4098c2ecf20Sopenharmony_ci	if (!fops)
4108c2ecf20Sopenharmony_ci		goto out_cdev_put;
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci	replace_fops(filp, fops);
4138c2ecf20Sopenharmony_ci	if (filp->f_op->open) {
4148c2ecf20Sopenharmony_ci		ret = filp->f_op->open(inode, filp);
4158c2ecf20Sopenharmony_ci		if (ret)
4168c2ecf20Sopenharmony_ci			goto out_cdev_put;
4178c2ecf20Sopenharmony_ci	}
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci	return 0;
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci out_cdev_put:
4228c2ecf20Sopenharmony_ci	cdev_put(p);
4238c2ecf20Sopenharmony_ci	return ret;
4248c2ecf20Sopenharmony_ci}
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_civoid cd_forget(struct inode *inode)
4278c2ecf20Sopenharmony_ci{
4288c2ecf20Sopenharmony_ci	spin_lock(&cdev_lock);
4298c2ecf20Sopenharmony_ci	list_del_init(&inode->i_devices);
4308c2ecf20Sopenharmony_ci	inode->i_cdev = NULL;
4318c2ecf20Sopenharmony_ci	inode->i_mapping = &inode->i_data;
4328c2ecf20Sopenharmony_ci	spin_unlock(&cdev_lock);
4338c2ecf20Sopenharmony_ci}
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_cistatic void cdev_purge(struct cdev *cdev)
4368c2ecf20Sopenharmony_ci{
4378c2ecf20Sopenharmony_ci	spin_lock(&cdev_lock);
4388c2ecf20Sopenharmony_ci	while (!list_empty(&cdev->list)) {
4398c2ecf20Sopenharmony_ci		struct inode *inode;
4408c2ecf20Sopenharmony_ci		inode = container_of(cdev->list.next, struct inode, i_devices);
4418c2ecf20Sopenharmony_ci		list_del_init(&inode->i_devices);
4428c2ecf20Sopenharmony_ci		inode->i_cdev = NULL;
4438c2ecf20Sopenharmony_ci	}
4448c2ecf20Sopenharmony_ci	spin_unlock(&cdev_lock);
4458c2ecf20Sopenharmony_ci}
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_ci/*
4488c2ecf20Sopenharmony_ci * Dummy default file-operations: the only thing this does
4498c2ecf20Sopenharmony_ci * is contain the open that then fills in the correct operations
4508c2ecf20Sopenharmony_ci * depending on the special file...
4518c2ecf20Sopenharmony_ci */
4528c2ecf20Sopenharmony_ciconst struct file_operations def_chr_fops = {
4538c2ecf20Sopenharmony_ci	.open = chrdev_open,
4548c2ecf20Sopenharmony_ci	.llseek = noop_llseek,
4558c2ecf20Sopenharmony_ci};
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_cistatic struct kobject *exact_match(dev_t dev, int *part, void *data)
4588c2ecf20Sopenharmony_ci{
4598c2ecf20Sopenharmony_ci	struct cdev *p = data;
4608c2ecf20Sopenharmony_ci	return &p->kobj;
4618c2ecf20Sopenharmony_ci}
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_cistatic int exact_lock(dev_t dev, void *data)
4648c2ecf20Sopenharmony_ci{
4658c2ecf20Sopenharmony_ci	struct cdev *p = data;
4668c2ecf20Sopenharmony_ci	return cdev_get(p) ? 0 : -1;
4678c2ecf20Sopenharmony_ci}
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci/**
4708c2ecf20Sopenharmony_ci * cdev_add() - add a char device to the system
4718c2ecf20Sopenharmony_ci * @p: the cdev structure for the device
4728c2ecf20Sopenharmony_ci * @dev: the first device number for which this device is responsible
4738c2ecf20Sopenharmony_ci * @count: the number of consecutive minor numbers corresponding to this
4748c2ecf20Sopenharmony_ci *         device
4758c2ecf20Sopenharmony_ci *
4768c2ecf20Sopenharmony_ci * cdev_add() adds the device represented by @p to the system, making it
4778c2ecf20Sopenharmony_ci * live immediately.  A negative error code is returned on failure.
4788c2ecf20Sopenharmony_ci */
4798c2ecf20Sopenharmony_ciint cdev_add(struct cdev *p, dev_t dev, unsigned count)
4808c2ecf20Sopenharmony_ci{
4818c2ecf20Sopenharmony_ci	int error;
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci	p->dev = dev;
4848c2ecf20Sopenharmony_ci	p->count = count;
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_ci	if (WARN_ON(dev == WHITEOUT_DEV))
4878c2ecf20Sopenharmony_ci		return -EBUSY;
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci	error = kobj_map(cdev_map, dev, count, NULL,
4908c2ecf20Sopenharmony_ci			 exact_match, exact_lock, p);
4918c2ecf20Sopenharmony_ci	if (error)
4928c2ecf20Sopenharmony_ci		return error;
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_ci	kobject_get(p->kobj.parent);
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_ci	return 0;
4978c2ecf20Sopenharmony_ci}
4988c2ecf20Sopenharmony_ci
4998c2ecf20Sopenharmony_ci/**
5008c2ecf20Sopenharmony_ci * cdev_set_parent() - set the parent kobject for a char device
5018c2ecf20Sopenharmony_ci * @p: the cdev structure
5028c2ecf20Sopenharmony_ci * @kobj: the kobject to take a reference to
5038c2ecf20Sopenharmony_ci *
5048c2ecf20Sopenharmony_ci * cdev_set_parent() sets a parent kobject which will be referenced
5058c2ecf20Sopenharmony_ci * appropriately so the parent is not freed before the cdev. This
5068c2ecf20Sopenharmony_ci * should be called before cdev_add.
5078c2ecf20Sopenharmony_ci */
5088c2ecf20Sopenharmony_civoid cdev_set_parent(struct cdev *p, struct kobject *kobj)
5098c2ecf20Sopenharmony_ci{
5108c2ecf20Sopenharmony_ci	WARN_ON(!kobj->state_initialized);
5118c2ecf20Sopenharmony_ci	p->kobj.parent = kobj;
5128c2ecf20Sopenharmony_ci}
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ci/**
5158c2ecf20Sopenharmony_ci * cdev_device_add() - add a char device and it's corresponding
5168c2ecf20Sopenharmony_ci *	struct device, linkink
5178c2ecf20Sopenharmony_ci * @dev: the device structure
5188c2ecf20Sopenharmony_ci * @cdev: the cdev structure
5198c2ecf20Sopenharmony_ci *
5208c2ecf20Sopenharmony_ci * cdev_device_add() adds the char device represented by @cdev to the system,
5218c2ecf20Sopenharmony_ci * just as cdev_add does. It then adds @dev to the system using device_add
5228c2ecf20Sopenharmony_ci * The dev_t for the char device will be taken from the struct device which
5238c2ecf20Sopenharmony_ci * needs to be initialized first. This helper function correctly takes a
5248c2ecf20Sopenharmony_ci * reference to the parent device so the parent will not get released until
5258c2ecf20Sopenharmony_ci * all references to the cdev are released.
5268c2ecf20Sopenharmony_ci *
5278c2ecf20Sopenharmony_ci * This helper uses dev->devt for the device number. If it is not set
5288c2ecf20Sopenharmony_ci * it will not add the cdev and it will be equivalent to device_add.
5298c2ecf20Sopenharmony_ci *
5308c2ecf20Sopenharmony_ci * This function should be used whenever the struct cdev and the
5318c2ecf20Sopenharmony_ci * struct device are members of the same structure whose lifetime is
5328c2ecf20Sopenharmony_ci * managed by the struct device.
5338c2ecf20Sopenharmony_ci *
5348c2ecf20Sopenharmony_ci * NOTE: Callers must assume that userspace was able to open the cdev and
5358c2ecf20Sopenharmony_ci * can call cdev fops callbacks at any time, even if this function fails.
5368c2ecf20Sopenharmony_ci */
5378c2ecf20Sopenharmony_ciint cdev_device_add(struct cdev *cdev, struct device *dev)
5388c2ecf20Sopenharmony_ci{
5398c2ecf20Sopenharmony_ci	int rc = 0;
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_ci	if (dev->devt) {
5428c2ecf20Sopenharmony_ci		cdev_set_parent(cdev, &dev->kobj);
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_ci		rc = cdev_add(cdev, dev->devt, 1);
5458c2ecf20Sopenharmony_ci		if (rc)
5468c2ecf20Sopenharmony_ci			return rc;
5478c2ecf20Sopenharmony_ci	}
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_ci	rc = device_add(dev);
5508c2ecf20Sopenharmony_ci	if (rc && dev->devt)
5518c2ecf20Sopenharmony_ci		cdev_del(cdev);
5528c2ecf20Sopenharmony_ci
5538c2ecf20Sopenharmony_ci	return rc;
5548c2ecf20Sopenharmony_ci}
5558c2ecf20Sopenharmony_ci
5568c2ecf20Sopenharmony_ci/**
5578c2ecf20Sopenharmony_ci * cdev_device_del() - inverse of cdev_device_add
5588c2ecf20Sopenharmony_ci * @dev: the device structure
5598c2ecf20Sopenharmony_ci * @cdev: the cdev structure
5608c2ecf20Sopenharmony_ci *
5618c2ecf20Sopenharmony_ci * cdev_device_del() is a helper function to call cdev_del and device_del.
5628c2ecf20Sopenharmony_ci * It should be used whenever cdev_device_add is used.
5638c2ecf20Sopenharmony_ci *
5648c2ecf20Sopenharmony_ci * If dev->devt is not set it will not remove the cdev and will be equivalent
5658c2ecf20Sopenharmony_ci * to device_del.
5668c2ecf20Sopenharmony_ci *
5678c2ecf20Sopenharmony_ci * NOTE: This guarantees that associated sysfs callbacks are not running
5688c2ecf20Sopenharmony_ci * or runnable, however any cdevs already open will remain and their fops
5698c2ecf20Sopenharmony_ci * will still be callable even after this function returns.
5708c2ecf20Sopenharmony_ci */
5718c2ecf20Sopenharmony_civoid cdev_device_del(struct cdev *cdev, struct device *dev)
5728c2ecf20Sopenharmony_ci{
5738c2ecf20Sopenharmony_ci	device_del(dev);
5748c2ecf20Sopenharmony_ci	if (dev->devt)
5758c2ecf20Sopenharmony_ci		cdev_del(cdev);
5768c2ecf20Sopenharmony_ci}
5778c2ecf20Sopenharmony_ci
5788c2ecf20Sopenharmony_cistatic void cdev_unmap(dev_t dev, unsigned count)
5798c2ecf20Sopenharmony_ci{
5808c2ecf20Sopenharmony_ci	kobj_unmap(cdev_map, dev, count);
5818c2ecf20Sopenharmony_ci}
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_ci/**
5848c2ecf20Sopenharmony_ci * cdev_del() - remove a cdev from the system
5858c2ecf20Sopenharmony_ci * @p: the cdev structure to be removed
5868c2ecf20Sopenharmony_ci *
5878c2ecf20Sopenharmony_ci * cdev_del() removes @p from the system, possibly freeing the structure
5888c2ecf20Sopenharmony_ci * itself.
5898c2ecf20Sopenharmony_ci *
5908c2ecf20Sopenharmony_ci * NOTE: This guarantees that cdev device will no longer be able to be
5918c2ecf20Sopenharmony_ci * opened, however any cdevs already open will remain and their fops will
5928c2ecf20Sopenharmony_ci * still be callable even after cdev_del returns.
5938c2ecf20Sopenharmony_ci */
5948c2ecf20Sopenharmony_civoid cdev_del(struct cdev *p)
5958c2ecf20Sopenharmony_ci{
5968c2ecf20Sopenharmony_ci	cdev_unmap(p->dev, p->count);
5978c2ecf20Sopenharmony_ci	kobject_put(&p->kobj);
5988c2ecf20Sopenharmony_ci}
5998c2ecf20Sopenharmony_ci
6008c2ecf20Sopenharmony_ci
6018c2ecf20Sopenharmony_cistatic void cdev_default_release(struct kobject *kobj)
6028c2ecf20Sopenharmony_ci{
6038c2ecf20Sopenharmony_ci	struct cdev *p = container_of(kobj, struct cdev, kobj);
6048c2ecf20Sopenharmony_ci	struct kobject *parent = kobj->parent;
6058c2ecf20Sopenharmony_ci
6068c2ecf20Sopenharmony_ci	cdev_purge(p);
6078c2ecf20Sopenharmony_ci	kobject_put(parent);
6088c2ecf20Sopenharmony_ci}
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_cistatic void cdev_dynamic_release(struct kobject *kobj)
6118c2ecf20Sopenharmony_ci{
6128c2ecf20Sopenharmony_ci	struct cdev *p = container_of(kobj, struct cdev, kobj);
6138c2ecf20Sopenharmony_ci	struct kobject *parent = kobj->parent;
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_ci	cdev_purge(p);
6168c2ecf20Sopenharmony_ci	kfree(p);
6178c2ecf20Sopenharmony_ci	kobject_put(parent);
6188c2ecf20Sopenharmony_ci}
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_cistatic struct kobj_type ktype_cdev_default = {
6218c2ecf20Sopenharmony_ci	.release	= cdev_default_release,
6228c2ecf20Sopenharmony_ci};
6238c2ecf20Sopenharmony_ci
6248c2ecf20Sopenharmony_cistatic struct kobj_type ktype_cdev_dynamic = {
6258c2ecf20Sopenharmony_ci	.release	= cdev_dynamic_release,
6268c2ecf20Sopenharmony_ci};
6278c2ecf20Sopenharmony_ci
6288c2ecf20Sopenharmony_ci/**
6298c2ecf20Sopenharmony_ci * cdev_alloc() - allocate a cdev structure
6308c2ecf20Sopenharmony_ci *
6318c2ecf20Sopenharmony_ci * Allocates and returns a cdev structure, or NULL on failure.
6328c2ecf20Sopenharmony_ci */
6338c2ecf20Sopenharmony_cistruct cdev *cdev_alloc(void)
6348c2ecf20Sopenharmony_ci{
6358c2ecf20Sopenharmony_ci	struct cdev *p = kzalloc(sizeof(struct cdev), GFP_KERNEL);
6368c2ecf20Sopenharmony_ci	if (p) {
6378c2ecf20Sopenharmony_ci		INIT_LIST_HEAD(&p->list);
6388c2ecf20Sopenharmony_ci		kobject_init(&p->kobj, &ktype_cdev_dynamic);
6398c2ecf20Sopenharmony_ci	}
6408c2ecf20Sopenharmony_ci	return p;
6418c2ecf20Sopenharmony_ci}
6428c2ecf20Sopenharmony_ci
6438c2ecf20Sopenharmony_ci/**
6448c2ecf20Sopenharmony_ci * cdev_init() - initialize a cdev structure
6458c2ecf20Sopenharmony_ci * @cdev: the structure to initialize
6468c2ecf20Sopenharmony_ci * @fops: the file_operations for this device
6478c2ecf20Sopenharmony_ci *
6488c2ecf20Sopenharmony_ci * Initializes @cdev, remembering @fops, making it ready to add to the
6498c2ecf20Sopenharmony_ci * system with cdev_add().
6508c2ecf20Sopenharmony_ci */
6518c2ecf20Sopenharmony_civoid cdev_init(struct cdev *cdev, const struct file_operations *fops)
6528c2ecf20Sopenharmony_ci{
6538c2ecf20Sopenharmony_ci	memset(cdev, 0, sizeof *cdev);
6548c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&cdev->list);
6558c2ecf20Sopenharmony_ci	kobject_init(&cdev->kobj, &ktype_cdev_default);
6568c2ecf20Sopenharmony_ci	cdev->ops = fops;
6578c2ecf20Sopenharmony_ci}
6588c2ecf20Sopenharmony_ci
6598c2ecf20Sopenharmony_cistatic struct kobject *base_probe(dev_t dev, int *part, void *data)
6608c2ecf20Sopenharmony_ci{
6618c2ecf20Sopenharmony_ci	if (request_module("char-major-%d-%d", MAJOR(dev), MINOR(dev)) > 0)
6628c2ecf20Sopenharmony_ci		/* Make old-style 2.4 aliases work */
6638c2ecf20Sopenharmony_ci		request_module("char-major-%d", MAJOR(dev));
6648c2ecf20Sopenharmony_ci	return NULL;
6658c2ecf20Sopenharmony_ci}
6668c2ecf20Sopenharmony_ci
6678c2ecf20Sopenharmony_civoid __init chrdev_init(void)
6688c2ecf20Sopenharmony_ci{
6698c2ecf20Sopenharmony_ci	cdev_map = kobj_map_init(base_probe, &chrdevs_lock);
6708c2ecf20Sopenharmony_ci}
6718c2ecf20Sopenharmony_ci
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_ci/* Let modules do char dev stuff */
6748c2ecf20Sopenharmony_ciEXPORT_SYMBOL(register_chrdev_region);
6758c2ecf20Sopenharmony_ciEXPORT_SYMBOL(unregister_chrdev_region);
6768c2ecf20Sopenharmony_ciEXPORT_SYMBOL(alloc_chrdev_region);
6778c2ecf20Sopenharmony_ciEXPORT_SYMBOL(cdev_init);
6788c2ecf20Sopenharmony_ciEXPORT_SYMBOL(cdev_alloc);
6798c2ecf20Sopenharmony_ciEXPORT_SYMBOL(cdev_del);
6808c2ecf20Sopenharmony_ciEXPORT_SYMBOL(cdev_add);
6818c2ecf20Sopenharmony_ciEXPORT_SYMBOL(cdev_set_parent);
6828c2ecf20Sopenharmony_ciEXPORT_SYMBOL(cdev_device_add);
6838c2ecf20Sopenharmony_ciEXPORT_SYMBOL(cdev_device_del);
6848c2ecf20Sopenharmony_ciEXPORT_SYMBOL(__register_chrdev);
6858c2ecf20Sopenharmony_ciEXPORT_SYMBOL(__unregister_chrdev);
686