]> sigrok.org Git - libsigrok.git/commitdiff
lascar-el-usb: generic EL-USB support + EL-USB-CO support
authorBert Vermeulen <redacted>
Sat, 15 Dec 2012 03:06:51 +0000 (04:06 +0100)
committerBert Vermeulen <redacted>
Sat, 15 Dec 2012 03:07:25 +0000 (04:07 +0100)
hardware/lascar-el-usb/api.c
hardware/lascar-el-usb/protocol.c
hardware/lascar-el-usb/protocol.h

index aa506fa66b475d4813a3f13986a24d19b1cb5e33..68a06eb1513f33300b5e8895d8f89bdeefd8f8e9 100644 (file)
@@ -27,7 +27,6 @@ SR_PRIV struct sr_dev_inst *lascar_scan(int bus, int address);
 SR_PRIV struct sr_dev_driver lascar_el_usb_driver_info;
 static struct sr_dev_driver *di = &lascar_el_usb_driver_info;
 static int hw_dev_close(struct sr_dev_inst *sdi);
-static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data);
 
 static const int hwopts[] = {
        SR_HWOPT_CONN,
@@ -95,7 +94,6 @@ static GSList *hw_scan(GSList *options)
        struct dev_context *devc;
        struct sr_dev_inst *sdi;
        struct sr_usb_dev_inst *usb;
-       struct sr_probe *probe;
        struct sr_hwopt *opt;
        GSList *usb_devices, *devices, *l;
        const char *conn;
@@ -133,15 +131,8 @@ static GSList *hw_scan(GSList *options)
                                g_free(usb);
                                continue;
                        }
-                       if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
-                               sr_err("Device context malloc failed.");
-                               return NULL;
-                       }
+                       devc = sdi->priv;
                        devc->usb = usb;
-                       sdi->priv = devc;
-                       if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
-                               return NULL;
-                       sdi->probes = g_slist_append(sdi->probes, probe);
                        drvc->instances = g_slist_append(drvc->instances, sdi);
                        devices = g_slist_append(devices, sdi);
                }
@@ -156,30 +147,73 @@ static GSList *hw_dev_list(void)
 {
        struct drv_context *drvc;
 
-       drvc = di->priv;
+       if (!(drvc = di->priv)) {
+               sr_err("Driver was not initialized.");
+               return NULL;
+       }
 
        return drvc->instances;
 }
 
 static int hw_dev_open(struct sr_dev_inst *sdi)
 {
-       /* TODO */
+       struct drv_context *drvc;
+       struct dev_context *devc;
+       int ret;
 
-       return SR_OK;
+       if (!(drvc = di->priv)) {
+               sr_err("Driver was not initialized.");
+               return SR_ERR;
+       }
+
+       devc = sdi->priv;
+       if (sr_usb_open(drvc->sr_ctx->libusb_ctx, devc->usb) != SR_OK)
+               return SR_ERR;
+
+       if ((ret = libusb_claim_interface(devc->usb->devhdl, LASCAR_INTERFACE))) {
+               sr_err("Failed to claim interface: %s.", libusb_error_name(ret));
+               return SR_ERR;
+       }
+       sdi->status = SR_ST_ACTIVE;
+
+       return ret;
 }
 
 static int hw_dev_close(struct sr_dev_inst *sdi)
 {
-       /* TODO */
+       struct dev_context *devc;
+
+       if (!di->priv) {
+               sr_err("Driver was not initialized.");
+               return SR_ERR;
+       }
+
+       devc = sdi->priv;
+       if (!devc->usb->devhdl)
+               /*  Nothing to do. */
+               return SR_OK;
+
+       libusb_release_interface(devc->usb->devhdl, LASCAR_INTERFACE);
+       libusb_close(devc->usb->devhdl);
+       devc->usb->devhdl = NULL;
+       g_free(devc->config);
+       sdi->status = SR_ST_INACTIVE;
+
 
        return SR_OK;
 }
 
 static int hw_cleanup(void)
 {
-       clear_instances();
+       struct drv_context *drvc;
 
-       /* TODO */
+       if (!(drvc = di->priv))
+               /* Can get called on an unused driver, doesn't matter. */
+               return SR_OK;
+
+       clear_instances();
+       g_free(drvc);
+       di->priv = NULL;
 
        return SR_OK;
 }
