]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/ikalogic-scanalogic2/api.c
Put driver pointers into special section
[libsigrok.git] / src / hardware / ikalogic-scanalogic2 / api.c
index 25cebb5facc83398b36638a479b8a8dae54a2983..c30c605c88444a71da4bbad89083c4fe9092419f 100644 (file)
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <config.h>
 #include "protocol.h"
 
-static const uint32_t hwcaps[] = {
+static const uint32_t devopts[] = {
        SR_CONF_LOGIC_ANALYZER,
-       SR_CONF_SAMPLERATE,
-       SR_CONF_LIMIT_SAMPLES,
-       SR_CONF_TRIGGER_MATCH,
-       SR_CONF_CAPTURE_RATIO,
+       SR_CONF_LIMIT_SAMPLES | SR_CONF_SET | SR_CONF_LIST,
+       SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
+       SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
+       SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET,
 };
 
 static const int32_t trigger_matches[] = {
@@ -47,57 +48,42 @@ SR_PRIV const uint64_t sl2_samplerates[NUM_SAMPLERATES] = {
        SR_MHZ(20),
 };
 
-static const char *channel_names[NUM_CHANNELS + 1] = {
+static const char *channel_names[] = {
        "0", "1", "2", "3",
-       NULL,
 };
 
-SR_PRIV struct sr_dev_driver ikalogic_scanalogic2_driver_info;
-static struct sr_dev_driver *di = &ikalogic_scanalogic2_driver_info;
-
-static int init(struct sr_context *sr_ctx)
-{
-       return std_init(sr_ctx, di, LOG_PREFIX);
-}
-
-static GSList *scan(GSList *options)
+static GSList *scan(struct sr_dev_driver *di, GSList *options)
 {
        GSList *usb_devices, *devices, *l;
        struct drv_context *drvc;
        struct sr_dev_inst *sdi;
-       struct sr_channel *ch;
        struct dev_context *devc;
        struct sr_usb_dev_inst *usb;
        struct device_info dev_info;
-       int ret, device_index, i;
-       char *fw_ver_str;
+       unsigned int i;
+       int ret;
 
        (void)options;
 
        devices = NULL;
-       drvc = di->priv;
+       drvc = di->context;
        drvc->instances = NULL;
-       device_index = 0;
 
        usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, USB_VID_PID);
 
-       if (usb_devices == NULL)
+       if (!usb_devices)
                return NULL;
 
        for (l = usb_devices; l; l = l->next) {
                usb = l->data;
 
-               if ((ret = sl2_get_device_info(*usb, &dev_info)) < 0) {
+               if ((ret = sl2_get_device_info(di, *usb, &dev_info)) < 0) {
                        sr_warn("Failed to get device information: %d.", ret);
                        sr_usb_dev_inst_free(usb);
                        continue;
                }
 
-               if (!(devc = g_try_malloc(sizeof(struct dev_context)))) {
-                       sr_err("Device instance malloc failed.");
-                       sr_usb_dev_inst_free(usb);
-                       continue;
-               }
+               devc = g_malloc0(sizeof(struct dev_context));
 
                if (!(devc->xfer_in = libusb_alloc_transfer(0))) {
                        sr_err("Transfer malloc failed.");
@@ -114,40 +100,20 @@ static GSList *scan(GSList *options)
                        continue;
                }
 
-               fw_ver_str = g_strdup_printf("%u.%u", dev_info.fw_ver_major,
-                       dev_info.fw_ver_minor);
-               if (!fw_ver_str) {
-                       sr_err("Firmware string malloc failed.");
-                       sr_usb_dev_inst_free(usb);
-                       libusb_free_transfer(devc->xfer_in);
-                       libusb_free_transfer(devc->xfer_out);
-                       g_free(devc);
-                       continue;
-               }
-
-               sdi = sr_dev_inst_new(device_index, SR_ST_INACTIVE, VENDOR_NAME,
-                       MODEL_NAME, fw_ver_str);
-               g_free(fw_ver_str);
-               if (!sdi) {
-                       sr_err("sr_dev_inst_new failed.");
-                       sr_usb_dev_inst_free(usb);
-                       libusb_free_transfer(devc->xfer_in);
-                       libusb_free_transfer(devc->xfer_out);
-                       g_free(devc);
-                       continue;
-               }
-
+               sdi = g_malloc0(sizeof(struct sr_dev_inst));
+               sdi->status = SR_ST_INACTIVE;
+               sdi->vendor = g_strdup(VENDOR_NAME);
+               sdi->model = g_strdup(MODEL_NAME);
+               sdi->version = g_strdup_printf("%u.%u", dev_info.fw_ver_major, dev_info.fw_ver_minor);
+               sdi->serial_num = g_strdup_printf("%d", dev_info.serial);
                sdi->priv = devc;
                sdi->driver = di;
                sdi->inst_type = SR_INST_USB;
                sdi->conn = usb;
 
-               for (i = 0; channel_names[i]; i++) {
-                       ch = sr_channel_new(i, SR_CHANNEL_LOGIC, TRUE,
-                               channel_names[i]);
-                       sdi->channels = g_slist_append(sdi->channels, ch);
-                       devc->channels[i] = ch;
-               }
+               for (i = 0; i < ARRAY_SIZE(channel_names); i++)
+                       devc->channels[i] = sr_channel_new(sdi, i,
+                               SR_CHANNEL_LOGIC, TRUE, channel_names[i]);
 
                devc->state = STATE_IDLE;
                devc->next_state = STATE_IDLE;
@@ -182,8 +148,6 @@ static GSList *scan(GSList *options)
 
                drvc->instances = g_slist_append(drvc->instances, sdi);
                devices = g_slist_append(devices, sdi);
-
-               device_index++;
        }
 
        g_slist_free(usb_devices);
@@ -191,11 +155,6 @@ static GSList *scan(GSList *options)
        return devices;
 }
 
-static GSList *dev_list(void)
-{
-       return ((struct drv_context *)(di->priv))->instances;
-}
-
 static void clear_dev_context(void *priv)
 {
        struct dev_context *devc;
@@ -209,24 +168,20 @@ static void clear_dev_context(void *priv)
        g_free(devc);
 }
 
-static int dev_clear(void)
+static int dev_clear(const struct sr_dev_driver *di)
 {
        return std_dev_clear(di, &clear_dev_context);
 }
 
 static int dev_open(struct sr_dev_inst *sdi)
 {
-       struct drv_context *drvc;
+       struct sr_dev_driver *di = sdi->driver;
+       struct drv_context *drvc = di->context;
        struct dev_context *devc;
        struct sr_usb_dev_inst *usb;
        uint8_t buffer[PACKET_LENGTH];
        int ret;
 
-       if (!(drvc = di->priv)) {
-               sr_err("Driver was not initialized.");
-               return SR_ERR;
-       }
-
        usb = sdi->conn;
        devc = sdi->priv;
 
@@ -254,11 +209,11 @@ static int dev_open(struct sr_dev_inst *sdi)
 
        libusb_fill_control_transfer(devc->xfer_in, usb->devhdl,
                devc->xfer_buf_in, sl2_receive_transfer_in,
-               sdi, USB_TIMEOUT);
+               sdi, USB_TIMEOUT_MS);
 
        libusb_fill_control_transfer(devc->xfer_out, usb->devhdl,
                devc->xfer_buf_out, sl2_receive_transfer_out,
-               sdi, USB_TIMEOUT);
+               sdi, USB_TIMEOUT_MS);
 
        memset(buffer, 0, sizeof(buffer));
 
@@ -289,11 +244,6 @@ static int dev_close(struct sr_dev_inst *sdi)
 {
        struct sr_usb_dev_inst *usb;
 
-       if (!di->priv) {
-               sr_err("Driver was not initialized.");
-               return SR_ERR;
-       }
-
        usb = sdi->conn;
 
        if (!usb->devhdl)
@@ -308,11 +258,6 @@ static int dev_close(struct sr_dev_inst *sdi)
        return SR_OK;
 }
 
-static int cleanup(void)
-{
-       return dev_clear();
-}
-
 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
                const struct sr_channel_group *cg)
 {
@@ -349,8 +294,6 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd
        if (sdi->status != SR_ST_ACTIVE)
                return SR_ERR_DEV_CLOSED;
 
-       ret = SR_OK;
-
        switch (key) {
        case SR_CONF_LIMIT_SAMPLES:
                limit_samples = g_variant_get_uint64(data);
@@ -382,11 +325,10 @@ static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *
        (void)cg;
 
        ret = SR_OK;
-
        switch (key) {
        case SR_CONF_DEVICE_OPTIONS:
                *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
-                       hwcaps, ARRAY_SIZE(hwcaps), sizeof(uint32_t));
+                               devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
                break;
        case SR_CONF_SAMPLERATE:
                g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
@@ -413,8 +355,9 @@ static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *
        return ret;
 }
 
-static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
+static int dev_acquisition_start(const struct sr_dev_inst *sdi)
 {
+       struct sr_dev_driver *di = sdi->driver;
        struct drv_context *drvc;
        struct dev_context *devc;
        uint16_t trigger_bytes, tmp;
@@ -425,9 +368,8 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
                return SR_ERR_DEV_CLOSED;
 
        devc = sdi->priv;
-       drvc = di->priv;
+       drvc = di->context;
 
-       devc->cb_data = cb_data;
        devc->wait_data_ready_locked = TRUE;
        devc->stopping_in_progress = FALSE;
        devc->transfer_error = FALSE;
@@ -495,18 +437,15 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
 
        sr_dbg("Acquisition started successfully.");
 
-       /* Send header packet to the session bus. */
-       std_session_send_df_header(cb_data, LOG_PREFIX);
+       std_session_send_df_header(sdi, LOG_PREFIX);
 
        devc->next_state = STATE_SAMPLE;
 
        return SR_OK;
 }
 
-static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
+static int dev_acquisition_stop(struct sr_dev_inst *sdi)
 {
-       (void)cb_data;
-
        if (sdi->status != SR_ST_ACTIVE)
                return SR_ERR_DEV_CLOSED;
 
@@ -517,14 +456,14 @@ static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
        return SR_OK;
 }
 
-SR_PRIV struct sr_dev_driver ikalogic_scanalogic2_driver_info = {
+static struct sr_dev_driver ikalogic_scanalogic2_driver_info = {
        .name = "ikalogic-scanalogic2",
        .longname = "IKALOGIC Scanalogic-2",
        .api_version = 1,
-       .init = init,
-       .cleanup = cleanup,
+       .init = std_init,
+       .cleanup = std_cleanup,
        .scan = scan,
-       .dev_list = dev_list,
+       .dev_list = std_dev_list,
        .dev_clear = dev_clear,
        .config_get = config_get,
        .config_set = config_set,
@@ -533,5 +472,6 @@ SR_PRIV struct sr_dev_driver ikalogic_scanalogic2_driver_info = {
        .dev_close = dev_close,
        .dev_acquisition_start = dev_acquisition_start,
        .dev_acquisition_stop = dev_acquisition_stop,
-       .priv = NULL,
+       .context = NULL,
 };
+SR_REGISTER_DEV_DRIVER(ikalogic_scanalogic2_driver_info);