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