@@ -187,8 +221,22 @@ static int hw_cleanup(void)
 static int hw_info_get(int info_id, const void **data,
                const struct sr_dev_inst *sdi)
 {
+
+       (void)sdi;
+
        switch (info_id) {
-       /* TODO */
+       case SR_DI_HWOPTS:
+               *data = hwopts;
+               break;
+       case SR_DI_HWCAPS:
+               *data = hwcaps;
+               break;
+       case SR_DI_NUM_PROBES:
+               *data = GINT_TO_POINTER(1);
+               break;
+       case SR_DI_PROBE_NAMES:
+               *data = probe_names;
+               break;
        default:
                sr_err("Unknown info_id: %d.", info_id);
                return SR_ERR_ARG;
@@ -200,16 +248,26 @@ static int hw_info_get(int info_id, const void **data,
 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
                const void *value)
 {
+       struct dev_context *devc;
        int ret;
 
+       if (!di->priv) {
+               sr_err("Driver was not initialized.");
+               return SR_ERR;
+       }
        if (sdi->status != SR_ST_ACTIVE) {
                sr_err("Device inactive, can't set config options.");
                return SR_ERR;
        }
 
+       devc = sdi->priv;
        ret = SR_OK;
        switch (hwcap) {
-       /* TODO */
+       case SR_HWCAP_LIMIT_SAMPLES:
+               devc->limit_samples = *(const uint64_t *)value;
+               sr_dbg("Setting sample limit to %" PRIu64 ".",
+                      devc->limit_samples);
+               break;
        default:
                sr_err("Unknown hardware capability: %d.", hwcap);
                ret = SR_ERR_ARG;
@@ -218,24 +276,209 @@ static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
        return ret;
 }
 
+static void mark_xfer(struct libusb_transfer *xfer)
+{
+
+       if (xfer->status == LIBUSB_TRANSFER_COMPLETED)
+               xfer->user_data = GINT_TO_POINTER(1);
+       else
+               xfer->user_data = GINT_TO_POINTER(-1);
+
+}
+
+/* The Lascar software, in its infinite ignorance, reads a set of four
+ * bytes from the device config struct and interprets it as a float.
+ * That only works because they only use windows, and only on x86. However
+ * we may be running on any architecture, any operating system. So we have
+ * to convert these four bytes as the Lascar software would on windows/x86,
+ * to the local representation of a float.
+ * The source format is little-endian, with IEEE 754-2008 BINARY32 encoding. */
+static float binary32_le_to_float(unsigned char *buf)
+{
+       GFloatIEEE754 f;
+
+       f.v_float = 0;
+       f.mpn.sign = (buf[3] & 0x80) ? 1 : 0;
+       f.mpn.biased_exponent = (buf[3] << 1) | (buf[2] >> 7);
+       f.mpn.mantissa = buf[0] | (buf[1] << 8) | ((buf[2] & 0x7f) << 16);
+
+       return f.v_float;
+}
+
+static int lascar_proc_config(const struct sr_dev_inst *sdi)
+{
+       struct dev_context *devc;
+       int ret;
+
+       devc = sdi->priv;
+       if (!(devc->config = lascar_get_config(devc->usb->devhdl)))
+               return SR_ERR;
+
+       ret = SR_OK;
+       switch (devc->profile->logformat) {
+       case LOG_TEMP_RH:
+               /* TODO */
+               break;
+       case LOG_CO:
+               devc->sample_size = 2;
+               devc->co_high = binary32_le_to_float(devc->config + 36);
+               devc->co_low = binary32_le_to_float(devc->config + 40);
+               sr_dbg("EL-USB-CO calibration high %f low %f", devc->co_high,
+                               devc->co_low);
+               break;
+       default:
+               ret = SR_ERR_ARG;
+       }
+       devc->logged_samples = devc->config[0x1e] | (devc->config[0x1f] << 8);
+       sr_dbg("device log contains %d samples.", devc->logged_samples);
+
+       return ret;
+}
+
 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
                void *cb_data)
 {
-       /* TODO */
+       struct sr_datafeed_packet packet;
+       struct sr_datafeed_header header;
+       struct sr_datafeed_meta_analog meta;
+       struct dev_context *devc;
+       struct drv_context *drvc = di->priv;
+       struct libusb_transfer *xfer_in, *xfer_out;
+       const struct libusb_pollfd **pfd;
+       struct timeval tv;
+       int ret, i;
+       unsigned char cmd[3], resp[4], *buf;
+
+       if (!di->priv) {
+               sr_err("Driver was not initialized.");
+               return SR_ERR;
+       }
+
+       devc = sdi->priv;
+       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. */
+       sr_dbg("Sending SR_DF_HEADER.");
+       packet.type = SR_DF_HEADER;
+       packet.payload = (uint8_t *)&header;
+       header.feed_version = 1;
+       sr_session_send(devc->cb_data, &packet);
+
+       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);
+               return SR_OK;
+       }
+
+       if (!(xfer_in = libusb_alloc_transfer(0)) ||
+                       !(xfer_out = libusb_alloc_transfer(0)))
+               return SR_ERR;
+
+       libusb_control_transfer(devc->usb->devhdl, LIBUSB_REQUEST_TYPE_VENDOR,
+                       0x00, 0xffff, 0x00, NULL, 0, 50);
+       libusb_control_transfer(devc->usb->devhdl, LIBUSB_REQUEST_TYPE_VENDOR,
+                       0x02, 0x0002, 0x00, NULL, 0, 50);
+       libusb_control_transfer(devc->usb->devhdl, LIBUSB_REQUEST_TYPE_VENDOR,
+                       0x02, 0x0001, 0x00, NULL, 0, 50);
+
+
+       /* Flush input. The F321 requires this. */
+       while (libusb_bulk_transfer(devc->usb->devhdl, LASCAR_EP_IN, resp,
+                       256, &ret, 5) == 0 && ret > 0)
+               ;
+
+       libusb_fill_bulk_transfer(xfer_in, devc->usb->devhdl, LASCAR_EP_IN,
+                       resp, sizeof(resp), mark_xfer, 0, 10000);
+       if (libusb_submit_transfer(xfer_in) != 0) {
+               libusb_free_transfer(xfer_in);
+               libusb_free_transfer(xfer_out);
+               return SR_ERR;
+       }
+
+       cmd[0] = 0x03;
+       cmd[1] = 0xff;
+       cmd[2] = 0xff;
+       libusb_fill_bulk_transfer(xfer_out, devc->usb->devhdl, LASCAR_EP_OUT,
+                       cmd, 3, mark_xfer, 0, 100);
+       if (libusb_submit_transfer(xfer_out) != 0) {
+               libusb_free_transfer(xfer_in);
+               libusb_free_transfer(xfer_out);
+               return SR_ERR;
+       }
+
+       tv.tv_sec = 0;
+       tv.tv_usec = 0;
+       while (!xfer_in->user_data || !xfer_out->user_data) {
+               g_usleep(5000);
+               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)) {
+               sr_dbg("no response to log transfer request");
+               libusb_free_transfer(xfer_in);
+               libusb_free_transfer(xfer_out);
+               return SR_ERR;
+       }
+       if (xfer_in->actual_length != 3 || xfer_in->buffer[0] != 2) {
+               sr_dbg("invalid response to log transfer request");
+               libusb_free_transfer(xfer_in);
+               libusb_free_transfer(xfer_out);
+               return SR_ERR;
+       }
+       devc->log_size = xfer_in->buffer[1] + (xfer_in->buffer[2] << 8);
+       libusb_free_transfer(xfer_out);
+
+       /* Send metadata about the SR_DF_ANALOG packets to come. */
+       packet.type = SR_DF_META_ANALOG;
+       packet.payload = &meta;
+       meta.num_probes = 1;
+       sr_session_send(devc->cb_data, &packet);
+
+       pfd = libusb_get_pollfds(drvc->sr_ctx->libusb_ctx);
+       for (i = 0; pfd[i]; i++) {
+               /* Handle USB events every 100ms, for decent latency. */
+               sr_source_add(pfd[i]->fd, pfd[i]->events, 100,
+                               lascar_el_usb_handle_events, (void *)sdi);
+               /* We'll need to remove this fd later. */
+               devc->usbfd[i] = pfd[i]->fd;
+       }
+       devc->usbfd[i] = -1;
+
+       buf = g_try_malloc(4096);
+       libusb_fill_bulk_transfer(xfer_in, devc->usb->devhdl, LASCAR_EP_IN,
+                       buf, 4096, lascar_el_usb_receive_transfer, cb_data, 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);
+               g_free(buf);
+               return SR_ERR;
+       }
 
        return SR_OK;
 }
 
