platform总线

1、平台总线模型

平台总线模型是Linux系统虚拟出来的总线,而I2C、SPI等物理总线是真实存在的。
平台总线模型将一个驱动分成两个部分,分别是device.c和driver.c,分别用来描述硬件信息和控制硬件。
平台总线通过字符串比较,将name相同的device.c和driver.c匹配到一起来控制硬件。
在这里插入图片描述

平台总线模型的优点:

  • 减少编写重复代码,提高效率
  • 提高代码的利用率

2、platform device

//\Linux-4.9.88\include\linux\platform_device.h
struct platform_device {
	const char	*name;//设备名
	int		id; //设备ID号
	bool		id_auto;
	struct device	dev; //包含一个具体的device结构体
	u32		num_resources; //资源的数量
	struct resource	*resource; //用来保存硬件资源的结构体    io资源,中断资源,内存资源

	const struct platform_device_id	*id_entry; //平台设备的id
	char *driver_override; /* Driver name to force a match */

	/* MFD cell pointer */
	struct mfd_cell *mfd_cell; //用户多功能卡,多功能设备的实现

	/* arch specific additions */
	struct pdev_archdata	archdata;
};
extern int platform_device_register(struct platform_device *); //注册平台总线设备
extern void platform_device_unregister(struct platform_device *);//删除平台总线设备

2.1、struct device

struct device {
	struct device		*parent;

	struct device_private	*p;

	struct kobject kobj;
	const char		*init_name; /* initial name of the device */
	const struct device_type *type;

	struct mutex		mutex;	/* mutex to synchronize calls to
					 * its driver.
					 */

	struct bus_type	*bus;		/* type of bus device is on */
	struct device_driver *driver;	/* which driver has allocated this
					   device */
	void		*platform_data;	/* Platform specific data, device
					   core doesn't touch it */
	void		*driver_data;	/* Driver data, set and get with
					   dev_set/get_drvdata */
	struct dev_links_info	links;
	struct dev_pm_info	power;
	struct dev_pm_domain	*pm_domain;

#ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN
	struct irq_domain	*msi_domain;
#endif
#ifdef CONFIG_PINCTRL
	struct dev_pin_info	*pins;
#endif
#ifdef CONFIG_GENERIC_MSI_IRQ
	struct list_head	msi_list;
#endif

#ifdef CONFIG_NUMA
	int		numa_node;	/* NUMA node this device is close to */
#endif
	u64		*dma_mask;	/* dma mask (if dma'able device) */
	u64		coherent_dma_mask;/* Like dma_mask, but for
					     alloc_coherent mappings as
					     not all hardware supports
					     64 bit addresses for consistent
					     allocations such descriptors. */
	unsigned long	dma_pfn_offset;

	struct device_dma_parameters *dma_parms;

	struct list_head	dma_pools;	/* dma pools (if dma'ble) */

	struct dma_coherent_mem	*dma_mem; /* internal for coherent mem
					     override */
#ifdef CONFIG_DMA_CMA
	struct cma *cma_area;		/* contiguous memory area for dma
					   allocations */
#endif
	/* arch specific additions */
	struct dev_archdata	archdata;

	struct device_node	*of_node; /* associated device tree node */
	struct fwnode_handle	*fwnode; /* firmware device node */

	dev_t			devt;	/* dev_t, creates the sysfs "dev" */
	u32			id;	/* device instance */

	spinlock_t		devres_lock;
	struct list_head	devres_head;

	struct klist_node	knode_class;
	struct class		*class;
	const struct attribute_group **groups;	/* optional groups */

	void	(*release)(struct device *dev);
	struct iommu_group	*iommu_group;
	struct iommu_fwspec	*iommu_fwspec;

	bool			offline_disabled:1;
	bool			offline:1;
};

3、platform driver

这个结构体包含了平台驱动需要实现的相关函数操作

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; //内嵌了一个设备驱动结构体
	/*平台设备ID,这与platform_device中的struct platform_device_id *id_entry是相同的
     主要是完成总线的匹配操作,platform总线的匹配操作第一匹配要素就是该元素。而不再是简单的name选项*/
	const struct platform_device_id *id_table;
	bool prevent_deferred_probe;
};

