自 2025 年 3 月 27 日起,我们建议您使用 android-latest-release
而非 aosp-main
构建 AOSP 并为其做出贡献。如需了解详情,请参阅 AOSP 的变更。
实现 USB HAL
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Android 8.0 版本将 USB 命令的处理从 init
脚本移到了原生 USB 守护程序,以改进配置并提高代码可靠性。对于 Gadget 函数配置,系统会使用 init
脚本(属性触发器)执行设备专属的小工具操作。
在以前的版本中,这些设备专属的配置通过设备专属的 init
脚本(使用属性触发器)来实现。移至硬件抽象层 (HAL) 的设计可以带来一种更简洁的实现,这种实现能够解决以下问题:
- 操作(例如写入到内核 sysfs 节点)可能会失败,但系统不会将操作结果传回设置属性触发器的框架代码。因此,框架会误认为这些操作已成功,即使它们已静默失败也是如此。
init
脚本包含的可执行的操作数量有限。
Android 12 版本添加了对网络控制模型 (NCM) 和 API 调用(返回 HAL 版本号和 USB 速度)的 USB Gadget HAL 支持。如需详细了解可通过 USB HAL 执行的 API 调用,请参阅 android.hardware.usb
软件包摘要。
HAL 和 Treble
过去,系统使用设备专属的 init
脚本替代 HAL 层来执行设备专属的 USB 操作。USB(通过 ADB)是用于调试系统问题的主要接口。让原生守护程序执行 USB 配置可以消除对框架代码的依赖,这样即使框架崩溃,USB 也应该能够正常运行。
Android 8.0 中还引入了 Treble 模型,在该模型下,所有 HAL 都与系统服务隔离开来,并且都需要在各自的原生守护程序中运行。这样就不需要使用专有的 USB 守护程序,因为 HAL 层可兼做 USB 守护程序。
默认 HAL 实现支持 Android 版本低于 8.0 的所有设备。因此,对于这些设备,不需要执行任何设备专属的工作。Android 8.0 使用 HAL 接口查询 USB 端口的状态,以及执行数据角色和电源角色交换。
实现
需要在搭载 Android 8.0 的每台设备上实现新的 USB HAL 接口。默认实现应支持 Android 版本低于 8.0 的设备。如果设备使用 dual_role_usb
类来报告 C 型端口的状态,默认实现便足以满足需求。可能需要在设备专属的 USB 脚本中进行细微的更改,以便将 C 型节点的所有权转移给系统。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-03-26。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-03-26。"],[],[],null,["# Implement USB HAL\n\nThe Android 8.0 release moves handling of USB commands out of `init`\nscripts and into a native USB daemon for better configuration and code\nreliability. For the Gadget function configuration, `init` scripts\n(property triggers) are used to perform device-specific gadget operations.\n\nIn previous releases, these device-specific configurations were achieved through\ndevice-specific `init` scripts (using property triggers). Moving to a\nHardware Abstraction Layer (HAL) design results in a much cleaner implementation\nthat solves these problems:\n\n1. Operations such as writes to the kernel sysfs nodes could fail but not be propagated back to the frameworks code that sets the property trigger. As a result, frameworks incorrectly assumes the operations have succeeded even though they have silently failed.\n2. `init` scripts have a limited number of operations that could be executed.\n\nThe Android 12 release adds USB Gadget HAL support for Network Control\nModels (NCM) and API calls that return both the HAL version number and USB speed. For more\ninformation on the API calls available through the USB HAL, see\n[the `android.hardware.usb` package summary](https://developer.android.com/reference/android/hardware/usb/package-summary).\n\nHAL and Treble\n--------------\n\n\nThe device-specific `init` scripts were used as a substitution for\nHAL layers to perform device-specific USB operations. USB (through ADB) is a\nprimary interface for debugging system issues. Having a native daemon to perform\nUSB configuration eliminates the dependency on the framework code so even if the\nframework crashes USB should be running.\n\n\nUnder the\n[Treble](https://android-developers.googleblog.com/2017/05/here-comes-treble-modular-base-for.html)\nmodel also introduced in Android 8.0, all of the HALs are isolated from System\nservices and are required to run in their own native daemons. This eliminates\nthe requirement to have an exclusive USB daemon as the HAL layer nicely doubles\nas a USB daemon.\n\n\nThe default HAL implementation takes care of all pre-Android 8.0 devices. Therefore, there\nwouldn't be any device-specific work for the pre-Android 8.0 devices. Android 8.0 uses the HAL\ninterface to query the status of USB ports and to perform data role and power\nrole swaps.\n\nImplementation\n--------------\n\n\nNew USB HAL interface needs to be implemented on every device launching on Android 8.0.\nThe default implementation should take care of pre-Android 8.0 devices. The default\nimplementation is sufficient if the device uses the `dual_role_usb` class to report\ntype-c port status. Trivial changes might be required in device-specific USB scripts\nto transfer ownership of the typc-c nodes to system."]]