码迷,mamicode.com
首页 > 其他好文 > 详细

在dev目录创建一个字符设备驱动的流程

时间:2016-10-21 16:24:54      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:ati   创建   rac   turn   ase   release   print   oct   结构   

1.struct file_operations 字符设备文件接口

   1:  static int mpu_open(struct inode *inode, struct file *file)
   2:  {    
   3:      return 0;
   4:  }
   5:   
   6:   
   7:   
   8:  static int mpu_close(struct inode *inode, struct file *file)
   9:  {
  10:      return 0;
  11:  }
  12:   
  13:   
  14:  static long mpu_ioctl ( struct file *filp, unsigned int cmd, unsigned long arg)
  15:  {
  16:      return 0;
  17:  }
  18:   
  19:  static ssize_t mpu_read(struct file * filp, char __user *buffer, size_t size, loff_t *off)
  20:  {
  21:      return 0;
  22:  }
  23:   
  24:  static ssize_t mpu_write(struct file *filp, const char __user *buffer, size_t size, loff_t *off)
  25:  {
  26:      return 0;
  27:  }
//结构
   1:  struct file_operations mpu_ops = {
   2:      .owner =  THIS_MODULE,
   3:      .open =   mpu_open,
   4:      .unlocked_ioctl =  mpu_ioctl,
   5:      .release = mpu_close, 
   6:      .read = mpu_read,
   7:      .write = mpu_write,
   8:  };
 
 
2.向内核注册字符设备框架
   1:  //根据设备号与设备名注册字符设备  
   2:      ret = register_chrdev(253, "dt-mpu", &mpu_ops);   
   3:      if (ret < 0) {  
   4:          printk("Unable to register character device %d!\n", 253);  
   5:          return ret;  
   6:      }  
 
3.在/sys/class下面注册一个类
   1:  mcls = class_create(THIS_MODULE, "dt-mpu"); 
   2:      if (!mcls) {
   3:          printk("class_create dt-mpu failure...\n");
   4:          goto un_register;
   5:      }
 

4.在/dev/目录创建dt-mpu设备文件

 //创建一个设备节点,节点名为DEVICE_NAME
    dev = device_create(mcls, NULL, MKDEV(253, 0), NULL, "dt-mpu");
    if (!dev) {
        printk("device_create dt-mpu failure...\n");
        goto des_class;
    }
 

在dev目录创建一个字符设备驱动的流程

标签:ati   创建   rac   turn   ase   release   print   oct   结构   

原文地址:http://www.cnblogs.com/xuyh/p/5984737.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!