]> sigrok.org Git - libsigrok.git/blame - src/hardware/yokogawa-dlm/api.c
Build: Include <config.h> first in all source files
[libsigrok.git] / src / hardware / yokogawa-dlm / api.c
CommitLineData
10763937
SA
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2014 abraxa (Soeren Apel) <soeren@apelpie.net>
5 * Based on the Hameg HMO driver by poljar (Damir Jelić) <poljarinho@gmail.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
6ec6c43b 21#include <config.h>
10763937 22#include <stdlib.h>
5a1afc09 23#include "scpi.h"
10763937
SA
24#include "protocol.h"
25
8ab929d6 26SR_PRIV struct sr_dev_driver yokogawa_dlm_driver_info;
8ab929d6 27
329733d9
UH
28static const char *MANUFACTURER_ID = "YOKOGAWA";
29static const char *MANUFACTURER_NAME = "Yokogawa";
8ab929d6 30
f3c60fb6
SA
31static const uint32_t dlm_scanopts[] = {
32 SR_CONF_CONN,
33};
34
35static const uint32_t dlm_drvopts[] = {
cf0280fa
AJ
36 SR_CONF_LOGIC_ANALYZER,
37 SR_CONF_OSCILLOSCOPE,
38};
39
f3c60fb6
SA
40static const uint32_t dlm_devopts[] = {
41 SR_CONF_LIMIT_FRAMES | SR_CONF_GET | SR_CONF_SET,
42 SR_CONF_SAMPLERATE | SR_CONF_GET,
43 SR_CONF_TIMEBASE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
44 SR_CONF_NUM_HDIV | SR_CONF_GET,
45 SR_CONF_HORIZ_TRIGGERPOS | SR_CONF_GET | SR_CONF_SET,
46 SR_CONF_TRIGGER_SLOPE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
47 SR_CONF_TRIGGER_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
48};
49
50static const uint32_t dlm_analog_devopts[] = {
51 SR_CONF_COUPLING | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
52 SR_CONF_VDIV | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
53 SR_CONF_NUM_VDIV | SR_CONF_GET,
54};
55
56static const uint32_t dlm_digital_devopts[] = {
57};
58
8ab929d6
SA
59enum {
60 CG_INVALID = -1,
61 CG_NONE,
62 CG_ANALOG,
63 CG_DIGITAL,
64};
65
4f840ce9 66static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
8ab929d6
SA
67{
68 return std_init(sr_ctx, di, LOG_PREFIX);
69}
70
71static struct sr_dev_inst *probe_usbtmc_device(struct sr_scpi_dev_inst *scpi)
72{
73 struct sr_dev_inst *sdi;
74 struct dev_context *devc;
75 struct sr_scpi_hw_info *hw_info;
76 char *model_name;
77 int model_index;
78
79 sdi = NULL;
80 devc = NULL;
81 hw_info = NULL;
82
83 if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) {
84 sr_info("Couldn't get IDN response.");
85 goto fail;
86 }
87
88 if (strcmp(hw_info->manufacturer, MANUFACTURER_ID) != 0)
89 goto fail;
90
91 if (dlm_model_get(hw_info->model, &model_name, &model_index) != SR_OK)
92 goto fail;
93
aac29cc1 94 sdi = g_malloc0(sizeof(struct sr_dev_inst));
0af636be
UH
95 sdi->status = SR_ST_ACTIVE;
96 sdi->vendor = g_strdup(MANUFACTURER_NAME);
97 sdi->model = g_strdup(model_name);
98 sdi->version = g_strdup(hw_info->firmware_version);
8ab929d6 99
d1314831
SA
100 sdi->serial_num = g_strdup(hw_info->serial_number);
101
8ab929d6
SA
102 sr_scpi_hw_info_free(hw_info);
103 hw_info = NULL;
104
ac10a927 105 devc = g_malloc0(sizeof(struct dev_context));
8ab929d6 106
4f840ce9 107 sdi->driver = &yokogawa_dlm_driver_info;
8ab929d6
SA
108 sdi->priv = devc;
109 sdi->inst_type = SR_INST_SCPI;
110 sdi->conn = scpi;
111
112 if (dlm_device_init(sdi, model_index) != SR_OK)
113 goto fail;
114
115 sr_scpi_close(sdi->conn);
116
117 sdi->status = SR_ST_INACTIVE;
118 return sdi;
119
120fail:
121 if (hw_info)
122 sr_scpi_hw_info_free(hw_info);
123 if (sdi)
124 sr_dev_inst_free(sdi);
b1f83103 125 g_free(devc);
8ab929d6
SA
126
127 return NULL;
128}
129
4f840ce9 130static GSList *scan(struct sr_dev_driver *di, GSList *options)
8ab929d6 131{
41812aca 132 return sr_scpi_scan(di->context, options, probe_usbtmc_device);
8ab929d6
SA
133}
134
4f840ce9 135static GSList *dev_list(const struct sr_dev_driver *di)
8ab929d6 136{
41812aca 137 return ((struct drv_context *)(di->context))->instances;
8ab929d6
SA
138}
139
140static void clear_helper(void *priv)
141{
142 struct dev_context *devc;
143
144 devc = priv;
145
146 dlm_scope_state_destroy(devc->model_state);
147
148 g_free(devc->analog_groups);
149 g_free(devc->digital_groups);
150 g_free(devc);
151}
152
4f840ce9 153static int dev_clear(const struct sr_dev_driver *di)
8ab929d6
SA
154{
155 return std_dev_clear(di, clear_helper);
156}
157
158static int dev_open(struct sr_dev_inst *sdi)
159{
160 if (sdi->status != SR_ST_ACTIVE && sr_scpi_open(sdi->conn) != SR_OK)
161 return SR_ERR;
162
163 if (dlm_scope_state_query(sdi) != SR_OK)
164 return SR_ERR;
165
166 sdi->status = SR_ST_ACTIVE;
167
168 return SR_OK;
169}
170
171static int dev_close(struct sr_dev_inst *sdi)
172{
173 if (sdi->status == SR_ST_INACTIVE)
174 return SR_OK;
175
176 sr_scpi_close(sdi->conn);
177
178 sdi->status = SR_ST_INACTIVE;
179
180 return SR_OK;
181}
182
4f840ce9 183static int cleanup(const struct sr_dev_driver *di)
8ab929d6 184{
4f840ce9 185 dev_clear(di);
8ab929d6
SA
186
187 return SR_OK;
188}
189
190/**
191 * Check which category a given channel group belongs to.
192 *
193 * @param devc Our internal device context.
a9308652 194 * @param cg The channel group to check.
8ab929d6 195 *
a9308652
UH
196 * @retval CG_NONE cg is NULL
197 * @retval CG_ANALOG cg is an analog group
8ab929d6
SA
198 * @retval CG_DIGITAL cg is a digital group
199 * @retval CG_INVALID cg is something else
200 */
201static int check_channel_group(struct dev_context *devc,
ac10a927 202 const struct sr_channel_group *cg)
8ab929d6
SA
203{
204 unsigned int i;
329733d9 205 const struct scope_config *model;
8ab929d6
SA
206
207 model = devc->model_config;
208
209 if (!cg)
210 return CG_NONE;
211
a9308652 212 for (i = 0; i < model->analog_channels; i++)
8ab929d6
SA
213 if (cg == devc->analog_groups[i])
214 return CG_ANALOG;
215
a9308652 216 for (i = 0; i < model->pods; i++)
8ab929d6
SA
217 if (cg == devc->digital_groups[i])
218 return CG_DIGITAL;
219
220 sr_err("Invalid channel group specified.");
221 return CG_INVALID;
222}
223
584560f1 224static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
ac10a927 225 const struct sr_channel_group *cg)
8ab929d6
SA
226{
227 int ret, cg_type;
228 unsigned int i;
229 struct dev_context *devc;
329733d9 230 const struct scope_config *model;
8ab929d6
SA
231 struct scope_state *state;
232
233 if (!sdi || !(devc = sdi->priv))
234 return SR_ERR_ARG;
235
236 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
237 return SR_ERR;
238
239 ret = SR_ERR_NA;
240 model = devc->model_config;
241 state = devc->model_state;
242
243 switch (key) {
bf622e6d 244 case SR_CONF_NUM_HDIV:
8ab929d6
SA
245 *data = g_variant_new_int32(model->num_xdivs);
246 ret = SR_OK;
247 break;
248 case SR_CONF_TIMEBASE:
249 *data = g_variant_new("(tt)",
f3c60fb6
SA
250 dlm_timebases[state->timebase][0],
251 dlm_timebases[state->timebase][1]);
8ab929d6
SA
252 ret = SR_OK;
253 break;
254 case SR_CONF_NUM_VDIV:
255 if (cg_type == CG_NONE) {
256 sr_err("No channel group specified.");
257 return SR_ERR_CHANNEL_GROUP;
258 } else if (cg_type == CG_ANALOG) {
259 *data = g_variant_new_int32(model->num_ydivs);
260 ret = SR_OK;
261 break;
262 } else {
263 ret = SR_ERR_NA;
264 }
265 break;
266 case SR_CONF_VDIV:
267 ret = SR_ERR_NA;
268 if (cg_type == CG_NONE) {
269 sr_err("No channel group specified.");
270 return SR_ERR_CHANNEL_GROUP;
271 } else if (cg_type != CG_ANALOG)
272 break;
273
a9308652 274 for (i = 0; i < model->analog_channels; i++) {
8ab929d6
SA
275 if (cg != devc->analog_groups[i])
276 continue;
277 *data = g_variant_new("(tt)",
f3c60fb6
SA
278 dlm_vdivs[state->analog_states[i].vdiv][0],
279 dlm_vdivs[state->analog_states[i].vdiv][1]);
8ab929d6
SA
280 ret = SR_OK;
281 break;
282 }
283 break;
284 case SR_CONF_TRIGGER_SOURCE:
285 *data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
286 ret = SR_OK;
287 break;
288 case SR_CONF_TRIGGER_SLOPE:
f3c60fb6 289 *data = g_variant_new_string(dlm_trigger_slopes[state->trigger_slope]);
8ab929d6
SA
290 ret = SR_OK;
291 break;
292 case SR_CONF_HORIZ_TRIGGERPOS:
293 *data = g_variant_new_double(state->horiz_triggerpos);
294 ret = SR_OK;
295 break;
296 case SR_CONF_COUPLING:
297 ret = SR_ERR_NA;
298 if (cg_type == CG_NONE) {
299 sr_err("No channel group specified.");
300 return SR_ERR_CHANNEL_GROUP;
301 } else if (cg_type != CG_ANALOG)
302 break;
303
a9308652 304 for (i = 0; i < model->analog_channels; i++) {
8ab929d6
SA
305 if (cg != devc->analog_groups[i])
306 continue;
307 *data = g_variant_new_string((*model->coupling_options)[state->analog_states[i].coupling]);
308 ret = SR_OK;
309 break;
310 }
311 break;
312 case SR_CONF_SAMPLERATE:
313 *data = g_variant_new_uint64(state->sample_rate);
314 ret = SR_OK;
315 break;
316 default:
317 ret = SR_ERR_NA;
318 }
319
320 return ret;
321}
322
323static GVariant *build_tuples(const uint64_t (*array)[][2], unsigned int n)
324{
325 unsigned int i;
326 GVariant *rational[2];
327 GVariantBuilder gvb;
328
329 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
330
331 for (i = 0; i < n; i++) {
332 rational[0] = g_variant_new_uint64((*array)[i][0]);
333 rational[1] = g_variant_new_uint64((*array)[i][1]);
334
335 /* FIXME: Valgrind reports a memory leak here. */
336 g_variant_builder_add_value(&gvb, g_variant_new_tuple(rational, 2));
337 }
338
339 return g_variant_builder_end(&gvb);
340}
341
584560f1 342static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
ac10a927 343 const struct sr_channel_group *cg)
8ab929d6
SA
344{
345 int ret, cg_type;
346 unsigned int i, j;
347 char float_str[30];
348 struct dev_context *devc;
329733d9 349 const struct scope_config *model;
8ab929d6
SA
350 struct scope_state *state;
351 const char *tmp;
352 uint64_t p, q;
353 double tmp_d;
354 gboolean update_sample_rate;
355
356 if (!sdi || !(devc = sdi->priv))
357 return SR_ERR_ARG;
358
359 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
360 return SR_ERR;
361
362 model = devc->model_config;
363 state = devc->model_state;
364 update_sample_rate = FALSE;
365
366 ret = SR_ERR_NA;
367
368 switch (key) {
369 case SR_CONF_LIMIT_FRAMES:
370 devc->frame_limit = g_variant_get_uint64(data);
371 ret = SR_OK;
372 break;
373 case SR_CONF_TRIGGER_SOURCE:
374 tmp = g_variant_get_string(data, NULL);
375 for (i = 0; (*model->trigger_sources)[i]; i++) {
376 if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
377 continue;
378 state->trigger_source = i;
379 /* TODO: A and B trigger support possible? */
380 ret = dlm_trigger_source_set(sdi->conn, (*model->trigger_sources)[i]);
381 break;
382 }
383 break;
384 case SR_CONF_VDIV:
385 if (cg_type == CG_NONE) {
386 sr_err("No channel group specified.");
387 return SR_ERR_CHANNEL_GROUP;
388 }
389
390 g_variant_get(data, "(tt)", &p, &q);
391
f3c60fb6
SA
392 for (i = 0; i < ARRAY_SIZE(dlm_vdivs); i++) {
393 if (p != dlm_vdivs[i][0] ||
394 q != dlm_vdivs[i][1])
8ab929d6 395 continue;
a9308652 396 for (j = 1; j <= model->analog_channels; j++) {
8ab929d6
SA
397 if (cg != devc->analog_groups[j - 1])
398 continue;
399 state->analog_states[j - 1].vdiv = i;
400 g_ascii_formatd(float_str, sizeof(float_str),
401 "%E", (float) p / q);
402 if (dlm_analog_chan_vdiv_set(sdi->conn, j, float_str) != SR_OK ||
ac10a927 403 sr_scpi_get_opc(sdi->conn) != SR_OK)
8ab929d6
SA
404 return SR_ERR;
405
406 break;
407 }
408
409 ret = SR_OK;
410 break;
411 }
412 break;
413 case SR_CONF_TIMEBASE:
414 g_variant_get(data, "(tt)", &p, &q);
415
f3c60fb6
SA
416 for (i = 0; i < ARRAY_SIZE(dlm_timebases); i++) {
417 if (p != dlm_timebases[i][0] ||
418 q != dlm_timebases[i][1])
8ab929d6
SA
419 continue;
420 state->timebase = i;
421 g_ascii_formatd(float_str, sizeof(float_str),
422 "%E", (float) p / q);
423 ret = dlm_timebase_set(sdi->conn, float_str);
424 update_sample_rate = TRUE;
425 break;
426 }
427 break;
428 case SR_CONF_HORIZ_TRIGGERPOS:
429 tmp_d = g_variant_get_double(data);
430
431 /* TODO: Check if the calculation makes sense for the DLM. */
432 if (tmp_d < 0.0 || tmp_d > 1.0)
433 return SR_ERR;
434
435 state->horiz_triggerpos = tmp_d;
436 tmp_d = -(tmp_d - 0.5) *
f3c60fb6
SA
437 ((double) dlm_timebases[state->timebase][0] /
438 dlm_timebases[state->timebase][1])
ac10a927 439 * model->num_xdivs;
8ab929d6
SA
440
441 g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
442 ret = dlm_horiz_trigger_pos_set(sdi->conn, float_str);
443 break;
444 case SR_CONF_TRIGGER_SLOPE:
445 tmp = g_variant_get_string(data, NULL);
446
447 if (!tmp || !(tmp[0] == 'f' || tmp[0] == 'r'))
448 return SR_ERR_ARG;
449
450 /* Note: See dlm_trigger_slopes[] in protocol.c. */
451 state->trigger_slope = (tmp[0] == 'r') ?
452 SLOPE_POSITIVE : SLOPE_NEGATIVE;
453
454 ret = dlm_trigger_slope_set(sdi->conn, state->trigger_slope);
455 break;
456 case SR_CONF_COUPLING:
457 if (cg_type == CG_NONE) {
458 sr_err("No channel group specified.");
459 return SR_ERR_CHANNEL_GROUP;
460 }
461
462 tmp = g_variant_get_string(data, NULL);
463
464 for (i = 0; (*model->coupling_options)[i]; i++) {
465 if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
466 continue;
a9308652 467 for (j = 1; j <= model->analog_channels; j++) {
8ab929d6
SA
468 if (cg != devc->analog_groups[j - 1])
469 continue;
470 state->analog_states[j-1].coupling = i;
471
472 if (dlm_analog_chan_coupl_set(sdi->conn, j, tmp) != SR_OK ||
ac10a927 473 sr_scpi_get_opc(sdi->conn) != SR_OK)
8ab929d6
SA
474 return SR_ERR;
475 break;
476 }
477
478 ret = SR_OK;
479 break;
480 }
481 break;
482 default:
483 ret = SR_ERR_NA;
484 break;
485 }
486
487 if (ret == SR_OK)
488 ret = sr_scpi_get_opc(sdi->conn);
489
490 if (ret == SR_OK && update_sample_rate)
491 ret = dlm_sample_rate_query(sdi);
492
493 return ret;
494}
495
c65a021c
SA
496static int config_channel_set(const struct sr_dev_inst *sdi,
497 struct sr_channel *ch, unsigned int changes)
498{
a9308652 499 /* Currently we only handle SR_CHANNEL_SET_ENABLED. */
c65a021c
SA
500 if (changes != SR_CHANNEL_SET_ENABLED)
501 return SR_ERR_NA;
502
503 return dlm_channel_state_set(sdi, ch->index, ch->enabled);
504}
505
584560f1 506static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
ac10a927 507 const struct sr_channel_group *cg)
8ab929d6 508{
cf0280fa
AJ
509 int cg_type = CG_NONE;
510 struct dev_context *devc = NULL;
329733d9 511 const struct scope_config *model = NULL;
8ab929d6 512
f3c60fb6
SA
513 /* SR_CONF_SCAN_OPTIONS is always valid, regardless of sdi or probe group. */
514 if (key == SR_CONF_SCAN_OPTIONS) {
515 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
516 dlm_scanopts, ARRAY_SIZE(dlm_scanopts), sizeof(uint32_t));
517 return SR_OK;
518 }
8ab929d6 519
f3c60fb6
SA
520 /* If sdi is NULL, nothing except SR_CONF_DEVICE_OPTIONS can be provided. */
521 if (key == SR_CONF_DEVICE_OPTIONS && !sdi) {
522 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
523 dlm_drvopts, ARRAY_SIZE(dlm_drvopts), sizeof(uint32_t));
524 return SR_OK;
cf0280fa 525 }
8ab929d6 526
f3c60fb6
SA
527 if (!sdi)
528 return SR_ERR_ARG;
529
530 devc = sdi->priv;
531 model = devc->model_config;
532
a9308652
UH
533 /*
534 * If cg is NULL, only the SR_CONF_DEVICE_OPTIONS that are not
535 * specific to a probe group must be returned.
536 */
f3c60fb6
SA
537 if (!cg) {
538 switch (key) {
539 case SR_CONF_DEVICE_OPTIONS:
540 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
541 dlm_devopts, ARRAY_SIZE(dlm_devopts), sizeof(uint32_t));
542 return SR_OK;
543 case SR_CONF_TIMEBASE:
544 *data = build_tuples(&dlm_timebases, ARRAY_SIZE(dlm_timebases));
545 return SR_OK;
546 case SR_CONF_TRIGGER_SOURCE:
547 if (!model)
548 return SR_ERR_ARG;
549 *data = g_variant_new_strv(*model->trigger_sources,
550 g_strv_length((char **)*model->trigger_sources));
551 return SR_OK;
552 case SR_CONF_TRIGGER_SLOPE:
553 *data = g_variant_new_strv(dlm_trigger_slopes,
554 g_strv_length((char **)dlm_trigger_slopes));
555 return SR_OK;
556 case SR_CONF_NUM_HDIV:
49f49cb5 557 *data = g_variant_new_uint32(model->num_xdivs);
f3c60fb6
SA
558 return SR_OK;
559 default:
560 return SR_ERR_NA;
561 }
562 }
563
564 if ((cg_type = check_channel_group(devc, cg)) == CG_INVALID)
565 return SR_ERR;
566
8ab929d6 567 switch (key) {
8ab929d6 568 case SR_CONF_DEVICE_OPTIONS:
f3c60fb6 569 if (cg_type == CG_ANALOG) {
584560f1 570 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
f3c60fb6
SA
571 dlm_analog_devopts, ARRAY_SIZE(dlm_analog_devopts), sizeof(uint32_t));
572 } else if (cg_type == CG_DIGITAL) {
573 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
574 dlm_digital_devopts, ARRAY_SIZE(dlm_digital_devopts), sizeof(uint32_t));
8ab929d6 575 } else {
584560f1
BV
576 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
577 NULL, 0, sizeof(uint32_t));
8ab929d6
SA
578 }
579 break;
580 case SR_CONF_COUPLING:
581 if (cg_type == CG_NONE)
582 return SR_ERR_CHANNEL_GROUP;
583 *data = g_variant_new_strv(*model->coupling_options,
ac10a927 584 g_strv_length((char **)*model->coupling_options));
8ab929d6 585 break;
8ab929d6
SA
586 case SR_CONF_VDIV:
587 if (cg_type == CG_NONE)
588 return SR_ERR_CHANNEL_GROUP;
f3c60fb6 589 *data = build_tuples(&dlm_vdivs, ARRAY_SIZE(dlm_vdivs));
8ab929d6
SA
590 break;
591 default:
592 return SR_ERR_NA;
593 }
594
595 return SR_OK;
596}
597
598static int dlm_check_channels(GSList *channels)
599{
600 GSList *l;
601 struct sr_channel *ch;
602 gboolean enabled_pod1, enabled_chan4;
603
604 enabled_pod1 = enabled_chan4 = FALSE;
605
606 /* Note: On the DLM2000, CH4 and Logic are shared. */
607 /* TODO Handle non-DLM2000 models. */
608 for (l = channels; l; l = l->next) {
609 ch = l->data;
610 switch (ch->type) {
611 case SR_CHANNEL_ANALOG:
612 if (ch->index == 3)
613 enabled_chan4 = TRUE;
614 break;
615 case SR_CHANNEL_LOGIC:
616 enabled_pod1 = TRUE;
617 break;
618 default:
619 return SR_ERR;
620 }
621 }
622
623 if (enabled_pod1 && enabled_chan4)
624 return SR_ERR;
625
626 return SR_OK;
627}
628
8ab929d6
SA
629static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
630{
631 GSList *l;
632 gboolean digital_added;
633 struct sr_channel *ch;
634 struct dev_context *devc;
635 struct sr_scpi_dev_inst *scpi;
636
637 (void)cb_data;
638
ac10a927
SA
639 if (sdi->status != SR_ST_ACTIVE)
640 return SR_ERR_DEV_CLOSED;
8ab929d6
SA
641
642 scpi = sdi->conn;
643 devc = sdi->priv;
644 digital_added = FALSE;
645
646 g_slist_free(devc->enabled_channels);
647 devc->enabled_channels = NULL;
648
649 for (l = sdi->channels; l; l = l->next) {
650 ch = l->data;
651 if (!ch->enabled)
652 continue;
653 /* Only add a single digital channel. */
654 if (ch->type != SR_CHANNEL_LOGIC || !digital_added) {
655 devc->enabled_channels = g_slist_append(
ac10a927
SA
656 devc->enabled_channels, ch);
657 if (ch->type == SR_CHANNEL_LOGIC)
658 digital_added = TRUE;
8ab929d6
SA
659 }
660 }
661
662 if (!devc->enabled_channels)
663 return SR_ERR;
664
665 if (dlm_check_channels(devc->enabled_channels) != SR_OK) {
666 sr_err("Invalid channel configuration specified!");
667 return SR_ERR_NA;
668 }
669
af3487ec
SA
670 /* Request data for the first enabled channel. */
671 devc->current_channel = devc->enabled_channels;
672 dlm_channel_data_request(sdi);
673
0028d5a1
SA
674 /* Call our callback when data comes in or after 5ms. */
675 sr_scpi_source_add(sdi->session, scpi, G_IO_IN, 5,
8ab929d6
SA
676 dlm_data_receive, (void *)sdi);
677
678 return SR_OK;
679}
680
681static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
682{
683 struct dev_context *devc;
8ab929d6
SA
684 struct sr_datafeed_packet packet;
685
686 (void)cb_data;
687
688 packet.type = SR_DF_END;
689 packet.payload = NULL;
690 sr_session_send(sdi, &packet);
691
692 if (sdi->status != SR_ST_ACTIVE)
693 return SR_ERR_DEV_CLOSED;
694
695 devc = sdi->priv;
696
697 devc->num_frames = 0;
698 g_slist_free(devc->enabled_channels);
699 devc->enabled_channels = NULL;
af3487ec
SA
700
701 sr_scpi_source_remove(sdi->session, sdi->conn);
8ab929d6
SA
702
703 return SR_OK;
704}
10763937
SA
705
706SR_PRIV struct sr_dev_driver yokogawa_dlm_driver_info = {
707 .name = "yokogawa-dlm",
ac10a927 708 .longname = "Yokogawa DL/DLM",
10763937 709 .api_version = 1,
8ab929d6
SA
710 .init = init,
711 .cleanup = cleanup,
712 .scan = scan,
713 .dev_list = dev_list,
714 .dev_clear = dev_clear,
715 .config_get = config_get,
716 .config_set = config_set,
c65a021c 717 .config_channel_set = config_channel_set,
8ab929d6
SA
718 .config_list = config_list,
719 .dev_open = dev_open,
720 .dev_close = dev_close,
721 .dev_acquisition_start = dev_acquisition_start,
722 .dev_acquisition_stop = dev_acquisition_stop,
41812aca 723 .context = NULL,
10763937 724};