1139825Simp/*-
2139825Simp ***********************************************************************
344574Sphk *								       *
475540Sjhay * Copyright (c) David L. Mills 1993-2001			       *
544574Sphk *								       *
644574Sphk * Permission to use, copy, modify, and distribute this software and   *
744574Sphk * its documentation for any purpose and without fee is hereby	       *
844574Sphk * granted, provided that the above copyright notice appears in all    *
944574Sphk * copies and that both the copyright notice and this permission       *
1044574Sphk * notice appear in supporting documentation, and that the name        *
1144574Sphk * University of Delaware not be used in advertising or publicity      *
1244574Sphk * pertaining to distribution of the software without specific,	       *
1344574Sphk * written prior permission. The University of Delaware makes no       *
1444574Sphk * representations about the suitability this software for any	       *
1544574Sphk * purpose. It is provided "as is" without express or implied	       *
1644574Sphk * warranty.							       *
1744574Sphk *								       *
1844574Sphk **********************************************************************/
192291Sjkh
202291Sjkh/*
212291Sjkh * Modification history timex.h
222291Sjkh *
2375540Sjhay * 16 Aug 00	David L. Mills
2465432Sphk *	API Version 4. Added MOD_TAI and tai member of ntptimeval
2565432Sphk *	structure.
2665432Sphk *
2744574Sphk * 17 Nov 98	David L. Mills
2844574Sphk *	Revised for nanosecond kernel and user interface.
2944574Sphk *
3021101Sjhay * 26 Sep 94	David L. Mills
3121101Sjhay *	Added defines for hybrid phase/frequency-lock loop.
3221101Sjhay *
332291Sjkh * 19 Mar 94	David L. Mills
342291Sjkh *	Moved defines from kernel routines to header file and added new
352291Sjkh *	defines for PPS phase-lock loop.
362291Sjkh *
372291Sjkh * 20 Feb 94	David L. Mills
382291Sjkh *	Revised status codes and structures for external clock and PPS
392291Sjkh *	signal discipline.
402291Sjkh *
412291Sjkh * 28 Nov 93	David L. Mills
422291Sjkh *	Adjusted parameters to improve stability and increase poll
432291Sjkh *	interval.
442291Sjkh *
452291Sjkh * 17 Sep 93    David L. Mills
462291Sjkh *      Created file
4755205Speter *
4855205Speter * $FreeBSD$
492291Sjkh */
502291Sjkh/*
512291Sjkh * This header file defines the Network Time Protocol (NTP) interfaces
522291Sjkh * for user and daemon application programs. These are implemented using
5344574Sphk * defined syscalls and data structures and require specific kernel
542291Sjkh * support.
552291Sjkh *
5644574Sphk * The original precision time kernels developed from 1993 have an
5744574Sphk * ultimate resolution of one microsecond; however, the most recent
5844574Sphk * kernels have an ultimate resolution of one nanosecond. In these
5944574Sphk * kernels, a ntp_adjtime() syscalls can be used to determine which
6044574Sphk * resolution is in use and to select either one at any time. The
6144574Sphk * resolution selected affects the scaling of certain fields in the
6244574Sphk * ntp_gettime() and ntp_adjtime() syscalls, as described below.
6344574Sphk *
642291Sjkh * NAME
652291Sjkh *	ntp_gettime - NTP user application interface
662291Sjkh *
672291Sjkh * SYNOPSIS
682291Sjkh *	#include <sys/timex.h>
692291Sjkh *
7044666Sphk *	int ntp_gettime(struct ntptimeval *ntv);
712291Sjkh *
7244574Sphk * DESCRIPTION
7344666Sphk *	The time returned by ntp_gettime() is in a timespec structure,
7444574Sphk *	but may be in either microsecond (seconds and microseconds) or
7544574Sphk *	nanosecond (seconds and nanoseconds) format. The particular
7644574Sphk *	format in use is determined by the STA_NANO bit of the status
7744574Sphk *	word returned by the ntp_adjtime() syscall.
782291Sjkh *
792291Sjkh * NAME
802291Sjkh *	ntp_adjtime - NTP daemon application interface
812291Sjkh *
822291Sjkh * SYNOPSIS
832291Sjkh *	#include <sys/timex.h>
8444574Sphk *	#include <sys/syscall.h>
852291Sjkh *
8644574Sphk *	int syscall(SYS_ntp_adjtime, tptr);
8744666Sphk *	int SYS_ntp_adjtime;
8844574Sphk *	struct timex *tptr;
892291Sjkh *
9044574Sphk * DESCRIPTION
9144574Sphk *	Certain fields of the timex structure are interpreted in either
9244574Sphk *	microseconds or nanoseconds according to the state of the
9344574Sphk *	STA_NANO bit in the status word. See the description below for
9444574Sphk *	further information.
952291Sjkh */
962291Sjkh#ifndef _SYS_TIMEX_H_
9745294Sphk#define _SYS_TIMEX_H_ 1
9865432Sphk#define NTP_API		4	/* NTP API version */
992291Sjkh
1002291Sjkh#ifndef MSDOS			/* Microsoft specific */
1012291Sjkh#include <sys/syscall.h>
1022291Sjkh#endif /* MSDOS */
1032291Sjkh
1042291Sjkh/*
10544574Sphk * The following defines establish the performance envelope of the
10644574Sphk * kernel discipline loop. Phase or frequency errors greater than
10744574Sphk * NAXPHASE or MAXFREQ are clamped to these maxima. For update intervals
10844574Sphk * less than MINSEC, the loop always operates in PLL mode; while, for
10944574Sphk * update intervals greater than MAXSEC, the loop always operates in FLL
11044574Sphk * mode. Between these two limits the operating mode is selected by the
11144574Sphk * STA_FLL bit in the status word.
1122291Sjkh */
11344574Sphk#define MAXPHASE	500000000L /* max phase error (ns) */
11444574Sphk#define MAXFREQ		500000L	/* max freq error (ns/s) */
11544574Sphk#define MINSEC		256	/* min FLL update interval (s) */
11645294Sphk#define MAXSEC		2048	/* max PLL update interval (s) */
11744574Sphk#define NANOSECOND	1000000000L /* nanoseconds in one second */
11844574Sphk#define SCALE_PPM	(65536 / 1000) /* crude ns/s to scaled PPM */
11950656Sphk#define MAXTC		10	/* max time constant */
1202291Sjkh
1212291Sjkh/*
1222291Sjkh * The following defines and structures define the user interface for
12344574Sphk * the ntp_gettime() and ntp_adjtime() syscalls.
1242291Sjkh *
12545294Sphk * Control mode codes (timex.modes)
1262291Sjkh */
1272291Sjkh#define MOD_OFFSET	0x0001	/* set time offset */
1282291Sjkh#define MOD_FREQUENCY	0x0002	/* set frequency offset */
1292291Sjkh#define MOD_MAXERROR	0x0004	/* set maximum time error */
1302291Sjkh#define MOD_ESTERROR	0x0008	/* set estimated time error */
1312291Sjkh#define MOD_STATUS	0x0010	/* set clock status bits */
13244574Sphk#define MOD_TIMECONST	0x0020	/* set PLL time constant */
13350656Sphk#define MOD_PPSMAX	0x0040	/* set PPS maximum averaging time */
13465432Sphk#define MOD_TAI		0x0080	/* set TAI offset */
13544574Sphk#define	MOD_MICRO	0x1000	/* select microsecond resolution */
13644574Sphk#define	MOD_NANO	0x2000	/* select nanosecond resolution */
13744574Sphk#define MOD_CLKB	0x4000	/* select clock B */
13844574Sphk#define MOD_CLKA	0x8000	/* select clock A */
1392291Sjkh
1402291Sjkh/*
1412291Sjkh * Status codes (timex.status)
1422291Sjkh */
1432291Sjkh#define STA_PLL		0x0001	/* enable PLL updates (rw) */
1442291Sjkh#define STA_PPSFREQ	0x0002	/* enable PPS freq discipline (rw) */
1452291Sjkh#define STA_PPSTIME	0x0004	/* enable PPS time discipline (rw) */
14644574Sphk#define STA_FLL		0x0008	/* enable FLL mode (rw) */
1472291Sjkh#define STA_INS		0x0010	/* insert leap (rw) */
1482291Sjkh#define STA_DEL		0x0020	/* delete leap (rw) */
1492291Sjkh#define STA_UNSYNC	0x0040	/* clock unsynchronized (rw) */
15021101Sjhay#define STA_FREQHOLD	0x0080	/* hold frequency (rw) */
1512291Sjkh#define STA_PPSSIGNAL	0x0100	/* PPS signal present (ro) */
1522291Sjkh#define STA_PPSJITTER	0x0200	/* PPS signal jitter exceeded (ro) */
1532291Sjkh#define STA_PPSWANDER	0x0400	/* PPS signal wander exceeded (ro) */
1542291Sjkh#define STA_PPSERROR	0x0800	/* PPS signal calibration error (ro) */
1552291Sjkh#define STA_CLOCKERR	0x1000	/* clock hardware fault (ro) */
15644574Sphk#define STA_NANO	0x2000	/* resolution (0 = us, 1 = ns) (ro) */
15744574Sphk#define STA_MODE	0x4000	/* mode (0 = PLL, 1 = FLL) (ro) */
15844574Sphk#define STA_CLK		0x8000	/* clock source (0 = A, 1 = B) (ro) */
1592291Sjkh
1602291Sjkh#define STA_RONLY (STA_PPSSIGNAL | STA_PPSJITTER | STA_PPSWANDER | \
16144574Sphk    STA_PPSERROR | STA_CLOCKERR | STA_NANO | STA_MODE | STA_CLK)
1622291Sjkh
1632291Sjkh/*
1642291Sjkh * Clock states (time_state)
1652291Sjkh */
1662291Sjkh#define TIME_OK		0	/* no leap second warning */
1672291Sjkh#define TIME_INS	1	/* insert leap second warning */
1682291Sjkh#define TIME_DEL	2	/* delete leap second warning */
1692291Sjkh#define TIME_OOP	3	/* leap second in progress */
1702291Sjkh#define TIME_WAIT	4	/* leap second has occured */
17144574Sphk#define TIME_ERROR	5	/* error (see status word) */
1722291Sjkh
1732291Sjkh/*
1742291Sjkh * NTP user interface (ntp_gettime()) - used to read kernel clock values
1752291Sjkh *
17644574Sphk * Note: The time member is in microseconds if STA_NANO is zero and
17744574Sphk * nanoseconds if not.
1782291Sjkh */
1792291Sjkhstruct ntptimeval {
18045294Sphk	struct timespec time;	/* current time (ns) (ro) */
1812291Sjkh	long maxerror;		/* maximum error (us) (ro) */
1822291Sjkh	long esterror;		/* estimated error (us) (ro) */
18365432Sphk	long tai;		/* TAI offset */
18444574Sphk	int time_state;		/* time status */
1852291Sjkh};
1862291Sjkh
1872291Sjkh/*
18844574Sphk * NTP daemon interface (ntp_adjtime()) - used to discipline CPU clock
18944574Sphk * oscillator and determine status.
19044574Sphk *
19144574Sphk * Note: The offset, precision and jitter members are in microseconds if
19244574Sphk * STA_NANO is zero and nanoseconds if not.
1932291Sjkh */
1942291Sjkhstruct timex {
1952291Sjkh	unsigned int modes;	/* clock mode bits (wo) */
19644574Sphk	long	offset;		/* time offset (ns/us) (rw) */
19744574Sphk	long	freq;		/* frequency offset (scaled PPM) (rw) */
19844574Sphk	long	maxerror;	/* maximum error (us) (rw) */
19944574Sphk	long	esterror;	/* estimated error (us) (rw) */
20044574Sphk	int	status;		/* clock status bits (rw) */
20144574Sphk	long	constant;	/* poll interval (log2 s) (rw) */
20244574Sphk	long	precision;	/* clock precision (ns/us) (ro) */
20344574Sphk	long	tolerance;	/* clock frequency tolerance (scaled
20444574Sphk				 * PPM) (ro) */
2052291Sjkh	/*
2062291Sjkh	 * The following read-only structure members are implemented
2072291Sjkh	 * only if the PPS signal discipline is configured in the
20844574Sphk	 * kernel. They are included in all configurations to insure
20944574Sphk	 * portability.
2102291Sjkh	 */
21144574Sphk	long	ppsfreq;	/* PPS frequency (scaled PPM) (ro) */
21244574Sphk	long	jitter;		/* PPS jitter (ns/us) (ro) */
21344574Sphk	int	shift;		/* interval duration (s) (shift) (ro) */
21444574Sphk	long	stabil;		/* PPS stability (scaled PPM) (ro) */
21544574Sphk	long	jitcnt;		/* jitter limit exceeded (ro) */
21644574Sphk	long	calcnt;		/* calibration intervals (ro) */
21744574Sphk	long	errcnt;		/* calibration errors (ro) */
21844574Sphk	long	stbcnt;		/* stability limit exceeded (ro) */
21944574Sphk};
2202291Sjkh
2212291Sjkh#ifdef __FreeBSD__
2222291Sjkh
22355205Speter#ifdef _KERNEL
22495529Sphkvoid	ntp_update_second(int64_t *adjustment, time_t *newsec);
22555205Speter#else /* !_KERNEL */
2262291Sjkh#include <sys/cdefs.h>
2272291Sjkh
2282291Sjkh__BEGIN_DECLS
22992719Salfredint	ntp_adjtime(struct timex *);
23092719Salfredint	ntp_gettime(struct ntptimeval *);
2312291Sjkh__END_DECLS
23255205Speter#endif /* _KERNEL */
2332291Sjkh
23444666Sphk#endif /* __FreeBSD__ */
2352291Sjkh
23644666Sphk#endif /* !_SYS_TIMEX_H_ */
237