• 在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;
        }
     

  • 相关阅读:
    构建调试Linux内核网络代码的环境MenuOS系统
    关于iOS开发证书的一些总结(很有用)
    iOS原型模式
    iOS不用调用,running time自动执行方法
    关于网络设计服务结构的一些理解
    iOS 6.0之后支持一个页面横屏的方法
    iOS返回一个前面没有0,小数点后保留两位的数字字符串
    CoreData总结
    分享一个复用代码块的博客
    分享一个可以打开沙盒的软件
  • 原文地址:https://www.cnblogs.com/xuyh/p/5984737.html
Copyright © 2020-2023  润新知