mobile-nixos/mobile-nixos

Bluetooth on SDM845

Opened this issue · 7 comments

I'm not sure what ever caused this to work out of the box in the first place when first investigating mobile-nixos, but the issue with bluetooth was resolved by following https://wiki.postmarketos.org/wiki/Qualcomm_Snapdragon_845/850_(SDM845/SDM850)#Bluetooth, specifically running this script that they suggest:

#!/bin/sh
SERIAL=$(grep -o "serialno.*" /proc/cmdline | cut -d" " -f1)
BT_MAC=$(echo "$SERIAL-BT" | sha256sum | awk -v prefix=0200 '{printf("%s%010s\n", prefix, $1)}')
BT_MAC=$(echo "$BT_MAC" | cut -c1-12 | sed 's/\(..\)/\1:/g' | sed '$s/:$//')

btmgmt -i hci0 power off
btmgmt -i hci0 public-addr "$BT_MAC"

bluetooth needs a temporary valid random mac on boot, and in pmOS they use bootmac to do this during boot I discovered after chatting with them in the sdm845-mainline matrix channel.

Originally posted by @MatthewCroughan in #747 (comment)

bluetooth needs a temporary valid random mac

The linked documentation doesn't seem to imply it is temporary. Additionally, this is not random, this is clearly using the serial number (as would be passed down by the command line) as a seed.

Was there anything from the discussion you had with the PostmarketOS people you can quote about randomness or temporary?

I've made a hacky module temporarily for my own use that works, looks like this

{ config, lib, pkgs, ... }:

let
  cfg = config.mobile.quirks.qualcomm;
  inherit (lib) mkIf mkOption types;
in
{
  options.mobile = {
    quirks.qualcomm.qca-bluetooth.enable = mkOption {
      type = types.bool;
      default = false;
      description = ''
        Enable this on a device which uses the hci_uart_qca driver and needs a
        random mac address set in order to allow bluetooth functionality to work
        correctly
      '';
    };
  };

  config = mkIf (cfg.qca-bluetooth.enable) {
    systemd.services.qca-bluetooth = let
      script = pkgs.writeShellScript "qca-bluetooth.sh" ''
        set -x
        trap 'sleep 1' DEBUG # Sleep 1 second before every command execution
        export PATH="${lib.makeBinPath (with pkgs; [ bluez coreutils-full gawk unixtools.script ])}:$PATH"

        SERIAL=$(grep -o "serialno.*" /proc/cmdline | cut -d" " -f1)
        BT_MAC=$(echo "$SERIAL-BT" | sha256sum | awk -v prefix=0200 '{printf("%s%010s\n", prefix, $1)}')
        BT_MAC=$(echo "$BT_MAC" | cut -c1-12 | sed 's/\(..\)/\1:/g' | sed '$s/:$//')

        script -qc "btmgmt --timeout 3 -i hci0 power off"
        script -qc "btmgmt --timeout 3 -i hci0 public-addr \"$BT_MAC\""
      '';
    in {
      description = "Setup the bluetooth interface";
      wantedBy = [ "multi-user.target" "bluetooth.service" ];
      script = toString script;
      serviceConfig = {
        User = "root";
        Type = "oneshot";
        RemainAfterExit = true;
      };
    };
  };
}

Regarding what they said in the SDM845-Mainline room:

Dylan Van Assche: Bluetooth needs a valid MAC, we have bootmac for this to take
care of during boot.

system64: Yup

But that's a pmos specific thing

I wish the changes would be upstreamed and fixed in the driver itself

Dylan Van Assche: Yeah it is annoying, I once digged into the whole Bluetooth
kernel stack to understand why BNEP was failing, turns out the MAC address was
set but it was just invalid.

system64: Sooo this is a kernel bug

A proper one must be set and that should fix it?

Hoping someone can push something that creates a random addr literally any valid
random mac addr

Proper device specific one could be set in pmos via bootmac or if other distros
care then they can implement something like that But BT should work by default..
Not by having to run custom commands in userspace..

