[PATCH v2] spi: detect wrong transfer->len values

From: Michael Thalmeier
Date: Mon Sep 26 2011 - 04:56:05 EST


From: Helmut Raiger <helmut.raiger@xxxxxxx>

Instead of returning an error, which could have unknown consequences for
different drivers, just use dev_WARN_ONCE to output a warning.

Signed-off-by: Michael Thalmeier <michael.thalmeier@xxxxxxx>
---
drivers/spi/spi.c | 15 ++++++++++++++-
1 files changed, 14 insertions(+), 1 deletions(-)

diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index 4d1b9f5..49f6666 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -760,6 +760,20 @@ EXPORT_SYMBOL_GPL(spi_setup);
static int __spi_async(struct spi_device *spi, struct spi_message *message)
{
struct spi_master *master = spi->master;
+ struct spi_transfer *xfer;
+
+ /* check if number of bytes relates to bits per word mode */
+ list_for_each_entry(xfer, &message->transfers, transfer_list) {
+ u8 bpw = xfer->bits_per_word ?
+ xfer->bits_per_word : spi->bits_per_word;
+
+ dev_WARN_ONCE(&spi->dev,
+ ((bpw == 16 && (xfer->len & 1)) ||
+ (bpw == 32 && (xfer->len & 3))),
+ "incorrect number of bytes detected: "
+ "bpw=%u xfer->len=%u", bpw, xfer->len);
+
+ }

/* Half-duplex links include original MicroWire, and ones with
* only one data pin like SPI_3WIRE (switches direction) or where
@@ -768,7 +782,6 @@ static int __spi_async(struct spi_device *spi, struct spi_message *message)
*/
if ((master->flags & SPI_MASTER_HALF_DUPLEX)
|| (spi->mode & SPI_3WIRE)) {
- struct spi_transfer *xfer;
unsigned flags = master->flags;

list_for_each_entry(xfer, &message->transfers, transfer_list) {
--
1.7.6.2



--
Scanned by MailScanner.

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/