xHci-PCI驅動設計

xHci-PCI驅動設計

雖然Linux內核擁有C語言構建的身體,但它骨子裏散發的是面向對象的氣質,這一個個的對象就是struct。面對一個內核模塊的時候,首先要找出關鍵的struct和它們之間的關係,才能摸清代碼的骨骼脈絡。

先上圖,下面分析的過程結束之後各個結構體就是這種關係:

wKiom1mEF2uw7NUWAAFvYOLlpus572.png-wh_50

 

重要的配置:

xHCIUSB 3.xhost controller規範,首先進入drivers/usb/host目錄瀏覽一下,其實從文件名就可以知道,跟xHCI關係最密切的必然是xhci.c。保險起見還是看一下KConfig

config USB_XHCI_HCD 
   tristate "xHCI HCD (USB 3.0)support" 
   ---help--- 
      TheeXtensible Host Controller Interface (xHCI) is standard forUSB 3.0 
      "SuperSpeed"host controller hardware. 
 
      Tocompile this driver as a module, choose M here: the 
     module will be called xhci-hcd. 
 
ifUSB_XHCI_HCD 
 
config USB_XHCI_PCI 
      tristate 
      depends on PCI 
       defaulty 
 
config USB_XHCI_PLATFORM 
   tristate 
 
config USB_XHCI_MVEBU 
   tristate "xHCI support forMarvell Armada 375/38x" 
   select USB_XHCI_PLATFORM 
   depends on ARCH_MVEBU || COMPILE_TEST 
   ---help--- 
      Say'Y'to enable the support for the xHCI host controller 
     found in Marvell Armada 375/38x ARM SOCs. 
 
config USB_XHCI_RCAR 
   tristate "xHCI support forRenesas R-Car SoCs" 
   select USB_XHCI_PLATFORM 
   depends on ARCH_SHMOBILE || COMPILE_TEST 
   ---help--- 
      Say'Y'to enable the support for the xHCI host controller 
     found in Renesas R-Car ARM SoCs. 
 
endif # USB_XHCI_HCD

主要配置項叫做USB_XHCI_HCD,另外還有USB_XHCI_PCIUSB_XHCI_PLATFORM。再看Makefile,直接搜索XHCI有關的內容:

xhci-hcd-y := xhci.oxhci-mem.o 
xhci-hcd-y += xhci-ring.o xhci-hub.oxhci-dbg.o 
xhci-hcd-y += xhci-trace.o 
 
xhci-plat-hcd-y := xhci-plat.o 
ifneq ($(CONFIG_USB_XHCI_MVEBU), ) 
   xhci-plat-hcd-y     +=xhci-mvebu.o 
endif 
ifneq ($(CONFIG_USB_XHCI_RCAR), ) 
   xhci-plat-hcd-y     +=xhci-rcar.o 
endif 
 
obj-$(CONFIG_USB_XHCI_PCI)  += xhci-pci.o 
obj-$(CONFIG_USB_XHCI_PLATFORM) +=xhci-plat-hcd.o 
 
obj-$(CONFIG_USB_XHCI_HCD)  += xhci-hcd.o 

USB_XHCI_PCI,顧名思義,是xHCI驅動和PCI總線驅動之間的接口(內核開發者稱這種接口glue)。USB控制器大多是PCI設備,若控制器連接到PCI總線上,那麼自然是先由PCI驅動發現該設備,識別之後才能交給xHCI驅動處理。所以實際上,作爲gluexhci-pci模塊代碼要早於xhci-hcd模塊代碼開始工作,因此關鍵的初始化過程放在xhci-pci裏面。

現在進入正題:

xHCI相關的代碼,很容易發現幾個貌似重要的struct類型:usb_hcdxhci_hcdhc_driver,還有幾個全局變量xhci_pci_driverxhci_hc_driverxhci_pci_hc_driver,再加上PCI總線相關的類型pci_devpci_driver

xhci-pci模塊啓動,執行xhci_pci_init函數

staticint__init xhci_pci_init(void)
{
   xhci_init_driver(&xhci_pci_hc_driver, xhci_pci_setup);
#ifdef CONFIG_PM
   xhci_pci_hc_driver.pci_suspend = xhci_pci_suspend;
   xhci_pci_hc_driver.pci_resume = xhci_pci_resume;
#endif
    returnpci_register_driver(&xhci_pci_driver);
}

