使用cdev_add注册字符设备的一个例子

1.2.3  使用cdev_add注册字符设备

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

 
 
  1. struct cdev   
  2. {  
  3.     struct kobject kobj;  
  4.     struct module *owner; //所属模块  
  5.     const struct file_operations *ops; //文件操作结构  
  6.     struct list_head list;  
  7.     dev_t dev; //设备号,int 类型,高12位为主设备号,低20位为次设备号  
  8.     unsigned int count;  
  9. };  

下面一组函数用来对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) ; 

例1.4  cdev_add注册字符设备实例

代码见光盘\src\1drivermodel\1-4cdev。核心代码如下所示:

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

本例的应用层代码与运行结果同上例。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值