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/>.
28 #include "sigrok-internal.h"
32 static const struct fx2lafw_profile supported_fx2[] = {
35 * EE Electronics ESLA201A
37 { 0x08a9, 0x0014, "CWAV", "USBee AX", NULL,
38 FIRMWARE_DIR "/fx2lafw-cwav-usbeeax.fw", 8 },
43 { 0x08a9, 0x0009, "CWAV", "USBee SX", NULL,
44 FIRMWARE_DIR "/fx2lafw-cwav-usbeesx.fw", 8 },
48 * EE Electronics ESLA100
50 * Robomotic BugLogic 3
52 { 0x0925, 0x3881, "Saleae", "Logic", NULL,
53 FIRMWARE_DIR "/fx2lafw-saleae-logic.fw", 8 },
56 * Default Cypress FX2 without EEPROM, e.g.:
58 * Braintechnology USB Interface V2.x
60 { 0x04B4, 0x8613, "Cypress", "FX2", NULL,
61 FIRMWARE_DIR "/fx2lafw-cypress-fx2.fw", 8 },
64 * Braintechnology USB-LPS
66 { 0x16d0, 0x0498, "Braintechnology", "USB-LPS", NULL,
67 FIRMWARE_DIR "/fx2lafw-braintechnology-usb-lps.fw", 8 },
69 { 0, 0, 0, 0, 0, 0, 0 }
72 static int hwcaps[] = {
73 SR_HWCAP_LOGIC_ANALYZER,
76 /* These are really implemented in the driver, not the hardware. */
77 SR_HWCAP_LIMIT_SAMPLES,
83 * TODO: Different probe_names[] for each supported device.
85 static const char *probe_names[] = {
97 static uint64_t supported_samplerates[] = {
113 static struct sr_samplerates samplerates = {
117 supported_samplerates,
120 static GSList *dev_insts = NULL;
121 static libusb_context *usb_context = NULL;
123 static int hw_dev_config_set(int dev_index, int hwcap, void *value);
124 static int hw_dev_acquisition_stop(int dev_index, void *cb_data);
127 * Check the USB configuration to determine if this is an fx2lafw device.
129 * @return TRUE if the device's configuration profile match fx2lafw
130 * configuration, FALSE otherwise.
132 static gboolean check_conf_profile(libusb_device *dev)
134 struct libusb_device_descriptor des;
135 struct libusb_device_handle *hdl;
137 unsigned char strdesc[64];
142 /* Assume the FW has not been loaded, unless proven wrong. */
143 if (libusb_get_device_descriptor(dev, &des) != 0)
146 if (libusb_open(dev, &hdl) != 0)
149 if (libusb_get_string_descriptor_ascii(hdl,
150 des.iManufacturer, strdesc, sizeof(strdesc)) < 0)
152 if (strncmp((const char *)strdesc, "sigrok", 6))
155 if (libusb_get_string_descriptor_ascii(hdl,
156 des.iProduct, strdesc, sizeof(strdesc)) < 0)
158 if (strncmp((const char *)strdesc, "fx2lafw", 7))
161 /* If we made it here, it must be an fx2lafw. */
170 static int fx2lafw_dev_open(int dev_index)
172 libusb_device **devlist;
173 struct libusb_device_descriptor des;
174 struct sr_dev_inst *sdi;
176 struct version_info vi;
180 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
184 if (sdi->status == SR_ST_ACTIVE)
189 const int device_count = libusb_get_device_list(usb_context, &devlist);
190 if (device_count < 0) {
191 sr_err("fx2lafw: Failed to retrieve device list (%d)",
196 for (i = 0; i < device_count; i++) {
197 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
198 sr_err("fx2lafw: Failed to get device descriptor: %d.",
203 if (des.idVendor != ctx->profile->vid
204 || des.idProduct != ctx->profile->pid)
207 if (sdi->status == SR_ST_INITIALIZING) {
208 if (skip != dev_index) {
209 /* Skip devices of this type that aren't the one we want. */
213 } else if (sdi->status == SR_ST_INACTIVE) {
215 * This device is fully enumerated, so we need to find
216 * this device by vendor, product, bus and address.
218 if (libusb_get_bus_number(devlist[i]) != ctx->usb->bus
219 || libusb_get_device_address(devlist[i]) != ctx->usb->address)
220 /* this is not the one */
224 if (!(ret = libusb_open(devlist[i], &ctx->usb->devhdl))) {
225 if (ctx->usb->address == 0xff)
227 * first time we touch this device after firmware upload,
228 * so we don't know the address yet.
230 ctx->usb->address = libusb_get_device_address(devlist[i]);
232 sr_err("fx2lafw: Failed to open device: %d.", ret);
236 ret = command_get_fw_version(ctx->usb->devhdl, &vi);
238 sr_err("fx2lafw: Failed to retrieve "
239 "firmware version information.");
243 ret = command_get_revid_version(ctx->usb->devhdl, &revid);
245 sr_err("fx2lafw: Failed to retrieve REVID.");
249 if (vi.major != FX2LAFW_VERSION_MAJOR ||
250 vi.minor != FX2LAFW_VERSION_MINOR) {
251 sr_err("fx2lafw: Expected firmware version %d.%d "
252 "got %d.%d.", FX2LAFW_VERSION_MAJOR,
253 FX2LAFW_VERSION_MINOR, vi.major, vi.minor);
257 sdi->status = SR_ST_ACTIVE;
258 sr_info("fx2lafw: Opened device %d on %d.%d "
259 "interface %d, firmware %d.%d, REVID %d.",
260 sdi->index, ctx->usb->bus, ctx->usb->address,
261 USB_INTERFACE, vi.major, vi.minor, revid);
265 libusb_free_device_list(devlist, 1);
267 if (sdi->status != SR_ST_ACTIVE)
273 static void close_dev(struct sr_dev_inst *sdi)
279 if (ctx->usb->devhdl == NULL)
282 sr_info("fx2lafw: Closing device %d on %d.%d interface %d.",
283 sdi->index, ctx->usb->bus, ctx->usb->address, USB_INTERFACE);
284 libusb_release_interface(ctx->usb->devhdl, USB_INTERFACE);
285 libusb_close(ctx->usb->devhdl);
286 ctx->usb->devhdl = NULL;
287 sdi->status = SR_ST_INACTIVE;
290 static int configure_probes(struct context *ctx, GSList *probes)
292 struct sr_probe *probe;
294 int probe_bit, stage, i;
297 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
298 ctx->trigger_mask[i] = 0;
299 ctx->trigger_value[i] = 0;
303 for (l = probes; l; l = l->next) {
304 probe = (struct sr_probe *)l->data;
305 if (probe->enabled == FALSE)
307 probe_bit = 1 << (probe->index - 1);
308 if (!(probe->trigger))
312 for (tc = probe->trigger; *tc; tc++) {
313 ctx->trigger_mask[stage] |= probe_bit;
315 ctx->trigger_value[stage] |= probe_bit;
317 if (stage > NUM_TRIGGER_STAGES)
324 * We didn't configure any triggers, make sure acquisition
325 * doesn't wait for any.
327 ctx->trigger_stage = TRIGGER_FIRED;
329 ctx->trigger_stage = 0;
334 static struct context *fx2lafw_dev_new(void)
338 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
339 sr_err("fx2lafw: %s: ctx malloc failed.", __func__);
343 ctx->trigger_stage = TRIGGER_FIRED;
352 static int hw_init(const char *devinfo)
354 struct sr_dev_inst *sdi;
355 struct libusb_device_descriptor des;
356 const struct fx2lafw_profile *prof;
358 libusb_device **devlist;
363 /* Avoid compiler warnings. */
366 if (libusb_init(&usb_context) != 0) {
367 sr_warn("fx2lafw: Failed to initialize libusb.");
371 /* Find all fx2lafw compatible devices and upload firware to them. */
372 libusb_get_device_list(usb_context, &devlist);
373 for (i = 0; devlist[i]; i++) {
375 if ((ret = libusb_get_device_descriptor(
376 devlist[i], &des)) != 0) {
377 sr_warn("fx2lafw: Failed to get device descriptor: %d.", ret);
382 for (j = 0; supported_fx2[j].vid; j++) {
383 if (des.idVendor == supported_fx2[j].vid &&
384 des.idProduct == supported_fx2[j].pid) {
385 prof = &supported_fx2[j];
389 /* Skip if the device was not found */
393 sdi = sr_dev_inst_new(devcnt, SR_ST_INITIALIZING,
394 prof->vendor, prof->model, prof->model_version);
398 ctx = fx2lafw_dev_new();
401 dev_insts = g_slist_append(dev_insts, sdi);
403 if (check_conf_profile(devlist[i])) {
404 /* Already has the firmware, so fix the new address. */
405 sr_dbg("fx2lafw: Found an fx2lafw device.");
406 sdi->status = SR_ST_INACTIVE;
407 ctx->usb = sr_usb_dev_inst_new
408 (libusb_get_bus_number(devlist[i]),
409 libusb_get_device_address(devlist[i]), NULL);
411 if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION,
412 prof->firmware) == SR_OK)
413 /* Remember when the firmware on this device was updated */
414 g_get_current_time(&ctx->fw_updated);
416 sr_err("fx2lafw: Firmware upload failed for "
417 "device %d.", devcnt);
418 ctx->usb = sr_usb_dev_inst_new
419 (libusb_get_bus_number(devlist[i]), 0xff, NULL);
424 libusb_free_device_list(devlist, 1);
429 static int hw_dev_open(int dev_index)
432 struct sr_dev_inst *sdi;
436 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
441 * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY ms
442 * for the FX2 to renumerate.
445 if (GTV_TO_MSEC(ctx->fw_updated) > 0) {
446 sr_info("fx2lafw: Waiting for device to reset.");
447 /* takes at least 300ms for the FX2 to be gone from the USB bus */
448 g_usleep(300 * 1000);
450 while (timediff < MAX_RENUM_DELAY) {
451 if ((ret = fx2lafw_dev_open(dev_index)) == SR_OK)
453 g_usleep(100 * 1000);
454 g_get_current_time(&cur_time);
455 timediff = GTV_TO_MSEC(cur_time) - GTV_TO_MSEC(ctx->fw_updated);
457 sr_info("fx2lafw: Device came back after %d ms.", timediff);
459 ret = fx2lafw_dev_open(dev_index);
463 sr_err("fx2lafw: Unable to open device.");
468 ret = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
470 sr_err("fx2lafw: Unable to claim interface: %d.", ret);
474 if (ctx->cur_samplerate == 0) {
475 /* Samplerate hasn't been set; default to the slowest one. */
476 if (hw_dev_config_set(dev_index, SR_HWCAP_SAMPLERATE,
477 &supported_samplerates[0]) == SR_ERR)
484 static int hw_dev_close(int dev_index)
486 struct sr_dev_inst *sdi;
488 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
489 sr_err("fx2lafw: %s: sdi was NULL.", __func__);
499 static int hw_cleanup(void)
502 struct sr_dev_inst *sdi;
506 for (l = dev_insts; l; l = l->next) {
507 if (!(sdi = l->data)) {
508 /* Log error, but continue cleaning up the rest. */
509 sr_err("fx2lafw: %s: sdi was NULL, continuing.",
514 if (!(ctx = sdi->priv)) {
515 /* Log error, but continue cleaning up the rest. */
516 sr_err("fx2lafw: %s: sdi->priv was NULL, continuing",
523 sr_dev_inst_free(sdi);
526 g_slist_free(dev_insts);
530 libusb_exit(usb_context);
536 static void *hw_dev_info_get(int dev_index, int dev_info_id)
538 struct sr_dev_inst *sdi;
541 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
545 switch (dev_info_id) {
548 case SR_DI_NUM_PROBES:
549 return GINT_TO_POINTER(ctx->profile->num_probes);
550 case SR_DI_PROBE_NAMES:
552 case SR_DI_SAMPLERATES:
554 case SR_DI_TRIGGER_TYPES:
555 return TRIGGER_TYPES;
556 case SR_DI_CUR_SAMPLERATE:
557 return &ctx->cur_samplerate;
563 static int hw_dev_status_get(int dev_index)
565 const struct sr_dev_inst *const sdi =
566 sr_dev_inst_get(dev_insts, dev_index);
569 return SR_ST_NOT_FOUND;
574 static int *hw_hwcap_get_all(void)
579 static int hw_dev_config_set(int dev_index, int hwcap, void *value)
581 struct sr_dev_inst *sdi;
585 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
589 if (hwcap == SR_HWCAP_SAMPLERATE) {
590 ctx->cur_samplerate = *(uint64_t *)value;
592 } else if (hwcap == SR_HWCAP_PROBECONFIG) {
593 ret = configure_probes(ctx, (GSList *) value);
594 } else if (hwcap == SR_HWCAP_LIMIT_SAMPLES) {
595 ctx->limit_samples = *(uint64_t *)value;
604 static int receive_data(int fd, int revents, void *cb_data)
608 /* Avoid compiler warnings. */
613 tv.tv_sec = tv.tv_usec = 0;
614 libusb_handle_events_timeout(usb_context, &tv);
619 static void abort_acquisition(struct context *ctx)
621 ctx->num_samples = -1;
624 static void finish_acquisition(struct context *ctx)
626 struct sr_datafeed_packet packet;
629 /* Terminate session */
630 packet.type = SR_DF_END;
631 sr_session_send(ctx->session_dev_id, &packet);
633 /* Remove fds from polling */
634 const struct libusb_pollfd **const lupfd =
635 libusb_get_pollfds(usb_context);
636 for (i = 0; lupfd[i]; i++)
637 sr_source_remove(lupfd[i]->fd);
638 free(lupfd); /* NOT g_free()! */
641 static void receive_transfer(struct libusb_transfer *transfer)
643 /* TODO: These statics have to move to the ctx struct. */
644 static int empty_transfer_count = 0;
645 struct sr_datafeed_packet packet;
646 struct sr_datafeed_logic logic;
647 struct context *ctx = transfer->user_data;
648 int cur_buflen, trigger_offset, i;
649 unsigned char *cur_buf, *new_buf;
652 * If acquisition has already ended, just free any queued up
653 * transfer that come in.
655 if (ctx->num_samples == -1) {
657 libusb_free_transfer(transfer);
659 ctx->submitted_transfers--;
660 if (ctx->submitted_transfers == 0)
661 finish_acquisition(ctx);
666 sr_info("fx2lafw: receive_transfer(): status %d received %d bytes.",
667 transfer->status, transfer->actual_length);
669 /* Save incoming transfer before reusing the transfer struct. */
670 cur_buf = transfer->buffer;
671 cur_buflen = transfer->actual_length;
673 /* Fire off a new request. */
674 if (!(new_buf = g_try_malloc(4096))) {
675 sr_err("fx2lafw: %s: new_buf malloc failed.", __func__);
676 return; /* TODO: SR_ERR_MALLOC */
679 transfer->buffer = new_buf;
680 transfer->length = 4096;
681 if (libusb_submit_transfer(transfer) != 0) {
682 /* TODO: Stop session? */
683 /* TODO: Better error message. */
684 sr_err("fx2lafw: %s: libusb_submit_transfer error.", __func__);
687 if (cur_buflen == 0) {
688 empty_transfer_count++;
689 if (empty_transfer_count > MAX_EMPTY_TRANSFERS) {
691 * The FX2 gave up. End the acquisition, the frontend
692 * will work out that the samplecount is short.
694 abort_acquisition(ctx);
698 empty_transfer_count = 0;
702 if (ctx->trigger_stage >= 0) {
703 for (i = 0; i < cur_buflen; i++) {
705 if ((cur_buf[i] & ctx->trigger_mask[ctx->trigger_stage]) == ctx->trigger_value[ctx->trigger_stage]) {
706 /* Match on this trigger stage. */
707 ctx->trigger_buffer[ctx->trigger_stage] = cur_buf[i];
708 ctx->trigger_stage++;
710 if (ctx->trigger_stage == NUM_TRIGGER_STAGES || ctx->trigger_mask[ctx->trigger_stage] == 0) {
711 /* Match on all trigger stages, we're done. */
712 trigger_offset = i + 1;
715 * TODO: Send pre-trigger buffer to session bus.
716 * Tell the frontend we hit the trigger here.
718 packet.type = SR_DF_TRIGGER;
719 packet.payload = NULL;
720 sr_session_send(ctx->session_dev_id, &packet);
723 * Send the samples that triggered it, since we're
724 * skipping past them.
726 packet.type = SR_DF_LOGIC;
727 packet.payload = &logic;
728 logic.length = ctx->trigger_stage;
730 logic.data = ctx->trigger_buffer;
731 sr_session_send(ctx->session_dev_id, &packet);
733 ctx->trigger_stage = TRIGGER_FIRED;
740 * We had a match before, but not in the next sample. However, we may
741 * have a match on this stage in the next bit -- trigger on 0001 will
742 * fail on seeing 00001, so we need to go back to stage 0 -- but at
743 * the next sample from the one that matched originally, which the
744 * counter increment at the end of the loop takes care of.
746 if (ctx->trigger_stage > 0) {
747 i -= ctx->trigger_stage;
749 i = -1; /* Oops, went back past this buffer. */
750 /* Reset trigger stage. */
751 ctx->trigger_stage = 0;
756 if (ctx->trigger_stage == TRIGGER_FIRED) {
757 /* Send the incoming transfer to the session bus. */
758 packet.type = SR_DF_LOGIC;
759 packet.payload = &logic;
760 logic.length = cur_buflen - trigger_offset;
762 logic.data = cur_buf + trigger_offset;
763 sr_session_send(ctx->session_dev_id, &packet);
766 ctx->num_samples += cur_buflen;
767 if (ctx->limit_samples &&
768 (unsigned int)ctx->num_samples > ctx->limit_samples) {
769 abort_acquisition(ctx);
773 * TODO: Buffer pre-trigger data in capture
774 * ratio-sized buffer.
779 static int hw_dev_acquisition_start(int dev_index, void *cb_data)
781 struct sr_dev_inst *sdi;
782 struct sr_datafeed_packet *packet;
783 struct sr_datafeed_header *header;
785 struct libusb_transfer *transfer;
786 const struct libusb_pollfd **lupfd;
790 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
793 ctx->session_dev_id = cb_data;
794 ctx->num_samples = 0;
796 if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
797 sr_err("fx2lafw: %s: packet malloc failed.", __func__);
798 return SR_ERR_MALLOC;
801 if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
802 sr_err("fx2lafw: %s: header malloc failed.", __func__);
803 return SR_ERR_MALLOC;
806 /* Start with 2K transfer, subsequently increased to 4K. */
808 for (i = 0; i < NUM_SIMUL_TRANSFERS; i++) {
809 if (!(buf = g_try_malloc(size))) {
810 sr_err("fx2lafw: %s: buf malloc failed.", __func__);
811 return SR_ERR_MALLOC;
813 transfer = libusb_alloc_transfer(0);
814 libusb_fill_bulk_transfer(transfer, ctx->usb->devhdl,
815 2 | LIBUSB_ENDPOINT_IN, buf, size,
816 receive_transfer, ctx, 40);
817 if (libusb_submit_transfer(transfer) != 0) {
818 /* TODO: Free them all. */
819 libusb_free_transfer(transfer);
824 ctx->submitted_transfers++;
828 lupfd = libusb_get_pollfds(usb_context);
829 for (i = 0; lupfd[i]; i++)
830 sr_source_add(lupfd[i]->fd, lupfd[i]->events,
831 40, receive_data, NULL);
832 free(lupfd); /* NOT g_free()! */
834 packet->type = SR_DF_HEADER;
835 packet->payload = header;
836 header->feed_version = 1;
837 gettimeofday(&header->starttime, NULL);
838 header->samplerate = ctx->cur_samplerate;
839 header->num_logic_probes = ctx->profile->num_probes;
840 sr_session_send(cb_data, packet);
844 if ((ret = command_start_acquisition (ctx->usb->devhdl,
845 ctx->cur_samplerate)) != SR_OK) {
852 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
853 static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
855 struct sr_dev_inst *sdi;
857 /* Avoid compiler warnings. */
860 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
863 abort_acquisition(sdi->priv);
868 SR_PRIV struct sr_dev_driver fx2lafw_driver_info = {
870 .longname = "fx2lafw (generic driver for FX2 based LAs)",
873 .cleanup = hw_cleanup,
874 .dev_open = hw_dev_open,
875 .dev_close = hw_dev_close,
876 .dev_info_get = hw_dev_info_get,
877 .dev_status_get = hw_dev_status_get,
878 .hwcap_get_all = hw_hwcap_get_all,
879 .dev_config_set = hw_dev_config_set,
880 .dev_acquisition_start = hw_dev_acquisition_start,
881 .dev_acquisition_stop = hw_dev_acquisition_stop,