]> sigrok.org Git - libsigrok.git/blob - hardware/rigol-ds/api.c
rigol-ds: Support VS5000 series devices.
[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 <glib.h>
27 #include "libsigrok.h"
28 #include "libsigrok-internal.h"
29 #include "protocol.h"
30
31 static const int32_t hwopts[] = {
32         SR_CONF_CONN,
33         SR_CONF_SERIALCOMM
34 };
35
36 static const int32_t hwcaps[] = {
37         SR_CONF_OSCILLOSCOPE,
38         SR_CONF_TIMEBASE,
39         SR_CONF_TRIGGER_SOURCE,
40         SR_CONF_TRIGGER_SLOPE,
41         SR_CONF_HORIZ_TRIGGERPOS,
42         SR_CONF_NUM_TIMEBASE,
43 };
44
45 static const int32_t analog_hwcaps[] = {
46         SR_CONF_NUM_VDIV,
47         SR_CONF_VDIV,
48         SR_CONF_COUPLING,
49         SR_CONF_DATA_SOURCE,
50 };
51
52 static const uint64_t timebases[][2] = {
53         /* nanoseconds */
54         { 2, 1000000000 },
55         { 5, 1000000000 },
56         { 10, 1000000000 },
57         { 20, 1000000000 },
58         { 50, 1000000000 },
59         { 100, 1000000000 },
60         { 500, 1000000000 },
61         /* microseconds */
62         { 1, 1000000 },
63         { 2, 1000000 },
64         { 5, 1000000 },
65         { 10, 1000000 },
66         { 20, 1000000 },
67         { 50, 1000000 },
68         { 100, 1000000 },
69         { 200, 1000000 },
70         { 500, 1000000 },
71         /* milliseconds */
72         { 1, 1000 },
73         { 2, 1000 },
74         { 5, 1000 },
75         { 10, 1000 },
76         { 20, 1000 },
77         { 50, 1000 },
78         { 100, 1000 },
79         { 200, 1000 },
80         { 500, 1000 },
81         /* seconds */
82         { 1, 1 },
83         { 2, 1 },
84         { 5, 1 },
85         { 10, 1 },
86         { 20, 1 },
87         { 50, 1 },
88         { 100, 1 },
89         { 200, 1 },
90         { 500, 1 },
91         /* { 1000, 1 }, Confuses other code? */
92 };
93
94 static const uint64_t vdivs[][2] = {
95         /* microvolts */
96         { 500, 1000000 },
97         /* millivolts */
98         { 1, 1000 },
99         { 2, 1000 },
100         { 5, 1000 },
101         { 10, 1000 },
102         { 20, 1000 },
103         { 50, 1000 },
104         { 100, 1000 },
105         { 200, 1000 },
106         { 500, 1000 },
107         /* volts */
108         { 1, 1 },
109         { 2, 1 },
110         { 5, 1 },
111         { 10, 1 },
112 };
113
114 #define NUM_TIMEBASE  ARRAY_SIZE(timebases)
115 #define NUM_VDIV      ARRAY_SIZE(vdivs)
116
117 static const char *trigger_sources[] = {
118         "CH1",
119         "CH2",
120         "EXT",
121         "AC Line",
122         "D0",
123         "D1",
124         "D2",
125         "D3",
126         "D4",
127         "D5",
128         "D6",
129         "D7",
130         "D8",
131         "D9",
132         "D10",
133         "D11",
134         "D12",
135         "D13",
136         "D14",
137         "D15",
138 };
139
140 static const char *coupling[] = {
141         "AC",
142         "DC",
143         "GND",
144 };
145
146 /* Do not change the order of entries */
147 static const char *data_sources[] = {
148         "Live",
149         "Memory",
150         "Segmented",
151 };
152
153 /* 
154  * name, series, protocol flavor, min timebase, max timebase, min vdiv,
155  * digital channels, number of horizontal divs
156  */
157 static const struct rigol_ds_model supported_models[] = {
158         {"DS1052E", RIGOL_DS1000, PROTOCOL_LEGACY, {5, 1000000000}, {50, 1}, {2, 1000}, false, 12},
159         {"DS1102E", RIGOL_DS1000, PROTOCOL_LEGACY, {2, 1000000000}, {50, 1}, {2, 1000}, false, 12},
160         {"DS1152E", RIGOL_DS1000, PROTOCOL_LEGACY, {2, 1000000000}, {50, 1}, {2, 1000}, false, 12},
161         {"DS1052D", RIGOL_DS1000, PROTOCOL_LEGACY, {5, 1000000000}, {50, 1}, {2, 1000}, true, 12},
162         {"DS1102D", RIGOL_DS1000, PROTOCOL_LEGACY, {2, 1000000000}, {50, 1}, {2, 1000}, true, 12},
163         {"DS1152D", RIGOL_DS1000, PROTOCOL_LEGACY, {2, 1000000000}, {50, 1}, {2, 1000}, true, 12},
164         {"DS2072", RIGOL_DS2000, PROTOCOL_IEEE488_2, {5, 1000000000}, {500, 1}, {500, 1000000}, false, 14},
165         {"DS2102", RIGOL_DS2000, PROTOCOL_IEEE488_2, {5, 1000000000}, {500, 1}, {500, 1000000}, false, 14},
166         {"DS2202", RIGOL_DS2000, PROTOCOL_IEEE488_2, {2, 1000000000}, {500, 1}, {500, 1000000}, false, 14},
167         {"VS5022", RIGOL_VS5000, PROTOCOL_LEGACY, {20, 1000000000}, {50, 1}, {2, 1000}, false, 14},
168         {"VS5022D", RIGOL_VS5000, PROTOCOL_LEGACY, {20, 1000000000}, {50, 1}, {2, 1000}, true, 14},
169         {"VS5042", RIGOL_VS5000, PROTOCOL_LEGACY, {10, 1000000000}, {50, 1}, {2, 1000}, false, 14},
170         {"VS5042D", RIGOL_VS5000, PROTOCOL_LEGACY, {10, 1000000000}, {50, 1}, {2, 1000}, true, 14},
171         {"VS5062", RIGOL_VS5000, PROTOCOL_LEGACY, {5, 1000000000}, {50, 1}, {2, 1000}, false, 14},
172         {"VS5062D", RIGOL_VS5000, PROTOCOL_LEGACY, {5, 1000000000}, {50, 1}, {2, 1000}, true, 14},
173         {"VS5102", RIGOL_VS5000, PROTOCOL_LEGACY, {2, 1000000000}, {50, 1}, {2, 1000}, false, 14},
174         {"VS5102D", RIGOL_VS5000, PROTOCOL_LEGACY, {2, 1000000000}, {50, 1}, {2, 1000}, true, 14},
175         {"VS5202", RIGOL_VS5000, PROTOCOL_LEGACY, {2, 1000000000}, {50, 1}, {2, 1000}, false, 14},
176         {"VS5202D", RIGOL_VS5000, PROTOCOL_LEGACY, {2, 1000000000}, {50, 1}, {2, 1000}, true, 14},
177 };
178
179 SR_PRIV struct sr_dev_driver rigol_ds_driver_info;
180 static struct sr_dev_driver *di = &rigol_ds_driver_info;
181
182 static void clear_helper(void *priv)
183 {
184         struct dev_context *devc;
185
186         devc = priv;
187         g_free(devc->data);
188         g_free(devc->buffer);
189         g_free(devc->coupling[0]);
190         g_free(devc->coupling[1]);
191         g_free(devc->trigger_source);
192         g_free(devc->trigger_slope);
193         g_slist_free(devc->analog_groups[0].probes);
194         g_slist_free(devc->analog_groups[1].probes);
195         g_slist_free(devc->digital_group.probes);
196 }
197
198 static int dev_clear(void)
199 {
200         return std_dev_clear(di, clear_helper);
201 }
202
203 static int set_cfg(const struct sr_dev_inst *sdi, const char *format, ...)
204 {
205         va_list args;
206         int ret;
207
208         va_start(args, format);
209         ret = sr_scpi_send_variadic(sdi->conn, format, args);
210         va_end(args);
211
212         if (ret != SR_OK)
213                 return SR_ERR;
214
215         /* When setting a bunch of parameters in a row, the DS1052E scrambles
216          * some of them unless there is at least 100ms delay in between. */
217         sr_spew("delay %dms", 100);
218         g_usleep(100000);
219
220         return SR_OK;
221 }
222
223 static int init(struct sr_context *sr_ctx)
224 {
225         return std_init(sr_ctx, di, LOG_PREFIX);
226 }
227
228 static int probe_port(const char *resource, const char *serialcomm, GSList **devices)
229 {
230         struct dev_context *devc;
231         struct sr_dev_inst *sdi;
232         const char *usbtmc_prefix = "/dev/usbtmc";
233         const char *tcp_prefix = "tcp/";
234         gchar **tokens, *address, *port;
235         struct sr_scpi_dev_inst *scpi;
236         struct sr_scpi_hw_info *hw_info;
237         struct sr_probe *probe;
238         unsigned int i;
239         const struct rigol_ds_model *model = NULL;
240         gchar *channel_name;
241
242         *devices = NULL;
243
244         if (strncmp(resource, usbtmc_prefix, strlen(usbtmc_prefix)) == 0) {
245                 sr_dbg("Opening USBTMC device %s", resource);
246                 if (!(scpi = scpi_usbtmc_dev_inst_new(resource)))
247                         return SR_ERR_MALLOC;
248         } else if (strncmp(resource, tcp_prefix, strlen(tcp_prefix)) == 0) {
249                 sr_dbg("Opening TCP connection %s", resource);
250                 tokens = g_strsplit(resource + strlen(tcp_prefix), "/", 0);
251                 address = tokens[0];
252                 port = tokens[1];
253                 if (!address || !port || tokens[2]) {
254                         sr_dbg("Invalid parameters");
255                         g_strfreev(tokens);
256                         return SR_ERR_ARG;
257                 }
258                 scpi = scpi_tcp_dev_inst_new(address, port);
259                 g_strfreev(tokens);
260                 if (!scpi)
261                         return SR_ERR_MALLOC;
262         } else {
263                 sr_dbg("Opening serial device %s", resource);
264                 if (!(scpi = scpi_serial_dev_inst_new(resource, serialcomm)))
265                         return SR_ERR_MALLOC;
266         }
267
268         if (sr_scpi_open(scpi) != SR_OK) {
269                 sr_scpi_free(scpi);
270                 return SR_ERR;
271         };
272
273         if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
274                 sr_info("Couldn't get IDN response.");
275                 sr_scpi_close(scpi);
276                 sr_scpi_free(scpi);
277                 return SR_ERR;
278         }
279
280         if (strcasecmp(hw_info->manufacturer, "Rigol Technologies")) {
281                 sr_scpi_hw_info_free(hw_info);
282                 sr_scpi_free(scpi);
283                 return SR_ERR_NA;
284         }
285
286         for (i = 0; i < ARRAY_SIZE(supported_models); i++) {
287                 if (!strcmp(hw_info->model, supported_models[i].name)) {
288                         model = &supported_models[i];
289                         break;
290                 }
291         }
292
293         if (!model || !(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE,
294                                               hw_info->manufacturer, hw_info->model,
295                                                   hw_info->firmware_version))) {
296                 sr_scpi_hw_info_free(hw_info);
297                 sr_scpi_free(scpi);
298                 return SR_ERR_NA;
299         }
300
301         sr_scpi_hw_info_free(hw_info);
302
303         sdi->conn = scpi;
304
305         sdi->driver = di;
306         sdi->inst_type = SR_INST_SCPI;
307
308         if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
309                 return SR_ERR_MALLOC;
310
311         devc->limit_frames = 0;
312         devc->model = model;
313
314         for (i = 0; i < 2; i++) {
315                 channel_name = (i == 0 ? "CH1" : "CH2");
316                 if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE, channel_name)))
317                         return SR_ERR_MALLOC;
318                 sdi->probes = g_slist_append(sdi->probes, probe);
319                 devc->analog_groups[i].name = channel_name;
320                 devc->analog_groups[i].probes = g_slist_append(NULL, probe);
321                 sdi->probe_groups = g_slist_append(sdi->probe_groups,
322                                 &devc->analog_groups[i]);
323         }
324
325         if (devc->model->has_digital) {
326                 for (i = 0; i < 16; i++) {
327                         if (!(channel_name = g_strdup_printf("D%d", i)))
328                                 return SR_ERR_MALLOC;
329                         probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE, channel_name);
330                         g_free(channel_name);
331                         if (!probe)
332                                 return SR_ERR_MALLOC;
333                         sdi->probes = g_slist_append(sdi->probes, probe);
334                         devc->digital_group.probes = g_slist_append(
335                                         devc->digital_group.probes, probe);
336                 }
337                 devc->digital_group.name = "LA";
338                 sdi->probe_groups = g_slist_append(sdi->probe_groups,
339                                 &devc->digital_group);
340         }
341
342         for (i = 0; i < NUM_TIMEBASE; i++) {
343                 if (!memcmp(&devc->model->min_timebase, &timebases[i], sizeof(uint64_t[2])))
344                         devc->timebases = &timebases[i];
345                 if (!memcmp(&devc->model->max_timebase, &timebases[i], sizeof(uint64_t[2])))
346                         devc->num_timebases = &timebases[i] - devc->timebases + 1;
347         }
348
349         for (i = 0; i < NUM_VDIV; i++) {
350                 if (!memcmp(&devc->model->min_vdiv, &vdivs[i], sizeof(uint64_t[2]))) {
351                         devc->vdivs = &vdivs[i];
352                         devc->num_vdivs = NUM_VDIV - (&vdivs[i] - &vdivs[0]);
353                 }
354         }
355
356         if (!(devc->buffer = g_try_malloc(ACQ_BUFFER_SIZE)))
357                 return SR_ERR_MALLOC;
358         if (!(devc->data = g_try_malloc(ACQ_BUFFER_SIZE * sizeof(float))))
359                 return SR_ERR_MALLOC;
360
361         devc->data_source = DATA_SOURCE_LIVE;
362
363         sdi->priv = devc;
364
365         *devices = g_slist_append(NULL, sdi);
366
367         return SR_OK;
368 }
369
370 static GSList *scan(GSList *options)
371 {
372         struct drv_context *drvc;
373         struct sr_config *src;
374         GSList *l, *devices;
375         GDir *dir;
376         int ret;
377         const gchar *dev_name;
378         gchar *port = NULL;
379         gchar *serialcomm = NULL;
380
381         drvc = di->priv;
382
383         for (l = options; l; l = l->next) {
384                 src = l->data;
385                 switch (src->key) {
386                 case SR_CONF_CONN:
387                         port = (char *)g_variant_get_string(src->data, NULL);
388                         break;
389                 case SR_CONF_SERIALCOMM:
390                         serialcomm = (char *)g_variant_get_string(src->data, NULL);
391                         break;
392                 }
393         }
394
395         devices = NULL;
396         if (port) {
397                 if (probe_port(port, serialcomm, &devices) == SR_ERR_MALLOC) {
398                         g_free(port);
399                         if (serialcomm)
400                                 g_free(serialcomm);
401                         return NULL;
402                 }
403         } else {
404                 if (!(dir = g_dir_open("/sys/class/usbmisc/", 0, NULL)))
405                         if (!(dir = g_dir_open("/sys/class/usb/", 0, NULL)))
406                                 return NULL;
407                 while ((dev_name = g_dir_read_name(dir))) {
408                         if (strncmp(dev_name, "usbtmc", 6))
409                                 continue;
410                         port = g_strconcat("/dev/", dev_name, NULL);
411                         ret = probe_port(port, serialcomm, &devices);
412                         g_free(port);
413                         if (serialcomm)
414                                 g_free(serialcomm);
415                         if (ret == SR_ERR_MALLOC) {
416                                 g_dir_close(dir);
417                                 return NULL;
418                         }
419                 }
420                 g_dir_close(dir);
421         }
422
423         /* Tack a copy of the newly found devices onto the driver list. */
424         l = g_slist_copy(devices);
425         drvc->instances = g_slist_concat(drvc->instances, l);
426
427         return devices;
428 }
429
430 static GSList *dev_list(void)
431 {
432         return ((struct drv_context *)(di->priv))->instances;
433 }
434
435 static int dev_open(struct sr_dev_inst *sdi)
436 {
437         struct sr_scpi_dev_inst *scpi = sdi->conn;
438
439         if (sr_scpi_open(scpi) < 0)
440                 return SR_ERR;
441
442         if (rigol_ds_get_dev_cfg(sdi) != SR_OK)
443                 return SR_ERR;
444
445         sdi->status = SR_ST_ACTIVE;
446
447         return SR_OK;
448 }
449
450 static int dev_close(struct sr_dev_inst *sdi)
451 {
452         struct sr_scpi_dev_inst *scpi;
453
454         scpi = sdi->conn;
455
456         if (scpi) {
457                 if (sr_scpi_close(scpi) < 0)
458                         return SR_ERR;
459                 sdi->status = SR_ST_INACTIVE;
460         }
461
462         return SR_OK;
463 }
464
465 static int cleanup(void)
466 {
467         return dev_clear();
468 }
469
470 static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi,
471                 const struct sr_probe_group *probe_group)
472 {
473         struct dev_context *devc;
474         unsigned int i;
475
476         if (!sdi || !(devc = sdi->priv))
477                 return SR_ERR_ARG;
478
479         /* If a probe group is specified, it must be a valid one. */
480         if (probe_group) {
481                 if (probe_group != &devc->analog_groups[0]
482                                 && probe_group != &devc->analog_groups[1]) {
483                         sr_err("Invalid probe group specified.");
484                         return SR_ERR;
485                 }
486         }
487
488         switch (id) {
489         case SR_CONF_NUM_TIMEBASE:
490                 *data = g_variant_new_int32(devc->num_timebases);
491                 break;
492         case SR_CONF_NUM_VDIV:
493                 if (!probe_group) {
494                         sr_err("No probe group specified.");
495                         return SR_ERR_PROBE_GROUP;
496                 }
497                 for (i = 0; i < 2; i++) {
498                         if (probe_group == &devc->analog_groups[i]) {
499                                 *data = g_variant_new_int32(devc->num_vdivs);
500                                 return SR_OK;
501                         }
502                 }
503                 return SR_ERR_NA;
504         case SR_CONF_DATA_SOURCE:
505                 if (devc->data_source == DATA_SOURCE_LIVE)
506                         *data = g_variant_new_string("Live");
507                 else if (devc->data_source == DATA_SOURCE_MEMORY)
508                         *data = g_variant_new_string("Memory");
509                 else
510                         *data = g_variant_new_string("Segmented");
511                 break;
512         default:
513                 return SR_ERR_NA;
514         }
515
516         return SR_OK;
517 }
518
519 static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
520                 const struct sr_probe_group *probe_group)
521 {
522         struct dev_context *devc;
523         uint64_t tmp_u64, p, q;
524         double t_dbl;
525         unsigned int i, j;
526         int ret;
527         const char *tmp_str;
528
529         if (!(devc = sdi->priv))
530                 return SR_ERR_ARG;
531
532         if (sdi->status != SR_ST_ACTIVE)
533                 return SR_ERR_DEV_CLOSED;
534
535         /* If a probe group is specified, it must be a valid one. */
536         if (probe_group) {
537                 if (probe_group != &devc->analog_groups[0]
538                                 && probe_group != &devc->analog_groups[1]) {
539                         sr_err("Invalid probe group specified.");
540                         return SR_ERR;
541                 }
542         }
543
544         ret = SR_OK;
545         switch (id) {
546         case SR_CONF_LIMIT_FRAMES:
547                 devc->limit_frames = g_variant_get_uint64(data);
548                 break;
549         case SR_CONF_TRIGGER_SLOPE:
550                 tmp_u64 = g_variant_get_uint64(data);
551                 if (tmp_u64 != 0 && tmp_u64 != 1)
552                         return SR_ERR;
553                 g_free(devc->trigger_slope);
554                 devc->trigger_slope = g_strdup(tmp_u64 ? "POS" : "NEG");
555                 ret = set_cfg(sdi, ":TRIG:EDGE:SLOP %s", devc->trigger_slope);
556                 break;
557         case SR_CONF_HORIZ_TRIGGERPOS:
558                 t_dbl = g_variant_get_double(data);
559                 if (t_dbl < 0.0 || t_dbl > 1.0)
560                         return SR_ERR;
561                 devc->horiz_triggerpos = t_dbl;
562                 /* We have the trigger offset as a percentage of the frame, but
563                  * need to express this in seconds. */
564                 t_dbl = -(devc->horiz_triggerpos - 0.5) * devc->timebase * devc->num_timebases;
565                 ret = set_cfg(sdi, ":TIM:OFFS %.6f", t_dbl);
566                 break;
567         case SR_CONF_TIMEBASE:
568                 g_variant_get(data, "(tt)", &p, &q);
569                 for (i = 0; i < devc->num_timebases; i++) {
570                         if (devc->timebases[i][0] == p && devc->timebases[i][1] == q) {
571                                 devc->timebase = (float)p / q;
572                                 ret = set_cfg(sdi, ":TIM:SCAL %.9f", devc->timebase);
573                                 break;
574                         }
575                 }
576                 if (i == devc->num_timebases)
577                         ret = SR_ERR_ARG;
578                 break;
579         case SR_CONF_TRIGGER_SOURCE:
580                 tmp_str = g_variant_get_string(data, NULL);
581                 for (i = 0; i < ARRAY_SIZE(trigger_sources); i++) {
582                         if (!strcmp(trigger_sources[i], tmp_str)) {
583                                 g_free(devc->trigger_source);
584                                 devc->trigger_source = g_strdup(trigger_sources[i]);
585                                 if (!strcmp(devc->trigger_source, "AC Line"))
586                                         tmp_str = "ACL";
587                                 else if (!strcmp(devc->trigger_source, "CH1"))
588                                         tmp_str = "CHAN1";
589                                 else if (!strcmp(devc->trigger_source, "CH2"))
590                                         tmp_str = "CHAN2";
591                                 else
592                                         tmp_str = (char *)devc->trigger_source;
593                                 ret = set_cfg(sdi, ":TRIG:EDGE:SOUR %s", tmp_str);
594                                 break;
595                         }
596                 }
597                 if (i == ARRAY_SIZE(trigger_sources))
598                         ret = SR_ERR_ARG;
599                 break;
600         case SR_CONF_VDIV:
601                 if (!probe_group) {
602                         sr_err("No probe group specified.");
603                         return SR_ERR_PROBE_GROUP;
604                 }
605                 g_variant_get(data, "(tt)", &p, &q);
606                 for (i = 0; i < 2; i++) {
607                         if (probe_group == &devc->analog_groups[i]) {
608                                 for (j = 0; j < ARRAY_SIZE(vdivs); j++) {
609                                         if (vdivs[j][0] != p || vdivs[j][1] != q)
610                                                 continue;
611                                         devc->vdiv[i] = (float)p / q;
612                                         return set_cfg(sdi, ":CHAN%d:SCAL %.3f", i + 1,
613                                                         devc->vdiv[i]);
614                                 }
615                                 return SR_ERR_ARG;
616                         }
617                 }
618                 return SR_ERR_NA;
619         case SR_CONF_COUPLING:
620                 if (!probe_group) {
621                         sr_err("No probe group specified.");
622                         return SR_ERR_PROBE_GROUP;
623                 }
624                 tmp_str = g_variant_get_string(data, NULL);
625                 for (i = 0; i < 2; i++) {
626                         if (probe_group == &devc->analog_groups[i]) {
627                                 for (j = 0; j < ARRAY_SIZE(coupling); j++) {
628                                         if (!strcmp(tmp_str, coupling[j])) {
629                                                 g_free(devc->coupling[i]);
630                                                 devc->coupling[i] = g_strdup(coupling[j]);
631                                                 return set_cfg(sdi, ":CHAN%d:COUP %s", i + 1,
632                                                                 devc->coupling[i]);
633                                         }
634                                 }
635                                 return SR_ERR_ARG;
636                         }
637                 }
638                 return SR_ERR_NA;
639         case SR_CONF_DATA_SOURCE:
640                 tmp_str = g_variant_get_string(data, NULL);
641                 if (!strcmp(tmp_str, "Live"))
642                         devc->data_source = DATA_SOURCE_LIVE;
643                 else if (!strcmp(tmp_str, "Memory"))
644                         devc->data_source = DATA_SOURCE_MEMORY;
645                 else if (devc->model->protocol == PROTOCOL_IEEE488_2
646                          && !strcmp(tmp_str, "Segmented"))
647                         devc->data_source = DATA_SOURCE_SEGMENTED;
648                 else
649                         return SR_ERR;
650                 break;
651         default:
652                 ret = SR_ERR_NA;
653                 break;
654         }
655
656         return ret;
657 }
658
659 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
660                 const struct sr_probe_group *probe_group)
661 {
662         GVariant *tuple, *rational[2];
663         GVariantBuilder gvb;
664         unsigned int i;
665         struct dev_context *devc = NULL;
666
667         if (sdi)
668                 devc = sdi->priv;
669
670         if (key == SR_CONF_SCAN_OPTIONS) {
671                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
672                                 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
673                 return SR_OK;
674         } else if (key == SR_CONF_DEVICE_OPTIONS && probe_group == NULL) {
675                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
676                         hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
677                 return SR_OK;
678         }
679
680         /* Every other option requires a valid device instance. */
681         if (!sdi || !(devc = sdi->priv))
682                 return SR_ERR_ARG;
683
684         /* If a probe group is specified, it must be a valid one. */
685         if (probe_group) {
686                 if (probe_group != &devc->analog_groups[0]
687                                 && probe_group != &devc->analog_groups[1]) {
688                         sr_err("Invalid probe group specified.");
689                         return SR_ERR;
690                 }
691         }
692
693         switch (key) {
694                 break;
695         case SR_CONF_DEVICE_OPTIONS:
696                 if (!probe_group) {
697                         sr_err("No probe group specified.");
698                         return SR_ERR_PROBE_GROUP;
699                 }
700                 if (probe_group == &devc->digital_group) {
701                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
702                                 NULL, 0, sizeof(int32_t));
703                         return SR_OK;
704                 } else {
705                         for (i = 0; i < 2; i++) {
706                                 if (probe_group == &devc->analog_groups[i]) {
707                                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
708                                                 analog_hwcaps, ARRAY_SIZE(analog_hwcaps), sizeof(int32_t));
709                                         return SR_OK;
710                                 }
711                         }
712                         return SR_ERR_NA;
713                 }
714                 break;
715         case SR_CONF_COUPLING:
716                 if (!probe_group) {
717                         sr_err("No probe group specified.");
718                         return SR_ERR_PROBE_GROUP;
719                 }
720                 *data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling));
721                 break;
722         case SR_CONF_VDIV:
723                 if (!devc)
724                         /* Can't know this until we have the exact model. */
725                         return SR_ERR_ARG;
726                 if (!probe_group) {
727                         sr_err("No probe group specified.");
728                         return SR_ERR_PROBE_GROUP;
729                 }
730                 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
731                 for (i = 0; i < devc->num_vdivs; i++) {
732                         rational[0] = g_variant_new_uint64(devc->vdivs[i][0]);
733                         rational[1] = g_variant_new_uint64(devc->vdivs[i][1]);
734                         tuple = g_variant_new_tuple(rational, 2);
735                         g_variant_builder_add_value(&gvb, tuple);
736                 }
737                 *data = g_variant_builder_end(&gvb);
738                 break;
739         case SR_CONF_TIMEBASE:
740                 if (!devc)
741                         /* Can't know this until we have the exact model. */
742                         return SR_ERR_ARG;
743                 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
744                 for (i = 0; i < devc->num_timebases; i++) {
745                         rational[0] = g_variant_new_uint64(devc->timebases[i][0]);
746                         rational[1] = g_variant_new_uint64(devc->timebases[i][1]);
747                         tuple = g_variant_new_tuple(rational, 2);
748                         g_variant_builder_add_value(&gvb, tuple);
749                 }
750                 *data = g_variant_builder_end(&gvb);
751                 break;
752         case SR_CONF_TRIGGER_SOURCE:
753                 if (!devc)
754                         /* Can't know this until we have the exact model. */
755                         return SR_ERR_ARG;
756                 *data = g_variant_new_strv(trigger_sources,
757                                 devc->model->has_digital ? ARRAY_SIZE(trigger_sources) : 4);
758                 break;
759         case SR_CONF_DATA_SOURCE:
760                 if (!devc)
761                         /* Can't know this until we have the exact model. */
762                         return SR_ERR_ARG;
763                 /* This needs tweaking by series/model! */
764                 if (devc->model->series == RIGOL_DS2000)
765                         *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources));
766                 else
767                         *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources) - 1);
768                 break;
769         default:
770                 return SR_ERR_NA;
771         }
772
773         return SR_OK;
774 }
775
776 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
777 {
778         struct sr_scpi_dev_inst *scpi;
779         struct dev_context *devc;
780         struct sr_probe *probe;
781         GSList *l;
782         char cmd[256];
783
784         if (sdi->status != SR_ST_ACTIVE)
785                 return SR_ERR_DEV_CLOSED;
786
787         scpi = sdi->conn;
788         devc = sdi->priv;
789
790         if (devc->data_source == DATA_SOURCE_LIVE) {
791                 if (sr_scpi_send(sdi->conn, ":RUN") != SR_OK)
792                         return SR_ERR;
793         } else if (devc->data_source == DATA_SOURCE_MEMORY) {
794                 if (devc->model->series != RIGOL_DS2000) {
795                         sr_err("Data source 'Memory' not supported for this device");
796                         return SR_ERR;
797                 }
798         } else if (devc->data_source == DATA_SOURCE_SEGMENTED) {
799                 sr_err("Data source 'Segmented' not yet supported");
800                 return SR_ERR;
801         }
802
803         for (l = sdi->probes; l; l = l->next) {
804                 probe = l->data;
805                 sr_dbg("handling probe %s", probe->name);
806                 if (probe->type == SR_PROBE_ANALOG) {
807                         if (probe->enabled)
808                                 devc->enabled_analog_probes = g_slist_append(
809                                                 devc->enabled_analog_probes, probe);
810                         if (probe->enabled != devc->analog_channels[probe->index]) {
811                                 /* Enabled channel is currently disabled, or vice versa. */
812                                 sprintf(cmd, ":CHAN%d:DISP %s", probe->index + 1,
813                                                 probe->enabled ? "ON" : "OFF");
814                                 if (sr_scpi_send(sdi->conn, cmd) != SR_OK)
815                                         return SR_ERR;
816                         }
817                 } else if (probe->type == SR_PROBE_LOGIC) {
818                         if (probe->enabled)
819                                 devc->enabled_digital_probes = g_slist_append(
820                                                 devc->enabled_digital_probes, probe);
821                         if (probe->enabled != devc->digital_channels[probe->index]) {
822                                 /* Enabled channel is currently disabled, or vice versa. */
823                                 sprintf(cmd, ":DIG%d:TURN %s", probe->index,
824                                                 probe->enabled ? "ON" : "OFF");
825                                 if (sr_scpi_send(sdi->conn, cmd) != SR_OK)
826                                         return SR_ERR;
827                         }
828                 }
829         }
830         if (!devc->enabled_analog_probes && !devc->enabled_digital_probes)
831                 return SR_ERR;
832
833         sr_scpi_source_add(scpi, G_IO_IN, 50, rigol_ds_receive, (void *)sdi);
834
835         /* Send header packet to the session bus. */
836         std_session_send_df_header(cb_data, LOG_PREFIX);
837
838         if (devc->model->protocol == PROTOCOL_LEGACY) {
839                 /* Fetch the first frame. */
840                 if (devc->enabled_analog_probes) {
841                         devc->analog_frame_size = DS1000_ANALOG_LIVE_WAVEFORM_SIZE;
842                         devc->channel_frame = devc->enabled_analog_probes->data;
843                         if (sr_scpi_send(sdi->conn, ":WAV:DATA? CHAN%d",
844                                         devc->channel_frame->index + 1) != SR_OK)
845                                 return SR_ERR;
846                 } else {
847                         devc->channel_frame = devc->enabled_digital_probes->data;
848                         if (sr_scpi_send(sdi->conn, ":WAV:DATA? DIG") != SR_OK)
849                                 return SR_ERR;
850                 }
851
852                 devc->num_frame_bytes = 0;
853         } else {
854                 if (devc->enabled_analog_probes) {
855                         if (devc->data_source == DATA_SOURCE_MEMORY)
856                         {
857                                 if (g_slist_length(devc->enabled_analog_probes) == 1)
858                                         devc->analog_frame_size = DS2000_ANALOG_MEM_WAVEFORM_SIZE_1C;
859                                 else
860                                         devc->analog_frame_size = DS2000_ANALOG_MEM_WAVEFORM_SIZE_2C;
861                                 /* Apparently for the DS2000 the memory
862                                  * depth can only be set in Running state -
863                                  * this matches the behaviour of the UI. */
864                                 if (sr_scpi_send(sdi->conn, ":RUN") != SR_OK)
865                                         return SR_ERR;
866                                 if (sr_scpi_send(sdi->conn, "ACQ:MDEP %d", devc->analog_frame_size) != SR_OK)
867                                         return SR_ERR;
868                                 if (sr_scpi_send(sdi->conn, ":STOP") != SR_OK)
869                                         return SR_ERR;
870                         } else
871                                 devc->analog_frame_size = DS2000_ANALOG_LIVE_WAVEFORM_SIZE;
872                         devc->channel_frame = devc->enabled_analog_probes->data;
873                         if (rigol_ds_capture_start(sdi) != SR_OK)
874                                 return SR_ERR;
875                 }
876         }
877
878         return SR_OK;
879 }
880
881 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
882 {
883         struct dev_context *devc;
884         struct sr_scpi_dev_inst *scpi;
885
886         (void)cb_data;
887
888         devc = sdi->priv;
889
890         if (sdi->status != SR_ST_ACTIVE) {
891                 sr_err("Device inactive, can't stop acquisition.");
892                 return SR_ERR;
893         }
894
895         g_slist_free(devc->enabled_analog_probes);
896         g_slist_free(devc->enabled_digital_probes);
897         devc->enabled_analog_probes = NULL;
898         devc->enabled_digital_probes = NULL;
899         scpi = sdi->conn;
900         sr_scpi_source_remove(scpi);
901
902         return SR_OK;
903 }
904
905 SR_PRIV struct sr_dev_driver rigol_ds_driver_info = {
906         .name = "rigol-ds",
907         .longname = "Rigol DS",
908         .api_version = 1,
909         .init = init,
910         .cleanup = cleanup,
911         .scan = scan,
912         .dev_list = dev_list,
913         .dev_clear = dev_clear,
914         .config_get = config_get,
915         .config_set = config_set,
916         .config_list = config_list,
917         .dev_open = dev_open,
918         .dev_close = dev_close,
919         .dev_acquisition_start = dev_acquisition_start,
920         .dev_acquisition_stop = dev_acquisition_stop,
921         .priv = NULL,
922 };