2020-04-13 15:22:41

by Oscar Carter

[permalink] [raw]
Subject: [RFC] staging: vt6656: Add formula to the vnt_rf_addpower function

Use a formula to calculate the return value of the vnt_rf_addpower
function instead of the "if" statement with literal values for every
case.

Signed-off-by: Oscar Carter <[email protected]>
---
What is the better approach for this function ? Leave it as is or use
a formula although it is less clear.

I prefer the formula as it is a more compact function.
What do you think ? Feedback wellcome.

Thanks,
Oscar Carter

drivers/staging/vt6656/rf.c | 20 +++-----------------
1 file changed, 3 insertions(+), 17 deletions(-)

diff --git a/drivers/staging/vt6656/rf.c b/drivers/staging/vt6656/rf.c
index 4f9aba0f21b0..3b200d7290a5 100644
--- a/drivers/staging/vt6656/rf.c
+++ b/drivers/staging/vt6656/rf.c
@@ -575,28 +575,14 @@ int vnt_rf_setpower(struct vnt_private *priv, u32 rate, u32 channel)

static u8 vnt_rf_addpower(struct vnt_private *priv)
{
+ s32 base;
s32 rssi = -priv->current_rssi;

if (!rssi)
return 7;

- if (priv->rf_type == RF_VT3226D0) {
- if (rssi < -70)
- return 9;
- else if (rssi < -65)
- return 7;
- else if (rssi < -60)
- return 5;
- } else {
- if (rssi < -80)
- return 9;
- else if (rssi < -75)
- return 7;
- else if (rssi < -70)
- return 5;
- }
-
- return 0;
+ base = (priv->rf_type == RF_VT3226D0) ? -60 : -70;
+ return (rssi < base--) ? ((rssi - base) / -5) * 2 + 5 : 0;
}

/* Set Tx power by power level and rate */
--
2.20.1


2020-04-14 15:41:14

by Dan Carpenter

[permalink] [raw]
Subject: Re: [RFC] staging: vt6656: Add formula to the vnt_rf_addpower function

