Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
/*	$NetBSD: refclock_true.c,v 1.6 2016/01/08 21:35:39 christos Exp $	*/

/*
 * refclock_true - clock driver for the Kinemetrics/TrueTime receivers
 *	Receiver Version 3.0C - tested plain, with CLKLDISC
 *	Development work being done:
 *      - Support TL-3 WWV TOD receiver
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#if defined(REFCLOCK) && defined(CLOCK_TRUETIME)

#include <stdio.h>
#include <ctype.h>

#include "ntpd.h"
#include "ntp_io.h"
#include "ntp_refclock.h"
#include "ntp_unixtime.h"
#include "ntp_stdlib.h"

#ifdef SYS_WINNT
extern int async_write(int, const void *, unsigned int);
#undef write
#define write(fd, data, octets)	async_write(fd, data, octets)
#endif

/* This should be an atom clock but those are very hard to build.
 *
 * The PCL720 from P C Labs has an Intel 8253 lookalike, as well as a bunch
 * of TTL input and output pins, all brought out to the back panel.  If you
 * wire a PPS signal (such as the TTL PPS coming out of a GOES or other
 * Kinemetrics/Truetime clock) to the 8253's GATE0, and then also wire the
 * 8253's OUT0 to the PCL720's INPUT3.BIT0, then we can read CTR0 to get the
 * number of uSecs since the last PPS upward swing, mediated by reading OUT0
 * to find out if the counter has wrapped around (this happens if more than
 * 65535us (65ms) elapses between the PPS event and our being called.)
 */
#ifdef CLOCK_PPS720
# undef min	/* XXX */
# undef max	/* XXX */
# include <machine/inline.h>
# include <sys/pcl720.h>
# include <sys/i8253.h>
# define PCL720_IOB 0x2a0	/* XXX */
# define PCL720_CTR 0		/* XXX */
#endif

