linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Input: joystick: analog - change msleep to usleep_range for small msecs
@ 2016-11-28 19:41 Aniroop Mathur
  2016-11-29  6:51 ` Vojtech Pavlik
  0 siblings, 1 reply; 2+ messages in thread
From: Aniroop Mathur @ 2016-11-28 19:41 UTC (permalink / raw)
  To: vojtech, dmitry.torokhov, linux-input, linux-kernel
  Cc: s.samuel, r.mahale, aniroop.mathur, Aniroop Mathur

msleep(1~20) may not do what the caller intends, and will often sleep longer.
(~20 ms actual sleep for any value given in the 1~20ms range)
This is not the desired behaviour for many cases like device resume time,
device suspend time, device enable time, connection time, probe time,
loops, retry logic, etc
msleep is built on jiffies / legacy timers which are not precise whereas
usleep_range is build on top of hrtimers so the wakeups are precise.
Thus, change msleep to usleep_range for precise wakeups.

For example:
On a machine with tick rate / HZ as 100, msleep(3) will make the process to
sleep for a minimum period of 10 ms whereas usleep_range(3000, 3100) will make
sure that the process does not sleep for more than 3100 us or 3.1ms

Signed-off-by: Aniroop Mathur <a.mathur@samsung.com>
---
 drivers/input/joystick/analog.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/drivers/input/joystick/analog.c b/drivers/input/joystick/analog.c
index 3d8ff09..2891704 100644
--- a/drivers/input/joystick/analog.c
+++ b/drivers/input/joystick/analog.c
@@ -88,7 +88,7 @@ MODULE_PARM_DESC(map, "Describes analog joysticks type/capabilities");
 #define ANALOG_EXTENSIONS	0x7ff00
 #define ANALOG_GAMEPAD		0x80000
 
-#define ANALOG_MAX_TIME		3	/* 3 ms */
+#define ANALOG_MAX_TIME		3000	/* 3000 us */
 #define ANALOG_LOOP_TIME	2000	/* 2 * loop */
 #define ANALOG_SAITEK_DELAY	200	/* 200 us */
 #define ANALOG_SAITEK_TIME	2000	/* 2000 us */
@@ -257,7 +257,7 @@ 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 / 1000) * port->speed;
 
 	local_irq_save(flags);
 	gameport_trigger(gameport);
@@ -625,20 +625,20 @@ static int analog_init_port(struct gameport *gameport, struct gameport_driver *d
 
 		gameport_trigger(gameport);
 		t = gameport_read(gameport);
-		msleep(ANALOG_MAX_TIME);
+		usleep_range(ANALOG_MAX_TIME, ANALOG_MAX_TIME + 100);
 		port->mask = (gameport_read(gameport) ^ t) & t & 0xf;
 		port->fuzz = (port->speed * ANALOG_FUZZ_MAGIC) / port->loop / 1000 + ANALOG_FUZZ_BITS;
 
 		for (i = 0; i < ANALOG_INIT_RETRIES; i++) {
 			if (!analog_cooked_read(port))
 				break;
-			msleep(ANALOG_MAX_TIME);
+			usleep_range(ANALOG_MAX_TIME, ANALOG_MAX_TIME + 100);
 		}
 
 		u = v = 0;
 
-		msleep(ANALOG_MAX_TIME);
-		t = gameport_time(gameport, ANALOG_MAX_TIME * 1000);
+		usleep_range(ANALOG_MAX_TIME, ANALOG_MAX_TIME + 100);
+		t = gameport_time(gameport, ANALOG_MAX_TIME);
 		gameport_trigger(gameport);
 		while ((gameport_read(port->gameport) & port->mask) && (u < t))
 			u++;
-- 
2.6.2

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

* Re: [PATCH] Input: joystick: analog - change msleep to usleep_range for small msecs
  2016-11-28 19:41 [PATCH] Input: joystick: analog - change msleep to usleep_range for small msecs Aniroop Mathur
@ 2016-11-29  6:51 ` Vojtech Pavlik
  0 siblings, 0 replies; 2+ messages in thread
