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
35 #define USB_INTERFACE 0
36 #define USB_CONFIGURATION 1
37 #define FX2_FIRMWARE FIRMWARE_DIR "/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;
43 static struct sr_dev_driver *di = &saleae_logic16_driver_info;
45 static const int32_t hwopts[] = {
49 static const int32_t hwcaps[] = {
50 SR_CONF_LOGIC_ANALYZER,
52 SR_CONF_VOLTAGE_THRESHOLD,
54 /* These are really implemented in the driver, not the hardware. */
55 SR_CONF_LIMIT_SAMPLES,
59 static const char *probe_names[NUM_PROBES + 1] = {
60 "0", "1", "2", "3", "4", "5", "6", "7", "8",
61 "9", "10", "11", "12", "13", "14", "15",
66 enum voltage_range range;
69 } volt_thresholds[] = {
70 { VOLTAGE_RANGE_18_33_V, 0.7, 1.4 },
71 { VOLTAGE_RANGE_5_V, 1.4, 3.6 },
74 static const uint64_t samplerates[] = {
91 static int init(struct sr_context *sr_ctx)
93 return std_init(sr_ctx, di, LOG_PREFIX);
96 static gboolean check_conf_profile(libusb_device *dev)
98 struct libusb_device_descriptor des;
99 struct libusb_device_handle *hdl;
101 unsigned char strdesc[64];
106 /* Assume the FW has not been loaded, unless proven wrong. */
107 if (libusb_get_device_descriptor(dev, &des) != 0)
110 if (libusb_open(dev, &hdl) != 0)
113 if (libusb_get_string_descriptor_ascii(hdl,
114 des.iManufacturer, strdesc, sizeof(strdesc)) < 0)
116 if (strcmp((const char *)strdesc, "Saleae LLC"))
119 if (libusb_get_string_descriptor_ascii(hdl,
120 des.iProduct, strdesc, sizeof(strdesc)) < 0)
122 if (strcmp((const char *)strdesc, "Logic S/16"))
125 /* If we made it here, it must be a configured Logic16. */
134 static GSList *scan(GSList *options)
136 struct drv_context *drvc;
137 struct dev_context *devc;
138 struct sr_dev_inst *sdi;
139 struct sr_usb_dev_inst *usb;
140 struct sr_probe *probe;
141 struct sr_config *src;
142 GSList *l, *devices, *conn_devices;
143 struct libusb_device_descriptor des;
144 libusb_device **devlist;
145 int devcnt, ret, i, j;
151 for (l = options; l; l = l->next) {
155 conn = g_variant_get_string(src->data, NULL);
160 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
164 /* Find all Logic16 devices and upload firmware to them. */
166 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
167 for (i = 0; devlist[i]; i++) {
170 for (l = conn_devices; l; l = l->next) {
172 if (usb->bus == libusb_get_bus_number(devlist[i])
173 && usb->address == libusb_get_device_address(devlist[i]))
177 /* This device matched none of the ones that
178 * matched the conn specification. */
182 if ((ret = libusb_get_device_descriptor(devlist[i], &des)) != 0) {
183 sr_warn("Failed to get device descriptor: %s.",
184 libusb_error_name(ret));
188 if (des.idVendor != LOGIC16_VID || des.idProduct != LOGIC16_PID)
191 devcnt = g_slist_length(drvc->instances);
192 sdi = sr_dev_inst_new(devcnt, SR_ST_INITIALIZING,
193 "Saleae", "Logic16", NULL);
198 for (j = 0; probe_names[j]; j++) {
199 if (!(probe = sr_probe_new(j, SR_PROBE_LOGIC, TRUE,
202 sdi->probes = g_slist_append(sdi->probes, probe);
205 if (!(devc = g_try_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(devlist[i], USB_CONFIGURATION,
222 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 for "
227 "device %d.", devcnt);
228 sdi->inst_type = SR_INST_USB;
229 sdi->conn = sr_usb_dev_inst_new(
230 libusb_get_bus_number(devlist[i]), 0xff, NULL);
233 libusb_free_device_list(devlist, 1);
234 g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
239 static GSList *dev_list(void)
241 struct drv_context *drvc;
245 return drvc->instances;
248 static int dev_clear(void)
250 return std_dev_clear(di, NULL);
253 static int logic16_dev_open(struct sr_dev_inst *sdi)
255 libusb_device **devlist;
256 struct sr_usb_dev_inst *usb;
257 struct libusb_device_descriptor des;
258 struct drv_context *drvc;
259 int ret, skip, i, device_count;
264 if (sdi->status == SR_ST_ACTIVE)
265 /* Device is already in use. */
269 device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
270 if (device_count < 0) {
271 sr_err("Failed to get device list: %s.",
272 libusb_error_name(device_count));
276 for (i = 0; i < device_count; i++) {
277 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
278 sr_err("Failed to get device descriptor: %s.",
279 libusb_error_name(ret));
283 if (des.idVendor != LOGIC16_VID || des.idProduct != LOGIC16_PID)
286 if (sdi->status == SR_ST_INITIALIZING) {
287 if (skip != sdi->index) {
288 /* Skip devices of this type that aren't the one we want. */
292 } else if (sdi->status == SR_ST_INACTIVE) {
294 * This device is fully enumerated, so we need to find
295 * this device by vendor, product, bus and address.
297 if (libusb_get_bus_number(devlist[i]) != usb->bus
298 || libusb_get_device_address(devlist[i]) != usb->address)
299 /* This is not the one. */
303 if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
304 if (usb->address == 0xff)
306 * First time we touch this device after FW
307 * upload, so we don't know the address yet.
309 usb->address = libusb_get_device_address(devlist[i]);
311 sr_err("Failed to open device: %s.",
312 libusb_error_name(ret));
316 if ((ret = logic16_init_device(sdi)) != SR_OK) {
317 sr_err("Failed to init device.");
321 sdi->status = SR_ST_ACTIVE;
322 sr_info("Opened device %d on %d.%d, interface %d.",
323 sdi->index, usb->bus, usb->address, USB_INTERFACE);
327 libusb_free_device_list(devlist, 1);
329 if (sdi->status != SR_ST_ACTIVE)
335 static int dev_open(struct sr_dev_inst *sdi)
337 struct sr_usb_dev_inst *usb;
338 struct dev_context *devc;
340 int64_t timediff_us, timediff_ms;
346 * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS
347 * milliseconds for the FX2 to renumerate.
350 if (devc->fw_updated > 0) {
351 sr_info("Waiting for device to reset.");
352 /* Takes >= 300ms for the FX2 to be gone from the USB bus. */
353 g_usleep(300 * 1000);
355 while (timediff_ms < MAX_RENUM_DELAY_MS) {
356 if ((ret = logic16_dev_open(sdi)) == SR_OK)
358 g_usleep(100 * 1000);
360 timediff_us = g_get_monotonic_time() - devc->fw_updated;
361 timediff_ms = timediff_us / 1000;
362 sr_spew("Waited %" PRIi64 "ms.", timediff_ms);
365 sr_err("Device failed to renumerate.");
368 sr_info("Device came back after %" PRIi64 "ms.", timediff_ms);
370 sr_info("Firmware upload was not needed.");
371 ret = logic16_dev_open(sdi);
375 sr_err("Unable to open device.");
379 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
382 case LIBUSB_ERROR_BUSY:
383 sr_err("Unable to claim USB interface. Another "
384 "program or driver has already claimed it.");
386 case LIBUSB_ERROR_NO_DEVICE:
387 sr_err("Device has been disconnected.");
390 sr_err("Unable to claim interface: %s.",
391 libusb_error_name(ret));
398 if (devc->cur_samplerate == 0) {
399 /* Samplerate hasn't been set; default to the slowest one. */
400 devc->cur_samplerate = samplerates[0];
406 static int dev_close(struct sr_dev_inst *sdi)
408 struct sr_usb_dev_inst *usb;
411 if (usb->devhdl == NULL)
414 sr_info("Closing device %d on %d.%d interface %d.",
415 sdi->index, usb->bus, usb->address, USB_INTERFACE);
416 libusb_release_interface(usb->devhdl, USB_INTERFACE);
417 libusb_close(usb->devhdl);
419 sdi->status = SR_ST_INACTIVE;
424 static int cleanup(void)
427 struct drv_context *drvc;
429 if (!(drvc = di->priv))
430 /* Can get called on an unused driver, doesn't matter. */
440 static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi)
442 struct dev_context *devc;
443 struct sr_usb_dev_inst *usb;
452 if (!sdi || !sdi->conn)
455 if (usb->address == 255)
456 /* Device still needs to re-enumerate after firmware
457 * upload, so we don't know its (future) address. */
459 snprintf(str, 128, "%d.%d", usb->bus, usb->address);
460 *data = g_variant_new_string(str);
462 case SR_CONF_SAMPLERATE:
466 *data = g_variant_new_uint64(devc->cur_samplerate);
468 case SR_CONF_VOLTAGE_THRESHOLD:
473 for (i = 0; i < ARRAY_SIZE(volt_thresholds); i++) {
474 if (devc->selected_voltage_range !=
475 volt_thresholds[i].range)
477 range[0] = g_variant_new_double(volt_thresholds[i].low);
478 range[1] = g_variant_new_double(volt_thresholds[i].high);
479 *data = g_variant_new_tuple(range, 2);
491 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi)
493 struct dev_context *devc;
498 if (sdi->status != SR_ST_ACTIVE)
499 return SR_ERR_DEV_CLOSED;
505 case SR_CONF_SAMPLERATE:
506 devc->cur_samplerate = g_variant_get_uint64(data);
508 case SR_CONF_LIMIT_SAMPLES:
509 devc->limit_samples = g_variant_get_uint64(data);
511 case SR_CONF_VOLTAGE_THRESHOLD:
512 g_variant_get(data, "(dd)", &low, &high);
514 for (i = 0; i < ARRAY_SIZE(volt_thresholds); i++) {
515 if (fabs(volt_thresholds[i].low - low) < 0.1 &&
516 fabs(volt_thresholds[i].high - high) < 0.1) {
517 devc->selected_voltage_range =
518 volt_thresholds[i].range;
531 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
533 GVariant *gvar, *range[2];
542 case SR_CONF_SCAN_OPTIONS:
543 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
544 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
546 case SR_CONF_DEVICE_OPTIONS:
547 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
548 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
550 case SR_CONF_SAMPLERATE:
551 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
552 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
553 samplerates, ARRAY_SIZE(samplerates), sizeof(uint64_t));
554 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
555 *data = g_variant_builder_end(&gvb);
557 case SR_CONF_VOLTAGE_THRESHOLD:
558 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
559 for (i = 0; i < ARRAY_SIZE(volt_thresholds); i++) {
560 range[0] = g_variant_new_double(volt_thresholds[i].low);
561 range[1] = g_variant_new_double(volt_thresholds[i].high);
562 gvar = g_variant_new_tuple(range, 2);
563 g_variant_builder_add_value(&gvb, gvar);
565 *data = g_variant_builder_end(&gvb);
574 static void abort_acquisition(struct dev_context *devc)
578 devc->num_samples = -1;
580 for (i = devc->num_transfers - 1; i >= 0; i--) {
581 if (devc->transfers[i])
582 libusb_cancel_transfer(devc->transfers[i]);
586 static unsigned int bytes_per_ms(struct dev_context *devc)
588 return devc->cur_samplerate * devc->num_channels / 8000;
591 static size_t get_buffer_size(struct dev_context *devc)
596 * The buffer should be large enough to hold 10ms of data and
599 s = 10 * bytes_per_ms(devc);
600 return (s + 511) & ~511;
603 static unsigned int get_number_of_transfers(struct dev_context *devc)
607 /* Total buffer size should be able to hold about 500ms of data. */
608 n = 500 * bytes_per_ms(devc) / get_buffer_size(devc);
610 if (n > NUM_SIMUL_TRANSFERS)
611 return NUM_SIMUL_TRANSFERS;
616 static unsigned int get_timeout(struct dev_context *devc)
619 unsigned int timeout;
621 total_size = get_buffer_size(devc) * get_number_of_transfers(devc);
622 timeout = total_size / bytes_per_ms(devc);
623 return timeout + timeout / 4; /* Leave a headroom of 25% percent. */
626 static int configure_probes(const struct sr_dev_inst *sdi)
628 struct dev_context *devc;
629 struct sr_probe *probe;
635 devc->cur_channels = 0;
636 devc->num_channels = 0;
637 for (l = sdi->probes; l; l = l->next) {
638 probe = (struct sr_probe *)l->data;
639 if (probe->enabled == FALSE)
642 probe_bit = 1 << (probe->index);
644 devc->cur_channels |= probe_bit;
646 #ifdef WORDS_BIGENDIAN
648 * Output logic data should be stored in little endian format.
649 * To speed things up during conversion, do the switcharoo
652 probe_bit = 1 << (probe->index ^ 8);
655 devc->channel_masks[devc->num_channels++] = probe_bit;
661 static int receive_data(int fd, int revents, void *cb_data)
664 struct dev_context *devc;
665 struct drv_context *drvc;
666 const struct sr_dev_inst *sdi;
675 tv.tv_sec = tv.tv_usec = 0;
676 libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
678 if (devc->num_samples == -2) {
679 logic16_abort_acquisition(sdi);
680 abort_acquisition(devc);
686 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
688 struct dev_context *devc;
689 struct drv_context *drvc;
690 struct sr_usb_dev_inst *usb;
691 struct libusb_transfer *transfer;
692 const struct libusb_pollfd **lupfd;
693 unsigned int i, timeout, num_transfers;
696 size_t size, convsize;
698 if (sdi->status != SR_ST_ACTIVE)
699 return SR_ERR_DEV_CLOSED;
705 /* Configures devc->cur_channels. */
706 if (configure_probes(sdi) != SR_OK) {
707 sr_err("Failed to configure probes.");
711 devc->cb_data = cb_data;
712 devc->num_samples = 0;
713 devc->empty_transfer_count = 0;
714 devc->cur_channel = 0;
715 memset(devc->channel_data, 0, sizeof(devc->channel_data));
717 timeout = get_timeout(devc);
718 num_transfers = get_number_of_transfers(devc);
719 size = get_buffer_size(devc);
720 convsize = (size / devc->num_channels + 2) * 16;
721 devc->submitted_transfers = 0;
724 devc->convbuffer_size = convsize;
725 if (!(devc->convbuffer = g_try_malloc(convsize))) {
726 sr_err("Conversion buffer malloc failed.");
727 return SR_ERR_MALLOC;
730 devc->transfers = g_try_malloc0(sizeof(*devc->transfers) * num_transfers);
731 if (!devc->transfers) {
732 sr_err("USB transfers malloc failed.");
733 g_free(devc->convbuffer);
734 return SR_ERR_MALLOC;
737 if ((ret = logic16_setup_acquisition(sdi, devc->cur_samplerate,
738 devc->cur_channels)) != SR_OK) {
739 g_free(devc->transfers);
740 g_free(devc->convbuffer);
744 devc->num_transfers = num_transfers;
745 for (i = 0; i < num_transfers; i++) {
746 if (!(buf = g_try_malloc(size))) {
747 sr_err("USB transfer buffer malloc failed.");
748 if (devc->submitted_transfers)
749 abort_acquisition(devc);
751 g_free(devc->transfers);
752 g_free(devc->convbuffer);
754 return SR_ERR_MALLOC;
756 transfer = libusb_alloc_transfer(0);
757 libusb_fill_bulk_transfer(transfer, usb->devhdl,
758 2 | LIBUSB_ENDPOINT_IN, buf, size,
759 logic16_receive_transfer, devc, timeout);
760 if ((ret = libusb_submit_transfer(transfer)) != 0) {
761 sr_err("Failed to submit transfer: %s.",
762 libusb_error_name(ret));
763 libusb_free_transfer(transfer);
765 abort_acquisition(devc);
768 devc->transfers[i] = transfer;
769 devc->submitted_transfers++;
772 lupfd = libusb_get_pollfds(drvc->sr_ctx->libusb_ctx);
773 for (i = 0; lupfd[i]; i++);
774 devc->usbfd = g_try_malloc(sizeof(struct libusb_pollfd) * (i + 1));
776 abort_acquisition(devc);
780 for (i = 0; lupfd[i]; i++) {
781 sr_source_add(lupfd[i]->fd, lupfd[i]->events,
782 timeout, receive_data, (void *)sdi);
783 devc->usbfd[i] = lupfd[i]->fd;
788 /* Send header packet to the session bus. */
789 std_session_send_df_header(cb_data, LOG_PREFIX);
791 if ((ret = logic16_start_acquisition(sdi)) != SR_OK) {
792 abort_acquisition(devc);
799 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
805 if (sdi->status != SR_ST_ACTIVE)
806 return SR_ERR_DEV_CLOSED;
808 ret = logic16_abort_acquisition(sdi);
810 abort_acquisition(sdi->priv);
815 SR_PRIV struct sr_dev_driver saleae_logic16_driver_info = {
816 .name = "saleae-logic16",
817 .longname = "Saleae Logic16",
822 .dev_list = dev_list,
823 .dev_clear = dev_clear,
824 .config_get = config_get,
825 .config_set = config_set,
826 .config_list = config_list,
827 .dev_open = dev_open,
828 .dev_close = dev_close,
829 .dev_acquisition_start = dev_acquisition_start,
830 .dev_acquisition_stop = dev_acquisition_stop,