# Autodetect sound card (script)



## soko1 (Nov 18, 2008)

snddetect:

```
#/bin/sh

# (c) 2008 Dmitry Klimov [TrueBSD Project] <lazyklimm@TrueBSD.org>
# (c) 2008 Sokolov Alexey [TrueBSD Project] <sokolov@TrueBSD.org>

if [ `whoami` != "root" ]; then
    echo "This utility should only be run as root"
    echo "Please use 'sudo $0' instead"
    sleep 3
    exit
fi

echo "Please wait..."

LOADERCONF=/boot/loader.conf

SNDSTAT="/dev/sndstat"

sed -i "" '/snd/ c\' /boot/loader.conf
(kldload snd_driver) 2>> /dev/null
if [ -e "$SNDSTAT" ] #file exists
then
SNDDRVS=`cat $SNDSTAT| awk '{for(k=1;k<NF;++k){if($k~"snd_") {print $k}}}'`
        #kldunload snd_driver
        if [ -n "$SNDDRVS" ]
        then
               for SNDDRV in $SNDDRVS
                do
                echo "${SNDDRV}_load=\"YES\"" >> /boot/loader.conf
                #kldload ${SNDDRV}
		echo "'${SNDDRV}' detected!"
                done
        fi
        #TODO: add sound system test.
else
        echo "No sound card detected, don't be upset, SILENCE IS GOLDEN!"
fi

uniq ${LOADERCONF} /tmp/tmp 
mv /tmp/tmp ${LOADERCONF}
exit
```

Use:

%sudo ./snddetect

```
Please wait...

'snd_hda' detected!
```

%grep snd /boot/loader.conf

```
snd_hda_load="YES"
```


----------



## smooth (Nov 19, 2008)

great job man thanx allot


----------



## rodrigo (Nov 26, 2008)

Another option:


```
#!/bin/sh
#
# Detect sound driver
for driver in /boot/kernel/snd_*; do
  driver=$(echo ${driver} | sed 's|/boot/kernel/snd_||')
  if [ ${driver} = "driver.ko" ]; then
    continue;
  fi
  
  kldload snd_${driver}
  if [ -c /dev/mixer0 ]; then
    echo "A driver was found : 'snd_${driver}'"
    echo "Add the driver to /boot/loader.conf'"
    
    echo "## Added by the detect_sound script" >> /boot/loader.conf
    echo "snd_${driver}_load=\"YES\""           >> /boot/loader.conf
    exit 0
  fi
  kldunload snd_${driver}
done
```


----------



## ph0enix (Mar 25, 2009)

soko1 said:
			
		

> snddetect:
> 
> ```
> #/bin/sh
> ...



Thanks but why did the script load all the other snd modules as well?

```
9    1 0xffffffff80ea4000 84a      snd_driver.ko
10    1 0xffffffff80ea5000 2202     snd_vibes.ko
11    1 0xffffffff80ea8000 1806     snd_via82c686.ko
12    1 0xffffffff80eaa000 3722     snd_via8233.ko
13    1 0xffffffff80eae000 21c2     snd_t4dwave.ko
14    3 0xffffffff80eb1000 b8a      snd_spicds.ko
15    1 0xffffffff80eb2000 257e     snd_solo.ko
16    4 0xffffffff80eb5000 18a6     snd_sbc.ko
17    1 0xffffffff80eb7000 173e     snd_sb8.ko
18    1 0xffffffff80eb9000 1c3e     snd_sb16.ko
19    1 0xffffffff80ebb000 e302     snd_neomagic.ko
20    2 0xffffffff80eca000 6b8c     snd_mss.ko
21    1 0xffffffff80ed1000 6c52     snd_maestro3.ko
22    1 0xffffffff80ed8000 477a     snd_maestro.ko
23    1 0xffffffff80edd000 2cfe     snd_ich.ko
24    1 0xffffffff80ee0000 1682     snd_fm801.ko
25    1 0xffffffff80ee2000 20b6     snd_ess.ko
26    1 0xffffffff80ee5000 4be2     snd_es137x.ko
27    1 0xffffffff80eea000 4322     snd_envy24ht.ko
28    1 0xffffffff80eef000 4722     snd_envy24.ko
29    1 0xffffffff80ef4000 d25e     snd_emu10kx.ko
30    1 0xffffffff80f02000 8b0e     snd_ds1.ko
31    2 0xffffffff80f0b000 428c     snd_csa.ko
32    1 0xffffffff80f10000 2202     snd_cs4281.ko
33    1 0xffffffff80f13000 1f9e     snd_cmi.ko
34    1 0xffffffff80f15000 2d42     snd_atiixp.ko
35    1 0xffffffff80f18000 1e22     snd_als4000.ko
36    1 0xffffffff80f1a000 1962     snd_ad1816.ko
```

Just snd_hda.ko and sound.ko usually do the trick.

J.


----------



## DutchDaemon (Mar 25, 2009)

If I remember correctly: I think loading sound.ko loads everything under the sun. Loading snd_hda.ko _without_ loading sound.ko _anywhere_ will load sound.ko as a dependency of snd_hda.ko while booting, without loading all of the snd modules.


----------



## lme@ (Mar 25, 2009)

Yup, DutchDaemon is right.


----------



## SirDice (Mar 25, 2009)

DutchDaemon said:
			
		

> If I remember correctly: I think loading sound.ko loads everything under the sun. Loading snd_hda.ko _without_ loading sound.ko _anywhere_ will load sound.ko as a dependency of snd_hda.ko while booting, without loading all of the snd modules.



If I'm not mistaken it's snd_driver.ko that does this not sound.ko.


----------



## DutchDaemon (Mar 25, 2009)

Yeah, I think that's about right. Anyway: only load the snd_ driver you need, nothing else. There's a bit of background here.


----------



## ph0enix (Mar 25, 2009)

DutchDaemon said:
			
		

> If I remember correctly: I think loading sound.ko loads everything under the sun. Loading snd_hda.ko _without_ loading sound.ko _anywhere_ will load sound.ko as a dependency of snd_hda.ko while booting, without loading all of the snd modules.



I noticed that it was the case on i386.  Now that I'm running AMD64, kldload'ing sound.ko, doesn't load all the extra modules.


----------



## DutchDaemon (Mar 25, 2009)

I believe SirDice was right: it's snd_driver.ko that pulls everything in.


----------



## ph0enix (Mar 25, 2009)

rodrigo said:
			
		

> Another option:
> 
> 
> ```
> ...



This script loaded snd_ad1816 for my card.  The module that's been working for me is snd_hda.  I'll test it with snd_ad1816 though to see if that works too.


----------



## ph0enix (Mar 25, 2009)

DutchDaemon said:
			
		

> I believe SirDice was right: it's snd_driver.ko that pulls everything in.



Sorry, I should have read all the replies before responding.

J.


----------



## MikeyIckey (May 6, 2009)

Thanks so much for posting this script! It made it so much easier getting sound working.


----------

