home *** CD-ROM | disk | FTP | other *** search
- #!/bin/bash
- #
- # alsasound This shell script takes care of starting and stopping
- # the ALSA sound driver.
- #
- # This script requires /usr/sbin/alsactl and /usr/bin/aconnect
- # programs from the alsa-utils package.
- #
- # Copyright (c) by Jaroslav Kysela <perex@suse.cz>
- # License: GNU GPL
- #
- # 2004-12-20: Kocil
- # On start : load stored values
- # If muted, set default values and store
- # On stop : If not muted, store values
- #
-
- if [ -r /etc/rc.config ]; then
- . /etc/rc.config
- rc_warning="\033[33m\033[1m"
- else
- rc_done="done"
- rc_warning=""
- rc_reset=""
- fi
-
- if [ x$START_ALSA != xno ]; then
- START_ALSA=yes
- fi
- if [ x$START_ALSA_SEQ != xno ]; then
- START_ALSA_SEQ=yes
- fi
-
- # Determine the base and follow a runlevel link name.
- base=${0##*/}
- link=${base#*[SK][0-9][0-9]}
-
- # Force execution if not called by a runlevel directory.
- test $link = $base && START_ALSA=yes
- test "$START_ALSA" = yes || exit 0
-
- alsactl=/usr/sbin/alsactl
- asoundcfg=/etc/asound.state
- aconnect=/usr/bin/aconnect
- alsascrdir=/etc/alsa.d
- amixer=/usr/bin/amixer
-
- lockfile=/var/lock/subsys/alsasound
-
- if [ -x /sbin/lsmod ]; then
- lsmod=/sbin/lsmod
- else
- lsmod=lsmod
- fi
-
- # modprobe returns 255 when failed..
- function probe_module () {
- /sbin/modprobe $*
- test $? = 0 && return 0
- return 1
- }
-
- #
- # set default mixer volumes
- #
- function mixer() {
- amixer set "$1" "$2" unmute >/dev/null 2>&1
- amixer set "$1" unmute >/dev/null 2>&1
- }
-
- function set_mixers() {
- mixer Master 75%
- mixer PCM 90%
- mixer Synth 90%
- mixer CD 90%
- # mute mic
- amixer set Mic 0% mute >/dev/null 2>&1
- # ESS 1969 chipset has 2 PCM channels
- mixer PCM,1 90%
- # Trident/YMFPCI/emu10k1
- mixer Wave 100%
- mixer Music 100%
- mixer AC97 100%
- # CS4237B chipset:
- mixer 'Master Digital' 75%
- # Envy24 chips with analog outs
- mixer DAC 90%
- mixer DAC,0 90%
- mixer DAC,1 90%
- # some notebooks use headphone instead of master
- mixer Headphone 75%
- mixer Playback 100%
- # turn off digital switches
- amixer set "SB Live Analog/Digital Output Jack" off >/dev/null 2>&1
- amixer set "Audigy Analog/Digital Output Jack" off >/dev/null 2>&1
- }
-
- # Check if the sound is muted
- function is_muted()
- {
- amixer get Master | grep -qE "(\[[0-9]%\]|\[off\])" && return 0
- amixer get PCM | grep -qE "(\[[0-9]%\]|\[off\])" && return 0
- return 1
- }
-
- function start() {
- #
- # insert all sound modules
- #
-
- module_loaded=0
- drivers=`/sbin/modprobe -c | \
- grep -E "^[[:space:]]*alias[[:space:]]+snd-card-[[:digit:]]" | \
- awk '{print $3}'`
- for i in $drivers; do
- if [ "$i" != off ]; then
- echo -n "Starting sound driver: $i "
- probe_module $i && module_loaded=1
- echo -e "$rc_done"
- fi
- done
- if test $module_loaded -eq 0; then
- echo "ERROR: Could not load ALSA drivers"
- return 1
- fi
- #
- # insert sequencer modules
- #
- if [ x"$START_ALSA_SEQ" = xyes -a -r /proc/asound/seq/drivers ]; then
- cut -d , -f 1 /proc/asound/seq/drivers | \
- while read t ; do
- test -z $t || /sbin/modprobe $t
- done
- fi
- #
- # restore driver settings
- #
- if [ -d /proc/asound ]; then
- set_mixers
- if [ -r $asoundcfg ] ; then
- if [ -x $alsactl ]; then
- $alsactl -F -f $asoundcfg restore
- if is_muted; then
- echo "Setting up ALSA with default values"
- set_mixers
- $alsactl -f $asoundcfg store
- fi
- else
- echo -e "${rc_warning}ERROR: alsactl not found${rc_reset}"
- fi
- fi
- else
- echo "alsa was not properly loaded"
- return 1
- fi
- #
- # run card-dependent scripts
- for i in $drivers; do
- t=${i##snd-}
- if [ -x $alsascrdir/$t ]; then
- $alsascrdir/$t
- fi
- done
-
- # touch lockfile if lockdir exists
- #
- if [ -d /var/lock/subsys ] ; then
- touch /var/lock/subsys/alsasound
- fi
- }
-
- function terminate() {
- #
- # Kill processes holding open sound devices
- #
- # DEVS=`find /dev/ -follow -type c -maxdepth 1 -print 2>/dev/null | xargs ls -dils | grep "1*1[46]," | cut -d: -f2 | cut -d" " -f2; echo /proc/asound/dev/*`
- fuser -k /dev/admmidi? /dev/adsp? /dev/amidi? /dev/audio* /dev/dmfm* \
- /dev/dmmidi? /dev/dsp* /dev/dspW* /dev/midi0? /dev/mixer? /dev/music \
- /dev/patmgr? /dev/sequencer* /dev/sndstat >/dev/null 2>&1
- if [ -d /proc/asound/dev ]; then
- fuser -k /proc/asound/dev/* >/dev/null 2>&1
- fi
- if [ -d /dev/snd ]; then
- fuser -k /dev/snd/* >/dev/null 2>&1
- fi
- #
- # remove all sequencer connections if any
- #
- if [ -f /proc/asound/seq/clients -a -x $aconnect ]; then
- $aconnect --removeall
- fi
- }
-
- function stop() {
- #
- # store driver settings
- #
- if ! is_muted; then
- if [ -x $alsactl ]; then
- $alsactl -f $asoundcfg store
- else
- echo -n -e "${rc_warning}!!!alsactl not found!!!${rc_reset} "
- fi
- fi
- # mute master to avoid clicks at unload
- #
- /usr/bin/amixer set Master mute >/dev/null 2>&1
- #
- # remove all sound modules
- #
- $lsmod | grep -E "^snd" | grep -Ev "(snd-page-alloc|snd_page_alloc)" | while read line; do \
- /sbin/rmmod `echo $line | cut -d ' ' -f 1`; \
- done
- # remove the 2.2 soundcore module (if possible)
- /sbin/rmmod soundcore 2> /dev/null
- /sbin/rmmod gameport 2> /dev/null
-
- #
- # remove lockfile if lockdir exists
- #
- if [ -d /var/lock/subsys ] ; then
- rm -f /var/lock/subsys/alsasound
- fi
- }
-
- # See how we were called.
- case "$1" in
- start)
- # Start driver if it isn't already up.
- if [ ! -f /var/lock/subsys/alsasound ]; then
- start
- else
- echo "ALSA driver is already running."
- fi
- ;;
- stop)
- # Stop daemons.
- if [ -d /proc/asound ]; then
- echo -n "Shutting down sound driver: "
- terminate
- stop
- fi
- ;;
- restart|reload)
- $0 stop
- $0 start
- ;;
- status)
- if [ -d /proc/asound ]; then
- echo -n "ALSA sound driver loaded."
- else
- echo -n "ALSA sound driver not loaded."
- fi
- echo
- ;;
- *)
- echo "Usage: alsasound {start|stop|restart|status}"
- exit 1
- esac
-
- exit 0
-