]> sigrok.org Git - libsigrok.git/blob - src/hardware/dreamsourcelab-dslogic/api.c
41f8a737965170eff2859be24626851e39ba8007
[libsigrok.git] / src / hardware / dreamsourcelab-dslogic / api.c
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>
22 #include <math.h>
23 #include "protocol.h"
24
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},
46
47         ALL_ZERO
48 };
49
50 static const uint32_t scanopts[] = {
51         SR_CONF_CONN,
52 };
53
54 static const uint32_t drvopts[] = {
55         SR_CONF_LOGIC_ANALYZER,
56 };
57
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,
68 };
69
70 static const char *const signal_edge_names[] = {
71         [DS_EDGE_RISING] = "rising",
72         [DS_EDGE_FALLING] = "falling",
73 };
74
75 static const struct {
76         gdouble low;
77         gdouble high;
78 } voltage_thresholds[] = {
79         { 0.7, 1.4 },
80         { 1.4, 3.6 },
81 };
82
83 static const uint64_t samplerates[] = {
84         SR_KHZ(10),
85         SR_KHZ(20),
86         SR_KHZ(50),
87         SR_KHZ(100),
88         SR_KHZ(200),
89         SR_KHZ(500),
90         SR_MHZ(1),
91         SR_MHZ(2),
92         SR_MHZ(5),
93         SR_MHZ(10),
94         SR_MHZ(20),
95         SR_MHZ(25),
96         SR_MHZ(50),
97         SR_MHZ(100),
98         SR_MHZ(200),
99         SR_MHZ(400),
100 };
101
102 static gboolean is_plausible(const struct libusb_device_descriptor *des)
103 {
104         int i;
105
106         for (i = 0; supported_device[i].vid; i++) {
107                 if (des->idVendor != supported_device[i].vid)
108                         continue;
109                 if (des->idProduct == supported_device[i].pid)
110                         return TRUE;
111         }
112
113         return FALSE;
114 }
115
116 static GSList *scan(struct sr_dev_driver *di, GSList *options)
117 {
118         struct drv_context *drvc;
119         struct dev_context *devc;
120         struct sr_dev_inst *sdi;
121         struct sr_usb_dev_inst *usb;
122         struct sr_channel *ch;
123         struct sr_channel_group *cg;
124         struct sr_config *src;
125         const struct dslogic_profile *prof;
126         GSList *l, *devices, *conn_devices;
127         gboolean has_firmware;
128         struct libusb_device_descriptor des;
129         libusb_device **devlist;
130         struct libusb_device_handle *hdl;
131         int ret, i, j;
132         const char *conn;
133         char manufacturer[64], product[64], serial_num[64], connection_id[64];
134         char channel_name[16];
135
136         drvc = di->context;
137
138         conn = NULL;
139         for (l = options; l; l = l->next) {
140                 src = l->data;
141                 switch (src->key) {
142                 case SR_CONF_CONN:
143                         conn = g_variant_get_string(src->data, NULL);
144                         break;
145                 }
146         }
147         if (conn)
148                 conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
149         else
150                 conn_devices = NULL;
151
152         /* Find all DSLogic compatible devices and upload firmware to them. */
153         devices = NULL;
154         libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
155         for (i = 0; devlist[i]; i++) {
156                 if (conn) {
157                         usb = NULL;
158                         for (l = conn_devices; l; l = l->next) {
159                                 usb = l->data;
160                                 if (usb->bus == libusb_get_bus_number(devlist[i])
161                                         && usb->address == libusb_get_device_address(devlist[i]))
162                                         break;
163                         }
164                         if (!l)
165                                 /* This device matched none of the ones that
166                                  * matched the conn specification. */
167                                 continue;
168                 }
169
170                 libusb_get_device_descriptor(devlist[i], &des);
171
172                 if (!is_plausible(&des))
173                         continue;
174
175                 if ((ret = libusb_open(devlist[i], &hdl)) < 0) {
176                         sr_warn("Failed to open potential device with "
177                                 "VID:PID %04x:%04x: %s.", des.idVendor,
178                                 des.idProduct, libusb_error_name(ret));
179                         continue;
180                 }
181
182                 if (des.iManufacturer == 0) {
183                         manufacturer[0] = '\0';
184                 } else if ((ret = libusb_get_string_descriptor_ascii(hdl,
185                                 des.iManufacturer, (unsigned char *) manufacturer,
186                                 sizeof(manufacturer))) < 0) {
187                         sr_warn("Failed to get manufacturer string descriptor: %s.",
188                                 libusb_error_name(ret));
189                         continue;
190                 }
191
192                 if (des.iProduct == 0) {
193                         product[0] = '\0';
194                 } else if ((ret = libusb_get_string_descriptor_ascii(hdl,
195                                 des.iProduct, (unsigned char *) product,
196                                 sizeof(product))) < 0) {
197                         sr_warn("Failed to get product string descriptor: %s.",
198                                 libusb_error_name(ret));
199                         continue;
200                 }
201
202                 if (des.iSerialNumber == 0) {
203                         serial_num[0] = '\0';
204                 } else if ((ret = libusb_get_string_descriptor_ascii(hdl,
205                                 des.iSerialNumber, (unsigned char *) serial_num,
206                                 sizeof(serial_num))) < 0) {
207                         sr_warn("Failed to get serial number string descriptor: %s.",
208                                 libusb_error_name(ret));
209                         continue;
210                 }
211
212                 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
213
214                 libusb_close(hdl);
215
216                 prof = NULL;
217                 for (j = 0; supported_device[j].vid; j++) {
218                         if (des.idVendor == supported_device[j].vid &&
219                                         des.idProduct == supported_device[j].pid) {
220                                 prof = &supported_device[j];
221                                 break;
222                         }
223                 }
224
225                 if (!prof)
226                         continue;
227
228                 sdi = g_malloc0(sizeof(struct sr_dev_inst));
229                 sdi->status = SR_ST_INITIALIZING;
230                 sdi->vendor = g_strdup(prof->vendor);
231                 sdi->model = g_strdup(prof->model);
232                 sdi->version = g_strdup(prof->model_version);
233                 sdi->serial_num = g_strdup(serial_num);
234                 sdi->connection_id = g_strdup(connection_id);
235
236                 /* Logic channels, all in one channel group. */
237                 cg = g_malloc0(sizeof(struct sr_channel_group));
238                 cg->name = g_strdup("Logic");
239                 for (j = 0; j < 16; j++) {
240                         sprintf(channel_name, "%d", j);
241                         ch = sr_channel_new(sdi, j, SR_CHANNEL_LOGIC,
242                                                 TRUE, channel_name);
243                         cg->channels = g_slist_append(cg->channels, ch);
244                 }
245                 sdi->channel_groups = g_slist_append(NULL, cg);
246
247                 devc = dslogic_dev_new();
248                 devc->profile = prof;
249                 sdi->priv = devc;
250                 devices = g_slist_append(devices, sdi);
251
252                 devc->samplerates = samplerates;
253                 devc->num_samplerates = ARRAY_SIZE(samplerates);
254                 has_firmware = usb_match_manuf_prod(devlist[i], "DreamSourceLab", "USB-based Instrument");
255
256                 if (has_firmware) {
257                         /* Already has the firmware, so fix the new address. */
258                         sr_dbg("Found a DSLogic device.");
259                         sdi->status = SR_ST_INACTIVE;
260                         sdi->inst_type = SR_INST_USB;
261                         sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
262                                         libusb_get_device_address(devlist[i]), NULL);
263                 } else {
264                         if (ezusb_upload_firmware(drvc->sr_ctx, devlist[i],
265                                         USB_CONFIGURATION, prof->firmware) == SR_OK)
266                                 /* Store when this device's FW was updated. */
267                                 devc->fw_updated = g_get_monotonic_time();
268                         else
269                                 sr_err("Firmware upload failed for "
270                                        "device %d.%d (logical).",
271                                        libusb_get_bus_number(devlist[i]),
272                                        libusb_get_device_address(devlist[i]));
273                         sdi->inst_type = SR_INST_USB;
274                         sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
275                                         0xff, NULL);
276                 }
277         }
278         libusb_free_device_list(devlist, 1);
279         g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free);
280
281         return std_scan_complete(di, devices);
282 }
283
284 static int dev_open(struct sr_dev_inst *sdi)
285 {
286         struct sr_dev_driver *di = sdi->driver;
287         struct sr_usb_dev_inst *usb;
288         struct dev_context *devc;
289         int ret;
290         int64_t timediff_us, timediff_ms;
291
292         devc = sdi->priv;
293         usb = sdi->conn;
294
295         /*
296          * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS
297          * milliseconds for the FX2 to renumerate.
298          */
299         ret = SR_ERR;
300         if (devc->fw_updated > 0) {
301                 sr_info("Waiting for device to reset.");
302                 /* Takes >= 300ms for the FX2 to be gone from the USB bus. */
303                 g_usleep(300 * 1000);
304                 timediff_ms = 0;
305                 while (timediff_ms < MAX_RENUM_DELAY_MS) {
306                         if ((ret = dslogic_dev_open(sdi, di)) == SR_OK)
307                                 break;
308                         g_usleep(100 * 1000);
309
310                         timediff_us = g_get_monotonic_time() - devc->fw_updated;
311                         timediff_ms = timediff_us / 1000;
312                         sr_spew("Waited %" PRIi64 "ms.", timediff_ms);
313                 }
314                 if (ret != SR_OK) {
315                         sr_err("Device failed to renumerate.");
316                         return SR_ERR;
317                 }
318                 sr_info("Device came back after %" PRIi64 "ms.", timediff_ms);
319         } else {
320                 sr_info("Firmware upload was not needed.");
321                 ret = dslogic_dev_open(sdi, di);
322         }
323
324         if (ret != SR_OK) {
325                 sr_err("Unable to open device.");
326                 return SR_ERR;
327         }
328
329         ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
330         if (ret != 0) {
331                 switch (ret) {
332                 case LIBUSB_ERROR_BUSY:
333                         sr_err("Unable to claim USB interface. Another "
334                                "program or driver has already claimed it.");
335                         break;
336                 case LIBUSB_ERROR_NO_DEVICE:
337                         sr_err("Device has been disconnected.");
338                         break;
339                 default:
340                         sr_err("Unable to claim interface: %s.",
341                                libusb_error_name(ret));
342                         break;
343                 }
344
345                 return SR_ERR;
346         }
347
348
349         if ((ret = dslogic_fpga_firmware_upload(sdi)) != SR_OK)
350                 return ret;
351
352         if (devc->cur_samplerate == 0) {
353                 /* Samplerate hasn't been set; default to the slowest one. */
354                 devc->cur_samplerate = devc->samplerates[0];
355         }
356
357         if (devc->cur_threshold == 0.0)
358                 devc->cur_threshold = 1.5;
359
360         return SR_OK;
361 }
362
363 static int dev_close(struct sr_dev_inst *sdi)
364 {
365         struct sr_usb_dev_inst *usb;
366
367         usb = sdi->conn;
368
369         if (!usb->devhdl)
370                 return SR_ERR_BUG;
371
372         sr_info("Closing device on %d.%d (logical) / %s (physical) interface %d.",
373                 usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
374         libusb_release_interface(usb->devhdl, USB_INTERFACE);
375         libusb_close(usb->devhdl);
376         usb->devhdl = NULL;
377
378         return SR_OK;
379 }
380
381 static int config_get(uint32_t key, GVariant **data,
382         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
383 {
384         struct dev_context *devc;
385         struct sr_usb_dev_inst *usb;
386         GVariant *range[2];
387         unsigned int i, voltage_range;
388         char str[128];
389
390         (void)cg;
391
392         if (!sdi)
393                 return SR_ERR_ARG;
394
395         devc = sdi->priv;
396
397         switch (key) {
398         case SR_CONF_CONN:
399                 if (!sdi->conn)
400                         return SR_ERR_ARG;
401                 usb = sdi->conn;
402                 if (usb->address == 255)
403                         /* Device still needs to re-enumerate after firmware
404                          * upload, so we don't know its (future) address. */
405                         return SR_ERR;
406                 snprintf(str, 128, "%d.%d", usb->bus, usb->address);
407                 *data = g_variant_new_string(str);
408                 break;
409         case SR_CONF_VOLTAGE_THRESHOLD:
410                 if (!strcmp(devc->profile->model, "DSLogic")) {
411                         voltage_range = 0;
412
413                         for (i = 0; i < ARRAY_SIZE(voltage_thresholds); i++)
414                                 if (voltage_thresholds[i].low == devc->cur_threshold) {
415                                         voltage_range = i;
416                                         break;
417                                 }
418
419                         range[0] = g_variant_new_double(
420                                 voltage_thresholds[voltage_range].low);
421                         range[1] = g_variant_new_double(
422                                 voltage_thresholds[voltage_range].high);
423                 } else {
424                         range[0] = g_variant_new_double(devc->cur_threshold);
425                         range[1] = g_variant_new_double(devc->cur_threshold);
426                 }
427                 *data = g_variant_new_tuple(range, 2);
428                 break;
429         case SR_CONF_LIMIT_SAMPLES:
430                 *data = g_variant_new_uint64(devc->limit_samples);
431                 break;
432         case SR_CONF_SAMPLERATE:
433                 *data = g_variant_new_uint64(devc->cur_samplerate);
434                 break;
435         case SR_CONF_CAPTURE_RATIO:
436                 *data = g_variant_new_uint64(devc->capture_ratio);
437                 break;
438         case SR_CONF_EXTERNAL_CLOCK:
439                 *data = g_variant_new_boolean(devc->external_clock);
440                 break;
441         case SR_CONF_CONTINUOUS:
442                 *data = g_variant_new_boolean(devc->continuous_mode);
443                 break;
444         case SR_CONF_CLOCK_EDGE:
445                 i = devc->clock_edge;
446                 if (i >= ARRAY_SIZE(signal_edge_names))
447                         return SR_ERR_BUG;
448                 *data = g_variant_new_string(signal_edge_names[0]);
449                 break;
450         default:
451                 return SR_ERR_NA;
452         }
453
454         return SR_OK;
455 }
456
457 /*
458  * Helper for mapping a string-typed configuration value to an index
459  * within a table of possible values.
460  */
461 static int lookup_index(GVariant *value, const char *const *table, int len)
462 {
463         const char *entry;
464         int i;
465
466         entry = g_variant_get_string(value, NULL);
467         if (!entry)
468                 return -1;
469
470         /* Linear search is fine for very small tables. */
471         for (i = 0; i < len; i++) {
472                 if (strcmp(entry, table[i]) == 0)
473                         return i;
474         }
475
476         return -1;
477 }
478
479 static int config_set(uint32_t key, GVariant *data,
480         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
481 {
482         struct dev_context *devc;
483         uint64_t arg;
484         int i, ret;
485         gdouble low, high;
486
487         (void)cg;
488
489         if (!sdi)
490                 return SR_ERR_ARG;
491
492         devc = sdi->priv;
493
494         ret = SR_OK;
495
496         switch (key) {
497         case SR_CONF_SAMPLERATE:
498                 arg = g_variant_get_uint64(data);
499                 for (i = 0; i < devc->num_samplerates; i++) {
500                         if (devc->samplerates[i] == arg) {
501                                 devc->cur_samplerate = arg;
502                                 break;
503                         }
504                 }
505                 if (i == devc->num_samplerates)
506                         ret = SR_ERR_ARG;
507                 break;
508         case SR_CONF_LIMIT_SAMPLES:
509                 devc->limit_samples = g_variant_get_uint64(data);
510                 break;
511         case SR_CONF_CAPTURE_RATIO:
512                 devc->capture_ratio = g_variant_get_uint64(data);
513                 ret = (devc->capture_ratio > 100) ? SR_ERR : SR_OK;
514                 break;
515         case SR_CONF_VOLTAGE_THRESHOLD:
516                 g_variant_get(data, "(dd)", &low, &high);
517                 if (!strcmp(devc->profile->model, "DSLogic")) {
518                         for (i = 0; (unsigned int)i < ARRAY_SIZE(voltage_thresholds); i++) {
519                                 if (fabs(voltage_thresholds[i].low - low) < 0.1 &&
520                                     fabs(voltage_thresholds[i].high - high) < 0.1) {
521                                         devc->cur_threshold =
522                                                 voltage_thresholds[i].low;
523                                         break;
524                                 }
525                         }
526                         ret = dslogic_fpga_firmware_upload(sdi);
527                 } else {
528                         ret = dslogic_set_voltage_threshold(sdi, (low + high) / 2.0);
529                 }
530                 break;
531         case SR_CONF_EXTERNAL_CLOCK:
532                 devc->external_clock = g_variant_get_boolean(data);
533                 break;
534         case SR_CONF_CONTINUOUS:
535                 devc->continuous_mode = g_variant_get_boolean(data);
536                 break;
537         case SR_CONF_CLOCK_EDGE:
538                 i = lookup_index(data, signal_edge_names,
539                                    ARRAY_SIZE(signal_edge_names));
540                 if (i < 0)
541                         return SR_ERR_ARG;
542                 devc->clock_edge = i;
543                 break;
544         default:
545                 ret = SR_ERR_NA;
546         }
547
548         return ret;
549 }
550
551 static int config_list(uint32_t key, GVariant **data,
552         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
553 {
554         struct dev_context *devc;
555         GVariant *gvar, *range[2];
556         GVariantBuilder gvb;
557         unsigned int i;
558
559         devc = (sdi) ? sdi->priv : NULL;
560
561         switch (key) {
562         case SR_CONF_SCAN_OPTIONS:
563         case SR_CONF_DEVICE_OPTIONS:
564                 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
565         case SR_CONF_VOLTAGE_THRESHOLD:
566                 if (!strcmp(devc->profile->model, "DSLogic")) {
567                         g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
568                         for (i = 0; i < ARRAY_SIZE(voltage_thresholds); i++) {
569                                 range[0] = g_variant_new_double(voltage_thresholds[i].low);
570                                 range[1] = g_variant_new_double(voltage_thresholds[i].high);
571                                 gvar = g_variant_new_tuple(range, 2);
572                                 g_variant_builder_add_value(&gvb, gvar);
573                         }
574                         *data = g_variant_builder_end(&gvb);
575                 } else {
576                         *data = std_gvar_min_max_step_thresholds(0.0, 5.0, 0.1);
577                 }
578                 break;
579         case SR_CONF_SAMPLERATE:
580                 *data = std_gvar_samplerates(devc->samplerates, devc->num_samplerates);
581                 break;
582         case SR_CONF_CLOCK_EDGE:
583                 *data = g_variant_new_strv(signal_edge_names,
584                         ARRAY_SIZE(signal_edge_names));
585                 break;
586         default:
587                 return SR_ERR_NA;
588         }
589
590         return SR_OK;
591 }
592
593 static struct sr_dev_driver dreamsourcelab_dslogic_driver_info = {
594         .name = "dreamsourcelab-dslogic",
595         .longname = "DreamSourceLab DSLogic",
596         .api_version = 1,
597         .init = std_init,
598         .cleanup = std_cleanup,
599         .scan = scan,
600         .dev_list = std_dev_list,
601         .dev_clear = std_dev_clear,
602         .config_get = config_get,
603         .config_set = config_set,
604         .config_list = config_list,
605         .dev_open = dev_open,
606         .dev_close = dev_close,
607         .dev_acquisition_start = dslogic_acquisition_start,
608         .dev_acquisition_stop = dslogic_acquisition_stop,
609         .context = NULL,
610 };
611 SR_REGISTER_DEV_DRIVER(dreamsourcelab_dslogic_driver_info);