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