Skip to content

Commit e4c3e13

Browse files
Fabrice Gasnierjic23
authored andcommitted
counter: stm32-timer-cnt: fix ceiling write max value
The ceiling value isn't checked before writing it into registers. The user could write a value higher than the counter resolution (e.g. 16 or 32 bits indicated by max_arr). This makes most significant bits to be truncated. Fix it by checking the max_arr to report a range error [1] to the user. [1] https://lkml.org/lkml/2021/2/12/358 Fixes: ad29937 ("counter: Add STM32 Timer quadrature encoder") Signed-off-by: Fabrice Gasnier <[email protected]> Acked-by: William Breathitt Gray <[email protected]> Cc: <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jonathan Cameron <[email protected]>
1 parent fae6f62 commit e4c3e13

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

drivers/counter/stm32-timer-cnt.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ struct stm32_timer_cnt {
3232
struct regmap *regmap;
3333
struct clk *clk;
3434
u32 ceiling;
35+
u32 max_arr;
3536
bool enabled;
3637
struct stm32_timer_regs bak;
3738
};
@@ -191,6 +192,9 @@ static ssize_t stm32_count_ceiling_write(struct counter_device *counter,
191192
if (ret)
192193
return ret;
193194

195+
if (ceiling > priv->max_arr)
196+
return -ERANGE;
197+
194198
/* TIMx_ARR register shouldn't be buffered (ARPE=0) */
195199
regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_ARPE, 0);
196200
regmap_write(priv->regmap, TIM_ARR, ceiling);
@@ -371,6 +375,7 @@ static int stm32_timer_cnt_probe(struct platform_device *pdev)
371375
priv->regmap = ddata->regmap;
372376
priv->clk = ddata->clk;
373377
priv->ceiling = ddata->max_arr;
378+
priv->max_arr = ddata->max_arr;
374379

375380
priv->counter.name = dev_name(dev);
376381
priv->counter.parent = dev;

0 commit comments

Comments
 (0)