-static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
+SR_PRIV int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
 {
        (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;
        }
 
-       /* TODO */
+       sdi->status = SR_ST_STOPPING;
+       /* TODO: free ongoing transfers? */
 
        return SR_OK;
 }
index 1a9b2be5dca0a441d7f29fcde7e9b3d972af860c..508727adbab795938cdf0be3d2b8be3a0a921a74 100644 (file)
 #include <stdlib.h>
 #include <sys/time.h>
 #include <string.h>
+#include <math.h>
 #include <glib.h>
 #include "libsigrok.h"
 #include "libsigrok-internal.h"
 #include "protocol.h"
 
-#define LASCAR_VENDOR "Lascar"
-#define LASCAR_INTERFACE 0
-#define LASCAR_EP_IN 0x82
-#define LASCAR_EP_OUT 2
-/* Max 100ms for a device to positively identify. */
-#define SCAN_TIMEOUT 100000
-
 extern struct sr_dev_driver lascar_el_usb_driver_info;
 static struct sr_dev_driver *di = &lascar_el_usb_driver_info;
 
@@ -61,40 +55,51 @@ static const struct elusb_profile profiles[] = {
 };
 
 
-static void scan_xfer(struct libusb_transfer *xfer)
+SR_PRIV libusb_device_handle *lascar_open(struct libusb_device *dev)
+{
+       libusb_device_handle *dev_hdl;
+       int ret;
+
+       if ((ret = libusb_open(dev, &dev_hdl)) != 0) {
+               sr_dbg("failed to open device for scan: %s",
+                               libusb_error_name(ret));
+               return NULL;
+       }
+
+       /* Some of these fail, but it needs doing -- some sort of mode
+        * setup for the SILabs F32x. */
+       libusb_control_transfer(dev_hdl, LIBUSB_REQUEST_TYPE_VENDOR,
+                       0x00, 0xffff, 0x00, NULL, 0, 50);
+       libusb_control_transfer(dev_hdl, LIBUSB_REQUEST_TYPE_VENDOR,
+                       0x02, 0x0002, 0x00, NULL, 0, 50);
+       libusb_control_transfer(dev_hdl, LIBUSB_REQUEST_TYPE_VENDOR,
+                       0x02, 0x0001, 0x00, NULL, 0, 50);
+
+       return dev_hdl;
+}
+
+static void mark_xfer(struct libusb_transfer *xfer)
 {
 
        xfer->user_data = GINT_TO_POINTER(1);
 
 }
 
