]> sigrok.org Git - libsigrok.git/blobdiff - hardware/link-mso19/link-mso19.c
Shorten probe_names[] arrays everywhere.
[libsigrok.git] / hardware / link-mso19 / link-mso19.c
index 5eaf9bd31d23f22d34ff6c6fbba9a47ef138cb5b..40e8d9e3ed802456cce81902494cb8be7465ee63 100644 (file)
@@ -28,8 +28,8 @@
 #include <glib.h>
 #include <libudev.h>
 #include <arpa/inet.h>
-#include "sigrok.h"
-#include "sigrok-internal.h"
+#include "libsigrok.h"
+#include "libsigrok-internal.h"
 #include "link-mso19.h"
 
 #define USB_VENDOR "3195"
@@ -37,7 +37,7 @@
 
 #define NUM_PROBES 8
 
-static int hwcaps[] = {
+static const int hwcaps[] = {
        SR_HWCAP_LOGIC_ANALYZER,
 //     SR_HWCAP_OSCILLOSCOPE,
 //     SR_HWCAP_PAT_GENERATOR,
@@ -54,18 +54,11 @@ static int hwcaps[] = {
  * See also: http://www.linkinstruments.com/images/mso19_1113.gif
  */
 static const char *probe_names[NUM_PROBES + 1] = {
-       "0",
-       "1",
-       "2",
-       "3",
-       "4",
-       "5",
-       "6",
-       "7",
+       "0", "1", "2", "3", "4", "5", "6", "7",
        NULL,
 };
 
-static uint64_t supported_samplerates[] = {
+static const uint64_t supported_samplerates[] = {
        SR_HZ(100),
        SR_HZ(200),
        SR_HZ(500),
@@ -89,10 +82,10 @@ static uint64_t supported_samplerates[] = {
        0,
 };
 
-static struct sr_samplerates samplerates = {
-       SR_HZ(100),
-       SR_MHZ(200),
-       SR_HZ(0),
+static const struct sr_samplerates samplerates = {
+       0,
+       0,
+       0,
        supported_samplerates,
 };
 
@@ -111,7 +104,7 @@ static int mso_send_control_message(struct sr_dev_inst *sdi,
                goto ret;
 
        if (!(buf = g_try_malloc(s))) {
-               sr_err("mso19: %s: buf malloc failed", __func__);
+               sr_err("Failed to malloc message buffer.");
                ret = SR_ERR_MALLOC;
                goto ret;
        }
@@ -151,7 +144,7 @@ static int mso_reset_adc(struct sr_dev_inst *sdi)
        ops[1] = mso_trans(REG_CTL1, ctx->ctlbase1);
        ctx->ctlbase1 |= BIT_CTL1_ADC_UNKNOWN4;
 
-       sr_dbg("mso19: Requesting ADC reset");
+       sr_dbg("Requesting ADC reset.");
        return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
 }
 
@@ -163,7 +156,7 @@ static int mso_reset_fsm(struct sr_dev_inst *sdi)
        ctx->ctlbase1 |= BIT_CTL1_RESETFSM;
        ops[0] = mso_trans(REG_CTL1, ctx->ctlbase1);
 
-       sr_dbg("mso19: Requesting ADC reset");
+       sr_dbg("Requesting ADC reset.");
        return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
 }
 
@@ -177,7 +170,7 @@ static int mso_toggle_led(struct sr_dev_inst *sdi, int state)
                ctx->ctlbase1 |= BIT_CTL1_LED;
        ops[0] = mso_trans(REG_CTL1, ctx->ctlbase1);
 
-       sr_dbg("mso19: Requesting LED toggle");
+       sr_dbg("Requesting LED toggle.");
        return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
 }
 
@@ -187,7 +180,7 @@ static int mso_check_trigger(struct sr_dev_inst *sdi, uint8_t *info)
        char buf[1];
        int ret;
 
-       sr_dbg("mso19: Requesting trigger state");
+       sr_dbg("Requesting trigger state.");
        ret = mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
        if (info == NULL || ret != SR_OK)
                return ret;
@@ -197,7 +190,7 @@ static int mso_check_trigger(struct sr_dev_inst *sdi, uint8_t *info)
                ret = SR_ERR;
        *info = buf[0];
 
-       sr_dbg("mso19: Trigger state is: 0x%x", *info);
+       sr_dbg("Trigger state is: 0x%x.", *info);
        return ret;
 }
 
@@ -205,7 +198,7 @@ static int mso_read_buffer(struct sr_dev_inst *sdi)
 {
        uint16_t ops[] = { mso_trans(REG_BUFFER, 0) };
 
-       sr_dbg("mso19: Requesting buffer dump");
+       sr_dbg("Requesting buffer dump.");
        return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
 }
 
@@ -218,7 +211,7 @@ static int mso_arm(struct sr_dev_inst *sdi)
                mso_trans(REG_CTL1, ctx->ctlbase1),
        };
 
-       sr_dbg("mso19: Requesting trigger arm");
+       sr_dbg("Requesting trigger arm.");
        return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
 }
 
@@ -230,7 +223,7 @@ static int mso_force_capture(struct sr_dev_inst *sdi)
                mso_trans(REG_CTL1, ctx->ctlbase1),
        };
 
