Skip to contentSkip to conversation

spi_mpc83xx: handle odd transfer sizes

PATCH In review Peter Korsgaard 3 participants 7 messages Sep 10, 2008 – Sep 11, 2008 6 messages not in the archive SPI

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 spi_mpc83xx: handle odd transfer sizes

Error out on transfer length != multiple of bytes per word.

---
 drivers/spi/spi_mpc83xx.c |   13 +++++++++++--
 1 files changed, 11 insertions(+), 2 deletions(-)
drivers/spi/spi_mpc83xx.c +11 -2
index 070c621..c04a25d 100644
@@ -315,11 +315,20 @@ static int mpc83xx_spi_bufs(struct spi_device *spi, struct spi_transfer *t)
 	if (t->bits_per_word)
 		bits_per_word = t->bits_per_word;
 	len = t->len;
-	if (bits_per_word > 8)
+	if (bits_per_word > 8) {
+		/* invalid length? */
+		if (len & 1)
+			return len;
 		len /= 2;
-	if (bits_per_word > 16)
+	}
+	if (bits_per_word > 16) {
+		/* invalid length? */
+		if (len & 1)
+			return len;
 		len /= 2;
+	}
 	mpc83xx_spi->count = len;
+
 	INIT_COMPLETION(mpc83xx_spi->done);
 
 	/* enable rx ints */
Signature · 8 lines
1.5.6.3


-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
#2 lore raw
In reply to a message not in the archive
Quoted 3 lines · Peter Korsgaard

Error out on transfer length != multiple of bytes per word.

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge Build the coolest Linux based applications with Moblin SDK & win great prizes Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/

#3 lore raw
In reply to a message not in the archive
Quoted 3 lines · Peter Korsgaard

+               /* invalid length? */
+               if (len & 1)
+                       return len;

Count me puzzled ... "return -EINVAL;" would make sense. You're succeeding here, not failing.

But then this routine puzzles me anyway. It always returns len, and then the caller fails with -EMSGSIZE if that return is nonzero. Surely the routine should return zero-or-errno?

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge Build the coolest Linux based applications with Moblin SDK & win great prizes Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/

#5 lore raw
In reply to a message not in the archive
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"David" == David Brownell <[email protected]> writes:

Quoted 3 lines · Peter Korsgaard
Quoted 3 lines

+               /* invalid length? */
+               if (len & 1)
+                       return len;

David> Count me puzzled ... "return -EINVAL;" would make sense. David> You're succeeding here, not failing.

David> But then this routine puzzles me anyway. It always returns David> len, and then the caller fails with -EMSGSIZE if that return David> is nonzero. Surely the routine should return zero-or-errno?

Yeah, it isn't the prettiest code. The function returns the amount of words NOT transferred - And the caller converts that to -EMSGSIZE if it's != 0.

It would indeed be clearer to change it to to return -errno on errors instead, but the problem my patch solves is real (crash on too small message lengths) and I wanted to keep the patch to it's minimum now that we're so close to 2.6.27.

But I can change it to use return -EINVAL instead if you prefer - The caller doesn't care as long as it's != 0.

Then we can do the real cleanup later and push it for 2.6.28.

OK?

Signature · 11 lines
Bye, Peter Korsgaard

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
spi-devel-general mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/spi-devel-general
#7 lore raw
In reply to a message not in the archive
Quoted 9 lines · Peter Korsgaard

It would indeed be clearer to change it to to return -errno on errors instead, but the problem my patch solves is real (crash on too small message lengths) and I wanted to keep the patch to it's minimum now that we're so close to 2.6.27.

But I can change it to use return -EINVAL instead if you prefer - The caller doesn't care as long as it's != 0.

Then we can do the real cleanup later and push it for 2.6.28.

OK, I was thinking this minimal tweak to your patch. It adds only one more patch segment, and makes the routine obey stndard kernel calling conventions ... still a small patch. :)

