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
50 { 0x0925, 0x3881, "Saleae", "Logic", NULL,
51 FIRMWARE_DIR "/fx2lafw-saleae-logic.fw", 8 },
54 * Default Cypress FX2 without EEPROM
56 { 0x04B4, 0x8613, "Cypress", "FX2", NULL,
57 FIRMWARE_DIR "/fx2lafw-cypress-fx2.fw", 8 },
59 { 0, 0, 0, 0, 0, 0, 0 }
62 static int hwcaps[] = {
63 SR_HWCAP_LOGIC_ANALYZER,
66 /* These are really implemented in the driver, not the hardware. */
67 SR_HWCAP_LIMIT_SAMPLES,
73 * TODO: Different probe_names[] for each supported device.
75 static const char *probe_names[] = {
87 static uint64_t supported_samplerates[] = {
103 static struct sr_samplerates samplerates = {
107 supported_samplerates,
110 static GSList *dev_insts = NULL;
111 static libusb_context *usb_context = NULL;
113 static int hw_dev_config_set(int dev_index, int hwcap, void *value);
114 static int hw_dev_acquisition_stop(int dev_index, void *cb_data);
117 * Check the USB configuration to determine if this is an fx2lafw device.
119 * @return TRUE if the device's configuration profile match fx2lafw
120 * configuration, FALSE otherwise.
122 static gboolean check_conf_profile(libusb_device *dev)
124 struct libusb_device_descriptor des;
125 struct libusb_config_descriptor *conf_dsc = NULL;
126 const struct libusb_interface_descriptor *intf_dsc;
127 gboolean ret = FALSE;
130 /* Assume the FW has not been loaded, unless proven wrong. */
133 if (libusb_get_device_descriptor(dev, &des) != 0)
136 /* Need exactly 1 configuration. */
137 if (des.bNumConfigurations != 1)
140 if (libusb_get_config_descriptor(dev, 0, &conf_dsc) != 0)
143 /* Need exactly 1 interface. */
144 if (conf_dsc->bNumInterfaces != 1)
147 /* Need just one alternate setting. */
148 if (conf_dsc->interface[0].num_altsetting != 1)
151 /* Need exactly 2 endpoints. */
152 intf_dsc = &(conf_dsc->interface[0].altsetting[0]);
153 if (intf_dsc->bNumEndpoints != 2)
156 /* The first endpoint should be 2 (inbound). */
157 if ((intf_dsc->endpoint[0].bEndpointAddress & 0x8f) !=
158 (2 | LIBUSB_ENDPOINT_IN))
161 /* If we made it here, it must be an fx2lafw. */
166 libusb_free_config_descriptor(conf_dsc);
171 static int fx2lafw_dev_open(int dev_index)
173 libusb_device **devlist;
174 struct libusb_device_descriptor des;
175 struct sr_dev_inst *sdi;
177 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 if (vi.major != FX2LAFW_VERSION_MAJOR ||
244 vi.minor != FX2LAFW_VERSION_MINOR) {
245 sr_err("fx2lafw: Expected firmware version %d.%02d "
246 "got %d.%02d.", FX2LAFW_VERSION_MAJOR,
247 FX2LAFW_VERSION_MINOR, vi.major, vi.minor);
251 sdi->status = SR_ST_ACTIVE;
252 sr_info("fx2lafw: Opened device %d on %d.%d "
253 "interface %d, firmware version %d.%02d",
254 sdi->index, ctx->usb->bus, ctx->usb->address,
255 USB_INTERFACE, vi.major, vi.minor);
259 libusb_free_device_list(devlist, 1);
261 if (sdi->status != SR_ST_ACTIVE)
267 static void close_dev(struct sr_dev_inst *sdi)
273 if (ctx->usb->devhdl == NULL)
276 sr_info("fx2lafw: Closing device %d on %d.%d interface %d.",
277 sdi->index, ctx->usb->bus, ctx->usb->address, USB_INTERFACE);
278 libusb_release_interface(ctx->usb->devhdl, USB_INTERFACE);
279 libusb_close(ctx->usb->devhdl);
280 ctx->usb->devhdl = NULL;
281 sdi->status = SR_ST_INACTIVE;
284 static int configure_probes(struct context *ctx, GSList *probes)
286 struct sr_probe *probe;
288 int probe_bit, stage, i;
291 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
292 ctx->trigger_mask[i] = 0;
293 ctx->trigger_value[i] = 0;
297 for (l = probes; l; l = l->next) {
298 probe = (struct sr_probe *)l->data;
299 if (probe->enabled == FALSE)
301 probe_bit = 1 << (probe->index - 1);
302 if (!(probe->trigger))
306 for (tc = probe->trigger; *tc; tc++) {
307 ctx->trigger_mask[stage] |= probe_bit;
309 ctx->trigger_value[stage] |= probe_bit;
311 if (stage > NUM_TRIGGER_STAGES)
318 * We didn't configure any triggers, make sure acquisition
319 * doesn't wait for any.
321 ctx->trigger_stage = TRIGGER_FIRED;
323 ctx->trigger_stage = 0;
328 static struct context *fx2lafw_dev_new(void)
332 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
333 sr_err("fx2lafw: %s: ctx malloc failed.", __func__);
337 ctx->trigger_stage = TRIGGER_FIRED;
346 static int hw_init(const char *devinfo)
348 struct sr_dev_inst *sdi;
349 struct libusb_device_descriptor des;
350 const struct fx2lafw_profile *prof;
352 libusb_device **devlist;
357 /* Avoid compiler warnings. */
360 if (libusb_init(&usb_context) != 0) {
361 sr_warn("fx2lafw: Failed to initialize libusb.");
365 /* Find all fx2lafw compatible devices and upload firware to them. */
366 libusb_get_device_list(usb_context, &devlist);
367 for (i = 0; devlist[i]; i++) {
369 if ((ret = libusb_get_device_descriptor(
370 devlist[i], &des)) != 0) {
371 sr_warn("fx2lafw: Failed to get device descriptor: %d.", ret);
376 for (j = 0; supported_fx2[j].vid; j++) {
377 if (des.idVendor == supported_fx2[j].vid &&
378 des.idProduct == supported_fx2[j].pid) {
379 prof = &supported_fx2[j];
383 /* Skip if the device was not found */
387 sdi = sr_dev_inst_new(devcnt, SR_ST_INITIALIZING,
388 prof->vendor, prof->model, prof->model_version);
392 ctx = fx2lafw_dev_new();
395 dev_insts = g_slist_append(dev_insts, sdi);
397 if (check_conf_profile(devlist[i])) {
398 /* Already has the firmware, so fix the new address. */
399 sr_dbg("fx2lafw: Found an fx2lafw device.");
400 sdi->status = SR_ST_INACTIVE;
401 ctx->usb = sr_usb_dev_inst_new
402 (libusb_get_bus_number(devlist[i]),
403 libusb_get_device_address(devlist[i]), NULL);
405 if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION,
406 prof->firmware) == SR_OK)
407 /* Remember when the firmware on this device was updated */
408 g_get_current_time(&ctx->fw_updated);
410 sr_err("fx2lafw: Firmware upload failed for "
411 "device %d.", devcnt);
412 ctx->usb = sr_usb_dev_inst_new
413 (libusb_get_bus_number(devlist[i]), 0xff, NULL);
418 libusb_free_device_list(devlist, 1);
423 static int hw_dev_open(int dev_index)
426 struct sr_dev_inst *sdi;
430 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
435 * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY ms
436 * for the FX2 to renumerate.
439 if (GTV_TO_MSEC(ctx->fw_updated) > 0) {
440 sr_info("fx2lafw: Waiting for device to reset.");
441 /* takes at least 300ms for the FX2 to be gone from the USB bus */
442 g_usleep(300 * 1000);
444 while (timediff < MAX_RENUM_DELAY) {
445 if ((ret = fx2lafw_dev_open(dev_index)) == SR_OK)
447 g_usleep(100 * 1000);
448 g_get_current_time(&cur_time);
449 timediff = GTV_TO_MSEC(cur_time) - GTV_TO_MSEC(ctx->fw_updated);
451 sr_info("fx2lafw: Device came back after %d ms.", timediff);
453 ret = fx2lafw_dev_open(dev_index);
457 sr_err("fx2lafw: Unable to open device.");
462 ret = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
464 sr_err("fx2lafw: Unable to claim interface: %d.", ret);
468 if (ctx->cur_samplerate == 0) {
469 /* Samplerate hasn't been set; default to the slowest one. */
470 if (hw_dev_config_set(dev_index, SR_HWCAP_SAMPLERATE,
471 &supported_samplerates[0]) == SR_ERR)
478 static int hw_dev_close(int dev_index)
480 struct sr_dev_inst *sdi;
482 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
483 sr_err("fx2lafw: %s: sdi was NULL.", __func__);
493 static int hw_cleanup(void)
496 struct sr_dev_inst *sdi;
500 for (l = dev_insts; l; l = l->next) {
501 if (!(sdi = l->data)) {
502 /* Log error, but continue cleaning up the rest. */
503 sr_err("fx2lafw: %s: sdi was NULL, continuing.",
508 if (!(ctx = sdi->priv)) {
509 /* Log error, but continue cleaning up the rest. */
510 sr_err("fx2lafw: %s: sdi->priv was NULL, continuing",
517 sr_dev_inst_free(sdi);
520 g_slist_free(dev_insts);
524 libusb_exit(usb_context);
530 static void *hw_dev_info_get(int dev_index, int dev_info_id)
532 struct sr_dev_inst *sdi;
535 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
539 switch (dev_info_id) {
542 case SR_DI_NUM_PROBES:
543 return GINT_TO_POINTER(ctx->profile->num_probes);
544 case SR_DI_PROBE_NAMES:
546 case SR_DI_SAMPLERATES:
548 case SR_DI_TRIGGER_TYPES:
549 return TRIGGER_TYPES;
550 case SR_DI_CUR_SAMPLERATE:
551 return &ctx->cur_samplerate;
557 static int hw_dev_status_get(int dev_index)
559 const struct sr_dev_inst *const sdi =
560 sr_dev_inst_get(dev_insts, dev_index);
563 return SR_ST_NOT_FOUND;
568 static int *hw_hwcap_get_all(void)
573 static int hw_dev_config_set(int dev_index, int hwcap, void *value)
575 struct sr_dev_inst *sdi;
579 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
583 if (hwcap == SR_HWCAP_SAMPLERATE) {
584 ctx->cur_samplerate = *(uint64_t *)value;
586 } else if (hwcap == SR_HWCAP_PROBECONFIG) {
587 ret = configure_probes(ctx, (GSList *) value);
588 } else if (hwcap == SR_HWCAP_LIMIT_SAMPLES) {
589 ctx->limit_samples = *(uint64_t *)value;
598 static int receive_data(int fd, int revents, void *cb_data)
602 /* Avoid compiler warnings. */
607 tv.tv_sec = tv.tv_usec = 0;
608 libusb_handle_events_timeout(usb_context, &tv);
613 static void abort_acquisition(struct context *ctx)
615 ctx->num_samples = -1;
618 static void finish_acquisition(struct context *ctx)
620 struct sr_datafeed_packet packet;
623 /* Terminate session */
624 packet.type = SR_DF_END;
625 sr_session_send(ctx->session_dev_id, &packet);
627 /* Remove fds from polling */
628 const struct libusb_pollfd **const lupfd =
629 libusb_get_pollfds(usb_context);
630 for (i = 0; lupfd[i]; i++)
631 sr_source_remove(lupfd[i]->fd);
632 free(lupfd); /* NOT g_free()! */
635 static void receive_transfer(struct libusb_transfer *transfer)
637 /* TODO: These statics have to move to the ctx struct. */
638 static int empty_transfer_count = 0;
639 struct sr_datafeed_packet packet;
640 struct sr_datafeed_logic logic;
641 struct context *ctx = transfer->user_data;
642 int cur_buflen, trigger_offset, i;
643 unsigned char *cur_buf, *new_buf;
646 * If acquisition has already ended, just free any queued up
647 * transfer that come in.
649 if (ctx->num_samples == -1) {
651 libusb_free_transfer(transfer);
653 ctx->submitted_transfers--;
654 if (ctx->submitted_transfers == 0)
655 finish_acquisition(ctx);
660 sr_info("fx2lafw: receive_transfer(): status %d received %d bytes.",
661 transfer->status, transfer->actual_length);
663 /* Save incoming transfer before reusing the transfer struct. */
664 cur_buf = transfer->buffer;
665 cur_buflen = transfer->actual_length;
667 /* Fire off a new request. */
668 if (!(new_buf = g_try_malloc(4096))) {
669 sr_err("fx2lafw: %s: new_buf malloc failed.", __func__);
670 return; /* TODO: SR_ERR_MALLOC */
673 transfer->buffer = new_buf;
674 transfer->length = 4096;
675 if (libusb_submit_transfer(transfer) != 0) {
676 /* TODO: Stop session? */
677 /* TODO: Better error message. */
678 sr_err("fx2lafw: %s: libusb_submit_transfer error.", __func__);
681 if (cur_buflen == 0) {
682 empty_transfer_count++;
683 if (empty_transfer_count > MAX_EMPTY_TRANSFERS) {
685 * The FX2 gave up. End the acquisition, the frontend
686 * will work out that the samplecount is short.
688 abort_acquisition(ctx);
692 empty_transfer_count = 0;
696 if (ctx->trigger_stage >= 0) {
697 for (i = 0; i < cur_buflen; i++) {
699 if ((cur_buf[i] & ctx->trigger_mask[ctx->trigger_stage]) == ctx->trigger_value[ctx->trigger_stage]) {
700 /* Match on this trigger stage. */
701 ctx->trigger_buffer[ctx->trigger_stage] = cur_buf[i];
702 ctx->trigger_stage++;
704 if (ctx->trigger_stage == NUM_TRIGGER_STAGES || ctx->trigger_mask[ctx->trigger_stage] == 0) {
705 /* Match on all trigger stages, we're done. */
706 trigger_offset = i + 1;
709 * TODO: Send pre-trigger buffer to session bus.
710 * Tell the frontend we hit the trigger here.
712 packet.type = SR_DF_TRIGGER;
713 packet.payload = NULL;
714 sr_session_send(ctx->session_dev_id, &packet);
717 * Send the samples that triggered it, since we're
718 * skipping past them.
720 packet.type = SR_DF_LOGIC;
721 packet.payload = &logic;
722 logic.length = ctx->trigger_stage;
724 logic.data = ctx->trigger_buffer;
725 sr_session_send(ctx->session_dev_id, &packet);
727 ctx->trigger_stage = TRIGGER_FIRED;
734 * We had a match before, but not in the next sample. However, we may
735 * have a match on this stage in the next bit -- trigger on 0001 will
736 * fail on seeing 00001, so we need to go back to stage 0 -- but at
737 * the next sample from the one that matched originally, which the
738 * counter increment at the end of the loop takes care of.
740 if (ctx->trigger_stage > 0) {
741 i -= ctx->trigger_stage;
743 i = -1; /* Oops, went back past this buffer. */
744 /* Reset trigger stage. */
745 ctx->trigger_stage = 0;
750 if (ctx->trigger_stage == TRIGGER_FIRED) {
751 /* Send the incoming transfer to the session bus. */
752 packet.type = SR_DF_LOGIC;
753 packet.payload = &logic;
754 logic.length = cur_buflen - trigger_offset;
756 logic.data = cur_buf + trigger_offset;
757 sr_session_send(ctx->session_dev_id, &packet);
760 ctx->num_samples += cur_buflen;
761 if (ctx->limit_samples &&
762 (unsigned int)ctx->num_samples > ctx->limit_samples) {
763 abort_acquisition(ctx);
767 * TODO: Buffer pre-trigger data in capture
768 * ratio-sized buffer.
773 static int hw_dev_acquisition_start(int dev_index, void *cb_data)
775 struct sr_dev_inst *sdi;
776 struct sr_datafeed_packet *packet;
777 struct sr_datafeed_header *header;
779 struct libusb_transfer *transfer;
780 const struct libusb_pollfd **lupfd;
784 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
787 ctx->session_dev_id = cb_data;
788 ctx->num_samples = 0;
790 if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
791 sr_err("fx2lafw: %s: packet malloc failed.", __func__);
792 return SR_ERR_MALLOC;
795 if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
796 sr_err("fx2lafw: %s: header malloc failed.", __func__);
797 return SR_ERR_MALLOC;
800 /* Start with 2K transfer, subsequently increased to 4K. */
802 for (i = 0; i < NUM_SIMUL_TRANSFERS; i++) {
803 if (!(buf = g_try_malloc(size))) {
804 sr_err("fx2lafw: %s: buf malloc failed.", __func__);
805 return SR_ERR_MALLOC;
807 transfer = libusb_alloc_transfer(0);
808 libusb_fill_bulk_transfer(transfer, ctx->usb->devhdl,
809 2 | LIBUSB_ENDPOINT_IN, buf, size,
810 receive_transfer, ctx, 40);
811 if (libusb_submit_transfer(transfer) != 0) {
812 /* TODO: Free them all. */
813 libusb_free_transfer(transfer);
818 ctx->submitted_transfers++;
822 lupfd = libusb_get_pollfds(usb_context);
823 for (i = 0; lupfd[i]; i++)
824 sr_source_add(lupfd[i]->fd, lupfd[i]->events,
825 40, receive_data, NULL);
826 free(lupfd); /* NOT g_free()! */
828 packet->type = SR_DF_HEADER;
829 packet->payload = header;
830 header->feed_version = 1;
831 gettimeofday(&header->starttime, NULL);
832 header->samplerate = ctx->cur_samplerate;
833 header->num_logic_probes = ctx->profile->num_probes;
834 sr_session_send(cb_data, packet);
838 if ((ret = command_start_acquisition (ctx->usb->devhdl,
839 ctx->cur_samplerate)) != SR_OK) {
846 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
847 static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
849 struct sr_dev_inst *sdi;
851 /* Avoid compiler warnings. */
854 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
857 abort_acquisition(sdi->priv);
862 SR_PRIV struct sr_dev_driver fx2lafw_driver_info = {
864 .longname = "fx2lafw (generic driver for FX2 based LAs)",
867 .cleanup = hw_cleanup,
868 .dev_open = hw_dev_open,
869 .dev_close = hw_dev_close,
870 .dev_info_get = hw_dev_info_get,
871 .dev_status_get = hw_dev_status_get,
872 .hwcap_get_all = hw_hwcap_get_all,
873 .dev_config_set = hw_dev_config_set,
874 .dev_acquisition_start = hw_dev_acquisition_start,
875 .dev_acquisition_stop = hw_dev_acquisition_stop,