Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed introduction to linux usb host driver programming

2025-03-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Usb protocol is a complex protocol. At present, the versions involved are usb1.0, usb2.0 and usb3.0. If you open the kernel usb host directory, you will find that the following contains ohci,uhci,ehci,xhci,whci and other forms of controller drivers. So, for those of us who don't know much about usb, how do we understand the code structure of usb?

1. Code distribution

Under the drivers/usb directory, the host directory contains the host driver code, the core directory contains the main api interface code, and the other directories are mainly device driver code.

2. What do you think of device driver

Most of the device drivers are related to the upper layer protocols and do not involve specific register reading and writing. For example code, please refer to usb-skeleton.c

3. What do you think of host driver

A, you might as well take the host of s3c2410 as an example, and first find Makefile.

Obj-$ (CONFIG_USB_OHCI_HCD_S3C2410) + = ohci-s3c2410.o

B, check Kconfig again

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, through Makefile and Kconfig found that s3c2410 depends on USB_OHCI_HCD_S3C2410 and USB_OHCI_HCD, what about 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.

The djol USBProtective Illustrated HCD relies only on DMA and IOMEM. Go back to Makefile to determine which files USB_OHCI_HCD will compile

Obj-$ (CONFIG_USB_OHCI_HCD) + = ohci-hcd.o

E, seeing here, we understand that to turn on the host function of s3c2410, you only need to compile the ohci-hcd.c and ohci-s3c2410.c files.

F, through observation, it is found that there is very little code for ohci-hcd.c and ohci-s3c2410.c. What is the reason for this? The following code is from ohci-hcd.c.

Static const char hcd_name [] = "ohci_hcd"; # define STATECHANGE_DELAY msecs_to_jiffies # define IO_WATCHDOG_DELAY msecs_to_jiffies # 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, by looking at the ohci-hcd.c file, it is found that it actually includes a lot of other ohci files. So how do registers operate? The following code comes from the ohci.h file.

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 (OMagerr) _ ohci_readl (oMagerr) # define ohci_writel (omeme vMagne r) _ ohci_writel (oMed vMagne r)

H, as you can see here, you should find that most of the underlying operations are actually done with the help of ohci. Each host driver is actually registered and tells where the mem address is. The following code is the code for the probe function in ohci-s3c2410.c.

Hcd- > regs = devm_ioremap_resource (& dev- > dev, & dev- > resource [0]); if (IS_ERR (hcd- > regs)) {retval = PTR_ERR (hcd- > regs); goto err_put;}

4. How to learn usb driver

In terms of code structure, the above analysis is an introduction. However, if you want to have an in-depth understanding of the usb host&device driver, in addition to the code logic, you should also familiarize yourself with the usb protocol manual and, more importantly, learn to use the catc protocol analyzer to really understand how usb sends and receives packets.

The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report