Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 94 additions & 8 deletions app/src/main/assets/scripts/gadget_common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,53 @@
# - Status is always the LAST stdout line: RootManager merges stderr after
# stdout, so keep helper stderr silent (2>/dev/null) or parsing breaks.

# sys.usb.config captured before we take over; restored on disable.
USB_CONFIG_STATE=/data/local/tmp/isodroid.usb_config

# UDC sysfs dir (overridable for tests).
UDC_SYS_DIR=${UDC_SYS_DIR:-/sys/class/udc}

# Log a gadget-related message to stderr.
gadget_log() {
echo "[isodroid] $1" >&2
}

# Save the live USB mode before quiescing the controller.
usb_config_capture() {
PREV_CONFIG=$(getprop sys.usb.config 2>/dev/null | tr -d '[:space:]')
case "$PREV_CONFIG" in
'')
rm -f "$USB_CONFIG_STATE" 2>/dev/null || true
;;
none)
# Keep the mode captured before the first enable.
if gadget_is_bound && our_link_present; then
:
elif [ ! -f "$USB_CONFIG_STATE" ]; then
printf 'none\n' > "$USB_CONFIG_STATE" 2>/dev/null || true
fi
;;
*)
printf '%s\n' "$PREV_CONFIG" > "$USB_CONFIG_STATE" 2>/dev/null || true
;;
esac
}

# Restore the user's mode; never force adb back on.
usb_config_restore() {
RESTORE_CONFIG=$(cat "$USB_CONFIG_STATE" 2>/dev/null | tr -d '[:space:]')
rm -f "$USB_CONFIG_STATE" 2>/dev/null || true
if [ -z "$RESTORE_CONFIG" ]; then
# No captured mode: derive a default from the adb switch.
if [ "$(settings get global adb_enabled 2>/dev/null | tr -d '[:space:]')" = "1" ]; then
RESTORE_CONFIG=mtp,adb
else
RESTORE_CONFIG=mtp
fi
fi
setprop sys.usb.config "$RESTORE_CONFIG" 2>/dev/null || true
}

# Load the kernel modules required for USB gadget and mass storage support.
ensure_modules() {
for mod in loop libcomposite usb_f_mass_storage; do
Expand Down Expand Up @@ -55,12 +97,25 @@ detect_config_root() {
}

# Detect the active USB controller for binding the gadget.
# Prefer the bound UDC, then sys.usb.controller, then the UDC list.
detect_udc_name() {
# Prefer the live UDC list over the (often stale) system property.
UDC_NAME=$(ls -1 /sys/class/udc 2>/dev/null | head -n1 | tr -d '[:space:]')
UDC_NAME=$(cat "$G_DIR/UDC" 2>/dev/null | tr -d '[:space:]')
case "$UDC_NAME" in
''|none|'(none)') UDC_NAME="" ;;
esac
if [ -z "$UDC_NAME" ]; then
UDC_NAME=$(getprop sys.usb.controller 2>/dev/null | tr -d '[:space:]')
fi
if [ -z "$UDC_NAME" ]; then
UDC_NAME=$(ls -1 "$UDC_SYS_DIR" 2>/dev/null | head -n1 | tr -d '[:space:]')
fi
# Validate the controller; only fall back if the UDC list is readable.
if [ -n "$UDC_NAME" ] && [ -d "$UDC_SYS_DIR" ]; then
ALT_UDC=$(ls -1 "$UDC_SYS_DIR" 2>/dev/null | head -n1 | tr -d '[:space:]')
if [ -n "$ALT_UDC" ] && [ ! -e "$UDC_SYS_DIR/$UDC_NAME" ]; then
UDC_NAME="$ALT_UDC"
fi
fi
}

# Initialize the USB gadget paths and metadata needed for all later operations.
Expand Down Expand Up @@ -124,23 +179,33 @@ unbind_gadget() {
fi
}

# True when the UDC node holds no controller ("none" included).
udc_free() {
CUR=$(cat "$UDC_FILE" 2>/dev/null | tr -d '[:space:]')
[ -z "$CUR" ] || [ "$CUR" = "none" ] || [ "$CUR" = "(none)" ]
}

# Check if the gadget is currently bound to the UDC.
gadget_is_bound() {
if [ -z "$UDC_NAME" ]; then return 1; fi
CUR=$(cat "$UDC_FILE" 2>/dev/null | tr -d '[:space:]')
[ -n "$CUR" ] && [ "$CUR" = "$UDC_NAME" ]
[ -n "$CUR" ] && [ "$CUR" != "none" ] && [ "$CUR" != "(none)" ] && [ "$CUR" = "$UDC_NAME" ]
}

# Check if the gadget is currently unbound.
gadget_is_unbound() {
CUR=$(cat "$UDC_FILE" 2>/dev/null | tr -d '[:space:]')
[ -z "$CUR" ]
udc_free
}

# True once init finished the sys.usb.config=none transition.
hal_settled() {
[ "$(getprop sys.usb.state 2>/dev/null | tr -d '[:space:]')" = "none" ] && udc_free
}