-static struct sr_dev_inst *lascar_identify(libusb_device_handle *dev_hdl)
+SR_PRIV unsigned char *lascar_get_config(libusb_device_handle *dev_hdl)
 {
        struct drv_context *drvc;
-       const struct elusb_profile *profile;
-       struct sr_dev_inst *sdi;
        struct libusb_transfer *xfer_in, *xfer_out;
        struct timeval tv;
        int64_t start;
-       int modelid, buflen, i;
-       unsigned char cmd[3], buf[256];
-       char firmware[5];
+       int buflen;
+       unsigned char cmd[3], buf[256], *config;
 
        drvc = di->priv;
-       modelid = 0;
-
-       /* Some of these fail, but it needs doing -- some sort of mode
-        * setup for the SILabs F32x. */
-       libusb_control_transfer(dev_hdl, LIBUSB_REQUEST_TYPE_VENDOR,
-                       0x00, 0xffff, 0x00, buf, 0, 50);
-       libusb_control_transfer(dev_hdl, LIBUSB_REQUEST_TYPE_VENDOR,
-                       0x02, 0x0002, 0x00, buf, 0, 50);
-       libusb_control_transfer(dev_hdl, LIBUSB_REQUEST_TYPE_VENDOR,
-                       0x02, 0x0001, 0x00, buf, 0, 50);
+       config = NULL;
 
        if (!(xfer_in = libusb_alloc_transfer(0)) ||
                        !(xfer_out = libusb_alloc_transfer(0)))
-               return 0;
+               return NULL;
 
        /* Flush anything the F321 still has queued. */
        while (libusb_bulk_transfer(dev_hdl, LASCAR_EP_IN, buf, 256, &buflen,
@@ -104,7 +109,7 @@ static struct sr_dev_inst *lascar_identify(libusb_device_handle *dev_hdl)
        /* Keep a read request waiting in the wings, ready to pounce
         * the moment the device sends something. */
        libusb_fill_bulk_transfer(xfer_in, dev_hdl, LASCAR_EP_IN,
-                       buf, 256, scan_xfer, 0, 10000);
+                       buf, 256, mark_xfer, 0, 10000);
        if (libusb_submit_transfer(xfer_in) != 0)
                goto cleanup;
 
@@ -113,7 +118,7 @@ static struct sr_dev_inst *lascar_identify(libusb_device_handle *dev_hdl)
        cmd[1] = 0xff;
        cmd[2] = 0xff;
        libusb_fill_bulk_transfer(xfer_out, dev_hdl, LASCAR_EP_OUT,
-                       cmd, 3, scan_xfer, 0, 100);
+                       cmd, 3, mark_xfer, 0, 100);
        if (libusb_submit_transfer(xfer_out) != 0)
                goto cleanup;
 
@@ -138,7 +143,7 @@ static struct sr_dev_inst *lascar_identify(libusb_device_handle *dev_hdl)
        }
 
        /* Got configuration structure header. */
