cdev_add alloc_chrdev_region 系列函数

在Linux 2.6内核中的字符设备用cdev结构来描述,其定义如下:

  1. struct cdev  
  2.     struct kobject kobj; 
  3.     struct module *owner; //所属模块 
  4.     const struct file_operations *ops; //文件操作结构 
  5.     struct list_head list; 
  6.     dev_t dev; //设备号,int 类型,高12位为主设备号,低20位为次设备号 
  7.     unsigned int count; 
  8. }; 
下面一组函数用来对cdev结构进行操作:
  1. struct cdev *cdev_alloc(void);//分配一个cdev 
  2. void cdev_init(struct cdev *, const struct file_operations *);//初始化cdev的file_operation 
  3. void cdev_put(struct cdev *p);// //减少使用计数 
  4. //注册设备,通常发生在驱动模块的加载函数中 
  5. int cdev_add(struct cdev *, dev_t, unsigned);  
  6. //注销设备,通常发生在驱动模块的卸载函数中 
  7. void cdev_del(struct cdev *);  

使用cdev_add注册字符设备前应该先调用register_chrdev_region或alloc_chrdev_region分配设备号。register_chrdev_region函数用于指定设备号的情况,alloc_chrdev_region函数用于动态申请设备号,系统自动返回没有占用的设备号。

  1. int register_chrdev_region(dev_t from, unsigned count, const char *name) 
  2. int alloc_chrdev_region(dev_t *dev,unsigned baseminor,unsigned count,const char *name); 
alloc_chrdev_region申请一个动态主设备号,并申请一系列次设备号。baseminor为起始 次设备号count为次设备号的数量。注销设备号(cdev_del)后使用unregister_chrdev_region:
  1. void unregister_chrdev_region(dev_t from,unsigned count) 

 

 cdev_add注册字符设备实例

struct file_operations simple_fops 

  1.     .owner    THIS_MODULE, 
  2.     .read     simple_read, 
  3.     .write    simple_write, 
  4.     .open     simple_open, 
  5.     .release  simple_release, 
  6. }; 
  7.  
  8. void simple_cleanup_module(void) 
  9.     dev_t devno MKDEV(simple_MAJOR, simple_MINOR); 
  10.     if (simple_devices)  
  11.     
  12.         cdev_del(&simple_devices->cdev); 
  13.         kfree(simple_devices); 
  14.     
  15.     unregister_chrdev_region(devno,1); 
  16. //模块初始化 
  17. int simple_init_module(void) 
  18.     int result; 
  19.     dev_t dev 0; 
  20.     dev MKDEV(simple_MAJOR, simple_MINOR); 
  21.     result register_chrdev_region(dev, 1, "DEMO");//申请设备号 
  22.     if (result <</SPAN> 0)  
  23.     
  24.         printk(KERN_WARNING "DEMO: can't get major %d\n", simple_MAJOR); 
  25.         return result; 
  26.     
  27.     simple_devices kmalloc(sizeof(struct simple_dev), GFP_KERNEL); 
  28.     if (!simple_devices) 
  29.     
  30.         result -ENOMEM; 
  31.         goto fail; 
  32.     
  33.     memset(simple_devices, 0, sizeof(struct simple_dev)); 
  34.     //初始化设备结构 
  35.     cdev_init(&simple_devices->cdev, &simple_fops); 
  36.     simple_devices->cdev.owner THIS_MODULE; 
  37.     simple_devices->cdev.ops &simple_fops; 
  38.     result cdev_add (&simple_devices->cdev, dev, 1);//添加字符设备 
  39.     if(result) 
  40.     
  41.         printk(KERN_NOTICE "Error %d adding DEMO\n", result); 
  42.         goto fail; 
  43.     
  44.     return 0; 
  45. fail: 
  46.     simple_cleanup_module(); 
  47.     return result; 
  48. module_init(simple_init_module); 
  49. module_exit(simple_cleanup_module);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值