Dylan Van Assche: Yeah, but needs to be unique as well, or you get issues, hence
bootmac using machine-id or serial-id

I'm not sure what ever caused this to work out of the box in the first place when first investigating mobile-nixos, but the issue with bluetooth was resolved by following https://wiki.postmarketos.org/wiki/Qualcomm_Snapdragon_845/850_(SDM845/SDM850)#Bluetooth, specifically running this script that they suggest:

#!/bin/sh
SERIAL=$(grep -o "serialno.*" /proc/cmdline | cut -d" " -f1)
BT_MAC=$(echo "$SERIAL-BT" | sha256sum | awk -v prefix=0200 '{printf("%s%010s\n", prefix, $1)}')
BT_MAC=$(echo "$BT_MAC" | cut -c1-12 | sed 's/\(..\)/\1:/g' | sed '$s/:$//')

btmgmt -i hci0 power off
btmgmt -i hci0 public-addr "$BT_MAC"

bluetooth needs a temporary valid random mac on boot, and in pmOS they use bootmac to do this during boot I discovered after chatting with them in the sdm845-mainline matrix channel.

Originally posted by @MatthewCroughan in #747 (comment)

[alice@nixos:~]$ fastfetch
          ▗▄▄▄       ▗▄▄▄▄    ▄▄▄▖             alice@nixos
          ▜███▙       ▜███▙  ▟███▛             -----------
           ▜███▙       ▜███▙▟███▛              OS: NixOS 24.11pre-git (Vicuna) aarch64
            ▜███▙       ▜██████▛               Host: OnePlus 6T
     ▟█████████████████▙ ▜████▛     ▟▙         Kernel: Linux 6.11.0
    ▟███████████████████▙ ▜███▙    ▟██▙        Uptime: 8 mins
           ▄▄▄▄▖           ▜███▙  ▟███▛        Packages: 853 (nix-system)
          ▟███▛             ▜██▛ ▟███▛         Shell: bash 5.2.32
         ▟███▛               ▜▛ ▟███▛          Display (DSI-1): 1080x2340 @ 60 Hz (as 540x1170) in 6″
▟███████████▛                  ▟██████████▙    WM: phoc (Wayland)
▜██████████▛                  ▟███████████▛    Terminal: /dev/pts/1
      ▟███▛ ▟▙               ▟███▛             CPU: Kryo-3XX-Silver*4 + Kryo-3XX-Gold*4 (8) @ 2.65 GHz
     ▟███▛ ▟██▙             ▟███▛              GPU: Qualcomm Turnip Adreno (TM) 630 [Integrated]
    ▟███▛  ▜███▙           ▝▀▀▀▀               Memory: 1018.26 MiB / 7.37 GiB (14%)
    ▜██▛    ▜███▙ ▜██████████████████▛         Swap: Disabled
     ▜▛     ▟████▙ ▜████████████████▛          Disk (/): 6.61 GiB / 108.14 GiB (6%) - ext4
           ▟██████▙       ▜███▙                Local IP (wlan0): 192.168.2.126/24
          ▟███▛▜███▙       ▜███▙               Battery: 98% [Discharging]
         ▟███▛  ▜███▙       ▜███▙              Locale: en_US.UTF-8
         ▝▀▀▀    ▀▀▀▀▘       ▀▀▀▘
                                                                       
                                                                       

[alice@nixos:~]$ echo "020023aaca93aed67e44337dff0e15836e07b149645e5890f90161643107216d04ee" | cut -c1-12 | sed 's/\(..\)/\1:/g' | sed '$s/:$//'
02:00:23:aa:ca:93

[alice@nixos:~]$ sudo btmgmt -i hci0 power off
hci0 Set Powered complete, settings: bondable ssp br/edr le secure-conn 

[alice@nixos:~]$ sudo btmgmt -i hci0 public-addr 02:00:23:aa:ca:93
hci0 Set Public Address complete, options: 

