ソースを参照

Add Realtek RTL8821AU USB WiFi (TP-Link Archer T2U Nano)

A port of the Linux rtw88 8821A/USB bring-up as a third WiFi backend, so
the wifi command, network menu, WPA2 supplicant and autoconnect work
unchanged with a plugged-in stick.  Tested through QEMU usb-host
passthrough on xHCI: 2.4 + 5 GHz scan, WPA2-PSK connect and DHCP.

- rtl8821au.c: USB probe (Linux rtw88_8821au ID list), vendor-request
  register access, eFuse, power sequences, LLT, 8051 firmware download,
  MAC/BB/AGC/RF tables with the 8821A condition rules, band/channel
  switch, eFuse TX power, software channel-hop scan, RX descriptor/RSSI
  parsing, fixed-rate TX on a single bulk-OUT endpoint (the xHCI driver
  has one bulk ring).  Delays are busy waits: SYS_WIFI runs with
  interrupts off, so the tick is frozen inside a call.
- third_party/rtw88 + mkrtwtbl.c: the Linux register tables (BSD-3-Clause)
  become firmware/rtl8821a.tbl in the pack, keeping ~11 KB out of the
  kernel; rtw8821a_fw.bin is packed from linux-firmware.
- ccmp.c: ccmp_tx_encrypt() (the chip's crypto engine stays off), with
  802.11 and RFC 3610 encrypt vectors in make test-ccmp.
- wifi_fw.c (was iwl_fw.c): backend dispatch; a plugged-in stick wins
  over the built-in Intel card.
- netdev_chip(): the network menu names the real WiFi chip.
- usb.c: six device-probe slots.
- AGENTS.md, docs: RTL8821AU section, known issue 38, size numbers.
Johannes Zellner 1 週間 前
親
コミット
3d3fef3263
26 ファイル変更、4363 行追加、201 行削除
  1. 71 8
      AGENTS.md
  2. 27 5
      Makefile
  3. 90 9
      ccmp.c
  4. 13 1
      ccmp.h
  5. 34 2
      ccmp_test.c
  6. 53 10
      docs/drivers.html
  7. 1 1
      docs/index.html
  8. 4 3
      docs/network.html
  9. 1 1
      iwl_dvm.h
  10. 0 90
      iwl_fw.c
  11. 0 24
      iwl_fw.h
  12. 2 0
      kernel.c
  13. 144 0
      mkrtwtbl.c
  14. 12 1
      netdev.c
  15. 4 0
      netdev.h
  16. 25 0
      rtl8821a_tbl.h
  17. 1784 0
      rtl8821au.c
  18. 40 0
      rtl8821au.h
  19. 7 3
      sys_net.c
  20. 30 0
      third_party/rtw88/LICENSE
  21. 1656 0
      third_party/rtw88/rtw8821a_table.inc
  22. 1 1
      usb.c
  23. 39 39
      wifi.c
  24. 11 3
      wifi.h
  25. 274 0
      wifi_fw.c
  26. 40 0
      wifi_fw.h

+ 71 - 8
AGENTS.md

@@ -107,7 +107,11 @@ make clean
   `firmware/iwlwifi-6000-4.ucode` (ThinkPad X201) are decompressed from the
   host's linux-firmware (`WIFI_FW_7265D` / `WIFI_FW_6000` override the
   search; `zstd` needed for `.zst`).  A missing file only prints a note and
-  leaves that card without firmware.
+  leaves that card without firmware.  The Realtek RTL8821AU stick needs
+  `firmware/rtw8821a_fw.bin` (linux-firmware `rtw88/`, `WIFI_FW_8821A`
+  overrides) and `firmware/rtl8821a.tbl`, which the host tool `mkrtwtbl`
+  builds from the vendored Linux tables in `third_party/rtw88/`
+  (BSD-3-Clause option of rtw88's dual license).
 - `root.img` is a second FAT12 floppy holding the disk fallback copies of the
   browser/gallery (`BROWSER`, `GALLERY`) and its `DEMO.HTM`/`JS.HTM` test
   pages plus `PIC.BMP`, and a few test files; it requires `mtools` to build.
@@ -708,8 +712,11 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   the `iwl:`/`wifi:` trail.
 - **WiFi (Intel 6200/6300, the ThinkPad X201)**: the same transport and
   `wifi.c` drive the 6000 family through `iwl_dvm.c/h` (Linux iwldvm,
-  `iwlwifi-6000-4.ucode`, API 4).  `iwl_fw.c/h` dispatches every firmware
-  call to `iwl_mvm.c` or `iwl_dvm.c` by the probed family (`iwl_is_dvm()`).
+  `iwlwifi-6000-4.ucode`, API 4).  `wifi_fw.c/h` (formerly `iwl_fw.c`)
+  dispatches every firmware call to `iwl_mvm.c`, `iwl_dvm.c` or the
+  Realtek USB driver `rtl8821au.c`; the backend is picked when the radio
+  comes up (a plugged-in RTL8821AU wins, otherwise the PCI family,
+  `iwl_is_dvm()`) and stays fixed until `wififw_down()`.
   `iwl_trans.c` keeps per-family parameters in `struct iwl_family`
   (`fam_7000`/`fam_6000`): the 6000 family has 20 scheduler queues, the
   command queue on 4 (FIFO 7), 4-byte command headers, a byte-count table in
@@ -761,6 +768,43 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   they were executing.  It runs with interrupts off, resets `sched_lock` (an AP
   may have been inside a scheduler critical section when INIT arrived) and then
   jumps; the new kernel starts the APs again from scratch.
+- **WiFi (Realtek RTL8821AU/8811AU USB, e.g. TP-Link Archer T2U Nano
+  `2357:011f`)**: `rtl8821au.c/h` ports the Linux rtw88 8821A/USB bring-up
+  (`rtw88xxa_power_on()`, `__rtw_download_firmware_legacy()`,
+  `rtw_parse_tbl_phy_cond()`, `rtw88xxa_set_channel()`) behind the same
+  entry points as `iwl_mvm.c`, so `wifi.c`, `wifi`, the netmenu and
+  autoconnect work unchanged.  The USB device probe (registered before
+  RNDIS; `USB_MAX_DEVICE_PROBES` is 6) matches the Linux `rtw88_8821au` ID
+  list, takes the bulk-IN and up to four bulk-OUT endpoints and sets the
+  configuration; nothing is powered on until the first `wifi` command.
+  Registers are vendor control requests (`bRequest 5`, `wValue` = address);
+  the 8051 firmware goes to `0x1000` in 196-byte requests per 4 KB page.
+  Bring-up: card-enable power sequence, 512-byte eFuse (MAC at logical
+  0x107, crystal cap, BB swing, RFE external LNA/PA flags, TX power base
+  indices), power off, then the full power-on: LLT (256 pages, boundary
+  248), firmware + `FW_READY` 0xC6, MAC table, page reservation, EDCA/
+  beacon/USB burst setup, BB/AGC/RF-A tables, 2.4 GHz band switch, RCR,
+  MAC address.  The register tables and power sequences (~11 KB) are read
+  from `/ramfs/firmware/rtl8821a.tbl` (layout `rtl8821a_tbl.h`, built by
+  `mkrtwtbl.c`), so the kernel only carries the code (~12 KB).  Every TX
+  queue maps to one DMA queue/bulk-OUT endpoint (LOW with 3-4 OUT
+  endpoints, NORMAL with 2), because the xHCI driver keeps one bulk-OUT
+  ring and reconfigures it on every endpoint switch; a TX transfer that
+  would be a multiple of 512 bytes gets the 8-byte `PKT_OFFSET` shift
+  instead of a zero-length packet.  RX aggregation is off; one 8 KB-aligned
+  8 KB buffer, up to 64 transfers drained per poll, RSSI from the PHY
+  status.  The chip's crypto engine is unused: `wifi.c` decrypts CCMP RX in
+  software (status 0) and the driver encrypts CCMP TX with
+  `ccmp_tx_encrypt()`.  Fixed rates (1/6 Mb/s management, 12 Mb/s data,
+  fallback off, hardware sequence numbers).  The scan is a software channel
+  hop driven by `rtl8821au_poll()` (2.4 GHz 1-13 and 36-48 active with a
+  wildcard probe, 70 ms; DFS/UNII-3 passive, 120 ms).  `SYS_WIFI` runs with
+  interrupts off (`syscall_stub.S`), so `get_ticks_ms()` is frozen inside a
+  call: every bring-up delay is a busy wait; only the dwell uses the tick.
+  USB errors, a disconnected port or a re-enumeration set `fatal`, and
+  `wifi.c` reloads on the next use.  Tested by QEMU `usb-host` passthrough
+  on xHCI (C-cut chip, firmware 42.4): 2.4 + 5 GHz scan, WPA2-PSK connect
+  and DHCP through the network menu.
 - **Mouse**: `mouse.c/h` — cursor move/draw shared by the PS/2 path
   (IntelliMouse including the 4th-byte wheel, Cirrus/Radeon hardware
   cursor) and the USB backend.  Scroll from every backend accumulates in
@@ -1849,8 +1893,9 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
    its ring-3 dependencies are compiled with function sections and linked
    with `--gc-sections`; the current full pack with the UI fonts, both
    WiFi firmwares (7265D 1,036,312 bytes, ~442 KiB gzip; 6000 454,608 bytes,
-   ~204 KiB gzip) and `install/kernel.bin` is ~1.72 MiB gzip (1,799,973
-   bytes, ~290 KB under the cap), inflating to ~3.6 MB, which adds about
+   ~204 KiB gzip), the RTL8821A firmware and tables (31,898 + 11,668
+   bytes) and `install/kernel.bin` is ~1.75 MiB gzip (1,832,893
+   bytes, ~264 KB under the cap), inflating to ~3.6 MB, which adds about
    half a second to the boot; the floppy lite pack is ~930 KiB gzip and
    deliberately omits the firmware and `install/kernel.bin`.  `libdeflate-gzip -12` is still preferred.  Over
    115200 serial a raw `cmds-dev` pack (2.6 MB) now takes about three
@@ -1912,8 +1957,8 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     (8 slots x 1 KB), and the FAT listing cache another 5 KB.  The command
     pack is out of the kernel now; after the WiFi/netmenu/install code and the
     per-connection TCP queues moved to the frame allocator, `_end` is
-    ~0x82320 (with the 6200/6300 DVM layer), ~56 KB below the `0x90000`
-    wall, and the remaining limit is kernel code/buffers only.
+    ~0x85ca0 (with the 6200/6300 DVM layer and the RTL8821AU driver),
+    ~41 KB below the `0x90000` wall, and the remaining limit is kernel code/buffers only.
 
 22. Browser limits: JavaScript runs inline and external classic scripts on
     QuickJS (ES2025) with the DOM surface described above (creation/mutation/selectors/
@@ -2190,7 +2235,8 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     throughput is CPU-bound.  The driver was written against the Linux iwlwifi
     sources without hardware in the loop; expect bring-up debugging on the
     Chromebook.  The kernel budget is tight: WiFi added ~28 KB with both
-    firmware layers, `_end` is ~0x82320 (~56 KB below the `0x90000` wall).
+    firmware layers, `_end` is ~0x85ca0 with the RTL8821AU driver (~41 KB
+    below the `0x90000` wall).
 
 36. Network menu limits (`ucmd_netmenu.c`): building the device list probes
     every driver, so opening the menu on a machine with an unconfigured USB
@@ -2225,3 +2271,20 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     intermediate state: P2 is written first, then the kernel, then the boot
     sector; there is no A/B slot or journal.  A later full `install` repairs
     it, and P4 is never touched by these updates.
+
+38. RTL8821AU USB WiFi limits (`rtl8821au.c`): only the 8821AU/8811AU (1x1)
+    rtw88 IDs; no 8812AU/8814AU/8821CU.  Bulk transfers need EHCI or xHCI
+    (UHCI/OHCI companions have no bulk path).  No IQ calibration, LC
+    calibration, thermal power tracking, DIG, BT coexistence or
+    by-rate/regulatory TX power tables: every rate uses the eFuse base
+    index of the channel group.  20 MHz legacy rates only (no HT/VHT,
+    aggregation or rate adaptation, no firmware H2C commands at all), no
+    power save, and the same open/WPA2-PSK-CCMP limits as item 35;
+    CCMP runs in software in both directions.  The xHCI driver has one
+    bulk-IN and one bulk-OUT ring, so the stick cannot stream at the same
+    time as an ASIX dongle, an RNDIS phone or a mass-storage transfer on the
+    same xHCI.  Bring-up (eFuse + tables + firmware) is a few thousand
+    control transfers with the BKL held and interrupts off, so the UI pauses
+    for a few seconds on the first `wifi` command.  A plugged-in stick
+    always wins over the built-in Intel card; there is no command to pick
+    the Intel card while it is attached.

ファイルの差分が大きいため隠しています
+ 27 - 5
Makefile


+ 90 - 9
ccmp.c

@@ -175,17 +175,69 @@ static int ccm_decrypt(const unsigned char key[16],
 	return 0;
 }
 
-int ccmp_rx_decrypt(unsigned char *f, unsigned int len, unsigned int hdr,
-		    const unsigned char key[16])
+/* RFC 3610 CCM encryption (L=2, M=8): CBC-MAC over the plaintext, then CTR
+ * in place; the 8-byte tag is written to data + clen. */
+static void ccm_encrypt(const unsigned char key[16],
+			const unsigned char nonce[13],
+			const unsigned char *aad, unsigned int alen,
+			unsigned char *data, unsigned int clen)
 {
-	unsigned char nonce[13], aad[30];
-	unsigned int clen, aad_len, fc, seq;
+	unsigned char rk[176], b0[16], x[16], a[16], s[16], blk[16];
+	unsigned int i, j, n, take;
 
-	if (hdr != 24 && hdr != 26)
-		return -1;                     /* no A4 frames in this driver */
-	if (len < hdr + 16)
-		return -1;
-	clen = len - hdr - 16;                 /* minus CCMP header and MIC */
+	aes_setkey(rk, key);
+	b0[0] = 0x59;
+	cpy(b0 + 1, nonce, 13);
+	b0[14] = (unsigned char)(clen >> 8);
+	b0[15] = (unsigned char)clen;
+	aes_encrypt(rk, b0, x);
+
+	zero(blk, 16);
+	blk[0] = (unsigned char)(alen >> 8);
+	blk[1] = (unsigned char)alen;
+	n = alen < 14 ? alen : 14;
+	cpy(blk + 2, aad, n);
+	xorb(blk, x, 16);
+	aes_encrypt(rk, blk, x);
+	for (i = n; i < alen; i += take) {
+		take = alen - i < 16 ? alen - i : 16;
+		zero(blk, 16);
+		cpy(blk, aad + i, take);
+		xorb(blk, x, 16);
+		aes_encrypt(rk, blk, x);
+	}
+
+	for (i = 0; i < clen; i += 16) {
+		take = clen - i < 16 ? clen - i : 16;
+		zero(blk, 16);
+		cpy(blk, data + i, take);
+		xorb(blk, x, 16);
+		aes_encrypt(rk, blk, x);
+
+		a[0] = 1;
+		cpy(a + 1, nonce, 13);
+		a[14] = (unsigned char)((i / 16 + 1) >> 8);
+		a[15] = (unsigned char)(i / 16 + 1);
+		aes_encrypt(rk, a, s);
+		for (j = 0; j < take; j++)
+			data[i + j] ^= s[j];
+	}
+
+	a[0] = 1;
+	cpy(a + 1, nonce, 13);
+	a[14] = 0;
+	a[15] = 0;
+	aes_encrypt(rk, a, s);
+	for (j = 0; j < 8; j++)
+		data[clen + j] = x[j] ^ s[j];
+}
+
+/* AAD and nonce of one CCMP MPDU (IEEE 802.11-2016 12.5.3.3.3/4). */
+static unsigned int ccmp_aad_nonce(const unsigned char *f, unsigned int hdr,
+				   unsigned char aad[30],
+				   unsigned char nonce[13])
+{
+	unsigned int aad_len, fc, seq;
 
 	/* AAD: FC (mute Retry/PwrMgmt/MoreData, force Protected), A1, A2,
 	 * A3, SC (sequence number zeroed, fragment kept).  The 2-byte
@@ -220,7 +272,36 @@ int ccmp_rx_decrypt(unsigned char *f, unsigned int len, unsigned int hdr,
 	nonce[10] = f[hdr + 4];                /* PN2 */
 	nonce[11] = f[hdr + 1];                /* PN1 */
 	nonce[12] = f[hdr + 0];                /* PN0 */
+	return aad_len;
+}
+
+int ccmp_rx_decrypt(unsigned char *f, unsigned int len, unsigned int hdr,
+		    const unsigned char key[16])
+{
+	unsigned char nonce[13], aad[30];
+	unsigned int clen, aad_len;
 
+	if (hdr != 24 && hdr != 26)
+		return -1;                     /* no A4 frames in this driver */
+	if (len < hdr + 16)
+		return -1;
+	clen = len - hdr - 16;                 /* minus CCMP header and MIC */
+	aad_len = ccmp_aad_nonce(f, hdr, aad, nonce);
 	return ccm_decrypt(key, nonce, aad, aad_len, f + hdr + 8, clen,
 			   f + len - 8);
 }
+
+int ccmp_tx_encrypt(unsigned char *f, unsigned int len, unsigned int hdr,
+		    const unsigned char key[16])
+{
+	unsigned char nonce[13], aad[30];
+	unsigned int aad_len;
+
+	if (hdr != 24 && hdr != 26)
+		return -1;
+	if (len < hdr + 8)
+		return -1;
+	aad_len = ccmp_aad_nonce(f, hdr, aad, nonce);
+	ccm_encrypt(key, nonce, aad, aad_len, f + hdr + 8, len - hdr - 8);
+	return 0;
+}

+ 13 - 1
ccmp.h

@@ -1,4 +1,4 @@
-/* Software CCMP (AES-CCM) RX decryption: see ccmp.c.
+/* Software CCMP (AES-CCM): see ccmp.c.
  *
  * The 7265D firmware's RX crypto path can fail to use keys that are correctly
  * installed with ADD_STA_KEY (SEC_ENC_ERR with the matching key ID), while TX
@@ -19,4 +19,16 @@
 int ccmp_rx_decrypt(unsigned char *frame, unsigned int len, unsigned int hdr,
 		    const unsigned char key[16]);
 
+/* Encrypt one ToDS CCMP MPDU in place (the RTL8821AU runs with its crypto
+ * engine off).
+ *
+ * frame  header, the filled-in 8-byte CCMP header (PN, ExtIV, key ID) and
+ *        the plaintext; the buffer must have 8 more bytes for the MIC
+ * len    header + CCMP header + plaintext length (without the MIC)
+ * hdr    802.11 header length (24 or 26)
+ *
+ * Returns 0 (the MPDU is then len + 8 bytes long) or -1. */
+int ccmp_tx_encrypt(unsigned char *frame, unsigned int len, unsigned int hdr,
+		    const unsigned char key[16]);
+
 #endif

+ 34 - 2
ccmp_test.c

@@ -1,5 +1,6 @@
-/* Host vectors for the software CCMP RX helper (ccmp.c):
- * FIPS-197 AES-128 and RFC 3610 CCM packet vector #1. */
+/* Host vectors for the software CCMP helpers (ccmp.c): FIPS-197 AES-128,
+ * RFC 3610 CCM packet vector #1 and the 802.11 CCMP MPDU vector, in both
+ * directions (RX decrypt, TX encrypt for the RTL8821AU). */
 #include <stdio.h>
 #include <string.h>
 
@@ -123,6 +124,37 @@ int main(void)
 		} else {
 			printf("ok   ccmp rx bad tag\n");
 		}
+
+		/* TX: header + CCMP header + plaintext must encrypt to the
+		 * same MPDU, MIC included. */
+		memcpy(frame, enc, 32);
+		memcpy(frame + 32, plain, sizeof(plain));
+		if (ccmp_tx_encrypt(frame, 32 + sizeof(plain), 24, tk) < 0 ||
+		    memcmp(frame, enc, sizeof(enc))) {
+			printf("FAIL ccmp tx vector\n");
+			failed = 1;
+		} else {
+			printf("ok   ccmp tx vector\n");
+		}
+	}
+
+	/* RFC 3610 vector #1 through the encrypt direction. */
+	{
+		unsigned char kk[16], nn[13], aa[8], buf[31], want[31];
+
+		unhex("c0c1c2c3c4c5c6c7c8c9cacbcccdcecf", kk, 16);
+		unhex("00000003020100a0a1a2a3a4a5", nn, 13);
+		unhex("0001020304050607", aa, 8);
+		unhex("08090a0b0c0d0e0f101112131415161718191a1b1c1d1e", buf, 23);
+		unhex("588c979a61c663d2f066d0c2c0f989806d5f6b61dac384"
+		      "17e8d12cfdf926e0", want, 31);
+		ccm_encrypt(kk, nn, aa, 8, buf, 23);
+		if (memcmp(buf, want, 31)) {
+			printf("FAIL ccm encrypt\n");
+			failed = 1;
+		} else {
+			printf("ok   ccm encrypt\n");
+		}
 	}
 
 	return failed;

+ 53 - 10
docs/drivers.html

@@ -55,7 +55,7 @@
 			<li><a href="#pci">PCI &amp; discovery</a></li>
 			<li><a href="#storage">Storage</a></li>
 			<li><a href="#usb">USB stack</a></li>
-			<li><a href="#wifi">WiFi (Intel 7265D/3165, 6200/6300)</a></li>
+			<li><a href="#wifi">WiFi (Intel 7265D/3165, 6200/6300, RTL8821AU)</a></li>
 			<li><a href="#hid">Input</a></li>
 			<li><a href="#serial">Serial</a></li>
 			<li><a href="#sensors">Sensors &amp; power</a></li>
@@ -86,6 +86,7 @@
 					<tr><td>USB</td><td><span class="file">usb.c</span> + <span class="file">uhci.c</span> <span class="file">ohci.c</span> <span class="file">ehci.c</span> <span class="file">xhci.c</span></td><td>PCI I/O ports or MMIO</td><td>Polled; hotplug watcher in the WM loop</td></tr>
 					<tr><td>Network</td><td><span class="file">e1000e.c</span> <span class="file">r8168.c</span> <span class="file">ne2000.c</span> <span class="file">asix.c</span> <span class="file">rndis.c</span></td><td>PCI or USB</td><td>Polled, one active receiver</td></tr>
 					<tr><td>WiFi</td><td><span class="file">iwl_trans.c</span> <span class="file">iwl_mvm.c</span> <span class="file">iwl_dvm.c</span> <span class="file">wifi.c</span></td><td>PCIe, Intel 7265D/3165 (<code>8086:095a/095b/3165/3166</code>) and 6200/6300 (<code>8086:4239/422c</code>, <code>4238/422b</code>)</td><td>Polled RX ring; firmware from <code>/ramfs/firmware</code>, loaded on first use</td></tr>
+					<tr><td>USB WiFi</td><td><span class="file">rtl8821au.c</span> <span class="file">wifi.c</span></td><td>USB 2.0 high speed, Realtek RTL8821AU/8811AU (TP-Link Archer T2U Nano <code>2357:011f</code> and the other Linux <code>rtw88_8821au</code> IDs)</td><td>Vendor control requests for registers, one bulk-IN/one bulk-OUT endpoint; firmware + register tables from <code>/ramfs/firmware</code></td></tr>
 					<tr><td>PS/2</td><td><span class="file">mouse.c</span>, <span class="file">io.c</span></td><td>i8042</td><td>IRQ 1 keyboard, IRQ 12 mouse; polling fallback</td></tr>
 					<tr><td>Touchpad</td><td><span class="file">i2c.c</span>, <span class="file">touchpad.c</span>, <span class="file">touchpad_gesture.c</span></td><td>LPSS DesignWare I2C, slave <code>0x15</code></td><td>Polled input register every 25 ms; software tap and two-finger scroll gestures; no GPIO interrupt driver</td></tr>
 					<tr><td>Serial</td><td><span class="file">serial.c</span></td><td>16550 UART, COM1</td><td>IRQ 4 ring buffer + polled transmitter</td></tr>
@@ -341,16 +342,19 @@
 	</section>
 
 	<section id="wifi">
-		<h2>WiFi (Intel 7265D/3165, 6200/6300)</h2>
+		<h2>WiFi (Intel 7265D/3165, 6200/6300, RTL8821AU)</h2>
 		<p>
 			Two Intel generations share one PCIe transport. The HP Chromebook's
 			Wireless-AC 7265 rev D or 3165 runs the newer <em>MVM</em> firmware
 			(<span class="file">iwl_mvm.c</span>); the ThinkPad X201's Centrino
 			Advanced-N 6200 (or Ultimate-N 6300) runs the older <em>DVM</em>
 			firmware (<span class="file">iwl_dvm.c</span>).
-			<span class="file">iwl_fw.c</span> picks the layer from the PCI
-			probe, so <span class="file">wifi.c</span> and the
-			<code>wifi</code> command are the same for both. Everything is
+			A Realtek RTL8821AU USB stick is the third backend
+			(<span class="file">rtl8821au.c</span>, below).
+			<span class="file">wifi_fw.c</span> picks the backend when the radio
+			comes up — a plugged-in stick wins over the built-in card, otherwise
+			the PCI probe decides — so <span class="file">wifi.c</span> and the
+			<code>wifi</code> command are the same for all three. Everything is
 			polled; every ring and buffer is page-allocated so nothing big lands
 			in the kernel BSS.
 		</p>
@@ -372,10 +376,10 @@
 				<text class="svg-sub" x="60" y="152">auth/assoc · 802.11 ↔ Ethernet (LLC/SNAP, CCMP header) · EAPOL queue · SYS_WIFI</text>
 
 				<rect class="svg-box" x="40" y="182" width="840" height="44" rx="9"/>
-				<text class="svg-text" x="60" y="209">iwl_mvm.c (7265D) · iwl_dvm.c (6200/6300) — firmware commands, via iwl_fw.c</text>
+				<text class="svg-text" x="60" y="209">iwl_mvm.c (7265D) · iwl_dvm.c (6200/6300) · rtl8821au.c (USB) — via wifi_fw.c</text>
 
 				<rect class="svg-box green" x="40" y="246" width="840" height="44" rx="9"/>
-				<text class="svg-text" x="60" y="273">iwl_trans.c — CSR/PRPH access, TFD + RX rings, firmware section DMA (PCIe)</text>
+				<text class="svg-text" x="60" y="273">iwl_trans.c (PCIe rings, firmware DMA) · usb.c bulk + vendor control (RTL8821AU)</text>
 
 				<path class="svg-line blue" d="M240 76v28"/>
 				<path class="svg-line blue" d="M234 98l6 7 6-7"/>
@@ -390,7 +394,8 @@
 				frames and hands the TK/GTK to the firmware, which does CCMP.
 				Sources: <span class="file">ucmd_wifi.c</span>,
 				<span class="file">wpa.c</span>, <span class="file">wifi.c</span>,