drivers/spi/spi_mpc83xx.c +3 -3
@@ -315,13 +315,13 @@ static int mpc83xx_spi_bufs(struct spi_d
 	if (bits_per_word > 8) {
 		/* invalid length? */
 		if (len & 1)
-			return len;
+			return -EINVAL;
 		len /= 2;
 	}
 	if (bits_per_word > 16) {
 		/* invalid length? */
 		if (len & 1)
-			return len;
+			return -EINVAL;
 		len /= 2;
 	}
 	mpc83xx_spi->count = len;
@@ -340,7 +340,7 @@ static int mpc83xx_spi_bufs(struct spi_d
 	/* disable rx ints */
 	mpc83xx_spi_write_reg(&mpc83xx_spi->base->mask, 0);
 
-	return mpc83xx_spi->count;
+	return 0;
 }
 
 static void mpc83xx_spi_work(struct work_struct *work)

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge Build the coolest Linux based applications with Moblin SDK & win great prizes Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/

#9 lore raw
In reply to a message not in the archive
Quoted 15 lines

-----Original Message-----
From: David Brownell [mailto:david-b-yBeKhBN/[email protected]]
Sent: den 10 september 2008 23:06
To: Peter Korsgaard
Cc: spi-devel-general-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org; [email protected]
Subject: Re: [PATCH] spi_mpc83xx: handle odd transfer sizes

Quoted 5 lines · Peter Korsgaard

It would indeed be clearer to change it to to return -errno on errors instead, but the problem my patch solves is real (crash on too small message lengths) and I wanted to keep the patch to it's minimum now that we're so close to 2.6.27.

Quoted 8 lines
Quoted 4 lines

But I can change it to use return -EINVAL instead if you prefer - The caller doesn't care as long as it's != 0.

Then we can do the real cleanup later and push it for 2.6.28.

OK, I was thinking this minimal tweak to your patch. It adds only one more patch segment, and makes the routine obey stndard kernel calling conventions ... still a small patch. :)

Yes, this is fine, but ..

Quoted 22 lines
drivers/spi/spi_mpc83xx.c +2 -2
@@ -315,13 +315,13 @@ static int mpc83xx_spi_bufs(struct spi_d
 	if (bits_per_word > 8) {
 		/* invalid length? */
 		if (len & 1)
-			return len;
+			return -EINVAL;
 		len /= 2;
 	}
 	if (bits_per_word > 16) {
 		/* invalid length? */
 		if (len & 1)
-			return len;
+			return -EINVAL;
 		len /= 2;
 	}
 	mpc83xx_spi->count = len;

drivers/spi/spi_mpc83xx.c static int mpc83xx_spi_bufs(struct spi_d · line 343

Quoted 5 lines
drivers/spi/spi_mpc83xx.c +1 -1
@@ -340,7 +340,7 @@ static int mpc83xx_spi_bufs(struct spi_d
 	/* disable rx ints */
 	mpc83xx_spi_write_reg(&mpc83xx_spi->base->mask, 0);
 
-	return mpc83xx_spi->count;
+	return 0;

This hunk should probably stay untouched, not sure the mpc83xx_spi->count always is zero in case of TX errors.

Jocke

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge Build the coolest Linux based applications with Moblin SDK & win great prizes Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/

#12 lore raw
In reply to a message not in the archive
Quoted 6 lines · Joakim Tjernlund
Quoted 3 lines

-     return mpc83xx_spi->count;
+     return 0;

This hunk should probably stay untouched, not sure the mpc83xx_spi->count always is zero in case of TX errors.

Good point. I'll just change the "return len" to "return -EINVAL" then and leave the rest alone. Fault handling could stand to be reworked in many places here ... if there's a TX (or RX) error, this version always returns -EMSGSIZE!

Likewise, these particular faults would be better off getting reported before mpc83xx_spi_transfer() queues the transfer. Aborting mid-transfer when you knew up front that you'd have to do so ... not friendly!

- Dave

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge Build the coolest Linux based applications with Moblin SDK & win great prizes Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/