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;
30 struct dev_context *devc;
31 struct sr_datafeed_packet packet;
35 /* Remove USB file descriptors from polling. */
36 usb_source_remove(sdi->session, drvc->sr_ctx);
38 packet.type = SR_DF_END;
39 sr_session_send(devc->cb_data, &packet);
41 sdi->status = SR_ST_ACTIVE;
44 static void abort_acquisition(struct sr_dev_inst *sdi)
46 struct drv_context *drvc = sdi->driver->context;
47 struct dev_context *devc;
48 struct sr_datafeed_packet packet;
52 /* Remove USB file descriptors from polling. */
53 usb_source_remove(sdi->session, drvc->sr_ctx);
55 packet.type = SR_DF_END;
56 sr_session_send(devc->cb_data, &packet);
58 sdi->driver->dev_close(sdi);
61 static void buffer_sample_data(const struct sr_dev_inst *sdi)
63 struct dev_context *devc;
64 unsigned int offset, packet_length;
68 if (devc->channels[devc->channel]->enabled) {
69 offset = devc->sample_packet * PACKET_NUM_SAMPLE_BYTES;
72 * Determine the packet length to ensure that the last packet
73 * will not exceed the buffer size.
75 packet_length = MIN(PACKET_NUM_SAMPLE_BYTES,
76 MAX_DEV_SAMPLE_BYTES - offset);
79 * Skip the first 4 bytes of the source buffer because they
80 * contain channel and packet information only.
82 memcpy(devc->sample_buffer[devc->channel] + offset,
83 devc->xfer_data_in + 4, packet_length);
87 static void process_sample_data(const struct sr_dev_inst *sdi)
89 struct dev_context *devc;
90 struct sr_datafeed_packet packet;
91 struct sr_datafeed_logic logic;
92 uint8_t i, j, tmp, buffer[PACKET_NUM_SAMPLES], *ptr[NUM_CHANNELS];
93 uint16_t offset, n = 0;
97 offset = devc->sample_packet * PACKET_NUM_SAMPLE_BYTES;
100 * Array of pointers to the sample data of all channels up to the last
101 * enabled one for an uniform access to them. Note that the currently
102 * received samples always belong to the last enabled channel.
104 for (i = 0; i < devc->num_enabled_channels - 1; i++)
105 ptr[i] = devc->sample_buffer[devc->channel_map[i]] + offset;
108 * Skip the first 4 bytes of the buffer because they contain channel
109 * and packet information only.
111 ptr[i] = devc->xfer_data_in + 4;
113 for (i = 0; i < PACKET_NUM_SAMPLE_BYTES; i++) {
114 /* Stop processing if all requested samples are processed. */
115 if (devc->samples_processed == devc->limit_samples)
120 if (devc->samples_processed == 0) {
122 * Adjust the position of the first sample to be
123 * processed because possibly more samples than
124 * necessary might have been acquired. This is because
125 * the number of acquired samples is always rounded up
126 * to a multiple of 8.
128 k = k - (devc->pre_trigger_bytes * 8) +
129 devc->pre_trigger_samples;
131 sr_dbg("Start processing at sample: %d.", 7 - k);
134 * Send the trigger before the first sample is
135 * processed if no pre trigger samples were calculated
136 * through the capture ratio.
138 if (devc->trigger_type != TRIGGER_TYPE_NONE &&
139 devc->pre_trigger_samples == 0) {
140 packet.type = SR_DF_TRIGGER;
141 sr_session_send(devc->cb_data, &packet);
145 for (; k >= 0; k--) {
147 * Stop processing if all requested samples are
150 if (devc->samples_processed == devc->limit_samples)
156 * Extract the current sample for each enabled channel
157 * and store them in the buffer.
159 for (j = 0; j < devc->num_enabled_channels; j++) {
160 tmp = (ptr[j][i] & (1 << k)) >> k;
161 buffer[n] |= tmp << devc->channel_map[j];
165 devc->samples_processed++;
168 * Send all processed samples and the trigger if the
169 * number of processed samples reaches the calculated
170 * number of pre trigger samples.
172 if (devc->samples_processed == devc->pre_trigger_samples &&
173 devc->trigger_type != TRIGGER_TYPE_NONE) {
174 packet.type = SR_DF_LOGIC;
175 packet.payload = &logic;
179 sr_session_send(devc->cb_data, &packet);
181 packet.type = SR_DF_TRIGGER;
182 sr_session_send(devc->cb_data, &packet);
190 packet.type = SR_DF_LOGIC;
191 packet.payload = &logic;
195 sr_session_send(devc->cb_data, &packet);
199 SR_PRIV int ikalogic_scanalogic2_receive_data(int fd, int revents, void *cb_data)
201 struct sr_dev_inst *sdi;
202 struct sr_dev_driver *di;
203 struct dev_context *devc;
204 struct drv_context *drvc;
206 int64_t current_time, time_elapsed;
212 if (!(sdi = cb_data))
215 if (!(devc = sdi->priv))
220 current_time = g_get_monotonic_time();
222 if (devc->state == STATE_WAIT_DATA_READY &&
223 !devc->wait_data_ready_locked) {
224 time_elapsed = current_time - devc->wait_data_ready_time;
227 * Check here for stopping in addition to the transfer
228 * callback functions to avoid waiting until the
229 * WAIT_DATA_READY_INTERVAL has expired.
231 if (sdi->status == SR_ST_STOPPING) {
232 if (!devc->stopping_in_progress) {
233 devc->next_state = STATE_RESET_AND_IDLE;
234 devc->stopping_in_progress = TRUE;
235 ret = libusb_submit_transfer(devc->xfer_in);
237 } else if (time_elapsed >= WAIT_DATA_READY_INTERVAL) {
238 devc->wait_data_ready_locked = TRUE;
239 ret = libusb_submit_transfer(devc->xfer_in);
244 sr_err("Submit transfer failed: %s.", libusb_error_name(ret));
245 abort_acquisition(sdi);
252 libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv,
255 /* Check if an error occurred on a transfer. */
256 if (devc->transfer_error)
257 abort_acquisition(sdi);
262 SR_PRIV void LIBUSB_CALL sl2_receive_transfer_in( struct libusb_transfer *transfer)
264 struct sr_dev_inst *sdi;
265 struct dev_context *devc;
266 uint8_t last_channel;
269 sdi = transfer->user_data;
272 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
273 sr_err("Transfer to device failed: %s.",
274 libusb_error_name(transfer->status));
275 devc->transfer_error = TRUE;
279 if (sdi->status == SR_ST_STOPPING && !devc->stopping_in_progress) {
280 devc->next_state = STATE_RESET_AND_IDLE;
281 devc->stopping_in_progress = TRUE;
283 if (libusb_submit_transfer(devc->xfer_in) != 0) {
284 sr_err("Submit transfer failed: %s.",
285 libusb_error_name(ret));
286 devc->transfer_error = TRUE;
292 if (devc->state != devc->next_state)
293 sr_spew("State changed from %i to %i.",
294 devc->state, devc->next_state);
295 devc->state = devc->next_state;
297 if (devc->state == STATE_WAIT_DATA_READY) {
298 /* Check if the received data are a valid device status. */
299 if (devc->xfer_data_in[0] == 0x05) {
300 if (devc->xfer_data_in[1] == STATUS_WAITING_FOR_TRIGGER)
301 sr_dbg("Waiting for trigger.");
302 else if (devc->xfer_data_in[1] == STATUS_SAMPLING)
303 sr_dbg("Sampling in progress.");
307 * Check if the received data are a valid device status and the
308 * sample data are ready.
310 if (devc->xfer_data_in[0] == 0x05 &&
311 devc->xfer_data_in[1] == STATUS_DATA_READY) {
312 devc->next_state = STATE_RECEIVE_DATA;
313 ret = libusb_submit_transfer(transfer);
315 devc->wait_data_ready_locked = FALSE;
316 devc->wait_data_ready_time = g_get_monotonic_time();
318 } else if (devc->state == STATE_RECEIVE_DATA) {
319 last_channel = devc->channel_map[devc->num_enabled_channels - 1];
321 if (devc->channel < last_channel) {
322 buffer_sample_data(sdi);
323 } else if (devc->channel == last_channel) {
324 process_sample_data(sdi);
327 * Stop acquisition because all samples of enabled
328 * channels are processed.
330 devc->next_state = STATE_RESET_AND_IDLE;
333 devc->sample_packet++;
334 devc->sample_packet %= devc->num_sample_packets;
336 if (devc->sample_packet == 0)
339 ret = libusb_submit_transfer(transfer);
340 } else if (devc->state == STATE_RESET_AND_IDLE) {
341 /* Check if the received data are a valid device status. */
342 if (devc->xfer_data_in[0] == 0x05) {
343 if (devc->xfer_data_in[1] == STATUS_DEVICE_READY) {
344 devc->next_state = STATE_IDLE;
345 devc->xfer_data_out[0] = CMD_IDLE;
347 devc->next_state = STATE_WAIT_DEVICE_READY;
348 devc->xfer_data_out[0] = CMD_RESET;
351 ret = libusb_submit_transfer(devc->xfer_out);
354 * The received device status is invalid which
355 * indicates that the device is not ready to accept
356 * commands. Request a new device status until a valid
357 * device status is received.
359 ret = libusb_submit_transfer(transfer);
361 } else if (devc->state == STATE_WAIT_DEVICE_READY) {
362 /* Check if the received data are a valid device status. */
363 if (devc->xfer_data_in[0] == 0x05) {
364 if (devc->xfer_data_in[1] == STATUS_DEVICE_READY) {
365 devc->next_state = STATE_IDLE;
366 devc->xfer_data_out[0] = CMD_IDLE;
369 * The received device status is valid but the
370 * device is not ready. Probably the device did
371 * not recognize the last reset. Reset the
374 devc->xfer_data_out[0] = CMD_RESET;
377 ret = libusb_submit_transfer(devc->xfer_out);
380 * The device is not ready and therefore not able to
381 * change to the idle state. Request a new device
382 * status until the device is ready.
384 ret = libusb_submit_transfer(transfer);
389 sr_err("Submit transfer failed: %s.", libusb_error_name(ret));
390 devc->transfer_error = TRUE;
394 SR_PRIV void LIBUSB_CALL sl2_receive_transfer_out( struct libusb_transfer *transfer)
396 struct sr_dev_inst *sdi;
397 struct dev_context *devc;
400 sdi = transfer->user_data;
403 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
404 sr_err("Transfer to device failed: %s.",
405 libusb_error_name(transfer->status));
406 devc->transfer_error = TRUE;
410 if (sdi->status == SR_ST_STOPPING && !devc->stopping_in_progress) {
411 devc->next_state = STATE_RESET_AND_IDLE;
412 devc->stopping_in_progress = TRUE;
414 if (libusb_submit_transfer(devc->xfer_in) != 0) {
415 sr_err("Submit transfer failed: %s.",
416 libusb_error_name(ret));
418 devc->transfer_error = TRUE;
424 if (devc->state != devc->next_state)
425 sr_spew("State changed from %i to %i.",
426 devc->state, devc->next_state);
427 devc->state = devc->next_state;
429 if (devc->state == STATE_IDLE) {
430 stop_acquisition(sdi);
431 } else if (devc->state == STATE_SAMPLE) {
432 devc->next_state = STATE_WAIT_DATA_READY;
433 ret = libusb_submit_transfer(devc->xfer_in);
434 } else if (devc->state == STATE_WAIT_DEVICE_READY) {
435 ret = libusb_submit_transfer(devc->xfer_in);
439 sr_err("Submit transfer failed: %s.", libusb_error_name(ret));
440 devc->transfer_error = TRUE;
444 SR_PRIV int sl2_set_samplerate(const struct sr_dev_inst *sdi,
447 struct dev_context *devc;
452 for (i = 0; i < NUM_SAMPLERATES; i++) {
453 if (sl2_samplerates[i] == samplerate) {
454 devc->samplerate = samplerate;
455 devc->samplerate_id = NUM_SAMPLERATES - i - 1;
463 SR_PRIV int sl2_set_limit_samples(const struct sr_dev_inst *sdi,
464 uint64_t limit_samples)
466 struct dev_context *devc;
470 if (limit_samples == 0) {
471 sr_err("Invalid number of limit samples: %" PRIu64 ".",
476 if (limit_samples > MAX_SAMPLES)
477 limit_samples = MAX_SAMPLES;
479 sr_dbg("Limit samples set to %" PRIu64 ".", limit_samples);
481 devc->limit_samples = limit_samples;
486 SR_PRIV int sl2_convert_trigger(const struct sr_dev_inst *sdi)
488 struct dev_context *devc;
489 struct sr_trigger *trigger;
490 struct sr_trigger_stage *stage;
491 struct sr_trigger_match *match;
493 int num_triggers_anyedge;
497 /* Disable the trigger by default. */
498 devc->trigger_channel = TRIGGER_CHANNEL_0;
499 devc->trigger_type = TRIGGER_TYPE_NONE;
501 if (!(trigger = sr_session_trigger_get(sdi->session)))
504 if (g_slist_length(trigger->stages) > 1) {
505 sr_err("This device only supports 1 trigger stage.");
509 num_triggers_anyedge = 0;
510 for (l = trigger->stages; l; l = l->next) {
512 for (m = stage->matches; m; m = m->next) {
514 if (!match->channel->enabled)
515 /* Ignore disabled channels with a trigger. */
517 devc->trigger_channel = match->channel->index + 1;
518 switch (match->match) {
519 case SR_TRIGGER_RISING:
520 devc->trigger_type = TRIGGER_TYPE_POSEDGE;
522 case SR_TRIGGER_FALLING:
523 devc->trigger_type = TRIGGER_TYPE_NEGEDGE;
525 case SR_TRIGGER_EDGE:
526 devc->trigger_type = TRIGGER_TYPE_ANYEDGE;
527 num_triggers_anyedge++;
534 * Set trigger to any edge on all channels if the trigger for each
535 * channel is set to any edge.
537 if (num_triggers_anyedge == NUM_CHANNELS) {
538 devc->trigger_channel = TRIGGER_CHANNEL_ALL;
539 devc->trigger_type = TRIGGER_TYPE_ANYEDGE;
542 sr_dbg("Trigger set to channel 0x%02x and type 0x%02x.",
543 devc->trigger_channel, devc->trigger_type);
548 SR_PRIV int sl2_set_capture_ratio(const struct sr_dev_inst *sdi,
549 uint64_t capture_ratio)
551 struct dev_context *devc;
555 if (capture_ratio > 100) {
556 sr_err("Invalid capture ratio: %" PRIu64 " %%.", capture_ratio);
560 sr_info("Capture ratio set to %" PRIu64 " %%.", capture_ratio);
562 devc->capture_ratio = capture_ratio;
567 SR_PRIV int sl2_set_after_trigger_delay(const struct sr_dev_inst *sdi,
568 uint64_t after_trigger_delay)
570 struct dev_context *devc;
574 if (after_trigger_delay > MAX_AFTER_TRIGGER_DELAY) {
575 sr_err("Invalid after trigger delay: %" PRIu64 " ms.",
576 after_trigger_delay);
580 sr_info("After trigger delay set to %" PRIu64 " ms.",
581 after_trigger_delay);
583 devc->after_trigger_delay = after_trigger_delay;
588 SR_PRIV void sl2_calculate_trigger_samples(const struct sr_dev_inst *sdi)
590 struct dev_context *devc;
591 uint64_t pre_trigger_samples, post_trigger_samples;
592 uint16_t pre_trigger_bytes, post_trigger_bytes;
596 cr = devc->capture_ratio;
598 /* Ignore the capture ratio if no trigger is enabled. */
599 if (devc->trigger_type == TRIGGER_TYPE_NONE)
602 pre_trigger_samples = (devc->limit_samples * cr) / 100;
603 post_trigger_samples = (devc->limit_samples * (100 - cr)) / 100;
606 * Increase the number of post trigger samples by one to compensate the
607 * possible loss of a sample through integer rounding.
609 if (pre_trigger_samples + post_trigger_samples != devc->limit_samples)
610 post_trigger_samples++;
613 * The device requires the number of samples in multiples of 8 which
614 * will also be called sample bytes in the following.
616 pre_trigger_bytes = pre_trigger_samples / 8;
617 post_trigger_bytes = post_trigger_samples / 8;
620 * Round up the number of sample bytes to ensure that at least the
621 * requested number of samples will be acquired. Note that due to this
622 * rounding the buffer to store these sample bytes needs to be at least
623 * one sample byte larger than the minimal number of sample bytes
624 * needed to store the requested samples.
626 if (pre_trigger_samples % 8 != 0)
629 if (post_trigger_samples % 8 != 0)
630 post_trigger_bytes++;
632 sr_info("Pre trigger samples: %" PRIu64 ".", pre_trigger_samples);
633 sr_info("Post trigger samples: %" PRIu64 ".", post_trigger_samples);
634 sr_dbg("Pre trigger sample bytes: %" PRIu16 ".", pre_trigger_bytes);
635 sr_dbg("Post trigger sample bytes: %" PRIu16 ".", post_trigger_bytes);
637 devc->pre_trigger_samples = pre_trigger_samples;
638 devc->pre_trigger_bytes = pre_trigger_bytes;
639 devc->post_trigger_bytes = post_trigger_bytes;
642 SR_PRIV int sl2_get_device_info(struct sr_usb_dev_inst usb,
643 struct device_info *dev_info)
645 struct drv_context *drvc;
646 uint8_t buffer[PACKET_LENGTH];
649 drvc = ikalogic_scanalogic2_driver_info.context;
654 if (sr_usb_open(drvc->sr_ctx->libusb_ctx, &usb) != SR_OK)
658 * Determine if a kernel driver is active on this interface and, if so,
661 if (libusb_kernel_driver_active(usb.devhdl, USB_INTERFACE) == 1) {
662 ret = libusb_detach_kernel_driver(usb.devhdl,
666 sr_err("Failed to detach kernel driver: %s.",
667 libusb_error_name(ret));
668 libusb_close(usb.devhdl);
673 ret = libusb_claim_interface(usb.devhdl, USB_INTERFACE);
676 sr_err("Failed to claim interface: %s.",
677 libusb_error_name(ret));
678 libusb_close(usb.devhdl);
682 memset(buffer, 0, sizeof(buffer));
685 * Reset the device to ensure it is in a proper state to request the
686 * device information.
688 buffer[0] = CMD_RESET;
689 if ((ret = sl2_transfer_out(usb.devhdl, buffer)) != PACKET_LENGTH) {
690 sr_err("Resetting of device failed: %s.",
691 libusb_error_name(ret));
692 libusb_release_interface(usb.devhdl, USB_INTERFACE);
693 libusb_close(usb.devhdl);
697 buffer[0] = CMD_INFO;
698 if ((ret = sl2_transfer_out(usb.devhdl, buffer)) != PACKET_LENGTH) {
699 sr_err("Requesting of device information failed: %s.",
700 libusb_error_name(ret));
701 libusb_release_interface(usb.devhdl, USB_INTERFACE);
702 libusb_close(usb.devhdl);
706 if ((ret = sl2_transfer_in(usb.devhdl, buffer)) != PACKET_LENGTH) {
707 sr_err("Receiving of device information failed: %s.",
708 libusb_error_name(ret));
709 libusb_release_interface(usb.devhdl, USB_INTERFACE);
710 libusb_close(usb.devhdl);
714 memcpy(&(dev_info->serial), buffer + 1, sizeof(uint32_t));
715 dev_info->serial = GUINT32_FROM_LE(dev_info->serial);
717 dev_info->fw_ver_major = buffer[5];
718 dev_info->fw_ver_minor = buffer[6];
720 buffer[0] = CMD_RESET;
721 if ((ret = sl2_transfer_out(usb.devhdl, buffer)) != PACKET_LENGTH) {
722 sr_err("Device reset failed: %s.", libusb_error_name(ret));
723 libusb_release_interface(usb.devhdl, USB_INTERFACE);
724 libusb_close(usb.devhdl);
729 * Set the device to idle state. If the device is not in idle state it
730 * possibly will reset itself after a few seconds without being used
731 * and thereby close the connection.
733 buffer[0] = CMD_IDLE;
734 if ((ret = sl2_transfer_out(usb.devhdl, buffer)) != PACKET_LENGTH) {
735 sr_err("Failed to set device in idle state: %s.",
736 libusb_error_name(ret));
737 libusb_release_interface(usb.devhdl, USB_INTERFACE);
738 libusb_close(usb.devhdl);
742 ret = libusb_release_interface(usb.devhdl, USB_INTERFACE);
745 sr_err("Failed to release interface: %s.",
746 libusb_error_name(ret));
747 libusb_close(usb.devhdl);
751 libusb_close(usb.devhdl);
756 SR_PRIV int sl2_transfer_in(libusb_device_handle *dev_handle, uint8_t *data)
758 return libusb_control_transfer(dev_handle, USB_REQUEST_TYPE_IN,
759 USB_HID_GET_REPORT, USB_HID_REPORT_TYPE_FEATURE, USB_INTERFACE,
760 (unsigned char *)data, PACKET_LENGTH, USB_TIMEOUT_MS);
763 SR_PRIV int sl2_transfer_out(libusb_device_handle *dev_handle, uint8_t *data)
765 return libusb_control_transfer(dev_handle, USB_REQUEST_TYPE_OUT,
766 USB_HID_SET_REPORT, USB_HID_REPORT_TYPE_FEATURE, USB_INTERFACE,
767 (unsigned char *)data, PACKET_LENGTH, USB_TIMEOUT_MS);