-				<span class="file">iwl_fw.c</span>, <span class="file">iwl_mvm.c</span>,
+				<span class="file">wifi_fw.c</span>, <span class="file">iwl_mvm.c</span>,
+				<span class="file">rtl8821au.c</span>,
 				<span class="file">iwl_dvm.c</span>, <span class="file">iwl_trans.c</span>.
 			</figcaption>
 		</figure>
@@ -431,6 +436,41 @@
 			<li><strong>Scan.</strong> One <code>REPLY_SCAN_CMD</code> per band: 2.4 GHz, then 5 GHz after <code>SCAN_COMPLETE_NOTIFICATION</code>. Only channels the EEPROM regulatory tables mark valid are scanned, and probes are only sent where they are marked active and not radar. A firmware assert makes the next scan or connect reload the firmware instead of failing until reboot.</li>
 			<li><strong>Keys.</strong> <code>REPLY_ADD_STA</code> in modify mode on the AP station (pairwise offset 0, group offset 1). RX frames are the same <code>RX_PHY</code> + <code>RX_MPDU</code> pair as on the 7265D, so the software CCMP fallback in <span class="file">wifi.c</span> covers both cards. The 6200 transmits on antenna B (its <code>ANT_BC</code> configuration).</li>
 		</ul>
+		<h3 id="rtl8821au">RTL8821AU USB stick</h3>
+		<p>
+			<span class="file">rtl8821au.c</span> ports the Linux
+			<code>rtw88</code> 8821A/USB bring-up (<code>rtw88xxa_power_on()</code>
+			and the helpers it calls) and implements the same entry points as
+			the Intel layers. Registers are USB vendor requests
+			(<code>bRequest 5</code>, <code>wValue</code> = address, 1/2/4 bytes);
+			the 8051 firmware is written in 196-byte requests to
+			<code>0x1000</code> page by page. The USB probe only matches the
+			VID/PID, parses the bulk endpoints and sets the configuration; the
+			chip is powered on by the first <code>wifi</code> command, which
+			takes a few seconds of control transfers.
+		</p>
+		<div class="table-wrap">
+			<table>
+				<thead>
+					<tr><th>Step</th><th>What</th><th>Linux source</th></tr>
+				</thead>
+				<tbody>
+					<tr><td>eFuse</td><td>Card-enable power sequence, 512-byte physical map → logical map; MAC at 0x107, crystal cap, BB swing, RFE (external LNA/PA) flags, TX power bases</td><td><code>rtw_parse_efuse_map</code>, <code>rtw88xxa_read_efuse</code></td></tr>
+					<tr><td>MAC</td><td>Power sequence, LLT (256 pages, boundary 248), firmware download + <code>FW_READY</code>, MAC table, page reservation, queue map, EDCA/beacon/USB burst setup</td><td><code>rtw88xxa_power_on</code>, <code>__rtw_download_firmware_legacy</code></td></tr>
+					<tr><td>PHY</td><td>BB + AGC tables, crystal cap, RF-A table, 2.4 GHz band switch; the tables' <code>if/elif/else</code> branches use the 8821A cut/RFE rules</td><td><code>rtw_parse_tbl_phy_cond</code>, <code>rtw88xxa_switch_band</code></td></tr>
+					<tr><td>Channel</td><td>Band switch (RFE pin mux, CCK on/off, basic rates), RF 0x18 channel/band, 20 MHz bandwidth, TX AGC from the eFuse base index</td><td><code>rtw88xxa_set_channel</code></td></tr>
+				</tbody>
+			</table>
+		</div>
+		<ul>
+			<li><strong>Tables outside the kernel.</strong> The MAC/BB/AGC/RF-A register tables and the power sequences (~11 KB) are taken unchanged from Linux into <code>third_party/rtw88/rtw8821a_table.inc</code> (BSD-3-Clause). The host tool <span class="file">mkrtwtbl.c</span> turns them into <code>firmware/rtl8821a.tbl</code> (layout in <span class="file">rtl8821a_tbl.h</span>), which ships in the full pack next to <code>firmware/rtw8821a_fw.bin</code> (31,898 bytes from linux-firmware). Only the driver code is in the kernel image (~12 KB).</li>
+			<li><strong>One TX endpoint.</strong> The xHCI driver keeps a single bulk-OUT ring and reconfigures it when the endpoint changes, so every TX queue (management and data) is mapped to one DMA queue — LOW with 3–4 bulk-OUT endpoints, NORMAL with 2 — and therefore one endpoint. A TX transfer that would be an exact multiple of 512 bytes is shifted by 8 bytes (<code>PKT_OFFSET</code> 1) instead of needing a zero-length packet.</li>
+			<li><strong>RX.</strong> RX aggregation is off, so a bulk-IN transfer (8 KB buffer) normally holds one packet; the parser still walks several. RSSI comes from the PHY status (CCK LNA/VGA table, OFDM gain − 110 dBm). <code>rtl8821au_poll()</code> drains up to 64 transfers per call.</li>
+			<li><strong>Scan.</strong> A software channel hop driven by the poll: 2.4 GHz 1–13 and 5 GHz 36–48 active (wildcard probe request, 70 ms), 52–64/100–140/149–165 passive (120 ms).</li>
+			<li><strong>Crypto in software.</strong> The chip's security engine is not used: <span class="file">wifi.c</span> decrypts CCMP RX with <span class="file">ccmp.c</span>, and the driver encrypts CCMP TX with <code>ccmp_tx_encrypt()</code> (802.11 and RFC 3610 vectors in <code>make test-ccmp</code>). TX uses fixed rates with fallback disabled: 1 Mb/s (2.4 GHz) / 6 Mb/s (5 GHz) management, 12 Mb/s data, hardware sequence numbers.</li>
+			<li><strong>Delays.</strong> <code>SYS_WIFI</code> runs with interrupts off, so the millisecond tick does not advance inside a call: all bring-up delays are busy waits; only the scan dwell uses the tick (between polls).</li>
+			<li><strong>Unplug.</strong> Repeated USB errors, a disconnected root port or a re-enumeration mark the device dead; <span class="file">wifi.c</span> drops to DOWN and the next <code>wifi</code> command brings it up again.</li>
+		</ul>
 	</section>
 
 	<section id="hid">
@@ -614,6 +654,7 @@
 			<li><strong>Storage serialization.</strong> A FAT/floppy operation sets <code>sys_fs_busy</code> and stalls process switching for its duration; the PIT keeps ticking.</li>
 			<li><strong>EC.</strong> The Chrome EC path uses the ACPI v2 memmap only: no MEC host-command interface, EC SCI events, charger current/voltage, charging limit or EC firmware version. The ThinkPad H8 path is limited to its page-selected battery window and has no SCI handler. Battery/AC is polled by the WM once a second.</li>
 			<li><strong>WiFi.</strong> Intel 7265 rev D/3165 (7265D firmware) and 6200/6300 (6000 firmware, written without hardware in the loop); WPA2-PSK/CCMP and open networks, no WPA3/SAE, 802.1X, TKIP or WEP. No roaming, power save, background scan, HT/VHT, aggregation or rate scaling. Group rekeys need the background <code>wifi __keep</code> helper running. RX CCMP falls back to the byte-oriented software AES-CCM path when the firmware rejects the installed keys, so it has no replay window and is CPU-bound at high RX rates.</li>
+			<li><strong>RTL8821AU.</strong> High-speed EHCI/xHCI only (no UHCI/OHCI bulk). No IQ calibration, thermal power tracking, DIG, BT coexistence or by-rate/regulatory TX power tables (the eFuse base index is used for every rate); 20 MHz legacy rates only. Only one bulk device can stream on xHCI at a time (single bulk ring), so it does not mix with an ASIX dongle or a USB stick transfer.</li>
 			<li><strong>Suspend.</strong> There is no S0ix/suspend path; package C-states and the EC sleep handshake remain firmware-managed.</li>
 		</ul>
 	</section>
@@ -633,9 +674,11 @@
 			<li><code>iwl_trans.c</code>, <code>iwl_trans.h</code> — Intel WiFi PCIe transport (7000 and 6000 families)</li>
 			<li><code>iwl_mvm.c</code>, <code>iwl_mvm.h</code> — 7265D/3165 firmware commands (MVM)</li>
 			<li><code>iwl_dvm.c</code>, <code>iwl_dvm.h</code> — 6200/6300 firmware commands (DVM)</li>
-			<li><code>iwl_fw.c</code>, <code>iwl_fw.h</code> — firmware-layer dispatch</li>
+			<li><code>wifi_fw.c</code>, <code>wifi_fw.h</code> — WiFi backend dispatch (Intel MVM/DVM, RTL8821AU)</li>
+			<li><code>rtl8821au.c</code>, <code>rtl8821au.h</code> — Realtek RTL8821AU USB WiFi</li>
+			<li><code>mkrtwtbl.c</code>, <code>rtl8821a_tbl.h</code>, <code>third_party/rtw88/</code> — RTL8821A register tables → <code>firmware/rtl8821a.tbl</code></li>
 			<li><code>wifi.c</code>, <code>wifi.h</code> — 802.11 station, <code>wifi</code> netdev, <code>SYS_WIFI</code></li>
-			<li><code>ccmp.c</code>, <code>ccmp.h</code> — software CCMP RX fallback (AES-CCM)</li>
+			<li><code>ccmp.c</code>, <code>ccmp.h</code> — software CCMP (AES-CCM): RX fallback, RTL8821AU TX</li>
 			<li><code>install.c</code> — live-image clone installer</li>
 			<li><code>usbhid.c</code>, <code>mouse.c</code>, <code>io.c</code> — input</li>
 			<li><code>i2c.c</code>, <code>i2c.h</code> — DesignWare/LPSS I2C master</li>

+ 1 - 1
docs/index.html

@@ -222,7 +222,7 @@
 			</a>
 			<a class="card" href="drivers.html#wifi">
 				<h3>WiFi</h3>
-				<p>Intel 7265D/3165 (Chromebook, MVM firmware) and 6200/6300 (ThinkPad X201, DVM firmware) on one PCIe transport, firmware from the command pack, open and WPA2-PSK/CCMP with the handshake in ring 3.</p>
+				<p>Intel 7265D/3165 (Chromebook, MVM firmware) and 6200/6300 (ThinkPad X201, DVM firmware) on one PCIe transport, plus Realtek RTL8821AU USB sticks; firmware from the command pack, open and WPA2-PSK/CCMP with the handshake in ring 3.</p>
 				<span class="card-tag">drivers.html#wifi</span>
 			</a>
 			<a class="card" href="network.html#chat">

+ 4 - 3
docs/network.html

