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