diff mbox series

[v8,10/22] counter: Standardize to ERANGE for limit exceeded errors

Message ID 7fa80c10fcd10d1d47d1bddced2b2cca3ff59ba9.1613131238.git.vilhelm.gray@gmail.com (mailing list archive)
State New
Headers show
Series Introduce the Counter character device interface | expand

Commit Message

William Breathitt Gray Feb. 12, 2021, 12:13 p.m. UTC
ERANGE is a semantically better error code to return when an argument
value falls outside the supported limit range of a device.

Cc: Syed Nayyar Waris <syednwaris@gmail.com>
Cc: Fabrice Gasnier <fabrice.gasnier@st.com>
Cc: Maxime Coquelin <mcoquelin.stm32@gmail.com>
Cc: Alexandre Torgue <alexandre.torgue@st.com>
Signed-off-by: William Breathitt Gray <vilhelm.gray@gmail.com>
---
 drivers/counter/104-quad-8.c        | 6 +++---
 drivers/counter/stm32-lptimer-cnt.c | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)

Comments

Jonathan Cameron Feb. 14, 2021, 5:10 p.m. UTC | #1
On Fri, 12 Feb 2021 21:13:34 +0900
William Breathitt Gray <vilhelm.gray@gmail.com> wrote:

> ERANGE is a semantically better error code to return when an argument
> value falls outside the supported limit range of a device.

#define	ERANGE		34	/* Math result not representable */

Not generally applicable to a parameter being out of range
despite the name.
#define	EINVAL		22	/* Invalid argument */
Is probably closer to what we want to describe here.

Jonathan


