2 * This file is part of the sigrok project.
4 * Copyright (C) 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 capabilities[] = {
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_device_instance, maintained by opendev()/closedev(). */
91 static GSList *device_instances = NULL;
92 static libusb_context *usb_context = NULL;
94 static int hw_set_configuration(int device_index, int capability, void *value);
95 static void hw_stop_acquisition(int device_index, gpointer session_device_id);
98 * Check the USB configuration to determine if this is a Saleae Logic.
100 * @return 1 if the device's configuration profile match the Logic firmware's
101 * configuration, 0 otherwise.
103 static int check_conf_profile(libusb_device *dev)
105 struct libusb_device_descriptor des;
106 struct libusb_config_descriptor *conf_dsc = NULL;
107 const struct libusb_interface_descriptor *intf_dsc;
111 /* Assume it's not a Saleae Logic unless proven wrong. */
114 if (libusb_get_device_descriptor(dev, &des) != 0)
117 if (des.bNumConfigurations != 1)
118 /* Need exactly 1 configuration. */
121 if (libusb_get_config_descriptor(dev, 0, &conf_dsc) != 0)
124 if (conf_dsc->bNumInterfaces != 1)
125 /* Need exactly 1 interface. */
128 if (conf_dsc->interface[0].num_altsetting != 1)
129 /* Need just one alternate setting. */
132 intf_dsc = &(conf_dsc->interface[0].altsetting[0]);
133 if (intf_dsc->bNumEndpoints != 2)
134 /* Need 2 endpoints. */
137 if ((intf_dsc->endpoint[0].bEndpointAddress & 0x8f) !=
138 (1 | LIBUSB_ENDPOINT_OUT))
139 /* First endpoint should be 1 (outbound). */
142 if ((intf_dsc->endpoint[1].bEndpointAddress & 0x8f) !=
143 (2 | LIBUSB_ENDPOINT_IN))
144 /* First endpoint should be 2 (inbound). */
147 /* If we made it here, it must be a Saleae Logic. */
152 libusb_free_config_descriptor(conf_dsc);
157 static int sl_open_device(int device_index)
159 libusb_device **devlist;
160 struct libusb_device_descriptor des;
161 struct sr_device_instance *sdi;
162 struct fx2_device *fx2;
165 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
169 if (sdi->status == SR_ST_ACTIVE)
174 libusb_get_device_list(usb_context, &devlist);
175 for (i = 0; devlist[i]; i++) {
176 if ((err = libusb_get_device_descriptor(devlist[i], &des))) {
177 sr_warn("failed to get device descriptor: %d", err);
181 if (des.idVendor != fx2->profile->fw_vid || des.idProduct != fx2->profile->fw_pid)
184 if (sdi->status == SR_ST_INITIALIZING) {
185 if (skip != device_index) {
186 /* Skip devices of this type that aren't the one we want. */
190 } else if (sdi->status == SR_ST_INACTIVE) {
192 * This device is fully enumerated, so we need to find this
193 * device by vendor, product, bus and address.
195 if (libusb_get_bus_number(devlist[i]) != fx2->usb->bus
196 || libusb_get_device_address(devlist[i]) != fx2->usb->address)
197 /* this is not the one */
201 if (!(err = libusb_open(devlist[i], &fx2->usb->devhdl))) {
202 if (fx2->usb->address == 0xff)
204 * first time we touch this device after firmware upload,
205 * so we don't know the address yet.
207 fx2->usb->address = libusb_get_device_address(devlist[i]);
209 sdi->status = SR_ST_ACTIVE;
210 sr_info("saleae: opened device %d on %d.%d interface %d",
211 sdi->index, fx2->usb->bus,
212 fx2->usb->address, USB_INTERFACE);
214 sr_warn("failed to open device: %d", err);
217 /* if we made it here, we handled the device one way or another */
220 libusb_free_device_list(devlist, 1);
222 if (sdi->status != SR_ST_ACTIVE)
228 static void close_device(struct sr_device_instance *sdi)
230 struct fx2_device *fx2;
234 if (fx2->usb->devhdl == NULL)
237 sr_info("saleae: closing device %d on %d.%d interface %d", sdi->index,
238 fx2->usb->bus, fx2->usb->address, USB_INTERFACE);
239 libusb_release_interface(fx2->usb->devhdl, USB_INTERFACE);
240 libusb_close(fx2->usb->devhdl);
241 fx2->usb->devhdl = NULL;
242 sdi->status = SR_ST_INACTIVE;
245 static int configure_probes(struct fx2_device *fx2, GSList *probes)
247 struct sr_probe *probe;
249 int probe_bit, stage, i;
253 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
254 fx2->trigger_mask[i] = 0;
255 fx2->trigger_value[i] = 0;
259 for (l = probes; l; l = l->next) {
260 probe = (struct sr_probe *)l->data;
261 if (probe->enabled == FALSE)
263 probe_bit = 1 << (probe->index - 1);
264 fx2->probe_mask |= probe_bit;
265 if (!(probe->trigger))
269 for (tc = probe->trigger; *tc; tc++) {
270 fx2->trigger_mask[stage] |= probe_bit;
272 fx2->trigger_value[stage] |= probe_bit;
274 if (stage > NUM_TRIGGER_STAGES)
281 * We didn't configure any triggers, make sure acquisition
282 * doesn't wait for any.
284 fx2->trigger_stage = TRIGGER_FIRED;
286 fx2->trigger_stage = 0;
291 static struct fx2_device *fx2_device_new(void)
293 struct fx2_device *fx2;
295 if (!(fx2 = g_try_malloc0(sizeof(struct fx2_device)))) {
296 sr_err("saleae: %s: fx2 malloc failed", __func__);
299 fx2->trigger_stage = TRIGGER_FIRED;
310 static int hw_init(const char *deviceinfo)
312 struct sr_device_instance *sdi;
313 struct libusb_device_descriptor des;
314 struct fx2_profile *fx2_prof;
315 struct fx2_device *fx2;
316 libusb_device **devlist;
317 int err, devcnt, i, j;
319 /* Avoid compiler warnings. */
322 if (libusb_init(&usb_context) != 0) {
323 sr_warn("Failed to initialize USB.");
327 /* Find all Saleae Logic devices and upload firmware to all of them. */
329 libusb_get_device_list(usb_context, &devlist);
330 for (i = 0; devlist[i]; i++) {
332 err = libusb_get_device_descriptor(devlist[i], &des);
334 sr_warn("failed to get device descriptor: %d", err);
338 for (j = 0; supported_fx2[j].orig_vid; j++) {
339 if (des.idVendor == supported_fx2[j].orig_vid
340 && des.idProduct == supported_fx2[j].orig_pid) {
341 fx2_prof = &supported_fx2[j];
346 /* not a supported VID/PID */
349 sdi = sr_device_instance_new(devcnt, SR_ST_INITIALIZING,
350 fx2_prof->vendor, fx2_prof->model, fx2_prof->model_version);
353 fx2 = fx2_device_new();
354 fx2->profile = fx2_prof;
356 device_instances = g_slist_append(device_instances, sdi);
358 if (check_conf_profile(devlist[i])) {
359 /* Already has the firmware, so fix the new address. */
360 sdi->status = SR_ST_INACTIVE;
361 fx2->usb = sr_usb_device_instance_new
362 (libusb_get_bus_number(devlist[i]),
363 libusb_get_device_address(devlist[i]), NULL);
365 if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION, FIRMWARE) == SR_OK)
366 /* Remember when the firmware on this device was updated */
367 g_get_current_time(&fx2->fw_updated);
369 sr_warn("firmware upload failed for device %d", devcnt);
370 fx2->usb = sr_usb_device_instance_new
371 (libusb_get_bus_number(devlist[i]), 0xff, NULL);
375 libusb_free_device_list(devlist, 1);
380 static int hw_opendev(int device_index)
383 struct sr_device_instance *sdi;
384 struct fx2_device *fx2;
387 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
392 * if the firmware was recently uploaded, wait up to MAX_RENUM_DELAY ms
393 * for the FX2 to renumerate
396 if (GTV_TO_MSEC(fx2->fw_updated) > 0) {
397 sr_info("saleae: waiting for device to reset");
398 /* takes at least 300ms for the FX2 to be gone from the USB bus */
401 while (timediff < MAX_RENUM_DELAY) {
402 if ((err = sl_open_device(device_index)) == SR_OK)
405 g_get_current_time(&cur_time);
406 timediff = GTV_TO_MSEC(cur_time) - GTV_TO_MSEC(fx2->fw_updated);
408 sr_info("saleae: device came back after %d ms", timediff);
410 err = sl_open_device(device_index);
414 sr_warn("unable to open device");
419 err = libusb_claim_interface(fx2->usb->devhdl, USB_INTERFACE);
421 sr_warn("Unable to claim interface: %d", err);
425 if (fx2->cur_samplerate == 0) {
426 /* Samplerate hasn't been set; default to the slowest one. */
427 if (hw_set_configuration(device_index, SR_HWCAP_SAMPLERATE,
428 &supported_samplerates[0]) == SR_ERR)
435 static int hw_closedev(int device_index)
437 struct sr_device_instance *sdi;
439 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
440 sr_err("logic: %s: sdi was NULL", __func__);
441 return SR_ERR; /* TODO: SR_ERR_ARG? */
450 static void hw_cleanup(void)
453 struct sr_device_instance *sdi;
454 struct fx2_device *fx2;
456 /* Properly close and free all devices. */
457 for (l = device_instances; l; l = l->next) {
461 sr_usb_device_instance_free(fx2->usb);
462 sr_device_instance_free(sdi);
465 g_slist_free(device_instances);
466 device_instances = NULL;
469 libusb_exit(usb_context);
473 static void *hw_get_device_info(int device_index, int device_info_id)
475 struct sr_device_instance *sdi;
476 struct fx2_device *fx2;
479 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
483 switch (device_info_id) {
487 case SR_DI_NUM_PROBES:
488 info = GINT_TO_POINTER(fx2->profile->num_probes);
490 case SR_DI_PROBE_NAMES:
493 case SR_DI_SAMPLERATES:
496 case SR_DI_TRIGGER_TYPES:
497 info = TRIGGER_TYPES;
499 case SR_DI_CUR_SAMPLERATE:
500 info = &fx2->cur_samplerate;
507 static int hw_get_status(int device_index)
509 struct sr_device_instance *sdi;
511 sdi = sr_get_device_instance(device_instances, device_index);
515 return SR_ST_NOT_FOUND;
518 static int *hw_get_capabilities(void)
523 static int set_configuration_samplerate(struct sr_device_instance *sdi,
526 struct fx2_device *fx2;
529 unsigned char buf[2];
532 for (i = 0; supported_samplerates[i]; i++) {
533 if (supported_samplerates[i] == samplerate)
536 if (supported_samplerates[i] == 0)
537 return SR_ERR_SAMPLERATE;
539 divider = (uint8_t) (48 / (samplerate / 1000000.0)) - 1;
541 sr_info("saleae: setting samplerate to %" PRIu64 " Hz (divider %d)",
542 samplerate, divider);
545 ret = libusb_bulk_transfer(fx2->usb->devhdl, 1 | LIBUSB_ENDPOINT_OUT,
546 buf, 2, &result, 500);
548 sr_warn("failed to set samplerate: %d", ret);
551 fx2->cur_samplerate = samplerate;
556 static int hw_set_configuration(int device_index, int capability, void *value)
558 struct sr_device_instance *sdi;
559 struct fx2_device *fx2;
563 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
567 if (capability == SR_HWCAP_SAMPLERATE) {
569 ret = set_configuration_samplerate(sdi, *tmp_u64);
570 } else if (capability == SR_HWCAP_PROBECONFIG) {
571 ret = configure_probes(fx2, (GSList *) value);
572 } else if (capability == SR_HWCAP_LIMIT_SAMPLES) {
574 fx2->limit_samples = *tmp_u64;
583 static int receive_data(int fd, int revents, void *user_data)
587 /* Avoid compiler warnings. */
592 tv.tv_sec = tv.tv_usec = 0;
593 libusb_handle_events_timeout(usb_context, &tv);
598 static void receive_transfer(struct libusb_transfer *transfer)
600 /* TODO: these statics have to move to fx2_device struct */
601 static int num_samples = 0;
602 static int empty_transfer_count = 0;
603 struct sr_datafeed_packet packet;
604 struct sr_datafeed_logic logic;
605 struct fx2_device *fx2;
606 int cur_buflen, trigger_offset, i;
607 unsigned char *cur_buf, *new_buf;
609 /* hw_stop_acquisition() is telling us to stop. */
610 if (transfer == NULL)
614 * If acquisition has already ended, just free any queued up
615 * transfer that come in.
617 if (num_samples == -1) {
619 libusb_free_transfer(transfer);
623 sr_info("saleae: receive_transfer(): status %d received %d bytes",
624 transfer->status, transfer->actual_length);
626 /* Save incoming transfer before reusing the transfer struct. */
627 cur_buf = transfer->buffer;
628 cur_buflen = transfer->actual_length;
629 fx2 = transfer->user_data;
631 /* Fire off a new request. */
632 if (!(new_buf = g_try_malloc(4096))) {
633 sr_err("saleae: %s: new_buf malloc failed", __func__);
634 // return SR_ERR_MALLOC;
638 transfer->buffer = new_buf;
639 transfer->length = 4096;
640 if (libusb_submit_transfer(transfer) != 0) {
641 /* TODO: Stop session? */
645 if (cur_buflen == 0) {
646 empty_transfer_count++;
647 if (empty_transfer_count > MAX_EMPTY_TRANSFERS) {
649 * The FX2 gave up. End the acquisition, the frontend
650 * will work out that the samplecount is short.
652 hw_stop_acquisition(-1, fx2->session_data);
656 empty_transfer_count = 0;
660 if (fx2->trigger_stage >= 0) {
661 for (i = 0; i < cur_buflen; i++) {
663 if ((cur_buf[i] & fx2->trigger_mask[fx2->trigger_stage]) == fx2->trigger_value[fx2->trigger_stage]) {
664 /* Match on this trigger stage. */
665 fx2->trigger_buffer[fx2->trigger_stage] = cur_buf[i];
666 fx2->trigger_stage++;
668 if (fx2->trigger_stage == NUM_TRIGGER_STAGES || fx2->trigger_mask[fx2->trigger_stage] == 0) {
669 /* Match on all trigger stages, we're done. */
670 trigger_offset = i + 1;
673 * TODO: Send pre-trigger buffer to session bus.
674 * Tell the frontend we hit the trigger here.
676 packet.type = SR_DF_TRIGGER;
677 packet.payload = NULL;
678 sr_session_bus(fx2->session_data, &packet);
681 * Send the samples that triggered it, since we're
682 * skipping past them.
684 packet.type = SR_DF_LOGIC;
685 packet.payload = &logic;
686 logic.length = fx2->trigger_stage;
688 logic.data = fx2->trigger_buffer;
689 sr_session_bus(fx2->session_data, &packet);
691 fx2->trigger_stage = TRIGGER_FIRED;
698 * We had a match before, but not in the next sample. However, we may
699 * have a match on this stage in the next bit -- trigger on 0001 will
700 * fail on seeing 00001, so we need to go back to stage 0 -- but at
701 * the next sample from the one that matched originally, which the
702 * counter increment at the end of the loop takes care of.
704 if (fx2->trigger_stage > 0) {
705 i -= fx2->trigger_stage;
707 i = -1; /* Oops, went back past this buffer. */
708 /* Reset trigger stage. */
709 fx2->trigger_stage = 0;
714 if (fx2->trigger_stage == TRIGGER_FIRED) {
715 /* Send the incoming transfer to the session bus. */
716 packet.type = SR_DF_LOGIC;
717 packet.payload = &logic;
718 logic.length = cur_buflen - trigger_offset;
720 logic.data = cur_buf + trigger_offset;
721 sr_session_bus(fx2->session_data, &packet);
724 num_samples += cur_buflen;
725 if (fx2->limit_samples && (unsigned int) num_samples > fx2->limit_samples) {
726 hw_stop_acquisition(-1, fx2->session_data);
730 * TODO: Buffer pre-trigger data in capture
731 * ratio-sized buffer.
736 static int hw_start_acquisition(int device_index, gpointer session_data)
738 struct sr_device_instance *sdi;
739 struct sr_datafeed_packet *packet;
740 struct sr_datafeed_header *header;
741 struct fx2_device *fx2;
742 struct libusb_transfer *transfer;
743 const struct libusb_pollfd **lupfd;
747 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
750 fx2->session_data = session_data;
752 if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
753 sr_err("saleae: %s: packet malloc failed", __func__);
754 return SR_ERR_MALLOC;
757 if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
758 sr_err("saleae: %s: header malloc failed", __func__);
759 return SR_ERR_MALLOC;
762 /* Start with 2K transfer, subsequently increased to 4K. */
764 for (i = 0; i < NUM_SIMUL_TRANSFERS; i++) {
765 if (!(buf = g_try_malloc(size))) {
766 sr_err("saleae: %s: buf malloc failed", __func__);
767 return SR_ERR_MALLOC;
769 transfer = libusb_alloc_transfer(0);
770 libusb_fill_bulk_transfer(transfer, fx2->usb->devhdl,
771 2 | LIBUSB_ENDPOINT_IN, buf, size,
772 receive_transfer, fx2, 40);
773 if (libusb_submit_transfer(transfer) != 0) {
774 /* TODO: Free them all. */
775 libusb_free_transfer(transfer);
782 lupfd = libusb_get_pollfds(usb_context);
783 for (i = 0; lupfd[i]; i++)
784 sr_source_add(lupfd[i]->fd, lupfd[i]->events, 40, receive_data,
788 packet->type = SR_DF_HEADER;
789 packet->payload = header;
790 header->feed_version = 1;
791 gettimeofday(&header->starttime, NULL);
792 header->samplerate = fx2->cur_samplerate;
793 header->num_logic_probes = fx2->profile->num_probes;
794 header->num_analog_probes = 0;
795 sr_session_bus(session_data, packet);
802 /* This stops acquisition on ALL devices, ignoring device_index. */
803 static void hw_stop_acquisition(int device_index, gpointer session_data)
805 struct sr_datafeed_packet packet;
807 /* Avoid compiler warnings. */
810 packet.type = SR_DF_END;
811 sr_session_bus(session_data, &packet);
813 receive_transfer(NULL);
815 /* TODO: Need to cancel and free any queued up transfers. */
818 SR_PRIV struct sr_device_plugin saleae_logic_plugin_info = {
819 .name = "saleae-logic",
820 .longname = "Saleae Logic",
823 .cleanup = hw_cleanup,
824 .opendev = hw_opendev,
825 .closedev = hw_closedev,
826 .get_device_info = hw_get_device_info,
827 .get_status = hw_get_status,
828 .get_capabilities = hw_get_capabilities,
829 .set_configuration = hw_set_configuration,
830 .start_acquisition = hw_start_acquisition,
831 .stop_acquisition = hw_stop_acquisition,