百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

Linux内核必备知识点-platform总线详解

haoteby 2025-02-16 20:55 15 浏览

platform总线是学习linux驱动必须要掌握的一个知识点。

本文参考已发布:Linux 3.14内核

一、概念

嵌入式系统中有很多的物理总线:I2c、SPI、USB、uart、PCIE、APB、AHB

linux从2.6起就加入了一套新的驱动管理和注册的机制platform平台总线,是一条虚拟的总线,并不是一个物理的总线。

相比 PCI、USB,它主要用于描述SOC上的片上资源。platform 所描述的资源有一个共同点:在CPU 的总线上直接取址。

平台设备会分到一个名称(用在驱动绑定中)以及一系列诸如地址和中断请求号(IRQ)之类的资源。

设备用platform_device表示,驱动用platform_driver进行注册。

与传统的bus/device/driver机制相比,platform由内核进行统一管理,在驱动中使用资源,提高了代码的安全性和可移植性。

二、platform

1. platform总线两个最重要的结构体

platform维护的所有的驱动都必须要用该结构体定义:

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;
?const?struct?platform_device_id?*id_table;
?bool?prevent_deferred_probe;
};

该结构体,用于注册驱动到platform总线,


我们编写驱动的时候往往需要填充以上几个成员

platform_device

platform总线用于描述设备硬件信息的结构体,包括该硬件的所有资源(io,memory、中断、DMA等等)。

struct?platform_device?{
?const?char?*name;
?int??id;
?bool??id_auto;
?struct?device?dev;
?u32??num_resources;
?struct?resource?*resource;

?const?struct?platform_device_id?*id_entry;

?/*?MFD?cell?pointer?*/
?struct?mfd_cell?*mfd_cell;

?/*?arch?specific?additions?*/
?struct?pdev_archdata?archdata;
};



struct device dev->release()必须实现,

其中描述硬件信息的成员struct resource

0x139d0000

struct?resource?{
?resource_size_t?start;??//表示资源的起始值,???????????
?resource_size_t?end;????//表示资源的最后一个字节的地址,?如果是中断,end和satrt相同
?const?char?*name;???//?可不写??
?unsigned?long?flags;?//资源的类型
?struct?resource?*parent,?*sibling,?*child;
};
flags的类型说明

#define?IORESOURCE_MEM??0x00000200????//内存
#define?IORESOURCE_IRQ??0x00000400????//中断

内核管理的所有的驱动,都必须包含一个叫struct device_driver成员, 描述的硬件,必须包含struct device结构体成员。 /

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

其中:

const?char??*name;

用于和硬件进行匹配。

内核描述硬件,必须包含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?*/
?struct?dev_pm_info?power;
?struct?dev_pm_domain?*pm_domain;

#ifdef?CONFIG_PINCTRL
?struct?dev_pin_info?*pins;
#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.?*/

?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?acpi_dev_node?acpi_node;?/*?associated?ACPI?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;

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

其中:

void?(*release)(struct?device?*dev);

不能为空。

2. 如何注册

要用注册一个platform驱动的步骤

1)注册设备platform_device_register

/**
?*?platform_device_register?-?add?a?platform-level?device
?*?@pdev:?platform?device?we're?adding
?*/
int?platform_device_register(struct?platform_device?*pdev)
{
?device_initialize(&pdev->dev);
?arch_setup_pdev_archdata(pdev);
?return?platform_device_add(pdev);
}

2) 注册驱动platform_driver_register

#define?platform_driver_register(drv)?\
?__platform_driver_register(drv,?THIS_MODULE)

三、举例

1. 开发步骤

platform 总线下驱动的开发步骤是:

设备

需要实现的结构体是:platform_device 。

1)初始化 resource 结构变量

2)初始化 platform_device 结构变量

3)向系统注册设备:platform_device_register。

以上三步,必须在设备驱动加载前完成,即执行platform_driver_register()之前,原因是驱动注册时需要匹配内核中所有已注册的设备名。

platform_driver_register()中添加device到内核最终还是调用的device_add函数。

Platform_device_add和device_add最主要的区别是多了一步insert_resource(p, r),即将platform资源(resource)添加进内核,由内核统一管理。

驱动

驱动注册中,需要实现的结构体是:platform_driver 。