[alice@nixos:~]$ sudo systemctl status bluetooth
● bluetooth.service - Bluetooth service
     Loaded: loaded (/etc/systemd/system/bluetooth.service; enabled; preset: enabled)
    Drop-In: /nix/store/2km10nq1nklwrs2nbj1i4gqrv72zik38-system-units/bluetooth.service.d
             └─overrides.conf
     Active: active (running) since Sat 2024-07-06 02:03:05 CST; 3 months 10 days ago
 Invocation: 8ad14bda96744e6f8ebaeda5452f92ab
       Docs: man:bluetoothd(8)
   Main PID: 715 (bluetoothd)
     Status: "Running"
         IP: 0B in, 0B out
         IO: 2.7M read, 36K written
      Tasks: 1 (limit: 8419)
     Memory: 3.7M (peak: 4.5M)
        CPU: 100ms
     CGroup: /system.slice/bluetooth.service
             └─715 /nix/store/ws1na8bi7zfxyab31s3110y5x4z6nzgs-bluez-5.76/libexec/bluetooth/bluetoothd -f /etc/bluetooth/main.conf

Oct 16 01:59:51 nixos bluetoothd[715]: Endpoint registered: sender=:1.24 path=/MediaEndpoint/A2DPSink/sbc
Oct 16 01:59:51 nixos bluetoothd[715]: Endpoint registered: sender=:1.24 path=/MediaEndpoint/A2DPSource/sbc
Oct 16 01:59:51 nixos bluetoothd[715]: Endpoint registered: sender=:1.24 path=/MediaEndpoint/A2DPSink/sbc_xq_453
Oct 16 01:59:51 nixos bluetoothd[715]: Endpoint registered: sender=:1.24 path=/MediaEndpoint/A2DPSource/sbc_xq_453
Oct 16 01:59:51 nixos bluetoothd[715]: Endpoint registered: sender=:1.24 path=/MediaEndpoint/A2DPSink/sbc_xq_512
Oct 16 01:59:51 nixos bluetoothd[715]: Endpoint registered: sender=:1.24 path=/MediaEndpoint/A2DPSource/sbc_xq_512
Oct 16 01:59:51 nixos bluetoothd[715]: Endpoint registered: sender=:1.24 path=/MediaEndpoint/A2DPSink/sbc_xq_552
Oct 16 01:59:51 nixos bluetoothd[715]: Endpoint registered: sender=:1.24 path=/MediaEndpoint/A2DPSource/sbc_xq_552
Oct 16 01:59:51 nixos bluetoothd[715]: Endpoint registered: sender=:1.24 path=/MediaEndpoint/A2DPSink/faststream
Oct 16 01:59:51 nixos bluetoothd[715]: Endpoint registered: sender=:1.24 path=/MediaEndpoint/A2DPSource/faststream

[alice@nixos:~]$ 

@MatthewCroughan Thanks for your great deployment. and the bluetooth's speaker is working normally now. only default sound card tfa98xx still need to be checked further.

[alice@nixos:~]$ dmesg |grep tfa
[    0.506775] tfa98xx_i2c_init(): TFA98XX driver version v6.7.14
[    0.568414] tfa98xx_i2c_probe(): addr=0x34
[    0.568479] tfa98xx 4-0034: No IRQ GPIO provided.
[    0.568488] tfa98xx 4-0034: reset-polarity:1
[    0.570732] tfa98xx_i2c_probe(): TFA9894 detected
[    0.570763] tfa98xx 4-0034: Skipping IRQ registration
[    0.570818] tfa98xx_i2c_probe(): tfa98xx_i2c_probe Probe completed successfully!
[    6.684896] tfa98xx 4-0034: tfa98xx codec registered ()
[    6.686701] tfa98xx 4-0034: Direct firmware load for tfa98xx.cnt failed with error -2
[    6.686706] tfa98xx_container_loaded(): Failed to read tfa98xx.cnt
● pulseaudio.service - Sound Service
     Loaded: loaded (/etc/systemd/user/pulseaudio.service; linked-runtime; preset: enabled)
    Drop-In: /nix/store/bq03f50pjz0hpfjdb6vn555np3b90qqz-user-units/pulseaudio.service.d
             └─overrides.conf
     Active: active (running) since Wed 2024-10-16 02:39:40 CST; 14min ago
 Invocation: f12aa153a15c4092abb12111888e5f9e
