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