高通 kernel代码里如何添加setprop属性
时间: 2024-05-05 14:21:54 浏览: 9
在高通 kernel 代码中,可以使用 `sysfs` 接口或 `procfs` 接口添加 `setprop` 属性。 `setprop` 属性是 `init` 进程用来设置系统属性值的一种方式,可以通过 `getprop` 命令来获取这些属性值。
下面是使用 `sysfs` 接口添加 `setprop` 属性的示例代码:
1. 在设备树中添加属性节点,例如:
```
my_device {
compatible = "my_device";
my_prop {
my_prop_value = <0>;
};
};
```
2. 在驱动程序中,使用 `sysfs_create_group()` 函数创建一个属性组,并在属性组中添加自定义属性,在 `store` 函数中设置属性值,例如:
```c
static ssize_t my_prop_value_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
{
unsigned long value;
if (kstrtoul(buf, 10, &value))
return -EINVAL;
pr_info("Setting my_prop_value to %lu\n", value);
// Set the property value using set_property() function
set_property("my_prop_value", value);
return count;
}
static DEVICE_ATTR(my_prop_value, 0664, NULL, my_prop_value_store);
static struct attribute *my_device_attrs[] = {
&dev_attr_my_prop_value.attr,
NULL
};
static struct attribute_group my_device_attr_group = {
.attrs = my_device_attrs,
};
static const struct attribute_group *my_device_attr_groups[] = {
&my_device_attr_group,
NULL
};
static int my_device_probe(struct platform_device *pdev)
{
struct device_node *np = pdev->dev.of_node;
struct device *dev = &pdev->dev;
int ret;
// create sysfs nodes
ret = sysfs_create_groups(&dev->kobj, my_device_attr_groups);
if (ret)
return ret;
return 0;
}
static int my_device_remove(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
// remove sysfs nodes
sysfs_remove_groups(&dev->kobj, my_device_attr_groups);
return 0;
}
static struct of_device_id my_device_match_table[] = {
{ .compatible = "my_device", },
{},
};
MODULE_DEVICE_TABLE(of, my_device_match_table);
static struct platform_driver my_device_driver = {
.driver = {
.name = "my_device",
.owner = THIS_MODULE,
.of_match_table = my_device_match_table,
},
.probe = my_device_probe,
.remove = my_device_remove,
};
module_platform_driver(my_device_driver);
```
这样,当用户写入 `my_prop_value` 属性时,`store` 函数会被调用,并且传递给 `store` 函数的 `buf` 参数是用户写入的属性值,然后 `set_property()` 函数会被调用来设置 `setprop` 属性。
需要注意的是,`setprop` 属性是 `init` 进程使用的,所以必须在系统启动时设置这些属性。如果在驱动程序中设置 `setprop` 属性,只有当驱动程序加载时属性才会被设置,而无法在系统启动后动态设置属性。