1/*-
2 ***********************************************************************
3 *								       *
4 * Copyright (c) David L. Mills 1993-2001			       *
5 * Copyright (c) Poul-Henning Kamp 2000-2001                           *
6 *								       *
7 * Permission to use, copy, modify, and distribute this software and   *
8 * its documentation for any purpose and without fee is hereby	       *
9 * granted, provided that the above copyright notice appears in all    *
10 * copies and that both the copyright notice and this permission       *
11 * notice appear in supporting documentation, and that the name        *
12 * University of Delaware not be used in advertising or publicity      *
13 * pertaining to distribution of the software without specific,	       *
14 * written prior permission. The University of Delaware makes no       *
15 * representations about the suitability this software for any	       *
16 * purpose. It is provided "as is" without express or implied	       *
17 * warranty.							       *
18 *								       *
19 ***********************************************************************
20 *
21 * This header file defines the Network Time Protocol (NTP) interfaces
22 * for user and daemon application programs.
23 *
24 * This file was originally created 17 Sep 93 by David L. Mills, Professor
25 * of University of Delaware, building on work which had already been ongoing
26 * for a decade and a half at that point in time.
27 *
28 * In 2000 the APIs got a upgrade from microseconds to nanoseconds,
29 * a joint work between Poul-Henning Kamp and David L. Mills.
30 *
31 */
32
33#ifndef _SYS_TIMEX_H_
34#define _SYS_TIMEX_H_ 1
35
36#define NTP_API		4		/* NTP API version */
37
38#ifdef __FreeBSD__
39#include <sys/_timespec.h>
40#endif /* __FreeBSD__ */
41
42/*
43 * The following defines establish the performance envelope of the
44 * kernel discipline loop. Phase or frequency errors greater than
45 * NAXPHASE or MAXFREQ are clamped to these maxima. For update intervals
46 * less than MINSEC, the loop always operates in PLL mode; while, for
47 * update intervals greater than MAXSEC, the loop always operates in FLL
48 * mode. Between these two limits the operating mode is selected by the
49 * STA_FLL bit in the status word.
50 */
51
52#define MAXPHASE	500000000L	/* max phase error (ns) */
53#define MAXFREQ		500000L		/* max freq error (ns/s) */
54#define MINSEC		256		/* min FLL update interval (s) */
55#define MAXSEC		2048		/* max PLL update interval (s) */
56#define NANOSECOND	1000000000L	/* nanoseconds in one second */
57#define SCALE_PPM	(65536 / 1000)	/* crude ns/s to scaled PPM */
58#define MAXTC		10		/* max time constant */
59
60/*
61 * Control mode codes (timex.modes)
62 */
63#define MOD_OFFSET	0x0001		/* set time offset */
64#define MOD_FREQUENCY	0x0002		/* set frequency offset */
65#define MOD_MAXERROR	0x0004		/* set maximum time error */
66#define MOD_ESTERROR	0x0008		/* set estimated time error */
67#define MOD_STATUS	0x0010		/* set clock status bits */
68#define MOD_TIMECONST	0x0020		/* set PLL time constant */
69#define MOD_PPSMAX	0x0040		/* set PPS maximum averaging time */
70#define MOD_TAI		0x0080		/* set TAI offset */
71#define	MOD_MICRO	0x1000		/* select microsecond resolution */
72#define	MOD_NANO	0x2000		/* select nanosecond resolution */
73#define MOD_CLKB	0x4000		/* select clock B */
74#define MOD_CLKA	0x8000		/* select clock A */
75
76/*
77 * Status codes (timex.status)
78 */
79#define STA_PLL		0x0001		/* enable PLL updates (rw) */
80#define STA_PPSFREQ	0x0002		/* enable PPS freq discipline (rw) */
81#define STA_PPSTIME	0x0004		/* enable PPS time discipline (rw) */
82#define STA_FLL		0x0008		/* enable FLL mode (rw) */
83#define STA_INS		0x0010		/* insert leap (rw) */
84#define STA_DEL		0x0020		/* delete leap (rw) */
85#define STA_UNSYNC	0x0040		/* clock unsynchronized (rw) */
86#define STA_FREQHOLD	0x0080		/* hold frequency (rw) */
87#define STA_PPSSIGNAL	0x0100		/* PPS signal present (ro) */
88#define STA_PPSJITTER	0x0200		/* PPS signal jitter exceeded (ro) */
89#define STA_PPSWANDER	0x0400		/* PPS signal wander exceeded (ro) */
90#define STA_PPSERROR	0x0800		/* PPS signal calibration error (ro) */
91#define STA_CLOCKERR	0x1000		/* clock hardware fault (ro) */
92#define STA_NANO	0x2000		/* resolution (0 = us, 1 = ns) (ro) */
93#define STA_MODE	0x4000		/* mode (0 = PLL, 1 = FLL) (ro) */
94#define STA_CLK		0x8000		/* clock source (0 = A, 1 = B) (ro) */
95
96#define STA_RONLY (STA_PPSSIGNAL | STA_PPSJITTER | STA_PPSWANDER | \
97    STA_PPSERROR | STA_CLOCKERR | STA_NANO | STA_MODE | STA_CLK)
98
99/*
100 * Clock states (ntptimeval.time_state)
101 */
102#define TIME_OK		0		/* no leap second warning */
103#define TIME_INS	1		/* insert leap second warning */
104#define TIME_DEL	2		/* delete leap second warning */
105#define TIME_OOP	3		/* leap second in progress */
106#define TIME_WAIT	4		/* leap second has occurred */
107#define TIME_ERROR	5		/* error (see status word) */
108
109/*
110 * NTP user interface -- ntp_gettime(2) - used to read kernel clock values
111 */
112struct ntptimeval {
113	struct timespec time;		/* current time (ns) (ro) */
114	long maxerror;			/* maximum error (us) (ro) */
115	long esterror;			/* estimated error (us) (ro) */
116	long tai;			/* TAI offset */
117	int time_state;			/* time status */
118};
119
120/*
121 * NTP daemon interface -- ntp_adjtime(2) -- used to discipline CPU clock
122 * oscillator and control/determine status.
123 *
124 * Note: The offset, precision and jitter members are in microseconds if
125 * STA_NANO is zero and nanoseconds if not.
126 */
127struct timex {
128	unsigned int modes;		/* clock mode bits (wo) */
129	long	offset;			/* time offset (ns/us) (rw) */
130	long	freq;			/* frequency offset (scaled PPM) (rw) */
131	long	maxerror;		/* maximum error (us) (rw) */
132	long	esterror;		/* estimated error (us) (rw) */
133	int	status;			/* clock status bits (rw) */
134	long	constant;		/* poll interval (log2 s) (rw) */
135	long	precision;		/* clock precision (ns/us) (ro) */
136	long	tolerance;		/* clock frequency tolerance (scaled
137				 	 * PPM) (ro) */
138	/*
139	 * The following read-only structure members are implemented
140	 * only if the PPS signal discipline is configured in the
141	 * kernel. They are included in all configurations to insure
142	 * portability.
143	 */
144	long	ppsfreq;		/* PPS frequency (scaled PPM) (ro) */
145	long	jitter;			/* PPS jitter (ns/us) (ro) */
146	int	shift;			/* interval duration (s) (shift) (ro) */
147	long	stabil;			/* PPS stability (scaled PPM) (ro) */
148	long	jitcnt;			/* jitter limit exceeded (ro) */
149	long	calcnt;			/* calibration intervals (ro) */
150	long	errcnt;			/* calibration errors (ro) */
151	long	stbcnt;			/* stability limit exceeded (ro) */
152};
153
154#ifdef __FreeBSD__
155
156#ifdef _KERNEL
157void	ntp_update_second(int64_t *adjustment, time_t *newsec);
158#else /* !_KERNEL */
159#include <sys/cdefs.h>
160
161__BEGIN_DECLS
162int	ntp_adjtime(struct timex *);
163int	ntp_gettime(struct ntptimeval *);
164__END_DECLS
165#endif /* _KERNEL */
166
167#endif /* __FreeBSD__ */
168
169#endif /* !_SYS_TIMEX_H_ */
170