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