# Read the raw UDC driver state.
udc_state() {
if [ -z "$UDC_NAME" ]; then return 1; fi
cat "/sys/class/udc/$UDC_NAME/state" 2>/dev/null | tr -d '[:space:]'
cat "$UDC_SYS_DIR/$UDC_NAME/state" 2>/dev/null | tr -d '[:space:]'
}

# Check if the UDC state matches the expected value.
Expand Down Expand Up @@ -191,9 +256,30 @@ await_live_gadget() {
return 1
}

# Check if our mass_storage link is present in the config.
# Check if the config has a live link that points at our function.
our_link_present() {
[ -L "$CONFIG_PATH/$FUNC_NAME" ] || [ -L "$CONFIG_PATH/f100" ] || [ -L "$CONFIG_PATH/mass_storage" ]
for l in "$CONFIG_PATH"/*; do
[ -L "$l" ] || continue
# Skip dangling links.
[ -e "$l" ] || continue
case "$(readlink "$l" 2>/dev/null)" in
*"/functions/$FUNC_NAME") return 0 ;;
esac
done
return 1
}

# Link our function under a free name without clobbering HAL links.
ensure_our_link() {
if our_link_present; then return 0; fi
for NAME in "$FUNC_NAME" isodroid f100 mass_storage funcmass; do
if [ -e "$CONFIG_PATH/$NAME" ] || [ -L "$CONFIG_PATH/$NAME" ]; then
continue
fi
ln -s "$FUNC_PATH" "$CONFIG_PATH/$NAME" 2>/dev/null || continue
if our_link_present; then return 0; fi
done
return 1
}

# Bind the UDC once and verify it stuck.
Expand Down
7 changes: 3 additions & 4 deletions app/src/main/assets/scripts/turn_off_gadget.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ for f in "$FUNC_PATH"/lun*/file; do
fi
done

OLD_STATE=$(udc_state)
unbind_gadget
poll_until_success 5 udc_state_changed_from "$OLD_STATE" || true
poll_until_success 20 udc_free || true

# Remove only our mass_storage link (keep adb/ffs links).
for l in "$CONFIG_PATH"/*; do
Expand All @@ -34,6 +33,6 @@ if [ -d "$CONFIG_ROOT/usb_gadget/g2" ]; then
rmdir "$CONFIG_ROOT/usb_gadget/g2/functions" "$CONFIG_ROOT/usb_gadget/g2/configs/b.1" "$CONFIG_ROOT/usb_gadget/g2/configs" "$CONFIG_ROOT/usb_gadget/g2/strings/0x409" "$CONFIG_ROOT/usb_gadget/g2/strings" "$CONFIG_ROOT/usb_gadget/g2" 2>/dev/null || true
fi

# Hand the controller back to Android's USB HAL.
setprop sys.usb.config mtp,adb 2>/dev/null || true
# Hand the controller back to Android's USB HAL in the mode the user had.
usb_config_restore
echo "Success"
17 changes: 13 additions & 4 deletions app/src/main/assets/scripts/turn_on_gadget.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@ USB_SERIAL=$4

gadget_locate || exit 1

# Quiesce Android USB HAL so it does not re-bind mid-setup (main EBUSY source).
# Remember the current mode so disable can restore it.
usb_config_capture

# Quiesce the USB HAL and wait for its async handler (main EBUSY source).
setprop sys.usb.config none 2>/dev/null || true
OLD_STATE=$(udc_state)
poll_until_success 20 hal_settled || true
# The controller is free now: prefer sys.usb.controller over the old binding.
detect_udc_name
if ! gadget_is_unbound; then
unbind_gadget
poll_until_success 5 udc_state_changed_from "$OLD_STATE" || true
poll_until_success 20 udc_free || true
fi

# Remove only our mass_storage link (keep adb/ffs links intact).
Expand Down Expand Up @@ -83,7 +88,11 @@ while [ $i -lt "$MAX_DEVICES" ]; do
i=$((i + 1))
done

ln -s "$FUNC_PATH" "$CONFIG_PATH/$FUNC_NAME" 2>/dev/null || true
# Link into the config under a free name (never reuse HAL/other-app links).
if ! ensure_our_link; then
echo "Error: Could not link mass_storage into the USB config"
exit 1
fi

# Drop stale LUNs beyond the new limit; only empty ones are removed and the bare lun dir is never touched.
i=$MAX_DEVICES
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,10 @@ class RootManager(context: Context) {
"echo cfg=\$( [ -d /sys/kernel/config/usb_gadget ] && echo /sys/kernel/config" +
" || { [ -d /config/usb_gadget ] && echo /config || echo none; } )" +
" udc=\$(ls -1 /sys/class/udc 2>/dev/null | tr '\\n' ',')" +
" state=\$(cat /sys/class/udc/*/state 2>/dev/null | tr '\\n' ',')"
" state=\$(cat /sys/class/udc/*/state 2>/dev/null | tr '\\n' ',')" +
" usbcfg=\$(getprop sys.usb.config 2>/dev/null)" +
" usbstate=\$(getprop sys.usb.state 2>/dev/null)" +
" ffs=\$(getprop sys.usb.ffs.ready 2>/dev/null)"
)
).trim()
return out.ifEmpty { "unknown" }
Expand Down
Loading