~ubuntu-branches/ubuntu/utopic/xoscope/utopic

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
/*
 * @(#)$Id: comedi.c,v 2.4 2009/01/17 06:44:55 baccala Exp $
 *
 * Author: Brent Baccala <baccala@freesoft.org>
 *
 * Public domain.
 *
 * This file implements the COMEDI interface for xoscope
 *
 * The capturing occurs in what a normal oscilloscope would call "chop
 * mode" - samples are alternated between the various channels being
 * captured.  This has the effect of reducing the overall sampling
 * rate by a factor equal to the number of channels being captured.
 * You could also (but we don't) implement an "alt mode" - an entire
 * sweep is taken from one channel, then the entire next sweep is
 * taken from the next channel, etc.  Triggering would be a problem.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/poll.h>
#include <asm/page.h>
#include <comedilib.h>
#include "oscope.h"		/* program defaults */
#include "func.h"

#define COMEDI_RANGE 0		/* XXX user should set this */

/* Some of these variables aren't defined static because we need to
 * get at them from our GTK dialog callbacks.  None of them are set
 * from there, that's done via set_option(), but there are read, and
 * for that reason need to be global.
 */

comedi_t *comedi_dev = NULL;
gchar *comedi_board_name = NULL;

static int comedi_opened = 0;	/* t if open has at least been _attempted_ */
static int comedi_running = 0;
static int comedi_error = 0;

/* device_name[] is an array we write the COMEDI device into if it is
 * set by an option (then point device at device_name).  If there's
 * no option, device stays pointing to the default - /dev/comedi0
 */

static char device_name[256];
char *comedi_devname = "/dev/comedi0";

int comedi_subdevice = 0;

int comedi_rate = 50000;	/* XXX set this to max valid upon open */

/* Size of COMEDI's kernel buffer.  -1 means leave it at the default.
 * Slight bug - if you change it, then change it back to -1, the
 * default setting won't return until you close and re-open the
 * device.  Actually, that might not be good enough - you might
 * need to re-configure the device.
 */

int comedi_bufsize = -1;

#define BUFSZ 1024
static sampl_t buf[BUFSZ];
static int bufvalid=0;

// has to be set later
int zero_value = -1;

static int lag = 0;			/* lag - see get_data() */

static int subdevice_flags = 0;
static int subdevice_type = COMEDI_SUBD_UNUSED;

int comedi_aref = AREF_GROUND;		/* Global voltage reference setting */

/* This structure associates xoscope Signal structures with the COMEDI
 * channel they are receiving on.  capture_list gets emptied by
 * close_comedi(), added to by enable_chan(), deleted from by
 * disable_chan(), used by start_comedi_running() to build a channel
 * list, and used by get_data() to figure out in which Signal(s) to
 * put the data.
 */

#define NCHANS 8

struct capture {
  struct capture * next;
  int chan;
  Signal * signal;
};

static struct capture *capture_list = NULL;

static int active_channels=0;

static Signal comedi_chans[NCHANS];

/* Triggering information - the (one) channel we're triggering on, the
 * sample level, the type of trigger (0 - freerun, 1 - ascending,
 * 2 - descending), and the index of the trigger channel in the
 * capture list
 */

static int trig_chan = 0;
static int trig_level = 0;
static int trig_mode = 0;
static int trig_index = -1;
 
/* This function is defined as do-nothing and weak, meaning it can be
 * overridden by the linker without error.  It's used to start the X
 * Windows GTK options dialog for COMEDI, and is defined in this way
 * so that this object file can be used either with or without GTK.
 * If this causes compiler problems, just comment out the attribute
 * line and leave the do-nothing function.  You will then need to
 * comment out both lines to generate an object file that can be used
 * with GTK.
 */

void comedi_gtk_options() __attribute__ ((weak));
void comedi_gtk_options() {}

/* This function gets called at various points that we need to stop
 * and restart COMEDI, like a rate change, or a change to the list
 * of channels we're capturing for.  start_comedi_running() gets
 * called automatically by get_data(), so we can use stop_comedi_running()
 * pretty liberally.
 */

static void stop_comedi_running(void)
{
  if (comedi_running) {
    comedi_cancel(comedi_dev, 0);
    comedi_running = 0;
  }
  bufvalid = 0;
}

