2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 Marcus Comstedt <marcus@mc.pp.se>
5 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
6 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 #include <libsigrok/libsigrok.h>
29 #include "libsigrok-internal.h"
32 #define LOGIC16_VID 0x21a9
33 #define LOGIC16_PID 0x1001
35 #define USB_INTERFACE 0
36 #define USB_CONFIGURATION 1
37 #define FX2_FIRMWARE "saleae-logic16-fx2.fw"
39 #define MAX_RENUM_DELAY_MS 3000
40 #define NUM_SIMUL_TRANSFERS 32
42 SR_PRIV struct sr_dev_driver saleae_logic16_driver_info;
44 static const uint32_t scanopts[] = {
48 static const uint32_t devopts[] = {
49 SR_CONF_LOGIC_ANALYZER,
51 SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
52 SR_CONF_CONN | SR_CONF_GET,
53 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
54 SR_CONF_VOLTAGE_THRESHOLD | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
55 SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
56 SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET,
59 static const int32_t soft_trigger_matches[] = {
67 static const char *channel_names[] = {
68 "0", "1", "2", "3", "4", "5", "6", "7", "8",
69 "9", "10", "11", "12", "13", "14", "15",
73 enum voltage_range range;
76 } volt_thresholds[] = {
77 { VOLTAGE_RANGE_18_33_V, 0.7, 1.4 },
78 { VOLTAGE_RANGE_5_V, 1.4, 3.6 },
81 static const uint64_t samplerates[] = {
98 static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
100 return std_init(di, sr_ctx);
103 static gboolean check_conf_profile(libusb_device *dev)
105 struct libusb_device_descriptor des;
106 struct libusb_device_handle *hdl;
108 unsigned char strdesc[64];
113 /* Assume the FW has not been loaded, unless proven wrong. */
114 libusb_get_device_descriptor(dev, &des);
116 if (libusb_open(dev, &hdl) != 0)
119 if (libusb_get_string_descriptor_ascii(hdl,
120 des.iManufacturer, strdesc, sizeof(strdesc)) < 0)
122 if (strcmp((const char *)strdesc, "Saleae LLC"))
125 if (libusb_get_string_descriptor_ascii(hdl,
126 des.iProduct, strdesc, sizeof(strdesc)) < 0)
128 if (strcmp((const char *)strdesc, "Logic S/16"))
131 /* If we made it here, it must be a configured Logic16. */
140 static GSList *scan(struct sr_dev_driver *di, GSList *options)
142 struct drv_context *drvc;
143 struct dev_context *devc;
144 struct sr_dev_inst *sdi;
145 struct sr_usb_dev_inst *usb;
146 struct sr_config *src;
147 GSList *l, *devices, *conn_devices;
148 struct libusb_device_descriptor des;
149 libusb_device **devlist;
152 char connection_id[64];
157 for (l = options; l; l = l->next) {
161 conn = g_variant_get_string(src->data, NULL);
166 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
170 /* Find all Logic16 devices and upload firmware to them. */
172 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
173 for (i = 0; devlist[i]; i++) {
176 for (l = conn_devices; l; l = l->next) {
178 if (usb->bus == libusb_get_bus_number(devlist[i])
179 && usb->address == libusb_get_device_address(devlist[i]))
183 /* This device matched none of the ones that
184 * matched the conn specification. */
188 libusb_get_device_descriptor(devlist[i], &des);
190 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
192 if (des.idVendor != LOGIC16_VID || des.idProduct != LOGIC16_PID)
195 sdi = g_malloc0(sizeof(struct sr_dev_inst));
196 sdi->status = SR_ST_INITIALIZING;
197 sdi->vendor = g_strdup("Saleae");
198 sdi->model = g_strdup("Logic16");
200 sdi->connection_id = g_strdup(connection_id);
202 for (j = 0; j < ARRAY_SIZE(channel_names); j++)
203 sr_channel_new(sdi, j, SR_CHANNEL_LOGIC, TRUE,
206 devc = g_malloc0(sizeof(struct dev_context));
207 devc->selected_voltage_range = VOLTAGE_RANGE_18_33_V;
209 drvc->instances = g_slist_append(drvc->instances, sdi);
210 devices = g_slist_append(devices, sdi);
212 if (check_conf_profile(devlist[i])) {
213 /* Already has the firmware, so fix the new address. */
214 sr_dbg("Found a Logic16 device.");
215 sdi->status = SR_ST_INACTIVE;
216 sdi->inst_type = SR_INST_USB;
217 sdi->conn = sr_usb_dev_inst_new(
218 libusb_get_bus_number(devlist[i]),
219 libusb_get_device_address(devlist[i]), NULL);
221 if (ezusb_upload_firmware(drvc->sr_ctx, devlist[i],
222 USB_CONFIGURATION, FX2_FIRMWARE) == SR_OK)
223 /* Store when this device's FW was updated. */
224 devc->fw_updated = g_get_monotonic_time();
226 sr_err("Firmware upload failed.");
227 sdi->inst_type = SR_INST_USB;
228 sdi->conn = sr_usb_dev_inst_new(
229 libusb_get_bus_number(devlist[i]), 0xff, NULL);
232 libusb_free_device_list(devlist, 1);
233 g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
238 static int logic16_dev_open(struct sr_dev_inst *sdi)
240 struct sr_dev_driver *di;
241 libusb_device **devlist;
242 struct sr_usb_dev_inst *usb;
243 struct libusb_device_descriptor des;
244 struct drv_context *drvc;
245 int ret, i, device_count;
246 char connection_id[64];
252 if (sdi->status == SR_ST_ACTIVE)
253 /* Device is already in use. */
256 device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
257 if (device_count < 0) {
258 sr_err("Failed to get device list: %s.",
259 libusb_error_name(device_count));
263 for (i = 0; i < device_count; i++) {
264 libusb_get_device_descriptor(devlist[i], &des);
266 if (des.idVendor != LOGIC16_VID || des.idProduct != LOGIC16_PID)
269 if ((sdi->status == SR_ST_INITIALIZING) ||
270 (sdi->status == SR_ST_INACTIVE)) {
272 * Check device by its physical USB bus/port address.
274 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
275 if (strcmp(sdi->connection_id, connection_id))
276 /* This is not the one. */
280 if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
281 if (usb->address == 0xff)
283 * First time we touch this device after FW
284 * upload, so we don't know the address yet.
286 usb->address = libusb_get_device_address(devlist[i]);
288 sr_err("Failed to open device: %s.",
289 libusb_error_name(ret));
293 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
294 if (ret == LIBUSB_ERROR_BUSY) {
295 sr_err("Unable to claim USB interface. Another "
296 "program or driver has already claimed it.");
298 } else if (ret == LIBUSB_ERROR_NO_DEVICE) {
299 sr_err("Device has been disconnected.");
301 } else if (ret != 0) {
302 sr_err("Unable to claim interface: %s.",
303 libusb_error_name(ret));
307 if ((ret = logic16_init_device(sdi)) != SR_OK) {
308 sr_err("Failed to init device.");
312 sdi->status = SR_ST_ACTIVE;
313 sr_info("Opened device on %d.%d (logical) / %s (physical), interface %d.",
314 usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
318 libusb_free_device_list(devlist, 1);
320 if (sdi->status != SR_ST_ACTIVE) {
322 libusb_release_interface(usb->devhdl, USB_INTERFACE);
323 libusb_close(usb->devhdl);
332 static int dev_open(struct sr_dev_inst *sdi)
334 struct dev_context *devc;
336 int64_t timediff_us, timediff_ms;
341 * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS
342 * milliseconds for the FX2 to renumerate.
345 if (devc->fw_updated > 0) {
346 sr_info("Waiting for device to reset.");
347 /* Takes >= 300ms for the FX2 to be gone from the USB bus. */
348 g_usleep(300 * 1000);
350 while (timediff_ms < MAX_RENUM_DELAY_MS) {
351 if ((ret = logic16_dev_open(sdi)) == SR_OK)
353 g_usleep(100 * 1000);
355 timediff_us = g_get_monotonic_time() - devc->fw_updated;
356 timediff_ms = timediff_us / 1000;
357 sr_spew("Waited %" PRIi64 "ms.", timediff_ms);
360 sr_err("Device failed to renumerate.");
363 sr_info("Device came back after %" PRIi64 "ms.", timediff_ms);
365 sr_info("Firmware upload was not needed.");
366 ret = logic16_dev_open(sdi);
370 sr_err("Unable to open device.");
374 if (devc->cur_samplerate == 0) {
375 /* Samplerate hasn't been set; default to the slowest one. */
376 devc->cur_samplerate = samplerates[0];
382 static int dev_close(struct sr_dev_inst *sdi)
384 struct sr_usb_dev_inst *usb;
390 sr_info("Closing device on %d.%d (logical) / %s (physical) interface %d.",
391 usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
392 libusb_release_interface(usb->devhdl, USB_INTERFACE);
393 libusb_close(usb->devhdl);
395 sdi->status = SR_ST_INACTIVE;
400 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
401 const struct sr_channel_group *cg)
403 struct dev_context *devc;
404 struct sr_usb_dev_inst *usb;
415 if (!sdi || !sdi->conn)
418 if (usb->address == 255)
419 /* Device still needs to re-enumerate after firmware
420 * upload, so we don't know its (future) address. */
422 snprintf(str, 128, "%d.%d", usb->bus, usb->address);
423 *data = g_variant_new_string(str);
425 case SR_CONF_SAMPLERATE:
429 *data = g_variant_new_uint64(devc->cur_samplerate);
431 case SR_CONF_CAPTURE_RATIO:
435 *data = g_variant_new_uint64(devc->capture_ratio);
437 case SR_CONF_VOLTAGE_THRESHOLD:
442 for (i = 0; i < ARRAY_SIZE(volt_thresholds); i++) {
443 if (devc->selected_voltage_range !=
444 volt_thresholds[i].range)
446 range[0] = g_variant_new_double(volt_thresholds[i].low);
447 range[1] = g_variant_new_double(volt_thresholds[i].high);
448 *data = g_variant_new_tuple(range, 2);
460 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
461 const struct sr_channel_group *cg)
463 struct dev_context *devc;
470 if (sdi->status != SR_ST_ACTIVE)
471 return SR_ERR_DEV_CLOSED;
477 case SR_CONF_SAMPLERATE:
478 devc->cur_samplerate = g_variant_get_uint64(data);
480 case SR_CONF_LIMIT_SAMPLES:
481 devc->limit_samples = g_variant_get_uint64(data);
483 case SR_CONF_CAPTURE_RATIO:
484 devc->capture_ratio = g_variant_get_uint64(data);
485 ret = (devc->capture_ratio > 100) ? SR_ERR : SR_OK;
487 case SR_CONF_VOLTAGE_THRESHOLD:
488 g_variant_get(data, "(dd)", &low, &high);
490 for (i = 0; i < ARRAY_SIZE(volt_thresholds); i++) {
491 if (fabs(volt_thresholds[i].low - low) < 0.1 &&
492 fabs(volt_thresholds[i].high - high) < 0.1) {
493 devc->selected_voltage_range =
494 volt_thresholds[i].range;
507 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
508 const struct sr_channel_group *cg)
510 GVariant *gvar, *range[2];
520 case SR_CONF_SCAN_OPTIONS:
521 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
522 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
524 case SR_CONF_DEVICE_OPTIONS:
525 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
526 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
528 case SR_CONF_SAMPLERATE:
529 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
530 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
531 samplerates, ARRAY_SIZE(samplerates), sizeof(uint64_t));
532 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
533 *data = g_variant_builder_end(&gvb);
535 case SR_CONF_VOLTAGE_THRESHOLD:
536 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
537 for (i = 0; i < ARRAY_SIZE(volt_thresholds); i++) {
538 range[0] = g_variant_new_double(volt_thresholds[i].low);
539 range[1] = g_variant_new_double(volt_thresholds[i].high);
540 gvar = g_variant_new_tuple(range, 2);
541 g_variant_builder_add_value(&gvb, gvar);
543 *data = g_variant_builder_end(&gvb);
545 case SR_CONF_TRIGGER_MATCH:
546 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
547 soft_trigger_matches, ARRAY_SIZE(soft_trigger_matches),
557 static void abort_acquisition(struct dev_context *devc)
561 devc->sent_samples = -1;
563 for (i = devc->num_transfers - 1; i >= 0; i--) {
564 if (devc->transfers[i])
565 libusb_cancel_transfer(devc->transfers[i]);
569 static unsigned int bytes_per_ms(struct dev_context *devc)
571 return devc->cur_samplerate * devc->num_channels / 8000;
574 static size_t get_buffer_size(struct dev_context *devc)
579 * The buffer should be large enough to hold 10ms of data and
582 s = 10 * bytes_per_ms(devc);
583 return (s + 511) & ~511;
586 static unsigned int get_number_of_transfers(struct dev_context *devc)
590 /* Total buffer size should be able to hold about 500ms of data. */
591 n = 500 * bytes_per_ms(devc) / get_buffer_size(devc);
593 if (n > NUM_SIMUL_TRANSFERS)
594 return NUM_SIMUL_TRANSFERS;
599 static unsigned int get_timeout(struct dev_context *devc)
602 unsigned int timeout;
604 total_size = get_buffer_size(devc) * get_number_of_transfers(devc);
605 timeout = total_size / bytes_per_ms(devc);
606 return timeout + timeout / 4; /* Leave a headroom of 25% percent. */
609 static int configure_channels(const struct sr_dev_inst *sdi)
611 struct dev_context *devc;
612 struct sr_channel *ch;
614 uint16_t channel_bit;
618 devc->cur_channels = 0;
619 devc->num_channels = 0;
620 for (l = sdi->channels; l; l = l->next) {
621 ch = (struct sr_channel *)l->data;
622 if (ch->enabled == FALSE)
625 channel_bit = 1 << (ch->index);
627 devc->cur_channels |= channel_bit;
629 #ifdef WORDS_BIGENDIAN
631 * Output logic data should be stored in little endian format.
632 * To speed things up during conversion, do the switcharoo
635 channel_bit = 1 << (ch->index ^ 8);
638 devc->channel_masks[devc->num_channels++] = channel_bit;
644 static int receive_data(int fd, int revents, void *cb_data)
647 struct dev_context *devc;
648 struct drv_context *drvc;
649 const struct sr_dev_inst *sdi;
650 struct sr_dev_driver *di;
660 tv.tv_sec = tv.tv_usec = 0;
661 libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
663 if (devc->sent_samples == -2) {
664 logic16_abort_acquisition(sdi);
665 abort_acquisition(devc);
671 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
673 struct sr_dev_driver *di = sdi->driver;
674 struct dev_context *devc;
675 struct drv_context *drvc;
676 struct sr_usb_dev_inst *usb;
677 struct sr_trigger *trigger;
678 struct libusb_transfer *transfer;
679 unsigned int i, timeout, num_transfers;
682 size_t size, convsize;
684 if (sdi->status != SR_ST_ACTIVE)
685 return SR_ERR_DEV_CLOSED;
691 /* Configures devc->cur_channels. */
692 if (configure_channels(sdi) != SR_OK) {
693 sr_err("Failed to configure channels.");
697 devc->sent_samples = 0;
698 devc->empty_transfer_count = 0;
699 devc->cur_channel = 0;
700 memset(devc->channel_data, 0, sizeof(devc->channel_data));
702 if ((trigger = sr_session_trigger_get(sdi->session))) {
703 int pre_trigger_samples = 0;
704 if (devc->limit_samples > 0)
705 pre_trigger_samples = devc->capture_ratio * devc->limit_samples/100;
706 devc->stl = soft_trigger_logic_new(sdi, trigger, pre_trigger_samples);
708 return SR_ERR_MALLOC;
709 devc->trigger_fired = FALSE;
711 devc->trigger_fired = TRUE;
713 timeout = get_timeout(devc);
714 num_transfers = get_number_of_transfers(devc);
715 size = get_buffer_size(devc);
716 convsize = (size / devc->num_channels + 2) * 16;
717 devc->submitted_transfers = 0;
719 devc->convbuffer_size = convsize;
720 if (!(devc->convbuffer = g_try_malloc(convsize))) {
721 sr_err("Conversion buffer malloc failed.");
722 return SR_ERR_MALLOC;
725 devc->transfers = g_try_malloc0(sizeof(*devc->transfers) * num_transfers);
726 if (!devc->transfers) {
727 sr_err("USB transfers malloc failed.");
728 g_free(devc->convbuffer);
729 return SR_ERR_MALLOC;
732 if ((ret = logic16_setup_acquisition(sdi, devc->cur_samplerate,
733 devc->cur_channels)) != SR_OK) {
734 g_free(devc->transfers);
735 g_free(devc->convbuffer);
739 devc->num_transfers = num_transfers;
740 for (i = 0; i < num_transfers; i++) {
741 if (!(buf = g_try_malloc(size))) {
742 sr_err("USB transfer buffer malloc failed.");
743 if (devc->submitted_transfers)
744 abort_acquisition(devc);
746 g_free(devc->transfers);
747 g_free(devc->convbuffer);
749 return SR_ERR_MALLOC;
751 transfer = libusb_alloc_transfer(0);
752 libusb_fill_bulk_transfer(transfer, usb->devhdl,
753 2 | LIBUSB_ENDPOINT_IN, buf, size,
754 logic16_receive_transfer, (void *)sdi, timeout);
755 if ((ret = libusb_submit_transfer(transfer)) != 0) {
756 sr_err("Failed to submit transfer: %s.",
757 libusb_error_name(ret));
758 libusb_free_transfer(transfer);
760 abort_acquisition(devc);
763 devc->transfers[i] = transfer;
764 devc->submitted_transfers++;
767 devc->ctx = drvc->sr_ctx;
769 usb_source_add(sdi->session, devc->ctx, timeout, receive_data, (void *)sdi);
771 std_session_send_df_header(sdi, LOG_PREFIX);
773 if ((ret = logic16_start_acquisition(sdi)) != SR_OK) {
774 abort_acquisition(devc);
781 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
785 if (sdi->status != SR_ST_ACTIVE)
786 return SR_ERR_DEV_CLOSED;
788 ret = logic16_abort_acquisition(sdi);
790 abort_acquisition(sdi->priv);
795 SR_PRIV struct sr_dev_driver saleae_logic16_driver_info = {
796 .name = "saleae-logic16",
797 .longname = "Saleae Logic16",
800 .cleanup = std_cleanup,
802 .dev_list = std_dev_list,
804 .config_get = config_get,
805 .config_set = config_set,
806 .config_list = config_list,
807 .dev_open = dev_open,
808 .dev_close = dev_close,
809 .dev_acquisition_start = dev_acquisition_start,
810 .dev_acquisition_stop = dev_acquisition_stop,