您好,登錄后才能下訂單哦!
usb協議是一個復雜的協議,目前涉及到的版本就有usb1.0, usb2.0, usb3.0。大家如果打開kernel usb host目錄,就會發現下面包含了ohci,uhci,ehci,xhci,whci等多種形式的控制器驅動。那么,對于我們這些不是很了解usb的開發人員,如何了解usb的代碼結構呢?
1、代碼分布
drivers/usb目錄下面,host目錄包括了host驅動代碼,core目錄包含了主要的api接口代碼,而其他目錄則主要是device驅動代碼。
2、device驅動怎么看
device驅動大多數和上層協議有關,不涉及到具體的寄存器讀寫。示例代碼可以參考usb-skeleton.c
3、host驅動怎么看
a,不妨以s3c2410的host作為范例進行分析,首先找到Makefile,
obj-$(CONFIG_USB_OHCI_HCD_S3C2410) += ohci-s3c2410.o
b,再查看一下Kconfig,
config USB_OHCI_HCD_S3C2410 tristate "OHCI support for Samsung S3C24xx/S3C64xx SoC series" depends on USB_OHCI_HCD && (ARCH_S3C24XX || ARCH_S3C64XX) default y ---help--- Enables support for the on-chip OHCI controller on S3C24xx/S3C64xx chips.
c,通過Makefile和Kconfig發現,s3c2410依賴于USB_OHCI_HCD_S3C2410 和 USB_OHCI_HCD,那USB_OHCI_HCD呢?
config USB_OHCI_HCD tristate "OHCI HCD (USB 1.1) support" depends on HAS_DMA && HAS_IOMEM ---help--- The Open Host Controller Interface (OHCI) is a standard for accessing USB 1.1 host controller hardware. It does more in hardware than Intel's UHCI specification. If your USB host controller follows the OHCI spec, say Y. On most non-x86 systems, and on x86 hardware that's not using a USB controller from Intel or VIA, this is appropriate. If your host controller doesn't use PCI, this is probably appropriate. For a PCI based system where you're not sure, the "lspci -v" entry will list the right "prog-if" for your USB controller(s): EHCI, OHCI, or UHCI. To compile this driver as a module, choose M here: the module will be called ohci-hcd.
d,USB_OHCI_HCD只依賴于DMA和IOMEM。繼續回到Makefile,判斷USB_OHCI_HCD會編譯哪些文件
obj-$(CONFIG_USB_OHCI_HCD) += ohci-hcd.o
e,看到這里,我們明白要打開s3c2410的host功能,只需要編譯ohci-hcd.c和ohci-s3c2410.c兩個文件就好了
f,通過觀察,發現ohci-hcd.c和ohci-s3c2410.c的代碼都很少,這原因是什么?下面這段代碼來自于ohci-hcd.c。
static const char hcd_name [] = "ohci_hcd"; #define STATECHANGE_DELAY msecs_to_jiffies(300) #define IO_WATCHDOG_DELAY msecs_to_jiffies(275) #define IO_WATCHDOG_OFF 0xffffff00 #include "ohci.h" #include "pci-quirks.h" static void ohci_dump(struct ohci_hcd *ohci); static void ohci_stop(struct usb_hcd *hcd); static void io_watchdog_func(struct timer_list *t); #include "ohci-hub.c" #include "ohci-dbg.c" #include "ohci-mem.c" #include "ohci-q.c"
g,通過觀察ohci-hcd.c文件,發現其實它其實已經包括了很多其他的ohci文件。那么寄存器又是怎么操作的呢?下面這段代碼來自于ohci.h文件。
static inline unsigned int _ohci_readl (const struct ohci_hcd *ohci, __hc32 __iomem * regs) { #ifdef CONFIG_USB_OHCI_BIG_ENDIAN_MMIO return big_endian_mmio(ohci) ? readl_be (regs) : readl (regs); #else return readl (regs); #endif } static inline void _ohci_writel (const struct ohci_hcd *ohci, const unsigned int val, __hc32 __iomem *regs) { #ifdef CONFIG_USB_OHCI_BIG_ENDIAN_MMIO big_endian_mmio(ohci) ? writel_be (val, regs) : writel (val, regs); #else writel (val, regs); #endif } #define ohci_readl(o,r) _ohci_readl(o,r) #define ohci_writel(o,v,r) _ohci_writel(o,v,r)
h,看到這里,你應該發現大部分底層操作其實也都是ohci幫助一起完成的。每個host driver其實就是注冊了一下,告知了mem地址在哪。下面這段代碼就是ohci-s3c2410.c中probe函數的代碼。
hcd->regs = devm_ioremap_resource(&dev->dev, &dev->resource[0]); if (IS_ERR(hcd->regs)) { retval = PTR_ERR(hcd->regs); goto err_put; }
4、usb驅動怎么學
如果從代碼結構來說,上面這段分析算是入門了。但是,如果要深入了解usb host&device驅動,那么除了這些代碼邏輯,那么還要熟讀usb協議手冊,更重要的學會用catc協議分析儀真正地去了解usb是如何發包和收包的。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。