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/>.
27 #include "libsigrok.h"
28 #include "libsigrok-internal.h"
31 #define LOGIC16_VID 0x21a9
32 #define LOGIC16_PID 0x1001
34 #define USB_INTERFACE 0
35 #define USB_CONFIGURATION 1
36 #define FX2_FIRMWARE FIRMWARE_DIR "/saleae-logic16-fx2.fw"
38 #define MAX_RENUM_DELAY_MS 3000
39 #define NUM_SIMUL_TRANSFERS 32
41 SR_PRIV struct sr_dev_driver saleae_logic16_driver_info;
42 static struct sr_dev_driver *di = &saleae_logic16_driver_info;
44 static const int32_t hwopts[] = {
48 static const int32_t hwcaps[] = {
49 SR_CONF_LOGIC_ANALYZER,
51 SR_CONF_VOLTAGE_THRESHOLD,
53 /* These are really implemented in the driver, not the hardware. */
54 SR_CONF_LIMIT_SAMPLES,
58 static const char *probe_names[] = {
59 "0", "1", "2", "3", "4", "5", "6", "7", "8",
60 "9", "10", "11", "12", "13", "14", "15",
65 enum voltage_range range;
68 } volt_thresholds[] = {
69 { VOLTAGE_RANGE_18_33_V, 0.7, 1.4 },
70 { VOLTAGE_RANGE_5_V, 1.4, 3.6 },
73 static const uint64_t samplerates[] = {
90 static int init(struct sr_context *sr_ctx)
92 return std_init(sr_ctx, di, LOG_PREFIX);
95 static gboolean check_conf_profile(libusb_device *dev)
97 struct libusb_device_descriptor des;
98 struct libusb_device_handle *hdl;
100 unsigned char strdesc[64];
105 /* Assume the FW has not been loaded, unless proven wrong. */
106 if (libusb_get_device_descriptor(dev, &des) != 0)
109 if (libusb_open(dev, &hdl) != 0)
112 if (libusb_get_string_descriptor_ascii(hdl,
113 des.iManufacturer, strdesc, sizeof(strdesc)) < 0)
115 if (strcmp((const char *)strdesc, "Saleae LLC"))
118 if (libusb_get_string_descriptor_ascii(hdl,
119 des.iProduct, strdesc, sizeof(strdesc)) < 0)
121 if (strcmp((const char *)strdesc, "Logic S/16"))
124 /* If we made it here, it must be a configured Logic16. */
133 static GSList *scan(GSList *options)
135 struct drv_context *drvc;
136 struct dev_context *devc;
137 struct sr_dev_inst *sdi;
138 struct sr_usb_dev_inst *usb;
139 struct sr_probe *probe;
140 struct sr_config *src;
141 GSList *l, *devices, *conn_devices;
142 struct libusb_device_descriptor des;
143 libusb_device **devlist;
144 int devcnt, ret, i, j;
150 for (l = options; l; l = l->next) {
154 conn = g_variant_get_string(src->data, NULL);
159 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
163 /* Find all Logic16 devices and upload firmware to them. */
165 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
166 for (i = 0; devlist[i]; i++) {
169 for (l = conn_devices; l; l = l->next) {
171 if (usb->bus == libusb_get_bus_number(devlist[i])
172 && usb->address == libusb_get_device_address(devlist[i]))
176 /* This device matched none of the ones that
177 * matched the conn specification. */
181 if ((ret = libusb_get_device_descriptor(devlist[i], &des)) != 0) {
182 sr_warn("Failed to get device descriptor: %s.",
183 libusb_error_name(ret));
187 if (des.idVendor != LOGIC16_VID || des.idProduct != LOGIC16_PID)
190 devcnt = g_slist_length(drvc->instances);
191 sdi = sr_dev_inst_new(devcnt, SR_ST_INITIALIZING,
192 "Saleae", "Logic16", NULL);
197 for (j = 0; probe_names[j]; j++) {
198 if (!(probe = sr_probe_new(j, SR_PROBE_LOGIC, TRUE,
201 sdi->probes = g_slist_append(sdi->probes, probe);
204 if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
206 devc->selected_voltage_range = VOLTAGE_RANGE_18_33_V;
208 drvc->instances = g_slist_append(drvc->instances, sdi);
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(devlist[i], USB_CONFIGURATION,
221 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 for "
226 "device %d.", devcnt);
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 GSList *dev_list(void)
240 return ((struct drv_context *)(di->priv))->instances;
243 static int dev_clear(void)
245 return std_dev_clear(di, NULL);
248 static int logic16_dev_open(struct sr_dev_inst *sdi)
250 libusb_device **devlist;
251 struct sr_usb_dev_inst *usb;
252 struct libusb_device_descriptor des;
253 struct drv_context *drvc;
254 int ret, skip, i, device_count;
259 if (sdi->status == SR_ST_ACTIVE)
260 /* Device is already in use. */
264 device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
265 if (device_count < 0) {
266 sr_err("Failed to get device list: %s.",
267 libusb_error_name(device_count));
271 for (i = 0; i < device_count; i++) {
272 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
273 sr_err("Failed to get device descriptor: %s.",
274 libusb_error_name(ret));
278 if (des.idVendor != LOGIC16_VID || des.idProduct != LOGIC16_PID)
281 if (sdi->status == SR_ST_INITIALIZING) {
282 if (skip != sdi->index) {
283 /* Skip devices of this type that aren't the one we want. */
287 } else if (sdi->status == SR_ST_INACTIVE) {
289 * This device is fully enumerated, so we need to find
290 * this device by vendor, product, bus and address.
292 if (libusb_get_bus_number(devlist[i]) != usb->bus
293 || libusb_get_device_address(devlist[i]) != usb->address)
294 /* This is not the one. */
298 if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
299 if (usb->address == 0xff)
301 * First time we touch this device after FW
302 * upload, so we don't know the address yet.
304 usb->address = libusb_get_device_address(devlist[i]);
306 sr_err("Failed to open device: %s.",
307 libusb_error_name(ret));
311 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
312 if (ret == LIBUSB_ERROR_BUSY) {
313 sr_err("Unable to claim USB interface. Another "
314 "program or driver has already claimed it.");
316 } else if (ret == LIBUSB_ERROR_NO_DEVICE) {
317 sr_err("Device has been disconnected.");
319 } else if (ret != 0) {
320 sr_err("Unable to claim interface: %s.",
321 libusb_error_name(ret));
325 if ((ret = logic16_init_device(sdi)) != SR_OK) {
326 sr_err("Failed to init device.");
330 sdi->status = SR_ST_ACTIVE;
331 sr_info("Opened device %d on %d.%d, interface %d.",
332 sdi->index, usb->bus, usb->address, USB_INTERFACE);
336 libusb_free_device_list(devlist, 1);
338 if (sdi->status != SR_ST_ACTIVE) {
340 libusb_release_interface(usb->devhdl, USB_INTERFACE);
341 libusb_close(usb->devhdl);
350 static int dev_open(struct sr_dev_inst *sdi)
352 struct dev_context *devc;
354 int64_t timediff_us, timediff_ms;
359 * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS
360 * milliseconds for the FX2 to renumerate.
363 if (devc->fw_updated > 0) {
364 sr_info("Waiting for device to reset.");
365 /* Takes >= 300ms for the FX2 to be gone from the USB bus. */
366 g_usleep(300 * 1000);
368 while (timediff_ms < MAX_RENUM_DELAY_MS) {
369 if ((ret = logic16_dev_open(sdi)) == SR_OK)
371 g_usleep(100 * 1000);
373 timediff_us = g_get_monotonic_time() - devc->fw_updated;
374 timediff_ms = timediff_us / 1000;
375 sr_spew("Waited %" PRIi64 "ms.", timediff_ms);
378 sr_err("Device failed to renumerate.");
381 sr_info("Device came back after %" PRIi64 "ms.", timediff_ms);
383 sr_info("Firmware upload was not needed.");
384 ret = logic16_dev_open(sdi);
388 sr_err("Unable to open device.");
392 if (devc->cur_samplerate == 0) {
393 /* Samplerate hasn't been set; default to the slowest one. */
394 devc->cur_samplerate = samplerates[0];
400 static int dev_close(struct sr_dev_inst *sdi)
402 struct sr_usb_dev_inst *usb;
405 if (usb->devhdl == NULL)
408 sr_info("Closing device %d on %d.%d interface %d.",
409 sdi->index, usb->bus, usb->address, USB_INTERFACE);
410 libusb_release_interface(usb->devhdl, USB_INTERFACE);
411 libusb_close(usb->devhdl);
413 sdi->status = SR_ST_INACTIVE;
418 static int cleanup(void)
421 struct drv_context *drvc;
423 if (!(drvc = di->priv))
424 /* Can get called on an unused driver, doesn't matter. */
434 static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi)
436 struct dev_context *devc;
437 struct sr_usb_dev_inst *usb;
446 if (!sdi || !sdi->conn)
449 if (usb->address == 255)
450 /* Device still needs to re-enumerate after firmware
451 * upload, so we don't know its (future) address. */
453 snprintf(str, 128, "%d.%d", usb->bus, usb->address);
454 *data = g_variant_new_string(str);
456 case SR_CONF_SAMPLERATE:
460 *data = g_variant_new_uint64(devc->cur_samplerate);
462 case SR_CONF_VOLTAGE_THRESHOLD:
467 for (i = 0; i < ARRAY_SIZE(volt_thresholds); i++) {
468 if (devc->selected_voltage_range !=
469 volt_thresholds[i].range)
471 range[0] = g_variant_new_double(volt_thresholds[i].low);
472 range[1] = g_variant_new_double(volt_thresholds[i].high);
473 *data = g_variant_new_tuple(range, 2);
485 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi)
487 struct dev_context *devc;
492 if (sdi->status != SR_ST_ACTIVE)
493 return SR_ERR_DEV_CLOSED;
499 case SR_CONF_SAMPLERATE:
500 devc->cur_samplerate = g_variant_get_uint64(data);
502 case SR_CONF_LIMIT_SAMPLES:
503 devc->limit_samples = g_variant_get_uint64(data);
505 case SR_CONF_VOLTAGE_THRESHOLD:
506 g_variant_get(data, "(dd)", &low, &high);
508 for (i = 0; i < ARRAY_SIZE(volt_thresholds); i++) {
509 if (fabs(volt_thresholds[i].low - low) < 0.1 &&
510 fabs(volt_thresholds[i].high - high) < 0.1) {
511 devc->selected_voltage_range =
512 volt_thresholds[i].range;
525 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
527 GVariant *gvar, *range[2];
536 case SR_CONF_SCAN_OPTIONS:
537 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
538 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
540 case SR_CONF_DEVICE_OPTIONS:
541 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
542 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
544 case SR_CONF_SAMPLERATE:
545 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
546 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
547 samplerates, ARRAY_SIZE(samplerates), sizeof(uint64_t));
548 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
549 *data = g_variant_builder_end(&gvb);
551 case SR_CONF_VOLTAGE_THRESHOLD:
552 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
553 for (i = 0; i < ARRAY_SIZE(volt_thresholds); i++) {
554 range[0] = g_variant_new_double(volt_thresholds[i].low);
555 range[1] = g_variant_new_double(volt_thresholds[i].high);
556 gvar = g_variant_new_tuple(range, 2);
557 g_variant_builder_add_value(&gvb, gvar);
559 *data = g_variant_builder_end(&gvb);
568 static void abort_acquisition(struct dev_context *devc)
572 devc->num_samples = -1;
574 for (i = devc->num_transfers - 1; i >= 0; i--) {
575 if (devc->transfers[i])
576 libusb_cancel_transfer(devc->transfers[i]);
580 static unsigned int bytes_per_ms(struct dev_context *devc)
582 return devc->cur_samplerate * devc->num_channels / 8000;
585 static size_t get_buffer_size(struct dev_context *devc)
590 * The buffer should be large enough to hold 10ms of data and
593 s = 10 * bytes_per_ms(devc);
594 return (s + 511) & ~511;
597 static unsigned int get_number_of_transfers(struct dev_context *devc)
601 /* Total buffer size should be able to hold about 500ms of data. */
602 n = 500 * bytes_per_ms(devc) / get_buffer_size(devc);
604 if (n > NUM_SIMUL_TRANSFERS)
605 return NUM_SIMUL_TRANSFERS;
610 static unsigned int get_timeout(struct dev_context *devc)
613 unsigned int timeout;
615 total_size = get_buffer_size(devc) * get_number_of_transfers(devc);
616 timeout = total_size / bytes_per_ms(devc);
617 return timeout + timeout / 4; /* Leave a headroom of 25% percent. */
620 static int configure_probes(const struct sr_dev_inst *sdi)
622 struct dev_context *devc;
623 struct sr_probe *probe;
629 devc->cur_channels = 0;
630 devc->num_channels = 0;
631 for (l = sdi->probes; l; l = l->next) {
632 probe = (struct sr_probe *)l->data;
633 if (probe->enabled == FALSE)
636 probe_bit = 1 << (probe->index);
638 devc->cur_channels |= probe_bit;
640 #ifdef WORDS_BIGENDIAN
642 * Output logic data should be stored in little endian format.
643 * To speed things up during conversion, do the switcharoo
646 probe_bit = 1 << (probe->index ^ 8);
649 devc->channel_masks[devc->num_channels++] = probe_bit;
655 static int receive_data(int fd, int revents, void *cb_data)
658 struct dev_context *devc;
659 struct drv_context *drvc;
660 const struct sr_dev_inst *sdi;
669 tv.tv_sec = tv.tv_usec = 0;
670 libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
672 if (devc->num_samples == -2) {
673 logic16_abort_acquisition(sdi);
674 abort_acquisition(devc);
680 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
682 struct dev_context *devc;
683 struct drv_context *drvc;
684 struct sr_usb_dev_inst *usb;
685 struct libusb_transfer *transfer;
686 const struct libusb_pollfd **lupfd;
687 unsigned int i, timeout, num_transfers;
690 size_t size, convsize;
692 if (sdi->status != SR_ST_ACTIVE)
693 return SR_ERR_DEV_CLOSED;
699 /* Configures devc->cur_channels. */
700 if (configure_probes(sdi) != SR_OK) {
701 sr_err("Failed to configure probes.");
705 devc->cb_data = cb_data;
706 devc->num_samples = 0;
707 devc->empty_transfer_count = 0;
708 devc->cur_channel = 0;
709 memset(devc->channel_data, 0, sizeof(devc->channel_data));
711 timeout = get_timeout(devc);
712 num_transfers = get_number_of_transfers(devc);
713 size = get_buffer_size(devc);
714 convsize = (size / devc->num_channels + 2) * 16;
715 devc->submitted_transfers = 0;
718 devc->convbuffer_size = convsize;
719 if (!(devc->convbuffer = g_try_malloc(convsize))) {
720 sr_err("Conversion buffer malloc failed.");
721 return SR_ERR_MALLOC;
724 devc->transfers = g_try_malloc0(sizeof(*devc->transfers) * num_transfers);
725 if (!devc->transfers) {
726 sr_err("USB transfers malloc failed.");
727 g_free(devc->convbuffer);
728 return SR_ERR_MALLOC;
731 if ((ret = logic16_setup_acquisition(sdi, devc->cur_samplerate,
732 devc->cur_channels)) != SR_OK) {
733 g_free(devc->transfers);
734 g_free(devc->convbuffer);
738 devc->num_transfers = num_transfers;
739 for (i = 0; i < num_transfers; i++) {
740 if (!(buf = g_try_malloc(size))) {
741 sr_err("USB transfer buffer malloc failed.");
742 if (devc->submitted_transfers)
743 abort_acquisition(devc);
745 g_free(devc->transfers);
746 g_free(devc->convbuffer);
748 return SR_ERR_MALLOC;
750 transfer = libusb_alloc_transfer(0);
751 libusb_fill_bulk_transfer(transfer, usb->devhdl,
752 2 | LIBUSB_ENDPOINT_IN, buf, size,
753 logic16_receive_transfer, devc, timeout);
754 if ((ret = libusb_submit_transfer(transfer)) != 0) {
755 sr_err("Failed to submit transfer: %s.",
756 libusb_error_name(ret));
757 libusb_free_transfer(transfer);
759 abort_acquisition(devc);
762 devc->transfers[i] = transfer;
763 devc->submitted_transfers++;
766 lupfd = libusb_get_pollfds(drvc->sr_ctx->libusb_ctx);
767 for (i = 0; lupfd[i]; i++);
768 devc->usbfd = g_try_malloc(sizeof(struct libusb_pollfd) * (i + 1));
770 abort_acquisition(devc);
774 for (i = 0; lupfd[i]; i++) {
775 sr_source_add(lupfd[i]->fd, lupfd[i]->events,
776 timeout, receive_data, (void *)sdi);
777 devc->usbfd[i] = lupfd[i]->fd;
782 /* Send header packet to the session bus. */
783 std_session_send_df_header(cb_data, LOG_PREFIX);
785 if ((ret = logic16_start_acquisition(sdi)) != SR_OK) {
786 abort_acquisition(devc);
793 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
799 if (sdi->status != SR_ST_ACTIVE)
800 return SR_ERR_DEV_CLOSED;
802 ret = logic16_abort_acquisition(sdi);
804 abort_acquisition(sdi->priv);
809 SR_PRIV struct sr_dev_driver saleae_logic16_driver_info = {
810 .name = "saleae-logic16",
811 .longname = "Saleae Logic16",
816 .dev_list = dev_list,
817 .dev_clear = dev_clear,
818 .config_get = config_get,
819 .config_set = config_set,
820 .config_list = config_list,
821 .dev_open = dev_open,
822 .dev_close = dev_close,
823 .dev_acquisition_start = dev_acquisition_start,
824 .dev_acquisition_stop = dev_acquisition_stop,