]> sigrok.org Git - libsigrok.git/commitdiff
sr: fx2lafw: Fix a firmware upload bug on 32bit systems.
authorUwe Hermann <redacted>
Wed, 30 May 2012 07:10:41 +0000 (09:10 +0200)
committerUwe Hermann <redacted>
Wed, 30 May 2012 07:30:18 +0000 (09:30 +0200)
The glib GTimeVal data type (and some functions using it) will be faded
out from glib sooner or later, so it's not a good idea to use them anyway.

In this specific case GTimeVal.tv_sec was overflowing, leading a check in
libsigrok to fail, and thus to FX2 firmware upload errors, i.e.
non-working fx2lafw devices.

  http://thread.gmane.org/gmane.comp.debugging.sigrok.devel/166

The root cause is that GTimeVal.tv_sec is a 'glong' (8 bytes on 64bit
systems, but only 4 on 32bit systems).

We now use an int64_t (and g_get_monotonic_time() instead of the more
problematics g_get_current_time() which uses a GTimeVal).

This has been verified to fix the issue on a 32bit system.

Other uses of GTimeVal in libsigrok will be removed in a later release.

Also, drop unneeded GTV_TO_MSEC.

hardware/fx2lafw/fx2lafw.c
hardware/fx2lafw/fx2lafw.h
sigrok-internal.h

index a13e9d80f3e6bb3331107486500b7fab49ca7074..1109d802824801537a3febe62de4665bbf6873f3 100644 (file)
@@ -415,7 +415,7 @@ static int hw_init(const char *devinfo)
                        if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION,
                                prof->firmware) == SR_OK)
                                /* Remember when the firmware on this device was updated */
-                               g_get_current_time(&ctx->fw_updated);
+                               ctx->fw_updated = g_get_monotonic_time();
                        else
                                sr_err("fx2lafw: Firmware upload failed for "
                                       "device %d.", devcnt);
@@ -432,10 +432,10 @@ static int hw_init(const char *devinfo)
 
 static int hw_dev_open(int dev_index)
 {
-       GTimeVal cur_time;
        struct sr_dev_inst *sdi;
        struct context *ctx;
-       int timediff, ret;
+       int ret;
+       int64_t timediff_us, timediff_ms;
 
        if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
                return SR_ERR;
@@ -446,19 +446,23 @@ static int hw_dev_open(int dev_index)
         * for the FX2 to renumerate.
         */
        ret = 0;
-       if (GTV_TO_MSEC(ctx->fw_updated) > 0) {
+
+       if (ctx->fw_updated > 0) {
                sr_info("fx2lafw: Waiting for device to reset.");
                /* takes at least 300ms for the FX2 to be gone from the USB bus */
                g_usleep(300 * 1000);
-               timediff = 0;
-               while (timediff < MAX_RENUM_DELAY) {
+               timediff_ms = 0;
+               while (timediff_ms < MAX_RENUM_DELAY) {
                        if ((ret = fx2lafw_dev_open(dev_index)) == SR_OK)
                                break;
                        g_usleep(100 * 1000);
-                       g_get_current_time(&cur_time);
-                       timediff = GTV_TO_MSEC(cur_time) - GTV_TO_MSEC(ctx->fw_updated);
+
+                       timediff_us = g_get_monotonic_time() - ctx->fw_updated;
+                       timediff_ms = timediff_us / G_USEC_PER_SEC;
+                       sr_spew("fx2lafw: timediff: %" PRIi64 " us.",
+                               timediff_us);
                }
-               sr_info("fx2lafw: Device came back after %d ms.", timediff);
+               sr_info("fx2lafw: Device came back after %d ms.", timediff_ms);
        } else {
                ret = fx2lafw_dev_open(dev_index);
        }
index dca21ba3aed9747d40f605976776955c51199754..f8bf99a6285b28a68c7bdd996f3de839c66a4330 100644 (file)
@@ -62,7 +62,7 @@ struct context {
         * after the upgrade) this is like a global lock. No device will open
         * until a proper delay after the last device was upgraded.
         */
-       GTimeVal fw_updated;
+       int64_t fw_updated;
 
        /* Device/Capture Settings */
        uint64_t cur_samplerate;
index d79be5f92df70ce0d2ec4f28e28ee5c2b6b16886..fde076bb931ab4bbf09ff62f9649ac3409ad054f 100644 (file)
@@ -53,8 +53,6 @@ struct sr_serial_dev_inst {
        int fd;
 };
 
-#define GTV_TO_MSEC(gtv)       (gtv.tv_sec * 1000 + gtv.tv_usec / 1000)
-
 #ifdef HAVE_LIBUSB_1_0
 /* USB-specific instances */
 SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,