自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
實作 USB HAL
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Android 8.0 版本將 USB 指令的處理作業從 init
指令碼移至原生 USB 守護程序,以便改善設定和程式碼的可靠性。針對小工具函式設定,init
指令碼 (屬性觸發事件) 會用於執行裝置專屬的小工具作業。
在先前的版本中,這些裝置專屬設定是透過裝置專屬的 init
指令碼 (使用屬性觸發條件) 完成。改用硬體抽象層 (HAL) 設計後,實作方式會變得更簡潔,並解決以下問題:
- 寫入核心 sysfs 節點的作業可能會失敗,但不會傳回至設定屬性觸發事件的架構程式碼。因此,即使作業已悄悄失敗,架構仍會錯誤地假設作業已成功。
init
指令碼可執行的作業數量有限。
Android 12 版本新增了 USB Gadget HAL 支援功能,可支援網路控制模型 (NCM) 和 API 呼叫,同時傳回 HAL 版本號碼和 USB 速度。如要進一步瞭解可透過 USB HAL 使用的 API 呼叫,請參閱android.hardware.usb
套件摘要。
HAL 和 Treble
裝置專屬的 init
指令碼可用來取代 HAL 層,執行裝置專屬的 USB 作業。USB (透過 ADB) 是偵錯系統問題的主要介面。使用原生 Daemon 執行 USB 設定,可消除對架構程式碼的依附性,因此即使架構發生當機,USB 仍可繼續執行。
在 Android 8.0 中也推出的 Treble 模型中,所有 HAL 都會與系統服務隔離,且必須在其自有的原生守護程式中執行。這樣一來,就不需要使用專屬的 USB 守護程序,因為 HAL 層會同時擔任 USB 守護程序。
預設的 HAL 實作項目會處理所有 Android 8.0 以下版本的裝置。因此,Android 8.0 以下版本的裝置不會有任何裝置專屬工作。Android 8.0 會使用 HAL 介面查詢 USB 埠的狀態,並執行資料角色和電源角色互換作業。
實作
在 Android 8.0 上推出的每部裝置都必須實作新的 USB HAL 介面。預設實作方式應可處理 Android 8.0 以下版本的裝置。如果裝置使用 dual_role_usb
類別回報 Type-C 通訊埠狀態,預設實作方式就足以應付。您可能需要在裝置專屬的 USB 指令碼中進行一些微小變更,才能將 typc-c 節點的擁有權轉移至系統。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[[["容易理解","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"]],["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],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."]]