X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Fhardware%2Fikalogic-scanalogic2%2Fapi.c;h=b911f03d182b224cd9a48c5db8edb50a99e349ff;hb=e26a443aadd8ad75dc45d58ffa51e8e86fbb804f;hp=abdbfd7228c562f8570f134c35fc4a54fb1626b3;hpb=dcd438ee3523098201c7937e75e55775da3b506f;p=libsigrok.git
diff --git a/src/hardware/ikalogic-scanalogic2/api.c b/src/hardware/ikalogic-scanalogic2/api.c
index abdbfd72..b911f03d 100644
--- a/src/hardware/ikalogic-scanalogic2/api.c
+++ b/src/hardware/ikalogic-scanalogic2/api.c
@@ -17,10 +17,14 @@
* along with this program. If not, see .
*/
+#include
#include "protocol.h"
-static const uint32_t devopts[] = {
+static const uint32_t drvopts[] = {
SR_CONF_LOGIC_ANALYZER,
+};
+
+static const uint32_t devopts[] = {
SR_CONF_LIMIT_SAMPLES | SR_CONF_SET | SR_CONF_LIST,
SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
@@ -49,16 +53,8 @@ SR_PRIV const uint64_t sl2_samplerates[NUM_SAMPLERATES] = {
static const char *channel_names[] = {
"0", "1", "2", "3",
- NULL,
};
-SR_PRIV struct sr_dev_driver ikalogic_scanalogic2_driver_info;
-
-static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
-{
- return std_init(sr_ctx, di, LOG_PREFIX);
-}
-
static GSList *scan(struct sr_dev_driver *di, GSList *options)
{
GSList *usb_devices, *devices, *l;
@@ -73,18 +69,17 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
(void)options;
devices = NULL;
- drvc = di->priv;
- drvc->instances = NULL;
+ drvc = di->context;
usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, USB_VID_PID);
- if (usb_devices == NULL)
+ if (!usb_devices)
return NULL;
for (l = usb_devices; l; l = l->next) {
usb = l->data;
- if ((ret = sl2_get_device_info(*usb, &dev_info)) < 0) {
+ if ((ret = sl2_get_device_info(di, *usb, &dev_info)) < 0) {
sr_warn("Failed to get device information: %d.", ret);
sr_usb_dev_inst_free(usb);
continue;
@@ -109,18 +104,17 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
sdi = g_malloc0(sizeof(struct sr_dev_inst));
sdi->status = SR_ST_INACTIVE;
- sdi->vendor = g_strdup(VENDOR_NAME);
- sdi->model = g_strdup(MODEL_NAME);
+ sdi->vendor = g_strdup("IKALOGIC");
+ sdi->model = g_strdup("Scanalogic-2");
sdi->version = g_strdup_printf("%u.%u", dev_info.fw_ver_major, dev_info.fw_ver_minor);
sdi->serial_num = g_strdup_printf("%d", dev_info.serial);
sdi->priv = devc;
- sdi->driver = di;
sdi->inst_type = SR_INST_USB;
sdi->conn = usb;
- for (i = 0; channel_names[i]; i++)
- devc->channels[i] = sr_channel_new(sdi, i, SR_CHANNEL_LOGIC,
- TRUE, channel_names[i]);
+ for (i = 0; i < ARRAY_SIZE(channel_names); i++)
+ devc->channels[i] = sr_channel_new(sdi, i,
+ SR_CHANNEL_LOGIC, TRUE, channel_names[i]);
devc->state = STATE_IDLE;
devc->next_state = STATE_IDLE;
@@ -153,62 +147,40 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
devc->xfer_data_out = devc->xfer_buf_out +
LIBUSB_CONTROL_SETUP_SIZE;
- drvc->instances = g_slist_append(drvc->instances, sdi);
devices = g_slist_append(devices, sdi);
}
g_slist_free(usb_devices);
- return devices;
-}
-
-static GSList *dev_list(const struct sr_dev_driver *di)
-{
- return ((struct drv_context *)(di->priv))->instances;
+ return std_scan_complete(di, devices);
}
-static void clear_dev_context(void *priv)
+static void clear_helper(struct dev_context *devc)
{
- struct dev_context *devc;
-
- devc = priv;
-
- sr_dbg("Device context cleared.");
-
libusb_free_transfer(devc->xfer_in);
libusb_free_transfer(devc->xfer_out);
- g_free(devc);
}
static int dev_clear(const struct sr_dev_driver *di)
{
- return std_dev_clear(di, &clear_dev_context);
+ return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
}
static int dev_open(struct sr_dev_inst *sdi)
{
struct sr_dev_driver *di = sdi->driver;
- struct drv_context *drvc;
+ struct drv_context *drvc = di->context;
struct dev_context *devc;
struct sr_usb_dev_inst *usb;
uint8_t buffer[PACKET_LENGTH];
int ret;
- if (!(drvc = di->priv)) {
- sr_err("Driver was not initialized.");
- return SR_ERR;
- }
-
usb = sdi->conn;
devc = sdi->priv;
if (sr_usb_open(drvc->sr_ctx->libusb_ctx, usb) != SR_OK)
return SR_ERR;
- /*
- * Determine if a kernel driver is active on this interface and, if so,
- * detach it.
- */
if (libusb_kernel_driver_active(usb->devhdl, USB_INTERFACE) == 1) {
ret = libusb_detach_kernel_driver(usb->devhdl, USB_INTERFACE);
if (ret < 0) {
@@ -226,11 +198,11 @@ static int dev_open(struct sr_dev_inst *sdi)
libusb_fill_control_transfer(devc->xfer_in, usb->devhdl,
devc->xfer_buf_in, sl2_receive_transfer_in,
- sdi, USB_TIMEOUT);
+ sdi, USB_TIMEOUT_MS);
libusb_fill_control_transfer(devc->xfer_out, usb->devhdl,
devc->xfer_buf_out, sl2_receive_transfer_out,
- sdi, USB_TIMEOUT);
+ sdi, USB_TIMEOUT_MS);
memset(buffer, 0, sizeof(buffer));
@@ -252,49 +224,33 @@ static int dev_open(struct sr_dev_inst *sdi)
return SR_ERR;
}
- sdi->status = SR_ST_ACTIVE;
-
return SR_OK;
}
static int dev_close(struct sr_dev_inst *sdi)
{
- struct sr_dev_driver *di = sdi->driver;
struct sr_usb_dev_inst *usb;
- if (!di->priv) {
- sr_err("Driver was not initialized.");
- return SR_ERR;
- }
-
usb = sdi->conn;
if (!usb->devhdl)
- return SR_OK;
+ return SR_ERR_BUG;
libusb_release_interface(usb->devhdl, USB_INTERFACE);
libusb_close(usb->devhdl);
usb->devhdl = NULL;
- sdi->status = SR_ST_INACTIVE;
return SR_OK;
}
-static int cleanup(const struct sr_dev_driver *di)
-{
- return dev_clear(di);
-}
-
-static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
- const struct sr_channel_group *cg)
+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;
- int ret;
(void)cg;
- ret = SR_OK;
devc = sdi->priv;
switch (key) {
@@ -308,84 +264,59 @@ static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *s
return SR_ERR_NA;
}
- return ret;
+ return SR_OK;
}
-static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
- const struct sr_channel_group *cg)
+static int config_set(uint32_t key, GVariant *data,
+ const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
{
- uint64_t samplerate, limit_samples, capture_ratio;
- int ret;
+ struct dev_context *devc;
+ uint64_t samplerate, limit_samples;
(void)cg;
- if (sdi->status != SR_ST_ACTIVE)
- return SR_ERR_DEV_CLOSED;
-
- ret = SR_OK;
+ devc = sdi->priv;
switch (key) {
case SR_CONF_LIMIT_SAMPLES:
limit_samples = g_variant_get_uint64(data);
- ret = sl2_set_limit_samples(sdi, limit_samples);
- break;
+ return sl2_set_limit_samples(sdi, limit_samples);
case SR_CONF_SAMPLERATE:
samplerate = g_variant_get_uint64(data);
- ret = sl2_set_samplerate(sdi, samplerate);
- break;
+ return sl2_set_samplerate(sdi, samplerate);
case SR_CONF_CAPTURE_RATIO:
- capture_ratio = g_variant_get_uint64(data);
- ret = sl2_set_capture_ratio(sdi, capture_ratio);
+ devc->capture_ratio = g_variant_get_uint64(data);
break;
default:
return SR_ERR_NA;
}
- return ret;
+ return SR_OK;
}
-static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
- const struct sr_channel_group *cg)
+static int config_list(uint32_t key, GVariant **data,
+ const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
{
- GVariant *gvar, *grange[2];
- GVariantBuilder gvb;
- int ret;
-
- (void)sdi;
- (void)cg;
-
- ret = SR_OK;
switch (key) {
case SR_CONF_DEVICE_OPTIONS:
- *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
- devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
- break;
+ return STD_CONFIG_LIST(key, data, sdi, cg, NO_OPTS, drvopts, devopts);
case SR_CONF_SAMPLERATE:
- g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
- gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
- sl2_samplerates, ARRAY_SIZE(sl2_samplerates),
- sizeof(uint64_t));
- g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
- *data = g_variant_builder_end(&gvb);
+ *data = std_gvar_samplerates(ARRAY_AND_SIZE(sl2_samplerates));
break;
case SR_CONF_TRIGGER_MATCH:
- *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
- trigger_matches, ARRAY_SIZE(trigger_matches),
- sizeof(int32_t));
+ *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
break;
case SR_CONF_LIMIT_SAMPLES:
- grange[0] = g_variant_new_uint64(0);
- grange[1] = g_variant_new_uint64(MAX_SAMPLES);
- *data = g_variant_new_tuple(grange, 2);
+ *data = std_gvar_tuple_u64(0, MAX_SAMPLES);
break;
default:
return SR_ERR_NA;
}
- return ret;
+ return SR_OK;
}
-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 drv_context *drvc;
@@ -394,13 +325,9 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
unsigned int i, j;
int ret;
- if (sdi->status != SR_ST_ACTIVE)
- return SR_ERR_DEV_CLOSED;
-
devc = sdi->priv;
- drvc = di->priv;
+ drvc = di->context;
- devc->cb_data = cb_data;
devc->wait_data_ready_locked = TRUE;
devc->stopping_in_progress = FALSE;
devc->transfer_error = FALSE;
@@ -466,38 +393,28 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
usb_source_add(sdi->session, drvc->sr_ctx, 100,
ikalogic_scanalogic2_receive_data, (void *)sdi);
- sr_dbg("Acquisition started successfully.");
-
- /* Send header packet to the session bus. */
- std_session_send_df_header(cb_data, LOG_PREFIX);
+ std_session_send_df_header(sdi);
devc->next_state = STATE_SAMPLE;
return SR_OK;
}
-static 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 (sdi->status != SR_ST_ACTIVE)
- return SR_ERR_DEV_CLOSED;
-
- sr_dbg("Stopping acquisition.");
-
sdi->status = SR_ST_STOPPING;
return SR_OK;
}
-SR_PRIV struct sr_dev_driver ikalogic_scanalogic2_driver_info = {
+static struct sr_dev_driver ikalogic_scanalogic2_driver_info = {
.name = "ikalogic-scanalogic2",
.longname = "IKALOGIC Scanalogic-2",
.api_version = 1,
- .init = init,
- .cleanup = cleanup,
+ .init = std_init,
+ .cleanup = std_cleanup,
.scan = scan,
- .dev_list = dev_list,
+ .dev_list = std_dev_list,
.dev_clear = dev_clear,
.config_get = config_get,
.config_set = config_set,
@@ -506,5 +423,6 @@ SR_PRIV struct sr_dev_driver ikalogic_scanalogic2_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(ikalogic_scanalogic2_driver_info);