]> sigrok.org Git - libsigrok.git/blobdiff - hardware/genericdmm/api.c
sr/drivers: change driver dev_acquisition_start/_stop calls to use sdi
[libsigrok.git] / hardware / genericdmm / api.c
index b744bc18beae4c5c3c249415c2478a31478843d6..e4de064ddc2647ead7ce2fb3fa389ffc633a3ccb 100644 (file)
@@ -41,14 +41,18 @@ static struct dev_profile dev_profiles[] = {
        { NULL, NULL, NULL, NULL, 0, NULL }
 };
 
+static const int hwopts[] = {
+       SR_HWOPT_MODEL,
+       SR_HWOPT_CONN,
+       SR_HWOPT_SERIALCOMM,
+       0,
+};
+
 static const int hwcaps[] = {
        SR_HWCAP_MULTIMETER,
        SR_HWCAP_LIMIT_SAMPLES,
        SR_HWCAP_LIMIT_MSEC,
        SR_HWCAP_CONTINUOUS,
-       SR_HWOPT_MODEL,
-       SR_HWOPT_CONN,
-       SR_HWOPT_SERIALCOMM,
        0,
 };
 
@@ -146,7 +150,7 @@ static GSList *connect_usb(const char *conn)
 
                devcnt = g_slist_length(gdi->instances);
                if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_ACTIVE,
-                               "Generic DMM", NULL, NULL))) {
+                               NULL, NULL, NULL))) {
                        sr_err("genericdmm: sr_dev_inst_new returned NULL.");
                        return NULL;
                }
@@ -187,6 +191,31 @@ GSList *genericdmm_connect(const char *conn, const char *serialcomm)
        return NULL;
 }
 
+static GSList *default_scan(GSList *options)
+{
+       GSList *l, *devices;
+       struct sr_hwopt *opt;
+       const char *conn, *serialcomm;
+
+       devices = NULL;
+       conn = serialcomm = NULL;
+       for (l = options; l; l = l->next) {
+               opt = l->data;
+               switch (opt->hwopt) {
+               case SR_HWOPT_CONN:
+                       conn = opt->value;
+                       break;
+               case SR_HWOPT_SERIALCOMM:
+                       serialcomm = opt->value;
+                       break;
+               }
+       }
+       if (conn)
+               devices = genericdmm_connect(conn, serialcomm);
+
+       return devices;
+}
+
 static int hw_init(void)
 {
 
@@ -199,40 +228,107 @@ static int hw_init(void)
        return SR_OK;
 }
 
