home *** CD-ROM | disk | FTP | other *** search
/ PCNET 2006 September - Disc 1 / PCNET_CD_2006_09.iso / linux / puppy-barebones-2.01r2.iso / pup_201.sfs / etc / rc.d / rc.alsa next >
Encoding:
Text File  |  2005-04-13  |  5.9 KB  |  262 lines

  1. #!/bin/bash
  2. #
  3. # alsasound     This shell script takes care of starting and stopping
  4. #               the ALSA sound driver.
  5. #
  6. # This script requires /usr/sbin/alsactl and /usr/bin/aconnect
  7. # programs from the alsa-utils package.
  8. #
  9. # Copyright (c) by Jaroslav Kysela <perex@suse.cz> 
  10. # License: GNU GPL
  11. #
  12. # 2004-12-20: Kocil
  13. # On start : load stored values
  14. #           If muted, set default values and store 
  15. # On stop  : If not muted, store values
  16.  
  17. if [ -r /etc/rc.config ]; then
  18.   . /etc/rc.config
  19.   rc_warning="\033[33m\033[1m"
  20. else
  21.   rc_done="done"
  22.   rc_warning=""
  23.   rc_reset=""
  24. fi
  25.  
  26. if [ x$START_ALSA != xno ]; then
  27.   START_ALSA=yes
  28. fi
  29. if [ x$START_ALSA_SEQ != xno ]; then
  30.   START_ALSA_SEQ=yes
  31. fi
  32.  
  33. # Determine the base and follow a runlevel link name.
  34. base=${0##*/}
  35. link=${base#*[SK][0-9][0-9]}
  36.  
  37. # Force execution if not called by a runlevel directory.
  38. test $link = $base && START_ALSA=yes
  39. test "$START_ALSA" = yes || exit 0
  40.  
  41. alsactl=/usr/sbin/alsactl
  42. asoundcfg=/etc/asound.state
  43. aconnect=/usr/bin/aconnect
  44. alsascrdir=/etc/alsa.d
  45. amixer=/usr/bin/amixer
  46.  
  47. lockfile=/var/lock/subsys/alsasound
  48.  
  49. if [ -x /sbin/lsmod ]; then
  50.   lsmod=/sbin/lsmod
  51. else
  52.   lsmod=lsmod
  53. fi
  54.  
  55. # modprobe returns 255 when failed..
  56. function probe_module () {
  57.    /sbin/modprobe $*
  58.    test $? = 0 && return 0
  59.    return 1
  60. }
  61.  
  62. #
  63. # set default mixer volumes
  64. #
  65. function mixer() {
  66.   amixer set "$1" "$2" unmute >/dev/null 2>&1
  67.   amixer set "$1" unmute >/dev/null 2>&1
  68. }
  69.  
  70. function set_mixers() {
  71.     mixer Master 75%
  72.     mixer PCM 90%
  73.     mixer Synth 90%
  74.     mixer CD 90%
  75.     # mute mic
  76.     amixer set Mic 0% mute >/dev/null 2>&1
  77.     # ESS 1969 chipset has 2 PCM channels
  78.     mixer PCM,1 90%
  79.     # Trident/YMFPCI/emu10k1
  80.     mixer Wave 100%
  81.     mixer Music 100%
  82.     mixer AC97 100%
  83.     # CS4237B chipset:
  84.     mixer 'Master Digital' 75%
  85.     # Envy24 chips with analog outs
  86.     mixer DAC 90%
  87.     mixer DAC,0 90%
  88.     mixer DAC,1 90%
  89.     # some notebooks use headphone instead of master
  90.     mixer Headphone 75%
  91.     mixer Playback 100%
  92.     # turn off digital switches
  93.     amixer set "SB Live Analog/Digital Output Jack" off >/dev/null 2>&1
  94.     amixer set "Audigy Analog/Digital Output Jack" off >/dev/null 2>&1
  95. }
  96.  
  97. # Check if the sound is muted
  98. function is_muted()
  99. {
  100.     amixer get Master | grep -qE "(\[[0-9]%\]|\[off\])" && return 0
  101.     amixer get PCM    | grep -qE "(\[[0-9]%\]|\[off\])" && return 0
  102.     return 1
  103. }
  104.  
  105. function start() {
  106.   #
  107.   # insert all sound modules
  108.   #
  109.  
  110.   module_loaded=0
  111.   drivers=`/sbin/modprobe -c | \
  112.     grep -E "^[[:space:]]*alias[[:space:]]+snd-card-[[:digit:]]" | \
  113.     awk '{print $3}'`
  114.   for i in $drivers; do
  115.     if [ "$i" != off ]; then
  116.       echo -n "Starting sound driver: $i "
  117.       probe_module $i && module_loaded=1
  118.       echo -e "$rc_done"
  119.     fi
  120.   done
  121.   if test $module_loaded -eq 0; then
  122.      echo "ERROR: Could not load ALSA drivers"
  123.      return 1
  124.   fi
  125.   #
  126.   # insert sequencer modules
  127.   #
  128.   if [ x"$START_ALSA_SEQ" = xyes -a -r /proc/asound/seq/drivers ]; then
  129.     cut -d , -f 1 /proc/asound/seq/drivers | \
  130.     while read t ; do
  131.       test -z $t || /sbin/modprobe $t
  132.     done
  133.   fi
  134.   #
  135.   # restore driver settings
  136.   #
  137.   if [ -d /proc/asound ]; then
  138.     set_mixers
  139.     if [ -r $asoundcfg ] ; then
  140.       if [ -x $alsactl ]; then
  141.         $alsactl -F -f $asoundcfg restore
  142.         if is_muted; then
  143.           echo "Setting up ALSA with default values"
  144.       set_mixers
  145.           $alsactl -f $asoundcfg store
  146.         fi
  147.       else
  148.         echo -e "${rc_warning}ERROR: alsactl not found${rc_reset}"
  149.       fi
  150.     fi
  151.   else
  152.     echo "alsa was not properly loaded"
  153.     return 1
  154.   fi
  155.   #
  156.   # run card-dependent scripts
  157.   for i in $drivers; do
  158.     t=${i##snd-}
  159.     if [ -x $alsascrdir/$t ]; then
  160.       $alsascrdir/$t
  161.     fi
  162.   done
  163.  
  164.   # touch lockfile if lockdir exists
  165.   #
  166.   if [ -d /var/lock/subsys ] ; then
  167.     touch /var/lock/subsys/alsasound
  168.   fi
  169. }
  170.  
  171. function terminate() {
  172.   #
  173.   # Kill processes holding open sound devices
  174.   #
  175.   # 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/*`
  176.   fuser -k /dev/admmidi? /dev/adsp? /dev/amidi? /dev/audio* /dev/dmfm* \
  177.      /dev/dmmidi? /dev/dsp* /dev/dspW* /dev/midi0? /dev/mixer? /dev/music \
  178.      /dev/patmgr? /dev/sequencer* /dev/sndstat >/dev/null 2>&1
  179.   if [ -d /proc/asound/dev ]; then
  180.     fuser -k /proc/asound/dev/* >/dev/null 2>&1
  181.   fi
  182.   if [ -d /dev/snd ]; then
  183.     fuser -k /dev/snd/* >/dev/null 2>&1
  184.   fi
  185.   #
  186.   # remove all sequencer connections if any
  187.   #
  188.   if [ -f /proc/asound/seq/clients -a -x $aconnect ]; then
  189.     $aconnect --removeall
  190.   fi
  191. }
  192.  
  193. function stop() {
  194.   #
  195.   # store driver settings
  196.   #
  197.   if ! is_muted; then
  198.     if [ -x $alsactl ]; then
  199.     $alsactl -f $asoundcfg store
  200.     else
  201.     echo -n -e "${rc_warning}!!!alsactl not found!!!${rc_reset} "
  202.     fi
  203.   fi
  204.   # mute master to avoid clicks at unload
  205.   #
  206.   /usr/bin/amixer set Master mute >/dev/null 2>&1
  207.   #
  208.   # remove all sound modules
  209.   #
  210.   $lsmod | grep -E "^snd" | grep -Ev "(snd-page-alloc|snd_page_alloc)" | while read line; do \
  211.      /sbin/rmmod `echo $line | cut -d ' ' -f 1`; \
  212.   done
  213.   # remove the 2.2 soundcore module (if possible)
  214.   /sbin/rmmod soundcore 2> /dev/null
  215.   /sbin/rmmod gameport 2> /dev/null
  216.  
  217.   #
  218.   # remove lockfile if lockdir exists
  219.   #
  220.   if [ -d /var/lock/subsys ] ; then
  221.     rm -f /var/lock/subsys/alsasound
  222.   fi
  223. }
  224.  
  225. # See how we were called.
  226. case "$1" in
  227.   start)
  228.         # Start driver if it isn't already up.
  229.     if [ ! -f /var/lock/subsys/alsasound ]; then
  230.       start
  231.     else
  232.       echo "ALSA driver is already running."
  233.     fi
  234.         ;;
  235.   stop)
  236.         # Stop daemons.
  237.     if [ -d /proc/asound ]; then
  238.           echo -n "Shutting down sound driver: "
  239.       terminate
  240.       stop
  241.     fi
  242.         ;;
  243.   restart|reload)
  244.     $0 stop
  245.     $0 start
  246.     ;;
  247.   status)
  248.         if [ -d /proc/asound ]; then
  249.           echo -n "ALSA sound driver loaded."
  250.         else
  251.           echo -n "ALSA sound driver not loaded."
  252.         fi
  253.         echo
  254.         ;;
  255.   *)
  256.         echo "Usage: alsasound {start|stop|restart|status}"
  257.         exit 1
  258. esac
  259.  
  260. exit 0
  261.