]> sigrok.org Git - libsigrok.git/blob - hardware/rigol-ds/api.c
hwdriver: Change TRIGGER_SLOPE setting to string type.
[libsigrok.git] / hardware / rigol-ds / api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2012 Martin Ling <martin-git@earth.li>
5  * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
6  * Copyright (C) 2013 Mathias Grimmberger <mgri@zaphod.sax.de>
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <math.h>
27 #include <glib.h>
28 #include "libsigrok.h"
29 #include "libsigrok-internal.h"
30 #include "protocol.h"
31
32 static const int32_t hwopts[] = {
33         SR_CONF_CONN,
34         SR_CONF_SERIALCOMM
35 };
36
37 static const int32_t hwcaps[] = {
38         SR_CONF_OSCILLOSCOPE,
39         SR_CONF_TIMEBASE,
40         SR_CONF_TRIGGER_SOURCE,
41         SR_CONF_TRIGGER_SLOPE,
42         SR_CONF_HORIZ_TRIGGERPOS,
43         SR_CONF_NUM_TIMEBASE,
44         SR_CONF_LIMIT_FRAMES,
45         SR_CONF_SAMPLERATE,
46 };
47
48 static const int32_t analog_hwcaps[] = {
49         SR_CONF_NUM_VDIV,
50         SR_CONF_VDIV,
51         SR_CONF_COUPLING,
52         SR_CONF_DATA_SOURCE,
53 };
54
55 static const uint64_t timebases[][2] = {
56         /* nanoseconds */
57         { 1, 1000000000 },
58         { 2, 1000000000 },
59         { 5, 1000000000 },
60         { 10, 1000000000 },
61         { 20, 1000000000 },
62         { 50, 1000000000 },
63         { 100, 1000000000 },
64         { 500, 1000000000 },
65         /* microseconds */
66         { 1, 1000000 },
67         { 2, 1000000 },
68         { 5, 1000000 },
69         { 10, 1000000 },
70         { 20, 1000000 },
71         { 50, 1000000 },
72         { 100, 1000000 },
73         { 200, 1000000 },
74         { 500, 1000000 },
75         /* milliseconds */
76         { 1, 1000 },
77         { 2, 1000 },
78         { 5, 1000 },
79         { 10, 1000 },
80         { 20, 1000 },
81         { 50, 1000 },
82         { 100, 1000 },
83         { 200, 1000 },
84         { 500, 1000 },
85         /* seconds */
86         { 1, 1 },
87         { 2, 1 },
88         { 5, 1 },
89         { 10, 1 },
90         { 20, 1 },
91         { 50, 1 },
92         { 100, 1 },
93         { 200, 1 },
94         { 500, 1 },
95         { 1000, 1 },
96 };
97
98 static const uint64_t vdivs[][2] = {
99         /* microvolts */
100         { 500, 1000000 },
101         /* millivolts */
102         { 1, 1000 },
103         { 2, 1000 },
104         { 5, 1000 },
105         { 10, 1000 },
106         { 20, 1000 },
107         { 50, 1000 },
108         { 100, 1000 },
109         { 200, 1000 },
110         { 500, 1000 },
111         /* volts */
112         { 1, 1 },
113         { 2, 1 },
114         { 5, 1 },
115         { 10, 1 },
116 };
117
118 #define NUM_TIMEBASE  ARRAY_SIZE(timebases)
119 #define NUM_VDIV      ARRAY_SIZE(vdivs)
120
121 static const char *trigger_sources[] = {
122         "CH1",
123         "CH2",
124         "CH3",
125         "CH4",
126         "EXT",
127         "AC Line",
128         "D0",
129         "D1",
130         "D2",
131         "D3",
132         "D4",
133         "D5",
134         "D6",
135         "D7",
136         "D8",
137         "D9",
138         "D10",
139         "D11",
140         "D12",
141         "D13",
142         "D14",
143         "D15",
144 };
145
146 static const char *coupling[] = {
147         "AC",
148         "DC",
149         "GND",
150 };
151
152 /* Do not change the order of entries */
153 static const char *data_sources[] = {
154         "Live",
155         "Memory",
156         "Segmented",
157 };
158
159 enum vendor {
160         RIGOL,
161         AGILENT,
162 };
163
164 enum series {
165         VS5000,
166         DS1000,
167         DS2000,
168         DS2000A,
169         DSO1000,
170 };
171
172 /* short name, full name */
173 static const struct rigol_ds_vendor supported_vendors[] = {
174         [RIGOL] = {"Rigol", "Rigol Technologies"},
175         [AGILENT] = {"Agilent", "Rigol Technologies"},
176 };
177
178 #define VENDOR(x) &supported_vendors[x]
179 /* vendor, series, protocol, max timebase, min vdiv, number of horizontal divs,
180  * live waveform samples, memory buffer samples */
181 static const struct rigol_ds_series supported_series[] = {
182         [VS5000] = {VENDOR(RIGOL), "VS5000", PROTOCOL_V1, FORMAT_RAW,
183                 {50, 1}, {2, 1000}, 14, 2048, 0},
184         [DS1000] = {VENDOR(RIGOL), "DS1000", PROTOCOL_V2, FORMAT_IEEE488_2,
185                 {50, 1}, {2, 1000}, 12, 600, 1048576},
186         [DS2000] = {VENDOR(RIGOL), "DS2000", PROTOCOL_V3, FORMAT_IEEE488_2,
187                 {500, 1}, {2, 1000}, 14, 1400, 14000},
188         [DS2000A] = {VENDOR(RIGOL), "DS2000A", PROTOCOL_V3, FORMAT_IEEE488_2,
189                 {1000, 1}, {500, 1000000}, 14, 1400, 14000},
190         [DSO1000] = {VENDOR(AGILENT), "DSO1000", PROTOCOL_V3, FORMAT_IEEE488_2,
191                 {50, 1}, {2, 1000}, 12, 600, 20480},
192 };
193
194 #define SERIES(x) &supported_series[x]
195 /* series, model, min timebase, analog channels, digital */
196 static const struct rigol_ds_model supported_models[] = {
197         {SERIES(VS5000), "VS5022", {20, 1000000000}, 2, false},
198         {SERIES(VS5000), "VS5042", {10, 1000000000}, 2, false},
199         {SERIES(VS5000), "VS5062", {5, 1000000000}, 2, false},
200         {SERIES(VS5000), "VS5102", {2, 1000000000}, 2, false},
201         {SERIES(VS5000), "VS5202", {2, 1000000000}, 2, false},
202         {SERIES(VS5000), "VS5022D", {20, 1000000000}, 2, true},
203         {SERIES(VS5000), "VS5042D", {10, 1000000000}, 2, true},
204         {SERIES(VS5000), "VS5062D", {5, 1000000000}, 2, true},
205         {SERIES(VS5000), "VS5102D", {2, 1000000000}, 2, true},
206         {SERIES(VS5000), "VS5202D", {2, 1000000000}, 2, true},
207         {SERIES(DS1000), "DS1052E", {5, 1000000000}, 2, false},
208         {SERIES(DS1000), "DS1102E", {2, 1000000000}, 2, false},
209         {SERIES(DS1000), "DS1152E", {2, 1000000000}, 2, false},
210         {SERIES(DS1000), "DS1052D", {5, 1000000000}, 2, true},
211         {SERIES(DS1000), "DS1102D", {2, 1000000000}, 2, true},
212         {SERIES(DS1000), "DS1152D", {2, 1000000000}, 2, true},
213         {SERIES(DS2000), "DS2072", {5, 1000000000}, 2, false},
214         {SERIES(DS2000), "DS2102", {5, 1000000000}, 2, false},
215         {SERIES(DS2000), "DS2202", {2, 1000000000}, 2, false},
216         {SERIES(DS2000), "DS2302", {1, 1000000000}, 2, false},
217         {SERIES(DS2000A), "DS2072A", {5, 1000000000}, 2, false},
218         {SERIES(DS2000A), "DS2102A", {5, 1000000000}, 2, false},
219         {SERIES(DS2000A), "DS2202A", {2, 1000000000}, 2, false},
220         {SERIES(DS2000A), "DS2302A", {1, 1000000000}, 2, false},
221         {SERIES(DSO1000), "DSO1002A", {5, 1000000000}, 2, false},
222         {SERIES(DSO1000), "DSO1004A", {5, 1000000000}, 4, false},
223         {SERIES(DSO1000), "DSO1012A", {2, 1000000000}, 2, false},
224         {SERIES(DSO1000), "DSO1014A", {2, 1000000000}, 4, false},
225         {SERIES(DSO1000), "DSO1022A", {2, 1000000000}, 2, false},
226         {SERIES(DSO1000), "DSO1024A", {2, 1000000000}, 4, false},
227 };
228
229 SR_PRIV struct sr_dev_driver rigol_ds_driver_info;
230 static struct sr_dev_driver *di = &rigol_ds_driver_info;
231
232 static void clear_helper(void *priv)
233 {
234         struct dev_context *devc;
235
236         devc = priv;
237         g_free(devc->data);
238         g_free(devc->buffer);
239         g_free(devc->coupling[0]);
240         g_free(devc->coupling[1]);
241         g_free(devc->trigger_source);
242         g_free(devc->trigger_slope);
243         g_slist_free(devc->analog_groups[0].probes);
244         g_slist_free(devc->analog_groups[1].probes);
245         g_slist_free(devc->digital_group.probes);
246 }
247
248 static int dev_clear(void)
249 {
250         return std_dev_clear(di, clear_helper);
251 }
252
253 static int init(struct sr_context *sr_ctx)
254 {
255         return std_init(sr_ctx, di, LOG_PREFIX);
256 }
257
258 static int probe_port(const char *resource, const char *serialcomm, GSList **devices)
259 {
260         struct dev_context *devc;
261         struct sr_dev_inst *sdi;
262         struct sr_scpi_dev_inst *scpi;
263         struct sr_scpi_hw_info *hw_info;
264         struct sr_probe *probe;
265         long n[3];
266         unsigned int i;
267         const struct rigol_ds_model *model = NULL;
268         gchar *channel_name, **version;
269
270         *devices = NULL;
271
272         if (!(scpi = scpi_dev_inst_new(resource, serialcomm)))
273                 return SR_ERR;
274
275         if (sr_scpi_open(scpi) != SR_OK) {
276                 sr_info("Couldn't open SCPI device.");
277                 sr_scpi_free(scpi);
278                 return SR_ERR;
279         };
280
281         if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
282                 sr_info("Couldn't get IDN response.");
283                 sr_scpi_close(scpi);
284                 sr_scpi_free(scpi);
285                 return SR_ERR;
286         }
287
288         for (i = 0; i < ARRAY_SIZE(supported_models); i++) {
289                 if (!strcasecmp(hw_info->manufacturer,
290                                         supported_models[i].series->vendor->full_name) &&
291                                 !strcmp(hw_info->model, supported_models[i].name)) {
292                         model = &supported_models[i];
293                         break;
294                 }
295         }
296
297         if (!model || !(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE,
298                                               model->series->vendor->name,
299                                                   model->name,
300                                                   hw_info->firmware_version))) {
301                 sr_scpi_hw_info_free(hw_info);
302                 sr_scpi_close(scpi);
303                 sr_scpi_free(scpi);
304                 return SR_ERR_NA;
305         }
306
307         sr_scpi_close(scpi);
308
309         sdi->conn = scpi;
310
311         sdi->driver = di;
312         sdi->inst_type = SR_INST_SCPI;
313
314         if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
315                 return SR_ERR_MALLOC;
316
317         devc->limit_frames = 0;
318         devc->model = model;
319         devc->format = model->series->format;
320
321         /* DS1000 models with firmware before 0.2.4 used the old data format. */
322         if (model->series == SERIES(DS1000)) {
323                 version = g_strsplit(hw_info->firmware_version, ".", 0);
324                 do {
325                         if (!version[0] || !version[1] || !version[2])
326                                 break;
327                         if (version[0][0] == 0 || version[1][0] == 0 || version[2][0] == 0)
328                                 break;
329                         for (i = 0; i < 3; i++) {
330                                 if (sr_atol(version[i], &n[i]) != SR_OK)
331                                         break;
332                         }
333                         if (i != 3)
334                                 break;
335                         if (n[0] != 0 || n[1] > 2)
336                                 break;
337                         if (n[1] == 2 && n[2] > 3)
338                                 break;
339                         sr_dbg("Found DS1000 firmware < 0.2.4, using raw data format.");
340                         devc->format = FORMAT_RAW;
341                 } while(0);
342                 g_strfreev(version);
343         }
344
345         sr_scpi_hw_info_free(hw_info);
346
347         for (i = 0; i < model->analog_channels; i++) {
348                 if (!(channel_name = g_strdup_printf("CH%d", i + 1)))
349                         return SR_ERR_MALLOC;
350                 probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE, channel_name);
351                 sdi->probes = g_slist_append(sdi->probes, probe);
352                 devc->analog_groups[i].name = channel_name;
353                 devc->analog_groups[i].probes = g_slist_append(NULL, probe);
354                 sdi->probe_groups = g_slist_append(sdi->probe_groups,
355                                 &devc->analog_groups[i]);
356         }
357
358         if (devc->model->has_digital) {
359                 for (i = 0; i < 16; i++) {
360                         if (!(channel_name = g_strdup_printf("D%d", i)))
361                                 return SR_ERR_MALLOC;
362                         probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE, channel_name);
363                         g_free(channel_name);
364                         if (!probe)
365                                 return SR_ERR_MALLOC;
366                         sdi->probes = g_slist_append(sdi->probes, probe);
367                         devc->digital_group.probes = g_slist_append(
368                                         devc->digital_group.probes, probe);
369                 }
370                 devc->digital_group.name = "LA";
371                 sdi->probe_groups = g_slist_append(sdi->probe_groups,
372                                 &devc->digital_group);
373         }
374
375         for (i = 0; i < NUM_TIMEBASE; i++) {
376                 if (!memcmp(&devc->model->min_timebase, &timebases[i], sizeof(uint64_t[2])))
377                         devc->timebases = &timebases[i];
378                 if (!memcmp(&devc->model->series->max_timebase, &timebases[i], sizeof(uint64_t[2])))
379                         devc->num_timebases = &timebases[i] - devc->timebases + 1;
380         }
381
382         for (i = 0; i < NUM_VDIV; i++)
383                 if (!memcmp(&devc->model->series->min_vdiv, &vdivs[i], sizeof(uint64_t[2])))
384                         devc->vdivs = &vdivs[i];
385
386         if (!(devc->buffer = g_try_malloc(ACQ_BUFFER_SIZE)))
387                 return SR_ERR_MALLOC;
388         if (!(devc->data = g_try_malloc(ACQ_BUFFER_SIZE * sizeof(float))))
389                 return SR_ERR_MALLOC;
390
391         devc->data_source = DATA_SOURCE_LIVE;
392
393         sdi->priv = devc;
394
395         *devices = g_slist_append(NULL, sdi);
396
397         return SR_OK;
398 }
399
400 static GSList *scan(GSList *options)
401 {
402         struct drv_context *drvc;
403         struct sr_config *src;
404         GSList *l, *devices;
405         GDir *dir;
406         int ret;
407         const gchar *dev_name;
408         gchar *port = NULL;
409         gchar *serialcomm = NULL;
410
411         drvc = di->priv;
412
413         for (l = options; l; l = l->next) {
414                 src = l->data;
415                 switch (src->key) {
416                 case SR_CONF_CONN:
417                         port = (char *)g_variant_get_string(src->data, NULL);
418                         break;
419                 case SR_CONF_SERIALCOMM:
420                         serialcomm = (char *)g_variant_get_string(src->data, NULL);
421                         break;
422                 }
423         }
424
425         devices = NULL;
426         if (port) {
427                 if (probe_port(port, serialcomm, &devices) == SR_ERR_MALLOC) {
428                         g_free(port);
429                         if (serialcomm)
430                                 g_free(serialcomm);
431                         return NULL;
432                 }
433         } else {
434                 if (!(dir = g_dir_open("/sys/class/usbmisc/", 0, NULL)))
435                         if (!(dir = g_dir_open("/sys/class/usb/", 0, NULL)))
436                                 return NULL;
437                 while ((dev_name = g_dir_read_name(dir))) {
438                         if (strncmp(dev_name, "usbtmc", 6))
439                                 continue;
440                         port = g_strconcat("/dev/", dev_name, NULL);
441                         ret = probe_port(port, serialcomm, &devices);
442                         g_free(port);
443                         if (serialcomm)
444                                 g_free(serialcomm);
445                         if (ret == SR_ERR_MALLOC) {
446                                 g_dir_close(dir);
447                                 return NULL;
448                         }
449                 }
450                 g_dir_close(dir);
451         }
452
453         /* Tack a copy of the newly found devices onto the driver list. */
454         l = g_slist_copy(devices);
455         drvc->instances = g_slist_concat(drvc->instances, l);
456
457         return devices;
458 }
459
460 static GSList *dev_list(void)
461 {
462         return ((struct drv_context *)(di->priv))->instances;
463 }
464
465 static int dev_open(struct sr_dev_inst *sdi)
466 {
467         struct sr_scpi_dev_inst *scpi = sdi->conn;
468
469         if (sr_scpi_open(scpi) < 0)
470                 return SR_ERR;
471
472         if (rigol_ds_get_dev_cfg(sdi) != SR_OK)
473                 return SR_ERR;
474
475         sdi->status = SR_ST_ACTIVE;
476
477         return SR_OK;
478 }
479
480 static int dev_close(struct sr_dev_inst *sdi)
481 {
482         struct sr_scpi_dev_inst *scpi;
483         struct dev_context *devc;
484
485         scpi = sdi->conn;
486         devc = sdi->priv;
487
488         if (devc->model->series->protocol >= PROTOCOL_V2)
489                 rigol_ds_config_set(sdi, ":KEY:LOCK DISABLE");
490
491         if (scpi) {
492                 if (sr_scpi_close(scpi) < 0)
493                         return SR_ERR;
494                 sdi->status = SR_ST_INACTIVE;
495         }
496
497         return SR_OK;
498 }
499
500 static int cleanup(void)
501 {
502         return dev_clear();
503 }
504
505 static int analog_frame_size(const struct sr_dev_inst *sdi)
506 {
507         struct dev_context *devc = sdi->priv;
508         struct sr_probe *probe;
509         int analog_probes = 0;
510         GSList *l;
511
512         for (l = sdi->probes; l; l = l->next) {
513                 probe = l->data;
514                 if (probe->type == SR_PROBE_ANALOG && probe->enabled)
515                         analog_probes++;
516         }
517
518         if (analog_probes == 0)
519                 return 0;
520
521         switch (devc->data_source) {
522         case DATA_SOURCE_LIVE:
523                 return devc->model->series->live_samples;
524         case DATA_SOURCE_MEMORY:
525                 return devc->model->series->buffer_samples / analog_probes;
526         default:
527                 return 0;
528         }
529 }
530
531 static int digital_frame_size(const struct sr_dev_inst *sdi)
532 {
533         struct dev_context *devc = sdi->priv;
534
535         switch (devc->data_source) {
536         case DATA_SOURCE_LIVE:
537                 return devc->model->series->live_samples * 2;
538         case DATA_SOURCE_MEMORY:
539                 return devc->model->series->buffer_samples * 2;
540         default:
541                 return 0;
542         }
543 }
544
545 static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi,
546                 const struct sr_probe_group *probe_group)
547 {
548         struct dev_context *devc;
549         struct sr_probe *probe;
550         const char *tmp_str;
551         uint64_t samplerate;
552         int analog_channel = -1;
553         float smallest_diff = 0.0000000001;
554         int idx = -1;
555         unsigned i;
556
557         if (!sdi || !(devc = sdi->priv))
558                 return SR_ERR_ARG;
559
560         /* If a probe group is specified, it must be a valid one. */
561         if (probe_group && !g_slist_find(sdi->probe_groups, probe_group)) {
562                 sr_err("Invalid probe group specified.");
563                 return SR_ERR;
564         }
565
566         if (probe_group) {
567                 probe = g_slist_nth_data(probe_group->probes, 0);
568                 if (!probe)
569                         return SR_ERR;
570                 if (probe->type == SR_PROBE_ANALOG) {
571                         if (probe->name[2] < '1' || probe->name[2] > '4')
572                                 return SR_ERR;
573                         analog_channel = probe->name[2] - '1';
574                 }
575         }
576
577         switch (id) {
578         case SR_CONF_NUM_TIMEBASE:
579                 *data = g_variant_new_int32(devc->model->series->num_horizontal_divs);
580                 break;
581         case SR_CONF_NUM_VDIV:
582                 *data = g_variant_new_int32(NUM_VDIV);
583         case SR_CONF_DATA_SOURCE:
584                 if (devc->data_source == DATA_SOURCE_LIVE)
585                         *data = g_variant_new_string("Live");
586                 else if (devc->data_source == DATA_SOURCE_MEMORY)
587                         *data = g_variant_new_string("Memory");
588                 else
589                         *data = g_variant_new_string("Segmented");
590                 break;
591         case SR_CONF_SAMPLERATE:
592                 if (devc->data_source == DATA_SOURCE_LIVE) {
593                         samplerate = analog_frame_size(sdi) /
594                                 (devc->timebase * devc->model->series->num_horizontal_divs);
595                         *data = g_variant_new_uint64(samplerate);
596                 } else {
597                         return SR_ERR_NA;
598                 }
599                 break;
600         case SR_CONF_TRIGGER_SOURCE:
601                 if (!strcmp(devc->trigger_source, "ACL"))
602                         tmp_str = "AC Line";
603                 else if (!strcmp(devc->trigger_source, "CHAN1"))
604                         tmp_str = "CH1";
605                 else if (!strcmp(devc->trigger_source, "CHAN2"))
606                         tmp_str = "CH2";
607                 else if (!strcmp(devc->trigger_source, "CHAN3"))
608                         tmp_str = "CH3";
609                 else if (!strcmp(devc->trigger_source, "CHAN4"))
610                         tmp_str = "CH4";
611                 else
612                         tmp_str = devc->trigger_source;
613                 *data = g_variant_new_string(tmp_str);
614                 break;
615         case SR_CONF_TIMEBASE:
616                 for (i = 0; i < devc->num_timebases; i++) {
617                         float tb = (float)devc->timebases[i][0] / devc->timebases[i][1];
618                         float diff = fabs(devc->timebase - tb);
619                         if (diff < smallest_diff) {
620                                 smallest_diff = diff;
621                                 idx = i;
622                         }
623                 }
624                 if (idx < 0)
625                         return SR_ERR_NA;
626                 *data = g_variant_new("(tt)", devc->timebases[idx][0],
627                                               devc->timebases[idx][1]);
628                 break;
629         case SR_CONF_VDIV:
630                 if (analog_channel < 0)
631                         return SR_ERR_NA;
632                 for (i = 0; i < ARRAY_SIZE(vdivs); i++) {
633                         float vdiv = (float)vdivs[i][0] / vdivs[i][1];
634                         float diff = fabs(devc->vdiv[analog_channel] - vdiv);
635                         if (diff < smallest_diff) {
636                                 smallest_diff = diff;
637                                 idx = i;
638                         }
639                 }
640                 if (idx < 0)
641                         return SR_ERR_NA;
642                 *data = g_variant_new("(tt)", vdivs[idx][0], vdivs[idx][1]);
643                 break;
644         case SR_CONF_COUPLING:
645                 if (analog_channel < 0)
646                         return SR_ERR_NA;
647                 *data = g_variant_new_string(devc->coupling[analog_channel]);
648                 break;
649         default:
650                 return SR_ERR_NA;
651         }
652
653         return SR_OK;
654 }
655
656 static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
657                 const struct sr_probe_group *probe_group)
658 {
659         struct dev_context *devc;
660         uint64_t p, q;
661         double t_dbl;
662         unsigned int i, j;
663         int ret;
664         const char *tmp_str;
665         char buffer[16];
666
667         if (!(devc = sdi->priv))
668                 return SR_ERR_ARG;
669
670         if (sdi->status != SR_ST_ACTIVE)
671                 return SR_ERR_DEV_CLOSED;
672
673         /* If a probe group is specified, it must be a valid one. */
674         if (probe_group && !g_slist_find(sdi->probe_groups, probe_group)) {
675                 sr_err("Invalid probe group specified.");
676                 return SR_ERR;
677         }
678
679         ret = SR_OK;
680         switch (id) {
681         case SR_CONF_LIMIT_FRAMES:
682                 devc->limit_frames = g_variant_get_uint64(data);
683                 break;
684         case SR_CONF_TRIGGER_SLOPE:
685                 tmp_str = g_variant_get_string(data, NULL);
686
687                 if (!tmp_str || !(tmp_str[0] == 'f' || tmp_str[0] == 'r'))
688                         return SR_ERR_ARG;
689
690                 g_free(devc->trigger_slope);
691                 devc->trigger_slope = g_strdup((tmp_str[0] == 'r') ? "POS" : "NEG");
692                 ret = rigol_ds_config_set(sdi, ":TRIG:EDGE:SLOP %s", devc->trigger_slope);
693                 break;
694         case SR_CONF_HORIZ_TRIGGERPOS:
695                 t_dbl = g_variant_get_double(data);
696                 if (t_dbl < 0.0 || t_dbl > 1.0)
697                         return SR_ERR;
698                 devc->horiz_triggerpos = t_dbl;
699                 /* We have the trigger offset as a percentage of the frame, but
700                  * need to express this in seconds. */
701                 t_dbl = -(devc->horiz_triggerpos - 0.5) * devc->timebase * devc->num_timebases;
702                 g_ascii_formatd(buffer, sizeof(buffer), "%.6f", t_dbl);
703                 ret = rigol_ds_config_set(sdi, ":TIM:OFFS %s", buffer);
704                 break;
705         case SR_CONF_TIMEBASE:
706                 g_variant_get(data, "(tt)", &p, &q);
707                 for (i = 0; i < devc->num_timebases; i++) {
708                         if (devc->timebases[i][0] == p && devc->timebases[i][1] == q) {
709                                 devc->timebase = (float)p / q;
710                                 g_ascii_formatd(buffer, sizeof(buffer), "%.9f",
711                                                 devc->timebase);
712                                 ret = rigol_ds_config_set(sdi, ":TIM:SCAL %s", buffer);
713                                 break;
714                         }
715                 }
716                 if (i == devc->num_timebases)
717                         ret = SR_ERR_ARG;
718                 break;
719         case SR_CONF_TRIGGER_SOURCE:
720                 tmp_str = g_variant_get_string(data, NULL);
721                 for (i = 0; i < ARRAY_SIZE(trigger_sources); i++) {
722                         if (!strcmp(trigger_sources[i], tmp_str)) {
723                                 g_free(devc->trigger_source);
724                                 devc->trigger_source = g_strdup(trigger_sources[i]);
725                                 if (!strcmp(devc->trigger_source, "AC Line"))
726                                         tmp_str = "ACL";
727                                 else if (!strcmp(devc->trigger_source, "CH1"))
728                                         tmp_str = "CHAN1";
729                                 else if (!strcmp(devc->trigger_source, "CH2"))
730                                         tmp_str = "CHAN2";
731                                 else if (!strcmp(devc->trigger_source, "CH3"))
732                                         tmp_str = "CHAN3";
733                                 else if (!strcmp(devc->trigger_source, "CH4"))
734                                         tmp_str = "CHAN4";
735                                 else
736                                         tmp_str = (char *)devc->trigger_source;
737                                 ret = rigol_ds_config_set(sdi, ":TRIG:EDGE:SOUR %s", tmp_str);
738                                 break;
739                         }
740                 }
741                 if (i == ARRAY_SIZE(trigger_sources))
742                         ret = SR_ERR_ARG;
743                 break;
744         case SR_CONF_VDIV:
745                 if (!probe_group) {
746                         sr_err("No probe group specified.");
747                         return SR_ERR_PROBE_GROUP;
748                 }
749                 g_variant_get(data, "(tt)", &p, &q);
750                 for (i = 0; i < 2; i++) {
751                         if (probe_group == &devc->analog_groups[i]) {
752                                 for (j = 0; j < ARRAY_SIZE(vdivs); j++) {
753                                         if (vdivs[j][0] != p || vdivs[j][1] != q)
754                                                 continue;
755                                         devc->vdiv[i] = (float)p / q;
756                                         g_ascii_formatd(buffer, sizeof(buffer), "%.3f",
757                                                         devc->vdiv[i]);
758                                         return rigol_ds_config_set(sdi, ":CHAN%d:SCAL %s", i + 1,
759                                                         buffer);
760                                 }
761                                 return SR_ERR_ARG;
762                         }
763                 }
764                 return SR_ERR_NA;
765         case SR_CONF_COUPLING:
766                 if (!probe_group) {
767                         sr_err("No probe group specified.");
768                         return SR_ERR_PROBE_GROUP;
769                 }
770                 tmp_str = g_variant_get_string(data, NULL);
771                 for (i = 0; i < 2; i++) {
772                         if (probe_group == &devc->analog_groups[i]) {
773                                 for (j = 0; j < ARRAY_SIZE(coupling); j++) {
774                                         if (!strcmp(tmp_str, coupling[j])) {
775                                                 g_free(devc->coupling[i]);
776                                                 devc->coupling[i] = g_strdup(coupling[j]);
777                                                 return rigol_ds_config_set(sdi, ":CHAN%d:COUP %s", i + 1,
778                                                                 devc->coupling[i]);
779                                         }
780                                 }
781                                 return SR_ERR_ARG;
782                         }
783                 }
784                 return SR_ERR_NA;
785         case SR_CONF_DATA_SOURCE:
786                 tmp_str = g_variant_get_string(data, NULL);
787                 if (!strcmp(tmp_str, "Live"))
788                         devc->data_source = DATA_SOURCE_LIVE;
789                 else if (devc->model->series->protocol >= PROTOCOL_V2
790                         && !strcmp(tmp_str, "Memory"))
791                         devc->data_source = DATA_SOURCE_MEMORY;
792                 else if (devc->model->series->protocol >= PROTOCOL_V3
793                          && !strcmp(tmp_str, "Segmented"))
794                         devc->data_source = DATA_SOURCE_SEGMENTED;
795                 else
796                         return SR_ERR;
797                 break;
798         default:
799                 ret = SR_ERR_NA;
800                 break;
801         }
802
803         return ret;
804 }
805
806 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
807                 const struct sr_probe_group *probe_group)
808 {
809         GVariant *tuple, *rational[2];
810         GVariantBuilder gvb;
811         unsigned int i;
812         struct dev_context *devc = NULL;
813
814         if (sdi)
815                 devc = sdi->priv;
816
817         if (key == SR_CONF_SCAN_OPTIONS) {
818                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
819                                 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
820                 return SR_OK;
821         } else if (key == SR_CONF_DEVICE_OPTIONS && probe_group == NULL) {
822                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
823                         hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
824                 return SR_OK;
825         }
826
827         /* Every other option requires a valid device instance. */
828         if (!sdi || !(devc = sdi->priv))
829                 return SR_ERR_ARG;
830
831         /* If a probe group is specified, it must be a valid one. */
832         if (probe_group) {
833                 if (probe_group != &devc->analog_groups[0]
834                                 && probe_group != &devc->analog_groups[1]) {
835                         sr_err("Invalid probe group specified.");
836                         return SR_ERR;
837                 }
838         }
839
840         switch (key) {
841         case SR_CONF_DEVICE_OPTIONS:
842                 if (!probe_group) {
843                         sr_err("No probe group specified.");
844                         return SR_ERR_PROBE_GROUP;
845                 }
846                 if (probe_group == &devc->digital_group) {
847                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
848                                 NULL, 0, sizeof(int32_t));
849                         return SR_OK;
850                 } else {
851                         for (i = 0; i < 2; i++) {
852                                 if (probe_group == &devc->analog_groups[i]) {
853                                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
854                                                 analog_hwcaps, ARRAY_SIZE(analog_hwcaps), sizeof(int32_t));
855                                         return SR_OK;
856                                 }
857                         }
858                         return SR_ERR_NA;
859                 }
860                 break;
861         case SR_CONF_COUPLING:
862                 if (!probe_group) {
863                         sr_err("No probe group specified.");
864                         return SR_ERR_PROBE_GROUP;
865                 }
866                 *data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling));
867                 break;
868         case SR_CONF_VDIV:
869                 if (!devc)
870                         /* Can't know this until we have the exact model. */
871                         return SR_ERR_ARG;
872                 if (!probe_group) {
873                         sr_err("No probe group specified.");
874                         return SR_ERR_PROBE_GROUP;
875                 }
876                 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
877                 for (i = 0; i < NUM_VDIV; i++) {
878                         rational[0] = g_variant_new_uint64(devc->vdivs[i][0]);
879                         rational[1] = g_variant_new_uint64(devc->vdivs[i][1]);
880                         tuple = g_variant_new_tuple(rational, 2);
881                         g_variant_builder_add_value(&gvb, tuple);
882                 }
883                 *data = g_variant_builder_end(&gvb);
884                 break;
885         case SR_CONF_TIMEBASE:
886                 if (!devc)
887                         /* Can't know this until we have the exact model. */
888                         return SR_ERR_ARG;
889                 if (devc->num_timebases <= 0)
890                         return SR_ERR_NA;
891                 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
892                 for (i = 0; i < devc->num_timebases; i++) {
893                         rational[0] = g_variant_new_uint64(devc->timebases[i][0]);
894                         rational[1] = g_variant_new_uint64(devc->timebases[i][1]);
895                         tuple = g_variant_new_tuple(rational, 2);
896                         g_variant_builder_add_value(&gvb, tuple);
897                 }
898                 *data = g_variant_builder_end(&gvb);
899                 break;
900         case SR_CONF_TRIGGER_SOURCE:
901                 if (!devc)
902                         /* Can't know this until we have the exact model. */
903                         return SR_ERR_ARG;
904                 *data = g_variant_new_strv(trigger_sources,
905                                 devc->model->has_digital ? ARRAY_SIZE(trigger_sources) : 4);
906                 break;
907         case SR_CONF_DATA_SOURCE:
908                 if (!devc)
909                         /* Can't know this until we have the exact model. */
910                         return SR_ERR_ARG;
911                 switch (devc->model->series->protocol) {
912                 case PROTOCOL_V1:
913                         *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources) - 2);
914                         break;
915                 case PROTOCOL_V2:
916                         *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources) - 1);
917                         break;
918                 default:
919                         *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources));
920                         break;
921                 }
922                 break;
923         default:
924                 return SR_ERR_NA;
925         }
926
927         return SR_OK;
928 }
929
930 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
931 {
932         struct sr_scpi_dev_inst *scpi;
933         struct dev_context *devc;
934         struct sr_probe *probe;
935         struct sr_datafeed_packet packet;
936         GSList *l;
937
938         if (sdi->status != SR_ST_ACTIVE)
939                 return SR_ERR_DEV_CLOSED;
940
941         scpi = sdi->conn;
942         devc = sdi->priv;
943
944         devc->num_frames = 0;
945
946         for (l = sdi->probes; l; l = l->next) {
947                 probe = l->data;
948                 sr_dbg("handling probe %s", probe->name);
949                 if (probe->type == SR_PROBE_ANALOG) {
950                         if (probe->enabled)
951                                 devc->enabled_analog_probes = g_slist_append(
952                                                 devc->enabled_analog_probes, probe);
953                         if (probe->enabled != devc->analog_channels[probe->index]) {
954                                 /* Enabled channel is currently disabled, or vice versa. */
955                                 if (rigol_ds_config_set(sdi, ":CHAN%d:DISP %s", probe->index + 1,
956                                                 probe->enabled ? "ON" : "OFF") != SR_OK)
957                                         return SR_ERR;
958                                 devc->analog_channels[probe->index] = probe->enabled;
959                         }
960                 } else if (probe->type == SR_PROBE_LOGIC) {
961                         if (probe->enabled) {
962                                 devc->enabled_digital_probes = g_slist_append(
963                                                 devc->enabled_digital_probes, probe);
964                                 /* Turn on LA module if currently off. */
965                                 if (!devc->la_enabled) {
966                                         if (rigol_ds_config_set(sdi, ":LA:DISP ON") != SR_OK)
967                                                 return SR_ERR;
968                                         devc->la_enabled = TRUE;
969                                 }
970                         }
971                         if (probe->enabled != devc->digital_channels[probe->index]) {
972                                 /* Enabled channel is currently disabled, or vice versa. */
973                                 if (rigol_ds_config_set(sdi, ":DIG%d:TURN %s", probe->index,
974                                                 probe->enabled ? "ON" : "OFF") != SR_OK)
975                                         return SR_ERR;
976                                 devc->digital_channels[probe->index] = probe->enabled;
977                         }
978                 }
979         }
980
981         if (!devc->enabled_analog_probes && !devc->enabled_digital_probes)
982                 return SR_ERR;
983
984         /* Turn off LA module if on and no digital probes selected. */
985         if (devc->la_enabled && !devc->enabled_digital_probes)
986                 if (rigol_ds_config_set(sdi, ":LA:DISP OFF") != SR_OK)
987                         return SR_ERR;
988
989         /* Set memory mode. */
990         if (devc->data_source == DATA_SOURCE_SEGMENTED) {
991                 sr_err("Data source 'Segmented' not yet supported");
992                 return SR_ERR;
993         }
994
995         devc->analog_frame_size = analog_frame_size(sdi);
996         devc->digital_frame_size = digital_frame_size(sdi);
997
998         switch (devc->model->series->protocol) {
999         case PROTOCOL_V2:
1000                 if (rigol_ds_config_set(sdi, ":ACQ:MDEP LONG") != SR_OK)
1001                         return SR_ERR;
1002                 break;
1003         case PROTOCOL_V3:
1004                 /* Apparently for the DS2000 the memory
1005                  * depth can only be set in Running state -
1006                  * this matches the behaviour of the UI. */
1007                 if (rigol_ds_config_set(sdi, ":RUN") != SR_OK)
1008                         return SR_ERR;
1009                 if (rigol_ds_config_set(sdi, ":ACQ:MDEP %d",
1010                                         devc->analog_frame_size) != SR_OK)
1011                         return SR_ERR;
1012                 if (rigol_ds_config_set(sdi, ":STOP") != SR_OK)
1013                         return SR_ERR;
1014                 break;
1015         default:
1016                 break;
1017         }
1018
1019         if (devc->data_source == DATA_SOURCE_LIVE)
1020                 if (rigol_ds_config_set(sdi, ":RUN") != SR_OK)
1021                         return SR_ERR;
1022
1023         sr_scpi_source_add(scpi, G_IO_IN, 50, rigol_ds_receive, (void *)sdi);
1024
1025         /* Send header packet to the session bus. */
1026         std_session_send_df_header(cb_data, LOG_PREFIX);
1027
1028         if (devc->enabled_analog_probes)
1029                 devc->channel_entry = devc->enabled_analog_probes;
1030         else
1031                 devc->channel_entry = devc->enabled_digital_probes;
1032
1033         if (rigol_ds_capture_start(sdi) != SR_OK)
1034                 return SR_ERR;
1035
1036         /* Start of first frame. */
1037         packet.type = SR_DF_FRAME_BEGIN;
1038         sr_session_send(cb_data, &packet);
1039
1040         return SR_OK;
1041 }
1042
1043 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
1044 {
1045         struct dev_context *devc;
1046         struct sr_scpi_dev_inst *scpi;
1047         struct sr_datafeed_packet packet;
1048
1049         (void)cb_data;
1050
1051         devc = sdi->priv;
1052
1053         if (sdi->status != SR_ST_ACTIVE) {
1054                 sr_err("Device inactive, can't stop acquisition.");
1055                 return SR_ERR;
1056         }
1057
1058         /* End of last frame. */
1059         packet.type = SR_DF_END;
1060         sr_session_send(sdi, &packet);
1061
1062         g_slist_free(devc->enabled_analog_probes);
1063         g_slist_free(devc->enabled_digital_probes);
1064         devc->enabled_analog_probes = NULL;
1065         devc->enabled_digital_probes = NULL;
1066         scpi = sdi->conn;
1067         sr_scpi_source_remove(scpi);
1068
1069         return SR_OK;
1070 }
1071
1072 SR_PRIV struct sr_dev_driver rigol_ds_driver_info = {
1073         .name = "rigol-ds",
1074         .longname = "Rigol DS",
1075         .api_version = 1,
1076         .init = init,
1077         .cleanup = cleanup,
1078         .scan = scan,
1079         .dev_list = dev_list,
1080         .dev_clear = dev_clear,
1081         .config_get = config_get,
1082         .config_set = config_set,
1083         .config_list = config_list,
1084         .dev_open = dev_open,
1085         .dev_close = dev_close,
1086         .dev_acquisition_start = dev_acquisition_start,
1087         .dev_acquisition_stop = dev_acquisition_stop,
1088         .priv = NULL,
1089 };