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 /* Remove USB file descriptors from polling. */
32 usb_source_remove(sdi->session, drvc->sr_ctx);
34 std_session_send_df_end(sdi, LOG_PREFIX);
36 sdi->status = SR_ST_ACTIVE;
39 static void abort_acquisition(struct sr_dev_inst *sdi)
41 struct drv_context *drvc = sdi->driver->context;
43 /* Remove USB file descriptors from polling. */
44 usb_source_remove(sdi->session, drvc->sr_ctx);
46 std_session_send_df_end(sdi, LOG_PREFIX);
48 sdi->driver->dev_close(sdi);
51 static void buffer_sample_data(const struct sr_dev_inst *sdi)
53 struct dev_context *devc;
54 unsigned int offset, packet_length;
58 if (devc->channels[devc->channel]->enabled) {
59 offset = devc->sample_packet * PACKET_NUM_SAMPLE_BYTES;
62 * Determine the packet length to ensure that the last packet
63 * will not exceed the buffer size.
65 packet_length = MIN(PACKET_NUM_SAMPLE_BYTES,
66 MAX_DEV_SAMPLE_BYTES - offset);
69 * Skip the first 4 bytes of the source buffer because they
70 * contain channel and packet information only.
72 memcpy(devc->sample_buffer[devc->channel] + offset,
73 devc->xfer_data_in + 4, packet_length);
77 static void process_sample_data(const struct sr_dev_inst *sdi)
79 struct dev_context *devc;
80 struct sr_datafeed_packet packet;
81 struct sr_datafeed_logic logic;
82 uint8_t i, j, tmp, buffer[PACKET_NUM_SAMPLES], *ptr[NUM_CHANNELS];
83 uint16_t offset, n = 0;
87 offset = devc->sample_packet * PACKET_NUM_SAMPLE_BYTES;
90 * Array of pointers to the sample data of all channels up to the last
91 * enabled one for an uniform access to them. Note that the currently
92 * received samples always belong to the last enabled channel.
94 for (i = 0; i < devc->num_enabled_channels - 1; i++)
95 ptr[i] = devc->sample_buffer[devc->channel_map[i]] + offset;
98 * Skip the first 4 bytes of the buffer because they contain channel
99 * and packet information only.
101 ptr[i] = devc->xfer_data_in + 4;
103 for (i = 0; i < PACKET_NUM_SAMPLE_BYTES; i++) {
104 /* Stop processing if all requested samples are processed. */
105 if (devc->samples_processed == devc->limit_samples)
110 if (devc->samples_processed == 0) {
112 * Adjust the position of the first sample to be
113 * processed because possibly more samples than
114 * necessary might have been acquired. This is because
115 * the number of acquired samples is always rounded up
116 * to a multiple of 8.
118 k = k - (devc->pre_trigger_bytes * 8) +
119 devc->pre_trigger_samples;
121 sr_dbg("Start processing at sample: %d.", 7 - k);
124 * Send the trigger before the first sample is
125 * processed if no pre trigger samples were calculated
126 * through the capture ratio.
128 if (devc->trigger_type != TRIGGER_TYPE_NONE &&
129 devc->pre_trigger_samples == 0) {
130 packet.type = SR_DF_TRIGGER;
131 sr_session_send(sdi, &packet);
135 for (; k >= 0; k--) {
137 * Stop processing if all requested samples are
140 if (devc->samples_processed == devc->limit_samples)
146 * Extract the current sample for each enabled channel
147 * and store them in the buffer.
149 for (j = 0; j < devc->num_enabled_channels; j++) {
150 tmp = (ptr[j][i] & (1 << k)) >> k;
151 buffer[n] |= tmp << devc->channel_map[j];
155 devc->samples_processed++;
158 * Send all processed samples and the trigger if the
159 * number of processed samples reaches the calculated
160 * number of pre trigger samples.
162 if (devc->samples_processed == devc->pre_trigger_samples &&
163 devc->trigger_type != TRIGGER_TYPE_NONE) {
164 packet.type = SR_DF_LOGIC;
165 packet.payload = &logic;
169 sr_session_send(sdi, &packet);
171 packet.type = SR_DF_TRIGGER;
172 sr_session_send(sdi, &packet);
180 packet.type = SR_DF_LOGIC;
181 packet.payload = &logic;
185 sr_session_send(sdi, &packet);
189 SR_PRIV int ikalogic_scanalogic2_receive_data(int fd, int revents, void *cb_data)
191 struct sr_dev_inst *sdi;
192 struct sr_dev_driver *di;
193 struct dev_context *devc;
194 struct drv_context *drvc;
196 int64_t current_time, time_elapsed;
202 if (!(sdi = cb_data))
205 if (!(devc = sdi->priv))
210 current_time = g_get_monotonic_time();
212 if (devc->state == STATE_WAIT_DATA_READY &&
213 !devc->wait_data_ready_locked) {
214 time_elapsed = current_time - devc->wait_data_ready_time;
217 * Check here for stopping in addition to the transfer
218 * callback functions to avoid waiting until the
219 * WAIT_DATA_READY_INTERVAL has expired.
221 if (sdi->status == SR_ST_STOPPING) {
222 if (!devc->stopping_in_progress) {
223 devc->next_state = STATE_RESET_AND_IDLE;
224 devc->stopping_in_progress = TRUE;
225 ret = libusb_submit_transfer(devc->xfer_in);
227 } else if (time_elapsed >= WAIT_DATA_READY_INTERVAL) {
228 devc->wait_data_ready_locked = TRUE;
229 ret = libusb_submit_transfer(devc->xfer_in);
234 sr_err("Submit transfer failed: %s.", libusb_error_name(ret));
235 abort_acquisition(sdi);
242 libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv,
245 /* Check if an error occurred on a transfer. */
246 if (devc->transfer_error)
247 abort_acquisition(sdi);
252 SR_PRIV void LIBUSB_CALL sl2_receive_transfer_in( struct libusb_transfer *transfer)
254 struct sr_dev_inst *sdi;
255 struct dev_context *devc;
256 uint8_t last_channel;
259 sdi = transfer->user_data;
262 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
263 sr_err("Transfer to device failed: %s.",
264 libusb_error_name(transfer->status));
265 devc->transfer_error = TRUE;
269 if (sdi->status == SR_ST_STOPPING && !devc->stopping_in_progress) {
270 devc->next_state = STATE_RESET_AND_IDLE;
271 devc->stopping_in_progress = TRUE;
273 if (libusb_submit_transfer(devc->xfer_in) != 0) {
274 sr_err("Submit transfer failed: %s.",
275 libusb_error_name(ret));
276 devc->transfer_error = TRUE;
282 if (devc->state != devc->next_state)
283 sr_spew("State changed from %i to %i.",
284 devc->state, devc->next_state);
285 devc->state = devc->next_state;
287 if (devc->state == STATE_WAIT_DATA_READY) {
288 /* Check if the received data are a valid device status. */
289 if (devc->xfer_data_in[0] == 0x05) {
290 if (devc->xfer_data_in[1] == STATUS_WAITING_FOR_TRIGGER)
291 sr_dbg("Waiting for trigger.");
292 else if (devc->xfer_data_in[1] == STATUS_SAMPLING)
293 sr_dbg("Sampling in progress.");
297 * Check if the received data are a valid device status and the
298 * sample data are ready.
300 if (devc->xfer_data_in[0] == 0x05 &&
301 devc->xfer_data_in[1] == STATUS_DATA_READY) {
302 devc->next_state = STATE_RECEIVE_DATA;
303 ret = libusb_submit_transfer(transfer);
305 devc->wait_data_ready_locked = FALSE;
306 devc->wait_data_ready_time = g_get_monotonic_time();
308 } else if (devc->state == STATE_RECEIVE_DATA) {
309 last_channel = devc->channel_map[devc->num_enabled_channels - 1];
311 if (devc->channel < last_channel) {
312 buffer_sample_data(sdi);
313 } else if (devc->channel == last_channel) {
314 process_sample_data(sdi);
317 * Stop acquisition because all samples of enabled
318 * channels are processed.
320 devc->next_state = STATE_RESET_AND_IDLE;
323 devc->sample_packet++;
324 devc->sample_packet %= devc->num_sample_packets;
326 if (devc->sample_packet == 0)
329 ret = libusb_submit_transfer(transfer);
330 } else if (devc->state == STATE_RESET_AND_IDLE) {
331 /* Check if the received data are a valid device status. */
332 if (devc->xfer_data_in[0] == 0x05) {
333 if (devc->xfer_data_in[1] == STATUS_DEVICE_READY) {
334 devc->next_state = STATE_IDLE;
335 devc->xfer_data_out[0] = CMD_IDLE;
337 devc->next_state = STATE_WAIT_DEVICE_READY;
338 devc->xfer_data_out[0] = CMD_RESET;
341 ret = libusb_submit_transfer(devc->xfer_out);
344 * The received device status is invalid which
345 * indicates that the device is not ready to accept
346 * commands. Request a new device status until a valid
347 * device status is received.
349 ret = libusb_submit_transfer(transfer);
351 } else if (devc->state == STATE_WAIT_DEVICE_READY) {
352 /* Check if the received data are a valid device status. */
353 if (devc->xfer_data_in[0] == 0x05) {
354 if (devc->xfer_data_in[1] == STATUS_DEVICE_READY) {
355 devc->next_state = STATE_IDLE;
356 devc->xfer_data_out[0] = CMD_IDLE;
359 * The received device status is valid but the
360 * device is not ready. Probably the device did
361 * not recognize the last reset. Reset the
364 devc->xfer_data_out[0] = CMD_RESET;
367 ret = libusb_submit_transfer(devc->xfer_out);
370 * The device is not ready and therefore not able to
371 * change to the idle state. Request a new device
372 * status until the device is ready.
374 ret = libusb_submit_transfer(transfer);
379 sr_err("Submit transfer failed: %s.", libusb_error_name(ret));
380 devc->transfer_error = TRUE;
384 SR_PRIV void LIBUSB_CALL sl2_receive_transfer_out( struct libusb_transfer *transfer)
386 struct sr_dev_inst *sdi;
387 struct dev_context *devc;
390 sdi = transfer->user_data;
393 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
394 sr_err("Transfer to device failed: %s.",
395 libusb_error_name(transfer->status));
396 devc->transfer_error = TRUE;
400 if (sdi->status == SR_ST_STOPPING && !devc->stopping_in_progress) {
401 devc->next_state = STATE_RESET_AND_IDLE;
402 devc->stopping_in_progress = TRUE;
404 if (libusb_submit_transfer(devc->xfer_in) != 0) {
405 sr_err("Submit transfer failed: %s.",
406 libusb_error_name(ret));
408 devc->transfer_error = TRUE;
414 if (devc->state != devc->next_state)
415 sr_spew("State changed from %i to %i.",
416 devc->state, devc->next_state);
417 devc->state = devc->next_state;
419 if (devc->state == STATE_IDLE) {
420 stop_acquisition(sdi);
421 } else if (devc->state == STATE_SAMPLE) {
422 devc->next_state = STATE_WAIT_DATA_READY;
423 ret = libusb_submit_transfer(devc->xfer_in);
424 } else if (devc->state == STATE_WAIT_DEVICE_READY) {
425 ret = libusb_submit_transfer(devc->xfer_in);
429 sr_err("Submit transfer failed: %s.", libusb_error_name(ret));
430 devc->transfer_error = TRUE;
434 SR_PRIV int sl2_set_samplerate(const struct sr_dev_inst *sdi,
437 struct dev_context *devc;
442 for (i = 0; i < NUM_SAMPLERATES; i++) {
443 if (sl2_samplerates[i] == samplerate) {
444 devc->samplerate = samplerate;
445 devc->samplerate_id = NUM_SAMPLERATES - i - 1;
453 SR_PRIV int sl2_set_limit_samples(const struct sr_dev_inst *sdi,
454 uint64_t limit_samples)
456 struct dev_context *devc;
460 if (limit_samples == 0) {
461 sr_err("Invalid number of limit samples: %" PRIu64 ".",
466 if (limit_samples > MAX_SAMPLES)
467 limit_samples = MAX_SAMPLES;
469 sr_dbg("Limit samples set to %" PRIu64 ".", limit_samples);
471 devc->limit_samples = limit_samples;
476 SR_PRIV int sl2_convert_trigger(const struct sr_dev_inst *sdi)
478 struct dev_context *devc;
479 struct sr_trigger *trigger;
480 struct sr_trigger_stage *stage;
481 struct sr_trigger_match *match;
483 int num_triggers_anyedge;
487 /* Disable the trigger by default. */
488 devc->trigger_channel = TRIGGER_CHANNEL_0;
489 devc->trigger_type = TRIGGER_TYPE_NONE;
491 if (!(trigger = sr_session_trigger_get(sdi->session)))
494 if (g_slist_length(trigger->stages) > 1) {
495 sr_err("This device only supports 1 trigger stage.");
499 num_triggers_anyedge = 0;
500 for (l = trigger->stages; l; l = l->next) {
502 for (m = stage->matches; m; m = m->next) {
504 if (!match->channel->enabled)
505 /* Ignore disabled channels with a trigger. */
507 devc->trigger_channel = match->channel->index + 1;
508 switch (match->match) {
509 case SR_TRIGGER_RISING:
510 devc->trigger_type = TRIGGER_TYPE_POSEDGE;
512 case SR_TRIGGER_FALLING:
513 devc->trigger_type = TRIGGER_TYPE_NEGEDGE;
515 case SR_TRIGGER_EDGE:
516 devc->trigger_type = TRIGGER_TYPE_ANYEDGE;
517 num_triggers_anyedge++;
524 * Set trigger to any edge on all channels if the trigger for each
525 * channel is set to any edge.
527 if (num_triggers_anyedge == NUM_CHANNELS) {
528 devc->trigger_channel = TRIGGER_CHANNEL_ALL;
529 devc->trigger_type = TRIGGER_TYPE_ANYEDGE;
532 sr_dbg("Trigger set to channel 0x%02x and type 0x%02x.",
533 devc->trigger_channel, devc->trigger_type);
538 SR_PRIV int sl2_set_capture_ratio(const struct sr_dev_inst *sdi,
539 uint64_t capture_ratio)
541 struct dev_context *devc;
545 if (capture_ratio > 100) {
546 sr_err("Invalid capture ratio: %" PRIu64 " %%.", capture_ratio);
550 sr_info("Capture ratio set to %" PRIu64 " %%.", capture_ratio);
552 devc->capture_ratio = capture_ratio;
557 SR_PRIV int sl2_set_after_trigger_delay(const struct sr_dev_inst *sdi,
558 uint64_t after_trigger_delay)
560 struct dev_context *devc;
564 if (after_trigger_delay > MAX_AFTER_TRIGGER_DELAY) {
565 sr_err("Invalid after trigger delay: %" PRIu64 " ms.",
566 after_trigger_delay);
570 sr_info("After trigger delay set to %" PRIu64 " ms.",
571 after_trigger_delay);
573 devc->after_trigger_delay = after_trigger_delay;
578 SR_PRIV void sl2_calculate_trigger_samples(const struct sr_dev_inst *sdi)
580 struct dev_context *devc;
581 uint64_t pre_trigger_samples, post_trigger_samples;
582 uint16_t pre_trigger_bytes, post_trigger_bytes;
586 cr = devc->capture_ratio;
588 /* Ignore the capture ratio if no trigger is enabled. */
589 if (devc->trigger_type == TRIGGER_TYPE_NONE)
592 pre_trigger_samples = (devc->limit_samples * cr) / 100;
593 post_trigger_samples = (devc->limit_samples * (100 - cr)) / 100;
596 * Increase the number of post trigger samples by one to compensate the
597 * possible loss of a sample through integer rounding.
599 if (pre_trigger_samples + post_trigger_samples != devc->limit_samples)
600 post_trigger_samples++;
603 * The device requires the number of samples in multiples of 8 which
604 * will also be called sample bytes in the following.
606 pre_trigger_bytes = pre_trigger_samples / 8;
607 post_trigger_bytes = post_trigger_samples / 8;
610 * Round up the number of sample bytes to ensure that at least the
611 * requested number of samples will be acquired. Note that due to this
612 * rounding the buffer to store these sample bytes needs to be at least
613 * one sample byte larger than the minimal number of sample bytes
614 * needed to store the requested samples.
616 if (pre_trigger_samples % 8 != 0)
619 if (post_trigger_samples % 8 != 0)
620 post_trigger_bytes++;
622 sr_info("Pre trigger samples: %" PRIu64 ".", pre_trigger_samples);
623 sr_info("Post trigger samples: %" PRIu64 ".", post_trigger_samples);
624 sr_dbg("Pre trigger sample bytes: %" PRIu16 ".", pre_trigger_bytes);
625 sr_dbg("Post trigger sample bytes: %" PRIu16 ".", post_trigger_bytes);
627 devc->pre_trigger_samples = pre_trigger_samples;
628 devc->pre_trigger_bytes = pre_trigger_bytes;
629 devc->post_trigger_bytes = post_trigger_bytes;
632 SR_PRIV int sl2_get_device_info(struct sr_usb_dev_inst usb,
633 struct device_info *dev_info)
635 struct drv_context *drvc;
636 uint8_t buffer[PACKET_LENGTH];
639 drvc = ikalogic_scanalogic2_driver_info.context;
644 if (sr_usb_open(drvc->sr_ctx->libusb_ctx, &usb) != SR_OK)
648 * Determine if a kernel driver is active on this interface and, if so,
651 if (libusb_kernel_driver_active(usb.devhdl, USB_INTERFACE) == 1) {
652 ret = libusb_detach_kernel_driver(usb.devhdl,
656 sr_err("Failed to detach kernel driver: %s.",
657 libusb_error_name(ret));
658 libusb_close(usb.devhdl);
663 ret = libusb_claim_interface(usb.devhdl, USB_INTERFACE);
666 sr_err("Failed to claim interface: %s.",
667 libusb_error_name(ret));
668 libusb_close(usb.devhdl);
672 memset(buffer, 0, sizeof(buffer));
675 * Reset the device to ensure it is in a proper state to request the
676 * device information.
678 buffer[0] = CMD_RESET;
679 if ((ret = sl2_transfer_out(usb.devhdl, buffer)) != PACKET_LENGTH) {
680 sr_err("Resetting of device failed: %s.",
681 libusb_error_name(ret));
682 libusb_release_interface(usb.devhdl, USB_INTERFACE);
683 libusb_close(usb.devhdl);
687 buffer[0] = CMD_INFO;
688 if ((ret = sl2_transfer_out(usb.devhdl, buffer)) != PACKET_LENGTH) {
689 sr_err("Requesting of device information failed: %s.",
690 libusb_error_name(ret));
691 libusb_release_interface(usb.devhdl, USB_INTERFACE);
692 libusb_close(usb.devhdl);
696 if ((ret = sl2_transfer_in(usb.devhdl, buffer)) != PACKET_LENGTH) {
697 sr_err("Receiving of device information failed: %s.",
698 libusb_error_name(ret));
699 libusb_release_interface(usb.devhdl, USB_INTERFACE);
700 libusb_close(usb.devhdl);
704 memcpy(&(dev_info->serial), buffer + 1, sizeof(uint32_t));
705 dev_info->serial = GUINT32_FROM_LE(dev_info->serial);
707 dev_info->fw_ver_major = buffer[5];
708 dev_info->fw_ver_minor = buffer[6];
710 buffer[0] = CMD_RESET;
711 if ((ret = sl2_transfer_out(usb.devhdl, buffer)) != PACKET_LENGTH) {
712 sr_err("Device reset failed: %s.", libusb_error_name(ret));
713 libusb_release_interface(usb.devhdl, USB_INTERFACE);
714 libusb_close(usb.devhdl);
719 * Set the device to idle state. If the device is not in idle state it
720 * possibly will reset itself after a few seconds without being used
721 * and thereby close the connection.
723 buffer[0] = CMD_IDLE;
724 if ((ret = sl2_transfer_out(usb.devhdl, buffer)) != PACKET_LENGTH) {
725 sr_err("Failed to set device in idle state: %s.",
726 libusb_error_name(ret));
727 libusb_release_interface(usb.devhdl, USB_INTERFACE);
728 libusb_close(usb.devhdl);
732 ret = libusb_release_interface(usb.devhdl, USB_INTERFACE);
735 sr_err("Failed to release interface: %s.",
736 libusb_error_name(ret));
737 libusb_close(usb.devhdl);
741 libusb_close(usb.devhdl);
746 SR_PRIV int sl2_transfer_in(libusb_device_handle *dev_handle, uint8_t *data)
748 return libusb_control_transfer(dev_handle, USB_REQUEST_TYPE_IN,
749 USB_HID_GET_REPORT, USB_HID_REPORT_TYPE_FEATURE, USB_INTERFACE,
750 (unsigned char *)data, PACKET_LENGTH, USB_TIMEOUT_MS);
753 SR_PRIV int sl2_transfer_out(libusb_device_handle *dev_handle, uint8_t *data)
755 return libusb_control_transfer(dev_handle, USB_REQUEST_TYPE_OUT,
756 USB_HID_SET_REPORT, USB_HID_REPORT_TYPE_FEATURE, USB_INTERFACE,
757 (unsigned char *)data, PACKET_LENGTH, USB_TIMEOUT_MS);