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 * 1024 * 1024},
30 /* DreamSourceLab DSCope */
31 { 0x2a0e, 0x0002, "DreamSourceLab", "DSCope", NULL,
32 "dreamsourcelab-dscope-fx2.fw",
33 0, "DreamSourceLab", "DSCope", 256 * 1024 * 1024},
34 /* DreamSourceLab DSLogic Pro */
35 { 0x2a0e, 0x0003, "DreamSourceLab", "DSLogic Pro", NULL,
36 "dreamsourcelab-dslogic-pro-fx2.fw",
37 0, "DreamSourceLab", "DSLogic", 256 * 1024 * 1024},
38 /* DreamSourceLab DSLogic Plus */
39 { 0x2a0e, 0x0020, "DreamSourceLab", "DSLogic Plus", NULL,
40 "dreamsourcelab-dslogic-plus-fx2.fw",
41 0, "DreamSourceLab", "DSLogic", 256 * 1024 * 1024},
42 /* DreamSourceLab DSLogic Basic */
43 { 0x2a0e, 0x0021, "DreamSourceLab", "DSLogic Basic", NULL,
44 "dreamsourcelab-dslogic-basic-fx2.fw",
45 0, "DreamSourceLab", "DSLogic", 256 * 1024},
50 static const uint32_t scanopts[] = {
54 static const uint32_t drvopts[] = {
55 SR_CONF_LOGIC_ANALYZER,
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 static const char *signal_edge_names[] = {
71 [DS_EDGE_RISING] = "rising",
72 [DS_EDGE_FALLING] = "falling",
75 static const double voltage_thresholds[][2] = {
80 static const uint64_t samplerates[] = {
99 static gboolean is_plausible(const struct libusb_device_descriptor *des)
103 for (i = 0; supported_device[i].vid; i++) {
104 if (des->idVendor != supported_device[i].vid)
106 if (des->idProduct == supported_device[i].pid)
113 static GSList *scan(struct sr_dev_driver *di, GSList *options)
115 struct drv_context *drvc;
116 struct dev_context *devc;
117 struct sr_dev_inst *sdi;
118 struct sr_usb_dev_inst *usb;
119 struct sr_channel *ch;
120 struct sr_channel_group *cg;
121 struct sr_config *src;
122 const struct dslogic_profile *prof;
123 GSList *l, *devices, *conn_devices;
124 gboolean has_firmware;
125 struct libusb_device_descriptor des;
126 libusb_device **devlist;
127 struct libusb_device_handle *hdl;
130 char manufacturer[64], product[64], serial_num[64], connection_id[64];
131 char channel_name[16];
136 for (l = options; l; l = l->next) {
140 conn = g_variant_get_string(src->data, NULL);
145 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
149 /* Find all DSLogic compatible devices and upload firmware to them. */
151 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
152 for (i = 0; devlist[i]; i++) {
155 for (l = conn_devices; l; l = l->next) {
157 if (usb->bus == libusb_get_bus_number(devlist[i])
158 && usb->address == libusb_get_device_address(devlist[i]))
162 /* This device matched none of the ones that
163 * matched the conn specification. */
167 libusb_get_device_descriptor(devlist[i], &des);
169 if (!is_plausible(&des))
172 if ((ret = libusb_open(devlist[i], &hdl)) < 0) {
173 sr_warn("Failed to open potential device with "
174 "VID:PID %04x:%04x: %s.", des.idVendor,
175 des.idProduct, libusb_error_name(ret));
179 if (des.iManufacturer == 0) {
180 manufacturer[0] = '\0';
181 } else if ((ret = libusb_get_string_descriptor_ascii(hdl,
182 des.iManufacturer, (unsigned char *) manufacturer,
183 sizeof(manufacturer))) < 0) {
184 sr_warn("Failed to get manufacturer string descriptor: %s.",
185 libusb_error_name(ret));
189 if (des.iProduct == 0) {
191 } else if ((ret = libusb_get_string_descriptor_ascii(hdl,
192 des.iProduct, (unsigned char *) product,
193 sizeof(product))) < 0) {
194 sr_warn("Failed to get product string descriptor: %s.",
195 libusb_error_name(ret));
199 if (des.iSerialNumber == 0) {
200 serial_num[0] = '\0';
201 } else if ((ret = libusb_get_string_descriptor_ascii(hdl,
202 des.iSerialNumber, (unsigned char *) serial_num,
203 sizeof(serial_num))) < 0) {
204 sr_warn("Failed to get serial number string descriptor: %s.",
205 libusb_error_name(ret));
209 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
214 for (j = 0; supported_device[j].vid; j++) {
215 if (des.idVendor == supported_device[j].vid &&
216 des.idProduct == supported_device[j].pid) {
217 prof = &supported_device[j];
225 sdi = g_malloc0(sizeof(struct sr_dev_inst));
226 sdi->status = SR_ST_INITIALIZING;
227 sdi->vendor = g_strdup(prof->vendor);
228 sdi->model = g_strdup(prof->model);
229 sdi->version = g_strdup(prof->model_version);
230 sdi->serial_num = g_strdup(serial_num);
231 sdi->connection_id = g_strdup(connection_id);
233 /* Logic channels, all in one channel group. */
234 cg = g_malloc0(sizeof(struct sr_channel_group));
235 cg->name = g_strdup("Logic");
236 for (j = 0; j < 16; j++) {
237 sprintf(channel_name, "%d", j);
238 ch = sr_channel_new(sdi, j, SR_CHANNEL_LOGIC,
240 cg->channels = g_slist_append(cg->channels, ch);
242 sdi->channel_groups = g_slist_append(NULL, cg);
244 devc = dslogic_dev_new();
245 devc->profile = prof;
247 devices = g_slist_append(devices, sdi);
249 devc->samplerates = samplerates;
250 devc->num_samplerates = ARRAY_SIZE(samplerates);
251 has_firmware = usb_match_manuf_prod(devlist[i], "DreamSourceLab", "USB-based Instrument");
254 /* Already has the firmware, so fix the new address. */
255 sr_dbg("Found a DSLogic device.");
256 sdi->status = SR_ST_INACTIVE;
257 sdi->inst_type = SR_INST_USB;
258 sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
259 libusb_get_device_address(devlist[i]), NULL);
261 if (ezusb_upload_firmware(drvc->sr_ctx, devlist[i],
262 USB_CONFIGURATION, prof->firmware) == SR_OK)
263 /* Store when this device's FW was updated. */
264 devc->fw_updated = g_get_monotonic_time();
266 sr_err("Firmware upload failed for "
267 "device %d.%d (logical).",
268 libusb_get_bus_number(devlist[i]),
269 libusb_get_device_address(devlist[i]));
270 sdi->inst_type = SR_INST_USB;
271 sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
275 libusb_free_device_list(devlist, 1);
276 g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
278 return std_scan_complete(di, devices);
281 static int dev_open(struct sr_dev_inst *sdi)
283 struct sr_dev_driver *di = sdi->driver;
284 struct sr_usb_dev_inst *usb;
285 struct dev_context *devc;
287 int64_t timediff_us, timediff_ms;
293 * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS
294 * milliseconds for the FX2 to renumerate.
297 if (devc->fw_updated > 0) {
298 sr_info("Waiting for device to reset.");
299 /* Takes >= 300ms for the FX2 to be gone from the USB bus. */
300 g_usleep(300 * 1000);
302 while (timediff_ms < MAX_RENUM_DELAY_MS) {
303 if ((ret = dslogic_dev_open(sdi, di)) == SR_OK)
305 g_usleep(100 * 1000);
307 timediff_us = g_get_monotonic_time() - devc->fw_updated;
308 timediff_ms = timediff_us / 1000;
309 sr_spew("Waited %" PRIi64 "ms.", timediff_ms);
312 sr_err("Device failed to renumerate.");
315 sr_info("Device came back after %" PRIi64 "ms.", timediff_ms);
317 sr_info("Firmware upload was not needed.");
318 ret = dslogic_dev_open(sdi, di);
322 sr_err("Unable to open device.");
326 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
329 case LIBUSB_ERROR_BUSY:
330 sr_err("Unable to claim USB interface. Another "
331 "program or driver has already claimed it.");
333 case LIBUSB_ERROR_NO_DEVICE:
334 sr_err("Device has been disconnected.");
337 sr_err("Unable to claim interface: %s.",
338 libusb_error_name(ret));
346 if ((ret = dslogic_fpga_firmware_upload(sdi)) != SR_OK)
349 if (devc->cur_samplerate == 0) {
350 /* Samplerate hasn't been set; default to the slowest one. */
351 devc->cur_samplerate = devc->samplerates[0];
354 if (devc->cur_threshold == 0.0)
355 devc->cur_threshold = 1.5;
360 static int dev_close(struct sr_dev_inst *sdi)
362 struct sr_usb_dev_inst *usb;
369 sr_info("Closing device on %d.%d (logical) / %s (physical) interface %d.",
370 usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
371 libusb_release_interface(usb->devhdl, USB_INTERFACE);
372 libusb_close(usb->devhdl);
378 static int config_get(uint32_t key, GVariant **data,
379 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
381 struct dev_context *devc;
382 struct sr_usb_dev_inst *usb;
397 if (usb->address == 255)
398 /* Device still needs to re-enumerate after firmware
399 * upload, so we don't know its (future) address. */
401 *data = g_variant_new_printf("%d.%d", usb->bus, usb->address);
403 case SR_CONF_VOLTAGE_THRESHOLD:
404 if (!strcmp(devc->profile->model, "DSLogic")) {
405 if ((idx = std_double_tuple_idx_d0(devc->cur_threshold,
406 ARRAY_AND_SIZE(voltage_thresholds))) < 0)
408 *data = std_gvar_tuple_double(voltage_thresholds[idx][0],
409 voltage_thresholds[idx][1]);
411 *data = std_gvar_tuple_double(devc->cur_threshold, devc->cur_threshold);
414 case SR_CONF_LIMIT_SAMPLES:
415 *data = g_variant_new_uint64(devc->limit_samples);
417 case SR_CONF_SAMPLERATE:
418 *data = g_variant_new_uint64(devc->cur_samplerate);
420 case SR_CONF_CAPTURE_RATIO:
421 *data = g_variant_new_uint64(devc->capture_ratio);
423 case SR_CONF_EXTERNAL_CLOCK:
424 *data = g_variant_new_boolean(devc->external_clock);
426 case SR_CONF_CONTINUOUS:
427 *data = g_variant_new_boolean(devc->continuous_mode);
429 case SR_CONF_CLOCK_EDGE:
430 idx = devc->clock_edge;
431 if (idx >= (int)ARRAY_SIZE(signal_edge_names))
433 *data = g_variant_new_string(signal_edge_names[0]);
442 static int config_set(uint32_t key, GVariant *data,
443 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
445 struct dev_context *devc;
457 case SR_CONF_SAMPLERATE:
458 if ((idx = std_u64_idx(data, devc->samplerates, devc->num_samplerates)) < 0)
460 devc->cur_samplerate = devc->samplerates[idx];
462 case SR_CONF_LIMIT_SAMPLES:
463 devc->limit_samples = g_variant_get_uint64(data);
465 case SR_CONF_CAPTURE_RATIO:
466 devc->capture_ratio = g_variant_get_uint64(data);
467 return (devc->capture_ratio > 100) ? SR_ERR : SR_OK;
468 case SR_CONF_VOLTAGE_THRESHOLD:
469 if (!strcmp(devc->profile->model, "DSLogic")) {
470 if ((idx = std_double_tuple_idx(data, ARRAY_AND_SIZE(voltage_thresholds))) < 0)
472 devc->cur_threshold = voltage_thresholds[idx][0];
473 return dslogic_fpga_firmware_upload(sdi);
475 g_variant_get(data, "(dd)", &low, &high);
476 return dslogic_set_voltage_threshold(sdi, (low + high) / 2.0);
479 case SR_CONF_EXTERNAL_CLOCK:
480 devc->external_clock = g_variant_get_boolean(data);
482 case SR_CONF_CONTINUOUS:
483 devc->continuous_mode = g_variant_get_boolean(data);
485 case SR_CONF_CLOCK_EDGE:
486 if ((idx = std_str_idx(data, ARRAY_AND_SIZE(signal_edge_names))) < 0)
488 devc->clock_edge = idx;
497 static int config_list(uint32_t key, GVariant **data,
498 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
500 struct dev_context *devc;
502 devc = (sdi) ? sdi->priv : NULL;
505 case SR_CONF_SCAN_OPTIONS:
506 case SR_CONF_DEVICE_OPTIONS:
507 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
508 case SR_CONF_VOLTAGE_THRESHOLD:
509 if (!strcmp(devc->profile->model, "DSLogic"))
510 *data = std_gvar_thresholds(ARRAY_AND_SIZE(voltage_thresholds));
512 *data = std_gvar_min_max_step_thresholds(0.0, 5.0, 0.1);
514 case SR_CONF_SAMPLERATE:
515 *data = std_gvar_samplerates(devc->samplerates, devc->num_samplerates);
517 case SR_CONF_CLOCK_EDGE:
518 *data = g_variant_new_strv(ARRAY_AND_SIZE(signal_edge_names));
527 static struct sr_dev_driver dreamsourcelab_dslogic_driver_info = {
528 .name = "dreamsourcelab-dslogic",
529 .longname = "DreamSourceLab DSLogic",
532 .cleanup = std_cleanup,
534 .dev_list = std_dev_list,
535 .dev_clear = std_dev_clear,
536 .config_get = config_get,
537 .config_set = config_set,
538 .config_list = config_list,
539 .dev_open = dev_open,
540 .dev_close = dev_close,
541 .dev_acquisition_start = dslogic_acquisition_start,
542 .dev_acquisition_stop = dslogic_acquisition_stop,
545 SR_REGISTER_DEV_DRIVER(dreamsourcelab_dslogic_driver_info);