Re: [PATCH v6] tty: Fix the keyboard led light display problem

From: andriy.shevchenko
Date: Mon Oct 25 2021 - 08:57:37 EST


On Mon, Oct 25, 2021 at 12:24:45PM +0800, changlianzhi@xxxxxxxxxxxxx wrote:
> On Mon, Oct 25, 2021 at 01:29:18AM +0800, changlianzhi@xxxxxxxxxxxxx wrote:
> > > >>On Thu, Oct 21, 2021 at 10:05:11AM +0800, lianzhi chang wrote:

...

> > > >> + unsigned long leds;
> > > >> +
> > > >> + leds = (unsigned long)ledstate;
> > >
> > > >Can be one line.
> > >
> > > >But most important question you are ignoring to answer / address is why do you
> > > >use casting?
>
> > > >>But most important question you are ignoring to answer / address is why do you
> > > >>use casting?
> > > Casting is used here because:
> > > The second parameter of test_bit is "volatile unsigned long *addr" (see constant_test_bit() in /arch/x86/include/asm/bitops.h), but ledstate is an "unsigned int" type. If casting is not used,
> > > compile Will report an error.
>
> > Wait, you are casting pointer to an integer?!
> > This is completely wrong in this case!
>
> Hello, I did not do that. For the complete code, please check the
> "patch v6" email I sent earlier. My code is like this:

> +static void kbd_update_ledstate(struct input_dev *dev)
> +{
> + unsigned long leds;
> +
> + leds = (unsigned long)ledstate;
> +
> + if (!!test_bit(LED_NUML, dev->led) != !!test_bit(VC_NUMLOCK, &leds))
> + ledstate ^= BIT(VC_NUMLOCK);
> + if (!!test_bit(LED_CAPSL, dev->led) != !!test_bit(VC_CAPSLOCK, &leds))
> + ledstate ^= BIT(VC_CAPSLOCK);
> + if (!!test_bit(LED_SCROLLL, dev->led) != !!test_bit(VC_SCROLLOCK, &leds))
> + ledstate ^= BIT(VC_SCROLLOCK);
> +}

> I mean, the second parameter of test_bit is "volatile unsigned long *addr",
> which is a pointer. And ledstate is an unsigned int type, so I defined an
> unsigned long type leds, assign the ledstate to the leds after forced
> conversion, and then when calling the test_bit function, get the address
> of the leds as a parameter.

This all is fine, but why casting is needed? I didn't still get.

--
With Best Regards,
Andy Shevchenko