]> sigrok.org Git - libsigrok.git/blobdiff - hardware/asix-sigma/asix-sigma.c
Fix some compiler warnings.
[libsigrok.git] / hardware / asix-sigma / asix-sigma.c
index 2722d0db10fd5a2339e2bef2eab0b02c8cd31740..e7d6f7833103bd608a4a29344bbec7ec2ad9f4ea 100644 (file)
@@ -20,7 +20,7 @@
  */
 
 /*
- * ASIX Sigma Logic Analyzer Driver
+ * ASIX SIGMA Logic Analyzer Driver
  */
 
 #include "config.h"
@@ -98,7 +98,7 @@ static const char *firmware_files[] = {
        "asix-sigma-phasor.fw", /* Frequency counter */
 };
 
-static void hw_stop_acquisition(int device_index, gpointer session_device_id);
+static void hw_stop_acquisition(int device_index, gpointer session_data);
 
 static int sigma_read(void *buf, size_t size, struct sigma *sigma)
 {
@@ -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);
 
@@ -406,6 +415,7 @@ static int hw_init(const char *deviceinfo)
                goto free;
 
        sigma->cur_samplerate = 0;
+       sigma->period_ps = 0;
        sigma->limit_msec = 0;
        sigma->cur_firmware = -1;
        sigma->num_probes = 0;
@@ -428,7 +438,7 @@ static int hw_init(const char *deviceinfo)
 
        return 1;
 free:
-       free(sigma);
+       g_free(sigma);
        return 0;
 }
 
@@ -484,10 +494,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. */
@@ -576,6 +586,7 @@ static int set_samplerate(struct sr_device_instance *sdi,
        }
 
        sigma->cur_samplerate = samplerate;
+       sigma->period_ps = 1000000000000 / samplerate;
        sigma->samples_per_event = 16 / sigma->num_probes;
        sigma->state.state = SIGMA_IDLE;
 