-       sr_spew("response to config request: 0x%.2x 0x%.2x 0x%.2x ",
+       sr_dbg("response to config request: 0x%.2x 0x%.2x 0x%.2x ",
                        buf[0], buf[1], buf[2]);
        buflen = buf[1] | (buf[2] << 8);
        if (buf[0] != 0x02 || buflen > 256) {
@@ -170,7 +175,10 @@ static struct sr_dev_inst *lascar_identify(libusb_device_handle *dev_hdl)
                                xfer_in->actual_length);
                goto cleanup;
        }
-       modelid = buf[0];
+
+       if (!(config = g_try_malloc(256)))
+               return NULL;
+       memcpy(config, buf, buflen);
 
 cleanup:
        if (!xfer_in->user_data || !xfer_in->user_data) {
@@ -189,6 +197,19 @@ cleanup:
        libusb_free_transfer(xfer_in);
        libusb_free_transfer(xfer_out);
 
+       return config;
+}
+
+static struct sr_dev_inst *lascar_identify(unsigned char *config)
+{
+       struct dev_context *devc;
+       const struct elusb_profile *profile;
+       struct sr_dev_inst *sdi;
+       struct sr_probe *probe;
+       int modelid, i;
+       char firmware[5];
+
+       modelid = config[0];
        sdi = NULL;
        if (modelid) {
                profile = NULL;
@@ -203,8 +224,8 @@ cleanup:
                        return NULL;
                }
 
-               i = buf[52] | (buf[53] << 8);
-               memcpy(firmware, buf + 0x30, 4);
+               i = config[52] | (config[53] << 8);
+               memcpy(firmware, config + 0x30, 4);
                firmware[4] = '\0';
                sr_dbg("found %s with firmware version %s serial %d",
                                profile->modelname, firmware, i);
@@ -218,6 +239,15 @@ cleanup:
                                profile->modelname, firmware)))
                        return NULL;
                sdi->driver = di;