-static int hw_scan(void)
+static GSList *hw_scan(GSList *options)
 {
+       GSList *l, *ldef, *defopts, *newopts, *devices;
+       struct sr_hwopt *opt, *defopt;
+       struct dev_profile *pr, *profile;
        struct sr_dev_inst *sdi;
-       struct context *ctx;
-       int devcnt = 0;
+       const char *model;
+
+       /* Separate model from the options list. */
+       model = NULL;
+       newopts = NULL;
+       for (l = options; l; l = l->next) {
+               opt = l->data;
+               if (opt->hwopt == SR_HWOPT_MODEL)
+                       model = opt->value;
+               else
+                       /* New list with references to the original data. */
+                       newopts = g_slist_append(newopts, opt);
+       }
+       if (!model) {
+               /* This driver only works when a model is specified. */
+               return NULL;
+       }
 
-       if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
-               sr_err("genericdmm: ctx malloc failed.");
-               return 0;
+       /* Find a profile with this model name. */
+       profile = NULL;
+       for (pr = dev_profiles; pr->modelid; pr++) {
+               if (!strcmp(pr->modelid, model)) {
+                       profile = pr;
+                       break;
+               }
+       }
+       if (!profile) {
+               sr_err("Unknown model %s.", model);
+               return NULL;
        }
 
-       devcnt = g_slist_length(genericdmm_dev_insts);
-       if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_ACTIVE, "Generic DMM",
-                       NULL, NULL))) {
-               sr_err("genericdmm: sr_dev_inst_new returned NULL.");
-               return 0;
+       /* Initialize the DMM chip driver. */
+       if (profile->chip->init)
+               profile->chip->init();
+
+       /* Convert the profile's default options list to a GSList. */
+       defopts = NULL;
+       for (opt = profile->defaults_opts; opt->hwopt; opt++) {
+               /* New list with references to const data in the profile. */
+               defopts = g_slist_append(defopts, opt);
        }
-       sdi->priv = ctx;
-       genericdmm_dev_insts = g_slist_append(genericdmm_dev_insts, sdi);
 
-       /* Always initialized just one device instance. */
-       return 0;
+       /* Options given as argument to this function override the
+        * profile's default options.
+        */
+       for (ldef = defopts; ldef; ldef = ldef->next) {
+               defopt = ldef->data;
+               for (l = newopts; l; l = l->next) {
+                       opt = l->data;
+                       if (opt->hwopt == defopt->hwopt) {
+                               /* Override the default, and drop it from the
+                                * options list.
+                                */
+                               ldef->data = l->data;
+                               newopts = g_slist_remove(newopts, opt);
+                               break;
+                       }
+               }
+       }
+       /* Whatever is left in newopts wasn't in the default options. */
+       defopts = g_slist_concat(defopts, newopts);
+       g_slist_free(newopts);
+
+       if (profile->chip->scan)
+               /* The DMM chip driver wants to do its own scanning. */
+               devices = profile->chip->scan(defopts);
+       else
+               devices = default_scan(defopts);
+       g_slist_free(defopts);
+
+       if (devices) {
+               /* TODO: need to fix up sdi->index fields */
+               for (l = devices; l; l = l->next) {
+                       /* The default connection-based scanner doesn't really
+                        * know about profiles, so it never filled in the vendor
+                        * or model. Do that now.
+                        */
+                       sdi = l->data;
+                       sdi->driver = gdi;
+                       if (!sdi->vendor)
+                               sdi->vendor = g_strdup(profile->vendor);
+                       if (!sdi->model)
+                               sdi->model = g_strdup(profile->model);
+                       /* Add a copy of these new devices to the driver instances. */
+                       gdi->instances = g_slist_append(gdi->instances, l->data);
+               }
+       }
+
+       return devices;
 }
 
