加入星計劃,您可以享受以下權(quán)益:

  • 創(chuàng)作內(nèi)容快速變現(xiàn)
  • 行業(yè)影響力擴散
  • 作品版權(quán)保護
  • 300W+ 專業(yè)用戶
  • 1.5W+ 優(yōu)質(zhì)創(chuàng)作者
  • 5000+ 長期合作伙伴
立即加入
  • 正文
    • 一、概念
    • 二、platform
    • 三、舉例
    • 四、platform_device是如何管理的?
    • 四、總線相關的其他的知識點
    • 五、注冊代碼流程詳解
  • 推薦器件
  • 相關推薦
  • 電子產(chǎn)業(yè)圖譜
申請入駐 產(chǎn)業(yè)圖譜

Linux驅(qū)動-platform總線詳解

06/27 10:30
802
  • 1評論
閱讀需 36 分鐘
加入交流群
掃碼加入
獲取工程師必備禮包
參與熱點資訊討論

platform總線是學習linux驅(qū)動必須要掌握的一個知識點。

本文參考已發(fā)布:Linux 3.14內(nèi)核

一、概念

嵌入式系統(tǒng)中有很多的物理總線:I2c、SPI、USB、uart、PCIE、APB、AHB

linux從2.6起就加入了一套新的驅(qū)動管理和注冊的機制platform平臺總線,是一條虛擬的總線,并不是一個物理的總線。

相比 PCI、USB,它主要用于描述SOC上的片上資源。platform 所描述的資源有一個共同點:在CPU 的總線上直接取址。

平臺設備會分到一個名稱(用在驅(qū)動綁定中)以及一系列諸如地址和中斷請求號(IRQ)之類的資源。

設備用platform_device表示,驅(qū)動用platform_driver進行注冊。

與傳統(tǒng)的bus/device/driver機制相比,platform由內(nèi)核進行統(tǒng)一管理,在驅(qū)動中使用資源,提高了代碼的安全性和可移植性。

二、platform

1. platform總線兩個最重要的結(jié)構(gòu)體

platform維護的所有的驅(qū)動都必須要用該結(jié)構(gòu)體定義:

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

該結(jié)構(gòu)體,用于注冊驅(qū)動到platform總線,

成員 含義
probe 當驅(qū)動和硬件信息匹配成功之后,就會調(diào)用probe函數(shù),驅(qū)動所有的資源的注冊和初始化全部放在probe函數(shù)中
remove 硬件信息被移除了,或者驅(qū)動被卸載了,全部要釋放,釋放資源的操作就放在該函數(shù)中
struct device_driver driver 內(nèi)核維護的所有的驅(qū)動必須包含該成員,通常driver->name用于和設備進行匹配
const struct platform_device_id *id_table 往往一個驅(qū)動可能能同時支持多個硬件,這些硬件的名字都放在該結(jié)構(gòu)體數(shù)組中

我們編寫驅(qū)動的時候往往需要填充以上幾個成員

platform_device

platform總線用于描述設備硬件信息的結(jié)構(gòu)體,包括該硬件的所有資源(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;
};
成員 含義
const char *name 設備的名字,用于和驅(qū)動進行匹配的
struct device dev 內(nèi)核中維護的所有的設備必須包含該成員,
u32 num_resources 資源個數(shù)
struct resource *resource 描述資源

struct device dev->release()必須實現(xiàn),

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

0x139d0000

struct?resource?{
?resource_size_t?start;??//表示資源的起始值,???????????
?resource_size_t?end;????//表示資源的最后一個字節(jié)的地址,?如果是中斷,end和satrt相同
?const?char?*name;???//?可不寫??
?unsigned?long?flags;?//資源的類型
?struct?resource?*parent,?*sibling,?*child;
};
flags的類型說明

#define?IORESOURCE_MEM??0x00000200????//內(nèi)存
#define?IORESOURCE_IRQ??0x00000400????//中斷

內(nèi)核管理的所有的驅(qū)動,都必須包含一個叫struct device_driver成員, ?//男性描述的硬件,必須包含struct device結(jié)構(gòu)體成員。? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //女性

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;

用于和硬件進行匹配。

內(nèi)核描述硬件,必須包含struct device結(jié)構(gòu)體成員:

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驅(qū)動的步驟

1)注冊驅(qū)動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. 開發(fā)步驟

platform 總線下驅(qū)動的開發(fā)步驟是:

設備

需要實現(xiàn)的結(jié)構(gòu)體是:platform_device 。

1)初始化 resource 結(jié)構(gòu)變量

2)初始化 platform_device 結(jié)構(gòu)變量

3)向系統(tǒng)注冊設備:platform_device_register。

以上三步,必須在設備驅(qū)動加載前完成,即執(zhí)行platform_driver_register()之前,原因是驅(qū)動注冊時需要匹配內(nèi)核中所有已注冊的設備名。

platform_driver_register()中添加device到內(nèi)核最終還是調(diào)用的device_add函數(shù)。

Platform_device_add和device_add最主要的區(qū)別是多了一步insert_resource(p, r),即將platform資源(resource)添加進內(nèi)核,由內(nèi)核統(tǒng)一管理。

驅(qū)動

驅(qū)動注冊中,需要實現(xiàn)的結(jié)構(gòu)體是:platform_driver 。

