Skip to content

Commit 5cdcd6a

Browse files
committed
add ADB to FunKey-OS
1 parent 7a15dcc commit 5cdcd6a

File tree

4 files changed

+204
-1
lines changed

4 files changed

+204
-1
lines changed

FunKey/board/funkey/linux.config

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ CONFIG_USB_ZERO=m
120120
CONFIG_USB_ETH=m
121121
CONFIG_USB_ETH_EEM=y
122122
CONFIG_USB_G_NCM=m
123+
CONFIG_USB_FUNCTIONFS=m
124+
CONFIG_USB_FUNCTIONFS_ETH=y
125+
CONFIG_USB_FUNCTIONFS_RNDIS=y
123126
CONFIG_USB_MASS_STORAGE=m
124127
CONFIG_USB_G_SERIAL=m
125128
CONFIG_USB_G_ACM_MS=m
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
#!/bin/sh
2+
3+
#set -xv
4+
5+
SELF=${SELF:-$(basename $0)}
6+
7+
source /usr/local/lib/utils
8+
source usb_gadget
9+
10+
# The composite gadget directory
11+
GADGET=/sys/kernel/config/usb_gadget/FunKey
12+
13+
# USB VID for Intel
14+
ID_VENDOR="0x8087"
15+
16+
# USB PID for Multifunction Composite Gadget
17+
ID_PRODUCT="0x011e"
18+
19+
# Get the CPU serial number
20+
SERIAL="$(grep Serial /proc/cpuinfo | sed 's/Serial\s*: \(\w*\)/\1/')"
21+
22+
# Initialize the ADB
23+
init_adb() {
24+
25+
# Don't proceed if existing gadget is present
26+
if [ -e ${GADGET} ]; then
27+
return 0
28+
fi
29+
30+
# Get the legacy drivers out of the way
31+
modprobe -r g_ether
32+
modprobe -r g_mass_storage
33+
34+
# Load the libcomposite USB driver, configfs and various other drivers
35+
modprobe libcomposite
36+
modprobe usb_f_serial
37+
modprobe usb_f_fs
38+
modprobe usb_f_acm
39+
40+
# USB Device Controller Driver
41+
local udc_driver=$(ls /sys/class/udc | cut -f1 | head -n 1)
42+
43+
# Create our gadget directory
44+
mkdir ${GADGET}
45+
mkdir ${GADGET}/strings/0x409
46+
mkdir ${GADGET}/configs/FunKey.1
47+
mkdir ${GADGET}/configs/FunKey.1/strings/0x409
48+
mkdir ${GADGET}/functions/acm.GS0
49+
mkdir ${GADGET}/functions/ffs.adb
50+
51+
# USB VID and PID
52+
echo ${ID_VENDOR} > ${GADGET}/idVendor
53+
echo ${ID_PRODUCT} > ${GADGET}/idProduct
54+
55+
# Device String Descriptiors
56+
echo "Intel" > ${GADGET}/strings/0x409/manufacturer
57+
echo "FunKey S" > ${GADGET}/strings/0x409/product
58+
echo ${SERIAL} > ${GADGET}/strings/0x409/serialnumber
59+
60+
# Configuration
61+
62+
# Maximum power is 120 mA
63+
echo 120 > ${GADGET}/configs/FunKey.1/MaxPower
64+
65+
# Configuration String Descriptors
66+
echo "ADB+CDC" > ${GADGET}/configs/FunKey.1/strings/0x409/configuration
67+
68+
# Add the ACM function to the FunKey.1 configuration
69+
ln -s ${GADGET}/functions/acm.GS0 ${GADGET}/configs/FunKey.1
70+
71+
# Add the FunctionFS function to the FunKey.1 configuration
72+
ln -s ${GADGET}/functions/ffs.adb ${GADGET}/configs/FunKey.1
73+
74+
# Create the function filesystem
75+
mkdir /dev/usb-ffs
76+
mkdir /dev/usb-ffs/adb
77+
78+
# Mount the ADB function filesystem
79+
mount -t functionfs adb /dev/usb-ffs/adb
80+
81+
# Bring up the loopback network
82+
ifup lo
83+
84+
# Launch the ADB daemon
85+
adbd >/dev/null &
86+
87+
# Sleeping is required to wait for the UDC to come up
88+
sleep 5
89+
90+
# Bind the USB Gadget
91+
echo ${udc_driver} > ${GADGET}/UDC
92+
return 0
93+
}
94+
95+
# Deinitialize the ADB
96+
deinit_adb() {
97+
98+
# Unbind the device
99+
echo > ${GADGET}/UDC
100+
101+
# Kill the ADB daemon
102+
killall adbd
103+
104+
# Bring down the local network
105+
ifdown lo
106+
107+
# Unmount the ADB function filesystem
108+
umount /dev/usb-ffs/adb
109+
110+
# Delete the function filesystem
111+
rmdir /dev/usb-ffs/adb
112+
rmdir /dev/usb-ffs
113+
114+
# Remove functions from configurations
115+
rm ${GADGET}/configs/FunKey.1/acm.GS0
116+
rm ${GADGET}/configs/FunKey.1/ffs.adb
117+
118+
# Remove string directories in configurations
119+
rmdir ${GADGET}/configs/FunKey.1/strings/0x409
120+
121+
# Remove configurations
122+
rmdir ${GADGET}/configs/FunKey.1
123+
124+
# Remove functions
125+
rmdir ${GADGET}/functions/acm.GS0
126+
rmdir ${GADGET}/functions/ffs.adb
127+
128+
# Remove strings
129+
rmdir ${GADGET}/strings/0x409
130+
131+
# Finallyy remove the gadget
132+
rmdir ${GADGET}
133+
134+
# Unload the kernel modules
135+
modprobe -r usb_f_serial usb_f_fs usb_f_acm
136+
}
137+
138+
case "$1" in
139+
140+
start)
141+
deinit_usb_gadget
142+
init_adb
143+
;;
144+
145+
stop)
146+
deinit_adb
147+
init_usb_gadget
148+
;;
149+
150+
*)
151+
die 15 "Usage $0 {start|stop}"
152+
;;
153+
esac
154+
exit $?