TriggeredBy: ● pulseaudio.socket
   Main PID: 1029 (pulseaudio)
      Tasks: 8 (limit: 8419)
     Memory: 12.5M (peak: 13.2M)
        CPU: 623ms
     CGroup: /user.slice/user-1000.slice/user@1000.service/session.slice/pulseaudio.service
             ├─1029 /nix/store/z3dsrg7vswf4kb4a3a9ciqmh558718jl-pulseaudio-17.0/bin/pulseaudio --daemonize=no --log-target=journal
             └─1053 /nix/store/z3dsrg7vswf4kb4a3a9ciqmh558718jl-pulseaudio-17.0/libexec/pulse/gsettings-helper

Oct 16 02:45:54 nixos pulseaudio[1029]: Failed to load module "module-alsa-card" (argument: "device_id="1" name="platform-sound" card_name="alsa_card.platform-sound" namereg_fail=false tsched=yes fixed_latency_range=no ignore_dB=no deferred_volume=yes use_ucm=yes avoid_resampling=no card_properties="module-udev-detect.discovered=1""): initialization failed.
Oct 16 02:45:54 nixos pulseaudio[1029]: Failed to find a working profile.
Oct 16 02:45:54 nixos pulseaudio[1029]: Failed to load module "module-alsa-card" (argument: "device_id="1" name="platform-sound" card_name="alsa_card.platform-sound" namereg_fail=false tsched=yes fixed_latency_range=no ignore_dB=no deferred_volume=yes use_ucm=yes avoid_resampling=no card_properties="module-udev-detect.discovered=1""): initialization failed.
Oct 16 02:45:54 nixos pulseaudio[1029]: Failed to find a working profile.
Oct 16 02:45:54 nixos pulseaudio[1029]: Failed to load module "module-alsa-card" (argument: "device_id="1" name="platform-sound" card_name="alsa_card.platform-sound" namereg_fail=false tsched=yes fixed_latency_range=no ignore_dB=no deferred_volume=yes use_ucm=yes avoid_resampling=no card_properties="module-udev-detect.discovered=1""): initialization failed.
Oct 16 02:45:54 nixos pulseaudio[1029]: Failed to find a working profile.
Oct 16 02:45:54 nixos pulseaudio[1029]: Failed to load module "module-alsa-card" (argument: "device_id="1" name="platform-sound" card_name="alsa_card.platform-sound" namereg_fail=false tsched=yes fixed_latency_range=no ignore_dB=no deferred_volume=yes use_ucm=yes avoid_resampling=no card_properties="module-udev-detect.discovered=1""): initialization failed.
Oct 16 02:45:54 nixos pulseaudio[1029]: Failed to find a working profile.
Oct 16 02:45:54 nixos pulseaudio[1029]: Failed to load module "module-alsa-card" (argument: "device_id="1" name="platform-sound" card_name="alsa_card.platform-sound" namereg_fail=false tsched=yes fixed_latency_range=no ignore_dB=no deferred_volume=yes use_ucm=yes avoid_resampling=no card_properties="module-udev-detect.discovered=1""): initialization failed.
Oct 16 02:45:54 nixos pulseaudio[1029]: Tried to configure /devices/platform/sound/sound/card1 (alsa_card.platform-sound) more often than 5 times in 10s

[alice@nixos:~]$ 

https://github.com/gmicroul/mobile-nixos/raw/refs/heads/development/examples/phosh/nixbuild.log

Any update?

how do you guys even have wifi working on latest commit (oneplus 6, non T)?

fyi:nixos25.05 used kernel 6.4.0 fixed bluetooth issue.
image