| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * linux/kernel/time/ntp.c | 
|  | 3 | * | 
|  | 4 | * NTP state machine interfaces and logic. | 
|  | 5 | * | 
|  | 6 | * This code was mainly moved from kernel/timer.c and kernel/time.c | 
|  | 7 | * Please see those files for relevant copyright info and historical | 
|  | 8 | * changelogs. | 
|  | 9 | */ | 
|  | 10 |  | 
|  | 11 | #include <linux/mm.h> | 
|  | 12 | #include <linux/time.h> | 
| Thomas Gleixner | 8264445 | 2007-07-21 04:37:37 -0700 | [diff] [blame] | 13 | #include <linux/timer.h> | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 14 | #include <linux/timex.h> | 
| Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 15 | #include <linux/jiffies.h> | 
|  | 16 | #include <linux/hrtimer.h> | 
| Alexey Dobriyan | aa0ac36 | 2007-07-15 23:40:39 -0700 | [diff] [blame] | 17 | #include <linux/capability.h> | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 18 | #include <asm/div64.h> | 
|  | 19 | #include <asm/timex.h> | 
|  | 20 |  | 
| Roman Zippel | b0ee755 | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 21 | /* | 
|  | 22 | * Timekeeping variables | 
|  | 23 | */ | 
|  | 24 | unsigned long tick_usec = TICK_USEC; 		/* USER_HZ period (usec) */ | 
|  | 25 | unsigned long tick_nsec;			/* ACTHZ period (nsec) */ | 
|  | 26 | static u64 tick_length, tick_length_base; | 
|  | 27 |  | 
| Roman Zippel | 8f807f8 | 2006-09-30 23:28:25 -0700 | [diff] [blame] | 28 | #define MAX_TICKADJ		500		/* microsecs */ | 
|  | 29 | #define MAX_TICKADJ_SCALED	(((u64)(MAX_TICKADJ * NSEC_PER_USEC) << \ | 
| john stultz | f4304ab | 2007-02-16 01:27:26 -0800 | [diff] [blame] | 30 | TICK_LENGTH_SHIFT) / NTP_INTERVAL_FREQ) | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 31 |  | 
|  | 32 | /* | 
|  | 33 | * phase-lock loop variables | 
|  | 34 | */ | 
|  | 35 | /* TIME_ERROR prevents overwriting the CMOS clock */ | 
| Adrian Bunk | 70bc42f | 2006-09-30 23:28:29 -0700 | [diff] [blame] | 36 | static int time_state = TIME_OK;	/* clock synchronization status	*/ | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 37 | int time_status = STA_UNSYNC;		/* clock status bits		*/ | 
| john stultz | d62ac21 | 2007-03-26 21:32:26 -0800 | [diff] [blame] | 38 | static s64 time_offset;		/* time adjustment (ns)		*/ | 
| Adrian Bunk | 70bc42f | 2006-09-30 23:28:29 -0700 | [diff] [blame] | 39 | static long time_constant = 2;		/* pll time constant		*/ | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 40 | long time_maxerror = NTP_PHASE_LIMIT;	/* maximum error (us)		*/ | 
|  | 41 | long time_esterror = NTP_PHASE_LIMIT;	/* estimated error (us)		*/ | 
| Roman Zippel | dc6a43e | 2006-09-30 23:28:24 -0700 | [diff] [blame] | 42 | long time_freq;				/* frequency offset (scaled ppm)*/ | 
| Adrian Bunk | 70bc42f | 2006-09-30 23:28:29 -0700 | [diff] [blame] | 43 | static long time_reftime;		/* time at last adjustment (s)	*/ | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 44 | long time_adjust; | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 45 |  | 
| Adrian Bunk | 70bc42f | 2006-09-30 23:28:29 -0700 | [diff] [blame] | 46 | #define CLOCK_TICK_OVERFLOW	(LATCH * HZ - CLOCK_TICK_RATE) | 
|  | 47 | #define CLOCK_TICK_ADJUST	(((s64)CLOCK_TICK_OVERFLOW * NSEC_PER_SEC) / \ | 
|  | 48 | (s64)CLOCK_TICK_RATE) | 
|  | 49 |  | 
|  | 50 | static void ntp_update_frequency(void) | 
|  | 51 | { | 
| john stultz | f4304ab | 2007-02-16 01:27:26 -0800 | [diff] [blame] | 52 | u64 second_length = (u64)(tick_usec * NSEC_PER_USEC * USER_HZ) | 
|  | 53 | << TICK_LENGTH_SHIFT; | 
|  | 54 | second_length += (s64)CLOCK_TICK_ADJUST << TICK_LENGTH_SHIFT; | 
|  | 55 | second_length += (s64)time_freq << (TICK_LENGTH_SHIFT - SHIFT_NSEC); | 
| Adrian Bunk | 70bc42f | 2006-09-30 23:28:29 -0700 | [diff] [blame] | 56 |  | 
| john stultz | f4304ab | 2007-02-16 01:27:26 -0800 | [diff] [blame] | 57 | tick_length_base = second_length; | 
| Adrian Bunk | 70bc42f | 2006-09-30 23:28:29 -0700 | [diff] [blame] | 58 |  | 
| john stultz | f4304ab | 2007-02-16 01:27:26 -0800 | [diff] [blame] | 59 | do_div(second_length, HZ); | 
|  | 60 | tick_nsec = second_length >> TICK_LENGTH_SHIFT; | 
|  | 61 |  | 
|  | 62 | do_div(tick_length_base, NTP_INTERVAL_FREQ); | 
| Adrian Bunk | 70bc42f | 2006-09-30 23:28:29 -0700 | [diff] [blame] | 63 | } | 
|  | 64 |  | 
| Roman Zippel | b0ee755 | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 65 | /** | 
|  | 66 | * ntp_clear - Clears the NTP state variables | 
|  | 67 | * | 
|  | 68 | * Must be called while holding a write on the xtime_lock | 
|  | 69 | */ | 
|  | 70 | void ntp_clear(void) | 
|  | 71 | { | 
|  | 72 | time_adjust = 0;		/* stop active adjtime() */ | 
|  | 73 | time_status |= STA_UNSYNC; | 
|  | 74 | time_maxerror = NTP_PHASE_LIMIT; | 
|  | 75 | time_esterror = NTP_PHASE_LIMIT; | 
|  | 76 |  | 
|  | 77 | ntp_update_frequency(); | 
|  | 78 |  | 
|  | 79 | tick_length = tick_length_base; | 
| Roman Zippel | 3d3675c | 2006-09-30 23:28:25 -0700 | [diff] [blame] | 80 | time_offset = 0; | 
| Roman Zippel | b0ee755 | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 81 | } | 
|  | 82 |  | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 83 | /* | 
|  | 84 | * this routine handles the overflow of the microsecond field | 
|  | 85 | * | 
|  | 86 | * The tricky bits of code to handle the accurate clock support | 
|  | 87 | * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame. | 
|  | 88 | * They were originally developed for SUN and DEC kernels. | 
|  | 89 | * All the kudos should go to Dave for this stuff. | 
|  | 90 | */ | 
|  | 91 | void second_overflow(void) | 
|  | 92 | { | 
| Roman Zippel | 3d3675c | 2006-09-30 23:28:25 -0700 | [diff] [blame] | 93 | long time_adj; | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 94 |  | 
|  | 95 | /* Bump the maxerror field */ | 
| Roman Zippel | 97eebe1 | 2006-09-30 23:28:26 -0700 | [diff] [blame] | 96 | time_maxerror += MAXFREQ >> SHIFT_USEC; | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 97 | if (time_maxerror > NTP_PHASE_LIMIT) { | 
|  | 98 | time_maxerror = NTP_PHASE_LIMIT; | 
|  | 99 | time_status |= STA_UNSYNC; | 
|  | 100 | } | 
|  | 101 |  | 
|  | 102 | /* | 
|  | 103 | * Leap second processing. If in leap-insert state at the end of the | 
|  | 104 | * day, the system clock is set back one second; if in leap-delete | 
|  | 105 | * state, the system clock is set ahead one second. The microtime() | 
|  | 106 | * routine or external clock driver will insure that reported time is | 
|  | 107 | * always monotonic. The ugly divides should be replaced. | 
|  | 108 | */ | 
|  | 109 | switch (time_state) { | 
|  | 110 | case TIME_OK: | 
|  | 111 | if (time_status & STA_INS) | 
|  | 112 | time_state = TIME_INS; | 
|  | 113 | else if (time_status & STA_DEL) | 
|  | 114 | time_state = TIME_DEL; | 
|  | 115 | break; | 
|  | 116 | case TIME_INS: | 
|  | 117 | if (xtime.tv_sec % 86400 == 0) { | 
|  | 118 | xtime.tv_sec--; | 
|  | 119 | wall_to_monotonic.tv_sec++; | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 120 | time_state = TIME_OOP; | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 121 | printk(KERN_NOTICE "Clock: inserting leap second " | 
|  | 122 | "23:59:60 UTC\n"); | 
|  | 123 | } | 
|  | 124 | break; | 
|  | 125 | case TIME_DEL: | 
|  | 126 | if ((xtime.tv_sec + 1) % 86400 == 0) { | 
|  | 127 | xtime.tv_sec++; | 
|  | 128 | wall_to_monotonic.tv_sec--; | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 129 | time_state = TIME_WAIT; | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 130 | printk(KERN_NOTICE "Clock: deleting leap second " | 
|  | 131 | "23:59:59 UTC\n"); | 
|  | 132 | } | 
|  | 133 | break; | 
|  | 134 | case TIME_OOP: | 
|  | 135 | time_state = TIME_WAIT; | 
|  | 136 | break; | 
|  | 137 | case TIME_WAIT: | 
|  | 138 | if (!(time_status & (STA_INS | STA_DEL))) | 
|  | 139 | time_state = TIME_OK; | 
|  | 140 | } | 
|  | 141 |  | 
|  | 142 | /* | 
| Roman Zippel | f199239 | 2006-09-30 23:28:28 -0700 | [diff] [blame] | 143 | * Compute the phase adjustment for the next second. The offset is | 
|  | 144 | * reduced by a fixed factor times the time constant. | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 145 | */ | 
| Roman Zippel | b0ee755 | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 146 | tick_length = tick_length_base; | 
| Roman Zippel | f199239 | 2006-09-30 23:28:28 -0700 | [diff] [blame] | 147 | time_adj = shift_right(time_offset, SHIFT_PLL + time_constant); | 
| Roman Zippel | 3d3675c | 2006-09-30 23:28:25 -0700 | [diff] [blame] | 148 | time_offset -= time_adj; | 
|  | 149 | tick_length += (s64)time_adj << (TICK_LENGTH_SHIFT - SHIFT_UPDATE); | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 150 |  | 
| Roman Zippel | 8f807f8 | 2006-09-30 23:28:25 -0700 | [diff] [blame] | 151 | if (unlikely(time_adjust)) { | 
|  | 152 | if (time_adjust > MAX_TICKADJ) { | 
|  | 153 | time_adjust -= MAX_TICKADJ; | 
|  | 154 | tick_length += MAX_TICKADJ_SCALED; | 
|  | 155 | } else if (time_adjust < -MAX_TICKADJ) { | 
|  | 156 | time_adjust += MAX_TICKADJ; | 
|  | 157 | tick_length -= MAX_TICKADJ_SCALED; | 
|  | 158 | } else { | 
| Roman Zippel | 8f807f8 | 2006-09-30 23:28:25 -0700 | [diff] [blame] | 159 | tick_length += (s64)(time_adjust * NSEC_PER_USEC / | 
| john stultz | f4304ab | 2007-02-16 01:27:26 -0800 | [diff] [blame] | 160 | NTP_INTERVAL_FREQ) << TICK_LENGTH_SHIFT; | 
| Jim Houston | bb1d860 | 2006-10-28 10:38:56 -0700 | [diff] [blame] | 161 | time_adjust = 0; | 
| Roman Zippel | 8f807f8 | 2006-09-30 23:28:25 -0700 | [diff] [blame] | 162 | } | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 163 | } | 
|  | 164 | } | 
|  | 165 |  | 
|  | 166 | /* | 
|  | 167 | * Return how long ticks are at the moment, that is, how much time | 
|  | 168 | * update_wall_time_one_tick will add to xtime next time we call it | 
|  | 169 | * (assuming no calls to do_adjtimex in the meantime). | 
|  | 170 | * The return value is in fixed-point nanoseconds shifted by the | 
|  | 171 | * specified number of bits to the right of the binary point. | 
|  | 172 | * This function has no side-effects. | 
|  | 173 | */ | 
|  | 174 | u64 current_tick_length(void) | 
|  | 175 | { | 
| Roman Zippel | 8f807f8 | 2006-09-30 23:28:25 -0700 | [diff] [blame] | 176 | return tick_length; | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 177 | } | 
|  | 178 |  | 
| Thomas Gleixner | 8264445 | 2007-07-21 04:37:37 -0700 | [diff] [blame] | 179 | #ifdef CONFIG_GENERIC_CMOS_UPDATE | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 180 |  | 
| Thomas Gleixner | 8264445 | 2007-07-21 04:37:37 -0700 | [diff] [blame] | 181 | /* Disable the cmos update - used by virtualization and embedded */ | 
|  | 182 | int no_sync_cmos_clock  __read_mostly; | 
|  | 183 |  | 
|  | 184 | static void sync_cmos_clock(unsigned long dummy); | 
|  | 185 |  | 
|  | 186 | static DEFINE_TIMER(sync_cmos_timer, sync_cmos_clock, 0, 0); | 
|  | 187 |  | 
|  | 188 | static void sync_cmos_clock(unsigned long dummy) | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 189 | { | 
| Thomas Gleixner | 8264445 | 2007-07-21 04:37:37 -0700 | [diff] [blame] | 190 | struct timespec now, next; | 
|  | 191 | int fail = 1; | 
|  | 192 |  | 
|  | 193 | /* | 
|  | 194 | * If we have an externally synchronized Linux clock, then update | 
|  | 195 | * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be | 
|  | 196 | * called as close as possible to 500 ms before the new second starts. | 
|  | 197 | * This code is run on a timer.  If the clock is set, that timer | 
|  | 198 | * may not expire at the correct time.  Thus, we adjust... | 
|  | 199 | */ | 
|  | 200 | if (!ntp_synced()) | 
|  | 201 | /* | 
|  | 202 | * Not synced, exit, do not restart a timer (if one is | 
|  | 203 | * running, let it run out). | 
|  | 204 | */ | 
|  | 205 | return; | 
|  | 206 |  | 
|  | 207 | getnstimeofday(&now); | 
| David P. Reed | fa6a1a5 | 2007-11-14 17:49:21 -0500 | [diff] [blame] | 208 | if (abs(now.tv_nsec - (NSEC_PER_SEC / 2)) <= tick_nsec / 2) | 
| Thomas Gleixner | 8264445 | 2007-07-21 04:37:37 -0700 | [diff] [blame] | 209 | fail = update_persistent_clock(now); | 
|  | 210 |  | 
|  | 211 | next.tv_nsec = (NSEC_PER_SEC / 2) - now.tv_nsec; | 
|  | 212 | if (next.tv_nsec <= 0) | 
|  | 213 | next.tv_nsec += NSEC_PER_SEC; | 
|  | 214 |  | 
|  | 215 | if (!fail) | 
|  | 216 | next.tv_sec = 659; | 
|  | 217 | else | 
|  | 218 | next.tv_sec = 0; | 
|  | 219 |  | 
|  | 220 | if (next.tv_nsec >= NSEC_PER_SEC) { | 
|  | 221 | next.tv_sec++; | 
|  | 222 | next.tv_nsec -= NSEC_PER_SEC; | 
|  | 223 | } | 
|  | 224 | mod_timer(&sync_cmos_timer, jiffies + timespec_to_jiffies(&next)); | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 225 | } | 
|  | 226 |  | 
| Thomas Gleixner | 8264445 | 2007-07-21 04:37:37 -0700 | [diff] [blame] | 227 | static void notify_cmos_timer(void) | 
|  | 228 | { | 
| Tony Breeds | 298a5df | 2007-09-11 15:24:03 -0700 | [diff] [blame] | 229 | if (!no_sync_cmos_clock) | 
| Thomas Gleixner | 8264445 | 2007-07-21 04:37:37 -0700 | [diff] [blame] | 230 | mod_timer(&sync_cmos_timer, jiffies + 1); | 
|  | 231 | } | 
|  | 232 |  | 
|  | 233 | #else | 
|  | 234 | static inline void notify_cmos_timer(void) { } | 
|  | 235 | #endif | 
|  | 236 |  | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 237 | /* adjtimex mainly allows reading (and writing, if superuser) of | 
|  | 238 | * kernel time-keeping variables. used by xntpd. | 
|  | 239 | */ | 
|  | 240 | int do_adjtimex(struct timex *txc) | 
|  | 241 | { | 
| john stultz | d62ac21 | 2007-03-26 21:32:26 -0800 | [diff] [blame] | 242 | long mtemp, save_adjust, rem; | 
| Roman Zippel | f199239 | 2006-09-30 23:28:28 -0700 | [diff] [blame] | 243 | s64 freq_adj, temp64; | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 244 | int result; | 
|  | 245 |  | 
|  | 246 | /* In order to modify anything, you gotta be super-user! */ | 
|  | 247 | if (txc->modes && !capable(CAP_SYS_TIME)) | 
|  | 248 | return -EPERM; | 
|  | 249 |  | 
|  | 250 | /* Now we validate the data before disabling interrupts */ | 
|  | 251 |  | 
| John Stultz | 52bfb36 | 2007-11-26 20:42:19 +0100 | [diff] [blame] | 252 | if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT) { | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 253 | /* singleshot must not be used with any other mode bits */ | 
| John Stultz | 52bfb36 | 2007-11-26 20:42:19 +0100 | [diff] [blame] | 254 | if (txc->modes != ADJ_OFFSET_SINGLESHOT && | 
|  | 255 | txc->modes != ADJ_OFFSET_SS_READ) | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 256 | return -EINVAL; | 
| John Stultz | 52bfb36 | 2007-11-26 20:42:19 +0100 | [diff] [blame] | 257 | } | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 258 |  | 
|  | 259 | if (txc->modes != ADJ_OFFSET_SINGLESHOT && (txc->modes & ADJ_OFFSET)) | 
|  | 260 | /* adjustment Offset limited to +- .512 seconds */ | 
|  | 261 | if (txc->offset <= - MAXPHASE || txc->offset >= MAXPHASE ) | 
|  | 262 | return -EINVAL; | 
|  | 263 |  | 
|  | 264 | /* if the quartz is off by more than 10% something is VERY wrong ! */ | 
|  | 265 | if (txc->modes & ADJ_TICK) | 
|  | 266 | if (txc->tick <  900000/USER_HZ || | 
|  | 267 | txc->tick > 1100000/USER_HZ) | 
|  | 268 | return -EINVAL; | 
|  | 269 |  | 
|  | 270 | write_seqlock_irq(&xtime_lock); | 
|  | 271 | result = time_state;	/* mostly `TIME_OK' */ | 
|  | 272 |  | 
|  | 273 | /* Save for later - semantics of adjtime is to return old value */ | 
| Roman Zippel | 8f807f8 | 2006-09-30 23:28:25 -0700 | [diff] [blame] | 274 | save_adjust = time_adjust; | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 275 |  | 
|  | 276 | #if 0	/* STA_CLOCKERR is never set yet */ | 
|  | 277 | time_status &= ~STA_CLOCKERR;		/* reset STA_CLOCKERR */ | 
|  | 278 | #endif | 
|  | 279 | /* If there are input parameters, then process them */ | 
|  | 280 | if (txc->modes) | 
|  | 281 | { | 
|  | 282 | if (txc->modes & ADJ_STATUS)	/* only set allowed bits */ | 
|  | 283 | time_status =  (txc->status & ~STA_RONLY) | | 
|  | 284 | (time_status & STA_RONLY); | 
|  | 285 |  | 
|  | 286 | if (txc->modes & ADJ_FREQUENCY) {	/* p. 22 */ | 
|  | 287 | if (txc->freq > MAXFREQ || txc->freq < -MAXFREQ) { | 
|  | 288 | result = -EINVAL; | 
|  | 289 | goto leave; | 
|  | 290 | } | 
| john stultz | f4304ab | 2007-02-16 01:27:26 -0800 | [diff] [blame] | 291 | time_freq = ((s64)txc->freq * NSEC_PER_USEC) | 
|  | 292 | >> (SHIFT_USEC - SHIFT_NSEC); | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 293 | } | 
|  | 294 |  | 
|  | 295 | if (txc->modes & ADJ_MAXERROR) { | 
|  | 296 | if (txc->maxerror < 0 || txc->maxerror >= NTP_PHASE_LIMIT) { | 
|  | 297 | result = -EINVAL; | 
|  | 298 | goto leave; | 
|  | 299 | } | 
|  | 300 | time_maxerror = txc->maxerror; | 
|  | 301 | } | 
|  | 302 |  | 
|  | 303 | if (txc->modes & ADJ_ESTERROR) { | 
|  | 304 | if (txc->esterror < 0 || txc->esterror >= NTP_PHASE_LIMIT) { | 
|  | 305 | result = -EINVAL; | 
|  | 306 | goto leave; | 
|  | 307 | } | 
|  | 308 | time_esterror = txc->esterror; | 
|  | 309 | } | 
|  | 310 |  | 
|  | 311 | if (txc->modes & ADJ_TIMECONST) {	/* p. 24 */ | 
|  | 312 | if (txc->constant < 0) {	/* NTP v4 uses values > 6 */ | 
|  | 313 | result = -EINVAL; | 
|  | 314 | goto leave; | 
|  | 315 | } | 
| Roman Zippel | f199239 | 2006-09-30 23:28:28 -0700 | [diff] [blame] | 316 | time_constant = min(txc->constant + 4, (long)MAXTC); | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 317 | } | 
|  | 318 |  | 
|  | 319 | if (txc->modes & ADJ_OFFSET) {	/* values checked earlier */ | 
|  | 320 | if (txc->modes == ADJ_OFFSET_SINGLESHOT) { | 
|  | 321 | /* adjtime() is independent from ntp_adjtime() */ | 
| Roman Zippel | 8f807f8 | 2006-09-30 23:28:25 -0700 | [diff] [blame] | 322 | time_adjust = txc->offset; | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 323 | } | 
|  | 324 | else if (time_status & STA_PLL) { | 
| john stultz | d62ac21 | 2007-03-26 21:32:26 -0800 | [diff] [blame] | 325 | time_offset = txc->offset * NSEC_PER_USEC; | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 326 |  | 
|  | 327 | /* | 
|  | 328 | * Scale the phase adjustment and | 
|  | 329 | * clamp to the operating range. | 
|  | 330 | */ | 
| john stultz | d62ac21 | 2007-03-26 21:32:26 -0800 | [diff] [blame] | 331 | time_offset = min(time_offset, (s64)MAXPHASE * NSEC_PER_USEC); | 
|  | 332 | time_offset = max(time_offset, (s64)-MAXPHASE * NSEC_PER_USEC); | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 333 |  | 
|  | 334 | /* | 
|  | 335 | * Select whether the frequency is to be controlled | 
|  | 336 | * and in which mode (PLL or FLL). Clamp to the operating | 
|  | 337 | * range. Ugly multiply/divide should be replaced someday. | 
|  | 338 | */ | 
|  | 339 |  | 
|  | 340 | if (time_status & STA_FREQHOLD || time_reftime == 0) | 
|  | 341 | time_reftime = xtime.tv_sec; | 
|  | 342 | mtemp = xtime.tv_sec - time_reftime; | 
|  | 343 | time_reftime = xtime.tv_sec; | 
| Roman Zippel | f199239 | 2006-09-30 23:28:28 -0700 | [diff] [blame] | 344 |  | 
| john stultz | d62ac21 | 2007-03-26 21:32:26 -0800 | [diff] [blame] | 345 | freq_adj = time_offset * mtemp; | 
| Roman Zippel | f199239 | 2006-09-30 23:28:28 -0700 | [diff] [blame] | 346 | freq_adj = shift_right(freq_adj, time_constant * 2 + | 
|  | 347 | (SHIFT_PLL + 2) * 2 - SHIFT_NSEC); | 
|  | 348 | if (mtemp >= MINSEC && (time_status & STA_FLL || mtemp > MAXSEC)) { | 
| john stultz | d62ac21 | 2007-03-26 21:32:26 -0800 | [diff] [blame] | 349 | temp64 = time_offset << (SHIFT_NSEC - SHIFT_FLL); | 
| Roman Zippel | f199239 | 2006-09-30 23:28:28 -0700 | [diff] [blame] | 350 | if (time_offset < 0) { | 
|  | 351 | temp64 = -temp64; | 
|  | 352 | do_div(temp64, mtemp); | 
|  | 353 | freq_adj -= temp64; | 
|  | 354 | } else { | 
|  | 355 | do_div(temp64, mtemp); | 
|  | 356 | freq_adj += temp64; | 
|  | 357 | } | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 358 | } | 
| Roman Zippel | 04b617e | 2006-09-30 23:28:27 -0700 | [diff] [blame] | 359 | freq_adj += time_freq; | 
|  | 360 | freq_adj = min(freq_adj, (s64)MAXFREQ_NSEC); | 
|  | 361 | time_freq = max(freq_adj, (s64)-MAXFREQ_NSEC); | 
| john stultz | d62ac21 | 2007-03-26 21:32:26 -0800 | [diff] [blame] | 362 | time_offset = div_long_long_rem_signed(time_offset, | 
|  | 363 | NTP_INTERVAL_FREQ, | 
|  | 364 | &rem); | 
|  | 365 | time_offset <<= SHIFT_UPDATE; | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 366 | } /* STA_PLL */ | 
|  | 367 | } /* txc->modes & ADJ_OFFSET */ | 
| Roman Zippel | b0ee755 | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 368 | if (txc->modes & ADJ_TICK) | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 369 | tick_usec = txc->tick; | 
| Roman Zippel | b0ee755 | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 370 |  | 
| Roman Zippel | dc6a43e | 2006-09-30 23:28:24 -0700 | [diff] [blame] | 371 | if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET)) | 
| Roman Zippel | b0ee755 | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 372 | ntp_update_frequency(); | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 373 | } /* txc->modes */ | 
|  | 374 | leave:	if ((time_status & (STA_UNSYNC|STA_CLOCKERR)) != 0) | 
|  | 375 | result = TIME_ERROR; | 
|  | 376 |  | 
| John Stultz | 52bfb36 | 2007-11-26 20:42:19 +0100 | [diff] [blame] | 377 | if ((txc->modes == ADJ_OFFSET_SINGLESHOT) || | 
|  | 378 | (txc->modes == ADJ_OFFSET_SS_READ)) | 
| john stultz | d62ac21 | 2007-03-26 21:32:26 -0800 | [diff] [blame] | 379 | txc->offset = save_adjust; | 
| Roman Zippel | 3d3675c | 2006-09-30 23:28:25 -0700 | [diff] [blame] | 380 | else | 
| john stultz | d62ac21 | 2007-03-26 21:32:26 -0800 | [diff] [blame] | 381 | txc->offset = ((long)shift_right(time_offset, SHIFT_UPDATE)) * | 
|  | 382 | NTP_INTERVAL_FREQ / 1000; | 
|  | 383 | txc->freq	   = (time_freq / NSEC_PER_USEC) << | 
|  | 384 | (SHIFT_USEC - SHIFT_NSEC); | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 385 | txc->maxerror	   = time_maxerror; | 
|  | 386 | txc->esterror	   = time_esterror; | 
|  | 387 | txc->status	   = time_status; | 
|  | 388 | txc->constant	   = time_constant; | 
| Adrian Bunk | 70bc42f | 2006-09-30 23:28:29 -0700 | [diff] [blame] | 389 | txc->precision	   = 1; | 
| Roman Zippel | 97eebe1 | 2006-09-30 23:28:26 -0700 | [diff] [blame] | 390 | txc->tolerance	   = MAXFREQ; | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 391 | txc->tick	   = tick_usec; | 
|  | 392 |  | 
|  | 393 | /* PPS is not implemented, so these are zero */ | 
|  | 394 | txc->ppsfreq	   = 0; | 
|  | 395 | txc->jitter	   = 0; | 
|  | 396 | txc->shift	   = 0; | 
|  | 397 | txc->stabil	   = 0; | 
|  | 398 | txc->jitcnt	   = 0; | 
|  | 399 | txc->calcnt	   = 0; | 
|  | 400 | txc->errcnt	   = 0; | 
|  | 401 | txc->stbcnt	   = 0; | 
|  | 402 | write_sequnlock_irq(&xtime_lock); | 
|  | 403 | do_gettimeofday(&txc->time); | 
| Thomas Gleixner | 8264445 | 2007-07-21 04:37:37 -0700 | [diff] [blame] | 404 | notify_cmos_timer(); | 
| john stultz | 4c7ee8d | 2006-09-30 23:28:22 -0700 | [diff] [blame] | 405 | return(result); | 
|  | 406 | } |