diff mbox series

[2/2] Input: wacom_i2c - Use macros for the bit masks

Message ID 20211009113707.17568-2-alistair@alistair23.me (mailing list archive)
State Superseded
Commit 91e2e76695fe52339be4bc4722475641ab0fc75c
Headers show
Series [1/2] Input: wacom_i2c - Clean up the query device fields | expand

Commit Message

Alistair Francis Oct. 9, 2021, 11:37 a.m. UTC
To make the code easier to read use macros for the bit masks.

Signed-off-by: Alistair Francis <alistair@alistair23.me>
---
 drivers/input/touchscreen/wacom_i2c.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

Comments

Dmitry Torokhov Oct. 13, 2021, 3:55 a.m. UTC | #1
Hi Alistair,

On Sat, Oct 09, 2021 at 09:37:07PM +1000, Alistair Francis wrote:
> To make the code easier to read use macros for the bit masks.
> 
> Signed-off-by: Alistair Francis <alistair@alistair23.me>
> ---
>  drivers/input/touchscreen/wacom_i2c.c | 14 ++++++++++----
>  1 file changed, 10 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/input/touchscreen/wacom_i2c.c b/drivers/input/touchscreen/wacom_i2c.c
> index 8d7267ccc661..6865342db659 100644
> --- a/drivers/input/touchscreen/wacom_i2c.c
> +++ b/drivers/input/touchscreen/wacom_i2c.c
> @@ -14,6 +14,12 @@
>  #include <linux/interrupt.h>
>  #include <asm/unaligned.h>
>  
> +// Bitmasks (for data[3])
> +#define WACOM_TIP_SWITCH_bm         (1 << 0)
> +#define WACOM_BARREL_SWITCH_bm      (1 << 1)
> +#define WACOM_ERASER_bm             (1 << 2)
> +#define WACOM_BARREL_SWITCH_2_bm    (1 << 4)

We have BIT() for that.

By the way, do you know what is the good name for bit 3? I see it is
being used in:

        if (!wac_i2c->prox)
                wac_i2c->tool = (data[3] & 0x0c) ?
                        BTN_TOOL_RUBBER : BTN_TOOL_PEN;

        wac_i2c->prox = data[3] & WACOM_IN_PROXIMITY;

0x0c is (WACOM_ERASER | <something else>).

Also, I am a bit confused by this code, now that I look at it closer.
Are we saying that the tool type (eraser or something else) is set only
in first packet for contact/touch?

> +
>  // Registers
>  #define WACOM_COMMAND_LSB   0x04
>  #define WACOM_COMMAND_MSB   0x00
> @@ -110,10 +116,10 @@ static irqreturn_t wacom_i2c_irq(int irq, void *dev_id)
>  	if (error < 0)
>  		goto out;
>  
> -	tsw = data[3] & 0x01;
> -	ers = data[3] & 0x04;
> -	f1 = data[3] & 0x02;
> -	f2 = data[3] & 0x10;
> +	tsw = data[3] & WACOM_TIP_SWITCH_bm;
> +	ers = data[3] & WACOM_ERASER_bm;
> +	f1 = data[3] & WACOM_BARREL_SWITCH_bm;
> +	f2 = data[3] & WACOM_BARREL_SWITCH_2_bm;
>  	x = le16_to_cpup((__le16 *)&data[4]);
>  	y = le16_to_cpup((__le16 *)&data[6]);
>  	pressure = le16_to_cpup((__le16 *)&data[8]);
> -- 
> 2.31.1
> 

