Linux下platform驱动简介

一. 简介

上一篇文章简单学习了Linux内核的 platform总线。文章地址如下:

Linux下platform总线-CSDN博客

在 SOC 中有些外设是没有总线这个概念的,但是又要使用总 线、驱动和设备模型该怎么办呢?
为了解决此问题, Linux 提出了 platform 这个虚拟总线,相应 的就有 platform_driver 和 platform_device 。

本文来简单学习一下 Linux内核中的 platform驱动。

二. Linux下platform驱动简介

1. platform_driver 结 构 体

platform_driver 结 构 体 表 示 platform 驱动,此结构体定义在文件 include/linux/platform_device.h 中,内容如下:

复制代码
struct platform_driver {
	int (*probe)(struct platform_device *);
	int (*remove)(struct platform_device *);
	void (*shutdown)(struct platform_device *);
	int (*suspend)(struct platform_device *, pm_message_t state);
	int (*resume)(struct platform_device *);
	struct device_driver driver;
	const struct platform_device_id *id_table;
	bool prevent_deferred_probe;
};

第 2 行,probe 函数,当驱动与设备匹配成功以后, probe 函数就会执行,非常重要的函数!!
一般驱动的提供者会编写,如果自己要编写一个全新的驱动,那么 probe 就需要自行实现。
第 7 行, driver 成员,为 device_driver 结构体变量, Linux 内核里面大量使用到了面向对象 的思维, device_driver 相当于基类,提供了最基础的驱动框架。 plaform_driver 继承了这个基类, 然后在此基础上又添加了一些特有的成员变量。

第 8 行, id_table 表,也就是我们上一篇文章(platform总线)讲解 platform 总线匹配驱动和设备的时候采用的 第三种方法。
id_table 是个表 ( 也就是数组 ) ,每个元素的类型为 platform_device_id , platform_device_id 结构体内容如下:

复制代码
struct platform_device_id {
	char name[PLATFORM_NAME_SIZE];
	kernel_ulong_t driver_data;
};

2. device_driver 结构体

device_driver 结构体定义在 include/linux/device.h , device_driver 结构体内容如下:

复制代码
struct device_driver {
	const char		*name;
	struct bus_type		*bus;

	struct module		*owner;
	const char		*mod_name;	/* used for built-in modules */

	bool suppress_bind_attrs;	/* disables bind/unbind via sysfs */

	const struct of_device_id	*of_match_table;
	const struct acpi_device_id	*acpi_match_table;

	int (*probe) (struct device *dev);
	int (*remove) (struct device *dev);
	void (*shutdown) (struct device *dev);
	int (*suspend) (struct device *dev, pm_message_t state);
	int (*resume) (struct device *dev);
	const struct attribute_group **groups;

	const struct dev_pm_ops *pm;

	struct driver_private *p;
};

上面 device_driver **结构体中,**第 10 行, of_match_table 就是采用设备树时,驱动使用的匹配表,同样是数组,每个匹 配项都为 of_device_id 结构体类型,此结构体定义在文件 include/linux/mod_devicetable.h 中,内 容如下:

复制代码
struct platform_device_id {
	char name[PLATFORM_NAME_SIZE];
	kernel_ulong_t driver_data;
};

struct of_device_id 结构体中,第 4 行的 compatible **非常重要,**因为对于设备树而言,就是通过设备节点的 compatible 属性值和 of_match_table 中每个项目的 compatible 成员变量进行比较,如果有相等,就表示设备和此驱动匹配成功。

关于Linux内核源码中的 platform驱动代码,简单学习到这里。

接下来学习一下,在编写 platform 驱动时需要做的工作。

相关推荐
阿部多瑞 ABU1 天前
《基于国产Linux的机房终端安全重构方案》
linux·安全
倔强的石头1061 天前
【Linux指南】Makefile入门:从概念到基础语法
linux·运维·服务器
ajassi20001 天前
linux C 语言开发 (七) 文件 IO 和标准 IO
linux·运维·服务器
程序猿编码1 天前
基于 Linux 内核模块的字符设备 FIFO 驱动设计与实现解析(C/C++代码实现)
linux·c语言·c++·内核模块·fifo·字符设备
一只游鱼1 天前
Zookeeper介绍与部署(Linux)
linux·运维·服务器·zookeeper
wheeldown1 天前
【Linux】 存储分级的秘密
linux·运维·服务器
天天进步20151 天前
掌握React状态管理:Redux Toolkit vs Zustand vs Context API
linux·运维·react.js
艾醒(AiXing-w)1 天前
探索大语言模型(LLM):Ollama快速安装部署及使用(含Linux环境下离线安装)
linux·人工智能·语言模型
mark-puls1 天前
C语言打印爱心
c语言·开发语言·算法
垚垚领先1 天前
Kdump 文档 - 基于 kexec 的崩溃转储解决方案
linux