of_property_read_string 剖析~

前言

今天在一个群里面看到的一个朋友提交,说of_property_read_string 这个函数有两个定义,到底是用了哪个呢?

所以这篇文章就说下这个函数。

函数引用的头文件

引用的头文件位置在

\kernel-4.4\include\linux\of.h

其中一个是

extern int of_property_read_string(struct device_node *np,
				   const char *propname,
				   const char **out_string);

还有一个是

static inline int of_property_read_string(struct device_node *np,
					  const char *propname,
					  const char **out_string)
{
	return -ENOSYS;
}

但是并不是两个都用到,他们用了一个宏  CONFIG_OF 来选择

CONFIG_OF 宏有什么用?

这个宏的解释是

Open Firmware. This was invented long time ago when Apple was producing laptops based on PowerPC CPUs. Openfirmware provides a good description of the devices connected to the platform. In Linux kernel the part that works with device data is called Device Tree (DT). More details in theUsage model.

他的作用是

Openfirmware provides a good description of the devices connected to the platform

他提供了一种更好的方式来连接设备和驱动。

他是名字是

called Device Tree (DT)

DTS,那很明显了,开了这个宏,就表示使用了DTS设备树的方式来连接设备和驱动程序。

of_property_read_string 函数本体

函数位置

"./drivers/of/base.c"

函数原型

/**
 * of_property_read_string - Find and read a string from a property
 * @np:         device node from which the property value is to be read.
 * @propname:   name of the property to be searched.
 * @out_string: pointer to null terminated return string, modified only if
 *              return value is 0.
 *
 * Search for a property in a device tree node and retrieve a null
 * terminated string value (pointer to data, not a copy). Returns 0 on
 * success, -EINVAL if the property does not exist, -ENODATA if property
 * does not have a value, and -EILSEQ if the string is not null-terminated
 * within the length of the property data.
 *
 * The out_string pointer is modified only if a valid string can be decoded.
 */
int of_property_read_string(struct device_node *np, const char *propname,
                                const char **out_string)
{
        struct property *prop = of_find_property(np, propname, NULL);
        if (!prop)
                return -EINVAL;
        if (!prop->value)
                return -ENODATA;
        if (strnlen(prop->value, prop->length) >= prop->length)
                return -EILSEQ;
        *out_string = prop->value;
        return 0;
}
EXPORT_SYMBOL_GPL(of_property_read_string);

函数的作用:

返回propname对应dts节点对应的值。

使用方式:

传入np,就是设备树的节点,然后返回 "clock-output-names" 字符串对应的值,存入clk_name 里面。

of_property_read_string 函数剖析

int of_property_read_string(struct device_node *np, const char *propname,
                                const char **out_string)
{
        struct property *prop = of_find_property(np, propname, NULL);
        if (!prop)
                return -EINVAL;
        if (!prop->value)
                return -ENODATA;
        if (strnlen(prop->value, prop->length) >= prop->length)
                return -EILSEQ;
        *out_string = prop->value;
        return 0;
}
EXPORT_SYMBOL_GPL(of_property_read_string);
  • of_find_property 这个是找到这个dts节点,怎么找,可以再去这个函数分析一下。

  • strnlen功能「获取字符串实际字符个数,不包括结尾的'\0';如果实际个数 <= 第二个参数,则返回字符串实际字符个数,否则返回第二个参数。」

  • prop->length 是之前预设的一个值,strnlen正常情况返回的就是字符串的长度 减1「去掉\n字符」。

  • *out_string = prop->value 这里就是二级指针起到作用了,没有重新分配内存,直接把指针指向字符串位置。

我们再看看prop 的结构体,就一目了然了。

struct property {
	char	*name;
	int	length;
	void	*value;
	struct property *next;
	unsigned long _flags;
	unsigned int unique_id;
	struct bin_attribute attr;
};

关于二级指针举个例子

#include "stdio.h"

char *str = "helloworld";
char **p = NULL;
int main(void)
{
	p = &str;
	printf("%s\n",*p);
	return (0);
}
推荐阅读
我把我的所有文章都汇总了,在公众号菜单也有。
专辑|Linux文章汇总
专辑|程序人生

===========

  

PS想加入技术群的同学,加了我好友后,就给我发「篮球的大肚子」这句话,有可能机器人打瞌睡,可以多发几次,不要发与技术无关的消息或者推广。

如果想获取学习资料,就在公众号后台回复「1024」,足够多的学习资料可以让你学习。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值