DEVICE_ATTR实例分析
发布日期:2021-06-30 21:49:46 浏览次数:2 分类:技术文章

本文共 3111 字,大约阅读时间需要 10 分钟。

在内核中, sysfs 属性一般是由 __ATTR 系列的宏来声明的,如对设备的使用 DEVICE_ATTR ,对总线使用 BUS_ATTR ,对驱动使用 DRIVER_ATTR ,对类别(class)使用 CLASS_ATTR, 这四个高级的宏来自于 <include/linux/device.h>, 都是以更低层的来自 <include/linux/sysfs.h> 中的 __ATTR/__ATRR_RO 宏实现。

在adb shell 终端查看到接口,当我们将数据 echo 到接口中时,在上层实际上完成了一次 write 操作,对应到 kernel ,调用了驱动中的 “store”。同理,当我们cat 一个 接口时则会调用 “show” 。到这里,只是简单的建立了 android 层到 kernel 的桥梁,真正实现对硬件操作的,还是在 "show" 和 "store" 中完成的。

 

实现办法,例一:

#include <linux/platform_device.h>

1)

static ssize_t rohm_proximity_show_debug(struct device* cd,struct device_attribute *attr, char* buf)

{
    ssize_t ret = 0;
    
    sprintf(buf, "ROHM Debug %d\n",debug_level);//将格式数据写到缓冲里面,此处为buf
    
    ret = strlen(buf) + 1;

    return ret;

}

static ssize_t rohm_proximity_store_debug(struct device* cd, struct device_attribute *attr,

               const char* buf, size_t len)
{
    unsigned long on_off = simple_strtoul(buf, NULL, 10);//解析字符串cp  81016 进制数   ,返回值是解析的数字,endp 指向字符串起始处,base :进制
    debug_level = on_off;

    printk("%s: debug_level=%d\n",__func__, debug_level);

    
    return len;
}

2)

static DEVICE_ATTR(debug, S_IRUGO | S_IWUSR, rohm_proximity_show_debug, rohm_proximity_store_debug);

 3)

static int rohm_proximity_create_sysfs(struct platform_device *client)

{
    struct device *dev = &(client->dev);
    int err = 0;

    PS_DBG("%s\n", __func__);

    if ((err = device_create_file(dev, &dev_attr_control)))

        goto err_out;

    if ((err = device_create_file(dev, &dev_attr_debug)))

        goto err_out;

    return 0;

err_out:

    return err;
}

4)

static int rohm_proximity_probe(struct platform_device *pdev)

{

         rohm_proximity_create_sysfs(pdev);

}

 

实现办法,例二:

1)

static ssize_t mc32x0_threshold_show(struct device *dev,

                     struct device_attribute *attr, char *buf)
{
#ifdef MC32X0_DEBUG
       printk("mcube %s\n",__FUNCTION__);
#endif

    return sprintf(buf, "%d\n", mc32x0_get_threshold(dev));

}

static ssize_t mc32x0_threshold_store(struct device *dev,

                      struct device_attribute *attr,
                      const char *buf, size_t count)
{
    unsigned long threshold;

#ifdef MC32X0_DEBUG

       printk("mcube %s\n",__FUNCTION__);
#endif

    threshold = simple_strtoul(buf, NULL,10);

        if (threshold >= 0 && threshold <= ABSMAX_8G) {
        mc32x0_set_threshold(dev, threshold);
        }   

    return count;

}

2)

static DEVICE_ATTR(threshold, 0666,    mc32x0_threshold_show, mc32x0_threshold_store);

static struct attribute *mc32x0_attributes[] = {

  //  &dev_attr_enable.attr,
   // &dev_attr_delay.attr,
  //  &dev_attr_position.attr,
    &dev_attr_threshold.attr,
};

3)

static struct attribute_group mc32x0_attribute_group = {

    .attrs = mc32x0_attributes
};

 

4)

static int mc32x0_probe(struct i2c_client *client, const struct i2c_device_id *id)

{

    err = sysfs_create_group(&mc32x0->input->dev.kobj, &mc32x0_attribute_group);//.probe中生成

    if (err < 0) {

        goto error_2;
    }

    //sysfs_remove_group(&mc32x0->input->dev.kobj, &mc32x0_attribute_group);//.remove中移除

 

}

adb下输入:(以下节点为DEVICE_ATTR(debug,,)同为,位置在/sys/devices/platform/rohm_proximity/,在要/目录下find . -name "debug"查找)

#cat debug         //此端口下会输出"ROHM Debug XX",XX为此处值。注:另一adb端口cat /proc/kmsg不会显示此字符

#echo 01>debug //在另外一个adb端口下cat /proc/kmsg会显示"rohm_proximity_store_debug: debug_level=1"。

注:echo 01>debug值为1,echo 1>debug值为0,echo 0>debug无效.

此时已给此端口写1,再#cat debug,端口下会输出"ROHM Debug 1"

 

参考文档:

转载地址:https://loongembedded.blog.csdn.net/article/details/41174525 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:内核驱动中常见的miscdevice、platform_device、platform_driver
下一篇:虚拟机安装Ubuntu14.04打开FireFox提示Server not found

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月27日 10时12分55秒