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指令生成所需要的更新檔(下面的指令并不是普遍适用的,隻是一個參考,需要根據具體情況靈活變通)
得到如下更新檔
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

針對Android的更改
從核心版本3.8開始,adb功能的實作發生了變化,把它的實作從核心代碼中移除,在使用者空間借助usb的functionfs功能實作adb。
核心 commit history
commit de8cff675b3de92ad4bb18a69cfe19091e810f49
Author:Benoit Goby <[email protected]>
Date:Mon Nov 5 18:47:08 2012 -0800
usb: gadget: Fix android gadget driver build
Removed obsolete f_adb function
Change-Id:Idfb4110429bc0ea63f493c68ad667f49ca471987
Signed-off-by: Benoit Goby<[email protected]>
android commit history
system/core
commitfd96db17b7f07eb6615af01fd1908b74383bf04b
Author:Andrzej Pietrasiewicz <[email protected]>
Date:Fri Jan 13 15:13:46 2012 +0100
FunctionFS: initial implementation
This is the second version of a patch whichdemonstrates
the possibility
of using adbd (Android Debug Bridge daemon)with
a generic FunctionFS gadget
instead of a custom adb usb gadget in theLinux
kernel. It contains changes
introduced after Benoit‘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=<vendor ID>iSerialNumber=<some
string>
$ mount -t functionfs adb /dev/usb-ffs/adb-o
uid=2000,gid=2000
$ ./adbd
This patch requires a patch to bionic whichadds
<linux/usb_functionfs.h>
which is an exact copy of the relevant filein
the linux kernel.
Change-Id:I4b42eb267ffa50fca7a5fba46f388a2f083e8b2d
Signed-off-by: Andrzej Pietrasiewicz<[email protected]>
Signed-off-by: Kyungmin Park<[email protected]>
[[email protected]: detect at runtime iffunctionfs
is mounted
or fallback using f_adb]
Signed-off-by: Benoit Goby<[email protected]>
為了使能adb功能,還需要在usb啟動配置腳本中挂載usb
functionfs檔案系統
目前目錄:<Android源代碼根目錄>/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