Linux Device tree(三) - 获取DT信息

  • 获取内核运行时device tree.

代码如下:

/*
 * Module to inspect device tree from the kernel
 */

#define pr_fmt(fmt) "%s: " fmt, KBUILD_MODNAME
#include <linux/moduleparam.h>
#include <linux/module.h>
#include <linux/of.h>

#define PATH_DEFAULT	"/"
static char *path = PATH_DEFAULT;
module_param(path, charp, S_IRUSR | S_IWUSR);
MODULE_PARM_DESC(path, "a device tree pathname " \
			"(default is \"" PATH_DEFAULT "\")");

/*
 * Local functions
 */

static void print_property_u32(struct device_node *node, const char *name)
{
	u32 val32;
	if (of_property_read_u32(node, name, &val32) == 0)
		pr_info(" \%s = %d\n", name, val32);
}

static void print_property_string(struct device_node *node, const char *name)
{
	const char *str;
	if (of_property_read_string(node, name, &str) == 0)
		pr_info(" \%s = %s\n", name, str);
}

static void print_main_prop(struct device_node *node)
{
	pr_info("+ node = %s\n", node->full_name);
	print_property_u32(node, "#address-cells");
	print_property_u32(node, "#size-cells");
	print_property_u32(node, "reg");
	print_property_string(node, "name");
	print_property_string(node, "compatible");
	print_property_string(node, "status");
}

/*
 * Init & exit stuff
 */

static int __init get_dt_data_init(void)
{
	struct device_node *node, *child;
	struct property *prop;

	pr_info("path = \"%s\"\n", path);

	/* Find node by its pathname */
	node = of_find_node_by_path(path);
	if (!node) {
		pr_err("failed to find device-tree node \"%s\"\n", path);
		return -ENODEV;
	}
	pr_info("device-tree node found!\n");

	pr_info("now getting main properties...\n");
	print_main_prop(node);

	pr_info("now move through all properties...\n");
	for_each_property_of_node(node, prop)
		pr_info("-> %s\n", prop->name);

	/* Move through node's children... */
	pr_info("Now move through children...\n");
	for_each_child_of_node(node, child)
		print_main_prop(child);

	/* Force module unloading... */
	return -EINVAL;
}

static void __exit get_dt_data_exit(void)
{
	/* nop */
}

module_init(get_dt_data_init);
module_exit(get_dt_data_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Rodolfo Giometti");
MODULE_DESCRIPTION("Module to inspect device tree from the kernel");
MODULE_VERSION("0.1");

Makefile:

ifndef KERNEL_DIR
$(error KERNEL_DIR must be set in the command line)
endif
PWD := $(shell pwd)
ARCH ?= arm64
CROSS_COMPILE ?= aarch64-linux-gnu-

# This specifies the kernel module to be compiled
obj-m += get_dt_data.o

# The default action
all: modules

# The main tasks
modules clean:
	make -C $(KERNEL_DIR) \
            ARCH=$(ARCH) \
            CROSS_COMPILE=$(CROSS_COMPILE) \
            SUBDIRS=$(PWD) $@
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值