#!/bin/sh
# SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
# SPDX-FileCopyrightText: 2025 Nate Graham <nate@kde.org>

# Turn USB auto-suspend back off after powertop --auto-tune for devices
# that misbehave when they sleep: input devices (KDE Linux's list), and on
# AtropOS also USB audio interfaces, Bluetooth adapters and USB network
# adapters, which drop out or crackle.

set -e

DRIVERS="usbhid snd-usb-audio btusb r8152 cdc_ether cdc_ncm ax88179_178a asix"

DEVICE_IDS=$(
    for driver in $DRIVERS; do
        [ -d "/sys/bus/usb/drivers/$driver" ] || continue
        find "/sys/bus/usb/drivers/$driver" -regex '.*\/[0-9:.-]+' -printf '%f\n'
    done | cut --delimiter ":" --fields 1 | sort --unique
)

for DEVICE_ID in $DEVICE_IDS; do
    if ! { DEVICE_PRETTY_NAME=$(cat "/sys/bus/usb/devices/${DEVICE_ID}/product"); } 2> /dev/null; then
        DEVICE_PRETTY_NAME="Unknown"
    fi
    DEVICE_INFO_MSG="USB auto-suspend for device \"${DEVICE_PRETTY_NAME}\" (with ID ${DEVICE_ID})"
    echo "Disabling ${DEVICE_INFO_MSG}"
    if ! (echo "on" >| "/sys/bus/usb/devices/${DEVICE_ID}/power/control") 2> /dev/null; then
        echo "Unable to disable ${DEVICE_INFO_MSG}"
    fi
done
