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