sys文件系统下的接口函数

来源:互联网 发布:孟克鞋搭配 知乎 编辑:程序博客网 时间:2024/05/21 22:49

有很多的文章组合而成:

说到sys接口文件,就不得不说“ DEVICE_ATTR”

在介绍 DEVICE_ATTR之前,先介绍两个函数:

1  ssccanf()函数:

char buf[512] ;
sscanf("123456 ", "%s", buf);//此处buf是数组名,它的意思是将123456以%s的形式存入buf中!
printf("%s\n", buf);
结果为:123456
2 sprintf()函数:
   //把整数123 打印成一个字符串保存在s 中。
     sprintf(s, "%d", 123); //产生"123" 
下面开始正式介绍DEVICE_ATTR:
1 先看两个结构体:
 struct attribute {
                 const char              *name;
                 mode_t                  mode;
         #ifdef CONFIG_DEBUG_LOCK_ALLOC
                struct lock_class_key   *key;
                struct lock_class_key   skey;
         #endif
         };
     /* interface for exporting device attributes */
            struct device_attribute {
                   struct attribute        attr;
                    ssize_t (*show)(struct device *dev, struct device_attribute *attr,
                                    char *buf);
                    ssize_t (*store)(struct device *dev, struct device_attribute *attr,
                                     const char *buf, size_t count);
            };
_show表示的是读方法,_stroe表示的是写方法。
DEVICE_ATTR  宏声明有四个参数,分别是名称、权限位、读函数、写函数。其中读函数和写函数是读写功能函数的函数名。

2 在linux内核中分4种attr:

      1 BUS_ATTR。
      2 DRIVER_ATTR
      3 CLASS_ATTR
      4 DEVICE_ATTR。
     对设备的使用  DEVICE_ATTR  ,对总线使用  BUS_ATTR  ,对驱动使用 DRIVER_ATTR  ,对类别 (class) 使用  CLASS_ATTR,  这四个高级的宏来自于<include/linux/device.h> 

3 ATTR的几种属性: