diff --git a/crates/stela-driver-net/src/wifi/mediatek/mcu.rs b/crates/stela-driver-net/src/wifi/mediatek/mcu.rs index 62ab137051..1d7c864fa9 100644 --- a/crates/stela-driver-net/src/wifi/mediatek/mcu.rs +++ b/crates/stela-driver-net/src/wifi/mediatek/mcu.rs @@ -21,6 +21,21 @@ pub mod runtime; const TX_HEADER: usize = 64; const MAX_TX_BYTES: usize = u16::MAX as usize; +/// `txd[1]` for an MCU command (`MT_HDR_FORMAT_CMD`). +/// +/// Connac3 (`mt76_connac3_mac.h`, included by `mt7925/mcu.c` via `mac.h`) +/// places `MT_TXD1_HDR_FORMAT` in bits 15..=14 (`1 << 14 = 0x4000`) and has no +/// `MT_TXD1_LONG_FORMAT`. Connac2 (`mt76_connac2_mac.h`) places +/// `MT_TXD1_HDR_FORMAT` in bits 17..=16 alongside `MT_TXD1_LONG_FORMAT` +/// (`(1 << 31) | (1 << 16) = 0x8001_0000`). +pub(super) const fn mcu_txd1(chip: Chip) -> u32 { + if matches!(chip, Chip::Mt7925) { + 1 << 14 + } else { + (1 << 31) | (1 << 16) + } +} + #[derive(Clone, Copy, Debug, Eq, PartialEq)] #[repr(u8)] pub enum Command { @@ -119,7 +134,7 @@ impl Encoder { let length = payload.len() + TX_HEADER; let mut bytes = vec![0u8; length]; let txd0 = length as u32 | (2 << 23) | (0x20 << 25); - let txd1: u32 = (1 << 16) | if chip == Chip::Mt7925 { 0 } else { 1 << 31 }; + let txd1 = mcu_txd1(chip); bytes[0..4].copy_from_slice(&txd0.to_le_bytes()); bytes[4..8].copy_from_slice(&txd1.to_le_bytes()); bytes[32..34].copy_from_slice(&((length - 32) as u16).to_le_bytes()); @@ -244,7 +259,7 @@ mod tests { assert_eq!( &p.bytes[4..8], &(if chip == Chip::Mt7925 { - 0x10000u32 + 0x4000u32 } else { 0x80010000 }) @@ -373,4 +388,34 @@ mod tests { assert_eq!(trace, expected, "chip {number}"); } } + + #[test] + fn connac3_mcu_commands_place_hdr_format_at_bit_14_while_connac2_uses_bit_16() { + for chip in [Chip::Mt7921, Chip::Mt7922, Chip::Mt7925] { + let expected = if chip == Chip::Mt7925 { + 0x4000u32.to_le_bytes() + } else { + 0x8001_0000u32.to_le_bytes() + }; + let mut enc = Encoder::default(); + let boot = enc + .encode(chip, Command::PatchSemaphore, &[1, 0, 0, 0]) + .unwrap(); + let caps = enc.encode_capabilities(chip); + let eeprom = enc.encode_encapsulation_query(chip); + let uni = enc + .encode_uni(chip, runtime::Command::DeviceInfo, &[0; 8]) + .unwrap(); + let restart = enc.encode_bootstrap(&init::Request::restart(chip)); + for bytes in [ + boot.bytes.as_slice(), + caps.bytes(), + eeprom.bytes(), + uni.bytes(), + restart.bytes(), + ] { + assert_eq!(&bytes[4..8], &expected, "chip {chip:?}"); + } + } + } } diff --git a/crates/stela-driver-net/src/wifi/mediatek/mcu/capabilities.rs b/crates/stela-driver-net/src/wifi/mediatek/mcu/capabilities.rs index 356eda93b6..c80b05a189 100644 --- a/crates/stela-driver-net/src/wifi/mediatek/mcu/capabilities.rs +++ b/crates/stela-driver-net/src/wifi/mediatek/mcu/capabilities.rs @@ -61,8 +61,7 @@ impl Encoder { let mut bytes = vec![0; if connac3 { 56 } else { 64 }]; let length = bytes.len(); bytes[..4].copy_from_slice(&(length as u32 | (2 << 23) | (0x20 << 25)).to_le_bytes()); - bytes[4..8] - .copy_from_slice(&(1u32 << 16 | if connac3 { 0 } else { 1 << 31 }).to_le_bytes()); + bytes[4..8].copy_from_slice(&super::mcu_txd1(chip).to_le_bytes()); bytes[32..34].copy_from_slice(&((length - 32) as u16).to_le_bytes()); bytes[37] = 0xa0; bytes[39] = sequence; diff --git a/crates/stela-driver-net/src/wifi/mediatek/mcu/clc.rs b/crates/stela-driver-net/src/wifi/mediatek/mcu/clc.rs index 454452128a..b9dcc84358 100644 --- a/crates/stela-driver-net/src/wifi/mediatek/mcu/clc.rs +++ b/crates/stela-driver-net/src/wifi/mediatek/mcu/clc.rs @@ -40,7 +40,7 @@ impl Encoder { let length = header + request.body().len(); let mut bytes = vec![0; length]; bytes[..4].copy_from_slice(&(length as u32 | (2 << 23) | (0x20 << 25)).to_le_bytes()); - bytes[4..8].copy_from_slice(&(1u32 << 16 | if c3 { 0 } else { 1 << 31 }).to_le_bytes()); + bytes[4..8].copy_from_slice(&super::mcu_txd1(chip).to_le_bytes()); bytes[32..34].copy_from_slice(&((length - 32) as u16).to_le_bytes()); bytes[37] = 0xa0; bytes[39] = sequence; diff --git a/crates/stela-driver-net/src/wifi/mediatek/mcu/domain.rs b/crates/stela-driver-net/src/wifi/mediatek/mcu/domain.rs index fc0eff0fc1..cfa61f7d55 100644 --- a/crates/stela-driver-net/src/wifi/mediatek/mcu/domain.rs +++ b/crates/stela-driver-net/src/wifi/mediatek/mcu/domain.rs @@ -142,7 +142,7 @@ impl Encoder { let length = header + request.body.len(); let mut bytes = vec![0; length]; bytes[..4].copy_from_slice(&(length as u32 | (2 << 23) | (0x20 << 25)).to_le_bytes()); - bytes[4..8].copy_from_slice(&(1u32 << 16 | if c3 { 0 } else { 1 << 31 }).to_le_bytes()); + bytes[4..8].copy_from_slice(&super::mcu_txd1(request.chip).to_le_bytes()); bytes[32..34].copy_from_slice(&((length - 32) as u16).to_le_bytes()); bytes[37] = 0xa0; bytes[39] = sequence; diff --git a/crates/stela-driver-net/src/wifi/mediatek/mcu/eeprom.rs b/crates/stela-driver-net/src/wifi/mediatek/mcu/eeprom.rs index b40643892c..c33ed2bbe6 100644 --- a/crates/stela-driver-net/src/wifi/mediatek/mcu/eeprom.rs +++ b/crates/stela-driver-net/src/wifi/mediatek/mcu/eeprom.rs @@ -52,7 +52,7 @@ impl Encoder { let mut bytes = vec![0; if c3 { 80 } else { 88 }]; let length = bytes.len(); bytes[..4].copy_from_slice(&(length as u32 | (2 << 23) | (0x20 << 25)).to_le_bytes()); - bytes[4..8].copy_from_slice(&(1u32 << 16 | if c3 { 0 } else { 1 << 31 }).to_le_bytes()); + bytes[4..8].copy_from_slice(&super::mcu_txd1(chip).to_le_bytes()); bytes[32..34].copy_from_slice(&((length - 32) as u16).to_le_bytes()); bytes[37] = 0xa0; bytes[39] = sequence; diff --git a/crates/stela-driver-net/src/wifi/mediatek/mcu/init.rs b/crates/stela-driver-net/src/wifi/mediatek/mcu/init.rs index c07dafe4f7..e8419a6d55 100644 --- a/crates/stela-driver-net/src/wifi/mediatek/mcu/init.rs +++ b/crates/stela-driver-net/src/wifi/mediatek/mcu/init.rs @@ -387,15 +387,7 @@ impl Encoder { let length = header + request.body.len(); let mut bytes = vec![0; length]; bytes[..4].copy_from_slice(&(length as u32 | (2 << 23) | (0x20 << 25)).to_le_bytes()); - bytes[4..8].copy_from_slice( - &(1u32 << 16 - | if request.chip == Chip::Mt7925 { - 0 - } else { - 1 << 31 - }) - .to_le_bytes(), - ); + bytes[4..8].copy_from_slice(&super::mcu_txd1(request.chip).to_le_bytes()); bytes[32..34].copy_from_slice(&((length - 32) as u16).to_le_bytes()); bytes[37] = 0xa0; bytes[39] = sequence; diff --git a/crates/stela-driver-net/src/wifi/mediatek/mcu/power.rs b/crates/stela-driver-net/src/wifi/mediatek/mcu/power.rs index 94084354d6..0b031b96f4 100644 --- a/crates/stela-driver-net/src/wifi/mediatek/mcu/power.rs +++ b/crates/stela-driver-net/src/wifi/mediatek/mcu/power.rs @@ -342,7 +342,7 @@ impl Encoder { let length = header + request.body.len(); let mut bytes = vec![0; length]; bytes[..4].copy_from_slice(&(length as u32 | (2 << 23) | (0x20 << 25)).to_le_bytes()); - bytes[4..8].copy_from_slice(&(1u32 << 16 | if c3 { 0 } else { 1 << 31 }).to_le_bytes()); + bytes[4..8].copy_from_slice(&super::mcu_txd1(request.chip).to_le_bytes()); bytes[32..34].copy_from_slice(&((length - 32) as u16).to_le_bytes()); bytes[37] = 0xa0; bytes[39] = sequence; diff --git a/crates/stela-driver-net/src/wifi/mediatek/mcu/runtime.rs b/crates/stela-driver-net/src/wifi/mediatek/mcu/runtime.rs index 92b76e912e..42f9ece53b 100644 --- a/crates/stela-driver-net/src/wifi/mediatek/mcu/runtime.rs +++ b/crates/stela-driver-net/src/wifi/mediatek/mcu/runtime.rs @@ -86,7 +86,7 @@ impl Encoder { let length = HEADER + payload.len(); let mut bytes = vec![0; length]; let txd0 = length as u32 | (2 << 23) | (0x20 << 25); - let txd1 = (1u32 << 16) | if chip == Chip::Mt7925 { 0 } else { 1 << 31 }; + let txd1 = super::mcu_txd1(chip); bytes[..4].copy_from_slice(&txd0.to_le_bytes()); bytes[4..8].copy_from_slice(&txd1.to_le_bytes()); bytes[32..34].copy_from_slice(&((length - 32) as u16).to_le_bytes());