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

相关推荐
fasewer5 分钟前
第五章 linux实战-挖矿 二
linux·运维·服务器
楚灵魈31 分钟前
[Linux]从零开始的网站搭建教程
linux·运维·服务器
小小不董33 分钟前
《Linux从小白到高手》理论篇:深入理解Linux的网络管理
linux·运维·服务器·数据库·php·dba
这可就有点麻烦了1 小时前
强化学习笔记之【TD3算法】
linux·笔记·算法·机器学习
DY009J1 小时前
深度探索Kali Linux的精髓与实践应用
linux·运维·服务器
程序员-珍2 小时前
虚拟机ip突然看不了了
linux·网络·网络协议·tcp/ip·centos
码农小白2 小时前
linux驱动:(22)中断节点和中断函数
linux·运维·服务器
4647的码农历程2 小时前
Linux网络编程 -- 网络基础
linux·运维·网络
aaasssdddd963 小时前
python和c
c语言·开发语言·python
C++忠实粉丝4 小时前
Linux环境基础开发工具使用(2)
linux·运维·服务器