/*
 * Support for Kinemetrics Truetime Receivers
 *	GOES:           (468-DC, usable with GPS->GOES converting antenna)
 *	GPS/TM-TMD:	
 *	XL-DC:		(a 151-602-210, reported by the driver as a GPS/TM-TMD)
 *	GPS-800 TCU:	(an 805-957 with the RS232 Talker/Listener module)
 *      TL-3:           3 channel WWV/H receiver w/ IRIG and RS-232 outputs
 *	OM-DC:		getting stale ("OMEGA")
 *
 * Most of this code is originally from refclock_wwvb.c with thanks.
 * It has been so mangled that wwvb is not a recognizable ancestor.
 *
 * Timcode format: ADDD:HH:MM:SSQCL
 *	A - control A		(this is stripped before we see it)
 *	Q - Quality indication	(see below)
 *	C - Carriage return
 *	L - Line feed
 *
 * Quality codes indicate possible error of
 *   468-DC GOES Receiver:
 *   GPS-TM/TMD Receiver: (default quality codes for XL-DC)
 *       ?     +/- 1  milliseconds	#     +/- 100 microseconds
 *       *     +/- 10 microseconds	.     +/- 1   microsecond
 *     space   less than 1 microsecond
 *   TL-3 Receiver: (default quality codes for TL-3)
 *       ?     unknown quality (receiver is unlocked)
 *     space   +/- 5 milliseconds
 *   OM-DC OMEGA Receiver: (default quality codes for OMEGA)
 *   WARNING OMEGA navigation system is no longer existent
 *       >     >+- 5 seconds
 *       ?     >+/- 500 milliseconds    #     >+/- 50 milliseconds
 *       *     >+/- 5 milliseconds      .     >+/- 1 millisecond
 *      A-H    less than 1 millisecond.  Character indicates which station
 *	       is being received as follows:
 *	       A = Norway, B = Liberia, C = Hawaii, D = North Dakota,
 *	       E = La Reunion, F = Argentina, G = Australia, H = Japan.
 *
 * The carriage return start bit begins on 0 seconds and extends to 1 bit time.
 *
 * Notes on 468-DC and OMEGA receiver:
 *
 * Send the clock a 'R' or 'C' and once per second a timestamp will
 * appear.  Send a 'P' to get the satellite position once (GOES only.)
 *
 * Notes on the 468-DC receiver:
 *
 * Since the old east/west satellite locations are only historical, you can't
 * set your clock propagation delay settings correctly and still use
 * automatic mode. The manual says to use a compromise when setting the
 * switches. This results in significant errors. The solution; use fudge
 * time1 and time2 to incorporate corrections. If your clock is set for
 * 50 and it should be 58 for using the west and 46 for using the east,
 * use the line
 *
 * fudge 127.127.5.0 time1 +0.008 time2 -0.004
 *
 * This corrects the 4 milliseconds advance and 8 milliseconds retard
 * needed. The software will ask the clock which satellite it sees.
 *
 * Notes on the TrueTime TimeLink TL-3 WWV TOD receiver:
 * 
 * This clock may be polled, or send one timecode per second.
 * That mode may be toggled via the front panel ("C" mode), or controlled
 * from the RS-232 port.  Send the receiver "ST1" to turn it on, and
 * "ST0" to turn it off.  Send "QV" to get the firmware revision (useful
 * for identifying this model.)
 * 
 * Note that it can take several polling cycles, especially if the receiver
 * was in the continuous timecode mode.  (It can be slow to leave that mode.)
 * 
 * ntp.conf parameters:
 * time1   - offset applied to samples when reading WEST satellite (default = 0)
 * time2   - offset applied to samples when reading EAST satellite (default = 0)
 * stratum - stratum to assign to this clock (default = 0)
 * refid   - refid assigned to this clock (default = "TRUE", see below)
 * flag1   - will silence the clock side of ntpd, just reading the clock
 *	     without trying to write to it.  (default = 0)
 * flag2   - generate a debug file /tmp/true%d.
 * flag3   - enable ppsclock streams module
 * flag4   - use the PCL-720 (BSD/OS only)
 */


/*
 * Definitions
 */
#define	DEVICE		"/dev/true%d"
#define	SPEED232	B9600	/* 9600 baud */

/*
 * Radio interface parameters
 */
#define	PRECISION	(-10)	/* precision assumed (about 1 ms) */
#define	REFID		"TRUE"	/* reference id */
#define	DESCRIPTION	"Kinemetrics/TrueTime Receiver"

/*
 * Tags which station (satellite) we see
 */
#define GOES_WEST	0	/* Default to WEST satellite and apply time1 */
#define GOES_EAST	1	/* until you discover otherwise */

/*
 * used by the state machine
 */
enum true_event	{e_Init, e_Huh, e_F18, e_F50, e_F51, e_Satellite,
		 e_TL3, e_Poll, e_Location, e_TS, e_Max};
const char *events[] = {"Init", "Huh", "F18", "F50", "F51", "Satellite",
			"TL3", "Poll", "Location", "TS"};
#define eventStr(x) (((int)x<(int)e_Max) ? events[(int)x] : "?")

enum true_state	{s_Base, s_InqTM, s_InqTCU, s_InqOmega, s_InqGOES,
		 s_InqTL3, s_Init, s_F18, s_F50, s_Start, s_Auto, s_Max};
const char *states[] = {"Base", "InqTM", "InqTCU", "InqOmega", "InqGOES",
			"InqTL3", "Init", "F18", "F50", "Start", "Auto"};
#define stateStr(x) (((int)x<(int)s_Max) ? states[(int)x] : "?")

enum true_type	{t_unknown, t_goes, t_tm, t_tcu, t_omega, t_tl3, t_Max};
const char *types[] = {"unknown", "goes", "tm", "tcu", "omega", "tl3"};
#define typeStr(x) (((int)x<(int)t_Max) ? types[(int)x] : "?")

