网站添加icp备案号,做网站建设推荐,网站建站工作室,网站代码开发定制前言今天在一个群里面看到的一个朋友提交#xff0c;说of_property_read_string 这个函数有两个定义#xff0c;到底是用了哪个呢#xff1f;所以这篇文章就说下这个函数。函数引用的头文件引用的头文件位置在kernel-4.4includelinuxof.h其中一个是extern int of_property_r…前言今天在一个群里面看到的一个朋友提交说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节点对应的值。使用方式传入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;
};