On Mon, Apr 13, 2020 at 04:02:09PM +0200, Oscar Carter wrote:
> diff --git a/drivers/staging/vt6656/rf.c b/drivers/staging/vt6656/rf.c
> index 4f9aba0f21b0..3b200d7290a5 100644
> --- a/drivers/staging/vt6656/rf.c
> +++ b/drivers/staging/vt6656/rf.c
> @@ -575,28 +575,14 @@ int vnt_rf_setpower(struct vnt_private *priv, u32 rate, u32 channel)
>
> static u8 vnt_rf_addpower(struct vnt_private *priv)
> {
> + s32 base;

Just use "int". s32 is for when signed 32 bit is specified in the
hardware. I realize that it's done in this file, but if all your
friends jumped off a bridge doesn't mean you should drink their kool-aid.

> s32 rssi = -priv->current_rssi;
>
> if (!rssi)
> return 7;
>
> - if (priv->rf_type == RF_VT3226D0) {
> - if (rssi < -70)
> - return 9;
> - else if (rssi < -65)
> - return 7;
> - else if (rssi < -60)
> - return 5;
> - } else {
> - if (rssi < -80)
> - return 9;
> - else if (rssi < -75)
> - return 7;
> - else if (rssi < -70)
> - return 5;
> - }
> -
> - return 0;
> + base = (priv->rf_type == RF_VT3226D0) ? -60 : -70;
> + return (rssi < base--) ? ((rssi - base) / -5) * 2 + 5 : 0;
^^^^^^
I quite hate this postop. It would have been cleaner to write it like:

return (rssi < base) ? ((rssi - (base - 1)) / -5) * 2 + 5 : 0

I'm sorry, I'm not clever enough to figure out the potential values of
"rssi". How did you work out this formula? It feels like it came from
a standard or something? Do we not have a function already which
implements the standard?

regards,
dan carpenter

2020-04-16 00:21:02

by Oscar Carter

[permalink] [raw]
Subject: Re: [RFC] staging: vt6656: Add formula to the vnt_rf_addpower function

On Tue, Apr 14, 2020 at 04:12:14PM +0300, Dan Carpenter wrote:
> On Mon, Apr 13, 2020 at 04:02:09PM +0200, Oscar Carter wrote:
> > diff --git a/drivers/staging/vt6656/rf.c b/drivers/staging/vt6656/rf.c
> > index 4f9aba0f21b0..3b200d7290a5 100644
> > --- a/drivers/staging/vt6656/rf.c
> > +++ b/drivers/staging/vt6656/rf.c
> > @@ -575,28 +575,14 @@ int vnt_rf_setpower(struct vnt_private *priv, u32 rate, u32 channel)
> >
> > static u8 vnt_rf_addpower(struct vnt_private *priv)
> > {
> > + s32 base;
>
> Just use "int". s32 is for when signed 32 bit is specified in the
> hardware. I realize that it's done in this file, but if all your
> friends jumped off a bridge doesn't mean you should drink their kool-aid.

Ok, lesson learned and thanks for the aclaration about when use every type.

> > s32 rssi = -priv->current_rssi;
> >
> > if (!rssi)
> > return 7;
> >
> > - if (priv->rf_type == RF_VT3226D0) {
> > - if (rssi < -70)
> > - return 9;
> > - else if (rssi < -65)
> > - return 7;
> > - else if (rssi < -60)
> > - return 5;
> > - } else {
> > - if (rssi < -80)
> > - return 9;
> > - else if (rssi < -75)
> > - return 7;
> > - else if (rssi < -70)
> > - return 5;
> > - }
> > -
> > - return 0;
> > + base = (priv->rf_type == RF_VT3226D0) ? -60 : -70;
> > + return (rssi < base--) ? ((rssi - base) / -5) * 2 + 5 : 0;
> ^^^^^^
> I quite hate this postop. It would have been cleaner to write it like:
>
> return (rssi < base) ? ((rssi - (base - 1)) / -5) * 2 + 5 : 0
^ ^
Now, if we apply the minus operator one parentheses can be removed. The
same expression is now:

return (rssi < base) ? ((rssi - base + 1) / -5) * 2 + 5 : 0

I think it's clear enought.

> I'm sorry, I'm not clever enough to figure out the potential values of
> "rssi".

The IEEE 802.11 standard specifies that RSSI can be on a scale of 0 to
up to 255, and that each chipset manufacturer can define their own max
RSSI value. It's all up to the manufacturer.

> How did you work out this formula? It feels like it came from
> a standard or something?

I realized that the two branches of the if statement return the same
values (5, 7, 9) and that each value has a difference of 2 units from
the previous one. Also, every branch has 3 ranges, and every range has
an interval of 5. The only difference in this case is the "base" value
of each branch.

So, the solution was obtain the range index --> (rssi - base) / -5
Then, we need two units for every range index -> * 2
Now, the return value starts with five -------> + 5

The base-- was to obtain the range index the same that the orignal
function.

> Do we not have a function already which implements the standard?

I have been searching but I have not found anything that relates the
RSSI value with the amount of power to add. I have found

struct station_parameters -> member txpwr (struct sta_txpwr type)

but all the functions related to this doesn't set the tx power
depending on the RSSI value.

> regards,
> dan carpenter
>

thanks,
oscar carter

2020-04-20 16:22:22

by Oscar Carter

[permalink] [raw]
Subject: Re: [RFC] staging: vt6656: Add formula to the vnt_rf_addpower function

On Wed, Apr 15, 2020 at 06:25:41PM +0200, Oscar Carter wrote:
> On Tue, Apr 14, 2020 at 04:12:14PM +0300, Dan Carpenter wrote:
> > On Mon, Apr 13, 2020 at 04:02:09PM +0200, Oscar Carter wrote:
> > > diff --git a/drivers/staging/vt6656/rf.c b/drivers/staging/vt6656/rf.c
> > > index 4f9aba0f21b0..3b200d7290a5 100644
> > > --- a/drivers/staging/vt6656/rf.c
> > > +++ b/drivers/staging/vt6656/rf.c
> > > @@ -575,28 +575,14 @@ int vnt_rf_setpower(struct vnt_private *priv, u32 rate, u32 channel)
> > >
> > > static u8 vnt_rf_addpower(struct vnt_private *priv)
> > > {
> > > + s32 base;
> >
> > Just use "int". s32 is for when signed 32 bit is specified in the
> > hardware. I realize that it's done in this file, but if all your
> > friends jumped off a bridge doesn't mean you should drink their kool-aid.
>
> Ok, lesson learned and thanks for the aclaration about when use every type.
>
> > > s32 rssi = -priv->current_rssi;
> > >
> > > if (!rssi)
> > > return 7;
> > >
> > > - if (priv->rf_type == RF_VT3226D0) {
> > > - if (rssi < -70)
> > > - return 9;
> > > - else if (rssi < -65)
> > > - return 7;
> > > - else if (rssi < -60)
> > > - return 5;
> > > - } else {
> > > - if (rssi < -80)
> > > - return 9;
> > > - else if (rssi < -75)
> > > - return 7;
> > > - else if (rssi < -70)
> > > - return 5;
> > > - }
> > > -
> > > - return 0;
> > > + base = (priv->rf_type == RF_VT3226D0) ? -60 : -70;
> > > + return (rssi < base--) ? ((rssi - base) / -5) * 2 + 5 : 0;
> > ^^^^^^
> > I quite hate this postop. It would have been cleaner to write it like:
> >
> > return (rssi < base) ? ((rssi - (base - 1)) / -5) * 2 + 5 : 0
> ^ ^
> Now, if we apply the minus operator one parentheses can be removed. The
> same expression is now:
>
> return (rssi < base) ? ((rssi - base + 1) / -5) * 2 + 5 : 0
>
> I think it's clear enought.
>
> > I'm sorry, I'm not clever enough to figure out the potential values of
> > "rssi".
>
> The IEEE 802.11 standard specifies that RSSI can be on a scale of 0 to
> up to 255, and that each chipset manufacturer can define their own max
> RSSI value. It's all up to the manufacturer.
>
> > How did you work out this formula? It feels like it came from
> > a standard or something?
>
> I realized that the two branches of the if statement return the same
> values (5, 7, 9) and that each value has a difference of 2 units from
> the previous one. Also, every branch has 3 ranges, and every range has
> an interval of 5. The only difference in this case is the "base" value
> of each branch.
>
> So, the solution was obtain the range index --> (rssi - base) / -5
> Then, we need two units for every range index -> * 2
> Now, the return value starts with five -------> + 5
>
> The base-- was to obtain the range index the same that the orignal
> function.
>
> > Do we not have a function already which implements the standard?
>
> I have been searching but I have not found anything that relates the
> RSSI value with the amount of power to add. I have found
>
> struct station_parameters -> member txpwr (struct sta_txpwr type)
>
> but all the functions related to this doesn't set the tx power
> depending on the RSSI value.
>
I will create a new version with the previous comments (only change the
type of "base" variable to "int"), but what's the correct process for
an RFC patch. I need to send an email with the subject RFC v2 or now I
can send an email with the subject PATCH v2.

> > regards,
> > dan carpenter
> >
>
> thanks,
> oscar carter

thanks,
oscar carter