驅(qū)動程序的初始化函數(shù)中,調(diào)用了platform_driver_register()注冊 platform_driver 。

需要注意的是:platform_driver 和 platform_device 中的 name 變量的值必須是相同的【在不考慮設備樹情況下,關于設備樹,后面會寫新的文章詳細講述】 。

這樣在
platform_driver_register() 注冊時,會將當前注冊的 platform_driver 中的 name
變量的值和已注冊的所有 platform_device 中的 name 變量的值進行比較,只有找到具有相同名稱的 platform_device
才能注冊成功。

當注冊成功時,會調(diào)用 platform_driver 結(jié)構(gòu)元素 probe 函數(shù)指針。

實例1

本例比較簡單,只用于測試platform_driver 和platform_device是否可以匹配成功。

左邊是platform_device結(jié)構(gòu)體注冊的代碼,右邊是platform_driver結(jié)構(gòu)體注冊的代碼。

platform_driver 定義和注冊:

?1?#include?<linux/init.h>
??2?#include?<linux/module.h>
??3?#include?<linux/platform_device.h>
??4?#include?<linux/ioport.h>
??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?<linux/init.h>????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
??2?#include?<linux/module.h>
??3?#include?<linux/platform_device.h>
??4?#include?<linux/ioport.h>
??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

給結(jié)構(gòu)體platform_device 增加硬件信息,并在內(nèi)核中能夠讀取出來。本例向結(jié)構(gòu)體hello_device 增加信息如下:

    1. 基址寄存器地址0x139d0000,該地址的空間是0x4中斷號199

 

    1. 【注意】

 

    實際的內(nèi)核中會把外設的中斷號根據(jù)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);

?//注冊中斷、申請內(nèi)存
?return?0;
}

重新編譯,卸載第一個例子的模塊,并清除log:

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

執(zhí)行

由結(jié)果可知,probe函數(shù)正確讀取到了硬件信息。

四、platform_device是如何管理的?

1. 沒有設備樹

在沒有設備樹的時候,以三星Cortex-A8 ?s5pc100為例,硬件信息放在以下位置

archarmmach-s5pc100Mach-smdkc100.c
archarmplat-samsung

注冊platform_deviceplatform_device定義

該數(shù)組存放了,內(nèi)核啟動需要初始化的硬件的信息。

2. 如果有設備樹

內(nèi)核會有設備初始化的完整代碼,會在內(nèi)核啟動的時候把設備樹信息解析初始化,把硬件信息初始化到對應的鏈表中。在總線匹配成功后,會把硬件的信息傳遞給probe()函數(shù)。

四、總線相關的其他的知識點

1. 內(nèi)核總線相關結(jié)構(gòu)體變量

內(nèi)核維護的所有的總線都需要用以下結(jié)構(gòu)體注冊一個變量。

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總線維護的驅(qū)動,
通過名字來匹配,如果相同,就說明硬件信息和驅(qū)動匹配,就會調(diào)用驅(qū)動的platform_driver ->probe函數(shù),初始化驅(qū)動的所有資源,讓該驅(qū)動生效。

當有設備的驅(qū)動注冊到platform_bus_type 總線的時候,會遍歷所有platform總線維護的硬件信息,
通過名字來匹配,如果相同,就說明硬件信息和驅(qū)動匹配,就會調(diào)用驅(qū)動的platform_driver ->probe函數(shù),初始化驅(qū)動的所有資源,讓該驅(qū)動生效。

注冊位置

driversbasePlatform.c

platform_bus_type的注冊

五、注冊代碼流程詳解

捋架構(gòu)的好處,就是可以幫助我們定位問題

1. match函數(shù)何時被調(diào)用到?

2. probe函數(shù)何時被調(diào)用到

以下是上述兩個問題代碼的調(diào)用流程:

代碼調(diào)用流程

后面我們會再詳細介紹設備樹。

end

推薦器件

更多器件
器件型號 數(shù)量 器件廠商 器件描述 數(shù)據(jù)手冊 ECAD模型 風險等級 參考價格 更多信息
NRF52840-QIAA-R7 1 Nordic Semiconductor Telecom Circuit, 1-Func, PBGA73, AQFN-73
$7.48 查看
CM7V-T1A-LOW-ESR-32.768KHZ-7PF-20-TA-QC 1 Micro Crystal AG Parallel - Fundamental Quartz Crystal,

ECAD模型

下載ECAD模型
$3.02 查看
AT27C512R-45JU-T 1 Microchip Technology Inc OTP ROM, 64KX8, 45ns, CMOS, PQCC32

ECAD模型

下載ECAD模型
$2.23 查看

相關推薦

電子產(chǎn)業(yè)圖譜

公眾號『一口Linux』號主彭老師,擁有15年嵌入式開發(fā)經(jīng)驗和培訓經(jīng)驗。曾任職ZTE,某研究所,華清遠見教學總監(jiān)。擁有多篇網(wǎng)絡協(xié)議相關專利和軟件著作。精通計算機網(wǎng)絡、Linux系統(tǒng)編程、ARM、Linux驅(qū)動、龍芯、物聯(lián)網(wǎng)。原創(chuàng)內(nèi)容基本從實際項目出發(fā),保持原理+實踐風格,適合Linux驅(qū)動新手入門和技術(shù)進階。