/*
 * unit control structure
 */
struct true_unit {
	unsigned int	pollcnt;	/* poll message counter */
	unsigned int	station;	/* which station we are on */
	unsigned int	polled;		/* Hand in a time sample? */
	enum true_state	state;		/* state machine */
	enum true_type	type;		/* what kind of clock is it? */
	int		unit;		/* save an extra copy of this */
	FILE		*debug;		/* debug logging file */
#ifdef CLOCK_PPS720
	int		pcl720init;	/* init flag for PCL 720 */
#endif
};

/*
 * Function prototypes
 */
static	int	true_start	(int, struct peer *);
static	void	true_shutdown	(int, struct peer *);
static	void	true_receive	(struct recvbuf *);
static	void	true_poll	(int, struct peer *);
static	void	true_send	(struct peer *, const char *);
static	void	true_doevent	(struct peer *, enum true_event);

#ifdef CLOCK_PPS720
static	u_long	true_sample720	(void);
#endif

/*
 * Transfer vector
 */
struct	refclock refclock_true = {
	true_start,		/* start up driver */
	true_shutdown,		/* shut down driver */
	true_poll,		/* transmit poll message */
	noentry,		/* not used (old true_control) */
	noentry,		/* initialize driver (not used) */
	noentry,		/* not used (old true_buginfo) */
	NOFLAGS			/* not used */
};


#if !defined(__STDC__)
# define true_debug (void)
#else
NTP_PRINTF(2, 3)
static void
true_debug(struct peer *peer, const char *fmt, ...)
{
	va_list ap;
	int want_debugging, now_debugging;
	struct refclockproc *pp;
	struct true_unit *up;

	va_start(ap, fmt);
	pp = peer->procptr;
	up = pp->unitptr;

	want_debugging = (pp->sloppyclockflag & CLK_FLAG2) != 0;
	now_debugging = (up->debug != NULL);
	if (want_debugging != now_debugging)
	{
		if (want_debugging) {
			char filename[40];
			int fd;

			snprintf(filename, sizeof(filename),
				 "/tmp/true%d.debug", up->unit);
			fd = open(filename, O_CREAT | O_WRONLY | O_EXCL,
				  0600);
			if (fd >= 0 && (up->debug = fdopen(fd, "w"))) {
#ifdef HAVE_SETVBUF
				static char buf[BUFSIZ];

				setvbuf(up->debug, buf, _IOLBF, BUFSIZ);
#else
				setlinebuf(up->debug);
#endif
			}
		} else {
			fclose(up->debug);
			up->debug = NULL;
		}
	}

	if (up->debug) {
		fprintf(up->debug, "true%d: ", up->unit);
		vfprintf(up->debug, fmt, ap);
	}
	va_end(ap);
}
#endif /*STDC*/

/*
 * true_start - open the devices and initialize data for processing
 */
static int
true_start(
	int unit,
	struct peer *peer
	)
{
	register struct true_unit *up;
	struct refclockproc *pp;
	char device[40];
	int fd;

	/*
	 * Open serial port
	 */
	snprintf(device, sizeof(device), DEVICE, unit);
	fd = refclock_open(device, SPEED232, LDISC_CLK);
	if (fd <= 0)
		return 0;

	/*
	 * Allocate and initialize unit structure
	 */
	up = emalloc_zero(sizeof(*up));
	pp = peer->procptr;
	pp->io.clock_recv = true_receive;
	pp->io.srcclock = peer;
	pp->io.datalen = 0;
	pp->io.fd = fd;
	if (!io_addclock(&pp->io)) {
		close(fd);
		pp->io.fd = -1;
		free(up);
		return (0);
	}
	pp->unitptr = up;

	/*
	 * Initialize miscellaneous variables
	 */
	peer->precision = PRECISION;
	pp->clockdesc = DESCRIPTION;
	memcpy(&pp->refid, REFID, 4);
	up->pollcnt = 2;
	up->type = t_unknown;
	up->state = s_Base;

	/*
	 * Send a CTRL-C character at the start,
	 * just in case the clock is already
	 * sending timecodes
	 */
	true_send(peer, "\03\r");
	
	true_doevent(peer, e_Init);

	return (1);
}


