Debian Codegraph @

drivers/dahdi/dahdi_dummy.c

raw
1 /*
2 * Dummy DAHDI Driver for DAHDI Telephony interface
3 *
4 * Required: kernel > 2.6.0
5 *
6 * Written by Robert Pleh <[email protected]>
7 * 2.6 version by Tony Hoyle
8 * Unified by Mark Spencer <[email protected]>
9 *
10 * Converted to use HighResTimers on i386 by Jeffery Palmer <[email protected]>
11 *
12 * Copyright (C) 2002, Hermes Softlab
13 * Copyright (C) 2004-2012, Digium, Inc.
14 *
15 * All rights reserved.
16 *
17 */
18
19 /*
20 * See http://www.asterisk.org for more information about
21 * the Asterisk project. Please do not directly contact
22 * any of the maintainers of this project for assistance;
23 * the project provides a web site, mailing lists and IRC
24 * channels for your use.
25 *
26 * This program is free software, distributed under the terms of
27 * the GNU General Public License Version 2 as published by the
28 * Free Software Foundation. See the LICENSE file included with
29 * this program for more details.
30 */
31
32 /*
33 * To use the high resolution timers, in your kernel CONFIG_HIGH_RES_TIMERS
34 * needs to be enabled (Processor type and features -> High Resolution
35 * Timer Support), and optionally HPET (Processor type and features ->
36 * HPET Timer Support) provides a better clock source.
37 */
38
39 #include <linux/version.h>
40
41 #if defined(CONFIG_HIGH_RES_TIMERS) && \
42 LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 22)
43 #define USE_HIGHRESTIMER
44 #endif
45
46 #include <linux/kernel.h>
47 #include <linux/errno.h>
48 #include <linux/module.h>
49 #include <linux/init.h>
50 #include <linux/moduleparam.h>
51 #include <linux/slab.h>
52
53 #if defined(USE_HIGHRESTIMER)
54 #include <linux/hrtimer.h>
55 #else
56 #include <linux/timer.h>
57 #endif
58
59 #include <dahdi/kernel.h>
60
61 #ifndef HAVE_HRTIMER_ACCESSORS
62 #if defined(USE_HIGHRESTIMER) && \
63 (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 28))
64 /* Compatibility with new hrtimer interface */
65 static inline ktime_t hrtimer_get_expires(const struct hrtimer *timer)
66 {
67 return timer->expires;
68 }
69
70 static inline void hrtimer_set_expires(struct hrtimer *timer, ktime_t time)
71 {
72 timer->expires = time;
73 }
74 #endif
75 #endif
76
77 struct dahdi_dummy {
78 struct dahdi_device *ddev;
79 struct dahdi_span span;
80 struct dahdi_chan _chan;
81 struct dahdi_chan *chan;
82 #if !defined(USE_HIGHRESTIMER)
83 unsigned long calls_since_start;
84 struct timespec start_interval;
85 #endif
86 };
87
88 static struct dahdi_dummy *ztd;
89
90 static int debug = 0;
91
92 #ifdef USE_HIGHRESTIMER
93 #define CLOCK_SRC "HRtimer"
94 static struct hrtimer zaptimer;
95 #define DAHDI_RATE 1000 /* DAHDI ticks per second */
96 #define DAHDI_TIME (1000000 / DAHDI_RATE) /* DAHDI tick time in us */
97 #define DAHDI_TIME_NS (DAHDI_TIME * 1000) /* DAHDI tick time in ns */
98 #else
99 #define CLOCK_SRC "Linux26"
100 static struct timer_list timer;
101 static atomic_t shutdown;
102 #define JIFFIES_INTERVAL max(HZ/250, 1) /* 4ms is fine for dahdi_dummy */
103 #endif
104
105 /* Different bits of the debug variable: */
106 #define DEBUG_GENERAL (1 << 0)
107 #define DEBUG_TICKS (1 << 1)
108
109 #if defined(USE_HIGHRESTIMER)
110 static enum hrtimer_restart dahdi_dummy_hr_int(struct hrtimer *htmr)
111 {
112 unsigned long overrun;
113
114 /* Trigger DAHDI */
115 dahdi_receive(&ztd->span);
116 dahdi_transmit(&ztd->span);
117
118 /* Overrun should always return 1, since we are in the timer that
119 * expired.
120 * We should worry if overrun is 2 or more; then we really missed
121 * a tick */
122 overrun = hrtimer_forward(&zaptimer, hrtimer_get_expires(htmr),
123 ktime_set(0, DAHDI_TIME_NS));
124 if(overrun > 1) {
125 if(printk_ratelimit())
126 printk(KERN_NOTICE "dahdi_dummy: HRTimer missed %lu ticks\n",
127 overrun - 1);
128 }
129
130 if(debug && DEBUG_TICKS) {
131 static int count = 0;
132 /* Printk every 5 seconds, good test to see if timer is
133 * running properly */
134 if (count++ % 5000 == 0)
135 printk(KERN_DEBUG "dahdi_dummy: 5000 ticks from hrtimer\n");
136 }
137
138 /* Always restart the timer */
139 return HRTIMER_RESTART;
140 }
141 #else
142 static unsigned long timespec_diff_ms(struct timespec *t0, struct timespec *t1)
143 {
144 long nanosec, sec;
145 unsigned long ms;
146 sec = (t1->tv_sec - t0->tv_sec);
147 nanosec = (t1->tv_nsec - t0->tv_nsec);
148 while (nanosec >= NSEC_PER_SEC) {
149 nanosec -= NSEC_PER_SEC;
150 ++sec;
151 }
152 while (nanosec < 0) {
153 nanosec += NSEC_PER_SEC;
154 --sec;
155 }
156 ms = (sec * 1000) + (nanosec / 1000000L);
157 return ms;
158 }
159
160 static void dahdi_dummy_timer(unsigned long param)
161 {
162 unsigned long ms_since_start;
163 struct timespec now;
164 const unsigned long MAX_INTERVAL = 100000L;
165 const unsigned long MS_LIMIT = 3000;
166
167 if (!atomic_read(&shutdown))
168 mod_timer(&timer, jiffies + JIFFIES_INTERVAL);
169
170 now = current_kernel_time();
171 ms_since_start = timespec_diff_ms(&ztd->start_interval, &now);
172
173 /*
174 * If the system time has changed, it is possible for us to be far
175 * behind. If we are more than MS_LIMIT milliseconds behind, just
176 * reset our time base and continue so that we do not hang the system
177 * here.
178 *
179 */
180 if (unlikely((ms_since_start - ztd->calls_since_start) > MS_LIMIT)) {
181 if (printk_ratelimit()) {
182 printk(KERN_INFO
183 "dahdi_dummy: Detected time shift.\n");
184 }
185 ztd->calls_since_start = 0;
186 ztd->start_interval = now;
187 return;
188 }
189
190 while (ms_since_start > ztd->calls_since_start) {
191 ztd->calls_since_start++;
192 dahdi_receive(&ztd->span);
193 dahdi_transmit(&ztd->span);
194 }
195
196 if (ms_since_start > MAX_INTERVAL) {
197 ztd->calls_since_start = 0;
198 ztd->start_interval = now;
199 }
200 }
201 #endif
202
203 static const struct dahdi_span_ops dummy_ops = {
204 .owner = THIS_MODULE,
205 };
206
207 static int dahdi_dummy_initialize(struct dahdi_dummy *ztd)
208 {
209 int res = 0;
210 /* DAHDI stuff */
211 ztd->ddev = dahdi_create_device();
212 if (!ztd->ddev)
213 return -ENOMEM;
214 dev_set_name(&ztd->ddev->dev, "dahdi_dummy");
215 ztd->chan = &ztd->_chan;
216 sprintf(ztd->span.name, "DAHDI_DUMMY/1");
217 snprintf(ztd->span.desc, sizeof(ztd->span.desc) - 1, "%s (source: " CLOCK_SRC ") %d", ztd->span.name, 1);
218 sprintf(ztd->chan->name, "DAHDI_DUMMY/%d/%d", 1, 0);
219 ztd->ddev->devicetype = "DAHDI Dummy Timing";
220 ztd->chan->chanpos = 1;
221 ztd->span.chans = &ztd->chan;
222 ztd->span.channels = 0; /* no channels on our span */
223 ztd->span.deflaw = DAHDI_LAW_MULAW;
224 ztd->chan->pvt = ztd;
225 ztd->span.ops = &dummy_ops;
226 list_add_tail(&ztd->span.device_node, &ztd->ddev->spans);
227 res = dahdi_register_device(ztd->ddev, NULL);
228 return res;
229 }
230
231 int init_module(void)
232 {
233 int res;
234 ztd = kzalloc(sizeof(*ztd), GFP_KERNEL);
235 if (ztd == NULL) {
236 printk(KERN_ERR "dahdi_dummy: Unable to allocate memory\n");
237 return -ENOMEM;
238 }
239
240 res = dahdi_dummy_initialize(ztd);
241 if (res) {
242 printk(KERN_ERR
243 "dahdi_dummy: Unable to intialize DAHDI driver (%d)\n",
244 res);
245 kfree(ztd);
246 return res;
247 }
248
249 #if defined(USE_HIGHRESTIMER)
250 printk(KERN_DEBUG "dahdi_dummy: Trying to load High Resolution Timer\n");
251 hrtimer_init(&zaptimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
252 printk(KERN_DEBUG "dahdi_dummy: Initialized High Resolution Timer\n");
253
254 /* Set timer callback function */
255 zaptimer.function = dahdi_dummy_hr_int;
256
257 printk(KERN_DEBUG "dahdi_dummy: Starting High Resolution Timer\n");
258 hrtimer_start(&zaptimer, ktime_set(0, DAHDI_TIME_NS), HRTIMER_MODE_REL);
259 printk(KERN_INFO "dahdi_dummy: High Resolution Timer started, good to go\n");
260 #else
261 init_timer(&timer);
262 timer.function = dahdi_dummy_timer;
263 ztd->start_interval = current_kernel_time();
264 timer.expires = jiffies + JIFFIES_INTERVAL;
265 atomic_set(&shutdown, 0);
266 add_timer(&timer);
267 #endif
268
269 if (debug)
270 printk(KERN_DEBUG "dahdi_dummy: init() finished\n");
271 return 0;
272 }
273
274
275 void cleanup_module(void)
276 {
277 #if defined(USE_HIGHRESTIMER)
278 /* Stop high resolution timer */
279 hrtimer_cancel(&zaptimer);
280 #else
281 atomic_set(&shutdown, 1);
282 del_timer_sync(&timer);
283 #endif
284 dahdi_unregister_device(ztd->ddev);
285 dahdi_free_device(ztd->ddev);
286 kfree(ztd);
287 if (debug)
288 printk(KERN_DEBUG "dahdi_dummy: cleanup() finished\n");
289 }
290
291 module_param(debug, int, 0600);
292
293 MODULE_DESCRIPTION("Timing-Only Driver");
294 MODULE_AUTHOR("Robert Pleh <[email protected]>");
295 MODULE_LICENSE("GPL v2");