linux-iio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/1] iio:proximity:sx9360: Fix hardware gain read/write
@ 2022-06-08  1:08 Xiaohui Zhang
  2022-06-08 23:44 ` kernel test robot
  2022-06-09  0:14 ` kernel test robot
  0 siblings, 2 replies; 6+ messages in thread
From: Xiaohui Zhang @ 2022-06-08  1:08 UTC (permalink / raw)
  To: Xiaohui Zhang, Jonathan Cameron, Lars-Peter Clausen,
	Gwendal Grignou, Stephen Boyd, Jongpil Jung, linux-iio,
	linux-kernel

Similar to the handling of read/write in commit 108e4d4de2b5
("iio:proximity:sx9324: Fix hardware gain read/write"), we thought
a patch might be needed here as well.

There are four possible gain values according to 'sx9360_gain_vals[]':

	1, 2, 4, and 8

The values are off by one when writing and reading the register. The
bits should be set according to this equation:

	ilog2(<gain>) + 1

so that a gain of 8 is 0x4 in the register field and a gain of 4 is 0x3
in the register field, etc. Note that a gain of 0 is reserved per the
datasheet. The default gain (SX9360_REG_PROX_CTRL0_GAIN_1) is also
wrong. It should be 0x1 << 3, i.e. 0x8, not 0x80 which is setting the
reserved bit 7.

Fix this all up to properly handle the hardware gain and return errors
for invalid settings.

Signed-off-by: Xiaohui Zhang <xiaohuizhang@ruc.edu.cn>
---
 drivers/iio/proximity/sx9360.c | 26 +++++++++++++++++++++-----
 1 file changed, 21 insertions(+), 5 deletions(-)

diff --git a/drivers/iio/proximity/sx9360.c b/drivers/iio/proximity/sx9360.c
index 3ebb30c8a4f6..3dbcc9f584cf 100644
--- a/drivers/iio/proximity/sx9360.c
+++ b/drivers/iio/proximity/sx9360.c
@@ -64,7 +64,10 @@
 #define SX9360_REG_PROX_CTRL0_PHR	0x40
 #define SX9360_REG_PROX_CTRL0_PHM	0x41
 #define SX9360_REG_PROX_CTRL0_GAIN_MASK	GENMASK(5, 3)
-#define SX9360_REG_PROX_CTRL0_GAIN_1		0x80
+#define SX9324_REG_PROX_CTRL0_GAIN_SHIFT	3
+#define SX9324_REG_PROX_CTRL0_GAIN_RSVD	0x0
+#define SX9324_REG_PROX_CTRL0_GAIN_1	0x1
+#define SX9324_REG_PROX_CTRL0_GAIN_8	0x4
 #define SX9360_REG_PROX_CTRL0_RAWFILT_MASK	GENMASK(2, 0)
 #define SX9360_REG_PROX_CTRL0_RAWFILT_1P50	0x01
 #define SX9360_REG_PROX_CTRL1		0x42
@@ -288,7 +291,14 @@ static int sx9360_read_gain(struct sx_common_data *data,
 	if (ret)
 		return ret;
 
-	*val = 1 << FIELD_GET(SX9360_REG_PROX_CTRL0_GAIN_MASK, regval);
+	regval = FIELD_GET(SX9360_REG_PROX_CTRL0_GAIN_MASK, regval);
+	if (regval)
+		regval--;
+	else if (regval == SX9360_REG_PROX_CTRL0_GAIN_RSVD ||
+		 regval > SX9360_REG_PROX_CTRL0_GAIN_8)
+		return -EINVAL;
+
+	*val = 1 << regval;
 
 	return IIO_VAL_INT;
 }