/*
 * true_shutdown - shut down the clock
 */
static void
true_shutdown(
	int unit,
	struct peer *peer
	)
{
	register struct true_unit *up;
	struct refclockproc *pp;

	pp = peer->procptr;
	up = pp->unitptr;
	if (pp->io.fd != -1)
		io_closeclock(&pp->io);
	if (up != NULL)
		free(up);
}


/*
 * true_receive - receive data from the serial interface on a clock
 */
static void
true_receive(
	struct recvbuf *rbufp
	)
{
	register struct true_unit *up;
	struct refclockproc *pp;
	struct peer *peer;
	u_short new_station;
	char synced;
	int i;
	int lat, lon, off;	/* GOES Satellite position */
	/* These variables hold data until we decide to keep it */
	char	rd_lastcode[BMAX];
	l_fp	rd_tmp;
	u_short	rd_lencode;

	/*
	 * Get the clock this applies to and pointers to the data.
	 */
	peer = rbufp->recv_peer;
	pp = peer->procptr;
	up = pp->unitptr;

	/*
	 * Read clock output.  Automatically handles STREAMS, CLKLDISC.
	 */
	rd_lencode = refclock_gtlin(rbufp, rd_lastcode, BMAX, &rd_tmp);
	rd_lastcode[rd_lencode] = '\0';

	/*
	 * There is a case where <cr><lf> generates 2 timestamps.
	 */
	if (rd_lencode == 0)
		return;
	pp->lencode = rd_lencode;
	strlcpy(pp->a_lastcode, rd_lastcode, sizeof(pp->a_lastcode));
	pp->lastrec = rd_tmp;
	true_debug(peer, "receive(%s) [%d]\n", pp->a_lastcode,
		   pp->lencode);

	up->pollcnt = 2;
	record_clock_stats(&peer->srcadr, pp->a_lastcode);

	/*
	 * We get down to business, check the timecode format and decode
	 * its contents. This code decodes a multitude of different
	 * clock messages. Timecodes are processed if needed. All replies
	 * will be run through the state machine to tweak driver options
	 * and program the clock.
	 */

	/*
	 * Clock misunderstood our last command?
	 */
	if (pp->a_lastcode[0] == '?' ||
	    strcmp(pp->a_lastcode, "ERROR 05 NO SUCH FUNCTION") == 0) {
		true_doevent(peer, e_Huh);
		return;
	}

	/*
	 * Timecode: "nnnnn+nnn-nnn"
	 * (from GOES clock when asked about satellite position)
	 */
	if ((pp->a_lastcode[5] == '+' || pp->a_lastcode[5] == '-') &&
	    (pp->a_lastcode[9] == '+' || pp->a_lastcode[9] == '-') &&
	    sscanf(pp->a_lastcode, "%5d%*c%3d%*c%3d", &lon, &lat, &off) == 3
	    ) {
		const char *label = "Botch!";

		/*
		 * This is less than perfect.  Call the (satellite)
		 * either EAST or WEST and adjust slop accodingly
		 * Perfectionists would recalculate the exact delay
		 * and adjust accordingly...
		 */
		if (lon > 7000 && lon < 14000) {
			if (lon < 10000) {
				new_station = GOES_EAST;
				label = "EAST";
			} else {
				new_station = GOES_WEST;
				label = "WEST";
			}
				
			if (new_station != up->station) {
				double dtemp;

				dtemp = pp->fudgetime1;
				pp->fudgetime1 = pp->fudgetime2;
				pp->fudgetime2 = dtemp;
				up->station = new_station;
			}
		}
		else {
			/*refclock_report(peer, CEVNT_BADREPLY);*/
			label = "UNKNOWN";
		}
		true_debug(peer, "GOES: station %s\n", label);
		true_doevent(peer, e_Satellite);
		return;
	}

	/*
	 * Timecode: "Fnn"
	 * (from TM/TMD clock when it wants to tell us what it's up to.)
	 */
	if (sscanf(pp->a_lastcode, "F%2d", &i) == 1 && i > 0 && i < 80) {
		switch (i) {
		case 50:
			true_doevent(peer, e_F50);
			break;
		case 51:
			true_doevent(peer, e_F51);
			break;
		default:
			true_debug(peer, "got F%02d - ignoring\n", i);
			break;
		}
		return;
	}

        /*
         * Timecode: "VER xx.xx"
         * (from a TL3 when sent "QV", so id's it during initialization.)
         */
        if (pp->a_lastcode[0] == 'V' && pp->a_lastcode[1] == 'E' &&
            pp->a_lastcode[2] == 'R' && pp->a_lastcode[6] == '.') {
                true_doevent(peer, e_TL3);
                NLOG(NLOG_CLOCKSTATUS) {
                        msyslog(LOG_INFO, "TL3: %s", pp->a_lastcode);
                }
                return;
        }

	/*
	 * Timecode: " TRUETIME Mk III" or " TRUETIME XL"
	 * (from a TM/TMD/XL clock during initialization.)
	 */
	if (strncmp(pp->a_lastcode, " TRUETIME Mk III ", 17) == 0 ||
	    strncmp(pp->a_lastcode, " TRUETIME XL", 12) == 0) {
		true_doevent(peer, e_F18);
		NLOG(NLOG_CLOCKSTATUS) {
			msyslog(LOG_INFO, "TM/TMD/XL: %s", pp->a_lastcode);
		}
		return;
	}

	/*
	 * Timecode: "N03726428W12209421+000033"
	 *			1	   2
	 * index      0123456789012345678901234
	 * (from a TCU during initialization)
	 */
	if ((pp->a_lastcode[0] == 'N' || pp->a_lastcode[0] == 'S') &&
	    (pp->a_lastcode[9] == 'W' || pp->a_lastcode[9] == 'E') &&
	    pp->a_lastcode[18] == '+') {
		true_doevent(peer, e_Location);
		NLOG(NLOG_CLOCKSTATUS) {
			msyslog(LOG_INFO, "TCU-800: %s", pp->a_lastcode);
		}
		return;
	}
	/*
	 * Timecode: "ddd:hh:mm:ssQ"
	 *			1	   2
	 * index      0123456789012345678901234
	 * (from all clocks supported by this driver.)
	 */
	if (pp->a_lastcode[3] == ':' &&
	    pp->a_lastcode[6] == ':' &&
	    pp->a_lastcode[9] == ':' &&
	    sscanf(pp->a_lastcode, "%3d:%2d:%2d:%2d%c",
		   &pp->day, &pp->hour, &pp->minute,
		   &pp->second, &synced) == 5) {

		/*
		 * Adjust the synchronize indicator according to timecode
		 * say were OK, and then say not if we really are not OK
		 */
		if (synced == '>' || synced == '#' || synced == '?'
		    || synced == 'X')
			pp->leap = LEAP_NOTINSYNC;
		else
			pp->leap = LEAP_NOWARNING;

		true_doevent(peer, e_TS);

#ifdef CLOCK_PPS720
		/* If it's taken more than 65ms to get here, we'll lose. */
		if ((pp->sloppyclockflag & CLK_FLAG4) && up->pcl720init) {
			l_fp   off;

#ifdef CLOCK_ATOM
			/*
			 * find out what time it really is. Include
			 * the count from the PCL720
			 */
			if (!clocktime(pp->day, pp->hour, pp->minute, 
				       pp->second, GMT, pp->lastrec.l_ui, 
				       &pp->yearstart, &off.l_ui)) {
				refclock_report(peer, CEVNT_BADTIME);
				return;
			}
			off.l_uf = 0;
#endif

			pp->usec = true_sample720();
#ifdef CLOCK_ATOM
			TVUTOTSF(pp->usec, off.l_uf);
#endif

			/*
			 * Stomp all over the timestamp that was pulled out
			 * of the input stream. It's irrelevant since we've
			 * adjusted the input time to reflect now (via pp->usec)
			 * rather than when the data was collected.
			 */
			get_systime(&pp->lastrec);
#ifdef CLOCK_ATOM
			/*
			 * Create a true offset for feeding to pps_sample()
			 */
			L_SUB(&off, &pp->lastrec);

			pps_sample(peer, &off);
#endif
			true_debug(peer, "true_sample720: %luus\n", pp->usec);
		}
#endif

		/*
		 * The clock will blurt a timecode every second but we only
		 * want one when polled.  If we havn't been polled, bail out.
		 */
		if (!up->polled)
			return;

                /* We only call doevent if additional things need be done
                 * at poll interval.  Currently, its only for GOES.  We also
                 * call it for clock unknown so that it gets logged.
                 */
                if (up->type == t_goes || up->type == t_unknown)
                    true_doevent(peer, e_Poll);

		if (!refclock_process(pp)) {
			refclock_report(peer, CEVNT_BADTIME);
			return;
		}
		/*
		 * If clock is good we send a NOMINAL message so that
		 * any previous BAD messages are nullified
		 */
		pp->lastref = pp->lastrec;
		refclock_receive(peer);
		refclock_report(peer, CEVNT_NOMINAL);

		/*
		 * We have succedded in answering the poll.
		 * Turn off the flag and return
		 */
		up->polled = 0;

		return;
	}

	/*
	 * No match to known timecodes, report failure and return
	 */
	refclock_report(peer, CEVNT_BADREPLY);
	return;
}


