Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android KitKat 4.4平台開發-添加USB ADB和MTP功能支持

Android KitKat 4.4平台開發-添加USB ADB和MTP功能支持

編輯:關於Android編程

ADB和MTP是Android基於USB實現的兩個重要功能,極大地方便了用戶在PC與Android設備之間的互操作,比如傳輸文件、安裝應用、開發調試應用。

本文講述如何在特定軟硬件平台下支持Android ADB和MTP功能。

Android版本: KitKat 4.4.2

Linux內核版本: 3.10 (Vendor Kernel)

硬件平台: Atmel SAMA5D3 SoC


針對Linux內核的更改

Merge Android Linux內核USB Gadget驅動到處理器廠商Linux內核

Vendor Linux內核和Android Linux內核都是基於Mainline Linux內核進行開發的。芯片廠商和Google公司都會修改Linux內核代碼以支持自己的硬件平台及Android系統。新添加的功能和支持在穩定後會重新合並到後續版本的Mainline Linux內核。

從內核版本3.3開始,Android Linux內核開始與Mainline Linux內核融合,但目前(本文使用的內核版本3.10)還是有一部分功能沒有並入Mainline Linux內核,比如用於支持Android ADB, Mass Storage和MTP/PTP等功能的Android USB Gadget驅動。

如果要在某個芯片廠商的硬件平台上運行Android系統,並支持這些USB相關的功能,必須將Android Linux內核中的Android USB Gadget驅動並入Vendor Linux內核。有些處理器芯片廠商會提供支持Android的Linux 內核,如果要命名的話,可以稱之為Vendor Android Linux內核,而如果我們所使用的硬件平台,廠商沒有提供完整支持Android的Linux內核,Android平台開發者可以自己合並代碼以添加支持。

可以在多個源找到Android Linux內核的源代碼,本文使用github上的源:

https://github.com/android/kernel_common branch:android-3.10


從Android Linux內核獲得Android USB Gadget驅動的補丁

受影響的文件

drivers/usb/gadget/Kconfig

drivers/usb/gadget/Makefile

drivers/usb/gadget/android.c

drivers/usb/gadget/composite.c

drivers/usb/gadget/f_accessory.c

drivers/usb/gadget/f_audio_source.c

drivers/usb/gadget/f_fs.c

drivers/usb/gadget/f_mtp.c

drivers/usb/gadget/f_rndis.c

drivers/usb/gadget/rndis.c

drivers/usb/gadget/u_serial.c

drivers/usb/gadget/udc-core.c

include/linux/usb/f_accessory.h

include/linux/usb/f_mtp.h

include/uapi/linux/usb/f_accessory.h

include/uapi/linux/usb/f_mtp.h


使用gitformat-patch命令生成所需要的補丁(下面的命令並不是普遍適用的,只是一個參考,需要根據具體情況靈活變通)

git format-patch -n29 drivers/usb/gadget/  include/linux/usb/f_mtp.h  include/linux/usb/f_accessory.h include/uapi/linux/usb/f_mtp.h  include/uapi/linux/usb/f_accessory.h


得到如下補丁

0001-usb-gadget-Add-Android-Composite-Gadget-driver.patch

0002-usb-gadget-mtp-Add-MTP-PTP-function.patch

0003-usb-gadget-adb-Add-ADB-function.patch

0004-usb-gadget-accessory-Add-Android-Accessory-function.patch

0005-usb-gadget-adb-allow-freezing-in-adb_read.patch

0006-usb-gadget-adb-do-not-set-error-flag-when-dequeuing-.patch

0007-usb-gadget-adb-Only-enable-the-gadget-when-adbd-is-r.patch

0008-usb-gadget-composite-Fix-corruption-when-changing-co.patch

0009-usb-gadget-android-Fix-product-name.patch

0010-usb-gadget-android-Add-FunctionFS.patch

0011-usb-gadget-accessory-Fix-section-mismatch.patch

0012-usb-gadget-Fix-usb-string-id-allocation.patch

0013-USB-gadget-Add-ACCESSORY_SET_AUDIO_MODE-control-requ.patch

0014-USB-gadget-f_accessory-Add-support-for-HID-input-dev.patch

0015-USB-gadget-f_audio_source-New-gadget-driver-for-audi.patch

0016-usb-gadget-f_fs-Fix-enumeration-in-fullspeed-mode.patch

0017-usb-gadget-accessory-Fix-section-mismatch-again.patch

0018-usb-gadget-android-Fixes-and-hacks-to-make-android-u.patch

0019-HACK-usb-gadget-Fix-enumeration-on-boot.patch

0020-usb-gadget-Fix-android-gadget-driver-build.patch

0021-usb-gadget-android-Fixes-and-hacks-to-make-android-u.patch

0022-usb-gadget-android-move-init-to-late_initcall-for-no.patch

0023-usb-gadget-android-3.10-fixes.patch

0024-USB-remove-duplicate-out-endpoint-creation-in-MTP-mo.patch

0025-usb-gadget-android-Remove-device-if-probe-fails.patch

0026-usb-gadget-f_mtp-move-userspace-interface-to-uapi.patch

0027-usb-gadget-f_accessory-move-userspace-interface-to-u.patch

0028-fix-false-disconnect-due-to-a-signal-sent-to-the-rea.patch

0029-drivers-usb-gadget-64-bit-related-type-fixes.patch


應用補丁

將這些patches復制到Vendor Linux內核源代碼根目錄下,使用git am命令將這些patches應用到Vendor Linux內核

git am *.patch

內核配置

為了讓內核支持USB ADB和MTP功能,在編譯前還需要在內核配置中使能相應選項。

Kernel Configuration

