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 hwcaps[] = {
57 SR_HWCAP_LOGIC_ANALYZER,
60 /* These are really implemented in the driver, not the hardware. */
61 SR_HWCAP_LIMIT_SAMPLES,
67 * TODO: Different probe_names[] for each supported device.
69 static const char *probe_names[] = {
81 static uint64_t supported_samplerates[] = {
96 static struct sr_samplerates samplerates = {
100 supported_samplerates,
103 static GSList *dev_insts = NULL;
104 static libusb_context *usb_context = NULL;
106 static int hw_dev_config_set(int dev_index, int hwcap, void *value);
107 static int hw_dev_acquisition_stop(int dev_index, void *cb_data);
110 * Check the USB configuration to determine if this is an fx2lafw device.
112 * @return TRUE if the device's configuration profile match fx2lafw
113 * configuration, FALSE otherwise.
115 static gboolean check_conf_profile(libusb_device *dev)
117 struct libusb_device_descriptor des;
118 struct libusb_config_descriptor *conf_dsc = NULL;
119 const struct libusb_interface_descriptor *intf_dsc;
120 gboolean ret = FALSE;
123 /* Assume the FW has not been loaded, unless proven wrong. */
126 if (libusb_get_device_descriptor(dev, &des) != 0)
129 /* Need exactly 1 configuration. */
130 if (des.bNumConfigurations != 1)
133 if (libusb_get_config_descriptor(dev, 0, &conf_dsc) != 0)
136 /* Need exactly 1 interface. */
137 if (conf_dsc->bNumInterfaces != 1)
140 /* Need just one alternate setting. */
141 if (conf_dsc->interface[0].num_altsetting != 1)
144 /* Need exactly 2 endpoints. */
145 intf_dsc = &(conf_dsc->interface[0].altsetting[0]);
146 if (intf_dsc->bNumEndpoints != 2)
149 /* The first endpoint should be 2 (inbound). */
150 if ((intf_dsc->endpoint[0].bEndpointAddress & 0x8f) !=
151 (2 | LIBUSB_ENDPOINT_IN))
154 /* If we made it here, it must be an fx2lafw. */
159 libusb_free_config_descriptor(conf_dsc);
164 static int fx2lafw_dev_open(int dev_index)
166 libusb_device **devlist;
167 struct libusb_device_descriptor des;
168 struct sr_dev_inst *sdi;
170 struct version_info vi;
173 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
177 if (sdi->status == SR_ST_ACTIVE)
182 const int device_count = libusb_get_device_list(usb_context, &devlist);
183 if (device_count < 0) {
184 sr_err("fx2lafw: Failed to retrieve device list (%d)",
189 for (i = 0; i < device_count; i++) {
190 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
191 sr_err("fx2lafw: Failed to get device descriptor: %d.",
196 if (des.idVendor != ctx->profile->vid
197 || des.idProduct != ctx->profile->pid)
200 if (sdi->status == SR_ST_INITIALIZING) {
201 if (skip != dev_index) {
202 /* Skip devices of this type that aren't the one we want. */
206 } else if (sdi->status == SR_ST_INACTIVE) {
208 * This device is fully enumerated, so we need to find
209 * this device by vendor, product, bus and address.
211 if (libusb_get_bus_number(devlist[i]) != ctx->usb->bus
212 || libusb_get_device_address(devlist[i]) != ctx->usb->address)
213 /* this is not the one */
217 if (!(ret = libusb_open(devlist[i], &ctx->usb->devhdl))) {
218 if (ctx->usb->address == 0xff)
220 * first time we touch this device after firmware upload,
221 * so we don't know the address yet.
223 ctx->usb->address = libusb_get_device_address(devlist[i]);
225 sr_err("fx2lafw: Failed to open device: %d.", ret);
229 ret = command_get_fw_version(ctx->usb->devhdl, &vi);
231 sr_err("fx2lafw: Failed to retrieve "
232 "firmware version information.");
236 if (vi.major != FX2LAFW_VERSION_MAJOR ||
237 vi.minor != FX2LAFW_VERSION_MINOR) {
238 sr_err("fx2lafw: Expected firmware version %d.%02d "
239 "got %d.%02d.", FX2LAFW_VERSION_MAJOR,
240 FX2LAFW_VERSION_MINOR, vi.major, vi.minor);
244 sdi->status = SR_ST_ACTIVE;
245 sr_info("fx2lafw: Opened device %d on %d.%d "
246 "interface %d, firmware version %d.%02d",
247 sdi->index, ctx->usb->bus, ctx->usb->address,
248 USB_INTERFACE, vi.major, vi.minor);
252 libusb_free_device_list(devlist, 1);
254 if (sdi->status != SR_ST_ACTIVE)
260 static void close_dev(struct sr_dev_inst *sdi)
266 if (ctx->usb->devhdl == NULL)
269 sr_info("fx2lafw: Closing device %d on %d.%d interface %d.",
270 sdi->index, ctx->usb->bus, ctx->usb->address, USB_INTERFACE);
271 libusb_release_interface(ctx->usb->devhdl, USB_INTERFACE);
272 libusb_close(ctx->usb->devhdl);
273 ctx->usb->devhdl = NULL;
274 sdi->status = SR_ST_INACTIVE;
277 static int configure_probes(struct context *ctx, GSList *probes)
279 struct sr_probe *probe;
281 int probe_bit, stage, i;
284 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
285 ctx->trigger_mask[i] = 0;
286 ctx->trigger_value[i] = 0;
290 for (l = probes; l; l = l->next) {
291 probe = (struct sr_probe *)l->data;
292 if (probe->enabled == FALSE)
294 probe_bit = 1 << (probe->index - 1);
295 if (!(probe->trigger))
299 for (tc = probe->trigger; *tc; tc++) {
300 ctx->trigger_mask[stage] |= probe_bit;
302 ctx->trigger_value[stage] |= probe_bit;
304 if (stage > NUM_TRIGGER_STAGES)
311 * We didn't configure any triggers, make sure acquisition
312 * doesn't wait for any.
314 ctx->trigger_stage = TRIGGER_FIRED;
316 ctx->trigger_stage = 0;
321 static struct context *fx2lafw_dev_new(void)
325 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
326 sr_err("fx2lafw: %s: ctx malloc failed.", __func__);
330 ctx->trigger_stage = TRIGGER_FIRED;
339 static int hw_init(const char *devinfo)
341 struct sr_dev_inst *sdi;
342 struct libusb_device_descriptor des;
343 const struct fx2lafw_profile *prof;
345 libusb_device **devlist;
350 /* Avoid compiler warnings. */
353 if (libusb_init(&usb_context) != 0) {
354 sr_warn("fx2lafw: Failed to initialize libusb.");
358 /* Find all fx2lafw compatible devices and upload firware to them. */
359 libusb_get_device_list(usb_context, &devlist);
360 for (i = 0; devlist[i]; i++) {
362 if ((ret = libusb_get_device_descriptor(
363 devlist[i], &des)) != 0) {
364 sr_warn("fx2lafw: Failed to get device descriptor: %d.", ret);
369 for (j = 0; supported_fx2[j].vid; j++) {
370 if (des.idVendor == supported_fx2[j].vid &&
371 des.idProduct == supported_fx2[j].pid) {
372 prof = &supported_fx2[j];
376 /* Skip if the device was not found */
380 sdi = sr_dev_inst_new(devcnt, SR_ST_INITIALIZING,
381 prof->vendor, prof->model, prof->model_version);
385 ctx = fx2lafw_dev_new();
388 dev_insts = g_slist_append(dev_insts, sdi);
390 if (check_conf_profile(devlist[i])) {
391 /* Already has the firmware, so fix the new address. */
392 sr_dbg("fx2lafw: Found an fx2lafw device.");
393 sdi->status = SR_ST_INACTIVE;
394 ctx->usb = sr_usb_dev_inst_new
395 (libusb_get_bus_number(devlist[i]),
396 libusb_get_device_address(devlist[i]), NULL);
398 if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION,
399 prof->firmware) == SR_OK)
400 /* Remember when the firmware on this device was updated */
401 g_get_current_time(&ctx->fw_updated);
403 sr_err("fx2lafw: Firmware upload failed for "
404 "device %d.", devcnt);
405 ctx->usb = sr_usb_dev_inst_new
406 (libusb_get_bus_number(devlist[i]), 0xff, NULL);
411 libusb_free_device_list(devlist, 1);
416 static int hw_dev_open(int dev_index)
419 struct sr_dev_inst *sdi;
423 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
428 * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY ms
429 * for the FX2 to renumerate.
432 if (GTV_TO_MSEC(ctx->fw_updated) > 0) {
433 sr_info("fx2lafw: Waiting for device to reset.");
434 /* takes at least 300ms for the FX2 to be gone from the USB bus */
435 g_usleep(300 * 1000);
437 while (timediff < MAX_RENUM_DELAY) {
438 if ((ret = fx2lafw_dev_open(dev_index)) == SR_OK)
440 g_usleep(100 * 1000);
441 g_get_current_time(&cur_time);
442 timediff = GTV_TO_MSEC(cur_time) - GTV_TO_MSEC(ctx->fw_updated);
444 sr_info("fx2lafw: Device came back after %d ms.", timediff);
446 ret = fx2lafw_dev_open(dev_index);
450 sr_err("fx2lafw: Unable to open device.");
455 ret = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
457 sr_err("fx2lafw: Unable to claim interface: %d.", ret);
461 if (ctx->cur_samplerate == 0) {
462 /* Samplerate hasn't been set; default to the slowest one. */
463 if (hw_dev_config_set(dev_index, SR_HWCAP_SAMPLERATE,
464 &supported_samplerates[0]) == SR_ERR)
471 static int hw_dev_close(int dev_index)
473 struct sr_dev_inst *sdi;
475 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
476 sr_err("fx2lafw: %s: sdi was NULL.", __func__);
486 static int hw_cleanup(void)
489 struct sr_dev_inst *sdi;
493 for (l = dev_insts; l; l = l->next) {
494 if (!(sdi = l->data)) {
495 /* Log error, but continue cleaning up the rest. */
496 sr_err("fx2lafw: %s: sdi was NULL, continuing.",
501 if (!(ctx = sdi->priv)) {
502 /* Log error, but continue cleaning up the rest. */
503 sr_err("fx2lafw: %s: sdi->priv was NULL, continuing",
510 sr_dev_inst_free(sdi);
513 g_slist_free(dev_insts);
517 libusb_exit(usb_context);
523 static void *hw_dev_info_get(int dev_index, int dev_info_id)
525 struct sr_dev_inst *sdi;
528 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
532 switch (dev_info_id) {
535 case SR_DI_NUM_PROBES:
536 return GINT_TO_POINTER(ctx->profile->num_probes);
537 case SR_DI_PROBE_NAMES:
539 case SR_DI_SAMPLERATES:
541 case SR_DI_TRIGGER_TYPES:
542 return TRIGGER_TYPES;
543 case SR_DI_CUR_SAMPLERATE:
544 return &ctx->cur_samplerate;
550 static int hw_dev_status_get(int dev_index)
552 const struct sr_dev_inst *const sdi =
553 sr_dev_inst_get(dev_insts, dev_index);
556 return SR_ST_NOT_FOUND;
561 static int *hw_hwcap_get_all(void)
566 static int hw_dev_config_set(int dev_index, int hwcap, void *value)
568 struct sr_dev_inst *sdi;
572 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
576 if (hwcap == SR_HWCAP_SAMPLERATE) {
577 ctx->cur_samplerate = *(uint64_t *)value;
579 } else if (hwcap == SR_HWCAP_PROBECONFIG) {
580 ret = configure_probes(ctx, (GSList *) value);
581 } else if (hwcap == SR_HWCAP_LIMIT_SAMPLES) {
582 ctx->limit_samples = *(uint64_t *)value;
591 static int receive_data(int fd, int revents, void *cb_data)
595 /* Avoid compiler warnings. */
600 tv.tv_sec = tv.tv_usec = 0;
601 libusb_handle_events_timeout(usb_context, &tv);
606 static void abort_acquisition(struct context *ctx)
608 ctx->num_samples = -1;
611 static void finish_acquisition(struct context *ctx)
613 struct sr_datafeed_packet packet;
616 /* Terminate session */
617 packet.type = SR_DF_END;
618 sr_session_send(ctx->session_dev_id, &packet);
620 /* Remove fds from polling */
621 const struct libusb_pollfd **const lupfd =
622 libusb_get_pollfds(usb_context);
623 for (i = 0; lupfd[i]; i++)
624 sr_source_remove(lupfd[i]->fd);
625 free(lupfd); /* NOT g_free()! */
628 static void receive_transfer(struct libusb_transfer *transfer)
630 /* TODO: These statics have to move to the ctx struct. */
631 static int empty_transfer_count = 0;
632 struct sr_datafeed_packet packet;
633 struct sr_datafeed_logic logic;
634 struct context *ctx = transfer->user_data;
635 int cur_buflen, trigger_offset, i;
636 unsigned char *cur_buf, *new_buf;
639 * If acquisition has already ended, just free any queued up
640 * transfer that come in.
642 if (ctx->num_samples == -1) {
644 libusb_free_transfer(transfer);
646 ctx->submitted_transfers--;
647 if (ctx->submitted_transfers == 0)
648 finish_acquisition(ctx);
653 sr_info("fx2lafw: receive_transfer(): status %d received %d bytes.",
654 transfer->status, transfer->actual_length);
656 /* Save incoming transfer before reusing the transfer struct. */
657 cur_buf = transfer->buffer;
658 cur_buflen = transfer->actual_length;
660 /* Fire off a new request. */
661 if (!(new_buf = g_try_malloc(4096))) {
662 sr_err("fx2lafw: %s: new_buf malloc failed.", __func__);
663 return; /* TODO: SR_ERR_MALLOC */
666 transfer->buffer = new_buf;
667 transfer->length = 4096;
668 if (libusb_submit_transfer(transfer) != 0) {
669 /* TODO: Stop session? */
670 /* TODO: Better error message. */
671 sr_err("fx2lafw: %s: libusb_submit_transfer error.", __func__);
674 if (cur_buflen == 0) {
675 empty_transfer_count++;
676 if (empty_transfer_count > MAX_EMPTY_TRANSFERS) {
678 * The FX2 gave up. End the acquisition, the frontend
679 * will work out that the samplecount is short.
681 abort_acquisition(ctx);
685 empty_transfer_count = 0;
689 if (ctx->trigger_stage >= 0) {
690 for (i = 0; i < cur_buflen; i++) {
692 if ((cur_buf[i] & ctx->trigger_mask[ctx->trigger_stage]) == ctx->trigger_value[ctx->trigger_stage]) {
693 /* Match on this trigger stage. */
694 ctx->trigger_buffer[ctx->trigger_stage] = cur_buf[i];
695 ctx->trigger_stage++;
697 if (ctx->trigger_stage == NUM_TRIGGER_STAGES || ctx->trigger_mask[ctx->trigger_stage] == 0) {
698 /* Match on all trigger stages, we're done. */
699 trigger_offset = i + 1;
702 * TODO: Send pre-trigger buffer to session bus.
703 * Tell the frontend we hit the trigger here.
705 packet.type = SR_DF_TRIGGER;
706 packet.payload = NULL;
707 sr_session_send(ctx->session_dev_id, &packet);
710 * Send the samples that triggered it, since we're
711 * skipping past them.
713 packet.type = SR_DF_LOGIC;
714 packet.payload = &logic;
715 logic.length = ctx->trigger_stage;
717 logic.data = ctx->trigger_buffer;
718 sr_session_send(ctx->session_dev_id, &packet);
720 ctx->trigger_stage = TRIGGER_FIRED;
727 * We had a match before, but not in the next sample. However, we may
728 * have a match on this stage in the next bit -- trigger on 0001 will
729 * fail on seeing 00001, so we need to go back to stage 0 -- but at
730 * the next sample from the one that matched originally, which the
731 * counter increment at the end of the loop takes care of.
733 if (ctx->trigger_stage > 0) {
734 i -= ctx->trigger_stage;
736 i = -1; /* Oops, went back past this buffer. */
737 /* Reset trigger stage. */
738 ctx->trigger_stage = 0;
743 if (ctx->trigger_stage == TRIGGER_FIRED) {
744 /* Send the incoming transfer to the session bus. */
745 packet.type = SR_DF_LOGIC;
746 packet.payload = &logic;
747 logic.length = cur_buflen - trigger_offset;
749 logic.data = cur_buf + trigger_offset;
750 sr_session_send(ctx->session_dev_id, &packet);
753 ctx->num_samples += cur_buflen;
754 if (ctx->limit_samples &&
755 (unsigned int)ctx->num_samples > ctx->limit_samples) {
756 abort_acquisition(ctx);
760 * TODO: Buffer pre-trigger data in capture
761 * ratio-sized buffer.
766 static int hw_dev_acquisition_start(int dev_index, void *cb_data)
768 struct sr_dev_inst *sdi;
769 struct sr_datafeed_packet *packet;
770 struct sr_datafeed_header *header;
772 struct libusb_transfer *transfer;
773 const struct libusb_pollfd **lupfd;
777 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
780 ctx->session_dev_id = cb_data;
781 ctx->num_samples = 0;
783 if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
784 sr_err("fx2lafw: %s: packet malloc failed.", __func__);
785 return SR_ERR_MALLOC;
788 if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
789 sr_err("fx2lafw: %s: header malloc failed.", __func__);
790 return SR_ERR_MALLOC;
793 /* Start with 2K transfer, subsequently increased to 4K. */
795 for (i = 0; i < NUM_SIMUL_TRANSFERS; i++) {
796 if (!(buf = g_try_malloc(size))) {
797 sr_err("fx2lafw: %s: buf malloc failed.", __func__);
798 return SR_ERR_MALLOC;
800 transfer = libusb_alloc_transfer(0);
801 libusb_fill_bulk_transfer(transfer, ctx->usb->devhdl,
802 2 | LIBUSB_ENDPOINT_IN, buf, size,
803 receive_transfer, ctx, 40);
804 if (libusb_submit_transfer(transfer) != 0) {
805 /* TODO: Free them all. */
806 libusb_free_transfer(transfer);
811 ctx->submitted_transfers++;
815 lupfd = libusb_get_pollfds(usb_context);
816 for (i = 0; lupfd[i]; i++)
817 sr_source_add(lupfd[i]->fd, lupfd[i]->events,
818 40, receive_data, NULL);
819 free(lupfd); /* NOT g_free()! */
821 packet->type = SR_DF_HEADER;
822 packet->payload = header;
823 header->feed_version = 1;
824 gettimeofday(&header->starttime, NULL);
825 header->samplerate = ctx->cur_samplerate;
826 header->num_logic_probes = ctx->profile->num_probes;
827 sr_session_send(cb_data, packet);
831 if ((ret = command_start_acquisition (ctx->usb->devhdl,
832 ctx->cur_samplerate)) != SR_OK) {
839 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
840 static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
842 struct sr_dev_inst *sdi;
844 /* Avoid compiler warnings. */
847 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
850 abort_acquisition(sdi->priv);
855 SR_PRIV struct sr_dev_driver fx2lafw_driver_info = {
857 .longname = "fx2lafw (generic driver for FX2 based LAs)",
860 .cleanup = hw_cleanup,
861 .dev_open = hw_dev_open,
862 .dev_close = hw_dev_close,
863 .dev_info_get = hw_dev_info_get,
864 .dev_status_get = hw_dev_status_get,
865 .hwcap_get_all = hw_hwcap_get_all,
866 .dev_config_set = hw_dev_config_set,
867 .dev_acquisition_start = hw_dev_acquisition_start,
868 .dev_acquisition_stop = hw_dev_acquisition_stop,