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 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;
75 } volt_thresholds_ranges[] = {
76 { VOLTAGE_RANGE_18_33_V, },
77 { VOLTAGE_RANGE_5_V, },
80 static const struct voltage_threshold volt_thresholds[] = {
85 static const uint64_t samplerates[] = {
104 static gboolean check_conf_profile(libusb_device *dev)
106 struct libusb_device_descriptor des;
107 struct libusb_device_handle *hdl;
109 unsigned char strdesc[64];
114 /* Assume the FW has not been loaded, unless proven wrong. */
115 libusb_get_device_descriptor(dev, &des);
117 if (libusb_open(dev, &hdl) != 0)
120 if (libusb_get_string_descriptor_ascii(hdl,
121 des.iManufacturer, strdesc, sizeof(strdesc)) < 0)
123 if (strcmp((const char *)strdesc, "Saleae LLC"))
126 if (libusb_get_string_descriptor_ascii(hdl,
127 des.iProduct, strdesc, sizeof(strdesc)) < 0)
129 if (strcmp((const char *)strdesc, "Logic S/16"))
132 /* If we made it here, it must be a configured Logic16. */
141 static GSList *scan(struct sr_dev_driver *di, GSList *options)
143 struct drv_context *drvc;
144 struct dev_context *devc;
145 struct sr_dev_inst *sdi;
146 struct sr_usb_dev_inst *usb;
147 struct sr_config *src;
148 GSList *l, *devices, *conn_devices;
149 struct libusb_device_descriptor des;
150 libusb_device **devlist;
153 char connection_id[64];
158 for (l = options; l; l = l->next) {
162 conn = g_variant_get_string(src->data, NULL);
167 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
171 /* Find all Logic16 devices and upload firmware to them. */
173 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
174 for (i = 0; devlist[i]; i++) {
177 for (l = conn_devices; l; l = l->next) {
179 if (usb->bus == libusb_get_bus_number(devlist[i])
180 && usb->address == libusb_get_device_address(devlist[i]))
184 /* This device matched none of the ones that
185 * matched the conn specification. */
189 libusb_get_device_descriptor(devlist[i], &des);
191 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
193 if (des.idVendor != LOGIC16_VID || des.idProduct != LOGIC16_PID)
196 sdi = g_malloc0(sizeof(struct sr_dev_inst));
197 sdi->status = SR_ST_INITIALIZING;
198 sdi->vendor = g_strdup("Saleae");
199 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 devices = g_slist_append(devices, sdi);
211 if (check_conf_profile(devlist[i])) {
212 /* Already has the firmware, so fix the new address. */
213 sr_dbg("Found a Logic16 device.");
214 sdi->status = SR_ST_INACTIVE;
215 sdi->inst_type = SR_INST_USB;
216 sdi->conn = sr_usb_dev_inst_new(
217 libusb_get_bus_number(devlist[i]),
218 libusb_get_device_address(devlist[i]), NULL);
220 if (ezusb_upload_firmware(drvc->sr_ctx, devlist[i],
221 USB_CONFIGURATION, FX2_FIRMWARE) == SR_OK)
222 /* Store when this device's FW was updated. */
223 devc->fw_updated = g_get_monotonic_time();
225 sr_err("Firmware upload failed.");
226 sdi->inst_type = SR_INST_USB;
227 sdi->conn = sr_usb_dev_inst_new(
228 libusb_get_bus_number(devlist[i]), 0xff, NULL);
231 libusb_free_device_list(devlist, 1);
232 g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
234 return std_scan_complete(di, devices);
237 static int logic16_dev_open(struct sr_dev_inst *sdi)
239 struct sr_dev_driver *di;
240 libusb_device **devlist;
241 struct sr_usb_dev_inst *usb;
242 struct libusb_device_descriptor des;
243 struct drv_context *drvc;
244 int ret = SR_ERR, i, device_count;
245 char connection_id[64];
251 device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
252 if (device_count < 0) {
253 sr_err("Failed to get device list: %s.",
254 libusb_error_name(device_count));
258 for (i = 0; i < device_count; i++) {
259 libusb_get_device_descriptor(devlist[i], &des);
261 if (des.idVendor != LOGIC16_VID || des.idProduct != LOGIC16_PID)
264 if ((sdi->status == SR_ST_INITIALIZING) ||
265 (sdi->status == SR_ST_INACTIVE)) {
267 * Check device by its physical USB bus/port address.
269 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
270 if (strcmp(sdi->connection_id, connection_id))
271 /* This is not the one. */
275 if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
276 if (usb->address == 0xff)
278 * First time we touch this device after FW
279 * upload, so we don't know the address yet.
281 usb->address = libusb_get_device_address(devlist[i]);
283 sr_err("Failed to open device: %s.",
284 libusb_error_name(ret));
289 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
290 if (ret == LIBUSB_ERROR_BUSY) {
291 sr_err("Unable to claim USB interface. Another "
292 "program or driver has already claimed it.");
295 } else if (ret == LIBUSB_ERROR_NO_DEVICE) {
296 sr_err("Device has been disconnected.");
299 } else if (ret != 0) {
300 sr_err("Unable to claim interface: %s.",
301 libusb_error_name(ret));
306 if ((ret = logic16_init_device(sdi)) != SR_OK) {
307 sr_err("Failed to init device.");
311 sr_info("Opened device on %d.%d (logical) / %s (physical), interface %d.",
312 usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
319 libusb_free_device_list(devlist, 1);
323 libusb_release_interface(usb->devhdl, USB_INTERFACE);
324 libusb_close(usb->devhdl);
333 static int dev_open(struct sr_dev_inst *sdi)
335 struct dev_context *devc;
337 int64_t timediff_us, timediff_ms;
342 * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS
343 * milliseconds for the FX2 to renumerate.
346 if (devc->fw_updated > 0) {
347 sr_info("Waiting for device to reset.");
348 /* Takes >= 300ms for the FX2 to be gone from the USB bus. */
349 g_usleep(300 * 1000);
351 while (timediff_ms < MAX_RENUM_DELAY_MS) {
352 if ((ret = logic16_dev_open(sdi)) == SR_OK)
354 g_usleep(100 * 1000);
356 timediff_us = g_get_monotonic_time() - devc->fw_updated;
357 timediff_ms = timediff_us / 1000;
358 sr_spew("Waited %" PRIi64 "ms.", timediff_ms);
361 sr_err("Device failed to renumerate.");
364 sr_info("Device came back after %" PRIi64 "ms.", timediff_ms);
366 sr_info("Firmware upload was not needed.");
367 ret = logic16_dev_open(sdi);
371 sr_err("Unable to open device.");
375 if (devc->cur_samplerate == 0) {
376 /* Samplerate hasn't been set; default to the slowest one. */
377 devc->cur_samplerate = samplerates[0];
383 static int dev_close(struct sr_dev_inst *sdi)
385 struct sr_usb_dev_inst *usb;
392 sr_info("Closing device on %d.%d (logical) / %s (physical) interface %d.",
393 usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
394 libusb_release_interface(usb->devhdl, USB_INTERFACE);
395 libusb_close(usb->devhdl);
401 static int config_get(uint32_t key, GVariant **data,
402 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
404 struct dev_context *devc;
405 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_ranges[i].range)
446 *data = std_gvar_tuple_double(volt_thresholds[i].low, volt_thresholds[i].high);
458 static int config_set(uint32_t key, GVariant *data,
459 const struct sr_dev_inst *sdi, 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_ranges[i].range;
502 static int config_list(uint32_t key, GVariant **data,
503 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
506 case SR_CONF_SCAN_OPTIONS:
507 case SR_CONF_DEVICE_OPTIONS:
508 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
509 case SR_CONF_SAMPLERATE:
510 *data = std_gvar_samplerates(ARRAY_AND_SIZE(samplerates));
512 case SR_CONF_VOLTAGE_THRESHOLD:
513 *data = std_gvar_thresholds(ARRAY_AND_SIZE(volt_thresholds));
515 case SR_CONF_TRIGGER_MATCH:
516 *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
525 static void abort_acquisition(struct dev_context *devc)
529 devc->sent_samples = -1;
531 for (i = devc->num_transfers - 1; i >= 0; i--) {
532 if (devc->transfers[i])
533 libusb_cancel_transfer(devc->transfers[i]);
537 static unsigned int bytes_per_ms(struct dev_context *devc)
539 return devc->cur_samplerate * devc->num_channels / 8000;
542 static size_t get_buffer_size(struct dev_context *devc)
547 * The buffer should be large enough to hold 10ms of data and
550 s = 10 * bytes_per_ms(devc);
551 return (s + 511) & ~511;
554 static unsigned int get_number_of_transfers(struct dev_context *devc)
558 /* Total buffer size should be able to hold about 500ms of data. */
559 n = 500 * bytes_per_ms(devc) / get_buffer_size(devc);
561 if (n > NUM_SIMUL_TRANSFERS)
562 return NUM_SIMUL_TRANSFERS;
567 static unsigned int get_timeout(struct dev_context *devc)
570 unsigned int timeout;
572 total_size = get_buffer_size(devc) * get_number_of_transfers(devc);
573 timeout = total_size / bytes_per_ms(devc);
574 return timeout + timeout / 4; /* Leave a headroom of 25% percent. */
577 static int configure_channels(const struct sr_dev_inst *sdi)
579 struct dev_context *devc;
580 struct sr_channel *ch;
582 uint16_t channel_bit;
586 devc->cur_channels = 0;
587 devc->num_channels = 0;
588 for (l = sdi->channels; l; l = l->next) {
589 ch = (struct sr_channel *)l->data;
590 if (ch->enabled == FALSE)
593 channel_bit = 1 << (ch->index);
595 devc->cur_channels |= channel_bit;
597 #ifdef WORDS_BIGENDIAN
599 * Output logic data should be stored in little endian format.
600 * To speed things up during conversion, do the switcharoo
603 channel_bit = 1 << (ch->index ^ 8);
606 devc->channel_masks[devc->num_channels++] = channel_bit;
612 static int receive_data(int fd, int revents, void *cb_data)
615 struct dev_context *devc;
616 struct drv_context *drvc;
617 const struct sr_dev_inst *sdi;
618 struct sr_dev_driver *di;
628 tv.tv_sec = tv.tv_usec = 0;
629 libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
631 if (devc->sent_samples == -2) {
632 logic16_abort_acquisition(sdi);
633 abort_acquisition(devc);
639 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
641 struct sr_dev_driver *di = sdi->driver;
642 struct dev_context *devc;
643 struct drv_context *drvc;
644 struct sr_usb_dev_inst *usb;
645 struct sr_trigger *trigger;
646 struct libusb_transfer *transfer;
647 unsigned int i, timeout, num_transfers;
650 size_t size, convsize;
656 /* Configures devc->cur_channels. */
657 if (configure_channels(sdi) != SR_OK) {
658 sr_err("Failed to configure channels.");
662 devc->sent_samples = 0;
663 devc->empty_transfer_count = 0;
664 devc->cur_channel = 0;
665 memset(devc->channel_data, 0, sizeof(devc->channel_data));
667 if ((trigger = sr_session_trigger_get(sdi->session))) {
668 int pre_trigger_samples = 0;
669 if (devc->limit_samples > 0)
670 pre_trigger_samples = devc->capture_ratio * devc->limit_samples/100;
671 devc->stl = soft_trigger_logic_new(sdi, trigger, pre_trigger_samples);
673 return SR_ERR_MALLOC;
674 devc->trigger_fired = FALSE;
676 devc->trigger_fired = TRUE;
678 timeout = get_timeout(devc);
679 num_transfers = get_number_of_transfers(devc);
680 size = get_buffer_size(devc);
681 convsize = (size / devc->num_channels + 2) * 16;
682 devc->submitted_transfers = 0;
684 devc->convbuffer_size = convsize;
685 if (!(devc->convbuffer = g_try_malloc(convsize))) {
686 sr_err("Conversion buffer malloc failed.");
687 return SR_ERR_MALLOC;
690 devc->transfers = g_try_malloc0(sizeof(*devc->transfers) * num_transfers);
691 if (!devc->transfers) {
692 sr_err("USB transfers malloc failed.");
693 g_free(devc->convbuffer);
694 return SR_ERR_MALLOC;
697 if ((ret = logic16_setup_acquisition(sdi, devc->cur_samplerate,
698 devc->cur_channels)) != SR_OK) {
699 g_free(devc->transfers);
700 g_free(devc->convbuffer);
704 devc->num_transfers = num_transfers;
705 for (i = 0; i < num_transfers; i++) {
706 if (!(buf = g_try_malloc(size))) {
707 sr_err("USB transfer buffer malloc failed.");
708 if (devc->submitted_transfers)
709 abort_acquisition(devc);
711 g_free(devc->transfers);
712 g_free(devc->convbuffer);
714 return SR_ERR_MALLOC;
716 transfer = libusb_alloc_transfer(0);
717 libusb_fill_bulk_transfer(transfer, usb->devhdl,
718 2 | LIBUSB_ENDPOINT_IN, buf, size,
719 logic16_receive_transfer, (void *)sdi, timeout);
720 if ((ret = libusb_submit_transfer(transfer)) != 0) {
721 sr_err("Failed to submit transfer: %s.",
722 libusb_error_name(ret));
723 libusb_free_transfer(transfer);
725 abort_acquisition(devc);
728 devc->transfers[i] = transfer;
729 devc->submitted_transfers++;
732 devc->ctx = drvc->sr_ctx;
734 usb_source_add(sdi->session, devc->ctx, timeout, receive_data, (void *)sdi);
736 std_session_send_df_header(sdi);
738 if ((ret = logic16_start_acquisition(sdi)) != SR_OK) {
739 abort_acquisition(devc);
746 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
750 ret = logic16_abort_acquisition(sdi);
752 abort_acquisition(sdi->priv);
757 static struct sr_dev_driver saleae_logic16_driver_info = {
758 .name = "saleae-logic16",
759 .longname = "Saleae Logic16",
762 .cleanup = std_cleanup,
764 .dev_list = std_dev_list,
765 .dev_clear = std_dev_clear,
766 .config_get = config_get,
767 .config_set = config_set,
768 .config_list = config_list,
769 .dev_open = dev_open,
770 .dev_close = dev_close,
771 .dev_acquisition_start = dev_acquisition_start,
772 .dev_acquisition_stop = dev_acquisition_stop,
775 SR_REGISTER_DEV_DRIVER(saleae_logic16_driver_info);