1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36#include <linux/slab.h>
37#include <linux/types.h>
38#include <linux/stddef.h>
39#include <linux/spinlock.h>
40#include <linux/mISDNif.h>
41#include <linux/export.h>
42#include "core.h"
43
44static u_int *debug;
45static LIST_HEAD(iclock_list);
46static DEFINE_RWLOCK(iclock_lock);
47static u16 iclock_count;
48static struct timeval iclock_tv;
49static int iclock_tv_valid;
50static struct mISDNclock *iclock_current;
51
52void
53mISDN_init_clock(u_int *dp)
54{
55 debug = dp;
56 do_gettimeofday(&iclock_tv);
57}
58
59static void
60select_iclock(void)
61{
62 struct mISDNclock *iclock, *bestclock = NULL, *lastclock = NULL;
63 int pri = -128;
64
65 list_for_each_entry(iclock, &iclock_list, list) {
66 if (iclock->pri > pri) {
67 pri = iclock->pri;
68 bestclock = iclock;
69 }
70 if (iclock_current == iclock)
71 lastclock = iclock;
72 }
73 if (lastclock && bestclock != lastclock) {
74
75 if (*debug & DEBUG_CLOCK)
76 printk(KERN_DEBUG "Old clock source '%s' disable.\n",
77 lastclock->name);
78 lastclock->ctl(lastclock->priv, 0);
79 }
80 if (bestclock && bestclock != iclock_current) {
81
82 if (*debug & DEBUG_CLOCK)
83 printk(KERN_DEBUG "New clock source '%s' enable.\n",
84 bestclock->name);
85 bestclock->ctl(bestclock->priv, 1);
86 }
87 if (bestclock != iclock_current) {
88
89 iclock_tv_valid = 0;
90 }
91 iclock_current = bestclock;
92}
93
94struct mISDNclock
95*mISDN_register_clock(char *name, int pri, clockctl_func_t *ctl, void *priv)
96{
97 u_long flags;
98 struct mISDNclock *iclock;
99
100 if (*debug & (DEBUG_CORE | DEBUG_CLOCK))
101 printk(KERN_DEBUG "%s: %s %d\n", __func__, name, pri);
102 iclock = kzalloc(sizeof(struct mISDNclock), GFP_ATOMIC);
103 if (!iclock) {
104 printk(KERN_ERR "%s: No memory for clock entry.\n", __func__);
105 return NULL;
106 }
107 strncpy(iclock->name, name, sizeof(iclock->name) - 1);
108 iclock->pri = pri;
109 iclock->priv = priv;
110 iclock->ctl = ctl;
111 write_lock_irqsave(&iclock_lock, flags);
112 list_add_tail(&iclock->list, &iclock_list);
113 select_iclock();
114 write_unlock_irqrestore(&iclock_lock, flags);
115 return iclock;
116}
117EXPORT_SYMBOL(mISDN_register_clock);
118
119void
120mISDN_unregister_clock(struct mISDNclock *iclock)
121{
122 u_long flags;
123
124 if (*debug & (DEBUG_CORE | DEBUG_CLOCK))
125 printk(KERN_DEBUG "%s: %s %d\n", __func__, iclock->name,
126 iclock->pri);
127 write_lock_irqsave(&iclock_lock, flags);
128 if (iclock_current == iclock) {
129 if (*debug & DEBUG_CLOCK)
130 printk(KERN_DEBUG
131 "Current clock source '%s' unregisters.\n",
132 iclock->name);
133 iclock->ctl(iclock->priv, 0);
134 }
135 list_del(&iclock->list);
136 select_iclock();
137 write_unlock_irqrestore(&iclock_lock, flags);
138}
139EXPORT_SYMBOL(mISDN_unregister_clock);
140
141void
142mISDN_clock_update(struct mISDNclock *iclock, int samples, struct timeval *tv)
143{
144 u_long flags;
145 struct timeval tv_now;
146 time_t elapsed_sec;
147 int elapsed_8000th;
148
149 write_lock_irqsave(&iclock_lock, flags);
150 if (iclock_current != iclock) {
151 printk(KERN_ERR "%s: '%s' sends us clock updates, but we do "
152 "listen to '%s'. This is a bug!\n", __func__,
153 iclock->name,
154 iclock_current ? iclock_current->name : "nothing");
155 iclock->ctl(iclock->priv, 0);
156 write_unlock_irqrestore(&iclock_lock, flags);
157 return;
158 }
159 if (iclock_tv_valid) {
160
161 iclock_count += samples;
162 if (tv) {
163 iclock_tv.tv_sec = tv->tv_sec;
164 iclock_tv.tv_usec = tv->tv_usec;
165 } else
166 do_gettimeofday(&iclock_tv);
167 } else {
168
169 if (tv) {
170 tv_now.tv_sec = tv->tv_sec;
171 tv_now.tv_usec = tv->tv_usec;
172 } else
173 do_gettimeofday(&tv_now);
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) {
178 elapsed_sec -= 1;
179 elapsed_8000th += 8000;
180 }
181
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;
185 iclock_tv_valid = 1;
186 if (*debug & DEBUG_CLOCK)
187 printk("Received first clock from source '%s'.\n",
188 iclock_current ? iclock_current->name : "nothing");
189 }
190 write_unlock_irqrestore(&iclock_lock, flags);
191}
192EXPORT_SYMBOL(mISDN_clock_update);
193
194unsigned short
195mISDN_clock_get(void)
196{
197 u_long flags;
198 struct timeval tv_now;
199 time_t elapsed_sec;
200 int elapsed_8000th;
201 u16 count;
202
203 read_lock_irqsave(&iclock_lock, flags);
204
205 do_gettimeofday(&tv_now);
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) {
209 elapsed_sec -= 1;
210 elapsed_8000th += 8000;
211 }
212
213 count = iclock_count + elapsed_sec * 8000 + elapsed_8000th;
214 read_unlock_irqrestore(&iclock_lock, flags);
215 return count;
216}
217EXPORT_SYMBOL(mISDN_clock_get);
218