Linux字符设备驱动之register_chrdev_region()系列

Linux字符设备驱动之 register_chrdev_region()函数系列

1.内核中所有已分配的字符设备编号都记录在一个名为 chrdevs 散列表里。该散列表中的每一个元素是一个 char_device_struct 结构,它的定义如下:

static struct char_device_struct {

struct char_device_struct *next; // 指向散列冲突链表中的下一个元素的指针

unsigned int major; // 主设备号

unsigned int baseminor; // 起始次设备号

int minorct; // 设备编号的范围大小

char name[64]; // 处理该设备编号范围内的设备驱动的名称

struct file_operations *fops;

struct cdev *cdev; // 指向字符设备驱动程序描述符的指针

} *chrdevs[CHRDEV_MAJOR_HASH_SIZE];

1>内核并不是为每一个字符设备编号定义一个 char_device_struct 结构,而是为一组对应同一个字符设备驱动的设备编号范围定义一个 char_device_struct 结构。chrdevs 散列表的大小是 255,散列算法是把每组字符设备编号范围的主设备号以 255 取模插入相应的散列桶中。同一个散列桶中的字符设备编号范围是按起始次设备号递增排序的。

2.注册

内核提供了三个函数来注册一组字符设备编号,这三个函数分别是 register_chrdev_region()alloc_chrdev_region()

register_chrdev()。这三个函数都会调用一个共用的

__register_chrdev_region() 函数来注册一组设备编号范围(即一个 char_device_struct 结构)。

1>int register_chrdev_region(dev_t from, unsigned count, const char *name)

from :要分配的设备编号范围的初始值(次设备号常设为0);

Count:连续编号范围.

name:编号相关联的设备名称. (/proc/devices);

2>动态分配:

int alloc_chrdev_region(dev_t *dev,unsigned int firstminor,unsigned int count,char *name);

firstminor是请求的最小的次编号;

count是请求的连续设备编号的总数;

name为设备名,返回值小于0表示分配失败。

然后通过major=MMOR(dev)获取主设备号

3>释放:

Void unregist_chrdev_region(dev_t first,unsigned int count);

调用Documentation/devices.txt中能够找到已分配的设备号.

3.__register_chrdev_region() 函数的实现代码

/*

84 * Register a single major with a specified minor range.

85 *

86 * If major == 0 this functions will dynamically allocate a major and return

87 * its number.

88 *

89 * If major > 0 this function will attempt to reserve the passed range of

90 * minors and will return zero on success.

91 *

92 * Returns a -ve errno on failure.

93 */

94static struct char_device_struct *

95__register_chrdev_region(unsigned int major, unsigned int baseminor,

96 int minorct, const char *name)

97{

98 struct char_device_struct *cd, **cp;

99 int ret = 0;

100 int i;

101

102 cd = kzalloc(sizeof(struct char_device_struct), GFP_KERNEL);

103 if (cd == NULL)

104 return ERR_PTR(-ENOMEM);

105

106 mutex_lock(&chrdevs_lock);

107

108 /* temporary */

109 if (major == 0) {

110 for (i = ARRAY_SIZE(chrdevs)-1; i > 0; i--) {

111 if (chrdevs[i] == NULL)

112 break;

113 }

114

115 if (i == 0) {

116 ret = -EBUSY;

117 goto out;

118 }

119 major = i;

120 ret = major;

121 }

122

123 cd->major = major;

124 cd->baseminor = baseminor;

125 cd->minorct = minorct;

126 strlcpy(cd->name, name, sizeof(cd->name));

127

128 i = major_to_index(major);

129

130 for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next)

131 if ((*cp)->major > major ||

132 ((*cp)->major == major &&

133 (((*cp)->baseminor >= baseminor) ||

134 ((*cp)->baseminor + (*cp)->minorct > baseminor))))

135 break;

136

137 /* Check for overlapping minor ranges. */

138 if (*cp && (*cp)->major == major) {

139 int old_min = (*cp)->baseminor;

140 int old_max = (*cp)->baseminor + (*cp)->minorct - 1;

141 int new_min = baseminor;

142 int new_max = baseminor + minorct - 1;

143

144 /* New driver overlaps from the left. */

145 if (new_max >= old_min && new_max <= old_max) {

146 ret = -EBUSY;

147 goto out;

148 }

149

150 /* New driver overlaps from the right. */

151 if (new_min <= old_max && new_min >= old_min) {

152 ret = -EBUSY;

153 goto out;

154 }

155 }

156

157 cd->next = *cp;

158 *cp = cd;

159 mutex_unlock(&chrdevs_lock);

160 return cd;

161out:

162 mutex_unlock(&chrdevs_lock);

163 kfree(cd);

164 return ERR_PTR(ret);

165}

函数 __register_chrdev_region() 主要执行以下步骤:

1> 分配一个新的 char_device_struct 结构,并用 0 填充。

2> 如果申请的设备编号范围的主设备号为 0,那么表示设备驱动程序请求动态分配一个主设备号。动态分配主设备号的原则是从散列表的最后一个桶向前寻找,那个桶是空的,主设备号就是相应散列桶的序号。所以动态分配的主设备号总是小于 256,如果每个桶都有字符设备编号了,那动态分配就会失败。