-       sr_dbg("mso19: Requesting forced capture");
+       sr_dbg("Requesting forced capture.");
        return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
 }
 
@@ -243,7 +236,7 @@ static int mso_dac_out(struct sr_dev_inst *sdi, uint16_t val)
                mso_trans(REG_CTL1, ctx->ctlbase1 | BIT_CTL1_RESETADC),
        };
 
-       sr_dbg("mso19: Setting dac word to 0x%x", val);
+       sr_dbg("Setting dac word to 0x%x.", val);
        return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
 }
 
@@ -254,7 +247,7 @@ static int mso_clkrate_out(struct sr_dev_inst *sdi, uint16_t val)
                mso_trans(REG_CLKRATE2, val & 0xff),
        };
 
-       sr_dbg("mso19: Setting clkrate word to 0x%x", val);
+       sr_dbg("Setting clkrate word to 0x%x.", val);
        return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
 }
 
@@ -401,7 +394,14 @@ static int mso_parse_serial(const char *iSerial, const char *iProduct,
        return SR_OK;
 }
 
-static int hw_init(const char *devinfo)
+static int hw_init(struct sr_context *sr_ctx)
+{
+       /* Nothing to do. */
+
+       return SR_OK;
+}
+
+static int hw_scan(void)
 {
        struct sr_dev_inst *sdi;
        int devcnt = 0;
@@ -410,8 +410,6 @@ static int hw_init(const char *devinfo)
        struct udev_list_entry *devs, *dev_list_entry;
        struct context *ctx;
 
-       devinfo = devinfo;
-
        /* It's easier to map usb<->serial using udev */
        /*
         * FIXME: On windows we can get the same information from the
@@ -419,7 +417,7 @@ static int hw_init(const char *devinfo)
         */
        udev = udev_new();
        if (!udev) {
-               sr_err("mso19: Failed to initialize udev.");
+               sr_err("Failed to initialize udev.");
                goto ret;
        }
        enumerate = udev_enumerate_new(udev);
@@ -439,7 +437,7 @@ static int hw_init(const char *devinfo)
                parent = udev_device_get_parent_with_subsystem_devtype(
                                dev, "usb", "usb_device");
                if (!parent) {
-                       sr_err("mso19: Unable to find parent usb device for %s",
+                       sr_err("Unable to find parent usb device for %s",
                               sysname);
                        continue;
                }
@@ -458,7 +456,7 @@ static int hw_init(const char *devinfo)
                s = strcspn(iProduct, " ");
                if (s > sizeof(product) ||
                                strlen(iProduct) - s > sizeof(manufacturer)) {
-                       sr_err("mso19: Could not parse iProduct: %s", iProduct);
+                       sr_err("Could not parse iProduct: %s.", iProduct);
                        continue;
                }
                strncpy(product, iProduct, s);
@@ -466,12 +464,12 @@ static int hw_init(const char *devinfo)
                strcpy(manufacturer, iProduct + s);
 
                if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
-                       sr_err("mso19: %s: ctx malloc failed", __func__);
+                       sr_err("Context malloc failed.");
                        continue; /* TODO: Errors handled correctly? */
                }
 
                if (mso_parse_serial(iSerial, iProduct, ctx) != SR_OK) {
-                       sr_err("mso19: Invalid iSerial: %s", iSerial);
+                       sr_err("Invalid iSerial: %s.", iSerial);
                        goto err_free_ctx;
                }
                sprintf(hwrev, "r%d", ctx->hwrev);
@@ -491,7 +489,7 @@ static int hw_init(const char *devinfo)
                sdi = sr_dev_inst_new(devcnt, SR_ST_INITIALIZING,
                                      manufacturer, product, hwrev);
                if (!sdi) {
-                       sr_err("mso19: Unable to create device instance for %s",
+                       sr_err("Unable to create device instance for %s",
                               sysname);
                        goto err_free_ctx;
                }
@@ -531,7 +529,7 @@ static int hw_cleanup(void)
        for (l = dev_insts; l; l = l->next) {
                if (!(sdi = l->data)) {
                        /* Log error, but continue cleaning up the rest. */
-                       sr_err("mso19: %s: sdi was NULL, continuing", __func__);
+                       sr_err("%s: sdi was NULL, continuing", __func__);
                        ret = SR_ERR_BUG;
                        continue;
                }
@@ -555,7 +553,7 @@ static int hw_dev_open(int dev_index)
                return ret;
 
        ctx = sdi->priv;
-       sdi->serial->fd = serial_open(sdi->serial->port, O_RDWR);
+       sdi->serial->fd = serial_open(sdi->serial->port, SERIAL_RDWR);
        if (sdi->serial->fd == -1)
                return ret;
 
@@ -568,21 +566,19 @@ static int hw_dev_open(int dev_index)
        /* FIXME: discard serial buffer */
 
        mso_check_trigger(sdi, &ctx->trigger_state);
-       sr_dbg("mso19: trigger state: 0x%x", ctx->trigger_state);
+       sr_dbg("Trigger state: 0x%x.", ctx->trigger_state);
 
        ret = mso_reset_adc(sdi);
        if (ret != SR_OK)
                return ret;
 
        mso_check_trigger(sdi, &ctx->trigger_state);
-       sr_dbg("mso19: trigger state: 0x%x", ctx->trigger_state);
+       sr_dbg("Trigger state: 0x%x.", ctx->trigger_state);
 
 //     ret = mso_reset_fsm(sdi);
 //     if (ret != SR_OK)
 //             return ret;
 
-       sr_dbg("mso19: Finished %s", __func__);
-
 //     return SR_ERR;
        return SR_OK;
 }
@@ -592,8 +588,8 @@ static int hw_dev_close(int dev_index)
        struct sr_dev_inst *sdi;
 
        if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
-               sr_err("mso19: %s: sdi was NULL", __func__);
-               return SR_ERR; /* TODO: SR_ERR_ARG? */
+               sr_err("%s: sdi was NULL", __func__);
+               return SR_ERR_BUG;
        }
 
        /* TODO */
@@ -603,15 +599,14 @@ static int hw_dev_close(int dev_index)
                sdi->status = SR_ST_INACTIVE;
        }
 