/* XXX This function should make sure the Signal arrays are reset to sane
 * values.  Right now, it just sets their volt and rate values.
 */

static int start_comedi_running(void)
{
  int try;
  int ret = -1;
  comedi_cmd cmd;
  unsigned int chanlist[NCHANS];
  struct capture *capture;
  comedi_range *comedi_rng;
  int maxdata;

  if (!comedi_dev) return 0;

  /* There might have been an error condition that was cleared (like
   * switching from an unsupported subdevice to a supported one), so
   * clear comedi_error here and set it if there's a problem later.
   * If we're not capturing anything, make sure we set subdevice_type
   * before we return, because nchans() depends on this variable to
   * figure out how to interpret comedi_get_n_channels()
   */

  comedi_error = 0;

  subdevice_flags = comedi_get_subdevice_flags(comedi_dev, comedi_subdevice);
  subdevice_type = comedi_get_subdevice_type(comedi_dev, comedi_subdevice);

  if (active_channels == 0) return 0;

  if (comedi_bufsize > 0) {
    /* comedi 0.7.66 has a bug in its buffer size handling.  Not only
     * does it fail to round up to a multiple of PAGE_SIZE correctly,
     * but if you attempt to set a buffer smaller than PAGE_SIZE, it
     * will deallocate the buffer and you'll never get it back without
     * re-configuring the device.  We round up to PAGE_SIZE here to
     * avoid the bug.  This is the only reason we need <asm/page.h> in
     * our include list.
     */
    comedi_bufsize = (comedi_bufsize + PAGE_SIZE - 1) & PAGE_MASK;
    ret = comedi_set_buffer_size(comedi_dev, comedi_subdevice, comedi_bufsize);
    if (ret < 0) {
      comedi_error = comedi_errno();
      return ret;
    }
  }

  /* Now we build a COMEDI command structure */

  bzero(&cmd, sizeof(cmd));
  cmd.subdev = comedi_subdevice;

  /* Start with a channel list based on capture_list
   *
   * This code matches up with get_data(), which assumes that the captured
   * data is in the same order as the channels in the capture_list
   */

  cmd.chanlist = chanlist;
  cmd.chanlist_len = 0;

  for (capture = capture_list; capture != NULL; capture = capture->next) {
    chanlist[cmd.chanlist_len++] = CR_PACK(capture->chan,0,comedi_aref);
  }

  if (cmd.chanlist_len == 0) {
    return 0;
  }

  /* comedilib has a comedi_get_cmd_generic_timed() function, but it's
   * set up for sampling a single channel, so I don't use it.
   * Instead, I try several different varients on comedi command
   * structures in the hopes of finding one that works.
   */

  try = 0;
  do {

    switch (try) {

      /* The first thing we try is to simultaneously sample (that's
       * the convert_src of TRIG_NOW) all the channels at the
       * requested rate.
       */

    case 0:
      cmd.start_src = TRIG_NOW;
      cmd.start_arg = 0;

      cmd.scan_begin_src = TRIG_TIMER;
      cmd.scan_begin_arg = 1e9 / comedi_rate;

      cmd.convert_src = TRIG_NOW;
      cmd.convert_arg = 0;

      cmd.scan_end_src = TRIG_COUNT;
      cmd.scan_end_arg = cmd.chanlist_len;

      cmd.stop_src = TRIG_NONE;
      cmd.stop_arg = 0;

      break;

      /* There's a good chance that won't work (not many cards support it).
       * So now try sampling each channel at a staggered interval of
       * the requested rate times the number of channels.
       */

    case 1:
      cmd.start_src = TRIG_NOW;
      cmd.start_arg = 0;

      cmd.scan_begin_src = TRIG_FOLLOW;
      cmd.scan_begin_arg = 0;

      cmd.convert_src = TRIG_TIMER;
      cmd.convert_arg = 1e9 / (comedi_rate * active_channels);

      cmd.scan_end_src = TRIG_COUNT;
      cmd.scan_end_arg = cmd.chanlist_len;

      cmd.stop_src = TRIG_NONE;
      cmd.stop_arg = 0;

      break;

      /* OK, that didn't work.  Maybe the card wants timers on
       * both the scan and conversion?
       */

    case 2:
      cmd.start_src = TRIG_NOW;
      cmd.start_arg = 0;

      cmd.scan_begin_src = TRIG_TIMER;
      cmd.scan_begin_arg = 1e9 / comedi_rate;

      cmd.convert_src = TRIG_TIMER;
      cmd.convert_arg = 1e9 / (comedi_rate * active_channels);

      cmd.scan_end_src = TRIG_COUNT;
      cmd.scan_end_arg = cmd.chanlist_len;

      cmd.stop_src = TRIG_NONE;
      cmd.stop_arg = 0;

      break;

      /* Nothing we tried worked!  There are other possibilities, but
       * none are currently supported by this code.  Complain and
       * return the error code from the last thing we tried.
       */

    default:

      comedi_error = comedi_errno();
      return ret;

    }

    /* COMEDI command testing can be a little funky.  We get a return
     * code indicating which phase of test failed.  Basically, if
     * phase 1 or 2 failed, we're screwed.  If phase 3 failed, it
     * might be because we've pushed the limits of the timing past
     * where it can go, and if phase 4 failed, it's just because the
     * device can't support exactly the timings we asked for.  In
     * either of the last two cases, the driver adjusts the offending
     * parameters.  That's why we call this function three times.
     */

    ret = comedi_command_test(comedi_dev,&cmd);
    ret = comedi_command_test(comedi_dev,&cmd);
    ret = comedi_command_test(comedi_dev,&cmd);

    try ++;

  } while (ret != 0);

  /* Now we adjust our global rate to whatever we got the card to do. */

  if (cmd.scan_begin_src == TRIG_TIMER) {
    comedi_rate = 1e9 / cmd.scan_begin_arg;
  } else if (cmd.convert_src == TRIG_TIMER) {
    comedi_rate = 1e9 / cmd.convert_arg;
    comedi_rate /= active_channels;
  } else {
    fprintf(stderr, "neither convert_src nor start_src is TRIG_TIMER!?!\n");
  }

  /* Voltage range is currently a global setting.  Find it, and save
   * it into all the Signal(s) we're collecting data into (if we're
   * capturing an analog input subdevice; digital subdevs don't do
   * this).  Signal->volts should be in milivolts per 320 sample
   * values, so take the voltage range given by COMEDI, multiply by
   * 1000 (volts -> millivolts), divide by 2^(sampl_t bits) (sample
   * values in an sampl_t), to get millivolts per sample value, and
   * multiply by 320 to get millivolts per 320 sample values.  320 is
   * the size of the vertical display area, in case you wondered.
   *
   * Also, set the rate (samples/sec) at which we'll capture data
   */

  for (capture = capture_list; capture != NULL; capture = capture->next) {

    if (subdevice_type == COMEDI_SUBD_AI) {

      comedi_rng = comedi_get_range(comedi_dev,
				    comedi_subdevice,
				    capture->chan, COMEDI_RANGE);
      maxdata=comedi_get_maxdata(comedi_dev,
				 comedi_subdevice,
				 0);
      capture->signal->volts
	= (comedi_rng->max - comedi_rng->min)
	* 1000 * 320 / maxdata;

      if (zero_value<0) {
	      // we have to set zero value
	      if ((comedi_rng->min<0)&&(comedi_rng->max>0)) {
		      // we are bipolar
		      zero_value=maxdata/2;
	      } else {
		      // we are unipolar
		      zero_value=0;
	      }
      }

      capture->signal->bits = 0;

#if 0
      printf(" [%g,%g] %s\n",comedi_rng->min,comedi_rng->max,
	     comedi_rng->unit == UNIT_volt ? "V" : "");
#endif

    } else {

      capture->signal->bits = comedi_get_n_channels(comedi_dev, comedi_subdevice);

      capture->signal->volts = 0;

    }

    capture->signal->rate = comedi_rate;
  }

#if 0
  fprintf(stderr, "Sampling every %d(%d) ns(Hz)\n",
	  cmd.convert_arg, comedi_rate);
#endif

  ret = comedi_command(comedi_dev,&cmd);
  if (ret >= 0) {
    fcntl(comedi_fileno(comedi_dev), F_SETFL, O_NONBLOCK);
    comedi_running = 1;
  } else {
    comedi_error = comedi_errno();
  }
  return ret;
}

