Skip to content

Commit eac7481

Browse files
Add multi-UDC support (#7)
* Add multi-UDC support * Modify src/radxa-otgutils usage * 修改服务Description * 修改选项顺序,并处理非预期参数 * 去除多余dh_installsystemd参数 * 去除错误忽略 * 使用bash语法进行判断 * 添加flock * 修复/var/run/adbd.pid为空的问题 * 修改flock粒度 * 修改configfs创建删除逻辑 * 修改configfs删除逻辑 * 修改 deinit configfs 逻辑 * 修改flock miaosh * 替换 start-stop-daemon 选项 -m 为 --make-pidfile * Apply suggestions from code review Co-authored-by: ZHANG Yuntian <[email protected]> * 修改configfs_make调用位置 * 修改 configuration 字段 * 修改 function_stop,取消删除$UDC文件夹 * 注销 configfs 判断文件夹是否存在 * 格式化语法 * 复用 UDC 设置 * 缩短 sleep 时间 * 修正日志 * 更正判断文件夹路径 * 删除多余调试日志 * add_udc 改名为 enable_udc --------- Co-authored-by: ZHANG Yuntian <[email protected]>
1 parent a0e47a7 commit eac7481

File tree

6 files changed

+125
-73
lines changed

6 files changed

+125
-73
lines changed

debian/radxa-otgutils.radxa-adbd.service

Lines changed: 0 additions & 13 deletions
This file was deleted.

debian/[email protected]

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[Unit]
2+
Description=Enable adbd on %i
3+
Documentation=https://github.com/radxa-pkg/radxa-otgutils/
4+
After=network.target
5+
6+
[Service]
7+
RemainAfterExit=yes
8+
ExecStart=/usr/sbin/radxa-otgutils adbd %i start
9+
ExecStop=/usr/sbin/radxa-otgutils adbd %i stop
10+
ExecReload=/usr/sbin/radxa-otgutils adbd %i reload
11+
12+
[Install]
13+
WantedBy=multi-user.target

debian/radxa-otgutils.radxa-usbnet.service

Lines changed: 0 additions & 13 deletions
This file was deleted.

debian/[email protected]

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[Unit]
2+
Description=Enable USB Ethernet on %i
3+
Documentation=https://github.com/radxa-pkg/radxa-otgutils/
4+
After=network.target
5+
6+
[Service]
7+
RemainAfterExit=yes
8+
ExecStart=/usr/sbin/radxa-otgutils usbnet %i start
9+
ExecStop=/usr/sbin/radxa-otgutils usbnet %i stop
10+
ExecReload=/usr/sbin/radxa-otgutils usbnet %i reload
11+
12+
[Install]
13+
WantedBy=multi-user.target

debian/rules

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ override_dh_builddeb:
1010
dh_builddeb -- -Zxz
1111

1212
override_dh_installsystemd:
13-
dh_installsystemd --no-enable --no-start --name=radxa-adbd
14-
dh_installsystemd --no-enable --no-start --name=radxa-usbnet
13+
dh_installsystemd --name=radxa-adbd@
14+
dh_installsystemd --name=radxa-usbnet@

src/radxa-otgutils

Lines changed: 97 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,38 @@
11
#!/bin/bash -e
22

3-
configfs_init()
3+
lock_udc()
44
{
5-
modprobe libcomposite
5+
configfs_make
6+
exec 100<"/sys/kernel/config/usb_gadget/$UDC"
7+
flock 100
8+
}
69

7-
mkdir -p "/sys/kernel/config/usb_gadget/radxa-otgutils"
8-
pushd "/sys/kernel/config/usb_gadget/radxa-otgutils"
10+
unlock_udc()
11+
{
12+
exec 100<&-
13+
}
14+
15+
enable_udc()
16+
{
17+
if [[ -n $(cat "/sys/kernel/config/usb_gadget/$UDC/UDC") ]];
18+
then
19+
echo "" > "/sys/kernel/config/usb_gadget/$UDC/UDC"
20+
fi
21+
echo "$UDC" > "/sys/kernel/config/usb_gadget/$UDC/UDC"
22+
}
23+
24+
configfs_make()
25+
{
26+
if [ ! -e "/sys/kernel/config/usb_gadget/$UDC" ];
27+
then
28+
modprobe libcomposite
29+
mkdir -p "/sys/kernel/config/usb_gadget/$UDC"
30+
fi
31+
}
32+
33+
configfs_init()
34+
{
35+
pushd "/sys/kernel/config/usb_gadget/$UDC"
936

1037
echo "0x1d6b" > "idVendor" # Linux Foundation
1138
echo "0x0104" > "idProduct" # Multifunction Composite Gadget
@@ -22,7 +49,7 @@ configfs_init()
2249

2350
mkdir -p "configs/r.1/strings/0x409"
2451
echo "500" > "configs/r.1/MaxPower"
25-
echo "adb" > "configs/r.1/strings/0x409/configuration"
52+
echo "USB Composite Device" > "configs/r.1/strings/0x409/configuration"
2653

2754
popd
2855
}
@@ -38,14 +65,14 @@ function_init()
3865
;;
3966
esac
4067

41-
if [ ! -e "/sys/kernel/config/usb_gadget/radxa-otgutils/functions/$GADGET" ];
68+
if [ ! -e "/sys/kernel/config/usb_gadget/$UDC/functions/$GADGET" ];
4269
then
43-
mkdir -p "/sys/kernel/config/usb_gadget/radxa-otgutils/functions/$GADGET"
70+
mkdir -p "/sys/kernel/config/usb_gadget/$UDC/functions/$GADGET"
4471
fi
4572

46-
if [ ! -e "/sys/kernel/config/usb_gadget/radxa-otgutils/configs/r.1/$GADGET" ];
73+
if [ ! -e "/sys/kernel/config/usb_gadget/$UDC/configs/r.1/$GADGET" ];
4774
then
48-
ln -s "/sys/kernel/config/usb_gadget/radxa-otgutils/functions/$GADGET" "/sys/kernel/config/usb_gadget/radxa-otgutils/configs/r.1/$GADGET"
75+
ln -s "/sys/kernel/config/usb_gadget/$UDC/functions/$GADGET" "/sys/kernel/config/usb_gadget/$UDC/configs/r.1/$GADGET"
4976
fi
5077
}
5178

