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 static const uint32_t scanopts[] = {
46 static const uint32_t drvopts[] = {
47 SR_CONF_LOGIC_ANALYZER,
50 static const uint32_t devopts[] = {
52 SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
53 SR_CONF_CONN | SR_CONF_GET,
54 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
55 SR_CONF_VOLTAGE_THRESHOLD | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
56 SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
57 SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET,
60 static const int32_t soft_trigger_matches[] = {
68 static const char *channel_names[] = {
69 "0", "1", "2", "3", "4", "5", "6", "7", "8",
70 "9", "10", "11", "12", "13", "14", "15",
74 enum voltage_range range;
77 } volt_thresholds[] = {
78 { VOLTAGE_RANGE_18_33_V, 0.7, 1.4 },
79 { VOLTAGE_RANGE_5_V, 1.4, 3.6 },
82 static const uint64_t samplerates[] = {
101 static gboolean check_conf_profile(libusb_device *dev)
103 struct libusb_device_descriptor des;
104 struct libusb_device_handle *hdl;
106 unsigned char strdesc[64];
111 /* Assume the FW has not been loaded, unless proven wrong. */
112 libusb_get_device_descriptor(dev, &des);
114 if (libusb_open(dev, &hdl) != 0)
117 if (libusb_get_string_descriptor_ascii(hdl,
118 des.iManufacturer, strdesc, sizeof(strdesc)) < 0)
120 if (strcmp((const char *)strdesc, "Saleae LLC"))
123 if (libusb_get_string_descriptor_ascii(hdl,
124 des.iProduct, strdesc, sizeof(strdesc)) < 0)
126 if (strcmp((const char *)strdesc, "Logic S/16"))
129 /* If we made it here, it must be a configured Logic16. */
138 static GSList *scan(struct sr_dev_driver *di, GSList *options)
140 struct drv_context *drvc;
141 struct dev_context *devc;
142 struct sr_dev_inst *sdi;
143 struct sr_usb_dev_inst *usb;
144 struct sr_config *src;
145 GSList *l, *devices, *conn_devices;
146 struct libusb_device_descriptor des;
147 libusb_device **devlist;
150 char connection_id[64];
155 for (l = options; l; l = l->next) {
159 conn = g_variant_get_string(src->data, NULL);
164 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
168 /* Find all Logic16 devices and upload firmware to them. */
170 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
171 for (i = 0; devlist[i]; i++) {
174 for (l = conn_devices; l; l = l->next) {
176 if (usb->bus == libusb_get_bus_number(devlist[i])
177 && usb->address == libusb_get_device_address(devlist[i]))
181 /* This device matched none of the ones that
182 * matched the conn specification. */
186 libusb_get_device_descriptor(devlist[i], &des);
188 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
190 if (des.idVendor != LOGIC16_VID || des.idProduct != LOGIC16_PID)
193 sdi = g_malloc0(sizeof(struct sr_dev_inst));
194 sdi->status = SR_ST_INITIALIZING;
195 sdi->vendor = g_strdup("Saleae");
196 sdi->model = g_strdup("Logic16");
197 sdi->connection_id = g_strdup(connection_id);
199 for (j = 0; j < ARRAY_SIZE(channel_names); j++)
200 sr_channel_new(sdi, j, SR_CHANNEL_LOGIC, TRUE,
203 devc = g_malloc0(sizeof(struct dev_context));
204 devc->selected_voltage_range = VOLTAGE_RANGE_18_33_V;
206 devices = g_slist_append(devices, sdi);
208 if (check_conf_profile(devlist[i])) {
209 /* Already has the firmware, so fix the new address. */
210 sr_dbg("Found a Logic16 device.");
211 sdi->status = SR_ST_INACTIVE;
212 sdi->inst_type = SR_INST_USB;
213 sdi->conn = sr_usb_dev_inst_new(
214 libusb_get_bus_number(devlist[i]),
215 libusb_get_device_address(devlist[i]), NULL);
217 if (ezusb_upload_firmware(drvc->sr_ctx, devlist[i],
218 USB_CONFIGURATION, FX2_FIRMWARE) == SR_OK)
219 /* Store when this device's FW was updated. */
220 devc->fw_updated = g_get_monotonic_time();
222 sr_err("Firmware upload failed.");
223 sdi->inst_type = SR_INST_USB;
224 sdi->conn = sr_usb_dev_inst_new(
225 libusb_get_bus_number(devlist[i]), 0xff, NULL);
228 libusb_free_device_list(devlist, 1);
229 g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
231 return std_scan_complete(di, devices);
234 static int logic16_dev_open(struct sr_dev_inst *sdi)
236 struct sr_dev_driver *di;
237 libusb_device **devlist;
238 struct sr_usb_dev_inst *usb;
239 struct libusb_device_descriptor des;
240 struct drv_context *drvc;
241 int ret = SR_ERR, i, device_count;
242 char connection_id[64];
248 device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
249 if (device_count < 0) {
250 sr_err("Failed to get device list: %s.",
251 libusb_error_name(device_count));
255 for (i = 0; i < device_count; i++) {
256 libusb_get_device_descriptor(devlist[i], &des);
258 if (des.idVendor != LOGIC16_VID || des.idProduct != LOGIC16_PID)
261 if ((sdi->status == SR_ST_INITIALIZING) ||
262 (sdi->status == SR_ST_INACTIVE)) {
264 * Check device by its physical USB bus/port address.
266 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
267 if (strcmp(sdi->connection_id, connection_id))
268 /* This is not the one. */
272 if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
273 if (usb->address == 0xff)
275 * First time we touch this device after FW
276 * upload, so we don't know the address yet.
278 usb->address = libusb_get_device_address(devlist[i]);
280 sr_err("Failed to open device: %s.",
281 libusb_error_name(ret));
286 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
287 if (ret == LIBUSB_ERROR_BUSY) {
288 sr_err("Unable to claim USB interface. Another "
289 "program or driver has already claimed it.");
292 } else if (ret == LIBUSB_ERROR_NO_DEVICE) {
293 sr_err("Device has been disconnected.");
296 } else if (ret != 0) {
297 sr_err("Unable to claim interface: %s.",
298 libusb_error_name(ret));
303 if ((ret = logic16_init_device(sdi)) != SR_OK) {
304 sr_err("Failed to init device.");
308 sr_info("Opened device on %d.%d (logical) / %s (physical), interface %d.",
309 usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
316 libusb_free_device_list(devlist, 1);
320 libusb_release_interface(usb->devhdl, USB_INTERFACE);
321 libusb_close(usb->devhdl);
330 static int dev_open(struct sr_dev_inst *sdi)
332 struct dev_context *devc;
334 int64_t timediff_us, timediff_ms;
339 * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS
340 * milliseconds for the FX2 to renumerate.
343 if (devc->fw_updated > 0) {
344 sr_info("Waiting for device to reset.");
345 /* Takes >= 300ms for the FX2 to be gone from the USB bus. */
346 g_usleep(300 * 1000);
348 while (timediff_ms < MAX_RENUM_DELAY_MS) {
349 if ((ret = logic16_dev_open(sdi)) == SR_OK)
351 g_usleep(100 * 1000);
353 timediff_us = g_get_monotonic_time() - devc->fw_updated;
354 timediff_ms = timediff_us / 1000;
355 sr_spew("Waited %" PRIi64 "ms.", timediff_ms);
358 sr_err("Device failed to renumerate.");
361 sr_info("Device came back after %" PRIi64 "ms.", timediff_ms);
363 sr_info("Firmware upload was not needed.");
364 ret = logic16_dev_open(sdi);
368 sr_err("Unable to open device.");
372 if (devc->cur_samplerate == 0) {
373 /* Samplerate hasn't been set; default to the slowest one. */
374 devc->cur_samplerate = samplerates[0];
380 static int dev_close(struct sr_dev_inst *sdi)
382 struct sr_usb_dev_inst *usb;
389 sr_info("Closing device on %d.%d (logical) / %s (physical) interface %d.",
390 usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
391 libusb_release_interface(usb->devhdl, USB_INTERFACE);
392 libusb_close(usb->devhdl);
398 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
399 const struct sr_channel_group *cg)
401 struct dev_context *devc;
402 struct sr_usb_dev_inst *usb;
413 if (!sdi || !sdi->conn)
416 if (usb->address == 255)
417 /* Device still needs to re-enumerate after firmware
418 * upload, so we don't know its (future) address. */
420 snprintf(str, 128, "%d.%d", usb->bus, usb->address);
421 *data = g_variant_new_string(str);
423 case SR_CONF_SAMPLERATE:
427 *data = g_variant_new_uint64(devc->cur_samplerate);
429 case SR_CONF_CAPTURE_RATIO:
433 *data = g_variant_new_uint64(devc->capture_ratio);
435 case SR_CONF_VOLTAGE_THRESHOLD:
440 for (i = 0; i < ARRAY_SIZE(volt_thresholds); i++) {
441 if (devc->selected_voltage_range !=
442 volt_thresholds[i].range)
444 range[0] = g_variant_new_double(volt_thresholds[i].low);
445 range[1] = g_variant_new_double(volt_thresholds[i].high);
446 *data = g_variant_new_tuple(range, 2);
458 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
459 const struct sr_channel_group *cg)
461 struct dev_context *devc;
472 case SR_CONF_SAMPLERATE:
473 devc->cur_samplerate = g_variant_get_uint64(data);
475 case SR_CONF_LIMIT_SAMPLES:
476 devc->limit_samples = g_variant_get_uint64(data);
478 case SR_CONF_CAPTURE_RATIO:
479 devc->capture_ratio = g_variant_get_uint64(data);
480 ret = (devc->capture_ratio > 100) ? SR_ERR : SR_OK;
482 case SR_CONF_VOLTAGE_THRESHOLD:
483 g_variant_get(data, "(dd)", &low, &high);
485 for (i = 0; i < ARRAY_SIZE(volt_thresholds); i++) {
486 if (fabs(volt_thresholds[i].low - low) < 0.1 &&
487 fabs(volt_thresholds[i].high - high) < 0.1) {
488 devc->selected_voltage_range =
489 volt_thresholds[i].range;
502 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
503 const struct sr_channel_group *cg)
505 GVariant *gvar, *range[2];
510 case SR_CONF_SCAN_OPTIONS:
511 case SR_CONF_DEVICE_OPTIONS:
512 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
513 case SR_CONF_SAMPLERATE:
514 *data = std_gvar_samplerates(samplerates, ARRAY_SIZE(samplerates));
516 case SR_CONF_VOLTAGE_THRESHOLD:
517 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
518 for (i = 0; i < ARRAY_SIZE(volt_thresholds); i++) {
519 range[0] = g_variant_new_double(volt_thresholds[i].low);
520 range[1] = g_variant_new_double(volt_thresholds[i].high);
521 gvar = g_variant_new_tuple(range, 2);
522 g_variant_builder_add_value(&gvb, gvar);
524 *data = g_variant_builder_end(&gvb);
526 case SR_CONF_TRIGGER_MATCH:
527 *data = std_gvar_array_i32(soft_trigger_matches, ARRAY_SIZE(soft_trigger_matches));
536 static void abort_acquisition(struct dev_context *devc)
540 devc->sent_samples = -1;
542 for (i = devc->num_transfers - 1; i >= 0; i--) {
543 if (devc->transfers[i])
544 libusb_cancel_transfer(devc->transfers[i]);
548 static unsigned int bytes_per_ms(struct dev_context *devc)
550 return devc->cur_samplerate * devc->num_channels / 8000;
553 static size_t get_buffer_size(struct dev_context *devc)
558 * The buffer should be large enough to hold 10ms of data and
561 s = 10 * bytes_per_ms(devc);
562 return (s + 511) & ~511;
565 static unsigned int get_number_of_transfers(struct dev_context *devc)
569 /* Total buffer size should be able to hold about 500ms of data. */
570 n = 500 * bytes_per_ms(devc) / get_buffer_size(devc);
572 if (n > NUM_SIMUL_TRANSFERS)
573 return NUM_SIMUL_TRANSFERS;
578 static unsigned int get_timeout(struct dev_context *devc)
581 unsigned int timeout;
583 total_size = get_buffer_size(devc) * get_number_of_transfers(devc);
584 timeout = total_size / bytes_per_ms(devc);
585 return timeout + timeout / 4; /* Leave a headroom of 25% percent. */
588 static int configure_channels(const struct sr_dev_inst *sdi)
590 struct dev_context *devc;
591 struct sr_channel *ch;
593 uint16_t channel_bit;
597 devc->cur_channels = 0;
598 devc->num_channels = 0;
599 for (l = sdi->channels; l; l = l->next) {
600 ch = (struct sr_channel *)l->data;
601 if (ch->enabled == FALSE)
604 channel_bit = 1 << (ch->index);
606 devc->cur_channels |= channel_bit;
608 #ifdef WORDS_BIGENDIAN
610 * Output logic data should be stored in little endian format.
611 * To speed things up during conversion, do the switcharoo
614 channel_bit = 1 << (ch->index ^ 8);
617 devc->channel_masks[devc->num_channels++] = channel_bit;
623 static int receive_data(int fd, int revents, void *cb_data)
626 struct dev_context *devc;
627 struct drv_context *drvc;
628 const struct sr_dev_inst *sdi;
629 struct sr_dev_driver *di;
639 tv.tv_sec = tv.tv_usec = 0;
640 libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
642 if (devc->sent_samples == -2) {
643 logic16_abort_acquisition(sdi);
644 abort_acquisition(devc);
650 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
652 struct sr_dev_driver *di = sdi->driver;
653 struct dev_context *devc;
654 struct drv_context *drvc;
655 struct sr_usb_dev_inst *usb;
656 struct sr_trigger *trigger;
657 struct libusb_transfer *transfer;
658 unsigned int i, timeout, num_transfers;
661 size_t size, convsize;
667 /* Configures devc->cur_channels. */
668 if (configure_channels(sdi) != SR_OK) {
669 sr_err("Failed to configure channels.");
673 devc->sent_samples = 0;
674 devc->empty_transfer_count = 0;
675 devc->cur_channel = 0;
676 memset(devc->channel_data, 0, sizeof(devc->channel_data));
678 if ((trigger = sr_session_trigger_get(sdi->session))) {
679 int pre_trigger_samples = 0;
680 if (devc->limit_samples > 0)
681 pre_trigger_samples = devc->capture_ratio * devc->limit_samples/100;
682 devc->stl = soft_trigger_logic_new(sdi, trigger, pre_trigger_samples);
684 return SR_ERR_MALLOC;
685 devc->trigger_fired = FALSE;
687 devc->trigger_fired = TRUE;
689 timeout = get_timeout(devc);
690 num_transfers = get_number_of_transfers(devc);
691 size = get_buffer_size(devc);
692 convsize = (size / devc->num_channels + 2) * 16;
693 devc->submitted_transfers = 0;
695 devc->convbuffer_size = convsize;
696 if (!(devc->convbuffer = g_try_malloc(convsize))) {
697 sr_err("Conversion buffer malloc failed.");
698 return SR_ERR_MALLOC;
701 devc->transfers = g_try_malloc0(sizeof(*devc->transfers) * num_transfers);
702 if (!devc->transfers) {
703 sr_err("USB transfers malloc failed.");
704 g_free(devc->convbuffer);
705 return SR_ERR_MALLOC;
708 if ((ret = logic16_setup_acquisition(sdi, devc->cur_samplerate,
709 devc->cur_channels)) != SR_OK) {
710 g_free(devc->transfers);
711 g_free(devc->convbuffer);
715 devc->num_transfers = num_transfers;
716 for (i = 0; i < num_transfers; i++) {
717 if (!(buf = g_try_malloc(size))) {
718 sr_err("USB transfer buffer malloc failed.");
719 if (devc->submitted_transfers)
720 abort_acquisition(devc);
722 g_free(devc->transfers);
723 g_free(devc->convbuffer);
725 return SR_ERR_MALLOC;
727 transfer = libusb_alloc_transfer(0);
728 libusb_fill_bulk_transfer(transfer, usb->devhdl,
729 2 | LIBUSB_ENDPOINT_IN, buf, size,
730 logic16_receive_transfer, (void *)sdi, timeout);
731 if ((ret = libusb_submit_transfer(transfer)) != 0) {
732 sr_err("Failed to submit transfer: %s.",
733 libusb_error_name(ret));
734 libusb_free_transfer(transfer);
736 abort_acquisition(devc);
739 devc->transfers[i] = transfer;
740 devc->submitted_transfers++;
743 devc->ctx = drvc->sr_ctx;
745 usb_source_add(sdi->session, devc->ctx, timeout, receive_data, (void *)sdi);
747 std_session_send_df_header(sdi);
749 if ((ret = logic16_start_acquisition(sdi)) != SR_OK) {
750 abort_acquisition(devc);
757 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
761 ret = logic16_abort_acquisition(sdi);
763 abort_acquisition(sdi->priv);
768 static struct sr_dev_driver saleae_logic16_driver_info = {
769 .name = "saleae-logic16",
770 .longname = "Saleae Logic16",
773 .cleanup = std_cleanup,
775 .dev_list = std_dev_list,
776 .dev_clear = std_dev_clear,
777 .config_get = config_get,
778 .config_set = config_set,
779 .config_list = config_list,
780 .dev_open = dev_open,
781 .dev_close = dev_close,
782 .dev_acquisition_start = dev_acquisition_start,
783 .dev_acquisition_stop = dev_acquisition_stop,
786 SR_REGISTER_DEV_DRIVER(saleae_logic16_driver_info);