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;
23 static struct sr_dev_driver *di = &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->priv;
30 struct dev_context *devc;
31 struct sr_datafeed_packet packet;
35 /* Remove USB file descriptors from polling. */
36 usb_source_remove(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->priv;
47 struct dev_context *devc;
48 struct sr_datafeed_packet packet;
52 /* Remove USB file descriptors from polling. */
53 usb_source_remove(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->probes[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_PROBES];
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_probes - 1; i++)
105 ptr[i] = devc->sample_buffer[devc->probe_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_probes; j++) {
160 tmp = (ptr[j][i] & (1 << k)) >> k;
161 buffer[n] |= tmp << devc->probe_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 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))
218 current_time = g_get_monotonic_time();
220 if (devc->state == STATE_WAIT_DATA_READY &&
221 !devc->wait_data_ready_locked) {
222 time_elapsed = current_time - devc->wait_data_ready_time;
225 * Check here for stopping in addition to the transfer
226 * callback functions to avoid waiting until the
227 * WAIT_DATA_READY_INTERVAL has expired.
229 if (sdi->status == SR_ST_STOPPING) {
230 if (!devc->stopping_in_progress) {
231 devc->next_state = STATE_RESET_AND_IDLE;
232 devc->stopping_in_progress = TRUE;
233 ret = libusb_submit_transfer(devc->xfer_in);
235 } else if (time_elapsed >= WAIT_DATA_READY_INTERVAL) {
236 devc->wait_data_ready_locked = TRUE;
237 ret = libusb_submit_transfer(devc->xfer_in);
242 sr_err("Submit transfer failed: %s.", libusb_error_name(ret));
243 abort_acquisition(sdi);
250 libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv,
253 /* Check if an error occurred on a transfer. */
254 if (devc->transfer_error)
255 abort_acquisition(sdi);
260 SR_PRIV void sl2_receive_transfer_in( struct libusb_transfer *transfer)
262 struct sr_dev_inst *sdi;
263 struct dev_context *devc;
264 uint8_t last_channel;
267 sdi = transfer->user_data;
270 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
271 sr_err("Transfer to device failed: %i.", transfer->status);
272 devc->transfer_error = TRUE;
276 if (sdi->status == SR_ST_STOPPING && !devc->stopping_in_progress) {
277 devc->next_state = STATE_RESET_AND_IDLE;
278 devc->stopping_in_progress = TRUE;
280 if (libusb_submit_transfer(devc->xfer_in) != 0) {
281 sr_err("Submit transfer failed: %s.",
282 libusb_error_name(ret));
283 devc->transfer_error = TRUE;
289 if (devc->state != devc->next_state)
290 sr_spew("State changed from %i to %i.",
291 devc->state, devc->next_state);
292 devc->state = devc->next_state;
294 if (devc->state == STATE_WAIT_DATA_READY) {
295 /* Check if the received data are a valid device status. */
296 if (devc->xfer_data_in[0] == 0x05) {
297 if (devc->xfer_data_in[1] == STATUS_WAITING_FOR_TRIGGER)
298 sr_dbg("Waiting for trigger.");
299 else if (devc->xfer_data_in[1] == STATUS_SAMPLING)
300 sr_dbg("Sampling in progress.");
304 * Check if the received data are a valid device status and the
305 * sample data are ready.
307 if (devc->xfer_data_in[0] == 0x05 &&
308 devc->xfer_data_in[1] == STATUS_DATA_READY) {
309 devc->next_state = STATE_RECEIVE_DATA;
310 ret = libusb_submit_transfer(transfer);
312 devc->wait_data_ready_locked = FALSE;
313 devc->wait_data_ready_time = g_get_monotonic_time();
315 } else if (devc->state == STATE_RECEIVE_DATA) {
316 last_channel = devc->probe_map[devc->num_enabled_probes - 1];
318 if (devc->channel < last_channel) {
319 buffer_sample_data(sdi);
320 } else if (devc->channel == last_channel) {
321 process_sample_data(sdi);
324 * Stop acquisition because all samples of enabled
325 * probes are processed.
327 devc->next_state = STATE_RESET_AND_IDLE;
330 devc->sample_packet++;
331 devc->sample_packet %= devc->num_sample_packets;
333 if (devc->sample_packet == 0)
336 ret = libusb_submit_transfer(transfer);
337 } else if (devc->state == STATE_RESET_AND_IDLE) {
338 /* Check if the received data are a valid device status. */
339 if (devc->xfer_data_in[0] == 0x05) {
340 if (devc->xfer_data_in[1] == STATUS_DEVICE_READY) {
341 devc->next_state = STATE_IDLE;
342 devc->xfer_data_out[0] = CMD_IDLE;
344 devc->next_state = STATE_WAIT_DEVICE_READY;
345 devc->xfer_data_out[0] = CMD_RESET;
348 ret = libusb_submit_transfer(devc->xfer_out);
351 * The received device status is invalid which
352 * indicates that the device is not ready to accept
353 * commands. Request a new device status until a valid
354 * device status is received.
356 ret = libusb_submit_transfer(transfer);
358 } else if (devc->state == STATE_WAIT_DEVICE_READY) {
359 /* Check if the received data are a valid device status. */
360 if (devc->xfer_data_in[0] == 0x05) {
361 if (devc->xfer_data_in[1] == STATUS_DEVICE_READY) {
362 devc->next_state = STATE_IDLE;
363 devc->xfer_data_out[0] = CMD_IDLE;
366 * The received device status is valid but the
367 * device is not ready. Probably the device did
368 * not recognize the last reset. Reset the
371 devc->xfer_data_out[0] = CMD_RESET;
374 ret = libusb_submit_transfer(devc->xfer_out);
377 * The device is not ready and therefore not able to
378 * change to the idle state. Request a new device
379 * status until the device is ready.
381 ret = libusb_submit_transfer(transfer);
386 sr_err("Submit transfer failed: %s.", libusb_error_name(ret));
387 devc->transfer_error = TRUE;
391 SR_PRIV void sl2_receive_transfer_out( struct libusb_transfer *transfer)
393 struct sr_dev_inst *sdi;
394 struct dev_context *devc;
397 sdi = transfer->user_data;
400 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
401 sr_err("Transfer to device failed: %i.", transfer->status);
402 devc->transfer_error = TRUE;
406 if (sdi->status == SR_ST_STOPPING && !devc->stopping_in_progress) {
407 devc->next_state = STATE_RESET_AND_IDLE;
408 devc->stopping_in_progress = TRUE;
410 if (libusb_submit_transfer(devc->xfer_in) != 0) {
411 sr_err("Submit transfer failed: %s.",
412 libusb_error_name(ret));
414 devc->transfer_error = TRUE;
420 if (devc->state != devc->next_state)
421 sr_spew("State changed from %i to %i.",
422 devc->state, devc->next_state);
423 devc->state = devc->next_state;
425 if (devc->state == STATE_IDLE) {
426 stop_acquisition(sdi);
427 } else if (devc->state == STATE_SAMPLE) {
428 devc->next_state = STATE_WAIT_DATA_READY;
429 ret = libusb_submit_transfer(devc->xfer_in);
430 } else if (devc->state == STATE_WAIT_DEVICE_READY) {
431 ret = libusb_submit_transfer(devc->xfer_in);
435 sr_err("Submit transfer failed: %s.", libusb_error_name(ret));
436 devc->transfer_error = TRUE;
440 SR_PRIV int sl2_set_samplerate(const struct sr_dev_inst *sdi,
443 struct dev_context *devc;
448 for (i = 0; i < NUM_SAMPLERATES; i++) {
449 if (sl2_samplerates[i] == samplerate) {
450 devc->samplerate = samplerate;
451 devc->samplerate_id = NUM_SAMPLERATES - i - 1;
459 SR_PRIV int sl2_set_limit_samples(const struct sr_dev_inst *sdi,
460 uint64_t limit_samples)
462 struct dev_context *devc;
466 if (limit_samples == 0) {
467 sr_err("Invalid number of limit samples: %" PRIu64 ".",
472 if (limit_samples > MAX_SAMPLES)
473 limit_samples = MAX_SAMPLES;
475 sr_dbg("Limit samples set to %" PRIu64 ".", limit_samples);
477 devc->limit_samples = limit_samples;
482 SR_PRIV void sl2_configure_trigger(const struct sr_dev_inst *sdi)
484 struct dev_context *devc;
485 struct sr_probe *probe;
486 uint8_t trigger_type;
487 int probe_index, num_triggers_anyedge;
493 /* Disable the trigger by default. */
494 devc->trigger_channel = TRIGGER_CHANNEL_0;
495 devc->trigger_type = TRIGGER_TYPE_NONE;
497 num_triggers_anyedge = 0;
499 for (l = sdi->probes, probe_index = 0; l; l = l->next, probe_index++) {
501 trigger = probe->trigger;
503 if (!trigger || !probe->enabled)
508 trigger_type = TRIGGER_TYPE_POSEDGE;
511 trigger_type = TRIGGER_TYPE_NEGEDGE;
514 trigger_type = TRIGGER_TYPE_ANYEDGE;
515 num_triggers_anyedge++;
521 devc->trigger_channel = probe_index + 1;
522 devc->trigger_type = trigger_type;
526 * Set trigger to any edge on all channels if the trigger for each
527 * channel is set to any edge.
529 if (num_triggers_anyedge == NUM_PROBES) {
530 devc->trigger_channel = TRIGGER_CHANNEL_ALL;
531 devc->trigger_type = TRIGGER_TYPE_ANYEDGE;
534 sr_dbg("Trigger set to channel 0x%02x and type 0x%02x.",
535 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];
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);
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);