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