Bluetooth: Improve ERTM sequence number offset calculation
authorMat Martineau <mathewm@codeaurora.org>
Wed, 25 Apr 2012 23:36:12 +0000 (16:36 -0700)
committerGustavo Padovan <gustavo@padovan.org>
Wed, 9 May 2012 04:40:46 +0000 (01:40 -0300)
Instead of using modular division, the offset can be calculated using
only addition and subtraction.  The previous calculation did not work
as intended and was more difficult to understand, involving unsigned
integer underflow and a check for a negative value where one was not
possible.

Signed-off-by: Mat Martineau <mathewm@codeaurora.org>
Acked-by: Marcel Holtmann <marcel@holtmann.org>
Signed-off-by: Gustavo Padovan <gustavo@padovan.org>
include/net/bluetooth/l2cap.h

index 86bb83b..084dec0 100644 (file)
@@ -724,13 +724,10 @@ static inline bool l2cap_clear_timer(struct l2cap_chan *chan,
 
 static inline int __seq_offset(struct l2cap_chan *chan, __u16 seq1, __u16 seq2)
 {
-       int offset;
-
-       offset = (seq1 - seq2) % (chan->tx_win_max + 1);
-       if (offset < 0)
-               offset += (chan->tx_win_max + 1);
-
-       return offset;
+       if (seq1 >= seq2)
+               return seq1 - seq2;
+       else
+               return chan->tx_win_max + 1 - seq2 + seq1;
 }
 
 static inline __u16 __next_seq(struct l2cap_chan *chan, __u16 seq)