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 dslogic_profile supported_device[] = {
27 /* DreamSourceLab DSLogic */
28 { 0x2a0e, 0x0001, "DreamSourceLab", "DSLogic", NULL,
29 "dreamsourcelab-dslogic-fx2.fw",
30 0, "DreamSourceLab", "DSLogic"},
31 /* DreamSourceLab DSCope */
32 { 0x2a0e, 0x0002, "DreamSourceLab", "DSCope", NULL,
33 "dreamsourcelab-dscope-fx2.fw",
34 0, "DreamSourceLab", "DSCope"},
35 /* DreamSourceLab DSLogic Pro */
36 { 0x2a0e, 0x0003, "DreamSourceLab", "DSLogic Pro", NULL,
37 "dreamsourcelab-dslogic-pro-fx2.fw",
38 0, "DreamSourceLab", "DSLogic"},
39 /* DreamSourceLab DSLogic Plus */
40 { 0x2a0e, 0x0020, "DreamSourceLab", "DSLogic Plus", NULL,
41 "dreamsourcelab-dslogic-plus-fx2.fw",
42 0, "DreamSourceLab", "DSLogic"},
43 /* DreamSourceLab DSLogic Basic */
44 { 0x2a0e, 0x0021, "DreamSourceLab", "DSLogic Basic", NULL,
45 "dreamsourcelab-dslogic-basic-fx2.fw",
46 0, "DreamSourceLab", "DSLogic"},
51 static const uint32_t drvopts[] = {
52 SR_CONF_LOGIC_ANALYZER,
55 static const uint32_t scanopts[] = {
59 static const uint32_t devopts[] = {
60 SR_CONF_CONTINUOUS | SR_CONF_SET | SR_CONF_GET,
61 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
62 SR_CONF_VOLTAGE_THRESHOLD | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
63 SR_CONF_CONN | SR_CONF_GET,
64 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
65 SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
66 SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET,
67 SR_CONF_EXTERNAL_CLOCK | SR_CONF_GET | SR_CONF_SET,
68 SR_CONF_CLOCK_EDGE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
71 /* Names assigned to available edge slope choices. */
72 static const char *const signal_edge_names[] = {
73 [DS_EDGE_RISING] = "rising",
74 [DS_EDGE_FALLING] = "falling",
81 } volt_thresholds[] = {
82 { DS_VOLTAGE_RANGE_18_33_V, 0.7, 1.4 },
83 { DS_VOLTAGE_RANGE_5_V, 1.4, 3.6 },
86 static const uint64_t samplerates[] = {
105 static gboolean is_plausible(const struct libusb_device_descriptor *des)
109 for (i = 0; supported_device[i].vid; i++) {
110 if (des->idVendor != supported_device[i].vid)
112 if (des->idProduct == supported_device[i].pid)
119 static GSList *scan(struct sr_dev_driver *di, GSList *options)
121 struct drv_context *drvc;
122 struct dev_context *devc;
123 struct sr_dev_inst *sdi;
124 struct sr_usb_dev_inst *usb;
125 struct sr_channel *ch;
126 struct sr_channel_group *cg;
127 struct sr_config *src;
128 const struct dslogic_profile *prof;
129 GSList *l, *devices, *conn_devices;
130 gboolean has_firmware;
131 struct libusb_device_descriptor des;
132 libusb_device **devlist;
133 struct libusb_device_handle *hdl;
136 char manufacturer[64], product[64], serial_num[64], connection_id[64];
137 char channel_name[16];
142 for (l = options; l; l = l->next) {
146 conn = g_variant_get_string(src->data, NULL);
151 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
155 /* Find all dslogic compatible devices and upload firmware to them. */
157 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
158 for (i = 0; devlist[i]; i++) {
161 for (l = conn_devices; l; l = l->next) {
163 if (usb->bus == libusb_get_bus_number(devlist[i])
164 && usb->address == libusb_get_device_address(devlist[i]))
168 /* This device matched none of the ones that
169 * matched the conn specification. */
173 libusb_get_device_descriptor( devlist[i], &des);
175 if (!is_plausible(&des))
178 if ((ret = libusb_open(devlist[i], &hdl)) < 0) {
179 sr_warn("Failed to open potential device with "
180 "VID:PID %04x:%04x: %s.", des.idVendor,
181 des.idProduct, libusb_error_name(ret));
185 if (des.iManufacturer == 0) {
186 manufacturer[0] = '\0';
187 } else if ((ret = libusb_get_string_descriptor_ascii(hdl,
188 des.iManufacturer, (unsigned char *) manufacturer,
189 sizeof(manufacturer))) < 0) {
190 sr_warn("Failed to get manufacturer string descriptor: %s.",
191 libusb_error_name(ret));
195 if (des.iProduct == 0) {
197 } else if ((ret = libusb_get_string_descriptor_ascii(hdl,
198 des.iProduct, (unsigned char *) product,
199 sizeof(product))) < 0) {
200 sr_warn("Failed to get product string descriptor: %s.",
201 libusb_error_name(ret));
205 if (des.iSerialNumber == 0) {
206 serial_num[0] = '\0';
207 } else if ((ret = libusb_get_string_descriptor_ascii(hdl,
208 des.iSerialNumber, (unsigned char *) serial_num,
209 sizeof(serial_num))) < 0) {
210 sr_warn("Failed to get serial number string descriptor: %s.",
211 libusb_error_name(ret));
215 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
220 for (j = 0; supported_device[j].vid; j++) {
221 if (des.idVendor == supported_device[j].vid &&
222 des.idProduct == supported_device[j].pid &&
223 (!strcmp(manufacturer, supported_device[j].usb_manufacturer)) &&
224 (!strcmp(product, "USB-based Instrument") ||
225 !strcmp(product, supported_device[j].usb_product))) {
226 prof = &supported_device[j];
231 /* Skip if the device was not found. */
235 sdi = g_malloc0(sizeof(struct sr_dev_inst));
236 sdi->status = SR_ST_INITIALIZING;
237 sdi->vendor = g_strdup(prof->vendor);
238 sdi->model = g_strdup(prof->model);
239 sdi->version = g_strdup(prof->model_version);
240 sdi->serial_num = g_strdup(serial_num);
241 sdi->connection_id = g_strdup(connection_id);
243 /* Logic channels, all in one channel group. */
244 cg = g_malloc0(sizeof(struct sr_channel_group));
245 cg->name = g_strdup("Logic");
246 for (j = 0; j < 16; j++) {
247 sprintf(channel_name, "%d", j);
248 ch = sr_channel_new(sdi, j, SR_CHANNEL_LOGIC,
250 cg->channels = g_slist_append(cg->channels, ch);
252 sdi->channel_groups = g_slist_append(NULL, cg);
254 devc = dslogic_dev_new();
255 devc->profile = prof;
257 devices = g_slist_append(devices, sdi);
259 devc->samplerates = samplerates;
260 devc->num_samplerates = ARRAY_SIZE(samplerates);
261 has_firmware = usb_match_manuf_prod(devlist[i], "DreamSourceLab", "USB-based Instrument");
264 /* Already has the firmware, so fix the new address. */
265 sr_dbg("Found an dslogic device.");
266 sdi->status = SR_ST_INACTIVE;
267 sdi->inst_type = SR_INST_USB;
268 sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
269 libusb_get_device_address(devlist[i]), NULL);
271 if (ezusb_upload_firmware(drvc->sr_ctx, devlist[i],
272 USB_CONFIGURATION, prof->firmware) == SR_OK)
273 /* Store when this device's FW was updated. */
274 devc->fw_updated = g_get_monotonic_time();
276 sr_err("Firmware upload failed for "
277 "device %d.%d (logical).",
278 libusb_get_bus_number(devlist[i]),
279 libusb_get_device_address(devlist[i]));
280 sdi->inst_type = SR_INST_USB;
281 sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
285 libusb_free_device_list(devlist, 1);
286 g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
288 return std_scan_complete(di, devices);
291 static void clear_dev_context(void *priv)
293 struct dev_context *devc;
299 static int dev_clear(const struct sr_dev_driver *di)
301 return std_dev_clear(di, clear_dev_context);
304 static int dev_open(struct sr_dev_inst *sdi)
306 struct sr_dev_driver *di = sdi->driver;
307 struct sr_usb_dev_inst *usb;
308 struct dev_context *devc;
310 int64_t timediff_us, timediff_ms;
316 * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS
317 * milliseconds for the FX2 to renumerate.
320 if (devc->fw_updated > 0) {
321 sr_info("Waiting for device to reset.");
322 /* Takes >= 300ms for the FX2 to be gone from the USB bus. */
323 g_usleep(300 * 1000);
325 while (timediff_ms < MAX_RENUM_DELAY_MS) {
326 if ((ret = dslogic_dev_open(sdi, di)) == SR_OK)
328 g_usleep(100 * 1000);
330 timediff_us = g_get_monotonic_time() - devc->fw_updated;
331 timediff_ms = timediff_us / 1000;
332 sr_spew("Waited %" PRIi64 "ms.", timediff_ms);
335 sr_err("Device failed to renumerate.");
338 sr_info("Device came back after %" PRIi64 "ms.", timediff_ms);
340 sr_info("Firmware upload was not needed.");
341 ret = dslogic_dev_open(sdi, di);
345 sr_err("Unable to open device.");
349 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
352 case LIBUSB_ERROR_BUSY:
353 sr_err("Unable to claim USB interface. Another "
354 "program or driver has already claimed it.");
356 case LIBUSB_ERROR_NO_DEVICE:
357 sr_err("Device has been disconnected.");
360 sr_err("Unable to claim interface: %s.",
361 libusb_error_name(ret));
369 if ((ret = dslogic_fpga_firmware_upload(sdi)) != SR_OK)
372 if (devc->cur_samplerate == 0) {
373 /* Samplerate hasn't been set; default to the slowest one. */
374 devc->cur_samplerate = devc->samplerates[0];
380 static int dev_close(struct sr_dev_inst *sdi)
382 struct sr_usb_dev_inst *usb;
389 sr_info("dslogic: Closing device on %d.%d (logical) / %s (physical) interface %d.",
390 usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
391 libusb_release_interface(usb->devhdl, USB_INTERFACE);
392 libusb_close(usb->devhdl);
394 sdi->status = SR_ST_INACTIVE;
399 static int config_get(uint32_t key, GVariant **data,
400 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
402 struct dev_context *devc;
403 struct sr_usb_dev_inst *usb;
420 if (usb->address == 255)
421 /* Device still needs to re-enumerate after firmware
422 * upload, so we don't know its (future) address. */
424 snprintf(str, 128, "%d.%d", usb->bus, usb->address);
425 *data = g_variant_new_string(str);
427 case SR_CONF_VOLTAGE_THRESHOLD:
428 for (i = 0; i < ARRAY_SIZE(volt_thresholds); i++) {
429 if (volt_thresholds[i].range != devc->voltage_threshold)
431 range[0] = g_variant_new_double(volt_thresholds[i].low);
432 range[1] = g_variant_new_double(volt_thresholds[i].high);
433 *data = g_variant_new_tuple(range, 2);
437 case SR_CONF_LIMIT_SAMPLES:
438 *data = g_variant_new_uint64(devc->limit_samples);
440 case SR_CONF_SAMPLERATE:
441 *data = g_variant_new_uint64(devc->cur_samplerate);
443 case SR_CONF_CAPTURE_RATIO:
444 *data = g_variant_new_uint64(devc->capture_ratio);
446 case SR_CONF_EXTERNAL_CLOCK:
447 *data = g_variant_new_boolean(devc->external_clock);
449 case SR_CONF_CONTINUOUS:
450 *data = g_variant_new_boolean(devc->continuous_mode);
452 case SR_CONF_CLOCK_EDGE:
453 i = devc->clock_edge;
454 if (i >= ARRAY_SIZE(signal_edge_names))
456 *data = g_variant_new_string(signal_edge_names[0]);
466 * Helper for mapping a string-typed configuration value to an index
467 * within a table of possible values.
469 static int lookup_index(GVariant *value, const char *const *table, int len)
474 entry = g_variant_get_string(value, NULL);
478 /* Linear search is fine for very small tables. */
479 for (i = 0; i < len; i++) {
480 if (strcmp(entry, table[i]) == 0)
487 static int config_set(uint32_t key, GVariant *data,
488 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
490 struct dev_context *devc;
500 if (sdi->status != SR_ST_ACTIVE)
508 case SR_CONF_SAMPLERATE:
509 arg = g_variant_get_uint64(data);
510 for (i = 0; i < devc->num_samplerates; i++) {
511 if (devc->samplerates[i] == arg) {
512 devc->cur_samplerate = arg;
516 if (i == devc->num_samplerates)
519 case SR_CONF_LIMIT_SAMPLES:
520 devc->limit_samples = g_variant_get_uint64(data);
522 case SR_CONF_CAPTURE_RATIO:
523 devc->capture_ratio = g_variant_get_uint64(data);
524 ret = (devc->capture_ratio > 100) ? SR_ERR : SR_OK;
526 case SR_CONF_VOLTAGE_THRESHOLD:
527 g_variant_get(data, "(dd)", &low, &high);
529 for (i = 0; (unsigned int)i < ARRAY_SIZE(volt_thresholds); i++) {
530 if (fabs(volt_thresholds[i].low - low) < 0.1 &&
531 fabs(volt_thresholds[i].high - high) < 0.1) {
532 devc->voltage_threshold = volt_thresholds[i].range;
536 ret = dslogic_fpga_firmware_upload(sdi);
538 case SR_CONF_EXTERNAL_CLOCK:
539 devc->external_clock = g_variant_get_boolean(data);
541 case SR_CONF_CONTINUOUS:
542 devc->continuous_mode = g_variant_get_boolean(data);
544 case SR_CONF_CLOCK_EDGE:
545 i = lookup_index(data, signal_edge_names,
546 ARRAY_SIZE(signal_edge_names));
549 devc->clock_edge = i;
558 static int config_list(uint32_t key, GVariant **data,
559 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
561 struct dev_context *devc;
562 GVariant *gvar, *range[2];
569 case SR_CONF_SCAN_OPTIONS:
570 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
571 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
573 case SR_CONF_DEVICE_OPTIONS:
575 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
576 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
579 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
580 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
583 case SR_CONF_VOLTAGE_THRESHOLD:
587 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
588 for (i = 0; i < ARRAY_SIZE(volt_thresholds); i++) {
589 range[0] = g_variant_new_double(volt_thresholds[i].low);
590 range[1] = g_variant_new_double(volt_thresholds[i].high);
591 gvar = g_variant_new_tuple(range, 2);
592 g_variant_builder_add_value(&gvb, gvar);
594 *data = g_variant_builder_end(&gvb);
596 case SR_CONF_SAMPLERATE:
598 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
599 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), devc->samplerates,
600 devc->num_samplerates, sizeof(uint64_t));
601 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
602 *data = g_variant_builder_end(&gvb);
604 case SR_CONF_CLOCK_EDGE:
605 *data = g_variant_new_strv(signal_edge_names,
606 ARRAY_SIZE(signal_edge_names));
615 static int receive_data(int fd, int revents, void *cb_data)
618 struct drv_context *drvc;
623 drvc = (struct drv_context *)cb_data;
625 tv.tv_sec = tv.tv_usec = 0;
626 libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
631 static int start_transfers(const struct sr_dev_inst *sdi)
633 struct dev_context *devc;
634 struct sr_usb_dev_inst *usb;
635 struct libusb_transfer *transfer;
636 unsigned int i, num_transfers;
644 devc->sent_samples = 0;
645 devc->acq_aborted = FALSE;
646 devc->empty_transfer_count = 0;
647 devc->trigger_fired = TRUE;
649 num_transfers = dslogic_get_number_of_transfers(devc);
651 if (devc->cur_samplerate == SR_MHZ(100))
653 else if (devc->cur_samplerate == SR_MHZ(200))
655 else if (devc->cur_samplerate == SR_MHZ(400))
658 size = dslogic_get_buffer_size(devc);
659 devc->submitted_transfers = 0;
661 devc->transfers = g_try_malloc0(sizeof(*devc->transfers) * num_transfers);
662 if (!devc->transfers) {
663 sr_err("USB transfers malloc failed.");
664 return SR_ERR_MALLOC;
667 timeout = dslogic_get_timeout(devc);
668 devc->num_transfers = num_transfers;
669 for (i = 0; i < num_transfers; i++) {
670 if (!(buf = g_try_malloc(size))) {
671 sr_err("USB transfer buffer malloc failed.");
672 return SR_ERR_MALLOC;
674 transfer = libusb_alloc_transfer(0);
675 libusb_fill_bulk_transfer(transfer, usb->devhdl,
676 6 | LIBUSB_ENDPOINT_IN, buf, size,
677 dslogic_receive_transfer, (void *)sdi, timeout);
678 sr_info("submitting transfer: %d", i);
679 if ((ret = libusb_submit_transfer(transfer)) != 0) {
680 sr_err("Failed to submit transfer: %s.",
681 libusb_error_name(ret));
682 libusb_free_transfer(transfer);
684 dslogic_abort_acquisition(devc);
687 devc->transfers[i] = transfer;
688 devc->submitted_transfers++;
691 std_session_send_df_header(sdi);
696 static void LIBUSB_CALL trigger_receive(struct libusb_transfer *transfer)
698 const struct sr_dev_inst *sdi;
699 struct dslogic_trigger_pos *tpos;
700 struct dev_context *devc;
702 sdi = transfer->user_data;
704 if (transfer->status == LIBUSB_TRANSFER_CANCELLED) {
705 sr_dbg("Trigger transfer canceled.");
706 /* Terminate session. */
707 std_session_send_df_end(sdi);
708 usb_source_remove(sdi->session, devc->ctx);
709 devc->num_transfers = 0;
710 g_free(devc->transfers);
711 } else if (transfer->status == LIBUSB_TRANSFER_COMPLETED
712 && transfer->actual_length == sizeof(struct dslogic_trigger_pos)) {
713 tpos = (struct dslogic_trigger_pos *)transfer->buffer;
714 sr_info("tpos real_pos %d ram_saddr %d cnt %d", tpos->real_pos,
715 tpos->ram_saddr, tpos->remain_cnt);
716 devc->trigger_pos = tpos->real_pos;
718 start_transfers(sdi);
720 libusb_free_transfer(transfer);
723 static int trigger_request(const struct sr_dev_inst *sdi)
725 struct sr_usb_dev_inst *usb;
726 struct libusb_transfer *transfer;
727 struct dslogic_trigger_pos *tpos;
728 struct dev_context *devc;
734 if ((ret = dslogic_stop_acquisition(sdi)) != SR_OK)
737 if ((ret = dslogic_fpga_configure(sdi)) != SR_OK)
740 /* If this is a DSLogic Pro, set the voltage threshold. */
741 if (!strcmp(devc->profile->model, "DSLogic Pro")){
742 if (devc->voltage_threshold == DS_VOLTAGE_RANGE_18_33_V) {
743 dslogic_set_vth(sdi, 1.4);
745 dslogic_set_vth(sdi, 3.3);
749 if ((ret = dslogic_start_acquisition(sdi)) != SR_OK)
752 sr_dbg("Getting trigger.");
753 tpos = g_malloc(sizeof(struct dslogic_trigger_pos));
754 transfer = libusb_alloc_transfer(0);
755 libusb_fill_bulk_transfer(transfer, usb->devhdl, 6 | LIBUSB_ENDPOINT_IN,
756 (unsigned char *)tpos, sizeof(struct dslogic_trigger_pos),
757 trigger_receive, (void *)sdi, 0);
758 if ((ret = libusb_submit_transfer(transfer)) < 0) {
759 sr_err("Failed to request trigger: %s.", libusb_error_name(ret));
760 libusb_free_transfer(transfer);
765 devc->transfers = g_try_malloc0(sizeof(*devc->transfers));
766 if (!devc->transfers) {
767 sr_err("USB trigger_pos transfer malloc failed.");
768 return SR_ERR_MALLOC;
770 devc->num_transfers = 1;
771 devc->submitted_transfers++;
772 devc->transfers[0] = transfer;
777 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
779 struct sr_dev_driver *di;
780 struct drv_context *drvc;
781 struct dev_context *devc;
784 if (sdi->status != SR_ST_ACTIVE)
785 return SR_ERR_DEV_CLOSED;
791 devc->ctx = drvc->sr_ctx;
792 devc->sent_samples = 0;
793 devc->empty_transfer_count = 0;
794 devc->acq_aborted = FALSE;
796 timeout = dslogic_get_timeout(devc);
797 usb_source_add(sdi->session, devc->ctx, timeout, receive_data, drvc);
799 trigger_request(sdi);
804 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
806 dslogic_stop_acquisition(sdi);
808 dslogic_abort_acquisition(sdi->priv);
813 static struct sr_dev_driver dslogic_driver_info = {
815 .longname = "DreamSourceLabs DSLogic",
818 .cleanup = std_cleanup,
820 .dev_list = std_dev_list,
821 .dev_clear = dev_clear,
822 .config_get = config_get,
823 .config_set = config_set,
824 .config_list = config_list,
825 .dev_open = dev_open,
826 .dev_close = dev_close,
827 .dev_acquisition_start = dev_acquisition_start,
828 .dev_acquisition_stop = dev_acquisition_stop,
831 SR_REGISTER_DEV_DRIVER(dslogic_driver_info);