]> sigrok.org Git - libsigrok.git/commitdiff
lascar-el-usb: add scanning functionality
authorBert Vermeulen <redacted>
Tue, 4 Dec 2012 22:58:26 +0000 (23:58 +0100)
committerBert Vermeulen <redacted>
Tue, 4 Dec 2012 22:58:26 +0000 (23:58 +0100)
hardware/lascar-el-usb/api.c
hardware/lascar-el-usb/protocol.c
hardware/lascar-el-usb/protocol.h

index 628252490a67adb6037d2238e78dbaf8978306bf..aa506fa66b475d4813a3f13986a24d19b1cb5e33 100644 (file)
  */
 
 #include <glib.h>
+#include <libusb.h>
 #include "libsigrok.h"
 #include "libsigrok-internal.h"
 #include "protocol.h"
 
+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,
+       0,
+};
+
+static const int hwcaps[] = {
+       SR_HWCAP_THERMOMETER,
+       SR_HWCAP_HYGROMETER,
+       SR_HWCAP_LIMIT_SAMPLES,
+       0
+};
+
+static const char *probe_names[] = {
+       "P1",
+};
+
 
 /* Properly close and free all devices. */
 static int clear_instances(void)
@@ -42,8 +63,8 @@ static int clear_instances(void)
                if (!(devc = sdi->priv))
                        continue;
 
-               /* TODO */
-
+               hw_dev_close(sdi);
+               sr_usb_dev_inst_free(devc->usb);
                sr_dev_inst_free(sdi);
        }
 
@@ -53,7 +74,7 @@ static int clear_instances(void)
        return SR_OK;
 }
 
-static int hw_init(void)
+static int hw_init(struct sr_context *sr_ctx)
 {
        struct drv_context *drvc;
 
@@ -62,8 +83,7 @@ static int hw_init(void)
                return SR_ERR_MALLOC;
        }
 
-       /* TODO */
-
+       drvc->sr_ctx = sr_ctx;
        di->priv = drvc;
 
        return SR_OK;
@@ -72,15 +92,62 @@ static int hw_init(void)
 static GSList *hw_scan(GSList *options)
 {
        struct drv_context *drvc;
-       GSList *devices;
+       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;
 
        (void)options;
 
-       devices = NULL;
-       drvc = di->priv;
-       drvc->instances = NULL;
+       if (!(drvc = di->priv)) {
+               sr_err("Driver was not initialized.");
+               return NULL;
+       }
 
-       /* TODO */
+       /* USB scan is always authoritative. */
+       clear_instances();
+
+       conn = NULL;
+       for (l = options; l; l = l->next) {
+               opt = l->data;
+               switch (opt->hwopt) {
+               case SR_HWOPT_CONN:
+                       conn = opt->value;
+                       break;
+               }
+       }
+       if (!conn)
+               return NULL;
+
+       devices = NULL;
+       if ((usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn))) {
+               /* We have a list of sr_usb_dev_inst matching the connection
+                * string. Wrap them in sr_dev_inst and we're done. */
+               for (l = usb_devices; l; l = l->next) {
+                       usb = l->data;
+                       if (!(sdi = lascar_scan(usb->bus, usb->address))) {
+                               /* Not a Lascar EL-USB. */
+                               g_free(usb);
+                               continue;
+                       }
+                       if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
+                               sr_err("Device context malloc failed.");
+                               return NULL;
+                       }
+                       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);
+               }
+               g_slist_free(usb_devices);
+       } else
+               g_slist_free_full(usb_devices, g_free);
 
        return devices;
 }
@@ -118,7 +185,7 @@ static int hw_cleanup(void)
 }
 
 static int hw_info_get(int info_id, const void **data,
-                      const struct sr_dev_inst *sdi)
+               const struct sr_dev_inst *sdi)
 {
        switch (info_id) {
        /* TODO */
@@ -131,7 +198,7 @@ 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)
+               const void *value)
 {
        int ret;
 
@@ -152,15 +219,14 @@ static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
 }
 
 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
-                                   void *cb_data)
+               void *cb_data)
 {
        /* TODO */
 
        return SR_OK;
 }
 