-static int hw_dev_open(int dev_index)
+static int hw_dev_open(struct sr_dev_inst *sdi)
 {
-       struct sr_dev_inst *sdi;
        struct context *ctx;
 
-       if (!(sdi = sr_dev_inst_get(gdi->instances, dev_index))) {
-               sr_err("genericdmm: sdi was NULL.");
-               return SR_ERR_BUG;
-       }
-
        if (!(ctx = sdi->priv)) {
                sr_err("genericdmm: sdi->priv was NULL.");
                return SR_ERR_BUG;
@@ -261,23 +357,15 @@ static int hw_dev_open(int dev_index)
        return SR_OK;
 }
 
-static int hw_dev_close(int dev_index)
+static int hw_dev_close(struct sr_dev_inst *sdi)
 {
-       struct sr_dev_inst *sdi;
        struct context *ctx;
 
-       if (!(sdi = sr_dev_inst_get(gdi->instances, dev_index))) {
-               sr_err("genericdmm: %s: sdi was NULL.", __func__);
-               return SR_ERR_BUG;
-       }
-
        if (!(ctx = sdi->priv)) {
                sr_err("genericdmm: %s: sdi->priv was NULL.", __func__);
                return SR_ERR_BUG;
        }
 
-       /* TODO: Check for != NULL. */
-
        switch (ctx->profile->transport) {
        case DMM_TRANSPORT_USBHID:
                /* TODO */
@@ -338,51 +426,38 @@ static int hw_cleanup(void)
        return SR_OK;
 }
 
-static const void *hw_dev_info_get(int dev_index, int dev_info_id)
+static int hw_info_get(int dev_info_id, const void **data,
+               const struct sr_dev_inst *sdi)
 {
-       struct sr_dev_inst *sdi;
        struct context *ctx;
-       const void *info;
-
-       if (!(sdi = sr_dev_inst_get(gdi->instances, dev_index))) {
-               sr_err("genericdmm: sdi was NULL.");
-               return NULL;
-       }
-
-       if (!(ctx = sdi->priv)) {
-               sr_err("genericdmm: sdi->priv was NULL.");
-               return NULL;
-       }
-
-               sr_spew("genericdmm: dev_index %d, dev_info_id %d.",
-                               dev_index, dev_info_id);
 
        switch (dev_info_id) {
        case SR_DI_INST:
-               info = sdi;
+               *data = sdi;
                sr_spew("genericdmm: Returning sdi.");
                break;
+       case SR_DI_HWOPTS:
+               *data = hwopts;
+               break;
+       case SR_DI_HWCAPS:
+               *data = hwcaps;
+               break;
        case SR_DI_NUM_PROBES:
-               info = GINT_TO_POINTER(1);
-               sr_spew("genericdmm: Returning number of probes: 1.");
+               *data = GINT_TO_POINTER(1);
                break;
        case SR_DI_PROBE_NAMES:
-               info = probe_names;
-               sr_spew("genericdmm: Returning probenames.");
+               *data = probe_names;
                break;
        case SR_DI_CUR_SAMPLERATE:
                /* TODO get rid of this */
-               info = NULL;
-               sr_spew("genericdmm: Returning samplerate: 0.");
+               *data = NULL;
                break;
        default:
                /* Unknown device info ID. */
-               sr_err("genericdmm: Unknown device info ID: %d.", dev_info_id);
-               info = NULL;
-               break;
+               return SR_ERR_ARG;
        }
 
-       return info;
+       return SR_OK;
 }
 
 static int hw_dev_status_get(int dev_index)
@@ -399,30 +474,16 @@ static int hw_dev_status_get(int dev_index)
        return sdi->status;
 }
 
-static const int *hw_hwcap_get_all(void)
+static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
+               const void *value)
 {
-       sr_spew("genericdmm: Returning list of device capabilities.");
-
-       return hwcaps;
-}
-
-static int hw_dev_config_set(int dev_index, int hwcap, const void *value)
-{
-       struct sr_dev_inst *sdi;
        struct context *ctx;
 
-       if (!(sdi = sr_dev_inst_get(gdi->instances, dev_index))) {
-               sr_err("genericdmm: sdi was NULL.");
-               return SR_ERR_BUG;
-       }
-
        if (!(ctx = sdi->priv)) {
                sr_err("genericdmm: sdi->priv was NULL.");
                return SR_ERR_BUG;
        }
 
-       sr_spew("genericdmm: dev_index %d, hwcap %d.", dev_index, hwcap);
-
        switch (hwcap) {
        case SR_HWCAP_LIMIT_MSEC:
                if (*(const uint64_t *)value == 0) {
@@ -475,19 +536,14 @@ static int receive_data(int fd, int revents, void *cb_data)
        return TRUE;
 }
 
-static int hw_dev_acquisition_start(int dev_index, void *cb_data)
+static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
+               void *cb_data)
 {
        struct sr_datafeed_packet packet;
        struct sr_datafeed_header header;
        struct sr_datafeed_meta_analog meta;
-       struct sr_dev_inst *sdi;
        struct context *ctx;
 
-       if (!(sdi = sr_dev_inst_get(gdi->instances, dev_index))) {
-               sr_err("genericdmm: sdi was NULL.");
-               return SR_ERR_BUG;
-       }
-
        if (!(ctx = sdi->priv)) {
                sr_err("genericdmm: sdi->priv was NULL.");
                return SR_ERR_BUG;
@@ -526,12 +582,13 @@ static int hw_dev_acquisition_start(int dev_index, void *cb_data)
        return SR_OK;
 }
 
-static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
+static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
+               void *cb_data)
 {
        struct sr_datafeed_packet packet;
 
        /* Avoid compiler warnings. */
-       (void)dev_index;
+       (void)sdi;
 
        sr_dbg("genericdmm: Stopping acquisition.");
 
@@ -552,9 +609,8 @@ SR_PRIV struct sr_dev_driver genericdmm_driver_info = {
        .scan = hw_scan,
        .dev_open = hw_dev_open,
        .dev_close = hw_dev_close,
-       .dev_info_get = hw_dev_info_get,
+       .info_get = hw_info_get,
        .dev_status_get = hw_dev_status_get,
-       .hwcap_get_all = hw_hwcap_get_all,
        .dev_config_set = hw_dev_config_set,
        .dev_acquisition_start = hw_dev_acquisition_start,
        .dev_acquisition_stop = hw_dev_acquisition_stop,