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 devopts[] = {
47 SR_CONF_LOGIC_ANALYZER,
49 SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
50 SR_CONF_CONN | SR_CONF_GET,
51 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
52 SR_CONF_VOLTAGE_THRESHOLD | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
53 SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
54 SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET,
57 static const int32_t soft_trigger_matches[] = {
65 static const char *channel_names[] = {
66 "0", "1", "2", "3", "4", "5", "6", "7", "8",
67 "9", "10", "11", "12", "13", "14", "15",
71 enum voltage_range range;
74 } volt_thresholds[] = {
75 { VOLTAGE_RANGE_18_33_V, 0.7, 1.4 },
76 { VOLTAGE_RANGE_5_V, 1.4, 3.6 },
79 static const uint64_t samplerates[] = {
98 static gboolean check_conf_profile(libusb_device *dev)
100 struct libusb_device_descriptor des;
101 struct libusb_device_handle *hdl;
103 unsigned char strdesc[64];
108 /* Assume the FW has not been loaded, unless proven wrong. */
109 libusb_get_device_descriptor(dev, &des);
111 if (libusb_open(dev, &hdl) != 0)
114 if (libusb_get_string_descriptor_ascii(hdl,
115 des.iManufacturer, strdesc, sizeof(strdesc)) < 0)
117 if (strcmp((const char *)strdesc, "Saleae LLC"))
120 if (libusb_get_string_descriptor_ascii(hdl,
121 des.iProduct, strdesc, sizeof(strdesc)) < 0)
123 if (strcmp((const char *)strdesc, "Logic S/16"))
126 /* If we made it here, it must be a configured Logic16. */
135 static GSList *scan(struct sr_dev_driver *di, GSList *options)
137 struct drv_context *drvc;
138 struct dev_context *devc;
139 struct sr_dev_inst *sdi;
140 struct sr_usb_dev_inst *usb;
141 struct sr_config *src;
142 GSList *l, *devices, *conn_devices;
143 struct libusb_device_descriptor des;
144 libusb_device **devlist;
147 char connection_id[64];
152 for (l = options; l; l = l->next) {
156 conn = g_variant_get_string(src->data, NULL);
161 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
165 /* Find all Logic16 devices and upload firmware to them. */
167 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
168 for (i = 0; devlist[i]; i++) {
171 for (l = conn_devices; l; l = l->next) {
173 if (usb->bus == libusb_get_bus_number(devlist[i])
174 && usb->address == libusb_get_device_address(devlist[i]))
178 /* This device matched none of the ones that
179 * matched the conn specification. */
183 libusb_get_device_descriptor(devlist[i], &des);
185 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
187 if (des.idVendor != LOGIC16_VID || des.idProduct != LOGIC16_PID)
190 sdi = g_malloc0(sizeof(struct sr_dev_inst));
191 sdi->status = SR_ST_INITIALIZING;
192 sdi->vendor = g_strdup("Saleae");
193 sdi->model = g_strdup("Logic16");
194 sdi->connection_id = g_strdup(connection_id);
196 for (j = 0; j < ARRAY_SIZE(channel_names); j++)
197 sr_channel_new(sdi, j, SR_CHANNEL_LOGIC, TRUE,
200 devc = g_malloc0(sizeof(struct dev_context));
201 devc->selected_voltage_range = VOLTAGE_RANGE_18_33_V;
203 devices = g_slist_append(devices, sdi);
205 if (check_conf_profile(devlist[i])) {
206 /* Already has the firmware, so fix the new address. */
207 sr_dbg("Found a Logic16 device.");
208 sdi->status = SR_ST_INACTIVE;
209 sdi->inst_type = SR_INST_USB;
210 sdi->conn = sr_usb_dev_inst_new(
211 libusb_get_bus_number(devlist[i]),
212 libusb_get_device_address(devlist[i]), NULL);
214 if (ezusb_upload_firmware(drvc->sr_ctx, devlist[i],
215 USB_CONFIGURATION, FX2_FIRMWARE) == SR_OK)
216 /* Store when this device's FW was updated. */
217 devc->fw_updated = g_get_monotonic_time();
219 sr_err("Firmware upload failed.");
220 sdi->inst_type = SR_INST_USB;
221 sdi->conn = sr_usb_dev_inst_new(
222 libusb_get_bus_number(devlist[i]), 0xff, NULL);
225 libusb_free_device_list(devlist, 1);
226 g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
228 return std_scan_complete(di, devices);
231 static int logic16_dev_open(struct sr_dev_inst *sdi)
233 struct sr_dev_driver *di;
234 libusb_device **devlist;
235 struct sr_usb_dev_inst *usb;
236 struct libusb_device_descriptor des;
237 struct drv_context *drvc;
238 int ret, i, device_count;
239 char connection_id[64];
245 if (sdi->status == SR_ST_ACTIVE)
246 /* Device is already in use. */
249 device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
250 if (device_count < 0) {
251 sr_err("Failed to get device list: %s.",
252 libusb_error_name(device_count));
256 for (i = 0; i < device_count; i++) {
257 libusb_get_device_descriptor(devlist[i], &des);
259 if (des.idVendor != LOGIC16_VID || des.idProduct != LOGIC16_PID)
262 if ((sdi->status == SR_ST_INITIALIZING) ||
263 (sdi->status == SR_ST_INACTIVE)) {
265 * Check device by its physical USB bus/port address.
267 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
268 if (strcmp(sdi->connection_id, connection_id))
269 /* This is not the one. */
273 if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
274 if (usb->address == 0xff)
276 * First time we touch this device after FW
277 * upload, so we don't know the address yet.
279 usb->address = libusb_get_device_address(devlist[i]);
281 sr_err("Failed to open device: %s.",
282 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.");
291 } else if (ret == LIBUSB_ERROR_NO_DEVICE) {
292 sr_err("Device has been disconnected.");
294 } else if (ret != 0) {
295 sr_err("Unable to claim interface: %s.",
296 libusb_error_name(ret));
300 if ((ret = logic16_init_device(sdi)) != SR_OK) {
301 sr_err("Failed to init device.");
305 sdi->status = SR_ST_ACTIVE;
306 sr_info("Opened device on %d.%d (logical) / %s (physical), interface %d.",
307 usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
311 libusb_free_device_list(devlist, 1);
313 if (sdi->status != SR_ST_ACTIVE) {
315 libusb_release_interface(usb->devhdl, USB_INTERFACE);
316 libusb_close(usb->devhdl);
325 static int dev_open(struct sr_dev_inst *sdi)
327 struct dev_context *devc;
329 int64_t timediff_us, timediff_ms;
334 * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS
335 * milliseconds for the FX2 to renumerate.
338 if (devc->fw_updated > 0) {
339 sr_info("Waiting for device to reset.");
340 /* Takes >= 300ms for the FX2 to be gone from the USB bus. */
341 g_usleep(300 * 1000);
343 while (timediff_ms < MAX_RENUM_DELAY_MS) {
344 if ((ret = logic16_dev_open(sdi)) == SR_OK)
346 g_usleep(100 * 1000);
348 timediff_us = g_get_monotonic_time() - devc->fw_updated;
349 timediff_ms = timediff_us / 1000;
350 sr_spew("Waited %" PRIi64 "ms.", timediff_ms);
353 sr_err("Device failed to renumerate.");
356 sr_info("Device came back after %" PRIi64 "ms.", timediff_ms);
358 sr_info("Firmware upload was not needed.");
359 ret = logic16_dev_open(sdi);
363 sr_err("Unable to open device.");
367 if (devc->cur_samplerate == 0) {
368 /* Samplerate hasn't been set; default to the slowest one. */
369 devc->cur_samplerate = samplerates[0];
375 static int dev_close(struct sr_dev_inst *sdi)
377 struct sr_usb_dev_inst *usb;
383 sr_info("Closing device on %d.%d (logical) / %s (physical) interface %d.",
384 usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
385 libusb_release_interface(usb->devhdl, USB_INTERFACE);
386 libusb_close(usb->devhdl);
388 sdi->status = SR_ST_INACTIVE;
393 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
394 const struct sr_channel_group *cg)
396 struct dev_context *devc;
397 struct sr_usb_dev_inst *usb;
408 if (!sdi || !sdi->conn)
411 if (usb->address == 255)
412 /* Device still needs to re-enumerate after firmware
413 * upload, so we don't know its (future) address. */
415 snprintf(str, 128, "%d.%d", usb->bus, usb->address);
416 *data = g_variant_new_string(str);
418 case SR_CONF_SAMPLERATE:
422 *data = g_variant_new_uint64(devc->cur_samplerate);
424 case SR_CONF_CAPTURE_RATIO:
428 *data = g_variant_new_uint64(devc->capture_ratio);
430 case SR_CONF_VOLTAGE_THRESHOLD:
435 for (i = 0; i < ARRAY_SIZE(volt_thresholds); i++) {
436 if (devc->selected_voltage_range !=
437 volt_thresholds[i].range)
439 range[0] = g_variant_new_double(volt_thresholds[i].low);
440 range[1] = g_variant_new_double(volt_thresholds[i].high);
441 *data = g_variant_new_tuple(range, 2);
453 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
454 const struct sr_channel_group *cg)
456 struct dev_context *devc;
467 case SR_CONF_SAMPLERATE:
468 devc->cur_samplerate = g_variant_get_uint64(data);
470 case SR_CONF_LIMIT_SAMPLES:
471 devc->limit_samples = g_variant_get_uint64(data);
473 case SR_CONF_CAPTURE_RATIO:
474 devc->capture_ratio = g_variant_get_uint64(data);
475 ret = (devc->capture_ratio > 100) ? SR_ERR : SR_OK;
477 case SR_CONF_VOLTAGE_THRESHOLD:
478 g_variant_get(data, "(dd)", &low, &high);
480 for (i = 0; i < ARRAY_SIZE(volt_thresholds); i++) {
481 if (fabs(volt_thresholds[i].low - low) < 0.1 &&
482 fabs(volt_thresholds[i].high - high) < 0.1) {
483 devc->selected_voltage_range =
484 volt_thresholds[i].range;
497 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
498 const struct sr_channel_group *cg)
500 GVariant *gvar, *range[2];
510 case SR_CONF_SCAN_OPTIONS:
511 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
512 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
514 case SR_CONF_DEVICE_OPTIONS:
515 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
516 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
518 case SR_CONF_SAMPLERATE:
519 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
520 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
521 samplerates, ARRAY_SIZE(samplerates), sizeof(uint64_t));
522 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
523 *data = g_variant_builder_end(&gvb);
525 case SR_CONF_VOLTAGE_THRESHOLD:
526 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
527 for (i = 0; i < ARRAY_SIZE(volt_thresholds); i++) {
528 range[0] = g_variant_new_double(volt_thresholds[i].low);
529 range[1] = g_variant_new_double(volt_thresholds[i].high);
530 gvar = g_variant_new_tuple(range, 2);
531 g_variant_builder_add_value(&gvb, gvar);
533 *data = g_variant_builder_end(&gvb);
535 case SR_CONF_TRIGGER_MATCH:
536 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
537 soft_trigger_matches, ARRAY_SIZE(soft_trigger_matches),
547 static void abort_acquisition(struct dev_context *devc)
551 devc->sent_samples = -1;
553 for (i = devc->num_transfers - 1; i >= 0; i--) {
554 if (devc->transfers[i])
555 libusb_cancel_transfer(devc->transfers[i]);
559 static unsigned int bytes_per_ms(struct dev_context *devc)
561 return devc->cur_samplerate * devc->num_channels / 8000;
564 static size_t get_buffer_size(struct dev_context *devc)
569 * The buffer should be large enough to hold 10ms of data and
572 s = 10 * bytes_per_ms(devc);
573 return (s + 511) & ~511;
576 static unsigned int get_number_of_transfers(struct dev_context *devc)
580 /* Total buffer size should be able to hold about 500ms of data. */
581 n = 500 * bytes_per_ms(devc) / get_buffer_size(devc);
583 if (n > NUM_SIMUL_TRANSFERS)
584 return NUM_SIMUL_TRANSFERS;
589 static unsigned int get_timeout(struct dev_context *devc)
592 unsigned int timeout;
594 total_size = get_buffer_size(devc) * get_number_of_transfers(devc);
595 timeout = total_size / bytes_per_ms(devc);
596 return timeout + timeout / 4; /* Leave a headroom of 25% percent. */
599 static int configure_channels(const struct sr_dev_inst *sdi)
601 struct dev_context *devc;
602 struct sr_channel *ch;
604 uint16_t channel_bit;
608 devc->cur_channels = 0;
609 devc->num_channels = 0;
610 for (l = sdi->channels; l; l = l->next) {
611 ch = (struct sr_channel *)l->data;
612 if (ch->enabled == FALSE)
615 channel_bit = 1 << (ch->index);
617 devc->cur_channels |= channel_bit;
619 #ifdef WORDS_BIGENDIAN
621 * Output logic data should be stored in little endian format.
622 * To speed things up during conversion, do the switcharoo
625 channel_bit = 1 << (ch->index ^ 8);
628 devc->channel_masks[devc->num_channels++] = channel_bit;
634 static int receive_data(int fd, int revents, void *cb_data)
637 struct dev_context *devc;
638 struct drv_context *drvc;
639 const struct sr_dev_inst *sdi;
640 struct sr_dev_driver *di;
650 tv.tv_sec = tv.tv_usec = 0;
651 libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
653 if (devc->sent_samples == -2) {
654 logic16_abort_acquisition(sdi);
655 abort_acquisition(devc);
661 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
663 struct sr_dev_driver *di = sdi->driver;
664 struct dev_context *devc;
665 struct drv_context *drvc;
666 struct sr_usb_dev_inst *usb;
667 struct sr_trigger *trigger;
668 struct libusb_transfer *transfer;
669 unsigned int i, timeout, num_transfers;
672 size_t size, convsize;
678 /* Configures devc->cur_channels. */
679 if (configure_channels(sdi) != SR_OK) {
680 sr_err("Failed to configure channels.");
684 devc->sent_samples = 0;
685 devc->empty_transfer_count = 0;
686 devc->cur_channel = 0;
687 memset(devc->channel_data, 0, sizeof(devc->channel_data));
689 if ((trigger = sr_session_trigger_get(sdi->session))) {
690 int pre_trigger_samples = 0;
691 if (devc->limit_samples > 0)
692 pre_trigger_samples = devc->capture_ratio * devc->limit_samples/100;
693 devc->stl = soft_trigger_logic_new(sdi, trigger, pre_trigger_samples);
695 return SR_ERR_MALLOC;
696 devc->trigger_fired = FALSE;
698 devc->trigger_fired = TRUE;
700 timeout = get_timeout(devc);
701 num_transfers = get_number_of_transfers(devc);
702 size = get_buffer_size(devc);
703 convsize = (size / devc->num_channels + 2) * 16;
704 devc->submitted_transfers = 0;
706 devc->convbuffer_size = convsize;
707 if (!(devc->convbuffer = g_try_malloc(convsize))) {
708 sr_err("Conversion buffer malloc failed.");
709 return SR_ERR_MALLOC;
712 devc->transfers = g_try_malloc0(sizeof(*devc->transfers) * num_transfers);
713 if (!devc->transfers) {
714 sr_err("USB transfers malloc failed.");
715 g_free(devc->convbuffer);
716 return SR_ERR_MALLOC;
719 if ((ret = logic16_setup_acquisition(sdi, devc->cur_samplerate,
720 devc->cur_channels)) != SR_OK) {
721 g_free(devc->transfers);
722 g_free(devc->convbuffer);
726 devc->num_transfers = num_transfers;
727 for (i = 0; i < num_transfers; i++) {
728 if (!(buf = g_try_malloc(size))) {
729 sr_err("USB transfer buffer malloc failed.");
730 if (devc->submitted_transfers)
731 abort_acquisition(devc);
733 g_free(devc->transfers);
734 g_free(devc->convbuffer);
736 return SR_ERR_MALLOC;
738 transfer = libusb_alloc_transfer(0);
739 libusb_fill_bulk_transfer(transfer, usb->devhdl,
740 2 | LIBUSB_ENDPOINT_IN, buf, size,
741 logic16_receive_transfer, (void *)sdi, timeout);
742 if ((ret = libusb_submit_transfer(transfer)) != 0) {
743 sr_err("Failed to submit transfer: %s.",
744 libusb_error_name(ret));
745 libusb_free_transfer(transfer);
747 abort_acquisition(devc);
750 devc->transfers[i] = transfer;
751 devc->submitted_transfers++;
754 devc->ctx = drvc->sr_ctx;
756 usb_source_add(sdi->session, devc->ctx, timeout, receive_data, (void *)sdi);
758 std_session_send_df_header(sdi);
760 if ((ret = logic16_start_acquisition(sdi)) != SR_OK) {
761 abort_acquisition(devc);
768 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
772 ret = logic16_abort_acquisition(sdi);
774 abort_acquisition(sdi->priv);
779 static struct sr_dev_driver saleae_logic16_driver_info = {
780 .name = "saleae-logic16",
781 .longname = "Saleae Logic16",
784 .cleanup = std_cleanup,
786 .dev_list = std_dev_list,
788 .config_get = config_get,
789 .config_set = config_set,
790 .config_list = config_list,
791 .dev_open = dev_open,
792 .dev_close = dev_close,
793 .dev_acquisition_start = dev_acquisition_start,
794 .dev_acquisition_stop = dev_acquisition_stop,
797 SR_REGISTER_DEV_DRIVER(saleae_logic16_driver_info);