Linux字符设备动态申请设备号


alloc_chrdev_region是一个函数语句,头文件是<linux/fs.h>,可以动态分配设备编号。

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


dev_t *dev:用于返回的设备号参数

unsigned int -firstminor:默认为0

unsigned int -count:请求连续设备号的个数

char *name:待注册的设备名称

在不再使用时释放这些设备编号

void unregister_chrdev_region(dev_t from, unsigned int count);


////////////////////////////////////////////////////////////////////////
dev_t dev = 0;

ret = alloc_chrdev_region(&dev, 0, 1,DEVICE_NAME);

if(ret<0)
{ 
    printk(KERN_ERR "register_chrdev_region error
"); goto alloc_err; 
}

///////////////////////////////////////////////////////////////////////

  

原文地址:https://www.cnblogs.com/achao123456/p/12012596.html