[PATCH 18/49] staging: comedi: ni_labpc: cleanup labpc_ai_chanlist_invalid()

H Hartley Sweeten hsweeten at visionengravers.com
Tue Apr 15 17:37:38 UTC 2014


This is the Step 5 helper function of the (*do_cmdtest). It 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 any sanity checks are not needed.

For aesthetics, rename this function and tidy it up to follow the style of
the other Step 5 helpers.

Also, remove a couple unnecessary sanity checks of the chanlist.

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/ni_labpc.c | 98 +++++++++++++++----------------
 1 file changed, 49 insertions(+), 49 deletions(-)

diff --git a/drivers/staging/comedi/drivers/ni_labpc.c b/drivers/staging/comedi/drivers/ni_labpc.c
index b0754a2..136214b 100644
--- a/drivers/staging/comedi/drivers/ni_labpc.c
+++ b/drivers/staging/comedi/drivers/ni_labpc.c
@@ -526,77 +526,73 @@ static void labpc_adc_timing(struct comedi_device *dev, struct comedi_cmd *cmd,
 
 static enum scan_mode labpc_ai_scan_mode(const struct comedi_cmd *cmd)
 {
-	if (cmd->chanlist_len == 1)
-		return MODE_SINGLE_CHAN;
+	unsigned int chan0 = CR_CHAN(cmd->chanlist[0]);
+	unsigned int chan1 = (cmd->chanlist_len > 1)
+				? CR_CHAN(cmd->chanlist[1]) : chan0;
 
-	if (CR_CHAN(cmd->chanlist[0]) == CR_CHAN(cmd->chanlist[1]))
-		return MODE_SINGLE_CHAN_INTERVAL;
-
-	if (CR_CHAN(cmd->chanlist[0]) < CR_CHAN(cmd->chanlist[1]))
+	if (chan0 == chan1) {
+		if (cmd->chanlist_len == 1)
+			return MODE_SINGLE_CHAN;
+		else
+			return MODE_SINGLE_CHAN_INTERVAL;
+	} else if (chan0 < chan1) {
 		return MODE_MULT_CHAN_UP;
-
-	if (CR_CHAN(cmd->chanlist[0]) > CR_CHAN(cmd->chanlist[1]))
+	} else {
 		return MODE_MULT_CHAN_DOWN;
-
-	pr_err("ni_labpc: bug! cannot determine AI scan mode\n");
-	return 0;
+	}
 }
 
-static int labpc_ai_chanlist_invalid(const struct comedi_device *dev,
-				     const struct comedi_cmd *cmd,
-				     enum scan_mode mode)
+static int labpc_ai_check_chanlist(const struct comedi_device *dev,
+				   const struct comedi_subdevice *s,
+				   const struct comedi_cmd *cmd)
 {
-	int channel, range, aref, i;
-
-	if (mode == MODE_SINGLE_CHAN)
-		return 0;
-
-	channel = CR_CHAN(cmd->chanlist[0]);
-	range = CR_RANGE(cmd->chanlist[0]);
-	aref = CR_AREF(cmd->chanlist[0]);
+	enum scan_mode mode = labpc_ai_scan_mode(cmd);
+	unsigned int chan0 = CR_CHAN(cmd->chanlist[0]);
+	unsigned int range0 = CR_RANGE(cmd->chanlist[0]);
+	unsigned int aref0 = CR_AREF(cmd->chanlist[0]);
+	int i;
 
 	for (i = 0; i < cmd->chanlist_len; i++) {
+		unsigned int chan = CR_CHAN(cmd->chanlist[i]);
+		unsigned int range = CR_RANGE(cmd->chanlist[i]);
+		unsigned int aref = CR_AREF(cmd->chanlist[i]);
 
 		switch (mode) {
+		case MODE_SINGLE_CHAN:
+			break;
 		case MODE_SINGLE_CHAN_INTERVAL:
-			if (CR_CHAN(cmd->chanlist[i]) != channel) {
-				comedi_error(dev,
-					     "channel scanning order specified in chanlist is not supported by hardware.\n");
-				return 1;
+			if (chan != chan0) {
+				dev_err(dev->class_dev,
+					"channel scanning order specified in chanlist is not supported by hardware\n");
+				return -EINVAL;
 			}
 			break;
 		case MODE_MULT_CHAN_UP:
-			if (CR_CHAN(cmd->chanlist[i]) != i) {
-				comedi_error(dev,
-					     "channel scanning order specified in chanlist is not supported by hardware.\n");
-				return 1;
+			if (chan != i) {
+				dev_err(dev->class_dev,
+					"channel scanning order specified in chanlist is not supported by hardware\n");
+				return -EINVAL;
 			}
 			break;
 		case MODE_MULT_CHAN_DOWN:
-			if (CR_CHAN(cmd->chanlist[i]) !=
-			    cmd->chanlist_len - i - 1) {
-				comedi_error(dev,
-					     "channel scanning order specified in chanlist is not supported by hardware.\n");
-				return 1;
+			if (chan != cmd->chanlist_len - i - 1) {
+				dev_err(dev->class_dev,
+					"channel scanning order specified in chanlist is not supported by hardware\n");
+				return -EINVAL;
 			}
 			break;
-		default:
-			dev_err(dev->class_dev,
-				"ni_labpc: bug! in chanlist check\n");
-			return 1;
-			break;
 		}
 
-		if (CR_RANGE(cmd->chanlist[i]) != range) {
-			comedi_error(dev,
-				     "entries in chanlist must all have the same range\n");
-			return 1;
+		if (range != range0) {
+			dev_err(dev->class_dev,
+				"chanlist must all have the same range\n");
+			return -EINVAL;
 		}
 
-		if (CR_AREF(cmd->chanlist[i]) != aref) {
-			comedi_error(dev,
-				     "entries in chanlist must all have the same reference\n");
-			return 1;
+		if (aref != aref0) {
+			dev_err(dev->class_dev,
+				"chanlist must all have the same reference\n");
+			return -EINVAL;
 		}
 	}
 
@@ -694,7 +690,11 @@ static int labpc_ai_cmdtest(struct comedi_device *dev,
 	if (err)
 		return 4;
 
-	if (labpc_ai_chanlist_invalid(dev, cmd, mode))
+	/* Step 5: check channel list */
+
+	err |= labpc_ai_check_chanlist(dev, s, cmd);
+
+	if (err)
 		return 5;
 
 	return 0;
-- 
1.8.5.2



More information about the devel mailing list