天天看點

c++ 傳回string_of_property_read_string 剖析

前言

今天在一個群裡面看到的一個朋友送出,說of_property_read_string 這個函數有兩個定義,到底是用了哪個呢?

是以這篇文章就說下這個函數。

函數引用的頭檔案

引用的頭檔案位置在

kernel-4.4includelinuxof.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節點對應的值。

使用方式:

c++ 傳回string_of_property_read_string 剖析

傳入np,就是裝置樹的節點,然後傳回 "clock-output-names" 字元串對應的值,存入clk_name 裡面。

of_property_read_string 函數剖析

c++ 傳回string_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;
};