2 * This file is part of the sigrok project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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/>.
27 #include "sigrok-internal.h"
31 static const struct fx2lafw_profile supported_fx2[] = {
34 * EE Electronics ESLA201A
36 { 0x08a9, 0x0014, "CWAV", "USBee AX", NULL,
37 FIRMWARE_DIR "/fx2lafw-cwav-usbeeax.fw", 8 },
42 { 0x08a9, 0x0009, "CWAV", "USBee SX", NULL,
43 FIRMWARE_DIR "/fx2lafw-cwav-usbeesx.fw", 8 },
47 * EE Electronics ESLA100
49 * Robomotic BugLogic 3
51 { 0x0925, 0x3881, "Saleae", "Logic", NULL,
52 FIRMWARE_DIR "/fx2lafw-saleae-logic.fw", 8 },
55 * Default Cypress FX2 without EEPROM, e.g.:
57 * Braintechnology USB Interface V2.x
59 { 0x04B4, 0x8613, "Cypress", "FX2", NULL,
60 FIRMWARE_DIR "/fx2lafw-cypress-fx2.fw", 8 },
63 * Braintechnology USB-LPS
65 { 0x16d0, 0x0498, "Braintechnology", "USB-LPS", NULL,
66 FIRMWARE_DIR "/fx2lafw-braintechnology-usb-lps.fw", 8 },
68 { 0, 0, 0, 0, 0, 0, 0 }
71 static int hwcaps[] = {
72 SR_HWCAP_LOGIC_ANALYZER,
75 /* These are really implemented in the driver, not the hardware. */
76 SR_HWCAP_LIMIT_SAMPLES,
82 * TODO: Different probe_names[] for each supported device.
84 static const char *probe_names[] = {
96 static uint64_t supported_samplerates[] = {
116 static struct sr_samplerates samplerates = {
120 supported_samplerates,
123 static GSList *dev_insts = NULL;
124 static libusb_context *usb_context = NULL;
126 static int hw_dev_config_set(int dev_index, int hwcap, void *value);
127 static int hw_dev_acquisition_stop(int dev_index, void *cb_data);
130 * Check the USB configuration to determine if this is an fx2lafw device.
132 * @return TRUE if the device's configuration profile match fx2lafw
133 * configuration, FALSE otherwise.
135 static gboolean check_conf_profile(libusb_device *dev)
137 struct libusb_device_descriptor des;
138 struct libusb_device_handle *hdl;
140 unsigned char strdesc[64];
145 /* Assume the FW has not been loaded, unless proven wrong. */
146 if (libusb_get_device_descriptor(dev, &des) != 0)
149 if (libusb_open(dev, &hdl) != 0)
152 if (libusb_get_string_descriptor_ascii(hdl,
153 des.iManufacturer, strdesc, sizeof(strdesc)) < 0)
155 if (strncmp((const char *)strdesc, "sigrok", 6))
158 if (libusb_get_string_descriptor_ascii(hdl,
159 des.iProduct, strdesc, sizeof(strdesc)) < 0)
161 if (strncmp((const char *)strdesc, "fx2lafw", 7))
164 /* If we made it here, it must be an fx2lafw. */
173 static int fx2lafw_dev_open(int dev_index)
175 libusb_device **devlist;
176 struct libusb_device_descriptor des;
177 struct sr_dev_inst *sdi;
179 struct version_info vi;
183 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
187 if (sdi->status == SR_ST_ACTIVE)
192 const int device_count = libusb_get_device_list(usb_context, &devlist);
193 if (device_count < 0) {
194 sr_err("fx2lafw: Failed to retrieve device list (%d)",
199 for (i = 0; i < device_count; i++) {
200 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
201 sr_err("fx2lafw: Failed to get device descriptor: %d.",
206 if (des.idVendor != ctx->profile->vid
207 || des.idProduct != ctx->profile->pid)
210 if (sdi->status == SR_ST_INITIALIZING) {
211 if (skip != dev_index) {
212 /* Skip devices of this type that aren't the one we want. */
216 } else if (sdi->status == SR_ST_INACTIVE) {
218 * This device is fully enumerated, so we need to find
219 * this device by vendor, product, bus and address.
221 if (libusb_get_bus_number(devlist[i]) != ctx->usb->bus
222 || libusb_get_device_address(devlist[i]) != ctx->usb->address)
223 /* this is not the one */
227 if (!(ret = libusb_open(devlist[i], &ctx->usb->devhdl))) {
228 if (ctx->usb->address == 0xff)
230 * first time we touch this device after firmware upload,
231 * so we don't know the address yet.
233 ctx->usb->address = libusb_get_device_address(devlist[i]);
235 sr_err("fx2lafw: Failed to open device: %d.", ret);
239 ret = command_get_fw_version(ctx->usb->devhdl, &vi);
241 sr_err("fx2lafw: Failed to retrieve "
242 "firmware version information.");
246 ret = command_get_revid_version(ctx->usb->devhdl, &revid);
248 sr_err("fx2lafw: Failed to retrieve REVID.");
252 if (vi.major != FX2LAFW_VERSION_MAJOR ||
253 vi.minor != FX2LAFW_VERSION_MINOR) {
254 sr_err("fx2lafw: Expected firmware version %d.%d "
255 "got %d.%d.", FX2LAFW_VERSION_MAJOR,
256 FX2LAFW_VERSION_MINOR, vi.major, vi.minor);
260 sdi->status = SR_ST_ACTIVE;
261 sr_info("fx2lafw: Opened device %d on %d.%d "
262 "interface %d, firmware %d.%d, REVID %d.",
263 sdi->index, ctx->usb->bus, ctx->usb->address,
264 USB_INTERFACE, vi.major, vi.minor, revid);
268 libusb_free_device_list(devlist, 1);
270 if (sdi->status != SR_ST_ACTIVE)
276 static void close_dev(struct sr_dev_inst *sdi)
282 if (ctx->usb->devhdl == NULL)
285 sr_info("fx2lafw: Closing device %d on %d.%d interface %d.",
286 sdi->index, ctx->usb->bus, ctx->usb->address, USB_INTERFACE);
287 libusb_release_interface(ctx->usb->devhdl, USB_INTERFACE);
288 libusb_close(ctx->usb->devhdl);
289 ctx->usb->devhdl = NULL;
290 sdi->status = SR_ST_INACTIVE;
293 static int configure_probes(struct context *ctx, GSList *probes)
295 struct sr_probe *probe;
297 int probe_bit, stage, i;
300 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
301 ctx->trigger_mask[i] = 0;
302 ctx->trigger_value[i] = 0;
306 for (l = probes; l; l = l->next) {
307 probe = (struct sr_probe *)l->data;
308 if (probe->enabled == FALSE)
310 probe_bit = 1 << (probe->index - 1);
311 if (!(probe->trigger))
315 for (tc = probe->trigger; *tc; tc++) {
316 ctx->trigger_mask[stage] |= probe_bit;
318 ctx->trigger_value[stage] |= probe_bit;
320 if (stage > NUM_TRIGGER_STAGES)
327 * We didn't configure any triggers, make sure acquisition
328 * doesn't wait for any.
330 ctx->trigger_stage = TRIGGER_FIRED;
332 ctx->trigger_stage = 0;
337 static struct context *fx2lafw_dev_new(void)
341 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
342 sr_err("fx2lafw: %s: ctx malloc failed.", __func__);
346 ctx->trigger_stage = TRIGGER_FIRED;
355 static int hw_init(const char *devinfo)
357 struct sr_dev_inst *sdi;
358 struct libusb_device_descriptor des;
359 const struct fx2lafw_profile *prof;
361 libusb_device **devlist;
366 /* Avoid compiler warnings. */
369 if (libusb_init(&usb_context) != 0) {
370 sr_warn("fx2lafw: Failed to initialize libusb.");
374 /* Find all fx2lafw compatible devices and upload firware to them. */
375 libusb_get_device_list(usb_context, &devlist);
376 for (i = 0; devlist[i]; i++) {
378 if ((ret = libusb_get_device_descriptor(
379 devlist[i], &des)) != 0) {
380 sr_warn("fx2lafw: Failed to get device descriptor: %d.", ret);
385 for (j = 0; supported_fx2[j].vid; j++) {
386 if (des.idVendor == supported_fx2[j].vid &&
387 des.idProduct == supported_fx2[j].pid) {
388 prof = &supported_fx2[j];
392 /* Skip if the device was not found */
396 sdi = sr_dev_inst_new(devcnt, SR_ST_INITIALIZING,
397 prof->vendor, prof->model, prof->model_version);
401 ctx = fx2lafw_dev_new();
404 dev_insts = g_slist_append(dev_insts, sdi);
406 if (check_conf_profile(devlist[i])) {
407 /* Already has the firmware, so fix the new address. */
408 sr_dbg("fx2lafw: Found an fx2lafw device.");
409 sdi->status = SR_ST_INACTIVE;
410 ctx->usb = sr_usb_dev_inst_new
411 (libusb_get_bus_number(devlist[i]),
412 libusb_get_device_address(devlist[i]), NULL);
414 if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION,
415 prof->firmware) == SR_OK)
416 /* Remember when the firmware on this device was updated */
417 g_get_current_time(&ctx->fw_updated);
419 sr_err("fx2lafw: Firmware upload failed for "
420 "device %d.", devcnt);
421 ctx->usb = sr_usb_dev_inst_new
422 (libusb_get_bus_number(devlist[i]), 0xff, NULL);
427 libusb_free_device_list(devlist, 1);
432 static int hw_dev_open(int dev_index)
435 struct sr_dev_inst *sdi;
439 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
444 * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY ms
445 * for the FX2 to renumerate.
448 if (GTV_TO_MSEC(ctx->fw_updated) > 0) {
449 sr_info("fx2lafw: Waiting for device to reset.");
450 /* takes at least 300ms for the FX2 to be gone from the USB bus */
451 g_usleep(300 * 1000);
453 while (timediff < MAX_RENUM_DELAY) {
454 if ((ret = fx2lafw_dev_open(dev_index)) == SR_OK)
456 g_usleep(100 * 1000);
457 g_get_current_time(&cur_time);
458 timediff = GTV_TO_MSEC(cur_time) - GTV_TO_MSEC(ctx->fw_updated);
460 sr_info("fx2lafw: Device came back after %d ms.", timediff);
462 ret = fx2lafw_dev_open(dev_index);
466 sr_err("fx2lafw: Unable to open device.");
471 ret = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
473 sr_err("fx2lafw: Unable to claim interface: %d.", ret);
477 if (ctx->cur_samplerate == 0) {
478 /* Samplerate hasn't been set; default to the slowest one. */
479 if (hw_dev_config_set(dev_index, SR_HWCAP_SAMPLERATE,
480 &supported_samplerates[0]) == SR_ERR)
487 static int hw_dev_close(int dev_index)
489 struct sr_dev_inst *sdi;
491 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
492 sr_err("fx2lafw: %s: sdi was NULL.", __func__);
502 static int hw_cleanup(void)
505 struct sr_dev_inst *sdi;
509 for (l = dev_insts; l; l = l->next) {
510 if (!(sdi = l->data)) {
511 /* Log error, but continue cleaning up the rest. */
512 sr_err("fx2lafw: %s: sdi was NULL, continuing.",
517 if (!(ctx = sdi->priv)) {
518 /* Log error, but continue cleaning up the rest. */
519 sr_err("fx2lafw: %s: sdi->priv was NULL, continuing",
526 sr_dev_inst_free(sdi);
529 g_slist_free(dev_insts);
533 libusb_exit(usb_context);
539 static void *hw_dev_info_get(int dev_index, int dev_info_id)
541 struct sr_dev_inst *sdi;
544 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
548 switch (dev_info_id) {
551 case SR_DI_NUM_PROBES:
552 return GINT_TO_POINTER(ctx->profile->num_probes);
553 case SR_DI_PROBE_NAMES:
555 case SR_DI_SAMPLERATES:
557 case SR_DI_TRIGGER_TYPES:
558 return TRIGGER_TYPES;
559 case SR_DI_CUR_SAMPLERATE:
560 return &ctx->cur_samplerate;
566 static int hw_dev_status_get(int dev_index)
568 const struct sr_dev_inst *const sdi =
569 sr_dev_inst_get(dev_insts, dev_index);
572 return SR_ST_NOT_FOUND;
577 static int *hw_hwcap_get_all(void)
582 static int hw_dev_config_set(int dev_index, int hwcap, void *value)
584 struct sr_dev_inst *sdi;
588 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
592 if (hwcap == SR_HWCAP_SAMPLERATE) {
593 ctx->cur_samplerate = *(uint64_t *)value;
595 } else if (hwcap == SR_HWCAP_PROBECONFIG) {
596 ret = configure_probes(ctx, (GSList *) value);
597 } else if (hwcap == SR_HWCAP_LIMIT_SAMPLES) {
598 ctx->limit_samples = *(uint64_t *)value;
607 static int receive_data(int fd, int revents, void *cb_data)
611 /* Avoid compiler warnings. */
616 tv.tv_sec = tv.tv_usec = 0;
617 libusb_handle_events_timeout(usb_context, &tv);
622 static void abort_acquisition(struct context *ctx)
624 ctx->num_samples = -1;
627 static void finish_acquisition(struct context *ctx)
629 struct sr_datafeed_packet packet;
632 /* Terminate session */
633 packet.type = SR_DF_END;
634 sr_session_send(ctx->session_dev_id, &packet);
636 /* Remove fds from polling */
637 const struct libusb_pollfd **const lupfd =
638 libusb_get_pollfds(usb_context);
639 for (i = 0; lupfd[i]; i++)
640 sr_source_remove(lupfd[i]->fd);
641 free(lupfd); /* NOT g_free()! */
644 static void receive_transfer(struct libusb_transfer *transfer)
646 /* TODO: These statics have to move to the ctx struct. */
647 static int empty_transfer_count = 0;
648 struct sr_datafeed_packet packet;
649 struct sr_datafeed_logic logic;
650 struct context *ctx = transfer->user_data;
651 int cur_buflen, trigger_offset, i;
652 unsigned char *cur_buf, *new_buf;
655 * If acquisition has already ended, just free any queued up
656 * transfer that come in.
658 if (ctx->num_samples == -1) {
660 libusb_free_transfer(transfer);
662 ctx->submitted_transfers--;
663 if (ctx->submitted_transfers == 0)
664 finish_acquisition(ctx);
669 sr_info("fx2lafw: receive_transfer(): status %d received %d bytes.",
670 transfer->status, transfer->actual_length);
672 /* Save incoming transfer before reusing the transfer struct. */
673 cur_buf = transfer->buffer;
674 cur_buflen = transfer->actual_length;
676 /* Fire off a new request. */
677 if (!(new_buf = g_try_malloc(4096))) {
678 sr_err("fx2lafw: %s: new_buf malloc failed.", __func__);
679 return; /* TODO: SR_ERR_MALLOC */
682 transfer->buffer = new_buf;
683 transfer->length = 4096;
684 if (libusb_submit_transfer(transfer) != 0) {
685 /* TODO: Stop session? */
686 /* TODO: Better error message. */
687 sr_err("fx2lafw: %s: libusb_submit_transfer error.", __func__);
690 if (cur_buflen == 0) {
691 empty_transfer_count++;
692 if (empty_transfer_count > MAX_EMPTY_TRANSFERS) {
694 * The FX2 gave up. End the acquisition, the frontend
695 * will work out that the samplecount is short.
697 abort_acquisition(ctx);
701 empty_transfer_count = 0;
705 if (ctx->trigger_stage >= 0) {
706 for (i = 0; i < cur_buflen; i++) {
708 if ((cur_buf[i] & ctx->trigger_mask[ctx->trigger_stage]) == ctx->trigger_value[ctx->trigger_stage]) {
709 /* Match on this trigger stage. */
710 ctx->trigger_buffer[ctx->trigger_stage] = cur_buf[i];
711 ctx->trigger_stage++;
713 if (ctx->trigger_stage == NUM_TRIGGER_STAGES || ctx->trigger_mask[ctx->trigger_stage] == 0) {
714 /* Match on all trigger stages, we're done. */
715 trigger_offset = i + 1;
718 * TODO: Send pre-trigger buffer to session bus.
719 * Tell the frontend we hit the trigger here.
721 packet.type = SR_DF_TRIGGER;
722 packet.payload = NULL;
723 sr_session_send(ctx->session_dev_id, &packet);
726 * Send the samples that triggered it, since we're
727 * skipping past them.
729 packet.type = SR_DF_LOGIC;
730 packet.payload = &logic;
731 logic.length = ctx->trigger_stage;
733 logic.data = ctx->trigger_buffer;
734 sr_session_send(ctx->session_dev_id, &packet);
736 ctx->trigger_stage = TRIGGER_FIRED;
743 * We had a match before, but not in the next sample. However, we may
744 * have a match on this stage in the next bit -- trigger on 0001 will
745 * fail on seeing 00001, so we need to go back to stage 0 -- but at
746 * the next sample from the one that matched originally, which the
747 * counter increment at the end of the loop takes care of.
749 if (ctx->trigger_stage > 0) {
750 i -= ctx->trigger_stage;
752 i = -1; /* Oops, went back past this buffer. */
753 /* Reset trigger stage. */
754 ctx->trigger_stage = 0;
759 if (ctx->trigger_stage == TRIGGER_FIRED) {
760 /* Send the incoming transfer to the session bus. */
761 packet.type = SR_DF_LOGIC;
762 packet.payload = &logic;
763 logic.length = cur_buflen - trigger_offset;
765 logic.data = cur_buf + trigger_offset;
766 sr_session_send(ctx->session_dev_id, &packet);
769 ctx->num_samples += cur_buflen;
770 if (ctx->limit_samples &&
771 (unsigned int)ctx->num_samples > ctx->limit_samples) {
772 abort_acquisition(ctx);
776 * TODO: Buffer pre-trigger data in capture
777 * ratio-sized buffer.
782 static int hw_dev_acquisition_start(int dev_index, void *cb_data)
784 struct sr_dev_inst *sdi;
785 struct sr_datafeed_packet *packet;
786 struct sr_datafeed_header *header;
788 struct libusb_transfer *transfer;
789 const struct libusb_pollfd **lupfd;
793 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
796 ctx->session_dev_id = cb_data;
797 ctx->num_samples = 0;
799 if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
800 sr_err("fx2lafw: %s: packet malloc failed.", __func__);
801 return SR_ERR_MALLOC;
804 if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
805 sr_err("fx2lafw: %s: header malloc failed.", __func__);
806 return SR_ERR_MALLOC;
809 /* Start with 2K transfer, subsequently increased to 4K. */
811 for (i = 0; i < NUM_SIMUL_TRANSFERS; i++) {
812 if (!(buf = g_try_malloc(size))) {
813 sr_err("fx2lafw: %s: buf malloc failed.", __func__);
814 return SR_ERR_MALLOC;
816 transfer = libusb_alloc_transfer(0);
817 libusb_fill_bulk_transfer(transfer, ctx->usb->devhdl,
818 2 | LIBUSB_ENDPOINT_IN, buf, size,
819 receive_transfer, ctx, 40);
820 if (libusb_submit_transfer(transfer) != 0) {
821 /* TODO: Free them all. */
822 libusb_free_transfer(transfer);
827 ctx->submitted_transfers++;
831 lupfd = libusb_get_pollfds(usb_context);
832 for (i = 0; lupfd[i]; i++)
833 sr_source_add(lupfd[i]->fd, lupfd[i]->events,
834 40, receive_data, NULL);
835 free(lupfd); /* NOT g_free()! */
837 packet->type = SR_DF_HEADER;
838 packet->payload = header;
839 header->feed_version = 1;
840 gettimeofday(&header->starttime, NULL);
841 header->samplerate = ctx->cur_samplerate;
842 header->num_logic_probes = ctx->profile->num_probes;
843 sr_session_send(cb_data, packet);
847 if ((ret = command_start_acquisition (ctx->usb->devhdl,
848 ctx->cur_samplerate)) != SR_OK) {
855 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
856 static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
858 struct sr_dev_inst *sdi;
860 /* Avoid compiler warnings. */
863 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
866 abort_acquisition(sdi->priv);
871 SR_PRIV struct sr_dev_driver fx2lafw_driver_info = {
873 .longname = "fx2lafw (generic driver for FX2 based LAs)",
876 .cleanup = hw_cleanup,
877 .dev_open = hw_dev_open,
878 .dev_close = hw_dev_close,
879 .dev_info_get = hw_dev_info_get,
880 .dev_status_get = hw_dev_status_get,
881 .hwcap_get_all = hw_hwcap_get_all,
882 .dev_config_set = hw_dev_config_set,
883 .dev_acquisition_start = hw_dev_acquisition_start,
884 .dev_acquisition_stop = hw_dev_acquisition_stop,