在网上看了好多关于DEVICE_ATTR得介绍未能理清,还是自己实际动手操作下才有效果,最近在设计I2C测试过程中用到了这个,因此进行了实验。
实验环境:
操作系统ubuntu12.04
内容:i2c_core i2c_test
1.文件i2c_core.c
定义*i2c_dev_attrs 指针 包含 dev_attr_test.attr,
static struct attribute *i2c_dev_attrs[] = {
&dev_attr_name.attr,
&dev_attr_modalias.attr,
&dev_attr_debug.attr,
#ifdef CONFIG_I2C_STRESS_TEST
&dev_attr_test.attr,
#endif
NULL
};
同时定义i2c_dev_attr_group包含 i2c_dev_attrs
static struct attribute_group i2c_dev_attr_group = {
.attrs = i2c_dev_attrs,
};
继续定义i2c_dev_attr_groups包含 i2c_dev_attr_group
static const struct attribute_group *i2c_dev_attr_groups[] = {
&i2c_dev_attr_group,
NULL
};
最后 定义 device_type i2c_client_type 包含
static struct device_type i2c_client_type = {
.groups = i2c_dev_attr_groups,
.uevent = i2c_device_uevent,
.release = i2c_client_dev_release,
};
挂载client
struct i2c_client *
i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
{
....
client->dev.type = &i2c_client_type;
// 添加 i2c_client_type
.....
}
/* 定义响应函数show_test store_tes*/
static ssize_t show_test(struct device *dev, struct device_attribute *attr, char *buf)
{
printk("lion_li -->show_test \r\n");
// 调用响应得函数
return i2c_stresstest_print_status(buf);
}
static ssize_t store_test(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
{
printk("lion_li -->store_test \r\n");
// 调用响应得函数
return i2c_stresstest_command_parser(buf, count);
}
//测出进行注册 其中 test 未设置显示得内容
static DEVICE_ATTR(test, 0755 , show_test, store_test);
至此 test 为标志得cat参数被添加到了sys/device/i2c-n下
2、进入终端 ,输入 adb shell 进入adb模式
root@android:/sys/devices/i2c-0 #
切换到路径如下,输入 ls可以查看到 挂载得 name modalias debug testd等内容
此时 可以 输入 cat test
这是调用得将是 show_test 函数
测试如果输入 echo xxx >test 此时调用得是 store_test()函数 参数为xxx 字符串 同时有count 系统自己计算
3、完成