+
+               if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
+                       return NULL;
+               sdi->probes = g_slist_append(NULL, probe);
+
+               if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
+                       return NULL;
+               sdi->priv = devc;
+               devc->profile = profile;
        }
 
        return sdi;
@@ -231,6 +261,7 @@ SR_PRIV struct sr_dev_inst *lascar_scan(int bus, int address)
        struct libusb_device_descriptor des;
        libusb_device_handle *dev_hdl;
        int ret, i;
+       unsigned char *config;
 
        drvc = di->priv;
        sdi = NULL;
@@ -246,34 +277,146 @@ SR_PRIV struct sr_dev_inst *lascar_scan(int bus, int address)
                                libusb_get_device_address(devlist[i]) != address)
                        continue;
 
-               if ((ret = libusb_open(devlist[i], &dev_hdl)) != 0) {
-                       sr_dbg("failed to open device for scan: %s",
-                                       libusb_error_name(ret));
+               if (!(dev_hdl = lascar_open(devlist[i])))
+                       continue;
+
+               if (!(config = lascar_get_config(dev_hdl)))
                        continue;
-               }
 
-               sdi = lascar_identify(dev_hdl);
                libusb_close(dev_hdl);
+               sdi = lascar_identify(config);
+               g_free(config);
        }
 
        return sdi;
 }
 
+static void lascar_el_usb_dispatch(struct sr_dev_inst *sdi, unsigned char *buf,
+               int buflen)
+{
+       struct dev_context *devc;
+       struct sr_datafeed_packet packet;
+       struct sr_datafeed_analog analog;
+       uint16_t s;
+       int samples, samples_left, i;
+
+       devc = sdi->priv;
+       samples = buflen / devc->sample_size;
+       samples_left = devc->logged_samples - devc->rcvd_samples;
+       if (samples_left < samples)
+               samples = samples_left;
+       switch (devc->profile->logformat) {
+       case LOG_TEMP_RH:
+               /* TODO */
+               break;
+       case LOG_CO:
+               packet.type = SR_DF_ANALOG;
+               packet.payload = &analog;
+               analog.num_samples = samples;
+               analog.mq = 0;
+               analog.unit = 0;
+               analog.mqflags = 0;
+               if (!(analog.data = g_try_malloc(sizeof(float) * samples)))
+                       break;
+               for (i = 0; i < samples; i++) {
+                       s = (buf[i * 2] << 8) | buf[i * 2 + 1];
+                       analog.data[i] = s * devc->co_high + devc->co_low;
+                       if (analog.data[i] < 0.0)
+                               analog.data[i] = 0.0;
+               }
+               sr_session_send(devc->cb_data, &packet);
+               g_free(analog.data);
+               break;
+       default:
+               /* How did we even get this far? */
+               break;
+       }
+       devc->rcvd_samples += samples;
+
+}
 
-SR_PRIV int lascar_el_usb_receive_data(int fd, int revents, void *cb_data)
+SR_PRIV int lascar_el_usb_handle_events(int fd, int revents, void *cb_data)
 {
-       const struct sr_dev_inst *sdi;
        struct dev_context *devc;
+       struct drv_context *drvc = di->priv;
+       struct sr_datafeed_packet packet;
+       struct sr_dev_inst *sdi;
+       struct timeval tv;
+       int i;
 
-       if (!(sdi = cb_data))
-               return TRUE;
+       (void)fd;
+       (void)revents;
 
-       if (!(devc = sdi->priv))
-               return TRUE;
+       sdi = cb_data;
+       devc = sdi->priv;
 
-       if (revents == G_IO_IN) {
-               /* TODO */
+       if (sdi->status == SR_ST_STOPPING) {
+               for (i = 0; devc->usbfd[i] != -1; i++)
+                       sr_source_remove(devc->usbfd[i]);
+
+               sdi->driver->dev_close(sdi);
+
+               packet.type = SR_DF_END;
+               sr_session_send(cb_data, &packet);
        }
 
+       memset(&tv, 0, sizeof(struct timeval));
+       libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv,
+                                              NULL);
+
        return TRUE;
 }