在驱动程序的初始化函数中,调用了platform_driver_register()注册 platform_driver 。

需要注意的是:platform_driver 和 platform_device 中的 name 变量的值必须是相同的【在不考虑设备树情况下,关于设备树,后面会写新的文章详细讲述】 。

这样在 platform_driver_register() 注册时,会将当前注册的 platform_driver 中的 name 变量的值和已注册的所有 platform_device 中的 name 变量的值进行比较,只有找到具有相同名称的 platform_device 才能注册成功。

当注册成功时,会调用 platform_driver 结构元素 probe 函数指针。

实例1

本例比较简单,只用于测试platform_driver 和platform_device是否可以匹配成功。

左边是platform_device结构体注册的代码,右边是platform_driver结构体注册的代码。

platform_driver 定义和注册:

?1?#include?
??2?#include?
??3?#include?
??4?#include?
??5?
??6?static?int?hello_probe(struct?platform_device?*pdev)
??7?{
??8?????printk("match?ok?\n");
??9?????return?0;
?10?}
?11?static??int?hello_remove(struct?platform_device?*pdev)
?12?{
?13?????printk("hello_remove?\n");
?14?????return?0;
?15?}
?16?static?struct?platform_driver?hello_driver?=
?17?{
?18?????.probe?=?hello_probe,
?19?????.driver.name?=?"duang",
?20?????.remove?=?hello_remove,?????
?21?};
?22?static?int?hello_init(void)
?23?{
?24?????printk("hello_init?\n");
?25?????return?platform_driver_register(&hello_driver);
?26?}
?27?static?void?hello_exit(void)
?28?{
?29?????printk("hello_exit?\n");
?30?????platform_driver_unregister(&hello_driver);
?31?????return;
?32?}
?33?MODULE_LICENSE("GPL");
?34?module_init(hello_init);
?35?module_exit(hello_exit);

platform_device定义和注册:

??1?#include?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
??2?#include?
??3?#include?
??4?#include?
??5?
??6?static?void?hello_release(struct?device?*dev)
??7?{
??8??????return;
??9?}
?10?static?struct?platform_device?hello_device?=
?11?{
?12?????.name?=?"duang",
?13?????.id?=?-1,
?14?????.dev.release?=?hello_release,
?15?};
?16?
?17?
?18?static?int?hello_init(void)
?19?{
?20?????printk("hello_init?\n");
?21?????return?platform_device_register(&hello_device);
?22?
?23?}
?24?static?void?hello_exit(void)
?25?{
?26?????printk("hello_exit?\n");
?27?????platform_device_unregister(&hello_device);
?28?????return;
?29?}
?30?MODULE_LICENSE("GPL");
?31?module_init(hello_init);
?32?module_exit(hello_exit);

该程序只用于测试platform框架是否可以成功匹配,struct platform_device hello_device 并没有设置任何硬件信息。

Makfile

??1?ifneq?($(KERNELRELEASE),)??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
??2?obj-m:=device.o?driver.o
??3?else
??4?KDIR?:=/lib/modules/$(shell?uname?-r)/build
??5?PWD??:=$(shell?pwd)
??6?all:
??7?????make?-C?$(KDIR)?M=$(PWD)?modules
??8?clean:
??9?????rm?-f?*.ko?*.o?*.mod.o?*.symvers?*.cmd??*.mod.c?*.order
?10?endif

该makefile可以同时将两个C文件编译成ko文件。

编译:


编译生成的文件:


加载模块

清空log信息
sudo?dmesg?-c


实例2

给结构体platform_device 增加硬件信息,并在内核中能够读取出来。本例向结构体hello_device 增加信息如下:

  1. 基址寄存器地址0x139d0000,该地址的空间是0x4
  2. 中断号199 【注意】 实际的内核中会把外设的中断号根据HW id(通常soc厂商设备soc的时候会给每一个中断源定义好唯一的ID)计算出一个新的中断号,该中断号会被cpu所识别。

device.c

