diff mbox

spi: add bits_per_word_mask check in spi_setup

Message ID 1372287286-12682-1-git-send-email-scott.jiang.linux@gmail.com (mailing list archive)
State Superseded, archived
Headers show

Commit Message

Scott Jiang June 26, 2013, 10:54 p.m. UTC
The spi_setup should fail if underlying controller or its driver
does not support requested bits_per_word. So if the master driver
set mask and let core do bits_per_word check then we should also
test this mask in spi_setup.

Signed-off-by: Scott Jiang <scott.jiang.linux@gmail.com>
---
 drivers/spi/spi.c |    9 +++++++++
 1 files changed, 9 insertions(+), 0 deletions(-)

Comments

Scott Jiang June 28, 2013, 2:51 a.m. UTC | #1
>> +     if (spi->master->bits_per_word_mask) {
>> +             /* Only 32 bits fit in the mask */
>> +             if (spi->bits_per_word > 32)
>> +                     return -EINVAL;
>> +             if (!(spi->master->bits_per_word_mask &
>> +                             BIT(spi->bits_per_word - 1)))
>> +                     return -EINVAL;
>> +     }
>
> Thinking about this a bit it might reject a valid transfer - it only
> checks the setting in the SPI device, not the setting in the transfer.
> This means that if the transfer overrides an unsuitable setting in the
> controller then an error will be generated as the fixed setting won't be
> seen.  It's hard to imagine when that would happen without something
> being very confused but still feels better to check the bits per word
> from the first transfer too.

But the API description said that this call would fail if the protocol
driver specifies an option
that the underlying controller or its driver does not support. It's
strange if I request 9 bits per
word in setup successfully while calling it in transfer fail.

------------------------------------------------------------------------------
This SF.net email is sponsored by Windows:

Build for Windows Store.

http://p.sf.net/sfu/windows-dev2dev
diff mbox

Patch

diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index 45cb6a9..dc8fde0 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -1326,6 +1326,15 @@  int spi_setup(struct spi_device *spi)
 	if (!spi->bits_per_word)
 		spi->bits_per_word = 8;
 
+	if (spi->master->bits_per_word_mask) {
+		/* Only 32 bits fit in the mask */
+		if (spi->bits_per_word > 32)
+			return -EINVAL;
+		if (!(spi->master->bits_per_word_mask &
+				BIT(spi->bits_per_word - 1)))
+			return -EINVAL;
+	}
+
 	if (spi->master->setup)
 		status = spi->master->setup(spi);