手机版
你好,游客 登录 注册
背景:
阅读新闻

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

[日期:2012-06-03] 来源:blog.csdn.net/tigerjb  作者:tigerjb [字体:大 中 小]

 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
相关资讯       Linux驱动开发 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款