[PATCH] media: cxd2880: fix carrier offset for negative raw offsets

From: Enrico Lorenzoni

Date: Wed Nov 18 2026 - 14:51:59 EST


cxd2880_tnrdmd_dvbt_mon_carrier_offset() and
cxd2880_tnrdmd_dvbt2_mon_carrier_offset() scale the sign-extended register
value by the channel bandwidth:

*offset = cxd2880_convert2s_complement(ctl_val, 29);
*offset = -1 * ((*offset) * tnr_dmd->bandwidth / 235);

tnr_dmd->bandwidth is an enum cxd2880_dtv_bandwidth. It has no negative
enumerators, so GCC gives it an unsigned underlying type and the
multiplication and the division are done in unsigned arithmetic. A
negative raw offset wraps before the division, and the function returns a
value near -(2^32 / 235) Hz instead of the real offset. The DVB-T2 variant
has the same expression with a divisor of 940.

Positive raw offsets are not affected: whether a receiver hits the bug
depends on the sign of its crystal error relative to the transmitter.

No in-tree code calls these functions today, so mainline has no
user-visible effect. The bug shows as soon as the value is used: on a
Raspberry Pi TV HAT, with the functions wired to a debugfs status file in
an out-of-tree build of this driver, 8 MHz DVB-T muxes whose carriers are
about 2.4 to 3.2 kHz above the tuned frequency read around -18273794 Hz,
and a DVB-T2 mux at the same site read -4565734 Hz. A user space model of
the unsigned arithmetic reproduces these readings exactly from the raw
register values.

Cast the bandwidth to int so the expression is evaluated as signed.

With the fix, on the same out-of-tree build, the same muxes read +2.4 to
+3.2 kHz. Detuning one DVB-T mux by known amounts between -100 kHz and
+100 kHz gave a slope of -0.9998 Hz/Hz, and a locally generated DVB-T
signal offset by -30 kHz to +30 kHz was tracked with the correct sign
over the whole range.

The analysis, the fix and this changelog were prepared with the help of
an AI coding assistant (Anthropic Claude). I have reviewed them, and the
tests above were run on my own hardware.

Fixes: cc438de9df2c ("media: cxd2880: Add DVB-T monitor functions")
Fixes: 55e13167c938 ("media: cxd2880: Add DVB-T2 monitor functions")
Signed-off-by: Enrico Lorenzoni <enricolorenzoni59@xxxxxxxxx>
---
drivers/media/dvb-frontends/cxd2880/cxd2880_tnrdmd_dvbt2_mon.c | 2 +-
drivers/media/dvb-frontends/cxd2880/cxd2880_tnrdmd_dvbt_mon.c | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/media/dvb-frontends/cxd2880/cxd2880_tnrdmd_dvbt2_mon.c b/drivers/media/dvb-frontends/cxd2880/cxd2880_tnrdmd_dvbt2_mon.c
index 4e173dd87..e35efff01 100644
--- a/drivers/media/dvb-frontends/cxd2880/cxd2880_tnrdmd_dvbt2_mon.c
+++ b/drivers/media/dvb-frontends/cxd2880/cxd2880_tnrdmd_dvbt2_mon.c
@@ -146,7 +146,7 @@ int cxd2880_tnrdmd_dvbt2_mon_carrier_offset(struct cxd2880_tnrdmd
case CXD2880_DTV_BW_6_MHZ:
case CXD2880_DTV_BW_7_MHZ:
case CXD2880_DTV_BW_8_MHZ:
- *offset = -1 * ((*offset) * tnr_dmd->bandwidth / 940);
+ *offset = -1 * ((*offset) * (int)tnr_dmd->bandwidth / 940);
break;
default:
return -EINVAL;
diff --git a/drivers/media/dvb-frontends/cxd2880/cxd2880_tnrdmd_dvbt_mon.c b/drivers/media/dvb-frontends/cxd2880/cxd2880_tnrdmd_dvbt_mon.c
index 86d5a1e40..c07949b11 100644
--- a/drivers/media/dvb-frontends/cxd2880/cxd2880_tnrdmd_dvbt_mon.c
+++ b/drivers/media/dvb-frontends/cxd2880/cxd2880_tnrdmd_dvbt_mon.c
@@ -184,7 +184,7 @@ int cxd2880_tnrdmd_dvbt_mon_carrier_offset(struct cxd2880_tnrdmd
((rdata[0] & 0x1f) << 24) | (rdata[1] << 16) | (rdata[2] << 8) |
(rdata[3]);
*offset = cxd2880_convert2s_complement(ctl_val, 29);
- *offset = -1 * ((*offset) * tnr_dmd->bandwidth / 235);
+ *offset = -1 * ((*offset) * (int)tnr_dmd->bandwidth / 235);

return ret;
}

base-commit: aa0f4c41d4870a18e65772a1817af16427200371
--
2.47.3