struct?resource?res[]={
?[0]?={
??.start?=?0x139d0000,
??.end??=?0x139d0000?+?0x3,
??.flags?=?IORESOURCE_MEM,
?},

?[1]?={
??.start?=?199,
??.end??=?199,
??.flags?=?IORESOURCE_IRQ,
?},?
};
static?struct?platform_device?hello_device?=?
{
?.name?=?"duang",
?.id?=?-1,
?.dev.release?=?hello_release,?
?.num_resources?=?ARRAY_SIZE(res),
?.resource?=?res,
};

driver.c

static?int?hello_probe(struct?platform_device?*pdev)
{
?printk("match?ok?\n");

?printk("mem?=?%x?\n",pdev->resource[0].start);
?printk("irq?=?%d?\n",pdev->resource[1].start);

?//注册中断、申请内存
?return?0;
}

重新编译,卸载第一个例子的模块,并清除log:

make
sudo?rmmod?device?
sudo?rmmod?driver
sudo?dmesg?-c

执行

由结果可知,probe函数正确读取到了硬件信息。

四、platform_device是如何管理的?

1. 没有设备树

在没有设备树的时候,以三星Cortex-A8 s5pc100为例,硬件信息放在以下位置

arch\arm\mach-s5pc100\Mach-smdkc100.c
arch\arm\plat-samsung\



该数组存放了,内核启动需要初始化的硬件的信息。

2. 如果有设备树

内核会有设备初始化的完整代码,会在内核启动的时候把设备树信息解析初始化,把硬件信息初始化到对应的链表中。在总线匹配成功后,会把硬件的信息传递给probe()函数。

四、总线相关的其他的知识点

1. 内核总线相关结构体变量

内核维护的所有的总线都需要用以下结构体注册一个变量。

struct?bus_type?{
?const?char??*name;
?const?char??*dev_name;
?struct?device??*dev_root;
?struct?device_attribute?*dev_attrs;?/*?use?dev_groups?instead?*/
?const?struct?attribute_group?**bus_groups;
?const?struct?attribute_group?**dev_groups;
?const?struct?attribute_group?**drv_groups;

?int?(*match)(struct?device?*dev,?struct?device_driver?*drv);
?int?(*uevent)(struct?device?*dev,?struct?kobj_uevent_env?*env);
?int?(*probe)(struct?device?*dev);???
?int?(*remove)(struct?device?*dev);
?void?(*shutdown)(struct?device?*dev);

?int?(*online)(struct?device?*dev);
?int?(*offline)(struct?device?*dev);

?int?(*suspend)(struct?device?*dev,?pm_message_t?state);
?int?(*resume)(struct?device?*dev);

?const?struct?dev_pm_ops?*pm;

?struct?iommu_ops?*iommu_ops;

?struct?subsys_private?*p;
?struct?lock_class_key?lock_key;
};

platform总线变量的定义struct bus_type platform_bus_type定义如下:

struct?bus_type?platform_bus_type?=?{
?.name??=?"platform",
?.dev_groups?=?platform_dev_groups,
?.match??=?platform_match,
?.uevent??=?platform_uevent,
?.pm??=?&platform_dev_pm_ops,
};

其中最重要的成员是**.match**。

当有设备的硬件信息注册到platform_bus_type 总线的时候,会遍历所有platform总线维护的驱动, 通过名字来匹配,如果相同,就说明硬件信息和驱动匹配,就会调用驱动的platform_driver ->probe函数,初始化驱动的所有资源,让该驱动生效。

当有设备的驱动注册到platform_bus_type 总线的时候,会遍历所有platform总线维护的硬件信息, 通过名字来匹配,如果相同,就说明硬件信息和驱动匹配,就会调用驱动的platform_driver ->probe函数,初始化驱动的所有资源,让该驱动生效。

注册位置

drivers\base\Platform.c


五、注册代码流程详解

捋架构的好处,就是可以帮助我们定位问题

1. match函数何时被调用到?

2. probe函数何时被调用到

以下是上述两个问题代码的调用流程:


后面我们会再详细介绍设备树。

相关推荐

Python的RSA操作(私钥与公钥)(python rsa 公钥解密)

RSA是1977年由罗纳德·李维斯特(RonRivest)、阿迪·萨莫尔(AdiShamir)和伦纳德·阿德曼(LeonardAdleman)一起提出的。当时他们三人都在麻省理工学院工作。RSA...

RSA在日益互联的世界网络中安全性能如何?

