DriverDev-Devel Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH] staging: comedi: usbduxfast: usbduxfast_ai_cmdtest rounding error
@ 2019-11-18 23:07 Bernd Porr
  2019-11-19 10:36 ` Ian Abbott
  0 siblings, 1 reply; 2+ messages in thread
From: Bernd Porr @ 2019-11-18 23:07 UTC (permalink / raw)
  To: gregkh; +Cc: abbotti, driverdev-devel

The userspace comedilib function 'get_cmd_generic_timed' fills
the cmd structure with an informed guess and then calls the
function 'usbduxfast_ai_cmdtest' in this driver repeatedly while
'usbduxfast_ai_cmdtest' is modifying the cmd struct until it
no longer changes. However, because of rounding errors this never
converged because 'steps = (cmd->convert_arg * 30) / 1000' and then
back to 'cmd->convert_arg = (steps * 1000) / 30' won't be the same
because of rounding errors. 'Steps' should only be converted back to
the 'convert_arg' if 'steps' has actually been modified. In addition
the case of steps being 0 wasn't checked which is also now done.

Signed-off-by: Bernd Porr <mail@berndporr.me.uk>
---
 drivers/staging/comedi/drivers/usbduxfast.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)

diff --git a/drivers/staging/comedi/drivers/usbduxfast.c b/drivers/staging/comedi/drivers/usbduxfast.c
index 04bc488385e6..4af012968cb6 100644
--- a/drivers/staging/comedi/drivers/usbduxfast.c
+++ b/drivers/staging/comedi/drivers/usbduxfast.c
@@ -1,6 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0+
 /*
- *  Copyright (C) 2004-2014 Bernd Porr, mail@berndporr.me.uk
+ *  Copyright (C) 2004-2019 Bernd Porr, mail@berndporr.me.uk
  */
 
 /*
@@ -8,7 +8,7 @@
  * Description: University of Stirling USB DAQ & INCITE Technology Limited
  * Devices: [ITL] USB-DUX-FAST (usbduxfast)
  * Author: Bernd Porr <mail@berndporr.me.uk>
- * Updated: 10 Oct 2014
+ * Updated: 16 Nov 2019
  * Status: stable
  */
 
@@ -22,6 +22,7 @@
  *
  *
  * Revision history:
+ * 1.0: Fixed a rounding error in usbduxfast_ai_cmdtest
  * 0.9: Dropping the first data packet which seems to be from the last transfer.
  *      Buffer overflows in the FX2 are handed over to comedi.
  * 0.92: Dropping now 4 packets. The quad buffer has to be emptied.
@@ -350,6 +351,7 @@ static int usbduxfast_ai_cmdtest(struct comedi_device *dev,
 				 struct comedi_cmd *cmd)
 {
 	int err = 0;
+	int err2 = 0;
 	unsigned int steps;
 	unsigned int arg;
 
@@ -399,11 +401,16 @@ static int usbduxfast_ai_cmdtest(struct comedi_device *dev,
 	 */
 	steps = (cmd->convert_arg * 30) / 1000;
 	if (cmd->chanlist_len !=  1)
-		err |= comedi_check_trigger_arg_min(&steps,
-						    MIN_SAMPLING_PERIOD);
-	err |= comedi_check_trigger_arg_max(&steps, MAX_SAMPLING_PERIOD);
-	arg = (steps * 1000) / 30;
-	err |= comedi_check_trigger_arg_is(&cmd->convert_arg, arg);
+		err2 |= comedi_check_trigger_arg_min(&steps,
+						     MIN_SAMPLING_PERIOD);
+	else
+		err2 |= comedi_check_trigger_arg_min(&steps, 1);
+	err2 |= comedi_check_trigger_arg_max(&steps, MAX_SAMPLING_PERIOD);
+	if (err2) {
+		err |= err2;
+		arg = (steps * 1000) / 30;
+		err |= comedi_check_trigger_arg_is(&cmd->convert_arg, arg);
+	}
 
 	if (cmd->stop_src == TRIG_COUNT)
 		err |= comedi_check_trigger_arg_min(&cmd->stop_arg, 1);
-- 
2.17.1

_______________________________________________
devel mailing list
devel@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel

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

* Re: [PATCH] staging: comedi: usbduxfast: usbduxfast_ai_cmdtest rounding error
  2019-11-18 23:07 [PATCH] staging: comedi: usbduxfast: usbduxfast_ai_cmdtest rounding error Bernd Porr
@ 2019-11-19 10:36 ` Ian Abbott
  0 siblings, 0 replies; 2+ messages in thread
From: Ian Abbott @ 2019-11-19 10:36 UTC (permalink / raw)
  To: Bernd Porr, gregkh; +Cc: driverdev-devel

