2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
5 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 static const struct fx2lafw_profile supported_fx2[] = {
29 * EE Electronics ESLA201A
32 { 0x08a9, 0x0014, "CWAV", "USBee AX", NULL,
33 "fx2lafw-cwav-usbeeax.fw",
34 DEV_CAPS_AX_ANALOG, NULL, NULL},
39 { 0x08a9, 0x0015, "CWAV", "USBee DX", NULL,
40 "fx2lafw-cwav-usbeedx.fw",
41 DEV_CAPS_16BIT, NULL, NULL },
46 { 0x08a9, 0x0009, "CWAV", "USBee SX", NULL,
47 "fx2lafw-cwav-usbeesx.fw",
53 { 0x08a9, 0x0005, "CWAV", "USBee ZX", NULL,
54 "fx2lafw-cwav-usbeezx.fw",
57 /* DreamSourceLab DSLogic (before FW upload) */
58 { 0x2a0e, 0x0001, "DreamSourceLab", "DSLogic", NULL,
59 "dreamsourcelab-dslogic-fx2.fw",
60 DEV_CAPS_16BIT | DEV_CAPS_DSLOGIC_FW, NULL, NULL},
61 /* DreamSourceLab DSLogic (after FW upload) */
62 { 0x2a0e, 0x0001, "DreamSourceLab", "DSLogic", NULL,
63 "dreamsourcelab-dslogic-fx2.fw",
64 DEV_CAPS_16BIT | DEV_CAPS_DSLOGIC_FW, "DreamSourceLab", "DSLogic"},
66 /* DreamSourceLab DSCope (before FW upload) */
67 { 0x2a0e, 0x0002, "DreamSourceLab", "DSCope", NULL,
68 "dreamsourcelab-dscope-fx2.fw",
69 DEV_CAPS_16BIT | DEV_CAPS_DSLOGIC_FW, NULL, NULL},
70 /* DreamSourceLab DSCope (after FW upload) */
71 { 0x2a0e, 0x0002, "DreamSourceLab", "DSCope", NULL,
72 "dreamsourcelab-dscope-fx2.fw",
73 DEV_CAPS_16BIT | DEV_CAPS_DSLOGIC_FW, "DreamSourceLab", "DSCope"},
75 /* DreamSourceLab DSLogic Pro (before FW upload) */
76 { 0x2a0e, 0x0003, "DreamSourceLab", "DSLogic Pro", NULL,
77 "dreamsourcelab-dslogic-pro-fx2.fw",
78 DEV_CAPS_16BIT | DEV_CAPS_DSLOGIC_FW, NULL, NULL},
79 /* DreamSourceLab DSLogic Pro (after FW upload) */
80 { 0x2a0e, 0x0003, "DreamSourceLab", "DSLogic Pro", NULL,
81 "dreamsourcelab-dslogic-pro-fx2.fw",
82 DEV_CAPS_16BIT | DEV_CAPS_DSLOGIC_FW, "DreamSourceLab", "DSLogic"},
84 /* DreamSourceLab DSLogic Plus (before FW upload) */
85 { 0x2a0e, 0x0020, "DreamSourceLab", "DSLogic Plus", NULL,
86 "dreamsourcelab-dslogic-plus-fx2.fw",
87 DEV_CAPS_16BIT | DEV_CAPS_DSLOGIC_FW, NULL, NULL},
88 /* DreamSourceLab DSLogic Plus (after FW upload) */
89 { 0x2a0e, 0x0020, "DreamSourceLab", "DSLogic Plus", NULL,
90 "dreamsourcelab-dslogic-plus-fx2.fw",
91 DEV_CAPS_16BIT | DEV_CAPS_DSLOGIC_FW, "DreamSourceLab", "DSLogic"},
93 /* DreamSourceLab DSLogic Basic (before FW upload) */
94 { 0x2a0e, 0x0021, "DreamSourceLab", "DSLogic Basic", NULL,
95 "dreamsourcelab-dslogic-basic-fx2.fw",
96 DEV_CAPS_16BIT | DEV_CAPS_DSLOGIC_FW, NULL, NULL},
97 /* DreamSourceLab DSLogic Basic (after FW upload) */
98 { 0x2a0e, 0x0021, "DreamSourceLab", "DSLogic Basic", NULL,
99 "dreamsourcelab-dslogic-basic-fx2.fw",
100 DEV_CAPS_16BIT | DEV_CAPS_DSLOGIC_FW, "DreamSourceLab", "DSLogic"},
104 * EE Electronics ESLA100
105 * Robomotic MiniLogic
106 * Robomotic BugLogic 3
108 { 0x0925, 0x3881, "Saleae", "Logic", NULL,
109 "fx2lafw-saleae-logic.fw",
113 * Default Cypress FX2 without EEPROM, e.g.:
115 * Braintechnology USB Interface V2.x
117 { 0x04B4, 0x8613, "Cypress", "FX2", NULL,
118 "fx2lafw-cypress-fx2.fw",
119 DEV_CAPS_16BIT, NULL, NULL },
122 * Braintechnology USB-LPS
124 { 0x16d0, 0x0498, "Braintechnology", "USB-LPS", NULL,
125 "fx2lafw-braintechnology-usb-lps.fw",
126 DEV_CAPS_16BIT, NULL, NULL },
129 * sigrok FX2 based 8-channel logic analyzer
131 { 0x1d50, 0x608c, "sigrok", "FX2 LA (8ch)", NULL,
132 "fx2lafw-sigrok-fx2-8ch.fw",
136 * sigrok FX2 based 16-channel logic analyzer
138 { 0x1d50, 0x608d, "sigrok", "FX2 LA (16ch)", NULL,
139 "fx2lafw-sigrok-fx2-16ch.fw",
140 DEV_CAPS_16BIT, NULL, NULL },
145 static const uint32_t drvopts[] = {
146 SR_CONF_LOGIC_ANALYZER,
149 static const uint32_t scanopts[] = {
153 static const uint32_t devopts[] = {
155 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
156 SR_CONF_CONN | SR_CONF_GET,
157 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
158 SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
159 SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET,
162 static const uint32_t dslogic_devopts[] = {
163 SR_CONF_CONTINUOUS | SR_CONF_SET | SR_CONF_GET,
164 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
165 SR_CONF_VOLTAGE_THRESHOLD | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
166 SR_CONF_CONN | SR_CONF_GET,
167 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
168 SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
169 SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET,
170 SR_CONF_EXTERNAL_CLOCK | SR_CONF_GET | SR_CONF_SET,
171 SR_CONF_CLOCK_EDGE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
174 static const int32_t soft_trigger_matches[] = {
182 /* Names assigned to available edge slope choices. */
183 static const char *const signal_edge_names[] = {
184 [DS_EDGE_RISING] = "rising",
185 [DS_EDGE_FALLING] = "falling",
188 static const struct {
192 } volt_thresholds[] = {
193 { DS_VOLTAGE_RANGE_18_33_V, 0.7, 1.4 },
194 { DS_VOLTAGE_RANGE_5_V, 1.4, 3.6 },
197 static const uint64_t samplerates[] = {
216 static const uint64_t dslogic_samplerates[] = {
235 static gboolean is_plausible(const struct libusb_device_descriptor *des)
239 for (i = 0; supported_fx2[i].vid; i++) {
240 if (des->idVendor != supported_fx2[i].vid)
242 if (des->idProduct == supported_fx2[i].pid)
249 static GSList *scan(struct sr_dev_driver *di, GSList *options)
251 struct drv_context *drvc;
252 struct dev_context *devc;
253 struct sr_dev_inst *sdi;
254 struct sr_usb_dev_inst *usb;
255 struct sr_channel *ch;
256 struct sr_channel_group *cg;
257 struct sr_config *src;
258 const struct fx2lafw_profile *prof;
259 GSList *l, *devices, *conn_devices;
260 gboolean has_firmware;
261 struct libusb_device_descriptor des;
262 libusb_device **devlist;
263 struct libusb_device_handle *hdl;
265 int num_logic_channels = 0, num_analog_channels = 0;
267 char manufacturer[64], product[64], serial_num[64], connection_id[64];
268 char channel_name[16];
273 for (l = options; l; l = l->next) {
277 conn = g_variant_get_string(src->data, NULL);
282 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
286 /* Find all fx2lafw compatible devices and upload firmware to them. */
288 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
289 for (i = 0; devlist[i]; i++) {
292 for (l = conn_devices; l; l = l->next) {
294 if (usb->bus == libusb_get_bus_number(devlist[i])
295 && usb->address == libusb_get_device_address(devlist[i]))
299 /* This device matched none of the ones that
300 * matched the conn specification. */
304 libusb_get_device_descriptor( devlist[i], &des);
306 if (!is_plausible(&des))
309 if ((ret = libusb_open(devlist[i], &hdl)) < 0) {
310 sr_warn("Failed to open potential device with "
311 "VID:PID %04x:%04x: %s.", des.idVendor,
312 des.idProduct, libusb_error_name(ret));
316 if (des.iManufacturer == 0) {
317 manufacturer[0] = '\0';
318 } else if ((ret = libusb_get_string_descriptor_ascii(hdl,
319 des.iManufacturer, (unsigned char *) manufacturer,
320 sizeof(manufacturer))) < 0) {
321 sr_warn("Failed to get manufacturer string descriptor: %s.",
322 libusb_error_name(ret));
326 if (des.iProduct == 0) {
328 } else if ((ret = libusb_get_string_descriptor_ascii(hdl,
329 des.iProduct, (unsigned char *) product,
330 sizeof(product))) < 0) {
331 sr_warn("Failed to get product string descriptor: %s.",
332 libusb_error_name(ret));
336 if (des.iSerialNumber == 0) {
337 serial_num[0] = '\0';
338 } else if ((ret = libusb_get_string_descriptor_ascii(hdl,
339 des.iSerialNumber, (unsigned char *) serial_num,
340 sizeof(serial_num))) < 0) {
341 sr_warn("Failed to get serial number string descriptor: %s.",
342 libusb_error_name(ret));
346 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
351 for (j = 0; supported_fx2[j].vid; j++) {
352 if (des.idVendor == supported_fx2[j].vid &&
353 des.idProduct == supported_fx2[j].pid &&
354 (!supported_fx2[j].usb_manufacturer ||
355 !strcmp(manufacturer, supported_fx2[j].usb_manufacturer)) &&
356 (!supported_fx2[j].usb_product ||
357 !strcmp(product, supported_fx2[j].usb_product))) {
358 prof = &supported_fx2[j];
363 /* Skip if the device was not found. */
367 sdi = g_malloc0(sizeof(struct sr_dev_inst));
368 sdi->status = SR_ST_INITIALIZING;
369 sdi->vendor = g_strdup(prof->vendor);
370 sdi->model = g_strdup(prof->model);
371 sdi->version = g_strdup(prof->model_version);
372 sdi->serial_num = g_strdup(serial_num);
373 sdi->connection_id = g_strdup(connection_id);
375 /* Fill in channellist according to this device's profile. */
376 num_logic_channels = prof->dev_caps & DEV_CAPS_16BIT ? 16 : 8;
377 num_analog_channels = prof->dev_caps & DEV_CAPS_AX_ANALOG ? 1 : 0;
379 /* Logic channels, all in one channel group. */
380 cg = g_malloc0(sizeof(struct sr_channel_group));
381 cg->name = g_strdup("Logic");
382 for (j = 0; j < num_logic_channels; j++) {
383 sprintf(channel_name, "D%d", j);
384 ch = sr_channel_new(sdi, j, SR_CHANNEL_LOGIC,
386 cg->channels = g_slist_append(cg->channels, ch);
388 sdi->channel_groups = g_slist_append(NULL, cg);
390 for (j = 0; j < num_analog_channels; j++) {
391 snprintf(channel_name, 16, "A%d", j);
392 ch = sr_channel_new(sdi, j + num_logic_channels,
393 SR_CHANNEL_ANALOG, TRUE, channel_name);
395 /* Every analog channel gets its own channel group. */
396 cg = g_malloc0(sizeof(struct sr_channel_group));
397 cg->name = g_strdup(channel_name);
398 cg->channels = g_slist_append(NULL, ch);
399 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
402 devc = fx2lafw_dev_new();
403 devc->profile = prof;
405 devices = g_slist_append(devices, sdi);
407 if (!strcmp(prof->model, "DSLogic")
408 || !strcmp(prof->model, "DSLogic Pro")
409 || !strcmp(prof->model, "DSLogic Plus")
410 || !strcmp(prof->model, "DSLogic Basic")
411 || !strcmp(prof->model, "DSCope")) {
412 devc->dslogic = TRUE;
413 devc->samplerates = dslogic_samplerates;
414 devc->num_samplerates = ARRAY_SIZE(dslogic_samplerates);
415 has_firmware = match_manuf_prod(devlist[i], "DreamSourceLab", "DSLogic")
416 || match_manuf_prod(devlist[i], "DreamSourceLab", "DSCope");
418 devc->dslogic = FALSE;
419 devc->samplerates = samplerates;
420 devc->num_samplerates = ARRAY_SIZE(samplerates);
421 has_firmware = match_manuf_prod(devlist[i],
422 "sigrok", "fx2lafw");
426 /* Already has the firmware, so fix the new address. */
427 sr_dbg("Found an fx2lafw device.");
428 sdi->status = SR_ST_INACTIVE;
429 sdi->inst_type = SR_INST_USB;
430 sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
431 libusb_get_device_address(devlist[i]), NULL);
433 if (ezusb_upload_firmware(drvc->sr_ctx, devlist[i],
434 USB_CONFIGURATION, prof->firmware) == SR_OK)
435 /* Store when this device's FW was updated. */
436 devc->fw_updated = g_get_monotonic_time();
438 sr_err("Firmware upload failed for "
439 "device %d.%d (logical).",
440 libusb_get_bus_number(devlist[i]),
441 libusb_get_device_address(devlist[i]));
442 sdi->inst_type = SR_INST_USB;
443 sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
447 libusb_free_device_list(devlist, 1);
448 g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
450 return std_scan_complete(di, devices);
453 static void clear_dev_context(void *priv)
455 struct dev_context *devc;
458 g_slist_free(devc->enabled_analog_channels);
462 static int dev_clear(const struct sr_dev_driver *di)
464 return std_dev_clear(di, clear_dev_context);
467 static int dev_open(struct sr_dev_inst *sdi)
469 struct sr_dev_driver *di = sdi->driver;
470 struct sr_usb_dev_inst *usb;
471 struct dev_context *devc;
472 const char *fpga_firmware = NULL;
474 int64_t timediff_us, timediff_ms;
480 * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS
481 * milliseconds for the FX2 to renumerate.
484 if (devc->fw_updated > 0) {
485 sr_info("Waiting for device to reset.");
486 /* Takes >= 300ms for the FX2 to be gone from the USB bus. */
487 g_usleep(300 * 1000);
489 while (timediff_ms < MAX_RENUM_DELAY_MS) {
490 if ((ret = fx2lafw_dev_open(sdi, di)) == SR_OK)
492 g_usleep(100 * 1000);
494 timediff_us = g_get_monotonic_time() - devc->fw_updated;
495 timediff_ms = timediff_us / 1000;
496 sr_spew("Waited %" PRIi64 "ms.", timediff_ms);
499 sr_err("Device failed to renumerate.");
502 sr_info("Device came back after %" PRIi64 "ms.", timediff_ms);
504 sr_info("Firmware upload was not needed.");
505 ret = fx2lafw_dev_open(sdi, di);
509 sr_err("Unable to open device.");
513 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
516 case LIBUSB_ERROR_BUSY:
517 sr_err("Unable to claim USB interface. Another "
518 "program or driver has already claimed it.");
520 case LIBUSB_ERROR_NO_DEVICE:
521 sr_err("Device has been disconnected.");
524 sr_err("Unable to claim interface: %s.",
525 libusb_error_name(ret));
533 if (!strcmp(devc->profile->model, "DSLogic")) {
534 if (devc->dslogic_voltage_threshold == DS_VOLTAGE_RANGE_18_33_V)
535 fpga_firmware = DSLOGIC_FPGA_FIRMWARE_3V3;
537 fpga_firmware = DSLOGIC_FPGA_FIRMWARE_5V;
538 } else if (!strcmp(devc->profile->model, "DSLogic Pro")){
539 fpga_firmware = DSLOGIC_PRO_FPGA_FIRMWARE;
540 } else if (!strcmp(devc->profile->model, "DSLogic Plus")){
541 fpga_firmware = DSLOGIC_PLUS_FPGA_FIRMWARE;
542 } else if (!strcmp(devc->profile->model, "DSLogic Basic")){
543 fpga_firmware = DSLOGIC_BASIC_FPGA_FIRMWARE;
544 } else if (!strcmp(devc->profile->model, "DSCope")) {
545 fpga_firmware = DSCOPE_FPGA_FIRMWARE;
548 if ((ret = dslogic_fpga_firmware_upload(sdi, fpga_firmware)) != SR_OK)
551 if (devc->cur_samplerate == 0) {
552 /* Samplerate hasn't been set; default to the slowest one. */
553 devc->cur_samplerate = devc->samplerates[0];
559 static int dev_close(struct sr_dev_inst *sdi)
561 struct sr_usb_dev_inst *usb;
568 sr_info("fx2lafw: Closing device on %d.%d (logical) / %s (physical) interface %d.",
569 usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
570 libusb_release_interface(usb->devhdl, USB_INTERFACE);
571 libusb_close(usb->devhdl);
573 sdi->status = SR_ST_INACTIVE;
578 static int config_get(uint32_t key, GVariant **data,
579 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
581 struct dev_context *devc;
582 struct sr_usb_dev_inst *usb;
599 if (usb->address == 255)
600 /* Device still needs to re-enumerate after firmware
601 * upload, so we don't know its (future) address. */
603 snprintf(str, 128, "%d.%d", usb->bus, usb->address);
604 *data = g_variant_new_string(str);
606 case SR_CONF_VOLTAGE_THRESHOLD:
607 for (i = 0; i < ARRAY_SIZE(volt_thresholds); i++) {
608 if (volt_thresholds[i].range != devc->dslogic_voltage_threshold)
610 range[0] = g_variant_new_double(volt_thresholds[i].low);
611 range[1] = g_variant_new_double(volt_thresholds[i].high);
612 *data = g_variant_new_tuple(range, 2);
616 case SR_CONF_LIMIT_SAMPLES:
617 *data = g_variant_new_uint64(devc->limit_samples);
619 case SR_CONF_SAMPLERATE:
620 *data = g_variant_new_uint64(devc->cur_samplerate);
622 case SR_CONF_CAPTURE_RATIO:
623 *data = g_variant_new_uint64(devc->capture_ratio);
625 case SR_CONF_EXTERNAL_CLOCK:
626 *data = g_variant_new_boolean(devc->dslogic_external_clock);
628 case SR_CONF_CONTINUOUS:
629 *data = g_variant_new_boolean(devc->dslogic_continuous_mode);
631 case SR_CONF_CLOCK_EDGE:
632 i = devc->dslogic_clock_edge;
633 if (i >= ARRAY_SIZE(signal_edge_names))
635 *data = g_variant_new_string(signal_edge_names[0]);
645 * Helper for mapping a string-typed configuration value to an index
646 * within a table of possible values.
648 static int lookup_index(GVariant *value, const char *const *table, int len)
653 entry = g_variant_get_string(value, NULL);
657 /* Linear search is fine for very small tables. */
658 for (i = 0; i < len; i++) {
659 if (strcmp(entry, table[i]) == 0)
666 static int config_set(uint32_t key, GVariant *data,
667 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
669 struct dev_context *devc;
679 if (sdi->status != SR_ST_ACTIVE)
687 case SR_CONF_SAMPLERATE:
688 arg = g_variant_get_uint64(data);
689 for (i = 0; i < devc->num_samplerates; i++) {
690 if (devc->samplerates[i] == arg) {
691 devc->cur_samplerate = arg;
695 if (i == devc->num_samplerates)
698 case SR_CONF_LIMIT_SAMPLES:
699 devc->limit_samples = g_variant_get_uint64(data);
701 case SR_CONF_CAPTURE_RATIO:
702 devc->capture_ratio = g_variant_get_uint64(data);
703 ret = (devc->capture_ratio > 100) ? SR_ERR : SR_OK;
705 case SR_CONF_VOLTAGE_THRESHOLD:
706 g_variant_get(data, "(dd)", &low, &high);
708 for (i = 0; (unsigned int)i < ARRAY_SIZE(volt_thresholds); i++) {
709 if (fabs(volt_thresholds[i].low - low) < 0.1 &&
710 fabs(volt_thresholds[i].high - high) < 0.1) {
711 devc->dslogic_voltage_threshold = volt_thresholds[i].range;
715 if (!strcmp(devc->profile->model, "DSLogic")) {
716 if (devc->dslogic_voltage_threshold == DS_VOLTAGE_RANGE_5_V)
717 ret = dslogic_fpga_firmware_upload(sdi, DSLOGIC_FPGA_FIRMWARE_5V);
719 ret = dslogic_fpga_firmware_upload(sdi, DSLOGIC_FPGA_FIRMWARE_3V3);
720 } else if (!strcmp(devc->profile->model, "DSLogic Pro")) {
721 ret = dslogic_fpga_firmware_upload(sdi, DSLOGIC_PRO_FPGA_FIRMWARE);
722 } else if (!strcmp(devc->profile->model, "DSLogic Plus")) {
723 ret = dslogic_fpga_firmware_upload(sdi, DSLOGIC_PLUS_FPGA_FIRMWARE);
724 } else if (!strcmp(devc->profile->model, "DSLogic Basic")) {
725 ret = dslogic_fpga_firmware_upload(sdi, DSLOGIC_BASIC_FPGA_FIRMWARE);
728 case SR_CONF_EXTERNAL_CLOCK:
729 devc->dslogic_external_clock = g_variant_get_boolean(data);
731 case SR_CONF_CONTINUOUS:
732 devc->dslogic_continuous_mode = g_variant_get_boolean(data);
734 case SR_CONF_CLOCK_EDGE:
735 i = lookup_index(data, signal_edge_names,
736 ARRAY_SIZE(signal_edge_names));
739 devc->dslogic_clock_edge = i;
748 static int config_list(uint32_t key, GVariant **data,
749 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
751 struct dev_context *devc;
752 GVariant *gvar, *range[2];
759 case SR_CONF_SCAN_OPTIONS:
760 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
761 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
763 case SR_CONF_DEVICE_OPTIONS:
765 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
766 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
770 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
771 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
773 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
774 dslogic_devopts, ARRAY_SIZE(dslogic_devopts), sizeof(uint32_t));
777 case SR_CONF_VOLTAGE_THRESHOLD:
783 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
784 for (i = 0; i < ARRAY_SIZE(volt_thresholds); i++) {
785 range[0] = g_variant_new_double(volt_thresholds[i].low);
786 range[1] = g_variant_new_double(volt_thresholds[i].high);
787 gvar = g_variant_new_tuple(range, 2);
788 g_variant_builder_add_value(&gvb, gvar);
790 *data = g_variant_builder_end(&gvb);
792 case SR_CONF_SAMPLERATE:
794 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
795 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), devc->samplerates,
796 devc->num_samplerates, sizeof(uint64_t));
797 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
798 *data = g_variant_builder_end(&gvb);
800 case SR_CONF_TRIGGER_MATCH:
801 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
802 soft_trigger_matches, ARRAY_SIZE(soft_trigger_matches),
805 case SR_CONF_CLOCK_EDGE:
806 *data = g_variant_new_strv(signal_edge_names,
807 ARRAY_SIZE(signal_edge_names));
816 static int receive_data(int fd, int revents, void *cb_data)
819 struct drv_context *drvc;
824 drvc = (struct drv_context *)cb_data;
826 tv.tv_sec = tv.tv_usec = 0;
827 libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
832 static int start_transfers(const struct sr_dev_inst *sdi)
834 struct dev_context *devc;
835 struct sr_usb_dev_inst *usb;
836 struct sr_trigger *trigger;
837 struct libusb_transfer *transfer;
838 unsigned int i, num_transfers;
839 int endpoint, timeout, ret;
846 devc->sent_samples = 0;
847 devc->acq_aborted = FALSE;
848 devc->empty_transfer_count = 0;
850 if ((trigger = sr_session_trigger_get(sdi->session)) && !devc->dslogic) {
851 int pre_trigger_samples = 0;
852 if (devc->limit_samples > 0)
853 pre_trigger_samples = devc->capture_ratio * devc->limit_samples/100;
854 devc->stl = soft_trigger_logic_new(sdi, trigger, pre_trigger_samples);
856 return SR_ERR_MALLOC;
857 devc->trigger_fired = FALSE;
859 devc->trigger_fired = TRUE;
861 num_transfers = fx2lafw_get_number_of_transfers(devc);
864 // num_transfers = dslogic_get_number_of_transfers(devc);
867 if (devc->cur_samplerate == SR_MHZ(100))
869 else if (devc->cur_samplerate == SR_MHZ(200))
871 else if (devc->cur_samplerate == SR_MHZ(400))
875 size = fx2lafw_get_buffer_size(devc);
876 devc->submitted_transfers = 0;
878 devc->transfers = g_try_malloc0(sizeof(*devc->transfers) * num_transfers);
879 if (!devc->transfers) {
880 sr_err("USB transfers malloc failed.");
881 return SR_ERR_MALLOC;
884 timeout = fx2lafw_get_timeout(devc);
885 endpoint = devc->dslogic ? 6 : 2;
886 devc->num_transfers = num_transfers;
887 for (i = 0; i < num_transfers; i++) {
888 if (!(buf = g_try_malloc(size))) {
889 sr_err("USB transfer buffer malloc failed.");
890 return SR_ERR_MALLOC;
892 transfer = libusb_alloc_transfer(0);
893 libusb_fill_bulk_transfer(transfer, usb->devhdl,
894 endpoint | LIBUSB_ENDPOINT_IN, buf, size,
895 fx2lafw_receive_transfer, (void *)sdi, timeout);
896 sr_info("submitting transfer: %d", i);
897 if ((ret = libusb_submit_transfer(transfer)) != 0) {
898 sr_err("Failed to submit transfer: %s.",
899 libusb_error_name(ret));
900 libusb_free_transfer(transfer);
902 fx2lafw_abort_acquisition(devc);
905 devc->transfers[i] = transfer;
906 devc->submitted_transfers++;
910 * If this device has analog channels and at least one of them is
911 * enabled, use mso_send_data_proc() to properly handle the analog
912 * data. Otherwise use la_send_data_proc().
914 if (g_slist_length(devc->enabled_analog_channels) > 0)
915 devc->send_data_proc = mso_send_data_proc;
917 devc->send_data_proc = la_send_data_proc;
919 std_session_send_df_header(sdi);
924 static void LIBUSB_CALL dslogic_trigger_receive(struct libusb_transfer *transfer)
926 const struct sr_dev_inst *sdi;
927 struct dslogic_trigger_pos *tpos;
928 struct dev_context *devc;
930 sdi = transfer->user_data;
932 if (transfer->status == LIBUSB_TRANSFER_CANCELLED) {
933 sr_dbg("Trigger transfer canceled.");
934 /* Terminate session. */
935 std_session_send_df_end(sdi);
936 usb_source_remove(sdi->session, devc->ctx);
937 devc->num_transfers = 0;
938 g_free(devc->transfers);
940 soft_trigger_logic_free(devc->stl);
943 } else if (transfer->status == LIBUSB_TRANSFER_COMPLETED
944 && transfer->actual_length == sizeof(struct dslogic_trigger_pos)) {
945 tpos = (struct dslogic_trigger_pos *)transfer->buffer;
946 sr_info("tpos real_pos %d ram_saddr %d cnt %d", tpos->real_pos,
947 tpos->ram_saddr, tpos->remain_cnt);
948 devc->trigger_pos = tpos->real_pos;
950 start_transfers(sdi);
952 libusb_free_transfer(transfer);
955 static int dslogic_trigger_request(const struct sr_dev_inst *sdi)
957 struct sr_usb_dev_inst *usb;
958 struct libusb_transfer *transfer;
959 struct dslogic_trigger_pos *tpos;
960 struct dev_context *devc;
966 if ((ret = dslogic_stop_acquisition(sdi)) != SR_OK)
969 if ((ret = dslogic_fpga_configure(sdi)) != SR_OK)
972 /* If this is a DSLogic Pro, set the voltage threshold. */
973 if (!strcmp(devc->profile->model, "DSLogic Pro")){
974 if (devc->dslogic_voltage_threshold == DS_VOLTAGE_RANGE_18_33_V) {
975 dslogic_set_vth(sdi, 1.4);
977 dslogic_set_vth(sdi, 3.3);
981 if ((ret = dslogic_start_acquisition(sdi)) != SR_OK)
984 sr_dbg("Getting trigger.");
985 tpos = g_malloc(sizeof(struct dslogic_trigger_pos));
986 transfer = libusb_alloc_transfer(0);
987 libusb_fill_bulk_transfer(transfer, usb->devhdl, 6 | LIBUSB_ENDPOINT_IN,
988 (unsigned char *)tpos, sizeof(struct dslogic_trigger_pos),
989 dslogic_trigger_receive, (void *)sdi, 0);
990 if ((ret = libusb_submit_transfer(transfer)) < 0) {
991 sr_err("Failed to request trigger: %s.", libusb_error_name(ret));
992 libusb_free_transfer(transfer);
997 devc->transfers = g_try_malloc0(sizeof(*devc->transfers));
998 if (!devc->transfers) {
999 sr_err("USB trigger_pos transfer malloc failed.");
1000 return SR_ERR_MALLOC;
1002 devc->num_transfers = 1;
1003 devc->submitted_transfers++;
1004 devc->transfers[0] = transfer;
1009 static int configure_channels(const struct sr_dev_inst *sdi)
1011 struct dev_context *devc;
1014 struct sr_channel *ch;
1015 uint32_t channel_mask = 0, num_analog = 0;
1019 g_slist_free(devc->enabled_analog_channels);
1020 devc->enabled_analog_channels = NULL;
1022 for (l = sdi->channels, p = 0; l; l = l->next, p++) {
1024 if ((p <= NUM_CHANNELS) && (ch->type == SR_CHANNEL_ANALOG)
1027 devc->enabled_analog_channels =
1028 g_slist_append(devc->enabled_analog_channels, ch);
1030 channel_mask |= ch->enabled << p;
1035 * Use wide sampling if either any of the LA channels 8..15 is enabled,
1036 * and/or at least one analog channel is enabled, and/or the device
1037 * is running DSLogic firmware (not fx2lafw).
1039 devc->sample_wide = (channel_mask > 0xff
1041 || (devc->profile->dev_caps & DEV_CAPS_DSLOGIC_FW));
1046 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
1048 struct sr_dev_driver *di;
1049 struct drv_context *drvc;
1050 struct dev_context *devc;
1054 if (sdi->status != SR_ST_ACTIVE)
1055 return SR_ERR_DEV_CLOSED;
1061 devc->ctx = drvc->sr_ctx;
1062 devc->sent_samples = 0;
1063 devc->empty_transfer_count = 0;
1064 devc->acq_aborted = FALSE;
1066 if (configure_channels(sdi) != SR_OK) {
1067 sr_err("Failed to configure channels.");
1071 timeout = fx2lafw_get_timeout(devc);
1072 usb_source_add(sdi->session, devc->ctx, timeout, receive_data, drvc);
1074 if (devc->dslogic) {
1075 dslogic_trigger_request(sdi);
1077 size = fx2lafw_get_buffer_size(devc);
1078 /* Prepare for analog sampling. */
1079 if (g_slist_length(devc->enabled_analog_channels) > 0) {
1080 /* We need a buffer half the size of a transfer. */
1081 devc->logic_buffer = g_try_malloc(size / 2);
1082 devc->analog_buffer = g_try_malloc(
1083 sizeof(float) * size / 2);
1085 start_transfers(sdi);
1086 if ((ret = fx2lafw_command_start_acquisition(sdi)) != SR_OK) {
1087 fx2lafw_abort_acquisition(devc);
1095 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
1097 struct dev_context *devc;
1102 dslogic_stop_acquisition(sdi);
1104 fx2lafw_abort_acquisition(sdi->priv);
1109 static struct sr_dev_driver fx2lafw_driver_info = {
1111 .longname = "fx2lafw (generic driver for FX2 based LAs)",
1114 .cleanup = std_cleanup,
1116 .dev_list = std_dev_list,
1117 .dev_clear = dev_clear,
1118 .config_get = config_get,
1119 .config_set = config_set,
1120 .config_list = config_list,
1121 .dev_open = dev_open,
1122 .dev_close = dev_close,
1123 .dev_acquisition_start = dev_acquisition_start,
1124 .dev_acquisition_stop = dev_acquisition_stop,
1127 SR_REGISTER_DEV_DRIVER(fx2lafw_driver_info);