最近在MTK 的android L版本上写了一个TP驱动创建设备属性节点
/*访问设置属性方法*/
static ssize_t tpd_val_show(struct device* dev, struct device_attribute* attr, char* buf);
static ssize_t tpd_val_store(struct device* dev, struct device_attribute* attr, const char* buf, size_t count);
/*定义设备属性*/
static DEVICE_ATTR(val, 0666, tpd_val_show, tpd_val_store);
......
ret = device_create_file(temp, &dev_attr_val);
if(ret < 0) {
printk(KERN_ALERT"Failed to create attribute val.\n");
goto destroy_device;
}
创建设备属性的其他代码可以参照转载的老罗驱动代码。
创建后发现没有访问权限,并且在device/mediatek/common/sepolicy下面增加了访问访问权限还是不行,代码如下:
file.te
type mytp_file, file_type, sysfs_type;
file_contexts
/sys/class/mytp/mytp/val u:object_r:yy5203a_file:s0
platform_app.te
allow platform_app mytp_file:file rw_file_perms;
后来听以前的同事说,节点属性是root组权限,在L版本以后,APK不能访问,需改为system权限,于是就在device/XXX/project/init.project.rc加上修改权限的代码
chown system system /sys/class/mytp/mytp/val
chmod 0666 /sys/class/mytp/mytp/val
编译后,烧录ROM发现还是没有权限OPEN /sys/class/mytp/mytp/val,后来在网上查了资料后是说因为是sysfs系统文件,需修改external/sepolic/app.te,放开dir_file_class_set权限:
将
neverallow { appdomain -bluetooth -nfc }
sysfs:dir_file_class_set write;
改为:
neverallow { appdomain -bluetooth -nfc -platform_app }
sysfs:dir_file_class_set write;
如果还不行,请在platform.te里增加一条
allow platform_app sysfs:file rw_file_perms;
编译烧录后,可以访问这属性了,一个这东东折腾了一下午。。。