How about the version of the patch below?
Alistair Francis Oct. 13, 2021, 1:04 p.m. UTC | #2
On Wed, Oct 13, 2021 at 1:56 PM Dmitry Torokhov
<dmitry.torokhov@gmail.com> wrote:
>
> Hi Alistair,
>
> On Sat, Oct 09, 2021 at 09:37:07PM +1000, Alistair Francis wrote:
> > To make the code easier to read use macros for the bit masks.
> >
> > Signed-off-by: Alistair Francis <alistair@alistair23.me>
> > ---
> >  drivers/input/touchscreen/wacom_i2c.c | 14 ++++++++++----
> >  1 file changed, 10 insertions(+), 4 deletions(-)
> >
> > diff --git a/drivers/input/touchscreen/wacom_i2c.c b/drivers/input/touchscreen/wacom_i2c.c
> > index 8d7267ccc661..6865342db659 100644
> > --- a/drivers/input/touchscreen/wacom_i2c.c
> > +++ b/drivers/input/touchscreen/wacom_i2c.c
> > @@ -14,6 +14,12 @@
> >  #include <linux/interrupt.h>
> >  #include <asm/unaligned.h>
> >
> > +// Bitmasks (for data[3])
> > +#define WACOM_TIP_SWITCH_bm         (1 << 0)
> > +#define WACOM_BARREL_SWITCH_bm      (1 << 1)
> > +#define WACOM_ERASER_bm             (1 << 2)
> > +#define WACOM_BARREL_SWITCH_2_bm    (1 << 4)
>
> We have BIT() for that.
>
> By the way, do you know what is the good name for bit 3? I see it is
> being used in:

I have this for bit 3:

#define WACOM_INVERT_bm             (1 << 3)


>
>         if (!wac_i2c->prox)
>                 wac_i2c->tool = (data[3] & 0x0c) ?
>                         BTN_TOOL_RUBBER : BTN_TOOL_PEN;
>
>         wac_i2c->prox = data[3] & WACOM_IN_PROXIMITY;
>
> 0x0c is (WACOM_ERASER | <something else>).
>
> Also, I am a bit confused by this code, now that I look at it closer.
> Are we saying that the tool type (eraser or something else) is set only
> in first packet for contact/touch?

I'm not sure, you would have to check with the wacom people.

>
> > +
> >  // Registers
> >  #define WACOM_COMMAND_LSB   0x04
> >  #define WACOM_COMMAND_MSB   0x00
> > @@ -110,10 +116,10 @@ static irqreturn_t wacom_i2c_irq(int irq, void *dev_id)
> >       if (error < 0)
> >               goto out;
> >
> > -     tsw = data[3] & 0x01;
> > -     ers = data[3] & 0x04;
> > -     f1 = data[3] & 0x02;
> > -     f2 = data[3] & 0x10;
> > +     tsw = data[3] & WACOM_TIP_SWITCH_bm;
> > +     ers = data[3] & WACOM_ERASER_bm;
> > +     f1 = data[3] & WACOM_BARREL_SWITCH_bm;
> > +     f2 = data[3] & WACOM_BARREL_SWITCH_2_bm;
> >       x = le16_to_cpup((__le16 *)&data[4]);
> >       y = le16_to_cpup((__le16 *)&data[6]);
> >       pressure = le16_to_cpup((__le16 *)&data[8]);
> > --
> > 2.31.1
> >
>
> How about the version of the patch below?

Looks good to me!

Alistair

