Staging: comedi: replace timeval with ktime_t
authorSomya Anand <somyaanand214@gmail.com>
Thu, 23 Oct 2014 19:28:37 +0000 (21:28 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Mon, 27 Oct 2014 02:33:05 +0000 (10:33 +0800)
'struct timeval last' is used for recording last time interrupt.
'struct timeval now' is used for calculating elapsed time.

32-bit systems using 'struct timeval' will break in the year 2038,
so we have to replace that code with more appropriate types.
This patch changes the comedi driver to use ktime_t.

Since this code doesn't communicate the time values
to the outside (user space, file system, network).Thus ktime_get()
is a better than using do_gettimeofday() as it uses monotonic
clock.

ktime_to_us() returns an 's64', and using the '%' operator on that requires
doing a 64-bit division which needs an expensive library function call
the specific value of usec_current does not actually matter although it might
matter that it's not always the same
which will start with the offset from the lower 32 bit of the microsecond.
Therefore:
  devpriv->usec_current = (ktime_to_us(devpriv->last) % USEC_PER_SEC)
                          % devpriv->usec_period;
is replaced by

  devpriv->usec_current = ((u32)ktime_to_us(devpriv->last))
                          % devpriv->usec_period;

Signed-off-by: Somya Anand <somyaanand214@gmail.com>
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/staging/comedi/drivers/comedi_test.c

index 8845075cd3ccb74ae5c3b44329b088ec712184eb..4d4358ce171078c70234b7229cdbeae84fae6ebd 100644 (file)
@@ -52,13 +52,14 @@ zero volts).
 
 #include "comedi_fc.h"
 #include <linux/timer.h>
+#include <linux/ktime.h>
 
 #define N_CHANS 8
 
 /* Data unique to this driver */
 struct waveform_private {
        struct timer_list timer;
-       struct timeval last;            /* time last timer interrupt occurred */
+       ktime_t last;   /* time last timer interrupt occurred */
        unsigned int uvolt_amplitude;   /* waveform amplitude in microvolts */
        unsigned long usec_period;      /* waveform period in microseconds */
        unsigned long usec_current;     /* current time (mod waveform period) */
@@ -171,14 +172,12 @@ static void waveform_ai_interrupt(unsigned long arg)
        /* all times in microsec */
        unsigned long elapsed_time;
        unsigned int num_scans;
-       struct timeval now;
+       ktime_t now;
        bool stopping = false;
 
-       do_gettimeofday(&now);
+       now = ktime_get();
 
-       elapsed_time =
-           1000000 * (now.tv_sec - devpriv->last.tv_sec) + now.tv_usec -
-           devpriv->last.tv_usec;
+       elapsed_time = ktime_to_us(ktime_sub(now, devpriv->last));
        devpriv->last = now;
        num_scans =
            (devpriv->usec_remainder + elapsed_time) / devpriv->scan_period;
@@ -317,8 +316,9 @@ static int waveform_ai_cmd(struct comedi_device *dev,
        else    /* TRIG_TIMER */
                devpriv->convert_period = cmd->convert_arg / nano_per_micro;
 
-       do_gettimeofday(&devpriv->last);
-       devpriv->usec_current = devpriv->last.tv_usec % devpriv->usec_period;
+       devpriv->last = ktime_get();
+       devpriv->usec_current =
+               ((u32)ktime_to_us(devpriv->last)) % devpriv->usec_period;
        devpriv->usec_remainder = 0;
 
        devpriv->timer.expires = jiffies + 1;
This page took 0.041408 seconds and 5 git commands to generate.