staging: comedi: daqboard2000: replace daqboard2000_poll_cpld()
authorIan Abbott <abbotti@mev.co.uk>
Wed, 4 Jan 2017 10:55:38 +0000 (10:55 +0000)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Tue, 10 Jan 2017 16:38:32 +0000 (17:38 +0100)
`daqboard2000_poll_cpld()` waits for a specified status bit in the CPLD
status register to be set, giving up after 50 tries over a period of
about 5 milliseconds.  It returns 1 if the status bit is set, otherwise
0.  It is only ever called to check the "INIT" status bit.  Replace it
with new function `daqboard2000_wait_cpld_init()`, which returns 0 if
the "INIT" status bit becomes set within 50 tries, or `-ETIMEDOUT` if
not set within 50 tries.  The firmware loading callback
`daqboard2000_load_firmware()` may return the error result from
`daqboard2000_wait_cpld_init()` if it has used up all its firmware
loading attempts and that was the last error.

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

index 49feec39c4c6a316dcc3f15014316c547615d9e5..f4c738037e283fcd4763a67108232766d38792b1 100644 (file)
@@ -470,17 +470,17 @@ static void daqboard2000_pulse_prog_pin(struct comedi_device *dev)
        mdelay(10);     /* Not in the original code, but I like symmetry... */
 }
 
-static int daqboard2000_poll_cpld(struct comedi_device *dev, u16 mask)
+static int daqboard2000_wait_cpld_init(struct comedi_device *dev)
 {
-       int result = 0;
+       int result = -ETIMEDOUT;
        int i;
        u16 cpld;
 
        /* timeout after 50 tries -> 5ms */
        for (i = 0; i < 50; i++) {
                cpld = readw(dev->mmio + DB2K_REG_CPLD_STATUS);
-               if ((cpld & mask) == mask) {
-                       result = 1;
+               if (cpld & DB2K_CPLD_STATUS_INIT) {
+                       result = 0;
                        break;
                }
                usleep_range(100, 1000);
@@ -540,20 +540,23 @@ static int daqboard2000_load_firmware(struct comedi_device *dev,
                daqboard2000_reset_local_bus(dev);
                daqboard2000_reload_plx(dev);
                daqboard2000_pulse_prog_pin(dev);
-               if (daqboard2000_poll_cpld(dev, DB2K_CPLD_STATUS_INIT)) {
-                       for (; i < len; i += 2) {
-                               u16 data =
-                                   (cpld_array[i] << 8) + cpld_array[i + 1];
-                               if (!daqboard2000_write_cpld(dev, data))
-                                       break;
-                       }
-                       if (i >= len) {
-                               daqboard2000_reset_local_bus(dev);
-                               daqboard2000_reload_plx(dev);
-                               result = 0;
+               result = daqboard2000_wait_cpld_init(dev);
+               if (result)
+                       continue;
+
+               for (; i < len; i += 2) {
+                       u16 data = (cpld_array[i] << 8) + cpld_array[i + 1];
+
+                       if (!daqboard2000_write_cpld(dev, data)) {
+                               result = -EIO;
                                break;
                        }
                }
+               if (result == 0) {
+                       daqboard2000_reset_local_bus(dev);
+                       daqboard2000_reload_plx(dev);
+                       break;
+               }
        }
        return result;
 }