1/*-
2 ***********************************************************************
3 *								       *
4 * Copyright (c) David L. Mills 1993-2001			       *
5 *								       *
6 * Permission to use, copy, modify, and distribute this software and   *
7 * its documentation for any purpose and without fee is hereby	       *
8 * granted, provided that the above copyright notice appears in all    *
9 * copies and that both the copyright notice and this permission       *
10 * notice appear in supporting documentation, and that the name        *
11 * University of Delaware not be used in advertising or publicity      *
12 * pertaining to distribution of the software without specific,	       *
13 * written prior permission. The University of Delaware makes no       *
14 * representations about the suitability this software for any	       *
15 * purpose. It is provided "as is" without express or implied	       *
16 * warranty.							       *
17 *								       *
18 **********************************************************************/
19
20/*
21 * Modification history timex.h
22 *
23 * 16 Aug 00	David L. Mills
24 *	API Version 4. Added MOD_TAI and tai member of ntptimeval
25 *	structure.
26 *
27 * 17 Nov 98	David L. Mills
28 *	Revised for nanosecond kernel and user interface.
29 *
30 * 26 Sep 94	David L. Mills
31 *	Added defines for hybrid phase/frequency-lock loop.
32 *
33 * 19 Mar 94	David L. Mills
34 *	Moved defines from kernel routines to header file and added new
35 *	defines for PPS phase-lock loop.
36 *
37 * 20 Feb 94	David L. Mills
38 *	Revised status codes and structures for external clock and PPS
39 *	signal discipline.
40 *
41 * 28 Nov 93	David L. Mills
42 *	Adjusted parameters to improve stability and increase poll
43 *	interval.
44 *
45 * 17 Sep 93    David L. Mills
46 *      Created file
47 *
48 * $FreeBSD$
49 */
50/*
51 * This header file defines the Network Time Protocol (NTP) interfaces
52 * for user and daemon application programs. These are implemented using
53 * defined syscalls and data structures and require specific kernel
54 * support.
55 *
56 * The original precision time kernels developed from 1993 have an
57 * ultimate resolution of one microsecond; however, the most recent
58 * kernels have an ultimate resolution of one nanosecond. In these
59 * kernels, a ntp_adjtime() syscalls can be used to determine which
60 * resolution is in use and to select either one at any time. The
61 * resolution selected affects the scaling of certain fields in the
62 * ntp_gettime() and ntp_adjtime() syscalls, as described below.
63 *
64 * NAME
65 *	ntp_gettime - NTP user application interface
66 *
67 * SYNOPSIS
68 *	#include <sys/timex.h>
69 *
70 *	int ntp_gettime(struct ntptimeval *ntv);
71 *
72 * DESCRIPTION
73 *	The time returned by ntp_gettime() is in a timespec structure,
74 *	but may be in either microsecond (seconds and microseconds) or
75 *	nanosecond (seconds and nanoseconds) format. The particular
76 *	format in use is determined by the STA_NANO bit of the status
77 *	word returned by the ntp_adjtime() syscall.
78 *
79 * NAME
80 *	ntp_adjtime - NTP daemon application interface
81 *
82 * SYNOPSIS
83 *	#include <sys/timex.h>
84 *	#include <sys/syscall.h>
85 *
86 *	int syscall(SYS_ntp_adjtime, tptr);
87 *	int SYS_ntp_adjtime;
88 *	struct timex *tptr;
89 *
90 * DESCRIPTION
91 *	Certain fields of the timex structure are interpreted in either
92 *	microseconds or nanoseconds according to the state of the
93 *	STA_NANO bit in the status word. See the description below for
94 *	further information.
95 */
96#ifndef _SYS_TIMEX_H_
97#define _SYS_TIMEX_H_ 1
98#define NTP_API		4	/* NTP API version */
99
100#ifdef __FreeBSD__
101#include <sys/_timespec.h>
102#endif /* __FreeBSD__ */
103#ifndef MSDOS			/* Microsoft specific */
104#include <sys/syscall.h>
105#endif /* MSDOS */
106
107/*
108 * The following defines establish the performance envelope of the
109 * kernel discipline loop. Phase or frequency errors greater than
110 * NAXPHASE or MAXFREQ are clamped to these maxima. For update intervals
111 * less than MINSEC, the loop always operates in PLL mode; while, for
112 * update intervals greater than MAXSEC, the loop always operates in FLL
113 * mode. Between these two limits the operating mode is selected by the
114 * STA_FLL bit in the status word.
115 */
116#define MAXPHASE	500000000L /* max phase error (ns) */
117#define MAXFREQ		500000L	/* max freq error (ns/s) */
118#define MINSEC		256	/* min FLL update interval (s) */
119#define MAXSEC		2048	/* max PLL update interval (s) */
120#define NANOSECOND	1000000000L /* nanoseconds in one second */
121#define SCALE_PPM	(65536 / 1000) /* crude ns/s to scaled PPM */
122#define MAXTC		10	/* max time constant */
123
124/*
125 * The following defines and structures define the user interface for
126 * the ntp_gettime() and ntp_adjtime() syscalls.
127 *
128 * Control mode codes (timex.modes)
129 */
130#define MOD_OFFSET	0x0001	/* set time offset */
131#define MOD_FREQUENCY	0x0002	/* set frequency offset */
132#define MOD_MAXERROR	0x0004	/* set maximum time error */
133#define MOD_ESTERROR	0x0008	/* set estimated time error */
134#define MOD_STATUS	0x0010	/* set clock status bits */
135#define MOD_TIMECONST	0x0020	/* set PLL time constant */
136#define MOD_PPSMAX	0x0040	/* set PPS maximum averaging time */
137#define MOD_TAI		0x0080	/* set TAI offset */
138#define	MOD_MICRO	0x1000	/* select microsecond resolution */
139#define	MOD_NANO	0x2000	/* select nanosecond resolution */
140#define MOD_CLKB	0x4000	/* select clock B */
141#define MOD_CLKA	0x8000	/* select clock A */
142
143/*
144 * Status codes (timex.status)
145 */
146#define STA_PLL		0x0001	/* enable PLL updates (rw) */
147#define STA_PPSFREQ	0x0002	/* enable PPS freq discipline (rw) */
148#define STA_PPSTIME	0x0004	/* enable PPS time discipline (rw) */
149#define STA_FLL		0x0008	/* enable FLL mode (rw) */
150#define STA_INS		0x0010	/* insert leap (rw) */
151#define STA_DEL		0x0020	/* delete leap (rw) */
152#define STA_UNSYNC	0x0040	/* clock unsynchronized (rw) */
153#define STA_FREQHOLD	0x0080	/* hold frequency (rw) */
154#define STA_PPSSIGNAL	0x0100	/* PPS signal present (ro) */
155#define STA_PPSJITTER	0x0200	/* PPS signal jitter exceeded (ro) */
156#define STA_PPSWANDER	0x0400	/* PPS signal wander exceeded (ro) */
157#define STA_PPSERROR	0x0800	/* PPS signal calibration error (ro) */
158#define STA_CLOCKERR	0x1000	/* clock hardware fault (ro) */
159#define STA_NANO	0x2000	/* resolution (0 = us, 1 = ns) (ro) */
160#define STA_MODE	0x4000	/* mode (0 = PLL, 1 = FLL) (ro) */
161#define STA_CLK		0x8000	/* clock source (0 = A, 1 = B) (ro) */
162
163#define STA_RONLY (STA_PPSSIGNAL | STA_PPSJITTER | STA_PPSWANDER | \
164    STA_PPSERROR | STA_CLOCKERR | STA_NANO | STA_MODE | STA_CLK)
165
166/*
167 * Clock states (time_state)
168 */
169#define TIME_OK		0	/* no leap second warning */
170#define TIME_INS	1	/* insert leap second warning */
171#define TIME_DEL	2	/* delete leap second warning */
172#define TIME_OOP	3	/* leap second in progress */
173#define TIME_WAIT	4	/* leap second has occured */
174#define TIME_ERROR	5	/* error (see status word) */
175
176/*
177 * NTP user interface (ntp_gettime()) - used to read kernel clock values
178 *
179 * Note: The time member is in microseconds if STA_NANO is zero and
180 * nanoseconds if not.
181 */
182struct ntptimeval {
183	struct timespec time;	/* current time (ns) (ro) */
184	long maxerror;		/* maximum error (us) (ro) */
185	long esterror;		/* estimated error (us) (ro) */
186	long tai;		/* TAI offset */
187	int time_state;		/* time status */
188};
189
190/*
191 * NTP daemon interface (ntp_adjtime()) - used to discipline CPU clock
192 * oscillator and determine status.
193 *
194 * Note: The offset, precision and jitter members are in microseconds if
195 * STA_NANO is zero and nanoseconds if not.
196 */
197struct timex {
198	unsigned int modes;	/* clock mode bits (wo) */
199	long	offset;		/* time offset (ns/us) (rw) */
200	long	freq;		/* frequency offset (scaled PPM) (rw) */
201	long	maxerror;	/* maximum error (us) (rw) */
202	long	esterror;	/* estimated error (us) (rw) */
203	int	status;		/* clock status bits (rw) */
204	long	constant;	/* poll interval (log2 s) (rw) */
205	long	precision;	/* clock precision (ns/us) (ro) */
206	long	tolerance;	/* clock frequency tolerance (scaled
207				 * PPM) (ro) */
208	/*
209	 * The following read-only structure members are implemented
210	 * only if the PPS signal discipline is configured in the
211	 * kernel. They are included in all configurations to insure
212	 * portability.
213	 */
214	long	ppsfreq;	/* PPS frequency (scaled PPM) (ro) */
215	long	jitter;		/* PPS jitter (ns/us) (ro) */
216	int	shift;		/* interval duration (s) (shift) (ro) */
217	long	stabil;		/* PPS stability (scaled PPM) (ro) */
218	long	jitcnt;		/* jitter limit exceeded (ro) */
219	long	calcnt;		/* calibration intervals (ro) */
220	long	errcnt;		/* calibration errors (ro) */
221	long	stbcnt;		/* stability limit exceeded (ro) */
222};
223
224#ifdef __FreeBSD__
225
226#ifdef _KERNEL
227void	ntp_update_second(int64_t *adjustment, time_t *newsec);
228#else /* !_KERNEL */
229#include <sys/cdefs.h>
230
231__BEGIN_DECLS
232int	ntp_adjtime(struct timex *);
233int	ntp_gettime(struct ntptimeval *);
234__END_DECLS
235#endif /* _KERNEL */
236
237#endif /* __FreeBSD__ */
238
239#endif /* !_SYS_TIMEX_H_ */
240