On 18/11/2019 23:07, Bernd Porr wrote:
> The userspace comedilib function 'get_cmd_generic_timed' fills
> the cmd structure with an informed guess and then calls the
> function 'usbduxfast_ai_cmdtest' in this driver repeatedly while
> 'usbduxfast_ai_cmdtest' is modifying the cmd struct until it
> no longer changes. However, because of rounding errors this never
> converged because 'steps = (cmd->convert_arg * 30) / 1000' and then
> back to 'cmd->convert_arg = (steps * 1000) / 30' won't be the same
> because of rounding errors. 'Steps' should only be converted back to
> the 'convert_arg' if 'steps' has actually been modified. In addition
> the case of steps being 0 wasn't checked which is also now done.
> 
> Signed-off-by: Bernd Porr <mail@berndporr.me.uk>
> ---
>   drivers/staging/comedi/drivers/usbduxfast.c | 21 ++++++++++++++-------
>   1 file changed, 14 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/staging/comedi/drivers/usbduxfast.c b/drivers/staging/comedi/drivers/usbduxfast.c
> index 04bc488385e6..4af012968cb6 100644
> --- a/drivers/staging/comedi/drivers/usbduxfast.c
> +++ b/drivers/staging/comedi/drivers/usbduxfast.c
> @@ -1,6 +1,6 @@
>   // SPDX-License-Identifier: GPL-2.0+
>   /*
> - *  Copyright (C) 2004-2014 Bernd Porr, mail@berndporr.me.uk
> + *  Copyright (C) 2004-2019 Bernd Porr, mail@berndporr.me.uk
>    */
>   
>   /*
> @@ -8,7 +8,7 @@
>    * Description: University of Stirling USB DAQ & INCITE Technology Limited
>    * Devices: [ITL] USB-DUX-FAST (usbduxfast)
>    * Author: Bernd Porr <mail@berndporr.me.uk>
> - * Updated: 10 Oct 2014
> + * Updated: 16 Nov 2019
>    * Status: stable
>    */
>   
> @@ -22,6 +22,7 @@
>    *
>    *
>    * Revision history:
> + * 1.0: Fixed a rounding error in usbduxfast_ai_cmdtest
>    * 0.9: Dropping the first data packet which seems to be from the last transfer.
>    *      Buffer overflows in the FX2 are handed over to comedi.
>    * 0.92: Dropping now 4 packets. The quad buffer has to be emptied.
> @@ -350,6 +351,7 @@ static int usbduxfast_ai_cmdtest(struct comedi_device *dev,
>   				 struct comedi_cmd *cmd)
>   {
>   	int err = 0;
> +	int err2 = 0;
>   	unsigned int steps;
>   	unsigned int arg;
>   
> @@ -399,11 +401,16 @@ static int usbduxfast_ai_cmdtest(struct comedi_device *dev,
>   	 */
>   	steps = (cmd->convert_arg * 30) / 1000;
>   	if (cmd->chanlist_len !=  1)
> -		err |= comedi_check_trigger_arg_min(&steps,
> -						    MIN_SAMPLING_PERIOD);
> -	err |= comedi_check_trigger_arg_max(&steps, MAX_SAMPLING_PERIOD);
> -	arg = (steps * 1000) / 30;
> -	err |= comedi_check_trigger_arg_is(&cmd->convert_arg, arg);
> +		err2 |= comedi_check_trigger_arg_min(&steps,
> +						     MIN_SAMPLING_PERIOD);
> +	else
> +		err2 |= comedi_check_trigger_arg_min(&steps, 1);
> +	err2 |= comedi_check_trigger_arg_max(&steps, MAX_SAMPLING_PERIOD);
> +	if (err2) {
> +		err |= err2;
> +		arg = (steps * 1000) / 30;
> +		err |= comedi_check_trigger_arg_is(&cmd->convert_arg, arg);
> +	}
>   
>   	if (cmd->stop_src == TRIG_COUNT)
>   		err |= comedi_check_trigger_arg_min(&cmd->stop_arg, 1);
> 

It looks fine to me.

Note to Greg: The patch subject line and patch description has been 
changed in this version of the patch, but the patch body is the same. 
The previous version with subject line "[PATCH] staging: comedi: 
usbduxsigma: usbduxfast_ai_cmdtest rounding error" can therefore be dropped.

I guess this can be applied to stable to fix problems with round-trip 
rounding.  If so, the following can be added:

Cc: <stable@vger.kernel.org> # 4.4+

Reviewed-by: Ian Abbott <abbotti@mev.co.uk>

-- 
-=( Ian Abbott <abbotti@mev.co.uk> || Web: www.mev.co.uk )=-
-=( MEV Ltd. is a company registered in England & Wales. )=-
-=( Registered number: 02862268.  Registered address:    )=-
-=( 15 West Park Road, Bramhall, STOCKPORT, SK7 3JZ, UK. )=-
_______________________________________________
devel mailing list
devel@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel

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

end of thread, back to index

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-11-18 23:07 [PATCH] staging: comedi: usbduxfast: usbduxfast_ai_cmdtest rounding error Bernd Porr
2019-11-19 10:36 ` Ian Abbott

DriverDev-Devel Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/driverdev-devel/0 driverdev-devel/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 driverdev-devel driverdev-devel/ https://lore.kernel.org/driverdev-devel \
		driverdev-devel@linuxdriverproject.org devel@driverdev.osuosl.org
	public-inbox-index driverdev-devel

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.linuxdriverproject.driverdev-devel


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git