设备号

设备号,是内核标识字符设备的唯一标志;设备号分为主设备号和次设备号;在inode中保存着设备号,用来标识设备,从VFS中找到相应的设备;

1.主设备号的动态分配:alloc_chrdev_region()

/**
 * alloc_chrdev_region() - register a range of char device numbers
 * @dev:       output parameter for first assigned number
 * @baseminor: first of the requested range of minor numbers,always 0;
 * @count:     the number of minor numbers required
 * @name:      the name of the associated device or driver
 *
 * Allocates a range of char device numbers.  The major number will be chosen dynamically, and returned (along with the first minor
   number)in @dev.  Returns zero or a negative error code.
 */
int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count,const char *name)
使用例子:

rc = alloc_chrdev_region(&ape->cdevno, 0/*requested minor*/, 1/*count*/, DRV_NAME);
	/* allocation failed? */
	if (rc < 0) {
		printk("alloc_chrdev_region() = %d\n", rc);
		goto fail_alloc;
	}

2.注册设备号:register_chrdev_orgion()

在建立一个字符设备之前,驱动程序首先要做的事情就是向内核注册一个或多个设备编号;

/**
 * register_chrdev_region() - register a range of device numbers
 * @from: the first in the desired range of device numbers; must include the major number.设备号
 * @count: the number of consecutive device numbers required
 * @name: the name of the device or driver.
 *
 * Return value is zero on success, a negative error code on failure.
 */
int register_chrdev_region(dev_t from, unsigned count, const char *name)
3.释放设备号   unregister_chrdev_region

设备号 也属于系统资源,再不用的时候硬释放这些设备编号。

/**
 * unregister_chrdev_region() - return a range of device numbers
 * @from: the first in the range of numbers to unregister
 * @count: the number of device numbers to unregister
 *
 * This function will unregister a range of @count device numbers,
 * starting with @from.  The caller should normally be the one who
 * allocated those numbers in the first place...
 */
void unregister_chrdev_region(dev_t from, unsigned count)



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值