Activate USB audio under WSL2

Published on by
Revised on

wsl2audiovideo

Background & functionality

To use audio (and video) under WSL21, it is necessary to recompile the kernel and activate additional modules. For example, audio and video support is helpful when accessing the WSL2 instance via “Xrdp”2 to use Linux as a full-fledged desktop environment on Windows. This article describes how this is possible and what steps need to be taken.

Prerequisites

  1. At least Windows 113 is installed
  2. WSL2 must be enabled under Windows 11
  3. → A WSL2 Linux distribution is already installed

Steps

  1. Start the installed subsystem

    - inside Windows Command Prompt -

    • Press the key combination Windows-Key + R-Key
    • In the small window enter cmd and confirm with the Return button or click OK
    • Start system
      wsl -d mylinux
      
    • Notes:
      • The Windows Command Prompt switches to the terminal of the Linux distribution
      • The distribution name “mylinux” has been specified within a previous article, see → 3. Prerequisites
  2. Installation of required packages and download of the kernel release

    - inside Linux Terminal -

    • Install the required build tools
      sudo apt install -y build-essential flex bison dwarves libssl-dev libelf-dev bc linux-tools-virtual libncurses-dev usbutils unzip
      
    • Download the kernel release
      cd
      
      wget https://github.com/microsoft/WSL2-Linux-Kernel/archive/refs/heads/linux-msft-wsl-5.15.y.zip
      
    • Unpack the kernel release
      unzip -o linux-msft-wsl-5.15.y.zip
      
  3. Modify the default configuration of the kernel, compile and install

    - inside Linux Terminal -

    • Copy the standard configuration of the WSL2 core to the top level of the cloned repository
      cd WSL2-Linux-Kernel-linux-msft-wsl-5.15.y
      
      cp Microsoft/config-wsl .
      
    • Add the following configuration flags to the default configuration
      sudo tee -a config-wsl > /dev/null <<EOT
      CONFIG_MEDIA_SUPPORT=m
      CONFIG_MEDIA_CAMERA_SUPPORT=y
      CONFIG_MEDIA_SDR_SUPPORT=y
      CONFIG_MEDIA_PLATFORM_SUPPORT=y
      CONFIG_MEDIA_TEST_SUPPORT=y
      CONFIG_VIDEO_DEV=m
      CONFIG_MEDIA_CONTROLLER=y
      CONFIG_VIDEO_V4L2=m
      CONFIG_MEDIA_USB_SUPPORT=y
      CONFIG_USB_VIDEO_CLASS=m
      CONFIG_USB_VIDEO_CLASS_INPUT_EVDEV=y
      CONFIG_USB_GSPCA=m
      CONFIG_VIDEOBUF2_CORE=m
      CONFIG_VIDEOBUF2_V4L2=m
      CONFIG_VIDEOBUF2_MEMOPS=m
      CONFIG_VIDEOBUF2_VMALLOC=m
      CONFIG_MEDIA_ATTACH=y
      CONFIG_HIDRAW=y
      CONFIG_SOUND=m
      CONFIG_SND_USB_AUDIO=m
      CONFIG_SND=m
      CONFIG_SND_PCM_TIMER=y
      CONFIG_SND_SUPPORT_OLD_API=y
      CONFIG_SND_PROC_FS=y
      CONFIG_SND_VERBOSE_PROCFS=y
      CONFIG_SND_DMA_SGBUF=y
      CONFIG_SND_DRIVERS=y
      CONFIG_SND_PCI=y
      CONFIG_SND_HDA_PREALLOC_SIZE=2048
      CONFIG_SND_USB=y
      CONFIG_SND_X86=y
      EOT
      
    • Automatic merging of the new and overwritten configuration
      sudo make menuconfig KCONFIG_CONFIG=config-wsl
      
    • Notes:
      • The kernel configuration UI is automatically opened
      • Leave this UI directly by choosing “Exit”, then confirm with “Yes”
    • Compile the kernel
      sudo make -j $(nproc) KCONFIG_CONFIG=config-wsl
      
    • Install the kernel modules
      sudo make modules_install -j$(nproc)  KCONFIG_CONFIG=config-wsl
      
  4. Copy the kernel image

    - inside Linux Terminal -

    • Create a directory on the host system and copy the image into it.
      mkdir /mnt/c/WSL2-Linux-Kernel
      
      sudo cp vmlinux /mnt/c/WSL2-Linux-Kernel
      
      exit
      
  5. Configure WSL2 to use the new kernel

    - inside Windows Command Prompt -

    • Add the individual core to the WSL configuration
      echo kernel=c:\\WSL2-Linux-Kernel\\vmlinux>> "%userprofile%\.wslconfig"
      
    • Shut down WSL2
      wsl --shutdown
      
    • Restart the Linux distribution
      wsl -d mylinux
      
    • Notes:
      • The Windows Command Prompt switches again into the terminal of the Linux distribution
      • The distribution name “mylinux” has been specified within a previous article, see → 3. Prerequisites
  6. Prepare the user and the usbip tool

    - inside Linux Terminal -

    • Add the user to the additional groups
      sudo usermod -a -G users,dialout,cdrom,floppy,sudo,audio,dip,video,plugdev,netdev,rtkit,ssl-cert tux
      
    • Hinweis:
    • Prepare usbip
      sudo update-alternatives --install /usr/local/bin/usbip usbip $(command -v ls /usr/lib/linux-tools/*/usbip | tail -n1) 20
      
      exit
      
  7. Close Windows Command Prompt and start with elevated rights

    - inside Windows Command Prompt -

    • Close the Windows Command Prompt
      exit
      
    • Press the key combination Windows-Key + R-Key
    • In the small window enter cmd then press the ‘Ctrl-key’ + ‘Shift-Key’ and confirm with the Return button or click OK
    • Confirm the following window with Yes
  8. Install the usbipd tool and make USB device(s) exportable

    - inside Windows Command Prompt (with elevated rights) -

    • Installation
      winget install usbipd
      
    • List USB devices
      usbipd list
      
    • The output should look something like this:
      BUSID  VID:PID    DEVICE                                                        STATE
      2-1    17ef:a387  Lenovo USB Ethernet                                           Not shared
      2-2    174c:5106  USB-Massenspeichergerät                                       Not shared
      4-5    1050:0407  USB-Eingabegerät, Microsoft Usbccid-Smartcard-Leser (WUDF)    Not shared
      4-6    046d:0a45  Logitech USB Headset, USB-Eingabegerät                        Not shared
      
    • Make USB device exportable at the spile of the USB headset
      usbipd bind -b 4-6 -f
      
      exit
      
    • Notes:
      • Instead of BUS ID 4-6 select the ID which corresponds to the device to be exported
  9. Start Windows Command Prompt with the standard rights

    - inside Windows Command Prompt (with elevated rights) -

    • Press the key combination Windows-Key + R-Key
    • In the small window enter cmd and confirm with the Return button or click OK
    • Log into the Linux distribution
      wsl -d mylinux
      
    • Hinweise:
      • The Windows Command Prompt switches again into the terminal of the Linux distribution
      • The distribution name “mylinux” has been specified within a previous article, see → 3. Prerequisites
  10. Show USB device inside the Linux distribution

    - inside Linux Terminal -

    • Set the IP address of the WSL host as an environment variable
      WSL_HOST_IP=$(ip route show default | awk '{print $3}')  
      
    • List the USB devices
      usbip list -r $WSL_HOST_IP
      
    • The output should look something like this:
      Exportable USB devices
      ======================
      - 172.28.48.1
              4-6: Logitech, Inc. : 960 Headset (046d:0a45)
              : USB\VID_046D&PID_0A45\000000000000
              : (Defined at Interface level) (00/00/00)
              :  0 - Audio / Control Device / unknown protocol (01/01/00)
              :  1 - Audio / Streaming / unknown protocol (01/02/00)
              :  2 - Audio / Streaming / unknown protocol (01/02/00)
              :  3 - Human Interface Device / No Subclass / None (03/00/00)
      
  11. Connect with USB device and check audio via the desktop environment

    • Start Linux desktop environment, see Tutorial at → 3. Prerequisites
    • Start a terminal inside the Linux desktop environment
    • Connect USB device via usbip
      sudo usbip attach -b 4-6 -r $WSL_HOST_IP
      
    • Check
      lsusb
      
    • The output should look something like this:
      Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
      Bus 001 Device 002: ID 046d:0a45 Logitech, Inc. 960 Headset
      Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
      
    • Notes:
      • As soon as desired device appears in the list (in this case Logitech, Inc. 960 headset) the connection is established correctly
      • The device should now be found in the audio mixer of the Linux desktop environment

See also

  1. → Linux with a desktop environment running under Windows 11

Disclaimer: The information in this article is to be understood as a non-binding reference.
The up-to-dateness, correctness and completeness of the content cannot be guaranteed.