mISDN: Added layer-1-hold feature
[linux-2.6-block.git] / drivers / isdn / mISDN / dsp_core.c
CommitLineData
960366cf
KK
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 performane 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 loosing
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 usefull if only announcements are played. Not sending is
131 * usefull if an answering machine records audio. Not sending and receiving is
132 * usefull 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/mISDNif.h>
158#include <linux/mISDNdsp.h>
159#include <linux/module.h>
160#include <linux/vmalloc.h>
161#include "core.h"
162#include "dsp.h"
163
5b834354 164static const char *mISDN_dsp_revision = "2.0";
960366cf
KK
165
166static int debug;
167static int options;
168static int poll;
169static int dtmfthreshold = 100;
170
171MODULE_AUTHOR("Andreas Eversberg");
172module_param(debug, uint, S_IRUGO | S_IWUSR);
173module_param(options, uint, S_IRUGO | S_IWUSR);
174module_param(poll, uint, S_IRUGO | S_IWUSR);
175module_param(dtmfthreshold, uint, S_IRUGO | S_IWUSR);
176MODULE_LICENSE("GPL");
177
178/*int spinnest = 0;*/
179
180spinlock_t dsp_lock; /* global dsp lock */
181struct list_head dsp_ilist;
182struct list_head conf_ilist;
183int dsp_debug;
184int dsp_options;
185int dsp_poll, dsp_tics;
186
187/* check if rx may be turned off or must be turned on */
188static void
189dsp_rx_off_member(struct dsp *dsp)
190{
191 struct mISDN_ctrl_req cq;
192 int rx_off = 1;
193
8dd2f36f
AE
194 memset(&cq, 0, sizeof(cq));
195
960366cf
KK
196 if (!dsp->features_rx_off)
197 return;
198
199 /* not disabled */
200 if (!dsp->rx_disabled)
201 rx_off = 0;
202 /* software dtmf */
203 else if (dsp->dtmf.software)
204 rx_off = 0;
205 /* echo in software */
206 else if (dsp->echo && dsp->pcm_slot_tx < 0)
207 rx_off = 0;
208 /* bridge in software */
209 else if (dsp->conf) {
210 if (dsp->conf->software)
211 rx_off = 0;
212 }
213
214 if (rx_off == dsp->rx_is_off)
215 return;
216
217 if (!dsp->ch.peer) {
218 if (dsp_debug & DEBUG_DSP_CORE)
219 printk(KERN_DEBUG "%s: no peer, no rx_off\n",
220 __func__);
221 return;
222 }
223 cq.op = MISDN_CTRL_RX_OFF;
224 cq.p1 = rx_off;
225 if (dsp->ch.peer->ctrl(dsp->ch.peer, CONTROL_CHANNEL, &cq)) {
226 printk(KERN_DEBUG "%s: 2nd CONTROL_CHANNEL failed\n",
227 __func__);
228 return;
229 }
230 dsp->rx_is_off = rx_off;
231 if (dsp_debug & DEBUG_DSP_CORE)
232 printk(KERN_DEBUG "%s: %s set rx_off = %d\n",
233 __func__, dsp->name, rx_off);
234}
235static void
236dsp_rx_off(struct dsp *dsp)
237{
238 struct dsp_conf_member *member;
239
240 if (dsp_options & DSP_OPT_NOHARDWARE)
241 return;
242
243 /* no conf */
244 if (!dsp->conf) {
245 dsp_rx_off_member(dsp);
246 return;
247 }
248 /* check all members in conf */
249 list_for_each_entry(member, &dsp->conf->mlist, list) {
250 dsp_rx_off_member(member->dsp);
251 }
252}
253
8dd2f36f
AE
254/* enable "fill empty" feature */
255static void
256dsp_fill_empty(struct dsp *dsp)
257{
258 struct mISDN_ctrl_req cq;
259
260 memset(&cq, 0, sizeof(cq));
261
262 if (!dsp->ch.peer) {
263 if (dsp_debug & DEBUG_DSP_CORE)
264 printk(KERN_DEBUG "%s: no peer, no fill_empty\n",
265 __func__);
266 return;
267 }
268 cq.op = MISDN_CTRL_FILL_EMPTY;
269 cq.p1 = 1;
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
960366cf
KK
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 cont = *((int *)skb->data);
292 len = skb->len - sizeof(int);
293 data = skb->data + sizeof(int);
294
295 switch (cont) {
296 case DTMF_TONE_START: /* turn on DTMF */
297 if (dsp->hdlc) {
298 ret = -EINVAL;
299 break;
300 }
301 if (dsp_debug & DEBUG_DSP_CORE)
302 printk(KERN_DEBUG "%s: start dtmf\n", __func__);
303 if (len == sizeof(int)) {
c6a2e587
AE
304 if (dsp_debug & DEBUG_DSP_CORE)
305 printk(KERN_NOTICE "changing DTMF Threshold "
306 "to %d\n", *((int *)data));
960366cf
KK
307 dsp->dtmf.treshold = (*(int *)data) * 10000;
308 }
309 /* init goertzel */
310 dsp_dtmf_goertzel_init(dsp);
311
312 /* check dtmf hardware */
313 dsp_dtmf_hardware(dsp);
314 break;
315 case DTMF_TONE_STOP: /* turn off DTMF */
316 if (dsp_debug & DEBUG_DSP_CORE)
317 printk(KERN_DEBUG "%s: stop dtmf\n", __func__);
318 dsp->dtmf.hardware = 0;
319 dsp->dtmf.software = 0;
320 break;
321 case DSP_CONF_JOIN: /* join / update conference */
322 if (len < sizeof(int)) {
323 ret = -EINVAL;
324 break;
325 }
326 if (*((u32 *)data) == 0)
327 goto conf_split;
328 if (dsp_debug & DEBUG_DSP_CORE)
329 printk(KERN_DEBUG "%s: join conference %d\n",
330 __func__, *((u32 *)data));
331 ret = dsp_cmx_conf(dsp, *((u32 *)data));
332 /* dsp_cmx_hardware will also be called here */
333 dsp_rx_off(dsp);
334 if (dsp_debug & DEBUG_DSP_CMX)
335 dsp_cmx_debug(dsp);
336 break;
337 case DSP_CONF_SPLIT: /* remove from conference */
338conf_split:
339 if (dsp_debug & DEBUG_DSP_CORE)
340 printk(KERN_DEBUG "%s: release conference\n", __func__);
341 ret = dsp_cmx_conf(dsp, 0);
342 /* dsp_cmx_hardware will also be called here */
343 if (dsp_debug & DEBUG_DSP_CMX)
344 dsp_cmx_debug(dsp);
345 dsp_rx_off(dsp);
346 break;
347 case DSP_TONE_PATT_ON: /* play tone */
348 if (dsp->hdlc) {
349 ret = -EINVAL;
350 break;
351 }
352 if (len < sizeof(int)) {
353 ret = -EINVAL;
354 break;
355 }
356 if (dsp_debug & DEBUG_DSP_CORE)
357 printk(KERN_DEBUG "%s: turn tone 0x%x on\n",
358 __func__, *((int *)skb->data));
359 ret = dsp_tone(dsp, *((int *)data));
360 if (!ret) {
361 dsp_cmx_hardware(dsp->conf, dsp);
362 dsp_rx_off(dsp);
363 }
364 if (!dsp->tone.tone)
365 goto tone_off;
366 break;
367 case DSP_TONE_PATT_OFF: /* stop tone */
368 if (dsp->hdlc) {
369 ret = -EINVAL;
370 break;
371 }
372 if (dsp_debug & DEBUG_DSP_CORE)
373 printk(KERN_DEBUG "%s: turn tone off\n", __func__);
374 dsp_tone(dsp, 0);
375 dsp_cmx_hardware(dsp->conf, dsp);
376 dsp_rx_off(dsp);
377 /* reset tx buffers (user space data) */
378tone_off:
379 dsp->rx_W = 0;
380 dsp->rx_R = 0;
381 break;
382 case DSP_VOL_CHANGE_TX: /* change volume */
383 if (dsp->hdlc) {
384 ret = -EINVAL;
385 break;
386 }
387 if (len < sizeof(int)) {
388 ret = -EINVAL;
389 break;
390 }
391 dsp->tx_volume = *((int *)data);
392 if (dsp_debug & DEBUG_DSP_CORE)
393 printk(KERN_DEBUG "%s: change tx vol to %d\n",
394 __func__, dsp->tx_volume);
395 dsp_cmx_hardware(dsp->conf, dsp);
396 dsp_dtmf_hardware(dsp);
397 dsp_rx_off(dsp);
398 break;
399 case DSP_VOL_CHANGE_RX: /* change volume */
400 if (dsp->hdlc) {
401 ret = -EINVAL;
402 break;
403 }
404 if (len < sizeof(int)) {
405 ret = -EINVAL;
406 break;
407 }
408 dsp->rx_volume = *((int *)data);
409 if (dsp_debug & DEBUG_DSP_CORE)
410 printk(KERN_DEBUG "%s: change rx vol to %d\n",
411 __func__, dsp->tx_volume);
412 dsp_cmx_hardware(dsp->conf, dsp);
413 dsp_dtmf_hardware(dsp);
414 dsp_rx_off(dsp);
415 break;
416 case DSP_ECHO_ON: /* enable echo */
417 dsp->echo = 1; /* soft echo */
418 if (dsp_debug & DEBUG_DSP_CORE)
419 printk(KERN_DEBUG "%s: enable cmx-echo\n", __func__);
420 dsp_cmx_hardware(dsp->conf, dsp);
421 dsp_rx_off(dsp);
422 if (dsp_debug & DEBUG_DSP_CMX)
423 dsp_cmx_debug(dsp);
424 break;
425 case DSP_ECHO_OFF: /* disable echo */
426 dsp->echo = 0;
427 if (dsp_debug & DEBUG_DSP_CORE)
428 printk(KERN_DEBUG "%s: disable cmx-echo\n", __func__);
429 dsp_cmx_hardware(dsp->conf, dsp);
430 dsp_rx_off(dsp);
431 if (dsp_debug & DEBUG_DSP_CMX)
432 dsp_cmx_debug(dsp);
433 break;
434 case DSP_RECEIVE_ON: /* enable receive to user space */
435 if (dsp_debug & DEBUG_DSP_CORE)
436 printk(KERN_DEBUG "%s: enable receive to user "
437 "space\n", __func__);
438 dsp->rx_disabled = 0;
439 dsp_rx_off(dsp);
440 break;
441 case DSP_RECEIVE_OFF: /* disable receive to user space */
442 if (dsp_debug & DEBUG_DSP_CORE)
443 printk(KERN_DEBUG "%s: disable receive to "
444 "user space\n", __func__);
445 dsp->rx_disabled = 1;
446 dsp_rx_off(dsp);
447 break;
448 case DSP_MIX_ON: /* enable mixing of tx data */
449 if (dsp->hdlc) {
450 ret = -EINVAL;
451 break;
452 }
453 if (dsp_debug & DEBUG_DSP_CORE)
454 printk(KERN_DEBUG "%s: enable mixing of "
455 "tx-data with conf mebers\n", __func__);
456 dsp->tx_mix = 1;
457 dsp_cmx_hardware(dsp->conf, dsp);
458 dsp_rx_off(dsp);
459 if (dsp_debug & DEBUG_DSP_CMX)
460 dsp_cmx_debug(dsp);
461 break;
462 case DSP_MIX_OFF: /* disable mixing of tx data */
463 if (dsp->hdlc) {
464 ret = -EINVAL;
465 break;
466 }
467 if (dsp_debug & DEBUG_DSP_CORE)
468 printk(KERN_DEBUG "%s: disable mixing of "
469 "tx-data with conf mebers\n", __func__);
470 dsp->tx_mix = 0;
471 dsp_cmx_hardware(dsp->conf, dsp);
472 dsp_rx_off(dsp);
473 if (dsp_debug & DEBUG_DSP_CMX)
474 dsp_cmx_debug(dsp);
475 break;
476 case DSP_TXDATA_ON: /* enable txdata */
477 dsp->tx_data = 1;
478 if (dsp_debug & DEBUG_DSP_CORE)
479 printk(KERN_DEBUG "%s: enable tx-data\n", __func__);
480 dsp_cmx_hardware(dsp->conf, dsp);
481 dsp_rx_off(dsp);
482 if (dsp_debug & DEBUG_DSP_CMX)
483 dsp_cmx_debug(dsp);
484 break;
485 case DSP_TXDATA_OFF: /* disable txdata */
486 dsp->tx_data = 0;
487 if (dsp_debug & DEBUG_DSP_CORE)
488 printk(KERN_DEBUG "%s: disable tx-data\n", __func__);
489 dsp_cmx_hardware(dsp->conf, dsp);
490 dsp_rx_off(dsp);
491 if (dsp_debug & DEBUG_DSP_CMX)
492 dsp_cmx_debug(dsp);
493 break;
494 case DSP_DELAY: /* use delay algorithm instead of dynamic
495 jitter algorithm */
496 if (dsp->hdlc) {
497 ret = -EINVAL;
498 break;
499 }
500 if (len < sizeof(int)) {
501 ret = -EINVAL;
502 break;
503 }
504 dsp->cmx_delay = (*((int *)data)) << 3;
505 /* miliseconds to samples */
506 if (dsp->cmx_delay >= (CMX_BUFF_HALF>>1))
507 /* clip to half of maximum usable buffer
508 (half of half buffer) */
509 dsp->cmx_delay = (CMX_BUFF_HALF>>1) - 1;
510 if (dsp_debug & DEBUG_DSP_CORE)
511 printk(KERN_DEBUG "%s: use delay algorithm to "
512 "compensate jitter (%d samples)\n",
513 __func__, dsp->cmx_delay);
514 break;
515 case DSP_JITTER: /* use dynamic jitter algorithm instead of
516 delay algorithm */
517 if (dsp->hdlc) {
518 ret = -EINVAL;
519 break;
520 }
521 dsp->cmx_delay = 0;
522 if (dsp_debug & DEBUG_DSP_CORE)
523 printk(KERN_DEBUG "%s: use jitter algorithm to "
524 "compensate jitter\n", __func__);
525 break;
526 case DSP_TX_DEJITTER: /* use dynamic jitter algorithm for tx-buffer */
527 if (dsp->hdlc) {
528 ret = -EINVAL;
529 break;
530 }
531 dsp->tx_dejitter = 1;
532 if (dsp_debug & DEBUG_DSP_CORE)
533 printk(KERN_DEBUG "%s: use dejitter on TX "
534 "buffer\n", __func__);
535 break;
536 case DSP_TX_DEJ_OFF: /* use tx-buffer without dejittering*/
537 if (dsp->hdlc) {
538 ret = -EINVAL;
539 break;
540 }
541 dsp->tx_dejitter = 0;
542 if (dsp_debug & DEBUG_DSP_CORE)
543 printk(KERN_DEBUG "%s: use TX buffer without "
544 "dejittering\n", __func__);
545 break;
546 case DSP_PIPELINE_CFG:
547 if (dsp->hdlc) {
548 ret = -EINVAL;
549 break;
550 }
551 if (len > 0 && ((char *)data)[len - 1]) {
552 printk(KERN_DEBUG "%s: pipeline config string "
553 "is not NULL terminated!\n", __func__);
554 ret = -EINVAL;
555 } else {
556 dsp->pipeline.inuse = 1;
557 dsp_cmx_hardware(dsp->conf, dsp);
558 ret = dsp_pipeline_build(&dsp->pipeline,
559 len > 0 ? (char *)data : NULL);
560 dsp_cmx_hardware(dsp->conf, dsp);
561 dsp_rx_off(dsp);
562 }
563 break;
564 case DSP_BF_ENABLE_KEY: /* turn blowfish on */
565 if (dsp->hdlc) {
566 ret = -EINVAL;
567 break;
568 }
569 if (len < 4 || len > 56) {
570 ret = -EINVAL;
571 break;
572 }
573 if (dsp_debug & DEBUG_DSP_CORE)
574 printk(KERN_DEBUG "%s: turn blowfish on (key "
575 "not shown)\n", __func__);
576 ret = dsp_bf_init(dsp, (u8 *)data, len);
577 /* set new cont */
578 if (!ret)
579 cont = DSP_BF_ACCEPT;
580 else
581 cont = DSP_BF_REJECT;
582 /* send indication if it worked to set it */
583 nskb = _alloc_mISDN_skb(PH_CONTROL_IND, MISDN_ID_ANY,
584 sizeof(int), &cont, GFP_ATOMIC);
585 if (nskb) {
586 if (dsp->up) {
587 if (dsp->up->send(dsp->up, nskb))
588 dev_kfree_skb(nskb);
589 } else
590 dev_kfree_skb(nskb);
591 }
592 if (!ret) {
593 dsp_cmx_hardware(dsp->conf, dsp);
594 dsp_dtmf_hardware(dsp);
595 dsp_rx_off(dsp);
596 }
597 break;
598 case DSP_BF_DISABLE: /* turn blowfish off */
599 if (dsp->hdlc) {
600 ret = -EINVAL;
601 break;
602 }
603 if (dsp_debug & DEBUG_DSP_CORE)
604 printk(KERN_DEBUG "%s: turn blowfish off\n", __func__);
605 dsp_bf_cleanup(dsp);
606 dsp_cmx_hardware(dsp->conf, dsp);
607 dsp_dtmf_hardware(dsp);
608 dsp_rx_off(dsp);
609 break;
610 default:
611 if (dsp_debug & DEBUG_DSP_CORE)
612 printk(KERN_DEBUG "%s: ctrl req %x unhandled\n",
613 __func__, cont);
614 ret = -EINVAL;
615 }
616 return ret;
617}
618
619static void
620get_features(struct mISDNchannel *ch)
621{
622 struct dsp *dsp = container_of(ch, struct dsp, ch);
623 struct mISDN_ctrl_req cq;
624
960366cf
KK
625 if (!ch->peer) {
626 if (dsp_debug & DEBUG_DSP_CORE)
627 printk(KERN_DEBUG "%s: no peer, no features\n",
628 __func__);
629 return;
630 }
631 memset(&cq, 0, sizeof(cq));
632 cq.op = MISDN_CTRL_GETOP;
633 if (ch->peer->ctrl(ch->peer, CONTROL_CHANNEL, &cq) < 0) {
634 printk(KERN_DEBUG "%s: CONTROL_CHANNEL failed\n",
635 __func__);
636 return;
637 }
638 if (cq.op & MISDN_CTRL_RX_OFF)
639 dsp->features_rx_off = 1;
8dd2f36f
AE
640 if (cq.op & MISDN_CTRL_FILL_EMPTY)
641 dsp->features_fill_empty = 1;
642 if (dsp_options & DSP_OPT_NOHARDWARE)
643 return;
960366cf
KK
644 if ((cq.op & MISDN_CTRL_HW_FEATURES_OP)) {
645 cq.op = MISDN_CTRL_HW_FEATURES;
646 *((u_long *)&cq.p1) = (u_long)&dsp->features;
647 if (ch->peer->ctrl(ch->peer, CONTROL_CHANNEL, &cq)) {
648 printk(KERN_DEBUG "%s: 2nd CONTROL_CHANNEL failed\n",
649 __func__);
650 }
651 } else
652 if (dsp_debug & DEBUG_DSP_CORE)
653 printk(KERN_DEBUG "%s: features not supported for %s\n",
654 __func__, dsp->name);
655}
656
657static int
658dsp_function(struct mISDNchannel *ch, struct sk_buff *skb)
659{
660 struct dsp *dsp = container_of(ch, struct dsp, ch);
661 struct mISDNhead *hh;
662 int ret = 0;
663 u8 *digits;
664 int cont;
960366cf
KK
665 u_long flags;
666
667 hh = mISDN_HEAD_P(skb);
668 switch (hh->prim) {
669 /* FROM DOWN */
670 case (PH_DATA_CNF):
671 dsp->data_pending = 0;
672 /* trigger next hdlc frame, if any */
673 if (dsp->hdlc) {
674 spin_lock_irqsave(&dsp_lock, flags);
675 if (dsp->b_active)
676 schedule_work(&dsp->workq);
677 spin_unlock_irqrestore(&dsp_lock, flags);
678 }
679 break;
680 case (PH_DATA_IND):
681 case (DL_DATA_IND):
682 if (skb->len < 1) {
683 ret = -EINVAL;
684 break;
685 }
686 if (dsp->rx_is_off) {
687 if (dsp_debug & DEBUG_DSP_CORE)
688 printk(KERN_DEBUG "%s: rx-data during rx_off"
689 " for %s\n",
690 __func__, dsp->name);
691 }
692 if (dsp->hdlc) {
693 /* hdlc */
694 spin_lock_irqsave(&dsp_lock, flags);
695 dsp_cmx_hdlc(dsp, skb);
696 spin_unlock_irqrestore(&dsp_lock, flags);
697 if (dsp->rx_disabled) {
698 /* if receive is not allowed */
699 break;
700 }
701 hh->prim = DL_DATA_IND;
702 if (dsp->up)
703 return dsp->up->send(dsp->up, skb);
704 break;
705 }
706
ba3af34e
AE
707 spin_lock_irqsave(&dsp_lock, flags);
708
960366cf
KK
709 /* decrypt if enabled */
710 if (dsp->bf_enable)
711 dsp_bf_decrypt(dsp, skb->data, skb->len);
712 /* pipeline */
713 if (dsp->pipeline.inuse)
714 dsp_pipeline_process_rx(&dsp->pipeline, skb->data,
7cfa153d 715 skb->len, hh->id);
960366cf
KK
716 /* change volume if requested */
717 if (dsp->rx_volume)
718 dsp_change_volume(skb, dsp->rx_volume);
719
720 /* check if dtmf soft decoding is turned on */
721 if (dsp->dtmf.software) {
722 digits = dsp_dtmf_goertzel_decode(dsp, skb->data,
723 skb->len, (dsp_options&DSP_OPT_ULAW)?1:0);
724 while (*digits) {
bb68b1d9 725 struct sk_buff *nskb;
960366cf
KK
726 if (dsp_debug & DEBUG_DSP_DTMF)
727 printk(KERN_DEBUG "%s: digit"
728 "(%c) to layer %s\n",
729 __func__, *digits, dsp->name);
730 cont = DTMF_TONE_VAL | *digits;
731 nskb = _alloc_mISDN_skb(PH_CONTROL_IND,
732 MISDN_ID_ANY, sizeof(int), &cont,
733 GFP_ATOMIC);
734 if (nskb) {
735 if (dsp->up) {
736 if (dsp->up->send(
737 dsp->up, nskb))
738 dev_kfree_skb(nskb);
739 } else
740 dev_kfree_skb(nskb);
741 }
742 digits++;
743 }
744 }
745 /* we need to process receive data if software */
960366cf
KK
746 if (dsp->pcm_slot_tx < 0 && dsp->pcm_slot_rx < 0) {
747 /* process data from card at cmx */
748 dsp_cmx_receive(dsp, skb);
749 }
ba3af34e 750
960366cf
KK
751 spin_unlock_irqrestore(&dsp_lock, flags);
752
753 if (dsp->rx_disabled) {
754 /* if receive is not allowed */
755 break;
756 }
757 hh->prim = DL_DATA_IND;
758 if (dsp->up)
759 return dsp->up->send(dsp->up, skb);
760 break;
761 case (PH_CONTROL_IND):
762 if (dsp_debug & DEBUG_DSP_DTMFCOEFF)
763 printk(KERN_DEBUG "%s: PH_CONTROL INDICATION "
764 "received: %x (len %d) %s\n", __func__,
765 hh->id, skb->len, dsp->name);
766 switch (hh->id) {
767 case (DTMF_HFC_COEF): /* getting coefficients */
768 if (!dsp->dtmf.hardware) {
769 if (dsp_debug & DEBUG_DSP_DTMFCOEFF)
770 printk(KERN_DEBUG "%s: ignoring DTMF "
771 "coefficients from HFC\n",
772 __func__);
773 break;
774 }
775 digits = dsp_dtmf_goertzel_decode(dsp, skb->data,
776 skb->len, 2);
777 while (*digits) {
778 int k;
779 struct sk_buff *nskb;
780 if (dsp_debug & DEBUG_DSP_DTMF)
781 printk(KERN_DEBUG "%s: digit"
782 "(%c) to layer %s\n",
783 __func__, *digits, dsp->name);
784 k = *digits | DTMF_TONE_VAL;
785 nskb = _alloc_mISDN_skb(PH_CONTROL_IND,
786 MISDN_ID_ANY, sizeof(int), &k,
787 GFP_ATOMIC);
788 if (nskb) {
789 if (dsp->up) {
790 if (dsp->up->send(
791 dsp->up, nskb))
792 dev_kfree_skb(nskb);
793 } else
794 dev_kfree_skb(nskb);
795 }
796 digits++;
797 }
798 break;
799 case (HFC_VOL_CHANGE_TX): /* change volume */
800 if (skb->len != sizeof(int)) {
801 ret = -EINVAL;
802 break;
803 }
804 spin_lock_irqsave(&dsp_lock, flags);
805 dsp->tx_volume = *((int *)skb->data);
806 if (dsp_debug & DEBUG_DSP_CORE)
807 printk(KERN_DEBUG "%s: change tx volume to "
808 "%d\n", __func__, dsp->tx_volume);
809 dsp_cmx_hardware(dsp->conf, dsp);
810 dsp_dtmf_hardware(dsp);
811 dsp_rx_off(dsp);
812 spin_unlock_irqrestore(&dsp_lock, flags);
813 break;
814 default:
815 if (dsp_debug & DEBUG_DSP_CORE)
816 printk(KERN_DEBUG "%s: ctrl ind %x unhandled "
817 "%s\n", __func__, hh->id, dsp->name);
818 ret = -EINVAL;
819 }
820 break;
821 case (PH_ACTIVATE_IND):
822 case (PH_ACTIVATE_CNF):
823 if (dsp_debug & DEBUG_DSP_CORE)
824 printk(KERN_DEBUG "%s: b_channel is now active %s\n",
825 __func__, dsp->name);
826 /* bchannel now active */
827 spin_lock_irqsave(&dsp_lock, flags);
828 dsp->b_active = 1;
829 dsp->data_pending = 0;
830 dsp->rx_init = 1;
831 /* rx_W and rx_R will be adjusted on first frame */
832 dsp->rx_W = 0;
833 dsp->rx_R = 0;
834 memset(dsp->rx_buff, 0, sizeof(dsp->rx_buff));
835 dsp_cmx_hardware(dsp->conf, dsp);
836 dsp_dtmf_hardware(dsp);
837 dsp_rx_off(dsp);
838 spin_unlock_irqrestore(&dsp_lock, flags);
839 if (dsp_debug & DEBUG_DSP_CORE)
840 printk(KERN_DEBUG "%s: done with activation, sending "
841 "confirm to user space. %s\n", __func__,
842 dsp->name);
843 /* send activation to upper layer */
844 hh->prim = DL_ESTABLISH_CNF;
845 if (dsp->up)
846 return dsp->up->send(dsp->up, skb);
847 break;
848 case (PH_DEACTIVATE_IND):
849 case (PH_DEACTIVATE_CNF):
850 if (dsp_debug & DEBUG_DSP_CORE)
851 printk(KERN_DEBUG "%s: b_channel is now inactive %s\n",
852 __func__, dsp->name);
853 /* bchannel now inactive */
854 spin_lock_irqsave(&dsp_lock, flags);
855 dsp->b_active = 0;
856 dsp->data_pending = 0;
857 dsp_cmx_hardware(dsp->conf, dsp);
858 dsp_rx_off(dsp);
859 spin_unlock_irqrestore(&dsp_lock, flags);
860 hh->prim = DL_RELEASE_CNF;
861 if (dsp->up)
862 return dsp->up->send(dsp->up, skb);
863 break;
864 /* FROM UP */
865 case (DL_DATA_REQ):
866 case (PH_DATA_REQ):
867 if (skb->len < 1) {
868 ret = -EINVAL;
869 break;
870 }
871 if (dsp->hdlc) {
872 /* hdlc */
e4cce225
PS
873 if (!dsp->b_active) {
874 ret = -EIO;
875 break;
960366cf 876 }
e4cce225
PS
877 hh->prim = PH_DATA_REQ;
878 spin_lock_irqsave(&dsp_lock, flags);
879 skb_queue_tail(&dsp->sendq, skb);
880 schedule_work(&dsp->workq);
960366cf
KK
881 spin_unlock_irqrestore(&dsp_lock, flags);
882 return 0;
883 }
884 /* send data to tx-buffer (if no tone is played) */
885 if (!dsp->tone.tone) {
886 spin_lock_irqsave(&dsp_lock, flags);
887 dsp_cmx_transmit(dsp, skb);
888 spin_unlock_irqrestore(&dsp_lock, flags);
889 }
890 break;
891 case (PH_CONTROL_REQ):
892 spin_lock_irqsave(&dsp_lock, flags);
893 ret = dsp_control_req(dsp, hh, skb);
894 spin_unlock_irqrestore(&dsp_lock, flags);
895 break;
896 case (DL_ESTABLISH_REQ):
897 case (PH_ACTIVATE_REQ):
898 if (dsp_debug & DEBUG_DSP_CORE)
899 printk(KERN_DEBUG "%s: activating b_channel %s\n",
900 __func__, dsp->name);
901 if (dsp->dtmf.hardware || dsp->dtmf.software)
902 dsp_dtmf_goertzel_init(dsp);
903 get_features(ch);
8dd2f36f
AE
904 /* enable fill_empty feature */
905 if (dsp->features_fill_empty)
906 dsp_fill_empty(dsp);
960366cf
KK
907 /* send ph_activate */
908 hh->prim = PH_ACTIVATE_REQ;
909 if (ch->peer)
910 return ch->recv(ch->peer, skb);
911 break;
912 case (DL_RELEASE_REQ):
913 case (PH_DEACTIVATE_REQ):
914 if (dsp_debug & DEBUG_DSP_CORE)
915 printk(KERN_DEBUG "%s: releasing b_channel %s\n",
916 __func__, dsp->name);
917 spin_lock_irqsave(&dsp_lock, flags);
918 dsp->tone.tone = 0;
919 dsp->tone.hardware = 0;
920 dsp->tone.software = 0;
921 if (timer_pending(&dsp->tone.tl))
922 del_timer(&dsp->tone.tl);
923 if (dsp->conf)
924 dsp_cmx_conf(dsp, 0); /* dsp_cmx_hardware will also be
925 called here */
926 skb_queue_purge(&dsp->sendq);
927 spin_unlock_irqrestore(&dsp_lock, flags);
928 hh->prim = PH_DEACTIVATE_REQ;
929 if (ch->peer)
930 return ch->recv(ch->peer, skb);
931 break;
932 default:
933 if (dsp_debug & DEBUG_DSP_CORE)
934 printk(KERN_DEBUG "%s: msg %x unhandled %s\n",
935 __func__, hh->prim, dsp->name);
936 ret = -EINVAL;
937 }
938 if (!ret)
939 dev_kfree_skb(skb);
940 return ret;
941}
942
943static int
944dsp_ctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
945{
946 struct dsp *dsp = container_of(ch, struct dsp, ch);
947 u_long flags;
948 int err = 0;
949
950 if (debug & DEBUG_DSP_CTRL)
951 printk(KERN_DEBUG "%s:(%x)\n", __func__, cmd);
952
953 switch (cmd) {
954 case OPEN_CHANNEL:
955 break;
956 case CLOSE_CHANNEL:
957 if (dsp->ch.peer)
958 dsp->ch.peer->ctrl(dsp->ch.peer, CLOSE_CHANNEL, NULL);
959
960 /* wait until workqueue has finished,
961 * must lock here, or we may hit send-process currently
962 * queueing. */
963 spin_lock_irqsave(&dsp_lock, flags);
964 dsp->b_active = 0;
965 spin_unlock_irqrestore(&dsp_lock, flags);
966 /* MUST not be locked, because it waits until queue is done. */
967 cancel_work_sync(&dsp->workq);
968 spin_lock_irqsave(&dsp_lock, flags);
969 if (timer_pending(&dsp->tone.tl))
970 del_timer(&dsp->tone.tl);
971 skb_queue_purge(&dsp->sendq);
972 if (dsp_debug & DEBUG_DSP_CTRL)
973 printk(KERN_DEBUG "%s: releasing member %s\n",
974 __func__, dsp->name);
975 dsp->b_active = 0;
976 dsp_cmx_conf(dsp, 0); /* dsp_cmx_hardware will also be called
977 here */
978 dsp_pipeline_destroy(&dsp->pipeline);
979
980 if (dsp_debug & DEBUG_DSP_CTRL)
981 printk(KERN_DEBUG "%s: remove & destroy object %s\n",
982 __func__, dsp->name);
983 list_del(&dsp->list);
984 spin_unlock_irqrestore(&dsp_lock, flags);
985
986 if (dsp_debug & DEBUG_DSP_CTRL)
987 printk(KERN_DEBUG "%s: dsp instance released\n",
988 __func__);
989 vfree(dsp);
990 module_put(THIS_MODULE);
991 break;
992 }
993 return err;
994}
995
996static void
997dsp_send_bh(struct work_struct *work)
998{
999 struct dsp *dsp = container_of(work, struct dsp, workq);
1000 struct sk_buff *skb;
1001 struct mISDNhead *hh;
1002
1003 if (dsp->hdlc && dsp->data_pending)
1004 return; /* wait until data has been acknowledged */
1005
1006 /* send queued data */
1007 while ((skb = skb_dequeue(&dsp->sendq))) {
1008 /* in locked date, we must have still data in queue */
1009 if (dsp->data_pending) {
1010 if (dsp_debug & DEBUG_DSP_CORE)
1011 printk(KERN_DEBUG "%s: fifo full %s, this is "
1012 "no bug!\n", __func__, dsp->name);
1013 /* flush transparent data, if not acked */
1014 dev_kfree_skb(skb);
1015 continue;
1016 }
1017 hh = mISDN_HEAD_P(skb);
1018 if (hh->prim == DL_DATA_REQ) {
1019 /* send packet up */
1020 if (dsp->up) {
1021 if (dsp->up->send(dsp->up, skb))
1022 dev_kfree_skb(skb);
1023 } else
1024 dev_kfree_skb(skb);
1025 } else {
1026 /* send packet down */
1027 if (dsp->ch.peer) {
1028 dsp->data_pending = 1;
1029 if (dsp->ch.recv(dsp->ch.peer, skb)) {
1030 dev_kfree_skb(skb);
1031 dsp->data_pending = 0;
1032 }
1033 } else
1034 dev_kfree_skb(skb);
1035 }
1036 }
1037}
1038
1039static int
1040dspcreate(struct channel_req *crq)
1041{
1042 struct dsp *ndsp;
1043 u_long flags;
1044
1045 if (crq->protocol != ISDN_P_B_L2DSP
1046 && crq->protocol != ISDN_P_B_L2DSPHDLC)
1047 return -EPROTONOSUPPORT;
1048 ndsp = vmalloc(sizeof(struct dsp));
1049 if (!ndsp) {
1050 printk(KERN_ERR "%s: vmalloc struct dsp failed\n", __func__);
1051 return -ENOMEM;
1052 }
1053 memset(ndsp, 0, sizeof(struct dsp));
1054 if (dsp_debug & DEBUG_DSP_CTRL)
1055 printk(KERN_DEBUG "%s: creating new dsp instance\n", __func__);
1056
1057 /* default enabled */
1058 INIT_WORK(&ndsp->workq, (void *)dsp_send_bh);
1059 skb_queue_head_init(&ndsp->sendq);
1060 ndsp->ch.send = dsp_function;
1061 ndsp->ch.ctrl = dsp_ctrl;
1062 ndsp->up = crq->ch;
1063 crq->ch = &ndsp->ch;
1064 if (crq->protocol == ISDN_P_B_L2DSP) {
1065 crq->protocol = ISDN_P_B_RAW;
1066 ndsp->hdlc = 0;
1067 } else {
1068 crq->protocol = ISDN_P_B_HDLC;
1069 ndsp->hdlc = 1;
1070 }
1071 if (!try_module_get(THIS_MODULE))
1072 printk(KERN_WARNING "%s:cannot get module\n",
1073 __func__);
1074
1075 sprintf(ndsp->name, "DSP_C%x(0x%p)",
1076 ndsp->up->st->dev->id + 1, ndsp);
1077 /* set frame size to start */
1078 ndsp->features.hfc_id = -1; /* current PCM id */
1079 ndsp->features.pcm_id = -1; /* current PCM id */
1080 ndsp->pcm_slot_rx = -1; /* current CPM slot */
1081 ndsp->pcm_slot_tx = -1;
1082 ndsp->pcm_bank_rx = -1;
1083 ndsp->pcm_bank_tx = -1;
1084 ndsp->hfc_conf = -1; /* current conference number */
1085 /* set tone timer */
1086 ndsp->tone.tl.function = (void *)dsp_tone_timeout;
1087 ndsp->tone.tl.data = (long) ndsp;
1088 init_timer(&ndsp->tone.tl);
1089
1090 if (dtmfthreshold < 20 || dtmfthreshold > 500)
1091 dtmfthreshold = 200;
1092 ndsp->dtmf.treshold = dtmfthreshold*10000;
1093
1094 /* init pipeline append to list */
1095 spin_lock_irqsave(&dsp_lock, flags);
1096 dsp_pipeline_init(&ndsp->pipeline);
1097 list_add_tail(&ndsp->list, &dsp_ilist);
1098 spin_unlock_irqrestore(&dsp_lock, flags);
1099
1100 return 0;
1101}
1102
1103
1104static struct Bprotocol DSP = {
1105 .Bprotocols = (1 << (ISDN_P_B_L2DSP & ISDN_P_B_MASK))
1106 | (1 << (ISDN_P_B_L2DSPHDLC & ISDN_P_B_MASK)),
1107 .name = "dsp",
1108 .create = dspcreate
1109};
1110
1111static int dsp_init(void)
1112{
1113 int err;
1114 int tics;
1115
1116 printk(KERN_INFO "DSP modul %s\n", mISDN_dsp_revision);
1117
1118 dsp_options = options;
1119 dsp_debug = debug;
1120
1121 /* set packet size */
1122 dsp_poll = poll;
1123 if (dsp_poll) {
1124 if (dsp_poll > MAX_POLL) {
1125 printk(KERN_ERR "%s: Wrong poll value (%d), use %d "
1126 "maximum.\n", __func__, poll, MAX_POLL);
1127 err = -EINVAL;
1128 return err;
1129 }
1130 if (dsp_poll < 8) {
1131 printk(KERN_ERR "%s: Wrong poll value (%d), use 8 "
1132 "minimum.\n", __func__, dsp_poll);
1133 err = -EINVAL;
1134 return err;
1135 }
1136 dsp_tics = poll * HZ / 8000;
1137 if (dsp_tics * 8000 != poll * HZ) {
1138 printk(KERN_INFO "mISDN_dsp: Cannot clock every %d "
1139 "samples (0,125 ms). It is not a multiple of "
1140 "%d HZ.\n", poll, HZ);
1141 err = -EINVAL;
1142 return err;
1143 }
1144 } else {
1145 poll = 8;
1146 while (poll <= MAX_POLL) {
400fd978 1147 tics = (poll * HZ) / 8000;
960366cf
KK
1148 if (tics * 8000 == poll * HZ) {
1149 dsp_tics = tics;
1150 dsp_poll = poll;
1151 if (poll >= 64)
1152 break;
1153 }
1154 poll++;
1155 }
1156 }
1157 if (dsp_poll == 0) {
1158 printk(KERN_INFO "mISDN_dsp: There is no multiple of kernel "
1159 "clock that equals exactly the duration of 8-256 "
1160 "samples. (Choose kernel clock speed like 100, 250, "
1161 "300, 1000)\n");
1162 err = -EINVAL;
1163 return err;
1164 }
1165 printk(KERN_INFO "mISDN_dsp: DSP clocks every %d samples. This equals "
1166 "%d jiffies.\n", dsp_poll, dsp_tics);
1167
1168 spin_lock_init(&dsp_lock);
1169 INIT_LIST_HEAD(&dsp_ilist);
1170 INIT_LIST_HEAD(&conf_ilist);
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)?dsp_audio_ulaw_to_s32:
1176 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 dsp_spl_tl.function = (void *)dsp_cmx_send;
1199 dsp_spl_tl.data = 0;
1200 init_timer(&dsp_spl_tl);
1201 dsp_spl_tl.expires = jiffies + dsp_tics;
1202 dsp_spl_jiffies = dsp_spl_tl.expires;
1203 add_timer(&dsp_spl_tl);
1204
1205 return 0;
1206}
1207
1208
1209static void dsp_cleanup(void)
1210{
1211 mISDN_unregister_Bprotocol(&DSP);
1212
1213 if (timer_pending(&dsp_spl_tl))
1214 del_timer(&dsp_spl_tl);
1215
1216 if (!list_empty(&dsp_ilist)) {
1217 printk(KERN_ERR "mISDN_dsp: Audio DSP object inst list not "
1218 "empty.\n");
1219 }
1220 if (!list_empty(&conf_ilist)) {
1221 printk(KERN_ERR "mISDN_dsp: Conference list not empty. Not "
1222 "all memory freed.\n");
1223 }
1224
1225 dsp_pipeline_module_exit();
1226}
1227
1228module_init(dsp_init);
1229module_exit(dsp_cleanup);
1230