> Device Drivers

> USB Support

> USB Gadget Support

> Android Composite Gadget


\



<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PC9wPgo8cD48c3Ryb25nPtXrttQ8L3N0cm9uZz48c3Ryb25nPkFuZHJvaWQ8L3N0cm9uZz48c3Ryb25nPrXEuPy4xDwvc3Ryb25nPjwvcD4KPHA+tNPE2rrLsOaxvjMuOL+qyryjrGFkYrmmxNy1xMq1z9a3osn6wcux5Luvo6yw0cv8tcTKtc/WtNPE2rrLtPrC69bQ0saz/aOs1NrTw7unv9W85L3o1vp1c2K1xGZ1bmN0aW9uZnO5psTcyrXP1mFkYqGjPC9wPgo8cD7E2rrLIGNvbW1pdCBoaXN0b3J5PC9wPgo8cD5jb21taXQgZGU4Y2ZmNjc1YjNkZTkyYWQ0YmIxOGE2OWNmZTE5MDkxZTgxMGY0OTwvcD4KPHA+QXV0aG9yOkJlbm9pdCBHb2J5IDxiZW5vaXRAYW5kcm9pZC5jb20+PC9wPgo8cD5EYXRlOk1vbiBOb3YgNSAxODo0NzowOCAyMDEyIC0wODAwPC9wPgo8cD4gICB1c2I6IGdhZGdldDogRml4IGFuZHJvaWQgZ2FkZ2V0IGRyaXZlciBidWlsZDwvcD4KPHA+ICAgUmVtb3ZlZCBvYnNvbGV0ZSBmX2FkYiBmdW5jdGlvbjwvcD4KPHA+ICAgQ2hhbmdlLUlkOklkZmI0MTEwNDI5YmMwZWE2M2Y0OTNjNjhhZDY2N2Y0OWNhNDcxOTg3PC9wPgo8cD4gICBTaWduZWQtb2ZmLWJ5OiBCZW5vaXQgR29ieTxiZW5vaXRAYW5kcm9pZC5jb20+PC9wPgo8cD4gPC9wPgo8cD5hbmRyb2lkIGNvbW1pdCBoaXN0b3J5PC9wPgo8cD5zeXN0ZW0vY29yZTwvcD4KPHA+Y29tbWl0ZmQ5NmRiMTdiN2YwN2ViNjYxNWFmMDFmZDE5MDhiNzQzODNiZjA0YjwvcD4KPHA+QXV0aG9yOkFuZHJ6ZWogUGlldHJhc2lld2ljeiA8YW5kcnplai5wQHNhbXN1bmcuY29tPjwvcD4KPHA+RGF0ZTpGcmkgSmFuIDEzIDE1OjEzOjQ2IDIwMTIgJiM0MzswMTAwPC9wPgo8cD4gICBGdW5jdGlvbkZTOiBpbml0aWFsIGltcGxlbWVudGF0aW9uPC9wPgo8cD4gICBUaGlzIGlzIHRoZSBzZWNvbmQgdmVyc2lvbiBvZiBhIHBhdGNoIHdoaWNoZGVtb25zdHJhdGVzCiB0aGUgcG9zc2liaWxpdHk8L3A+CjxwPiAgIG9mIHVzaW5nIGFkYmQgKEFuZHJvaWQgRGVidWcgQnJpZGdlIGRhZW1vbil3aXRoCiBhIGdlbmVyaWMgRnVuY3Rpb25GUyBnYWRnZXQ8L3A+CjxwPiAgIGluc3RlYWQgb2YgYSBjdXN0b20gYWRiIHVzYiBnYWRnZXQgaW4gdGhlTGludXgKIGtlcm5lbC4gSXQgY29udGFpbnMgY2hhbmdlczwvcD4KPHA+ICAgaW50cm9kdWNlZCBhZnRlciBCZW5vaXQ="s review - thank youBenoit.

The patch adds a new usb access layer toadbd using FunctionFS. The former

usb access method is still available. Themethod is chosen at runtime

depending if /dev/usb-ffs/adb/ep0 or/dev/android_adb is accessible.

How to use on the target device:

$ insmod g_ffs.ko idVendor=iSerialNumber=

$ mount -t functionfs adb /dev/usb-ffs/adb-o uid=2000,gid=2000

$ ./adbd

This patch requires a patch to bionic whichadds

which is an exact copy of the relevant filein the linux kernel.

Change-Id:I4b42eb267ffa50fca7a5fba46f388a2f083e8b2d

Signed-off-by: Andrzej Pietrasiewicz

Signed-off-by: Kyungmin Park

[[email protected]: detect at runtime iffunctionfs is mounted

or fallback using f_adb]

Signed-off-by: Benoit Goby


為了使能adb功能,還需要在usb啟動配置腳本中掛載usb functionfs文件系統

當前目錄:/device/atmel

補丁如下:

diff--git a/sama5d3/init.sama5-ek.usb.rc b/sama5d3/init.sama5-ek.usb.rc

index80a2efa..be101e8 100644

---a/sama5d3/init.sama5-ek.usb.rc

+++b/sama5d3/init.sama5-ek.usb.rc

@@-1,3 +1,9 @@

+onfs

+mkdir /dev/usb-ffs 0770 shell shell

+mkdir /dev/usb-ffs/adb 0770 shell shell

+mount functionfs adb /dev/usb-ffs/adb uid=2000,gid=2000

+write /sys/class/android_usb/android0/f_ffs/aliases adb

+

on property:sys.usb.config=mtp,adb

write /sys/class/android_usb/android0/enable0

write/sys/class/android_usb/android0/idVendor 03EB





  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved