前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ATTR节点应用

ATTR节点应用

作者头像
开源519
发布2020-07-23 17:08:23
2.3K0
发布2020-07-23 17:08:23
举报
文章被收录于专栏:开源519

1.ATTR介绍

应用层与内核驱动层的交互,一般是通过驱动节点的读写来实现。即驱动开发人员在完成驱动设备的创建后,同时会创建对应的节点,且提供节点的访问函数,以便应用层开发调用。驱动提供接口的方法有注册file_operation结构体,另一种方法就是本文要记录的建立ATTR节点。

使用DEVICE_ATTR,可以实现驱动在sys目录自动创建文件,我们只需要实现show和store函数即可。然后在应用层就能通过cat和echo命令来对sys创建出来的文件进行读写驱动设备,实现交互。

2.基本原理概述

2.1 DEVICE_ATTR()

DEVICE_ATTR()定义位于Android/kernel-4.14/include/linux/device.h

代码语言:javascript
复制
#define DEVICE_ATTR(_name, _mode, _show, _store) \
   struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)

__ATTR()位于Android/kernel-4.14/include/linux/sysfs.h

代码语言:javascript
复制
#define __ATTR_PREALLOC(_name, _mode, _show, _store) {         \
   .attr = {.name = __stringify(_name),                            \
             .mode = SYSFS_PREALLOC | VERIFY_OCTAL_PERMISSIONS(_mode) },\
   .show   = _show,                                                  \
   .store   = _store,                                                  \
}

发现DEVICE_ATTR()只是内核定义的一个宏,使用此宏作用相当于定义并填充了一个device_attribute类型的结构体dev_attr_xx。其中需要注意的是_mode参数表示文件权限,一般是给0664,权限比0664高会报错(也可以用S_IWUSR(用户可写),S_IRUSR(用户可读)等宏代替)。

将DEVICE_ATTR()宏完全展开

代码语言:javascript
复制
#define DEVICE_ATTR(_name, _mode, _show, _store)         \
struct device_attribute dev_attr_##_name = {             \
   .attr = {.name = __stringify(_name),                  \
             .mode = SYSFS_PREALLOC | VERIFY_OCTAL_PERMISSIONS(_mode) },\
   .show   = _show,                                      \
   .store   = _store,                                    \
}

2.2 device_attribute

device_attribute同样定义位于Android/kernel-4.14/include/linux/device.h

代码语言:javascript
复制
/* 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);
};

device_attribute结构体内包含一个attribute 和 show、store函数。其中show接口实现adb内 cat读取显示功能,store实现的是echo 写入功能。

因此在驱动中,需要实现show、store接口功能。

2.3 device_create_file

在实例完struct device_attribute dev_attr_xx后,需要将此结构体通过device_create_file注册到内核中/sys/bus/platform/devices下。定义在kernel-4.14/drivers/base/core.c

代码语言:javascript
复制
int device_create_file(struct device *dev,
             const struct device_attribute *attr)
{
   int error = 0;
   if (dev) {
      WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
         "Attribute %s: write permission without 'store'\n",
         attr->attr.name);
      WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
         "Attribute %s: read permission without 'show'\n",
         attr->attr.name);
      error = sysfs_create_file(&dev->kobj, &attr->attr);
   }
   return error;
}
EXPORT_SYMBOL_GPL(device_create_file);

3.实际操作

首先在内核创建一个驱动文件,这里命名为attr_test。

增加节点读接口

/*创建ATTR可读节点接口*/

代码语言:javascript
复制
static ssize_t show_attr_test(struct device *dev, struct device_attribute *attr, char *buf)
{
    int ret = 0;
    
    ret = snprintf(buf, strlen(global_char), "%s\n", global_char);
    printk("%s:%d: Entry %s, buf:%s", __FILE__, __LINE__, __func__, buf);
    
    return ret;
}

/* 创建ATTR可写节点接口 */

代码语言:javascript
复制
static ssize_t store_attr_test(struct device *dev, struct device_attribute *attr,
             const char *buf, size_t count)
{
    int ret = 0;
   
    memset(global_char, 0x00, sizeof(global_char));
    ret = snprintf(global_char, sizeof(global_char), "%s", buf);
    printk("%s:%d: Entry %s", __FILE__, __LINE__, __func__);


    return ret;
}

填充到结构体dev_attr_attr_test

代码语言:javascript
复制
DEVICE_ATTR(attr_test, 0664, show_attr_test, store_attr_test);

注册到内核中

代码语言:javascript
复制
ret = device_create_file(&pdev->dev, &dev_attr_attr_test);

注意:在实现show、store时,返回值必须为具体的字符长度,若返回0则会出导致数据传输出现问题,cat、echo功能也会失败。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-06-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 开源519 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 2.基本原理概述
    • 2.1 DEVICE_ATTR()
      • 2.2 device_attribute
        • 2.3 device_create_file
        • 3.实际操作
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档