zoukankan      html  css  js  c++  java
  • DEVICE_ATTR实例分析

    在内核中, 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 ",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 ",__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 ", __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 ",__FUNCTION__);
    #endif

        return sprintf(buf, "%d ", 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 ",__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"

     

    参考文档:http://blog.chinaunix.net/uid-26413351-id-3180609.html

  • 相关阅读:
    Java 基础巩固:装箱拆箱 你真的熟悉吗
    Java数据类型的转换:隐式(自动)转换与强制转换
    oozie学习笔记
    flume学习笔记
    sqoop学习笔记
    Oracle故障排查之oracle解决锁表问题
    zookerper安装部署
    hadoop HA架构安装部署(QJM HA)
    hadoop第一部分-安装、测试
    hadoop完全分布式安装部署-笔记
  • 原文地址:https://www.cnblogs.com/liang123/p/6325419.html
Copyright © 2011-2022 走看看