1#!/bin/sh23# When controlC* device are added, restore the card's settings, or set Master and PCM to 10%.4# Some devices may have absolute bizzare defaults that may hurt the user in the process. *cough* Dragonfly Red 1.07 *cough*56# mdev -s does not expose $DEVNAME but $MDEV7if ! [ "${DEVNAME}" ] && [ "${MDEV}" ]; then8 DEVNAME="${MDEV}"9fi1011mute_mixers() {12 card_id="$1"13 for control in PCM Master; do14 amixer -q -M -c "${card_id}" set "${control}" '0%'15 done1617 return 018}1920restore_mixers() {21 # Running restore twice, with mute in-between, otherwise volume control may not be flushed to device.22 # Either it could be edge case with USB DAC firmware or issue with cross-loading state of one USB DAC23 # onto another one, as all we have is ID.2425 card_id="$1"26 alsactl restore "${card_id}" && mute_mixers "${card_id}" && alsactl restore "${card_id}"27}2829case "${DEVNAME}" in30 *'controlC'*)31 control_name="${DEVNAME##*/}"32 card_id="${control_name#controlC}"33 # Use NAME instead of numeric ID34 card_id="$(cat /proc/asound/card${card_id}/id)"35 if [ "${card_id}" ]; then36 # Now, we can heavly timeout here, because although we have the control device, the hardware may not yet be ready37 # so the query about mixers like PCM or Master may fail hard, better to wait for it to become ready38 # + amixer -q -c 2 scontrols39 # amixer: Mixer hw:2 load error: Connection timed out4041 i=142 while [ "$i" -lt 5 ]; do43 amixer -q -c "${card_id}" scontrols >/dev/null && break44 i="$(( i + 1))"45 done4647 # Always set PCM and Master to 0% prior to running `alsactl restore`.48 # It appears that *sometimes* the `restore` does not 'flushes' volume level49 # resulting in at least Dragonfly Red 1.07 blasting at full volume.50 mute_mixers "${card_id}"5152 # If there was no state for this device, set volume of Master and PCM to 10%.53 # It seems that loading mixer state not always reset volume (Dragonfly Cobalt).54 if ! restore_mixers "${card_id}"; then55 for control in PCM Master; do56 amixer -q -M -c "${card_id}" set "${control}" '10%'57 done58 fi59 fi60 ;;61esac