2 * This file is part of the sigrok project.
4 * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 #include "sigrok-internal.h"
29 #include "saleae-logic.h"
31 static struct fx2_profile supported_fx2[] = {
33 { 0x0925, 0x3881, 0x0925, 0x3881, "Saleae", "Logic", NULL, 8 },
34 /* default Cypress FX2 without EEPROM */
35 { 0x04b4, 0x8613, 0x0925, 0x3881, "Cypress", "FX2", NULL, 16 },
36 { 0, 0, 0, 0, 0, 0, 0, 0 }
39 static int hwcaps[] = {
40 SR_HWCAP_LOGIC_ANALYZER,
43 /* These are really implemented in the driver, not the hardware. */
44 SR_HWCAP_LIMIT_SAMPLES,
50 * Probes are numbered 1-8.
52 * TODO: FX2 eval boards with the standard Cypress VID/PID can have 16 pins
53 * or probes in theory, which is not supported by the Saleae Logic firmware.
55 static const char *probe_names[] = {
75 static uint64_t supported_samplerates[] = {
89 static struct sr_samplerates samplerates = {
93 supported_samplerates,
96 /* List of struct sr_dev_inst, maintained by dev_open()/dev_close(). */
97 static GSList *dev_insts = NULL;
98 static libusb_context *usb_context = NULL;
100 static int new_saleae_logic_firmware = 0;
102 static int hw_dev_config_set(int dev_index, int hwcap, void *value);
103 static int hw_dev_acquisition_stop(int dev_index, void *cb_data);
106 * Check the USB configuration to determine if this is a Saleae Logic.
108 * @return 1 if the device's configuration profile match the Logic firmware's
109 * configuration, 0 otherwise.
111 static int check_conf_profile(libusb_device *dev)
113 struct libusb_device_descriptor des;
114 struct libusb_config_descriptor *conf_dsc = NULL;
115 const struct libusb_interface_descriptor *intf_dsc;
119 /* Assume it's not a Saleae Logic unless proven wrong. */
122 if (libusb_get_device_descriptor(dev, &des) != 0)
125 if (des.bNumConfigurations != 1)
126 /* Need exactly 1 configuration. */
129 if (libusb_get_config_descriptor(dev, 0, &conf_dsc) != 0)
132 if (conf_dsc->bNumInterfaces != 1)
133 /* Need exactly 1 interface. */
136 if (conf_dsc->interface[0].num_altsetting != 1)
137 /* Need just one alternate setting. */
140 intf_dsc = &(conf_dsc->interface[0].altsetting[0]);
141 if (intf_dsc->bNumEndpoints == 4) {
142 /* The new Saleae Logic firmware has 4 endpoints. */
143 new_saleae_logic_firmware = 1;
144 } else if (intf_dsc->bNumEndpoints == 2) {
145 /* The old Saleae Logic firmware has 2 endpoints. */
146 new_saleae_logic_firmware = 0;
148 /* Other number of endpoints -> not a Saleae Logic. */
152 if ((intf_dsc->endpoint[0].bEndpointAddress & 0x8f) !=
153 (1 | LIBUSB_ENDPOINT_OUT))
154 /* The first endpoint should be 1 (outbound). */
157 if ((intf_dsc->endpoint[1].bEndpointAddress & 0x8f) !=
158 (2 | LIBUSB_ENDPOINT_IN))
159 /* The second endpoint should be 2 (inbound). */
162 /* TODO: The new firmware has 4 endpoints... */
164 /* If we made it here, it must be a Saleae Logic. */
169 libusb_free_config_descriptor(conf_dsc);
174 static int sl_open_dev(int dev_index)
176 libusb_device **devlist;
177 struct libusb_device_descriptor des;
178 struct sr_dev_inst *sdi;
182 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
186 if (sdi->status == SR_ST_ACTIVE)
191 libusb_get_device_list(usb_context, &devlist);
192 for (i = 0; devlist[i]; i++) {
193 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
194 sr_err("logic: failed to get device descriptor: %d", ret);
198 if (des.idVendor != ctx->profile->fw_vid
199 || des.idProduct != ctx->profile->fw_pid)
202 if (sdi->status == SR_ST_INITIALIZING) {
203 if (skip != dev_index) {
204 /* Skip devices of this type that aren't the one we want. */
208 } else if (sdi->status == SR_ST_INACTIVE) {
210 * This device is fully enumerated, so we need to find
211 * this device by vendor, product, bus and address.
213 if (libusb_get_bus_number(devlist[i]) != ctx->usb->bus
214 || libusb_get_device_address(devlist[i]) != ctx->usb->address)
215 /* this is not the one */
219 if (!(ret = libusb_open(devlist[i], &ctx->usb->devhdl))) {
220 if (ctx->usb->address == 0xff)
222 * first time we touch this device after firmware upload,
223 * so we don't know the address yet.
225 ctx->usb->address = libusb_get_device_address(devlist[i]);
227 sdi->status = SR_ST_ACTIVE;
228 sr_info("logic: opened device %d on %d.%d interface %d",
229 sdi->index, ctx->usb->bus,
230 ctx->usb->address, USB_INTERFACE);
232 sr_err("logic: failed to open device: %d", ret);
235 /* if we made it here, we handled the device one way or another */
238 libusb_free_device_list(devlist, 1);
240 if (sdi->status != SR_ST_ACTIVE)
246 static void close_dev(struct sr_dev_inst *sdi)
252 if (ctx->usb->devhdl == NULL)
255 sr_info("logic: closing device %d on %d.%d interface %d", sdi->index,
256 ctx->usb->bus, ctx->usb->address, USB_INTERFACE);
257 libusb_release_interface(ctx->usb->devhdl, USB_INTERFACE);
258 libusb_close(ctx->usb->devhdl);
259 ctx->usb->devhdl = NULL;
260 sdi->status = SR_ST_INACTIVE;
263 static int configure_probes(struct context *ctx, GSList *probes)
265 struct sr_probe *probe;
267 int probe_bit, stage, i;
271 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
272 ctx->trigger_mask[i] = 0;
273 ctx->trigger_value[i] = 0;
277 for (l = probes; l; l = l->next) {
278 probe = (struct sr_probe *)l->data;
279 if (probe->enabled == FALSE)
281 probe_bit = 1 << (probe->index - 1);
282 ctx->probe_mask |= probe_bit;
283 if (!(probe->trigger))
287 for (tc = probe->trigger; *tc; tc++) {
288 ctx->trigger_mask[stage] |= probe_bit;
290 ctx->trigger_value[stage] |= probe_bit;
292 if (stage > NUM_TRIGGER_STAGES)
299 * We didn't configure any triggers, make sure acquisition
300 * doesn't wait for any.
302 ctx->trigger_stage = TRIGGER_FIRED;
304 ctx->trigger_stage = 0;
309 static struct context *fx2_dev_new(void)
313 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
314 sr_err("logic: %s: ctx malloc failed", __func__);
317 ctx->trigger_stage = TRIGGER_FIRED;
328 static int hw_init(const char *devinfo)
330 struct sr_dev_inst *sdi;
331 struct libusb_device_descriptor des;
332 struct fx2_profile *fx2_prof;
334 libusb_device **devlist;
335 int ret, devcnt, i, j;
337 /* Avoid compiler warnings. */
340 if (libusb_init(&usb_context) != 0) {
341 sr_err("logic: Failed to initialize USB.");
345 /* Find all Saleae Logic devices and upload firmware to all of them. */
347 libusb_get_device_list(usb_context, &devlist);
348 for (i = 0; devlist[i]; i++) {
350 ret = libusb_get_device_descriptor(devlist[i], &des);
352 sr_err("logic: failed to get device descriptor: %d",
357 for (j = 0; supported_fx2[j].orig_vid; j++) {
358 if (des.idVendor == supported_fx2[j].orig_vid
359 && des.idProduct == supported_fx2[j].orig_pid) {
360 fx2_prof = &supported_fx2[j];
365 /* not a supported VID/PID */
368 sdi = sr_dev_inst_new(devcnt, SR_ST_INITIALIZING,
369 fx2_prof->vendor, fx2_prof->model, fx2_prof->model_version);
373 ctx->profile = fx2_prof;
375 dev_insts = g_slist_append(dev_insts, sdi);
377 if (check_conf_profile(devlist[i])) {
378 /* Already has the firmware, so fix the new address. */
379 sr_dbg("logic: Found a Saleae Logic with %s firmware.",
380 new_saleae_logic_firmware ? "new" : "old");
381 sdi->status = SR_ST_INACTIVE;
382 ctx->usb = sr_usb_dev_inst_new
383 (libusb_get_bus_number(devlist[i]),
384 libusb_get_device_address(devlist[i]), NULL);
386 if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION, FIRMWARE) == SR_OK)
387 /* Remember when the firmware on this device was updated */
388 g_get_current_time(&ctx->fw_updated);
390 sr_err("logic: firmware upload failed for "
391 "device %d", devcnt);
392 ctx->usb = sr_usb_dev_inst_new
393 (libusb_get_bus_number(devlist[i]), 0xff, NULL);
397 libusb_free_device_list(devlist, 1);
402 static int hw_dev_open(int dev_index)
405 struct sr_dev_inst *sdi;
409 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
414 * if the firmware was recently uploaded, wait up to MAX_RENUM_DELAY ms
415 * for the FX2 to renumerate
418 if (GTV_TO_MSEC(ctx->fw_updated) > 0) {
419 sr_info("logic: waiting for device to reset");
420 /* takes at least 300ms for the FX2 to be gone from the USB bus */
421 g_usleep(300 * 1000);
423 while (timediff < MAX_RENUM_DELAY) {
424 if ((ret = sl_open_dev(dev_index)) == SR_OK)
426 g_usleep(100 * 1000);
427 g_get_current_time(&cur_time);
428 timediff = GTV_TO_MSEC(cur_time) - GTV_TO_MSEC(ctx->fw_updated);
430 sr_info("logic: device came back after %d ms", timediff);
432 ret = sl_open_dev(dev_index);
436 sr_err("logic: unable to open device");
441 ret = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
443 sr_err("logic: Unable to claim interface: %d", ret);
447 if (ctx->cur_samplerate == 0) {
448 /* Samplerate hasn't been set; default to the slowest one. */
449 if (hw_dev_config_set(dev_index, SR_HWCAP_SAMPLERATE,
450 &supported_samplerates[0]) == SR_ERR)
457 static int hw_dev_close(int dev_index)
459 struct sr_dev_inst *sdi;
461 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
462 sr_err("logic: %s: sdi was NULL", __func__);
472 static int hw_cleanup(void)
475 struct sr_dev_inst *sdi;
479 /* Properly close and free all devices. */
480 for (l = dev_insts; l; l = l->next) {
481 if (!(sdi = l->data)) {
482 /* Log error, but continue cleaning up the rest. */
483 sr_err("logic: %s: sdi was NULL, continuing", __func__);
487 if (!(ctx = sdi->priv)) {
488 /* Log error, but continue cleaning up the rest. */
489 sr_err("logic: %s: sdi->priv was NULL, continuing",
495 sr_usb_dev_inst_free(ctx->usb);
496 sr_dev_inst_free(sdi);
499 g_slist_free(dev_insts);
503 libusb_exit(usb_context);
509 static void *hw_dev_info_get(int dev_index, int dev_info_id)
511 struct sr_dev_inst *sdi;
515 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
519 switch (dev_info_id) {
523 case SR_DI_NUM_PROBES:
524 info = GINT_TO_POINTER(ctx->profile->num_probes);
526 case SR_DI_PROBE_NAMES:
529 case SR_DI_SAMPLERATES:
532 case SR_DI_TRIGGER_TYPES:
533 info = TRIGGER_TYPES;
535 case SR_DI_CUR_SAMPLERATE:
536 info = &ctx->cur_samplerate;
543 static int hw_dev_status_get(int dev_index)
545 struct sr_dev_inst *sdi;
547 sdi = sr_dev_inst_get(dev_insts, dev_index);
551 return SR_ST_NOT_FOUND;
554 static int *hw_hwcap_get_all(void)
559 static uint8_t new_firmware_divider_value(uint64_t samplerate)
561 switch (samplerate) {
594 /* Shouldn't happen. */
595 sr_err("logic: %s: Invalid samplerate %" PRIu64 "",
596 __func__, samplerate);
600 static int set_samplerate(struct sr_dev_inst *sdi, uint64_t samplerate)
605 unsigned char buf[2];
608 for (i = 0; supported_samplerates[i]; i++) {
609 if (supported_samplerates[i] == samplerate)
612 if (supported_samplerates[i] == 0)
613 return SR_ERR_SAMPLERATE;
615 if (new_saleae_logic_firmware)
616 divider = new_firmware_divider_value(samplerate);
618 divider = (uint8_t) (48 / (samplerate / 1000000.0)) - 1;
620 sr_info("logic: setting samplerate to %" PRIu64 " Hz (divider %d)",
621 samplerate, divider);
623 buf[0] = (new_saleae_logic_firmware) ? 0xd5 : 0x01;
625 ret = libusb_bulk_transfer(ctx->usb->devhdl, 1 | LIBUSB_ENDPOINT_OUT,
626 buf, 2, &result, 500);
628 sr_err("logic: failed to set samplerate: %d", ret);
631 ctx->cur_samplerate = samplerate;
636 static int hw_dev_config_set(int dev_index, int hwcap, void *value)
638 struct sr_dev_inst *sdi;
642 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
646 if (hwcap == SR_HWCAP_SAMPLERATE) {
647 ret = set_samplerate(sdi, *(uint64_t *)value);
648 } else if (hwcap == SR_HWCAP_PROBECONFIG) {
649 ret = configure_probes(ctx, (GSList *) value);
650 } else if (hwcap == SR_HWCAP_LIMIT_SAMPLES) {
651 ctx->limit_samples = *(uint64_t *)value;
660 static int receive_data(int fd, int revents, void *cb_data)
664 /* Avoid compiler warnings. */
669 tv.tv_sec = tv.tv_usec = 0;
670 libusb_handle_events_timeout(usb_context, &tv);
675 static void abort_acquisition(struct context *ctx)
677 struct sr_datafeed_packet packet;
679 packet.type = SR_DF_END;
680 sr_session_send(ctx->session_dev_id, &packet);
682 ctx->num_samples = -1;
684 /* TODO: Need to cancel and free any queued up transfers. */
687 static void receive_transfer(struct libusb_transfer *transfer)
689 /* TODO: These statics have to move to the ctx struct. */
690 static int empty_transfer_count = 0;
691 struct sr_datafeed_packet packet;
692 struct sr_datafeed_logic logic;
693 struct context *ctx = transfer->user_data;
694 int cur_buflen, trigger_offset, i;
695 unsigned char *cur_buf, *new_buf;
698 * If acquisition has already ended, just free any queued up
699 * transfer that come in.
701 if (ctx->num_samples == -1) {
703 libusb_free_transfer(transfer);
707 sr_info("logic: receive_transfer(): status %d received %d bytes",
708 transfer->status, transfer->actual_length);
710 /* Save incoming transfer before reusing the transfer struct. */
711 cur_buf = transfer->buffer;
712 cur_buflen = transfer->actual_length;
713 ctx = transfer->user_data;
715 /* Fire off a new request. */
716 if (!(new_buf = g_try_malloc(4096))) {
717 sr_err("logic: %s: new_buf malloc failed", __func__);
718 return; /* TODO: SR_ERR_MALLOC */
721 transfer->buffer = new_buf;
722 transfer->length = 4096;
723 if (libusb_submit_transfer(transfer) != 0) {
724 /* TODO: Stop session? */
725 /* TODO: Better error message. */
726 sr_err("logic: %s: libusb_submit_transfer error", __func__);
729 if (cur_buflen == 0) {
730 empty_transfer_count++;
731 if (empty_transfer_count > MAX_EMPTY_TRANSFERS) {
733 * The FX2 gave up. End the acquisition, the frontend
734 * will work out that the samplecount is short.
736 abort_acquisition(ctx);
740 empty_transfer_count = 0;
744 if (ctx->trigger_stage >= 0) {
745 for (i = 0; i < cur_buflen; i++) {
747 if ((cur_buf[i] & ctx->trigger_mask[ctx->trigger_stage]) == ctx->trigger_value[ctx->trigger_stage]) {
748 /* Match on this trigger stage. */
749 ctx->trigger_buffer[ctx->trigger_stage] = cur_buf[i];
750 ctx->trigger_stage++;
752 if (ctx->trigger_stage == NUM_TRIGGER_STAGES || ctx->trigger_mask[ctx->trigger_stage] == 0) {
753 /* Match on all trigger stages, we're done. */
754 trigger_offset = i + 1;
757 * TODO: Send pre-trigger buffer to session bus.
758 * Tell the frontend we hit the trigger here.
760 packet.type = SR_DF_TRIGGER;
761 packet.payload = NULL;
762 sr_session_send(ctx->session_dev_id, &packet);
765 * Send the samples that triggered it, since we're
766 * skipping past them.
768 packet.type = SR_DF_LOGIC;
769 packet.payload = &logic;
770 logic.length = ctx->trigger_stage;
772 logic.data = ctx->trigger_buffer;
773 sr_session_send(ctx->session_dev_id, &packet);
775 ctx->trigger_stage = TRIGGER_FIRED;
782 * We had a match before, but not in the next sample. However, we may
783 * have a match on this stage in the next bit -- trigger on 0001 will
784 * fail on seeing 00001, so we need to go back to stage 0 -- but at
785 * the next sample from the one that matched originally, which the
786 * counter increment at the end of the loop takes care of.
788 if (ctx->trigger_stage > 0) {
789 i -= ctx->trigger_stage;
791 i = -1; /* Oops, went back past this buffer. */
792 /* Reset trigger stage. */
793 ctx->trigger_stage = 0;
798 if (ctx->trigger_stage == TRIGGER_FIRED) {
799 /* Send the incoming transfer to the session bus. */
800 packet.type = SR_DF_LOGIC;
801 packet.payload = &logic;
802 logic.length = cur_buflen - trigger_offset;
804 logic.data = cur_buf + trigger_offset;
805 sr_session_send(ctx->session_dev_id, &packet);
808 ctx->num_samples += cur_buflen;
809 if (ctx->limit_samples && (unsigned int)ctx->num_samples > ctx->limit_samples) {
810 abort_acquisition(ctx);
814 * TODO: Buffer pre-trigger data in capture
815 * ratio-sized buffer.
820 static int hw_dev_acquisition_start(int dev_index, void *cb_data)
822 struct sr_dev_inst *sdi;
823 struct sr_datafeed_packet *packet;
824 struct sr_datafeed_header *header;
826 struct libusb_transfer *transfer;
827 const struct libusb_pollfd **lupfd;
831 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
834 ctx->session_dev_id = cb_data;
835 ctx->num_samples = 0;
837 if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
838 sr_err("logic: %s: packet malloc failed", __func__);
839 return SR_ERR_MALLOC;
842 if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
843 sr_err("logic: %s: header malloc failed", __func__);
844 return SR_ERR_MALLOC;
847 /* Start with 2K transfer, subsequently increased to 4K. */
849 for (i = 0; i < NUM_SIMUL_TRANSFERS; i++) {
850 if (!(buf = g_try_malloc(size))) {
851 sr_err("logic: %s: buf malloc failed", __func__);
852 return SR_ERR_MALLOC;
854 transfer = libusb_alloc_transfer(0);
855 libusb_fill_bulk_transfer(transfer, ctx->usb->devhdl,
856 2 | LIBUSB_ENDPOINT_IN, buf, size,
857 receive_transfer, ctx, 40);
858 if (libusb_submit_transfer(transfer) != 0) {
859 /* TODO: Free them all. */
860 libusb_free_transfer(transfer);
867 lupfd = libusb_get_pollfds(usb_context);
868 for (i = 0; lupfd[i]; i++)
869 sr_source_add(lupfd[i]->fd, lupfd[i]->events, 40, receive_data,
871 free(lupfd); /* NOT g_free()! */
873 packet->type = SR_DF_HEADER;
874 packet->payload = header;
875 header->feed_version = 1;
876 gettimeofday(&header->starttime, NULL);
877 header->samplerate = ctx->cur_samplerate;
878 header->num_logic_probes = ctx->profile->num_probes;
879 sr_session_send(ctx->session_dev_id, packet);
886 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
887 static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
889 struct sr_dev_inst *sdi;
891 /* unused parameter */
894 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
897 abort_acquisition(sdi->priv);
902 SR_PRIV struct sr_dev_driver saleae_logic_driver_info = {
903 .name = "saleae-logic",
904 .longname = "Saleae Logic",
907 .cleanup = hw_cleanup,
908 .dev_open = hw_dev_open,
909 .dev_close = hw_dev_close,
910 .dev_info_get = hw_dev_info_get,
911 .dev_status_get = hw_dev_status_get,
912 .hwcap_get_all = hw_hwcap_get_all,
913 .dev_config_set = hw_dev_config_set,
914 .dev_acquisition_start = hw_dev_acquisition_start,
915 .dev_acquisition_stop = hw_dev_acquisition_stop,