]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/sysclk-lwla/api.c
baylibre-acme: Add SR_CONF_POWERMETER key.
[libsigrok.git] / src / hardware / sysclk-lwla / api.c
index 8ee85a62be0c08aa0ec214798fdce4757c52e71d..1ff70eeb8dce3eba523dc9793963f9bf0c6e14c8 100644 (file)
@@ -61,13 +61,6 @@ static const char *const signal_edge_names[] = {
        [EDGE_NEGATIVE] = "f",
 };
 
-/* Initialize the SysClk LWLA driver.
- */
-static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
-{
-       return std_init(sr_ctx, di, LOG_PREFIX);
-}
-
 /* Create a new sigrok device instance for the indicated LWLA model.
  */
 static struct sr_dev_inst *dev_inst_new(const struct model_info *model)
@@ -201,46 +194,13 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
                        continue; /* no match */
 
                /* Register device instance with driver. */
-               sdi->driver = di;
-               drvc->instances = g_slist_append(drvc->instances, sdi);
                devices = g_slist_append(devices, sdi);
        }
 
        libusb_free_device_list(devlist, 1);
        g_slist_free_full(conn_devices, (GDestroyNotify)&sr_usb_dev_inst_free);
 
-       return devices;
-}
-
-/* Return the list of devices found during scan.
- */
-static GSList *dev_list(const struct sr_dev_driver *di)
-{
-       return ((struct drv_context *)(di->context))->instances;
-}
-
-/* Destroy the private device context.
- */
-static void clear_dev_context(void *priv)
-{
-       struct dev_context *devc;
-
-       devc = priv;
-
-       if (devc->acquisition) {
-               sr_err("Cannot clear device context during acquisition!");
-               return; /* Leak and pray. */
-       }
-       sr_dbg("Device context cleared.");
-
-       g_free(devc);
-}
-
-/* Destroy all device instances.
- */
-static int dev_clear(const struct sr_dev_driver *di)
-{
-       return std_dev_clear(di, &clear_dev_context);
+       return std_scan_complete(di, devices);
 }
 
 /* Drain any pending data from the USB transfer buffers on the device.
@@ -289,15 +249,6 @@ static int dev_open(struct sr_dev_inst *sdi)
        devc = sdi->priv;
        usb = sdi->conn;
 
-       if (!drvc) {
-               sr_err("Driver was not initialized.");
-               return SR_ERR;
-       }
-       if (sdi->status != SR_ST_INACTIVE) {
-               sr_err("Device already open.");
-               return SR_ERR;
-       }
-
        /* Try the whole shebang three times, fingers crossed. */
        for (i = 0; i < 3; i++) {
                ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb);
@@ -328,8 +279,6 @@ static int dev_open(struct sr_dev_inst *sdi)
                /* This delay appears to be necessary for reliable operation. */
                g_usleep(30 * 1000);
 
-               sdi->status = SR_ST_ACTIVE;
-
                devc->active_fpga_config = FPGA_NOCONF;
                devc->short_transfer_quirk = FALSE;
                devc->state = STATE_IDLE;
@@ -342,7 +291,6 @@ static int dev_open(struct sr_dev_inst *sdi)
                        break;
 
                /* Rinse and repeat. */
-               sdi->status = SR_ST_INACTIVE;
                sr_usb_close(usb);
        }
 
@@ -352,27 +300,16 @@ static int dev_open(struct sr_dev_inst *sdi)
        return ret;
 }
 
-/* Shutdown and close device.
- */
+/* Shutdown and close device. */
 static int dev_close(struct sr_dev_inst *sdi)
 {
-       struct drv_context *drvc;
        struct dev_context *devc;
        struct sr_usb_dev_inst *usb;
        int ret;
 
-       drvc = sdi->driver->context;
        devc = sdi->priv;
        usb = sdi->conn;
 
-       if (!drvc) {
-               sr_err("Driver was not initialized.");
-               return SR_ERR;
-       }
-       if (sdi->status == SR_ST_INACTIVE) {
-               sr_dbg("Device already closed.");
-               return SR_OK;
-       }
        if (devc->acquisition) {
                sr_err("Cannot close device during acquisition!");
                /* Request stop, leak handle, and prepare for the worst. */
@@ -380,17 +317,16 @@ static int dev_close(struct sr_dev_inst *sdi)
                return SR_ERR_BUG;
        }
 
-       sdi->status = SR_ST_INACTIVE;
-
        /* Download of the shutdown bitstream, if any. */
        ret = (*devc->model->apply_fpga_config)(sdi);
        if (ret != SR_OK)
                sr_warn("Unable to shut down device.");
 
        libusb_release_interface(usb->devhdl, USB_INTERFACE);
+
        sr_usb_close(usb);
 
-       return ret;
+       return SR_OK;
 }
 
 /* Check whether the device options contain a specific key.
@@ -671,9 +607,6 @@ static int config_commit(const struct sr_dev_inst *sdi)
 
        devc = sdi->priv;
 
-       if (sdi->status != SR_ST_ACTIVE)
-               return SR_ERR_DEV_CLOSED;
-
        if (devc->acquisition) {
                sr_err("Acquisition still in progress?");
                return SR_ERR;
@@ -692,8 +625,6 @@ static int config_commit(const struct sr_dev_inst *sdi)
        return SR_OK;
 }
 
-/* List available choices for a configuration setting.
- */
 static int config_list(uint32_t key, GVariant **data,
                       const struct sr_dev_inst *sdi,
                       const struct sr_channel_group *cg)
