staging: comedi: usbdux: tidy up usbdux_ao_insn_write()
authorH Hartley Sweeten <hsweeten@visionengravers.com>
Thu, 25 Jul 2013 23:05:00 +0000 (16:05 -0700)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 26 Jul 2013 22:09:55 +0000 (15:09 -0700)
Rename the local variable used for the private data pointer to the
comedi "norm".

Remove the unnecessary sanity check of the private data pointer. This
function can only be called is the private data was allocated during
the attach.

Tidy up the exit path using goto to ensure that the semaphore is
released.

Return -EBUSY instead of 0 if the (*insn_write) cannot be done because
a command is running.

Tidy up the for() loop that writes the data. The dux_commands[1] and [4]
can be set outside the loop since they are constant. Use a local pointer
for dux_commands[2] to load the value to write. Only the last value needs
to be cached in the private data for read back.

Signed-off-by: H Hartley Sweeten <hsweeten@visionengravers.com>
Reviewed-by: Ian Abbott <abbotti@mev.co.uk>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/staging/comedi/drivers/usbdux.c

index 4159936..1e6cb0a 100644 (file)
@@ -1035,39 +1035,42 @@ static int usbdux_ao_insn_read(struct comedi_device *dev,
 
 static int usbdux_ao_insn_write(struct comedi_device *dev,
                                struct comedi_subdevice *s,
-                               struct comedi_insn *insn, unsigned int *data)
+                               struct comedi_insn *insn,
+                               unsigned int *data)
 {
-       int i, err;
-       int chan = CR_CHAN(insn->chanspec);
-       struct usbdux_private *this_usbduxsub = dev->private;
+       struct usbdux_private *devpriv = dev->private;
+       unsigned int chan = CR_CHAN(insn->chanspec);
+       unsigned int val = devpriv->out_buffer[chan];
+       int16_t *p = (int16_t *)&devpriv->dux_commands[2];
+       int ret = -EBUSY;
+       int i;
 
-       if (!this_usbduxsub)
-               return -EFAULT;
+       down(&devpriv->sem);
 
-       down(&this_usbduxsub->sem);
-       if (this_usbduxsub->ao_cmd_running) {
-               up(&this_usbduxsub->sem);
-               return 0;
-       }
+       if (devpriv->ao_cmd_running)
+               goto ao_write_exit;
+
+       /* number of channels: 1 */
+       devpriv->dux_commands[1] = 1;
+       /* channel number */
+       devpriv->dux_commands[4] = chan << 6;
 
        for (i = 0; i < insn->n; i++) {
-               /* number of channels: 1 */
-               this_usbduxsub->dux_commands[1] = 1;
+               val = data[i];
+
                /* one 16 bit value */
-               *((int16_t *) (this_usbduxsub->dux_commands + 2)) =
-                   cpu_to_le16(data[i]);
-               this_usbduxsub->out_buffer[chan] = data[i];
-               /* channel number */
-               this_usbduxsub->dux_commands[4] = (chan << 6);
-               err = send_dux_commands(dev, SENDDACOMMANDS);
-               if (err < 0) {
-                       up(&this_usbduxsub->sem);
-                       return err;
-               }
+               *p = cpu_to_le16(val);
+
+               ret = send_dux_commands(dev, SENDDACOMMANDS);
+               if (ret < 0)
+                       goto ao_write_exit;
        }
-       up(&this_usbduxsub->sem);
+       devpriv->out_buffer[chan] = val;
 
-       return i;
+ao_write_exit:
+       up(&devpriv->sem);
+
+       return ret ? ret : insn->n;
 }
 
 static int usbdux_ao_inttrig(struct comedi_device *dev,