3> 根据参数设置 char_device_struct 结构中的初始设备号,范围大小及设备驱动名称。

4> 计算出主设备号所对应的散列桶,为新的 char_device_struct 结构寻找正确的位置。同时,如果设备编号范围有重复的话,则出错返回。

5> 将新的 char_device_struct 结构插入散列表中,并返回 char_device_struct 结构的地址。

4.分析三个注册函数

1> register_chrdev_region()

186

187/**

188 * register_chrdev_region() - register a range of device numbers

189 * @from: the first in the desired range of device numbers; must include

190 * the major number.

191 * @count: the number of consecutive device numbers required

192 * @name: the name of the device or driver.

193 *

194 * Return value is zero on success, a negative error code on failure.

195 */

196int register_chrdev_region(dev_t from, unsigned count, const char *name)

197{

198 struct char_device_struct *cd;

199 dev_t to = from + count;

200 dev_t n, next;

201

202 for (n = from; n < to; n = next) {

203 next = MKDEV(MAJOR(n)+1, 0);

204 if (next > to)

205 next = to;

206 cd = __register_chrdev_region(MAJOR(n), MINOR(n),

207 next - n, name);

208 if (IS_ERR(cd))

209 goto fail;

210 }

211 return 0;

212fail:

213 to = n;

214 for (n = from; n < to; n = next) {

215 next = MKDEV(MAJOR(n)+1, 0);

216 kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));

217 }

218 return PTR_ERR(cd);

}

register_chrdev_region() 函数用于分配指定的设备编号范围。如果申请的设备编号范围跨越了主设备号,它会把分配范围内的编号按主设备号分割成较小的子范围,并在每个子范围上调用 __register_chrdev_region() 。如果其中有一次分配失败的话,那会把之前成功分配的都全部退回。

2> alloc_chrdev_region()

221/**

222 * alloc_chrdev_region() - register a range of char device numbers

223 * @dev: output parameter for first assigned number

224 * @baseminor: first of the requested range of minor numbers

225 * @count: the number of minor numbers required

226 * @name: the name of the associated device or driver

227 *

228 * Allocates a range of char device numbers. The major number will be

229 * chosen dynamically, and returned (along with the first minor number)

230 * in @dev. Returns zero or a negative error code.

231 */

232int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count,

233 const char *name)

234{

235 struct char_device_struct *cd;

236 cd = __register_chrdev_region(0, baseminor, count, name);

237 if (IS_ERR(cd))

238 return PTR_ERR(cd);

239 *dev = MKDEV(cd->major, cd->baseminor);

240 return 0;

}

alloc_chrdev_region() 函数用于动态申请设备编号范围,这个函数好像并没有检查范围过大的情况,不过动态分配总是找个空的散列桶,所以问题也不大。通过指针参数返回实际获得的起始设备编号。

3> register_chrdev()

2090static inline int register_chrdev(unsigned int major, const char *name,

2091 const struct file_operations *fops)

2092{

2093 return __register_chrdev(major, 0, 256, name, fops);

2094}

2095

最后一个 register_chrdev() 是一个老式分配设备编号范围的函数。它分配一个单独主设备号和 0 ~ 255 的次设备号范围。如果申请的主设备号为 0 则动态分配一个。该函数还需传入一个 file_operations 结构的指针,函数内部自动分配了一个新的 cdev 结构。关于这些,在后续讲字符设备驱动的注册时会说明

5.注销:

和注册分配字符设备编号范围类似,内核提供了两个注销字符设备编号范围的函数,分别是 unregister_chrdev_region() unregister_chrdev() 。它们都调用了

__unregister_chrdev_region() 函数。

1>__unregister_chrdev_region()

319

320/**

321 * __unregister_chrdev - unregister and destroy a cdev

322 * @major: major device number

323 * @baseminor: first of the range of minor numbers

324 * @count: the number of minor numbers this cdev is occupying

325 * @name: name of this range of devices

326 *

327 * Unregister and destroy the cdev occupying the region described by

328 * @major, @baseminor and @count. This function undoes what

329 * __register_chrdev() did.

330 */

331void __unregister_chrdev(unsigned int major, unsigned int baseminor,

332 unsigned int count, const char *name)

333{

334 struct char_device_struct *cd;

335

336 cd = __unregister_chrdev_region(major, baseminor, count);

337 if (cd && cd->cdev)

338 cdev_del(cd->cdev);

339 kfree(cd);

340}

341

2>unregister_chrdev_region()

298/**

299 * unregister_chrdev_region() - return a range of device numbers

300 * @from: the first in the range of numbers to unregister

301 * @count: the number of device numbers to unregister

302 *

303 * This function will unregister a range of @count device numbers,

304 * starting with @from. The caller should normally be the one who

305 * allocated those numbers in the first place...

306 */

307void unregister_chrdev_region(dev_t from, unsigned count)

308{

309 dev_t to = from + count;

310 dev_t n, next;

311

312 for (n = from; n < to; n = next) {

313 next = MKDEV(MAJOR(n)+1, 0);

314 if (next > to)

315 next = to;

316 kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));

317 }

3>unregister_chrdev

2096static inline void unregister_chrdev(unsigned int major, const char *name)

2097{

2098 __unregister_chrdev(major, 0, 256, name);

2099}

你可能感兴趣的:(linux)