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

相关推荐
heisd_121 小时前
使用搭载Ubuntu的树莓派开启热点
linux·运维·ubuntu
阿干tkl21 小时前
优麒麟(Ubuntu Kylin) 安装向日葵远程工具/ToDesk
linux·ubuntu·kylin
Goafan1 天前
ubuntu20.04插入USB耳机无法识别
linux
草莓工作室1 天前
数据结构8:栈
c语言·数据结构
口嗨农民工1 天前
SVN冲突处理相关,标识 C 语言源文件(.c)的不同版本或冲突状态
c语言·svn
Wins_calculator1 天前
Fixed VLC snap on Ubuntu
linux·ubuntu·vlc·snap
偶尔贪玩的骑士1 天前
Kioptrix Level 1渗透测试
linux·开发语言·网络安全·php
楠木s1 天前
ctfshow pwn44
linux·服务器·网络·安全·网络攻击模型·二进制
瑞禧生物ruixibio1 天前
4-ARM-PEG-Pyrene(2)/Biotin(2),多功能化聚乙二醇修饰荧光标记生物分子的设计与应用探索
arm开发·人工智能
ajassi20001 天前
开源 Linux 服务器与中间件(七)数据库--MySQL
linux·服务器·数据库·ubuntu·开源