core: make detach non-destructive
This commit is contained in:
parent
617a1d844f
commit
16c74879f2
@ -13,21 +13,36 @@ find_udc() {
|
||||
ls /sys/class/udc 2>/dev/null | head -n1 || true
|
||||
}
|
||||
|
||||
udc_state() {
|
||||
local udc="$1"
|
||||
if [[ -z $udc ]]; then
|
||||
echo "unknown"
|
||||
return 0
|
||||
fi
|
||||
cat "/sys/class/udc/$udc/state" 2>/dev/null || echo "unknown"
|
||||
}
|
||||
|
||||
detach_gadget() {
|
||||
local udc=""
|
||||
udc="$(find_udc)"
|
||||
local state
|
||||
state="$(udc_state "$udc")"
|
||||
case "$state" in
|
||||
configured|addressed|default|suspended)
|
||||
log "detach skipped (state=$state)"
|
||||
return 0
|
||||
;;
|
||||
esac
|
||||
if [[ -n $udc && -w /sys/class/udc/$udc/soft_connect ]]; then
|
||||
echo 0 >"/sys/class/udc/$udc/soft_connect" 2>/dev/null || true
|
||||
fi
|
||||
if [[ -e $G/UDC ]]; then
|
||||
if [[ -n ${LESAVKA_DETACH_CLEAR_UDC:-} && -e $G/UDC ]]; then
|
||||
echo "" >"$G/UDC" 2>/dev/null || true
|
||||
fi
|
||||
if [[ -n $udc && -f /sys/class/udc/$udc/state ]]; then
|
||||
local state
|
||||
state="$(cat "/sys/class/udc/$udc/state" 2>/dev/null || true)"
|
||||
if [[ -n $udc ]]; then
|
||||
log "detached (state=${state:-unknown})"
|
||||
else
|
||||
log "detached (no UDC state)"
|
||||
log "detached (no UDC)"
|
||||
fi
|
||||
}
|
||||
|
||||
@ -42,7 +57,10 @@ attach_gadget() {
|
||||
log "UDC not found; need full setup"
|
||||
return 1
|
||||
fi
|
||||
if [[ -e $G/UDC ]]; then
|
||||
if [[ -n $udc && -w /sys/class/udc/$udc/soft_connect ]]; then
|
||||
echo 1 >"/sys/class/udc/$udc/soft_connect" 2>/dev/null || true
|
||||
fi
|
||||
if [[ -n ${LESAVKA_ATTACH_WRITE_UDC:-} && -e $G/UDC ]]; then
|
||||
echo "$udc" >"$G/UDC" 2>/dev/null || true
|
||||
fi
|
||||
log "attached to $udc"
|
||||
@ -54,13 +72,17 @@ case "${1:-}" in
|
||||
detach_gadget
|
||||
exit 0
|
||||
;;
|
||||
--detach-hard)
|
||||
LESAVKA_DETACH_CLEAR_UDC=1 detach_gadget
|
||||
exit 0
|
||||
;;
|
||||
--attach)
|
||||
if attach_gadget; then
|
||||
exit 0
|
||||
fi
|
||||
;;
|
||||
--help|-h)
|
||||
echo "Usage: $0 [--attach|--detach]"
|
||||
echo "Usage: $0 [--attach|--detach|--detach-hard]"
|
||||
exit 0
|
||||
;;
|
||||
esac
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user