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 int32_t trigger_matches[] = {
78 static const char *signal_edges[] = {
79 [DS_EDGE_RISING] = "rising",
80 [DS_EDGE_FALLING] = "falling",
83 static const double thresholds[][2] = {
88 static const uint64_t samplerates[] = {
107 static gboolean is_plausible(const struct libusb_device_descriptor *des)
111 for (i = 0; supported_device[i].vid; i++) {
112 if (des->idVendor != supported_device[i].vid)
114 if (des->idProduct == supported_device[i].pid)
121 static GSList *scan(struct sr_dev_driver *di, GSList *options)
123 struct drv_context *drvc;
124 struct dev_context *devc;
125 struct sr_dev_inst *sdi;
126 struct sr_usb_dev_inst *usb;
127 struct sr_channel *ch;
128 struct sr_channel_group *cg;
129 struct sr_config *src;
130 const struct dslogic_profile *prof;
131 GSList *l, *devices, *conn_devices;
132 gboolean has_firmware;
133 struct libusb_device_descriptor des;
134 libusb_device **devlist;
135 struct libusb_device_handle *hdl;
138 char manufacturer[64], product[64], serial_num[64], connection_id[64];
139 char channel_name[16];
144 for (l = options; l; l = l->next) {
148 conn = g_variant_get_string(src->data, NULL);
153 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
157 /* Find all DSLogic compatible devices and upload firmware to them. */
159 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
160 for (i = 0; devlist[i]; i++) {
163 for (l = conn_devices; l; l = l->next) {
165 if (usb->bus == libusb_get_bus_number(devlist[i])
166 && usb->address == libusb_get_device_address(devlist[i]))
170 /* This device matched none of the ones that
171 * matched the conn specification. */
175 libusb_get_device_descriptor(devlist[i], &des);
177 if (!is_plausible(&des))
180 if ((ret = libusb_open(devlist[i], &hdl)) < 0) {
181 sr_warn("Failed to open potential device with "
182 "VID:PID %04x:%04x: %s.", des.idVendor,
183 des.idProduct, libusb_error_name(ret));
187 if (des.iManufacturer == 0) {
188 manufacturer[0] = '\0';
189 } else if ((ret = libusb_get_string_descriptor_ascii(hdl,
190 des.iManufacturer, (unsigned char *) manufacturer,
191 sizeof(manufacturer))) < 0) {
192 sr_warn("Failed to get manufacturer string descriptor: %s.",
193 libusb_error_name(ret));
197 if (des.iProduct == 0) {
199 } else if ((ret = libusb_get_string_descriptor_ascii(hdl,
200 des.iProduct, (unsigned char *) product,
201 sizeof(product))) < 0) {
202 sr_warn("Failed to get product string descriptor: %s.",
203 libusb_error_name(ret));
207 if (des.iSerialNumber == 0) {
208 serial_num[0] = '\0';
209 } else if ((ret = libusb_get_string_descriptor_ascii(hdl,
210 des.iSerialNumber, (unsigned char *) serial_num,
211 sizeof(serial_num))) < 0) {
212 sr_warn("Failed to get serial number string descriptor: %s.",
213 libusb_error_name(ret));
219 if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0)
223 for (j = 0; supported_device[j].vid; j++) {
224 if (des.idVendor == supported_device[j].vid &&
225 des.idProduct == supported_device[j].pid) {
226 prof = &supported_device[j];
234 sdi = g_malloc0(sizeof(struct sr_dev_inst));
235 sdi->status = SR_ST_INITIALIZING;
236 sdi->vendor = g_strdup(prof->vendor);
237 sdi->model = g_strdup(prof->model);
238 sdi->version = g_strdup(prof->model_version);
239 sdi->serial_num = g_strdup(serial_num);
240 sdi->connection_id = g_strdup(connection_id);
242 /* Logic channels, all in one channel group. */
243 cg = g_malloc0(sizeof(struct sr_channel_group));
244 cg->name = g_strdup("Logic");
245 for (j = 0; j < NUM_CHANNELS; j++) {
246 sprintf(channel_name, "%d", j);
247 ch = sr_channel_new(sdi, j, SR_CHANNEL_LOGIC,
249 cg->channels = g_slist_append(cg->channels, ch);
251 sdi->channel_groups = g_slist_append(NULL, cg);
253 devc = dslogic_dev_new();
254 devc->profile = prof;
256 devices = g_slist_append(devices, sdi);
258 devc->samplerates = samplerates;
259 devc->num_samplerates = ARRAY_SIZE(samplerates);
260 has_firmware = usb_match_manuf_prod(devlist[i], "DreamSourceLab", "USB-based Instrument");
263 /* Already has the firmware, so fix the new address. */
264 sr_dbg("Found a DSLogic device.");
265 sdi->status = SR_ST_INACTIVE;
266 sdi->inst_type = SR_INST_USB;
267 sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
268 libusb_get_device_address(devlist[i]), NULL);
270 if (ezusb_upload_firmware(drvc->sr_ctx, devlist[i],
271 USB_CONFIGURATION, prof->firmware) == SR_OK) {
272 /* Store when this device's FW was updated. */
273 devc->fw_updated = g_get_monotonic_time();
275 sr_err("Firmware upload failed for "
276 "device %d.%d (logical), name %s.",
277 libusb_get_bus_number(devlist[i]),
278 libusb_get_device_address(devlist[i]),
281 sdi->inst_type = SR_INST_USB;
282 sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
286 libusb_free_device_list(devlist, 1);
287 g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
289 return std_scan_complete(di, devices);
292 static int dev_open(struct sr_dev_inst *sdi)
294 struct sr_dev_driver *di = sdi->driver;
295 struct sr_usb_dev_inst *usb;
296 struct dev_context *devc;
298 int64_t timediff_us, timediff_ms;
304 * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS
305 * milliseconds for the FX2 to renumerate.
308 if (devc->fw_updated > 0) {
309 sr_info("Waiting for device to reset.");
310 /* Takes >= 300ms for the FX2 to be gone from the USB bus. */
311 g_usleep(300 * 1000);
313 while (timediff_ms < MAX_RENUM_DELAY_MS) {
314 if ((ret = dslogic_dev_open(sdi, di)) == SR_OK)
316 g_usleep(100 * 1000);
318 timediff_us = g_get_monotonic_time() - devc->fw_updated;
319 timediff_ms = timediff_us / 1000;
320 sr_spew("Waited %" PRIi64 "ms.", timediff_ms);
323 sr_err("Device failed to renumerate.");
326 sr_info("Device came back after %" PRIi64 "ms.", timediff_ms);
328 sr_info("Firmware upload was not needed.");
329 ret = dslogic_dev_open(sdi, di);
333 sr_err("Unable to open device.");
337 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
340 case LIBUSB_ERROR_BUSY:
341 sr_err("Unable to claim USB interface. Another "
342 "program or driver has already claimed it.");
344 case LIBUSB_ERROR_NO_DEVICE:
345 sr_err("Device has been disconnected.");
348 sr_err("Unable to claim interface: %s.",
349 libusb_error_name(ret));
357 if ((ret = dslogic_fpga_firmware_upload(sdi)) != SR_OK)
360 if (devc->cur_samplerate == 0) {
361 /* Samplerate hasn't been set; default to the slowest one. */
362 devc->cur_samplerate = devc->samplerates[0];
365 if (devc->cur_threshold == 0.0) {
366 devc->cur_threshold = thresholds[1][0];
367 return dslogic_set_voltage_threshold(sdi, devc->cur_threshold);
373 static int dev_close(struct sr_dev_inst *sdi)
375 struct sr_usb_dev_inst *usb;
382 sr_info("Closing device on %d.%d (logical) / %s (physical) interface %d.",
383 usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
384 libusb_release_interface(usb->devhdl, USB_INTERFACE);
385 libusb_close(usb->devhdl);
391 static int config_get(uint32_t key, GVariant **data,
392 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
394 struct dev_context *devc;
395 struct sr_usb_dev_inst *usb;
410 if (usb->address == 255)
411 /* Device still needs to re-enumerate after firmware
412 * upload, so we don't know its (future) address. */
414 *data = g_variant_new_printf("%d.%d", usb->bus, usb->address);
416 case SR_CONF_VOLTAGE_THRESHOLD:
417 if (!strcmp(devc->profile->model, "DSLogic")) {
418 if ((idx = std_double_tuple_idx_d0(devc->cur_threshold,
419 ARRAY_AND_SIZE(thresholds))) < 0)
421 *data = std_gvar_tuple_double(thresholds[idx][0],
424 *data = std_gvar_tuple_double(devc->cur_threshold, devc->cur_threshold);
427 case SR_CONF_LIMIT_SAMPLES:
428 *data = g_variant_new_uint64(devc->limit_samples);
430 case SR_CONF_SAMPLERATE:
431 *data = g_variant_new_uint64(devc->cur_samplerate);
433 case SR_CONF_CAPTURE_RATIO:
434 *data = g_variant_new_uint64(devc->capture_ratio);
436 case SR_CONF_EXTERNAL_CLOCK:
437 *data = g_variant_new_boolean(devc->external_clock);
439 case SR_CONF_CONTINUOUS:
440 *data = g_variant_new_boolean(devc->continuous_mode);
442 case SR_CONF_CLOCK_EDGE:
443 idx = devc->clock_edge;
444 if (idx >= (int)ARRAY_SIZE(signal_edges))
446 *data = g_variant_new_string(signal_edges[0]);
455 static int config_set(uint32_t key, GVariant *data,
456 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
458 struct dev_context *devc;
470 case SR_CONF_SAMPLERATE:
471 if ((idx = std_u64_idx(data, devc->samplerates, devc->num_samplerates)) < 0)
473 devc->cur_samplerate = devc->samplerates[idx];
475 case SR_CONF_LIMIT_SAMPLES:
476 devc->limit_samples = g_variant_get_uint64(data);
478 case SR_CONF_CAPTURE_RATIO:
479 devc->capture_ratio = g_variant_get_uint64(data);
481 case SR_CONF_VOLTAGE_THRESHOLD:
482 if (!strcmp(devc->profile->model, "DSLogic")) {
483 if ((idx = std_double_tuple_idx(data, ARRAY_AND_SIZE(thresholds))) < 0)
485 devc->cur_threshold = thresholds[idx][0];
486 return dslogic_fpga_firmware_upload(sdi);
488 g_variant_get(data, "(dd)", &low, &high);
489 return dslogic_set_voltage_threshold(sdi, (low + high) / 2.0);
492 case SR_CONF_EXTERNAL_CLOCK:
493 devc->external_clock = g_variant_get_boolean(data);
495 case SR_CONF_CONTINUOUS:
496 devc->continuous_mode = g_variant_get_boolean(data);
498 case SR_CONF_CLOCK_EDGE:
499 if ((idx = std_str_idx(data, ARRAY_AND_SIZE(signal_edges))) < 0)
501 devc->clock_edge = idx;
510 static int config_list(uint32_t key, GVariant **data,
511 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
513 struct dev_context *devc;
515 devc = (sdi) ? sdi->priv : NULL;
518 case SR_CONF_SCAN_OPTIONS:
519 case SR_CONF_DEVICE_OPTIONS:
520 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
521 case SR_CONF_VOLTAGE_THRESHOLD:
522 if (!devc || !devc->profile)
524 if (!strcmp(devc->profile->model, "DSLogic"))
525 *data = std_gvar_thresholds(ARRAY_AND_SIZE(thresholds));
527 *data = std_gvar_min_max_step_thresholds(0.0, 5.0, 0.1);
529 case SR_CONF_SAMPLERATE:
532 *data = std_gvar_samplerates(devc->samplerates, devc->num_samplerates);
534 case SR_CONF_TRIGGER_MATCH:
535 *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
537 case SR_CONF_CLOCK_EDGE:
538 *data = g_variant_new_strv(ARRAY_AND_SIZE(signal_edges));
547 static struct sr_dev_driver dreamsourcelab_dslogic_driver_info = {
548 .name = "dreamsourcelab-dslogic",
549 .longname = "DreamSourceLab DSLogic",
552 .cleanup = std_cleanup,
554 .dev_list = std_dev_list,
555 .dev_clear = std_dev_clear,
556 .config_get = config_get,
557 .config_set = config_set,
558 .config_list = config_list,
559 .dev_open = dev_open,
560 .dev_close = dev_close,
561 .dev_acquisition_start = dslogic_acquisition_start,
562 .dev_acquisition_stop = dslogic_acquisition_stop,
565 SR_REGISTER_DEV_DRIVER(dreamsourcelab_dslogic_driver_info);