>
> --
> Dmitry
>
> Input: wacom_i2c - use macros for the bit masks
>
> From: Alistair Francis <alistair@alistair23.me>
>
> To make the code easier to read use macros for the bit masks.
>
> Signed-off-by: Alistair Francis <alistair@alistair23.me>
> Link: https://lore.kernel.org/r/20211009113707.17568-2-alistair@alistair23.me
> Patchwork-Id: 12547519
> Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
> ---
>  drivers/input/touchscreen/wacom_i2c.c |   19 ++++++++++++++-----
>  1 file changed, 14 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/input/touchscreen/wacom_i2c.c b/drivers/input/touchscreen/wacom_i2c.c
> index 22826c387da5..d3ea9aa8a98c 100644
> --- a/drivers/input/touchscreen/wacom_i2c.c
> +++ b/drivers/input/touchscreen/wacom_i2c.c
> @@ -6,6 +6,7 @@
>   * <tobita.tatsunosuke@wacom.co.jp>
>   */
>
> +#include <linux/bits.h>
>  #include <linux/module.h>
>  #include <linux/input.h>
>  #include <linux/i2c.h>
> @@ -14,6 +15,14 @@
>  #include <linux/interrupt.h>
>  #include <asm/unaligned.h>
>
> +// Bitmasks (for data[3])
> +#define WACOM_TIP_SWITCH       BIT(0)
> +#define WACOM_BARREL_SWITCH    BIT(1)
> +#define WACOM_ERASER           BIT(2)
> +#define WACOM_BARREL_SWITCH_2  BIT(4)
> +#define WACOM_IN_PROXIMITY     BIT(5)
> +
> +// Registers
>  #define WACOM_CMD_QUERY0       0x04
>  #define WACOM_CMD_QUERY1       0x00
>  #define WACOM_CMD_QUERY2       0x33
> @@ -99,10 +108,10 @@ static irqreturn_t wacom_i2c_irq(int irq, void *dev_id)
>         if (error < 0)
>                 goto out;
>
> -       tsw = data[3] & 0x01;
> -       ers = data[3] & 0x04;
> -       f1 = data[3] & 0x02;
> -       f2 = data[3] & 0x10;
> +       tsw = data[3] & WACOM_TIP_SWITCH;
> +       ers = data[3] & WACOM_ERASER;
> +       f1 = data[3] & WACOM_BARREL_SWITCH;
> +       f2 = data[3] & WACOM_BARREL_SWITCH_2;
>         x = le16_to_cpup((__le16 *)&data[4]);
>         y = le16_to_cpup((__le16 *)&data[6]);
>         pressure = le16_to_cpup((__le16 *)&data[8]);
> @@ -111,7 +120,7 @@ static irqreturn_t wacom_i2c_irq(int irq, void *dev_id)
>                 wac_i2c->tool = (data[3] & 0x0c) ?
>                         BTN_TOOL_RUBBER : BTN_TOOL_PEN;
>
> -       wac_i2c->prox = data[3] & 0x20;
> +       wac_i2c->prox = data[3] & WACOM_IN_PROXIMITY;
>
>         input_report_key(input, BTN_TOUCH, tsw || ers);
>         input_report_key(input, wac_i2c->tool, wac_i2c->prox);
Alistair Francis Nov. 9, 2021, 9:58 a.m. UTC | #3
On Wed, Oct 13, 2021 at 11:04 PM Alistair Francis <alistair23@gmail.com> wrote:
>
> On Wed, Oct 13, 2021 at 1:56 PM Dmitry Torokhov
> <dmitry.torokhov@gmail.com> wrote:
> >
> > Hi Alistair,
> >
> > On Sat, Oct 09, 2021 at 09:37:07PM +1000, Alistair Francis wrote:
> > > To make the code easier to read use macros for the bit masks.
> > >
> > > Signed-off-by: Alistair Francis <alistair@alistair23.me>
> > > ---
> > >  drivers/input/touchscreen/wacom_i2c.c | 14 ++++++++++----
> > >  1 file changed, 10 insertions(+), 4 deletions(-)
> > >
> > > diff --git a/drivers/input/touchscreen/wacom_i2c.c b/drivers/input/touchscreen/wacom_i2c.c
> > > index 8d7267ccc661..6865342db659 100644
> > > --- a/drivers/input/touchscreen/wacom_i2c.c
> > > +++ b/drivers/input/touchscreen/wacom_i2c.c
> > > @@ -14,6 +14,12 @@
> > >  #include <linux/interrupt.h>
> > >  #include <asm/unaligned.h>
> > >
> > > +// Bitmasks (for data[3])
> > > +#define WACOM_TIP_SWITCH_bm         (1 << 0)
> > > +#define WACOM_BARREL_SWITCH_bm      (1 << 1)
> > > +#define WACOM_ERASER_bm             (1 << 2)
> > > +#define WACOM_BARREL_SWITCH_2_bm    (1 << 4)
> >
> > We have BIT() for that.
> >
> > By the way, do you know what is the good name for bit 3? I see it is
> > being used in:
>
> I have this for bit 3:
>
> #define WACOM_INVERT_bm             (1 << 3)
>
>
> >
> >         if (!wac_i2c->prox)
> >                 wac_i2c->tool = (data[3] & 0x0c) ?
> >                         BTN_TOOL_RUBBER : BTN_TOOL_PEN;
> >
> >         wac_i2c->prox = data[3] & WACOM_IN_PROXIMITY;
> >
> > 0x0c is (WACOM_ERASER | <something else>).
> >
> > Also, I am a bit confused by this code, now that I look at it closer.
> > Are we saying that the tool type (eraser or something else) is set only
> > in first packet for contact/touch?
>
> I'm not sure, you would have to check with the wacom people.
>
> >
> > > +
> > >  // Registers
> > >  #define WACOM_COMMAND_LSB   0x04
> > >  #define WACOM_COMMAND_MSB   0x00
> > > @@ -110,10 +116,10 @@ static irqreturn_t wacom_i2c_irq(int irq, void *dev_id)
> > >       if (error < 0)
> > >               goto out;
> > >
> > > -     tsw = data[3] & 0x01;
> > > -     ers = data[3] & 0x04;
> > > -     f1 = data[3] & 0x02;
> > > -     f2 = data[3] & 0x10;
> > > +     tsw = data[3] & WACOM_TIP_SWITCH_bm;
> > > +     ers = data[3] & WACOM_ERASER_bm;
> > > +     f1 = data[3] & WACOM_BARREL_SWITCH_bm;
> > > +     f2 = data[3] & WACOM_BARREL_SWITCH_2_bm;
> > >       x = le16_to_cpup((__le16 *)&data[4]);
> > >       y = le16_to_cpup((__le16 *)&data[6]);
> > >       pressure = le16_to_cpup((__le16 *)&data[8]);
> > > --
> > > 2.31.1
> > >
> >
> > How about the version of the patch below?
>
> Looks good to me!
>
> Alistair
>
> >
> > --
> > Dmitry
> >
> > Input: wacom_i2c - use macros for the bit masks
> >
> > From: Alistair Francis <alistair@alistair23.me>
> >
> > To make the code easier to read use macros for the bit masks.
> >
> > Signed-off-by: Alistair Francis <alistair@alistair23.me>
> > Link: https://lore.kernel.org/r/20211009113707.17568-2-alistair@alistair23.me
> > Patchwork-Id: 12547519
> > Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>

