LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
* [PATCH v3] Input: analog: Always use ktime functions
@ 2021-09-07 12:37 Guenter Roeck
  2021-09-07 12:41 ` Geert Uytterhoeven
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Guenter Roeck @ 2021-09-07 12:37 UTC (permalink / raw)
  To: Dmitry Torokhov
  Cc: Geert Uytterhoeven, linux-input, linux-kernel, Guenter Roeck

m68k, mips, s390, and sparc allmodconfig images fail to build with the
following error.

drivers/input/joystick/analog.c:160:2: error:
	#warning Precise timer not defined for this architecture.

Remove architecture specific time handling code and always use ktime
functions to determine time deltas. Also remove the now useless use_ktime
kernel parameter.

Signed-off-by: Guenter Roeck <linux@roeck-us.net>
---
v3: Avoid 64-bit divide operation
v2: Drop helper functions and use ktime_get() and ktime_sub() directly
    Drop 'speed' variable and use NSEC_PER_MSEC directly

 drivers/input/joystick/analog.c | 103 ++++----------------------------
 1 file changed, 11 insertions(+), 92 deletions(-)

diff --git a/drivers/input/joystick/analog.c b/drivers/input/joystick/analog.c
index f798922a4598..824e77da67bb 100644
--- a/drivers/input/joystick/analog.c
+++ b/drivers/input/joystick/analog.c
@@ -28,10 +28,6 @@ MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
 MODULE_DESCRIPTION(DRIVER_DESC);
 MODULE_LICENSE("GPL");
 
-static bool use_ktime = true;
-module_param(use_ktime, bool, 0400);
-MODULE_PARM_DESC(use_ktime, "Use ktime for measuring I/O speed");
-
 /*
  * Option parsing.
  */
@@ -110,7 +106,6 @@ struct analog_port {
 	char cooked;
 	int bads;
 	int reads;
-	int speed;
 	int loop;
 	int fuzz;
 	int axes[4];
@@ -119,66 +114,6 @@ struct analog_port {
 	int axtime;
 };
 
-/*
- * Time macros.
- */
-
-#ifdef __i386__
-
-#include <linux/i8253.h>
-
-#define GET_TIME(x)	do { if (boot_cpu_has(X86_FEATURE_TSC)) x = (unsigned int)rdtsc(); else x = get_time_pit(); } while (0)
-#define DELTA(x,y)	(boot_cpu_has(X86_FEATURE_TSC) ? ((y) - (x)) : ((x) - (y) + ((x) < (y) ? PIT_TICK_RATE / HZ : 0)))
-#define TIME_NAME	(boot_cpu_has(X86_FEATURE_TSC)?"TSC":"PIT")
-static unsigned int get_time_pit(void)
-{
-        unsigned long flags;
-        unsigned int count;
-
-        raw_spin_lock_irqsave(&i8253_lock, flags);
-        outb_p(0x00, 0x43);
-        count = inb_p(0x40);
-        count |= inb_p(0x40) << 8;
-        raw_spin_unlock_irqrestore(&i8253_lock, flags);
-
-        return count;
-}
-#elif defined(__x86_64__)
-#define GET_TIME(x)	do { x = (unsigned int)rdtsc(); } while (0)
-#define DELTA(x,y)	((y)-(x))
-#define TIME_NAME	"TSC"
-#elif defined(__alpha__) || defined(CONFIG_ARM) || defined(CONFIG_ARM64) || defined(CONFIG_PPC) || defined(CONFIG_RISCV)
-#define GET_TIME(x)	do { x = get_cycles(); } while (0)
-#define DELTA(x,y)	((y)-(x))
-#define TIME_NAME	"get_cycles"
-#else
-#define FAKE_TIME
-static unsigned long analog_faketime = 0;
-#define GET_TIME(x)     do { x = analog_faketime++; } while(0)
-#define DELTA(x,y)	((y)-(x))
-#define TIME_NAME	"Unreliable"
-#warning Precise timer not defined for this architecture.
-#endif
-
-static inline u64 get_time(void)
-{
-	if (use_ktime) {
-		return ktime_get_ns();
-	} else {
-		unsigned int x;
-		GET_TIME(x);
-		return x;
-	}
-}
-
-static inline unsigned int delta(u64 x, u64 y)
-{
-	if (use_ktime)
-		return y - x;
-	else
-		return DELTA((unsigned int)x, (unsigned int)y);
-}
-
 /*
  * analog_decode() decodes analog joystick data and reports input events.
  */
@@ -241,11 +176,11 @@ static int analog_cooked_read(struct analog_port *port)
 	int i, j;
 
 	loopout = (ANALOG_LOOP_TIME * port->loop) / 1000;
-	timeout = ANALOG_MAX_TIME * port->speed;
+	timeout = ANALOG_MAX_TIME * NSEC_PER_MSEC;
 
 	local_irq_save(flags);
 	gameport_trigger(gameport);
-	now = get_time();
+	now = ktime_get();
 	local_irq_restore(flags);
 
 	start = now;
@@ -258,16 +193,16 @@ static int analog_cooked_read(struct analog_port *port)
 
 		local_irq_disable();
 		this = gameport_read(gameport) & port->mask;
-		now = get_time();
+		now = ktime_get();
 		local_irq_restore(flags);
 
-		if ((last ^ this) && (delta(loop, now) < loopout)) {
+		if ((last ^ this) && (ktime_sub(now, loop) < loopout)) {
 			data[i] = last ^ this;
 			time[i] = now;
 			i++;
 		}
 
-	} while (this && (i < 4) && (delta(start, now) < timeout));
+	} while (this && (i < 4) && (ktime_sub(now, start) < timeout));
 
 	this <<= 4;
 
@@ -275,7 +210,7 @@ static int analog_cooked_read(struct analog_port *port)
 		this |= data[i];
 		for (j = 0; j < 4; j++)
 			if (data[i] & (1 << j))
-				port->axes[j] = (delta(start, time[i]) << ANALOG_FUZZ_BITS) / port->loop;
+				port->axes[j] = ((u32)ktime_sub(time[i], start) << ANALOG_FUZZ_BITS) / port->loop;
 	}
 
 	return -(this != port->mask);
@@ -378,35 +313,19 @@ static void analog_calibrate_timer(struct analog_port *port)
 	u64 t1, t2, t3;
 	unsigned long flags;
 
-	if (use_ktime) {
-		port->speed = 1000000;
-	} else {
-		local_irq_save(flags);
-		t1 = get_time();
-#ifdef FAKE_TIME
-		analog_faketime += 830;
-#endif
-		mdelay(1);
-		t2 = get_time();
-		t3 = get_time();
-		local_irq_restore(flags);
-
-		port->speed = delta(t1, t2) - delta(t2, t3);
-	}
-
 	tx = ~0;
 
 	for (i = 0; i < 50; i++) {
 		local_irq_save(flags);
-		t1 = get_time();
+		t1 = ktime_get();
 		for (t = 0; t < 50; t++) {
 			gameport_read(gameport);
-			t2 = get_time();
+			t2 = ktime_get();
 		}
-		t3 = get_time();
+		t3 = ktime_get();
 		local_irq_restore(flags);
 		udelay(i);
-		t = delta(t1, t2) - delta(t2, t3);
+		t = ktime_sub(t2, t1) - ktime_sub(t3, t2);
 		if (t < tx) tx = t;
 	}
 
@@ -611,7 +530,7 @@ static int analog_init_port(struct gameport *gameport, struct gameport_driver *d
 		t = gameport_read(gameport);
 		msleep(ANALOG_MAX_TIME);
 		port->mask = (gameport_read(gameport) ^ t) & t & 0xf;
-		port->fuzz = (port->speed * ANALOG_FUZZ_MAGIC) / port->loop / 1000 + ANALOG_FUZZ_BITS;
+		port->fuzz = (NSEC_PER_MSEC * ANALOG_FUZZ_MAGIC) / port->loop / 1000 + ANALOG_FUZZ_BITS;
 
 		for (i = 0; i < ANALOG_INIT_RETRIES; i++) {
 			if (!analog_cooked_read(port))
-- 
2.33.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v3] Input: analog: Always use ktime functions
  2021-09-07 12:37 [PATCH v3] Input: analog: Always use ktime functions Guenter Roeck
@ 2021-09-07 12:41 ` Geert Uytterhoeven
  2021-09-08  0:52 ` Randy Dunlap
  2021-09-08  1:43 ` Dmitry Torokhov
  2 siblings, 0 replies; 6+ messages in thread