@@ -702,31 +633,16 @@ static int config_list(uint32_t key, GVariant **data,
        GVariant *gvar;
        GVariantBuilder gvb;
 
-       (void)cg;
-
-       if (key == SR_CONF_SCAN_OPTIONS) {
-               *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
-                       scanopts, ARRAY_SIZE(scanopts), sizeof(scanopts[0]));
-               return SR_OK;
-       }
-       if (!sdi) {
-               if (key != SR_CONF_DEVICE_OPTIONS)
-                       return SR_ERR_ARG;
-
-               /* List driver capabilities. */
-               *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
-                       drvopts, ARRAY_SIZE(drvopts), sizeof(drvopts[0]));
-               return SR_OK;
-       }
-
-       devc = sdi->priv;
+       devc = (sdi) ? sdi->priv : NULL;
 
-       /* List the model's device options. */
-       if (key == SR_CONF_DEVICE_OPTIONS) {
-               *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
-                       devc->model->devopts, devc->model->num_devopts,
-                       sizeof(devc->model->devopts[0]));
-               return SR_OK;
+       switch (key) {
+       case SR_CONF_SCAN_OPTIONS:
+       case SR_CONF_DEVICE_OPTIONS:
+               return std_opts_config_list(key, data, sdi, cg,
+                       scanopts, ARRAY_SIZE(scanopts),
+                       drvopts, ARRAY_SIZE(drvopts),
+                       (devc) ? devc->model->devopts : NULL,
+                       (devc) ? devc->model->num_devopts : 0);
        }
 
        if (!has_devopt(devc->model, key | SR_CONF_LIST))
@@ -769,11 +685,6 @@ static int config_list(uint32_t key, GVariant **data,
  */
 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
 {
-       if (sdi->status != SR_ST_ACTIVE)
-               return SR_ERR_DEV_CLOSED;
-
-       sr_info("Starting acquisition.");
-
        return lwla_start_acquisition(sdi);
 }
 
@@ -785,12 +696,9 @@ static int dev_acquisition_stop(struct sr_dev_inst *sdi)
 
        devc = sdi->priv;
 
-       if (sdi->status != SR_ST_ACTIVE)
-               return SR_ERR_DEV_CLOSED;
-
        if (devc->state != STATE_IDLE && !devc->cancel_requested) {
                devc->cancel_requested = TRUE;
-               sr_dbg("Stopping acquisition.");
+               sr_dbg("Requesting cancel.");
        }
 
        return SR_OK;
@@ -798,15 +706,15 @@ static int dev_acquisition_stop(struct sr_dev_inst *sdi)
 
 /* SysClk LWLA driver descriptor.
  */
-SR_PRIV struct sr_dev_driver sysclk_lwla_driver_info = {
+static struct sr_dev_driver sysclk_lwla_driver_info = {
        .name = "sysclk-lwla",
        .longname = "SysClk LWLA series",
        .api_version = 1,
-       .init = init,
-       .cleanup = dev_clear,
+       .init = std_init,
+       .cleanup = std_cleanup,
        .scan = scan,
-       .dev_list = dev_list,
-       .dev_clear = dev_clear,
+       .dev_list = std_dev_list,
+       .dev_clear = std_dev_clear,
        .config_get = config_get,
        .config_set = config_set,
        .config_channel_set = config_channel_set,
@@ -818,3 +726,4 @@ SR_PRIV struct sr_dev_driver sysclk_lwla_driver_info = {
        .dev_acquisition_stop = dev_acquisition_stop,
        .context = NULL,
 };
+SR_REGISTER_DEV_DRIVER(sysclk_lwla_driver_info);