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 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 new_saleae_logic_firmware = 0;
96 static int hw_set_configuration(int device_index, int capability, void *value);
97 static int hw_stop_acquisition(int device_index, gpointer session_device_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_device(int device_index)
170 libusb_device **devlist;
171 struct libusb_device_descriptor des;
172 struct sr_device_instance *sdi;
173 struct fx2_device *fx2;
176 if (!(sdi = sr_get_device_instance(device_instances, device_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("failed to get device descriptor: %d", err);
192 if (des.idVendor != fx2->profile->fw_vid || des.idProduct != fx2->profile->fw_pid)
195 if (sdi->status == SR_ST_INITIALIZING) {
196 if (skip != device_index) {
197 /* Skip devices of this type that aren't the one we want. */
201 } else if (sdi->status == SR_ST_INACTIVE) {
203 * This device is fully enumerated, so we need to find this
204 * device by vendor, product, bus and address.
206 if (libusb_get_bus_number(devlist[i]) != fx2->usb->bus
207 || libusb_get_device_address(devlist[i]) != fx2->usb->address)
208 /* this is not the one */
212 if (!(err = libusb_open(devlist[i], &fx2->usb->devhdl))) {
213 if (fx2->usb->address == 0xff)
215 * first time we touch this device after firmware upload,
216 * so we don't know the address yet.
218 fx2->usb->address = libusb_get_device_address(devlist[i]);
220 sdi->status = SR_ST_ACTIVE;
221 sr_info("saleae: opened device %d on %d.%d interface %d",
222 sdi->index, fx2->usb->bus,
223 fx2->usb->address, USB_INTERFACE);
225 sr_err("failed to open device: %d", err);
228 /* if we made it here, we handled the device one way or another */
231 libusb_free_device_list(devlist, 1);
233 if (sdi->status != SR_ST_ACTIVE)
239 static void close_device(struct sr_device_instance *sdi)
241 struct fx2_device *fx2;
245 if (fx2->usb->devhdl == NULL)
248 sr_info("saleae: closing device %d on %d.%d interface %d", sdi->index,
249 fx2->usb->bus, fx2->usb->address, USB_INTERFACE);
250 libusb_release_interface(fx2->usb->devhdl, USB_INTERFACE);
251 libusb_close(fx2->usb->devhdl);
252 fx2->usb->devhdl = NULL;
253 sdi->status = SR_ST_INACTIVE;
256 static int configure_probes(struct fx2_device *fx2, GSList *probes)
258 struct sr_probe *probe;
260 int probe_bit, stage, i;
264 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
265 fx2->trigger_mask[i] = 0;
266 fx2->trigger_value[i] = 0;
270 for (l = probes; l; l = l->next) {
271 probe = (struct sr_probe *)l->data;
272 if (probe->enabled == FALSE)
274 probe_bit = 1 << (probe->index - 1);
275 fx2->probe_mask |= probe_bit;
276 if (!(probe->trigger))
280 for (tc = probe->trigger; *tc; tc++) {
281 fx2->trigger_mask[stage] |= probe_bit;
283 fx2->trigger_value[stage] |= probe_bit;
285 if (stage > NUM_TRIGGER_STAGES)
292 * We didn't configure any triggers, make sure acquisition
293 * doesn't wait for any.
295 fx2->trigger_stage = TRIGGER_FIRED;
297 fx2->trigger_stage = 0;
302 static struct fx2_device *fx2_device_new(void)
304 struct fx2_device *fx2;
306 if (!(fx2 = g_try_malloc0(sizeof(struct fx2_device)))) {
307 sr_err("saleae: %s: fx2 malloc failed", __func__);
310 fx2->trigger_stage = TRIGGER_FIRED;
321 static int hw_init(const char *deviceinfo)
323 struct sr_device_instance *sdi;
324 struct libusb_device_descriptor des;
325 struct fx2_profile *fx2_prof;
326 struct fx2_device *fx2;
327 libusb_device **devlist;
328 int err, devcnt, i, j;
330 /* Avoid compiler warnings. */
333 if (libusb_init(&usb_context) != 0) {
334 sr_err("Failed to initialize USB.");
338 /* Find all Saleae Logic devices and upload firmware to all of them. */
340 libusb_get_device_list(usb_context, &devlist);
341 for (i = 0; devlist[i]; i++) {
343 err = libusb_get_device_descriptor(devlist[i], &des);
345 sr_err("failed to get device descriptor: %d", err);
349 for (j = 0; supported_fx2[j].orig_vid; j++) {
350 if (des.idVendor == supported_fx2[j].orig_vid
351 && des.idProduct == supported_fx2[j].orig_pid) {
352 fx2_prof = &supported_fx2[j];
357 /* not a supported VID/PID */
360 sdi = sr_device_instance_new(devcnt, SR_ST_INITIALIZING,
361 fx2_prof->vendor, fx2_prof->model, fx2_prof->model_version);
364 fx2 = fx2_device_new();
365 fx2->profile = fx2_prof;
367 device_instances = g_slist_append(device_instances, sdi);
369 if (check_conf_profile(devlist[i])) {
370 /* Already has the firmware, so fix the new address. */
371 sr_dbg("Found a Saleae Logic with %s firmware.",
372 new_saleae_logic_firmware ? "new" : "old");
373 sdi->status = SR_ST_INACTIVE;
374 fx2->usb = sr_usb_device_instance_new
375 (libusb_get_bus_number(devlist[i]),
376 libusb_get_device_address(devlist[i]), NULL);
378 if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION, FIRMWARE) == SR_OK)
379 /* Remember when the firmware on this device was updated */
380 g_get_current_time(&fx2->fw_updated);
382 sr_err("firmware upload failed for device %d", devcnt);
383 fx2->usb = sr_usb_device_instance_new
384 (libusb_get_bus_number(devlist[i]), 0xff, NULL);
388 libusb_free_device_list(devlist, 1);
393 static int hw_opendev(int device_index)
396 struct sr_device_instance *sdi;
397 struct fx2_device *fx2;
400 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
405 * if the firmware was recently uploaded, wait up to MAX_RENUM_DELAY ms
406 * for the FX2 to renumerate
409 if (GTV_TO_MSEC(fx2->fw_updated) > 0) {
410 sr_info("saleae: waiting for device to reset");
411 /* takes at least 300ms for the FX2 to be gone from the USB bus */
414 while (timediff < MAX_RENUM_DELAY) {
415 if ((err = sl_open_device(device_index)) == SR_OK)
418 g_get_current_time(&cur_time);
419 timediff = GTV_TO_MSEC(cur_time) - GTV_TO_MSEC(fx2->fw_updated);
421 sr_info("saleae: device came back after %d ms", timediff);
423 err = sl_open_device(device_index);
427 sr_err("unable to open device");
432 err = libusb_claim_interface(fx2->usb->devhdl, USB_INTERFACE);
434 sr_err("Unable to claim interface: %d", err);
438 if (fx2->cur_samplerate == 0) {
439 /* Samplerate hasn't been set; default to the slowest one. */
440 if (hw_set_configuration(device_index, SR_HWCAP_SAMPLERATE,
441 &supported_samplerates[0]) == SR_ERR)
448 static int hw_closedev(int device_index)
450 struct sr_device_instance *sdi;
452 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
453 sr_err("logic: %s: sdi was NULL", __func__);
454 return SR_ERR; /* TODO: SR_ERR_ARG? */
463 static int hw_cleanup(void)
466 struct sr_device_instance *sdi;
467 struct fx2_device *fx2;
470 /* Properly close and free all devices. */
471 for (l = device_instances; l; l = l->next) {
472 if (!(sdi = l->data)) {
473 /* Log error, but continue cleaning up the rest. */
474 sr_err("fx2: %s: sdi was NULL, continuing", __func__);
478 if (!(fx2 = sdi->priv)) {
479 /* Log error, but continue cleaning up the rest. */
480 sr_err("fx2: %s: sdi->priv was NULL, continuing",
486 sr_usb_device_instance_free(fx2->usb);
487 sr_device_instance_free(sdi);
490 g_slist_free(device_instances);
491 device_instances = NULL;
494 libusb_exit(usb_context);
500 static void *hw_get_device_info(int device_index, int device_info_id)
502 struct sr_device_instance *sdi;
503 struct fx2_device *fx2;
506 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
510 switch (device_info_id) {
514 case SR_DI_NUM_PROBES:
515 info = GINT_TO_POINTER(fx2->profile->num_probes);
517 case SR_DI_PROBE_NAMES:
520 case SR_DI_SAMPLERATES:
523 case SR_DI_TRIGGER_TYPES:
524 info = TRIGGER_TYPES;
526 case SR_DI_CUR_SAMPLERATE:
527 info = &fx2->cur_samplerate;
534 static int hw_get_status(int device_index)
536 struct sr_device_instance *sdi;
538 sdi = sr_get_device_instance(device_instances, device_index);
542 return SR_ST_NOT_FOUND;
545 static int *hw_get_capabilities(void)
550 static uint8_t new_firmware_divider_value(uint64_t samplerate)
552 switch (samplerate) {
585 /* Shouldn't happen. */
586 sr_err("saleae: %s: Invalid samplerate %" PRIu64 "",
587 __func__, samplerate);
591 static int set_configuration_samplerate(struct sr_device_instance *sdi,
594 struct fx2_device *fx2;
597 unsigned char buf[2];
600 for (i = 0; supported_samplerates[i]; i++) {
601 if (supported_samplerates[i] == samplerate)
604 if (supported_samplerates[i] == 0)
605 return SR_ERR_SAMPLERATE;
607 if (new_saleae_logic_firmware)
608 divider = new_firmware_divider_value(samplerate);
610 divider = (uint8_t) (48 / (samplerate / 1000000.0)) - 1;
612 sr_info("saleae: setting samplerate to %" PRIu64 " Hz (divider %d)",
613 samplerate, divider);
615 buf[0] = (new_saleae_logic_firmware) ? 0xd5 : 0x01;
617 ret = libusb_bulk_transfer(fx2->usb->devhdl, 1 | LIBUSB_ENDPOINT_OUT,
618 buf, 2, &result, 500);
620 sr_err("failed to set samplerate: %d", ret);
623 fx2->cur_samplerate = samplerate;
628 static int hw_set_configuration(int device_index, int capability, void *value)
630 struct sr_device_instance *sdi;
631 struct fx2_device *fx2;
635 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
639 if (capability == SR_HWCAP_SAMPLERATE) {
641 ret = set_configuration_samplerate(sdi, *tmp_u64);
642 } else if (capability == SR_HWCAP_PROBECONFIG) {
643 ret = configure_probes(fx2, (GSList *) value);
644 } else if (capability == SR_HWCAP_LIMIT_SAMPLES) {
646 fx2->limit_samples = *tmp_u64;
655 static int receive_data(int fd, int revents, void *user_data)
659 /* Avoid compiler warnings. */
664 tv.tv_sec = tv.tv_usec = 0;
665 libusb_handle_events_timeout(usb_context, &tv);
670 static void receive_transfer(struct libusb_transfer *transfer)
672 /* TODO: these statics have to move to fx2_device struct */
673 static int num_samples = 0;
674 static int empty_transfer_count = 0;
675 struct sr_datafeed_packet packet;
676 struct sr_datafeed_logic logic;
677 struct fx2_device *fx2;
678 int cur_buflen, trigger_offset, i;
679 unsigned char *cur_buf, *new_buf;
681 /* hw_stop_acquisition() is telling us to stop. */
682 if (transfer == NULL)
686 * If acquisition has already ended, just free any queued up
687 * transfer that come in.
689 if (num_samples == -1) {
691 libusb_free_transfer(transfer);
695 sr_info("saleae: receive_transfer(): status %d received %d bytes",
696 transfer->status, transfer->actual_length);
698 /* Save incoming transfer before reusing the transfer struct. */
699 cur_buf = transfer->buffer;
700 cur_buflen = transfer->actual_length;
701 fx2 = transfer->user_data;
703 /* Fire off a new request. */
704 if (!(new_buf = g_try_malloc(4096))) {
705 sr_err("saleae: %s: new_buf malloc failed", __func__);
706 return; /* TODO: SR_ERR_MALLOC */
709 transfer->buffer = new_buf;
710 transfer->length = 4096;
711 if (libusb_submit_transfer(transfer) != 0) {
712 /* TODO: Stop session? */
716 if (cur_buflen == 0) {
717 empty_transfer_count++;
718 if (empty_transfer_count > MAX_EMPTY_TRANSFERS) {
720 * The FX2 gave up. End the acquisition, the frontend
721 * will work out that the samplecount is short.
723 hw_stop_acquisition(-1, fx2->session_data);
727 empty_transfer_count = 0;
731 if (fx2->trigger_stage >= 0) {
732 for (i = 0; i < cur_buflen; i++) {
734 if ((cur_buf[i] & fx2->trigger_mask[fx2->trigger_stage]) == fx2->trigger_value[fx2->trigger_stage]) {
735 /* Match on this trigger stage. */
736 fx2->trigger_buffer[fx2->trigger_stage] = cur_buf[i];
737 fx2->trigger_stage++;
739 if (fx2->trigger_stage == NUM_TRIGGER_STAGES || fx2->trigger_mask[fx2->trigger_stage] == 0) {
740 /* Match on all trigger stages, we're done. */
741 trigger_offset = i + 1;
744 * TODO: Send pre-trigger buffer to session bus.
745 * Tell the frontend we hit the trigger here.
747 packet.type = SR_DF_TRIGGER;
748 packet.payload = NULL;
749 sr_session_bus(fx2->session_data, &packet);
752 * Send the samples that triggered it, since we're
753 * skipping past them.
755 packet.type = SR_DF_LOGIC;
756 packet.payload = &logic;
757 logic.length = fx2->trigger_stage;
759 logic.data = fx2->trigger_buffer;
760 sr_session_bus(fx2->session_data, &packet);
762 fx2->trigger_stage = TRIGGER_FIRED;
769 * We had a match before, but not in the next sample. However, we may
770 * have a match on this stage in the next bit -- trigger on 0001 will
771 * fail on seeing 00001, so we need to go back to stage 0 -- but at
772 * the next sample from the one that matched originally, which the
773 * counter increment at the end of the loop takes care of.
775 if (fx2->trigger_stage > 0) {
776 i -= fx2->trigger_stage;
778 i = -1; /* Oops, went back past this buffer. */
779 /* Reset trigger stage. */
780 fx2->trigger_stage = 0;
785 if (fx2->trigger_stage == TRIGGER_FIRED) {
786 /* Send the incoming transfer to the session bus. */
787 packet.type = SR_DF_LOGIC;
788 packet.payload = &logic;
789 logic.length = cur_buflen - trigger_offset;
791 logic.data = cur_buf + trigger_offset;
792 sr_session_bus(fx2->session_data, &packet);
795 num_samples += cur_buflen;
796 if (fx2->limit_samples && (unsigned int) num_samples > fx2->limit_samples) {
797 hw_stop_acquisition(-1, fx2->session_data);
801 * TODO: Buffer pre-trigger data in capture
802 * ratio-sized buffer.
807 static int hw_start_acquisition(int device_index, gpointer session_data)
809 struct sr_device_instance *sdi;
810 struct sr_datafeed_packet *packet;
811 struct sr_datafeed_header *header;
812 struct fx2_device *fx2;
813 struct libusb_transfer *transfer;
814 const struct libusb_pollfd **lupfd;
818 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
821 fx2->session_data = session_data;
823 if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
824 sr_err("saleae: %s: packet malloc failed", __func__);
825 return SR_ERR_MALLOC;
828 if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
829 sr_err("saleae: %s: header malloc failed", __func__);
830 return SR_ERR_MALLOC;
833 /* Start with 2K transfer, subsequently increased to 4K. */
835 for (i = 0; i < NUM_SIMUL_TRANSFERS; i++) {
836 if (!(buf = g_try_malloc(size))) {
837 sr_err("saleae: %s: buf malloc failed", __func__);
838 return SR_ERR_MALLOC;
840 transfer = libusb_alloc_transfer(0);
841 libusb_fill_bulk_transfer(transfer, fx2->usb->devhdl,
842 2 | LIBUSB_ENDPOINT_IN, buf, size,
843 receive_transfer, fx2, 40);
844 if (libusb_submit_transfer(transfer) != 0) {
845 /* TODO: Free them all. */
846 libusb_free_transfer(transfer);
853 lupfd = libusb_get_pollfds(usb_context);
854 for (i = 0; lupfd[i]; i++)
855 sr_source_add(lupfd[i]->fd, lupfd[i]->events, 40, receive_data,
857 free(lupfd); /* NOT g_free()! */
859 packet->type = SR_DF_HEADER;
860 packet->payload = header;
861 header->feed_version = 1;
862 gettimeofday(&header->starttime, NULL);
863 header->samplerate = fx2->cur_samplerate;
864 header->num_logic_probes = fx2->profile->num_probes;
865 sr_session_bus(session_data, packet);
872 /* This stops acquisition on ALL devices, ignoring device_index. */
873 static int hw_stop_acquisition(int device_index, gpointer session_data)
875 struct sr_datafeed_packet packet;
877 /* Avoid compiler warnings. */
880 packet.type = SR_DF_END;
881 sr_session_bus(session_data, &packet);
883 receive_transfer(NULL);
885 /* TODO: Need to cancel and free any queued up transfers. */
890 SR_PRIV struct sr_device_plugin saleae_logic_plugin_info = {
891 .name = "saleae-logic",
892 .longname = "Saleae Logic",
895 .cleanup = hw_cleanup,
896 .opendev = hw_opendev,
897 .closedev = hw_closedev,
898 .get_device_info = hw_get_device_info,
899 .get_status = hw_get_status,
900 .get_capabilities = hw_get_capabilities,
901 .set_configuration = hw_set_configuration,
902 .start_acquisition = hw_start_acquisition,
903 .stop_acquisition = hw_stop_acquisition,