2 * This file is part of the sigrok project.
4 * Copyright (C) 2010 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>
30 #define USB_INTERFACE 0
31 #define USB_CONFIGURATION 1
32 #define NUM_TRIGGER_STAGES 4
33 #define TRIGGER_TYPES "01"
34 #define FIRMWARE FIRMWARE_DIR "/saleae-logic.fw"
35 #define GTV_TO_MSEC(gtv) (gtv.tv_sec * 1000 + gtv.tv_usec / 1000)
38 #define MAX_RENUM_DELAY 3000
39 #define NUM_SIMUL_TRANSFERS 10
40 #define MAX_EMPTY_TRANSFERS (NUM_SIMUL_TRANSFERS * 2)
42 /* Software trigger implementation: positive values indicate trigger stage. */
43 #define TRIGGER_FIRED -1
46 /* VID/PID when first found */
49 /* VID/PID after firmware upload */
58 static struct fx2_device supported_fx2[] = {
60 { 0x0925, 0x3881, 0x0925, 0x3881, "Saleae", "Logic", NULL, 8 },
61 /* default Cypress FX2 without EEPROM */
62 { 0x04b4, 0x8613, 0x0925, 0x3881, "Cypress", "FX2", NULL, 16 },
63 { 0, 0, 0, 0, NULL, NULL, NULL, 0 }
66 /* There is only one model Saleae Logic, and this is what it supports: */
67 static int capabilities[] = {
68 SR_HWCAP_LOGIC_ANALYZER,
71 /* These are really implemented in the driver, not the hardware. */
72 SR_HWCAP_LIMIT_SAMPLES,
77 /* List of struct sr_device_instance, maintained by opendev()/closedev(). */
78 static GSList *device_instances = NULL;
81 * Since we can't keep track of a Saleae Logic device after upgrading the
82 * firmware -- it re-enumerates into a different device address after the
83 * upgrade -- this is like a global lock. No device will open until a proper
84 * delay after the last device was upgraded.
86 static GTimeVal fw_updated = { 0, 0 };
88 static libusb_context *usb_context = NULL;
90 static uint64_t supported_samplerates[] = {
104 static struct sr_samplerates samplerates = {
108 supported_samplerates,
111 /* TODO: All of these should go in a device-specific struct. */
112 static uint64_t cur_samplerate = 0;
113 static uint64_t limit_samples = 0;
114 static uint8_t probe_mask = 0;
115 static uint8_t trigger_mask[NUM_TRIGGER_STAGES] = { 0 };
116 static uint8_t trigger_value[NUM_TRIGGER_STAGES] = { 0 };
117 static uint8_t trigger_buffer[NUM_TRIGGER_STAGES] = { 0 };
118 static struct fx2_device *fx2 = NULL;
119 static int trigger_stage = TRIGGER_FIRED;
121 static int hw_set_configuration(int device_index, int capability, void *value);
122 static void hw_stop_acquisition(int device_index, gpointer session_device_id);
125 * Check the USB configuration to determine if this is a Saleae Logic.
127 * @return 1 if the device's configuration profile match the Logic firmware's
128 * configuration, 0 otherwise.
130 static int check_conf_profile(libusb_device *dev)
132 struct libusb_device_descriptor des;
133 struct libusb_config_descriptor *conf_dsc = NULL;
134 const struct libusb_interface_descriptor *intf_dsc;
138 /* Assume it's not a Saleae Logic unless proven wrong. */
141 if (libusb_get_device_descriptor(dev, &des) != 0)
144 if (des.bNumConfigurations != 1)
145 /* Need exactly 1 configuration. */
148 if (libusb_get_config_descriptor(dev, 0, &conf_dsc) != 0)
151 if (conf_dsc->bNumInterfaces != 1)
152 /* Need exactly 1 interface. */
155 if (conf_dsc->interface[0].num_altsetting != 1)
156 /* Need just one alternate setting. */
159 intf_dsc = &(conf_dsc->interface[0].altsetting[0]);
160 if (intf_dsc->bNumEndpoints != 2)
161 /* Need 2 endpoints. */
164 if ((intf_dsc->endpoint[0].bEndpointAddress & 0x8f) !=
165 (1 | LIBUSB_ENDPOINT_OUT))
166 /* First endpoint should be 1 (outbound). */
169 if ((intf_dsc->endpoint[1].bEndpointAddress & 0x8f) !=
170 (2 | LIBUSB_ENDPOINT_IN))
171 /* First endpoint should be 2 (inbound). */
174 /* If we made it here, it must be a Saleae Logic. */
179 libusb_free_config_descriptor(conf_dsc);
184 static struct sr_device_instance *sl_open_device(int device_index)
186 libusb_device **devlist;
187 struct libusb_device_descriptor des;
188 struct sr_device_instance *sdi;
191 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
194 if (sdi->status == SR_ST_ACTIVE)
199 libusb_get_device_list(usb_context, &devlist);
200 for (i = 0; devlist[i]; i++) {
201 if ((err = libusb_get_device_descriptor(devlist[i], &des))) {
202 sr_warn("failed to get device descriptor: %d", err);
206 if (des.idVendor != fx2->fw_vid || des.idProduct != fx2->fw_pid)
209 if (sdi->status == SR_ST_INITIALIZING) {
210 if (skip != device_index) {
211 /* Skip devices of this type that aren't the one we want. */
215 } else if (sdi->status == SR_ST_INACTIVE) {
217 * This device is fully enumerated, so we need to find this
218 * device by vendor, product, bus and address.
220 if (libusb_get_bus_number(devlist[i]) != sdi->usb->bus
221 || libusb_get_device_address(devlist[i]) != sdi->usb->address)
222 /* this is not the one */
226 if (!(err = libusb_open(devlist[i], &sdi->usb->devhdl))) {
227 if (sdi->usb->address == 0xff)
229 * first time we touch this device after firmware upload,
230 * so we don't know the address yet.
232 sdi->usb->address = libusb_get_device_address(devlist[i]);
234 sdi->status = SR_ST_ACTIVE;
235 sr_info("saleae: opened device %d on %d.%d interface %d",
236 sdi->index, sdi->usb->bus,
237 sdi->usb->address, USB_INTERFACE);
239 sr_warn("failed to open device: %d", err);
243 libusb_free_device_list(devlist, 1);
245 if (sdi && sdi->status != SR_ST_ACTIVE)
251 static int upload_firmware(libusb_device *dev)
255 ret = ezusb_upload_firmware(dev, USB_CONFIGURATION, FIRMWARE);
259 /* Remember when the last firmware update was done. */
260 g_get_current_time(&fw_updated);
265 static void close_device(struct sr_device_instance *sdi)
267 if (sdi->usb->devhdl == NULL)
270 sr_info("saleae: closing device %d on %d.%d interface %d", sdi->index,
271 sdi->usb->bus, sdi->usb->address, USB_INTERFACE);
272 libusb_release_interface(sdi->usb->devhdl, USB_INTERFACE);
273 libusb_close(sdi->usb->devhdl);
274 sdi->usb->devhdl = NULL;
275 sdi->status = SR_ST_INACTIVE;
278 static int configure_probes(GSList *probes)
280 struct sr_probe *probe;
282 int probe_bit, stage, i;
286 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
288 trigger_value[i] = 0;
292 for (l = probes; l; l = l->next) {
293 probe = (struct sr_probe *)l->data;
294 if (probe->enabled == FALSE)
296 probe_bit = 1 << (probe->index - 1);
297 probe_mask |= probe_bit;
298 if (!(probe->trigger))
302 for (tc = probe->trigger; *tc; tc++) {
303 trigger_mask[stage] |= probe_bit;
305 trigger_value[stage] |= probe_bit;
307 if (stage > NUM_TRIGGER_STAGES)
314 * We didn't configure any triggers, make sure acquisition
315 * doesn't wait for any.
317 trigger_stage = TRIGGER_FIRED;
328 static int hw_init(const char *deviceinfo)
330 struct sr_device_instance *sdi;
331 struct libusb_device_descriptor des;
332 libusb_device **devlist;
333 int err, devcnt, i, j;
335 /* Avoid compiler warnings. */
336 deviceinfo = deviceinfo;
338 if (libusb_init(&usb_context) != 0) {
339 sr_warn("Failed to initialize USB.");
343 /* Find all Saleae Logic devices and upload firmware to all of them. */
346 libusb_get_device_list(usb_context, &devlist);
347 for (i = 0; devlist[i]; i++) {
348 err = libusb_get_device_descriptor(devlist[i], &des);
350 sr_warn("failed to get device descriptor: %d", err);
354 for (j = 0; supported_fx2[j].orig_vid; j++) {
355 if (des.idVendor == supported_fx2[j].orig_vid
356 && des.idProduct == supported_fx2[j].orig_pid) {
357 fx2 = &supported_fx2[j];
362 /* not a supported VID/PID */
365 sdi = sr_device_instance_new(devcnt, SR_ST_INITIALIZING,
366 fx2->vendor, fx2->model, fx2->model_version);
369 device_instances = g_slist_append(device_instances, sdi);
371 if (check_conf_profile(devlist[i]) == 0) {
373 * Continue on the off chance that the device is in a
374 * working state. TODO: Could maybe try a USB reset,
375 * or uploading the firmware again.
377 if (upload_firmware(devlist[i]) > 0)
378 sr_warn("firmware upload failed for device %d",
381 sdi->usb = sr_usb_device_instance_new
382 (libusb_get_bus_number(devlist[i]), 0xff, NULL);
384 /* Already has the firmware, so fix the new address. */
385 sdi->status = SR_ST_INACTIVE;
386 sdi->usb = sr_usb_device_instance_new
387 (libusb_get_bus_number(devlist[i]),
388 libusb_get_device_address(devlist[i]), NULL);
392 /* not supporting multiple FX2s in this driver yet */
395 libusb_free_device_list(devlist, 1);
400 static int hw_opendev(int device_index)
403 struct sr_device_instance *sdi;
407 * if the firmware was recently uploaded, wait up to MAX_RENUM_DELAY ms
408 * for the FX2 to renumerate
411 if (fw_updated.tv_sec > 0) {
412 sr_info("saleae: waiting for device to reset");
413 /* takes at least 300ms for the FX2 to be gone from the USB bus */
416 while (timediff < MAX_RENUM_DELAY) {
417 if ((sdi = sl_open_device(device_index)))
420 g_get_current_time(&cur_time);
421 timediff = GTV_TO_MSEC(cur_time) - GTV_TO_MSEC(fw_updated);
423 sr_info("saleae: device came back after %d ms", timediff);
425 sdi = sl_open_device(device_index);
429 sr_warn("unable to open device");
433 err = libusb_claim_interface(sdi->usb->devhdl, USB_INTERFACE);
435 sr_warn("Unable to claim interface: %d", err);
439 if (cur_samplerate == 0) {
440 /* Samplerate hasn't been set; default to the slowest one. */
441 if (hw_set_configuration(device_index, SR_HWCAP_SAMPLERATE,
442 &supported_samplerates[0]) == SR_ERR)
449 static int hw_closedev(int device_index)
451 struct sr_device_instance *sdi;
453 if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
454 sr_err("logic: %s: sdi was NULL", __func__);
455 return SR_ERR; /* TODO: SR_ERR_ARG? */
464 static void hw_cleanup(void)
468 /* Properly close all devices... */
469 for (l = device_instances; l; l = l->next)
470 close_device((struct sr_device_instance *)l->data);
472 /* ...and free all their memory. */
473 for (l = device_instances; l; l = l->next)
475 g_slist_free(device_instances);
476 device_instances = NULL;
479 libusb_exit(usb_context);
483 static void *hw_get_device_info(int device_index, int device_info_id)
485 struct sr_device_instance *sdi;
488 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
491 switch (device_info_id) {
495 case SR_DI_NUM_PROBES:
496 info = GINT_TO_POINTER(fx2->num_probes);
498 case SR_DI_SAMPLERATES:
501 case SR_DI_TRIGGER_TYPES:
502 info = TRIGGER_TYPES;
504 case SR_DI_CUR_SAMPLERATE:
505 info = &cur_samplerate;
512 static int hw_get_status(int device_index)
514 struct sr_device_instance *sdi;
516 sdi = sr_get_device_instance(device_instances, device_index);
520 return SR_ST_NOT_FOUND;
523 static int *hw_get_capabilities(void)
528 static int set_configuration_samplerate(struct sr_device_instance *sdi,
533 unsigned char buf[2];
535 for (i = 0; supported_samplerates[i]; i++) {
536 if (supported_samplerates[i] == samplerate)
539 if (supported_samplerates[i] == 0)
540 return SR_ERR_SAMPLERATE;
542 divider = (uint8_t) (48 / (samplerate / 1000000.0)) - 1;
544 sr_info("saleae: setting samplerate to %" PRIu64 " Hz (divider %d)",
545 samplerate, divider);
548 ret = libusb_bulk_transfer(sdi->usb->devhdl, 1 | LIBUSB_ENDPOINT_OUT,
549 buf, 2, &result, 500);
551 sr_warn("failed to set samplerate: %d", ret);
554 cur_samplerate = samplerate;
559 static int hw_set_configuration(int device_index, int capability, void *value)
561 struct sr_device_instance *sdi;
565 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
568 if (capability == SR_HWCAP_SAMPLERATE) {
570 ret = set_configuration_samplerate(sdi, *tmp_u64);
571 } else if (capability == SR_HWCAP_PROBECONFIG) {
572 ret = configure_probes((GSList *) value);
573 } else if (capability == SR_HWCAP_LIMIT_SAMPLES) {
575 limit_samples = *tmp_u64;
584 static int receive_data(int fd, int revents, void *user_data)
588 /* Avoid compiler warnings. */
591 user_data = user_data;
593 tv.tv_sec = tv.tv_usec = 0;
594 libusb_handle_events_timeout(usb_context, &tv);
599 void receive_transfer(struct libusb_transfer *transfer)
601 static int num_samples = 0;
602 static int empty_transfer_count = 0;
603 struct sr_datafeed_packet packet;
605 int cur_buflen, trigger_offset, i;
606 unsigned char *cur_buf, *new_buf;
608 /* hw_stop_acquisition() is telling us to stop. */
609 if (transfer == NULL)
613 * If acquisition has already ended, just free any queued up
614 * transfer that come in.
616 if (num_samples == -1) {
618 libusb_free_transfer(transfer);
622 sr_info("saleae: receive_transfer(): status %d received %d bytes",
623 transfer->status, transfer->actual_length);
625 /* Save incoming transfer before reusing the transfer struct. */
626 cur_buf = transfer->buffer;
627 cur_buflen = transfer->actual_length;
628 user_data = transfer->user_data;
630 /* Fire off a new request. */
631 if (!(new_buf = g_try_malloc(4096))) {
632 sr_err("saleae: %s: new_buf malloc failed", __func__);
633 // return SR_ERR_MALLOC;
637 transfer->buffer = new_buf;
638 transfer->length = 4096;
639 if (libusb_submit_transfer(transfer) != 0) {
640 /* TODO: Stop session? */
644 if (cur_buflen == 0) {
645 empty_transfer_count++;
646 if (empty_transfer_count > MAX_EMPTY_TRANSFERS) {
648 * The FX2 gave up. End the acquisition, the frontend
649 * will work out that the samplecount is short.
651 hw_stop_acquisition(-1, user_data);
655 empty_transfer_count = 0;
659 if (trigger_stage >= 0) {
660 for (i = 0; i < cur_buflen; i++) {
662 if ((cur_buf[i] & trigger_mask[trigger_stage]) == trigger_value[trigger_stage]) {
663 /* Match on this trigger stage. */
664 trigger_buffer[trigger_stage] = cur_buf[i];
666 if (trigger_stage == NUM_TRIGGER_STAGES || trigger_mask[trigger_stage] == 0) {
667 /* Match on all trigger stages, we're done. */
668 trigger_offset = i + 1;
671 * TODO: Send pre-trigger buffer to session bus.
672 * Tell the frontend we hit the trigger here.
674 packet.type = SR_DF_TRIGGER;
676 sr_session_bus(user_data, &packet);
679 * Send the samples that triggered it, since we're
680 * skipping past them.
682 packet.type = SR_DF_LOGIC;
683 packet.length = trigger_stage;
685 packet.payload = trigger_buffer;
686 sr_session_bus(user_data, &packet);
688 trigger_stage = TRIGGER_FIRED;
695 * We had a match before, but not in the next sample. However, we may
696 * have a match on this stage in the next bit -- trigger on 0001 will
697 * fail on seeing 00001, so we need to go back to stage 0 -- but at
698 * the next sample from the one that matched originally, which the
699 * counter increment at the end of the loop takes care of.
701 if (trigger_stage > 0) {
704 i = -1; /* Oops, went back past this buffer. */
705 /* Reset trigger stage. */
713 if (trigger_stage == TRIGGER_FIRED) {
714 /* Send the incoming transfer to the session bus. */
715 packet.type = SR_DF_LOGIC;
716 packet.length = cur_buflen - trigger_offset;
718 packet.payload = cur_buf + trigger_offset;
719 sr_session_bus(user_data, &packet);
722 num_samples += cur_buflen;
723 if (limit_samples && (unsigned int) num_samples > limit_samples) {
724 hw_stop_acquisition(-1, user_data);
728 * TODO: Buffer pre-trigger data in capture
729 * ratio-sized buffer.
734 static int hw_start_acquisition(int device_index, gpointer session_device_id)
736 struct sr_device_instance *sdi;
737 struct sr_datafeed_packet *packet;
738 struct sr_datafeed_header *header;
739 struct libusb_transfer *transfer;
740 const struct libusb_pollfd **lupfd;
744 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
747 if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
748 sr_err("saleae: %s: packet malloc failed", __func__);
749 return SR_ERR_MALLOC;
752 if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
753 sr_err("saleae: %s: header malloc failed", __func__);
754 return SR_ERR_MALLOC;
757 /* Start with 2K transfer, subsequently increased to 4K. */
759 for (i = 0; i < NUM_SIMUL_TRANSFERS; i++) {
760 if (!(buf = g_try_malloc(size))) {
761 sr_err("saleae: %s: buf malloc failed", __func__);
762 return SR_ERR_MALLOC;
764 transfer = libusb_alloc_transfer(0);
765 libusb_fill_bulk_transfer(transfer, sdi->usb->devhdl,
766 2 | LIBUSB_ENDPOINT_IN, buf, size,
767 receive_transfer, session_device_id, 40);
768 if (libusb_submit_transfer(transfer) != 0) {
769 /* TODO: Free them all. */
770 libusb_free_transfer(transfer);
777 lupfd = libusb_get_pollfds(usb_context);
778 for (i = 0; lupfd[i]; i++)
779 sr_source_add(lupfd[i]->fd, lupfd[i]->events, 40, receive_data,
783 packet->type = SR_DF_HEADER;
784 packet->length = sizeof(struct sr_datafeed_header);
785 packet->payload = (unsigned char *)header;
786 header->feed_version = 1;
787 gettimeofday(&header->starttime, NULL);
788 header->samplerate = cur_samplerate;
789 header->protocol_id = SR_PROTO_RAW;
790 header->num_logic_probes = fx2->num_probes;
791 header->num_analog_probes = 0;
792 sr_session_bus(session_device_id, packet);
799 /* This stops acquisition on ALL devices, ignoring device_index. */
800 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
802 struct sr_datafeed_packet packet;
804 /* Avoid compiler warnings. */
805 device_index = device_index;
807 packet.type = SR_DF_END;
808 sr_session_bus(session_device_id, &packet);
810 receive_transfer(NULL);
812 /* TODO: Need to cancel and free any queued up transfers. */
815 struct sr_device_plugin saleae_logic_plugin_info = {
816 .name = "saleae-logic",
817 .longname = "Saleae Logic",
820 .cleanup = hw_cleanup,
821 .opendev = hw_opendev,
822 .closedev = hw_closedev,
823 .get_device_info = hw_get_device_info,
824 .get_status = hw_get_status,
825 .get_capabilities = hw_get_capabilities,
826 .set_configuration = hw_set_configuration,
827 .start_acquisition = hw_start_acquisition,
828 .stop_acquisition = hw_stop_acquisition,