Commit 0dbf41a3 authored by Arnd Bergmann's avatar Arnd Bergmann Committed by Mauro Carvalho Chehab

[media] staging: media: lirc: Replace timeval with ktime_t in lirc_parallel.c

'struct timeval tv' and 'struct timeval now' is used to calculate the
elapsed time. 'LIRC_SFH506_DELAY' is a delay t_phl in usecs.

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 lirc_parallel.c file of  media: lirc driver
to use ktime_t.

ktime_get() is  better than using do_gettimeofday(),
because it uses the monotonic clock. ktime_sub is used
to subtract two ktime variables. ktime_to_us() is used to
convert ktime to microsecond.

New ktime_t variable timeout, is added in lirc_off(),to improve
clarity. Introduced a new ktime_t variable in lirc_lirc_irq_handler()
function, to avoid the use of signal variable for storing
seconds in the first part of this function as later it uses
a time unit that is defined by the global "timer" variable.
This makes it more clear.

ktime_set() is used to set a value in seconds to a value in
nanosecond so that ktime_compare() can be used appropriately.
ktime_compare() is used to compare two ktime values.
ktime_add_ns() is used to increment a ktime value by 1 sec.

One comment is also shifted a line up, as it was creating a 80
character warning.

Build tested it. Also tested it with sparse.
Signed-off-by: default avatarTapasweni Pathak <tapaswenipathak@gmail.com>
Signed-off-by: default avatarArnd Bergmann <arnd@arndb.de>
Signed-off-by: default avatarMauro Carvalho Chehab <mchehab@osg.samsung.com>
parent f07c73fe
...@@ -33,7 +33,7 @@ ...@@ -33,7 +33,7 @@
#include <linux/fs.h> #include <linux/fs.h>
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/ioport.h> #include <linux/ioport.h>
#include <linux/time.h> #include <linux/ktime.h>
#include <linux/mm.h> #include <linux/mm.h>
#include <linux/delay.h> #include <linux/delay.h>
...@@ -144,25 +144,22 @@ static void lirc_off(void) ...@@ -144,25 +144,22 @@ static void lirc_off(void)
static unsigned int init_lirc_timer(void) static unsigned int init_lirc_timer(void)
{ {
struct timeval tv, now; ktime_t kt, now, timeout;
unsigned int level, newlevel, timeelapsed, newtimer; unsigned int level, newlevel, timeelapsed, newtimer;
int count = 0; int count = 0;
do_gettimeofday(&tv); kt = ktime_get();
tv.tv_sec++; /* wait max. 1 sec. */ /* wait max. 1 sec. */
timeout = ktime_add_ns(kt, NSEC_PER_SEC);
level = lirc_get_timer(); level = lirc_get_timer();
do { do {
newlevel = lirc_get_timer(); newlevel = lirc_get_timer();
if (level == 0 && newlevel != 0) if (level == 0 && newlevel != 0)
count++; count++;
level = newlevel; level = newlevel;
do_gettimeofday(&now); now = ktime_get();
} while (count < 1000 && (now.tv_sec < tv.tv_sec } while (count < 1000 && (ktime_before(now, timeout)));
|| (now.tv_sec == tv.tv_sec timeelapsed = ktime_us_delta(now, kt);
&& now.tv_usec < tv.tv_usec)));
timeelapsed = (now.tv_sec + 1 - tv.tv_sec)*1000000
+ (now.tv_usec - tv.tv_usec);
if (count >= 1000 && timeelapsed > 0) { if (count >= 1000 && timeelapsed > 0) {
if (default_timer == 0) { if (default_timer == 0) {
/* autodetect timer */ /* autodetect timer */
...@@ -220,8 +217,8 @@ static void rbuf_write(int signal) ...@@ -220,8 +217,8 @@ static void rbuf_write(int signal)
static void lirc_lirc_irq_handler(void *blah) static void lirc_lirc_irq_handler(void *blah)
{ {
struct timeval tv; ktime_t kt, delkt;
static struct timeval lasttv; static ktime_t lastkt;
static int init; static int init;
long signal; long signal;
int data; int data;
...@@ -244,16 +241,14 @@ static void lirc_lirc_irq_handler(void *blah) ...@@ -244,16 +241,14 @@ static void lirc_lirc_irq_handler(void *blah)
#ifdef LIRC_TIMER #ifdef LIRC_TIMER
if (init) { if (init) {
do_gettimeofday(&tv); kt = ktime_get();
signal = tv.tv_sec - lasttv.tv_sec; delkt = ktime_sub(kt, lastkt);
if (signal > 15) if (ktime_compare(delkt, ktime_set(15, 0)) > 0)
/* really long time */ /* really long time */
data = PULSE_MASK; data = PULSE_MASK;
else else
data = (int) (signal*1000000 + data = (int)(ktime_to_us(delkt) + LIRC_SFH506_DELAY);
tv.tv_usec - lasttv.tv_usec +
LIRC_SFH506_DELAY);
rbuf_write(data); /* space */ rbuf_write(data); /* space */
} else { } else {
...@@ -301,7 +296,7 @@ static void lirc_lirc_irq_handler(void *blah) ...@@ -301,7 +296,7 @@ static void lirc_lirc_irq_handler(void *blah)
data = 1; data = 1;
rbuf_write(PULSE_BIT|data); /* pulse */ rbuf_write(PULSE_BIT|data); /* pulse */
} }
do_gettimeofday(&lasttv); lastkt = ktime_get();
#else #else
/* add your code here */ /* add your code here */
#endif #endif
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment