Debian Codegraph @

drivers/dahdi/dahdi-base.c

raw
1 /*
2 * DAHDI Telephony Interface Driver
3 *
4 * Written by Mark Spencer <[email protected]>
5 * Based on previous works, designs, and architectures conceived and
6 * written by Jim Dixon <[email protected]>.
7 *
8 * Special thanks to Steve Underwood <[email protected]>
9 * for substantial contributions to signal processing functions
10 * in DAHDI and the Zapata library.
11 *
12 * Yury Bokhoncovich <[email protected]>
13 * Adaptation for 2.4.20+ kernels (HDLC API was changed)
14 * The work has been performed as a part of our move
15 * from Cisco 3620 to IBM x305 here in F1 Group
16 *
17 * Copyright (C) 2001 Jim Dixon / Zapata Telephony.
18 * Copyright (C) 2001 - 2012 Digium, Inc.
19 *
20 * All rights reserved.
21 *
22 */
23
24 /*
25 * See http://www.asterisk.org for more information about
26 * the Asterisk project. Please do not directly contact
27 * any of the maintainers of this project for assistance;
28 * the project provides a web site, mailing lists and IRC
29 * channels for your use.
30 *
31 * This program is free software, distributed under the terms of
32 * the GNU General Public License Version 2 as published by the
33 * Free Software Foundation. See the LICENSE file included with
34 * this program for more details.
35 */
36
37
38 #include <linux/kernel.h>
39 #include <linux/errno.h>
40 #include <linux/module.h>
41 #include <linux/proc_fs.h>
42 #include <linux/seq_file.h>
43 #include <linux/pci.h>
44 #include <linux/init.h>
45 #include <linux/version.h>
46 #include <linux/ctype.h>
47 #include <linux/kmod.h>
48 #include <linux/moduleparam.h>
49 #include <linux/sched.h>
50 #include <linux/list.h>
51 #include <linux/delay.h>
52 #include <linux/mutex.h>
53 #include <linux/ktime.h>
54 #include <linux/slab.h>
55
56 #if defined(HAVE_UNLOCKED_IOCTL) && defined(CONFIG_BKL)
57 #include <linux/smp_lock.h>
58 #endif
59
60 #include <linux/ppp_defs.h>
61
62 #include <asm/atomic.h>
63
64 #define DAHDI_PRINK_MACROS_USE_debug
65
66 /* Grab fasthdlc with tables */
67 #define FAST_HDLC_NEED_TABLES
68 #include <dahdi/kernel.h>
69
70 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
71 #include <linux/sched/signal.h>
72 #endif /* 4.11.0 */
73
74 #include "ecdis.h"
75 #include "dahdi.h"
76
77 #ifdef CONFIG_DAHDI_PPP
78 #include <linux/netdevice.h>
79 #include <linux/if.h>
80 #include <linux/if_ppp.h>
81 #endif
82
83 #ifdef CONFIG_DAHDI_NET
84 #include <linux/netdevice.h>
85 #endif
86
87 #include "hpec/hpec_user.h"
88
89 #include <stdbool.h>
90
91 #if defined(EMPULSE) && defined(EMFLASH)
92 #error "You cannot define both EMPULSE and EMFLASH"
93 #endif
94
95 /* Get helper arithmetic */
96 #include "arith.h"
97 #if defined(CONFIG_DAHDI_MMX) || defined(ECHO_CAN_FP)
98 #include <asm/i387.h>
99 #endif
100
101 #define hdlc_to_chan(h) (((struct dahdi_hdlc *)(h))->chan)
102 #define netdev_to_chan(h) (((struct dahdi_hdlc *)(dev_to_hdlc(h)->priv))->chan)
103 #define chan_to_netdev(h) ((h)->hdlcnetdev->netdev)
104
105 /* macro-oni for determining a unit (channel) number */
106 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
107 #define UNIT(file) MINOR(file->f_dentry->d_inode->i_rdev)
108 #else
109 #define UNIT(file) MINOR(file->f_path.dentry->d_inode->i_rdev)
110 #endif
111
112 EXPORT_SYMBOL(dahdi_transcode_fops);
113 EXPORT_SYMBOL(dahdi_init_tone_state);
114 EXPORT_SYMBOL(dahdi_mf_tone);
115 EXPORT_SYMBOL(__dahdi_mulaw);
116 EXPORT_SYMBOL(__dahdi_alaw);
117 #ifdef CONFIG_CALC_XLAW
118 EXPORT_SYMBOL(__dahdi_lineartoulaw);
119 EXPORT_SYMBOL(__dahdi_lineartoalaw);
120 #else
121 EXPORT_SYMBOL(__dahdi_lin2mu);
122 EXPORT_SYMBOL(__dahdi_lin2a);
123 #endif
124 EXPORT_SYMBOL(dahdi_rbsbits);
125 EXPORT_SYMBOL(dahdi_qevent_nolock);
126 EXPORT_SYMBOL(dahdi_qevent_lock);
127 EXPORT_SYMBOL(dahdi_hooksig);
128 EXPORT_SYMBOL(dahdi_alarm_notify);
129 EXPORT_SYMBOL(dahdi_hdlc_abort);
130 EXPORT_SYMBOL(dahdi_hdlc_finish);
131 EXPORT_SYMBOL(dahdi_hdlc_getbuf);
132 EXPORT_SYMBOL(dahdi_hdlc_putbuf);
133 EXPORT_SYMBOL(dahdi_alarm_channel);
134
135 EXPORT_SYMBOL(dahdi_register_echocan_factory);
136 EXPORT_SYMBOL(dahdi_unregister_echocan_factory);
137
138 EXPORT_SYMBOL(dahdi_set_hpec_ioctl);
139
140 #ifdef CONFIG_PROC_FS
141 static struct proc_dir_entry *root_proc_entry;
142 #endif
143
144 static int deftaps = 64;
145
146 int debug;
147 #define DEBUG_MAIN (1 << 0)
148 #define DEBUG_RBS (1 << 5)
149
150 static int hwec_overrides_swec = 1;
151
152 /*!
153 * \brief states for transmit signalling
154 */
155 enum dahdi_txstate {
156 DAHDI_TXSTATE_ONHOOK,
157 DAHDI_TXSTATE_OFFHOOK,
158 DAHDI_TXSTATE_START,
159 DAHDI_TXSTATE_PREWINK,
160 DAHDI_TXSTATE_WINK,
161 DAHDI_TXSTATE_PREFLASH,
162 DAHDI_TXSTATE_FLASH,
163 DAHDI_TXSTATE_DEBOUNCE,
164 DAHDI_TXSTATE_AFTERSTART,
165 DAHDI_TXSTATE_RINGON,
166 DAHDI_TXSTATE_RINGOFF,
167 DAHDI_TXSTATE_KEWL,
168 DAHDI_TXSTATE_AFTERKEWL,
169 DAHDI_TXSTATE_PULSEBREAK,
170 DAHDI_TXSTATE_PULSEMAKE,
171 DAHDI_TXSTATE_PULSEAFTER,
172 };
173
174 typedef short sumtype[DAHDI_MAX_CHUNKSIZE];
175
176 static sumtype sums[(DAHDI_MAX_CONF + 1) * 3];
177
178 /* Translate conference aliases into actual conferences
179 and vice-versa */
180 static short confalias[DAHDI_MAX_CONF + 1];
181 static short confrev[DAHDI_MAX_CONF + 1];
182
183 static sumtype *conf_sums_next;
184 static sumtype *conf_sums;
185 static sumtype *conf_sums_prev;
186
187 static struct dahdi_span *master_span;
188 struct file_operations *dahdi_transcode_fops = NULL;
189
190
191 #ifdef CONFIG_DAHDI_CONFLINK
192 static struct {
193 int src; /* source conf number */
194 int dst; /* dst conf number */
195 } conf_links[DAHDI_MAX_CONF + 1];
196
197 static int maxlinks;
198
199 #endif
200
201 #ifdef CONFIG_DAHDI_CORE_TIMER
202
203 static struct core_timer {
204 struct timer_list timer;
205 struct timespec start_interval;
206 unsigned long interval;
207 int dahdi_receive_used;
208 atomic_t count;
209 atomic_t shutdown;
210 atomic_t last_count;
211 } core_timer;
212
213 #endif /* CONFIG_DAHDI_CORE_TIMER */
214
215
216 enum dahdi_digit_mode {
217 DIGIT_MODE_DTMF,
218 DIGIT_MODE_MFR1,
219 DIGIT_MODE_PULSE,
220 DIGIT_MODE_MFR2_FWD,
221 DIGIT_MODE_MFR2_REV,
222 };
223
224 /* At the end of silence, the tone stops */
225 static struct dahdi_tone dtmf_silence = {
226 .tonesamples = DAHDI_MS_TO_SAMPLES(DAHDI_CONFIG_DEFAULT_DTMF_LENGTH),
227 };
228
229 /* At the end of silence, the tone stops */
230 static struct dahdi_tone mfr1_silence = {
231 .tonesamples = DAHDI_MS_TO_SAMPLES(DAHDI_CONFIG_DEFAULT_MFR1_LENGTH),
232 };
233
234 /* At the end of silence, the tone stops */
235 static struct dahdi_tone mfr2_silence = {
236 .tonesamples = DAHDI_MS_TO_SAMPLES(DAHDI_CONFIG_DEFAULT_MFR2_LENGTH),
237 };
238
239 /* A pause in the dialing */
240 static struct dahdi_tone tone_pause = {
241 .tonesamples = DAHDI_MS_TO_SAMPLES(DAHDI_CONFIG_PAUSE_LENGTH),
242 };
243
244 static struct dahdi_dialparams global_dialparams = {
245 .dtmf_tonelen = DAHDI_MS_TO_SAMPLES(DAHDI_CONFIG_DEFAULT_DTMF_LENGTH),
246 .mfv1_tonelen = DAHDI_MS_TO_SAMPLES(DAHDI_CONFIG_DEFAULT_MFR1_LENGTH),
247 .mfr2_tonelen = DAHDI_MS_TO_SAMPLES(DAHDI_CONFIG_DEFAULT_MFR2_LENGTH),
248 };
249
250 static DEFINE_MUTEX(global_dialparamslock);
251
252 static int dahdi_chan_ioctl(struct file *file, unsigned int cmd, unsigned long data);
253
254 #if defined(CONFIG_DAHDI_MMX) || defined(ECHO_CAN_FP)
255 #if (defined(CONFIG_X86) && !defined(CONFIG_X86_64)) || defined(CONFIG_I386)
256 struct fpu_save_buf {
257 unsigned long cr0;
258 unsigned long fpu_buf[128];
259 };
260
261 static DEFINE_PER_CPU(struct fpu_save_buf, fpu_buf);
262
263 /** dahdi_kernel_fpu_begin() - Save floating point registers
264 *
265 * This function is similar to kernel_fpu_begin() . However it is
266 * designed to work in an interrupt context. Restoring must be done with
267 * dahdi_kernel_fpu_end().
268 *
269 * Furthermore, the whole code between the call to
270 * dahdi_kernel_fpu_begin() and dahdi_kernel_fpu_end() must reside
271 * inside a spinlock. Otherwise the context might be restored to the
272 * wrong process.
273 *
274 * Current implementation is x86/ia32-specific and will not even build on
275 * x86_64)
276 * */
277 static inline void dahdi_kernel_fpu_begin(void)
278 {
279 struct fpu_save_buf *buf = &__get_cpu_var(fpu_buf);
280 __asm__ __volatile__ ("movl %%cr0,%0; clts" : "=r" (buf->cr0));
281 __asm__ __volatile__ ("fnsave %0" : "=m" (buf->fpu_buf));
282 }
283
284 /** dahdi_kernel_fpu_end() - restore floating point context
285 *
286 * Must be used with context saved by dahdi_kernel_fpu_begin(). See its
287 * documentation for further information.
288 */
289 static inline void dahdi_kernel_fpu_end(void)
290 {
291 struct fpu_save_buf *buf = &__get_cpu_var(fpu_buf);
292 __asm__ __volatile__ ("frstor %0" : "=m" (buf->fpu_buf));
293 __asm__ __volatile__ ("movl %0,%%cr0" : : "r" (buf->cr0));
294 }
295
296 #else /* We haven't fixed FP context saving/restoring yet */
297 /* Very strange things can happen when the context is not properly
298 * restored. OTOH, some people do report success with this. Hence we
299 * so far just issue a warning */
300 #warning CONFIG_DAHDI_MMX may behave randomly on this platform
301 #define dahdi_kernel_fpu_begin kernel_fpu_begin
302 #define dahdi_kernel_fpu_end kernel_fpu_end
303 #endif
304
305 #endif
306
307 struct dahdi_timer {
308 spinlock_t lock;
309 int ms; /* Countdown */
310 int pos; /* Position */
311 int ping; /* Whether we've been ping'd */
312 int tripped; /* Whether we're tripped */
313 struct list_head list;
314 wait_queue_head_t sel;
315 };
316
317 static LIST_HEAD(dahdi_timers);
318
319 static DEFINE_SPINLOCK(dahdi_timer_lock);
320
321 #define DEFAULT_TONE_ZONE (-1)
322
323 struct dahdi_zone {
324 int ringcadence[DAHDI_MAX_CADENCE];
325 struct dahdi_tone *tones[DAHDI_TONE_MAX];
326 /* Each of these is a circular list
327 of dahdi_tones to generate what we
328 want. Use NULL if the tone is
329 unavailable */
330 struct dahdi_tone dtmf[16]; /* DTMF tones for this zone, with desired length */
331 struct dahdi_tone dtmf_continuous[16]; /* DTMF tones for this zone, continuous play */
332 struct dahdi_tone mfr1[15]; /* MFR1 tones for this zone, with desired length */
333 struct dahdi_tone mfr2_fwd[15]; /* MFR2 FWD tones for this zone, with desired length */
334 struct dahdi_tone mfr2_rev[15]; /* MFR2 REV tones for this zone, with desired length */
335 struct dahdi_tone mfr2_fwd_continuous[16]; /* MFR2 FWD tones for this zone, continuous play */
336 struct dahdi_tone mfr2_rev_continuous[16]; /* MFR2 REV tones for this zone, continuous play */
337 struct list_head node;
338 struct kref refcount;
339 const char *name; /* Informational, only */
340 u8 num;
341 };
342
343 static void tone_zone_release(struct kref *kref)
344 {
345 struct dahdi_zone *z = container_of(kref, struct dahdi_zone, refcount);
346 kfree(z->name);
347 kfree(z);
348 }
349
350 /**
351 * tone_zone_put() - Release the reference on the tone_zone.
352 *
353 * On old kernels, since kref_put does not have a return value, we'll just
354 * always report that we released the memory.
355 *
356 */
357 static inline int tone_zone_put(struct dahdi_zone *z)
358 {
359 return kref_put(&z->refcount, tone_zone_release);
360 }
361
362 static inline void tone_zone_get(struct dahdi_zone *z)
363 {
364 kref_get(&z->refcount);
365 }
366
367 static DEFINE_SPINLOCK(zone_lock);
368
369 /* The first zone on the list is the default zone. */
370 static LIST_HEAD(tone_zones);
371
372 static inline struct device *span_device(struct dahdi_span *span)
373 {
374 return &span->parent->dev;
375 }
376
377 /* Protects the span_list and pseudo_chans lists from concurrent access in
378 * process context. The spin_lock is needed to synchronize with the interrupt
379 * handler. */
380 static DEFINE_SPINLOCK(chan_lock);
381
382 struct pseudo_chan {
383 struct dahdi_chan chan;
384 struct list_head node;
385 };
386
387 static inline struct pseudo_chan *chan_to_pseudo(struct dahdi_chan *chan)
388 {
389 return container_of(chan, struct pseudo_chan, chan);
390 }
391
392 enum { FIRST_PSEUDO_CHANNEL = 0x8000, };
393 /* This list is protected by the chan_lock. */
394 static LIST_HEAD(pseudo_chans);
395
396 /**
397 * is_pseudo_chan() - By definition pseudo channels are not on a span.
398 */
399 static inline bool is_pseudo_chan(const struct dahdi_chan *chan)
400 {
401 return (NULL == chan->span);
402 }
403
404 static DEFINE_MUTEX(registration_mutex);
405 static LIST_HEAD(span_list);
406
407 static unsigned long
408 __for_each_channel(unsigned long (*func)(struct dahdi_chan *chan,
409 unsigned long data),
410 unsigned long data)
411 {
412 int res;
413 struct dahdi_span *s;
414 struct pseudo_chan *pseudo;
415
416 list_for_each_entry(s, &span_list, spans_node) {
417 unsigned long x;
418 for (x = 0; x < s->channels; x++) {
419 struct dahdi_chan *const chan = s->chans[x];
420 res = func(chan, data);
421 if (res)
422 return res;
423 }
424 }
425
426 list_for_each_entry(pseudo, &pseudo_chans, node) {
427 res = func(&pseudo->chan, data);
428 if (res)
429 return res;
430 }
431 return 0;
432 }
433
434 /**
435 * _chan_from_num - Lookup a channel
436 *
437 * Must be called with the registration_mutex held.
438 *
439 */
440 static struct dahdi_chan *_chan_from_num(unsigned int channo)
441 {
442 struct dahdi_span *s;
443 struct pseudo_chan *pseudo;
444
445 if (channo >= FIRST_PSEUDO_CHANNEL) {
446 list_for_each_entry(pseudo, &pseudo_chans, node) {
447 if (pseudo->chan.channo == channo)
448 return &pseudo->chan;
449 }
450 return NULL;
451 }
452
453 /* When searching for the channel amongst the spans, we can use the
454 * fact that channels on a span must be numbered consecutively to skip
455 * checking each individual channel. */
456 list_for_each_entry(s, &span_list, spans_node) {
457 unsigned int basechan;
458 struct dahdi_chan *chan;
459
460 if (unlikely(!s->channels))
461 continue;
462
463 basechan = s->chans[0]->channo;
464 if (channo >= (basechan + s->channels))
465 continue;
466
467 /* Since all the spans should be on the list in sorted order,
468 * if channo is less than base chan, the caller must be
469 * looking for a channel that has already been removed. */
470 if (unlikely(channo < basechan))
471 return NULL;
472
473 chan = s->chans[channo - basechan];
474 WARN_ON(chan->channo != channo);
475 return chan;
476 }
477
478 return NULL;
479 }
480
481 static struct dahdi_chan *chan_from_num(unsigned int channo)
482 {
483 struct dahdi_chan *chan;
484 mutex_lock(&registration_mutex);
485 chan = _chan_from_num(channo);
486 mutex_unlock(&registration_mutex);
487 return chan;
488 }
489
490 static inline struct dahdi_chan *chan_from_file(struct file *file)
491 {
492 return (file->private_data) ?
493 file->private_data : chan_from_num(UNIT(file));
494 }
495
496 /**
497 * _find_span() - Find a span by span number.
498 *
499 * Must be called with registration_mutex held.
500 *
501 */
502 static struct dahdi_span *_find_span(int spanno)
503 {
504 struct dahdi_span *s;
505 list_for_each_entry(s, &span_list, spans_node) {
506 if (s->spanno == spanno) {
507 return s;
508 }
509 }
510 return NULL;
511 }
512 /**
513 * span_find_and_get() - Search for the span by number, and if found take out
514 * a reference on it.
515 *
516 * When you are no longer using the returned pointer, you must release it with
517 * a put_span call.
518 *
519 */
520 static struct dahdi_span *span_find_and_get(int spanno)
521 {
522 struct dahdi_span *found;
523
524 mutex_lock(&registration_mutex);
525 found = _find_span(spanno);
526 if (found && !get_span(found))
527 found = NULL;
528 mutex_unlock(&registration_mutex);
529 return found;
530 }
531
532 static unsigned int span_count(void)
533 {
534 unsigned int count = 0;
535 struct dahdi_span *s;
536 unsigned long flags;
537 spin_lock_irqsave(&chan_lock, flags);
538 list_for_each_entry(s, &span_list, spans_node)
539 ++count;
540 spin_unlock_irqrestore(&chan_lock, flags);
541 return count;
542 }
543
544 static inline bool can_provide_timing(const struct dahdi_span *const s)
545 {
546 return !s->cannot_provide_timing;
547 }
548
549 static int maxconfs;
550
551 short __dahdi_mulaw[256];
552 short __dahdi_alaw[256];
553
554 #ifndef CONFIG_CALC_XLAW
555 u_char __dahdi_lin2mu[16384];
556
557 u_char __dahdi_lin2a[16384];
558 #endif
559
560 static u_char defgain[256];
561
562 #define NUM_SIGS 10
563
564 static DEFINE_SPINLOCK(ecfactory_list_lock);
565
566 static LIST_HEAD(ecfactory_list);
567
568 struct ecfactory {
569 const struct dahdi_echocan_factory *ec;
570 struct list_head list;
571 };
572
573 int dahdi_register_echocan_factory(const struct dahdi_echocan_factory *ec)
574 {
575 struct ecfactory *cur;
576 struct ecfactory *new;
577
578 WARN_ON(!ec->owner);
579
580 new = kzalloc(sizeof(*new), GFP_KERNEL);
581 if (!new)
582 return -ENOMEM;
583
584 INIT_LIST_HEAD(&new->list);
585
586 spin_lock(&ecfactory_list_lock);
587
588 /* make sure it isn't already registered */
589 list_for_each_entry(cur, &ecfactory_list, list) {
590 if (cur->ec == ec) {
591 spin_unlock(&ecfactory_list_lock);
592 kfree(new);
593 return -EPERM;
594 }
595 }
596
597 new->ec = ec;
598 list_add_tail(&new->list, &ecfactory_list);
599
600 spin_unlock(&ecfactory_list_lock);
601
602 return 0;
603 }
604
605 void dahdi_unregister_echocan_factory(const struct dahdi_echocan_factory *ec)
606 {
607 struct ecfactory *cur, *next;
608
609 spin_lock(&ecfactory_list_lock);
610
611 list_for_each_entry_safe(cur, next, &ecfactory_list, list) {
612 if (cur->ec == ec) {
613 list_del(&cur->list);
614 kfree(cur);
615 break;
616 }
617 }
618
619 spin_unlock(&ecfactory_list_lock);
620 }
621
622 /* Is this span our syncronization master? */
623 int dahdi_is_sync_master(const struct dahdi_span *span)
624 {
625 return span == master_span;
626 }
627
628 static inline void rotate_sums(void)
629 {
630 /* Rotate where we sum and so forth */
631 static int pos = 0;
632 conf_sums_prev = sums + (DAHDI_MAX_CONF + 1) * pos;
633 conf_sums = sums + (DAHDI_MAX_CONF + 1) * ((pos + 1) % 3);
634 conf_sums_next = sums + (DAHDI_MAX_CONF + 1) * ((pos + 2) % 3);
635 pos = (pos + 1) % 3;
636 memset(conf_sums_next, 0, maxconfs * sizeof(sumtype));
637 }
638
639 /**
640 * is_chan_dacsed() - True if chan is sourcing it's data from another channel.
641 *
642 */
643 static inline bool is_chan_dacsed(const struct dahdi_chan *const chan)
644 {
645 return (NULL != chan->dacs_chan);
646 }
647
648 /**
649 * can_dacs_chans() - Returns true if it may be possible to dacs two channels.
650 *
651 */
652 static bool can_dacs_chans(struct dahdi_chan *dst, struct dahdi_chan *src)
653 {
654 if (src && dst && src->span && dst->span && src->span->ops &&
655 dst->span->ops && src->span->ops->dacs &&
656 (src->span->ops->dacs == dst->span->ops->dacs))
657 return true;
658 else
659 return false;
660 }
661
662 /**
663 * dahdi_chan_dacs() - Cross (or uncross) connect two channels.
664 * @dst: Channel on which to transmit the src data.
665 * @src: NULL to disable cross connect, otherwise the source of the
666 * data.
667 *
668 * This allows those boards that support it to cross connect one channel to
669 * another in hardware. If the cards cannot be crossed, uncross the
670 * destination channel by default..
671 *
672 */
673 static int dahdi_chan_dacs(struct dahdi_chan *dst, struct dahdi_chan *src)
674 {
675 int ret = 0;
676 if (can_dacs_chans(dst, src))
677 ret = dst->span->ops->dacs(dst, src);
678 else if (dst->span && dst->span->ops->dacs)
679 ret = dst->span->ops->dacs(dst, NULL);
680 return ret;
681 }
682
683 static void dahdi_disable_dacs(struct dahdi_chan *chan)
684 {
685 dahdi_chan_dacs(chan, NULL);
686 }
687
688 /*!
689 * \return quiescent (idle) signalling states, for the various signalling types
690 */
691 static int dahdi_q_sig(struct dahdi_chan *chan)
692 {
693 int x;
694 static const unsigned int in_sig[NUM_SIGS][2] = {
695 { DAHDI_SIG_NONE, 0 },
696 { DAHDI_SIG_EM, (DAHDI_ABIT << 8) },
697 { DAHDI_SIG_FXSLS, DAHDI_BBIT | (DAHDI_BBIT << 8) },
698 { DAHDI_SIG_FXSGS, DAHDI_ABIT | DAHDI_BBIT | ((DAHDI_ABIT | DAHDI_BBIT) << 8) },
699 { DAHDI_SIG_FXSKS, DAHDI_BBIT | ((DAHDI_ABIT | DAHDI_BBIT) << 8) },
700 { DAHDI_SIG_FXOLS, (DAHDI_ABIT << 8) },
701 { DAHDI_SIG_FXOGS, DAHDI_BBIT | ((DAHDI_ABIT | DAHDI_BBIT) << 8) },
702 { DAHDI_SIG_FXOKS, (DAHDI_ABIT << 8) },
703 { DAHDI_SIG_SF, 0 },
704 { DAHDI_SIG_EM_E1, DAHDI_DBIT | ((DAHDI_ABIT | DAHDI_DBIT) << 8) },
705 };
706
707 /* must have span to begin with */
708 if (!chan->span)
709 return -1;
710
711 /* if RBS does not apply, return error */
712 if (!(chan->span->flags & DAHDI_FLAG_RBS) || !chan->span->ops->rbsbits)
713 return -1;
714
715 if (chan->sig == DAHDI_SIG_CAS)
716 return chan->idlebits;
717
718 for (x = 0; x < NUM_SIGS; x++) {
719 if (in_sig[x][0] == chan->sig)
720 return in_sig[x][1];
721 }
722
723 return -1; /* not found -- error */
724 }
725
726 enum spantypes dahdi_str2spantype(const char *name)
727 {
728 if (strcasecmp("FXS", name) == 0)
729 return SPANTYPE_ANALOG_FXS;
730 else if (strcasecmp("FXO", name) == 0)
731 return SPANTYPE_ANALOG_FXO;
732 else if (strcasecmp("ANALOG_MIXED", name) == 0)
733 return SPANTYPE_ANALOG_MIXED;
734 else if (strcasecmp("E1", name) == 0)
735 return SPANTYPE_DIGITAL_E1;
736 else if (strcasecmp("T1", name) == 0)
737 return SPANTYPE_DIGITAL_T1;
738 else if (strcasecmp("J1", name) == 0)
739 return SPANTYPE_DIGITAL_J1;
740 else if (strcasecmp("BRI_NT", name) == 0)
741 return SPANTYPE_DIGITAL_BRI_NT;
742 else if (strcasecmp("BRI_TE", name) == 0)
743 return SPANTYPE_DIGITAL_BRI_TE;
744 else if (strcasecmp("BRI_SOFT", name) == 0)
745 return SPANTYPE_DIGITAL_BRI_SOFT;
746 else if (strcasecmp("DYNAMIC", name) == 0)
747 return SPANTYPE_DIGITAL_DYNAMIC;
748 else
749 return SPANTYPE_INVALID;
750 }
751 EXPORT_SYMBOL(dahdi_str2spantype);
752
753 const char *dahdi_spantype2str(enum spantypes st)
754 {
755 switch (st) {
756 case SPANTYPE_ANALOG_FXS: return "FXS";
757 case SPANTYPE_ANALOG_FXO: return "FXO";
758 case SPANTYPE_ANALOG_MIXED: return "ANALOG_MIXED";
759 case SPANTYPE_DIGITAL_E1: return "E1";
760 case SPANTYPE_DIGITAL_T1: return "T1";
761 case SPANTYPE_DIGITAL_J1: return "J1";
762 case SPANTYPE_DIGITAL_BRI_NT: return "BRI_NT";
763 case SPANTYPE_DIGITAL_BRI_TE: return "BRI_TE";
764 case SPANTYPE_DIGITAL_BRI_SOFT: return "BRI_SOFT";
765 case SPANTYPE_DIGITAL_DYNAMIC: return "DYNAMIC";
766 default:
767 case SPANTYPE_INVALID: return "INVALID";
768 };
769 }
770 EXPORT_SYMBOL(dahdi_spantype2str);
771
772
773 const char *dahdi_lineconfig_bit_name(int lineconfig_bit)
774 {
775 static const char * const table[] = {
776 /* These apply to T1 */
777 [4] = "D4",
778 [5] = "ESF",
779 [6] = "AMI",
780 [7] = "B8ZS",
781 /* These apply to E1 */
782 [8] = "CCS",
783 [9] = "HDB3",
784 [10] = "CRC4",
785 /* These apply to BRI */
786 [11] = "NTTE",
787 [12] = "TERM",
788 /* Finish */
789 [16] = "NOTOPEN",
790 };
791 if (lineconfig_bit < 0 || lineconfig_bit >= ARRAY_SIZE(table))
792 return NULL;
793 return table[lineconfig_bit];
794 }
795 EXPORT_SYMBOL(dahdi_lineconfig_bit_name);
796
797 ssize_t lineconfig_str(int lineconfig, char buf[], size_t size)
798 {
799 int framing_bit = 0;
800 int coding_bit = 0;
801 int crc4_bit = 0;
802 int len = 0;
803 int bit;
804 bool written = false;
805
806 for (bit = 4; bit <= 12; bit++) {
807 int mask = (1 << bit);
808 if (!(lineconfig & mask))
809 continue;
810 if (!framing_bit) {
811 switch (mask) {
812 case DAHDI_CONFIG_B8ZS:
813 case DAHDI_CONFIG_AMI:
814 case DAHDI_CONFIG_HDB3:
815 framing_bit = bit;
816 len += snprintf(buf + len, size, "%s%s",
817 (written) ? "/" : "",
818 dahdi_lineconfig_bit_name(bit));
819 written = true;
820 }
821 }
822 if (!coding_bit) {
823 switch (mask) {
824 case DAHDI_CONFIG_ESF:
825 case DAHDI_CONFIG_D4:
826 case DAHDI_CONFIG_CCS:
827 coding_bit = bit;
828 len += snprintf(buf + len, size, "%s%s",
829 (written) ? "/" : "",
830 dahdi_lineconfig_bit_name(bit));
831 written = true;
832 }
833 }
834 if (!crc4_bit && mask == DAHDI_CONFIG_CRC4) {
835 crc4_bit = bit;
836 len += snprintf(buf + len, size, "%s%s",
837 (written) ? "/" : "",
838 dahdi_lineconfig_bit_name(bit));
839 written = true;
840 }
841 }
842 return len;
843 }
844 EXPORT_SYMBOL(lineconfig_str);
845
846 #ifdef CONFIG_PROC_FS
847 const char *sigstr(int sig)
848 {
849 switch (sig) {
850 case DAHDI_SIG_FXSLS:
851 return "FXSLS";
852 case DAHDI_SIG_FXSKS:
853 return "FXSKS";
854 case DAHDI_SIG_FXSGS:
855 return "FXSGS";
856 case DAHDI_SIG_FXOLS:
857 return "FXOLS";
858 case DAHDI_SIG_FXOKS:
859 return "FXOKS";
860 case DAHDI_SIG_FXOGS:
861 return "FXOGS";
862 case DAHDI_SIG_EM:
863 return "E&M";
864 case DAHDI_SIG_EM_E1:
865 return "E&M-E1";
866 case DAHDI_SIG_CLEAR:
867 return "Clear";
868 case DAHDI_SIG_HDLCRAW:
869 return "HDLCRAW";
870 case DAHDI_SIG_HDLCFCS:
871 return "HDLCFCS";
872 case DAHDI_SIG_HDLCNET:
873 return "HDLCNET";
874 case DAHDI_SIG_HARDHDLC:
875 return "Hardware-assisted HDLC";
876 case DAHDI_SIG_MTP2:
877 return "MTP2";
878 case DAHDI_SIG_SLAVE:
879 return "Slave";
880 case DAHDI_SIG_CAS:
881 return "CAS";
882 case DAHDI_SIG_DACS:
883 return "DACS";
884 case DAHDI_SIG_DACS_RBS:
885 return "DACS+RBS";
886 case DAHDI_SIG_SF:
887 return "SF (ToneOnly)";
888 case DAHDI_SIG_NONE:
889 default:
890 return "Unconfigured";
891 }
892 }
893
894 int fill_alarm_string(char *buf, int count, int alarms)
895 {
896 int len;
897
898 if (alarms <= 0)
899 return 0;
900
901 len = snprintf(buf, count, "%s%s%s%s%s%s",
902 (alarms & DAHDI_ALARM_BLUE) ? "BLUE " : "",
903 (alarms & DAHDI_ALARM_YELLOW) ? "YELLOW " : "",
904 (alarms & DAHDI_ALARM_RED) ? "RED " : "",
905 (alarms & DAHDI_ALARM_LOOPBACK) ? "LOOP " : "",
906 (alarms & DAHDI_ALARM_RECOVER) ? "RECOVERING " : "",
907 (alarms & DAHDI_ALARM_NOTOPEN) ? "NOTOPEN " : "");
908
909 return len;
910 }
911
912 /*
913 * Sequential proc interface
914 */
915
916 static void seq_fill_alarm_string(struct seq_file *sfile, int alarms)
917 {
918 char tmp[70];
919
920 if (fill_alarm_string(tmp, sizeof(tmp), alarms))
921 seq_printf(sfile, "%s", tmp);
922 }
923
924 static int dahdi_seq_show(struct seq_file *sfile, void *data)
925 {
926 long spanno = (long)sfile->private;
927 int x;
928 struct dahdi_span *s;
929
930 s = span_find_and_get(spanno);
931 if (!s)
932 return -ENODEV;
933
934 if (s->name)
935 seq_printf(sfile, "Span %d: %s ", s->spanno, s->name);
936 if (s->desc)
937 seq_printf(sfile, "\"%s\"", s->desc);
938 else
939 seq_printf(sfile, "\"\"");
940
941 if (dahdi_is_sync_master(s))
942 seq_printf(sfile, " (MASTER)");
943
944 if (s->lineconfig) {
945 char tmpbuf[20];
946 lineconfig_str(s->lineconfig, tmpbuf, sizeof(tmpbuf));
947 seq_printf(sfile, " %s", tmpbuf);
948 }
949
950 seq_printf(sfile, " ");
951
952 /* list alarms */
953 seq_fill_alarm_string(sfile, s->alarms);
954 if (s->syncsrc &&
955 (s->syncsrc == s->spanno))
956 seq_printf(sfile, "ClockSource ");
957 seq_printf(sfile, "\n");
958 if (s->count.bpv)
959 seq_printf(sfile, "\tBPV count: %d\n", s->count.bpv);
960 if (s->count.crc4)
961 seq_printf(sfile, "\tCRC4 error count: %d\n", s->count.crc4);
962 if (s->count.ebit)
963 seq_printf(sfile, "\tE-bit error count: %d\n", s->count.ebit);
964 if (s->count.fas)
965 seq_printf(sfile, "\tFAS error count: %d\n", s->count.fas);
966 if (s->parent->irqmisses)
967 seq_printf(sfile, "\tIRQ misses: %d\n", s->parent->irqmisses);
968 if (s->count.timingslips)
969 seq_printf(sfile, "\tTiming slips: %d\n", s->count.timingslips);
970 seq_printf(sfile, "\n");
971
972 for (x = 0; x < s->channels; x++) {
973 struct dahdi_chan *chan = s->chans[x];
974
975 if (chan->name)
976 seq_printf(sfile, "\t%4d %s ", chan->channo,
977 chan->name);
978
979 if (chan->sig) {
980 if (chan->sig == DAHDI_SIG_SLAVE)
981 seq_printf(sfile, "%s ",
982 sigstr(chan->master->sig));
983 else {
984 seq_printf(sfile, "%s ", sigstr(chan->sig));
985 if (chan->nextslave &&
986 (chan->master->channo == chan->channo))
987 seq_printf(sfile, "Master ");
988 }
989 } else if (!chan->sigcap) {
990 seq_printf(sfile, "Reserved ");
991 }
992
993 if (test_bit(DAHDI_FLAGBIT_OPEN, &chan->flags))
994 seq_printf(sfile, "(In use) ");
995
996 #ifdef OPTIMIZE_CHANMUTE
997 if (chan->chanmute)
998 seq_printf(sfile, "(no pcm) ");
999 #endif
1000
1001 seq_fill_alarm_string(sfile, chan->chan_alarms);
1002
1003 mutex_lock(&chan->mutex);
1004 if (chan->ec_factory) {
1005 seq_printf(sfile, "(EC: %s - %s) ",
1006 chan->ec_factory->get_name(chan),
1007 chan->ec_state ? "ACTIVE" : "INACTIVE");
1008 }
1009 mutex_unlock(&chan->mutex);
1010
1011 seq_printf(sfile, "\n");
1012 }
1013 put_span(s);
1014 return 0;
1015 }
1016
1017 static int dahdi_proc_open(struct inode *inode, struct file *file)
1018 {
1019 return single_open(file, dahdi_seq_show, PDE_DATA(inode));
1020 }
1021
1022 static const struct file_operations dahdi_proc_ops = {
1023 .owner = THIS_MODULE,
1024 .open = dahdi_proc_open,
1025 .read = seq_read,
1026 .llseek = seq_lseek,
1027 .release = single_release,
1028 };
1029
1030 #endif
1031
1032 static int dahdi_first_empty_alias(void)
1033 {
1034 /* Find the first conference which has no alias pointing to it */
1035 int x;
1036 for (x=1;x<DAHDI_MAX_CONF;x++) {
1037 if (!confrev[x])
1038 return x;
1039 }
1040 return -1;
1041 }
1042
1043 static void recalc_maxconfs(void)
1044 {
1045 int x;
1046
1047 for (x = DAHDI_MAX_CONF - 1; x > 0; x--) {
1048 if (confrev[x]) {
1049 maxconfs = x + 1;
1050 return;
1051 }
1052 }
1053
1054 maxconfs = 0;
1055 }
1056
1057 static int dahdi_first_empty_conference(void)
1058 {
1059 /* Find the first conference which has no alias */
1060 int x;
1061
1062 for (x = DAHDI_MAX_CONF - 1; x > 0; x--) {
1063 if (!confalias[x])
1064 return x;
1065 }
1066
1067 return -1;
1068 }
1069
1070 static int dahdi_get_conf_alias(int x)
1071 {
1072 int a;
1073
1074 if (confalias[x])
1075 return confalias[x];
1076
1077 /* Allocate an alias */
1078 a = dahdi_first_empty_alias();
1079 confalias[x] = a;
1080 confrev[a] = x;
1081
1082 /* Highest conference may have changed */
1083 recalc_maxconfs();
1084
1085 return a;
1086 }
1087
1088 static unsigned long _chan_in_conf(struct dahdi_chan *chan, unsigned long x)
1089 {
1090 const int confmode = chan->confmode & DAHDI_CONF_MODE_MASK;
1091 return (chan && (chan->confna == x) &&
1092 (confmode == DAHDI_CONF_CONF ||
1093 confmode == DAHDI_CONF_CONFANN ||
1094 confmode == DAHDI_CONF_CONFMON ||
1095 confmode == DAHDI_CONF_CONFANNMON ||
1096 confmode == DAHDI_CONF_REALANDPSEUDO)) ? 1 : 0;
1097 }
1098
1099 #ifdef CONFIG_DAHDI_CONFLINK
1100 static void recalc_maxlinks(void)
1101 {
1102 int x;
1103
1104 for (x = DAHDI_MAX_CONF - 1; x > 0; x--) {
1105 if (conf_links[x].src || conf_links[x].dst) {
1106 maxlinks = x + 1;
1107 return;
1108 }
1109 }
1110
1111 maxlinks = 0;
1112 }
1113 #endif
1114
1115 static void dahdi_check_conf(int x)
1116 {
1117 unsigned long res;
1118 unsigned long flags;
1119 #ifdef CONFIG_DAHDI_CONFLINK
1120 int i;
1121 #endif
1122
1123 /* return if no valid conf number */
1124 if (x <= 0)
1125 return;
1126
1127 /* Return if there is no alias */
1128 if (!confalias[x])
1129 return;
1130
1131 spin_lock_irqsave(&chan_lock, flags);
1132 res = __for_each_channel(_chan_in_conf, x);
1133 spin_unlock_irqrestore(&chan_lock, flags);
1134 if (res)
1135 return;
1136
1137 /* If we get here, nobody is in the conference anymore. Clear it out
1138 both forward and reverse */
1139 confrev[confalias[x]] = 0;
1140 confalias[x] = 0;
1141
1142 /* Highest conference may have changed */
1143 recalc_maxconfs();
1144
1145 #ifdef CONFIG_DAHDI_CONFLINK
1146 /* And unlink it from any conflinks */
1147 for (i = DAHDI_MAX_CONF - 1; i > 0; i--) {
1148 if (conf_links[i].src == x)
1149 conf_links[i].src = 0;
1150 if (conf_links[i].dst == x)
1151 conf_links[i].dst = 0;
1152 }
1153 recalc_maxlinks();
1154 #endif
1155 }
1156
1157 /* enqueue an event on a channel */
1158 static void __qevent(struct dahdi_chan *chan, int event)
1159 {
1160 /* if full, ignore */
1161 if ((chan->eventoutidx == 0) && (chan->eventinidx == (DAHDI_MAX_EVENTSIZE - 1)))
1162 return;
1163
1164 /* if full, ignore */
1165 if (chan->eventinidx == (chan->eventoutidx - 1))
1166 return;
1167
1168 /* save the event */
1169 chan->eventbuf[chan->eventinidx++] = event;
1170
1171 /* wrap the index, if necessary */
1172 if (chan->eventinidx >= DAHDI_MAX_EVENTSIZE)
1173 chan->eventinidx = 0;
1174
1175 /* wake em all up */
1176 wake_up_interruptible(&chan->waitq);
1177
1178 return;
1179 }
1180
1181 void dahdi_qevent_nolock(struct dahdi_chan *chan, int event)
1182 {
1183 __qevent(chan, event);
1184 }
1185
1186 void dahdi_qevent_lock(struct dahdi_chan *chan, int event)
1187 {
1188 unsigned long flags;
1189 spin_lock_irqsave(&chan->lock, flags);
1190 __qevent(chan, event);
1191 spin_unlock_irqrestore(&chan->lock, flags);
1192 }
1193
1194 static inline void calc_fcs(struct dahdi_chan *ss, int inwritebuf)
1195 {
1196 int x;
1197 unsigned int fcs = PPP_INITFCS;
1198 unsigned char *data = ss->writebuf[inwritebuf];
1199 int len = ss->writen[inwritebuf];
1200
1201 /* Not enough space to do FCS calculation */
1202 if (len < 2)
1203 return;
1204
1205 for (x = 0; x < len - 2; x++)
1206 fcs = PPP_FCS(fcs, data[x]);
1207
1208 fcs ^= 0xffff;
1209 /* Send out the FCS */
1210 data[len - 2] = (fcs & 0xff);
1211 data[len - 1] = (fcs >> 8) & 0xff;
1212 }
1213
1214 static int dahdi_reallocbufs(struct dahdi_chan *ss, int blocksize, int numbufs)
1215 {
1216 unsigned char *newtxbuf = NULL;
1217 unsigned char *newrxbuf = NULL;
1218 unsigned char *oldtxbuf = NULL;
1219 unsigned char *oldrxbuf = NULL;
1220 unsigned long flags;
1221 int x;
1222
1223 /* Check numbufs */
1224 if (numbufs < 2)
1225 numbufs = 2;
1226
1227 if (numbufs > DAHDI_MAX_NUM_BUFS)
1228 numbufs = DAHDI_MAX_NUM_BUFS;
1229
1230 /* We need to allocate our buffers now */
1231 if (blocksize) {
1232 newtxbuf = kzalloc(blocksize * numbufs, GFP_KERNEL);
1233 if (NULL == newtxbuf)
1234 return -ENOMEM;
1235 newrxbuf = kzalloc(blocksize * numbufs, GFP_KERNEL);
1236 if (NULL == newrxbuf) {
1237 kfree(newtxbuf);
1238 return -ENOMEM;
1239 }
1240 }
1241
1242 /* Now that we've allocated our new buffers, we can safely
1243 move things around... */
1244
1245 spin_lock_irqsave(&ss->lock, flags);
1246
1247 ss->blocksize = blocksize; /* set the blocksize */
1248 oldrxbuf = ss->readbuf[0]; /* Keep track of the old buffer */
1249 oldtxbuf = ss->writebuf[0];
1250 ss->readbuf[0] = NULL;
1251
1252 if (newrxbuf) {
1253 BUG_ON(NULL == newtxbuf);
1254 for (x = 0; x < numbufs; x++) {
1255 ss->readbuf[x] = newrxbuf + x * blocksize;
1256 ss->writebuf[x] = newtxbuf + x * blocksize;
1257 }
1258 } else {
1259 for (x = 0; x < numbufs; x++) {
1260 ss->readbuf[x] = NULL;
1261 ss->writebuf[x] = NULL;
1262 }
1263 }
1264
1265 /* Mark all buffers as empty */
1266 for (x = 0; x < numbufs; x++) {
1267 ss->writen[x] =
1268 ss->writeidx[x]=
1269 ss->readn[x]=
1270 ss->readidx[x] = 0;
1271 }
1272
1273 /* Keep track of where our data goes (if it goes
1274 anywhere at all) */
1275 if (newrxbuf) {
1276 ss->inreadbuf = 0;
1277 ss->inwritebuf = 0;
1278 } else {
1279 ss->inreadbuf = -1;
1280 ss->inwritebuf = -1;
1281 }
1282
1283 ss->outreadbuf = -1;
1284 ss->outwritebuf = -1;
1285 ss->numbufs = numbufs;
1286
1287 if ((ss->txbufpolicy == DAHDI_POLICY_WHEN_FULL) || (ss->txbufpolicy == DAHDI_POLICY_HALF_FULL))
1288 ss->txdisable = 1;
1289 else
1290 ss->txdisable = 0;
1291
1292 spin_unlock_irqrestore(&ss->lock, flags);
1293
1294 kfree(oldtxbuf);
1295 kfree(oldrxbuf);
1296
1297 return 0;
1298 }
1299
1300 static int dahdi_hangup(struct dahdi_chan *chan);
1301 static void dahdi_set_law(struct dahdi_chan *chan, int law);
1302
1303 /* Pull a DAHDI_CHUNKSIZE piece off the queue. Returns
1304 0 on success or -1 on failure. If failed, provides
1305 silence */
1306 static int __buf_pull(struct confq *q, u_char *data, struct dahdi_chan *c)
1307 {
1308 int oldoutbuf = q->outbuf;
1309 /* Ain't nuffin to read */
1310 if (q->outbuf < 0) {
1311 if (data)
1312 memset(data, DAHDI_LIN2X(0,c), DAHDI_CHUNKSIZE);
1313 return -1;
1314 }
1315 if (data)
1316 memcpy(data, q->buf[q->outbuf], DAHDI_CHUNKSIZE);
1317 q->outbuf = (q->outbuf + 1) % DAHDI_CB_SIZE;
1318
1319 /* Won't be nuffin next time */
1320 if (q->outbuf == q->inbuf) {
1321 q->outbuf = -1;
1322 }
1323
1324 /* If they thought there was no space then
1325 there is now where we just read */
1326 if (q->inbuf < 0)
1327 q->inbuf = oldoutbuf;
1328 return 0;
1329 }
1330
1331 /* Returns a place to put stuff, or NULL if there is
1332 no room */
1333
1334 static u_char *__buf_pushpeek(struct confq *q)
1335 {
1336 if (q->inbuf < 0)
1337 return NULL;
1338 return q->buf[q->inbuf];
1339 }
1340
1341 static u_char *__buf_peek(struct confq *q)
1342 {
1343 if (q->outbuf < 0)
1344 return NULL;
1345 return q->buf[q->outbuf];
1346 }
1347
1348 /* Push something onto the queue, or assume what
1349 is there is valid if data is NULL */
1350 static int __buf_push(struct confq *q, const u_char *data)
1351 {
1352 int oldinbuf = q->inbuf;
1353 if (q->inbuf < 0) {
1354 return -1;
1355 }
1356 if (data)
1357 /* Copy in the data */
1358 memcpy(q->buf[q->inbuf], data, DAHDI_CHUNKSIZE);
1359
1360 /* Advance the inbuf pointer */
1361 q->inbuf = (q->inbuf + 1) % DAHDI_CB_SIZE;
1362
1363 if (q->inbuf == q->outbuf) {
1364 /* No space anymore... */
1365 q->inbuf = -1;
1366 }
1367 /* If they don't think data is ready, let
1368 them know it is now */
1369 if (q->outbuf < 0) {
1370 q->outbuf = oldinbuf;
1371 }
1372 return 0;
1373 }
1374
1375 static void reset_conf(struct dahdi_chan *chan)
1376 {
1377 int x;
1378
1379 /* Empty out buffers and reset to initialization */
1380
1381 for (x = 0; x < DAHDI_CB_SIZE; x++)
1382 chan->confin.buf[x] = chan->confin.buffer + DAHDI_CHUNKSIZE * x;
1383
1384 chan->confin.inbuf = 0;
1385 chan->confin.outbuf = -1;
1386
1387 for (x = 0; x < DAHDI_CB_SIZE; x++)
1388 chan->confout.buf[x] = chan->confout.buffer + DAHDI_CHUNKSIZE * x;
1389
1390 chan->confout.inbuf = 0;
1391 chan->confout.outbuf = -1;
1392 }
1393
1394
1395 static const struct dahdi_echocan_factory *find_echocan(const char *name)
1396 {
1397 struct ecfactory *cur;
1398 char *name_upper;
1399 char *c;
1400 const char *d;
1401 char modname_buf[128] = "dahdi_echocan_";
1402 unsigned int tried_once = 0;
1403
1404 name_upper = kmalloc(strlen(name) + 1, GFP_KERNEL);
1405 if (!name_upper)
1406 return NULL;
1407
1408 for (c = name_upper, d = name; *d; c++, d++) {
1409 *c = toupper(*d);
1410 }
1411
1412 *c = '\0';
1413
1414 retry:
1415 spin_lock(&ecfactory_list_lock);
1416
1417 list_for_each_entry(cur, &ecfactory_list, list) {
1418 if (!strcmp(name_upper, cur->ec->get_name(NULL))) {
1419 if (try_module_get(cur->ec->owner)) {
1420 spin_unlock(&ecfactory_list_lock);
1421 kfree(name_upper);
1422 return cur->ec;
1423 } else {
1424 spin_unlock(&ecfactory_list_lock);
1425 kfree(name_upper);
1426 return NULL;
1427 }
1428 }
1429 }
1430
1431 spin_unlock(&ecfactory_list_lock);
1432
1433 if (tried_once) {
1434 kfree(name_upper);
1435 return NULL;
1436 }
1437
1438 /* couldn't find it, let's try to load it */
1439
1440 for (c = &modname_buf[strlen(modname_buf)], d = name; *d; c++, d++) {
1441 *c = tolower(*d);
1442 }
1443
1444 request_module("%s", modname_buf);
1445
1446 tried_once = 1;
1447
1448 /* and try one more time */
1449 goto retry;
1450 }
1451
1452 static void release_echocan(const struct dahdi_echocan_factory *ec)
1453 {
1454 if (ec)
1455 module_put(ec->owner);
1456 }
1457
1458 /**
1459 * is_gain_allocated() - True if gain tables were dynamically allocated.
1460 * @chan: The channel to check.
1461 */
1462 static inline bool is_gain_allocated(const struct dahdi_chan *chan)
1463 {
1464 return (chan->rxgain && (chan->rxgain != defgain));
1465 }
1466
1467 static const char *hwec_def_name = "HWEC";
1468 static const char *hwec_get_name(const struct dahdi_chan *chan)
1469 {
1470 if (chan && chan->span && chan->span->ops->echocan_name)
1471 return chan->span->ops->echocan_name(chan);
1472 else
1473 return hwec_def_name;
1474 }
1475
1476 static int hwec_echocan_create(struct dahdi_chan *chan,
1477 struct dahdi_echocanparams *ecp, struct dahdi_echocanparam *p,
1478 struct dahdi_echocan_state **ec)
1479 {
1480 if (chan->span && chan->span->ops->echocan_create)
1481 return chan->span->ops->echocan_create(chan, ecp, p, ec);
1482 else
1483 return -ENODEV;
1484 }
1485
1486 static const struct dahdi_echocan_factory hwec_factory = {
1487 .get_name = hwec_get_name,
1488 .owner = THIS_MODULE,
1489 .echocan_create = hwec_echocan_create,
1490 };
1491
1492 /**
1493 * dahdi_enable_hw_preechocan - Let the board driver enable hwpreec if possible.
1494 * @chan: The channel to monitor.
1495 *
1496 * Returns 0 on success, if there is a software echocanceler attached on
1497 * the channel, or the span does not have an enable_hw_preechocan callback.
1498 * Otherwise an error code.
1499 *
1500 */
1501 static int dahdi_enable_hw_preechocan(struct dahdi_chan *chan)
1502 {
1503 int res;
1504
1505 mutex_lock(&chan->mutex);
1506 if (chan->ec_factory != &hwec_factory)
1507 res = -ENODEV;
1508 else
1509 res = 0;
1510 mutex_unlock(&chan->mutex);
1511
1512 if (-ENODEV == res)
1513 return 0;
1514
1515 if (chan->span->ops->enable_hw_preechocan)
1516 return chan->span->ops->enable_hw_preechocan(chan);
1517 else
1518 return 0;
1519 }
1520
1521 /**
1522 * dahdi_disable_hw_preechocan - Disable any hardware pre echocan monitoring.
1523 * @chan: The channel to stop monitoring.
1524 *
1525 * Give the board driver the option to free any resources needed to monitor
1526 * the preechocan stream.
1527 *
1528 */
1529 static void dahdi_disable_hw_preechocan(struct dahdi_chan *chan)
1530 {
1531 if (chan->span->ops->disable_hw_preechocan)
1532 chan->span->ops->disable_hw_preechocan(chan);
1533 }
1534
1535 /*
1536 * close_channel - close the channel, resetting any channel variables
1537 * @chan: the dahdi_chan to close
1538 *
1539 * This function is called before either the parent span is linked into the
1540 * span list, or for pseudos, place on the psuedo_list. Therefore, this
1541 * function nor it's callers should depend on the channel being findable
1542 * via those methods.
1543 */
1544 static void close_channel(struct dahdi_chan *chan)
1545 {
1546 unsigned long flags;
1547 const void *rxgain = NULL;
1548 struct dahdi_echocan_state *ec_state;
1549 const struct dahdi_echocan_factory *ec_current;
1550 int oldconf;
1551 short *readchunkpreec;
1552 #ifdef CONFIG_DAHDI_PPP
1553 struct ppp_channel *ppp;
1554 #endif
1555
1556 might_sleep();
1557
1558 if (chan->conf_chan &&
1559 ((DAHDI_CONF_MONITOR_RX_PREECHO == chan->confmode) ||
1560 (DAHDI_CONF_MONITOR_TX_PREECHO == chan->confmode) ||
1561 (DAHDI_CONF_MONITORBOTH_PREECHO == chan->confmode))) {
1562 void *readchunkpreec;
1563
1564 spin_lock_irqsave(&chan->conf_chan->lock, flags);
1565 readchunkpreec = chan->conf_chan->readchunkpreec;
1566 chan->conf_chan->readchunkpreec = NULL;
1567 spin_unlock_irqrestore(&chan->conf_chan->lock, flags);
1568
1569 if (readchunkpreec) {
1570 dahdi_disable_hw_preechocan(chan->conf_chan);
1571 kfree(readchunkpreec);
1572 }
1573 }
1574
1575 /* XXX Buffers should be send out before reallocation!!! XXX */
1576 if (!(chan->flags & DAHDI_FLAG_NOSTDTXRX))
1577 dahdi_reallocbufs(chan, 0, 0);
1578 spin_lock_irqsave(&chan->lock, flags);
1579 #ifdef CONFIG_DAHDI_PPP
1580 ppp = chan->ppp;
1581 chan->ppp = NULL;
1582 #endif
1583 ec_state = chan->ec_state;
1584 chan->ec_state = NULL;
1585 ec_current = chan->ec_current;
1586 chan->ec_current = NULL;
1587 readchunkpreec = chan->readchunkpreec;
1588 chan->readchunkpreec = NULL;
1589 chan->curtone = NULL;
1590 if (chan->curzone) {
1591 struct dahdi_zone *zone = chan->curzone;
1592 chan->curzone = NULL;
1593 tone_zone_put(zone);
1594 }
1595 chan->cadencepos = 0;
1596 chan->pdialcount = 0;
1597 dahdi_hangup(chan);
1598 chan->itimerset = chan->itimer = 0;
1599 chan->pulsecount = 0;
1600 chan->pulsetimer = 0;
1601 chan->ringdebtimer = 0;
1602 chan->txdialbuf[0] = '\0';
1603 chan->digitmode = DIGIT_MODE_DTMF;
1604 chan->dialing = 0;
1605 chan->afterdialingtimer = 0;
1606 /* initialize IO MUX mask */
1607 chan->iomask = 0;
1608 /* save old conf number, if any */
1609 oldconf = chan->confna;
1610 /* initialize conference variables */
1611 chan->_confn = 0;
1612 chan->confna = 0;
1613 chan->confmode = 0;
1614 if ((chan->sig & __DAHDI_SIG_DACS) != __DAHDI_SIG_DACS)
1615 chan->dacs_chan = NULL;
1616
1617 chan->confmute = 0;
1618 chan->gotgs = 0;
1619 reset_conf(chan);
1620 chan->dacs_chan = NULL;
1621
1622 if (is_gain_allocated(chan))
1623 rxgain = chan->rxgain;
1624
1625 chan->rxgain = defgain;
1626 chan->txgain = defgain;
1627 chan->eventinidx = chan->eventoutidx = 0;
1628 chan->flags &= ~(DAHDI_FLAG_LOOPED | DAHDI_FLAG_LINEAR | DAHDI_FLAG_PPP | DAHDI_FLAG_SIGFREEZE);
1629
1630 dahdi_set_law(chan, DAHDI_LAW_DEFAULT);
1631
1632 memset(chan->conflast, 0, sizeof(chan->conflast));
1633 memset(chan->conflast1, 0, sizeof(chan->conflast1));
1634 memset(chan->conflast2, 0, sizeof(chan->conflast2));
1635
1636 if (chan->span && oldconf)
1637 dahdi_disable_dacs(chan);
1638
1639 spin_unlock_irqrestore(&chan->lock, flags);
1640
1641 if (ec_state) {
1642 ec_state->ops->echocan_free(chan, ec_state);
1643 release_echocan(ec_current);
1644 }
1645
1646 /* release conference resource, if any to release */
1647 if (oldconf)
1648 dahdi_check_conf(oldconf);
1649
1650 if (rxgain)
1651 kfree(rxgain);
1652
1653 if (readchunkpreec) {
1654 dahdi_disable_hw_preechocan(chan);
1655 kfree(readchunkpreec);
1656 }
1657
1658 #ifdef CONFIG_DAHDI_PPP
1659 if (ppp) {
1660 tasklet_kill(&chan->ppp_calls);
1661 skb_queue_purge(&chan->ppp_rq);
1662 ppp_unregister_channel(ppp);
1663 kfree(ppp);
1664 }
1665 #endif
1666
1667 }
1668
1669 static int dahdi_ioctl_freezone(unsigned long data)
1670 {
1671 struct dahdi_zone *z;
1672 struct dahdi_zone *found = NULL;
1673 int num;
1674
1675 if (get_user(num, (int __user *) data))
1676 return -EFAULT;
1677
1678 spin_lock(&zone_lock);
1679 list_for_each_entry(z, &tone_zones, node) {
1680 if (z->num == num) {
1681 found = z;
1682 break;
1683 }
1684 }
1685 if (found) {
1686 list_del(&found->node);
1687 }
1688 spin_unlock(&zone_lock);
1689
1690 if (found) {
1691 if (debug) {
1692 module_printk(KERN_INFO,
1693 "Unregistering tone zone %d (%s)\n",
1694 found->num, found->name);
1695 }
1696 tone_zone_put(found);
1697 }
1698 return 0;
1699 }
1700
1701 static int dahdi_register_tone_zone(struct dahdi_zone *zone)
1702 {
1703 struct dahdi_zone *cur;
1704 int res = 0;
1705
1706 kref_init(&zone->refcount);
1707 spin_lock(&zone_lock);
1708 list_for_each_entry(cur, &tone_zones, node) {
1709 if (cur->num == zone->num) {
1710 res = -EINVAL;
1711 break;
1712 }
1713 }
1714 if (!res) {
1715 list_add_tail(&zone->node, &tone_zones);
1716 if (debug) {
1717 module_printk(KERN_INFO,
1718 "Registered tone zone %d (%s)\n",
1719 zone->num, zone->name);
1720 }
1721 }
1722 spin_unlock(&zone_lock);
1723
1724 return res;
1725 }
1726
1727 static int start_tone_digit(struct dahdi_chan *chan, int tone)
1728 {
1729 struct dahdi_tone *playtone = NULL;
1730 int base, max;
1731
1732 if (!chan->curzone)
1733 return -ENODATA;
1734
1735 switch (chan->digitmode) {
1736 case DIGIT_MODE_DTMF:
1737 /* Set dialing so that a dial operation doesn't interrupt this tone */
1738 chan->dialing = 1;
1739 base = DAHDI_TONE_DTMF_BASE;
1740 max = DAHDI_TONE_DTMF_MAX;
1741 break;
1742 case DIGIT_MODE_MFR2_FWD:
1743 base = DAHDI_TONE_MFR2_FWD_BASE;
1744 max = DAHDI_TONE_MFR2_FWD_MAX;
1745 break;
1746 case DIGIT_MODE_MFR2_REV:
1747 base = DAHDI_TONE_MFR2_REV_BASE;
1748 max = DAHDI_TONE_MFR2_REV_MAX;
1749 break;
1750 default:
1751 return -EINVAL;
1752 }
1753
1754 if ((tone < base) || (tone > max))
1755 return -EINVAL;
1756
1757 switch (chan->digitmode) {
1758 case DIGIT_MODE_DTMF:
1759 playtone = &chan->curzone->dtmf_continuous[tone - base];
1760 break;
1761 case DIGIT_MODE_MFR2_FWD:
1762 playtone = &chan->curzone->mfr2_fwd_continuous[tone - base];
1763 break;
1764 case DIGIT_MODE_MFR2_REV:
1765 playtone = &chan->curzone->mfr2_rev_continuous[tone - base];
1766 break;
1767 }
1768
1769 if (!playtone || !playtone->tonesamples)
1770 return -ENOSYS;
1771
1772 chan->curtone = playtone;
1773
1774 return 0;
1775 }
1776
1777 static int start_tone(struct dahdi_chan *chan, int tone)
1778 {
1779 int res = -EINVAL;
1780
1781 /* Stop the current tone, no matter what */
1782 chan->tonep = 0;
1783 chan->curtone = NULL;
1784 chan->pdialcount = 0;
1785 chan->txdialbuf[0] = '\0';
1786 chan->dialing = 0;
1787
1788 if (tone == -1) {
1789 /* Just stop the current tone */
1790 res = 0;
1791 } else if (!chan->curzone) {
1792 static int __warnonce = 1;
1793 if (__warnonce) {
1794 __warnonce = 0;
1795 /* The tonezones are loaded by dahdi_cfg based on /etc/dahdi/system.conf. */
1796 module_printk(KERN_WARNING, "DAHDI: Cannot start tones until tone zone is loaded.\n");
1797 }
1798 /* Note that no tone zone exists at the moment */
1799 res = -ENODATA;
1800 } else if ((tone >= 0 && tone <= DAHDI_TONE_MAX)) {
1801 /* Have a tone zone */
1802 if (chan->curzone->tones[tone]) {
1803 chan->curtone = chan->curzone->tones[tone];
1804 res = 0;
1805 } else { /* Indicate that zone is loaded but no such tone exists */
1806 res = -ENOSYS;
1807 }
1808 } else if (chan->digitmode == DIGIT_MODE_DTMF ||
1809 chan->digitmode == DIGIT_MODE_MFR2_FWD ||
1810 chan->digitmode == DIGIT_MODE_MFR2_REV) {
1811 res = start_tone_digit(chan, tone);
1812 } else {
1813 chan->dialing = 0;
1814 res = -EINVAL;
1815 }
1816
1817 if (chan->curtone)
1818 dahdi_init_tone_state(&chan->ts, chan->curtone);
1819
1820 return res;
1821 }
1822
1823 /**
1824 * stop_tone - Stops any tones on a channel.
1825 *
1826 * Must be called with chan->lock held.
1827 *
1828 */
1829 static inline int stop_tone(struct dahdi_chan *chan)
1830 {
1831 return start_tone(chan, -1);
1832 }
1833
1834 static int set_tone_zone(struct dahdi_chan *chan, int zone)
1835 {
1836 int res = 0;
1837 struct dahdi_zone *cur;
1838 struct dahdi_zone *z;
1839 unsigned long flags;
1840
1841 z = NULL;
1842 spin_lock(&zone_lock);
1843 if ((DEFAULT_TONE_ZONE == zone) && !list_empty(&tone_zones)) {
1844 z = list_entry(tone_zones.next, struct dahdi_zone, node);
1845 tone_zone_get(z);
1846 } else {
1847 list_for_each_entry(cur, &tone_zones, node) {
1848 if (cur->num != (u8)zone)
1849 continue;
1850 z = cur;
1851 tone_zone_get(z);
1852 break;
1853 }
1854 }
1855 spin_unlock(&zone_lock);
1856
1857 if (unlikely(!z))
1858 return -ENODATA;
1859
1860 spin_lock_irqsave(&chan->lock, flags);
1861
1862 stop_tone(chan);
1863
1864 if (chan->curzone) {
1865 struct dahdi_zone *zone = chan->curzone;
1866 chan->curzone = NULL;
1867 tone_zone_put(zone);
1868 }
1869 chan->curzone = z;
1870 memcpy(chan->ringcadence, z->ringcadence, sizeof(chan->ringcadence));
1871 spin_unlock_irqrestore(&chan->lock, flags);
1872
1873 return res;
1874 }
1875
1876 static void dahdi_set_law(struct dahdi_chan *chan, int law)
1877 {
1878 if (DAHDI_LAW_DEFAULT == law) {
1879 if (chan->deflaw)
1880 law = chan->deflaw;
1881 else
1882 if (chan->span) law = chan->span->deflaw;
1883 else law = DAHDI_LAW_MULAW;
1884 }
1885 if (law == DAHDI_LAW_ALAW) {
1886 chan->xlaw = __dahdi_alaw;
1887 #ifdef CONFIG_CALC_XLAW
1888 chan->lineartoxlaw = __dahdi_lineartoalaw;
1889 #else
1890 chan->lin2x = __dahdi_lin2a;
1891 #endif
1892 } else {
1893 chan->xlaw = __dahdi_mulaw;
1894 #ifdef CONFIG_CALC_XLAW
1895 chan->lineartoxlaw = __dahdi_lineartoulaw;
1896 #else
1897 chan->lin2x = __dahdi_lin2mu;
1898 #endif
1899 }
1900 }
1901
1902 /**
1903 * __dahdi_init_chan - Initialize the channel data structures.
1904 * @chan: The channel to initialize
1905 *
1906 */
1907 static void __dahdi_init_chan(struct dahdi_chan *chan)
1908 {
1909 might_sleep();
1910
1911 spin_lock_init(&chan->lock);
1912 mutex_init(&chan->mutex);
1913 init_waitqueue_head(&chan->waitq);
1914 if (!chan->master)
1915 chan->master = chan;
1916 if (!chan->readchunk)
1917 chan->readchunk = chan->sreadchunk;
1918 if (!chan->writechunk)
1919 chan->writechunk = chan->swritechunk;
1920 chan->rxgain = NULL;
1921 chan->txgain = NULL;
1922 close_channel(chan);
1923 }
1924
1925 /**
1926 * dahdi_chan_reg - Mark the channel registered.
1927 *
1928 * This must be called after close channel during registration, normally
1929 * covered by the call to __dahdi_init_chan, to avoid "HDLC hangage"
1930 */
1931 static inline void dahdi_chan_reg(struct dahdi_chan *chan)
1932 {
1933 set_bit(DAHDI_FLAGBIT_REGISTERED, &chan->flags);
1934 }
1935
1936 /**
1937 * dahdi_lboname() - Convert line build out number to string.
1938 *
1939 */
1940 const char *dahdi_lboname(int lbo)
1941 {
1942 /* names of tx level settings */
1943 static const char *const dahdi_txlevelnames[] = {
1944 "0 db (CSU)/0-133 feet (DSX-1)",
1945 "133-266 feet (DSX-1)",
1946 "266-399 feet (DSX-1)",
1947 "399-533 feet (DSX-1)",
1948 "533-655 feet (DSX-1)",
1949 "-7.5db (CSU)",
1950 "-15db (CSU)",
1951 "-22.5db (CSU)"
1952 };
1953
1954 if ((lbo < 0) || (lbo > 7))
1955 return "Unknown";
1956 return dahdi_txlevelnames[lbo];
1957 }
1958 EXPORT_SYMBOL(dahdi_lboname);
1959
1960 #if defined(CONFIG_DAHDI_NET) || defined(CONFIG_DAHDI_PPP)
1961 static inline void print_debug_writebuf(struct dahdi_chan* ss, struct sk_buff *skb, int oldbuf)
1962 {
1963 #ifdef CONFIG_DAHDI_DEBUG
1964 int x;
1965
1966 module_printk(KERN_NOTICE, "Buffered %d bytes to go out in buffer %d\n", ss->writen[oldbuf], oldbuf);
1967 module_printk(KERN_DEBUG "");
1968 for (x=0;x<ss->writen[oldbuf];x++)
1969 printk("%02x ", ss->writebuf[oldbuf][x]);
1970 printk("\n");
1971 #endif
1972 }
1973 #endif
1974
1975 #ifdef CONFIG_DAHDI_NET
1976 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 26)
1977 static inline struct net_device_stats *hdlc_stats(struct net_device *dev)
1978 {
1979 return &dev->stats;
1980 }
1981 #endif
1982
1983 static int dahdi_net_open(struct net_device *dev)
1984 {
1985 int res = hdlc_open(dev);
1986 struct dahdi_chan *ms = netdev_to_chan(dev);
1987
1988 /* if (!dev->hard_start_xmit) return res; is this really necessary? --byg */
1989 if (res) /* this is necessary to avoid kernel panic when UNSPEC link encap, proven --byg */
1990 return res;
1991
1992 if (!ms) {
1993 module_printk(KERN_NOTICE, "dahdi_net_open: nothing??\n");
1994 return -EINVAL;
1995 }
1996 if (test_bit(DAHDI_FLAGBIT_OPEN, &ms->flags)) {
1997 module_printk(KERN_NOTICE, "%s is already open!\n", ms->name);
1998 return -EBUSY;
1999 }
2000 if (!dahdi_have_netdev(ms)) {
2001 module_printk(KERN_NOTICE, "%s is not a net device!\n", ms->name);
2002 return -EINVAL;
2003 }
2004 ms->txbufpolicy = DAHDI_POLICY_IMMEDIATE;
2005
2006 res = dahdi_reallocbufs(ms, DAHDI_DEFAULT_MTU_MRU, DAHDI_DEFAULT_NUM_BUFS);
2007 if (res)
2008 return res;
2009
2010 fasthdlc_init(&ms->rxhdlc, (ms->flags & DAHDI_FLAG_HDLC56) ? FASTHDLC_MODE_56 : FASTHDLC_MODE_64);
2011 fasthdlc_init(&ms->txhdlc, (ms->flags & DAHDI_FLAG_HDLC56) ? FASTHDLC_MODE_56 : FASTHDLC_MODE_64);
2012 ms->infcs = PPP_INITFCS;
2013
2014 netif_start_queue(chan_to_netdev(ms));
2015
2016 #ifdef CONFIG_DAHDI_DEBUG
2017 module_printk(KERN_NOTICE, "DAHDINET: Opened channel %d name %s\n", ms->channo, ms->name);
2018 #endif
2019 return 0;
2020 }
2021
2022 static int dahdi_register_hdlc_device(struct net_device *dev, const char *dev_name)
2023 {
2024 int result;
2025
2026 if (dev_name && *dev_name) {
2027 if ((result = dev_alloc_name(dev, dev_name)) < 0)
2028 return result;
2029 }
2030 result = register_netdev(dev);
2031 if (result != 0)
2032 return -EIO;
2033 return 0;
2034 }
2035
2036 static int dahdi_net_stop(struct net_device *dev)
2037 {
2038 hdlc_device *h = dev_to_hdlc(dev);
2039 struct dahdi_hdlc *hdlc = h->priv;
2040
2041 struct dahdi_chan *ms = hdlc_to_chan(hdlc);
2042 if (!ms) {
2043 module_printk(KERN_NOTICE, "dahdi_net_stop: nothing??\n");
2044 return 0;
2045 }
2046 if (!dahdi_have_netdev(ms)) {
2047 module_printk(KERN_NOTICE, "dahdi_net_stop: %s is not a net device!\n", ms->name);
2048 return 0;
2049 }
2050 /* Not much to do here. Just deallocate the buffers */
2051 netif_stop_queue(chan_to_netdev(ms));
2052 dahdi_reallocbufs(ms, 0, 0);
2053 hdlc_close(dev);
2054 return 0;
2055 }
2056
2057 /* kernel 2.4.20+ has introduced attach function, dunno what to do,
2058 just copy sources from dscc4 to be sure and ready for further mastering,
2059 NOOP right now (i.e. really a stub) --byg */
2060 static int dahdi_net_attach(struct net_device *dev, unsigned short encoding,
2061 unsigned short parity)
2062 {
2063 /* struct net_device *dev = hdlc_to_dev(hdlc);
2064 struct dscc4_dev_priv *dpriv = dscc4_priv(dev);
2065
2066 if (encoding != ENCODING_NRZ &&
2067 encoding != ENCODING_NRZI &&
2068 encoding != ENCODING_FM_MARK &&
2069 encoding != ENCODING_FM_SPACE &&
2070 encoding != ENCODING_MANCHESTER)
2071 return -EINVAL;
2072
2073 if (parity != PARITY_NONE &&
2074 parity != PARITY_CRC16_PR0_CCITT &&
2075 parity != PARITY_CRC16_PR1_CCITT &&
2076 parity != PARITY_CRC32_PR0_CCITT &&
2077 parity != PARITY_CRC32_PR1_CCITT)
2078 return -EINVAL;
2079
2080 dpriv->encoding = encoding;
2081 dpriv->parity = parity;*/
2082 return 0;
2083 }
2084
2085 static struct dahdi_hdlc *dahdi_hdlc_alloc(void)
2086 {
2087 return kzalloc(sizeof(struct dahdi_hdlc), GFP_KERNEL);
2088 }
2089
2090 static int dahdi_xmit(struct sk_buff *skb, struct net_device *dev)
2091 {
2092 /* FIXME: this construction seems to be not very optimal for me but I
2093 * could find nothing better at the moment (Friday, 10PM :( ) --byg
2094 * */
2095 struct dahdi_chan *ss = netdev_to_chan(dev);
2096 struct net_device_stats *stats = hdlc_stats(dev);
2097
2098 int retval = 1;
2099 int x,oldbuf;
2100 unsigned int fcs;
2101 unsigned char *data;
2102 unsigned long flags;
2103 /* See if we have any buffers */
2104 spin_lock_irqsave(&ss->lock, flags);
2105 if (skb->len > ss->blocksize - 2) {
2106 module_printk(KERN_ERR, "dahdi_xmit(%s): skb is too large (%d > %d)\n", dev->name, skb->len, ss->blocksize -2);
2107 stats->tx_dropped++;
2108 retval = 0;
2109 } else if (ss->inwritebuf >= 0) {
2110 /* We have a place to put this packet */
2111 /* XXX We should keep the SKB and avoid the memcpy XXX */
2112 data = ss->writebuf[ss->inwritebuf];
2113 memcpy(data, skb->data, skb->len);
2114 ss->writen[ss->inwritebuf] = skb->len;
2115 ss->writeidx[ss->inwritebuf] = 0;
2116 /* Calculate the FCS */
2117 fcs = PPP_INITFCS;
2118 for (x=0;x<skb->len;x++)
2119 fcs = PPP_FCS(fcs, data[x]);
2120 /* Invert it */
2121 fcs ^= 0xffff;
2122 /* Send it out LSB first */
2123 data[ss->writen[ss->inwritebuf]++] = (fcs & 0xff);
2124 data[ss->writen[ss->inwritebuf]++] = (fcs >> 8) & 0xff;
2125 /* Advance to next window */
2126 oldbuf = ss->inwritebuf;
2127 ss->inwritebuf = (ss->inwritebuf + 1) % ss->numbufs;
2128
2129 if (ss->inwritebuf == ss->outwritebuf) {
2130 /* Whoops, no more space. */
2131 ss->inwritebuf = -1;
2132
2133 netif_stop_queue(chan_to_netdev(ss));
2134 }
2135 if (ss->outwritebuf < 0) {
2136 /* Let the interrupt handler know there's
2137 some space for us */
2138 ss->outwritebuf = oldbuf;
2139 }
2140 dev->trans_start = jiffies;
2141 stats->tx_packets++;
2142 stats->tx_bytes += ss->writen[oldbuf];
2143 print_debug_writebuf(ss, skb, oldbuf);
2144 retval = 0;
2145 /* Free the SKB */
2146 dev_kfree_skb_any(skb);
2147 }
2148 spin_unlock_irqrestore(&ss->lock, flags);
2149 return retval;
2150 }
2151
2152 static int dahdi_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2153 {
2154 return hdlc_ioctl(dev, ifr, cmd);
2155 }
2156
2157 #endif
2158
2159 #ifdef CONFIG_DAHDI_PPP
2160
2161 static int dahdi_ppp_xmit(struct ppp_channel *ppp, struct sk_buff *skb)
2162 {
2163
2164 /*
2165 * If we can't handle the packet right now, return 0. If we
2166 * we handle or drop it, return 1. Always free if we return
2167 * 1 and never if we return 0
2168 */
2169 struct dahdi_chan *ss = ppp->private;
2170 int x,oldbuf;
2171 unsigned int fcs;
2172 unsigned char *data;
2173 unsigned long flags;
2174 int retval = 0;
2175
2176 /* See if we have any buffers */
2177 spin_lock_irqsave(&ss->lock, flags);
2178 if (!(test_bit(DAHDI_FLAGBIT_OPEN, &ss->flags))) {
2179 module_printk(KERN_ERR, "Can't transmit on closed channel\n");
2180 retval = 1;
2181 } else if (skb->len > ss->blocksize - 4) {
2182 module_printk(KERN_ERR, "dahdi_ppp_xmit(%s): skb is too large (%d > %d)\n", ss->name, skb->len, ss->blocksize -2);
2183 retval = 1;
2184 } else if (ss->inwritebuf >= 0) {
2185 /* We have a place to put this packet */
2186 /* XXX We should keep the SKB and avoid the memcpy XXX */
2187 data = ss->writebuf[ss->inwritebuf];
2188 /* Start with header of two bytes */
2189 /* Add "ALL STATIONS" and "UNNUMBERED" */
2190 data[0] = 0xff;
2191 data[1] = 0x03;
2192 ss->writen[ss->inwritebuf] = 2;
2193
2194 /* Copy real data and increment amount written */
2195 memcpy(data + 2, skb->data, skb->len);
2196
2197 ss->writen[ss->inwritebuf] += skb->len;
2198
2199 /* Re-set index back to zero */
2200 ss->writeidx[ss->inwritebuf] = 0;
2201
2202 /* Calculate the FCS */
2203 fcs = PPP_INITFCS;
2204 for (x=0;x<skb->len + 2;x++)
2205 fcs = PPP_FCS(fcs, data[x]);
2206 /* Invert it */
2207 fcs ^= 0xffff;
2208
2209 /* Point past the real data now */
2210 data += (skb->len + 2);
2211
2212 /* Send FCS out LSB first */
2213 data[0] = (fcs & 0xff);
2214 data[1] = (fcs >> 8) & 0xff;
2215
2216 /* Account for FCS length */
2217 ss->writen[ss->inwritebuf]+=2;
2218
2219 /* Advance to next window */
2220 oldbuf = ss->inwritebuf;
2221 ss->inwritebuf = (ss->inwritebuf + 1) % ss->numbufs;
2222
2223 if (ss->inwritebuf == ss->outwritebuf) {
2224 /* Whoops, no more space. */
2225 ss->inwritebuf = -1;
2226 }
2227 if (ss->outwritebuf < 0) {
2228 /* Let the interrupt handler know there's
2229 some space for us */
2230 ss->outwritebuf = oldbuf;
2231 }
2232 print_debug_writebuf(ss, skb, oldbuf);
2233 retval = 1;
2234 }
2235 spin_unlock_irqrestore(&ss->lock, flags);
2236 if (retval) {
2237 /* Get rid of the SKB if we're returning non-zero */
2238 /* N.B. this is called in process or BH context so
2239 dev_kfree_skb is OK. */
2240 dev_kfree_skb(skb);
2241 }
2242 return retval;
2243 }
2244
2245 static int dahdi_ppp_ioctl(struct ppp_channel *ppp, unsigned int cmd, unsigned long flags)
2246 {
2247 return -EIO;
2248 }
2249
2250 static struct ppp_channel_ops ztppp_ops =
2251 {
2252 .start_xmit = dahdi_ppp_xmit,
2253 .ioctl = dahdi_ppp_ioctl,
2254 };
2255
2256 #endif
2257
2258 /**
2259 * is_monitor_mode() - True if the confmode indicates that one channel is monitoring another.
2260 *
2261 */
2262 static bool is_monitor_mode(int confmode)
2263 {
2264 confmode &= DAHDI_CONF_MODE_MASK;
2265 if ((confmode == DAHDI_CONF_MONITOR) ||
2266 (confmode == DAHDI_CONF_MONITORTX) ||
2267 (confmode == DAHDI_CONF_MONITORBOTH) ||
2268 (confmode == DAHDI_CONF_MONITOR_RX_PREECHO) ||
2269 (confmode == DAHDI_CONF_MONITOR_TX_PREECHO) ||
2270 (confmode == DAHDI_CONF_MONITORBOTH_PREECHO)) {
2271 return true;
2272 } else {
2273 return false;
2274 }
2275 }
2276
2277 static unsigned long _chan_cleanup(struct dahdi_chan *pos, unsigned long data)
2278 {
2279 unsigned long flags;
2280 struct dahdi_chan *const chan = (struct dahdi_chan *)data;
2281 /* Remove anyone pointing to us as master
2282 and make them their own thing */
2283 if (pos->master == chan)
2284 pos->master = pos;
2285
2286 if (((pos->confna == chan->channo) &&
2287 is_monitor_mode(pos->confmode)) ||
2288 (pos->dacs_chan == chan) ||
2289 (pos->conf_chan == chan)) {
2290 /* Take them out of conference with us */
2291 /* release conference resource if any */
2292 if (pos->confna)
2293 dahdi_check_conf(pos->confna);
2294
2295 dahdi_disable_dacs(pos);
2296 spin_lock_irqsave(&pos->lock, flags);
2297 pos->confna = 0;
2298 pos->_confn = 0;
2299 pos->confmode = 0;
2300 pos->conf_chan = NULL;
2301 pos->dacs_chan = NULL;
2302 spin_unlock_irqrestore(&pos->lock, flags);
2303 }
2304
2305 return 0;
2306 }
2307
2308 static const struct file_operations nodev_fops;
2309
2310 static void dahdi_chan_unreg(struct dahdi_chan *chan)
2311 {
2312 unsigned long flags;
2313
2314 might_sleep();
2315
2316 /* In the case of surprise removal of hardware, make sure any open
2317 * file handles to this channel are disassociated with the actual
2318 * dahdi_chan. */
2319 if (chan->file) {
2320 module_printk(KERN_NOTICE,
2321 "%s: surprise removal: chan %d\n",
2322 __func__, chan->channo);
2323 chan->file->private_data = NULL;
2324 chan->file->f_op = &nodev_fops;
2325 /*
2326 * From now on, any file_operations for this device
2327 * would call the nodev_fops methods.
2328 */
2329 }
2330
2331 mutex_lock(&chan->mutex);
2332 release_echocan(chan->ec_factory);
2333 chan->ec_factory = NULL;
2334 mutex_unlock(&chan->mutex);
2335
2336 #ifdef CONFIG_DAHDI_NET
2337 if (dahdi_have_netdev(chan)) {
2338 unregister_hdlc_device(chan->hdlcnetdev->netdev);
2339 free_netdev(chan->hdlcnetdev->netdev);
2340 kfree(chan->hdlcnetdev);
2341 chan->hdlcnetdev = NULL;
2342 }
2343 #endif
2344 clear_bit(DAHDI_FLAGBIT_REGISTERED, &chan->flags);
2345
2346 #ifdef CONFIG_DAHDI_PPP
2347 if (chan->ppp) {
2348 module_printk(KERN_NOTICE, "HUH??? PPP still attached??\n");
2349 }
2350 #endif
2351 spin_lock_irqsave(&chan_lock, flags);
2352 __for_each_channel(_chan_cleanup, (unsigned long)chan);
2353 spin_unlock_irqrestore(&chan_lock, flags);
2354
2355 chan->channo = -1;
2356
2357 /* Let processeses out of their poll_wait() */
2358 wake_up_interruptible(&chan->waitq);
2359
2360 /* release tone_zone */
2361 close_channel(chan);
2362
2363 if (chan->file) {
2364 if (test_bit(DAHDI_FLAGBIT_OPEN, &chan->flags)) {
2365 clear_bit(DAHDI_FLAGBIT_OPEN, &chan->flags);
2366 if (chan->span) {
2367 if (chan->span->ops->close) {
2368 int res;
2369
2370 res = chan->span->ops->close(chan);
2371 if (res)
2372 module_printk(KERN_NOTICE,
2373 "%s: close() failed: %d\n",
2374 __func__, res);
2375 }
2376 }
2377 }
2378 msleep(20);
2379 /*
2380 * FIXME: THE BIG SLEEP above, is hiding a terrible
2381 * race condition:
2382 * - the module_put() ahead, would allow the low-level driver
2383 * to free the channel.
2384 * - We should make sure no-one reference this channel
2385 * from now on.
2386 */
2387 if (chan->span)
2388 put_span(chan->span);
2389 }
2390 }
2391
2392 static ssize_t dahdi_chan_read(struct file *file, char __user *usrbuf,
2393 size_t count, loff_t *ppos)
2394 {
2395 struct dahdi_chan *chan = file->private_data;
2396 int amnt;
2397 int res, rv;
2398 int oldbuf,x;
2399 unsigned long flags;
2400
2401 /* Make sure count never exceeds 65k, and make sure it's unsigned */
2402 count &= 0xffff;
2403
2404 if (unlikely(!chan)) {
2405 /*
2406 * This should never happen. Surprise device removal
2407 * should lead us to the nodev_* file_operations
2408 */
2409 msleep(5);
2410 module_printk(KERN_ERR, "%s: NODEV\n", __func__);
2411 return -ENODEV;
2412 }
2413
2414 if (unlikely(count < 1))
2415 return -EINVAL;
2416
2417 if (unlikely(!test_bit(DAHDI_FLAGBIT_REGISTERED, &chan->flags)))
2418 return -ENODEV;
2419
2420 for (;;) {
2421 spin_lock_irqsave(&chan->lock, flags);
2422 if (chan->eventinidx != chan->eventoutidx) {
2423 spin_unlock_irqrestore(&chan->lock, flags);
2424 return -ELAST /* - chan->eventbuf[chan->eventoutidx]*/;
2425 }
2426 res = chan->outreadbuf;
2427 spin_unlock_irqrestore(&chan->lock, flags);
2428 if (res >= 0)
2429 break;
2430 if (file->f_flags & O_NONBLOCK)
2431 return -EAGAIN;
2432
2433 /* Wake up when data is available or when the board driver
2434 * unregistered the channel. */
2435 rv = wait_event_interruptible(chan->waitq,
2436 (!chan->file->private_data || chan->outreadbuf > -1));
2437 if (rv)
2438 return rv;
2439 if (unlikely(!chan->file->private_data))
2440 return -ENODEV;
2441 }
2442 amnt = count;
2443 if (chan->flags & DAHDI_FLAG_LINEAR) {
2444 if (amnt > (chan->readn[res] << 1))
2445 amnt = chan->readn[res] << 1;
2446 if (amnt) {
2447 /* There seems to be a max stack size, so we have
2448 to do this in smaller pieces */
2449 short lindata[128];
2450 int left = amnt >> 1; /* amnt is in bytes */
2451 int pos = 0;
2452 int pass;
2453 while (left) {
2454 pass = left;
2455 if (pass > 128)
2456 pass = 128;
2457 for (x = 0; x < pass; x++)
2458 lindata[x] = DAHDI_XLAW(chan->readbuf[res][x + pos], chan);
2459 if (copy_to_user(usrbuf + (pos << 1), lindata, pass << 1))
2460 return -EFAULT;
2461 left -= pass;
2462 pos += pass;
2463 }
2464 }
2465 } else {
2466 if (amnt > chan->readn[res])
2467 amnt = chan->readn[res];
2468 if (amnt) {
2469 if (copy_to_user(usrbuf, chan->readbuf[res], amnt))
2470 return -EFAULT;
2471 }
2472 }
2473 spin_lock_irqsave(&chan->lock, flags);
2474 chan->readidx[res] = 0;
2475 chan->readn[res] = 0;
2476 oldbuf = res;
2477 chan->outreadbuf = (res + 1) % chan->numbufs;
2478 if (chan->outreadbuf == chan->inreadbuf) {
2479 /* Out of stuff */
2480 chan->outreadbuf = -1;
2481 }
2482 if (chan->inreadbuf < 0) {
2483 /* Notify interrupt handler that we have some space now */
2484 chan->inreadbuf = oldbuf;
2485 }
2486 spin_unlock_irqrestore(&chan->lock, flags);
2487
2488 return amnt;
2489 }
2490
2491 static int num_filled_bufs(struct dahdi_chan *chan)
2492 {
2493 int range1, range2;
2494
2495 if (chan->inwritebuf < 0) {
2496 return chan->numbufs;
2497 }
2498
2499 if (chan->outwritebuf < 0) {
2500 return 0;
2501 }
2502
2503 if (chan->outwritebuf <= chan->inwritebuf) {
2504 return chan->inwritebuf - chan->outwritebuf;
2505 }
2506
2507 /* This means (in > out) and we have wrap around */
2508 range1 = chan->numbufs - chan->outwritebuf;
2509 range2 = chan->inwritebuf;
2510
2511 return range1 + range2;
2512 }
2513
2514 static ssize_t dahdi_chan_write(struct file *file, const char __user *usrbuf,
2515 size_t count, loff_t *ppos)
2516 {
2517 unsigned long flags;
2518 struct dahdi_chan *chan = file->private_data;
2519 int res, amnt, oldbuf, rv, x;
2520
2521 /* Make sure count never exceeds 65k, and make sure it's unsigned */
2522 count &= 0xffff;
2523
2524 if (unlikely(!chan)) {
2525 /*
2526 * This should never happen. Surprise device removal
2527 * should lead us to the nodev_* file_operations
2528 */
2529 msleep(5);
2530 module_printk(KERN_ERR, "%s: NODEV\n", __func__);
2531 return -ENODEV;
2532 }
2533
2534 if (unlikely(count < 1))
2535 return -EINVAL;
2536
2537 if (unlikely(!test_bit(DAHDI_FLAGBIT_REGISTERED, &chan->flags)))
2538 return -ENODEV;
2539
2540 for (;;) {
2541 spin_lock_irqsave(&chan->lock, flags);
2542 if ((chan->curtone || chan->pdialcount) && !is_pseudo_chan(chan)) {
2543 chan->curtone = NULL;
2544 chan->tonep = 0;
2545 chan->dialing = 0;
2546 chan->txdialbuf[0] = '\0';
2547 chan->pdialcount = 0;
2548 }
2549 if (chan->eventinidx != chan->eventoutidx) {
2550 spin_unlock_irqrestore(&chan->lock, flags);
2551 return -ELAST;
2552 }
2553 res = chan->inwritebuf;
2554 spin_unlock_irqrestore(&chan->lock, flags);
2555 if (res >= 0)
2556 break;
2557 if (file->f_flags & O_NONBLOCK) {
2558 #ifdef BUFFER_DEBUG
2559 printk("Error: Nonblock\n");
2560 #endif
2561 return -EAGAIN;
2562 }
2563
2564 /* Wake up when room in the write queue is available or when
2565 * the board driver unregistered the channel. */
2566 rv = wait_event_interruptible(chan->waitq,
2567 (!chan->file->private_data || chan->inwritebuf > -1));
2568 if (rv)
2569 return rv;
2570 if (unlikely(!chan->file->private_data))
2571 return -ENODEV;
2572 }
2573
2574 amnt = count;
2575 if (chan->flags & DAHDI_FLAG_LINEAR) {
2576 if (amnt > (chan->blocksize << 1))
2577 amnt = chan->blocksize << 1;
2578 } else {
2579 if (amnt > chan->blocksize)
2580 amnt = chan->blocksize;
2581 }
2582
2583 #ifdef CONFIG_DAHDI_DEBUG
2584 module_printk(KERN_NOTICE, "dahdi_chan_write(chan: %d, res: %d, outwritebuf: %d amnt: %d\n",
2585 chan->channo, res, chan->outwritebuf, amnt);
2586 #endif
2587
2588 if (amnt) {
2589 if (chan->flags & DAHDI_FLAG_LINEAR) {
2590 /* There seems to be a max stack size, so we have
2591 to do this in smaller pieces */
2592 short lindata[128];
2593 int left = amnt >> 1; /* amnt is in bytes */
2594 int pos = 0;
2595 int pass;
2596 while (left) {
2597 pass = left;
2598 if (pass > 128)
2599 pass = 128;
2600 if (copy_from_user(lindata, usrbuf + (pos << 1), pass << 1)) {
2601 return -EFAULT;
2602 }
2603 left -= pass;
2604 for (x = 0; x < pass; x++)
2605 chan->writebuf[res][x + pos] = DAHDI_LIN2X(lindata[x], chan);
2606 pos += pass;
2607 }
2608 chan->writen[res] = amnt >> 1;
2609 } else {
2610 if (copy_from_user(chan->writebuf[res], usrbuf, amnt)) {
2611 return -EFAULT;
2612 }
2613 chan->writen[res] = amnt;
2614 }
2615 #ifdef CONFIG_DAHDI_ECHOCAN_PROCESS_TX
2616 if ((chan->ec_state) &&
2617 (ECHO_MODE_ACTIVE == chan->ec_state->status.mode) &&
2618 (chan->ec_state->ops->echocan_process_tx)) {
2619 struct dahdi_echocan_state *const ec = chan->ec_state;
2620 for (x = 0; x < chan->writen[res]; ++x) {
2621 short tx;
2622 tx = DAHDI_XLAW(chan->writebuf[res][x], chan);
2623 ec->ops->echocan_process_tx(ec, &tx, 1);
2624 chan->writebuf[res][x] = DAHDI_LIN2X((int) tx,
2625 chan);
2626 }
2627 }
2628 #endif
2629 chan->writeidx[res] = 0;
2630 if (chan->flags & DAHDI_FLAG_FCS)
2631 calc_fcs(chan, res);
2632 oldbuf = res;
2633 spin_lock_irqsave(&chan->lock, flags);
2634 chan->inwritebuf = (res + 1) % chan->numbufs;
2635
2636 if (chan->inwritebuf == chan->outwritebuf) {
2637 /* Don't stomp on the transmitter, just wait for them to
2638 wake us up */
2639 chan->inwritebuf = -1;
2640 /* Make sure the transmitter is transmitting in case of POLICY_WHEN_FULL */
2641 chan->txdisable = 0;
2642 }
2643
2644 if (chan->outwritebuf < 0) {
2645 /* Okay, the interrupt handler has been waiting for us. Give them a buffer */
2646 chan->outwritebuf = oldbuf;
2647 }
2648
2649 if ((chan->txbufpolicy == DAHDI_POLICY_HALF_FULL) && (chan->txdisable)) {
2650 if (num_filled_bufs(chan) >= (chan->numbufs >> 1)) {
2651 #ifdef BUFFER_DEBUG
2652 printk("Reached buffer fill mark of %d\n", num_filled_bufs(chan));
2653 #endif
2654 chan->txdisable = 0;
2655 }
2656 }
2657
2658 #ifdef BUFFER_DEBUG
2659 if ((chan->statcount <= 0) || (amnt != 128) || (num_filled_bufs(chan) != chan->lastnumbufs)) {
2660 printk("amnt: %d Number of filled buffers: %d\n", amnt, num_filled_bufs(chan));
2661 chan->statcount = 32000;
2662 chan->lastnumbufs = num_filled_bufs(chan);
2663 }
2664 #endif
2665
2666 spin_unlock_irqrestore(&chan->lock, flags);
2667
2668 if (chan->flags & DAHDI_FLAG_NOSTDTXRX && chan->span->ops->hdlc_hard_xmit)
2669 chan->span->ops->hdlc_hard_xmit(chan);
2670 }
2671 return amnt;
2672 }
2673
2674 static int dahdi_ctl_open(struct file *file)
2675 {
2676 /* Nothing to do, really */
2677 return 0;
2678 }
2679
2680 static int dahdi_chan_open(struct file *file)
2681 {
2682 /* Nothing to do here for now either */
2683 return 0;
2684 }
2685
2686 static int dahdi_ctl_release(struct file *file)
2687 {
2688 /* Nothing to do */
2689 return 0;
2690 }
2691
2692 static int dahdi_chan_release(struct file *file)
2693 {
2694 /* Nothing to do for now */
2695 return 0;
2696 }
2697
2698 static void set_txtone(struct dahdi_chan *ss, int fac, int init_v2, int init_v3)
2699 {
2700 if (fac == 0) {
2701 ss->v2_1 = 0;
2702 ss->v3_1 = 0;
2703 return;
2704 }
2705 ss->txtone = fac;
2706 ss->v1_1 = 0;
2707 ss->v2_1 = init_v2;
2708 ss->v3_1 = init_v3;
2709 return;
2710 }
2711
2712 static void dahdi_rbs_sethook(struct dahdi_chan *chan, int txsig, int txstate,
2713 int timeout)
2714 {
2715 static const struct {
2716 unsigned int sig_type;
2717 /* Index is dahdi_txsig enum */
2718 unsigned int bits[DAHDI_TXSIG_TOTAL];
2719 } outs[NUM_SIGS] = {
2720 {
2721 /*
2722 * We set the idle case of the DAHDI_SIG_NONE to this pattern to make idle E1 CAS
2723 * channels happy. Should not matter with T1, since on an un-configured channel,
2724 * who cares what the sig bits are as long as they are stable
2725 */
2726 .sig_type = DAHDI_SIG_NONE,
2727 .bits[DAHDI_TXSIG_ONHOOK] = DAHDI_BITS_ACD,
2728 }, {
2729 .sig_type = DAHDI_SIG_EM,
2730 .bits[DAHDI_TXSIG_OFFHOOK] = DAHDI_BITS_ABCD,
2731 .bits[DAHDI_TXSIG_START] = DAHDI_BITS_ABCD,
2732 }, {
2733 .sig_type = DAHDI_SIG_FXSLS,
2734 .bits[DAHDI_TXSIG_ONHOOK] = DAHDI_BITS_BD,
2735 .bits[DAHDI_TXSIG_OFFHOOK] = DAHDI_BITS_ABCD,
2736 .bits[DAHDI_TXSIG_START] = DAHDI_BITS_ABCD,
2737 }, {
2738 .sig_type = DAHDI_SIG_FXSGS,
2739 .bits[DAHDI_TXSIG_ONHOOK] = DAHDI_BITS_BD,
2740 .bits[DAHDI_TXSIG_OFFHOOK] = DAHDI_BITS_ABCD,
2741 #ifndef CONFIG_CAC_GROUNDSTART
2742 .bits[DAHDI_TXSIG_START] = DAHDI_BITS_AC,
2743 #endif
2744 }, {
2745 .sig_type = DAHDI_SIG_FXSKS,
2746 .bits[DAHDI_TXSIG_ONHOOK] = DAHDI_BITS_BD,
2747 .bits[DAHDI_TXSIG_OFFHOOK] = DAHDI_BITS_ABCD,
2748 .bits[DAHDI_TXSIG_START] = DAHDI_BITS_ABCD,
2749 }, {
2750 .sig_type = DAHDI_SIG_FXOLS,
2751 .bits[DAHDI_TXSIG_ONHOOK] = DAHDI_BITS_BD,
2752 .bits[DAHDI_TXSIG_OFFHOOK] = DAHDI_BITS_BD,
2753 }, {
2754 .sig_type = DAHDI_SIG_FXOGS,
2755 .bits[DAHDI_TXSIG_ONHOOK] = DAHDI_BITS_ABCD,
2756 .bits[DAHDI_TXSIG_OFFHOOK] = DAHDI_BITS_BD,
2757 }, {
2758 .sig_type = DAHDI_SIG_FXOKS,
2759 .bits[DAHDI_TXSIG_ONHOOK] = DAHDI_BITS_BD,
2760 .bits[DAHDI_TXSIG_OFFHOOK] = DAHDI_BITS_BD,
2761 .bits[DAHDI_TXSIG_KEWL] = DAHDI_BITS_ABCD,
2762 }, {
2763 .sig_type = DAHDI_SIG_SF,
2764 .bits[DAHDI_TXSIG_ONHOOK] = DAHDI_BITS_BCD,
2765 .bits[DAHDI_TXSIG_OFFHOOK] = DAHDI_BITS_ABCD,
2766 .bits[DAHDI_TXSIG_START] = DAHDI_BITS_ABCD,
2767 .bits[DAHDI_TXSIG_KEWL] = DAHDI_BITS_BCD,
2768 }, {
2769 .sig_type = DAHDI_SIG_EM_E1,
2770 .bits[DAHDI_TXSIG_ONHOOK] = DAHDI_DBIT,
2771 .bits[DAHDI_TXSIG_OFFHOOK] = DAHDI_BITS_ABD,
2772 .bits[DAHDI_TXSIG_START] = DAHDI_BITS_ABD,
2773 .bits[DAHDI_TXSIG_KEWL] = DAHDI_DBIT,
2774 }
2775 };
2776 int x;
2777
2778 /* if no span, return doing nothing */
2779 if (!chan->span)
2780 return;
2781
2782 if (!(chan->span->flags & DAHDI_FLAG_RBS)) {
2783 module_printk(KERN_NOTICE, "dahdi_rbs: Tried to set RBS hook state on non-RBS channel %s\n", chan->name);
2784 return;
2785 }
2786 if ((txsig > 3) || (txsig < 0)) {
2787 module_printk(KERN_NOTICE, "dahdi_rbs: Tried to set RBS hook state %d (> 3) on channel %s\n", txsig, chan->name);
2788 return;
2789 }
2790 if (!chan->span->ops->rbsbits && !chan->span->ops->hooksig) {
2791 module_printk(KERN_NOTICE, "dahdi_rbs: Tried to set RBS hook state %d on channel %s while span %s lacks rbsbits or hooksig function\n",
2792 txsig, chan->name, chan->span->name);
2793 return;
2794 }
2795 /* Don't do anything for RBS */
2796 if (chan->sig == DAHDI_SIG_DACS_RBS)
2797 return;
2798 chan->txstate = txstate;
2799
2800 /* if tone signalling */
2801 if (chan->sig == DAHDI_SIG_SF) {
2802 chan->txhooksig = txsig;
2803 if (chan->txtone) { /* if set to make tone for tx */
2804 if ((txsig && !(chan->toneflags & DAHDI_REVERSE_TXTONE)) ||
2805 ((!txsig) && (chan->toneflags & DAHDI_REVERSE_TXTONE))) {
2806 set_txtone(chan,chan->txtone,chan->tx_v2,chan->tx_v3);
2807 } else {
2808 set_txtone(chan,0,0,0);
2809 }
2810 }
2811 chan->otimer = timeout * DAHDI_CHUNKSIZE; /* Otimer is timer in samples */
2812 return;
2813 }
2814 if (chan->span->ops->hooksig) {
2815 if (chan->txhooksig != txsig) {
2816 chan->txhooksig = txsig;
2817 chan->span->ops->hooksig(chan, txsig);
2818 }
2819 chan->otimer = timeout * DAHDI_CHUNKSIZE; /* Otimer is timer in samples */
2820 return;
2821 } else {
2822 for (x = 0; x < NUM_SIGS; x++) {
2823 if (outs[x].sig_type == chan->sig) {
2824 #ifdef CONFIG_DAHDI_DEBUG
2825 module_printk(KERN_NOTICE, "Setting bits to %d for channel %s state %d in %d signalling\n", outs[x].bits[txsig], chan->name, txsig, chan->sig);
2826 #endif
2827 chan->txhooksig = txsig;
2828 chan->txsig = outs[x].bits[txsig];
2829 chan->span->ops->rbsbits(chan, chan->txsig);
2830 chan->otimer = timeout * DAHDI_CHUNKSIZE; /* Otimer is timer in samples */
2831 return;
2832 }
2833 }
2834 }
2835 module_printk(KERN_NOTICE, "dahdi_rbs: Don't know RBS signalling type %d on channel %s\n", chan->sig, chan->name);
2836 }
2837
2838 static int dahdi_cas_setbits(struct dahdi_chan *chan, int bits)
2839 {
2840 /* if no span, return as error */
2841 if (!chan->span)
2842 return -1;
2843 if (chan->span->ops->rbsbits) {
2844 chan->txsig = bits;
2845 chan->span->ops->rbsbits(chan, bits);
2846 } else {
2847 module_printk(KERN_NOTICE, "Huh? CAS setbits, but no RBS bits function\n");
2848 }
2849
2850 return 0;
2851 }
2852
2853 static int dahdi_hangup(struct dahdi_chan *chan)
2854 {
2855 int x, res = 0;
2856
2857 /* Can't hangup pseudo channels */
2858 if (!chan->span)
2859 return 0;
2860
2861 /* Can't hang up a clear channel */
2862 if (chan->flags & (DAHDI_FLAG_CLEAR | DAHDI_FLAG_NOSTDTXRX))
2863 return -EINVAL;
2864
2865 chan->kewlonhook = 0;
2866
2867 if ((chan->sig == DAHDI_SIG_FXSLS) || (chan->sig == DAHDI_SIG_FXSKS) ||
2868 (chan->sig == DAHDI_SIG_FXSGS)) {
2869 chan->ringdebtimer = RING_DEBOUNCE_TIME;
2870 }
2871
2872 if (chan->span->flags & DAHDI_FLAG_RBS) {
2873 if (chan->sig == DAHDI_SIG_CAS) {
2874 dahdi_cas_setbits(chan, chan->idlebits);
2875 } else if ((chan->sig == DAHDI_SIG_FXOKS) && (chan->txstate != DAHDI_TXSTATE_ONHOOK)
2876 /* if other party is already on-hook we shouldn't do any battery drop */
2877 && !((chan->rxhooksig == DAHDI_RXSIG_ONHOOK) && (chan->itimer <= 0))) {
2878 /* Do RBS signalling on the channel's behalf */
2879 dahdi_rbs_sethook(chan, DAHDI_TXSIG_KEWL, DAHDI_TXSTATE_KEWL, DAHDI_KEWLTIME);
2880 } else
2881 dahdi_rbs_sethook(chan, DAHDI_TXSIG_ONHOOK, DAHDI_TXSTATE_ONHOOK, 0);
2882 } else {
2883 /* Let the driver hang up the line if it wants to */
2884 if (chan->span->ops->sethook) {
2885 if (chan->txhooksig != DAHDI_ONHOOK) {
2886 chan->txhooksig = DAHDI_ONHOOK;
2887 res = chan->span->ops->sethook(chan, DAHDI_ONHOOK);
2888 } else
2889 res = 0;
2890 }
2891 }
2892
2893 /* if not registered yet, just return here */
2894 if (!test_bit(DAHDI_FLAGBIT_REGISTERED, &chan->flags))
2895 return res;
2896
2897 /* Mark all buffers as empty */
2898 for (x = 0; x < chan->numbufs; x++) {
2899 chan->writen[x] =
2900 chan->writeidx[x]=
2901 chan->readn[x]=
2902 chan->readidx[x] = 0;
2903 }
2904
2905 if (chan->readbuf[0]) {
2906 chan->inreadbuf = 0;
2907 chan->inwritebuf = 0;
2908 } else {
2909 chan->inreadbuf = -1;
2910 chan->inwritebuf = -1;
2911 }
2912 chan->outreadbuf = -1;
2913 chan->outwritebuf = -1;
2914 chan->dialing = 0;
2915 chan->afterdialingtimer = 0;
2916 chan->curtone = NULL;
2917 chan->pdialcount = 0;
2918 chan->cadencepos = 0;
2919 chan->txdialbuf[0] = 0;
2920
2921 return res;
2922 }
2923
2924 static int initialize_channel(struct dahdi_chan *chan)
2925 {
2926 int res;
2927 unsigned long flags;
2928 const void *rxgain = NULL;
2929 struct dahdi_echocan_state *ec_state;
2930 const struct dahdi_echocan_factory *ec_current;
2931
2932 if ((res = dahdi_reallocbufs(chan, DAHDI_DEFAULT_BLOCKSIZE, DAHDI_DEFAULT_NUM_BUFS)))
2933 return res;
2934
2935 spin_lock_irqsave(&chan->lock, flags);
2936
2937 chan->txbufpolicy = DAHDI_POLICY_IMMEDIATE;
2938
2939 ec_state = chan->ec_state;
2940 chan->ec_state = NULL;
2941 ec_current = chan->ec_current;
2942 chan->ec_current = NULL;
2943
2944 chan->txdisable = 0;
2945
2946 chan->digitmode = DIGIT_MODE_DTMF;
2947 chan->dialing = 0;
2948 chan->afterdialingtimer = 0;
2949
2950 chan->cadencepos = 0;
2951 chan->firstcadencepos = 0; /* By default loop back to first cadence position */
2952
2953 /* HDLC & FCS stuff */
2954 fasthdlc_init(&chan->rxhdlc, (chan->flags & DAHDI_FLAG_HDLC56) ? FASTHDLC_MODE_56 : FASTHDLC_MODE_64);
2955 fasthdlc_init(&chan->txhdlc, (chan->flags & DAHDI_FLAG_HDLC56) ? FASTHDLC_MODE_56 : FASTHDLC_MODE_64);
2956 chan->infcs = PPP_INITFCS;
2957
2958 /* Timings for RBS */
2959 chan->prewinktime = DAHDI_DEFAULT_PREWINKTIME;
2960 chan->preflashtime = DAHDI_DEFAULT_PREFLASHTIME;
2961 chan->winktime = DAHDI_DEFAULT_WINKTIME;
2962 chan->flashtime = DAHDI_DEFAULT_FLASHTIME;
2963
2964 if (chan->sig & __DAHDI_SIG_FXO)
2965 chan->starttime = DAHDI_DEFAULT_RINGTIME;
2966 else
2967 chan->starttime = DAHDI_DEFAULT_STARTTIME;
2968 chan->rxwinktime = DAHDI_DEFAULT_RXWINKTIME;
2969 chan->rxflashtime = DAHDI_DEFAULT_RXFLASHTIME;
2970 chan->debouncetime = DAHDI_DEFAULT_DEBOUNCETIME;
2971 chan->pulsemaketime = DAHDI_DEFAULT_PULSEMAKETIME;
2972 chan->pulsebreaktime = DAHDI_DEFAULT_PULSEBREAKTIME;
2973 chan->pulseaftertime = DAHDI_DEFAULT_PULSEAFTERTIME;
2974
2975 /* Initialize RBS timers */
2976 chan->itimerset = chan->itimer = chan->otimer = 0;
2977 chan->ringdebtimer = 0;
2978
2979 /* Reset conferences */
2980 reset_conf(chan);
2981
2982 chan->dacs_chan = NULL;
2983
2984 /* I/O Mask, etc */
2985 chan->iomask = 0;
2986 /* release conference resource if any */
2987 if (chan->confna)
2988 dahdi_check_conf(chan->confna);
2989 if ((chan->sig & __DAHDI_SIG_DACS) != __DAHDI_SIG_DACS) {
2990 chan->confna = 0;
2991 chan->confmode = 0;
2992 chan->conf_chan = NULL;
2993 dahdi_disable_dacs(chan);
2994 }
2995 chan->_confn = 0;
2996 memset(chan->conflast, 0, sizeof(chan->conflast));
2997 memset(chan->conflast1, 0, sizeof(chan->conflast1));
2998 memset(chan->conflast2, 0, sizeof(chan->conflast2));
2999 chan->confmute = 0;
3000 chan->gotgs = 0;
3001 chan->curtone = NULL;
3002 chan->tonep = 0;
3003 chan->pdialcount = 0;
3004 if (is_gain_allocated(chan))
3005 rxgain = chan->rxgain;
3006 chan->rxgain = defgain;
3007 chan->txgain = defgain;
3008 chan->eventinidx = chan->eventoutidx = 0;
3009 dahdi_set_law(chan, DAHDI_LAW_DEFAULT);
3010 dahdi_hangup(chan);
3011
3012 /* Make sure that the audio flag is cleared on a clear channel */
3013 if ((chan->sig & DAHDI_SIG_CLEAR) || (chan->sig & DAHDI_SIG_HARDHDLC))
3014 chan->flags &= ~DAHDI_FLAG_AUDIO;
3015
3016 if ((chan->sig == DAHDI_SIG_CLEAR) || (chan->sig == DAHDI_SIG_HARDHDLC))
3017 chan->flags &= ~(DAHDI_FLAG_PPP | DAHDI_FLAG_FCS | DAHDI_FLAG_HDLC);
3018
3019 chan->flags &= ~DAHDI_FLAG_LINEAR;
3020 if (chan->curzone) {
3021 /* Take cadence from tone zone */
3022 memcpy(chan->ringcadence, chan->curzone->ringcadence, sizeof(chan->ringcadence));
3023 } else {
3024 /* Do a default */
3025 memset(chan->ringcadence, 0, sizeof(chan->ringcadence));
3026 chan->ringcadence[0] = chan->starttime;
3027 chan->ringcadence[1] = DAHDI_RINGOFFTIME;
3028 }
3029
3030 if (ec_state) {
3031 ec_state->ops->echocan_free(chan, ec_state);
3032 release_echocan(ec_current);
3033 }
3034
3035 spin_unlock_irqrestore(&chan->lock, flags);
3036
3037 set_tone_zone(chan, DEFAULT_TONE_ZONE);
3038
3039 if (rxgain)
3040 kfree(rxgain);
3041
3042 return 0;
3043 }
3044
3045 static const struct file_operations dahdi_timer_fops;
3046
3047 static int dahdi_timer_open(struct file *file)
3048 {
3049 struct dahdi_timer *t = kzalloc(sizeof(*t), GFP_KERNEL);
3050 if (!t)
3051 return -ENOMEM;
3052 INIT_LIST_HEAD(&t->list);
3053 init_waitqueue_head(&t->sel);
3054 file->private_data = t;
3055 spin_lock_init(&t->lock);
3056 file->f_op = &dahdi_timer_fops;
3057
3058 return 0;
3059 }
3060
3061 static int dahdi_timer_release(struct inode *inode, struct file *file)
3062 {
3063 struct dahdi_timer *timer = file->private_data;
3064 unsigned long flags;
3065
3066 if (!timer)
3067 return 0;
3068
3069 spin_lock_irqsave(&timer->lock, flags);
3070 if (!list_empty(&timer->list)) {
3071 spin_unlock(&timer->lock);
3072 spin_lock(&dahdi_timer_lock);
3073 spin_lock(&timer->lock);
3074 list_del_init(&timer->list);
3075 spin_unlock(&dahdi_timer_lock);
3076 }
3077 file->private_data = NULL;
3078 spin_unlock_irqrestore(&timer->lock, flags);
3079
3080 kfree(timer);
3081
3082 return 0;
3083 }
3084
3085 static const struct file_operations dahdi_chan_fops;
3086
3087 static int dahdi_specchan_open(struct file *file)
3088 {
3089 int res = -ENXIO;
3090 struct dahdi_chan *const chan = chan_from_file(file);
3091
3092 if (!chan || !chan->sig)
3093 return -ENXIO;
3094
3095 /* Make sure we're not already open, a net device, or a slave
3096 * device */
3097 if (dahdi_have_netdev(chan))
3098 res = -EBUSY;
3099 else if (chan->master != chan)
3100 res = -EBUSY;
3101 else if ((chan->sig & __DAHDI_SIG_DACS) == __DAHDI_SIG_DACS)
3102 res = -EBUSY;
3103 else if (!test_and_set_bit(DAHDI_FLAGBIT_OPEN, &chan->flags)) {
3104 unsigned long flags;
3105 const struct dahdi_span_ops *const ops =
3106 (!is_pseudo_chan(chan)) ? chan->span->ops : NULL;
3107
3108 if (ops && !try_module_get(ops->owner)) {
3109 clear_bit(DAHDI_FLAGBIT_OPEN, &chan->flags);
3110 return -ENXIO;
3111 }
3112
3113 res = initialize_channel(chan);
3114 if (res) {
3115 /* Reallocbufs must have failed */
3116 clear_bit(DAHDI_FLAGBIT_OPEN, &chan->flags);
3117 return res;
3118 }
3119
3120 spin_lock_irqsave(&chan->lock, flags);
3121 if (is_pseudo_chan(chan))
3122 chan->flags |= DAHDI_FLAG_AUDIO;
3123 chan->file = file;
3124 file->private_data = chan;
3125 /* Since we know we're a channel now, we can
3126 * update the f_op pointer and bypass a few of
3127 * the checks on the minor number. */
3128 file->f_op = &dahdi_chan_fops;
3129 spin_unlock_irqrestore(&chan->lock, flags);
3130
3131 if (ops && ops->open) {
3132 res = ops->open(chan);
3133 if (res) {
3134 spin_lock_irqsave(&chan->lock, flags);
3135 chan->file = NULL;
3136 file->private_data = NULL;
3137 spin_unlock_irqrestore(&chan->lock, flags);
3138 module_put(ops->owner);
3139 close_channel(chan);
3140 clear_bit(DAHDI_FLAGBIT_OPEN,
3141 &chan->flags);
3142 }
3143 }
3144 } else {
3145 res = -EBUSY;
3146 }
3147 return res;
3148 }
3149
3150 static int dahdi_specchan_release(struct file *file)
3151 {
3152 int res=0;
3153 unsigned long flags;
3154 struct dahdi_chan *chan = chan_from_file(file);
3155
3156 if (chan) {
3157 /* Chan lock protects contents against potentially non atomic accesses.
3158 * So if the pointer setting is not atomic, we should protect */
3159 #ifdef CONFIG_DAHDI_MIRROR
3160 if (chan->srcmirror) {
3161 struct dahdi_chan *const srcmirror = chan->srcmirror;
3162 spin_lock_irqsave(&srcmirror->lock, flags);
3163 if (chan == srcmirror->txmirror) {
3164 module_printk(KERN_INFO, "Chan %d tx mirror " \
3165 "to %d stopped\n",
3166 srcmirror->txmirror->channo,
3167 srcmirror->channo);
3168 srcmirror->txmirror = NULL;
3169 }
3170
3171 if (chan == srcmirror->rxmirror) {
3172 module_printk(KERN_INFO, "Chan %d rx mirror " \
3173 "to %d stopped\n",
3174 srcmirror->rxmirror->channo,
3175 srcmirror->channo);
3176 chan->srcmirror->rxmirror = NULL;
3177 }
3178 spin_unlock_irqrestore(&chan->srcmirror->lock, flags);
3179 }
3180 #endif /* CONFIG_DAHDI_MIRROR */
3181
3182 spin_lock_irqsave(&chan->lock, flags);
3183 chan->file = NULL;
3184 file->private_data = NULL;
3185 #ifdef CONFIG_DAHDI_MIRROR
3186 chan->srcmirror = NULL;
3187 #endif /* CONFIG_DAHDI_MIRROR */
3188
3189 spin_unlock_irqrestore(&chan->lock, flags);
3190 close_channel(chan);
3191 clear_bit(DAHDI_FLAGBIT_OPEN, &chan->flags);
3192 if (chan->span) {
3193 struct module *owner = chan->span->ops->owner;
3194
3195 if (chan->span->ops->close)
3196 res = chan->span->ops->close(chan);
3197 module_put(owner);
3198 }
3199 } else
3200 res = -ENXIO;
3201 return res;
3202 }
3203
3204 static int can_open_timer(void)
3205 {
3206 #ifdef CONFIG_DAHDI_CORE_TIMER
3207 return 1;
3208 #else
3209 return (list_empty(&span_list)) ? 0 : 1;
3210 #endif
3211 }
3212
3213 static unsigned int max_pseudo_channels = 512;
3214 static unsigned int num_pseudo_channels;
3215
3216 /**
3217 * dahdi_alloc_pseudo() - Returns a new pseudo channel.
3218 *
3219 * Call with the registration_mutex held since this function will determine a
3220 * channel number, and must be protected from additional registrations while
3221 * that is happening.
3222 *
3223 */
3224 static struct dahdi_chan *dahdi_alloc_pseudo(struct file *file)
3225 {
3226 struct pseudo_chan *pseudo;
3227 unsigned long flags;
3228 unsigned int channo;
3229 struct pseudo_chan *p;
3230 struct list_head *pos = &pseudo_chans;
3231
3232 /* Don't allow /dev/dahdi/pseudo to open if there is not a timing
3233 * source. */
3234 if (!can_open_timer())
3235 return NULL;
3236
3237 if (unlikely(num_pseudo_channels >= max_pseudo_channels))
3238 return NULL;
3239
3240 pseudo = kzalloc(sizeof(*pseudo), GFP_KERNEL);
3241 if (NULL == pseudo)
3242 return NULL;
3243
3244 pseudo->chan.sig = DAHDI_SIG_CLEAR;
3245 pseudo->chan.sigcap = DAHDI_SIG_CLEAR;
3246 pseudo->chan.flags = DAHDI_FLAG_AUDIO;
3247 pseudo->chan.span = NULL; /* No span == psuedo channel */
3248
3249 channo = FIRST_PSEUDO_CHANNEL;
3250 list_for_each_entry(p, &pseudo_chans, node) {
3251 if (channo != p->chan.channo)
3252 break;
3253 pos = &p->node;
3254 ++channo;
3255 }
3256
3257 pseudo->chan.channo = channo;
3258 pseudo->chan.chanpos = channo - FIRST_PSEUDO_CHANNEL + 1;
3259 __dahdi_init_chan(&pseudo->chan);
3260 dahdi_chan_reg(&pseudo->chan);
3261
3262 snprintf(pseudo->chan.name, sizeof(pseudo->chan.name)-1,
3263 "Pseudo/%d", pseudo->chan.chanpos);
3264
3265 file->private_data = &pseudo->chan;
3266
3267 /* Once we place the pseudo chan on the list...it's registered and
3268 * live. */
3269 spin_lock_irqsave(&chan_lock, flags);
3270 ++num_pseudo_channels;
3271 list_add(&pseudo->node, pos);
3272 spin_unlock_irqrestore(&chan_lock, flags);
3273
3274 return &pseudo->chan;
3275 }
3276
3277 static void dahdi_free_pseudo(struct dahdi_chan *chan)
3278 {
3279 struct pseudo_chan *pseudo;
3280 unsigned long flags;
3281
3282 if (!chan)
3283 return;
3284
3285 mutex_lock(&registration_mutex);
3286 pseudo = chan_to_pseudo(chan);
3287
3288 spin_lock_irqsave(&chan_lock, flags);
3289 list_del(&pseudo->node);
3290 --num_pseudo_channels;
3291 spin_unlock_irqrestore(&chan_lock, flags);
3292
3293 dahdi_chan_unreg(chan);
3294 mutex_unlock(&registration_mutex);
3295 kfree(pseudo);
3296 }
3297
3298 static int dahdi_open(struct inode *inode, struct file *file)
3299 {
3300 int unit = UNIT(file);
3301 struct dahdi_chan *chan;
3302 /* Minor 0: Special "control" descriptor */
3303 if (unit == DAHDI_CTL)
3304 return dahdi_ctl_open(file);
3305 if (unit == DAHDI_TRANSCODE) {
3306 if (!dahdi_transcode_fops) {
3307 if (request_module("dahdi_transcode")) {
3308 return -ENXIO;
3309 }
3310 }
3311 if (!try_module_get(dahdi_transcode_fops->owner)) {
3312 return -ENXIO;
3313 }
3314 if (dahdi_transcode_fops && dahdi_transcode_fops->open) {
3315 return dahdi_transcode_fops->open(inode, file);
3316 } else {
3317 /* dahdi_transcode module should have exported a
3318 * file_operations table. */
3319 WARN_ON(1);
3320 }
3321 return -ENXIO;
3322 }
3323 if (unit == DAHDI_TIMER) {
3324 if (can_open_timer()) {
3325 return dahdi_timer_open(file);
3326 } else {
3327 return -ENXIO;
3328 }
3329 }
3330 if (unit == DAHDI_CHANNEL)
3331 return dahdi_chan_open(file);
3332 if (unit == DAHDI_PSEUDO) {
3333 mutex_lock(&registration_mutex);
3334 chan = dahdi_alloc_pseudo(file);
3335 mutex_unlock(&registration_mutex);
3336 if (unlikely(!chan))
3337 return -ENOMEM;
3338 return dahdi_specchan_open(file);
3339 }
3340 return dahdi_specchan_open(file);
3341 }
3342
3343 /**
3344 * dahdi_ioctl_defaultzone() - Set defzone to the default.
3345 * @defzone: The number of the default zone.
3346 *
3347 * The default zone is the zone that will be used if the channels request the
3348 * default zone in dahdi_ioctl_chanconfig. The first entry on the tone_zones
3349 * list is the default zone. This function searches the list for the zone,
3350 * and if found, moves it to the head of the list.
3351 */
3352 static int dahdi_ioctl_defaultzone(unsigned long data)
3353 {
3354 int defzone;
3355 struct dahdi_zone *cur;
3356 struct dahdi_zone *dz = NULL;
3357
3358 if (get_user(defzone, (int __user *)data))
3359 return -EFAULT;
3360
3361 spin_lock(&zone_lock);
3362 list_for_each_entry(cur, &tone_zones, node) {
3363 if (cur->num != defzone)
3364 continue;
3365 dz = cur;
3366 break;
3367 }
3368 if (dz)
3369 list_move(&dz->node, &tone_zones);
3370 spin_unlock(&zone_lock);
3371
3372 return (dz) ? 0 : -EINVAL;
3373 }
3374
3375 /* No bigger than 32k for everything per tone zone */
3376 #define MAX_SIZE 32768
3377 /* No more than 128 subtones */
3378 #define MAX_TONES 128
3379
3380 /* The tones to be loaded can (will) be a mix of regular tones,
3381 DTMF tones and MF tones. We need to load DTMF and MF tones
3382 a bit differently than regular tones because their storage
3383 format is much simpler (an array structure field of the zone
3384 structure, rather an array of pointers).
3385 */
3386 static int dahdi_ioctl_loadzone(unsigned long data)
3387 {
3388 struct load_zone_workarea {
3389 struct dahdi_tone *samples[MAX_TONES];
3390 short next[MAX_TONES];
3391 struct dahdi_tone_def_header th;
3392 struct dahdi_tone_def td;
3393 } *work;
3394
3395 size_t space;
3396 size_t size;
3397 int res;
3398 int x;
3399 void *ptr;
3400 struct dahdi_zone *z = NULL;
3401 struct dahdi_tone *t = NULL;
3402 void __user * user_data = (void __user *)data;
3403 const unsigned char MAX_ZONE = -1;
3404
3405 work = kzalloc(sizeof(*work), GFP_KERNEL);
3406 if (!work)
3407 return -ENOMEM;
3408
3409 if (copy_from_user(&work->th, user_data, sizeof(work->th))) {
3410 res = -EFAULT;
3411 goto error_exit;
3412 }
3413
3414 if ((work->th.zone < 0) || (work->th.zone > MAX_ZONE)) {
3415 res = -EINVAL;
3416 goto error_exit;
3417 }
3418
3419 user_data += sizeof(work->th);
3420
3421 if ((work->th.count < 0) || (work->th.count > MAX_TONES)) {
3422 module_printk(KERN_NOTICE, "Too many tones included\n");
3423 res = -EINVAL;
3424 goto error_exit;
3425 }
3426
3427 space = size = sizeof(*z) + work->th.count * sizeof(*t);
3428
3429 if (size > MAX_SIZE) {
3430 res = -E2BIG;
3431 goto error_exit;
3432 }
3433
3434 z = ptr = kzalloc(size, GFP_KERNEL);
3435 if (!z) {
3436 res = -ENOMEM;
3437 goto error_exit;
3438 }
3439
3440 ptr = (char *) ptr + sizeof(*z);
3441 space -= sizeof(*z);
3442
3443 z->name = kasprintf(GFP_KERNEL, work->th.name);
3444 if (!z->name) {
3445 res = -ENOMEM;
3446 goto error_exit;
3447 }
3448
3449 for (x = 0; x < DAHDI_MAX_CADENCE; x++)
3450 z->ringcadence[x] = work->th.ringcadence[x];
3451
3452 mutex_lock(&global_dialparamslock);
3453 for (x = 0; x < work->th.count; x++) {
3454 enum {
3455 REGULAR_TONE,
3456 DTMF_TONE,
3457 MFR1_TONE,
3458 MFR2_FWD_TONE,
3459 MFR2_REV_TONE,
3460 } tone_type;
3461
3462 if (space < sizeof(*t)) {
3463 module_printk(KERN_NOTICE, "Insufficient tone zone space\n");
3464 res = -EINVAL;
3465 goto unlock_error_exit;
3466 }
3467
3468 res = copy_from_user(&work->td, user_data,
3469 sizeof(work->td));
3470 if (res) {
3471 res = -EFAULT;
3472 goto unlock_error_exit;
3473 }
3474
3475 user_data += sizeof(work->td);
3476
3477 if ((work->td.tone >= 0) && (work->td.tone < DAHDI_TONE_MAX)) {
3478 tone_type = REGULAR_TONE;
3479
3480 t = work->samples[x] = ptr;
3481
3482 space -= sizeof(*t);
3483 ptr = (char *) ptr + sizeof(*t);
3484
3485 /* Remember which sample is work->next */
3486 work->next[x] = work->td.next;
3487
3488 /* Make sure the "next" one is sane */
3489 if ((work->next[x] >= work->th.count) ||
3490 (work->next[x] < 0)) {
3491 module_printk(KERN_NOTICE,
3492 "Invalid 'next' pointer: %d\n",
3493 work->next[x]);
3494 res = -EINVAL;
3495 goto unlock_error_exit;
3496 }
3497 } else if ((work->td.tone >= DAHDI_TONE_DTMF_BASE) &&
3498 (work->td.tone <= DAHDI_TONE_DTMF_MAX)) {
3499 tone_type = DTMF_TONE;
3500 work->td.tone -= DAHDI_TONE_DTMF_BASE;
3501 t = &z->dtmf[work->td.tone];
3502 } else if ((work->td.tone >= DAHDI_TONE_MFR1_BASE) &&
3503 (work->td.tone <= DAHDI_TONE_MFR1_MAX)) {
3504 tone_type = MFR1_TONE;
3505 work->td.tone -= DAHDI_TONE_MFR1_BASE;
3506 t = &z->mfr1[work->td.tone];
3507 } else if ((work->td.tone >= DAHDI_TONE_MFR2_FWD_BASE) &&
3508 (work->td.tone <= DAHDI_TONE_MFR2_FWD_MAX)) {
3509 tone_type = MFR2_FWD_TONE;
3510 work->td.tone -= DAHDI_TONE_MFR2_FWD_BASE;
3511 t = &z->mfr2_fwd[work->td.tone];
3512 } else if ((work->td.tone >= DAHDI_TONE_MFR2_REV_BASE) &&
3513 (work->td.tone <= DAHDI_TONE_MFR2_REV_MAX)) {
3514 tone_type = MFR2_REV_TONE;
3515 work->td.tone -= DAHDI_TONE_MFR2_REV_BASE;
3516 t = &z->mfr2_rev[work->td.tone];
3517 } else {
3518 module_printk(KERN_NOTICE,
3519 "Invalid tone (%d) defined\n",
3520 work->td.tone);
3521 res = -EINVAL;
3522 goto unlock_error_exit;
3523 }
3524
3525 t->fac1 = work->td.fac1;
3526 t->init_v2_1 = work->td.init_v2_1;
3527 t->init_v3_1 = work->td.init_v3_1;
3528 t->fac2 = work->td.fac2;
3529 t->init_v2_2 = work->td.init_v2_2;
3530 t->init_v3_2 = work->td.init_v3_2;
3531 t->modulate = work->td.modulate;
3532
3533 switch (tone_type) {
3534 case REGULAR_TONE:
3535 t->tonesamples = work->td.samples;
3536 if (!z->tones[work->td.tone])
3537 z->tones[work->td.tone] = t;
3538 break;
3539 case DTMF_TONE:
3540 t->tonesamples = global_dialparams.dtmf_tonelen;
3541 t->next = &dtmf_silence;
3542 z->dtmf_continuous[work->td.tone] = *t;
3543 z->dtmf_continuous[work->td.tone].next =
3544 &z->dtmf_continuous[work->td.tone];
3545 break;
3546 case MFR1_TONE:
3547 switch (work->td.tone + DAHDI_TONE_MFR1_BASE) {
3548 case DAHDI_TONE_MFR1_KP:
3549 case DAHDI_TONE_MFR1_ST:
3550 case DAHDI_TONE_MFR1_STP:
3551 case DAHDI_TONE_MFR1_ST2P:
3552 case DAHDI_TONE_MFR1_ST3P:
3553 /* signaling control tones are always 100ms */
3554 t->tonesamples = 100 * DAHDI_CHUNKSIZE;
3555 break;
3556 default:
3557 t->tonesamples = global_dialparams.mfv1_tonelen;
3558 break;
3559 }
3560 t->next = &mfr1_silence;
3561 break;
3562 case MFR2_FWD_TONE:
3563 t->tonesamples = global_dialparams.mfr2_tonelen;
3564 t->next = &dtmf_silence;
3565 z->mfr2_fwd_continuous[work->td.tone] = *t;
3566 z->mfr2_fwd_continuous[work->td.tone].next =
3567 &z->mfr2_fwd_continuous[work->td.tone];
3568 break;
3569 case MFR2_REV_TONE:
3570 t->tonesamples = global_dialparams.mfr2_tonelen;
3571 t->next = &dtmf_silence;
3572 z->mfr2_rev_continuous[work->td.tone] = *t;
3573 z->mfr2_rev_continuous[work->td.tone].next =
3574 &z->mfr2_rev_continuous[work->td.tone];
3575 break;
3576 }
3577 }
3578 mutex_unlock(&global_dialparamslock);
3579
3580 for (x = 0; x < work->th.count; x++) {
3581 if (work->samples[x])
3582 work->samples[x]->next = work->samples[work->next[x]];
3583 }
3584
3585 z->num = work->th.zone;
3586
3587 /* After we call dahdi_register_tone_zone, the only safe way to free
3588 * the zone is with a tone_zone_put call. */
3589 res = dahdi_register_tone_zone(z);
3590 if (res)
3591 tone_zone_put(z);
3592
3593 kfree(work);
3594 return res;
3595
3596 unlock_error_exit:
3597 mutex_unlock(&global_dialparamslock);
3598 error_exit:
3599 if (z)
3600 kfree(z->name);
3601 kfree(z);
3602 kfree(work);
3603 return res;
3604 }
3605
3606 void dahdi_init_tone_state(struct dahdi_tone_state *ts, struct dahdi_tone *zt)
3607 {
3608 ts->v1_1 = 0;
3609 ts->v2_1 = zt->init_v2_1;
3610 ts->v3_1 = zt->init_v3_1;
3611 ts->v1_2 = 0;
3612 ts->v2_2 = zt->init_v2_2;
3613 ts->v3_2 = zt->init_v3_2;
3614 ts->modulate = zt->modulate;
3615 }
3616
3617 struct dahdi_tone *dahdi_mf_tone(const struct dahdi_chan *chan, char digit, int digitmode)
3618 {
3619 unsigned int tone_index;
3620
3621 if (!chan->curzone) {
3622 static int __warnonce = 1;
3623 if (__warnonce) {
3624 __warnonce = 0;
3625 /* The tonezones are loaded by dahdi_cfg based on /etc/dahdi/system.conf. */
3626 module_printk(KERN_WARNING, "Cannot get dtmf tone until tone zone is loaded.\n");
3627 }
3628 return NULL;
3629 }
3630
3631 switch (digitmode) {
3632 case DIGIT_MODE_PULSE:
3633 /* We should only get here with a pulse digit if we need
3634 * to "dial" 'W' (wait 0.5 second)
3635 */
3636 if (digit == 'W')
3637 return &tone_pause;
3638
3639 return NULL;
3640 case DIGIT_MODE_DTMF:
3641 switch (digit) {
3642 case '0':
3643 case '1':
3644 case '2':
3645 case '3':
3646 case '4':
3647 case '5':
3648 case '6':
3649 case '7':
3650 case '8':
3651 case '9':
3652 tone_index = DAHDI_TONE_DTMF_0 + (digit - '0');
3653 break;
3654 case '*':
3655 tone_index = DAHDI_TONE_DTMF_s;
3656 break;
3657 case '#':
3658 tone_index = DAHDI_TONE_DTMF_p;
3659 break;
3660 case 'A':
3661 case 'B':
3662 case 'C':
3663 case 'D':
3664 tone_index = DAHDI_TONE_DTMF_A + (digit - 'A');
3665 break;
3666 case 'W':
3667 return &tone_pause;
3668 default:
3669 return NULL;
3670 }
3671 return &chan->curzone->dtmf[tone_index - DAHDI_TONE_DTMF_BASE];
3672 case DIGIT_MODE_MFR1:
3673 switch (digit) {
3674 case '0':
3675 case '1':
3676 case '2':
3677 case '3':
3678 case '4':
3679 case '5':
3680 case '6':
3681 case '7':
3682 case '8':
3683 case '9':
3684 tone_index = DAHDI_TONE_MFR1_0 + (digit - '0');
3685 break;
3686 case '*':
3687 tone_index = DAHDI_TONE_MFR1_KP;
3688 break;
3689 case '#':
3690 tone_index = DAHDI_TONE_MFR1_ST;
3691 break;
3692 case 'A':
3693 tone_index = DAHDI_TONE_MFR1_STP;
3694 break;
3695 case 'B':
3696 tone_index = DAHDI_TONE_MFR1_ST2P;
3697 break;
3698 case 'C':
3699 tone_index = DAHDI_TONE_MFR1_ST3P;
3700 break;
3701 case 'W':
3702 return &tone_pause;
3703 default:
3704 return NULL;
3705 }
3706 return &chan->curzone->mfr1[tone_index - DAHDI_TONE_MFR1_BASE];
3707 case DIGIT_MODE_MFR2_FWD:
3708 switch (digit) {
3709 case '1':
3710 case '2':
3711 case '3':
3712 case '4':
3713 case '5':
3714 case '6':
3715 case '7':
3716 case '8':
3717 case '9':
3718 tone_index = DAHDI_TONE_MFR2_FWD_1 + (digit - '1');
3719 break;
3720 case 'A':
3721 case 'B':
3722 case 'C':
3723 case 'D':
3724 case 'E':
3725 case 'F':
3726 tone_index = DAHDI_TONE_MFR2_FWD_10 + (digit - 'A');
3727 break;
3728 case 'W':
3729 return &tone_pause;
3730 default:
3731 return NULL;
3732 }
3733 return &chan->curzone->mfr2_fwd[tone_index - DAHDI_TONE_MFR2_FWD_BASE];
3734 case DIGIT_MODE_MFR2_REV:
3735 switch (digit) {
3736 case '1':
3737 case '2':
3738 case '3':
3739 case '4':
3740 case '5':
3741 case '6':
3742 case '7':
3743 case '8':
3744 case '9':
3745 tone_index = DAHDI_TONE_MFR2_REV_1 + (digit - '1');
3746 break;
3747 case 'A':
3748 case 'B':
3749 case 'C':
3750 case 'D':
3751 case 'E':
3752 case 'F':
3753 tone_index = DAHDI_TONE_MFR2_REV_10 + (digit - 'A');
3754 break;
3755 case 'W':
3756 return &tone_pause;
3757 default:
3758 return NULL;
3759 }
3760 return &chan->curzone->mfr2_rev[tone_index - DAHDI_TONE_MFR2_REV_BASE];
3761 default:
3762 return NULL;
3763 }
3764 }
3765
3766 static void __do_dtmf(struct dahdi_chan *chan)
3767 {
3768 char c;
3769
3770 /* Called with chan->lock held */
3771 while ((c = chan->txdialbuf[0])) {
3772 memmove(chan->txdialbuf, chan->txdialbuf + 1, sizeof(chan->txdialbuf) - 1);
3773 switch (c) {
3774 case 'T':
3775 chan->digitmode = DIGIT_MODE_DTMF;
3776 chan->tonep = 0;
3777 break;
3778 case 'M':
3779 chan->digitmode = DIGIT_MODE_MFR1;
3780 chan->tonep = 0;
3781 break;
3782 case 'O':
3783 chan->digitmode = DIGIT_MODE_MFR2_FWD;
3784 chan->tonep = 0;
3785 break;
3786 case 'R':
3787 chan->digitmode = DIGIT_MODE_MFR2_REV;
3788 chan->tonep = 0;
3789 break;
3790 case 'P':
3791 chan->digitmode = DIGIT_MODE_PULSE;
3792 chan->tonep = 0;
3793 break;
3794 default:
3795 if ((c != 'W') && (chan->digitmode == DIGIT_MODE_PULSE)) {
3796 if ((c >= '0') && (c <= '9') && (chan->txhooksig == DAHDI_TXSIG_OFFHOOK)) {
3797 chan->pdialcount = (c == '0') ? 10 : c - '0';
3798 dahdi_rbs_sethook(chan, DAHDI_TXSIG_ONHOOK, DAHDI_TXSTATE_PULSEBREAK,
3799 chan->pulsebreaktime);
3800 return;
3801 }
3802 } else {
3803 chan->curtone = dahdi_mf_tone(chan, c, chan->digitmode);
3804 chan->tonep = 0;
3805 if (chan->curtone) {
3806 dahdi_init_tone_state(&chan->ts, chan->curtone);
3807 return;
3808 }
3809 }
3810 }
3811 }
3812
3813 /* Notify userspace process if there is nothing left */
3814 chan->dialing = 0;
3815 __qevent(chan, DAHDI_EVENT_DIALCOMPLETE);
3816 }
3817
3818 static int dahdi_release(struct inode *inode, struct file *file)
3819 {
3820 int unit = UNIT(file);
3821 int res;
3822 struct dahdi_chan *chan;
3823
3824 if (unit == DAHDI_CTL)
3825 return dahdi_ctl_release(file);
3826 if (unit == DAHDI_TIMER) {
3827 return dahdi_timer_release(inode, file);
3828 }
3829 if (unit == DAHDI_TRANSCODE) {
3830 /* We should not be here because the dahdi_transcode.ko module
3831 * should have updated the file_operations for this file
3832 * handle when the file was opened. */
3833 WARN_ON(1);
3834 return -EFAULT;
3835 }
3836 if (unit == DAHDI_CHANNEL) {
3837 chan = file->private_data;
3838 if (!chan)
3839 return dahdi_chan_release(file);
3840 else
3841 return dahdi_specchan_release(file);
3842 }
3843 if (unit == DAHDI_PSEUDO) {
3844 chan = file->private_data;
3845 if (chan) {
3846 res = dahdi_specchan_release(file);
3847 dahdi_free_pseudo(chan);
3848 } else {
3849 module_printk(KERN_NOTICE, "Pseudo release and no private data??\n");
3850 res = 0;
3851 }
3852 return res;
3853 }
3854 return dahdi_specchan_release(file);
3855 }
3856
3857 /**
3858 * dahdi_alarm_channel() - notify userspace channel is (not) in alarm
3859 * @chan: the DAHDI channel
3860 * @alarms: alarm bits set
3861 *
3862 * Notify userspace about a change in alarm status of this channel.
3863 *
3864 * Note that channel drivers should only use this function directly if
3865 * they have a single port per channel. Whole-span alarms should be sent
3866 * using dahdi_alarm_notify() .
3867 *
3868 * Does nothing if alarms on the channel have not changed. If they have,
3869 * triggers sending either DAHDI_EVENT_NOALARM (if they were cleared) or
3870 * DAHDI_EVENT_ALARM (otherwise).
3871 *
3872 * Currently it is only used by drivers of FXO ports to notify (with a
3873 * red alarm) they have no battery current.
3874 */
3875 void dahdi_alarm_channel(struct dahdi_chan *chan, int alarms)
3876 {
3877 unsigned long flags;
3878
3879 spin_lock_irqsave(&chan->lock, flags);
3880 if (chan->chan_alarms != alarms) {
3881 chan->chan_alarms = alarms;
3882 dahdi_qevent_nolock(chan, alarms ? DAHDI_EVENT_ALARM : DAHDI_EVENT_NOALARM);
3883 }
3884 spin_unlock_irqrestore(&chan->lock, flags);
3885 }
3886
3887 struct dahdi_span *get_master_span(void)
3888 {
3889 return master_span;
3890 }
3891
3892 void set_master_span(int spanno)
3893 {
3894 struct dahdi_span *s;
3895 unsigned long flags;
3896 struct dahdi_span *old_master;
3897
3898 spin_lock_irqsave(&chan_lock, flags);
3899 old_master = master_span;
3900 list_for_each_entry(s, &span_list, spans_node) {
3901 if (spanno == s->spanno) {
3902 master_span = s;
3903 break;
3904 }
3905 }
3906 spin_unlock_irqrestore(&chan_lock, flags);
3907 if ((debug & DEBUG_MAIN) && (old_master != master_span))
3908 module_printk(KERN_NOTICE, "Master span is set to %d (%s)\n",
3909 master_span->spanno, master_span->name);
3910 }
3911
3912 static void __dahdi_find_master_span(void)
3913 {
3914 struct dahdi_span *s;
3915 unsigned long flags;
3916 struct dahdi_span *old_master;
3917
3918 spin_lock_irqsave(&chan_lock, flags);
3919 old_master = master_span;
3920 list_for_each_entry(s, &span_list, spans_node) {
3921 if (s->alarms && old_master)
3922 continue;
3923 if (dahdi_is_digital_span(s) &&
3924 !test_bit(DAHDI_FLAGBIT_RUNNING, &s->flags) &&
3925 old_master)
3926 continue;
3927 if (!can_provide_timing(s))
3928 continue;
3929 if (master_span == s)
3930 continue;
3931
3932 master_span = s;
3933 break;
3934 }
3935 spin_unlock_irqrestore(&chan_lock, flags);
3936
3937 if ((debug & DEBUG_MAIN) && (old_master != master_span))
3938 module_printk(KERN_NOTICE, "Master changed to %s\n", s->name);
3939 }
3940
3941 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
3942 static void _dahdi_find_master_span(void *work)
3943 {
3944 __dahdi_find_master_span();
3945 }
3946 static DECLARE_WORK(find_master_work, _dahdi_find_master_span, NULL);
3947 #else
3948 static void _dahdi_find_master_span(struct work_struct *work)
3949 {
3950 __dahdi_find_master_span();
3951 }
3952 static DECLARE_WORK(find_master_work, _dahdi_find_master_span);
3953 #endif
3954
3955 static void dahdi_find_master_span(void)
3956 {
3957 schedule_work(&find_master_work);
3958 }
3959
3960 void dahdi_alarm_notify(struct dahdi_span *span)
3961 {
3962 int x;
3963
3964 span->alarms &= ~DAHDI_ALARM_LOOPBACK;
3965 /* Determine maint status */
3966 if (span->maintstat || span->mainttimer)
3967 span->alarms |= DAHDI_ALARM_LOOPBACK;
3968 /* DON'T CHANGE THIS AGAIN. THIS WAS DONE FOR A REASON.
3969 The expression (a != b) does *NOT* do the same thing
3970 as ((!a) != (!b)) */
3971 /* if change in general state */
3972 if ((!span->alarms) != (!span->lastalarms)) {
3973 span->lastalarms = span->alarms;
3974 for (x = 0; x < span->channels; x++)
3975 dahdi_alarm_channel(span->chans[x], span->alarms);
3976
3977 /* If we're going into or out of alarm we should try to find a
3978 * new master_span that may be a better fit. */
3979 dahdi_find_master_span();
3980
3981 /* Report more detailed alarms */
3982 if (debug & DEBUG_MAIN) {
3983 if (span->alarms & DAHDI_ALARM_LOS) {
3984 module_printk(KERN_NOTICE,
3985 "Span %d: Loss of signal\n",
3986 span->spanno);
3987 }
3988 if (span->alarms & DAHDI_ALARM_LFA) {
3989 module_printk(KERN_NOTICE,
3990 "Span %d: Loss of Frame Alignment\n",
3991 span->spanno);
3992 }
3993 if (span->alarms & DAHDI_ALARM_LMFA) {
3994 module_printk(KERN_NOTICE,
3995 "Span %d: Loss of Multi-Frame "\
3996 "Alignment\n", span->spanno);
3997 }
3998 }
3999 }
4000 }
4001
4002 static long
4003 dahdi_timer_unlocked_ioctl(struct file *file, unsigned int cmd,
4004 unsigned long data)
4005 {
4006 int j;
4007 unsigned long flags;
4008 struct dahdi_timer *const timer = file->private_data;
4009
4010 switch(cmd) {
4011 case DAHDI_TIMERCONFIG:
4012 get_user(j, (int __user *)data);
4013 if (j < 0)
4014 j = 0;
4015 spin_lock_irqsave(&timer->lock, flags);
4016 if (timer->ms != j) {
4017 if (j && list_empty(&timer->list)) {
4018 /* The timer is being activated so add to the
4019 * global timer list. */
4020 spin_unlock(&timer->lock);
4021 spin_lock(&dahdi_timer_lock);
4022 spin_lock(&timer->lock);
4023 timer->ms = timer->pos = j;
4024 list_add(&timer->list, &dahdi_timers);
4025 spin_unlock(&dahdi_timer_lock);
4026 } else if (!j && !list_empty(&timer->list)) {
4027 /* The timer is being disabled so we can remove
4028 * from the global timer list. */
4029 spin_unlock(&timer->lock);
4030 spin_lock(&dahdi_timer_lock);
4031 spin_lock(&timer->lock);
4032 list_del_init(&timer->list);
4033 timer->ms = timer->pos = j;
4034 spin_unlock(&dahdi_timer_lock);
4035 } else {
4036 timer->ms = timer->pos = j;
4037 }
4038 }
4039 spin_unlock_irqrestore(&timer->lock, flags);
4040 break;
4041 case DAHDI_TIMERACK:
4042 get_user(j, (int __user *)data);
4043 spin_lock_irqsave(&timer->lock, flags);
4044 if ((j < 1) || (j > timer->tripped))
4045 j = timer->tripped;
4046 timer->tripped -= j;
4047 spin_unlock_irqrestore(&timer->lock, flags);
4048 break;
4049 case DAHDI_GETEVENT: /* Get event on queue */
4050 j = DAHDI_EVENT_NONE;
4051 spin_lock_irqsave(&timer->lock, flags);
4052 /* set up for no event */
4053 if (timer->tripped)
4054 j = DAHDI_EVENT_TIMER_EXPIRED;
4055 if (timer->ping)
4056 j = DAHDI_EVENT_TIMER_PING;
4057 spin_unlock_irqrestore(&timer->lock, flags);
4058 put_user(j, (int __user *)data);
4059 break;
4060 case DAHDI_TIMERPING:
4061 spin_lock_irqsave(&timer->lock, flags);
4062 timer->ping = 1;
4063 wake_up_interruptible(&timer->sel);
4064 spin_unlock_irqrestore(&timer->lock, flags);
4065 break;
4066 case DAHDI_TIMERPONG:
4067 spin_lock_irqsave(&timer->lock, flags);
4068 timer->ping = 0;
4069 spin_unlock_irqrestore(&timer->lock, flags);
4070 break;
4071 default:
4072 return -ENOTTY;
4073 }
4074 return 0;
4075 }
4076
4077 #ifndef HAVE_UNLOCKED_IOCTL
4078 static int dahdi_timer_ioctl(struct inode *inode, struct file *file,
4079 unsigned int cmd, unsigned long data)
4080 {
4081 return dahdi_timer_unlocked_ioctl(file, cmd, data);
4082 }
4083 #endif
4084
4085 static int dahdi_ioctl_getgains(struct file *file, unsigned long data)
4086 {
4087 int res = 0;
4088 struct dahdi_gains *gain;
4089 int j;
4090 void __user * const user_data = (void __user *)data;
4091 struct dahdi_chan *chan;
4092
4093 gain = kzalloc(sizeof(*gain), GFP_KERNEL);
4094 if (!gain)
4095 return -ENOMEM;
4096
4097 if (copy_from_user(gain, user_data, sizeof(*gain))) {
4098 res = -EFAULT;
4099 goto cleanup;
4100 }
4101 chan = (!gain->chan) ? chan_from_file(file) :
4102 chan_from_num(gain->chan);
4103 if (!chan) {
4104 res = -EINVAL;
4105 goto cleanup;
4106 }
4107
4108 if (!(chan->flags & DAHDI_FLAG_AUDIO)) {
4109 res = -EINVAL;
4110 goto cleanup;
4111 }
4112 gain->chan = chan->channo;
4113 for (j = 0; j < 256; ++j) {
4114 gain->txgain[j] = chan->txgain[j];
4115 gain->rxgain[j] = chan->rxgain[j];
4116 }
4117 if (copy_to_user(user_data, gain, sizeof(*gain))) {
4118 res = -EFAULT;
4119 goto cleanup;
4120 }
4121 cleanup:
4122
4123 kfree(gain);
4124 return res;
4125 }
4126
4127 static int dahdi_ioctl_setgains(struct file *file, unsigned long data)
4128 {
4129 int res = 0;
4130 struct dahdi_gains *gain;
4131 unsigned char *txgain, *rxgain;
4132 int j;
4133 unsigned long flags;
4134 const int GAIN_TABLE_SIZE = sizeof(defgain);
4135 void __user * const user_data = (void __user *)data;
4136 struct dahdi_chan *chan;
4137
4138 gain = kzalloc(sizeof(*gain), GFP_KERNEL);
4139 if (!gain)
4140 return -ENOMEM;
4141
4142 if (copy_from_user(gain, user_data, sizeof(*gain))) {
4143 res = -EFAULT;
4144 goto cleanup;
4145 }
4146
4147 chan = (!gain->chan) ? chan_from_file(file) :
4148 chan_from_num(gain->chan);
4149 if (!chan) {
4150 res = -EINVAL;
4151 goto cleanup;
4152 }
4153 if (!(chan->flags & DAHDI_FLAG_AUDIO)) {
4154 res = -EINVAL;
4155 goto cleanup;
4156 }
4157
4158 rxgain = kzalloc(GAIN_TABLE_SIZE*2, GFP_KERNEL);
4159 if (!rxgain) {
4160 res = -ENOMEM;
4161 goto cleanup;
4162 }
4163
4164 gain->chan = chan->channo;
4165 txgain = rxgain + GAIN_TABLE_SIZE;
4166
4167 for (j = 0; j < GAIN_TABLE_SIZE; ++j) {
4168 rxgain[j] = gain->rxgain[j];
4169 txgain[j] = gain->txgain[j];
4170 }
4171
4172 if (!memcmp(rxgain, defgain, GAIN_TABLE_SIZE) &&
4173 !memcmp(txgain, defgain, GAIN_TABLE_SIZE)) {
4174 kfree(rxgain);
4175 spin_lock_irqsave(&chan->lock, flags);
4176 if (is_gain_allocated(chan))
4177 kfree(chan->rxgain);
4178 chan->rxgain = defgain;
4179 chan->txgain = defgain;
4180 spin_unlock_irqrestore(&chan->lock, flags);
4181 } else {
4182 /* This is a custom gain setting */
4183 spin_lock_irqsave(&chan->lock, flags);
4184 if (is_gain_allocated(chan))
4185 kfree(chan->rxgain);
4186 chan->rxgain = rxgain;
4187 chan->txgain = txgain;
4188 spin_unlock_irqrestore(&chan->lock, flags);
4189 }
4190
4191 if (copy_to_user(user_data, gain, sizeof(*gain))) {
4192 res = -EFAULT;
4193 goto cleanup;
4194 }
4195 cleanup:
4196
4197 kfree(gain);
4198 return res;
4199 }
4200
4201 static int dahdi_ioctl_chandiag(struct file *file, unsigned long data)
4202 {
4203 unsigned long flags;
4204 int channo;
4205 /* there really is no need to initialize this structure because when it is used it has
4206 * already been completely overwritten, but apparently the compiler cannot figure that
4207 * out and warns about uninitialized usage... so initialize it.
4208 */
4209 struct dahdi_echocan_state ec_state = { .ops = NULL, };
4210 struct dahdi_chan *chan;
4211 struct dahdi_chan *temp;
4212
4213 /* get channel number from user */
4214 get_user(channo, (int __user *)data);
4215
4216 chan = chan_from_num(channo);
4217 if (!chan)
4218 return -EINVAL;
4219
4220 temp = kmalloc(sizeof(*chan), GFP_KERNEL);
4221 if (!temp)
4222 return -ENOMEM;
4223
4224 spin_lock_irqsave(&chan->lock, flags);
4225 *temp = *chan;
4226 if (temp->ec_state)
4227 ec_state = *temp->ec_state;
4228 if (temp->curzone)
4229 tone_zone_get(temp->curzone);
4230 spin_unlock_irqrestore(&chan->lock, flags);
4231
4232 module_printk(KERN_INFO, "Dump of DAHDI Channel %d (%s,%d,%d):\n\n",
4233 channo, temp->name, temp->channo, temp->chanpos);
4234 module_printk(KERN_INFO, "flags: %x hex, writechunk: %p, readchunk: %p\n",
4235 (unsigned int) temp->flags, temp->writechunk, temp->readchunk);
4236 module_printk(KERN_INFO, "rxgain: %p, txgain: %p, gainalloc: %d\n",
4237 temp->rxgain, temp->txgain, is_gain_allocated(temp));
4238 module_printk(KERN_INFO, "span: %p, sig: %x hex, sigcap: %x hex\n",
4239 temp->span, temp->sig, temp->sigcap);
4240 module_printk(KERN_INFO, "inreadbuf: %d, outreadbuf: %d, inwritebuf: %d, outwritebuf: %d\n",
4241 temp->inreadbuf, temp->outreadbuf, temp->inwritebuf, temp->outwritebuf);
4242 module_printk(KERN_INFO, "blocksize: %d, numbufs: %d, txbufpolicy: %d, txbufpolicy: %d\n",
4243 temp->blocksize, temp->numbufs, temp->txbufpolicy,
4244 DAHDI_POLICY_IMMEDIATE);
4245 module_printk(KERN_INFO, "txdisable: %d, rxdisable: %d, iomask: %d\n",
4246 temp->txdisable, 0, temp->iomask);
4247 module_printk(KERN_INFO, "curzone: %p, tonezone: %d, curtone: %p, tonep: %d\n",
4248 temp->curzone,
4249 ((temp->curzone) ? temp->curzone->num : -1),
4250 temp->curtone, temp->tonep);
4251 module_printk(KERN_INFO, "digitmode: %d, txdialbuf: %s, dialing: %d, aftdialtimer: %d, cadpos. %d\n",
4252 temp->digitmode, temp->txdialbuf, temp->dialing,
4253 temp->afterdialingtimer, temp->cadencepos);
4254 module_printk(KERN_INFO, "confna: %d, confn: %d, confmode: %d, confmute: %d\n",
4255 temp->confna, temp->_confn, temp->confmode, temp->confmute);
4256 module_printk(KERN_INFO, "ec: %p, deflaw: %d, xlaw: %p\n",
4257 temp->ec_state, temp->deflaw, temp->xlaw);
4258 if (temp->ec_state) {
4259 module_printk(KERN_INFO, "echostate: %02x, echotimer: %d, echolastupdate: %d\n",
4260 ec_state.status.mode, ec_state.status.pretrain_timer, ec_state.status.last_train_tap);
4261 }
4262 module_printk(KERN_INFO, "itimer: %d, otimer: %d, ringdebtimer: %d\n\n",
4263 temp->itimer, temp->otimer, temp->ringdebtimer);
4264
4265 if (temp->curzone)
4266 tone_zone_put(temp->curzone);
4267 kfree(temp);
4268 return 0;
4269 }
4270
4271 /**
4272 * dahdi_ioctl_getparams() - Get channel parameters.
4273 *
4274 */
4275 static int dahdi_ioctl_getparams(struct file *file, unsigned long data)
4276 {
4277 size_t size_to_copy;
4278 struct dahdi_params param;
4279 bool return_master = false;
4280 struct dahdi_chan *chan;
4281 int j;
4282 size_to_copy = sizeof(struct dahdi_params);
4283 if (copy_from_user(&param, (void __user *)data, size_to_copy))
4284 return -EFAULT;
4285
4286 /* check to see if the caller wants to receive our master channel
4287 * number */
4288 if (param.channo & DAHDI_GET_PARAMS_RETURN_MASTER) {
4289 return_master = true;
4290 param.channo &= ~DAHDI_GET_PARAMS_RETURN_MASTER;
4291 }
4292
4293 /* Pick the right channo's */
4294 chan = chan_from_file(file);
4295 if (!chan)
4296 chan = chan_from_num(param.channo);
4297
4298 if (!chan)
4299 return -EINVAL;
4300
4301 /* point to relevant structure */
4302 param.sigtype = chan->sig; /* get signalling type */
4303 /* return non-zero if rx not in idle state */
4304 if (chan->span) {
4305 j = dahdi_q_sig(chan);
4306 if (j >= 0) { /* if returned with success */
4307 param.rxisoffhook = ((chan->rxsig & (j >> 8)) !=
4308 (j & 0xff));
4309 } else {
4310 const int sig = chan->rxhooksig;
4311 param.rxisoffhook = ((sig != DAHDI_RXSIG_ONHOOK) &&
4312 (sig != DAHDI_RXSIG_INITIAL));
4313 }
4314 } else if ((chan->txstate == DAHDI_TXSTATE_KEWL) ||
4315 (chan->txstate == DAHDI_TXSTATE_AFTERKEWL)) {
4316 param.rxisoffhook = 1;
4317 } else {
4318 param.rxisoffhook = 0;
4319 }
4320
4321 if (chan->span &&
4322 chan->span->ops->rbsbits && !(chan->sig & DAHDI_SIG_CLEAR)) {
4323 param.rxbits = chan->rxsig;
4324 param.txbits = chan->txsig;
4325 param.idlebits = chan->idlebits;
4326 } else {
4327 param.rxbits = -1;
4328 param.txbits = -1;
4329 param.idlebits = 0;
4330 }
4331 if (chan->span &&
4332 (chan->span->ops->rbsbits || chan->span->ops->hooksig) &&
4333 !(chan->sig & DAHDI_SIG_CLEAR)) {
4334 param.rxhooksig = chan->rxhooksig;
4335 param.txhooksig = chan->txhooksig;
4336 } else {
4337 param.rxhooksig = -1;
4338 param.txhooksig = -1;
4339 }
4340 param.prewinktime = chan->prewinktime;
4341 param.preflashtime = chan->preflashtime;
4342 param.winktime = chan->winktime;
4343 param.flashtime = chan->flashtime;
4344 param.starttime = chan->starttime;
4345 param.rxwinktime = chan->rxwinktime;
4346 param.rxflashtime = chan->rxflashtime;
4347 param.debouncetime = chan->debouncetime;
4348 param.channo = chan->channo;
4349 param.chan_alarms = chan->chan_alarms;
4350
4351 /* if requested, put the master channel number in the top 16 bits of
4352 * the result */
4353 if (return_master)
4354 param.channo |= chan->master->channo << 16;
4355
4356 param.pulsemaketime = chan->pulsemaketime;
4357 param.pulsebreaktime = chan->pulsebreaktime;
4358 param.pulseaftertime = chan->pulseaftertime;
4359 param.spanno = (chan->span) ? chan->span->spanno : 0;
4360 strlcpy(param.name, chan->name, sizeof(param.name));
4361 param.chanpos = chan->chanpos;
4362 param.sigcap = chan->sigcap;
4363 /* Return current law */
4364 if (chan->xlaw == __dahdi_alaw)
4365 param.curlaw = DAHDI_LAW_ALAW;
4366 else
4367 param.curlaw = DAHDI_LAW_MULAW;
4368
4369 if (copy_to_user((void __user *)data, &param, size_to_copy))
4370 return -EFAULT;
4371
4372 return 0;
4373 }
4374
4375 /**
4376 * dahdi_ioctl_setparams() - Set channel parameters.
4377 *
4378 */
4379 static int dahdi_ioctl_setparams(struct file *file, unsigned long data)
4380 {
4381 struct dahdi_params param;
4382 struct dahdi_chan *chan;
4383
4384 if (copy_from_user(&param, (void __user *)data, sizeof(param)))
4385 return -EFAULT;
4386
4387 param.chan_alarms = 0; /* be explicit about the above */
4388
4389 /* Pick the right channo's */
4390 chan = chan_from_file(file);
4391 if (!chan)
4392 chan = chan_from_num(param.channo);
4393
4394 if (!chan)
4395 return -EINVAL;
4396
4397 /* point to relevant structure */
4398 /* NOTE: sigtype is *not* included in this */
4399 /* get timing paramters */
4400 chan->prewinktime = param.prewinktime;
4401 chan->preflashtime = param.preflashtime;
4402 chan->winktime = param.winktime;
4403 chan->flashtime = param.flashtime;
4404 chan->starttime = param.starttime;
4405 /* Update ringtime if not using a tone zone */
4406 if (!chan->curzone)
4407 chan->ringcadence[0] = chan->starttime;
4408 chan->rxwinktime = param.rxwinktime;
4409 chan->rxflashtime = param.rxflashtime;
4410 chan->debouncetime = param.debouncetime;
4411 chan->pulsemaketime = param.pulsemaketime;
4412 chan->pulsebreaktime = param.pulsebreaktime;
4413 chan->pulseaftertime = param.pulseaftertime;
4414 return 0;
4415 }
4416
4417 /**
4418 * dahdi_ioctl_spanstat() - Return statistics for a span.
4419 *
4420 */
4421 static int dahdi_ioctl_spanstat(struct file *file, unsigned long data)
4422 {
4423 int ret = 0;
4424 struct dahdi_spaninfo spaninfo;
4425 struct dahdi_span *s;
4426 int j;
4427 size_t size_to_copy;
4428 bool via_chan = false;
4429
4430 size_to_copy = sizeof(struct dahdi_spaninfo);
4431 if (copy_from_user(&spaninfo, (void __user *)data, size_to_copy))
4432 return -EFAULT;
4433 if (!spaninfo.spanno) {
4434 struct dahdi_chan *const chan = chan_from_file(file);
4435 if (!chan)
4436 return -EINVAL;
4437
4438 s = chan->span;
4439 via_chan = true;
4440 } else {
4441 s = span_find_and_get(spaninfo.spanno);
4442 }
4443 if (!s)
4444 return -EINVAL;
4445
4446 spaninfo.spanno = s->spanno; /* put the span # in here */
4447 spaninfo.totalspans = span_count();
4448 strlcpy(spaninfo.desc, s->desc, sizeof(spaninfo.desc));
4449 strlcpy(spaninfo.name, s->name, sizeof(spaninfo.name));
4450 spaninfo.alarms = s->alarms; /* get alarm status */
4451 spaninfo.rxlevel = s->rxlevel; /* get rx level */
4452 spaninfo.txlevel = s->txlevel; /* get tx level */
4453
4454 spaninfo.bpvcount = s->count.bpv;
4455 spaninfo.crc4count = s->count.crc4;
4456 spaninfo.ebitcount = s->count.ebit;
4457 spaninfo.fascount = s->count.fas;
4458 spaninfo.fecount = s->count.fe;
4459 spaninfo.cvcount = s->count.cv;
4460 spaninfo.becount = s->count.be;
4461 spaninfo.prbs = s->count.prbs;
4462 spaninfo.errsec = s->count.errsec;
4463
4464 spaninfo.irqmisses = s->parent->irqmisses; /* get IRQ miss count */
4465 spaninfo.syncsrc = s->syncsrc; /* get active sync source */
4466 spaninfo.totalchans = s->channels;
4467 spaninfo.numchans = 0;
4468 for (j = 0; j < s->channels; j++) {
4469 if (s->chans[j]->sig)
4470 spaninfo.numchans++;
4471 }
4472 spaninfo.lbo = s->lbo;
4473 spaninfo.lineconfig = s->lineconfig;
4474 spaninfo.irq = 0;
4475 spaninfo.linecompat = s->linecompat;
4476 strlcpy(spaninfo.lboname, dahdi_lboname(s->lbo),
4477 sizeof(spaninfo.lboname));
4478 if (s->parent->manufacturer) {
4479 strlcpy(spaninfo.manufacturer, s->parent->manufacturer,
4480 sizeof(spaninfo.manufacturer));
4481 }
4482 if (s->parent->devicetype) {
4483 strlcpy(spaninfo.devicetype, s->parent->devicetype,
4484 sizeof(spaninfo.devicetype));
4485 }
4486 if (s->parent->location) {
4487 strlcpy(spaninfo.location, s->parent->location,
4488 sizeof(spaninfo.location));
4489 }
4490 if (s->spantype) {
4491 /*
4492 * The API is brain-damaged, returning fixed length
4493 * null terminated strings via ioctl() is...
4494 *
4495 * This field contain only 6 characters
4496 * (including null termination, 5 effective characters).
4497 *
4498 * For backward compatibility, massage this info for dahdi-scan
4499 * and friends, until:
4500 * - They either learn to read the info from sysfs
4501 * - Or this API is broken to return the enum value
4502 */
4503 const char *st = dahdi_spantype2str(s->spantype);
4504 switch (s->spantype) {
4505 case SPANTYPE_DIGITAL_BRI_NT:
4506 strlcpy(spaninfo.spantype, "NT",
4507 sizeof(spaninfo.spantype));
4508 break;
4509 case SPANTYPE_DIGITAL_BRI_TE:
4510 strlcpy(spaninfo.spantype, "TE",
4511 sizeof(spaninfo.spantype));
4512 break;
4513 default:
4514 /*
4515 * The rest are either short (FXS, FXO, E1, T1, J1)
4516 * Or new (BRI_SOFT, ANALOG_MIXED, INVALID),
4517 * so no backward compatibility for this
4518 * broken interface.
4519 */
4520 strlcpy(spaninfo.spantype, st,
4521 sizeof(spaninfo.spantype));
4522 break;
4523 }
4524 }
4525
4526 if (copy_to_user((void __user *)data, &spaninfo, size_to_copy))
4527 ret = -EFAULT;
4528
4529 if (!via_chan)
4530 put_span(s);
4531
4532 return ret;
4533 }
4534
4535 /**
4536 * dahdi_ioctl_spanstat_v1() - Return statistics for a span in a legacy format.
4537 *
4538 */
4539 static int dahdi_ioctl_spanstat_v1(struct file *file, unsigned long data)
4540 {
4541 int ret = 0;
4542 struct dahdi_spaninfo_v1 spaninfo_v1;
4543 struct dahdi_span *s;
4544 int j;
4545 bool via_chan = false;
4546
4547 if (copy_from_user(&spaninfo_v1, (void __user *)data,
4548 sizeof(spaninfo_v1))) {
4549 return -EFAULT;
4550 }
4551
4552 if (!spaninfo_v1.spanno) {
4553 struct dahdi_chan *const chan = chan_from_file(file);
4554 if (!chan)
4555 return -EINVAL;
4556
4557 s = chan->span;
4558 via_chan = true;
4559 } else {
4560 s = span_find_and_get(spaninfo_v1.spanno);
4561 }
4562
4563 if (!s)
4564 return -EINVAL;
4565
4566 spaninfo_v1.spanno = s->spanno; /* put the span # in here */
4567 spaninfo_v1.totalspans = 0;
4568 spaninfo_v1.totalspans = span_count();
4569 strlcpy(spaninfo_v1.desc,
4570 s->desc,
4571 sizeof(spaninfo_v1.desc));
4572 strlcpy(spaninfo_v1.name,
4573 s->name,
4574 sizeof(spaninfo_v1.name));
4575 spaninfo_v1.alarms = s->alarms;
4576 spaninfo_v1.bpvcount = s->count.bpv;
4577 spaninfo_v1.rxlevel = s->rxlevel;
4578 spaninfo_v1.txlevel = s->txlevel;
4579 spaninfo_v1.crc4count = s->count.crc4;
4580 spaninfo_v1.ebitcount = s->count.ebit;
4581 spaninfo_v1.fascount = s->count.fas;
4582 spaninfo_v1.irqmisses = s->parent->irqmisses;
4583 spaninfo_v1.syncsrc = s->syncsrc;
4584 spaninfo_v1.totalchans = s->channels;
4585 spaninfo_v1.numchans = 0;
4586 for (j = 0; j < s->channels; j++) {
4587 if (s->chans[j]->sig)
4588 spaninfo_v1.numchans++;
4589 }
4590 spaninfo_v1.lbo = s->lbo;
4591 spaninfo_v1.lineconfig = s->lineconfig;
4592 spaninfo_v1.irq = 0;
4593 spaninfo_v1.linecompat = s->linecompat;
4594 strlcpy(spaninfo_v1.lboname,
4595 dahdi_lboname(s->lbo),
4596 sizeof(spaninfo_v1.lboname));
4597
4598 if (s->parent->manufacturer) {
4599 strlcpy(spaninfo_v1.manufacturer, s->parent->manufacturer,
4600 sizeof(spaninfo_v1.manufacturer));
4601 }
4602
4603 if (s->parent->devicetype) {
4604 strlcpy(spaninfo_v1.devicetype, s->parent->devicetype,
4605 sizeof(spaninfo_v1.devicetype));
4606 }
4607
4608 strlcpy(spaninfo_v1.location, s->parent->location,
4609 sizeof(spaninfo_v1.location));
4610
4611 if (s->spantype) {
4612 strlcpy(spaninfo_v1.spantype,
4613 dahdi_spantype2str(s->spantype),
4614 sizeof(spaninfo_v1.spantype));
4615 }
4616
4617 if (copy_to_user((void __user *)data, &spaninfo_v1,
4618 sizeof(spaninfo_v1))) {
4619 ret = -EFAULT;
4620 }
4621 if (!via_chan)
4622 put_span(s);
4623 return ret;
4624 }
4625
4626 #ifdef CONFIG_DAHDI_CONFLINK
4627 static int dahdi_ioctl_conflink(struct file *file, unsigned long data)
4628 {
4629 struct dahdi_chan *chan;
4630 struct dahdi_confinfo conf;
4631 unsigned long flags;
4632 int res = 0;
4633 int i;
4634
4635 chan = chan_from_file(file);
4636 if (!chan)
4637 return -EINVAL;
4638 if (!(chan->flags & DAHDI_FLAG_AUDIO))
4639 return -EINVAL;
4640 if (copy_from_user(&conf, (void __user *)data, sizeof(conf)))
4641 return -EFAULT;
4642 /* check sanity of arguments */
4643 if ((conf.chan < 0) || (conf.chan > DAHDI_MAX_CONF))
4644 return -EINVAL;
4645 if ((conf.confno < 0) || (conf.confno > DAHDI_MAX_CONF))
4646 return -EINVAL;
4647 /* cant listen to self!! */
4648 if (conf.chan && (conf.chan == conf.confno))
4649 return -EINVAL;
4650
4651 spin_lock_irqsave(&chan_lock, flags);
4652 spin_lock(&chan->lock);
4653
4654 /* if to clear all links */
4655 if ((!conf.chan) && (!conf.confno)) {
4656 /* clear all the links */
4657 memset(conf_links, 0, sizeof(conf_links));
4658 recalc_maxlinks();
4659 spin_unlock(&chan->lock);
4660 spin_unlock_irqrestore(&chan_lock, flags);
4661 return 0;
4662 }
4663 /* look for already existant specified combination */
4664 for (i = 1; i <= DAHDI_MAX_CONF; i++) {
4665 /* if found, exit */
4666 if ((conf_links[i].src == conf.chan) &&
4667 (conf_links[i].dst == conf.confno))
4668 break;
4669 }
4670 if (i <= DAHDI_MAX_CONF) { /* if found */
4671 if (!conf.confmode) { /* if to remove link */
4672 conf_links[i].src = 0;
4673 conf_links[i].dst = 0;
4674 } else { /* if to add and already there, error */
4675 res = -EEXIST;
4676 }
4677 } else { /* if not found */
4678 if (conf.confmode) { /* if to add link */
4679 /* look for empty location */
4680 for (i = 1; i <= DAHDI_MAX_CONF; i++) {
4681 /* if empty, exit loop */
4682 if ((!conf_links[i].src) &&
4683 (!conf_links[i].dst))
4684 break;
4685 }
4686 /* if empty spot found */
4687 if (i <= DAHDI_MAX_CONF) {
4688 conf_links[i].src = conf.chan;
4689 conf_links[i].dst = conf.confno;
4690 } else { /* if no empties -- error */
4691 res = -ENOSPC;
4692 }
4693 } else { /* if to remove, and not found -- error */
4694 res = -ENOENT;
4695 }
4696 }
4697 recalc_maxlinks();
4698 spin_unlock(&chan->lock);
4699 spin_unlock_irqrestore(&chan_lock, flags);
4700 return res;
4701 }
4702 #else
4703 static int dahdi_ioctl_conflink(struct file *file, unsigned long data)
4704 {
4705 return -ENOSYS;
4706 }
4707 #endif
4708
4709
4710 static int dahdi_common_ioctl(struct file *file, unsigned int cmd,
4711 unsigned long data)
4712 {
4713 switch (cmd) {
4714 /* get channel parameters */
4715 case DAHDI_GET_PARAMS_V1: /* Intentional drop through. */
4716 case DAHDI_GET_PARAMS:
4717 return dahdi_ioctl_getparams(file, data);
4718
4719 case DAHDI_SET_PARAMS:
4720 return dahdi_ioctl_setparams(file, data);
4721
4722 case DAHDI_GETGAINS_V1: /* Intentional drop through. */
4723 case DAHDI_GETGAINS: /* get gain stuff */
4724 return dahdi_ioctl_getgains(file, data);
4725
4726 case DAHDI_SETGAINS: /* set gain stuff */
4727 return dahdi_ioctl_setgains(file, data);
4728
4729 case DAHDI_SPANSTAT:
4730 return dahdi_ioctl_spanstat(file, data);
4731
4732 case DAHDI_SPANSTAT_V1:
4733 return dahdi_ioctl_spanstat_v1(file, data);
4734
4735 case DAHDI_CHANDIAG_V1: /* Intentional drop through. */
4736 case DAHDI_CHANDIAG:
4737 return dahdi_ioctl_chandiag(file, data);
4738
4739 case DAHDI_CONFLINK:
4740 return dahdi_ioctl_conflink(file, data);
4741
4742 default:
4743 return -ENOTTY;
4744 }
4745 return 0;
4746 }
4747
4748 static const struct dahdi_dynamic_ops *dahdi_dynamic_ops;
4749 void dahdi_set_dynamic_ops(const struct dahdi_dynamic_ops *ops)
4750 {
4751 mutex_lock(&registration_mutex);
4752 dahdi_dynamic_ops = ops;
4753 mutex_unlock(&registration_mutex);
4754 }
4755 EXPORT_SYMBOL(dahdi_set_dynamic_ops);
4756
4757 static int (*dahdi_hpec_ioctl)(unsigned int cmd, unsigned long data);
4758
4759 void dahdi_set_hpec_ioctl(int (*func)(unsigned int cmd, unsigned long data))
4760 {
4761 dahdi_hpec_ioctl = func;
4762 }
4763
4764 static void recalc_slaves(struct dahdi_chan *chan)
4765 {
4766 int x;
4767 struct dahdi_chan *last = chan;
4768
4769 /* Makes no sense if you don't have a span */
4770 if (!chan->span)
4771 return;
4772
4773 #ifdef CONFIG_DAHDI_DEBUG
4774 module_printk(KERN_NOTICE, "Recalculating slaves on %s\n", chan->name);
4775 #endif
4776
4777 /* Link all slaves appropriately */
4778 for (x=chan->chanpos;x<chan->span->channels;x++)
4779 if (chan->span->chans[x]->master == chan) {
4780 #ifdef CONFIG_DAHDI_DEBUG
4781 module_printk(KERN_NOTICE, "Channel %s, slave to %s, last is %s, its next will be %d\n",
4782 chan->span->chans[x]->name, chan->name, last->name, x);
4783 #endif
4784 last->nextslave = chan->span->chans[x];
4785 last = last->nextslave;
4786 }
4787 /* Terminate list */
4788 last->nextslave = NULL;
4789 #ifdef CONFIG_DAHDI_DEBUG
4790 module_printk(KERN_NOTICE, "Done Recalculating slaves on %s (last is %s)\n", chan->name, last->name);
4791 #endif
4792 }
4793
4794 #if defined(CONFIG_DAHDI_NET) && defined(HAVE_NET_DEVICE_OPS)
4795 static const struct net_device_ops dahdi_netdev_ops = {
4796 .ndo_open = dahdi_net_open,
4797 .ndo_stop = dahdi_net_stop,
4798 .ndo_do_ioctl = dahdi_net_ioctl,
4799 .ndo_start_xmit = dahdi_xmit,
4800 };
4801 #endif
4802
4803 static int dahdi_ioctl_chanconfig(struct file *file, unsigned long data)
4804 {
4805 int res = 0;
4806 int y;
4807 struct dahdi_chanconfig ch;
4808 struct dahdi_chan *newmaster;
4809 struct dahdi_chan *chan;
4810 struct dahdi_chan *dacs_chan = NULL;
4811 unsigned long flags;
4812 int sigcap;
4813
4814 if (copy_from_user(&ch, (void __user *)data, sizeof(ch)))
4815 return -EFAULT;
4816
4817 chan = chan_from_num(ch.chan);
4818 if (!chan) {
4819 printk(KERN_NOTICE "%s: No channel for number %d\n",
4820 __func__, ch.chan);
4821 return -EINVAL;
4822 }
4823
4824 if (ch.sigtype == DAHDI_SIG_SLAVE) {
4825 newmaster = chan_from_num(ch.master);
4826 if (!newmaster) {
4827 chan_notice(chan, "%s: slave channel without master.\n",
4828 __func__);
4829 return -EINVAL;
4830 }
4831 ch.sigtype = newmaster->sig;
4832 } else if ((ch.sigtype & __DAHDI_SIG_DACS) == __DAHDI_SIG_DACS) {
4833 newmaster = chan;
4834 dacs_chan = chan_from_num(ch.idlebits);
4835 if (!dacs_chan) {
4836 chan_notice(chan, "%s: dacs channel not found: %d.\n",
4837 __func__, ch.idlebits);
4838 return -EINVAL;
4839 }
4840 } else {
4841 newmaster = chan;
4842 }
4843 spin_lock_irqsave(&chan->lock, flags);
4844 #ifdef CONFIG_DAHDI_NET
4845 if (dahdi_have_netdev(chan)) {
4846 if (chan_to_netdev(chan)->flags & IFF_UP) {
4847 spin_unlock_irqrestore(&chan->lock, flags);
4848 module_printk(KERN_WARNING, "Can't switch HDLC net mode on channel %s, since current interface is up\n", chan->name);
4849 return -EBUSY;
4850 }
4851 spin_unlock_irqrestore(&chan->lock, flags);
4852 unregister_hdlc_device(chan->hdlcnetdev->netdev);
4853 spin_lock_irqsave(&chan->lock, flags);
4854 free_netdev(chan->hdlcnetdev->netdev);
4855 kfree(chan->hdlcnetdev);
4856 chan->hdlcnetdev = NULL;
4857 clear_bit(DAHDI_FLAGBIT_NETDEV, &chan->flags);
4858 }
4859 #else
4860 if (ch.sigtype == DAHDI_SIG_HDLCNET) {
4861 spin_unlock_irqrestore(&chan->lock, flags);
4862 module_printk(KERN_WARNING, "DAHDI networking not supported by this build.\n");
4863 return -ENOSYS;
4864 }
4865 #endif
4866 sigcap = chan->sigcap;
4867 /* If they support clear channel, then they support the HDLC and such through
4868 us. */
4869 if (sigcap & DAHDI_SIG_CLEAR)
4870 sigcap |= (DAHDI_SIG_HDLCRAW | DAHDI_SIG_HDLCFCS | DAHDI_SIG_HDLCNET | DAHDI_SIG_DACS);
4871
4872 if ((sigcap & ch.sigtype) != ch.sigtype) {
4873 if (debug) {
4874 chan_notice(chan, "%s: bad sigtype. sigcap: %x, sigtype: %x.\n",
4875 __func__, sigcap, ch.sigtype);
4876 }
4877 res = -EINVAL;
4878 }
4879
4880 if (chan->master != chan) {
4881 struct dahdi_chan *oldmaster = chan->master;
4882
4883 /* Clear the master channel */
4884 chan->master = chan;
4885 chan->nextslave = NULL;
4886 /* Unlink this channel from the master's channel list */
4887 recalc_slaves(oldmaster);
4888 }
4889
4890 if (!res) {
4891 chan->sig = ch.sigtype;
4892 if (chan->sig == DAHDI_SIG_CAS)
4893 chan->idlebits = ch.idlebits;
4894 else
4895 chan->idlebits = 0;
4896 if ((ch.sigtype & DAHDI_SIG_CLEAR) == DAHDI_SIG_CLEAR) {
4897 /* Set clear channel flag if appropriate */
4898 chan->flags &= ~DAHDI_FLAG_AUDIO;
4899 chan->flags |= DAHDI_FLAG_CLEAR;
4900 } else {
4901 /* Set audio flag and not clear channel otherwise */
4902 chan->flags |= DAHDI_FLAG_AUDIO;
4903 chan->flags &= ~DAHDI_FLAG_CLEAR;
4904 }
4905 if ((ch.sigtype & DAHDI_SIG_HDLCRAW) == DAHDI_SIG_HDLCRAW) {
4906 /* Set the HDLC flag */
4907 chan->flags |= DAHDI_FLAG_HDLC;
4908 } else {
4909 /* Clear the HDLC flag */
4910 chan->flags &= ~DAHDI_FLAG_HDLC;
4911 }
4912 if ((ch.sigtype & DAHDI_SIG_HDLCFCS) == DAHDI_SIG_HDLCFCS) {
4913 /* Set FCS to be calculated if appropriate */
4914 chan->flags |= DAHDI_FLAG_FCS;
4915 } else {
4916 /* Clear FCS flag */
4917 chan->flags &= ~DAHDI_FLAG_FCS;
4918 }
4919 if ((ch.sigtype & __DAHDI_SIG_DACS) == __DAHDI_SIG_DACS) {
4920 if (unlikely(!dacs_chan)) {
4921 spin_unlock_irqrestore(&chan->lock, flags);
4922 chan_notice(chan, "%s: dacs but no dacs_chan\n",
4923 __func__);
4924 return -EINVAL;
4925 }
4926 /* Setup conference properly */
4927 chan->confmode = DAHDI_CONF_DIGITALMON;
4928 chan->confna = ch.idlebits;
4929 chan->dacs_chan = dacs_chan;
4930 res = dahdi_chan_dacs(chan, dacs_chan);
4931 } else {
4932 dahdi_disable_dacs(chan);
4933 }
4934 chan->master = newmaster;
4935 /* Note new slave if we are not our own master */
4936 if (newmaster != chan) {
4937 recalc_slaves(chan->master);
4938 }
4939 if ((ch.sigtype & DAHDI_SIG_HARDHDLC) == DAHDI_SIG_HARDHDLC) {
4940 chan->flags &= ~DAHDI_FLAG_FCS;
4941 chan->flags &= ~DAHDI_FLAG_HDLC;
4942 chan->flags |= DAHDI_FLAG_NOSTDTXRX;
4943 } else {
4944 chan->flags &= ~DAHDI_FLAG_NOSTDTXRX;
4945 }
4946
4947 if ((ch.sigtype & DAHDI_SIG_MTP2) == DAHDI_SIG_MTP2)
4948 chan->flags |= DAHDI_FLAG_MTP2;
4949 else
4950 chan->flags &= ~DAHDI_FLAG_MTP2;
4951 }
4952
4953 /* Chanconfig can block, do not call through the function pointer with
4954 * the channel lock held. */
4955 spin_unlock_irqrestore(&chan->lock, flags);
4956 if (!res && chan->span->ops->chanconfig)
4957 res = chan->span->ops->chanconfig(file, chan, ch.sigtype);
4958 spin_lock_irqsave(&chan->lock, flags);
4959
4960
4961 #ifdef CONFIG_DAHDI_NET
4962 if (!res &&
4963 (newmaster == chan) &&
4964 (chan->sig == DAHDI_SIG_HDLCNET)) {
4965 chan->hdlcnetdev = dahdi_hdlc_alloc();
4966 if (chan->hdlcnetdev) {
4967 /* struct hdlc_device *hdlc = chan->hdlcnetdev;
4968 struct net_device *d = hdlc_to_dev(hdlc); mmm...get it right later --byg */
4969
4970 chan->hdlcnetdev->netdev = alloc_hdlcdev(chan->hdlcnetdev);
4971 if (chan->hdlcnetdev->netdev) {
4972 chan->hdlcnetdev->chan = chan;
4973 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 23)
4974 SET_MODULE_OWNER(chan->hdlcnetdev->netdev);
4975 #endif
4976 chan->hdlcnetdev->netdev->tx_queue_len = 50;
4977 #ifdef HAVE_NET_DEVICE_OPS
4978 chan->hdlcnetdev->netdev->netdev_ops = &dahdi_netdev_ops;
4979 #else
4980 chan->hdlcnetdev->netdev->do_ioctl = dahdi_net_ioctl;
4981 chan->hdlcnetdev->netdev->open = dahdi_net_open;
4982 chan->hdlcnetdev->netdev->stop = dahdi_net_stop;
4983 #endif
4984 dev_to_hdlc(chan->hdlcnetdev->netdev)->attach = dahdi_net_attach;
4985 dev_to_hdlc(chan->hdlcnetdev->netdev)->xmit = dahdi_xmit;
4986 spin_unlock_irqrestore(&chan->lock, flags);
4987 /* Briefly restore interrupts while we register the device */
4988 res = dahdi_register_hdlc_device(chan->hdlcnetdev->netdev, ch.netdev_name);
4989 spin_lock_irqsave(&chan->lock, flags);
4990 } else {
4991 module_printk(KERN_NOTICE, "Unable to allocate hdlc: *shrug*\n");
4992 res = -1;
4993 }
4994 if (!res)
4995 set_bit(DAHDI_FLAGBIT_NETDEV, &chan->flags);
4996 } else {
4997 module_printk(KERN_NOTICE, "Unable to allocate netdev: out of memory\n");
4998 res = -1;
4999 }
5000 }
5001 #endif
5002 if ((chan->sig == DAHDI_SIG_HDLCNET) &&
5003 (chan == newmaster) &&
5004 !dahdi_have_netdev(chan))
5005 module_printk(KERN_NOTICE, "Unable to register HDLC device for channel %s\n", chan->name);
5006 if (!res) {
5007 /* Setup default law */
5008 chan->deflaw = ch.deflaw;
5009 /* Copy back any modified settings */
5010 spin_unlock_irqrestore(&chan->lock, flags);
5011 if (copy_to_user((void __user *)data, &ch, sizeof(ch)))
5012 return -EFAULT;
5013 spin_lock_irqsave(&chan->lock, flags);
5014 /* And hangup */
5015 dahdi_hangup(chan);
5016 y = dahdi_q_sig(chan) & 0xff;
5017 if (y >= 0)
5018 chan->rxsig = (unsigned char) y;
5019 chan->rxhooksig = DAHDI_RXSIG_INITIAL;
5020 }
5021 #ifdef CONFIG_DAHDI_DEBUG
5022 module_printk(KERN_NOTICE, "Configured channel %s, flags %04lx, sig %04x\n", chan->name, chan->flags, chan->sig);
5023 #endif
5024 spin_unlock_irqrestore(&chan->lock, flags);
5025
5026 return res;
5027 }
5028
5029 /**
5030 * dahdi_ioctl_set_dialparms - Set the global dial parameters.
5031 * @data: Pointer to user space that contains dahdi_dialparams.
5032 */
5033 static int dahdi_ioctl_set_dialparams(unsigned long data)
5034 {
5035 unsigned int i;
5036 struct dahdi_dialparams tdp;
5037 struct dahdi_zone *z;
5038 struct dahdi_dialparams *const gdp = &global_dialparams;
5039
5040 if (copy_from_user(&tdp, (void __user *)data, sizeof(tdp)))
5041 return -EFAULT;
5042
5043 mutex_lock(&global_dialparamslock);
5044
5045 if ((tdp.dtmf_tonelen >= 10) && (tdp.dtmf_tonelen <= 4000))
5046 gdp->dtmf_tonelen = tdp.dtmf_tonelen;
5047
5048 if ((tdp.mfv1_tonelen >= 10) && (tdp.mfv1_tonelen <= 4000))
5049 gdp->mfv1_tonelen = tdp.mfv1_tonelen;
5050
5051 if ((tdp.mfr2_tonelen >= 10) && (tdp.mfr2_tonelen <= 4000))
5052 gdp->mfr2_tonelen = tdp.mfr2_tonelen;
5053
5054 /* update the lengths in all currently loaded zones */
5055 spin_lock(&zone_lock);
5056 list_for_each_entry(z, &tone_zones, node) {
5057
5058 for (i = 0; i < ARRAY_SIZE(z->dtmf); i++) {
5059 z->dtmf[i].tonesamples =
5060 gdp->dtmf_tonelen * DAHDI_CHUNKSIZE;
5061 }
5062
5063 /* for MFR1, we only adjust the length of the digits */
5064 for (i = DAHDI_TONE_MFR1_0; i <= DAHDI_TONE_MFR1_9; i++) {
5065 z->mfr1[i - DAHDI_TONE_MFR1_BASE].tonesamples =
5066 gdp->mfv1_tonelen * DAHDI_CHUNKSIZE;
5067 }
5068
5069 for (i = 0; i < ARRAY_SIZE(z->mfr2_fwd); i++) {
5070 z->mfr2_fwd[i].tonesamples =
5071 gdp->mfr2_tonelen * DAHDI_CHUNKSIZE;
5072 }
5073
5074 for (i = 0; i < ARRAY_SIZE(z->mfr2_rev); i++) {
5075 z->mfr2_rev[i].tonesamples =
5076 gdp->mfr2_tonelen * DAHDI_CHUNKSIZE;
5077 }
5078 }
5079 spin_unlock(&zone_lock);
5080
5081 dtmf_silence.tonesamples = gdp->dtmf_tonelen * DAHDI_CHUNKSIZE;
5082 mfr1_silence.tonesamples = gdp->mfv1_tonelen * DAHDI_CHUNKSIZE;
5083 mfr2_silence.tonesamples = gdp->mfr2_tonelen * DAHDI_CHUNKSIZE;
5084
5085 mutex_unlock(&global_dialparamslock);
5086
5087 return 0;
5088 }
5089
5090 static int dahdi_ioctl_get_dialparams(unsigned long data)
5091 {
5092 struct dahdi_dialparams tdp;
5093
5094 mutex_lock(&global_dialparamslock);
5095 tdp = global_dialparams;
5096 mutex_unlock(&global_dialparamslock);
5097 if (copy_to_user((void __user *)data, &tdp, sizeof(tdp)))
5098 return -EFAULT;
5099 return 0;
5100 }
5101
5102 static int dahdi_ioctl_indirect(struct file *file, unsigned long data)
5103 {
5104 int res;
5105 struct dahdi_indirect_data ind;
5106 void *old;
5107 static bool warned;
5108 struct dahdi_chan *chan;
5109
5110 if (copy_from_user(&ind, (void __user *)data, sizeof(ind)))
5111 return -EFAULT;
5112
5113 chan = chan_from_num(ind.chan);
5114 if (!chan)
5115 return -EINVAL;
5116
5117 if (!warned) {
5118 warned = true;
5119 module_printk(KERN_WARNING, "Using deprecated " \
5120 "DAHDI_INDIRECT. Please update " \
5121 "dahdi-tools.\n");
5122 }
5123
5124 /* Since dahdi_chan_ioctl expects to be called on file handles
5125 * associated with channels, we'll temporarily set the
5126 * private_data pointer on the ctl file handle just for this
5127 * call. */
5128 old = file->private_data;
5129 file->private_data = chan;
5130 res = dahdi_chan_ioctl(file, ind.op, (unsigned long) ind.data);
5131 file->private_data = old;
5132 return res;
5133 }
5134
5135 static int dahdi_ioctl_spanconfig(struct file *file, unsigned long data)
5136 {
5137 int res = 0;
5138 struct dahdi_lineconfig lc;
5139 struct dahdi_span *s;
5140
5141 if (copy_from_user(&lc, (void __user *)data, sizeof(lc)))
5142 return -EFAULT;
5143 s = span_find_and_get(lc.span);
5144 if (!s)
5145 return -ENXIO;
5146
5147 if ((lc.lineconfig & 0x1ff0 & s->linecompat) !=
5148 (lc.lineconfig & 0x1ff0)) {
5149 put_span(s);
5150 return -EINVAL;
5151 }
5152 if (s->ops->spanconfig) {
5153 s->lineconfig = lc.lineconfig;
5154 s->lbo = lc.lbo;
5155 s->txlevel = lc.lbo;
5156 s->rxlevel = 0;
5157
5158 res = s->ops->spanconfig(file, s, &lc);
5159 }
5160 put_span(s);
5161 return res;
5162 }
5163
5164 static int dahdi_ioctl_startup(struct file *file, unsigned long data)
5165 {
5166 /* I/O CTL's for control interface */
5167 int j;
5168 int res = 0;
5169 int x, y;
5170 unsigned long flags;
5171 struct dahdi_span *s;
5172
5173 if (get_user(j, (int __user *)data))
5174 return -EFAULT;
5175 s = span_find_and_get(j);
5176 if (!s)
5177 return -ENXIO;
5178
5179 if (s->flags & DAHDI_FLAG_RUNNING) {
5180 put_span(s);
5181 return 0;
5182 }
5183
5184 if (s->ops->startup)
5185 res = s->ops->startup(file, s);
5186
5187 if (!res) {
5188 /* Mark as running and hangup any channels */
5189 s->flags |= DAHDI_FLAG_RUNNING;
5190 for (x = 0; x < s->channels; x++) {
5191 y = dahdi_q_sig(s->chans[x]) & 0xff;
5192 if (y >= 0)
5193 s->chans[x]->rxsig = (unsigned char)y;
5194 spin_lock_irqsave(&s->chans[x]->lock, flags);
5195 dahdi_hangup(s->chans[x]);
5196 spin_unlock_irqrestore(&s->chans[x]->lock, flags);
5197 /*
5198 * Set the rxhooksig back to
5199 * DAHDI_RXSIG_INITIAL so that new events are
5200 * queued on the channel with the actual
5201 * received hook state.
5202 *
5203 */
5204 s->chans[x]->rxhooksig = DAHDI_RXSIG_INITIAL;
5205 }
5206
5207 /* Now that this span is running, it might be selected as the
5208 * master_span */
5209 __dahdi_find_master_span();
5210 }
5211 put_span(s);
5212 return res;
5213 }
5214
5215 static int dahdi_shutdown_span(struct dahdi_span *s)
5216 {
5217 int res = 0;
5218 int x;
5219
5220 /* Unconfigure channels */
5221 for (x = 0; x < s->channels; x++)
5222 s->chans[x]->sig = 0;
5223
5224 if (s->ops->shutdown)
5225 res = s->ops->shutdown(s);
5226
5227 clear_bit(DAHDI_FLAGBIT_RUNNING, &s->flags);
5228 return res;
5229 }
5230
5231 static int dahdi_ioctl_shutdown(unsigned long data)
5232 {
5233 int res;
5234 /* I/O CTL's for control interface */
5235 int j;
5236
5237 struct dahdi_span *s;
5238
5239 if (get_user(j, (int __user *)data))
5240 return -EFAULT;
5241 s = span_find_and_get(j);
5242 if (!s)
5243 return -ENXIO;
5244 res = dahdi_shutdown_span(s);
5245 put_span(s);
5246 return res;
5247 }
5248
5249 /**
5250 * dahdi_is_hwec_available - Is hardware echocan available on a channel?
5251 * @chan: The channel to check
5252 *
5253 * Returns true if there is a hardware echocan available for the attached
5254 * channel, or false otherwise.
5255 *
5256 */
5257 static bool dahdi_is_hwec_available(const struct dahdi_chan *chan)
5258 {
5259 if (!chan->span || !chan->span->ops->echocan_name ||
5260 !hwec_factory.get_name(chan))
5261 return false;
5262
5263 return true;
5264 }
5265
5266 static int dahdi_ioctl_attach_echocan(unsigned long data)
5267 {
5268 struct dahdi_chan *chan;
5269 struct dahdi_attach_echocan ae;
5270 const struct dahdi_echocan_factory *new = NULL, *old;
5271
5272 if (copy_from_user(&ae, (void __user *)data, sizeof(ae)))
5273 return -EFAULT;
5274
5275 chan = chan_from_num(ae.chan);
5276 if (!chan)
5277 return -EINVAL;
5278
5279 ae.echocan[sizeof(ae.echocan) - 1] = '\0';
5280 if (dahdi_is_hwec_available(chan)) {
5281 if (hwec_overrides_swec) {
5282 chan_dbg(GENERAL, chan,
5283 "Using echocan '%s' instead of requested " \
5284 "'%s'.\n", hwec_def_name, ae.echocan);
5285 /* If there is a hardware echocan available we'll
5286 * always use it instead of any configured software
5287 * echocan. This matches the behavior in dahdi 2.4.1.2
5288 * and earlier releases. */
5289 strlcpy(ae.echocan, hwec_def_name, sizeof(ae.echocan));
5290
5291 } else if (strcasecmp(ae.echocan, hwec_def_name) != 0) {
5292 chan_dbg(GENERAL, chan,
5293 "Using '%s' on channel even though '%s' is " \
5294 "available.\n", ae.echocan, hwec_def_name);
5295 }
5296 }
5297
5298 if (ae.echocan[0]) {
5299 new = find_echocan(ae.echocan);
5300 if (!new)
5301 return -EINVAL;
5302
5303 if (!new->get_name(chan)) {
5304 release_echocan(new);
5305 return -EINVAL;
5306 }
5307 }
5308
5309 mutex_lock(&chan->mutex);
5310 old = chan->ec_factory;
5311 chan->ec_factory = new;
5312 mutex_unlock(&chan->mutex);
5313
5314 if (old)
5315 release_echocan(old);
5316
5317 return 0;
5318 }
5319
5320 static int dahdi_ioctl_sfconfig(unsigned long data)
5321 {
5322 int res = 0;
5323 unsigned long flags;
5324 struct dahdi_chan *chan;
5325 struct dahdi_sfconfig sf;
5326
5327 if (copy_from_user(&sf, (void __user *)data, sizeof(sf)))
5328 return -EFAULT;
5329 chan = chan_from_num(sf.chan);
5330 if (!chan)
5331 return -EINVAL;
5332
5333 if (chan->sig != DAHDI_SIG_SF)
5334 return -EINVAL;
5335
5336 spin_lock_irqsave(&chan->lock, flags);
5337 chan->rxp1 = sf.rxp1;
5338 chan->rxp2 = sf.rxp2;
5339 chan->rxp3 = sf.rxp3;
5340 chan->txtone = sf.txtone;
5341 chan->tx_v2 = sf.tx_v2;
5342 chan->tx_v3 = sf.tx_v3;
5343 chan->toneflags = sf.toneflag;
5344 if (sf.txtone) { /* if set to make tone for tx */
5345 if ((chan->txhooksig &&
5346 !(sf.toneflag & DAHDI_REVERSE_TXTONE)) ||
5347 ((!chan->txhooksig) &&
5348 (sf.toneflag & DAHDI_REVERSE_TXTONE))) {
5349 set_txtone(chan, sf.txtone, sf.tx_v2, sf.tx_v3);
5350 } else {
5351 set_txtone(chan, 0, 0, 0);
5352 }
5353 }
5354 spin_unlock_irqrestore(&chan->lock, flags);
5355 return res;
5356 }
5357
5358 /* Returns true if there are any hardware echocan on any span. */
5359 static bool dahdi_any_hwec_available(void)
5360 {
5361 int i;
5362 bool hwec_available = false;
5363 struct dahdi_span *s;
5364
5365 mutex_lock(&registration_mutex);
5366 list_for_each_entry(s, &span_list, spans_node) {
5367 for (i = 0; i < s->channels; ++i) {
5368 struct dahdi_chan *const chan = s->chans[i];
5369 if (dahdi_is_hwec_available(chan)) {
5370 hwec_available = true;
5371 break;
5372 }
5373 }
5374 }
5375 mutex_unlock(&registration_mutex);
5376
5377 return hwec_available;
5378 }
5379
5380 static int dahdi_ioctl_get_version(unsigned long data)
5381 {
5382 struct dahdi_versioninfo vi;
5383 struct ecfactory *cur;
5384 size_t space = sizeof(vi.echo_canceller) - 1;
5385 bool have_hwec = dahdi_any_hwec_available();
5386
5387 memset(&vi, 0, sizeof(vi));
5388 strlcpy(vi.version, dahdi_version, sizeof(vi.version));
5389 spin_lock(&ecfactory_list_lock);
5390 list_for_each_entry(cur, &ecfactory_list, list) {
5391 const char * const ec_name = cur->ec->get_name(NULL);
5392 if ((ec_name == hwec_def_name) && !have_hwec) {
5393 /*
5394 * The hardware echocan factory is always registered so
5395 * that hwec can be configured on the channels as if it
5396 * was a software echocan. However, it can be confusing
5397 * to list it as one of the available options in the
5398 * output of dahdi_cfg if there isn't a REAL hardware
5399 * echocanceler attached to any of the spans. In that
5400 * case, do not report the presence of the hardware
5401 * echocan factory to userspace.
5402 *
5403 */
5404 continue;
5405 }
5406 strncat(vi.echo_canceller + strlen(vi.echo_canceller),
5407 ec_name, space);
5408 space -= strlen(ec_name);
5409 if (space < 1)
5410 break;
5411 if (cur->list.next && (cur->list.next != &ecfactory_list)) {
5412 strncat(vi.echo_canceller + strlen(vi.echo_canceller),
5413 ", ", space);
5414 space -= 2;
5415 if (space < 1)
5416 break;
5417 }
5418 }
5419 spin_unlock(&ecfactory_list_lock);
5420 if (copy_to_user((void __user *)data, &vi, sizeof(vi)))
5421 return -EFAULT;
5422
5423 return 0;
5424 }
5425
5426 static int dahdi_ioctl_maint(unsigned long data)
5427 {
5428 int i;
5429 unsigned long flags;
5430 int rv;
5431 struct dahdi_span *s;
5432 struct dahdi_maintinfo maint;
5433
5434 /* get struct from user */
5435 if (copy_from_user(&maint, (void __user *)data, sizeof(maint)))
5436 return -EFAULT;
5437 s = span_find_and_get(maint.spanno);
5438 if (!s)
5439 return -EINVAL;
5440 if (!s->ops->maint) {
5441 put_span(s);
5442 return -ENOSYS;
5443 }
5444 spin_lock_irqsave(&s->lock, flags);
5445 /* save current maint state */
5446 i = s->maintstat;
5447 /* set maint mode */
5448 s->maintstat = maint.command;
5449 switch (maint.command) {
5450 case DAHDI_MAINT_NONE:
5451 case DAHDI_MAINT_LOCALLOOP:
5452 case DAHDI_MAINT_NETWORKLINELOOP:
5453 case DAHDI_MAINT_NETWORKPAYLOADLOOP:
5454 /* if same, ignore it */
5455 if (i == maint.command)
5456 break;
5457 rv = s->ops->maint(s, maint.command);
5458 spin_unlock_irqrestore(&s->lock, flags);
5459 if (rv) {
5460 put_span(s);
5461 /* Restore the state on error */
5462 s->maintstat = i;
5463 return rv;
5464 }
5465 spin_lock_irqsave(&s->lock, flags);
5466 break;
5467 case DAHDI_MAINT_LOOPUP:
5468 case DAHDI_MAINT_LOOPDOWN:
5469 s->mainttimer = DAHDI_LOOPCODE_TIME * DAHDI_CHUNKSIZE;
5470 rv = s->ops->maint(s, maint.command);
5471 spin_unlock_irqrestore(&s->lock, flags);
5472 if (rv) {
5473 put_span(s);
5474 /* Restore the state on error */
5475 s->maintstat = i;
5476 return rv;
5477 }
5478 spin_lock_irqsave(&s->lock, flags);
5479 break;
5480 case DAHDI_MAINT_FAS_DEFECT:
5481 case DAHDI_MAINT_MULTI_DEFECT:
5482 case DAHDI_MAINT_CRC_DEFECT:
5483 case DAHDI_MAINT_CAS_DEFECT:
5484 case DAHDI_MAINT_PRBS_DEFECT:
5485 case DAHDI_MAINT_BIPOLAR_DEFECT:
5486 case DAHDI_MAINT_PRBS:
5487 case DAHDI_RESET_COUNTERS:
5488 case DAHDI_MAINT_ALARM_SIM:
5489 /* Prevent notifying an alarm state for generic
5490 maintenance functions, unless the driver is
5491 already in a maint state */
5492 if (!i)
5493 s->maintstat = 0;
5494
5495 rv = s->ops->maint(s, maint.command);
5496 spin_unlock_irqrestore(&s->lock, flags);
5497 if (rv) {
5498 put_span(s);
5499 /* Restore the state on error */
5500 s->maintstat = i;
5501 return rv;
5502 }
5503 spin_lock_irqsave(&s->lock, flags);
5504 break;
5505 default:
5506 spin_unlock_irqrestore(&s->lock, flags);
5507 module_printk(KERN_NOTICE,
5508 "Unknown maintenance event: %d\n",
5509 maint.command);
5510 put_span(s);
5511 /* Restore the state on error */
5512 s->maintstat = i;
5513 return -ENOSYS;
5514 }
5515 dahdi_alarm_notify(s); /* process alarm-related events */
5516 spin_unlock_irqrestore(&s->lock, flags);
5517 put_span(s);
5518
5519 return 0;
5520 }
5521
5522 static int dahdi_ioctl_dynamic(unsigned int cmd, unsigned long data)
5523 {
5524 bool tried_load = false;
5525 int res;
5526
5527 retry_check:
5528 mutex_lock(&registration_mutex);
5529 if (!dahdi_dynamic_ops) {
5530 mutex_unlock(&registration_mutex);
5531 if (tried_load)
5532 return -ENOSYS;
5533
5534 request_module("dahdi_dynamic");
5535 tried_load = true;
5536 goto retry_check;
5537 }
5538 if (!try_module_get(dahdi_dynamic_ops->owner)) {
5539 mutex_unlock(&registration_mutex);
5540 return -ENOSYS;
5541 }
5542 mutex_unlock(&registration_mutex);
5543
5544 res = dahdi_dynamic_ops->ioctl(cmd, data);
5545 module_put(dahdi_dynamic_ops->owner);
5546 return res;
5547 }
5548
5549 static int
5550 dahdi_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long data)
5551 {
5552 switch (cmd) {
5553 case DAHDI_INDIRECT:
5554 return dahdi_ioctl_indirect(file, data);
5555 case DAHDI_SPANCONFIG:
5556 return dahdi_ioctl_spanconfig(file, data);
5557 case DAHDI_STARTUP:
5558 return dahdi_ioctl_startup(file, data);
5559 case DAHDI_SHUTDOWN:
5560 return dahdi_ioctl_shutdown(data);
5561 case DAHDI_ATTACH_ECHOCAN:
5562 return dahdi_ioctl_attach_echocan(data);
5563 case DAHDI_CHANCONFIG:
5564 return dahdi_ioctl_chanconfig(file, data);
5565 case DAHDI_SFCONFIG:
5566 return dahdi_ioctl_sfconfig(data);
5567 case DAHDI_DEFAULTZONE:
5568 return dahdi_ioctl_defaultzone(data);
5569 case DAHDI_LOADZONE:
5570 return dahdi_ioctl_loadzone(data);
5571 case DAHDI_FREEZONE:
5572 return dahdi_ioctl_freezone(data);
5573 case DAHDI_SET_DIALPARAMS:
5574 return dahdi_ioctl_set_dialparams(data);
5575 case DAHDI_GET_DIALPARAMS:
5576 return dahdi_ioctl_get_dialparams(data);
5577 case DAHDI_GETVERSION:
5578 return dahdi_ioctl_get_version(data);
5579 case DAHDI_MAINT:
5580 return dahdi_ioctl_maint(data);
5581 case DAHDI_DYNAMIC_CREATE:
5582 case DAHDI_DYNAMIC_DESTROY:
5583 return dahdi_ioctl_dynamic(cmd, data);
5584 case DAHDI_EC_LICENSE_CHALLENGE:
5585 case DAHDI_EC_LICENSE_RESPONSE:
5586 if (dahdi_hpec_ioctl) {
5587 return dahdi_hpec_ioctl(cmd, data);
5588 } else {
5589 request_module("dahdi_echocan_hpec");
5590 if (dahdi_hpec_ioctl)
5591 return dahdi_hpec_ioctl(cmd, data);
5592 }
5593 return -ENOSYS;
5594 }
5595
5596 return dahdi_common_ioctl(file, cmd, data);
5597 }
5598
5599 static int ioctl_dahdi_dial(struct dahdi_chan *chan, unsigned long data)
5600 {
5601 struct dahdi_dialoperation *tdo;
5602 unsigned long flags;
5603 char *s;
5604 int rv;
5605 void __user * const user_data = (void __user *)data;
5606
5607 tdo = kmalloc(sizeof(*tdo), GFP_KERNEL);
5608
5609 if (!tdo)
5610 return -ENOMEM;
5611
5612 if (copy_from_user(tdo, user_data, sizeof(*tdo)))
5613 return -EFAULT;
5614 rv = 0;
5615 /* Force proper NULL termination and uppercase entry */
5616 tdo->dialstr[DAHDI_MAX_DTMF_BUF - 1] = '\0';
5617 for (s = tdo->dialstr; *s; s++)
5618 *s = toupper(*s);
5619 spin_lock_irqsave(&chan->lock, flags);
5620 if (!chan->curzone) {
5621 spin_unlock_irqrestore(&chan->lock, flags);
5622 /* The tone zones are loaded by dahdi_cfg from /etc/dahdi/system.conf */
5623 module_printk(KERN_WARNING, "Cannot dial until a tone zone is loaded.\n");
5624 return -ENODATA;
5625 }
5626 switch (tdo->op) {
5627 case DAHDI_DIAL_OP_CANCEL:
5628 chan->curtone = NULL;
5629 chan->dialing = 0;
5630 chan->txdialbuf[0] = '\0';
5631 chan->tonep = 0;
5632 chan->pdialcount = 0;
5633 break;
5634 case DAHDI_DIAL_OP_REPLACE:
5635 strcpy(chan->txdialbuf, tdo->dialstr);
5636 chan->dialing = 1;
5637 __do_dtmf(chan);
5638 break;
5639 case DAHDI_DIAL_OP_APPEND:
5640 if (strlen(tdo->dialstr) + strlen(chan->txdialbuf) >= (DAHDI_MAX_DTMF_BUF - 1)) {
5641 rv = -EBUSY;
5642 break;
5643 }
5644 strlcpy(chan->txdialbuf + strlen(chan->txdialbuf), tdo->dialstr,
5645 DAHDI_MAX_DTMF_BUF - strlen(chan->txdialbuf));
5646 if (!chan->dialing) {
5647 chan->dialing = 1;
5648 __do_dtmf(chan);
5649 }
5650 break;
5651 default:
5652 rv = -EINVAL;
5653 }
5654 spin_unlock_irqrestore(&chan->lock, flags);
5655 return rv;
5656 }
5657
5658 static int dahdi_ioctl_setconf(struct file *file, unsigned long data)
5659 {
5660 struct dahdi_confinfo conf;
5661 struct dahdi_chan *chan;
5662 struct dahdi_chan *conf_chan = NULL;
5663 unsigned long flags;
5664 unsigned int confmode;
5665 int oldconf;
5666 enum {NONE, ENABLE_HWPREEC, DISABLE_HWPREEC} preec = NONE;
5667
5668 if (copy_from_user(&conf, (void __user *)data, sizeof(conf)))
5669 return -EFAULT;
5670
5671 confmode = conf.confmode & DAHDI_CONF_MODE_MASK;
5672
5673 chan = (conf.chan) ? chan_from_num(conf.chan) :
5674 chan_from_file(file);
5675 if (!chan)
5676 return -EINVAL;
5677
5678 if (!(chan->flags & DAHDI_FLAG_AUDIO))
5679 return -EINVAL;
5680
5681 if ((DAHDI_CONF_DIGITALMON == confmode) ||
5682 is_monitor_mode(conf.confmode)) {
5683 conf_chan = chan_from_num(conf.confno);
5684 if (!conf_chan)
5685 return -EINVAL;
5686 } else {
5687 /* make sure conf number makes sense, too */
5688 if ((conf.confno < -1) || (conf.confno > DAHDI_MAX_CONF))
5689 return -EINVAL;
5690 }
5691
5692 /* if taking off of any conf, must have 0 mode */
5693 if ((!conf.confno) && conf.confmode)
5694 return -EINVAL;
5695 /* likewise if 0 mode must have no conf */
5696 if ((!conf.confmode) && conf.confno)
5697 return -EINVAL;
5698 dahdi_check_conf(conf.confno);
5699 conf.chan = chan->channo; /* return with real channel # */
5700 spin_lock_irqsave(&chan_lock, flags);
5701 spin_lock(&chan->lock);
5702 if (conf.confno == -1)
5703 conf.confno = dahdi_first_empty_conference();
5704 if ((conf.confno < 1) && (conf.confmode)) {
5705 /* No more empty conferences */
5706 spin_unlock(&chan->lock);
5707 spin_unlock_irqrestore(&chan_lock, flags);
5708 return -EBUSY;
5709 }
5710 /* if changing confs, clear last added info */
5711 if (conf.confno != chan->confna) {
5712 memset(chan->conflast, 0, DAHDI_MAX_CHUNKSIZE);
5713 memset(chan->conflast1, 0, DAHDI_MAX_CHUNKSIZE);
5714 memset(chan->conflast2, 0, DAHDI_MAX_CHUNKSIZE);
5715 }
5716 oldconf = chan->confna; /* save old conference number */
5717 chan->confna = conf.confno; /* set conference number */
5718 chan->conf_chan = conf_chan;
5719 chan->confmode = conf.confmode; /* set conference mode */
5720 chan->_confn = 0; /* Clear confn */
5721 if (chan->span && chan->span->ops->dacs) {
5722 if ((confmode == DAHDI_CONF_DIGITALMON) &&
5723 (chan->txgain == defgain) &&
5724 (chan->rxgain == defgain) &&
5725 (conf_chan->txgain == defgain) &&
5726 (conf_chan->rxgain == defgain)) {
5727 dahdi_chan_dacs(chan, conf_chan);
5728 } else {
5729 dahdi_disable_dacs(chan);
5730 }
5731 }
5732 /* if we are going onto a conf */
5733 if (conf.confno &&
5734 (confmode == DAHDI_CONF_CONF ||
5735 confmode == DAHDI_CONF_CONFANN ||
5736 confmode == DAHDI_CONF_CONFMON ||
5737 confmode == DAHDI_CONF_CONFANNMON ||
5738 confmode == DAHDI_CONF_REALANDPSEUDO)) {
5739 /* Get alias */
5740 chan->_confn = dahdi_get_conf_alias(conf.confno);
5741 }
5742
5743 spin_unlock(&chan->lock);
5744
5745 if (conf_chan) {
5746 if ((confmode == DAHDI_CONF_MONITOR_RX_PREECHO) ||
5747 (confmode == DAHDI_CONF_MONITOR_TX_PREECHO) ||
5748 (confmode == DAHDI_CONF_MONITORBOTH_PREECHO)) {
5749 if (!conf_chan->readchunkpreec) {
5750 void *temp = kmalloc(sizeof(short) *
5751 DAHDI_CHUNKSIZE, GFP_ATOMIC);
5752 if (temp) {
5753 preec = ENABLE_HWPREEC;
5754
5755 spin_lock(&conf_chan->lock);
5756 conf_chan->readchunkpreec = temp;
5757 spin_unlock(&conf_chan->lock);
5758 }
5759 }
5760 } else {
5761 preec = DISABLE_HWPREEC;
5762
5763 spin_lock(&conf_chan->lock);
5764 kfree(conf_chan->readchunkpreec);
5765 conf_chan->readchunkpreec = NULL;
5766 spin_unlock(&conf_chan->lock);
5767 }
5768 }
5769
5770 spin_unlock_irqrestore(&chan_lock, flags);
5771
5772 if (ENABLE_HWPREEC == preec) {
5773 int res = dahdi_enable_hw_preechocan(conf_chan);
5774 if (res) {
5775 spin_lock_irqsave(&chan_lock, flags);
5776 spin_lock(&conf_chan->lock);
5777 kfree(conf_chan->readchunkpreec);
5778 conf_chan->readchunkpreec = NULL;
5779 spin_unlock(&conf_chan->lock);
5780 spin_unlock_irqrestore(&chan_lock, flags);
5781 }
5782 return res;
5783 } else if (DISABLE_HWPREEC == preec) {
5784 dahdi_disable_hw_preechocan(conf_chan);
5785 }
5786
5787 dahdi_check_conf(oldconf);
5788 if (copy_to_user((void __user *)data, &conf, sizeof(conf)))
5789 return -EFAULT;
5790 return 0;
5791 }
5792
5793 /**
5794 * dahdi_ioctl_confdiag() - Output debug info about conferences to console.
5795 *
5796 * This is a pure debugging aide since the only result is to the console.
5797 *
5798 * TODO: Does anyone use this anymore? Should it be hidden behind a debug
5799 * compile time option?
5800 */
5801 static int dahdi_ioctl_confdiag(struct file *file, unsigned long data)
5802 {
5803 struct dahdi_chan *chan;
5804 unsigned long flags;
5805 int i;
5806 int j;
5807 int c;
5808
5809 chan = chan_from_file(file);
5810 if (!chan)
5811 return -EINVAL;
5812
5813 if (!(chan->flags & DAHDI_FLAG_AUDIO))
5814 return -EINVAL;
5815
5816 get_user(j, (int __user *)data); /* get conf # */
5817
5818 /* loop thru the interesting ones */
5819 for (i = ((j) ? j : 1); i <= ((j) ? j : DAHDI_MAX_CONF); i++) {
5820 struct dahdi_span *s;
5821 struct pseudo_chan *pseudo;
5822 int k;
5823 c = 0;
5824 spin_lock_irqsave(&chan_lock, flags);
5825 list_for_each_entry(s, &span_list, spans_node) {
5826 for (k = 0; k < s->channels; k++) {
5827 chan = s->chans[k];
5828 if (chan->confna != i)
5829 continue;
5830 if (!c)
5831 module_printk(KERN_NOTICE, "Conf #%d:\n", i);
5832 c = 1;
5833 module_printk(KERN_NOTICE, "chan %d, mode %x\n",
5834 chan->channo, chan->confmode);
5835 }
5836 }
5837 list_for_each_entry(pseudo, &pseudo_chans, node) {
5838 /* skip if not in this conf */
5839 if (pseudo->chan.confna != i)
5840 continue;
5841 if (!c)
5842 module_printk(KERN_NOTICE, "Conf #%d:\n", i);
5843 c = 1;
5844 module_printk(KERN_NOTICE, "chan %d, mode %x\n",
5845 pseudo->chan.channo, pseudo->chan.confmode);
5846 }
5847
5848 #ifdef CONFIG_DAHDI_CONFLINK
5849 {
5850 int rv;
5851 rv = 0;
5852 for (k = 1; k <= DAHDI_MAX_CONF; k++) {
5853 if (conf_links[k].dst == i) {
5854 if (!c) {
5855 c = 1;
5856 module_printk(KERN_NOTICE,
5857 "Conf #%d:\n", i);
5858 }
5859 if (!rv) {
5860 rv = 1;
5861 module_printk(KERN_NOTICE,
5862 "Snooping on:\n");
5863 }
5864 module_printk(KERN_NOTICE, "conf %d\n",
5865 conf_links[k].src);
5866 }
5867 }
5868 }
5869 #endif
5870 spin_unlock_irqrestore(&chan_lock, flags);
5871 if (c)
5872 module_printk(KERN_NOTICE, "\n");
5873 }
5874 return 0;
5875 }
5876
5877 static int dahdi_ioctl_getconf(struct file *file, unsigned long data)
5878 {
5879 struct dahdi_confinfo conf;
5880 struct dahdi_chan *chan;
5881
5882 if (copy_from_user(&conf, (void __user *)data, sizeof(conf)))
5883 return -EFAULT;
5884
5885 chan = (!conf.chan) ? chan_from_file(file) :
5886 chan_from_num(conf.chan);
5887 if (!chan)
5888 return -EINVAL;
5889
5890 if (!(chan->flags & DAHDI_FLAG_AUDIO))
5891 return -EINVAL;
5892 conf.chan = chan->channo; /* get channel number */
5893 conf.confno = chan->confna; /* get conference number */
5894 conf.confmode = chan->confmode; /* get conference mode */
5895 if (copy_to_user((void __user *)data, &conf, sizeof(conf)))
5896 return -EFAULT;
5897
5898 return 0;
5899 }
5900
5901 /**
5902 * dahdi_ioctl_iomux() - Wait for *something* to happen.
5903 *
5904 * This is now basically like the wait_event_interruptible function, but with
5905 * a much more involved wait condition.
5906 */
5907 static int dahdi_ioctl_iomux(struct file *file, unsigned long data)
5908 {
5909 struct dahdi_chan *const chan = chan_from_file(file);
5910 unsigned long flags;
5911 unsigned int iomask;
5912 int ret = 0;
5913 DEFINE_WAIT(wait);
5914
5915 if (get_user(iomask, (int __user *)data))
5916 return -EFAULT;
5917
5918 if (unlikely(!iomask || !chan))
5919 return -EINVAL;
5920
5921 while (1) {
5922 unsigned int wait_result;
5923
5924 wait_result = 0;
5925 prepare_to_wait(&chan->waitq, &wait, TASK_INTERRUPTIBLE);
5926 if (unlikely(!chan->file->private_data)) {
5927 /*
5928 * This should never happen. Surprise device removal
5929 * should lead us to the nodev_* file_operations
5930 */
5931 msleep(5);
5932 module_printk(KERN_ERR, "%s: NODEV\n", __func__);
5933 ret = -ENODEV;
5934 break;
5935 }
5936
5937 spin_lock_irqsave(&chan->lock, flags);
5938 chan->iomask = iomask;
5939 if (iomask & DAHDI_IOMUX_READ) {
5940 if (chan->outreadbuf > -1)
5941 wait_result |= DAHDI_IOMUX_READ;
5942 }
5943 if (iomask & DAHDI_IOMUX_WRITE) {
5944 if (chan->inwritebuf > -1)
5945 wait_result |= DAHDI_IOMUX_WRITE;
5946 }
5947 if (iomask & DAHDI_IOMUX_WRITEEMPTY) {
5948 /* if everything empty -- be sure the transmitter is
5949 * enabled */
5950 chan->txdisable = 0;
5951 if (chan->outwritebuf < 0)
5952 wait_result |= DAHDI_IOMUX_WRITEEMPTY;
5953 }
5954 if (iomask & DAHDI_IOMUX_SIGEVENT) {
5955 if (chan->eventinidx != chan->eventoutidx)
5956 wait_result |= DAHDI_IOMUX_SIGEVENT;
5957 }
5958 spin_unlock_irqrestore(&chan->lock, flags);
5959
5960 if (wait_result || (iomask & DAHDI_IOMUX_NOWAIT)) {
5961 put_user(wait_result, (int __user *)data);
5962 break;
5963 }
5964
5965 if (signal_pending(current)) {
5966 finish_wait(&chan->waitq, &wait);
5967 return -ERESTARTSYS;
5968 }
5969
5970 schedule();
5971 }
5972
5973 finish_wait(&chan->waitq, &wait);
5974 spin_lock_irqsave(&chan->lock, flags);
5975 chan->iomask = 0;
5976 spin_unlock_irqrestore(&chan->lock, flags);
5977 return ret;
5978 }
5979
5980 #ifdef CONFIG_DAHDI_MIRROR
5981 static int dahdi_ioctl_rxmirror(struct file *file, unsigned long data)
5982 {
5983 int res;
5984 int i;
5985 unsigned long flags;
5986 struct dahdi_chan *const chan = chan_from_file(file);
5987 struct dahdi_chan *srcmirror;
5988
5989 if (!chan || chan->srcmirror)
5990 return -ENODEV;
5991
5992 res = get_user(i, (int __user *)data);
5993 if (res)
5994 return res;
5995
5996 srcmirror = chan_from_num(i);
5997 if (!srcmirror)
5998 return -EINVAL;
5999
6000 module_printk(KERN_INFO, "Chan %d rx mirrored to %d\n",
6001 srcmirror->channo, chan->channo);
6002
6003 spin_lock_irqsave(&srcmirror->lock, flags);
6004 if (srcmirror->rxmirror == NULL)
6005 srcmirror->rxmirror = chan;
6006
6007 spin_unlock_irqrestore(&srcmirror->lock, flags);
6008 if (srcmirror->rxmirror != chan) {
6009 module_printk(KERN_INFO, "Chan %d cannot be rxmirrored, " \
6010 "already in use\n", srcmirror->channo);
6011 return -EFAULT;
6012 }
6013
6014 spin_lock_irqsave(&chan->lock, flags);
6015 chan->srcmirror = srcmirror;
6016 chan->flags = srcmirror->flags;
6017 chan->sig = srcmirror->sig;
6018 clear_bit(DAHDI_FLAGBIT_OPEN, &chan->flags);
6019 spin_unlock_irqrestore(&chan->lock, flags);
6020
6021 return 0;
6022 }
6023
6024 static int dahdi_ioctl_txmirror(struct file *file, unsigned long data)
6025 {
6026 int res;
6027 int i;
6028 unsigned long flags;
6029 struct dahdi_chan *const chan = chan_from_file(file);
6030 struct dahdi_chan *srcmirror;
6031
6032 if (!chan || chan->srcmirror)
6033 return -ENODEV;
6034
6035 res = get_user(i, (int __user *)data);
6036 if (res)
6037 return res;
6038
6039 srcmirror = chan_from_num(i);
6040 if (!srcmirror)
6041 return -EINVAL;
6042
6043 module_printk(KERN_INFO, "Chan %d tx mirrored to %d\n",
6044 srcmirror->channo, chan->channo);
6045
6046 spin_lock_irqsave(&srcmirror->lock, flags);
6047 srcmirror->txmirror = chan;
6048 if (srcmirror->txmirror == NULL)
6049 srcmirror->txmirror = chan;
6050 spin_unlock_irqrestore(&srcmirror->lock, flags);
6051
6052 if (srcmirror->txmirror != chan) {
6053 module_printk(KERN_INFO, "Chan %d cannot be txmirrored, " \
6054 "already in use\n", i);
6055 return -EFAULT;
6056 }
6057
6058 spin_lock_irqsave(&chan->lock, flags);
6059 chan->srcmirror = srcmirror;
6060 chan->flags = srcmirror->flags;
6061 chan->sig = srcmirror->sig;
6062 clear_bit(DAHDI_FLAGBIT_OPEN, &chan->flags);
6063 spin_unlock_irqrestore(&chan->lock, flags);
6064
6065 return 0;
6066 }
6067 #endif /* CONFIG_DAHDI_MIRROR */
6068
6069 static int
6070 dahdi_chanandpseudo_ioctl(struct file *file, unsigned int cmd,
6071 unsigned long data)
6072 {
6073 struct dahdi_chan *chan = chan_from_file(file);
6074 union {
6075 struct dahdi_bufferinfo bi;
6076 struct dahdi_ring_cadence cad;
6077 } stack;
6078 unsigned long flags;
6079 int i, j, rv;
6080 void __user * const user_data = (void __user *)data;
6081
6082 if (!chan)
6083 return -EINVAL;
6084 switch(cmd) {
6085 #ifdef CONFIG_DAHDI_MIRROR
6086 case DAHDI_RXMIRROR:
6087 return dahdi_ioctl_rxmirror(file, data);
6088
6089 case DAHDI_TXMIRROR:
6090 return dahdi_ioctl_txmirror(file, data);
6091 #endif /* CONFIG_DAHDI_MIRROR */
6092
6093 case DAHDI_DIALING:
6094 spin_lock_irqsave(&chan->lock, flags);
6095 j = chan->dialing;
6096 spin_unlock_irqrestore(&chan->lock, flags);
6097 if (copy_to_user(user_data, &j, sizeof(int)))
6098 return -EFAULT;
6099 return 0;
6100 case DAHDI_DIAL:
6101 return ioctl_dahdi_dial(chan, data);
6102 case DAHDI_GET_BUFINFO:
6103 memset(&stack.bi, 0, sizeof(stack.bi));
6104 stack.bi.rxbufpolicy = DAHDI_POLICY_IMMEDIATE;
6105 stack.bi.txbufpolicy = chan->txbufpolicy;
6106 stack.bi.numbufs = chan->numbufs;
6107 stack.bi.bufsize = chan->blocksize;
6108 /* XXX FIXME! XXX */
6109 stack.bi.readbufs = -1;
6110 stack.bi.writebufs = -1;
6111 if (copy_to_user(user_data, &stack.bi, sizeof(stack.bi)))
6112 return -EFAULT;
6113 break;
6114 case DAHDI_SET_BUFINFO:
6115 if (copy_from_user(&stack.bi, user_data, sizeof(stack.bi)))
6116 return -EFAULT;
6117 if (stack.bi.bufsize > DAHDI_MAX_BLOCKSIZE)
6118 return -EINVAL;
6119 if (stack.bi.bufsize < 16)
6120 return -EINVAL;
6121 if (stack.bi.bufsize * stack.bi.numbufs > DAHDI_MAX_BUF_SPACE)
6122 return -EINVAL;
6123 /* It does not make sense to allow user mode to change the
6124 * receive buffering policy. DAHDI always provides received
6125 * buffers to upper layers immediately. Transmission is
6126 * different since we might want to allow the kernel to build
6127 * up a buffer in order to prevent underruns from the
6128 * interrupt context. */
6129 chan->txbufpolicy = stack.bi.txbufpolicy & 0x3;
6130 if ((rv = dahdi_reallocbufs(chan, stack.bi.bufsize, stack.bi.numbufs)))
6131 return (rv);
6132 break;
6133 case DAHDI_GET_BLOCKSIZE: /* get blocksize */
6134 /* return block size */
6135 put_user(chan->blocksize, (int __user *)data);
6136 break;
6137 case DAHDI_SET_BLOCKSIZE: /* set blocksize */
6138 get_user(j, (int __user *)data);
6139 /* cannot be larger than max amount */
6140 if (j > DAHDI_MAX_BLOCKSIZE) return(-EINVAL);
6141 /* cannot be less then 16 */
6142 if (j < 16) return(-EINVAL);
6143 /* allocate a single kernel buffer which we then
6144 sub divide into four pieces */
6145 if ((rv = dahdi_reallocbufs(chan, j, chan->numbufs)))
6146 return (rv);
6147 break;
6148 case DAHDI_FLUSH: /* flush input buffer, output buffer, and/or event queue */
6149 get_user(i, (int __user *)data); /* get param */
6150 spin_lock_irqsave(&chan->lock, flags);
6151 if (i & DAHDI_FLUSH_READ) /* if for read (input) */
6152 {
6153 /* initialize read buffers and pointers */
6154 chan->inreadbuf = 0;
6155 chan->outreadbuf = -1;
6156 for (j=0;j<chan->numbufs;j++) {
6157 /* Do we need this? */
6158 chan->readn[j] = 0;
6159 chan->readidx[j] = 0;
6160 }
6161 wake_up_interruptible(&chan->waitq); /* wake_up_interruptible waiting on read */
6162 }
6163 if (i & DAHDI_FLUSH_WRITE) /* if for write (output) */
6164 {
6165 /* initialize write buffers and pointers */
6166 chan->outwritebuf = -1;
6167 chan->inwritebuf = 0;
6168 for (j=0;j<chan->numbufs;j++) {
6169 /* Do we need this? */
6170 chan->writen[j] = 0;
6171 chan->writeidx[j] = 0;
6172 }
6173 wake_up_interruptible(&chan->waitq); /* wake_up_interruptible waiting on write */
6174 }
6175 if (i & DAHDI_FLUSH_EVENT) /* if for events */
6176 {
6177 /* initialize the event pointers */
6178 chan->eventinidx = chan->eventoutidx = 0;
6179 }
6180 spin_unlock_irqrestore(&chan->lock, flags);
6181 break;
6182 case DAHDI_SYNC: /* wait for no tx */
6183 for(;;) /* loop forever */
6184 {
6185 spin_lock_irqsave(&chan->lock, flags);
6186 /* Know if there is a write pending */
6187 i = (chan->outwritebuf > -1);
6188 spin_unlock_irqrestore(&chan->lock, flags);
6189 if (!i)
6190 break; /* skip if none */
6191 rv = wait_event_interruptible(chan->waitq,
6192 (!chan->file->private_data || chan->outwritebuf > -1));
6193 if (rv)
6194 return rv;
6195 if (unlikely(!chan->file->private_data))
6196 return -ENODEV;
6197 }
6198 break;
6199 case DAHDI_IOMUX: /* wait for something to happen */
6200 return dahdi_ioctl_iomux(file, data);
6201
6202 case DAHDI_GETEVENT: /* Get event on queue */
6203 /* set up for no event */
6204 j = DAHDI_EVENT_NONE;
6205 spin_lock_irqsave(&chan->lock, flags);
6206 /* if some event in queue */
6207 if (chan->eventinidx != chan->eventoutidx)
6208 {
6209 j = chan->eventbuf[chan->eventoutidx++];
6210 /* get the data, bump index */
6211 /* if index overflow, set to beginning */
6212 if (chan->eventoutidx >= DAHDI_MAX_EVENTSIZE)
6213 chan->eventoutidx = 0;
6214 }
6215 spin_unlock_irqrestore(&chan->lock, flags);
6216 put_user(j, (int __user *)data);
6217 break;
6218 case DAHDI_CONFMUTE: /* set confmute flag */
6219 get_user(j, (int __user *)data); /* get conf # */
6220 if (!(chan->flags & DAHDI_FLAG_AUDIO)) return (-EINVAL);
6221 spin_lock_irqsave(&chan_lock, flags);
6222 chan->confmute = j;
6223 spin_unlock_irqrestore(&chan_lock, flags);
6224 break;
6225 case DAHDI_GETCONFMUTE: /* get confmute flag */
6226 if (!(chan->flags & DAHDI_FLAG_AUDIO)) return (-EINVAL);
6227 j = chan->confmute;
6228 put_user(j, (int __user *)data); /* get conf # */
6229 rv = 0;
6230 break;
6231 case DAHDI_SETTONEZONE:
6232 get_user(j, (int __user *) data);
6233 rv = set_tone_zone(chan, j);
6234 return rv;
6235 case DAHDI_GETTONEZONE:
6236 spin_lock_irqsave(&chan->lock, flags);
6237 j = (chan->curzone) ? chan->curzone->num : 0;
6238 spin_unlock_irqrestore(&chan->lock, flags);
6239 put_user(j, (int __user *) data);
6240 break;
6241 case DAHDI_SENDTONE:
6242 get_user(j, (int __user *)data);
6243 spin_lock_irqsave(&chan->lock, flags);
6244 rv = start_tone(chan, j);
6245 spin_unlock_irqrestore(&chan->lock, flags);
6246 return rv;
6247 case DAHDI_GETCONF_V1: /* intentional drop through */
6248 case DAHDI_GETCONF: /* get conf stuff */
6249 return dahdi_ioctl_getconf(file, data);
6250
6251 case DAHDI_SETCONF_V1: /* Intentional fall through. */
6252 case DAHDI_SETCONF: /* set conf stuff */
6253 return dahdi_ioctl_setconf(file, data);
6254
6255 case DAHDI_CONFDIAG_V1: /* Intentional fall-through */
6256 case DAHDI_CONFDIAG: /* output diagnostic info to console */
6257 return dahdi_ioctl_confdiag(file, data);
6258
6259 case DAHDI_CHANNO: /* get channel number of stream */
6260 /* return channel number */
6261 put_user(chan->channo, (int __user *)data);
6262 break;
6263 case DAHDI_SETLAW:
6264 get_user(j, (int __user *)data);
6265 if ((j < 0) || (j > DAHDI_LAW_ALAW))
6266 return -EINVAL;
6267 dahdi_set_law(chan, j);
6268 break;
6269 case DAHDI_SETLINEAR:
6270 get_user(j, (int __user *)data);
6271 /* Makes no sense on non-audio channels */
6272 if (!(chan->flags & DAHDI_FLAG_AUDIO))
6273 return -EINVAL;
6274
6275 if (j)
6276 chan->flags |= DAHDI_FLAG_LINEAR;
6277 else
6278 chan->flags &= ~DAHDI_FLAG_LINEAR;
6279 break;
6280 case DAHDI_SETCADENCE:
6281 if (data) {
6282 /* Use specific ring cadence */
6283 if (copy_from_user(&stack.cad, user_data,
6284 sizeof(stack.cad))) {
6285 return -EFAULT;
6286 }
6287 memcpy(chan->ringcadence, &stack.cad, sizeof(chan->ringcadence));
6288 chan->firstcadencepos = 0;
6289 /* Looking for negative ringing time indicating where to loop back into ringcadence */
6290 for (i=0; i<DAHDI_MAX_CADENCE; i+=2 ) {
6291 if (chan->ringcadence[i]<0) {
6292 chan->ringcadence[i] *= -1;
6293 chan->firstcadencepos = i;
6294 break;
6295 }
6296 }
6297 } else {
6298 /* Reset to default */
6299 chan->firstcadencepos = 0;
6300 if (chan->curzone) {
6301 memcpy(chan->ringcadence, chan->curzone->ringcadence, sizeof(chan->ringcadence));
6302 /* Looking for negative ringing time indicating where to loop back into ringcadence */
6303 for (i=0; i<DAHDI_MAX_CADENCE; i+=2 ) {
6304 if (chan->ringcadence[i]<0) {
6305 chan->ringcadence[i] *= -1;
6306 chan->firstcadencepos = i;
6307 break;
6308 }
6309 }
6310 } else {
6311 memset(chan->ringcadence, 0, sizeof(chan->ringcadence));
6312 chan->ringcadence[0] = chan->starttime;
6313 chan->ringcadence[1] = DAHDI_RINGOFFTIME;
6314 }
6315 }
6316 break;
6317 default:
6318 /* Check for common ioctl's and private ones */
6319 rv = dahdi_common_ioctl(file, cmd, data);
6320 /* if no span, just return with value */
6321 if (!chan->span) return rv;
6322 if ((rv == -ENOTTY) && chan->span->ops->ioctl)
6323 rv = chan->span->ops->ioctl(chan, cmd, data);
6324 return rv;
6325
6326 }
6327 return 0;
6328 }
6329
6330 #ifdef CONFIG_DAHDI_PPP
6331 /*
6332 * This is called at softirq (BH) level when there are calls
6333 * we need to make to the ppp_generic layer. We do it this
6334 * way because the ppp_generic layer functions may not be called
6335 * at interrupt level.
6336 */
6337 static void do_ppp_calls(unsigned long data)
6338 {
6339 struct dahdi_chan *chan = (struct dahdi_chan *) data;
6340 struct sk_buff *skb;
6341
6342 if (!chan->ppp)
6343 return;
6344 if (chan->do_ppp_wakeup) {
6345 chan->do_ppp_wakeup = 0;
6346 ppp_output_wakeup(chan->ppp);
6347 }
6348 while ((skb = skb_dequeue(&chan->ppp_rq)) != NULL)
6349 ppp_input(chan->ppp, skb);
6350 if (chan->do_ppp_error) {
6351 chan->do_ppp_error = 0;
6352 ppp_input_error(chan->ppp, 0);
6353 }
6354 }
6355 #endif
6356
6357 static int
6358 ioctl_echocancel(struct dahdi_chan *chan, struct dahdi_echocanparams *ecp,
6359 const void __user *data)
6360 {
6361 struct dahdi_echocan_state *ec = NULL, *ec_state;
6362 const struct dahdi_echocan_factory *ec_current;
6363 struct dahdi_echocanparam *params;
6364 int ret = 0;
6365 unsigned long flags;
6366
6367 if (ecp->param_count > DAHDI_MAX_ECHOCANPARAMS)
6368 return -E2BIG;
6369
6370 if (ecp->tap_length == 0) {
6371 /* disable mode, don't need to inspect params */
6372 mutex_lock(&chan->mutex);
6373 spin_lock_irqsave(&chan->lock, flags);
6374 ec_state = chan->ec_state;
6375 chan->ec_state = NULL;
6376 ec_current = chan->ec_current;
6377 chan->ec_current = NULL;
6378 spin_unlock_irqrestore(&chan->lock, flags);
6379 if (ec_state) {
6380 ec_state->ops->echocan_free(chan, ec_state);
6381 release_echocan(ec_current);
6382 }
6383 mutex_unlock(&chan->mutex);
6384 return 0;
6385 }
6386
6387 params = kmalloc(sizeof(params[0]) * DAHDI_MAX_ECHOCANPARAMS, GFP_KERNEL);
6388
6389 if (!params)
6390 return -ENOMEM;
6391
6392 /* enable mode, need the params */
6393
6394 if (copy_from_user(params, data,
6395 sizeof(params[0]) * ecp->param_count)) {
6396 ret = -EFAULT;
6397 goto exit_with_free;
6398 }
6399
6400 mutex_lock(&chan->mutex);
6401 /* free any echocan that may be on the channel already */
6402 spin_lock_irqsave(&chan->lock, flags);
6403 ec_state = chan->ec_state;
6404 chan->ec_state = NULL;
6405 ec_current = chan->ec_current;
6406 chan->ec_current = NULL;
6407 spin_unlock_irqrestore(&chan->lock, flags);
6408 if (ec_state) {
6409 ec_state->ops->echocan_free(chan, ec_state);
6410 release_echocan(ec_current);
6411 }
6412
6413 switch (ecp->tap_length) {
6414 case 32:
6415 case 64:
6416 case 128:
6417 case 256:
6418 case 512:
6419 case 1024:
6420 break;
6421 default:
6422 ecp->tap_length = deftaps;
6423 }
6424
6425 ec_current = NULL;
6426
6427 if (chan->ec_factory) {
6428 /* try to get another reference to the module providing
6429 this channel's echo canceler */
6430 if (!try_module_get(chan->ec_factory->owner)) {
6431 module_printk(KERN_ERR, "Cannot get a reference to the"
6432 " '%s' echo canceler\n",
6433 chan->ec_factory->get_name(chan));
6434 goto exit_with_free;
6435 }
6436
6437 /* got the reference, copy the pointer and use it for making
6438 an echo canceler instance if possible */
6439 ec_current = chan->ec_factory;
6440
6441 ret = ec_current->echocan_create(chan, ecp, params, &ec);
6442 if (ret) {
6443 release_echocan(ec_current);
6444
6445 goto exit_with_free;
6446 }
6447 if (!ec) {
6448 module_printk(KERN_ERR, "%s failed to allocate an " \
6449 "dahdi_echocan_state instance.\n",
6450 ec_current->get_name(chan));
6451 ret = -EFAULT;
6452 goto exit_with_free;
6453 }
6454 }
6455
6456 if (ec) {
6457 spin_lock_irqsave(&chan->lock, flags);
6458 chan->ec_current = ec_current;
6459 chan->ec_state = ec;
6460 ec->status.mode = ECHO_MODE_ACTIVE;
6461 if (!ec->features.CED_tx_detect) {
6462 echo_can_disable_detector_init(&chan->ec_state->txecdis);
6463 }
6464 if (!ec->features.CED_rx_detect) {
6465 echo_can_disable_detector_init(&chan->ec_state->rxecdis);
6466 }
6467 spin_unlock_irqrestore(&chan->lock, flags);
6468 }
6469
6470 exit_with_free:
6471 mutex_unlock(&chan->mutex);
6472 kfree(params);
6473
6474 return ret;
6475 }
6476
6477 static void set_echocan_fax_mode(struct dahdi_chan *chan, unsigned int channo, const char *reason, unsigned int enable)
6478 {
6479 if (enable) {
6480 if (!chan->ec_state)
6481 module_printk(KERN_NOTICE, "Ignoring FAX mode request because of %s for channel %d with no echo canceller\n", reason, channo);
6482 else if (chan->ec_state->status.mode == ECHO_MODE_FAX)
6483 module_printk(KERN_NOTICE, "Ignoring FAX mode request because of %s for echo canceller already in FAX mode on channel %d\n", reason, channo);
6484 else if (chan->ec_state->status.mode != ECHO_MODE_ACTIVE)
6485 module_printk(KERN_NOTICE, "Ignoring FAX mode request because of %s for echo canceller not in active mode on channel %d\n", reason, channo);
6486 else if (chan->ec_state->features.NLP_automatic) {
6487 /* for echocans that automatically do the right thing, just
6488 * mark it as being in FAX mode without making any
6489 * changes, as none are necessary.
6490 */
6491 chan->ec_state->status.mode = ECHO_MODE_FAX;
6492 } else if (chan->ec_state->features.NLP_toggle) {
6493 module_printk(KERN_NOTICE, "Disabled echo canceller NLP because of %s on channel %d\n", reason, channo);
6494 dahdi_qevent_nolock(chan, DAHDI_EVENT_EC_NLP_DISABLED);
6495 chan->ec_state->ops->echocan_NLP_toggle(chan->ec_state, 0);
6496 chan->ec_state->status.mode = ECHO_MODE_FAX;
6497 } else {
6498 module_printk(KERN_NOTICE, "Idled echo canceller because of %s on channel %d\n", reason, channo);
6499 chan->ec_state->status.mode = ECHO_MODE_IDLE;
6500 }
6501 } else {
6502 if (!chan->ec_state)
6503 module_printk(KERN_NOTICE, "Ignoring voice mode request because of %s for channel %d with no echo canceller\n", reason, channo);
6504 else if (chan->ec_state->status.mode == ECHO_MODE_ACTIVE)
6505 module_printk(KERN_NOTICE, "Ignoring voice mode request because of %s for echo canceller already in voice mode on channel %d\n", reason, channo);
6506 else if ((chan->ec_state->status.mode != ECHO_MODE_FAX) &&
6507 (chan->ec_state->status.mode != ECHO_MODE_IDLE))
6508 module_printk(KERN_NOTICE, "Ignoring voice mode request because of %s for echo canceller not in FAX or idle mode on channel %d\n", reason, channo);
6509 else if (chan->ec_state->features.NLP_automatic) {
6510 /* for echocans that automatically do the right thing, just
6511 * mark it as being in active mode without making any
6512 * changes, as none are necessary.
6513 */
6514 chan->ec_state->status.mode = ECHO_MODE_ACTIVE;
6515 } else if (chan->ec_state->features.NLP_toggle) {
6516 module_printk(KERN_NOTICE, "Enabled echo canceller NLP because of %s on channel %d\n", reason, channo);
6517 dahdi_qevent_nolock(chan, DAHDI_EVENT_EC_NLP_ENABLED);
6518 chan->ec_state->ops->echocan_NLP_toggle(chan->ec_state, 1);
6519 chan->ec_state->status.mode = ECHO_MODE_ACTIVE;
6520 } else {
6521 module_printk(KERN_NOTICE, "Activated echo canceller because of %s on channel %d\n", reason, channo);
6522 chan->ec_state->status.mode = ECHO_MODE_ACTIVE;
6523 }
6524 }
6525 }
6526
6527 static inline bool
6528 is_txstate(struct dahdi_chan *const chan, const int txstate)
6529 {
6530 bool ret;
6531 unsigned long flags;
6532
6533 spin_lock_irqsave(&chan->lock, flags);
6534 ret = (txstate == chan->txstate);
6535 spin_unlock_irqrestore(&chan->lock, flags);
6536 return ret;
6537 }
6538
6539 static int dahdi_chan_ioctl(struct file *file, unsigned int cmd, unsigned long data)
6540 {
6541 struct dahdi_chan *const chan = chan_from_file(file);
6542 unsigned long flags;
6543 int j;
6544 int ret;
6545 int oldconf;
6546 const void *rxgain = NULL;
6547
6548 if (!chan)
6549 return -ENOSYS;
6550
6551 WARN_ON(!chan->master);
6552
6553 switch(cmd) {
6554 case DAHDI_SETSIGFREEZE:
6555 get_user(j, (int __user *)data);
6556 spin_lock_irqsave(&chan->lock, flags);
6557 if (j) {
6558 chan->flags |= DAHDI_FLAG_SIGFREEZE;
6559 } else {
6560 chan->flags &= ~DAHDI_FLAG_SIGFREEZE;
6561 }
6562 spin_unlock_irqrestore(&chan->lock, flags);
6563 break;
6564 case DAHDI_GETSIGFREEZE:
6565 spin_lock_irqsave(&chan->lock, flags);
6566 if (chan->flags & DAHDI_FLAG_SIGFREEZE)
6567 j = 1;
6568 else
6569 j = 0;
6570 spin_unlock_irqrestore(&chan->lock, flags);
6571 put_user(j, (int __user *)data);
6572 break;
6573 case DAHDI_AUDIOMODE:
6574 /* Only literal clear channels can be put in */
6575 if (chan->sig != DAHDI_SIG_CLEAR) return (-EINVAL);
6576 get_user(j, (int __user *)data);
6577 if (j) {
6578 spin_lock_irqsave(&chan->lock, flags);
6579 chan->flags |= DAHDI_FLAG_AUDIO;
6580 chan->flags &= ~(DAHDI_FLAG_HDLC | DAHDI_FLAG_FCS);
6581 spin_unlock_irqrestore(&chan->lock, flags);
6582 } else {
6583 /* Coming out of audio mode, also clear all
6584 conferencing and gain related info as well
6585 as echo canceller */
6586 struct dahdi_echocan_state *ec_state;
6587 const struct dahdi_echocan_factory *ec_current;
6588
6589 spin_lock_irqsave(&chan->lock, flags);
6590 chan->flags &= ~DAHDI_FLAG_AUDIO;
6591 /* save old conf number, if any */
6592 oldconf = chan->confna;
6593 /* initialize conference variables */
6594 chan->_confn = 0;
6595 chan->confna = 0;
6596 chan->conf_chan = NULL;
6597 dahdi_disable_dacs(chan);
6598 chan->confmode = 0;
6599 chan->confmute = 0;
6600 memset(chan->conflast, 0, sizeof(chan->conflast));
6601 memset(chan->conflast1, 0, sizeof(chan->conflast1));
6602 memset(chan->conflast2, 0, sizeof(chan->conflast2));
6603 ec_state = chan->ec_state;
6604 chan->ec_state = NULL;
6605 ec_current = chan->ec_current;
6606 chan->ec_current = NULL;
6607 /* release conference resource, if any to release */
6608 reset_conf(chan);
6609 if (is_gain_allocated(chan))
6610 rxgain = chan->rxgain;
6611 else
6612 rxgain = NULL;
6613
6614 chan->rxgain = defgain;
6615 chan->txgain = defgain;
6616 spin_unlock_irqrestore(&chan->lock, flags);
6617
6618 if (ec_state) {
6619 ec_state->ops->echocan_free(chan, ec_state);
6620 release_echocan(ec_current);
6621 }
6622
6623 if (rxgain)
6624 kfree(rxgain);
6625 if (oldconf) dahdi_check_conf(oldconf);
6626 }
6627 #ifdef DAHDI_AUDIO_NOTIFY
6628 if (chan->span->ops->audio_notify)
6629 chan->span->ops->audio_notify(chan, j);
6630 #endif
6631 break;
6632 case DAHDI_HDLCPPP:
6633 #ifdef CONFIG_DAHDI_PPP
6634 if (chan->sig != DAHDI_SIG_CLEAR) return (-EINVAL);
6635 get_user(j, (int __user *)data);
6636 if (j) {
6637 if (!chan->ppp) {
6638 chan->ppp = kzalloc(sizeof(struct ppp_channel), GFP_KERNEL);
6639 if (chan->ppp) {
6640 struct dahdi_echocan_state *tec;
6641 const struct dahdi_echocan_factory *ec_current;
6642
6643 chan->ppp->private = chan;
6644 chan->ppp->ops = &ztppp_ops;
6645 chan->ppp->mtu = DAHDI_DEFAULT_MTU_MRU;
6646 chan->ppp->hdrlen = 0;
6647 skb_queue_head_init(&chan->ppp_rq);
6648 chan->do_ppp_wakeup = 0;
6649 tasklet_init(&chan->ppp_calls, do_ppp_calls,
6650 (unsigned long)chan);
6651 if ((ret = dahdi_reallocbufs(chan, DAHDI_DEFAULT_MTU_MRU, DAHDI_DEFAULT_NUM_BUFS))) {
6652 kfree(chan->ppp);
6653 chan->ppp = NULL;
6654 return ret;
6655 }
6656
6657 if ((ret = ppp_register_channel(chan->ppp))) {
6658 kfree(chan->ppp);
6659 chan->ppp = NULL;
6660 return ret;
6661 }
6662 tec = chan->ec_state;
6663 chan->ec_state = NULL;
6664 ec_current = chan->ec_current;
6665 chan->ec_current = NULL;
6666 /* Make sure there's no gain */
6667 if (is_gain_allocated(chan))
6668 kfree(chan->rxgain);
6669 chan->rxgain = defgain;
6670 chan->txgain = defgain;
6671 chan->flags &= ~DAHDI_FLAG_AUDIO;
6672 chan->flags |= (DAHDI_FLAG_PPP | DAHDI_FLAG_HDLC | DAHDI_FLAG_FCS);
6673
6674 if (tec) {
6675 tec->ops->echocan_free(chan, tec);
6676 release_echocan(ec_current);
6677 }
6678 } else
6679 return -ENOMEM;
6680 }
6681 } else {
6682 chan->flags &= ~(DAHDI_FLAG_PPP | DAHDI_FLAG_HDLC | DAHDI_FLAG_FCS);
6683 if (chan->ppp) {
6684 struct ppp_channel *ppp = chan->ppp;
6685 chan->ppp = NULL;
6686 tasklet_kill(&chan->ppp_calls);
6687 skb_queue_purge(&chan->ppp_rq);
6688 ppp_unregister_channel(ppp);
6689 kfree(ppp);
6690 }
6691 }
6692 #else
6693 module_printk(KERN_NOTICE, "PPP support not compiled in\n");
6694 return -ENOSYS;
6695 #endif
6696 break;
6697 case DAHDI_HDLCRAWMODE:
6698 if (chan->sig != DAHDI_SIG_CLEAR) return (-EINVAL);
6699 get_user(j, (int __user *)data);
6700 chan->flags &= ~(DAHDI_FLAG_AUDIO | DAHDI_FLAG_HDLC | DAHDI_FLAG_FCS);
6701 if (j) {
6702 chan->flags |= DAHDI_FLAG_HDLC;
6703 fasthdlc_init(&chan->rxhdlc, (chan->flags & DAHDI_FLAG_HDLC56) ? FASTHDLC_MODE_56 : FASTHDLC_MODE_64);
6704 fasthdlc_init(&chan->txhdlc, (chan->flags & DAHDI_FLAG_HDLC56) ? FASTHDLC_MODE_56 : FASTHDLC_MODE_64);
6705 }
6706 break;
6707 case DAHDI_HDLCFCSMODE:
6708 if (chan->sig != DAHDI_SIG_CLEAR) return (-EINVAL);
6709 get_user(j, (int __user *)data);
6710 chan->flags &= ~(DAHDI_FLAG_AUDIO | DAHDI_FLAG_HDLC | DAHDI_FLAG_FCS);
6711 if (j) {
6712 chan->flags |= DAHDI_FLAG_HDLC | DAHDI_FLAG_FCS;
6713 fasthdlc_init(&chan->rxhdlc, (chan->flags & DAHDI_FLAG_HDLC56) ? FASTHDLC_MODE_56 : FASTHDLC_MODE_64);
6714 fasthdlc_init(&chan->txhdlc, (chan->flags & DAHDI_FLAG_HDLC56) ? FASTHDLC_MODE_56 : FASTHDLC_MODE_64);
6715 }
6716 break;
6717 case DAHDI_HDLC_RATE:
6718 get_user(j, (int __user *)data);
6719 if (j == 56) {
6720 chan->flags |= DAHDI_FLAG_HDLC56;
6721 } else {
6722 chan->flags &= ~DAHDI_FLAG_HDLC56;
6723 }
6724
6725 fasthdlc_init(&chan->rxhdlc, (chan->flags & DAHDI_FLAG_HDLC56) ? FASTHDLC_MODE_56 : FASTHDLC_MODE_64);
6726 fasthdlc_init(&chan->txhdlc, (chan->flags & DAHDI_FLAG_HDLC56) ? FASTHDLC_MODE_56 : FASTHDLC_MODE_64);
6727 break;
6728 case DAHDI_ECHOCANCEL_PARAMS:
6729 {
6730 struct dahdi_echocanparams ecp;
6731
6732 if (!(chan->flags & DAHDI_FLAG_AUDIO))
6733 return -EINVAL;
6734 ret = copy_from_user(&ecp,
6735 (struct dahdi_echocanparams __user *)data,
6736 sizeof(ecp));
6737 if (ret)
6738 return -EFAULT;
6739 data += sizeof(ecp);
6740 ret = ioctl_echocancel(chan, &ecp, (void __user *)data);
6741 if (ret)
6742 return ret;
6743 break;
6744 }
6745 case DAHDI_ECHOCANCEL:
6746 {
6747 struct dahdi_echocanparams ecp;
6748
6749 if (!(chan->flags & DAHDI_FLAG_AUDIO))
6750 return -EINVAL;
6751 get_user(j, (int __user *) data);
6752 ecp.tap_length = j;
6753 ecp.param_count = 0;
6754 if ((ret = ioctl_echocancel(chan, &ecp, NULL)))
6755 return ret;
6756 break;
6757 }
6758 case DAHDI_ECHOTRAIN:
6759 /* get pre-training time from user */
6760 get_user(j, (int __user *)data);
6761 if ((j < 0) || (j >= DAHDI_MAX_PRETRAINING))
6762 return -EINVAL;
6763 j <<= 3;
6764 spin_lock_irqsave(&chan->lock, flags);
6765 if (chan->ec_state) {
6766 /* Start pretraining stage */
6767 if (chan->ec_state->ops->echocan_traintap) {
6768 chan->ec_state->status.mode = ECHO_MODE_PRETRAINING;
6769 chan->ec_state->status.pretrain_timer = j;
6770 }
6771 spin_unlock_irqrestore(&chan->lock, flags);
6772 } else {
6773 spin_unlock_irqrestore(&chan->lock, flags);
6774 return -EINVAL;
6775 }
6776 break;
6777 case DAHDI_ECHOCANCEL_FAX_MODE:
6778 if (!chan->ec_state) {
6779 return -EINVAL;
6780 } else {
6781 get_user(j, (int __user *) data);
6782 spin_lock_irqsave(&chan->lock, flags);
6783 set_echocan_fax_mode(chan, chan->channo, "ioctl", j ? 1 : 0);
6784 spin_unlock_irqrestore(&chan->lock, flags);
6785 }
6786 break;
6787 case DAHDI_SETTXBITS:
6788 if (chan->sig != DAHDI_SIG_CAS)
6789 return -EINVAL;
6790 get_user(j, (int __user *)data);
6791 dahdi_cas_setbits(chan, j);
6792 break;
6793 case DAHDI_GETRXBITS:
6794 put_user(chan->rxsig, (int __user *)data);
6795 break;
6796 case DAHDI_LOOPBACK:
6797 get_user(j, (int __user *)data);
6798 spin_lock_irqsave(&chan->lock, flags);
6799 if (j)
6800 chan->flags |= DAHDI_FLAG_LOOPED;
6801 else
6802 chan->flags &= ~DAHDI_FLAG_LOOPED;
6803 spin_unlock_irqrestore(&chan->lock, flags);
6804 break;
6805 case DAHDI_HOOK:
6806 get_user(j, (int __user *)data);
6807 if (chan->flags & DAHDI_FLAG_CLEAR)
6808 return -EINVAL;
6809 if (chan->sig == DAHDI_SIG_CAS)
6810 return -EINVAL;
6811 /* if no span, just do nothing */
6812 if (!chan->span) return(0);
6813 spin_lock_irqsave(&chan->lock, flags);
6814 /* if dialing, stop it */
6815 chan->curtone = NULL;
6816 chan->dialing = 0;
6817 chan->txdialbuf[0] = '\0';
6818 chan->tonep = 0;
6819 chan->pdialcount = 0;
6820 spin_unlock_irqrestore(&chan->lock, flags);
6821 if (chan->span->flags & DAHDI_FLAG_RBS) {
6822 switch (j) {
6823 case DAHDI_ONHOOK:
6824 spin_lock_irqsave(&chan->lock, flags);
6825 dahdi_hangup(chan);
6826 spin_unlock_irqrestore(&chan->lock, flags);
6827 break;
6828 case DAHDI_OFFHOOK:
6829 spin_lock_irqsave(&chan->lock, flags);
6830 if ((chan->txstate == DAHDI_TXSTATE_KEWL) ||
6831 (chan->txstate == DAHDI_TXSTATE_AFTERKEWL)) {
6832 spin_unlock_irqrestore(&chan->lock, flags);
6833 return -EBUSY;
6834 }
6835 dahdi_rbs_sethook(chan, DAHDI_TXSIG_OFFHOOK, DAHDI_TXSTATE_DEBOUNCE, chan->debouncetime);
6836 spin_unlock_irqrestore(&chan->lock, flags);
6837 break;
6838 case DAHDI_RING:
6839 case DAHDI_START:
6840 spin_lock_irqsave(&chan->lock, flags);
6841 if (!chan->curzone) {
6842 spin_unlock_irqrestore(&chan->lock, flags);
6843 module_printk(KERN_WARNING, "Cannot start tone until a tone zone is loaded.\n");
6844 return -ENODATA;
6845 }
6846 if (chan->txstate != DAHDI_TXSTATE_ONHOOK) {
6847 spin_unlock_irqrestore(&chan->lock, flags);
6848 return -EBUSY;
6849 }
6850 if (chan->sig & __DAHDI_SIG_FXO) {
6851 ret = 0;
6852 chan->cadencepos = 0;
6853 ret = chan->ringcadence[0];
6854 dahdi_rbs_sethook(chan, DAHDI_TXSIG_START, DAHDI_TXSTATE_RINGON, ret);
6855 } else
6856 dahdi_rbs_sethook(chan, DAHDI_TXSIG_START, DAHDI_TXSTATE_START, chan->starttime);
6857 spin_unlock_irqrestore(&chan->lock, flags);
6858 if (file->f_flags & O_NONBLOCK)
6859 return -EINPROGRESS;
6860 break;
6861 case DAHDI_WINK:
6862 spin_lock_irqsave(&chan->lock, flags);
6863 if (chan->txstate != DAHDI_TXSTATE_ONHOOK) {
6864 spin_unlock_irqrestore(&chan->lock, flags);
6865 return -EBUSY;
6866 }
6867 dahdi_rbs_sethook(chan, DAHDI_TXSIG_ONHOOK, DAHDI_TXSTATE_PREWINK, chan->prewinktime);
6868 spin_unlock_irqrestore(&chan->lock, flags);
6869 if (file->f_flags & O_NONBLOCK)
6870 return -EINPROGRESS;
6871 wait_event_interruptible(chan->waitq,
6872 !chan->file->private_data || is_txstate(chan, DAHDI_TXSIG_ONHOOK));
6873 if (unlikely(!chan->file->private_data))
6874 return -ENODEV;
6875 if (signal_pending(current))
6876 return -ERESTARTSYS;
6877 break;
6878 case DAHDI_FLASH:
6879 spin_lock_irqsave(&chan->lock, flags);
6880 if (chan->txstate != DAHDI_TXSTATE_OFFHOOK) {
6881 spin_unlock_irqrestore(&chan->lock, flags);
6882 return -EBUSY;
6883 }
6884 dahdi_rbs_sethook(chan, DAHDI_TXSIG_OFFHOOK, DAHDI_TXSTATE_PREFLASH, chan->preflashtime);
6885 spin_unlock_irqrestore(&chan->lock, flags);
6886 if (file->f_flags & O_NONBLOCK)
6887 return -EINPROGRESS;
6888 wait_event_interruptible(chan->waitq,
6889 !chan->file->private_data || is_txstate(chan, DAHDI_TXSIG_OFFHOOK));
6890 if (unlikely(!chan->file->private_data))
6891 return -ENODEV;
6892 if (signal_pending(current))
6893 return -ERESTARTSYS;
6894 break;
6895 case DAHDI_RINGOFF:
6896 spin_lock_irqsave(&chan->lock, flags);
6897 dahdi_rbs_sethook(chan, DAHDI_TXSIG_ONHOOK, DAHDI_TXSTATE_ONHOOK, 0);
6898 spin_unlock_irqrestore(&chan->lock, flags);
6899 break;
6900 default:
6901 return -EINVAL;
6902 }
6903 } else if (chan->span->ops->sethook) {
6904 if (chan->txhooksig != j) {
6905 chan->txhooksig = j;
6906 chan->span->ops->sethook(chan, j);
6907 }
6908 } else
6909 return -ENOSYS;
6910 break;
6911 #ifdef CONFIG_DAHDI_PPP
6912 case PPPIOCGCHAN:
6913 if (chan->flags & DAHDI_FLAG_PPP) {
6914 return put_user(ppp_channel_index(chan->ppp),
6915 (int __user *)data) ? -EFAULT : 0;
6916 } else {
6917 return -EINVAL;
6918 }
6919 break;
6920 case PPPIOCGUNIT:
6921 if (chan->flags & DAHDI_FLAG_PPP) {
6922 return put_user(ppp_unit_number(chan->ppp),
6923 (int __user *)data) ? -EFAULT : 0;
6924 } else {
6925 return -EINVAL;
6926 }
6927 break;
6928 #endif
6929 case DAHDI_BUFFER_EVENTS:
6930 if (get_user(j, (int __user *)data))
6931 return -EFAULT;
6932 if (j)
6933 set_bit(DAHDI_FLAGBIT_BUFEVENTS, &chan->flags);
6934 else
6935 clear_bit(DAHDI_FLAGBIT_BUFEVENTS, &chan->flags);
6936
6937 break;
6938 default:
6939 return dahdi_chanandpseudo_ioctl(file, cmd, data);
6940 }
6941 return 0;
6942 }
6943
6944 static int dahdi_prechan_ioctl(struct file *file, unsigned int cmd, unsigned long data)
6945 {
6946 int channo;
6947 int res;
6948
6949 if (file->private_data) {
6950 module_printk(KERN_NOTICE, "Huh? Prechan already has private data??\n");
6951 }
6952 switch(cmd) {
6953 case DAHDI_SPECIFY:
6954 get_user(channo, (int __user *)data);
6955 file->private_data = chan_from_num(channo);
6956 if (!file->private_data)
6957 return -EINVAL;
6958 res = dahdi_specchan_open(file);
6959 if (res)
6960 file->private_data = NULL;
6961 return res;
6962 default:
6963 return -ENOSYS;
6964 }
6965 return 0;
6966 }
6967
6968 static long
6969 dahdi_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long data)
6970 {
6971 int unit = UNIT(file);
6972 int ret;
6973
6974 if (unit == DAHDI_CTL) {
6975 ret = dahdi_ctl_ioctl(file, cmd, data);
6976 goto exit;
6977 }
6978
6979 if (unit == DAHDI_TRANSCODE) {
6980 /* dahdi_transcode should have updated the file_operations on
6981 * this file object on open, so we shouldn't be here. */
6982 WARN_ON(1);
6983 ret = -EFAULT;
6984 goto exit;
6985 }
6986
6987 if (unit == DAHDI_TIMER) {
6988 /* The file operations for a timer device should have been
6989 * updated. */
6990 WARN_ON(1);
6991 ret = -EFAULT;
6992 goto exit;
6993 }
6994 if (unit == DAHDI_CHANNEL) {
6995 if (file->private_data)
6996 ret = dahdi_chan_ioctl(file, cmd, data);
6997 else
6998 ret = dahdi_prechan_ioctl(file, cmd, data);
6999 goto exit;
7000 }
7001 if (unit == DAHDI_PSEUDO) {
7002 if (!file->private_data) {
7003 module_printk(KERN_NOTICE, "No pseudo channel structure to read?\n");
7004 ret = -EINVAL;
7005 goto exit;
7006 }
7007 ret = dahdi_chanandpseudo_ioctl(file, cmd, data);
7008 goto exit;
7009 }
7010
7011 if (!file->private_data) {
7012 ret = -ENXIO;
7013 goto exit;
7014 }
7015
7016 ret = dahdi_chan_ioctl(file, cmd, data);
7017
7018 exit:
7019 return ret;
7020 }
7021
7022 #ifndef HAVE_UNLOCKED_IOCTL
7023 static int dahdi_ioctl(struct inode *inode, struct file *file,
7024 unsigned int cmd, unsigned long data)
7025 {
7026 return dahdi_unlocked_ioctl(file, cmd, data);
7027 }
7028 #endif
7029
7030 #ifdef HAVE_COMPAT_IOCTL
7031 static long dahdi_ioctl_compat(struct file *file, unsigned int cmd,
7032 unsigned long data)
7033 {
7034 if (cmd == DAHDI_SFCONFIG)
7035 return -ENOTTY; /* Not supported yet */
7036
7037 return dahdi_unlocked_ioctl(file, cmd, data);
7038 }
7039 #endif
7040
7041 /**
7042 * _get_next_channo - Return the next taken channel number from the span list.
7043 * @span: The span with which to start the search.
7044 *
7045 * Returns -1 if there aren't any channels on span or any of the following
7046 * spans, otherwise, returns the channel number of the first channel.
7047 *
7048 * Must be callled with registration_mutex held.
7049 *
7050 */
7051 static unsigned int _get_next_channo(const struct dahdi_span *span)
7052 {
7053 const struct list_head *pos = &span->spans_node;
7054 while (pos != &span_list) {
7055 span = list_entry(pos, struct dahdi_span, spans_node);
7056 if (span->channels)
7057 return span->chans[0]->channo;
7058 pos = pos->next;
7059 }
7060 return -1;
7061 }
7062
7063 static void
7064 set_spanno_and_basechan(struct dahdi_span *span, u32 spanno, u32 basechan)
7065 {
7066 int i;
7067 dahdi_dev_dbg(ASSIGN, span_device(span),
7068 "set: spanno=%d, basechan=%d (span->channels=%d)\n",
7069 spanno, basechan, span->channels);
7070 span->spanno = spanno;
7071 for (i = 0; i < span->channels; ++i)
7072 span->chans[i]->channo = basechan + i;
7073 }
7074
7075 /**
7076 * _assign_spanno_and_basechan - Assign next available span and channel numbers.
7077 *
7078 * This function will set span->spanno and channo for all the member channels.
7079 * It will assign the first available location.
7080 *
7081 * Must be called with registration_mutex held.
7082 *
7083 */
7084 static int _assign_spanno_and_basechan(struct dahdi_span *span)
7085 {
7086 struct dahdi_span *pos;
7087 unsigned int next_channo;
7088 unsigned int spanno = 1;
7089 unsigned int basechan = 1;
7090
7091 dahdi_dev_dbg(ASSIGN, span_device(span),
7092 "assign: channels=%d\n", span->channels);
7093 list_for_each_entry(pos, &span_list, spans_node) {
7094
7095 if (pos->spanno <= spanno) {
7096 spanno = pos->spanno + 1;
7097 basechan = pos->chans[0]->channo + pos->channels;
7098 continue;
7099 }
7100
7101 next_channo = _get_next_channo(pos);
7102 if ((basechan + span->channels) <= next_channo)
7103 break;
7104
7105 /* We can't fit here, let's look at the next location. */
7106 spanno = pos->spanno + 1;
7107 if (pos->channels)
7108 basechan = pos->chans[0]->channo + pos->channels;
7109 }
7110
7111 dahdi_dev_dbg(ASSIGN, span_device(span),
7112 "good: spanno=%d, basechan=%d (span->channels=%d)\n",
7113 spanno, basechan, span->channels);
7114 set_spanno_and_basechan(span, spanno, basechan);
7115 return 0;
7116 }
7117
7118 static inline struct dahdi_span *span_from_node(struct list_head *node)
7119 {
7120 return container_of(node, struct dahdi_span, spans_node);
7121 }
7122
7123 /*
7124 * Call with registration_mutex held. Make sure all the spans are on the list
7125 * ordered by span.
7126 *
7127 */
7128 static void _dahdi_add_span_to_span_list(struct dahdi_span *span)
7129 {
7130 unsigned long flags;
7131 struct dahdi_span *pos;
7132
7133 if (list_empty(&span_list)) {
7134 list_add_tail(&span->spans_node, &span_list);
7135 return;
7136 }
7137
7138 list_for_each_entry(pos, &span_list, spans_node) {
7139 WARN_ON(0 == pos->spanno);
7140 if (pos->spanno > span->spanno)
7141 break;
7142 }
7143
7144 spin_lock_irqsave(&chan_lock, flags);
7145 list_add(&span->spans_node, pos->spans_node.prev);
7146 spin_unlock_irqrestore(&chan_lock, flags);
7147 }
7148
7149 /**
7150 * _check_spanno_and_basechan - Check if we can fit the new span in the requested location.
7151 *
7152 * Must be called with registration_mutex held.
7153 *
7154 */
7155 static int
7156 _check_spanno_and_basechan(struct dahdi_span *span, u32 spanno, u32 basechan)
7157 {
7158 struct dahdi_span *pos;
7159 unsigned int next_channo;
7160
7161 dahdi_dev_dbg(ASSIGN, span_device(span),
7162 "check: spanno=%d, basechan=%d (span->channels=%d)\n",
7163 spanno, basechan, span->channels);
7164 list_for_each_entry(pos, &span_list, spans_node) {
7165
7166 next_channo = _get_next_channo(pos);
7167 dahdi_dev_dbg(ASSIGN, span_device(span),
7168 "pos: spanno=%d channels=%d (next_channo=%d)\n",
7169 pos->spanno, pos->channels, next_channo);
7170
7171 if (pos->spanno <= spanno) {
7172 if (basechan < next_channo + pos->channels) {
7173 /* Requested basechan breaks channel sorting */
7174 dev_notice(span_device(span),
7175 "[%d] basechan (%d) is too low for wanted span %d\n",
7176 local_spanno(span), basechan, spanno);
7177 return -EINVAL;
7178 }
7179 continue;
7180 }
7181
7182 if (next_channo == -1)
7183 break;
7184
7185 if ((basechan + span->channels) <= next_channo)
7186 break;
7187
7188 /* Cannot fit the span into the requested location. Abort. */
7189 dev_notice(span_device(span),
7190 "cannot fit span %d (basechan=%d) into requested location\n",
7191 spanno, basechan);
7192 return -EINVAL;
7193 }
7194
7195 dahdi_dev_dbg(ASSIGN, span_device(span),
7196 "good: spanno=%d, basechan=%d (span->channels=%d)\n",
7197 spanno, basechan, span->channels);
7198 set_spanno_and_basechan(span, spanno, basechan);
7199 return 0;
7200 }
7201
7202
7203 struct dahdi_device *dahdi_create_device(void)
7204 {
7205 struct dahdi_device *ddev;
7206 ddev = kzalloc(sizeof(*ddev), GFP_KERNEL);
7207 if (!ddev)
7208 return NULL;
7209 INIT_LIST_HEAD(&ddev->spans);
7210 dahdi_sysfs_init_device(ddev);
7211 return ddev;
7212 }
7213 EXPORT_SYMBOL(dahdi_create_device);
7214
7215 void dahdi_free_device(struct dahdi_device *ddev)
7216 {
7217 put_device(&ddev->dev);
7218 }
7219 EXPORT_SYMBOL(dahdi_free_device);
7220
7221 /**
7222 * __dahdi_init_span - Setup all the data structures for the span.
7223 * @span: The span of interest.
7224 *
7225 */
7226 static void __dahdi_init_span(struct dahdi_span *span)
7227 {
7228 int x;
7229
7230 INIT_LIST_HEAD(&span->spans_node);
7231 spin_lock_init(&span->lock);
7232 clear_bit(DAHDI_FLAGBIT_REGISTERED, &span->flags);
7233
7234 if (!span->deflaw) {
7235 module_printk(KERN_NOTICE, "Span %s didn't specify default "
7236 "law. Assuming mulaw, please fix driver!\n",
7237 span->name);
7238 span->deflaw = DAHDI_LAW_MULAW;
7239 }
7240 if (span->spantype == SPANTYPE_INVALID) {
7241 module_printk(KERN_NOTICE,
7242 "Warning: Span %s didn't specify a spantype. "
7243 "Please fix driver!\n", span->name);
7244 }
7245
7246 for (x = 0; x < span->channels; ++x) {
7247 span->chans[x]->span = span;
7248 __dahdi_init_chan(span->chans[x]);
7249 }
7250 }
7251
7252 /**
7253 * dahdi_init_span - (Re)Initializes a dahdi span.
7254 * @span: The span to initialize.
7255 *
7256 * Reinitializing a device span might be necessary if a span has been changed
7257 * (channels added / removed) between when the dahdi_device it is on was first
7258 * registered and when the spans are actually assigned.
7259 *
7260 */
7261 void dahdi_init_span(struct dahdi_span *span)
7262 {
7263 mutex_lock(&registration_mutex);
7264 __dahdi_init_span(span);
7265 mutex_unlock(&registration_mutex);
7266 }
7267 EXPORT_SYMBOL(dahdi_init_span);
7268
7269 /**
7270 * _dahdi_assign_span() - Assign a new DAHDI span
7271 * @span: the DAHDI span
7272 * @spanno: The span number we would like assigned. If 0, the first
7273 * available spanno/basechan will be used.
7274 * @basechan: The base channel number we would like. Ignored if spanno is 0.
7275 * @prefmaster: will the new span be preferred as a master_span?
7276 *
7277 * Assigns a span for usage with DAHDI. All the channel numbers in it will
7278 * have their numbering started at basechan.
7279 *
7280 * If prefmaster is set to anything > 0, span will attempt to become the
7281 * master DAHDI span at registration time. If 0: it will only become
7282 * master if no other span is currently the master (i.e.: it is the
7283 * first one).
7284 *
7285 * Must be called with registration_mutex held, and the span must have already
7286 * been initialized ith the __dahdi_init_span call.
7287 *
7288 */
7289 static int _dahdi_assign_span(struct dahdi_span *span, unsigned int spanno,
7290 unsigned int basechan, int prefmaster)
7291 {
7292 int res = 0;
7293 unsigned int x;
7294 unsigned long flags;
7295
7296 if (!span || !span->ops || !span->ops->owner)
7297 return -EFAULT;
7298
7299 if (test_bit(DAHDI_FLAGBIT_REGISTERED, &span->flags)) {
7300 dev_notice(span_device(span),
7301 "local span %d is already assigned span %d\n",
7302 local_spanno(span), span->spanno);
7303 return -EINVAL;
7304 }
7305
7306 /* DAHDI_ALARM_NOTOPEN can be set when a span is disabled, i.e. via
7307 * sysfs, so when the span is being reassigned we should make sure it's
7308 * cleared. This eliminates the need for board drivers to re-report
7309 * their alarm states on span reassignment. */
7310
7311 spin_lock_irqsave(&span->lock, flags);
7312 span->alarms &= ~DAHDI_ALARM_NOTOPEN;
7313 dahdi_alarm_notify(span);
7314 spin_unlock_irqrestore(&span->lock, flags);
7315
7316 if (span->ops->enable_hw_preechocan ||
7317 span->ops->disable_hw_preechocan) {
7318 if ((NULL == span->ops->enable_hw_preechocan) ||
7319 (NULL == span->ops->disable_hw_preechocan)) {
7320 dev_notice(span_device(span),
7321 "span with inconsistent enable/disable hw_preechocan");
7322 return -EFAULT;
7323 }
7324 }
7325
7326 if (!span->deflaw) {
7327 module_printk(KERN_NOTICE, "Span %s didn't specify default law. "
7328 "Assuming mulaw, please fix driver!\n", span->name);
7329 span->deflaw = DAHDI_LAW_MULAW;
7330 }
7331
7332 /* Look through the span list to find the first available span number.
7333 * The spans are kept on this list in sorted order. We'll also save
7334 * off the next available channel number to use. */
7335
7336 if (0 == spanno)
7337 res = _assign_spanno_and_basechan(span);
7338 else
7339 res = _check_spanno_and_basechan(span, spanno, basechan);
7340
7341 if (res)
7342 return res;
7343
7344 for (x = 0; x < span->channels; x++)
7345 dahdi_chan_reg(span->chans[x]);
7346
7347 #ifdef CONFIG_PROC_FS
7348 {
7349 char tempfile[17];
7350 snprintf(tempfile, sizeof(tempfile), "%d", span->spanno);
7351 span->proc_entry = proc_create_data(tempfile, 0444,
7352 root_proc_entry, &dahdi_proc_ops,
7353 (void *)((unsigned long)span->spanno));
7354 if (!span->proc_entry) {
7355 res = -EFAULT;
7356 span_err(span, "Error creating procfs entry\n");
7357 goto cleanup;
7358 }
7359 }
7360 #endif
7361
7362 res = span_sysfs_create(span);
7363 if (res)
7364 goto cleanup;
7365
7366 if (debug & DEBUG_MAIN) {
7367 module_printk(KERN_NOTICE, "Registered Span %d ('%s') with "
7368 "%d channels\n", span->spanno, span->name, span->channels);
7369 }
7370
7371 _dahdi_add_span_to_span_list(span);
7372
7373 set_bit(DAHDI_FLAGBIT_REGISTERED, &span->flags);
7374 if (span->ops->assigned)
7375 span->ops->assigned(span);
7376
7377 __dahdi_find_master_span();
7378
7379 return 0;
7380
7381 cleanup:
7382 #ifdef CONFIG_PROC_FS
7383 if (span->proc_entry) {
7384 char tempfile[17];
7385
7386 snprintf(tempfile, sizeof(tempfile), "dahdi/%d", span->spanno);
7387 remove_proc_entry(tempfile, NULL);
7388 span->proc_entry = NULL;
7389 }
7390 #endif
7391 for (x = 0; x < span->channels; x++) {
7392 struct dahdi_chan *chan = span->chans[x];
7393 if (test_bit(DAHDI_FLAGBIT_REGISTERED, &chan->flags))
7394 dahdi_chan_unreg(chan);
7395 }
7396 return res;
7397 }
7398
7399 int dahdi_assign_span(struct dahdi_span *span, unsigned int spanno,
7400 unsigned int basechan, int prefmaster)
7401 {
7402 int ret;
7403 mutex_lock(&registration_mutex);
7404 ret = _dahdi_assign_span(span, spanno, basechan, prefmaster);
7405 mutex_unlock(&registration_mutex);
7406 return ret;
7407 }
7408
7409 int dahdi_assign_device_spans(struct dahdi_device *ddev)
7410 {
7411 struct dahdi_span *span;
7412 mutex_lock(&registration_mutex);
7413 list_for_each_entry(span, &ddev->spans, device_node)
7414 _dahdi_assign_span(span, 0, 0, 1);
7415 mutex_unlock(&registration_mutex);
7416 return 0;
7417 }
7418
7419 static int auto_assign_spans = 1;
7420 static const char *UNKNOWN = "";
7421
7422 /**
7423 * dahdi_auto_assign_spans - is the parameter auto_assign_spans set?
7424 */
7425 int dahdi_get_auto_assign_spans(void)
7426 {
7427 return auto_assign_spans;
7428 }
7429 EXPORT_SYMBOL(dahdi_get_auto_assign_spans);
7430
7431 /**
7432 * _dahdi_register_device - Registers a DAHDI device and assign its spans.
7433 * @ddev: the DAHDI device
7434 *
7435 * If auto_assign_spans is 0, add the device to the device list and wait for
7436 * userspace to finish registration. Otherwise, go ahead and register the
7437 * spans in order as was done historically.
7438 *
7439 * Must hold registration_mutex when this function is called.
7440 *
7441 */
7442 static int _dahdi_register_device(struct dahdi_device *ddev,
7443 struct device *parent)
7444 {
7445 struct dahdi_span *s;
7446 int ret;
7447
7448 ddev->manufacturer = (ddev->manufacturer) ?: UNKNOWN;
7449 ddev->location = (ddev->location) ?: UNKNOWN;
7450 ddev->devicetype = (ddev->devicetype) ?: UNKNOWN;
7451
7452 list_for_each_entry(s, &ddev->spans, device_node) {
7453 s->parent = ddev;
7454 s->spanno = 0;
7455 __dahdi_init_span(s);
7456 }
7457
7458 ktime_get_ts(&ddev->registration_time);
7459 ret = dahdi_sysfs_add_device(ddev, parent);
7460 if (ret)
7461 return ret;
7462
7463 if (!auto_assign_spans)
7464 return 0;
7465
7466 list_for_each_entry(s, &ddev->spans, device_node)
7467 ret = _dahdi_assign_span(s, 0, 0, 1);
7468
7469 if (ret)
7470 dahdi_sysfs_unregister_device(ddev);
7471
7472 return ret;
7473 }
7474
7475 /**
7476 * dahdi_register_device() - unregister a new DAHDI device
7477 * @ddev: the DAHDI device
7478 *
7479 * Registers a device for usage with DAHDI.
7480 *
7481 */
7482 int dahdi_register_device(struct dahdi_device *ddev, struct device *parent)
7483 {
7484 int ret;
7485
7486 if (!ddev)
7487 return -EINVAL;
7488
7489 mutex_lock(&registration_mutex);
7490 ret = _dahdi_register_device(ddev, parent);
7491 mutex_unlock(&registration_mutex);
7492
7493 return ret;
7494 }
7495 EXPORT_SYMBOL(dahdi_register_device);
7496
7497 static void disable_span(struct dahdi_span *span)
7498 {
7499 int x;
7500 unsigned long flags;
7501
7502 spin_lock_irqsave(&span->lock, flags);
7503 span->alarms = DAHDI_ALARM_NOTOPEN;
7504 for (x = 0; x < span->channels; x++) {
7505 /*
7506 * This event may not make it to user space before the channel
7507 * is gone, but let's try.
7508 */
7509 dahdi_qevent_lock(span->chans[x], DAHDI_EVENT_REMOVED);
7510 }
7511 dahdi_alarm_notify(span);
7512 spin_unlock_irqrestore(&span->lock, flags);
7513 module_printk(KERN_INFO, "%s: span %d\n", __func__, span->spanno);
7514 }
7515
7516 #ifdef CONFIG_PROC_FS
7517 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 10, 0)
7518 static inline void proc_remove(struct proc_dir_entry *proc_entry)
7519 {
7520 remove_proc_entry(proc_entry->name, root_proc_entry);
7521 }
7522 #endif
7523 #endif
7524
7525 /**
7526 * _dahdi_unassign_span() - unassign a DAHDI span
7527 * @span: the DAHDI span
7528 *
7529 * Unassigns a span that has been previously assigned with
7530 * dahdi_assign_span().
7531 *
7532 * Must be called with the registration_mutex held.
7533 *
7534 */
7535 static int _dahdi_unassign_span(struct dahdi_span *span)
7536 {
7537 int res;
7538 int x;
7539 struct dahdi_span *new_master, *s;
7540 unsigned long flags;
7541
7542 if (!test_bit(DAHDI_FLAGBIT_REGISTERED, &span->flags)) {
7543 dev_info(span_device(span),
7544 "local span %d is already unassigned\n",
7545 local_spanno(span));
7546 return -EINVAL;
7547 }
7548 spin_lock_irqsave(&chan_lock, flags);
7549 list_del_init(&span->spans_node);
7550 spin_unlock_irqrestore(&chan_lock, flags);
7551 span->spanno = 0;
7552 clear_bit(DAHDI_FLAGBIT_REGISTERED, &span->flags);
7553
7554 res = dahdi_shutdown_span(span);
7555 if (res) {
7556 dev_err(span_device(span),
7557 "Failed to shutdown when unassigning.\n");
7558 }
7559
7560 if (debug & DEBUG_MAIN)
7561 module_printk(KERN_NOTICE, "Unassigning Span '%s' with %d channels\n", span->name, span->channels);
7562 #ifdef CONFIG_PROC_FS
7563 if (span->proc_entry) {
7564 proc_remove(span->proc_entry);
7565 span->proc_entry = NULL;
7566 }
7567 #endif /* CONFIG_PROC_FS */
7568
7569 span_sysfs_remove(span);
7570
7571 for (x=0;x<span->channels;x++)
7572 dahdi_chan_unreg(span->chans[x]);
7573
7574 new_master = master_span; /* FIXME: locking */
7575 if (master_span == span)
7576 new_master = NULL;
7577
7578 spin_lock_irqsave(&chan_lock, flags);
7579 list_for_each_entry(s, &span_list, spans_node) {
7580 if ((s == new_master) || !can_provide_timing(s))
7581 continue;
7582 new_master = s;
7583 break;
7584 }
7585 spin_unlock_irqrestore(&chan_lock, flags);
7586 if (master_span != new_master) {
7587 if (debug & DEBUG_MAIN) {
7588 module_printk(KERN_NOTICE, "%s: Span ('%s') is new master\n", __FUNCTION__,
7589 (new_master)? new_master->name: "no master");
7590 }
7591 }
7592 master_span = new_master;
7593 return 0;
7594 }
7595
7596 static int open_channel_count(const struct dahdi_span *span)
7597 {
7598 int i;
7599 int open_channels = 0;
7600 struct dahdi_chan *chan;
7601
7602 for (i = 0; i < span->channels; ++i) {
7603 chan = span->chans[i];
7604 if (test_bit(DAHDI_FLAGBIT_OPEN, &chan->flags))
7605 ++open_channels;
7606 }
7607 return open_channels;
7608 }
7609
7610 int dahdi_unassign_span(struct dahdi_span *span)
7611 {
7612 int ret;
7613
7614 module_printk(KERN_NOTICE, "%s: %s\n", __func__, span->name);
7615 disable_span(span);
7616 if (open_channel_count(span) > 0)
7617 msleep(1000); /* Give user space a chance to read this */
7618 mutex_lock(&registration_mutex);
7619 ret = _dahdi_unassign_span(span);
7620 mutex_unlock(&registration_mutex);
7621 return ret;
7622 }
7623
7624 /**
7625 * dahdi_unregister_device() - unregister a DAHDI device
7626 * @span: the DAHDI span
7627 *
7628 * Unregisters a device that has been previously registered with
7629 * dahdi_register_device().
7630 *
7631 */
7632 void dahdi_unregister_device(struct dahdi_device *ddev)
7633 {
7634 struct dahdi_span *s;
7635 struct dahdi_span *next;
7636 unsigned int spans_with_open_channels = 0;
7637
7638 WARN_ON(!ddev);
7639 might_sleep();
7640 if (unlikely(!ddev))
7641 return;
7642
7643 list_for_each_entry_safe(s, next, &ddev->spans, device_node) {
7644 disable_span(s);
7645 if (open_channel_count(s) > 0)
7646 ++spans_with_open_channels;
7647 }
7648
7649 if (spans_with_open_channels > 0)
7650 msleep(1000); /* give user space a chance to read this */
7651
7652 mutex_lock(&registration_mutex);
7653 list_for_each_entry_safe(s, next, &ddev->spans, device_node) {
7654 _dahdi_unassign_span(s);
7655 list_del_init(&s->device_node);
7656 }
7657 mutex_unlock(&registration_mutex);
7658
7659 dahdi_sysfs_unregister_device(ddev);
7660
7661 if (UNKNOWN == ddev->location)
7662 ddev->location = NULL;
7663 if (UNKNOWN == ddev->manufacturer)
7664 ddev->manufacturer = NULL;
7665 if (UNKNOWN == ddev->devicetype)
7666 ddev->devicetype = NULL;
7667
7668 }
7669 EXPORT_SYMBOL(dahdi_unregister_device);
7670
7671 /*
7672 ** This routine converts from linear to ulaw
7673 **
7674 ** Craig Reese: IDA/Supercomputing Research Center
7675 ** Joe Campbell: Department of Defense
7676 ** 29 September 1989
7677 **
7678 ** References:
7679 ** 1) CCITT Recommendation G.711 (very difficult to follow)
7680 ** 2) "A New Digital Technique for Implementation of Any
7681 ** Continuous PCM Companding Law," Villeret, Michel,
7682 ** et al. 1973 IEEE Int. Conf. on Communications, Vol 1,
7683 ** 1973, pg. 11.12-11.17
7684 ** 3) MIL-STD-188-113,"Interoperability and Performance Standards
7685 ** for Analog-to_Digital Conversion Techniques,"
7686 ** 17 February 1987
7687 **
7688 ** Input: Signed 16 bit linear sample
7689 ** Output: 8 bit ulaw sample
7690 */
7691
7692 #define ZEROTRAP /* turn on the trap as per the MIL-STD */
7693 #define BIAS 0x84 /* define the add-in bias for 16 bit samples */
7694 #define CLIP 32635
7695
7696 #ifdef CONFIG_CALC_XLAW
7697 unsigned char
7698 #else
7699 static unsigned char __init
7700 #endif
7701 __dahdi_lineartoulaw(short sample)
7702 {
7703 static int exp_lut[256] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
7704 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
7705 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
7706 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
7707 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
7708 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
7709 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
7710 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
7711 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
7712 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
7713 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
7714 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
7715 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
7716 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
7717 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
7718 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7};
7719 int sign, exponent, mantissa;
7720 unsigned char ulawbyte;
7721
7722 /* Get the sample into sign-magnitude. */
7723 sign = (sample >> 8) & 0x80; /* set aside the sign */
7724 if (sign != 0) sample = -sample; /* get magnitude */
7725 if (sample > CLIP) sample = CLIP; /* clip the magnitude */
7726
7727 /* Convert from 16 bit linear to ulaw. */
7728 sample = sample + BIAS;
7729 exponent = exp_lut[(sample >> 7) & 0xFF];
7730 mantissa = (sample >> (exponent + 3)) & 0x0F;
7731 ulawbyte = ~(sign | (exponent << 4) | mantissa);
7732 #ifdef ZEROTRAP
7733 if (ulawbyte == 0) ulawbyte = 0x02; /* optional CCITT trap */
7734 #endif
7735 if (ulawbyte == 0xff) ulawbyte = 0x7f; /* never return 0xff */
7736 return(ulawbyte);
7737 }
7738
7739 #define AMI_MASK 0x55
7740
7741 #ifdef CONFIG_CALC_XLAW
7742 unsigned char
7743 #else
7744 static inline unsigned char __init
7745 #endif
7746 __dahdi_lineartoalaw (short linear)
7747 {
7748 int mask;
7749 int seg;
7750 int pcm_val;
7751 static int seg_end[8] =
7752 {
7753 0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF
7754 };
7755
7756 pcm_val = linear;
7757 if (pcm_val >= 0)
7758 {
7759 /* Sign (7th) bit = 1 */
7760 mask = AMI_MASK | 0x80;
7761 }
7762 else
7763 {
7764 /* Sign bit = 0 */
7765 mask = AMI_MASK;
7766 pcm_val = -pcm_val;
7767 }
7768
7769 /* Convert the scaled magnitude to segment number. */
7770 for (seg = 0; seg < 8; seg++)
7771 {
7772 if (pcm_val <= seg_end[seg])
7773 break;
7774 }
7775 /* Combine the sign, segment, and quantization bits. */
7776 return ((seg << 4) | ((pcm_val >> ((seg) ? (seg + 3) : 4)) & 0x0F)) ^ mask;
7777 }
7778 /*- End of function --------------------------------------------------------*/
7779
7780 static inline short int __init alaw2linear (uint8_t alaw)
7781 {
7782 int i;
7783 int seg;
7784
7785 alaw ^= AMI_MASK;
7786 i = ((alaw & 0x0F) << 4);
7787 seg = (((int) alaw & 0x70) >> 4);
7788 if (seg)
7789 i = (i + 0x100) << (seg - 1);
7790 return (short int) ((alaw & 0x80) ? i : -i);
7791 }
7792 /*- End of function --------------------------------------------------------*/
7793 static void __init dahdi_conv_init(void)
7794 {
7795 int i;
7796
7797 /*
7798 * Set up mu-law conversion table
7799 */
7800 for(i = 0;i < 256;i++)
7801 {
7802 short mu,e,f,y;
7803 static short etab[]={0,132,396,924,1980,4092,8316,16764};
7804
7805 mu = 255-i;
7806 e = (mu & 0x70)/16;
7807 f = mu & 0x0f;
7808 y = f * (1 << (e + 3));
7809 y += etab[e];
7810 if (mu & 0x80) y = -y;
7811 __dahdi_mulaw[i] = y;
7812 __dahdi_alaw[i] = alaw2linear(i);
7813 /* Default (0.0 db) gain table */
7814 defgain[i] = i;
7815 }
7816 #ifndef CONFIG_CALC_XLAW
7817 /* set up the reverse (mu-law) conversion table */
7818 for(i = -32768; i < 32768; i += 4)
7819 {
7820 __dahdi_lin2mu[((unsigned short)(short)i) >> 2] = __dahdi_lineartoulaw(i);
7821 __dahdi_lin2a[((unsigned short)(short)i) >> 2] = __dahdi_lineartoalaw(i);
7822 }
7823 #endif
7824 }
7825
7826 static inline void __dahdi_process_getaudio_chunk(struct dahdi_chan *ss, unsigned char *txb)
7827 {
7828 /* We transmit data from our master channel */
7829 /* Called with ss->lock held */
7830 struct dahdi_chan *ms = ss->master;
7831 /* Linear representation */
7832 short getlin[DAHDI_CHUNKSIZE], k[DAHDI_CHUNKSIZE];
7833 int x;
7834
7835 /* Okay, now we've got something to transmit */
7836 for (x=0;x<DAHDI_CHUNKSIZE;x++)
7837 getlin[x] = DAHDI_XLAW(txb[x], ms);
7838
7839 #ifndef CONFIG_DAHDI_NO_ECHOCAN_DISABLE
7840 if (ms->ec_state && (ms->ec_state->status.mode == ECHO_MODE_ACTIVE) && !ms->ec_state->features.CED_tx_detect) {
7841 for (x = 0; x < DAHDI_CHUNKSIZE; x++) {
7842 if (echo_can_disable_detector_update(&ms->ec_state->txecdis, getlin[x])) {
7843 set_echocan_fax_mode(ms, ss->channo, "CED tx detected", 1);
7844 dahdi_qevent_nolock(ms, DAHDI_EVENT_TX_CED_DETECTED);
7845 break;
7846 }
7847 }
7848 }
7849 #endif
7850
7851 if ((!ms->confmute && !ms->dialing) || (is_pseudo_chan(ms))) {
7852 struct dahdi_chan *const conf_chan = ms->conf_chan;
7853 /* Handle conferencing on non-clear channel and non-HDLC channels */
7854 switch(ms->confmode & DAHDI_CONF_MODE_MASK) {
7855 case DAHDI_CONF_NORMAL:
7856 /* Do nuffin */
7857 break;
7858 case DAHDI_CONF_MONITOR: /* Monitor a channel's rx mode */
7859 /* if a pseudo-channel, ignore */
7860 if (is_pseudo_chan(ms))
7861 break;
7862 /* Add monitored channel */
7863 if (is_pseudo_chan(conf_chan))
7864 ACSS(getlin, conf_chan->getlin);
7865 else
7866 ACSS(getlin, conf_chan->putlin);
7867
7868 for (x=0;x<DAHDI_CHUNKSIZE;x++)
7869 txb[x] = DAHDI_LIN2X(getlin[x], ms);
7870 break;
7871 case DAHDI_CONF_MONITORTX: /* Monitor a channel's tx mode */
7872 /* if a pseudo-channel, ignore */
7873 if (is_pseudo_chan(ms))
7874 break;
7875 /* Add monitored channel */
7876 if (is_pseudo_chan(conf_chan))
7877 ACSS(getlin, conf_chan->putlin);
7878 else
7879 ACSS(getlin, conf_chan->getlin);
7880
7881 for (x=0;x<DAHDI_CHUNKSIZE;x++)
7882 txb[x] = DAHDI_LIN2X(getlin[x], ms);
7883 break;
7884 case DAHDI_CONF_MONITORBOTH: /* monitor a channel's rx and tx mode */
7885 /* if a pseudo-channel, ignore */
7886 if (is_pseudo_chan(ms))
7887 break;
7888 ACSS(getlin, conf_chan->putlin);
7889 ACSS(getlin, conf_chan->getlin);
7890 for (x=0;x<DAHDI_CHUNKSIZE;x++)
7891 txb[x] = DAHDI_LIN2X(getlin[x], ms);
7892 break;
7893 case DAHDI_CONF_MONITOR_RX_PREECHO: /* Monitor a channel's rx mode */
7894 /* if a pseudo-channel, ignore */
7895 if (is_pseudo_chan(ms))
7896 break;
7897
7898 if (!conf_chan->readchunkpreec)
7899 break;
7900
7901 /* Add monitored channel */
7902 ACSS(getlin, is_pseudo_chan(conf_chan) ?
7903 conf_chan->readchunkpreec : conf_chan->putlin);
7904 for (x = 0; x < DAHDI_CHUNKSIZE; x++)
7905 txb[x] = DAHDI_LIN2X(getlin[x], ms);
7906
7907 break;
7908 case DAHDI_CONF_MONITOR_TX_PREECHO: /* Monitor a channel's tx mode */
7909 /* if a pseudo-channel, ignore */
7910 if (is_pseudo_chan(ms))
7911 break;
7912
7913 if (!conf_chan->readchunkpreec)
7914 break;
7915
7916 /* Add monitored channel */
7917 ACSS(getlin, is_pseudo_chan(conf_chan) ?
7918 conf_chan->putlin : conf_chan->readchunkpreec);
7919 for (x = 0; x < DAHDI_CHUNKSIZE; x++)
7920 txb[x] = DAHDI_LIN2X(getlin[x], ms);
7921
7922 break;
7923 case DAHDI_CONF_MONITORBOTH_PREECHO: /* monitor a channel's rx and tx mode */
7924 /* if a pseudo-channel, ignore */
7925 if (is_pseudo_chan(ms))
7926 break;
7927
7928 if (!conf_chan->readchunkpreec)
7929 break;
7930
7931 ACSS(getlin, conf_chan->putlin);
7932 ACSS(getlin, conf_chan->readchunkpreec);
7933
7934 for (x = 0; x < DAHDI_CHUNKSIZE; x++)
7935 txb[x] = DAHDI_LIN2X(getlin[x], ms);
7936
7937 break;
7938 case DAHDI_CONF_REALANDPSEUDO:
7939 /* This strange mode takes the transmit buffer and
7940 puts it on the conference, minus its last sample,
7941 then outputs from the conference minus the
7942 real channel's last sample. */
7943 /* if to talk on conf */
7944 if (ms->confmode & DAHDI_CONF_PSEUDO_TALKER) {
7945 /* Store temp value */
7946 memcpy(k, getlin, DAHDI_CHUNKSIZE * sizeof(short));
7947 /* Add conf value */
7948 ACSS(k, conf_sums_next[ms->_confn]);
7949 /* save last one */
7950 memcpy(ms->conflast2, ms->conflast1, DAHDI_CHUNKSIZE * sizeof(short));
7951 memcpy(ms->conflast1, k, DAHDI_CHUNKSIZE * sizeof(short));
7952 /* get amount actually added */
7953 SCSS(ms->conflast1, conf_sums_next[ms->_confn]);
7954 /* Really add in new value */
7955 ACSS(conf_sums_next[ms->_confn], ms->conflast1);
7956 } else {
7957 memset(ms->conflast1, 0, DAHDI_CHUNKSIZE * sizeof(short));
7958 memset(ms->conflast2, 0, DAHDI_CHUNKSIZE * sizeof(short));
7959 }
7960 memset(getlin, 0, DAHDI_CHUNKSIZE * sizeof(short));
7961 txb[0] = DAHDI_LIN2X(0, ms);
7962 memset(txb + 1, txb[0], DAHDI_CHUNKSIZE - 1);
7963 /* fall through to normal conf mode */
7964 case DAHDI_CONF_CONF: /* Normal conference mode */
7965 if (is_pseudo_chan(ms)) /* if pseudo-channel */
7966 {
7967 /* if to talk on conf */
7968 if (ms->confmode & DAHDI_CONF_TALKER) {
7969 /* Store temp value */
7970 memcpy(k, getlin, DAHDI_CHUNKSIZE * sizeof(short));
7971 /* Add conf value */
7972 ACSS(k, conf_sums[ms->_confn]);
7973 /* get amount actually added */
7974 memcpy(ms->conflast, k, DAHDI_CHUNKSIZE * sizeof(short));
7975 SCSS(ms->conflast, conf_sums[ms->_confn]);
7976 /* Really add in new value */
7977 ACSS(conf_sums[ms->_confn], ms->conflast);
7978 memcpy(ms->getlin, getlin, DAHDI_CHUNKSIZE * sizeof(short));
7979 } else {
7980 memset(ms->conflast, 0, DAHDI_CHUNKSIZE * sizeof(short));
7981 memcpy(getlin, ms->getlin, DAHDI_CHUNKSIZE * sizeof(short));
7982 }
7983 txb[0] = DAHDI_LIN2X(0, ms);
7984 memset(txb + 1, txb[0], DAHDI_CHUNKSIZE - 1);
7985 break;
7986 }
7987 /* fall through */
7988 case DAHDI_CONF_CONFMON: /* Conference monitor mode */
7989 if (ms->confmode & DAHDI_CONF_LISTENER) {
7990 /* Subtract out last sample written to conf */
7991 SCSS(getlin, ms->conflast);
7992 /* Add in conference */
7993 ACSS(getlin, conf_sums[ms->_confn]);
7994 }
7995 for (x=0;x<DAHDI_CHUNKSIZE;x++)
7996 txb[x] = DAHDI_LIN2X(getlin[x], ms);
7997 break;
7998 case DAHDI_CONF_CONFANN:
7999 case DAHDI_CONF_CONFANNMON:
8000 /* First, add tx buffer to conf */
8001 ACSS(conf_sums_next[ms->_confn], getlin);
8002 /* Start with silence */
8003 memset(getlin, 0, DAHDI_CHUNKSIZE * sizeof(short));
8004 /* If a listener on the conf... */
8005 if (ms->confmode & DAHDI_CONF_LISTENER) {
8006 /* Subtract last value written */
8007 SCSS(getlin, ms->conflast);
8008 /* Add in conf */
8009 ACSS(getlin, conf_sums[ms->_confn]);
8010 }
8011 for (x=0;x<DAHDI_CHUNKSIZE;x++)
8012 txb[x] = DAHDI_LIN2X(getlin[x], ms);
8013 break;
8014 case DAHDI_CONF_DIGITALMON:
8015 /* Real digital monitoring, but still echo cancel if
8016 * desired */
8017 if (!conf_chan)
8018 break;
8019 if (is_pseudo_chan(conf_chan)) {
8020 if (ms->ec_state) {
8021 for (x = 0; x < DAHDI_CHUNKSIZE; x++)
8022 txb[x] = DAHDI_LIN2X(conf_chan->getlin[x], ms);
8023 } else {
8024 memcpy(txb, conf_chan->getraw, DAHDI_CHUNKSIZE);
8025 }
8026 } else {
8027 if (ms->ec_state) {
8028 for (x = 0; x < DAHDI_CHUNKSIZE; x++)
8029 txb[x] = DAHDI_LIN2X(conf_chan->putlin[x], ms);
8030 } else {
8031 memcpy(txb, conf_chan->putraw,
8032 DAHDI_CHUNKSIZE);
8033 }
8034 }
8035 for (x = 0; x < DAHDI_CHUNKSIZE; x++)
8036 getlin[x] = DAHDI_XLAW(txb[x], ms);
8037 break;
8038 }
8039 }
8040 if (ms->confmute || (ms->ec_state && (ms->ec_state->status.mode) & __ECHO_MODE_MUTE)) {
8041 txb[0] = DAHDI_LIN2X(0, ms);
8042 memset(txb + 1, txb[0], DAHDI_CHUNKSIZE - 1);
8043 if (ms->ec_state && (ms->ec_state->status.mode == ECHO_MODE_STARTTRAINING)) {
8044 /* Transmit impulse now */
8045 txb[0] = DAHDI_LIN2X(16384, ms);
8046 ms->ec_state->status.mode = ECHO_MODE_AWAITINGECHO;
8047 }
8048 }
8049 /* save value from current */
8050 memcpy(ms->getlin, getlin, DAHDI_CHUNKSIZE * sizeof(short));
8051 /* save value from current */
8052 memcpy(ms->getraw, txb, DAHDI_CHUNKSIZE);
8053 /* if to make tx tone */
8054 if (ms->v1_1 || ms->v2_1 || ms->v3_1)
8055 {
8056 for (x=0;x<DAHDI_CHUNKSIZE;x++)
8057 {
8058 getlin[x] += dahdi_txtone_nextsample(ms);
8059 txb[x] = DAHDI_LIN2X(getlin[x], ms);
8060 }
8061 }
8062 /* This is what to send (after having applied gain) */
8063 for (x=0;x<DAHDI_CHUNKSIZE;x++)
8064 txb[x] = ms->txgain[txb[x]];
8065 }
8066
8067 static void __putbuf_chunk(struct dahdi_chan *ss, unsigned char *rxb,
8068 int bytes);
8069
8070 static inline void __dahdi_getbuf_chunk(struct dahdi_chan *ss, unsigned char *txb)
8071 {
8072
8073
8074 #ifdef CONFIG_DAHDI_MIRROR
8075 unsigned char *orig_txb = txb;
8076 #endif /* CONFIG_DAHDI_MIRROR */
8077
8078 /* Called with ss->lock held */
8079 /* We transmit data from our master channel */
8080 struct dahdi_chan *ms = ss->master;
8081 /* Buffer we're using */
8082 unsigned char *buf;
8083 /* Old buffer number */
8084 int oldbuf;
8085 /* Linear representation */
8086 int getlin;
8087 /* How many bytes we need to process */
8088 int bytes = DAHDI_CHUNKSIZE, left;
8089 bool needtxunderrun = false;
8090 int x;
8091
8092 /* Let's pick something to transmit. First source to
8093 try is our write-out buffer. Always check it first because
8094 its our 'fast path' for whatever that's worth. */
8095 while(bytes) {
8096 if ((ms->outwritebuf > -1) && !ms->txdisable) {
8097 buf= ms->writebuf[ms->outwritebuf];
8098 left = ms->writen[ms->outwritebuf] - ms->writeidx[ms->outwritebuf];
8099 if (left > bytes)
8100 left = bytes;
8101 if (ms->flags & DAHDI_FLAG_HDLC) {
8102 /* If this is an HDLC channel we only send a byte of
8103 HDLC. */
8104 for(x=0;x<left;x++) {
8105 if (fasthdlc_tx_need_data(&ms->txhdlc))
8106 /* Load a byte of data only if needed */
8107 fasthdlc_tx_load_nocheck(&ms->txhdlc, buf[ms->writeidx[ms->outwritebuf]++]);
8108 *(txb++) = fasthdlc_tx_run_nocheck(&ms->txhdlc);
8109 }
8110 bytes -= left;
8111 } else {
8112 memcpy(txb, buf + ms->writeidx[ms->outwritebuf], left);
8113 ms->writeidx[ms->outwritebuf]+=left;
8114 txb += left;
8115 bytes -= left;
8116 }
8117 /* Check buffer status */
8118 if (ms->writeidx[ms->outwritebuf] >= ms->writen[ms->outwritebuf]) {
8119 /* We've reached the end of our buffer. Go to the next. */
8120 oldbuf = ms->outwritebuf;
8121 /* Clear out write index and such */
8122 ms->writeidx[oldbuf] = 0;
8123 ms->outwritebuf = (ms->outwritebuf + 1) % ms->numbufs;
8124
8125 if (!(ms->flags & DAHDI_FLAG_MTP2)) {
8126 ms->writen[oldbuf] = 0;
8127 if (ms->outwritebuf == ms->inwritebuf) {
8128 /* Whoopsies, we're run out of buffers. Mark ours
8129 as -1 and wait for the filler to notify us that
8130 there is something to write */
8131 ms->outwritebuf = -1;
8132 if (ms->iomask & (DAHDI_IOMUX_WRITE | DAHDI_IOMUX_WRITEEMPTY))
8133 wake_up_interruptible(&ms->waitq);
8134 /* If we're only supposed to start when full, disable the transmitter */
8135 if ((ms->txbufpolicy == DAHDI_POLICY_WHEN_FULL) ||
8136 (ms->txbufpolicy == DAHDI_POLICY_HALF_FULL))
8137 ms->txdisable = 1;
8138 }
8139 } else {
8140 if (ms->outwritebuf == ms->inwritebuf) {
8141 ms->outwritebuf = oldbuf;
8142 if (ms->iomask & (DAHDI_IOMUX_WRITE | DAHDI_IOMUX_WRITEEMPTY))
8143 wake_up_interruptible(&ms->waitq);
8144 /* If we're only supposed to start when full, disable the transmitter */
8145 if ((ms->txbufpolicy == DAHDI_POLICY_WHEN_FULL) ||
8146 (ms->txbufpolicy == DAHDI_POLICY_HALF_FULL))
8147 ms->txdisable = 1;
8148 }
8149 }
8150 if (ms->inwritebuf < 0) {
8151 /* The filler doesn't have a place to put data. Now
8152 that we're done with this buffer, notify them. */
8153 ms->inwritebuf = oldbuf;
8154 }
8155 /* In the very orignal driver, it was quite well known to me (Jim) that there
8156 was a possibility that a channel sleeping on a write block needed to
8157 be potentially woken up EVERY time a buffer was emptied, not just on the first
8158 one, because if only done on the first one there is a slight timing potential
8159 of missing the wakeup (between where it senses the (lack of) active condition
8160 (with interrupts disabled) and where it does the sleep (interrupts enabled)
8161 in the read or iomux call, etc). That is why the write and iomux calls start
8162 with an infinite loop that gets broken out of upon an active condition,
8163 otherwise keeps sleeping and looking. The part in this code got "optimized"
8164 out in the later versions, and is put back now. */
8165 if (!(ms->flags & DAHDI_FLAG_PPP) ||
8166 !dahdi_have_netdev(ms)) {
8167 wake_up_interruptible(&ms->waitq);
8168 }
8169 /* Transmit a flag if this is an HDLC channel */
8170 if (ms->flags & DAHDI_FLAG_HDLC)
8171 fasthdlc_tx_frame_nocheck(&ms->txhdlc);
8172 #ifdef CONFIG_DAHDI_NET
8173 if (dahdi_have_netdev(ms))
8174 netif_wake_queue(chan_to_netdev(ms));
8175 #endif
8176 #ifdef CONFIG_DAHDI_PPP
8177 if (ms->flags & DAHDI_FLAG_PPP) {
8178 ms->do_ppp_wakeup = 1;
8179 tasklet_schedule(&ms->ppp_calls);
8180 }
8181 #endif
8182 }
8183 } else if (ms->curtone && !is_pseudo_chan(ms)) {
8184 left = ms->curtone->tonesamples - ms->tonep;
8185 if (left > bytes)
8186 left = bytes;
8187 for (x=0;x<left;x++) {
8188 /* Pick our default value from the next sample of the current tone */
8189 getlin = dahdi_tone_nextsample(&ms->ts, ms->curtone);
8190 *(txb++) = DAHDI_LIN2X(getlin, ms);
8191 }
8192 ms->tonep+=left;
8193 bytes -= left;
8194 if (ms->tonep >= ms->curtone->tonesamples) {
8195 struct dahdi_tone *last;
8196 /* Go to the next sample of the tone */
8197 ms->tonep = 0;
8198 last = ms->curtone;
8199 ms->curtone = ms->curtone->next;
8200 if (!ms->curtone) {
8201 /* No more tones... Is this dtmf or mf? If so, go to the next digit */
8202 if (ms->dialing)
8203 __do_dtmf(ms);
8204 } else {
8205 if (last != ms->curtone)
8206 dahdi_init_tone_state(&ms->ts, ms->curtone);
8207 }
8208 }
8209 } else if (ms->flags & DAHDI_FLAG_LOOPED) {
8210 for (x = 0; x < bytes; x++)
8211 txb[x] = ms->readchunk[x];
8212 bytes = 0;
8213 } else if (ms->flags & DAHDI_FLAG_HDLC) {
8214 for (x=0;x<bytes;x++) {
8215 /* Okay, if we're HDLC, then transmit a flag by default */
8216 if (fasthdlc_tx_need_data(&ms->txhdlc))
8217 fasthdlc_tx_frame_nocheck(&ms->txhdlc);
8218 *(txb++) = fasthdlc_tx_run_nocheck(&ms->txhdlc);
8219 }
8220 bytes = 0;
8221 } else if (ms->flags & DAHDI_FLAG_CLEAR) {
8222 /* Clear channels that are idle in audio mode need
8223 to send silence; in non-audio mode, always send 0xff
8224 so stupid switches won't consider the channel active
8225 */
8226 if (ms->flags & DAHDI_FLAG_AUDIO) {
8227 memset(txb, DAHDI_LIN2X(0, ms), bytes);
8228 } else {
8229 memset(txb, 0xFF, bytes);
8230 }
8231 needtxunderrun += bytes;
8232 bytes = 0;
8233 } else {
8234 memset(txb, DAHDI_LIN2X(0, ms), bytes); /* Lastly we use silence on telephony channels */
8235 needtxunderrun += bytes;
8236 bytes = 0;
8237 }
8238 }
8239
8240 if (needtxunderrun) {
8241 if (!test_bit(DAHDI_FLAGBIT_TXUNDERRUN, &ms->flags)) {
8242 if (test_bit(DAHDI_FLAGBIT_BUFEVENTS, &ms->flags))
8243 __qevent(ms, DAHDI_EVENT_WRITE_UNDERRUN);
8244 set_bit(DAHDI_FLAGBIT_TXUNDERRUN, &ms->flags);
8245 }
8246 } else {
8247 clear_bit(DAHDI_FLAGBIT_TXUNDERRUN, &ms->flags);
8248 }
8249
8250 #ifdef CONFIG_DAHDI_MIRROR
8251 if (ss->txmirror) {
8252 spin_lock(&ss->txmirror->lock);
8253 __putbuf_chunk(ss->txmirror, orig_txb, DAHDI_CHUNKSIZE);
8254 spin_unlock(&ss->txmirror->lock);
8255 }
8256 #endif /* CONFIG_DAHDI_MIRROR */
8257 }
8258
8259 static inline void rbs_itimer_expire(struct dahdi_chan *chan)
8260 {
8261 /* the only way this could have gotten here, is if a channel
8262 went onf hook longer then the wink or flash detect timeout */
8263 /* Called with chan->lock held */
8264 switch(chan->sig)
8265 {
8266 case DAHDI_SIG_FXOLS: /* if FXO, its definitely on hook */
8267 case DAHDI_SIG_FXOGS:
8268 case DAHDI_SIG_FXOKS:
8269 __qevent(chan,DAHDI_EVENT_ONHOOK);
8270 chan->gotgs = 0;
8271 break;
8272 #if defined(EMFLASH) || defined(EMPULSE)
8273 case DAHDI_SIG_EM:
8274 case DAHDI_SIG_EM_E1:
8275 if (chan->rxhooksig == DAHDI_RXSIG_ONHOOK) {
8276 __qevent(chan,DAHDI_EVENT_ONHOOK);
8277 break;
8278 }
8279 __qevent(chan,DAHDI_EVENT_RINGOFFHOOK);
8280 break;
8281 #endif
8282 #ifdef FXSFLASH
8283 case DAHDI_SIG_FXSKS:
8284 if (chan->rxhooksig == DAHDI_RXSIG_ONHOOK) {
8285 __qevent(chan, DAHDI_EVENT_ONHOOK);
8286 break;
8287 }
8288 #endif
8289 /* fall thru intentionally */
8290 default: /* otherwise, its definitely off hook */
8291 __qevent(chan,DAHDI_EVENT_RINGOFFHOOK);
8292 break;
8293 }
8294 }
8295
8296 static inline void __rbs_otimer_expire(struct dahdi_chan *chan)
8297 {
8298 int len = 0;
8299 /* Called with chan->lock held */
8300
8301 chan->otimer = 0;
8302 /* Move to the next timer state */
8303 switch(chan->txstate) {
8304 case DAHDI_TXSTATE_RINGOFF:
8305 /* Turn on the ringer now that the silent time has passed */
8306 ++chan->cadencepos;
8307 if (chan->cadencepos >= DAHDI_MAX_CADENCE)
8308 chan->cadencepos = chan->firstcadencepos;
8309 len = chan->ringcadence[chan->cadencepos];
8310
8311 if (!len) {
8312 chan->cadencepos = chan->firstcadencepos;
8313 len = chan->ringcadence[chan->cadencepos];
8314 }
8315
8316 dahdi_rbs_sethook(chan, DAHDI_TXSIG_START, DAHDI_TXSTATE_RINGON, len);
8317 __qevent(chan, DAHDI_EVENT_RINGERON);
8318 break;
8319
8320 case DAHDI_TXSTATE_RINGON:
8321 /* Turn off the ringer now that the loud time has passed */
8322 ++chan->cadencepos;
8323 if (chan->cadencepos >= DAHDI_MAX_CADENCE)
8324 chan->cadencepos = 0;
8325 len = chan->ringcadence[chan->cadencepos];
8326
8327 if (!len) {
8328 chan->cadencepos = 0;
8329 len = chan->curzone->ringcadence[chan->cadencepos];
8330 }
8331
8332 dahdi_rbs_sethook(chan, DAHDI_TXSIG_OFFHOOK, DAHDI_TXSTATE_RINGOFF, len);
8333 __qevent(chan, DAHDI_EVENT_RINGEROFF);
8334 break;
8335
8336 case DAHDI_TXSTATE_START:
8337 /* If we were starting, go off hook now ready to debounce */
8338 dahdi_rbs_sethook(chan, DAHDI_TXSIG_OFFHOOK, DAHDI_TXSTATE_AFTERSTART, DAHDI_AFTERSTART_TIME);
8339 wake_up_interruptible(&chan->waitq);
8340 break;
8341
8342 case DAHDI_TXSTATE_PREWINK:
8343 /* Actually wink */
8344 dahdi_rbs_sethook(chan, DAHDI_TXSIG_OFFHOOK, DAHDI_TXSTATE_WINK, chan->winktime);
8345 break;
8346
8347 case DAHDI_TXSTATE_WINK:
8348 /* Wink complete, go on hook and stabalize */
8349 dahdi_rbs_sethook(chan, DAHDI_TXSIG_ONHOOK, DAHDI_TXSTATE_ONHOOK, 0);
8350 if (chan->file && (chan->file->f_flags & O_NONBLOCK))
8351 __qevent(chan, DAHDI_EVENT_HOOKCOMPLETE);
8352 wake_up_interruptible(&chan->waitq);
8353 break;
8354
8355 case DAHDI_TXSTATE_PREFLASH:
8356 /* Actually flash */
8357 dahdi_rbs_sethook(chan, DAHDI_TXSIG_ONHOOK, DAHDI_TXSTATE_FLASH, chan->flashtime);
8358 break;
8359
8360 case DAHDI_TXSTATE_FLASH:
8361 dahdi_rbs_sethook(chan, DAHDI_TXSIG_OFFHOOK, DAHDI_TXSTATE_OFFHOOK, 0);
8362 if (chan->file && (chan->file->f_flags & O_NONBLOCK))
8363 __qevent(chan, DAHDI_EVENT_HOOKCOMPLETE);
8364 wake_up_interruptible(&chan->waitq);
8365 break;
8366
8367 case DAHDI_TXSTATE_DEBOUNCE:
8368 dahdi_rbs_sethook(chan, DAHDI_TXSIG_OFFHOOK, DAHDI_TXSTATE_OFFHOOK, 0);
8369 /* See if we've gone back on hook */
8370 if ((chan->rxhooksig == DAHDI_RXSIG_ONHOOK) && (chan->rxflashtime > 2))
8371 chan->itimerset = chan->itimer = chan->rxflashtime * DAHDI_CHUNKSIZE;
8372 wake_up_interruptible(&chan->waitq);
8373 break;
8374
8375 case DAHDI_TXSTATE_AFTERSTART:
8376 dahdi_rbs_sethook(chan, DAHDI_TXSIG_OFFHOOK, DAHDI_TXSTATE_OFFHOOK, 0);
8377 if (chan->file && (chan->file->f_flags & O_NONBLOCK))
8378 __qevent(chan, DAHDI_EVENT_HOOKCOMPLETE);
8379 wake_up_interruptible(&chan->waitq);
8380 break;
8381
8382 case DAHDI_TXSTATE_KEWL:
8383 dahdi_rbs_sethook(chan, DAHDI_TXSIG_ONHOOK, DAHDI_TXSTATE_AFTERKEWL, DAHDI_AFTERKEWLTIME);
8384 if (chan->file && (chan->file->f_flags & O_NONBLOCK))
8385 __qevent(chan, DAHDI_EVENT_HOOKCOMPLETE);
8386 wake_up_interruptible(&chan->waitq);
8387 break;
8388
8389 case DAHDI_TXSTATE_AFTERKEWL:
8390 if (chan->kewlonhook) {
8391 __qevent(chan,DAHDI_EVENT_ONHOOK);
8392 }
8393 chan->txstate = DAHDI_TXSTATE_ONHOOK;
8394 chan->gotgs = 0;
8395 break;
8396
8397 case DAHDI_TXSTATE_PULSEBREAK:
8398 dahdi_rbs_sethook(chan, DAHDI_TXSIG_OFFHOOK, DAHDI_TXSTATE_PULSEMAKE,
8399 chan->pulsemaketime);
8400 wake_up_interruptible(&chan->waitq);
8401 break;
8402
8403 case DAHDI_TXSTATE_PULSEMAKE:
8404 if (chan->pdialcount)
8405 chan->pdialcount--;
8406 if (chan->pdialcount)
8407 {
8408 dahdi_rbs_sethook(chan, DAHDI_TXSIG_ONHOOK,
8409 DAHDI_TXSTATE_PULSEBREAK, chan->pulsebreaktime);
8410 break;
8411 }
8412 chan->txstate = DAHDI_TXSTATE_PULSEAFTER;
8413 chan->otimer = chan->pulseaftertime * DAHDI_CHUNKSIZE;
8414 wake_up_interruptible(&chan->waitq);
8415 break;
8416
8417 case DAHDI_TXSTATE_PULSEAFTER:
8418 chan->txstate = DAHDI_TXSTATE_OFFHOOK;
8419 __do_dtmf(chan);
8420 wake_up_interruptible(&chan->waitq);
8421 break;
8422
8423 default:
8424 break;
8425 }
8426 }
8427
8428 static void __dahdi_hooksig_pvt(struct dahdi_chan *chan, enum dahdi_rxsig rxsig)
8429 {
8430
8431 /* State machines for receive hookstate transitions
8432 called with chan->lock held */
8433
8434 if ((chan->rxhooksig) == rxsig) return;
8435
8436 if ((chan->flags & DAHDI_FLAG_SIGFREEZE)) return;
8437
8438 chan->rxhooksig = rxsig;
8439 #ifdef RINGBEGIN
8440 if ((chan->sig & __DAHDI_SIG_FXS) && (rxsig == DAHDI_RXSIG_RING) &&
8441 (!chan->ringdebtimer))
8442 __qevent(chan,DAHDI_EVENT_RINGBEGIN);
8443 #endif
8444 switch(chan->sig) {
8445 case DAHDI_SIG_EM: /* E and M */
8446 case DAHDI_SIG_EM_E1:
8447 switch(rxsig) {
8448 case DAHDI_RXSIG_OFFHOOK: /* went off hook */
8449 /* The interface is going off hook */
8450 #ifdef EMFLASH
8451 if (chan->itimer)
8452 {
8453 __qevent(chan,DAHDI_EVENT_WINKFLASH);
8454 chan->itimerset = chan->itimer = 0;
8455 break;
8456 }
8457 #endif
8458 #ifdef EMPULSE
8459 if (chan->itimer) /* if timer still running */
8460 {
8461 int plen = chan->itimerset - chan->itimer;
8462 if (plen <= DAHDI_MAXPULSETIME)
8463 {
8464 if (plen >= DAHDI_MINPULSETIME)
8465 {
8466 chan->pulsecount++;
8467
8468 chan->pulsetimer = DAHDI_PULSETIMEOUT;
8469 chan->itimerset = chan->itimer = 0;
8470 if (chan->pulsecount == 1)
8471 __qevent(chan,DAHDI_EVENT_PULSE_START);
8472 }
8473 }
8474 break;
8475 }
8476 #endif
8477 /* set wink timer */
8478 chan->itimerset = chan->itimer = chan->rxwinktime * DAHDI_CHUNKSIZE;
8479 break;
8480 case DAHDI_RXSIG_ONHOOK: /* went on hook */
8481 /* This interface is now going on hook.
8482 Check for WINK, etc */
8483 if (chan->itimer)
8484 __qevent(chan,DAHDI_EVENT_WINKFLASH);
8485 #if defined(EMFLASH) || defined(EMPULSE)
8486 else {
8487 #ifdef EMFLASH
8488 chan->itimerset = chan->itimer = chan->rxflashtime * DAHDI_CHUNKSIZE;
8489
8490 #else /* EMFLASH */
8491 chan->itimerset = chan->itimer = chan->rxwinktime * DAHDI_CHUNKSIZE;
8492
8493 #endif /* EMFLASH */
8494 chan->gotgs = 0;
8495 break;
8496 }
8497 #else /* EMFLASH || EMPULSE */
8498 else {
8499 __qevent(chan,DAHDI_EVENT_ONHOOK);
8500 chan->gotgs = 0;
8501 }
8502 #endif
8503 chan->itimerset = chan->itimer = 0;
8504 break;
8505 default:
8506 break;
8507 }
8508 break;
8509 case DAHDI_SIG_FXSKS: /* FXS Kewlstart */
8510 /* ignore a bit error if loop not closed and stable */
8511 if (chan->txstate != DAHDI_TXSTATE_OFFHOOK) break;
8512 #ifdef FXSFLASH
8513 if (rxsig == DAHDI_RXSIG_ONHOOK) {
8514 chan->itimer = DAHDI_FXSFLASHMAXTIME * DAHDI_CHUNKSIZE;
8515 break;
8516 } else if (rxsig == DAHDI_RXSIG_OFFHOOK) {
8517 if (chan->itimer) {
8518 /* did the offhook occur in the window? if not, ignore both events */
8519 if (chan->itimer <= ((DAHDI_FXSFLASHMAXTIME - DAHDI_FXSFLASHMINTIME) * DAHDI_CHUNKSIZE))
8520 __qevent(chan, DAHDI_EVENT_WINKFLASH);
8521 }
8522 chan->itimer = 0;
8523 break;
8524 }
8525 #endif
8526 /* fall through intentionally */
8527 case DAHDI_SIG_FXSGS: /* FXS Groundstart */
8528 if (rxsig == DAHDI_RXSIG_ONHOOK) {
8529 chan->ringdebtimer = RING_DEBOUNCE_TIME;
8530 chan->ringtrailer = 0;
8531 if (chan->txstate != DAHDI_TXSTATE_DEBOUNCE) {
8532 chan->gotgs = 0;
8533 __qevent(chan,DAHDI_EVENT_ONHOOK);
8534 }
8535 }
8536 break;
8537 case DAHDI_SIG_FXOGS: /* FXO Groundstart */
8538 if (rxsig == DAHDI_RXSIG_START) {
8539 /* if havent got gs, report it */
8540 if (!chan->gotgs) {
8541 __qevent(chan,DAHDI_EVENT_RINGOFFHOOK);
8542 chan->gotgs = 1;
8543 }
8544 }
8545 /* fall through intentionally */
8546 case DAHDI_SIG_FXOLS: /* FXO Loopstart */
8547 case DAHDI_SIG_FXOKS: /* FXO Kewlstart */
8548 switch(rxsig) {
8549 case DAHDI_RXSIG_OFFHOOK: /* went off hook */
8550 /* if asserti ng ring, stop it */
8551 if (chan->txstate == DAHDI_TXSTATE_START) {
8552 dahdi_rbs_sethook(chan,DAHDI_TXSIG_OFFHOOK, DAHDI_TXSTATE_AFTERSTART, DAHDI_AFTERSTART_TIME);
8553 }
8554 chan->kewlonhook = 0;
8555 #ifdef CONFIG_DAHDI_DEBUG
8556 module_printk(KERN_NOTICE, "Off hook on channel %d, itimer = %d, gotgs = %d\n", chan->channo, chan->itimer, chan->gotgs);
8557 #endif
8558 if (chan->itimer) /* if timer still running */
8559 {
8560 int plen = chan->itimerset - chan->itimer;
8561 if (plen <= DAHDI_MAXPULSETIME)
8562 {
8563 if (plen >= DAHDI_MINPULSETIME)
8564 {
8565 chan->pulsecount++;
8566 chan->pulsetimer = DAHDI_PULSETIMEOUT;
8567 chan->itimer = chan->itimerset;
8568 if (chan->pulsecount == 1)
8569 __qevent(chan,DAHDI_EVENT_PULSE_START);
8570 }
8571 } else
8572 __qevent(chan,DAHDI_EVENT_WINKFLASH);
8573 } else {
8574 /* if havent got GS detect */
8575 if (!chan->gotgs) {
8576 __qevent(chan,DAHDI_EVENT_RINGOFFHOOK);
8577 chan->gotgs = 1;
8578 chan->itimerset = chan->itimer = 0;
8579 }
8580 }
8581 chan->itimerset = chan->itimer = 0;
8582 break;
8583 case DAHDI_RXSIG_ONHOOK: /* went on hook */
8584 /* if not during offhook debounce time */
8585 if ((chan->txstate != DAHDI_TXSTATE_DEBOUNCE) &&
8586 (chan->txstate != DAHDI_TXSTATE_KEWL) &&
8587 (chan->txstate != DAHDI_TXSTATE_AFTERKEWL)) {
8588 chan->itimerset = chan->itimer = chan->rxflashtime * DAHDI_CHUNKSIZE;
8589 }
8590 if (chan->txstate == DAHDI_TXSTATE_KEWL)
8591 chan->kewlonhook = 1;
8592 break;
8593 default:
8594 break;
8595 }
8596 default:
8597 break;
8598 }
8599 }
8600
8601 /**
8602 * dahdi_hooksig() - send a signal on a channel to userspace
8603 * @chan: the DAHDI channel
8604 * @rxsig: signal (number) to send
8605 *
8606 * Called from a channel driver to send a DAHDI signal to userspace.
8607 * The signal will be queued for delivery to userspace.
8608 *
8609 * If the signal is the same as previous one sent, it won't be re-sent.
8610 */
8611 void dahdi_hooksig(struct dahdi_chan *chan, enum dahdi_rxsig rxsig)
8612 {
8613 /* skip if no change */
8614 unsigned long flags;
8615 spin_lock_irqsave(&chan->lock, flags);
8616 __dahdi_hooksig_pvt(chan,rxsig);
8617 spin_unlock_irqrestore(&chan->lock, flags);
8618 }
8619
8620
8621 /**
8622 * dahdi_rbsbits() - set Rx RBS bits on the channel
8623 * @chan: the DAHDI channel
8624 * @cursig: the bits to set
8625 *
8626 * Set the channel's rxsig (received: from device to userspace) and act
8627 * accordingly.
8628 */
8629 void dahdi_rbsbits(struct dahdi_chan *chan, int cursig)
8630 {
8631 unsigned long flags;
8632 if (cursig == chan->rxsig)
8633 return;
8634
8635 if ((chan->flags & DAHDI_FLAG_SIGFREEZE)) return;
8636
8637 spin_lock_irqsave(&chan->lock, flags);
8638 switch(chan->sig) {
8639 case DAHDI_SIG_FXOGS: /* FXO Groundstart */
8640 /* B-bit only matters for FXO GS */
8641 if (!(cursig & DAHDI_BBIT)) {
8642 __dahdi_hooksig_pvt(chan, DAHDI_RXSIG_START);
8643 break;
8644 }
8645 /* Fall through */
8646 case DAHDI_SIG_EM_E1:
8647 case DAHDI_SIG_FXOLS: /* FXO Loopstart */
8648 case DAHDI_SIG_FXOKS: /* FXO Kewlstart */
8649 if (cursig & DAHDI_ABIT) /* off hook */
8650 __dahdi_hooksig_pvt(chan,DAHDI_RXSIG_OFFHOOK);
8651 else /* on hook */
8652 __dahdi_hooksig_pvt(chan,DAHDI_RXSIG_ONHOOK);
8653 break;
8654 case DAHDI_SIG_EM: /* E and M */
8655 /* Watch only the ABIT for changes. */
8656 if ((cursig & DAHDI_ABIT) == (chan->rxsig & DAHDI_ABIT))
8657 break;
8658 __dahdi_hooksig_pvt(chan, (cursig & DAHDI_ABIT) ?
8659 DAHDI_RXSIG_OFFHOOK : DAHDI_RXSIG_ONHOOK);
8660 break;
8661 case DAHDI_SIG_FXSKS: /* FXS Kewlstart */
8662 case DAHDI_SIG_FXSGS: /* FXS Groundstart */
8663 /* Fall through */
8664 case DAHDI_SIG_FXSLS:
8665 if (!(cursig & DAHDI_BBIT)) {
8666 /* Check for ringing first */
8667 __dahdi_hooksig_pvt(chan, DAHDI_RXSIG_RING);
8668 break;
8669 }
8670 if ((chan->sig != DAHDI_SIG_FXSLS) && (cursig & DAHDI_ABIT)) {
8671 /* if went on hook */
8672 __dahdi_hooksig_pvt(chan, DAHDI_RXSIG_ONHOOK);
8673 } else {
8674 __dahdi_hooksig_pvt(chan, DAHDI_RXSIG_OFFHOOK);
8675 }
8676 break;
8677 case DAHDI_SIG_CAS:
8678 /* send event that something changed */
8679 __qevent(chan, DAHDI_EVENT_BITSCHANGED);
8680 break;
8681
8682 default:
8683 break;
8684 }
8685 /* Keep track of signalling for next time */
8686 chan->rxsig = cursig;
8687 spin_unlock_irqrestore(&chan->lock, flags);
8688
8689 if ((debug & DEBUG_RBS) && printk_ratelimit()) {
8690 chan_notice(chan, "Detected sigbits change to %04x\n", cursig);
8691 }
8692 }
8693
8694 static void process_echocan_events(struct dahdi_chan *chan)
8695 {
8696 union dahdi_echocan_events events = chan->ec_state->events;
8697
8698 if (events.bit.CED_tx_detected) {
8699 dahdi_qevent_nolock(chan, DAHDI_EVENT_TX_CED_DETECTED);
8700 if (chan->ec_state) {
8701 if (chan->ec_state->status.mode == ECHO_MODE_ACTIVE)
8702 set_echocan_fax_mode(chan, chan->channo, "CED tx detected", 1);
8703 else
8704 module_printk(KERN_NOTICE, "Detected CED tone (tx) on channel %d\n", chan->channo);
8705 }
8706 }
8707
8708 if (events.bit.CED_rx_detected) {
8709 dahdi_qevent_nolock(chan, DAHDI_EVENT_RX_CED_DETECTED);
8710 if (chan->ec_state) {
8711 if (chan->ec_state->status.mode == ECHO_MODE_ACTIVE)
8712 set_echocan_fax_mode(chan, chan->channo, "CED rx detected", 1);
8713 else
8714 module_printk(KERN_NOTICE, "Detected CED tone (rx) on channel %d\n", chan->channo);
8715 }
8716 }
8717
8718 if (events.bit.CNG_tx_detected)
8719 dahdi_qevent_nolock(chan, DAHDI_EVENT_TX_CNG_DETECTED);
8720
8721 if (events.bit.CNG_rx_detected)
8722 dahdi_qevent_nolock(chan, DAHDI_EVENT_RX_CNG_DETECTED);
8723
8724 if (events.bit.NLP_auto_disabled) {
8725 dahdi_qevent_nolock(chan, DAHDI_EVENT_EC_NLP_DISABLED);
8726 chan->ec_state->status.mode = ECHO_MODE_FAX;
8727 }
8728
8729 if (events.bit.NLP_auto_enabled) {
8730 dahdi_qevent_nolock(chan, DAHDI_EVENT_EC_NLP_ENABLED);
8731 chan->ec_state->status.mode = ECHO_MODE_ACTIVE;
8732 }
8733 }
8734
8735 /**
8736 * __dahdi_ec_chunk() - process echo for a single channel
8737 * @ss: DAHDI channel
8738 * @rxchunk: buffer to store audio with cancelled audio
8739 * @preecchunk: chunk of audio on which to cancel echo
8740 * @txchunk: reference chunk from the other direction
8741 *
8742 * The echo canceller function fixes received (from device to userspace)
8743 * audio. In order to fix it it uses the transmitted audio as a
8744 * reference. This call updates the echo canceller for a single chunk (8
8745 * bytes).
8746 *
8747 * Call with local interrupts disabled.
8748 */
8749 void __dahdi_ec_chunk(struct dahdi_chan *ss, u8 *rxchunk,
8750 const u8 *preecchunk, const u8 *txchunk)
8751 {
8752 short rxlin;
8753 int x;
8754
8755 spin_lock(&ss->lock);
8756
8757 if (ss->readchunkpreec) {
8758 /* Save a copy of the audio before the echo can has its way with it */
8759 for (x = 0; x < DAHDI_CHUNKSIZE; x++)
8760 /* We only ever really need to deal with signed linear - let's just convert it now */
8761 ss->readchunkpreec[x] = DAHDI_XLAW(preecchunk[x], ss);
8762 }
8763
8764 /* Perform echo cancellation on a chunk if necessary */
8765 if (ss->ec_state) {
8766 #if defined(CONFIG_DAHDI_MMX) || defined(ECHO_CAN_FP)
8767 dahdi_kernel_fpu_begin();
8768 #endif
8769 if (ss->ec_state->status.mode & __ECHO_MODE_MUTE) {
8770 /* Special stuff for training the echo can */
8771 for (x=0;x<DAHDI_CHUNKSIZE;x++) {
8772 rxlin = DAHDI_XLAW(preecchunk[x], ss);
8773 if (ss->ec_state->status.mode == ECHO_MODE_PRETRAINING) {
8774 if (--ss->ec_state->status.pretrain_timer <= 0) {
8775 ss->ec_state->status.pretrain_timer = 0;
8776 ss->ec_state->status.mode = ECHO_MODE_STARTTRAINING;
8777 }
8778 }
8779 if (ss->ec_state->status.mode == ECHO_MODE_AWAITINGECHO) {
8780 ss->ec_state->status.last_train_tap = 0;
8781 ss->ec_state->status.mode = ECHO_MODE_TRAINING;
8782 }
8783 if ((ss->ec_state->status.mode == ECHO_MODE_TRAINING) &&
8784 (ss->ec_state->ops->echocan_traintap)) {
8785 if (ss->ec_state->ops->echocan_traintap(ss->ec_state, ss->ec_state->status.last_train_tap++, rxlin)) {
8786 ss->ec_state->status.mode = ECHO_MODE_ACTIVE;
8787 }
8788 }
8789 rxlin = 0;
8790 rxchunk[x] = DAHDI_LIN2X((int)rxlin, ss);
8791 }
8792 } else if (ss->ec_state->status.mode != ECHO_MODE_IDLE) {
8793 ss->ec_state->events.all = 0;
8794
8795 if (ss->ec_state->ops->echocan_process) {
8796 short rxlins[DAHDI_CHUNKSIZE], txlins[DAHDI_CHUNKSIZE];
8797
8798 for (x = 0; x < DAHDI_CHUNKSIZE; x++) {
8799 rxlins[x] = DAHDI_XLAW(preecchunk[x],
8800 ss);
8801 txlins[x] = DAHDI_XLAW(txchunk[x], ss);
8802 }
8803 ss->ec_state->ops->echocan_process(ss->ec_state, rxlins, txlins, DAHDI_CHUNKSIZE);
8804
8805 for (x = 0; x < DAHDI_CHUNKSIZE; x++)
8806 rxchunk[x] = DAHDI_LIN2X((int) rxlins[x], ss);
8807 } else if (ss->ec_state->ops->echocan_events)
8808 ss->ec_state->ops->echocan_events(ss->ec_state);
8809
8810 if (ss->ec_state->events.all)
8811 process_echocan_events(ss);
8812
8813 }
8814 #if defined(CONFIG_DAHDI_MMX) || defined(ECHO_CAN_FP)
8815 dahdi_kernel_fpu_end();
8816 #endif
8817 }
8818
8819 spin_unlock(&ss->lock);
8820 }
8821 EXPORT_SYMBOL(__dahdi_ec_chunk);
8822
8823 /**
8824 * dahdi_ec_span() - process echo for all channels in a span.
8825 * @span: DAHDI span
8826 *
8827 * Similar to calling dahdi_ec_chunk() for each of the channels in the
8828 * span. Uses dahdi_chunk.write_chunk for the rxchunk (the chunk to fix)
8829 * and dahdi_chan.readchunk as the txchunk (the reference chunk).
8830 */
8831 void _dahdi_ec_span(struct dahdi_span *span)
8832 {
8833 int x;
8834 for (x = 0; x < span->channels; x++) {
8835 struct dahdi_chan *const chan = span->chans[x];
8836 if (!chan->ec_current)
8837 continue;
8838 _dahdi_ec_chunk(chan, chan->readchunk, chan->writechunk);
8839 }
8840 }
8841 EXPORT_SYMBOL(_dahdi_ec_span);
8842
8843 /* return 0 if nothing detected, 1 if lack of tone, 2 if presence of tone */
8844 /* modifies buffer pointed to by 'amp' with notched-out values */
8845 static inline int sf_detect(struct sf_detect_state *s,
8846 short *amp,
8847 int samples,long p1, long p2, long p3)
8848 {
8849 int i,rv = 0;
8850 long x,y;
8851
8852 #define SF_DETECT_SAMPLES (DAHDI_CHUNKSIZE * 5)
8853 #define SF_DETECT_MIN_ENERGY 500
8854 #define NB 14 /* number of bits to shift left */
8855
8856 /* determine energy level before filtering */
8857 for(i = 0; i < samples; i++)
8858 {
8859 if (amp[i] < 0) s->e1 -= amp[i];
8860 else s->e1 += amp[i];
8861 }
8862 /* do 2nd order IIR notch filter at given freq. and calculate
8863 energy */
8864 for(i = 0; i < samples; i++)
8865 {
8866 x = amp[i] << NB;
8867 y = s->x2 + (p1 * (s->x1 >> NB)) + x;
8868 y += (p2 * (s->y2 >> NB)) +
8869 (p3 * (s->y1 >> NB));
8870 s->x2 = s->x1;
8871 s->x1 = x;
8872 s->y2 = s->y1;
8873 s->y1 = y;
8874 amp[i] = y >> NB;
8875 if (amp[i] < 0) s->e2 -= amp[i];
8876 else s->e2 += amp[i];
8877 }
8878 s->samps += i;
8879 /* if time to do determination */
8880 if ((s->samps) >= SF_DETECT_SAMPLES)
8881 {
8882 rv = 1; /* default to no tone */
8883 /* if enough energy, it is determined to be a tone */
8884 if (((s->e1 - s->e2) / s->samps) > SF_DETECT_MIN_ENERGY) rv = 2;
8885 /* reset energy processing variables */
8886 s->samps = 0;
8887 s->e1 = s->e2 = 0;
8888 }
8889 return(rv);
8890 }
8891
8892 static inline void __dahdi_process_putaudio_chunk(struct dahdi_chan *ss, unsigned char *rxb)
8893 {
8894 /* We transmit data from our master channel */
8895 /* Called with ss->lock held */
8896 struct dahdi_chan *ms = ss->master;
8897 /* Linear version of received data */
8898 short putlin[DAHDI_CHUNKSIZE],k[DAHDI_CHUNKSIZE];
8899 int x,r;
8900
8901 if (ms->dialing) ms->afterdialingtimer = 50;
8902 else if (ms->afterdialingtimer) ms->afterdialingtimer--;
8903 if (ms->afterdialingtimer && !is_pseudo_chan(ms)) {
8904 /* Be careful since memset is likely a macro */
8905 rxb[0] = DAHDI_LIN2X(0, ms);
8906 memset(&rxb[1], rxb[0], DAHDI_CHUNKSIZE - 1); /* receive as silence if dialing */
8907 }
8908 for (x=0;x<DAHDI_CHUNKSIZE;x++) {
8909 rxb[x] = ms->rxgain[rxb[x]];
8910 putlin[x] = DAHDI_XLAW(rxb[x], ms);
8911 }
8912
8913 #ifndef CONFIG_DAHDI_NO_ECHOCAN_DISABLE
8914 if (ms->ec_state && (ms->ec_state->status.mode == ECHO_MODE_ACTIVE) && !ms->ec_state->features.CED_rx_detect) {
8915 for (x = 0; x < DAHDI_CHUNKSIZE; x++) {
8916 if (echo_can_disable_detector_update(&ms->ec_state->rxecdis, putlin[x])) {
8917 set_echocan_fax_mode(ms, ss->channo, "CED rx detected", 1);
8918 dahdi_qevent_nolock(ms, DAHDI_EVENT_RX_CED_DETECTED);
8919 break;
8920 }
8921 }
8922 }
8923 #endif
8924
8925 /* if doing rx tone decoding */
8926 if (ms->rxp1 && ms->rxp2 && ms->rxp3)
8927 {
8928 r = sf_detect(&ms->rd,putlin,DAHDI_CHUNKSIZE,ms->rxp1,
8929 ms->rxp2,ms->rxp3);
8930 /* Convert back */
8931 for(x=0;x<DAHDI_CHUNKSIZE;x++)
8932 rxb[x] = DAHDI_LIN2X(putlin[x], ms);
8933 if (r) /* if something happened */
8934 {
8935 if (r != ms->rd.lastdetect)
8936 {
8937 if (((r == 2) && !(ms->toneflags & DAHDI_REVERSE_RXTONE)) ||
8938 ((r == 1) && (ms->toneflags & DAHDI_REVERSE_RXTONE)))
8939 {
8940 __qevent(ms,DAHDI_EVENT_RINGOFFHOOK);
8941 }
8942 else
8943 {
8944 __qevent(ms,DAHDI_EVENT_ONHOOK);
8945 }
8946 ms->rd.lastdetect = r;
8947 }
8948 }
8949 }
8950
8951 if (!is_pseudo_chan(ms)) {
8952 memcpy(ms->putlin, putlin, DAHDI_CHUNKSIZE * sizeof(short));
8953 memcpy(ms->putraw, rxb, DAHDI_CHUNKSIZE);
8954 }
8955
8956 /* Take the rxc, twiddle it for conferencing if appropriate and put it
8957 back */
8958 if ((!ms->confmute && !ms->afterdialingtimer) || is_pseudo_chan(ms)) {
8959 struct dahdi_chan *const conf_chan = ms->conf_chan;
8960 switch(ms->confmode & DAHDI_CONF_MODE_MASK) {
8961 case DAHDI_CONF_NORMAL: /* Normal mode */
8962 /* Do nothing. rx goes output */
8963 break;
8964 case DAHDI_CONF_MONITOR: /* Monitor a channel's rx mode */
8965 /* if not a pseudo-channel, ignore */
8966 if (!is_pseudo_chan(ms))
8967 break;
8968 /* Add monitored channel */
8969 if (is_pseudo_chan(conf_chan))
8970 ACSS(putlin, conf_chan->getlin);
8971 else
8972 ACSS(putlin, conf_chan->putlin);
8973 /* Convert back */
8974 for(x=0;x<DAHDI_CHUNKSIZE;x++)
8975 rxb[x] = DAHDI_LIN2X(putlin[x], ms);
8976 break;
8977 case DAHDI_CONF_MONITORTX: /* Monitor a channel's tx mode */
8978 /* if not a pseudo-channel, ignore */
8979 if (!is_pseudo_chan(ms))
8980 break;
8981 /* Add monitored channel */
8982 if (is_pseudo_chan(conf_chan))
8983 ACSS(putlin, conf_chan->putlin);
8984 else
8985 ACSS(putlin, conf_chan->getlin);
8986 /* Convert back */
8987 for(x=0;x<DAHDI_CHUNKSIZE;x++)
8988 rxb[x] = DAHDI_LIN2X(putlin[x], ms);
8989 break;
8990 case DAHDI_CONF_MONITORBOTH: /* Monitor a channel's tx and rx mode */
8991 /* if not a pseudo-channel, ignore */
8992 if (!is_pseudo_chan(ms))
8993 break;
8994 /* Note: Technically, saturation should be done at
8995 the end of the whole addition, but for performance
8996 reasons, we don't do that. Besides, it only matters
8997 when you're so loud you're clipping anyway */
8998 ACSS(putlin, conf_chan->getlin);
8999 ACSS(putlin, conf_chan->putlin);
9000 /* Convert back */
9001 for(x=0;x<DAHDI_CHUNKSIZE;x++)
9002 rxb[x] = DAHDI_LIN2X(putlin[x], ms);
9003 break;
9004 case DAHDI_CONF_MONITOR_RX_PREECHO: /* Monitor a channel's rx mode */
9005 /* if not a pseudo-channel, ignore */
9006 if (!is_pseudo_chan(ms))
9007 break;
9008
9009 if (!conf_chan->readchunkpreec)
9010 break;
9011
9012 /* Add monitored channel */
9013 ACSS(putlin, is_pseudo_chan(conf_chan) ?
9014 conf_chan->getlin : conf_chan->readchunkpreec);
9015 for (x = 0; x < DAHDI_CHUNKSIZE; x++)
9016 rxb[x] = DAHDI_LIN2X(putlin[x], ms);
9017
9018 break;
9019 case DAHDI_CONF_MONITOR_TX_PREECHO: /* Monitor a channel's tx mode */
9020 /* if not a pseudo-channel, ignore */
9021 if (!is_pseudo_chan(ms))
9022 break;
9023
9024 if (!conf_chan->readchunkpreec)
9025 break;
9026
9027 /* Add monitored channel */
9028 ACSS(putlin, is_pseudo_chan(conf_chan) ?
9029 conf_chan->readchunkpreec : conf_chan->getlin);
9030 for (x = 0; x < DAHDI_CHUNKSIZE; x++)
9031 rxb[x] = DAHDI_LIN2X(putlin[x], ms);
9032
9033 break;
9034 case DAHDI_CONF_MONITORBOTH_PREECHO: /* Monitor a channel's tx and rx mode */
9035 /* if not a pseudo-channel, ignore */
9036 if (!is_pseudo_chan(ms))
9037 break;
9038
9039 if (!conf_chan->readchunkpreec)
9040 break;
9041
9042 /* Note: Technically, saturation should be done at
9043 the end of the whole addition, but for performance
9044 reasons, we don't do that. Besides, it only matters
9045 when you're so loud you're clipping anyway */
9046 ACSS(putlin, conf_chan->getlin);
9047 ACSS(putlin, conf_chan->readchunkpreec);
9048 for (x = 0; x < DAHDI_CHUNKSIZE; x++)
9049 rxb[x] = DAHDI_LIN2X(putlin[x], ms);
9050
9051 break;
9052 case DAHDI_CONF_REALANDPSEUDO:
9053 /* do normal conf mode processing */
9054 if (ms->confmode & DAHDI_CONF_TALKER) {
9055 /* Store temp value */
9056 memcpy(k, putlin, DAHDI_CHUNKSIZE * sizeof(short));
9057 /* Add conf value */
9058 ACSS(k, conf_sums_next[ms->_confn]);
9059 /* get amount actually added */
9060 memcpy(ms->conflast, k, DAHDI_CHUNKSIZE * sizeof(short));
9061 SCSS(ms->conflast, conf_sums_next[ms->_confn]);
9062 /* Really add in new value */
9063 ACSS(conf_sums_next[ms->_confn], ms->conflast);
9064 } else memset(ms->conflast, 0, DAHDI_CHUNKSIZE * sizeof(short));
9065 /* do the pseudo-channel part processing */
9066 memset(putlin, 0, DAHDI_CHUNKSIZE * sizeof(short));
9067 if (ms->confmode & DAHDI_CONF_PSEUDO_LISTENER) {
9068 /* Subtract out previous last sample written to conf */
9069 SCSS(putlin, ms->conflast2);
9070 /* Add in conference */
9071 ACSS(putlin, conf_sums[ms->_confn]);
9072 }
9073 /* Convert back */
9074 for(x=0;x<DAHDI_CHUNKSIZE;x++)
9075 rxb[x] = DAHDI_LIN2X(putlin[x], ms);
9076 break;
9077 case DAHDI_CONF_CONF: /* Normal conference mode */
9078 if (is_pseudo_chan(ms)) /* if a pseudo-channel */
9079 {
9080 if (ms->confmode & DAHDI_CONF_LISTENER) {
9081 /* Subtract out last sample written to conf */
9082 SCSS(putlin, ms->conflast);
9083 /* Add in conference */
9084 ACSS(putlin, conf_sums[ms->_confn]);
9085 }
9086 /* Convert back */
9087 for(x=0;x<DAHDI_CHUNKSIZE;x++)
9088 rxb[x] = DAHDI_LIN2X(putlin[x], ms);
9089 memcpy(ss->putlin, putlin, DAHDI_CHUNKSIZE * sizeof(short));
9090 break;
9091 }
9092 /* fall through */
9093 case DAHDI_CONF_CONFANN: /* Conference with announce */
9094 if (ms->confmode & DAHDI_CONF_TALKER) {
9095 /* Store temp value */
9096 memcpy(k, putlin, DAHDI_CHUNKSIZE * sizeof(short));
9097 /* Add conf value */
9098 ACSS(k, conf_sums_next[ms->_confn]);
9099 /* get amount actually added */
9100 memcpy(ms->conflast, k, DAHDI_CHUNKSIZE * sizeof(short));
9101 SCSS(ms->conflast, conf_sums_next[ms->_confn]);
9102 /* Really add in new value */
9103 ACSS(conf_sums_next[ms->_confn], ms->conflast);
9104 } else
9105 memset(ms->conflast, 0, DAHDI_CHUNKSIZE * sizeof(short));
9106 /* rxc unmodified */
9107 break;
9108 case DAHDI_CONF_CONFMON:
9109 case DAHDI_CONF_CONFANNMON:
9110 if (ms->confmode & DAHDI_CONF_TALKER) {
9111 /* Store temp value */
9112 memcpy(k, putlin, DAHDI_CHUNKSIZE * sizeof(short));
9113 /* Subtract last value */
9114 SCSS(conf_sums[ms->_confn], ms->conflast);
9115 /* Add conf value */
9116 ACSS(k, conf_sums[ms->_confn]);
9117 /* get amount actually added */
9118 memcpy(ms->conflast, k, DAHDI_CHUNKSIZE * sizeof(short));
9119 SCSS(ms->conflast, conf_sums[ms->_confn]);
9120 /* Really add in new value */
9121 ACSS(conf_sums[ms->_confn], ms->conflast);
9122 } else
9123 memset(ms->conflast, 0, DAHDI_CHUNKSIZE * sizeof(short));
9124 for (x=0;x<DAHDI_CHUNKSIZE;x++)
9125 rxb[x] = DAHDI_LIN2X((int)conf_sums_prev[ms->_confn][x], ms);
9126 break;
9127 case DAHDI_CONF_DIGITALMON:
9128 /* if not a pseudo-channel, ignore */
9129 if (!is_pseudo_chan(ms))
9130 break;
9131 /* Add monitored channel */
9132 if (is_pseudo_chan(conf_chan))
9133 memcpy(rxb, conf_chan->getraw, DAHDI_CHUNKSIZE);
9134 else
9135 memcpy(rxb, conf_chan->putraw, DAHDI_CHUNKSIZE);
9136 break;
9137 }
9138 }
9139 }
9140
9141 /* HDLC (or other) receiver buffer functions for read side */
9142 static void __putbuf_chunk(struct dahdi_chan *ss, unsigned char *rxb, int bytes)
9143 {
9144 /* We transmit data from our master channel */
9145 /* Called with ss->lock held */
9146 struct dahdi_chan *ms = ss->master;
9147 /* Our receive buffer */
9148 unsigned char *buf;
9149 #if defined(CONFIG_DAHDI_NET) || defined(CONFIG_DAHDI_PPP)
9150 /* SKB for receiving network stuff */
9151 struct sk_buff *skb=NULL;
9152 #endif
9153 int oldbuf;
9154 int eof=0;
9155 int abort=0;
9156 int res;
9157 int left, x;
9158
9159 while(bytes) {
9160 #if defined(CONFIG_DAHDI_NET) || defined(CONFIG_DAHDI_PPP)
9161 skb = NULL;
9162 #endif
9163 abort = 0;
9164 eof = 0;
9165 /* Next, figure out if we've got a buffer to receive into */
9166 if (ms->inreadbuf > -1) {
9167 /* Read into the current buffer */
9168 buf = ms->readbuf[ms->inreadbuf];
9169 left = ms->blocksize - ms->readidx[ms->inreadbuf];
9170 if (left > bytes)
9171 left = bytes;
9172 if (ms->flags & DAHDI_FLAG_HDLC) {
9173 for (x=0;x<left;x++) {
9174 /* Handle HDLC deframing */
9175 fasthdlc_rx_load_nocheck(&ms->rxhdlc, *(rxb++));
9176 bytes--;
9177 res = fasthdlc_rx_run(&ms->rxhdlc);
9178 /* If there is nothing there, continue */
9179 if (res & RETURN_EMPTY_FLAG)
9180 continue;
9181 else if (res & RETURN_COMPLETE_FLAG) {
9182 /* Only count this if it's a non-empty frame */
9183 if (ms->readidx[ms->inreadbuf]) {
9184 if ((ms->flags & DAHDI_FLAG_FCS) && (ms->infcs != PPP_GOODFCS)) {
9185 abort = DAHDI_EVENT_BADFCS;
9186 } else
9187 eof=1;
9188 break;
9189 }
9190 continue;
9191 } else if (res & RETURN_DISCARD_FLAG) {
9192 /* This could be someone idling with
9193 "idle" instead of "flag" */
9194 if (!ms->readidx[ms->inreadbuf])
9195 continue;
9196 abort = DAHDI_EVENT_ABORT;
9197 break;
9198 } else {
9199 unsigned char rxc;
9200 rxc = res;
9201 ms->infcs = PPP_FCS(ms->infcs, rxc);
9202 buf[ms->readidx[ms->inreadbuf]++] = rxc;
9203 /* Pay attention to the possibility of an overrun */
9204 if (ms->readidx[ms->inreadbuf] >= ms->blocksize) {
9205 if (!ss->span->alarms)
9206 module_printk(KERN_WARNING, "HDLC Receiver overrun on channel %s (master=%s)\n", ss->name, ss->master->name);
9207 abort=DAHDI_EVENT_OVERRUN;
9208 /* Force the HDLC state back to frame-search mode */
9209 ms->rxhdlc.state = 0;
9210 ms->rxhdlc.bits = 0;
9211 ms->readidx[ms->inreadbuf]=0;
9212 break;
9213 }
9214 }
9215 }
9216 } else {
9217 /* Not HDLC */
9218 memcpy(buf + ms->readidx[ms->inreadbuf], rxb, left);
9219 rxb += left;
9220 ms->readidx[ms->inreadbuf] += left;
9221 bytes -= left;
9222 /* End of frame is decided by block size of 'N' */
9223 eof = (ms->readidx[ms->inreadbuf] >= ms->blocksize);
9224 if (eof && (ss->flags & DAHDI_FLAG_NOSTDTXRX)) {
9225 eof = 0;
9226 abort = DAHDI_EVENT_OVERRUN;
9227 }
9228 }
9229 if (eof) {
9230 /* Finished with this buffer, try another. */
9231 oldbuf = ms->inreadbuf;
9232 ms->infcs = PPP_INITFCS;
9233 ms->readn[ms->inreadbuf] = ms->readidx[ms->inreadbuf];
9234 #ifdef CONFIG_DAHDI_DEBUG
9235 module_printk(KERN_NOTICE, "EOF, len is %d\n", ms->readn[ms->inreadbuf]);
9236 #endif
9237 #if defined(CONFIG_DAHDI_NET) || defined(CONFIG_DAHDI_PPP)
9238 if ((ms->flags & DAHDI_FLAG_PPP) ||
9239 dahdi_have_netdev(ms)) {
9240 #ifdef CONFIG_DAHDI_NET
9241 #endif /* CONFIG_DAHDI_NET */
9242 /* Our network receiver logic is MUCH
9243 different. We actually only use a single
9244 buffer */
9245 if (ms->readn[ms->inreadbuf] > 1) {
9246 /* Drop the FCS */
9247 ms->readn[ms->inreadbuf] -= 2;
9248 /* Allocate an SKB */
9249 #ifdef CONFIG_DAHDI_PPP
9250 if (!ms->do_ppp_error)
9251 #endif
9252 skb = dev_alloc_skb(ms->readn[ms->inreadbuf] + 2);
9253 if (skb) {
9254 unsigned char cisco_addr = *(ms->readbuf[ms->inreadbuf]);
9255 if (cisco_addr != 0x0f && cisco_addr != 0x8f)
9256 skb_reserve(skb, 2);
9257 /* XXX Get rid of this memcpy XXX */
9258 memcpy(skb->data, ms->readbuf[ms->inreadbuf], ms->readn[ms->inreadbuf]);
9259 skb_put(skb, ms->readn[ms->inreadbuf]);
9260 #ifdef CONFIG_DAHDI_NET
9261 if (dahdi_have_netdev(ms)) {
9262 struct net_device_stats *stats = hdlc_stats(ms->hdlcnetdev->netdev);
9263 stats->rx_packets++;
9264 stats->rx_bytes += ms->readn[ms->inreadbuf];
9265 }
9266 #endif
9267
9268 } else {
9269 #ifdef CONFIG_DAHDI_NET
9270 if (dahdi_have_netdev(ms)) {
9271 struct net_device_stats *stats = hdlc_stats(ms->hdlcnetdev->netdev);
9272 stats->rx_dropped++;
9273 }
9274 #endif
9275 #ifdef CONFIG_DAHDI_PPP
9276 if (ms->flags & DAHDI_FLAG_PPP) {
9277 abort = DAHDI_EVENT_OVERRUN;
9278 }
9279 #endif
9280 #if 1
9281 #ifdef CONFIG_DAHDI_PPP
9282 if (!ms->do_ppp_error)
9283 #endif
9284 module_printk(KERN_NOTICE, "Memory squeeze, dropped one\n");
9285 #endif
9286 }
9287 }
9288 /* We don't cycle through buffers, just
9289 reuse the same one */
9290 ms->readn[ms->inreadbuf] = 0;
9291 ms->readidx[ms->inreadbuf] = 0;
9292 } else
9293 #endif
9294 {
9295 /* This logic might confuse and astound. Basically we need to find
9296 * the previous buffer index. It should be safe because, regardless
9297 * of whether or not it has been copied to user space, nothing should
9298 * have messed around with it since then */
9299
9300 int comparemessage;
9301 /* Shut compiler up */
9302 int myres = 0;
9303
9304 if (ms->flags & DAHDI_FLAG_MTP2) {
9305 comparemessage = (ms->inreadbuf - 1) & (ms->numbufs - 1);
9306
9307 myres = memcmp(ms->readbuf[comparemessage], ms->readbuf[ms->inreadbuf], ms->readn[ms->inreadbuf]);
9308 }
9309
9310 if ((ms->flags & DAHDI_FLAG_MTP2) && !myres) {
9311 /* Our messages are the same, so discard -
9312 * Don't advance buffers, reset indexes and buffer sizes. */
9313 ms->readn[ms->inreadbuf] = 0;
9314 ms->readidx[ms->inreadbuf] = 0;
9315 } else {
9316 ms->inreadbuf = (ms->inreadbuf + 1) % ms->numbufs;
9317 if (ms->inreadbuf == ms->outreadbuf) {
9318 /* Whoops, we're full, and have no where else
9319 to store into at the moment. We'll drop it
9320 until there's a buffer available */
9321 #ifdef BUFFER_DEBUG
9322 module_printk(KERN_NOTICE, "Out of storage space\n");
9323 #endif
9324 ms->inreadbuf = -1;
9325 }
9326 if (ms->outreadbuf < 0) { /* start out buffer if not already */
9327 ms->outreadbuf = oldbuf;
9328 /* if there are processes waiting in poll() on this channel,
9329 wake them up */
9330 wake_up_interruptible(&ms->waitq);
9331 }
9332 /* In the very orignal driver, it was quite well known to me (Jim) that there
9333 was a possibility that a channel sleeping on a receive block needed to
9334 be potentially woken up EVERY time a buffer was filled, not just on the first
9335 one, because if only done on the first one there is a slight timing potential
9336 of missing the wakeup (between where it senses the (lack of) active condition
9337 (with interrupts disabled) and where it does the sleep (interrupts enabled)
9338 in the read or iomux call, etc). That is why the read and iomux calls start
9339 with an infinite loop that gets broken out of upon an active condition,
9340 otherwise keeps sleeping and looking. The part in this code got "optimized"
9341 out in the later versions, and is put back now. Note that this is *NOT*
9342 needed for poll() waiters, because the poll_wait() function that is used there
9343 is atomic enough for this purpose; it will not go to sleep before ensuring
9344 that the waitqueue is empty. */
9345 /* Notify a blocked reader that there is data available
9346 to be read, unless we're waiting for it to be full */
9347 #ifdef CONFIG_DAHDI_DEBUG
9348 module_printk(KERN_NOTICE, "Notifying reader data in block %d\n", oldbuf);
9349 #endif
9350 wake_up_interruptible(&ms->waitq);
9351 }
9352 }
9353 }
9354 if (abort) {
9355 /* Start over reading frame */
9356 ms->readidx[ms->inreadbuf] = 0;
9357 ms->infcs = PPP_INITFCS;
9358
9359 #ifdef CONFIG_DAHDI_NET
9360 if (dahdi_have_netdev(ms)) {
9361 struct net_device_stats *stats = hdlc_stats(ms->hdlcnetdev->netdev);
9362 stats->rx_errors++;
9363 if (abort == DAHDI_EVENT_OVERRUN)
9364 stats->rx_over_errors++;
9365 if (abort == DAHDI_EVENT_BADFCS)
9366 stats->rx_crc_errors++;
9367 if (abort == DAHDI_EVENT_ABORT)
9368 stats->rx_frame_errors++;
9369 } else
9370 #endif
9371 #ifdef CONFIG_DAHDI_PPP
9372 if (ms->flags & DAHDI_FLAG_PPP) {
9373 ms->do_ppp_error = 1;
9374 tasklet_schedule(&ms->ppp_calls);
9375 } else
9376 #endif
9377 if (test_bit(DAHDI_FLAGBIT_OPEN, &ms->flags) && !ss->span->alarms) {
9378 /* Notify the receiver... */
9379 __qevent(ss->master, abort);
9380 }
9381 }
9382 } else /* No place to receive -- drop on the floor */
9383 break;
9384 #ifdef CONFIG_DAHDI_NET
9385 if (skb && dahdi_have_netdev(ms))
9386 {
9387 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
9388 skb->mac.raw = skb->data;
9389 #else
9390 skb_reset_mac_header(skb);
9391 #endif
9392 skb->dev = chan_to_netdev(ms);
9393 #ifdef DAHDI_HDLC_TYPE_TRANS
9394 skb->protocol = hdlc_type_trans(skb,
9395 chan_to_netdev(ms));
9396 #else
9397 skb->protocol = htons (ETH_P_HDLC);
9398 #endif
9399 netif_rx(skb);
9400 }
9401 #endif
9402 #ifdef CONFIG_DAHDI_PPP
9403 if (skb && (ms->flags & DAHDI_FLAG_PPP)) {
9404 unsigned char *tmp;
9405 tmp = skb->data;
9406 skb_pull(skb, 2);
9407 /* Make sure that it's addressed to ALL STATIONS and UNNUMBERED */
9408 if (!tmp || (tmp[0] != 0xff) || (tmp[1] != 0x03)) {
9409 /* Invalid SKB -- drop */
9410 if (tmp)
9411 module_printk(KERN_NOTICE, "Received invalid SKB (%02x, %02x)\n", tmp[0], tmp[1]);
9412 dev_kfree_skb_irq(skb);
9413 } else {
9414 skb_queue_tail(&ms->ppp_rq, skb);
9415 tasklet_schedule(&ms->ppp_calls);
9416 }
9417 }
9418 #endif
9419 }
9420
9421 if (bytes) {
9422 if (!test_bit(DAHDI_FLAGBIT_RXOVERRUN, &ms->flags)) {
9423 if (test_bit(DAHDI_FLAGBIT_BUFEVENTS, &ms->flags))
9424 __qevent(ms, DAHDI_EVENT_READ_OVERRUN);
9425 set_bit(DAHDI_FLAGBIT_RXOVERRUN, &ms->flags);
9426 }
9427 } else {
9428 clear_bit(DAHDI_FLAGBIT_RXOVERRUN, &ms->flags);
9429 }
9430 }
9431
9432 static inline void __dahdi_putbuf_chunk(struct dahdi_chan *ss, unsigned char *rxb)
9433 {
9434 __putbuf_chunk(ss, rxb, DAHDI_CHUNKSIZE);
9435
9436 #ifdef CONFIG_DAHDI_MIRROR
9437 if (ss->rxmirror) {
9438 spin_lock(&ss->rxmirror->lock);
9439 __putbuf_chunk(ss->rxmirror, rxb, DAHDI_CHUNKSIZE);
9440 spin_unlock(&ss->rxmirror->lock);
9441 }
9442 #endif /* CONFIG_DAHDI_MIRROR */
9443 }
9444
9445 static void __dahdi_hdlc_abort(struct dahdi_chan *ss, int event)
9446 {
9447 if (ss->inreadbuf >= 0)
9448 ss->readidx[ss->inreadbuf] = 0;
9449 if (test_bit(DAHDI_FLAGBIT_OPEN, &ss->flags) && !ss->span->alarms)
9450 __qevent(ss->master, event);
9451 }
9452
9453 void dahdi_hdlc_abort(struct dahdi_chan *ss, int event)
9454 {
9455 unsigned long flags;
9456 spin_lock_irqsave(&ss->lock, flags);
9457 __dahdi_hdlc_abort(ss, event);
9458 spin_unlock_irqrestore(&ss->lock, flags);
9459 }
9460
9461 void dahdi_hdlc_putbuf(struct dahdi_chan *ss, unsigned char *rxb, int bytes)
9462 {
9463 unsigned long flags;
9464 int left;
9465
9466 spin_lock_irqsave(&ss->lock, flags);
9467 if (ss->inreadbuf < 0) {
9468 #ifdef CONFIG_DAHDI_DEBUG
9469 module_printk(KERN_NOTICE, "No place to receive HDLC frame\n");
9470 #endif
9471 spin_unlock_irqrestore(&ss->lock, flags);
9472 return;
9473 }
9474 /* Read into the current buffer */
9475 left = ss->blocksize - ss->readidx[ss->inreadbuf];
9476 if (left > bytes)
9477 left = bytes;
9478 if (left > 0) {
9479 memcpy(ss->readbuf[ss->inreadbuf] + ss->readidx[ss->inreadbuf], rxb, left);
9480 rxb += left;
9481 ss->readidx[ss->inreadbuf] += left;
9482 bytes -= left;
9483 }
9484 /* Something isn't fit into buffer */
9485 if (bytes) {
9486 #ifdef CONFIG_DAHDI_DEBUG
9487 module_printk(KERN_NOTICE, "HDLC frame isn't fit into buffer space\n");
9488 #endif
9489 __dahdi_hdlc_abort(ss, DAHDI_EVENT_OVERRUN);
9490 }
9491 spin_unlock_irqrestore(&ss->lock, flags);
9492 }
9493
9494 void dahdi_hdlc_finish(struct dahdi_chan *ss)
9495 {
9496 int oldreadbuf;
9497 unsigned long flags;
9498
9499 spin_lock_irqsave(&ss->lock, flags);
9500
9501 if ((oldreadbuf = ss->inreadbuf) < 0) {
9502 #ifdef CONFIG_DAHDI_DEBUG
9503 module_printk(KERN_NOTICE, "No buffers to finish\n");
9504 #endif
9505 spin_unlock_irqrestore(&ss->lock, flags);
9506 return;
9507 }
9508
9509 if (!ss->readidx[ss->inreadbuf]) {
9510 #ifdef CONFIG_DAHDI_DEBUG
9511 module_printk(KERN_NOTICE, "Empty HDLC frame received\n");
9512 #endif
9513 spin_unlock_irqrestore(&ss->lock, flags);
9514 return;
9515 }
9516
9517 ss->readn[ss->inreadbuf] = ss->readidx[ss->inreadbuf];
9518 ss->inreadbuf = (ss->inreadbuf + 1) % ss->numbufs;
9519 if (ss->inreadbuf == ss->outreadbuf) {
9520 ss->inreadbuf = -1;
9521 #ifdef CONFIG_DAHDI_DEBUG
9522 module_printk(KERN_NOTICE, "Notifying reader data in block %d\n", oldreadbuf);
9523 #endif
9524 }
9525 if (ss->outreadbuf < 0) {
9526 ss->outreadbuf = oldreadbuf;
9527 }
9528
9529 wake_up_interruptible(&ss->waitq);
9530 spin_unlock_irqrestore(&ss->lock, flags);
9531 }
9532
9533 /* Returns 1 if EOF, 0 if data is still in frame, -1 if EOF and no buffers left */
9534 int dahdi_hdlc_getbuf(struct dahdi_chan *ss, unsigned char *bufptr, unsigned int *size)
9535 {
9536 unsigned char *buf;
9537 unsigned long flags;
9538 int left = 0;
9539 int res;
9540 int oldbuf;
9541
9542 spin_lock_irqsave(&ss->lock, flags);
9543 if (ss->outwritebuf > -1) {
9544 buf = ss->writebuf[ss->outwritebuf];
9545 left = ss->writen[ss->outwritebuf] - ss->writeidx[ss->outwritebuf];
9546 /* Strip off the empty HDLC CRC end */
9547 left -= 2;
9548 if (left <= *size) {
9549 *size = left;
9550 res = 1;
9551 } else
9552 res = 0;
9553
9554 memcpy(bufptr, &buf[ss->writeidx[ss->outwritebuf]], *size);
9555 ss->writeidx[ss->outwritebuf] += *size;
9556
9557 if (res) {
9558 /* Rotate buffers */
9559 oldbuf = ss->outwritebuf;
9560 ss->writeidx[oldbuf] = 0;
9561 ss->writen[oldbuf] = 0;
9562 ss->outwritebuf = (ss->outwritebuf + 1) % ss->numbufs;
9563 if (ss->outwritebuf == ss->inwritebuf) {
9564 ss->outwritebuf = -1;
9565 if (ss->iomask & (DAHDI_IOMUX_WRITE | DAHDI_IOMUX_WRITEEMPTY))
9566 wake_up_interruptible(&ss->waitq);
9567 /* If we're only supposed to start when full, disable the transmitter */
9568 if ((ss->txbufpolicy == DAHDI_POLICY_WHEN_FULL) || (ss->txbufpolicy == DAHDI_POLICY_HALF_FULL))
9569 ss->txdisable = 1;
9570 res = -1;
9571 }
9572
9573 if (ss->inwritebuf < 0)
9574 ss->inwritebuf = oldbuf;
9575
9576 if (!(ss->flags & DAHDI_FLAG_PPP) ||
9577 !dahdi_have_netdev(ss)) {
9578 wake_up_interruptible(&ss->waitq);
9579 }
9580 }
9581 } else {
9582 res = -1;
9583 *size = 0;
9584 }
9585 spin_unlock_irqrestore(&ss->lock, flags);
9586
9587 return res;
9588 }
9589
9590
9591 static void process_timers(void)
9592 {
9593 struct dahdi_timer *cur;
9594
9595 if (list_empty(&dahdi_timers))
9596 return;
9597
9598 spin_lock(&dahdi_timer_lock);
9599 list_for_each_entry(cur, &dahdi_timers, list) {
9600 spin_lock(&cur->lock);
9601 cur->pos -= DAHDI_CHUNKSIZE;
9602 if (cur->pos <= 0) {
9603 cur->tripped++;
9604 cur->pos = cur->ms;
9605 wake_up_interruptible(&cur->sel);
9606 }
9607 spin_unlock(&cur->lock);
9608 }
9609 spin_unlock(&dahdi_timer_lock);
9610 }
9611
9612 static unsigned int dahdi_timer_poll(struct file *file, struct poll_table_struct *wait_table)
9613 {
9614 struct dahdi_timer *timer = file->private_data;
9615 unsigned long flags;
9616 int ret = 0;
9617 if (timer) {
9618 poll_wait(file, &timer->sel, wait_table);
9619 spin_lock_irqsave(&timer->lock, flags);
9620 if (timer->tripped || timer->ping)
9621 ret |= POLLPRI;
9622 spin_unlock_irqrestore(&timer->lock, flags);
9623 } else {
9624 /*
9625 * This should never happen. Surprise device removal
9626 * should lead us to the nodev_* file_operations
9627 */
9628 msleep(5);
9629 module_printk(KERN_ERR, "%s: NODEV\n", __func__);
9630 return POLLERR | POLLHUP | POLLRDHUP | POLLNVAL | POLLPRI;
9631 }
9632 return ret;
9633 }
9634
9635 /* device poll routine */
9636 static unsigned int
9637 dahdi_chan_poll(struct file *file, struct poll_table_struct *wait_table)
9638 {
9639 struct dahdi_chan *const c = file->private_data;
9640 int ret = 0;
9641 unsigned long flags;
9642
9643 if (unlikely(!c)) {
9644 /*
9645 * This should never happen. Surprise device removal
9646 * should lead us to the nodev_* file_operations
9647 */
9648 msleep(5);
9649 module_printk(KERN_ERR, "%s: NODEV\n", __func__);
9650 return POLLERR | POLLHUP | POLLRDHUP | POLLNVAL | POLLPRI;
9651 }
9652
9653 poll_wait(file, &c->waitq, wait_table);
9654
9655 spin_lock_irqsave(&c->lock, flags);
9656 ret |= (c->inwritebuf > -1) ? POLLOUT|POLLWRNORM : 0;
9657 ret |= (c->outreadbuf > -1) ? POLLIN|POLLRDNORM : 0;
9658 ret |= (c->eventoutidx != c->eventinidx) ? POLLPRI : 0;
9659 spin_unlock_irqrestore(&c->lock, flags);
9660
9661 return ret;
9662 }
9663
9664 static unsigned int dahdi_poll(struct file *file, struct poll_table_struct *wait_table)
9665 {
9666 const int unit = UNIT(file);
9667
9668 if (likely(unit == DAHDI_TIMER))
9669 return dahdi_timer_poll(file, wait_table);
9670
9671 /* transcoders and channels should have updated their file_operations
9672 * before poll is ever called. */
9673 return -EINVAL;
9674 }
9675
9676 static void __dahdi_transmit_chunk(struct dahdi_chan *chan, unsigned char *buf)
9677 {
9678 unsigned char silly[DAHDI_CHUNKSIZE];
9679 /* Called with chan->lock locked */
9680 #ifdef OPTIMIZE_CHANMUTE
9681 if(likely(chan->chanmute))
9682 return;
9683 #endif
9684 if (!buf)
9685 buf = silly;
9686 __dahdi_getbuf_chunk(chan, buf);
9687
9688 if ((chan->flags & DAHDI_FLAG_AUDIO) || (chan->confmode)) {
9689 #ifdef CONFIG_DAHDI_MMX
9690 dahdi_kernel_fpu_begin();
9691 #endif
9692 __dahdi_process_getaudio_chunk(chan, buf);
9693 #ifdef CONFIG_DAHDI_MMX
9694 dahdi_kernel_fpu_end();
9695 #endif
9696 }
9697 }
9698
9699 static inline void __dahdi_real_transmit(struct dahdi_chan *chan)
9700 {
9701 /* Called with chan->lock held */
9702 #ifdef OPTIMIZE_CHANMUTE
9703 if(likely(chan->chanmute))
9704 return;
9705 #endif
9706 if (chan->confmode) {
9707 /* Pull queued data off the conference */
9708 __buf_pull(&chan->confout, chan->writechunk, chan);
9709 } else {
9710 __dahdi_transmit_chunk(chan, chan->writechunk);
9711 }
9712 }
9713
9714 static void __dahdi_getempty(struct dahdi_chan *ms, unsigned char *buf)
9715 {
9716 int bytes = DAHDI_CHUNKSIZE;
9717 int left;
9718 unsigned char *txb = buf;
9719 int x;
9720 short getlin;
9721 /* Called with ms->lock held */
9722
9723 while(bytes) {
9724 /* Receive silence, or tone */
9725 if (ms->curtone) {
9726 left = ms->curtone->tonesamples - ms->tonep;
9727 if (left > bytes)
9728 left = bytes;
9729 for (x=0;x<left;x++) {
9730 /* Pick our default value from the next sample of the current tone */
9731 getlin = dahdi_tone_nextsample(&ms->ts, ms->curtone);
9732 *(txb++) = DAHDI_LIN2X(getlin, ms);
9733 }
9734 ms->tonep+=left;
9735 bytes -= left;
9736 if (ms->tonep >= ms->curtone->tonesamples) {
9737 struct dahdi_tone *last;
9738 /* Go to the next sample of the tone */
9739 ms->tonep = 0;
9740 last = ms->curtone;
9741 ms->curtone = ms->curtone->next;
9742 if (!ms->curtone) {
9743 /* No more tones... Is this dtmf or mf? If so, go to the next digit */
9744 if (ms->dialing)
9745 __do_dtmf(ms);
9746 } else {
9747 if (last != ms->curtone)
9748 dahdi_init_tone_state(&ms->ts, ms->curtone);
9749 }
9750 }
9751 } else {
9752 /* Use silence */
9753 memset(txb, DAHDI_LIN2X(0, ms), bytes);
9754 bytes = 0;
9755 }
9756 }
9757
9758 }
9759
9760 static void __dahdi_receive_chunk(struct dahdi_chan *chan, unsigned char *buf)
9761 {
9762 /* Receive chunk of audio -- called with chan->lock held */
9763 unsigned char waste[DAHDI_CHUNKSIZE];
9764
9765 #ifdef OPTIMIZE_CHANMUTE
9766 if(likely(chan->chanmute))
9767 return;
9768 #endif
9769 if (!buf) {
9770 memset(waste, DAHDI_LIN2X(0, chan), sizeof(waste));
9771 buf = waste;
9772 }
9773 if ((chan->flags & DAHDI_FLAG_AUDIO) || (chan->confmode)) {
9774 #ifdef CONFIG_DAHDI_MMX
9775 dahdi_kernel_fpu_begin();
9776 #endif
9777 __dahdi_process_putaudio_chunk(chan, buf);
9778 #ifdef CONFIG_DAHDI_MMX
9779 dahdi_kernel_fpu_end();
9780 #endif
9781 }
9782 __dahdi_putbuf_chunk(chan, buf);
9783 }
9784
9785 static inline void __dahdi_real_receive(struct dahdi_chan *chan)
9786 {
9787 /* Called with chan->lock held */
9788 #ifdef OPTIMIZE_CHANMUTE
9789 if(likely(chan->chanmute))
9790 return;
9791 #endif
9792 if (chan->confmode) {
9793 /* Load into queue if we have space */
9794 __buf_push(&chan->confin, chan->readchunk);
9795 } else {
9796 __dahdi_receive_chunk(chan, chan->readchunk);
9797 }
9798 }
9799
9800 /**
9801 * __transmit_to_slaves() - Distribute the tx data to all the slave channels.
9802 *
9803 */
9804 static void __transmit_to_slaves(struct dahdi_chan *const chan)
9805 {
9806 u_char data[DAHDI_CHUNKSIZE];
9807 int i;
9808 int pos = DAHDI_CHUNKSIZE;
9809 struct dahdi_chan *slave;
9810 for (i = 0; i < DAHDI_CHUNKSIZE; i++) {
9811 for (slave = chan; (NULL != slave); slave = slave->nextslave) {
9812 if (pos == DAHDI_CHUNKSIZE) {
9813 __dahdi_transmit_chunk(chan, data);
9814 pos = 0;
9815 }
9816 slave->writechunk[i] = data[pos++];
9817 }
9818 }
9819 }
9820
9821 int _dahdi_transmit(struct dahdi_span *span)
9822 {
9823 unsigned int x;
9824
9825 for (x=0;x<span->channels;x++) {
9826 struct dahdi_chan *const chan = span->chans[x];
9827 spin_lock(&chan->lock);
9828 if (unlikely(chan->flags & DAHDI_FLAG_NOSTDTXRX)) {
9829 spin_unlock(&chan->lock);
9830 continue;
9831 }
9832 if (chan == chan->master) {
9833 if (is_chan_dacsed(chan)) {
9834 struct dahdi_chan *const src = chan->dacs_chan;
9835 memcpy(chan->writechunk, src->readchunk,
9836 DAHDI_CHUNKSIZE);
9837 if (chan->sig == DAHDI_SIG_DACS_RBS) {
9838 /* Just set bits for our destination */
9839 if (chan->txsig != src->rxsig) {
9840 chan->txsig = src->rxsig;
9841 span->ops->rbsbits(chan, src->rxsig);
9842 }
9843 }
9844 /* there is no further processing to do for
9845 * DACS channels, so jump to the next channel
9846 * in the span */
9847 spin_unlock(&chan->lock);
9848 continue;
9849 } else if (chan->nextslave) {
9850 __transmit_to_slaves(chan);
9851 } else {
9852 /* Process a normal channel */
9853 __dahdi_real_transmit(chan);
9854 }
9855 if (chan->otimer) {
9856 chan->otimer -= DAHDI_CHUNKSIZE;
9857 if (chan->otimer <= 0)
9858 __rbs_otimer_expire(chan);
9859 }
9860 }
9861 spin_unlock(&chan->lock);
9862 }
9863
9864 if (span->mainttimer) {
9865 span->mainttimer -= DAHDI_CHUNKSIZE;
9866 if (span->mainttimer <= 0) {
9867 span->mainttimer = 0;
9868 span->maintstat = 0;
9869 }
9870 }
9871 return 0;
9872 }
9873 EXPORT_SYMBOL(_dahdi_transmit);
9874
9875 static inline void __pseudo_rx_audio(struct dahdi_chan *chan)
9876 {
9877 unsigned char tmp[DAHDI_CHUNKSIZE];
9878 spin_lock(&chan->lock);
9879 __dahdi_getempty(chan, tmp);
9880 __dahdi_receive_chunk(chan, tmp);
9881 spin_unlock(&chan->lock);
9882 }
9883
9884 #ifdef CONFIG_DAHDI_MIRROR
9885 static inline void pseudo_rx_audio(struct dahdi_chan *chan)
9886 {
9887 if (!chan->srcmirror)
9888 __pseudo_rx_audio(chan);
9889 }
9890 #else
9891 static inline void pseudo_rx_audio(struct dahdi_chan *chan)
9892 {
9893 __pseudo_rx_audio(chan);
9894 }
9895 #endif /* CONFIG_DAHDI_MIRROR */
9896
9897 #ifdef DAHDI_SYNC_TICK
9898 static inline void dahdi_sync_tick(struct dahdi_span *const s)
9899 {
9900 if (s->ops->sync_tick)
9901 s->ops->sync_tick(s, dahdi_is_sync_master(s));
9902 }
9903 #else
9904 #define dahdi_sync_tick(x) do { ; } while (0)
9905 #endif
9906
9907 /**
9908 * _process_masterspan - Handle conferencing and timers.
9909 *
9910 * There are three sets of conference sum accumulators. One for the current
9911 * sample chunk (conf_sums), one for the next sample chunk (conf_sums_next), and
9912 * one for the previous sample chunk (conf_sums_prev). The following routine
9913 * (rotate_sums) "rotates" the pointers to these accululator arrays as part
9914 * of the events of sample chink processing as follows:
9915 *
9916 * 1. All (real span) receive chunks are processed (with putbuf). The last one
9917 * to be processed is the master span. The data received is loaded into the
9918 * accumulators for the next chunk (conf_sums_next), to be in alignment with
9919 * current data after rotate_sums() is called (which immediately follows).
9920 * Keep in mind that putbuf is *also* a transmit routine for the pseudo parts
9921 * of channels that are in the REALANDPSEUDO conference mode. These channels
9922 * are processed from data in the current sample chunk (conf_sums), being
9923 * that this is a "transmit" function (for the pseudo part).
9924 *
9925 * 2. rotate_sums() is called.
9926 *
9927 * 3. All pseudo channel receive chunks are processed. This data is loaded into
9928 * the current sample chunk accumulators (conf_sums).
9929 *
9930 * 4. All conference links are processed (being that all receive data for this
9931 * chunk has already been processed by now).
9932 *
9933 * 5. All pseudo channel transmit chunks are processed. This data is loaded from
9934 * the current sample chunk accumulators (conf_sums).
9935 *
9936 * 6. All (real span) transmit chunks are processed (with getbuf). This data is
9937 * loaded from the current sample chunk accumulators (conf_sums). Keep in mind
9938 * that getbuf is *also* a receive routine for the pseudo part of channels that
9939 * are in the REALANDPSEUDO conference mode. These samples are loaded into
9940 * the next sample chunk accumulators (conf_sums_next) to be processed as part
9941 * of the next sample chunk's data (next time around the world).
9942 *
9943 */
9944 static void _process_masterspan(void)
9945 {
9946 int x;
9947 struct pseudo_chan *pseudo;
9948 struct dahdi_span *s;
9949 u_char *data;
9950
9951 #ifdef CONFIG_DAHDI_CORE_TIMER
9952 /* We increment the calls since start here, so that if we switch over
9953 * to the core timer, we know how many times we need to call
9954 * process_masterspan in order to catch up since this function needs
9955 * to be called (1000 / (DAHDI_CHUNKSIZE / 8)) times per second. */
9956 atomic_inc(&core_timer.count);
9957 #endif
9958 /* Hold the chan_lock for the duration of major
9959 activities which touch all sorts of channels */
9960 spin_lock(&chan_lock);
9961
9962 /* Process any timers */
9963 process_timers();
9964
9965 list_for_each_entry(s, &span_list, spans_node) {
9966 for (x = 0; x < s->channels; ++x) {
9967 struct dahdi_chan *const chan = s->chans[x];
9968 if (!chan->confmode)
9969 continue;
9970 spin_lock(&chan->lock);
9971 data = __buf_peek(&chan->confin);
9972 __dahdi_receive_chunk(chan, data);
9973 if (data)
9974 __buf_pull(&chan->confin, NULL, chan);
9975 spin_unlock(&chan->lock);
9976 }
9977 }
9978
9979 /* This is the master channel, so make things switch over */
9980 rotate_sums();
9981
9982 /* do all the pseudo and/or conferenced channel receives (getbuf's) */
9983 list_for_each_entry(pseudo, &pseudo_chans, node) {
9984 spin_lock(&pseudo->chan.lock);
9985 __dahdi_transmit_chunk(&pseudo->chan, NULL);
9986 spin_unlock(&pseudo->chan.lock);
9987 }
9988
9989 #ifdef CONFIG_DAHDI_CONFLINK
9990 if (maxlinks) {
9991 int z;
9992 int y;
9993 #ifdef CONFIG_DAHDI_MMX
9994 dahdi_kernel_fpu_begin();
9995 #endif
9996 /* process all the conf links */
9997 for (x = 1; x <= maxlinks; x++) {
9998 /* if we have a destination conf */
9999 z = confalias[conf_links[x].dst];
10000 if (z) {
10001 y = confalias[conf_links[x].src];
10002 if (y)
10003 ACSS(conf_sums[z], conf_sums[y]);
10004 }
10005 }
10006 #ifdef CONFIG_DAHDI_MMX
10007 dahdi_kernel_fpu_end();
10008 #endif
10009 }
10010 #endif /* CONFIG_DAHDI_CONFLINK */
10011
10012 /* do all the pseudo/conferenced channel transmits (putbuf's) */
10013 list_for_each_entry(pseudo, &pseudo_chans, node) {
10014 pseudo_rx_audio(&pseudo->chan);
10015 }
10016
10017 list_for_each_entry(s, &span_list, spans_node) {
10018 for (x = 0; x < s->channels; x++) {
10019 struct dahdi_chan *const chan = s->chans[x];
10020 if (!chan->confmode)
10021 continue;
10022 spin_lock(&chan->lock);
10023 data = __buf_pushpeek(&chan->confout);
10024 __dahdi_transmit_chunk(chan, data);
10025 if (data)
10026 __buf_push(&chan->confout, NULL);
10027 spin_unlock(&chan->lock);
10028 }
10029
10030 dahdi_sync_tick(s);
10031 }
10032 spin_unlock(&chan_lock);
10033 }
10034
10035 #ifndef CONFIG_DAHDI_CORE_TIMER
10036
10037 static void coretimer_init(void)
10038 {
10039 return;
10040 }
10041
10042 static void coretimer_cleanup(void)
10043 {
10044 return;
10045 }
10046
10047 #else
10048
10049 static unsigned long core_diff_ms(struct timespec *t0, struct timespec *t1)
10050 {
10051 long nanosec, sec;
10052 unsigned long ms;
10053 sec = (t1->tv_sec - t0->tv_sec);
10054 nanosec = (t1->tv_nsec - t0->tv_nsec);
10055 while (nanosec >= NSEC_PER_SEC) {
10056 nanosec -= NSEC_PER_SEC;
10057 ++sec;
10058 }
10059 while (nanosec < 0) {
10060 nanosec += NSEC_PER_SEC;
10061 --sec;
10062 }
10063 ms = (sec * 1000) + (nanosec / 1000000L);
10064 return ms;
10065 }
10066
10067 static inline unsigned long msecs_processed(const struct core_timer *const ct)
10068 {
10069 return atomic_read(&ct->count) * DAHDI_MSECS_PER_CHUNK;
10070 }
10071
10072 static void coretimer_func(unsigned long param)
10073 {
10074 unsigned long flags;
10075 unsigned long ms_since_start;
10076 struct timespec now;
10077 const unsigned long MAX_INTERVAL = 100000L;
10078 const unsigned long ONESEC_INTERVAL = HZ;
10079 const long MS_LIMIT = 3000;
10080 long difference;
10081
10082 ktime_get_ts(&now);
10083
10084 if (atomic_read(&core_timer.count) ==
10085 atomic_read(&core_timer.last_count)) {
10086
10087 /* This is the code path if a board driver is not calling
10088 * dahdi_receive, and therefore the core of dahdi needs to
10089 * perform the master span processing itself. */
10090 if (core_timer.dahdi_receive_used) {
10091 core_timer.dahdi_receive_used = 0;
10092 dahdi_dbg(GENERAL, "Master changed to core_timer\n");
10093 }
10094
10095 if (!atomic_read(&core_timer.shutdown)) {
10096 mod_timer(&core_timer.timer, jiffies +
10097 core_timer.interval);
10098 }
10099
10100 ms_since_start = core_diff_ms(&core_timer.start_interval, &now);
10101
10102 /*
10103 * If the system time has changed, it is possible for us to be
10104 * far behind. If we are more than MS_LIMIT milliseconds
10105 * behind (or ahead in time), just reset our time base and
10106 * continue so that we do not hang the system here.
10107 *
10108 */
10109 difference = ms_since_start - msecs_processed(&core_timer);
10110 if (unlikely((difference > MS_LIMIT) || (difference < 0))) {
10111 if (printk_ratelimit()) {
10112 module_printk(KERN_INFO,
10113 "Detected time shift.\n");
10114 }
10115 atomic_set(&core_timer.count, 0);
10116 atomic_set(&core_timer.last_count, 0);
10117 core_timer.start_interval = now;
10118 return;
10119 }
10120
10121 local_irq_save(flags);
10122 while (ms_since_start > msecs_processed(&core_timer))
10123 _process_masterspan();
10124 local_irq_restore(flags);
10125
10126 if (ms_since_start > MAX_INTERVAL) {
10127 atomic_set(&core_timer.count, 0);
10128 atomic_set(&core_timer.last_count, 0);
10129 core_timer.start_interval = now;
10130 } else {
10131 atomic_set(&core_timer.last_count,
10132 atomic_read(&core_timer.count));
10133 }
10134
10135 } else {
10136
10137 /* It looks like a board driver is calling dahdi_receive. We
10138 * will just check again in a second. */
10139 if (!core_timer.dahdi_receive_used) {
10140 core_timer.dahdi_receive_used = 1;
10141 dahdi_dbg(GENERAL, "Master is no longer core_timer\n");
10142 }
10143 atomic_set(&core_timer.count, 0);
10144 atomic_set(&core_timer.last_count, 0);
10145 core_timer.start_interval = now;
10146 if (!atomic_read(&core_timer.shutdown))
10147 mod_timer(&core_timer.timer, jiffies + ONESEC_INTERVAL);
10148 }
10149 }
10150
10151 static void coretimer_init(void)
10152 {
10153 init_timer(&core_timer.timer);
10154 core_timer.timer.function = coretimer_func;
10155 ktime_get_ts(&core_timer.start_interval);
10156 atomic_set(&core_timer.count, 0);
10157 atomic_set(&core_timer.shutdown, 0);
10158 core_timer.interval = max(msecs_to_jiffies(DAHDI_MSECS_PER_CHUNK), 1UL);
10159 if (core_timer.interval < (HZ/250))
10160 core_timer.interval = (HZ/250);
10161 core_timer.timer.expires = jiffies + core_timer.interval;
10162 add_timer(&core_timer.timer);
10163 }
10164
10165 static void coretimer_cleanup(void)
10166 {
10167 atomic_set(&core_timer.shutdown, 1);
10168 del_timer_sync(&core_timer.timer);
10169 }
10170
10171 #endif /* CONFIG_DAHDI_CORE_TIMER */
10172
10173 /**
10174 * __receive_from_slaves() - Collect the rx data from all the slave channels.
10175 *
10176 */
10177 static void __receive_from_slaves(struct dahdi_chan *const chan)
10178 {
10179 u_char data[DAHDI_CHUNKSIZE];
10180 int i;
10181 int pos = 0;
10182 struct dahdi_chan *slave;
10183
10184 for (i = 0; i < DAHDI_CHUNKSIZE; ++i) {
10185 for (slave = chan; (NULL != slave); slave = slave->nextslave) {
10186 data[pos++] = slave->readchunk[i];
10187 if (pos == DAHDI_CHUNKSIZE) {
10188 __dahdi_receive_chunk(chan, data);
10189 pos = 0;
10190 }
10191 }
10192 }
10193 }
10194
10195 static inline bool should_skip_receive(const struct dahdi_chan *const chan)
10196 {
10197 return (unlikely(chan->flags & DAHDI_FLAG_NOSTDTXRX) ||
10198 (chan->master != chan) ||
10199 is_chan_dacsed(chan));
10200 }
10201
10202 int _dahdi_receive(struct dahdi_span *span)
10203 {
10204 unsigned int x;
10205
10206 #ifdef CONFIG_DAHDI_WATCHDOG
10207 span->watchcounter--;
10208 #endif
10209 for (x = 0; x < span->channels; x++) {
10210 struct dahdi_chan *const chan = span->chans[x];
10211 spin_lock(&chan->lock);
10212 if (should_skip_receive(chan)) {
10213 spin_unlock(&chan->lock);
10214 continue;
10215 }
10216
10217 if (chan->nextslave) {
10218 __receive_from_slaves(chan);
10219 } else {
10220 /* Process a normal channel */
10221 __dahdi_real_receive(chan);
10222 }
10223 if (chan->itimer) {
10224 chan->itimer -= DAHDI_CHUNKSIZE;
10225 if (chan->itimer <= 0)
10226 rbs_itimer_expire(chan);
10227 }
10228 if (chan->ringdebtimer)
10229 chan->ringdebtimer--;
10230 if (chan->sig & __DAHDI_SIG_FXS) {
10231 if (chan->rxhooksig == DAHDI_RXSIG_RING)
10232 chan->ringtrailer = DAHDI_RINGTRAILER;
10233 else if (chan->ringtrailer) {
10234 chan->ringtrailer -= DAHDI_CHUNKSIZE;
10235 /* See if RING trailer is expired */
10236 if (!chan->ringtrailer && !chan->ringdebtimer)
10237 __qevent(chan, DAHDI_EVENT_RINGOFFHOOK);
10238 }
10239 }
10240 if (chan->pulsetimer) {
10241 chan->pulsetimer--;
10242 if (chan->pulsetimer <= 0) {
10243 if (chan->pulsecount) {
10244 if (chan->pulsecount > 12) {
10245
10246 module_printk(KERN_NOTICE, "Got pulse digit %d on %s???\n",
10247 chan->pulsecount,
10248 chan->name);
10249 } else if (chan->pulsecount > 11) {
10250 __qevent(chan, DAHDI_EVENT_PULSEDIGIT | '#');
10251 } else if (chan->pulsecount > 10) {
10252 __qevent(chan, DAHDI_EVENT_PULSEDIGIT | '*');
10253 } else if (chan->pulsecount > 9) {
10254 __qevent(chan, DAHDI_EVENT_PULSEDIGIT | '0');
10255 } else {
10256 __qevent(chan, DAHDI_EVENT_PULSEDIGIT | ('0' +
10257 chan->pulsecount));
10258 }
10259 chan->pulsecount = 0;
10260 }
10261 }
10262 }
10263 #ifdef BUFFER_DEBUG
10264 chan->statcount -= DAHDI_CHUNKSIZE;
10265 #endif
10266 spin_unlock(&chan->lock);
10267 }
10268
10269 if (dahdi_is_sync_master(span))
10270 _process_masterspan();
10271
10272 return 0;
10273 }
10274 EXPORT_SYMBOL(_dahdi_receive);
10275
10276 MODULE_AUTHOR("Mark Spencer <[email protected]>");
10277 MODULE_DESCRIPTION("DAHDI Telephony Interface");
10278 MODULE_LICENSE("GPL v2");
10279 /* DAHDI now provides timing. If anybody wants dahdi_dummy it's probably
10280 * for that. So make dahdi provide it for now. This alias may be removed
10281 * in the future, and users are encouraged not to rely on it. */
10282 MODULE_ALIAS("dahdi_dummy");
10283
10284 module_param(debug, int, 0644);
10285 MODULE_PARM_DESC(debug, "Sets debugging verbosity as a bitfield, to see"\
10286 " general debugging set this to 1. To see RBS debugging set"\
10287 " this to 32");
10288 module_param(deftaps, int, 0644);
10289
10290 module_param(max_pseudo_channels, int, 0644);
10291 MODULE_PARM_DESC(max_pseudo_channels, "Maximum number of pseudo channels.");
10292
10293 module_param(hwec_overrides_swec, int, 0644);
10294 MODULE_PARM_DESC(hwec_overrides_swec, "When true, a hardware echo canceller is used instead of configured SWEC.");
10295
10296 module_param(auto_assign_spans, int, 0644);
10297 MODULE_PARM_DESC(auto_assign_spans,
10298 "If 1 spans will automatically have their children span and "
10299 "channel numbers assigned by the driver. If 0, user space "
10300 "will need to assign them via /sys/bus/dahdi_devices.");
10301
10302
10303 static ssize_t dahdi_no_read(struct file *file, char __user *usrbuf,
10304 size_t count, loff_t *ppos)
10305 {
10306 return -ENOSYS;
10307 }
10308 static ssize_t dahdi_no_write(struct file *file, const char __user *usrbuf,
10309 size_t count, loff_t *ppos)
10310 {
10311 return -ENOSYS;
10312 }
10313
10314 static const struct file_operations dahdi_fops = {
10315 .owner = THIS_MODULE,
10316 .open = dahdi_open,
10317 .release = dahdi_release,
10318 #ifdef HAVE_UNLOCKED_IOCTL
10319 .unlocked_ioctl = dahdi_unlocked_ioctl,
10320 #ifdef HAVE_COMPAT_IOCTL
10321 .compat_ioctl = dahdi_ioctl_compat,
10322 #endif
10323 #else
10324 .ioctl = dahdi_ioctl,
10325 #endif
10326 .poll = dahdi_poll,
10327 .read = dahdi_no_read,
10328 .write = dahdi_no_write,
10329 };
10330
10331 static const struct file_operations dahdi_timer_fops = {
10332 .owner = THIS_MODULE,
10333 .release = dahdi_timer_release,
10334 #ifdef HAVE_UNLOCKED_IOCTL
10335 .unlocked_ioctl = dahdi_timer_unlocked_ioctl,
10336 #ifdef HAVE_COMPAT_IOCTL
10337 .compat_ioctl = dahdi_timer_unlocked_ioctl,
10338 #endif
10339 #else
10340 .ioctl = dahdi_timer_ioctl,
10341 #endif
10342 .poll = dahdi_timer_poll,
10343 .read = dahdi_no_read,
10344 .write = dahdi_no_write,
10345 };
10346
10347 /*
10348 * DAHDI stability should not depend on the calling process behaviour.
10349 * In case of suprise device removal, we should be able to return
10350 * sane results (-ENODEV) even after the underlying device was released.
10351 *
10352 * This should be OK even if the calling process (hint, hint Asterisk)
10353 * ignores the system calls return value.
10354 *
10355 * We simply use dummy file_operations to implement this.
10356 */
10357
10358 /*
10359 * Common behaviour called from all other nodev_*() file_operations
10360 */
10361 static int nodev_common(const char msg[])
10362 {
10363 if (printk_ratelimit()) {
10364 module_printk(KERN_NOTICE,
10365 "nodev: %s: process %d still calling\n",
10366 msg, current->tgid);
10367 }
10368 msleep(5);
10369 return -ENODEV;
10370 }
10371
10372 static ssize_t nodev_chan_read(struct file *file, char __user *usrbuf,
10373 size_t count, loff_t *ppos)
10374 {
10375 return nodev_common("read");
10376 }
10377
10378 static ssize_t nodev_chan_write(struct file *file, const char __user *usrbuf,
10379 size_t count, loff_t *ppos)
10380 {
10381 return nodev_common("write");
10382 }
10383
10384 static unsigned int
10385 nodev_chan_poll(struct file *file, struct poll_table_struct *wait_table)
10386 {
10387 return nodev_common("poll");
10388 }
10389
10390 static long
10391 nodev_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long data)
10392 {
10393 switch (cmd) {
10394 case DAHDI_GETEVENT: /* Get event on queue */
10395 /*
10396 * Hint the bugger that the channel is gone for good
10397 */
10398 put_user(DAHDI_EVENT_REMOVED, (int __user *)data);
10399 break;
10400 }
10401 return nodev_common("ioctl");
10402 }
10403
10404 #ifndef HAVE_UNLOCKED_IOCTL
10405 static int nodev_ioctl(struct inode *inode, struct file *file,
10406 unsigned int cmd, unsigned long data)
10407 {
10408 return nodev_unlocked_ioctl(file, cmd, data);
10409 }
10410 #endif
10411
10412 #ifdef HAVE_COMPAT_IOCTL
10413 static long nodev_ioctl_compat(struct file *file, unsigned int cmd,
10414 unsigned long data)
10415 {
10416 if (cmd == DAHDI_SFCONFIG)
10417 return -ENOTTY; /* Not supported yet */
10418
10419 return nodev_unlocked_ioctl(file, cmd, data);
10420 }
10421 #endif
10422
10423 static const struct file_operations nodev_fops = {
10424 .owner = THIS_MODULE,
10425 #ifdef HAVE_UNLOCKED_IOCTL
10426 .unlocked_ioctl = nodev_unlocked_ioctl,
10427 #ifdef HAVE_COMPAT_IOCTL
10428 .compat_ioctl = nodev_ioctl_compat,
10429 #endif
10430 #else
10431 .ioctl = nodev_ioctl,
10432 #endif
10433 .read = nodev_chan_read,
10434 .write = nodev_chan_write,
10435 .poll = nodev_chan_poll,
10436 };
10437
10438 static const struct file_operations dahdi_chan_fops = {
10439 .owner = THIS_MODULE,
10440 .open = dahdi_open,
10441 .release = dahdi_release,
10442 #ifdef HAVE_UNLOCKED_IOCTL
10443 .unlocked_ioctl = dahdi_unlocked_ioctl,
10444 #ifdef HAVE_COMPAT_IOCTL
10445 .compat_ioctl = dahdi_ioctl_compat,
10446 #endif
10447 #else
10448 .ioctl = dahdi_ioctl,
10449 #endif
10450 .read = dahdi_chan_read,
10451 .write = dahdi_chan_write,
10452 .poll = dahdi_chan_poll,
10453 };
10454
10455 #ifdef CONFIG_DAHDI_WATCHDOG
10456 static struct timer_list watchdogtimer;
10457
10458 static void watchdog_check(unsigned long ignored)
10459 {
10460 unsigned long flags;
10461 static int wdcheck=0;
10462 struct dahdi_span *s;
10463
10464 spin_lock_irqsave(&chan_lock, flags);
10465 list_for_each_entry(s, &span_list, spans_node) {
10466 if (s->flags & DAHDI_FLAG_RUNNING) {
10467 if (s->watchcounter == DAHDI_WATCHDOG_INIT) {
10468 /* Whoops, dead card */
10469 if ((s->watchstate == DAHDI_WATCHSTATE_OK) ||
10470 (s->watchstate == DAHDI_WATCHSTATE_UNKNOWN)) {
10471 s->watchstate = DAHDI_WATCHSTATE_RECOVERING;
10472 if (s->ops->watchdog) {
10473 module_printk(KERN_NOTICE, "Kicking span %s\n", s->name);
10474 s->ops->watchdog(s, DAHDI_WATCHDOG_NOINTS);
10475 } else {
10476 module_printk(KERN_NOTICE, "Span %s is dead with no revival\n", s->name);
10477 s->watchstate = DAHDI_WATCHSTATE_FAILED;
10478 }
10479 }
10480 } else {
10481 if ((s->watchstate != DAHDI_WATCHSTATE_OK) &&
10482 (s->watchstate != DAHDI_WATCHSTATE_UNKNOWN))
10483 module_printk(KERN_NOTICE, "Span %s is alive!\n", s->name);
10484 s->watchstate = DAHDI_WATCHSTATE_OK;
10485 }
10486 s->watchcounter = DAHDI_WATCHDOG_INIT;
10487 }
10488 }
10489 spin_unlock_irqrestore(&chan_lock, flags);
10490 if (!wdcheck) {
10491 module_printk(KERN_NOTICE, "watchdog on duty!\n");
10492 wdcheck=1;
10493 }
10494 mod_timer(&watchdogtimer, jiffies + 2);
10495 }
10496
10497 static int __init watchdog_init(void)
10498 {
10499 init_timer(&watchdogtimer);
10500 watchdogtimer.expires = 0;
10501 watchdogtimer.data =0;
10502 watchdogtimer.function = watchdog_check;
10503 /* Run every couple of jiffy or so */
10504 mod_timer(&watchdogtimer, jiffies + 2);
10505 return 0;
10506 }
10507
10508 static void __exit watchdog_cleanup(void)
10509 {
10510 del_timer(&watchdogtimer);
10511 }
10512
10513 #endif
10514
10515 static int __init dahdi_init(void)
10516 {
10517 int res = 0;
10518
10519 module_printk(KERN_INFO, "Version: %s\n", dahdi_version);
10520 #ifdef CONFIG_PROC_FS
10521 root_proc_entry = proc_mkdir("dahdi", NULL);
10522 if (!root_proc_entry) {
10523 dahdi_err("dahdi init: Failed creating /proc/dahdi\n");
10524 return -EEXIST;
10525 }
10526 #endif
10527 res = dahdi_sysfs_init(&dahdi_fops);
10528 if (res)
10529 goto failed_driver_init;
10530
10531 dahdi_conv_init();
10532 fasthdlc_precalc();
10533 rotate_sums();
10534 #ifdef CONFIG_DAHDI_WATCHDOG
10535 watchdog_init();
10536 #endif
10537 coretimer_init();
10538
10539 res = dahdi_register_echocan_factory(&hwec_factory);
10540 if (res) {
10541 WARN_ON(1);
10542 goto failed_register_ec_factory;
10543 }
10544
10545 return 0;
10546
10547 failed_register_ec_factory:
10548 coretimer_cleanup();
10549 dahdi_sysfs_exit();
10550 failed_driver_init:
10551 if (root_proc_entry) {
10552 remove_proc_entry("dahdi", NULL);
10553 root_proc_entry = NULL;
10554 }
10555 return res;
10556 }
10557
10558 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25)
10559 #ifdef CONFIG_PCI
10560 void dahdi_pci_disable_link_state(struct pci_dev *pdev, int state)
10561 {
10562 u16 reg16;
10563 int pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
10564 state &= (PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
10565 PCIE_LINK_STATE_CLKPM);
10566 if (!pos)
10567 return;
10568 pci_read_config_word(pdev, pos + PCI_EXP_LNKCTL, &reg16);
10569 reg16 &= ~(state);
10570 pci_write_config_word(pdev, pos + PCI_EXP_LNKCTL, reg16);
10571 }
10572 EXPORT_SYMBOL(dahdi_pci_disable_link_state);
10573 #endif /* CONFIG_PCI */
10574 #endif /* 2.6.25 */
10575
10576 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 22)
10577 static inline void flush_find_master_work(void)
10578 {
10579 flush_scheduled_work();
10580 }
10581 #else
10582 static inline void flush_find_master_work(void)
10583 {
10584 cancel_work_sync(&find_master_work);
10585 }
10586 #endif
10587
10588 static void __exit dahdi_cleanup(void)
10589 {
10590 struct dahdi_zone *z;
10591
10592 dahdi_unregister_echocan_factory(&hwec_factory);
10593 coretimer_cleanup();
10594 dahdi_sysfs_exit();
10595
10596 #ifdef CONFIG_PROC_FS
10597 if (root_proc_entry) {
10598 remove_proc_entry("dahdi", NULL);
10599 root_proc_entry = NULL;
10600 }
10601 #endif
10602
10603 module_printk(KERN_INFO, "Telephony Interface Unloaded\n");
10604
10605 spin_lock(&zone_lock);
10606 while (!list_empty(&tone_zones)) {
10607 z = list_entry(tone_zones.next, struct dahdi_zone, node);
10608 list_del(&z->node);
10609 if (!tone_zone_put(z)) {
10610 module_printk(KERN_WARNING,
10611 "Potential memory leak detected in %s\n",
10612 __func__);
10613 }
10614 }
10615 spin_unlock(&zone_lock);
10616
10617 #ifdef CONFIG_DAHDI_WATCHDOG
10618 watchdog_cleanup();
10619 #endif
10620 flush_find_master_work();
10621 }
10622
10623 module_init(dahdi_init);
10624 module_exit(dahdi_cleanup);