]> sigrok.org Git - libsigrok.git/blobdiff - hardware/ikalogic-scanalogic2/api.c
build: Portability fixes.
[libsigrok.git] / hardware / ikalogic-scanalogic2 / api.c
index a9bb456f6914582da404683cda6076475d729abb..b4c697f91a8e7b715825465bbb4c00e82c0acd78 100644 (file)
@@ -23,11 +23,17 @@ static const int hwcaps[] = {
        SR_CONF_LOGIC_ANALYZER,
        SR_CONF_SAMPLERATE,
        SR_CONF_LIMIT_SAMPLES,
-       SR_CONF_TRIGGER_TYPE,
+       SR_CONF_TRIGGER_MATCH,
        SR_CONF_CAPTURE_RATIO,
 };
 
-SR_PRIV const uint64_t ikalogic_scanalogic2_samplerates[NUM_SAMPLERATES] = {
+static const int32_t trigger_matches[] = {
+       SR_TRIGGER_RISING,
+       SR_TRIGGER_FALLING,
+       SR_TRIGGER_EDGE,
+};
+
+SR_PRIV const uint64_t sl2_samplerates[NUM_SAMPLERATES] = {
        SR_KHZ(1.25),
        SR_KHZ(10),
        SR_KHZ(50),
@@ -41,7 +47,7 @@ SR_PRIV const uint64_t ikalogic_scanalogic2_samplerates[NUM_SAMPLERATES] = {
        SR_MHZ(20),
 };
 
-static const char *probe_names[NUM_PROBES + 1] = {
+static const char *channel_names[NUM_CHANNELS + 1] = {
        "0", "1", "2", "3",
        NULL,
 };
@@ -59,7 +65,7 @@ static GSList *scan(GSList *options)
        GSList *usb_devices, *devices, *l;
        struct drv_context *drvc;
        struct sr_dev_inst *sdi;
-       struct sr_probe *probe;
+       struct sr_channel *ch;
        struct dev_context *devc;
        struct sr_usb_dev_inst *usb;
        struct device_info dev_info;
@@ -81,9 +87,8 @@ static GSList *scan(GSList *options)
        for (l = usb_devices; l; l = l->next) {
                usb = l->data;
 
-               ret = ikalogic_scanalogic2_get_device_info(*usb, &dev_info);
-               if (ret != SR_OK) {
-                       sr_warn("Failed to get device information.");
+               if ((ret = sl2_get_device_info(*usb, &dev_info)) < 0) {
+                       sr_warn("Failed to get device information: %d.", ret);
                        sr_usb_dev_inst_free(usb);
                        continue;
                }
@@ -137,18 +142,18 @@ static GSList *scan(GSList *options)
                sdi->inst_type = SR_INST_USB;
                sdi->conn = usb;
 
-               for (i = 0; probe_names[i]; i++) {
-                       probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE,
-                               probe_names[i]);
-                       sdi->probes = g_slist_append(sdi->probes, probe);
-                       devc->probes[i] = probe;
+               for (i = 0; channel_names[i]; i++) {
+                       ch = sr_channel_new(i, SR_CHANNEL_LOGIC, TRUE,
+                               channel_names[i]);
+                       sdi->channels = g_slist_append(sdi->channels, ch);
+                       devc->channels[i] = ch;
                }
 
                devc->state = STATE_IDLE;
                devc->next_state = STATE_IDLE;
 
                /* Set default samplerate. */
-               ikalogic_scanalogic2_set_samplerate(sdi, DEFAULT_SAMPLERATE);
+               sl2_set_samplerate(sdi, DEFAULT_SAMPLERATE);
 
                /* Set default capture ratio. */
                devc->capture_ratio = 0;
@@ -162,7 +167,7 @@ static GSList *scan(GSList *options)
                        PACKET_LENGTH);
 
                libusb_fill_control_setup(devc->xfer_buf_in,
-                       USB_REQUEST_TYPE_IN, USB_HID_SET_REPORT,
+                       USB_REQUEST_TYPE_IN, USB_HID_GET_REPORT,
                        USB_HID_REPORT_TYPE_FEATURE, USB_INTERFACE,
                        PACKET_LENGTH);
                libusb_fill_control_setup(devc->xfer_buf_out,
@@ -235,32 +240,30 @@ static int dev_open(struct sr_dev_inst *sdi)
        if (libusb_kernel_driver_active(usb->devhdl, USB_INTERFACE) == 1) {
                ret = libusb_detach_kernel_driver(usb->devhdl, USB_INTERFACE);
                if (ret < 0) {
-                       sr_err("Failed to detach kernel driver: %i.",
+                       sr_err("Failed to detach kernel driver: %s.",
                                libusb_error_name(ret));
                        return SR_ERR;
                }
        }
 
-       ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
-       if (ret) {
+       if ((ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE)) < 0) {
                sr_err("Failed to claim interface: %s.",
                        libusb_error_name(ret));
                return SR_ERR;
        }
 
        libusb_fill_control_transfer(devc->xfer_in, usb->devhdl,
-               devc->xfer_buf_in, ikalogic_scanalogic2_receive_transfer_in,
+               devc->xfer_buf_in, sl2_receive_transfer_in,
                sdi, USB_TIMEOUT);
 
        libusb_fill_control_transfer(devc->xfer_out, usb->devhdl,
-               devc->xfer_buf_out, ikalogic_scanalogic2_receive_transfer_out,
+               devc->xfer_buf_out, sl2_receive_transfer_out,
                sdi, USB_TIMEOUT);
 
        memset(buffer, 0, sizeof(buffer));
 
        buffer[0] = CMD_RESET;
-       ret = ikalogic_scanalogic2_transfer_out(usb->devhdl, buffer);
-       if (ret != PACKET_LENGTH) {
+       if ((ret = sl2_transfer_out(usb->devhdl, buffer)) != PACKET_LENGTH) {
                sr_err("Device reset failed: %s.", libusb_error_name(ret));
                return SR_ERR;
        }
@@ -271,8 +274,7 @@ static int dev_open(struct sr_dev_inst *sdi)
         * and thereby close the connection.
         */
        buffer[0] = CMD_IDLE;
-       ret = ikalogic_scanalogic2_transfer_out(usb->devhdl, buffer);
-       if (ret != PACKET_LENGTH) {
+       if ((ret = sl2_transfer_out(usb->devhdl, buffer)) != PACKET_LENGTH) {
                sr_err("Failed to set device in idle state: %s.",
                        libusb_error_name(ret));
                return SR_ERR;
@@ -311,11 +313,14 @@ static int cleanup(void)
        return dev_clear();
 }
 
-static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi)
+static int config_get(int 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;
 
@@ -333,11 +338,14 @@ static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi)
        return ret;
 }
 
-static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi)
+static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
+               const struct sr_channel_group *cg)
 {
        uint64_t samplerate, limit_samples, capture_ratio;
        int ret;
 
+       (void)cg;
+
        if (sdi->status != SR_ST_ACTIVE)
                return SR_ERR_DEV_CLOSED;
 
@@ -346,17 +354,15 @@ static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi)
        switch (key) {
        case SR_CONF_LIMIT_SAMPLES:
                limit_samples = g_variant_get_uint64(data);
-               ret = ikalogic_scanalogic2_set_limit_samples(sdi,
-                       limit_samples);
+               ret = sl2_set_limit_samples(sdi, limit_samples);
                break;
        case SR_CONF_SAMPLERATE:
                samplerate = g_variant_get_uint64(data);
-               ret = ikalogic_scanalogic2_set_samplerate(sdi, samplerate);
+               ret = sl2_set_samplerate(sdi, samplerate);
                break;
        case SR_CONF_CAPTURE_RATIO:
                capture_ratio = g_variant_get_uint64(data);
-               ret = ikalogic_scanalogic2_set_capture_ratio(sdi,
-                       capture_ratio);
+               ret = sl2_set_capture_ratio(sdi, capture_ratio);
                break;
        default:
                return SR_ERR_NA;
@@ -365,13 +371,15 @@ static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi)
        return ret;
 }
 
-static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
+static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
+               const struct sr_channel_group *cg)
 {
-       GVariant *gvar;
+       GVariant *gvar, *grange[2];
        GVariantBuilder gvb;
        int ret;
 
        (void)sdi;
+       (void)cg;
 
        ret = SR_OK;
 
@@ -383,14 +391,20 @@ static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
        case SR_CONF_SAMPLERATE:
                g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
                gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
-                       ikalogic_scanalogic2_samplerates,
-                       ARRAY_SIZE(ikalogic_scanalogic2_samplerates),
+                       sl2_samplerates, ARRAY_SIZE(sl2_samplerates),
                        sizeof(uint64_t));
                g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
                *data = g_variant_builder_end(&gvb);
                break;
-       case SR_CONF_TRIGGER_TYPE:
-               *data = g_variant_new_string(TRIGGER_TYPES);
+       case SR_CONF_TRIGGER_MATCH:
+               *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
+                               trigger_matches, ARRAY_SIZE(trigger_matches),
+                               sizeof(int32_t));
+               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);
                break;
        default:
                return SR_ERR_NA;
