]> sigrok.org Git - libsigrok.git/blame - src/hardware/dreamsourcelab-dslogic/api.c
drivers: Factor out std_gvar_thresholds().
[libsigrok.git] / src / hardware / dreamsourcelab-dslogic / api.c
CommitLineData
adcb9951
JH
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
5 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
6 *
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.
11 *
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.
16 *
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/>.
19 */
20
21#include <config.h>
adcb9951 22#include <math.h>
4bd770f5 23#include "protocol.h"
adcb9951
JH
24
25static const struct dslogic_profile supported_device[] = {
6c317a8d 26 /* DreamSourceLab DSLogic */
adcb9951
JH
27 { 0x2a0e, 0x0001, "DreamSourceLab", "DSLogic", NULL,
28 "dreamsourcelab-dslogic-fx2.fw",
44b46d70 29 0, "DreamSourceLab", "DSLogic", 256 * 1024 * 1024},
6c317a8d 30 /* DreamSourceLab DSCope */
adcb9951
JH
31 { 0x2a0e, 0x0002, "DreamSourceLab", "DSCope", NULL,
32 "dreamsourcelab-dscope-fx2.fw",
44b46d70 33 0, "DreamSourceLab", "DSCope", 256 * 1024 * 1024},
6c317a8d 34 /* DreamSourceLab DSLogic Pro */
adcb9951
JH
35 { 0x2a0e, 0x0003, "DreamSourceLab", "DSLogic Pro", NULL,
36 "dreamsourcelab-dslogic-pro-fx2.fw",
44b46d70 37 0, "DreamSourceLab", "DSLogic", 256 * 1024 * 1024},
6c317a8d 38 /* DreamSourceLab DSLogic Plus */
adcb9951
JH
39 { 0x2a0e, 0x0020, "DreamSourceLab", "DSLogic Plus", NULL,
40 "dreamsourcelab-dslogic-plus-fx2.fw",
44b46d70 41 0, "DreamSourceLab", "DSLogic", 256 * 1024 * 1024},
6c317a8d 42 /* DreamSourceLab DSLogic Basic */
adcb9951
JH
43 { 0x2a0e, 0x0021, "DreamSourceLab", "DSLogic Basic", NULL,
44 "dreamsourcelab-dslogic-basic-fx2.fw",
44b46d70 45 0, "DreamSourceLab", "DSLogic", 256 * 1024},
adcb9951
JH
46
47 ALL_ZERO
48};
49
adcb9951
JH
50static const uint32_t scanopts[] = {
51 SR_CONF_CONN,
52};
53
55fb76b3
UH
54static const uint32_t drvopts[] = {
55 SR_CONF_LOGIC_ANALYZER,
56};
57
adcb9951
JH
58static 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,
68};
69
adcb9951
JH
70static const char *const signal_edge_names[] = {
71 [DS_EDGE_RISING] = "rising",
72 [DS_EDGE_FALLING] = "falling",
73};
74
9fb9afb5 75static const struct voltage_threshold voltage_thresholds[] = {
1ee70746
JH
76 { 0.7, 1.4 },
77 { 1.4, 3.6 },
adcb9951
JH
78};
79
80static const uint64_t samplerates[] = {
81 SR_KHZ(10),
82 SR_KHZ(20),
83 SR_KHZ(50),
84 SR_KHZ(100),
85 SR_KHZ(200),
86 SR_KHZ(500),
87 SR_MHZ(1),
88 SR_MHZ(2),
89 SR_MHZ(5),
90 SR_MHZ(10),
91 SR_MHZ(20),
92 SR_MHZ(25),
93 SR_MHZ(50),
94 SR_MHZ(100),
95 SR_MHZ(200),
96 SR_MHZ(400),
97};
98
99static gboolean is_plausible(const struct libusb_device_descriptor *des)
100{
101 int i;
102
103 for (i = 0; supported_device[i].vid; i++) {
104 if (des->idVendor != supported_device[i].vid)
105 continue;
106 if (des->idProduct == supported_device[i].pid)
107 return TRUE;
108 }
109
110 return FALSE;
111}
112
113static GSList *scan(struct sr_dev_driver *di, GSList *options)
114{
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;
128 int ret, i, j;
129 const char *conn;
130 char manufacturer[64], product[64], serial_num[64], connection_id[64];
131 char channel_name[16];
132
133 drvc = di->context;
134
135 conn = NULL;
136 for (l = options; l; l = l->next) {
137 src = l->data;
138 switch (src->key) {
139 case SR_CONF_CONN:
140 conn = g_variant_get_string(src->data, NULL);
141 break;
142 }
143 }
144 if (conn)
145 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
146 else
147 conn_devices = NULL;
148
44b46d70 149 /* Find all DSLogic compatible devices and upload firmware to them. */
adcb9951
JH
150 devices = NULL;
151 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
152 for (i = 0; devlist[i]; i++) {
153 if (conn) {
154 usb = NULL;
155 for (l = conn_devices; l; l = l->next) {
156 usb = l->data;
157 if (usb->bus == libusb_get_bus_number(devlist[i])
158 && usb->address == libusb_get_device_address(devlist[i]))
159 break;
160 }
161 if (!l)
162 /* This device matched none of the ones that
163 * matched the conn specification. */
164 continue;
165 }
166
44b46d70 167 libusb_get_device_descriptor(devlist[i], &des);
adcb9951
JH
168
169 if (!is_plausible(&des))
170 continue;
171
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));
176 continue;
177 }
178
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));
186 continue;
187 }
188
189 if (des.iProduct == 0) {
190 product[0] = '\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));
196 continue;
197 }
198
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));
206 continue;
207 }
208
209 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
210
211 libusb_close(hdl);
212
213 prof = NULL;
214 for (j = 0; supported_device[j].vid; j++) {
215 if (des.idVendor == supported_device[j].vid &&
4984ca28 216 des.idProduct == supported_device[j].pid) {
adcb9951
JH
217 prof = &supported_device[j];
218 break;
219 }
220 }
221
adcb9951
JH
222 if (!prof)
223 continue;
224
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);
232
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++) {
84f6d40a 237 sprintf(channel_name, "%d", j);
adcb9951
JH
238 ch = sr_channel_new(sdi, j, SR_CHANNEL_LOGIC,
239 TRUE, channel_name);
240 cg->channels = g_slist_append(cg->channels, ch);
241 }
242 sdi->channel_groups = g_slist_append(NULL, cg);
243
244 devc = dslogic_dev_new();
245 devc->profile = prof;
246 sdi->priv = devc;
247 devices = g_slist_append(devices, sdi);
248
249 devc->samplerates = samplerates;
250 devc->num_samplerates = ARRAY_SIZE(samplerates);
3c749da1 251 has_firmware = usb_match_manuf_prod(devlist[i], "DreamSourceLab", "USB-based Instrument");
adcb9951
JH
252
253 if (has_firmware) {
254 /* Already has the firmware, so fix the new address. */
44b46d70 255 sr_dbg("Found a DSLogic device.");
adcb9951
JH
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);
260 } else {
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();
265 else
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]),
272 0xff, NULL);
273 }
274 }
275 libusb_free_device_list(devlist, 1);
276 g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
277
278 return std_scan_complete(di, devices);
279}
280
adcb9951
JH
281static int dev_open(struct sr_dev_inst *sdi)
282{
283 struct sr_dev_driver *di = sdi->driver;
284 struct sr_usb_dev_inst *usb;
285 struct dev_context *devc;
adcb9951
JH
286 int ret;
287 int64_t timediff_us, timediff_ms;
288
289 devc = sdi->priv;
290 usb = sdi->conn;
291
292 /*
293 * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS
294 * milliseconds for the FX2 to renumerate.
295 */
296 ret = SR_ERR;
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);
301 timediff_ms = 0;
302 while (timediff_ms < MAX_RENUM_DELAY_MS) {
303 if ((ret = dslogic_dev_open(sdi, di)) == SR_OK)
304 break;
305 g_usleep(100 * 1000);
306
307 timediff_us = g_get_monotonic_time() - devc->fw_updated;
308 timediff_ms = timediff_us / 1000;
309 sr_spew("Waited %" PRIi64 "ms.", timediff_ms);
310 }
311 if (ret != SR_OK) {
312 sr_err("Device failed to renumerate.");
313 return SR_ERR;
314 }
315 sr_info("Device came back after %" PRIi64 "ms.", timediff_ms);
316 } else {
317 sr_info("Firmware upload was not needed.");
318 ret = dslogic_dev_open(sdi, di);
319 }
320
321 if (ret != SR_OK) {
322 sr_err("Unable to open device.");
323 return SR_ERR;
324 }
325
326 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
327 if (ret != 0) {
328 switch (ret) {
329 case LIBUSB_ERROR_BUSY:
330 sr_err("Unable to claim USB interface. Another "
331 "program or driver has already claimed it.");
332 break;
333 case LIBUSB_ERROR_NO_DEVICE:
334 sr_err("Device has been disconnected.");
335 break;
336 default:
337 sr_err("Unable to claim interface: %s.",
338 libusb_error_name(ret));
339 break;
340 }
341
342 return SR_ERR;
343 }
344
adcb9951 345
3566348b 346 if ((ret = dslogic_fpga_firmware_upload(sdi)) != SR_OK)
adcb9951
JH
347 return ret;
348
349 if (devc->cur_samplerate == 0) {
350 /* Samplerate hasn't been set; default to the slowest one. */
351 devc->cur_samplerate = devc->samplerates[0];
352 }
353
1ee70746
JH
354 if (devc->cur_threshold == 0.0)
355 devc->cur_threshold = 1.5;
356
adcb9951
JH
357 return SR_OK;
358}
359
360static int dev_close(struct sr_dev_inst *sdi)
361{
362 struct sr_usb_dev_inst *usb;
363
364 usb = sdi->conn;
365
366 if (!usb->devhdl)
f1ba6b4b 367 return SR_ERR_BUG;
adcb9951 368
44b46d70 369 sr_info("Closing device on %d.%d (logical) / %s (physical) interface %d.",
adcb9951
JH
370 usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
371 libusb_release_interface(usb->devhdl, USB_INTERFACE);
372 libusb_close(usb->devhdl);
373 usb->devhdl = NULL;
adcb9951
JH
374
375 return SR_OK;
376}
377
378static int config_get(uint32_t key, GVariant **data,
379 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
380{
381 struct dev_context *devc;
382 struct sr_usb_dev_inst *usb;
1ee70746 383 unsigned int i, voltage_range;
adcb9951
JH
384 char str[128];
385
386 (void)cg;
387
388 if (!sdi)
389 return SR_ERR_ARG;
390
391 devc = sdi->priv;
392
393 switch (key) {
394 case SR_CONF_CONN:
395 if (!sdi->conn)
396 return SR_ERR_ARG;
397 usb = sdi->conn;
398 if (usb->address == 255)
399 /* Device still needs to re-enumerate after firmware
400 * upload, so we don't know its (future) address. */
401 return SR_ERR;
402 snprintf(str, 128, "%d.%d", usb->bus, usb->address);
403 *data = g_variant_new_string(str);
404 break;
405 case SR_CONF_VOLTAGE_THRESHOLD:
1ee70746
JH
406 if (!strcmp(devc->profile->model, "DSLogic")) {
407 voltage_range = 0;
408
7962b129
UH
409 for (i = 0; i < ARRAY_SIZE(voltage_thresholds); i++)
410 if (voltage_thresholds[i].low == devc->cur_threshold) {
1ee70746
JH
411 voltage_range = i;
412 break;
413 }
43995cda
UH
414 *data = std_gvar_tuple_double(voltage_thresholds[voltage_range].low,
415 voltage_thresholds[voltage_range].high);
1ee70746 416 } else {
43995cda 417 *data = std_gvar_tuple_double(devc->cur_threshold, devc->cur_threshold);
adcb9951
JH
418 }
419 break;
420 case SR_CONF_LIMIT_SAMPLES:
421 *data = g_variant_new_uint64(devc->limit_samples);
422 break;
423 case SR_CONF_SAMPLERATE:
424 *data = g_variant_new_uint64(devc->cur_samplerate);
425 break;
426 case SR_CONF_CAPTURE_RATIO:
427 *data = g_variant_new_uint64(devc->capture_ratio);
428 break;
429 case SR_CONF_EXTERNAL_CLOCK:
430 *data = g_variant_new_boolean(devc->external_clock);
431 break;
432 case SR_CONF_CONTINUOUS:
433 *data = g_variant_new_boolean(devc->continuous_mode);
434 break;
435 case SR_CONF_CLOCK_EDGE:
436 i = devc->clock_edge;
437 if (i >= ARRAY_SIZE(signal_edge_names))
438 return SR_ERR_BUG;
439 *data = g_variant_new_string(signal_edge_names[0]);
440 break;
441 default:
442 return SR_ERR_NA;
443 }
444
445 return SR_OK;
446}
447
448/*
449 * Helper for mapping a string-typed configuration value to an index
450 * within a table of possible values.
451 */
452static int lookup_index(GVariant *value, const char *const *table, int len)
453{
454 const char *entry;
455 int i;
456
457 entry = g_variant_get_string(value, NULL);
458 if (!entry)
459 return -1;
460
461 /* Linear search is fine for very small tables. */
462 for (i = 0; i < len; i++) {
463 if (strcmp(entry, table[i]) == 0)
464 return i;
465 }
466
467 return -1;
468}
469
470static int config_set(uint32_t key, GVariant *data,
471 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
472{
473 struct dev_context *devc;
474 uint64_t arg;
475 int i, ret;
476 gdouble low, high;
477
478 (void)cg;
479
480 if (!sdi)
481 return SR_ERR_ARG;
482
adcb9951
JH
483 devc = sdi->priv;
484
485 ret = SR_OK;
486
487 switch (key) {
488 case SR_CONF_SAMPLERATE:
489 arg = g_variant_get_uint64(data);
490 for (i = 0; i < devc->num_samplerates; i++) {
491 if (devc->samplerates[i] == arg) {
492 devc->cur_samplerate = arg;
493 break;
494 }
495 }
496 if (i == devc->num_samplerates)
497 ret = SR_ERR_ARG;
498 break;
499 case SR_CONF_LIMIT_SAMPLES:
500 devc->limit_samples = g_variant_get_uint64(data);
501 break;
502 case SR_CONF_CAPTURE_RATIO:
503 devc->capture_ratio = g_variant_get_uint64(data);
504 ret = (devc->capture_ratio > 100) ? SR_ERR : SR_OK;
505 break;
506 case SR_CONF_VOLTAGE_THRESHOLD:
507 g_variant_get(data, "(dd)", &low, &high);
1ee70746 508 if (!strcmp(devc->profile->model, "DSLogic")) {
7962b129
UH
509 for (i = 0; (unsigned int)i < ARRAY_SIZE(voltage_thresholds); i++) {
510 if (fabs(voltage_thresholds[i].low - low) < 0.1 &&
511 fabs(voltage_thresholds[i].high - high) < 0.1) {
1ee70746 512 devc->cur_threshold =
7962b129 513 voltage_thresholds[i].low;
1ee70746
JH
514 break;
515 }
adcb9951 516 }
1ee70746
JH
517 ret = dslogic_fpga_firmware_upload(sdi);
518 } else {
519 ret = dslogic_set_voltage_threshold(sdi, (low + high) / 2.0);
adcb9951 520 }
adcb9951
JH
521 break;
522 case SR_CONF_EXTERNAL_CLOCK:
523 devc->external_clock = g_variant_get_boolean(data);
524 break;
525 case SR_CONF_CONTINUOUS:
526 devc->continuous_mode = g_variant_get_boolean(data);
527 break;
528 case SR_CONF_CLOCK_EDGE:
53012da6 529 i = lookup_index(data, ARRAY_AND_SIZE(signal_edge_names));
adcb9951
JH
530 if (i < 0)
531 return SR_ERR_ARG;
532 devc->clock_edge = i;
533 break;
534 default:
535 ret = SR_ERR_NA;
536 }
537
538 return ret;
539}
540
541static int config_list(uint32_t key, GVariant **data,
542 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
543{
e66d1892 544 struct dev_context *devc;
adcb9951 545
e66d1892 546 devc = (sdi) ? sdi->priv : NULL;
adcb9951
JH
547
548 switch (key) {
549 case SR_CONF_SCAN_OPTIONS:
adcb9951 550 case SR_CONF_DEVICE_OPTIONS:
e66d1892 551 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
adcb9951 552 case SR_CONF_VOLTAGE_THRESHOLD:
9fb9afb5
UH
553 if (!strcmp(devc->profile->model, "DSLogic"))
554 *data = std_gvar_thresholds(ARRAY_AND_SIZE(voltage_thresholds));
555 else
7bc3cfe6 556 *data = std_gvar_min_max_step_thresholds(0.0, 5.0, 0.1);
adcb9951
JH
557 break;
558 case SR_CONF_SAMPLERATE:
463160cb 559 *data = std_gvar_samplerates(devc->samplerates, devc->num_samplerates);
adcb9951
JH
560 break;
561 case SR_CONF_CLOCK_EDGE:
53012da6 562 *data = g_variant_new_strv(ARRAY_AND_SIZE(signal_edge_names));
adcb9951
JH
563 break;
564 default:
565 return SR_ERR_NA;
566 }
567
568 return SR_OK;
569}
570
44b46d70
UH
571static struct sr_dev_driver dreamsourcelab_dslogic_driver_info = {
572 .name = "dreamsourcelab-dslogic",
573 .longname = "DreamSourceLab DSLogic",
adcb9951
JH
574 .api_version = 1,
575 .init = std_init,
576 .cleanup = std_cleanup,
577 .scan = scan,
578 .dev_list = std_dev_list,
8bf18daa 579 .dev_clear = std_dev_clear,
adcb9951
JH
580 .config_get = config_get,
581 .config_set = config_set,
582 .config_list = config_list,
583 .dev_open = dev_open,
584 .dev_close = dev_close,
4bd770f5
JH
585 .dev_acquisition_start = dslogic_acquisition_start,
586 .dev_acquisition_stop = dslogic_acquisition_stop,
adcb9951
JH
587 .context = NULL,
588};
44b46d70 589SR_REGISTER_DEV_DRIVER(dreamsourcelab_dslogic_driver_info);