设备号,是内核标识字符设备的唯一标志;设备号分为主设备号和次设备号;在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)