static void
close_comedi()
{
#if 0
  struct capture *capture;
#endif

  if (comedi_dev) comedi_close(comedi_dev);
  comedi_dev = NULL;
  if (comedi_board_name) g_free(comedi_board_name);
  comedi_board_name = NULL;
  comedi_running = 0;
  comedi_opened = 0;

  /* Leave active channels alone here in case we're closing
   * a device because of an error and want to re-open later.
   */

#if 0
  while (capture_list != NULL) {
    capture = capture_list->next;
    free(capture_list);
    capture_list = capture;
  }

  active_channels = 0;
#endif
}

static int
open_comedi(void)
{
  int i;
  static int once=0;

  close_comedi();
  comedi_error = 0;
  comedi_opened = 1;
  subdevice_flags = 0;
  subdevice_type = COMEDI_SUBD_UNUSED;

  if (!once) {

    /* XXX once is a kludge */

    /* Setup the Signal structures.  Note that the name is set to 'a',
     * 'b', 'c', etc, to conform with xoscope usage and to avoid
     * confusing the user with the display channels.  COMEDI, of
     * course, numbers its channels 0, 1, 2, etc
     */

    for (i = 0 ; i < NCHANS ; i++) {		/* XXX hardwired at 8 */
      comedi_chans[i].data = NULL;
      comedi_chans[i].num = comedi_chans[i].frame = comedi_chans[i].volts = 0;
      comedi_chans[i].listeners = 0;
      //sprintf(comedi_chans[i].name, "Channel %d", i);
      sprintf(comedi_chans[i].name, "Channel %c", 'a' + i);
      comedi_chans[i].savestr[0] = 'a' + i;
      comedi_chans[i].savestr[1] = '\0';
    }

    once = 1;
  }

  comedi_dev = comedi_open(comedi_devname);

  if (! comedi_dev) {
    comedi_error = comedi_errno();
    return 0;
  }

  /* All the GTK stuff uses UTF8, and complains to stderr if it
   * doesn't get it.
   */

  comedi_board_name = g_locale_to_utf8(comedi_get_board_name(comedi_dev),
				       -1, NULL, NULL, NULL);

  /* XXX I'd kinda like to do this here, but then the read() loop
   * below (to get offset correction for the DAQP) returns errors
   * (-EAGAIN).  If do this later, and start_comedi_running() has
   * problems, then it might hang in get_data(), but I hope I've
   * fixed that now...
   */
  /* fcntl(comedi_fileno(comedi_dev), F_SETFL, O_NONBLOCK); */

  if (comedi_board_name && strncmp(comedi_board_name, "DAQP", 4) == 0) {

    /* Special case for DAQP - unfortunately, COMEDI doesn't (yet) provide
     * a generic interface for boards that can do offset correction,
     * so this special case is designed to handle the Quatech DAQP.
     * We collect a hundred samples from channel 4 in differential
     * mode, a non-existant channel used by the DAQP specifically for
     * offset correction.
     */

    comedi_cmd cmd;
    unsigned int chan;
    int ret;

    ret = comedi_get_cmd_generic_timed(comedi_dev, comedi_subdevice, &cmd, 0);

    if (ret >= 0) {
      chan = CR_PACK(4,0,AREF_DIFF);
      cmd.chanlist = &chan;
      cmd.chanlist_len = 1;
      cmd.start_src = TRIG_NOW;
      cmd.start_arg = 0;
      cmd.stop_src = TRIG_COUNT;
      cmd.stop_arg = 100;

      ret = comedi_command_test(comedi_dev, &cmd);

      if (ret >= 0) {
	ret = comedi_command(comedi_dev, &cmd);
	if (ret >= 0) {
	  int i = 0;
	  while ((i < (100 * sizeof(sampl_t)))
		 && (ret = read(comedi_fileno(comedi_dev), buf,
				100 * sizeof(sampl_t) - i)) > 0) {
	    i += ret;
	  }
	  if (i == (100 * sizeof(sampl_t))) {
	    zero_value = 0;
	    for (i=0; i<100; i++) zero_value += buf[i];
	    zero_value /= 100;
	  }
	}
      }
    }

    if (ret == -1) {
      comedi_error = comedi_errno();
      /* close_comedi(); */
      return 0;
    }

    comedi_cancel(comedi_dev, 0);
  }

  /* XXX Why can't we do this here?  It doesn't seem to "take" */
  /* fcntl(comedi_fileno(comedi_dev), F_SETFL, O_NONBLOCK); */

  if (start_comedi_running() < 0) {
    return 0;
  } else {
    return 1;
  }

}

