1 From 93cea2e49a3bd7f8e3fdf36915688b64c7f2d88f Mon Sep 17 00:00:00 2001
2 From: Sasha Levin <sashal@kernel.org>
3 Date: Wed, 14 Oct 2020 10:56:31 +0200
4 Subject: can: peak_usb: peak_usb_get_ts_time(): fix timestamp wrapping
6 From: Stephane Grosjean <s.grosjean@peak-system.com>
8 [ Upstream commit ecc7b4187dd388549544195fb13a11b4ea8e6a84 ]
10 Fabian Inostroza <fabianinostrozap@gmail.com> has discovered a potential
11 problem in the hardware timestamp reporting from the PCAN-USB USB CAN interface
12 (only), related to the fact that a timestamp of an event may precede the
13 timestamp used for synchronization when both records are part of the same USB
14 packet. However, this case was used to detect the wrapping of the time counter.
16 This patch details and fixes the two identified cases where this problem can
19 Reported-by: Fabian Inostroza <fabianinostrozap@gmail.com>
20 Signed-off-by: Stephane Grosjean <s.grosjean@peak-system.com>
21 Link: https://lore.kernel.org/r/20201014085631.15128-1-s.grosjean@peak-system.com
22 Fixes: bb4785551f64 ("can: usb: PEAK-System Technik USB adapters driver core")
23 Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
24 Signed-off-by: Sasha Levin <sashal@kernel.org>
26 drivers/net/can/usb/peak_usb/pcan_usb_core.c | 51 ++++++++++++++++++--
27 1 file changed, 46 insertions(+), 5 deletions(-)
29 diff --git a/drivers/net/can/usb/peak_usb/pcan_usb_core.c b/drivers/net/can/usb/peak_usb/pcan_usb_core.c
30 index 85d92f129af2d..9d78ba7776140 100644
31 --- a/drivers/net/can/usb/peak_usb/pcan_usb_core.c
32 +++ b/drivers/net/can/usb/peak_usb/pcan_usb_core.c
33 @@ -154,14 +154,55 @@ void peak_usb_get_ts_tv(struct peak_time_ref *time_ref, u32 ts,
34 /* protect from getting timeval before setting now */
35 if (time_ref->tv_host.tv_sec > 0) {
39 + /* General case: dev_ts_1 < dev_ts_2 < ts, with:
41 + * - dev_ts_1 = previous sync timestamp
42 + * - dev_ts_2 = last sync timestamp
43 + * - ts = event timestamp
44 + * - ts_period = known sync period (theoretical)
45 + * ~ dev_ts2 - dev_ts1
48 + * - time counters wrap (see adapter->ts_used_bits)
49 + * - sometimes, dev_ts_1 < ts < dev_ts2
51 + * "normal" case (sync time counters increase):
52 + * must take into account case when ts wraps (tsw)
56 + * ---+--------+----+-------0-+--+-->
57 + * ts_dev_1 | ts_dev_2 |
60 + if (time_ref->ts_dev_1 < time_ref->ts_dev_2) {
61 + /* case when event time (tsw) wraps */
62 + if (ts < time_ref->ts_dev_1)
63 + delta_ts = 1 << time_ref->adapter->ts_used_bits;
65 + /* Otherwise, sync time counter (ts_dev_2) has wrapped:
66 + * handle case when event time (tsn) hasn't.
70 + * ---+--------+--0-+---------+--+-->
71 + * ts_dev_1 | ts_dev_2 |
74 + } else if (time_ref->ts_dev_1 < ts) {
75 + delta_ts = -(1 << time_ref->adapter->ts_used_bits);
78 - delta_us = ts - time_ref->ts_dev_2;
79 - if (ts < time_ref->ts_dev_2)
80 - delta_us &= (1 << time_ref->adapter->ts_used_bits) - 1;
81 + /* add delay between last sync and event timestamps */
82 + delta_ts += (signed int)(ts - time_ref->ts_dev_2);
84 - delta_us += time_ref->ts_total;
85 + /* add time from beginning to last sync */
86 + delta_ts += time_ref->ts_total;
88 - delta_us *= time_ref->adapter->us_per_ts_scale;
89 + /* convert ticks number into microseconds */
90 + delta_us = delta_ts * time_ref->adapter->us_per_ts_scale;
91 delta_us >>= time_ref->adapter->us_per_ts_shift;
93 *tv = time_ref->tv_host_0;