linux-spi.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2 RESEND] spi: check bits_per_word in spi_setup
       [not found] ` <1440338791-30841-1-git-send-email-stefan.bruens-vA1bhqPz9FBZXbeN9DUtxg@public.gmane.org>
@ 2015-08-23 14:06   ` Stefan Brüns
  2015-08-23 14:06   ` [PATCH 2/2 RESEND] staging/fbtft: use spi_setup instead of direct call to master->setup Stefan Brüns
  1 sibling, 0 replies; 3+ messages in thread
From: Stefan Brüns @ 2015-08-23 14:06 UTC (permalink / raw)
  To: linux-spi-u79uwXL29TY76Z2rM5mHXA
  Cc: Mark Brown, Noralf Trønnes, Stefan Brüns

This allows drivers for devices connected via SPI to check if the
controller supports a given bits_per_word value during setup.
Currently any BPW value is accepted durings setup, and transfers
are rejected later.

Signed-off-by: Stefan Brüns <stefan.bruens-vA1bhqPz9FBZXbeN9DUtxg@public.gmane.org>
---
 drivers/spi/spi.c | 27 +++++++++++++++++++--------
 1 file changed, 19 insertions(+), 8 deletions(-)

diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index cf8b91b..98aaa31 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -1740,6 +1740,20 @@ EXPORT_SYMBOL_GPL(spi_busnum_to_master);
  * other core methods are currently defined as inline functions.
  */
 
+static int __spi_validate_bits_per_word(struct spi_master *master, u8 bits_per_word)
+{
+	if (master->bits_per_word_mask) {
+		/* Only 32 bits fit in the mask */
+		if (bits_per_word > 32)
+			return -EINVAL;
+		if (!(master->bits_per_word_mask &
+				SPI_BPW_MASK(bits_per_word)))
+			return -EINVAL;
+	}
+
+	return 0;
+}
+
 /**
  * spi_setup - setup SPI mode and clock rate
  * @spi: the device whose settings are being modified
@@ -1798,6 +1812,9 @@ int spi_setup(struct spi_device *spi)
 	if (!spi->bits_per_word)
 		spi->bits_per_word = 8;
 
+	if (__spi_validate_bits_per_word(spi->master, spi->bits_per_word))
+		return -EINVAL;
+
 	if (!spi->max_speed_hz)
 		spi->max_speed_hz = spi->master->max_speed_hz;
 
@@ -1865,14 +1882,8 @@ static int __spi_validate(struct spi_device *spi, struct spi_message *message)
 		    xfer->speed_hz > master->max_speed_hz)
 			xfer->speed_hz = master->max_speed_hz;
 
-		if (master->bits_per_word_mask) {
-			/* Only 32 bits fit in the mask */
-			if (xfer->bits_per_word > 32)
-				return -EINVAL;
-			if (!(master->bits_per_word_mask &
-					BIT(xfer->bits_per_word - 1)))
-				return -EINVAL;
-		}
+		if (__spi_validate_bits_per_word(master, xfer->bits_per_word))
+			return -EINVAL;
 
 		/*
 		 * SPI transfer length should be multiple of SPI word size
-- 
2.1.4

--
To unsubscribe from this list: send the line "unsubscribe linux-spi" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* [PATCH 2/2 RESEND] staging/fbtft: use spi_setup instead of direct call to master->setup
       [not found] ` <1440338791-30841-1-git-send-email-stefan.bruens-vA1bhqPz9FBZXbeN9DUtxg@public.gmane.org>
  2015-08-23 14:06   ` [PATCH 1/2 RESEND] spi: check bits_per_word in spi_setup Stefan Brüns
@ 2015-08-23 14:06   ` Stefan Brüns
       [not found]     ` <4787d5ed-b810-4d3e-b1bc-3a6db745d444-nCK4yPIUxYSnzeKPzzZwVg@public.gmane.org>
  1 sibling, 1 reply; 3+ messages in thread
From: Stefan Brüns @ 2015-08-23 14:06 UTC (permalink / raw)
  To: linux-spi-u79uwXL29TY76Z2rM5mHXA
  Cc: Mark Brown, Noralf Trønnes, Stefan Brüns

Avoid a crash, as master->setup may be NULL (e.g. xilinx controller).
Even if master->setup is set, spi_setup does several compatibility/
sanity checks which should not be skipped (fixes problems with
displays/controllers needing emulation for bits_per_word = 9).

Signed-off-by: Stefan Brüns <stefan.bruens-vA1bhqPz9FBZXbeN9DUtxg@public.gmane.org>
---
 drivers/staging/fbtft/fb_watterott.c | 4 ++--
 drivers/staging/fbtft/fbtft-core.c   | 4 ++--
 drivers/staging/fbtft/flexfb.c       | 4 ++--
 3 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/drivers/staging/fbtft/fb_watterott.c b/drivers/staging/fbtft/fb_watterott.c
index 88fb2c0..8eae6ef 100644
--- a/drivers/staging/fbtft/fb_watterott.c
+++ b/drivers/staging/fbtft/fb_watterott.c
@@ -169,7 +169,7 @@ static int init_display(struct fbtft_par *par)
 	/* enable SPI interface by having CS and MOSI low during reset */
 	save_mode = par->spi->mode;
 	par->spi->mode |= SPI_CS_HIGH;
-	ret = par->spi->master->setup(par->spi); /* set CS inactive low */
+	ret = spi_setup(par->spi); /* set CS inactive low */
 	if (ret) {
 		dev_err(par->info->device, "Could not set SPI_CS_HIGH\n");
 		return ret;
@@ -180,7 +180,7 @@ static int init_display(struct fbtft_par *par)
 	par->fbtftops.reset(par);
 	mdelay(1000);
 	par->spi->mode = save_mode;
-	ret = par->spi->master->setup(par->spi);
+	ret = spi_setup(par->spi);
 	if (ret) {
 		dev_err(par->info->device, "Could not restore SPI mode\n");
 		return ret;
diff --git a/drivers/staging/fbtft/fbtft-core.c b/drivers/staging/fbtft/fbtft-core.c
index 9cc8141..ba08da3 100644
--- a/drivers/staging/fbtft/fbtft-core.c
+++ b/drivers/staging/fbtft/fbtft-core.c
@@ -1434,12 +1434,12 @@ int fbtft_probe_common(struct fbtft_display *display,
 	/* 9-bit SPI setup */
 	if (par->spi && display->buswidth == 9) {
 		par->spi->bits_per_word = 9;
-		ret = par->spi->master->setup(par->spi);
+		ret = spi_setup(par->spi);
 		if (ret) {
 			dev_warn(&par->spi->dev,
 				"9-bit SPI not available, emulating using 8-bit.\n");
 			par->spi->bits_per_word = 8;
-			ret = par->spi->master->setup(par->spi);
+			ret = spi_setup(par->spi);
 			if (ret)
 				goto out_release;
 			/* allocate buffer with room for dc bits */
diff --git a/drivers/staging/fbtft/flexfb.c b/drivers/staging/fbtft/flexfb.c
index 2c4ce07..fecf486 100644
--- a/drivers/staging/fbtft/flexfb.c
+++ b/drivers/staging/fbtft/flexfb.c
@@ -440,12 +440,12 @@ static int flexfb_probe_common(struct spi_device *sdev,
 			par->fbtftops.write_register = fbtft_write_reg8_bus9;
 			par->fbtftops.write_vmem = fbtft_write_vmem16_bus9;
 			sdev->bits_per_word = 9;
-			ret = sdev->master->setup(sdev);
+			ret = spi_setup(sdev);
 			if (ret) {
 				dev_warn(dev,
 					"9-bit SPI not available, emulating using 8-bit.\n");
 				sdev->bits_per_word = 8;
-				ret = sdev->master->setup(sdev);
+				ret = spi_setup(sdev);
 				if (ret)
 					goto out_release;
 				/* allocate buffer with room for dc bits */
-- 
2.1.4

--
To unsubscribe from this list: send the line "unsubscribe linux-spi" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

* Re: [PATCH 2/2 RESEND] staging/fbtft: use spi_setup instead of direct call to master->setup
       [not found]     ` <4787d5ed-b810-4d3e-b1bc-3a6db745d444-nCK4yPIUxYSnzeKPzzZwVg@public.gmane.org>
