mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Andy Shevchenko <andy.shevchenko@gmail.com>
Cc: Xiaohui Zhang <xiaohuizhang@ruc.edu.cn>,
	Lars-Peter Clausen <lars@metafoo.de>,
	Gwendal Grignou <gwendal@chromium.org>,
	Stephen Boyd <swboyd@chromium.org>,
	Jongpil Jung <jongpil19.jung@samsung.com>,
	linux-iio <linux-iio@vger.kernel.org>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH 1/1] iio:proximity:sx9360: Fix hardware gain read/write
Date: Sat, 11 Jun 2022 17:03:40 +0100	[thread overview]
Message-ID: <20220611170340.1538ce98@jic23-huawei> (raw)
In-Reply-To: <CAHp75Vd6y7RJcwi_Egyb5guJ3i+FJHD-_pdeOmq4LC7xBmhnPg@mail.gmail.com>

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.
> 

  reply	other threads:[~2022-06-11 15:54 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-10  5:30 Xiaohui Zhang
2022-06-10 14:37 ` Andy Shevchenko
2022-06-11 16:03   ` Jonathan Cameron [this message]
  -- strict thread matches above, loose matches on Subject: below --
2022-06-08  1:08 Xiaohui Zhang
2022-06-08 23:44 ` kernel test robot
2022-06-09  0:14 ` kernel test robot

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=20220611170340.1538ce98@jic23-huawei \
    --to=jic23@kernel.org \
    --cc=andy.shevchenko@gmail.com \
    --cc=gwendal@chromium.org \
    --cc=jongpil19.jung@samsung.com \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=swboyd@chromium.org \
    --cc=xiaohuizhang@ruc.edu.cn \
    /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

all inboxes | Powered by JetHome®