# Wi-Fi on Raspberry Pi: the cywfm driver

<span class="badge badge-ok">works</span> Version `0.22.11-t88`, tested on FreeBSD 15.1-RELEASE-p7. Source: [spagu/freebsd-rpi-wifi](https://github.com/spagu/freebsd-rpi-wifi).

## Boards and chips

| Board | Chip | Bands | Status |
|---|---|---|---|
| Pi 4 Model B Rev 1.4 | CYW43455 | 2.4 + 5 GHz | tested |
| Pi 5 Model B (UEFI firmware v0.3) | CYW43455 | 2.4 + 5 GHz | tested |
| Pi Zero 2 W Rev 1.0 | 43430 family | 2.4 GHz | tested |
| Other Pi 4 revisions, Pi 400, CM4, Pi 3 B+/A+, CM5, Pi 500 | CYW43455 / 43456 | | expected to work, report wanted |
| Pi 3 Model B | | | needs a board profile first |

The module creates the net80211 device `cywfm0`; a `wlan0` goes on top of it as with any other FreeBSD wireless card. It speaks SDIO through its own layer on the classic MMC stack, so kernels built with `options MMCCAM` are not supported.

## Option A: prepare a fresh SD card

The `tools/prepare-card.sh` script copies the module, firmware and network settings onto a card freshly written with the FreeBSD image.

<ol class="steps">
<li>

Write the FreeBSD 15.1 RPI image to the card (see [Before starting](/rpi/)).

</li>
<li>

From a FreeBSD computer, mount both partitions and run the script. The board then joins the network on first boot.

```sh
mkdir -p /mnt/fat /mnt/root
mount -t msdosfs /dev/da0s1 /mnt/fat
mount /dev/da0s2a /mnt/root
tools/prepare-card.sh -b zero2w -m /mnt/fat -r /mnt/root -s "network name" -c GB
umount /mnt/fat /mnt/root
```

Use `-b pi4` or `-b pi5` for the other boards and the two-letter country code after `-c`.

</li>
<li>

From Windows the PowerShell version can only reach the FAT partition:

```powershell
powershell -ExecutionPolicy Bypass -File tools\prepare-card.ps1 -Board pi4 -Drive E: -Ssid "network name" -Country GB
```

Boot the board, log in once and finish the install with `sh /boot/efi/cywfm/install.sh`.

</li>
</ol>

## Option B: install on a running board

This needs `/usr/src/sys` on the board.

<ol class="steps">
<li>

Pi Zero 2 W only: remove `dtoverlay=mmc` from `config.txt` and reboot. That overlay switches the Wi-Fi bus off.

</li>
<li>

Install the firmware into `/boot/firmware` and build the module:

```sh
make install-firmware        # Zero 2 W: tools/fetch-firmware.sh -i -c 43436s
                             # Pi 5:     tools/fetch-firmware.sh -i -b raspberrypi,5-model-b
make module
make load
```

The firmware files (`brcmfmac43455-sdio.*`, `brcmfmac43456-sdio.*`, `brcmfmac43436s-sdio.*`) are vendor binaries under the Cypress/Synaptics licence. The repository fetches them; it does not ship them.

</li>
<li>

Bring the interface up by hand:

```sh
ifconfig wlan0 create wlandev cywfm0
wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant.conf
dhclient wlan0
```

</li>
<li>

Make it permanent. `make install` copies the module to `/boot/modules` and runs `kldxref`. Then add to `/etc/rc.conf`:

```sh
kld_list="cywfm"
wlans_cywfm0="wlan0"
create_args_wlan0="country XX"
ifconfig_wlan0="WPA DHCP"
```

Load it from `kld_list`, not from `loader.conf`. A module the loader brings in before the SDIO controller is ready logs "loaded at boot, bring-up skipped" and does nothing. Keep `/etc/wpa_supplicant.conf` at mode 0600.

</li>
</ol>

To remove it again: `sysrc -x wlans_cywfm0 ifconfig_wlan0 create_args_wlan0` and `sysrc kld_list-=cywfm`.

## Is it working?

```sh
sysctl hw.cywfm.stage      # "net80211" means the radio is up
dmesg | grep cywfm:
ifconfig wlan0
wpa_cli status
```

## What it can do

- Station mode with WPA2-PSK and open networks, 802.11w (management frame protection).
- Access point mode with `hostapd`.
- Monitor mode with radiotap headers (`hw.cywfm.monitor`).
- Firmware offloads: ARP, IPv6 neighbour discovery, packet filters, keepalive.
- Automatic recovery after a firmware crash (`hw.cywfm.recover`, 0 to 10 restarts).
- `kldunload` and `kldload` without a reboot.

## What it cannot do (yet)

- WPA3-SAE: net80211 has no external authentication for FullMAC drivers. A write-up for the FreeBSD wireless list is drafted (see [Upstream reports](/reports/)).
- Wi-Fi Direct (P2P), scheduled scans, wake on wireless.
- 5 GHz on the Zero 2 W: its radio has no 5 GHz.
- On the Pi 4 and Pi 5 the first join after loading often fails and the retry succeeds. Each load restarts the firmware's 802.11 sequence counter, so the first authentication frame is almost always number 38 with the Retry bit set. An access point that saw number 38 last time stays quiet, and the result is a neat ok/fail/ok/fail pattern. Two attempted fixes did not change it.
- Untested: AP with a client on the Pi 4, 40/80 MHz and DFS channels for the AP, several virtual interfaces at once.

## Tunables

Set them in `/boot/loader.conf` as `hw.cywfm.<name>="value"`. The full list (more than 60) is in `docs/sysctl.md` and the `cywfm(4)` man page. The ones most often changed:

| Name | What it does |
|---|---|
| `country` | Regulatory country code |
| `pm` | Power save on or off |
| `irq_fix` | Pi 4 shared-interrupt workaround (on by default, see below) |
| `bus_hs` | High-speed SDIO bus |
| `mfp` | 802.11w management frame protection |
| `recover` | How many times to restart after a fatal firmware fault |
| `debug` | Extra logging, for example `hw.cywfm.debug="1"` |

## A bug in the Pi 4 kernel

On the Pi 4 the Wi-Fi SDIO controller and the SD card controller share one interrupt line. If the Wi-Fi controller interrupts while the SD card is in the middle of a DMA write, the SD driver loses its "buffer ready" event. The board keeps answering ping for 9 to 10 seconds and then reboots. That is a FreeBSD kernel bug, not a driver bug, so `cywfm` patches the `sdhci_bcm` method table at runtime (`irq_fix`) until the kernel is fixed. The kernel patch and the report are on the [Kernel bugs](/kernel/bcm2711-shared-sdhci-interrupt/) page.

Two smaller surprises from the same board: the Pi 4's radio reports the SDIO card ID of a 43430 (`0xa9a6`), so the driver reads the chip ID register instead, which says 0x4345 rev 6. And `make -j4` on a Pi 4 once killed the compiler with "illegal instruction". The power supply was sagging, so builds now run with one job.

## Report a board

Reports from boards in the "expected to work" row are welcome. Two scripts collect what a report needs:

```sh
sh tools/board-report.sh -d dump.txt > report.md
sh tools/self-test.sh -m driver/cywfm.ko > test.md
```
