Search code examples
linux-device-driveralsa

How can i identify multiple USB sound cards


I have a Linux computer with a usb hub and multiple usb sound cards.

Here is what i can see with aplay -l command:

aplay -l
**** Liste des Périphériques Matériels PLAYBACK ****
carte 0: ALSA [bcm2835 ALSA], périphérique 0: bcm2835 ALSA [bcm2835 ALSA]
  Sous-périphériques: 8/8
  Sous-périphérique #0: subdevice #0
  Sous-périphérique #1: subdevice #1
  Sous-périphérique #2: subdevice #2
  Sous-périphérique #3: subdevice #3
  Sous-périphérique #4: subdevice #4
  Sous-périphérique #5: subdevice #5
  Sous-périphérique #6: subdevice #6
  Sous-périphérique #7: subdevice #7
carte 0: ALSA [bcm2835 ALSA], périphérique 1: bcm2835 ALSA [bcm2835 IEC958/HDMI]
  Sous-périphériques: 1/1
  Sous-périphérique #0: subdevice #0
carte 1: Device [USB PnP Sound Device], périphérique 0: USB Audio [USB Audio]
  Sous-périphériques: 1/1
  Sous-périphérique #0: subdevice #0
carte 2: Device_1 [USB PnP Sound Device], périphérique 0: USB Audio [USB Audio]
  Sous-périphériques: 1/1
  Sous-périphérique #0: subdevice #0
carte 3: Device_2 [USB PnP Sound Device], périphérique 0: USB Audio [USB Audio]
  Sous-périphériques: 1/1
  Sous-périphérique #0: subdevice #0
carte 4: Device_3 [USB PnP Sound Device], périphérique 0: USB Audio [USB Audio]
  Sous-périphériques: 1/1
  Sous-périphérique #0: subdevice #0

So, i have 5 devices, which are identified by this names: ALSA, Device, Device_1, Device_2, Device_3

My question is: How can i be sure that Device_2 will match with the same USB sound card each time i boot my computer ? Do you think the matching could be random when the computer starts ? There is no unique identifier inside a sound card, so i am wondering how the kernel makes the matching.

Thanks


Solution

  • Device path basically defines which USB port the card is plugged into. Run ls -la /sys/class/sound/ to list cards and their device pathes, then write new name to card's id attribute.(Use "device path" to rename each card.)

    For example:

    $ ls -la /sys/class/sound/
    total 0
    drwxr-xr-x  2 root root 0 May 27 17:48 .
    drwxr-xr-x 34 root root 0 May 27 17:48 ..
    lrwxrwxrwx  1 root root 0 May 27 17:48 card1 -> ../../devices/pci0000:00/0000:00:1a.0/usb3/3-1/3-1:1.0/sound/card1
    lrwxrwxrwx  1 root root 0 May 27 17:48 card2 -> ../../devices/pci0000:00/0000:00:1a.0/usb3/3-2/3-2:1.0/sound/card2
    lrwxrwxrwx  1 root root 0 May 27 17:48 card3 -> ../../devices/pci0000:00/0000:00:1a.1/usb4/4-1/4-1:1.0/sound/card3
    lrwxrwxrwx  1 root root 0 May 27 17:48 card4 -> ../../devices/pci0000:00/0000:00:1a.1/usb4/4-2/4-2:1.0/sound/card4
    ...
    

    gives 4 device paths. Index might be different, but device path won't change until you plug the card into a different USB port.

    Use these device paths to set new name:

    echo -n NewName1 > /sys/devices/pci0000:00/0000:00:1a.0/usb3/3-1/3-1:1.0/sound/card*/id
    echo -n NewName2 > /sys/devices/pci0000:00/0000:00:1a.0/usb3/3-2/3-2:1.0/sound/card*/id
    echo -n NewName3 > /sys/devices/pci0000:00/0000:00:1a.1/usb4/4-1/4-1:1.0/sound/card*/id
    echo -n NewName4 > /sys/devices/pci0000:00/0000:00:1a.1/usb4/4-2/4-2:1.0/sound/card*/id
    

    That will also change names in cat /proc/asound/cards and aplay -l output.

    You can define rules to set those names automatically when devices are detected. For udev write to /etc/udev/rules.d/70-my-sound-cards.rules something like:

    ACTION=="add", SUBSYSTEM=="sound", DEVPATH=="/devices/pci0000:00/0000:00:1a.0/usb3/3-1/3-1:1.0/sound/card?", ATTR{id}="NewName1"
    ACTION=="add", SUBSYSTEM=="sound", DEVPATH=="/devices/pci0000:00/0000:00:1a.0/usb3/3-2/3-2:1.0/sound/card?", ATTR{id}="NewName2"
    ACTION=="add", SUBSYSTEM=="sound", DEVPATH=="/devices/pci0000:00/0000:00:1a.1/usb4/4-1/4-1:1.0/sound/card?", ATTR{id}="NewName3"
    ACTION=="add", SUBSYSTEM=="sound", DEVPATH=="/devices/pci0000:00/0000:00:1a.1/usb4/4-2/4-2:1.0/sound/card?", ATTR{id}="NewName4"
    

    (don't forget to write your card names and device pathes there)

    Then use those names to reference cards in your software, like say, "plughw:NewName4", "dmix:NewName2"...