/*
 * true_send - time to send the clock a signal to cough up a time sample
 */
static void
true_send(
	struct peer *peer,
	const char *cmd
	)
{
	struct refclockproc *pp;

	pp = peer->procptr;
	if (!(pp->sloppyclockflag & CLK_FLAG1)) {
		size_t len = strlen(cmd);

		true_debug(peer, "Send '%s'\n", cmd);
		if (write(pp->io.fd, cmd, len) != (ssize_t)len)
			refclock_report(peer, CEVNT_FAULT);
		else
			pp->polls++;
	}
}


/*
 * state machine for initializing and controlling a clock
 */
static void
true_doevent(
	struct peer *peer,
	enum true_event event
	)
{
	struct true_unit *up;
	struct refclockproc *pp;

	pp = peer->procptr;
	up = pp->unitptr;
	if (event != e_TS) {
		NLOG(NLOG_CLOCKSTATUS) {
			msyslog(LOG_INFO, "TRUE: clock %s, state %s, event %s",
				typeStr(up->type),
				stateStr(up->state),
				eventStr(event));
		}
	}
	true_debug(peer, "clock %s, state %s, event %s\n",
		   typeStr(up->type), stateStr(up->state), eventStr(event));
	switch (up->type) {
	case t_goes:
		switch (event) {
		case e_Init:	/* FALLTHROUGH */
		case e_Satellite:
			/*
			 * Switch back to on-second time codes and return.
			 */
			true_send(peer, "C");
			up->state = s_Start;
			break;
		case e_Poll:
			/*
			 * After each poll, check the station (satellite).
			 */
			true_send(peer, "P");
			/* No state change needed. */
			break;
		default:
			break;
		}
		/* FALLTHROUGH */
	case t_omega:
		switch (event) {
		case e_Init:
			true_send(peer, "C");
			up->state = s_Start;
			break;
		case e_TS:
			if (up->state != s_Start && up->state != s_Auto) {
				true_send(peer, "\03\r");
				break;
			}
			up->state = s_Auto;
			break;
		default:
			break;
		}
		break;
	case t_tm:
		switch (event) {
		case e_Init:
			true_send(peer, "F18\r");
			up->state = s_Init;
			break;
		case e_F18:
			true_send(peer, "F50\r");
                        /*
                         * Timecode: " TRUETIME Mk III" or " TRUETIME XL"
                         * (from a TM/TMD/XL clock during initialization.)
                         */
                        if ( strcmp(pp->a_lastcode, " TRUETIME Mk III") == 0 ||
                            strncmp(pp->a_lastcode, " TRUETIME XL", 12) == 0) {
                                true_doevent(peer, e_F18);
                                NLOG(NLOG_CLOCKSTATUS) {
                                    msyslog(LOG_INFO, "TM/TMD/XL: %s", 
                                            pp->a_lastcode);
                                }
                                return;
                        }
			up->state = s_F18;
			break;
		case e_F50:
			true_send(peer, "F51\r");
			up->state = s_F50;
			break;
		case e_F51:
			true_send(peer, "F08\r");
			up->state = s_Start;
			break;
		case e_TS:
			if (up->state != s_Start && up->state != s_Auto) {
				true_send(peer, "\03\r");
				break;
			}
			up->state = s_Auto;
			break;
		default:
			break;
		}
		break;
	case t_tcu:
		switch (event) {
		case e_Init:
			true_send(peer, "MD3\r");	/* GPS Synch'd Gen. */
			true_send(peer, "TSU\r");	/* UTC, not GPS. */
			true_send(peer, "AU\r");	/* Auto Timestamps. */
			up->state = s_Start;
			break;
		case e_TS:
			if (up->state != s_Start && up->state != s_Auto) {
				true_send(peer, "\03\r");
				break;
			}
			up->state = s_Auto;
			break;
		default:
			break;
		}
		break;
	case t_tl3:
                switch (event) {
                    case e_Init:
                        true_send(peer, "ST1"); /* Turn on continuous stream */
                        break;
                    case e_TS:
                        up->state = s_Auto;
                        break;
                    default:
                        break;
                }
                break;
	case t_unknown:
               if (event == e_Poll)
                   break;
		switch (up->state) {
		case s_Base:
			if (event != e_Init)
			    abort();
			true_send(peer, "P\r");
			up->state = s_InqGOES;
			break;
		case s_InqGOES:
			switch (event) {
			case e_Satellite:
				up->type = t_goes;
				true_doevent(peer, e_Init);
				break;
			case e_Init:	/*FALLTHROUGH*/
			case e_Huh:
			case e_TS:
                                true_send(peer, "ST0"); /* turn off TL3 auto */
                                sleep(1);               /* wait for it */
                                up->state = s_InqTL3;
                                true_send(peer, "QV");  /* see if its a TL3 */
                                break;
                            default:
                                abort();
                        }
                        break;
                    case s_InqTL3:
                        switch (event) {
                            case e_TL3:
                                up->type = t_tl3;
                                up->state = s_Auto;     /* Inq side-effect. */
                                true_send(peer, "ST1"); /* Turn on 1/sec data */
                                break;
                            case e_Init:        /*FALLTHROUGH*/
                            case e_Huh:
				up->state = s_InqOmega;
				true_send(peer, "C\r");
				break;
                            case e_TS:
                                 up->type = t_tl3;    /* Already sending data */
                                 up->state = s_Auto;
                                 break;
			    default:
                                msyslog(LOG_INFO, 
                                        "TRUE: TL3 init fellthrough! (%d)", event);
                                break; 
			}
			break;
		case s_InqOmega:
			switch (event) {
			case e_TS:
				up->type = t_omega;
				up->state = s_Auto;	/* Inq side-effect. */
				break;
			case e_Init:	/*FALLTHROUGH*/
			case e_Huh:
				up->state = s_InqTM;
				true_send(peer, "F18\r");
				break;
			default:
				abort();
			}
			break;
		case s_InqTM:
			switch (event) {
			case e_F18:
				up->type = t_tm;
				true_doevent(peer, e_Init);
				break;
			case e_Init:	/*FALLTHROUGH*/
			case e_Huh:
				true_send(peer, "PO\r");
				up->state = s_InqTCU;
				break;
			default:
                                msyslog(LOG_INFO, 
                                        "TRUE: TM/TMD init fellthrough!");
			        break;
			}
			break;
		case s_InqTCU:
			switch (event) {
			case e_Location:
				up->type = t_tcu;
				true_doevent(peer, e_Init);
				break;
			case e_Init:	/*FALLTHROUGH*/
			case e_Huh:
				up->state = s_Base;
				sleep(1);	/* XXX */
				break;
			default:
                                msyslog(LOG_INFO, 
                                        "TRUE: TCU init fellthrough!");
                                break;
			}
			break;
			/*
			 * An expedient hack to prevent lint complaints,
			 * these don't actually need to be used here...
			 */
		case s_Init:
		case s_F18:
		case s_F50:
		case s_Start:
		case s_Auto:
		case s_Max:
			msyslog(LOG_INFO, "TRUE: state %s is unexpected!",
				stateStr(up->state));
		}
		break;
	default:
                msyslog(LOG_INFO, "TRUE: cannot identify refclock!");
		abort();    
		/* NOTREACHED */
	}

#ifdef CLOCK_PPS720
	if ((pp->sloppyclockflag & CLK_FLAG4) && !up->pcl720init) {
		/* Make counter trigger on gate0, count down from 65535. */
		pcl720_load(PCL720_IOB, PCL720_CTR, i8253_oneshot, 65535);
		/*
		 * (These constants are OK since
		 * they represent hardware maximums.)
		 */
		NLOG(NLOG_CLOCKINFO) {
			msyslog(LOG_NOTICE, "PCL-720 initialized");
		}
		up->pcl720init++;
	}
#endif


}

