2 * Copyright 2008 by Andreas Eversberg <andreas@eversberg.eu>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * Quick API description:
15 * A clock source registers using mISDN_register_clock:
16 * name = text string to name clock source
17 * priority = value to priorize clock sources (0 = default)
18 * ctl = callback function to enable/disable clock source
19 * priv = private pointer of clock source
20 * return = pointer to clock source structure;
22 * Note: Callback 'ctl' can be called before mISDN_register_clock returns!
23 * Also it can be called during mISDN_unregister_clock.
25 * A clock source calls mISDN_clock_update with given samples elapsed, if
26 * enabled. If function call is delayed, tv must be set with the timestamp
27 * of the actual event.
29 * A clock source unregisters using mISDN_unregister_clock.
31 * To get current clock, call mISDN_clock_get. The signed short value
32 * counts the number of samples since. Time since last clock event is added.
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>
45 static LIST_HEAD(iclock_list
);
46 static DEFINE_RWLOCK(iclock_lock
);
47 static u16 iclock_count
; /* counter of last clock */
48 static struct timeval iclock_tv
; /* time stamp of last clock */
49 static int iclock_tv_valid
; /* already received one timestamp */
50 static struct mISDNclock
*iclock_current
;
53 mISDN_init_clock(u_int
*dp
)
56 do_gettimeofday(&iclock_tv
);
62 struct mISDNclock
*iclock
, *bestclock
= NULL
, *lastclock
= NULL
;
65 list_for_each_entry(iclock
, &iclock_list
, list
) {
66 if (iclock
->pri
> pri
) {
70 if (iclock_current
== iclock
)
73 if (lastclock
&& bestclock
!= lastclock
) {
74 /* last used clock source still exists but changes, disable */
75 if (*debug
& DEBUG_CLOCK
)
76 printk(KERN_DEBUG
"Old clock source '%s' disable.\n",
78 lastclock
->ctl(lastclock
->priv
, 0);
80 if (bestclock
&& bestclock
!= iclock_current
) {
81 /* new clock source selected, enable */
82 if (*debug
& DEBUG_CLOCK
)
83 printk(KERN_DEBUG
"New clock source '%s' enable.\n",
85 bestclock
->ctl(bestclock
->priv
, 1);
87 if (bestclock
!= iclock_current
) {
88 /* no clock received yet */
91 iclock_current
= bestclock
;
95 *mISDN_register_clock(char *name
, int pri
, clockctl_func_t
*ctl
, void *priv
)
98 struct mISDNclock
*iclock
;
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
);
104 printk(KERN_ERR
"%s: No memory for clock entry.\n", __func__
);
107 strncpy(iclock
->name
, name
, sizeof(iclock
->name
)-1);
111 write_lock_irqsave(&iclock_lock
, flags
);
112 list_add_tail(&iclock
->list
, &iclock_list
);
114 write_unlock_irqrestore(&iclock_lock
, flags
);
117 EXPORT_SYMBOL(mISDN_register_clock
);
120 mISDN_unregister_clock(struct mISDNclock
*iclock
)
124 if (*debug
& (DEBUG_CORE
| DEBUG_CLOCK
))
125 printk(KERN_DEBUG
"%s: %s %d\n", __func__
, iclock
->name
,
127 write_lock_irqsave(&iclock_lock
, flags
);
128 if (iclock_current
== iclock
) {
129 if (*debug
& DEBUG_CLOCK
)
131 "Current clock source '%s' unregisters.\n",
133 iclock
->ctl(iclock
->priv
, 0);
135 list_del(&iclock
->list
);
137 write_unlock_irqrestore(&iclock_lock
, flags
);
139 EXPORT_SYMBOL(mISDN_unregister_clock
);
142 mISDN_clock_update(struct mISDNclock
*iclock
, int samples
, struct timeval
*tv
)
145 struct timeval tv_now
;
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__
,
154 iclock_current
? iclock_current
->name
: "nothing");
155 iclock
->ctl(iclock
->priv
, 0);
156 write_unlock_irqrestore(&iclock_lock
, flags
);
159 if (iclock_tv_valid
) {
160 /* increment sample counter by given samples */
161 iclock_count
+= samples
;
162 if (tv
) { /* tv must be set, if function call is delayed */
163 iclock_tv
.tv_sec
= tv
->tv_sec
;
164 iclock_tv
.tv_usec
= tv
->tv_usec
;
166 do_gettimeofday(&iclock_tv
);
168 /* calc elapsed time by system clock */
169 if (tv
) { /* tv must be set, if function call is delayed */
170 tv_now
.tv_sec
= tv
->tv_sec
;
171 tv_now
.tv_usec
= tv
->tv_usec
;
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) {
179 elapsed_8000th
+= 8000;
181 /* add elapsed time to counter and set new timestamp */
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");
190 write_unlock_irqrestore(&iclock_lock
, flags
);
192 EXPORT_SYMBOL(mISDN_clock_update
);
195 mISDN_clock_get(void)
198 struct timeval tv_now
;
203 read_lock_irqsave(&iclock_lock
, flags
);
204 /* calc elapsed time by system clock */
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) {
210 elapsed_8000th
+= 8000;
212 /* add elapsed time to counter */
213 count
= iclock_count
+ elapsed_sec
* 8000 + elapsed_8000th
;
214 read_unlock_irqrestore(&iclock_lock
, flags
);
217 EXPORT_SYMBOL(mISDN_clock_get
);