Linux USB 3.0驱动分析(六)——USB主机控制器HCD分析
一.USB主机控制器HCD(Host Controller Device)简介
USB的主机控制器(HCD),出现了多种不同的类型,即OHCI和UHCI,EHCI,和xHCI,不同USB控制器类型OHCI,UHCI,EHCI,xHCI的区别和联系
USB采用树形拓扑结构,主机侧和设备侧的USB控制器分别称为主机控制器(Host Controller)和USB设备控制器(UDC),每条总线上只有一个主机控制器,负责协调主机和设备间的通信,设备不能主动向主机发送任何消息。
1.usb phy
二.USB主机控制器驱动
1.分析的usb主机控制器硬件情况
USB Host带有Root Hub,第一个USB设备是一个根集线器(Root_hub)它控制连接到其上的整个USB总线。
鉴于现在大部分设备都已经支持usb3.0, 我们来分析xHCI主机控制器驱动代码。
我手里的设备是imx8mq,用的DWC3 USB控制芯片
static struct platform_driver dwc3_driver = {
.probe = dwc3_probe,
.remove = dwc3_remove,
.driver = {
.name = "dwc3",
.of_match_table = of_match_ptr(of_dwc3_match),
.acpi_match_table = ACPI_PTR(dwc3_acpi_match),
.pm = &dwc3_dev_pm_ops,
},
};
module_platform_driver(dwc3_driver);
static int dwc3_probe(struct platform_device *pdev)
{
dwc3_get_properties(dwc); //获取一些属性,主要是读取dts里面的配置,比如dr_mode可以配置为otg、host或者peripheral。
dwc3_cache_hwparams(dwc); //读取一些硬件参数
ret = dwc3_core_init(dwc); //这里面是最重要的初始化,一些硬件初始化,包括USB PHY
if (ret) {
if (ret != -EPROBE_DEFER)
dev_err(dev, "failed to initialize core: %d\n", ret);
goto err4;
}
ret = dwc3_core_init_mode(dwc); //根据不同的模式进行初始化
if (ret)
goto err5;
dwc3_debugfs_init(dwc); //调试节点,/sys/kernel/debug/38100000.usb/
}
static int dwc3_core_init_mode(struct dwc3 *dwc)
{
switch (dwc->dr_mode) {
case USB_DR_MODE_PERIPHERAL: //外围模式,也就是当从设备
dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_DEVICE);
if (dwc->usb2_phy)
otg_set_vbus(dwc->usb2_phy->otg, false);
phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_DEVICE);
phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_DEVICE);
ret = dwc3_gadget_init(dwc);
if (ret) {
if (ret != -EPROBE_DEFER)
dev_err(dev, "failed to initialize gadget\n");
return ret;
}
break;
case USB_DR_MODE_HOST: //主机模式,也就是当主设备
dwc3_set_prtcap(dwc, DWC3_GCTL_PRTCAP_HOST);
if (dwc->usb2_phy)
otg_set_vbus(dwc->usb2_phy->otg, true);
phy_set_mode(dwc->usb2_generic_phy, PHY_MODE_USB_HOST);
phy_set_mode(dwc->usb3_generic_phy, PHY_MODE_USB_HOST);
ret = dwc3_host_init(dwc);
if (ret) {
if (ret != -EPROBE_DEFER)
dev_err(dev, "failed to initialize host\n");
return ret;
}
break;
case USB_DR_MODE_OTG: //otg模式,OTG控制器可以做host,也能做device
INIT_WORK(&dwc->drd_work, __dwc3_set_mode);
ret = dwc3_drd_init(dwc);
if (ret) {
if (ret != -EPROBE_DEFER)
dev_err(dev, "failed to initialize dual-role\n");
return ret;
}
break;
default:
dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode);
return -EINVAL;
}
return 0;
}
int dwc3_host_init(struct dwc3 *dwc)
{
xhci = platform_device_alloc("xhci-hcd", PLATFORM_DEVID_AUTO); //创建一个平台设备,名字是xhci-hcd
if (!xhci) {
dev_err(dwc->dev, "couldn't allocate xHCI device\n");
return -ENOMEM;
}
ret = platform_device_add_resources(xhci, dwc->xhci_resources,
DWC3_XHCI_RESOURCES_NUM); //添加资源到这个平台设备
if (ret) {
dev_err(dwc->dev, "couldn't add resources to xHCI device\n");
goto err;
}
/**
* WORKAROUND: dwc3 revisions <=3.00a have a limitation
* where Port Disable command doesn't work.
*
* The suggested workaround is that we avoid Port Disable
* completely.
*
* This following flag tells XHCI to do just that.
*/
if (dwc->revision <= DWC3_REVISION_300A)
props[prop_idx++].name = "quirk-broken-port-ped";
if (prop_idx) {
ret = platform_device_add_properties(xhci, props); //向平台设备添加内置属性
if (ret) {
dev_err(dwc->dev, "failed to add properties to xHCI\n");
goto err;
}
}
ret = platform_device_add(xhci); //把这个平台设备加入到系统,这样就可以与平台驱动匹配了
if (ret) {
dev_err(dwc->dev, "failed to register xHCI device\n");
goto err;
}
}
3.xHCI driver分析
前面已经分析到注册平台设备,我们这里分析xHCI驱动
usb/host/xhci-plat.c
static struct platform_driver usb_xhci_driver = {
.probe = xhci_plat_probe, //后面会调用
.remove = xhci_plat_remove,
.driver = {
.name = "xhci-hcd",
.pm = &xhci_plat_pm_ops,
.of_match_table = of_match_ptr(usb_xhci_of_match),
.acpi_match_table = ACPI_PTR(usb_xhci_acpi_match),
},
};
static int __init xhci_plat_init(void)
{
xhci_init_driver(&xhci_plat_hc_driver, &xhci_plat_overrides); //这里会初始化xhci_plat_hc_driver
return platform_driver_register(&usb_xhci_driver); //这里注册之后就会,根据name="xhci-hcd"匹配到之前的platform device后,执行xhci_plat_probe
}
我们先分析xhci_init_driver函数,看看做了什么
static const struct hc_driver xhci_hc_driver = { //大部分事情都是这里在干了
.description = "xhci-hcd",
.product_desc = "xHCI Host Controller",
.hcd_priv_size = sizeof(struct xhci_hcd),
/*
* generic hardware linkage
*/
.irq = xhci_irq,
.flags = HCD_MEMORY | HCD_DMA | HCD_USB3 | HCD_SHARED,
/*
* basic lifecycle operations
*/
.reset = NULL, /* set in xhci_init_driver() */
.start = xhci_run, //This function is called by the USB core when the HC driver is added
.stop = xhci_stop,
.shutdown = xhci_shutdown,
/*
* managing i/o requests and associated device resources
*/
.map_urb_for_dma = xhci_map_urb_for_dma,
.urb_enqueue = xhci_urb_enqueue,
.urb_dequeue = xhci_urb_dequeue,
.alloc_dev = xhci_alloc_dev,
.free_dev = xhci_free_dev,
.alloc_streams = xhci_alloc_streams,
.free_streams = xhci_free_streams,
.add_endpoint = xhci_add_endpoint,
.drop_endpoint = xhci_drop_endpoint,
.endpoint_disable = xhci_endpoint_disable,
.endpoint_reset = xhci_endpoint_reset,
.check_bandwidth = xhci_check_bandwidth,
.reset_bandwidth = xhci_reset_bandwidth,
.address_device = xhci_address_device,
.enable_device = xhci_enable_device,
.update_hub_device = xhci_update_hub_device,
.reset_device = xhci_discover_or_reset_device,
/*
* scheduling support
*/
.get_frame_number = xhci_get_frame,
/*
* root hub support
*/
.hub_control = xhci_hub_control,
.hub_status_data = xhci_hub_status_data,
.bus_suspend = xhci_bus_suspend,
.bus_resume = xhci_bus_resume,
.get_resuming_ports = xhci_get_resuming_ports,
/*
* call back when device connected and addressed
*/
.update_device = xhci_update_device,
.set_usb2_hw_lpm = xhci_set_usb2_hardware_lpm,
.enable_usb3_lpm_timeout = xhci_enable_usb3_lpm_timeout,
.disable_usb3_lpm_timeout = xhci_disable_usb3_lpm_timeout,
.find_raw_port_number = xhci_find_raw_port_number,
.clear_tt_buffer_complete = xhci_clear_tt_buffer_complete,
.submit_single_step_set_feature = xhci_submit_single_step_set_feature,
};
void xhci_init_driver(struct hc_driver *drv,
const struct xhci_driver_overrides *over)
{
BUG_ON(!over);
/* Copy the generic table to drv then apply the overrides */
*drv = xhci_hc_driver; //赋值给drv
if (over) {
drv->hcd_priv_size += over->extra_priv_size;
if (over->reset)
drv->reset = over->reset;
if (over->start)
drv->start = over->start;
if (over->bus_suspend)
drv->bus_suspend = over->bus_suspend;
}
}
我们现在分析一下xhci_plat_probe,主要是创建和注册hcd
static int xhci_plat_probe(struct platform_device *pdev)
{
hcd = __usb_create_hcd(driver, sysdev, &pdev->dev,
dev_name(&pdev->dev), NULL); //创建一个usb_hcd结构体,并进行一些赋值操作, usb2.0(main_hcd)
if (!hcd) {
ret = -ENOMEM;
goto disable_runtime;
}
xhci = hcd_to_xhci(hcd);
xhci->main_hcd = hcd;
xhci->shared_hcd = __usb_create_hcd(driver, sysdev, &pdev->dev,
dev_name(&pdev->dev), hcd); //对应usb3.0及以上(shared_hcd)。
hcd->usb_phy = devm_usb_get_phy_by_phandle(sysdev, "usb-phy", 0);
if (IS_ERR(hcd->usb_phy)) {
ret = PTR_ERR(hcd->usb_phy);
if (ret == -EPROBE_DEFER)
goto put_usb3_hcd;
hcd->usb_phy = NULL;
} else {
ret = usb_phy_init(hcd->usb_phy); //这里调用usb phy的初始化函数。imx8mq用的imx8mq_usb_phy_init。usb phy驱动比较简单,我就不分析了
if (ret)
goto put_usb3_hcd;
}
ret = usb_add_hcd(hcd, irq, IRQF_SHARED); //完成通用HCD结构初始化和注册,这里是usb2.0
if (ret)
goto disable_usb_phy;
ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED); //完成通用HCD结构初始化和注册,这里是usb3.0
if (ret)
goto dealloc_usb2_hcd;
}
我们来看看usb_add_hcd里面做了什么?主要是注册分配roothub, 还有初始化usb phy,把usb总线加入到系统.
int usb_add_hcd(struct usb_hcd *hcd,
unsigned int irqnum, unsigned long irqflags)
{
if (!hcd->skip_phy_initialization && usb_hcd_is_primary_hcd(hcd)) {
hcd->phy_roothub = usb_phy_roothub_alloc(hcd->self.sysdev); //分配一个usb_phy_roothub结构体,并加入到roothub_entry->list链表
if (IS_ERR(hcd->phy_roothub))
return PTR_ERR(hcd->phy_roothub);
retval = usb_phy_roothub_init(hcd->phy_roothub); //初始化roothub,主要是调用imx8m_usb_phy_init
if (retval)
return retval;
retval = usb_phy_roothub_set_mode(hcd->phy_roothub,
PHY_MODE_USB_HOST_SS); //看起来是什么都没有做
if (retval)
retval = usb_phy_roothub_set_mode(hcd->phy_roothub,
PHY_MODE_USB_HOST); //看起来是什么都没有做
if (retval)
goto err_usb_phy_roothub_power_on;
retval = usb_phy_roothub_power_on(hcd->phy_roothub); //给roothub上电,调用imx8mq_phy_power_on
if (retval)
goto err_usb_phy_roothub_power_on;
}
/*此功能允许您控制在一个系统中是否使用一个USB设备。这个特性将允许您实现锁定USB设备,完全由用户空间控制。
到目前为止,当一个USB设备连接后,经过配置,它会立即暴露给给用户。使用这个特性时,只有root用户授权后的设备才有可能被使用。*/
switch (authorized_default) {
case USB_AUTHORIZE_NONE:
hcd->dev_policy = USB_DEVICE_AUTHORIZE_NONE;
break;
case USB_AUTHORIZE_ALL:
hcd->dev_policy = USB_DEVICE_AUTHORIZE_ALL;
break;
case USB_AUTHORIZE_INTERNAL:
hcd->dev_policy = USB_DEVICE_AUTHORIZE_INTERNAL;
break;
case USB_AUTHORIZE_WIRED:
default:
hcd->dev_policy = hcd->wireless ?
USB_DEVICE_AUTHORIZE_NONE : USB_DEVICE_AUTHORIZE_ALL;
break;
}
/* HC is in reset state, but accessible. Now do the one-time init,
* bottom up so that hcds can customize the root hubs before hub_wq
* starts talking to them. (Note, bus id is assigned early too.)
*/
retval = hcd_buffer_create(hcd); //分配一个内存池给DMA用
if (retval != 0) {
dev_dbg(hcd->self.sysdev, "pool alloc failed\n");
goto err_create_buf;
}
retval = usb_register_bus(&hcd->self); //使用USB核心注册USB主机控制器,把usb总线加入到系统
if (retval < 0)
goto err_register_bus;
rhdev = usb_alloc_dev(NULL, &hcd->self, 0); //分配一个hub设备
if (rhdev == NULL) {
dev_err(hcd->self.sysdev, "unable to allocate root hub\n");
retval = -ENOMEM;
goto err_allocate_root_hub;
}
mutex_lock(&usb_port_peer_mutex);
hcd->self.root_hub = rhdev;
mutex_unlock(&usb_port_peer_mutex);
switch (hcd->speed) { //判断属于的usb速率
case HCD_USB11:
rhdev->speed = USB_SPEED_FULL;
break;
case HCD_USB2:
rhdev->speed = USB_SPEED_HIGH;
break;
case HCD_USB25:
rhdev->speed = USB_SPEED_WIRELESS;
break;
case HCD_USB3:
rhdev->speed = USB_SPEED_SUPER;
break;
case HCD_USB32:
rhdev->rx_lanes = 2;
rhdev->tx_lanes = 2;
/* fall through */
case HCD_USB31:
rhdev->speed = USB_SPEED_SUPER_PLUS;
break;
default:
retval = -EINVAL;
goto err_set_rh_speed;
}
/* initialize tasklets */
init_giveback_urb_bh(&hcd->high_prio_bh); //初始化tasklet, usb_giveback_urb_bh
init_giveback_urb_bh(&hcd->low_prio_bh);
/* enable irqs just before we start the controller,
* if the BIOS provides legacy PCI irqs.
*/
if (usb_hcd_is_primary_hcd(hcd) && irqnum) {
//xHCI规范说我们可以得到一个中断,如果HC在某种情况出现了错误,我们可能会从事件环中获取坏数据。这个中断不是用来探测插入了设备的
retval = usb_hcd_request_irqs(hcd, irqnum, irqflags); //申请中断,中断处理函数usb_hcd_irq,实际调用xhci_irq
if (retval)
goto err_request_irq;
}
retval = hcd->driver->start(hcd); //实际是调用xhci_run, 启动xhci host controller
if (retval < 0) {
dev_err(hcd->self.controller, "startup error %d\n", retval);
goto err_hcd_driver_start;
}
/* starting here, usbcore will pay attention to this root hub */
retval = register_root_hub(hcd); //注册一个root hub
if (retval != 0)
goto err_register_root_hub;
if (hcd->uses_new_polling && HCD_POLL_RH(hcd))
//如果驱动请求roothub中断传输,会用一个定时器轮询;否则由驱动在事件发生时调用usb_hcd_poll_rh_status()。
usb_hcd_poll_rh_status(hcd);
return retval;
}
我们来分析一下usb_hcd_poll_rh_status,这关系到一个usb设备插入的时候,如何通知hub。这个函数usb_hcd_poll_rh_status会一直使用定时器调用自己,如果读取到hub有变化,而且有提交的urb,就返回。
void usb_hcd_poll_rh_status(struct usb_hcd *hcd)
{
length = hcd->driver->hub_status_data(hcd, buffer); //这里会调用xhci_hub_status_data读取roothub的寄存器,返回数据buffer和length
if (length > 0) {
/* try to complete the status urb */
spin_lock_irqsave(&hcd_root_hub_lock, flags);
urb = hcd->status_urb;
if (urb) { //如果已经提交了获取状态的urb, 将状态值拷贝进入urb,并把urb giveback
clear_bit(HCD_FLAG_POLL_PENDING, &hcd->flags);
hcd->status_urb = NULL;
urb->actual_length = length;
memcpy(urb->transfer_buffer, buffer, length);
usb_hcd_unlink_urb_from_ep(hcd, urb); //从它的端点队列中移除一个URB
usb_hcd_giveback_urb(hcd, urb, 0);
} else { //若此时没有已经提交的urb,则设置poll_pending标志
length = 0;
set_bit(HCD_FLAG_POLL_PENDING, &hcd->flags);
}
spin_unlock_irqrestore(&hcd_root_hub_lock, flags);
}
/* The USB 2.0 spec says 256 ms. This is close enough and won't
* exceed that limit if HZ is 100. The math is more clunky than
* maybe expected, this is to make sure that all timers for USB devices
* fire at the same time to give the CPU a break in between */
if (hcd->uses_new_polling ? HCD_POLL_RH(hcd) : //这里hcd->uses_new_polling=1 HCD_POLL_RH(hcd)如果不等于0,会一直调用mod_timer
(length == 0 && hcd->status_urb != NULL))
//此时开启rh_timer.rh_timer的处理函数rh_timer_func,实际就是usb_hcd_poll_rh_status。
mod_timer (&hcd->rh_timer, (jiffies/(HZ/4) + 1) * (HZ/4));
}
参考:
USB驱动框架_WuYujun's blog-CSDN博客_usb驱动框架