2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 Marc Schink <sigrok-dev@marcschink.de>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 extern struct sr_dev_driver ikalogic_scanalogic2_driver_info;
25 extern uint64_t sl2_samplerates[NUM_SAMPLERATES];
27 static void stop_acquisition(struct sr_dev_inst *sdi)
29 struct drv_context *drvc = sdi->driver->context;
31 usb_source_remove(sdi->session, drvc->sr_ctx);
33 std_session_send_df_end(sdi, LOG_PREFIX);
35 sdi->status = SR_ST_ACTIVE;
38 static void abort_acquisition(struct sr_dev_inst *sdi)
40 struct drv_context *drvc = sdi->driver->context;
42 usb_source_remove(sdi->session, drvc->sr_ctx);
44 std_session_send_df_end(sdi, LOG_PREFIX);
46 sdi->driver->dev_close(sdi);
49 static void buffer_sample_data(const struct sr_dev_inst *sdi)
51 struct dev_context *devc;
52 unsigned int offset, packet_length;
56 if (devc->channels[devc->channel]->enabled) {
57 offset = devc->sample_packet * PACKET_NUM_SAMPLE_BYTES;
60 * Determine the packet length to ensure that the last packet
61 * will not exceed the buffer size.
63 packet_length = MIN(PACKET_NUM_SAMPLE_BYTES,
64 MAX_DEV_SAMPLE_BYTES - offset);
67 * Skip the first 4 bytes of the source buffer because they
68 * contain channel and packet information only.
70 memcpy(devc->sample_buffer[devc->channel] + offset,
71 devc->xfer_data_in + 4, packet_length);
75 static void process_sample_data(const struct sr_dev_inst *sdi)
77 struct dev_context *devc;
78 struct sr_datafeed_packet packet;
79 struct sr_datafeed_logic logic;
80 uint8_t i, j, tmp, buffer[PACKET_NUM_SAMPLES], *ptr[NUM_CHANNELS];
81 uint16_t offset, n = 0;
85 offset = devc->sample_packet * PACKET_NUM_SAMPLE_BYTES;
88 * Array of pointers to the sample data of all channels up to the last
89 * enabled one for an uniform access to them. Note that the currently
90 * received samples always belong to the last enabled channel.
92 for (i = 0; i < devc->num_enabled_channels - 1; i++)
93 ptr[i] = devc->sample_buffer[devc->channel_map[i]] + offset;
96 * Skip the first 4 bytes of the buffer because they contain channel
97 * and packet information only.
99 ptr[i] = devc->xfer_data_in + 4;
101 for (i = 0; i < PACKET_NUM_SAMPLE_BYTES; i++) {
102 /* Stop processing if all requested samples are processed. */
103 if (devc->samples_processed == devc->limit_samples)
108 if (devc->samples_processed == 0) {
110 * Adjust the position of the first sample to be
111 * processed because possibly more samples than
112 * necessary might have been acquired. This is because
113 * the number of acquired samples is always rounded up
114 * to a multiple of 8.
116 k = k - (devc->pre_trigger_bytes * 8) +
117 devc->pre_trigger_samples;
119 sr_dbg("Start processing at sample: %d.", 7 - k);
122 * Send the trigger before the first sample is
123 * processed if no pre trigger samples were calculated
124 * through the capture ratio.
126 if (devc->trigger_type != TRIGGER_TYPE_NONE &&
127 devc->pre_trigger_samples == 0) {
128 packet.type = SR_DF_TRIGGER;
129 sr_session_send(sdi, &packet);
133 for (; k >= 0; k--) {
135 * Stop processing if all requested samples are
138 if (devc->samples_processed == devc->limit_samples)
144 * Extract the current sample for each enabled channel
145 * and store them in the buffer.
147 for (j = 0; j < devc->num_enabled_channels; j++) {
148 tmp = (ptr[j][i] & (1 << k)) >> k;
149 buffer[n] |= tmp << devc->channel_map[j];
153 devc->samples_processed++;
156 * Send all processed samples and the trigger if the
157 * number of processed samples reaches the calculated
158 * number of pre trigger samples.
160 if (devc->samples_processed == devc->pre_trigger_samples &&
161 devc->trigger_type != TRIGGER_TYPE_NONE) {
162 packet.type = SR_DF_LOGIC;
163 packet.payload = &logic;
167 sr_session_send(sdi, &packet);
169 packet.type = SR_DF_TRIGGER;
170 sr_session_send(sdi, &packet);
178 packet.type = SR_DF_LOGIC;
179 packet.payload = &logic;
183 sr_session_send(sdi, &packet);
187 SR_PRIV int ikalogic_scanalogic2_receive_data(int fd, int revents, void *cb_data)
189 struct sr_dev_inst *sdi;
190 struct sr_dev_driver *di;
191 struct dev_context *devc;
192 struct drv_context *drvc;
194 int64_t current_time, time_elapsed;
200 if (!(sdi = cb_data))
203 if (!(devc = sdi->priv))
208 current_time = g_get_monotonic_time();
210 if (devc->state == STATE_WAIT_DATA_READY &&
211 !devc->wait_data_ready_locked) {
212 time_elapsed = current_time - devc->wait_data_ready_time;
215 * Check here for stopping in addition to the transfer
216 * callback functions to avoid waiting until the
217 * WAIT_DATA_READY_INTERVAL has expired.
219 if (sdi->status == SR_ST_STOPPING) {
220 if (!devc->stopping_in_progress) {
221 devc->next_state = STATE_RESET_AND_IDLE;
222 devc->stopping_in_progress = TRUE;
223 ret = libusb_submit_transfer(devc->xfer_in);
225 } else if (time_elapsed >= WAIT_DATA_READY_INTERVAL) {
226 devc->wait_data_ready_locked = TRUE;
227 ret = libusb_submit_transfer(devc->xfer_in);
232 sr_err("Submit transfer failed: %s.", libusb_error_name(ret));
233 abort_acquisition(sdi);
240 libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv,
243 /* Check if an error occurred on a transfer. */
244 if (devc->transfer_error)
245 abort_acquisition(sdi);
250 SR_PRIV void LIBUSB_CALL sl2_receive_transfer_in( struct libusb_transfer *transfer)
252 struct sr_dev_inst *sdi;
253 struct dev_context *devc;
254 uint8_t last_channel;
257 sdi = transfer->user_data;
260 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
261 sr_err("Transfer to device failed: %s.",
262 libusb_error_name(transfer->status));
263 devc->transfer_error = TRUE;
267 if (sdi->status == SR_ST_STOPPING && !devc->stopping_in_progress) {
268 devc->next_state = STATE_RESET_AND_IDLE;
269 devc->stopping_in_progress = TRUE;
271 if (libusb_submit_transfer(devc->xfer_in) != 0) {
272 sr_err("Submit transfer failed: %s.",
273 libusb_error_name(ret));
274 devc->transfer_error = TRUE;
280 if (devc->state != devc->next_state)
281 sr_spew("State changed from %i to %i.",
282 devc->state, devc->next_state);
283 devc->state = devc->next_state;
285 if (devc->state == STATE_WAIT_DATA_READY) {
286 /* Check if the received data are a valid device status. */
287 if (devc->xfer_data_in[0] == 0x05) {
288 if (devc->xfer_data_in[1] == STATUS_WAITING_FOR_TRIGGER)
289 sr_dbg("Waiting for trigger.");
290 else if (devc->xfer_data_in[1] == STATUS_SAMPLING)
291 sr_dbg("Sampling in progress.");
295 * Check if the received data are a valid device status and the
296 * sample data are ready.
298 if (devc->xfer_data_in[0] == 0x05 &&
299 devc->xfer_data_in[1] == STATUS_DATA_READY) {
300 devc->next_state = STATE_RECEIVE_DATA;
301 ret = libusb_submit_transfer(transfer);
303 devc->wait_data_ready_locked = FALSE;
304 devc->wait_data_ready_time = g_get_monotonic_time();
306 } else if (devc->state == STATE_RECEIVE_DATA) {
307 last_channel = devc->channel_map[devc->num_enabled_channels - 1];
309 if (devc->channel < last_channel) {
310 buffer_sample_data(sdi);
311 } else if (devc->channel == last_channel) {
312 process_sample_data(sdi);
315 * Stop acquisition because all samples of enabled
316 * channels are processed.
318 devc->next_state = STATE_RESET_AND_IDLE;
321 devc->sample_packet++;
322 devc->sample_packet %= devc->num_sample_packets;
324 if (devc->sample_packet == 0)
327 ret = libusb_submit_transfer(transfer);
328 } else if (devc->state == STATE_RESET_AND_IDLE) {
329 /* Check if the received data are a valid device status. */
330 if (devc->xfer_data_in[0] == 0x05) {
331 if (devc->xfer_data_in[1] == STATUS_DEVICE_READY) {
332 devc->next_state = STATE_IDLE;
333 devc->xfer_data_out[0] = CMD_IDLE;
335 devc->next_state = STATE_WAIT_DEVICE_READY;
336 devc->xfer_data_out[0] = CMD_RESET;
339 ret = libusb_submit_transfer(devc->xfer_out);
342 * The received device status is invalid which
343 * indicates that the device is not ready to accept
344 * commands. Request a new device status until a valid
345 * device status is received.
347 ret = libusb_submit_transfer(transfer);
349 } else if (devc->state == STATE_WAIT_DEVICE_READY) {
350 /* Check if the received data are a valid device status. */
351 if (devc->xfer_data_in[0] == 0x05) {
352 if (devc->xfer_data_in[1] == STATUS_DEVICE_READY) {
353 devc->next_state = STATE_IDLE;
354 devc->xfer_data_out[0] = CMD_IDLE;
357 * The received device status is valid but the
358 * device is not ready. Probably the device did
359 * not recognize the last reset. Reset the
362 devc->xfer_data_out[0] = CMD_RESET;
365 ret = libusb_submit_transfer(devc->xfer_out);
368 * The device is not ready and therefore not able to
369 * change to the idle state. Request a new device
370 * status until the device is ready.
372 ret = libusb_submit_transfer(transfer);
377 sr_err("Submit transfer failed: %s.", libusb_error_name(ret));
378 devc->transfer_error = TRUE;
382 SR_PRIV void LIBUSB_CALL sl2_receive_transfer_out( struct libusb_transfer *transfer)
384 struct sr_dev_inst *sdi;
385 struct dev_context *devc;
388 sdi = transfer->user_data;
391 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
392 sr_err("Transfer to device failed: %s.",
393 libusb_error_name(transfer->status));
394 devc->transfer_error = TRUE;
398 if (sdi->status == SR_ST_STOPPING && !devc->stopping_in_progress) {
399 devc->next_state = STATE_RESET_AND_IDLE;
400 devc->stopping_in_progress = TRUE;
402 if (libusb_submit_transfer(devc->xfer_in) != 0) {
403 sr_err("Submit transfer failed: %s.",
404 libusb_error_name(ret));
406 devc->transfer_error = TRUE;
412 if (devc->state != devc->next_state)
413 sr_spew("State changed from %i to %i.",
414 devc->state, devc->next_state);
415 devc->state = devc->next_state;
417 if (devc->state == STATE_IDLE) {
418 stop_acquisition(sdi);
419 } else if (devc->state == STATE_SAMPLE) {
420 devc->next_state = STATE_WAIT_DATA_READY;
421 ret = libusb_submit_transfer(devc->xfer_in);
422 } else if (devc->state == STATE_WAIT_DEVICE_READY) {
423 ret = libusb_submit_transfer(devc->xfer_in);
427 sr_err("Submit transfer failed: %s.", libusb_error_name(ret));
428 devc->transfer_error = TRUE;
432 SR_PRIV int sl2_set_samplerate(const struct sr_dev_inst *sdi,
435 struct dev_context *devc;
440 for (i = 0; i < NUM_SAMPLERATES; i++) {
441 if (sl2_samplerates[i] == samplerate) {
442 devc->samplerate = samplerate;
443 devc->samplerate_id = NUM_SAMPLERATES - i - 1;
451 SR_PRIV int sl2_set_limit_samples(const struct sr_dev_inst *sdi,
452 uint64_t limit_samples)
454 struct dev_context *devc;
458 if (limit_samples == 0) {
459 sr_err("Invalid number of limit samples: %" PRIu64 ".",
464 if (limit_samples > MAX_SAMPLES)
465 limit_samples = MAX_SAMPLES;
467 sr_dbg("Limit samples set to %" PRIu64 ".", limit_samples);
469 devc->limit_samples = limit_samples;
474 SR_PRIV int sl2_convert_trigger(const struct sr_dev_inst *sdi)
476 struct dev_context *devc;
477 struct sr_trigger *trigger;
478 struct sr_trigger_stage *stage;
479 struct sr_trigger_match *match;
481 int num_triggers_anyedge;
485 /* Disable the trigger by default. */
486 devc->trigger_channel = TRIGGER_CHANNEL_0;
487 devc->trigger_type = TRIGGER_TYPE_NONE;
489 if (!(trigger = sr_session_trigger_get(sdi->session)))
492 if (g_slist_length(trigger->stages) > 1) {
493 sr_err("This device only supports 1 trigger stage.");
497 num_triggers_anyedge = 0;
498 for (l = trigger->stages; l; l = l->next) {
500 for (m = stage->matches; m; m = m->next) {
502 if (!match->channel->enabled)
503 /* Ignore disabled channels with a trigger. */
505 devc->trigger_channel = match->channel->index + 1;
506 switch (match->match) {
507 case SR_TRIGGER_RISING:
508 devc->trigger_type = TRIGGER_TYPE_POSEDGE;
510 case SR_TRIGGER_FALLING:
511 devc->trigger_type = TRIGGER_TYPE_NEGEDGE;
513 case SR_TRIGGER_EDGE:
514 devc->trigger_type = TRIGGER_TYPE_ANYEDGE;
515 num_triggers_anyedge++;
522 * Set trigger to any edge on all channels if the trigger for each
523 * channel is set to any edge.
525 if (num_triggers_anyedge == NUM_CHANNELS) {
526 devc->trigger_channel = TRIGGER_CHANNEL_ALL;
527 devc->trigger_type = TRIGGER_TYPE_ANYEDGE;
530 sr_dbg("Trigger set to channel 0x%02x and type 0x%02x.",
531 devc->trigger_channel, devc->trigger_type);
536 SR_PRIV int sl2_set_capture_ratio(const struct sr_dev_inst *sdi,
537 uint64_t capture_ratio)
539 struct dev_context *devc;
543 if (capture_ratio > 100) {
544 sr_err("Invalid capture ratio: %" PRIu64 " %%.", capture_ratio);
548 sr_info("Capture ratio set to %" PRIu64 " %%.", capture_ratio);
550 devc->capture_ratio = capture_ratio;
555 SR_PRIV int sl2_set_after_trigger_delay(const struct sr_dev_inst *sdi,
556 uint64_t after_trigger_delay)
558 struct dev_context *devc;
562 if (after_trigger_delay > MAX_AFTER_TRIGGER_DELAY) {
563 sr_err("Invalid after trigger delay: %" PRIu64 " ms.",
564 after_trigger_delay);
568 sr_info("After trigger delay set to %" PRIu64 " ms.",
569 after_trigger_delay);
571 devc->after_trigger_delay = after_trigger_delay;
576 SR_PRIV void sl2_calculate_trigger_samples(const struct sr_dev_inst *sdi)
578 struct dev_context *devc;
579 uint64_t pre_trigger_samples, post_trigger_samples;
580 uint16_t pre_trigger_bytes, post_trigger_bytes;
584 cr = devc->capture_ratio;
586 /* Ignore the capture ratio if no trigger is enabled. */
587 if (devc->trigger_type == TRIGGER_TYPE_NONE)
590 pre_trigger_samples = (devc->limit_samples * cr) / 100;
591 post_trigger_samples = (devc->limit_samples * (100 - cr)) / 100;
594 * Increase the number of post trigger samples by one to compensate the
595 * possible loss of a sample through integer rounding.
597 if (pre_trigger_samples + post_trigger_samples != devc->limit_samples)
598 post_trigger_samples++;
601 * The device requires the number of samples in multiples of 8 which
602 * will also be called sample bytes in the following.
604 pre_trigger_bytes = pre_trigger_samples / 8;
605 post_trigger_bytes = post_trigger_samples / 8;
608 * Round up the number of sample bytes to ensure that at least the
609 * requested number of samples will be acquired. Note that due to this
610 * rounding the buffer to store these sample bytes needs to be at least
611 * one sample byte larger than the minimal number of sample bytes
612 * needed to store the requested samples.
614 if (pre_trigger_samples % 8 != 0)
617 if (post_trigger_samples % 8 != 0)
618 post_trigger_bytes++;
620 sr_info("Pre trigger samples: %" PRIu64 ".", pre_trigger_samples);
621 sr_info("Post trigger samples: %" PRIu64 ".", post_trigger_samples);
622 sr_dbg("Pre trigger sample bytes: %" PRIu16 ".", pre_trigger_bytes);
623 sr_dbg("Post trigger sample bytes: %" PRIu16 ".", post_trigger_bytes);
625 devc->pre_trigger_samples = pre_trigger_samples;
626 devc->pre_trigger_bytes = pre_trigger_bytes;
627 devc->post_trigger_bytes = post_trigger_bytes;
630 SR_PRIV int sl2_get_device_info(struct sr_usb_dev_inst usb,
631 struct device_info *dev_info)
633 struct drv_context *drvc;
634 uint8_t buffer[PACKET_LENGTH];
637 drvc = ikalogic_scanalogic2_driver_info.context;
642 if (sr_usb_open(drvc->sr_ctx->libusb_ctx, &usb) != SR_OK)
646 * Determine if a kernel driver is active on this interface and, if so,
649 if (libusb_kernel_driver_active(usb.devhdl, USB_INTERFACE) == 1) {
650 ret = libusb_detach_kernel_driver(usb.devhdl,
654 sr_err("Failed to detach kernel driver: %s.",
655 libusb_error_name(ret));
656 libusb_close(usb.devhdl);
661 ret = libusb_claim_interface(usb.devhdl, USB_INTERFACE);
664 sr_err("Failed to claim interface: %s.",
665 libusb_error_name(ret));
666 libusb_close(usb.devhdl);
670 memset(buffer, 0, sizeof(buffer));
673 * Reset the device to ensure it is in a proper state to request the
674 * device information.
676 buffer[0] = CMD_RESET;
677 if ((ret = sl2_transfer_out(usb.devhdl, buffer)) != PACKET_LENGTH) {
678 sr_err("Resetting of device failed: %s.",
679 libusb_error_name(ret));
680 libusb_release_interface(usb.devhdl, USB_INTERFACE);
681 libusb_close(usb.devhdl);
685 buffer[0] = CMD_INFO;
686 if ((ret = sl2_transfer_out(usb.devhdl, buffer)) != PACKET_LENGTH) {
687 sr_err("Requesting of device information failed: %s.",
688 libusb_error_name(ret));
689 libusb_release_interface(usb.devhdl, USB_INTERFACE);
690 libusb_close(usb.devhdl);
694 if ((ret = sl2_transfer_in(usb.devhdl, buffer)) != PACKET_LENGTH) {
695 sr_err("Receiving of device information failed: %s.",
696 libusb_error_name(ret));
697 libusb_release_interface(usb.devhdl, USB_INTERFACE);
698 libusb_close(usb.devhdl);
702 memcpy(&(dev_info->serial), buffer + 1, sizeof(uint32_t));
703 dev_info->serial = GUINT32_FROM_LE(dev_info->serial);
705 dev_info->fw_ver_major = buffer[5];
706 dev_info->fw_ver_minor = buffer[6];
708 buffer[0] = CMD_RESET;
709 if ((ret = sl2_transfer_out(usb.devhdl, buffer)) != PACKET_LENGTH) {
710 sr_err("Device reset failed: %s.", libusb_error_name(ret));
711 libusb_release_interface(usb.devhdl, USB_INTERFACE);
712 libusb_close(usb.devhdl);
717 * Set the device to idle state. If the device is not in idle state it
718 * possibly will reset itself after a few seconds without being used
719 * and thereby close the connection.
721 buffer[0] = CMD_IDLE;
722 if ((ret = sl2_transfer_out(usb.devhdl, buffer)) != PACKET_LENGTH) {
723 sr_err("Failed to set device in idle state: %s.",
724 libusb_error_name(ret));
725 libusb_release_interface(usb.devhdl, USB_INTERFACE);
726 libusb_close(usb.devhdl);
730 ret = libusb_release_interface(usb.devhdl, USB_INTERFACE);
733 sr_err("Failed to release interface: %s.",
734 libusb_error_name(ret));
735 libusb_close(usb.devhdl);
739 libusb_close(usb.devhdl);
744 SR_PRIV int sl2_transfer_in(libusb_device_handle *dev_handle, uint8_t *data)
746 return libusb_control_transfer(dev_handle, USB_REQUEST_TYPE_IN,
747 USB_HID_GET_REPORT, USB_HID_REPORT_TYPE_FEATURE, USB_INTERFACE,
748 (unsigned char *)data, PACKET_LENGTH, USB_TIMEOUT_MS);
751 SR_PRIV int sl2_transfer_out(libusb_device_handle *dev_handle, uint8_t *data)
753 return libusb_control_transfer(dev_handle, USB_REQUEST_TYPE_OUT,
754 USB_HID_SET_REPORT, USB_HID_REPORT_TYPE_FEATURE, USB_INTERFACE,
755 (unsigned char *)data, PACKET_LENGTH, USB_TIMEOUT_MS);