36 #include <linux/slab.h>
37 #include <linux/types.h>
38 #include <linux/stddef.h>
41 #include <linux/export.h>
47 static u16 iclock_count;
48 static struct timeval iclock_tv;
49 static int iclock_tv_valid;
50 static struct mISDNclock *iclock_current;
62 struct mISDNclock *iclock, *bestclock =
NULL, *lastclock =
NULL;
66 if (iclock->pri > pri) {
70 if (iclock_current == iclock)
73 if (lastclock && bestclock != lastclock) {
75 if (*
debug & DEBUG_CLOCK)
78 lastclock->ctl(lastclock->priv, 0);
80 if (bestclock && bestclock != iclock_current) {
82 if (*
debug & DEBUG_CLOCK)
85 bestclock->ctl(bestclock->priv, 1);
87 if (bestclock != iclock_current) {
91 iclock_current = bestclock;
98 struct mISDNclock *iclock;
100 if (*
debug & (DEBUG_CORE | DEBUG_CLOCK))
102 iclock = kzalloc(
sizeof(
struct mISDNclock),
GFP_ATOMIC);
107 strncpy(iclock->name, name,
sizeof(iclock->name) - 1);
124 if (*
debug & (DEBUG_CORE | DEBUG_CLOCK))
128 if (iclock_current == iclock) {
129 if (*
debug & DEBUG_CLOCK)
131 "Current clock source '%s' unregisters.\n",
133 iclock->ctl(iclock->priv, 0);
150 if (iclock_current != iclock) {
152 "listen to '%s'. This is a bug!\n", __func__,
154 iclock_current ? iclock_current->name :
"nothing");
155 iclock->ctl(iclock->priv, 0);
159 if (iclock_tv_valid) {
161 iclock_count += samples;
163 iclock_tv.tv_sec = tv->
tv_sec;
164 iclock_tv.tv_usec = tv->
tv_usec;
174 elapsed_sec = tv_now.
tv_sec - iclock_tv.tv_sec;
175 elapsed_8000th = (tv_now.
tv_usec / 125)
176 - (iclock_tv.tv_usec / 125);
177 if (elapsed_8000th < 0) {
179 elapsed_8000th += 8000;
182 iclock_count += elapsed_sec * 8000 + elapsed_8000th;
183 iclock_tv.tv_sec = tv_now.
tv_sec;
184 iclock_tv.tv_usec = tv_now.
tv_usec;
186 if (*
debug & DEBUG_CLOCK)
187 printk(
"Received first clock from source '%s'.\n",
188 iclock_current ? iclock_current->name :
"nothing");
206 elapsed_sec = tv_now.
tv_sec - iclock_tv.tv_sec;
207 elapsed_8000th = (tv_now.
tv_usec / 125) - (iclock_tv.tv_usec / 125);
208 if (elapsed_8000th < 0) {
210 elapsed_8000th += 8000;
213 count = iclock_count + elapsed_sec * 8000 + elapsed_8000th;