Debian Codegraph @

drivers/dahdi/dahdi_dummy.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
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 #define USE_HIGHRESTIMER
43 #endif
44
45 #include <linux/kernel.h>
46 #include <linux/errno.h>
47 #include <linux/module.h>
48 #include <linux/init.h>
49 #include <linux/moduleparam.h>
50 #include <linux/slab.h>
51
52 #include <linux/hrtimer.h>
53 #if !defined(USE_HIGHRESTIMER)
54 #include <linux/timer.h>
55 #endif
56
57 #include <dahdi/kernel.h>
58
59 #ifndef HAVE_HRTIMER_ACCESSORS
60 #if defined(USE_HIGHRESTIMER) && \
61 (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 28))
62 /* Compatibility with new hrtimer interface */
63 static inline ktime_t hrtimer_get_expires(const struct hrtimer *timer)
64 {
65 return timer->expires;
66 }
67
68 static inline void hrtimer_set_expires(struct hrtimer *timer, ktime_t time)
69 {
70 timer->expires = time;
71 }
72 #endif
73 #endif
74
75 struct dahdi_dummy {
76 struct dahdi_device *ddev;
77 struct dahdi_span span;
78 struct dahdi_chan _chan;
79 struct dahdi_chan *chan;
80 #if !defined(USE_HIGHRESTIMER)
81 unsigned long calls_since_start;
82 ktime_t start_interval;
83 #endif
84 };
85
86 static struct dahdi_dummy *ztd;
87
88 static int debug = 0;
89
90 #ifdef USE_HIGHRESTIMER
91 #define CLOCK_SRC "HRtimer"
92 static struct hrtimer zaptimer;
93 #define DAHDI_RATE 1000 /* DAHDI ticks per second */
94 #define DAHDI_TIME (1000000 / DAHDI_RATE) /* DAHDI tick time in us */
95 #define DAHDI_TIME_NS (DAHDI_TIME * 1000) /* DAHDI tick time in ns */
96 #else
97 #define CLOCK_SRC "Linux26"
98 static struct timer_list timer;
99 static atomic_t shutdown;
100 #define JIFFIES_INTERVAL max(HZ/250, 1) /* 4ms is fine for dahdi_dummy */
101 #endif
102
103 /* Different bits of the debug variable: */
104 #define DEBUG_GENERAL (1 << 0)
105 #define DEBUG_TICKS (1 << 1)
106
107 #if defined(USE_HIGHRESTIMER)
108 static enum hrtimer_restart dahdi_dummy_hr_int(struct hrtimer *htmr)
109 {
110 unsigned long overrun;
111
112 /* Trigger DAHDI */
113 dahdi_receive(&ztd->span);
114 dahdi_transmit(&ztd->span);
115
116 /* Overrun should always return 1, since we are in the timer that
117 * expired.
118 * We should worry if overrun is 2 or more; then we really missed
119 * a tick */
120 overrun = hrtimer_forward(&zaptimer, hrtimer_get_expires(htmr),
121 ktime_set(0, DAHDI_TIME_NS));
122 if(overrun > 1) {
123 if(printk_ratelimit())
124 printk(KERN_NOTICE "dahdi_dummy: HRTimer missed %lu ticks\n",
125 overrun - 1);
126 }
127
128 if(debug && DEBUG_TICKS) {
129 static int count = 0;
130 /* Printk every 5 seconds, good test to see if timer is
131 * running properly */
132 if (count++ % 5000 == 0)
133 printk(KERN_DEBUG "dahdi_dummy: 5000 ticks from hrtimer\n");
134 }
135
136 /* Always restart the timer */
137 return HRTIMER_RESTART;
138 }
139 #else
140
141 static void dahdi_dummy_timer(TIMER_DATA_TYPE unused)
142 {
143 long ms_since_start;
144 ktime_t now;
145 const long MAX_INTERVAL = 100000L;
146 const long MS_LIMIT = 3000;
147
148 if (!atomic_read(&shutdown))
149 mod_timer(&timer, jiffies + JIFFIES_INTERVAL);
150
151 now = ktime_get();
152 ms_since_start = ktime_ms_delta(now, ztd->start_interval);
153
154 /*
155 * If the system time has changed, it is possible for us to be far
156 * behind. If we are more than MS_LIMIT milliseconds behind, just
157 * reset our time base and continue so that we do not hang the system
158 * here.
159 *
160 */
161 if (unlikely((ms_since_start - ztd->calls_since_start) > MS_LIMIT)) {
162 if (printk_ratelimit()) {
163 printk(KERN_INFO
164 "dahdi_dummy: Detected time shift.\n");
165 }
166 ztd->calls_since_start = 0;
167 ztd->start_interval = now;
168 return;
169 }
170
171 while (ms_since_start > ztd->calls_since_start) {
172 ztd->calls_since_start++;
173 dahdi_receive(&ztd->span);
174 dahdi_transmit(&ztd->span);
175 }
176
177 if (ms_since_start > MAX_INTERVAL) {
178 ztd->calls_since_start = 0;
179 ztd->start_interval = now;
180 }
181 }
182 #endif
183
184 static const struct dahdi_span_ops dummy_ops = {
185 .owner = THIS_MODULE,
186 };
187
188 static int dahdi_dummy_initialize(struct dahdi_dummy *ztd)
189 {
190 int res = 0;
191 /* DAHDI stuff */
192 ztd->ddev = dahdi_create_device();
193 if (!ztd->ddev)
194 return -ENOMEM;
195 dev_set_name(&ztd->ddev->dev, "dahdi_dummy");
196 ztd->chan = &ztd->_chan;
197 sprintf(ztd->span.name, "DAHDI_DUMMY/1");
198 snprintf(ztd->span.desc, sizeof(ztd->span.desc) - 1, "%s (source: " CLOCK_SRC ") %d", ztd->span.name, 1);
199 sprintf(ztd->chan->name, "DAHDI_DUMMY/%d/%d", 1, 0);
200 ztd->ddev->devicetype = "DAHDI Dummy Timing";
201 ztd->chan->chanpos = 1;
202 ztd->span.chans = &ztd->chan;
203 ztd->span.channels = 0; /* no channels on our span */
204 ztd->span.deflaw = DAHDI_LAW_MULAW;
205 ztd->chan->pvt = ztd;
206 ztd->span.ops = &dummy_ops;
207 list_add_tail(&ztd->span.device_node, &ztd->ddev->spans);
208 res = dahdi_register_device(ztd->ddev, NULL);
209 return res;
210 }
211
212 int init_module(void)
213 {
214 int res;
215 ztd = kzalloc(sizeof(*ztd), GFP_KERNEL);
216 if (ztd == NULL) {
217 printk(KERN_ERR "dahdi_dummy: Unable to allocate memory\n");
218 return -ENOMEM;
219 }
220
221 res = dahdi_dummy_initialize(ztd);
222 if (res) {
223 printk(KERN_ERR
224 "dahdi_dummy: Unable to intialize DAHDI driver (%d)\n",
225 res);
226 kfree(ztd);
227 return res;
228 }
229
230 #if defined(USE_HIGHRESTIMER)
231 printk(KERN_DEBUG "dahdi_dummy: Trying to load High Resolution Timer\n");
232 hrtimer_init(&zaptimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
233 printk(KERN_DEBUG "dahdi_dummy: Initialized High Resolution Timer\n");
234
235 /* Set timer callback function */
236 zaptimer.function = dahdi_dummy_hr_int;
237
238 printk(KERN_DEBUG "dahdi_dummy: Starting High Resolution Timer\n");
239 hrtimer_start(&zaptimer, ktime_set(0, DAHDI_TIME_NS), HRTIMER_MODE_REL);
240 printk(KERN_INFO "dahdi_dummy: High Resolution Timer started, good to go\n");
241 #else
242 timer_setup(&timer, dahdi_dummy_timer, 0);
243 ztd->start_interval = ktime_get();
244 atomic_set(&shutdown, 0);
245 mod_timer(&timer, jiffies + JIFFIES_INTERVAL);
246 #endif
247
248 if (debug)
249 printk(KERN_DEBUG "dahdi_dummy: init() finished\n");
250 return 0;
251 }
252
253
254 void cleanup_module(void)
255 {
256 #if defined(USE_HIGHRESTIMER)
257 /* Stop high resolution timer */
258 hrtimer_cancel(&zaptimer);
259 #else
260 atomic_set(&shutdown, 1);
•261 timer_delete_sync(&timer);
262 #endif
263 dahdi_unregister_device(ztd->ddev);
264 dahdi_free_device(ztd->ddev);
265 kfree(ztd);
266 if (debug)
267 printk(KERN_DEBUG "dahdi_dummy: cleanup() finished\n");
268 }
269
270 module_param(debug, int, 0600);
271
272 MODULE_DESCRIPTION("Timing-Only Driver");
273 MODULE_AUTHOR("Robert Pleh <[email protected]>");
274 MODULE_LICENSE("GPL v2");