@@ -171,7 +171,8 @@
 		<p>
 			<span class="file">wifi.c</span> is the built-in WiFi of the
 			Chromebook (Intel 7265D/3165) and of the ThinkPad X201 (Intel
-			6200/6300), see the
+			6200/6300), or a Realtek RTL8821AU USB stick when one is plugged
+			in, see the
 			<a href="drivers.html#wifi">driver page</a>). It is last in the
 			registry, so the boot probe only makes it active when nothing
 			wired is present (a later <code>wifi connect</code> can still select
@@ -215,7 +216,7 @@
 					<tr><td><span class="file">ne2000.c</span></td><td>RTL8029</td><td>ISA / QEMU PCI</td><td>Simple DP8390 path used by <code>make run-net</code>.</td></tr>
 					<tr><td><span class="file">asix.c</span></td><td>AX88772</td><td>USB bulk</td><td>Runs over the USB core; usable on EHCI/xHCI dongles.</td></tr>
 					<tr><td><span class="file">rndis.c</span></td><td>Android USB tethering, QEMU <code>usb-net</code></td><td>USB control + bulk</td><td>RNDIS INIT/QUERY/packet filter; presents the phone's tethering link as an Ethernet netdev.</td></tr>
-					<tr><td><span class="file">wifi.c</span></td><td>Intel 7265D / 3165 (Chromebook), 6200 / 6300 (X201)</td><td>PCIe (firmware)</td><td>Open and WPA2-PSK/CCMP; firmware loaded on first use; link down until <code>wifi connect</code>.</td></tr>
+					<tr><td><span class="file">wifi.c</span></td><td>Intel 7265D / 3165 (Chromebook), 6200 / 6300 (X201), Realtek RTL8821AU USB stick</td><td>PCIe (firmware) or USB</td><td>Open and WPA2-PSK/CCMP; firmware loaded on first use; link down until <code>wifi connect</code>.</td></tr>
 				</tbody>
 			</table>
 		</div>
@@ -475,7 +476,7 @@
 		<ul class="filelist">
 			<li><code>netdev.c</code>, <code>netdev.h</code> — registry</li>
 			<li><code>e1000e.c</code>, <code>r8168.c</code>, <code>ne2000.c</code>, <code>asix.c</code>, <code>rndis.c</code></li>
-			<li><code>wifi.c</code>, <code>iwl_fw.c</code>, <code>iwl_mvm.c</code>, <code>iwl_dvm.c</code>, <code>iwl_trans.c</code> — Intel 7265D/3165 and 6200/6300 WiFi</li>
+			<li><code>wifi.c</code>, <code>wifi_fw.c</code>, <code>iwl_mvm.c</code>, <code>iwl_dvm.c</code>, <code>iwl_trans.c</code>, <code>rtl8821au.c</code> — Intel 7265D/3165 and 6200/6300 WiFi, Realtek RTL8821AU USB WiFi</li>
 			<li><code>wpa.c</code>, <code>wpa_crypto.c</code>, <code>ucmd_wifi.c</code> — WPA2-PSK supplicant and the <code>wifi</code> command (<code>make test-wpa</code>)</li>
 			<li><code>ucmd_netmenu.c</code>, <code>wifi_cfg.h</code> — top-bar network menu and its staged WiFi connect request</li>
 			<li><code>net.c</code>, <code>net.h</code> — ARP cache/ICMP/UDP/IP</li>

+ 1 - 1
iwl_dvm.h

@@ -1,5 +1,5 @@
 /* Intel 6200/6300 (iwlwifi "6000" family) firmware layer ("dvm"): see
- * iwl_dvm.c.  Same entry points as iwl_mvm.h; iwl_fw.c picks one of the two
+ * iwl_dvm.c.  Same entry points as iwl_mvm.h; wifi_fw.c picks one of them
  * by the probed device family.
  *
  * One BSS RXON context: the AP station is ID 0 (IWL_AP_ID), the broadcast

+ 0 - 90
iwl_fw.c

@@ -1,90 +0,0 @@
-/* Firmware-layer dispatch: see iwl_fw.h. */
-
-#include "iwl_fw.h"
-#include "iwl_dvm.h"
-#include "iwl_mvm.h"
-#include "iwl_trans.h"
-
-int iwlfw_up(unsigned char mac[6])
-{
-	if (iwl_probe() == 0 && iwl_is_dvm())
-		return iwl_dvm_up(mac);
-	return iwl_mvm_up(mac);
-}
-
-void iwlfw_down(void)
-{
-	if (iwl_is_dvm())
-		iwl_dvm_down();
-	else
-		iwl_mvm_down();
-}
-
-const char *iwlfw_state(void)
-{
-	return iwl_is_dvm() ? iwl_dvm_state() : iwl_mvm_state();
-}
-
-int iwlfw_poll(void)
-{
-	return iwl_is_dvm() ? iwl_dvm_poll() : iwl_mvm_poll();
-}
-
-int iwlfw_scan_start(void)
-{
-	return iwl_is_dvm() ? iwl_dvm_scan_start() : iwl_mvm_scan_start();
-}
-
-int iwlfw_scan_running(void)
-{
-	return iwl_is_dvm() ? iwl_dvm_scan_running() : iwl_mvm_scan_running();
-}
-
-int iwlfw_prepare_connect(const unsigned char bssid[6], unsigned int chan,
-			  unsigned int beacon_int)
-{
-	return iwl_is_dvm() ? iwl_dvm_prepare_connect(bssid, chan, beacon_int)
-			    : iwl_mvm_prepare_connect(bssid, chan, beacon_int);
-}
-
-int iwlfw_set_assoc(unsigned short aid, unsigned int beacon_int,
-		    unsigned int dtim)
-{
-	return iwl_is_dvm() ? iwl_dvm_set_assoc(aid, beacon_int, dtim)
-			    : iwl_mvm_set_assoc(aid, beacon_int, dtim);
-}
-
-void iwlfw_disconnect(void)
-{
-	if (iwl_is_dvm())
-		iwl_dvm_disconnect();
-	else
-		iwl_mvm_disconnect();
-}
-
-int iwlfw_tx_mgmt(const unsigned char *frame, unsigned int len)
-{
-	return iwl_is_dvm() ? iwl_dvm_tx_mgmt(frame, len)
-			    : iwl_mvm_tx_mgmt(frame, len);
-}
-
-int iwlfw_tx_data(const unsigned char *frame, unsigned int len, int protect)
-{
-	return iwl_is_dvm() ? iwl_dvm_tx_data(frame, len, protect)
-			    : iwl_mvm_tx_data(frame, len, protect);
-}
-
-int iwlfw_set_key(int pairwise, int keyidx, const unsigned char key[16],
-		  const unsigned char rsc[6])
-{
-	return iwl_is_dvm() ? iwl_dvm_set_key(pairwise, keyidx, key, rsc)
-			    : iwl_mvm_set_key(pairwise, keyidx, key, rsc);
-}
-
-void iwlfw_clear_keys(void)
-{
-	if (iwl_is_dvm())
-		iwl_dvm_clear_keys();
-	else
-		iwl_mvm_clear_keys();
-}

+ 0 - 24
iwl_fw.h

@@ -1,24 +0,0 @@
-/* Firmware-layer dispatch for wifi.c: the 7000 family (7265D/3165) runs
- * iwl_mvm.c, the 6000 family (6200/6300) iwl_dvm.c.  The family comes from
- * the PCI probe (iwl_is_dvm()); every call has the iwl_mvm.h semantics. */
-#ifndef IWL_FW_H
-#define IWL_FW_H
-
-int  iwlfw_up(unsigned char mac[6]);
-void iwlfw_down(void);
-const char *iwlfw_state(void);
-int  iwlfw_poll(void);
-int  iwlfw_scan_start(void);
-int  iwlfw_scan_running(void);
-int  iwlfw_prepare_connect(const unsigned char bssid[6], unsigned int chan,
-			   unsigned int beacon_int);
-int  iwlfw_set_assoc(unsigned short aid, unsigned int beacon_int,
-		     unsigned int dtim);
-void iwlfw_disconnect(void);
-int  iwlfw_tx_mgmt(const unsigned char *frame, unsigned int len);
-int  iwlfw_tx_data(const unsigned char *frame, unsigned int len, int protect);
-int  iwlfw_set_key(int pairwise, int keyidx, const unsigned char key[16],
-		   const unsigned char rsc[6]);
-void iwlfw_clear_keys(void);
-
-#endif

+ 2 - 0
kernel.c

@@ -22,6 +22,7 @@
 #include "process.h"
 #include "ramfs.h"
 #include "rndis.h"
+#include "rtl8821au.h"
 #include "sched.h"
 #include "thermal.h"
 #include "timer.h"
@@ -461,6 +462,7 @@ void kernel_main(void)
 	 * watcher in the WM loop. */
 	asix_register();
 	usbmsc_register();
+	rtl8821au_register();
 	rndis_register();
 	usbhid_init();
 	boot_trace("usb");

+ 144 - 0
mkrtwtbl.c

@@ -0,0 +1,144 @@
+/* Host tool: turn the vendored RTL8821A tables (third_party/rtw88) into
+ * firmware/rtl8821a.tbl, which the kernel driver (rtl8821au.c) reads from
+ * the pack.  Keeping ~11 KB of register tables out of the kernel image keeps
+ * _end well below the 0x90000 wall.
+ *
+ * Format (little endian):
+ *   "RTWT", u32 version (1), u32 section count, then per section
+ *   u32 id, u32 byte length, data (already a multiple of 4 bytes).
+ *   RTW_TBL_MAC/AGC/BB/RF_A: u32 (addr, data) pairs exactly as in Linux,
+ *   including the condition words (bit 31 / bit 30 headers).
+ *   RTW_TBL_PWR_*: 8-byte commands {u16 offset, cut_mask, intf_mask, base,
+ *   cmd, mask, value}; a flow's sub-sequences are concatenated and end with
+ *   one RTW_PWR_CMD_END entry. */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "rtl8821a_tbl.h"
+
+typedef unsigned int u32;
+typedef unsigned short u16;
+typedef unsigned char u8;
+
+#define BIT(n) (1u << (n))
+
+struct rtw_pwr_seq_cmd {
+	u16 offset;
+	u8 cut_mask;
+	u8 intf_mask;
+	u8 base:4;
+	u8 cmd:4;
+	u8 mask;
+	u8 value;
+};
+
+#define RTW_PWR_ADDR_MAC	0x00
+#define RTW_PWR_ADDR_USB	0x01
+#define RTW_PWR_ADDR_PCIE	0x02
+#define RTW_PWR_ADDR_SDIO	0x03
+#define RTW_PWR_INTF_SDIO_MSK	BIT(0)
+#define RTW_PWR_INTF_USB_MSK	BIT(1)
+#define RTW_PWR_INTF_PCI_MSK	BIT(2)
+#define RTW_PWR_INTF_ALL_MSK	(BIT(0) | BIT(1) | BIT(2) | BIT(3))
+#define RTW_PWR_CUT_TEST_MSK	BIT(0)
+#define RTW_PWR_CUT_A_MSK	BIT(1)
+#define RTW_PWR_CUT_B_MSK	BIT(2)
+#define RTW_PWR_CUT_C_MSK	BIT(3)
+#define RTW_PWR_CUT_D_MSK	BIT(4)
+#define RTW_PWR_CUT_E_MSK	BIT(5)
+#define RTW_PWR_CUT_F_MSK	BIT(6)
+#define RTW_PWR_CUT_G_MSK	BIT(7)
+#define RTW_PWR_CUT_ALL_MSK	0xFF
+#define RTW_PWR_DELAY_US	RTL_PWR_DELAY_US
+#define RTW_PWR_DELAY_MS	RTL_PWR_DELAY_MS
+#define RTW_PWR_CMD_READ	RTL_PWR_CMD_READ
+#define RTW_PWR_CMD_WRITE	RTL_PWR_CMD_WRITE
+#define RTW_PWR_CMD_POLLING	RTL_PWR_CMD_POLLING
+#define RTW_PWR_CMD_DELAY	RTL_PWR_CMD_DELAY
+#define RTW_PWR_CMD_END		RTL_PWR_CMD_END
+
+#include "third_party/rtw88/rtw8821a_table.inc"
+
+#define N(a) (sizeof(a) / sizeof((a)[0]))
+
+static unsigned char out[65536];
+static unsigned int olen, nsect;
+
+static void put32(u32 v)
+{
+	if (olen + 4 > sizeof(out)) {
+		fprintf(stderr, "mkrtwtbl: output too large\n");
+		exit(1);
+	}
+	out[olen++] = v;
+	out[olen++] = v >> 8;
+	out[olen++] = v >> 16;
+	out[olen++] = v >> 24;
+}
+
+static void sect_words(u32 id, const u32 *w, unsigned int n)
+{
+	unsigned int i;
+
+	put32(id);
+	put32(n * 4);
+	for (i = 0; i < n; i++)
+		put32(w[i]);
+	nsect++;
+}
+
+static void put_cmd(const struct rtw_pwr_seq_cmd *c)
+{
+	put32(c->offset | ((u32)c->cut_mask << 16) | ((u32)c->intf_mask << 24));
+	put32(c->base | ((u32)c->cmd << 8) | ((u32)c->mask << 16) |
+	      ((u32)c->value << 24));
+}
+
+static void sect_flow(u32 id, const struct rtw_pwr_seq_cmd *const *flow)
+{
+	static const struct rtw_pwr_seq_cmd end = { 0xffff, 0, 0, 0,
+						     RTL_PWR_CMD_END, 0, 0 };
+	unsigned int n = 0, at;
+	const struct rtw_pwr_seq_cmd *c;
+
+	put32(id);
+	at = olen;
+	put32(0);
+	for (; *flow; flow++)
+		for (c = *flow; c->cmd != RTL_PWR_CMD_END; c++, n++)
+			put_cmd(c);
+	put_cmd(&end);
+	n++;
+	out[at] = (n * 8);
+	out[at + 1] = (n * 8) >> 8;
+	out[at + 2] = (n * 8) >> 16;
+	out[at + 3] = 0;
+	nsect++;
+}
+
+int main(int argc, char **argv)
+{
+	FILE *f;
+
+	if (argc != 2) {
+		fprintf(stderr, "usage: mkrtwtbl out.tbl\n");
+		return 1;
+	}
+	put32(RTL_TBL_MAGIC);
+	put32(RTL_TBL_VERSION);
+	put32(0);                       /* patched below */
+	sect_words(RTL_TBL_MAC, rtw8821a_mac, N(rtw8821a_mac));
+	sect_words(RTL_TBL_AGC, rtw8821a_agc, N(rtw8821a_agc));
+	sect_words(RTL_TBL_BB, rtw8821a_bb, N(rtw8821a_bb));
+	sect_words(RTL_TBL_RF_A, rtw8821a_rf_a, N(rtw8821a_rf_a));
+	sect_flow(RTL_TBL_PWR_ON, card_enable_flow_8821a);
+	sect_flow(RTL_TBL_PWR_OFF, card_disable_flow_8821a);
+	sect_flow(RTL_TBL_PWR_LPS, enter_lps_flow_8821a);
+	out[8] = nsect;
+	f = fopen(argv[1], "wb");
+	if (!f || fwrite(out, 1, olen, f) != olen || fclose(f) != 0) {
+		perror(argv[1]);
+		return 1;
+	}
+	return 0;
+}

+ 12 - 1
netdev.c

@@ -11,6 +11,7 @@
 #include "rndis.h"
 #include "tcp.h"
 #include "wifi.h"
+#include "wifi_fw.h"
 static const struct netdev *const netdev_drivers[] = {
 	/* e1000e is the active test NIC; the ASIX is kept as the management
 	 * link by netdev_mgmt() even while e1000e is active. */
@@ -219,13 +220,23 @@ int netdev_select(const char *name)
 	return -1;
 }
 
+const char *netdev_chip(const struct netdev *nd)
+{
+	if (nd == &wifi_netdev) {
+		const char *c = wififw_chip_name();
+
+		return c[0] != 'n' ? c : nd->chip;    /* "none" */
+	}
+	return nd->chip;
+}
+
 static void print_netdev(const char *label, const struct netdev *nd)
 {
 	io_putstr("netdev: ");
 	io_putstr(label);
 	io_putstr(nd->name);
 	io_putstr(" (");
-	io_putstr(nd->chip);
+	io_putstr(netdev_chip(nd));
 	io_putstr(")\n");
 }
 

+ 4 - 0
netdev.h

@@ -82,6 +82,10 @@ int  netdev_in_use(void);
 unsigned int netdev_owner(void);
 void netdev_release_all(void);
 
+/* Chip name for display: the WiFi netdev reports the device behind it
+ * (Intel card or RTL8821AU stick) instead of its static string. */
+const char *netdev_chip(const struct netdev *nd);
+
 /* Fill the active NIC's MAC; -1 (and a zero MAC) when there is no NIC. */
 int netdev_get_mac(unsigned char mac[6]);
 

+ 25 - 0
rtl8821a_tbl.h

@@ -0,0 +1,25 @@
+/* Shared layout of firmware/rtl8821a.tbl: written by the host tool
+ * mkrtwtbl.c, parsed by the kernel driver rtl8821au.c. */
+#ifndef RTL8821A_TBL_H
+#define RTL8821A_TBL_H
+
+#define RTL_TBL_MAGIC    0x54575452u   /* "RTWT" */
+#define RTL_TBL_VERSION  1u
+
+#define RTL_TBL_MAC      1   /* u32 pairs: 8-bit MAC register writes */
+#define RTL_TBL_AGC      2   /* u32 pairs: 32-bit BB writes */
+#define RTL_TBL_BB       3   /* u32 pairs: 32-bit BB writes + delays */
+#define RTL_TBL_RF_A     4   /* u32 pairs: RF path A writes + delays */
+#define RTL_TBL_PWR_ON   5   /* power sequence: card enable */
+#define RTL_TBL_PWR_OFF  6   /* power sequence: card disable */
+#define RTL_TBL_PWR_LPS  7   /* power sequence: enter LPS */
+
+#define RTL_PWR_CMD_READ     0
+#define RTL_PWR_CMD_WRITE    1
+#define RTL_PWR_CMD_POLLING  2
+#define RTL_PWR_CMD_DELAY    3
+#define RTL_PWR_CMD_END      4
+#define RTL_PWR_DELAY_US     0
+#define RTL_PWR_DELAY_MS     1
+
+#endif

+ 1784 - 0
rtl8821au.c

@@ -0,0 +1,1784 @@
+/* Realtek RTL8821AU USB WiFi: see rtl8821au.h.
+ *
+ * Bring-up follows Linux rtw88 (rtw88xxa_power_on() and friends) step by
+ * step; the comments name the Linux function a block comes from.  The
+ * register tables and power sequences are not in the kernel image: they are
+ * read from /ramfs/firmware/rtl8821a.tbl (built by mkrtwtbl.c from
+ * third_party/rtw88), the 8051 firmware from /ramfs/firmware/rtw8821a_fw.bin.
+ *
+ * USB: registers are vendor control requests (bRequest 5, wValue = address).
+ * Every TX queue is mapped to one DMA channel and therefore one bulk-OUT
+ * endpoint, because the xHCI driver keeps a single bulk-OUT ring and would
+ * reconfigure it on every endpoint switch.  RX aggregation is off, so one
+ * bulk-IN transfer carries one packet (the parser still walks several).
+ *
+ * Page-allocated buffers only: the kernel image must stay below 0x90000. */
+
+#include "rtl8821au.h"
+#include "rtl8821a_tbl.h"
+#include "ccmp.h"
+#include "mm.h"
+#include "pci.h"
+#include "ramfs.h"
+#include "serial.h"
+#include "timer.h"
+#include "usb.h"
+#include "wifi.h"
+
+#define FW_PATH  "firmware/rtw8821a_fw.bin"
+#define TBL_PATH "firmware/rtl8821a.tbl"
+
+/* ---- registers (Linux rtw88 reg.h) -------------------------------------- */
+#define REG_SYS_FUNC_EN       0x0002
+#define REG_APS_FSMCO         0x0004
+#define REG_SYS_CLKR          0x0008
+#define REG_RSV_CTRL          0x001C
+#define REG_RF_CTRL           0x001F
+#define REG_AFE_CTRL3         0x002C
+#define REG_EFUSE_CTRL        0x0030
+#define REG_LDO_EFUSE_CTRL    0x0034
+#define REG_ACLK_MON          0x003E
+#define REG_GPIO_MUXCFG       0x0040
+#define REG_LED_CFG           0x004C
+#define REG_WL_BT_PWR_CTRL    0x0068
+#define REG_SYS_SDIO_CTRL     0x0070
+#define REG_RF_B_CTRL         0x0076
+#define REG_LDO_SWR_CTRL      0x007C
+#define REG_MCUFW_CTRL        0x0080
+#define REG_HIMR0             0x00B0
+#define REG_HIMR1             0x00B8
+#define REG_EFUSE_ACCESS      0x00CF
+#define REG_SYS_CFG1          0x00F0
+#define REG_CR                0x0100
+#define REG_TXDMA_PQ_MAP      0x010C
+#define REG_TRXFF_BNDY        0x0114
+#define REG_HMETFR            0x01CC
+#define REG_LLT_INIT          0x01E0
+#define REG_RQPN              0x0200
+#define REG_DWBCN0_CTRL       0x0208
+#define REG_TXDMA_OFFSET_CHK  0x020C
+#define REG_RQPN_NPQ          0x0214
+#define REG_DWBCN1_CTRL       0x0228
+#define REG_RXDMA_STATUS      0x0288
+#define REG_RXDMA_MODE        0x0290
+#define REG_EARLY_MODE_CONTROL 0x02BC
+#define REG_TXPKT_EMPTY       0x041A
+#define REG_FWHW_TXQ_CTRL     0x0420
+#define REG_HWSEQ_CTRL        0x0423
+#define REG_BCNQ_BDNY         0x0424
+#define REG_MGQ_BDNY          0x0425
+#define REG_SPEC_SIFS         0x0428
+#define REG_RETRY_LIMIT       0x042A
+#define REG_RRSR              0x0440
+#define REG_ARFR0             0x0444
+#define REG_ARFRH0            0x0448
+#define REG_ARFR1_V1          0x044C
+#define REG_ARFRH1_V1         0x0450
+#define REG_CCK_CHECK         0x0454
+#define REG_AMPDU_MAX_TIME    0x0456
+#define REG_AMPDU_MAX_LENGTH  0x0458
+#define REG_WMAC_LBK_BF_HD    0x045D
+#define REG_FAST_EDCA_CTRL    0x0460
+#define REG_DATA_SC           0x0483
+#define REG_ARFR2_V1          0x048C
+#define REG_ARFRH2_V1         0x0490
+#define REG_ARFR3_V1          0x0494
+#define REG_ARFRH3_V1         0x0498
+#define REG_QUEUE_CTRL        0x04C6
+#define REG_SINGLE_AMPDU_CTRL 0x04C7
+#define REG_MAX_AGGR_NUM      0x04CA
+#define REG_BAR_MODE_CTRL     0x04CC
+#define REG_TX_RPT_TIME       0x04F0
+#define REG_EDCA_VO_PARAM     0x0500
+#define REG_EDCA_VI_PARAM     0x0504
+#define REG_EDCA_BE_PARAM     0x0508
+#define REG_EDCA_BK_PARAM     0x050C
+#define REG_BCNTCFG           0x0510
+#define REG_PIFS              0x0512
+#define REG_SIFS              0x0514
+#define REG_TBTT_PROHIBIT     0x0540
+#define REG_BCN_CTRL          0x0550
+#define REG_DRVERLYINT        0x0558
+#define REG_BCNDMATIM         0x0559
+#define REG_USTIME_TSF        0x055C
+#define REG_BCN_MAX_ERR       0x055D
+#define REG_RCR               0x0608
+#define REG_RX_PKT_LIMIT      0x060C
+#define REG_RX_DRVINFO_SZ     0x060F
+#define REG_MACID             0x0610
+#define REG_BSSID             0x0618
+#define REG_MAR               0x0620
+#define REG_USTIME_EDCA       0x0638
+#define REG_MAC_SPEC_SIFS     0x063A
+#define REG_ACKTO             0x0640
+#define REG_NAV_CTRL          0x0650
+#define REG_WMAC_TRXPTCL_CTL  0x0668
+#define REG_SEC_CMD           0x0670
+#define REG_RXFLTMAP0         0x06A0
+#define REG_RXFLTMAP1         0x06A2
+#define REG_RXFLTMAP2         0x06A4
+#define REG_AID               0x06A8
+#define REG_USB3_RXITV        0xF050
+#define REG_USB_HRPWM         0xFE58
+
+/* baseband / RF (Jaguar) */
+#define REG_RXPSEL            0x0808
+#define REG_TXPSEL            0x080C
+#define REG_L1PKTH            0x0848
+#define REG_CLKTRK            0x0860
+#define REG_ADCCLK            0x08AC
+#define REG_HSSI_READ         0x08B0
+#define REG_FPGA0_XCD_RF_PARA 0x08B4
+#define REG_ADC160            0x08C4
+#define REG_CCK_RX            0x0A04
+#define REG_3WIRE_SWA         0x0C00
+#define REG_TXSCALE_A         0x0C1C
+#define REG_TXAGC_A_CCK       0x0C20
+#define REG_RXIGI_A           0x0C50
+#define REG_TX_PWR_TRAINING_A 0x0C54
+#define REG_LSSI_WRITE_A      0x0C90
+#define REG_RFE_PINMUX_A      0x0CB0
+#define REG_RFE_INV_A         0x0CB4
+#define REG_PI_READ_A         0x0D04
+#define REG_SI_READ_A         0x0D08
+#define REG_TXSCALE_B         0x0E1C
+
+#define BIT(n)  (1u << (n))
+
+#define RCR_DEFAULT  (BIT(31) | BIT(30) | BIT(29) | BIT(20) | BIT(14) | \
+		      BIT(28) | BIT(3) | BIT(2) | BIT(1))  /* APP_FCS/MIC/ICV,
+		      PKTCTL_DLEN, HTC_LOC_CTRL, APP_PHYSTS, AB, AM, APM */
+#define RCR_CBSSID_DATA BIT(6)
+#define RCR_CBSSID_BCN  BIT(7)
+
+#define FW_READY_LEGACY 0xC6        /* RAM_DL_SEL|WINTINI_RDY|CHK_RPT|RDY */
+
+/* TX descriptor queue selects and rates (rtw88 tx.h/main.h) */
+#define QSEL_BE    0
+#define QSEL_MGMT  18
+#define RATE_1M    0x00
+#define RATE_6M    0x04
+#define RATE_12M   0x06
+#define RATEID_BG  6
+#define RATEID_G   7
+#define RATEID_B   8
+
+#define TXDESC_SZ  40
+#define RXDESC_SZ  24
+#define RX_BUF_SZ  8192
+#define TX_BUF_SZ  4096
+
+/* DMA mappings (rtw88 main.h enum rtw_dma_mapping) */
+#define DMA_LOW    1
+#define DMA_NORMAL 2
+
+/* Scan dwell times (ms). */
+#define DWELL_ACTIVE  70
+#define DWELL_PASSIVE 120
+
+static const unsigned char scan_chans[] = {
+	1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
+	36, 40, 44, 48, 52, 56, 60, 64,
+	100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140,
+	149, 153, 157, 161, 165,
+};
+
+/* Supported USB IDs: Linux rtw8821au.c. */
+static const unsigned short usb_ids[][2] = {
+	{ 0x0bda, 0x0811 }, { 0x0bda, 0x0820 }, { 0x0bda, 0x0821 },
+	{ 0x0bda, 0x8822 }, { 0x0bda, 0x0823 }, { 0x0bda, 0xa811 },
+	{ 0x0411, 0x0242 }, { 0x0411, 0x029b }, { 0x04bb, 0x0953 },
+	{ 0x056e, 0x4007 }, { 0x056e, 0x400e }, { 0x056e, 0x400f },
+	{ 0x0846, 0x9052 }, { 0x0e66, 0x0023 }, { 0x2001, 0x3314 },
+	{ 0x2001, 0x3318 }, { 0x2019, 0xab32 }, { 0x20f4, 0x804b },
+	{ 0x2357, 0x011e }, { 0x2357, 0x011f }, { 0x2357, 0x0120 },
+	{ 0x3823, 0x6249 }, { 0x7392, 0xa811 }, { 0x7392, 0xa812 },
+	{ 0x7392, 0xa813 }, { 0x7392, 0xb611 },
+};
+
+static struct {
+	/* USB */
+	int present;
+	unsigned int ctrl, addr, mps, port, gen;
+	int low;
+	unsigned int vendor, product;
+	unsigned int ep_in, ep_in_mps, ep_out[4], n_out, ep_out_mps;
+	unsigned int ep_tx;               /* the one bulk-OUT endpoint used */
+	unsigned int io_errs;
+	/* chip */
+	int up, fatal;
+	unsigned int cut;
+	unsigned char *io;                /* control-transfer data (page) */
+	unsigned char *efuse;             /* 512-byte logical map (page) */
+	unsigned char *rxbuf;             /* 8 KB, inside 16 KB of pages */
+	unsigned char *txbuf;             /* one page */
+	const unsigned char *tbl;
+	unsigned int tbl_size;
+	unsigned char mac[6];
+	unsigned int rfe_cond;            /* rtw_phy_setup_phy_cond() rfe */
+	unsigned int bb_swing_2g, bb_swing_5g, crystal;
+	unsigned int fw_ver, fw_sub;
+	char fw_name[24];
+	const char *state;
+	/* radio */
+	unsigned int chan;
+	int band5;
+	/* scan */
+	int scanning;
+	unsigned int scan_idx, scan_next;
+	/* link */
+	unsigned char bssid[6];
+	unsigned char tk[16];
+	int have_tk;
+	unsigned int rx_pkts, rx_crc, tx_pkts, tx_errs;
+	int last_rssi;
+} R;
+
+/* ---- small helpers ------------------------------------------------------- */
+static void rmemcpy(void *d, const void *s, unsigned int n)
+{
+	unsigned char *dd = d;
+	const unsigned char *ss = s;
+
+	while (n--)
+		*dd++ = *ss++;
+}
+
+static void rmemzero(void *d, unsigned int n)
+{
+	unsigned char *dd = d;
+
+	while (n--)
+		*dd++ = 0;
+}
+
+static void udelay(unsigned int us)
+{
+	while (us--)
+		port_outb(0x80, 0);
+}
+
+/* Busy-wait: SYS_WIFI runs with interrupts off (syscall_stub.S), so the
+ * millisecond tick does not advance inside a call. */
+static void mdelay(unsigned int ms)
+{
+	while (ms--)
+		udelay(1000);
+}
+
+static void rlog(const char *s)
+{
+	serial_puts(SERIAL_COM1, s);
+}
+
+static void loghex(const char *s, unsigned int v)
+{
+	serial_puts(SERIAL_COM1, s);
+	serial_puthex(SERIAL_COM1, v);
+	serial_puts(SERIAL_COM1, "\n");
+}
+
+static unsigned int le32(const unsigned char *p)
+{
+	return p[0] | (p[1] << 8) | (p[2] << 16) | ((unsigned int)p[3] << 24);
+}
+
+static void put32(unsigned char *p, unsigned int v)
+{
+	p[0] = v; p[1] = v >> 8; p[2] = v >> 16; p[3] = v >> 24;
+}
+
+static unsigned int ffs32(unsigned int m)
+{
+	unsigned int s = 0;
+
+	while (m && !(m & 1)) {
+		m >>= 1;
+		s++;
+	}
+	return s;
+}
+
+/* ---- register access (rtw_usb_read/rtw_usb_write) ------------------------ */
+static void io_fail(void)
+{
+	if (++R.io_errs > 16 && !R.fatal) {
+		rlog("rtl8821au: too many USB errors, device gone?\n");
+		R.fatal = 1;
+	}
+}
+
+static unsigned int rd(unsigned int a, unsigned int len)
+{
+	int n;
+
+	if (R.fatal)
+		return 0xffffffffu;
+	R.io[0] = R.io[1] = R.io[2] = R.io[3] = 0;
+	n = usb_control_in(R.ctrl, R.addr, R.mps, R.low, 0xC0, 0x05,
+			   (unsigned short)a, 0, R.io, len);
+	if (n != (int)len) {
+		io_fail();
+		return 0xffffffffu;
+	}
+	R.io_errs = 0;
+	return le32(R.io);
+}
+
+static void wr(unsigned int a, unsigned int v, unsigned int len)
+{
+	int n;
+
+	if (R.fatal)
+		return;
+	put32(R.io, v);
+	n = usb_control_out_data(R.ctrl, R.addr, R.mps, R.low, 0x40, 0x05,
+				 (unsigned short)a, 0, R.io, len);
+	if (n < 0)
+		io_fail();
+	else
+		R.io_errs = 0;
+}
+
+static unsigned int rd8(unsigned int a)  { return rd(a, 1) & 0xff; }
+static unsigned int rd16(unsigned int a) { return rd(a, 2) & 0xffff; }
+static unsigned int rd32(unsigned int a) { return rd(a, 4); }
+static void wr8(unsigned int a, unsigned int v)  { wr(a, v & 0xff, 1); }
+static void wr16(unsigned int a, unsigned int v) { wr(a, v & 0xffff, 2); }
+static void wr32(unsigned int a, unsigned int v) { wr(a, v, 4); }
+
+static void set8(unsigned int a, unsigned int b)  { wr8(a, rd8(a) | b); }
+static void clr8(unsigned int a, unsigned int b)  { wr8(a, rd8(a) & ~b); }
+static void set16(unsigned int a, unsigned int b) { wr16(a, rd16(a) | b); }
+static void clr16(unsigned int a, unsigned int b) { wr16(a, rd16(a) & ~b); }
+static void set32(unsigned int a, unsigned int b) { wr32(a, rd32(a) | b); }
+
+static void wr32m(unsigned int a, unsigned int mask, unsigned int v)
+{
+	unsigned int s = ffs32(mask);
+
+	wr32(a, (rd32(a) & ~mask) | ((v << s) & mask));
+}
+
+static unsigned int rd32m(unsigned int a, unsigned int mask)
+{
+	return (rd32(a) & mask) >> ffs32(mask);
+}
+
+/* Bulk write of a block (firmware download), 196 bytes per request. */
+static int wr_block(unsigned int a, const unsigned char *p, unsigned int n)
+{
+	if (R.fatal)
+		return -1;
+	if (usb_control_out_data(R.ctrl, R.addr, R.mps, R.low, 0x40, 0x05,
+				 (unsigned short)a, 0, p, n) < 0) {
+		io_fail();
+		return -1;
+	}
+	return 0;
+}
+
+/* ---- RF access (rtw88xxa_phy_read_rf / rtw_phy_write_rf_reg_sipi) ------ */
+#define RFREG_MASK 0xfffffu
+
+static unsigned int rf_read(unsigned int addr, unsigned int mask)
+{
+	unsigned int pi;
+
+	addr &= 0xff;
+	pi = rd32m(REG_3WIRE_SWA, 0x4);
+	wr32m(REG_HSSI_READ, 0xff, addr);
+	udelay(20);
+	return rd32m(pi ? REG_PI_READ_A : REG_SI_READ_A, mask);
+}
+
+static void rf_write(unsigned int addr, unsigned int mask, unsigned int data)
+{
+	addr &= 0xff;
+	mask &= RFREG_MASK;
+	if (mask != RFREG_MASK)
+		data = (rf_read(addr, RFREG_MASK) & ~mask) |
+		       ((data << ffs32(mask)) & mask);
+	wr32(REG_LSSI_WRITE_A, ((addr << 20) | (data & 0xfffff)) & 0x0fffffff);
+	udelay(13);
+}
+
+/* ---- table blob ---------------------------------------------------------- */
+static const unsigned char *tbl_section(unsigned int id, unsigned int *len)
+{
+	unsigned int off = 12, n, i, sid, slen;
+
+	if (!R.tbl)
+		return 0;
+	n = le32(R.tbl + 8);
+	for (i = 0; i < n && off + 8 <= R.tbl_size; i++) {
+		sid = le32(R.tbl + off);
+		slen = le32(R.tbl + off + 4);
+		if (off + 8 + slen > R.tbl_size)
+			return 0;
+		if (sid == id) {
+			*len = slen;
+			return R.tbl + off + 8;
+		}
+		off += 8 + slen;
+	}
+	return 0;
+}
+
+static int load_blobs(void)
+{
+	unsigned int size;
+	const unsigned char *t = ramfs_lookup(TBL_PATH, &size);
+
+	if (!t || size < 12 || le32(t) != RTL_TBL_MAGIC ||
+	    le32(t + 4) != RTL_TBL_VERSION) {
+		R.state = "missing /ramfs/" TBL_PATH;
+		return -1;
+	}
+	R.tbl = t;
+	R.tbl_size = size;
+	return 0;
+}
+
+/* rtw_pwr_seq_parser(): USB interface mask, cut mask from the chip. */
+static int pwr_seq(unsigned int id)
+{
+	unsigned int len, i, cut_mask = 1u << (R.cut + 1);
+	const unsigned char *p = tbl_section(id, &len);
+
+	if (!p)
+		return -1;
+	for (i = 0; i + 8 <= len; i += 8) {
+		unsigned int off = p[i] | (p[i + 1] << 8);
+		unsigned int cm = p[i + 2], im = p[i + 3], cmd = p[i + 5];
+		unsigned int mask = p[i + 6], val = p[i + 7], t;
+
+		if (cmd == RTL_PWR_CMD_END)
+			break;
+		if (!(im & 0x02) || !(cm & cut_mask))
+			continue;
+		switch (cmd) {
+		case RTL_PWR_CMD_WRITE:
+			wr8(off, (rd8(off) & ~mask) | (val & mask));
+			break;
+		case RTL_PWR_CMD_POLLING:
+			for (t = 0; t < 5000; t++) {
+				if ((rd8(off) & mask) == (val & mask))
+					break;
+				udelay(50);
+			}
+			if (t == 5000) {
+				loghex("rtl8821au: power poll timeout at ", off);
+				return -1;
+			}
+			break;
+		case RTL_PWR_CMD_DELAY:
+			if (val == RTL_PWR_DELAY_US)
+				udelay(off);
+			else
+				mdelay(off);
+			break;
+		default:
+			break;
+		}
+		if (R.fatal)
+			return -1;
+	}
+	return 0;
+}
+
+/* rtw_parse_tbl_phy_cond() + check_positive() for the 8812A/8821A rules. */
+static int cond_ok(unsigned int c, unsigned int c2)
+{
+	unsigned int cut = (c >> 24) & 0xf, pkg = (c >> 12) & 0xf;
+	unsigned int intf = (c >> 8) & 0xf, rfe = c & 0xff;
+	unsigned int drv_cut = R.cut ? R.cut : 15;
+
+	(void)c2;      /* 8821A: glna/gpa/alna/apa types are all 0 */
+	if (cut && cut != drv_cut)
+		return 0;
+	if (pkg && pkg != 15)
+		return 0;
+	if (intf && intf != 0x2)          /* INTF_USB */
+		return 0;
+	if (!(rfe & 0x0f))
+		return 1;
+	if ((rfe & R.rfe_cond) != rfe)
+		return 0;
+	/* Any type check against the all-zero driver types fails unless the
+	 * table also asks for type 0. */
+	if ((rfe & 1) && (c2 & 0xff))
+		return 0;
+	if ((rfe & 2) && ((c2 >> 8) & 0xff))
+		return 0;
+	if ((rfe & 4) && ((c2 >> 16) & 0xff))
+		return 0;
+	if ((rfe & 8) && ((c2 >> 24) & 0xff))
+		return 0;
+	return 1;
+}
+
+static void do_cfg(unsigned int id, unsigned int a, unsigned int v)
+{
+	switch (id) {
+	case RTL_TBL_MAC:
+		wr8(a, v);
+		break;
+	case RTL_TBL_AGC:
+		wr32(a, v);
+		break;
+	case RTL_TBL_BB:
+		if (a == 0xfe)
+			mdelay(50);
+		else if (a == 0xfd)
+			mdelay(5);
+		else if (a == 0xfc)
+			mdelay(1);
+		else if (a == 0xfb)
+			udelay(60);
+		else if (a == 0xfa)
+			udelay(5);
+		else if (a == 0xf9)
+			udelay(1);
+		else
+			wr32(a, v);
+		break;
+	case RTL_TBL_RF_A:
+		if (a == 0xffe)
+			mdelay(50);
+		else if (a == 0xfe)
+			udelay(110);
+		else {
+			rf_write(a, RFREG_MASK, v);
+			udelay(1);
+		}
+		break;
+	}
+}
+
+static int load_table(unsigned int id)
+{
+	unsigned int len, i, pos_c = 0, pos_c2 = 0;
+	int matched = 1, skipped = 0;
+	const unsigned char *p = tbl_section(id, &len);
+
+	if (!p)
+		return -1;
+	for (i = 0; i + 8 <= len; i += 8) {
+		unsigned int a = le32(p + i), v = le32(p + i + 4);
+
+		if (a & BIT(31)) {                    /* pos: branch marker */
+			switch ((a >> 28) & 3) {
+			case 3:                       /* ENDIF */
+				matched = 1;
+				skipped = 0;
+				break;
+			case 2:                       /* ELSE */
+				matched = !skipped;
+				break;
+			default:                      /* IF / ELIF */
+				pos_c = a;
+				pos_c2 = v;
+				break;
+			}
+		} else if (a & BIT(30)) {             /* neg: evaluate */
+			if (!skipped) {
+				if (cond_ok(pos_c, pos_c2)) {
+					matched = 1;
+					skipped = 1;
+				} else {
+					matched = 0;
+					skipped = 0;
+				}
+			} else {
+				matched = 0;
+			}
+		} else if (matched) {
+			do_cfg(id, a, v);
+		}
+		if (R.fatal)
+			return -1;
+	}
+	return 0;
+}
+
+/* ---- eFuse (rtw_dump_physical_efuse_map + logical map) ------------------ */
+static int read_efuse(void)
+{
+	unsigned char *phy = R.rxbuf;       /* scratch: 512 bytes */
+	unsigned char *log_map = R.efuse;
+	unsigned int addr, ctl, t, p, i;
+
+	wr8(REG_EFUSE_ACCESS, 0x69);
+	set16(REG_SYS_FUNC_EN, BIT(12));
+	set16(REG_SYS_CLKR, BIT(5) | BIT(1));
+	wr32m(REG_LDO_EFUSE_CTRL, BIT(8) | BIT(9), 0);
+	ctl = rd32(REG_EFUSE_CTRL);
+	for (addr = 0; addr < 512; addr++) {
+		ctl &= ~(0xffu | (0x3ffu << 8));
+		ctl |= addr << 8;
+		wr32(REG_EFUSE_CTRL, ctl & ~BIT(31));
+		for (t = 0; t < 1000; t++) {
+			ctl = rd32(REG_EFUSE_CTRL);
+			if (ctl & BIT(31))
+				break;
+			udelay(1);
+		}
+		if (t == 1000 || R.fatal) {
+			wr8(REG_EFUSE_ACCESS, 0);
+			return -1;
+		}
+		phy[addr] = ctl & 0xff;
+	}
+	wr8(REG_EFUSE_ACCESS, 0);
+
+	for (i = 0; i < 512; i++)
+		log_map[i] = 0xff;
+	for (p = 0; p < 512;) {
+		unsigned int h1 = phy[p], h2 = p + 1 < 512 ? phy[p + 1] : 0xff;
+		unsigned int blk, wen, w;
+
+		if (h1 == 0xff || ((h1 & 0x1f) == 0xf && h2 == 0xff))
+			break;
+		if ((h1 & 0x1f) == 0xf) {
+			blk = ((h2 & 0xf0) >> 1) | ((h1 >> 5) & 7);
+			wen = h2 & 0xf;
+			p += 2;
+		} else {
+			blk = (h1 & 0xf0) >> 4;
+			wen = h1 & 0xf;
+			p += 1;
+		}
+		for (w = 0; w < 4; w++) {
+			unsigned int li = (blk << 3) + (w << 1);
+
+			if (wen & (1u << w))
+				continue;
+			if (p + 1 >= 512 || li + 1 >= 512)
+				return -1;
+			log_map[li] = phy[p];
+			log_map[li + 1] = phy[p + 1];
+			p += 2;
+		}
+	}
+	return 0;
+}
+
+/* rtw88xxa_read_efuse() + rtw_chip_efuse_info_setup() fixups. */
+static void parse_efuse(void)
+{
+	const unsigned char *m = R.efuse;
+	unsigned int pa = m[0xbc], lna2 = m[0xbd], lna5 = m[0xbf];
+	unsigned int ext_lna_2g, ext_pa_2g, ext_lna_5g, ext_pa_5g, btcoex;
+
+	rmemcpy(R.mac, m + 0x107, 6);
+	R.crystal = m[0xb9] == 0xff ? 0x20 : m[0xb9];
+	R.bb_swing_2g = m[0xc6] == 0xff ? 0 : m[0xc6];
+	R.bb_swing_5g = m[0xc7] == 0xff ? 0 : m[0xc7];
+	if (pa == 0xff)
+		pa = 0;
+	if (lna2 == 0xff)
+		lna2 = 0;
+	if (lna5 == 0xff)
+		lna5 = 0;
+	ext_pa_2g = (pa & BIT(4)) ? 1 : 0;
+	ext_lna_2g = (lna2 & BIT(3)) ? 1 : 0;
+	ext_pa_5g = (pa & BIT(0)) ? 1 : 0;
+	ext_lna_5g = (lna5 & BIT(3)) ? 1 : 0;
+	btcoex = m[0xc1] != 0xff && (m[0xc1] & 0xe0) == 0x20;
+	R.rfe_cond = ext_lna_2g | (ext_pa_2g << 1) | (ext_lna_5g << 2) |
+		     (ext_pa_5g << 3) | (btcoex << 4);
+	if (R.mac[0] & 1 || (R.mac[0] | R.mac[1] | R.mac[2] | R.mac[3] |
+			     R.mac[4] | R.mac[5]) == 0) {
+		/* No valid address programmed: a fixed locally administered
+		 * one, so the device is still usable. */
+		static const unsigned char fallback[6] = {
+			0x02, 0x4e, 0x45, 0x42, 0x08, 0x21 };
+
+		rmemcpy(R.mac, fallback, 6);
+	}
+}
+
+/* ---- power / MAC init ---------------------------------------------------- */
+/* rtw88xxa_reset_8051() */
+static void reset_8051(void)
+{
+	unsigned int v;
+
+	clr8(REG_RSV_CTRL, BIT(1));
+	clr8(REG_RSV_CTRL + 1, BIT(0));
+	v = rd8(REG_SYS_FUNC_EN + 1);
+	wr8(REG_SYS_FUNC_EN + 1, v & ~BIT(2));
+	clr8(REG_RSV_CTRL, BIT(1));
+	set8(REG_RSV_CTRL + 1, BIT(0));
+	wr8(REG_SYS_FUNC_EN + 1, v | BIT(2));
+}
+
+/* rtw88xxau_hw_reset(): undo a previous run that was not shut down. */
+static void hw_reset(void)
+{
+	unsigned int v, t;
+
+	if (!(rd8(REG_MCUFW_CTRL) & BIT(7)))
+		return;
+	reset_8051();
+	wr8(REG_MCUFW_CTRL, 0);
+	set32(REG_FPGA0_XCD_RF_PARA, BIT(6));
+	clr8(REG_SYS_FUNC_EN, BIT(0) | BIT(1));
+	wr8(REG_RF_CTRL, 0);
+	wr16(REG_CR, 0);
+	set8(REG_APS_FSMCO + 1, BIT(1));
+	for (t = 0; t < 5000; t++) {
+		v = rd8(REG_APS_FSMCO + 1);
+		if (!(v & BIT(1)))
+			break;
+		udelay(1);
+	}
+	wr8(REG_APS_FSMCO + 1, v | BIT(0));
+	clr8(REG_SYS_FUNC_EN + 1, BIT(4) | BIT(7));
+	set8(REG_SYS_FUNC_EN + 1, BIT(4) | BIT(7));
+}
+
+static int llt_write(unsigned int a, unsigned int d)
+{
+	unsigned int n;
+
+	wr32(REG_LLT_INIT, BIT(30) | (a << 8) | d);
+	for (n = 0; n <= 20; n++)
+		if (!(rd32(REG_LLT_INIT) & (BIT(31) | BIT(30))))
+			return 0;
+	return -1;
+}
+
+/* rtw88xxa_llt_init(): reserved pages at the top of the 256-page TX FIFO. */
+static int llt_init(unsigned int boundary)
+{
+	unsigned int i;
+
+	for (i = 0; i < boundary - 1; i++)
+		if (llt_write(i, i + 1) < 0)
+			return -1;
+	if (llt_write(boundary - 1, 0xff) < 0)
+		return -1;
+	for (i = boundary; i < 255; i++)
+		if (llt_write(i, i + 1) < 0)
+			return -1;
+	return llt_write(255, boundary);
+}
+
+static void cpu_enable(int on)
+{
+	if (on) {
+		set8(REG_RSV_CTRL + 1, BIT(0));
+		set8(REG_SYS_FUNC_EN + 1, BIT(2));
+	} else {
+		clr8(REG_SYS_FUNC_EN + 1, BIT(2));
+		clr8(REG_RSV_CTRL + 1, BIT(0));
+	}
+}
+
+/* __rtw_download_firmware_legacy() with rtw_usb_write_firmware_page(). */
+static int download_fw(void)
+{
+	unsigned int size, page, off, n, t, v;
+	const unsigned char *fw = ramfs_lookup(FW_PATH, &size);
+
+	if (!fw || size < 64) {
+		R.state = "missing /ramfs/" FW_PATH;
+		return -1;
+	}
+	R.fw_ver = fw[4] | (fw[5] << 8);
+	R.fw_sub = fw[6];
+	fw += 32;                                 /* rtw_fw_hdr_legacy */
+	size -= 32;
+
+	cpu_enable(0);
+	cpu_enable(1);
+	set8(REG_MCUFW_CTRL, BIT(0));             /* MCUFWDL_EN */
+	for (t = 0; t < 10 && !(rd8(REG_MCUFW_CTRL) & BIT(0)); t++) {
+		set8(REG_MCUFW_CTRL, BIT(0));
+		mdelay(20);
+	}
+	wr32(REG_MCUFW_CTRL, rd32(REG_MCUFW_CTRL) & ~BIT(19));  /* ROM_DLEN */
+	set8(REG_MCUFW_CTRL, BIT(2));             /* clear FWDL_CHK_RPT */
+
+	for (page = 0; page * 4096 < size; page++) {
+		unsigned int psz = size - page * 4096;
+
+		if (psz > 4096)
+			psz = 4096;
+		wr32m(REG_MCUFW_CTRL, 0x70000, page);
+		for (off = 0; off < psz; off += n) {
+			n = psz - off >= 196 ? 196 : (psz - off >= 8 ? 8 : 1);
+			if (wr_block(0x1000 + off, fw + page * 4096 + off,
+				     n) < 0)
+				return -1;
+		}
+	}
+	for (t = 0; t < 1000; t++) {
+		if (rd8(REG_MCUFW_CTRL) & BIT(2))
+			break;
+		udelay(50);
+	}
+	clr8(REG_MCUFW_CTRL, BIT(0));
+	if (t == 1000) {
+		R.state = "firmware download not acknowledged";
+		return -1;
+	}
+
+	/* download_firmware_validate_legacy() */
+	v = rd32(REG_MCUFW_CTRL);
+	v |= BIT(1);
+	v &= ~BIT(6);
+	wr32(REG_MCUFW_CTRL, v);
+	cpu_enable(0);
+	cpu_enable(1);
+	for (t = 0; t < 20; t++) {
+		if ((rd32(REG_MCUFW_CTRL) & FW_READY_LEGACY) == FW_READY_LEGACY)
+			return 0;
+		mdelay(20);
+	}
+	R.state = "firmware did not start";
+	return -1;
+}
+
+/* ---- channel / band (rtw88xxa_set_channel and helpers) ------------------- */
+/* rtw88xxa_get_bb_swing(): two bits per path in the eFuse byte. */
+static unsigned int bb_swing(int band5, int path_b)
+{
+	static const unsigned int s2s[4] = { 0x200, 0x16a, 0x101, 0x0b6 };
+	unsigned int v = band5 ? R.bb_swing_5g : R.bb_swing_2g;
+
+	return s2s[(path_b ? v >> 2 : v) & 3];
+}
+
+static void switch_band(int band5)
+{
+	unsigned int t;
+
+	/* rtw8821a_set_ext_band_switch(): no BT coex, no antenna diversity */
+	wr32m(REG_LED_CFG, BIT(23), 0);
+	wr32m(REG_LED_CFG, BIT(24), 1);
+	wr32m(REG_RFE_INV_A, 0xf, 7);
+	wr32m(REG_RFE_INV_A, 0xf0, 7);
+	wr32m(REG_RFE_INV_A, BIT(29) | BIT(28), band5 ? 2 : 1);
+
+	if (!band5) {
+		set32(REG_RXPSEL, BIT(28) | BIT(29));
+		/* rtw8821a_phy_set_rfe_reg_24g() */
+		wr32m(REG_RFE_PINMUX_A, 0xf000, 0x7);
+		wr32m(REG_RFE_PINMUX_A, 0xf0, 0x7);
+		if (R.rfe_cond & 1) {
+			wr32m(REG_RFE_INV_A, BIT(20), 1);
+			wr32m(REG_RFE_INV_A, BIT(22), 0);
+			wr32m(REG_RFE_PINMUX_A, 0x7, 0x2);
+			wr32m(REG_RFE_PINMUX_A, 0x700, 0x2);
+		} else {
+			wr32m(REG_RFE_INV_A, BIT(20), 0);
+			wr32m(REG_RFE_INV_A, BIT(22), 0);
+			wr32m(REG_RFE_PINMUX_A, 0x7, 0x7);
+			wr32m(REG_RFE_PINMUX_A, 0x700, 0x7);
+		}
+		wr32m(REG_TXSCALE_A, 0xf00, 0);
+		wr32m(REG_TXPSEL, 0xf0, 1);
+		wr32m(REG_CCK_RX, 0x0f000000, 1);
+		wr32m(REG_RRSR, 0xfffff, 0x15f);      /* 1/2/5.5/11/6/12/24 */
+		clr8(REG_CCK_CHECK, BIT(7));
+	} else {
+		/* rtw8821a_phy_set_rfe_reg_5g() */
+		wr32m(REG_RFE_PINMUX_A, 0xf000, 0x5);
+		wr32m(REG_RFE_PINMUX_A, 0xf0, 0x4);
+		wr32m(REG_RFE_INV_A, BIT(20), 0);
+		wr32m(REG_RFE_INV_A, BIT(22), 0);
+		wr32m(REG_RFE_PINMUX_A, 0x7, 0x7);
+		wr32m(REG_RFE_PINMUX_A, 0x700, 0x7);
+		set8(REG_CCK_CHECK, BIT(7));
+		for (t = 0; t < 50; t++) {
+			if ((rd16(REG_TXPKT_EMPTY) & 0x30) == 0x30)
+				break;
+			udelay(50);
+		}
+		set32(REG_RXPSEL, BIT(28) | BIT(29));
+		wr32m(REG_TXSCALE_A, 0xf00, 1);
+		wr32m(REG_TXPSEL, 0xf0, 0);
+		wr32m(REG_CCK_RX, 0x0f000000, 0xf);
+		wr32m(REG_RRSR, 0xfffff, 0x150);      /* 6/12/24 */
+	}
+	/* rtw88xxa_set_channel_bb_swing() */
+	wr32m(REG_TXSCALE_A, 0xffe00000u, bb_swing(band5, 0));
+	wr32m(REG_TXSCALE_B, 0xffe00000u, bb_swing(band5, 1));
+	R.band5 = band5;
+}
+
+/* Channel group of the eFuse power tables (rtw_get_channel_group). */
+static unsigned int chan_group(unsigned int ch)
+{
+	if (ch <= 14)
+		return ch <= 2 ? 0 : ch <= 5 ? 1 : ch <= 8 ? 2 :
+		       ch <= 11 ? 3 : ch <= 13 ? 4 : 5;
+	if (ch <= 42) return 0;
+	if (ch <= 50) return 1;
+	if (ch <= 58) return 2;
+	if (ch <= 64) return 3;
+	if (ch <= 106) return 4;
+	if (ch <= 114) return 5;
+	if (ch <= 122) return 6;
+	if (ch <= 130) return 7;
+	if (ch <= 138) return 8;
+	if (ch <= 144) return 9;
+	if (ch <= 155) return 10;
+	if (ch <= 161) return 11;
+	if (ch <= 171) return 12;
+	return 13;
+}
+
+static int s4(unsigned int nib)
+{
+	nib &= 0xf;
+	return nib & 8 ? (int)nib - 16 : (int)nib;
+}
+
+static unsigned int clamp_idx(int v)
+{
+	return v < 0 ? 0 : v > 0x3f ? 0x3f : (unsigned int)v;
+}
+
+/* TX power: the path-A eFuse base indices (rtw_phy_get_2g/5g_tx_power_index
+ * without the by-rate and regulatory-limit tables), written to the TX AGC
+ * registers for CCK, OFDM and HT MCS0-7. */
+static void set_tx_power(unsigned int ch)
+{
+	const unsigned char *p = R.efuse + 0x10;  /* txpwr_idx_table[A] */
+	unsigned int g = chan_group(ch), cck, ofdm, ht, v;
+	int base, train, i;
+	unsigned int word;
+
+	if (ch <= 14) {
+		cck = p[g < 6 ? g : 5];
+		base = p[6 + (g < 5 ? g : 4)];
+		if (cck == 0xff)
+			cck = 0x20;
+		if (base == 0xff) {
+			base = 0x20;
+			ofdm = ht = 0x20;
+		} else {
+			ofdm = clamp_idx(base + s4(p[11]));
+			ht = clamp_idx(base + s4(p[11] >> 4));
+		}
+	} else {
+		base = p[18 + g];
+		cck = 0;
+		if (base == 0xff) {
+			ofdm = ht = 0x20;
+		} else {
+			ofdm = clamp_idx(base + s4(p[32]));
+			ht = clamp_idx(base + s4(p[32] >> 4));
+		}
+	}
+	cck = clamp_idx((int)cck);
+	if (ch <= 14)
+		wr32(REG_TXAGC_A_CCK, cck * 0x01010101u);
+	v = ofdm * 0x01010101u;
+	wr32(REG_TXAGC_A_CCK + 0x4, v);
+	wr32(REG_TXAGC_A_CCK + 0x8, v);
+	v = ht * 0x01010101u;
+	wr32(REG_TXAGC_A_CCK + 0xc, v);
+	wr32(REG_TXAGC_A_CCK + 0x10, v);
+	/* rtw88xxa_tx_power_training() */
+	word = 0;
+	train = (int)ht;
+	for (i = 0; i < 3; i++) {
+		train -= i == 0 ? 10 : i == 1 ? 8 : 6;
+		word |= (unsigned int)(train < 2 ? 2 : train) << (i * 8);
+	}
+	wr32m(REG_TX_PWR_TRAINING_A, 0xffffff, word);
+}
+
+static void set_channel(unsigned int ch)
+{
+	unsigned int fc, mod;
+	int band5 = ch > 14;
+
+	if (ch == R.chan)
+		return;
+	if (band5 != R.band5 || !R.chan)
+		switch_band(band5);
+
+	/* rtw88xxa_switch_channel() */
+	if (ch >= 36 && ch <= 48)
+		fc = 0x494;
+	else if (ch >= 50 && ch <= 64)
+		fc = 0x453;
+	else if (ch >= 100 && ch <= 116)
+		fc = 0x452;
+	else if (ch >= 118)
+		fc = 0x412;
+	else
+		fc = 0x96a;
+	wr32m(REG_CLKTRK, 0x1ffe0000, fc);
+	if (ch >= 36 && ch <= 64)
+		mod = 0x101;
+	else if (ch >= 100 && ch <= 140)
+		mod = 0x301;
+	else if (ch > 140)
+		mod = 0x501;
+	else
+		mod = 0;
+	rf_write(0x18, BIT(18) | BIT(17) | BIT(16) | BIT(9) | BIT(8), mod);
+	rf_write(0x18, 0xff, ch);
+
+	/* rtw88xxa_post_set_bw_mode(): 20 MHz, 1T1R */
+	clr16(REG_WMAC_TRXPTCL_CTL, BIT(7) | BIT(8));
+	wr8(REG_DATA_SC, 0);
+	wr32m(REG_ADCCLK, 0x003003C3, 0x00300200);
+	wr32m(REG_ADC160, BIT(30), 0);
+	wr32m(REG_L1PKTH, 0x03C00000, 8);
+	/* rtw88xxa_set_channel_rf(): RF 0x18 BW bits = 20 MHz */
+	rf_write(0x18, BIT(11) | BIT(10), 3);
+
+	set_tx_power(ch);
+	R.chan = ch;
+}
+
+/* ---- power on (rtw88xxa_power_on) ---------------------------------------- */
+static int power_on(void)
+{
+	/* page_table_8821a / rqpn: everything on one DMA queue (LOW with 3-4
+	 * bulk-OUT endpoints, NORMAL with 2). */
+	unsigned int hq = 8, nq = 0, lq = 0, exq = 0, pub, boundary = 248;
+	unsigned int dma = R.n_out >= 3 ? DMA_LOW : DMA_NORMAL, v;
+
+	if (R.n_out >= 3)
+		lq = 8;
+	if (R.n_out >= 4)
+		exq = 4;
+
+	hw_reset();
+	/* rtw88xxau_init_power_on() */
+	if (pwr_seq(RTL_TBL_PWR_ON) < 0) {
+		R.state = "power-on sequence failed";
+		return -1;
+	}
+	wr16(REG_CR, 0);
+	set16(REG_CR, 0x063F);   /* HCI/MAC TX+RX DMA, protocol, sched, sec, 32K */
+	if (rd8(REG_SYS_CFG1 + 3) & BIT(0))
+		set8(REG_LDO_SWR_CTRL, BIT(6));
+
+	/* rtw_set_trx_fifo_info(): 256 pages, 8 reserved -> boundary 248 */
+	if (llt_init(boundary) < 0) {
+		R.state = "LLT init failed";
+		return -1;
+	}
+	set32(REG_TXDMA_OFFSET_CHK, BIT(9));
+	if (download_fw() < 0)
+		return -1;
+	wr8(REG_HMETFR, 0xf);
+	if (load_table(RTL_TBL_MAC) < 0) {
+		R.state = "MAC table failed";
+		return -1;
+	}
+
+	/* rtw88xxau_init_queue_reserved_page() */
+	pub = boundary - hq - lq - nq - exq - 1;
+	wr32(REG_RQPN_NPQ, nq | (exq << 16));
+	wr32(REG_RQPN, BIT(31) | hq | (lq << 8) | (pub << 16));
+	/* rtw88xxau_init_tx_buffer_boundary() */
+	wr8(REG_BCNQ_BDNY, boundary);
+	wr8(REG_MGQ_BDNY, boundary);
+	wr8(REG_WMAC_LBK_BF_HD, boundary);
+	wr8(REG_TRXFF_BNDY, boundary);
+	wr8(REG_DWBCN0_CTRL + 1, boundary);
+	/* rtw88xxau_init_queue_priority(), all queues on `dma`; RX
+	 * aggregation (bit 2) off */
+	v = rd16(REG_TXDMA_PQ_MAP) & 0x3;
+	v |= (dma << 4) | (dma << 6) | (dma << 8) | (dma << 10) |
+	     (dma << 12) | (dma << 14);
+	wr16(REG_TXDMA_PQ_MAP, v);
+	wr16(REG_TRXFF_BNDY + 2, 16128 - 128 - 1);
+	wr8(REG_RX_DRVINFO_SZ, 4);
+	wr32(REG_HIMR0, 0);
+	wr32(REG_HIMR1, 0);
+	wr32m(REG_CR, 0x30000, 0);                /* net type: no link */
+
+	/* rtw88xxa_init_wmac_setting() */
+	wr16(REG_RXFLTMAP0, 0xffff);
+	wr16(REG_RXFLTMAP1, 0x0400);
+	wr16(REG_RXFLTMAP2, 0xffff);
+	wr32(REG_MAR, 0xffffffff);
+	wr32(REG_MAR + 4, 0xffffffff);
+	/* rtw88xxa_init_adaptive_ctrl() */
+	wr32m(REG_RRSR, 0xfffff, 0xffff1);
+	wr16(REG_RETRY_LIMIT, 0x3030);
+	/* rtw88xxa_init_edca() */
+	wr16(REG_SPEC_SIFS, 0x100a);
+	wr16(REG_MAC_SPEC_SIFS, 0x100a);
+	wr16(REG_SIFS, 0x100a);
+	wr16(REG_SIFS + 2, 0x100a);
+	wr32(REG_EDCA_BE_PARAM, 0x005EA42B);
+	wr32(REG_EDCA_BK_PARAM, 0x0000A44F);
+	wr32(REG_EDCA_VI_PARAM, 0x005EA324);
+	wr32(REG_EDCA_VO_PARAM, 0x002FA226);
+	wr8(REG_USTIME_TSF, 0x50);
+	wr8(REG_USTIME_EDCA, 0x50);
+	set8(REG_FWHW_TXQ_CTRL, BIT(7));
+	wr8(REG_ACKTO, 0x80);
+	/* rtw88xxau_tx_aggregation() */
+	wr32m(REG_DWBCN0_CTRL, 0xf0, 6);
+	wr8(REG_DWBCN1_CTRL, 6 << 1);
+	/* rtw88xxa_init_beacon_parameters() */
+	wr16(REG_BCN_CTRL, (BIT(4) << 8) | BIT(4));
+	wr32m(REG_TBTT_PROHIBIT, 0xfffff, 0x04 | (0x64 << 8));
+	wr8(REG_DRVERLYINT, 0x05);
+	wr8(REG_BCNDMATIM, 0x02);
+	wr16(REG_BCNTCFG, 0x4413);
+	wr8(REG_BCN_MAX_ERR, 0xff);
+	/* rtw_usb_init_burst_pkt_len(): high speed, 512-byte bursts */
+	wr8(REG_RXDMA_MODE, (3 << 2) | BIT(1) | (1 << 4));
+	set16(REG_TXDMA_OFFSET_CHK, BIT(9));
+	wr8(REG_USB3_RXITV, 0x01);
+	wr16(REG_RXDMA_STATUS, 0x7400);
+	wr8(REG_RXDMA_STATUS + 1, 0xf5);
+	wr8(REG_AMPDU_MAX_TIME, 0x5e);
+	wr32(REG_AMPDU_MAX_LENGTH, 0xffffffff);
+	wr8(REG_USTIME_TSF, 0x50);
+	wr8(REG_USTIME_EDCA, 0x50);
+	set8(REG_SINGLE_AMPDU_CTRL, BIT(7));
+	wr8(REG_RX_PKT_LIMIT, 0x18);
+	wr8(REG_PIFS, 0x00);
+	wr16(REG_MAX_AGGR_NUM, 0x1f1f);
+	wr8(REG_FWHW_TXQ_CTRL, 0x80);
+	wr32(REG_FAST_EDCA_CTRL, 0x03087777);
+	set8(REG_RSV_CTRL, BIT(5) | BIT(6));
+	wr32(REG_ARFR0, 0x00000010);
+	wr32(REG_ARFRH0, 0xfffff000);
+	wr32(REG_ARFR1_V1, 0x00000010);
+	wr32(REG_ARFRH1_V1, 0x003ff000);
+	wr32(REG_ARFR2_V1, 0x00000015);
+	wr32(REG_ARFRH2_V1, 0x003ff000);
+	wr32(REG_ARFR3_V1, 0x00000015);
+	wr32(REG_ARFRH3_V1, 0xffcff000);
+	set8(REG_CR, BIT(6) | BIT(7));            /* MACTXEN | MACRXEN */
+
+	/* rtw88xxa_phy_bb_config() */
+	v = rd8(REG_SYS_FUNC_EN) | BIT(2);
+	wr8(REG_SYS_FUNC_EN, v);
+	wr8(REG_SYS_FUNC_EN, v | BIT(0) | BIT(1));
+	wr8(REG_RF_CTRL, 7);
+	wr8(REG_RF_B_CTRL, 7);
+	if (load_table(RTL_TBL_BB) < 0 || load_table(RTL_TBL_AGC) < 0) {
+		R.state = "BB table failed";
+		return -1;
+	}
+	wr32m(REG_AFE_CTRL3, 0x00FFF000,
+	      (R.crystal & 0x3f) | ((R.crystal & 0x3f) << 6));
+	/* rtw88xxa_phy_rf_config() */
+	if (load_table(RTL_TBL_RF_A) < 0) {
+		R.state = "RF table failed";
+		return -1;
+	}
+	R.chan = 0;
+	R.band5 = 0;
+	switch_band(0);
+
+	wr32(REG_SEC_CMD, BIT(31) | BIT(30));     /* clear the key CAM */
+	wr8(REG_HWSEQ_CTRL, 0xff);
+	wr32(REG_BAR_MODE_CTRL, 0x0201ffff);
+	wr8(REG_NAV_CTRL + 2, 0);
+	clr8(REG_GPIO_MUXCFG, BIT(5));
+	clr8(REG_QUEUE_CTRL, BIT(3));
+	wr8(REG_FWHW_TXQ_CTRL + 1, 0x0f);
+	wr8(REG_EARLY_MODE_CONTROL + 3, 0x01);
+	wr16(REG_TX_RPT_TIME, 0x3df0);
+	wr8(REG_SYS_SDIO_CTRL, 0);
+	wr8(REG_ACLK_MON, 0);
+	wr8(REG_USB_HRPWM, 0);
+	set32(REG_FWHW_TXQ_CTRL, BIT(12));
+
+	/* rtw_core_start(): RX filter; then our address */
+	wr32(REG_RCR, RCR_DEFAULT);
+	{
+		unsigned int i;
+
+		for (i = 0; i < 6; i++) {
+			wr8(REG_MACID + i, R.mac[i]);
+			wr8(REG_BSSID + i, 0);
+		}
+	}
+	set_channel(1);
+	return R.fatal ? -1 : 0;
+}
+
+/* rtw88xxa_power_off(), without BT coex. */
+static void power_off(void)
+{
+	unsigned int cr = rd8(REG_CR);
+
+	if (cr == 0 || cr == 0xea || R.fatal)
+		return;
+	wr32(REG_HIMR0, 0);
+	wr32(REG_HIMR1, 0);
+	wr8(REG_CR, 0);
+	pwr_seq(RTL_TBL_PWR_LPS);
+	if (rd8(REG_MCUFW_CTRL) & BIT(7))
+		reset_8051();
+	clr8(REG_SYS_FUNC_EN + 1, BIT(2));
+	wr8(REG_MCUFW_CTRL, 0);
+	pwr_seq(RTL_TBL_PWR_OFF);
+}
+
+/* ---- RX ------------------------------------------------------------------ */
+static int cck_rx_pwr(unsigned int lna, unsigned int vga)
+{
+	static const signed char lna_gain[] = { 15, -1, -17, 0, -30, -38 };
+
+	if (lna > 5 || lna == 3)
+		return -100;
+	return lna_gain[lna] - 2 * (int)vga;
+}
+
+static void rx_packet(const unsigned char *d, unsigned int avail)
+{
+	unsigned int w0, len, drv, shift, rate, off;
+	const unsigned char *phy, *f;
+	int rssi = -100;
+
+	while (avail >= RXDESC_SZ) {
+		w0 = le32(d);
+		len = w0 & 0x3fff;
+		drv = ((w0 >> 16) & 0xf) * 8;
+		shift = (w0 >> 24) & 3;
+		rate = le32(d + 12) & 0x7f;
+		off = RXDESC_SZ + drv + shift;
+		if (!len || off + len > avail)
+			return;
+		phy = d + RXDESC_SZ + shift;
+		f = d + off;
+		if (le32(d + 8) & BIT(28)) {
+			/* C2H firmware event: not used. */
+		} else if (w0 & BIT(14)) {
+			R.rx_crc++;
+		} else if (len > 4 + 24) {
+			if (drv && (w0 & BIT(26))) {
+				if (rate <= 3)
+					rssi = cck_rx_pwr((le32(phy + 4) >> 13) & 7,
+							  (le32(phy + 4) >> 8) & 0x1f);
+				else
+					rssi = (int)(phy[0] & 0x7f) - 110;
+			}
+			R.rx_pkts++;
+			R.last_rssi = rssi;
+			wifi_rx_frame(f, len - 4, rssi, 0, R.chan);
+		}
+		off = (off + len + 7) & ~7u;
+		if (off >= avail)
+			return;
+		d += off;
+		avail -= off;
+	}
+}
+
+/* Drain the RX FIFO: every completed bulk-IN transfer is parsed, then the
+ * next one is armed; stop after ~0.3 ms without data or 64 packets (the
+ * tick is frozen inside a syscall, so the bounds are counts). */
+static int drain_rx(void)
+{
+	unsigned int frames = 0, idle = 0;
+	int n;
+
+	while (frames < 64) {
+		n = usb_bulk_in(R.ctrl, R.addr, R.ep_in, R.ep_in_mps,
+				R.rxbuf, RX_BUF_SZ);
+		if (n < 0) {
+			io_fail();
+			return -1;
+		}
+		if (n == 0) {
+			if (++idle > 6)
+				break;
+			udelay(50);
+			continue;
+		}
+		idle = 0;
+		frames++;
+		rx_packet(R.rxbuf, (unsigned int)n);
+	}
+	return 0;
+}
+
+/* ---- TX ------------------------------------------------------------------ */
+static int tx_frame(const unsigned char *frame, unsigned int len, int mgmt,
+		    int protect)
+{
+	unsigned char *b = R.txbuf, *f;
+	unsigned int pad, rate, rate_id, qsel, i, sum, w;
+
+	if (!R.up || R.fatal)
+		return -1;
+	if (len + TXDESC_SZ + 8 + 8 > TX_BUF_SZ)
+		return -1;
+	if (protect && !R.have_tk)
+		return -1;
+	/* A transfer of an exact multiple of 512 bytes would need a
+	 * zero-length packet; the vendor drivers shift the frame by 8 bytes
+	 * (PKT_OFFSET 1) instead. */
+	pad = ((len + (protect ? 8 : 0) + TXDESC_SZ) % 512) == 0 ? 8 : 0;
+	f = b + TXDESC_SZ + pad;
+	rmemcpy(f, frame, len);
+	if (protect) {
+		if (ccmp_tx_encrypt(f, len, 24, R.tk) < 0)
+			return -1;
+		len += 8;
+	}
+	if (mgmt) {
+		rate = R.band5 ? RATE_6M : RATE_1M;
+		rate_id = R.band5 ? RATEID_G : RATEID_B;
+		qsel = QSEL_MGMT;
+	} else {
+		rate = RATE_12M;
+		rate_id = R.band5 ? RATEID_G : RATEID_BG;
+		qsel = QSEL_BE;
+	}
+	rmemzero(b, TXDESC_SZ + pad);
+	/* rtw_tx_fill_tx_desc() */
+	put32(b + 0, len | ((TXDESC_SZ + pad) << 16) |
+		     ((f[4] & 1) ? BIT(24) : 0) | BIT(26) | BIT(31));
+	put32(b + 4, (qsel << 8) | (rate_id << 16) | ((pad ? 1 : 0) << 24));
+	put32(b + 12, BIT(8) | BIT(10));          /* USE_RATE, DISDATAFB */
+	put32(b + 16, rate | (0x1f << 8));        /* old_datarate_fb_limit */
+	put32(b + 28, 1u << 24);                  /* DMA_TXAGG_NUM 1 */
+	put32(b + 32, BIT(15));                   /* EN_HWSEQ */
+	/* fill_txdesc_checksum_common(): XOR of the first 16 le16 words */
+	sum = 0;
+	for (i = 0; i < 32; i += 2)
+		sum ^= b[i] | (b[i + 1] << 8);
+	w = le32(b + 28) & ~0xffffu;
+	put32(b + 28, w | (sum & 0xffff));
+
+	if (usb_bulk_out(R.ctrl, R.addr, R.ep_tx, R.ep_out_mps, b,
+			 TXDESC_SZ + pad + len) != 0) {
+		R.tx_errs++;
+		io_fail();
+		return -1;
+	}
+	R.tx_pkts++;
+	return 0;
+}
+
+static void send_probe_req(void)
+{
+	static const unsigned char rates24[] = { 1, 8, 0x82, 0x84, 0x8b, 0x96,
+						 0x0c, 0x12, 0x18, 0x24,
+						 50, 4, 0x30, 0x48, 0x60, 0x6c };
+	static const unsigned char rates5[] = { 1, 8, 0x8c, 0x12, 0x98, 0x24,
+						0xb0, 0x48, 0x60, 0x6c };
+	unsigned char f[64];
+	unsigned int o;
+
+	rmemzero(f, 24);
+	f[0] = 0x40;                               /* probe request */
+	rmemcpy(f + 10, R.mac, 6);
+	for (o = 0; o < 6; o++)
+		f[4 + o] = f[16 + o] = 0xff;
+	o = 24;
+	f[o++] = 0;                                /* wildcard SSID */
+	f[o++] = 0;
+	if (R.band5) {
+		rmemcpy(f + o, rates5, sizeof(rates5));
+		o += sizeof(rates5);
+	} else {
+		rmemcpy(f + o, rates24, sizeof(rates24));
+		o += sizeof(rates24);
+	}
+	tx_frame(f, o, 1, 0);
+}
+
+/* ---- scan ---------------------------------------------------------------- */
+static int chan_active(unsigned int ch)
+{
+	return ch <= 14 || (ch >= 36 && ch <= 48);
+}
+
+static void scan_step(void)
+{
+	unsigned int ch;
+
+	if (R.scan_idx >= sizeof(scan_chans)) {
+		R.scanning = 0;
+		wifi_scan_done(1);
+		return;
+	}
+	ch = scan_chans[R.scan_idx++];
+	set_channel(ch);
+	if (chan_active(ch))
+		send_probe_req();
+	R.scan_next = get_ticks_ms() +
+		      (chan_active(ch) ? DWELL_ACTIVE : DWELL_PASSIVE);
+}
+
+/* ---- public API ---------------------------------------------------------- */
+static int check_present(void)
+{
+	if (!R.present)
+		return 0;
+	if (R.port != 0xFFu && !usb_port_connected(R.ctrl, R.port)) {
+		R.present = 0;
+		R.fatal = 1;
+		return 0;
+	}
+	return 1;
+}
+
+int rtl8821au_present(void)
+{
+	return check_present();
+}
+
+const char *rtl8821au_chip_name(void)
+{
+	return "RTL8821AU USB";
+}
+
+const char *rtl8821au_fw_name(void)
+{
+	return R.up ? R.fw_name : "";
+}
+
+int rtl8821au_fatal(void)
+{
+	return R.up && (R.fatal || !check_present());
+}
+
+static int alloc_buffers(void)
+{
+	unsigned int rx;
+
+	if (R.io)
+		return 0;
+	R.io = (unsigned char *)mm_alloc_page();
+	R.efuse = (unsigned char *)mm_alloc_page();
+	R.txbuf = (unsigned char *)mm_alloc_page();
+	rx = mm_alloc_pages(4);
+	if (!R.io || !R.efuse || !R.txbuf || !rx) {
+		R.io = 0;
+		return -1;
+	}
+	/* An 8 KB-aligned 8 KB window never crosses a 64 KB boundary (one
+	 * xHCI TRB) and is contiguous for EHCI's qTD pages. */
+	R.rxbuf = (unsigned char *)((rx + 8191) & ~8191u);
+	return 0;
+}
+
+static void fmt_fw_name(void)
+{
+	static const char pre[] = "rtw8821a ";
+	unsigned int i = 0, v, d = 10000;
+	int started = 0;
+
+	for (; pre[i]; i++)
+		R.fw_name[i] = pre[i];
+	v = R.fw_ver;
+	for (; d; d /= 10) {
+		if (v / d || started || d == 1) {
+			R.fw_name[i++] = (char)('0' + v / d);
+			started = 1;
+		}
+		v %= d;
+	}
+	R.fw_name[i++] = '.';
+	v = R.fw_sub;
+	if (v >= 10)
+		R.fw_name[i++] = (char)('0' + v / 10 % 10);
+	R.fw_name[i++] = (char)('0' + v % 10);
+	R.fw_name[i] = 0;
+}
+
+int rtl8821au_up(unsigned char mac[6])
+{
+
+	if (!check_present()) {
+		R.state = "no RTL8821AU stick";
+		return -1;
+	}
+	if (R.up && !R.fatal) {
+		rmemcpy(mac, R.mac, 6);
+		return 0;
+	}
+	R.up = 0;
+	R.fatal = 0;
+	R.io_errs = 0;
+	R.scanning = 0;
+	R.have_tk = 0;
+	if (alloc_buffers() < 0) {
+		R.state = "out of memory";
+		return -1;
+	}
+	if (load_blobs() < 0)
+		return -1;
+	R.cut = (rd32(REG_SYS_CFG1) >> 12) & 0xf;
+	rlog("rtl8821au: power on, cut ");
+	serial_putdec(SERIAL_COM1, R.cut);
+	rlog("\n");
+
+	/* rtw_chip_efuse_info_setup(): power on the MAC, read the eFuse. */
+	wr8(REG_RSV_CTRL, 0);
+	wr8(REG_LDO_SWR_CTRL, (rd32(REG_SYS_CFG1) & BIT(24)) ? 0xC3 : 0x83);
+	if (pwr_seq(RTL_TBL_PWR_ON) < 0 || read_efuse() < 0) {
+		R.state = R.fatal ? "USB I/O failed" : "eFuse read failed";
+		return -1;
+	}
+	parse_efuse();
+	rlog("rtl8821au: eFuse ok, rfe ");
+	serial_puthex(SERIAL_COM1, R.rfe_cond);
+	rlog("\n");
+	power_off();
+
+	R.state = 0;
+	if (power_on() < 0) {
+		if (R.fatal)
+			R.state = "USB I/O failed";
+		else if (!R.state)
+			R.state = "power on failed";
+		loghex("rtl8821au: bring-up failed, MCUFW_CTRL ",
+		       rd32(REG_MCUFW_CTRL));
+		rlog("rtl8821au: ");
+		rlog(R.state);
+		rlog("\n");
+		return -1;
+	}
+	fmt_fw_name();
+	R.up = 1;
+	R.state = "ready";
+	rmemcpy(mac, R.mac, 6);
+	rlog("rtl8821au: up, firmware ");
+	rlog(R.fw_name);
+	rlog("\n");
+	return 0;
+}
+
+void rtl8821au_down(void)
+{
+	R.scanning = 0;
+	R.have_tk = 0;
+	if (R.up && check_present() && !R.fatal)
+		power_off();
+	R.up = 0;
+	R.chan = 0;
+	R.state = "down";
+}
+
+const char *rtl8821au_state(void)
+{
+	return R.state ? R.state : "not started";
+}
+
+int rtl8821au_poll(void)
+{
+	if (!R.up)
+		return -1;
+	if (R.fatal || !check_present())
+		return -1;
+	if (drain_rx() < 0)
+		return -1;
+	if (R.scanning && (int)(get_ticks_ms() - R.scan_next) >= 0)
+		scan_step();
+	return R.fatal ? -1 : 0;
+}
+
+int rtl8821au_scan_start(void)
+{
+	if (!R.up || R.fatal)
+		return -1;
+	wr32(REG_RCR, RCR_DEFAULT);
+	R.scanning = 1;
+	R.scan_idx = 0;
+	scan_step();
+	return 0;
+}
+
+int rtl8821au_scan_running(void)
+{
+	return R.scanning;
+}
+
+int rtl8821au_prepare_connect(const unsigned char bssid[6],
+			      unsigned int chan, unsigned int beacon_int)
+{
+	unsigned int i;
+
+	(void)beacon_int;
+	if (!R.up || R.fatal)
+		return -1;
+	R.scanning = 0;
+	R.have_tk = 0;
+	rmemcpy(R.bssid, bssid, 6);
+	set_channel(chan);
+	for (i = 0; i < 6; i++)
+		wr8(REG_BSSID + i, bssid[i]);
+	wr32(REG_RCR, RCR_DEFAULT);
+	drain_rx();
+	return R.fatal ? -1 : 0;
+}
+
+int rtl8821au_set_assoc(unsigned short aid, unsigned int beacon_int,
+			unsigned int dtim)
+{
+	(void)beacon_int;
+	(void)dtim;
+	if (!R.up || R.fatal)
+		return -1;
+	wr32m(REG_AID, 0x7ff, aid);
+	wr32m(REG_CR, 0x30000, 2);                /* RTW_NET_MGD_LINKED */
+	/* Only our BSS's data and beacons from here on. */
+	wr32(REG_RCR, RCR_DEFAULT | RCR_CBSSID_DATA | RCR_CBSSID_BCN);
+	return R.fatal ? -1 : 0;
+}
+
+void rtl8821au_disconnect(void)
+{
+	unsigned int i;
+
+	R.have_tk = 0;
+	if (!R.up || R.fatal)
+		return;
+	wr32m(REG_CR, 0x30000, 0);
+	wr32m(REG_AID, 0x7ff, 0);
+	for (i = 0; i < 6; i++)
+		wr8(REG_BSSID + i, 0);
+	wr32(REG_RCR, RCR_DEFAULT);
+}
+
+int rtl8821au_tx_mgmt(const unsigned char *frame, unsigned int len)
+{
+	return tx_frame(frame, len, 1, 0);
+}
+
+int rtl8821au_tx_data(const unsigned char *frame, unsigned int len,
+		      int protect)
+{
+	return tx_frame(frame, len, 0, protect);
+}
+
+int rtl8821au_set_key(int pairwise, int keyidx, const unsigned char key[16],
+		      const unsigned char rsc[6])
+{
+	(void)keyidx;
+	(void)rsc;
+	/* The group key is only used for RX, which wifi.c decrypts. */
+	if (pairwise) {
+		rmemcpy(R.tk, key, 16);
+		R.have_tk = 1;
+	}
+	return 0;
+}
+
+void rtl8821au_clear_keys(void)
+{
+	rmemzero(R.tk, 16);
+	R.have_tk = 0;
+}
+
+/* ---- USB probe ----------------------------------------------------------- */
+static int parse_config(void)
+{
+	unsigned char *c = R.io;
+	unsigned int total, o, n_out = 0, in = 0, in_mps = 512, out_mps = 512;
+	int n;
+
+	n = usb_control_in(R.ctrl, R.addr, R.mps, R.low, 0x80, 0x06,
+			   0x0200, 0, c, 9);
+	if (n < 9)
+		return -1;
+	total = c[2] | (c[3] << 8);
+	if (total > 512)
+		total = 512;
+	n = usb_control_in(R.ctrl, R.addr, R.mps, R.low, 0x80, 0x06,
+			   0x0200, 0, c, total);
+	if (n < 9)
+		return -1;
+	total = (unsigned int)n;
+	for (o = 0; o + 2 <= total && c[o] >= 2; o += c[o]) {
+		if (c[o + 1] == 4 && o + 9 <= total && c[o + 2] != 0)
+			break;                 /* only interface 0 */
+		if (c[o + 1] != 5 || o + 7 > total)
+			continue;
+		if ((c[o + 3] & 3) != 2)
+			continue;              /* bulk only */
+		if (c[o + 2] & 0x80) {
+			if (!in) {
+				in = c[o + 2] & 0x0f;
+				in_mps = c[o + 4] | (c[o + 5] << 8);
+			}
+		} else if (n_out < 4) {
+			R.ep_out[n_out++] = c[o + 2] & 0x0f;
+			out_mps = c[o + 4] | (c[o + 5] << 8);
+		}
+	}
+	if (!in || n_out < 2)
+		return -1;
+	R.ep_in = in;
+	R.ep_in_mps = in_mps ? in_mps : 512;
+	R.n_out = n_out;
+	R.ep_out_mps = out_mps ? out_mps : 512;
+	/* dma_mapping_to_ep(): HIGH 0, NORMAL 1, LOW 2, EXTRA 3 */
+	R.ep_tx = R.ep_out[n_out >= 3 ? 2 : 1];
+	if (usb_control_out(R.ctrl, R.addr, R.mps, R.low, 0x00, 0x09,
+			    c[5], 0) < 0)
+		return -1;
+	return 0;
+}
+
+static int device_probe(unsigned int ctrl, unsigned int addr,
+			unsigned int mps, int low_speed, unsigned int port,
+			unsigned int vendor, unsigned int product)
+{
+	unsigned int i;
+
+	for (i = 0; i < sizeof(usb_ids) / sizeof(usb_ids[0]); i++)
+		if (usb_ids[i][0] == vendor && usb_ids[i][1] == product)
+			break;
+	if (i == sizeof(usb_ids) / sizeof(usb_ids[0]))
+		return 0;
+	if (!usb_bulk_supported(ctrl)) {
+		rlog("rtl8821au: stick on a controller without bulk "
+		    "transfers (UHCI/OHCI), ignored\n");
+		return 0;
+	}
+	if (alloc_buffers() < 0)
+		return 0;
+	/* A re-enumeration resets the chip: a running instance is dead. */
+	if (R.up)
+		R.fatal = 1;
+	R.ctrl = ctrl;
+	R.addr = addr;
+	R.mps = mps;
+	R.low = low_speed;
+	R.port = port;
+	R.vendor = vendor;
+	R.product = product;
+	R.gen = usb_generation();
+	R.present = 1;
+	R.io_errs = 0;
+	if (parse_config() < 0) {
+		rlog("rtl8821au: unexpected USB configuration\n");
+		R.present = 0;
+		return 0;
+	}
+	if (!R.up)
+		R.fatal = 0;
+	R.state = "firmware not loaded";
+	rlog("rtl8821au: found ");
+	serial_puthex(SERIAL_COM1, (vendor << 16) | product);
+	rlog(", bulk in ");
+	serial_putdec(SERIAL_COM1, R.ep_in);
+	rlog(", tx ep ");
+	serial_putdec(SERIAL_COM1, R.ep_tx);
+	rlog(" of ");
+	serial_putdec(SERIAL_COM1, R.n_out);
+	rlog("\n");
+	return 1;
+}
+
+void rtl8821au_register(void)
+{
+	usb_register_device_probe(device_probe);
+}

