TP驱动的sys节点建立

来源:互联网 发布:mac ipython 安装失败 编辑:程序博客网 时间:2024/05/16 11:26

2012.5.30 星期三
  
  关于TP驱动的sys节点建立问题的解释。
  网上关于sysfs接口建立的解释为:
  
  sysfs接口函数到建立_DEVICE_ATTR

最近在弄Sensor驱动,看过一个某厂家的成品驱动,里面实现的全都是sysfs接口,hal层利用sysfs生成的接口,对Sensor进行操作。

说道sysfs接口,就不得不提到函数宏 DEVICE_ATTR

原型是#define DEVICE_ATTR(_name, _mode, _show, _store) \

struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)

函数宏DEVICE_ATTR内封装的是__ATTR(_name,_mode,_show,_stroe)方法,_show表示的是读方法,_stroe表示的是写方法。

当然_ATTR不是独生子女,他还有一系列的姊妹__ATTR_RO宏只有读方法,__ATTR_NULL等等

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

DEVICE_ATTR  宏声明有四个参数,分别是名称、权限位、读函数、写函数。其中读函数和写函数是读写功能函数的函数名。

如果你完成了DEVICE_ATTR函数宏的填充,下面就需要创建接口了

例如:

    static DEVICE_ATTR(polling, S_IRUGO | S_IWUSR, show_polling, set_polling);
    static struct attribute *dev_attrs[] = {
            &dev_attr_polling.attr,
            NULL,
    };

当你想要实现的接口名字是polling的时候,需要实现结构体struct attribute *dev_attrs[]

其中成员变量的名字必须是&dev_attr_polling.attr

然后再封装

    static struct attribute_group dev_attr_grp = {
            .attrs = dev_attrs,
    };


在利用sysfs_create_group(&pdev->dev.kobj, &dev_attr_grp);创建接口
通过以上简单的三个步骤,就可以在adb shell 终端查看到接口了。当我们将数据 echo 到接口中时,在上层实际上完成了一次 write 操作,对应到 kernel ,调用了驱动中的 “store”。同理,当我们cat 一个 接口时则会调用 “show” 。到这里,只是简单的建立了 android 层到 kernel 的桥梁,真正实现对硬件操作的,还是在 "show" 和 "store" 中完成的。
///////////////////////////////////////////////////////////////////////////

具体到TP驱动中就是
/* sysfs */
//static DEVICE_ATTR(rawbase, S_IRUGO|S_IWUSR, ft5x0x_rawbase_show, ft5x0x_rawbase_store);
static DEVICE_ATTR(ftstpfwver, S_IRUGO|S_IWUSR, ft5x0x_tpfwver_show, ft5x0x_tpfwver_store);
//upgrade from *.i
static DEVICE_ATTR(ftsfwupdate, S_IRUGO|S_IWUSR, ft5x0x_fwupdate_show, ft5x0x_fwupdate_store);
static DEVICE_ATTR(ftstprwreg, S_IRUGO|S_IWUSR, ft5x0x_tprwreg_show, ft5x0x_tprwreg_store);
//upgrade from app.bin 
static DEVICE_ATTR(ftsfwupgradeapp, S_IRUGO|S_IWUSR, ft5x0x_fwupgradeapp_show, ft5x0x_fwupgradeapp_store);
//static DEVICE_ATTR(ftsrawdatashow, S_IRUGO|S_IWUSR, ft5x0x_rawdata_show, ft5x0x_rawdata_store);


static struct attribute *ft5x0x_attributes[] = {
 &dev_attr_ftstpfwver.attr,
 &dev_attr_ftsfwupdate.attr,
 &dev_attr_ftstprwreg.attr,
 &dev_attr_ftsfwupgradeapp.attr,
 //ev_attr_ftsrawdatashow.attr,
 NULL
};

static struct attribute_group ft5x0x_attribute_group = {
 .attrs = ft5x0x_attributes
};
  err = sysfs_create_group(&client->dev.kobj, &ft5x0x_attribute_group);
  功能为:
  驱动加载后,会在sys/devices/platform/*/i2c-*/0-0038目录下产生ftstpfwver,ftsfwupdate,ftstprwreg,ftsfwupgradeapp,分别是查看FW版本,升级内核里的.i文件,读写寄存器,从TF卡升级app.bin文件。
示例如下:
1. cat ftstpfwver
2. echo 1 > ftsfwupdata
3. echo 88 > ftstprwreg 读0x88寄存器
   echo 8807 > ftstprwreg 向0x88寄存器中写入0x07
   注意:输入必须是2或4个,不够两位的用0填充。
4. echo "*_app.bin" > ftsfwupgradeapp