staging: media: lirc: Replace timeval with ktime_t in lirc_serial.c
Subsystems (2)
PATCH 1/3 staging: media: lirc: Replace timeval with ktime_t in lirc_serial.c
'struct timeval tv' is used to get current time. 'static struct timeval lasttv' is used to get last interrupt 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 lirc_serial.c file of media: lirc 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. The check to test time going backwards is also removed. Intialization to static variable is also removed. ktime_to_us() is used to convert ktime_t to microsecond value. deltv is changed to delkt, a ktime_t type varibale from long to assign the ktime_sub value directly. ktime_compare is used to compare delkt with 15 seconds, which is changed to a nanosecond value by using ktime_set().
Build tested it.
---
These three patches were still in my backlog and part of linux-next but never made it into mainline. Please apply to the v4l tree.
drivers/staging/media/lirc/lirc_serial.c +17 -33
@@ -59,7 +59,7 @@#include <linux/ioport.h> #include <linux/kernel.h> #include <linux/serial_reg.h> -#include <linux/time.h> +#include <linux/ktime.h> #include <linux/string.h> #include <linux/types.h> #include <linux/wait.h>
@@ -204,7 +204,7 @@ static struct lirc_serial hardware[] = {#define RBUF_LEN 256 -static struct timeval lasttv = {0 ,0 }; +static ktime_t lastkt;static struct lirc_buffer rbuf;
@@ -542,10 +542,10 @@ static void frbwrite(int l)static irqreturn_t lirc_irq_handler (int i,void *blah) { -struct timeval tv; +ktime_t kt;int counter, dcd;u8 status; -long deltv; +ktime_t delkt;int data;static int last_dcd =-1 ;
@@ -565,7 +565,7 @@ static irqreturn_t lirc_irq_handler(int i, void *blah)if ((status & hardware[type].signal_pin_change) && sense !=-1 ) {/* get current time */ -do_gettimeofday (&tv); + kt =ktime_get ();/* New mode, written by Trent Piepho <[email protected]>. */
@@ -594,34 +594,20 @@ static irqreturn_t lirc_irq_handler(int i, void *blah) dcd = (status & hardware[type].signal_pin) ?1 :0 ;if (dcd == last_dcd) { -pr_warn ("ignoring spike: %d %d %lx %lx %lx %lx\n" , - dcd, sense, - tv.tv_sec, lasttv.tv_sec, - (unsigned long )tv.tv_usec, - (unsigned long )lasttv.tv_usec); +pr_warn ("ignoring spike: %d %d %llx %llx\n" , + dcd, sense,ktime_to_us (kt), +ktime_to_us (lastkt));continue ; } - deltv = tv.tv_sec-lasttv.tv_sec; -if (tv.tv_sec < lasttv.tv_sec || - (tv.tv_sec == lasttv.tv_sec && - tv.tv_usec < lasttv.tv_usec)) { -pr_warn ("AIEEEE: your clock just jumped backwards\n" ); -pr_warn ("%d %d %lx %lx %lx %lx\n" , - dcd, sense, - tv.tv_sec, lasttv.tv_sec, - (unsigned long )tv.tv_usec, - (unsigned long )lasttv.tv_usec); - data =PULSE_MASK ; - }else if (deltv >15 ) { + delkt =ktime_sub (kt, lastkt); +if (ktime_compare (delkt,ktime_set (15 ,0 )) >0 ) { data =PULSE_MASK ;/* really long time */ if (!(dcd^sense)) {/* sanity check */ -pr_warn ("AIEEEE: %d %d %lx %lx %lx %lx\n" , - dcd, sense, - tv.tv_sec, lasttv.tv_sec, - (unsigned long )tv.tv_usec, - (unsigned long )lasttv.tv_usec); +pr_warn ("AIEEEE: %d %d %llx %llx\n" , + dcd, sense,ktime_to_us (kt), +ktime_to_us (lastkt));/* * detecting pulse while this * MUST be a space!
@@ -629,11 +615,9 @@ static irqreturn_t lirc_irq_handler(int i, void *blah) sense = sense ?0 :1 ; } }else - data = (int ) (deltv*1000000 + - tv.tv_usec - - lasttv.tv_usec); + data = (int )ktime_to_us (delkt);frbwrite (dcd^sense ? data : (data|PULSE_BIT )); - lasttv = tv; + lastkt = kt; last_dcd = dcd;wake_up_interruptible (&rbuf.wait_poll); }
@@ -790,7 +774,7 @@ static int set_use_inc(void *data)unsigned long flags;/* initialize timestamp */ -do_gettimeofday (&lasttv); + lastkt =ktime_get ();spin_lock_irqsave (&hardware[type].lock, flags);
@@ -979,7 +963,7 @@ static int lirc_serial_resume(struct platform_device *dev)spin_lock_irqsave (&hardware[type].lock, flags);/* Enable Interrupt */ -do_gettimeofday (&lasttv); + lastkt =ktime_get ();soutp (UART_IER ,sinp (UART_IER )|UART_IER_MSI );off ();
PATCH 2/3 staging: media: lirc: Replace timeval with ktime_t in lirc_sasem.c
'struct timeval presstime' and 'struct timeval tv' is used to calculate the time since the last button press.
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 media: lirc driver to use ktime_t.
ktime_get() is better than using do_gettimeofday(), because it uses the monotonic clock. ktime_sub() are used to subtract two ktime variables. 'ms' is only used to check how much time has passed by comparing to 250. So instead of using expensive ktime_to_ms() call, it has been changed to hold nanoseconds by using ktime_to_ns().
Build tested it. Tested with sparse too.
drivers/staging/media/lirc/lirc_sasem.c +9 -11
@@ -42,6 +42,7 @@#include <linux/slab.h> #include <linux/uaccess.h> #include <linux/usb.h> +#include <linux/ktime.h> #include <media/lirc.h> #include <media/lirc_dev.h>
@@ -111,7 +112,7 @@ struct sasem_context { } tx;/* for dealing with repeat codes (wish there was a toggle bit!) */ -struct timeval presstime; +ktime_t presstime;char lastcode[8 ];int codesaved; };
@@ -566,8 +567,8 @@ static void incoming_packet(struct sasem_context *context, {int len = urb->actual_length;unsigned char *buf = urb->transfer_buffer; -long ms; -struct timeval tv; +u64 ns; +ktime_t kt;if (len !=8 ) { dev_warn(&context->dev->dev,
@@ -584,9 +585,8 @@ static void incoming_packet(struct sasem_context *context,*/ /* get the time since the last button press */ -do_gettimeofday (&tv); - ms = (tv.tv_sec - context->presstime.tv_sec) *1000 + - (tv.tv_usec - context->presstime.tv_usec) /1000 ; + kt =ktime_get (); + ns =ktime_to_ns (ktime_sub (kt, context->presstime));if (memcmp (buf,"\x08\0\0\0\0\0\0\0" ,8 ) ==0 ) {/*
@@ -600,10 +600,9 @@ static void incoming_packet(struct sasem_context *context,* in that time and then get a false repeat of the previous * press but it is long enough for a genuine repeat */ -if ((ms <250 ) && (context->codesaved !=0 )) { +if ((ns <250 *NSEC_PER_MSEC ) && (context->codesaved !=0 )) {memcpy (buf, &context->lastcode,8 ); - context->presstime.tv_sec = tv.tv_sec; - context->presstime.tv_usec = tv.tv_usec; + context->presstime = kt; } }else {/* save the current valid code for repeats */
@@ -613,8 +612,7 @@ static void incoming_packet(struct sasem_context *context,* just for safety reasons */ context->codesaved =1 ; - context->presstime.tv_sec = tv.tv_sec; - context->presstime.tv_usec = tv.tv_usec; + context->presstime = kt; }lirc_buffer_write (context->driver->rbuf, buf);
PATCH 3/3 staging: media: lirc: Replace timeval with ktime_t in lirc_parallel.c
In reply to Arnd Bergmann'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 fucntion 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.
drivers/staging/media/lirc/lirc_parallel.c +15 -20
@@ -33,7 +33,7 @@#include <linux/fs.h> #include <linux/kernel.h> #include <linux/ioport.h> -#include <linux/time.h> +#include <linux/ktime.h> #include <linux/mm.h> #include <linux/delay.h>
@@ -144,25 +144,22 @@ static void lirc_off(void)static unsigned int init_lirc_timer (void ) { -struct timeval tv, now; +ktime_t kt, now, timeout;unsigned int level, newlevel, timeelapsed, newtimer;int count =0 ; -do_gettimeofday (&tv); - tv.tv_sec++;/* wait max. 1 sec. */ + kt =ktime_get (); +/* wait max. 1 sec. */ + timeout =ktime_add_ns (kt,NSEC_PER_SEC ); level =lirc_get_timer ();do { newlevel =lirc_get_timer ();if (level ==0 && newlevel !=0 ) count++; level = newlevel; -do_gettimeofday (&now); - }while (count <1000 && (now.tv_sec < tv.tv_sec - || (now.tv_sec == tv.tv_sec - && now.tv_usec < tv.tv_usec))); - - timeelapsed = (now.tv_sec +1 - tv.tv_sec)*1000000 - + (now.tv_usec - tv.tv_usec); + now =ktime_get (); + }while (count <1000 && (ktime_before (now, timeout))); + timeelapsed =ktime_us_delta (now, kt);if (count >=1000 && timeelapsed >0 ) {if (default_timer ==0 ) {/* autodetect timer */
@@ -220,8 +217,8 @@ static void rbuf_write(int signal)static void lirc_lirc_irq_handler (void *blah) { -struct timeval tv; -static struct timeval lasttv; +ktime_t kt, delkt; +static ktime_t lastkt;static int init;long signal;int data;
@@ -244,16 +241,14 @@ static void lirc_lirc_irq_handler(void *blah)#ifdef LIRC_TIMER if (init) { -do_gettimeofday (&tv); + kt =ktime_get (); - signal = tv.tv_sec - lasttv.tv_sec; -if (signal >15 ) + delkt =ktime_sub (kt, lastkt); +if (ktime_compare (delkt,ktime_set (15 ,0 )) >0 )/* really long time */ data =PULSE_MASK ;else - data = (int ) (signal*1000000 + - tv.tv_usec - lasttv.tv_usec + -LIRC_SFH506_DELAY ); + data = (int ) (ktime_to_us (delkt) +LIRC_SFH506_DELAY );rbuf_write (data);/* space */ }else {
@@ -301,7 +296,7 @@ static void lirc_lirc_irq_handler(void *blah) data =1 ; rbuf_write(PULSE_BIT|data); /* pulse */ } -do_gettimeofday (&lasttv); + lastkt =ktime_get ();#else /* add your code here */ #endif