void
reset_comedi(void)
{
  if (comedi_dev == NULL) {
    open_comedi();
    if (comedi_dev == NULL) {
      return;
    }
  }

  stop_comedi_running();
}

static int nchans(void)
{
  int chans;
  int i;

  if (! comedi_opened) open_comedi();

  /* open_comedi() calls start_comedi_running() just before it
   * returns, so that's how we know subdevice_type has been set
   * correctly when we get here.  However, I really don't know if
   * open_comedi() SHOULD call start_comedi_running() at all, so we
   * may need to revisit this.
   */

  if (comedi_dev == NULL) {
    return 0;
  } else if (subdevice_type == COMEDI_SUBD_AI) {
    /* analog subdevice - mark all channels analog and return num of chans */
    chans = comedi_get_n_channels(comedi_dev, comedi_subdevice);
    for (i = 0; i < chans; i ++) comedi_chans[i].bits = 0;
    return chans;
  } else {
    /* digital subdevice - n_channels returns number of bits */
    comedi_chans[0].bits = comedi_get_n_channels(comedi_dev, comedi_subdevice);
    return 1;
  }
}

static int fd(void)
{
  return (comedi_running ? comedi_fileno(comedi_dev) : -1);
}

/* reset() - part of the data source API.  Called when we're ready to
 * start capturing.  Clears the old capture_list and builds a new one.
 * capture_ptr is used to make sure we build the list from the top
 * down, not the bottom up, mainly to make sure trig_index counts from
 * the top down.  Finally, we start COMEDI.  We don't really need to
 * start COMEDI, just prep it, but we start it in order to set the
 * rate and volts fields (during start_comedi_running) in the Signal
 * structures.
 */

