linux-spi.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] spi: spidev: mask SPI_CS_HIGH in SPI_IOC_RD_MODE
@ 2022-11-30 14:39 A. Sverdlin
  2022-11-30 14:57 ` Mark Brown
  0 siblings, 1 reply; 4+ messages in thread
From: A. Sverdlin @ 2022-11-30 14:39 UTC (permalink / raw)
  To: linux-spi
  Cc: Alexander Sverdlin, Mark Brown, linux-kernel, Linus Walleij,
	Lukas Wunner

From: Alexander Sverdlin <alexander.sverdlin@siemens.com>

Commit f3186dd87669 ("spi: Optionally use GPIO descriptors for CS GPIOs")
has changed the user-space interface so that bogus SPI_CS_HIGH started
to appear in the mask returned by SPI_IOC_RD_MODE even for active-low CS
pins. Commit 138c9c32f090
("spi: spidev: Fix CS polarity if GPIO descriptors are used") fixed only
SPI_IOC_WR_MODE part of the problem. Let's fix SPI_IOC_RD_MODE
symmetrically.

Test case:

	#include <sys/ioctl.h>
	#include <fcntl.h>
	#include <linux/spi/spidev.h>

	int main(int argc, char **argv)
	{
		char modew = SPI_CPHA;
		char moder;
		int f = open("/dev/spidev0.0", O_RDWR);

		if (f < 0)
			return 1;

		ioctl(f, SPI_IOC_WR_MODE, &modew);
		ioctl(f, SPI_IOC_RD_MODE, &moder);

		return moder == modew ? 0 : 2;
	}

Fixes: f3186dd87669 ("spi: Optionally use GPIO descriptors for CS GPIOs")
Signed-off-by: Alexander Sverdlin <alexander.sverdlin@siemens.com>
---
 drivers/spi/spidev.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/drivers/spi/spidev.c b/drivers/spi/spidev.c
index b2775d82d2d7b..23177a7c916c3 100644
--- a/drivers/spi/spidev.c
+++ b/drivers/spi/spidev.c
@@ -377,8 +377,17 @@ spidev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 	switch (cmd) {
 	/* read requests */
 	case SPI_IOC_RD_MODE:
-		retval = put_user(spi->mode & SPI_MODE_MASK,
-					(__u8 __user *)arg);
+		tmp = spi->mode;
+
+		{
+			struct spi_controller *ctlr = spi->controller;
+
+			if (ctlr->use_gpio_descriptors && ctlr->cs_gpiods &&
+			    ctlr->cs_gpiods[spi->chip_select])
+				tmp &= ~SPI_CS_HIGH;
+		}
+
+		retval = put_user(tmp & SPI_MODE_MASK, (__u8 __user *)arg);
 		break;
 	case SPI_IOC_RD_MODE32:
 		retval = put_user(spi->mode & SPI_MODE_MASK,
-- 
2.34.1


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

* Re: [PATCH] spi: spidev: mask SPI_CS_HIGH in SPI_IOC_RD_MODE
  2022-11-30 14:39 [PATCH] spi: spidev: mask SPI_CS_HIGH in SPI_IOC_RD_MODE A. Sverdlin
@ 2022-11-30 14:57 ` Mark Brown
  2022-11-30 15:43   ` Sverdlin, Alexander
  0 siblings, 1 reply; 4+ messages in thread
From: Mark Brown @ 2022-11-30 14:57 UTC (permalink / raw)
  To: A. Sverdlin; +Cc: linux-spi, linux-kernel, Linus Walleij, Lukas Wunner

[-- Attachment #1: Type: text/plain, Size: 652 bytes --]

On Wed, Nov 30, 2022 at 03:39:48PM +0100, A. Sverdlin wrote:

>  	/* read requests */
>  	case SPI_IOC_RD_MODE:
> -		retval = put_user(spi->mode & SPI_MODE_MASK,
> -					(__u8 __user *)arg);
> +		tmp = spi->mode;
> +
> +		{
> +			struct spi_controller *ctlr = spi->controller;
> +
> +			if (ctlr->use_gpio_descriptors && ctlr->cs_gpiods &&
> +			    ctlr->cs_gpiods[spi->chip_select])
> +				tmp &= ~SPI_CS_HIGH;
> +		}
> +
> +		retval = put_user(tmp & SPI_MODE_MASK, (__u8 __user *)arg);
>  		break;
>  	case SPI_IOC_RD_MODE32:
>  		retval = put_user(spi->mode & SPI_MODE_MASK,

What about SPI_IOC_RD_MODE_32?  On the write path the code is shared...

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

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

* Re: [PATCH] spi: spidev: mask SPI_CS_HIGH in SPI_IOC_RD_MODE
  2022-11-30 14:57 ` Mark Brown
@ 2022-11-30 15:43   ` Sverdlin, Alexander
  2022-11-30 16:25     ` Mark Brown
  0 siblings, 1 reply; 4+ messages in thread
From: Sverdlin, Alexander @ 2022-11-30 15:43 UTC (permalink / raw)
  To: broonie; +Cc: linux-spi, lukas, linux-kernel, linus.walleij

Hello Mark,

On Wed, 2022-11-30 at 14:57 +0000, Mark Brown wrote:
> >   	/* read requests */
> >   	case SPI_IOC_RD_MODE:
> > -		retval = put_user(spi->mode & SPI_MODE_MASK,
> > -					(__u8 __user *)arg);
> > +		tmp = spi->mode;
> > +
> > +		{
> > +			struct spi_controller *ctlr = spi-
> > >controller;
> > +
> > +			if (ctlr->use_gpio_descriptors && ctlr-
> > >cs_gpiods &&
> > +			    ctlr->cs_gpiods[spi->chip_select])
> > +				tmp &= ~SPI_CS_HIGH;
> > +		}
> > +
> > +		retval = put_user(tmp & SPI_MODE_MASK, (__u8
> > __user *)arg);
> >   		break;
> >   	case SPI_IOC_RD_MODE32:
> >   		retval = put_user(spi->mode & SPI_MODE_MASK,
> 
> What about SPI_IOC_RD_MODE_32?  On the write path the code is
> shared...

you are right, thanks for this hint!
I'll re-send if there are no objections to this approach in general.

-- 
Alexander Sverdlin
Siemens AG
www.siemens.com

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

* Re: [PATCH] spi: spidev: mask SPI_CS_HIGH in SPI_IOC_RD_MODE
  2022-11-30 15:43   ` Sverdlin, Alexander
@ 2022-11-30 16:25     ` Mark Brown
  0 siblings, 0 replies; 4+ messages in thread
From: Mark Brown @ 2022-11-30 16:25 UTC (permalink / raw)
  To: Sverdlin, Alexander; +Cc: linux-spi, lukas, linux-kernel, linus.walleij

[-- Attachment #1: Type: text/plain, Size: 343 bytes --]

On Wed, Nov 30, 2022 at 03:43:27PM +0000, Sverdlin, Alexander wrote:
> On Wed, 2022-11-30 at 14:57 +0000, Mark Brown wrote:

> > What about SPI_IOC_RD_MODE_32?  On the write path the code is
> > shared...

> you are right, thanks for this hint!
> I'll re-send if there are no objections to this approach in general.

Should be fine.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

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

end of thread, other threads:[~2022-11-30 16:25 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-30 14:39 [PATCH] spi: spidev: mask SPI_CS_HIGH in SPI_IOC_RD_MODE A. Sverdlin
2022-11-30 14:57 ` Mark Brown
2022-11-30 15:43   ` Sverdlin, Alexander
2022-11-30 16:25     ` Mark Brown

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