]> sigrok.org Git - libsigrok.git/blobdiff - hardware/asix-sigma/asix-sigma.c
LA8: Remove trailing whitespace.
[libsigrok.git] / hardware / asix-sigma / asix-sigma.c
index 2722d0db10fd5a2339e2bef2eab0b02c8cd31740..e33b5b7959242376ed2c7573867303cd488d4e29 100644 (file)
@@ -20,7 +20,7 @@
  */
 
 /*
- * ASIX Sigma Logic Analyzer Driver
+ * ASIX SIGMA Logic Analyzer Driver
  */
 
 #include "config.h"
@@ -318,25 +318,30 @@ static int bin2bitbang(const char *filename,
        f = g_fopen(filename, "rb");
        if (!f) {
                sr_warn("g_fopen(\"%s\", \"rb\")", filename);
-               return -1;
+               return SR_ERR;
        }
 
        if (-1 == fseek(f, 0, SEEK_END)) {
                sr_warn("fseek on %s failed", filename);
                fclose(f);
-               return -1;
+               return SR_ERR;
        }
 
        file_size = ftell(f);
 
        fseek(f, 0, SEEK_SET);
 
-       compressed_buf = g_malloc(file_size);
-       firmware = g_malloc(buffer_size);
+       if (!(compressed_buf = g_try_malloc(file_size))) {
+               sr_err("sigma: %s: compressed_buf malloc failed", __func__);
+               fclose(f);
+               return SR_ERR_MALLOC;
+       }
 
-       if (!compressed_buf || !firmware) {
-               sr_warn("Error allocating buffers");
-               return -1;
+       if (!(firmware = g_try_malloc(buffer_size))) {
+               sr_err("sigma: %s: firmware malloc failed", __func__);
+               fclose(f);
+               g_free(compressed_buf);
+               return SR_ERR_MALLOC;
        }
 
        csize = 0;
@@ -352,18 +357,19 @@ static int bin2bitbang(const char *filename,
                g_free(compressed_buf);
                g_free(firmware);
                sr_warn("Could not unpack Sigma firmware. (Error %d)\n", ret);
-               return -1;
+               return SR_ERR;
        }
 
        g_free(compressed_buf);
 
        *buf_size = fwsize * 2 * 8;
 
-       *buf = p = (unsigned char *)g_malloc(*buf_size);
-
+       *buf = p = (unsigned char *)g_try_malloc(*buf_size);
        if (!p) {
-               sr_warn("Error allocating buffers");
-               return -1;
+               sr_err("sigma: %s: buf/p malloc failed", __func__);
+               g_free(compressed_buf);
+               g_free(firmware);
+               return SR_ERR_MALLOC;
        }
 
        for (i = 0; i < fwsize; ++i) {
@@ -382,21 +388,24 @@ static int bin2bitbang(const char *filename,
                        "offset=%ld, file_size=%ld, buf_size=%zd\n",
                        filename, offset, file_size, *buf_size);
 
-               return -1;
+               return SR_ERR;
        }
 
-       return 0;
+       return SR_OK;
 }
 
 static int hw_init(const char *deviceinfo)
 {
        struct sr_device_instance *sdi;
-       struct sigma *sigma = g_malloc(sizeof(struct sigma));
+       struct sigma *sigma;
 
+       /* Avoid compiler warnings. */
        deviceinfo = deviceinfo;
 
-       if (!sigma)
-               return 0;
+       if (!(sigma = g_try_malloc(sizeof(struct sigma)))) {
+               sr_err("sigma: %s: sigma malloc failed", __func__);
+               return 0; /* FIXME: Should be SR_ERR_MALLOC. */
+       }
 
        ftdi_init(&sigma->ftdic);
 
@@ -428,7 +437,7 @@ static int hw_init(const char *deviceinfo)
 
        return 1;
 free:
-       free(sigma);
+       g_free(sigma);
        return 0;
 }
 
@@ -484,10 +493,10 @@ static int upload_firmware(int firmware_idx, struct sigma *sigma)
        snprintf(firmware_path, sizeof(firmware_path), "%s/%s", FIRMWARE_DIR,
                 firmware_files[firmware_idx]);
 
-       if (-1 == bin2bitbang(firmware_path, &buf, &buf_size)) {
+       if ((ret = bin2bitbang(firmware_path, &buf, &buf_size)) != SR_OK) {
                sr_warn("An error occured while reading the firmware: %s",
                        firmware_path);
-               return SR_ERR;
+               return ret;
        }
 
        /* Upload firmare. */
@@ -612,7 +621,7 @@ static int configure_probes(struct sr_device_instance *sdi, GSList *probes)
                if (sigma->cur_samplerate >= SR_MHZ(100)) {
                        /* Fast trigger support. */
                        if (trigger_set) {
-                               sr_warn("Asix Sigma only supports a single "
+                               sr_warn("ASIX SIGMA only supports a single "
                                        "pin trigger in 100 and 200MHz mode.");
                                return SR_ERR;
                        }
@@ -621,7 +630,7 @@ static int configure_probes(struct sr_device_instance *sdi, GSList *probes)
                        else if (probe->trigger[0] == 'r')
                                sigma->trigger.risingmask |= probebit;
                        else {
-                               sr_warn("Asix Sigma only supports "
+                               sr_warn("ASIX SIGMA only supports "
                                        "rising/falling trigger in 100 "
                                        "and 200MHz mode.");
                                return SR_ERR;
@@ -653,7 +662,7 @@ static int configure_probes(struct sr_device_instance *sdi, GSList *probes)
                          * does not permit ORed triggers.
                          */
                        if (trigger_set > 1) {
-                               sr_warn("Asix Sigma only supports 1 rising/"
+                               sr_warn("ASIX SIGMA only supports 1 rising/"
                                        "falling triggers.");
                                return SR_ERR;
                        }
@@ -666,19 +675,28 @@ static int configure_probes(struct sr_device_instance *sdi, GSList *probes)
        return SR_OK;
 }
 
-static void hw_closedev(int device_index)
+static int hw_closedev(int device_index)
 {
        struct sr_device_instance *sdi;
        struct sigma *sigma;
 
-       if ((sdi = sr_get_device_instance(device_instances, device_index)))
-       {
-               sigma = sdi->priv;
-               if (sdi->status == SR_ST_ACTIVE)
-                       ftdi_usb_close(&sigma->ftdic);
+       if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
+               sr_err("sigma: %s: sdi was NULL", __func__);
+               return SR_ERR; /* TODO: SR_ERR_ARG? */
+       }
 
-               sdi->status = SR_ST_INACTIVE;
+       if (!(sigma = sdi->priv)) {
+               sr_err("sigma: %s: sdi->priv was NULL", __func__);
+               return SR_ERR; /* TODO: SR_ERR_ARG? */
        }
+
+       /* TODO */
+       if (sdi->status == SR_ST_ACTIVE)
+               ftdi_usb_close(&sigma->ftdic);
+
+       sdi->status = SR_ST_INACTIVE;
+
+       return SR_OK;
 }
 
 static void hw_cleanup(void)
@@ -1353,17 +1371,17 @@ static void hw_stop_acquisition(int device_index, gpointer session_device_id)
 }
 
 struct sr_device_plugin asix_sigma_plugin_info = {
-       "asix-sigma",
-       "ASIX SIGMA",
-       1,
-       hw_init,
-       hw_cleanup,
-       hw_opendev,
-       hw_closedev,
-       hw_get_device_info,
-       hw_get_status,
-       hw_get_capabilities,
-       hw_set_configuration,
-       hw_start_acquisition,
-       hw_stop_acquisition,
+       .name = "asix-sigma",
+       .longname = "ASIX SIGMA",
+       .api_version = 1,
+       .init = hw_init,
+       .cleanup = hw_cleanup,
+       .opendev = hw_opendev,
+       .closedev = hw_closedev,
+       .get_device_info = hw_get_device_info,
+       .get_status = hw_get_status,
+       .get_capabilities = hw_get_capabilities,
+       .set_configuration = hw_set_configuration,
+       .start_acquisition = hw_start_acquisition,
+       .stop_acquisition = hw_stop_acquisition,
 };