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