简单字符设备驱动和自动创建设备文件

 

转载地址:http://blog.csdn.net/cjok376240497/article/details/6848536

必要的头文件]

  1. /* 
  2. *       Asimple character driver for learn 
  3. */  
  4. #include <linux/module.h>   
  5. #include <linux/types.h>   
  6. #include <linux/fs.h>   
  7. #include <linux/errno.h>   
  8. #include <linux/mm.h>   
  9. #include <linux/init.h>   
  10. #include <linux/sched.h>   
  11. #include <linux/cdev.h>   
  12. #include <asm/io.h>   
  13. #include <asm/system.h>   
  14. #include <asm/uaccess.h>   
  15. #include <linux/slab.h>    /* kmalloc */   
  16. #include <linux/device.h>         /* device_create, class_create */  

[宏定义]

  1. #define MAX_SIZE   1024   
  2. #define SIMPLE_MAJOR 256   
  3. #define CLASS_NAME "simple_class"   
  4. #define CHRDEV_NAME"simple_chrdev"  

[定义simple_chrdev结构体来表示一个简单的字符设备]

  1. struct simple_chrdev {  
  2.          structcdev cdev;  
  3.          charmem[MAX_SIZE];  
  4. };  

  1. struct simple_chrdev *dev;  
  2. static unsigned int major_no = 0;  
  3. static struct class *simple_class;  

[open方法]

  1. static int simple_open(struct inode *inode,struct file *filp)  
  2. {  
  3.          structsimple_chrdev *dev;  
  4.    
  5.          dev= container_of(inode->i_cdev, struct simple_chrdev, cdev);  
  6.          filp->private_data= dev;  
  7.          return0;  
  8. }  

填写filp->private_data里的数据结构,后面的read,write等方法可能会用到

container_of(pointer, containter_type, container_field):inode参数在其i_cdev字段中包含了我们所需要的信息,即我们先前设置的cdev。然而我们不需要cdev结构体本身,而是希望得到包含cdev结构体的simple_chrdev结构体。


[release 方法]

  1. static int simple_release(struct inode*inode, struct file *filp)  
  2. {  
  3.          return0;  
  4. }  

[read 方法]

  1. static ssize_t simple_read(struct file*filp, char __user *buf, size_t count,  
  2.                                                                  loff_t*f_pos)  
  3. {  
  4.          structsimple_chrdev *dev = filp->private_data;  
  5.          unsignedlong pos = *f_pos;  
  6.          intret = 0;  
  7.    
  8.          if(pos >= MAX_SIZE)  
  9.                    return- EFAULT;  
  10.    
  11.          if(count > MAX_SIZE - pos)  
  12.                    count= MAX_SIZE - pos;  
  13.           
  14.          if(copy_to_user(buf, (void *)(dev->mem + pos), count)) {  
  15.                    ret= -EFAULT;  
  16.                    gotoout;  
  17.          }  
  18.          *f_pos+= count;  
  19.          ret= count;  
  20.    
  21. out:  
  22.          returnret;  
  23. }  


[write 方法]

  1. static ssize_t simple_write(struct file*filp, const char __user *buf,  
  2.                                      size_tcount, loff_t *f_pos)  
  3. {  
  4.          structsimple_chrdev *dev = filp->private_data;  
  5.          unsignedlong pos = *f_pos;  
  6.          intret = 0;  
  7.    
  8.          if(pos >= MAX_SIZE)  
  9.                    return- EFAULT;  
  10.    
  11.          if(count > MAX_SIZE - pos)  
  12.                    count= MAX_SIZE - pos;  
  13.    
  14.          if(copy_from_user(dev->mem + pos, buf, count)) {  
  15.                    ret= - EFAULT;  
  16.                    gotoout;  
  17.          }  
  18.          *f_pos+= count;  
  19.          ret= count;  
  20.    
  21. out:  
  22.          returnret;  
  23. }  

read和write代码要做的工作就是在用户地址空间和内核地址空间之间进行整段数据的拷贝。注意:buf参数是用户空间的指针,内核代码不能直接引用其中的内容。

unsigned long copy_to_user(void __user *to,  const void * from, unsigned long count)

unsigned long copy_from_user(void *to,  const void __user *from, unsigned long count)

返回值:复制成功时,返回0;失败时,返回未复制的字节数。

这两个函数并不限于在内核空间和用户空间之间拷贝数据,它们还会检查用户空间的指针是否有效。


[llseek 方法]

  1. loff_t simple_llseek(struct file *filp,loff_t off, int whence)  
  2. {  
  3.          loff_tnewpos;  
  4.    
  5.          switch(whence) {  
  6.                    case0:     /*SEEK_SET*/  
  7.                             newpos= off;  
  8.                             break;  
  9.                     
  10.                    case1:     /*SEEK_CUR*/  
  11.                             newpos= filp->f_pos + off;  
  12.                             break;  
  13.    
  14.                    case2:     /*SEEK_END*/  
  15.                             newpos= MAX_SIZE + off;  
  16.                             break;  
  17.                     
  18.                    default:  
  19.                             return-EINVAL;  
  20.          }  
  21.    
  22.          if(newpos < 0)  
  23.                    return-EINVAL;  
  24.          filp->f_pos= newpos;  
  25.          returnnewpos;  
  26. }  
  27.    

  1. struct file_operations simple_fops = {  
  2.          .owner     = THIS_MODULE,  
  3.          .llseek       = simple_llseek,  
  4.          .read         = simple_read,  
  5.          .write       = simple_write,  
  6.          .open        = simple_open,  
  7.          .release   = simple_release,  
  8. };  



