詳解linux usb host驅動編寫入門

2022-09-26 14:51:12 字數 3382 閱讀 2536

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

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

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.**件。

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.ctumfvn中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 usb host驅動編寫入門

本文位址:

SpringCloud Stream訊息驅動概述

簡介 如果系統裡同時存在多種mq,可以使用使用cloud stream,只需要和stream互動就可以進行管理。一句話,遮蔽底層訊息中介軟體的差異,降低切換成本,統一訊息的程式設計模型 官網 中文手冊 官方定義springcloud stream是乙個構建訊息驅動微服務的框架 應用程式通過input...

SpringCloud Stream訊息驅動

遮蔽底層訊息中介軟體的差異,降低切換成本,統一訊息的程式設計模型。在沒有binder這個概念的情況下,springboot應用要直接與訊息中介軟體進行資訊互動的時候,由於各訊息中介軟體構建的初衷不同,他們的實現細節上有較大差異,通過定義binder作為中間層,完美實現了應用程式與訊息中介軟體細節之間...

springcloud stream訊息驅動

什麼是spring cloud stream?官方定義 spring cloud stream 是乙個構建 訊息驅動 微服務的框架。應用程式通過inputs或者outputs來與 spring cloud stream 中binder互動,通過我們配置來 binding 而 spring cloud...