From: Vojtech Pavlik @ 2016-11-29  6:51 UTC (permalink / raw)
  To: Aniroop Mathur
  Cc: dmitry.torokhov, linux-input, linux-kernel, s.samuel, r.mahale,
	aniroop.mathur

On Tue, Nov 29, 2016 at 01:11:31AM +0530, Aniroop Mathur wrote:

> msleep(1~20) may not do what the caller intends, and will often sleep longer.
> (~20 ms actual sleep for any value given in the 1~20ms range)
> This is not the desired behaviour for many cases like device resume time,
> device suspend time, device enable time, connection time, probe time,
> loops, retry logic, etc
> msleep is built on jiffies / legacy timers which are not precise whereas
> usleep_range is build on top of hrtimers so the wakeups are precise.
> Thus, change msleep to usleep_range for precise wakeups.
> 
> For example:
> On a machine with tick rate / HZ as 100, msleep(3) will make the process to
> sleep for a minimum period of 10 ms whereas usleep_range(3000, 3100) will make
> sure that the process does not sleep for more than 3100 us or 3.1ms

Again, not needed, if the MAX_TIME sleeps are longer, nobody cares.

> 
> Signed-off-by: Aniroop Mathur <a.mathur@samsung.com>
> ---
>  drivers/input/joystick/analog.c | 12 ++++++------
>  1 file changed, 6 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/input/joystick/analog.c b/drivers/input/joystick/analog.c
> index 3d8ff09..2891704 100644
> --- a/drivers/input/joystick/analog.c
> +++ b/drivers/input/joystick/analog.c
> @@ -88,7 +88,7 @@ MODULE_PARM_DESC(map, "Describes analog joysticks type/capabilities");
>  #define ANALOG_EXTENSIONS	0x7ff00
>  #define ANALOG_GAMEPAD		0x80000
>  
> -#define ANALOG_MAX_TIME		3	/* 3 ms */
> +#define ANALOG_MAX_TIME		3000	/* 3000 us */
>  #define ANALOG_LOOP_TIME	2000	/* 2 * loop */
>  #define ANALOG_SAITEK_DELAY	200	/* 200 us */
>  #define ANALOG_SAITEK_TIME	2000	/* 2000 us */
> @@ -257,7 +257,7 @@ 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 / 1000) * port->speed;
>  
>  	local_irq_save(flags);
>  	gameport_trigger(gameport);
> @@ -625,20 +625,20 @@ static int analog_init_port(struct gameport *gameport, struct gameport_driver *d
>  
>  		gameport_trigger(gameport);
>  		t = gameport_read(gameport);
> -		msleep(ANALOG_MAX_TIME);
> +		usleep_range(ANALOG_MAX_TIME, ANALOG_MAX_TIME + 100);
>  		port->mask = (gameport_read(gameport) ^ t) & t & 0xf;
>  		port->fuzz = (port->speed * ANALOG_FUZZ_MAGIC) / port->loop / 1000 + ANALOG_FUZZ_BITS;
>  
>  		for (i = 0; i < ANALOG_INIT_RETRIES; i++) {
>  			if (!analog_cooked_read(port))
>  				break;
> -			msleep(ANALOG_MAX_TIME);
> +			usleep_range(ANALOG_MAX_TIME, ANALOG_MAX_TIME + 100);
>  		}
>  
>  		u = v = 0;
>  
> -		msleep(ANALOG_MAX_TIME);
> -		t = gameport_time(gameport, ANALOG_MAX_TIME * 1000);
> +		usleep_range(ANALOG_MAX_TIME, ANALOG_MAX_TIME + 100);
> +		t = gameport_time(gameport, ANALOG_MAX_TIME);
>  		gameport_trigger(gameport);
>  		while ((gameport_read(port->gameport) & port->mask) && (u < t))
>  			u++;
> -- 
> 2.6.2
> 

-- 
Vojtech Pavlik

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

end of thread, other threads:[~2016-11-29  6:51 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-11-28 19:41 [PATCH] Input: joystick: analog - change msleep to usleep_range for small msecs Aniroop Mathur
2016-11-29  6:51 ` Vojtech Pavlik

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).