[PATCH] Staging: media: lirc: Replace timeval with ktime_t
'struct timeval last_tv' is used to get the time of last signal change and 'struct timeval last_intr_tv' is used to get the time of last UART interrupt. 32-bit systems using 'struct timeval' will break in the year 2038, so we have to replace that code with more appropriate types. Here struct timeval is replaced with ktime_t. Signed-off-by: Ksenija Stanojevic ksenija.stanoje...@gmail.com --- drivers/staging/media/lirc/lirc_sir.c | 75 ++- 1 file changed, 30 insertions(+), 45 deletions(-) diff --git a/drivers/staging/media/lirc/lirc_sir.c b/drivers/staging/media/lirc/lirc_sir.c index 29087f6..4f326e9 100644 --- a/drivers/staging/media/lirc/lirc_sir.c +++ b/drivers/staging/media/lirc/lirc_sir.c @@ -44,7 +44,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 @@ -127,9 +127,9 @@ static int threshold = 3; static DEFINE_SPINLOCK(timer_lock); static struct timer_list timerlist; /* time of last signal change detected */ -static struct timeval last_tv = {0, 0}; +static ktime_t last; /* time of last UART data ready interrupt */ -static struct timeval last_intr_tv = {0, 0}; +static ktime_t last_intr_time; static int last_value; static DECLARE_WAIT_QUEUE_HEAD(lirc_read_queue); @@ -400,20 +400,6 @@ static void drop_chrdev(void) } /* SECTION: Hardware */ -static long delta(struct timeval *tv1, struct timeval *tv2) -{ - unsigned long deltv; - - deltv = tv2-tv_sec - tv1-tv_sec; - if (deltv 15) - deltv = 0xFF; - else - deltv = deltv*100 + - tv2-tv_usec - - tv1-tv_usec; - return deltv; -} - static void sir_timeout(unsigned long data) { /* @@ -432,12 +418,14 @@ static void sir_timeout(unsigned long data) /* clear unread bits in UART and restart */ outb(UART_FCR_CLEAR_RCVR, io + UART_FCR); /* determine 'virtual' pulse end: */ - pulse_end = delta(last_tv, last_intr_tv); + pulse_end = min_t(unsigned long, + ktime_us_delta(last, last_intr_time), + PULSE_MASK); dev_dbg(driver.dev, timeout add %d for %lu usec\n, last_value, pulse_end); add_read_queue(last_value, pulse_end); last_value = 0; - last_tv = last_intr_tv; + last = last_intr_time; } spin_unlock_irqrestore(timer_lock, flags); } @@ -445,9 +433,9 @@ static void sir_timeout(unsigned long data) static irqreturn_t sir_interrupt(int irq, void *dev_id) { unsigned char data; - struct timeval curr_tv; - static unsigned long deltv; - unsigned long deltintrtv; + ktime_t curr_time; + static unsigned long delt; + unsigned long deltintr; unsigned long flags; int iir, lsr; @@ -471,49 +459,46 @@ static irqreturn_t sir_interrupt(int irq, void *dev_id) do { del_timer(timerlist); data = inb(io + UART_RX); - do_gettimeofday(curr_tv); - deltv = delta(last_tv, curr_tv); - deltintrtv = delta(last_intr_tv, curr_tv); + curr_time = ktime_get(); + delt = min_t(unsigned long, +ktime_us_delta(last, curr_time), +PULSE_MASK); + deltintr = min_t(unsigned long, +ktime_us_delta(last_intr_time, + curr_time), +PULSE_MASK); dev_dbg(driver.dev, t %lu, d %d\n, - deltintrtv, (int)data); + deltintr, (int)data); /* * if nothing came in last X cycles, * it was gap */ - if (deltintrtv TIME_CONST * threshold) { + if (deltintr TIME_CONST * threshold) { if (last_value) { dev_dbg(driver.dev, GAP\n); /* simulate signal change */ add_read_queue(last_value, - deltv
[PATCH v4] Staging: media: lirc: Replace timeval with ktime_t
'struct timeval last_tv' is used to get the time of last signal change and 'struct timeval last_intr_tv' is used to get the time of last UART interrupt. 32-bit systems using 'struct timeval' will break in the year 2038, so we have to replace that code with more appropriate types. Here struct timeval is replaced with ktime_t. Signed-off-by: Ksenija Stanojevic ksenija.stanoje...@gmail.com --- Changes in v4: - add lirc in subject line. - remove delta function. - change variable names. Changes in v3: - delta function is changed to inline function. - change variable names. Changes in v2: - change subject line drivers/staging/media/lirc/lirc_sir.c | 75 ++- 1 file changed, 30 insertions(+), 45 deletions(-) diff --git a/drivers/staging/media/lirc/lirc_sir.c b/drivers/staging/media/lirc/lirc_sir.c index 29087f6..4f326e9 100644 --- a/drivers/staging/media/lirc/lirc_sir.c +++ b/drivers/staging/media/lirc/lirc_sir.c @@ -44,7 +44,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 @@ -127,9 +127,9 @@ static int threshold = 3; static DEFINE_SPINLOCK(timer_lock); static struct timer_list timerlist; /* time of last signal change detected */ -static struct timeval last_tv = {0, 0}; +static ktime_t last; /* time of last UART data ready interrupt */ -static struct timeval last_intr_tv = {0, 0}; +static ktime_t last_intr_time; static int last_value; static DECLARE_WAIT_QUEUE_HEAD(lirc_read_queue); @@ -400,20 +400,6 @@ static void drop_chrdev(void) } /* SECTION: Hardware */ -static long delta(struct timeval *tv1, struct timeval *tv2) -{ - unsigned long deltv; - - deltv = tv2-tv_sec - tv1-tv_sec; - if (deltv 15) - deltv = 0xFF; - else - deltv = deltv*100 + - tv2-tv_usec - - tv1-tv_usec; - return deltv; -} - static void sir_timeout(unsigned long data) { /* @@ -432,12 +418,14 @@ static void sir_timeout(unsigned long data) /* clear unread bits in UART and restart */ outb(UART_FCR_CLEAR_RCVR, io + UART_FCR); /* determine 'virtual' pulse end: */ - pulse_end = delta(last_tv, last_intr_tv); + pulse_end = min_t(unsigned long, + ktime_us_delta(last, last_intr_time), + PULSE_MASK); dev_dbg(driver.dev, timeout add %d for %lu usec\n, last_value, pulse_end); add_read_queue(last_value, pulse_end); last_value = 0; - last_tv = last_intr_tv; + last = last_intr_time; } spin_unlock_irqrestore(timer_lock, flags); } @@ -445,9 +433,9 @@ static void sir_timeout(unsigned long data) static irqreturn_t sir_interrupt(int irq, void *dev_id) { unsigned char data; - struct timeval curr_tv; - static unsigned long deltv; - unsigned long deltintrtv; + ktime_t curr_time; + static unsigned long delt; + unsigned long deltintr; unsigned long flags; int iir, lsr; @@ -471,49 +459,46 @@ static irqreturn_t sir_interrupt(int irq, void *dev_id) do { del_timer(timerlist); data = inb(io + UART_RX); - do_gettimeofday(curr_tv); - deltv = delta(last_tv, curr_tv); - deltintrtv = delta(last_intr_tv, curr_tv); + curr_time = ktime_get(); + delt = min_t(unsigned long, +ktime_us_delta(last, curr_time), +PULSE_MASK); + deltintr = min_t(unsigned long, +ktime_us_delta(last_intr_time, + curr_time), +PULSE_MASK); dev_dbg(driver.dev, t %lu, d %d\n, - deltintrtv, (int)data); + deltintr, (int)data); /* * if nothing came in last X cycles, * it was gap */ - if (deltintrtv TIME_CONST * threshold) { + if (deltintr TIME_CONST * threshold) { if (last_value) { dev_dbg(driver.dev, GAP\n
[PATCH v3] Staging: media: Replace timeval with ktime_t
'struct timeval last_tv' is used to get the time of last signal change and 'struct timeval last_intr_tv' is used to get the time of last UART interrupt. 32-bit systems using 'struct timeval' will break in the year 2038, so we have to replace that code with more appropriate types. Here struct timeval is replaced with ktime_t. Signed-off-by: Ksenija Stanojevic ksenija.stanoje...@gmail.com --- Changes in v3: - as John suggested delta function is changed to inline function, checkpatch signals a warning to change min to min_t. Is it a false positive? - change variable names. Changes in v2: - change subject line drivers/staging/media/lirc/lirc_sir.c | 51 +-- 1 file changed, 18 insertions(+), 33 deletions(-) diff --git a/drivers/staging/media/lirc/lirc_sir.c b/drivers/staging/media/lirc/lirc_sir.c index 29087f6..c98c486 100644 --- a/drivers/staging/media/lirc/lirc_sir.c +++ b/drivers/staging/media/lirc/lirc_sir.c @@ -44,7 +44,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 @@ -127,9 +127,9 @@ static int threshold = 3; static DEFINE_SPINLOCK(timer_lock); static struct timer_list timerlist; /* time of last signal change detected */ -static struct timeval last_tv = {0, 0}; +static ktime_t last; /* time of last UART data ready interrupt */ -static struct timeval last_intr_tv = {0, 0}; +static ktime_t last_intr_time; static int last_value; static DECLARE_WAIT_QUEUE_HEAD(lirc_read_queue); @@ -400,18 +400,11 @@ static void drop_chrdev(void) } /* SECTION: Hardware */ -static long delta(struct timeval *tv1, struct timeval *tv2) +static inline long delta(ktime_t t1, ktime_t t2) { - unsigned long deltv; - - deltv = tv2-tv_sec - tv1-tv_sec; - if (deltv 15) - deltv = 0xFF; - else - deltv = deltv*100 + - tv2-tv_usec - - tv1-tv_usec; - return deltv; + /* return the delta in 32bit usecs, but cap to UINTMAX in case the +* delta is greater then 32bits */ + return (long) min((unsigned int) ktime_us_delta(t1, t2), UINT_MAX); } static void sir_timeout(unsigned long data) @@ -432,12 +425,12 @@ static void sir_timeout(unsigned long data) /* clear unread bits in UART and restart */ outb(UART_FCR_CLEAR_RCVR, io + UART_FCR); /* determine 'virtual' pulse end: */ - pulse_end = delta(last_tv, last_intr_tv); + pulse_end = delta(last, last_intr_time); dev_dbg(driver.dev, timeout add %d for %lu usec\n, last_value, pulse_end); add_read_queue(last_value, pulse_end); last_value = 0; - last_tv = last_intr_tv; + last = last_intr_time; } spin_unlock_irqrestore(timer_lock, flags); } @@ -445,7 +438,7 @@ static void sir_timeout(unsigned long data) static irqreturn_t sir_interrupt(int irq, void *dev_id) { unsigned char data; - struct timeval curr_tv; + ktime_t curr_time; static unsigned long deltv; unsigned long deltintrtv; unsigned long flags; @@ -471,9 +464,9 @@ static irqreturn_t sir_interrupt(int irq, void *dev_id) do { del_timer(timerlist); data = inb(io + UART_RX); - do_gettimeofday(curr_tv); - deltv = delta(last_tv, curr_tv); - deltintrtv = delta(last_intr_tv, curr_tv); + curr_time = ktime_get(); + deltv = delta(last, curr_time); + deltintrtv = delta(last_intr_time, curr_time); dev_dbg(driver.dev, t %lu, d %d\n, deltintrtv, (int)data); /* @@ -488,10 +481,7 @@ static irqreturn_t sir_interrupt(int irq, void *dev_id) deltv - deltintrtv); last_value = 0; - last_tv.tv_sec = - last_intr_tv.tv_sec; - last_tv.tv_usec = - last_intr_tv.tv_usec; + last = last_intr_time; deltv = deltintrtv; } } @@ -504,16 +494,11 @@ static