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