refclock_true.c revision 106163
168349Sobrien/*
268349Sobrien * refclock_true - clock driver for the Kinemetrics Truetime receivers
3226048Sobrien *	Receiver Version 3.0C - tested plain, with CLKLDISC
468349Sobrien *	Developement work being done:
568349Sobrien * 	- Properly handle varying satellite positions (more acurately)
668349Sobrien *	- Integrate GPSTM and/or OMEGA and/or TRAK and/or ??? drivers
7186690Sobrien */
8
9#ifdef HAVE_CONFIG_H
10#include <config.h>
11#endif
12
13#if defined(REFCLOCK) && defined(CLOCK_TRUETIME)
14
15#include "ntpd.h"
16#include "ntp_io.h"
17#include "ntp_refclock.h"
18#include "ntp_unixtime.h"
19#include "ntp_stdlib.h"
20
21#include <stdio.h>
22#include <ctype.h>
23
24/* This should be an atom clock but those are very hard to build.
25 *
26 * The PCL720 from P C Labs has an Intel 8253 lookalike, as well as a bunch
27 * of TTL input and output pins, all brought out to the back panel.  If you
28 * wire a PPS signal (such as the TTL PPS coming out of a GOES or other
29 * Kinemetrics/Truetime clock) to the 8253's GATE0, and then also wire the
30 * 8253's OUT0 to the PCL720's INPUT3.BIT0, then we can read CTR0 to get the
31 * number of uSecs since the last PPS upward swing, mediated by reading OUT0
32 * to find out if the counter has wrapped around (this happens if more than
33 * 65535us (65ms) elapses between the PPS event and our being called.)
34 */
35#ifdef CLOCK_PPS720
36# undef min	/* XXX */
37# undef max	/* XXX */
38# include <machine/inline.h>
39# include <sys/pcl720.h>
40# include <sys/i8253.h>
41# define PCL720_IOB 0x2a0	/* XXX */
42# define PCL720_CTR 0		/* XXX */
43#endif
44
45/*
46 * Support for Kinemetrics Truetime Receivers
47 *	GOES
48 *	GPS/TM-TMD
49 *	XL-DC		(a 151-602-210, reported by the driver as a GPS/TM-TMD)
50 *	GPS-800 TCU	(an 805-957 with the RS232 Talker/Listener module)
51 *	OM-DC:		getting stale ("OMEGA")
52 *
53 * Most of this code is originally from refclock_wwvb.c with thanks.
54 * It has been so mangled that wwvb is not a recognizable ancestor.
55 *
56 * Timcode format: ADDD:HH:MM:SSQCL
57 *	A - control A		(this is stripped before we see it)
58 *	Q - Quality indication	(see below)
59 *	C - Carriage return
60 *	L - Line feed
61 *
62 * Quality codes indicate possible error of
63 *   468-DC GOES Receiver:
64 *   GPS-TM/TMD Receiver: (default quality codes for XL-DC)
65 *       ?     +/- 1  milliseconds	#     +/- 100 microseconds
66 *       *     +/- 10 microseconds	.     +/- 1   microsecond
67 *     space   less than 1 microsecond
68 *   OM-DC OMEGA Receiver: (default quality codes for OMEGA)
69 *   WARNING OMEGA navigation system is no longer existent
70 *       >     >+- 5 seconds
71 *       ?     >+/- 500 milliseconds    #     >+/- 50 milliseconds
72 *       *     >+/- 5 milliseconds      .     >+/- 1 millisecond
73 *      A-H    less than 1 millisecond.  Character indicates which station
74 *             is being received as follows:
75 *             A = Norway, B = Liberia, C = Hawaii, D = North Dakota,
76 *             E = La Reunion, F = Argentina, G = Australia, H = Japan.
77 *
78 * The carriage return start bit begins on 0 seconds and extends to 1 bit time.
79 *
80 * Notes on 468-DC and OMEGA receiver:
81 *
82 * Send the clock a 'R' or 'C' and once per second a timestamp will
83 * appear.  Send a 'P' to get the satellite position once (GOES only.)
84 *
85 * Notes on the 468-DC receiver:
86 *
87 * Since the old east/west satellite locations are only historical, you can't
88 * set your clock propagation delay settings correctly and still use
89 * automatic mode. The manual says to use a compromise when setting the
90 * switches. This results in significant errors. The solution; use fudge
91 * time1 and time2 to incorporate corrections. If your clock is set for
92 * 50 and it should be 58 for using the west and 46 for using the east,
93 * use the line
94 *
95 * fudge 127.127.5.0 time1 +0.008 time2 -0.004
96 *
97 * This corrects the 4 milliseconds advance and 8 milliseconds retard
98 * needed. The software will ask the clock which satellite it sees.
99 *
100 * Ntp.conf parameters:
101 * time1 - offset applied to samples when reading WEST satellite (default = 0)
102 * time2 - offset applied to samples when reading EAST satellite (default = 0)
103 * val1  - stratum to assign to this clock (default = 0)
104 * val2  - refid assigned to this clock (default = "TRUE", see below)
105 * flag1 - will silence the clock side of ntpd, just reading the clock
106 *         without trying to write to it.  (default = 0)
107 * flag2 - generate a debug file /tmp/true%d.
108 * flag3 - enable ppsclock streams module
109 * flag4 - use the PCL-720 (BSD/OS only)
110 */
111
112
113/*
114 * Definitions
115 */
116#define	DEVICE		"/dev/true%d"
117#define	SPEED232	B9600	/* 9600 baud */
118
119/*
120 * Radio interface parameters
121 */
122#define	PRECISION	(-10)	/* precision assumed (about 1 ms) */
123#define	REFID		"TRUE"	/* reference id */
124#define	DESCRIPTION	"Kinemetrics/TrueTime Receiver"
125
126/*
127 * Tags which station (satellite) we see
128 */
129#define GOES_WEST	0	/* Default to WEST satellite and apply time1 */
130#define GOES_EAST	1	/* until you discover otherwise */
131
132/*
133 * used by the state machine
134 */
135enum true_event	{e_Init, e_Huh, e_F18, e_F50, e_F51, e_Satellite,
136		 e_Poll, e_Location, e_TS, e_Max};
137const char *events[] = {"Init", "Huh", "F18", "F50", "F51", "Satellite",
138			"Poll", "Location", "TS"};
139#define eventStr(x) (((int)x<(int)e_Max) ? events[(int)x] : "?")
140
141enum true_state	{s_Base, s_InqTM, s_InqTCU, s_InqOmega, s_InqGOES,
142		 s_Init, s_F18, s_F50, s_Start, s_Auto, s_Max};
143const char *states[] = {"Base", "InqTM", "InqTCU", "InqOmega", "InqGOES",
144			"Init", "F18", "F50", "Start", "Auto"};
145#define stateStr(x) (((int)x<(int)s_Max) ? states[(int)x] : "?")
146
147enum true_type	{t_unknown, t_goes, t_tm, t_tcu, t_omega, t_Max};
148const char *types[] = {"unknown", "goes", "tm", "tcu", "omega"};
149#define typeStr(x) (((int)x<(int)t_Max) ? types[(int)x] : "?")
150
151/*
152 * unit control structure
153 */
154struct true_unit {
155	unsigned int	pollcnt;	/* poll message counter */
156	unsigned int	station;	/* which station we are on */
157	unsigned int	polled;		/* Hand in a time sample? */
158	enum true_state	state;		/* state machine */
159	enum true_type	type;		/* what kind of clock is it? */
160	int		unit;		/* save an extra copy of this */
161	FILE		*debug;		/* debug logging file */
162#ifdef CLOCK_PPS720
163	int		pcl720init;	/* init flag for PCL 720 */
164#endif
165};
166
167/*
168 * Function prototypes
169 */
170static	int	true_start	P((int, struct peer *));
171static	void	true_shutdown	P((int, struct peer *));
172static	void	true_receive	P((struct recvbuf *));
173static	void	true_poll	P((int, struct peer *));
174static	void	true_send	P((struct peer *, const char *));
175static	void	true_doevent	P((struct peer *, enum true_event));
176
177#ifdef CLOCK_PPS720
178static	u_long	true_sample720	P((void));
179#endif
180
181/*
182 * Transfer vector
183 */
184struct	refclock refclock_true = {
185	true_start,		/* start up driver */
186	true_shutdown,		/* shut down driver */
187	true_poll,		/* transmit poll message */
188	noentry,		/* not used (old true_control) */
189	noentry,		/* initialize driver (not used) */
190	noentry,		/* not used (old true_buginfo) */
191	NOFLAGS			/* not used */
192};
193
194
195#if !defined(__STDC__)
196# define true_debug (void)
197#else
198static void
199true_debug(struct peer *peer, const char *fmt, ...)
200{
201	va_list ap;
202	int want_debugging, now_debugging;
203	struct refclockproc *pp;
204	struct true_unit *up;
205
206	va_start(ap, fmt);
207	pp = peer->procptr;
208	up = (struct true_unit *)pp->unitptr;
209
210	want_debugging = (pp->sloppyclockflag & CLK_FLAG2) != 0;
211	now_debugging = (up->debug != NULL);
212	if (want_debugging != now_debugging)
213	{
214		if (want_debugging) {
215		    char filename[40];
216		    int fd;
217
218		    snprintf(filename, sizeof(filename), "/tmp/true%d.debug", up->unit);
219		    fd = open(filename, O_CREAT | O_WRONLY | O_EXCL, 0600);
220		    if (fd >= 0 && (up->debug = fdopen(fd, "r+"))) {
221#ifdef HAVE_SETVBUF
222			    static char buf[BUFSIZ];
223			    setvbuf(up->debug, buf, _IOLBF, BUFSIZ);
224#else
225			    setlinebuf(up->debug);
226#endif
227		    }
228	    } else {
229		    fclose(up->debug);
230		    up->debug = NULL;
231	    }
232	}
233
234	if (up->debug) {
235		fprintf(up->debug, "true%d: ", up->unit);
236		vfprintf(up->debug, fmt, ap);
237	}
238}
239#endif /*STDC*/
240
241/*
242 * true_start - open the devices and initialize data for processing
243 */
244static int
245true_start(
246	int unit,
247	struct peer *peer
248	)
249{
250	register struct true_unit *up;
251	struct refclockproc *pp;
252	char device[40];
253	int fd;
254
255	/*
256	 * Open serial port
257	 */
258	(void)snprintf(device, sizeof(device), DEVICE, unit);
259	if (!(fd = refclock_open(device, SPEED232, LDISC_CLK)))
260	    return (0);
261
262	/*
263	 * Allocate and initialize unit structure
264	 */
265	if (!(up = (struct true_unit *)
266	      emalloc(sizeof(struct true_unit)))) {
267		(void) close(fd);
268		return (0);
269	}
270	memset((char *)up, 0, sizeof(struct true_unit));
271	pp = peer->procptr;
272	pp->io.clock_recv = true_receive;
273	pp->io.srcclock = (caddr_t)peer;
274	pp->io.datalen = 0;
275	pp->io.fd = fd;
276	if (!io_addclock(&pp->io)) {
277		(void) close(fd);
278		free(up);
279		return (0);
280	}
281	pp->unitptr = (caddr_t)up;
282
283	/*
284	 * Initialize miscellaneous variables
285	 */
286	peer->precision = PRECISION;
287	pp->clockdesc = DESCRIPTION;
288	memcpy((char *)&pp->refid, REFID, 4);
289	up->pollcnt = 2;
290	up->type = t_unknown;
291	up->state = s_Base;
292	true_doevent(peer, e_Init);
293	return (1);
294}
295
296/*
297 * true_shutdown - shut down the clock
298 */
299static void
300true_shutdown(
301	int unit,
302	struct peer *peer
303	)
304{
305	register struct true_unit *up;
306	struct refclockproc *pp;
307
308	pp = peer->procptr;
309	up = (struct true_unit *)pp->unitptr;
310	io_closeclock(&pp->io);
311	free(up);
312}
313
314
315/*
316 * true_receive - receive data from the serial interface on a clock
317 */
318static void
319true_receive(
320	struct recvbuf *rbufp
321	)
322{
323	register struct true_unit *up;
324	struct refclockproc *pp;
325	struct peer *peer;
326	u_short new_station;
327	char synced;
328	int i;
329	int lat, lon, off;	/* GOES Satellite position */
330        /* Use these variable to hold data until we decide its worth keeping */
331        char    rd_lastcode[BMAX];
332        l_fp    rd_tmp;
333        u_short rd_lencode;
334
335	/*
336	 * Get the clock this applies to and pointers to the data.
337	 */
338	peer = (struct peer *)rbufp->recv_srcclock;
339	pp = peer->procptr;
340	up = (struct true_unit *)pp->unitptr;
341
342	/*
343	 * Read clock output.  Automatically handles STREAMS, CLKLDISC.
344	 */
345        rd_lencode = refclock_gtlin(rbufp, rd_lastcode, BMAX, &rd_tmp);
346        rd_lastcode[rd_lencode] = '\0';
347
348	/*
349	 * There is a case where <cr><lf> generates 2 timestamps.
350	 */
351        if (rd_lencode == 0)
352            return;
353        pp->lencode = rd_lencode;
354        strcpy(pp->a_lastcode, rd_lastcode);
355        pp->lastrec = rd_tmp;
356	true_debug(peer, "receive(%s) [%d]\n", pp->a_lastcode, pp->lencode);
357
358	up->pollcnt = 2;
359	record_clock_stats(&peer->srcadr, pp->a_lastcode);
360
361	/*
362	 * We get down to business, check the timecode format and decode
363	 * its contents. This code decodes a multitude of different
364	 * clock messages. Timecodes are processed if needed. All replies
365	 * will be run through the state machine to tweak driver options
366	 * and program the clock.
367	 */
368
369	/*
370	 * Clock misunderstood our last command?
371	 */
372	if (pp->a_lastcode[0] == '?' ||
373	    strcmp(pp->a_lastcode, "ERROR 05 NO SUCH FUNCTION") == 0) {
374		true_doevent(peer, e_Huh);
375		return;
376	}
377
378	/*
379	 * Timecode: "nnnnn+nnn-nnn"
380	 * (from GOES clock when asked about satellite position)
381	 */
382	if ((pp->a_lastcode[5] == '+' || pp->a_lastcode[5] == '-') &&
383	    (pp->a_lastcode[9] == '+' || pp->a_lastcode[9] == '-') &&
384	    sscanf(pp->a_lastcode, "%5d%*c%3d%*c%3d", &lon, &lat, &off) == 3
385	    ) {
386		const char *label = "Botch!";
387
388		/*
389		 * This is less than perfect.  Call the (satellite)
390		 * either EAST or WEST and adjust slop accodingly
391		 * Perfectionists would recalculate the exact delay
392		 * and adjust accordingly...
393		 */
394		if (lon > 7000 && lon < 14000) {
395			if (lon < 10000) {
396				new_station = GOES_EAST;
397				label = "EAST";
398			} else {
399				new_station = GOES_WEST;
400				label = "WEST";
401			}
402
403			if (new_station != up->station) {
404				double dtemp;
405
406				dtemp = pp->fudgetime1;
407				pp->fudgetime1 = pp->fudgetime2;
408				pp->fudgetime2 = dtemp;
409				up->station = new_station;
410			}
411		}
412		else {
413			/*refclock_report(peer, CEVNT_BADREPLY);*/
414			label = "UNKNOWN";
415		}
416		true_debug(peer, "GOES: station %s\n", label);
417		true_doevent(peer, e_Satellite);
418		return;
419	}
420
421	/*
422	 * Timecode: "Fnn"
423	 * (from TM/TMD clock when it wants to tell us what it's up to.)
424	 */
425	if (sscanf(pp->a_lastcode, "F%2d", &i) == 1 && i > 0 && i < 80) {
426		switch (i) {
427		    case 50:
428			true_doevent(peer, e_F50);
429			break;
430		    case 51:
431			true_doevent(peer, e_F51);
432			break;
433		    default:
434			true_debug(peer, "got F%02d - ignoring\n", i);
435			break;
436		}
437		return;
438	}
439
440	/*
441	 * Timecode: " TRUETIME Mk III" or " TRUETIME XL"
442	 * (from a TM/TMD/XL clock during initialization.)
443	 */
444	if (strcmp(pp->a_lastcode, " TRUETIME Mk III") == 0 ||
445	    strncmp(pp->a_lastcode, " TRUETIME XL", 12) == 0) {
446		true_doevent(peer, e_F18);
447		NLOG(NLOG_CLOCKSTATUS) {
448			msyslog(LOG_INFO, "TM/TMD/XL: %s", pp->a_lastcode);
449		}
450		return;
451	}
452
453	/*
454	 * Timecode: "N03726428W12209421+000033"
455	 *                      1         2
456	 *            0123456789012345678901234
457	 * (from a TCU during initialization)
458	 */
459	if ((pp->a_lastcode[0] == 'N' || pp->a_lastcode[0] == 'S') &&
460	    (pp->a_lastcode[9] == 'W' || pp->a_lastcode[9] == 'E') &&
461	    pp->a_lastcode[18] == '+') {
462		true_doevent(peer, e_Location);
463		NLOG(NLOG_CLOCKSTATUS) {
464			msyslog(LOG_INFO, "TCU-800: %s", pp->a_lastcode);
465		}
466		return;
467	}
468	/*
469	 * Timecode: "ddd:hh:mm:ssQ"
470	 * (from all clocks supported by this driver.)
471	 */
472	if (pp->a_lastcode[3] == ':' &&
473	    pp->a_lastcode[6] == ':' &&
474	    pp->a_lastcode[9] == ':' &&
475	    sscanf(pp->a_lastcode, "%3d:%2d:%2d:%2d%c",
476		   &pp->day, &pp->hour, &pp->minute,
477		   &pp->second, &synced) == 5) {
478
479		/*
480		 * Adjust the synchronize indicator according to timecode
481		 * say were OK, and then say not if we really are not OK
482		 */
483		if (synced == '>' || synced == '#' || synced == '?')
484		    pp->leap = LEAP_NOTINSYNC;
485		else
486                    pp->leap = LEAP_NOWARNING;
487
488		true_doevent(peer, e_TS);
489
490#ifdef CLOCK_PPS720
491		/* If it's taken more than 65ms to get here, we'll lose. */
492		if ((pp->sloppyclockflag & CLK_FLAG4) && up->pcl720init) {
493			l_fp   off;
494
495#ifdef CLOCK_ATOM
496			/*
497			 * find out what time it really is. Include
498			 * the count from the PCL720
499			 */
500			if (!clocktime(pp->day, pp->hour, pp->minute,
501				       pp->second, GMT, pp->lastrec.l_ui,
502				       &pp->yearstart, &off.l_ui)) {
503				refclock_report(peer, CEVNT_BADTIME);
504				return;
505			}
506#endif
507
508			pp->usec = true_sample720();
509#ifdef CLOCK_ATOM
510			TVUTOTSF(pp->usec, off.l_uf);
511#endif
512
513			/*
514			 * Stomp all over the timestamp that was pulled out
515			 * of the input stream. It's irrelevant since we've
516			 * adjusted the input time to reflect now (via pp->usec)
517			 * rather than when the data was collected.
518			 */
519			get_systime(&pp->lastrec);
520#ifdef CLOCK_ATOM
521			/*
522			 * Create a true offset for feeding to pps_sample()
523			 */
524			L_SUB(&off, &pp->lastrec);
525
526			pps_sample(peer, &off);
527#endif
528			true_debug(peer, "true_sample720: %luus\n", pp->usec);
529		}
530#endif
531
532		/*
533		 * The clock will blurt a timecode every second but we only
534		 * want one when polled.  If we havn't been polled, bail out.
535		 */
536		if (!up->polled)
537		    return;
538
539		true_doevent(peer, e_Poll);
540		if (!refclock_process(pp)) {
541			refclock_report(peer, CEVNT_BADTIME);
542			return;
543		}
544		/*
545		 * If clock is good we send a NOMINAL message so that
546		 * any previous BAD messages are nullified
547		 */
548                refclock_report(peer, CEVNT_NOMINAL);
549		refclock_receive(peer);
550
551		/*
552		 * We have succedded in answering the poll.
553		 * Turn off the flag and return
554		 */
555		up->polled = 0;
556
557		return;
558	}
559
560	/*
561	 * No match to known timecodes, report failure and return
562	 */
563	refclock_report(peer, CEVNT_BADREPLY);
564	return;
565}
566
567
568/*
569 * true_send - time to send the clock a signal to cough up a time sample
570 */
571static void
572true_send(
573	struct peer *peer,
574	const char *cmd
575	)
576{
577	struct refclockproc *pp;
578
579	pp = peer->procptr;
580	if (!(pp->sloppyclockflag & CLK_FLAG1)) {
581		register int len = strlen(cmd);
582
583		true_debug(peer, "Send '%s'\n", cmd);
584		if (write(pp->io.fd, cmd, (unsigned)len) != len)
585		    refclock_report(peer, CEVNT_FAULT);
586		else
587		    pp->polls++;
588	}
589}
590
591
592/*
593 * state machine for initializing and controlling a clock
594 */
595static void
596true_doevent(
597	struct peer *peer,
598	enum true_event event
599	)
600{
601	struct true_unit *up;
602	struct refclockproc *pp;
603
604	pp = peer->procptr;
605	up = (struct true_unit *)pp->unitptr;
606	if (event != e_TS) {
607		NLOG(NLOG_CLOCKSTATUS) {
608			msyslog(LOG_INFO, "TRUE: clock %s, state %s, event %s",
609				typeStr(up->type),
610				stateStr(up->state),
611				eventStr(event));
612		}
613	}
614	true_debug(peer, "clock %s, state %s, event %s\n",
615		   typeStr(up->type), stateStr(up->state), eventStr(event));
616	switch (up->type) {
617	    case t_goes:
618		switch (event) {
619		    case e_Init:	/* FALLTHROUGH */
620		    case e_Satellite:
621			/*
622			 * Switch back to on-second time codes and return.
623			 */
624			true_send(peer, "C");
625			up->state = s_Start;
626			break;
627		    case e_Poll:
628			/*
629			 * After each poll, check the station (satellite).
630			 */
631			true_send(peer, "P");
632			/* No state change needed. */
633			break;
634		    default:
635			break;
636		}
637		/* FALLTHROUGH */
638	    case t_omega:
639		switch (event) {
640		    case e_Init:
641			true_send(peer, "C");
642			up->state = s_Start;
643			break;
644		    case e_TS:
645			if (up->state != s_Start && up->state != s_Auto) {
646				true_send(peer, "\03\r");
647				break;
648			}
649			up->state = s_Auto;
650			break;
651		    default:
652			break;
653		}
654		break;
655	    case t_tm:
656		switch (event) {
657		    case e_Init:
658			true_send(peer, "F18\r");
659			up->state = s_Init;
660			break;
661		    case e_F18:
662			true_send(peer, "F50\r");
663			up->state = s_F18;
664			break;
665		    case e_F50:
666			true_send(peer, "F51\r");
667			up->state = s_F50;
668			break;
669		    case e_F51:
670			true_send(peer, "F08\r");
671			up->state = s_Start;
672			break;
673		    case e_TS:
674			if (up->state != s_Start && up->state != s_Auto) {
675				true_send(peer, "\03\r");
676				break;
677			}
678			up->state = s_Auto;
679			break;
680		    default:
681			break;
682		}
683		break;
684	    case t_tcu:
685		switch (event) {
686		    case e_Init:
687			true_send(peer, "MD3\r");	/* GPS Synch'd Gen. */
688			true_send(peer, "TSU\r");	/* UTC, not GPS. */
689			true_send(peer, "AU\r");	/* Auto Timestamps. */
690			up->state = s_Start;
691			break;
692		    case e_TS:
693			if (up->state != s_Start && up->state != s_Auto) {
694				true_send(peer, "\03\r");
695				break;
696			}
697			up->state = s_Auto;
698			break;
699		    default:
700			break;
701		}
702		break;
703	    case t_unknown:
704		switch (up->state) {
705		    case s_Base:
706			if (event != e_Init)
707			    abort();
708			true_send(peer, "P\r");
709			up->state = s_InqGOES;
710			break;
711		    case s_InqGOES:
712			switch (event) {
713			    case e_Satellite:
714				up->type = t_goes;
715				true_doevent(peer, e_Init);
716				break;
717			    case e_Init:	/*FALLTHROUGH*/
718			    case e_Huh:	/*FALLTHROUGH*/
719			    case e_TS:
720				up->state = s_InqOmega;
721				true_send(peer, "C\r");
722				break;
723			    default:
724				abort();
725			}
726			break;
727		    case s_InqOmega:
728			switch (event) {
729			    case e_TS:
730				up->type = t_omega;
731				up->state = s_Auto;	/* Inq side-effect. */
732				break;
733			    case e_Init:	/*FALLTHROUGH*/
734			    case e_Huh:
735				up->state = s_InqTM;
736				true_send(peer, "F18\r");
737				break;
738			    default:
739				abort();
740			}
741			break;
742		    case s_InqTM:
743			switch (event) {
744			    case e_F18:
745				up->type = t_tm;
746				true_doevent(peer, e_Init);
747				break;
748			    case e_Init:	/*FALLTHROUGH*/
749			    case e_Huh:
750				true_send(peer, "PO\r");
751				up->state = s_InqTCU;
752				break;
753			    default:
754				abort();
755			}
756			break;
757		    case s_InqTCU:
758			switch (event) {
759			    case e_Location:
760				up->type = t_tcu;
761				true_doevent(peer, e_Init);
762				break;
763			    case e_Init:	/*FALLTHROUGH*/
764			    case e_Huh:
765				up->state = s_Base;
766				sleep(1);	/* XXX */
767				break;
768			    default:
769				abort();
770			}
771			break;
772			/*
773			 * An expedient hack to prevent lint complaints,
774			 * these don't actually need to be used here...
775			 */
776		    case s_Init:
777		    case s_F18:
778		    case s_F50:
779		    case s_Start:
780		    case s_Auto:
781		    case s_Max:
782			msyslog(LOG_INFO, "TRUE: state %s is unexpected!", stateStr(up->state));
783		}
784		break;
785	    default:
786		abort();
787		/* NOTREACHED */
788	}
789
790#ifdef CLOCK_PPS720
791	if ((pp->sloppyclockflag & CLK_FLAG4) && !up->pcl720init) {
792		/* Make counter trigger on gate0, count down from 65535. */
793		pcl720_load(PCL720_IOB, PCL720_CTR, i8253_oneshot, 65535);
794		/*
795		 * (These constants are OK since
796		 * they represent hardware maximums.)
797		 */
798		NLOG(NLOG_CLOCKINFO) {
799			msyslog(LOG_NOTICE, "PCL-720 initialized");
800		}
801		up->pcl720init++;
802	}
803#endif
804
805
806}
807
808/*
809 * true_poll - called by the transmit procedure
810 */
811static void
812true_poll(
813	int unit,
814	struct peer *peer
815	)
816{
817	struct true_unit *up;
818	struct refclockproc *pp;
819
820	/*
821	 * You don't need to poll this clock.  It puts out timecodes
822	 * once per second.  If asked for a timestamp, take note.
823	 * The next time a timecode comes in, it will be fed back.
824	 */
825	pp = peer->procptr;
826	up = (struct true_unit *)pp->unitptr;
827	if (up->pollcnt > 0)
828	    up->pollcnt--;
829	else {
830		true_doevent(peer, e_Init);
831		refclock_report(peer, CEVNT_TIMEOUT);
832	}
833
834	/*
835	 * polled every 64 seconds. Ask true_receive to hand in a
836	 * timestamp.
837	 */
838	up->polled = 1;
839	pp->polls++;
840}
841
842#ifdef CLOCK_PPS720
843/*
844 * true_sample720 - sample the PCL-720
845 */
846static u_long
847true_sample720(void)
848{
849	unsigned long f;
850
851	/* We wire the PCL-720's 8253.OUT0 to bit 0 of connector 3.
852	 * If it is not being held low now, we did not get called
853	 * within 65535us.
854	 */
855	if (inb(pcl720_data_16_23(PCL720_IOB)) & 0x01) {
856		NLOG(NLOG_CLOCKINFO) {
857			msyslog(LOG_NOTICE, "PCL-720 out of synch");
858		}
859		return (0);
860	}
861	f = (65536 - pcl720_read(PCL720_IOB, PCL720_CTR));
862#ifdef PPS720_DEBUG
863	msyslog(LOG_DEBUG, "PCL-720: %luus", f);
864#endif
865	return (f);
866}
867#endif
868
869#else
870int refclock_true_bs;
871#endif /* REFCLOCK */
872