Linux wireless drivers development
 help / color / mirror / Atom feed
* [PATCH RFC mt76-next] wifi: mt76: mt7996: support spectrum scan / IQ capture
@ 2026-12-04 10:55 Benjamin Koerner
  2026-12-04 12:35 ` Sven Eckelmann
  0 siblings, 1 reply; 2+ messages in thread
From: Benjamin Koerner @ 2026-12-04 10:55 UTC (permalink / raw)
  To: Felix Fietkau, Lorenzo Bianconi, Ryder Lee, Shayne Chen,
	Sean Wang, Matthias Brugger, AngeloGioacchino Del Regno
  Cc: linux-wireless, linux-kernel, linux-arm-kernel, linux-mediatek,
	sw, se, Harshal Gohel, Benjamin Koerner

The firmware for mt7996 wifi chips can capture baseband IQ samples using
the SPECTRUM UNI command (0x30). The mt7996 driver doesn't make use of
it yet.

Similar information is exported by ath family wlan drivers through
their spectral scan feature. Unlike mt7996, the ath firmware doesn't
provide IQ samples but already the result of an FFT.

Expose the SPECTRUM command through the mt76 debugfs allowing a user to
request IQ samples from the chip. Either a specific number of captures
can be requested or continuously running background capture.

The following debugfs files can be used:

- spectrum_samples:     requested number of IQ samples in each capture
                        (def `256`, max `2048`)
- spectrum_gain:        `auto`, `high` (def), `mid`, `low`
- spectrum_band:        `2` (def), `5`, `6` GHz
- spectrum_bandwidth:   `20` (def), `40`, `80`, `160`, `320` MHz, `auto`
- spectrum_repeat:      number of captures (def `1`) each manual trigger
- spectrum_trigger:     `disable` (def), `manual`, `background`
- spectrum_data0:       relay channel with captured data

The spectrum_data0 channel gets created at the first write of `manual`
or `background` to spectrum_trigger. Writing `manual` while having set
spectrum_repeat to 0 allows for creating the relay channel without
publishing any data yet. While reading from the relay channel only
`disable` may be written to spectrum_trigger to stop captures and flush
the channel because `manual` or `background` resets the relay channel
(discarding any unread data).

If the relay channel is not read from fast enough, fewer captures than
requested may arrive with sequence number gaps.

For a user, a typical scan involves:

    cd /sys/kernel/debug/ieee80211/phy0/mt76
    echo 2 > spectrum_band          # 2.4 GHz band
    echo 20 > spectrum_bandwidth    # 20 MHz bandwidth
    echo 256 > spectrum_samples     # decrease if whole scan does not
                                    # fit in channel buffer or read
                                    # continuously from channel
    echo background > spectrum_trigger
    iw dev phy0.0-ap0 scan passive
    echo disable > spectrum_trigger
    cat spectrum_data0 > /tmp/spectrum_data

The captured data are raw I/Q values. To get actual spectrum data, an
FFT can be performed in userspace.

The ABI format in drivers/net/wireless/mediatek/mt76/mt7996/spectrum.h
is compatible with ath family wlan drivers' TLV-based sample format.

Tested-on: MT7996 rev 79900000 PCI hw/sw version 0x8a108a10 20250904203308a

Co-developed-by: Harshal Gohel <hg@simonwunderlich.de>
Signed-off-by: Harshal Gohel <hg@simonwunderlich.de>
Signed-off-by: Benjamin Koerner <bk@simonwunderlich.de>
---
This patch has been tested on a board using the MT7988A SoC and MT7996
Wi-Fi chip with an OpenWrt kernel (backporting took place as necessary).
However, the patch should just work fine in the normal kernel tree.

Obtained data can be visualized, e. g., using FFT_eval. FFT_eval is
updated alongside this patch at
https://github.com/simonwunderlich/FFT_eval/pull/25.

As this is my first patch submission to the Linux kernel, I am very
glad for any feedback in order to improve this patch.

Thank you very much!
---
 .../net/wireless/mediatek/mt76/mt76_connac_mcu.h   |   2 +
 drivers/net/wireless/mediatek/mt76/mt7996/Makefile |   2 +-
 drivers/net/wireless/mediatek/mt76/mt7996/init.c   |   4 +
 drivers/net/wireless/mediatek/mt76/mt7996/mcu.c    | 346 +++++++++++
 drivers/net/wireless/mediatek/mt76/mt7996/mcu.h    |  95 +++
 drivers/net/wireless/mediatek/mt76/mt7996/mt7996.h | 105 ++++
 .../net/wireless/mediatek/mt76/mt7996/spectrum.c   | 668 +++++++++++++++++++++
 .../net/wireless/mediatek/mt76/mt7996/spectrum.h   | 102 ++++
 8 files changed, 1323 insertions(+), 1 deletion(-)

diff --git a/drivers/net/wireless/mediatek/mt76/mt76_connac_mcu.h b/drivers/net/wireless/mediatek/mt76/mt76_connac_mcu.h
index 2ced2a526fb9..7a788849d4a1 100644
--- a/drivers/net/wireless/mediatek/mt76/mt76_connac_mcu.h
+++ b/drivers/net/wireless/mediatek/mt76/mt76_connac_mcu.h
@@ -1104,6 +1104,7 @@ enum {
 	MCU_UNI_EVENT_MBMC = 0x28,
 	MCU_UNI_EVENT_BSS_IS_ABSENCE = 0x29,
 	MCU_UNI_EVENT_TX_DONE = 0x2d,
+	MCU_UNI_EVENT_SPECTRUM = 0x30,
 	MCU_UNI_EVENT_THERMAL = 0x35,
 	MCU_UNI_EVENT_RSSI_MONITOR = 0x41,
 	MCU_UNI_EVENT_NIC_CAPAB = 0x43,
@@ -1345,6 +1346,7 @@ enum {
 	MCU_UNI_CMD_SET_POWER_LIMIT = 0x2c,
 	MCU_UNI_CMD_EFUSE_CTRL = 0x2d,
 	MCU_UNI_CMD_RA = 0x2f,
+	MCU_UNI_CMD_SPECTRUM = 0x30,
 	MCU_UNI_CMD_MURU = 0x31,
 	MCU_UNI_CMD_TESTMODE_RX_STAT = 0x32,
 	MCU_UNI_CMD_BF = 0x33,
diff --git a/drivers/net/wireless/mediatek/mt76/mt7996/Makefile b/drivers/net/wireless/mediatek/mt76/mt7996/Makefile
index 69d2d4bb9e69..358a17e5c4ab 100644
--- a/drivers/net/wireless/mediatek/mt76/mt7996/Makefile
+++ b/drivers/net/wireless/mediatek/mt76/mt7996/Makefile
@@ -3,7 +3,7 @@
 obj-$(CONFIG_MT7996E) += mt7996e.o
 
 mt7996e-y := pci.o init.o dma.o eeprom.o main.o mcu.o mac.o \
-	     debugfs.o mmio.o
+	     debugfs.o mmio.o spectrum.o
 
 mt7996e-$(CONFIG_MT7996_NPU) += npu.o
 mt7996e-$(CONFIG_DEV_COREDUMP) += coredump.o
diff --git a/drivers/net/wireless/mediatek/mt76/mt7996/init.c b/drivers/net/wireless/mediatek/mt76/mt7996/init.c
index 8962d4dab678..79f32bbdfb8d 100644
--- a/drivers/net/wireless/mediatek/mt76/mt7996/init.c
+++ b/drivers/net/wireless/mediatek/mt76/mt7996/init.c
@@ -1302,6 +1302,7 @@ static int mt7996_init_hardware(struct mt7996_dev *dev)
 	for (i = 0; i < ARRAY_SIZE(dev->wed_rro.page_map); i++)
 		INIT_LIST_HEAD(&dev->wed_rro.page_map[i]);
 	spin_lock_init(&dev->wed_rro.lock);
+	spin_lock_init(&dev->spectrum.lock);
 
 	ret = mt7996_variant_type_init(dev);
 	if (ret)
@@ -1832,6 +1833,8 @@ int mt7996_register_device(struct mt7996_dev *dev)
 	if (ret)
 		goto unregister;
 
+	mt7996_spectrum_init(dev);
+
 	return 0;
 
 unregister:
@@ -1851,6 +1854,7 @@ int mt7996_register_device(struct mt7996_dev *dev)
 void mt7996_unregister_device(struct mt7996_dev *dev)
 {
 	mt7996_set_hw_init_done(dev, false);
+	mt7996_spectrum_free(dev);
 	cancel_work_sync(&dev->dump_work);
 	cancel_work_sync(&dev->wed_rro.work);
 	cancel_work_sync(&dev->reset_work);
diff --git a/drivers/net/wireless/mediatek/mt76/mt7996/mcu.c b/drivers/net/wireless/mediatek/mt76/mt7996/mcu.c
index 65f2563d856a..b0742cd2d5b1 100644
--- a/drivers/net/wireless/mediatek/mt76/mt7996/mcu.c
+++ b/drivers/net/wireless/mediatek/mt76/mt7996/mcu.c
@@ -1039,6 +1039,108 @@ mt7996_mcu_rx_ps_sync(struct mt7996_dev *dev, struct sk_buff *skb)
 	}
 }
 
+static void
+mt7996_spectrum_copy_samples(struct mt7996_dev *dev,
+			     struct mt7996_mcu_spectrum_data_event *event)
+	__must_hold(&dev->spectrum.lock)
+{
+	u32 data_len, pkt_num, base, end;
+
+	data_len = le32_to_cpu(event->data_len);
+	pkt_num = le32_to_cpu(event->pkt_num);
+
+	if (check_mul_overflow(pkt_num, MT7996_SPECTRUM_EVENT_DATA_LEN, &base) ||
+	    check_add_overflow(base, data_len, &end) ||
+	    end > MT7996_SPECTRUM_MAX_SAMPLES * 2)
+		return;
+
+	static_assert(MT7996_SPECTRUM_MAX_SAMPLES * 2 <=
+		      MT7996_SPECTRUM_EVENT_DATA_LEN * BITS_PER_TYPE(u32));
+	if (dev->spectrum.rx.pkt_mask & BIT(pkt_num)) {
+		dev->spectrum.rx.invalid_event = true;
+		return;
+	}
+
+	dev->spectrum.rx.pkt_mask |= BIT(pkt_num);
+	dev->spectrum.rx.received += data_len;
+
+	static_assert(sizeof(*dev->spectrum.buf) == sizeof(*event->data));
+	memcpy(&dev->spectrum.buf[base], &event->data,
+	       data_len * sizeof(*dev->spectrum.buf));
+}
+
+static void
+mt7996_mcu_rx_spectrum_data_event(struct mt7996_dev *dev,
+				  struct mt7996_mcu_spectrum_data_event *event)
+{
+	u32 data_len = le32_to_cpu(event->data_len);
+
+	/* Data events have to be checked to have occurred while the mutex is
+	 * held while mt7996_mcu_spectrum_get_data() is waiting for events. This
+	 * is facilitated by the spinlock, data_requested flag and
+	 * all_data_arrived completion.
+	 */
+	spin_lock(&dev->spectrum.lock);
+	if (!dev->spectrum.rx.data_requested)
+		goto out;
+
+	/* MT7996_SPECTRUM_EVENT_DATA_LEN __le32 values (alternating I and Q)
+	 * per event. data_len == 0 marks end of dump.
+	 */
+	if (data_len) {
+		mt7996_spectrum_copy_samples(dev, event);
+	} else {
+		dev->spectrum.rx.data_requested = false;
+		complete(&dev->spectrum.all_data_arrived);
+	}
+
+out:
+	spin_unlock(&dev->spectrum.lock);
+}
+
+static void
+mt7996_mcu_rx_spectrum_data_event_check(struct mt7996_dev *dev,
+					struct sk_buff *skb)
+{
+	const size_t header_size = offsetof(struct mt7996_mcu_spectrum_data_event, data);
+	struct mt7996_mcu_spectrum_data_event *e;
+	u32 avail_len, data_len;
+
+	if (skb->len < header_size)
+		return;
+
+	e = (struct mt7996_mcu_spectrum_data_event *)skb->data;
+
+	data_len = le32_to_cpu(e->data_len);
+	avail_len = (skb->len - header_size) / sizeof(__le32);
+	if (data_len > avail_len || data_len > MT7996_SPECTRUM_EVENT_DATA_LEN)
+		return;
+
+	mt7996_mcu_rx_spectrum_data_event(dev, e);
+}
+
+static void
+mt7996_mcu_rx_spectrum_event(struct mt7996_dev *dev, struct sk_buff *skb)
+{
+	struct mt7996_mcu_spectrum_event *event;
+	u16 tag;
+
+	skb_pull(skb, sizeof(struct mt7996_mcu_rxd));
+	if (skb->len < sizeof(struct mt7996_mcu_spectrum_event))
+		return;
+
+	event = (struct mt7996_mcu_spectrum_event *)skb->data;
+	tag = le16_to_cpu(event->tag);
+
+	switch (tag) {
+	case UNI_EVENT_SPECTRUM_DATA:
+		mt7996_mcu_rx_spectrum_data_event_check(dev, skb);
+		break;
+	default:
+		break;
+	}
+}
+
 static void
 mt7996_mcu_uni_rx_unsolicited_event(struct mt7996_dev *dev, struct sk_buff *skb)
 {
@@ -1063,6 +1165,9 @@ mt7996_mcu_uni_rx_unsolicited_event(struct mt7996_dev *dev, struct sk_buff *skb)
 	case MCU_UNI_EVENT_PS_SYNC:
 		mt7996_mcu_rx_ps_sync(dev, skb);
 		break;
+	case MCU_UNI_EVENT_SPECTRUM:
+		mt7996_mcu_rx_spectrum_event(dev, skb);
+		break;
 	default:
 		break;
 	}
@@ -6064,3 +6169,244 @@ int mt7996_mcu_set_dup_wtbl(struct mt7996_dev *dev)
 	return mt76_mcu_send_msg(&dev->mt76, MCU_WM_UNI_CMD(CHIP_CONFIG), &req,
 				 sizeof(req), true);
 }
+
+static int mt7996_spectrum_chan_bw(struct cfg80211_chan_def *chandef,
+				   enum mt7996_spectrum_bw *bw)
+{
+	u8 chan_bw = mt76_connac_chan_bw(chandef);
+
+	switch (chan_bw) {
+	case CMD_CBW_20MHZ:
+		*bw = MT7996_SPECTRUM_BW_20;
+		break;
+	case CMD_CBW_40MHZ:
+		*bw = MT7996_SPECTRUM_BW_40;
+		break;
+	case CMD_CBW_80MHZ:
+		*bw = MT7996_SPECTRUM_BW_80;
+		break;
+	case CMD_CBW_160MHZ:
+		*bw = MT7996_SPECTRUM_BW_160;
+		break;
+	case CMD_CBW_320MHZ:
+		*bw = MT7996_SPECTRUM_BW_320;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static int mt7996_spectrum_prepare_params(struct mt7996_dev *dev)
+	__must_hold(&dev->mt76.mutex)
+	__must_hold(&dev->spectrum.mutex)
+{
+	enum mt7996_spectrum_bw bw, chandef_bw;
+	enum mt7996_spectrum_gain gain;
+	enum nl80211_band band;
+	struct mt7996_phy *phy;
+	u32 samples;
+	int ret = 0;
+
+	samples = READ_ONCE(dev->spectrum.user.samples);
+	band = READ_ONCE(dev->spectrum.user.band);
+	bw = READ_ONCE(dev->spectrum.user.bw);
+	gain = READ_ONCE(dev->spectrum.user.gain);
+
+	phy = mt7996_band_phy(dev, band);
+	if (!phy)
+		return -EINVAL;
+
+	if (!test_bit(MT76_STATE_RUNNING, &phy->mt76->state))
+		return -ENETDOWN;
+
+	/* setting the capture bandwidth higher than the bandwidth the
+	 * channel is configured with results in invalid data (every
+	 * sample duplicated)
+	 */
+
+	ret = mt7996_spectrum_chan_bw(&phy->mt76->chandef, &chandef_bw);
+	if (ret)
+		return ret;
+
+	if (bw == MT7996_SPECTRUM_BW_AUTO)
+		bw = chandef_bw;
+	else if (bw > chandef_bw)
+		return -EINVAL;
+
+	dev->spectrum.curr.timestamp    = ktime_get_boottime_ns();
+	dev->spectrum.curr.capture_node = MT7996_SPECTRUM_CAP_NODE_WF0;
+	dev->spectrum.curr.center_freq  = phy->mt76->chandef.center_freq1;
+	dev->spectrum.curr.samples      = min(samples,
+					      MT7996_SPECTRUM_MAX_SAMPLES);
+	dev->spectrum.curr.band         = band;
+	dev->spectrum.curr.bw           = bw;
+	dev->spectrum.curr.gain         = gain;
+	dev->spectrum.curr.band_idx     = phy->mt76->band_idx;
+
+	return ret;
+}
+
+/**
+ * mt7996_mcu_spectrum_set_param() - request and execute a spectrum capture
+ * @dev: mt7996 device
+ *
+ * This waits on the MCU to finish the capture. On success, the MCU filled its
+ * internal capture buffer.
+ *
+ * If called consecutively without reading the buffer, the unread data gets
+ * lost.
+ *
+ * Context: Takes and releases @dev->mt76.mutex.
+ *
+ * Return: %0 on success, %-Exxx on error
+ */
+static int mt7996_mcu_spectrum_set_param(struct mt7996_dev *dev)
+	__must_hold(&dev->spectrum.mutex)
+{
+	struct mt7996_mcu_spectrum_status_event *event;
+	struct mt7996_mcu_spectrum_set_param req = {
+		.tag = cpu_to_le16(UNI_CMD_SPECTRUM_SET_PARAM),
+		.len = cpu_to_le16(sizeof(req) - 4),
+
+		.trigger = cpu_to_le32(1),
+		.en_bit_width = cpu_to_le32(MT7996_SPECTRUM_CAP_96_BIT),
+		.architech = cpu_to_le32(MT7996_SPECTRUM_CAP_ON_CHIP),
+	};
+	struct sk_buff *skb;
+	int ret;
+
+	/* channel must not switch between preparing the parameters and
+	 * executing the capture command
+	 */
+
+	mutex_lock(&dev->mt76.mutex);
+
+	ret = mt7996_spectrum_prepare_params(dev);
+	if (ret)
+		goto out;
+
+	req.capture_node = cpu_to_le32(dev->spectrum.curr.capture_node);
+	req.capture_len = cpu_to_le32(dev->spectrum.curr.samples);
+	/* because of DBDC: firmware wants 0 in band_idx, actual band in phy_idx */
+	req.band_idx = cpu_to_le32(0);
+	req.phy_idx = cpu_to_le32(dev->spectrum.curr.band_idx);
+	req.bw = cpu_to_le32(dev->spectrum.curr.bw);
+	req.fix_rx_gain = cpu_to_le32(dev->spectrum.curr.gain);
+
+	ret = mt76_mcu_send_and_get_msg(&dev->mt76, MCU_WM_UNI_CMD(SPECTRUM),
+					&req, sizeof(req), true, &skb);
+	if (ret)
+		goto out_print;
+
+	if (skb->len < sizeof(struct mt7996_mcu_spectrum_status_event)) {
+		ret = -EPROTO;
+		goto out_free_skb;
+	}
+
+	event = (struct mt7996_mcu_spectrum_status_event *)skb->data;
+	if (!le32_to_cpu(event->cap_done)) {
+		ret = -EIO;
+		goto out_free_skb;
+	}
+
+out_free_skb:
+	dev_kfree_skb(skb);
+
+out_print:
+	dev_dbg(dev->mt76.dev,
+		"spectrum: SET_PARAM(node = %u, len = %u, band = %u, bw = %u, gain = %u) -> %d\n",
+		dev->spectrum.curr.capture_node, dev->spectrum.curr.samples,
+		dev->spectrum.curr.band_idx, dev->spectrum.curr.bw,
+		dev->spectrum.curr.gain, ret);
+
+out:
+	mutex_unlock(&dev->mt76.mutex);
+	return ret;
+}
+
+/**
+ * mt7996_mcu_spectrum_get_data() - process latest capture data
+ * @dev: mt7996 device
+ *
+ * Return: %0 on success, %-EAGAIN on invalid/incomplete response,
+ *         %-Exxx on error
+ */
+static int mt7996_mcu_spectrum_get_data(struct mt7996_dev *dev)
+	__must_hold(&dev->spectrum.mutex)
+{
+	struct mt7996_mcu_spectrum_get_data req = {
+		.tag = cpu_to_le16(UNI_CMD_SPECTRUM_GET_DATA),
+		.len = cpu_to_le16(sizeof(req) - 4),
+	};
+	unsigned long completed = 0;
+	int ret;
+
+	reinit_completion(&dev->spectrum.all_data_arrived);
+
+	spin_lock_bh(&dev->spectrum.lock);
+	memset(dev->spectrum.buf, 0,
+	       MT7996_SPECTRUM_MAX_SAMPLES * 2 * sizeof(*dev->spectrum.buf));
+	dev->spectrum.rx.data_requested = true;
+	dev->spectrum.rx.invalid_event = false;
+	dev->spectrum.rx.pkt_mask = 0;
+	dev->spectrum.rx.received = 0;
+	spin_unlock_bh(&dev->spectrum.lock);
+
+	ret = mt76_mcu_send_msg(&dev->mt76, MCU_WM_UNI_CMD(SPECTRUM),
+				&req, sizeof(req), false);
+	if (ret)
+		goto out;
+
+	completed = wait_for_completion_timeout(&dev->spectrum.all_data_arrived,
+						msecs_to_jiffies(100));
+
+out:
+	spin_lock_bh(&dev->spectrum.lock);
+	dev->spectrum.rx.data_requested = false;
+	/* Not every corner case of invalid data events can ever be handled
+	 * because there is no identifier from GET_DATA preserved in the data
+	 * events. After having checked most invalid states, arriving events
+	 * have to be assumed to be valid. Invalid firmware behavior regarding
+	 * spectrum data events hasn't been observed yet.
+	 */
+	if (!ret && (!completed ||
+		     dev->spectrum.rx.invalid_event ||
+		     dev->spectrum.rx.received != dev->spectrum.curr.samples * 2))
+		ret = -EAGAIN;
+	spin_unlock_bh(&dev->spectrum.lock);
+
+	dev_dbg(dev->mt76.dev, "spectrum: GET_DATA -> %d\n", ret);
+	return ret;
+}
+
+/**
+ * mt7996_mcu_spectrum_capture() - do a spectrum capture
+ * @dev: mt7996 device
+ *
+ * Captures spectrum data and waits until all data has been received and
+ * processed.
+ *
+ * Context: Sleeps. Must hold @dev->spectrum.mutex.
+ *
+ * Return: %0 on success, %-EAGAIN on invalid/incomplete response,
+ *         %-Exxx on error
+ *
+ * After an %-EAGAIN error, before calling mt7996_mcu_spectrum_capture() again,
+ * caller should wait some time to make %-EAGAIN reappearing less likely.
+ * %-EAGAIN may be returned indefinitely.
+ */
+int mt7996_mcu_spectrum_capture(struct mt7996_dev *dev)
+	__must_hold(&dev->spectrum.mutex)
+{
+	int ret;
+
+	ret = mt7996_mcu_spectrum_set_param(dev);
+	if (ret)
+		return ret;
+
+	ret = mt7996_mcu_spectrum_get_data(dev);
+
+	return ret;
+}
diff --git a/drivers/net/wireless/mediatek/mt76/mt7996/mcu.h b/drivers/net/wireless/mediatek/mt76/mt7996/mcu.h
index a39ed2dc9e65..bd04ff7ea020 100644
--- a/drivers/net/wireless/mediatek/mt76/mt7996/mcu.h
+++ b/drivers/net/wireless/mediatek/mt76/mt7996/mcu.h
@@ -1198,4 +1198,99 @@ struct fixed_rate_table_ctrl {
 	u8 _rsv2;
 } __packed;
 
+enum {
+	UNI_CMD_SPECTRUM_SET_PARAM,
+	UNI_CMD_SPECTRUM_GET_STATUS,
+	UNI_CMD_SPECTRUM_GET_DATA,
+};
+
+enum {
+	UNI_EVENT_SPECTRUM_STATUS,
+	UNI_EVENT_SPECTRUM_DATA,
+};
+
+#define MT7996_SPECTRUM_CAP_NODE_WF0	0x10504000	/* available on all chips */
+#define MT7996_SPECTRUM_CAP_96_BIT	1
+#define MT7996_SPECTRUM_CAP_ON_CHIP	0
+
+struct mt7996_mcu_spectrum_set_param {
+	u8 _rsv[4];
+
+	__le16 tag;
+	__le16 len;
+
+	__le32 trigger;
+	__le32 ring_cap_en;
+	__le32 trigger_event;
+	__le32 capture_node;
+	__le32 capture_len;
+	__le32 cap_stop_cycle;
+	__le32 mac_trigger_event;
+	__le32 source_addr_lsb;
+	__le32 source_addr_msb;
+	__le32 band_idx;
+	__le32 bw;
+	__le32 en_bit_width;
+	__le32 architech;
+	__le32 phy_idx;
+	__le32 emi_start_addr;
+	__le32 emi_end_addr;
+	__le32 emi_msb_addr;
+	__le32 cap_source;
+	__le32 wifi_path;
+	__le32 fix_rx_gain;
+	__le32 rsv[2];
+} __packed;
+
+struct mt7996_mcu_spectrum_get_data {
+	u8 _rsv[4];
+
+	__le16 tag;
+	__le16 len;
+
+	__le32 addr;
+	__le32 addr_offset;
+	__le32 bank;
+	__le32 bank_size;
+	__le32 wf_num;
+	__le32 iq_type;
+	__le32 rsv[6];
+} __packed;
+
+struct mt7996_mcu_spectrum_event {
+	u8 _rsv[4];
+
+	__le16 tag;
+	__le16 len;
+} __packed;
+
+struct mt7996_mcu_spectrum_status_event {
+	u8 _rsv[4];
+
+	__le16 tag;
+	__le16 len;
+
+	__le32 func_index;
+	__le32 cap_done;
+	__le32 _rsv2[15];
+} __packed;
+
+#define MT7996_SPECTRUM_EVENT_DATA_LEN	256
+
+struct mt7996_mcu_spectrum_data_event {
+	u8 _rsv[4];
+
+	__le16 tag;
+	__le16 len;
+
+	__le32 func_index;
+	__le32 pkt_num;
+	__le32 bank;
+	__le32 data_len;	/* 2 * number of samples */
+	__le32 wf_cnt;
+	__le32 samp_cnt;	/* not the sample count, despite the name */
+	__le32 _rsv2[6];
+	__le32 data[MT7996_SPECTRUM_EVENT_DATA_LEN];
+} __packed;
+
 #endif
diff --git a/drivers/net/wireless/mediatek/mt76/mt7996/mt7996.h b/drivers/net/wireless/mediatek/mt76/mt7996/mt7996.h
index fda43d2ae620..65278c70f229 100644
--- a/drivers/net/wireless/mediatek/mt76/mt7996/mt7996.h
+++ b/drivers/net/wireless/mediatek/mt76/mt7996/mt7996.h
@@ -8,8 +8,10 @@
 
 #include <linux/interrupt.h>
 #include <linux/ktime.h>
+#include <linux/completion.h>
 #include "../mt76_connac.h"
 #include "regs.h"
+#include "spectrum.h"
 
 #define MT7996_MAX_RADIOS		3
 #define MT7996_MAX_INTERFACES		19	/* per-band */
@@ -546,6 +548,106 @@ struct mt7996_dev {
 		u8 type:4;
 		u8 fem:4;
 	} var;
+
+	struct {
+		/** @mutex: must be hold throughout a complete capture */
+		struct mutex mutex;
+
+		/** @rearm_work: captures and emits data */
+		struct delayed_work rearm_work;
+
+		/** @relay: capture data output */
+		struct rchan *relay __guarded_by(&mutex);
+
+		/** @dead: %true if setting trigger is deactivated */
+		bool dead __guarded_by(&mutex);
+
+		/**
+		 * @trigger: current trigger state which controls the behavior
+		 *           of @rearm_work
+		 */
+		enum mt7996_spectrum_trigger trigger __guarded_by(&mutex);
+
+		/** @lock: serializes spectrum event handling */
+		spinlock_t lock;
+
+		/** @buf: buffer for stitching together received data */
+		__le32 *buf __pt_guarded_by(&lock);
+
+		/**
+		 * @all_data_arrived: completes after receiving the
+		 *                    finishing spectrum data event
+		 */
+		struct completion all_data_arrived;
+
+		/** @run: state of the current run */
+		struct {
+			/**
+			 * @seq: sequence number of the current capture since
+			 *       the last restart of @rearm_work
+			 */
+			u32 seq;
+
+			/**
+			 * @retries: consecutive failed attempts before stopping
+			 */
+			u32 retries;
+		} run __guarded_by(&mutex);
+
+		/**
+		 * @rx: state of the current spectrum data request and its
+		 *      spectrum data event responses
+		 */
+		struct {
+			/**
+			 * @data_requested: %true if spectrum data events are
+			 *                  currently expected to arrive. While
+			 *                  %false, all spectrum data events
+			 *                  will be ignored.
+			 */
+			bool data_requested:1;
+
+			/** @invalid_event: unreconcilably invalid data event */
+			bool invalid_event:1;
+
+			/**
+			 * @pkt_mask: data events (by pkt_num) seen for this
+			 *            request
+			 */
+			u32 pkt_mask;
+
+			/**
+			 * @received: number of I/Q values copied for this
+			 *            request
+			 */
+			u32 received;
+		} rx __guarded_by(&lock);
+
+		/** @curr: committed parameters of the current request */
+		struct {
+			u64 timestamp;
+			u32 capture_node;
+			u32 center_freq;
+			u32 samples;
+			enum nl80211_band band;
+			enum mt7996_spectrum_bw bw;
+			enum mt7996_spectrum_gain gain;
+			enum mt76_band_id band_idx;
+		} curr __guarded_by(&mutex);
+
+		/**
+		 * @user: parameters exposed through debugfs
+		 *
+		 * Must be accessed through READ_ONCE()/WRITE_ONCE().
+		 */
+		struct {
+			u32 repeat;
+			u32 samples;
+			enum nl80211_band band;
+			enum mt7996_spectrum_bw bw;
+			enum mt7996_spectrum_gain gain;
+		} user;
+	} spectrum;
 };
 
 enum {
@@ -985,6 +1087,9 @@ int mt7996_dfs_init_radar_detector(struct mt7996_phy *phy);
 void mt7996_set_stream_he_eht_caps(struct mt7996_phy *phy);
 void mt7996_set_stream_vht_txbf_caps(struct mt7996_phy *phy);
 void mt7996_update_channel(struct mt76_phy *mphy);
+int mt7996_mcu_spectrum_capture(struct mt7996_dev *dev);
+void mt7996_spectrum_init(struct mt7996_dev *dev);
+void mt7996_spectrum_free(struct mt7996_dev *dev);
 int mt7996_init_debugfs(struct mt7996_dev *dev);
 void mt7996_debugfs_rx_fw_monitor(struct mt7996_dev *dev, const void *data, int len);
 bool mt7996_debugfs_rx_log(struct mt7996_dev *dev, const void *data, int len);
diff --git a/drivers/net/wireless/mediatek/mt76/mt7996/spectrum.c b/drivers/net/wireless/mediatek/mt76/mt7996/spectrum.c
new file mode 100644
index 000000000000..aacbcf8fd5a3
--- /dev/null
+++ b/drivers/net/wireless/mediatek/mt76/mt7996/spectrum.c
@@ -0,0 +1,668 @@
+// SPDX-License-Identifier: BSD-3-Clause-Clear
+
+#include <linux/relay.h>
+#include <linux/debugfs.h>
+#include <linux/completion.h>
+#include "mt7996.h"
+
+#define MT7996_SPECTRUM_DEFAULT_DELAY		1
+#define MT7996_SPECTRUM_CHANNEL_FULL_DELAY	(HZ / 25)
+#define MT7996_SPECTRUM_RETRY_DELAY		(HZ / 2)
+#define MT7996_SPECTRUM_MAX_RETRIES		3
+#define MT7996_SPECTRUM_ADC_BITS		14
+#define MT7996_SPECTRUM_OVERSAMPLING		2
+#define MT7996_SPECTRUM_MAX_SAMPLERATE		320
+
+static int mt7996_spectrum_repeat_get(void *data, u64 *val)
+{
+	struct mt7996_dev *dev = data;
+
+	*val = READ_ONCE(dev->spectrum.user.repeat);
+
+	return 0;
+}
+
+static int mt7996_spectrum_repeat_set(void *data, u64 val)
+{
+	struct mt7996_dev *dev = data;
+
+	if (val > U32_MAX)
+		return -EINVAL;
+
+	WRITE_ONCE(dev->spectrum.user.repeat, val);
+
+	return 0;
+}
+
+DEFINE_DEBUGFS_ATTRIBUTE(fops_spectrum_repeat, mt7996_spectrum_repeat_get,
+			 mt7996_spectrum_repeat_set, "%llu\n");
+
+static int mt7996_spectrum_samples_get(void *data, u64 *val)
+{
+	struct mt7996_dev *dev = data;
+
+	*val = READ_ONCE(dev->spectrum.user.samples);
+
+	return 0;
+}
+
+static int mt7996_spectrum_samples_set(void *data, u64 val)
+{
+	struct mt7996_dev *dev = data;
+
+	if (val < 1 || val > MT7996_SPECTRUM_MAX_SAMPLES)
+		return -EINVAL;
+
+	WRITE_ONCE(dev->spectrum.user.samples, val);
+
+	return 0;
+}
+
+DEFINE_DEBUGFS_ATTRIBUTE(fops_spectrum_samples,
+			 mt7996_spectrum_samples_get,
+			 mt7996_spectrum_samples_set, "%llu\n");
+
+static ssize_t
+mt7996_spectrum_band_read(struct file *file, char __user *user_buf,
+			  size_t count, loff_t *ppos)
+{
+	struct mt7996_dev *dev = file->private_data;
+	enum nl80211_band band;
+	const char *string;
+	size_t len;
+
+	band = READ_ONCE(dev->spectrum.user.band);
+
+	switch (band) {
+	case NL80211_BAND_2GHZ:
+		string = "2\n";
+		break;
+	case NL80211_BAND_5GHZ:
+		string = "5\n";
+		break;
+	case NL80211_BAND_6GHZ:
+		string = "6\n";
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	len = strlen(string);
+	return simple_read_from_buffer(user_buf, count, ppos, string, len);
+}
+
+static ssize_t
+mt7996_spectrum_band_write(struct file *file, const char __user *user_buf,
+			   size_t count, loff_t *ppos)
+{
+	struct mt7996_dev *dev = file->private_data;
+	enum nl80211_band band;
+	char buf[32];
+
+	if (count > sizeof(buf) - 1)
+		return -EINVAL;
+
+	if (copy_from_user(buf, user_buf, count))
+		return -EFAULT;
+
+	buf[count] = '\0';
+
+	if (sysfs_streq(buf, "2"))
+		band = NL80211_BAND_2GHZ;
+	else if (sysfs_streq(buf, "5"))
+		band = NL80211_BAND_5GHZ;
+	else if (sysfs_streq(buf, "6"))
+		band = NL80211_BAND_6GHZ;
+	else
+		return -EINVAL;
+
+	WRITE_ONCE(dev->spectrum.user.band, band);
+
+	return count;
+}
+
+static const struct file_operations fops_spectrum_band = {
+	.read = mt7996_spectrum_band_read,
+	.write = mt7996_spectrum_band_write,
+	.open = simple_open,
+	.owner = THIS_MODULE,
+	.llseek = default_llseek,
+};
+
+static ssize_t
+mt7996_spectrum_bw_read(struct file *file, char __user *user_buf,
+			size_t count, loff_t *ppos)
+{
+	struct mt7996_dev *dev = file->private_data;
+	enum mt7996_spectrum_bw bw;
+	const char *string;
+	size_t len;
+
+	bw = READ_ONCE(dev->spectrum.user.bw);
+
+	switch (bw) {
+	case MT7996_SPECTRUM_BW_20:
+		string = "20\n";
+		break;
+	case MT7996_SPECTRUM_BW_40:
+		string = "40\n";
+		break;
+	case MT7996_SPECTRUM_BW_80:
+		string = "80\n";
+		break;
+	case MT7996_SPECTRUM_BW_160:
+		string = "160\n";
+		break;
+	case MT7996_SPECTRUM_BW_320:
+		string = "320\n";
+		break;
+	case MT7996_SPECTRUM_BW_AUTO:
+		string = "auto\n";
+		break;
+	}
+
+	len = strlen(string);
+	return simple_read_from_buffer(user_buf, count, ppos, string, len);
+}
+
+static ssize_t
+mt7996_spectrum_bw_write(struct file *file, const char __user *user_buf,
+			 size_t count, loff_t *ppos)
+{
+	struct mt7996_dev *dev = file->private_data;
+	enum mt7996_spectrum_bw bw;
+	char buf[32];
+
+	if (count > sizeof(buf) - 1)
+		return -EINVAL;
+
+	if (copy_from_user(buf, user_buf, count))
+		return -EFAULT;
+
+	buf[count] = '\0';
+
+	if (sysfs_streq(buf, "20"))
+		bw = MT7996_SPECTRUM_BW_20;
+	else if (sysfs_streq(buf, "40"))
+		bw = MT7996_SPECTRUM_BW_40;
+	else if (sysfs_streq(buf, "80"))
+		bw = MT7996_SPECTRUM_BW_80;
+	else if (sysfs_streq(buf, "160"))
+		bw = MT7996_SPECTRUM_BW_160;
+	else if (sysfs_streq(buf, "320"))
+		bw = MT7996_SPECTRUM_BW_320;
+	else if (sysfs_streq(buf, "auto"))
+		bw = MT7996_SPECTRUM_BW_AUTO;
+	else
+		return -EINVAL;
+
+	WRITE_ONCE(dev->spectrum.user.bw, bw);
+
+	return count;
+}
+
+static const struct file_operations fops_spectrum_bw = {
+	.read = mt7996_spectrum_bw_read,
+	.write = mt7996_spectrum_bw_write,
+	.open = simple_open,
+	.owner = THIS_MODULE,
+	.llseek = default_llseek,
+};
+
+static ssize_t
+mt7996_spectrum_gain_read(struct file *file, char __user *user_buf,
+			  size_t count, loff_t *ppos)
+{
+	struct mt7996_dev *dev = file->private_data;
+	enum mt7996_spectrum_gain gain;
+	const char *string;
+	size_t len;
+
+	gain = READ_ONCE(dev->spectrum.user.gain);
+
+	switch (gain) {
+	case MT7996_SPECTRUM_GAIN_AUTO:
+		string = "auto\n";
+		break;
+	case MT7996_SPECTRUM_GAIN_HIGH:
+		string = "high\n";
+		break;
+	case MT7996_SPECTRUM_GAIN_MID:
+		string = "mid\n";
+		break;
+	case MT7996_SPECTRUM_GAIN_LOW:
+		string = "low\n";
+		break;
+	}
+
+	len = strlen(string);
+	return simple_read_from_buffer(user_buf, count, ppos, string, len);
+}
+
+static ssize_t
+mt7996_spectrum_gain_write(struct file *file, const char __user *user_buf,
+			   size_t count, loff_t *ppos)
+{
+	struct mt7996_dev *dev = file->private_data;
+	enum mt7996_spectrum_gain gain;
+	char buf[32];
+
+	if (count > sizeof(buf) - 1)
+		return -EINVAL;
+
+	if (copy_from_user(buf, user_buf, count))
+		return -EFAULT;
+
+	buf[count] = '\0';
+
+	if (sysfs_streq(buf, "auto"))
+		gain = MT7996_SPECTRUM_GAIN_AUTO;
+	else if (sysfs_streq(buf, "high"))
+		gain = MT7996_SPECTRUM_GAIN_HIGH;
+	else if (sysfs_streq(buf, "mid"))
+		gain = MT7996_SPECTRUM_GAIN_MID;
+	else if (sysfs_streq(buf, "low"))
+		gain = MT7996_SPECTRUM_GAIN_LOW;
+	else
+		return -EINVAL;
+
+	WRITE_ONCE(dev->spectrum.user.gain, gain);
+
+	return count;
+}
+
+static const struct file_operations fops_spectrum_gain = {
+	.read = mt7996_spectrum_gain_read,
+	.write = mt7996_spectrum_gain_write,
+	.open = simple_open,
+	.owner = THIS_MODULE,
+	.llseek = default_llseek,
+};
+
+static struct dentry *
+create_buf_file_cb(const char *filename, struct dentry *parent, umode_t mode,
+		   struct rchan_buf *buf, int *is_global)
+{
+	*is_global = 1;
+
+	return debugfs_create_file(filename, mode, parent, buf,
+				   &relay_file_operations);
+}
+
+static int remove_buf_file_cb(struct dentry *f)
+{
+	debugfs_remove(f);
+
+	return 0;
+}
+
+static int mt7996_spectrum_relay_open(struct mt7996_dev *dev)
+	__must_hold(&dev->spectrum.mutex)
+{
+	static const struct rchan_callbacks relay_cb = {
+		.create_buf_file = create_buf_file_cb,
+		.remove_buf_file = remove_buf_file_cb,
+	};
+
+	dev->spectrum.relay = relay_open("spectrum_data", dev->debugfs_dir,
+					 MT7996_SPECTRUM_SUBBUF_SIZE,
+					 MT7996_SPECTRUM_SUBBUFS,
+					 &relay_cb, NULL);
+	if (!dev->spectrum.relay)
+		return -ENOMEM;
+
+	return 0;
+}
+
+static void mt7996_spectrum_relay_close(struct mt7996_dev *dev)
+	__must_hold(&dev->spectrum.mutex)
+{
+	if (!dev->spectrum.relay)
+		return;
+
+	relay_close(dev->spectrum.relay);
+	dev->spectrum.relay = NULL;
+}
+
+static int mt7996_spectrum_alloc(struct mt7996_dev *dev)
+	__must_hold(&dev->spectrum.mutex)
+{
+	__le32 *buf;
+	int ret;
+
+	if (dev->spectrum.buf)
+		return 0;
+
+	buf = devm_kcalloc(dev->mt76.dev, MT7996_SPECTRUM_MAX_SAMPLES * 2,
+			   sizeof(*buf), GFP_KERNEL);
+	if (!buf) {
+		ret = -ENOMEM;
+		goto err;
+	}
+
+	/* relay always NULL at this point, if buf was NULL */
+
+	ret = mt7996_spectrum_relay_open(dev);
+	if (ret)
+		goto err_free_buf;
+
+	dev->spectrum.buf = buf;
+
+	return 0;
+
+err_free_buf:
+	devm_kfree(dev->mt76.dev, buf);
+err:
+	dev_warn_once(dev->mt76.dev,
+		      "spectrum: error while allocating buffers\n");
+	return ret;
+}
+
+static int mt7996_spectrum_trigger_set(struct mt7996_dev *dev,
+				       enum mt7996_spectrum_trigger trigger)
+{
+	int ret = 0;
+
+	cancel_delayed_work_sync(&dev->spectrum.rearm_work);
+
+	mutex_lock(&dev->spectrum.mutex);
+
+	if (dev->spectrum.dead) {
+		ret = -ENODEV;
+		goto out;
+	}
+
+	if (trigger == MT7996_SPECTRUM_TRIGGER_DISABLE) {
+		if (dev->spectrum.relay)
+			relay_flush(dev->spectrum.relay);
+		dev->spectrum.trigger = trigger;
+		goto out;
+	}
+
+	ret = mt7996_spectrum_alloc(dev);
+	if (ret)
+		goto out;
+
+	relay_reset(dev->spectrum.relay);
+
+	dev->spectrum.trigger = trigger;
+	dev->spectrum.run.seq = 0;
+	dev->spectrum.run.retries = 0;
+	queue_delayed_work(system_dfl_wq, &dev->spectrum.rearm_work, 0);
+
+out:
+	mutex_unlock(&dev->spectrum.mutex);
+	return ret;
+}
+
+static ssize_t
+mt7996_spectrum_trigger_write(struct file *file, const char __user *user_buf,
+			      size_t count, loff_t *ppos)
+{
+	struct mt7996_dev *dev = file->private_data;
+	enum mt7996_spectrum_trigger trigger;
+	char buf[32];
+	int ret;
+
+	if (count > sizeof(buf) - 1)
+		return -EINVAL;
+
+	if (copy_from_user(buf, user_buf, count))
+		return -EFAULT;
+
+	buf[count] = '\0';
+
+	if (sysfs_streq(buf, "disable"))
+		trigger = MT7996_SPECTRUM_TRIGGER_DISABLE;
+	else if (sysfs_streq(buf, "manual"))
+		trigger = MT7996_SPECTRUM_TRIGGER_MANUAL;
+	else if (sysfs_streq(buf, "background"))
+		trigger = MT7996_SPECTRUM_TRIGGER_BACKGROUND;
+	else
+		return -EINVAL;
+
+	ret = mt7996_spectrum_trigger_set(dev, trigger);
+	if (ret)
+		return ret;
+
+	return count;
+}
+
+static ssize_t
+mt7996_spectrum_trigger_read(struct file *file, char __user *user_buf,
+			     size_t count, loff_t *ppos)
+{
+	struct mt7996_dev *dev = file->private_data;
+	enum mt7996_spectrum_trigger trigger;
+	const char *string;
+	size_t len;
+
+	if (mutex_lock_interruptible(&dev->spectrum.mutex))
+		return -ERESTARTSYS;
+
+	trigger = dev->spectrum.trigger;
+	mutex_unlock(&dev->spectrum.mutex);
+
+	switch (trigger) {
+	case MT7996_SPECTRUM_TRIGGER_DISABLE:
+		string = "disable\n";
+		break;
+	case MT7996_SPECTRUM_TRIGGER_MANUAL:
+		string = "manual\n";
+		break;
+	case MT7996_SPECTRUM_TRIGGER_BACKGROUND:
+		string = "background\n";
+		break;
+	}
+
+	len = strlen(string);
+	return simple_read_from_buffer(user_buf, count, ppos, string, len);
+}
+
+static const struct file_operations fops_spectrum_trigger = {
+	.read = mt7996_spectrum_trigger_read,
+	.write = mt7996_spectrum_trigger_write,
+	.open = simple_open,
+	.owner = THIS_MODULE,
+	.llseek = default_llseek,
+};
+
+static int mt7996_spectrum_bw_to_mhz(enum mt7996_spectrum_bw bw, u16 *mhz)
+{
+	switch (bw) {
+	case MT7996_SPECTRUM_BW_20:
+		*mhz = 20;
+		break;
+	case MT7996_SPECTRUM_BW_40:
+		*mhz = 40;
+		break;
+	case MT7996_SPECTRUM_BW_80:
+		*mhz = 80;
+		break;
+	case MT7996_SPECTRUM_BW_160:
+		*mhz = 160;
+		break;
+	case MT7996_SPECTRUM_BW_320:
+		*mhz = 320;
+		break;
+	default:
+		return -1;
+	}
+
+	return 0;
+}
+
+static u16 mt7996_spectrum_band_gain_db(enum nl80211_band band)
+{
+	return band == NL80211_BAND_2GHZ ? 46 : 44;
+}
+
+static u16 mt7996_spectrum_gain_db(u8 gain)
+{
+	const u16 gain_db[] = { 1, 55, 47, 39 };
+
+	if (gain < ARRAY_SIZE(gain_db))
+		return gain_db[gain];
+
+	return 1;
+}
+
+static int mt7996_spectrum_relay_emit(struct mt7996_dev *dev)
+	__must_hold(&dev->spectrum.mutex)
+{
+	struct mt7996_spectrum_capture capture = {
+		.tlv.type = MT7996_SPECTRUM_TYPE,
+	};
+	u16 samplerate;
+	u32 data_size;
+	u16 gain_mb;
+	u16 bw_mhz;
+	void *dest;
+
+	if (WARN(mt7996_spectrum_bw_to_mhz(dev->spectrum.curr.bw, &bw_mhz),
+		 "%s encountered unresolved bandwidth", __func__)) {
+		return -EINVAL;
+	}
+
+	samplerate = min(bw_mhz * MT7996_SPECTRUM_OVERSAMPLING,
+			 MT7996_SPECTRUM_MAX_SAMPLERATE);
+
+	gain_mb = 100 * (mt7996_spectrum_band_gain_db(dev->spectrum.curr.band) +
+			 mt7996_spectrum_gain_db(dev->spectrum.curr.gain));
+
+	capture.adc_bits = MT7996_SPECTRUM_ADC_BITS;
+	capture.seq = cpu_to_le32(dev->spectrum.run.seq);
+	capture.timestamp = cpu_to_le64(dev->spectrum.curr.timestamp);
+	capture.capture_node = cpu_to_le32(dev->spectrum.curr.capture_node);
+	capture.center_freq_mhz = cpu_to_le32(dev->spectrum.curr.center_freq);
+	capture.bandwidth_mhz = cpu_to_le16(bw_mhz);
+	capture.samplerate_mhz = cpu_to_le16(samplerate);
+	capture.gain_mb = cpu_to_le16(gain_mb);
+	capture.band = dev->spectrum.curr.band;
+
+	data_size = dev->spectrum.curr.samples * sizeof(capture.data[0]);
+
+	capture.tlv.length = cpu_to_be16(sizeof(capture) - sizeof(capture.tlv) +
+					 data_size);
+
+	dest = relay_reserve(dev->spectrum.relay, sizeof(capture) + data_size);
+	if (!dest) {
+		dev_warn_ratelimited(dev->mt76.dev,
+				     "spectrum: relay full, dropped capture %u\n",
+				     le32_to_cpu(capture.seq));
+		return -ENOBUFS;
+	}
+
+	memcpy(dest, &capture, sizeof(capture));
+	spin_lock_bh(&dev->spectrum.lock);
+	memcpy(dest + sizeof(capture), dev->spectrum.buf, data_size);
+	spin_unlock_bh(&dev->spectrum.lock);
+
+	return 0;
+}
+
+static void mt7996_spectrum_rearm_work(struct work_struct *work)
+{
+	struct mt7996_dev *dev = container_of(work, struct mt7996_dev,
+					      spectrum.rearm_work.work);
+	unsigned long delay = MT7996_SPECTRUM_DEFAULT_DELAY;
+	u32 repeat;
+	int ret;
+
+	mutex_lock(&dev->spectrum.mutex);
+
+	if (dev->spectrum.trigger == MT7996_SPECTRUM_TRIGGER_DISABLE)
+		goto out;
+
+	if (WARN_ON(!dev->spectrum.buf)) {
+		dev->spectrum.trigger = MT7996_SPECTRUM_TRIGGER_DISABLE;
+		goto out;
+	}
+
+	repeat = READ_ONCE(dev->spectrum.user.repeat);
+	if (dev->spectrum.run.seq >= repeat &&
+	    dev->spectrum.trigger != MT7996_SPECTRUM_TRIGGER_BACKGROUND) {
+		relay_flush(dev->spectrum.relay);
+		dev->spectrum.trigger = MT7996_SPECTRUM_TRIGGER_DISABLE;
+		goto out;
+	}
+
+	/* Test for MCU reset. If this doesn't catch a reset, the capture will
+	 * fail and stop anyway.
+	 */
+	if (test_bit(MT76_RESET, &dev->mphy.state) ||
+	    test_bit(MT76_MCU_RESET, &dev->mphy.state)) {
+		relay_flush(dev->spectrum.relay);
+		dev->spectrum.trigger = MT7996_SPECTRUM_TRIGGER_DISABLE;
+		goto out;
+	}
+
+	ret = mt7996_mcu_spectrum_capture(dev);
+	if (ret)
+		goto err;
+
+	ret = mt7996_spectrum_relay_emit(dev);
+	if (ret)
+		delay = MT7996_SPECTRUM_CHANNEL_FULL_DELAY;
+
+	dev->spectrum.run.seq++;
+	dev->spectrum.run.retries = 0;
+
+	goto requeue;
+
+err:
+	delay = MT7996_SPECTRUM_RETRY_DELAY;
+	dev->spectrum.run.retries++;
+	if (ret != -EAGAIN ||
+	    dev->spectrum.run.retries > MT7996_SPECTRUM_MAX_RETRIES) {
+		dev_warn(dev->mt76.dev, "spectrum: capture failed (%d), stopping after %d tries\n",
+			 dev->spectrum.run.retries, ret);
+		relay_flush(dev->spectrum.relay);
+		dev->spectrum.trigger = MT7996_SPECTRUM_TRIGGER_DISABLE;
+	}
+
+requeue:
+	queue_delayed_work(system_dfl_wq, &dev->spectrum.rearm_work, delay);
+
+out:
+	mutex_unlock(&dev->spectrum.mutex);
+}
+
+void mt7996_spectrum_init(struct mt7996_dev *dev)
+	__context_unsafe(init)
+{
+	struct dentry *dir = dev->debugfs_dir;
+
+	mutex_init(&dev->spectrum.mutex);
+	init_completion(&dev->spectrum.all_data_arrived);
+	INIT_DELAYED_WORK(&dev->spectrum.rearm_work,
+			  mt7996_spectrum_rearm_work);
+
+	WRITE_ONCE(dev->spectrum.user.repeat, 1);
+	WRITE_ONCE(dev->spectrum.user.samples, 256);
+	WRITE_ONCE(dev->spectrum.user.band, 0);
+	WRITE_ONCE(dev->spectrum.user.bw, MT7996_SPECTRUM_BW_20);
+	WRITE_ONCE(dev->spectrum.user.gain, MT7996_SPECTRUM_GAIN_HIGH);
+
+	debugfs_create_file_unsafe("spectrum_repeat", 0600, dir, dev,
+				   &fops_spectrum_repeat);
+	debugfs_create_file_unsafe("spectrum_samples", 0600, dir, dev,
+				   &fops_spectrum_samples);
+	debugfs_create_file("spectrum_band", 0600, dir, dev,
+			    &fops_spectrum_band);
+	debugfs_create_file("spectrum_bandwidth", 0600, dir, dev,
+			    &fops_spectrum_bw);
+	debugfs_create_file("spectrum_gain", 0600, dir, dev,
+			    &fops_spectrum_gain);
+	debugfs_create_file("spectrum_trigger", 0600, dir, dev,
+			    &fops_spectrum_trigger);
+}
+
+void mt7996_spectrum_free(struct mt7996_dev *dev)
+{
+	disable_delayed_work_sync(&dev->spectrum.rearm_work);
+	mutex_lock(&dev->spectrum.mutex);
+	dev->spectrum.dead = true;
+	mt7996_spectrum_relay_close(dev);
+	mutex_unlock(&dev->spectrum.mutex);
+}
diff --git a/drivers/net/wireless/mediatek/mt76/mt7996/spectrum.h b/drivers/net/wireless/mediatek/mt76/mt7996/spectrum.h
new file mode 100644
index 000000000000..ebe7797a3594
--- /dev/null
+++ b/drivers/net/wireless/mediatek/mt76/mt7996/spectrum.h
@@ -0,0 +1,102 @@
+/* SPDX-License-Identifier: BSD-3-Clause-Clear */
+
+#ifndef __MT7996_SPECTRUM_H
+#define __MT7996_SPECTRUM_H
+
+#include <linux/types.h>
+
+#define MT7996_SPECTRUM_SUBBUF_SIZE	(17 * 1024)
+#define MT7996_SPECTRUM_SUBBUFS		32
+
+enum mt7996_spectrum_bw {
+	MT7996_SPECTRUM_BW_20 = 0,
+	MT7996_SPECTRUM_BW_40,
+	MT7996_SPECTRUM_BW_80,
+	MT7996_SPECTRUM_BW_160,
+	MT7996_SPECTRUM_BW_320,
+	MT7996_SPECTRUM_BW_AUTO,
+};
+
+enum mt7996_spectrum_trigger {
+	MT7996_SPECTRUM_TRIGGER_DISABLE = 0,
+	MT7996_SPECTRUM_TRIGGER_MANUAL,
+	MT7996_SPECTRUM_TRIGGER_BACKGROUND,
+};
+
+enum mt7996_spectrum_gain {
+	MT7996_SPECTRUM_GAIN_AUTO = 0,
+	MT7996_SPECTRUM_GAIN_HIGH,
+	MT7996_SPECTRUM_GAIN_MID,
+	MT7996_SPECTRUM_GAIN_LOW,
+};
+
+/**
+ * MT7996_SPECTRUM_TYPE - MT7996 spectrum type
+ *
+ * Extension of &enum ath_fft_sample_type.
+ */
+#define MT7996_SPECTRUM_TYPE	5
+
+/**
+ * MT7996_SPECTRUM_MAX_SAMPLES - maximum number of samples in a single capture
+ */
+#define MT7996_SPECTRUM_MAX_SAMPLES	2048
+
+/**
+ * struct mt7996_spectrum_tlv - spectrum base data format
+ * @type: type different from &enum ath_fft_sample_type
+ * @length: number of bytes following the tlv header
+ *
+ * Analogous to &struct fft_sample_tlv with types &enum ath_fft_sample_type.
+ */
+struct mt7996_spectrum_tlv {
+	u8 type;	/* see ath_fft_sample */
+	__be16 length;
+} __packed;
+
+/**
+ * struct mt7996_spectrum_capture - spectrum data format
+ * @tlv:                with @tlv.type being %MT7996_SPECTRUM_TYPE
+ * @adc_bits:           number of bits of a full scale data value
+ * @seq:                sequence number, %0 on first capture after
+ *                      (re)triggering
+ * @timestamp:          time since boot in nanoseconds
+ * @capture_node:       data origin along the internal signal path
+ * @center_freq_mhz:    frequency (MHz) the radio was tuned to
+ * @bandwidth_mhz:      bandwidth (MHz) for which the capture was requested
+ * @samplerate_mhz:     samplerate (MHz) of the capture
+ * @gain_mb:            gain in mB applied before analog-digital conversion
+ * @band:               band, one of &enum nl80211_band
+ * @data:               raw I/Q values (signed)
+ *
+ * The number of I/Q pairs in @data is
+ * ``(tlv.length -
+ *    (sizeof(struct mt7996_spectrum_capture) -
+ *     sizeof(struct mt7996_spectrum_tlv))) / (2 * sizeof(__le32))``.
+ *
+ * @seq increases on each successful capture. A gap in the sequence numbers
+ * indicates that some captures have been dropped because the relay channel was
+ * full due to not being read from.
+ */
+struct mt7996_spectrum_capture {
+	struct mt7996_spectrum_tlv tlv;
+
+	u8 adc_bits;
+	__le32 seq;
+	__le64 timestamp;
+	__le32 capture_node;
+	__le32 center_freq_mhz;
+	__le16 bandwidth_mhz;
+	__le16 samplerate_mhz;
+	__le16 gain_mb;
+	u8 band;
+	/* private: padding */
+	u8 _rsv;
+	/* public: */
+
+	struct {
+		__le32 i, q;
+	} data[];
+} __packed;
+
+#endif /* __MT7996_SPECTRUM_H */

---
base-commit: 04f839dd9028a04413b99b45b50265715f3c40d3
change-id: 20261102-spectrum-scan-4922e8b3b9f6

Best regards,
--  
Benjamin Koerner <bk@simonwunderlich.de>


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH RFC mt76-next] wifi: mt76: mt7996: support spectrum scan / IQ capture
  2026-12-04 10:55 [PATCH RFC mt76-next] wifi: mt76: mt7996: support spectrum scan / IQ capture Benjamin Koerner
@ 2026-12-04 12:35 ` Sven Eckelmann
  0 siblings, 0 replies; 2+ messages in thread
