counter: Return error code on invalid modes
authorWilliam Breathitt Gray <vilhelm.gray@gmail.com>
Tue, 3 Aug 2021 12:06:12 +0000 (21:06 +0900)
committerJonathan Cameron <Jonathan.Cameron@huawei.com>
Mon, 9 Aug 2021 19:17:36 +0000 (20:17 +0100)
Only a select set of modes (function, action, etc.) are valid for a
given device configuration. This patch ensures that invalid modes result
in a return -EINVAL. Such a situation should never occur in reality, but
it's good to define a default switch case for the sake of making the
intent of the code clear.

Cc: Kamel Bouhara <kamel.bouhara@bootlin.com>
Cc: Maxime Coquelin <mcoquelin.stm32@gmail.com>
Cc: Alexandre Torgue <alexandre.torgue@st.com>
Acked-by: David Lechner <david@lechnology.com>
Acked-by: Syed Nayyar Waris <syednwaris@gmail.com>
Reviewed-by: Fabrice Gasnier <fabrice.gasnier@foss.st.com>
Signed-off-by: William Breathitt Gray <vilhelm.gray@gmail.com>
Link: https://lore.kernel.org/r/7af82d4e39610da11edce0ee370285fe1cb1eac8.1627990337.git.vilhelm.gray@gmail.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
drivers/counter/104-quad-8.c
drivers/counter/microchip-tcb-capture.c
drivers/counter/stm32-lptimer-cnt.c
drivers/counter/ti-eqep.c

index 81f9642..b358b2b 100644 (file)
@@ -273,6 +273,10 @@ static int quad8_function_set(struct counter_device *counter,
                        *scale = 2;
                        mode_cfg |= QUAD8_CMR_QUADRATURE_X4;
                        break;
+               default:
+                       /* should never reach this path */
+                       mutex_unlock(&priv->lock);
+                       return -EINVAL;
                }
        }
 
@@ -349,7 +353,7 @@ static int quad8_action_get(struct counter_device *counter,
        case QUAD8_COUNT_FUNCTION_PULSE_DIRECTION:
                if (synapse->signal->id == signal_a_id)
                        *action = QUAD8_SYNAPSE_ACTION_RISING_EDGE;
-               break;
+               return 0;
        case QUAD8_COUNT_FUNCTION_QUADRATURE_X1:
                if (synapse->signal->id == signal_a_id) {
                        quad8_direction_get(counter, count, &direction);
@@ -359,17 +363,18 @@ static int quad8_action_get(struct counter_device *counter,
                        else
                                *action = QUAD8_SYNAPSE_ACTION_FALLING_EDGE;
                }
-               break;
+               return 0;
        case QUAD8_COUNT_FUNCTION_QUADRATURE_X2:
                if (synapse->signal->id == signal_a_id)
                        *action = QUAD8_SYNAPSE_ACTION_BOTH_EDGES;
-               break;
+               return 0;
        case QUAD8_COUNT_FUNCTION_QUADRATURE_X4:
                *action = QUAD8_SYNAPSE_ACTION_BOTH_EDGES;
-               break;
+               return 0;
+       default:
+               /* should never reach this path */
+               return -EINVAL;
        }
-
-       return 0;
 }
 
 static const struct counter_ops quad8_ops = {
@@ -529,6 +534,9 @@ static int quad8_count_mode_set(struct counter_device *counter,
        case COUNTER_COUNT_MODE_MODULO_N:
                cnt_mode = 3;
                break;
+       default:
+               /* should never reach this path */
+               return -EINVAL;
        }
 
        mutex_lock(&priv->lock);
index 51b8af8..0c9a619 100644 (file)
@@ -133,6 +133,9 @@ static int mchp_tc_count_function_set(struct counter_device *counter,
                bmr |= ATMEL_TC_QDEN | ATMEL_TC_POSEN;
                cmr |= ATMEL_TC_ETRGEDG_RISING | ATMEL_TC_ABETRG | ATMEL_TC_XC0;
                break;
+       default:
+               /* should never reach this path */
+               return -EINVAL;
        }
 
        regmap_write(priv->regmap, ATMEL_TC_BMR, bmr);
@@ -226,6 +229,9 @@ static int mchp_tc_count_action_set(struct counter_device *counter,
        case MCHP_TC_SYNAPSE_ACTION_BOTH_EDGE:
                edge = ATMEL_TC_ETRGEDG_BOTH;
                break;
+       default:
+               /* should never reach this path */
+               return -EINVAL;
        }
 
        return regmap_write_bits(priv->regmap,
index c19d998..78f383b 100644 (file)
@@ -206,9 +206,10 @@ static int stm32_lptim_cnt_function_set(struct counter_device *counter,
                priv->quadrature_mode = 1;
                priv->polarity = STM32_LPTIM_SYNAPSE_ACTION_BOTH_EDGES;
                return 0;
+       default:
+               /* should never reach this path */
+               return -EINVAL;
        }
-
-       return -EINVAL;
 }
 
 static ssize_t stm32_lptim_cnt_enable_read(struct counter_device *counter,
@@ -326,9 +327,10 @@ static int stm32_lptim_cnt_action_get(struct counter_device *counter,
        case STM32_LPTIM_ENCODER_BOTH_EDGE:
                *action = priv->polarity;
                return 0;
+       default:
+               /* should never reach this path */
+               return -EINVAL;
        }
-
-       return -EINVAL;
 }
 
 static int stm32_lptim_cnt_action_set(struct counter_device *counter,
index 65df9ef..c303eb1 100644 (file)
@@ -157,7 +157,7 @@ static int ti_eqep_action_get(struct counter_device *counter,
                 * QEPA and QEPB trigger QCLK.
                 */
                *action = TI_EQEP_SYNAPSE_ACTION_BOTH_EDGES;
-               break;
+               return 0;
        case TI_EQEP_COUNT_FUNC_DIR_COUNT:
                /* In direction-count mode only rising edge of QEPA is counted
                 * and QEPB gives direction.
@@ -165,12 +165,14 @@ static int ti_eqep_action_get(struct counter_device *counter,
                switch (synapse->signal->id) {
                case TI_EQEP_SIGNAL_QEPA:
                        *action = TI_EQEP_SYNAPSE_ACTION_RISING_EDGE;
-                       break;
-               default:
+                       return 0;
+               case TI_EQEP_SIGNAL_QEPB:
                        *action = TI_EQEP_SYNAPSE_ACTION_NONE;
-                       break;
+                       return 0;
+               default:
+                       /* should never reach this path */
+                       return -EINVAL;
                }
-               break;
        case TI_EQEP_COUNT_FUNC_UP_COUNT:
        case TI_EQEP_COUNT_FUNC_DOWN_COUNT:
                /* In up/down-count modes only QEPA is counted and QEPB is not
@@ -186,15 +188,18 @@ static int ti_eqep_action_get(struct counter_device *counter,
                                *action = TI_EQEP_SYNAPSE_ACTION_BOTH_EDGES;
                        else
                                *action = TI_EQEP_SYNAPSE_ACTION_RISING_EDGE;
-                       break;
-               default:
+                       return 0;
+               case TI_EQEP_SIGNAL_QEPB:
                        *action = TI_EQEP_SYNAPSE_ACTION_NONE;
-                       break;
+                       return 0;
+               default:
+                       /* should never reach this path */
+                       return -EINVAL;
                }
-               break;
+       default:
+               /* should never reach this path */
+               return -EINVAL;
        }
-
-       return 0;
 }
 
 static const struct counter_ops ti_eqep_counter_ops = {