Changing card IDs with udev

From AlsaProject
Revision as of 23:06, 23 July 2012 by ClemensLadisch (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Determining the device path

First, run the command

udevadm monitor --kernel --subsystem-match=sound

Then plug your USB device in; the output will look like this:

KERNEL[1343075515.559055] add      /devices/pci0000:00/0000:00:12.2/usb1/1-4/1-4.3/1-4.3:1.0/sound/card5 (sound)
KERNEL[1343075515.559673] add      /devices/pci0000:00/0000:00:12.2/usb1/1-4/1-4.3/1-4.3:1.0/sound/card5/pcmC5D0p (sound)
KERNEL[1343075515.559978] add      /devices/pci0000:00/0000:00:12.2/usb1/1-4/1-4.3/1-4.3:1.0/sound/card5/pcmC5D0c (sound)
KERNEL[1343075515.561556] add      /devices/pci0000:00/0000:00:12.2/usb1/1-4/1-4.3/1-4.3:1.0/sound/card5/controlC5 (sound)
KERNEL[1343075515.575976] change   /devices/pci0000:00/0000:00:12.2/usb1/1-4/1-4.3/1-4.3:1.0/sound/card5 (sound)

For PCI cards, unload and reload the driver module; the output will look like this:

KERNEL[1343075492.525461] add      /devices/pci0000:00/0000:00:14.4/0000:06:05.0/sound/card2 (sound)
KERNEL[1343075492.530679] add      /devices/pci0000:00/0000:00:14.4/0000:06:05.0/sound/card2/midiC2D0 (sound)
KERNEL[1343075492.536421] add      /devices/pci0000:00/0000:00:14.4/0000:06:05.0/sound/card2/midi2 (sound)
KERNEL[1343075492.541358] add      /devices/pci0000:00/0000:00:14.4/0000:06:05.0/sound/card2/dmmidi2 (sound)
KERNEL[1343075492.546662] add      /devices/pci0000:00/0000:00:14.4/0000:06:05.0/sound/card2/pcmC2D1p (sound)
KERNEL[1343075492.551704] add      /devices/pci0000:00/0000:00:14.4/0000:06:05.0/sound/card2/pcmC2D1c (sound)
KERNEL[1343075492.557349] add      /devices/pci0000:00/0000:00:14.4/0000:06:05.0/sound/card2/pcmC2D0p (sound)
KERNEL[1343075492.562739] add      /devices/pci0000:00/0000:00:14.4/0000:06:05.0/sound/card2/pcmC2D0c (sound)
KERNEL[1343075492.568942] add      /devices/pci0000:00/0000:00:14.4/0000:06:05.0/sound/card2/controlC2 (sound)
KERNEL[1343075492.574929] change   /devices/pci0000:00/0000:00:14.4/0000:06:05.0/sound/card2 (sound)

The device path we want is the string that is output for the last change event.
In these example, the device path would be /devices/pci0000:00/0000:00:12.2/usb1/1-4/1-4.3/1-4.3:1.0/sound/card5 or /devices/pci0000:00/0000:00:14.4/0000:06:05.0/sound/card2.

Creating udev rules

Create a file in /lib/udev/rules.d/ with a name like 85-my-usb-audio.rules and contents like the following:

SUBSYSTEM!="sound", GOTO="my_usb_audio_end"
ACTION!="add", GOTO="my_usb_audio_end"

DEVPATH=="/devices/pci0000:00/0000:00:12.2/usb1/1-4/1-4.3/1-4.3:1.0/sound/card?", ATTR{id}="MyDev_A"
DEVPATH=="/devices/pci0000:00/0000:00:12.2/usb1/1-4/1-4.2/1-4.2:1.0/sound/card?", ATTR{id}="MyDev_B"

LABEL="my_usb_audio_end"

The SUBSYSTEM and ACTION rules skip over the file if udev is called for any non-sound device, or if some sound device is removed.

The DEVPATH=="..." check matches our cards. Please note the "card?" at the end which will match for any random sound card number.

The ATTR{id}="..." assignment actually changes the card's ID. Use whatever ID you want (but most special characters are not allowed).

Using the devices

Use the ID string where you would otherwise use the card number, such as

aplay -D default:MyDev_A something.wav
mplayer -ao alsa:device=hw=MyDev_B somethingelse.avi
Custom Search
Personal tools
Namespaces

Variants
Actions
Navigation
wiki
Toolbox