下面是在Linux驱动中添加sys属性的简单介绍
方法一:在驱动中添加如下代码
static int Infrared_detect_info_open = 0;
直接调用module_param_named
module_param_named(infrared_info_switch, Infrared_detect_info_open, int, 0644);
infrared_info_switch为sys文件系统中属性的名字
方法二:在驱动中添加如下代码
static ssize_t infrare_distance_control_store(struct device *dev, struct device_attribute *attr, char *buf)
{
ssize_t ret = 0;
int buf_num;
buf_num=buf[0] - '0';
if(buf_num >= 1 && buf_num <= 9)
effective_int_num=buf_num;
ret = strlen(buf);
return ret;
}
static ssize_t infrare_distance_control_show(struct device *dev, struct device_attribute *attr, char *buf)
{
ssize_t ret = 0;
buf[0] = '0'+effective_int_num;
ret = strlen(buf);
return ret;
}
static DEVICE_ATTR(infrare_distance_control, S_IWUGO | S_IRUGO, infrare_distance_control_show, infrare_distance_control_store);
并在模块初始化函数处添加如下代码:
res = device_create_file(infrared_dev, &dev_attr_infrare_distance_control);
if (res) {
pr_err("%s: device_create_file failed\n", __FUNCTION__);
goto out;
}
阅读(1858) | 评论(0) | 转发(0) |