Linux驅動的platform機制

來源:互聯網
上載者:User

最近研究了一下Linux驅動的platform機制。

從Linux2.6起,引入了一套新的驅動管理和註冊機制:Platform_device和Platform_driver。Linux中大部分的裝置驅動,都可以使用這套機制,裝置用platform_device表示,驅動用platform_driver進行註冊。

 

Linux platform driver機制和傳統的device_driver機制相比,一個十分明顯的優勢在於platform機制將本身的資源註冊進核心,由核心統一管理,在驅動程式中使用這些資源時通過platform_device提供的標準介面進行申請並使用。這樣提高了驅動和資源管理的獨立性,並且擁有較好的可移植性和安全性。

 

和傳統的驅動一樣,platform機制也分為三個步驟:

 

1 匯流排註冊階段:

核心啟動初始化時的main.c檔案中的kernel_init() à do_basic_setup() à driver_init() à platform_bus_init() à bus_register(&platform_bus_type),註冊了一條platform匯流排(虛擬匯流排)。

 

2 添加裝置時:

裝置註冊的時候Platform_device_register() à platform_device_add() à (pdev à dev.bus = &platform_bus_type) à device_add(),就這樣把裝置給掛到虛擬匯流排上。

 

3 驅動註冊時:

    Platform_driver_register() à driver_register() à bus_add_driver() à driver_attach() à bus_for_each_dev(), 對在每個掛在虛擬platform bus的裝置作__driver_attach() à driver_probe_device(),判斷drvàbusàmatch()是否執行成功,此時通過指標執行platform_match à strncmp(pdev à name , drvàname , BUS_ID_SIZE),如果相符就調用really_probe(實際就是執行相應裝置的platform_driver àprobe(platform_device)。)開始真正的探測,如果probe成功,則綁定裝置到該驅動。

 

    從上面可以看出,platform機制最後還是調用了bus_register() , device_add() , driver_register()這三個關鍵的函數。

 

下面看幾個結構體:

struct platform_device           (/include/linux/Platform_device.h)

{

       const char   * name;

       int          id;

       struct device     dev;

       u32        num_resources;

       struct resource * resource;

};

    Platform_device結構體描述了一個platform結構的裝置,在其中包含了一般裝置的結構體struct device       dev;裝置的資源結構體struct resource    * resource;還有裝置的名字const char     * name。(注意,這個名字一定要和後面platform_driver.driver àname相同,原因會在後面說明。)

 

該結構體中最重要的就是resource結構,這也是之所以引入platform機制的原因。

struct resource                            ( /include/linux/ioport.h)

{

       resource_size_t start;

       resource_size_t end;

       const char *name;

       unsigned long flags;

       struct resource *parent, *sibling, *child;

};

    其中 flags位表示該資源的類型,start和end分別表示該資源的起始地址和結束位址

 

struct platform_driver                  (/include/linux/Platform_device.h)

{

       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 (*suspend_late)(struct platform_device *, pm_message_t state);

       int (*resume_early)(struct platform_device *);

       int (*resume)(struct platform_device *);

       struct device_driver driver;

};

Platform_driver結構體描述了一個platform結構的驅動。其中除了一些函數指標外,還有一個一般驅動的device_driver結構。

 

 

名字要一致的原因:

上面說的驅動在註冊的時候會調用函數bus_for_each_dev(), 對在每個掛在虛擬platform bus的裝置作__driver_attach() à driver_probe_device(),在此函數中會對dev和drv做初步的匹配,調用的是drv->bus->match所指向的函數。platform_driver_register函數中drv->driver.bus = &platform_bus_type,所以drv->bus->match就為platform_bus_typeàmatch,為platform_match函數,該函數如下:

static int platform_match(struct device * dev, struct device_driver * drv)

{

       struct platform_device *pdev = container_of(dev, struct platform_device, dev);

 

       return (strncmp(pdev->name, drv->name, BUS_ID_SIZE) == 0);

}

是比較dev和drv的name,相同則會進入really_probe()函數,從而進入自己寫的probe函數做進一步的匹配。所以devàname和driveràdrvàname在初始化時一定要填一樣的。

 

不同類型的驅動,其match函數是不一樣的,這個platform的驅動,比較的是dev和drv的名字,還記得usb類驅動裡的match嗎?它比較的是Product ID和Vendor ID。

個人總結Platform機制的好處:

 

1 提供platform_bus_type類型的匯流排,把那些不是匯流排型的soc裝置都添加到這條虛擬匯流排上。使得,匯流排——裝置——驅動 的模式可以得到普及。

 

2 提供platform_device和platform_driver類型的資料結構,將傳統的device和driver資料結構嵌入其中,並且加入resource成員,以便於和Open Firmware這種動態傳遞裝置資源的新型bootloader和kernel 接軌。

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.