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 驱动时需要做的工作。

相关推荐
鹿鹿学长2 小时前
2025年全国大学生数学建模竞赛(C题) 建模解析|婴儿染色体数学建模|小鹿学长带队指引全代码文章与思路
c语言·开发语言·数学建模
伴杯猫2 小时前
【ESP32-IDF】基础外设开发2:系统中断矩阵
c语言·单片机·嵌入式硬件·mcu·物联网·github
Miracle&3 小时前
2.TCP深度解析:握手、挥手、状态机、流量与拥塞控制
linux·网络·tcp/ip
专注API从业者4 小时前
Python/Java 代码示例:手把手教程调用 1688 API 获取商品详情实时数据
java·linux·数据库·python
Ribou4 小时前
Ubuntu 24.04.2安装k8s 1.33.4 配置cilium
linux·ubuntu·kubernetes
Want5955 小时前
C/C++圣诞树①
c语言·开发语言·c++
tan180°5 小时前
Boost搜索引擎 网络库与前端(4)
linux·网络·c++·搜索引擎
DebugKitty5 小时前
硬件开发2-ARM基本概要
arm开发·mmu·soc·指令集·计算机系统·alu
Aczone285 小时前
硬件(五) 存储、ARM 架构与指令系统
arm开发·嵌入式硬件·架构
Mr. Cao code6 小时前
Docker:颠覆传统虚拟化的轻量级革命
linux·运维·ubuntu·docker·容器