Skip to content

RF Sentinel (RFSentinel): free, open-source police detector app for Android. Spots police body cams, Flock ALPR cameras, AirTag trackers, Remote ID drones, smart glasses and fake cell towers from Bluetooth LE and WiFi. No root.

Add an experimental receive-only RTL8812AU USB WiFi monitor-mode driver

CIS-C0118f03e60369
diff --git a/README.md b/README.md
index d711881..f920091 100644
--- a/README.md
+++ b/README.md
@@ -158,7 +158,7 @@ headline as you get closer (strong within 50 m, fading out by 500 m). Full refer
**Hardware**
- ESP32 boards over USB OTG (OUI-Spy, GhostESP, Marauder incl. dual-band ESP32-C5 boards, or through a Flipper Zero) or Bluetooth (OUI-SPY App-Controlled) add their detections and extend range
-- **USB WiFi adapters in monitor mode** over OTG, no root: RTL8811AU / RTL8821AU (e.g. ALFA AWUS036ACS, 2.4 GHz; 5 GHz experimental, Settings → External hardware), **RTL8812BU / RTL8822BU** (2.4 + 5 GHz, e.g. TP-Link Archer T3U, Wise Tiger AC1200), **RTL8814AU** (ALFA AWUS1900, 2.4 + 5 GHz), **MT7612U** (ALFA AWUS036ACM, 2.4 + 5 GHz), **MT7921AU** (ALFA AWUS036AXML, WiFi 6E: 2.4 + 5 + **6 GHz**, experimental), **RTL8187** (ALFA AWUS036H), **RT3070** (ALFA AWUS036NH / NEH) and, experimental, **AR9271** (ALFA AWUS036NHA). No Android scan limit, longer range, and **client devices** (laptops, phones, cameras connected to a network) the phone's WiFi scan can't see. Single-antenna dongles are detected and handled automatically
+- **USB WiFi adapters in monitor mode** over OTG, no root: RTL8811AU / RTL8821AU (e.g. ALFA AWUS036ACS, 2.4 GHz; 5 GHz experimental, Settings → External hardware), **RTL8812AU** (e.g. ALFA AWUS036ACH, 2.4 + 5 GHz, experimental), **RTL8812BU / RTL8822BU** (2.4 + 5 GHz, e.g. TP-Link Archer T3U, Wise Tiger AC1200), **RTL8814AU** (ALFA AWUS1900, 2.4 + 5 GHz), **MT7612U** (ALFA AWUS036ACM, 2.4 + 5 GHz), **MT7921AU** (ALFA AWUS036AXML, WiFi 6E: 2.4 + 5 + **6 GHz**, experimental), **RTL8187** (ALFA AWUS036H), **RT3070** (ALFA AWUS036NH / NEH) and, experimental, **AR9271** (ALFA AWUS036NHA). No Android scan limit, longer range, and **client devices** (laptops, phones, cameras connected to a network) the phone's WiFi scan can't see. Single-antenna dongles are detected and handled automatically
- **Requested networks:** the WiFi names nearby devices ask for (probe requests), from a USB adapter or a Marauder board, in an optional list; **watch** a name to get an alert whenever any device asks for it
- **Record frames to .pcap** for Wireshark (Settings → External hardware): every frame a USB WiFi adapter or V2X board hears, with its channel and signal
- The ALFA AWUS036ACS's light blinks with received traffic (Settings → External hardware switches it off, for the adapters whose light the app can control)
@@ -496,6 +496,7 @@ go through RF Sentinel's own rules like everything else, with a purple
| Chip | Example adapters | Status |
|---|---|---|
| Realtek RTL8811AU / RTL8821AU (`0bda:0811`) | ALFA AWUS036ACS | Supported (2.4 GHz; 5 GHz experimental, opt-in) |
+| Realtek RTL8812AU (`0bda:8812`, `0bda:881a` and ~30 other IDs) | ALFA AWUS036ACH, TP-Link Archer T4U / T4UH, Netgear A6200v2, Edimax | Experimental (2.4 + 5 GHz): written from the Realtek driver, not yet tested on a real adapter |
| Realtek RTL8812BU / RTL8822BU (`0bda:b812`, `0bda:b82c` and ~35 other IDs) | TP-Link Archer T3U, ASUS USB-AC53/AC55/AC58, Edimax EW-7822U*, Wise Tiger AC1200 | Supported (2.4 + 5 GHz), tested on a Wise Tiger 8812BU |
| Realtek RTL8814AU (`0bda:8813` and 13 other IDs) | ALFA AWUS1900, TP-Link Archer T9UH, ASUS USB-AC68, Netgear A7000 | Supported (2.4 + 5 GHz), not yet tested on hardware |
| MediaTek MT7612U / MT7632U (`0e8d:7612` and 16 other IDs) | ALFA AWUS036ACM, ASUS USB-AC55, Netgear A6210 | Supported (2.4 + 5 GHz), not yet tested on hardware |
@@ -503,7 +504,7 @@ go through RF Sentinel's own rules like everything else, with a purple
| Realtek RTL8187L / RTL8187B (`0bda:8187` and 20 other IDs) | ALFA AWUS036H, Netgear WG111v2/v3 | Supported (2.4 GHz), tested on an ALFA AWUS036H |
| Ralink RT3070 (`148f:3070` and 4 other IDs) | ALFA AWUS036NH / AWUS036NEH | Supported (2.4 GHz), tested on an ALFA AWUS036NEH |
| Atheros AR9271 (`0cf3:9271` and 17 other IDs) | ALFA AWUS036NHA, TP-Link TL-WN722N v1 | Experimental (2.4 GHz) |
-| RTL8812AU, RTL8811CU / 8821CU, RT5370, MT7610U, AR7010 | AWUS036ACH... | Detected and named, not supported yet |
+| RTL8811CU / 8821CU, RT5370, MT7610U, AR7010 | | Detected and named, not supported yet |
The adapter never transmits (on the MT7612U and RT3070 even the automatic ACK
replies are switched off).
diff --git a/app/src/main/java/com/rfsentinel/app/usb/NOTICE.md b/app/src/main/java/com/rfsentinel/app/usb/NOTICE.md
index 25606c4..df80f34 100644
--- a/app/src/main/java/com/rfsentinel/app/usb/NOTICE.md
+++ b/app/src/main/java/com/rfsentinel/app/usb/NOTICE.md
@@ -3,6 +3,7 @@
These drivers run USB WiFi adapters in receive-only monitor mode from userspace.
- `Rtl8821auMonitor.kt` and `Rtl8821auTables.kt` drive Realtek RTL8811AU / RTL8821AU adapters (e.g. ALFA AWUS036ACS).
+- `Rtl8812auMonitor.kt` and `Rtl8812auTables.kt` drive Realtek RTL8812AU adapters (e.g. ALFA AWUS036ACH), 2.4 + 5 GHz, receive only - experimental. The tables are generated from the vendor driver (see below).
- `Rtl8822buMonitor.kt` and `Rtl8822bTables.kt` drive Realtek RTL8812BU / RTL8822BU adapters, with the firmware and register tables in `assets/usbwifi/rtl8822b_*.bin`.
- `Rtl8187Monitor.kt` drives Realtek RTL8187L / RTL8187B adapters (e.g. ALFA AWUS036H). They need no firmware.
- `Rt3070Monitor.kt` drives Ralink RT3070 adapters (e.g. ALFA AWUS036NH / AWUS036NEH), with Ralink's firmware in `assets/usbwifi/rt2870.bin`.
@@ -18,6 +19,7 @@ These drivers run USB WiFi adapters in receive-only monitor mode from userspace.
|---|---|---|
| RTL8811AU / 8821AU register tables (power-on sequence, MAC / BB / AGC / RF tables, channel tuning) | Realtek 88xxau Linux driver, as maintained in [aircrack-ng/rtl8812au](https://github.com/aircrack-ng/rtl8812au) | GPL-2.0 |
| RTL8811AU / 8821AU userspace USB driver and 802.11 frame parsing | [Wardrive Go](https://github.com/RocketGod-git/wardrive-go) by RocketGod | GPL-3.0 |
+| RTL8812AU power-on flow (Hal8812PwrSeq), MAC / baseband / AGC / radio A and B tables (halhwimg8812a_*), board-type (RFE) pin set-up and efuse reading, band / channel / 20 MHz bandwidth switching | Realtek rtl8812au Linux driver, as maintained in [aircrack-ng/rtl8812au](https://github.com/aircrack-ng/rtl8812au) (the vendor's condition blocks resolved for a USB board without external PA / LNA; transmit, power and firmware parts left out) | GPL-2.0 |
| RTL8812BU / 8822BU bring-up (power sequence, HalMAC firmware download, MAC init, channel tuning, gain control, RX descriptor parsing) | [devourer](https://github.com/OpenIPC/devourer) by OpenIPC, `src/jaguar2/` | GPL-2.0 |
| RTL8822B firmware image and phydm BB / AGC / RF tables | Realtek rtl88x2bu Linux driver, as extracted by devourer | Realtek firmware / GPL-2.0 |
| RTL8187L / 8187B bring-up, RTL8225 / RTL8225Z2 radio tuning and RX descriptor handling | Linux kernel `rtl8187` driver (Michael Wu, Andrea Merello, Herton Ronaldo Krzesinski, Hin-Tak Leung, Larry Finger; register values from Realtek's r8187 driver), with Kismet's [Android PCAP Capture](https://www.kismetwireless.net/android-pcap/) (Mike Kershaw / Dragorn; signal-strength work by Jinghao Shi, via Gabriel Nyman's AndroidPCAP fork) as the USB reference | GPL-2.0 |
diff --git a/app/src/main/java/com/rfsentinel/app/usb/Rtl8812auMonitor.kt b/app/src/main/java/com/rfsentinel/app/usb/Rtl8812auMonitor.kt
new file mode 100644
index 0000000..b6216c6
--- /dev/null
+++ b/app/src/main/java/com/rfsentinel/app/usb/Rtl8812auMonitor.kt
@@ -0,0 +1,433 @@
+package com.rfsentinel.app.usb
+
+/*
+ * Receive-only monitor mode for Realtek RTL8812AU USB WiFi adapters (2 x 2 dual band 802.11ac, e.g. ALFA
+ * AWUS036ACH), driven from userspace over Android's USB host API - no root, no kernel driver. Hops the 2.4 and
+ * 5 GHz channels and reports the access points (beacons, probe responses) and client devices (probe requests,
+ * data frames) it hears. It never transmits.
+ *
+ * Follows the Realtek rtl8812au Linux driver as maintained in aircrack-ng/rtl8812au (GPL-2.0): the card enable
+ * flow, MAC / baseband / AGC / radio A and B tables (Rtl8812auTables), the RFE pin set-up read from the adapter's
+ * efuse, and the band, channel and 20 MHz bandwidth switching. The USB plumbing and 802.11 frame handling are those
+ * of the RTL8811AU / 8821AU driver (Wardrive Go by RocketGod, GPL-3.0). Transmit, firmware download, power tracking
+ * and calibration for sending are left out: nothing is ever sent. Used under the GNU GPL v3.
+ */
+
+import android.content.Context
+import android.hardware.usb.UsbConstants
+import android.hardware.usb.UsbDevice
+import android.hardware.usb.UsbDeviceConnection
+import android.hardware.usb.UsbEndpoint
+import android.hardware.usb.UsbManager
+import android.hardware.usb.UsbRequest
+import java.nio.ByteBuffer
+import java.util.concurrent.TimeoutException
+
+object Rtl8812auMonitor {
+ @Volatile var running = false; private set
+ @Volatile private var stop = false
+
+ private val frames = MonitorFrames()
+ private val HOP_24 = intArrayOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)
+ /** UNII-1 and UNII-3, like the RTL8814AU driver (where most 5 GHz networks are). */
+ private val HOP_5 = intArrayOf(36, 40, 44, 48, 149, 153, 157, 161, 165)
+ private val HOP = HOP_24 + HOP_5
+ /** The board type the efuse says (which antenna switch and LNA lines the baseband drives); 0 when it can't be read. */
+ private var rfe = 0
+ /** The chip's revision: 0 = A, 1 = B, 2 = C... */
+ private var cut = 0
+ private const val DWELL_MS = 350L
+
+ private val FRESH_MS get() = (HOP.size * DWELL_MS * 9 / 5).coerceAtLeast(3000L)
+ private const val REPORT_MS = 10_000L
+ private const val NOMINAL_RSSI = -60
+
+ private const val OUT_VENDOR = 0x40; private const val IN_VENDOR = 0xC0
+ private const val VREQ = 0x05
+ private const val REG_RX_DRVINFO_SZ = 0x060F; private const val RCR_APP_PHYST_RXFF = 0x10000000
+ private const val REG_SYS_FUNC_EN = 0x02; private const val FEN_USBA = 0x04; private const val FEN_BB_GLB_RSTN = 0x02; private const val FEN_BBRSTB = 0x01
+ private const val FEN_ELDR = 0x1000
+ private const val REG_RF_CTRL = 0x1F; private const val REG_RF_B_CTRL = 0x76
+ private const val REG_SYS_CLKR = 0x08; private const val LOADER_CLK_EN = 0x20; private const val ANA8M = 0x02
+ private const val REG_SYS_CFG = 0xF0
+ private const val REG_EFUSE_CTRL = 0x30; private const val REG_EFUSE_ACCESS = 0xCF
+
+ private const val rA_LSSIWrite = 0xC90; private const val rB_LSSIWrite = 0xE90
+ private const val rHSSIRead = 0x8B0; private const val rA_PIRead = 0xD04; private const val rB_PIRead = 0xD44
+
+ /** What was last written to each radio register, per path: reading one back through the baseband is slow. */
+ private val rfShadow = arrayOf(HashMap<Int, Int>(), HashMap<Int, Int>())
+
+ fun abort() { stop = true }
+
+ fun run(ctx: Context, device: UsbDevice, onStatus: (String) -> Unit,
+ onFrame: (List<MonitorSighting>) -> Unit) {
+ if (running) return
+ running = true; stop = false; frames.clear(); rfShadow.forEach { it.clear() }
+ val mgr = ctx.getSystemService(Context.USB_SERVICE) as UsbManager
+ val conn = mgr.openDevice(device) ?: run { onStatus("RTL8812AU: couldn't reopen adapter"); running = false; return }
+ var epRx: UsbEndpoint? = null
+ try {
+ val intf = device.getInterface(0)
+ conn.claimInterface(intf, true)
+ for (i in 0 until intf.endpointCount) {
+ val ep = intf.getEndpoint(i)
+ if (ep.type == UsbConstants.USB_ENDPOINT_XFER_BULK && ep.direction == UsbConstants.USB_DIR_IN) { epRx = ep; break }
+ }
+ val rx = epRx ?: run { UsbWifi.log("RTL8812AU: no bulk-IN endpoint"); onStatus("RTL8812AU: no RX endpoint"); return }
+ UsbWifi.log("=== RTL8812AU bring-up (rtl8812au) ===")
+ UsbWifi.log("RTL8812AU: RX EP=0x%02x".format(rx.address))
+
+ onStatus("RTL8812AU · powering on…")
+ if (!powerOn(conn)) UsbWifi.log("RTL8812AU: power-on poll timed out (continuing)")
+ w16(conn, Rtl8812auTables.REG_CR, 0x0000)
+ w16(conn, Rtl8812auTables.REG_CR, Rtl8812auTables.CR_INIT16)
+ w16(conn, Rtl8812auTables.REG_RXFF_BNDY, Rtl8812auTables.RX_DMA_BOUNDARY)
+ w16(conn, Rtl8812auTables.REG_CR, r16(conn, Rtl8812auTables.REG_CR) or Rtl8812auTables.CR_MACEN8)
+ val cr = r16(conn, Rtl8812auTables.REG_CR)
+ cut = (r32(conn, REG_SYS_CFG) ushr 12) and 0xF
+ UsbWifi.log("RTL8812AU: MAC on, REG_CR=0x%04x, chip revision %d".format(cr, cut))
+ if (cr == 0xEAEA) { onStatus("RTL8812AU: MAC didn't power on (replug)"); return }
+ rfe = runCatching { readRfeType(conn) }.getOrDefault(0)
+ UsbWifi.log("RTL8812AU: board type (RFE) $rfe")
+
+ onStatus("RTL8812AU · loading MAC/BB/RF…")
+ applyTable(conn, Rtl8812auTables.MAC_REG, Kind.MAC)
+ // The baseband and both radio paths on (PHY_BBConfig8812).
+ run { val v = r8(conn, REG_SYS_FUNC_EN); w8(conn, REG_SYS_FUNC_EN, v or FEN_USBA); w8(conn, REG_SYS_FUNC_EN, v or FEN_USBA or FEN_BB_GLB_RSTN or FEN_BBRSTB) }
+ w8(conn, REG_RF_CTRL, 0x07)
+ w8(conn, REG_RF_B_CTRL, 0x07)
+ sleep(1)
+ applyTable(conn, Rtl8812auTables.PHY_REG, Kind.BB)
+ applyTable(conn, Rtl8812auTables.AGC_TAB, Kind.BB)
+ applyTable(conn, Rtl8812auTables.RADIOA, Kind.RF_A)
+ applyTable(conn, Rtl8812auTables.RADIOB, Kind.RF_B)
+
+ w8(conn, REG_RX_DRVINFO_SZ, 0x04)
+ w32(conn, Rtl8812auTables.REG_RCR, Rtl8812auTables.RCR_MONITOR or RCR_APP_PHYST_RXFF)
+ w16(conn, Rtl8812auTables.REG_RXFLTMAP0, Rtl8812auTables.RXFLTMAP_ALL)
+ w16(conn, Rtl8812auTables.REG_RXFLTMAP1, Rtl8812auTables.RXFLTMAP_ALL)
+ w16(conn, Rtl8812auTables.REG_RXFLTMAP2, Rtl8812auTables.RXFLTMAP_ALL)
+ // The first channel: the band is switched by the tune itself.
+ channelTune(conn, HOP[0])
+ UsbWifi.log("RTL8812AU: RCR=0x%08x BB(0x800)=0x%08x — monitor live ch ${HOP[0]}".format(
+ r32(conn, Rtl8812auTables.REG_RCR), r32(conn, 0x800)))
+ onStatus("RTL8812AU · live · 2.4 + 5 GHz monitor")
+
+ rxLoop(conn, rx, onFrame, onStatus)
+ } catch (e: Exception) {
+ UsbWifi.log("RTL8812AU: bring-up error — ${e.message}")
+ onStatus("RTL8812AU: error · ${e.message}")
+ } finally {
+ runCatching { conn.close() }
+ running = false
+ }
+ }
+
+ private enum class Kind { MAC, BB, RF_A, RF_B }
+
+ /** A table of (address, value) pairs; a [Rtl8812auTables.DELAY] address pauses for the value in milliseconds. */
+ private fun applyTable(conn: UsbDeviceConnection, tab: IntArray, kind: Kind) {
+ var i = 0
+ while (i + 1 < tab.size) {
+ val a = tab[i]; val v = tab[i + 1]; i += 2
+ if (a == Rtl8812auTables.DELAY) { sleep(v); continue }
+ when (kind) {
+ Kind.MAC -> w8(conn, a, v)
+ Kind.BB -> w32(conn, a, v)
+ Kind.RF_A -> rfWrite(conn, 0, a, v)
+ Kind.RF_B -> rfWrite(conn, 1, a, v)
+ }
+ }
+ }
+
+ private fun powerOn(conn: UsbDeviceConnection): Boolean {
+ var ok = true
+ for (st in Rtl8812auTables.PWR_ON) {
+ val cmd = st[0]; val off = st[1]; val msk = st[2]; val v = st[3]
+ when (cmd) {
+ Rtl8812auTables.E -> return ok
+ Rtl8812auTables.D -> sleep(v)
+ Rtl8812auTables.W -> { val cur = r8(conn, off); w8(conn, off, (cur and msk.inv()) or (v and msk)) }
+ Rtl8812auTables.P -> {
+ var hit = false; val end = System.currentTimeMillis() + 500
+ while (System.currentTimeMillis() < end) { if ((r8(conn, off) and msk) == (v and msk)) { hit = true; break }; sleep(1) }
+ if (!hit) ok = false
+ }
+ }
+ }
+ return ok
+ }
+
+ private fun channelTune(conn: UsbDeviceConnection, ch: Int) {
+ for (op in Rtl8812auTables.channelTune(ch, rfe, cut)) {
+ val kind = op[0]; val addr = op[1]; val mask = op[2]; val v = op[3]
+ when (kind) {
+ Rtl8812auTables.RF -> rfMask(conn, 0, addr, mask, v)
+ Rtl8812auTables.RFB -> rfMask(conn, 1, addr, mask, v)
+ Rtl8812auTables.MAC8 -> macMask(conn, addr, mask, v, 1)
+ Rtl8812auTables.MAC16 -> macMask(conn, addr, mask, v, 2)
+ else -> bbMask(conn, addr, mask, v)
+ }
+ }
+ }
+
+ /**
+ * The board's RFE type from the adapter's efuse (logical byte 0xCA, as Hal_ReadRFEType_8812A): which antenna switch
+ * and LNA lines the baseband drives. An empty efuse is type 0, and a type with bit 7 set (the PA / LNA flags are
+ * elsewhere in the efuse) is taken as 4.
+ */
+ private fun readRfeType(conn: UsbDeviceConnection): Int {
+ // Efuse power: access key, loader clock and the efuse loader reset on.
+ w8(conn, REG_EFUSE_ACCESS, 0x69)
+ w16(conn, REG_SYS_FUNC_EN, r16(conn, REG_SYS_FUNC_EN) or FEN_ELDR)
+ w16(conn, REG_SYS_CLKR, r16(conn, REG_SYS_CLKR) or LOADER_CLK_EN or ANA8M)
+ try {
+ val map = EfuseMap.logical(512) { addr -> efuseByte(conn, addr) }
+ val v = map[0xCA]
+ return when {
+ v == 0xFF -> 0
+ v and 0x80 != 0 -> 4
+ else -> v and 0x3F
+ }
+ } finally {
+ w8(conn, REG_EFUSE_ACCESS, 0x00)
+ }
+ }
+
+ private fun efuseByte(conn: UsbDeviceConnection, addr: Int): Int {
+ w8(conn, REG_EFUSE_CTRL + 1, addr and 0xFF)
+ w8(conn, REG_EFUSE_CTRL + 2, ((addr shr 8) and 0x03) or (r8(conn, REG_EFUSE_CTRL + 2) and 0xFC))
+ w8(conn, REG_EFUSE_CTRL + 3, r8(conn, REG_EFUSE_CTRL + 3) and 0x7F)
+ var tries = 0
+ while ((r8(conn, REG_EFUSE_CTRL + 3) and 0x80) == 0 && tries < 100) { sleep(1); tries++ }
+ return if (tries < 100) r8(conn, REG_EFUSE_CTRL) else 0xFF
+ }
+
+ /** The efuse's logical map out of its raw bytes (Hal_EfuseReadEFuse8812A): headers say which words follow. */
+ internal object EfuseMap {
+ fun logical(rawLen: Int, raw: (Int) -> Int): IntArray {
+ val words = IntArray(64 * 4) { 0xFFFF }
+ var addr = 0
+ var h = raw(addr)
+ if (h == 0xFF) return IntArray(512) { 0xFF }
+ addr++
+ while (h != 0xFF && addr < rawLen) {
+ val offset: Int; var wren: Int
+ if ((h and 0x1F) == 0x0F) {
+ val low = (h and 0xE0) shr 5
+ h = raw(addr)
+ if ((h and 0x0F) == 0x0F) {
+ addr++
+ h = raw(addr)
+ if (h != 0xFF && addr < rawLen) addr++
+ continue
+ }
+ offset = ((h and 0xF0) shr 1) or low
+ wren = h and 0x0F
+ addr++
+ } else {
+ offset = (h shr 4) and 0x0F
+ wren = h and 0x0F
+ }
+ for (i in 0 until 4) {
+ if ((wren and 1) == 0) {
+ val lo = raw(addr); addr++
+ if (addr >= rawLen) break
+ val hi = raw(addr); addr++
+ if (offset < 64) words[offset * 4 + i] = lo or (hi shl 8)
+ if (addr >= rawLen) break
+ }
+ wren = wren shr 1
+ }
+ h = raw(addr)
+ if (h != 0xFF && addr < rawLen) addr++
+ }
+ return IntArray(512) { i -> val w = words[i / 2]; if (i % 2 == 0) w and 0xFF else (w shr 8) and 0xFF }
+ }
+ }
+
+ private class RxState {
+ var total = 0L; var bufs = 0L; var lastLog = 0L; var hopAt = 0L; var lastEmit = 0L; var hopIdx = 0
+ var frames = 0L
+ fun start() { val t = System.currentTimeMillis(); lastLog = t; hopAt = t; lastEmit = t }
+ }
+
+ private fun rxTick(conn: UsbDeviceConnection, now: Long, st: RxState,
+ onFrame: (List<MonitorSighting>) -> Unit, onStatus: (String) -> Unit) {
+ if (now - st.lastLog > 3000) {
+ val (aps, clients) = frames.live(now, REPORT_MS)
+ UsbWifi.log("RTL8812AU: RX ${st.bufs} bufs / ${st.total} B ch ${HOP[st.hopIdx]} | ${aps + clients} live ($aps AP · $clients client, ${frames.tracked} tracked)")
+ st.lastLog = now
+ }
+ if (now - st.lastEmit > 2000) { emitSightings(onFrame); st.lastEmit = now }
+ if (now - st.hopAt > DWELL_MS) {
+ val ni = (st.hopIdx + 1) % HOP.size
+ if (ni != st.hopIdx) { st.hopIdx = ni; runCatching { channelTune(conn, HOP[st.hopIdx]) } }
+ val (aps, clients) = frames.live(now, REPORT_MS)
+ onStatus("RTL8812AU · live · $aps access points · $clients devices · ch ${HOP[st.hopIdx]}")
+ st.hopAt = now
+ }
+ }
+
+ private fun rxLoop(conn: UsbDeviceConnection, epRx: UsbEndpoint,
+ onFrame: (List<MonitorSighting>) -> Unit, onStatus: (String) -> Unit) {
+ val nbuf = 24; val bufSz = 16384
+ val buffers = Array(nbuf) { ByteBuffer.allocateDirect(bufSz) }
+ val reqs = ArrayList<UsbRequest>(nbuf)
+ var initOk = true
+ for (i in 0 until nbuf) {
+ val r = UsbRequest()
+ if (!r.initialize(conn, epRx)) { initOk = false; break }
+ r.clientData = i; buffers[i].clear()
+ if (!r.queue(buffers[i])) { initOk = false; break }
+ reqs.add(r)
+ }
+ if (!initOk) {
+ UsbWifi.log("RTL8812AU: async RX unavailable (${reqs.size}/$nbuf) — synchronous RX")
+ reqs.forEach { runCatching { it.cancel() }; runCatching { it.close() } }
+ rxLoopSync(conn, epRx, onFrame, onStatus); return
+ }
+ UsbWifi.log("RTL8812AU: async RX live — $nbuf buffers in flight")
+ val scratch = ByteArray(bufSz)
+ val st = RxState().apply { start() }
+ try {
+ while (!stop) {
+ val req = try { conn.requestWait(100L) } catch (e: TimeoutException) { null }
+ val now = System.currentTimeMillis()
+ if (req != null) {
+ val idx = req.clientData as? Int ?: -1
+ if (idx in 0 until nbuf) {
+ val b = buffers[idx]
+ val n = b.position()
+ val parseLen = if (n in 1..bufSz) {
+ st.total += n; st.bufs++
+ b.rewind(); b.get(scratch, 0, n); n
+ } else 0
+
+ b.clear()
+ if (!runCatching { req.queue(b) }.getOrDefault(false)) {
+ runCatching { req.close() }
+ val nr = UsbRequest()
+ if (runCatching { nr.initialize(conn, epRx) }.getOrDefault(false)) {
+ nr.clientData = idx; b.clear()
+ if (runCatching { nr.queue(b) }.getOrDefault(false)) reqs[idx] = nr
+ }
+ }
+ if (parseLen > 0) runCatching { st.frames += parseRxBuf(scratch, parseLen, HOP[st.hopIdx]) }
+ }
+ }
+ rxTick(conn, now, st, onFrame, onStatus)
+ }
+ } finally {
+ reqs.forEach { runCatching { it.cancel() }; runCatching { it.close() } }
+ }
+ emitSightings(onFrame)
+ UsbWifi.log("RTL8812AU: RX loop stopped (${st.total} B)")
+ }
+
+ private fun rxLoopSync(conn: UsbDeviceConnection, epRx: UsbEndpoint,
+ onFrame: (List<MonitorSighting>) -> Unit, onStatus: (String) -> Unit) {
+ val buf = ByteArray(16384)
+ val st = RxState().apply { start() }
+ while (!stop) {
+ val n = conn.bulkTransfer(epRx, buf, buf.size, 500)
+ val now = System.currentTimeMillis()
+ if (n > 0) { st.total += n; st.bufs++; runCatching { st.frames += parseRxBuf(buf, n, HOP[st.hopIdx]) } }
+ rxTick(conn, now, st, onFrame, onStatus)
+ }
+ emitSightings(onFrame)
+ UsbWifi.log("RTL8812AU: RX loop stopped (${st.total} B)")
+ }
+
+ /** Parses one USB buffer; returns how many good frames it held. */
+ private fun parseRxBuf(b: ByteArray, n: Int, ch: Int): Int {
+ val size = Rtl8812auTables.RXDESC_SIZE
+ var off = 0
+ var good = 0
+ while (off + size <= n) {
+ val d0 = le32(b, off)
+ val pkt = d0 and 0x3fff
+ if (pkt == 0 || pkt > 4096) break
+ val crc = (d0 ushr 14) and 1
+ val drv = ((d0 ushr 16) and 0xf) * 8
+ val shift = (d0 ushr 24) and 3
+ val fs = off + size + drv + shift
+ val fe = minOf(n, fs + pkt)
+ if (crc == 0 && fe - fs >= 24) {
+ val rate = b[off + 12].toInt() and 0x7f
+ val rssi = if (drv >= 8 && off + size + drv <= n) rtlRssi(b, off + size, rate) else NOMINAL_RSSI
+ frames.frame(b, fs, fe, ch, rssi)
+ good++
+ }
+ off += (size + drv + shift + pkt + 7) and 7.inv()
+ }
+ return good
+ }
+
+ private fun rtlRssi(b: ByteArray, di: Int, rate: Int): Int {
+ fun u8(i: Int) = b[i].toInt() and 0xff
+ return if (rate <= 3) {
+ val agc = u8(di + 5); val lna = (agc and 0xE0) shr 5; val vga = agc and 0x1F
+ when (lna) {
+ 7 -> if (vga <= 27) -94 + 2 * (27 - vga) else -94
+ 6 -> -42 + 2 * (2 - vga)
+ 5 -> -36 + 2 * (7 - vga)
+ 4 -> -30 + 2 * (7 - vga)
+ 3 -> -18 + 2 * (7 - vga)
+ 2 -> 2 * (5 - vga)
+ 1 -> 14 - 2 * vga
+ else -> 20 - 2 * vga
+ }
+ } else {
+ ((u8(di + 4) shr 1) and 0x7f) - 110
+ }
+ }
+
+ private fun emitSightings(onFrame: (List<MonitorSighting>) -> Unit) {
+ val batch = frames.drain(System.currentTimeMillis(), FRESH_MS)
+ if (batch.isNotEmpty()) onFrame(batch)
+ }
+
+ private fun r(conn: UsbDeviceConnection, addr: Int, len: Int): ByteArray {
+ val b = ByteArray(len)
+ return if (conn.controlTransfer(IN_VENDOR, VREQ, addr, 0, b, len, 500) == len) b else ByteArray(len)
+ }
+ private fun w(conn: UsbDeviceConnection, addr: Int, data: ByteArray) {
+ conn.controlTransfer(OUT_VENDOR, VREQ, addr, 0, data, data.size, 500)
+ }
+ private fun r8(conn: UsbDeviceConnection, a: Int) = r(conn, a, 1)[0].toInt() and 0xff
+ private fun r16(conn: UsbDeviceConnection, a: Int): Int { val b = r(conn, a, 2); return (b[0].toInt() and 0xff) or ((b[1].toInt() and 0xff) shl 8) }
+ private fun r32(conn: UsbDeviceConnection, a: Int) = le32(r(conn, a, 4), 0)
+ private fun w8(conn: UsbDeviceConnection, a: Int, v: Int) = w(conn, a, byteArrayOf(v.toByte()))
+ private fun w16(conn: UsbDeviceConnection, a: Int, v: Int) = w(conn, a, byteArrayOf(v.toByte(), (v ushr 8).toByte()))
+ private fun w32(conn: UsbDeviceConnection, a: Int, v: Int) = w(conn, a, byteArrayOf(v.toByte(), (v ushr 8).toByte(), (v ushr 16).toByte(), (v ushr 24).toByte()))
+
+ private fun bbMask(conn: UsbDeviceConnection, a: Int, mask: Int, v: Int) {
+ val cur = r32(conn, a); w32(conn, a, (cur and mask.inv()) or ((v shl sh(mask)) and mask))
+ }
+ private fun macMask(conn: UsbDeviceConnection, a: Int, mask: Int, v: Int, width: Int) {
+ val s = sh(mask)
+ if (width == 1) { val cur = r8(conn, a); val m = mask and 0xff; w8(conn, a, (cur and m.inv()) or ((v shl s) and m)) }
+ else { val cur = r16(conn, a); val m = mask and 0xffff; w16(conn, a, (cur and m.inv()) or ((v shl s) and m)) }
+ }
+
+ /** [path] 0 = radio A, 1 = radio B. */
+ private fun rfWrite(conn: UsbDeviceConnection, path: Int, addr: Int, v: Int) {
+ val d = v and 0xfffff
+ w32(conn, if (path == 0) rA_LSSIWrite else rB_LSSIWrite, (((addr and 0xff) shl 20) or d) and 0x0fffffff)
+ rfShadow[path][addr] = d
+ }
+ private fun rfRead(conn: UsbDeviceConnection, path: Int, addr: Int): Int {
+ rfShadow[path][addr]?.let { return it }
+ bbMask(conn, rHSSIRead, 0xff, addr and 0xff); sleep(1)
+ return r32(conn, if (path == 0) rA_PIRead else rB_PIRead) and 0xfffff
+ }
+ private fun rfMask(conn: UsbDeviceConnection, path: Int, addr: Int, mask: Int, v: Int) {
+ val cur = rfRead(conn, path, addr); rfWrite(conn, path, addr, (cur and mask.inv()) or ((v shl sh(mask)) and mask))
+ }
+
+ private fun sh(mask: Int) = Integer.numberOfTrailingZeros(mask)
+ private fun le32(b: ByteArray, i: Int) = (b[i].toInt() and 0xff) or ((b[i+1].toInt() and 0xff) shl 8) or ((b[i+2].toInt() and 0xff) shl 16) or ((b[i+3].toInt() and 0xff) shl 24)
+ private fun sleep(ms: Int) = try { Thread.sleep(ms.toLong()) } catch (_: InterruptedException) {}
+}
diff --git a/app/src/main/java/com/rfsentinel/app/usb/Rtl8812auTables.kt b/app/src/main/java/com/rfsentinel/app/usb/Rtl8812auTables.kt
new file mode 100644
index 0000000..473828c
--- /dev/null
+++ b/app/src/main/java/com/rfsentinel/app/usb/Rtl8812auTables.kt
@@ -0,0 +1,381 @@
+package com.rfsentinel.app.usb
+
+/*
+ * RTL8812AU register tables (power-on sequence, MAC / baseband / AGC / radio A and B tables) for receive-only
+ * monitor mode over USB.
+ *
+ * Generated from the Realtek rtl8812au Linux driver as maintained in aircrack-ng/rtl8812au
+ * (hal/phydm/rtl8812a/halhwimg8812a_{mac,bb,rf}.c and hal/rtl8812a/Hal8812PwrSeq): the vendor's condition
+ * blocks are resolved for a USB board without an external PA or LNA, which is the plain-board branch, and
+ * the tables are kept as they are. GPL-2.0, used under the GNU GPL v3 of RF Sentinel. The transmit power,
+ * power-by-rate and power-tracking tables are not here: nothing is ever sent.
+ */
+
+object Rtl8812auTables {
+
+ const val W = 0; const val P = 1; const val D = 2; const val E = 3
+
+ /** In a table: a pause of this many milliseconds follows (the second number). */
+ const val DELAY = -2
+
+ /** Card disabled -> card emulation -> active (Hal8812PwrSeq, USB): write / poll / delay / end, like the 8821AU table. */
+ val PWR_ON = arrayOf(
+ intArrayOf(0, 0x12, 0x1, 0x1),
+ intArrayOf(0, 0x14, 0x80, 0x0),
+ intArrayOf(0, 0x15, 0x1, 0x0),
+ intArrayOf(0, 0x23, 0x10, 0x0),
+ intArrayOf(0, 0x46, 0xFF, 0x0),
+ intArrayOf(0, 0x43, 0xFF, 0x0),
+ intArrayOf(0, 0x5, 0x8, 0x0),
+ intArrayOf(0, 0x3, 0x4, 0x4),
+ intArrayOf(0, 0x24, 0x2, 0x2),
+ intArrayOf(0, 0x28, 0x8, 0x8),
+ intArrayOf(0, 0x5, 0x4, 0x0),
+ intArrayOf(1, 0x6, 0x2, 0x2),
+ intArrayOf(0, 0x5, 0x8, 0x0),
+ intArrayOf(0, 0x5, 0x1, 0x1),
+ intArrayOf(1, 0x5, 0x1, 0x0),
+ intArrayOf(0, 0x24, 0x2, 0x0),
+ intArrayOf(0, 0x28, 0x8, 0x0),
+ intArrayOf(3, 0x0, 0x0, 0x0)
+ )
+
+ /** MAC registers: address, value (one byte each). */
+ val MAC_REG = intArrayOf(
+ 0x10, 0xC, 0x11, 0x66, 0x25, 0xF, 0x72, 0x0,
+ 0x420, 0x80, 0x428, 0xA, 0x429, 0x10, 0x430, 0x0,
+ 0x431, 0x0, 0x432, 0x0, 0x433, 0x1, 0x434, 0x2,
+ 0x435, 0x3, 0x436, 0x5, 0x437, 0x7, 0x438, 0x0,
+ 0x439, 0x0, 0x43A, 0x0, 0x43B, 0x1, 0x43C, 0x2,
+ 0x43D, 0x3, 0x43E, 0x5, 0x43F, 0x7, 0x440, 0x5D,
+ 0x441, 0x1, 0x442, 0x0, 0x444, 0x10, 0x445, 0x0,
+ 0x446, 0x0, 0x447, 0x0, 0x448, 0x0, 0x449, 0xF0,
+ 0x44A, 0xF, 0x44B, 0x3E, 0x44C, 0x10, 0x44D, 0x0,
+ 0x44E, 0x0, 0x44F, 0x0, 0x450, 0x0, 0x451, 0xF0,
+ 0x452, 0xF, 0x453, 0x0, 0x45B, 0x80, 0x460, 0x66,
+ 0x461, 0x66, 0x4C8, 0xFF, 0x4C9, 0x8, 0x4CC, 0xFF,
+ 0x4CD, 0xFF, 0x4CE, 0x1, 0x500, 0x26, 0x501, 0xA2,
+ 0x502, 0x2F, 0x503, 0x0, 0x504, 0x28, 0x505, 0xA3,
+ 0x506, 0x5E, 0x507, 0x0, 0x508, 0x2B, 0x509, 0xA4,
+ 0x50A, 0x5E, 0x50B, 0x0, 0x50C, 0x4F, 0x50D, 0xA4,
+ 0x50E, 0x0, 0x50F, 0x0, 0x512, 0x1C, 0x514, 0xA,
+ 0x516, 0xA, 0x525, 0x4F, 0x550, 0x10, 0x551, 0x10,
+ 0x559, 0x2, 0x55C, 0x50, 0x55D, 0xFF, 0x604, 0x9,
+ 0x605, 0x30, 0x607, 0x3, 0x608, 0xE, 0x609, 0x2A,
+ 0x620, 0xFF, 0x621, 0xFF, 0x622, 0xFF, 0x623, 0xFF,
+ 0x624, 0xFF, 0x625, 0xFF, 0x626, 0xFF, 0x627, 0xFF,
+ 0x638, 0x50, 0x63C, 0xA, 0x63D, 0xA, 0x63E, 0xE,
+ 0x63F, 0xE, 0x640, 0x80, 0x642, 0x40, 0x643, 0x0,
+ 0x652, 0xC8, 0x66E, 0x5, 0x700, 0x21, 0x701, 0x43,
+ 0x702, 0x65, 0x703, 0x87, 0x708, 0x21, 0x709, 0x43,
+ 0x70A, 0x65, 0x70B, 0x87, 0x718, 0x40
+ )
+
+ /** Baseband registers: address, value (a whole word each). */
+ val PHY_REG = intArrayOf(
+ 0x800, 0x8020D010.toInt(), 0x804, 0x80112E0, 0x808, 0xE028233, 0x80C, 0x12131113,
+ 0x810, 0x20101263, 0x814, 0x20C3D10, 0x818, 0x3A00385, 0x820, 0x0,
+ 0x824, 0x30FE0, 0x828, 0x0, 0x82C, 0x2083DD, 0x830, 0x2EAAEEB8,
+ 0x834, 0x37A706, 0x838, 0x6C89B44, 0x83C, 0x95B, 0x840, 0xC0000001.toInt(),
+ 0x844, 0x40003CDE, 0x848, 0x6210FF8B, 0x84C, 0x6CFDFFB8, 0x850, 0x28874706,
+ 0x854, 0x1520C, 0x858, 0x8060E000.toInt(), 0x85C, 0x74210168, 0x860, 0x6929C321,
+ 0x864, 0x79727432, 0x868, 0x8CA7A314.toInt(), 0x86C, 0x338C2878, 0x870, 0x3333333,
+ 0x874, 0x31602C2E, 0x878, 0x3152, 0x87C, 0xFC000, 0x8A0, 0x13,
+ 0x8A4, 0x7F7F7F7F, 0x8A8, 0xA202033E.toInt(), 0x8AC, 0xFF0FA0A, 0x8B0, 0x600,
+ 0x8B4, 0xFC080, 0x8B8, 0x6C10D7FF, 0x8BC, 0x4CA520A3, 0x8C0, 0x27F00020,
+ 0x8C4, 0x0, 0x8C8, 0x12D69, 0x8CC, 0x8248492, 0x8D0, 0xB800,
+ 0x8DC, 0x0, 0x8D4, 0x940008A0.toInt(), 0x8D8, 0x290B5612, 0x8F8, 0x400002C0,
+ 0x8FC, 0x0, 0x900, 0x701, 0x90C, 0x0, 0x910, 0xFC00,
+ 0x914, 0x404, 0x918, 0x1C1028C0, 0x91C, 0x64B11A1C, 0x920, 0xE0767233.toInt(),
+ 0x924, 0x55AA500, 0x928, 0x4, 0x92C, 0xFFFE0000.toInt(), 0x930, 0xFFFFFFFE.toInt(),
+ 0x934, 0x1FFFFF, 0x960, 0x0, 0x964, 0x0, 0x968, 0x0,
+ 0x96C, 0x0, 0x970, 0x801FFFFF.toInt(), 0x978, 0x0, 0x97C, 0x0,
+ 0x980, 0x0, 0x984, 0x0, 0x988, 0x0, 0x990, 0x27100000,
+ 0x994, 0xFFFF0100.toInt(), 0x998, 0xFFFFFF5C.toInt(), 0x99C, 0xFFFFFFFF.toInt(), 0x9A0, 0xFF,
+ 0x9A4, 0x80080, 0x9A8, 0x0, 0x9AC, 0x0, 0x9B0, 0x81081008.toInt(),
+ 0x9B4, 0x0, 0x9B8, 0x1081008, 0x9BC, 0x1081008, 0x9D0, 0x0,
+ 0x9D4, 0x0, 0x9D8, 0x0, 0x9DC, 0x0, 0x9E4, 0x3,
+ 0x9E8, 0x2D5, 0xA00, 0xD047C8, 0xA04, 0x1FF000C, 0xA08, 0x8C838300.toInt(),
+ 0xA0C, 0x2E7F000F, 0xA10, 0x9500BB78.toInt(), 0xA14, 0x11144028, 0xA18, 0x881117,
+ 0xA1C, 0x89140F00.toInt(), 0xA20, 0x1A1B0000, 0xA24, 0x90E1217, 0xA28, 0x305,
+ 0xA2C, 0x900000, 0xA70, 0x101FFF00, 0xA74, 0x8, 0xA78, 0x900,
+ 0xA7C, 0x225B0606, 0xA80, 0x218075B2, 0xA84, 0x1F8C80, 0xB00, 0x3100000,
+ 0xB04, 0xB000, 0xB08, 0xAE0201EB.toInt(), 0xB0C, 0x1003207, 0xB10, 0x9807,
+ 0xB14, 0x1000000, 0xB18, 0x2, 0xB1C, 0x2, 0xB20, 0x1F,
+ 0xB24, 0x3020100, 0xB28, 0x7060504, 0xB2C, 0xB0A0908, 0xB30, 0xF0E0D0C,
+ 0xB34, 0x13121110, 0xB38, 0x17161514, 0xB3C, 0x3A, 0xB40, 0x0,
+ 0xB44, 0x0, 0xB48, 0x13000032, 0xB4C, 0x48080000, 0xB50, 0x0,
+ 0xB54, 0x0, 0xB58, 0x0, 0xB5C, 0x0, 0xC00, 0x7,
+ 0xC04, 0x42020, 0xC08, 0x80410231.toInt(), 0xC0C, 0x0, 0xC10, 0x100,
+ 0xC14, 0x1000000, 0xC1C, 0x40000003, 0xC20, 0x12121212, 0xC24, 0x12121212,
+ 0xC28, 0x12121212, 0xC2C, 0x12121212, 0xC30, 0x12121212, 0xC34, 0x12121212,
+ 0xC38, 0x12121212, 0xC3C, 0x12121212, 0xC40, 0x12121212, 0xC44, 0x12121212,
+ 0xC48, 0x12121212, 0xC4C, 0x12121212, 0xC50, 0x20, 0xC54, 0x8121C,
+ 0xC58, 0x30000C1C, 0xC5C, 0x58, 0xC60, 0x34344443, 0xC64, 0x7003333,
+ 0xC68, 0x59799979, 0xC6C, 0x59795979, 0xC70, 0x19795979, 0xC74, 0x19795979,
+ 0xC78, 0x19791979, 0xC7C, 0x19791979, 0xC80, 0x19791979, 0xC84, 0x19791979,
+ 0xC94, 0x100005C, 0xC98, 0x0, 0xC9C, 0x0, 0xCA0, 0x29,
+ 0xCA4, 0x8040201, 0xCA8, 0x80402010.toInt(), 0xCB0, 0x77547777, 0xCB4, 0x77,
+ 0xCB8, 0x508242, 0xE00, 0x7, 0xE04, 0x42020, 0xE08, 0x80410231.toInt(),
+ 0xE0C, 0x0, 0xE10, 0x100, 0xE14, 0x1000000, 0xE1C, 0x40000003,
+ 0xE20, 0x12121212, 0xE24, 0x12121212, 0xE28, 0x12121212, 0xE2C, 0x12121212,
+ 0xE30, 0x12121212, 0xE34, 0x12121212, 0xE38, 0x12121212, 0xE3C, 0x12121212,
+ 0xE40, 0x12121212, 0xE44, 0x12121212, 0xE48, 0x12121212, 0xE4C, 0x12121212,
+ 0xE50, 0x20, 0xE54, 0x8121C, 0xE58, 0x30000C1C, 0xE5C, 0x58,
+ 0xE60, 0x34344443, 0xE64, 0x7003333, 0xE68, 0x59791979, 0xE6C, 0x59795979,
+ 0xE70, 0x19795979, 0xE74, 0x19795979, 0xE78, 0x19791979, 0xE7C, 0x19791979,
+ 0xE80, 0x19791979, 0xE84, 0x19791979, 0xE94, 0x100005C, 0xE98, 0x0,
+ 0xE9C, 0x0, 0xEA0, 0x29, 0xEA4, 0x8040201, 0xEA8, 0x80402010.toInt(),
+ 0xEB0, 0x77547777, 0xEB4, 0x77, 0xEB8, 0x508242
+ )
+
+ /** Gain-control table: address, value. */
+ val AGC_TAB = intArrayOf(
+ 0x81C, 0xFF000001.toInt(), 0x81C, 0xFF020001.toInt(), 0x81C, 0xFF040001.toInt(), 0x81C, 0xFF060001.toInt(),
+ 0x81C, 0xFF080001.toInt(), 0x81C, 0xFE0A0001.toInt(), 0x81C, 0xFD0C0001.toInt(), 0x81C, 0xFC0E0001.toInt(),
+ 0x81C, 0xFB100001.toInt(), 0x81C, 0xFA120001.toInt(), 0x81C, 0xF9140001.toInt(), 0x81C, 0xF8160001.toInt(),
+ 0x81C, 0xF7180001.toInt(), 0x81C, 0xF61A0001.toInt(), 0x81C, 0xF51C0001.toInt(), 0x81C, 0xF41E0001.toInt(),
+ 0x81C, 0xF3200001.toInt(), 0x81C, 0xF2220001.toInt(), 0x81C, 0xF1240001.toInt(), 0x81C, 0xF0260001.toInt(),
+ 0x81C, 0xEF280001.toInt(), 0x81C, 0xEE2A0001.toInt(), 0x81C, 0xED2C0001.toInt(), 0x81C, 0xEC2E0001.toInt(),
+ 0x81C, 0xEB300001.toInt(), 0x81C, 0xEA320001.toInt(), 0x81C, 0xE9340001.toInt(), 0x81C, 0xE8360001.toInt(),
+ 0x81C, 0xE7380001.toInt(), 0x81C, 0xE63A0001.toInt(), 0x81C, 0xE53C0001.toInt(), 0x81C, 0xC73E0001.toInt(),
+ 0x81C, 0xC6400001.toInt(), 0x81C, 0xC5420001.toInt(), 0x81C, 0xC4440001.toInt(), 0x81C, 0xC3460001.toInt(),
+ 0x81C, 0xC2480001.toInt(), 0x81C, 0xC14A0001.toInt(), 0x81C, 0xA74C0001.toInt(), 0x81C, 0xA64E0001.toInt(),
+ 0x81C, 0xA5500001.toInt(), 0x81C, 0xA4520001.toInt(), 0x81C, 0xA3540001.toInt(), 0x81C, 0xA2560001.toInt(),
+ 0x81C, 0xA1580001.toInt(), 0x81C, 0x675A0001, 0x81C, 0x665C0001, 0x81C, 0x655E0001,
+ 0x81C, 0x64600001, 0x81C, 0x63620001, 0x81C, 0x48640001, 0x81C, 0x47660001,
+ 0x81C, 0x46680001, 0x81C, 0x456A0001, 0x81C, 0x446C0001, 0x81C, 0x436E0001,
+ 0x81C, 0x42700001, 0x81C, 0x41720001, 0x81C, 0x41740001, 0x81C, 0x41760001,
+ 0x81C, 0x41780001, 0x81C, 0x417A0001, 0x81C, 0x417C0001, 0x81C, 0x417E0001,
+ 0x81C, 0xFF800001.toInt(), 0x81C, 0xFF820001.toInt(), 0x81C, 0xFF840001.toInt(), 0x81C, 0xFE860001.toInt(),
+ 0x81C, 0xFD880001.toInt(), 0x81C, 0xFC8A0001.toInt(), 0x81C, 0xFB8C0001.toInt(), 0x81C, 0xFA8E0001.toInt(),
+ 0x81C, 0xF9900001.toInt(), 0x81C, 0xF8920001.toInt(), 0x81C, 0xF7940001.toInt(), 0x81C, 0xF6960001.toInt(),
+ 0x81C, 0xF5980001.toInt(), 0x81C, 0xF49A0001.toInt(), 0x81C, 0xF39C0001.toInt(), 0x81C, 0xF29E0001.toInt(),
+ 0x81C, 0xF1A00001.toInt(), 0x81C, 0xF0A20001.toInt(), 0x81C, 0xEFA40001.toInt(), 0x81C, 0xEEA60001.toInt(),
+ 0x81C, 0xEDA80001.toInt(), 0x81C, 0xECAA0001.toInt(), 0x81C, 0xEBAC0001.toInt(), 0x81C, 0xEAAE0001.toInt(),
+ 0x81C, 0xE9B00001.toInt(), 0x81C, 0xE8B20001.toInt(), 0x81C, 0xE7B40001.toInt(), 0x81C, 0xE6B60001.toInt(),
+ 0x81C, 0xE5B80001.toInt(), 0x81C, 0xE4BA0001.toInt(), 0x81C, 0xE3BC0001.toInt(), 0x81C, 0xA8BE0001.toInt(),
+ 0x81C, 0xA7C00001.toInt(), 0x81C, 0xA6C20001.toInt(), 0x81C, 0xA5C40001.toInt(), 0x81C, 0xA4C60001.toInt(),
+ 0x81C, 0xA3C80001.toInt(), 0x81C, 0xA2CA0001.toInt(), 0x81C, 0xA1CC0001.toInt(), 0x81C, 0x68CE0001,
+ 0x81C, 0x67D00001, 0x81C, 0x66D20001, 0x81C, 0x65D40001, 0x81C, 0x64D60001,
+ 0x81C, 0x47D80001, 0x81C, 0x46DA0001, 0x81C, 0x45DC0001, 0x81C, 0x44DE0001,
+ 0x81C, 0x43E00001, 0x81C, 0x42E20001, 0x81C, 0x8E40001, 0x81C, 0x7E60001,
+ 0x81C, 0x6E80001, 0x81C, 0x5EA0001, 0x81C, 0x4EC0001, 0x81C, 0x3EE0001,
+ 0x81C, 0x2F00001, 0x81C, 0x1F20001, 0x81C, 0x1F40001, 0x81C, 0x1F60001,
+ 0x81C, 0x1F80001, 0x81C, 0x1FA0001, 0x81C, 0x1FC0001, 0x81C, 0x1FE0001,
+ 0xC50, 0x22, 0xC50, 0x20, 0xE50, 0x22, 0xE50, 0x20
+ )
+
+ /** Radio path A: register, value (20 bits). */
+ val RADIOA = intArrayOf(
+ 0x0, 0x10000, 0x18, 0x1712A, 0x56, 0x51CF2, 0x66, 0x40000,
+ 0x1E, 0x80000, 0x89, 0x80, 0x86, 0x14B38, 0x8B, 0x87180,
+ 0xB1, 0x1FC1A, 0xB3, 0xF0810, 0xB4, 0x1A78D, 0xBA, 0x86180,
+ 0x18, 0x6, 0xEF, 0x2000, 0x3B, 0x38A58, 0x3B, 0x37A58,
+ 0x3B, 0x2A590, 0x3B, 0x27A50, 0x3B, 0x18248, 0x3B, 0x10240,
+ 0x3B, 0x8240, 0xEF, 0x100, 0x34, 0xADF4, 0x34, 0x9DF1,
+ 0x34, 0x8DEE, 0x34, 0x7DEB, 0x34, 0x6DE8, 0x34, 0x5DE5,
+ 0x34, 0x4DE2, 0x34, 0x3CE6, 0x34, 0x24E7, 0x34, 0x14E4,
+ 0x34, 0x4E1, 0xEF, 0x0, 0xEF, 0x20A2, 0xDF, 0x80,
+ 0x35, 0x192, 0x35, 0x8192, 0x35, 0x10192, 0x36, 0x24,
+ 0x36, 0x8024, 0x36, 0x10024, 0x36, 0x18024, 0xEF, 0x0,
+ 0x51, 0xC21, 0x52, 0x6D9, 0x53, 0xFC649, 0x54, 0x17E,
+ 0xEF, 0x2, 0x8, 0x8400, 0x18, 0x1712A, 0xEF, 0x1000,
+ 0x3A, 0x80, 0x3B, 0x3A02C, 0x3C, 0x4000, 0x3A, 0x400,
+ 0x3B, 0x3202C, 0x3C, 0x10000, 0x3A, 0xA0, 0x3B, 0x2B064,
+ 0x3C, 0x4000, 0x3A, 0xD8, 0x3B, 0x23070, 0x3C, 0x4000,
+ 0x3A, 0x468, 0x3B, 0x1B870, 0x3C, 0x10000, 0x3A, 0x98,
+ 0x3B, 0x12085, 0x3C, 0xE4000, 0x3A, 0x418, 0x3B, 0xA080,
+ 0x3C, 0xF0000, 0x3A, 0x418, 0x3B, 0x2080, 0x3C, 0x10000,
+ 0x3A, 0x80, 0x3B, 0x7A02C, 0x3C, 0x4000, 0x3A, 0x400,
+ 0x3B, 0x7202C, 0x3C, 0x10000, 0x3A, 0xA0, 0x3B, 0x6B064,
+ 0x3C, 0x4000, 0x3A, 0xD8, 0x3B, 0x63070, 0x3C, 0x4000,
+ 0x3A, 0x468, 0x3B, 0x5B870, 0x3C, 0x10000, 0x3A, 0x98,
+ 0x3B, 0x52085, 0x3C, 0xE4000, 0x3A, 0x418, 0x3B, 0x4A080,
+ 0x3C, 0xF0000, 0x3A, 0x418, 0x3B, 0x42080, 0x3C, 0x10000,
+ 0x3A, 0x80, 0x3B, 0xBA02C, 0x3C, 0x4000, 0x3A, 0x400,
+ 0x3B, 0xB202C, 0x3C, 0x10000, 0x3A, 0xA0, 0x3B, 0xAB064,
+ 0x3C, 0x4000, 0x3A, 0xD8, 0x3B, 0xA3070, 0x3C, 0x4000,
+ 0x3A, 0x468, 0x3B, 0x9B870, 0x3C, 0x10000, 0x3A, 0x98,
+ 0x3B, 0x92085, 0x3C, 0xE4000, 0x3A, 0x418, 0x3B, 0x8A080,
+ 0x3C, 0xF0000, 0x3A, 0x418, 0x3B, 0x82080, 0x3C, 0x10000,
+ 0xEF, 0x1100, 0x34, 0x4ADF5, 0x34, 0x49DF2, 0x34, 0x48DEF,
+ 0x34, 0x47DEC, 0x34, 0x46DE9, 0x34, 0x45DE6, 0x34, 0x44DE3,
+ 0x34, 0x438C8, 0x34, 0x428C5, 0x34, 0x418C2, 0x34, 0x408C0,
+ 0x34, 0x2ADF5, 0x34, 0x29DF2, 0x34, 0x28DEF, 0x34, 0x27DEC,
+ 0x34, 0x26DE9, 0x34, 0x25DE6, 0x34, 0x24DE3, 0x34, 0x238C8,
+ 0x34, 0x228C5, 0x34, 0x218C2, 0x34, 0x208C0, 0x34, 0xAFF7,
+ 0x34, 0x9DF7, 0x34, 0x8DF4, 0x34, 0x7DF1, 0x34, 0x6DEE,
+ 0x34, 0x5DEB, 0x34, 0x4DE8, 0x34, 0x38CC, 0x34, 0x28C9,
+ 0x34, 0x18C6, 0x34, 0x8C3, 0xEF, 0x0, 0x18, 0x1712A,
+ 0xEF, 0x40, 0x35, 0x188, 0x35, 0x8147, 0x35, 0x10147,
+ 0x35, 0x201D7, 0x35, 0x281D7, 0x35, 0x301D7, 0x35, 0x401D8,
+ 0x35, 0x481D8, 0x35, 0x501D8, 0xEF, 0x0, 0x18, 0x1712A,
+ 0xEF, 0x10, 0x36, 0x84EB4, 0x36, 0x8CC35, 0x36, 0x94C35,
+ 0x36, 0x9CC35, 0x36, 0xA4C35, 0x36, 0xACC35, 0x36, 0xB4C35,
+ 0x36, 0xBCC35, 0x36, 0xC4C34, 0x36, 0xCCC35, 0x36, 0xD4C35,
+ 0x36, 0xDCC35, 0xEF, 0x0, 0xEF, 0x8, 0x3C, 0x2A8,
+ 0x3C, 0x5A2, 0x3C, 0x880, 0xEF, 0x0, 0x18, 0x1712A,
+ 0xEF, 0x2, 0xDF, 0x80, 0x1F, 0x64, 0x61, 0xE5D53,
+ 0x62, 0x38FCD, 0x63, 0x114EB, 0x64, 0x196AC, 0x65, 0x911D7,
+ 0x8, 0x8400, 0x1C, 0x739D2, 0xB4, 0x1E78D, 0x18, 0x1F12A,
+ 0xFFFFFFFE.toInt(), 0x32, 0xFFFFFFFE.toInt(), 0x32, 0xFFFFFFFE.toInt(), 0x32, 0xFFFFFFFE.toInt(), 0x32,
+ 0xB4, 0x1A78D, 0x18, 0x1712A
+ )
+
+ /** Radio path B: register, value (20 bits). */
+ val RADIOB = intArrayOf(
+ 0x56, 0x51CF2, 0x66, 0x40000, 0x89, 0x80, 0x86, 0x14B38,
+ 0x8B, 0x87180, 0x18, 0x6, 0xEF, 0x2000, 0x3B, 0x38A58,
+ 0x3B, 0x37A58, 0x3B, 0x2A590, 0x3B, 0x27A50, 0x3B, 0x18248,
+ 0x3B, 0x10240, 0x3B, 0x8240, 0xEF, 0x100, 0x34, 0xADF4,
+ 0x34, 0x9DF1, 0x34, 0x8DEE, 0x34, 0x7DEB, 0x34, 0x6DE8,
+ 0x34, 0x5DE5, 0x34, 0x4DE2, 0x34, 0x3CE6, 0x34, 0x24E7,
+ 0x34, 0x14E4, 0x34, 0x4E1, 0xEF, 0x0, 0xEF, 0x20A2,
+ 0xDF, 0x80, 0x35, 0x192, 0x35, 0x8192, 0x35, 0x10192,
+ 0x36, 0x24, 0x36, 0x8024, 0x36, 0x10024, 0x36, 0x18024,
+ 0xEF, 0x0, 0x51, 0xC21, 0x52, 0x6D9, 0x53, 0xFC649,
+ 0x54, 0x17E, 0xEF, 0x2, 0x8, 0x8400, 0x18, 0x1712A,
+ 0xEF, 0x1000, 0x3A, 0x80, 0x3B, 0x3A02C, 0x3C, 0x4000,
+ 0x3A, 0x400, 0x3B, 0x3202C, 0x3C, 0x10000, 0x3A, 0xA0,
+ 0x3B, 0x2B064, 0x3C, 0x4000, 0x3A, 0xD8, 0x3B, 0x23070,
+ 0x3C, 0x4000, 0x3A, 0x468, 0x3B, 0x1B870, 0x3C, 0x10000,
+ 0x3A, 0x98, 0x3B, 0x12085, 0x3C, 0xE4000, 0x3A, 0x418,
+ 0x3B, 0xA080, 0x3C, 0xF0000, 0x3A, 0x418, 0x3B, 0x2080,
+ 0x3C, 0x10000, 0x3A, 0x80, 0x3B, 0x7A02C, 0x3C, 0x4000,
+ 0x3A, 0x400, 0x3B, 0x7202C, 0x3C, 0x10000, 0x3A, 0xA0,
+ 0x3B, 0x6B064, 0x3C, 0x4000, 0x3A, 0xD8, 0x3B, 0x63070,
+ 0x3C, 0x4000, 0x3A, 0x468, 0x3B, 0x5B870, 0x3C, 0x10000,
+ 0x3A, 0x98, 0x3B, 0x52085, 0x3C, 0xE4000, 0x3A, 0x418,
+ 0x3B, 0x4A080, 0x3C, 0xF0000, 0x3A, 0x418, 0x3B, 0x42080,
+ 0x3C, 0x10000, 0x3A, 0x80, 0x3B, 0xBA02C, 0x3C, 0x4000,
+ 0x3A, 0x400, 0x3B, 0xB202C, 0x3C, 0x10000, 0x3A, 0xA0,
+ 0x3B, 0xAB064, 0x3C, 0x4000, 0x3A, 0xD8, 0x3B, 0xA3070,
+ 0x3C, 0x4000, 0x3A, 0x468, 0x3B, 0x9B870, 0x3C, 0x10000,
+ 0x3A, 0x98, 0x3B, 0x92085, 0x3C, 0xE4000, 0x3A, 0x418,
+ 0x3B, 0x8A080, 0x3C, 0xF0000, 0x3A, 0x418, 0x3B, 0x82080,
+ 0x3C, 0x10000, 0xEF, 0x1100, 0x34, 0x4ADF5, 0x34, 0x49DF2,
+ 0x34, 0x48DEF, 0x34, 0x47DEC, 0x34, 0x46DE9, 0x34, 0x45DE6,
+ 0x34, 0x44DE3, 0x34, 0x438C8, 0x34, 0x428C5, 0x34, 0x418C2,
+ 0x34, 0x408C0, 0x34, 0x2ADF5, 0x34, 0x29DF2, 0x34, 0x28DEF,
+ 0x34, 0x27DEC, 0x34, 0x26DE9, 0x34, 0x25DE6, 0x34, 0x24DE3,
+ 0x34, 0x238C8, 0x34, 0x228C5, 0x34, 0x218C2, 0x34, 0x208C0,
+ 0x34, 0xAFF7, 0x34, 0x9DF7, 0x34, 0x8DF4, 0x34, 0x7DF1,
+ 0x34, 0x6DEE, 0x34, 0x5DEB, 0x34, 0x4DE8, 0x34, 0x38CC,
+ 0x34, 0x28C9, 0x34, 0x18C6, 0x34, 0x8C3, 0xEF, 0x0,
+ 0x18, 0x1712A, 0xEF, 0x40, 0x35, 0x188, 0x35, 0x8147,
+ 0x35, 0x10147, 0x35, 0x201D7, 0x35, 0x281D7, 0x35, 0x301D7,
+ 0x35, 0x401D8, 0x35, 0x481D8, 0x35, 0x501D8, 0xEF, 0x0,
+ 0x18, 0x1712A, 0xEF, 0x10, 0x36, 0x84EB4, 0x36, 0x8CC35,
+ 0x36, 0x94C35, 0x36, 0x9CC35, 0x36, 0xA4C35, 0x36, 0xACC35,
+ 0x36, 0xB4C35, 0x36, 0xBCC35, 0x36, 0xC4C34, 0x36, 0xCCC35,
+ 0x36, 0xD4C35, 0x36, 0xDCC35, 0xEF, 0x0, 0xEF, 0x8,
+ 0x3C, 0x2A8, 0x3C, 0x5A2, 0x3C, 0x880, 0xEF, 0x0,
+ 0x18, 0x1712A, 0xEF, 0x2, 0xDF, 0x80, 0x61, 0xE5D53,
+ 0x62, 0x38FCD, 0x63, 0x114EB, 0x64, 0x196AC, 0x65, 0x911D7,
+ 0x8, 0x8400
+ )
+
+ const val BB = 0; const val RF = 1; const val MAC8 = 2; const val MAC16 = 3; const val RFB = 4
+
+ const val RFE_PINMUX_A = 0xCB0; const val RFE_INV_A = 0xCB4
+ const val RFE_PINMUX_B = 0xEB0; const val RFE_INV_B = 0xEB4
+ private const val RFE_INV_MASK = 0x3FF00000
+
+ /**
+ * The RFE pins (phy_SetRFEReg8812): which antenna switch / LNA / PA lines the baseband drives, by the board's
+ * RFE type (read from the adapter's efuse), for 2.4 or 5 GHz. A type that is not listed leaves the pins alone.
+ */
+ fun rfePins(rfe: Int, ghz5: Boolean): Array<IntArray> {
+ fun pin(v: Int, inv: Int) = arrayOf(
+ intArrayOf(BB, RFE_PINMUX_A, -1, v), intArrayOf(BB, RFE_PINMUX_B, -1, v),
+ intArrayOf(BB, RFE_INV_A, RFE_INV_MASK, inv), intArrayOf(BB, RFE_INV_B, RFE_INV_MASK, inv))
+ return if (!ghz5) when (rfe) {
+ 0, 1, 2 -> pin(0x77777777, 0x000)
+ 3 -> pin(0x54337770, 0x010) + intArrayOf(BB, 0x900, 0x303, 0x1)
+ 4 -> pin(0x77777777, 0x001)
+ 5 -> arrayOf(intArrayOf(BB, RFE_PINMUX_A, 0x00FF0000, 0x77), intArrayOf(BB, RFE_PINMUX_B, -1, 0x77777777),
+ intArrayOf(BB, RFE_INV_A, 0x01000000, 0x0), intArrayOf(BB, RFE_INV_B, RFE_INV_MASK, 0x000))
+ 6 -> arrayOf(intArrayOf(BB, RFE_PINMUX_A, -1, 0x07772770), intArrayOf(BB, RFE_PINMUX_B, -1, 0x07772770),
+ intArrayOf(BB, RFE_INV_A, -1, 0x77), intArrayOf(BB, RFE_INV_B, -1, 0x77))
+ else -> emptyArray()
+ } else when (rfe) {
+ 0 -> pin(0x77337717, 0x010)
+ 1 -> pin(0x77337717, 0x000)
+ 2, 4 -> pin(0x77337777, 0x010)
+ 3 -> pin(0x54337717, 0x010) + intArrayOf(BB, 0x900, 0x303, 0x1)
+ 5 -> arrayOf(intArrayOf(BB, RFE_PINMUX_A, 0x00FF0000, 0x33), intArrayOf(BB, RFE_PINMUX_B, -1, 0x77337777),
+ intArrayOf(BB, RFE_INV_A, 0x01000000, 0x1), intArrayOf(BB, RFE_INV_B, RFE_INV_MASK, 0x010))
+ 6 -> arrayOf(intArrayOf(BB, RFE_PINMUX_A, -1, 0x07737717), intArrayOf(BB, RFE_PINMUX_B, -1, 0x07737717),
+ intArrayOf(BB, RFE_INV_A, -1, 0x77), intArrayOf(BB, RFE_INV_B, -1, 0x77))
+ else -> emptyArray()
+ }
+ }
+
+ /**
+ * Tunes to [ch] at 20 MHz, receive only: the band switch (PHY_SwitchWirelessBand8812), the sub-band and channel
+ * for both radio paths (phy_SwChnl8812) and the 20 MHz bandwidth (phy_PostSetBwMode8812, PHY_RF6052SetBandwidth8812).
+ * [cut] is the chip's revision (0 = A, 1 = B, 2 = C...): the C cut has its own spur fix.
+ */
+ fun channelTune(ch: Int, rfe: Int, cut: Int): Array<IntArray> {
+ val g5 = ch > 14
+ val band: Array<IntArray> = if (!g5) arrayOf(
+ intArrayOf(BB, 0x808, 0x30000000, 0x3),
+ intArrayOf(BB, 0x834, 0x3, 0x1),
+ intArrayOf(BB, 0x830, 0x3E000, 0x17),
+ intArrayOf(BB, 0x830, 0xE, 0x4),
+ intArrayOf(BB, 0x82C, 0x3, 0x0)
+ ) + rfePins(rfe, false) + arrayOf(
+ intArrayOf(BB, 0x80C, 0xF0, 0x1),
+ intArrayOf(BB, 0xA04, 0x0F000000, 0x1),
+ intArrayOf(MAC8, 0x454, 0x80, 0x0)
+ ) else arrayOf(
+ intArrayOf(MAC8, 0x454, 0x80, 0x1),
+ intArrayOf(BB, 0x808, 0x30000000, 0x3),
+ intArrayOf(BB, 0x834, 0x3, 0x2),
+ intArrayOf(BB, 0x830, 0x3E000, 0x15),
+ intArrayOf(BB, 0x830, 0xE, 0x4),
+ intArrayOf(BB, 0x82C, 0x3, 0x1)
+ ) + rfePins(rfe, true) + arrayOf(
+ intArrayOf(BB, 0x80C, 0xF0, 0x0),
+ intArrayOf(BB, 0xA04, 0x0F000000, 0xF)
+ )
+ val fc = when { ch in 15..48 -> 0x494; ch in 50..80 -> 0x453; ch in 82..116 -> 0x452; ch >= 118 -> 0x412; else -> 0x96A }
+ val mod = when { ch in 15..80 -> 0x101; ch in 82..140 -> 0x301; ch > 140 -> 0x501; else -> 0x0 }
+ val chan = arrayOf(intArrayOf(BB, 0x860, 0x1FFE0000, fc)) + fixSpur(ch, cut) +
+ arrayOf(intArrayOf(RF, 0x18, 0x70300, mod), intArrayOf(RFB, 0x18, 0x70300, mod)) +
+ arrayOf(intArrayOf(RF, 0x18, 0xFF, ch), intArrayOf(RFB, 0x18, 0xFF, ch))
+ val bw20 = arrayOf(
+ intArrayOf(MAC16, 0x668, 0x180, 0x0),
+ intArrayOf(MAC8, 0x483, 0xFF, 0x0),
+ intArrayOf(BB, 0x8AC, 0x3003C3, 0x300200),
+ intArrayOf(BB, 0x8C4, 0x40000000, 0x0),
+ intArrayOf(BB, 0x848, 0x3C00000, 0x7)
+ ) + fixSpur(ch, cut) + arrayOf(intArrayOf(RF, 0x18, 0xC00, 0x3), intArrayOf(RFB, 0x18, 0xC00, 0x3))
+ return band + chan + bw20
+ }
+
+ /** phy_FixSpur_8812A at 20 MHz: the ADC clock for the 2.4 GHz spurs (the C cut and later also set the FIFO clock). */
+ private fun fixSpur(ch: Int, cut: Int): Array<IntArray> {
+ val top = ch == 13 || ch == 14
+ return if (cut >= 2) arrayOf(
+ intArrayOf(BB, 0x8AC, 0xC00, 0x2),
+ intArrayOf(BB, 0x8AC, 0x300, if (top) 0x3 else 0x2),
+ intArrayOf(BB, 0x8C4, 0x40000000, if (top) 0x1 else 0x0)
+ ) else if (ch <= 14) arrayOf(intArrayOf(BB, 0x8AC, 0x300, if (top) 0x3 else 0x2)) else emptyArray()
+ }
+
+ const val REG_CR = 0x100
+ const val REG_RXFF_BNDY = 0x116
+ const val REG_RCR = 0x608
+ const val REG_RXFLTMAP0 = 0x6A0
+ const val REG_RXFLTMAP1 = 0x6A2
+ const val REG_RXFLTMAP2 = 0x6A4
+ const val CR_INIT16 = 0x63F
+ const val CR_MACEN8 = 0xC0
+ const val RX_DMA_BOUNDARY = 0x3E7F
+
+ const val RCR_MONITOR = 0x80003B2F.toInt()
+ const val RXFLTMAP_ALL = 0xFFFF
+ const val RXDESC_SIZE = 24
+}
diff --git a/app/src/main/java/com/rfsentinel/app/usb/UsbWifi.kt b/app/src/main/java/com/rfsentinel/app/usb/UsbWifi.kt
index 4b5c684..10452a5 100644
--- a/app/src/main/java/com/rfsentinel/app/usb/UsbWifi.kt
+++ b/app/src/main/java/com/rfsentinel/app/usb/UsbWifi.kt
@@ -43,12 +43,13 @@ object UsbWifi {
private const val ACTION_PERMISSION = "com.rfsentinel.app.USB_WIFI_PERMISSION"
/** Driver families. */
- enum class Driver { RTL88XXAU, RTL88X2BU, RTL8187, RT3070, RTL8814AU, MT7612U, MT7921U, AR9271 }
+ enum class Driver { RTL88XXAU, RTL8812AU, RTL88X2BU, RTL8187, RT3070, RTL8814AU, MT7612U, MT7921U, AR9271 }
/** A supported adapter: the chip name shown to the user and the driver that runs it. */
data class Chip(val name: String, val driver: Driver)
private val AU = Chip("RTL8811AU / 8821AU", Driver.RTL88XXAU)
+ private val AU12 = Chip("RTL8812AU", Driver.RTL8812AU)
private val BU = Chip("RTL8812BU / 8822BU", Driver.RTL88X2BU)
private val L8187 = Chip("RTL8187L", Driver.RTL8187)
private val B8187 = Chip("RTL8187B", Driver.RTL8187)
@@ -67,6 +68,17 @@ object UsbWifi {
*/
private val SUPPORTED: Map<Pair<Int, Int>, Chip> = mapOf(
(0x0bda to 0x0811) to AU, // ALFA AWUS036ACS and other 88xxau dongles
+ (0x0bda to 0x8812) to AU12, // ALFA AWUS036ACH and other RTL8812AU dongles (IDs from the rtl8812au driver)
+ (0x0bda to 0x881a) to AU12, (0x0bda to 0x881b) to AU12, (0x0bda to 0x881c) to AU12,
+ (0x0409 to 0x0408) to AU12, (0x0411 to 0x025d) to AU12, (0x04bb to 0x0952) to AU12, // NEC, Buffalo WI-U3-866D, I-O DATA
+ (0x050d to 0x1106) to AU12, (0x050d to 0x1109) to AU12, (0x0586 to 0x3426) to AU12, (0x0789 to 0x016e) to AU12, // Belkin, ZyXEL, Logitec
+ (0x07b8 to 0x8812) to AU12, (0x0846 to 0x9051) to AU12, (0x0b05 to 0x17d2) to AU12, (0x0df6 to 0x0074) to AU12, // Abocom, Netgear A6200v2, ASUS
+ (0x0e66 to 0x0022) to AU12, (0x1058 to 0x0632) to AU12, (0x13b1 to 0x003f) to AU12, (0x148f to 0x9097) to AU12, // Hawking, WD, Linksys WUSB6300
+ (0x1740 to 0x0100) to AU12, (0x2001 to 0x330e) to AU12, (0x2001 to 0x3313) to AU12, (0x2001 to 0x3315) to AU12, // EnGenius, D-Link
+ (0x2001 to 0x3316) to AU12, (0x2019 to 0xab30) to AU12, (0x20f4 to 0x805b) to AU12, // Planex, TRENDnet
+ (0x2357 to 0x0101) to AU12, (0x2357 to 0x0103) to AU12, (0x2357 to 0x010d) to AU12, // TP-Link Archer T4U / T4UH
+ (0x2357 to 0x010e) to AU12, (0x2357 to 0x010f) to AU12, (0x2357 to 0x0122) to AU12,
+ (0x2604 to 0x0012) to AU12, (0x7392 to 0xa822) to AU12, // Tenda U12, Edimax
(0x0bda to 0xb812) to Chip("RTL8812BU", Driver.RTL88X2BU), // generic AC1200 dongles (e.g. Wise Tiger)
(0x0bda to 0xb82c) to Chip("RTL8822BU", Driver.RTL88X2BU),
(0x0bda to 0x2102) to BU, (0x0bda to 0xb81a) to BU,
@@ -116,7 +128,6 @@ object UsbWifi {
)
/** Known monitor-mode chips without a driver here yet (named in the status so users know why). */
private val KNOWN = mapOf(
- (0x0bda to 0x8812) to "RTL8812AU", (0x0bda to 0x881a) to "RTL8812AU",
(0x0bda to 0xc811) to "RTL8811CU / 8821CU", (0x0bda to 0xc820) to "RTL8821CU", (0x0bda to 0xc812) to "RTL8812CU",
(0x0cf3 to 0x7010) to "AR7010", (0x0cf3 to 0x7015) to "AR7010", (0x0e8d to 0x7610) to "MT7610U",
(0x148f to 0x3071) to "RT3071", (0x148f to 0x3072) to "RT3072", (0x148f to 0x5370) to "RT5370",
@@ -312,6 +323,7 @@ object UsbWifi {
Rtl8821auMonitor.lightEnabled = com.rfsentinel.app.util.Prefs.usbAdapterLight(context)
Rtl8821auMonitor.run(context, d, onStatus, onFrames)
}
+ Driver.RTL8812AU -> Rtl8812auMonitor.run(context, d, onStatus, onFrames)
Driver.RTL88X2BU -> Rtl8822buMonitor.run(context, d, chip.name, onStatus, onFrames)
Driver.RTL8187 -> Rtl8187Monitor.run(context, d, chip.name, onStatus, onFrames)
Driver.RT3070 -> Rt3070Monitor.run(context, d, chip.name, onStatus, onFrames)
@@ -328,6 +340,7 @@ object UsbWifi {
@Synchronized
private fun stopAdapter(newStatus: String) {
Rtl8821auMonitor.abort()
+ Rtl8812auMonitor.abort()
Rtl8822buMonitor.abort()
Rtl8187Monitor.abort()
Rt3070Monitor.abort()
diff --git a/app/src/test/java/com/rfsentinel/app/usb/Rtl8812auDriverTest.kt b/app/src/test/java/com/rfsentinel/app/usb/Rtl8812auDriverTest.kt
new file mode 100644
index 0000000..4d03827
--- /dev/null
+++ b/app/src/test/java/com/rfsentinel/app/usb/Rtl8812auDriverTest.kt
@@ -0,0 +1,149 @@
+package com.rfsentinel.app.usb
+
+import org.junit.Assert.assertArrayEquals
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertNotNull
+import org.junit.Assert.assertNull
+import org.junit.Assert.assertTrue
+import org.junit.Test
+
+/** The RTL8812AU driver: its tables came out of the vendor's condition blocks right, the channel tuning, the efuse map and the USB IDs. */
+class Rtl8812auDriverTest {
+
+ private val T = Rtl8812auTables
+
+ private fun find(tab: IntArray, addr: Int): List<Int> = (0 until tab.size / 2).filter { tab[it * 2] == addr }.map { tab[it * 2 + 1] }
+
+ @Test
+ fun theTablesAreWholePairsOfTheRightSizes() {
+ for ((name, tab) in listOf("MAC" to T.MAC_REG, "BB" to T.PHY_REG, "AGC" to T.AGC_TAB, "A" to T.RADIOA, "B" to T.RADIOB))
+ assertEquals("$name has pairs", 0, tab.size % 2)
+ assertTrue(T.MAC_REG.size / 2 in 100..130)
+ assertTrue(T.PHY_REG.size / 2 in 200..230)
+ assertTrue(T.AGC_TAB.size / 2 in 120..150)
+ assertTrue(T.RADIOA.size / 2 in 190..230)
+ assertTrue(T.RADIOB.size / 2 in 180..220)
+ }
+
+ @Test
+ fun theVendorsConditionBlocksPickedTheUsbBoardBranch() {
+ // 0x011: the USB branch (0x66), not the PCIe one (0x5A).
+ assertEquals(listOf(0x66), find(T.MAC_REG, 0x011))
+ // No condition marker is left in a table (they have bit 31 or 30 set in the address).
+ for (tab in listOf(T.MAC_REG, T.PHY_REG, T.AGC_TAB)) for (i in tab.indices step 2) {
+ val a = tab[i]
+ assertTrue("address 0x%X is an address".format(a), a == T.DELAY || a in 0..0xFFF)
+ }
+ // The plain-board branch of the radio: no external LNA / PA, no duplicate of one register back to back with a condition's value.
+ assertTrue(find(T.RADIOA, 0x018).isNotEmpty())
+ assertTrue(find(T.RADIOB, 0x018).isNotEmpty())
+ assertNotNull(find(T.PHY_REG, 0x800).singleOrNull())
+ assertEquals(0x8020D010.toInt(), find(T.PHY_REG, 0x800).single())
+ }
+
+ @Test
+ fun thePowerOnSequenceEndsAndPollsTheReadyBits() {
+ val steps = T.PWR_ON
+ assertEquals(T.E, steps.last()[0])
+ assertTrue("polls power ready (0x06 bit 1)", steps.any { it[0] == T.P && it[1] == 0x6 && it[2] == 0x2 })
+ assertTrue("no PCIe-only step is in it", steps.none { it[1] == 0x301 })
+ assertTrue(steps.size in 10..25)
+ }
+
+ @Test
+ fun aChannelIsWrittenToBothRadioPaths() {
+ for (ch in intArrayOf(1, 6, 13, 36, 149, 165)) {
+ val ops = T.channelTune(ch, rfe = 0, cut = 2)
+ fun chan(kind: Int) = ops.filter { it[0] == kind && it[1] == 0x18 && it[2] == 0xFF }.map { it[3] }
+ assertEquals("path A channel $ch", listOf(ch), chan(T.RF))
+ assertEquals("path B channel $ch", listOf(ch), chan(T.RFB))
+ // 20 MHz on both paths.
+ assertEquals(1, ops.count { it[0] == T.RF && it[1] == 0x18 && it[2] == 0xC00 && it[3] == 3 })
+ assertEquals(1, ops.count { it[0] == T.RFB && it[1] == 0x18 && it[2] == 0xC00 && it[3] == 3 })
+ }
+ }
+
+ @Test
+ fun theBandSwitchFollowsTheChannel() {
+ fun has(ops: Array<IntArray>, kind: Int, a: Int, mask: Int, v: Int) = ops.any { it[0] == kind && it[1] == a && it[2] == mask && it[3] == v }
+ val g24 = T.channelTune(6, 0, 2)
+ val g5 = T.channelTune(44, 0, 2)
+ assertTrue("2.4 GHz AGC table", has(g24, T.BB, 0x82C, 0x3, 0)); assertTrue("5 GHz AGC table", has(g5, T.BB, 0x82C, 0x3, 1))
+ assertTrue(has(g24, T.MAC8, 0x454, 0x80, 0)); assertTrue(has(g5, T.MAC8, 0x454, 0x80, 1))
+ assertTrue("5 GHz sub-band", has(g5, T.BB, 0x860, 0x1FFE0000, 0x494))
+ assertTrue("UNII-3", T.channelTune(149, 0, 2).any { it[0] == T.BB && it[1] == 0x860 && it[3] == 0x412 })
+ assertTrue("RF mode for UNII-3", T.channelTune(149, 0, 2).any { it[0] == T.RF && it[1] == 0x18 && it[2] == 0x70300 && it[3] == 0x501 })
+ assertTrue("RF mode for 2.4", has(g24, T.RF, 0x18, 0x70300, 0))
+ }
+
+ @Test
+ fun theAntennaPinsFollowTheBoardType() {
+ fun pinmux(rfe: Int, g5: Boolean) = T.rfePins(rfe, g5).first { it[1] == T.RFE_PINMUX_A }[3]
+ assertEquals(0x77777777, pinmux(0, false)); assertEquals(0x77337717, pinmux(0, true))
+ assertEquals(0x54337770, pinmux(3, false)); assertEquals(0x77337777, pinmux(2, true))
+ assertEquals(0, T.rfePins(9, true).size)
+ // Type 3 also switches the antenna select line.
+ assertTrue(T.rfePins(3, false).any { it[1] == 0x900 })
+ }
+
+ @Test
+ fun theCCutHasItsOwnSpurFix() {
+ fun spur(cut: Int, ch: Int) = T.channelTune(ch, 0, cut).filter { it[0] == T.BB && it[1] == 0x8C4 && it[2] == 0x40000000 }.map { it[3] }
+ // Older cuts leave the FIFO clock to the bandwidth step (always off at 20 MHz); the C cut turns it on for 2472 / 2484 MHz.
+ assertTrue(spur(0, 13).all { it == 0 })
+ assertEquals(1, spur(2, 13).last()); assertEquals(0, spur(2, 6).last())
+ }
+
+ // ---- the efuse ---------------------------------------------------------------------------
+
+ /** Raw efuse bytes: a header for section [offset] with the words in [words] enabled, then those words. */
+ private fun pg(offset: Int, vararg words: Pair<Int, Int>): List<Int> {
+ var wren = 0xF
+ for ((w, _) in words) wren = wren and (1 shl w).inv()
+ val out = mutableListOf((offset shl 4) or wren)
+ for ((w, v) in words.sortedBy { it.first }) { out += v and 0xFF; out += v shr 8 }
+ return out
+ }
+
+ private fun logical(raw: List<Int>): IntArray = Rtl8812auMonitor.EfuseMap.logical(512) { raw.getOrElse(it) { 0xFF } }
+
+ @Test
+ fun theEfuseMapCollectsWordsFromTheirHeaders() {
+ // Section 12 (bytes 0x60..0x67) word 1 = bytes 0x62, 0x63.
+ val map = logical(pg(12, 1 to 0xBEEF))
+ assertEquals(0xEF, map[0x62]); assertEquals(0xBE, map[0x63])
+ assertEquals("what is not written stays 0xFF", 0xFF, map[0x60])
+ }
+
+ @Test
+ fun theRfeTypeByteIsFoundThroughAnExtendedHeader() {
+ // Offset 0xCA is in section 25 (0xC8..0xCF), word 1 - section 25 needs an extended header: low 3 bits | next nibble.
+ val section = 25
+ val ext = listOf(((section and 0x7) shl 5) or 0x0F, ((section shr 3) shl 4) or (0xF and (1 shl 1).inv()))
+ val map = logical(ext + listOf(0x02, 0xFF))
+ assertEquals(0x02, map[0xCA])
+ }
+
+ @Test
+ fun anEmptyEfuseIsAllOnes() {
+ val map = logical(emptyList())
+ assertEquals(512, map.size)
+ assertTrue(map.all { it == 0xFF })
+ }
+
+ // ---- the adapters ------------------------------------------------------------------------
+
+ @Test
+ fun theUsbIdsOfRtl8812auAdaptersAreSupported() {
+ for ((v, p) in listOf(0x0bda to 0x8812, 0x0bda to 0x881a, 0x2357 to 0x0101, 0x2357 to 0x0103, 0x0b05 to 0x17d2, 0x7392 to 0xa822)) {
+ val chip = UsbWifi.chipOf(v, p)
+ assertNotNull("%04x:%04x".format(v, p), chip)
+ assertEquals(UsbWifi.Driver.RTL8812AU, chip!!.driver)
+ }
+ // The neighbours keep their own drivers.
+ assertEquals(UsbWifi.Driver.RTL88XXAU, UsbWifi.chipOf(0x0bda, 0x0811)!!.driver)
+ assertEquals(UsbWifi.Driver.RTL8814AU, UsbWifi.chipOf(0x0bda, 0x8813)!!.driver)
+ assertEquals(UsbWifi.Driver.RTL88X2BU, UsbWifi.chipOf(0x0bda, 0xb812)!!.driver)
+ assertNull(UsbWifi.chipOf(0x0bda, 0xc811))
+ }
+}
diff --git a/app/src/test/java/com/rfsentinel/app/usb/UsbWifiDriverTest.kt b/app/src/test/java/com/rfsentinel/app/usb/UsbWifiDriverTest.kt
index 80b9eb1..0eca163 100644
--- a/app/src/test/java/com/rfsentinel/app/usb/UsbWifiDriverTest.kt
+++ b/app/src/test/java/com/rfsentinel/app/usb/UsbWifiDriverTest.kt
@@ -377,6 +377,6 @@ class UsbWifiDriverTest {
assertEquals("RTL8812BU", UsbWifi.chipOf(0x0bda, 0xb812)?.name)
assertEquals(UsbWifi.Driver.RTL88X2BU, UsbWifi.chipOf(0x2357, 0x012d)?.driver) // TP-Link Archer T3U
assertEquals(UsbWifi.Driver.RTL88XXAU, UsbWifi.chipOf(0x0bda, 0x0811)?.driver) // ALFA AWUS036ACS
- assertNull(UsbWifi.chipOf(0x0bda, 0x8812))
+ assertEquals("RTL8812AU", UsbWifi.chipOf(0x0bda, 0x8812)?.name) // has its own driver now
}
}
diff --git a/docs/how-it-works.html b/docs/how-it-works.html
index 243db2c..7cc1a67 100644
--- a/docs/how-it-works.html
+++ b/docs/how-it-works.html
@@ -51,7 +51,7 @@
<li><b>Screen off:</b> Android suspends unfiltered scans, so a second, hardware-filtered scan runs on the identifiers that matter (vendor company IDs, Remote ID, tracker and glasses service data and UUIDs, watchlisted exact addresses).</li>
<li><b>WiFi</b>: <code>WifiManager</code> scans every 30 s by default (Android allows about 4 per 2 minutes), including raw information elements on Android 11+.</li>
<li><b>Cell</b>: <code>getAllCellInfo()</code> every 15 s for <a href="imsi-catcher-detector.html">fake-tower signs</a>.</li>
-<li><b>External</b>: OUI-Spy / GhostESP / Marauder <a href="esp32.html">ESP32 boards</a> over USB or Bluetooth (also through a Flipper Zero), FREE-WiLi 2, and USB WiFi adapters: RTL8811AU / 8821AU, RTL8812BU / 8822BU, RTL8814AU, MT7612U (2.4 + 5 GHz), RTL8187, RT3070 and, experimental, AR9271 and the WiFi 6E MT7921AU (2.4 + 5 + 6 GHz).</li>
+<li><b>External</b>: OUI-Spy / GhostESP / Marauder <a href="esp32.html">ESP32 boards</a> over USB or Bluetooth (also through a Flipper Zero), FREE-WiLi 2, and USB WiFi adapters: RTL8811AU / 8821AU, RTL8812AU (experimental), RTL8812BU / 8822BU, RTL8814AU, MT7612U (2.4 + 5 GHz), RTL8187, RT3070 and, experimental, AR9271 and the WiFi 6E MT7921AU (2.4 + 5 + 6 GHz).</li>
</ul>
<p>Nothing is ever transmitted to, connected to or paired with a scanned device.</p>
diff --git a/docs/sitemap.xml b/docs/sitemap.xml
index 0619f45..24c046d 100644
--- a/docs/sitemap.xml
+++ b/docs/sitemap.xml
@@ -5,7 +5,7 @@
<url><loc>https://cis-c0.github.io/RFSentinel/drone-detector.html</loc><lastmod>2026-10-05</lastmod><priority>0.8</priority></url>
<url><loc>https://cis-c0.github.io/RFSentinel/esp32.html</loc><lastmod>2026-10-10</lastmod><priority>0.8</priority></url>
<url><loc>https://cis-c0.github.io/RFSentinel/flock-camera-detector.html</loc><lastmod>2026-10-05</lastmod><priority>0.8</priority></url>
- <url><loc>https://cis-c0.github.io/RFSentinel/how-it-works.html</loc><lastmod>2026-10-10</lastmod><priority>0.8</priority></url>
+ <url><loc>https://cis-c0.github.io/RFSentinel/how-it-works.html</loc><lastmod>2026-10-14</lastmod><priority>0.8</priority></url>
<url><loc>https://cis-c0.github.io/RFSentinel/imsi-catcher-detector.html</loc><lastmod>2026-10-05</lastmod><priority>0.8</priority></url>
<url><loc>https://cis-c0.github.io/RFSentinel/</loc><lastmod>2026-10-13</lastmod><priority>1.0</priority></url>
<url><loc>https://cis-c0.github.io/RFSentinel/install.html</loc><lastmod>2026-10-10</lastmod><priority>0.8</priority></url>
diff --git a/site/pages/how-it-works.html b/site/pages/how-it-works.html
index 28cf03e..373ded8 100644
--- a/site/pages/how-it-works.html
+++ b/site/pages/how-it-works.html
@@ -11,7 +11,7 @@
<li><b>Screen off:</b> Android suspends unfiltered scans, so a second, hardware-filtered scan runs on the identifiers that matter (vendor company IDs, Remote ID, tracker and glasses service data and UUIDs, watchlisted exact addresses).</li>
<li><b>WiFi</b>: <code>WifiManager</code> scans every 30 s by default (Android allows about 4 per 2 minutes), including raw information elements on Android 11+.</li>
<li><b>Cell</b>: <code>getAllCellInfo()</code> every 15 s for <a href="imsi-catcher-detector.html">fake-tower signs</a>.</li>
-<li><b>External</b>: OUI-Spy / GhostESP / Marauder <a href="esp32.html">ESP32 boards</a> over USB or Bluetooth (also through a Flipper Zero), FREE-WiLi 2, and USB WiFi adapters: RTL8811AU / 8821AU, RTL8812BU / 8822BU, RTL8814AU, MT7612U (2.4 + 5 GHz), RTL8187, RT3070 and, experimental, AR9271 and the WiFi 6E MT7921AU (2.4 + 5 + 6 GHz).</li>
+<li><b>External</b>: OUI-Spy / GhostESP / Marauder <a href="esp32.html">ESP32 boards</a> over USB or Bluetooth (also through a Flipper Zero), FREE-WiLi 2, and USB WiFi adapters: RTL8811AU / 8821AU, RTL8812AU (experimental), RTL8812BU / 8822BU, RTL8814AU, MT7612U (2.4 + 5 GHz), RTL8187, RT3070 and, experimental, AR9271 and the WiFi 6E MT7921AU (2.4 + 5 + 6 GHz).</li>
</ul>
<p>Nothing is ever transmitted to, connected to or paired with a scanned device.</p>