Skip to content

Commit aa4f3c6

Browse files
arndbmarckleinebudde
authored andcommitted
can: peak_usb: use ktime_t consistently
This changes the calculation of the timestamps to use ktime_t instead of struct timeval as the base. This gets rid of one of the few remaining users of the deprecated ktime_to_timeval() and timeval_to_ktime() helpers. The code should also get more efficient, as we have now removed all of the divisions. I have left the cut-off for resetting the counters as 4.200 seconds, in order to leave the behavior unchanged otherwise. Signed-off-by: Arnd Bergmann <[email protected]> Signed-off-by: Marc Kleine-Budde <[email protected]>
1 parent d5888a1 commit aa4f3c6

File tree

2 files changed

+15
-33
lines changed

2 files changed

+15
-33
lines changed

drivers/net/can/usb/peak_usb/pcan_usb_core.c

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -80,21 +80,6 @@ void peak_usb_init_time_ref(struct peak_time_ref *time_ref,
8080
}
8181
}
8282

83-
static void peak_usb_add_us(struct timeval *tv, u32 delta_us)
84-
{
85-
/* number of s. to add to final time */
86-
u32 delta_s = delta_us / 1000000;
87-
88-
delta_us -= delta_s * 1000000;
89-
90-
tv->tv_usec += delta_us;
91-
if (tv->tv_usec >= 1000000) {
92-
tv->tv_usec -= 1000000;
93-
delta_s++;
94-
}
95-
tv->tv_sec += delta_s;
96-
}
97-
9883
/*
9984
* sometimes, another now may be more recent than current one...
10085
*/
@@ -103,7 +88,7 @@ void peak_usb_update_ts_now(struct peak_time_ref *time_ref, u32 ts_now)
10388
time_ref->ts_dev_2 = ts_now;
10489

10590
/* should wait at least two passes before computing */
106-
if (time_ref->tv_host.tv_sec > 0) {
91+
if (ktime_to_ns(time_ref->tv_host) > 0) {
10792
u32 delta_ts = time_ref->ts_dev_2 - time_ref->ts_dev_1;
10893

10994
if (time_ref->ts_dev_2 < time_ref->ts_dev_1)
@@ -118,26 +103,26 @@ void peak_usb_update_ts_now(struct peak_time_ref *time_ref, u32 ts_now)
118103
*/
119104
void peak_usb_set_ts_now(struct peak_time_ref *time_ref, u32 ts_now)
120105
{
121-
if (time_ref->tv_host_0.tv_sec == 0) {
106+
if (ktime_to_ns(time_ref->tv_host_0) == 0) {
122107
/* use monotonic clock to correctly compute further deltas */
123-
time_ref->tv_host_0 = ktime_to_timeval(ktime_get());
124-
time_ref->tv_host.tv_sec = 0;
108+
time_ref->tv_host_0 = ktime_get();
109+
time_ref->tv_host = ktime_set(0, 0);
125110
} else {
126111
/*
127-
* delta_us should not be >= 2^32 => delta_s should be < 4294
112+
* delta_us should not be >= 2^32 => delta should be < 4294s
128113
* handle 32-bits wrapping here: if count of s. reaches 4200,
129114
* reset counters and change time base
130115
*/
131-
if (time_ref->tv_host.tv_sec != 0) {
132-
u32 delta_s = time_ref->tv_host.tv_sec
133-
- time_ref->tv_host_0.tv_sec;
134-
if (delta_s > 4200) {
116+
if (ktime_to_ns(time_ref->tv_host)) {
117+
ktime_t delta = ktime_sub(time_ref->tv_host,
118+
time_ref->tv_host_0);
119+
if (ktime_to_ns(delta) > (4200ull * NSEC_PER_SEC)) {
135120
time_ref->tv_host_0 = time_ref->tv_host;
136121
time_ref->ts_total = 0;
137122
}
138123
}
139124

140-
time_ref->tv_host = ktime_to_timeval(ktime_get());
125+
time_ref->tv_host = ktime_get();
141126
time_ref->tick_count++;
142127
}
143128

@@ -146,13 +131,12 @@ void peak_usb_set_ts_now(struct peak_time_ref *time_ref, u32 ts_now)
146131
}
147132

148133
/*
149-
* compute timeval according to current ts and time_ref data
134+
* compute time according to current ts and time_ref data
150135
*/
151136
void peak_usb_get_ts_time(struct peak_time_ref *time_ref, u32 ts, ktime_t *time)
152137
{
153-
/* protect from getting timeval before setting now */
154-
if (time_ref->tv_host.tv_sec > 0) {
155-
struct timeval tv;
138+
/* protect from getting time before setting now */
139+
if (ktime_to_ns(time_ref->tv_host)) {
156140
u64 delta_us;
157141

158142
delta_us = ts - time_ref->ts_dev_2;
@@ -164,9 +148,7 @@ void peak_usb_get_ts_time(struct peak_time_ref *time_ref, u32 ts, ktime_t *time)
164148
delta_us *= time_ref->adapter->us_per_ts_scale;
165149
delta_us >>= time_ref->adapter->us_per_ts_shift;
166150

167-
tv = time_ref->tv_host_0;
168-
peak_usb_add_us(&tv, (u32)delta_us);
169-
*time = timeval_to_ktime(tv);
151+
*time = ktime_add_us(time_ref->tv_host_0, delta_us);
170152
} else {
171153
*time = ktime_get();
172154
}

drivers/net/can/usb/peak_usb/pcan_usb_core.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ extern const struct peak_usb_adapter pcan_usb_pro_fd;
9696
extern const struct peak_usb_adapter pcan_usb_x6;
9797

9898
struct peak_time_ref {
99-
struct timeval tv_host_0, tv_host;
99+
ktime_t tv_host_0, tv_host;
100100
u32 ts_dev_1, ts_dev_2;
101101
u64 ts_total;
102102
u32 tick_count;

0 commit comments

Comments
 (0)