From: Geert Uytterhoeven @ 2021-09-07 12:41 UTC (permalink / raw)
  To: Guenter Roeck; +Cc: Dmitry Torokhov, linux-input, Linux Kernel Mailing List

On Tue, Sep 7, 2021 at 2:37 PM Guenter Roeck <linux@roeck-us.net> wrote:
> m68k, mips, s390, and sparc allmodconfig images fail to build with the
> following error.
>
> drivers/input/joystick/analog.c:160:2: error:
>         #warning Precise timer not defined for this architecture.
>
> Remove architecture specific time handling code and always use ktime
> functions to determine time deltas. Also remove the now useless use_ktime
> kernel parameter.
>
> Signed-off-by: Guenter Roeck <linux@roeck-us.net>

Reviewed-by: Geert Uytterhoeven <geert+renesas@glider.be>

Gr{oetje,eeting}s,

                        Geert

-- 
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v3] Input: analog: Always use ktime functions
  2021-09-07 12:37 [PATCH v3] Input: analog: Always use ktime functions Guenter Roeck
  2021-09-07 12:41 ` Geert Uytterhoeven
@ 2021-09-08  0:52 ` Randy Dunlap
  2021-09-08  1:43 ` Dmitry Torokhov
  2 siblings, 0 replies; 6+ messages in thread
From: Randy Dunlap @ 2021-09-08  0:52 UTC (permalink / raw)
  To: Guenter Roeck, Dmitry Torokhov
  Cc: Geert Uytterhoeven, linux-input, linux-kernel

On 9/7/21 5:37 AM, Guenter Roeck wrote:
> m68k, mips, s390, and sparc allmodconfig images fail to build with the
> following error.
> 
> drivers/input/joystick/analog.c:160:2: error:
> 	#warning Precise timer not defined for this architecture.
> 
> Remove architecture specific time handling code and always use ktime
> functions to determine time deltas. Also remove the now useless use_ktime
> kernel parameter.
> 
> Signed-off-by: Guenter Roeck <linux@roeck-us.net>
> ---
> v3: Avoid 64-bit divide operation
> v2: Drop helper functions and use ktime_get() and ktime_sub() directly
>      Drop 'speed' variable and use NSEC_PER_MSEC directly
> 
>   drivers/input/joystick/analog.c | 103 ++++----------------------------
>   1 file changed, 11 insertions(+), 92 deletions(-)

Acked-by: Randy Dunlap <rdunlap@infradead.org> # build-tested

thanks.
-- 
~Randy

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v3] Input: analog: Always use ktime functions
  2021-09-07 12:37 [PATCH v3] Input: analog: Always use ktime functions Guenter Roeck
  2021-09-07 12:41 ` Geert Uytterhoeven
  2021-09-08  0:52 ` Randy Dunlap
@ 2021-09-08  1:43 ` Dmitry Torokhov
  2021-09-08  2:29   ` Guenter Roeck
  2 siblings, 1 reply; 6+ messages in thread
From: Dmitry Torokhov @ 2021-09-08  1:43 UTC (permalink / raw)
  To: Guenter Roeck; +Cc: Geert Uytterhoeven, linux-input, linux-kernel

Hi Guenter,

On Tue, Sep 07, 2021 at 05:37:34AM -0700, Guenter Roeck wrote:
> @@ -241,11 +176,11 @@ static int analog_cooked_read(struct analog_port *port)
>  	int i, j;
>  
>  	loopout = (ANALOG_LOOP_TIME * port->loop) / 1000;
> -	timeout = ANALOG_MAX_TIME * port->speed;
> +	timeout = ANALOG_MAX_TIME * NSEC_PER_MSEC;
>  
>  	local_irq_save(flags);
>  	gameport_trigger(gameport);
> -	now = get_time();
> +	now = ktime_get();

There are time[4], now, etc variables above this block that are u64. I
think they can be make ktime_t. I can do it myself if you agree.

> @@ -378,35 +313,19 @@ static void analog_calibrate_timer(struct analog_port *port)
>  	u64 t1, t2, t3;

I think these should also be ktime_t.

Thanks.

-- 
Dmitry

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v3] Input: analog: Always use ktime functions
  2021-09-08  1:43 ` Dmitry Torokhov
