# Sound on Raspberry Pi: vchiq and bcm2835_audio

<span class="badge badge-part">first sound</span> Pi 4, FreeBSD 15.1. The repository is not public yet.

Before these modules `/dev/sndstat` on a Pi 4 running arm64 FreeBSD was empty. Sound on the Pi goes through the VideoCore GPU firmware, and the kernel talks to the firmware over VCHIQ. The drivers in the FreeBSD tree assumed 32-bit pointers, so on arm64 they did not build.

## What works

- `pcm0` attaches on the Pi 4, and a test melody plays on the 3.5 mm jack with 0 underruns.
- The firmware answers VCHIQ protocol version 8.

Not done yet: HDMI audio (not tried), detach and `kldunload`, the Zero 2 W (not tried). The Pi 5 is out of scope: it has no analogue jack and its audio path is different.

## Build and load

<ol class="steps">
<li>

Apply the two patches to the kernel sources: `patches/0001-vchiq-arm64.patch` and `patches/0002-bcm2835_audio-arm64.patch`. The second one also fixes the audio message size, which is 20 bytes on the wire.

</li>
<li>

Build both modules against the source tree:

```sh
cd driver && make SYSDIR=/usr/src/sys
```

</li>
<li>

Load them, VCHIQ first:

```sh
kldload ./vchiq/vchiq.ko ./bcm2835_audio/bcm2835_audio.ko
```

</li>
<li>

Pick the output:

```sh
sysctl dev.pcm.0.dest=1        # 0 auto, 1 headphone jack, 2 HDMI
```

</li>
</ol>

## The bug that kept it silent

The first builds loaded and played nothing. The fix was memory barriers in `remote_event_signal`, the function that tells the VideoCore firmware new data is waiting in shared memory. With them in place the tone and the melody came out of the jack.