/*
 * true_poll - called by the transmit procedure
 */
static void
true_poll(
	int unit,
	struct peer *peer
	)
{
	struct true_unit *up;
	struct refclockproc *pp;

	/*
	 * You don't need to poll this clock.  It puts out timecodes
	 * once per second.  If asked for a timestamp, take note.
	 * The next time a timecode comes in, it will be fed back.
	 */
	pp = peer->procptr;
	up = pp->unitptr;
	if (up->pollcnt > 0) {
		up->pollcnt--;
	} else {
		true_doevent(peer, e_Init);
		refclock_report(peer, CEVNT_TIMEOUT);
	}

	/*
	 * polled every 64 seconds. Ask true_receive to hand in a
	 * timestamp.
	 */
	up->polled = 1;
	pp->polls++;
}

#ifdef CLOCK_PPS720
/*
 * true_sample720 - sample the PCL-720
 */
static u_long
true_sample720(void)
{
	unsigned long f;

	/* We wire the PCL-720's 8253.OUT0 to bit 0 of connector 3.
	 * If it is not being held low now, we did not get called
	 * within 65535us.
	 */
	if (inb(pcl720_data_16_23(PCL720_IOB)) & 0x01) {
		NLOG(NLOG_CLOCKINFO) {
			msyslog(LOG_NOTICE, "PCL-720 out of synch");
		}
		return (0);
	}
	f = (65536 - pcl720_read(PCL720_IOB, PCL720_CTR));
#ifdef PPS720_DEBUG
	msyslog(LOG_DEBUG, "PCL-720: %luus", f);
#endif
	return (f);
}
#endif

#else
int refclock_true_bs;
#endif /* REFCLOCK */