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