linux resouce,platform_device和platform_driver驅動的關係

來源:互聯網
上載者:User

Author: taoyuetao
Email: tao_yuetao@yahoo.com.cn
Blog: http://www.eetop.cn/blog/?11145

2006-11-21

================================

從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,它描述了一個{
tagshow(event, 'usb');return false;
}" href="javascript:;">usb 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所指定的名稱,來擷取資源中的中斷號。  這個網址也有相應的內容,看了之後也有點模糊了。  platform_device註冊過程               

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.