Skip to contentSkip to conversation

spi: core: Validate length of the transfers in message

PATCH In review Ivan T. Ivanov 3 participants 7 messages Feb 20, 2014 – Feb 28, 2014 SPI

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 spi: core: Validate length of the transfers in message

From: "Ivan T. Ivanov" <[email protected]>

SPI transfer length should be multiple of SPI word size, where SPI word size should be power-of-two multiple

---
 drivers/spi/spi.c |   17 +++++++++++++++++
 1 file changed, 17 insertions(+)
drivers/spi/spi.c +17 -0
index d0b28bb..45b1610 100644
@@ -1617,6 +1617,7 @@ static int __spi_validate(struct spi_device *spi, struct spi_message *message)
 {
 	struct spi_master *master = spi->master;
 	struct spi_transfer *xfer;
+	int w_size, n_words;
 
 	if (list_empty(&message->transfers))
 		return -EINVAL;
@@ -1668,6 +1669,22 @@ static int __spi_validate(struct spi_device *spi, struct spi_message *message)
 				return -EINVAL;
 		}
 
+		/*
+		 * SPI transfer length should be multiple of SPI word size
+		 * where SPI word size should be power-of-two multiple
+		 */
+		if (xfer->bits_per_word <= 8)
+			w_size = 1;
+		else if (xfer->bits_per_word <= 16)
+			w_size = 2;
+		else
+			w_size = 4;
+
+		n_words = xfer->len / w_size;
+		/* No partial transfers accepted */
+		if (!n_words || xfer->len % w_size)
+			return -EINVAL;
+
 		if (xfer->speed_hz && master->min_speed_hz &&
 		    xfer->speed_hz < master->min_speed_hz)
 			return -EINVAL;
Signature · 6 lines
1.7.9.5

--
To unsubscribe from this list: send the line "unsubscribe linux-spi" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
maintainer
#1 lore raw
Quoted 4 lines · Ivan T. Ivanov

From: "Ivan T. Ivanov" <[email protected]>

SPI transfer length should be multiple of SPI word size, where SPI word size should be power-of-two multiple

OK, I checked all the existing users and everything using unusual bits per word settings seems to be doing the right thing here so I've applied this - thanks!

#2 lore raw
In reply to Ivan T. Ivanov
Quoted 2 lines · Ivan T. Ivanov

SPI transfer length should be multiple of SPI word size, where SPI word size should be power-of-two multiple

...

drivers/spi/spi.c static int __spi_validate(struct spi_device *spi, struct spi_message *message) · line 1686

Quoted 4 lines
drivers/spi/spi.c +4 -0
+		n_words = xfer->len / w_size;
+		/* No partial transfers accepted */
+		if (!n_words || xfer->len % w_size)
+			return -EINVAL;

Is xfer->len == 0 invalid?
Long time ago I have fixed atmel spi driver to support zero length transfer (commit 06719814 atmel_spi: support zero length transfer).

According to Documentation/spi/spi-summary, zeto length transfer seems valid.

+ optionally defining short delays after transfers ... using the spi_transfer.delay_usecs setting (this delay can be the only protocol effect, if the buffer length is zero);

---
Atsushi Nemoto
--
To unsubscribe from this list: send the line "unsubscribe linux-spi" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

maintainer
#3 lore raw
Quoted 10 lines · Atsushi Nemoto

Is xfer->len == 0 invalid?
Long time ago I have fixed atmel spi driver to support zero length transfer (commit 06719814 atmel_spi: support zero length transfer).

According to Documentation/spi/spi-summary, zeto length transfer seems valid.

+ optionally defining short delays after transfers ... using the spi_transfer.delay_usecs setting (this delay can be the only protocol effect, if the buffer length is zero);

*sigh* I guess it is valid, though frankly I'm concerned that this isn't a good idea - it's certainly not going to work reliably given the need for every driver to open code this, most of them get the delay stuff wrong.

#4 lore raw
Quoted 8 lines · Mark Brown
Quoted 3 lines

+ optionally defining short delays after transfers ... using the spi_transfer.delay_usecs setting (this delay can be the only protocol effect, if the buffer length is zero);

*sigh* I guess it is valid, though frankly I'm concerned that this isn't a good idea - it's certainly not going to work reliably given the need for every driver to open code this, most of them get the delay stuff wrong.

I don't object to the whole patch. Validating in spi core is good of course, and "xfer->len % w_size" part looks no problem.

I just want to keep ways to handle an odd device, for example, which requires long delay between chipselect and the first transfer, etc.

---
Atsushi Nemoto

maintainer
#5 lore raw
Quoted 5 lines · Atsushi Nemoto

I don't object to the whole patch. Validating in spi core is good of course, and "xfer->len % w_size" part looks no problem.

I just want to keep ways to handle an odd device, for example, which requires long delay between chipselect and the first transfer, etc.

Can you submit a patch adding that support back please?

#6 lore raw
Quoted 7 lines · Mark Brown
Quoted 5 lines

I don't object to the whole patch. Validating in spi core is good of course, and "xfer->len % w_size" part looks no problem.

I just want to keep ways to handle an odd device, for example, which requires long delay between chipselect and the first transfer, etc.

Can you submit a patch adding that support back please?

OK, I will send a patch against spi.git/for-next branch.

---
Atsushi Nemoto
--
To unsubscribe from this list: send the line "unsubscribe linux-spi" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html