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