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