@ 2021-09-08  2:29   ` Guenter Roeck
  2021-09-09  6:40     ` Dmitry Torokhov
  0 siblings, 1 reply; 6+ messages in thread
From: Guenter Roeck @ 2021-09-08  2:29 UTC (permalink / raw)
  To: Dmitry Torokhov; +Cc: Geert Uytterhoeven, linux-input, linux-kernel

On 9/7/21 6:43 PM, Dmitry Torokhov wrote:
> Hi Guenter,
> 
> On Tue, Sep 07, 2021 at 05:37:34AM -0700, Guenter Roeck wrote:
>> @@ -241,11 +176,11 @@ static int analog_cooked_read(struct analog_port *port)
>>   	int i, j;
>>   
>>   	loopout = (ANALOG_LOOP_TIME * port->loop) / 1000;
>> -	timeout = ANALOG_MAX_TIME * port->speed;
>> +	timeout = ANALOG_MAX_TIME * NSEC_PER_MSEC;
>>   
>>   	local_irq_save(flags);
>>   	gameport_trigger(gameport);
>> -	now = get_time();
>> +	now = ktime_get();
> 
> There are time[4], now, etc variables above this block that are u64. I
> think they can be make ktime_t. I can do it myself if you agree.
> 
Sure.

>> @@ -378,35 +313,19 @@ static void analog_calibrate_timer(struct analog_port *port)
>>   	u64 t1, t2, t3;
> 
> I think these should also be ktime_t.
> 
Ok. Do you want me to resend or are you going to make the changes ?

