详解linux usb host驱动编写入门

(编辑:jimmy 日期: 2024/12/30 浏览:2)

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) "htmlcode">
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是如何发包和收包的。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。