4 Achegas dc5395d1aa ... 852e70701c

Autor SHA1 Mensaxe Data
  Johannes Zellner 852e70701c Speed up boot to the terminal and keep storage/lsusb from stalling hai 1 semana
  Johannes Zellner 3d3fef3263 Add Realtek RTL8821AU USB WiFi (TP-Link Archer T2U Nano) hai 1 semana
  Johannes Zellner 02ed133120 Add Intel 6200/6300 WiFi (iwlwifi DVM) for the ThinkPad X201 hai 1 semana
  Johannes Zellner 22a97523c0 Share one selector engine, add ::before/::after and speed up page scripts hai 1 semana
Modificáronse 58 ficheiros con 8058 adicións e 956 borrados
  1. 203 31
      AGENTS.md
  2. 60 24
      Makefile
  3. 9 6
      ahci.c
  4. 8 1
      ata.c
  5. 37 14
      boot.S
  6. 100 20
      browser.c
  7. 1047 589
      browser_html.c
  8. 17 1
      browser_html.h
  9. 90 9
      ccmp.c
  10. 13 1
      ccmp.h
  11. 34 2
      ccmp_test.c
  12. 27 5
      disk.c
  13. 18 5
      docs/boot.html
  14. 2 1
      docs/development.html
  15. 105 15
      docs/drivers.html
  16. 6 1
      docs/filesystems.html
  17. 1 1
      docs/index.html
  18. 7 5
      docs/network.html
  19. 0 2
      docs/userland.html
  20. 17 7
      e1000e.c
  21. 2 3
      ehci.c
  22. 367 50
      inflate.c
  23. 1194 0
      iwl_dvm.c
  24. 30 0
      iwl_dvm.h
  25. 250 46
      iwl_trans.c
  26. 22 1
      iwl_trans.h
  27. 8 2
      js_browser.c
  28. 75 30
      kernel.c
  29. 144 0
      mkrtwtbl.c
  30. 12 1
      netdev.c
  31. 4 0
      netdev.h
  32. 2 3
      ohci.c
  33. 25 0
      rtl8821a_tbl.h
  34. 1784 0
      rtl8821au.c
  35. 40 0
      rtl8821au.h
  36. 3 1
      sdhci.c
  37. 39 1
      sys_fs.c
  38. 2 0
      sys_fs.h
  39. 1 3
      sys_info.c
  40. 7 3
      sys_net.c
  41. 21 0
      sys_wm.c
  42. 1 1
      syscall.c
  43. 1 1
      syscall.h
  44. 30 0
      third_party/rtw88/LICENSE
  45. 1656 0
      third_party/rtw88/rtw8821a_table.inc
  46. 2 2
      ucmd_wifi.c
  47. 2 3
      uhci.c
  48. 56 14
      usb.c
  49. 10 0
      usb.h
  50. 4 0
      usbhid.c
  51. 39 10
      vesa_stage.S
  52. 32 0
      vfs.c
  53. 15 0
      vfs.h
  54. 50 34
      wifi.c
  55. 11 3
      wifi.h
  56. 274 0
      wifi_fw.c
  57. 40 0
      wifi_fw.h
  58. 2 4
      xhci.c

+ 203 - 31
AGENTS.md

@@ -103,9 +103,15 @@ make clean
   firmware does not fit a 1.44 MB floppy.  The kernel itself no longer
   firmware does not fit a 1.44 MB floppy.  The kernel itself no longer
   carries the pack; the boot path loads it into RAM and `ramfs_init` takes a
   carries the pack; the boot path loads it into RAM and `ramfs_init` takes a
   private copy (`bin/<name>` mounted at `/ramfs`).
   private copy (`bin/<name>` mounted at `/ramfs`).
-- The WiFi firmware `firmware/iwlwifi-7265D-29.ucode` is decompressed from
-  the host's linux-firmware (`WIFI_FW` overrides the search; `zstd` needed for
-  `.zst`).  Without it the build prints a note and packs no firmware.
+- The WiFi firmware `firmware/iwlwifi-7265D-29.ucode` (Chromebook) and
+  `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.  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
 - `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
   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.
   pages plus `PIC.BMP`, and a few test files; it requires `mtools` to build.
@@ -135,12 +141,17 @@ make clean
 ## Boot chain
 ## Boot chain
 
 
 1. `boot.S` — 512-byte boot sector (FAT16 BPB). Loads contiguous kernel sectors
 1. `boot.S` — 512-byte boot sector (FAT16 BPB). Loads contiguous kernel sectors
-   to `0x7E00` (CHS for floppy, INT 13h AH=42h LBA for hard disk), jumps there.
+   to `0x7E00` (CHS for floppy, one sector per call; INT 13h AH=42h LBA for
+   hard disk, up to 64 sectors per call into `ES:0` with `ES` advanced, so the
+   offset never wraps), jumps there with the boot drive still in DL.
 2. `vesa_stage.S` — real-mode VESA stage. Probes INT 15h E820 into `0x1000`,
 2. `vesa_stage.S` — real-mode VESA stage. Probes INT 15h E820 into `0x1000`,
    enables A20, reads the `NBROOT1` rootfs header at the boot sector's
    enables A20, reads the `NBROOT1` rootfs header at the boot sector's
    `pack_lba` field and copies the gzip pack directly to `PACK_HIGH_ADDR`
    `pack_lba` field and copies the gzip pack directly to `PACK_HIGH_ADDR`
    (`0x900000`, above the 8 MB VGA-shadow reserve) in 16 KB chunks through
    (`0x900000`, above the 8 MB VGA-shadow reserve) in 16 KB chunks through
-   the `0x3000` staging buffer with a temporary flat protected-mode
+   the `0x3000` staging buffer — one INT 13h per chunk (32 sectors over EDD,
+   up to the end of the track over CHS; a refused multi-sector request falls
+   back to one sector per call; the old per-sector loop made ~3600 BIOS calls
+   and took ~1.5 s in QEMU) — with a temporary flat protected-mode
    `rep movsb` (`pm_copy`).  The X201 BIOS's INT 15h AH=87h block move
    `rep movsb` (`pm_copy`).  The X201 BIOS's INT 15h AH=87h block move
    silently copies zeros to this range, which left the kernel with an empty
    silently copies zeros to this range, which left the kernel with an empty
    pack; the CPU copy is used instead.  `paging_prepare_cpu()` still clears
    pack; the CPU copy is used instead.  `paging_prepare_cpu()` still clears
