1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2008 by Andreas Eversberg <andreas@eversberg.eu>
5 * Quick API description:
7 * A clock source registers using mISDN_register_clock:
8 * name = text string to name clock source
9 * priority = value to priorize clock sources (0 = default)
10 * ctl = callback function to enable/disable clock source
11 * priv = private pointer of clock source
12 * return = pointer to clock source structure;
14 * Note: Callback 'ctl' can be called before mISDN_register_clock returns!
15 * Also it can be called during mISDN_unregister_clock.
17 * A clock source calls mISDN_clock_update with given samples elapsed, if
18 * enabled. If function call is delayed, tv must be set with the timestamp
19 * of the actual event.
21 * A clock source unregisters using mISDN_unregister_clock.
23 * To get current clock, call mISDN_clock_get. The signed short value
24 * counts the number of samples since. Time since last clock event is added.
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>
37 static LIST_HEAD(iclock_list
);
38 static DEFINE_RWLOCK(iclock_lock
);
39 static u16 iclock_count
; /* counter of last clock */
40 static ktime_t iclock_timestamp
; /* time stamp of last clock */
41 static int iclock_timestamp_valid
; /* already received one timestamp */
42 static struct mISDNclock
*iclock_current
;
45 mISDN_init_clock(u_int
*dp
)
48 iclock_timestamp
= ktime_get();
54 struct mISDNclock
*iclock
, *bestclock
= NULL
, *lastclock
= NULL
;
57 list_for_each_entry(iclock
, &iclock_list
, list
) {
58 if (iclock
->pri
> pri
) {
62 if (iclock_current
== iclock
)
65 if (lastclock
&& bestclock
!= lastclock
) {
66 /* last used clock source still exists but changes, disable */
67 if (*debug
& DEBUG_CLOCK
)
68 printk(KERN_DEBUG
"Old clock source '%s' disable.\n",
70 lastclock
->ctl(lastclock
->priv
, 0);
72 if (bestclock
&& bestclock
!= iclock_current
) {
73 /* new clock source selected, enable */
74 if (*debug
& DEBUG_CLOCK
)
75 printk(KERN_DEBUG
"New clock source '%s' enable.\n",
77 bestclock
->ctl(bestclock
->priv
, 1);
79 if (bestclock
!= iclock_current
) {
80 /* no clock received yet */
81 iclock_timestamp_valid
= 0;
83 iclock_current
= bestclock
;
87 *mISDN_register_clock(char *name
, int pri
, clockctl_func_t
*ctl
, void *priv
)
90 struct mISDNclock
*iclock
;
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
);
96 printk(KERN_ERR
"%s: No memory for clock entry.\n", __func__
);
99 strscpy(iclock
->name
, name
, sizeof(iclock
->name
));
103 write_lock_irqsave(&iclock_lock
, flags
);
104 list_add_tail(&iclock
->list
, &iclock_list
);
106 write_unlock_irqrestore(&iclock_lock
, flags
);
109 EXPORT_SYMBOL(mISDN_register_clock
);
112 mISDN_unregister_clock(struct mISDNclock
*iclock
)
116 if (*debug
& (DEBUG_CORE
| DEBUG_CLOCK
))
117 printk(KERN_DEBUG
"%s: %s %d\n", __func__
, iclock
->name
,
119 write_lock_irqsave(&iclock_lock
, flags
);
120 if (iclock_current
== iclock
) {
121 if (*debug
& DEBUG_CLOCK
)
123 "Current clock source '%s' unregisters.\n",
125 iclock
->ctl(iclock
->priv
, 0);
127 list_del(&iclock
->list
);
129 write_unlock_irqrestore(&iclock_lock
, flags
);
131 EXPORT_SYMBOL(mISDN_unregister_clock
);
134 mISDN_clock_update(struct mISDNclock
*iclock
, int samples
, ktime_t
*timestamp
)
137 ktime_t timestamp_now
;
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__
,
145 iclock_current
? iclock_current
->name
: "nothing");
146 iclock
->ctl(iclock
->priv
, 0);
147 write_unlock_irqrestore(&iclock_lock
, flags
);
150 if (iclock_timestamp_valid
) {
151 /* increment sample counter by given samples */
152 iclock_count
+= samples
;
153 if (timestamp
) { /* timestamp must be set, if function call is delayed */
154 iclock_timestamp
= *timestamp
;
156 iclock_timestamp
= ktime_get();
159 /* calc elapsed time by system clock */
160 if (timestamp
) { /* timestamp must be set, if function call is delayed */
161 timestamp_now
= *timestamp
;
163 timestamp_now
= ktime_get();
165 delta
= ktime_divns(ktime_sub(timestamp_now
, iclock_timestamp
),
166 (NSEC_PER_SEC
/ 8000));
167 /* add elapsed time to counter and set new timestamp */
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");
175 write_unlock_irqrestore(&iclock_lock
, flags
);
177 EXPORT_SYMBOL(mISDN_clock_update
);
180 mISDN_clock_get(void)
183 ktime_t timestamp_now
;
187 read_lock_irqsave(&iclock_lock
, flags
);
188 /* calc elapsed time by system clock */
189 timestamp_now
= ktime_get();
190 delta
= ktime_divns(ktime_sub(timestamp_now
, iclock_timestamp
),
191 (NSEC_PER_SEC
/ 8000));
192 /* add elapsed time to counter */
193 count
= iclock_count
+ delta
;
194 read_unlock_irqrestore(&iclock_lock
, flags
);
197 EXPORT_SYMBOL(mISDN_clock_get
);