@@ -630,8 +640,12 @@ static int sx9360_write_gain(struct sx_common_data *data,
 	unsigned int gain, reg;
 	int ret;
 
-	gain = ilog2(val);
 	reg = SX9360_REG_PROX_CTRL0_PHR + chan->channel;
+
+	gain = ilog2(val) + 1;
+	if (val <= 0 || gain > SX9360_REG_PROX_CTRL0_GAIN_8)
+		return -EINVAL;
+
 	gain = FIELD_PREP(SX9360_REG_PROX_CTRL0_GAIN_MASK, gain);
 
 	mutex_lock(&data->mutex);
@@ -681,9 +695,11 @@ static const struct sx_common_reg_default sx9360_default_regs[] = {
 	{ SX9360_REG_AFE_PARAM1_PHM, SX9360_REG_AFE_PARAM1_AGAIN_PHM_6PF |
 		SX9360_REG_AFE_PARAM1_FREQ_83_33HZ },
 
-	{ SX9360_REG_PROX_CTRL0_PHR, SX9360_REG_PROX_CTRL0_GAIN_1 |
+	{ SX9360_REG_PROX_CTRL0_PHR,
+		SX9360_REG_PROX_CTRL0_GAIN_1 << SX9360_REG_PROX_CTRL0_GAIN_SHIFT |
 		SX9360_REG_PROX_CTRL0_RAWFILT_1P50 },
-	{ SX9360_REG_PROX_CTRL0_PHM, SX9360_REG_PROX_CTRL0_GAIN_1 |
+	{ SX9360_REG_PROX_CTRL0_PHM,
+		SX9360_REG_PROX_CTRL0_GAIN_1 << SX9360_REG_PROX_CTRL0_GAIN_SHIFT |
 		SX9360_REG_PROX_CTRL0_RAWFILT_1P50 },
 	{ SX9360_REG_PROX_CTRL1, SX9360_REG_PROX_CTRL1_AVGNEG_THRESH_16K },
 	{ SX9360_REG_PROX_CTRL2, SX9360_REG_PROX_CTRL2_AVGDEB_2SAMPLES |
-- 
2.17.1


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

* Re: [PATCH 1/1] iio:proximity:sx9360: Fix hardware gain read/write
  2022-06-08  1:08 [PATCH 1/1] iio:proximity:sx9360: Fix hardware gain read/write Xiaohui Zhang
@ 2022-06-08 23:44 ` kernel test robot
  2022-06-09  0:14 ` kernel test robot
  1 sibling, 0 replies; 6+ messages in thread
From: kernel test robot @ 2022-06-08 23:44 UTC (permalink / raw)
  To: Xiaohui Zhang, Jonathan Cameron, Lars-Peter Clausen,
	Gwendal Grignou, Stephen Boyd, Jongpil Jung, linux-iio,
	linux-kernel
  Cc: llvm, kbuild-all

Hi Xiaohui,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on jic23-iio/togreg]
[also build test ERROR on v5.19-rc1 next-20220608]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/intel-lab-lkp/linux/commits/Xiaohui-Zhang/iio-proximity-sx9360-Fix-hardware-gain-read-write/20220608-125912
base:   https://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio.git togreg
config: hexagon-randconfig-r041-20220608 (https://download.01.org/0day-ci/archive/20220609/202206090719.8FcfVgxx-lkp@intel.com/config)
compiler: clang version 15.0.0 (https://github.com/llvm/llvm-project b92436efcb7813fc481b30f2593a4907568d917a)
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # https://github.com/intel-lab-lkp/linux/commit/bdc0a6947530604152de0d152635e144a486ee32
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Xiaohui-Zhang/iio-proximity-sx9360-Fix-hardware-gain-read-write/20220608-125912
        git checkout bdc0a6947530604152de0d152635e144a486ee32
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=hexagon SHELL=/bin/bash drivers/iio/proximity/

If you fix the issue, kindly add following tag where applicable
Reported-by: kernel test robot <lkp@intel.com>

All errors (new ones prefixed by >>):

>> drivers/iio/proximity/sx9360.c:297:21: error: use of undeclared identifier 'SX9360_REG_PROX_CTRL0_GAIN_RSVD'
           else if (regval == SX9360_REG_PROX_CTRL0_GAIN_RSVD ||
                              ^
>> drivers/iio/proximity/sx9360.c:298:13: error: use of undeclared identifier 'SX9360_REG_PROX_CTRL0_GAIN_8'
                    regval > SX9360_REG_PROX_CTRL0_GAIN_8)
                             ^
   drivers/iio/proximity/sx9360.c:646:25: error: use of undeclared identifier 'SX9360_REG_PROX_CTRL0_GAIN_8'
           if (val <= 0 || gain > SX9360_REG_PROX_CTRL0_GAIN_8)
                                  ^
>> drivers/iio/proximity/sx9360.c:699:3: error: use of undeclared identifier 'SX9360_REG_PROX_CTRL0_GAIN_1'
                   SX9360_REG_PROX_CTRL0_GAIN_1 << SX9360_REG_PROX_CTRL0_GAIN_SHIFT |
                   ^
>> drivers/iio/proximity/sx9360.c:699:35: error: use of undeclared identifier 'SX9360_REG_PROX_CTRL0_GAIN_SHIFT'
                   SX9360_REG_PROX_CTRL0_GAIN_1 << SX9360_REG_PROX_CTRL0_GAIN_SHIFT |
                                                   ^
   drivers/iio/proximity/sx9360.c:702:3: error: use of undeclared identifier 'SX9360_REG_PROX_CTRL0_GAIN_1'
                   SX9360_REG_PROX_CTRL0_GAIN_1 << SX9360_REG_PROX_CTRL0_GAIN_SHIFT |
                   ^
   drivers/iio/proximity/sx9360.c:702:35: error: use of undeclared identifier 'SX9360_REG_PROX_CTRL0_GAIN_SHIFT'
                   SX9360_REG_PROX_CTRL0_GAIN_1 << SX9360_REG_PROX_CTRL0_GAIN_SHIFT |
                                                   ^
>> drivers/iio/proximity/sx9360.c:796:22: error: invalid application of 'sizeof' to an incomplete type 'const struct sx_common_reg_default[]'
           .num_default_regs = ARRAY_SIZE(sx9360_default_regs),
                               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/kernel.h:55:32: note: expanded from macro 'ARRAY_SIZE'
   #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
                                  ^~~~~
   8 errors generated.


vim +/SX9360_REG_PROX_CTRL0_GAIN_RSVD +297 drivers/iio/proximity/sx9360.c

   282	
   283	static int sx9360_read_gain(struct sx_common_data *data,
   284				    const struct iio_chan_spec *chan, int *val)
   285	{
   286		unsigned int reg, regval;
   287		int ret;
   288	
   289		reg = SX9360_REG_PROX_CTRL0_PHR + chan->channel;
   290		ret = regmap_read(data->regmap, reg, &regval);
   291		if (ret)
   292			return ret;
   293	
   294		regval = FIELD_GET(SX9360_REG_PROX_CTRL0_GAIN_MASK, regval);
   295		if (regval)
   296			regval--;
 > 297		else if (regval == SX9360_REG_PROX_CTRL0_GAIN_RSVD ||
 > 298			 regval > SX9360_REG_PROX_CTRL0_GAIN_8)
   299			return -EINVAL;
   300	
   301		*val = 1 << regval;
   302	
   303		return IIO_VAL_INT;
   304	}
   305	

-- 
0-DAY CI Kernel Test Service
https://01.org/lkp

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

* Re: [PATCH 1/1] iio:proximity:sx9360: Fix hardware gain read/write
  2022-06-08  1:08 [PATCH 1/1] iio:proximity:sx9360: Fix hardware gain read/write Xiaohui Zhang
  2022-06-08 23:44 ` kernel test robot
@ 2022-06-09  0:14 ` kernel test robot
  1 sibling, 0 replies; 6+ messages in thread
From: kernel test robot @ 2022-06-09  0:14 UTC (permalink / raw)
  To: Xiaohui Zhang, Jonathan Cameron, Lars-Peter Clausen,
	Gwendal Grignou, Stephen Boyd, Jongpil Jung, linux-iio,
	linux-kernel
  Cc: kbuild-all

Hi Xiaohui,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on jic23-iio/togreg]
[also build test ERROR on v5.19-rc1 next-20220608]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/intel-lab-lkp/linux/commits/Xiaohui-Zhang/iio-proximity-sx9360-Fix-hardware-gain-read-write/20220608-125912
base:   https://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio.git togreg
config: openrisc-randconfig-r033-20220608 (https://download.01.org/0day-ci/archive/20220609/202206090828.1Q7GljFY-lkp@intel.com/config)
compiler: or1k-linux-gcc (GCC) 11.3.0
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # https://github.com/intel-lab-lkp/linux/commit/bdc0a6947530604152de0d152635e144a486ee32
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Xiaohui-Zhang/iio-proximity-sx9360-Fix-hardware-gain-read-write/20220608-125912
        git checkout bdc0a6947530604152de0d152635e144a486ee32
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.3.0 make.cross W=1 O=build_dir ARCH=openrisc SHELL=/bin/bash drivers/iio/proximity/

If you fix the issue, kindly add following tag where applicable
Reported-by: kernel test robot <lkp@intel.com>

All errors (new ones prefixed by >>):

   drivers/iio/proximity/sx9360.c: In function 'sx9360_read_gain':
>> drivers/iio/proximity/sx9360.c:297:28: error: 'SX9360_REG_PROX_CTRL0_GAIN_RSVD' undeclared (first use in this function); did you mean 'SX9324_REG_PROX_CTRL0_GAIN_RSVD'?
     297 |         else if (regval == SX9360_REG_PROX_CTRL0_GAIN_RSVD ||
         |                            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
         |                            SX9324_REG_PROX_CTRL0_GAIN_RSVD
   drivers/iio/proximity/sx9360.c:297:28: note: each undeclared identifier is reported only once for each function it appears in
>> drivers/iio/proximity/sx9360.c:298:27: error: 'SX9360_REG_PROX_CTRL0_GAIN_8' undeclared (first use in this function); did you mean 'SX9324_REG_PROX_CTRL0_GAIN_8'?
     298 |                  regval > SX9360_REG_PROX_CTRL0_GAIN_8)
         |                           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
         |                           SX9324_REG_PROX_CTRL0_GAIN_8
   drivers/iio/proximity/sx9360.c: In function 'sx9360_write_gain':
   drivers/iio/proximity/sx9360.c:646:32: error: 'SX9360_REG_PROX_CTRL0_GAIN_8' undeclared (first use in this function); did you mean 'SX9324_REG_PROX_CTRL0_GAIN_8'?
     646 |         if (val <= 0 || gain > SX9360_REG_PROX_CTRL0_GAIN_8)
         |                                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
         |                                SX9324_REG_PROX_CTRL0_GAIN_8
   drivers/iio/proximity/sx9360.c: At top level:
>> drivers/iio/proximity/sx9360.c:699:17: error: 'SX9360_REG_PROX_CTRL0_GAIN_1' undeclared here (not in a function); did you mean 'SX9324_REG_PROX_CTRL0_GAIN_1'?
     699 |                 SX9360_REG_PROX_CTRL0_GAIN_1 << SX9360_REG_PROX_CTRL0_GAIN_SHIFT |
         |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
         |                 SX9324_REG_PROX_CTRL0_GAIN_1
>> drivers/iio/proximity/sx9360.c:699:49: error: 'SX9360_REG_PROX_CTRL0_GAIN_SHIFT' undeclared here (not in a function); did you mean 'SX9324_REG_PROX_CTRL0_GAIN_SHIFT'?
     699 |                 SX9360_REG_PROX_CTRL0_GAIN_1 << SX9360_REG_PROX_CTRL0_GAIN_SHIFT |
         |                                                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
         |                                                 SX9324_REG_PROX_CTRL0_GAIN_SHIFT


vim +297 drivers/iio/proximity/sx9360.c

   282	
   283	static int sx9360_read_gain(struct sx_common_data *data,
   284				    const struct iio_chan_spec *chan, int *val)
   285	{
   286		unsigned int reg, regval;
   287		int ret;
   288	
   289		reg = SX9360_REG_PROX_CTRL0_PHR + chan->channel;
   290		ret = regmap_read(data->regmap, reg, &regval);
   291		if (ret)
   292			return ret;
   293	
   294		regval = FIELD_GET(SX9360_REG_PROX_CTRL0_GAIN_MASK, regval);
   295		if (regval)
   296			regval--;
 > 297		else if (regval == SX9360_REG_PROX_CTRL0_GAIN_RSVD ||
 > 298			 regval > SX9360_REG_PROX_CTRL0_GAIN_8)
   299			return -EINVAL;
   300	
   301		*val = 1 << regval;
   302	
   303		return IIO_VAL_INT;
   304	}
   305	

-- 
0-DAY CI Kernel Test Service
https://01.org/lkp

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

* [PATCH 1/1] iio:proximity:sx9360: Fix hardware gain read/write
@ 2022-06-10  5:30 Xiaohui Zhang
  2022-06-10 14:37 ` Andy Shevchenko
  0 siblings, 1 reply; 6+ messages in thread
From: Xiaohui Zhang @ 2022-06-10  5:30 UTC (permalink / raw)
  To: Xiaohui Zhang, Jonathan Cameron, Lars-Peter Clausen,
	Gwendal Grignou, Stephen Boyd, Jongpil Jung, linux-iio,
	linux-kernel

Similar to the handling of read/write in commit 108e4d4de2b5
("iio:proximity:sx9324: Fix hardware gain read/write"), we thought
a patch might be needed here as well.

There are four possible gain values according to 'sx9360_gain_vals[]':

	1, 2, 4, and 8

The values are off by one when writing and reading the register. The
bits should be set according to this equation:

	ilog2(<gain>) + 1

so that a gain of 8 is 0x4 in the register field and a gain of 4 is 0x3
in the register field, etc. Note that a gain of 0 is reserved per the
datasheet. The default gain (SX9360_REG_PROX_CTRL0_GAIN_1) is also
wrong. It should be 0x1 << 3, i.e. 0x8, not 0x80 which is setting the
reserved bit 7.

Fix this all up to properly handle the hardware gain and return errors
for invalid settings.

Signed-off-by: Xiaohui Zhang <xiaohuizhang@ruc.edu.cn>
---
 drivers/iio/proximity/sx9360.c | 26 +++++++++++++++++++++-----
 1 file changed, 21 insertions(+), 5 deletions(-)

diff --git a/drivers/iio/proximity/sx9360.c b/drivers/iio/proximity/sx9360.c
index 3ebb30c8a4f6..f231929debb7 100644
--- a/drivers/iio/proximity/sx9360.c
+++ b/drivers/iio/proximity/sx9360.c
@@ -64,7 +64,10 @@
 #define SX9360_REG_PROX_CTRL0_PHR	0x40
 #define SX9360_REG_PROX_CTRL0_PHM	0x41
 #define SX9360_REG_PROX_CTRL0_GAIN_MASK	GENMASK(5, 3)
-#define SX9360_REG_PROX_CTRL0_GAIN_1		0x80
+#define SX9360_REG_PROX_CTRL0_GAIN_SHIFT	3
+#define SX9360_REG_PROX_CTRL0_GAIN_RSVD	0x0
+#define SX9360_REG_PROX_CTRL0_GAIN_1	0x1
+#define SX9360_REG_PROX_CTRL0_GAIN_8	0x4
 #define SX9360_REG_PROX_CTRL0_RAWFILT_MASK	GENMASK(2, 0)
 #define SX9360_REG_PROX_CTRL0_RAWFILT_1P50	0x01
 #define SX9360_REG_PROX_CTRL1		0x42
@@ -288,7 +291,14 @@ static int sx9360_read_gain(struct sx_common_data *data,
 	if (ret)
 		return ret;
 
-	*val = 1 << FIELD_GET(SX9360_REG_PROX_CTRL0_GAIN_MASK, regval);
+	regval = FIELD_GET(SX9360_REG_PROX_CTRL0_GAIN_MASK, regval);
+	if (regval)
+		regval--;
+	else if (regval == SX9360_REG_PROX_CTRL0_GAIN_RSVD ||
+		 regval > SX9360_REG_PROX_CTRL0_GAIN_8)
+		return -EINVAL;
+
+	*val = 1 << regval;
 
 	return IIO_VAL_INT;
 }
@@ -630,8 +640,12 @@ static int sx9360_write_gain(struct sx_common_data *data,
 	unsigned int gain, reg;
 	int ret;
 
-	gain = ilog2(val);
 	reg = SX9360_REG_PROX_CTRL0_PHR + chan->channel;
+
+	gain = ilog2(val) + 1;
+	if (val <= 0 || gain > SX9360_REG_PROX_CTRL0_GAIN_8)
+		return -EINVAL;
+
 	gain = FIELD_PREP(SX9360_REG_PROX_CTRL0_GAIN_MASK, gain);
 
 	mutex_lock(&data->mutex);
@@ -681,9 +695,11 @@ static const struct sx_common_reg_default sx9360_default_regs[] = {
 	{ SX9360_REG_AFE_PARAM1_PHM, SX9360_REG_AFE_PARAM1_AGAIN_PHM_6PF |
 		SX9360_REG_AFE_PARAM1_FREQ_83_33HZ },
 
-	{ SX9360_REG_PROX_CTRL0_PHR, SX9360_REG_PROX_CTRL0_GAIN_1 |
+	{ SX9360_REG_PROX_CTRL0_PHR,
+		SX9360_REG_PROX_CTRL0_GAIN_1 << SX9360_REG_PROX_CTRL0_GAIN_SHIFT |
 		SX9360_REG_PROX_CTRL0_RAWFILT_1P50 },
-	{ SX9360_REG_PROX_CTRL0_PHM, SX9360_REG_PROX_CTRL0_GAIN_1 |
+	{ SX9360_REG_PROX_CTRL0_PHM,
+		SX9360_REG_PROX_CTRL0_GAIN_1 << SX9360_REG_PROX_CTRL0_GAIN_SHIFT |
 		SX9360_REG_PROX_CTRL0_RAWFILT_1P50 },
 	{ SX9360_REG_PROX_CTRL1, SX9360_REG_PROX_CTRL1_AVGNEG_THRESH_16K },
 	{ SX9360_REG_PROX_CTRL2, SX9360_REG_PROX_CTRL2_AVGDEB_2SAMPLES |
-- 
2.17.1


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

* Re: [PATCH 1/1] iio:proximity:sx9360: Fix hardware gain read/write
  2022-06-10  5:30 Xiaohui Zhang
@ 2022-06-10 14:37 ` Andy Shevchenko
  2022-06-11 16:03   ` Jonathan Cameron
  0 siblings, 1 reply; 6+ messages in thread
From: Andy Shevchenko @ 2022-06-10 14:37 UTC (permalink / raw)
  To: Xiaohui Zhang
  Cc: Jonathan Cameron, Lars-Peter Clausen, Gwendal Grignou,
	Stephen Boyd, Jongpil Jung, linux-iio, Linux Kernel Mailing List

On Fri, Jun 10, 2022 at 7:53 AM Xiaohui Zhang <xiaohuizhang@ruc.edu.cn> wrote:
>
> Similar to the handling of read/write in commit 108e4d4de2b5
> ("iio:proximity:sx9324: Fix hardware gain read/write"), we thought
> a patch might be needed here as well.
>
> There are four possible gain values according to 'sx9360_gain_vals[]':
>
>         1, 2, 4, and 8
>
> The values are off by one when writing and reading the register. The
> bits should be set according to this equation:
>
>         ilog2(<gain>) + 1
>
> so that a gain of 8 is 0x4 in the register field and a gain of 4 is 0x3
> in the register field, etc. Note that a gain of 0 is reserved per the
> datasheet. The default gain (SX9360_REG_PROX_CTRL0_GAIN_1) is also
> wrong. It should be 0x1 << 3, i.e. 0x8, not 0x80 which is setting the
> reserved bit 7.
>
> Fix this all up to properly handle the hardware gain and return errors
> for invalid settings.

...

> +       regval = FIELD_GET(SX9360_REG_PROX_CTRL0_GAIN_MASK, regval);
> +       if (regval)
> +               regval--;
> +       else if (regval == SX9360_REG_PROX_CTRL0_GAIN_RSVD ||
> +                regval > SX9360_REG_PROX_CTRL0_GAIN_8)

else?! Isn't it a dead code? How has it been tested?

> +               return -EINVAL;

> +       *val = 1 << regval;

Even in the original code this is wrong in accordance with C standard.
It might have potentially UB. BIT(), for example, solves this issue.
You may do what it does under the hood.

-- 
With Best Regards,
Andy Shevchenko

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

* Re: [PATCH 1/1] iio:proximity:sx9360: Fix hardware gain read/write
  2022-06-10 14:37 ` Andy Shevchenko
@ 2022-06-11 16:03   ` Jonathan Cameron
  0 siblings, 0 replies; 6+ messages in thread
From: Jonathan Cameron @ 2022-06-11 16:03 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: Xiaohui Zhang, Lars-Peter Clausen, Gwendal Grignou, Stephen Boyd,
	Jongpil Jung, linux-iio, Linux Kernel Mailing List

On Fri, 10 Jun 2022 16:37:05 +0200
Andy Shevchenko <andy.shevchenko@gmail.com> wrote:

> On Fri, Jun 10, 2022 at 7:53 AM Xiaohui Zhang <xiaohuizhang@ruc.edu.cn> wrote:
> >
> > Similar to the handling of read/write in commit 108e4d4de2b5
> > ("iio:proximity:sx9324: Fix hardware gain read/write"), we thought
> > a patch might be needed here as well.
> >
> > There are four possible gain values according to 'sx9360_gain_vals[]':
> >
> >         1, 2, 4, and 8
> >
> > The values are off by one when writing and reading the register. The
> > bits should be set according to this equation:
> >
> >         ilog2(<gain>) + 1
> >
> > so that a gain of 8 is 0x4 in the register field and a gain of 4 is 0x3
> > in the register field, etc. Note that a gain of 0 is reserved per the
> > datasheet. The default gain (SX9360_REG_PROX_CTRL0_GAIN_1) is also
> > wrong. It should be 0x1 << 3, i.e. 0x8, not 0x80 which is setting the
> > reserved bit 7.
> >
> > Fix this all up to properly handle the hardware gain and return errors
> > for invalid settings.  
> 
> ...
> 
> > +       regval = FIELD_GET(SX9360_REG_PROX_CTRL0_GAIN_MASK, regval);
> > +       if (regval)
> > +               regval--;
> > +       else if (regval == SX9360_REG_PROX_CTRL0_GAIN_RSVD ||
> > +                regval > SX9360_REG_PROX_CTRL0_GAIN_8)  
> 
> else?! Isn't it a dead code? How has it been tested?

Gah. Missed this in review of sx9324 change.  First check is
fine because GAIN_RSVD is 0 though not a lot of point in the if.

Second one is intended as hardening against malicious / broken
hardware only so you would never see that value except via emulation
or a unit test.  So test wouldn't have spotted this as far as I
can see.
Needs good old eyeballs. :)


> 
> > +               return -EINVAL;  
> 
> > +       *val = 1 << regval;  
> 
> Even in the original code this is wrong in accordance with C standard.
> It might have potentially UB. BIT(), for example, solves this issue.
> You may do what it does under the hood.
> 

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

end of thread, other threads:[~2022-06-11 15:54 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-06-08  1:08 [PATCH 1/1] iio:proximity:sx9360: Fix hardware gain read/write Xiaohui Zhang
2022-06-08 23:44 ` kernel test robot
2022-06-09  0:14 ` kernel test robot
  -- strict thread matches above, loose matches on Subject: below --
2022-06-10  5:30 Xiaohui Zhang
2022-06-10 14:37 ` Andy Shevchenko
2022-06-11 16:03   ` Jonathan Cameron

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).