I'm assuming you don't need me to re-send this. Let me know if you
want me to though.

Alistair

> > ---
> >  drivers/input/touchscreen/wacom_i2c.c |   19 ++++++++++++++-----
> >  1 file changed, 14 insertions(+), 5 deletions(-)
> >
> > diff --git a/drivers/input/touchscreen/wacom_i2c.c b/drivers/input/touchscreen/wacom_i2c.c
> > index 22826c387da5..d3ea9aa8a98c 100644
> > --- a/drivers/input/touchscreen/wacom_i2c.c
> > +++ b/drivers/input/touchscreen/wacom_i2c.c
> > @@ -6,6 +6,7 @@
> >   * <tobita.tatsunosuke@wacom.co.jp>
> >   */
> >
> > +#include <linux/bits.h>
> >  #include <linux/module.h>
> >  #include <linux/input.h>
> >  #include <linux/i2c.h>
> > @@ -14,6 +15,14 @@
> >  #include <linux/interrupt.h>
> >  #include <asm/unaligned.h>
> >
> > +// Bitmasks (for data[3])
> > +#define WACOM_TIP_SWITCH       BIT(0)
> > +#define WACOM_BARREL_SWITCH    BIT(1)
> > +#define WACOM_ERASER           BIT(2)
> > +#define WACOM_BARREL_SWITCH_2  BIT(4)
> > +#define WACOM_IN_PROXIMITY     BIT(5)
> > +
> > +// Registers
> >  #define WACOM_CMD_QUERY0       0x04
> >  #define WACOM_CMD_QUERY1       0x00
> >  #define WACOM_CMD_QUERY2       0x33
> > @@ -99,10 +108,10 @@ static irqreturn_t wacom_i2c_irq(int irq, void *dev_id)
> >         if (error < 0)
> >                 goto out;
> >
> > -       tsw = data[3] & 0x01;
> > -       ers = data[3] & 0x04;
> > -       f1 = data[3] & 0x02;
> > -       f2 = data[3] & 0x10;
> > +       tsw = data[3] & WACOM_TIP_SWITCH;
> > +       ers = data[3] & WACOM_ERASER;
> > +       f1 = data[3] & WACOM_BARREL_SWITCH;
> > +       f2 = data[3] & WACOM_BARREL_SWITCH_2;
> >         x = le16_to_cpup((__le16 *)&data[4]);
> >         y = le16_to_cpup((__le16 *)&data[6]);
> >         pressure = le16_to_cpup((__le16 *)&data[8]);
> > @@ -111,7 +120,7 @@ static irqreturn_t wacom_i2c_irq(int irq, void *dev_id)
> >                 wac_i2c->tool = (data[3] & 0x0c) ?
> >                         BTN_TOOL_RUBBER : BTN_TOOL_PEN;
> >
> > -       wac_i2c->prox = data[3] & 0x20;
> > +       wac_i2c->prox = data[3] & WACOM_IN_PROXIMITY;
> >
> >         input_report_key(input, BTN_TOUCH, tsw || ers);
> >         input_report_key(input, wac_i2c->tool, wac_i2c->prox);
Dmitry Torokhov Nov. 10, 2021, 6:54 a.m. UTC | #4
On Tue, Nov 09, 2021 at 07:58:00PM +1000, Alistair Francis wrote:
> 
> I'm assuming you don't need me to re-send this. Let me know if you
> want me to though.

