Staging: comedi: replace timeval with ktime_t

'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>
This commit is contained in:
Somya Anand 2014-10-23 21:28:37 +02:00 committed by Greg Kroah-Hartman
parent b0ef3ed48e
commit dd28153b2a
1 changed files with 8 additions and 8 deletions

View File

@ -52,13 +52,14 @@ zero volts).
#include "comedi_fc.h" #include "comedi_fc.h"
#include <linux/timer.h> #include <linux/timer.h>
#include <linux/ktime.h>
#define N_CHANS 8 #define N_CHANS 8
/* Data unique to this driver */ /* Data unique to this driver */
struct waveform_private { struct waveform_private {
struct timer_list timer; 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 int uvolt_amplitude; /* waveform amplitude in microvolts */
unsigned long usec_period; /* waveform period in microseconds */ unsigned long usec_period; /* waveform period in microseconds */
unsigned long usec_current; /* current time (mod waveform period) */ 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 */ /* all times in microsec */
unsigned long elapsed_time; unsigned long elapsed_time;
unsigned int num_scans; unsigned int num_scans;
struct timeval now; ktime_t now;
bool stopping = false; bool stopping = false;
do_gettimeofday(&now); now = ktime_get();
elapsed_time = elapsed_time = ktime_to_us(ktime_sub(now, devpriv->last));
1000000 * (now.tv_sec - devpriv->last.tv_sec) + now.tv_usec -
devpriv->last.tv_usec;
devpriv->last = now; devpriv->last = now;
num_scans = num_scans =
(devpriv->usec_remainder + elapsed_time) / devpriv->scan_period; (devpriv->usec_remainder + elapsed_time) / devpriv->scan_period;
@ -317,8 +316,9 @@ static int waveform_ai_cmd(struct comedi_device *dev,
else /* TRIG_TIMER */ else /* TRIG_TIMER */
devpriv->convert_period = cmd->convert_arg / nano_per_micro; devpriv->convert_period = cmd->convert_arg / nano_per_micro;
do_gettimeofday(&devpriv->last); devpriv->last = ktime_get();
devpriv->usec_current = devpriv->last.tv_usec % devpriv->usec_period; devpriv->usec_current =
((u32)ktime_to_us(devpriv->last)) % devpriv->usec_period;
devpriv->usec_remainder = 0; devpriv->usec_remainder = 0;
devpriv->timer.expires = jiffies + 1; devpriv->timer.expires = jiffies + 1;