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