3.1、 struct 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 */
	enum probe_type probe_type;

	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;
};

4、实验demo

4.1 platform_device.c

#include <linux/module.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/mutex.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/stat.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/tty.h>
#include <linux/kmod.h>
#include <linux/gfp.h>
#include <linux/platform_device.h>

//static struct resource  * res;

static void platform_dev_release(struct device *dev)
{
    printk("this is a test for platform_device\n");
} 

static struct resource platform_dev_resource[] = {

};

struct platform_device platform_dev_test = {
    .name = "100ask_test",
    .num_resources = ARRAY_SIZE(platform_dev_resource),
    .id = -1,
    .resource = platform_dev_resource,
    .dev = {
        .release = platform_dev_release,
    },
};

static int __init platform_dev_test_init(void)
{
    int err;
    
    err = platform_device_register(&platform_dev_test);   
    
    return 0;
}

static void __exit platform_dev_test_exit(void)
{
    platform_device_unregister(&platform_dev_test);

}

module_init(platform_dev_test_init);
module_exit(platform_dev_test_exit);

MODULE_LICENSE("GPL");

4.2 platform_driver.c

#include <linux/module.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/mutex.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/stat.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/tty.h>
#include <linux/kmod.h>
#include <linux/gfp.h>
#include <linux/platform_device.h>



static int platform_drv_probe(struct platform_device *pdev)
{
    printk("this is a test for platform_drv\n");
    return 0;
    
}

static int platform_drv_remove(struct platform_device *pdev)
{
    printk("this is in platform_drv_remove\n");
    return 0;
}


static struct platform_driver platform_drv_test = {
    .probe      = platform_drv_probe,
    .remove     = platform_drv_remove,
    .driver     = {
        .name   = "100ask_test",
    },
};

static int __init platform_drv_test_init(void)
{
    int err;
    
    err = platform_driver_register(&platform_drv_test); 
    return 0;
}

static void __exit platform_drv_test_exit(void)
{
    platform_driver_unregister(&platform_drv_test);
}

module_init(platform_drv_test_init);
module_exit(platform_drv_test_exit);

MODULE_LICENSE("GPL");


4.3 Makefile


# 1. 使用不同的开发板内核时, 一定要修改KERN_DIR
# 2. KERN_DIR中的内核要事先配置、编译, 为了能编译内核, 要先设置下列环境变量:
# 2.1 ARCH,          比如: export ARCH=arm64
# 2.2 CROSS_COMPILE, 比如: export CROSS_COMPILE=aarch64-linux-gnu-
# 2.3 PATH,          比如: export PATH=$PATH:/home/book/100ask_roc-rk3399-pc/ToolChain-6.3.1/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin 
# 注意: 不同的开发板不同的编译器上述3个环境变量不一定相同,
#       请参考各开发板的高级用户使用手册

KERN_DIR = /home/johan/share/linux_bsp/100ask_imx6ull-sdk/Linux-4.9.88

all:
	make -C $(KERN_DIR) M=`pwd` modules 
	

clean:
	make -C $(KERN_DIR) M=`pwd` modules clean


# 参考内核源码drivers/char/ipmi/Makefile
# 要想把a.c, b.c编译成ab.ko, 可以这样指定:
# ab-y := a.o b.o
# obj-m += ab.o

#编译成ko
platform_dev-y := platform_device.o
platform_drv-y := platform_driver.o
obj-m	+= platform_dev.o
obj-m	+= platform_drv.o

4.4 板子上测试
加载驱动
在这里插入图片描述
匹配成功,调用prode函数
在这里插入图片描述

5、总结

总的来说,主要是填充两个结构体,struct platform_device和struct platform_driver

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/368280.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

MySQL数据库①_MySQL入门(概念+使用)