@@ -60,19 +87,39 @@ function_stop()
6087
;;
6188
esac
6289

63-
if [ -e "/sys/kernel/config/usb_gadget/radxa-otgutils/configs/r.1/$GADGET" ];
90+
if [ -e "/sys/kernel/config/usb_gadget/$UDC/configs/r.1/$GADGET" ];
6491
then
65-
rm "/sys/kernel/config/usb_gadget/radxa-otgutils/configs/r.1/$GADGET"
92+
rm "/sys/kernel/config/usb_gadget/$UDC/configs/r.1/$GADGET"
6693
fi
6794

68-
if [ -e "/sys/kernel/config/usb_gadget/radxa-otgutils/functions/$GADGET" ];
95+
if [ -e "/sys/kernel/config/usb_gadget/$UDC/functions/$GADGET" ];
96+
then
97+
rmdir "/sys/kernel/config/usb_gadget/$UDC/functions/$GADGET"
98+
fi
99+
if [ -n "$(find "/sys/kernel/config/usb_gadget/$UDC/functions" -maxdepth 0 -type d -empty)" ];
69100
then
70-
rmdir "/sys/kernel/config/usb_gadget/radxa-otgutils/functions/$GADGET"
101+
if [ -e "/sys/kernel/config/usb_gadget/$UDC/configs/r.1/strings/0x409" ];
102+
then
103+
rmdir "/sys/kernel/config/usb_gadget/$UDC/configs/r.1/strings/0x409"
104+
fi
105+
106+
if [ -e "/sys/kernel/config/usb_gadget/$UDC/configs/r.1" ];
107+
then
108+
rmdir "/sys/kernel/config/usb_gadget/$UDC/configs/r.1"
109+
fi
110+
111+
if [ -e "/sys/kernel/config/usb_gadget/$UDC/strings/0x409" ];
112+
then
113+
rmdir "/sys/kernel/config/usb_gadget/$UDC/strings/0x409"
114+
fi
115+
else
116+
enable_udc
71117
fi
72118
}
73119

