]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/lascar-el-usb/api.c
drivers: Provide proper drvopts.
[libsigrok.git] / src / hardware / lascar-el-usb / api.c
index 04f844b3318b1d575a4eec0a8f23708e59a162a9..f91755af32c1c7ec323e135567d98150841a5f41 100644 (file)
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <config.h>
 #include <glib.h>
 #include <libusb.h>
-#include "libsigrok.h"
+#include <libsigrok/libsigrok.h>
 #include "libsigrok-internal.h"
 #include "protocol.h"
 
-SR_PRIV struct sr_dev_driver lascar_el_usb_driver_info;
-static struct sr_dev_driver *di = &lascar_el_usb_driver_info;
-
-static const int32_t hwopts[] = {
+static const uint32_t scanopts[] = {
        SR_CONF_CONN,
 };
 
-static const int32_t hwcaps[] = {
+static const uint32_t drvopts[] = {
        SR_CONF_THERMOMETER,
        SR_CONF_HYGROMETER,
-       SR_CONF_DATALOG,
-       SR_CONF_LIMIT_SAMPLES,
 };
 
-static int init(struct sr_context *sr_ctx)
-{
-       return std_init(sr_ctx, di, LOG_PREFIX);
-}
+static const uint32_t devopts[] = {
+       SR_CONF_CONN | SR_CONF_GET,
+       SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
+       SR_CONF_DATALOG | SR_CONF_GET | SR_CONF_SET,
+};
 
-static GSList *scan(GSList *options)
+static GSList *scan(struct sr_dev_driver *di, GSList *options)
 {
        struct drv_context *drvc;
        struct sr_dev_inst *sdi;
@@ -51,7 +48,7 @@ static GSList *scan(GSList *options)
        GSList *usb_devices, *devices, *l;
        const char *conn;
 
-       drvc = di->priv;
+       drvc = di->context;
 
        conn = NULL;
        for (l = options; l; l = l->next) {
@@ -78,32 +75,22 @@ static GSList *scan(GSList *options)
                        }
                        sdi->inst_type = SR_INST_USB;
                        sdi->conn = usb;
-                       drvc->instances = g_slist_append(drvc->instances, sdi);
                        devices = g_slist_append(devices, sdi);
                }
                g_slist_free(usb_devices);
        } else
                g_slist_free_full(usb_devices, g_free);
 
-       return devices;
-}
-
-static GSList *dev_list(void)
-{
-       return ((struct drv_context *)(di->priv))->instances;
+       return std_scan_complete(di, devices);
 }
 
 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 sr_usb_dev_inst *usb;
        int ret;
 
-       if (!(drvc = di->priv)) {
-               sr_err("Driver was not initialized.");
-               return SR_ERR;
-       }
-
        usb = sdi->conn;
 
        if (sr_usb_open(drvc->sr_ctx->libusb_ctx, usb) != SR_OK)
@@ -113,52 +100,27 @@ static int dev_open(struct sr_dev_inst *sdi)
                sr_err("Failed to claim interface: %s.", libusb_error_name(ret));
                return SR_ERR;
        }
-       sdi->status = SR_ST_ACTIVE;
 
-       return ret;
+       return SR_OK;
 }
 
 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)
-               /*  Nothing to do. */
-               return SR_OK;
+               return SR_ERR_BUG;
 
        libusb_release_interface(usb->devhdl, LASCAR_INTERFACE);
        libusb_close(usb->devhdl);
        usb->devhdl = NULL;
-       sdi->status = SR_ST_INACTIVE;
 
        return SR_OK;
 }
 