[模块初始化函数]

  1. static int __init simple_chrdev_init(void)  
  2. {  
  3.          intminor_no = 0;     /* 次设备号 */  
  4.          dev_tdev_no;     /* 设备号 */  
  5.          intret;  
  6.    
  7.          if(major_no) {  
  8.                    dev_no= MKDEV(major_no, minor_no);  
  9.                    ret= register_chrdev_region(dev_no, 1, CLASS_NAME);  
  10.          }else {  
  11.                    ret= alloc_chrdev_region(&dev_no, minor_no, 1, CHRDEV_NAME);  
  12.                    major_no= MAJOR(dev_no);  
  13.          }  
  14.          if(ret) {  
  15.                    printk("cannotget major %d\n", major_no);  
  16.                    returnret;  
  17.          }  
  18.          printk("getmajor %d\n", major_no);  
  19.    
  20.          dev= kmalloc(sizeof(struct simple_chrdev), GFP_KERNEL);  
  21.          if(dev == NULL) {  
  22.                    ret= - ENOMEM;  
  23.                    gotoerr_malloc;  
  24.          }  
  25.          memset(dev,0, sizeof(struct simple_chrdev));  
  26.    
  27.          cdev_init(&dev->cdev,&simple_fops);  
  28.          dev->cdev.owner= THIS_MODULE;  
  29.          dev->cdev.ops= &simple_fops;  
  30.          ret= cdev_add(&dev->cdev, dev_no, 1);  
  31.          if(ret) {  
  32.                    printk("cannotcdev add\n");  
  33.                    gotoerr_cdev_add;  
  34.          }  
  35.    
  36.          /*autocreate device inode file*/  
  37.          simple_class= class_create(THIS_MODULE, CHRDEV_NAME);  
  38.          if(IS_ERR(simple_class)) {  
  39.                    printk("ERR:cannot create a simple_class");  
  40.                    gotoerr_class_crt;  
  41.          }  
  42.          device_create(simple_class,NULL, MKDEV(major_no, 0), 0, CHRDEV_NAME);  
  43.    
  44.          printk("Iam in\n");  
  45.          return 0; /* 这个return 0;不能少 */  
  46.   
  47. err_class_crt:  
  48.          cdev_del(&dev->cdev);  
  49. err_cdev_add:  
  50.          kfree(dev);  
  51. err_malloc:  
  52.          unregister_chrdev_region(MKDEV(major_no,0), 1);  
  53.          returnret;  
  54. }  

模块加载函数所做的工作:

1、申请主设备号;

静态分配设备号  int register_chrdev_region(dev_t dev_no, unsigned int count, char *dev_name);

动态分配设备号  int alloc_chrdev_region(dev_t *dev, unsigned int firstminor, unsigned int count, char *dev_name);

2、注册字符设备;

初始化字符设备 cdev_init();

注册字符设备   cdev_add();

3、创建设备节点文件

当使用udev制作的文件系统时,内核为我们提供了一组函数,可以用来在模块加载的时候自动在/dev目录下创建设备文件OR在卸载时自动删除设备文件。

主要涉及两个函数

class_create(owner, class_name) 

用来在sysfs创建一个类,设备信息导出在这下面;

struct device *device_create(struct class *class, struct device *parent,
    dev_t devt, void *drvdata, const char *fmt, ...)

用来在/dev目录下创建一个设备文件;

加载模块时,用户空间的udev会自动响应device_create()函数,在sysfs下寻找对应的类从而创建设备节点。


[模块卸载函数]

  1. static void __exit simple_chrdev_exit(void)  
  2. {  
  3.          device_destroy(simple_class,MKDEV(major_no, 0));  
  4.          class_destroy(simple_class);  
  5.          cdev_del(&dev->cdev);  
  6.          kfree(dev);  
  7.          unregister_chrdev_region(MKDEV(major_no,0), 1);  
  8.          printk("Iam exit\n");  
  9. }  
注意:卸载函数顺序必须加载函数顺序相反

  1. module_init(simple_chrdev_init);  
  2. module_exit(simple_chrdev_exit);  
  3.    
  4. MODULE_AUTHOR("CJOK<cjok.liao@gmail.com>");  
  5. MODULE_LICENSE("Dual BSD/GPL");  
  6. MODULE_DESCRIPTION("A simple characterdriver for learn");  


if you have any questions, please contact me<cjok.liao@gmail.com> or leave a comment, we will exchange views, it's good for us, so great!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值