從2.6版本開始引入了platform這個概念,在開發底層驅動程序時,首先要確認的就是設備的資源信息,例如設備的地址,
在2.6內核中將每一個設備的資源用結構platform_device來描述,該結構體定義在kernel\include\linux\platform_device.h中,
struct platform_device {
const char * name;
u32 id;
struct device dev;
u32 num_resources;
struct resource * resource;
};
該結構一個重要的元素是resource,該元素存入了最爲重要的設備資源信息,定義在kernel\include\linux\ioport.h中,
struct resource {
const char *name;
unsigned long start, end;
unsigned long flags;
struct resource *parent, *sibling, *child;
};
下面舉個例子來講明一下:
在kernel\arch\arm\mach-pxa\pxa27x.c定義了
tatic struct resource pxa27x_ohci_resources[] = {
[0] = {
.start = 0x4C000000,
.end = 0x4C00ff6f,
.flags = IORESOURCE_MEM,
},
[1] = {
.start = IRQ_USBH1,
.end = IRQ_USBH1,
.flags = IORESOURCE_IRQ,
},
};
這裏定義了兩組resource,它描述了一個
[url=javascript:;]
usb[/url] host設備的資源,第1組描述了這個usb host設備所佔用的總線地址範圍,IORESOURCE_MEM表示第1組描述的是內存類型的資源信息,第2組描述了這個usb host設備的中斷號,IORESOURCE_IRQ表示第2組描述的是中斷資源信息。設備驅動會根據flags來獲取相應的資源信息。 有了resource信息,就能夠定義platform_device了: static struct platform_device ohci_device = { .name = "pxa27x-ohci", .id = -1, .dev = { .dma_mask = &pxa27x_dmamask, .coherent_dma_mask = 0xffffffff, }, .num_resources = ARRAY_SIZE(pxa27x_ohci_resources), .resource = pxa27x_ohci_resources, }; 有了platform_device就能夠調用函數platform_add_devices向系統中添加該設備了,這裏的實現是 static int __init pxa27x_init(void) { return platform_add_devices(devices, ARRAY_SIZE(devices)); } 這裏的pxa27x_init必須在設備驅動加載以前被調用,能夠把它放到 subsys_initcall(pxa27x_init); 驅動程序須要實現結構體struct platform_driver,參考kernel\driver\usb\host\ohci-pxa27.c, static struct platform_driver ohci_hcd_pxa27x_driver = { .probe = ohci_hcd_pxa27x_drv_probe, .remove = ohci_hcd_pxa27x_drv_remove, #ifdef CONFIG_PM .suspend = ohci_hcd_pxa27x_drv_suspend, .resume = ohci_hcd_pxa27x_drv_resume, #endif .driver = { .name = "pxa27x-ohci", }, }; 在驅動初始化函數中調用函數platform_driver_register()註冊platform_driver,須要注意的是 ohci_device結構中name元素和ohci_hcd_pxa27x_driver結構中driver.name必須是相同的,這樣在 platform_driver_register()註冊時會對全部已註冊的所platform_device中的name和當前註冊的 platform_driver的driver.name進行比較,只有找到相同的名稱的platfomr_device才能註冊成功,當註冊成功時會調 用platform_driver結構元素probe函數指針,這裏就是ohci_hcd_pxa27x_drv_probe。 當進入probe函數後,須要獲取設備的資源信息,獲取資源的函數有: struct resource * platform_get_resource(struct platform_device *dev, unsigned int type, unsigned int num); 根據參數type所指定類型,例如IORESOURCE_MEM,來獲取指定的資源。 struct int platform_get_irq(struct platform_device *dev, unsigned int num); 獲取資源中的中斷號。 struct resource * platform_get_resource_byname(struct platform_device *dev, unsigned int type, char *name); 根據參數name所指定的名稱,來獲取指定的資源。 int platform_get_irq_byname(struct platform_device *dev, char *name); 根據參數name所指定的名稱,來獲取資源中的中斷號。 在這個網址也有類似的內容供你們參考。