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