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