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