+ 40 - 0
rtl8821au.h

@@ -0,0 +1,40 @@
+/* Realtek RTL8821AU / RTL8811AU USB WiFi (802.11ac, 1x1), e.g. the TP-Link
+ * Archer T2U Nano (2357:011f).  A port of the Linux rtw88 8821A/USB bring-up
+ * (rtw88xxa.c, rtw8821a.c, mac.c, usb.c) that plugs into wifi.c through the
+ * wififw dispatch layer (wifi_fw.h); every call below has the iwl_mvm.h
+ * semantics.
+ *
+ * The chip runs with its crypto engine off: wifi.c decrypts CCMP RX in
+ * software (ccmp.c) and this driver encrypts CCMP TX itself, so the
+ * firmware never sees a key.  Scanning is a software channel hop driven by
+ * rtl8821au_poll(); TX uses fixed legacy rates (no rate adaptation). */
+#ifndef RTL8821AU_H
+#define RTL8821AU_H
+
+/* Register the USB device probe (kernel.c, next to the ASIX/RNDIS ones). */
+void rtl8821au_register(void);
+
+int  rtl8821au_present(void);      /* a supported stick is attached */
+const char *rtl8821au_chip_name(void);
+const char *rtl8821au_fw_name(void);   /* "" until the firmware runs */
+int  rtl8821au_fatal(void);        /* unplugged / USB errors: reload */
+
+int  rtl8821au_up(unsigned char mac[6]);
+void rtl8821au_down(void);
+const char *rtl8821au_state(void);
+int  rtl8821au_poll(void);
+int  rtl8821au_scan_start(void);
+int  rtl8821au_scan_running(void);
+int  rtl8821au_prepare_connect(const unsigned char bssid[6],
+			       unsigned int chan, unsigned int beacon_int);
+int  rtl8821au_set_assoc(unsigned short aid, unsigned int beacon_int,
+			 unsigned int dtim);
+void rtl8821au_disconnect(void);
+int  rtl8821au_tx_mgmt(const unsigned char *frame, unsigned int len);
+int  rtl8821au_tx_data(const unsigned char *frame, unsigned int len,
+		       int protect);
+int  rtl8821au_set_key(int pairwise, int keyidx, const unsigned char key[16],
+		       const unsigned char rsc[6]);
+void rtl8821au_clear_keys(void);
+
+#endif

