From 4e97605c0e5adb36a2eae083b930b9d607273b4c Mon Sep 17 00:00:00 2001 From: Mauricio Alvarez Leon <65101411+BBBmau@users.noreply.github.com> Date: Thu, 23 Jan 2025 16:31:22 -0800 Subject: [PATCH] Add ADB over USB support (#417) * figuring out adb device agent * start adbd service in comma.sh * implement thundercomm adb implementation * UDC check * setup_gadget_mode * usb gadget created * adb device shown * android.rules * check functionfs mount + add serial number * 250 maxPower * lil more * fix * doesn't seem to be needed * cleanup --------- Co-authored-by: Adeeb Shihadeh --- userspace/base_setup.sh | 1 + userspace/files/adb-param-watcher.path | 5 ++ userspace/files/adb-param-watcher.service | 10 ++++ userspace/services.sh | 2 + userspace/usr/comma/comma.sh | 2 + userspace/usr/comma/set_adb.sh | 66 +++++++++++++++++++++++ 6 files changed, 86 insertions(+) create mode 100644 userspace/files/adb-param-watcher.path create mode 100644 userspace/files/adb-param-watcher.service create mode 100644 userspace/usr/comma/set_adb.sh diff --git a/userspace/base_setup.sh b/userspace/base_setup.sh index 9d79e99..b583e09 100755 --- a/userspace/base_setup.sh +++ b/userspace/base_setup.sh @@ -113,6 +113,7 @@ rm -rf /var/lib/apt/lists/* # Create dirs mkdir /data && chown $USERNAME:$USERNAME /data mkdir /persist && chown $USERNAME:$USERNAME /persist +mkdir /config && chown root:root /config # Disable pstore service that moves files out of /sys/fs/pstore systemctl disable systemd-pstore.service diff --git a/userspace/files/adb-param-watcher.path b/userspace/files/adb-param-watcher.path new file mode 100644 index 0000000..ca7327f --- /dev/null +++ b/userspace/files/adb-param-watcher.path @@ -0,0 +1,5 @@ +[Path] +PathModified=/data/params/d/AdbEnabled + +[Install] +WantedBy=multi-user.target diff --git a/userspace/files/adb-param-watcher.service b/userspace/files/adb-param-watcher.service new file mode 100644 index 0000000..e71306b --- /dev/null +++ b/userspace/files/adb-param-watcher.service @@ -0,0 +1,10 @@ +[Unit] +Description=ADB param watcher +After=network.target + +[Service] +Type=oneshot +ExecStart=/usr/comma/set_adb.sh + +[Install] +WantedBy=multi-user.target diff --git a/userspace/services.sh b/userspace/services.sh index cc58148..bdf2904 100755 --- a/userspace/services.sh +++ b/userspace/services.sh @@ -21,6 +21,8 @@ systemctl enable power_drop_monitor.service systemctl enable brightnessd.service systemctl enable ssh-param-watcher.path systemctl enable ssh-param-watcher.service +systemctl enable adb-param-watcher.path +systemctl enable adb-param-watcher.service systemctl enable logrotate-hourly.timer # Disable some of our services diff --git a/userspace/usr/comma/comma.sh b/userspace/usr/comma/comma.sh index 4dcb641..7694bd5 100755 --- a/userspace/usr/comma/comma.sh +++ b/userspace/usr/comma/comma.sh @@ -33,10 +33,12 @@ handle_setup_keys () { ln -s /data/params/d_tmp /data/params/d fi + echo -n 1 > /data/params/d/AdbEnabled echo -n 1 > /data/params/d/SshEnabled cp /usr/comma/setup_keys /data/params/d/GithubSshKeys elif [[ -e /data/params/d/GithubSshKeys && -e /data/continue.sh ]]; then if cmp -s /data/params/d/GithubSshKeys /usr/comma/setup_keys; then + rm /data/params/d/AdbEnabled rm /data/params/d/SshEnabled rm /data/params/d/GithubSshKeys fi diff --git a/userspace/usr/comma/set_adb.sh b/userspace/usr/comma/set_adb.sh new file mode 100644 index 0000000..5860d03 --- /dev/null +++ b/userspace/usr/comma/set_adb.sh @@ -0,0 +1,66 @@ +#!/bin/bash + +setup_gadget() { + # Check if /config is already mounted + if ! mountpoint -q /config; then + sudo mount -t configfs none /config + else + echo "/config is already mounted." + fi + + # Create USB gadget directory structure + sudo mkdir -p /config/usb_gadget/g1 + cd /config/usb_gadget/g1 + sudo mkdir -p strings/0x409 + sudo mkdir -p configs/c.1/strings/0x409 + sudo mkdir -p functions/ncm.0 + + # Set Vendor and Product ID + echo 0x04D8 | sudo tee idVendor + echo 0x1234 | sudo tee idProduct + + # Set strings + echo "$(cat /proc/cmdline | sed -e 's/^.*androidboot.serialno=//' -e 's/ .*$//')" | sudo tee strings/0x409/serialnumber + echo "comma.ai" | sudo tee strings/0x409/manufacturer + echo "Linux USB Gadget" | sudo tee strings/0x409/product + echo 250 | sudo tee configs/c.1/MaxPower + + # Create ADB function + sudo mkdir -p functions/ffs.adb + sudo mkdir -p /dev/usb-ffs/adb + if ! mountpoint -q /dev/usb-ffs/adb; then + sudo mount -t functionfs adb /dev/usb-ffs/adb + else + echo "/dev/usb-ffs/adb is already mounted" + fi + # Link both functions to configuration + echo "NCM+ADB" | sudo tee configs/c.1/strings/0x409/configuration + sudo rm -f configs/c.1/ncm.0 + sudo rm -f configs/c.1/ffs.adb + sudo ln -s functions/ncm.0 configs/c.1/ + sudo ln -s functions/ffs.adb configs/c.1/ +} + +start_gadget() { + cd /config/usb_gadget/g1 + echo "a600000.dwc3" | sudo tee UDC +} + +stop_gadget() { + cd /config/usb_gadget/g1 + echo "" | sudo tee UDC +} + +ADB_PARAM="/data/params/d/AdbEnabled" +if [ -f "$ADB_PARAM" ] && [ "$(< $ADB_PARAM)" == "1" ]; then + echo "Enabling ADB" + + setup_gadget + systemctl start adbd + sleep 1 + start_gadget +else + echo "Disabling ADB" + systemctl stop adbd + stop_gadget +fi