http://www.top-e.org/jiaoshi/html/?377.html
從Linux 2.6起引入了一套新的驅動管理和註冊機制:platform_device和platform_driver。Linux中大部分的裝置驅動,都可以使用這套機制,裝置用platform_device表示,驅動用platform_driver進行註冊。
Linux platform driver機制和傳統的device driver 機制(通過driver_register函數進行註冊)相比,一個十分明顯的優勢在於platform機制將裝置本身的資源註冊進核心,由核心統一管理,在驅動程式中使用這些資源時通過platform device提供的標準介面進行申請並使用。這樣提高了驅動和資源管理的獨立性,並且擁有較好的可移植性和安全性(這些標準介面是安全的)。platform機制的本身使用並不複雜,由兩部分組成:platform_device和platfrom_driver。通過platform機制開發底層裝置驅動的大致流程。
圖 platform機制開發驅動流程
platform_device結構體用來描述裝置的名稱、資源資訊等。該結構被定義在include/linux/platform_device.h中,定義原型如下:
struct platform_device {
const char * name; //定義平台裝置的名稱
int id;
struct device dev;
u32 num_resources;
struct resource * resource; //定義平台裝置的資源。
};
下面來看一下platform_device結構體中最重要的一個成員struct resource * resource。struct resource被定義在include/linux/ioport.h中,定義原型如下:
struct resource {
resource_size_t start; //定義資源的起始地址
resource_size_t end; //定義資源的結束位址
const char *name; //定義資源的名稱
unsigned long flags; //定義資源的類型,比如MEM,IO,IRQ,DMA類型
struct resource *parent, *sibling, *child; //資源鏈表指標
};
通過調用函數platform_add_devices()向系統中添加該裝置了,該函數內部調用platform_device_register( )進行裝置註冊。要注意的是,這裡的platform_device裝置的註冊過程必須在相應裝置驅動載入之前被調用,即執行platform_driver_register()之前,原因是驅動註冊時需要匹配核心中所有登入的裝置名稱。
接下來來看platform_driver結構體的原型定義,在include/linux/platform_device.h中,代碼如下:
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 (*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_driver_register(),其原型定義在driver/base/platform.c檔案中,具體實現代碼如下:
int platform_driver_register(struct platform_driver *drv)
{
drv->driver.bus = &platform_bus_type;
if (drv->probe)
drv->driver.probe = platform_drv_probe;
if (drv->remove)
drv->driver.remove = platform_drv_remove;
if (drv->shutdown)
drv->driver.shutdown = platform_drv_shutdown;
if (drv->suspend)
drv->driver.suspend = platform_drv_suspend;
if (drv->resume)
drv->driver.resume = platform_drv_resume;
return driver_register(&drv->driver);
}
總結,通常情況下只要和核心本身運行依賴性不大的外圍裝置,相對獨立的,擁有各自獨自的資源(地址匯流排和IRQs),都可以用platform_driver實現。如:LCD,網卡、USB、UART等,都可以用platfrom_driver寫,而timer,irq等小系統之內的裝置則最好不用platfrom_driver機制。