目录 1. 数据库的概念 1.1 数据库的存储介质 1.2 主流数据库 2. MySQL的基本使用 2.1 链接数据库 2.2 服务器管理 2.3 数据库&#xff0c;服务器和表关系 2.4 简单MySQL语句 3. MySQL架构 4. SQL分类 5. 存储引擎 本篇完。 1. 数据库的概念 数据库是按照数据结构来…

蓝桥杯备战——12.PCF8591芯片的使用

目录 1.芯片简介2.读写时序3.控制字4.代码封装库5.原理图分析6.使用示例 1.芯片简介 截取自NXP的PCF8591芯片数据手册&#xff0c;我把重点关注部分划出来了&#xff0c;请务必自行阅读一遍数据手册&#xff01; 2.读写时序 ①器件地址&#xff1a; Bit0决定是读还是写操作&…

阳光倒灌光催化太阳光(太阳能)模拟器

太阳光&#xff08;太阳能&#xff09;模拟器是一种模拟太阳光照射的设备。由于太阳模拟器本身体积较小&#xff0c;测试过程不受环境、气候、时间等因素影响&#xff0c;从而避免了室外测量的各种因素限制&#xff0c;广泛应用于太阳能电池特性测试&#xff0c;光电材料特性测…

数学建模比赛期间,网上的各种思路靠谱吗

美赛第二天&#xff0c;很多人已经撑不住了。 从昨天起网上就那么多“思路”、“指导”、“代做”、“成品论文”&#xff0c;有免费的&#xff0c;也有收钱的。 有人说那些思路都是滥竽充数&#xff0c;纯坑钱浪费时间 有人说自己啥都不会&#xff0c;看那些思路就有启发 有…

一些你可能用到的函数和头文件

对于排序想必大家应该挺熟悉的&#xff0c;如果要是给一连串打乱的整数让你由小到大排序&#xff0c;常见的方法有冒泡排序法和选择排序法等&#xff0c;今天我就给大家介绍一个十分好用的方法&#xff0c;就是使用 sort 函数来进行快排。 sort 函数是位于头文件 #include <…

正点原子--STM32定时器学习笔记(2)

书接上文&#xff0c;本篇是对基本定时器实验部分进行的总结~ 实验目标&#xff1a;通过TIM6基本定时器定时500ms&#xff0c;让LED0每隔500ms闪烁。 解决思路&#xff1a;使用定时器6&#xff0c;实现500ms产生一次定时器更新中断&#xff0c;在中断里执行“翻转LED0”。 定时…

其他发现:开源数据可视化分析工具DataEase介绍文档

一、 简介 DataEase 是开源的数据可视化分析工具&#xff0c;帮助用户快速分析数据并洞察业务趋势&#xff0c;从而实现业务的改进与优化。DataEase 支持丰富的数据源连接&#xff0c;能够通过拖拉拽方式快速制作图表&#xff0c;并可以方便地与他人分享。 二、 优势 1、 开…

MySQL-----DML基础操作

DML语句 DML英文全称是Data Manipulation Language(数据操作语言)&#xff0c;用来对数据库中表的数据记录进行增删改操作。 ▶ 添加数据(INSERT) 【语法】 1. 给指定字段添加数据 INSERTO 表名 (字段名1&#xff0c;字段名2,...) VALUES (值1&#xff0c;值2,...); 2.给全…

npm install一直报错 failed, reason: certificate has expired

刚开始我以为是taobao镜像源的问题&#xff0c;所以我把npm的地址切换成了 https://resgistry.npmjs.org/ &#xff0c;发现还是不行。 问题解决&#xff1a; npm config set strict-ssl false 执行上面命令之后&#xff0c;npm install 成功

如何搭建私有云盘SeaFile并实现远程访问本地文件资料

&#x1f308;个人主页: Aileen_0v0 &#x1f525;热门专栏: 华为鸿蒙系统学习|计算机网络|数据结构与算法 ​&#x1f4ab;个人格言:“没有罗马,那就自己创造罗马~” #mermaid-svg-hsDnDEybLME85dTx {font-family:"trebuchet ms",verdana,arial,sans-serif;font-siz…

有限合伙协议书(模板)上

