]> sigrok.org Git - libsigrok.git/blobdiff - hardware/ikalogic-scanalogic2/protocol.c
Replace 'probe' with 'channel' in most places.
[libsigrok.git] / hardware / ikalogic-scanalogic2 / protocol.c
index f6c0b2782cdc8cc1109baaaecfa4d135ca264348..dd460d42d0f6fecab04455b880b214ac15ef9fd2 100644 (file)
 extern struct sr_dev_driver ikalogic_scanalogic2_driver_info;
 static struct sr_dev_driver *di = &ikalogic_scanalogic2_driver_info;
 
-extern uint64_t ikalogic_scanalogic2_samplerates[NUM_SAMPLERATES];
+extern uint64_t sl2_samplerates[NUM_SAMPLERATES];
 
 static void stop_acquisition(struct sr_dev_inst *sdi)
 {
+       struct drv_context *drvc = sdi->driver->priv;
        struct dev_context *devc;
        struct sr_datafeed_packet packet;
-       unsigned int i;
 
        devc = sdi->priv;
 
        /* Remove USB file descriptors from polling. */
-       for (i = 0; i < devc->num_usbfd; i++)
-               sr_source_remove(devc->usbfd[i]);
-
-       g_free(devc->usbfd);
+       usb_source_remove(drvc->sr_ctx);
 
        packet.type = SR_DF_END;
        sr_session_send(devc->cb_data, &packet);
@@ -46,17 +43,14 @@ static void stop_acquisition(struct sr_dev_inst *sdi)
 
 static void abort_acquisition(struct sr_dev_inst *sdi)
 {
+       struct drv_context *drvc = sdi->driver->priv;
        struct dev_context *devc;
        struct sr_datafeed_packet packet;
-       unsigned int i;
 
        devc = sdi->priv;
 
        /* Remove USB file descriptors from polling. */
-       for (i = 0; i < devc->num_usbfd; i++)
-               sr_source_remove(devc->usbfd[i]);
-
-       g_free(devc->usbfd);
+       usb_source_remove(drvc->sr_ctx);
 
        packet.type = SR_DF_END;
        sr_session_send(devc->cb_data, &packet);
@@ -71,7 +65,7 @@ static void buffer_sample_data(const struct sr_dev_inst *sdi)
 
        devc = sdi->priv;
 
-       if (devc->probes[devc->channel]->enabled) {
+       if (devc->channels[devc->channel]->enabled) {
                offset = devc->sample_packet * PACKET_NUM_SAMPLE_BYTES;
 
                /*
@@ -107,12 +101,12 @@ static void process_sample_data(const struct sr_dev_inst *sdi)
         * enabled one for an uniform access to them. Note that the currently
         * received samples always belong to the last enabled channel.
         */
-       for (i = 0; i < devc->num_enabled_probes - 1; i++)
-               ptr[i] = devc->sample_buffer[devc->probe_map[i]] + offset;
+       for (i = 0; i < devc->num_enabled_channels - 1; i++)
+               ptr[i] = devc->sample_buffer[devc->channel_map[i]] + offset;
 
        /*
-        * Skip the first 4 bytes of the buffer because they contain channel and
-        * packet information only.
+        * Skip the first 4 bytes of the buffer because they contain channel
+        * and packet information only.
         */
        ptr[i] = devc->xfer_data_in + 4;
 
@@ -127,20 +121,19 @@ static void process_sample_data(const struct sr_dev_inst *sdi)
                        /*
                         * Adjust the position of the first sample to be
                         * processed because possibly more samples than
-                        * necessary might have been aquired. This is because
-                        * the number of aquired samples is always rounded up to
-                        * a multiple of 8.
+                        * necessary might have been acquired. This is because
+                        * the number of acquired samples is always rounded up
+                        * to a multiple of 8.
                         */
                        k = k - (devc->pre_trigger_bytes * 8) +
                                devc->pre_trigger_samples;
 
-                       sr_dbg("Start processing at sample: %" PRIu8 ".",
-                               7 - k);
+                       sr_dbg("Start processing at sample: %d.", 7 - k);
 
                        /*
-                        * Send the trigger before the first sample is processed
-                        * if no pre trigger samples were calculated through the
-                        * capture ratio.
+                        * Send the trigger before the first sample is
+                        * processed if no pre trigger samples were calculated
+                        * through the capture ratio.
                         */
                        if (devc->trigger_type != TRIGGER_TYPE_NONE &&
                                        devc->pre_trigger_samples == 0) {
@@ -163,9 +156,9 @@ static void process_sample_data(const struct sr_dev_inst *sdi)
                         * Extract the current sample for each enabled channel
                         * and store them in the buffer.
                         */
-                       for (j = 0; j < devc->num_enabled_probes; j++) {
+                       for (j = 0; j < devc->num_enabled_channels; j++) {
                                tmp = (ptr[j][i] & (1 << k)) >> k;
-                               buffer[n] |= tmp << devc->probe_map[j];
+                               buffer[n] |= tmp << devc->channel_map[j];
                        }
 
                        n++;
@@ -203,8 +196,7 @@ static void process_sample_data(const struct sr_dev_inst *sdi)
        }
 }
 
-SR_PRIV int ikalogic_scanalogic2_receive_data(int fd, int revents,
-               void *cb_data)
+SR_PRIV int ikalogic_scanalogic2_receive_data(int fd, int revents, void *cb_data)
 {
        struct sr_dev_inst *sdi;
        struct dev_context *devc;
@@ -230,9 +222,9 @@ SR_PRIV int ikalogic_scanalogic2_receive_data(int fd, int revents,
                time_elapsed = current_time - devc->wait_data_ready_time;
 
                /*
-                * Check here for stopping in addition to the transfer callback
-                * functions to avoid waiting until the WAIT_DATA_READY_INTERVAL
-                * has expired.
+                * Check here for stopping in addition to the transfer
+                * callback functions to avoid waiting until the
+                * WAIT_DATA_READY_INTERVAL has expired.
                 */
                if (sdi->status == SR_ST_STOPPING) {
                        if (!devc->stopping_in_progress) {
@@ -247,7 +239,7 @@ SR_PRIV int ikalogic_scanalogic2_receive_data(int fd, int revents,
        }
 
        if (ret != 0) {
-               sr_err("Submit transfer failed: %s", libusb_error_name(ret));
+               sr_err("Submit transfer failed: %s.", libusb_error_name(ret));
                abort_acquisition(sdi);
                return TRUE;
        }
@@ -259,15 +251,13 @@ SR_PRIV int ikalogic_scanalogic2_receive_data(int fd, int revents,
                NULL);
 
        /* Check if an error occurred on a transfer. */
-       if (devc->transfer_error) {
+       if (devc->transfer_error)
                abort_acquisition(sdi);
-       }
 
        return TRUE;
 }
 
-SR_PRIV void ikalogic_scanalogic2_receive_transfer_in(
-               struct libusb_transfer *transfer)
+SR_PRIV void sl2_receive_transfer_in( struct libusb_transfer *transfer)
 {
        struct sr_dev_inst *sdi;
        struct dev_context *devc;
@@ -278,7 +268,7 @@ SR_PRIV void ikalogic_scanalogic2_receive_transfer_in(
        devc = sdi->priv;
 
        if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
-               sr_err("Transfer to device failed: %i", transfer->status);
+               sr_err("Transfer to device failed: %i.", transfer->status);
                devc->transfer_error = TRUE;
                return;
        }
@@ -288,7 +278,7 @@ SR_PRIV void ikalogic_scanalogic2_receive_transfer_in(
                devc->stopping_in_progress = TRUE;
 
                if (libusb_submit_transfer(devc->xfer_in) != 0) {
-                       sr_err("Submit transfer failed: %s",
+                       sr_err("Submit transfer failed: %s.",
                                libusb_error_name(ret));
                        devc->transfer_error = TRUE;
                }
@@ -296,7 +286,9 @@ SR_PRIV void ikalogic_scanalogic2_receive_transfer_in(
                return;
        }
 
-       sr_dbg("State changed from %i to %i.", devc->state, devc->next_state);
+       if (devc->state != devc->next_state)
+               sr_spew("State changed from %i to %i.",
+                       devc->state, devc->next_state);
        devc->state = devc->next_state;
 
        if (devc->state == STATE_WAIT_DATA_READY) {
@@ -321,7 +313,7 @@ SR_PRIV void ikalogic_scanalogic2_receive_transfer_in(
                        devc->wait_data_ready_time = g_get_monotonic_time();
                }
        } else if (devc->state == STATE_RECEIVE_DATA) {
-               last_channel = devc->probe_map[devc->num_enabled_probes - 1];
+               last_channel = devc->channel_map[devc->num_enabled_channels - 1];
 
                if (devc->channel < last_channel) {
                        buffer_sample_data(sdi);
@@ -330,7 +322,7 @@ SR_PRIV void ikalogic_scanalogic2_receive_transfer_in(
                } else {
                        /*
                         * Stop acquisition because all samples of enabled
-                        * probes are processed.
+                        * channels are processed.
                         */
                        devc->next_state = STATE_RESET_AND_IDLE;
                }
@@ -356,10 +348,10 @@ SR_PRIV void ikalogic_scanalogic2_receive_transfer_in(
                        ret = libusb_submit_transfer(devc->xfer_out);
                } else {
                        /*
-                        * The received device status is invalid which indicates
-                        * that the device is not ready to accept commands.
-                        * Request a new device status until a valid device
-                        * status is received.
+                        * The received device status is invalid which
+                        * indicates that the device is not ready to accept
+                        * commands. Request a new device status until a valid
+                        * device status is received.
                         */
                        ret = libusb_submit_transfer(transfer);
                }
@@ -383,21 +375,20 @@ SR_PRIV void ikalogic_scanalogic2_receive_transfer_in(
                } else {
                        /*
                         * The device is not ready and therefore not able to
-                        * change to the idle state. Request a new device status
-                        * until the device is ready.
+                        * change to the idle state. Request a new device
+                        * status until the device is ready.
                         */
                        ret = libusb_submit_transfer(transfer);
                }
        }
 
        if (ret != 0) {
-               sr_err("Submit transfer failed: %s", libusb_error_name(ret));
+               sr_err("Submit transfer failed: %s.", libusb_error_name(ret));
                devc->transfer_error = TRUE;
        }
 }
 
-SR_PRIV void ikalogic_scanalogic2_receive_transfer_out(
-               struct libusb_transfer *transfer)
+SR_PRIV void sl2_receive_transfer_out( struct libusb_transfer *transfer)
 {
        struct sr_dev_inst *sdi;
        struct dev_context *devc;
@@ -407,7 +398,7 @@ SR_PRIV void ikalogic_scanalogic2_receive_transfer_out(
        devc = sdi->priv;
 
        if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
-               sr_err("Transfer to device failed: %i", transfer->status);
+               sr_err("Transfer to device failed: %i.", transfer->status);
                devc->transfer_error = TRUE;
                return;
        }
@@ -417,7 +408,7 @@ SR_PRIV void ikalogic_scanalogic2_receive_transfer_out(
                devc->stopping_in_progress = TRUE;
 
                if (libusb_submit_transfer(devc->xfer_in) != 0) {
-                       sr_err("Submit transfer failed: %s",
+                       sr_err("Submit transfer failed: %s.",
                                libusb_error_name(ret));
 
                        devc->transfer_error = TRUE;
@@ -426,7 +417,9 @@ SR_PRIV void ikalogic_scanalogic2_receive_transfer_out(
                return;
        }
 
-       sr_dbg("State changed from %i to %i.", devc->state, devc->next_state);
+       if (devc->state != devc->next_state)
+               sr_spew("State changed from %i to %i.",
+                       devc->state, devc->next_state);
        devc->state = devc->next_state;
 
        if (devc->state == STATE_IDLE) {
@@ -439,12 +432,12 @@ SR_PRIV void ikalogic_scanalogic2_receive_transfer_out(
        }
 
        if (ret != 0) {
-               sr_err("Submit transfer failed: %s", libusb_error_name(ret));
+               sr_err("Submit transfer failed: %s.", libusb_error_name(ret));
                devc->transfer_error = TRUE;
        }
 }
 
-SR_PRIV int ikalogic_scanalogic2_set_samplerate(const struct sr_dev_inst *sdi,
+SR_PRIV int sl2_set_samplerate(const struct sr_dev_inst *sdi,
                uint64_t samplerate)
 {
        struct dev_context *devc;
@@ -453,7 +446,7 @@ SR_PRIV int ikalogic_scanalogic2_set_samplerate(const struct sr_dev_inst *sdi,
        devc = sdi->priv;
 
        for (i = 0; i < NUM_SAMPLERATES; i++) {
-               if (ikalogic_scanalogic2_samplerates[i] == samplerate) {
+               if (sl2_samplerates[i] == samplerate) {
                        devc->samplerate = samplerate;
                        devc->samplerate_id = NUM_SAMPLERATES - i - 1;
                        return SR_OK;
@@ -463,8 +456,8 @@ SR_PRIV int ikalogic_scanalogic2_set_samplerate(const struct sr_dev_inst *sdi,
        return SR_ERR_ARG;
 }
 
-SR_PRIV int ikalogic_scanalogic2_set_limit_samples(
-               const struct sr_dev_inst *sdi, uint64_t limit_samples)
+SR_PRIV int sl2_set_limit_samples(const struct sr_dev_inst *sdi,
+                                 uint64_t limit_samples)
 {
        struct dev_context *devc;
 
@@ -479,20 +472,19 @@ SR_PRIV int ikalogic_scanalogic2_set_limit_samples(
        if (limit_samples > MAX_SAMPLES)
                limit_samples = MAX_SAMPLES;
 
-       sr_info("Limit samples set to %" PRIu64 ".", limit_samples);
+       sr_dbg("Limit samples set to %" PRIu64 ".", limit_samples);
 
        devc->limit_samples = limit_samples;
 
        return SR_OK;
 }
 
-SR_PRIV void ikalogic_scanalogic2_configure_trigger(
-               const struct sr_dev_inst *sdi)
+SR_PRIV void sl2_configure_trigger(const struct sr_dev_inst *sdi)
 {
        struct dev_context *devc;
-       struct sr_probe *probe;
+       struct sr_channel *ch;
        uint8_t trigger_type;
-       int probe_index, num_triggers_anyedge;
+       int channel_index, num_triggers_anyedge;
        char *trigger;
        GSList *l;
 
@@ -504,11 +496,11 @@ SR_PRIV void ikalogic_scanalogic2_configure_trigger(
 
        num_triggers_anyedge = 0;
 
-       for (l = sdi->probes, probe_index = 0; l; l = l->next, probe_index++) {
-               probe = l->data;
-               trigger = probe->trigger;
+       for (l = sdi->channels, channel_index = 0; l; l = l->next, channel_index++) {
+               ch = l->data;
+               trigger = ch->trigger;
 
-               if (!trigger || !probe->enabled)
+               if (!trigger || !ch->enabled)
                        continue;
 
                switch (*trigger) {
@@ -526,7 +518,7 @@ SR_PRIV void ikalogic_scanalogic2_configure_trigger(
                        continue;
                }
 
-               devc->trigger_channel = probe_index + 1;
+               devc->trigger_channel = channel_index + 1;
                devc->trigger_type = trigger_type;
        }
 
@@ -539,12 +531,12 @@ SR_PRIV void ikalogic_scanalogic2_configure_trigger(
                devc->trigger_type = TRIGGER_TYPE_ANYEDGE;
        }
 
-       sr_dbg("Trigger set to channel %" PRIu8 " and type %" PRIu8 ".",
+       sr_dbg("Trigger set to channel 0x%02x and type 0x%02x.",
                devc->trigger_channel, devc->trigger_type);
 }
 
-SR_PRIV int ikalogic_scanalogic2_set_capture_ratio(
-               const struct sr_dev_inst *sdi, uint64_t capture_ratio)
+SR_PRIV int sl2_set_capture_ratio(const struct sr_dev_inst *sdi,
+                                 uint64_t capture_ratio)
 {
        struct dev_context *devc;
 
@@ -562,8 +554,8 @@ SR_PRIV int ikalogic_scanalogic2_set_capture_ratio(
        return SR_OK;
 }
 
-SR_PRIV int ikaloigc_scanalogic2_set_after_trigger_delay(
-               const struct sr_dev_inst *sdi, uint64_t after_trigger_delay)
+SR_PRIV int sl2_set_after_trigger_delay(const struct sr_dev_inst *sdi,
+                                       uint64_t after_trigger_delay)
 {
        struct dev_context *devc;
 
@@ -583,8 +575,7 @@ SR_PRIV int ikaloigc_scanalogic2_set_after_trigger_delay(
        return SR_OK;
 }
 
-SR_PRIV void ikalogic_scanalogic2_calculate_trigger_samples(
-               const struct sr_dev_inst *sdi)
+SR_PRIV void sl2_calculate_trigger_samples(const struct sr_dev_inst *sdi)
 {
        struct dev_context *devc;
        uint64_t pre_trigger_samples, post_trigger_samples;
@@ -619,8 +610,8 @@ SR_PRIV void ikalogic_scanalogic2_calculate_trigger_samples(
         * Round up the number of sample bytes to ensure that at least the
         * requested number of samples will be acquired. Note that due to this
         * rounding the buffer to store these sample bytes needs to be at least
-        * one sample byte larger than the minimal number of sample bytes needed
-        * to store the requested samples.
+        * one sample byte larger than the minimal number of sample bytes
+        * needed to store the requested samples.
         */
        if (pre_trigger_samples % 8 != 0)
                pre_trigger_bytes++;
@@ -638,7 +629,7 @@ SR_PRIV void ikalogic_scanalogic2_calculate_trigger_samples(
        devc->post_trigger_bytes = post_trigger_bytes;
 }
 
-SR_PRIV int ikalogic_scanalogic2_get_device_info(struct sr_usb_dev_inst usb,
+SR_PRIV int sl2_get_device_info(struct sr_usb_dev_inst usb,
                struct device_info *dev_info)
 {
        struct drv_context *drvc;
@@ -662,7 +653,7 @@ SR_PRIV int ikalogic_scanalogic2_get_device_info(struct sr_usb_dev_inst usb,
                        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));
                        libusb_close(usb.devhdl);
                        return SR_ERR;
@@ -685,9 +676,7 @@ SR_PRIV int ikalogic_scanalogic2_get_device_info(struct sr_usb_dev_inst usb,
         * device information.
         */
        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("Resetting of device failed: %s.",
                        libusb_error_name(ret));
                libusb_release_interface(usb.devhdl, USB_INTERFACE);
@@ -696,9 +685,7 @@ SR_PRIV int ikalogic_scanalogic2_get_device_info(struct sr_usb_dev_inst usb,
        }
 
        buffer[0] = CMD_INFO;
-       ret = ikalogic_scanalogic2_transfer_out(usb.devhdl, buffer);
-
-       if (ret != PACKET_LENGTH) {
+       if ((ret = sl2_transfer_out(usb.devhdl, buffer)) != PACKET_LENGTH) {
                sr_err("Requesting of device information failed: %s.",
                        libusb_error_name(ret));
                libusb_release_interface(usb.devhdl, USB_INTERFACE);
@@ -706,9 +693,7 @@ SR_PRIV int ikalogic_scanalogic2_get_device_info(struct sr_usb_dev_inst usb,
                return SR_ERR;
        }
 
-       ret = ikalogic_scanalogic2_transfer_in(usb.devhdl, buffer);
-
-       if (ret != PACKET_LENGTH) {
+       if ((ret = sl2_transfer_in(usb.devhdl, buffer)) != PACKET_LENGTH) {
                sr_err("Receiving of device information failed: %s.",
                        libusb_error_name(ret));
                libusb_release_interface(usb.devhdl, USB_INTERFACE);
@@ -723,9 +708,7 @@ SR_PRIV int ikalogic_scanalogic2_get_device_info(struct sr_usb_dev_inst usb,
        dev_info->fw_ver_minor = buffer[6];
 
        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));
                libusb_release_interface(usb.devhdl, USB_INTERFACE);
                libusb_close(usb.devhdl);
@@ -734,13 +717,11 @@ SR_PRIV int ikalogic_scanalogic2_get_device_info(struct sr_usb_dev_inst usb,
 
        /*
         * Set the device to idle state. If the device is not in idle state it
-        * possibly will reset itself after a few seconds without being used and
-        * thereby close the connection.
+        * possibly will reset itself after a few seconds without being used
+        * 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));
                libusb_release_interface(usb.devhdl, USB_INTERFACE);
@@ -751,7 +732,7 @@ SR_PRIV int ikalogic_scanalogic2_get_device_info(struct sr_usb_dev_inst usb,
        ret = libusb_release_interface(usb.devhdl, USB_INTERFACE);
 
        if (ret < 0) {
-               sr_err("Failed to release interface: %i.",
+               sr_err("Failed to release interface: %s.",
                        libusb_error_name(ret));
                libusb_close(usb.devhdl);
                return SR_ERR;
@@ -762,18 +743,16 @@ SR_PRIV int ikalogic_scanalogic2_get_device_info(struct sr_usb_dev_inst usb,
        return SR_OK;
 }
 
-SR_PRIV int ikalogic_scanalogic2_transfer_in(libusb_device_handle *dev_handle,
-               unsigned char *data)
+SR_PRIV int sl2_transfer_in(libusb_device_handle *dev_handle, uint8_t *data)
 {
        return libusb_control_transfer(dev_handle, USB_REQUEST_TYPE_IN,
-               USB_HID_SET_REPORT, USB_HID_REPORT_TYPE_FEATURE, USB_INTERFACE,
-               data, PACKET_LENGTH, USB_TIMEOUT);
+               USB_HID_GET_REPORT, USB_HID_REPORT_TYPE_FEATURE, USB_INTERFACE,
+               (unsigned char *)data, PACKET_LENGTH, USB_TIMEOUT);
 }
 
-SR_PRIV int ikalogic_scanalogic2_transfer_out(libusb_device_handle *dev_handle,
-               unsigned char *data)
+SR_PRIV int sl2_transfer_out(libusb_device_handle *dev_handle, uint8_t *data)
 {
        return libusb_control_transfer(dev_handle, USB_REQUEST_TYPE_OUT,
                USB_HID_SET_REPORT, USB_HID_REPORT_TYPE_FEATURE, USB_INTERFACE,
-               data, PACKET_LENGTH, USB_TIMEOUT);
+               (unsigned char *)data, PACKET_LENGTH, USB_TIMEOUT);
 }