@@ -401,7 +415,6 @@ static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
 
 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
 {
-       const struct libusb_pollfd **pfd;
        struct drv_context *drvc;
        struct dev_context *devc;
        uint16_t trigger_bytes, tmp;
@@ -426,8 +439,8 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
         * The trigger must be configured first because the calculation of the
         * pre and post trigger samples depends on a configured trigger.
         */
-       ikalogic_scanalogic2_configure_trigger(sdi);
-       ikalogic_scanalogic2_calculate_trigger_samples(sdi);
+       sl2_convert_trigger(sdi);
+       sl2_calculate_trigger_samples(sdi);
 
        trigger_bytes = devc->pre_trigger_bytes + devc->post_trigger_bytes;
 
@@ -438,21 +451,21 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
        if (trigger_bytes % PACKET_NUM_SAMPLE_BYTES != 0)
                devc->num_sample_packets++;
 
-       devc->num_enabled_probes = 0;
+       devc->num_enabled_channels = 0;
 
        /*
-        * Count the number of enabled probes and number them for a sequential
+        * Count the number of enabled channels and number them for a sequential
         * access.
         */
-       for (i = 0, j = 0; i < NUM_PROBES; i++) {
-               if (devc->probes[i]->enabled) {
-                       devc->num_enabled_probes++;
-                       devc->probe_map[j] = i;
+       for (i = 0, j = 0; i < NUM_CHANNELS; i++) {
+               if (devc->channels[i]->enabled) {
+                       devc->num_enabled_channels++;
+                       devc->channel_map[j] = i;
                        j++;
                }
        }
 
-       sr_dbg("Number of enabled probes: %i.", devc->num_enabled_probes);
+       sr_dbg("Number of enabled channels: %i.", devc->num_enabled_channels);
 
        /* Set up the transfer buffer for the acquisition. */
        devc->xfer_data_out[0] = CMD_SAMPLE;
@@ -472,35 +485,14 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
        tmp = GUINT16_TO_LE(devc->after_trigger_delay);
        memcpy(devc->xfer_data_out + 10, &tmp, sizeof(tmp));
 
-       if (!(pfd = libusb_get_pollfds(drvc->sr_ctx->libusb_ctx))) {
-               sr_err("libusb_get_pollfds failed.");
-               return SR_ERR;
-       }
-
-       /* Count the number of file descriptors. */
-       for (devc->num_usbfd = 0; pfd[devc->num_usbfd]; devc->num_usbfd++);
-
-       if (!(devc->usbfd = g_try_malloc(devc->num_usbfd * sizeof(int)))) {
-               sr_err("File descriptor array malloc failed.");
-               free(pfd);
-               return SR_ERR_MALLOC;
-       }
-
        if ((ret = libusb_submit_transfer(devc->xfer_out)) != 0) {
                sr_err("Submit transfer failed: %s.", libusb_error_name(ret));
-               g_free(devc->usbfd);
                return SR_ERR;
        }
 
-       for (i = 0; i < devc->num_usbfd; i++) {
-               sr_source_add(pfd[i]->fd, pfd[i]->events, 100,
+       usb_source_add(sdi->session, drvc->sr_ctx, 100,
                        ikalogic_scanalogic2_receive_data, (void *)sdi);
 
-               devc->usbfd[i] = pfd[i]->fd;
-       }
-
-       free(pfd);
-
        sr_dbg("Acquisition started successfully.");
 
        /* Send header packet to the session bus. */