+ 7 - 3
sys_net.c

@@ -132,8 +132,12 @@ int sys_net_info(struct sys_netinfo *out)
 	net_get_dns(out->dns);
 	for (i = 0; nd->name[i] && i + 1 < sizeof(out->name); i++)
 		out->name[i] = nd->name[i];
-	for (i = 0; nd->chip[i] && i + 1 < sizeof(out->chip); i++)
-		out->chip[i] = nd->chip[i];
+	{
+		const char *chip = netdev_chip(nd);
+
+		for (i = 0; chip[i] && i + 1 < sizeof(out->chip); i++)
+			out->chip[i] = chip[i];
+	}
 	out->link = (unsigned int)netdev_link(nd);
 	return 1;
 }
@@ -167,7 +171,7 @@ int sys_net_devlist(struct sys_netdev *out, unsigned int max)
 		for (j = 0; j < sizeof(out[n]); j++)
 			((unsigned char *)&out[n])[j] = 0;
 		nd_copy_name(out[n].name, sizeof(out[n].name), nd->name);
-		nd_copy_name(out[n].chip, sizeof(out[n].chip), nd->chip);
+		nd_copy_name(out[n].chip, sizeof(out[n].chip), netdev_chip(nd));
 		out[n].kind = nd_kind(nd);
 		out[n].active = (nd == act);
 		out[n].link = (unsigned int)(nd == act ? netdev_link(nd) :

+ 30 - 0
third_party/rtw88/LICENSE

@@ -0,0 +1,30 @@
+The files in this directory are taken from the Linux kernel's rtw88 driver
+(drivers/net/wireless/realtek/rtw88), which Realtek dual-licenses as
+"GPL-2.0 OR BSD-3-Clause".  NebOS uses them under the BSD-3-Clause terms:
+
+Copyright(c) 2018-2024  Realtek Corporation
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+1. Redistributions of source code must retain the above copyright notice,
+   this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright notice,
+   this list of conditions and the following disclaimer in the documentation
+   and/or other materials provided with the distribution.
+3. Neither the name of the copyright holder nor the names of its
+   contributors may be used to endorse or promote products derived from this
+   software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.

+ 1656 - 0
third_party/rtw88/rtw8821a_table.inc

@@ -0,0 +1,1656 @@
+/* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause */
+/* Copyright(c) 2024  Realtek Corporation
+ *
+ * RTL8821A register tables and power sequences, taken unchanged from Linux
+ * drivers/net/wireless/realtek/rtw88/rtw8821a_table.c (torvalds/linux
+ * 067c5c2).  NebOS uses them under the BSD-3-Clause option (see LICENSE in this
+ * directory).  Only the MAC/AGC/BB/RF-A tables and the card power sequences
+ * are kept; mkrtwtbl.c turns them into firmware/rtl8821a.tbl for the pack.
+ */
+
+static const u32 rtw8821a_mac[] = {
+	0x421, 0x0000000F,
+	0x428, 0x0000000A,
+	0x429, 0x00000010,
+	0x430, 0x00000000,
+	0x431, 0x00000000,
+	0x432, 0x00000000,
+	0x433, 0x00000001,
+	0x434, 0x00000004,
+	0x435, 0x00000005,
+	0x436, 0x00000007,
+	0x437, 0x00000008,
+	0x43C, 0x00000004,
+	0x43D, 0x00000005,
+	0x43E, 0x00000007,
+	0x43F, 0x00000008,
+	0x440, 0x0000005D,
+	0x441, 0x00000001,
+	0x442, 0x00000000,
+	0x444, 0x00000010,
+	0x445, 0x00000000,
+	0x446, 0x00000000,
+	0x447, 0x00000000,
+	0x448, 0x00000000,
+	0x449, 0x000000F0,
+	0x44A, 0x0000000F,
+	0x44B, 0x0000003E,
+	0x44C, 0x00000010,
+	0x44D, 0x00000000,
+	0x44E, 0x00000000,
+	0x44F, 0x00000000,
+	0x450, 0x00000000,
+	0x451, 0x000000F0,
+	0x452, 0x0000000F,
+	0x453, 0x00000000,
+	0x456, 0x0000005E,
+	0x460, 0x00000066,
+	0x461, 0x00000066,
+	0x4C8, 0x0000003F,
+	0x4C9, 0x000000FF,
+	0x4CC, 0x000000FF,
+	0x4CD, 0x000000FF,
+	0x4CE, 0x00000001,
+	0x500, 0x00000026,
+	0x501, 0x000000A2,
+	0x502, 0x0000002F,
+	0x503, 0x00000000,
+	0x504, 0x00000028,
+	0x505, 0x000000A3,
+	0x506, 0x0000005E,
+	0x507, 0x00000000,
+	0x508, 0x0000002B,
+	0x509, 0x000000A4,
+	0x50A, 0x0000005E,
+	0x50B, 0x00000000,
+	0x50C, 0x0000004F,
+	0x50D, 0x000000A4,
+	0x50E, 0x00000000,
+	0x50F, 0x00000000,
+	0x512, 0x0000001C,
+	0x514, 0x0000000A,
+	0x516, 0x0000000A,
+	0x525, 0x0000004F,
+	0x550, 0x00000010,
+	0x551, 0x00000010,
+	0x559, 0x00000002,
+	0x55C, 0x00000050,
+	0x55D, 0x000000FF,
+	0x605, 0x00000030,
+	0x607, 0x00000007,
+	0x608, 0x0000000E,
+	0x609, 0x0000002A,
+	0x620, 0x000000FF,
+	0x621, 0x000000FF,
+	0x622, 0x000000FF,
+	0x623, 0x000000FF,
+	0x624, 0x000000FF,
+	0x625, 0x000000FF,
+	0x626, 0x000000FF,
+	0x627, 0x000000FF,
+	0x638, 0x00000050,
+	0x63C, 0x0000000A,
+	0x63D, 0x0000000A,
+	0x63E, 0x0000000E,
+	0x63F, 0x0000000E,
+	0x640, 0x00000040,
+	0x642, 0x00000040,
+	0x643, 0x00000000,
+	0x652, 0x000000C8,
+	0x66E, 0x00000005,
+	0x700, 0x00000021,
+	0x701, 0x00000043,
+	0x702, 0x00000065,
+	0x703, 0x00000087,
+	0x708, 0x00000021,
+	0x709, 0x00000043,
+	0x70A, 0x00000065,
+	0x70B, 0x00000087,
+	0x718, 0x00000040,
+};
+
+static const u32 rtw8821a_agc[] = {
+	0x81C, 0xBF000001,
+	0x81C, 0xBF020001,
+	0x81C, 0xBF040001,
+	0x81C, 0xBF060001,
+	0x81C, 0xBE080001,
+	0x81C, 0xBD0A0001,
+	0x81C, 0xBC0C0001,
+	0x81C, 0xBA0E0001,
+	0x81C, 0xB9100001,
+	0x81C, 0xB8120001,
+	0x81C, 0xB7140001,
+	0x81C, 0xB6160001,
+	0x81C, 0xB5180001,
+	0x81C, 0xB41A0001,
+	0x81C, 0xB31C0001,
+	0x81C, 0xB21E0001,
+	0x81C, 0xB1200001,
+	0x81C, 0xB0220001,
+	0x81C, 0xAF240001,
+	0x81C, 0xAE260001,
+	0x81C, 0xAD280001,
+	0x81C, 0xAC2A0001,
+	0x81C, 0xAB2C0001,
+	0x81C, 0xAA2E0001,
+	0x81C, 0xA9300001,
+	0x81C, 0xA8320001,
+	0x81C, 0xA7340001,
+	0x81C, 0xA6360001,
+	0x81C, 0xA5380001,
+	0x81C, 0xA43A0001,
+	0x81C, 0x683C0001,
+	0x81C, 0x673E0001,
+	0x81C, 0x66400001,
+	0x81C, 0x65420001,
+	0x81C, 0x64440001,
+	0x81C, 0x63460001,
+	0x81C, 0x62480001,
+	0x81C, 0x614A0001,
+	0x81C, 0x474C0001,
+	0x81C, 0x464E0001,
+	0x81C, 0x45500001,
+	0x81C, 0x44520001,
+	0x81C, 0x43540001,
+	0x81C, 0x42560001,
+	0x81C, 0x41580001,
+	0x81C, 0x285A0001,
+	0x81C, 0x275C0001,
+	0x81C, 0x265E0001,
+	0x81C, 0x25600001,
+	0x81C, 0x24620001,
+	0x81C, 0x0A640001,
+	0x81C, 0x09660001,
+	0x81C, 0x08680001,
+	0x81C, 0x076A0001,
+	0x81C, 0x066C0001,
+	0x81C, 0x056E0001,
+	0x81C, 0x04700001,
+	0x81C, 0x03720001,
+	0x81C, 0x02740001,
+	0x81C, 0x01760001,
+	0x81C, 0x01780001,
+	0x81C, 0x017A0001,
+	0x81C, 0x017C0001,
+	0x81C, 0x017E0001,
+	0x8000020c,	0x00000000,	0x40000000,	0x00000000,
+	0x81C, 0xFB000101,
+	0x81C, 0xFA020101,
+	0x81C, 0xF9040101,
+	0x81C, 0xF8060101,
+	0x81C, 0xF7080101,
+	0x81C, 0xF60A0101,
+	0x81C, 0xF50C0101,
+	0x81C, 0xF40E0101,
+	0x81C, 0xF3100101,
+	0x81C, 0xF2120101,
+	0x81C, 0xF1140101,
+	0x81C, 0xF0160101,
+	0x81C, 0xEF180101,
+	0x81C, 0xEE1A0101,
+	0x81C, 0xED1C0101,
+	0x81C, 0xEC1E0101,
+	0x81C, 0xEB200101,
+	0x81C, 0xEA220101,
+	0x81C, 0xE9240101,
+	0x81C, 0xE8260101,
+	0x81C, 0xE7280101,
+	0x81C, 0xE62A0101,
+	0x81C, 0xE52C0101,
+	0x81C, 0xE42E0101,
+	0x81C, 0xE3300101,
+	0x81C, 0xA5320101,
+	0x81C, 0xA4340101,
+	0x81C, 0xA3360101,
+	0x81C, 0x87380101,
+	0x81C, 0x863A0101,
+	0x81C, 0x853C0101,
+	0x81C, 0x843E0101,
+	0x81C, 0x69400101,
+	0x81C, 0x68420101,
+	0x81C, 0x67440101,
+	0x81C, 0x66460101,
+	0x81C, 0x49480101,
+	0x81C, 0x484A0101,
+	0x81C, 0x474C0101,
+	0x81C, 0x2A4E0101,
+	0x81C, 0x29500101,
+	0x81C, 0x28520101,
+	0x81C, 0x27540101,
+	0x81C, 0x26560101,
+	0x81C, 0x25580101,
+	0x81C, 0x245A0101,
+	0x81C, 0x235C0101,
+	0x81C, 0x055E0101,
+	0x81C, 0x04600101,
+	0x81C, 0x03620101,
+	0x81C, 0x02640101,
+	0x81C, 0x01660101,
+	0x81C, 0x01680101,
+	0x81C, 0x016A0101,
+	0x81C, 0x016C0101,
+	0x81C, 0x016E0101,
+	0x81C, 0x01700101,
+	0x81C, 0x01720101,
+	0x9000040c,	0x00000000,	0x40000000,	0x00000000,
+	0x81C, 0xFB000101,
+	0x81C, 0xFA020101,
+	0x81C, 0xF9040101,
+	0x81C, 0xF8060101,
+	0x81C, 0xF7080101,
+	0x81C, 0xF60A0101,
+	0x81C, 0xF50C0101,
+	0x81C, 0xF40E0101,
+	0x81C, 0xF3100101,
+	0x81C, 0xF2120101,
+	0x81C, 0xF1140101,
+	0x81C, 0xF0160101,
+	0x81C, 0xEF180101,
+	0x81C, 0xEE1A0101,
+	0x81C, 0xED1C0101,
+	0x81C, 0xEC1E0101,
+	0x81C, 0xEB200101,
+	0x81C, 0xEA220101,
+	0x81C, 0xE9240101,
+	0x81C, 0xE8260101,
+	0x81C, 0xE7280101,
+	0x81C, 0xE62A0101,
+	0x81C, 0xE52C0101,
+	0x81C, 0xE42E0101,
+	0x81C, 0xE3300101,
+	0x81C, 0xA5320101,
+	0x81C, 0xA4340101,
+	0x81C, 0xA3360101,
+	0x81C, 0x87380101,
+	0x81C, 0x863A0101,
+	0x81C, 0x853C0101,
+	0x81C, 0x843E0101,
+	0x81C, 0x69400101,
+	0x81C, 0x68420101,
+	0x81C, 0x67440101,
+	0x81C, 0x66460101,
+	0x81C, 0x49480101,
+	0x81C, 0x484A0101,
+	0x81C, 0x474C0101,
+	0x81C, 0x2A4E0101,
+	0x81C, 0x29500101,
+	0x81C, 0x28520101,
+	0x81C, 0x27540101,
+	0x81C, 0x26560101,
+	0x81C, 0x25580101,
+	0x81C, 0x245A0101,
+	0x81C, 0x235C0101,
+	0x81C, 0x055E0101,
+	0x81C, 0x04600101,
+	0x81C, 0x03620101,
+	0x81C, 0x02640101,
+	0x81C, 0x01660101,
+	0x81C, 0x01680101,
+	0x81C, 0x016A0101,
+	0x81C, 0x016C0101,
+	0x81C, 0x016E0101,
+	0x81C, 0x01700101,
+	0x81C, 0x01720101,
+	0xA0000000,	0x00000000,
+	0x81C, 0xFF000101,
+	0x81C, 0xFF020101,
+	0x81C, 0xFE040101,
+	0x81C, 0xFD060101,
+	0x81C, 0xFC080101,
+	0x81C, 0xFD0A0101,
+	0x81C, 0xFC0C0101,
+	0x81C, 0xFB0E0101,
+	0x81C, 0xFA100101,
+	0x81C, 0xF9120101,
+	0x81C, 0xF8140101,
+	0x81C, 0xF7160101,
+	0x81C, 0xF6180101,
+	0x81C, 0xF51A0101,
+	0x81C, 0xF41C0101,
+	0x81C, 0xF31E0101,
+	0x81C, 0xF2200101,
+	0x81C, 0xF1220101,
+	0x81C, 0xF0240101,
+	0x81C, 0xEF260101,
+	0x81C, 0xEE280101,
+	0x81C, 0xED2A0101,
+	0x81C, 0xEC2C0101,
+	0x81C, 0xEB2E0101,
+	0x81C, 0xEA300101,
+	0x81C, 0xE9320101,
+	0x81C, 0xE8340101,
+	0x81C, 0xE7360101,
+	0x81C, 0xE6380101,
+	0x81C, 0xE53A0101,
+	0x81C, 0xE43C0101,
+	0x81C, 0xE33E0101,
+	0x81C, 0xA5400101,
+	0x81C, 0xA4420101,
+	0x81C, 0xA3440101,
+	0x81C, 0x87460101,
+	0x81C, 0x86480101,
+	0x81C, 0x854A0101,
+	0x81C, 0x844C0101,
+	0x81C, 0x694E0101,
+	0x81C, 0x68500101,
+	0x81C, 0x67520101,
+	0x81C, 0x66540101,
+	0x81C, 0x49560101,
+	0x81C, 0x48580101,
+	0x81C, 0x475A0101,
+	0x81C, 0x2A5C0101,
+	0x81C, 0x295E0101,
+	0x81C, 0x28600101,
+	0x81C, 0x27620101,
+	0x81C, 0x26640101,
+	0x81C, 0x25660101,
+	0x81C, 0x24680101,
+	0x81C, 0x236A0101,
+	0x81C, 0x056C0101,
+	0x81C, 0x046E0101,
+	0x81C, 0x03700101,
+	0x81C, 0x02720101,
+	0xB0000000,	0x00000000,
+	0x81C, 0x01740101,
+	0x81C, 0x01760101,
+	0x81C, 0x01780101,
+	0x81C, 0x017A0101,
+	0x81C, 0x017C0101,
+	0x81C, 0x017E0101,
+	0xC50, 0x00000022,
+	0xC50, 0x00000020,
+};
+
+static const u32 rtw8821a_bb[] = {
+	0x800, 0x0020D090,
+	0x804, 0x080112E0,
+	0x808, 0x0E028211,
+	0x80C, 0x92131111,
+	0x810, 0x20101261,
+	0x814, 0x020C3D10,
+	0x818, 0x03A00385,
+	0x820, 0x00000000,
+	0x824, 0x00030FE0,
+	0x828, 0x00000000,
+	0x82C, 0x002081DD,
+	0x830, 0x2AAAEEC8,
+	0x834, 0x0037A706,
+	0x838, 0x06489B44,
+	0x83C, 0x0000095B,
+	0x840, 0xC0000001,
+	0x844, 0x40003CDE,
+	0x848, 0x62103F8B,
+	0x84C, 0x6CFDFFB8,
+	0x850, 0x28874706,
+	0x854, 0x0001520C,
+	0x858, 0x8060E000,
+	0x85C, 0x74210168,
+	0x860, 0x6929C321,
+	0x864, 0x79727432,
+	0x868, 0x8CA7A314,
+	0x86C, 0x888C2878,
+	0x870, 0x08888888,
+	0x874, 0x31612C2E,
+	0x878, 0x00000152,
+	0x87C, 0x000FD000,
+	0x8A0, 0x00000013,
+	0x8A4, 0x7F7F7F7F,
+	0x8A8, 0xA2000338,
+	0x8AC, 0x0FF0FA0A,
+	0x8B4, 0x000FC080,
+	0x8B8, 0x6C10D7FF,
+	0x8BC, 0x0CA52090,
+	0x8C0, 0x1BF00020,
+	0x8C4, 0x00000000,
+	0x8C8, 0x00013169,
+	0x8CC, 0x08248492,
+	0x8D4, 0x940008A0,
+	0x8D8, 0x290B5612,
+	0x8F8, 0x400002C0,
+	0x8FC, 0x00000000,
+	0x900, 0x00000700,
+	0x90C, 0x00000000,
+	0x910, 0x0000FC00,
+	0x914, 0x00000404,
+	0x918, 0x1C1028C0,
+	0x91C, 0x64B11A1C,
+	0x920, 0xE0767233,
+	0x924, 0x055AA500,
+	0x928, 0x00000004,
+	0x92C, 0xFFFE0000,
+	0x930, 0xFFFFFFFE,
+	0x934, 0x001FFFFF,
+	0x960, 0x00000000,
+	0x964, 0x00000000,
+	0x968, 0x00000000,
+	0x96C, 0x00000000,
+	0x970, 0x801FFFFF,
+	0x974, 0x000003FF,
+	0x978, 0x00000000,
+	0x97C, 0x00000000,
+	0x980, 0x00000000,
+	0x984, 0x00000000,
+	0x988, 0x00000000,
+	0x990, 0x27100000,
+	0x994, 0xFFFF0100,
+	0x998, 0xFFFFFF5C,
+	0x99C, 0xFFFFFFFF,
+	0x9A0, 0x000000FF,
+	0x9A4, 0x00480080,
+	0x9A8, 0x00000000,
+	0x9AC, 0x00000000,
+	0x9B0, 0x81081008,
+	0x9B4, 0x01081008,
+	0x9B8, 0x01081008,
+	0x9BC, 0x01081008,
+	0x9D0, 0x00000000,
+	0x9D4, 0x00000000,
+	0x9D8, 0x00000000,
+	0x9DC, 0x00000000,
+	0x9E0, 0x00005D00,
+	0x9E4, 0x00000003,
+	0x9E8, 0x00000001,
+	0xA00, 0x00D047C8,
+	0xA04, 0x01FF800C,
+	0xA08, 0x8C8A8300,
+	0xA0C, 0x2E68000F,
+	0xA10, 0x9500BB78,
+	0xA14, 0x11144028,
+	0xA18, 0x00881117,
+	0xA1C, 0x89140F00,
+	0xA20, 0x1A1B0000,
+	0xA24, 0x090E1317,
+	0xA28, 0x00000204,
+	0xA2C, 0x00900000,
+	0xA70, 0x101FFF00,
+	0xA74, 0x00000008,
+	0xA78, 0x00000900,
+	0xA7C, 0x225B0606,
+	0xA80, 0x21805490,
+	0xA84, 0x001F0000,
+	0XB00, 0x03100040,
+	0XB04, 0x0000B000,
+	0XB08, 0xAE0201EB,
+	0XB0C, 0x01003207,
+	0XB10, 0x00009807,
+	0XB14, 0x01000000,
+	0XB18, 0x00000002,
+	0XB1C, 0x00000002,
+	0XB20, 0x0000001F,
+	0XB24, 0x03020100,
+	0XB28, 0x07060504,
+	0XB2C, 0x0B0A0908,
+	0XB30, 0x0F0E0D0C,
+	0XB34, 0x13121110,
+	0XB38, 0x17161514,
+	0XB3C, 0x0000003A,
+	0XB40, 0x00000000,
+	0XB44, 0x00000000,
+	0XB48, 0x13000032,
+	0XB4C, 0x48080000,
+	0XB50, 0x00000000,
+	0XB54, 0x00000000,
+	0XB58, 0x00000000,
+	0XB5C, 0x00000000,
+	0xC00, 0x00000007,
+	0xC04, 0x00042020,
+	0xC08, 0x80410231,
+	0xC0C, 0x00000000,
+	0xC10, 0x00000100,
+	0xC14, 0x01000000,
+	0xC1C, 0x40000003,
+	0xC20, 0x2C2C2C2C,
+	0xC24, 0x30303030,
+	0xC28, 0x30303030,
+	0xC2C, 0x2C2C2C2C,
+	0xC30, 0x2C2C2C2C,
+	0xC34, 0x2C2C2C2C,
+	0xC38, 0x2C2C2C2C,
+	0xC3C, 0x2A2A2A2A,
+	0xC40, 0x2A2A2A2A,
+	0xC44, 0x2A2A2A2A,
+	0xC48, 0x2A2A2A2A,
+	0xC4C, 0x2A2A2A2A,
+	0xC50, 0x00000020,
+	0xC54, 0x001C1208,
+	0xC58, 0x30000C1C,
+	0xC5C, 0x00000058,
+	0xC60, 0x34344443,
+	0xC64, 0x07003333,
+	0xC68, 0x19791979,
+	0xC6C, 0x19791979,
+	0xC70, 0x19791979,
+	0xC74, 0x19791979,
+	0xC78, 0x19791979,
+	0xC7C, 0x19791979,
+	0xC80, 0x19791979,
+	0xC84, 0x19791979,
+	0xC94, 0x0100005C,
+	0xC98, 0x00000000,
+	0xC9C, 0x00000000,
+	0xCA0, 0x00000029,
+	0xCA4, 0x08040201,
+	0xCA8, 0x80402010,
+	0xCB0, 0x77775747,
+	0xCB4, 0x10000077,
+	0xCB8, 0x00508240,
+};
+
+static const u32 rtw8821a_rf_a[] = {
+	0x018, 0x0001712A,
+	0x056, 0x00051CF2,
+	0x066, 0x00040000,
+	0x000, 0x00010000,
+	0x01E, 0x00080000,
+	0x082, 0x00000830,
+	0x083, 0x00021800,
+	0x084, 0x00028000,
+	0x085, 0x00048000,
+	0x80000111,	0x00000000,	0x40000000,	0x00000000,
+	0x086, 0x0009483A,
+	0xA0000000,	0x00000000,
+	0x086, 0x00094838,
+	0xB0000000,	0x00000000,
+	0x087, 0x00044980,
+	0x088, 0x00048000,
+	0x089, 0x0000D480,
+	0x08A, 0x00042240,
+	0x08B, 0x000F0380,
+	0x08C, 0x00090000,
+	0x08D, 0x00022852,
+	0x08E, 0x00065540,
+	0x08F, 0x00088001,
+	0x0EF, 0x00020000,
+	0x03E, 0x00000380,
+	0x03F, 0x00090018,
+	0x03E, 0x00020380,
+	0x03F, 0x000A0018,
+	0x03E, 0x00040308,
+	0x03F, 0x000A0018,
+	0x03E, 0x00060018,
+	0x03F, 0x000A0018,
+	0x0EF, 0x00000000,
+	0x018, 0x0001712A,
+	0x089, 0x00000080,
+	0x08B, 0x00080180,
+	0x0EF, 0x00001000,
+	0x03A, 0x00000244,
+	0x03B, 0x00038027,
+	0x03C, 0x00082000,
+	0x03A, 0x00000244,
+	0x03B, 0x00030113,
+	0x03C, 0x00082000,
+	0x03A, 0x0000014C,
+	0x03B, 0x00028027,
+	0x03C, 0x00082000,
+	0x03A, 0x000000CC,
+	0x03B, 0x00027027,
+	0x03C, 0x00042000,
+	0x03A, 0x0000014C,
+	0x03B, 0x0001F913,
+	0x03C, 0x00042000,
+	0x03A, 0x0000010C,
+	0x03B, 0x00017F10,
+	0x03C, 0x00012000,
+	0x03A, 0x000000D0,
+	0x03B, 0x00008027,
+	0x03C, 0x000CA000,
+	0x03A, 0x00000244,
+	0x03B, 0x00078027,
+	0x03C, 0x00082000,
+	0x03A, 0x00000244,
+	0x03B, 0x00070113,
+	0x03C, 0x00082000,
+	0x03A, 0x0000014C,
+	0x03B, 0x00068027,
+	0x03C, 0x00082000,
+	0x03A, 0x000000CC,
+	0x03B, 0x00067027,
+	0x03C, 0x00042000,
+	0x03A, 0x0000014C,
+	0x03B, 0x0005F913,
+	0x03C, 0x00042000,
+	0x03A, 0x0000010C,
+	0x03B, 0x00057F10,
+	0x03C, 0x00012000,
+	0x03A, 0x000000D0,
+	0x03B, 0x00048027,
+	0x03C, 0x000CA000,
+	0x03A, 0x00000244,
+	0x03B, 0x000B8027,
+	0x03C, 0x00082000,
+	0x03A, 0x00000244,
+	0x03B, 0x000B0113,
+	0x03C, 0x00082000,
+	0x03A, 0x0000014C,
+	0x03B, 0x000A8027,
+	0x03C, 0x00082000,
+	0x03A, 0x000000CC,
+	0x03B, 0x000A7027,
+	0x03C, 0x00042000,
+	0x03A, 0x0000014C,
+	0x03B, 0x0009F913,
+	0x03C, 0x00042000,
+	0x03A, 0x0000010C,
+	0x03B, 0x00097F10,
+	0x03C, 0x00012000,
+	0x03A, 0x000000D0,
+	0x03B, 0x00088027,
+	0x03C, 0x000CA000,
+	0x0EF, 0x00000000,
+	0x0EF, 0x00001100,
+	0x80000111,	0x00000000,	0x40000000,	0x00000000,
+	0x034, 0x0004ADF3,
+	0x034, 0x00049DF0,
+	0x90000110,	0x00000000,	0x40000000,	0x00000000,
+	0x034, 0x0004ADF3,
+	0x034, 0x00049DF0,
+	0x90000210,	0x00000000,	0x40000000,	0x00000000,
+	0x034, 0x0004ADF5,
+	0x034, 0x00049DF2,
+	0x9000020c,	0x00000000,	0x40000000,	0x00000000,
+	0x034, 0x0004A0F3,
+	0x034, 0x000490B1,
+	0x9000040c,	0x00000000,	0x40000000,	0x00000000,
+	0x034, 0x0004A0F3,
+	0x034, 0x000490B1,
+	0x90000200,	0x00000000,	0x40000000,	0x00000000,
+	0x034, 0x0004ADF5,
+	0x034, 0x00049DF2,
+	0x90000410,	0x00000000,	0x40000000,	0x00000000,
+	0x034, 0x0004ADF3,
+	0x034, 0x00049DF0,
+	0xA0000000,	0x00000000,
+	0x034, 0x0004ADF7,
+	0x034, 0x00049DF3,
+	0xB0000000,	0x00000000,
+	0x80000111,	0x00000000,	0x40000000,	0x00000000,
+	0x034, 0x00048DED,
+	0x034, 0x00047DEA,
+	0x034, 0x00046DE7,
+	0x034, 0x00045CE9,
+	0x034, 0x00044CE6,
+	0x034, 0x000438C6,
+	0x034, 0x00042886,
+	0x034, 0x00041486,
+	0x034, 0x00040447,
+	0x90000110,	0x00000000,	0x40000000,	0x00000000,
+	0x034, 0x00048DED,
+	0x034, 0x00047DEA,
+	0x034, 0x00046DE7,
+	0x034, 0x00045CE9,
+	0x034, 0x00044CE6,
+	0x034, 0x000438C6,
+	0x034, 0x00042886,
+	0x034, 0x00041486,
+	0x034, 0x00040447,
+	0x9000020c,	0x00000000,	0x40000000,	0x00000000,
+	0x034, 0x000480AE,
+	0x034, 0x000470AB,
+	0x034, 0x0004608B,
+	0x034, 0x00045069,
+	0x034, 0x00044048,
+	0x034, 0x00043045,
+	0x034, 0x00042026,
+	0x034, 0x00041023,
+	0x034, 0x00040002,
+	0x9000040c,	0x00000000,	0x40000000,	0x00000000,
+	0x034, 0x000480AE,
+	0x034, 0x000470AB,
+	0x034, 0x0004608B,
+	0x034, 0x00045069,
+	0x034, 0x00044048,
+	0x034, 0x00043045,
+	0x034, 0x00042026,
+	0x034, 0x00041023,
+	0x034, 0x00040002,
+	0x90000410,	0x00000000,	0x40000000,	0x00000000,
+	0x034, 0x00048DED,
+	0x034, 0x00047DEA,
+	0x034, 0x00046DE7,
+	0x034, 0x00045CE9,
+	0x034, 0x00044CE6,
+	0x034, 0x000438C6,
+	0x034, 0x00042886,
+	0x034, 0x00041486,
+	0x034, 0x00040447,
+	0xA0000000,	0x00000000,
+	0x034, 0x00048DEF,
+	0x034, 0x00047DEC,
+	0x034, 0x00046DE9,
+	0x034, 0x00045CCB,
+	0x034, 0x0004488D,
+	0x034, 0x0004348D,
+	0x034, 0x0004248A,
+	0x034, 0x0004108D,
+	0x034, 0x0004008A,
+	0xB0000000,	0x00000000,
+	0x80000210,	0x00000000,	0x40000000,	0x00000000,
+	0x034, 0x0002ADF4,
+	0x9000020c,	0x00000000,	0x40000000,	0x00000000,
+	0x034, 0x0002A0F3,
+	0x9000040c,	0x00000000,	0x40000000,	0x00000000,
+	0x034, 0x0002A0F3,
+	0x90000200,	0x00000000,	0x40000000,	0x00000000,
+	0x034, 0x0002ADF4,
+	0xA0000000,	0x00000000,
+	0x034, 0x0002ADF7,
+	0xB0000000,	0x00000000,
+	0x80000111,	0x00000000,	0x40000000,	0x00000000,
+	0x034, 0x00029DF4,
+	0x90000110,	0x00000000,	0x40000000,	0x00000000,
+	0x034, 0x00029DF4,
+	0x90000210,	0x00000000,	0x40000000,	0x00000000,
+	0x034, 0x00029DF1,
+	0x9000020c,	0x00000000,	0x40000000,	0x00000000,
+	0x034, 0x000290F0,
+	0x9000040c,	0x00000000,	0x40000000,	0x00000000,
+	0x034, 0x000290F0,
+	0x90000200,	0x00000000,	0x40000000,	0x00000000,
+	0x034, 0x00029DF1,
+	0x90000410,	0x00000000,	0x40000000,	0x00000000,
+	0x034, 0x00029DF4,
+	0xA0000000,	0x00000000,
+	0x034, 0x00029DF2,
+	0xB0000000,	0x00000000,
+	0x80000111,	0x00000000,	0x40000000,	0x00000000,
+	0x034, 0x00028DF1,
+	0x034, 0x00027DEE,
+	0x034, 0x00026DEB,
+	0x034, 0x00025CEC,
+	0x034, 0x00024CE9,
+	0x034, 0x000238CA,
+	0x034, 0x00022889,
+	0x034, 0x00021489,
+	0x034, 0x0002044A,
+	0x90000110,	0x00000000,	0x40000000,	0x00000000,
+	0x034, 0x00028DF1,
+	0x034, 0x00027DEE,
+	0x034, 0x00026DEB,
+	0x034, 0x00025CEC,
+	0x034, 0x00024CE9,
+	0x034, 0x000238CA,
+	0x034, 0x00022889,
+	0x034, 0x00021489,
+	0x034, 0x0002044A,
+	0x9000020c,	0x00000000,	0x40000000,	0x00000000,
+	0x034, 0x000280AF,
+	0x034, 0x000270AC,
+	0x034, 0x0002608B,
+	0x034, 0x00025069,
+	0x034, 0x00024048,
+	0x034, 0x00023045,
+	0x034, 0x00022026,
+	0x034, 0x00021023,
+	0x034, 0x00020002,
+	0x9000040c,	0x00000000,	0x40000000,	0x00000000,
+	0x034, 0x000280AF,
+	0x034, 0x000270AC,
+	0x034, 0x0002608B,
+	0x034, 0x00025069,
+	0x034, 0x00024048,
+	0x034, 0x00023045,
+	0x034, 0x00022026,
+	0x034, 0x00021023,
+	0x034, 0x00020002,
+	0x90000410,	0x00000000,	0x40000000,	0x00000000,
+	0x034, 0x00028DF1,
+	0x034, 0x00027DEE,
+	0x034, 0x00026DEB,
+	0x034, 0x00025CEC,
+	0x034, 0x00024CE9,
+	0x034, 0x000238CA,
+	0x034, 0x00022889,
+	0x034, 0x00021489,
+	0x034, 0x0002044A,
+	0xA0000000,	0x00000000,
+	0x034, 0x00028DEE,
+	0x034, 0x00027DEB,
+	0x034, 0x00026CCD,
+	0x034, 0x00025CCA,
+	0x034, 0x0002488C,
+	0x034, 0x0002384C,
+	0x034, 0x00022849,
+	0x034, 0x00021449,
+	0x034, 0x0002004D,
+	0xB0000000,	0x00000000,
+	0x8000020c,	0x00000000,	0x40000000,	0x00000000,
+	0x034, 0x0000A0D7,
+	0x034, 0x000090D3,
+	0x034, 0x000080B1,
+	0x034, 0x000070AE,
+	0x9000040c,	0x00000000,	0x40000000,	0x00000000,
+	0x034, 0x0000A0D7,
+	0x034, 0x000090D3,
+	0x034, 0x000080B1,
+	0x034, 0x000070AE,
+	0xA0000000,	0x00000000,
+	0x034, 0x0000ADF7,
+	0x034, 0x00009DF4,
+	0x034, 0x00008DF1,
+	0x034, 0x00007DEE,
+	0xB0000000,	0x00000000,
+	0x80000111,	0x00000000,	0x40000000,	0x00000000,
+	0x034, 0x00006DEB,
+	0x034, 0x00005CEC,
+	0x034, 0x00004CE9,
+	0x034, 0x000038CA,
+	0x034, 0x00002889,
+	0x034, 0x00001489,
+	0x034, 0x0000044A,
+	0x90000110,	0x00000000,	0x40000000,	0x00000000,
+	0x034, 0x00006DEB,
+	0x034, 0x00005CEC,
+	0x034, 0x00004CE9,
+	0x034, 0x000038CA,
+	0x034, 0x00002889,
+	0x034, 0x00001489,
+	0x034, 0x0000044A,
+	0x9000020c,	0x00000000,	0x40000000,	0x00000000,
+	0x034, 0x0000608D,
+	0x034, 0x0000506B,
+	0x034, 0x0000404A,
+	0x034, 0x00003047,
+	0x034, 0x00002044,
+	0x034, 0x00001025,
+	0x034, 0x00000004,
+	0x9000040c,	0x00000000,	0x40000000,	0x00000000,
+	0x034, 0x0000608D,
+	0x034, 0x0000506B,
+	0x034, 0x0000404A,
+	0x034, 0x00003047,
+	0x034, 0x00002044,
+	0x034, 0x00001025,
+	0x034, 0x00000004,
+	0x90000410,	0x00000000,	0x40000000,	0x00000000,
+	0x034, 0x00006DEB,
+	0x034, 0x00005CEC,
+	0x034, 0x00004CE9,
+	0x034, 0x000038CA,
+	0x034, 0x00002889,
+	0x034, 0x00001489,
+	0x034, 0x0000044A,
+	0xA0000000,	0x00000000,
+	0x034, 0x00006DCD,
+	0x034, 0x00005CCD,
+	0x034, 0x00004CCA,
+	0x034, 0x0000388C,
+	0x034, 0x00002888,
+	0x034, 0x00001488,
+	0x034, 0x00000486,
+	0xB0000000,	0x00000000,
+	0x0EF, 0x00000000,
+	0x018, 0x0001712A,
+	0x0EF, 0x00000040,
+	0x80000111,	0x00000000,	0x40000000,	0x00000000,
+	0x035, 0x00000187,
+	0x035, 0x00008187,
+	0x035, 0x00010187,
+	0x035, 0x00020188,
+	0x035, 0x00028188,
+	0x035, 0x00030188,
+	0x035, 0x00040188,
+	0x035, 0x00048188,
+	0x035, 0x00050188,
+	0x90000110,	0x00000000,	0x40000000,	0x00000000,
+	0x035, 0x00000187,
+	0x035, 0x00008187,
+	0x035, 0x00010187,
+	0x035, 0x00020188,
+	0x035, 0x00028188,
+	0x035, 0x00030188,
+	0x035, 0x00040188,
+	0x035, 0x00048188,
+	0x035, 0x00050188,
+	0x90000210,	0x00000000,	0x40000000,	0x00000000,
+	0x035, 0x00000128,
+	0x035, 0x00008128,
+	0x035, 0x00010128,
+	0x035, 0x000201C8,
+	0x035, 0x000281C8,
+	0x035, 0x000301C8,
+	0x035, 0x000401C8,
+	0x035, 0x000481C8,
+	0x035, 0x000501C8,
+	0x9000040c,	0x00000000,	0x40000000,	0x00000000,
+	0x035, 0x00000145,
+	0x035, 0x00008145,
+	0x035, 0x00010145,
+	0x035, 0x00020196,
+	0x035, 0x00028196,
+	0x035, 0x00030196,
+	0x035, 0x000401C7,
+	0x035, 0x000481C7,
+	0x035, 0x000501C7,
+	0x90000200,	0x00000000,	0x40000000,	0x00000000,
+	0x035, 0x00000128,
+	0x035, 0x00008128,
+	0x035, 0x00010128,
+	0x035, 0x000201C8,
+	0x035, 0x000281C8,
+	0x035, 0x000301C8,
+	0x035, 0x000401C8,
+	0x035, 0x000481C8,
+	0x035, 0x000501C8,
+	0x90000410,	0x00000000,	0x40000000,	0x00000000,
+	0x035, 0x00000187,
+	0x035, 0x00008187,
+	0x035, 0x00010187,
+	0x035, 0x00020188,
+	0x035, 0x00028188,
+	0x035, 0x00030188,
+	0x035, 0x00040188,
+	0x035, 0x00048188,
+	0x035, 0x00050188,
+	0xA0000000,	0x00000000,
+	0x035, 0x00000145,
+	0x035, 0x00008145,
+	0x035, 0x00010145,
+	0x035, 0x00020196,
+	0x035, 0x00028196,
+	0x035, 0x00030196,
+	0x035, 0x000401C7,
+	0x035, 0x000481C7,
+	0x035, 0x000501C7,
+	0xB0000000,	0x00000000,
+	0x0EF, 0x00000000,
+	0x018, 0x0001712A,
+	0x0EF, 0x00000010,
+	0x80000111,	0x00000000,	0x40000000,	0x00000000,
+	0x036, 0x00085733,
+	0x036, 0x0008D733,
+	0x036, 0x00095733,
+	0x036, 0x0009D733,
+	0x036, 0x000A64B4,
+	0x036, 0x000AE4B4,
+	0x036, 0x000B64B4,
+	0x036, 0x000BE4B4,
+	0x036, 0x000C64B4,
+	0x036, 0x000CE4B4,
+	0x036, 0x000D64B4,
+	0x036, 0x000DE4B4,
+	0x90000110,	0x00000000,	0x40000000,	0x00000000,
+	0x036, 0x00085733,
+	0x036, 0x0008D733,
+	0x036, 0x00095733,
+	0x036, 0x0009D733,
+	0x036, 0x000A64B4,
+	0x036, 0x000AE4B4,
+	0x036, 0x000B64B4,
+	0x036, 0x000BE4B4,
+	0x036, 0x000C64B4,
+	0x036, 0x000CE4B4,
+	0x036, 0x000D64B4,
+	0x036, 0x000DE4B4,
+	0x90000210,	0x00000000,	0x40000000,	0x00000000,
+	0x036, 0x000063B5,
+	0x036, 0x0000E3B5,
+	0x036, 0x000163B5,
+	0x036, 0x0001E3B5,
+	0x036, 0x000263B5,
+	0x036, 0x0002E3B5,
+	0x036, 0x000363B5,
+	0x036, 0x0003E3B5,
+	0x036, 0x000463B5,
+	0x036, 0x0004E3B5,
+	0x036, 0x000563B5,
+	0x036, 0x0005E3B5,
+	0x9000040c,	0x00000000,	0x40000000,	0x00000000,
+	0x036, 0x000056B3,
+	0x036, 0x0000D6B3,
+	0x036, 0x000156B3,
+	0x036, 0x0001D6B3,
+	0x036, 0x00026634,
+	0x036, 0x0002E634,
+	0x036, 0x00036634,
+	0x036, 0x0003E634,
+	0x036, 0x000467B4,
+	0x036, 0x0004E7B4,
+	0x036, 0x000567B4,
+	0x036, 0x0005E7B4,
+	0x90000200,	0x00000000,	0x40000000,	0x00000000,
+	0x036, 0x000063B5,
+	0x036, 0x0000E3B5,
+	0x036, 0x000163B5,
+	0x036, 0x0001E3B5,
+	0x036, 0x000263B5,
+	0x036, 0x0002E3B5,
+	0x036, 0x000363B5,
+	0x036, 0x0003E3B5,
+	0x036, 0x000463B5,
+	0x036, 0x0004E3B5,
+	0x036, 0x000563B5,
+	0x036, 0x0005E3B5,
+	0x90000410,	0x00000000,	0x40000000,	0x00000000,
+	0x036, 0x00085733,
+	0x036, 0x0008D733,
+	0x036, 0x00095733,
+	0x036, 0x0009D733,
+	0x036, 0x000A64B4,
+	0x036, 0x000AE4B4,
+	0x036, 0x000B64B4,
+	0x036, 0x000BE4B4,
+	0x036, 0x000C64B4,
+	0x036, 0x000CE4B4,
+	0x036, 0x000D64B4,
+	0x036, 0x000DE4B4,
+	0xA0000000,	0x00000000,
+	0x036, 0x000056B3,
+	0x036, 0x0000D6B3,
+	0x036, 0x000156B3,
+	0x036, 0x0001D6B3,
+	0x036, 0x00026634,
+	0x036, 0x0002E634,
+	0x036, 0x00036634,
+	0x036, 0x0003E634,
+	0x036, 0x000467B4,
+	0x036, 0x0004E7B4,
+	0x036, 0x000567B4,
+	0x036, 0x0005E7B4,
+	0xB0000000,	0x00000000,
+	0x0EF, 0x00000000,
+	0x0EF, 0x00000008,
+	0x80000111,	0x00000000,	0x40000000,	0x00000000,
+	0x03C, 0x000001C8,
+	0x03C, 0x00000492,
+	0x90000110,	0x00000000,	0x40000000,	0x00000000,
+	0x03C, 0x000001C8,
+	0x03C, 0x00000492,
+	0x90000210,	0x00000000,	0x40000000,	0x00000000,
+	0x03C, 0x000001B6,
+	0x03C, 0x00000492,
+	0x9000040c,	0x00000000,	0x40000000,	0x00000000,
+	0x03C, 0x0000022A,
+	0x03C, 0x00000594,
+	0x90000200,	0x00000000,	0x40000000,	0x00000000,
+	0x03C, 0x000001B6,
+	0x03C, 0x00000492,
+	0x90000410,	0x00000000,	0x40000000,	0x00000000,
+	0x03C, 0x000001C8,
+	0x03C, 0x00000492,
+	0xA0000000,	0x00000000,
+	0x03C, 0x0000022A,
+	0x03C, 0x00000594,
+	0xB0000000,	0x00000000,
+	0x80000111,	0x00000000,	0x40000000,	0x00000000,
+	0x03C, 0x00000800,
+	0x90000110,	0x00000000,	0x40000000,	0x00000000,
+	0x03C, 0x00000800,
+	0x90000210,	0x00000000,	0x40000000,	0x00000000,
+	0x03C, 0x00000800,
+	0x9000020c,	0x00000000,	0x40000000,	0x00000000,
+	0x03C, 0x00000820,
+	0x9000040c,	0x00000000,	0x40000000,	0x00000000,
+	0x03C, 0x00000820,
+	0x90000200,	0x00000000,	0x40000000,	0x00000000,
+	0x03C, 0x00000800,
+	0x90000410,	0x00000000,	0x40000000,	0x00000000,
+	0x03C, 0x00000800,
+	0xA0000000,	0x00000000,
+	0x03C, 0x00000900,
+	0xB0000000,	0x00000000,
+	0x0EF, 0x00000000,
+	0x018, 0x0001712A,
+	0x0EF, 0x00000002,
+	0x80000111,	0x00000000,	0x40000000,	0x00000000,
+	0x008, 0x0004E400,
+	0x90000110,	0x00000000,	0x40000000,	0x00000000,
+	0x008, 0x0004E400,
+	0x90000210,	0x00000000,	0x40000000,	0x00000000,
+	0x008, 0x00002000,
+	0x9000020c,	0x00000000,	0x40000000,	0x00000000,
+	0x008, 0x00002000,
+	0x9000040c,	0x00000000,	0x40000000,	0x00000000,
+	0x008, 0x00002000,
+	0x90000200,	0x00000000,	0x40000000,	0x00000000,
+	0x008, 0x00002000,
+	0x90000410,	0x00000000,	0x40000000,	0x00000000,
+	0x008, 0x0004E400,
+	0xA0000000,	0x00000000,
+	0x008, 0x00002000,
+	0xB0000000,	0x00000000,
+	0x0EF, 0x00000000,
+	0x0DF, 0x000000C0,
+	0x01F, 0x00000064,
+	0x80000111,	0x00000000,	0x40000000,	0x00000000,
+	0x058, 0x000A7284,
+	0x059, 0x000600EC,
+	0x90000110,	0x00000000,	0x40000000,	0x00000000,
+	0x058, 0x000A7284,
+	0x059, 0x000600EC,
+	0x9000020c,	0x00000000,	0x40000000,	0x00000000,
+	0x058, 0x00081184,
+	0x059, 0x0006016C,
+	0x9000040c,	0x00000000,	0x40000000,	0x00000000,
+	0x058, 0x00081184,
+	0x059, 0x0006016C,
+	0x90000200,	0x00000000,	0x40000000,	0x00000000,
+	0x058, 0x00081184,
+	0x059, 0x0006016C,
+	0x90000410,	0x00000000,	0x40000000,	0x00000000,
+	0x058, 0x000A7284,
+	0x059, 0x000600EC,
+	0xA0000000,	0x00000000,
+	0x058, 0x00081184,
+	0x059, 0x0006016C,
+	0xB0000000,	0x00000000,
+	0x80000111,	0x00000000,	0x40000000,	0x00000000,
+	0x061, 0x000E8D73,
+	0x062, 0x00093FC5,
+	0x90000110,	0x00000000,	0x40000000,	0x00000000,
+	0x061, 0x000E8D73,
+	0x062, 0x00093FC5,
+	0x90000210,	0x00000000,	0x40000000,	0x00000000,
+	0x061, 0x000EFD83,
+	0x062, 0x00093FCC,
+	0x9000040c,	0x00000000,	0x40000000,	0x00000000,
+	0x061, 0x000EAD53,
+	0x062, 0x00093BC4,
+	0x90000200,	0x00000000,	0x40000000,	0x00000000,
+	0x061, 0x000EFD83,
+	0x062, 0x00093FCC,
+	0x90000410,	0x00000000,	0x40000000,	0x00000000,
+	0x061, 0x000E8D73,
+	0x062, 0x00093FC5,
+	0xA0000000,	0x00000000,
+	0x061, 0x000EAD53,
+	0x062, 0x00093BC4,
+	0xB0000000,	0x00000000,
+	0x80000111,	0x00000000,	0x40000000,	0x00000000,
+	0x063, 0x000110E9,
+	0x90000110,	0x00000000,	0x40000000,	0x00000000,
+	0x063, 0x000110E9,
+	0x90000210,	0x00000000,	0x40000000,	0x00000000,
+	0x063, 0x000110EB,
+	0x9000020c,	0x00000000,	0x40000000,	0x00000000,
+	0x063, 0x000110E9,
+	0x9000040c,	0x00000000,	0x40000000,	0x00000000,
+	0x063, 0x000110E9,
+	0x90000200,	0x00000000,	0x40000000,	0x00000000,
+	0x063, 0x000110EB,
+	0x90000410,	0x00000000,	0x40000000,	0x00000000,
+	0x063, 0x000110E9,
+	0xA0000000,	0x00000000,
+	0x063, 0x000714E9,
+	0xB0000000,	0x00000000,
+	0x80000111,	0x00000000,	0x40000000,	0x00000000,
+	0x064, 0x0001C27C,
+	0x90000110,	0x00000000,	0x40000000,	0x00000000,
+	0x064, 0x0001C27C,
+	0x90000210,	0x00000000,	0x40000000,	0x00000000,
+	0x064, 0x0001C27C,
+	0x9000040c,	0x00000000,	0x40000000,	0x00000000,
+	0x064, 0x0001C67C,
+	0x90000200,	0x00000000,	0x40000000,	0x00000000,
+	0x064, 0x0001C27C,
+	0x90000410,	0x00000000,	0x40000000,	0x00000000,
+	0x064, 0x0001C27C,
+	0xA0000000,	0x00000000,
+	0x064, 0x0001C67C,
+	0xB0000000,	0x00000000,
+	0x80000111,	0x00000000,	0x40000000,	0x00000000,
+	0x065, 0x00091016,
+	0x90000110,	0x00000000,	0x40000000,	0x00000000,
+	0x065, 0x00091016,
+	0x90000210,	0x00000000,	0x40000000,	0x00000000,
+	0x065, 0x00093016,
+	0x9000020c,	0x00000000,	0x40000000,	0x00000000,
+	0x065, 0x00093015,
+	0x9000040c,	0x00000000,	0x40000000,	0x00000000,
+	0x065, 0x00093015,
+	0x90000200,	0x00000000,	0x40000000,	0x00000000,
+	0x065, 0x00093016,
+	0xA0000000,	0x00000000,
+	0x065, 0x00091016,
+	0xB0000000,	0x00000000,
+	0x018, 0x00000006,
+	0x0EF, 0x00002000,
+	0x03B, 0x0003824B,
+	0x03B, 0x0003024B,
+	0x03B, 0x0002844B,
+	0x03B, 0x00020F4B,
+	0x03B, 0x00018F4B,
+	0x03B, 0x000104B2,
+	0x03B, 0x00008049,
+	0x03B, 0x00000148,
+	0x03B, 0x0007824B,
+	0x03B, 0x0007024B,
+	0x03B, 0x0006824B,
+	0x03B, 0x00060F4B,
+	0x03B, 0x00058F4B,
+	0x03B, 0x000504B2,
+	0x03B, 0x00048049,
+	0x03B, 0x00040148,
+	0x0EF, 0x00000000,
+	0x0EF, 0x00000100,
+	0x034, 0x0000ADF3,
+	0x034, 0x00009DF0,
+	0x034, 0x00008D70,
+	0x034, 0x00007D6D,
+	0x034, 0x00006CEE,
+	0x034, 0x00005CCC,
+	0x034, 0x000044EC,
+	0x034, 0x000034AC,
+	0x034, 0x0000246D,
+	0x034, 0x0000106F,
+	0x034, 0x0000006C,
+	0x0EF, 0x00000000,
+	0x0ED, 0x00000010,
+	0x044, 0x0000ADF2,
+	0x044, 0x00009DEF,
+	0x044, 0x00008DEC,
+	0x044, 0x00007DE9,
+	0x044, 0x00006CEC,
+	0x044, 0x00005CE9,
+	0x044, 0x000044EC,
+	0x044, 0x000034E9,
+	0x044, 0x0000246C,
+	0x044, 0x00001469,
+	0x044, 0x0000006C,
+	0x0ED, 0x00000000,
+	0x0ED, 0x00000001,
+	0x040, 0x00038DA7,
+	0x040, 0x000300C2,
+	0x040, 0x000288E2,
+	0x040, 0x000200B8,
+	0x040, 0x000188A5,
+	0x040, 0x00010FBC,
+	0x040, 0x00008F71,
+	0x040, 0x00000240,
+	0x0ED, 0x00000000,
+	0x0EF, 0x000020A2,
+	0x0DF, 0x00000080,
+	0x035, 0x00000120,
+	0x035, 0x00008120,
+	0x035, 0x00010120,
+	0x036, 0x00000085,
+	0x036, 0x00008085,
+	0x036, 0x00010085,
+	0x036, 0x00018085,
+	0x0EF, 0x00000000,
+	0x051, 0x00000C31,
+	0x052, 0x00000622,
+	0x053, 0x000FC70B,
+	0x054, 0x0000017E,
+	0x056, 0x00051DF3,
+	0x051, 0x00000C01,
+	0x052, 0x000006D6,
+	0x053, 0x000FC649,
+	0x070, 0x00049661,
+	0x071, 0x0007843E,
+	0x072, 0x00000382,
+	0x074, 0x00051400,
+	0x035, 0x00000160,
+	0x035, 0x00008160,
+	0x035, 0x00010160,
+	0x036, 0x00000124,
+	0x036, 0x00008124,
+	0x036, 0x00010124,
+	0x036, 0x00018124,
+	0x0ED, 0x0000000C,
+	0x045, 0x00000140,
+	0x045, 0x00008140,
+	0x045, 0x00010140,
+	0x046, 0x00000124,
+	0x046, 0x00008124,
+	0x046, 0x00010124,
+	0x046, 0x00018124,
+	0x0DF, 0x00000088,
+	0x0B3, 0x000F0E18,
+	0x0B4, 0x0001214C,
+	0x0B7, 0x0003000C,
+	0x01C, 0x000539D2,
+	0x0C4, 0x000AFE00,
+	0x018, 0x0001F12A,
+	0xFFE, 0x00000000,
+	0xFFE, 0x00000000,
+	0x018, 0x0001712A,
+};
+
+static const struct rtw_pwr_seq_cmd trans_carddis_to_cardemu_8821a[] = {
+	{0x0005,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_ALL_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_WRITE, BIT(3) | BIT(7), 0},
+	{0x0086,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_SDIO_MSK,
+	 RTW_PWR_ADDR_SDIO,
+	 RTW_PWR_CMD_WRITE, BIT(0), 0},
+	{0x0086,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_SDIO_MSK,
+	 RTW_PWR_ADDR_SDIO,
+	 RTW_PWR_CMD_POLLING, BIT(1), BIT(1)},
+	{0x004A,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_USB_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_WRITE, BIT(0), 0},
+	{0x0005,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_ALL_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_WRITE, BIT(3) | BIT(4), 0},
+	{0x0023,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_SDIO_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_WRITE, BIT(4), 0},
+	{0x0301,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_PCI_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_WRITE, 0xFF, 0},
+	{0xFFFF,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_ALL_MSK,
+	 0,
+	 RTW_PWR_CMD_END, 0, 0},
+};
+
+static const struct rtw_pwr_seq_cmd trans_cardemu_to_act_8821a[] = {
+	{0x0020,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_USB_MSK | RTW_PWR_INTF_SDIO_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_WRITE, BIT(0), BIT(0)},
+	{0x0067,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_USB_MSK | RTW_PWR_INTF_SDIO_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_WRITE, BIT(4), 0},
+	{0x0001,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_USB_MSK | RTW_PWR_INTF_SDIO_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_DELAY, 1, RTW_PWR_DELAY_MS},
+	{0x0000,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_USB_MSK | RTW_PWR_INTF_SDIO_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_WRITE, BIT(5), 0},
+	{0x0005,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_ALL_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_WRITE, BIT(4) | BIT(3) | BIT(2), 0},
+	{0x0075,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_PCI_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_WRITE, BIT(0), BIT(0)},
+	{0x0006,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_ALL_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_POLLING, BIT(1), BIT(1)},
+	{0x0075,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_PCI_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_WRITE, BIT(0), 0},
+	{0x0006,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_ALL_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_WRITE, BIT(0), BIT(0)},
+	{0x0005,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_ALL_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_WRITE, BIT(7), 0},
+	{0x0005,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_ALL_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_WRITE, BIT(4) | BIT(3), 0},
+	{0x0005,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_ALL_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_WRITE, BIT(0), BIT(0)},
+	{0x0005,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_ALL_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_POLLING, BIT(0), 0},
+	{0x004F,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_ALL_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_WRITE, BIT(0), BIT(0)},
+	{0x0067,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_ALL_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_WRITE, BIT(5) | BIT(4), BIT(5) | BIT(4)},
+	{0x0025,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_ALL_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_WRITE, BIT(6), 0},
+	{0x0049,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_ALL_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_WRITE, BIT(1), BIT(1)},
+	{0x0063,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_ALL_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_WRITE, BIT(1), BIT(1)},
+	{0x0062,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_ALL_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_WRITE, BIT(1), 0},
+	{0x0058,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_ALL_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_WRITE, BIT(0), BIT(0)},
+	{0x005A,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_ALL_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_WRITE, BIT(1), BIT(1)},
+	{0x002E,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_ALL_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_WRITE, 0xFF, 0x82},
+	{0x0010,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_ALL_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_WRITE, BIT(6), BIT(6)},
+	{0xFFFF,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_ALL_MSK,
+	 0,
+	 RTW_PWR_CMD_END, 0, 0},
+};
+
+static const struct rtw_pwr_seq_cmd trans_act_to_lps_8821a[] = {
+	{0x0301,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_PCI_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_WRITE, 0xFF, 0xFF},
+	{0x0522,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_ALL_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_WRITE, 0xFF, 0xFF},
+	{0x05F8,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_ALL_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_POLLING, 0xFF, 0},
+	{0x05F9,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_ALL_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_POLLING, 0xFF, 0},
+	{0x05FA,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_ALL_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_POLLING, 0xFF, 0},
+	{0x05FB,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_ALL_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_POLLING, 0xFF, 0},
+	{0x0002,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_ALL_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_WRITE, BIT(0), 0},
+	{0x0002,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_ALL_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_DELAY, 0, RTW_PWR_DELAY_US},
+	{0x0002,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_ALL_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_WRITE, BIT(1), 0},
+	{0x0100,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_ALL_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_WRITE, 0xFF, 0x03},
+	{0x0101,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_ALL_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_WRITE, BIT(1), 0},
+	{0x0093,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_SDIO_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_WRITE, 0xFF, 0x00},
+	{0x0553,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_ALL_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_WRITE, BIT(5), BIT(5)},
+	{0xFFFF,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_ALL_MSK,
+	 0,
+	 RTW_PWR_CMD_END, 0, 0},
+};
+
+static const struct rtw_pwr_seq_cmd trans_act_to_cardemu_8821a[] = {
+	{0x001F,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_ALL_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_WRITE, 0xFF, 0},
+	{0x004F,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_ALL_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_WRITE, BIT(0), 0},
+	{0x0049,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_ALL_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_WRITE, BIT(1), 0},
+	{0x0006,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_USB_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_WRITE, BIT(0), BIT(0)},
+	{0x0005,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_ALL_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_WRITE, BIT(1), BIT(1)},
+	{0x0005,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_ALL_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_POLLING, BIT(1), 0},
+	{0x0000,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_USB_MSK | RTW_PWR_INTF_SDIO_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_WRITE, BIT(5), BIT(5)},
+	{0x0020,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_USB_MSK | RTW_PWR_INTF_SDIO_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_WRITE, BIT(0), 0},
+	{0xFFFF,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_ALL_MSK,
+	 0,
+	 RTW_PWR_CMD_END, 0, 0},
+};
+
+static const struct rtw_pwr_seq_cmd trans_cardemu_to_carddis_8821a[] = {
+	{0x0007,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_SDIO_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_WRITE, 0xFF, 0x20},
+	{0x0005,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_USB_MSK | RTW_PWR_INTF_SDIO_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_WRITE, BIT(3) | BIT(4), BIT(3)},
+	{0x0005,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_PCI_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_WRITE, BIT(2), BIT(2)},
+	{0x004A,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_USB_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_WRITE, BIT(0), 1},
+	{0x0023,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_SDIO_MSK,
+	 RTW_PWR_ADDR_MAC,
+	 RTW_PWR_CMD_WRITE, BIT(4), BIT(4)},
+	{0x0086,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_SDIO_MSK,
+	 RTW_PWR_ADDR_SDIO,
+	 RTW_PWR_CMD_WRITE, BIT(0), BIT(0)},
+	{0x0086,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_SDIO_MSK,
+	 RTW_PWR_ADDR_SDIO,
+	 RTW_PWR_CMD_POLLING, BIT(1), 0},
+	{0xFFFF,
+	 RTW_PWR_CUT_ALL_MSK,
+	 RTW_PWR_INTF_ALL_MSK,
+	 0,
+	 RTW_PWR_CMD_END, 0, 0},
+};
+
+const struct rtw_pwr_seq_cmd * const card_enable_flow_8821a[] = {
+	trans_carddis_to_cardemu_8821a,
+	trans_cardemu_to_act_8821a,
+	NULL
+};
+
+const struct rtw_pwr_seq_cmd * const enter_lps_flow_8821a[] = {
+	trans_act_to_lps_8821a,
+	NULL
+};
+
+const struct rtw_pwr_seq_cmd * const card_disable_flow_8821a[] = {
+	trans_act_to_cardemu_8821a,
+	trans_cardemu_to_carddis_8821a,
+	NULL
+};

+ 1 - 1
usb.c

@@ -105,7 +105,7 @@ static unsigned int generation;
 static unsigned int next_addr[USB_MAX_CTRL];
 static const struct usb_hcd_ops *slot_ops[USB_HCD_INT_SLOTS];
 static usb_probe_fn current_probe;
-#define USB_MAX_DEVICE_PROBES 4
+#define USB_MAX_DEVICE_PROBES 6
 static usb_device_probe_fn device_probes[USB_MAX_DEVICE_PROBES];
 static unsigned int n_device_probes;
 static int print_devices;

+ 39 - 39
wifi.c

@@ -4,7 +4,9 @@
  * parsed here; the RSN IE for the association request and the whole WPA2
  * 4-way/group handshake come from the ring-3 `wifi` command (wpa.c), which
  * exchanges EAPOL frames through SYS_WIFI_EAPOL_RX/TX and installs the
- * TK/GTK with SYS_WIFI_SET_KEY.  CCMP runs in the firmware.
+ * TK/GTK with SYS_WIFI_SET_KEY.  CCMP TX runs in the Intel firmware or in
+ * rtl8821au.c; CCMP RX is checked here and decrypted in software when the
+ * device did not (ccmp.c).
  *
  * Data frames are non-QoS (the association request has no WMM/HT IE), so
  * every frame is a plain 24-byte-header 802.11 data frame + LLC/SNAP.
@@ -12,9 +14,9 @@
 
 #include "wifi.h"
 #include "ccmp.h"
-#include "iwl_fw.h"
-#include "iwl_mvm.h"   /* IWL_RX_* status bits, wifi_rx_frame() upcalls */
-#include "iwl_trans.h"
+#include "iwl_mvm.h"   /* IWL_RX_* status bits */
+#include "iwl_trans.h" /* iwl_memcpy/memzero/udelay */
+#include "wifi_fw.h"
 #include "io.h"
 #include "mm.h"
 #include "net.h"
@@ -118,12 +120,13 @@ static int alloc_buffers(void)
  * connect runs the whole bring-up again (it also clears the fatal flag). */
 static void wifi_check_crash(void)
 {
-	if (W.state == SYS_WIFI_ST_DOWN || !iwl_fatal())
+	if (W.state == SYS_WIFI_ST_DOWN || !wififw_fatal())
 		return;
-	serial_puts(SERIAL_COM1, "wifi: firmware crashed, reloading on next use\n");
-	iwlfw_down();
+	serial_puts(SERIAL_COM1, "wifi: device died (firmware crash or unplug), "
+		    "reloading on next use\n");
+	wififw_down();
 	W.state = SYS_WIFI_ST_DOWN;
-	W.detail = "firmware crashed; reload on next use";
+	W.detail = "device died; reload on next use";
 }
 
 static int wifi_up(void)
@@ -134,8 +137,8 @@ static int wifi_up(void)
 		W.detail = "out of memory";
 		return -1;
 	}