-static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
-                                  void *cb_data)
+static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
 {
        (void)cb_data;
 
index ccb4d2c910440ce610793e9acecab2d1dd981232..1a9b2be5dca0a441d7f29fcde7e9b3d972af860c 100644 (file)
  */
 
 #include <stdlib.h>
+#include <sys/time.h>
+#include <string.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;
+
+static const struct elusb_profile profiles[] = {
+       { 1, "EL-USB-1", LOG_UNSUPPORTED },
+       { 2, "EL-USB-1", LOG_UNSUPPORTED },
+       { 3, "EL-USB-2", LOG_TEMP_RH },
+       { 4, "EL-USB-3", LOG_UNSUPPORTED },
+       { 5, "EL-USB-4", LOG_UNSUPPORTED },
+       { 6, "EL-USB-3", LOG_UNSUPPORTED },
+       { 7, "EL-USB-4", LOG_UNSUPPORTED },
+       { 8, "EL-USB-LITE", LOG_UNSUPPORTED },
+       { 9, "EL-USB-CO", LOG_CO },
+       { 10, "EL-USB-TC", LOG_UNSUPPORTED },
+       { 11, "EL-USB-CO300", LOG_UNSUPPORTED },
+       { 12, "EL-USB-2-LCD", LOG_UNSUPPORTED },
+       { 13, "EL-USB-2+", LOG_UNSUPPORTED },
+       { 14, "EL-USB-1-PRO", LOG_UNSUPPORTED },
+       { 15, "EL-USB-TC-LCD", LOG_UNSUPPORTED },
+       { 16, "EL-USB-2-LCD+", LOG_UNSUPPORTED },
+       { 17, "EL-USB-5", LOG_UNSUPPORTED },
+       { 18, "EL-USB-1-RCG", LOG_UNSUPPORTED },
+       { 19, "EL-USB-1-LCD", LOG_UNSUPPORTED },
+       { 20, "EL-OEM-3", LOG_UNSUPPORTED },
+       { 21, "EL-USB-1-LCD", LOG_UNSUPPORTED },
+       { 0, NULL, 0 }
+};
+
+
+static void scan_xfer(struct libusb_transfer *xfer)
+{
+
+       xfer->user_data = GINT_TO_POINTER(1);
+
+}
+
+static struct sr_dev_inst *lascar_identify(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];
+
+       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);
+
+       if (!(xfer_in = libusb_alloc_transfer(0)) ||
+                       !(xfer_out = libusb_alloc_transfer(0)))
+               return 0;
+
+       /* Flush anything the F321 still has queued. */
+       while (libusb_bulk_transfer(dev_hdl, LASCAR_EP_IN, buf, 256, &buflen,
+                       5) == 0 && buflen > 0)
+               ;
+
+       /* 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);
+       if (libusb_submit_transfer(xfer_in) != 0)
+               goto cleanup;
+
+       /* Request device configuration structure. */
+       cmd[0] = 0x00;
+       cmd[1] = 0xff;
+       cmd[2] = 0xff;
+       libusb_fill_bulk_transfer(xfer_out, dev_hdl, LASCAR_EP_OUT,
+                       cmd, 3, scan_xfer, 0, 100);
+       if (libusb_submit_transfer(xfer_out) != 0)
+               goto cleanup;
+
+       tv.tv_sec = 0;
+       tv.tv_usec = 0;
+       start = g_get_monotonic_time();
+       while (!xfer_in->user_data || !xfer_out->user_data) {
+               if (g_get_monotonic_time() - start > SCAN_TIMEOUT) {
+                       start = 0;
+                       break;
+               }
+               g_usleep(5000);
+               libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
+       }
+       if (!start) {
+               sr_dbg("no response");
+               goto cleanup;
+       }
+       if (xfer_in->actual_length != 3) {
+               sr_dbg("expected 3-byte header, got %d bytes", xfer_in->actual_length);
+               goto cleanup;
+       }
+
+       /* Got configuration structure header. */
+       sr_spew("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) {
+               sr_dbg("Invalid response to config request: "
+                               "0x%.2x 0x%.2x 0x%.2x ", buf[0], buf[1], buf[2]);
+               libusb_close(dev_hdl);
+               goto cleanup;
+       }
+
+       /* Get configuration structure. */
+       xfer_in->length = buflen;
+       xfer_in->user_data = 0;
+       if (libusb_submit_transfer(xfer_in) != 0)
+               goto cleanup;
+       while (!xfer_in->user_data) {
+               if (g_get_monotonic_time() - start > SCAN_TIMEOUT) {
+                       start = 0;
+                       break;
+               }
+               g_usleep(5000);
+               libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
+       }
+       if (!start) {
+               sr_dbg("Timeout waiting for configuration structure.");
+               goto cleanup;
+       }
+       if (xfer_in->actual_length != buflen) {
+               sr_dbg("expected %d-byte structure, got %d bytes", buflen,
+                               xfer_in->actual_length);
+               goto cleanup;
+       }
+       modelid = buf[0];
+
+cleanup:
+       if (!xfer_in->user_data || !xfer_in->user_data) {
+               if (!xfer_in->user_data)
+                       libusb_cancel_transfer(xfer_in);
+               if (!xfer_out->user_data)
+                       libusb_cancel_transfer(xfer_out);
+               start = g_get_monotonic_time();
+               while (!xfer_in->user_data || !xfer_out->user_data) {
+                       if (g_get_monotonic_time() - start > 10000)
+                               break;
+                       g_usleep(1000);
+                       libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
+               }
+       }
+       libusb_free_transfer(xfer_in);
+       libusb_free_transfer(xfer_out);
+
+       sdi = NULL;
+       if (modelid) {
+               profile = NULL;
+               for (i = 0; profiles[i].modelid; i++) {
+                       if (profiles[i].modelid == modelid) {
+                               profile = &profiles[i];
+                               break;
+                       }
+               }
+               if (!profile) {
+                       sr_dbg("unknown EL-USB modelid %d", modelid);
+                       return NULL;
+               }
+
+               i = buf[52] | (buf[53] << 8);
+               memcpy(firmware, buf + 0x30, 4);
+               firmware[4] = '\0';
+               sr_dbg("found %s with firmware version %s serial %d",
+                               profile->modelname, firmware, i);
+
+               if (profile->logformat == LOG_UNSUPPORTED) {
+                       sr_dbg("unsupported EL-USB logformat for %s", profile->modelname);
+                       return NULL;
+               }
+
+               if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, LASCAR_VENDOR,
+                               profile->modelname, firmware)))
+                       return NULL;
+               sdi->driver = di;
+       }
+
+       return sdi;
+}
+
+SR_PRIV struct sr_dev_inst *lascar_scan(int bus, int address)
+{
+       struct drv_context *drvc;
+       struct sr_dev_inst *sdi;
+       struct libusb_device **devlist;
+       struct libusb_device_descriptor des;
+       libusb_device_handle *dev_hdl;
+       int ret, i;
+
+       drvc = di->priv;
+       sdi = NULL;
+
+       libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
+       for (i = 0; devlist[i]; i++) {
+               if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
+                       sr_err("Failed to get device descriptor: %d.", ret);
+                       continue;
+               }
+
+               if (libusb_get_bus_number(devlist[i]) != bus ||
+                               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));
+                       continue;
+               }
+
+               sdi = lascar_identify(dev_hdl);
+               libusb_close(dev_hdl);
+       }
+
+       return sdi;
+}
+
+
 SR_PRIV int lascar_el_usb_receive_data(int fd, int revents, void *cb_data)
 {
        const struct sr_dev_inst *sdi;
index ea3e4c81f049d4e3a5f3d9696220deb36113c2d2..4aa2e2d6f13d2b5d3eb2168a9bbd10cc792ca03f 100644 (file)
@@ -46,6 +46,23 @@ struct dev_context {
 
        /** 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];
+};
+
+enum {
+       LOG_UNSUPPORTED,
+       LOG_TEMP_RH,
+       LOG_CO,
+};
+
+struct elusb_profile {
+       int modelid;
+       char *modelname;
+       int logformat;
 };
 
 SR_PRIV int lascar_el_usb_receive_data(int fd, int revents, void *cb_data);