UPSTREAM: pwm: Improve args checking in pwm_apply_state()
authorBrian Norris <briannorris@chromium.org>
Fri, 27 May 2016 16:45:49 +0000 (09:45 -0700)
committerHuang, Tao <huangtao@rock-chips.com>
Mon, 6 Mar 2017 10:28:40 +0000 (18:28 +0800)
commit5e88e30e7eaeda711f0cc819fbfdffbb130e42db
tree7300fe2acdeab66bcfb2b87922846fdfaa06d96b
parentd1b4c71db8e6247e654ab32383f7ff0dc431405f
UPSTREAM: pwm: Improve args checking in pwm_apply_state()

It seems like in the process of refactoring pwm_config() to utilize the
newly-introduced pwm_apply_state() API, some args/bounds checking was
dropped.

In particular, I noted that we are now allowing invalid period
selections, e.g.:

  # echo 1 > /sys/class/pwm/pwmchip0/export
  # cat /sys/class/pwm/pwmchip0/pwm1/period
  100
  # echo 101 > /sys/class/pwm/pwmchip0/pwm1/duty_cycle
  [... driver may or may not reject the value, or trigger some logic bug ...]

It's better to see:

  # echo 1 > /sys/class/pwm/pwmchip0/export
  # cat /sys/class/pwm/pwmchip0/pwm1/period
  100
  # echo 101 > /sys/class/pwm/pwmchip0/pwm1/duty_cycle
  -bash: echo: write error: Invalid argument

This patch reintroduces some bounds checks in both pwm_config() (for its
signed parameters; we don't want to convert negative values into large
unsigned values) and in pwm_apply_state() (which fix the above described
behavior, as well as other potential API misuses).

Fixes: 5ec803edcb70 ("pwm: Add core infrastructure to allow atomic updates")
Signed-off-by: Brian Norris <briannorris@chromium.org>
Acked-by: Boris Brezillon <boris.brezillon@free-electrons.com>
Signed-off-by: Thierry Reding <thierry.reding@gmail.com>
(cherry picked from commit ef2bf4997f7da6efa8540d9cf726c44bf2b863af)

Change-Id: I7d7515a51b5c3c2a77ae9743b7ed69eedc11d4b4
Signed-off-by: David Wu <david.wu@rock-chips.com>
drivers/pwm/core.c
include/linux/pwm.h