xhci_pci_init調用xhci_init_driver,初始化xhci_pci_hc_driver變量,主要作用就是把全局變量xhci_hc_driver的值一股腦賦給另一個全局變量xhci_pci_hc_driver。兩者都是struct hc_driver類型,xhci_pci_hc_driverxhci-pci.c中定義,是真正起作用的xHCI驅動,但它在定義的時候沒有進行任何成員的初始化:

void xhci_init_driver(struct hc_driver *drv, int(*setup_fn)(struct usb_hcd *))
{
   BUG_ON(!setup_fn);
    *drv= xhci_hc_driver;
    drv->reset= setup_fn;
}

staticstructhc_driver __read_mostly xhci_pci_hc_driver;

xhci_hc_driverxhci.c中定義,它包攬了所有的髒活累活:

staticconststruct hc_driver xhci_hc_driver = {
   .description =      "xhci-hcd",
   .product_desc =     "xHCI Host Controller",
    .hcd_priv_size=    sizeof(structxhci_hcd *),

    /*
     *generic hardware linkage
     */
    .irq=          xhci_irq,
   .flags =        HCD_MEMORY |HCD_USB3 | HCD_SHARED,

    /*
     *basic lifecycle operations
     */
   .reset =        NULL, /* set in xhci_init_driver() */
   .start =        xhci_run,
    .stop=         xhci_stop,
   .shutdown =     xhci_shutdown,

    /*
     *managing i/o requests and associated device resources
     */
   .urb_enqueue =     xhci_urb_enqueue,
   .urb_dequeue =     xhci_urb_dequeue,
   .alloc_dev =       xhci_alloc_dev,
   .free_dev =     xhci_free_dev,
   .alloc_streams =   xhci_alloc_streams,
   .free_streams =    xhci_free_streams,
   .add_endpoint =    xhci_add_endpoint,
    .drop_endpoint=    xhci_drop_endpoint,
   .endpoint_reset =  xhci_endpoint_reset,
   .check_bandwidth = xhci_check_bandwidth,
   .reset_bandwidth = xhci_reset_bandwidth,
   .address_device =  xhci_address_device,
   .enable_device =   xhci_enable_device,
   .update_hub_device =   xhci_update_hub_device,
   .reset_device =    xhci_discover_or_reset_device,

    /*
     *scheduling support
     */
   .get_frame_number = xhci_get_frame,

    /*
     *root hub support
     */
   .hub_control =      xhci_hub_control,
   .hub_status_data = xhci_hub_status_data,
   .bus_suspend =     xhci_bus_suspend,
   .bus_resume =      xhci_bus_resume,

    /*
     *call back when device connected and addressed
     */
   .update_device =        xhci_update_device,
   .set_usb2_hw_lpm = xhci_set_usb2_hardware_lpm,
   .enable_usb3_lpm_timeout = xhci_enable_usb3_lpm_timeout,
   .disable_usb3_lpm_timeout = xhci_disable_usb3_lpm_timeout,
   .find_raw_port_number = xhci_find_raw_port_number,
};

xhci_init_driver函數將xhci_hc_driver的值賦給xhci_pci_hc_driver後,前者也就退下了舞臺。

xhci_pci_driver被專業人員稱爲pcidriver glue,屬於一種新型的PCI驅動模塊。

xhci_pci_init調用pci_register_driver,將xhci_pci_driver註冊爲PCI設備驅動。在xhci-pci.c中靜態定義並初始化:

/* pci driver glue; this is a"new style" PCI driver module */
staticstructpci_driver xhci_pci_driver = {
    .name=     (char*) hcd_name,
   .id_table = pci_ids,

   .probe =    xhci_pci_probe,
   .remove =   xhci_pci_remove,
    /* suspend and resume implemented later */

   .shutdown =    usb_hcd_pci_shutdown,
#ifdef CONFIG_PM
   .driver = {
       .pm = &usb_hcd_pci_pm_ops
    },
#endif
};

id_table包含了驅動支持的PCI設備類型,PCI總線就是靠它判斷驅動和設備能否配對。這裏的id_table成員設置爲pci_ids,它也是靜態定義的全局變量:

/* PCI driver selectionmetadata; PCI hotplugging uses this */
staticconststructpci_device_id pci_ids[] = { {
    /* handle any USB 3.0 xHCI controller */
   PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_USB_XHCI, ~0),
   .driver_data =  (unsignedlong)&xhci_pci_hc_driver,
    },
    { /* end: all zeroes */ }
};
MODULE_DEVICE_TABLE(pci, pci_ids);

