+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_CO },
+ { 12, "EL-USB-2-LCD", LOG_TEMP_RH },
+ { 13, "EL-USB-2+", LOG_TEMP_RH },
+ { 14, "EL-USB-1-PRO", LOG_UNSUPPORTED },
+ { 15, "EL-USB-TC-LCD", LOG_UNSUPPORTED },
+ { 16, "EL-USB-2-LCD+", LOG_TEMP_RH },
+ { 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 }
+};
+
+
+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);
+
+}
+
+SR_PRIV int lascar_get_config(libusb_device_handle *dev_hdl,
+ unsigned char *configblock, int *configlen)
+{
+ struct drv_context *drvc;
+ struct libusb_transfer *xfer_in, *xfer_out;
+ struct timeval tv;
+ int64_t start;
+ int buflen;
+ unsigned char cmd[3], buf[MAX_CONFIGBLOCK_SIZE];
+
+ sr_spew("Reading config block.");
+
+ drvc = di->priv;
+ *configlen = 0;
+
+ if (!(xfer_in = libusb_alloc_transfer(0)) ||
+ !(xfer_out = libusb_alloc_transfer(0)))
+ return SR_ERR;
+
+ /* 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, mark_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, mark_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 > MAX_CONFIGBLOCK_SIZE) {
+ 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;
+ }
+
+ memcpy(configblock, buf, buflen);
+ *configlen = buflen;
+
+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);
+
+ return *configlen ? SR_OK : SR_ERR;
+}
+
+SR_PRIV int lascar_save_config(libusb_device_handle *dev_hdl,
+ unsigned char *config, int configlen)
+{
+ struct drv_context *drvc;
+ struct libusb_transfer *xfer_in, *xfer_out;
+ struct timeval tv;
+ int64_t start;
+ int buflen, ret;
+ unsigned char cmd[3], buf[256];
+
+ sr_spew("Writing config block.");
+
+ drvc = di->priv;
+
+ if (!(xfer_in = libusb_alloc_transfer(0)) ||
+ !(xfer_out = libusb_alloc_transfer(0)))
+ return SR_ERR;
+
+ /* Flush anything the F321 still has queued. */
+ while (libusb_bulk_transfer(dev_hdl, LASCAR_EP_IN, buf, 256, &buflen,
+ 5) == 0 && buflen > 0)
+ ;
+ ret = SR_OK;
+
+ /* 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, mark_xfer, 0, 10000);
+ if (libusb_submit_transfer(xfer_in) != 0) {
+ ret = SR_ERR;
+ goto cleanup;
+ }
+
+ /* Request device configuration structure. */
+ cmd[0] = 0x01;
+ cmd[1] = configlen & 0xff;
+ cmd[2] = (configlen >> 8) & 0xff;
+ libusb_fill_bulk_transfer(xfer_out, dev_hdl, LASCAR_EP_OUT,
+ cmd, 3, mark_xfer, 0, 100);
+ if (libusb_submit_transfer(xfer_out) != 0) {
+ ret = SR_ERR;
+ goto cleanup;
+ }
+ tv.tv_sec = 0;
+ tv.tv_usec = 0;
+ while (!xfer_out->user_data) {
+ g_usleep(5000);
+ libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
+ }
+
+ libusb_fill_bulk_transfer(xfer_out, dev_hdl, LASCAR_EP_OUT,
+ config, configlen, mark_xfer, 0, 100);
+ if (libusb_submit_transfer(xfer_out) != 0) {
+ ret = SR_ERR;
+ goto cleanup;
+ }
+ 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->actual_length != 1 || buf[0] != 0xff) {
+ sr_dbg("unexpected response after transfer");
+ ret = SR_ERR;
+ }
+
+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);
+
+ return ret;
+}
+
+static struct sr_dev_inst *lascar_identify(unsigned char *config)