]> sigrok.org Git - libsigrok.git/blob - src/hardware/hameg-hmo/api.c
hameg-hmo: Add RTB2000 and RTM3000 MSO support (untested).
[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, i;
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                 /* Check if the oscilloscope is currently in custom threshold mode. */
243                 for (i = 0; i < model->num_logic_threshold; i++) {
244                         if (!strcmp("USER2", (*model->logic_threshold)[i]))
245                                 if (strcmp("USER2", (*model->logic_threshold)[state->digital_pods[idx].threshold]))
246                                         return SR_ERR_NA;
247                         if (!strcmp("USER", (*model->logic_threshold)[i]))
248                                 if (strcmp("USER", (*model->logic_threshold)[state->digital_pods[idx].threshold]))
249                                         return SR_ERR_NA;
250                         if (!strcmp("MAN", (*model->logic_threshold)[i]))
251                                 if (strcmp("MAN", (*model->logic_threshold)[state->digital_pods[idx].threshold]))
252                                         return SR_ERR_NA;
253                 }
254                 *data = g_variant_new_double(state->digital_pods[idx].user_threshold);
255                 break;
256         default:
257                 return SR_ERR_NA;
258         }
259
260         return SR_OK;
261 }
262
263 static int config_set(uint32_t key, GVariant *data,
264         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
265 {
266         int ret, cg_type, idx, i, j;
267         char command[MAX_COMMAND_SIZE], command2[MAX_COMMAND_SIZE];
268         char float_str[30], *tmp_str;
269         struct dev_context *devc;
270         const struct scope_config *model;
271         struct scope_state *state;
272         double tmp_d, tmp_d2;
273         gboolean update_sample_rate;
274
275         if (!sdi)
276                 return SR_ERR_ARG;
277
278         devc = sdi->priv;
279
280         if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
281                 return SR_ERR;
282
283         model = devc->model_config;
284         state = devc->model_state;
285         update_sample_rate = FALSE;
286
287         switch (key) {
288         case SR_CONF_LIMIT_SAMPLES:
289                 devc->samples_limit = g_variant_get_uint64(data);
290                 ret = SR_OK;
291                 break;
292         case SR_CONF_LIMIT_FRAMES:
293                 devc->frame_limit = g_variant_get_uint64(data);
294                 ret = SR_OK;
295                 break;
296         case SR_CONF_TRIGGER_SOURCE:
297                 if ((idx = std_str_idx(data, *model->trigger_sources, model->num_trigger_sources)) < 0)
298                         return SR_ERR_ARG;
299                 g_snprintf(command, sizeof(command),
300                            (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE],
301                            (*model->trigger_sources)[idx]);
302                 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
303                     sr_scpi_get_opc(sdi->conn) != SR_OK)
304                         return SR_ERR;
305                 state->trigger_source = idx;
306                 ret = SR_OK;
307                 break;
308         case SR_CONF_VDIV:
309                 if (!cg)
310                         return SR_ERR_CHANNEL_GROUP;
311                 if ((idx = std_u64_tuple_idx(data, *model->vdivs, model->num_vdivs)) < 0)
312                         return SR_ERR_ARG;
313                 if ((j = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
314                         return SR_ERR_ARG;
315                 g_ascii_formatd(float_str, sizeof(float_str), "%E",
316                         (float) (*model->vdivs)[idx][0] / (*model->vdivs)[idx][1]);
317                 g_snprintf(command, sizeof(command),
318                            (*model->scpi_dialect)[SCPI_CMD_SET_VERTICAL_DIV],
319                            j + 1, float_str);
320                 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
321                     sr_scpi_get_opc(sdi->conn) != SR_OK)
322                         return SR_ERR;
323                 state->analog_channels[j].vdiv = idx;
324                 ret = SR_OK;
325                 break;
326         case SR_CONF_TIMEBASE:
327                 if ((idx = std_u64_tuple_idx(data, *model->timebases, model->num_timebases)) < 0)
328                         return SR_ERR_ARG;
329                 g_ascii_formatd(float_str, sizeof(float_str), "%E",
330                         (float) (*model->timebases)[idx][0] / (*model->timebases)[idx][1]);
331                 g_snprintf(command, sizeof(command),
332                            (*model->scpi_dialect)[SCPI_CMD_SET_TIMEBASE],
333                            float_str);
334                 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
335                     sr_scpi_get_opc(sdi->conn) != SR_OK)
336                         return SR_ERR;
337                 state->timebase = idx;
338                 ret = SR_OK;
339                 update_sample_rate = TRUE;
340                 break;
341         case SR_CONF_HORIZ_TRIGGERPOS:
342                 tmp_d = g_variant_get_double(data);
343                 if (tmp_d < 0.0 || tmp_d > 1.0)
344                         return SR_ERR;
345                 tmp_d2 = -(tmp_d - 0.5) *
346                         ((double) (*model->timebases)[state->timebase][0] /
347                         (*model->timebases)[state->timebase][1])
348                          * model->num_xdivs;
349                 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d2);
350                 g_snprintf(command, sizeof(command),
351                            (*model->scpi_dialect)[SCPI_CMD_SET_HORIZ_TRIGGERPOS],
352                            float_str);
353                 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
354                     sr_scpi_get_opc(sdi->conn) != SR_OK)
355                         return SR_ERR;
356                 state->horiz_triggerpos = tmp_d;
357                 ret = SR_OK;
358                 break;
359         case SR_CONF_TRIGGER_SLOPE:
360                 if ((idx = std_str_idx(data, *model->trigger_slopes, model->num_trigger_slopes)) < 0)
361                         return SR_ERR_ARG;
362                 g_snprintf(command, sizeof(command),
363                            (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE],
364                            (*model->trigger_slopes)[idx]);
365                 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
366                     sr_scpi_get_opc(sdi->conn) != SR_OK)
367                         return SR_ERR;
368                 state->trigger_slope = idx;
369                 ret = SR_OK;
370                 break;
371         case SR_CONF_TRIGGER_PATTERN:
372                 tmp_str = (char *)g_variant_get_string(data, 0);
373                 idx = strlen(tmp_str);
374                 if (idx == 0 || idx > model->analog_channels + model->digital_channels)
375                         return SR_ERR_ARG;
376                 g_snprintf(command, sizeof(command),
377                            (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_PATTERN],
378                            tmp_str);
379                 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
380                     sr_scpi_get_opc(sdi->conn) != SR_OK)
381                         return SR_ERR;
382                 g_free(state->trigger_pattern);
383                 state->trigger_pattern = g_strdup(tmp_str);
384                 ret = SR_OK;
385                 break;
386         case SR_CONF_COUPLING:
387                 if (!cg)
388                         return SR_ERR_CHANNEL_GROUP;
389                 if ((idx = std_str_idx(data, *model->coupling_options, model->num_coupling_options)) < 0)
390                         return SR_ERR_ARG;
391                 if ((j = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
392                         return SR_ERR_ARG;
393                 g_snprintf(command, sizeof(command),
394                            (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING],
395                            j + 1, (*model->coupling_options)[idx]);
396                 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
397                     sr_scpi_get_opc(sdi->conn) != SR_OK)
398                         return SR_ERR;
399                 state->analog_channels[j].coupling = idx;
400                 ret = SR_OK;
401                 break;
402         case SR_CONF_LOGIC_THRESHOLD:
403                 if (!cg)
404                         return SR_ERR_CHANNEL_GROUP;
405                 if (cg_type != CG_DIGITAL)
406                         return SR_ERR_NA;
407                 if (!model)
408                         return SR_ERR_ARG;
409                 if ((idx = std_str_idx(data, *model->logic_threshold, model->num_logic_threshold)) < 0)
410                         return SR_ERR_ARG;
411                 if ((j = std_cg_idx(cg, devc->digital_groups, model->digital_pods)) < 0)
412                         return SR_ERR_ARG;
413                 /* Check if the threshold command is based on the POD or digital channel index. */
414                 if (model->logic_threshold_for_pod)
415                         i = j + 1;
416                 else
417                         i = j * 8;
418                 g_snprintf(command, sizeof(command),
419                            (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_THRESHOLD],
420                            i, (*model->logic_threshold)[idx]);
421                 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
422                     sr_scpi_get_opc(sdi->conn) != SR_OK)
423                         return SR_ERR;
424                 state->digital_pods[j].threshold = idx;
425                 ret = SR_OK;
426                 break;
427         case SR_CONF_LOGIC_THRESHOLD_CUSTOM:
428                 if (!cg)
429                         return SR_ERR_CHANNEL_GROUP;
430                 if (cg_type != CG_DIGITAL)
431                         return SR_ERR_NA;
432                 if (!model)
433                         return SR_ERR_ARG;
434                 if ((j = std_cg_idx(cg, devc->digital_groups, model->digital_pods)) < 0)
435                         return SR_ERR_ARG;
436                 tmp_d = g_variant_get_double(data);
437                 if (tmp_d < -2.0 || tmp_d > 8.0)
438                         return SR_ERR;
439                 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
440                 /* Check if the threshold command is based on the POD or digital channel index. */
441                 if (model->logic_threshold_for_pod)
442                         idx = j + 1;
443                 else
444                         idx = j * 8;
445                 /* Try to support different dialects exhaustively. */
446                 for (i = 0; i < model->num_logic_threshold; i++) {
447                         if (!strcmp("USER2", (*model->logic_threshold)[i])) {
448                                 g_snprintf(command, sizeof(command),
449                                            (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_USER_THRESHOLD],
450                                            idx, 2, float_str); /* USER2 */
451                                 g_snprintf(command2, sizeof(command2),
452                                            (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_THRESHOLD],
453                                            idx, "USER2");
454                                 break;
455                         }
456                         if (!strcmp("USER", (*model->logic_threshold)[i])) {
457                                 g_snprintf(command, sizeof(command),
458                                            (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_USER_THRESHOLD],
459                                            idx, float_str);
460                                 g_snprintf(command2, sizeof(command2),
461                                            (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_THRESHOLD],
462                                            idx, "USER");
463                                 break;
464                         }
465                         if (!strcmp("MAN", (*model->logic_threshold)[i])) {
466                                 g_snprintf(command, sizeof(command),
467                                            (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_USER_THRESHOLD],
468                                            idx, float_str);
469                                 g_snprintf(command2, sizeof(command2),
470                                            (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_THRESHOLD],
471                                            idx, "MAN");
472                                 break;
473                         }
474                 }
475                 if (sr_scpi_send(sdi->conn, command) != SR_OK ||
476                     sr_scpi_get_opc(sdi->conn) != SR_OK)
477                         return SR_ERR;
478                 if (sr_scpi_send(sdi->conn, command2) != SR_OK ||
479                     sr_scpi_get_opc(sdi->conn) != SR_OK)
480                         return SR_ERR;
481                 state->digital_pods[j].user_threshold = tmp_d;
482                 ret = SR_OK;
483                 break;
484         default:
485                 ret = SR_ERR_NA;
486                 break;
487         }
488
489         if (ret == SR_OK && update_sample_rate)
490                 ret = hmo_update_sample_rate(sdi);
491
492         return ret;
493 }
494
495 static int config_list(uint32_t key, GVariant **data,
496         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
497 {
498         int cg_type = CG_NONE;
499         struct dev_context *devc = NULL;
500         const struct scope_config *model = NULL;
501
502         if (sdi) {
503                 devc = sdi->priv;
504                 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
505                         return SR_ERR;
506
507                 model = devc->model_config;
508         }
509
510         switch (key) {
511         case SR_CONF_SCAN_OPTIONS:
512                 *data = std_gvar_array_u32(ARRAY_AND_SIZE(scanopts));
513                 break;
514         case SR_CONF_DEVICE_OPTIONS:
515                 if (!cg) {
516                         if (model)
517                                 *data = std_gvar_array_u32(*model->devopts, model->num_devopts);
518                         else
519                                 *data = std_gvar_array_u32(ARRAY_AND_SIZE(drvopts));
520                 } else if (cg_type == CG_ANALOG) {
521                         *data = std_gvar_array_u32(*model->devopts_cg_analog, model->num_devopts_cg_analog);
522                 } else if (cg_type == CG_DIGITAL) {
523                         *data = std_gvar_array_u32(*model->devopts_cg_digital, model->num_devopts_cg_digital);
524                 } else {
525                         *data = std_gvar_array_u32(NULL, 0);
526                 }
527                 break;
528         case SR_CONF_COUPLING:
529                 if (!cg)
530                         return SR_ERR_CHANNEL_GROUP;
531                 if (!model)
532                         return SR_ERR_ARG;
533                 *data = g_variant_new_strv(*model->coupling_options, model->num_coupling_options);
534                 break;
535         case SR_CONF_TRIGGER_SOURCE:
536                 if (!model)
537                         return SR_ERR_ARG;
538                 *data = g_variant_new_strv(*model->trigger_sources, model->num_trigger_sources);
539                 break;
540         case SR_CONF_TRIGGER_SLOPE:
541                 if (!model)
542                         return SR_ERR_ARG;
543                 *data = g_variant_new_strv(*model->trigger_slopes, model->num_trigger_slopes);
544                 break;
545         case SR_CONF_TIMEBASE:
546                 if (!model)
547                         return SR_ERR_ARG;
548                 *data = std_gvar_tuple_array(*model->timebases, model->num_timebases);
549                 break;
550         case SR_CONF_VDIV:
551                 if (!cg)
552                         return SR_ERR_CHANNEL_GROUP;
553                 if (!model)
554                         return SR_ERR_ARG;
555                 *data = std_gvar_tuple_array(*model->vdivs, model->num_vdivs);
556                 break;
557         case SR_CONF_LOGIC_THRESHOLD:
558                 if (!cg)
559                         return SR_ERR_CHANNEL_GROUP;
560                 if (!model)
561                         return SR_ERR_ARG;
562                 *data = g_variant_new_strv(*model->logic_threshold, model->num_logic_threshold);
563                 break;
564         default:
565                 return SR_ERR_NA;
566         }
567
568         return SR_OK;
569 }
570
571 SR_PRIV int hmo_request_data(const struct sr_dev_inst *sdi)
572 {
573         char command[MAX_COMMAND_SIZE];
574         struct sr_channel *ch;
575         struct dev_context *devc;
576         const struct scope_config *model;
577
578         devc = sdi->priv;
579         model = devc->model_config;
580
581         ch = devc->current_channel->data;
582
583         switch (ch->type) {
584         case SR_CHANNEL_ANALOG:
585                 g_snprintf(command, sizeof(command),
586                            (*model->scpi_dialect)[SCPI_CMD_GET_ANALOG_DATA],
587 #ifdef WORDS_BIGENDIAN
588                            "MSBF",
589 #else
590                            "LSBF",
591 #endif
592                            ch->index + 1);
593                 break;
594         case SR_CHANNEL_LOGIC:
595                 g_snprintf(command, sizeof(command),
596                            (*model->scpi_dialect)[SCPI_CMD_GET_DIG_DATA],
597                            ch->index < 8 ? 1 : 2);
598                 break;
599         default:
600                 sr_err("Invalid channel type.");
601                 break;
602         }
603
604         return sr_scpi_send(sdi->conn, command);
605 }
606
607 static int hmo_check_channels(GSList *channels)
608 {
609         GSList *l;
610         struct sr_channel *ch;
611         gboolean enabled_chan[MAX_ANALOG_CHANNEL_COUNT];
612         gboolean enabled_pod[MAX_DIGITAL_GROUP_COUNT];
613         size_t idx;
614
615         /* Preset "not enabled" for all channels / pods. */
616         for (idx = 0; idx < ARRAY_SIZE(enabled_chan); idx++)
617                 enabled_chan[idx] = FALSE;
618         for (idx = 0; idx < ARRAY_SIZE(enabled_pod); idx++)
619                 enabled_pod[idx] = FALSE;
620
621         /*
622          * Determine which channels / pods are required for the caller's
623          * specified configuration.
624          */
625         for (l = channels; l; l = l->next) {
626                 ch = l->data;
627                 switch (ch->type) {
628                 case SR_CHANNEL_ANALOG:
629                         idx = ch->index;
630                         if (idx < ARRAY_SIZE(enabled_chan))
631                                 enabled_chan[idx] = TRUE;
632                         break;
633                 case SR_CHANNEL_LOGIC:
634                         idx = ch->index / 8;
635                         if (idx < ARRAY_SIZE(enabled_pod))
636                                 enabled_pod[idx] = TRUE;
637                         break;
638                 default:
639                         return SR_ERR;
640                 }
641         }
642
643         /*
644          * Check for resource conflicts. Some channels can be either
645          * analog or digital, but never both at the same time.
646          *
647          * Note that the constraints might depend on the specific model.
648          * These tests might need some adjustment when support for more
649          * models gets added to the driver.
650          */
651         if (enabled_pod[0] && enabled_chan[2])
652                 return SR_ERR;
653         if (enabled_pod[1] && enabled_chan[3])
654                 return SR_ERR;
655         return SR_OK;
656 }
657
658 static int hmo_setup_channels(const struct sr_dev_inst *sdi)
659 {
660         GSList *l;
661         unsigned int i;
662         gboolean *pod_enabled, setup_changed;
663         char command[MAX_COMMAND_SIZE];
664         struct scope_state *state;
665         const struct scope_config *model;
666         struct sr_channel *ch;
667         struct dev_context *devc;
668         struct sr_scpi_dev_inst *scpi;
669         int ret;
670
671         devc = sdi->priv;
672         scpi = sdi->conn;
673         state = devc->model_state;
674         model = devc->model_config;
675         setup_changed = FALSE;
676
677         pod_enabled = g_try_malloc0(sizeof(gboolean) * model->digital_pods);
678
679         for (l = sdi->channels; l; l = l->next) {
680                 ch = l->data;
681                 switch (ch->type) {
682                 case SR_CHANNEL_ANALOG:
683                         if (ch->enabled == state->analog_channels[ch->index].state)
684                                 break;
685                         g_snprintf(command, sizeof(command),
686                                    (*model->scpi_dialect)[SCPI_CMD_SET_ANALOG_CHAN_STATE],
687                                    ch->index + 1, ch->enabled);
688
689                         if (sr_scpi_send(scpi, command) != SR_OK) {
690                                 g_free(pod_enabled);
691                                 return SR_ERR;
692                         }
693                         state->analog_channels[ch->index].state = ch->enabled;
694                         setup_changed = TRUE;
695                         break;
696                 case SR_CHANNEL_LOGIC:
697                         /*
698                          * A digital POD needs to be enabled for every group of
699                          * 8 channels.
700                          */
701                         if (ch->enabled)
702                                 pod_enabled[ch->index < 8 ? 0 : 1] = TRUE;
703
704                         if (ch->enabled == state->digital_channels[ch->index])
705                                 break;
706                         g_snprintf(command, sizeof(command),
707                                    (*model->scpi_dialect)[SCPI_CMD_SET_DIG_CHAN_STATE],
708                                    ch->index, ch->enabled);
709
710                         if (sr_scpi_send(scpi, command) != SR_OK) {
711                                 g_free(pod_enabled);
712                                 return SR_ERR;
713                         }
714
715                         state->digital_channels[ch->index] = ch->enabled;
716                         setup_changed = TRUE;
717                         break;
718                 default:
719                         g_free(pod_enabled);
720                         return SR_ERR;
721                 }
722         }
723
724         ret = SR_OK;
725         for (i = 0; i < model->digital_pods; i++) {
726                 if (state->digital_pods[i].state == pod_enabled[i])
727                         continue;
728                 g_snprintf(command, sizeof(command),
729                            (*model->scpi_dialect)[SCPI_CMD_SET_DIG_POD_STATE],
730                            i + 1, pod_enabled[i]);
731                 if (sr_scpi_send(scpi, command) != SR_OK) {
732                         ret = SR_ERR;
733                         break;
734                 }
735                 state->digital_pods[i].state = pod_enabled[i];
736                 setup_changed = TRUE;
737         }
738         g_free(pod_enabled);
739         if (ret != SR_OK)
740                 return ret;
741
742         if (setup_changed && hmo_update_sample_rate(sdi) != SR_OK)
743                 return SR_ERR;
744
745         return SR_OK;
746 }
747
748 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
749 {
750         GSList *l;
751         gboolean digital_added[MAX_DIGITAL_GROUP_COUNT];
752         size_t group, pod_count;
753         struct sr_channel *ch;
754         struct dev_context *devc;
755         struct sr_scpi_dev_inst *scpi;
756         int ret;
757
758         scpi = sdi->conn;
759         devc = sdi->priv;
760
761         devc->num_samples = 0;
762         devc->num_frames = 0;
763
764         /* Preset empty results. */
765         for (group = 0; group < ARRAY_SIZE(digital_added); group++)
766                 digital_added[group] = FALSE;
767         g_slist_free(devc->enabled_channels);
768         devc->enabled_channels = NULL;
769
770         /*
771          * Contruct the list of enabled channels. Determine the highest
772          * number of digital pods involved in the acquisition.
773          */
774         pod_count = 0;
775         for (l = sdi->channels; l; l = l->next) {
776                 ch = l->data;
777                 if (!ch->enabled)
778                         continue;
779                 /* Only add a single digital channel per group (pod). */
780                 group = ch->index / 8;
781                 if (ch->type != SR_CHANNEL_LOGIC || !digital_added[group]) {
782                         devc->enabled_channels = g_slist_append(
783                                         devc->enabled_channels, ch);
784                         if (ch->type == SR_CHANNEL_LOGIC) {
785                                 digital_added[group] = TRUE;
786                                 if (pod_count < group + 1)
787                                         pod_count = group + 1;
788                         }
789                 }
790         }
791         if (!devc->enabled_channels)
792                 return SR_ERR;
793         devc->pod_count = pod_count;
794         devc->logic_data = NULL;
795
796         /*
797          * Check constraints. Some channels can be either analog or
798          * digital, but not both at the same time.
799          */
800         if (hmo_check_channels(devc->enabled_channels) != SR_OK) {
801                 sr_err("Invalid channel configuration specified!");
802                 ret = SR_ERR_NA;
803                 goto free_enabled;
804         }
805
806         /*
807          * Configure the analog and digital channels and the
808          * corresponding digital pods.
809          */
810         if (hmo_setup_channels(sdi) != SR_OK) {
811                 sr_err("Failed to setup channel configuration!");
812                 ret = SR_ERR;
813                 goto free_enabled;
814         }
815
816         /*
817          * Start acquisition on the first enabled channel. The
818          * receive routine will continue driving the acquisition.
819          */
820         sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 50,
821                         hmo_receive_data, (void *)sdi);
822
823         std_session_send_df_header(sdi);
824
825         devc->current_channel = devc->enabled_channels;
826
827         return hmo_request_data(sdi);
828
829 free_enabled:
830         g_slist_free(devc->enabled_channels);
831         devc->enabled_channels = NULL;
832         return ret;
833 }
834
835 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
836 {
837         struct dev_context *devc;
838         struct sr_scpi_dev_inst *scpi;
839
840         std_session_send_df_end(sdi);
841
842         devc = sdi->priv;
843
844         devc->num_samples = 0;
845         devc->num_frames = 0;
846         g_slist_free(devc->enabled_channels);
847         devc->enabled_channels = NULL;
848         scpi = sdi->conn;
849         sr_scpi_source_remove(sdi->session, scpi);
850
851         return SR_OK;
852 }
853
854 static struct sr_dev_driver hameg_hmo_driver_info = {
855         .name = "hameg-hmo",
856         .longname = "Hameg HMO",
857         .api_version = 1,
858         .init = std_init,
859         .cleanup = std_cleanup,
860         .scan = scan,
861         .dev_list = std_dev_list,
862         .dev_clear = dev_clear,
863         .config_get = config_get,
864         .config_set = config_set,
865         .config_list = config_list,
866         .dev_open = dev_open,
867         .dev_close = dev_close,
868         .dev_acquisition_start = dev_acquisition_start,
869         .dev_acquisition_stop = dev_acquisition_stop,
870         .context = NULL,
871 };
872 SR_REGISTER_DEV_DRIVER(hameg_hmo_driver_info);