Thanks,
Guenter


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v3] Input: analog: Always use ktime functions
  2021-09-08  2:29   ` Guenter Roeck
@ 2021-09-09  6:40     ` Dmitry Torokhov
  0 siblings, 0 replies; 6+ messages in thread
From: Dmitry Torokhov @ 2021-09-09  6:40 UTC (permalink / raw)
  To: Guenter Roeck; +Cc: Geert Uytterhoeven, linux-input, linux-kernel

On Tue, Sep 07, 2021 at 07:29:36PM -0700, Guenter Roeck wrote:
> On 9/7/21 6:43 PM, Dmitry Torokhov wrote:
> > Hi Guenter,
> > 
> > On Tue, Sep 07, 2021 at 05:37:34AM -0700, Guenter Roeck wrote:
> > > @@ -241,11 +176,11 @@ static int analog_cooked_read(struct analog_port *port)
> > >   	int i, j;
> > >   	loopout = (ANALOG_LOOP_TIME * port->loop) / 1000;
> > > -	timeout = ANALOG_MAX_TIME * port->speed;
> > > +	timeout = ANALOG_MAX_TIME * NSEC_PER_MSEC;
> > >   	local_irq_save(flags);
> > >   	gameport_trigger(gameport);
> > > -	now = get_time();
> > > +	now = ktime_get();
> > 
> > There are time[4], now, etc variables above this block that are u64. I
> > think they can be make ktime_t. I can do it myself if you agree.
> > 
> Sure.
> 
> > > @@ -378,35 +313,19 @@ static void analog_calibrate_timer(struct analog_port *port)
> > >   	u64 t1, t2, t3;
> > 
> > I think these should also be ktime_t.
> > 
> Ok. Do you want me to resend or are you going to make the changes ?

I made the changes and applied, thank you.

-- 
Dmitry

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-09-09  6:52 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-07 12:37 [PATCH v3] Input: analog: Always use ktime functions Guenter Roeck
2021-09-07 12:41 ` Geert Uytterhoeven
2021-09-08  0:52 ` Randy Dunlap
2021-09-08  1:43 ` Dmitry Torokhov
2021-09-08  2:29   ` Guenter Roeck
2021-09-09  6:40     ` Dmitry Torokhov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).