+
+SR_PRIV void lascar_el_usb_receive_transfer(struct libusb_transfer *transfer)
+{
+       struct dev_context *devc;
+       struct sr_dev_inst *sdi;
+       int ret;
+       gboolean packet_has_error;
+
+       sdi = transfer->user_data;
+       devc = sdi->priv;
+
+       packet_has_error = FALSE;
+       switch (transfer->status) {
+       case LIBUSB_TRANSFER_NO_DEVICE:
+               /* USB device was unplugged. */
+               hw_dev_acquisition_stop(sdi, sdi);
+               return;
+       case LIBUSB_TRANSFER_COMPLETED:
+       case LIBUSB_TRANSFER_TIMED_OUT: /* We may have received some data though */
+               break;
+       default:
+               packet_has_error = TRUE;
+               break;
+       }
+
+       if (!packet_has_error) {
+               if (devc->rcvd_samples < devc->logged_samples)
+                       lascar_el_usb_dispatch(sdi, transfer->buffer,
+                                       transfer->actual_length);
+               devc->rcvd_bytes += transfer->actual_length;
+               sr_spew("received %d/%d bytes (%d/%d samples)",
+                               devc->rcvd_bytes, devc->log_size,
+                               devc->rcvd_samples, devc->logged_samples);
+               if (devc->rcvd_bytes >= devc->log_size)
+                       hw_dev_acquisition_stop(sdi, sdi);
+       }
+
+       if (sdi->status == SR_ST_ACTIVE) {
+               /* Send the same request again. */
+               if ((ret = libusb_submit_transfer(transfer) != 0)) {
+                       sr_err("Unable to resubmit transfer: %s.",
+                              libusb_error_name(ret));
+                       g_free(transfer->buffer);
+                       libusb_free_transfer(transfer);
+                       hw_dev_acquisition_stop(sdi, sdi);
+               }
+       } else {
+               /* This was the last transfer we're going to receive, so
+                * clean up now. */
+               g_free(transfer->buffer);
+               libusb_free_transfer(transfer);
+       }
+
+}
index 4aa2e2d6f13d2b5d3eb2168a9bbd10cc792ca03f..80d23824378955fe9a62a6f4d96627e252e75cb7 100644 (file)
 #define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args)
 #define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args)
 
+#define LASCAR_VENDOR "Lascar"
+#define LASCAR_INTERFACE 0
+#define LASCAR_EP_IN 0x82
+#define LASCAR_EP_OUT 2
+/* Max 100ms for a device to positively identify. */
+#define SCAN_TIMEOUT 100000
+
 /** Private, per-device-instance driver context. */
 struct dev_context {
-       /** The current sampling limit (in number of samples). */
-       uint64_t limit_samples;
-
-       /** The current sampling limit (in ms). */
-       uint64_t limit_msec;
-
-       /** Opaque pointer passed in by the frontend. */
        void *cb_data;
-
-       /** The current number of already received samples. */
-       uint64_t num_samples;
-
        struct sr_usb_dev_inst *usb;
        const struct elusb_profile *profile;
-       /* Only requires 3 really. */
        int usbfd[10];
+       /* Generic EL-USB */
+       unsigned char *config;
+       unsigned int log_size;
+       unsigned int rcvd_bytes;
+       unsigned int sample_size;
+       unsigned int logged_samples;
+       unsigned int rcvd_samples;
+       unsigned int limit_samples;
+       /* Model-specific */
+       /* EL-USB-CO: these are something like calibration values fixed per
+        * device, used to convert the sample values to CO ppm. */
+       float co_high;
+       float co_low;
 };
 
 enum {
@@ -65,6 +73,9 @@ struct elusb_profile {
        int logformat;
 };
 
-SR_PRIV int lascar_el_usb_receive_data(int fd, int revents, void *cb_data);
+SR_PRIV unsigned char *lascar_get_config(libusb_device_handle *dev_hdl);
+SR_PRIV int lascar_el_usb_handle_events(int fd, int revents, void *cb_data);
+SR_PRIV void lascar_el_usb_receive_transfer(struct libusb_transfer *transfer);
+SR_PRIV int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data);
 
 #endif