HomeLinux › Automatically Enable USB Headset

Automatically Enable USB Headset

I, along with many others, would like a USB headset to be the default sound device in ALSA whenever it’s connected, and it seems to be a tough thing to find out how to do.

Short answer: Create UDEV rules to invoke a script that will modify your ~/.asoundrc file.

Long answer:

Assumptions:

  1. You’re using Linux kernel >=2.6
  2. You’re using ALSA.
  3. You’re using UDEV
  4. You already have the headset working

First, we need to know what device we are looking for, so with the headset unplugged, fire up a terminal and type:
ls /dev/snd/
You’ll see something similar to the below:

localhost ~ # ls /dev/snd
by-path  controlC0  hwC0D0  hwC0D3  pcmC0D0c  pcmC0D0p  pcmC0D3p  seq  timer

Now, plug in the headset and check it again, ie:

localhost ~ # ls /dev/snd
by-id    controlC0  hwC0D0  pcmC0D0c  pcmC0D3p  pcmC1D0p  timer
by-path  controlC1  hwC0D3  pcmC0D0p  pcmC1D0c  seq

Notice the new items, in my case, the ones we are interested in are pcmC1D0p and pcmC1D0c. Note: The suffixes "p" and "c" indicate "Playback" and "Capture". For the purposes of this article, I’ll ignore the capture device, but the same process applies for it as for the playback.

Knowing the device name, we can now create the UDEV rules. Rules files are kept in /etc/udev/rules.d on my Gentoo system – other distros should be similar. You may have no files in there, or there may be many. I made a new file called /etc/udev/rules.d/99-audio.rules. the 99- prefix indicates the order in which the rules files will be applied, and I simply chose 99 to make this one run last.

My /etc/udev/rules.d/99-audio.rules file:

KERNEL=="pcmC1D0p", ACTION=="add", DRIVERS=="usb", PROGRAM="/home/pvint/headsetToggle.sh '%E{ACTION}'"
KERNEL=="pcmC1D0p", ACTION=="remove", DRIVERS=="usb", PROGRAM="/home/pvint/headsetToggle.sh '%E{ACTION}'"

Rough explanation: When UDEV sees a kernel message about the device "pcmC1D0p" via USB, and the action that occurred was that it was "add", it runs the script specified with the ACTION as a parameter (in this case, "add". Note that you’ll need to be root to do this.

Now for the script that actually changes the device, I should put it somewhere better, but I created /home/pvint/headsetToggle.sh with the following contents:

#!/bin/sh
# Set default ALSA device appropriately when USB  headset is plugged/unplugged
if [ "$1" = "add" ]
then
        echo 'defaults.pcm.card 1' > /home/pvint/.asoundrc
else
        echo 'defaults.pcm.card 0' > /home/pvint/.asoundrc
fi

Note that if you had anything in the .asoundrc file, it’ll be clobbered. This could easily be avoided by creating two dummy files like .asoundrc_headset and .asoundrc_normal and having the script copy the appropriate one to .asoundrc. The above was simple and suited my purpose just fine.

That should do it, so restart UDEV (ie: /etc/init.d udevd restart) and try it out.

Notes:

  • Check your /var/log/messages to ensure there were no errors with the UDEV rules
  • Some applications need to be restarted before this takes effect.
  • The script will be run as root, so be careful

This post was written by

– who has written 16 posts on DotSlash.ca.
Plain old nerd.

Email  • Google + • Facebook

Share Button

1 Comments.[ Leave a comment ]

  1. Hi there to all, it’s genuinely a nice for me to go to see this site,
    it includes important Information.

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>