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