static void
reset(void)
{
  struct capture *capture;
  struct capture **capture_ptr;
  int i;

  stop_comedi_running();

  for (capture = capture_list; capture != NULL; capture = capture_list) {
    capture_list = capture->next;
    free(capture);
  }

  capture_list = NULL;
  active_channels = 0;
  trig_index = -1;
  capture_ptr = &capture_list;

  for (i = 0; i < NCHANS; i++) {
    if ((comedi_chans[i].listeners) || ((trig_mode > 0) && (trig_chan == i))) {

      capture = malloc(sizeof(struct capture));
      if (capture == NULL) {
	perror("enable_chan() malloc failed");
	exit(1);
      }

      capture->chan = i;
      capture->signal = &comedi_chans[i];
      capture->next = NULL;
      *capture_ptr = capture;
      capture_ptr = &capture->next;

      comedi_chans[i].num = 0;
      comedi_chans[i].frame ++;

      if ((trig_mode > 0) && (trig_chan == i)) trig_index = active_channels;

      active_channels ++;
    }
  }

  start_comedi_running();
}

static Signal * comedi_chan(int chan)
{
  return &comedi_chans[chan];
}

static int set_trigger(int chan, int *levelp, int mode)
{
  trig_chan = chan;
  trig_level = *levelp;
  trig_mode = mode;
  /* XXX check that trig_level is within subdevice's range */
  return 1;
}

static void clear_trigger(void)
{
  trig_mode = 0;
}

/* Current COMEDI rate logic has some bizarre effects.  As we increase
 * the number of channels sampled, the rate goes down (usually), but
 * doesn't go back up when we decrease the number of sampled chans.
 */

/* XXX the rate we calculate might not be the one that actually gets used */

static int change_rate(int dir)
{
  int oldrate = comedi_rate;

  if (dir == 1) {
    comedi_rate *= 2;
  } else {
    comedi_rate /= 2;
  }

  stop_comedi_running();

  return (comedi_rate != oldrate);
}

