]> sigrok.org Git - libsigrok.git/blob - src/hardware/hameg-hmo/protocol.c
9135601c73f905010c04f3a6b932910ba0eff3f4
[libsigrok.git] / src / hardware / hameg-hmo / protocol.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 <math.h>
23 #include <stdlib.h>
24 #include "scpi.h"
25 #include "protocol.h"
26
27 SR_PRIV void hmo_queue_logic_data(struct dev_context *devc,
28                                   size_t group, GByteArray *pod_data);
29 SR_PRIV void hmo_send_logic_packet(struct sr_dev_inst *sdi,
30                                    struct dev_context *devc);
31 SR_PRIV void hmo_cleanup_logic_data(struct dev_context *devc);
32
33 static const char *hameg_scpi_dialect[] = {
34         [SCPI_CMD_GET_DIG_DATA]               = ":FORM UINT,8;:POD%d:DATA?",
35         [SCPI_CMD_GET_TIMEBASE]               = ":TIM:SCAL?",
36         [SCPI_CMD_SET_TIMEBASE]               = ":TIM:SCAL %s",
37         [SCPI_CMD_GET_COUPLING]               = ":CHAN%d:COUP?",
38         [SCPI_CMD_SET_COUPLING]               = ":CHAN%d:COUP %s",
39         [SCPI_CMD_GET_SAMPLE_RATE]            = ":ACQ:SRAT?",
40         [SCPI_CMD_GET_ANALOG_DATA]            = ":FORM:BORD %s;" \
41                                                 ":FORM REAL,32;:CHAN%d:DATA?",
42         [SCPI_CMD_GET_VERTICAL_SCALE]         = ":CHAN%d:SCAL?",
43         [SCPI_CMD_SET_VERTICAL_SCALE]         = ":CHAN%d:SCAL %s",
44         [SCPI_CMD_GET_DIG_POD_STATE]          = ":POD%d:STAT?",
45         [SCPI_CMD_SET_DIG_POD_STATE]          = ":POD%d:STAT %d",
46         [SCPI_CMD_GET_TRIGGER_SLOPE]          = ":TRIG:A:EDGE:SLOP?",
47         [SCPI_CMD_SET_TRIGGER_SLOPE]          = ":TRIG:A:TYPE EDGE;:TRIG:A:EDGE:SLOP %s",
48         [SCPI_CMD_GET_TRIGGER_PATTERN]        = ":TRIG:A:PATT:SOUR?",
49         [SCPI_CMD_SET_TRIGGER_PATTERN]        = ":TRIG:A:TYPE LOGIC;" \
50                                                 ":TRIG:A:PATT:FUNC AND;" \
51                                                 ":TRIG:A:PATT:COND \"TRUE\";" \
52                                                 ":TRIG:A:PATT:MODE OFF;" \
53                                                 ":TRIG:A:PATT:SOUR \"%s\"",
54         [SCPI_CMD_GET_TRIGGER_SOURCE]         = ":TRIG:A:SOUR?",
55         [SCPI_CMD_SET_TRIGGER_SOURCE]         = ":TRIG:A:SOUR %s",
56         [SCPI_CMD_GET_DIG_CHAN_STATE]         = ":LOG%d:STAT?",
57         [SCPI_CMD_SET_DIG_CHAN_STATE]         = ":LOG%d:STAT %d",
58         [SCPI_CMD_GET_VERTICAL_OFFSET]        = ":CHAN%d:POS?",
59         [SCPI_CMD_GET_HORIZ_TRIGGERPOS]       = ":TIM:POS?",
60         [SCPI_CMD_SET_HORIZ_TRIGGERPOS]       = ":TIM:POS %s",
61         [SCPI_CMD_GET_ANALOG_CHAN_STATE]      = ":CHAN%d:STAT?",
62         [SCPI_CMD_SET_ANALOG_CHAN_STATE]      = ":CHAN%d:STAT %d",
63         [SCPI_CMD_GET_PROBE_UNIT]             = ":PROB%d:SET:ATT:UNIT?",
64         [SCPI_CMD_GET_DIG_POD_THRESHOLD]      = ":POD%d:THR?",
65         [SCPI_CMD_SET_DIG_POD_THRESHOLD]      = ":POD%d:THR %s",
66         [SCPI_CMD_GET_DIG_POD_USER_THRESHOLD] = ":POD%d:THR:UDL%d?",
67         [SCPI_CMD_SET_DIG_POD_USER_THRESHOLD] = ":POD%d:THR:UDL%d %s",
68 };
69
70 static const char *rohde_schwarz_log_not_pod_scpi_dialect[] = {
71         [SCPI_CMD_GET_DIG_DATA]               = ":FORM UINT,8;:LOG%d:DATA?",
72         [SCPI_CMD_GET_TIMEBASE]               = ":TIM:SCAL?",
73         [SCPI_CMD_SET_TIMEBASE]               = ":TIM:SCAL %s",
74         [SCPI_CMD_GET_COUPLING]               = ":CHAN%d:COUP?",
75         [SCPI_CMD_SET_COUPLING]               = ":CHAN%d:COUP %s",
76         [SCPI_CMD_GET_SAMPLE_RATE]            = ":ACQ:SRAT?",
77         [SCPI_CMD_GET_ANALOG_DATA]            = ":FORM:BORD %s;" \
78                                                 ":FORM REAL,32;:CHAN%d:DATA?",
79         [SCPI_CMD_GET_VERTICAL_SCALE]         = ":CHAN%d:SCAL?",
80         [SCPI_CMD_SET_VERTICAL_SCALE]         = ":CHAN%d:SCAL %s",
81         [SCPI_CMD_GET_DIG_POD_STATE]          = ":LOG%d:STAT?",
82         [SCPI_CMD_SET_DIG_POD_STATE]          = ":LOG%d:STAT %d",
83         [SCPI_CMD_GET_TRIGGER_SLOPE]          = ":TRIG:A:EDGE:SLOP?",
84         [SCPI_CMD_SET_TRIGGER_SLOPE]          = ":TRIG:A:TYPE EDGE;:TRIG:A:EDGE:SLOP %s",
85         [SCPI_CMD_GET_TRIGGER_PATTERN]        = ":TRIG:A:PATT:SOUR?",
86         [SCPI_CMD_SET_TRIGGER_PATTERN]        = ":TRIG:A:TYPE LOGIC;" \
87                                                 ":TRIG:A:PATT:FUNC AND;" \
88                                                 ":TRIG:A:PATT:COND \"TRUE\";" \
89                                                 ":TRIG:A:PATT:MODE OFF;" \
90                                                 ":TRIG:A:PATT:SOUR \"%s\"",
91         [SCPI_CMD_GET_TRIGGER_SOURCE]         = ":TRIG:A:SOUR?",
92         [SCPI_CMD_SET_TRIGGER_SOURCE]         = ":TRIG:A:SOUR %s",
93         [SCPI_CMD_GET_DIG_CHAN_STATE]         = ":LOG%d:STAT?",
94         [SCPI_CMD_SET_DIG_CHAN_STATE]         = ":LOG%d:STAT %d",
95         [SCPI_CMD_GET_VERTICAL_OFFSET]        = ":CHAN%d:POS?", /* Might not be supported on RTB200x... */
96         [SCPI_CMD_GET_HORIZ_TRIGGERPOS]       = ":TIM:POS?",
97         [SCPI_CMD_SET_HORIZ_TRIGGERPOS]       = ":TIM:POS %s",
98         [SCPI_CMD_GET_ANALOG_CHAN_STATE]      = ":CHAN%d:STAT?",
99         [SCPI_CMD_SET_ANALOG_CHAN_STATE]      = ":CHAN%d:STAT %d",
100         [SCPI_CMD_GET_PROBE_UNIT]             = ":PROB%d:SET:ATT:UNIT?",
101         [SCPI_CMD_GET_DIG_POD_THRESHOLD]      = ":DIG%d:TECH?",
102         [SCPI_CMD_SET_DIG_POD_THRESHOLD]      = ":DIG%d:TECH %s",
103         [SCPI_CMD_GET_DIG_POD_USER_THRESHOLD] = ":DIG%d:THR?",
104         [SCPI_CMD_SET_DIG_POD_USER_THRESHOLD] = ":DIG%d:THR %s",
105 };
106
107 static const uint32_t devopts[] = {
108         SR_CONF_OSCILLOSCOPE,
109         SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
110         SR_CONF_LIMIT_FRAMES | SR_CONF_SET,
111         SR_CONF_SAMPLERATE | SR_CONF_GET,
112         SR_CONF_TIMEBASE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
113         SR_CONF_NUM_HDIV | SR_CONF_GET,
114         SR_CONF_HORIZ_TRIGGERPOS | SR_CONF_GET | SR_CONF_SET,
115         SR_CONF_TRIGGER_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
116         SR_CONF_TRIGGER_SLOPE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
117         SR_CONF_TRIGGER_PATTERN | SR_CONF_GET | SR_CONF_SET,
118 };
119
120 static const uint32_t devopts_cg_analog[] = {
121         SR_CONF_NUM_VDIV | SR_CONF_GET,
122         SR_CONF_VDIV | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
123         SR_CONF_COUPLING | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
124 };
125
126 static const uint32_t devopts_cg_digital[] = {
127         SR_CONF_LOGIC_THRESHOLD | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
128         SR_CONF_LOGIC_THRESHOLD_CUSTOM | SR_CONF_GET | SR_CONF_SET,
129 };
130
131 static const char *coupling_options[] = {
132         "AC",  // AC with 50 Ohm termination (152x, 202x, 30xx, 1202)
133         "ACL", // AC with 1 MOhm termination
134         "DC",  // DC with 50 Ohm termination
135         "DCL", // DC with 1 MOhm termination
136         "GND",
137 };
138
139 static const char *coupling_options_rtb200x[] = {
140         "ACL", // AC with 1 MOhm termination
141         "DCL", // DC with 1 MOhm termination
142         "GND",
143 };
144
145 static const char *coupling_options_rtm300x[] = {
146         "ACL", // AC with 1 MOhm termination
147         "DC",  // DC with 50 Ohm termination
148         "DCL", // DC with 1 MOhm termination
149         "GND",
150 };
151
152 static const char *scope_trigger_slopes[] = {
153         "POS",
154         "NEG",
155         "EITH",
156 };
157
158 /* Predefined logic thresholds. */
159 static const char *logic_threshold[] = {
160         "TTL",
161         "ECL",
162         "CMOS",
163         "USER1",
164         "USER2", // overwritten by logic_threshold_custom, use USER1 for permanent setting
165 };
166
167 static const char *logic_threshold_rtb200x_rtm300x[] = {
168         "TTL",
169         "ECL",
170         "CMOS",
171         "MAN", // overwritten by logic_threshold_custom
172 };
173
174 /* RTC1002, HMO Compact2 and HMO1002/HMO1202 */
175 static const char *an2_dig8_trigger_sources[] = {
176         "CH1", "CH2",
177         "LINE", "EXT", "PATT", "BUS1", "BUS2",
178         "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7",
179 };
180
181 /* HMO3xx2 */
182 static const char *an2_dig16_trigger_sources[] = {
183         "CH1", "CH2",
184         "LINE", "EXT", "PATT", "BUS1", "BUS2",
185         "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7",
186         "D8", "D9", "D10", "D11", "D12", "D13", "D14", "D15",
187 };
188
189 /* RTB2002 and RTM3002 */
190 static const char *an2_dig16_sbus_trigger_sources[] = {
191         "CH1", "CH2",
192         "LINE", "EXT", "PATT", "SBUS1", "SBUS2",
193         "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7",
194         "D8", "D9", "D10", "D11", "D12", "D13", "D14", "D15",
195 };
196
197 /* HMO Compact4 */
198 static const char *an4_dig8_trigger_sources[] = {
199         "CH1", "CH2", "CH3", "CH4",
200         "LINE", "EXT", "PATT", "BUS1", "BUS2",
201         "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7",
202 };
203
204 /* HMO3xx4 and HMO2524 */
205 static const char *an4_dig16_trigger_sources[] = {
206         "CH1", "CH2", "CH3", "CH4",
207         "LINE", "EXT", "PATT", "BUS1", "BUS2",
208         "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7",
209         "D8", "D9", "D10", "D11", "D12", "D13", "D14", "D15",
210 };
211
212 /* RTB2004, RTM3004 and RTA4004 */
213 static const char *an4_dig16_sbus_trigger_sources[] = {
214         "CH1", "CH2", "CH3", "CH4",
215         "LINE", "EXT", "PATT", "SBUS1", "SBUS2",
216         "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7",
217         "D8", "D9", "D10", "D11", "D12", "D13", "D14", "D15",
218 };
219
220 static const uint64_t timebases[][2] = {
221         /* nanoseconds */
222         { 1, 1000000000 },
223         { 2, 1000000000 },
224         { 5, 1000000000 },
225         { 10, 1000000000 },
226         { 20, 1000000000 },
227         { 50, 1000000000 },
228         { 100, 1000000000 },
229         { 200, 1000000000 },
230         { 500, 1000000000 },
231         /* microseconds */
232         { 1, 1000000 },
233         { 2, 1000000 },
234         { 5, 1000000 },
235         { 10, 1000000 },
236         { 20, 1000000 },
237         { 50, 1000000 },
238         { 100, 1000000 },
239         { 200, 1000000 },
240         { 500, 1000000 },
241         /* milliseconds */
242         { 1, 1000 },
243         { 2, 1000 },
244         { 5, 1000 },
245         { 10, 1000 },
246         { 20, 1000 },
247         { 50, 1000 },
248         { 100, 1000 },
249         { 200, 1000 },
250         { 500, 1000 },
251         /* seconds */
252         { 1, 1 },
253         { 2, 1 },
254         { 5, 1 },
255         { 10, 1 },
256         { 20, 1 },
257         { 50, 1 },
258 };
259
260 /* HMO Compact series (HMO722/724/1022/1024/1522/1524/2022/2024) do
261  * not support 1 ns timebase setting.
262  */
263 static const uint64_t timebases_hmo_compact[][2] = {
264         /* nanoseconds */
265         { 2, 1000000000 },
266         { 5, 1000000000 },
267         { 10, 1000000000 },
268         { 20, 1000000000 },
269         { 50, 1000000000 },
270         { 100, 1000000000 },
271         { 200, 1000000000 },
272         { 500, 1000000000 },
273         /* microseconds */
274         { 1, 1000000 },
275         { 2, 1000000 },
276         { 5, 1000000 },
277         { 10, 1000000 },
278         { 20, 1000000 },
279         { 50, 1000000 },
280         { 100, 1000000 },
281         { 200, 1000000 },
282         { 500, 1000000 },
283         /* milliseconds */
284         { 1, 1000 },
285         { 2, 1000 },
286         { 5, 1000 },
287         { 10, 1000 },
288         { 20, 1000 },
289         { 50, 1000 },
290         { 100, 1000 },
291         { 200, 1000 },
292         { 500, 1000 },
293         /* seconds */
294         { 1, 1 },
295         { 2, 1 },
296         { 5, 1 },
297         { 10, 1 },
298         { 20, 1 },
299         { 50, 1 },
300 };
301
302 static const uint64_t vdivs[][2] = {
303         /* millivolts */
304         { 1, 1000 },
305         { 2, 1000 },
306         { 5, 1000 },
307         { 10, 1000 },
308         { 20, 1000 },
309         { 50, 1000 },
310         { 100, 1000 },
311         { 200, 1000 },
312         { 500, 1000 },
313         /* volts */
314         { 1, 1 },
315         { 2, 1 },
316         { 5, 1 },
317         { 10, 1 },
318 };
319
320 static const char *scope_analog_channel_names[] = {
321         "CH1", "CH2", "CH3", "CH4",
322 };
323
324 static const char *scope_digital_channel_names[] = {
325         "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7",
326         "D8", "D9", "D10", "D11", "D12", "D13", "D14", "D15",
327 };
328
329 static struct scope_config scope_models[] = {
330         {
331                 /* HMO Compact2: HMO722/1022/1522/2022 support only 8 digital channels. */
332                 .name = {"HMO722", "HMO1022", "HMO1522", "HMO2022", NULL},
333                 .analog_channels = 2,
334                 .digital_channels = 8,
335
336                 .analog_names = &scope_analog_channel_names,
337                 .digital_names = &scope_digital_channel_names,
338
339                 .devopts = &devopts,
340                 .num_devopts = ARRAY_SIZE(devopts),
341
342                 .devopts_cg_analog = &devopts_cg_analog,
343                 .num_devopts_cg_analog = ARRAY_SIZE(devopts_cg_analog),
344
345                 .devopts_cg_digital = &devopts_cg_digital,
346                 .num_devopts_cg_digital = ARRAY_SIZE(devopts_cg_digital),
347
348                 .coupling_options = &coupling_options,
349                 .num_coupling_options = ARRAY_SIZE(coupling_options),
350
351                 .logic_threshold = &logic_threshold,
352                 .num_logic_threshold = ARRAY_SIZE(logic_threshold),
353                 .logic_threshold_for_pod = TRUE,
354
355                 .trigger_sources = &an2_dig8_trigger_sources,
356                 .num_trigger_sources = ARRAY_SIZE(an2_dig8_trigger_sources),
357
358                 .trigger_slopes = &scope_trigger_slopes,
359                 .num_trigger_slopes = ARRAY_SIZE(scope_trigger_slopes),
360
361                 .timebases = &timebases_hmo_compact,
362                 .num_timebases = ARRAY_SIZE(timebases_hmo_compact),
363
364                 .vdivs = &vdivs,
365                 .num_vdivs = ARRAY_SIZE(vdivs),
366
367                 .num_xdivs = 12,
368                 .num_ydivs = 8,
369
370                 .scpi_dialect = &hameg_scpi_dialect,
371         },
372         {
373                 /* RTC1002 and HMO1002/HMO1202 support only 8 digital channels. */
374                 .name = {"RTC1002", "HMO1002", "HMO1202", NULL},
375                 .analog_channels = 2,
376                 .digital_channels = 8,
377
378                 .analog_names = &scope_analog_channel_names,
379                 .digital_names = &scope_digital_channel_names,
380
381                 .devopts = &devopts,
382                 .num_devopts = ARRAY_SIZE(devopts),
383
384                 .devopts_cg_analog = &devopts_cg_analog,
385                 .num_devopts_cg_analog = ARRAY_SIZE(devopts_cg_analog),
386
387                 .devopts_cg_digital = &devopts_cg_digital,
388                 .num_devopts_cg_digital = ARRAY_SIZE(devopts_cg_digital),
389
390                 .coupling_options = &coupling_options,
391                 .num_coupling_options = ARRAY_SIZE(coupling_options),
392
393                 .logic_threshold = &logic_threshold,
394                 .num_logic_threshold = ARRAY_SIZE(logic_threshold),
395                 .logic_threshold_for_pod = TRUE,
396
397                 .trigger_sources = &an2_dig8_trigger_sources,
398                 .num_trigger_sources = ARRAY_SIZE(an2_dig8_trigger_sources),
399
400                 .trigger_slopes = &scope_trigger_slopes,
401                 .num_trigger_slopes = ARRAY_SIZE(scope_trigger_slopes),
402
403                 .timebases = &timebases,
404                 .num_timebases = ARRAY_SIZE(timebases),
405
406                 .vdivs = &vdivs,
407                 .num_vdivs = ARRAY_SIZE(vdivs),
408
409                 .num_xdivs = 12,
410                 .num_ydivs = 8,
411
412                 .scpi_dialect = &hameg_scpi_dialect,
413         },
414         {
415                 /* HMO3032/3042/3052/3522 support 16 digital channels. */
416                 .name = {"HMO3032", "HMO3042", "HMO3052", "HMO3522", NULL},
417                 .analog_channels = 2,
418                 .digital_channels = 16,
419
420                 .analog_names = &scope_analog_channel_names,
421                 .digital_names = &scope_digital_channel_names,
422
423                 .devopts = &devopts,
424                 .num_devopts = ARRAY_SIZE(devopts),
425
426                 .devopts_cg_analog = &devopts_cg_analog,
427                 .num_devopts_cg_analog = ARRAY_SIZE(devopts_cg_analog),
428
429                 .devopts_cg_digital = &devopts_cg_digital,
430                 .num_devopts_cg_digital = ARRAY_SIZE(devopts_cg_digital),
431
432                 .coupling_options = &coupling_options,
433                 .num_coupling_options = ARRAY_SIZE(coupling_options),
434
435                 .logic_threshold = &logic_threshold,
436                 .num_logic_threshold = ARRAY_SIZE(logic_threshold),
437                 .logic_threshold_for_pod = TRUE,
438
439                 .trigger_sources = &an2_dig16_trigger_sources,
440                 .num_trigger_sources = ARRAY_SIZE(an2_dig16_trigger_sources),
441
442                 .trigger_slopes = &scope_trigger_slopes,
443                 .num_trigger_slopes = ARRAY_SIZE(scope_trigger_slopes),
444
445                 .timebases = &timebases,
446                 .num_timebases = ARRAY_SIZE(timebases),
447
448                 .vdivs = &vdivs,
449                 .num_vdivs = ARRAY_SIZE(vdivs),
450
451                 .num_xdivs = 12,
452                 .num_ydivs = 8,
453
454                 .scpi_dialect = &hameg_scpi_dialect,
455         },
456         {
457                 /* HMO Compact4: HMO724/1024/1524/2024 support only 8 digital channels. */
458                 .name = {"HMO724", "HMO1024", "HMO1524", "HMO2024", NULL},
459                 .analog_channels = 4,
460                 .digital_channels = 8,
461
462                 .analog_names = &scope_analog_channel_names,
463                 .digital_names = &scope_digital_channel_names,
464
465                 .devopts = &devopts,
466                 .num_devopts = ARRAY_SIZE(devopts),
467
468                 .devopts_cg_analog = &devopts_cg_analog,
469                 .num_devopts_cg_analog = ARRAY_SIZE(devopts_cg_analog),
470
471                 .devopts_cg_digital = &devopts_cg_digital,
472                 .num_devopts_cg_digital = ARRAY_SIZE(devopts_cg_digital),
473
474                 .coupling_options = &coupling_options,
475                 .num_coupling_options = ARRAY_SIZE(coupling_options),
476
477                 .logic_threshold = &logic_threshold,
478                 .num_logic_threshold = ARRAY_SIZE(logic_threshold),
479                 .logic_threshold_for_pod = TRUE,
480
481                 .trigger_sources = &an4_dig8_trigger_sources,
482                 .num_trigger_sources = ARRAY_SIZE(an4_dig8_trigger_sources),
483
484                 .trigger_slopes = &scope_trigger_slopes,
485                 .num_trigger_slopes = ARRAY_SIZE(scope_trigger_slopes),
486
487                 .timebases = &timebases_hmo_compact,
488                 .num_timebases = ARRAY_SIZE(timebases_hmo_compact),
489
490                 .vdivs = &vdivs,
491                 .num_vdivs = ARRAY_SIZE(vdivs),
492
493                 .num_xdivs = 12,
494                 .num_ydivs = 8,
495
496                 .scpi_dialect = &hameg_scpi_dialect,
497         },
498         {
499                 .name = {"HMO2524", "HMO3034", "HMO3044", "HMO3054", "HMO3524", NULL},
500                 .analog_channels = 4,
501                 .digital_channels = 16,
502
503                 .analog_names = &scope_analog_channel_names,
504                 .digital_names = &scope_digital_channel_names,
505
506                 .devopts = &devopts,
507                 .num_devopts = ARRAY_SIZE(devopts),
508
509                 .devopts_cg_analog = &devopts_cg_analog,
510                 .num_devopts_cg_analog = ARRAY_SIZE(devopts_cg_analog),
511
512                 .devopts_cg_digital = &devopts_cg_digital,
513                 .num_devopts_cg_digital = ARRAY_SIZE(devopts_cg_digital),
514
515                 .coupling_options = &coupling_options,
516                 .num_coupling_options = ARRAY_SIZE(coupling_options),
517
518                 .logic_threshold = &logic_threshold,
519                 .num_logic_threshold = ARRAY_SIZE(logic_threshold),
520                 .logic_threshold_for_pod = TRUE,
521
522                 .trigger_sources = &an4_dig16_trigger_sources,
523                 .num_trigger_sources = ARRAY_SIZE(an4_dig16_trigger_sources),
524
525                 .trigger_slopes = &scope_trigger_slopes,
526                 .num_trigger_slopes = ARRAY_SIZE(scope_trigger_slopes),
527
528                 .timebases = &timebases,
529                 .num_timebases = ARRAY_SIZE(timebases),
530
531                 .vdivs = &vdivs,
532                 .num_vdivs = ARRAY_SIZE(vdivs),
533
534                 .num_xdivs = 12,
535                 .num_ydivs = 8,
536
537                 .scpi_dialect = &hameg_scpi_dialect,
538         },
539         {
540                 .name = {"RTB2002", NULL},
541                 .analog_channels = 2,
542                 .digital_channels = 16,
543
544                 .analog_names = &scope_analog_channel_names,
545                 .digital_names = &scope_digital_channel_names,
546
547                 .devopts = &devopts,
548                 .num_devopts = ARRAY_SIZE(devopts),
549
550                 .devopts_cg_analog = &devopts_cg_analog,
551                 .num_devopts_cg_analog = ARRAY_SIZE(devopts_cg_analog),
552
553                 .devopts_cg_digital = &devopts_cg_digital,
554                 .num_devopts_cg_digital = ARRAY_SIZE(devopts_cg_digital),
555
556                 .coupling_options = &coupling_options_rtb200x,
557                 .num_coupling_options = ARRAY_SIZE(coupling_options_rtb200x),
558
559                 .logic_threshold = &logic_threshold_rtb200x_rtm300x,
560                 .num_logic_threshold = ARRAY_SIZE(logic_threshold_rtb200x_rtm300x),
561                 .logic_threshold_for_pod = FALSE,
562
563                 .trigger_sources = &an2_dig16_sbus_trigger_sources,
564                 .num_trigger_sources = ARRAY_SIZE(an2_dig16_sbus_trigger_sources),
565
566                 .trigger_slopes = &scope_trigger_slopes,
567                 .num_trigger_slopes = ARRAY_SIZE(scope_trigger_slopes),
568
569                 .timebases = &timebases,
570                 .num_timebases = ARRAY_SIZE(timebases),
571
572                 .vdivs = &vdivs,
573                 .num_vdivs = ARRAY_SIZE(vdivs),
574
575                 .num_xdivs = 12,
576                 .num_ydivs = 8,
577
578                 .scpi_dialect = &rohde_schwarz_log_not_pod_scpi_dialect,
579         },
580         {
581                 .name = {"RTB2004", NULL},
582                 .analog_channels = 4,
583                 .digital_channels = 16,
584
585                 .analog_names = &scope_analog_channel_names,
586                 .digital_names = &scope_digital_channel_names,
587
588                 .devopts = &devopts,
589                 .num_devopts = ARRAY_SIZE(devopts),
590
591                 .devopts_cg_analog = &devopts_cg_analog,
592                 .num_devopts_cg_analog = ARRAY_SIZE(devopts_cg_analog),
593
594                 .devopts_cg_digital = &devopts_cg_digital,
595                 .num_devopts_cg_digital = ARRAY_SIZE(devopts_cg_digital),
596
597                 .coupling_options = &coupling_options_rtb200x,
598                 .num_coupling_options = ARRAY_SIZE(coupling_options_rtb200x),
599
600                 .logic_threshold = &logic_threshold_rtb200x_rtm300x,
601                 .num_logic_threshold = ARRAY_SIZE(logic_threshold_rtb200x_rtm300x),
602                 .logic_threshold_for_pod = FALSE,
603
604                 .trigger_sources = &an4_dig16_sbus_trigger_sources,
605                 .num_trigger_sources = ARRAY_SIZE(an4_dig16_sbus_trigger_sources),
606
607                 .trigger_slopes = &scope_trigger_slopes,
608                 .num_trigger_slopes = ARRAY_SIZE(scope_trigger_slopes),
609
610                 .timebases = &timebases,
611                 .num_timebases = ARRAY_SIZE(timebases),
612
613                 .vdivs = &vdivs,
614                 .num_vdivs = ARRAY_SIZE(vdivs),
615
616                 .num_xdivs = 12,
617                 .num_ydivs = 8,
618
619                 .scpi_dialect = &rohde_schwarz_log_not_pod_scpi_dialect,
620         },
621         {
622                 .name = {"RTM3002", NULL},
623                 .analog_channels = 2,
624                 .digital_channels = 16,
625
626                 .analog_names = &scope_analog_channel_names,
627                 .digital_names = &scope_digital_channel_names,
628
629                 .devopts = &devopts,
630                 .num_devopts = ARRAY_SIZE(devopts),
631
632                 .devopts_cg_analog = &devopts_cg_analog,
633                 .num_devopts_cg_analog = ARRAY_SIZE(devopts_cg_analog),
634
635                 .devopts_cg_digital = &devopts_cg_digital,
636                 .num_devopts_cg_digital = ARRAY_SIZE(devopts_cg_digital),
637
638                 .coupling_options = &coupling_options_rtm300x,
639                 .num_coupling_options = ARRAY_SIZE(coupling_options_rtm300x),
640
641                 .logic_threshold = &logic_threshold_rtb200x_rtm300x,
642                 .num_logic_threshold = ARRAY_SIZE(logic_threshold_rtb200x_rtm300x),
643                 .logic_threshold_for_pod = FALSE,
644
645                 .trigger_sources = &an2_dig16_sbus_trigger_sources,
646                 .num_trigger_sources = ARRAY_SIZE(an2_dig16_sbus_trigger_sources),
647
648                 .trigger_slopes = &scope_trigger_slopes,
649                 .num_trigger_slopes = ARRAY_SIZE(scope_trigger_slopes),
650
651                 .timebases = &timebases,
652                 .num_timebases = ARRAY_SIZE(timebases),
653
654                 .vdivs = &vdivs,
655                 .num_vdivs = ARRAY_SIZE(vdivs),
656
657                 .num_xdivs = 12,
658                 .num_ydivs = 8,
659
660                 .scpi_dialect = &rohde_schwarz_log_not_pod_scpi_dialect,
661         },
662         {
663                 .name = {"RTM3004", NULL},
664                 .analog_channels = 4,
665                 .digital_channels = 16,
666
667                 .analog_names = &scope_analog_channel_names,
668                 .digital_names = &scope_digital_channel_names,
669
670                 .devopts = &devopts,
671                 .num_devopts = ARRAY_SIZE(devopts),
672
673                 .devopts_cg_analog = &devopts_cg_analog,
674                 .num_devopts_cg_analog = ARRAY_SIZE(devopts_cg_analog),
675
676                 .devopts_cg_digital = &devopts_cg_digital,
677                 .num_devopts_cg_digital = ARRAY_SIZE(devopts_cg_digital),
678
679                 .coupling_options = &coupling_options_rtm300x,
680                 .num_coupling_options = ARRAY_SIZE(coupling_options_rtm300x),
681
682                 .logic_threshold = &logic_threshold_rtb200x_rtm300x,
683                 .num_logic_threshold = ARRAY_SIZE(logic_threshold_rtb200x_rtm300x),
684                 .logic_threshold_for_pod = FALSE,
685
686                 .trigger_sources = &an4_dig16_sbus_trigger_sources,
687                 .num_trigger_sources = ARRAY_SIZE(an4_dig16_sbus_trigger_sources),
688
689                 .trigger_slopes = &scope_trigger_slopes,
690                 .num_trigger_slopes = ARRAY_SIZE(scope_trigger_slopes),
691
692                 .timebases = &timebases,
693                 .num_timebases = ARRAY_SIZE(timebases),
694
695                 .vdivs = &vdivs,
696                 .num_vdivs = ARRAY_SIZE(vdivs),
697
698                 .num_xdivs = 12,
699                 .num_ydivs = 8,
700
701                 .scpi_dialect = &rohde_schwarz_log_not_pod_scpi_dialect,
702         },
703         {
704                 .name = {"RTA4004", NULL},
705                 .analog_channels = 4,
706                 .digital_channels = 16,
707
708                 .analog_names = &scope_analog_channel_names,
709                 .digital_names = &scope_digital_channel_names,
710
711                 .devopts = &devopts,
712                 .num_devopts = ARRAY_SIZE(devopts),
713
714                 .devopts_cg_analog = &devopts_cg_analog,
715                 .num_devopts_cg_analog = ARRAY_SIZE(devopts_cg_analog),
716
717                 .devopts_cg_digital = &devopts_cg_digital,
718                 .num_devopts_cg_digital = ARRAY_SIZE(devopts_cg_digital),
719
720                 .coupling_options = &coupling_options_rtm300x,
721                 .num_coupling_options = ARRAY_SIZE(coupling_options_rtm300x),
722
723                 .logic_threshold = &logic_threshold_rtb200x_rtm300x,
724                 .num_logic_threshold = ARRAY_SIZE(logic_threshold_rtb200x_rtm300x),
725                 .logic_threshold_for_pod = FALSE,
726
727                 .trigger_sources = &an4_dig16_sbus_trigger_sources,
728                 .num_trigger_sources = ARRAY_SIZE(an4_dig16_sbus_trigger_sources),
729
730                 .trigger_slopes = &scope_trigger_slopes,
731                 .num_trigger_slopes = ARRAY_SIZE(scope_trigger_slopes),
732
733                 .timebases = &timebases,
734                 .num_timebases = ARRAY_SIZE(timebases),
735
736                 .vdivs = &vdivs,
737                 .num_vdivs = ARRAY_SIZE(vdivs),
738
739                 .num_xdivs = 12,
740                 .num_ydivs = 8,
741
742                 .scpi_dialect = &rohde_schwarz_log_not_pod_scpi_dialect,
743         },
744 };
745
746 static void scope_state_dump(const struct scope_config *config,
747                              struct scope_state *state)
748 {
749         unsigned int i;
750         char *tmp;
751
752         for (i = 0; i < config->analog_channels; i++) {
753                 tmp = sr_voltage_string((*config->vdivs)[state->analog_channels[i].vdiv][0],
754                                              (*config->vdivs)[state->analog_channels[i].vdiv][1]);
755                 sr_info("State of analog channel %d -> %s : %s (coupling) %s (vdiv) %2.2e (offset)",
756                         i + 1, state->analog_channels[i].state ? "On" : "Off",
757                         (*config->coupling_options)[state->analog_channels[i].coupling],
758                         tmp, state->analog_channels[i].vertical_offset);
759         }
760
761         for (i = 0; i < config->digital_channels; i++) {
762                 sr_info("State of digital channel %d -> %s", i,
763                         state->digital_channels[i] ? "On" : "Off");
764         }
765
766         for (i = 0; i < config->digital_pods; i++) {
767                 if (!strncmp("USER", (*config->logic_threshold)[state->digital_pods[i].threshold], 4) ||
768                     !strcmp("MAN", (*config->logic_threshold)[state->digital_pods[i].threshold]))
769                         sr_info("State of digital POD %d -> %s : %E (threshold)", i + 1,
770                                 state->digital_pods[i].state ? "On" : "Off",
771                                 state->digital_pods[i].user_threshold);
772                 else
773                         sr_info("State of digital POD %d -> %s : %s (threshold)", i + 1,
774                                 state->digital_pods[i].state ? "On" : "Off",
775                                 (*config->logic_threshold)[state->digital_pods[i].threshold]);
776         }
777
778         tmp = sr_period_string((*config->timebases)[state->timebase][0],
779                                (*config->timebases)[state->timebase][1]);
780         sr_info("Current timebase: %s", tmp);
781         g_free(tmp);
782
783         tmp = sr_samplerate_string(state->sample_rate);
784         sr_info("Current samplerate: %s", tmp);
785         g_free(tmp);
786
787         if (!strcmp("PATT", (*config->trigger_sources)[state->trigger_source]))
788                 sr_info("Current trigger: %s (pattern), %.2f (offset)",
789                         state->trigger_pattern,
790                         state->horiz_triggerpos);
791         else // Edge (slope) trigger
792                 sr_info("Current trigger: %s (source), %s (slope) %.2f (offset)",
793                         (*config->trigger_sources)[state->trigger_source],
794                         (*config->trigger_slopes)[state->trigger_slope],
795                         state->horiz_triggerpos);
796 }
797
798 static int scope_state_get_array_option(struct sr_scpi_dev_inst *scpi,
799                 const char *command, const char *(*array)[], unsigned int n, int *result)
800 {
801         char *tmp;
802         int idx;
803
804         if (sr_scpi_get_string(scpi, command, &tmp) != SR_OK)
805                 return SR_ERR;
806
807         if ((idx = std_str_idx_s(tmp, *array, n)) < 0) {
808                 g_free(tmp);
809                 return SR_ERR_ARG;
810         }
811
812         *result = idx;
813
814         g_free(tmp);
815
816         return SR_OK;
817 }
818
819 /**
820  * This function takes a value of the form "2.000E-03" and returns the index
821  * of an array where a matching pair was found.
822  *
823  * @param value The string to be parsed.
824  * @param array The array of s/f pairs.
825  * @param array_len The number of pairs in the array.
826  * @param result The index at which a matching pair was found.
827  *
828  * @return SR_ERR on any parsing error, SR_OK otherwise.
829  */
830 static int array_float_get(gchar *value, const uint64_t array[][2],
831                 int array_len, unsigned int *result)
832 {
833         struct sr_rational rval;
834         struct sr_rational aval;
835
836         if (sr_parse_rational(value, &rval) != SR_OK)
837                 return SR_ERR;
838
839         for (int i = 0; i < array_len; i++) {
840                 sr_rational_set(&aval, array[i][0], array[i][1]);
841                 if (sr_rational_eq(&rval, &aval)) {
842                         *result = i;
843                         return SR_OK;
844                 }
845         }
846
847         return SR_ERR;
848 }
849
850 static struct sr_channel *get_channel_by_index_and_type(GSList *channel_lhead,
851                                                         int index, int type)
852 {
853         while (channel_lhead) {
854                 struct sr_channel *ch = channel_lhead->data;
855                 if (ch->index == index && ch->type == type)
856                         return ch;
857
858                 channel_lhead = channel_lhead->next;
859         }
860
861         return 0;
862 }
863
864 static int analog_channel_state_get(struct sr_dev_inst *sdi,
865                                     const struct scope_config *config,
866                                     struct scope_state *state)
867 {
868         unsigned int i, j;
869         char command[MAX_COMMAND_SIZE];
870         char *tmp_str;
871         struct sr_channel *ch;
872         struct sr_scpi_dev_inst *scpi = sdi->conn;
873
874         for (i = 0; i < config->analog_channels; i++) {
875                 g_snprintf(command, sizeof(command),
876                            (*config->scpi_dialect)[SCPI_CMD_GET_ANALOG_CHAN_STATE],
877                            i + 1);
878
879                 if (sr_scpi_get_bool(scpi, command,
880                                      &state->analog_channels[i].state) != SR_OK)
881                         return SR_ERR;
882
883                 ch = get_channel_by_index_and_type(sdi->channels, i, SR_CHANNEL_ANALOG);
884                 if (ch)
885                         ch->enabled = state->analog_channels[i].state;
886
887                 g_snprintf(command, sizeof(command),
888                            (*config->scpi_dialect)[SCPI_CMD_GET_VERTICAL_SCALE],
889                            i + 1);
890
891                 if (sr_scpi_get_string(scpi, command, &tmp_str) != SR_OK)
892                         return SR_ERR;
893
894                 if (array_float_get(tmp_str, ARRAY_AND_SIZE(vdivs), &j) != SR_OK) {
895                         g_free(tmp_str);
896                         sr_err("Could not determine array index for vertical div scale.");
897                         return SR_ERR;
898                 }
899
900                 g_free(tmp_str);
901                 state->analog_channels[i].vdiv = j;
902
903                 g_snprintf(command, sizeof(command),
904                            (*config->scpi_dialect)[SCPI_CMD_GET_VERTICAL_OFFSET],
905                            i + 1);
906
907                 if (sr_scpi_get_float(scpi, command,
908                                      &state->analog_channels[i].vertical_offset) != SR_OK)
909                         return SR_ERR;
910
911                 g_snprintf(command, sizeof(command),
912                            (*config->scpi_dialect)[SCPI_CMD_GET_COUPLING],
913                            i + 1);
914
915                 if (scope_state_get_array_option(scpi, command, config->coupling_options,
916                                          config->num_coupling_options,
917                                          &state->analog_channels[i].coupling) != SR_OK)
918                         return SR_ERR;
919
920                 g_snprintf(command, sizeof(command),
921                            (*config->scpi_dialect)[SCPI_CMD_GET_PROBE_UNIT],
922                            i + 1);
923
924                 if (sr_scpi_get_string(scpi, command, &tmp_str) != SR_OK)
925                         return SR_ERR;
926
927                 if (tmp_str[0] == 'A')
928                         state->analog_channels[i].probe_unit = 'A';
929                 else
930                         state->analog_channels[i].probe_unit = 'V';
931                 g_free(tmp_str);
932         }
933
934         return SR_OK;
935 }
936
937 static int digital_channel_state_get(struct sr_dev_inst *sdi,
938                                      const struct scope_config *config,
939                                      struct scope_state *state)
940 {
941         unsigned int i, idx;
942         int result = SR_ERR;
943         static char *logic_threshold_short[] = {};
944         char command[MAX_COMMAND_SIZE];
945         struct sr_channel *ch;
946         struct sr_scpi_dev_inst *scpi = sdi->conn;
947
948         for (i = 0; i < config->digital_channels; i++) {
949                 g_snprintf(command, sizeof(command),
950                            (*config->scpi_dialect)[SCPI_CMD_GET_DIG_CHAN_STATE],
951                            i);
952
953                 if (sr_scpi_get_bool(scpi, command,
954                                      &state->digital_channels[i]) != SR_OK)
955                         return SR_ERR;
956
957                 ch = get_channel_by_index_and_type(sdi->channels, i, SR_CHANNEL_LOGIC);
958                 if (ch)
959                         ch->enabled = state->digital_channels[i];
960         }
961
962         /* According to the SCPI standard, on models that support multiple
963          * user-defined logic threshold settings the response to the command
964          * SCPI_CMD_GET_DIG_POD_THRESHOLD might return "USER" instead of
965          * "USER1".
966          *
967          * This makes more difficult to validate the response when the logic
968          * threshold is set to "USER1" and therefore we need to prevent device
969          * opening failures in such configuration case...
970          */
971         for (i = 0; i < config->num_logic_threshold; i++) {
972                 logic_threshold_short[i] = g_strdup((*config->logic_threshold)[i]);
973                 if (!strcmp("USER1", (*config->logic_threshold)[i]))
974                         g_strlcpy(logic_threshold_short[i],
975                                   (*config->logic_threshold)[i], strlen((*config->logic_threshold)[i]));
976         }
977
978         for (i = 0; i < config->digital_pods; i++) {
979                 g_snprintf(command, sizeof(command),
980                            (*config->scpi_dialect)[SCPI_CMD_GET_DIG_POD_STATE],
981                            i + 1);
982
983                 if (sr_scpi_get_bool(scpi, command,
984                                      &state->digital_pods[i].state) != SR_OK)
985                         goto exit;
986
987                 /* Check if the threshold command is based on the POD or digital channel index. */
988                 if (config->logic_threshold_for_pod)
989                         idx = i + 1;
990                 else
991                         idx = i * DIGITAL_CHANNELS_PER_POD;
992
993                 g_snprintf(command, sizeof(command),
994                            (*config->scpi_dialect)[SCPI_CMD_GET_DIG_POD_THRESHOLD],
995                            idx);
996
997                 /* Check for both standard and shortened responses. */
998                 if (scope_state_get_array_option(scpi, command, config->logic_threshold,
999                                                  config->num_logic_threshold,
1000                                                  &state->digital_pods[i].threshold) != SR_OK)
1001                         if (scope_state_get_array_option(scpi, command, (const char * (*)[]) &logic_threshold_short,
1002                                                          config->num_logic_threshold,
1003                                                          &state->digital_pods[i].threshold) != SR_OK)
1004                                 goto exit;
1005
1006                 /* If used-defined or custom threshold is active, get the level. */
1007                 if (!strcmp("USER1", (*config->logic_threshold)[state->digital_pods[i].threshold]))
1008                         g_snprintf(command, sizeof(command),
1009                                    (*config->scpi_dialect)[SCPI_CMD_GET_DIG_POD_USER_THRESHOLD],
1010                                    idx, 1); /* USER1 logic threshold setting. */
1011                 else if (!strcmp("USER2", (*config->logic_threshold)[state->digital_pods[i].threshold]))
1012                         g_snprintf(command, sizeof(command),
1013                                    (*config->scpi_dialect)[SCPI_CMD_GET_DIG_POD_USER_THRESHOLD],
1014                                    idx, 2); /* USER2 for custom logic_threshold setting. */
1015                 else if (!strcmp("USER", (*config->logic_threshold)[state->digital_pods[i].threshold]) ||
1016                          !strcmp("MAN", (*config->logic_threshold)[state->digital_pods[i].threshold]))
1017                         g_snprintf(command, sizeof(command),
1018                                    (*config->scpi_dialect)[SCPI_CMD_GET_DIG_POD_USER_THRESHOLD],
1019                                    idx); /* USER or MAN for custom logic_threshold setting. */
1020                 if (!strcmp("USER1", (*config->logic_threshold)[state->digital_pods[i].threshold]) ||
1021                     !strcmp("USER2", (*config->logic_threshold)[state->digital_pods[i].threshold]) ||
1022                     !strcmp("USER", (*config->logic_threshold)[state->digital_pods[i].threshold]) ||
1023                     !strcmp("MAN", (*config->logic_threshold)[state->digital_pods[i].threshold]))
1024                         if (sr_scpi_get_float(scpi, command,
1025                             &state->digital_pods[i].user_threshold) != SR_OK)
1026                                 goto exit;
1027         }
1028
1029         result = SR_OK;
1030
1031 exit:
1032         for (i = 0; i < config->num_logic_threshold; i++)
1033                 g_free(logic_threshold_short[i]);
1034
1035         return result;
1036 }
1037
1038 SR_PRIV int hmo_update_sample_rate(const struct sr_dev_inst *sdi)
1039 {
1040         struct dev_context *devc;
1041         struct scope_state *state;
1042         const struct scope_config *config;
1043         float tmp_float;
1044
1045         devc = sdi->priv;
1046         config = devc->model_config;
1047         state = devc->model_state;
1048
1049         if (sr_scpi_get_float(sdi->conn,
1050                               (*config->scpi_dialect)[SCPI_CMD_GET_SAMPLE_RATE],
1051                               &tmp_float) != SR_OK)
1052                 return SR_ERR;
1053
1054         state->sample_rate = tmp_float;
1055
1056         return SR_OK;
1057 }
1058
1059 SR_PRIV int hmo_scope_state_get(struct sr_dev_inst *sdi)
1060 {
1061         struct dev_context *devc;
1062         struct scope_state *state;
1063         const struct scope_config *config;
1064         float tmp_float;
1065         unsigned int i;
1066         char *tmp_str;
1067
1068         devc = sdi->priv;
1069         config = devc->model_config;
1070         state = devc->model_state;
1071
1072         sr_info("Fetching scope state");
1073
1074         if (analog_channel_state_get(sdi, config, state) != SR_OK)
1075                 return SR_ERR;
1076
1077         if (digital_channel_state_get(sdi, config, state) != SR_OK)
1078                 return SR_ERR;
1079
1080         if (sr_scpi_get_string(sdi->conn,
1081                         (*config->scpi_dialect)[SCPI_CMD_GET_TIMEBASE],
1082                         &tmp_str) != SR_OK)
1083                 return SR_ERR;
1084
1085         if (array_float_get(tmp_str, ARRAY_AND_SIZE(timebases), &i) != SR_OK) {
1086                 g_free(tmp_str);
1087                 sr_err("Could not determine array index for time base.");
1088                 return SR_ERR;
1089         }
1090         g_free(tmp_str);
1091
1092         state->timebase = i;
1093
1094         if (sr_scpi_get_float(sdi->conn,
1095                         (*config->scpi_dialect)[SCPI_CMD_GET_HORIZ_TRIGGERPOS],
1096                         &tmp_float) != SR_OK)
1097                 return SR_ERR;
1098         state->horiz_triggerpos = tmp_float /
1099                 (((double) (*config->timebases)[state->timebase][0] /
1100                   (*config->timebases)[state->timebase][1]) * config->num_xdivs);
1101         state->horiz_triggerpos -= 0.5;
1102         state->horiz_triggerpos *= -1;
1103
1104         if (scope_state_get_array_option(sdi->conn,
1105                         (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SOURCE],
1106                         config->trigger_sources, config->num_trigger_sources,
1107                         &state->trigger_source) != SR_OK)
1108                 return SR_ERR;
1109
1110         if (scope_state_get_array_option(sdi->conn,
1111                         (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SLOPE],
1112                         config->trigger_slopes, config->num_trigger_slopes,
1113                         &state->trigger_slope) != SR_OK)
1114                 return SR_ERR;
1115
1116         if (sr_scpi_get_string(sdi->conn,
1117                                (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_PATTERN],
1118                                &tmp_str) != SR_OK)
1119                 return SR_ERR;
1120         strncpy(state->trigger_pattern,
1121                 sr_scpi_unquote_string(tmp_str),
1122                 MAX_ANALOG_CHANNEL_COUNT + MAX_DIGITAL_CHANNEL_COUNT);
1123         g_free(tmp_str);
1124
1125         if (hmo_update_sample_rate(sdi) != SR_OK)
1126                 return SR_ERR;
1127
1128         sr_info("Fetching finished.");
1129
1130         scope_state_dump(config, state);
1131
1132         return SR_OK;
1133 }
1134
1135 static struct scope_state *scope_state_new(const struct scope_config *config)
1136 {
1137         struct scope_state *state;
1138
1139         state = g_malloc0(sizeof(struct scope_state));
1140         state->analog_channels = g_malloc0_n(config->analog_channels,
1141                         sizeof(struct analog_channel_state));
1142         state->digital_channels = g_malloc0_n(
1143                         config->digital_channels, sizeof(gboolean));
1144         state->digital_pods = g_malloc0_n(config->digital_pods,
1145                         sizeof(struct digital_pod_state));
1146
1147         return state;
1148 }
1149
1150 SR_PRIV void hmo_scope_state_free(struct scope_state *state)
1151 {
1152         g_free(state->analog_channels);
1153         g_free(state->digital_channels);
1154         g_free(state->digital_pods);
1155         g_free(state);
1156 }
1157
1158 SR_PRIV int hmo_init_device(struct sr_dev_inst *sdi)
1159 {
1160         int model_index;
1161         unsigned int i, j, group;
1162         struct sr_channel *ch;
1163         struct dev_context *devc;
1164         int ret;
1165
1166         devc = sdi->priv;
1167         model_index = -1;
1168
1169         /* Find the exact model. */
1170         for (i = 0; i < ARRAY_SIZE(scope_models); i++) {
1171                 for (j = 0; scope_models[i].name[j]; j++) {
1172                         if (!strcmp(sdi->model, scope_models[i].name[j])) {
1173                                 model_index = i;
1174                                 break;
1175                         }
1176                 }
1177                 if (model_index != -1)
1178                         break;
1179         }
1180
1181         if (model_index == -1) {
1182                 sr_dbg("Unsupported device.");
1183                 return SR_ERR_NA;
1184         }
1185         /* Configure the number of PODs given the number of digital channels. */
1186         scope_models[model_index].digital_pods = scope_models[model_index].digital_channels / DIGITAL_CHANNELS_PER_POD;
1187
1188         devc->analog_groups = g_malloc0(sizeof(struct sr_channel_group*) *
1189                                         scope_models[model_index].analog_channels);
1190         devc->digital_groups = g_malloc0(sizeof(struct sr_channel_group*) *
1191                                          scope_models[model_index].digital_pods);
1192         if (!devc->analog_groups || !devc->digital_groups) {
1193                 g_free(devc->analog_groups);
1194                 g_free(devc->digital_groups);
1195                 return SR_ERR_MALLOC;
1196         }
1197
1198         /* Add analog channels. */
1199         for (i = 0; i < scope_models[model_index].analog_channels; i++) {
1200                 ch = sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE,
1201                            (*scope_models[model_index].analog_names)[i]);
1202
1203                 devc->analog_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
1204
1205                 devc->analog_groups[i]->name = g_strdup(
1206                         (char *)(*scope_models[model_index].analog_names)[i]);
1207                 devc->analog_groups[i]->channels = g_slist_append(NULL, ch);
1208
1209                 sdi->channel_groups = g_slist_append(sdi->channel_groups,
1210                                                    devc->analog_groups[i]);
1211         }
1212
1213         /* Add digital channel groups. */
1214         ret = SR_OK;
1215         for (i = 0; i < scope_models[model_index].digital_pods; i++) {
1216                 devc->digital_groups[i] = g_malloc0(sizeof(struct sr_channel_group));
1217                 if (!devc->digital_groups[i]) {
1218                         ret = SR_ERR_MALLOC;
1219                         break;
1220                 }
1221                 devc->digital_groups[i]->name = g_strdup_printf("POD%d", i + 1);
1222                 sdi->channel_groups = g_slist_append(sdi->channel_groups,
1223                                    devc->digital_groups[i]);
1224         }
1225         if (ret != SR_OK)
1226                 return ret;
1227
1228         /* Add digital channels. */
1229         for (i = 0; i < scope_models[model_index].digital_channels; i++) {
1230                 ch = sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE,
1231                            (*scope_models[model_index].digital_names)[i]);
1232
1233                 group = i / DIGITAL_CHANNELS_PER_POD;
1234                 devc->digital_groups[group]->channels = g_slist_append(
1235                         devc->digital_groups[group]->channels, ch);
1236         }
1237
1238         devc->model_config = &scope_models[model_index];
1239         devc->samples_limit = 0;
1240         devc->frame_limit = 0;
1241
1242         if (!(devc->model_state = scope_state_new(devc->model_config)))
1243                 return SR_ERR_MALLOC;
1244
1245         return SR_OK;
1246 }
1247
1248 /* Queue data of one channel group, for later submission. */
1249 SR_PRIV void hmo_queue_logic_data(struct dev_context *devc,
1250                                   size_t group, GByteArray *pod_data)
1251 {
1252         size_t size;
1253         GByteArray *store;
1254         uint8_t *logic_data;
1255         size_t idx, logic_step;
1256
1257         /*
1258          * Upon first invocation, allocate the array which can hold the
1259          * combined logic data for all channels. Assume that each channel
1260          * will yield an identical number of samples per receive call.
1261          *
1262          * As a poor man's safety measure: (Silently) skip processing
1263          * for unexpected sample counts, and ignore samples for
1264          * unexpected channel groups. Don't bother with complicated
1265          * resize logic, considering that many models only support one
1266          * pod, and the most capable supported models have two pods of
1267          * identical size. We haven't yet seen any "odd" configuration.
1268          */
1269         if (!devc->logic_data) {
1270                 size = pod_data->len * devc->pod_count;
1271                 store = g_byte_array_sized_new(size);
1272                 memset(store->data, 0, size);
1273                 store = g_byte_array_set_size(store, size);
1274                 devc->logic_data = store;
1275         } else {
1276                 store = devc->logic_data;
1277                 size = store->len / devc->pod_count;
1278                 if (group >= devc->pod_count)
1279                         return;
1280         }
1281
1282         /*
1283          * Fold the data of the most recently received channel group into
1284          * the storage, where data resides for all channels combined.
1285          */
1286         logic_data = store->data;
1287         logic_data += group;
1288         logic_step = devc->pod_count;
1289         for (idx = 0; idx < pod_data->len; idx++) {
1290                 *logic_data = pod_data->data[idx];
1291                 logic_data += logic_step;
1292         }
1293
1294         /* Truncate acquisition if a smaller number of samples has been requested. */
1295         if (devc->samples_limit > 0 && devc->logic_data->len > devc->samples_limit * devc->pod_count)
1296                 devc->logic_data->len = devc->samples_limit * devc->pod_count;
1297 }
1298
1299 /* Submit data for all channels, after the individual groups got collected. */
1300 SR_PRIV void hmo_send_logic_packet(struct sr_dev_inst *sdi,
1301                                    struct dev_context *devc)
1302 {
1303         struct sr_datafeed_packet packet;
1304         struct sr_datafeed_logic logic;
1305
1306         if (!devc->logic_data)
1307                 return;
1308
1309         logic.data = devc->logic_data->data;
1310         logic.length = devc->logic_data->len;
1311         logic.unitsize = devc->pod_count;
1312
1313         packet.type = SR_DF_LOGIC;
1314         packet.payload = &logic;
1315
1316         sr_session_send(sdi, &packet);
1317 }
1318
1319 /* Undo previous resource allocation. */
1320 SR_PRIV void hmo_cleanup_logic_data(struct dev_context *devc)
1321 {
1322
1323         if (devc->logic_data) {
1324                 g_byte_array_free(devc->logic_data, TRUE);
1325                 devc->logic_data = NULL;
1326         }
1327         /*
1328          * Keep 'pod_count'! It's required when more frames will be
1329          * received, and does not harm when kept after acquisition.
1330          */
1331 }
1332
1333 SR_PRIV int hmo_receive_data(int fd, int revents, void *cb_data)
1334 {
1335         struct sr_channel *ch;
1336         struct sr_dev_inst *sdi;
1337         struct dev_context *devc;
1338         struct scope_state *state;
1339         struct sr_datafeed_packet packet;
1340         GByteArray *data;
1341         struct sr_datafeed_analog analog;
1342         struct sr_analog_encoding encoding;
1343         struct sr_analog_meaning meaning;
1344         struct sr_analog_spec spec;
1345         struct sr_datafeed_logic logic;
1346         size_t group;
1347
1348         (void)fd;
1349         (void)revents;
1350
1351         data = NULL;
1352
1353         if (!(sdi = cb_data))
1354                 return TRUE;
1355
1356         if (!(devc = sdi->priv))
1357                 return TRUE;
1358
1359         /* Although this is correct in general, the USBTMC libusb implementation
1360          * currently does not generate an event prior to the first read. Often
1361          * it is ok to start reading just after the 50ms timeout. See bug #785.
1362         if (revents != G_IO_IN)
1363                 return TRUE;
1364         */
1365
1366         ch = devc->current_channel->data;
1367         state = devc->model_state;
1368
1369         /*
1370          * Send "frame begin" packet upon reception of data for the
1371          * first enabled channel.
1372          */
1373         if (devc->current_channel == devc->enabled_channels) {
1374                 packet.type = SR_DF_FRAME_BEGIN;
1375                 sr_session_send(sdi, &packet);
1376         }
1377
1378         /*
1379          * Pass on the received data of the channel(s).
1380          */
1381         switch (ch->type) {
1382         case SR_CHANNEL_ANALOG:
1383                 if (sr_scpi_get_block(sdi->conn, NULL, &data) != SR_OK) {
1384                         if (data)
1385                                 g_byte_array_free(data, TRUE);
1386                         return TRUE;
1387                 }
1388
1389                 packet.type = SR_DF_ANALOG;
1390
1391                 analog.data = data->data;
1392                 analog.num_samples = data->len / sizeof(float);
1393                 /* Truncate acquisition if a smaller number of samples has been requested. */
1394                 if (devc->samples_limit > 0 && analog.num_samples > devc->samples_limit)
1395                         analog.num_samples = devc->samples_limit;
1396                 analog.encoding = &encoding;
1397                 analog.meaning = &meaning;
1398                 analog.spec = &spec;
1399
1400                 encoding.unitsize = sizeof(float);
1401                 encoding.is_signed = TRUE;
1402                 encoding.is_float = TRUE;
1403 #ifdef WORDS_BIGENDIAN
1404                 encoding.is_bigendian = TRUE;
1405 #else
1406                 encoding.is_bigendian = FALSE;
1407 #endif
1408                 /* TODO: Use proper 'digits' value for this device (and its modes). */
1409                 encoding.digits = 2;
1410                 encoding.is_digits_decimal = FALSE;
1411                 encoding.scale.p = 1;
1412                 encoding.scale.q = 1;
1413                 encoding.offset.p = 0;
1414                 encoding.offset.q = 1;
1415                 if (state->analog_channels[ch->index].probe_unit == 'V') {
1416                         meaning.mq = SR_MQ_VOLTAGE;
1417                         meaning.unit = SR_UNIT_VOLT;
1418                 } else {
1419                         meaning.mq = SR_MQ_CURRENT;
1420                         meaning.unit = SR_UNIT_AMPERE;
1421                 }
1422                 meaning.mqflags = 0;
1423                 meaning.channels = g_slist_append(NULL, ch);
1424                 /* TODO: Use proper 'digits' value for this device (and its modes). */
1425                 spec.spec_digits = 2;
1426                 packet.payload = &analog;
1427                 sr_session_send(sdi, &packet);
1428                 devc->num_samples = data->len / sizeof(float);
1429                 g_slist_free(meaning.channels);
1430                 g_byte_array_free(data, TRUE);
1431                 data = NULL;
1432                 break;
1433         case SR_CHANNEL_LOGIC:
1434                 if (sr_scpi_get_block(sdi->conn, NULL, &data) != SR_OK) {
1435                         if (data)
1436                                 g_byte_array_free(data, TRUE);
1437                         return TRUE;
1438                 }
1439
1440                 /*
1441                  * If only data from the first pod is involved in the
1442                  * acquisition, then the raw input bytes can get passed
1443                  * forward for performance reasons. When the second pod
1444                  * is involved (either alone, or in combination with the
1445                  * first pod), then the received bytes need to be put
1446                  * into memory in such a layout that all channel groups
1447                  * get combined, and a unitsize larger than a single byte
1448                  * applies. The "queue" logic transparently copes with
1449                  * any such configuration. This works around the lack
1450                  * of support for "meaning" to logic data, which is used
1451                  * above for analog data.
1452                  */
1453                 if (devc->pod_count == 1) {
1454                         packet.type = SR_DF_LOGIC;
1455                         logic.data = data->data;
1456                         logic.length = data->len;
1457                         /* Truncate acquisition if a smaller number of samples has been requested. */
1458                         if (devc->samples_limit > 0 && logic.length > devc->samples_limit)
1459                                 logic.length = devc->samples_limit;
1460                         logic.unitsize = 1;
1461                         packet.payload = &logic;
1462                         sr_session_send(sdi, &packet);
1463                 } else {
1464                         group = ch->index / DIGITAL_CHANNELS_PER_POD;
1465                         hmo_queue_logic_data(devc, group, data);
1466                 }
1467
1468                 devc->num_samples = data->len / devc->pod_count;
1469                 g_byte_array_free(data, TRUE);
1470                 data = NULL;
1471                 break;
1472         default:
1473                 sr_err("Invalid channel type.");
1474                 break;
1475         }
1476
1477         /*
1478          * Advance to the next enabled channel. When data for all enabled
1479          * channels was received, then flush potentially queued logic data,
1480          * and send the "frame end" packet.
1481          */
1482         if (devc->current_channel->next) {
1483                 devc->current_channel = devc->current_channel->next;
1484                 hmo_request_data(sdi);
1485                 return TRUE;
1486         }
1487         hmo_send_logic_packet(sdi, devc);
1488
1489         /*
1490          * Release the logic data storage after each frame. This copes
1491          * with sample counts that differ in length per frame. -- Is
1492          * this a real constraint when acquiring multiple frames with
1493          * identical device settings?
1494          */
1495         hmo_cleanup_logic_data(devc);
1496
1497         packet.type = SR_DF_FRAME_END;
1498         sr_session_send(sdi, &packet);
1499
1500         /*
1501          * End of frame was reached. Stop acquisition after the specified
1502          * number of frames or after the specified number of samples, or
1503          * continue reception by starting over at the first enabled channel.
1504          */
1505         if (++devc->num_frames >= devc->frame_limit || devc->num_samples >= devc->samples_limit) {
1506                 sr_dev_acquisition_stop(sdi);
1507                 hmo_cleanup_logic_data(devc);
1508         } else {
1509                 devc->current_channel = devc->enabled_channels;
1510                 hmo_request_data(sdi);
1511         }
1512
1513         return TRUE;
1514 }