linux驅動編寫(usb host驅動入門)

2021-08-18 07:19:03 字數 3257 閱讀 5348

】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 inte***ce (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

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)

static inline void _ohci_writel (const struct ohci_hcd *ohci,

const unsigned int val, __hc32 __iomem *regs)

#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))

4、usb驅動怎麼學

如果從**結構來說,上面這段分析算是入門了。但是,如果要深入了解usb host&device驅動,那麼除了這些**邏輯,那麼還要熟讀usb協議手冊,更重要的學會用catc協議分析儀真正地去了解usb是如何發包和收包的。

編寫Linux裝置驅動

核心版本 2.4.22 閱讀此文的目的 學會編寫linux裝置 驅動。閱讀此文的方法 閱讀以下2個 檔案 hello.c,asdf.c。此文假設讀者 已經能用c語言編寫linux應用程式,理解 字元裝置檔案,塊裝置檔案,主裝置號,次裝置號 會寫簡單的shell指令碼和makefile。1.hello...

linux驅動編寫之程序獨佔驅動

一 描述 嵌入式開發系統中,有各種硬體資源,而有些硬體資源使用時候是需要程序獨佔的。也就是說,同一時刻只有乙個程序允許使用這個硬體資源,其他的程序只能放棄執行或者掛起等待。在設計其對應驅動的時候,就需要做獨佔處理。example led燈驅動,4盞led燈,在open的時候呼叫驅動,對其引腳進行配置...

linux驅動編寫(入門)

csdn 大牛 feixiaoxing 在我離職之前,工作內容幾乎不涉及到驅動方面的知識。我所要做的內容就是把客戶對裝置的請求拆分成乙個乙個的介面,呼叫驅動的設定進行配置就可以了。當然,至於驅動下面是怎麼實現那就要根據具體情況而定了。比如說,有的驅動是晶元廠商直接寫好的,假設晶元廠商提供了對應平台的...