KeyFactor公司(美国一家领先的安全数字身份管理解决方案提供商及网络安全行业权威机构)研究表明,许多物联网设备制造商正在生成不安全的RSA密钥,182个RSA证书里就有一个可能会被破解,由于不正...

让频谱分析更高效,澄清RSA使用中的一些误解

从事射频应用的研究人员、工程师和技术人员通常都能充分理解频谱分析仪的用途和优点,无论是传统的扫频分析仪(TSA)还是更现代的矢量信号分析仪(VSA)。他们熟练掌握这些重要射频仪器的关键规范和工作...

微软公告:Win10/Win11将不再支持短于2048位的RSA密钥证书

IT之家3月16日消息,微软近日发布公告,表示即将放弃短于2048位的RSA密钥证书。在公告中微软并未明确弃用时间,对于用户来说,这其实有利于构建更安全的上网环境。IT之家翻译微软公告...

目前已知的最强加密算法RSA(rsa加密算法的优点)

前面有人让我讲解一下RSA算法,今天我就用我所学的知识讲解一下,首先我们先了解一下RSARSA是一种非对称加密算法,1977年由罗纳德·李维斯特(RonRivest)、阿迪·萨莫尔(AdiSha...

韩国 CryptoLab 将在 2025年 RSA 大会发布加密人脸识别解决方案

据美通社4月23日报道,韩国同态加密网络安全企业CryptoLab宣布,将于4月24日在2025年RSA大会上,首次发布加密人脸识别(EFR)方案,为生物识别安全难题提供创新解法。当前,人脸识...

应对变化!盘点RSA2015十大热门产品

4月20日-24日,全球知名信息安全峰会RSAConference2015在美国旧金山召开。作为IT安全领域的权威科技大会,RSA大会不仅会邀请各地区著名安全专家出席与分享,更吸引汇集了全球众多顶...

RSA 2015主题:变化挑战当今的安全理念

1“变化”成为RSA2015主题4月20日-24日,全球知名信息安全峰会RSAConference2015在美国旧金山召开。作为IT安全领域的权威科技大会,RSA大会不仅会邀请各地区著名安全专家出...

非对称加密——一文看懂RSA(非对称加密详解)

非对称加密----RSA的使用"非对称加密也叫公钥密码:使用公钥加密,使用私钥解密"在对称密码中,由于加密和解密的密钥是相同的,因此必须向接收者配送密钥。用于解密的密钥必须被配送给...

RSA算法详解(rsa算法图解)

什么是RSA前面文章我们讲了AES算法,AES算法是一种是对称加密算法,本文我们来介绍一个十分常用的非对称加密算法RSA。非对称加密算法也叫公钥密码算法,通过生成的公私钥来对明文密文进行加密解密。R...

升级SSH后ssh-rsa失效?一文带你轻松解决!

背景今天刚给Linux桌面系统完成升级,结果SSH连接突然“罢工”了,还弹出了这个报错信息:...

历史回顾RSA大会:25年,十个瞬间(rsa conference)

国家安全局、Clipper芯片、苹果对决FBI、禁止ShowGirl——RSA大会都经历过。RSA需要你RSA这个词代表一家密码及安全厂商,也代表着世界上最大的网络安全展会,它今年在旧...

RSA 加密技术详解(rsa的加密原理是什么)

RSA的安全性基于数学难题的理论安全:RSA的安全性主要基于大质数分解和离散对数问题这两个数学难题。在RSA加密算法中,公钥包含一个大整数N,它是两个大质数p和q的乘积。攻击者如果想要破解RSA加密,...

「游戏开发」请别再说Unity不如Unreal:Unity室内场景 + 光照练习 3

关注“indienova”,挖掘独立游戏的更多乐趣引言上两节慢吞吞的补了很多技术实现的细节,感觉要是把用到的所有技术细节都过一遍可能还需要若干篇文章。所以决定先把整体的流程这篇好玩的写了,以后再慢慢补...

再做一个Android!Google发布第二代VR眼镜Cardboard

在去年的GoogleI/O上,Google向所有与会者发放了一款名为Cardboard的纸盒版虚拟现实眼镜,相比OculusRift等颇为酷炫的VR头盔,第一代Cardboard着实糙得很。不过,...