Don't try to "validate" a non-existing timeval.
[pandora-kernel.git] / kernel / time.c
index 40c2410..1f23e68 100644 (file)
@@ -29,6 +29,7 @@
 
 #include <linux/module.h>
 #include <linux/timex.h>
+#include <linux/capability.h>
 #include <linux/errno.h>
 #include <linux/smp_lock.h>
 #include <linux/syscalls.h>
@@ -154,6 +155,9 @@ int do_sys_settimeofday(struct timespec *tv, struct timezone *tz)
        static int firsttime = 1;
        int error = 0;
 
+       if (tv && !timespec_valid(tv))
+               return -EINVAL;
+
        error = security_settime(tv, tz);
        if (error)
                return error;
@@ -338,30 +342,20 @@ int do_adjtimex(struct timex *txc)
                        if (mtemp >= MINSEC) {
                            ltemp = (time_offset / mtemp) << (SHIFT_USEC -
                                                              SHIFT_UPDATE);
-                           if (ltemp < 0)
-                               time_freq -= -ltemp >> SHIFT_KH;
-                           else
-                               time_freq += ltemp >> SHIFT_KH;
+                           time_freq += shift_right(ltemp, SHIFT_KH);
                        } else /* calibration interval too short (p. 12) */
                                result = TIME_ERROR;
                    } else {    /* PLL mode */
                        if (mtemp < MAXSEC) {
                            ltemp *= mtemp;
-                           if (ltemp < 0)
-                               time_freq -= -ltemp >> (time_constant +
-                                                       time_constant +
-                                                       SHIFT_KF - SHIFT_USEC);
-                           else
-                               time_freq += ltemp >> (time_constant +
+                           time_freq += shift_right(ltemp,(time_constant +
                                                       time_constant +
-                                                      SHIFT_KF - SHIFT_USEC);
+                                                      SHIFT_KF - SHIFT_USEC));
                        } else /* calibration interval too long (p. 12) */
                                result = TIME_ERROR;
                    }
-                   if (time_freq > time_tolerance)
-                       time_freq = time_tolerance;
-                   else if (time_freq < -time_tolerance)
-                       time_freq = -time_tolerance;
+                   time_freq = min(time_freq, time_tolerance);
+                   time_freq = max(time_freq, -time_tolerance);
                } /* STA_PLL || STA_PPSTIME */
            } /* txc->modes & ADJ_OFFSET */
            if (txc->modes & ADJ_TICK) {
@@ -384,10 +378,7 @@ leave:     if ((time_status & (STA_UNSYNC|STA_CLOCKERR)) != 0
        if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT)
            txc->offset    = save_adjust;
        else {
-           if (time_offset < 0)
-               txc->offset = -(-time_offset >> SHIFT_UPDATE);
-           else
-               txc->offset = time_offset >> SHIFT_UPDATE;
+           txc->offset = shift_right(time_offset, SHIFT_UPDATE);
        }
        txc->freq          = time_freq + pps_freq;
        txc->maxerror      = time_maxerror;
@@ -532,6 +523,7 @@ int do_settimeofday (struct timespec *tv)
        clock_was_set();
        return 0;
 }
+EXPORT_SYMBOL(do_settimeofday);
 
 void do_gettimeofday (struct timeval *tv)
 {
@@ -573,6 +565,108 @@ void getnstimeofday(struct timespec *tv)
 EXPORT_SYMBOL_GPL(getnstimeofday);
 #endif
 
+/* Converts Gregorian date to seconds since 1970-01-01 00:00:00.
+ * Assumes input in normal date format, i.e. 1980-12-31 23:59:59
+ * => year=1980, mon=12, day=31, hour=23, min=59, sec=59.
+ *
+ * [For the Julian calendar (which was used in Russia before 1917,
+ * Britain & colonies before 1752, anywhere else before 1582,
+ * and is still in use by some communities) leave out the
+ * -year/100+year/400 terms, and add 10.]
+ *
+ * This algorithm was first published by Gauss (I think).
+ *
+ * WARNING: this function will overflow on 2106-02-07 06:28:16 on
+ * machines were long is 32-bit! (However, as time_t is signed, we
+ * will already get problems at other places on 2038-01-19 03:14:08)
+ */
+unsigned long
+mktime(const unsigned int year0, const unsigned int mon0,
+       const unsigned int day, const unsigned int hour,
+       const unsigned int min, const unsigned int sec)
+{
+       unsigned int mon = mon0, year = year0;
+
+       /* 1..12 -> 11,12,1..10 */
+       if (0 >= (int) (mon -= 2)) {
+               mon += 12;      /* Puts Feb last since it has leap day */
+               year -= 1;
+       }
+
+       return ((((unsigned long)
+                 (year/4 - year/100 + year/400 + 367*mon/12 + day) +
+                 year*365 - 719499
+           )*24 + hour /* now have hours */
+         )*60 + min /* now have minutes */
+       )*60 + sec; /* finally seconds */
+}
+
+EXPORT_SYMBOL(mktime);
+
+/**
+ * set_normalized_timespec - set timespec sec and nsec parts and normalize
+ *
+ * @ts:                pointer to timespec variable to be set
+ * @sec:       seconds to set
+ * @nsec:      nanoseconds to set
+ *
+ * Set seconds and nanoseconds field of a timespec variable and
+ * normalize to the timespec storage format
+ *
+ * Note: The tv_nsec part is always in the range of
+ *     0 <= tv_nsec < NSEC_PER_SEC
+ * For negative values only the tv_sec field is negative !
+ */
+void set_normalized_timespec(struct timespec *ts, time_t sec, long nsec)
+{
+       while (nsec >= NSEC_PER_SEC) {
+               nsec -= NSEC_PER_SEC;
+               ++sec;
+       }
+       while (nsec < 0) {
+               nsec += NSEC_PER_SEC;
+               --sec;
+       }
+       ts->tv_sec = sec;
+       ts->tv_nsec = nsec;
+}
+
+/**
+ * ns_to_timespec - Convert nanoseconds to timespec
+ * @nsec:       the nanoseconds value to be converted
+ *
+ * Returns the timespec representation of the nsec parameter.
+ */
+inline struct timespec ns_to_timespec(const nsec_t nsec)
+{
+       struct timespec ts;
+
+       if (nsec)
+               ts.tv_sec = div_long_long_rem_signed(nsec, NSEC_PER_SEC,
+                                                    &ts.tv_nsec);
+       else
+               ts.tv_sec = ts.tv_nsec = 0;
+
+       return ts;
+}
+
+/**
+ * ns_to_timeval - Convert nanoseconds to timeval
+ * @nsec:       the nanoseconds value to be converted
+ *
+ * Returns the timeval representation of the nsec parameter.
+ */
+struct timeval ns_to_timeval(const nsec_t nsec)
+{
+       struct timespec ts = ns_to_timespec(nsec);
+       struct timeval tv;
+
+       tv.tv_sec = ts.tv_sec;
+       tv.tv_usec = (suseconds_t) ts.tv_nsec / 1000;
+
+       return tv;
+}
+
 #if (BITS_PER_LONG < 64)
 u64 get_jiffies_64(void)
 {