@@ -612,7 +623,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 +632,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 +664,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 +677,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)
@@ -823,13 +843,14 @@ static int get_trigger_offset(uint16_t *samples, uint16_t last_sample,
  */
 static int decode_chunk_ts(uint8_t *buf, uint16_t *lastts,
                           uint16_t *lastsample, int triggerpos,
-                          uint16_t limit_chunk, void *user_data)
+                          uint16_t limit_chunk, void *session_data)
 {
-       struct sr_device_instance *sdi = user_data;
+       struct sr_device_instance *sdi = session_data;
        struct sigma *sigma = sdi->priv;
        uint16_t tsdiff, ts;
        uint16_t samples[65536 * sigma->samples_per_event];
        struct sr_datafeed_packet packet;
+       struct sr_datafeed_logic logic;
        int i, j, k, l, numpad, tosend;
        size_t n = 0, sent = 0;
        int clustersize = EVENTS_PER_CLUSTER * sigma->samples_per_event;
@@ -874,9 +895,13 @@ static int decode_chunk_ts(uint8_t *buf, uint16_t *lastts,
                        tosend = MIN(2048, n - sent);
 
                        packet.type = SR_DF_LOGIC;
-                       packet.length = tosend * sizeof(uint16_t);
-                       packet.unitsize = 2;
-                       packet.payload = samples + sent;
+                       /* TODO: fill in timeoffset and duration */
+                       packet.timeoffset = 0;
+                       packet.duration = 0;
+                       packet.payload = &logic;
+                       logic.length = tosend * sizeof(uint16_t);
+                       logic.unitsize = 2;
+                       logic.data = samples + sent;
                        sr_session_bus(sigma->session_id, &packet);
 
                        sent += tosend;
@@ -918,9 +943,13 @@ static int decode_chunk_ts(uint8_t *buf, uint16_t *lastts,
 
                        if (tosend > 0) {
                                packet.type = SR_DF_LOGIC;
-                               packet.length = tosend * sizeof(uint16_t);
-                               packet.unitsize = 2;
-                               packet.payload = samples;
+                               /* TODO: fill in timeoffset and duration */
+                               packet.timeoffset = 0;
+                               packet.duration = 0;
+                               packet.payload = &logic;
+                               logic.length = tosend * sizeof(uint16_t);
+                               logic.unitsize = 2;
+                               logic.data = samples;
                                sr_session_bus(sigma->session_id, &packet);
 
                                sent += tosend;
@@ -929,8 +958,9 @@ static int decode_chunk_ts(uint8_t *buf, uint16_t *lastts,
                        /* Only send trigger if explicitly enabled. */
                        if (sigma->use_triggers) {
                                packet.type = SR_DF_TRIGGER;
-                               packet.length = 0;
-                               packet.payload = 0;
+                               /* TODO: fill in timeoffset only */
+                               packet.timeoffset = 0;
+                               packet.duration = 0;
                                sr_session_bus(sigma->session_id, &packet);
                        }
                }
@@ -940,9 +970,13 @@ static int decode_chunk_ts(uint8_t *buf, uint16_t *lastts,
 
                if (tosend > 0) {
                        packet.type = SR_DF_LOGIC;
-                       packet.length = tosend * sizeof(uint16_t);
-                       packet.unitsize = 2;
-                       packet.payload = samples + sent;
+                       /* TODO: fill in timeoffset and duration */
+                       packet.timeoffset = 0;
+                       packet.duration = 0;
+                       packet.payload = &logic;
+                       logic.length = tosend * sizeof(uint16_t);
+                       logic.unitsize = 2;
+                       logic.data = samples + sent;
                        sr_session_bus(sigma->session_id, &packet);
                }
 
@@ -952,9 +986,9 @@ static int decode_chunk_ts(uint8_t *buf, uint16_t *lastts,
        return SR_OK;
 }
 
-static int receive_data(int fd, int revents, void *user_data)
+static int receive_data(int fd, int revents, void *session_data)
 {
-       struct sr_device_instance *sdi = user_data;
+       struct sr_device_instance *sdi = session_data;
        struct sigma *sigma = sdi->priv;
        struct sr_datafeed_packet packet;
        const int chunks_per_read = 32;
@@ -981,7 +1015,7 @@ static int receive_data(int fd, int revents, void *user_data)
                if (running_msec < sigma->limit_msec && numchunks < 32767)
                        return FALSE;
 
-               hw_stop_acquisition(sdi->index, user_data);
+               hw_stop_acquisition(sdi->index, session_data);
 
                return FALSE;
 
@@ -989,7 +1023,6 @@ static int receive_data(int fd, int revents, void *user_data)
                if (sigma->state.chunks_downloaded >= numchunks) {
                        /* End of samples. */
                        packet.type = SR_DF_END;
-                       packet.length = 0;
                        sr_session_bus(sigma->session_id, &packet);
 
                        sigma->state.state = SIGMA_IDLE;
@@ -1005,6 +1038,8 @@ static int receive_data(int fd, int revents, void *user_data)
 
                bufsz = sigma_read_dram(sigma->state.chunks_downloaded,
                                        newchunks, buf, sigma);
+               /* TODO: Check bufsz. For now, just avoid compiler warnings. */
+               (void)bufsz;
 
                /* Find first ts. */
                if (sigma->state.chunks_downloaded == 0) {
@@ -1028,12 +1063,12 @@ static int receive_data(int fd, int revents, void *user_data)
                                                &sigma->state.lastts,
                                                &sigma->state.lastsample,
                                                sigma->state.triggerpos & 0x1ff,
-                                               limit_chunk, user_data);
+                                               limit_chunk, session_data);
                        else
                                decode_chunk_ts(buf + (i * CHUNK_SIZE),
                                                &sigma->state.lastts,
                                                &sigma->state.lastsample,
-                                               -1, limit_chunk, user_data);
+                                               -1, limit_chunk, session_data);
 
                        ++sigma->state.chunks_downloaded;
                }
@@ -1198,7 +1233,7 @@ static int build_basic_trigger(struct triggerlut *lut, struct sigma *sigma)
        return SR_OK;
 }
 
-static int hw_start_acquisition(int device_index, gpointer session_device_id)
+static int hw_start_acquisition(int device_index, gpointer session_data)
 {
        struct sr_device_instance *sdi;
        struct sigma *sigma;
@@ -1210,7 +1245,7 @@ static int hw_start_acquisition(int device_index, gpointer session_device_id)
        struct triggerinout triggerinout_conf;
        struct triggerlut lut;
 
-       session_device_id = session_device_id;
+       session_data = session_data;
 
        if (!(sdi = sr_get_device_instance(device_instances, device_index)))
                return SR_ERR;
@@ -1295,19 +1330,17 @@ static int hw_start_acquisition(int device_index, gpointer session_device_id)
        gettimeofday(&sigma->start_tv, 0);
        sigma_set_register(WRITE_MODE, 0x0d, sigma);
 
-       sigma->session_id = session_device_id;
+       sigma->session_id = session_data;
 
        /* Send header packet to the session bus. */
        packet.type = SR_DF_HEADER;
-       packet.length = sizeof(struct sr_datafeed_header);
        packet.payload = &header;
        header.feed_version = 1;
        gettimeofday(&header.starttime, NULL);
        header.samplerate = sigma->cur_samplerate;
-       header.protocol_id = SR_PROTO_RAW;
        header.num_logic_probes = sigma->num_probes;
        header.num_analog_probes = 0;
-       sr_session_bus(session_device_id, &packet);
+       sr_session_bus(session_data, &packet);
 
        /* Add capture source. */
        sr_source_add(0, G_IO_IN, 10, receive_data, sdi);
@@ -1317,7 +1350,7 @@ static int hw_start_acquisition(int device_index, gpointer session_device_id)
        return SR_OK;
 }
 
-static void hw_stop_acquisition(int device_index, gpointer session_device_id)
+static void hw_stop_acquisition(int device_index, gpointer session_data)
 {
        struct sr_device_instance *sdi;
        struct sigma *sigma;
@@ -1328,7 +1361,7 @@ static void hw_stop_acquisition(int device_index, gpointer session_device_id)
 
        sigma = sdi->priv;
 
-       session_device_id = session_device_id;
+       session_data = session_data;
 
        /* Stop acquisition. */
        sigma_set_register(WRITE_MODE, 0x11, sigma);
@@ -1353,17 +1386,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,
 };