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,
49 static const char *probe_names[] = {
69 static uint64_t supported_samplerates[] = {
83 static struct sr_samplerates samplerates = {
87 supported_samplerates,
90 /* List of struct sr_dev_inst, maintained by opendev()/closedev(). */
91 static GSList *dev_insts = NULL;
92 static libusb_context *usb_context = NULL;
94 static int new_saleae_logic_firmware = 0;
96 static int hw_config_set(int dev_index, int hwcap, void *value);
97 static int hw_acquisition_stop(int dev_index, gpointer session_dev_id);
100 * Check the USB configuration to determine if this is a Saleae Logic.
102 * @return 1 if the device's configuration profile match the Logic firmware's
103 * configuration, 0 otherwise.
105 static int check_conf_profile(libusb_device *dev)
107 struct libusb_device_descriptor des;
108 struct libusb_config_descriptor *conf_dsc = NULL;
109 const struct libusb_interface_descriptor *intf_dsc;
113 /* Assume it's not a Saleae Logic unless proven wrong. */
116 if (libusb_get_device_descriptor(dev, &des) != 0)
119 if (des.bNumConfigurations != 1)
120 /* Need exactly 1 configuration. */
123 if (libusb_get_config_descriptor(dev, 0, &conf_dsc) != 0)
126 if (conf_dsc->bNumInterfaces != 1)
127 /* Need exactly 1 interface. */
130 if (conf_dsc->interface[0].num_altsetting != 1)
131 /* Need just one alternate setting. */
134 intf_dsc = &(conf_dsc->interface[0].altsetting[0]);
135 if (intf_dsc->bNumEndpoints == 4) {
136 /* The new Saleae Logic firmware has 4 endpoints. */
137 new_saleae_logic_firmware = 1;
138 } else if (intf_dsc->bNumEndpoints == 2) {
139 /* The old Saleae Logic firmware has 2 endpoints. */
140 new_saleae_logic_firmware = 0;
142 /* Other number of endpoints -> not a Saleae Logic. */
146 if ((intf_dsc->endpoint[0].bEndpointAddress & 0x8f) !=
147 (1 | LIBUSB_ENDPOINT_OUT))
148 /* The first endpoint should be 1 (outbound). */
151 if ((intf_dsc->endpoint[1].bEndpointAddress & 0x8f) !=
152 (2 | LIBUSB_ENDPOINT_IN))
153 /* The second endpoint should be 2 (inbound). */
156 /* TODO: The new firmware has 4 endpoints... */
158 /* If we made it here, it must be a Saleae Logic. */
163 libusb_free_config_descriptor(conf_dsc);
168 static int sl_open_dev(int dev_index)
170 libusb_device **devlist;
171 struct libusb_device_descriptor des;
172 struct sr_dev_inst *sdi;
176 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
180 if (sdi->status == SR_ST_ACTIVE)
185 libusb_get_device_list(usb_context, &devlist);
186 for (i = 0; devlist[i]; i++) {
187 if ((err = libusb_get_device_descriptor(devlist[i], &des))) {
188 sr_err("logic: failed to get device descriptor: %d", err);
192 if (des.idVendor != fx2->profile->fw_vid
193 || des.idProduct != fx2->profile->fw_pid)
196 if (sdi->status == SR_ST_INITIALIZING) {
197 if (skip != dev_index) {
198 /* Skip devices of this type that aren't the one we want. */
202 } else if (sdi->status == SR_ST_INACTIVE) {
204 * This device is fully enumerated, so we need to find
205 * this device by vendor, product, bus and address.
207 if (libusb_get_bus_number(devlist[i]) != fx2->usb->bus
208 || libusb_get_device_address(devlist[i]) != fx2->usb->address)
209 /* this is not the one */
213 if (!(err = libusb_open(devlist[i], &fx2->usb->devhdl))) {
214 if (fx2->usb->address == 0xff)
216 * first time we touch this device after firmware upload,
217 * so we don't know the address yet.
219 fx2->usb->address = libusb_get_device_address(devlist[i]);
221 sdi->status = SR_ST_ACTIVE;
222 sr_info("logic: opened device %d on %d.%d interface %d",
223 sdi->index, fx2->usb->bus,
224 fx2->usb->address, USB_INTERFACE);
226 sr_err("logic: failed to open device: %d", err);
229 /* if we made it here, we handled the device one way or another */
232 libusb_free_device_list(devlist, 1);
234 if (sdi->status != SR_ST_ACTIVE)
240 static void close_dev(struct sr_dev_inst *sdi)
246 if (fx2->usb->devhdl == NULL)
249 sr_info("logic: closing device %d on %d.%d interface %d", sdi->index,
250 fx2->usb->bus, fx2->usb->address, USB_INTERFACE);
251 libusb_release_interface(fx2->usb->devhdl, USB_INTERFACE);
252 libusb_close(fx2->usb->devhdl);
253 fx2->usb->devhdl = NULL;
254 sdi->status = SR_ST_INACTIVE;
257 static int configure_probes(struct fx2_dev *fx2, GSList *probes)
259 struct sr_probe *probe;
261 int probe_bit, stage, i;
265 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
266 fx2->trigger_mask[i] = 0;
267 fx2->trigger_value[i] = 0;
271 for (l = probes; l; l = l->next) {
272 probe = (struct sr_probe *)l->data;
273 if (probe->enabled == FALSE)
275 probe_bit = 1 << (probe->index - 1);
276 fx2->probe_mask |= probe_bit;
277 if (!(probe->trigger))
281 for (tc = probe->trigger; *tc; tc++) {
282 fx2->trigger_mask[stage] |= probe_bit;
284 fx2->trigger_value[stage] |= probe_bit;
286 if (stage > NUM_TRIGGER_STAGES)
293 * We didn't configure any triggers, make sure acquisition
294 * doesn't wait for any.
296 fx2->trigger_stage = TRIGGER_FIRED;
298 fx2->trigger_stage = 0;
303 static struct fx2_dev *fx2_dev_new(void)
307 if (!(fx2 = g_try_malloc0(sizeof(struct fx2_dev)))) {
308 sr_err("logic: %s: fx2 malloc failed", __func__);
311 fx2->trigger_stage = TRIGGER_FIRED;
322 static int hw_init(const char *devinfo)
324 struct sr_dev_inst *sdi;
325 struct libusb_device_descriptor des;
326 struct fx2_profile *fx2_prof;
328 libusb_device **devlist;
329 int err, devcnt, i, j;
331 /* Avoid compiler warnings. */
334 if (libusb_init(&usb_context) != 0) {
335 sr_err("logic: Failed to initialize USB.");
339 /* Find all Saleae Logic devices and upload firmware to all of them. */
341 libusb_get_device_list(usb_context, &devlist);
342 for (i = 0; devlist[i]; i++) {
344 err = libusb_get_device_descriptor(devlist[i], &des);
346 sr_err("logic: failed to get device descriptor: %d",
351 for (j = 0; supported_fx2[j].orig_vid; j++) {
352 if (des.idVendor == supported_fx2[j].orig_vid
353 && des.idProduct == supported_fx2[j].orig_pid) {
354 fx2_prof = &supported_fx2[j];
359 /* not a supported VID/PID */
362 sdi = sr_dev_inst_new(devcnt, SR_ST_INITIALIZING,
363 fx2_prof->vendor, fx2_prof->model, fx2_prof->model_version);
367 fx2->profile = fx2_prof;
369 dev_insts = g_slist_append(dev_insts, sdi);
371 if (check_conf_profile(devlist[i])) {
372 /* Already has the firmware, so fix the new address. */
373 sr_dbg("logic: Found a Saleae Logic with %s firmware.",
374 new_saleae_logic_firmware ? "new" : "old");
375 sdi->status = SR_ST_INACTIVE;
376 fx2->usb = sr_usb_dev_inst_new
377 (libusb_get_bus_number(devlist[i]),
378 libusb_get_device_address(devlist[i]), NULL);
380 if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION, FIRMWARE) == SR_OK)
381 /* Remember when the firmware on this device was updated */
382 g_get_current_time(&fx2->fw_updated);
384 sr_err("logic: firmware upload failed for "
385 "device %d", devcnt);
386 fx2->usb = sr_usb_dev_inst_new
387 (libusb_get_bus_number(devlist[i]), 0xff, NULL);
391 libusb_free_device_list(devlist, 1);
396 static int hw_opendev(int dev_index)
399 struct sr_dev_inst *sdi;
403 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
408 * if the firmware was recently uploaded, wait up to MAX_RENUM_DELAY ms
409 * for the FX2 to renumerate
412 if (GTV_TO_MSEC(fx2->fw_updated) > 0) {
413 sr_info("logic: waiting for device to reset");
414 /* takes at least 300ms for the FX2 to be gone from the USB bus */
415 g_usleep(300 * 1000);
417 while (timediff < MAX_RENUM_DELAY) {
418 if ((err = sl_open_dev(dev_index)) == SR_OK)
420 g_usleep(100 * 1000);
421 g_get_current_time(&cur_time);
422 timediff = GTV_TO_MSEC(cur_time) - GTV_TO_MSEC(fx2->fw_updated);
424 sr_info("logic: device came back after %d ms", timediff);
426 err = sl_open_dev(dev_index);
430 sr_err("logic: unable to open device");
435 err = libusb_claim_interface(fx2->usb->devhdl, USB_INTERFACE);
437 sr_err("logic: Unable to claim interface: %d", err);
441 if (fx2->cur_samplerate == 0) {
442 /* Samplerate hasn't been set; default to the slowest one. */
443 if (hw_config_set(dev_index, SR_HWCAP_SAMPLERATE,
444 &supported_samplerates[0]) == SR_ERR)
451 static int hw_closedev(int dev_index)
453 struct sr_dev_inst *sdi;
455 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
456 sr_err("logic: %s: sdi was NULL", __func__);
457 return SR_ERR; /* TODO: SR_ERR_ARG? */
466 static int hw_cleanup(void)
469 struct sr_dev_inst *sdi;
473 /* Properly close and free all devices. */
474 for (l = dev_insts; l; l = l->next) {
475 if (!(sdi = l->data)) {
476 /* Log error, but continue cleaning up the rest. */
477 sr_err("logic: %s: sdi was NULL, continuing", __func__);
481 if (!(fx2 = sdi->priv)) {
482 /* Log error, but continue cleaning up the rest. */
483 sr_err("logic: %s: sdi->priv was NULL, continuing",
489 sr_usb_dev_inst_free(fx2->usb);
490 sr_dev_inst_free(sdi);
493 g_slist_free(dev_insts);
497 libusb_exit(usb_context);
503 static void *hw_dev_info_get(int dev_index, int dev_info_id)
505 struct sr_dev_inst *sdi;
509 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
513 switch (dev_info_id) {
517 case SR_DI_NUM_PROBES:
518 info = GINT_TO_POINTER(fx2->profile->num_probes);
520 case SR_DI_PROBE_NAMES:
523 case SR_DI_SAMPLERATES:
526 case SR_DI_TRIGGER_TYPES:
527 info = TRIGGER_TYPES;
529 case SR_DI_CUR_SAMPLERATE:
530 info = &fx2->cur_samplerate;
537 static int hw_get_status(int dev_index)
539 struct sr_dev_inst *sdi;
541 sdi = sr_dev_inst_get(dev_insts, dev_index);
545 return SR_ST_NOT_FOUND;
548 static int *hw_hwcap_get_all(void)
553 static uint8_t new_firmware_divider_value(uint64_t samplerate)
555 switch (samplerate) {
588 /* Shouldn't happen. */
589 sr_err("logic: %s: Invalid samplerate %" PRIu64 "",
590 __func__, samplerate);
594 static int config_set_samplerate(struct sr_dev_inst *sdi, uint64_t samplerate)
599 unsigned char buf[2];
602 for (i = 0; supported_samplerates[i]; i++) {
603 if (supported_samplerates[i] == samplerate)
606 if (supported_samplerates[i] == 0)
607 return SR_ERR_SAMPLERATE;
609 if (new_saleae_logic_firmware)
610 divider = new_firmware_divider_value(samplerate);
612 divider = (uint8_t) (48 / (samplerate / 1000000.0)) - 1;
614 sr_info("logic: setting samplerate to %" PRIu64 " Hz (divider %d)",
615 samplerate, divider);
617 buf[0] = (new_saleae_logic_firmware) ? 0xd5 : 0x01;
619 ret = libusb_bulk_transfer(fx2->usb->devhdl, 1 | LIBUSB_ENDPOINT_OUT,
620 buf, 2, &result, 500);
622 sr_err("logic: failed to set samplerate: %d", ret);
625 fx2->cur_samplerate = samplerate;
630 static int hw_config_set(int dev_index, int hwcap, void *value)
632 struct sr_dev_inst *sdi;
637 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
641 if (hwcap == SR_HWCAP_SAMPLERATE) {
643 ret = config_set_samplerate(sdi, *tmp_u64);
644 } else if (hwcap == SR_HWCAP_PROBECONFIG) {
645 ret = configure_probes(fx2, (GSList *) value);
646 } else if (hwcap == SR_HWCAP_LIMIT_SAMPLES) {
648 fx2->limit_samples = *tmp_u64;
657 static int receive_data(int fd, int revents, void *user_data)
661 /* Avoid compiler warnings. */
666 tv.tv_sec = tv.tv_usec = 0;
667 libusb_handle_events_timeout(usb_context, &tv);
672 static void receive_transfer(struct libusb_transfer *transfer)
674 /* TODO: these statics have to move to fx2_dev struct */
675 static int num_samples = 0;
676 static int empty_transfer_count = 0;
677 struct sr_datafeed_packet packet;
678 struct sr_datafeed_logic logic;
680 int cur_buflen, trigger_offset, i;
681 unsigned char *cur_buf, *new_buf;
683 /* hw_acquisition_stop() is telling us to stop. */
684 if (transfer == NULL)
688 * If acquisition has already ended, just free any queued up
689 * transfer that come in.
691 if (num_samples == -1) {
693 libusb_free_transfer(transfer);
697 sr_info("logic: receive_transfer(): status %d received %d bytes",
698 transfer->status, transfer->actual_length);
700 /* Save incoming transfer before reusing the transfer struct. */
701 cur_buf = transfer->buffer;
702 cur_buflen = transfer->actual_length;
703 fx2 = transfer->user_data;
705 /* Fire off a new request. */
706 if (!(new_buf = g_try_malloc(4096))) {
707 sr_err("logic: %s: new_buf malloc failed", __func__);
708 return; /* TODO: SR_ERR_MALLOC */
711 transfer->buffer = new_buf;
712 transfer->length = 4096;
713 if (libusb_submit_transfer(transfer) != 0) {
714 /* TODO: Stop session? */
715 /* TODO: Better error message. */
716 sr_err("logic: %s: libusb_submit_transfer error", __func__);
719 if (cur_buflen == 0) {
720 empty_transfer_count++;
721 if (empty_transfer_count > MAX_EMPTY_TRANSFERS) {
723 * The FX2 gave up. End the acquisition, the frontend
724 * will work out that the samplecount is short.
726 hw_acquisition_stop(-1, fx2->session_data);
730 empty_transfer_count = 0;
734 if (fx2->trigger_stage >= 0) {
735 for (i = 0; i < cur_buflen; i++) {
737 if ((cur_buf[i] & fx2->trigger_mask[fx2->trigger_stage]) == fx2->trigger_value[fx2->trigger_stage]) {
738 /* Match on this trigger stage. */
739 fx2->trigger_buffer[fx2->trigger_stage] = cur_buf[i];
740 fx2->trigger_stage++;
742 if (fx2->trigger_stage == NUM_TRIGGER_STAGES || fx2->trigger_mask[fx2->trigger_stage] == 0) {
743 /* Match on all trigger stages, we're done. */
744 trigger_offset = i + 1;
747 * TODO: Send pre-trigger buffer to session bus.
748 * Tell the frontend we hit the trigger here.
750 packet.type = SR_DF_TRIGGER;
751 packet.payload = NULL;
752 sr_session_bus(fx2->session_data, &packet);
755 * Send the samples that triggered it, since we're
756 * skipping past them.
758 packet.type = SR_DF_LOGIC;
759 packet.payload = &logic;
760 logic.length = fx2->trigger_stage;
762 logic.data = fx2->trigger_buffer;
763 sr_session_bus(fx2->session_data, &packet);
765 fx2->trigger_stage = TRIGGER_FIRED;
772 * We had a match before, but not in the next sample. However, we may
773 * have a match on this stage in the next bit -- trigger on 0001 will
774 * fail on seeing 00001, so we need to go back to stage 0 -- but at
775 * the next sample from the one that matched originally, which the
776 * counter increment at the end of the loop takes care of.
778 if (fx2->trigger_stage > 0) {
779 i -= fx2->trigger_stage;
781 i = -1; /* Oops, went back past this buffer. */
782 /* Reset trigger stage. */
783 fx2->trigger_stage = 0;
788 if (fx2->trigger_stage == TRIGGER_FIRED) {
789 /* Send the incoming transfer to the session bus. */
790 packet.type = SR_DF_LOGIC;
791 packet.payload = &logic;
792 logic.length = cur_buflen - trigger_offset;
794 logic.data = cur_buf + trigger_offset;
795 sr_session_bus(fx2->session_data, &packet);
798 num_samples += cur_buflen;
799 if (fx2->limit_samples && (unsigned int) num_samples > fx2->limit_samples) {
800 hw_acquisition_stop(-1, fx2->session_data);
804 * TODO: Buffer pre-trigger data in capture
805 * ratio-sized buffer.
810 static int hw_acquisition_start(int dev_index, gpointer session_data)
812 struct sr_dev_inst *sdi;
813 struct sr_datafeed_packet *packet;
814 struct sr_datafeed_header *header;
816 struct libusb_transfer *transfer;
817 const struct libusb_pollfd **lupfd;
821 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
824 fx2->session_data = session_data;
826 if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
827 sr_err("logic: %s: packet malloc failed", __func__);
828 return SR_ERR_MALLOC;
831 if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
832 sr_err("logic: %s: header malloc failed", __func__);
833 return SR_ERR_MALLOC;
836 /* Start with 2K transfer, subsequently increased to 4K. */
838 for (i = 0; i < NUM_SIMUL_TRANSFERS; i++) {
839 if (!(buf = g_try_malloc(size))) {
840 sr_err("logic: %s: buf malloc failed", __func__);
841 return SR_ERR_MALLOC;
843 transfer = libusb_alloc_transfer(0);
844 libusb_fill_bulk_transfer(transfer, fx2->usb->devhdl,
845 2 | LIBUSB_ENDPOINT_IN, buf, size,
846 receive_transfer, fx2, 40);
847 if (libusb_submit_transfer(transfer) != 0) {
848 /* TODO: Free them all. */
849 libusb_free_transfer(transfer);
856 lupfd = libusb_get_pollfds(usb_context);
857 for (i = 0; lupfd[i]; i++)
858 sr_source_add(lupfd[i]->fd, lupfd[i]->events, 40, receive_data,
860 free(lupfd); /* NOT g_free()! */
862 packet->type = SR_DF_HEADER;
863 packet->payload = header;
864 header->feed_version = 1;
865 gettimeofday(&header->starttime, NULL);
866 header->samplerate = fx2->cur_samplerate;
867 header->num_logic_probes = fx2->profile->num_probes;
868 sr_session_bus(session_data, packet);
875 /* This stops acquisition on ALL devices, ignoring dev_index. */
876 static int hw_acquisition_stop(int dev_index, gpointer session_data)
878 struct sr_datafeed_packet packet;
880 /* Avoid compiler warnings. */
883 packet.type = SR_DF_END;
884 sr_session_bus(session_data, &packet);
886 receive_transfer(NULL);
888 /* TODO: Need to cancel and free any queued up transfers. */
893 SR_PRIV struct sr_dev_plugin saleae_logic_plugin_info = {
894 .name = "saleae-logic",
895 .longname = "Saleae Logic",
898 .cleanup = hw_cleanup,
899 .opendev = hw_opendev,
900 .closedev = hw_closedev,
901 .dev_info_get = hw_dev_info_get,
902 .get_status = hw_get_status,
903 .hwcap_get_all = hw_hwcap_get_all,
904 .config_set = hw_config_set,
905 .acquisition_start = hw_acquisition_start,
906 .acquisition_stop = hw_acquisition_stop,