From: Sean Young <sean@mess.org>
To: linux-media@vger.kernel.org, Sean Young <sean@mess.org>,
Mauro Carvalho Chehab <mchehab@kernel.org>,
Neil Armstrong <neil.armstrong@linaro.org>,
Kevin Hilman <khilman@baylibre.com>,
Jerome Brunet <jbrunet@baylibre.com>,
Martin Blumenstingl <martin.blumenstingl@googlemail.com>,
Viktor Prutyanov <viktor.prutyanov@phystech.edu>
Cc: Rik van Riel <riel@surriel.com>,
stable@vger.kernel.org,
Mauro Carvalho Chehab <mchehab+huawei@kernel.org>,
linux-arm-kernel@lists.infradead.org,
linux-amlogic@lists.infradead.org, linux-kernel@vger.kernel.org
Subject: [PATCH v4 19/19] media: meson-ir-tx: Validate carrier and duty_cycle
Date: Tue, 8 Sep 2026 16:52:07 +0100 [thread overview]
Message-ID: <528055c59611d6711e15b05c5ab150d4d3d338af.1788882189.git.sean@mess.org> (raw)
In-Reply-To: <cover.1788882189.git.sean@mess.org>
It is possible to set a combination of carrier and duty cycle that do
not produce a correct signal. In addition, setting a carrier larger
than USEC_PER_SEC will result in a divide by zero in
meson_irtx_prepare_pulse() during transmit.
Fixes: 49be1c78d575 ("media: rc: introduce Meson IR TX driver")
Signed-off-by: Sean Young <sean@mess.org>
Cc: stable@vger.kernel.org
---
drivers/media/rc/meson-ir-tx.c | 54 +++++++++++++++++++++++++++-------
1 file changed, 44 insertions(+), 10 deletions(-)
diff --git a/drivers/media/rc/meson-ir-tx.c b/drivers/media/rc/meson-ir-tx.c
index 1ab898069e67..a410464ede22 100644
--- a/drivers/media/rc/meson-ir-tx.c
+++ b/drivers/media/rc/meson-ir-tx.c
@@ -76,11 +76,24 @@ struct meson_irtx {
unsigned long clk_rate;
};
-static void meson_irtx_set_mod(struct meson_irtx *ir)
+static bool meson_irtx_calc_mod(unsigned long clk_rate, u32 carrier,
+ u32 duty_cycle, unsigned int *pulse_cnt,
+ unsigned int *space_cnt)
{
- unsigned int cnt = DIV_ROUND_CLOSEST(ir->clk_rate, ir->carrier);
- unsigned int pulse_cnt = DIV_ROUND_CLOSEST(cnt * ir->duty_cycle, 100);
- unsigned int space_cnt = cnt - pulse_cnt;
+ unsigned int cnt;
+
+ cnt = DIV_ROUND_CLOSEST(clk_rate, carrier);
+ *pulse_cnt = DIV_ROUND_CLOSEST(cnt * duty_cycle, 100);
+ *space_cnt = cnt - *pulse_cnt;
+
+ return *pulse_cnt >= 1 && *pulse_cnt <= 65536 &&
+ *space_cnt >= 1 && *space_cnt <= 65536;
+}
+
+static void meson_irtx_write_mod(struct meson_irtx *ir, unsigned int pulse_cnt,
+ unsigned int space_cnt)
+{
+ unsigned int cnt = pulse_cnt + space_cnt;
dev_dbg(ir->dev, "F_mod = %uHz, T_mod = %luns, duty_cycle = %u%%\n",
ir->carrier, NSEC_PER_SEC / ir->clk_rate * cnt,
@@ -90,8 +103,10 @@ static void meson_irtx_set_mod(struct meson_irtx *ir)
ir->reg_base + IRB_ADDR1);
}
-static void meson_irtx_setup(struct meson_irtx *ir, unsigned int clk_nr)
+static int meson_irtx_setup(struct meson_irtx *ir, unsigned int clk_nr)
{
+ unsigned int pulse_cnt, space_cnt;
+
/*
* Disable the TX, set modulator clock tick and set initialize
* output to be high. Set up carrier frequency and duty cycle. Then
@@ -100,13 +115,18 @@ static void meson_irtx_setup(struct meson_irtx *ir, unsigned int clk_nr)
*/
writel(~IRB_ENABLE & (IRB_MOD_CLK(clk_nr) | IRB_INIT_HIGH),
ir->reg_base + IRB_ADDR0);
- meson_irtx_set_mod(ir);
+ if (!meson_irtx_calc_mod(ir->clk_rate, ir->carrier, ir->duty_cycle,
+ &pulse_cnt, &space_cnt))
+ return -EINVAL;
+ meson_irtx_write_mod(ir, pulse_cnt, space_cnt);
writel(readl(ir->reg_base + IRB_ADDR0) & ~IRB_INIT_HIGH,
ir->reg_base + IRB_ADDR0);
writel(IRB_FIFO_IRQ_ENABLE | MIRTX_FIFO_THD,
ir->reg_base + IRB_ADDR3);
writel(readl(ir->reg_base + IRB_ADDR0) | IRB_ENABLE,
ir->reg_base + IRB_ADDR0);
+
+ return 0;
}
static u32 meson_irtx_prepare_pulse(struct meson_irtx *ir, unsigned int time)
@@ -215,12 +235,17 @@ static irqreturn_t meson_irtx_irqhandler(int irq, void *data)
static int meson_irtx_set_carrier(struct rc_dev *rc, u32 carrier)
{
struct meson_irtx *ir = rc->priv;
+ unsigned int pulse_cnt, space_cnt;
+
+ if (!carrier)
+ return -EINVAL;
- if (carrier == 0)
+ if (!meson_irtx_calc_mod(ir->clk_rate, carrier, ir->duty_cycle,
+ &pulse_cnt, &space_cnt))
return -EINVAL;
ir->carrier = carrier;
- meson_irtx_set_mod(ir);
+ meson_irtx_write_mod(ir, pulse_cnt, space_cnt);
return 0;
}
@@ -228,9 +253,14 @@ static int meson_irtx_set_carrier(struct rc_dev *rc, u32 carrier)
static int meson_irtx_set_duty_cycle(struct rc_dev *rc, u32 duty_cycle)
{
struct meson_irtx *ir = rc->priv;
+ unsigned int pulse_cnt, space_cnt;
+
+ if (!meson_irtx_calc_mod(ir->clk_rate, ir->carrier, duty_cycle,
+ &pulse_cnt, &space_cnt))
+ return -EINVAL;
ir->duty_cycle = duty_cycle;
- meson_irtx_set_mod(ir);
+ meson_irtx_write_mod(ir, pulse_cnt, space_cnt);
return 0;
}
@@ -337,7 +367,11 @@ static int meson_irtx_probe(struct platform_device *pdev)
if (ret)
return dev_err_probe(dev, ret, "modulator clock setup failed\n");
- meson_irtx_setup(ir, clk_nr);
+ ret = meson_irtx_setup(ir, clk_nr);
+ if (ret)
+ return dev_err_probe(dev, ret,
+ "carrier %u, duty_cycle %u%% not supported at clk_rate %luHz\n",
+ ir->carrier, ir->duty_cycle, ir->clk_rate);
ret = devm_request_irq(dev, irq,
meson_irtx_irqhandler,
--
2.55.0
parent reply other threads:[~2026-09-08 15:54 UTC|newest]
Thread overview: expand[flat|nested] mbox.gz Atom feed
[parent not found: <cover.1788882189.git.sean@mess.org>]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=528055c59611d6711e15b05c5ab150d4d3d338af.1788882189.git.sean@mess.org \
--to=sean@mess.org \
--cc=jbrunet@baylibre.com \
--cc=khilman@baylibre.com \
--cc=linux-amlogic@lists.infradead.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-media@vger.kernel.org \
--cc=martin.blumenstingl@googlemail.com \
--cc=mchehab+huawei@kernel.org \
--cc=mchehab@kernel.org \
--cc=neil.armstrong@linaro.org \
--cc=riel@surriel.com \
--cc=stable@vger.kernel.org \
--cc=viktor.prutyanov@phystech.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox