Tuesday, November 6, 2018

Auto switching of audio output to headphones when connected on freebsd

It has been a lot of fun over a couple of months. I could have just written "fun" but it was really lot of fun. I have successfully installed and been using both freebsd and Openbsd on my laptop. Running operating systems with such long lineage on a modern 4K laptop is a dream come true. It is also testament to the amount of hard work hobby developers and users have put in keeping the BSD's usable to the next generation of users. I would like to write at length regarding my experiences in doing so. But, this post will only be related to configure auto switching of audio output to headphones when they are plugged in and vice-versa.

Let us start with enabling verbose boot from the boot menu. That is the screen u see the beastie once you boot freebsd from your favorite boot loader. The verbose boot option is not available in the first screen. Go to the next screen and choose it. Allow the OS to boot. Lots and lots of boot messages would scroll down on your screen. Dont worry this is a one time config. The next time you boot you will get back your non-verbose boot.

Get the dmesg output into a text file
$sudo dmesg | grep hda > boot.txt

The above command will save all the lines with "hda" which
Open the boot.txt with your favorite editor. Let us concentrate on the part


----------------------------
hdaa0: nid   0x    as seq device       conn  jack  
hdaa0: 18 b7a60130 3  0  Mic           Fixed Digital
hdaa0: 19 40000000 0  0  Line-out      None  Unknown
hdaa0: 20 411111f0 15 0  Speaker       None  1/8   
hdaa0: 22 411111f0 15 0  Speaker       None  1/8   
hdaa0: 23 90170110 1  0  Speaker       Fixed Analog
hdaa0: 24 411111f0 15 0  Speaker       None  1/8   
hdaa0: 25 03a11040 4  0  Mic           Jack  1/8   
hdaa0: 26 411111f0 15 0  Speaker       None  1/8   
hdaa0: 27 411111f0 15 0  Speaker       None  1/8   
hdaa0: 29 40600001 0  1  Modem-line    None  Unknown
hdaa0: 30 411111f0 15 0  Speaker       None  1/8   
hdaa0: 33 03211020 2  0  Headphones    Jack  1/8   
hdaa0: nid   0x    as seq device       conn  jack  
hdaa0: 18 b7a60130 3  0  Mic           Fixed Digital
hdaa0: 19 40000000 0  0  Line-out      None  Unknown
hdaa0: 20 411111f0 15 0  Speaker       None  1/8   
hdaa0: 22 411111f0 15 0  Speaker       None  1/8   
hdaa0: 23 90170110 1  0  Speaker       Fixed Analog
hdaa0: 25 03a11040 4  0  Mic           Jack  1/8   
hdaa0: 26 411111f0 15 0  Speaker       None  1/8   
hdaa0: 27 411111f0 15 0  Speaker       None  1/8   
hdaa0: 30 411111f0 15 0  Speaker       None  1/8   
hdaa0: 33 03211020 2  0  Headphones    Jack  1/8   
-----------------------------------
Now let us formulate a line which goes in /boot/device.hints

hint.hdaa.0.nid33.config="as=1 seq=15"

Let us go one by one

hint -> syntax
hdaa.0 -> is from the first column of all lines "hdaa0"
nid33 -> is from the second column and the number on the line of device column which has "Headphones"
config -> syntax
as=1 -> is from the column "as" corresponding to "Fixed analog" on column jack
seq=15 -> is from column "seq" and will be the most repeated number in that column

Dont forget to add
snd_driver_load="YES"   in your /boot/loader.conf

and
hw.snd.default_auto=1      in your /etc/sysctl.conf

Reboot. Profit.