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/>.
25 static const struct dslogic_profile supported_device[] = {
26 /* DreamSourceLab DSLogic */
27 { 0x2a0e, 0x0001, "DreamSourceLab", "DSLogic", NULL,
28 "dreamsourcelab-dslogic-fx2.fw",
29 0, "DreamSourceLab", "DSLogic", 256 * 1048576ULL},
30 /* DreamSourceLab DSCope */
31 { 0x2a0e, 0x0002, "DreamSourceLab", "DSCope", NULL,
32 "dreamsourcelab-dscope-fx2.fw",
33 0, "DreamSourceLab", "DSCope", 256 * 1048576ULL},
34 /* DreamSourceLab DSLogic Pro */
35 { 0x2a0e, 0x0003, "DreamSourceLab", "DSLogic Pro", NULL,
36 "dreamsourcelab-dslogic-pro-fx2.fw",
37 0, "DreamSourceLab", "DSLogic", 256 * 1048576ULL},
38 /* DreamSourceLab DSLogic Plus */
39 { 0x2a0e, 0x0020, "DreamSourceLab", "DSLogic Plus", NULL,
40 "dreamsourcelab-dslogic-plus-fx2.fw",
41 0, "DreamSourceLab", "DSLogic", 256 * 1048576ULL},
42 /* DreamSourceLab DSLogic Basic */
43 { 0x2a0e, 0x0021, "DreamSourceLab", "DSLogic Basic", NULL,
44 "dreamsourcelab-dslogic-basic-fx2.fw",
45 0, "DreamSourceLab", "DSLogic", 256 * 1024ULL},
50 static const uint32_t drvopts[] = {
51 SR_CONF_LOGIC_ANALYZER,
54 static const uint32_t scanopts[] = {
58 static const uint32_t devopts[] = {
59 SR_CONF_CONTINUOUS | SR_CONF_SET | SR_CONF_GET,
60 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
61 SR_CONF_VOLTAGE_THRESHOLD | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
62 SR_CONF_CONN | SR_CONF_GET,
63 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
64 SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
65 SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET,
66 SR_CONF_EXTERNAL_CLOCK | SR_CONF_GET | SR_CONF_SET,
67 SR_CONF_CLOCK_EDGE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
70 /* Names assigned to available edge slope choices. */
71 static const char *const signal_edge_names[] = {
72 [DS_EDGE_RISING] = "rising",
73 [DS_EDGE_FALLING] = "falling",
79 } dslogic_voltage_thresholds[] = {
84 static const uint64_t samplerates[] = {
103 static gboolean is_plausible(const struct libusb_device_descriptor *des)
107 for (i = 0; supported_device[i].vid; i++) {
108 if (des->idVendor != supported_device[i].vid)
110 if (des->idProduct == supported_device[i].pid)
117 static GSList *scan(struct sr_dev_driver *di, GSList *options)
119 struct drv_context *drvc;
120 struct dev_context *devc;
121 struct sr_dev_inst *sdi;
122 struct sr_usb_dev_inst *usb;
123 struct sr_channel *ch;
124 struct sr_channel_group *cg;
125 struct sr_config *src;
126 const struct dslogic_profile *prof;
127 GSList *l, *devices, *conn_devices;
128 gboolean has_firmware;
129 struct libusb_device_descriptor des;
130 libusb_device **devlist;
131 struct libusb_device_handle *hdl;
134 char manufacturer[64], product[64], serial_num[64], connection_id[64];
135 char channel_name[16];
140 for (l = options; l; l = l->next) {
144 conn = g_variant_get_string(src->data, NULL);
149 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
153 /* Find all dslogic compatible devices and upload firmware to them. */
155 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
156 for (i = 0; devlist[i]; i++) {
159 for (l = conn_devices; l; l = l->next) {
161 if (usb->bus == libusb_get_bus_number(devlist[i])
162 && usb->address == libusb_get_device_address(devlist[i]))
166 /* This device matched none of the ones that
167 * matched the conn specification. */
171 libusb_get_device_descriptor( devlist[i], &des);
173 if (!is_plausible(&des))
176 if ((ret = libusb_open(devlist[i], &hdl)) < 0) {
177 sr_warn("Failed to open potential device with "
178 "VID:PID %04x:%04x: %s.", des.idVendor,
179 des.idProduct, libusb_error_name(ret));
183 if (des.iManufacturer == 0) {
184 manufacturer[0] = '\0';
185 } else if ((ret = libusb_get_string_descriptor_ascii(hdl,
186 des.iManufacturer, (unsigned char *) manufacturer,
187 sizeof(manufacturer))) < 0) {
188 sr_warn("Failed to get manufacturer string descriptor: %s.",
189 libusb_error_name(ret));
193 if (des.iProduct == 0) {
195 } else if ((ret = libusb_get_string_descriptor_ascii(hdl,
196 des.iProduct, (unsigned char *) product,
197 sizeof(product))) < 0) {
198 sr_warn("Failed to get product string descriptor: %s.",
199 libusb_error_name(ret));
203 if (des.iSerialNumber == 0) {
204 serial_num[0] = '\0';
205 } else if ((ret = libusb_get_string_descriptor_ascii(hdl,
206 des.iSerialNumber, (unsigned char *) serial_num,
207 sizeof(serial_num))) < 0) {
208 sr_warn("Failed to get serial number string descriptor: %s.",
209 libusb_error_name(ret));
213 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
218 for (j = 0; supported_device[j].vid; j++) {
219 if (des.idVendor == supported_device[j].vid &&
220 des.idProduct == supported_device[j].pid &&
221 (!strcmp(manufacturer, supported_device[j].usb_manufacturer)) &&
222 (!strcmp(product, "USB-based Instrument") ||
223 !strcmp(product, supported_device[j].usb_product))) {
224 prof = &supported_device[j];
229 /* Skip if the device was not found. */
233 sdi = g_malloc0(sizeof(struct sr_dev_inst));
234 sdi->status = SR_ST_INITIALIZING;
235 sdi->vendor = g_strdup(prof->vendor);
236 sdi->model = g_strdup(prof->model);
237 sdi->version = g_strdup(prof->model_version);
238 sdi->serial_num = g_strdup(serial_num);
239 sdi->connection_id = g_strdup(connection_id);
241 /* Logic channels, all in one channel group. */
242 cg = g_malloc0(sizeof(struct sr_channel_group));
243 cg->name = g_strdup("Logic");
244 for (j = 0; j < 16; j++) {
245 sprintf(channel_name, "%d", j);
246 ch = sr_channel_new(sdi, j, SR_CHANNEL_LOGIC,
248 cg->channels = g_slist_append(cg->channels, ch);
250 sdi->channel_groups = g_slist_append(NULL, cg);
252 devc = dslogic_dev_new();
253 devc->profile = prof;
255 devices = g_slist_append(devices, sdi);
257 devc->samplerates = samplerates;
258 devc->num_samplerates = ARRAY_SIZE(samplerates);
259 has_firmware = usb_match_manuf_prod(devlist[i], "DreamSourceLab", "USB-based Instrument");
262 /* Already has the firmware, so fix the new address. */
263 sr_dbg("Found an dslogic device.");
264 sdi->status = SR_ST_INACTIVE;
265 sdi->inst_type = SR_INST_USB;
266 sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
267 libusb_get_device_address(devlist[i]), NULL);
269 if (ezusb_upload_firmware(drvc->sr_ctx, devlist[i],
270 USB_CONFIGURATION, prof->firmware) == SR_OK)
271 /* Store when this device's FW was updated. */
272 devc->fw_updated = g_get_monotonic_time();
274 sr_err("Firmware upload failed for "
275 "device %d.%d (logical).",
276 libusb_get_bus_number(devlist[i]),
277 libusb_get_device_address(devlist[i]));
278 sdi->inst_type = SR_INST_USB;
279 sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
283 libusb_free_device_list(devlist, 1);
284 g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
286 return std_scan_complete(di, devices);
289 static void clear_dev_context(void *priv)
291 struct dev_context *devc;
297 static int dev_clear(const struct sr_dev_driver *di)
299 return std_dev_clear(di, clear_dev_context);
302 static int dev_open(struct sr_dev_inst *sdi)
304 struct sr_dev_driver *di = sdi->driver;
305 struct sr_usb_dev_inst *usb;
306 struct dev_context *devc;
308 int64_t timediff_us, timediff_ms;
314 * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS
315 * milliseconds for the FX2 to renumerate.
318 if (devc->fw_updated > 0) {
319 sr_info("Waiting for device to reset.");
320 /* Takes >= 300ms for the FX2 to be gone from the USB bus. */
321 g_usleep(300 * 1000);
323 while (timediff_ms < MAX_RENUM_DELAY_MS) {
324 if ((ret = dslogic_dev_open(sdi, di)) == SR_OK)
326 g_usleep(100 * 1000);
328 timediff_us = g_get_monotonic_time() - devc->fw_updated;
329 timediff_ms = timediff_us / 1000;
330 sr_spew("Waited %" PRIi64 "ms.", timediff_ms);
333 sr_err("Device failed to renumerate.");
336 sr_info("Device came back after %" PRIi64 "ms.", timediff_ms);
338 sr_info("Firmware upload was not needed.");
339 ret = dslogic_dev_open(sdi, di);
343 sr_err("Unable to open device.");
347 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
350 case LIBUSB_ERROR_BUSY:
351 sr_err("Unable to claim USB interface. Another "
352 "program or driver has already claimed it.");
354 case LIBUSB_ERROR_NO_DEVICE:
355 sr_err("Device has been disconnected.");
358 sr_err("Unable to claim interface: %s.",
359 libusb_error_name(ret));
367 if ((ret = dslogic_fpga_firmware_upload(sdi)) != SR_OK)
370 if (devc->cur_samplerate == 0) {
371 /* Samplerate hasn't been set; default to the slowest one. */
372 devc->cur_samplerate = devc->samplerates[0];
375 if (devc->cur_threshold == 0.0)
376 devc->cur_threshold = 1.5;
381 static int dev_close(struct sr_dev_inst *sdi)
383 struct sr_usb_dev_inst *usb;
390 sr_info("dslogic: Closing device on %d.%d (logical) / %s (physical) interface %d.",
391 usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
392 libusb_release_interface(usb->devhdl, USB_INTERFACE);
393 libusb_close(usb->devhdl);
395 sdi->status = SR_ST_INACTIVE;
400 static int config_get(uint32_t key, GVariant **data,
401 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
403 struct dev_context *devc;
404 struct sr_usb_dev_inst *usb;
406 unsigned int i, voltage_range;
421 if (usb->address == 255)
422 /* Device still needs to re-enumerate after firmware
423 * upload, so we don't know its (future) address. */
425 snprintf(str, 128, "%d.%d", usb->bus, usb->address);
426 *data = g_variant_new_string(str);
428 case SR_CONF_VOLTAGE_THRESHOLD:
429 if (!strcmp(devc->profile->model, "DSLogic")) {
432 for (i = 0; i < ARRAY_SIZE(dslogic_voltage_thresholds); i++)
433 if (dslogic_voltage_thresholds[i].low ==
434 devc->cur_threshold) {
439 range[0] = g_variant_new_double(
440 dslogic_voltage_thresholds[voltage_range].low);
441 range[1] = g_variant_new_double(
442 dslogic_voltage_thresholds[voltage_range].high);
444 range[0] = g_variant_new_double(devc->cur_threshold);
445 range[1] = g_variant_new_double(devc->cur_threshold);
447 *data = g_variant_new_tuple(range, 2);
449 case SR_CONF_LIMIT_SAMPLES:
450 *data = g_variant_new_uint64(devc->limit_samples);
452 case SR_CONF_SAMPLERATE:
453 *data = g_variant_new_uint64(devc->cur_samplerate);
455 case SR_CONF_CAPTURE_RATIO:
456 *data = g_variant_new_uint64(devc->capture_ratio);
458 case SR_CONF_EXTERNAL_CLOCK:
459 *data = g_variant_new_boolean(devc->external_clock);
461 case SR_CONF_CONTINUOUS:
462 *data = g_variant_new_boolean(devc->continuous_mode);
464 case SR_CONF_CLOCK_EDGE:
465 i = devc->clock_edge;
466 if (i >= ARRAY_SIZE(signal_edge_names))
468 *data = g_variant_new_string(signal_edge_names[0]);
478 * Helper for mapping a string-typed configuration value to an index
479 * within a table of possible values.
481 static int lookup_index(GVariant *value, const char *const *table, int len)
486 entry = g_variant_get_string(value, NULL);
490 /* Linear search is fine for very small tables. */
491 for (i = 0; i < len; i++) {
492 if (strcmp(entry, table[i]) == 0)
499 static int config_set(uint32_t key, GVariant *data,
500 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
502 struct dev_context *devc;
512 if (sdi->status != SR_ST_ACTIVE)
520 case SR_CONF_SAMPLERATE:
521 arg = g_variant_get_uint64(data);
522 for (i = 0; i < devc->num_samplerates; i++) {
523 if (devc->samplerates[i] == arg) {
524 devc->cur_samplerate = arg;
528 if (i == devc->num_samplerates)
531 case SR_CONF_LIMIT_SAMPLES:
532 devc->limit_samples = g_variant_get_uint64(data);
534 case SR_CONF_CAPTURE_RATIO:
535 devc->capture_ratio = g_variant_get_uint64(data);
536 ret = (devc->capture_ratio > 100) ? SR_ERR : SR_OK;
538 case SR_CONF_VOLTAGE_THRESHOLD:
539 g_variant_get(data, "(dd)", &low, &high);
540 if (!strcmp(devc->profile->model, "DSLogic")) {
541 for (i = 0; (unsigned int)i <
542 ARRAY_SIZE(dslogic_voltage_thresholds); i++) {
543 if (fabs(dslogic_voltage_thresholds[i].low - low) < 0.1 &&
544 fabs(dslogic_voltage_thresholds[i].high - high) < 0.1) {
545 devc->cur_threshold =
546 dslogic_voltage_thresholds[i].low;
550 ret = dslogic_fpga_firmware_upload(sdi);
552 ret = dslogic_set_voltage_threshold(sdi, (low + high) / 2.0);
555 case SR_CONF_EXTERNAL_CLOCK:
556 devc->external_clock = g_variant_get_boolean(data);
558 case SR_CONF_CONTINUOUS:
559 devc->continuous_mode = g_variant_get_boolean(data);
561 case SR_CONF_CLOCK_EDGE:
562 i = lookup_index(data, signal_edge_names,
563 ARRAY_SIZE(signal_edge_names));
566 devc->clock_edge = i;
575 static int config_list(uint32_t key, GVariant **data,
576 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
578 struct dev_context *devc = NULL;
579 GVariant *gvar, *range[2];
587 case SR_CONF_SCAN_OPTIONS:
588 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
589 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
591 case SR_CONF_DEVICE_OPTIONS:
593 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
594 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
597 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
598 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
601 case SR_CONF_VOLTAGE_THRESHOLD:
604 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
605 if (devc && !strcmp(devc->profile->model, "DSLogic")) {
606 for (i = 0; i < ARRAY_SIZE(dslogic_voltage_thresholds); i++) {
607 range[0] = g_variant_new_double(dslogic_voltage_thresholds[i].low);
608 range[1] = g_variant_new_double(dslogic_voltage_thresholds[i].high);
609 gvar = g_variant_new_tuple(range, 2);
610 g_variant_builder_add_value(&gvb, gvar);
613 for (v = 0.0; v <= 5.0; v += 0.1) {
614 range[0] = g_variant_new_double(v);
615 range[1] = g_variant_new_double(v);
616 gvar = g_variant_new_tuple(range, 2);
617 g_variant_builder_add_value(&gvb, gvar);
620 *data = g_variant_builder_end(&gvb);
622 case SR_CONF_SAMPLERATE:
624 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
625 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), devc->samplerates,
626 devc->num_samplerates, sizeof(uint64_t));
627 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
628 *data = g_variant_builder_end(&gvb);
630 case SR_CONF_CLOCK_EDGE:
631 *data = g_variant_new_strv(signal_edge_names,
632 ARRAY_SIZE(signal_edge_names));
641 static struct sr_dev_driver dslogic_driver_info = {
643 .longname = "DreamSourceLabs DSLogic",
646 .cleanup = std_cleanup,
648 .dev_list = std_dev_list,
649 .dev_clear = dev_clear,
650 .config_get = config_get,
651 .config_set = config_set,
652 .config_list = config_list,
653 .dev_open = dev_open,
654 .dev_close = dev_close,
655 .dev_acquisition_start = dslogic_acquisition_start,
656 .dev_acquisition_stop = dslogic_acquisition_stop,
659 SR_REGISTER_DEV_DRIVER(dslogic_driver_info);