-	if (iwlfw_up(W.mac) < 0) {
-		W.detail = iwlfw_state();
+	if (wififw_up(W.mac) < 0) {
+		W.detail = wififw_state();
 		return -1;
 	}
 	W.present = 1;
@@ -264,7 +267,7 @@ static void rx_mgmt(const unsigned char *f, unsigned int len, int rssi,
 			serial_puts(SERIAL_COM1, "\n");
 			W.state = SYS_WIFI_ST_IDLE;
 			W.detail = "deauthenticated by AP";
-			iwlfw_disconnect();
+			wififw_disconnect();
 		}
 	}
 }
@@ -530,7 +533,7 @@ static int tx_data(const unsigned char *dst, unsigned int ethertype,
 	f[o++] = ethertype >> 8;
 	f[o++] = ethertype;
 	wmemcpy(f + o, payload, plen);
-	if (iwlfw_tx_data(f, o + plen, prot) < 0)
+	if (wififw_tx_data(f, o + plen, prot) < 0)
 		return -1;
 	W.tx_frames++;
 	return 0;
@@ -549,7 +552,7 @@ static int tx_mgmt_frame(unsigned int stype, const unsigned char *body,
 	wmemcpy(f + 10, W.mac, 6);
 	wmemcpy(f + 16, W.cur.bssid, 6);
 	wmemcpy(f + 24, body, blen);
-	return iwlfw_tx_mgmt(f, 24 + blen);
+	return wififw_tx_mgmt(f, 24 + blen);
 }
 
 static int wait_flag(int *flag, unsigned int ms)
@@ -557,7 +560,7 @@ static int wait_flag(int *flag, unsigned int ms)
 	unsigned int t;
 
 	for (t = 0; t < ms && !*flag; t++) {
-		if (iwlfw_poll() < 0)
+		if (wififw_poll() < 0)
 			return -1;
 		if (!*flag)
 			iwl_udelay(1000);
@@ -578,7 +581,7 @@ static int do_connect(const struct sys_wifi_connect *c)
 	if (W.state == SYS_WIFI_ST_DOWN && wifi_up() < 0)
 		return -1;
 	if (W.state >= SYS_WIFI_ST_ASSOC) {
-		iwlfw_disconnect();
+		wififw_disconnect();
 		W.state = SYS_WIFI_ST_IDLE;
 	}
 	if (c->ssid_len > 32 || c->ie_len > sizeof(c->ie) || !c->channel)
@@ -602,7 +605,7 @@ static int do_connect(const struct sys_wifi_connect *c)
 	W.sw_rx_log = W.sw_fail_log = 0;
 	W.ptk_conn = 0;
 
-	if (iwlfw_prepare_connect(c->bssid, c->channel, W.cur.beacon_int) < 0) {
+	if (wififw_prepare_connect(c->bssid, c->channel, W.cur.beacon_int) < 0) {
 		W.detail = "firmware connect setup failed";
 		return -1;
 	}
@@ -620,7 +623,7 @@ static int do_connect(const struct sys_wifi_connect *c)
 	if (!W.auth_done || W.auth_status) {
 		W.detail = W.auth_done ? "authentication refused"
 				       : "no authentication reply";
-		iwlfw_disconnect();
+		wififw_disconnect();
 		return -1;
 	}
 	/* Association request: capability, listen interval, SSID, rates,
@@ -659,12 +662,12 @@ static int do_connect(const struct sys_wifi_connect *c)
 			serial_putdec(SERIAL_COM1, W.assoc_status);
 			serial_puts(SERIAL_COM1, "\n");
 		}
-		iwlfw_disconnect();
+		wififw_disconnect();
 		return -1;
 	}
-	if (iwlfw_set_assoc(W.aid, W.cur.beacon_int, W.cur.dtim) < 0) {
+	if (wififw_set_assoc(W.aid, W.cur.beacon_int, W.cur.dtim) < 0) {
 		W.detail = "firmware association update failed";
-		iwlfw_disconnect();
+		wififw_disconnect();
 		return -1;
 	}
 	W.state = SYS_WIFI_ST_ASSOC;
@@ -683,8 +686,8 @@ static void do_disconnect(void)
 	W.ptk_conn = 0;
 	if (W.state >= SYS_WIFI_ST_ASSOC) {
 		tx_mgmt_frame(12, reason, 2);
-		iwlfw_poll();
-		iwlfw_disconnect();
+		wififw_poll();
+		wififw_disconnect();
 	}
 	if (W.state != SYS_WIFI_ST_DOWN)
 		W.state = SYS_WIFI_ST_IDLE;
@@ -698,7 +701,7 @@ static int nd_init(void)
 		return 0;
 	/* Boot probe: find the card only.  The firmware is loaded on first
 	 * use (`wifi`), so boot time and RAM are unchanged without WiFi. */
-	if (iwl_probe() < 0)
+	if (wififw_probe() < 0)
 		return -1;
 	W.present = 1;
 	W.detail = "firmware not loaded";
@@ -719,7 +722,7 @@ static int nd_rx(unsigned char *buf, unsigned int maxlen)
 	if (W.state == SYS_WIFI_ST_DOWN)
 		return -1;
 	if (W.rxq_head == W.rxq_tail)
-		iwlfw_poll();
+		wififw_poll();
 	if (W.rxq_head == W.rxq_tail)
 		return -1;
 	s = &W.rxq[W.rxq_tail];
@@ -770,7 +773,7 @@ static void nd_print_info(void)
 	static const char *const st[] = { "down", "idle", "associated", "link up" };
 
 	io_putstr("chip:  ");
-	io_putstr(iwl_chip_name());
+	io_putstr(wififw_chip_name());
 	io_putstr("\nstate: ");
 	io_putstr(st[W.state & 3]);
 	io_putstr(" (");
@@ -807,7 +810,7 @@ static int nd_link_up(void)
 
 const struct netdev wifi_netdev = {
 	"wifi",
-	"Intel 7265D/3165 or 6200/6300 WiFi",
+	"WiFi",
 	nd_init,
 	nd_get_mac,
 	nd_rx,
@@ -827,8 +830,7 @@ const struct netdev wifi_netdev = {
 
 void wifi_shutdown(void)
 {
-	if (W.state != SYS_WIFI_ST_DOWN || iwl_present())
-		iwlfw_down();
+	wififw_shutdown();
 	if (W.state != SYS_WIFI_ST_DOWN)
 		W.state = SYS_WIFI_ST_DOWN;
 }
@@ -859,11 +861,9 @@ int sys_wifi(int op, int a2, int a3)
 		return wifi_up();
 	case SYS_WIFI_STATUS: {
 		struct sys_wifi_status *s = (struct sys_wifi_status *)a2;
-		const struct iwl_fw_info *fw;
-
 		if (!user_ok(a2, sizeof(*s), 1))
 			return -1;
-		if (!W.present && iwl_probe() == 0)
+		if (!W.present && wififw_probe() == 0)
 			W.present = 1;
 		iwl_memzero(s, sizeof(*s));
 		s->state = W.state;
@@ -876,9 +876,9 @@ int sys_wifi(int op, int a2, int a3)
 			s->ssid_len = W.cur.ssid_len;
 			wmemcpy(s->ssid, W.cur.ssid, W.cur.ssid_len);
 		}
-		copy_str(s->chip, sizeof(s->chip), W.present ? iwl_chip_name() : "none");
-		fw = iwl_fw();
-		copy_str(s->fw, sizeof(s->fw), fw && fw->present ? fw->human : "");
+		copy_str(s->chip, sizeof(s->chip),
+			 W.present ? wififw_chip_name() : "none");
+		copy_str(s->fw, sizeof(s->fw), wififw_fw_name());
 		copy_str(s->detail, sizeof(s->detail),
 			 W.detail ? W.detail : (W.present ? "firmware not loaded"
 							  : "no card"));
@@ -894,14 +894,14 @@ int sys_wifi(int op, int a2, int a3)
 		if (W.state >= SYS_WIFI_ST_ASSOC)
 			return -1;        /* scanning while associated: not yet */
 		W.nbss = 0;
-		return iwlfw_scan_start();
+		return wififw_scan_start();
 	case SYS_WIFI_RESULTS: {
 		unsigned int max = (unsigned int)a3, n;
 
 		if (W.state == SYS_WIFI_ST_DOWN)
 			return -1;
-		iwlfw_poll();
-		if (iwlfw_scan_running())
+		wififw_poll();
+		if (wififw_scan_running())
 			return -2;
 		if (max > W.nbss)
 			max = W.nbss;
@@ -928,7 +928,7 @@ int sys_wifi(int op, int a2, int a3)
 
 		if (W.state < SYS_WIFI_ST_ASSOC)
 			return -1;
-		iwlfw_poll();
+		wififw_poll();
 		if (W.eapq_head == W.eapq_tail)
 			return 0;
 		s = &W.eapq[W.eapq_tail];
@@ -956,7 +956,7 @@ int sys_wifi(int op, int a2, int a3)
 		if (W.state < SYS_WIFI_ST_ASSOC || !user_ok(a2, sizeof(k), 0))
 			return -1;
 		wmemcpy(&k, (const void *)a2, sizeof(k));
-		r = iwlfw_set_key(k.pairwise, k.keyidx, k.key,
+		r = wififw_set_key(k.pairwise, k.keyidx, k.key,
 				    k.pairwise ? 0 : k.rsc);
 		if (r == 0) {
 			if (k.pairwise) {

+ 11 - 3
wifi.h

@@ -1,6 +1,7 @@
-/* WiFi station (Intel 7265D/3165, 6200/6300): 802.11 MLME, Ethernet <-> 802.11 data
- * path, EAPOL queue for the ring-3 supplicant, and the `wifi` netdev.  The
- * firmware and the PCIe transport are iwl_mvm.c / iwl_trans.c. */
+/* WiFi station (Intel 7265D/3165, 6200/6300, Realtek RTL8821AU USB): 802.11
+ * MLME, Ethernet <-> 802.11 data path, EAPOL queue for the ring-3
+ * supplicant, and the `wifi` netdev.  The device layer is reached through
+ * wifi_fw.h (iwl_mvm.c / iwl_dvm.c over iwl_trans.c, or rtl8821au.c). */
 #ifndef WIFI_H
 #define WIFI_H
 
@@ -15,4 +16,11 @@ int sys_wifi(int op, int a2, int a3);
  * netdev (its RX ring would otherwise keep writing into freed pages). */
 void wifi_shutdown(void);
 
+/* Upcalls from the device drivers.  `status` is the Intel RX status word
+ * (IWL_RX_*); 0 means "not decrypted", so CCMP frames go through the
+ * software path. */
+void wifi_rx_frame(const unsigned char *frame, unsigned int len, int rssi,
+		   unsigned int status, unsigned int channel);
+void wifi_scan_done(int ok);
+
 #endif

+ 274 - 0
wifi_fw.c

@@ -0,0 +1,274 @@
+/* Firmware/driver-layer dispatch: see wifi_fw.h. */
+
+#include "wifi_fw.h"
+#include "iwl_dvm.h"
+#include "iwl_mvm.h"
+#include "iwl_trans.h"
+#include "rtl8821au.h"
+
+enum { BE_NONE, BE_MVM, BE_DVM, BE_RTL };
+
+static int be;           /* backend of the radio that is up (or last up) */
+
+/* Which backend would come up now. */
+static int pick(void)
+{
+	if (rtl8821au_present())
+		return BE_RTL;
+	if (iwl_probe() == 0)
+		return iwl_is_dvm() ? BE_DVM : BE_MVM;
+	return BE_NONE;
+}
+
+/* The backend for calls that need a device: the active one, else the one
+ * that would be picked. */
+static int cur(void)
+{
+	return be != BE_NONE ? be : pick();
+}
+
+int wififw_probe(void)
+{
+	return pick() == BE_NONE ? -1 : 0;
+}
+
+int wififw_present(void)
+{
+	return cur() == BE_RTL ? rtl8821au_present() : iwl_present();
+}
+
+/* No probing here: SYS_NET_INFO asks once a second, and iwl_probe() scans
+ * the whole PCI bus when no Intel card exists. */
+const char *wififw_chip_name(void)
+{
+	int b = be;
+
+	if (b == BE_NONE)
+		b = rtl8821au_present() ? BE_RTL :
+		    iwl_present() ? BE_MVM : BE_NONE;
+	switch (b) {
+	case BE_RTL:
+		return rtl8821au_chip_name();
+	case BE_MVM:
+	case BE_DVM:
+		return iwl_chip_name();
+	}
+	return "none";
+}
+
+const char *wififw_fw_name(void)
+{
+	const struct iwl_fw_info *fw;
+
+	if (cur() == BE_RTL)
+		return rtl8821au_fw_name();
+	fw = iwl_fw();
+	return fw && fw->present ? fw->human : "";
+}
+
+int wififw_fatal(void)
+{
+	switch (be) {
+	case BE_RTL:
+		return rtl8821au_fatal();
+	case BE_MVM:
+	case BE_DVM:
+		return iwl_fatal();
+	}
+	return 0;
+}
+
+int wififw_up(unsigned char mac[6])
+{
+	be = pick();
+	switch (be) {
+	case BE_RTL:
+		return rtl8821au_up(mac);
+	case BE_DVM:
+		return iwl_dvm_up(mac);
+	case BE_MVM:
+		return iwl_mvm_up(mac);
+	}
+	return -1;
+}
+
+void wififw_down(void)
+{
+	switch (be) {
+	case BE_RTL:
+		rtl8821au_down();
+		break;
+	case BE_DVM:
+		iwl_dvm_down();
+		break;
+	case BE_MVM:
+		iwl_mvm_down();
+		break;
+	}
+	be = BE_NONE;
+}
+
+void wififw_shutdown(void)
+{
+	int was = be;
+
+	wififw_down();
+	/* The Intel card may have run before the stick was plugged in: its
+	 * RX ring must stop writing into pages the next kernel reuses. */
+	if (was != BE_MVM && was != BE_DVM && iwl_present()) {
+		if (iwl_is_dvm())
+			iwl_dvm_down();
+		else
+			iwl_mvm_down();
+	}
+}
+
+const char *wififw_state(void)
+{
+	switch (cur()) {
+	case BE_RTL:
+		return rtl8821au_state();
+	case BE_DVM:
+		return iwl_dvm_state();
+	case BE_MVM:
+		return iwl_mvm_state();
+	}
+	return "no WiFi device";
+}
+
+int wififw_poll(void)
+{
+	switch (be) {
+	case BE_RTL:
+		return rtl8821au_poll();
+	case BE_DVM:
+		return iwl_dvm_poll();
+	case BE_MVM:
+		return iwl_mvm_poll();
+	}
+	return -1;
+}
+
+int wififw_scan_start(void)
+{
+	switch (be) {
+	case BE_RTL:
+		return rtl8821au_scan_start();
+	case BE_DVM:
+		return iwl_dvm_scan_start();
+	case BE_MVM:
+		return iwl_mvm_scan_start();
+	}
+	return -1;
+}
+
+int wififw_scan_running(void)
+{
+	switch (be) {
+	case BE_RTL:
+		return rtl8821au_scan_running();
+	case BE_DVM:
+		return iwl_dvm_scan_running();
+	case BE_MVM:
+		return iwl_mvm_scan_running();
+	}
+	return 0;
+}
+
+int wififw_prepare_connect(const unsigned char bssid[6], unsigned int chan,
+			   unsigned int beacon_int)
+{
+	switch (be) {
+	case BE_RTL:
+		return rtl8821au_prepare_connect(bssid, chan, beacon_int);
+	case BE_DVM:
+		return iwl_dvm_prepare_connect(bssid, chan, beacon_int);
+	case BE_MVM:
+		return iwl_mvm_prepare_connect(bssid, chan, beacon_int);
+	}
+	return -1;
+}
+
+int wififw_set_assoc(unsigned short aid, unsigned int beacon_int,
+		     unsigned int dtim)
+{
+	switch (be) {
+	case BE_RTL:
+		return rtl8821au_set_assoc(aid, beacon_int, dtim);
+	case BE_DVM:
+		return iwl_dvm_set_assoc(aid, beacon_int, dtim);
+	case BE_MVM:
+		return iwl_mvm_set_assoc(aid, beacon_int, dtim);
+	}
+	return -1;
+}
+
+void wififw_disconnect(void)
+{
+	switch (be) {
+	case BE_RTL:
+		rtl8821au_disconnect();
+		break;
+	case BE_DVM:
+		iwl_dvm_disconnect();
+		break;
+	case BE_MVM:
+		iwl_mvm_disconnect();
+		break;
+	}
+}
+
+int wififw_tx_mgmt(const unsigned char *frame, unsigned int len)
+{
+	switch (be) {
+	case BE_RTL:
+		return rtl8821au_tx_mgmt(frame, len);
+	case BE_DVM:
+		return iwl_dvm_tx_mgmt(frame, len);
+	case BE_MVM:
+		return iwl_mvm_tx_mgmt(frame, len);
+	}
+	return -1;
+}
+
+int wififw_tx_data(const unsigned char *frame, unsigned int len, int protect)
+{
+	switch (be) {
+	case BE_RTL:
+		return rtl8821au_tx_data(frame, len, protect);
+	case BE_DVM:
+		return iwl_dvm_tx_data(frame, len, protect);
+	case BE_MVM:
+		return iwl_mvm_tx_data(frame, len, protect);
+	}
+	return -1;
+}
+
+int wififw_set_key(int pairwise, int keyidx, const unsigned char key[16],
+		   const unsigned char rsc[6])
+{
+	switch (be) {
+	case BE_RTL:
+		return rtl8821au_set_key(pairwise, keyidx, key, rsc);
+	case BE_DVM:
+		return iwl_dvm_set_key(pairwise, keyidx, key, rsc);
+	case BE_MVM:
+		return iwl_mvm_set_key(pairwise, keyidx, key, rsc);
+	}
+	return -1;
+}
+
+void wififw_clear_keys(void)
+{
+	switch (be) {
+	case BE_RTL:
+		rtl8821au_clear_keys();
+		break;
+	case BE_DVM:
+		iwl_dvm_clear_keys();
+		break;
+	case BE_MVM:
+		iwl_mvm_clear_keys();
+		break;
+	}
+}

+ 40 - 0
wifi_fw.h

@@ -0,0 +1,40 @@
+/* Firmware/driver-layer dispatch for wifi.c.  Three backends share the
+ * iwl_mvm.h call semantics:
+ *   - Intel 7000 family (7265D/3165, PCIe): iwl_mvm.c
+ *   - Intel 6000 family (6200/6300, PCIe):  iwl_dvm.c
+ *   - Realtek RTL8821AU (USB stick):         rtl8821au.c
+ * The backend is chosen when the radio comes up: a plugged-in USB stick
+ * wins over the built-in Intel card, otherwise the PCI probe decides
+ * (iwl_is_dvm()).  It stays fixed until wififw_down(). */
+#ifndef WIFI_FW_H
+#define WIFI_FW_H
+
+/* Find a supported device without loading firmware: 0 = found. */
+int  wififw_probe(void);
+int  wififw_present(void);
+const char *wififw_chip_name(void);
+const char *wififw_fw_name(void);    /* "" while no firmware runs */
+/* The device died (firmware assert, USB unplug): wifi.c reloads. */
+int  wififw_fatal(void);
+/* Pick the backend and bring it up (firmware + radio); fills the MAC.
+ * On failure wififw_state() says why (SYS_WIFI_STATUS `detail`). */
+int  wififw_up(unsigned char mac[6]);
+void wififw_down(void);
+/* Stop every WiFi device's DMA before a kexec, used or not. */
+void wififw_shutdown(void);
+const char *wififw_state(void);
+int  wififw_poll(void);
+int  wififw_scan_start(void);
+int  wififw_scan_running(void);
+int  wififw_prepare_connect(const unsigned char bssid[6], unsigned int chan,
+			    unsigned int beacon_int);
+int  wififw_set_assoc(unsigned short aid, unsigned int beacon_int,
+		      unsigned int dtim);
+void wififw_disconnect(void);
+int  wififw_tx_mgmt(const unsigned char *frame, unsigned int len);
+int  wififw_tx_data(const unsigned char *frame, unsigned int len, int protect);
+int  wififw_set_key(int pairwise, int keyidx, const unsigned char key[16],
+		    const unsigned char rsc[6]);
+void wififw_clear_keys(void);
+
+#endif

この差分においてかなりの量のファイルが変更されているため、一部のファイルを表示していません