It is applied now, thank you.
diff mbox series

Patch

diff --git a/drivers/input/touchscreen/wacom_i2c.c b/drivers/input/touchscreen/wacom_i2c.c
index 8d7267ccc661..6865342db659 100644
--- a/drivers/input/touchscreen/wacom_i2c.c
+++ b/drivers/input/touchscreen/wacom_i2c.c
@@ -14,6 +14,12 @@ 
 #include <linux/interrupt.h>
 #include <asm/unaligned.h>
 
+// Bitmasks (for data[3])
+#define WACOM_TIP_SWITCH_bm         (1 << 0)
+#define WACOM_BARREL_SWITCH_bm      (1 << 1)
+#define WACOM_ERASER_bm             (1 << 2)
+#define WACOM_BARREL_SWITCH_2_bm    (1 << 4)
+
 // Registers
 #define WACOM_COMMAND_LSB   0x04
 #define WACOM_COMMAND_MSB   0x00
@@ -110,10 +116,10 @@  static irqreturn_t wacom_i2c_irq(int irq, void *dev_id)
 	if (error < 0)
 		goto out;
 
-	tsw = data[3] & 0x01;
-	ers = data[3] & 0x04;
-	f1 = data[3] & 0x02;
-	f2 = data[3] & 0x10;
+	tsw = data[3] & WACOM_TIP_SWITCH_bm;
+	ers = data[3] & WACOM_ERASER_bm;
+	f1 = data[3] & WACOM_BARREL_SWITCH_bm;
+	f2 = data[3] & WACOM_BARREL_SWITCH_2_bm;
 	x = le16_to_cpup((__le16 *)&data[4]);
 	y = le16_to_cpup((__le16 *)&data[6]);
 	pressure = le16_to_cpup((__le16 *)&data[8]);