第一章 合伙的目的和合伙经营范围 第一条 合伙目的&#xff1a;为了适应市场经济的发展&#xff0c;满足市场需求&#xff0c;按照《合伙企业法》规范企业行为&#xff0c;合伙人本着公平、平等、互利的原则&#xff0c;成立 聚源企业管理中心&#xff08;有限合伙&am…

React 面试题

1、组件通信的方式 父组件传子组件&#xff1a;通过props 的方式 子组件传父组件&#xff1a;父组件将自身函数传入&#xff0c;子组件调用该函数&#xff0c;父组件在函数中拿到子组件传递的数据 兄弟组件通信&#xff1a;找到共同的父节点&#xff0c;用父节点转发进行通信 …

三层交换组网实验(思科)

一&#xff0c;技术简介 三层交换技术的出现&#xff0c;解决子网必须依赖路由器进行管理的问题&#xff0c;解决传统路由器低速、复杂所造成的网络瓶颈问题。一个具有三层交换功能的设备可简单理解为&#xff1a;一个带有第三层路由功能的第二层交换机。 二&#xff0c;实验目…

yum命令下载出现Failed to synchronize cache for repo ‘AppStream‘, ignoring this repo.

修改下面的配置文件 问题&#xff1a; cd /etc/yum.repos.d 修改下面四个文件 vim CentOS-Base.repo vim CentOS-AppStream.repo vim CentOS-Extras.repo vim CentOS-PowerTools.repo测试yum是否正常 yum -y install wget

备战蓝桥杯---搜索(DFS基础1)

何为深搜&#xff1f; 即不撞南墙不罢休。 话不多说&#xff0c;直接看题&#xff1a; 我们可以把这看成深搜的模板题&#xff0c;下面是AC代码&#xff1a; #include<bits/stdc.h> using namespace std; int a[15];//存值并输出 int vis[15]; int n18; void dfs(int …

【Git】04 .git目录

文章目录 一、.git目录二、切换分支三、仓库配置信息四、引用五、对象六、总结 一、.git目录 ls -a .git/./ COMMIT_EDITMSG description hooks/ info/ objects/ ../ config HEAD index logs/ refs/二、切换分支 cat .git/HEAD #…

2024年美国大学生数学建模比赛MCM问题B:搜索潜水器-思路解析与代码解答

2024 MCM Problem B Searching for Submersibles 一、题目翻译 背景&#xff1a; 总部位于希腊的小型海上巡航潜艇&#xff08;MCMS&#xff09;公司&#xff0c;制造能够将人类运送到海洋最深处的潜水器。潜水器被移动到该位置&#xff0c;并不受主船的束缚。MCMS现在希望用…

跟着pink老师前端入门教程-day17

2、CSS3 动画 动画&#xff08;animation&#xff09;是CSS3中就要有颠覆性的特征之一&#xff0c;可通过设置多个节点来精确控制一个或一组动画&#xff0c;常用来实现复杂的动画效果 相比较过渡&#xff0c;动画可以实现更多变化&#xff0c;更多控制&#xff0c;连续自动播…

HSRP配置指南

实验大纲 第 1 部分&#xff1a;验证连通性 步骤 1&#xff1a;追踪从 PC-A 到 Web 服务器的路径 步骤 2&#xff1a;追踪从 PC-B 到 Web 服务器的路径 步骤 3&#xff1a;观察当 R3 不可用时&#xff0c;网络的行为 第 2 部分&#xff1a;配置 HSRP 主用和 备用路由器 步…

WebGL+Three.js入门与实战——绘制水平移动的点、通过鼠标控制绘制(点击绘制、移动绘制、模拟画笔)

个人简介 &#x1f440;个人主页&#xff1a; 前端杂货铺 &#x1f64b;‍♂️学习方向&#xff1a; 主攻前端方向&#xff0c;正逐渐往全干发展 &#x1f4c3;个人状态&#xff1a; 研发工程师&#xff0c;现效力于中国工业软件事业 &#x1f680;人生格言&#xff1a; 积跬步…