/* set_width(int)
 *
 * sets the frame width (number of samples captured per sweep) globally
 * for all the channels.
 */

static void set_width(int width)
{
  int i;

  for (i=0; i<NCHANS; i++) {
    comedi_chans[i].width = width;
    if (comedi_chans[i].data != NULL) free(comedi_chans[i].data);
    comedi_chans[i].data = malloc(width * sizeof(short));
  }
}

/* get_data() -
 * read all available data from comedi device, return value is TRUE if we
 * actually put some samples into the sweep buffer (and thus need a redisplay)
 *
 * This function should always return at the end of a sweep
 */

#define convert(sample) (sample - zero_value)

static int get_data(void)
{
  int bytes_read;
  int samples_read;
  int scans_read;
  int samples_per_frame;
  sampl_t *current_scan, *last_scan;
  int i, j;
  int delay;
  int triggered=0;
  int was_in_sweep=in_progress;
  static struct timeval tv1, tv2;
  struct capture *capture;

  /* This code used to try and start COMEDI running if it wasn't running
   * already.  But if fd() already returned -1, the main code doesn't
   * think we're running, so it's best to leave things alone here...
   */

  if (! comedi_dev || ! comedi_running) return 0;

  /* The way the code's written right now, all the channels are
   * sampled at the same rate and for the same width (number of
   * samples per frame), so we just use the width from the first
   * channel in the capture list to figure how many samples we're
   * capturing.
   */

  samples_per_frame = capture_list->signal->width;

  /* It is possible for this loop to be entered with a full buffer of
   * data already (bufvalid == sizeof(buf)).  In that case, the read will
   * be called with a zero byte buffer size, and will return zero.
   * That's why the comparison reads ">=0" and not ">0"
   */
  while ((bytes_read = read(comedi_fileno(comedi_dev),
			    ((char *)buf) + bufvalid, sizeof(buf) - bufvalid))
	 >= 0) {

    // fprintf(stderr, "bytes_read=%d; bufvalid=%d\n", bytes_read, bufvalid);

    bytes_read += bufvalid;

    gettimeofday(&tv1, NULL);
    samples_read = bytes_read / sizeof(sampl_t);
    scans_read = samples_read / active_channels;

    /* This is here to catch the case when there's nothing (or not
     * much) in the buffer, and the read() call returned nothing.
     */

    if (scans_read == 0 && bytes_read == 0) break;

    for (i = 0; i < scans_read; i++) {

      current_scan = buf + i * active_channels;

      if (!in_progress && scope.run && i>0) {

	/* Sweep isn't in_progress, so look for a trigger -
	 * anything (trig_mode==0) or a transition between the last
	 * sample and the current one that crossed the trig_level
	 * threshold, either going positive (trig_mode==1) or going
	 * negative (trig_mode==2).  Since we check the previous sample,
	 * there's an "i>0" case in the above if statement, and that
	 * does mean that we'll miss a trigger if the transition
	 * exactly corresponds with a read buffer boundary.
	 */

	last_scan = buf + (i-1) * active_channels;

	if ((trig_mode == 0) ||
	    ((trig_mode == 1) &&
	     (convert(current_scan[trig_index]) >= trig_level) &&
	     (convert(last_scan[trig_index]) < trig_level)) ||
	    ((trig_mode == 2) &&
	     (convert(current_scan[trig_index]) <= trig_level) &&
	     (convert(last_scan[trig_index]) > trig_level))) {

	  /* found something to trigger on, so compute a delay value
	   * based on extrapolating a straight line between the two
	   * sample values that straddle the triggering point, for
	   * high-frequency signals that change significantly between
	   * the two samples.  Set up all the relevent Signal
	   * structures, then fall through into the triggered case
	   * below
	   */

	  delay = 0;

	  if (trig_mode != 0) {
	    short current = convert(current_scan[trig_index]);
	    short last = convert(last_scan[trig_index]);
	    if (current != last) {
	      delay = abs(10000 * (current - trig_level) / (current - last));
	    }
	  }

	  for (j=0, capture=capture_list;
	       capture != NULL; capture=capture->next, j++) {
	    capture->signal->frame ++;
	    capture->signal->delay = delay;
	    capture->signal->num = 0;
	  }
	  if (j != active_channels) {
	    fprintf(stderr, "ERROR!   j != active_channels in get_data()\n");
	  }

	  in_progress = 1;

	}
      }

      if (in_progress) {

	/* Sweep in progress */

	for (j=0, capture=capture_list;
	     capture != NULL; capture=capture->next, j++) {
	  capture->signal->data[capture->signal->num ++]
	    = convert(current_scan[j]);
	  in_progress = capture->signal->num;
	}
	if (j != active_channels) {
	  fprintf(stderr, "ERROR!   j != active_channels in get_data()\n");
	}

	triggered = 1;

	if (in_progress >= samples_per_frame) {

	  in_progress = 0;

	  /* If we were in the middle of a sweep when we entered this function,
	   * return now.  Otherwise, keep looking for more sweeps.
	   */

	  if (was_in_sweep) {

	    bufvalid = bytes_read - (i * active_channels * sizeof(sampl_t));
	    if (bufvalid) {
	      memcpy(buf, (char *) buf + bytes_read - bufvalid, bufvalid);
	    }

	    lag = 0;
	    return triggered;
	  }
	}

      }
    }

    /* It would be nice if COMEDI never returned a partial scan
     * to a read() call.  Unfortunately, it often does, so we
     * need to tuck the "extra" data away until the next time
     * through this loop...
     */

    bufvalid = bytes_read - (scans_read * active_channels * sizeof(sampl_t));
    if (bufvalid) {
      memcpy(buf, (char *) buf + bytes_read - bufvalid, bufvalid);
    }

  }

  if ((bytes_read < 0) && (errno != EAGAIN)) {

    /* The most common cause of a COMEDI read error is a buffer
     * overflow.  There are all kinds of ways to do it, from hitting
     * space to stop the scope trace to dragging a window while
     * xoscope is running.  In the later case, the window manager will
     * do an X server grab, which will block xoscope the next time it
     * tries to perform an X operation.  Holding the mouse down longer
     * than a split second will cause the COMEDI kernel buffer to
     * overflow, which will trigger this code the next time through.
     *
     * comedi-0.7.60 returned EINVAL on buffer overflows;
     * comedi-0.7.66 returns EPIPE
     *
     * In any event, if we detect an overflow, we reset the capture to
     * start a new trace, record how many microseconds elapsed since
     * the last time we were able to read the device, and report this
     * on the screen as "lag".
     */

    if (errno != EINVAL && errno != EPIPE) perror("comedi read");

    start_comedi_running();
    bufvalid = 0;
    gettimeofday(&tv2, NULL);
    lag = 1000000*(tv2.tv_sec-tv1.tv_sec) + tv2.tv_usec - tv1.tv_usec;
    return 0;
  }

  lag = 0;
  return triggered;
}


