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