> 
> Cc: Syed Nayyar Waris <syednwaris@gmail.com>
> Cc: Fabrice Gasnier <fabrice.gasnier@st.com>
> Cc: Maxime Coquelin <mcoquelin.stm32@gmail.com>
> Cc: Alexandre Torgue <alexandre.torgue@st.com>
> Signed-off-by: William Breathitt Gray <vilhelm.gray@gmail.com>
> ---
>  drivers/counter/104-quad-8.c        | 6 +++---
>  drivers/counter/stm32-lptimer-cnt.c | 2 +-
>  2 files changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/counter/104-quad-8.c b/drivers/counter/104-quad-8.c
> index 674263b4d2c4..a2cabb028db0 100644
> --- a/drivers/counter/104-quad-8.c
> +++ b/drivers/counter/104-quad-8.c
> @@ -154,7 +154,7 @@ static int quad8_count_write(struct counter_device *counter,
>  
>  	/* Only 24-bit values are supported */
>  	if (val > 0xFFFFFF)
> -		return -EINVAL;
> +		return -ERANGE;
>  
>  	mutex_lock(&priv->lock);
>  
> @@ -671,7 +671,7 @@ static ssize_t quad8_count_preset_write(struct counter_device *counter,
>  
>  	/* Only 24-bit values are supported */
>  	if (preset > 0xFFFFFF)
> -		return -EINVAL;
> +		return -ERANGE;
>  
>  	mutex_lock(&priv->lock);
>  
> @@ -716,7 +716,7 @@ static ssize_t quad8_count_ceiling_write(struct counter_device *counter,
>  
>  	/* Only 24-bit values are supported */
>  	if (ceiling > 0xFFFFFF)
> -		return -EINVAL;
> +		return -ERANGE;
>  
>  	mutex_lock(&priv->lock);
>  
> diff --git a/drivers/counter/stm32-lptimer-cnt.c b/drivers/counter/stm32-lptimer-cnt.c
> index daf988e7b208..d5f9d580d06d 100644
> --- a/drivers/counter/stm32-lptimer-cnt.c
> +++ b/drivers/counter/stm32-lptimer-cnt.c
> @@ -283,7 +283,7 @@ static ssize_t stm32_lptim_cnt_ceiling_write(struct counter_device *counter,
>  		return ret;
>  
>  	if (ceiling > STM32_LPTIM_MAX_ARR)
> -		return -EINVAL;
> +		return -ERANGE;
>  
>  	priv->ceiling = ceiling;
>
William Breathitt Gray Feb. 16, 2021, 1:26 a.m. UTC | #2
On Sun, Feb 14, 2021 at 05:10:21PM +0000, Jonathan Cameron wrote:
> On Fri, 12 Feb 2021 21:13:34 +0900
> William Breathitt Gray <vilhelm.gray@gmail.com> wrote:
> 
> > ERANGE is a semantically better error code to return when an argument
> > value falls outside the supported limit range of a device.
> 
> #define	ERANGE		34	/* Math result not representable */
> 
> Not generally applicable to a parameter being out of range
> despite the name.
> #define	EINVAL		22	/* Invalid argument */
> Is probably closer to what we want to describe here.
> 
> Jonathan

The comment for ERANGE in error-base.h may be terse to a fault. I
believe there's a connotation here provided by ERANGE that is absent
from EINVAL: primarily that the device buffer is incapable of supporting
the desired value (i.e. there is a hardware limitation).

This is why strtoul() returns ERANGE if the correct value is outside the
range of representable values: the result of the operation is valid in
theory (it would be an unsigned integer), but it cannot be returned to
the user due to a limitation of the hardware to support that value (e.g.
32-bit registers) [1].

The changes in this patch follow the same logic: these are arguments
that are valid in theory (e.g. they are unsigned integers), but the
underlying devices are incapable of processing such a value (e.g. the
104-QUAD-8 can only handle 24-bit values).

[1] https://stackoverflow.com/a/34981398/1806289

William Breathitt Gray
David Lechner Feb. 20, 2021, 4:48 p.m. UTC | #3
On 2/12/21 6:13 AM, William Breathitt Gray wrote:
> ERANGE is a semantically better error code to return when an argument
> value falls outside the supported limit range of a device.
> 
> Cc: Syed Nayyar Waris <syednwaris@gmail.com>
> Cc: Fabrice Gasnier <fabrice.gasnier@st.com>
> Cc: Maxime Coquelin <mcoquelin.stm32@gmail.com>
> Cc: Alexandre Torgue <alexandre.torgue@st.com>
> Signed-off-by: William Breathitt Gray <vilhelm.gray@gmail.com>
> ---

Reviewed-by: David Lechner <david@lechnology.com>

(I agree with William's assessment that this use of ERANGE
is consistent with other uses in the kernel.)
Jonathan Cameron Feb. 21, 2021, 2:03 p.m. UTC | #4
On Tue, 16 Feb 2021 10:26:52 +0900
William Breathitt Gray <vilhelm.gray@gmail.com> wrote:

> On Sun, Feb 14, 2021 at 05:10:21PM +0000, Jonathan Cameron wrote:
> > On Fri, 12 Feb 2021 21:13:34 +0900
> > William Breathitt Gray <vilhelm.gray@gmail.com> wrote:
> >   
> > > ERANGE is a semantically better error code to return when an argument
> > > value falls outside the supported limit range of a device.  
> > 
> > #define	ERANGE		34	/* Math result not representable */
> > 
> > Not generally applicable to a parameter being out of range
> > despite the name.
> > #define	EINVAL		22	/* Invalid argument */
> > Is probably closer to what we want to describe here.
> > 
> > Jonathan  
> 
> The comment for ERANGE in error-base.h may be terse to a fault. I
> believe there's a connotation here provided by ERANGE that is absent
> from EINVAL: primarily that the device buffer is incapable of supporting
> the desired value (i.e. there is a hardware limitation).
> 
> This is why strtoul() returns ERANGE if the correct value is outside the
> range of representable values: the result of the operation is valid in
> theory (it would be an unsigned integer), but it cannot be returned to
> the user due to a limitation of the hardware to support that value (e.g.
> 32-bit registers) [1].
> 
> The changes in this patch follow the same logic: these are arguments
> that are valid in theory (e.g. they are unsigned integers), but the
> underlying devices are incapable of processing such a value (e.g. the
> 104-QUAD-8 can only handle 24-bit values).
> 
> [1] https://stackoverflow.com/a/34981398/1806289

Its a bit of a stretch, but I can't claim to feel that strongly about
this.

Jonathan

> 
> William Breathitt Gray
diff mbox series

Patch

diff --git a/drivers/counter/104-quad-8.c b/drivers/counter/104-quad-8.c
index 674263b4d2c4..a2cabb028db0 100644
--- a/drivers/counter/104-quad-8.c
+++ b/drivers/counter/104-quad-8.c
@@ -154,7 +154,7 @@  static int quad8_count_write(struct counter_device *counter,
 
 	/* Only 24-bit values are supported */
 	if (val > 0xFFFFFF)
-		return -EINVAL;
+		return -ERANGE;
 
 	mutex_lock(&priv->lock);
 
@@ -671,7 +671,7 @@  static ssize_t quad8_count_preset_write(struct counter_device *counter,
 
 	/* Only 24-bit values are supported */
 	if (preset > 0xFFFFFF)
-		return -EINVAL;
+		return -ERANGE;
 
 	mutex_lock(&priv->lock);
 
@@ -716,7 +716,7 @@  static ssize_t quad8_count_ceiling_write(struct counter_device *counter,
 
 	/* Only 24-bit values are supported */
 	if (ceiling > 0xFFFFFF)
-		return -EINVAL;
+		return -ERANGE;
 
 	mutex_lock(&priv->lock);
 
diff --git a/drivers/counter/stm32-lptimer-cnt.c b/drivers/counter/stm32-lptimer-cnt.c
index daf988e7b208..d5f9d580d06d 100644
--- a/drivers/counter/stm32-lptimer-cnt.c
+++ b/drivers/counter/stm32-lptimer-cnt.c
@@ -283,7 +283,7 @@  static ssize_t stm32_lptim_cnt_ceiling_write(struct counter_device *counter,
 		return ret;
 
 	if (ceiling > STM32_LPTIM_MAX_ARR)
-		return -EINVAL;
+		return -ERANGE;
 
 	priv->ceiling = ceiling;