[PATCH 10/49] staging: comedi: das16: factor out step 5 of (*do_cmdtest)

Ian Abbott abbotti at mev.co.uk
Wed Apr 16 16:38:48 UTC 2014


On 2014-04-15 18:37, H Hartley Sweeten wrote:
> Step 5 of the (*do_cmdtest) validates that the chanlist, chan/range/aref/etc,
> is compatible with the actual hardware. At this point in the (*do_cmdtest)
> the chanlist is valid, due to checks in the core, so the sanity check is not
> needed.
>
> For aesthetics, factor the step 5 code into a helper functions. Tidy up the
> factored out code.
>
> Signed-off-by: H Hartley Sweeten <hsweeten at visionengravers.com>
> Cc: Ian Abbott <abbotti at mev.co.uk>
> Cc: Greg Kroah-Hartman <gregkh at linuxfoundation.org>
> ---
>   drivers/staging/comedi/drivers/das16.c | 50 +++++++++++++++++++++-------------
>   1 file changed, 31 insertions(+), 19 deletions(-)
>
> diff --git a/drivers/staging/comedi/drivers/das16.c b/drivers/staging/comedi/drivers/das16.c
> index 6a7d652..f8cae95 100644
> --- a/drivers/staging/comedi/drivers/das16.c
> +++ b/drivers/staging/comedi/drivers/das16.c
> @@ -600,13 +600,39 @@ static void das16_timer_interrupt(unsigned long arg)
>   		mod_timer(&devpriv->timer, jiffies + timer_period());
>   }
>
> +static int das16_ai_check_chanlist(struct comedi_device *dev,
> +				   struct comedi_subdevice *s,
> +				   struct comedi_cmd *cmd)
> +{
> +	unsigned int chan0 = CR_CHAN(cmd->chanlist[0]);
> +	unsigned int range0 = CR_RANGE(cmd->chanlist[0]);
> +	int i;
> +
> +	for (i = 1; i < cmd->chanlist_len; i++) {
> +		unsigned int chan = CR_CHAN(cmd->chanlist[i]);
> +		unsigned int range = CR_RANGE(cmd->chanlist[i]);
> +
> +		if (chan != (chan0 + i) % s->n_chan) {
> +			dev_err(dev->class_dev,
> +				"chanlist must be consecutive channels, counting upwards\n");
> +			return -EINVAL;
> +		}
> +
> +		if (range != range0) {
> +			dev_err(dev->class_dev,
> +				"chanlist must all have the same range\n");
> +			return -EINVAL;
> +		}
> +	}
> +	return 0;
> +}
> +
>   static int das16_cmd_test(struct comedi_device *dev, struct comedi_subdevice *s,
>   			  struct comedi_cmd *cmd)
>   {
>   	const struct das16_board *board = comedi_board(dev);
>   	struct das16_private_struct *devpriv = dev->private;
>   	int err = 0, tmp;
> -	int gain, start_chan, i;
>   	int mask;
>
>   	/* Step 1 : check if triggers are trivially valid */
> @@ -693,24 +719,10 @@ static int das16_cmd_test(struct comedi_device *dev, struct comedi_subdevice *s,
>   	if (err)
>   		return 4;
>
> -	/*  check channel/gain list against card's limitations */
> -	if (cmd->chanlist) {
> -		gain = CR_RANGE(cmd->chanlist[0]);
> -		start_chan = CR_CHAN(cmd->chanlist[0]);
> -		for (i = 1; i < cmd->chanlist_len; i++) {
> -			if (CR_CHAN(cmd->chanlist[i]) !=
> -			    (start_chan + i) % s->n_chan) {
> -				dev_err(dev->class_dev,
> -					"entries in chanlist must be consecutive channels, counting upwards\n");
> -				err++;
> -			}
> -			if (CR_RANGE(cmd->chanlist[i]) != gain) {
> -				dev_err(dev->class_dev,
> -					"entries in chanlist must all have the same gain\n");
> -				err++;
> -			}
> -		}
> -	}
> +	/* Step 5: check channel list */
> +
> +	err |= das16_ai_check_chanlist(dev, s, cmd);
> +
>   	if (err)
>   		return 5;
>
>

As for the remarks in patch 02, it's possible for chanlist to be NULL or 
for chanlist_len to be 0, so das16_ai_check_chanlist() should only be 
called if chanlist is non-NULL and chanlist_len is non-0.

But if chanlist is forced to be NULL by __comedi_get_user_chanlist() 
when chanlist_len is 0, it would be sufficient to check that chanlist is 
non-NULL before calling das16_ai_check_chanlist().

The original code is buggy as well because it accesses cmd->chanlist[0] 
without checking cmd->chanlist_len.

-- 
-=( Ian Abbott @ MEV Ltd.    E-mail: <abbotti at mev.co.uk>        )=-
-=( Tel: +44 (0)161 477 1898   FAX: +44 (0)161 718 3587         )=-


More information about the devel mailing list