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