74120
start()
75121
{
122+
lock_udc
76123
configfs_init
77124
function_init
78125

@@ -84,59 +131,64 @@ start()
84131
mount -o uid=2000,gid=2000 -t functionfs adb /dev/usb-ffs/adb
85132
fi
86133
export service_adb_tcp_port=5555
87-
start-stop-daemon --start --oknodo --pidfile /var/run/adbd.pid --startas /usr/bin/adbd --background
88-
sleep 1
134+
start-stop-daemon --start --oknodo --make-pidfile --pidfile /var/run/adbd.pid --startas /usr/bin/adbd --background
135+
sleep 0
89136
;;
90137
esac
91138

92-
local UDC=
93-
UDC="$(basename "$(find /sys/class/udc -maxdepth 1 -mindepth 1 | sort | head -1)")"
94-
if [ -z "$UDC" ];
95-
then
96-
echo "No USB Device Controller was detected. Did you set your OTG port to host mode?"
97-
exit 1
98-
fi
99-
echo "$UDC" > "/sys/kernel/config/usb_gadget/radxa-otgutils/UDC" || true
139+
enable_udc
140+
unlock_udc
100141
}
101142

102143
stop()
103144
{
145+
lock_udc
104146
function_stop
105-
106147
case "$MODE" in
107148
adbd)
108-
start-stop-daemon --stop --oknodo --pidfile /var/run/adbd.pid --retry 5
149+
start-stop-daemon --stop --oknodo --pidfile /var/run/adbd.pid --remove-pidfile --retry 5
109150
umount /dev/usb-ffs/adb
110151
rmdir /dev/usb-ffs/adb
111152
rmdir /dev/usb-ffs
112153
;;
113154
esac
155+
unlock_udc
114156
}
115157

116-
MODE=
158+
MODE=""
117159
case "$1" in
118-
adbd|usbnet)
119-
MODE="$1"
120-
;;
121-
*)
122-
echo "Unknown mode. Valid values are adbd or usbnet."
123-
exit 1
124-
;;
160+
adbd|usbnet)
161+
MODE="$1"
162+
;;
163+
*)
164+
echo "Unknown mode. Valid values are adbd or usbnet."
165+
echo "Usage: $0 <adbd|usbnet> <UDC> <start|stop|restart>"
166+
exit 1
125167
esac
126168

127-
case "$2" in
128-
start)
129-
start
130-
;;
131-
stop)
132-
stop
133-
;;
134-
restart|reload)
135-
stop
136-
start
137-
;;
169+
UDC=""
170+
if [ -e "/sys/class/udc/$2" ];
171+
then
172+
UDC="$2"
173+
else
174+
echo "This UDC does not exist, you can turn on the OTG port Peripheral mode device tree overlay at rsetup and look in the /sys/class/udc directory for."
175+
echo "Usage: $0 <adbd|usbnet> <UDC> <start|stop|restart>"
176+
exit 1
177+
fi
178+
179+
case "$3" in
180+
start)
181+
start
182+
;;
183+
stop)
184+
stop
185+
;;
186+
restart|reload)
187+
stop
188+
start
189+
;;
138190
*)
139-
echo "Usage: $0 <adbd|usbnet> <start|stop|restart>"
191+
echo "Usage: $0 <adbd|usbnet> <UDC> <start|stop|restart>"
140192
exit 1
141193
esac
142194

0 commit comments

Comments
 (0)