Debian Codegraph @

drivers/dahdi/dahdi-base.c

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