Skip to content
Open
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
25 changes: 20 additions & 5 deletions rpi-usb-gadget
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ SUPPORTED_DEVICES="$(printf '%s\n' 'Raspberry Pi Zero' 'Raspberry Pi Zero W' 'Ra

have_systemctl() { command -v systemctl >/dev/null 2>&1; }

# /sys/class/udc is populated when a UDC driver (dwc2 in peripheral mode) is active
overlay_active() { [ -n "$(ls /sys/class/udc/ 2>/dev/null)" ]; }

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
overlay_active() { [ -n "$(ls /sys/class/udc/ 2>/dev/null)" ]; }
overlay_active() { [ "$(ls -A /sys/class/udc 2>/dev/null)" ]; }

If I remember correctly this approach can be prone to shell special features and we might want to go more POSIX compliant here. What do you think?


cfg_has_overlay() {
grep -q "^${OVERLAY_LINE}\$" "$CFG_FW" 2>/dev/null || \
grep -q "^${OVERLAY_LINE}\$" "$CFG_LEGACY" 2>/dev/null
}

svc_enable_now() {
have_systemctl || return 0
systemctl daemon-reload || true
Expand Down Expand Up @@ -318,10 +326,17 @@ else

printf 'Turning \033[32mon\033[0m USB Gadget mode\n'
printf "g_ether\n" > "$MODS_CONF"
# ensure overlay present once
sed -i "\|^$OVERLAY_LINE\$|d" "$CFG_FW" 2>/dev/null || true
sed -i "\|^$OVERLAY_LINE\$|d" "$CFG_LEGACY" 2>/dev/null || true
printf '%s\n' "$OVERLAY_LINE" >> "$CFG_FW" 2>/dev/null || printf '%s\n' "$OVERLAY_LINE" >> "$CFG_LEGACY"
modprobe g_ether 2>/dev/null || true
NEED_REBOOT=false
if ! cfg_has_overlay; then
# ensure overlay present once
sed -i "\|^$OVERLAY_LINE\$|d" "$CFG_FW" 2>/dev/null || true
sed -i "\|^$OVERLAY_LINE\$|d" "$CFG_LEGACY" 2>/dev/null || true
printf '%s\n' "$OVERLAY_LINE" >> "$CFG_FW" 2>/dev/null || printf '%s\n' "$OVERLAY_LINE" >> "$CFG_LEGACY"
if ! overlay_active; then
NEED_REBOOT=true
fi
fi

Comment on lines +331 to 340

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if ! cfg_has_overlay; then
# ensure overlay present once
sed -i "\|^$OVERLAY_LINE\$|d" "$CFG_FW" 2>/dev/null || true
sed -i "\|^$OVERLAY_LINE\$|d" "$CFG_LEGACY" 2>/dev/null || true
printf '%s\n' "$OVERLAY_LINE" >> "$CFG_FW" 2>/dev/null || printf '%s\n' "$OVERLAY_LINE" >> "$CFG_LEGACY"
if ! overlay_active; then
NEED_REBOOT=true
fi
fi
if ! cfg_has_overlay; then
# ensure overlay present once
sed -i "\|^$OVERLAY_LINE\$|d" "$CFG_FW" 2>/dev/null || true
sed -i "\|^$OVERLAY_LINE\$|d" "$CFG_LEGACY" 2>/dev/null || true
printf '%s\n' "$OVERLAY_LINE" >> "$CFG_FW" 2>/dev/null || printf '%s\n' "$OVERLAY_LINE" >> "$CFG_LEGACY"
fi
if ! overlay_active; then
NEED_REBOOT=true
fi

If the module is already in the config but the Pi didn't restart yet the overlay_active will be false but since the check is in the conditional block NEED_REBOOT will not be set.

# NetworkManager: dnsmasq + client profile + shared profile
if have_nmcli; then
Expand All @@ -344,4 +359,4 @@ else
fi
fi

printf "Reboot to apply changes\n"
[ "${NEED_REBOOT:-false}" = true ] && printf "Reboot to apply changes\n" || true