FunKey/board/funkey/rootfs-overlay/usr/local/sbin/usb_gadget

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ SELF=${SELF:-$(basename $0)}
66

77
source /usr/local/lib/utils
88

9-
# The composite gadget directory
9+
# The composite gadget directory
1010
GADGET=/sys/kernel/config/usb_gadget/FunKey
1111

1212
# Check if Ethernet over USB network is requested
@@ -167,3 +167,48 @@ init_usb_gadget() {
167167
echo ${udc_driver} > ${GADGET}/UDC
168168
return 0
169169
}
170+
171+
# Deinitialize the USB gadget
172+
deinit_usb_gadget() {
173+
174+
# Unbind the device
175+
echo > ${GADGET}/UDC
176+
177+
# Remove functions from configurations
178+
rm ${GADGET}/configs/FunKey.1/mass_storage.mmcblk0p4
179+
if [ ${USBNET} -eq 1 ]; then
180+
rm ${GADGET}/configs/FunKey.1/rndis.usb0
181+
fi
182+
183+
# Remove string directories in configurations
184+
rmdir ${GADGET}/configs/FunKey.1/strings/0x409
185+
186+
# Remove configurations from OS descriptors
187+
if [ ${USBNET} -eq 1 ]; then
188+
rm ${GADGET}/os_desc/FunKey.1
189+
fi
190+
191+
# Remove configurations
192+
rmdir ${GADGET}/configs/FunKey.1
193+
194+
# Remove extended properties from OS descriptors
195+
if [ ${USBNET} -eq 1 ]; then
196+
rmdir ${GADGET}/functions/rndis.usb0/os_desc/interface.rndis/Icons
197+
rmdir ${GADGET}/functions/rndis.usb0/os_desc/interface.rndis/Label
198+
fi
199+
200+
# Remove functions
201+
rmdir ${GADGET}/functions/mass_storage.mmcblk0p4
202+
if [ ${USBNET} -eq 1 ]; then
203+
rmdir ${GADGET}/functions/rndis.usb0
204+
fi
205+
206+
# Remove strings
207+
rmdir ${GADGET}/strings/0x409
208+
209+
# Finallyy remove the gadget
210+
rmdir ${GADGET}
211+
212+
# Unload the kernel modules
213+
modprobe -r usb_f_mass_storage usb_f_rndis
214+
}

FunKey/configs/funkey_defconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ BR2_PACKAGE_FMT=y
9797
BR2_PACKAGE_ICU=y
9898
BR2_PACKAGE_DHCPCD=y
9999
BR2_PACKAGE_DROPBEAR=y
100+
BR2_PACKAGE_ANDROID_TOOLS=y
100101
BR2_PACKAGE_PROCPS_NG=y
101102
BR2_PACKAGE_SWUPDATE=y
102103
BR2_PACKAGE_SWUPDATE_CONFIG="$(BR2_EXTERNAL_FUNKEY_PATH)/board/funkey/swupdate.config"

0 commit comments

Comments
 (0)