-       sr_dbg("mso19: finished %s", __func__);
        return SR_OK;
 }
 
-static void *hw_dev_info_get(int dev_index, int dev_info_id)
+static const void *hw_dev_info_get(int dev_index, int dev_info_id)
 {
        struct sr_dev_inst *sdi;
        struct context *ctx;
-       void *info = NULL;
+       const void *info = NULL;
 
        if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
                return NULL;
@@ -650,12 +645,12 @@ static int hw_dev_status_get(int dev_index)
        return sdi->status;
 }
 
-static int *hw_hwcap_get_all(void)
+static const int *hw_hwcap_get_all(void)
 {
        return hwcaps;
 }
 
-static int hw_dev_config_set(int dev_index, int hwcap, void *value)
+static int hw_dev_config_set(int dev_index, int hwcap, const void *value)
 {
        struct sr_dev_inst *sdi;
 
@@ -664,7 +659,7 @@ static int hw_dev_config_set(int dev_index, int hwcap, void *value)
 
        switch (hwcap) {
        case SR_HWCAP_SAMPLERATE:
-               return mso_configure_rate(sdi, *(uint64_t *) value);
+               return mso_configure_rate(sdi, *(const uint64_t *) value);
        case SR_HWCAP_PROBECONFIG:
        case SR_HWCAP_LIMIT_SAMPLES:
        default:
@@ -681,9 +676,9 @@ static int hw_dev_config_set(int dev_index, int hwcap, void *value)
 #define MSO_TRIGGER_DATAREADY  '6'
 
 /* FIXME: Pass errors? */
-static int receive_data(int fd, int revents, void *user_data)
+static int receive_data(int fd, int revents, void *cb_data)
 {
-       struct sr_dev_inst *sdi = user_data;
+       struct sr_dev_inst *sdi = cb_data;
        struct context *ctx = sdi->priv;
        struct sr_datafeed_packet packet;
        struct sr_datafeed_logic logic;
@@ -691,7 +686,7 @@ static int receive_data(int fd, int revents, void *user_data)
        double analog_out[1024];
        size_t i, s;
 
-       revents = revents;
+       (void)revents;
 
        s = serial_read(fd, in, sizeof(in));
        if (s <= 0)
@@ -731,7 +726,7 @@ static int receive_data(int fd, int revents, void *user_data)
        logic.length = 1024;
        logic.unitsize = 1;
        logic.data = logic_out;
-       sr_session_bus(ctx->session_id, &packet);
+       sr_session_send(ctx->session_dev_id, &packet);
 
        // Dont bother fixing this yet, keep it "old style"
        /*
@@ -739,16 +734,16 @@ static int receive_data(int fd, int revents, void *user_data)
        packet.length = 1024;
        packet.unitsize = sizeof(double);
        packet.payload = analog_out;
-       sr_session_bus(ctx->session_id, &packet);
+       sr_session_send(ctx->session_dev_id, &packet);
        */
 
        packet.type = SR_DF_END;
-       sr_session_bus(ctx->session_id, &packet);
+       sr_session_send(ctx->session_dev_id, &packet);
 
        return TRUE;
 }
 
-static int hw_dev_acquisition_start(int dev_index, gpointer session_dev_id)
+static int hw_dev_acquisition_start(int dev_index, void *cb_data)
 {
        struct sr_dev_inst *sdi;
        struct context *ctx;
@@ -806,7 +801,7 @@ static int hw_dev_acquisition_start(int dev_index, gpointer session_dev_id)
        if (ret != SR_OK)
                return ret;
 
-       ctx->session_id = session_dev_id;
+       ctx->session_dev_id = cb_data;
        sr_source_add(sdi->serial->fd, G_IO_IN, -1, receive_data, sdi);
 
        packet.type = SR_DF_HEADER;
@@ -816,30 +811,31 @@ static int hw_dev_acquisition_start(int dev_index, gpointer session_dev_id)
        header.samplerate = ctx->cur_rate;
        // header.num_analog_probes = 1;
        header.num_logic_probes = 8;
-       sr_session_bus(session_dev_id, &packet);
+       sr_session_send(ctx->session_dev_id, &packet);
 
        return ret;
 }
 
-/* FIXME */
-static int hw_dev_acquisition_stop(int dev_index, gpointer session_dev_id)
+/* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
+static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
 {
        struct sr_datafeed_packet packet;
 
-       dev_index = dev_index;
+       (void)dev_index;
 
        packet.type = SR_DF_END;
-       sr_session_bus(session_dev_id, &packet);
+       sr_session_send(cb_data, &packet);
 
        return SR_OK;
 }
 
-SR_PRIV struct sr_dev_plugin link_mso19_plugin_info = {
+SR_PRIV struct sr_dev_driver link_mso19_driver_info = {
        .name = "link-mso19",
        .longname = "Link Instruments MSO-19",
        .api_version = 1,
        .init = hw_init,
        .cleanup = hw_cleanup,
+       .scan = hw_scan,
        .dev_open = hw_dev_open,
        .dev_close = hw_dev_close,
        .dev_info_get = hw_dev_info_get,