Loading...
1/*
2 * Author Andreas Eversberg (jolly@eversberg.eu)
3 * Based on source code structure by
4 * Karsten Keil (keil@isdn4linux.de)
5 *
6 * This file is (c) under GNU PUBLIC LICENSE
7 *
8 * Thanks to Karsten Keil (great drivers)
9 * Cologne Chip (great chips)
10 *
11 * This module does:
12 * Real-time tone generation
13 * DTMF detection
14 * Real-time cross-connection and conferrence
15 * Compensate jitter due to system load and hardware fault.
16 * All features are done in kernel space and will be realized
17 * using hardware, if available and supported by chip set.
18 * Blowfish encryption/decryption
19 */
20
21/* STRUCTURE:
22 *
23 * The dsp module provides layer 2 for b-channels (64kbit). It provides
24 * transparent audio forwarding with special digital signal processing:
25 *
26 * - (1) generation of tones
27 * - (2) detection of dtmf tones
28 * - (3) crossconnecting and conferences (clocking)
29 * - (4) echo generation for delay test
30 * - (5) volume control
31 * - (6) disable receive data
32 * - (7) pipeline
33 * - (8) encryption/decryption
34 *
35 * Look:
36 * TX RX
37 * ------upper layer------
38 * | ^
39 * | |(6)
40 * v |
41 * +-----+-------------+-----+
42 * |(3)(4) |
43 * | CMX |
44 * | |
45 * | +-------------+
46 * | | ^
47 * | | |
48 * |+---------+| +----+----+
49 * ||(1) || |(2) |
50 * || || | |
51 * || Tones || | DTMF |
52 * || || | |
53 * || || | |
54 * |+----+----+| +----+----+
55 * +-----+-----+ ^
56 * | |
57 * v |
58 * +----+----+ +----+----+
59 * |(5) | |(5) |
60 * | | | |
61 * |TX Volume| |RX Volume|
62 * | | | |
63 * | | | |
64 * +----+----+ +----+----+
65 * | ^
66 * | |
67 * v |
68 * +----+-------------+----+
69 * |(7) |
70 * | |
71 * | Pipeline Processing |
72 * | |
73 * | |
74 * +----+-------------+----+
75 * | ^
76 * | |
77 * v |
78 * +----+----+ +----+----+
79 * |(8) | |(8) |
80 * | | | |
81 * | Encrypt | | Decrypt |
82 * | | | |
83 * | | | |
84 * +----+----+ +----+----+
85 * | ^
86 * | |
87 * v |
88 * ------card layer------
89 * TX RX
90 *
91 * Above you can see the logical data flow. If software is used to do the
92 * process, it is actually the real data flow. If hardware is used, data
93 * may not flow, but hardware commands to the card, to provide the data flow
94 * as shown.
95 *
96 * NOTE: The channel must be activated in order to make dsp work, even if
97 * no data flow to the upper layer is intended. Activation can be done
98 * after and before controlling the setting using PH_CONTROL requests.
99 *
100 * DTMF: Will be detected by hardware if possible. It is done before CMX
101 * processing.
102 *
103 * Tones: Will be generated via software if endless looped audio fifos are
104 * not supported by hardware. Tones will override all data from CMX.
105 * It is not required to join a conference to use tones at any time.
106 *
107 * CMX: Is transparent when not used. When it is used, it will do
108 * crossconnections and conferences via software if not possible through
109 * hardware. If hardware capability is available, hardware is used.
110 *
111 * Echo: Is generated by CMX and is used to check performance of hard and
112 * software CMX.
113 *
114 * The CMX has special functions for conferences with one, two and more
115 * members. It will allow different types of data flow. Receive and transmit
116 * data to/form upper layer may be switched on/off individually without losing
117 * features of CMX, Tones and DTMF.
118 *
119 * Echo Cancellation: Sometimes we like to cancel echo from the interface.
120 * Note that a VoIP call may not have echo caused by the IP phone. The echo
121 * is generated by the telephone line connected to it. Because the delay
122 * is high, it becomes an echo. RESULT: Echo Cachelation is required if
123 * both echo AND delay is applied to an interface.
124 * Remember that software CMX always generates a more or less delay.
125 *
126 * If all used features can be realized in hardware, and if transmit and/or
127 * receive data ist disabled, the card may not send/receive any data at all.
128 * Not receiving is useful if only announcements are played. Not sending is
129 * useful if an answering machine records audio. Not sending and receiving is
130 * useful during most states of the call. If supported by hardware, tones
131 * will be played without cpu load. Small PBXs and NT-Mode applications will
132 * not need expensive hardware when processing calls.
133 *
134 *
135 * LOCKING:
136 *
137 * When data is received from upper or lower layer (card), the complete dsp
138 * module is locked by a global lock. This lock MUST lock irq, because it
139 * must lock timer events by DSP poll timer.
140 * When data is ready to be transmitted down, the data is queued and sent
141 * outside lock and timer event.
142 * PH_CONTROL must not change any settings, join or split conference members
143 * during process of data.
144 *
145 * HDLC:
146 *
147 * It works quite the same as transparent, except that HDLC data is forwarded
148 * to all other conference members if no hardware bridging is possible.
149 * Send data will be writte to sendq. Sendq will be sent if confirm is received.
150 * Conference cannot join, if one member is not hdlc.
151 *
152 */
153
154#include <linux/delay.h>
155#include <linux/gfp.h>
156#include <linux/mISDNif.h>
157#include <linux/mISDNdsp.h>
158#include <linux/module.h>
159#include <linux/vmalloc.h>
160#include "core.h"
161#include "dsp.h"
162
163static const char *mISDN_dsp_revision = "2.0";
164
165static int debug;
166static int options;
167static int poll;
168static int dtmfthreshold = 100;
169
170MODULE_AUTHOR("Andreas Eversberg");
171module_param(debug, uint, S_IRUGO | S_IWUSR);
172module_param(options, uint, S_IRUGO | S_IWUSR);
173module_param(poll, uint, S_IRUGO | S_IWUSR);
174module_param(dtmfthreshold, uint, S_IRUGO | S_IWUSR);
175MODULE_LICENSE("GPL");
176
177/*int spinnest = 0;*/
178
179DEFINE_SPINLOCK(dsp_lock); /* global dsp lock */
180LIST_HEAD(dsp_ilist);
181LIST_HEAD(conf_ilist);
182int dsp_debug;
183int dsp_options;
184int dsp_poll, dsp_tics;
185
186/* check if rx may be turned off or must be turned on */
187static void
188dsp_rx_off_member(struct dsp *dsp)
189{
190 struct mISDN_ctrl_req cq;
191 int rx_off = 1;
192
193 memset(&cq, 0, sizeof(cq));
194
195 if (!dsp->features_rx_off)
196 return;
197
198 /* not disabled */
199 if (!dsp->rx_disabled)
200 rx_off = 0;
201 /* software dtmf */
202 else if (dsp->dtmf.software)
203 rx_off = 0;
204 /* echo in software */
205 else if (dsp->echo.software)
206 rx_off = 0;
207 /* bridge in software */
208 else if (dsp->conf && dsp->conf->software)
209 rx_off = 0;
210 /* data is not required by user space and not required
211 * for echo dtmf detection, soft-echo, soft-bridging */
212
213 if (rx_off == dsp->rx_is_off)
214 return;
215
216 if (!dsp->ch.peer) {
217 if (dsp_debug & DEBUG_DSP_CORE)
218 printk(KERN_DEBUG "%s: no peer, no rx_off\n",
219 __func__);
220 return;
221 }
222 cq.op = MISDN_CTRL_RX_OFF;
223 cq.p1 = rx_off;
224 if (dsp->ch.peer->ctrl(dsp->ch.peer, CONTROL_CHANNEL, &cq)) {
225 printk(KERN_DEBUG "%s: 2nd CONTROL_CHANNEL failed\n",
226 __func__);
227 return;
228 }
229 dsp->rx_is_off = rx_off;
230 if (dsp_debug & DEBUG_DSP_CORE)
231 printk(KERN_DEBUG "%s: %s set rx_off = %d\n",
232 __func__, dsp->name, rx_off);
233}
234static void
235dsp_rx_off(struct dsp *dsp)
236{
237 struct dsp_conf_member *member;
238
239 if (dsp_options & DSP_OPT_NOHARDWARE)
240 return;
241
242 /* no conf */
243 if (!dsp->conf) {
244 dsp_rx_off_member(dsp);
245 return;
246 }
247 /* check all members in conf */
248 list_for_each_entry(member, &dsp->conf->mlist, list) {
249 dsp_rx_off_member(member->dsp);
250 }
251}
252
253/* enable "fill empty" feature */
254static void
255dsp_fill_empty(struct dsp *dsp)
256{
257 struct mISDN_ctrl_req cq;
258
259 memset(&cq, 0, sizeof(cq));
260
261 if (!dsp->ch.peer) {
262 if (dsp_debug & DEBUG_DSP_CORE)
263 printk(KERN_DEBUG "%s: no peer, no fill_empty\n",
264 __func__);
265 return;
266 }
267 cq.op = MISDN_CTRL_FILL_EMPTY;
268 cq.p1 = 1;
269 cq.p2 = dsp_silence;
270 if (dsp->ch.peer->ctrl(dsp->ch.peer, CONTROL_CHANNEL, &cq)) {
271 printk(KERN_DEBUG "%s: CONTROL_CHANNEL failed\n",
272 __func__);
273 return;
274 }
275 if (dsp_debug & DEBUG_DSP_CORE)
276 printk(KERN_DEBUG "%s: %s set fill_empty = 1\n",
277 __func__, dsp->name);
278}
279
280static int
281dsp_control_req(struct dsp *dsp, struct mISDNhead *hh, struct sk_buff *skb)
282{
283 struct sk_buff *nskb;
284 int ret = 0;
285 int cont;
286 u8 *data;
287 int len;
288
289 if (skb->len < sizeof(int)) {
290 printk(KERN_ERR "%s: PH_CONTROL message too short\n", __func__);
291 return -EINVAL;
292 }
293 cont = *((int *)skb->data);
294 len = skb->len - sizeof(int);
295 data = skb->data + sizeof(int);
296
297 switch (cont) {
298 case DTMF_TONE_START: /* turn on DTMF */
299 if (dsp->hdlc) {
300 ret = -EINVAL;
301 break;
302 }
303 if (dsp_debug & DEBUG_DSP_CORE)
304 printk(KERN_DEBUG "%s: start dtmf\n", __func__);
305 if (len == sizeof(int)) {
306 if (dsp_debug & DEBUG_DSP_CORE)
307 printk(KERN_NOTICE "changing DTMF Threshold "
308 "to %d\n", *((int *)data));
309 dsp->dtmf.treshold = (*(int *)data) * 10000;
310 }
311 dsp->dtmf.enable = 1;
312 /* init goertzel */
313 dsp_dtmf_goertzel_init(dsp);
314
315 /* check dtmf hardware */
316 dsp_dtmf_hardware(dsp);
317 dsp_rx_off(dsp);
318 break;
319 case DTMF_TONE_STOP: /* turn off DTMF */
320 if (dsp_debug & DEBUG_DSP_CORE)
321 printk(KERN_DEBUG "%s: stop dtmf\n", __func__);
322 dsp->dtmf.enable = 0;
323 dsp->dtmf.hardware = 0;
324 dsp->dtmf.software = 0;
325 break;
326 case DSP_CONF_JOIN: /* join / update conference */
327 if (len < sizeof(int)) {
328 ret = -EINVAL;
329 break;
330 }
331 if (*((u32 *)data) == 0)
332 goto conf_split;
333 if (dsp_debug & DEBUG_DSP_CORE)
334 printk(KERN_DEBUG "%s: join conference %d\n",
335 __func__, *((u32 *)data));
336 ret = dsp_cmx_conf(dsp, *((u32 *)data));
337 /* dsp_cmx_hardware will also be called here */
338 dsp_rx_off(dsp);
339 if (dsp_debug & DEBUG_DSP_CMX)
340 dsp_cmx_debug(dsp);
341 break;
342 case DSP_CONF_SPLIT: /* remove from conference */
343 conf_split:
344 if (dsp_debug & DEBUG_DSP_CORE)
345 printk(KERN_DEBUG "%s: release conference\n", __func__);
346 ret = dsp_cmx_conf(dsp, 0);
347 /* dsp_cmx_hardware will also be called here */
348 if (dsp_debug & DEBUG_DSP_CMX)
349 dsp_cmx_debug(dsp);
350 dsp_rx_off(dsp);
351 break;
352 case DSP_TONE_PATT_ON: /* play tone */
353 if (dsp->hdlc) {
354 ret = -EINVAL;
355 break;
356 }
357 if (len < sizeof(int)) {
358 ret = -EINVAL;
359 break;
360 }
361 if (dsp_debug & DEBUG_DSP_CORE)
362 printk(KERN_DEBUG "%s: turn tone 0x%x on\n",
363 __func__, *((int *)skb->data));
364 ret = dsp_tone(dsp, *((int *)data));
365 if (!ret) {
366 dsp_cmx_hardware(dsp->conf, dsp);
367 dsp_rx_off(dsp);
368 }
369 if (!dsp->tone.tone)
370 goto tone_off;
371 break;
372 case DSP_TONE_PATT_OFF: /* stop tone */
373 if (dsp->hdlc) {
374 ret = -EINVAL;
375 break;
376 }
377 if (dsp_debug & DEBUG_DSP_CORE)
378 printk(KERN_DEBUG "%s: turn tone off\n", __func__);
379 dsp_tone(dsp, 0);
380 dsp_cmx_hardware(dsp->conf, dsp);
381 dsp_rx_off(dsp);
382 /* reset tx buffers (user space data) */
383 tone_off:
384 dsp->rx_W = 0;
385 dsp->rx_R = 0;
386 break;
387 case DSP_VOL_CHANGE_TX: /* change volume */
388 if (dsp->hdlc) {
389 ret = -EINVAL;
390 break;
391 }
392 if (len < sizeof(int)) {
393 ret = -EINVAL;
394 break;
395 }
396 dsp->tx_volume = *((int *)data);
397 if (dsp_debug & DEBUG_DSP_CORE)
398 printk(KERN_DEBUG "%s: change tx vol to %d\n",
399 __func__, dsp->tx_volume);
400 dsp_cmx_hardware(dsp->conf, dsp);
401 dsp_dtmf_hardware(dsp);
402 dsp_rx_off(dsp);
403 break;
404 case DSP_VOL_CHANGE_RX: /* change volume */
405 if (dsp->hdlc) {
406 ret = -EINVAL;
407 break;
408 }
409 if (len < sizeof(int)) {
410 ret = -EINVAL;
411 break;
412 }
413 dsp->rx_volume = *((int *)data);
414 if (dsp_debug & DEBUG_DSP_CORE)
415 printk(KERN_DEBUG "%s: change rx vol to %d\n",
416 __func__, dsp->tx_volume);
417 dsp_cmx_hardware(dsp->conf, dsp);
418 dsp_dtmf_hardware(dsp);
419 dsp_rx_off(dsp);
420 break;
421 case DSP_ECHO_ON: /* enable echo */
422 dsp->echo.software = 1; /* soft echo */
423 if (dsp_debug & DEBUG_DSP_CORE)
424 printk(KERN_DEBUG "%s: enable cmx-echo\n", __func__);
425 dsp_cmx_hardware(dsp->conf, dsp);
426 dsp_rx_off(dsp);
427 if (dsp_debug & DEBUG_DSP_CMX)
428 dsp_cmx_debug(dsp);
429 break;
430 case DSP_ECHO_OFF: /* disable echo */
431 dsp->echo.software = 0;
432 dsp->echo.hardware = 0;
433 if (dsp_debug & DEBUG_DSP_CORE)
434 printk(KERN_DEBUG "%s: disable cmx-echo\n", __func__);
435 dsp_cmx_hardware(dsp->conf, dsp);
436 dsp_rx_off(dsp);
437 if (dsp_debug & DEBUG_DSP_CMX)
438 dsp_cmx_debug(dsp);
439 break;
440 case DSP_RECEIVE_ON: /* enable receive to user space */
441 if (dsp_debug & DEBUG_DSP_CORE)
442 printk(KERN_DEBUG "%s: enable receive to user "
443 "space\n", __func__);
444 dsp->rx_disabled = 0;
445 dsp_rx_off(dsp);
446 break;
447 case DSP_RECEIVE_OFF: /* disable receive to user space */
448 if (dsp_debug & DEBUG_DSP_CORE)
449 printk(KERN_DEBUG "%s: disable receive to "
450 "user space\n", __func__);
451 dsp->rx_disabled = 1;
452 dsp_rx_off(dsp);
453 break;
454 case DSP_MIX_ON: /* enable mixing of tx data */
455 if (dsp->hdlc) {
456 ret = -EINVAL;
457 break;
458 }
459 if (dsp_debug & DEBUG_DSP_CORE)
460 printk(KERN_DEBUG "%s: enable mixing of "
461 "tx-data with conf members\n", __func__);
462 dsp->tx_mix = 1;
463 dsp_cmx_hardware(dsp->conf, dsp);
464 dsp_rx_off(dsp);
465 if (dsp_debug & DEBUG_DSP_CMX)
466 dsp_cmx_debug(dsp);
467 break;
468 case DSP_MIX_OFF: /* disable mixing of tx data */
469 if (dsp->hdlc) {
470 ret = -EINVAL;
471 break;
472 }
473 if (dsp_debug & DEBUG_DSP_CORE)
474 printk(KERN_DEBUG "%s: disable mixing of "
475 "tx-data with conf members\n", __func__);
476 dsp->tx_mix = 0;
477 dsp_cmx_hardware(dsp->conf, dsp);
478 dsp_rx_off(dsp);
479 if (dsp_debug & DEBUG_DSP_CMX)
480 dsp_cmx_debug(dsp);
481 break;
482 case DSP_TXDATA_ON: /* enable txdata */
483 dsp->tx_data = 1;
484 if (dsp_debug & DEBUG_DSP_CORE)
485 printk(KERN_DEBUG "%s: enable tx-data\n", __func__);
486 dsp_cmx_hardware(dsp->conf, dsp);
487 dsp_rx_off(dsp);
488 if (dsp_debug & DEBUG_DSP_CMX)
489 dsp_cmx_debug(dsp);
490 break;
491 case DSP_TXDATA_OFF: /* disable txdata */
492 dsp->tx_data = 0;
493 if (dsp_debug & DEBUG_DSP_CORE)
494 printk(KERN_DEBUG "%s: disable tx-data\n", __func__);
495 dsp_cmx_hardware(dsp->conf, dsp);
496 dsp_rx_off(dsp);
497 if (dsp_debug & DEBUG_DSP_CMX)
498 dsp_cmx_debug(dsp);
499 break;
500 case DSP_DELAY: /* use delay algorithm instead of dynamic
501 jitter algorithm */
502 if (dsp->hdlc) {
503 ret = -EINVAL;
504 break;
505 }
506 if (len < sizeof(int)) {
507 ret = -EINVAL;
508 break;
509 }
510 dsp->cmx_delay = (*((int *)data)) << 3;
511 /* milliseconds to samples */
512 if (dsp->cmx_delay >= (CMX_BUFF_HALF >> 1))
513 /* clip to half of maximum usable buffer
514 (half of half buffer) */
515 dsp->cmx_delay = (CMX_BUFF_HALF >> 1) - 1;
516 if (dsp_debug & DEBUG_DSP_CORE)
517 printk(KERN_DEBUG "%s: use delay algorithm to "
518 "compensate jitter (%d samples)\n",
519 __func__, dsp->cmx_delay);
520 break;
521 case DSP_JITTER: /* use dynamic jitter algorithm instead of
522 delay algorithm */
523 if (dsp->hdlc) {
524 ret = -EINVAL;
525 break;
526 }
527 dsp->cmx_delay = 0;
528 if (dsp_debug & DEBUG_DSP_CORE)
529 printk(KERN_DEBUG "%s: use jitter algorithm to "
530 "compensate jitter\n", __func__);
531 break;
532 case DSP_TX_DEJITTER: /* use dynamic jitter algorithm for tx-buffer */
533 if (dsp->hdlc) {
534 ret = -EINVAL;
535 break;
536 }
537 dsp->tx_dejitter = 1;
538 if (dsp_debug & DEBUG_DSP_CORE)
539 printk(KERN_DEBUG "%s: use dejitter on TX "
540 "buffer\n", __func__);
541 break;
542 case DSP_TX_DEJ_OFF: /* use tx-buffer without dejittering*/
543 if (dsp->hdlc) {
544 ret = -EINVAL;
545 break;
546 }
547 dsp->tx_dejitter = 0;
548 if (dsp_debug & DEBUG_DSP_CORE)
549 printk(KERN_DEBUG "%s: use TX buffer without "
550 "dejittering\n", __func__);
551 break;
552 case DSP_PIPELINE_CFG:
553 if (dsp->hdlc) {
554 ret = -EINVAL;
555 break;
556 }
557 if (len > 0 && ((char *)data)[len - 1]) {
558 printk(KERN_DEBUG "%s: pipeline config string "
559 "is not NULL terminated!\n", __func__);
560 ret = -EINVAL;
561 } else {
562 dsp->pipeline.inuse = 1;
563 dsp_cmx_hardware(dsp->conf, dsp);
564 ret = dsp_pipeline_build(&dsp->pipeline,
565 len > 0 ? data : NULL);
566 dsp_cmx_hardware(dsp->conf, dsp);
567 dsp_rx_off(dsp);
568 }
569 break;
570 case DSP_BF_ENABLE_KEY: /* turn blowfish on */
571 if (dsp->hdlc) {
572 ret = -EINVAL;
573 break;
574 }
575 if (len < 4 || len > 56) {
576 ret = -EINVAL;
577 break;
578 }
579 if (dsp_debug & DEBUG_DSP_CORE)
580 printk(KERN_DEBUG "%s: turn blowfish on (key "
581 "not shown)\n", __func__);
582 ret = dsp_bf_init(dsp, (u8 *)data, len);
583 /* set new cont */
584 if (!ret)
585 cont = DSP_BF_ACCEPT;
586 else
587 cont = DSP_BF_REJECT;
588 /* send indication if it worked to set it */
589 nskb = _alloc_mISDN_skb(PH_CONTROL_IND, MISDN_ID_ANY,
590 sizeof(int), &cont, GFP_ATOMIC);
591 if (nskb) {
592 if (dsp->up) {
593 if (dsp->up->send(dsp->up, nskb))
594 dev_kfree_skb(nskb);
595 } else
596 dev_kfree_skb(nskb);
597 }
598 if (!ret) {
599 dsp_cmx_hardware(dsp->conf, dsp);
600 dsp_dtmf_hardware(dsp);
601 dsp_rx_off(dsp);
602 }
603 break;
604 case DSP_BF_DISABLE: /* turn blowfish off */
605 if (dsp->hdlc) {
606 ret = -EINVAL;
607 break;
608 }
609 if (dsp_debug & DEBUG_DSP_CORE)
610 printk(KERN_DEBUG "%s: turn blowfish off\n", __func__);
611 dsp_bf_cleanup(dsp);
612 dsp_cmx_hardware(dsp->conf, dsp);
613 dsp_dtmf_hardware(dsp);
614 dsp_rx_off(dsp);
615 break;
616 default:
617 if (dsp_debug & DEBUG_DSP_CORE)
618 printk(KERN_DEBUG "%s: ctrl req %x unhandled\n",
619 __func__, cont);
620 ret = -EINVAL;
621 }
622 return ret;
623}
624
625static void
626get_features(struct mISDNchannel *ch)
627{
628 struct dsp *dsp = container_of(ch, struct dsp, ch);
629 struct mISDN_ctrl_req cq;
630
631 if (!ch->peer) {
632 if (dsp_debug & DEBUG_DSP_CORE)
633 printk(KERN_DEBUG "%s: no peer, no features\n",
634 __func__);
635 return;
636 }
637 memset(&cq, 0, sizeof(cq));
638 cq.op = MISDN_CTRL_GETOP;
639 if (ch->peer->ctrl(ch->peer, CONTROL_CHANNEL, &cq) < 0) {
640 printk(KERN_DEBUG "%s: CONTROL_CHANNEL failed\n",
641 __func__);
642 return;
643 }
644 if (cq.op & MISDN_CTRL_RX_OFF)
645 dsp->features_rx_off = 1;
646 if (cq.op & MISDN_CTRL_FILL_EMPTY)
647 dsp->features_fill_empty = 1;
648 if (dsp_options & DSP_OPT_NOHARDWARE)
649 return;
650 if ((cq.op & MISDN_CTRL_HW_FEATURES_OP)) {
651 cq.op = MISDN_CTRL_HW_FEATURES;
652 *((u_long *)&cq.p1) = (u_long)&dsp->features;
653 if (ch->peer->ctrl(ch->peer, CONTROL_CHANNEL, &cq)) {
654 printk(KERN_DEBUG "%s: 2nd CONTROL_CHANNEL failed\n",
655 __func__);
656 }
657 } else
658 if (dsp_debug & DEBUG_DSP_CORE)
659 printk(KERN_DEBUG "%s: features not supported for %s\n",
660 __func__, dsp->name);
661}
662
663static int
664dsp_function(struct mISDNchannel *ch, struct sk_buff *skb)
665{
666 struct dsp *dsp = container_of(ch, struct dsp, ch);
667 struct mISDNhead *hh;
668 int ret = 0;
669 u8 *digits = NULL;
670 u_long flags;
671
672 hh = mISDN_HEAD_P(skb);
673 switch (hh->prim) {
674 /* FROM DOWN */
675 case (PH_DATA_CNF):
676 dsp->data_pending = 0;
677 /* trigger next hdlc frame, if any */
678 if (dsp->hdlc) {
679 spin_lock_irqsave(&dsp_lock, flags);
680 if (dsp->b_active)
681 schedule_work(&dsp->workq);
682 spin_unlock_irqrestore(&dsp_lock, flags);
683 }
684 break;
685 case (PH_DATA_IND):
686 case (DL_DATA_IND):
687 if (skb->len < 1) {
688 ret = -EINVAL;
689 break;
690 }
691 if (dsp->rx_is_off) {
692 if (dsp_debug & DEBUG_DSP_CORE)
693 printk(KERN_DEBUG "%s: rx-data during rx_off"
694 " for %s\n",
695 __func__, dsp->name);
696 }
697 if (dsp->hdlc) {
698 /* hdlc */
699 spin_lock_irqsave(&dsp_lock, flags);
700 dsp_cmx_hdlc(dsp, skb);
701 spin_unlock_irqrestore(&dsp_lock, flags);
702 if (dsp->rx_disabled) {
703 /* if receive is not allowed */
704 break;
705 }
706 hh->prim = DL_DATA_IND;
707 if (dsp->up)
708 return dsp->up->send(dsp->up, skb);
709 break;
710 }
711
712 spin_lock_irqsave(&dsp_lock, flags);
713
714 /* decrypt if enabled */
715 if (dsp->bf_enable)
716 dsp_bf_decrypt(dsp, skb->data, skb->len);
717 /* pipeline */
718 if (dsp->pipeline.inuse)
719 dsp_pipeline_process_rx(&dsp->pipeline, skb->data,
720 skb->len, hh->id);
721 /* change volume if requested */
722 if (dsp->rx_volume)
723 dsp_change_volume(skb, dsp->rx_volume);
724 /* check if dtmf soft decoding is turned on */
725 if (dsp->dtmf.software) {
726 digits = dsp_dtmf_goertzel_decode(dsp, skb->data,
727 skb->len, (dsp_options & DSP_OPT_ULAW) ? 1 : 0);
728 }
729 /* we need to process receive data if software */
730 if (dsp->conf && dsp->conf->software) {
731 /* process data from card at cmx */
732 dsp_cmx_receive(dsp, skb);
733 }
734
735 spin_unlock_irqrestore(&dsp_lock, flags);
736
737 /* send dtmf result, if any */
738 if (digits) {
739 while (*digits) {
740 int k;
741 struct sk_buff *nskb;
742 if (dsp_debug & DEBUG_DSP_DTMF)
743 printk(KERN_DEBUG "%s: digit"
744 "(%c) to layer %s\n",
745 __func__, *digits, dsp->name);
746 k = *digits | DTMF_TONE_VAL;
747 nskb = _alloc_mISDN_skb(PH_CONTROL_IND,
748 MISDN_ID_ANY, sizeof(int), &k,
749 GFP_ATOMIC);
750 if (nskb) {
751 if (dsp->up) {
752 if (dsp->up->send(
753 dsp->up, nskb))
754 dev_kfree_skb(nskb);
755 } else
756 dev_kfree_skb(nskb);
757 }
758 digits++;
759 }
760 }
761 if (dsp->rx_disabled) {
762 /* if receive is not allowed */
763 break;
764 }
765 hh->prim = DL_DATA_IND;
766 if (dsp->up)
767 return dsp->up->send(dsp->up, skb);
768 break;
769 case (PH_CONTROL_IND):
770 if (dsp_debug & DEBUG_DSP_DTMFCOEFF)
771 printk(KERN_DEBUG "%s: PH_CONTROL INDICATION "
772 "received: %x (len %d) %s\n", __func__,
773 hh->id, skb->len, dsp->name);
774 switch (hh->id) {
775 case (DTMF_HFC_COEF): /* getting coefficients */
776 if (!dsp->dtmf.hardware) {
777 if (dsp_debug & DEBUG_DSP_DTMFCOEFF)
778 printk(KERN_DEBUG "%s: ignoring DTMF "
779 "coefficients from HFC\n",
780 __func__);
781 break;
782 }
783 digits = dsp_dtmf_goertzel_decode(dsp, skb->data,
784 skb->len, 2);
785 while (*digits) {
786 int k;
787 struct sk_buff *nskb;
788 if (dsp_debug & DEBUG_DSP_DTMF)
789 printk(KERN_DEBUG "%s: digit"
790 "(%c) to layer %s\n",
791 __func__, *digits, dsp->name);
792 k = *digits | DTMF_TONE_VAL;
793 nskb = _alloc_mISDN_skb(PH_CONTROL_IND,
794 MISDN_ID_ANY, sizeof(int), &k,
795 GFP_ATOMIC);
796 if (nskb) {
797 if (dsp->up) {
798 if (dsp->up->send(
799 dsp->up, nskb))
800 dev_kfree_skb(nskb);
801 } else
802 dev_kfree_skb(nskb);
803 }
804 digits++;
805 }
806 break;
807 case (HFC_VOL_CHANGE_TX): /* change volume */
808 if (skb->len != sizeof(int)) {
809 ret = -EINVAL;
810 break;
811 }
812 spin_lock_irqsave(&dsp_lock, flags);
813 dsp->tx_volume = *((int *)skb->data);
814 if (dsp_debug & DEBUG_DSP_CORE)
815 printk(KERN_DEBUG "%s: change tx volume to "
816 "%d\n", __func__, dsp->tx_volume);
817 dsp_cmx_hardware(dsp->conf, dsp);
818 dsp_dtmf_hardware(dsp);
819 dsp_rx_off(dsp);
820 spin_unlock_irqrestore(&dsp_lock, flags);
821 break;
822 default:
823 if (dsp_debug & DEBUG_DSP_CORE)
824 printk(KERN_DEBUG "%s: ctrl ind %x unhandled "
825 "%s\n", __func__, hh->id, dsp->name);
826 ret = -EINVAL;
827 }
828 break;
829 case (PH_ACTIVATE_IND):
830 case (PH_ACTIVATE_CNF):
831 if (dsp_debug & DEBUG_DSP_CORE)
832 printk(KERN_DEBUG "%s: b_channel is now active %s\n",
833 __func__, dsp->name);
834 /* bchannel now active */
835 spin_lock_irqsave(&dsp_lock, flags);
836 dsp->b_active = 1;
837 dsp->data_pending = 0;
838 dsp->rx_init = 1;
839 /* rx_W and rx_R will be adjusted on first frame */
840 dsp->rx_W = 0;
841 dsp->rx_R = 0;
842 memset(dsp->rx_buff, 0, sizeof(dsp->rx_buff));
843 dsp_cmx_hardware(dsp->conf, dsp);
844 dsp_dtmf_hardware(dsp);
845 dsp_rx_off(dsp);
846 spin_unlock_irqrestore(&dsp_lock, flags);
847 if (dsp_debug & DEBUG_DSP_CORE)
848 printk(KERN_DEBUG "%s: done with activation, sending "
849 "confirm to user space. %s\n", __func__,
850 dsp->name);
851 /* send activation to upper layer */
852 hh->prim = DL_ESTABLISH_CNF;
853 if (dsp->up)
854 return dsp->up->send(dsp->up, skb);
855 break;
856 case (PH_DEACTIVATE_IND):
857 case (PH_DEACTIVATE_CNF):
858 if (dsp_debug & DEBUG_DSP_CORE)
859 printk(KERN_DEBUG "%s: b_channel is now inactive %s\n",
860 __func__, dsp->name);
861 /* bchannel now inactive */
862 spin_lock_irqsave(&dsp_lock, flags);
863 dsp->b_active = 0;
864 dsp->data_pending = 0;
865 dsp_cmx_hardware(dsp->conf, dsp);
866 dsp_rx_off(dsp);
867 spin_unlock_irqrestore(&dsp_lock, flags);
868 hh->prim = DL_RELEASE_CNF;
869 if (dsp->up)
870 return dsp->up->send(dsp->up, skb);
871 break;
872 /* FROM UP */
873 case (DL_DATA_REQ):
874 case (PH_DATA_REQ):
875 if (skb->len < 1) {
876 ret = -EINVAL;
877 break;
878 }
879 if (dsp->hdlc) {
880 /* hdlc */
881 if (!dsp->b_active) {
882 ret = -EIO;
883 break;
884 }
885 hh->prim = PH_DATA_REQ;
886 spin_lock_irqsave(&dsp_lock, flags);
887 skb_queue_tail(&dsp->sendq, skb);
888 schedule_work(&dsp->workq);
889 spin_unlock_irqrestore(&dsp_lock, flags);
890 return 0;
891 }
892 /* send data to tx-buffer (if no tone is played) */
893 if (!dsp->tone.tone) {
894 spin_lock_irqsave(&dsp_lock, flags);
895 dsp_cmx_transmit(dsp, skb);
896 spin_unlock_irqrestore(&dsp_lock, flags);
897 }
898 break;
899 case (PH_CONTROL_REQ):
900 spin_lock_irqsave(&dsp_lock, flags);
901 ret = dsp_control_req(dsp, hh, skb);
902 spin_unlock_irqrestore(&dsp_lock, flags);
903 break;
904 case (DL_ESTABLISH_REQ):
905 case (PH_ACTIVATE_REQ):
906 if (dsp_debug & DEBUG_DSP_CORE)
907 printk(KERN_DEBUG "%s: activating b_channel %s\n",
908 __func__, dsp->name);
909 if (dsp->dtmf.hardware || dsp->dtmf.software)
910 dsp_dtmf_goertzel_init(dsp);
911 get_features(ch);
912 /* enable fill_empty feature */
913 if (dsp->features_fill_empty)
914 dsp_fill_empty(dsp);
915 /* send ph_activate */
916 hh->prim = PH_ACTIVATE_REQ;
917 if (ch->peer)
918 return ch->recv(ch->peer, skb);
919 break;
920 case (DL_RELEASE_REQ):
921 case (PH_DEACTIVATE_REQ):
922 if (dsp_debug & DEBUG_DSP_CORE)
923 printk(KERN_DEBUG "%s: releasing b_channel %s\n",
924 __func__, dsp->name);
925 spin_lock_irqsave(&dsp_lock, flags);
926 dsp->tone.tone = 0;
927 dsp->tone.hardware = 0;
928 dsp->tone.software = 0;
929 if (timer_pending(&dsp->tone.tl))
930 del_timer(&dsp->tone.tl);
931 if (dsp->conf)
932 dsp_cmx_conf(dsp, 0); /* dsp_cmx_hardware will also be
933 called here */
934 skb_queue_purge(&dsp->sendq);
935 spin_unlock_irqrestore(&dsp_lock, flags);
936 hh->prim = PH_DEACTIVATE_REQ;
937 if (ch->peer)
938 return ch->recv(ch->peer, skb);
939 break;
940 default:
941 if (dsp_debug & DEBUG_DSP_CORE)
942 printk(KERN_DEBUG "%s: msg %x unhandled %s\n",
943 __func__, hh->prim, dsp->name);
944 ret = -EINVAL;
945 }
946 if (!ret)
947 dev_kfree_skb(skb);
948 return ret;
949}
950
951static int
952dsp_ctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
953{
954 struct dsp *dsp = container_of(ch, struct dsp, ch);
955 u_long flags;
956
957 if (debug & DEBUG_DSP_CTRL)
958 printk(KERN_DEBUG "%s:(%x)\n", __func__, cmd);
959
960 switch (cmd) {
961 case OPEN_CHANNEL:
962 break;
963 case CLOSE_CHANNEL:
964 if (dsp->ch.peer)
965 dsp->ch.peer->ctrl(dsp->ch.peer, CLOSE_CHANNEL, NULL);
966
967 /* wait until workqueue has finished,
968 * must lock here, or we may hit send-process currently
969 * queueing. */
970 spin_lock_irqsave(&dsp_lock, flags);
971 dsp->b_active = 0;
972 spin_unlock_irqrestore(&dsp_lock, flags);
973 /* MUST not be locked, because it waits until queue is done. */
974 cancel_work_sync(&dsp->workq);
975 spin_lock_irqsave(&dsp_lock, flags);
976 if (timer_pending(&dsp->tone.tl))
977 del_timer(&dsp->tone.tl);
978 skb_queue_purge(&dsp->sendq);
979 if (dsp_debug & DEBUG_DSP_CTRL)
980 printk(KERN_DEBUG "%s: releasing member %s\n",
981 __func__, dsp->name);
982 dsp->b_active = 0;
983 dsp_cmx_conf(dsp, 0); /* dsp_cmx_hardware will also be called
984 here */
985 dsp_pipeline_destroy(&dsp->pipeline);
986
987 if (dsp_debug & DEBUG_DSP_CTRL)
988 printk(KERN_DEBUG "%s: remove & destroy object %s\n",
989 __func__, dsp->name);
990 list_del(&dsp->list);
991 spin_unlock_irqrestore(&dsp_lock, flags);
992
993 if (dsp_debug & DEBUG_DSP_CTRL)
994 printk(KERN_DEBUG "%s: dsp instance released\n",
995 __func__);
996 vfree(dsp);
997 module_put(THIS_MODULE);
998 break;
999 }
1000 return 0;
1001}
1002
1003static void
1004dsp_send_bh(struct work_struct *work)
1005{
1006 struct dsp *dsp = container_of(work, struct dsp, workq);
1007 struct sk_buff *skb;
1008 struct mISDNhead *hh;
1009
1010 if (dsp->hdlc && dsp->data_pending)
1011 return; /* wait until data has been acknowledged */
1012
1013 /* send queued data */
1014 while ((skb = skb_dequeue(&dsp->sendq))) {
1015 /* in locked date, we must have still data in queue */
1016 if (dsp->data_pending) {
1017 if (dsp_debug & DEBUG_DSP_CORE)
1018 printk(KERN_DEBUG "%s: fifo full %s, this is "
1019 "no bug!\n", __func__, dsp->name);
1020 /* flush transparent data, if not acked */
1021 dev_kfree_skb(skb);
1022 continue;
1023 }
1024 hh = mISDN_HEAD_P(skb);
1025 if (hh->prim == DL_DATA_REQ) {
1026 /* send packet up */
1027 if (dsp->up) {
1028 if (dsp->up->send(dsp->up, skb))
1029 dev_kfree_skb(skb);
1030 } else
1031 dev_kfree_skb(skb);
1032 } else {
1033 /* send packet down */
1034 if (dsp->ch.peer) {
1035 dsp->data_pending = 1;
1036 if (dsp->ch.recv(dsp->ch.peer, skb)) {
1037 dev_kfree_skb(skb);
1038 dsp->data_pending = 0;
1039 }
1040 } else
1041 dev_kfree_skb(skb);
1042 }
1043 }
1044}
1045
1046static int
1047dspcreate(struct channel_req *crq)
1048{
1049 struct dsp *ndsp;
1050 u_long flags;
1051
1052 if (crq->protocol != ISDN_P_B_L2DSP
1053 && crq->protocol != ISDN_P_B_L2DSPHDLC)
1054 return -EPROTONOSUPPORT;
1055 ndsp = vzalloc(sizeof(struct dsp));
1056 if (!ndsp) {
1057 printk(KERN_ERR "%s: vmalloc struct dsp failed\n", __func__);
1058 return -ENOMEM;
1059 }
1060 if (dsp_debug & DEBUG_DSP_CTRL)
1061 printk(KERN_DEBUG "%s: creating new dsp instance\n", __func__);
1062
1063 /* default enabled */
1064 INIT_WORK(&ndsp->workq, (void *)dsp_send_bh);
1065 skb_queue_head_init(&ndsp->sendq);
1066 ndsp->ch.send = dsp_function;
1067 ndsp->ch.ctrl = dsp_ctrl;
1068 ndsp->up = crq->ch;
1069 crq->ch = &ndsp->ch;
1070 if (crq->protocol == ISDN_P_B_L2DSP) {
1071 crq->protocol = ISDN_P_B_RAW;
1072 ndsp->hdlc = 0;
1073 } else {
1074 crq->protocol = ISDN_P_B_HDLC;
1075 ndsp->hdlc = 1;
1076 }
1077 if (!try_module_get(THIS_MODULE))
1078 printk(KERN_WARNING "%s:cannot get module\n",
1079 __func__);
1080
1081 sprintf(ndsp->name, "DSP_C%x(0x%p)",
1082 ndsp->up->st->dev->id + 1, ndsp);
1083 /* set frame size to start */
1084 ndsp->features.hfc_id = -1; /* current PCM id */
1085 ndsp->features.pcm_id = -1; /* current PCM id */
1086 ndsp->pcm_slot_rx = -1; /* current CPM slot */
1087 ndsp->pcm_slot_tx = -1;
1088 ndsp->pcm_bank_rx = -1;
1089 ndsp->pcm_bank_tx = -1;
1090 ndsp->hfc_conf = -1; /* current conference number */
1091 /* set tone timer */
1092 timer_setup(&ndsp->tone.tl, dsp_tone_timeout, 0);
1093
1094 if (dtmfthreshold < 20 || dtmfthreshold > 500)
1095 dtmfthreshold = 200;
1096 ndsp->dtmf.treshold = dtmfthreshold * 10000;
1097
1098 /* init pipeline append to list */
1099 spin_lock_irqsave(&dsp_lock, flags);
1100 dsp_pipeline_init(&ndsp->pipeline);
1101 list_add_tail(&ndsp->list, &dsp_ilist);
1102 spin_unlock_irqrestore(&dsp_lock, flags);
1103
1104 return 0;
1105}
1106
1107
1108static struct Bprotocol DSP = {
1109 .Bprotocols = (1 << (ISDN_P_B_L2DSP & ISDN_P_B_MASK))
1110 | (1 << (ISDN_P_B_L2DSPHDLC & ISDN_P_B_MASK)),
1111 .name = "dsp",
1112 .create = dspcreate
1113};
1114
1115static int __init dsp_init(void)
1116{
1117 int err;
1118 int tics;
1119
1120 printk(KERN_INFO "DSP module %s\n", mISDN_dsp_revision);
1121
1122 dsp_options = options;
1123 dsp_debug = debug;
1124
1125 /* set packet size */
1126 dsp_poll = poll;
1127 if (dsp_poll) {
1128 if (dsp_poll > MAX_POLL) {
1129 printk(KERN_ERR "%s: Wrong poll value (%d), use %d "
1130 "maximum.\n", __func__, poll, MAX_POLL);
1131 err = -EINVAL;
1132 return err;
1133 }
1134 if (dsp_poll < 8) {
1135 printk(KERN_ERR "%s: Wrong poll value (%d), use 8 "
1136 "minimum.\n", __func__, dsp_poll);
1137 err = -EINVAL;
1138 return err;
1139 }
1140 dsp_tics = poll * HZ / 8000;
1141 if (dsp_tics * 8000 != poll * HZ) {
1142 printk(KERN_INFO "mISDN_dsp: Cannot clock every %d "
1143 "samples (0,125 ms). It is not a multiple of "
1144 "%d HZ.\n", poll, HZ);
1145 err = -EINVAL;
1146 return err;
1147 }
1148 } else {
1149 poll = 8;
1150 while (poll <= MAX_POLL) {
1151 tics = (poll * HZ) / 8000;
1152 if (tics * 8000 == poll * HZ) {
1153 dsp_tics = tics;
1154 dsp_poll = poll;
1155 if (poll >= 64)
1156 break;
1157 }
1158 poll++;
1159 }
1160 }
1161 if (dsp_poll == 0) {
1162 printk(KERN_INFO "mISDN_dsp: There is no multiple of kernel "
1163 "clock that equals exactly the duration of 8-256 "
1164 "samples. (Choose kernel clock speed like 100, 250, "
1165 "300, 1000)\n");
1166 err = -EINVAL;
1167 return err;
1168 }
1169 printk(KERN_INFO "mISDN_dsp: DSP clocks every %d samples. This equals "
1170 "%d jiffies.\n", dsp_poll, dsp_tics);
1171
1172 /* init conversion tables */
1173 dsp_audio_generate_law_tables();
1174 dsp_silence = (dsp_options & DSP_OPT_ULAW) ? 0xff : 0x2a;
1175 dsp_audio_law_to_s32 = (dsp_options & DSP_OPT_ULAW) ?
1176 dsp_audio_ulaw_to_s32 : dsp_audio_alaw_to_s32;
1177 dsp_audio_generate_s2law_table();
1178 dsp_audio_generate_seven();
1179 dsp_audio_generate_mix_table();
1180 if (dsp_options & DSP_OPT_ULAW)
1181 dsp_audio_generate_ulaw_samples();
1182 dsp_audio_generate_volume_changes();
1183
1184 err = dsp_pipeline_module_init();
1185 if (err) {
1186 printk(KERN_ERR "mISDN_dsp: Can't initialize pipeline, "
1187 "error(%d)\n", err);
1188 return err;
1189 }
1190
1191 err = mISDN_register_Bprotocol(&DSP);
1192 if (err) {
1193 printk(KERN_ERR "Can't register %s error(%d)\n", DSP.name, err);
1194 return err;
1195 }
1196
1197 /* set sample timer */
1198 timer_setup(&dsp_spl_tl, dsp_cmx_send, 0);
1199 dsp_spl_tl.expires = jiffies + dsp_tics;
1200 dsp_spl_jiffies = dsp_spl_tl.expires;
1201 add_timer(&dsp_spl_tl);
1202
1203 return 0;
1204}
1205
1206
1207static void __exit dsp_cleanup(void)
1208{
1209 mISDN_unregister_Bprotocol(&DSP);
1210
1211 del_timer_sync(&dsp_spl_tl);
1212
1213 if (!list_empty(&dsp_ilist)) {
1214 printk(KERN_ERR "mISDN_dsp: Audio DSP object inst list not "
1215 "empty.\n");
1216 }
1217 if (!list_empty(&conf_ilist)) {
1218 printk(KERN_ERR "mISDN_dsp: Conference list not empty. Not "
1219 "all memory freed.\n");
1220 }
1221
1222 dsp_pipeline_module_exit();
1223}
1224
1225module_init(dsp_init);
1226module_exit(dsp_cleanup);
1/*
2 * Author Andreas Eversberg (jolly@eversberg.eu)
3 * Based on source code structure by
4 * Karsten Keil (keil@isdn4linux.de)
5 *
6 * This file is (c) under GNU PUBLIC LICENSE
7 * For changes and modifications please read
8 * ../../../Documentation/isdn/mISDN.cert
9 *
10 * Thanks to Karsten Keil (great drivers)
11 * Cologne Chip (great chips)
12 *
13 * This module does:
14 * Real-time tone generation
15 * DTMF detection
16 * Real-time cross-connection and conferrence
17 * Compensate jitter due to system load and hardware fault.
18 * All features are done in kernel space and will be realized
19 * using hardware, if available and supported by chip set.
20 * Blowfish encryption/decryption
21 */
22
23/* STRUCTURE:
24 *
25 * The dsp module provides layer 2 for b-channels (64kbit). It provides
26 * transparent audio forwarding with special digital signal processing:
27 *
28 * - (1) generation of tones
29 * - (2) detection of dtmf tones
30 * - (3) crossconnecting and conferences (clocking)
31 * - (4) echo generation for delay test
32 * - (5) volume control
33 * - (6) disable receive data
34 * - (7) pipeline
35 * - (8) encryption/decryption
36 *
37 * Look:
38 * TX RX
39 * ------upper layer------
40 * | ^
41 * | |(6)
42 * v |
43 * +-----+-------------+-----+
44 * |(3)(4) |
45 * | CMX |
46 * | |
47 * | +-------------+
48 * | | ^
49 * | | |
50 * |+---------+| +----+----+
51 * ||(1) || |(2) |
52 * || || | |
53 * || Tones || | DTMF |
54 * || || | |
55 * || || | |
56 * |+----+----+| +----+----+
57 * +-----+-----+ ^
58 * | |
59 * v |
60 * +----+----+ +----+----+
61 * |(5) | |(5) |
62 * | | | |
63 * |TX Volume| |RX Volume|
64 * | | | |
65 * | | | |
66 * +----+----+ +----+----+
67 * | ^
68 * | |
69 * v |
70 * +----+-------------+----+
71 * |(7) |
72 * | |
73 * | Pipeline Processing |
74 * | |
75 * | |
76 * +----+-------------+----+
77 * | ^
78 * | |
79 * v |
80 * +----+----+ +----+----+
81 * |(8) | |(8) |
82 * | | | |
83 * | Encrypt | | Decrypt |
84 * | | | |
85 * | | | |
86 * +----+----+ +----+----+
87 * | ^
88 * | |
89 * v |
90 * ------card layer------
91 * TX RX
92 *
93 * Above you can see the logical data flow. If software is used to do the
94 * process, it is actually the real data flow. If hardware is used, data
95 * may not flow, but hardware commands to the card, to provide the data flow
96 * as shown.
97 *
98 * NOTE: The channel must be activated in order to make dsp work, even if
99 * no data flow to the upper layer is intended. Activation can be done
100 * after and before controlling the setting using PH_CONTROL requests.
101 *
102 * DTMF: Will be detected by hardware if possible. It is done before CMX
103 * processing.
104 *
105 * Tones: Will be generated via software if endless looped audio fifos are
106 * not supported by hardware. Tones will override all data from CMX.
107 * It is not required to join a conference to use tones at any time.
108 *
109 * CMX: Is transparent when not used. When it is used, it will do
110 * crossconnections and conferences via software if not possible through
111 * hardware. If hardware capability is available, hardware is used.
112 *
113 * Echo: Is generated by CMX and is used to check performance of hard and
114 * software CMX.
115 *
116 * The CMX has special functions for conferences with one, two and more
117 * members. It will allow different types of data flow. Receive and transmit
118 * data to/form upper layer may be swithed on/off individually without losing
119 * features of CMX, Tones and DTMF.
120 *
121 * Echo Cancellation: Sometimes we like to cancel echo from the interface.
122 * Note that a VoIP call may not have echo caused by the IP phone. The echo
123 * is generated by the telephone line connected to it. Because the delay
124 * is high, it becomes an echo. RESULT: Echo Cachelation is required if
125 * both echo AND delay is applied to an interface.
126 * Remember that software CMX always generates a more or less delay.
127 *
128 * If all used features can be realized in hardware, and if transmit and/or
129 * receive data ist disabled, the card may not send/receive any data at all.
130 * Not receiving is useful if only announcements are played. Not sending is
131 * useful if an answering machine records audio. Not sending and receiving is
132 * useful during most states of the call. If supported by hardware, tones
133 * will be played without cpu load. Small PBXs and NT-Mode applications will
134 * not need expensive hardware when processing calls.
135 *
136 *
137 * LOCKING:
138 *
139 * When data is received from upper or lower layer (card), the complete dsp
140 * module is locked by a global lock. This lock MUST lock irq, because it
141 * must lock timer events by DSP poll timer.
142 * When data is ready to be transmitted down, the data is queued and sent
143 * outside lock and timer event.
144 * PH_CONTROL must not change any settings, join or split conference members
145 * during process of data.
146 *
147 * HDLC:
148 *
149 * It works quite the same as transparent, except that HDLC data is forwarded
150 * to all other conference members if no hardware bridging is possible.
151 * Send data will be writte to sendq. Sendq will be sent if confirm is received.
152 * Conference cannot join, if one member is not hdlc.
153 *
154 */
155
156#include <linux/delay.h>
157#include <linux/gfp.h>
158#include <linux/mISDNif.h>
159#include <linux/mISDNdsp.h>
160#include <linux/module.h>
161#include <linux/vmalloc.h>
162#include "core.h"
163#include "dsp.h"
164
165static const char *mISDN_dsp_revision = "2.0";
166
167static int debug;
168static int options;
169static int poll;
170static int dtmfthreshold = 100;
171
172MODULE_AUTHOR("Andreas Eversberg");
173module_param(debug, uint, S_IRUGO | S_IWUSR);
174module_param(options, uint, S_IRUGO | S_IWUSR);
175module_param(poll, uint, S_IRUGO | S_IWUSR);
176module_param(dtmfthreshold, uint, S_IRUGO | S_IWUSR);
177MODULE_LICENSE("GPL");
178
179/*int spinnest = 0;*/
180
181spinlock_t dsp_lock; /* global dsp lock */
182struct list_head dsp_ilist;
183struct list_head conf_ilist;
184int dsp_debug;
185int dsp_options;
186int dsp_poll, dsp_tics;
187
188/* check if rx may be turned off or must be turned on */
189static void
190dsp_rx_off_member(struct dsp *dsp)
191{
192 struct mISDN_ctrl_req cq;
193 int rx_off = 1;
194
195 memset(&cq, 0, sizeof(cq));
196
197 if (!dsp->features_rx_off)
198 return;
199
200 /* not disabled */
201 if (!dsp->rx_disabled)
202 rx_off = 0;
203 /* software dtmf */
204 else if (dsp->dtmf.software)
205 rx_off = 0;
206 /* echo in software */
207 else if (dsp->echo.software)
208 rx_off = 0;
209 /* bridge in software */
210 else if (dsp->conf && dsp->conf->software)
211 rx_off = 0;
212 /* data is not required by user space and not required
213 * for echo dtmf detection, soft-echo, soft-bridging */
214
215 if (rx_off == dsp->rx_is_off)
216 return;
217
218 if (!dsp->ch.peer) {
219 if (dsp_debug & DEBUG_DSP_CORE)
220 printk(KERN_DEBUG "%s: no peer, no rx_off\n",
221 __func__);
222 return;
223 }
224 cq.op = MISDN_CTRL_RX_OFF;
225 cq.p1 = rx_off;
226 if (dsp->ch.peer->ctrl(dsp->ch.peer, CONTROL_CHANNEL, &cq)) {
227 printk(KERN_DEBUG "%s: 2nd CONTROL_CHANNEL failed\n",
228 __func__);
229 return;
230 }
231 dsp->rx_is_off = rx_off;
232 if (dsp_debug & DEBUG_DSP_CORE)
233 printk(KERN_DEBUG "%s: %s set rx_off = %d\n",
234 __func__, dsp->name, rx_off);
235}
236static void
237dsp_rx_off(struct dsp *dsp)
238{
239 struct dsp_conf_member *member;
240
241 if (dsp_options & DSP_OPT_NOHARDWARE)
242 return;
243
244 /* no conf */
245 if (!dsp->conf) {
246 dsp_rx_off_member(dsp);
247 return;
248 }
249 /* check all members in conf */
250 list_for_each_entry(member, &dsp->conf->mlist, list) {
251 dsp_rx_off_member(member->dsp);
252 }
253}
254
255/* enable "fill empty" feature */
256static void
257dsp_fill_empty(struct dsp *dsp)
258{
259 struct mISDN_ctrl_req cq;
260
261 memset(&cq, 0, sizeof(cq));
262
263 if (!dsp->ch.peer) {
264 if (dsp_debug & DEBUG_DSP_CORE)
265 printk(KERN_DEBUG "%s: no peer, no fill_empty\n",
266 __func__);
267 return;
268 }
269 cq.op = MISDN_CTRL_FILL_EMPTY;
270 cq.p1 = 1;
271 if (dsp->ch.peer->ctrl(dsp->ch.peer, CONTROL_CHANNEL, &cq)) {
272 printk(KERN_DEBUG "%s: CONTROL_CHANNEL failed\n",
273 __func__);
274 return;
275 }
276 if (dsp_debug & DEBUG_DSP_CORE)
277 printk(KERN_DEBUG "%s: %s set fill_empty = 1\n",
278 __func__, dsp->name);
279}
280
281static int
282dsp_control_req(struct dsp *dsp, struct mISDNhead *hh, struct sk_buff *skb)
283{
284 struct sk_buff *nskb;
285 int ret = 0;
286 int cont;
287 u8 *data;
288 int len;
289
290 if (skb->len < sizeof(int))
291 printk(KERN_ERR "%s: PH_CONTROL message too short\n", __func__);
292 cont = *((int *)skb->data);
293 len = skb->len - sizeof(int);
294 data = skb->data + sizeof(int);
295
296 switch (cont) {
297 case DTMF_TONE_START: /* turn on DTMF */
298 if (dsp->hdlc) {
299 ret = -EINVAL;
300 break;
301 }
302 if (dsp_debug & DEBUG_DSP_CORE)
303 printk(KERN_DEBUG "%s: start dtmf\n", __func__);
304 if (len == sizeof(int)) {
305 if (dsp_debug & DEBUG_DSP_CORE)
306 printk(KERN_NOTICE "changing DTMF Threshold "
307 "to %d\n", *((int *)data));
308 dsp->dtmf.treshold = (*(int *)data) * 10000;
309 }
310 dsp->dtmf.enable = 1;
311 /* init goertzel */
312 dsp_dtmf_goertzel_init(dsp);
313
314 /* check dtmf hardware */
315 dsp_dtmf_hardware(dsp);
316 dsp_rx_off(dsp);
317 break;
318 case DTMF_TONE_STOP: /* turn off DTMF */
319 if (dsp_debug & DEBUG_DSP_CORE)
320 printk(KERN_DEBUG "%s: stop dtmf\n", __func__);
321 dsp->dtmf.enable = 0;
322 dsp->dtmf.hardware = 0;
323 dsp->dtmf.software = 0;
324 break;
325 case DSP_CONF_JOIN: /* join / update conference */
326 if (len < sizeof(int)) {
327 ret = -EINVAL;
328 break;
329 }
330 if (*((u32 *)data) == 0)
331 goto conf_split;
332 if (dsp_debug & DEBUG_DSP_CORE)
333 printk(KERN_DEBUG "%s: join conference %d\n",
334 __func__, *((u32 *)data));
335 ret = dsp_cmx_conf(dsp, *((u32 *)data));
336 /* dsp_cmx_hardware will also be called here */
337 dsp_rx_off(dsp);
338 if (dsp_debug & DEBUG_DSP_CMX)
339 dsp_cmx_debug(dsp);
340 break;
341 case DSP_CONF_SPLIT: /* remove from conference */
342conf_split:
343 if (dsp_debug & DEBUG_DSP_CORE)
344 printk(KERN_DEBUG "%s: release conference\n", __func__);
345 ret = dsp_cmx_conf(dsp, 0);
346 /* dsp_cmx_hardware will also be called here */
347 if (dsp_debug & DEBUG_DSP_CMX)
348 dsp_cmx_debug(dsp);
349 dsp_rx_off(dsp);
350 break;
351 case DSP_TONE_PATT_ON: /* play tone */
352 if (dsp->hdlc) {
353 ret = -EINVAL;
354 break;
355 }
356 if (len < sizeof(int)) {
357 ret = -EINVAL;
358 break;
359 }
360 if (dsp_debug & DEBUG_DSP_CORE)
361 printk(KERN_DEBUG "%s: turn tone 0x%x on\n",
362 __func__, *((int *)skb->data));
363 ret = dsp_tone(dsp, *((int *)data));
364 if (!ret) {
365 dsp_cmx_hardware(dsp->conf, dsp);
366 dsp_rx_off(dsp);
367 }
368 if (!dsp->tone.tone)
369 goto tone_off;
370 break;
371 case DSP_TONE_PATT_OFF: /* stop tone */
372 if (dsp->hdlc) {
373 ret = -EINVAL;
374 break;
375 }
376 if (dsp_debug & DEBUG_DSP_CORE)
377 printk(KERN_DEBUG "%s: turn tone off\n", __func__);
378 dsp_tone(dsp, 0);
379 dsp_cmx_hardware(dsp->conf, dsp);
380 dsp_rx_off(dsp);
381 /* reset tx buffers (user space data) */
382tone_off:
383 dsp->rx_W = 0;
384 dsp->rx_R = 0;
385 break;
386 case DSP_VOL_CHANGE_TX: /* change volume */
387 if (dsp->hdlc) {
388 ret = -EINVAL;
389 break;
390 }
391 if (len < sizeof(int)) {
392 ret = -EINVAL;
393 break;
394 }
395 dsp->tx_volume = *((int *)data);
396 if (dsp_debug & DEBUG_DSP_CORE)
397 printk(KERN_DEBUG "%s: change tx vol to %d\n",
398 __func__, dsp->tx_volume);
399 dsp_cmx_hardware(dsp->conf, dsp);
400 dsp_dtmf_hardware(dsp);
401 dsp_rx_off(dsp);
402 break;
403 case DSP_VOL_CHANGE_RX: /* change volume */
404 if (dsp->hdlc) {
405 ret = -EINVAL;
406 break;
407 }
408 if (len < sizeof(int)) {
409 ret = -EINVAL;
410 break;
411 }
412 dsp->rx_volume = *((int *)data);
413 if (dsp_debug & DEBUG_DSP_CORE)
414 printk(KERN_DEBUG "%s: change rx vol to %d\n",
415 __func__, dsp->tx_volume);
416 dsp_cmx_hardware(dsp->conf, dsp);
417 dsp_dtmf_hardware(dsp);
418 dsp_rx_off(dsp);
419 break;
420 case DSP_ECHO_ON: /* enable echo */
421 dsp->echo.software = 1; /* soft echo */
422 if (dsp_debug & DEBUG_DSP_CORE)
423 printk(KERN_DEBUG "%s: enable cmx-echo\n", __func__);
424 dsp_cmx_hardware(dsp->conf, dsp);
425 dsp_rx_off(dsp);
426 if (dsp_debug & DEBUG_DSP_CMX)
427 dsp_cmx_debug(dsp);
428 break;
429 case DSP_ECHO_OFF: /* disable echo */
430 dsp->echo.software = 0;
431 dsp->echo.hardware = 0;
432 if (dsp_debug & DEBUG_DSP_CORE)
433 printk(KERN_DEBUG "%s: disable cmx-echo\n", __func__);
434 dsp_cmx_hardware(dsp->conf, dsp);
435 dsp_rx_off(dsp);
436 if (dsp_debug & DEBUG_DSP_CMX)
437 dsp_cmx_debug(dsp);
438 break;
439 case DSP_RECEIVE_ON: /* enable receive to user space */
440 if (dsp_debug & DEBUG_DSP_CORE)
441 printk(KERN_DEBUG "%s: enable receive to user "
442 "space\n", __func__);
443 dsp->rx_disabled = 0;
444 dsp_rx_off(dsp);
445 break;
446 case DSP_RECEIVE_OFF: /* disable receive to user space */
447 if (dsp_debug & DEBUG_DSP_CORE)
448 printk(KERN_DEBUG "%s: disable receive to "
449 "user space\n", __func__);
450 dsp->rx_disabled = 1;
451 dsp_rx_off(dsp);
452 break;
453 case DSP_MIX_ON: /* enable mixing of tx data */
454 if (dsp->hdlc) {
455 ret = -EINVAL;
456 break;
457 }
458 if (dsp_debug & DEBUG_DSP_CORE)
459 printk(KERN_DEBUG "%s: enable mixing of "
460 "tx-data with conf mebers\n", __func__);
461 dsp->tx_mix = 1;
462 dsp_cmx_hardware(dsp->conf, dsp);
463 dsp_rx_off(dsp);
464 if (dsp_debug & DEBUG_DSP_CMX)
465 dsp_cmx_debug(dsp);
466 break;
467 case DSP_MIX_OFF: /* disable mixing of tx data */
468 if (dsp->hdlc) {
469 ret = -EINVAL;
470 break;
471 }
472 if (dsp_debug & DEBUG_DSP_CORE)
473 printk(KERN_DEBUG "%s: disable mixing of "
474 "tx-data with conf mebers\n", __func__);
475 dsp->tx_mix = 0;
476 dsp_cmx_hardware(dsp->conf, dsp);
477 dsp_rx_off(dsp);
478 if (dsp_debug & DEBUG_DSP_CMX)
479 dsp_cmx_debug(dsp);
480 break;
481 case DSP_TXDATA_ON: /* enable txdata */
482 dsp->tx_data = 1;
483 if (dsp_debug & DEBUG_DSP_CORE)
484 printk(KERN_DEBUG "%s: enable tx-data\n", __func__);
485 dsp_cmx_hardware(dsp->conf, dsp);
486 dsp_rx_off(dsp);
487 if (dsp_debug & DEBUG_DSP_CMX)
488 dsp_cmx_debug(dsp);
489 break;
490 case DSP_TXDATA_OFF: /* disable txdata */
491 dsp->tx_data = 0;
492 if (dsp_debug & DEBUG_DSP_CORE)
493 printk(KERN_DEBUG "%s: disable tx-data\n", __func__);
494 dsp_cmx_hardware(dsp->conf, dsp);
495 dsp_rx_off(dsp);
496 if (dsp_debug & DEBUG_DSP_CMX)
497 dsp_cmx_debug(dsp);
498 break;
499 case DSP_DELAY: /* use delay algorithm instead of dynamic
500 jitter algorithm */
501 if (dsp->hdlc) {
502 ret = -EINVAL;
503 break;
504 }
505 if (len < sizeof(int)) {
506 ret = -EINVAL;
507 break;
508 }
509 dsp->cmx_delay = (*((int *)data)) << 3;
510 /* milliseconds to samples */
511 if (dsp->cmx_delay >= (CMX_BUFF_HALF>>1))
512 /* clip to half of maximum usable buffer
513 (half of half buffer) */
514 dsp->cmx_delay = (CMX_BUFF_HALF>>1) - 1;
515 if (dsp_debug & DEBUG_DSP_CORE)
516 printk(KERN_DEBUG "%s: use delay algorithm to "
517 "compensate jitter (%d samples)\n",
518 __func__, dsp->cmx_delay);
519 break;
520 case DSP_JITTER: /* use dynamic jitter algorithm instead of
521 delay algorithm */
522 if (dsp->hdlc) {
523 ret = -EINVAL;
524 break;
525 }
526 dsp->cmx_delay = 0;
527 if (dsp_debug & DEBUG_DSP_CORE)
528 printk(KERN_DEBUG "%s: use jitter algorithm to "
529 "compensate jitter\n", __func__);
530 break;
531 case DSP_TX_DEJITTER: /* use dynamic jitter algorithm for tx-buffer */
532 if (dsp->hdlc) {
533 ret = -EINVAL;
534 break;
535 }
536 dsp->tx_dejitter = 1;
537 if (dsp_debug & DEBUG_DSP_CORE)
538 printk(KERN_DEBUG "%s: use dejitter on TX "
539 "buffer\n", __func__);
540 break;
541 case DSP_TX_DEJ_OFF: /* use tx-buffer without dejittering*/
542 if (dsp->hdlc) {
543 ret = -EINVAL;
544 break;
545 }
546 dsp->tx_dejitter = 0;
547 if (dsp_debug & DEBUG_DSP_CORE)
548 printk(KERN_DEBUG "%s: use TX buffer without "
549 "dejittering\n", __func__);
550 break;
551 case DSP_PIPELINE_CFG:
552 if (dsp->hdlc) {
553 ret = -EINVAL;
554 break;
555 }
556 if (len > 0 && ((char *)data)[len - 1]) {
557 printk(KERN_DEBUG "%s: pipeline config string "
558 "is not NULL terminated!\n", __func__);
559 ret = -EINVAL;
560 } else {
561 dsp->pipeline.inuse = 1;
562 dsp_cmx_hardware(dsp->conf, dsp);
563 ret = dsp_pipeline_build(&dsp->pipeline,
564 len > 0 ? data : NULL);
565 dsp_cmx_hardware(dsp->conf, dsp);
566 dsp_rx_off(dsp);
567 }
568 break;
569 case DSP_BF_ENABLE_KEY: /* turn blowfish on */
570 if (dsp->hdlc) {
571 ret = -EINVAL;
572 break;
573 }
574 if (len < 4 || len > 56) {
575 ret = -EINVAL;
576 break;
577 }
578 if (dsp_debug & DEBUG_DSP_CORE)
579 printk(KERN_DEBUG "%s: turn blowfish on (key "
580 "not shown)\n", __func__);
581 ret = dsp_bf_init(dsp, (u8 *)data, len);
582 /* set new cont */
583 if (!ret)
584 cont = DSP_BF_ACCEPT;
585 else
586 cont = DSP_BF_REJECT;
587 /* send indication if it worked to set it */
588 nskb = _alloc_mISDN_skb(PH_CONTROL_IND, MISDN_ID_ANY,
589 sizeof(int), &cont, GFP_ATOMIC);
590 if (nskb) {
591 if (dsp->up) {
592 if (dsp->up->send(dsp->up, nskb))
593 dev_kfree_skb(nskb);
594 } else
595 dev_kfree_skb(nskb);
596 }
597 if (!ret) {
598 dsp_cmx_hardware(dsp->conf, dsp);
599 dsp_dtmf_hardware(dsp);
600 dsp_rx_off(dsp);
601 }
602 break;
603 case DSP_BF_DISABLE: /* turn blowfish off */
604 if (dsp->hdlc) {
605 ret = -EINVAL;
606 break;
607 }
608 if (dsp_debug & DEBUG_DSP_CORE)
609 printk(KERN_DEBUG "%s: turn blowfish off\n", __func__);
610 dsp_bf_cleanup(dsp);
611 dsp_cmx_hardware(dsp->conf, dsp);
612 dsp_dtmf_hardware(dsp);
613 dsp_rx_off(dsp);
614 break;
615 default:
616 if (dsp_debug & DEBUG_DSP_CORE)
617 printk(KERN_DEBUG "%s: ctrl req %x unhandled\n",
618 __func__, cont);
619 ret = -EINVAL;
620 }
621 return ret;
622}
623
624static void
625get_features(struct mISDNchannel *ch)
626{
627 struct dsp *dsp = container_of(ch, struct dsp, ch);
628 struct mISDN_ctrl_req cq;
629
630 if (!ch->peer) {
631 if (dsp_debug & DEBUG_DSP_CORE)
632 printk(KERN_DEBUG "%s: no peer, no features\n",
633 __func__);
634 return;
635 }
636 memset(&cq, 0, sizeof(cq));
637 cq.op = MISDN_CTRL_GETOP;
638 if (ch->peer->ctrl(ch->peer, CONTROL_CHANNEL, &cq) < 0) {
639 printk(KERN_DEBUG "%s: CONTROL_CHANNEL failed\n",
640 __func__);
641 return;
642 }
643 if (cq.op & MISDN_CTRL_RX_OFF)
644 dsp->features_rx_off = 1;
645 if (cq.op & MISDN_CTRL_FILL_EMPTY)
646 dsp->features_fill_empty = 1;
647 if (dsp_options & DSP_OPT_NOHARDWARE)
648 return;
649 if ((cq.op & MISDN_CTRL_HW_FEATURES_OP)) {
650 cq.op = MISDN_CTRL_HW_FEATURES;
651 *((u_long *)&cq.p1) = (u_long)&dsp->features;
652 if (ch->peer->ctrl(ch->peer, CONTROL_CHANNEL, &cq)) {
653 printk(KERN_DEBUG "%s: 2nd CONTROL_CHANNEL failed\n",
654 __func__);
655 }
656 } else
657 if (dsp_debug & DEBUG_DSP_CORE)
658 printk(KERN_DEBUG "%s: features not supported for %s\n",
659 __func__, dsp->name);
660}
661
662static int
663dsp_function(struct mISDNchannel *ch, struct sk_buff *skb)
664{
665 struct dsp *dsp = container_of(ch, struct dsp, ch);
666 struct mISDNhead *hh;
667 int ret = 0;
668 u8 *digits = NULL;
669 u_long flags;
670
671 hh = mISDN_HEAD_P(skb);
672 switch (hh->prim) {
673 /* FROM DOWN */
674 case (PH_DATA_CNF):
675 dsp->data_pending = 0;
676 /* trigger next hdlc frame, if any */
677 if (dsp->hdlc) {
678 spin_lock_irqsave(&dsp_lock, flags);
679 if (dsp->b_active)
680 schedule_work(&dsp->workq);
681 spin_unlock_irqrestore(&dsp_lock, flags);
682 }
683 break;
684 case (PH_DATA_IND):
685 case (DL_DATA_IND):
686 if (skb->len < 1) {
687 ret = -EINVAL;
688 break;
689 }
690 if (dsp->rx_is_off) {
691 if (dsp_debug & DEBUG_DSP_CORE)
692 printk(KERN_DEBUG "%s: rx-data during rx_off"
693 " for %s\n",
694 __func__, dsp->name);
695 }
696 if (dsp->hdlc) {
697 /* hdlc */
698 spin_lock_irqsave(&dsp_lock, flags);
699 dsp_cmx_hdlc(dsp, skb);
700 spin_unlock_irqrestore(&dsp_lock, flags);
701 if (dsp->rx_disabled) {
702 /* if receive is not allowed */
703 break;
704 }
705 hh->prim = DL_DATA_IND;
706 if (dsp->up)
707 return dsp->up->send(dsp->up, skb);
708 break;
709 }
710
711 spin_lock_irqsave(&dsp_lock, flags);
712
713 /* decrypt if enabled */
714 if (dsp->bf_enable)
715 dsp_bf_decrypt(dsp, skb->data, skb->len);
716 /* pipeline */
717 if (dsp->pipeline.inuse)
718 dsp_pipeline_process_rx(&dsp->pipeline, skb->data,
719 skb->len, hh->id);
720 /* change volume if requested */
721 if (dsp->rx_volume)
722 dsp_change_volume(skb, dsp->rx_volume);
723 /* check if dtmf soft decoding is turned on */
724 if (dsp->dtmf.software) {
725 digits = dsp_dtmf_goertzel_decode(dsp, skb->data,
726 skb->len, (dsp_options&DSP_OPT_ULAW) ? 1 : 0);
727 }
728 /* we need to process receive data if software */
729 if (dsp->conf && dsp->conf->software) {
730 /* process data from card at cmx */
731 dsp_cmx_receive(dsp, skb);
732 }
733
734 spin_unlock_irqrestore(&dsp_lock, flags);
735
736 /* send dtmf result, if any */
737 if (digits) {
738 while (*digits) {
739 int k;
740 struct sk_buff *nskb;
741 if (dsp_debug & DEBUG_DSP_DTMF)
742 printk(KERN_DEBUG "%s: digit"
743 "(%c) to layer %s\n",
744 __func__, *digits, dsp->name);
745 k = *digits | DTMF_TONE_VAL;
746 nskb = _alloc_mISDN_skb(PH_CONTROL_IND,
747 MISDN_ID_ANY, sizeof(int), &k,
748 GFP_ATOMIC);
749 if (nskb) {
750 if (dsp->up) {
751 if (dsp->up->send(
752 dsp->up, nskb))
753 dev_kfree_skb(nskb);
754 } else
755 dev_kfree_skb(nskb);
756 }
757 digits++;
758 }
759 }
760 if (dsp->rx_disabled) {
761 /* if receive is not allowed */
762 break;
763 }
764 hh->prim = DL_DATA_IND;
765 if (dsp->up)
766 return dsp->up->send(dsp->up, skb);
767 break;
768 case (PH_CONTROL_IND):
769 if (dsp_debug & DEBUG_DSP_DTMFCOEFF)
770 printk(KERN_DEBUG "%s: PH_CONTROL INDICATION "
771 "received: %x (len %d) %s\n", __func__,
772 hh->id, skb->len, dsp->name);
773 switch (hh->id) {
774 case (DTMF_HFC_COEF): /* getting coefficients */
775 if (!dsp->dtmf.hardware) {
776 if (dsp_debug & DEBUG_DSP_DTMFCOEFF)
777 printk(KERN_DEBUG "%s: ignoring DTMF "
778 "coefficients from HFC\n",
779 __func__);
780 break;
781 }
782 digits = dsp_dtmf_goertzel_decode(dsp, skb->data,
783 skb->len, 2);
784 while (*digits) {
785 int k;
786 struct sk_buff *nskb;
787 if (dsp_debug & DEBUG_DSP_DTMF)
788 printk(KERN_DEBUG "%s: digit"
789 "(%c) to layer %s\n",
790 __func__, *digits, dsp->name);
791 k = *digits | DTMF_TONE_VAL;
792 nskb = _alloc_mISDN_skb(PH_CONTROL_IND,
793 MISDN_ID_ANY, sizeof(int), &k,
794 GFP_ATOMIC);
795 if (nskb) {
796 if (dsp->up) {
797 if (dsp->up->send(
798 dsp->up, nskb))
799 dev_kfree_skb(nskb);
800 } else
801 dev_kfree_skb(nskb);
802 }
803 digits++;
804 }
805 break;
806 case (HFC_VOL_CHANGE_TX): /* change volume */
807 if (skb->len != sizeof(int)) {
808 ret = -EINVAL;
809 break;
810 }
811 spin_lock_irqsave(&dsp_lock, flags);
812 dsp->tx_volume = *((int *)skb->data);
813 if (dsp_debug & DEBUG_DSP_CORE)
814 printk(KERN_DEBUG "%s: change tx volume to "
815 "%d\n", __func__, dsp->tx_volume);
816 dsp_cmx_hardware(dsp->conf, dsp);
817 dsp_dtmf_hardware(dsp);
818 dsp_rx_off(dsp);
819 spin_unlock_irqrestore(&dsp_lock, flags);
820 break;
821 default:
822 if (dsp_debug & DEBUG_DSP_CORE)
823 printk(KERN_DEBUG "%s: ctrl ind %x unhandled "
824 "%s\n", __func__, hh->id, dsp->name);
825 ret = -EINVAL;
826 }
827 break;
828 case (PH_ACTIVATE_IND):
829 case (PH_ACTIVATE_CNF):
830 if (dsp_debug & DEBUG_DSP_CORE)
831 printk(KERN_DEBUG "%s: b_channel is now active %s\n",
832 __func__, dsp->name);
833 /* bchannel now active */
834 spin_lock_irqsave(&dsp_lock, flags);
835 dsp->b_active = 1;
836 dsp->data_pending = 0;
837 dsp->rx_init = 1;
838 /* rx_W and rx_R will be adjusted on first frame */
839 dsp->rx_W = 0;
840 dsp->rx_R = 0;
841 memset(dsp->rx_buff, 0, sizeof(dsp->rx_buff));
842 dsp_cmx_hardware(dsp->conf, dsp);
843 dsp_dtmf_hardware(dsp);
844 dsp_rx_off(dsp);
845 spin_unlock_irqrestore(&dsp_lock, flags);
846 if (dsp_debug & DEBUG_DSP_CORE)
847 printk(KERN_DEBUG "%s: done with activation, sending "
848 "confirm to user space. %s\n", __func__,
849 dsp->name);
850 /* send activation to upper layer */
851 hh->prim = DL_ESTABLISH_CNF;
852 if (dsp->up)
853 return dsp->up->send(dsp->up, skb);
854 break;
855 case (PH_DEACTIVATE_IND):
856 case (PH_DEACTIVATE_CNF):
857 if (dsp_debug & DEBUG_DSP_CORE)
858 printk(KERN_DEBUG "%s: b_channel is now inactive %s\n",
859 __func__, dsp->name);
860 /* bchannel now inactive */
861 spin_lock_irqsave(&dsp_lock, flags);
862 dsp->b_active = 0;
863 dsp->data_pending = 0;
864 dsp_cmx_hardware(dsp->conf, dsp);
865 dsp_rx_off(dsp);
866 spin_unlock_irqrestore(&dsp_lock, flags);
867 hh->prim = DL_RELEASE_CNF;
868 if (dsp->up)
869 return dsp->up->send(dsp->up, skb);
870 break;
871 /* FROM UP */
872 case (DL_DATA_REQ):
873 case (PH_DATA_REQ):
874 if (skb->len < 1) {
875 ret = -EINVAL;
876 break;
877 }
878 if (dsp->hdlc) {
879 /* hdlc */
880 if (!dsp->b_active) {
881 ret = -EIO;
882 break;
883 }
884 hh->prim = PH_DATA_REQ;
885 spin_lock_irqsave(&dsp_lock, flags);
886 skb_queue_tail(&dsp->sendq, skb);
887 schedule_work(&dsp->workq);
888 spin_unlock_irqrestore(&dsp_lock, flags);
889 return 0;
890 }
891 /* send data to tx-buffer (if no tone is played) */
892 if (!dsp->tone.tone) {
893 spin_lock_irqsave(&dsp_lock, flags);
894 dsp_cmx_transmit(dsp, skb);
895 spin_unlock_irqrestore(&dsp_lock, flags);
896 }
897 break;
898 case (PH_CONTROL_REQ):
899 spin_lock_irqsave(&dsp_lock, flags);
900 ret = dsp_control_req(dsp, hh, skb);
901 spin_unlock_irqrestore(&dsp_lock, flags);
902 break;
903 case (DL_ESTABLISH_REQ):
904 case (PH_ACTIVATE_REQ):
905 if (dsp_debug & DEBUG_DSP_CORE)
906 printk(KERN_DEBUG "%s: activating b_channel %s\n",
907 __func__, dsp->name);
908 if (dsp->dtmf.hardware || dsp->dtmf.software)
909 dsp_dtmf_goertzel_init(dsp);
910 get_features(ch);
911 /* enable fill_empty feature */
912 if (dsp->features_fill_empty)
913 dsp_fill_empty(dsp);
914 /* send ph_activate */
915 hh->prim = PH_ACTIVATE_REQ;
916 if (ch->peer)
917 return ch->recv(ch->peer, skb);
918 break;
919 case (DL_RELEASE_REQ):
920 case (PH_DEACTIVATE_REQ):
921 if (dsp_debug & DEBUG_DSP_CORE)
922 printk(KERN_DEBUG "%s: releasing b_channel %s\n",
923 __func__, dsp->name);
924 spin_lock_irqsave(&dsp_lock, flags);
925 dsp->tone.tone = 0;
926 dsp->tone.hardware = 0;
927 dsp->tone.software = 0;
928 if (timer_pending(&dsp->tone.tl))
929 del_timer(&dsp->tone.tl);
930 if (dsp->conf)
931 dsp_cmx_conf(dsp, 0); /* dsp_cmx_hardware will also be
932 called here */
933 skb_queue_purge(&dsp->sendq);
934 spin_unlock_irqrestore(&dsp_lock, flags);
935 hh->prim = PH_DEACTIVATE_REQ;
936 if (ch->peer)
937 return ch->recv(ch->peer, skb);
938 break;
939 default:
940 if (dsp_debug & DEBUG_DSP_CORE)
941 printk(KERN_DEBUG "%s: msg %x unhandled %s\n",
942 __func__, hh->prim, dsp->name);
943 ret = -EINVAL;
944 }
945 if (!ret)
946 dev_kfree_skb(skb);
947 return ret;
948}
949
950static int
951dsp_ctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
952{
953 struct dsp *dsp = container_of(ch, struct dsp, ch);
954 u_long flags;
955 int err = 0;
956
957 if (debug & DEBUG_DSP_CTRL)
958 printk(KERN_DEBUG "%s:(%x)\n", __func__, cmd);
959
960 switch (cmd) {
961 case OPEN_CHANNEL:
962 break;
963 case CLOSE_CHANNEL:
964 if (dsp->ch.peer)
965 dsp->ch.peer->ctrl(dsp->ch.peer, CLOSE_CHANNEL, NULL);
966
967 /* wait until workqueue has finished,
968 * must lock here, or we may hit send-process currently
969 * queueing. */
970 spin_lock_irqsave(&dsp_lock, flags);
971 dsp->b_active = 0;
972 spin_unlock_irqrestore(&dsp_lock, flags);
973 /* MUST not be locked, because it waits until queue is done. */
974 cancel_work_sync(&dsp->workq);
975 spin_lock_irqsave(&dsp_lock, flags);
976 if (timer_pending(&dsp->tone.tl))
977 del_timer(&dsp->tone.tl);
978 skb_queue_purge(&dsp->sendq);
979 if (dsp_debug & DEBUG_DSP_CTRL)
980 printk(KERN_DEBUG "%s: releasing member %s\n",
981 __func__, dsp->name);
982 dsp->b_active = 0;
983 dsp_cmx_conf(dsp, 0); /* dsp_cmx_hardware will also be called
984 here */
985 dsp_pipeline_destroy(&dsp->pipeline);
986
987 if (dsp_debug & DEBUG_DSP_CTRL)
988 printk(KERN_DEBUG "%s: remove & destroy object %s\n",
989 __func__, dsp->name);
990 list_del(&dsp->list);
991 spin_unlock_irqrestore(&dsp_lock, flags);
992
993 if (dsp_debug & DEBUG_DSP_CTRL)
994 printk(KERN_DEBUG "%s: dsp instance released\n",
995 __func__);
996 vfree(dsp);
997 module_put(THIS_MODULE);
998 break;
999 }
1000 return err;
1001}
1002
1003static void
1004dsp_send_bh(struct work_struct *work)
1005{
1006 struct dsp *dsp = container_of(work, struct dsp, workq);
1007 struct sk_buff *skb;
1008 struct mISDNhead *hh;
1009
1010 if (dsp->hdlc && dsp->data_pending)
1011 return; /* wait until data has been acknowledged */
1012
1013 /* send queued data */
1014 while ((skb = skb_dequeue(&dsp->sendq))) {
1015 /* in locked date, we must have still data in queue */
1016 if (dsp->data_pending) {
1017 if (dsp_debug & DEBUG_DSP_CORE)
1018 printk(KERN_DEBUG "%s: fifo full %s, this is "
1019 "no bug!\n", __func__, dsp->name);
1020 /* flush transparent data, if not acked */
1021 dev_kfree_skb(skb);
1022 continue;
1023 }
1024 hh = mISDN_HEAD_P(skb);
1025 if (hh->prim == DL_DATA_REQ) {
1026 /* send packet up */
1027 if (dsp->up) {
1028 if (dsp->up->send(dsp->up, skb))
1029 dev_kfree_skb(skb);
1030 } else
1031 dev_kfree_skb(skb);
1032 } else {
1033 /* send packet down */
1034 if (dsp->ch.peer) {
1035 dsp->data_pending = 1;
1036 if (dsp->ch.recv(dsp->ch.peer, skb)) {
1037 dev_kfree_skb(skb);
1038 dsp->data_pending = 0;
1039 }
1040 } else
1041 dev_kfree_skb(skb);
1042 }
1043 }
1044}
1045
1046static int
1047dspcreate(struct channel_req *crq)
1048{
1049 struct dsp *ndsp;
1050 u_long flags;
1051
1052 if (crq->protocol != ISDN_P_B_L2DSP
1053 && crq->protocol != ISDN_P_B_L2DSPHDLC)
1054 return -EPROTONOSUPPORT;
1055 ndsp = vmalloc(sizeof(struct dsp));
1056 if (!ndsp) {
1057 printk(KERN_ERR "%s: vmalloc struct dsp failed\n", __func__);
1058 return -ENOMEM;
1059 }
1060 memset(ndsp, 0, sizeof(struct dsp));
1061 if (dsp_debug & DEBUG_DSP_CTRL)
1062 printk(KERN_DEBUG "%s: creating new dsp instance\n", __func__);
1063
1064 /* default enabled */
1065 INIT_WORK(&ndsp->workq, (void *)dsp_send_bh);
1066 skb_queue_head_init(&ndsp->sendq);
1067 ndsp->ch.send = dsp_function;
1068 ndsp->ch.ctrl = dsp_ctrl;
1069 ndsp->up = crq->ch;
1070 crq->ch = &ndsp->ch;
1071 if (crq->protocol == ISDN_P_B_L2DSP) {
1072 crq->protocol = ISDN_P_B_RAW;
1073 ndsp->hdlc = 0;
1074 } else {
1075 crq->protocol = ISDN_P_B_HDLC;
1076 ndsp->hdlc = 1;
1077 }
1078 if (!try_module_get(THIS_MODULE))
1079 printk(KERN_WARNING "%s:cannot get module\n",
1080 __func__);
1081
1082 sprintf(ndsp->name, "DSP_C%x(0x%p)",
1083 ndsp->up->st->dev->id + 1, ndsp);
1084 /* set frame size to start */
1085 ndsp->features.hfc_id = -1; /* current PCM id */
1086 ndsp->features.pcm_id = -1; /* current PCM id */
1087 ndsp->pcm_slot_rx = -1; /* current CPM slot */
1088 ndsp->pcm_slot_tx = -1;
1089 ndsp->pcm_bank_rx = -1;
1090 ndsp->pcm_bank_tx = -1;
1091 ndsp->hfc_conf = -1; /* current conference number */
1092 /* set tone timer */
1093 ndsp->tone.tl.function = (void *)dsp_tone_timeout;
1094 ndsp->tone.tl.data = (long) ndsp;
1095 init_timer(&ndsp->tone.tl);
1096
1097 if (dtmfthreshold < 20 || dtmfthreshold > 500)
1098 dtmfthreshold = 200;
1099 ndsp->dtmf.treshold = dtmfthreshold*10000;
1100
1101 /* init pipeline append to list */
1102 spin_lock_irqsave(&dsp_lock, flags);
1103 dsp_pipeline_init(&ndsp->pipeline);
1104 list_add_tail(&ndsp->list, &dsp_ilist);
1105 spin_unlock_irqrestore(&dsp_lock, flags);
1106
1107 return 0;
1108}
1109
1110
1111static struct Bprotocol DSP = {
1112 .Bprotocols = (1 << (ISDN_P_B_L2DSP & ISDN_P_B_MASK))
1113 | (1 << (ISDN_P_B_L2DSPHDLC & ISDN_P_B_MASK)),
1114 .name = "dsp",
1115 .create = dspcreate
1116};
1117
1118static int __init dsp_init(void)
1119{
1120 int err;
1121 int tics;
1122
1123 printk(KERN_INFO "DSP modul %s\n", mISDN_dsp_revision);
1124
1125 dsp_options = options;
1126 dsp_debug = debug;
1127
1128 /* set packet size */
1129 dsp_poll = poll;
1130 if (dsp_poll) {
1131 if (dsp_poll > MAX_POLL) {
1132 printk(KERN_ERR "%s: Wrong poll value (%d), use %d "
1133 "maximum.\n", __func__, poll, MAX_POLL);
1134 err = -EINVAL;
1135 return err;
1136 }
1137 if (dsp_poll < 8) {
1138 printk(KERN_ERR "%s: Wrong poll value (%d), use 8 "
1139 "minimum.\n", __func__, dsp_poll);
1140 err = -EINVAL;
1141 return err;
1142 }
1143 dsp_tics = poll * HZ / 8000;
1144 if (dsp_tics * 8000 != poll * HZ) {
1145 printk(KERN_INFO "mISDN_dsp: Cannot clock every %d "
1146 "samples (0,125 ms). It is not a multiple of "
1147 "%d HZ.\n", poll, HZ);
1148 err = -EINVAL;
1149 return err;
1150 }
1151 } else {
1152 poll = 8;
1153 while (poll <= MAX_POLL) {
1154 tics = (poll * HZ) / 8000;
1155 if (tics * 8000 == poll * HZ) {
1156 dsp_tics = tics;
1157 dsp_poll = poll;
1158 if (poll >= 64)
1159 break;
1160 }
1161 poll++;
1162 }
1163 }
1164 if (dsp_poll == 0) {
1165 printk(KERN_INFO "mISDN_dsp: There is no multiple of kernel "
1166 "clock that equals exactly the duration of 8-256 "
1167 "samples. (Choose kernel clock speed like 100, 250, "
1168 "300, 1000)\n");
1169 err = -EINVAL;
1170 return err;
1171 }
1172 printk(KERN_INFO "mISDN_dsp: DSP clocks every %d samples. This equals "
1173 "%d jiffies.\n", dsp_poll, dsp_tics);
1174
1175 spin_lock_init(&dsp_lock);
1176 INIT_LIST_HEAD(&dsp_ilist);
1177 INIT_LIST_HEAD(&conf_ilist);
1178
1179 /* init conversion tables */
1180 dsp_audio_generate_law_tables();
1181 dsp_silence = (dsp_options&DSP_OPT_ULAW) ? 0xff : 0x2a;
1182 dsp_audio_law_to_s32 = (dsp_options&DSP_OPT_ULAW) ?
1183 dsp_audio_ulaw_to_s32 : dsp_audio_alaw_to_s32;
1184 dsp_audio_generate_s2law_table();
1185 dsp_audio_generate_seven();
1186 dsp_audio_generate_mix_table();
1187 if (dsp_options & DSP_OPT_ULAW)
1188 dsp_audio_generate_ulaw_samples();
1189 dsp_audio_generate_volume_changes();
1190
1191 err = dsp_pipeline_module_init();
1192 if (err) {
1193 printk(KERN_ERR "mISDN_dsp: Can't initialize pipeline, "
1194 "error(%d)\n", err);
1195 return err;
1196 }
1197
1198 err = mISDN_register_Bprotocol(&DSP);
1199 if (err) {
1200 printk(KERN_ERR "Can't register %s error(%d)\n", DSP.name, err);
1201 return err;
1202 }
1203
1204 /* set sample timer */
1205 dsp_spl_tl.function = (void *)dsp_cmx_send;
1206 dsp_spl_tl.data = 0;
1207 init_timer(&dsp_spl_tl);
1208 dsp_spl_tl.expires = jiffies + dsp_tics;
1209 dsp_spl_jiffies = dsp_spl_tl.expires;
1210 add_timer(&dsp_spl_tl);
1211
1212 return 0;
1213}
1214
1215
1216static void __exit dsp_cleanup(void)
1217{
1218 mISDN_unregister_Bprotocol(&DSP);
1219
1220 if (timer_pending(&dsp_spl_tl))
1221 del_timer(&dsp_spl_tl);
1222
1223 if (!list_empty(&dsp_ilist)) {
1224 printk(KERN_ERR "mISDN_dsp: Audio DSP object inst list not "
1225 "empty.\n");
1226 }
1227 if (!list_empty(&conf_ilist)) {
1228 printk(KERN_ERR "mISDN_dsp: Conference list not empty. Not "
1229 "all memory freed.\n");
1230 }
1231
1232 dsp_pipeline_module_exit();
1233}
1234
1235module_init(dsp_init);
1236module_exit(dsp_cleanup);
1237