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 },
53 { 0, 0, 0, 0, 0, 0, 0 }
56 static int fx2lafw_capabilities[] = {
57 SR_HWCAP_LOGIC_ANALYZER,
60 /* These are really implemented in the driver, not the hardware. */
61 SR_HWCAP_LIMIT_SAMPLES,
66 static const char *fx2lafw_probe_names[] = {
78 static uint64_t supported_samplerates[] = {
90 static struct sr_samplerates samplerates = {
94 supported_samplerates,
97 static GSList *dev_insts = NULL;
98 static libusb_context *usb_context = NULL;
100 static int hw_dev_config_set(int dev_index, int hwcap, void *value);
101 static int hw_dev_acquisition_stop(int dev_index, void *session_dev_id);
104 * Check the USB configuration to determine if this is an fx2lafw device.
106 * @return TRUE if the device's configuration profile match fx2lafw
107 * configuration, FALSE otherwise.
109 static gboolean check_conf_profile(libusb_device *dev)
111 struct libusb_device_descriptor des;
112 struct libusb_config_descriptor *conf_dsc = NULL;
113 const struct libusb_interface_descriptor *intf_dsc;
114 gboolean ret = FALSE;
117 /* Assume the firmware has not been loaded, unless proven wrong. */
120 if (libusb_get_device_descriptor(dev, &des) != 0)
123 if (des.bNumConfigurations != 1)
124 /* Need exactly 1 configuration. */
127 if (libusb_get_config_descriptor(dev, 0, &conf_dsc) != 0)
130 if (conf_dsc->bNumInterfaces != 1)
131 /* Need exactly 1 interface. */
134 if (conf_dsc->interface[0].num_altsetting != 1)
135 /* Need just one alternate setting. */
138 intf_dsc = &(conf_dsc->interface[0].altsetting[0]);
139 if (intf_dsc->bNumEndpoints != 2)
140 /* Need exactly 2 end points. */
143 if ((intf_dsc->endpoint[0].bEndpointAddress & 0x8f) !=
144 (2 | LIBUSB_ENDPOINT_IN)) // 0x82
145 /* The first endpoint should be 2 (inbound). */
148 /* TODO: Check the debug channel... */
150 /* If we made it here, it must be an fx2lafw. */
155 libusb_free_config_descriptor(conf_dsc);
160 static int fx2lafw_open_dev(int dev_index)
162 libusb_device **devlist;
163 struct libusb_device_descriptor des;
164 struct sr_dev_inst *sdi;
168 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
172 if (sdi->status == SR_ST_ACTIVE)
177 libusb_get_device_list(usb_context, &devlist);
178 for (i = 0; devlist[i]; i++) {
179 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
180 sr_err("fx2lafw: Failed to get device descriptor: %d.",
185 if (des.idVendor != ctx->profile->vid
186 || des.idProduct != ctx->profile->pid)
189 if (sdi->status == SR_ST_INITIALIZING) {
190 if (skip != dev_index) {
191 /* Skip devices of this type that aren't the one we want. */
195 } else if (sdi->status == SR_ST_INACTIVE) {
197 * This device is fully enumerated, so we need to find
198 * this device by vendor, product, bus and address.
200 if (libusb_get_bus_number(devlist[i]) != ctx->usb->bus
201 || libusb_get_device_address(devlist[i]) != ctx->usb->address)
202 /* this is not the one */
206 if (!(ret = libusb_open(devlist[i], &ctx->usb->devhdl))) {
207 if (ctx->usb->address == 0xff)
209 * first time we touch this device after firmware upload,
210 * so we don't know the address yet.
212 ctx->usb->address = libusb_get_device_address(devlist[i]);
214 sdi->status = SR_ST_ACTIVE;
215 sr_info("fx2lafw: Opened device %d on %d.%d "
216 "interface %d.", sdi->index, ctx->usb->bus,
217 ctx->usb->address, USB_INTERFACE);
219 sr_err("fx2lafw: Failed to open device: %d.", ret);
222 /* if we made it here, we handled the device one way or another */
225 libusb_free_device_list(devlist, 1);
227 if (sdi->status != SR_ST_ACTIVE)
233 static void close_dev(struct sr_dev_inst *sdi)
239 if (ctx->usb->devhdl == NULL)
242 sr_info("fx2lafw: Closing device %d on %d.%d interface %d.",
243 sdi->index, ctx->usb->bus, ctx->usb->address, USB_INTERFACE);
244 libusb_release_interface(ctx->usb->devhdl, USB_INTERFACE);
245 libusb_close(ctx->usb->devhdl);
246 ctx->usb->devhdl = NULL;
247 sdi->status = SR_ST_INACTIVE;
250 static int configure_probes(struct context *ctx, GSList *probes)
252 struct sr_probe *probe;
254 int probe_bit, stage, i;
257 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
258 ctx->trigger_mask[i] = 0;
259 ctx->trigger_value[i] = 0;
263 for (l = probes; l; l = l->next) {
264 probe = (struct sr_probe *)l->data;
265 if (probe->enabled == FALSE)
267 probe_bit = 1 << (probe->index - 1);
268 if (!(probe->trigger))
272 for (tc = probe->trigger; *tc; tc++) {
273 ctx->trigger_mask[stage] |= probe_bit;
275 ctx->trigger_value[stage] |= probe_bit;
277 if (stage > NUM_TRIGGER_STAGES)
284 * We didn't configure any triggers, make sure acquisition
285 * doesn't wait for any.
287 ctx->trigger_stage = TRIGGER_FIRED;
289 ctx->trigger_stage = 0;
294 static struct context *fx2lafw_device_new(void)
298 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
299 sr_err("fx2lafw: %s: ctx malloc failed.", __func__);
303 ctx->trigger_stage = TRIGGER_FIRED;
312 static int hw_init(const char *deviceinfo)
314 struct sr_dev_inst *sdi;
315 struct libusb_device_descriptor des;
316 const struct fx2lafw_profile *fx2lafw_prof;
318 libusb_device **devlist;
323 /* Avoid compiler warnings. */
326 if (libusb_init(&usb_context) != 0) {
327 sr_warn("fx2lafw: Failed to initialize libusb.");
331 /* Find all fx2lafw compatible devices and upload firware to them. */
332 libusb_get_device_list(usb_context, &devlist);
333 for (i = 0; devlist[i]; i++) {
335 if ((ret = libusb_get_device_descriptor(
336 devlist[i], &des)) != 0) {
337 sr_warn("fx2lafw: Failed to get device descriptor: %d.", ret);
342 for (j = 0; supported_fx2[j].vid; j++) {
343 if (des.idVendor == supported_fx2[j].vid &&
344 des.idProduct == supported_fx2[j].pid) {
345 fx2lafw_prof = &supported_fx2[j];
349 /* Skip if the device was not found */
353 sdi = sr_dev_inst_new(devcnt, SR_ST_INITIALIZING,
354 fx2lafw_prof->vendor, fx2lafw_prof->model,
355 fx2lafw_prof->model_version);
359 ctx = fx2lafw_device_new();
360 ctx->profile = fx2lafw_prof;
362 dev_insts = g_slist_append(dev_insts, sdi);
364 if (check_conf_profile(devlist[i])) {
365 /* Already has the firmware, so fix the new address. */
366 sr_dbg("fx2lafw: Found an fx2lafw device.");
367 sdi->status = SR_ST_INACTIVE;
368 ctx->usb = sr_usb_dev_inst_new
369 (libusb_get_bus_number(devlist[i]),
370 libusb_get_device_address(devlist[i]), NULL);
372 if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION,
373 fx2lafw_prof->firmware) == SR_OK)
374 /* Remember when the firmware on this device was updated */
375 g_get_current_time(&ctx->fw_updated);
377 sr_err("fx2lafw: Firmware upload failed for "
378 "device %d.", devcnt);
379 ctx->usb = sr_usb_dev_inst_new
380 (libusb_get_bus_number(devlist[i]), 0xff, NULL);
385 libusb_free_device_list(devlist, 1);
390 static int hw_dev_open(int dev_index)
393 struct sr_dev_inst *sdi;
397 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
402 * if the firmware was recently uploaded, wait up to MAX_RENUM_DELAY ms
403 * for the FX2 to renumerate
406 if (GTV_TO_MSEC(ctx->fw_updated) > 0) {
407 sr_info("fx2lafw: Waiting for device to reset.");
408 /* takes at least 300ms for the FX2 to be gone from the USB bus */
409 g_usleep(300 * 1000);
411 while (timediff < MAX_RENUM_DELAY) {
412 if ((ret = fx2lafw_open_dev(dev_index)) == SR_OK)
414 g_usleep(100 * 1000);
415 g_get_current_time(&cur_time);
416 timediff = GTV_TO_MSEC(cur_time) - GTV_TO_MSEC(ctx->fw_updated);
418 sr_info("fx2lafw: Device came back after %d ms.", timediff);
420 ret = fx2lafw_open_dev(dev_index);
424 sr_err("fx2lafw: Unable to open device.");
429 ret = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
431 sr_err("fx2lafw: Unable to claim interface: %d.", ret);
435 if (ctx->cur_samplerate == 0) {
436 /* Samplerate hasn't been set; default to the slowest one. */
437 if (hw_dev_config_set(dev_index, SR_HWCAP_SAMPLERATE,
438 &supported_samplerates[0]) == SR_ERR)
445 static int hw_dev_close(int dev_index)
447 struct sr_dev_inst *sdi;
449 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
450 sr_err("fx2lafw: %s: sdi was NULL.", __func__);
460 static int hw_cleanup(void)
463 struct sr_dev_inst *sdi;
467 for (l = dev_insts; l; l = l->next) {
468 if (!(sdi = l->data)) {
469 /* Log error, but continue cleaning up the rest. */
470 sr_err("fx2lafw: %s: sdi was NULL, continuing.",
475 if (!(ctx = sdi->priv)) {
476 /* Log error, but continue cleaning up the rest. */
477 sr_err("fx2lafw: %s: sdi->priv was NULL, continuing",
484 sr_dev_inst_free(sdi);
487 g_slist_free(dev_insts);
491 libusb_exit(usb_context);
497 static void *hw_dev_info_get(int dev_index, int dev_info_id)
499 struct sr_dev_inst *sdi;
502 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
506 switch (dev_info_id) {
509 case SR_DI_NUM_PROBES:
510 return GINT_TO_POINTER(ctx->profile->num_probes);
511 case SR_DI_PROBE_NAMES:
512 return fx2lafw_probe_names;
513 case SR_DI_SAMPLERATES:
515 case SR_DI_TRIGGER_TYPES:
516 return TRIGGER_TYPES;
517 case SR_DI_CUR_SAMPLERATE:
518 return &ctx->cur_samplerate;
524 static int hw_dev_status_get(int dev_index)
526 const struct sr_dev_inst *const sdi =
527 sr_dev_inst_get(dev_insts, dev_index);
530 return SR_ST_NOT_FOUND;
535 static int *hw_hwcap_get_all(void)
537 return fx2lafw_capabilities;
540 static int hw_dev_config_set(int dev_index, int hwcap, void *value)
542 struct sr_dev_inst *sdi;
546 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
550 if (hwcap == SR_HWCAP_SAMPLERATE) {
551 ctx->cur_samplerate = *(uint64_t *)value;
553 } else if (hwcap == SR_HWCAP_PROBECONFIG) {
554 ret = configure_probes(ctx, (GSList *) value);
555 } else if (hwcap == SR_HWCAP_LIMIT_SAMPLES) {
556 ctx->limit_samples = *(uint64_t *)value;
565 static int receive_data(int fd, int revents, void *cb_data)
569 /* Avoid compiler warnings. */
574 tv.tv_sec = tv.tv_usec = 0;
575 libusb_handle_events_timeout(usb_context, &tv);
580 static void abort_acquisition(struct context *ctx)
582 ctx->num_samples = -1;
585 static void finish_acquisition(struct context *ctx)
587 struct sr_datafeed_packet packet;
590 /* Terminate session */
591 packet.type = SR_DF_END;
592 sr_session_send(ctx->session_dev_id, &packet);
594 /* Remove fds from polling */
595 const struct libusb_pollfd **const lupfd =
596 libusb_get_pollfds(usb_context);
597 for (i = 0; lupfd[i]; i++)
598 sr_source_remove(lupfd[i]->fd);
599 free(lupfd); /* NOT g_free()! */
602 static void receive_transfer(struct libusb_transfer *transfer)
604 /* TODO: These statics have to move to the ctx struct. */
605 static int empty_transfer_count = 0;
606 struct sr_datafeed_packet packet;
607 struct sr_datafeed_logic logic;
608 struct context *ctx = transfer->user_data;
609 int cur_buflen, trigger_offset, i;
610 unsigned char *cur_buf, *new_buf;
613 * If acquisition has already ended, just free any queued up
614 * transfer that come in.
616 if (ctx->num_samples == -1) {
618 libusb_free_transfer(transfer);
620 ctx->submitted_transfers--;
621 if (ctx->submitted_transfers == 0)
622 finish_acquisition(ctx);
627 sr_info("fx2lafw: receive_transfer(): status %d received %d bytes.",
628 transfer->status, transfer->actual_length);
630 /* Save incoming transfer before reusing the transfer struct. */
631 cur_buf = transfer->buffer;
632 cur_buflen = transfer->actual_length;
634 /* Fire off a new request. */
635 if (!(new_buf = g_try_malloc(4096))) {
636 sr_err("fx2lafw: %s: new_buf malloc failed.", __func__);
637 return; /* TODO: SR_ERR_MALLOC */
640 transfer->buffer = new_buf;
641 transfer->length = 4096;
642 if (libusb_submit_transfer(transfer) != 0) {
643 /* TODO: Stop session? */
644 /* TODO: Better error message. */
645 sr_err("fx2lafw: %s: libusb_submit_transfer error.", __func__);
648 if (cur_buflen == 0) {
649 empty_transfer_count++;
650 if (empty_transfer_count > MAX_EMPTY_TRANSFERS) {
652 * The FX2 gave up. End the acquisition, the frontend
653 * will work out that the samplecount is short.
655 abort_acquisition(ctx);
659 empty_transfer_count = 0;
663 if (ctx->trigger_stage >= 0) {
664 for (i = 0; i < cur_buflen; i++) {
666 if ((cur_buf[i] & ctx->trigger_mask[ctx->trigger_stage]) == ctx->trigger_value[ctx->trigger_stage]) {
667 /* Match on this trigger stage. */
668 ctx->trigger_buffer[ctx->trigger_stage] = cur_buf[i];
669 ctx->trigger_stage++;
671 if (ctx->trigger_stage == NUM_TRIGGER_STAGES || ctx->trigger_mask[ctx->trigger_stage] == 0) {
672 /* Match on all trigger stages, we're done. */
673 trigger_offset = i + 1;
676 * TODO: Send pre-trigger buffer to session bus.
677 * Tell the frontend we hit the trigger here.
679 packet.type = SR_DF_TRIGGER;
680 packet.payload = NULL;
681 sr_session_send(ctx->session_dev_id, &packet);
684 * Send the samples that triggered it, since we're
685 * skipping past them.
687 packet.type = SR_DF_LOGIC;
688 packet.payload = &logic;
689 logic.length = ctx->trigger_stage;
691 logic.data = ctx->trigger_buffer;
692 sr_session_send(ctx->session_dev_id, &packet);
694 ctx->trigger_stage = TRIGGER_FIRED;
701 * We had a match before, but not in the next sample. However, we may
702 * have a match on this stage in the next bit -- trigger on 0001 will
703 * fail on seeing 00001, so we need to go back to stage 0 -- but at
704 * the next sample from the one that matched originally, which the
705 * counter increment at the end of the loop takes care of.
707 if (ctx->trigger_stage > 0) {
708 i -= ctx->trigger_stage;
710 i = -1; /* Oops, went back past this buffer. */
711 /* Reset trigger stage. */
712 ctx->trigger_stage = 0;
717 if (ctx->trigger_stage == TRIGGER_FIRED) {
718 /* Send the incoming transfer to the session bus. */
719 packet.type = SR_DF_LOGIC;
720 packet.payload = &logic;
721 logic.length = cur_buflen - trigger_offset;
723 logic.data = cur_buf + trigger_offset;
724 sr_session_send(ctx->session_dev_id, &packet);
727 ctx->num_samples += cur_buflen;
728 if (ctx->limit_samples &&
729 (unsigned int)ctx->num_samples > ctx->limit_samples) {
730 abort_acquisition(ctx);
734 * TODO: Buffer pre-trigger data in capture
735 * ratio-sized buffer.
740 static int hw_dev_acquisition_start(int dev_index, void *cb_data)
742 struct sr_dev_inst *sdi;
743 struct sr_datafeed_packet *packet;
744 struct sr_datafeed_header *header;
746 struct libusb_transfer *transfer;
747 const struct libusb_pollfd **lupfd;
751 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
754 ctx->session_dev_id = cb_data;
755 ctx->num_samples = 0;
757 if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
758 sr_err("fx2lafw: %s: packet malloc failed.", __func__);
759 return SR_ERR_MALLOC;
762 if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
763 sr_err("fx2lafw: %s: header malloc failed.", __func__);
764 return SR_ERR_MALLOC;
767 /* Start with 2K transfer, subsequently increased to 4K. */
769 for (i = 0; i < NUM_SIMUL_TRANSFERS; i++) {
770 if (!(buf = g_try_malloc(size))) {
771 sr_err("fx2lafw: %s: buf malloc failed.", __func__);
772 return SR_ERR_MALLOC;
774 transfer = libusb_alloc_transfer(0);
775 libusb_fill_bulk_transfer(transfer, ctx->usb->devhdl,
776 2 | LIBUSB_ENDPOINT_IN, buf, size,
777 receive_transfer, ctx, 40);
778 if (libusb_submit_transfer(transfer) != 0) {
779 /* TODO: Free them all. */
780 libusb_free_transfer(transfer);
785 ctx->submitted_transfers++;
789 lupfd = libusb_get_pollfds(usb_context);
790 for (i = 0; lupfd[i]; i++)
791 sr_source_add(lupfd[i]->fd, lupfd[i]->events,
792 40, receive_data, NULL);
793 free(lupfd); /* NOT g_free()! */
795 packet->type = SR_DF_HEADER;
796 packet->payload = header;
797 header->feed_version = 1;
798 gettimeofday(&header->starttime, NULL);
799 header->samplerate = ctx->cur_samplerate;
800 header->num_logic_probes = ctx->profile->num_probes;
801 sr_session_send(cb_data, packet);
805 if ((ret = command_start_acquisition (ctx->usb->devhdl,
806 ctx->cur_samplerate)) != SR_OK) {
813 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
814 static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
816 struct sr_dev_inst *sdi;
818 /* Avoid compiler warnings. */
821 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
824 abort_acquisition(sdi->priv);
829 SR_PRIV struct sr_dev_driver fx2lafw_driver_info = {
831 .longname = "fx2lafw (generic driver for FX2 based LAs)",
834 .cleanup = hw_cleanup,
835 .dev_open = hw_dev_open,
836 .dev_close = hw_dev_close,
837 .dev_info_get = hw_dev_info_get,
838 .dev_status_get = hw_dev_status_get,
839 .hwcap_get_all = hw_hwcap_get_all,
840 .dev_config_set = hw_dev_config_set,
841 .dev_acquisition_start = hw_dev_acquisition_start,
842 .dev_acquisition_stop = hw_dev_acquisition_stop,