From: Sven Eckelmann @ 2026-12-04 12:35 UTC (permalink / raw)
  To: Felix Fietkau, Lorenzo Bianconi, Ryder Lee, Shayne Chen,
	Sean Wang, Matthias Brugger, AngeloGioacchino Del Regno,
	Benjamin Koerner
  Cc: linux-wireless, linux-kernel, linux-arm-kernel, linux-mediatek,
	sw, Harshal Gohel, Benjamin Koerner

[-- Attachment #1: Type: text/plain, Size: 1219 bytes --]

On Friday, 4 December 2026 11:55:27 CET Benjamin Koerner wrote:
> +static u16 mt7996_spectrum_gain_db(u8 gain)
> +{
> +       const u16 gain_db[] = { 1, 55, 47, 39 };
> +
> +       if (gain < ARRAY_SIZE(gain_db))
> +               return gain_db[gain];
> +
> +       return 1;
> +}

This hardware supports more gains. We already discussed that we might to add 
them too. But here a specific idea from Simon:

The gains are most likely hardware specific. And it is a little bit awkward to 
remember which name belongs to which gain. It might therefore be a good idea 
to have a read-only file "gains_available" (like the scaling governor) which 
shows which gains can be selected. And then the user can then write these 
values to gain file. Either the actual dB value from this list or the alias. 
But the aliases high/medium/low can of course also be dropped when this would 
not be appropriate. If it is not one from the list then the write will be 
rejected with an -EINVAL.

And maybe we disallow "auto" because then the samples are returned with a 
random gain (which we don't know and don't get reported).

The list could then for example look like this:

55 (high)
47 (medium)
39 (low)
31
23
15
5

Regards,
	Sven

[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 228 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-04 12:35 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 10:55 [PATCH RFC mt76-next] wifi: mt76: mt7996: support spectrum scan / IQ capture Benjamin Koerner
2026-12-04 12:35 ` Sven Eckelmann

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox