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[] = {
33 * EE Electronics ESLA201A
35 { 0x08a9, 0x0014, "CWAV", "USBee AX", NULL,
36 FIRMWARE_DIR "/fx2lafw-cwav-usbeeax.fw", 8 },
40 { 0x08a9, 0x0009, "CWAV", "USBee SX", NULL,
41 FIRMWARE_DIR "/fx2lafw-cwav-usbeesx.fw", 8 },
44 * EE Electronics ESLA100
47 { 0x0925, 0x3881, "Saleae", "Logic", NULL,
48 FIRMWARE_DIR "/fx2lafw-saleae-logic.fw", 8 },
50 { 0, 0, 0, 0, 0, 0, 0 }
53 static int fx2lafw_capabilities[] = {
54 SR_HWCAP_LOGIC_ANALYZER,
57 /* These are really implemented in the driver, not the hardware. */
58 SR_HWCAP_LIMIT_SAMPLES,
63 static const char *fx2lafw_probe_names[] = {
75 static uint64_t fx2lafw_supported_samplerates[] = {
87 static struct sr_samplerates fx2lafw_samplerates = {
91 fx2lafw_supported_samplerates,
94 static GSList *dev_insts = NULL;
95 static libusb_context *usb_context = NULL;
97 static int hw_dev_config_set(int dev_index, int hwcap, void *value);
98 static int hw_dev_acquisition_stop(int dev_index, void *session_dev_id);
101 * Check the USB configuration to determine if this is an fx2lafw device.
103 * @return TRUE if the device's configuration profile match fx2lafw
104 * configuration, FALSE otherwise.
106 static gboolean check_conf_profile(libusb_device *dev)
108 struct libusb_device_descriptor des;
109 struct libusb_config_descriptor *conf_dsc = NULL;
110 const struct libusb_interface_descriptor *intf_dsc;
111 gboolean ret = FALSE;
114 /* Assume the firmware has not been loaded, unless proven wrong. */
117 if (libusb_get_device_descriptor(dev, &des) != 0)
120 if (des.bNumConfigurations != 1)
121 /* Need exactly 1 configuration. */
124 if (libusb_get_config_descriptor(dev, 0, &conf_dsc) != 0)
127 if (conf_dsc->bNumInterfaces != 1)
128 /* Need exactly 1 interface. */
131 if (conf_dsc->interface[0].num_altsetting != 1)
132 /* Need just one alternate setting. */
135 intf_dsc = &(conf_dsc->interface[0].altsetting[0]);
136 if (intf_dsc->bNumEndpoints != 2)
137 /* Need exactly 2 end points. */
140 if ((intf_dsc->endpoint[0].bEndpointAddress & 0x8f) !=
141 (2 | LIBUSB_ENDPOINT_IN)) // 0x82
142 /* The first endpoint should be 2 (inbound). */
145 /* TODO: Check the debug channel... */
147 /* If we made it here, it must be an fx2lafw. */
152 libusb_free_config_descriptor(conf_dsc);
157 static int fx2lafw_open_dev(int dev_index)
159 libusb_device **devlist;
160 struct libusb_device_descriptor des;
161 struct sr_dev_inst *sdi;
165 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
169 if (sdi->status == SR_ST_ACTIVE)
174 libusb_get_device_list(usb_context, &devlist);
175 for (i = 0; devlist[i]; i++) {
176 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
177 sr_err("fx2lafw: failed to get device descriptor: %d", ret);
181 if (des.idVendor != ctx->profile->vid
182 || des.idProduct != ctx->profile->pid)
185 if (sdi->status == SR_ST_INITIALIZING) {
186 if (skip != dev_index) {
187 /* Skip devices of this type that aren't the one we want. */
191 } else if (sdi->status == SR_ST_INACTIVE) {
193 * This device is fully enumerated, so we need to find
194 * this device by vendor, product, bus and address.
196 if (libusb_get_bus_number(devlist[i]) != ctx->usb->bus
197 || libusb_get_device_address(devlist[i]) != ctx->usb->address)
198 /* this is not the one */
202 if (!(ret = libusb_open(devlist[i], &ctx->usb->devhdl))) {
203 if (ctx->usb->address == 0xff)
205 * first time we touch this device after firmware upload,
206 * so we don't know the address yet.
208 ctx->usb->address = libusb_get_device_address(devlist[i]);
210 sdi->status = SR_ST_ACTIVE;
211 sr_info("fx2lafw: opened device %d on %d.%d interface %d",
212 sdi->index, ctx->usb->bus,
213 ctx->usb->address, USB_INTERFACE);
215 sr_err("fx2lafw: failed to open device: %d", ret);
218 /* if we made it here, we handled the device one way or another */
221 libusb_free_device_list(devlist, 1);
223 if (sdi->status != SR_ST_ACTIVE)
229 static void close_dev(struct sr_dev_inst *sdi)
235 if (ctx->usb->devhdl == NULL)
238 sr_info("fx2lafw: closing device %d on %d.%d interface %d", sdi->index,
239 ctx->usb->bus, ctx->usb->address, USB_INTERFACE);
240 libusb_release_interface(ctx->usb->devhdl, USB_INTERFACE);
241 libusb_close(ctx->usb->devhdl);
242 ctx->usb->devhdl = NULL;
243 sdi->status = SR_ST_INACTIVE;
246 static int configure_probes(struct context *ctx, GSList *probes)
248 struct sr_probe *probe;
250 int probe_bit, stage, i;
253 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
254 ctx->trigger_mask[i] = 0;
255 ctx->trigger_value[i] = 0;
259 for (l = probes; l; l = l->next) {
260 probe = (struct sr_probe *)l->data;
261 if (probe->enabled == FALSE)
263 probe_bit = 1 << (probe->index - 1);
264 if (!(probe->trigger))
268 for (tc = probe->trigger; *tc; tc++) {
269 ctx->trigger_mask[stage] |= probe_bit;
271 ctx->trigger_value[stage] |= probe_bit;
273 if (stage > NUM_TRIGGER_STAGES)
280 * We didn't configure any triggers, make sure acquisition
281 * doesn't wait for any.
283 ctx->trigger_stage = TRIGGER_FIRED;
285 ctx->trigger_stage = 0;
290 static struct context *fx2lafw_device_new(void)
294 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
295 sr_err("fx2lafw: %s: ctx malloc failed", __func__);
299 ctx->trigger_stage = TRIGGER_FIRED;
308 static int hw_init(const char *deviceinfo)
310 struct sr_dev_inst *sdi;
311 struct libusb_device_descriptor des;
312 const struct fx2lafw_profile *fx2lafw_prof;
314 libusb_device **devlist;
319 /* Avoid compiler warnings. */
322 if (libusb_init(&usb_context) != 0) {
323 sr_warn("Failed to initialize USB.");
327 /* Find all fx2lafw compatible devices and upload firware to all of them. */
328 libusb_get_device_list(usb_context, &devlist);
329 for (i = 0; devlist[i]; i++) {
331 if ((ret = libusb_get_device_descriptor(
332 devlist[i], &des)) != 0) {
333 sr_warn("failed to get device descriptor: %d", ret);
338 for (j = 0; supported_fx2[j].vid; j++) {
339 if (des.idVendor == supported_fx2[j].vid &&
340 des.idProduct == supported_fx2[j].pid) {
341 fx2lafw_prof = &supported_fx2[j];
345 /* Skip if the device was not found */
349 sdi = sr_dev_inst_new(devcnt, SR_ST_INITIALIZING,
350 fx2lafw_prof->vendor, fx2lafw_prof->model,
351 fx2lafw_prof->model_version);
355 ctx = fx2lafw_device_new();
356 ctx->profile = fx2lafw_prof;
358 dev_insts = g_slist_append(dev_insts, sdi);
360 if (check_conf_profile(devlist[i])) {
361 /* Already has the firmware, so fix the new address. */
362 sr_dbg("fx2lafw: Found a fx2lafw device.");
363 sdi->status = SR_ST_INACTIVE;
364 ctx->usb = sr_usb_dev_inst_new
365 (libusb_get_bus_number(devlist[i]),
366 libusb_get_device_address(devlist[i]), NULL);
368 if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION,
369 fx2lafw_prof->firmware) == SR_OK)
370 /* Remember when the firmware on this device was updated */
371 g_get_current_time(&ctx->fw_updated);
373 sr_err("fx2lafw: firmware upload failed for "
374 "device %d", devcnt);
375 ctx->usb = sr_usb_dev_inst_new
376 (libusb_get_bus_number(devlist[i]), 0xff, NULL);
381 libusb_free_device_list(devlist, 1);
386 static int hw_dev_open(int dev_index)
389 struct sr_dev_inst *sdi;
393 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
398 * if the firmware was recently uploaded, wait up to MAX_RENUM_DELAY ms
399 * for the FX2 to renumerate
402 if (GTV_TO_MSEC(ctx->fw_updated) > 0) {
403 sr_info("fx2lafw: waiting for device to reset");
404 /* takes at least 300ms for the FX2 to be gone from the USB bus */
405 g_usleep(300 * 1000);
407 while (timediff < MAX_RENUM_DELAY) {
408 if ((ret = fx2lafw_open_dev(dev_index)) == SR_OK)
410 g_usleep(100 * 1000);
411 g_get_current_time(&cur_time);
412 timediff = GTV_TO_MSEC(cur_time) - GTV_TO_MSEC(ctx->fw_updated);
414 sr_info("fx2lafw: device came back after %d ms", timediff);
416 ret = fx2lafw_open_dev(dev_index);
420 sr_err("fx2lafw: unable to open device");
425 ret = libusb_claim_interface(ctx->usb->devhdl, USB_INTERFACE);
427 sr_err("fx2lafw: Unable to claim interface: %d", ret);
431 if (ctx->cur_samplerate == 0) {
432 /* Samplerate hasn't been set; default to the slowest one. */
433 if (hw_dev_config_set(dev_index, SR_HWCAP_SAMPLERATE,
434 &fx2lafw_supported_samplerates[0]) == SR_ERR)
441 static int hw_dev_close(int dev_index)
443 struct sr_dev_inst *sdi;
445 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
446 sr_err("fx2lafw: %s: sdi was NULL", __func__);
456 static int hw_cleanup(void)
459 struct sr_dev_inst *sdi;
463 for(l = dev_insts; l; l = l->next) {
464 if (!(sdi = l->data)) {
465 /* Log error, but continue cleaning up the rest. */
466 sr_err("fx2lafw: %s: sdi was NULL, continuing", __func__);
470 if (!(ctx = sdi->priv)) {
471 /* Log error, but continue cleaning up the rest. */
472 sr_err("fx2lafw: %s: sdi->priv was NULL, continuing",
479 sr_dev_inst_free(sdi);
482 g_slist_free(dev_insts);
486 libusb_exit(usb_context);
492 static void *hw_dev_info_get(int dev_index, int dev_info_id)
494 struct sr_dev_inst *sdi;
497 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
501 switch (dev_info_id) {
504 case SR_DI_NUM_PROBES:
505 return GINT_TO_POINTER(ctx->profile->num_probes);
506 case SR_DI_PROBE_NAMES:
507 return fx2lafw_probe_names;
508 case SR_DI_SAMPLERATES:
509 return &fx2lafw_samplerates;
510 case SR_DI_TRIGGER_TYPES:
511 return TRIGGER_TYPES;
512 case SR_DI_CUR_SAMPLERATE:
513 return &ctx->cur_samplerate;
519 static int hw_dev_status_get(int dev_index)
521 const struct sr_dev_inst *const sdi =
522 sr_dev_inst_get(dev_insts, dev_index);
525 return SR_ST_NOT_FOUND;
530 static int *hw_hwcap_get_all(void)
532 return fx2lafw_capabilities;
535 static int hw_dev_config_set(int dev_index, int hwcap, void *value)
537 struct sr_dev_inst *sdi;
541 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
545 if (hwcap == SR_HWCAP_SAMPLERATE) {
546 ctx->cur_samplerate = *(uint64_t *)value;
548 } else if (hwcap == SR_HWCAP_PROBECONFIG) {
549 ret = configure_probes(ctx, (GSList *) value);
550 } else if (hwcap == SR_HWCAP_LIMIT_SAMPLES) {
551 ctx->limit_samples = *(uint64_t *)value;
560 static int receive_data(int fd, int revents, void *cb_data)
564 /* Avoid compiler warnings. */
569 tv.tv_sec = tv.tv_usec = 0;
570 libusb_handle_events_timeout(usb_context, &tv);
575 static void abort_acquisition(struct context *ctx)
577 struct sr_datafeed_packet packet;
579 packet.type = SR_DF_END;
580 sr_session_send(ctx->session_dev_id, &packet);
582 ctx->num_samples = -1;
584 /* TODO: Need to cancel and free any queued up transfers. */
587 static void receive_transfer(struct libusb_transfer *transfer)
589 /* TODO: These statics have to move to the ctx struct. */
590 static int empty_transfer_count = 0;
591 struct sr_datafeed_packet packet;
592 struct sr_datafeed_logic logic;
593 struct context *ctx = transfer->user_data;
594 int cur_buflen, trigger_offset, i;
595 unsigned char *cur_buf, *new_buf;
598 * If acquisition has already ended, just free any queued up
599 * transfer that come in.
601 if (ctx->num_samples == -1) {
603 libusb_free_transfer(transfer);
607 sr_info("fx2lafw: receive_transfer(): status %d received %d bytes",
608 transfer->status, transfer->actual_length);
610 /* Save incoming transfer before reusing the transfer struct. */
611 cur_buf = transfer->buffer;
612 cur_buflen = transfer->actual_length;
613 ctx = transfer->user_data;
615 /* Fire off a new request. */
616 if (!(new_buf = g_try_malloc(4096))) {
617 sr_err("fx2lafw: %s: new_buf malloc failed", __func__);
618 return; /* TODO: SR_ERR_MALLOC */
621 transfer->buffer = new_buf;
622 transfer->length = 4096;
623 if (libusb_submit_transfer(transfer) != 0) {
624 /* TODO: Stop session? */
625 /* TODO: Better error message. */
626 sr_err("fx2lafw: %s: libusb_submit_transfer error", __func__);
629 if (cur_buflen == 0) {
630 empty_transfer_count++;
631 if (empty_transfer_count > MAX_EMPTY_TRANSFERS) {
633 * The FX2 gave up. End the acquisition, the frontend
634 * will work out that the samplecount is short.
636 abort_acquisition(ctx);
640 empty_transfer_count = 0;
644 if (ctx->trigger_stage >= 0) {
645 for (i = 0; i < cur_buflen; i++) {
647 if ((cur_buf[i] & ctx->trigger_mask[ctx->trigger_stage]) == ctx->trigger_value[ctx->trigger_stage]) {
648 /* Match on this trigger stage. */
649 ctx->trigger_buffer[ctx->trigger_stage] = cur_buf[i];
650 ctx->trigger_stage++;
652 if (ctx->trigger_stage == NUM_TRIGGER_STAGES || ctx->trigger_mask[ctx->trigger_stage] == 0) {
653 /* Match on all trigger stages, we're done. */
654 trigger_offset = i + 1;
657 * TODO: Send pre-trigger buffer to session bus.
658 * Tell the frontend we hit the trigger here.
660 packet.type = SR_DF_TRIGGER;
661 packet.payload = NULL;
662 sr_session_send(ctx->session_dev_id, &packet);
665 * Send the samples that triggered it, since we're
666 * skipping past them.
668 packet.type = SR_DF_LOGIC;
669 packet.payload = &logic;
670 logic.length = ctx->trigger_stage;
672 logic.data = ctx->trigger_buffer;
673 sr_session_send(ctx->session_dev_id, &packet);
675 ctx->trigger_stage = TRIGGER_FIRED;
682 * We had a match before, but not in the next sample. However, we may
683 * have a match on this stage in the next bit -- trigger on 0001 will
684 * fail on seeing 00001, so we need to go back to stage 0 -- but at
685 * the next sample from the one that matched originally, which the
686 * counter increment at the end of the loop takes care of.
688 if (ctx->trigger_stage > 0) {
689 i -= ctx->trigger_stage;
691 i = -1; /* Oops, went back past this buffer. */
692 /* Reset trigger stage. */
693 ctx->trigger_stage = 0;
698 if (ctx->trigger_stage == TRIGGER_FIRED) {
699 /* Send the incoming transfer to the session bus. */
700 packet.type = SR_DF_LOGIC;
701 packet.payload = &logic;
702 logic.length = cur_buflen - trigger_offset;
704 logic.data = cur_buf + trigger_offset;
705 sr_session_send(ctx->session_dev_id, &packet);
708 ctx->num_samples += cur_buflen;
709 if (ctx->limit_samples &&
710 (unsigned int)ctx->num_samples > ctx->limit_samples) {
711 abort_acquisition(ctx);
715 * TODO: Buffer pre-trigger data in capture
716 * ratio-sized buffer.
721 static int hw_dev_acquisition_start(int dev_index, void *cb_data)
723 struct sr_dev_inst *sdi;
724 struct sr_datafeed_packet *packet;
725 struct sr_datafeed_header *header;
727 struct libusb_transfer *transfer;
728 const struct libusb_pollfd **lupfd;
732 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
735 ctx->session_dev_id = cb_data;
736 ctx->num_samples = 0;
738 if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
739 sr_err("fx2lafw: %s: packet malloc failed", __func__);
740 return SR_ERR_MALLOC;
743 if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
744 sr_err("fx2lafw: %s: header malloc failed", __func__);
745 return SR_ERR_MALLOC;
748 /* Start with 2K transfer, subsequently increased to 4K. */
750 for (i = 0; i < NUM_SIMUL_TRANSFERS; i++) {
751 if (!(buf = g_try_malloc(size))) {
752 sr_err("fx2lafw: %s: buf malloc failed", __func__);
753 return SR_ERR_MALLOC;
755 transfer = libusb_alloc_transfer(0);
756 libusb_fill_bulk_transfer(transfer, ctx->usb->devhdl,
757 2 | LIBUSB_ENDPOINT_IN, buf, size,
758 receive_transfer, ctx, 40);
759 if (libusb_submit_transfer(transfer) != 0) {
760 /* TODO: Free them all. */
761 libusb_free_transfer(transfer);
768 lupfd = libusb_get_pollfds(usb_context);
769 for (i = 0; lupfd[i]; i++)
770 sr_source_add(lupfd[i]->fd, lupfd[i]->events,
771 40, receive_data, NULL);
772 free(lupfd); /* NOT g_free()! */
774 packet->type = SR_DF_HEADER;
775 packet->payload = header;
776 header->feed_version = 1;
777 gettimeofday(&header->starttime, NULL);
778 header->samplerate = ctx->cur_samplerate;
779 header->num_logic_probes = ctx->profile->num_probes;
780 sr_session_send(cb_data, packet);
784 if ((ret = command_start_acquisition (ctx->usb->devhdl,
785 ctx->cur_samplerate)) != SR_OK) {
792 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
793 static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
795 struct sr_dev_inst *sdi;
797 /* unused parameter */
800 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index)))
803 abort_acquisition(sdi->priv);
808 SR_PRIV struct sr_dev_driver fx2lafw_driver_info = {
810 .longname = "fx2lafw (generic driver for FX2 based LAs)",
813 .cleanup = hw_cleanup,
814 .dev_open = hw_dev_open,
815 .dev_close = hw_dev_close,
816 .dev_info_get = hw_dev_info_get,
817 .dev_status_get = hw_dev_status_get,
818 .hwcap_get_all = hw_hwcap_get_all,
819 .dev_config_set = hw_dev_config_set,
820 .dev_acquisition_start = hw_dev_acquisition_start,
821 .dev_acquisition_stop = hw_dev_acquisition_stop,