]> sigrok.org Git - libsigrok.git/blob - src/hardware/hameg-hmo/api.c
8a4212af096d7610209f7419de1a36135405dee2
[libsigrok.git] / src / hardware / hameg-hmo / api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2013 poljar (Damir Jelić) <poljarinho@gmail.com>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <config.h>
21 #include <stdlib.h>
22 #include "scpi.h"
23 #include "protocol.h"
24
25 #define SERIALCOMM "115200/8n1/flow=1"
26
27 static struct sr_dev_driver hameg_hmo_driver_info;
28
29 static const char *manufacturers[] = {
30         "HAMEG",
31         "Rohde&Schwarz",
32 };
33
34 static const uint32_t drvopts[] = {
35         SR_CONF_OSCILLOSCOPE,
36 };
37
38 static const uint32_t scanopts[] = {
39         SR_CONF_CONN,
40         SR_CONF_SERIALCOMM,
41 };
42
43 enum {
44         CG_INVALID = -1,
45         CG_NONE,
46         CG_ANALOG,
47         CG_DIGITAL,
48 };
49
50 static int check_manufacturer(const char *manufacturer)
51 {
52         unsigned int i;
53
54         for (i = 0; i < ARRAY_SIZE(manufacturers); i++)
55                 if (!strcmp(manufacturer, manufacturers[i]))
56                         return SR_OK;
57
58         return SR_ERR;
59 }
60
61 static struct sr_dev_inst *hmo_probe_serial_device(struct sr_scpi_dev_inst *scpi)
62 {
63         struct sr_dev_inst *sdi;
64         struct dev_context *devc;
65         struct sr_scpi_hw_info *hw_info;
66
67         sdi = NULL;
68         devc = NULL;
69         hw_info = NULL;
70
71         if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
72                 sr_info("Couldn't get IDN response.");
73                 goto fail;
74         }
75
76         if (check_manufacturer(hw_info->manufacturer) != SR_OK)
77                 goto fail;
78
79         sdi = g_malloc0(sizeof(struct sr_dev_inst));
80         sdi->vendor = g_strdup(hw_info->manufacturer);
81         sdi->model = g_strdup(hw_info->model);
82         sdi->version = g_strdup(hw_info->firmware_version);
83         sdi->serial_num = g_strdup(hw_info->serial_number);
84         sdi->driver = &hameg_hmo_driver_info;
85         sdi->inst_type = SR_INST_SCPI;
86         sdi->conn = scpi;
87
88         sr_scpi_hw_info_free(hw_info);
89         hw_info = NULL;
90
91         devc = g_malloc0(sizeof(struct dev_context));
92
93         sdi->priv = devc;
94
95         if (hmo_init_device(sdi) != SR_OK)
96                 goto fail;
97
98         return sdi;
99
100 fail:
101         sr_scpi_hw_info_free(hw_info);
102         sr_dev_inst_free(sdi);
103         g_free(devc);
104
105         return NULL;
106 }
107
108 static GSList *scan(struct sr_dev_driver *di, GSList *options)
109 {
110         return sr_scpi_scan(di->context, options, hmo_probe_serial_device);
111 }
112
113 static void clear_helper(void *priv)
114 {
115         struct dev_context *devc;
116
117         devc = priv;
118
119         hmo_scope_state_free(devc->model_state);
120
121         g_free(devc->analog_groups);
122         g_free(devc->digital_groups);
123
124         g_free(devc);
125 }
126
127 static int dev_clear(const struct sr_dev_driver *di)
128 {
129         return std_dev_clear(di, clear_helper);
130 }
131
132 static int dev_open(struct sr_dev_inst *sdi)
133 {
134         if (sdi->status != SR_ST_ACTIVE && sr_scpi_open(sdi->conn) != SR_OK)
135                 return SR_ERR;
136
137         if (hmo_scope_state_get(sdi) != SR_OK)
138                 return SR_ERR;
139
140         sdi->status = SR_ST_ACTIVE;
141
142         return SR_OK;
143 }
144
145 static int dev_close(struct sr_dev_inst *sdi)
146 {
147         if (sdi->status == SR_ST_INACTIVE)
148                 return SR_OK;
149
150         sr_scpi_close(sdi->conn);
151
152         sdi->status = SR_ST_INACTIVE;
153
154         return SR_OK;
155 }
156
157 static int check_channel_group(struct dev_context *devc,
158                              const struct sr_channel_group *cg)
159 {
160         unsigned int i;
161         const struct scope_config *model;
162
163         model = devc->model_config;
164
165         if (!cg)
166                 return CG_NONE;
167
168         for (i = 0; i < model->analog_channels; i++)
169                 if (cg == devc->analog_groups[i])
170                         return CG_ANALOG;
171
172         for (i = 0; i < model->digital_pods; i++)
173                 if (cg == devc->digital_groups[i])
174                         return CG_DIGITAL;
175
176         sr_err("Invalid channel group specified.");
177
178         return CG_INVALID;
179 }
180
181 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
182                       const struct sr_channel_group *cg)
183 {
184         int ret, cg_type;
185         unsigned int i;
186         struct dev_context *devc;
187         const struct scope_config *model;
188         struct scope_state *state;
189
190         if (!sdi)
191                 return SR_ERR_ARG;
192
193         devc = sdi->priv;
194
195         if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
196                 return SR_ERR;
197
198         ret = SR_ERR_NA;
199         model = devc->model_config;
200         state = devc->model_state;
201
202         switch (key) {
203         case SR_CONF_NUM_HDIV:
204                 *data = g_variant_new_int32(model->num_xdivs);
205                 ret = SR_OK;
206                 break;
207         case SR_CONF_TIMEBASE:
208                 *data = g_variant_new("(tt)", (*model->timebases)[state->timebase][0],
209                                       (*model->timebases)[state->timebase][1]);
210                 ret = SR_OK;
211                 break;
212         case SR_CONF_NUM_VDIV:
213                 if (cg_type == CG_NONE) {
214                         sr_err("No channel group specified.");
215                         return SR_ERR_CHANNEL_GROUP;
216                 } else if (cg_type == CG_ANALOG) {
217                         for (i = 0; i < model->analog_channels; i++) {
218                                 if (cg != devc->analog_groups[i])
219                                         continue;
220                                 *data = g_variant_new_int32(model->num_ydivs);
221                                 ret = SR_OK;
222                                 break;
223                         }
224
225                 } else {
226                         ret = SR_ERR_NA;
227                 }
228                 break;
229         case SR_CONF_VDIV:
230                 if (cg_type == CG_NONE) {
231                         sr_err("No channel group specified.");
232                         return SR_ERR_CHANNEL_GROUP;
233                 } else if (cg_type == CG_ANALOG) {
234                         for (i = 0; i < model->analog_channels; i++) {
235                                 if (cg != devc->analog_groups[i])
236                                         continue;
237                                 *data = g_variant_new("(tt)",
238                                                       (*model->vdivs)[state->analog_channels[i].vdiv][0],
239                                                       (*model->vdivs)[state->analog_channels[i].vdiv][1]);
240                                 ret = SR_OK;
241                                 break;
242                         }
243
244                 } else {
245                         ret = SR_ERR_NA;
246                 }
247                 break;
248         case SR_CONF_TRIGGER_SOURCE:
249                 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
250                 ret = SR_OK;
251                 break;
252         case SR_CONF_TRIGGER_SLOPE:
253                 *data = g_variant_new_string((*model->trigger_slopes)[state->trigger_slope]);
254                 ret = SR_OK;
255                 break;
256         case SR_CONF_HORIZ_TRIGGERPOS:
257                 *data = g_variant_new_double(state->horiz_triggerpos);
258                 ret = SR_OK;
259                 break;
260         case SR_CONF_COUPLING:
261                 if (cg_type == CG_NONE) {
262                         sr_err("No channel group specified.");
263                         return SR_ERR_CHANNEL_GROUP;
264                 } else if (cg_type == CG_ANALOG) {
265                         for (i = 0; i < model->analog_channels; i++) {
266                                 if (cg != devc->analog_groups[i])
267                                         continue;
268                                 *data = g_variant_new_string((*model->coupling_options)[state->analog_channels[i].coupling]);
269                                 ret = SR_OK;
270                                 break;
271                         }
272
273                 } else {
274                         ret = SR_ERR_NA;
275                 }
276                 break;
277         case SR_CONF_SAMPLERATE:
278                 *data = g_variant_new_uint64(state->sample_rate);
279                 ret = SR_OK;
280                 break;
281         default:
282                 ret = SR_ERR_NA;
283         }
284
285         return ret;
286 }
287
288 static GVariant *build_tuples(const uint64_t (*array)[][2], unsigned int n)
289 {
290         unsigned int i;
291         GVariant *rational[2];
292         GVariantBuilder gvb;
293
294         g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
295
296         for (i = 0; i < n; i++) {
297                 rational[0] = g_variant_new_uint64((*array)[i][0]);
298                 rational[1] = g_variant_new_uint64((*array)[i][1]);
299
300                 /* FIXME: Valgrind reports a memory leak here. */
301                 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
302         }
303
304         return g_variant_builder_end(&gvb);
305 }
306
307 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
308                       const struct sr_channel_group *cg)
309 {
310         int ret, cg_type;
311         unsigned int i, j;
312         char command[MAX_COMMAND_SIZE], float_str[30];
313         struct dev_context *devc;
314         const struct scope_config *model;
315         struct scope_state *state;
316         const char *tmp;
317         uint64_t p, q;
318         double tmp_d;
319         gboolean update_sample_rate;
320
321         if (!sdi)
322                 return SR_ERR_ARG;
323
324         devc = sdi->priv;
325
326         if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
327                 return SR_ERR;
328
329         model = devc->model_config;
330         state = devc->model_state;
331         update_sample_rate = FALSE;
332
333         ret = SR_ERR_NA;
334
335         switch (key) {
336         case SR_CONF_LIMIT_FRAMES:
337                 devc->frame_limit = g_variant_get_uint64(data);
338                 ret = SR_OK;
339                 break;
340         case SR_CONF_TRIGGER_SOURCE:
341                 tmp = g_variant_get_string(data, NULL);
342                 for (i = 0; (*model->trigger_sources)[i]; i++) {
343                         if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
344                                 continue;
345                         state->trigger_source = i;
346                         g_snprintf(command, sizeof(command),
347                                    (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE],
348                                    (*model->trigger_sources)[i]);
349
350                         ret = sr_scpi_send(sdi->conn, command);
351                         break;
352                 }
353                 break;
354         case SR_CONF_VDIV:
355                 if (cg_type == CG_NONE) {
356                         sr_err("No channel group specified.");
357                         return SR_ERR_CHANNEL_GROUP;
358                 }
359
360                 g_variant_get(data, "(tt)", &p, &q);
361
362                 for (i = 0; i < model->num_vdivs; i++) {
363                         if (p != (*model->vdivs)[i][0] ||
364                             q != (*model->vdivs)[i][1])
365                                 continue;
366                         for (j = 1; j <= model->analog_channels; j++) {
367                                 if (cg != devc->analog_groups[j - 1])
368                                         continue;
369                                 state->analog_channels[j - 1].vdiv = i;
370                                 g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q);
371                                 g_snprintf(command, sizeof(command),
372                                            (*model->scpi_dialect)[SCPI_CMD_SET_VERTICAL_DIV],
373                                            j, float_str);
374
375                                 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
376                                     sr_scpi_get_opc(sdi->conn) != SR_OK)
377                                         return SR_ERR;
378
379                                 break;
380                         }
381
382                         ret = SR_OK;
383                         break;
384                 }
385                 break;
386         case SR_CONF_TIMEBASE:
387                 g_variant_get(data, "(tt)", &p, &q);
388
389                 for (i = 0; i < model->num_timebases; i++) {
390                         if (p != (*model->timebases)[i][0] ||
391                             q != (*model->timebases)[i][1])
392                                 continue;
393                         state->timebase = i;
394                         g_ascii_formatd(float_str, sizeof(float_str), "%E", (float) p / q);
395                         g_snprintf(command, sizeof(command),
396                                    (*model->scpi_dialect)[SCPI_CMD_SET_TIMEBASE],
397                                    float_str);
398
399                         ret = sr_scpi_send(sdi->conn, command);
400                         update_sample_rate = TRUE;
401                         break;
402                 }
403                 break;
404         case SR_CONF_HORIZ_TRIGGERPOS:
405                 tmp_d = g_variant_get_double(data);
406
407                 if (tmp_d < 0.0 || tmp_d > 1.0)
408                         return SR_ERR;
409
410                 state->horiz_triggerpos = tmp_d;
411                 tmp_d = -(tmp_d - 0.5) *
412                         ((double) (*model->timebases)[state->timebase][0] /
413                         (*model->timebases)[state->timebase][1])
414                          * model->num_xdivs;
415
416                 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
417                 g_snprintf(command, sizeof(command),
418                            (*model->scpi_dialect)[SCPI_CMD_SET_HORIZ_TRIGGERPOS],
419                            float_str);
420
421                 ret = sr_scpi_send(sdi->conn, command);
422                 break;
423         case SR_CONF_TRIGGER_SLOPE:
424                 tmp = g_variant_get_string(data, NULL);
425                 for (i = 0; (*model->trigger_slopes)[i]; i++) {
426                         if (g_strcmp0(tmp, (*model->trigger_slopes)[i]) != 0)
427                                 continue;
428                         state->trigger_slope = i;
429                         g_snprintf(command, sizeof(command),
430                                    (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE],
431                                    (*model->trigger_slopes)[i]);
432
433                         ret = sr_scpi_send(sdi->conn, command);
434                         break;
435                 }
436                 break;
437         case SR_CONF_COUPLING:
438                 if (cg_type == CG_NONE) {
439                         sr_err("No channel group specified.");
440                         return SR_ERR_CHANNEL_GROUP;
441                 }
442
443                 tmp = g_variant_get_string(data, NULL);
444
445                 for (i = 0; (*model->coupling_options)[i]; i++) {
446                         if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
447                                 continue;
448                         for (j = 1; j <= model->analog_channels; j++) {
449                                 if (cg != devc->analog_groups[j - 1])
450                                         continue;
451                                 state->analog_channels[j-1].coupling = i;
452
453                                 g_snprintf(command, sizeof(command),
454                                            (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING],
455                                            j, tmp);
456
457                                 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
458                                     sr_scpi_get_opc(sdi->conn) != SR_OK)
459                                         return SR_ERR;
460                                 break;
461                         }
462
463                         ret = SR_OK;
464                         break;
465                 }
466                 break;
467         default:
468                 ret = SR_ERR_NA;
469                 break;
470         }
471
472         if (ret == SR_OK)
473                 ret = sr_scpi_get_opc(sdi->conn);
474
475         if (ret == SR_OK && update_sample_rate)
476                 ret = hmo_update_sample_rate(sdi);
477
478         return ret;
479 }
480
481 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
482                        const struct sr_channel_group *cg)
483 {
484         int cg_type = CG_NONE;
485         struct dev_context *devc = NULL;
486         const struct scope_config *model = NULL;
487
488         if (sdi) {
489                 devc = sdi->priv;
490                 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
491                         return SR_ERR;
492
493                 model = devc->model_config;
494         }
495
496         switch (key) {
497         case SR_CONF_SCAN_OPTIONS:
498                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
499                                 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
500                 break;
501         case SR_CONF_DEVICE_OPTIONS:
502                 if (cg_type == CG_NONE) {
503                         if (model)
504                                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
505                                         model->devopts, model->num_devopts, sizeof(uint32_t));
506                         else
507                                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
508                                         drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
509                 } else if (cg_type == CG_ANALOG) {
510                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
511                                 model->analog_devopts, model->num_analog_devopts,
512                                 sizeof(uint32_t));
513                 } else {
514                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
515                                 NULL, 0, sizeof(uint32_t));
516                 }
517                 break;
518         case SR_CONF_COUPLING:
519                 if (cg_type == CG_NONE)
520                         return SR_ERR_CHANNEL_GROUP;
521                 *data = g_variant_new_strv(*model->coupling_options,
522                            g_strv_length((char **)*model->coupling_options));
523                 break;
524         case SR_CONF_TRIGGER_SOURCE:
525                 if (!model)
526                         return SR_ERR_ARG;
527                 *data = g_variant_new_strv(*model->trigger_sources,
528                            g_strv_length((char **)*model->trigger_sources));
529                 break;
530         case SR_CONF_TRIGGER_SLOPE:
531                 if (!model)
532                         return SR_ERR_ARG;
533                 *data = g_variant_new_strv(*model->trigger_slopes,
534                            g_strv_length((char **)*model->trigger_slopes));
535                 break;
536         case SR_CONF_TIMEBASE:
537                 if (!model)
538                         return SR_ERR_ARG;
539                 *data = build_tuples(model->timebases, model->num_timebases);
540                 break;
541         case SR_CONF_VDIV:
542                 if (cg_type == CG_NONE)
543                         return SR_ERR_CHANNEL_GROUP;
544                 *data = build_tuples(model->vdivs, model->num_vdivs);
545                 break;
546         default:
547                 return SR_ERR_NA;
548         }
549
550         return SR_OK;
551 }
552
553 SR_PRIV int hmo_request_data(const struct sr_dev_inst *sdi)
554 {
555         char command[MAX_COMMAND_SIZE];
556         struct sr_channel *ch;
557         struct dev_context *devc;
558         const struct scope_config *model;
559
560         devc = sdi->priv;
561         model = devc->model_config;
562
563         ch = devc->current_channel->data;
564
565         switch (ch->type) {
566         case SR_CHANNEL_ANALOG:
567                 g_snprintf(command, sizeof(command),
568                            (*model->scpi_dialect)[SCPI_CMD_GET_ANALOG_DATA],
569 #ifdef WORDS_BIGENDIAN
570                            "MSBF",
571 #else
572                            "LSBF",
573 #endif
574                            ch->index + 1);
575                 break;
576         case SR_CHANNEL_LOGIC:
577                 g_snprintf(command, sizeof(command),
578                            (*model->scpi_dialect)[SCPI_CMD_GET_DIG_DATA],
579                            ch->index < 8 ? 1 : 2);
580                 break;
581         default:
582                 sr_err("Invalid channel type.");
583                 break;
584         }
585
586         return sr_scpi_send(sdi->conn, command);
587 }
588
589 static int hmo_check_channels(GSList *channels)
590 {
591         GSList *l;
592         struct sr_channel *ch;
593         gboolean enabled_chan[MAX_ANALOG_CHANNEL_COUNT];
594         gboolean enabled_pod[MAX_DIGITAL_GROUP_COUNT];
595         size_t idx;
596
597         /* Preset "not enabled" for all channels / pods. */
598         for (idx = 0; idx < ARRAY_SIZE(enabled_chan); idx++)
599                 enabled_chan[idx] = FALSE;
600         for (idx = 0; idx < ARRAY_SIZE(enabled_pod); idx++)
601                 enabled_pod[idx] = FALSE;
602
603         /*
604          * Determine which channels / pods are required for the caller's
605          * specified configuration.
606          */
607         for (l = channels; l; l = l->next) {
608                 ch = l->data;
609                 switch (ch->type) {
610                 case SR_CHANNEL_ANALOG:
611                         idx = ch->index;
612                         if (idx < ARRAY_SIZE(enabled_chan))
613                                 enabled_chan[idx] = TRUE;
614                         break;
615                 case SR_CHANNEL_LOGIC:
616                         idx = ch->index / 8;
617                         if (idx < ARRAY_SIZE(enabled_pod))
618                                 enabled_pod[idx] = TRUE;
619                         break;
620                 default:
621                         return SR_ERR;
622                 }
623         }
624
625         /*
626          * Check for resource conflicts. Some channels can be either
627          * analog or digital, but never both at the same time.
628          *
629          * Note that the constraints might depend on the specific model.
630          * These tests might need some adjustment when support for more
631          * models gets added to the driver.
632          */
633         if (enabled_pod[0] && enabled_chan[2])
634                 return SR_ERR;
635         if (enabled_pod[1] && enabled_chan[3])
636                 return SR_ERR;
637         return SR_OK;
638 }
639
640 static int hmo_setup_channels(const struct sr_dev_inst *sdi)
641 {
642         GSList *l;
643         unsigned int i;
644         gboolean *pod_enabled, setup_changed;
645         char command[MAX_COMMAND_SIZE];
646         struct scope_state *state;
647         const struct scope_config *model;
648         struct sr_channel *ch;
649         struct dev_context *devc;
650         struct sr_scpi_dev_inst *scpi;
651
652         devc = sdi->priv;
653         scpi = sdi->conn;
654         state = devc->model_state;
655         model = devc->model_config;
656         setup_changed = FALSE;
657
658         pod_enabled = g_try_malloc0(sizeof(gboolean) * model->digital_pods);
659
660         for (l = sdi->channels; l; l = l->next) {
661                 ch = l->data;
662                 switch (ch->type) {
663                 case SR_CHANNEL_ANALOG:
664                         if (ch->enabled == state->analog_channels[ch->index].state)
665                                 break;
666                         g_snprintf(command, sizeof(command),
667                                    (*model->scpi_dialect)[SCPI_CMD_SET_ANALOG_CHAN_STATE],
668                                    ch->index + 1, ch->enabled);
669
670                         if (sr_scpi_send(scpi, command) != SR_OK)
671                                 return SR_ERR;
672                         state->analog_channels[ch->index].state = ch->enabled;
673                         setup_changed = TRUE;
674                         break;
675                 case SR_CHANNEL_LOGIC:
676                         /*
677                          * A digital POD needs to be enabled for every group of
678                          * 8 channels.
679                          */
680                         if (ch->enabled)
681                                 pod_enabled[ch->index < 8 ? 0 : 1] = TRUE;
682
683                         if (ch->enabled == state->digital_channels[ch->index])
684                                 break;
685                         g_snprintf(command, sizeof(command),
686                                    (*model->scpi_dialect)[SCPI_CMD_SET_DIG_CHAN_STATE],
687                                    ch->index, ch->enabled);
688
689                         if (sr_scpi_send(scpi, command) != SR_OK)
690                                 return SR_ERR;
691
692                         state->digital_channels[ch->index] = ch->enabled;
693                         setup_changed = TRUE;
694                         break;
695                 default:
696                         return SR_ERR;
697                 }
698         }
699
700         for (i = 1; i <= model->digital_pods; i++) {
701                 if (state->digital_pods[i - 1] == pod_enabled[i - 1])
702                         continue;
703                 g_snprintf(command, sizeof(command),
704                            (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_STATE],
705                            i, pod_enabled[i - 1]);
706                 if (sr_scpi_send(scpi, command) != SR_OK)
707                         return SR_ERR;
708                 state->digital_pods[i - 1] = pod_enabled[i - 1];
709                 setup_changed = TRUE;
710         }
711
712         g_free(pod_enabled);
713
714         if (setup_changed && hmo_update_sample_rate(sdi) != SR_OK)
715                 return SR_ERR;
716
717         return SR_OK;
718 }
719
720 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
721 {
722         GSList *l;
723         gboolean digital_added[MAX_DIGITAL_GROUP_COUNT];
724         size_t group, pod_count;
725         struct sr_channel *ch;
726         struct dev_context *devc;
727         struct sr_scpi_dev_inst *scpi;
728         int ret;
729
730         if (sdi->status != SR_ST_ACTIVE)
731                 return SR_ERR_DEV_CLOSED;
732
733         scpi = sdi->conn;
734         devc = sdi->priv;
735
736         /* Preset empty results. */
737         for (group = 0; group < ARRAY_SIZE(digital_added); group++)
738                 digital_added[group] = FALSE;
739         g_slist_free(devc->enabled_channels);
740         devc->enabled_channels = NULL;
741
742         /*
743          * Contruct the list of enabled channels. Determine the highest
744          * number of digital pods involved in the acquisition.
745          */
746         pod_count = 0;
747         for (l = sdi->channels; l; l = l->next) {
748                 ch = l->data;
749                 if (!ch->enabled)
750                         continue;
751                 /* Only add a single digital channel per group (pod). */
752                 group = ch->index / 8;
753                 if (ch->type != SR_CHANNEL_LOGIC || !digital_added[group]) {
754                         devc->enabled_channels = g_slist_append(
755                                         devc->enabled_channels, ch);
756                         if (ch->type == SR_CHANNEL_LOGIC) {
757                                 digital_added[group] = TRUE;
758                                 if (pod_count < group + 1)
759                                         pod_count = group + 1;
760                         }
761                 }
762         }
763         if (!devc->enabled_channels)
764                 return SR_ERR;
765         devc->pod_count = pod_count;
766         devc->logic_data = NULL;
767
768         /*
769          * Check constraints. Some channels can be either analog or
770          * digital, but not both at the same time.
771          */
772         if (hmo_check_channels(devc->enabled_channels) != SR_OK) {
773                 sr_err("Invalid channel configuration specified!");
774                 ret = SR_ERR_NA;
775                 goto free_enabled;
776         }
777
778         /*
779          * Configure the analog and digital channels and the
780          * corresponding digital pods.
781          */
782         if (hmo_setup_channels(sdi) != SR_OK) {
783                 sr_err("Failed to setup channel configuration!");
784                 ret = SR_ERR;
785                 goto free_enabled;
786         }
787
788         /*
789          * Start acquisition on the first enabled channel. The
790          * receive routine will continue driving the acquisition.
791          */
792         sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
793                         hmo_receive_data, (void *)sdi);
794
795         std_session_send_df_header(sdi);
796
797         devc->current_channel = devc->enabled_channels;
798
799         return hmo_request_data(sdi);
800
801 free_enabled:
802         g_slist_free(devc->enabled_channels);
803         devc->enabled_channels = NULL;
804         return ret;
805 }
806
807 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
808 {
809         struct dev_context *devc;
810         struct sr_scpi_dev_inst *scpi;
811
812         std_session_send_df_end(sdi);
813
814         devc = sdi->priv;
815
816         devc->num_frames = 0;
817         g_slist_free(devc->enabled_channels);
818         devc->enabled_channels = NULL;
819         scpi = sdi->conn;
820         sr_scpi_source_remove(sdi->session, scpi);
821
822         return SR_OK;
823 }
824
825 static struct sr_dev_driver hameg_hmo_driver_info = {
826         .name = "hameg-hmo",
827         .longname = "Hameg HMO",
828         .api_version = 1,
829         .init = std_init,
830         .cleanup = std_cleanup,
831         .scan = scan,
832         .dev_list = std_dev_list,
833         .dev_clear = dev_clear,
834         .config_get = config_get,
835         .config_set = config_set,
836         .config_list = config_list,
837         .dev_open = dev_open,
838         .dev_close = dev_close,
839         .dev_acquisition_start = dev_acquisition_start,
840         .dev_acquisition_stop = dev_acquisition_stop,
841         .context = NULL,
842 };
843 SR_REGISTER_DEV_DRIVER(hameg_hmo_driver_info);