platform_driver(Linux内核必备知识点)

/ 0评 / 0

platform_driver(Linux内核必备知识点)

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

structplatform_driver{int(*probe)(structplatform_device*);//int(*remove)(structplatform_device*);void(*shutdown)(structplatform_de百思特网vice*);int(*suspend)(structplatform_device*,pm_message_tstate);int(*resume)(structplatform_device*);structdevice_driverdriver;conststructplatform_device_id*id_table;boolprevent_deferred_probe;};

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

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

platform_device

platform总线用于描写装备硬件信息的构造体,包含该硬件的所有资源(io,memory、中止、DMA等等)。

structplatform_device{constchar*name;intid;boolid_auto;structdevicedev;u32num_resources;structresource*resource;conststructplatform_device_id*id_entry;/*MFDcellpointer*/structmfd_cell*mfd_cell;/*archspecificadditions*/structpdev_archdataarchdata;};

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

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

0x139d0000

structresource{resource_size_tstart;//表现资源的起始值,resource_size_tend;//表现资源的最后一个字节的地址,如果是中止,end和satrt雷同constchar*name;//可不写unsignedlongflags;//资源的类型structresource*parent,*sibling,*child;};flags的类型解释#defineIORESOURCE_MEM0x00000200//内存#defineIORESOURCE_IRQ0x00000400//中止

内核管理的所有的驱动,都必需包括一个叫struct device_driver成员, 描写的硬件,必需包括struct device构造体成员。 /

structdevice_driver{constchar*name;structbus_type*bus;structmodule*owner;constchar*mod_name;/*usedforbuilt-inmodules*/boolsuppress_bind_attrs;/*disablesbind/unbindviasysfs*/conststructof_device_id*of_match_table;conststructacpi_device_id*acpi_match_table;int(*probe)(structdevice*dev);int(*remove)(structdevice*dev);void(*shutdown)(structdevice*dev);int(*suspend)(structdevice*dev,pm_message_tstate);int(*resume)(structdevice*dev);conststructattribute_group**groups;conststructdev_pm_ops*pm;structdriver_private*p;};

其中:

constchar*name;

用于和硬件进行匹配。

内核描写硬件,必需包括struct device构造体成员:

structdevice{structdevice*parent;structdevice_private*p;structkobjectkobj;constchar*init_name;/*initialnameofthedevice*/conststructdevice_type*type;structmutexmutex;/*mutextosynchronizecallsto*itsdriver.*/structbus_type*bus;/*typeofbusdeviceison*/structdevice_driver*driver;/*whichdriverhasallocatedthisdevice*/void*platform_data;/*Platformspecificdata,devicecoredoesn'ttouchit*/structdev_pm_infopower;structdev_pm_domain*pm_domain;#ifdefCONFIG_PINCTRLstructdev_pin_info*pins;#endif#ifdefCONFIG_NUMAintnuma_node;/*NUMAnodethisdeviceiscloseto*/#endifu64*dma_mask;/*dmamask(ifdma'abledevice)*/u64coherent_dma_mask;/*Likedma_mask,butforalloc_coherentmappingsasnotallhardwaresupports64bitaddressesforconsistentallocationssuchdescriptors.*/structdevice_dma_parameters*dma_parms;structlist_headdma_pools;/*dmapools(ifdma'ble)*/structdma_coherent_mem*dma_mem;/*internalforcoherentmemoverride*/#ifdefCONFIG_DMA_CMAstructcma*cma_area;/*contiguousmemoryareafordmaallocations*/#endif/*archspecificadditions*/structdev_archdataarchdata;structdevice_node*of_node;/*associateddevicetreenode*/structacpi_dev_nodeacpi_node;/*associatedACPIdevicenode*/dev_tdevt;/*dev_t,createsthesysfs"dev"*/u32id;/*deviceinstance*/spinlock_tdevres_lock;structlist_headdevres_head;structklist_nodeknode_class;structclass*class;conststructattribute_group**groups;/*optionalgroups*/void(*release)(structdevice*dev);structiommu_group*iommu_group;booloffline_disabled:1;booloffline:1;};

其中:

void(*release)(structdevice*dev);

不能为空。

2. 如何注册

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

1)注册装备platform_device_register

/***platform_device_register-addaplatform-leveldevice*@pdev:platformdevicewe'readding*/intplatform_device_register(structplatform_device*pdev){device_initialize(&pdev->dev);arch_setup_pdev_archdata(pdev);returnplatform_device_add(pdev);}

2) 注册驱动platform_driver_register

#defineplatform_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)添加进内核,由内核统一管理。

驱动

驱动注册中,须要实现的构造体是:pla(www.isoyu.com原创版权)tform_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

本例比拟简略,只用于测试p百思特网latform_driver 和platform_device是否可以匹配胜利。

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

platform_driver 定义和注册:

1#include<linux/init.h>2#include<linux/module.h>3#include<linux/platform_device.h>4#include<linux/ioport.h>56staticinthello_probe(structplatform_device*pdev)7{8printk("matchok\n");9return0;10}11staticinthello_remove(structplatform_device*pdev)12{13printk("hello_remove\n");14return0;15}16staticstructplatform_driverhello_driver=17{18.probe=hello_probe,19.driver.name="duang",20.remove=hello_remove,21};22staticinthello_init(void)23{24printk("hello_init\n");25returnplatform_driver_register(&hello_driver);26}27staticvoidhello_exit(void)28{29printk("hello_exit\n");30platform_driver_unregister(&hello_driver);31return;32}33MODULE_LICENSE("GPL");34module_init(hello_init);35module_exit(hello_exit);

platform_device定义和注册:

1#include<linux/init.h>2#include<linux/module.h>3#include<linux/platform_device.h>4#include<linux/ioport.h>56staticvoidhello_release(structdevice*dev)7{8return;9}10staticstructplatform_devicehello_device=11{12.name="duang",13.id=-1,14.dev.release=hello_release,15};161718staticinthello_init(void)19{20printk("hello_init\n");21returnplatform_device_register(&hello_device);2223}24staticvoidhello_exit(void)25{26printk("hello_exit\n");27platform_device_unregister百思特网(&hello_device);28return;29}30MODULE_LICENSE("GPL");31module_init(hello_init);32module_exit(hello_exit);

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

Makfile

1ifneq($(KERNELRELEASE),)2obj-m:=device.odriver.o3else4KDIR:=/lib/modules/$(shelluname-r)/build5PWD:=$(shellpwd)6all:7make-C$(KDIR)M=$(PWD)modules8clean:9rm-f*.ko*.o*.mod.o*.symvers*.cmd*.mod.c*.order10endif

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

编译:

编译

编译生成的文件:

加载模块

清空log信息sudodmesg-c

匹配胜利

实例2

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

  1. 基址存放器地址0x139d0000,该地址的空间是0x4

  2. 中止号199 【注意】 实际的内核中会把外设的中止号依据HW id(通常soc厂商装备soc的时候会给每一个中止源定义好唯一的ID)盘算出一个新的中止号,该中止号会被cpu所辨认。

device.c

structresourceres[]={[0]={.start=0x139d0000,.end=0x139d0000+0x3,.flags=IORESOURCE_MEM,},[1]={.start=199,.end=199,.flags=IORESOURCE_IRQ,},};staticstructplatform_devicehello_device={.name="duang",.id=-1,.dev.release=hello_release,.num_resources=ARRAY_SIZE(res),.resource=res,};

driver.c

staticinthello_probe(structplatform_device*pdev){printk("matchok\n");printk("mem=%x\n",pdev->resource[0].start);printk("irq=%d\n",pdev->resource[1].start);//注册中止、申请内存return0;}

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

makesudormmoddevicesudormmoddriversudodmesg-c

履行

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

四、platform_device是如何管理的?

1. 没有装备树

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

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

注册platform_device

platform_device定义

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

2. 如果有装备树

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

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

1. 内核总线相干构造体变量

内核保护的所有的总线都须要用以下构造体注册一个变量。

structbus_type{constchar*name;constchar*dev_name;structdevice*dev_root;structdevice_attribute*dev_attrs;/*usedev_groupsinstead*/conststructattribute_group**bus_groups;conststructattribute_group**dev_groups;conststructattribute_group**drv_groups;int(*match)(structdevice*dev,structdevice_driver*drv);int(*uevent)(structdevice*dev,structkobj_uevent_env*env);int(*probe)(structdevice*dev);int(*remove)(structdevice*dev);void(*shutdown)(structdevice*dev);int(*online)(structdevice*dev);int(*offline)(structdevice*dev);int(*suspend)(structdevice*dev,pm_message_tstate);int(*resume)(structdevice*dev);conststructdev_pm_ops*pm;structiommu_ops*iommu_ops;structsubsys_private*p;structlock_class_keylock_key;};

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

structbus_typeplatform_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

platform_bus_type的注册

五、注册代码流程详解

捋架构的利益,就是可以赞助我们定位问题

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

2. probe函数何时被调用到

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

代码调用流程