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/>.
22 extern struct sr_dev_driver ikalogic_scanalogic2_driver_info;
24 extern uint64_t sl2_samplerates[NUM_SAMPLERATES];
26 static void stop_acquisition(struct sr_dev_inst *sdi)
28 struct drv_context *drvc = sdi->driver->priv;
29 struct dev_context *devc;
30 struct sr_datafeed_packet packet;
34 /* Remove USB file descriptors from polling. */
35 usb_source_remove(sdi->session, drvc->sr_ctx);
37 packet.type = SR_DF_END;
38 sr_session_send(devc->cb_data, &packet);
40 sdi->status = SR_ST_ACTIVE;
43 static void abort_acquisition(struct sr_dev_inst *sdi)
45 struct drv_context *drvc = sdi->driver->priv;
46 struct dev_context *devc;
47 struct sr_datafeed_packet packet;
51 /* Remove USB file descriptors from polling. */
52 usb_source_remove(sdi->session, drvc->sr_ctx);
54 packet.type = SR_DF_END;
55 sr_session_send(devc->cb_data, &packet);
57 sdi->driver->dev_close(sdi);
60 static void buffer_sample_data(const struct sr_dev_inst *sdi)
62 struct dev_context *devc;
63 unsigned int offset, packet_length;
67 if (devc->channels[devc->channel]->enabled) {
68 offset = devc->sample_packet * PACKET_NUM_SAMPLE_BYTES;
71 * Determine the packet length to ensure that the last packet
72 * will not exceed the buffer size.
74 packet_length = MIN(PACKET_NUM_SAMPLE_BYTES,
75 MAX_DEV_SAMPLE_BYTES - offset);
78 * Skip the first 4 bytes of the source buffer because they
79 * contain channel and packet information only.
81 memcpy(devc->sample_buffer[devc->channel] + offset,
82 devc->xfer_data_in + 4, packet_length);
86 static void process_sample_data(const struct sr_dev_inst *sdi)
88 struct dev_context *devc;
89 struct sr_datafeed_packet packet;
90 struct sr_datafeed_logic logic;
91 uint8_t i, j, tmp, buffer[PACKET_NUM_SAMPLES], *ptr[NUM_CHANNELS];
92 uint16_t offset, n = 0;
96 offset = devc->sample_packet * PACKET_NUM_SAMPLE_BYTES;
99 * Array of pointers to the sample data of all channels up to the last
100 * enabled one for an uniform access to them. Note that the currently
101 * received samples always belong to the last enabled channel.
103 for (i = 0; i < devc->num_enabled_channels - 1; i++)
104 ptr[i] = devc->sample_buffer[devc->channel_map[i]] + offset;
107 * Skip the first 4 bytes of the buffer because they contain channel
108 * and packet information only.
110 ptr[i] = devc->xfer_data_in + 4;
112 for (i = 0; i < PACKET_NUM_SAMPLE_BYTES; i++) {
113 /* Stop processing if all requested samples are processed. */
114 if (devc->samples_processed == devc->limit_samples)
119 if (devc->samples_processed == 0) {
121 * Adjust the position of the first sample to be
122 * processed because possibly more samples than
123 * necessary might have been acquired. This is because
124 * the number of acquired samples is always rounded up
125 * to a multiple of 8.
127 k = k - (devc->pre_trigger_bytes * 8) +
128 devc->pre_trigger_samples;
130 sr_dbg("Start processing at sample: %d.", 7 - k);
133 * Send the trigger before the first sample is
134 * processed if no pre trigger samples were calculated
135 * through the capture ratio.
137 if (devc->trigger_type != TRIGGER_TYPE_NONE &&
138 devc->pre_trigger_samples == 0) {
139 packet.type = SR_DF_TRIGGER;
140 sr_session_send(devc->cb_data, &packet);
144 for (; k >= 0; k--) {
146 * Stop processing if all requested samples are
149 if (devc->samples_processed == devc->limit_samples)
155 * Extract the current sample for each enabled channel
156 * and store them in the buffer.
158 for (j = 0; j < devc->num_enabled_channels; j++) {
159 tmp = (ptr[j][i] & (1 << k)) >> k;
160 buffer[n] |= tmp << devc->channel_map[j];
164 devc->samples_processed++;
167 * Send all processed samples and the trigger if the
168 * number of processed samples reaches the calculated
169 * number of pre trigger samples.
171 if (devc->samples_processed == devc->pre_trigger_samples &&
172 devc->trigger_type != TRIGGER_TYPE_NONE) {
173 packet.type = SR_DF_LOGIC;
174 packet.payload = &logic;
178 sr_session_send(devc->cb_data, &packet);
180 packet.type = SR_DF_TRIGGER;
181 sr_session_send(devc->cb_data, &packet);
189 packet.type = SR_DF_LOGIC;
190 packet.payload = &logic;
194 sr_session_send(devc->cb_data, &packet);
198 SR_PRIV int ikalogic_scanalogic2_receive_data(int fd, int revents, void *cb_data)
200 struct sr_dev_inst *sdi;
201 struct sr_dev_driver *di;
202 struct dev_context *devc;
203 struct drv_context *drvc;
205 int64_t current_time, time_elapsed;
211 if (!(sdi = cb_data))
214 if (!(devc = sdi->priv))
219 current_time = g_get_monotonic_time();
221 if (devc->state == STATE_WAIT_DATA_READY &&
222 !devc->wait_data_ready_locked) {
223 time_elapsed = current_time - devc->wait_data_ready_time;
226 * Check here for stopping in addition to the transfer
227 * callback functions to avoid waiting until the
228 * WAIT_DATA_READY_INTERVAL has expired.
230 if (sdi->status == SR_ST_STOPPING) {
231 if (!devc->stopping_in_progress) {
232 devc->next_state = STATE_RESET_AND_IDLE;
233 devc->stopping_in_progress = TRUE;
234 ret = libusb_submit_transfer(devc->xfer_in);
236 } else if (time_elapsed >= WAIT_DATA_READY_INTERVAL) {
237 devc->wait_data_ready_locked = TRUE;
238 ret = libusb_submit_transfer(devc->xfer_in);
243 sr_err("Submit transfer failed: %s.", libusb_error_name(ret));
244 abort_acquisition(sdi);
251 libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv,
254 /* Check if an error occurred on a transfer. */
255 if (devc->transfer_error)
256 abort_acquisition(sdi);
261 SR_PRIV void LIBUSB_CALL sl2_receive_transfer_in( struct libusb_transfer *transfer)
263 struct sr_dev_inst *sdi;
264 struct dev_context *devc;
265 uint8_t last_channel;
268 sdi = transfer->user_data;
271 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
272 sr_err("Transfer to device failed: %i.", transfer->status);
273 devc->transfer_error = TRUE;
277 if (sdi->status == SR_ST_STOPPING && !devc->stopping_in_progress) {
278 devc->next_state = STATE_RESET_AND_IDLE;
279 devc->stopping_in_progress = TRUE;
281 if (libusb_submit_transfer(devc->xfer_in) != 0) {
282 sr_err("Submit transfer failed: %s.",
283 libusb_error_name(ret));
284 devc->transfer_error = TRUE;
290 if (devc->state != devc->next_state)
291 sr_spew("State changed from %i to %i.",
292 devc->state, devc->next_state);
293 devc->state = devc->next_state;
295 if (devc->state == STATE_WAIT_DATA_READY) {
296 /* Check if the received data are a valid device status. */
297 if (devc->xfer_data_in[0] == 0x05) {
298 if (devc->xfer_data_in[1] == STATUS_WAITING_FOR_TRIGGER)
299 sr_dbg("Waiting for trigger.");
300 else if (devc->xfer_data_in[1] == STATUS_SAMPLING)
301 sr_dbg("Sampling in progress.");
305 * Check if the received data are a valid device status and the
306 * sample data are ready.
308 if (devc->xfer_data_in[0] == 0x05 &&
309 devc->xfer_data_in[1] == STATUS_DATA_READY) {
310 devc->next_state = STATE_RECEIVE_DATA;
311 ret = libusb_submit_transfer(transfer);
313 devc->wait_data_ready_locked = FALSE;
314 devc->wait_data_ready_time = g_get_monotonic_time();
316 } else if (devc->state == STATE_RECEIVE_DATA) {
317 last_channel = devc->channel_map[devc->num_enabled_channels - 1];
319 if (devc->channel < last_channel) {
320 buffer_sample_data(sdi);
321 } else if (devc->channel == last_channel) {
322 process_sample_data(sdi);
325 * Stop acquisition because all samples of enabled
326 * channels are processed.
328 devc->next_state = STATE_RESET_AND_IDLE;
331 devc->sample_packet++;
332 devc->sample_packet %= devc->num_sample_packets;
334 if (devc->sample_packet == 0)
337 ret = libusb_submit_transfer(transfer);
338 } else if (devc->state == STATE_RESET_AND_IDLE) {
339 /* Check if the received data are a valid device status. */
340 if (devc->xfer_data_in[0] == 0x05) {
341 if (devc->xfer_data_in[1] == STATUS_DEVICE_READY) {
342 devc->next_state = STATE_IDLE;
343 devc->xfer_data_out[0] = CMD_IDLE;
345 devc->next_state = STATE_WAIT_DEVICE_READY;
346 devc->xfer_data_out[0] = CMD_RESET;
349 ret = libusb_submit_transfer(devc->xfer_out);
352 * The received device status is invalid which
353 * indicates that the device is not ready to accept
354 * commands. Request a new device status until a valid
355 * device status is received.
357 ret = libusb_submit_transfer(transfer);
359 } else if (devc->state == STATE_WAIT_DEVICE_READY) {
360 /* Check if the received data are a valid device status. */
361 if (devc->xfer_data_in[0] == 0x05) {
362 if (devc->xfer_data_in[1] == STATUS_DEVICE_READY) {
363 devc->next_state = STATE_IDLE;
364 devc->xfer_data_out[0] = CMD_IDLE;
367 * The received device status is valid but the
368 * device is not ready. Probably the device did
369 * not recognize the last reset. Reset the
372 devc->xfer_data_out[0] = CMD_RESET;
375 ret = libusb_submit_transfer(devc->xfer_out);
378 * The device is not ready and therefore not able to
379 * change to the idle state. Request a new device
380 * status until the device is ready.
382 ret = libusb_submit_transfer(transfer);
387 sr_err("Submit transfer failed: %s.", libusb_error_name(ret));
388 devc->transfer_error = TRUE;
392 SR_PRIV void LIBUSB_CALL sl2_receive_transfer_out( struct libusb_transfer *transfer)
394 struct sr_dev_inst *sdi;
395 struct dev_context *devc;
398 sdi = transfer->user_data;
401 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
402 sr_err("Transfer to device failed: %i.", transfer->status);
403 devc->transfer_error = TRUE;
407 if (sdi->status == SR_ST_STOPPING && !devc->stopping_in_progress) {
408 devc->next_state = STATE_RESET_AND_IDLE;
409 devc->stopping_in_progress = TRUE;
411 if (libusb_submit_transfer(devc->xfer_in) != 0) {
412 sr_err("Submit transfer failed: %s.",
413 libusb_error_name(ret));
415 devc->transfer_error = TRUE;
421 if (devc->state != devc->next_state)
422 sr_spew("State changed from %i to %i.",
423 devc->state, devc->next_state);
424 devc->state = devc->next_state;
426 if (devc->state == STATE_IDLE) {
427 stop_acquisition(sdi);
428 } else if (devc->state == STATE_SAMPLE) {
429 devc->next_state = STATE_WAIT_DATA_READY;
430 ret = libusb_submit_transfer(devc->xfer_in);
431 } else if (devc->state == STATE_WAIT_DEVICE_READY) {
432 ret = libusb_submit_transfer(devc->xfer_in);
436 sr_err("Submit transfer failed: %s.", libusb_error_name(ret));
437 devc->transfer_error = TRUE;
441 SR_PRIV int sl2_set_samplerate(const struct sr_dev_inst *sdi,
444 struct dev_context *devc;
449 for (i = 0; i < NUM_SAMPLERATES; i++) {
450 if (sl2_samplerates[i] == samplerate) {
451 devc->samplerate = samplerate;
452 devc->samplerate_id = NUM_SAMPLERATES - i - 1;
460 SR_PRIV int sl2_set_limit_samples(const struct sr_dev_inst *sdi,
461 uint64_t limit_samples)
463 struct dev_context *devc;
467 if (limit_samples == 0) {
468 sr_err("Invalid number of limit samples: %" PRIu64 ".",
473 if (limit_samples > MAX_SAMPLES)
474 limit_samples = MAX_SAMPLES;
476 sr_dbg("Limit samples set to %" PRIu64 ".", limit_samples);
478 devc->limit_samples = limit_samples;
483 SR_PRIV int sl2_convert_trigger(const struct sr_dev_inst *sdi)
485 struct dev_context *devc;
486 struct sr_trigger *trigger;
487 struct sr_trigger_stage *stage;
488 struct sr_trigger_match *match;
490 int num_triggers_anyedge;
494 /* Disable the trigger by default. */
495 devc->trigger_channel = TRIGGER_CHANNEL_0;
496 devc->trigger_type = TRIGGER_TYPE_NONE;
498 if (!(trigger = sr_session_trigger_get(sdi->session)))
501 if (g_slist_length(trigger->stages) > 1) {
502 sr_err("This device only supports 1 trigger stage.");
506 num_triggers_anyedge = 0;
507 for (l = trigger->stages; l; l = l->next) {
509 for (m = stage->matches; m; m = m->next) {
511 if (!match->channel->enabled)
512 /* Ignore disabled channels with a trigger. */
514 devc->trigger_channel = match->channel->index + 1;
515 switch (match->match) {
516 case SR_TRIGGER_RISING:
517 devc->trigger_type = TRIGGER_TYPE_POSEDGE;
519 case SR_TRIGGER_FALLING:
520 devc->trigger_type = TRIGGER_TYPE_NEGEDGE;
522 case SR_TRIGGER_EDGE:
523 devc->trigger_type = TRIGGER_TYPE_ANYEDGE;
524 num_triggers_anyedge++;
531 * Set trigger to any edge on all channels if the trigger for each
532 * channel is set to any edge.
534 if (num_triggers_anyedge == NUM_CHANNELS) {
535 devc->trigger_channel = TRIGGER_CHANNEL_ALL;
536 devc->trigger_type = TRIGGER_TYPE_ANYEDGE;
539 sr_dbg("Trigger set to channel 0x%02x and type 0x%02x.",
540 devc->trigger_channel, devc->trigger_type);
545 SR_PRIV int sl2_set_capture_ratio(const struct sr_dev_inst *sdi,
546 uint64_t capture_ratio)
548 struct dev_context *devc;
552 if (capture_ratio > 100) {
553 sr_err("Invalid capture ratio: %" PRIu64 " %%.", capture_ratio);
557 sr_info("Capture ratio set to %" PRIu64 " %%.", capture_ratio);
559 devc->capture_ratio = capture_ratio;
564 SR_PRIV int sl2_set_after_trigger_delay(const struct sr_dev_inst *sdi,
565 uint64_t after_trigger_delay)
567 struct dev_context *devc;
571 if (after_trigger_delay > MAX_AFTER_TRIGGER_DELAY) {
572 sr_err("Invalid after trigger delay: %" PRIu64 " ms.",
573 after_trigger_delay);
577 sr_info("After trigger delay set to %" PRIu64 " ms.",
578 after_trigger_delay);
580 devc->after_trigger_delay = after_trigger_delay;
585 SR_PRIV void sl2_calculate_trigger_samples(const struct sr_dev_inst *sdi)
587 struct dev_context *devc;
588 uint64_t pre_trigger_samples, post_trigger_samples;
589 uint16_t pre_trigger_bytes, post_trigger_bytes;
593 cr = devc->capture_ratio;
595 /* Ignore the capture ratio if no trigger is enabled. */
596 if (devc->trigger_type == TRIGGER_TYPE_NONE)
599 pre_trigger_samples = (devc->limit_samples * cr) / 100;
600 post_trigger_samples = (devc->limit_samples * (100 - cr)) / 100;
603 * Increase the number of post trigger samples by one to compensate the
604 * possible loss of a sample through integer rounding.
606 if (pre_trigger_samples + post_trigger_samples != devc->limit_samples)
607 post_trigger_samples++;
610 * The device requires the number of samples in multiples of 8 which
611 * will also be called sample bytes in the following.
613 pre_trigger_bytes = pre_trigger_samples / 8;
614 post_trigger_bytes = post_trigger_samples / 8;
617 * Round up the number of sample bytes to ensure that at least the
618 * requested number of samples will be acquired. Note that due to this
619 * rounding the buffer to store these sample bytes needs to be at least
620 * one sample byte larger than the minimal number of sample bytes
621 * needed to store the requested samples.
623 if (pre_trigger_samples % 8 != 0)
626 if (post_trigger_samples % 8 != 0)
627 post_trigger_bytes++;
629 sr_info("Pre trigger samples: %" PRIu64 ".", pre_trigger_samples);
630 sr_info("Post trigger samples: %" PRIu64 ".", post_trigger_samples);
631 sr_dbg("Pre trigger sample bytes: %" PRIu16 ".", pre_trigger_bytes);
632 sr_dbg("Post trigger sample bytes: %" PRIu16 ".", post_trigger_bytes);
634 devc->pre_trigger_samples = pre_trigger_samples;
635 devc->pre_trigger_bytes = pre_trigger_bytes;
636 devc->post_trigger_bytes = post_trigger_bytes;
639 SR_PRIV int sl2_get_device_info(struct sr_usb_dev_inst usb,
640 struct device_info *dev_info)
642 struct drv_context *drvc;
643 uint8_t buffer[PACKET_LENGTH];
646 drvc = ikalogic_scanalogic2_driver_info.priv;
651 if (sr_usb_open(drvc->sr_ctx->libusb_ctx, &usb) != SR_OK)
655 * Determine if a kernel driver is active on this interface and, if so,
658 if (libusb_kernel_driver_active(usb.devhdl, USB_INTERFACE) == 1) {
659 ret = libusb_detach_kernel_driver(usb.devhdl,
663 sr_err("Failed to detach kernel driver: %s.",
664 libusb_error_name(ret));
665 libusb_close(usb.devhdl);
670 ret = libusb_claim_interface(usb.devhdl, USB_INTERFACE);
673 sr_err("Failed to claim interface: %s.",
674 libusb_error_name(ret));
675 libusb_close(usb.devhdl);
679 memset(buffer, 0, sizeof(buffer));
682 * Reset the device to ensure it is in a proper state to request the
683 * device information.
685 buffer[0] = CMD_RESET;
686 if ((ret = sl2_transfer_out(usb.devhdl, buffer)) != PACKET_LENGTH) {
687 sr_err("Resetting of device failed: %s.",
688 libusb_error_name(ret));
689 libusb_release_interface(usb.devhdl, USB_INTERFACE);
690 libusb_close(usb.devhdl);
694 buffer[0] = CMD_INFO;
695 if ((ret = sl2_transfer_out(usb.devhdl, buffer)) != PACKET_LENGTH) {
696 sr_err("Requesting of device information failed: %s.",
697 libusb_error_name(ret));
698 libusb_release_interface(usb.devhdl, USB_INTERFACE);
699 libusb_close(usb.devhdl);
703 if ((ret = sl2_transfer_in(usb.devhdl, buffer)) != PACKET_LENGTH) {
704 sr_err("Receiving of device information failed: %s.",
705 libusb_error_name(ret));
706 libusb_release_interface(usb.devhdl, USB_INTERFACE);
707 libusb_close(usb.devhdl);
711 memcpy(&(dev_info->serial), buffer + 1, sizeof(uint32_t));
712 dev_info->serial = GUINT32_FROM_LE(dev_info->serial);
714 dev_info->fw_ver_major = buffer[5];
715 dev_info->fw_ver_minor = buffer[6];
717 buffer[0] = CMD_RESET;
718 if ((ret = sl2_transfer_out(usb.devhdl, buffer)) != PACKET_LENGTH) {
719 sr_err("Device reset failed: %s.", libusb_error_name(ret));
720 libusb_release_interface(usb.devhdl, USB_INTERFACE);
721 libusb_close(usb.devhdl);
726 * Set the device to idle state. If the device is not in idle state it
727 * possibly will reset itself after a few seconds without being used
728 * and thereby close the connection.
730 buffer[0] = CMD_IDLE;
731 if ((ret = sl2_transfer_out(usb.devhdl, buffer)) != PACKET_LENGTH) {
732 sr_err("Failed to set device in idle state: %s.",
733 libusb_error_name(ret));
734 libusb_release_interface(usb.devhdl, USB_INTERFACE);
735 libusb_close(usb.devhdl);
739 ret = libusb_release_interface(usb.devhdl, USB_INTERFACE);
742 sr_err("Failed to release interface: %s.",
743 libusb_error_name(ret));
744 libusb_close(usb.devhdl);
748 libusb_close(usb.devhdl);
753 SR_PRIV int sl2_transfer_in(libusb_device_handle *dev_handle, uint8_t *data)
755 return libusb_control_transfer(dev_handle, USB_REQUEST_TYPE_IN,
756 USB_HID_GET_REPORT, USB_HID_REPORT_TYPE_FEATURE, USB_INTERFACE,
757 (unsigned char *)data, PACKET_LENGTH, USB_TIMEOUT_MS);
760 SR_PRIV int sl2_transfer_out(libusb_device_handle *dev_handle, uint8_t *data)
762 return libusb_control_transfer(dev_handle, USB_REQUEST_TYPE_OUT,
763 USB_HID_SET_REPORT, USB_HID_REPORT_TYPE_FEATURE, USB_INTERFACE,
764 (unsigned char *)data, PACKET_LENGTH, USB_TIMEOUT_MS);