-static int cleanup(void)
-{
-       int ret;
-       struct drv_context *drvc;
-
-       if (!(drvc = di->priv))
-               /* Can get called on an unused driver, doesn't matter. */
-               return SR_OK;
-
-
-       ret = std_dev_clear(di, NULL);
-       g_free(drvc);
-       di->priv = NULL;
-
-       return ret;
-}
-
-static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi,
+static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
                const struct sr_channel_group *cg)
 {
        struct dev_context *devc;
@@ -169,7 +131,7 @@ static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi,
        (void)cg;
 
        devc = sdi->priv;
-       switch (id) {
+       switch (key) {
        case SR_CONF_CONN:
                if (!sdi || !sdi->conn)
                        return SR_ERR_ARG;
@@ -194,7 +156,7 @@ static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi,
        return SR_OK;
 }
 
-static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
+static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
                const struct sr_channel_group *cg)
 {
        struct dev_context *devc;
@@ -202,30 +164,17 @@ static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
 
        (void)cg;
 
-       if (sdi->status != SR_ST_ACTIVE)
-               return SR_ERR_DEV_CLOSED;
-
-       if (!di->priv) {
-               sr_err("Driver was not initialized.");
-               return SR_ERR;
-       }
-
        devc = sdi->priv;
        ret = SR_OK;
-       switch (id) {
+       switch (key) {
        case SR_CONF_DATALOG:
-               if (g_variant_get_boolean(data)) {
-                       /* Start logging. */
+               if (g_variant_get_boolean(data))
                        ret = lascar_start_logging(sdi);
-               } else {
-                       /* Stop logging. */
+               else
                        ret = lascar_stop_logging(sdi);
-               }
                break;
        case SR_CONF_LIMIT_SAMPLES:
                devc->limit_samples = g_variant_get_uint64(data);
-               sr_dbg("Setting sample limit to %" PRIu64 ".",
-                      devc->limit_samples);
                break;
        default:
                ret = SR_ERR_NA;
@@ -234,29 +183,13 @@ static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
        return ret;
 }
 
-static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
+static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
                const struct sr_channel_group *cg)
 {
-       (void)sdi;
-       (void)cg;
-
-       switch (key) {
-       case SR_CONF_SCAN_OPTIONS:
-               *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
-                               hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
-               break;
-       case SR_CONF_DEVICE_OPTIONS:
-               *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
-                               hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
-               break;
-       default:
-               return SR_ERR_NA;
-       }
-
-       return SR_OK;
+       return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
 }
 
-static void mark_xfer(struct libusb_transfer *xfer)
+static void LIBUSB_CALL mark_xfer(struct libusb_transfer *xfer)
 {
 
        if (xfer->status == LIBUSB_TRANSFER_COMPLETED)
@@ -304,11 +237,11 @@ static int lascar_proc_config(const struct sr_dev_inst *sdi)
                devc->temp_unit = devc->config[0x2e] | (devc->config[0x2f] << 8);
                if (devc->temp_unit != 0 && devc->temp_unit != 1) {
                        sr_dbg("invalid temperature unit %d", devc->temp_unit);
-                       /* Default to Celcius, we're all adults here. */
+                       /* Default to Celsius, we're all adults here. */
                        devc->temp_unit = 0;
                } else
                        sr_dbg("temperature unit is %s", devc->temp_unit
-                                       ? "Fahrenheit" : "Celcius");
+                                       ? "Fahrenheit" : "Celsius");
                break;
        case LOG_CO:
                devc->sample_size = 2;
@@ -326,8 +259,9 @@ static int lascar_proc_config(const struct sr_dev_inst *sdi)
        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 sr_datafeed_packet packet;
        struct sr_datafeed_meta meta;
        struct sr_config *src;
@@ -340,39 +274,28 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
        int ret;
        unsigned char cmd[3], resp[4], *buf;
 
-       if (sdi->status != SR_ST_ACTIVE)
-               return SR_ERR_DEV_CLOSED;
-
-       if (!di->priv) {
-               sr_err("Driver was not initialized.");
-               return SR_ERR;
-       }
-
-       drvc = di->priv;
+       drvc = di->context;
        devc = sdi->priv;
        usb = sdi->conn;
-       devc->cb_data = cb_data;
 
        if (lascar_proc_config(sdi) != SR_OK)
                return SR_ERR;
 
        sr_dbg("Starting log retrieval.");
 
-       /* Send header packet to the session bus. */
-       std_session_send_df_header(cb_data, LOG_PREFIX);
+       std_session_send_df_header(sdi);
 
        interval = (devc->config[0x1c] | (devc->config[0x1d] << 8)) * 1000;
        packet.type = SR_DF_META;
        packet.payload = &meta;
        src = sr_config_new(SR_CONF_SAMPLE_INTERVAL, g_variant_new_uint64(interval));
        meta.config = g_slist_append(NULL, src);
-       sr_session_send(devc->cb_data, &packet);
+       sr_session_send(sdi, &packet);
        g_free(src);
 
        if (devc->logged_samples == 0) {
                /* This ensures the frontend knows the session is done. */
-               packet.type = SR_DF_END;
-               sr_session_send(devc->cb_data, &packet);
+               std_session_send_df_end(sdi);
                return SR_OK;
        }
 
@@ -387,14 +310,13 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
        libusb_control_transfer(usb->devhdl, LIBUSB_REQUEST_TYPE_VENDOR,
                        0x02, 0x0001, 0x00, NULL, 0, 50);
 
-
        /* Flush input. The F321 requires this. */
        while (libusb_bulk_transfer(usb->devhdl, LASCAR_EP_IN, resp,
                        256, &ret, 5) == 0 && ret > 0)
                ;
 
        libusb_fill_bulk_transfer(xfer_in, usb->devhdl, LASCAR_EP_IN,
-                       resp, sizeof(resp), mark_xfer, 0, 10000);
+                       resp, sizeof(resp), mark_xfer, 0, BULK_XFER_TIMEOUT);
        if (libusb_submit_transfer(xfer_in) != 0) {
                libusb_free_transfer(xfer_in);
                libusb_free_transfer(xfer_out);
@@ -415,11 +337,11 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
        tv.tv_sec = 0;
        tv.tv_usec = 0;
        while (!xfer_in->user_data || !xfer_out->user_data) {
-               g_usleep(5000);
+               g_usleep(SLEEP_US_LONG);
                libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
        }
        if (xfer_in->user_data != GINT_TO_POINTER(1) ||
-                       xfer_in->user_data != GINT_TO_POINTER(1)) {
+                       xfer_out->user_data != GINT_TO_POINTER(1)) {
                sr_dbg("no response to log transfer request");
                libusb_free_transfer(xfer_in);
                libusb_free_transfer(xfer_out);
@@ -437,9 +359,10 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
        usb_source_add(sdi->session, drvc->sr_ctx, 100,
                        lascar_el_usb_handle_events, (void *)sdi);
 
-       buf = g_try_malloc(4096);
+       buf = g_malloc(4096);
        libusb_fill_bulk_transfer(xfer_in, usb->devhdl, LASCAR_EP_IN,
-                       buf, 4096, lascar_el_usb_receive_transfer, cb_data, 100);
+                       buf, 4096, lascar_el_usb_receive_transfer,
+                       (struct sr_dev_inst *)sdi, 100);
        if ((ret = libusb_submit_transfer(xfer_in) != 0)) {
                sr_err("Unable to submit transfer: %s.", libusb_error_name(ret));
                libusb_free_transfer(xfer_in);
@@ -450,20 +373,8 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
        return SR_OK;
 }
 
-SR_PRIV 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 (!di->priv) {
-               sr_err("Driver was not initialized.");
-               return SR_ERR;
-       }
-
-       if (sdi->status != SR_ST_ACTIVE) {
-               sr_err("Device inactive, can't stop acquisition.");
-               return SR_ERR;
-       }
-
        sdi->status = SR_ST_STOPPING;
        /* TODO: free ongoing transfers? */
 
@@ -474,11 +385,11 @@ SR_PRIV struct sr_dev_driver lascar_el_usb_driver_info = {
        .name = "lascar-el-usb",
        .longname = "Lascar EL-USB",
        .api_version = 1,
-       .init = init,
-       .cleanup = cleanup,
+       .init = std_init,
+       .cleanup = std_cleanup,
        .scan = scan,
-       .dev_list = dev_list,
-       .dev_clear = NULL,
+       .dev_list = std_dev_list,
+       .dev_clear = std_dev_clear,
        .config_get = config_get,
        .config_set = config_set,
        .config_list = config_list,
@@ -486,5 +397,6 @@ SR_PRIV struct sr_dev_driver lascar_el_usb_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(lascar_el_usb_driver_info);