#!/bin/sh

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

set -e

case "$1" in
  start)
        echo -n "Initializing DSP..."

        if [ -c /dev/dspctl ]
        then
            # udevd is configured badly
            rm /dev/dspctl
        fi

        if [ -x /usr/sbin/mkdev.sh -a ! -c /dev/dspctl/ctl ]
        then
            /usr/sbin/mkdev.sh > /dev/null 2>&1
        fi
        # workaround for DSP utilities bug
        if [ ! -d /dev/dsptask ]
        then
            mkdir /dev/dsptask
        fi

        if [ -x /usr/sbin/dspctl -a ! -f /var/run/dsp_mem_reserve ]
        then
            /usr/sbin/dspctl kmem_reserve 360000
            touch /var/run/dsp_mem_reserve
        fi
        if [ -x /usr/sbin/dsp_dld ]
        then
            /usr/sbin/dsmetool -c 5 -T 300 -t "/usr/sbin/dsp_dld -p --disable-restart -c /lib/dsp/dsp_dld_avs.conf"
        fi
        # Temporary hack to ensure dsp has been loaded:
        if [ `uname -m` = armv6l ]; then
            while [ ! -f /sys/devices/platform/dsp/loadinfo ]; do
                echo -n .
                sleep 1
            done
            cat /sys/devices/platform/dsp/loadinfo >/dev/null
            # The following should really be done by udev (or alsa)
            if [ ! -e /dev/snd/controlC0 ]; then
                mkdir -p /dev/snd
                mknod -m 666 /dev/snd/controlC0 c 116 0
                echo -n "Created /dev/snd/controlC0..."
            fi
        fi
        # end temporary hack
        echo "done."
        ;;
  stop)
        if [ -x /usr/sbin/dsp_dld ]
        then
            /usr/sbin/dsmetool -k /usr/sbin/dsp_dld
        fi
        ;;
  restart|force-reload)
        ;;
  *)
        N=/etc/init.d/dsp-init
        echo "Usage: $N {start|stop|restart|force-reload}" >&2
        exit 1
        ;;
esac

exit 0

