zoukankan      html  css  js  c++  java
  • 在dev目录创建一个字符设备驱动的流程

    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!
    ", 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...
    ");
       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...
    ");
            goto des_class;
        }
     

  • 相关阅读:
    微信小程序中 数据缓存
    javascript 数组去重方法
    常用网站 ---总结
    vue面试题
    vue创建项目(命令方式)
    shutil 模块:文件复制.移动.压缩.解压.递归删除文件夹
    面向对象一些零散的知识点
    面向对象之多态
    面向对象与继承
    序列化与反序列化的三个模块:json pickle shelve
  • 原文地址:https://www.cnblogs.com/xuyh/p/5984737.html
Copyright © 2011-2022 走看看