}
/**
- * Open the specified device.
+ * Open the specified device instance.
+ *
+ * If the device instance is already open (sdi->status == SR_ST_ACTIVE),
+ * SR_ERR will be returned and no re-opening of the device will be attempted.
+ *
+ * If opening was successful, sdi->status is set to SR_ST_ACTIVE, otherwise
+ * it will be left unchanged.
*
* @param sdi Device instance to use. Must not be NULL.
*
- * @return SR_OK upon success, a negative error code upon errors.
+ * @retval SR_OK Success.
+ * @retval SR_ERR_ARG Invalid arguments.
+ * @retval SR_ERR Device instance was already active, or other error.
*
* @since 0.2.0
*/
int ret;
if (!sdi || !sdi->driver || !sdi->driver->dev_open)
- return SR_ERR;
+ return SR_ERR_ARG;
if (sdi->status == SR_ST_ACTIVE) {
sr_err("%s: Device instance already active, can't re-open.",
return SR_ERR;
}
- sr_dbg("%s: Opening device.", sdi->driver->name)
+ sr_dbg("%s: Opening device instance.", sdi->driver->name);
ret = sdi->driver->dev_open(sdi);
+ if (ret == SR_OK)
+ sdi->status = SR_ST_ACTIVE;
+
return ret;
}
devc = sdi->priv;
- /* Make sure it's an ASIX SIGMA. */
if ((ret = ftdi_usb_open_desc(&devc->ftdic,
- USB_VENDOR, USB_PRODUCT, USB_DESCRIPTION, NULL)) < 0) {
-
- sr_err("ftdi_usb_open failed: %s",
- ftdi_get_error_string(&devc->ftdic));
-
- return 0;
+ USB_VENDOR, USB_PRODUCT, USB_DESCRIPTION, NULL)) < 0) {
+ sr_err("Failed to open device (%d): %s.",
+ ret, ftdi_get_error_string(&devc->ftdic));
+ return SR_ERR;
}
- sdi->status = SR_ST_ACTIVE;
-
return SR_OK;
}
{
(void)sdi;
- sdi->status = SR_ST_ACTIVE;
-
return SR_OK;
}
return SR_ERR;
}
- /* We're good to go now */
- sdi->status = SR_ST_ACTIVE;
return SR_OK;
}
usb = sdi->conn;
devc = sdi->priv;
- if ((ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb)) == SR_OK)
- sdi->status = SR_ST_ACTIVE;
- else
+ if ((ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb)) < 0)
return SR_ERR;
/* Detach kernel drivers which grabbed this device (if any). */
}
sr_dbg("Successfully claimed interface 0.");
- return ret;
+ return SR_OK;
}
static int dev_close(struct sr_dev_inst *sdi)
/* Wait 100ms. */
g_usleep(100 * 1000);
- sdi->status = SR_ST_ACTIVE;
-
- if (ret == SR_OK)
- return SR_OK;
+ return SR_OK;
err_ftdi_free:
ftdi_free(devc->ftdic); /* Close device (if open), free FTDI context. */
devc->ftdic = NULL;
- return ret;
+ return SR_ERR;
}
static int dev_close(struct sr_dev_inst *sdi)
static int dev_open(struct sr_dev_inst *sdi)
{
- sdi->status = SR_ST_ACTIVE;
+ (void)sdi;
return SR_OK;
}
struct dev_context *devc;
struct drv_context *drvc;
struct version_info vi;
- int ret, i, device_count;
+ int ret = SR_ERR, i, device_count;
uint8_t revid;
char connection_id[64];
if ((sdi->status == SR_ST_INITIALIZING) ||
(sdi->status == SR_ST_INACTIVE)) {
- /*
- * Check device by its physical USB bus/port address.
- */
+ /* Check device by its physical USB bus/port address. */
usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
if (strcmp(sdi->connection_id, connection_id))
/* This is not the one. */
} else {
sr_err("Failed to open device: %s.",
libusb_error_name(ret));
+ ret = SR_ERR;
break;
}
if ((ret = libusb_detach_kernel_driver(usb->devhdl, USB_INTERFACE)) < 0) {
sr_err("Failed to detach kernel driver: %s.",
libusb_error_name(ret));
- return SR_ERR;
+ ret = SR_ERR;
+ break;
}
}
}
sr_err("Expected firmware version %d.x, "
"got %d.%d.", DSLOGIC_REQUIRED_VERSION_MAJOR,
vi.major, vi.minor);
+ ret = SR_ERR;
break;
}
- sdi->status = SR_ST_ACTIVE;
sr_info("Opened device on %d.%d (logical) / %s (physical), "
"interface %d, firmware %d.%d.",
usb->bus, usb->address, connection_id,
sr_info("Detected REVID=%d, it's a Cypress CY7C68013%s.",
revid, (revid != 1) ? " (FX2)" : "A (FX2LP)");
+ ret = SR_OK;
+
break;
}
- libusb_free_device_list(devlist, 1);
- if (sdi->status != SR_ST_ACTIVE)
- return SR_ERR;
+ libusb_free_device_list(devlist, 1);
- return SR_OK;
+ return ret;
}
SR_PRIV struct dev_context *dslogic_dev_new(void)
}
sr_dbg("FTDI chip bitbang mode entered successfully.");
- sdi->status = SR_ST_ACTIVE;
-
return SR_OK;
+
err_dev_open_close_ftdic:
ftdi_usb_close(devc->ftdic);
+
err_ftdi_free:
ftdi_free(devc->ftdic);
+
return SR_ERR;
}
struct dev_context *devc;
struct drv_context *drvc;
struct version_info vi;
- int ret, i, device_count;
+ int ret = SR_ERR, i, device_count;
uint8_t revid;
char connection_id[64];
} else {
sr_err("Failed to open device: %s.",
libusb_error_name(ret));
+ ret = SR_ERR;
break;
}
if ((ret = libusb_detach_kernel_driver(usb->devhdl, USB_INTERFACE)) < 0) {
sr_err("Failed to detach kernel driver: %s.",
libusb_error_name(ret));
- return SR_ERR;
+ ret = SR_ERR;
+ break;
}
}
}
break;
}
- sdi->status = SR_ST_ACTIVE;
sr_info("Opened device on %d.%d (logical) / %s (physical), "
"interface %d, firmware %d.%d.",
usb->bus, usb->address, connection_id,
sr_info("Detected REVID=%d, it's a Cypress CY7C68013%s.",
revid, (revid != 1) ? " (FX2)" : "A (FX2LP)");
+ ret = SR_OK;
+
break;
}
- libusb_free_device_list(devlist, 1);
- if (sdi->status != SR_ST_ACTIVE)
- return SR_ERR;
+ libusb_free_device_list(devlist, 1);
- return SR_OK;
+ return ret;
}
SR_PRIV struct dev_context *fx2lafw_dev_new(void)
return SR_ERR;
}
- sdi->status = SR_ST_ACTIVE;
-
return SR_OK;
}
if (hmo_scope_state_get(sdi) != SR_OK)
return SR_ERR;
- sdi->status = SR_ST_ACTIVE;
-
return SR_OK;
}
struct sr_usb_dev_inst *usb;
struct libusb_device_descriptor des;
libusb_device **devlist;
- int err, i;
+ int err = SR_ERR, i;
char connection_id[64];
devc = sdi->priv;
usb->address = libusb_get_device_address(devlist[i]);
}
- sdi->status = SR_ST_ACTIVE;
sr_info("Opened device on %d.%d (logical) / "
"%s (physical) interface %d.",
usb->bus, usb->address,
sdi->connection_id, USB_INTERFACE);
+
+ err = SR_OK;
} else {
sr_err("Failed to open device: %s.",
libusb_error_name(err));
+ err = SR_ERR;
}
/* If we made it here, we handled the device (somehow). */
libusb_free_device_list(devlist, 1);
- if (sdi->status != SR_ST_ACTIVE)
- return SR_ERR;
-
- return SR_OK;
+ return err;
}
SR_PRIV void hantek_6xxx_close(struct sr_dev_inst *sdi)
sr_scpi_send(scpi, "TRIG HOLD");
sr_scpi_get_float(scpi, "NPLC?", &devc->nplc);
- sdi->status = SR_ST_ACTIVE;
-
return SR_OK;
}
if (!devc->samples)
goto fail3;
- sdi->status = SR_ST_ACTIVE;
-
return SR_OK;
fail3:
return SR_ERR;
}
- sdi->status = SR_ST_ACTIVE;
-
return SR_OK;
}
sr_dbg("Received ScanaPLUS device ID successfully: %02x %02x %02x.",
devc->devid[0], devc->devid[1], devc->devid[2]);
- sdi->status = SR_ST_ACTIVE;
-
return SR_OK;
err_dev_open_close_ftdic:
scanaplus_close(devc);
+
return SR_ERR;
}
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)
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)
if (lecroy_xstream_state_get(sdi) != SR_OK)
return SR_ERR;
- sdi->status = SR_ST_ACTIVE;
-
return SR_OK;
}
if (serial_open(devc->serial, SERIAL_RDWR) != SR_OK)
return SR_ERR;
- sdi->status = SR_ST_ACTIVE;
-
/* FIXME: discard serial buffer */
mso_check_trigger(devc->serial, &devc->trigger_state);
sr_dbg("Trigger state: 0x%x.", devc->trigger_state);
if (sr_modbus_open(modbus) < 0)
return SR_ERR;
- sdi->status = SR_ST_ACTIVE;
-
maynuo_m97_set_bit(modbus, PC1, 1);
return SR_OK;
devc = sdi->priv;
- if (p_ols_open(devc) != SR_OK)
- return SR_ERR;
-
- sdi->status = SR_ST_ACTIVE;
-
- return SR_OK;
+ return p_ols_open(devc);
}
static int dev_close(struct sr_dev_inst *sdi)
err_open_close_ftdic:
ftdi_usb_close(devc->ftdic);
+
return SR_ERR;
}
return SR_ERR;
}
- sdi->status = SR_ST_ACTIVE;
-
return SR_OK;
}
static int dev_open(struct sr_dev_inst *sdi)
{
- if (sr_scpi_open(sdi->conn) != SR_OK)
- return SR_ERR;
-
- sdi->status = SR_ST_ACTIVE;
-
- return SR_OK;
+ return sr_scpi_open(sdi->conn);
}
static int dev_close(struct sr_dev_inst *sdi)
if (devc->dig_samplerate == 0)
devc->dig_samplerate = samplerates[3];
- sdi->status = SR_ST_ACTIVE;
-
return SR_OK;
}
struct sr_usb_dev_inst *usb;
struct libusb_device_descriptor des;
struct drv_context *drvc;
- int ret, i, device_count;
+ int ret = SR_ERR, i, device_count;
char connection_id[64];
di = sdi->driver;
} else {
sr_err("Failed to open device: %s.",
libusb_error_name(ret));
+ ret = SR_ERR;
break;
}
if (ret == LIBUSB_ERROR_BUSY) {
sr_err("Unable to claim USB interface. Another "
"program or driver has already claimed it.");
+ ret = SR_ERR;
break;
} else if (ret == LIBUSB_ERROR_NO_DEVICE) {
sr_err("Device has been disconnected.");
+ ret = SR_ERR;
break;
} else if (ret != 0) {
sr_err("Unable to claim interface: %s.",
libusb_error_name(ret));
+ ret = SR_ERR;
break;
}
break;
}
- sdi->status = SR_ST_ACTIVE;
sr_info("Opened device on %d.%d (logical) / %s (physical), interface %d.",
usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
+ ret = SR_OK;
+
break;
}
+
libusb_free_device_list(devlist, 1);
- if (sdi->status != SR_ST_ACTIVE) {
+ if (ret != SR_OK) {
if (usb->devhdl) {
libusb_release_interface(usb->devhdl, USB_INTERFACE);
libusb_close(usb->devhdl);
if (sr_scpi_open(scpi) < 0)
return SR_ERR;
- sdi->status = SR_ST_ACTIVE;
-
devc = sdi->priv;
scpi_cmd(sdi, devc->device->commands, SCPI_CMD_REMOTE);
devc->beeper_was_set = FALSE;
/* 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;
break;
/* Rinse and repeat. */
- sdi->status = SR_ST_INACTIVE;
sr_usb_close(usb);
}
sr_err("Failed to claim interface: %s.", libusb_error_name(ret));
return SR_ERR;
}
- sdi->status = SR_ST_ACTIVE;
return SR_OK;
}
struct sr_dev_driver *di;
struct drv_context *drvc;
struct sr_usb_dev_inst *usb;
- int ret;
di = sdi->driver;
drvc = di->context;
usb = sdi->conn;
- if ((ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb)) == SR_OK)
- sdi->status = SR_ST_ACTIVE;
-
- return ret;
+ return sr_usb_open(drvc->sr_ctx->libusb_ctx, usb);
}
static int dev_close(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)
sr_err("Failed to claim interface: %s.", libusb_error_name(ret));
return SR_ERR;
}
- sdi->status = SR_ST_ACTIVE;
return SR_OK;
}
if (dlm_scope_state_query(sdi) != SR_OK)
return SR_ERR;
- sdi->status = SR_ST_ACTIVE;
-
return SR_OK;
}
if (ret != SR_OK)
return ret;
- sdi->status = SR_ST_ACTIVE;
-
ret = libusb_set_configuration(usb->devhdl, USB_CONFIGURATION);
if (ret < 0) {
sr_err("Unable to set USB configuration %d: %s.",
* callback in drivers that use a serial port. The port is opened
* with the SERIAL_RDWR flag.
*
- * If the open succeeded, the status field of the given sdi is set
- * to SR_ST_ACTIVE.
- *
* @retval SR_OK Success.
+ * @retval SR_ERR_ARG Invalid arguments.
* @retval SR_ERR Serial port open failed.
*/
SR_PRIV int std_serial_dev_open(struct sr_dev_inst *sdi)
{
struct sr_serial_dev_inst *serial;
- serial = sdi->conn;
- if (serial_open(serial, SERIAL_RDWR) != SR_OK)
- return SR_ERR;
+ if (!sdi || !sdi->conn)
+ return SR_ERR_ARG;
- sdi->status = SR_ST_ACTIVE;
+ serial = sdi->conn;
- return SR_OK;
+ return serial_open(serial, SERIAL_RDWR);
}
/**