@ 2015-08-23 15:32       ` Noralf Trønnes
  0 siblings, 0 replies; 3+ messages in thread
From: Noralf Trønnes @ 2015-08-23 15:32 UTC (permalink / raw)
  To: Stefan Brüns, linux-spi-u79uwXL29TY76Z2rM5mHXA; +Cc: Mark Brown

Hi Stefan,

fbtft lives in staging/fbtft not drivers/spi, so you need to send it to 
the correct list:
devel-gWbeCf7V1WCQmaza687I9mD2FQJk+8+b@public.gmane.org

See https://github.com/notro/fbtft/wiki/Development


Noralf.

Den 23.08.2015 16:06, skrev Stefan Brüns:
> Avoid a crash, as master->setup may be NULL (e.g. xilinx controller).
> Even if master->setup is set, spi_setup does several compatibility/
> sanity checks which should not be skipped (fixes problems with
> displays/controllers needing emulation for bits_per_word = 9).
>
> Signed-off-by: Stefan Brüns <stefan.bruens-vA1bhqPz9FBZXbeN9DUtxg@public.gmane.org>
> ---
>   drivers/staging/fbtft/fb_watterott.c | 4 ++--
>   drivers/staging/fbtft/fbtft-core.c   | 4 ++--
>   drivers/staging/fbtft/flexfb.c       | 4 ++--
>   3 files changed, 6 insertions(+), 6 deletions(-)
>
> diff --git a/drivers/staging/fbtft/fb_watterott.c b/drivers/staging/fbtft/fb_watterott.c
> index 88fb2c0..8eae6ef 100644
> --- a/drivers/staging/fbtft/fb_watterott.c
> +++ b/drivers/staging/fbtft/fb_watterott.c
> @@ -169,7 +169,7 @@ static int init_display(struct fbtft_par *par)
>   	/* enable SPI interface by having CS and MOSI low during reset */
>   	save_mode = par->spi->mode;
>   	par->spi->mode |= SPI_CS_HIGH;
> -	ret = par->spi->master->setup(par->spi); /* set CS inactive low */
> +	ret = spi_setup(par->spi); /* set CS inactive low */
>   	if (ret) {
>   		dev_err(par->info->device, "Could not set SPI_CS_HIGH\n");
>   		return ret;
> @@ -180,7 +180,7 @@ static int init_display(struct fbtft_par *par)
>   	par->fbtftops.reset(par);
>   	mdelay(1000);
>   	par->spi->mode = save_mode;
> -	ret = par->spi->master->setup(par->spi);
> +	ret = spi_setup(par->spi);
>   	if (ret) {
>   		dev_err(par->info->device, "Could not restore SPI mode\n");
>   		return ret;
> diff --git a/drivers/staging/fbtft/fbtft-core.c b/drivers/staging/fbtft/fbtft-core.c
> index 9cc8141..ba08da3 100644
> --- a/drivers/staging/fbtft/fbtft-core.c
> +++ b/drivers/staging/fbtft/fbtft-core.c
> @@ -1434,12 +1434,12 @@ int fbtft_probe_common(struct fbtft_display *display,
>   	/* 9-bit SPI setup */
>   	if (par->spi && display->buswidth == 9) {
>   		par->spi->bits_per_word = 9;
> -		ret = par->spi->master->setup(par->spi);
> +		ret = spi_setup(par->spi);
>   		if (ret) {
>   			dev_warn(&par->spi->dev,
>   				"9-bit SPI not available, emulating using 8-bit.\n");
>   			par->spi->bits_per_word = 8;
> -			ret = par->spi->master->setup(par->spi);
> +			ret = spi_setup(par->spi);
>   			if (ret)
>   				goto out_release;
>   			/* allocate buffer with room for dc bits */
> diff --git a/drivers/staging/fbtft/flexfb.c b/drivers/staging/fbtft/flexfb.c
> index 2c4ce07..fecf486 100644
> --- a/drivers/staging/fbtft/flexfb.c
> +++ b/drivers/staging/fbtft/flexfb.c
> @@ -440,12 +440,12 @@ static int flexfb_probe_common(struct spi_device *sdev,
>   			par->fbtftops.write_register = fbtft_write_reg8_bus9;
>   			par->fbtftops.write_vmem = fbtft_write_vmem16_bus9;
>   			sdev->bits_per_word = 9;
> -			ret = sdev->master->setup(sdev);
> +			ret = spi_setup(sdev);
>   			if (ret) {
>   				dev_warn(dev,
>   					"9-bit SPI not available, emulating using 8-bit.\n");
>   				sdev->bits_per_word = 8;
> -				ret = sdev->master->setup(sdev);
> +				ret = spi_setup(sdev);
>   				if (ret)
>   					goto out_release;
>   				/* allocate buffer with room for dc bits */

--
To unsubscribe from this list: send the line "unsubscribe linux-spi" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

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

end of thread, other threads:[~2015-08-23 15:32 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <1440338791-30841-1-git-send-email-stefan.bruens@rwth-aachen.de>
     [not found] ` <1440338791-30841-1-git-send-email-stefan.bruens-vA1bhqPz9FBZXbeN9DUtxg@public.gmane.org>
2015-08-23 14:06   ` [PATCH 1/2 RESEND] spi: check bits_per_word in spi_setup Stefan Brüns
2015-08-23 14:06   ` [PATCH 2/2 RESEND] staging/fbtft: use spi_setup instead of direct call to master->setup Stefan Brüns
     [not found]     ` <4787d5ed-b810-4d3e-b1bc-3a6db745d444-nCK4yPIUxYSnzeKPzzZwVg@public.gmane.org>
2015-08-23 15:32       ` Noralf Trønnes

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