static char * status_str(int i)
{
  static char buffer[16];
  char *error = comedi_strerror(comedi_error);

  switch (i) {
  case 0:
    return comedi_devname;
  case 2:
    if (comedi_dev) {
      return comedi_board_name;
    } else {
      return split_field(error, 0, 16);
    }

  case 4:
    if (!comedi_dev) {
      return split_field(error, 1, 16);
    } else {
      return "";
    }

  case 1:
    if (comedi_dev) {
      sprintf(buffer, "Subdevice %d", comedi_subdevice);
      return buffer;
    } else {
      return "";
    }
  case 3:
    if (comedi_dev && comedi_error) {
      return split_field(error, 0, 16);
    } else if (lag > 1000) {
      snprintf(buffer, sizeof(buffer), "%d ms lag", lag/1000);
    } else if (lag > 0) {
      snprintf(buffer, sizeof(buffer), "%d \302\265s lag", lag);
    } else {
      return "";
    }
  case 5:
    if (comedi_dev && comedi_error) {
      return split_field(error, 1, 16);
    } else {
      return "";
    }

  default:
    return NULL;
  }
}


/* Option 1 key - global analog reference toggle
 *
 * Analog COMEDI devices typically can select between different
 * references (which signal line is treated as zero point).
 */

static int option1(void)
{
  if (comedi_aref == AREF_GROUND && subdevice_flags & SDF_DIFF)
    comedi_aref = AREF_DIFF;
  else if (comedi_aref == AREF_GROUND && subdevice_flags & SDF_COMMON)
    comedi_aref = AREF_COMMON;
  else if (comedi_aref == AREF_DIFF && subdevice_flags & SDF_COMMON)
    comedi_aref = AREF_COMMON;
  else if (comedi_aref == AREF_DIFF && subdevice_flags & SDF_GROUND)
    comedi_aref = AREF_GROUND;
  else if (comedi_aref == AREF_COMMON && subdevice_flags & SDF_GROUND)
    comedi_aref = AREF_GROUND;
  else if (comedi_aref == AREF_COMMON && subdevice_flags & SDF_DIFF)
    comedi_aref = AREF_DIFF;
  else
    return 0;

  return 1;
}

