]> sigrok.org Git - libsigrok.git/blame - src/hardware/dreamsourcelab-dslogic/api.c
dmm/ms8250d: Fix two log messages.
[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
bc98407b
UH
70static const int32_t trigger_matches[] = {
71 SR_TRIGGER_ZERO,
72 SR_TRIGGER_ONE,
73 SR_TRIGGER_RISING,
74 SR_TRIGGER_FALLING,
75 SR_TRIGGER_EDGE,
76};
77
e124cf9b 78static const char *signal_edges[] = {
adcb9951
JH
79 [DS_EDGE_RISING] = "rising",
80 [DS_EDGE_FALLING] = "falling",
81};
82
21fe5dba 83static const double thresholds[][2] = {
1ee70746
JH
84 { 0.7, 1.4 },
85 { 1.4, 3.6 },
adcb9951
JH
86};
87
88static const uint64_t samplerates[] = {
89 SR_KHZ(10),
90 SR_KHZ(20),
91 SR_KHZ(50),
92 SR_KHZ(100),
93 SR_KHZ(200),
94 SR_KHZ(500),
95 SR_MHZ(1),
96 SR_MHZ(2),
97 SR_MHZ(5),
98 SR_MHZ(10),
99 SR_MHZ(20),
100 SR_MHZ(25),
101 SR_MHZ(50),
102 SR_MHZ(100),
103 SR_MHZ(200),
104 SR_MHZ(400),
105};
106
107static gboolean is_plausible(const struct libusb_device_descriptor *des)
108{
109 int i;
110
111 for (i = 0; supported_device[i].vid; i++) {
112 if (des->idVendor != supported_device[i].vid)
113 continue;
114 if (des->idProduct == supported_device[i].pid)
115 return TRUE;
116 }
117
118 return FALSE;
119}
120
121static GSList *scan(struct sr_dev_driver *di, GSList *options)
122{
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;
136 int ret, i, j;
137 const char *conn;
138 char manufacturer[64], product[64], serial_num[64], connection_id[64];
139 char channel_name[16];
140
141 drvc = di->context;
142
143 conn = NULL;
144 for (l = options; l; l = l->next) {
145 src = l->data;
146 switch (src->key) {
147 case SR_CONF_CONN:
148 conn = g_variant_get_string(src->data, NULL);
149 break;
150 }
151 }
152 if (conn)
153 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
154 else
155 conn_devices = NULL;
156
44b46d70 157 /* Find all DSLogic compatible devices and upload firmware to them. */
adcb9951
JH
158 devices = NULL;
159 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
160 for (i = 0; devlist[i]; i++) {
161 if (conn) {
162 usb = NULL;
163 for (l = conn_devices; l; l = l->next) {
164 usb = l->data;
165 if (usb->bus == libusb_get_bus_number(devlist[i])
166 && usb->address == libusb_get_device_address(devlist[i]))
167 break;
168 }
169 if (!l)
170 /* This device matched none of the ones that
171 * matched the conn specification. */
172 continue;
173 }
174
44b46d70 175 libusb_get_device_descriptor(devlist[i], &des);
adcb9951
JH
176
177 if (!is_plausible(&des))
178 continue;
179
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));
184 continue;
185 }
186
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));
194 continue;
195 }
196
197 if (des.iProduct == 0) {
198 product[0] = '\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));
204 continue;
205 }
206
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));
214 continue;
215 }
216
adcb9951
JH
217 libusb_close(hdl);
218
6c1a76d1
RT
219 if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0)
220 continue;
7bbe5a2b 221
adcb9951
JH
222 prof = NULL;
223 for (j = 0; supported_device[j].vid; j++) {
224 if (des.idVendor == supported_device[j].vid &&
4984ca28 225 des.idProduct == supported_device[j].pid) {
adcb9951
JH
226 prof = &supported_device[j];
227 break;
228 }
229 }
230
adcb9951
JH
231 if (!prof)
232 continue;
233
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);
241
242 /* Logic channels, all in one channel group. */
243 cg = g_malloc0(sizeof(struct sr_channel_group));
244 cg->name = g_strdup("Logic");
b3fd0993 245 for (j = 0; j < NUM_CHANNELS; j++) {
84f6d40a 246 sprintf(channel_name, "%d", j);
adcb9951
JH
247 ch = sr_channel_new(sdi, j, SR_CHANNEL_LOGIC,
248 TRUE, channel_name);
249 cg->channels = g_slist_append(cg->channels, ch);
250 }
251 sdi->channel_groups = g_slist_append(NULL, cg);
252
253 devc = dslogic_dev_new();
254 devc->profile = prof;
255 sdi->priv = devc;
256 devices = g_slist_append(devices, sdi);
257
258 devc->samplerates = samplerates;
259 devc->num_samplerates = ARRAY_SIZE(samplerates);
3c749da1 260 has_firmware = usb_match_manuf_prod(devlist[i], "DreamSourceLab", "USB-based Instrument");
adcb9951
JH
261
262 if (has_firmware) {
263 /* Already has the firmware, so fix the new address. */
44b46d70 264 sr_dbg("Found a DSLogic device.");
adcb9951
JH
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);
269 } else {
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();
274 else
275 sr_err("Firmware upload failed for "
276 "device %d.%d (logical).",
277 libusb_get_bus_number(devlist[i]),
278 libusb_get_device_address(devlist[i]));
279 sdi->inst_type = SR_INST_USB;
280 sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
281 0xff, NULL);
282 }
283 }
284 libusb_free_device_list(devlist, 1);
285 g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
286
287 return std_scan_complete(di, devices);
288}
289
adcb9951
JH
290static int dev_open(struct sr_dev_inst *sdi)
291{
292 struct sr_dev_driver *di = sdi->driver;
293 struct sr_usb_dev_inst *usb;
294 struct dev_context *devc;
adcb9951
JH
295 int ret;
296 int64_t timediff_us, timediff_ms;
297
298 devc = sdi->priv;
299 usb = sdi->conn;
300
301 /*
302 * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS
303 * milliseconds for the FX2 to renumerate.
304 */
305 ret = SR_ERR;
306 if (devc->fw_updated > 0) {
307 sr_info("Waiting for device to reset.");
308 /* Takes >= 300ms for the FX2 to be gone from the USB bus. */
309 g_usleep(300 * 1000);
310 timediff_ms = 0;
311 while (timediff_ms < MAX_RENUM_DELAY_MS) {
312 if ((ret = dslogic_dev_open(sdi, di)) == SR_OK)
313 break;
314 g_usleep(100 * 1000);
315
316 timediff_us = g_get_monotonic_time() - devc->fw_updated;
317 timediff_ms = timediff_us / 1000;
318 sr_spew("Waited %" PRIi64 "ms.", timediff_ms);
319 }
320 if (ret != SR_OK) {
321 sr_err("Device failed to renumerate.");
322 return SR_ERR;
323 }
324 sr_info("Device came back after %" PRIi64 "ms.", timediff_ms);
325 } else {
326 sr_info("Firmware upload was not needed.");
327 ret = dslogic_dev_open(sdi, di);
328 }
329
330 if (ret != SR_OK) {
331 sr_err("Unable to open device.");
332 return SR_ERR;
333 }
334
335 ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
336 if (ret != 0) {
337 switch (ret) {
338 case LIBUSB_ERROR_BUSY:
339 sr_err("Unable to claim USB interface. Another "
340 "program or driver has already claimed it.");
341 break;
342 case LIBUSB_ERROR_NO_DEVICE:
343 sr_err("Device has been disconnected.");
344 break;
345 default:
346 sr_err("Unable to claim interface: %s.",
347 libusb_error_name(ret));
348 break;
349 }
350
351 return SR_ERR;
352 }
353
adcb9951 354
3566348b 355 if ((ret = dslogic_fpga_firmware_upload(sdi)) != SR_OK)
adcb9951
JH
356 return ret;
357
358 if (devc->cur_samplerate == 0) {
359 /* Samplerate hasn't been set; default to the slowest one. */
360 devc->cur_samplerate = devc->samplerates[0];
361 }
362
1ee70746 363 if (devc->cur_threshold == 0.0)
86a15711 364 devc->cur_threshold = thresholds[1][0];
1ee70746 365
adcb9951
JH
366 return SR_OK;
367}
368
369static int dev_close(struct sr_dev_inst *sdi)
370{
371 struct sr_usb_dev_inst *usb;
372
373 usb = sdi->conn;
374
375 if (!usb->devhdl)
f1ba6b4b 376 return SR_ERR_BUG;
adcb9951 377
44b46d70 378 sr_info("Closing device on %d.%d (logical) / %s (physical) interface %d.",
adcb9951
JH
379 usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
380 libusb_release_interface(usb->devhdl, USB_INTERFACE);
381 libusb_close(usb->devhdl);
382 usb->devhdl = NULL;
adcb9951
JH
383
384 return SR_OK;
385}
386
387static int config_get(uint32_t key, GVariant **data,
388 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
389{
390 struct dev_context *devc;
391 struct sr_usb_dev_inst *usb;
697fb6dd 392 int idx;
adcb9951
JH
393
394 (void)cg;
395
396 if (!sdi)
397 return SR_ERR_ARG;
398
399 devc = sdi->priv;
400
401 switch (key) {
402 case SR_CONF_CONN:
403 if (!sdi->conn)
404 return SR_ERR_ARG;
405 usb = sdi->conn;
406 if (usb->address == 255)
407 /* Device still needs to re-enumerate after firmware
408 * upload, so we don't know its (future) address. */
409 return SR_ERR;
95c1fe62 410 *data = g_variant_new_printf("%d.%d", usb->bus, usb->address);
adcb9951
JH
411 break;
412 case SR_CONF_VOLTAGE_THRESHOLD:
1ee70746 413 if (!strcmp(devc->profile->model, "DSLogic")) {
697fb6dd 414 if ((idx = std_double_tuple_idx_d0(devc->cur_threshold,
21fe5dba 415 ARRAY_AND_SIZE(thresholds))) < 0)
697fb6dd 416 return SR_ERR_BUG;
21fe5dba
UH
417 *data = std_gvar_tuple_double(thresholds[idx][0],
418 thresholds[idx][1]);
1ee70746 419 } else {
43995cda 420 *data = std_gvar_tuple_double(devc->cur_threshold, devc->cur_threshold);
adcb9951
JH
421 }
422 break;
423 case SR_CONF_LIMIT_SAMPLES:
424 *data = g_variant_new_uint64(devc->limit_samples);
425 break;
426 case SR_CONF_SAMPLERATE:
427 *data = g_variant_new_uint64(devc->cur_samplerate);
428 break;
429 case SR_CONF_CAPTURE_RATIO:
430 *data = g_variant_new_uint64(devc->capture_ratio);
431 break;
432 case SR_CONF_EXTERNAL_CLOCK:
433 *data = g_variant_new_boolean(devc->external_clock);
434 break;
435 case SR_CONF_CONTINUOUS:
436 *data = g_variant_new_boolean(devc->continuous_mode);
437 break;
438 case SR_CONF_CLOCK_EDGE:
697fb6dd 439 idx = devc->clock_edge;
e124cf9b 440 if (idx >= (int)ARRAY_SIZE(signal_edges))
adcb9951 441 return SR_ERR_BUG;
e124cf9b 442 *data = g_variant_new_string(signal_edges[0]);
adcb9951
JH
443 break;
444 default:
445 return SR_ERR_NA;
446 }
447
448 return SR_OK;
449}
450
adcb9951
JH
451static int config_set(uint32_t key, GVariant *data,
452 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
453{
454 struct dev_context *devc;
758906aa 455 int idx;
adcb9951
JH
456 gdouble low, high;
457
458 (void)cg;
459
460 if (!sdi)
461 return SR_ERR_ARG;
462
adcb9951
JH
463 devc = sdi->priv;
464
adcb9951
JH
465 switch (key) {
466 case SR_CONF_SAMPLERATE:
697fb6dd
UH
467 if ((idx = std_u64_idx(data, devc->samplerates, devc->num_samplerates)) < 0)
468 return SR_ERR_ARG;
469 devc->cur_samplerate = devc->samplerates[idx];
adcb9951
JH
470 break;
471 case SR_CONF_LIMIT_SAMPLES:
472 devc->limit_samples = g_variant_get_uint64(data);
473 break;
474 case SR_CONF_CAPTURE_RATIO:
475 devc->capture_ratio = g_variant_get_uint64(data);
efad7ccc 476 break;
adcb9951 477 case SR_CONF_VOLTAGE_THRESHOLD:
1ee70746 478 if (!strcmp(devc->profile->model, "DSLogic")) {
21fe5dba 479 if ((idx = std_double_tuple_idx(data, ARRAY_AND_SIZE(thresholds))) < 0)
697fb6dd 480 return SR_ERR_ARG;
21fe5dba 481 devc->cur_threshold = thresholds[idx][0];
758906aa 482 return dslogic_fpga_firmware_upload(sdi);
1ee70746 483 } else {
697fb6dd 484 g_variant_get(data, "(dd)", &low, &high);
758906aa 485 return dslogic_set_voltage_threshold(sdi, (low + high) / 2.0);
adcb9951 486 }
adcb9951
JH
487 break;
488 case SR_CONF_EXTERNAL_CLOCK:
489 devc->external_clock = g_variant_get_boolean(data);
490 break;
491 case SR_CONF_CONTINUOUS:
492 devc->continuous_mode = g_variant_get_boolean(data);
493 break;
494 case SR_CONF_CLOCK_EDGE:
e124cf9b 495 if ((idx = std_str_idx(data, ARRAY_AND_SIZE(signal_edges))) < 0)
adcb9951 496 return SR_ERR_ARG;
697fb6dd 497 devc->clock_edge = idx;
adcb9951
JH
498 break;
499 default:
758906aa 500 return SR_ERR_NA;
adcb9951
JH
501 }
502
758906aa 503 return SR_OK;
adcb9951
JH
504}
505
506static int config_list(uint32_t key, GVariant **data,
507 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
508{
e66d1892 509 struct dev_context *devc;
adcb9951 510
e66d1892 511 devc = (sdi) ? sdi->priv : NULL;
adcb9951
JH
512
513 switch (key) {
514 case SR_CONF_SCAN_OPTIONS:
adcb9951 515 case SR_CONF_DEVICE_OPTIONS:
e66d1892 516 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
adcb9951 517 case SR_CONF_VOLTAGE_THRESHOLD:
8e2fd23a
GS
518 if (!devc || !devc->profile)
519 return SR_ERR_ARG;
9fb9afb5 520 if (!strcmp(devc->profile->model, "DSLogic"))
21fe5dba 521 *data = std_gvar_thresholds(ARRAY_AND_SIZE(thresholds));
9fb9afb5 522 else
7bc3cfe6 523 *data = std_gvar_min_max_step_thresholds(0.0, 5.0, 0.1);
adcb9951
JH
524 break;
525 case SR_CONF_SAMPLERATE:
8e2fd23a
GS
526 if (!devc)
527 return SR_ERR_ARG;
463160cb 528 *data = std_gvar_samplerates(devc->samplerates, devc->num_samplerates);
adcb9951 529 break;
bc98407b
UH
530 case SR_CONF_TRIGGER_MATCH:
531 *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
532 break;
adcb9951 533 case SR_CONF_CLOCK_EDGE:
e124cf9b 534 *data = g_variant_new_strv(ARRAY_AND_SIZE(signal_edges));
adcb9951
JH
535 break;
536 default:
537 return SR_ERR_NA;
538 }
539
540 return SR_OK;
541}
542
44b46d70
UH
543static struct sr_dev_driver dreamsourcelab_dslogic_driver_info = {
544 .name = "dreamsourcelab-dslogic",
545 .longname = "DreamSourceLab DSLogic",
adcb9951
JH
546 .api_version = 1,
547 .init = std_init,
548 .cleanup = std_cleanup,
549 .scan = scan,
550 .dev_list = std_dev_list,
8bf18daa 551 .dev_clear = std_dev_clear,
adcb9951
JH
552 .config_get = config_get,
553 .config_set = config_set,
554 .config_list = config_list,
555 .dev_open = dev_open,
556 .dev_close = dev_close,
4bd770f5
JH
557 .dev_acquisition_start = dslogic_acquisition_start,
558 .dev_acquisition_stop = dslogic_acquisition_stop,
adcb9951
JH
559 .context = NULL,
560};
44b46d70 561SR_REGISTER_DEV_DRIVER(dreamsourcelab_dslogic_driver_info);