Linux Driver APIs - devicetree

Devicetree API 使用指南
Devicetree API for Device Driver
==========================================
#include <linux/of.h>

0.Index
=======
- 1.查找Devicetree节点
- 2.获取节点的属性和值
- 3.Sample Code

1.查找Devicetree节点
=================
1.1使用compatible属性寻找节点
-------------------------------------------
以@from节点为起点,用@compatible字符串匹配各节点compatible属性的值,返回
找到的devicetree节点。
/**
 * of_find_compatible_node - Find a node based on type and one of the
 *                                tokens in its "compatible" property
 * @from: The node to start searching from or NULL, the node
 * you pass will not be searched, only the next one
 * will; typically, you pass what the previous call
 * returned. of_node_put() will be called on it
 * @type: The type string to match "device_type" or NULL to ignore
 * @compatibleThe string to match to one of the tokens in the device
 * "compatible" list.
 *
 * Returns a node pointer with refcount incremented, use
 * of_node_put() on it when done.
 */
struct device_node *of_find_compatible_node(struct device_node *from,
		const char *type, const char *compatible)

Demo:
struct device_node *of_node;
/* Searching from root node */
of_node = of_find_compatible_node(NULL, NULL, "vendor,chip");

/* Searching child node from an existing node */
struct device_node *parent = of_node; /* node that got from other code*/
of_node = of_find_compatible_node(parent, NULL, "vendor,chip");


1.2使用节点名称寻找

----------------------------

/**
 * of_find_node_by_name - Find a node by its "name" property
 * @from: The node to start searching from or NULL, the node
 * you pass will not be searched, only the next one
 * will; typically, you pass what the previous call
 * returned. of_node_put() will be called on it
 * @name: The name string to match against
 *
 * Returns a node pointer with refcount incremented, use
 * of_node_put() on it when done.
 */
struct device_node *of_find_node_by_name(struct device_node *from,
		const char *name)

Demo:
&i2c1 {
touchpanel {
property-a = <1>;
property-b = "string a";
};
};

struct device_node *of_node;
of_node = of_find_node_by_name(NULL, "touchpanel");

1.3使用phandle寻找节点
--------------------------------
/**
 * of_find_node_by_phandle - Find a node given a phandle
 * @handle: phandle of the node to find
 *
 * Returns a node pointer with refcount incremented, use
 * of_node_put() on it when done.
 */
struct device_node *of_find_node_by_phandle(phandle handle)

1.4通过属性名称来寻找节点

-------------------------------------

/**
 * of_find_node_with_property - Find a node which has a property with
 *                                   the given name.
 * @from: The node to start searching from or NULL, the node
 * you pass will not be searched, only the next one
 * will; typically, you pass what the previous call
 * returned. of_node_put() will be called on it
 * @prop_name:The name of the property to look for.
 *
 * Returns a node pointer with refcount incremented, use
 * of_node_put() on it when done.
 */
struct device_node *of_find_node_with_property(struct device_node *from,
		const char *prop_name)


2.获取节点的属性的值
====================
2.1读取整形数据
---------------------
int of_property_read_u8(const struct device_node *np,
		const char *propname, u8 *out_value)

int of_property_read_u16(const struct device_node *np,
		const char *propname, u16 *out_value)

int of_property_read_u32(const struct device_node *np,
		const char *propname, u32 *out_value)

int of_property_read_u64(const struct device_node *np,
		const char *propname, u64 *out_value);

2.2读取整型数组
---------------------
int of_property_read_u8_array(const struct device_node *np,
		const char *propname, u8 *out_values, size_t sz)


int of_property_read_u16_array(const struct device_node *np,
		const char *propname, u16 *out_values, size_t sz)
	

int of_property_read_u32_array(const struct device_node *np,
		const char *propname, u32 *out_values, size_t sz)

2.3读取整型数组中某个索引位置的数据
----------------------------------------------------
static inline int of_property_read_u32_index(const struct device_node *np,
		const char *propname, u32 index, u32 *out_value)

2.4读取字符串数据
-------------------------
int of_property_read_string(struct device_node *np,
		const char *propname, const char **out_string);


int of_property_read_string_index(struct device_node *np,
		const char *propname, int index, const char **output);


int of_property_match_string(struct device_node *np,
		const char *propname, const char *string);


int of_property_count_strings(struct device_node *np,
		const char *propname);

3.Sample Code
============
#include <linux/of.h>
static int samp_parse_dts(struct device *dev)
{
	struct device_node *of_node = dev->of_node;
	const char *compatible = "vendor,x";
	u32 val0;
	u8 val1;
	const char *string;

	if (!of_node)
		return -EINVAL;

	if (!of_device_is_compatible(of_node, compatible))
		return -ENODEV;

	if (of_property_read_u32(of_node, "vendor,val0", &val0))
		/* fail */
		val0 = -1;

	if (of_property_read_u8(of_node, "vendor,val0", &val1))
		/* fail */
		val1 = -1;

	if (of_property_read_string(of_node, "vendor,str", &string)
		string = NULL;
	return 0;
}


### Linux TEE Driver Configuration Settings and Options In the context of configuring the Linux TEE (Trusted Execution Environment) driver, several key aspects need consideration. The TEE driver facilitates secure execution environments on devices by interfacing between user space applications and trusted applications running within a secure environment. #### Configuring the TEE Driver via Kernel Parameters To enable or configure specific features of the TEE subsystem during boot time, one can pass parameters directly through the kernel command line: - `tee`: Enables support for Trusted Execution Environments. - `optee.max_num_contexts=<number>`: Sets maximum contexts allowed per session[^1]. These configurations ensure that the system initializes correctly with desired capabilities enabled from startup. #### Using Device Tree Nodes for Hardware-Specific Setup For platforms where hardware-specific initialization is required, device tree nodes provide an effective method to define properties relevant to the TEE interface. Common entries include defining memory regions reserved exclusively for use by the TEE OS as well as specifying interrupt lines used for communication purposes. Example snippet showing how these might appear in a DTS file: ```dts tee { compatible = "linaro,optee"; status = "okay"; /* Define shared memory region */ shmem@0 { reg = <0x7e800000 0x2000>; }; }; ``` This setup ensures proper allocation of resources necessary for optimal operation when interacting with the TEE component. #### Leveraging Userspace Tools like OP-TEE Client API Libraries Beyond low-level configuration at build-time or boot-time, developers often interact programmatically using libraries provided alongside implementations such as OP-TEE. These APIs allow dynamic management of sessions, invocation of commands against loaded TA's (Trusted Applications), passing data securely across boundaries without exposing sensitive information outside protected areas. An example demonstrating opening a connection would look something like this: ```c #include <tee_client_api.h> TEEC_Result result; TEEC_Context ctx; result = TEEC_InitializeContext(NULL, &ctx); if(result != TEEC_SUCCESS){ printf("Failed initializing context\n"); } // Further operations... ``` Such approaches abstract much complexity away while still offering full control over interactions needed for developing robust solutions around security-sensitive functionalities offered through modern SoCs supporting ARM TrustZone technology among others. --related questions-- 1. How do you integrate custom drivers into existing Linux distributions? 2. What are some common challenges faced when debugging issues related to TEE interfaces? 3. Can you explain more about setting up secure world communications under ARM architecture? 4. Are there any best practices recommended for maintaining privacy concerns when working inside isolated execution zones?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值