static char * option1str(void)
{
  if (! (subdevice_flags & (SDF_GROUND | SDF_DIFF | SDF_COMMON))) {
    return NULL;
  } else if (comedi_aref == AREF_GROUND) {
    return "AREF_GROUND";
  } else if (comedi_aref == AREF_DIFF) {
    return "AREF_DIFF";
  } else if (comedi_aref == AREF_COMMON) {
    return "AREF_COMMON";
  } else {
    return "AREF unknown";
  }
}


#if 0

/* XXX Option 2 key - use this for a per-channel Range setting?
 *
 * If so, would also need to add option strings below.
 */


static int option2(void)
{
  return 0;
}

static char * option2str(void)
{
  return NULL;
}

#endif

static int comedi_set_option(char *option)
{
  char buf[256];
  char *p = buf;

  do {
    *p++ = tolower(*option);
  } while (*option++ && p < buf + sizeof(buf));

  if (sscanf(buf, "rate=%d", &comedi_rate) == 1) {
    reset_comedi();
    return 1;
  } else if (sscanf(buf, "device=%s", device_name) == 1) {
    comedi_devname = device_name;
    close_comedi();
    /* reset_comedi(); */
    open_comedi();
    return 1;
  } else if (sscanf(buf, "subdevice=%d", &comedi_subdevice) == 1) {
    reset_comedi();
    return 1;
  } else if (strcasecmp(buf, "aref=ground") == 0) {
    comedi_aref = AREF_GROUND;
    reset_comedi();
    return 1;
  } else if (strcasecmp(buf, "aref=diff") == 0) {
    comedi_aref = AREF_DIFF;
    reset_comedi();
    return 1;
  } else if (strcasecmp(buf, "bufsize=default") == 0) {
    comedi_bufsize = -1;
    return 1;
  } else if (sscanf(buf, "bufsize=%d", &comedi_bufsize) == 1) {
    reset_comedi();
    return 1;
  } else {
    return 0;
  }
}

static char * comedi_save_option(int i)
{
  static char buf[256];

  switch (i) {
  case 0:
    snprintf(buf, sizeof(buf), "rate=%d", comedi_rate);
    return buf;

  case 1:
    snprintf(buf, sizeof(buf), "device=%s", comedi_devname);
    return buf;

  case 2:
    snprintf(buf, sizeof(buf), "subdevice=%d", comedi_subdevice);
    return buf;

  case 3:
    switch (comedi_aref) {
    case AREF_GROUND:
      return "aref=ground";
    case AREF_DIFF:
      return "aref=diff";
    case AREF_COMMON:
      return "aref=common";
    default:
      return "";
    }

  case 4:
    if (comedi_bufsize > 0) {
      snprintf(buf, sizeof(buf), "bufsize=%d", comedi_bufsize);
      return buf;
    } else {
      return "bufsize=default";
    }

  default:
    return NULL;
  }
}

DataSrc datasrc_comedi = {
  "COMEDI",
  nchans,
  comedi_chan,
  set_trigger,
  clear_trigger,
  change_rate,
  set_width,
  reset,
  fd,
  get_data,
  status_str,
  option1,
  option1str,
#if 0
  option2,
  option2str,
#else
  NULL,
  NULL,
#endif
  comedi_set_option,
  comedi_save_option,
  comedi_gtk_options,
};