linuxOS_D21X/doc/topics/sdk/usb/usb-gadget_device.html
2025-01-23 16:35:08 +08:00

553 lines
51 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-cn" lang="zh-cn" data-whc_version="27.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="上一节说过 Gadget Device 由 UDC Driver 创建。 dwc2_driver_probe() → usb_add_gadget_udc() → usb_add_gadget_udc_release() → usb_add_gadget() Gadget Device 的主要作用是提供了 Endpoint 资源,供 Function Layer 使用标准的 Gadget API ..."/><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.relation" content="../../../topics/sdk/usb/usb-device-device_layer.html"/><meta name="DC.relation" content="../../../topics/sdk/usb/usb-gadget_bus.html"/><meta name="DC.relation" content="../../../topics/sdk/usb/usb-gadget_driver_configfs.html"/><meta name="DC.contributor" content="yan.wang"/><meta name="DC.contributor" content="yan.wang"/><meta name="DC.date.modified" content="2024-01-15"/><meta name="DC.format" content="HTML5"/><meta name="DC.identifier" content="usb_gadget_device"/><meta name="DC.language" content="zh-CN"/><title>Gadget Device</title><!-- Generated with build number 2024112209. --><meta name="wh-path2root" content="../../../"/><meta name="wh-toc-id" content="usb_gadget_device-d4445e7846"/><meta name="wh-source-relpath" content="topics/sdk/usb/usb-gadget_device.dita"/><meta name="wh-out-relpath" content="topics/sdk/usb/usb-gadget_device.html"/>
<link rel="stylesheet" type="text/css" href="../../../webhelp/app/commons.css?buildId=2024112209"/>
<link rel="stylesheet" type="text/css" href="../../../webhelp/app/topic.css?buildId=2024112209"/>
<script src="../../../webhelp/app/options/properties.js?buildId=20250123154945"></script>
<script src="../../../webhelp/app/localization/strings.js?buildId=2024112209"></script>
<script src="../../../webhelp/app/search/index/keywords.js?buildId=20250123154945"></script>
<script defer="defer" src="../../../webhelp/app/commons.js?buildId=2024112209"></script>
<script defer="defer" src="../../../webhelp/app/topic.js?buildId=2024112209"></script>
<link rel="stylesheet" type="text/css" href="../../../webhelp/template/aic-styles-web.css?buildId=2024112209"/><link rel="stylesheet" type="text/css" href="../../../webhelp/template/notes.css?buildId=2024112209"/><link rel="stylesheet" type="text/css" href="../../../webhelp/template/aic-common.css?buildId=2024112209"/><link rel="stylesheet" type="text/css" href="../../../webhelp/template/aic-images.css?buildId=2024112209"/><link rel="stylesheet" type="text/css" href="../../../webhelp/template/footnote.css?buildId=2024112209"/><link rel="stylesheet" type="text/css" href="../../../webhelp/template/aic-web-watermark.css?buildId=2024112209"/><link rel="stylesheet" type="text/css" href="../../../webhelp/template/topic-body-list.css?buildId=2024112209"/></head>
<body id="usb_gadget_device" 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=" Linux SDK 使用指南 SDK 指南文件 "/></a>
<div class=" wh_publication_title "><a href="../../../index.html"><span class="booktitle"> <span class="ph mainbooktitle">Linux 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 "><ol class="d-print-none"><li><span class="home"><a href="../../../index.html"><span>主页</span></a></span></li><li><div class="topicref" data-id="id"><div class="title"><a href="../../../topics/sdk/chapter-advanced-app.html">高级应用</a><div class="wh-tooltip"><p class="shortdesc">系统、存储、多媒体、接口、安全等模块的详细配置和设计说明。</p></div></div></div></li><li><div class="topicref" data-id="concept_nww_hzh_pzb"><div class="title"><a href="../../../topics/chapter-title/chapter-interface-sdk.html">接口</a><div class="wh-tooltip"><p class="shortdesc">CAN、CIR、GPAI、GPIO、I2C、PSADC、PWM 等接口模块的介绍和使用说明。</p></div></div></div></li><li><div class="topicref" data-id="usb_user_guide"><div class="title"><a href="../../../topics/sdk/usb/usb_user_guide.html">USB 使用指南</a></div></div></li><li><div class="topicref" data-id="usb_device_subsystem_code_structure"><div class="title"><a href="../../../topics/sdk/usb/usb-device_subsystem_code_structure.html">USB Device 子系统代码架构</a></div></div></li><li><div class="topicref" data-id="usb_device_device_layer"><div class="title"><a href="../../../topics/sdk/usb/usb-device-device_layer.html">USB Device Layer</a></div></div></li><li class="active"><div class="topicref" data-id="usb_gadget_device"><div class="title"><a href="../../../topics/sdk/usb/usb-gadget_device.html">Gadget Device</a></div></div></li></ol></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_navigation_links "><span id="topic_navigation_links" class="navheader">
<span class="navprev"><a class="- topic/link link" href="../../../topics/sdk/usb/usb-gadget_bus.html" title="Gadget Bus" aria-label="上一主题: Gadget Bus" rel="prev"></a></span>
<span class="navnext"><a class="- topic/link link" href="../../../topics/sdk/usb/usb-gadget_driver_configfs.html" title="Gadget Driver (Configfs)" aria-label="下一主题: Gadget Driver (Configfs)" rel="next"></a></span> </span></div>
<div class=" wh_print_link print d-none d-md-inline-block "><button onClick="window.print()" title="打印此页" aria-label="打印此页"></button></div>
<button type="button" id="wh_toc_button" class="custom-toggler navbar-toggler collapsed wh_toggle_button navbar-light" aria-expanded="false" aria-label="Toggle publishing table of content" aria-controls="wh_publication_toc">
<span class="navbar-toggler-icon"></span>
</button>
</div>
</nav>
</div>
<div class="wh_content_area">
<div class="row">
<nav id="wh_publication_toc" class="col-lg-3 col-md-3 col-sm-12 d-md-block d-none d-print-none" aria-label="Table of Contents Container">
<div id="wh_publication_toc_content">
<div class=" wh_publication_toc " data-tooltip-position="right"><span class="expand-button-action-labels"><span id="button-expand-action" role="button" aria-label="Expand"></span><span id="button-collapse-action" role="button" aria-label="Collapse"></span><span id="button-pending-action" role="button" aria-label="Pending"></span></span><ul role="tree" aria-label="Table of Contents"><li role="treeitem"><div data-tocid="revinfo_linux-d4445e1079" class="topicref" data-id="revinfo_linux" data-state="leaf"><span role="button" class="wh-expand-btn"></span><div class="title"><a href="../../../topics/revinfo/revinfo_linux.html" id="revinfo_linux-d4445e1079-link">修订记录</a></div></div></li><li role="treeitem" aria-expanded="false"><div data-tocid="id-d4445e1096" class="topicref" data-id="id" data-state="not-ready"><span role="button" tabindex="0" aria-labelledby="button-expand-action id-d4445e1096-link" class="wh-expand-btn"></span><div class="title"><a href="../../../topics/sdk/env/sdk-compile.html" id="id-d4445e1096-link">SDK 编译</a><div class="wh-tooltip"><p class="shortdesc">介绍不同编译环境下 SDK 的详细编译流程。</p></div></div></div></li><li role="treeitem" aria-expanded="false"><div data-tocid="tocId-d4445e1240" class="topicref" data-state="not-ready"><span role="button" tabindex="0" aria-labelledby="button-expand-action tocId-d4445e1240-link" class="wh-expand-btn"></span><div class="title"><a href="../../../topics/sdk/advanced/lb_usage_commands.html" id="tocId-d4445e1240-link">使用指南</a></div></div></li><li role="treeitem" aria-expanded="false"><div data-tocid="concept_rcx_czh_pzb-d4445e1360" class="topicref" data-id="concept_rcx_czh_pzb" data-state="not-ready"><span role="button" tabindex="0" aria-labelledby="button-expand-action concept_rcx_czh_pzb-d4445e1360-link" class="wh-expand-btn"></span><div class="title"><a href="../../../topics/sdk/chapter-app.html" id="concept_rcx_czh_pzb-d4445e1360-link">应用场景</a><div class="wh-tooltip"><p class="shortdesc">描述了 SDK 在不同应用场景中的配置和使用包括系统更新、OTA、安全方案等。</p></div></div></div></li><li role="treeitem" aria-expanded="false"><div data-tocid="id-d4445e1678" class="topicref" data-id="id" data-state="not-ready"><span role="button" tabindex="0" aria-labelledby="button-expand-action id-d4445e1678-link" class="wh-expand-btn"></span><div class="title"><a href="../../../topics/sdk/peripheral/peripheral-intro.html" id="id-d4445e1678-link">外设移植</a><div class="wh-tooltip"><p class="shortdesc"><span class="ph">触摸屏、显示器、WIFI 模块、按键</span>等外设的介绍和使用说明。</p></div></div></div></li><li role="treeitem" aria-expanded="false"><div data-tocid="id-d4445e1964" class="topicref" data-id="id" data-state="not-ready"><span role="button" tabindex="0" aria-labelledby="button-expand-action id-d4445e1964-link" class="wh-expand-btn"></span><div class="title"><a href="../../../topics/sdk/bringup/chapter-bringup.html" id="id-d4445e1964-link">BringUp</a><div class="wh-tooltip"><p class="shortdesc">在硬件上电后快速初始化系统,为操作系统的启动准备好必要的硬件环境。</p></div></div></div></li><li role="treeitem" aria-expanded="true"><div data-tocid="id-d4445e2153" class="topicref" data-id="id" data-state="expanded"><span role="button" tabindex="0" aria-labelledby="button-collapse-action id-d4445e2153-link" class="wh-expand-btn"></span><div class="title"><a href="../../../topics/sdk/chapter-advanced-app.html" id="id-d4445e2153-link">高级应用</a><div class="wh-tooltip"><p class="shortdesc">系统、存储、多媒体、接口、安全等模块的详细配置和设计说明。</p></div></div></div><ul role="group" class="navbar-nav nav-list"><li role="treeitem" aria-expanded="false"><div data-tocid="uBoot-d4445e2170" class="topicref" data-id="uBoot" data-state="not-ready"><span role="button" tabindex="0" aria-labelledby="button-expand-action uBoot-d4445e2170-link" class="wh-expand-btn"></span><div class="title"><a href="../../../topics/sdk/boot/uboot-module.html" id="uBoot-d4445e2170-link">U-Boot</a><div class="wh-tooltip"><p class="shortdesc">启动支持的基本功能以及运行时的基本硬件环境。</p></div></div></div></li><li role="treeitem" aria-expanded="false"><div data-tocid="concept_mtx_tk3_pzb-d4445e3028" class="topicref" data-id="concept_mtx_tk3_pzb" data-state="not-ready"><span role="button" tabindex="0" aria-labelledby="button-expand-action concept_mtx_tk3_pzb-d4445e3028-link" class="wh-expand-btn"></span><div class="title"><a href="../../../topics/sdk/linux/chapter_linux.html" id="concept_mtx_tk3_pzb-d4445e3028-link">Linux</a></div></div></li><li role="treeitem" aria-expanded="false"><div data-tocid="chapter-system-d4445e3198" class="topicref" data-id="chapter-system" data-state="not-ready"><span role="button" tabindex="0" aria-labelledby="button-expand-action chapter-system-d4445e3198-link" class="wh-expand-btn"></span><div class="title"><a href="../../../topics/chapter-title/chapter-system.html" id="chapter-system-d4445e3198-link">系统</a><div class="wh-tooltip"><p class="shortdesc"></p></div></div></div></li><li role="treeitem" aria-expanded="false"><div data-tocid="chapter-memory-d4445e4054" class="topicref" data-id="chapter-memory" data-state="not-ready"><span role="button" tabindex="0" aria-labelledby="button-expand-action chapter-memory-d4445e4054-link" class="wh-expand-btn"></span><div class="title"><a href="../../../topics/chapter-title/chapter-memory-sdk.html" id="chapter-memory-d4445e4054-link">存储</a><div class="wh-tooltip"><p class="shortdesc">SDMC、SPI NAND、SPI NOR 等存储模块的介绍和使用说明。</p></div></div></div></li><li role="treeitem" aria-expanded="false"><div data-tocid="concept_mcc_32s_nbc-d4445e4438" class="topicref" data-id="concept_mcc_32s_nbc" data-state="not-ready"><span role="button" tabindex="0" aria-labelledby="button-expand-action concept_mcc_32s_nbc-d4445e4438-link" class="wh-expand-btn"></span><div class="title"><a href="../../../topics/chapter-title/chapter-multi-media-sdk.html" id="concept_mcc_32s_nbc-d4445e4438-link">多媒体</a><div class="wh-tooltip"><p class="shortdesc">GE、VE、Display、DVP、MPP、MPP 播放器等多媒体模块的介绍和使用说明。</p></div></div></div></li><li role="treeitem" aria-expanded="true"><div data-tocid="concept_nww_hzh_pzb-d4445e5947" class="topicref" data-id="concept_nww_hzh_pzb" data-state="expanded"><span role="button" tabindex="0" aria-labelledby="button-collapse-action concept_nww_hzh_pzb-d4445e5947-link" class="wh-expand-btn"></span><div class="title"><a href="../../../topics/chapter-title/chapter-interface-sdk.html" id="concept_nww_hzh_pzb-d4445e5947-link">接口</a><div class="wh-tooltip"><p class="shortdesc">CAN、CIR、GPAI、GPIO、I2C、PSADC、PWM 等接口模块的介绍和使用说明。</p></div></div></div><ul role="group" class="navbar-nav nav-list"><li role="treeitem" aria-expanded="false"><div data-tocid="can-d4445e5964" class="topicref" data-id="can" data-state="not-ready"><span role="button" tabindex="0" aria-labelledby="button-expand-action can-d4445e5964-link" class="wh-expand-btn"></span><div class="title"><a href="../../../topics/sdk/can/can_user_guide.html" id="can-d4445e5964-link">CAN 使用指南</a></div></div></li><li role="treeitem" aria-expanded="false"><div data-tocid="cir-d4445e6020" class="topicref" data-id="cir" data-state="not-ready"><span role="button" tabindex="0" aria-labelledby="button-expand-action cir-d4445e6020-link" class="wh-expand-btn"></span><div class="title"><a href="../../../topics/sdk/cir/cir_user_guide.html" id="cir-d4445e6020-link">CIR 使用指南</a></div></div></li><li role="treeitem" aria-expanded="false"><div data-tocid="gpai-user-guide-d4445e6146" class="topicref" data-id="gpai-user-guide" data-state="not-ready"><span role="button" tabindex="0" aria-labelledby="button-expand-action gpai-user-guide-d4445e6146-link" class="wh-expand-btn"></span><div class="title"><a href="../../../topics/sdk/gpai/gpai_user_guide.html" id="gpai-user-guide-d4445e6146-link">GPAI 使用指南</a></div></div></li><li role="treeitem" aria-expanded="false"><div data-tocid="i2c_user_guide-d4445e6288" class="topicref" data-id="i2c_user_guide" data-state="not-ready"><span role="button" tabindex="0" aria-labelledby="button-expand-action i2c_user_guide-d4445e6288-link" class="wh-expand-btn"></span><div class="title"><a href="../../../topics/sdk/i2c/i2c_user_guide.html" id="i2c_user_guide-d4445e6288-link">I2C 使用指南</a></div></div></li><li role="treeitem" aria-expanded="false"><div data-tocid="id-d4445e6400" class="topicref" data-id="id" data-state="not-ready"><span role="button" tabindex="0" aria-labelledby="button-expand-action id-d4445e6400-link" class="wh-expand-btn"></span><div class="title"><a href="../../../topics/sdk/mac/mac_user_guide.html" id="id-d4445e6400-link">MAC 使用指南</a></div></div></li><li role="treeitem" aria-expanded="false"><div data-tocid="pbus_user_guide-d4445e6514" class="topicref" data-id="pbus_user_guide" data-state="not-ready"><span role="button" tabindex="0" aria-labelledby="button-expand-action pbus_user_guide-d4445e6514-link" class="wh-expand-btn"></span><div class="title"><a href="../../../topics/sdk/pbus/pbus-user-guide.html" id="pbus_user_guide-d4445e6514-link">PBUS 使用指南</a></div></div></li><li role="treeitem" aria-expanded="false"><div data-tocid="pinctrl_user_guide-d4445e6598" class="topicref" data-id="pinctrl_user_guide" data-state="not-ready"><span role="button" tabindex="0" aria-labelledby="button-expand-action pinctrl_user_guide-d4445e6598-link" class="wh-expand-btn"></span><div class="title"><a href="../../../topics/sdk/pinctrl/pinctrl-user-guide.html" id="pinctrl_user_guide-d4445e6598-link">PINCTRL 使用指南</a></div></div></li><li role="treeitem" aria-expanded="false"><div data-tocid="id-d4445e6738" class="topicref" data-id="id" data-state="not-ready"><span role="button" tabindex="0" aria-labelledby="button-expand-action id-d4445e6738-link" class="wh-expand-btn"></span><div class="title"><a href="../../../topics/sdk/pwm/pwm_user_guide.html" id="id-d4445e6738-link">PWM 使用指南</a></div></div></li><li role="treeitem" aria-expanded="false"><div data-tocid="rtp-user-guide-d4445e6822" class="topicref" data-id="rtp-user-guide" data-state="not-ready"><span role="button" tabindex="0" aria-labelledby="button-expand-action rtp-user-guide-d4445e6822-link" class="wh-expand-btn"></span><div class="title"><a href="../../../topics/sdk/rtp/rtp_user_guide.html" id="rtp-user-guide-d4445e6822-link">RTP 使用指南</a></div></div></li><li role="treeitem" aria-expanded="false"><div data-tocid="spi-d4445e6962" class="topicref" data-id="spi" data-state="not-ready"><span role="button" tabindex="0" aria-labelledby="button-expand-action spi-d4445e6962-link" class="wh-expand-btn"></span><div class="title"><a href="../../../topics/sdk/spi/spi_user_guide.html" id="spi-d4445e6962-link">SPI 使用指南</a></div></div></li><li role="treeitem" aria-expanded="false"><div data-tocid="id-d4445e7075" class="topicref" data-id="id" data-state="not-ready"><span role="button" tabindex="0" aria-labelledby="button-expand-action id-d4445e7075-link" class="wh-expand-btn"></span><div class="title"><a href="../../../topics/sdk/psadc/psadc_user_guide.html" id="id-d4445e7075-link">PSADC 使用指南</a></div></div></li><li role="treeitem" aria-expanded="false"><div data-tocid="uart_user_guide-d4445e7215" class="topicref" data-id="uart_user_guide" data-state="not-ready"><span role="button" tabindex="0" aria-labelledby="button-expand-action uart_user_guide-d4445e7215-link" class="wh-expand-btn"></span><div class="title"><a href="../../../topics/sdk/uart/uart_user_guide.html" id="uart_user_guide-d4445e7215-link">UART 使用指南</a></div></div></li><li role="treeitem" aria-expanded="true"><div data-tocid="usb_user_guide-d4445e7299" class="topicref" data-id="usb_user_guide" data-state="expanded"><span role="button" tabindex="0" aria-labelledby="button-collapse-action usb_user_guide-d4445e7299-link" class="wh-expand-btn"></span><div class="title"><a href="../../../topics/sdk/usb/usb_user_guide.html" id="usb_user_guide-d4445e7299-link">USB 使用指南</a></div></div><ul role="group" class="navbar-nav nav-list"><li role="treeitem" aria-expanded="false"><div data-tocid="usb_configuration-d4445e7313" class="topicref" data-id="usb_configuration" data-state="not-ready"><span role="button" tabindex="0" aria-labelledby="button-expand-action usb_configuration-d4445e7313-link" class="wh-expand-btn"></span><div class="title"><a href="../../../topics/sdk/usb/usb_config.html" id="usb_configuration-d4445e7313-link">USB 配置</a></div></div></li><li role="treeitem"><div data-tocid="usb_debug_guide-d4445e7369" class="topicref" data-id="usb_debug_guide" data-state="leaf"><span role="button" class="wh-expand-btn"></span><div class="title"><a href="../../../topics/sdk/usb/usb-debug_guide.html" id="usb_debug_guide-d4445e7369-link">调试指南</a></div></div></li><li role="treeitem" aria-expanded="false"><div data-tocid="usb_test_guide-d4445e7383" class="topicref" data-id="usb_test_guide" data-state="not-ready"><span role="button" tabindex="0" aria-labelledby="button-expand-action usb_test_guide-d4445e7383-link" class="wh-expand-btn"></span><div class="title"><a href="../../../topics/sdk/usb/usb-test_guide.html" id="usb_test_guide-d4445e7383-link">测试指南</a></div></div></li><li role="treeitem" aria-expanded="false"><div data-tocid="usb_design_intro-d4445e7468" class="topicref" data-id="usb_design_intro" data-state="not-ready"><span role="button" tabindex="0" aria-labelledby="button-expand-action usb_design_intro-d4445e7468-link" class="wh-expand-btn"></span><div class="title"><a href="../../../topics/sdk/usb/usb-design_intro.html" id="usb_design_intro-d4445e7468-link">设计说明</a></div></div></li><li role="treeitem" aria-expanded="false"><div data-tocid="usb_host_subsystem_code_structure-d4445e7594" class="topicref" data-id="usb_host_subsystem_code_structure" data-state="not-ready"><span role="button" tabindex="0" aria-labelledby="button-expand-action usb_host_subsystem_code_structure-d4445e7594-link" class="wh-expand-btn"></span><div class="title"><a href="../../../topics/sdk/usb/usb-host_subsystem_code_structure.html" id="usb_host_subsystem_code_structure-d4445e7594-link">USB Host 子系统代码架构</a></div></div></li><li role="treeitem" aria-expanded="true"><div data-tocid="usb_device_subsystem_code_structure-d4445e7762" class="topicref" data-id="usb_device_subsystem_code_structure" data-state="expanded"><span role="button" tabindex="0" aria-labelledby="button-collapse-action usb_device_subsystem_code_structure-d4445e7762-link" class="wh-expand-btn"></span><div class="title"><a href="../../../topics/sdk/usb/usb-device_subsystem_code_structure.html" id="usb_device_subsystem_code_structure-d4445e7762-link">USB Device 子系统代码架构</a></div></div><ul role="group" class="navbar-nav nav-list"><li role="treeitem" aria-expanded="false"><div data-tocid="usb_device_controller_layer-d4445e7776" class="topicref" data-id="usb_device_controller_layer" data-state="not-ready"><span role="button" tabindex="0" aria-labelledby="button-expand-action usb_device_controller_layer-d4445e7776-link" class="wh-expand-btn"></span><div class="title"><a href="../../../topics/sdk/usb/usb-device-controller_layer.html" id="usb_device_controller_layer-d4445e7776-link">USB Device Controller Layer</a></div></div></li><li role="treeitem" aria-expanded="true"><div data-tocid="usb_device_device_layer-d4445e7818" class="topicref" data-id="usb_device_device_layer" data-state="expanded"><span role="button" tabindex="0" aria-labelledby="button-collapse-action usb_device_device_layer-d4445e7818-link" class="wh-expand-btn"></span><div class="title"><a href="../../../topics/sdk/usb/usb-device-device_layer.html" id="usb_device_device_layer-d4445e7818-link">USB Device Layer</a></div></div><ul role="group" class="navbar-nav nav-list"><li role="treeitem"><div data-tocid="usb_gadget_bus-d4445e7832" class="topicref" data-id="usb_gadget_bus" data-state="leaf"><span role="button" class="wh-expand-btn"></span><div class="title"><a href="../../../topics/sdk/usb/usb-gadget_bus.html" id="usb_gadget_bus-d4445e7832-link">Gadget Bus</a></div></div></li><li role="treeitem" class="active"><div data-tocid="usb_gadget_device-d4445e7846" class="topicref" data-id="usb_gadget_device" data-state="leaf"><span role="button" class="wh-expand-btn"></span><div class="title"><a href="../../../topics/sdk/usb/usb-gadget_device.html" id="usb_gadget_device-d4445e7846-link">Gadget Device</a></div></div></li><li role="treeitem"><div data-tocid="usb_gadget_driver_configfs-d4445e7860" class="topicref" data-id="usb_gadget_driver_configfs" data-state="leaf"><span role="button" class="wh-expand-btn"></span><div class="title"><a href="../../../topics/sdk/usb/usb-gadget_driver_configfs.html" id="usb_gadget_driver_configfs-d4445e7860-link">Gadget Driver (Configfs)</a></div></div></li><li role="treeitem"><div data-tocid="usb_gadget_driver_legacy-d4445e7874" class="topicref" data-id="usb_gadget_driver_legacy" data-state="leaf"><span role="button" class="wh-expand-btn"></span><div class="title"><a href="../../../topics/sdk/usb/usb-gadget_driver_legacy.html" id="usb_gadget_driver_legacy-d4445e7874-link">Gadget Driver Legacy</a></div></div></li></ul></li><li role="treeitem" aria-expanded="false"><div data-tocid="usb_device_interface_layer-d4445e7888" class="topicref" data-id="usb_device_interface_layer" data-state="not-ready"><span role="button" tabindex="0" aria-labelledby="button-expand-action usb_device_interface_layer-d4445e7888-link" class="wh-expand-btn"></span><div class="title"><a href="../../../topics/sdk/usb/usb-device-interface_layer.html" id="usb_device_interface_layer-d4445e7888-link">USB Interface Layer</a></div></div></li></ul></li><li role="treeitem"><div data-tocid="usb_common_issues-d4445e7930" class="topicref" data-id="usb_common_issues" data-state="leaf"><span role="button" class="wh-expand-btn"></span><div class="title"><a href="../../../topics/sdk/usb/usb_faq.html" id="usb_common_issues-d4445e7930-link">常见问题</a></div></div></li></ul></li></ul></li><li role="treeitem" aria-expanded="false"><div data-tocid="chapter-safety-d4445e7944" class="topicref" data-id="chapter-safety" data-state="not-ready"><span role="button" tabindex="0" aria-labelledby="button-expand-action chapter-safety-d4445e7944-link" class="wh-expand-btn"></span><div class="title"><a href="../../../topics/chapter-title/chapter-safety-sdk.html" id="chapter-safety-d4445e7944-link">安全</a><div class="wh-tooltip"><p class="shortdesc">SPI ENC、CE、eFuse 等安全模块的介绍和使用说明。</p></div></div></div></li></ul></li></ul></div>
</div>
</nav>
<div class="col-lg-7 col-md-9 col-sm-12" id="wh_topic_body">
<button id="wh_close_publication_toc_button" class="close-toc-button d-none" aria-label="Toggle publishing table of content" aria-controls="wh_publication_toc" aria-expanded="true">
<span class="close-toc-icon-container">
<span class="close-toc-icon"></span>
</span>
</button>
<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" title="Edit this document" href="http://172.16.35.88/tasks/jdssno1uvvbf2mltu9kb9v3if05d5gopuakboe8hlud18rma/edit/F:/aicdita/aicdita-cn/topics/sdk/usb/usb-gadget_device.dita">Edit online</a></span><h1 class="- topic/title title topictitle1" id="ariaid-title1">Gadget Device</h1>
<div class="date inPage">15 Jan 2024</div><div style="color: gray;">
Read time: 10 minute(s)
</div>
<div class="- topic/body concept/conbody body conbody">
<div class="- topic/p p" data-ofbid="d190487e33__20250123155220">上一节说过 Gadget Device 由 UDC Driver
创建。<pre class="+ topic/pre pr-d/codeblock pre codeblock language-c" id="usb_gadget_device__codeblock_rrv_zqz_21c" data-ofbid="usb_gadget_device__codeblock_rrv_zqz_21c"><code>dwc2_driver_probe() → usb_add_gadget_udc() → usb_add_gadget_udc_release() → usb_add_gadget()</code></pre></div>
<p class="- topic/p p" data-ofbid="d190487e38__20250123155220">Gadget Device 的主要作用是提供了 Endpoint 资源,供 Function Layer 使用标准的 Gadget API 来进行访问。</p>
<section class="- topic/section section" id="usb_gadget_device__section_uq1_zqz_21c" data-ofbid="usb_gadget_device__section_uq1_zqz_21c"><h2 class="- topic/title title sectiontitle">Endpoint Alloc</h2>
<p class="- topic/p p" data-ofbid="d190487e46__20250123155220">UDC Driver 在调用 usb_add_gadget_udc() 注册 Gadget Device 之前,初始化了 Gadget 的 Endpoint
资源链表:</p>
<pre class="+ topic/pre pr-d/codeblock pre codeblock language-c" id="usb_gadget_device__pre_vq1_zqz_21c" data-ofbid="usb_gadget_device__pre_vq1_zqz_21c"><code>dwc2_driver_probe() → dwc2_gadget_init():
<strong class="hl-keyword">int</strong> dwc2_gadget_init(<strong class="hl-keyword">struct</strong> dwc2_hsotg *hsotg)
{
<em class="hl-comment">/* (1) 初始化 Gadget Device 的 Endpoint 资源链表为空 */</em>
INIT_LIST_HEAD(&amp;hsotg-&gt;gadget.ep_list);
hsotg-&gt;gadget.ep0 = &amp;hsotg-&gt;eps_out[<span class="hl-number">0</span>]-&gt;ep;
<em class="hl-comment">/* initialise the endpoints now the core has been initialised */</em>
<em class="hl-comment">/* (2) 初始化 UDC 拥有的 Endpoint加入到 Gadget Device 的 Endpoint 资源链表中 */</em>
<strong class="hl-keyword">for</strong> (epnum = <span class="hl-number">0</span>; epnum &lt; hsotg-&gt;num_of_eps; epnum++) {
<strong class="hl-keyword">if</strong> (hsotg-&gt;eps_in[epnum])
dwc2_hsotg_initep(hsotg, hsotg-&gt;eps_in[epnum],
epnum, <span class="hl-number">1</span>);
<strong class="hl-keyword">if</strong> (hsotg-&gt;eps_out[epnum])
dwc2_hsotg_initep(hsotg, hsotg-&gt;eps_out[epnum],
epnum, <span class="hl-number">0</span>);
}
}
<strong class="hl-keyword">static</strong> <strong class="hl-keyword">void</strong> dwc2_hsotg_initep(<strong class="hl-keyword">struct</strong> dwc2_hsotg *hsotg,
<strong class="hl-keyword">struct</strong> dwc2_hsotg_ep *hs_ep,
<strong class="hl-keyword">int</strong> epnum,
bool dir_in)
{
INIT_LIST_HEAD(&amp;hs_ep-&gt;queue);
INIT_LIST_HEAD(&amp;hs_ep-&gt;ep.ep_list);
<em class="hl-comment">/* add to the list of endpoints known by the gadget driver */</em>
<em class="hl-comment">/* (2.1) UDC 中除了 endpoint0 以外,其他的 endpoint 都加入到 Device 的 Endpoint 资源链表 `gadget.ep_list` 中
endpoint0 的操作由 UDC 驱动自己来处理
*/</em>
<strong class="hl-keyword">if</strong> (epnum)
list_add_tail(&amp;hs_ep-&gt;ep.ep_list, &amp;hsotg-&gt;gadget.ep_list);
<em class="hl-comment">/* (2.2) 初始化 endpoint 的结构体成员 */</em>
hs_ep-&gt;parent = hsotg;
hs_ep-&gt;ep.name = hs_ep-&gt;name;
<strong class="hl-keyword">if</strong> (hsotg-&gt;params.speed == DWC2_SPEED_PARAM_LOW)
usb_ep_set_maxpacket_limit(&amp;hs_ep-&gt;ep, <span class="hl-number">8</span>);
<strong class="hl-keyword">else</strong>
usb_ep_set_maxpacket_limit(&amp;hs_ep-&gt;ep,
epnum ? <span class="hl-number">1024</span> : EP0_MPS_LIMIT);
<em class="hl-comment">/* (2.3) endpoint 最重要的结构体成员endpoint 操作函数集
endpoint 的相关操作最后调用到这些函数上
*/</em>
hs_ep-&gt;ep.ops = &amp;dwc2_hsotg_ep_ops;
<strong class="hl-keyword">if</strong> (epnum == <span class="hl-number">0</span>) {
hs_ep-&gt;ep.caps.type_control = true;
} <strong class="hl-keyword">else</strong> {
<strong class="hl-keyword">if</strong> (hsotg-&gt;params.speed != DWC2_SPEED_PARAM_LOW) {
hs_ep-&gt;ep.caps.type_iso = true;
hs_ep-&gt;ep.caps.type_bulk = true;
}
hs_ep-&gt;ep.caps.type_int = true;
}
<strong class="hl-keyword">if</strong> (dir_in)
hs_ep-&gt;ep.caps.dir_in = true;
<strong class="hl-keyword">else</strong>
hs_ep-&gt;ep.caps.dir_out = true;
}
</code></pre>
<p class="- topic/p p" data-ofbid="d190487e52__20250123155220">Gadget Device 准备好了 Endpoint 资源链表以后,通过 usb_add_gadget_udc() 注册。这样就可以 Function Layer
就可以通过调用 Gadget Api 来动态分配 Endpoint 了。例如:</p>
<pre class="+ topic/pre pr-d/codeblock pre codeblock language-c" id="usb_gadget_device__pre_wq1_zqz_21c" data-ofbid="usb_gadget_device__pre_wq1_zqz_21c"><code><strong class="hl-keyword">static</strong> <strong class="hl-keyword">int</strong>
acm_bind(<strong class="hl-keyword">struct</strong> usb_configuration *c, <strong class="hl-keyword">struct</strong> usb_function *f)
{
<em class="hl-comment">/* allocate instance-specific endpoints */</em>
<em class="hl-comment">/* (1) 从 Gadget Device 中分配一个 in endpoint */</em>
ep = usb_ep_autoconfig(cdev-&gt;gadget, &amp;acm_fs_in_desc);
<strong class="hl-keyword">if</strong> (!ep)
<strong class="hl-keyword">goto</strong> fail;
acm-&gt;port.in = ep;
<em class="hl-comment">/* (2) 从 Gadget Device 中分配一个 out endpoint */</em>
ep = usb_ep_autoconfig(cdev-&gt;gadget, &amp;acm_fs_out_desc);
<strong class="hl-keyword">if</strong> (!ep)
<strong class="hl-keyword">goto</strong> fail;
acm-&gt;port.out = ep;
<em class="hl-comment">/* (3) 从 Gadget Device 中分配一个 notify endpoint */</em>
ep = usb_ep_autoconfig(cdev-&gt;gadget, &amp;acm_fs_notify_desc);
<strong class="hl-keyword">if</strong> (!ep)
<strong class="hl-keyword">goto</strong> fail;
acm-&gt;notify = ep;
}
</code></pre>
<p class="- topic/p p" data-ofbid="d190487e59__20250123155220">其中通过 usb_ep_autoconfig() 函数从 Gadget Device 的 Endpoint 资源链表中分配空闲的 endpoint:</p>
<pre class="+ topic/pre pr-d/codeblock pre codeblock language-c" id="usb_gadget_device__pre_xq1_zqz_21c" data-ofbid="usb_gadget_device__pre_xq1_zqz_21c"><code>drivers\usb\gadget\function\f_acm.c:
usb_ep_autoconfig() → usb_ep_autoconfig_ss():
<strong class="hl-keyword">struct</strong> usb_ep *usb_ep_autoconfig_ss(
<strong class="hl-keyword">struct</strong> usb_gadget *gadget,
<strong class="hl-keyword">struct</strong> usb_endpoint_descriptor *desc,
<strong class="hl-keyword">struct</strong> usb_ss_ep_comp_descriptor *ep_comp
)
{
<strong class="hl-keyword">struct</strong> usb_ep *ep;
<strong class="hl-keyword">if</strong> (gadget-&gt;ops-&gt;match_ep) {
ep = gadget-&gt;ops-&gt;match_ep(gadget, desc, ep_comp);
<strong class="hl-keyword">if</strong> (ep)
<strong class="hl-keyword">goto</strong> found_ep;
}
<em class="hl-comment">/* Second, look at endpoints until an unclaimed one looks usable */</em>
<em class="hl-comment">/* (1) 从 Gadget Device 的 Endpoint 资源链表中查找一个空闲的(ep-&gt;claimed 为空) 且符合要求的 endpoint */</em>
list_for_each_entry (ep, &amp;gadget-&gt;ep_list, ep_list) {
<strong class="hl-keyword">if</strong> (usb_gadget_ep_match_desc(gadget, ep, desc, ep_comp))
<strong class="hl-keyword">goto</strong> found_ep;
}
<em class="hl-comment">/* Fail */</em>
<strong class="hl-keyword">return</strong> NULL;
found_ep:
...
ep-&gt;address = desc-&gt;bEndpointAddress;
ep-&gt;desc = NULL;
ep-&gt;comp_desc = NULL;
<em class="hl-comment">/* (2) 设置 endpoint 为已分配 */</em>
ep-&gt;claimed = true;
<strong class="hl-keyword">return</strong> ep;
}
</code></pre>
</section>
<section class="- topic/section section" id="usb_gadget_device__section_yq1_zqz_21c" data-ofbid="usb_gadget_device__section_yq1_zqz_21c"><h2 class="- topic/title title sectiontitle">EndPoint Access</h2>
<p class="- topic/p p" data-ofbid="d190487e71__20250123155220">Gadget Device 不仅仅为 Gadget Api 提供了分配 endpoint 的支持,还支持对 endpoint 收发数据的底层支持。在上一节的
endpoint 初始化时,就已经设置 endpoint 的操作函数集 <code class="+ topic/ph pr-d/codeph ph codeph">dwc2_hsotg_ep_ops</code> </p>
<pre class="+ topic/pre pr-d/codeblock pre codeblock language-c" id="usb_gadget_device__pre_zq1_zqz_21c" data-ofbid="usb_gadget_device__pre_zq1_zqz_21c"><code>dwc2_driver_probe() → dwc2_gadget_init() → dwc2_hsotg_initep():
<strong class="hl-keyword">static</strong> <strong class="hl-keyword">void</strong> dwc2_hsotg_initep(<strong class="hl-keyword">struct</strong> dwc2_hsotg *hsotg,
<strong class="hl-keyword">struct</strong> dwc2_hsotg_ep *hs_ep,
<strong class="hl-keyword">int</strong> epnum,
bool dir_in)
{
<em class="hl-comment">/* (2.3) endpoint 最重要的结构体成员endpoint 操作函数集
endpoint 的相关操作最后调用到这些函数上
*/</em>
hs_ep-&gt;ep.ops = &amp;dwc2_hsotg_ep_ops;
}
<strong class="hl-keyword">static</strong> <strong class="hl-keyword">const</strong> <strong class="hl-keyword">struct</strong> usb_ep_ops dwc2_hsotg_ep_ops = {
.enable = dwc2_hsotg_ep_enable,
.disable = dwc2_hsotg_ep_disable_lock,
.alloc_request = dwc2_hsotg_ep_alloc_request,
.free_request = dwc2_hsotg_ep_free_request,
.queue = dwc2_hsotg_ep_queue_lock,
.dequeue = dwc2_hsotg_ep_dequeue,
.set_halt = dwc2_hsotg_ep_sethalt_lock,
<em class="hl-comment">/* note, don't believe we have any call for the fifo routines */</em>
};
</code></pre>
<p class="- topic/p p" data-ofbid="d190487e80__20250123155220">Gadget Api 提供了以下接口来操作 endpoint 读写数据。在 Host 侧对 endpoint 进行一次操作请求的数据结构是 <code class="+ topic/ph pr-d/codeph ph codeph">struct
urb</code> ,而在 Device 侧也有类似的数据结构称为 <code class="+ topic/ph pr-d/codeph ph codeph">struct usb_request</code> ,对
endpoint 的数据读写就是围绕 <code class="+ topic/ph pr-d/codeph ph codeph">struct usb_request</code> 展开的:</p>
<pre class="+ topic/pre pr-d/codeblock pre codeblock language-c" id="usb_gadget_device__pre_ar1_zqz_21c" data-ofbid="usb_gadget_device__pre_ar1_zqz_21c"><code>drivers\usb\gadget\function\f_acm.c:
<strong class="hl-keyword">static</strong> <strong class="hl-keyword">int</strong> acm_cdc_notify(<strong class="hl-keyword">struct</strong> f_acm *acm, u8 type, u16 value,
<strong class="hl-keyword">void</strong> *data, <strong class="hl-keyword">unsigned</strong> length)
{
<strong class="hl-keyword">struct</strong> usb_ep *ep = acm-&gt;notify;
<strong class="hl-keyword">struct</strong> usb_request *req;
<strong class="hl-keyword">struct</strong> usb_cdc_notification *notify;
<strong class="hl-keyword">const</strong> <strong class="hl-keyword">unsigned</strong> len = <strong class="hl-keyword">sizeof</strong>(*notify) + length;
<strong class="hl-keyword">void</strong> *buf;
<strong class="hl-keyword">int</strong> status;
<em class="hl-comment">/* (1) 初始化 `struct usb_request` 数据结构 */</em>
req = acm-&gt;notify_req;
acm-&gt;notify_req = NULL;
acm-&gt;pending = false;
req-&gt;length = len;
notify = req-&gt;buf;
buf = notify + <span class="hl-number">1</span>;
notify-&gt;bmRequestType = USB_DIR_IN | USB_TYPE_CLASS
| USB_RECIP_INTERFACE;
notify-&gt;bNotificationType = type;
notify-&gt;wValue = cpu_to_le16(value);
notify-&gt;wIndex = cpu_to_le16(acm-&gt;ctrl_id);
notify-&gt;wLength = cpu_to_le16(length);
memcpy(buf, data, length);
<em class="hl-comment">/* ep_queue() can complete immediately if it fills the fifo... */</em>
spin_unlock(&amp;acm-&gt;lock);
<em class="hl-comment">/* (2) 提交 `usb_request` 请求到 endpoint 处理队列中 */</em>
status = usb_ep_queue(ep, req, GFP_ATOMIC);
spin_lock(&amp;acm-&gt;lock);
}
</code></pre>
<p class="- topic/p p" data-ofbid="d190487e96__20250123155220">其中 usb_ep_queue() 函数就会调用 endpoint 的操作函数集 <code class="+ topic/ph pr-d/codeph ph codeph">dwc2_hsotg_ep_ops</code> 中的
<code class="+ topic/ph pr-d/codeph ph codeph">.queue</code> 函数:</p>
<pre class="+ topic/pre pr-d/codeblock pre codeblock language-c" id="usb_gadget_device__pre_br1_zqz_21c" data-ofbid="usb_gadget_device__pre_br1_zqz_21c"><code><strong class="hl-keyword">int</strong> usb_ep_queue(<strong class="hl-keyword">struct</strong> usb_ep *ep,
<strong class="hl-keyword">struct</strong> usb_request *req, gfp_t gfp_flags)
{
<strong class="hl-keyword">int</strong> ret = <span class="hl-number">0</span>;
<strong class="hl-keyword">if</strong> (WARN_ON_ONCE(!ep-&gt;enabled &amp;&amp; ep-&gt;address)) {
ret = -ESHUTDOWN;
<strong class="hl-keyword">goto</strong> out;
}
<em class="hl-comment">/* (1) 实际调用 dwc2_hsotg_ep_queue_lock() */</em>
ret = ep-&gt;ops-&gt;queue(ep, req, gfp_flags);
out:
trace_usb_ep_queue(ep, req, ret);
<strong class="hl-keyword">return</strong> ret;
}
</code></pre>
</section>
<section class="- topic/section section" id="usb_gadget_device__section_cr1_zqz_21c" data-ofbid="usb_gadget_device__section_cr1_zqz_21c"><h2 class="- topic/title title sectiontitle">UDC Control</h2>
<p class="- topic/p p" data-ofbid="d190487e114__20250123155220">Gadget Device 还提供了 UDC 层级的一些操作函数UDC Driver 在调用 usb_add_gadget_udc() 注册 Gadget Device
之前,初始化了 Gadget 的 操作函数集:</p>
<pre class="+ topic/pre pr-d/codeblock pre codeblock language-c" id="usb_gadget_device__pre_dr1_zqz_21c" data-ofbid="usb_gadget_device__pre_dr1_zqz_21c"><code>dwc2_driver_probe() → dwc2_gadget_init():
<strong class="hl-keyword">int</strong> dwc2_gadget_init(<strong class="hl-keyword">struct</strong> dwc2_hsotg *hsotg)
{
hsotg-&gt;gadget.max_speed = USB_SPEED_HIGH;
<em class="hl-comment">/* (1) 初始化 Gadget Device 的操作函数集 */</em>
hsotg-&gt;gadget.ops = &amp;dwc2_hsotg_gadget_ops;
hsotg-&gt;gadget.name = dev_name(dev);
hsotg-&gt;remote_wakeup_allowed = <span class="hl-number">0</span>;
}
<strong class="hl-keyword">static</strong> <strong class="hl-keyword">const</strong> <strong class="hl-keyword">struct</strong> usb_gadget_ops dwc2_hsotg_gadget_ops = {
.get_frame = dwc2_hsotg_gadget_getframe,
.set_selfpowered = dwc2_hsotg_set_selfpowered,
.udc_start = dwc2_hsotg_udc_start,
.udc_stop = dwc2_hsotg_udc_stop,
.pullup = dwc2_hsotg_pullup,
.vbus_session = dwc2_hsotg_vbus_session,
.vbus_draw = dwc2_hsotg_vbus_draw,
};
</code></pre>
<p class="- topic/p p" data-ofbid="d190487e120__20250123155220">Gadget Api 提供了一些内部函数来调用:</p>
<pre class="+ topic/pre pr-d/codeblock pre codeblock language-c" id="usb_gadget_device__pre_er1_zqz_21c" data-ofbid="usb_gadget_device__pre_er1_zqz_21c"><code><strong class="hl-keyword">static</strong> <strong class="hl-keyword">inline</strong> <strong class="hl-keyword">int</strong> usb_gadget_udc_start(<strong class="hl-keyword">struct</strong> usb_udc *udc)
{
<strong class="hl-keyword">return</strong> udc-&gt;gadget-&gt;ops-&gt;udc_start(udc-&gt;gadget, udc-&gt;driver);
}
<strong class="hl-keyword">static</strong> <strong class="hl-keyword">inline</strong> <strong class="hl-keyword">void</strong> usb_gadget_udc_stop(<strong class="hl-keyword">struct</strong> usb_udc *udc)
{
udc-&gt;gadget-&gt;ops-&gt;udc_stop(udc-&gt;gadget);
}
<strong class="hl-keyword">static</strong> <strong class="hl-keyword">inline</strong> <strong class="hl-keyword">void</strong> usb_gadget_udc_set_speed(<strong class="hl-keyword">struct</strong> usb_udc *udc,
<strong class="hl-keyword">enum</strong> usb_device_speed speed)
{
<strong class="hl-keyword">if</strong> (udc-&gt;gadget-&gt;ops-&gt;udc_set_speed) {
<strong class="hl-keyword">enum</strong> usb_device_speed s;
s = min(speed, udc-&gt;gadget-&gt;max_speed);
udc-&gt;gadget-&gt;ops-&gt;udc_set_speed(udc-&gt;gadget, s);
}
}
<strong class="hl-keyword">int</strong> usb_gadget_connect(<strong class="hl-keyword">struct</strong> usb_gadget *gadget)
{
<strong class="hl-keyword">int</strong> ret = <span class="hl-number">0</span>;
<strong class="hl-keyword">if</strong> (!gadget-&gt;ops-&gt;pullup) {
ret = -EOPNOTSUPP;
<strong class="hl-keyword">goto</strong> out;
}
<strong class="hl-keyword">if</strong> (gadget-&gt;deactivated) {
<em class="hl-comment">/*
* If gadget is deactivated we only save new state.
* Gadget will be connected automatically after activation.
*/</em>
gadget-&gt;connected = true;
<strong class="hl-keyword">goto</strong> out;
}
ret = gadget-&gt;ops-&gt;pullup(gadget, <span class="hl-number">1</span>);
<strong class="hl-keyword">if</strong> (!ret)
gadget-&gt;connected = <span class="hl-number">1</span>;
out:
trace_usb_gadget_connect(gadget, ret);
<strong class="hl-keyword">return</strong> ret;
}
<strong class="hl-keyword">int</strong> usb_gadget_disconnect(<strong class="hl-keyword">struct</strong> usb_gadget *gadget)
{
<strong class="hl-keyword">int</strong> ret = <span class="hl-number">0</span>;
<strong class="hl-keyword">if</strong> (!gadget-&gt;ops-&gt;pullup) {
ret = -EOPNOTSUPP;
<strong class="hl-keyword">goto</strong> out;
}
<strong class="hl-keyword">if</strong> (!gadget-&gt;connected)
<strong class="hl-keyword">goto</strong> out;
<strong class="hl-keyword">if</strong> (gadget-&gt;deactivated) {
<em class="hl-comment">/*
* If gadget is deactivated we only save new state.
* Gadget will stay disconnected after activation.
*/</em>
gadget-&gt;connected = false;
<strong class="hl-keyword">goto</strong> out;
}
ret = gadget-&gt;ops-&gt;pullup(gadget, <span class="hl-number">0</span>);
<strong class="hl-keyword">if</strong> (!ret) {
gadget-&gt;connected = <span class="hl-number">0</span>;
gadget-&gt;udc-&gt;driver-&gt;disconnect(gadget);
}
out:
trace_usb_gadget_disconnect(gadget, ret);
<strong class="hl-keyword">return</strong> ret;
}</code></pre>
</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_gadget_device__section_uq1_zqz_21c" data-tocid="usb_gadget_device__section_uq1_zqz_21c">Endpoint Alloc</a></div></li><li class="section-item"><div class="section-title"><a href="#usb_gadget_device__section_yq1_zqz_21c" data-tocid="usb_gadget_device__section_yq1_zqz_21c">EndPoint Access</a></div></li><li class="section-item"><div class="section-title"><a href="#usb_gadget_device__section_cr1_zqz_21c" data-tocid="usb_gadget_device__section_cr1_zqz_21c">UDC Control</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-23
</div>
</div>
</div>
</footer>
<button id="go2top" class="d-print-none" title="返回顶部">
<span class="oxy-icon oxy-icon-up"></span>
</button>
<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>