@@ -158,8 +169,14 @@ make clean
    used); fixed disks use EDD LBA.
    used); fixed disks use EDD LBA.
 3. `kernel.c` — `kernel_main` zeroes BSS then inits serial → memory/paging →
 3. `kernel.c` — `kernel_main` zeroes BSS then inits serial → memory/paging →
    ACPI/MADT → I/O → power → idle → processes → timer (PIT; 1 kHz busy,
    ACPI/MADT → I/O → power → idle → processes → timer (PIT; 1 kHz busy,
-   100 Hz idle + IDT) → SMP AP bring-up → mouse → VGA shadow buffer →
-   terminal.
+   100 Hz idle + IDT) → SMP AP bring-up → mouse/USB → VGA shadow buffer →
+   ramfs inflate → WM → NIC probe → kworker → the `bootscan` kernel thread
+   (storage scan + FAT mounts, then the `/data` WiFi autoconnect once the
+   boot DHCP finished) → `wm_server_boot_ready()` immediately, so the first
+   terminal starts while the disks are still probed → DHCP.  COM1 prints
+   `wm: first window frame (term) at N ms` (uptime) when the first app
+   window presents: the end point of a boot-time measurement (~0.65 s after
+   the kernel starts on QEMU's USB image).
 4. `mbr.S` — MBR (USB/HDD boot); loads the active partition's boot sector
 4. `mbr.S` — MBR (USB/HDD boot); loads the active partition's boot sector
    (partition 1 at LBA 63).
    (partition 1 at LBA 63).
 5. **UEFI (`efi/`)** — an x86-64 PE32+ `BOOTX64.EFI` loads from the ESP. It
 5. **UEFI (`efi/`)** — an x86-64 PE32+ `BOOTX64.EFI` loads from the ESP. It
@@ -515,6 +532,17 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   64 entries of the last FAT directory (`fat_cache` in `vfs.c`), so the
   64 entries of the last FAT directory (`fat_cache` in `vfs.c`), so the
   WebDAV server's index 0..N-1 sequence is one directory walk instead of N;
   WebDAV server's index 0..N-1 sequence is one directory walk instead of N;
   any write/mkdir/rmdir/unlink invalidates it.
   any write/mkdir/rmdir/unlink invalidates it.
+  A rescan (`vfs_mount_all()` from the boot `bootscan` thread or the
+  `storage` command via `sys_fs_scan()`) re-adds `/ramfs` and `/share`
+  first; their operations (and SYS_SPAWN of a RAM path,
+  `sys_fs_enter_path()`) do not wait for `vfs_scan_busy`
+  (`vfs_mount_is_ram()`), only disk paths do.  The scan runs preemptible;
+  the floppy/ATA/AHCI/SDHCI wait loops call `vfs_scan_poll(t0)` /
+  `vfs_scan_relax(ms)`, which sleep (dropping the BKL) only for the scan's
+  owning process, with interrupts on and preemption enabled, and only once
+  a wait passes 2 ms; at boot in the idle context and in normal I/O they
+  are no-ops.  So `storage` no longer freezes the WM.  `disk_probe()` skips
+  a floppy whose CMOS drive type (register `0x10`) is 0.
 - **Installer**: `install.c/h` — the privileged `install <target> [source|self]`
 - **Installer**: `install.c/h` — the privileged `install <target> [source|self]`
   command clones the live NebOS image or writes the running image.  A clone
   command clones the live NebOS image or writes the running image.  A clone
   detects a source by the MBR plus the LBA-63 boot-sector signature (`NEBOS`
   detects a source by the MBR plus the LBA-63 boot-sector signature (`NEBOS`
@@ -583,8 +611,12 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   82577LM/82578/82579; polled, PCI INTx disabled before MMIO, PCH D0 and
   82577LM/82578/82579; polled, PCI INTx disabled before MMIO, PCH D0 and
   no BAR sizing so the ME/AMT cannot leave the BAR undecoded; skips the
   no BAR sizing so the ME/AMT cannot leave the BAR undecoded; skips the
   global MAC reset while the link is up, because the PCH/ME wedges on the
   global MAC reset while the link is up, because the PCH/ME wedges on the
-  CTRL_RST access; extended RX descriptors only when `RFCTL.EXTEN` reads
-  back set, legacy write-back on the 82540EM, which has no RFCTL),
+  CTRL_RST access; the PCIe GIO-master-disable handshake is skipped on the
+  conventional-PCI 82540EM, whose STATUS bit 19 never clears in QEMU and
+  used to cost the full 1 s timeout at every boot; extended RX descriptors only when `RFCTL.EXTEN` reads
+  back set; the 82540EM (`0x100E`) is pinned to the legacy write-back
+  layout, because it has no RFCTL but QEMU stores the register, so EXTEN
+  reads back set while the device still writes legacy descriptors),
   `r8168.c`
   `r8168.c`
   RTL8111/8168 (PCIe, polled, C+ mode, 64-entry RX ring), `ne2000.c`
   RTL8111/8168 (PCIe, polled, C+ mode, 64-entry RX ring), `ne2000.c`
   RTL8029 (QEMU/ISA), `asix.c` AX88772 USB Ethernet (bulk over the USB
   RTL8029 (QEMU/ISA), `asix.c` AX88772 USB Ethernet (bulk over the USB
@@ -688,7 +720,7 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   ("wifi")`), runs DHCP and leaves `wifi __keep` running for group rekeys.
   ("wifi")`), runs DHCP and leaves `wifi __keep` running for group rekeys.
   A successful menu/CLI connect also writes the SSID and passphrase to
   A successful menu/CLI connect also writes the SSID and passphrase to
   `/data/WIFI.CFG` (8.3, plaintext, BSSID zeroed so a mesh/repeater AP works);
   `/data/WIFI.CFG` (8.3, plaintext, BSSID zeroed so a mesh/repeater AP works);
-  after the boot DHCP attempt `kernel_main` spawns
+  after the storage scan and the boot DHCP attempt the `bootscan` thread spawns
   `/ramfs/bin/wifi autoconnect` when that file exists.  Autoconnect leaves a
   `/ramfs/bin/wifi autoconnect` when that file exists.  Autoconnect leaves a
   wired link that is physically up alone and only tries WiFi when no wired
   wired link that is physically up alone and only tries WiFi when no wired
   link is present; an already-linked WiFi card is also left alone.  `/data`
   link is present; an already-linked WiFi card is also left alone.  `/data`
@@ -702,6 +734,47 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   There is no QEMU model: test on the Chromebook over `make net-dev` with
   There is no QEMU model: test on the Chromebook over `make net-dev` with
   the RNDIS phone/ASIX dongle as the management link and `make netmon` for
   the RNDIS phone/ASIX dongle as the management link and `make netmon` for
   the `iwl:`/`wifi:` trail.
   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).  `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
+  bytes, the SCD chain-extension workaround, no shadow registers and the
+  pre-TLV v2 `.ucode` (inst/data sections loaded at `0x0`/`0x800000`).  The
+  DVM AC queues 0..3 (VO/VI/BE/BK, FIFOs 3/2/1/0) are programmed by the
+  transport (`iwl_txq_enable_fifo()`), not by a firmware command.  PCI IDs:
+  `8086:4239`/`422c` = 6200 (`6000i`, antennas B+C, 2x2 IPA radio SKU bit in
+  `CSR_GP_DRIVER_REG`), `4238`/`422b` = 6300 (`6000`, antennas from the
+  EEPROM).  `iwl_dvm.c`: the host reads the 2 KB OTP through
+  `CSR_EEPROM_REG` (MAC at word 0x15, radio config at 0x48, crystal
+  calibration at 0x128 behind the calibration link 0x67), `nic_config()`
+  runs from the transport between APM init and ring setup; INIT image
+  (ALIVE, AC queues, WiMAX coex off, crystal calibration,
+  `CALIBRATION_CFG_CMD`, `CALIBRATION_RES_NOTIFICATION`s stored by op code
+  until `CALIBRATION_COMPLETE`), runtime image (stored results replayed as
+  `REPLY_PHY_CALIBRATION_CMD`, DC calibration, TX antennas, RXON, TX power,
+  CAM `POWER_TABLE_CMD`).  A full RXON commit clears the association first,
+  which wipes the firmware station table, so the broadcast station 15 and
+  the AP station 0 are re-added with fixed-rate `LINK_QUALITY` tables after
+  every unassociated RXON, and every retune is followed by
+  `REPLY_TX_POWER_DBM_CMD` (15 dBm).  Association is `REPLY_RXON_TIMING`,
+  `REPLY_QOS_PARAM`, then RXON with `RXON_FILTER_ASSOC`.  Scans are one
+  `REPLY_SCAN_CMD` per band (2.4 GHz, then 5 GHz, kicked from the poll path
+  because a synchronous command must not run inside the RX handler) over
+  the channels the EEPROM regulatory tables mark valid (bands at 0x08,
+  0x42, 0x5c, 0x74 behind link 0x66); probes only where a channel is ACTIVE
+  and not RADAR.  On the X201's 6300 the earlier fixed 5 GHz list made the
+  firmware SYSASSERT.  Any firmware assert (`CSR_INT` SW_ERR) now drops
+  `wifi.c` to DOWN (`iwl_fatal()`), so the next scan/connect reloads the
+  firmware instead of timing out forever.  Keys are `REPLY_ADD_STA` in
+  modify mode on the AP station.  RX is the same `RX_PHY` + `RX_MPDU` pair
+  as the 7265D (RSSI from the AGC/in-band fields, `IWLAGN_RSSI_OFFSET` 44),
+  so the software CCMP fallback covers both.
 - **Hot-reload (kexec)**: `kexec.c/h`, `kexec_tramp.S`, `entry.S`, and the
 - **Hot-reload (kexec)**: `kexec.c/h`, `kexec_tramp.S`, `entry.S`, and the
   host-side `kexec_send.c` — reload the kernel over serial without rebooting
   host-side `kexec_send.c` — reload the kernel over serial without rebooting
   (C-light: reuses the current framebuffer, re-runs `kernel_main`).
   (C-light: reuses the current framebuffer, re-runs `kernel_main`).
@@ -719,6 +792,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
   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
   may have been inside a scheduler critical section when INIT arrived) and then
   jumps; the new kernel starts the APs again from scratch.
   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
 - **Mouse**: `mouse.c/h` — cursor move/draw shared by the PS/2 path
   (IntelliMouse including the 4th-byte wheel, Cirrus/Radeon hardware
   (IntelliMouse including the 4th-byte wheel, Cirrus/Radeon hardware
   cursor) and the USB backend.  Scroll from every backend accumulates in
   cursor) and the USB backend.  Scroll from every backend accumulates in
@@ -768,6 +878,14 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   and USB3 root ports; halt/HCH reset sequence and BIOS/OS handoff)
   and USB3 root ports; halt/HCH reset sequence and BIOS/OS handoff)
   implement it.
   implement it.
   `lsusb.c` — the `lsusb` command (lists every controller and its tree).
   `lsusb.c` — the `lsusb` command (lists every controller and its tree).
+  Every HCD delay goes through `usb_delay_ms()`: inside a scan
+  (`usb_scan`/`usb_hotplug_poll`) owned by a sleepable process — `lsusb`,
+  the kworker's HID rescan/hotplug enumeration — it sleeps and drops the
+  BKL, so port resets no longer stall the system; elsewhere it busy-waits.
+  While such a scan sleeps, `usb_scan_foreign()` makes the `usb.c`
+  wrappers refuse other contexts (control/bulk transfers fail, interrupt
+  polls return 0, `usb_port_connected` reports 1) and `usbhid_poll` skips
+  its tick; USB NICs, the RTL8821AU and USB disks see a transient error.
   `usbmsc.c/h` — USB mass-storage class driver (BOT/SCSI) exposed as
   `usbmsc.c/h` — USB mass-storage class driver (BOT/SCSI) exposed as
   `usb0..`; its device probe is registered next to ASIX and `blk_scan()`
   `usb0..`; its device probe is registered next to ASIX and `blk_scan()`
   registers the devices in the block registry.
   registers the devices in the block registry.
@@ -978,7 +1096,18 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   parsing/resolution, redirects, chunked decoding, timeouts and TLS; curl,
   parsing/resolution, redirects, chunked decoding, timeouts and TLS; curl,
   the browser and chat use it.  `browser_html.c/h` is a tolerant HTML tree + the
   the browser and chat use it.  `browser_html.c/h` is a tolerant HTML tree + the
   CSS subset: external `<link rel=stylesheet>` sheets (in document order,
   CSS subset: external `<link rel=stylesheet>` sheets (in document order,
-  inline `<style>` last), tag/class/id and descendant/child selectors,
+  inline `<style>` last), one selector engine shared by the cascade,
+  `querySelector(All)` and `matches` (type/class/id, attribute presence and
+  `= ~= |= ^= $= *=` values with the `i` flag, descendant/`>`/`+`/`~`
+  combinators with backtracking, `:root`, `:link`, the `first/last/only`
+  child and of-type classes, `:nth-child`/`:nth-last-child`/`:nth-of-type`/
+  `:nth-last-of-type` (`an+b`, `odd`, `even`), `:empty`, `:checked`,
+  `:disabled`/`:enabled`, `:required`/`:optional`, `:not`/`:is`/`:where`
+  with selector lists, and `:has` with relative combinators; user-state
+  pseudo-classes such as `:hover` never match and their rules are dropped
+  at parse time), `::before`/`::after` generated text (`content` strings,
+  `attr()`, quotes; cascaded as an anonymous inline box per element by
+  `br_pseudo()` and emitted as literal runs, so scripts never see it),
   colours/`rgba()`, legacy `bgcolor` attributes, solid backgrounds and
   colours/`rgba()`, legacy `bgcolor` attributes, solid backgrounds and
   `background-image` with
   `background-image` with
   `repeat`/`position`/`size` (`auto` keeps the legacy stretch, `cover` and
   `repeat`/`position`/`size` (`auto` keeps the legacy stretch, `cover` and
@@ -1064,8 +1193,12 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   focus, so raw commands can acquire the NIC.  A page fetch that fails with a
   focus, so raw commands can acquire the NIC.  A page fetch that fails with a
   DNS error is torn down and tried once more after half a second.  The helper
   DNS error is torn down and tried once more after half a second.  The helper
   sends `Accept-Encoding: gzip`; the browser inflates with a
   sends `Accept-Encoding: gzip`; the browser inflates with a
-  progress hook that pumps the UI.  The status bar reports the page fetch
-  (headers and first-byte milliseconds).  Page, CSS and
+  progress hook that pumps the UI.  While loading, the status bar reports
+  the page fetch (headers and first-byte milliseconds); when the load is
+  done it shows `Loaded in N ms: page P (headers H), parse, css, paint, js`
+  for 12 s, the phase breakdown to read before optimizing a page.  QuickJS
+  is compiled `-O1` (`QJS_CFLAGS`): faster than `-Os` and `-O2` on the
+  store's scripts and ~170 KB smaller than `-O2`.  Page, CSS and
   image buffers come from the ring-3 libc heap; the DOM uses fixed static
   image buffers come from the ring-3 libc heap; the DOM uses fixed static
   arenas (6144 nodes, 512 KB attributes, 4608 rules), and page bodies are
   arenas (6144 nodes, 512 KB attributes, 4608 rules), and page bodies are
   capped at 2 MB with CSS sheets up to 832 KB (six sheets).  Each image gets
   capped at 2 MB with CSS sheets up to 832 KB (six sheets).  Each image gets
@@ -1128,7 +1261,7 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   dispatches a bubbling, cancelable `click` before the default action.
   dispatches a bubbling, cancelable `click` before the default action.
   A state is created after `heap_start()`, shared by the page's scripts and
   A state is created after `heap_start()`, shared by the page's scripts and
   freed before `heap_reset()`.  `js_setlimit()` gives every script, timer
   freed before `heap_reset()`.  `js_setlimit()` gives every script, timer
-  and event a 10 s wall-clock budget (QuickJS's interrupt handler), so a
+  and event a 20 s wall-clock budget (QuickJS's interrupt handler), so a
   runaway loop raises `InternalError: interrupted` instead of freezing the
   runaway loop raises `InternalError: interrupted` instead of freezing the
   browser; errors are reported as `Name: message` plus the stack.  Math, Date and JSON come from the engine plus the
   browser; errors are reported as `Name: message` plus the stack.  Math, Date and JSON come from the engine plus the
   libc math/time layers.  The parsed page is painted before scripts are
   libc math/time layers.  The parsed page is painted before scripts are
@@ -1197,7 +1330,13 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   8-bit alpha plane.  The codecs are independent of the OS and ngui: they take
   8-bit alpha plane.  The codecs are independent of the OS and ngui: they take
   an input buffer plus a caller-provided `struct img_mem` arena, so a browser
   an input buffer plus a caller-provided `struct img_mem` arena, so a browser
   can link the same objects and decode HTTP bodies.  `inflate.c/h` (DEFLATE,
   can link the same objects and decode HTTP bodies.  `inflate.c/h` (DEFLATE,
-  zlib, gzip, CRC32/Adler32) is shared with the boot pack.  `img_sniff()` is a
+  zlib, gzip, CRC32/Adler32) is shared with the boot pack.  Its whole-buffer
+  helpers (`inflate_gzip_mem`/`_zlib_mem`/`_raw_mem`, used by `ramfs_init`
+  and the image decoders) are a separate fast path: 10-bit Huffman lookup
+  tables in the caller's `struct inflate` workspace, the output buffer as
+  the LZ77 history, and one checksum pass at the end (the 3.7 MB pack went
+  from ~1.1 s to ~0.25 s in QEMU).  The streaming API (`inflate_gzip`
+  with a source/sink and progress hook) keeps the bit-at-a-time decoder.  `img_sniff()` is a
   cheap header probe; `img_decode()` returns `struct img_image`; `img_draw()`
   cheap header probe; `img_decode()` returns `struct img_image`; `img_draw()`
   is a clipped, scaled (box-filter down, bilinear up) RGB565 blit with alpha
   is a clipped, scaled (box-filter down, bilinear up) RGB565 blit with alpha
   over a checkerboard or over the destination (for a browser).  Progressive
   over a checkerboard or over the destination (for a browser).  Progressive
@@ -1790,11 +1929,13 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
    `rep movsb`, so the cap is `PACK_MAX` 2 MB (raised from 1 MB for the WiFi
    `rep movsb`, so the cap is `PACK_MAX` 2 MB (raised from 1 MB for the WiFi
    firmware).  The browser and
    firmware).  The browser and
    its ring-3 dependencies are compiled with function sections and linked
    its ring-3 dependencies are compiled with function sections and linked
-   with `--gc-sections`; the current full pack with the UI fonts, the
-   1,036,312-byte WiFi firmware and `install/kernel.bin` is ~1.26 MiB gzip
-   (~1.33 MB), inflating to ~2.6 MB, which adds about half a second to the
-   boot; the floppy lite pack is ~680 KiB gzip and deliberately omits
-   `install/kernel.bin`.  `libdeflate-gzip -12` is still preferred.  Over
+   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), 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
    115200 serial a raw `cmds-dev` pack (2.6 MB) now takes about three
    minutes; use `net-cmds`.
    minutes; use `net-cmds`.
 9. SMP is **ACPI/MADT only**: no Intel MP-table fallback, so a Pentium II SMP
 9. SMP is **ACPI/MADT only**: no Intel MP-table fallback, so a Pentium II SMP
@@ -1854,8 +1995,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
     (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
     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
     per-connection TCP queues moved to the frame allocator, `_end` is
-    ~0x80180, ~64 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
 22. Browser limits: JavaScript runs inline and external classic scripts on
     QuickJS (ES2025) with the DOM surface described above (creation/mutation/selectors/
     QuickJS (ES2025) with the DOM surface described above (creation/mutation/selectors/
@@ -1868,8 +2009,12 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     and `enctype=multipart/form-data` plus file inputs are unsupported.
     and `enctype=multipart/form-data` plus file inputs are unsupported.
     Also missing: `type="module"`,
     Also missing: `type="module"`,
     `defer`/`async`, `MutationObserver`/`IntersectionObserver`,
     `defer`/`async`, `MutationObserver`/`IntersectionObserver`,
-    `XMLHttpRequest`/`fetch`, `DOMParser`, canvas/2D contexts and
-    pseudo-class/element selectors.  `getComputedStyle` returns the engine's
+    `XMLHttpRequest`/`fetch`, `DOMParser`, canvas/2D contexts, user-state
+    pseudo-classes (`:hover`, `:focus`, ...), `:nth-child(... of S)` and
+    pseudo-elements other than `::before`/`::after`; generated content is
+    text only (no box, background, counters or `url()` images, and
+    pseudo-elements inside flex/grid containers are not emitted).
+    `getComputedStyle` returns the engine's
     known properties (display, box metrics, colours, fonts, borders,
     known properties (display, box metrics, colours, fonts, borders,
     background and transform state), `outerHTML` returns only the string last
     background and transform state), `outerHTML` returns only the string last
     assigned to `innerHTML`, and `style`/`dataset` cover inline state rather
     assigned to `innerHTML`, and `style`/`dataset` cover inline state rather
@@ -1906,8 +2051,11 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     text following a float wraps beside it, `clear` advances past the active
     text following a float wraps beside it, `clear` advances past the active
     float, and a float without a width uses a text-width estimate.  `@media` width
     float, and a float without a width uses a text-width estimate.  `@media` width
     blocks are evaluated against the canvas width at page-parse time, so a
     blocks are evaluated against the canvas width at page-parse time, so a
-    window resize does not re-evaluate them until the next load, and
-    pseudo-classes are never matched.  `font-size` maps to real pixel sizes
+    window resize does not re-evaluate them until the next load.  An
+    absolutely positioned box placed by `bottom` without an explicit
+    height, or by a percentage `left` inside a nested positioned box, can
+    land at the wrong place (the store's navbar search box).  `font-size`
+    maps to real pixel sizes
     (8..48), `font-weight` selects the bold face, `font-style: italic`
     (8..48), `font-weight` selects the bold face, `font-style: italic`
     selects the italic face, `font-family: monospace` selects Liberation
     selects the italic face, `font-family: monospace` selects Liberation
     Mono, and nested `em`/`%` font sizes, `em`/`rem` widths, and
     Mono, and nested `em`/`%` font sizes, `em`/`rem` widths, and
@@ -2099,9 +2247,15 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     messages over 4 KB are truncated, history holds 240 messages, times are
     messages over 4 KB are truncated, history holds 240 messages, times are
     UTC and text is Latin-1.  Network calls block the UI for their duration.
     UTC and text is Latin-1.  Network calls block the UI for their duration.
 
 
-35. WiFi limits (`iwl_trans.c`, `iwl_mvm.c`, `wifi.c`): Intel 7265 rev D and
-    3165 only (one firmware, `iwlwifi-7265D-29`); a non-D 7265 is logged and
-    will likely fail.  Open networks and WPA2-PSK with CCMP pairwise *and*
+35. WiFi limits (`iwl_trans.c`, `iwl_mvm.c`, `iwl_dvm.c`, `wifi.c`): Intel
+    7265 rev D/3165 (`iwlwifi-7265D-29`) and 6200/6300 (`iwlwifi-6000-4`)
+    only; a non-D 7265 is logged and will likely fail, and other 6000-family
+    parts (6005/6030/6050/6150, which use different firmware files) are not
+    probed.  The 6200/6300 DVM path was written against Linux iwldvm without
+    hardware in the loop at first; on the X201 (a 6300, `8086:4238`) the
+    firmware boots and the 2.4 GHz scan works, while the 5 GHz scan and
+    connect are still being brought up.  It has no sensitivity, chain-noise
+    or temperature calibration and a fixed 15 dBm TX power.  Open networks and WPA2-PSK with CCMP pairwise *and*
     group only: no WPA3/SAE, PMF/802.11w, 802.1X, TKIP, WEP or hidden-SSID
     group only: no WPA3/SAE, PMF/802.11w, 802.1X, TKIP, WEP or hidden-SSID
     connects.  Legacy rates only (12 Mb/s data, no LQ/rate scaling, no
     connects.  Legacy rates only (12 Mb/s data, no LQ/rate scaling, no
     HT/VHT, no aggregation, non-QoS association), no power save, roaming,
     HT/VHT, no aggregation, non-QoS association), no power save, roaming,
@@ -2118,8 +2272,9 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     window yet and uses a byte-oriented AES-CCM per frame, so high RX
     window yet and uses a byte-oriented AES-CCM per frame, so high RX
     throughput is CPU-bound.  The driver was written against the Linux iwlwifi
     throughput is CPU-bound.  The driver was written against the Linux iwlwifi
     sources without hardware in the loop; expect bring-up debugging on the
     sources without hardware in the loop; expect bring-up debugging on the
-    Chromebook.  The kernel budget is tight: WiFi added ~20 KB, `_end` is
-    ~0x80180 (~64 KB below the `0x90000` wall).
+    Chromebook.  The kernel budget is tight: WiFi added ~28 KB with both
+    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
 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
     every driver, so opening the menu on a machine with an unconfigured USB
@@ -2154,3 +2309,20 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     intermediate state: P2 is written first, then the kernel, then the boot
     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
     sector; there is no A/B slot or journal.  A later full `install` repairs
     it, and P4 is never touched by these updates.
     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.

A diferenza do arquivo foi suprimida porque é demasiado grande
+ 60 - 24
Makefile


+ 9 - 6
ahci.c

@@ -10,6 +10,7 @@
  * linker bound, so nothing large may go into BSS. */
  * linker bound, so nothing large may go into BSS. */
 
 
 #include "ahci.h"
 #include "ahci.h"
+#include "vfs.h"
 #include "ata.h"
 #include "ata.h"
 #include "mm.h"
 #include "mm.h"
 #include "paging.h"
 #include "paging.h"
@@ -110,7 +111,7 @@ static void delay_ms(unsigned int ms)
 {
 {
 	unsigned int t0 = get_ticks_ms();
 	unsigned int t0 = get_ticks_ms();
 	while (get_ticks_ms() - t0 < ms)
 	while (get_ticks_ms() - t0 < ms)
-		;
+		vfs_scan_poll(t0);
 }
 }
 
 
 /* ---- PCI discovery ---- */
 /* ---- PCI discovery ---- */
@@ -189,14 +190,14 @@ static void ahci_port_init(unsigned int port)
 		pwr(port, P_CMD, c & ~CMD_ST);
 		pwr(port, P_CMD, c & ~CMD_ST);
 		t0 = get_ticks_ms();
 		t0 = get_ticks_ms();
 		while ((prd(port, P_CMD) & CMD_CR) && get_ticks_ms() - t0 < 500)
 		while ((prd(port, P_CMD) & CMD_CR) && get_ticks_ms() - t0 < 500)
-			;
+			vfs_scan_poll(t0);
 	}
 	}
 	c = prd(port, P_CMD);
 	c = prd(port, P_CMD);
 	if (c & CMD_FRE) {
 	if (c & CMD_FRE) {
 		pwr(port, P_CMD, c & ~CMD_FRE);
 		pwr(port, P_CMD, c & ~CMD_FRE);
 		t0 = get_ticks_ms();
 		t0 = get_ticks_ms();
 		while ((prd(port, P_CMD) & CMD_FR) && get_ticks_ms() - t0 < 500)
 		while ((prd(port, P_CMD) & CMD_FR) && get_ticks_ms() - t0 < 500)
-			;
+			vfs_scan_poll(t0);
 	}
 	}
 
 
 	pwr(port, P_CLB, ahci.cl);
 	pwr(port, P_CLB, ahci.cl);
@@ -211,20 +212,20 @@ static void ahci_port_init(unsigned int port)
 	pwr(port, P_CMD, prd(port, P_CMD) | CMD_FRE);
 	pwr(port, P_CMD, prd(port, P_CMD) | CMD_FRE);
 	t0 = get_ticks_ms();
 	t0 = get_ticks_ms();
 	while (!(prd(port, P_CMD) & CMD_FR) && get_ticks_ms() - t0 < 500)
 	while (!(prd(port, P_CMD) & CMD_FR) && get_ticks_ms() - t0 < 500)
-		;
+		vfs_scan_poll(t0);
 
 
 	/* Cold plug: reset the link if it is not established yet. */
 	/* Cold plug: reset the link if it is not established yet. */
 	if ((prd(port, P_SSTS) & 0x0Fu) != 3) {
 	if ((prd(port, P_SSTS) & 0x0Fu) != 3) {
 		pwr(port, P_SCTL, 1);   /* DET = 1: initiate COMRESET */
 		pwr(port, P_SCTL, 1);   /* DET = 1: initiate COMRESET */
 		t0 = get_ticks_ms();
 		t0 = get_ticks_ms();
 		while ((prd(port, P_SSTS) & 0x0Fu) != 3 && get_ticks_ms() - t0 < 1000)
 		while ((prd(port, P_SSTS) & 0x0Fu) != 3 && get_ticks_ms() - t0 < 1000)
-			;
+			vfs_scan_poll(t0);
 	}
 	}
 
 
 	pwr(port, P_CMD, prd(port, P_CMD) | CMD_ST);
 	pwr(port, P_CMD, prd(port, P_CMD) | CMD_ST);
 	t0 = get_ticks_ms();
 	t0 = get_ticks_ms();
 	while (!(prd(port, P_CMD) & CMD_CR) && get_ticks_ms() - t0 < 500)
 	while (!(prd(port, P_CMD) & CMD_CR) && get_ticks_ms() - t0 < 500)
-		;
+		vfs_scan_poll(t0);
 }
 }
 
 
 int ahci_init(void)
 int ahci_init(void)
@@ -324,6 +325,7 @@ static int ahci_issue(unsigned int port, const unsigned char fis[20], int write,
 	/* Wait for the port to be idle (previous command finished). */
 	/* Wait for the port to be idle (previous command finished). */
 	t0 = get_ticks_ms();
 	t0 = get_ticks_ms();
 	while (prd(port, P_CI) & 1u) {
 	while (prd(port, P_CI) & 1u) {
+		vfs_scan_poll(t0);
 		if (get_ticks_ms() - t0 > 4000)
 		if (get_ticks_ms() - t0 > 4000)
 			return -1;
 			return -1;
 	}
 	}
@@ -353,6 +355,7 @@ static int ahci_issue(unsigned int port, const unsigned char fis[20], int write,
 
 
 	t0 = get_ticks_ms();
 	t0 = get_ticks_ms();
 	while (prd(port, P_CI) & 1u) {
 	while (prd(port, P_CI) & 1u) {
+		vfs_scan_poll(t0);
 		if (get_ticks_ms() - t0 > 4000) {
 		if (get_ticks_ms() - t0 > 4000) {
 #if AHCI_DEBUG
 #if AHCI_DEBUG
 			serial_puts(SERIAL_COM1, "ahci: command timeout\n");
 			serial_puts(SERIAL_COM1, "ahci: command timeout\n");

+ 8 - 1
ata.c

@@ -6,6 +6,7 @@
  * (nVidia MCP79 "compatibility mode" and friends). */
  * (nVidia MCP79 "compatibility mode" and friends). */
 
 
 #include "ata.h"
 #include "ata.h"
+#include "vfs.h"
 #include "pci.h"
 #include "pci.h"
 #include "serial.h"
 #include "serial.h"
 #include "timer.h"
 #include "timer.h"
@@ -184,6 +185,7 @@ static int ata_wait_not_busy(unsigned short base, unsigned int timeout_ms)
 {
 {
 	unsigned int t0 = get_ticks_ms();
 	unsigned int t0 = get_ticks_ms();
 	while (get_ticks_ms() - t0 < timeout_ms) {
 	while (get_ticks_ms() - t0 < timeout_ms) {
+		vfs_scan_poll(t0);
 		if (!(port_inb(base + REG_CMD) & SR_BSY))
 		if (!(port_inb(base + REG_CMD) & SR_BSY))
 			return 0;
 			return 0;
 	}
 	}
@@ -201,7 +203,7 @@ static void ata_reset_channel(unsigned short base, unsigned short ctrl)
 	port_outb(ctrl, 0x04);                    /* SRST */
 	port_outb(ctrl, 0x04);                    /* SRST */
 	t0 = get_ticks_ms();
 	t0 = get_ticks_ms();
 	while (get_ticks_ms() - t0 < 10)
 	while (get_ticks_ms() - t0 < 10)
-		;
+		vfs_scan_poll(t0);
 	/* nIEN=1: the driver polls, so keep the drive's IRQ line quiet
 	/* nIEN=1: the driver polls, so keep the drive's IRQ line quiet
 	 * instead of leaving a masked level-triggered interrupt asserted. */
 	 * instead of leaving a masked level-triggered interrupt asserted. */
 	port_outb(ctrl, 0x02);
 	port_outb(ctrl, 0x02);
@@ -209,6 +211,8 @@ static void ata_reset_channel(unsigned short base, unsigned short ctrl)
 	while (get_ticks_ms() - t0 < 1000) {
 	while (get_ticks_ms() - t0 < 1000) {
 		unsigned char st = port_inb(base + REG_CMD);
 		unsigned char st = port_inb(base + REG_CMD);
 
 
+		vfs_scan_poll(t0);
+
 		if (!(st & SR_BSY) || st == 0x00 || st == 0xFF)
 		if (!(st & SR_BSY) || st == 0x00 || st == 0xFF)
 			break;
 			break;
 	}
 	}
@@ -219,6 +223,7 @@ static int ata_wait_drq(unsigned short base, unsigned int timeout_ms)
 {
 {
 	unsigned int t0 = get_ticks_ms();
 	unsigned int t0 = get_ticks_ms();
 	while (get_ticks_ms() - t0 < timeout_ms) {
 	while (get_ticks_ms() - t0 < timeout_ms) {
+		vfs_scan_poll(t0);
 		unsigned char st = port_inb(base + REG_CMD);
 		unsigned char st = port_inb(base + REG_CMD);
 		if (st & SR_ERR)
 		if (st & SR_ERR)
 			return -1;
 			return -1;
@@ -237,6 +242,7 @@ static int ata_wait_idle(unsigned short base, unsigned int timeout_ms)
 	unsigned int t0 = get_ticks_ms();
 	unsigned int t0 = get_ticks_ms();
 
 
 	while (get_ticks_ms() - t0 < timeout_ms) {
 	while (get_ticks_ms() - t0 < timeout_ms) {
+		vfs_scan_poll(t0);
 		unsigned char st = port_inb(base + REG_CMD);
 		unsigned char st = port_inb(base + REG_CMD);
 
 
 		if (st & SR_ERR)
 		if (st & SR_ERR)
@@ -280,6 +286,7 @@ static int ata_identify(int bus, int unit, unsigned short *id)
 			break;
 			break;
 		if (get_ticks_ms() - t0 > ATA_TIMEOUT_MS)
 		if (get_ticks_ms() - t0 > ATA_TIMEOUT_MS)
 			return -1;
 			return -1;
+		vfs_scan_poll(t0);
 	}
 	}
 	if (!(st & SR_DRQ))
 	if (!(st & SR_DRQ))
 		return -1;
 		return -1;

+ 37 - 14
boot.S

@@ -68,31 +68,40 @@ load_kernel:
 	movw  kernel_lba, %si
 	movw  kernel_lba, %si
 	movw  $0x07e0, %ax
 	movw  $0x07e0, %ax
 	movw  %ax, %es
 	movw  %ax, %es
-	xorw  %bx, %bx
 
 
+	/* EDD reads up to 64 sectors per INT 13h into ES:0 and then advances
+	 * ES, so the offset never wraps (one call per sector made the kernel
+	 * load ~700 BIOS calls).  CHS (floppy) stays at one sector: a longer
+	 * transfer could cross a 64 KB ISA DMA boundary. */
 load_loop:
 load_loop:
-	cmpw  $0, %cx
+	testw %cx, %cx
 	jz    load_done
 	jz    load_done
 	pushw %cx
 	pushw %cx
 	pushw %si
 	pushw %si
+	movw  $1, %ax
 	movb  boot_drive, %dl
 	movb  boot_drive, %dl
 	cmpb  $0x80, %dl
 	cmpb  $0x80, %dl
 	jb    read_chs
 	jb    read_chs
+	movw  %cx, %ax
+	cmpw  chunk_max, %ax
+	jbe   1f
+	movw  chunk_max, %ax
+1:	movw  %ax, cur_count
+	movw  %ax, 0x502
 	xorw  %ax, %ax
 	xorw  %ax, %ax
+	movw  %ax, 0x504
 	movw  %ax, 0x50a
 	movw  %ax, 0x50a
 	movw  %ax, 0x50c
 	movw  %ax, 0x50c
 	movw  %ax, 0x50e
 	movw  %ax, 0x50e
 	movb  $0x10, 0x500
 	movb  $0x10, 0x500
 	movb  %al, 0x501
 	movb  %al, 0x501
-	incw  %ax
-	movw  %ax, 0x502
-	movw  %bx, 0x504
 	movw  %es, 0x506
 	movw  %es, 0x506
 	movw  %si, 0x508
 	movw  %si, 0x508
 	movw  $0x500, %si
 	movw  $0x500, %si
 	movb  $0x42, %ah
 	movb  $0x42, %ah
 	jmp   do_int13
 	jmp   do_int13
 read_chs:
 read_chs:
+	movw  %ax, cur_count
 	movw  %si, %ax
 	movw  %si, %ax
 	xorw  %dx, %dx
 	xorw  %dx, %dx
 	movw  $18, %di
 	movw  $18, %di
@@ -107,19 +116,29 @@ read_chs:
 	movb  boot_drive, %dl
 	movb  boot_drive, %dl
 	movb  $0x02, %ah
 	movb  $0x02, %ah
 	movb  $1, %al
 	movb  $1, %al
+	xorw  %bx, %bx
 do_int13:
 do_int13:
 	int   $0x13
 	int   $0x13
 	popw  %si
 	popw  %si
 	popw  %cx
 	popw  %cx
-	jc    read_error
-	addw  $0x200, %bx
-	jnc   1f
-	movw  %es, %ax
-	addw  $0x1000, %ax
-	movw  %ax, %es
-1:	incw  %si
-	decw  %cx
-	jnz   load_loop
+	jnc   2f
+	/* A BIOS that refuses a multi-sector EDD read: reset the drive and
+	 * continue one sector per call (the old behaviour). */
+	cmpw  $1, cur_count
+	je    read_error
+	movw  $1, chunk_max
+	movb  boot_drive, %dl
+	xorw  %ax, %ax
+	int   $0x13
+	jmp   load_loop
+2:	movw  cur_count, %ax
+	addw  %ax, %si
+	subw  %ax, %cx
+	shlw  $5, %ax
+	movw  %es, %bx            /* not DX: DL carries the boot drive */
+	addw  %ax, %bx            /* into the VESA stage */
+	movw  %bx, %es
+	jmp   load_loop
 
 
 load_done:
 load_done:
 	/* Second marker: the whole kernel image was read without an INT 13h
 	/* Second marker: the whole kernel image was read without an INT 13h
@@ -132,6 +151,10 @@ load_done:
 
 
 boot_drive:
 boot_drive:
 	.byte 0
 	.byte 0
+chunk_max:
+	.word 64
+cur_count:
+	.word 0
 
 
 /* INT 13h error: say so instead of halting silently. */
 /* INT 13h error: say so instead of halting silently. */
 read_error:
 read_error:

+ 100 - 20
browser.c

@@ -181,7 +181,8 @@ static int layout_content_h;
 
 
 /* layout output */
 /* layout output */
 struct lay_run {
 struct lay_run {
-	unsigned short off, len;
+	unsigned int off;
+	unsigned short len;
 	unsigned short gap;
 	unsigned short gap;
 	unsigned short color;
 	unsigned short color;
 	unsigned short href;
 	unsigned short href;
@@ -3649,6 +3650,9 @@ static void load_page(const char *url, int local, int push)
 	const char *post = pending_post_len ? pending_post : 0;
 	const char *post = pending_post_len ? pending_post : 0;
 	unsigned int post_len = pending_post_len;
 	unsigned int post_len = pending_post_len;
 	unsigned int page_open_ms = 0, page_first_ms = 0, page_total_ms = 0;
 	unsigned int page_open_ms = 0, page_first_ms = 0, page_total_ms = 0;
+	/* Phase timings for the status line: where a slow load spends it. */
+	unsigned int t_start = u_ticks_ms(), t_fetch = 0, t_parse = 0;
+	unsigned int t_css = 0, t_paint = 0, t_js = 0, t_mark;
 	int upgraded = nav_https_upgraded;
 	int upgraded = nav_https_upgraded;
 	int retried = 0;
 	int retried = 0;
 	int rv;
 	int rv;
@@ -3712,6 +3716,7 @@ static void load_page(const char *url, int local, int push)
 	}
 	}
 	if (rv == FETCH_ABORT)
 	if (rv == FETCH_ABORT)
 		return;
 		return;
+	t_fetch = u_ticks_ms() - t_start;
 	/* Capture the page timing before CSS/script/image fetches overwrite the
 	/* Capture the page timing before CSS/script/image fetches overwrite the
 	 * shared fetch_meta block. */
 	 * shared fetch_meta block. */
 	if (rv == 0 && !local) {
 	if (rv == 0 && !local) {
@@ -3765,11 +3770,15 @@ static void load_page(const char *url, int local, int push)
 		s_copy(page_title, "Image", sizeof(page_title));
 		s_copy(page_title, "Image", sizeof(page_title));
 	} else {
 	} else {
 		br_set_viewport(ui.w);
 		br_set_viewport(ui.w);
+		t_mark = u_ticks_ms();
 		br_parse(&doc, (const char *)body, len);
 		br_parse(&doc, (const char *)body, len);
+		t_parse = u_ticks_ms() - t_mark;
 		s_copy(page_title, doc.title[0] ? doc.title : page_url,
 		s_copy(page_title, doc.title[0] ? doc.title : page_url,
 		       sizeof(page_title));
 		       sizeof(page_title));
+		t_mark = u_ticks_ms();
 		if (load_stylesheets() == FETCH_ABORT)
 		if (load_stylesheets() == FETCH_ABORT)
 			return;
 			return;
+		t_css = u_ticks_ms() - t_mark;
 		if (pending_action)
 		if (pending_action)
 			return;
 			return;
 	}
 	}
@@ -3785,14 +3794,18 @@ static void load_page(const char *url, int local, int push)
 	 * remote <script src> then does not hide the page behind the loading
 	 * remote <script src> then does not hide the page behind the loading
 	 * screen; the fetch helper pumps this UI while it waits. */
 	 * screen; the fetch helper pumps this UI while it waits. */
 	set_status("Loading scripts...", 0, 0);
 	set_status("Loading scripts...", 0, 0);
+	t_mark = u_ticks_ms();
 	redraw_now();
 	redraw_now();
+	t_paint = u_ticks_ms() - t_mark;
 	/* Start one image download before the scripts.  The other workers
 	/* Start one image download before the scripts.  The other workers
 	 * fetch the external script bodies in parallel, and the remaining
 	 * fetch the external script bodies in parallel, and the remaining
 	 * images start as soon as the script phase ends. */
 	 * images start as soon as the script phase ends. */
 	preload_max_jobs = 1;
 	preload_max_jobs = 1;
 	preload_fill_jobs();
 	preload_fill_jobs();
 	preload_wait = 0;
 	preload_wait = 0;
+	t_mark = u_ticks_ms();
 	run_page_scripts();
 	run_page_scripts();
+	t_js = u_ticks_ms() - t_mark;
 	preload_max_jobs = FETCH_WORKERS;
 	preload_max_jobs = FETCH_WORKERS;
 	if (pending_action)
 	if (pending_action)
 		return;
 		return;
@@ -3814,32 +3827,44 @@ static void load_page(const char *url, int local, int push)
 		       sizeof(post_status_text));
 		       sizeof(post_status_text));
 		post_status_ttl = 6000;
 		post_status_ttl = 6000;
 	} else if (page_total_ms) {
 	} else if (page_total_ms) {
+		/* "Loaded in 2410 ms: page 84 (headers 69), parse 40, css 310,
+		 * paint 220, js 1750" -- the phases of this load. */
 		unsigned int n = 0;
 		unsigned int n = 0;
-
+		static const char *const names[] = {
+			", parse ", ", css ", ", paint ", ", js "
+		};
+		unsigned int vals[4];
+		int k;
+
+		vals[0] = t_parse;
+		vals[1] = t_css;
+		vals[2] = t_paint;
+		vals[3] = t_js;
+		(void)page_first_ms;
 		if (starts_with(page_url, "https://"))
 		if (starts_with(page_url, "https://"))
 			s_append(post_status_text, &n,
 			s_append(post_status_text, &n,
 				 "HTTPS (not verified), ",
 				 "HTTPS (not verified), ",
 				 sizeof(post_status_text));
 				 sizeof(post_status_text));
-		else
-			s_append(post_status_text, &n, "Loaded in ",
-				 sizeof(post_status_text));
-		s_append_dec(post_status_text, &n, page_total_ms,
+		s_append(post_status_text, &n, "Loaded in ",
+			 sizeof(post_status_text));
+		s_append_dec(post_status_text, &n, u_ticks_ms() - t_start,
 			     sizeof(post_status_text));
 			     sizeof(post_status_text));
-		s_append(post_status_text, &n, " ms (headers ",
+		s_append(post_status_text, &n, " ms: page ",
+			 sizeof(post_status_text));
+		s_append_dec(post_status_text, &n, t_fetch,
+			     sizeof(post_status_text));
+		s_append(post_status_text, &n, " (headers ",
 			 sizeof(post_status_text));
 			 sizeof(post_status_text));
 		s_append_dec(post_status_text, &n, page_open_ms,
 		s_append_dec(post_status_text, &n, page_open_ms,
 			     sizeof(post_status_text));
 			     sizeof(post_status_text));
-		s_append(post_status_text, &n, " ms", sizeof(post_status_text));
-		if (page_first_ms) {
-			s_append(post_status_text, &n, ", first byte ",
+		s_append(post_status_text, &n, ")", sizeof(post_status_text));
+		for (k = 0; k < 4; k++) {
+			s_append(post_status_text, &n, names[k],
 				 sizeof(post_status_text));
 				 sizeof(post_status_text));
-			s_append_dec(post_status_text, &n, page_first_ms,
+			s_append_dec(post_status_text, &n, vals[k],
 				     sizeof(post_status_text));
 				     sizeof(post_status_text));
-			s_append(post_status_text, &n, " ms",
-				 sizeof(post_status_text));
 		}
 		}
-		s_append(post_status_text, &n, ")", sizeof(post_status_text));
-		post_status_ttl = 6000;
+		post_status_ttl = 12000;
 	} else if (starts_with(page_url, "https://")) {
 	} else if (starts_with(page_url, "https://")) {
 		s_copy(post_status_text, "HTTPS: certificate NOT verified",
 		s_copy(post_status_text, "HTTPS: certificate NOT verified",
 		       sizeof(post_status_text));
 		       sizeof(post_status_text));
@@ -4873,7 +4898,7 @@ static void flush_line(struct lay *l)
 	l->space_pending = 0;
 	l->space_pending = 0;
 }
 }
 
 
-static void add_run(struct lay *l, unsigned short off, unsigned short len,
+static void add_run(struct lay *l, unsigned int off, unsigned short len,
 		    const struct br_style *st, unsigned short href,
 		    const struct br_style *st, unsigned short href,
 		    const char *lit)
 		    const char *lit)
 {
 {
@@ -5182,14 +5207,14 @@ static void emit_word(struct lay *l, const char *s, unsigned int start,
 	avail = l->line_right - l->line_left;
 	avail = l->line_right - l->line_left;
 	if (st->nowrap) {
 	if (st->nowrap) {
 		if (n)
 		if (n)
-			add_run(l, (unsigned short)(base + start),
+			add_run(l, base + start,
 				(unsigned short)n, st, href, 0);
 				(unsigned short)n, st, href, 0);
 		return;
 		return;
 	}
 	}
 	/* A padded/bordered one-word button can be a few pixels wider than
 	/* A padded/bordered one-word button can be a few pixels wider than
 	 * the content box; keep it on one line instead of splitting it. */
 	 * the content box; keep it on one line instead of splitting it. */
 	if (l->nruns == 0 && st->has_bg && w > avail && w <= avail + 8) {
 	if (l->nruns == 0 && st->has_bg && w > avail && w <= avail + 8) {
-		add_run(l, (unsigned short)(base + start),
+		add_run(l, base + start,
 			(unsigned short)n, st, href, 0);
 			(unsigned short)n, st, href, 0);
 		return;
 		return;
 	}
 	}
@@ -5201,14 +5226,14 @@ static void emit_word(struct lay *l, const char *s, unsigned int start,
 	}
 	}
 	if (w <= avail) {
 	if (w <= avail) {
 		if (n)
 		if (n)
-			add_run(l, (unsigned short)(base + start),
+			add_run(l, base + start,
 				(unsigned short)n, st, href, 0);
 				(unsigned short)n, st, href, 0);
 		return;
 		return;
 	}
 	}
 	while (n > 0) {
 	while (n > 0) {
 		unsigned int take = fit_prefix(l, s + off, n, st, avail);
 		unsigned int take = fit_prefix(l, s + off, n, st, avail);
 
 
-		add_run(l, (unsigned short)(base + off),
+		add_run(l, base + off,
 			(unsigned short)take, st, href, 0);
 			(unsigned short)take, st, href, 0);
 		off += take;
 		off += take;
 		n -= take;
 		n -= take;
@@ -8380,6 +8405,59 @@ struct br_deferred {
 static struct br_deferred deferred[BR_MAX_DEFERRED];
 static struct br_deferred deferred[BR_MAX_DEFERRED];
 static int ndeferred;
 static int ndeferred;
 
 
+/* ::before/::after generated text (br_pseudo()) as literal runs in the
+ * element's line.  A block-level pseudo-element gets its own line; its box
+ * (margins, padding, background) is not drawn. */
+static void emit_pseudo(struct lay *l, unsigned int node,
+			const struct br_style *st, int pe, unsigned short link)
+{
+	char buf[160];
+	struct br_style ps;
+	unsigned int n = br_pseudo(&doc, node, pe, st, &ps, buf, sizeof(buf));
+	unsigned int i = 0;
+	int block, space_w;
+
+	if (!n)
+		return;
+	block = ps.display == BR_DISPLAY_BLOCK || ps.display == BR_DISPLAY_FLEX;
+	if (block)
+		flush_line(l);
+	if (ps.uppercase)
+		for (i = 0; i < n; i++)
+			if (buf[i] >= 'a' && buf[i] <= 'z')
+				buf[i] = (char)(buf[i] - 'a' + 'A');
+	space_w = (int)face_text_width(l->ui, &ps, " ", 1);
+	if (space_w <= 0)
+		space_w = 8;
+	i = 0;
+	while (i < n) {
+		char word[28];
+		unsigned int k = 0;
+		int w;
+
+		if (buf[i] == ' ') {
+			l->space_pending = 1;
+			i++;
+			continue;
+		}
+		while (i < n && buf[i] != ' ' && k + 1 < sizeof(word))
+			word[k++] = buf[i++];
+		word[k] = '\0';
+		if (l->space_pending &&
+		    (l->nruns > 0 || l->line_x > l->line_left) &&
+		    l->line_x + space_w <= l->line_right)
+			l->line_x += space_w;
+		l->space_pending = 0;
+		w = (int)face_text_width(l->ui, &ps, word, k);
+		if (l->line_x + w > l->line_right &&
+		    (l->nruns > 0 || l->line_x > l->line_left))
+			flush_line(l);
+		add_run(l, 0, 0, &ps, link, word);
+	}
+	if (block)
+		flush_line(l);
+}
+
 static void layout_children(struct lay *l, unsigned int parent,
 static void layout_children(struct lay *l, unsigned int parent,
 			    const struct br_style *pst, int depth,
 			    const struct br_style *pst, int depth,
 			    unsigned short link)
 			    unsigned short link)
@@ -8388,6 +8466,7 @@ static void layout_children(struct lay *l, unsigned int parent,
 
 
 	if (depth > 48)
 	if (depth > 48)
 		return;
 		return;
+	emit_pseudo(l, parent, pst, 1, link);
 	n = doc.nodes[parent].first;
 	n = doc.nodes[parent].first;
 	while (n) {
 	while (n) {
 		struct br_node *nd = &doc.nodes[n];
 		struct br_node *nd = &doc.nodes[n];
@@ -8547,6 +8626,7 @@ static void layout_children(struct lay *l, unsigned int parent,
 		}
 		}
 		n = nd->next;
 		n = nd->next;
 	}
 	}
+	emit_pseudo(l, parent, pst, 2, link);
 }
 }
 
 
 /* One layout state, kept out of the 8 KB user stack: it holds 96 line
 /* One layout state, kept out of the 8 KB user stack: it holds 96 line

A diferenza do arquivo foi suprimida porque é demasiado grande
+ 1047 - 589
browser_html.c


+ 17 - 1
browser_html.h

@@ -16,6 +16,7 @@
 #define BR_FACE_FAMILY   40
 #define BR_FACE_FAMILY   40
 #define BR_FACE_URL      192
 #define BR_FACE_URL      192
 #define BR_MAX_VARS      512
 #define BR_MAX_VARS      512
+#define BR_MAX_CONTENT   512
 #define BR_TAG_MAX       128
 #define BR_TAG_MAX       128
 #define BR_RULE_HASH     256
 #define BR_RULE_HASH     256
 #define BR_DECLS         48
 #define BR_DECLS         48
@@ -196,6 +197,7 @@ struct br_style {
 	unsigned short fill, stroke;
 	unsigned short fill, stroke;
 	unsigned short stroke_w;
 	unsigned short stroke_w;
 	unsigned char inline_flex;  /* display:inline-flex (not inherited) */
 	unsigned char inline_flex;  /* display:inline-flex (not inherited) */
+	unsigned short content;     /* ::before/::after content: index + 1 */
 };
 };
 
 
 #define BR_PAINT_UNSET   0    /* fill: black, stroke: none */
 #define BR_PAINT_UNSET   0    /* fill: black, stroke: none */
@@ -213,7 +215,7 @@ struct br_node {
 	unsigned short first;
 	unsigned short first;
 	unsigned short last;
 	unsigned short last;
 	unsigned short next;
 	unsigned short next;
-	unsigned short text_off;    /* text arena offset (text nodes) */
+	unsigned int text_off;      /* text arena offset (text nodes) */
 	unsigned short text_len;
 	unsigned short text_len;
 	unsigned int href_off;
 	unsigned int href_off;
 	unsigned int class_off;
 	unsigned int class_off;
@@ -238,6 +240,7 @@ struct br_node {
 	unsigned char svg_rx;       /* inline SVG rect corner radius */
 	unsigned char svg_rx;       /* inline SVG rect corner radius */
 	struct br_style style;      /* computed-style cache */
 	struct br_style style;      /* computed-style cache */
 	unsigned char style_valid;
 	unsigned char style_valid;
+	unsigned char pe_state;     /* br_pseudo() cache: 1/4 checked, 2/8 has */
 };
 };
 
 
 struct br_decl {
 struct br_decl {
@@ -252,6 +255,7 @@ struct br_rule {
 	unsigned short spec;        /* selector specificity */
 	unsigned short spec;        /* selector specificity */
 	unsigned char nd;
 	unsigned char nd;
 	unsigned char single;       /* one compound, no ancestor walk needed */
 	unsigned char single;       /* one compound, no ancestor walk needed */
+	unsigned char pe;           /* 0 element, 1 ::before, 2 ::after */
 	struct br_decl d[BR_DECLS];
 	struct br_decl d[BR_DECLS];
 };
 };
 
 
@@ -288,6 +292,11 @@ struct br_doc {
 	unsigned short rule_bucket[BR_RULE_HASH];
 	unsigned short rule_bucket[BR_RULE_HASH];
 	unsigned short rule_tag[BR_TAG_MAX];
 	unsigned short rule_tag[BR_TAG_MAX];
 	unsigned short rule_any;
 	unsigned short rule_any;
+	/* ::before/::after content strings (attr arena offsets); attr(x) is
+	 * stored as \001x\002 and expanded per element. */
+	unsigned int content_off[BR_MAX_CONTENT];
+	unsigned short ncontent;
+	unsigned short npe_rules;
 	int truncated;
 	int truncated;
 	int plain;                  /* text/plain: one preformatted text node */
 	int plain;                  /* text/plain: one preformatted text node */
 	const char *script_src;     /* buffer the script offsets point into */
 	const char *script_src;     /* buffer the script offsets point into */
@@ -297,6 +306,13 @@ struct br_doc {
 };
 };
 
 
 void br_doc_init(struct br_doc *d);
 void br_doc_init(struct br_doc *d);
+/* ::before (pe 1) or ::after (pe 2) of an element: the pseudo-element's
+ * style (inheriting from the element's style `elem`) and its generated
+ * text.  Returns the text length; 0 when there is no content, or when the
+ * pseudo-element is display:none or absolutely positioned. */
+unsigned int br_pseudo(struct br_doc *d, unsigned int node, int pe,
+		       const struct br_style *elem, struct br_style *out,
+		       char *buf, unsigned int cap);
 void br_set_rule_store(struct br_doc *d, struct br_rule *buf,
 void br_set_rule_store(struct br_doc *d, struct br_rule *buf,
 		       unsigned int cap);
 		       unsigned int cap);
 int  br_parse(struct br_doc *d, const char *html, unsigned int len);
 int  br_parse(struct br_doc *d, const char *html, unsigned int len);

+ 90 - 9
ccmp.c

@@ -175,17 +175,69 @@ static int ccm_decrypt(const unsigned char key[16],
 	return 0;
 	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,
 	/* AAD: FC (mute Retry/PwrMgmt/MoreData, force Protected), A1, A2,
 	 * A3, SC (sequence number zeroed, fragment kept).  The 2-byte
 	 * 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[10] = f[hdr + 4];                /* PN2 */
 	nonce[11] = f[hdr + 1];                /* PN1 */
 	nonce[11] = f[hdr + 1];                /* PN1 */
 	nonce[12] = f[hdr + 0];                /* PN0 */
 	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,
 	return ccm_decrypt(key, nonce, aad, aad_len, f + hdr + 8, clen,
 			   f + len - 8);
 			   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
  * 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
  * 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,
 int ccmp_rx_decrypt(unsigned char *frame, unsigned int len, unsigned int hdr,
 		    const unsigned char key[16]);
 		    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
 #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 <stdio.h>
 #include <string.h>
 #include <string.h>
 
 
@@ -123,6 +124,37 @@ int main(void)
 		} else {
 		} else {
 			printf("ok   ccmp rx bad tag\n");
 			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;
 	return failed;

+ 27 - 5
disk.c

@@ -1,6 +1,7 @@
 /* Disk layer: read/write sectors via direct FDC + ISA DMA in protected mode. */
 /* Disk layer: read/write sectors via direct FDC + ISA DMA in protected mode. */
 
 
 #include "disk.h"
 #include "disk.h"
+#include "vfs.h"
 #include "io.h"
 #include "io.h"
 #include "timer.h"
 #include "timer.h"
 
 
@@ -81,6 +82,7 @@ static int fdc_wait_rqm(void)
 	while ((get_ticks_ms() - t0) < 500) {
 	while ((get_ticks_ms() - t0) < 500) {
 		if (inb(FDC_MSR) & MSR_RQM)
 		if (inb(FDC_MSR) & MSR_RQM)
 			return 0;
 			return 0;
+		vfs_scan_poll(t0);
 	}
 	}
 	return -1;
 	return -1;
 }
 }
@@ -111,7 +113,8 @@ static int fdc_wait_irq(void)
 	while (!fdc_irq_fired) {
 	while (!fdc_irq_fired) {
 		if ((get_ticks_ms() - t0) >= irq_timeout_ms)
 		if ((get_ticks_ms() - t0) >= irq_timeout_ms)
 			return -1;
 			return -1;
-		__asm__ volatile ("hlt");
+		if (!vfs_scan_relax(1))
+			__asm__ volatile ("hlt");
 	}
 	}
 	return 0;
 	return 0;
 }
 }
@@ -130,6 +133,7 @@ static int fdc_controller_alive(void)
 			return 1;
 			return 1;
 		if ((get_ticks_ms() - t0) >= 200)
 		if ((get_ticks_ms() - t0) >= 200)
 			return 0;
 			return 0;
+		vfs_scan_poll(t0);
 	}
 	}
 }
 }
 
 
@@ -174,7 +178,8 @@ static void fdc_motor_on(void)
 	{
 	{
 		unsigned int t0 = get_ticks_ms();
 		unsigned int t0 = get_ticks_ms();
 		while ((get_ticks_ms() - t0) < MOTOR_DELAY_MS)
 		while ((get_ticks_ms() - t0) < MOTOR_DELAY_MS)
-			__asm__ volatile ("hlt");
+			if (!vfs_scan_relax(MOTOR_DELAY_MS))
+				__asm__ volatile ("hlt");
 	}
 	}
 }
 }
 
 
@@ -194,7 +199,7 @@ static int fdc_reset(void)
 	{
 	{
 		unsigned int t0 = get_ticks_ms();
 		unsigned int t0 = get_ticks_ms();
 		while ((get_ticks_ms() - t0) < 10)
 		while ((get_ticks_ms() - t0) < 10)
-			;
+			vfs_scan_poll(t0);
 	}
 	}
 
 
 	/* Release reset: controller on, DMA/IRQ enabled, no motor.
 	/* Release reset: controller on, DMA/IRQ enabled, no motor.
@@ -269,7 +274,7 @@ static int fdc_seek(unsigned int cylinder, unsigned int head)
 	{
 	{
 		unsigned int t0 = get_ticks_ms();
 		unsigned int t0 = get_ticks_ms();
 		while ((get_ticks_ms() - t0) < SEEK_SETTLE_MS)
 		while ((get_ticks_ms() - t0) < SEEK_SETTLE_MS)
-			;
+			vfs_scan_poll(t0);
 	}
 	}
 	return 0;
 	return 0;
 }
 }
@@ -491,7 +496,7 @@ static int fdc_init_once(void)
 	{
 	{
 		unsigned int t0 = get_ticks_ms();
 		unsigned int t0 = get_ticks_ms();
 		while ((get_ticks_ms() - t0) < 10)
 		while ((get_ticks_ms() - t0) < 10)
-			;
+			vfs_scan_poll(t0);
 	}
 	}
 
 
 	fdc_irq_fired = 0;
 	fdc_irq_fired = 0;
@@ -554,12 +559,29 @@ void disk_set_drive(unsigned int drive)
 /* Quick presence/media probe used by the boot scan.  One command with a short
 /* Quick presence/media probe used by the boot scan.  One command with a short
  * timeout, never the ten-retry recovery loop: an empty drive must not hold the
  * timeout, never the ten-retry recovery loop: an empty drive must not hold the
  * UI back.  Normal I/O after a successful mount still gets full retries. */
  * UI back.  Normal I/O after a successful mount still gets full retries. */
+/* CMOS register 0x10 holds the BIOS's floppy drive types (high nibble
+ * drive 0, low nibble drive 1; 0 = not installed).  Every AT-compatible
+ * BIOS fills it in.  Without this gate a machine whose FDC exists but has
+ * no drive (QEMU without -fda, most laptops/boards with a Super I/O FDC)
+ * paid the 300 ms motor spin-up plus a probe timeout per drive at boot. */
+static int cmos_floppy_type(unsigned int drive)
+{
+	unsigned char v;
+
+	outb(0x70, 0x80 | 0x10);        /* bit 7: NMI disabled while reading */
+	v = inb(0x71);
+	outb(0x70, 0x00);
+	return drive == 0 ? (v >> 4) & 0x0F : v & 0x0F;
+}
+
 int disk_probe(unsigned int drive)
 int disk_probe(unsigned int drive)
 {
 {
 	int rc;
 	int rc;
 
 
 	if (drive > 1 || fdc_absent)
 	if (drive > 1 || fdc_absent)
 		return 0;
 		return 0;
+	if (cmos_floppy_type(drive) == 0)
+		return 0;
 
 
 	irq_timeout_ms = PROBE_TIMEOUT_MS;
 	irq_timeout_ms = PROBE_TIMEOUT_MS;
 	disk_set_drive(drive);
 	disk_set_drive(drive);

+ 18 - 5
docs/boot.html

@@ -145,8 +145,11 @@
 		<p>
 		<p>
 			The boot sector carries a FAT16 BPB, loads the contiguous kernel
 			The boot sector carries a FAT16 BPB, loads the contiguous kernel
 			directly after itself to <code>0x7E00</code>, and jumps there.
 			directly after itself to <code>0x7E00</code>, and jumps there.
-			Floppy drives use CHS; fixed disks use <code>INT 13h AH=42h</code>
-			(EDD LBA). The sector count and the rootfs LBA are patched into the
+			Floppy drives use CHS, one sector per call; fixed disks use
+			<code>INT 13h AH=42h</code> (EDD LBA) with up to 64 sectors per call
+			into <code>ES:0</code>, advancing <code>ES</code> so the offset never
+			wraps (one call per sector used to make the kernel load ~700 BIOS
+			calls). The sector count and the rootfs LBA are patched into the
 			boot sector by the Makefile.
 			boot sector by the Makefile.
 		</p>
 		</p>
 		<p>
 		<p>
@@ -163,7 +166,7 @@
 		<ol>
 		<ol>
 			<li>Probe the <code>INT 15h</code> E820 memory map into <code>0x1000</code>.</li>
 			<li>Probe the <code>INT 15h</code> E820 memory map into <code>0x1000</code>.</li>
 			<li>Enable A20 and read the <code>NBROOT1</code> rootfs header at the boot sector's <code>pack_lba</code> field.</li>
 			<li>Enable A20 and read the <code>NBROOT1</code> rootfs header at the boot sector's <code>pack_lba</code> field.</li>
-			<li>Copy the compressed pack directly to <code>0x900000</code> above the 8 MB VGA-shadow reserve, in 16 KB staging chunks at <code>0x3000</code>, using a temporary flat protected-mode <code>rep movsb</code>. The X201 BIOS's <code>INT 15h AH=87h</code> block move silently copies zeros to this range, so the CPU copy is used instead.</li>
+			<li>Copy the compressed pack directly to <code>0x900000</code> above the 8 MB VGA-shadow reserve, in 16 KB staging chunks at <code>0x3000</code> (one <code>INT 13h</code> per chunk: 32 sectors over EDD, or up to the end of the track over CHS, which never crosses a 64 KB DMA boundary; a BIOS that refuses a multi-sector request falls back to one sector per call), using a temporary flat protected-mode <code>rep movsb</code>. The X201 BIOS's <code>INT 15h AH=87h</code> block move silently copies zeros to this range, so the CPU copy is used instead.</li>
 			<li>Pick the best VBE mode: at least 16 bpp, a linear framebuffer, it must fit the card's VRAM, and the width must be at most 1280 pixels. Mode information is written to <code>0x600</code>; the 12-dword boot-info block goes to <code>0x500</code>.</li>
 			<li>Pick the best VBE mode: at least 16 bpp, a linear framebuffer, it must fit the card's VRAM, and the width must be at most 1280 pixels. Mode information is written to <code>0x600</code>; the 12-dword boot-info block goes to <code>0x500</code>.</li>
 			<li>Enter protected mode and jump to <code>kernel_entry</code>.</li>
 			<li>Enter protected mode and jump to <code>kernel_entry</code>.</li>
 		</ol>
 		</ol>
@@ -399,11 +402,21 @@
 			<li>Create the process table, then start the PIT and verify it ticks.</li>
 			<li>Create the process table, then start the PIT and verify it ticks.</li>
 			<li>Start APs through INIT-SIPI-SIPI (they idle until scheduling is enabled).</li>
 			<li>Start APs through INIT-SIPI-SIPI (they idle until scheduling is enabled).</li>
 			<li>Initialise PS/2, USB HID and the VGA shadow buffer.</li>
 			<li>Initialise PS/2, USB HID and the VGA shadow buffer.</li>
-			<li>Inflate the command pack into <code>/ramfs</code>; start the WM and the first frame.</li>
+			<li>Inflate the command pack into <code>/ramfs</code> (table-driven whole-buffer inflate); start the WM and the first frame.</li>
 			<li>Probe the NIC, restore retained network config, start the kworker.</li>
 			<li>Probe the NIC, restore retained network config, start the kworker.</li>
-			<li>Mount FAT devices; call <code>wm_server_boot_ready()</code> so the WM spawns the first terminal; run DHCP if needed.</li>
+			<li>Start the <code>bootscan</code> kernel thread (storage probe + FAT mounts, then the <code>/data</code> WiFi autoconnect once the boot DHCP is done) and publish <code>vfs_scan_busy</code>; call <code>wm_server_boot_ready()</code> at once so the WM spawns the first terminal while the disks are probed; run DHCP if needed.</li>
 			<li>Enable scheduling on the APs and enter the kernel idle loop.</li>
 			<li>Enable scheduling on the APs and enter the kernel idle loop.</li>
 		</ol>
 		</ol>
+		<p>
+			The terminal no longer waits for storage: <code>/ramfs</code> and
+			<code>/share</code> operations (including spawning commands from
+			<code>/ramfs/bin</code>) skip the scan wait, a disk path waits for it,
+			and the scan thread sleeps through its long probe timeouts.  The
+			end point of a boot measurement is the one-shot COM1 line
+			<code>wm: first window frame (term) at N ms</code> (uptime), written
+			by <span class="file">sys_wm.c</span> when the first application
+			window presents.
+		</p>
 		<p>
 		<p>
 			The boot trace lines written along the way (<code>boot_trace</code>,
 			The boot trace lines written along the way (<code>boot_trace</code>,
 			<code>boot_val</code>) go to the framebuffer console and COM1, so a
 			<code>boot_val</code>) go to the framebuffer console and COM1, so a

+ 2 - 1
docs/development.html

@@ -80,7 +80,7 @@
 			<li><strong>User commands.</strong> Linked at <code>0x40000000</code> with <code>ucmd.ld</code>, stripped, and packed as <code>bin/&lt;name&gt;</code>.</li>
 			<li><strong>User commands.</strong> Linked at <code>0x40000000</code> with <code>ucmd.ld</code>, stripped, and packed as <code>bin/&lt;name&gt;</code>.</li>
 			<li><strong>UEFI.</strong> <code>x86_64-w64-mingw32-gcc</code> builds <code>BOOTX64.EFI</code>; <code>make all</code> does not require it.</li>
 			<li><strong>UEFI.</strong> <code>x86_64-w64-mingw32-gcc</code> builds <code>BOOTX64.EFI</code>; <code>make all</code> does not require it.</li>
 			<li><strong>Images.</strong> <code>mtools</code> builds <code>root.img</code> and the ESP.</li>
 			<li><strong>Images.</strong> <code>mtools</code> builds <code>root.img</code> and the ESP.</li>
-			<li><strong>WiFi firmware.</strong> The pack includes <code>firmware/iwlwifi-7265D-29.ucode</code>, decompressed with <code>zstd</code> from the host's linux-firmware (<code>WIFI_FW=/path/to/file[.zst]</code> overrides the search). Without it the build prints a note and packs no firmware; the floppy images always use the firmware-less <code>cmds-lite.pack</code>.</li>
+			<li><strong>WiFi firmware.</strong> The pack includes <code>firmware/iwlwifi-7265D-29.ucode</code> (Chromebook) and <code>firmware/iwlwifi-6000-4.ucode</code> (X201), decompressed with <code>zstd</code> from the host's linux-firmware (<code>WIFI_FW_7265D=</code> / <code>WIFI_FW_6000=/path/to/file[.zst]</code> override the search). A missing file only prints a note and leaves that card without firmware; the floppy images always use the firmware-less <code>cmds-lite.pack</code>.</li>
 		</ul>
 		</ul>
 	</section>
 	</section>
 
 
@@ -302,6 +302,7 @@ make net-cmds                   # replace /ramfs only</code></pre>
 		<h2>Debugging</h2>
 		<h2>Debugging</h2>
 		<ul>
 		<ul>
 			<li><strong>Serial log.</strong> <code>make monitor</code> shows the boot trace and printf-style driver output. The <code>NebOS v… booted</code> line is written before the framebuffer is ready.</li>
 			<li><strong>Serial log.</strong> <code>make monitor</code> shows the boot trace and printf-style driver output. The <code>NebOS v… booted</code> line is written before the framebuffer is ready.</li>
+			<li><strong>Boot time.</strong> Timestamp COM1 on the host (QEMU <code>-serial stdio</code>): the kernel starts at <code>NebOS v… booted</code>, the desktop at <code>wm: first frame</code>, and the terminal is on screen at <code>wm: first window frame (term) at N ms</code> (kernel uptime).  On QEMU the USB image reaches it about 0.65 s after the kernel starts.</li>
 			<li><strong>Network console.</strong> <code>make netmon</code> listens to UDP 19290 for serial and terminal output; <code>netconsole host &lt;ip&gt;</code> narrows the target.</li>
 			<li><strong>Network console.</strong> <code>make netmon</code> listens to UDP 19290 for serial and terminal output; <code>netconsole host &lt;ip&gt;</code> narrows the target.</li>
 			<li><strong>dbg.</strong> The ring-0 serial monitor accepts <code>r addr len</code>, <code>w addr b0 …</code>, <code>i port</code>, <code>o port val</code>, <code>h</code> and <code>q</code>.</li>
 			<li><strong>dbg.</strong> The ring-0 serial monitor accepts <code>r addr len</code>, <code>w addr b0 …</code>, <code>i port</code>, <code>o port val</code>, <code>h</code> and <code>q</code>.</li>
 			<li><strong>Panic.</strong> A ring-0 fault dumps vector, error code, registers, CRs, EIP bytes and stack candidates to the LFB and COM1. Resolve addresses with <code>nm kernel.elf</code> / <code>addr2line -e kernel.elf</code>.</li>
 			<li><strong>Panic.</strong> A ring-0 fault dumps vector, error code, registers, CRs, EIP bytes and stack candidates to the LFB and COM1. Resolve addresses with <code>nm kernel.elf</code> / <code>addr2line -e kernel.elf</code>.</li>

+ 105 - 15
docs/drivers.html

@@ -55,7 +55,7 @@
 			<li><a href="#pci">PCI &amp; discovery</a></li>
 			<li><a href="#pci">PCI &amp; discovery</a></li>
 			<li><a href="#storage">Storage</a></li>
 			<li><a href="#storage">Storage</a></li>
 			<li><a href="#usb">USB stack</a></li>
 			<li><a href="#usb">USB stack</a></li>
-			<li><a href="#wifi">WiFi (Intel 7265D/3165)</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="#hid">Input</a></li>
 			<li><a href="#serial">Serial</a></li>
 			<li><a href="#serial">Serial</a></li>
 			<li><a href="#sensors">Sensors &amp; power</a></li>
 			<li><a href="#sensors">Sensors &amp; power</a></li>
@@ -85,7 +85,8 @@
 					<tr><td>eMMC</td><td><span class="file">sdhci.c</span></td><td>Intel Braswell SDHCI, PCI <code>00:10.0</code> (<code>8086:2294</code>)</td><td>Polled PIO, 1-bit/25 MHz; no DMA/IRQ</td></tr>
 					<tr><td>eMMC</td><td><span class="file">sdhci.c</span></td><td>Intel Braswell SDHCI, PCI <code>00:10.0</code> (<code>8086:2294</code>)</td><td>Polled PIO, 1-bit/25 MHz; no DMA/IRQ</td></tr>
 					<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>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>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">wifi.c</span></td><td>PCIe, Intel 7265D/3165 (<code>8086:095a/095b/3165/3166</code>)</td><td>Polled RX ring; firmware from <code>/ramfs/firmware</code>, loaded on first use</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>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>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>
 					<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>
@@ -296,7 +297,17 @@
 			recursively. This is what makes the X201's internal PCH hubs work.
 			recursively. This is what makes the X201's internal PCH hubs work.
 			Hotplug watches root ports; a device behind a hub is only found when a
 			Hotplug watches root ports; a device behind a hub is only found when a
 			full scan runs (<code>lsusb</code> also bumps the USB generation so
 			full scan runs (<code>lsusb</code> also bumps the USB generation so
-			<code>usbhid</code> re-attaches).
+			<code>usbhid</code> re-attaches).  Every HCD delay goes through
+			<code>usb_delay_ms()</code>: inside a scan owned by a sleepable
+			process (<code>lsusb</code>, the kworker's HID rescan/hotplug) it
+			sleeps and drops the BKL, so port resets and settle times no longer
+			freeze the system.  While such a scan sleeps, the
+			<span class="file">usb.c</span> wrappers refuse controller access
+			from any other context (<code>usb_scan_foreign()</code>: control and
+			bulk transfers fail, interrupt polls return no data, ports read as
+			connected) and <code>usbhid_poll</code> skips its tick.  A USB NIC,
+			the RTL8821AU or a USB disk therefore sees a transient error during a
+			bus scan; the scan re-enumerates those devices anyway.
 		</p>
 		</p>
 
 
 		<h3>Mass storage</h3>
 		<h3>Mass storage</h3>
@@ -341,12 +352,21 @@
 	</section>
 	</section>
 
 
 	<section id="wifi">
 	<section id="wifi">
-		<h2>WiFi (Intel 7265D/3165)</h2>
+		<h2>WiFi (Intel 7265D/3165, 6200/6300, RTL8821AU)</h2>
 		<p>
 		<p>
-			The HP Chromebook (Braswell) has no wired NIC; its built-in Intel
-			Wireless-AC 7265 rev D or 3165 is driven in three layers, all
-			polled. The kernel part is about 20 KB of code; every ring and
-			buffer is page-allocated so nothing new lands in the kernel BSS.
+			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>).
+			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>
 		</p>
 		<figure class="figure">
 		<figure class="figure">
 			<svg viewBox="0 0 920 300" role="img" aria-labelledby="wifi-title wifi-desc">
 			<svg viewBox="0 0 920 300" role="img" aria-labelledby="wifi-title wifi-desc">
@@ -366,10 +386,10 @@
 				<text class="svg-sub" x="60" y="152">auth/assoc · 802.11 ↔ Ethernet (LLC/SNAP, CCMP header) · EAPOL queue · SYS_WIFI</text>
 				<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"/>
 				<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 — firmware commands: INIT/RT image, NVM, scan, contexts, stations, keys</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"/>
 				<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="M240 76v28"/>
 				<path class="svg-line blue" d="M234 98l6 7 6-7"/>
 				<path class="svg-line blue" d="M234 98l6 7 6-7"/>
@@ -384,7 +404,9 @@
 				frames and hands the TK/GTK to the firmware, which does CCMP.
 				frames and hands the TK/GTK to the firmware, which does CCMP.
 				Sources: <span class="file">ucmd_wifi.c</span>,
 				Sources: <span class="file">ucmd_wifi.c</span>,
 				<span class="file">wpa.c</span>, <span class="file">wifi.c</span>,
 				<span class="file">wpa.c</span>, <span class="file">wifi.c</span>,
-				<span class="file">iwl_mvm.c</span>, <span class="file">iwl_trans.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>
 			</figcaption>
 		</figure>
 		</figure>
 		<ul>
 		<ul>
@@ -396,6 +418,69 @@
 			<li><strong>Bring-up on first use.</strong> The boot probe only finds the PCI function. The firmware is loaded by the first <code>wifi</code> command, so boot time and RAM do not change when WiFi is unused. The netdev reports <code>link_up() = 0</code> until it is connected, so the boot DHCP is skipped.</li>
 			<li><strong>Bring-up on first use.</strong> The boot probe only finds the PCI function. The firmware is loaded by the first <code>wifi</code> command, so boot time and RAM do not change when WiFi is unused. The netdev reports <code>link_up() = 0</code> until it is connected, so the boot DHCP is skipped.</li>
 			<li><strong>kexec.</strong> <code>netdev_shutdown()</code> always stops the card through <code>wifi_shutdown()</code>, even when it is not the active netdev, because its RX ring would keep writing into pages the next kernel reuses.</li>
 			<li><strong>kexec.</strong> <code>netdev_shutdown()</code> always stops the card through <code>wifi_shutdown()</code>, even when it is not the active netdev, because its RX ring would keep writing into pages the next kernel reuses.</li>
 		</ul>
 		</ul>
+		<h3>6200/6300: the DVM firmware</h3>
+		<p>
+			The transport differences live in one per-family descriptor
+			(<code>fam_7000</code>/<code>fam_6000</code> in
+			<span class="file">iwl_trans.c</span>):
+		</p>
+		<div class="table-wrap">
+			<table>
+				<thead>
+					<tr><th>Parameter</th><th>7265D/3165 (MVM)</th><th>6200/6300 (DVM)</th></tr>
+				</thead>
+				<tbody>
+					<tr><td>Firmware</td><td><code>iwlwifi-7265D-29.ucode</code> (TLV)</td><td><code>iwlwifi-6000-4.ucode</code> (v2 header: inst/data sections at <code>0x0</code>/<code>0x800000</code>)</td></tr>
+					<tr><td>Scheduler queues</td><td>31</td><td>20</td></tr>
+					<tr><td>Command queue / FIFO</td><td>0 / 7</td><td>4 / 7</td></tr>
+					<tr><td>Data queues</td><td>aux 1, BSS 4 (configured by firmware, <code>SCD_QUEUE_CFG</code>)</td><td>VO 0 (management), VI 1, BE 2 (data), BK 3 (configured by the transport)</td></tr>
+					<tr><td>Command header</td><td>8-byte wide (group 1)</td><td>4-byte</td></tr>
+					<tr><td>Byte-count table</td><td>dwords</td><td>bytes</td></tr>
+					<tr><td>MAC address</td><td>NVM via <code>NVM_ACCESS_CMD</code></td><td>OTP read by the host through <code>CSR_EEPROM_REG</code></td></tr>
+				</tbody>
+			</table>
+		</div>
+		<ul>
+			<li><strong>Bring-up.</strong> The host reads the 2 KB OTP (MAC at word 0x15, radio config at 0x48, crystal calibration through the calibration link), writes the radio SKU bits during NIC init, then runs the INIT image: <code>CALIBRATION_CFG_CMD</code> and every <code>CALIBRATION_RES_NOTIFICATION</code> stored by op code until <code>CALIBRATION_COMPLETE</code>. The runtime image gets the stored results back as <code>REPLY_PHY_CALIBRATION_CMD</code>, DC calibration, TX antennas, an RXON, TX power and a CAM power table.</li>
+			<li><strong>RXON.</strong> DVM has no MAC/PHY contexts: one <code>REPLY_RXON</code> carries channel, BSSID, filters and association. A full RXON first clears the association, which wipes the firmware's station table, so the broadcast station (15) and the AP station (0) are re-added with their fixed-rate <code>LINK_QUALITY</code> tables, and a new TX power command follows every retune.</li>
+			<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>
 
 
 	<section id="hid">
 	<section id="hid">
@@ -578,7 +663,8 @@
 			<li><strong>eMMC.</strong> Polled 1-bit/25 MHz PIO; no DMA, IRQ, CMD6 bus-width/high-speed/DDR switch or hotplug. Only the eMMC function is driven; the SDIO/SD functions and SD card reader are not.</li>
 			<li><strong>eMMC.</strong> Polled 1-bit/25 MHz PIO; no DMA, IRQ, CMD6 bus-width/high-speed/DDR switch or hotplug. Only the eMMC function is driven; the SDIO/SD functions and SD card reader are not.</li>
 			<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>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>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 and 3165 only (the 7265D firmware); 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>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>
 			<li><strong>Suspend.</strong> There is no S0ix/suspend path; package C-states and the EC sleep handshake remain firmware-managed.</li>
 		</ul>
 		</ul>
 	</section>
 	</section>
@@ -595,10 +681,14 @@
 			<li><code>uhci.c</code>, <code>ohci.c</code>, <code>ehci.c</code>, <code>xhci.c</code></li>
 			<li><code>uhci.c</code>, <code>ohci.c</code>, <code>ehci.c</code>, <code>xhci.c</code></li>
 			<li><code>usbmsc.c</code>, <code>usbmsc.h</code> — USB BOT/SCSI mass storage</li>
 			<li><code>usbmsc.c</code>, <code>usbmsc.h</code> — USB BOT/SCSI mass storage</li>
 			<li><code>rndis.c</code>, <code>rndis.h</code> — USB RNDIS tethering</li>
 			<li><code>rndis.c</code>, <code>rndis.h</code> — USB RNDIS tethering</li>
-			<li><code>iwl_trans.c</code>, <code>iwl_trans.h</code> — Intel 7265D/3165 PCIe transport</li>
-			<li><code>iwl_mvm.c</code>, <code>iwl_mvm.h</code> — WiFi firmware commands</li>
+			<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>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>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>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>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>
 			<li><code>i2c.c</code>, <code>i2c.h</code> — DesignWare/LPSS I2C master</li>

+ 6 - 1
docs/filesystems.html

@@ -165,7 +165,7 @@
 			<li><strong>Read/write.</strong> Read, write, delete, mkdir and rmdir are implemented; FAT32 allocation uses an FSInfo-seeded free-cluster hint.</li>
 			<li><strong>Read/write.</strong> Read, write, delete, mkdir and rmdir are implemented; FAT32 allocation uses an FSInfo-seeded free-cluster hint.</li>
 			<li><strong>8.3 names.</strong> The on-disk name is the canonical one. The shell and <code>files</code> display the space-padded form; a long name cannot be created.</li>
 			<li><strong>8.3 names.</strong> The on-disk name is the canonical one. The shell and <code>files</code> display the space-padded form; a long name cannot be created.</li>
 			<li><strong>Directory cache.</strong> The last FAT directory's first 64 entries are cached in <code>vfs.c</code>, so WebDAV's index sequence is one walk instead of N. Any write invalidates the cache.</li>
 			<li><strong>Directory cache.</strong> The last FAT directory's first 64 entries are cached in <code>vfs.c</code>, so WebDAV's index sequence is one walk instead of N. Any write invalidates the cache.</li>
-			<li><strong>Serialization.</strong> A file operation runs in a non-preemptible section because the FAT driver has static state and the floppy needs IRQ 6; <code>vfs_scan_busy</code>/<code>format_busy</code> make it sleep first while a mount scan or a format owns the disks. A format releases the BKL for its raw-sector loop, and only disk-backed mounts wait for it, so <code>/ramfs</code> and <code>/share</code> stay usable.</li>
+			<li><strong>Serialization.</strong> A file operation runs in a non-preemptible section because the FAT driver has static state and the floppy needs IRQ 6; <code>vfs_scan_busy</code>/<code>format_busy</code> make it sleep first while a mount scan or a format owns the disks. A format releases the BKL for its raw-sector loop, and only disk-backed mounts wait for it, so <code>/ramfs</code> and <code>/share</code> stay usable.  A rescan (the <code>bootscan</code> thread or the <code>storage</code> command) re-adds <code>/ramfs</code>/<code>/share</code> first and is skipped by their operations too (<code>vfs_mount_is_ram</code>); it runs preemptible, and the floppy/ATA/AHCI/SDHCI wait loops call <code>vfs_scan_poll</code>/<code>vfs_scan_relax</code>, which sleep (dropping the BKL) once a wait passes 2 ms and only for the scan's owning process, so <code>storage</code> no longer freezes the WM.  The floppy probe is skipped for a drive whose CMOS type (register <code>0x10</code>) is 0.</li>
 		</ul>
 		</ul>
 		<p>
 		<p>
 			<span class="file">fatdetect.c</span> and <span class="file">fatformat.c</span>
 			<span class="file">fatdetect.c</span> and <span class="file">fatformat.c</span>
@@ -189,6 +189,11 @@
 			one-sector <code>NBROOT1</code> header. At boot the path loads the
 			one-sector <code>NBROOT1</code> header. At boot the path loads the
 			pack into RAM and <span class="file">ramfs.c</span> inflates it into a
 			pack into RAM and <span class="file">ramfs.c</span> inflates it into a
 			private page-allocated copy mounted read-only at <code>/ramfs</code>.
 			private page-allocated copy mounted read-only at <code>/ramfs</code>.
+			The whole-buffer helpers in <span class="file">inflate.c</span>
+			(<code>inflate_gzip_mem</code> and friends, also used by the image
+			decoders) decode with 10-bit Huffman lookup tables straight into the
+			output buffer and checksum once at the end, about five times faster
+			than the bit-at-a-time streaming decoder that HTTP bodies still use.
 		</p>
 		</p>
 
 
 		<figure class="figure">
 		<figure class="figure">

+ 1 - 1
docs/index.html

@@ -222,7 +222,7 @@
 			</a>
 			</a>
 			<a class="card" href="drivers.html#wifi">
 			<a class="card" href="drivers.html#wifi">
 				<h3>WiFi</h3>
 				<h3>WiFi</h3>
-				<p>Intel 7265D/3165 on the Chromebook: PCIe transport, firmware from the command pack, LMAC scan, 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>
 				<span class="card-tag">drivers.html#wifi</span>
 			</a>
 			</a>
 			<a class="card" href="network.html#chat">
 			<a class="card" href="network.html#chat">

+ 7 - 5
docs/network.html

@@ -170,7 +170,9 @@
 		</p>
 		</p>
 		<p>
 		<p>
 			<span class="file">wifi.c</span> is the built-in WiFi of the
 			<span class="file">wifi.c</span> is the built-in WiFi of the
-			Chromebook (Intel 7265D/3165, see the
+			Chromebook (Intel 7265D/3165) and of the ThinkPad X201 (Intel
+			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
 			<a href="drivers.html#wifi">driver page</a>). It is last in the
 			registry, so the boot probe only makes it active when nothing
 			registry, so the boot probe only makes it active when nothing
 			wired is present (a later <code>wifi connect</code> can still select
 			wired is present (a later <code>wifi connect</code> can still select
@@ -194,7 +196,7 @@
 			A successful <code>wifi connect</code> (from the CLI or the menu) also saves
 			A successful <code>wifi connect</code> (from the CLI or the menu) also saves
 			the SSID and passphrase to <code>/data/WIFI.CFG</code> on the
 			the SSID and passphrase to <code>/data/WIFI.CFG</code> on the
 			persistent <code>NEBOSDATA</code> partition. After the boot DHCP
 			persistent <code>NEBOSDATA</code> partition. After the boot DHCP
-			attempt <code>kernel_main</code> spawns
+			attempt and the storage scan, the <code>bootscan</code> thread spawns
 			<code>wifi autoconnect</code> when that file exists. It samples the
 			<code>wifi autoconnect</code> when that file exists. It samples the
 			active NIC's <code>link_up()</code> state: a wired cable that is
 			active NIC's <code>link_up()</code> state: a wired cable that is
 			physically up keeps the Ethernet path, while no wired link lets
 			physically up keeps the Ethernet path, while no wired link lets
@@ -209,12 +211,12 @@
 					<tr><th>Driver</th><th>Hardware</th><th>Transport</th><th>Notes</th></tr>
 					<tr><th>Driver</th><th>Hardware</th><th>Transport</th><th>Notes</th></tr>
 				</thead>
 				</thead>
 				<tbody>
 				<tbody>
-					<tr><td><span class="file">e1000e.c</span></td><td>Intel 82540EM, 82577LM, 82578, 82579</td><td>PCI MMIO</td><td>PCI INTx disabled before MMIO; PCH D0; skips the global MAC reset while the link is up; extended RX descriptors only when <code>RFCTL.EXTEN</code> reads back set (the 82540EM has no RFCTL and writes legacy descriptors).</td></tr>
+					<tr><td><span class="file">e1000e.c</span></td><td>Intel 82540EM, 82577LM, 82578, 82579</td><td>PCI MMIO</td><td>PCI INTx disabled before MMIO; PCH D0; skips the global MAC reset while the link is up; extended RX descriptors only when <code>RFCTL.EXTEN</code> reads back set; the 82540EM (<code>0x100E</code>) is pinned to legacy descriptors, because it has no RFCTL but QEMU stores the register, so EXTEN reads back set.</td></tr>
 					<tr><td><span class="file">r8168.c</span></td><td>RTL8111/8168</td><td>PCIe MMIO</td><td>C+ mode, 64-entry RX ring, polled.</td></tr>
 					<tr><td><span class="file">r8168.c</span></td><td>RTL8111/8168</td><td>PCIe MMIO</td><td>C+ mode, 64-entry RX ring, polled.</td></tr>
 					<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">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">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">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)</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>
 				</tbody>
 			</table>
 			</table>
 		</div>
 		</div>
@@ -474,7 +476,7 @@
 		<ul class="filelist">
 		<ul class="filelist">
 			<li><code>netdev.c</code>, <code>netdev.h</code> — registry</li>
 			<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>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_mvm.c</code>, <code>iwl_trans.c</code> — Intel 7265D/3165 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>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>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>
 			<li><code>net.c</code>, <code>net.h</code> — ARP cache/ICMP/UDP/IP</li>

A diferenza do arquivo foi suprimida porque é demasiado grande
+ 0 - 2
docs/userland.html


+ 17 - 7
e1000e.c

@@ -226,7 +226,7 @@ static unsigned char *rx_bufs;
 static unsigned char *tx_bufs;
 static unsigned char *tx_bufs;
 static unsigned int rx_cur;
 static unsigned int rx_cur;
 /* 1 when RFCTL.EXTEN stuck (PCH parts): status/error at offset 8, length
 /* 1 when RFCTL.EXTEN stuck (PCH parts): status/error at offset 8, length
- * at 12.  0 for MACs without RFCTL (QEMU's 82540EM reads it as zero):
+ * at 12.  0 for MACs without RFCTL (the 82540EM, whatever RFCTL reads):
  * legacy write-back, length at 8, status at 12, errors at 13. */
  * legacy write-back, length at 8, status at 12, errors at 13. */
 static int rx_ext;
 static int rx_ext;
 static unsigned int tx_cur;
 static unsigned int tx_cur;
@@ -1329,12 +1329,17 @@ static int hw_reset(void)
 	 * PCIe master first; clearing RCTL while DMA is active can wedge the
 	 * PCIe master first; clearing RCTL while DMA is active can wedge the
 	 * receive packet buffer on these parts. */
 	 * receive packet buffer on these parts. */
 	ctrl = r32(REG_CTRL);
 	ctrl = r32(REG_CTRL);
-	w32(REG_CTRL, ctrl | CTRL_GIO_MASTER_DISABLE);
-	t0 = get_ticks_ms();
-	while ((r32(REG_STATUS) & STATUS_GIO_MASTER_ENABLE) &&
-	       !timed_out(t0, 1000))
-		;
-	delay_ms(5);
+	/* The GIO master handshake only exists on PCIe parts.  The 82540EM
+	 * is conventional PCI: QEMU keeps STATUS bit 19 set forever, so the
+	 * wait used to burn its whole 1 s timeout on every boot. */
+	if (device_id != 0x100E) {
+		w32(REG_CTRL, ctrl | CTRL_GIO_MASTER_DISABLE);
+		t0 = get_ticks_ms();
+		while ((r32(REG_STATUS) & STATUS_GIO_MASTER_ENABLE) &&
+		       !timed_out(t0, 1000))
+			;
+		delay_ms(5);
+	}
 
 
 	hw_stop();
 	hw_stop();
 	delay_ms(5);
 	delay_ms(5);
@@ -1499,6 +1504,11 @@ static void rx_desc_config(void)
 	 * extended layout reads the frame length as status, so DD looks clear
 	 * extended layout reads the frame length as status, so DD looks clear
 	 * for every even-length frame and the ring stalls full. */
 	 * for every even-length frame and the ring stalls full. */
 	rx_ext = (r32(REG_RFCTL) & RFCTL_EXTEN) ? 1 : 0;
 	rx_ext = (r32(REG_RFCTL) & RFCTL_EXTEN) ? 1 : 0;
+	/* QEMU's e1000 stores RFCTL like any register, so EXTEN reads back
+	 * set, yet the emulated 82540EM still writes legacy descriptors.
+	 * The 82540EM never has extended RX descriptors. */
+	if (device_id == 0x100E)
+		rx_ext = 0;
 
 
 	/* No checksum offload: the stack verifies checksums, and the driver
 	/* No checksum offload: the stack verifies checksums, and the driver
 	 * rejects any descriptor whose error byte is non-zero.  With RXCSUM
 	 * rejects any descriptor whose error byte is non-zero.  With RXCSUM

+ 2 - 3
ehci.c

@@ -17,6 +17,7 @@
  * Descriptors (frame list, QHs, qTDs, bounce buffers) come from the page
  * Descriptors (frame list, QHs, qTDs, bounce buffers) come from the page
  * allocator, so the driver does not grow the kernel BSS. */
  * allocator, so the driver does not grow the kernel BSS. */
 
 
+#include "usb.h"
 #include "ehci.h"
 #include "ehci.h"
 #include "io.h"
 #include "io.h"
 #include "mm.h"
 #include "mm.h"
@@ -290,9 +291,7 @@ static unsigned int port_reg(struct ehci_ctrl *c, unsigned int port)
 
 
 static void delay_ms(unsigned int ms)
 static void delay_ms(unsigned int ms)
 {
 {
-	unsigned int t0 = get_ticks_ms();
-	while (get_ticks_ms() - t0 < ms)
-		;
+	usb_delay_ms(ms);    /* sleeps inside a sleepable scan */
 }
 }
 
 
 static void zero_region(unsigned int addr, unsigned int len)
 static void zero_region(unsigned int addr, unsigned int len)

+ 367 - 50
inflate.c

@@ -11,7 +11,7 @@
 #define MAXDCODES 30
 #define MAXDCODES 30
 #define MAXCODES  (MAXLCODES + MAXDCODES)
 #define MAXCODES  (MAXLCODES + MAXDCODES)
 #define FIXLCODES 288
 #define FIXLCODES 288
-#define FASTBITS  9
+#define FASTBITS  10
 
 
 static unsigned int crc_table[256];
 static unsigned int crc_table[256];
 static int crc_table_ready;
 static int crc_table_ready;
@@ -580,82 +580,399 @@ int inflate_gzip(struct inflate *z, unsigned int *out_len)
 	return 0;
 	return 0;
 }
 }
 
 
-/* ---- whole-buffer helpers ------------------------------------------- */
+/* ---- whole-buffer fast path ----------------------------------------
+ *
+ * The streaming decoder above reads one bit per Huffman step and stages
+ * every byte in the 32 KB ring.  When the whole input and output are in
+ * memory (the boot rootfs, PNG/HTTP bodies decoded into one buffer) that
+ * is needlessly slow: here the output buffer itself is the LZ77 history,
+ * the bit buffer is refilled a byte at a time up to 32 bits, and codes of
+ * up to FASTBITS are resolved with one table lookup.  Longer codes fall
+ * back to the canonical walk on the bits already in the buffer.  Only the
+ * checksum the container needs is computed, once, over the finished
+ * output.  The tables live in the caller's struct-inflate workspace. */
+
+#define FAST_SIZE (1u << FASTBITS)
+
+struct fhuff {
+	struct inflate_huff h;
+	unsigned short fast[FAST_SIZE];     /* (sym << 4) | len, 0 = slow */
+};
+
+struct fz {
+	const unsigned char *in, *in_end;
+	unsigned int bitbuf;
+	int bitcnt;
+	unsigned int over;                  /* zero bytes padded past the end */
+	unsigned char *out, *out_start, *out_end;
+};
 
 
-struct mem_sink {
-	unsigned char *dst;
-	unsigned int cap, pos;
+struct fz_ws {
+	struct fhuff lit, dist;
 };
 };
 
 
-static int mem_write(void *ctx, const unsigned char *data, unsigned int len)
+static void fz_refill(struct fz *f)
+{
+	while (f->bitcnt <= 24) {
+		unsigned int b = 0;
+
+		if (f->in < f->in_end)
+			b = *f->in++;
+		else
+			f->over++;
+		f->bitbuf |= b << f->bitcnt;
+		f->bitcnt += 8;
+	}
+}
+
+static int fz_bits(struct fz *f, int n)
+{
+	unsigned int v;
+
+	if (f->bitcnt < n)
+		fz_refill(f);
+	v = f->bitbuf & ((1u << n) - 1u);
+	f->bitbuf >>= n;
+	f->bitcnt -= n;
+	return (int)v;
+}
+
+static int fhuff_build(struct fhuff *fh, const unsigned char *lengths, int n)
 {
 {
-	struct mem_sink *m = (struct mem_sink *)ctx;
+	unsigned int next[MAXBITS + 2];
+	int r, len, sym, i;
 
 
-	if (len > m->cap - m->pos)
+	r = huff_build(&fh->h, lengths, n);
+	if (r < 0)
 		return -1;
 		return -1;
+	for (i = 0; i < (int)FAST_SIZE; i++)
+		fh->fast[i] = 0;
 	{
 	{
-		unsigned int i;
+		/* next[len] = first canonical code of length len. */
+		unsigned int c = 0;
+
+		next[1] = 0;
+		for (len = 1; len < MAXBITS; len++) {
+			c = (c + fh->h.count[len]) << 1;
+			next[len + 1] = c;
+		}
+	}
+	for (sym = 0; sym < n; sym++) {
+		unsigned int c, rev = 0;
+		int l = lengths[sym], k;
+
+		if (!l)
+			continue;
+		c = next[l]++;
+		if (l > FASTBITS)
+			continue;
+		for (k = 0; k < l; k++)
+			rev |= ((c >> k) & 1u) << (l - 1 - k);
+		for (; rev < FAST_SIZE; rev += 1u << l)
+			fh->fast[rev] = (unsigned short)((sym << 4) | l);
+	}
+	return r;
+}
+
+static int fz_decode(struct fz *f, const struct fhuff *fh)
+{
+	unsigned int e;
+	int len, code = 0, first = 0, index = 0;
+
+	if (f->bitcnt < MAXBITS)
+		fz_refill(f);
+	e = fh->fast[f->bitbuf & (FAST_SIZE - 1u)];
+	if (e) {
+		f->bitbuf >>= e & 15u;
+		f->bitcnt -= (int)(e & 15u);
+		return (int)(e >> 4);
+	}
+	for (len = 1; len <= MAXBITS; len++) {
+		int count;
+
+		code |= (int)(f->bitbuf & 1u);
+		f->bitbuf >>= 1;
+		f->bitcnt--;
+		count = fh->h.count[len];
+		if (code - count < first)
+			return fh->h.symbol[index + (code - first)];
+		index += count;
+		first += count;
+		first <<= 1;
+		code <<= 1;
+	}
+	return -1;
+}
+
+static int fz_codes(struct fz *f, const struct fhuff *lit,
+		    const struct fhuff *dist)
+{
+	for (;;) {
+		int sym = fz_decode(f, lit);
+		unsigned int len, d;
+		unsigned char *out, *src;
+
+		if (sym < 256) {
+			if (sym < 0 || f->out >= f->out_end)
+				return -1;
+			*f->out++ = (unsigned char)sym;
+			continue;
+		}
+		if (sym == 256)
+			return f->over > 4 ? -1 : 0;
+		sym -= 257;
+		if (sym >= 29)
+			return -1;
+		len = len_base[sym];
+		if (len_ext[sym])
+			len += (unsigned int)fz_bits(f, len_ext[sym]);
+		sym = fz_decode(f, dist);
+		if (sym < 0 || sym >= 30)
+			return -1;
+		d = dist_base[sym];
+		if (dist_ext[sym])
+			d += (unsigned int)fz_bits(f, dist_ext[sym]);
+		out = f->out;
+		if (d > (unsigned int)(out - f->out_start) ||
+		    len > (unsigned int)(f->out_end - out))
+			return -1;
+		src = out - d;
+		if (d >= 4) {
+			while (len >= 4) {
+				out[0] = src[0]; out[1] = src[1];
+				out[2] = src[2]; out[3] = src[3];
+				out += 4; src += 4; len -= 4;
+			}
+		}
+		while (len--)
+			*out++ = *src++;
+		f->out = out;
+	}
+}
 
 
-		for (i = 0; i < len; i++)
-			m->dst[m->pos + i] = data[i];
+static int fz_blocks(struct fz *f, struct fz_ws *ws)
+{
+	static const unsigned char order[19] = {
+		16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
+	};
+	unsigned char lengths[FIXLCODES + MAXDCODES];
+	int last, type, i;
+
+	do {
+		last = fz_bits(f, 1);
+		type = fz_bits(f, 2);
+		if (type == 0) {
+			unsigned int len, nlen;
+
+			fz_bits(f, f->bitcnt & 7);
+			len = (unsigned int)fz_bits(f, 16);
+			nlen = (unsigned int)fz_bits(f, 16);
+			if ((len ^ nlen) != 0xFFFFu ||
+			    len > (unsigned int)(f->out_end - f->out))
+				return -1;
+			/* Return the whole buffered bytes to the input and
+			 * copy the stored data straight from it. */
+			{
+				unsigned int back = (unsigned int)f->bitcnt >> 3;
+
+				if (back < f->over)
+					return -1;
+				f->in -= back - f->over;
+				f->bitbuf = 0;
+				f->bitcnt = 0;
+				f->over = 0;
+			}
+			if (len > (unsigned int)(f->in_end - f->in))
+				return -1;
+			while (len--)
+				*f->out++ = *f->in++;
+		} else if (type == 1) {
+			for (i = 0; i < 144; i++)
+				lengths[i] = 8;
+			for (; i < 256; i++)
+				lengths[i] = 9;
+			for (; i < 280; i++)
+				lengths[i] = 7;
+			for (; i < 288; i++)
+				lengths[i] = 8;
+			for (i = 0; i < 30; i++)
+				lengths[288 + i] = 5;
+			if (fhuff_build(&ws->lit, lengths, 288) < 0 ||
+			    fhuff_build(&ws->dist, lengths + 288, 30) < 0 ||
+			    fz_codes(f, &ws->lit, &ws->dist))
+				return -1;
+		} else if (type == 2) {
+			int hlit = fz_bits(f, 5) + 257;
+			int hdist = fz_bits(f, 5) + 1;
+			int hclen = fz_bits(f, 4) + 4;
+			int total = hlit + hdist;
+
+			if (hlit > MAXLCODES || hdist > MAXDCODES)
+				return -1;
+			for (i = 0; i < 19; i++)
+				lengths[i] = 0;
+			for (i = 0; i < hclen; i++)
+				lengths[order[i]] = (unsigned char)fz_bits(f, 3);
+			if (fhuff_build(&ws->lit, lengths, 19) < 0)
+				return -1;
+			for (i = 0; i < total; ) {
+				int sym = fz_decode(f, &ws->lit), rep, val;
+
+				if (sym < 0)
+					return -1;
+				if (sym < 16) {
+					lengths[i++] = (unsigned char)sym;
+					continue;
+				}
+				if (sym == 16) {
+					if (i == 0)
+						return -1;
+					rep = 3 + fz_bits(f, 2);
+					val = lengths[i - 1];
+				} else if (sym == 17) {
+					rep = 3 + fz_bits(f, 3);
+					val = 0;
+				} else {
+					rep = 11 + fz_bits(f, 7);
+					val = 0;
+				}
+				if (i + rep > total)
+					return -1;
+				while (rep--)
+					lengths[i++] = (unsigned char)val;
+			}
+			if (fhuff_build(&ws->lit, lengths, hlit) < 0 ||
+			    fhuff_build(&ws->dist, lengths + hlit, hdist) < 0 ||
+			    fz_codes(f, &ws->lit, &ws->dist))
+				return -1;
+		} else {
+			return -1;
+		}
+		if (f->over > 4)
+			return -1;
+	} while (!last);
+
+	/* Byte-align and hand the whole bytes still buffered back to the
+	 * input, so the container trailer is read from memory. */
+	fz_bits(f, f->bitcnt & 7);
+	{
+		unsigned int back = (unsigned int)f->bitcnt >> 3;
+
+		if (back < f->over)
+			return -1;          /* consumed padding: truncated */
+		f->in -= back - f->over;
+		f->bitbuf = 0;
+		f->bitcnt = 0;
+		f->over = 0;
 	}
 	}
-	m->pos += len;
 	return 0;
 	return 0;
 }
 }
 
 
-static int mem_inflate(const void *src, unsigned int len, void *dst,
-		       unsigned int cap, unsigned int *out_len, void *ws,
-		       int gzip)
-{
-	struct inflate *z = (struct inflate *)ws;
-	struct inflate_src s;
-	struct inflate_sink sink;
-	struct mem_sink m;
-
-	s.p = (const unsigned char *)src;
-	s.len = len;
-	s.more = 0;
-	s.ctx = 0;
-	m.dst = (unsigned char *)dst;
-	m.cap = cap;
-	m.pos = 0;
-	sink.write = mem_write;
-	sink.ctx = &m;
-	inflate_init(z, &s, &sink);
-	if (gzip ? inflate_gzip(z, out_len) : inflate_zlib(z, out_len))
+/* container: 0 raw, 1 zlib, 2 gzip */
+static int fz_inflate(const void *src, unsigned int len, void *dst,
+		      unsigned int cap, unsigned int *out_len, void *ws,
+		      int container)
+{
+	struct fz f;
+	const unsigned char *p = (const unsigned char *)src;
+	const unsigned char *end = p + len;
+	unsigned int n;
+
+	if (container == 2) {
+		unsigned int flg;
+
+		if (len < 18 || p[0] != 0x1F || p[1] != 0x8B || p[2] != 8)
+			return -1;
+		flg = p[3];
+		p += 10;
+		if (flg & 0x04) {
+			if (end - p < 2)
+				return -1;
+			n = p[0] | ((unsigned int)p[1] << 8);
+			p += 2;
+			if ((unsigned int)(end - p) < n)
+				return -1;
+			p += n;
+		}
+		if (flg & 0x08) {
+			while (p < end && *p)
+				p++;
+			p++;
+		}
+		if (flg & 0x10) {
+			while (p < end && *p)
+				p++;
+			p++;
+		}
+		if (flg & 0x02)
+			p += 2;
+		if (p >= end)
+			return -1;
+	} else if (container == 1) {
+		if (len < 6 || (p[0] & 0x0F) != 8 || (p[0] >> 4) > 7 ||
+		    (((unsigned int)p[0] << 8) | p[1]) % 31 != 0 ||
+		    (p[1] & 0x20))
+			return -1;
+		p += 2;
+	}
+
+	f.in = p;
+	f.in_end = end;
+	f.bitbuf = 0;
+	f.bitcnt = 0;
+	f.over = 0;
+	f.out = f.out_start = (unsigned char *)dst;
+	f.out_end = f.out_start + cap;
+	if (fz_blocks(&f, (struct fz_ws *)ws))
 		return -1;
 		return -1;
+	n = (unsigned int)(f.out - f.out_start);
+
+	if (container == 2) {
+		unsigned int crc, size;
+
+		if (f.in_end - f.in < 8)
+			return -1;
+		crc_table_init();
+		crc = crc_update(0xFFFFFFFFu, f.out_start, n) ^ 0xFFFFFFFFu;
+		if (crc != (f.in[0] | ((unsigned int)f.in[1] << 8) |
+			    ((unsigned int)f.in[2] << 16) |
+			    ((unsigned int)f.in[3] << 24)))
+			return -1;
+		size = f.in[4] | ((unsigned int)f.in[5] << 8) |
+		       ((unsigned int)f.in[6] << 16) |
+		       ((unsigned int)f.in[7] << 24);
+		if (size != n)
+			return -1;
+	} else if (container == 1) {
+		unsigned int a = 1, b = 0;
+
+		if (f.in_end - f.in < 4)
+			return -1;
+		adler_update(&a, &b, f.out_start, n);
+		if (((b << 16) | a) != (((unsigned int)f.in[0] << 24) |
+					((unsigned int)f.in[1] << 16) |
+					((unsigned int)f.in[2] << 8) | f.in[3]))
+			return -1;
+	}
+	if (out_len)
+		*out_len = n;
 	return 0;
 	return 0;
 }
 }
 
 
 int inflate_zlib_mem(const void *src, unsigned int len, void *dst,
 int inflate_zlib_mem(const void *src, unsigned int len, void *dst,
 		     unsigned int cap, unsigned int *out_len, void *ws)
 		     unsigned int cap, unsigned int *out_len, void *ws)
 {
 {
-	return mem_inflate(src, len, dst, cap, out_len, ws, 0);
+	return fz_inflate(src, len, dst, cap, out_len, ws, 1);
 }
 }
 
 
 int inflate_gzip_mem(const void *src, unsigned int len, void *dst,
 int inflate_gzip_mem(const void *src, unsigned int len, void *dst,
 		     unsigned int cap, unsigned int *out_len, void *ws)
 		     unsigned int cap, unsigned int *out_len, void *ws)
 {
 {
-	return mem_inflate(src, len, dst, cap, out_len, ws, 1);
+	return fz_inflate(src, len, dst, cap, out_len, ws, 2);
 }
 }
 
 
 int inflate_raw_mem(const void *src, unsigned int len, void *dst,
 int inflate_raw_mem(const void *src, unsigned int len, void *dst,
 		    unsigned int cap, unsigned int *out_len, void *ws)
 		    unsigned int cap, unsigned int *out_len, void *ws)
 {
 {
-	struct inflate *z = (struct inflate *)ws;
-	struct inflate_src s;
-	struct inflate_sink sink;
-	struct mem_sink m;
-
-	s.p = (const unsigned char *)src;
-	s.len = len;
-	s.more = 0;
-	s.ctx = 0;
-	m.dst = (unsigned char *)dst;
-	m.cap = cap;
-	m.pos = 0;
-	sink.write = mem_write;
-	sink.ctx = &m;
-	inflate_init(z, &s, &sink);
-	return inflate_raw(z, out_len);
+	return fz_inflate(src, len, dst, cap, out_len, ws, 0);
 }
 }

+ 1194 - 0
iwl_dvm.c

@@ -0,0 +1,1194 @@
+/* Intel 6200/6300 firmware layer (iwlwifi DVM): see iwl_dvm.h.
+ *
+ * Follows Linux iwldvm for iwlwifi-6000-4.ucode (API 4):
+ *
+ *   EEPROM/OTP is read by the host (MAC address, radio config, crystal
+ *     calibration), NIC config writes the radio SKU bits before the rings.
+ *   INIT image: ALIVE -> AC queues -> WiMAX coex off -> crystal calibration
+ *     -> CALIBRATION_CFG_CMD; CALIBRATION_RES_NOTIFICATIONs are stored by op
+ *     code until CALIBRATION_COMPLETE_NOTIFICATION, then the device stops.
+ *   runtime image: ALIVE -> AC queues -> coex -> stored calibration results
+ *     (REPLY_PHY_CALIBRATION_CMD) -> DC calibration -> TX antennas ->
+ *     unassociated RXON (+ broadcast station and its LQ) -> TX power ->
+ *     CAM power table.
+ *
+ * A full RXON commit always clears the association first, which wipes the
+ * firmware's station table, so the broadcast station (and the AP station
+ * once added) are re-added after it, as iwl_restore_stations() does.
+ * Legacy rates only (no HT, no rate scaling): an LQ table holds one fixed
+ * rate.  RX frames arrive as REPLY_RX_PHY_CMD + REPLY_RX_MPDU_CMD, the same
+ * as the 7265D's legacy path, and go to wifi.c unchanged. */
+
+#include "iwl_dvm.h"
+#include "iwl_mvm.h"       /* wifi_rx_frame/wifi_scan_done, IWL_RX_* */
+#include "iwl_trans.h"
+#include "mm.h"
+#include "serial.h"
+
+/* ---- command ids (dvm/commands.h) --------------------------------------- */
+#define REPLY_ALIVE                  0x01
+#define REPLY_ERROR                  0x02
+#define REPLY_RXON                   0x10
+#define REPLY_QOS_PARAM              0x13
+#define REPLY_RXON_TIMING            0x14
+#define REPLY_ADD_STA                0x18
+#define REPLY_REMOVE_STA             0x19
+#define REPLY_TX                     0x1c
+#define REPLY_TX_LINK_QUALITY_CMD    0x4e
+#define COEX_PRIORITY_TABLE_CMD      0x5a
+#define CALIBRATION_CFG_CMD          0x65
+#define CALIBRATION_RES_NOTIFICATION 0x66
+#define CALIBRATION_COMPLETE_NOTIF   0x67
+#define POWER_TABLE_CMD              0x77
+#define REPLY_SCAN_CMD               0x80
+#define SCAN_COMPLETE_NOTIFICATION   0x84
+#define REPLY_TX_POWER_DBM_CMD       0x95
+#define TX_ANT_CONFIGURATION_CMD     0x98
+#define REPLY_PHY_CALIBRATION_CMD    0xb0
+#define REPLY_RX_PHY_CMD             0xc0
+#define REPLY_RX_MPDU_CMD            0xc1
+
+#define IWL_AP_ID            0
+#define IWLAGN_BROADCAST_ID  15
+
+#define RXON_FLG_BAND_24G    (1u << 0)
+#define RXON_FLG_AUTO_DETECT (1u << 2)
+#define RXON_FLG_SHORT_SLOT  (1u << 4)
+#define RXON_FLG_TSF2HOST    (1u << 15)
+#define RXON_FILTER_ACCEPT_GRP (1u << 2)
+#define RXON_FILTER_ASSOC    (1u << 5)
+#define RXON_DEV_TYPE_ESS    3
+
+#define STA_CONTROL_MODIFY   0x01
+#define STA_MODIFY_KEY_MASK  0x01
+#define STA_KEY_FLG_CCMP     0x0002
+#define STA_KEY_FLG_MAP_KEY  0x0008
+#define STA_KEY_FLG_INVALID  0x0800
+#define STA_KEY_MULTICAST    0x4000
+
+#define RATE_CCK       (1u << 9)
+#define RATE_ANT_POS   14
+#define PLCP_1M        10
+#define PLCP_6M        13
+#define PLCP_12M       5
+
+#define TX_FLG_ACK       (1u << 3)
+#define TX_FLG_SEQ_CTL   (1u << 13)
+#define TX_SEC_CCM       0x02
+
+#define IWL_CALIB_INIT_CFG_ALL  0x5fu   /* RX_BB|DC|LO|TX_IQ|RX_IQ|CRYSTAL */
+#define IWL_CALIB_RT_CFG_ALL    0x5dfu  /* + TEMPERATURE|PAPD|TX_PWR */
+#define IWL_CALIB_CFG_DC        (1u << 1)
+#define IWL_CALIB_SEND_COMPLETE (1u << 0)
+#define IWL_PHY_CALIBRATE_CRYSTAL_FRQ_CMD 15
+
+#define IWLAGN_RSSI_OFFSET 44
+
+/* ---- wire structures (packed, little endian) --------------------------- */
+#define PACKED __attribute__((packed))
+
+struct alive_resp {                  /* struct iwl_alive_resp */
+	unsigned char ucode_minor, ucode_major;
+	unsigned short res1;
+	unsigned char sw_rev[8];
+	unsigned char ver_type, ver_subtype;
+	unsigned short res2;
+	unsigned int log_event_table_ptr, error_event_table_ptr;
+	unsigned int timestamp, is_valid;
+} PACKED;
+
+struct rxon_cmd {                    /* struct iwl_rxon_cmd */
+	unsigned char node_addr[6];
+	unsigned short res1;
+	unsigned char bssid_addr[6];
+	unsigned short res2;
+	unsigned char wlap_bssid_addr[6];
+	unsigned short res3;
+	unsigned char dev_type, air_propagation;
+	unsigned short rx_chain;
+	unsigned char ofdm_basic_rates, cck_basic_rates;
+	unsigned short assoc_id;
+	unsigned int flags, filter_flags;
+	unsigned short channel;
+	unsigned char ht_single, ht_dual, ht_triple, res5;
+	unsigned short acquisition_data, res6;
+} PACKED;
+
+struct rxon_timing {                 /* struct iwl_rxon_time_cmd */
+	unsigned int timestamp_lo, timestamp_hi;
+	unsigned short beacon_interval, atim_window;
+	unsigned int beacon_init_val;
+	unsigned short listen_interval;
+	unsigned char dtim_period, delta_cp_bss_tbtts;
+} PACKED;
+
+struct keyinfo {                     /* struct iwl_keyinfo */
+	unsigned short key_flags;
+	unsigned char tkip_rx_tsc_byte2, res1;
+	unsigned short tkip_rx_ttak[5];
+	unsigned char key_offset, res2;
+	unsigned char key[16];
+	unsigned int tx_seq[2], mic_rx[2], mic_tx[2];
+} PACKED;
+
+struct addsta_cmd {                  /* struct iwl_addsta_cmd */
+	unsigned char mode, res[3];
+	unsigned char addr[6];
+	unsigned short res1;
+	unsigned char sta_id, modify_mask;
+	unsigned short res2;
+	struct keyinfo key;
+	unsigned int station_flags, station_flags_msk;
+	unsigned short tid_disable_tx, legacy_res;
+	unsigned char add_ba_tid, remove_ba_tid;
+	unsigned short add_ba_ssn, sleep_tx_count, res3;
+} PACKED;
+
+struct lq_cmd {                      /* struct iwl_link_quality_cmd */
+	unsigned char sta_id, res1;
+	unsigned short control;
+	unsigned char flags, mimo_delimiter, single_ant, dual_ant;
+	unsigned char start_rate_index[4];
+	unsigned short agg_time_limit;
+	unsigned char agg_dis_start_th, agg_frame_cnt_limit;
+	unsigned int agg_res;
+	unsigned int rs_table[16];
+	unsigned int res2;
+} PACKED;
+
+struct tx_cmd {                      /* struct iwl_tx_cmd */
+	unsigned short len, next_frame_len;
+	unsigned int tx_flags;
+	unsigned char try_cnt, btkill_cnt;
+	unsigned short scratch_res;
+	unsigned int rate_n_flags;
+	unsigned char sta_id, sec_ctl, initial_rate_index, res;
+	unsigned char key[16];
+	unsigned short next_frame_flags, res2;
+	unsigned int life_time, dram_lsb_ptr;
+	unsigned char dram_msb_ptr, rts_retry_limit, data_retry_limit, tid_tspec;
+	unsigned short pm_frame_timeout, driver_txop;
+} PACKED;
+
+struct scan_cmd {                    /* struct iwl_scan_cmd (fixed part) */
+	unsigned short len;
+	unsigned char scan_flags, channel_count;
+	unsigned short quiet_time, quiet_plcp_th, good_crc_th, rx_chain;
+	unsigned int max_out_time, suspend_time, flags, filter_flags;
+	struct tx_cmd tx;
+	struct { unsigned char id, len, ssid[32]; } direct_scan[20];
+} PACKED;
+
+struct scan_chan {                   /* struct iwl_scan_channel */
+	unsigned int type;
+	unsigned short channel;
+	unsigned char tx_gain, dsp_atten;
+	unsigned short active_dwell, passive_dwell;
+} PACKED;
+
+struct calib_cfg_elmnt {
+	unsigned int is_enable, start, send_res, apply_res, res;
+} PACKED;
+
+struct calib_cfg_cmd {               /* struct iwl_calib_cfg_cmd */
+	struct calib_cfg_elmnt ucd_once, ucd_perd;
+	unsigned int ucd_flags;
+	struct calib_cfg_elmnt drv_once, drv_perd;
+	unsigned int drv_flags;
+	unsigned int res1;
+} PACKED;
+
+typedef char dchk1[sizeof(struct rxon_cmd) == 50 ? 1 : -1];
+typedef char dchk2[sizeof(struct addsta_cmd) == 92 ? 1 : -1];
+typedef char dchk3[sizeof(struct lq_cmd) == 88 ? 1 : -1];
+typedef char dchk4[sizeof(struct tx_cmd) == 56 ? 1 : -1];
+typedef char dchk5[sizeof(struct scan_cmd) == 764 ? 1 : -1];
+typedef char dchk6[sizeof(struct alive_resp) == 32 ? 1 : -1];
+typedef char dchk7[sizeof(struct rxon_timing) == 20 ? 1 : -1];
+
+/* ---- state ------------------------------------------------------------- */
+#define EEPROM_SIZE   2048            /* OTP_LOW_IMAGE_SIZE_2K */
+#define CALIB_PAGES   4
+
+static struct {
+	int up;
+	const char *state;
+	unsigned char mac[6];
+	unsigned char bssid[6];
+	unsigned int chan;
+	unsigned int valid_tx_ant, valid_rx_ant, first_tx_ant;
+	unsigned int radio_cfg;
+	unsigned char xtal[2];
+	int alive, alive_ok, calib_done;
+	unsigned int err_table;
+	unsigned char *calib;          /* [op u8][pad][len u16][data...] */
+	unsigned int calib_len;
+	unsigned char *eeprom;
+	unsigned char *scratch;        /* 8 KB command scratch */
+	struct rxon_cmd rxon;
+	int ap_sta;
+	int assoc;
+	int scanning, scan_band_5, scan_next5;
+	/* scan lists from the EEPROM regulatory tables: channel | 0x100 active */
+	unsigned short ch24[14], ch5[32];
+	unsigned int n24, n5;
+	int last_rssi, last_chan;
+	unsigned char tk[16];
+	int have_tk;
+} D;
+
+static void logs(const char *s)
+{
+	iwl_log(s);
+}
+
+static unsigned int get16(const unsigned char *p)
+{
+	return p[0] | (p[1] << 8);
+}
+
+static unsigned int get32(const unsigned char *p)
+{
+	return p[0] | (p[1] << 8) | (p[2] << 16) | ((unsigned int)p[3] << 24);
+}
+
+static void put16(unsigned char *p, unsigned int v)
+{
+	p[0] = v; p[1] = v >> 8;
+}
+
+static void dump_error_log(void);
+
+static int cmd(unsigned int id, const void *data, unsigned int len)
+{
+	if (iwl_send_cmd(id, data, len, 0, 0) < 0) {
+		iwl_loghex("command failed, id", id);
+		dump_error_log();
+		return -1;
+	}
+	return 0;
+}
+
+/* ---- EEPROM / OTP (iwl-eeprom-read.c) ----------------------------------- */
+#define CSR_HW_IF_CONFIG_REG   0x000
+#define CSR_GP_CNTRL           0x024
+#define CSR_EEPROM_REG         0x02c
+#define CSR_EEPROM_GP          0x030
+#define CSR_OTP_GP_REG         0x034
+#define CSR_GP_DRIVER_REG      0x050
+#define CSR_DBG_LINK_PWR_MGMT  0x250
+#define APMG_PS_CTRL_REG       0x300c
+
+static void set32(unsigned int reg, unsigned int bits)
+{
+	iwl_wr(reg, iwl_rd(reg) | bits);
+}
+
+static int poll32(unsigned int reg, unsigned int bits, unsigned int us)
+{
+	unsigned int t;
+
+	for (t = 0; t < us; t += 5) {
+		if ((iwl_rd(reg) & bits) == bits)
+			return 0;
+		iwl_udelay(5);
+	}
+	return -1;
+}
+
+static int read_eeprom(void)
+{
+	unsigned int gp, otp, addr, i;
+
+	/* Signature: OTP with a good signature (6000 family uses OTP). */
+	gp = iwl_rd(CSR_EEPROM_GP) & 7u;
+	otp = (iwl_rd(CSR_OTP_GP_REG) & 0x00010000u) != 0;
+	if (!(otp ? gp == 1 : (gp == 2 || gp == 4))) {
+		iwl_loghex("bad EEPROM/OTP signature, EEPROM_GP", iwl_rd(CSR_EEPROM_GP));
+		return -1;
+	}
+	/* EEPROM semaphore (CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM). */
+	for (i = 0; i < 1000; i++) {
+		set32(CSR_HW_IF_CONFIG_REG, 0x00200000u);
+		if (poll32(CSR_HW_IF_CONFIG_REG, 0x00200000u, 10) == 0)
+			break;
+	}
+	if (i == 1000) {
+		logs("EEPROM semaphore busy");
+		return -1;
+	}
+	if (otp) {
+		/* iwl_init_otp_access(): MAC clock is up (start_hw), reset the
+		 * OTP block, disable the auto clock gate (shadow RAM). */
+		iwl_wr_prph(APMG_PS_CTRL_REG,
+			    iwl_rd_prph(APMG_PS_CTRL_REG) | 0x04000000u);
+		iwl_udelay(5);
+		iwl_wr_prph(APMG_PS_CTRL_REG,
+			    iwl_rd_prph(APMG_PS_CTRL_REG) & ~0x04000000u);
+		set32(CSR_DBG_LINK_PWR_MGMT, 0x80000000u);
+		iwl_wr(CSR_EEPROM_GP, iwl_rd(CSR_EEPROM_GP) & ~0x00000180u);
+		set32(CSR_OTP_GP_REG, 0x00300000u);   /* ack ECC status bits */
+	}
+	for (addr = 0; addr < EEPROM_SIZE; addr += 2) {
+		unsigned int r;
+
+		iwl_wr(CSR_EEPROM_REG, (addr << 1) & 0xfffcu);
+		if (poll32(CSR_EEPROM_REG, 1u, 5000) < 0) {
+			iwl_loghex("EEPROM read timeout at", addr);
+			goto fail;
+		}
+		r = iwl_rd(CSR_EEPROM_REG);
+		if (otp && (iwl_rd(CSR_OTP_GP_REG) & 0x00200000u)) {
+			set32(CSR_OTP_GP_REG, 0x00200000u);
+			logs("uncorrectable OTP ECC error");
+			goto fail;
+		}
+		D.eeprom[addr] = r >> 16;
+		D.eeprom[addr + 1] = r >> 24;
+	}
+	iwl_wr(CSR_HW_IF_CONFIG_REG, iwl_rd(CSR_HW_IF_CONFIG_REG) & ~0x00200000u);
+	return 0;
+fail:
+	iwl_wr(CSR_HW_IF_CONFIG_REG, iwl_rd(CSR_HW_IF_CONFIG_REG) & ~0x00200000u);
+	return -1;
+}
+
+/* iwl-eeprom-parse.c: direct word offsets, and an indirect address that
+ * adds the calibration section link (EEPROM_LINK_CALIBRATION, 0x67). */
+static const unsigned char *eeprom_at(unsigned int byte_off, int calib)
+{
+	if (calib)
+		byte_off += get16(D.eeprom + 2 * 0x67) << 1;
+	return byte_off + 2 <= EEPROM_SIZE ? D.eeprom + byte_off : 0;
+}
+
+/* EEPROM regulatory channel tables (iwl_init_band_reference(); offsets
+ * EEPROM_REG_BAND_[1345]_CHANNELS behind the regulatory link 0x66): two
+ * bytes per channel, flags (VALID bit 0, ACTIVE bit 3, RADAR bit 4) and the
+ * max power.  Only valid channels are scanned, and probes are only sent
+ * where the table allows active scanning without radar detection. */
+static void parse_channels(void)
+{
+	static const unsigned char b1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
+					    12, 13, 14 };
+	static const unsigned char b3[] = { 34, 36, 38, 40, 42, 44, 46, 48, 52,
+					    56, 60, 64 };
+	static const unsigned char b4[] = { 100, 104, 108, 112, 116, 120, 124,
+					    128, 132, 136, 140 };
+	static const unsigned char b5[] = { 145, 149, 153, 157, 161, 165 };
+	static const struct { const unsigned char *ch; unsigned char n, off; }
+		bands[4] = { { b1, 14, 0x08 }, { b3, 12, 0x42 },
+			     { b4, 11, 0x5c }, { b5, 6, 0x74 } };
+	unsigned int link = get16(D.eeprom + 2 * 0x66) << 1, b, i;
+
+	D.n24 = D.n5 = 0;
+	for (b = 0; b < 4; b++) {
+		for (i = 0; i < bands[b].n; i++) {
+			unsigned int off = link + bands[b].off + 2 * i, f, ch;
+			unsigned short e;
+
+			if (off + 2 > EEPROM_SIZE)
+				break;
+			f = D.eeprom[off];
+			ch = bands[b].ch[i];
+			if (!(f & 0x01) || ch == 34 || ch == 38 || ch == 42 ||
+			    ch == 46 || ch == 145)
+				continue;        /* invalid, or not a 20 MHz channel */
+			e = (unsigned short)(ch | (((f & 0x08) && !(f & 0x10)) ? 0x100 : 0));
+			if (b == 0)
+				D.ch24[D.n24++] = e;
+			else if (D.n5 < 32)
+				D.ch5[D.n5++] = e;
+		}
+	}
+	serial_puts(SERIAL_COM1, "iwl: eeprom channels 2.4 ");
+	serial_putdec(SERIAL_COM1, D.n24);
+	serial_puts(SERIAL_COM1, " 5 ");
+	serial_putdec(SERIAL_COM1, D.n5);
+	serial_puts(SERIAL_COM1, "\n");
+}
+
+static int parse_eeprom(void)
+{
+	const unsigned char *m = eeprom_at(2 * 0x15, 0);
+	const unsigned char *x = eeprom_at(2 * 0x128, 1);
+	unsigned int i;
+
+	for (i = 0; i < 6; i++)
+		D.mac[i] = m[i];
+	if ((D.mac[0] & 1) || !(D.mac[0] | D.mac[1] | D.mac[2] | D.mac[3] |
+				 D.mac[4] | D.mac[5])) {
+		logs("EEPROM MAC address invalid");
+		return -1;
+	}
+	D.radio_cfg = get16(eeprom_at(2 * 0x48, 0));
+	D.valid_tx_ant = (D.radio_cfg >> 8) & 0xf;
+	D.valid_rx_ant = (D.radio_cfg >> 12) & 0xf;
+	/* The 6200's iwl_cfg overrides the EEPROM with ANT_BC. */
+	if (iwl_subfamily() == IWL_SUB_6000I)
+		D.valid_tx_ant = D.valid_rx_ant = 0x6;
+	if (!D.valid_tx_ant)
+		D.valid_tx_ant = 0x6;
+	if (!D.valid_rx_ant)
+		D.valid_rx_ant = D.valid_tx_ant;
+	D.first_tx_ant = D.valid_tx_ant & (0u - D.valid_tx_ant);
+	parse_channels();
+	if (x) {
+		D.xtal[0] = x[0];        /* le16 xtal_calib[0], low byte */
+		D.xtal[1] = x[2];
+	}
+	return 0;
+}
+
+/* iwl_nic_config() + iwl6000_nic_config(), run from the transport between
+ * APM init and the ring setup. */
+static void nic_config(void)
+{
+	unsigned int rev = iwl_hw_rev(), v;
+
+	v = iwl_rd(CSR_HW_IF_CONFIG_REG);
+	v = (v & ~0xfu) | (rev & 0xfu);              /* MAC step/dash */
+	v &= ~(0x0c00u | 0xc000u | 0x3000u);
+	v |= ((D.radio_cfg & 3u) << 10) |            /* PHY type */
+	     (((D.radio_cfg >> 2) & 3u) << 14) |     /* PHY step */
+	     (((D.radio_cfg >> 4) & 3u) << 12);      /* PHY dash */
+	v |= 0x0200u | 0x0100u;                      /* RADIO_SI | MAC_SI */
+	iwl_wr(CSR_HW_IF_CONFIG_REG, v);
+	/* NIC stuck after early PCIe power off W/A. */
+	iwl_wr_prph(APMG_PS_CTRL_REG,
+		    iwl_rd_prph(APMG_PS_CTRL_REG) | 0x00400000u);
+	if (iwl_subfamily() == IWL_SUB_6000I)
+		iwl_wr(CSR_GP_DRIVER_REG, 0x00000002u);  /* 2x2 IPA radio SKU */
+}
+
+/* ---- calibration results ------------------------------------------------ */
+static void calib_store(const unsigned char *d, unsigned int n)
+{
+	unsigned int off, rec = (4 + n + 3) & ~3u;
+
+	if (n < 4 || !D.calib)
+		return;
+	/* replace an earlier result with the same op code */
+	for (off = 0; off < D.calib_len;) {
+		unsigned int len = get16(D.calib + off + 2);
+
+		if (D.calib[off] == d[0] && len == n) {
+			iwl_memcpy(D.calib + off + 4, d, n);
+			return;
+		}
+		off += (4 + len + 3) & ~3u;
+	}
+	if (D.calib_len + rec > CALIB_PAGES * 4096) {
+		logs("calibration store full");
+		return;
+	}
+	D.calib[D.calib_len] = d[0];
+	D.calib[D.calib_len + 1] = 0;
+	put16(D.calib + D.calib_len + 2, n);
+	iwl_memcpy(D.calib + D.calib_len + 4, d, n);
+	D.calib_len += rec;
+}
+
+static int calib_send_all(void)
+{
+	unsigned int off, n = 0;
+
+	for (off = 0; off < D.calib_len;) {
+		unsigned int len = get16(D.calib + off + 2);
+
+		if (cmd(REPLY_PHY_CALIBRATION_CMD, D.calib + off + 4, len) < 0)
+			return -1;
+		off += (4 + len + 3) & ~3u;
+		n++;
+	}
+	iwl_loghex("calibration results sent", n);
+	return 0;
+}
+
+static void store_xtal_calib(void)
+{
+	unsigned char c[8] = { IWL_PHY_CALIBRATE_CRYSTAL_FRQ_CMD, 0, 1, 1,
+			       D.xtal[0], D.xtal[1], 0, 0 };
+
+	calib_store(c, sizeof(c));
+}
+
+/* ---- RX dispatch -------------------------------------------------------- */
+static void rx_phy(const struct iwl_pkt *pkt)
+{
+	const unsigned char *d = pkt->data;
+	unsigned int agc, a, b, c, m;
+
+	if (iwl_pkt_payload_len(pkt) < 60)
+		return;
+	/* struct iwl_rx_phy_res: channel at 18, non_cfg_phy_buf at 20. */
+	D.last_chan = get16(d + 18);
+	agc = (get32(d + 20 + 4) & 0xfe00u) >> 9;
+	a = get32(d + 20 + 8) & 0xff;
+	b = (get32(d + 20 + 8) >> 16) & 0xff;
+	c = get32(d + 20 + 12) & 0xff;
+	m = a > b ? a : b;
+	m = m > c ? m : c;
+	D.last_rssi = (int)m - (int)agc - IWLAGN_RSSI_OFFSET;
+}
+
+static void rx_mpdu(const struct iwl_pkt *pkt)
+{
+	unsigned int plen = iwl_pkt_payload_len(pkt), len, status;
+	const unsigned char *d = pkt->data;
+
+	if (plen < 8)
+		return;
+	len = get16(d);
+	if (4 + len + 4 > plen)
+		return;
+	status = get32(d + 4 + len);
+	if (!(status & IWL_RX_CRC_OK) || !(status & IWL_RX_OVERRUN_OK))
+		return;
+	wifi_rx_frame(d + 4, len, D.last_rssi, status, (unsigned int)D.last_chan);
+}
+
+static int scan_band(int band5);
+
+static void rx_handler(const struct iwl_pkt *pkt)
+{
+	const unsigned char *d = pkt->data;
+	unsigned int n = iwl_pkt_payload_len(pkt);
+
+	switch (pkt->cmd) {
+	case REPLY_ALIVE:
+		if (n >= sizeof(struct alive_resp)) {
+			const struct alive_resp *a = (const void *)d;
+
+			D.alive_ok = a->is_valid == 1;
+			D.err_table = a->error_event_table_ptr;
+			D.alive = 1;
+		}
+		break;
+	case CALIBRATION_RES_NOTIFICATION:
+		calib_store(d, n);
+		break;
+	case CALIBRATION_COMPLETE_NOTIF:
+		D.calib_done = 1;
+		break;
+	case REPLY_RX_PHY_CMD:
+		rx_phy(pkt);
+		break;
+	case REPLY_RX_MPDU_CMD:
+		rx_mpdu(pkt);
+		break;
+	case REPLY_TX:
+		/* struct iwlagn_tx_resp: status at 32.  Unlike the 7265D's
+		 * tx_resp_v3 there is no measurement_req_id/reduced_tpc/
+		 * reserved before tfd_info, so 36 is the agg sequence. */
+		iwl_tx_reclaim((pkt->seq >> 8) & 0x1f, pkt->seq & 0xff);
+		if (n >= 34 && d[32] != 1 && d[32] != 2) {
+			serial_puts(SERIAL_COM1, "iwl: tx failed q");
+			serial_putdec(SERIAL_COM1, (pkt->seq >> 8) & 0x1f);
+			serial_puts(SERIAL_COM1, " status ");
+			serial_puthex(SERIAL_COM1, get16(d + 32));
+			serial_puts(SERIAL_COM1, "\n");
+		}
+		break;
+	case SCAN_COMPLETE_NOTIFICATION:
+		/* DVM scans one band per request: 2.4 GHz, then 5 GHz.  The
+		 * second request is sent from scan_kick(): a synchronous command
+		 * from inside the RX handler would poll the ring re-entrantly. */
+		if (D.scanning && !D.scan_band_5 && D.n5) {
+			D.scan_next5 = 1;
+			break;
+		}
+		D.scanning = 0;
+		wifi_scan_done(1);
+		break;
+	case REPLY_ERROR:
+		if (n >= 8) {
+			serial_puts(SERIAL_COM1, "iwl: REPLY_ERROR type ");
+			serial_puthex(SERIAL_COM1, get32(d));
+			serial_puts(SERIAL_COM1, " cmd ");
+			serial_puthex(SERIAL_COM1, d[4]);
+			serial_puts(SERIAL_COM1, "\n");
+		}
+		break;
+	default:
+		break;
+	}
+}
+
+static void scan_kick(void)
+{
+	if (!D.scan_next5)
+		return;
+	D.scan_next5 = 0;
+	if (scan_band(1) < 0) {
+		D.scanning = 0;
+		wifi_scan_done(0);
+	}
+}
+
+int iwl_dvm_poll(void)
+{
+	int r = iwl_rx_poll();
+
+	scan_kick();
+	return r;
+}
+
+/* ---- firmware load ------------------------------------------------------- */
+/* struct iwl_error_event_table (dvm/main.c): valid, error_id, pc, blink1,
+ * blink2, ilink1, ilink2, data1, data2, line, bcon_time, tsf_low, tsf_hi,
+ * gp1, gp2, gp3.  Dumped once per firmware run, two lines so the netlog
+ * mirror keeps them. */
+static void dump_error_log(void)
+{
+	unsigned int i;
+	static unsigned int dumped_for;
+
+	if (!D.err_table || D.err_table >= 0x01000000u ||
+	    dumped_for == D.err_table + D.alive)
+		return;
+	dumped_for = D.err_table + D.alive;
+	for (i = 0; i < 16; i++) {
+		if (i % 8 == 0)
+			serial_puts(SERIAL_COM1, i ? "\niwl: errlog+8:" : "iwl: errlog:");
+		serial_puts(SERIAL_COM1, " ");
+		serial_puthex(SERIAL_COM1, iwl_mem_rd32(D.err_table + i * 4));
+	}
+	serial_puts(SERIAL_COM1, "\n");
+}
+
+/* iwl_load_ucode_wait_alive() + iwl_alive_notify(). */
+static int load_and_alive(int img)
+{
+	static const unsigned char q_fifo[4] = { 3, 2, 1, 0 }; /* VO VI BE BK */
+	unsigned char coex[4 + 16 * 4];
+	unsigned int t, i;
+
+	D.alive = 0;
+	if (iwl_start_hw() < 0 || iwl_load_fw(img) < 0)
+		return -1;
+	for (t = 0; t < 1000 && !D.alive; t++) {
+		if (iwl_rx_poll() < 0)
+			break;
+		if (!D.alive)
+			iwl_udelay(1000);
+	}
+	if (!D.alive || !D.alive_ok) {
+		logs(D.alive ? "loaded ucode is not valid" : "no ALIVE from firmware");
+		return -1;
+	}
+	iwl_udelay(5000);                     /* give RF-kill time to run */
+	iwl_fw_alive(0);
+	for (i = 0; i < 4; i++)
+		iwl_txq_enable_fifo(i, q_fifo[i]);
+	iwl_memzero(coex, sizeof(coex));      /* WiMAX coexistence off */
+	if (cmd(COEX_PRIORITY_TABLE_CMD, coex, sizeof(coex)) < 0)
+		return -1;
+	store_xtal_calib();
+	return calib_send_all();
+}
+
+static int run_init_image(void)
+{
+	struct calib_cfg_cmd c;
+	unsigned int t;
+
+	D.state = "init image";
+	D.calib_done = 0;
+	if (load_and_alive(IWL_IMG_INIT) < 0)
+		return -1;
+	iwl_memzero(&c, sizeof(c));
+	c.ucd_once.is_enable = IWL_CALIB_INIT_CFG_ALL;
+	c.ucd_once.start = IWL_CALIB_INIT_CFG_ALL;
+	c.ucd_once.send_res = IWL_CALIB_INIT_CFG_ALL;
+	c.ucd_flags = IWL_CALIB_SEND_COMPLETE;
+	if (cmd(CALIBRATION_CFG_CMD, &c, sizeof(c)) < 0)
+		return -1;
+	for (t = 0; t < 2000 && !D.calib_done; t++) {
+		if (iwl_rx_poll() < 0)
+			break;
+		if (!D.calib_done)
+			iwl_udelay(1000);
+	}
+	iwl_stop_device();
+	if (!D.calib_done) {
+		logs("INIT calibration did not complete");
+		dump_error_log();
+		return -1;
+	}
+	return 0;
+}
+
+/* ---- stations ------------------------------------------------------------ */
+static int send_add_sta(const struct addsta_cmd *c)
+{
+	unsigned char r[4] = { 0, 0, 0, 0 };
+
+	if (iwl_send_cmd(REPLY_ADD_STA, c, sizeof(*c), r, 4) < 0)
+		return -1;
+	if (!(r[0] & 1)) {                    /* ADD_STA_SUCCESS_MSK */
+		iwl_loghex("ADD_STA failed, status", r[0]);
+		return -1;
+	}
+	return 0;
+}
+
+static int send_lq(unsigned int sta, unsigned int rate)
+{
+	struct lq_cmd q;
+	unsigned int i;
+
+	iwl_memzero(&q, sizeof(q));
+	q.sta_id = sta;
+	q.single_ant = D.first_tx_ant;
+	q.dual_ant = D.valid_tx_ant & ~D.first_tx_ant;
+	if (!q.dual_ant)
+		q.dual_ant = 0x3;
+	q.agg_dis_start_th = 3;
+	q.agg_time_limit = 4000;
+	for (i = 0; i < 16; i++)
+		q.rs_table[i] = rate;
+	return cmd(REPLY_TX_LINK_QUALITY_CMD, &q, sizeof(q));
+}
+
+static unsigned int mgmt_rate(void)
+{
+	return D.chan > 14 ? (PLCP_6M | (D.first_tx_ant << RATE_ANT_POS))
+			   : (PLCP_1M | RATE_CCK | (D.first_tx_ant << RATE_ANT_POS));
+}
+
+static int add_station(unsigned int id, const unsigned char addr[6])
+{
+	struct addsta_cmd c;
+
+	iwl_memzero(&c, sizeof(c));
+	c.mode = 0;
+	iwl_memcpy(c.addr, addr, 6);
+	c.sta_id = id;
+	c.key.key_flags = STA_KEY_FLG_INVALID;
+	c.key.key_offset = 0xff;
+	if (send_add_sta(&c) < 0)
+		return -1;
+	return send_lq(id, mgmt_rate());
+}
+
+/* Un-assoc RXON clears the station table: re-add what we know. */
+static int restore_stations(void)
+{
+	static const unsigned char bcast[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
+
+	if (add_station(IWLAGN_BROADCAST_ID, bcast) < 0)
+		return -1;
+	if (D.ap_sta && add_station(IWL_AP_ID, D.bssid) < 0)
+		return -1;
+	return 0;
+}
+
+/* ---- RXON ---------------------------------------------------------------- */
+static void rxon_defaults(void)
+{
+	struct rxon_cmd *r = &D.rxon;
+	unsigned int rx = D.valid_rx_ant;
+	unsigned int cnt = (rx & (rx - 1)) ? 2 : 1;
+
+	iwl_memzero(r, sizeof(*r));
+	iwl_memcpy(r->node_addr, D.mac, 6);
+	r->dev_type = RXON_DEV_TYPE_ESS;
+	r->filter_flags = RXON_FILTER_ACCEPT_GRP;
+	/* iwlagn_set_rxon_chain(): valid chains, single-stream counts. */
+	r->rx_chain = (unsigned short)((rx << 1) | (cnt << 10) | (cnt << 12));
+	r->ht_single = r->ht_dual = r->ht_triple = 0xff;
+	r->channel = 1;
+	r->flags = RXON_FLG_BAND_24G | RXON_FLG_AUTO_DETECT;
+}
+
+static void rxon_set_channel(unsigned int chan)
+{
+	struct rxon_cmd *r = &D.rxon;
+
+	r->channel = chan;
+	if (chan > 14)
+		r->flags = (r->flags & ~(RXON_FLG_BAND_24G | RXON_FLG_AUTO_DETECT)) |
+			   RXON_FLG_SHORT_SLOT;
+	else
+		r->flags = (r->flags & ~RXON_FLG_SHORT_SLOT) |
+			   RXON_FLG_BAND_24G | RXON_FLG_AUTO_DETECT;
+	D.chan = chan;
+}
+
+static int send_tx_power(void)
+{
+	/* iwlagn_tx_power_dbm_cmd: 15 dBm global limit in half-dBm,
+	 * IWLAGN_TX_POWER_NO_CLOSED, srv_chan_lmt IWLAGN_TX_POWER_AUTO. */
+	unsigned char c[4] = { 30, 0x40, 0x7f, 0 };
+
+	return cmd(REPLY_TX_POWER_DBM_CMD, c, sizeof(c));
+}
+
+static int send_qos(void)
+{
+	unsigned char c[4 + 4 * 8];
+	unsigned int i;
+
+	iwl_memzero(c, sizeof(c));
+	for (i = 0; i < 4; i++) {            /* non-QoS EDCA: 15/1023, AIFS 2 */
+		put16(c + 4 + i * 8, 15);
+		put16(c + 4 + i * 8 + 2, 1023);
+		c[4 + i * 8 + 4] = 2;
+	}
+	return cmd(REPLY_QOS_PARAM, c, sizeof(c));
+}
+
+static int send_timing(unsigned int bi, unsigned int dtim)
+{
+	struct rxon_timing t;
+
+	iwl_memzero(&t, sizeof(t));
+	t.beacon_interval = bi ? bi : 100;
+	t.listen_interval = 10;
+	t.beacon_init_val = (unsigned int)t.beacon_interval * 1024u;
+	t.dtim_period = dtim ? dtim : 1;
+	return cmd(REPLY_RXON_TIMING, &t, sizeof(t));
+}
+
+/* iwlagn_commit_rxon(): always clear the association first (which wipes
+ * the station table), restore the stations, then connect if asked. */
+static int commit_rxon(int assoc, unsigned short aid, unsigned int bi,
+		       unsigned int dtim)
+{
+	struct rxon_cmd *r = &D.rxon;
+
+	r->flags |= RXON_FLG_TSF2HOST;
+	r->cck_basic_rates = r->channel <= 14 ? 0x0f : 0;
+	r->ofdm_basic_rates = 0x15;          /* 6, 12, 24 Mb/s */
+	r->filter_flags &= ~RXON_FILTER_ASSOC;
+	r->assoc_id = 0;
+	if (cmd(REPLY_RXON, r, sizeof(*r)) < 0)
+		return -1;
+	if (restore_stations() < 0)
+		return -1;
+	if (assoc) {
+		if (send_timing(bi, dtim) < 0 || send_qos() < 0)
+			return -1;
+		r->filter_flags |= RXON_FILTER_ASSOC;
+		r->assoc_id = aid;
+		if (cmd(REPLY_RXON, r, sizeof(*r)) < 0)
+			return -1;
+	}
+	D.assoc = assoc;
+	/* A tune needs a new TX power command, or nothing can be sent. */
+	return send_tx_power();
+}
+
+/* ---- bring-up ------------------------------------------------------------ */
+static int run_rt_image(void)
+{
+	struct calib_cfg_cmd c;
+	unsigned int ant = D.valid_tx_ant;
+	unsigned char power[36];
+
+	D.state = "runtime image";
+	if (load_and_alive(IWL_IMG_RT) < 0)
+		return -1;
+	/* iwlagn_send_calib_cfg_rt(DC) */
+	iwl_memzero(&c, sizeof(c));
+	c.ucd_once.is_enable = IWL_CALIB_RT_CFG_ALL;
+	c.ucd_once.start = IWL_CALIB_CFG_DC;
+	if (cmd(CALIBRATION_CFG_CMD, &c, sizeof(c)) < 0)
+		return -1;
+	if (cmd(TX_ANT_CONFIGURATION_CMD, &ant, 4) < 0)
+		return -1;
+	rxon_defaults();
+	D.ap_sta = 0;
+	if (commit_rxon(0, 0, 0, 0) < 0)
+		return -1;
+	iwl_memzero(power, sizeof(power));   /* CAM: no power save */
+	return cmd(POWER_TABLE_CMD, power, sizeof(power));
+}
+
+int iwl_dvm_up(unsigned char mac[6])
+{
+	if (D.up) {
+		iwl_memcpy(mac, D.mac, 6);
+		return 0;
+	}
+	if (iwl_probe() < 0 || !iwl_is_dvm()) {
+		D.state = "no card";
+		return -1;
+	}
+	if (iwl_fw_parse() < 0) {
+		D.state = "firmware missing";
+		return -1;
+	}
+	if (!D.calib) {
+		D.calib = (unsigned char *)mm_alloc_pages(CALIB_PAGES);
+		D.eeprom = (unsigned char *)mm_alloc_page();
+		D.scratch = (unsigned char *)mm_alloc_pages(2);
+		if (!D.calib || !D.eeprom || !D.scratch) {
+			D.calib = 0;
+			D.state = "out of memory";
+			return -1;
+		}
+	}
+	D.state = "eeprom";
+	if (iwl_start_hw() < 0 || read_eeprom() < 0 || parse_eeprom() < 0) {
+		iwl_stop_device();
+		if (D.state[0] == 'e')
+			D.state = "EEPROM read failed";
+		return -1;
+	}
+	serial_puts(SERIAL_COM1, "iwl: radio cfg ");
+	serial_puthex(SERIAL_COM1, D.radio_cfg);
+	serial_puts(SERIAL_COM1, " tx ant ");
+	serial_puthex(SERIAL_COM1, D.valid_tx_ant);
+	serial_puts(SERIAL_COM1, "\n");
+	if (iwl_rfkill()) {
+		logs("radio is off (hardware RF-kill switch)");
+		iwl_stop_device();
+		D.state = "RF-kill switch is on";
+		return -1;
+	}
+	iwl_set_nic_config(nic_config);
+	iwl_set_rx_handler(rx_handler);
+	D.calib_len = 0;
+	if (run_init_image() < 0 || run_rt_image() < 0) {
+		dump_error_log();
+		iwl_stop_device();
+		D.state = "firmware start failed";
+		return -1;
+	}
+	D.up = 1;
+	D.state = "ready";
+	iwl_memcpy(mac, D.mac, 6);
+	logs("firmware running");
+	return 0;
+}
+
+int iwl_dvm_is_up(void)
+{
+	return D.up;
+}
+
+const char *iwl_dvm_state(void)
+{
+	return D.state ? D.state : "down";
+}
+
+void iwl_dvm_down(void)
+{
+	if (iwl_present())
+		iwl_stop_device();
+	D.up = 0;
+	D.scanning = 0;
+	D.ap_sta = D.assoc = 0;
+	D.have_tk = 0;
+	D.state = "down";
+}
+
+/* ---- scan ---------------------------------------------------------------- */
+static int scan_band(int band5)
+{
+	struct scan_cmd *s = (struct scan_cmd *)D.scratch;
+	const unsigned short *list = band5 ? D.ch5 : D.ch24;
+	unsigned int n = band5 ? D.n5 : D.n24, i, o;
+	unsigned char *f;
+	struct scan_chan *ch;
+	static const unsigned char r24[] = { 1, 8, 0x82, 0x84, 0x8b, 0x96,
+					     0x0c, 0x12, 0x18, 0x24,
+					     50, 4, 0x30, 0x48, 0x60, 0x6c };
+	static const unsigned char r5[] = { 1, 8, 0x8c, 0x12, 0x98, 0x24,
+					    0xb0, 0x48, 0x60, 0x6c };
+	unsigned int rx = D.valid_rx_ant;
+
+	iwl_memzero(D.scratch, 4096);
+	s->quiet_plcp_th = 1;                    /* IWL_PLCP_QUIET_THRESH */
+	s->quiet_time = 10;                      /* IWL_ACTIVE_QUIET_TIME */
+	s->good_crc_th = 1;                      /* IWL_GOOD_CRC_TH_DEFAULT */
+	s->rx_chain = (unsigned short)((rx << 1) | (rx << 7) | (rx << 4) | 1);
+	s->flags = band5 ? 0 : (RXON_FLG_BAND_24G | RXON_FLG_AUTO_DETECT);
+	s->filter_flags = RXON_FILTER_ACCEPT_GRP | (1u << 6); /* BCON_AWARE */
+	s->tx.tx_flags = TX_FLG_SEQ_CTL;
+	s->tx.sta_id = IWLAGN_BROADCAST_ID;
+	s->tx.life_time = 0xffffffffu;
+	s->tx.rate_n_flags = (band5 ? PLCP_6M : (PLCP_1M | RATE_CCK)) |
+			     (D.first_tx_ant << RATE_ANT_POS);
+	/* Probe request: broadcast, wildcard SSID, our rates. */
+	f = D.scratch + sizeof(*s);
+	f[0] = 0x40;
+	for (i = 0; i < 6; i++) {
+		f[4 + i] = 0xff;
+		f[10 + i] = D.mac[i];
+		f[16 + i] = 0xff;
+	}
+	o = 24;
+	f[o++] = 0;
+	f[o++] = 0;
+	if (band5) {
+		iwl_memcpy(f + o, r5, sizeof(r5));
+		o += sizeof(r5);
+	} else {
+		iwl_memcpy(f + o, r24, sizeof(r24));
+		o += sizeof(r24);
+	}
+	s->tx.len = o;
+	ch = (struct scan_chan *)(f + o);
+	for (i = 0; i < n; i++) {
+		ch[i].channel = list[i] & 0xff;
+		ch[i].type = (list[i] & 0x100) ? 1 : 0;   /* active : passive */
+		ch[i].active_dwell = band5 ? 22 : 33;
+		ch[i].passive_dwell = band5 ? 110 : 120;
+		ch[i].dsp_atten = 110;
+		ch[i].tx_gain = band5 ? ((1 << 5) | (3 << 3) | 3) : ((1 << 5) | (5 << 3));
+	}
+	s->channel_count = n;
+	s->len = (unsigned short)(sizeof(*s) + o + n * sizeof(struct scan_chan));
+	D.scan_band_5 = band5;
+	serial_puts(SERIAL_COM1, "iwl: scan band ");
+	serial_puts(SERIAL_COM1, band5 ? "5" : "2.4");
+	serial_puts(SERIAL_COM1, " len ");
+	serial_putdec(SERIAL_COM1, s->len);
+	serial_puts(SERIAL_COM1, " chans ");
+	serial_putdec(SERIAL_COM1, n);
+	serial_puts(SERIAL_COM1, "\n");
+	return cmd(REPLY_SCAN_CMD, s, s->len);
+}
+
+int iwl_dvm_scan_running(void)
+{
+	scan_kick();
+	return D.scanning;
+}
+
+int iwl_dvm_scan_start(void)
+{
+	if (!D.up || D.scanning || D.assoc || !D.n24)
+		return -1;
+	D.scanning = 1;
+	D.scan_next5 = 0;
+	if (scan_band(0) < 0) {
+		D.scanning = 0;
+		return -1;
+	}
+	return 0;
+}
+
+/* ---- connection ---------------------------------------------------------- */
+int iwl_dvm_prepare_connect(const unsigned char bssid[6], unsigned int chan,
+			    unsigned int beacon_int)
+{
+	(void)beacon_int;
+	if (!D.up)
+		return -1;
+	if (D.ap_sta || D.assoc)
+		iwl_dvm_disconnect();
+	iwl_memcpy(D.bssid, bssid, 6);
+	iwl_memcpy(D.rxon.bssid_addr, bssid, 6);
+	rxon_set_channel(chan);
+	D.ap_sta = 1;            /* restore_stations() adds the AP station */
+	D.have_tk = 0;
+	if (commit_rxon(0, 0, 0, 0) < 0) {
+		D.ap_sta = 0;
+		return -1;
+	}
+	return 0;
+}
+
+int iwl_dvm_set_assoc(unsigned short aid, unsigned int beacon_int,
+		      unsigned int dtim)
+{
+	if (!D.ap_sta)
+		return -1;
+	if (commit_rxon(1, aid, beacon_int, dtim) < 0)
+		return -1;
+	/* Data at a fixed OFDM rate on the first antenna. */
+	return send_lq(IWL_AP_ID, PLCP_12M | (D.first_tx_ant << RATE_ANT_POS));
+}
+
+void iwl_dvm_disconnect(void)
+{
+	unsigned int i;
+
+	if (!D.up)
+		return;
+	D.ap_sta = 0;
+	D.have_tk = 0;
+	for (i = 0; i < 6; i++)
+		D.rxon.bssid_addr[i] = 0;
+	commit_rxon(0, 0, 0, 0);
+}
+
+/* ---- TX ------------------------------------------------------------------ */
+static int tx_frame(const unsigned char *frame, unsigned int len, int mgmt,
+		    int protect)
+{
+	struct tx_cmd c;
+	int bcast = frame[4] & 1;
+	unsigned int stype = (frame[0] >> 4) & 0xf;
+	unsigned int sta = D.ap_sta ? IWL_AP_ID : IWLAGN_BROADCAST_ID;
+	int q = mgmt ? IWL_DVM_Q_VO : IWL_DVM_Q_BE;
+
+	iwl_memzero(&c, sizeof(c));
+	c.len = len;
+	c.tx_flags = TX_FLG_SEQ_CTL | (bcast ? 0 : TX_FLG_ACK);
+	c.rate_n_flags = mgmt ? mgmt_rate()
+			      : (PLCP_12M | (D.first_tx_ant << RATE_ANT_POS));
+	c.tid_tspec = 8;                     /* IWL_TID_NON_QOS (= 8 on DVM) */
+	c.pm_frame_timeout = mgmt ? ((stype == 0 || stype == 2) ? 3 : 2) : 0;
+	c.sta_id = sta;
+	c.life_time = 0xffffffffu;
+	c.rts_retry_limit = 60;
+	c.data_retry_limit = mgmt ? 3 : 15;
+	if (protect) {
+		if (!D.have_tk)
+			return -1;
+		c.sec_ctl = TX_SEC_CCM;
+		iwl_memcpy(c.key, D.tk, 16);
+	}
+	return iwl_tx(q, &c, sizeof(c), frame, len, sta, protect ? 8 : 0);
+}
+
+int iwl_dvm_tx_mgmt(const unsigned char *frame, unsigned int len)
+{
+	return D.up ? tx_frame(frame, len, 1, 0) : -1;
+}
+
+int iwl_dvm_tx_data(const unsigned char *frame, unsigned int len, int protect)
+{
+	return D.up && D.ap_sta ? tx_frame(frame, len, 0, protect) : -1;
+}
+
+/* ---- keys ---------------------------------------------------------------- */
+int iwl_dvm_set_key(int pairwise, int keyidx, const unsigned char key[16],
+		    const unsigned char rsc[6])
+{
+	struct addsta_cmd c;
+
+	(void)rsc;          /* RX replay counters start at 0 (see iwl_mvm.c) */
+	if (!D.ap_sta)
+		return -1;
+	/* iwlagn_send_sta_key(): ADD_STA modify on the AP station; the group
+	 * key also lives on the AP station in station mode. */
+	iwl_memzero(&c, sizeof(c));
+	c.mode = STA_CONTROL_MODIFY;
+	iwl_memcpy(c.addr, D.bssid, 6);
+	c.sta_id = IWL_AP_ID;
+	c.modify_mask = STA_MODIFY_KEY_MASK;
+	c.key.key_flags = (unsigned short)(((keyidx & 3) << 8) |
+					   STA_KEY_FLG_MAP_KEY | STA_KEY_FLG_CCMP);
+	if (!pairwise)
+		c.key.key_flags |= STA_KEY_MULTICAST;
+	c.key.key_offset = pairwise ? 0 : 1;
+	iwl_memcpy(c.key.key, key, 16);
+	if (send_add_sta(&c) < 0)
+		return -1;
+	if (pairwise) {
+		iwl_memcpy(D.tk, key, 16);
+		D.have_tk = 1;
+	}
+	return 0;
+}
+
+void iwl_dvm_clear_keys(void)
+{
+	D.have_tk = 0;
+}

+ 30 - 0
iwl_dvm.h

@@ -0,0 +1,30 @@
+/* Intel 6200/6300 (iwlwifi "6000" family) firmware layer ("dvm"): see
+ * 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
+ * station ID 15 (IWLAGN_BROADCAST_ID); management frames go on the VO
+ * queue, non-QoS data on BE. */
+#ifndef IWL_DVM_H
+#define IWL_DVM_H
+
+int  iwl_dvm_up(unsigned char mac[6]);
+int  iwl_dvm_is_up(void);
+void iwl_dvm_down(void);
+const char *iwl_dvm_state(void);
+int  iwl_dvm_poll(void);
+int  iwl_dvm_scan_start(void);
+int  iwl_dvm_scan_running(void);
+int  iwl_dvm_prepare_connect(const unsigned char bssid[6], unsigned int chan,
+			     unsigned int beacon_int);
+int  iwl_dvm_set_assoc(unsigned short aid, unsigned int beacon_int,
+		       unsigned int dtim);
+void iwl_dvm_disconnect(void);
+int  iwl_dvm_tx_mgmt(const unsigned char *frame, unsigned int len);
+int  iwl_dvm_tx_data(const unsigned char *frame, unsigned int len,
+		     int protect);
+int  iwl_dvm_set_key(int pairwise, int keyidx, const unsigned char key[16],
+		     const unsigned char rsc[6]);
+void iwl_dvm_clear_keys(void);
+
+#endif

+ 250 - 46
iwl_trans.c

@@ -18,7 +18,37 @@
 #include "ramfs.h"
 #include "ramfs.h"
 #include "serial.h"
 #include "serial.h"
 
 
-#define FW_PATH "firmware/iwlwifi-7265D-29.ucode"
+/* Per-family transport parameters (Linux: iwl_cfg_trans_params plus the
+ * op_mode's iwl_trans_config).  The 7000 family runs the MVM firmware with
+ * DQA queues and wide command headers; the 6000 family runs the DVM
+ * firmware with the four AC queues, the command queue on 4 and 4-byte
+ * headers. */
+struct iwl_family {
+	const char *fw_path;
+	unsigned char dvm;            /* DVM firmware (6000) vs MVM (7000) */
+	unsigned char tlv;            /* TLV .ucode vs the v2 fixed header */
+	unsigned char cmd_q, cmd_fifo, nq;
+	unsigned char wide_hdr;       /* mvm: trans->wide_cmd_header */
+	unsigned char bc_dword;       /* byte-count table in dwords */
+	unsigned char scd_set_active; /* mvm: trans_cfg.scd_set_active */
+	unsigned char chain_ext_wa;   /* base_params->scd_chain_ext_wa */
+	unsigned char shadow_reg;     /* base_params->shadow_reg_enable */
+	unsigned char wake_me;        /* tell ME we leave (7000 family) */
+	unsigned char q[4], q_slots[4]; /* data/mgmt queues to allocate */
+};
+
+static const struct iwl_family fam_7000 = {
+	"firmware/iwlwifi-7265D-29.ucode", 0, 1, IWL_Q_CMD, 7, 31,
+	1, 1, 1, 0, 1, 1,
+	{ IWL_Q_AUX, IWL_Q_BSS, 0xff, 0xff }, { 8, 32, 0, 0 },
+};
+
+static const struct iwl_family fam_6000 = {
+	"firmware/iwlwifi-6000-4.ucode", 1, 0, IWL_DVM_Q_CMD, 7, 20,
+	0, 0, 0, 1, 0, 0,
+	{ IWL_DVM_Q_VO, IWL_DVM_Q_VI, IWL_DVM_Q_BE, IWL_DVM_Q_BK },
+	{ 16, 4, 32, 4 },
+};
 
 
 /* ---- CSR / HBUS / FH / PRPH registers (iwl-csr.h, iwl-fh.h, iwl-prph.h) */
 /* ---- CSR / HBUS / FH / PRPH registers (iwl-csr.h, iwl-fh.h, iwl-prph.h) */
 #define CSR_HW_IF_CONFIG_REG     0x000
 #define CSR_HW_IF_CONFIG_REG     0x000
@@ -109,6 +139,7 @@
 #define SCD_DRAM_BASE_ADDR       (SCD_BASE + 0x8)
 #define SCD_DRAM_BASE_ADDR       (SCD_BASE + 0x8)
 #define SCD_TXFACT               (SCD_BASE + 0x10)
 #define SCD_TXFACT               (SCD_BASE + 0x10)
 #define SCD_QUEUECHAIN_SEL       (SCD_BASE + 0xe8)
 #define SCD_QUEUECHAIN_SEL       (SCD_BASE + 0xe8)
+#define SCD_CHAINEXT_EN          (SCD_BASE + 0x244)
 #define SCD_AGGR_SEL             (SCD_BASE + 0x248)
 #define SCD_AGGR_SEL             (SCD_BASE + 0x248)
 #define SCD_GP_CTRL              (SCD_BASE + 0x1a8)
 #define SCD_GP_CTRL              (SCD_BASE + 0x1a8)
 #define SCD_EN_CTRL              (SCD_BASE + 0x254)
 #define SCD_EN_CTRL              (SCD_BASE + 0x254)
@@ -186,6 +217,12 @@ struct iwl_state {
 	struct iwl_fw_info fw;
 	struct iwl_fw_info fw;
 	const unsigned char *fw_data;
 	const unsigned char *fw_data;
 	unsigned int fw_size;
 	unsigned int fw_size;
+	const struct iwl_family *fam;
+	unsigned int sub;           /* IWL_SUB_* within the family */
+	void (*nic_config)(void);   /* op-mode NIC config (DVM), may be 0 */
+	/* v2 (pre-TLV) image: [IWL_IMG_*] instruction and data sections */
+	const unsigned char *img_inst[2], *img_data[2];
+	unsigned int img_inst_len[2], img_data_len[2];
 };
 };
 
 
 static struct iwl_state *S;   /* page-allocated on probe */
 static struct iwl_state *S;   /* page-allocated on probe */
@@ -317,15 +354,49 @@ static int grab_nic(void)
 }
 }
 
 
 /* ---- probe --------------------------------------------------------------- */
 /* ---- probe --------------------------------------------------------------- */
-static const char *chip_for(unsigned int devid)
+static const char *chip_for(unsigned int devid,
+			    const struct iwl_family **fam, unsigned int *sub)
 {
 {
+	*fam = &fam_7000;
+	*sub = IWL_SUB_7265;
 	switch (devid) {
 	switch (devid) {
 	case 0x095a: case 0x095b: return "Intel Wireless 7265";
 	case 0x095a: case 0x095b: return "Intel Wireless 7265";
 	case 0x3165: case 0x3166: return "Intel Wireless 3165";
 	case 0x3165: case 0x3166: return "Intel Wireless 3165";
 	}
 	}
+	*fam = &fam_6000;
+	switch (devid) {
+	case 0x4239: case 0x422c:
+		*sub = IWL_SUB_6000I;
+		return "Intel Centrino Advanced-N 6200";
+	case 0x4238: case 0x422b:
+		*sub = IWL_SUB_6000;
+		return "Intel Centrino Ultimate-N 6300";
+	}
 	return 0;
 	return 0;
 }
 }
 
 
+/* The firmware reported SW_ERR/HW_ERR: it is dead until reloaded. */
+int iwl_fatal(void)
+{
+	return S && S->fatal;
+}
+
+int iwl_is_dvm(void)
+{
+	return S && S->fam->dvm;
+}
+
+unsigned int iwl_subfamily(void)
+{
+	return S ? S->sub : 0;
+}
+
+void iwl_set_nic_config(void (*fn)(void))
+{
+	if (S)
+		S->nic_config = fn;
+}
+
 int iwl_present(void)
 int iwl_present(void)
 {
 {
 	return S != 0;
 	return S != 0;
@@ -397,12 +468,13 @@ int iwl_probe(void)
 			nfunc = (pci_cfg_read32(bus, dev, 0, 0x0c) & 0x800000u) ? 8 : 1;
 			nfunc = (pci_cfg_read32(bus, dev, 0, 0x0c) & 0x800000u) ? 8 : 1;
 			for (func = 0; func < nfunc; func++) {
 			for (func = 0; func < nfunc; func++) {
 				unsigned int idf = pci_cfg_read32(bus, dev, func, 0);
 				unsigned int idf = pci_cfg_read32(bus, dev, func, 0);
-				unsigned int bar, cmd, page;
+				unsigned int bar, cmd, page, sub;
+				const struct iwl_family *fam;
 				const char *name;
 				const char *name;
 
 
 				if ((idf & 0xffffu) != 0x8086)
 				if ((idf & 0xffffu) != 0x8086)
 					continue;
 					continue;
-				name = chip_for(idf >> 16);
+				name = chip_for(idf >> 16, &fam, &sub);
 				if (!name)
 				if (!name)
 					continue;
 					continue;
 				bar = pci_cfg_read32(bus, dev, func, 0x10);
 				bar = pci_cfg_read32(bus, dev, func, 0x10);
@@ -424,6 +496,8 @@ int iwl_probe(void)
 				S->func = func;
 				S->func = func;
 				S->devid = idf >> 16;
 				S->devid = idf >> 16;
 				S->chip = name;
 				S->chip = name;
+				S->fam = fam;
+				S->sub = sub;
 				pci_d0(bus, dev, func);
 				pci_d0(bus, dev, func);
 				/* Memory + bus master, INTx off (polled). */
 				/* Memory + bus master, INTx off (polled). */
 				cmd = pci_cfg_read32(bus, dev, func, 0x04);
 				cmd = pci_cfg_read32(bus, dev, func, 0x04);
@@ -453,7 +527,8 @@ int iwl_probe(void)
 				}
 				}
 				/* 0x095a/b: only rev-D silicon runs the 7265D
 				/* 0x095a/b: only rev-D silicon runs the 7265D
 				 * firmware (Linux: CSR_HW_REV_TYPE_7265D). */
 				 * firmware (Linux: CSR_HW_REV_TYPE_7265D). */
-				if (S->devid != 0x3165 && S->devid != 0x3166 &&
+				if (!fam->dvm &&
+				    S->devid != 0x3165 && S->devid != 0x3166 &&
 				    (S->hw_rev & 0xfff0u) != 0x210u)
 				    (S->hw_rev & 0xfff0u) != 0x210u)
 					iwl_log("warning: not a 7265D; the 7265D firmware may fail");
 					iwl_log("warning: not a 7265D; the 7265D firmware may fail");
 				return 0;
 				return 0;
@@ -478,6 +553,57 @@ static unsigned int le32(const unsigned char *p)
 	return p[0] | (p[1] << 8) | (p[2] << 16) | ((unsigned int)p[3] << 24);
 	return p[0] | (p[1] << 8) | (p[2] << 16) | ((unsigned int)p[3] << 24);
 }
 }
 
 
+/* Pre-TLV image (iwl-drv.c iwl_parse_v1_v2_firmware(), API >= 3 = v2):
+ * ver, build, inst, data, init, init_data, boot sizes, then the sections
+ * in that order. */
+static int fw_parse_v2(const unsigned char *d, unsigned int size)
+{
+	unsigned int inst, data, init, initd, boot, o = 28;
+
+	if (size < 28)
+		return -1;
+	S->fw.ver = le32(d);
+	inst = le32(d + 8);
+	data = le32(d + 12);
+	init = le32(d + 16);
+	initd = le32(d + 20);
+	boot = le32(d + 24);
+	if (((S->fw.ver >> 8) & 0xff) < 3 ||
+	    o + inst + data + init + initd + boot != size || !inst || !init) {
+		iwl_log("firmware is not a v2 .ucode image");
+		return -1;
+	}
+	S->img_inst[IWL_IMG_RT] = d + o;
+	S->img_inst_len[IWL_IMG_RT] = inst;
+	o += inst;
+	S->img_data[IWL_IMG_RT] = d + o;
+	S->img_data_len[IWL_IMG_RT] = data;
+	o += data;
+	S->img_inst[IWL_IMG_INIT] = d + o;
+	S->img_inst_len[IWL_IMG_INIT] = init;
+	o += init;
+	S->img_data[IWL_IMG_INIT] = d + o;
+	S->img_data_len[IWL_IMG_INIT] = initd;
+	{
+		static const char hx[] = "0123456789abcdef";
+		unsigned int i, v = S->fw.ver;
+		char *h = S->fw.human;
+
+		/* "6000 v9.221.4.1", like Linux's "%u.%u.%u.%u" */
+		h[0] = 'v';
+		for (i = 0; i < 4; i++) {
+			unsigned int b = (v >> (24 - 8 * i)) & 0xff;
+
+			*++h = hx[b >> 4 & 0xf];
+			*++h = hx[b & 0xf];
+			if (i < 3)
+				*++h = '.';
+		}
+		*++h = 0;
+	}
+	return 0;
+}
+
 int iwl_fw_parse(void)
 int iwl_fw_parse(void)
 {
 {
 	const unsigned char *d;
 	const unsigned char *d;
@@ -487,9 +613,23 @@ int iwl_fw_parse(void)
 		return -1;
 		return -1;
 	if (S->fw.present)
 	if (S->fw.present)
 		return 0;
 		return 0;
-	d = ramfs_lookup(FW_PATH, &size);
-	if (!d || size < 88 || le32(d) != 0 || le32(d + 4) != 0x0a4c5749u) {
-		iwl_log("firmware " FW_PATH " missing or not a TLV image");
+	d = ramfs_lookup(S->fam->fw_path, &size);
+	if (!d) {
+		iwl_log("firmware missing from /ramfs:");
+		iwl_log(S->fam->fw_path);
+		return -1;
+	}
+	if (!S->fam->tlv) {
+		if (fw_parse_v2(d, size) < 0)
+			return -1;
+		S->fw_data = d;
+		S->fw_size = size;
+		S->fw.present = 1;
+		iwl_log(S->fw.human);
+		return 0;
+	}
+	if (size < 88 || le32(d) != 0 || le32(d + 4) != 0x0a4c5749u) {
+		iwl_log("firmware is not a TLV image");
 		return -1;
 		return -1;
 	}
 	}
 	S->fw_data = d;
 	S->fw_data = d;
@@ -631,8 +771,10 @@ void iwl_stop_device(void)
 		rx_stop();
 		rx_stop();
 		iwl_wr_prph(APMG_CLK_EN_REG, APMG_CLK_DMA_RQT);
 		iwl_wr_prph(APMG_CLK_EN_REG, APMG_CLK_DMA_RQT);
 		/* Inform ME that we are leaving (7000 family). */
 		/* Inform ME that we are leaving (7000 family). */
-		prph_set(APMG_PCIDEV_STT_REG, APMG_PCIDEV_WAKE_ME);
-		iwl_udelay(5000);
+		if (S->fam->wake_me) {
+			prph_set(APMG_PCIDEV_STT_REG, APMG_PCIDEV_WAKE_ME);
+			iwl_udelay(5000);
+		}
 	}
 	}
 	stop_master();
 	stop_master();
 	iwl_set(CSR_RESET, RESET_SW_RESET);
 	iwl_set(CSR_RESET, RESET_SW_RESET);
@@ -690,10 +832,12 @@ static int alloc_rings(void)
 				return -1;
 				return -1;
 		}
 		}
 	}
 	}
-	if (alloc_txq(IWL_Q_CMD, CMD_SLOTS, CMD_BUF) < 0 ||
-	    alloc_txq(IWL_Q_AUX, 8, DATA_BUF) < 0 ||
-	    alloc_txq(IWL_Q_BSS, DATA_SLOTS, DATA_BUF) < 0)
+	if (alloc_txq(S->fam->cmd_q, CMD_SLOTS, CMD_BUF) < 0)
 		return -1;
 		return -1;
+	for (i = 0; i < 4; i++)
+		if (S->fam->q[i] != 0xff &&
+		    alloc_txq(S->fam->q[i], S->fam->q_slots[i], DATA_BUF) < 0)
+			return -1;
 	return 0;
 	return 0;
 }
 }
 
 
@@ -734,7 +878,8 @@ static void tx_init(void)
 	for (q = 0; q < IWL_NUM_Q; q++)
 	for (q = 0; q < IWL_NUM_Q; q++)
 		if (S->q[q].tfds)
 		if (S->q[q].tfds)
 			iwl_wr(fh_cbbc(q), (unsigned int)S->q[q].tfds >> 8);
 			iwl_wr(fh_cbbc(q), (unsigned int)S->q[q].tfds >> 8);
-	prph_set(SCD_GP_CTRL, SCD_GP_AUTO_ACTIVE | SCD_GP_31_QUEUES);
+	prph_set(SCD_GP_CTRL, SCD_GP_AUTO_ACTIVE |
+		 (S->fam->nq > 20 ? SCD_GP_31_QUEUES : 0));
 }
 }
 
 
 /* ---- firmware load ------------------------------------------------------ */
 /* ---- firmware load ------------------------------------------------------ */
@@ -772,12 +917,37 @@ static int load_chunk(unsigned int dst, const unsigned char *src,
 	return 0;
 	return 0;
 }
 }
 
 
+static int load_section(unsigned int dst, const unsigned char *p,
+			unsigned int len)
+{
+	unsigned int o;
+
+	for (o = 0; o < len; o += FH_MEM_TB_MAX_LENGTH) {
+		unsigned int n = len - o;
+
+		if (n > FH_MEM_TB_MAX_LENGTH)
+			n = FH_MEM_TB_MAX_LENGTH;
+		if (load_chunk(dst + o, p + o, n) < 0)
+			return -1;
+	}
+	return 0;
+}
+
 static int load_sections(int img)
 static int load_sections(int img)
 {
 {
 	unsigned int want = img == IWL_IMG_INIT ? TLV_SEC_INIT : TLV_SEC_RT;
 	unsigned int want = img == IWL_IMG_INIT ? TLV_SEC_INIT : TLV_SEC_RT;
 	const unsigned char *d = S->fw_data;
 	const unsigned char *d = S->fw_data;
 	unsigned int off = 88, nsec = 0;
 	unsigned int off = 88, nsec = 0;
 
 
+	if (!S->fam->tlv) {
+		/* IWLAGN_RTC_INST_LOWER_BOUND / IWLAGN_RTC_DATA_LOWER_BOUND */
+		if (load_section(0x000000, S->img_inst[img],
+				 S->img_inst_len[img]) < 0 ||
+		    load_section(0x800000, S->img_data[img],
+				 S->img_data_len[img]) < 0)
+			return -1;
+		return 0;
+	}
 	while (off + 8 <= S->fw_size) {
 	while (off + 8 <= S->fw_size) {
 		unsigned int t = le32(d + off), l = le32(d + off + 4);
 		unsigned int t = le32(d + off), l = le32(d + off + 4);
 		const unsigned char *p = d + off + 8;
 		const unsigned char *p = d + off + 8;
@@ -825,10 +995,14 @@ int iwl_load_fw(int img)
 	}
 	}
 	S->fatal = 0;
 	S->fatal = 0;
 	S->waiting = 0;
 	S->waiting = 0;
+	/* iwl_op_mode_nic_config(): radio SKU bits (DVM) before the rings. */
+	if (S->nic_config)
+		S->nic_config();
 	rx_init();
 	rx_init();
 	tx_init();
 	tx_init();
-	iwl_wr(CSR_MAC_SHADOW_REG_CTRL,
-	       iwl_rd(CSR_MAC_SHADOW_REG_CTRL) | 0x800fffffu);
+	if (S->fam->shadow_reg)
+		iwl_wr(CSR_MAC_SHADOW_REG_CTRL,
+		       iwl_rd(CSR_MAC_SHADOW_REG_CTRL) | 0x800fffffu);
 	iwl_wr(CSR_UCODE_DRV_GP1_CLR, UCODE_SW_BIT_RFKILL);
 	iwl_wr(CSR_UCODE_DRV_GP1_CLR, UCODE_SW_BIT_RFKILL);
 	if (load_sections(img) < 0) {
 	if (load_sections(img) < 0) {
 		iwl_log("firmware section load failed");
 		iwl_log("firmware section load failed");
@@ -841,34 +1015,56 @@ int iwl_load_fw(int img)
 
 
 /* After ALIVE: scheduler context, byte-count tables, cmd queue on FIFO 7,
 /* After ALIVE: scheduler context, byte-count tables, cmd queue on FIFO 7,
  * TX DMA channels (iwl_pcie_tx_start()). */
  * TX DMA channels (iwl_pcie_tx_start()). */
+/* iwl_trans_pcie_txq_enable() with a cfg (the transport programs the
+ * scheduler: FIFO, window, frame limit), as iwl_trans_ac_txq_enable()
+ * does for the command queue and for DVM's AC queues. */
+static void scd_enable_queue(unsigned int qn, unsigned int fifo)
+{
+	struct txq *q = &S->q[qn];
+	int cmdq = qn == S->fam->cmd_q;
+
+	if (cmdq && S->fam->scd_set_active)
+		iwl_wr_prph(SCD_EN_CTRL, 0);
+	iwl_wr_prph(scd_queue_status(qn), 1u << SCD_STTS_ACT_EN);
+	if (!cmdq)
+		prph_set(SCD_QUEUECHAIN_SEL, 1u << qn);
+	prph_clr(SCD_AGGR_SEL, 1u << qn);
+	q->wptr = q->rptr = 0;
+	iwl_wr(HBUS_TARG_WRPTR, 0 | (qn << 8));
+	iwl_wr_prph(scd_queue_rdptr(qn), 0);
+	mem_wr32(S->scd_base + SCD_CONTEXT_MEM + qn * 8, 0);
+	mem_wr32(S->scd_base + SCD_CONTEXT_MEM + qn * 8 + 4,
+		 (64u & 0x7f) | ((64u << 16) & 0x7f0000u));
+	iwl_wr_prph(scd_queue_status(qn),
+		    (1u << SCD_STTS_ACTIVE) | (fifo << SCD_STTS_TXF) |
+		    (1u << SCD_STTS_WSL) | SCD_STTS_MSK);
+	if (cmdq && S->fam->scd_set_active)
+		iwl_wr_prph(SCD_EN_CTRL, 1u << qn);
+	q->active = 1;
+}
+
+void iwl_txq_enable_fifo(int qn, unsigned int fifo)
+{
+	if (S && qn >= 0 && qn < IWL_NUM_Q && S->q[qn].tfds)
+		scd_enable_queue((unsigned int)qn, fifo);
+}
+
 void iwl_fw_alive(unsigned int scd_base)
 void iwl_fw_alive(unsigned int scd_base)
 {
 {
-	struct txq *q = &S->q[IWL_Q_CMD];
 	unsigned int c;
 	unsigned int c;
 
 
 	S->scd_base = iwl_rd_prph(SCD_SRAM_BASE_ADDR);
 	S->scd_base = iwl_rd_prph(SCD_SRAM_BASE_ADDR);
 	if (scd_base && scd_base != S->scd_base)
 	if (scd_base && scd_base != S->scd_base)
 		iwl_loghex("scd base mismatch, alive says", scd_base);
 		iwl_loghex("scd base mismatch, alive says", scd_base);
 	mem_zero(S->scd_base + SCD_CONTEXT_MEM,
 	mem_zero(S->scd_base + SCD_CONTEXT_MEM,
-		 (((SCD_TRANS_TBL_MEM + IWL_NUM_Q * 2) & 0xfffcu) -
+		 (((SCD_TRANS_TBL_MEM + S->fam->nq * 2) & 0xfffcu) -
 		  SCD_CONTEXT_MEM) / 4);
 		  SCD_CONTEXT_MEM) / 4);
 	iwl_wr_prph(SCD_DRAM_BASE_ADDR, (unsigned int)S->bc >> 10);
 	iwl_wr_prph(SCD_DRAM_BASE_ADDR, (unsigned int)S->bc >> 10);
+	/* The SCD chain extension misbehaves on the 6000 family. */
+	if (S->fam->chain_ext_wa)
+		iwl_wr_prph(SCD_CHAINEXT_EN, 0);
 
 
-	/* iwl_trans_ac_txq_enable(cmd queue, FIFO 7). */
-	iwl_wr_prph(SCD_EN_CTRL, 0);
-	iwl_wr_prph(scd_queue_status(IWL_Q_CMD), 1u << SCD_STTS_ACT_EN);
-	prph_clr(SCD_AGGR_SEL, 1u << IWL_Q_CMD);
-	q->wptr = q->rptr = 0;
-	iwl_wr(HBUS_TARG_WRPTR, 0 | (IWL_Q_CMD << 8));
-	iwl_wr_prph(scd_queue_rdptr(IWL_Q_CMD), 0);
-	mem_wr32(S->scd_base + SCD_CONTEXT_MEM + IWL_Q_CMD * 8, 0);
-	mem_wr32(S->scd_base + SCD_CONTEXT_MEM + IWL_Q_CMD * 8 + 4,
-		 (64u & 0x7f) | ((64u << 16) & 0x7f0000u));
-	iwl_wr_prph(scd_queue_status(IWL_Q_CMD),
-		    (1u << SCD_STTS_ACTIVE) | (7u << SCD_STTS_TXF) |
-		    (1u << SCD_STTS_WSL) | SCD_STTS_MSK);
-	iwl_wr_prph(SCD_EN_CTRL, 1u << IWL_Q_CMD);
-	q->active = 1;
+	scd_enable_queue(S->fam->cmd_q, S->fam->cmd_fifo);
 
 
 	iwl_wr_prph(SCD_TXFACT, 0xffu);                 /* FIFOs 0..7 */
 	iwl_wr_prph(SCD_TXFACT, 0xffu);                 /* FIFOs 0..7 */
 	for (c = 0; c < 8; c++)
 	for (c = 0; c < 8; c++)
@@ -915,8 +1111,12 @@ static void bc_set(int qn, unsigned int idx, unsigned int bytes,
 	unsigned short *t = S->bc + qn * BC_ENTRIES;
 	unsigned short *t = S->bc + qn * BC_ENTRIES;
 	unsigned short v;
 	unsigned short v;
 
 
-	/* + CRC + delimiter, in dwords (bc_table_dword on 7000). */
-	v = (unsigned short)((((bytes + 8) + 3) / 4) | (sta << 12));
+	/* + CRC + delimiter; in dwords with bc_table_dword (7000), else
+	 * bytes (iwl_txq_gen1_update_byte_cnt_tbl()). */
+	bytes += 8;
+	if (S->fam->bc_dword)
+		bytes = (bytes + 3) / 4;
+	v = (unsigned short)(bytes | (sta << 12));
 	t[idx] = v;
 	t[idx] = v;
 	if (idx < BC_ENTRIES - TFD_RING)
 	if (idx < BC_ENTRIES - TFD_RING)
 		t[TFD_RING + idx] = v;
 		t[TFD_RING + idx] = v;
@@ -943,12 +1143,14 @@ static void ring_doorbell(int qn)
 static int enqueue_cmd(unsigned int id, const void *data, unsigned int len,
 static int enqueue_cmd(unsigned int id, const void *data, unsigned int len,
 		       unsigned short *seq_out)
 		       unsigned short *seq_out)
 {
 {
-	struct txq *q = &S->q[IWL_Q_CMD];
+	unsigned int cq = S->fam->cmd_q;
+	struct txq *q = &S->q[cq];
 	/* mvm sets wide_cmd_header: legacy (group 0) commands are sent as
 	/* mvm sets wide_cmd_header: legacy (group 0) commands are sent as
-	 * group 1 with the 8-byte header (iwl_trans_send_cmd(), DEF_ID). */
+	 * group 1 with the 8-byte header (iwl_trans_send_cmd(), DEF_ID).
+	 * DVM uses the plain 4-byte iwl_cmd_header. */
 	unsigned char group = (id >> 8) & 0xff ? (id >> 8) & 0xff : 1;
 	unsigned char group = (id >> 8) & 0xff ? (id >> 8) & 0xff : 1;
 	unsigned char *buf, *tfd;
 	unsigned char *buf, *tfd;
-	unsigned int hdr = 8, total = hdr + len, idx;
+	unsigned int hdr = S->fam->wide_hdr ? 8 : 4, total = hdr + len, idx;
 	unsigned short seq;
 	unsigned short seq;
 
 
 	if (!q->active || total > CMD_BUF)
 	if (!q->active || total > CMD_BUF)
@@ -960,15 +1162,17 @@ static int enqueue_cmd(unsigned int id, const void *data, unsigned int len,
 	}
 	}
 	idx = q->wptr;
 	idx = q->wptr;
 	buf = slot_buf(q, idx);
 	buf = slot_buf(q, idx);
-	seq = (unsigned short)((IWL_Q_CMD << 8) | idx);
+	seq = (unsigned short)((cq << 8) | idx);
 	buf[0] = id & 0xff;
 	buf[0] = id & 0xff;
-	buf[1] = group;
+	buf[1] = S->fam->wide_hdr ? group : 0;
 	buf[2] = seq & 0xff;
 	buf[2] = seq & 0xff;
 	buf[3] = seq >> 8;
 	buf[3] = seq >> 8;
-	buf[4] = len & 0xff;
-	buf[5] = len >> 8;
-	buf[6] = 0;
-	buf[7] = (id >> 16) & 0xff;
+	if (S->fam->wide_hdr) {
+		buf[4] = len & 0xff;
+		buf[5] = len >> 8;
+		buf[6] = 0;
+		buf[7] = (id >> 16) & 0xff;
+	}
 	iwl_memcpy(buf + hdr, data, len);
 	iwl_memcpy(buf + hdr, data, len);
 	tfd = q->tfds + idx * TFD_SIZE;
 	tfd = q->tfds + idx * TFD_SIZE;
 	iwl_memzero(tfd, TFD_SIZE);
 	iwl_memzero(tfd, TFD_SIZE);
@@ -979,7 +1183,7 @@ static int enqueue_cmd(unsigned int id, const void *data, unsigned int len,
 		tfd_set(tfd, 1, (unsigned int)buf + FIRST_TB, total - FIRST_TB);
 		tfd_set(tfd, 1, (unsigned int)buf + FIRST_TB, total - FIRST_TB);
 	}
 	}
 	*seq_out = seq;
 	*seq_out = seq;
-	ring_doorbell(IWL_Q_CMD);
+	ring_doorbell(cq);
 	return 0;
 	return 0;
 }
 }
 
 
@@ -1152,7 +1356,7 @@ int iwl_send_cmd(unsigned int id, const void *data, unsigned int len,
 			iwl_udelay(100);
 			iwl_udelay(100);
 	}
 	}
 	S->waiting = 0;
 	S->waiting = 0;
-	S->q[IWL_Q_CMD].rptr = S->q[IWL_Q_CMD].wptr;
+	S->q[S->fam->cmd_q].rptr = S->q[S->fam->cmd_q].wptr;
 	if (!S->resp_done) {
 	if (!S->resp_done) {
 		iwl_loghex("command timeout, id", id);
 		iwl_loghex("command timeout, id", id);
 		return -1;
 		return -1;

+ 22 - 1
iwl_trans.h

@@ -1,4 +1,5 @@
-/* Intel 7265D/3165 (iwlwifi "7000" family) PCIe transport.
+/* Intel iwlwifi PCIe transport: the 7000 family (7265D/3165, MVM firmware)
+ * and the 6000 family (6200/6300, DVM firmware).
  *
  *
  * Polled, single RX queue, gen1 TFD TX queues.  Every ring and buffer comes
  * Polled, single RX queue, gen1 TFD TX queues.  Every ring and buffer comes
  * from the page allocator (nothing big in BSS: the kernel image must stay
  * from the page allocator (nothing big in BSS: the kernel image must stay
@@ -13,6 +14,19 @@
 #define IWL_Q_BSS   4     /* AP station: management + data */
 #define IWL_Q_BSS   4     /* AP station: management + data */
 #define IWL_NUM_Q   31
 #define IWL_NUM_Q   31
 
 
+/* DVM (6000 family) queues: the four AC queues in Linux's default
+ * iwlagn_default_queue_to_tx_fifo order, command queue on 4. */
+#define IWL_DVM_Q_VO   0   /* FIFO 3: management frames */
+#define IWL_DVM_Q_VI   1   /* FIFO 2 */
+#define IWL_DVM_Q_BE   2   /* FIFO 1: non-QoS data */
+#define IWL_DVM_Q_BK   3   /* FIFO 0 */
+#define IWL_DVM_Q_CMD  4   /* IWL_DEFAULT_CMD_QUEUE_NUM, FIFO 7 */
+
+/* Device sub-families (iwl_subfamily()). */
+#define IWL_SUB_7265   1   /* 7265D / 3165: MVM firmware */
+#define IWL_SUB_6000I  2   /* 6200 (6000i): DVM, antennas B+C */
+#define IWL_SUB_6000   3   /* 6300: DVM, antennas A+B+C */
+
 /* Firmware images in the .ucode file. */
 /* Firmware images in the .ucode file. */
 #define IWL_IMG_RT    0
 #define IWL_IMG_RT    0
 #define IWL_IMG_INIT  1
 #define IWL_IMG_INIT  1
@@ -57,6 +71,13 @@ int  iwl_start_hw(void);               /* prepare, reset, APM init */
 int  iwl_load_fw(int img);             /* rings + sections + release reset */
 int  iwl_load_fw(int img);             /* rings + sections + release reset */
 void iwl_fw_alive(unsigned int scd_base); /* SCD setup + cmd queue enable */
 void iwl_fw_alive(unsigned int scd_base); /* SCD setup + cmd queue enable */
 void iwl_stop_device(void);            /* stop DMA, APM stop (kexec, retry) */
 void iwl_stop_device(void);            /* stop DMA, APM stop (kexec, retry) */
+int  iwl_is_dvm(void);                 /* 6000 family (DVM firmware) */
+int  iwl_fatal(void);                  /* firmware crashed (SW/HW error) */
+unsigned int iwl_subfamily(void);      /* IWL_SUB_* */
+/* DVM: radio SKU / config bits written during NIC init, before rings. */
+void iwl_set_nic_config(void (*fn)(void));
+/* DVM AC queue: scheduler programmed by the transport (FIFO, window). */
+void iwl_txq_enable_fifo(int q, unsigned int fifo);
 int  iwl_rfkill(void);                 /* 1 = hardware RF-kill asserted */
 int  iwl_rfkill(void);                 /* 1 = hardware RF-kill asserted */
 
 
 /* --- firmware I/O ------------------------------------------------------- */
 /* --- firmware I/O ------------------------------------------------------- */

+ 8 - 2
js_browser.c

@@ -16,8 +16,10 @@
 
 
 /* Wall-clock budget per script/handler/timer entry, in milliseconds.  It is
 /* Wall-clock budget per script/handler/timer entry, in milliseconds.  It is
  * reset before every entry; a runaway loop becomes a catchable
  * reset before every entry; a runaway loop becomes a catchable
- * "interrupted" exception instead of a frozen browser. */
-#define JS_RUN_LIMIT 10000
+ * "interrupted" exception instead of a frozen browser.  A framework
+ * bootstrap (AngularJS rendering ~300 cards) needs several seconds on a
+ * slow CPU, so the budget is generous. */
+#define JS_RUN_LIMIT 20000
 #define JS_TIMERS 16
 #define JS_TIMERS 16
 
 
 struct js_timer {
 struct js_timer {
@@ -2956,6 +2958,10 @@ static void report_top(js_State *state)
 {
 {
 	const char *msg;
 	const char *msg;
 
 
+	/* An "interrupted" error leaves the deadline expired: formatting the
+	 * error runs JavaScript (toString), which would be interrupted too. */
+	js_setlimit(state, 0, 0);
+
 	js_getproperty(state, -1, "stack");
 	js_getproperty(state, -1, "stack");
 	if (js_isstring(state, -1)) {
 	if (js_isstring(state, -1)) {
 		/* QuickJS's stack is only the frames: lead with
 		/* QuickJS's stack is only the frames: lead with

+ 75 - 30
kernel.c

@@ -22,6 +22,7 @@
 #include "process.h"
 #include "process.h"
 #include "ramfs.h"
 #include "ramfs.h"
 #include "rndis.h"
 #include "rndis.h"
+#include "rtl8821au.h"
 #include "sched.h"
 #include "sched.h"
 #include "thermal.h"
 #include "thermal.h"
 #include "timer.h"
 #include "timer.h"
@@ -367,11 +368,54 @@ static int net_restore_boot_config(void)
 	return dns[0] || dns[1] || dns[2] || dns[3];
 	return dns[0] || dns[1] || dns[2] || dns[3];
 }
 }
 
 
+/* Saved WiFi credentials on the writable /data volume: let the normal
+ * supplicant reconnect in the background.  It leaves a wired link that is
+ * physically up alone, and a missing /data or wifi helper is harmless. */
+static void boot_wifi_autoconnect(void)
+{
+	unsigned int sz = 0;
+	int is_dir = 0;
+
+	if (vfs_stat(WIFI_CRED_PATH, &sz, &is_dir) == 0 &&
+	    !is_dir && sz > 0) {
+		struct vfs_file f;
+		struct vfs_mount *m = 0;
+
+		if (vfs_load_file("/ramfs/bin/wifi", &f, &m) == 0) {
+			if (process_create_user("wifi", f.data, f.size,
+						"autoconnect", "/") > 0)
+				serial_puts(SERIAL_COM1,
+					    "wifi: autoconnect started\n");
+			vfs_release_file(m, &f);
+		}
+	}
+}
+
+/* Boot-time storage scan in its own kernel thread.  A process (unlike the
+ * idle context kernel_main runs in) may sleep, so the probes' long waits
+ * (floppy motor, ATA/AHCI timeouts) go through vfs_scan_relax() and the WM
+ * and the first terminal run meanwhile.  Disk file operations wait for
+ * vfs_scan_busy; /ramfs and /share do not.  The WiFi autoconnect needs
+ * /data, so it runs here after the mount. */
+static volatile int boot_dhcp_done;
+
+static void boot_storage_main(void)
+{
+	vfs_mount_all();
+	boot_trace("storage");
+	/* As before, WiFi only starts after the boot DHCP attempt on the
+	 * wired NIC, so two DHCP clients never overlap. */
+	while (!boot_dhcp_done)
+		process_sleep_ms(20);
+	boot_wifi_autoconnect();
+	boot_trace("wifi auto");
+}
+
 void kernel_main(void);
 void kernel_main(void);
 void kernel_main(void)
 void kernel_main(void)
 {
 {
 	unsigned char *p = __bss_start;
 	unsigned char *p = __bss_start;
-	int net_kept;
+	int net_kept, storage_inline;
 	while (p < __bss_end)
 	while (p < __bss_end)
 		*p++ = 0;
 		*p++ = 0;
 
 
@@ -461,6 +505,7 @@ void kernel_main(void)
 	 * watcher in the WM loop. */
 	 * watcher in the WM loop. */
 	asix_register();
 	asix_register();
 	usbmsc_register();
 	usbmsc_register();
+	rtl8821au_register();
 	rndis_register();
 	rndis_register();
 	usbhid_init();
 	usbhid_init();
 	boot_trace("usb");
 	boot_trace("usb");
@@ -517,6 +562,10 @@ void kernel_main(void)
 		}
 		}
 		(void)ok;
 		(void)ok;
 		vfs_mount_ramfs();
 		vfs_mount_ramfs();
+		/* /share too: the WM and the first terminal may use it while the
+		 * bootscan thread is still probing the disks, and a path that
+		 * does not resolve yet would wait for the whole scan. */
+		vfs_mount_share();
 	}
 	}
 	boot_trace("ramfs");
 	boot_trace("ramfs");
 	boot_val("ramfs files", ramfs_count());
 	boot_val("ramfs files", ramfs_count());
@@ -553,14 +602,28 @@ void kernel_main(void)
 	}
 	}
 	boot_trace("kworker");
 	boot_trace("kworker");
 
 
-	/* Slow storage bring-up happens behind the usable desktop now. */
+	/* Slow storage bring-up happens behind the usable desktop: publish the
+	 * scan (disk paths wait for it) before the thread even runs, so no
+	 * command can see the half-built boot mount table.  Without a thread
+	 * slot, scan inline as before. */
 	boot_trace("storage begin");
 	boot_trace("storage begin");
-	vfs_mount_all();
-	boot_trace("storage");
+	storage_inline = 0;
+	{
+		int pid = process_create("bootscan", boot_storage_main);
+
+		if (pid > 0) {
+			vfs_scan_owner_pid = (unsigned int)pid;
+			vfs_scan_busy = 1;
+		} else {
+			vfs_mount_all();
+			boot_trace("storage");
+			storage_inline = 1;
+		}
+	}
 
 
-	/* The WM spawns the first terminal once it sees boot_ready: while the
-	 * boot scan was rebuilding the mount table, no user command could touch
-	 * the disks.  The desktop itself was already painted from /ramfs. */
+	/* The WM spawns the first terminal once it sees boot_ready.  The
+	 * terminal and every /ramfs command load without waiting for the
+	 * storage scan; a disk path waits for it (vfs_scan_busy). */
 #ifndef NOWM
 #ifndef NOWM
 	wm_server_boot_ready();
 	wm_server_boot_ready();
 	boot_trace("terminal ready");
 	boot_trace("terminal ready");
@@ -582,30 +645,12 @@ void kernel_main(void)
 		boot_trace(net_kept ? "dhcp skipped" :
 		boot_trace(net_kept ? "dhcp skipped" :
 			   (linked ? "dhcp" : "dhcp no link"));
 			   (linked ? "dhcp" : "dhcp no link"));
 	}
 	}
-
-	/* If the writable /data volume has saved WiFi credentials, let the
-	 * normal supplicant reconnect in the background.  It leaves a wired
-	 * link that is physically up alone, and a missing /data or wifi
-	 * helper is harmless. */
-	{
-		unsigned int sz = 0;
-		int is_dir = 0;
-
-		if (vfs_stat(WIFI_CRED_PATH, &sz, &is_dir) == 0 &&
-		    !is_dir && sz > 0) {
-			struct vfs_file f;
-			struct vfs_mount *m = 0;
-
-			if (vfs_load_file("/ramfs/bin/wifi", &f, &m) == 0) {
-				if (process_create_user("wifi", f.data, f.size,
-							"autoconnect", "/") > 0)
-					serial_puts(SERIAL_COM1,
-						    "wifi: autoconnect started\n");
-				vfs_release_file(m, &f);
-			}
-		}
+	boot_dhcp_done = 1;
+	if (storage_inline) {
+		boot_wifi_autoconnect();
+		boot_trace("wifi auto");
 	}
 	}
-	boot_trace("wifi auto");
+
 
 
 	/* Dump the active NIC's counters after the boot DHCP attempt.  With
 	/* Dump the active NIC's counters after the boot DHCP attempt.  With
 	 * the ASIX as the management link this appears in netmon even when
 	 * the ASIX as the management link this appears in netmon even when

+ 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 "rndis.h"
 #include "tcp.h"
 #include "tcp.h"
 #include "wifi.h"
 #include "wifi.h"
+#include "wifi_fw.h"
 static const struct netdev *const netdev_drivers[] = {
 static const struct netdev *const netdev_drivers[] = {
 	/* e1000e is the active test NIC; the ASIX is kept as the management
 	/* e1000e is the active test NIC; the ASIX is kept as the management
 	 * link by netdev_mgmt() even while e1000e is active. */
 	 * link by netdev_mgmt() even while e1000e is active. */
@@ -219,13 +220,23 @@ int netdev_select(const char *name)
 	return -1;
 	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)
 static void print_netdev(const char *label, const struct netdev *nd)
 {
 {
 	io_putstr("netdev: ");
 	io_putstr("netdev: ");
 	io_putstr(label);
 	io_putstr(label);
 	io_putstr(nd->name);
 	io_putstr(nd->name);
 	io_putstr(" (");
 	io_putstr(" (");
-	io_putstr(nd->chip);
+	io_putstr(netdev_chip(nd));
 	io_putstr(")\n");
 	io_putstr(")\n");
 }
 }
 
 

+ 4 - 0
netdev.h

@@ -82,6 +82,10 @@ int  netdev_in_use(void);
 unsigned int netdev_owner(void);
 unsigned int netdev_owner(void);
 void netdev_release_all(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. */
 /* Fill the active NIC's MAC; -1 (and a zero MAC) when there is no NIC. */
 int netdev_get_mac(unsigned char mac[6]);
 int netdev_get_mac(unsigned char mac[6]);
 
 

+ 2 - 3
ohci.c

@@ -10,6 +10,7 @@
    Up to four controller instances (MCP79 exposes four of them); each PCI
    Up to four controller instances (MCP79 exposes four of them); each PCI
    function is one root hub with its own device address space. */
    function is one root hub with its own device address space. */
 
 
+#include "usb.h"
 #include "ohci.h"
 #include "ohci.h"
 #include "usb_hcd.h"
 #include "usb_hcd.h"
 #include "pci.h"
 #include "pci.h"
@@ -168,9 +169,7 @@ static void wr(struct ohci_ctrl *c, unsigned int ofs, unsigned int val)
 
 
 static void delay_ms(unsigned int ms)
 static void delay_ms(unsigned int ms)
 {
 {
-	unsigned int t0 = get_ticks_ms();
-	while (get_ticks_ms() - t0 < ms)
-		;
+	usb_delay_ms(ms);    /* sleeps inside a sleepable scan */
 }
 }
 
 
 static unsigned int port_reg(unsigned int port)
 static unsigned int port_reg(unsigned int port)

+ 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

+ 3 - 1
sdhci.c

@@ -15,6 +15,7 @@
  * the card.
  * the card.
  */
  */
 #include "sdhci.h"
 #include "sdhci.h"
+#include "vfs.h"
 #include "paging.h"
 #include "paging.h"
 #include "pci.h"
 #include "pci.h"
 #include "process.h"
 #include "process.h"
@@ -145,7 +146,7 @@ static void tiny_delay(unsigned int ms)
 	unsigned int t0 = get_ticks_ms();
 	unsigned int t0 = get_ticks_ms();
 
 
 	while (get_ticks_ms() - t0 < ms)
 	while (get_ticks_ms() - t0 < ms)
-		;
+		vfs_scan_poll(t0);
 }
 }
 
 
 static int wait_state(unsigned int mask, int set, unsigned int ms)
 static int wait_state(unsigned int mask, int set, unsigned int ms)
@@ -159,6 +160,7 @@ static int wait_state(unsigned int mask, int set, unsigned int ms)
 			return 0;
 			return 0;
 		if (get_ticks_ms() - t0 > ms)
 		if (get_ticks_ms() - t0 > ms)
 			return -1;
 			return -1;
+		vfs_scan_poll(t0);
 	}
 	}
 }
 }
 
 

+ 39 - 1
sys_fs.c

@@ -49,6 +49,9 @@ static void fs_scan_wait(void)
 	}
 	}
 }
 }
 
 
+static int fs_resolve(const char *path, struct vfs_mount **m,
+		      char *rel, unsigned int rel_size);
+
 /* The disk may need its IRQ (floppy), and the FAT driver's static state must
 /* The disk may need its IRQ (floppy), and the FAT driver's static state must
  * not be preempted in the middle of an operation. */
  * not be preempted in the middle of an operation. */
 static void fs_preempt_enter(void)
 static void fs_preempt_enter(void)
@@ -67,6 +70,35 @@ void sys_fs_enter(void)
 	fs_preempt_enter();
 	fs_preempt_enter();
 }
 }
 
 
+/* sys_fs_enter() for an ELF load: a RAM-backed path (the usual
+ * /ramfs/bin/<cmd>) neither waits for a rescan nor for a format, so the WM
+ * and terminals can launch commands while `storage` probes the disks. */
+void sys_fs_enter_path(const char *path)
+{
+	struct vfs_mount *m;
+	char rel[VFS_PATH_MAX];
+
+	if (!(path && fs_resolve(path, &m, rel, sizeof(rel)) == 0 &&
+	      vfs_mount_is_ram(m))) {
+		fs_format_wait();
+		fs_scan_wait();
+	}
+	fs_preempt_enter();
+}
+
+/* The `storage` rescan: wait for a format or another scan, but do NOT
+ * disable preemption.  The scan publishes vfs_scan_busy (disk file
+ * operations wait for it) and its probes sleep through vfs_scan_relax(),
+ * so the rest of the system keeps running while the disks are probed. */
+void sys_fs_scan(void)
+{
+	fs_format_wait();
+	fs_scan_wait();
+	__asm__ volatile ("sti");
+	vfs_mount_all();
+	__asm__ volatile ("cli");
+}
+
 void sys_fs_exit(void)
 void sys_fs_exit(void)
 {
 {
 	__asm__ volatile ("cli");
 	__asm__ volatile ("cli");
@@ -115,8 +147,14 @@ static int sys_fs_begin(const char *path, struct vfs_mount **m,
 {
 {
 	int r;
 	int r;
 
 
-	fs_scan_wait();
+	/* /ramfs and /share are re-added first by a rescan and stay valid
+	 * while it sleeps: only disk paths (and paths that do not resolve
+	 * yet, which may be a disk being re-mounted) wait for the scan. */
 	r = fs_resolve(path, m, rel, rel_size);
 	r = fs_resolve(path, m, rel, rel_size);
+	if (vfs_scan_busy && (r != 0 || !vfs_mount_is_ram(*m))) {
+		fs_scan_wait();
+		r = fs_resolve(path, m, rel, rel_size);
+	}
 	if (r != 0)
 	if (r != 0)
 		return r;
 		return r;
 	if (vfs_mount_is_disk(*m)) {
 	if (vfs_mount_is_disk(*m)) {

+ 2 - 0
sys_fs.h

@@ -7,6 +7,8 @@
 /* Enter/leave the fs non-preemptible section (used by other disk-touching
 /* Enter/leave the fs non-preemptible section (used by other disk-touching
  * services, e.g. the storage info refresh).  See sys_fs.c. */
  * services, e.g. the storage info refresh).  See sys_fs.c. */
 void sys_fs_enter(void);
 void sys_fs_enter(void);
+void sys_fs_enter_path(const char *path);
+void sys_fs_scan(void);
 void sys_fs_exit(void);
 void sys_fs_exit(void);
 
 
 int sys_read_file(const char *path, void *buf, unsigned int max);
 int sys_read_file(const char *path, void *buf, unsigned int max);

+ 1 - 3
sys_info.c

@@ -102,9 +102,7 @@ int sys_mouseinfo(struct sys_mouseinfo *out)
 
 
 int sys_storage_info(struct sys_storage_info *out)
 int sys_storage_info(struct sys_storage_info *out)
 {
 {
-	sys_fs_enter();                 /* vfs_mount_all() reads disks */
-	vfs_mount_all();
-	sys_fs_exit();
+	sys_fs_scan();                  /* rescan; sleeps in the probes */
 
 
 	out->ahci_present = ahci_controller_present() ? 1 : 0;
 	out->ahci_present = ahci_controller_present() ? 1 : 0;
 	out->blkdev_count = blk_dev_count;
 	out->blkdev_count = blk_dev_count;

+ 7 - 3
sys_net.c

@@ -132,8 +132,12 @@ int sys_net_info(struct sys_netinfo *out)
 	net_get_dns(out->dns);
 	net_get_dns(out->dns);
 	for (i = 0; nd->name[i] && i + 1 < sizeof(out->name); i++)
 	for (i = 0; nd->name[i] && i + 1 < sizeof(out->name); i++)
 		out->name[i] = nd->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);
 	out->link = (unsigned int)netdev_link(nd);
 	return 1;
 	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++)
 		for (j = 0; j < sizeof(out[n]); j++)
 			((unsigned char *)&out[n])[j] = 0;
 			((unsigned char *)&out[n])[j] = 0;
 		nd_copy_name(out[n].name, sizeof(out[n].name), nd->name);
 		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].kind = nd_kind(nd);
 		out[n].active = (nd == act);
 		out[n].active = (nd == act);
 		out[n].link = (unsigned int)(nd == act ? netdev_link(nd) :
 		out[n].link = (unsigned int)(nd == act ? netdev_link(nd) :

+ 21 - 0
sys_wm.c

@@ -6,6 +6,8 @@
 #include "paging.h"
 #include "paging.h"
 #include "process.h"
 #include "process.h"
 #include "wm.h"
 #include "wm.h"
+#include "serial.h"
+#include "timer.h"
 
 
 static int user_ok(const void *p, unsigned int size, int write)
 static int user_ok(const void *p, unsigned int size, int write)
 {
 {
@@ -164,10 +166,29 @@ int sys_win_create(const struct win_create *in, struct win_info *out)
 	return r;
 	return r;
 }
 }
 
 
+/* One serial line when the first application window (normally the boot
+ * terminal) presents a frame: the end point of the boot-time measurement
+ * ("[boot] ..." lines are relative to the PIT start, this is uptime). */
+static void boot_first_present(void)
+{
+	static int done;
+
+	if (done)
+		return;
+	done = 1;
+	serial_puts(SERIAL_COM1, "wm: first window frame (");
+	serial_puts(SERIAL_COM1, current_proc()->name);
+	serial_puts(SERIAL_COM1, ") at ");
+	serial_putdec(SERIAL_COM1, get_ticks_ms());
+	serial_puts(SERIAL_COM1, " ms\n");
+}
+
 int sys_win_present(const struct wm_win_present *in)
 int sys_win_present(const struct wm_win_present *in)
 {
 {
 	struct wm_win_present p;
 	struct wm_win_present p;
 
 
+	boot_first_present();
+
 	if (!in)
 	if (!in)
 		return wm_server_win_present(0);
 		return wm_server_win_present(0);
 	if (!user_ok(in, sizeof(p), 0))
 	if (!user_ok(in, sizeof(p), 0))

+ 1 - 1
syscall.c

@@ -377,7 +377,7 @@ static int syscall_dispatch_locked(int num, int a1, int a2, int a3)
 		/* A FAT load reads the floppy through IRQ 6, so open the
 		/* A FAT load reads the floppy through IRQ 6, so open the
 		 * syscall's interrupts-off section for the duration (the same
 		 * syscall's interrupts-off section for the duration (the same
 		 * window SYS_READ_FILE uses). */
 		 * window SYS_READ_FILE uses). */
-		sys_fs_enter();
+		sys_fs_enter_path(path);
 		lr = vfs_load_file(path, &f, &m);
 		lr = vfs_load_file(path, &f, &m);
 		sys_fs_exit();
 		sys_fs_exit();
 		if (lr != 0)
 		if (lr != 0)

+ 1 - 1
syscall.h

@@ -163,7 +163,7 @@
 /* Current process id (used for per-process temporary paths). */
 /* Current process id (used for per-process temporary paths). */
 #define SYS_GETPID        108  /* () -> pid */
 #define SYS_GETPID        108  /* () -> pid */
 
 
-/* Intel 7265D/3165 WiFi (wifi.c).  One multiplexed call: (op, a2, a3). */
+/* Intel 7265D/3165 and 6200/6300 WiFi (wifi.c).  One multiplexed call: (op, a2, a3). */
 #define SYS_WIFI          110
 #define SYS_WIFI          110
 #define SYS_WIFI_UP          0  /* () -> 0 / -1: load firmware, read NVM */
 #define SYS_WIFI_UP          0  /* () -> 0 / -1: load firmware, read NVM */
 #define SYS_WIFI_STATUS      1  /* (struct sys_wifi_status *out) -> 0/-1 */
 #define SYS_WIFI_STATUS      1  /* (struct sys_wifi_status *out) -> 0/-1 */

+ 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
+};

+ 2 - 2
ucmd_wifi.c

@@ -1,4 +1,4 @@
-/* wifi: ring-3 WiFi command for the Intel 7265D/3165 (see wifi.c).
+/* wifi: ring-3 WiFi command for the Intel 7265D/3165 and 6200/6300 (see wifi.c).
  *
  *
  *   wifi                         status
  *   wifi                         status
  *   wifi up                      load the firmware, show the MAC address
  *   wifi up                      load the firmware, show the MAC address
@@ -207,7 +207,7 @@ static int cmd_status(void)
 		return 1;
 		return 1;
 	}
 	}
 	if (!st.present) {
 	if (!st.present) {
-		u_puts("wifi: no Intel 7265/3165 card found\n");
+		u_puts("wifi: no Intel 7265/3165/6200/6300 card found\n");
 		return 1;
 		return 1;
 	}
 	}
 	u_puts("chip:     "); u_puts(st.chip);
 	u_puts("chip:     "); u_puts(st.chip);

+ 2 - 3
uhci.c

@@ -8,6 +8,7 @@
    fixed low-memory address); among several PCI functions we pick the one
    fixed low-memory address); among several PCI functions we pick the one
    whose root port has a device attached. */
    whose root port has a device attached. */
 
 
+#include "usb.h"
 #include "pci.h"
 #include "pci.h"
 #include "serial.h"
 #include "serial.h"
 #include "timer.h"
 #include "timer.h"
@@ -120,9 +121,7 @@ static void reg_writel(unsigned int reg, unsigned int val)
 
 
 static void delay_ms(unsigned int ms)
 static void delay_ms(unsigned int ms)
 {
 {
-	unsigned int t0 = get_ticks_ms();
-	while (get_ticks_ms() - t0 < ms)
-		;
+	usb_delay_ms(ms);    /* sleeps inside a sleepable scan */
 }
 }
 
 
 static void td_set(struct uhci_td *td, unsigned int link, unsigned int status,
 static void td_set(struct uhci_td *td, unsigned int link, unsigned int status,

+ 56 - 14
usb.c

@@ -3,6 +3,8 @@
    usb_hcd_ops vtable (uhci.c / ohci.c / ehci.c / xhci.c). */
    usb_hcd_ops vtable (uhci.c / ohci.c / ehci.c / xhci.c). */
 
 
 #include "io.h"
 #include "io.h"
+#include "process.h"
+#include "sched.h"
 #include "timer.h"
 #include "timer.h"
 #include "usb.h"
 #include "usb.h"
 #include "usb_hcd.h"
 #include "usb_hcd.h"
@@ -105,11 +107,48 @@ static unsigned int generation;
 static unsigned int next_addr[USB_MAX_CTRL];
 static unsigned int next_addr[USB_MAX_CTRL];
 static const struct usb_hcd_ops *slot_ops[USB_HCD_INT_SLOTS];
 static const struct usb_hcd_ops *slot_ops[USB_HCD_INT_SLOTS];
 static usb_probe_fn current_probe;
 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 usb_device_probe_fn device_probes[USB_MAX_DEVICE_PROBES];
 static unsigned int n_device_probes;
 static unsigned int n_device_probes;
 static int print_devices;
 static int print_devices;
 static int scan_busy;
 static int scan_busy;
+static unsigned int scan_owner_pid;     /* pid that set scan_busy */
+
+static unsigned int cur_pid(void)
+{
+	struct process *p = current_proc();
+
+	return p ? p->pid : 0;
+}
+
+static void scan_begin(void)
+{
+	scan_busy = 1;
+	scan_owner_pid = cur_pid();
+}
+
+int usb_scan_foreign(void)
+{
+	return scan_busy && cur_pid() != scan_owner_pid;
+}
+
+void usb_delay_ms(unsigned int ms)
+{
+	unsigned int t0, flags;
+
+	__asm__ volatile ("pushfl; popl %0" : "=r"(flags));
+	/* Only the scan owner sleeps, never the idle/boot context (pid 0),
+	 * a non-preemptible section or an interrupts-off syscall (SYS_WIFI
+	 * runs with IF clear). */
+	if (scan_busy && scan_owner_pid && cur_pid() == scan_owner_pid &&
+	    !this_cpu()->preempt_count && (flags & 0x200u)) {
+		process_sleep_ms(ms ? ms : 1);
+		return;
+	}
+	t0 = get_ticks_ms();
+	while (get_ticks_ms() - t0 < ms)
+		;
+}
 
 
 static unsigned char dev_desc[18];
 static unsigned char dev_desc[18];
 static unsigned char desc8[64];
 static unsigned char desc8[64];
@@ -145,9 +184,7 @@ static void put_hex(unsigned int n, unsigned int digits)
 
 
 static void msleep(unsigned int ms)
 static void msleep(unsigned int ms)
 {
 {
-	unsigned int t0 = get_ticks_ms();
-	while (get_ticks_ms() - t0 < ms)
-		;
+	usb_delay_ms(ms);
 }
 }
 
 
 static void indent(unsigned int depth)
 static void indent(unsigned int depth)
@@ -196,7 +233,7 @@ int usb_control_in(unsigned int ctrl, unsigned int addr, unsigned int mps,
 	const struct usb_hcd_ops *ops;
 	const struct usb_hcd_ops *ops;
 	unsigned char setup[8];
 	unsigned char setup[8];
 
 
-	if (ctrl >= n_usb_ctrls)
+	if (ctrl >= n_usb_ctrls || usb_scan_foreign())
 		return -1;
 		return -1;
 	ops = usb_ctrls[ctrl].ops;
 	ops = usb_ctrls[ctrl].ops;
 	setup[0] = rt;
 	setup[0] = rt;
@@ -218,7 +255,7 @@ int usb_control_out(unsigned int ctrl, unsigned int addr, unsigned int mps,
 	const struct usb_hcd_ops *ops;
 	const struct usb_hcd_ops *ops;
 	unsigned char setup[8];
 	unsigned char setup[8];
 
 
-	if (ctrl >= n_usb_ctrls)
+	if (ctrl >= n_usb_ctrls || usb_scan_foreign())
 		return -1;
 		return -1;
 	ops = usb_ctrls[ctrl].ops;
 	ops = usb_ctrls[ctrl].ops;
 	setup[0] = rt;
 	setup[0] = rt;
@@ -242,7 +279,7 @@ int usb_control_out_data(unsigned int ctrl, unsigned int addr,
 	const struct usb_hcd_ops *ops;
 	const struct usb_hcd_ops *ops;
 	unsigned char setup[8];
 	unsigned char setup[8];
 
 
-	if (ctrl >= n_usb_ctrls)
+	if (ctrl >= n_usb_ctrls || usb_scan_foreign())
 		return -1;
 		return -1;
 	ops = usb_ctrls[ctrl].ops;
 	ops = usb_ctrls[ctrl].ops;
 	setup[0] = rt;
 	setup[0] = rt;
@@ -671,6 +708,8 @@ int usb_port_connected(unsigned int ctrl, unsigned int port)
 {
 {
 	if (ctrl >= n_usb_ctrls)
 	if (ctrl >= n_usb_ctrls)
 		return 0;
 		return 0;
+	if (usb_scan_foreign())
+		return 1;       /* do not detach anything mid-scan */
 	return usb_ctrls[ctrl].ops->port_connected(usb_ctrls[ctrl].local,
 	return usb_ctrls[ctrl].ops->port_connected(usb_ctrls[ctrl].local,
 						   port);
 						   port);
 }
 }
@@ -681,7 +720,8 @@ int usb_int_in_start(unsigned int ctrl, unsigned int slot, unsigned int addr,
 	const struct usb_hcd_ops *ops;
 	const struct usb_hcd_ops *ops;
 	int r;
 	int r;
 
 
-	if (ctrl >= n_usb_ctrls || slot >= USB_HCD_INT_SLOTS)
+	if (ctrl >= n_usb_ctrls || slot >= USB_HCD_INT_SLOTS ||
+	    usb_scan_foreign())
 		return -1;
 		return -1;
 	ops = usb_ctrls[ctrl].ops;
 	ops = usb_ctrls[ctrl].ops;
 	if (slot_ops[slot]) {           /* stale owner from another HCD */
 	if (slot_ops[slot]) {           /* stale owner from another HCD */
@@ -696,14 +736,14 @@ int usb_int_in_start(unsigned int ctrl, unsigned int slot, unsigned int addr,
 
 
 int usb_int_in_poll(unsigned int slot, unsigned char *buf, unsigned int len)
 int usb_int_in_poll(unsigned int slot, unsigned char *buf, unsigned int len)
 {
 {
-	if (slot >= USB_HCD_INT_SLOTS || !slot_ops[slot])
+	if (slot >= USB_HCD_INT_SLOTS || !slot_ops[slot] || usb_scan_foreign())
 		return 0;
 		return 0;
 	return slot_ops[slot]->int_in_poll(slot, buf, len);
 	return slot_ops[slot]->int_in_poll(slot, buf, len);
 }
 }
 
 
 void usb_int_in_stop(unsigned int slot)
 void usb_int_in_stop(unsigned int slot)
 {
 {
-	if (slot >= USB_HCD_INT_SLOTS || !slot_ops[slot])
+	if (slot >= USB_HCD_INT_SLOTS || !slot_ops[slot] || usb_scan_foreign())
 		return;
 		return;
 	slot_ops[slot]->int_in_stop(slot);
 	slot_ops[slot]->int_in_stop(slot);
 	slot_ops[slot] = 0;
 	slot_ops[slot] = 0;
@@ -726,7 +766,8 @@ int usb_bulk_out(unsigned int ctrl, unsigned int addr, unsigned int ep,
 		 unsigned int mps, const unsigned char *buf,
 		 unsigned int mps, const unsigned char *buf,
 		 unsigned int len)
 		 unsigned int len)
 {
 {
-	if (ctrl >= n_usb_ctrls || !usb_ctrls[ctrl].ops->bulk_out)
+	if (ctrl >= n_usb_ctrls || !usb_ctrls[ctrl].ops->bulk_out ||
+	    usb_scan_foreign())
 		return -1;
 		return -1;
 	return usb_ctrls[ctrl].ops->bulk_out(usb_ctrls[ctrl].local, addr, ep,
 	return usb_ctrls[ctrl].ops->bulk_out(usb_ctrls[ctrl].local, addr, ep,
 					     mps, buf, len);
 					     mps, buf, len);
@@ -735,7 +776,8 @@ int usb_bulk_out(unsigned int ctrl, unsigned int addr, unsigned int ep,
 int usb_bulk_in(unsigned int ctrl, unsigned int addr, unsigned int ep,
 int usb_bulk_in(unsigned int ctrl, unsigned int addr, unsigned int ep,
 		unsigned int mps, unsigned char *buf, unsigned int len)
 		unsigned int mps, unsigned char *buf, unsigned int len)
 {
 {
-	if (ctrl >= n_usb_ctrls || !usb_ctrls[ctrl].ops->bulk_in)
+	if (ctrl >= n_usb_ctrls || !usb_ctrls[ctrl].ops->bulk_in ||
+	    usb_scan_foreign())
 		return -1;
 		return -1;
 	return usb_ctrls[ctrl].ops->bulk_in(usb_ctrls[ctrl].local, addr, ep,
 	return usb_ctrls[ctrl].ops->bulk_in(usb_ctrls[ctrl].local, addr, ep,
 					    mps, buf, len);
 					    mps, buf, len);
@@ -758,7 +800,7 @@ int usb_scan(usb_probe_fn probe)
 	if (usb_hcd_init() != 0)
 	if (usb_hcd_init() != 0)
 		return -1;
 		return -1;
 
 
-	scan_busy = 1;
+	scan_begin();
 	generation++;
 	generation++;
 	current_probe = probe;
 	current_probe = probe;
 	print_devices = (probe == 0);
 	print_devices = (probe == 0);
@@ -832,7 +874,7 @@ int usb_hotplug_poll(usb_probe_fn probe)
 		return 0;
 		return 0;
 	last = now;
 	last = now;
 
 
-	scan_busy = 1;
+	scan_begin();
 	current_probe = probe;
 	current_probe = probe;
 	for (c = 0; c < n_usb_ctrls; c++) {
 	for (c = 0; c < n_usb_ctrls; c++) {
 		const struct usb_hcd_ops *ops = usb_ctrls[c].ops;
 		const struct usb_hcd_ops *ops = usb_ctrls[c].ops;

+ 10 - 0
usb.h

@@ -22,6 +22,16 @@ const char *usb_hcd_name_ctrl(unsigned int ctrl);
    completed scan bumps the generation counter. */
    completed scan bumps the generation counter. */
 int usb_scan(usb_probe_fn probe);
 int usb_scan(usb_probe_fn probe);
 
 
+/* Delay used by the USB core and every HCD.  Inside a bus scan owned by a
+ * sleepable process (lsusb, the kworker's HID rescan) it sleeps, which
+ * drops the BKL so the WM and other processes keep running through port
+ * resets and settle times; otherwise it busy-waits as before. */
+void usb_delay_ms(unsigned int ms);
+/* 1 while another context's scan owns the controllers (it is sleeping in
+ * usb_delay_ms).  The transfer wrappers then refuse access; pollers such as
+ * usbhid skip their tick. */
+int usb_scan_foreign(void);
+
 /* Cheap periodic watcher: enumerates devices on root ports whose connect
 /* Cheap periodic watcher: enumerates devices on root ports whose connect
    state changed since the last call (internally throttled).  Returns the
    state changed since the last call (internally throttled).  Returns the
    number of ports with a new device. */
    number of ports with a new device. */

+ 4 - 0
usbhid.c

@@ -386,6 +386,10 @@ int usbhid_poll(unsigned int *buttons, int *dx, int *dy, int *scroll)
 		*scroll = 0;
 		*scroll = 0;
 	if (!hcd_ok)
 	if (!hcd_ok)
 		return 0;
 		return 0;
+	/* Another context's scan is sleeping between port resets: the
+	 * controllers are its until it finishes. */
+	if (usb_scan_foreign())
+		return 0;
 
 
 	/* A full bus re-scan (`lsusb`) invalidates every handle. */
 	/* A full bus re-scan (`lsusb`) invalidates every handle. */
 	if (usb_generation() != gen) {
 	if (usb_generation() != gen) {

+ 39 - 10
vesa_stage.S

@@ -390,14 +390,26 @@ a20_wait:
  * out: data at 0x3000, CF set on failure
  * out: data at 0x3000, CF set on failure
  * Clobbers all general registers. */
  * Clobbers all general registers. */
 read_sectors:
 read_sectors:
+	/* One INT 13h per run of sectors, not per sector: EDD transfers the
+	 * whole chunk (<= 32 sectors into the 16 KB 0x3000 buffer), CHS
+	 * transfers up to the end of the current track (the buffer never
+	 * crosses a 64 KB ISA DMA boundary).  The old per-sector loop made
+	 * ~3600 BIOS calls for a 1.8 MB rootfs.  A BIOS that refuses a
+	 * multi-sector request falls back to one sector per call for the
+	 * rest of this read, so the old behaviour is the failure path. */
 	movl  %eax, rd_lba
 	movl  %eax, rd_lba
 	movw  %cx, rd_left
 	movw  %cx, rd_left
 	movw  $0, rd_off
 	movw  $0, rd_off
 	movw  $0, rd_tries
 	movw  $0, rd_tries
+	movb  $0, rd_single
 read_retry:
 read_retry:
-	movw  rd_off, %bx
 	xorw  %ax, %ax
 	xorw  %ax, %ax
 	movw  %ax, %ds
 	movw  %ax, %ds
+	movw  rd_left, %ax
+	cmpb  $0, rd_single
+	je    1f
+	movw  $1, %ax
+1:	movw  %ax, rd_count
 	movw  $0x0300, %ax
 	movw  $0x0300, %ax
 	movw  %ax, %es
 	movw  %ax, %es
 	movb  stage_drive, %dl
 	movb  stage_drive, %dl
@@ -407,7 +419,9 @@ read_retry:
 read_lba:
 read_lba:
 	/* EDD LBA read */
 	/* EDD LBA read */
 	movw  $0x10, 0x500
 	movw  $0x10, 0x500
-	movw  $1, 0x502
+	movw  rd_count, %ax
+	movw  %ax, 0x502
+	movw  rd_off, %bx
 	movw  %bx, 0x504
 	movw  %bx, 0x504
 	movw  $0x0300, 0x506
 	movw  $0x0300, 0x506
 	movl  rd_lba, %eax
 	movl  rd_lba, %eax
@@ -421,8 +435,13 @@ read_chs:
 	movw  rd_lba, %ax
 	movw  rd_lba, %ax
 	xorw  %dx, %dx
 	xorw  %dx, %dx
 	movw  $18, %di
 	movw  $18, %di
-	divw  %di
-	movb  %dl, %cl
+	divw  %di                     /* ax = track index, dx = sector - 1 */
+	movw  $18, %bx
+	subw  %dx, %bx                /* sectors left on this track */
+	cmpw  rd_count, %bx
+	jae   2f
+	movw  %bx, rd_count
+2:	movb  %dl, %cl
 	incb  %cl
 	incb  %cl
 	xorw  %dx, %dx
 	xorw  %dx, %dx
 	movw  $2, %di
 	movw  $2, %di
@@ -431,28 +450,36 @@ read_chs:
 	movb  %al, %ch
 	movb  %al, %ch
 	movb  stage_drive, %dl
 	movb  stage_drive, %dl
 	movb  $0x02, %ah
 	movb  $0x02, %ah
-	movb  $1, %al
+	movb  rd_count, %al
 	movw  rd_off, %bx
 	movw  rd_off, %bx
 
 
 read_int:
 read_int:
 	int   $0x13
 	int   $0x13
 	jnc   read_ok
 	jnc   read_ok
-	/* Reset and retry the same sector a few times. */
+	/* Reset the drive.  A refused multi-sector request switches to the
+	 * single-sector path; a single sector gets a few retries. */
 	pushw %cx
 	pushw %cx
 	movb  stage_drive, %dl
 	movb  stage_drive, %dl
 	xorw  %ax, %ax
 	xorw  %ax, %ax
 	int   $0x13
 	int   $0x13
 	popw  %cx
 	popw  %cx
-	incw  rd_tries
+	cmpw  $1, rd_count
+	je    3f
+	movb  $1, rd_single
+	jmp   read_retry
+3:	incw  rd_tries
 	cmpw  $3, rd_tries
 	cmpw  $3, rd_tries
 	jb    read_retry
 	jb    read_retry
 	stc
 	stc
 	ret
 	ret
 
 
 read_ok:
 read_ok:
-	incl  rd_lba
-	addw  $0x200, rd_off
-	decw  rd_left
+	movzwl rd_count, %eax
+	addl  %eax, rd_lba
+	shlw  $9, %ax
+	addw  %ax, rd_off
+	movw  rd_count, %ax
+	subw  %ax, rd_left
 	jnz   read_retry
 	jnz   read_retry
 	clc
 	clc
 	ret
 	ret
@@ -624,6 +651,8 @@ copy_len:       .long 0
 rd_lba:         .long 0
 rd_lba:         .long 0
 rd_off:         .word 0
 rd_off:         .word 0
 rd_left:        .word 0
 rd_left:        .word 0
+rd_count:       .word 0
+rd_single:      .byte 0
 rd_tries:       .word 0
 rd_tries:       .word 0
 
 
 msg_pack:       .asciz "Loading rootfs [d="
 msg_pack:       .asciz "Loading rootfs [d="

+ 32 - 0
vfs.c

@@ -7,6 +7,7 @@
 #include "io.h"
 #include "io.h"
 #include "kmalloc.h"
 #include "kmalloc.h"
 #include "process.h"
 #include "process.h"
+#include "sched.h"
 #include "ramfs.h"
 #include "ramfs.h"
 #include "rootfs.h"
 #include "rootfs.h"
 #include "serial.h"
 #include "serial.h"
@@ -398,6 +399,32 @@ int vfs_mount_is_disk(const vfs_mount_t *m)
 	return m && m->ops == &fat_ops;
 	return m && m->ops == &fat_ops;
 }
 }
 
 
+void vfs_scan_poll(unsigned int t0)
+{
+	if (vfs_scan_busy && vfs_scan_owner_pid && get_ticks_ms() - t0 >= 2)
+		vfs_scan_relax(1);
+}
+
+int vfs_scan_relax(unsigned int ms)
+{
+	struct process *p;
+
+	if (!vfs_scan_busy || !vfs_scan_owner_pid)
+		return 0;
+	p = current_proc();
+	if (!p || p->pid != vfs_scan_owner_pid || this_cpu()->preempt_count)
+		return 0;
+	{
+		unsigned int flags;
+
+		__asm__ volatile ("pushfl; popl %0" : "=r"(flags));
+		if (!(flags & 0x200u))
+			return 0;       /* interrupts off: cannot sleep */
+	}
+	process_sleep_ms(ms ? ms : 1);
+	return 1;
+}
+
 /* ---- ramfs implementation ---- */
 /* ---- ramfs implementation ---- */
 
 
 static int ramfs_op_read_file(struct vfs_mount *m, const char *path,
 static int ramfs_op_read_file(struct vfs_mount *m, const char *path,
@@ -539,6 +566,11 @@ static const struct fs_ops ramfs_ops = {
 	.list_dir = ramfs_op_list_dir,
 	.list_dir = ramfs_op_list_dir,
 };
 };
 
 
+int vfs_mount_is_ram(const vfs_mount_t *m)
+{
+	return m && (m->ops == &ramfs_ops || m->ops == &share_ops);
+}
+
 /* ---- mounts ---- */
 /* ---- mounts ---- */
 
 
 static vfs_mount_t *mount_slot(const char *name, const char *dev)
 static vfs_mount_t *mount_slot(const char *name, const char *dev)

+ 15 - 0
vfs.h

@@ -87,6 +87,21 @@ void vfs_remount_device(blk_dev_t *d);
 extern volatile int vfs_scan_busy;
 extern volatile int vfs_scan_busy;
 extern volatile unsigned int vfs_scan_owner_pid;
 extern volatile unsigned int vfs_scan_owner_pid;
 
 
+/* 1 for the RAM-backed mounts (/ramfs, /share).  They are re-added first and
+ * stay valid while a rescan sleeps, so their file operations do not wait. */
+int vfs_mount_is_ram(const vfs_mount_t *m);
+
+/* Called from the wait loops of the storage probes (floppy, ATA, AHCI,
+ * SDHCI).  When the scan belongs to a sleepable process (the `storage`
+ * command) it sleeps for `ms` instead of spinning, which drops the BKL so
+ * the WM and other processes keep running; at boot (idle context) and in
+ * normal I/O it is a no-op and the caller keeps busy-waiting. */
+int vfs_scan_relax(unsigned int ms);
+/* For a driver wait loop that started at tick t0: once it has waited 2 ms
+ * or more, relax (sleep 1 ms) if a sleepable rescan owns the disks.  Fast
+ * handshakes and normal I/O never sleep. */
+void vfs_scan_poll(unsigned int t0);
+
 /* Mount the command pack (ramfs) at /ramfs; no-op when no pack is loaded. */
 /* Mount the command pack (ramfs) at /ramfs; no-op when no pack is loaded. */
 void vfs_mount_ramfs(void);
 void vfs_mount_ramfs(void);
 
 

+ 50 - 34
wifi.c

@@ -4,7 +4,9 @@
  * parsed here; the RSN IE for the association request and the whole WPA2
  * 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
  * 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
  * 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
  * 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.
  * every frame is a plain 24-byte-header 802.11 data frame + LLC/SNAP.
@@ -12,8 +14,9 @@
 
 
 #include "wifi.h"
 #include "wifi.h"
 #include "ccmp.h"
 #include "ccmp.h"
-#include "iwl_mvm.h"
-#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 "io.h"
 #include "mm.h"
 #include "mm.h"
 #include "net.h"
 #include "net.h"
@@ -112,6 +115,20 @@ static int alloc_buffers(void)
 	return 0;
 	return 0;
 }
 }
 
 
+/* A firmware assert (CSR_INT SW_ERR) leaves the card dead: every command
+ * times out until the image is reloaded.  Drop to DOWN so the next scan or
+ * 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 || !wififw_fatal())
+		return;
+	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 = "device died; reload on next use";
+}
+
 static int wifi_up(void)
 static int wifi_up(void)
 {
 {
 	if (W.state != SYS_WIFI_ST_DOWN)
 	if (W.state != SYS_WIFI_ST_DOWN)
@@ -120,8 +137,8 @@ static int wifi_up(void)
 		W.detail = "out of memory";
 		W.detail = "out of memory";
 		return -1;
 		return -1;
 	}
 	}
-	if (iwl_mvm_up(W.mac) < 0) {
-		W.detail = iwl_mvm_state();
+	if (wififw_up(W.mac) < 0) {
+		W.detail = wififw_state();
 		return -1;
 		return -1;
 	}
 	}
 	W.present = 1;
 	W.present = 1;
@@ -250,7 +267,7 @@ static void rx_mgmt(const unsigned char *f, unsigned int len, int rssi,
 			serial_puts(SERIAL_COM1, "\n");
 			serial_puts(SERIAL_COM1, "\n");
 			W.state = SYS_WIFI_ST_IDLE;
 			W.state = SYS_WIFI_ST_IDLE;
 			W.detail = "deauthenticated by AP";
 			W.detail = "deauthenticated by AP";
-			iwl_mvm_disconnect();
+			wififw_disconnect();
 		}
 		}
 	}
 	}
 }
 }
@@ -516,7 +533,7 @@ static int tx_data(const unsigned char *dst, unsigned int ethertype,
 	f[o++] = ethertype >> 8;
 	f[o++] = ethertype >> 8;
 	f[o++] = ethertype;
 	f[o++] = ethertype;
 	wmemcpy(f + o, payload, plen);
 	wmemcpy(f + o, payload, plen);
-	if (iwl_mvm_tx_data(f, o + plen, prot) < 0)
+	if (wififw_tx_data(f, o + plen, prot) < 0)
 		return -1;
 		return -1;
 	W.tx_frames++;
 	W.tx_frames++;
 	return 0;
 	return 0;
@@ -535,7 +552,7 @@ static int tx_mgmt_frame(unsigned int stype, const unsigned char *body,
 	wmemcpy(f + 10, W.mac, 6);
 	wmemcpy(f + 10, W.mac, 6);
 	wmemcpy(f + 16, W.cur.bssid, 6);
 	wmemcpy(f + 16, W.cur.bssid, 6);
 	wmemcpy(f + 24, body, blen);
 	wmemcpy(f + 24, body, blen);
-	return iwl_mvm_tx_mgmt(f, 24 + blen);
+	return wififw_tx_mgmt(f, 24 + blen);
 }
 }
 
 
 static int wait_flag(int *flag, unsigned int ms)
 static int wait_flag(int *flag, unsigned int ms)
@@ -543,7 +560,7 @@ static int wait_flag(int *flag, unsigned int ms)
 	unsigned int t;
 	unsigned int t;
 
 
 	for (t = 0; t < ms && !*flag; t++) {
 	for (t = 0; t < ms && !*flag; t++) {
-		if (iwl_mvm_poll() < 0)
+		if (wififw_poll() < 0)
 			return -1;
 			return -1;
 		if (!*flag)
 		if (!*flag)
 			iwl_udelay(1000);
 			iwl_udelay(1000);
@@ -564,7 +581,7 @@ static int do_connect(const struct sys_wifi_connect *c)
 	if (W.state == SYS_WIFI_ST_DOWN && wifi_up() < 0)
 	if (W.state == SYS_WIFI_ST_DOWN && wifi_up() < 0)
 		return -1;
 		return -1;
 	if (W.state >= SYS_WIFI_ST_ASSOC) {
 	if (W.state >= SYS_WIFI_ST_ASSOC) {
-		iwl_mvm_disconnect();
+		wififw_disconnect();
 		W.state = SYS_WIFI_ST_IDLE;
 		W.state = SYS_WIFI_ST_IDLE;
 	}
 	}
 	if (c->ssid_len > 32 || c->ie_len > sizeof(c->ie) || !c->channel)
 	if (c->ssid_len > 32 || c->ie_len > sizeof(c->ie) || !c->channel)
@@ -588,7 +605,7 @@ static int do_connect(const struct sys_wifi_connect *c)
 	W.sw_rx_log = W.sw_fail_log = 0;
 	W.sw_rx_log = W.sw_fail_log = 0;
 	W.ptk_conn = 0;
 	W.ptk_conn = 0;
 
 
-	if (iwl_mvm_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";
 		W.detail = "firmware connect setup failed";
 		return -1;
 		return -1;
 	}
 	}
@@ -606,7 +623,7 @@ static int do_connect(const struct sys_wifi_connect *c)
 	if (!W.auth_done || W.auth_status) {
 	if (!W.auth_done || W.auth_status) {
 		W.detail = W.auth_done ? "authentication refused"
 		W.detail = W.auth_done ? "authentication refused"
 				       : "no authentication reply";
 				       : "no authentication reply";
-		iwl_mvm_disconnect();
+		wififw_disconnect();
 		return -1;
 		return -1;
 	}
 	}
 	/* Association request: capability, listen interval, SSID, rates,
 	/* Association request: capability, listen interval, SSID, rates,
@@ -645,12 +662,12 @@ static int do_connect(const struct sys_wifi_connect *c)
 			serial_putdec(SERIAL_COM1, W.assoc_status);
 			serial_putdec(SERIAL_COM1, W.assoc_status);
 			serial_puts(SERIAL_COM1, "\n");
 			serial_puts(SERIAL_COM1, "\n");
 		}
 		}
-		iwl_mvm_disconnect();
+		wififw_disconnect();
 		return -1;
 		return -1;
 	}
 	}
-	if (iwl_mvm_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";
 		W.detail = "firmware association update failed";
-		iwl_mvm_disconnect();
+		wififw_disconnect();
 		return -1;
 		return -1;
 	}
 	}
 	W.state = SYS_WIFI_ST_ASSOC;
 	W.state = SYS_WIFI_ST_ASSOC;
@@ -669,8 +686,8 @@ static void do_disconnect(void)
 	W.ptk_conn = 0;
 	W.ptk_conn = 0;
 	if (W.state >= SYS_WIFI_ST_ASSOC) {
 	if (W.state >= SYS_WIFI_ST_ASSOC) {
 		tx_mgmt_frame(12, reason, 2);
 		tx_mgmt_frame(12, reason, 2);
-		iwl_mvm_poll();
-		iwl_mvm_disconnect();
+		wififw_poll();
+		wififw_disconnect();
 	}
 	}
 	if (W.state != SYS_WIFI_ST_DOWN)
 	if (W.state != SYS_WIFI_ST_DOWN)
 		W.state = SYS_WIFI_ST_IDLE;
 		W.state = SYS_WIFI_ST_IDLE;
@@ -684,7 +701,7 @@ static int nd_init(void)
 		return 0;
 		return 0;
 	/* Boot probe: find the card only.  The firmware is loaded on first
 	/* Boot probe: find the card only.  The firmware is loaded on first
 	 * use (`wifi`), so boot time and RAM are unchanged without WiFi. */
 	 * use (`wifi`), so boot time and RAM are unchanged without WiFi. */
-	if (iwl_probe() < 0)
+	if (wififw_probe() < 0)
 		return -1;
 		return -1;
 	W.present = 1;
 	W.present = 1;
 	W.detail = "firmware not loaded";
 	W.detail = "firmware not loaded";
@@ -701,10 +718,11 @@ static int nd_rx(unsigned char *buf, unsigned int maxlen)
 	struct rxq_slot *s;
 	struct rxq_slot *s;
 	unsigned int n;
 	unsigned int n;
 
 
+	wifi_check_crash();
 	if (W.state == SYS_WIFI_ST_DOWN)
 	if (W.state == SYS_WIFI_ST_DOWN)
 		return -1;
 		return -1;
 	if (W.rxq_head == W.rxq_tail)
 	if (W.rxq_head == W.rxq_tail)
-		iwl_mvm_poll();
+		wififw_poll();
 	if (W.rxq_head == W.rxq_tail)
 	if (W.rxq_head == W.rxq_tail)
 		return -1;
 		return -1;
 	s = &W.rxq[W.rxq_tail];
 	s = &W.rxq[W.rxq_tail];
@@ -755,7 +773,7 @@ static void nd_print_info(void)
 	static const char *const st[] = { "down", "idle", "associated", "link up" };
 	static const char *const st[] = { "down", "idle", "associated", "link up" };
 
 
 	io_putstr("chip:  ");
 	io_putstr("chip:  ");
-	io_putstr(iwl_chip_name());
+	io_putstr(wififw_chip_name());
 	io_putstr("\nstate: ");
 	io_putstr("\nstate: ");
 	io_putstr(st[W.state & 3]);
 	io_putstr(st[W.state & 3]);
 	io_putstr(" (");
 	io_putstr(" (");
@@ -792,7 +810,7 @@ static int nd_link_up(void)
 
 
 const struct netdev wifi_netdev = {
 const struct netdev wifi_netdev = {
 	"wifi",
 	"wifi",
-	"Intel 7265D/3165 WiFi",
+	"WiFi",
 	nd_init,
 	nd_init,
 	nd_get_mac,
 	nd_get_mac,
 	nd_rx,
 	nd_rx,
@@ -812,8 +830,7 @@ const struct netdev wifi_netdev = {
 
 
 void wifi_shutdown(void)
 void wifi_shutdown(void)
 {
 {
-	if (W.state != SYS_WIFI_ST_DOWN || iwl_present())
-		iwl_mvm_down();
+	wififw_shutdown();
 	if (W.state != SYS_WIFI_ST_DOWN)
 	if (W.state != SYS_WIFI_ST_DOWN)
 		W.state = SYS_WIFI_ST_DOWN;
 		W.state = SYS_WIFI_ST_DOWN;
 }
 }
@@ -836,6 +853,7 @@ static void copy_str(char *dst, unsigned int max, const char *src)
 
 
 int sys_wifi(int op, int a2, int a3)
 int sys_wifi(int op, int a2, int a3)
 {
 {
+	wifi_check_crash();
 	switch (op) {
 	switch (op) {
 	case SYS_WIFI_UP:
 	case SYS_WIFI_UP:
 		if (!W.present && nd_init() < 0)
 		if (!W.present && nd_init() < 0)
@@ -843,11 +861,9 @@ int sys_wifi(int op, int a2, int a3)
 		return wifi_up();
 		return wifi_up();
 	case SYS_WIFI_STATUS: {
 	case SYS_WIFI_STATUS: {
 		struct sys_wifi_status *s = (struct sys_wifi_status *)a2;
 		struct sys_wifi_status *s = (struct sys_wifi_status *)a2;
-		const struct iwl_fw_info *fw;
-
 		if (!user_ok(a2, sizeof(*s), 1))
 		if (!user_ok(a2, sizeof(*s), 1))
 			return -1;
 			return -1;
-		if (!W.present && iwl_probe() == 0)
+		if (!W.present && wififw_probe() == 0)
 			W.present = 1;
 			W.present = 1;
 		iwl_memzero(s, sizeof(*s));
 		iwl_memzero(s, sizeof(*s));
 		s->state = W.state;
 		s->state = W.state;
@@ -860,9 +876,9 @@ int sys_wifi(int op, int a2, int a3)
 			s->ssid_len = W.cur.ssid_len;
 			s->ssid_len = W.cur.ssid_len;
 			wmemcpy(s->ssid, W.cur.ssid, 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),
 		copy_str(s->detail, sizeof(s->detail),
 			 W.detail ? W.detail : (W.present ? "firmware not loaded"
 			 W.detail ? W.detail : (W.present ? "firmware not loaded"
 							  : "no card"));
 							  : "no card"));
@@ -878,14 +894,14 @@ int sys_wifi(int op, int a2, int a3)
 		if (W.state >= SYS_WIFI_ST_ASSOC)
 		if (W.state >= SYS_WIFI_ST_ASSOC)
 			return -1;        /* scanning while associated: not yet */
 			return -1;        /* scanning while associated: not yet */
 		W.nbss = 0;
 		W.nbss = 0;
-		return iwl_mvm_scan_start();
+		return wififw_scan_start();
 	case SYS_WIFI_RESULTS: {
 	case SYS_WIFI_RESULTS: {
 		unsigned int max = (unsigned int)a3, n;
 		unsigned int max = (unsigned int)a3, n;
 
 
 		if (W.state == SYS_WIFI_ST_DOWN)
 		if (W.state == SYS_WIFI_ST_DOWN)
 			return -1;
 			return -1;
-		iwl_mvm_poll();
-		if (iwl_mvm_scan_running())
+		wififw_poll();
+		if (wififw_scan_running())
 			return -2;
 			return -2;
 		if (max > W.nbss)
 		if (max > W.nbss)
 			max = W.nbss;
 			max = W.nbss;
@@ -912,7 +928,7 @@ int sys_wifi(int op, int a2, int a3)
 
 
 		if (W.state < SYS_WIFI_ST_ASSOC)
 		if (W.state < SYS_WIFI_ST_ASSOC)
 			return -1;
 			return -1;
-		iwl_mvm_poll();
+		wififw_poll();
 		if (W.eapq_head == W.eapq_tail)
 		if (W.eapq_head == W.eapq_tail)
 			return 0;
 			return 0;
 		s = &W.eapq[W.eapq_tail];
 		s = &W.eapq[W.eapq_tail];
@@ -940,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))
 		if (W.state < SYS_WIFI_ST_ASSOC || !user_ok(a2, sizeof(k), 0))
 			return -1;
 			return -1;
 		wmemcpy(&k, (const void *)a2, sizeof(k));
 		wmemcpy(&k, (const void *)a2, sizeof(k));
-		r = iwl_mvm_set_key(k.pairwise, k.keyidx, k.key,
+		r = wififw_set_key(k.pairwise, k.keyidx, k.key,
 				    k.pairwise ? 0 : k.rsc);
 				    k.pairwise ? 0 : k.rsc);
 		if (r == 0) {
 		if (r == 0) {
 			if (k.pairwise) {
 			if (k.pairwise) {

+ 11 - 3
wifi.h

@@ -1,6 +1,7 @@
-/* WiFi station (Intel 7265D/3165): 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
 #ifndef WIFI_H
 #define 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). */
  * netdev (its RX ring would otherwise keep writing into freed pages). */
 void wifi_shutdown(void);
 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
 #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

+ 2 - 4
xhci.c

@@ -10,6 +10,7 @@
  * The schedule is polled, like the rest of the kernel.  Rings and contexts
  * The schedule is polled, like the rest of the kernel.  Rings and contexts
  * are page-allocated so the driver does not grow the kernel BSS. */
  * are page-allocated so the driver does not grow the kernel BSS. */
 
 
+#include "usb.h"
 #include "mm.h"
 #include "mm.h"
 #include "paging.h"
 #include "paging.h"
 #include "pci.h"
 #include "pci.h"
@@ -266,10 +267,7 @@ static void dbg_dec(const char *s, unsigned int v) { (void)s; (void)v; }
 
 
 static void delay_ms(unsigned int ms)
 static void delay_ms(unsigned int ms)
 {
 {
-	unsigned int t0 = get_ticks_ms();
-
-	while (get_ticks_ms() - t0 < ms)
-		;
+	usb_delay_ms(ms);    /* sleeps inside a sleepable scan */
 }
 }
 
 
 /* ---- MMIO helpers ------------------------------------------------------ */
 /* ---- MMIO helpers ------------------------------------------------------ */

Algúns arquivos non se mostraron porque demasiados arquivos cambiaron neste cambio