pci_ids中唯一一個元素的driver_data成員指向剛纔在xhci_pci_init中完成初始化的xhci_pci_hc_driver變量,這就將作爲PCI設備驅動的xhci_pci_driver和作爲USB主機控制器設備驅動xhci_pci_hc_driver聯繫了起來。

staticintxhci_pci_setup(struct usb_hcd *hcd)
{
        structxhci_hcd         *xhci;
        structpci_dev          *pdev =to_pci_dev(hcd->self.controller);
        int                     retval;
       //uDP720202芯片的配置
       xhci_fwdownload(hcd);
        //創建了對xHCI至關重要的數據結構——xhci_hcd類型,並完成了大量的初始化工作
       retval = xhci_gen_setup(hcd, xhci_pci_quirks);
        if(retval)
               return retval;

       xhci = hcd_to_xhci(hcd);
        if(!usb_hcd_is_primary_hcd(hcd))
               return0;

       pci_read_config_byte(pdev, XHCI_SBRN_OFFSET, &xhci->sbrn);
       xhci_dbg(xhci, "GotSBRN %u\n", (unsignedint)xhci->sbrn);

        /* Find any debug ports */
       retval = xhci_pci_reinit(xhci, pdev);
        if(!retval)
               return retval;

       kfree(xhci);
        returnretval;
}

USB 3.1 SpecFigure 3-1可以知道,USB 3.xHost包含兩個roothub,對應兩條USB總線,一條連接USB 2.0設備,一條連接USB 3.x設備。

xhci_pci_probe調用usb_hcd_pci_probe,創建usb_hcdxhci_hcd。回到最初的示意圖,現在所有的連接都已經完成!

int usb_hcd_pci_probe(struct pci_dev *dev, conststruct pci_device_id *id)
{
    structhc_driver        *driver;
    structusb_hcd          *hcd;
    int                     retval;
    int                     hcd_irq = 0;

    if(usb_disabled())
           return-ENODEV;

    if(!id)
           return-EINVAL;
    driver= (structhc_driver *)id->driver_data;
    if(!driver)
           return-EINVAL;

    if(pci_enable_device(dev) < 0)
           return-ENODEV;

    /*
     * The xHCI driver has its own irq management
     * make sure irq setup is not touched for xhciin generic hcd code
     */
    if((driver->flags & HCD_MASK) != HCD_USB3) {
           if(!dev->irq) {
                  dev_err(&dev->dev,
                  "Found HC with no IRQ. Check BIOS/PCI %s setup!\n",
                         pci_name(dev));
                  retval= -ENODEV;
                  gotodisable_pci;
           }
           hcd_irq= dev->irq;
    }

    hcd =usb_create_hcd(driver, &dev->dev, pci_name(dev));
    if(!hcd) {
           retval= -ENOMEM;
           gotodisable_pci;
    }
    ......
    returnretval;
}

uDP720202芯片的配置的時序圖:

wKioL1mEF4SDyhvOAADqWQQPPkE195.png-wh_50

 

//@@----------------------PCI 信息
#define XHCI_FWFILENAME_720201_202ES20  "renesas/K2011070.mem"
//uDp720202 寄存器配置
#define XHCI_VENDOR_ID_720202           (0x1912)
#define XHCI_DEVICE_ID_720202           (0x0015)
#define XHCI_DEVICE_REV_ES20            (0x02)


//@@---------------------- FM下載配置寄存器
#define PCICNF0F4  0xF4  // 來自芯片寄存器手冊
   /*
   0 = FWdownload enable (1), RW
   1 = FWdownload lock (1) or unlock (0), need 0 to perform download, RW(Write Once)
   6:4 =Result code, RO -- processing (0), success (1), error (2),
   8 =Set Data 0, RW
   9 =Set Data 1, RW
   16:31= (used for serial EEPROM read/write.  31= serial EEPROM present.)
   */
#define PCICNF0F4_FWDOWNLOADENABLE_B    (0)
#define PCICNF0F4_FWDOWNLOADLOCK_B      (1)
#define PCICNF0F4_SETDATA0_B            (8)
#define PCICNF0F4_SETDATA1_B            (9)
#define PCICNF0F4_RESULT_B              (4)

#define PCICNF0F8  0xF8  // 數據頁 0

#define PCICNF0FC  0xFC  // 數據頁 1

主要調用過程:

wKiom1mEF5HieAR-AADdUwoIFHY340.png-wh_50

 

 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章