前言

今天在一个群里面看到的一个朋友提交,说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」,足够多的学习资料可以让你学习。

of_property_read_string 剖析~相关推荐

  1. read函数头文件 window_of_property_read_string 剖析

    前言 今天在一个群里面看到的一个朋友提交,说of_property_read_string 这个函数有两个定义,到底是用了哪个呢? 所以这篇文章就说下这个函数. 函数引用的头文件 引用的头文件位置在 ...

  2. volatile关键字之全面深度剖析

    引言 volatile这个关键字可能很多朋友都听说过,或许也都用过.在Java 5之前,它是一个备受争议的关键字,因为在程序中使用它往往会导致出人意料的结果.在Java 5之后,volatile关键字 ...

  3. TensorFlow基础剖析

    TensorFlow基础剖析 一.概述 TensorFlow 是一个使用数据流图 (Dataflow Graph) 表达数值计算的开源软件库.它使用节点表示抽象的数学计算,并使用 OP 表达计算的逻辑 ...

  4. c语言赋值x为字母,C语言算术、赋值、关系、逻辑运算详细剖析---

    标识符和关键字 ¨标识符:用来标识程序中的变量.符号常量.函数.数组.类型.文件等对象的名字.标识符只能由字母.数字和下划线组成,且第一个字符必需为字母或下划线.C语言中大小写字母是两个不同的字符. ...

  5. 如何在HHDI中进行数据质量探查并获取数据剖析报告

    通过执行多种数据剖析规则,对目标表(或一段SQL语句)进行数据质量探查,从而得到其数据质量情况.目前支持以下几种数据剖析类型,分别是:数字值分析.值匹配检查.字符值分析.日期值分析.布尔值分析.重复值 ...

  6. 老李推荐:第14章4节《MonkeyRunner源码剖析》 HierarchyViewer实现原理-装备ViewServer-端口转发 1...

    老李推荐:第14章4节<MonkeyRunner源码剖析> HierarchyViewer实现原理-装备ViewServer-端口转发 在初始化HierarchyViewer的实例过程中, ...

  7. JS魔法堂:mmDeferred源码剖析

    一.前言 avalon.js的影响力愈发强劲,而作为子模块之一的mmDeferred必然成为异步调用模式学习之旅的又一站呢!本文将记录我对mmDeferred的认识,若有纰漏请各位指正,谢谢.项目请见 ...

  8. Linux 文件系统剖析

    Linux 文件系统剖析 按照分层结构讨论 Linux 文件系统 M. Tim Jones, 顾问工程师, Emulex Corp. 简介: 在文件系统方面,Linux® 可以算得上操作系统中的 &q ...

  9. 剖析PHP中的输出缓冲

    剖析PHP中的输出缓冲 本文按署名·非商业用途·保持一致授权 作者:  ,发表于2005年12月24日01时54分 我们先来看一段代码. <?php for ($i=10; $i>0; $ ...

最新文章

  1. 【longPressKey】长按键盘任意键(或组合键)3秒触发自定义事件(以Pause/Break键为例)
  2. java jsp 脚本 声明 表达式 简介
  3. tensorflow从入门到精通100讲(一)-如何申明一个tensor,Tensorflow中最重要的基本操作
  4. 电脑组装与维护教程_小白不会装机?教你如何自己组装一台电脑。装机图文教程...
  5. Ubuntu 12.04安装英汉词典
  6. 斯坦福大学Andrew Ng - 机器学习笔记(8) -- 推荐系统 大规模机器学习 图片文字识别...
  7. 如何提高gps精度_如何在锻炼应用程序中提高GPS跟踪精度
  8. C++中全局变量的使用
  9. wpf中UserControl制作
  10. 苹果备忘录怎么调字体大小_苹果手机的备忘录怎么恢复?不知道的快来看看
  11. 单片机实验中用到的元器件(Multisim14)
  12. 植物大战僵尸无尽模式最强阵容可以无限打
  13. 查看本机IP地址、测试本机与其他设备是否连通、查本机IP归属地
  14. 企业网站排名,关键词选择原则,6个基本策略
  15. 有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。(java)
  16. JAVA生成带LOGO的二维码
  17. DBT-50000及相关问题处理
  18. 8虚拟内存9页面置换算法
  19. 安卓获取手机视频和图片
  20. 戴蒙德对新冠疫情等的分析与看法笔记

热门文章

  1. unix-ln 命令
  2. PHP多种形式发送邮件
  3. 职场有影帝出没,屌丝们请当心!
  4. android DatePicker
  5. 20110126 学习记录:一些关于html中布局的代码 CSS hack速查表
  6. golang学习之旅(1)
  7. 基于websocket的聊天实现逻辑(springboot)
  8. android打印intent flag,Android flag详解
  9. sql修改链接服务器名称,SQL Server 创建链接服务器的脚本,自定义链路服务器的简短名称...
  10. 文本分析软件_十大针对机器学习的文本注释工具与服务,你选哪个?