Files
luban-lite-t3e-pro/doc/topics/sdk/usb/usb-host-interface-usb_hub_driver.html

371 lines
24 KiB
HTML
Raw Normal View History

2025-01-23 16:37:00 +08:00
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-cn" lang="zh-cn" data-whc_version="26.0">
<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/><meta name="viewport" content="width=device-width, initial-scale=1.0"/><meta http-equiv="X-UA-Compatible" content="IE=edge"/><meta name="description" content="普通的 Usb Device 通过内部的 Interface 提供各种业务功能。而 Hub 这类特殊的 Usb Device 功能就一种,那就是监控端口的状态变化: 在端口上有设备 attach 时,创建新的 usb device给其适配驱动。如果是 hub device子 usb 驱动会进一步扫描端口。 在端口上有设备 deattach 时,移除掉对应的 usb device。如果是 hub ..."/><meta name="DC.rights.owner" content="(C) 版权 2025"/><meta name="copyright" content="(C) 版权 2025"/><meta name="generator" content="DITA-OT"/><meta name="DC.type" content="concept"/><meta name="DC.contributor" content="yan.wang"/><meta name="DC.date.modified" content="2024-02-04"/><meta name="DC.format" content="HTML5"/><meta name="DC.identifier" content="usb_host_interface_usb_hub_driver"/><meta name="DC.language" content="zh-CN"/><title>USB Hub Driver</title><!-- Build number 2023110923. --><meta name="wh-path2root" content="../../../"/><meta name="wh-toc-id" content=""/><meta name="wh-source-relpath" content="topics/sdk/usb/usb-host-interface-usb_hub_driver.dita"/><meta name="wh-out-relpath" content="topics/sdk/usb/usb-host-interface-usb_hub_driver.html"/>
<link rel="stylesheet" type="text/css" href="../../../webhelp/app/commons.css?buildId=2023110923"/>
<link rel="stylesheet" type="text/css" href="../../../webhelp/app/topic.css?buildId=2023110923"/>
<script src="../../../webhelp/app/options/properties.js?buildId=20250121171154"></script>
<script src="../../../webhelp/app/localization/strings.js?buildId=2023110923"></script>
<script src="../../../webhelp/app/search/index/keywords.js?buildId=20250121171154"></script>
<script defer="defer" src="../../../webhelp/app/commons.js?buildId=2023110923"></script>
<script defer="defer" src="../../../webhelp/app/topic.js?buildId=2023110923"></script>
<link rel="stylesheet" type="text/css" href="../../../webhelp/template/aic-styles-web.css?buildId=2023110923"/><link rel="stylesheet" type="text/css" href="../../../webhelp/template/notes.css?buildId=2023110923"/><link rel="stylesheet" type="text/css" href="../../../webhelp/template/aic-common.css?buildId=2023110923"/><link rel="stylesheet" type="text/css" href="../../../webhelp/template/aic-images.css?buildId=2023110923"/><link rel="stylesheet" type="text/css" href="../../../webhelp/template/footnote.css?buildId=2023110923"/><link rel="stylesheet" type="text/css" href="../../../webhelp/template/aic-web-watermark.css?buildId=2023110923"/><link rel="stylesheet" type="text/css" href="../../../webhelp/template/topic-body-list.css?buildId=2023110923"/></head>
<body id="usb_host_interface_usb_hub_driver" class="wh_topic_page frmBody">
<a href="#wh_topic_body" class="sr-only sr-only-focusable">
跳转到主要内容
</a>
<header class="navbar navbar-default wh_header">
<div class="container-fluid">
<div class="wh_header_flex_container navbar-nav navbar-expand-md navbar-dark">
<div class="wh_logo_and_publication_title_container">
<div class="wh_logo_and_publication_title">
<a href="http://www.artinchip.com" class=" wh_logo d-none d-sm-block "><img src="../../../company-logo-white.png" alt="RTOS SDK 使用指南SDK 指南文件"/></a>
<div class=" wh_publication_title "><a href="../../../index.html"><span class="booktitle"><span class="ph mainbooktitle">RTOS SDK 使用指南</span><span class="ph booktitlealt">SDK 指南文件</span></span></a></div>
</div>
</div>
<div class="wh_top_menu_and_indexterms_link collapse navbar-collapse" id="wh_top_menu_and_indexterms_link">
</div>
</div>
</div>
</header>
<div class=" wh_search_input navbar-form wh_topic_page_search search " role="form">
<form id="searchForm" method="get" role="search" action="../../../search.html"><div><input type="search" placeholder="搜索 " class="wh_search_textfield" id="textToSearch" name="searchQuery" aria-label="搜索查询" required="required"/><button type="submit" class="wh_search_button" aria-label="搜索"><span class="search_input_text">搜索</span></button></div></form>
</div>
<div class="container-fluid" id="wh_topic_container">
<div class="row">
<nav class="wh_tools d-print-none navbar-expand-md" aria-label="Tools">
<div data-tooltip-position="bottom" class=" wh_breadcrumb "></div>
<div class="wh_right_tools">
<button class="wh_hide_highlight" aria-label="切换搜索突出显示" title="切换搜索突出显示"></button>
<button class="webhelp_expand_collapse_sections" data-next-state="collapsed" aria-label="折叠截面" title="折叠截面"></button>
<div class=" wh_print_link print d-none d-md-inline-block "><button onClick="window.print()" title="打印此页" aria-label="打印此页"></button></div>
</div>
</nav>
</div>
<div class="wh_content_area">
<div class="row">
<div class="col-lg-10 col-md-10 col-sm-10 col-xs-12" id="wh_topic_body">
<button id="wh_close_topic_toc_button" class="close-toc-button d-none" aria-label="Toggle topic table of content" aria-controls="wh_topic_toc" aria-expanded="true">
<span class="close-toc-icon-container">
<span class="close-toc-icon"></span>
</span>
</button>
<div class=" wh_topic_content body "><main role="main"><article class="- topic/topic concept/concept topic concept" role="article" aria-labelledby="ariaid-title1"><span class="edit-link" style="font-size:12px; opacity:0.6; text-align:right; vertical-align:middle"><a target="_blank" href="http://172.16.35.88/tasks/jdssno1uvvbf2mltu9kb9v3if05d5gopuakboe8hlud18rma/edit/F:/aicdita/aicdita-cn/topics/sdk/usb/usb-host-interface-usb_hub_driver.dita">Edit online</a></span><h1 class="- topic/title title topictitle1" id="ariaid-title1">USB Hub Driver</h1><div class="date inPage">4 Feb 2024</div><div style="color: gray;">
Read time: 6 minute(s)
</div><div class="- topic/body concept/conbody body conbody"><div class="- topic/p p" data-ofbid="d295865e22__20250121171801"><br/><div class="imagecenter"><img class="- topic/image image imagecenter" id="usb_host_interface_usb_hub_driver__image_dns_14z_21c" src="../../../images/usb/usb_hub.png" alt="image3"/></div><br/></div><p class="- topic/p p" data-ofbid="d295865e26__20250121171801">普通的 Usb Device 通过内部的 Interface 提供各种业务功能。而 Hub 这类特殊的 Usb Device 功能就一种,那就是监控端口的状态变化:</p><ul class="- topic/ul ul" id="usb_host_interface_usb_hub_driver__ul_ens_14z_21c" data-ofbid="usb_host_interface_usb_hub_driver__ul_ens_14z_21c"><li class="- topic/li li" data-ofbid="d295865e29__20250121171801">
<p class="- topic/p p" data-ofbid="d295865e31__20250121171801">在端口上有设备 attach 时,创建新的 usb device给其适配驱动。如果是 hub device子 usb 驱动会进一步扫描端口。</p>
</li><li class="- topic/li li" data-ofbid="d295865e34__20250121171801">
<p class="- topic/p p" data-ofbid="d295865e36__20250121171801">在端口上有设备 deattach 时,移除掉对应的 usb device。如果是 hub device 进一步移除其所有的子 usb device。</p>
</li></ul><div class="- topic/p p" data-ofbid="d295865e39__20250121171801">Hub 也是标准的 Usb Device它也是标准的流程被上一级设备发现后 <code class="+ topic/ph pr-d/codeph ph codeph">创建 Usb Device</code><code class="+ topic/ph pr-d/codeph ph codeph">创建 Usb
Interface</code> ,然后被 <code class="+ topic/ph pr-d/codeph ph codeph">Usb Hub Interface</code> Driver 给适配到。系统中只有一个 Hub
驱动:<pre class="+ topic/pre pr-d/codeblock pre codeblock language-c" id="usb_host_interface_usb_hub_driver__codeblock_abf_34z_21c" data-ofbid="usb_host_interface_usb_hub_driver__codeblock_abf_34z_21c"><strong class="hl-keyword">static</strong> <strong class="hl-keyword">const</strong> <strong class="hl-keyword">struct</strong> usb_device_id hub_id_table[] = {
{ .match_flags = USB_DEVICE_ID_MATCH_VENDOR
| USB_DEVICE_ID_MATCH_PRODUCT
| USB_DEVICE_ID_MATCH_INT_CLASS,
.idVendor = USB_VENDOR_SMSC,
.idProduct = USB_PRODUCT_USB5534B,
.bInterfaceClass = USB_CLASS_HUB,
.driver_info = HUB_QUIRK_DISABLE_AUTOSUSPEND},
{ .match_flags = USB_DEVICE_ID_MATCH_VENDOR
| USB_DEVICE_ID_MATCH_INT_CLASS,
.idVendor = USB_VENDOR_GENESYS_LOGIC,
.bInterfaceClass = USB_CLASS_HUB,
.driver_info = HUB_QUIRK_CHECK_PORT_AUTOSUSPEND},
{ .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
.bDeviceClass = USB_CLASS_HUB},
{ .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
.bInterfaceClass = USB_CLASS_HUB},
{ } <em class="hl-comment">/* Terminating entry */</em>
};
MODULE_DEVICE_TABLE(usb, hub_id_table);
<strong class="hl-keyword">static</strong> <strong class="hl-keyword">struct</strong> usb_driver hub_driver = {
.name = <span class="hl-string">"hub"</span>,
.probe = hub_probe,
.disconnect = hub_disconnect,
.suspend = hub_suspend,
.resume = hub_resume,
.reset_resume = hub_reset_resume,
.pre_reset = hub_pre_reset,
.post_reset = hub_post_reset,
.unlocked_ioctl = hub_ioctl,
.id_table = hub_id_table,
.supports_autosuspend = <span class="hl-number">1</span>,
};</pre></div><p class="- topic/p p" data-ofbid="d295865e52__20250121171801">hub_driver 驱动启动以后,只做一件事情发送一个查询端口状态的 <code class="+ topic/ph pr-d/codeph ph codeph">urb</code> </p><pre class="+ topic/pre pr-d/codeblock pre codeblock language-c" id="usb_host_interface_usb_hub_driver__pre_gns_14z_21c" data-ofbid="usb_host_interface_usb_hub_driver__pre_gns_14z_21c">hub_probe() → hub_configure():
<strong class="hl-keyword">static</strong> <strong class="hl-keyword">int</strong> hub_configure(<strong class="hl-keyword">struct</strong> usb_hub *hub,
<strong class="hl-keyword">struct</strong> usb_endpoint_descriptor *endpoint)
{
<em class="hl-comment">/* (1) 分配 urb */</em>
hub-&gt;urb = usb_alloc_urb(<span class="hl-number">0</span>, GFP_KERNEL);
<strong class="hl-keyword">if</strong> (!hub-&gt;urb) {
ret = -ENOMEM;
<strong class="hl-keyword">goto</strong> fail;
}
<em class="hl-comment">/* (2) 初始化 urb作用就是通过 ep0 查询 hub 的端口状态
urb 的回调函数是 hub_irq()
*/</em>
usb_fill_int_urb(hub-&gt;urb, hdev, pipe, *hub-&gt;buffer, maxp, hub_irq,
hub, endpoint-&gt;bInterval);
<em class="hl-comment">/* (3) 发送 urb */</em>
hub_activate(hub, HUB_INIT);
}
<strong class="hl-keyword">static</strong> <strong class="hl-keyword">void</strong> hub_activate(<strong class="hl-keyword">struct</strong> usb_hub *hub, <strong class="hl-keyword">enum</strong> hub_activation_type type)
{
<em class="hl-comment">/* (3.1) 提交 urb */</em>
status = usb_submit_urb(hub-&gt;urb, GFP_NOIO);
}
</pre><section class="- topic/section section" id="usb_host_interface_usb_hub_driver__section_hns_14z_21c" data-ofbid="usb_host_interface_usb_hub_driver__section_hns_14z_21c"><h2 class="- topic/title title sectiontitle">Normal Hub Port op</h2>
<p class="- topic/p p" data-ofbid="d295865e64__20250121171801">在普通的 hub 中,端口操作是通过标准的 urb 发起 usb ep0 读写。分为两类:</p>
<ul class="- topic/ul ul" id="usb_host_interface_usb_hub_driver__ul_ins_14z_21c" data-ofbid="usb_host_interface_usb_hub_driver__ul_ins_14z_21c"><li class="- topic/li li" data-ofbid="d295865e68__20250121171801"><p class="- topic/p p" data-ofbid="d295865e69__20250121171801">通过轮询读取 <em class="+ topic/ph hi-d/i ph i">Hub Class-specific Requests</em> 配置来查询端口的状态:</p><div class="- topic/p p" data-ofbid="d295865e74__20250121171801"><br/><div class="imagecenter"><img class="- topic/image image imagecenter" id="usb_host_interface_usb_hub_driver__image_jns_14z_21c" src="../../../images/usb/usb_hub_get_port.png" alt="image4"/></div><br/></div></li><li class="- topic/li li" data-ofbid="d295865e78__20250121171801"><p class="- topic/p p" data-ofbid="d295865e79__20250121171801">设置和使能端口也是通过 <em class="+ topic/ph hi-d/i ph i">Hub Class-specific Requests</em> 中相应的命令实现的:</p><div class="- topic/p p" data-ofbid="d295865e84__20250121171801"><br/><div class="imagecenter"><img class="- topic/image image imagecenter" id="usb_host_interface_usb_hub_driver__image_lns_14z_21c" src="../../../images/usb/usb_hub_set_port.png" alt="image5"/></div><br/></div></li></ul>
</section><section class="- topic/section section" id="usb_host_interface_usb_hub_driver__section_mns_14z_21c" data-ofbid="usb_host_interface_usb_hub_driver__section_mns_14z_21c"><h2 class="- topic/title title sectiontitle">RootHub Port op</h2>
<p class="- topic/p p" data-ofbid="d295865e94__20250121171801">而对于 roothub 来说,对端口的操作的 urb 都需要特殊处理 (以 EHCI 的驱动为例)</p>
<ul class="- topic/ul ul" id="usb_host_interface_usb_hub_driver__ul_nns_14z_21c" data-ofbid="usb_host_interface_usb_hub_driver__ul_nns_14z_21c"><li class="- topic/li li" data-ofbid="d295865e98__20250121171801">
<div class="- topic/p p" data-ofbid="d295865e100__20250121171801">端口状态的变化可以通过 HCD
触发中断再上报:<pre class="+ topic/pre pr-d/codeblock pre codeblock language-c" id="usb_host_interface_usb_hub_driver__pre_ons_14z_21c" data-ofbid="usb_host_interface_usb_hub_driver__pre_ons_14z_21c">ehci_irq() → usb_hcd_poll_rh_status() :
<strong class="hl-keyword">void</strong> usb_hcd_poll_rh_status(<strong class="hl-keyword">struct</strong> usb_hcd *hcd)
{
<em class="hl-comment">/* (1) 获取端口状态的变化 */</em>
length = hcd-&gt;driver-&gt;hub_status_data(hcd, buffer);
<strong class="hl-keyword">if</strong> (length &gt; <span class="hl-number">0</span>) {
<em class="hl-comment">/* try to complete the status urb */</em>
spin_lock_irqsave(&amp;hcd_root_hub_lock, flags);
<em class="hl-comment">/* (2) 通过回复 hcd-&gt;status_urb 来进行上报 */</em>
urb = hcd-&gt;status_urb;
<strong class="hl-keyword">if</strong> (urb) {
clear_bit(HCD_FLAG_POLL_PENDING, &amp;hcd-&gt;flags);
hcd-&gt;status_urb = NULL;
urb-&gt;actual_length = length;
memcpy(urb-&gt;transfer_buffer, buffer, length);
usb_hcd_unlink_urb_from_ep(hcd, urb);
usb_hcd_giveback_urb(hcd, urb, <span class="hl-number">0</span>);
} <strong class="hl-keyword">else</strong> {
length = <span class="hl-number">0</span>;
set_bit(HCD_FLAG_POLL_PENDING, &amp;hcd-&gt;flags);
}
spin_unlock_irqrestore(&amp;hcd_root_hub_lock, flags);
}
}
hcd-&gt;driver-&gt;hub_status_data() → ehci_hub_status_data():
<strong class="hl-keyword">static</strong> <strong class="hl-keyword">int</strong>
ehci_hub_status_data (<strong class="hl-keyword">struct</strong> usb_hcd *hcd, <strong class="hl-keyword">char</strong> *buf)
{
<em class="hl-comment">/* (1.1) 通过 HCD 驱动,获取 roothub 端口的状态 */</em>
}
</pre></div>
</li><li class="- topic/li li" data-ofbid="d295865e105__20250121171801">
<div class="- topic/p p" data-ofbid="d295865e107__20250121171801">设置和使能端口需要嫁接到 HCD
驱动相关函数上实现:<pre class="+ topic/pre pr-d/codeblock pre codeblock language-c" id="usb_host_interface_usb_hub_driver__pre_qns_14z_21c" data-ofbid="usb_host_interface_usb_hub_driver__pre_qns_14z_21c">usb_hcd_submit_urb() → rh_urb_enqueue() → rh_call_control() → hcd-&gt;driver-&gt;hub_control() → ehci_hub_control():
<strong class="hl-keyword">int</strong> ehci_hub_control(
<strong class="hl-keyword">struct</strong> usb_hcd *hcd,
u16 typeReq,
u16 wValue,
u16 wIndex,
<strong class="hl-keyword">char</strong> *buf,
u16 wLength
) {
<em class="hl-comment">/* (1) 通过 HCD 驱动,设置 roothub 的端口 */</em>
}
</pre></div>
</li></ul>
</section><section class="- topic/section section" id="usb_host_interface_usb_hub_driver__section_j4w_y4z_21c" data-ofbid="usb_host_interface_usb_hub_driver__section_j4w_y4z_21c"><h2 class="- topic/title title sectiontitle">Device Attach</h2>
<div class="- topic/p p" data-ofbid="d295865e118__20250121171801">
<pre class="+ topic/pre pr-d/codeblock pre codeblock language-c" id="usb_host_interface_usb_hub_driver__pre_rns_14z_21c" data-ofbid="usb_host_interface_usb_hub_driver__pre_rns_14z_21c">hub_event() → port_event() → hub_port_connect_change() → hub_port_connect():
<strong class="hl-keyword">static</strong> <strong class="hl-keyword">void</strong> hub_port_connect(<strong class="hl-keyword">struct</strong> usb_hub *hub, <strong class="hl-keyword">int</strong> port1, u16 portstatus,
u16 portchange)
{
<strong class="hl-keyword">for</strong> (i = <span class="hl-number">0</span>; i &lt; PORT_INIT_TRIES; i++) {
<em class="hl-comment">/* (1) 给端口上新 Device 分配 `struct usb_device` 数据结构 */</em>
udev = usb_alloc_dev(hdev, hdev-&gt;bus, port1);
<strong class="hl-keyword">if</strong> (!udev) {
dev_err(&amp;port_dev-&gt;dev,
<span class="hl-string">"couldn't allocate usb_device\n"</span>);
<strong class="hl-keyword">goto</strong> done;
}
<em class="hl-comment">/* (2) 给新的 Device 分配一个新的 Address */</em>
choose_devnum(udev);
<strong class="hl-keyword">if</strong> (udev-&gt;devnum &lt;= <span class="hl-number">0</span>) {
status = -ENOTCONN; <em class="hl-comment">/* Don't retry */</em>
<strong class="hl-keyword">goto</strong> loop;
}
<em class="hl-comment">/* reset (non-USB 3.0 devices) and get descriptor */</em>
usb_lock_port(port_dev);
<em class="hl-comment">/* (3) 使能端口,并且调用 hub_set_address() 给 Device 配置上新分配的 Address */</em>
status = hub_port_init(hub, udev, port1, i);
usb_unlock_port(port_dev);
<em class="hl-comment">/* (4) 注册 `struct usb_device` */</em>
status = usb_new_device(udev);
}
}
</pre>
</div>
</section><section class="- topic/section section" id="usb_host_interface_usb_hub_driver__section_xzh_z4z_21c" data-ofbid="usb_host_interface_usb_hub_driver__section_xzh_z4z_21c"><h2 class="- topic/title title sectiontitle">Device Deattach</h2>
<div class="- topic/p p" data-ofbid="d295865e129__20250121171801">
<pre class="+ topic/pre pr-d/codeblock pre codeblock language-c" id="usb_host_interface_usb_hub_driver__pre_sns_14z_21c" data-ofbid="usb_host_interface_usb_hub_driver__pre_sns_14z_21c">hub_event() → port_event() → hub_port_connect_change() → hub_port_connect():
<strong class="hl-keyword">static</strong> <strong class="hl-keyword">void</strong> hub_port_connect(<strong class="hl-keyword">struct</strong> usb_hub *hub, <strong class="hl-keyword">int</strong> port1, u16 portstatus,
u16 portchange)
{
<em class="hl-comment">/* (1) 移除端口上的 `struct usb_device` */</em>
<strong class="hl-keyword">if</strong> (udev) {
<strong class="hl-keyword">if</strong> (hcd-&gt;usb_phy &amp;&amp; !hdev-&gt;parent)
usb_phy_notify_disconnect(hcd-&gt;usb_phy, udev-&gt;speed);
usb_disconnect(&amp;port_dev-&gt;child);
}
}
</pre>
</div>
</section></div></article></main></div>
</div>
<nav role="navigation" id="wh_topic_toc" aria-label="On this page" class="col-lg-2 d-none d-lg-block navbar d-print-none">
<div id="wh_topic_toc_content">
<div class=" wh_topic_toc "><div class="wh_topic_label">在本页上</div><ul><li class="section-item"><div class="section-title"><a href="#usb_host_interface_usb_hub_driver__section_hns_14z_21c" data-tocid="usb_host_interface_usb_hub_driver__section_hns_14z_21c">Normal Hub Port op</a></div></li><li class="section-item"><div class="section-title"><a href="#usb_host_interface_usb_hub_driver__section_mns_14z_21c" data-tocid="usb_host_interface_usb_hub_driver__section_mns_14z_21c">RootHub Port op</a></div></li><li class="section-item"><div class="section-title"><a href="#usb_host_interface_usb_hub_driver__section_j4w_y4z_21c" data-tocid="usb_host_interface_usb_hub_driver__section_j4w_y4z_21c">Device Attach</a></div></li><li class="section-item"><div class="section-title"><a href="#usb_host_interface_usb_hub_driver__section_xzh_z4z_21c" data-tocid="usb_host_interface_usb_hub_driver__section_xzh_z4z_21c">Device Deattach</a></div></li></ul></div>
</div>
</nav>
</div>
</div>
</div>
<footer class="navbar navbar-default wh_footer">
<div class=" footer-container mx-auto ">
<title>footer def</title>
<style><!--
.p1 {
font-family: FangZhengShuSong, Times, serif;
}
.p2 {
font-family: Arial, Helvetica, sans-serif;
}
.p3 {
font-family: "Lucida Console", "Courier New", monospace;
}
--></style>
<div class="webhelp.fragment.footer">
<p class="p1">Copyright © 2019-2024 广东匠芯创科技有限公司. All rights reserved.</p>
</div><div>
<div class="generation_time">
Update Time: 2025-01-21
</div>
</div>
</div>
</footer>
<div id="go2top" class="d-print-none">
<span class="oxy-icon oxy-icon-up"></span>
</div>
<div id="modal_img_large" class="modal">
<span class="close oxy-icon oxy-icon-remove"></span>
<div id="modal_img_container"></div>
<div id="caption"></div>
</div>
<script src="${pd}/publishing/publishing-styles-AIC-template/js/custom.js" defer="defer"></script>
</body>
</html>