2 * Author Andreas Eversberg (jolly@eversberg.eu)
3 * Based on source code structure by
4 * Karsten Keil (keil@isdn4linux.de)
6 * This file is (c) under GNU PUBLIC LICENSE
7 * For changes and modifications please read
8 * ../../../Documentation/isdn/mISDN.cert
10 * Thanks to Karsten Keil (great drivers)
11 * Cologne Chip (great chips)
14 * Real-time tone generation
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
25 * The dsp module provides layer 2 for b-channels (64kbit). It provides
26 * transparent audio forwarding with special digital signal processing:
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
35 * - (8) encryption/decryption
39 * ------upper layer------
43 * +-----+-------------+-----+
50 * |+---------+| +----+----+
53 * || Tones || | DTMF |
56 * |+----+----+| +----+----+
60 * +----+----+ +----+----+
63 * |TX Volume| |RX Volume|
66 * +----+----+ +----+----+
70 * +----+-------------+----+
73 * | Pipeline Processing |
76 * +----+-------------+----+
80 * +----+----+ +----+----+
83 * | Encrypt | | Decrypt |
86 * +----+----+ +----+----+
90 * ------card layer------
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
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.
102 * DTMF: Will be detected by hardware if possible. It is done before CMX
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.
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.
113 * Echo: Is generated by CMX and is used to check performane of hard and
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.
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.
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.
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.
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.
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>
164 static const char *mISDN_dsp_revision = "2.0";
169 static int dtmfthreshold = 100;
171 MODULE_AUTHOR("Andreas Eversberg");
172 module_param(debug, uint, S_IRUGO | S_IWUSR);
173 module_param(options, uint, S_IRUGO | S_IWUSR);
174 module_param(poll, uint, S_IRUGO | S_IWUSR);
175 module_param(dtmfthreshold, uint, S_IRUGO | S_IWUSR);
176 MODULE_LICENSE("GPL");
178 /*int spinnest = 0;*/
180 spinlock_t dsp_lock; /* global dsp lock */
181 struct list_head dsp_ilist;
182 struct list_head conf_ilist;
185 int dsp_poll, dsp_tics;
187 /* check if rx may be turned off or must be turned on */
189 dsp_rx_off_member(struct dsp *dsp)
191 struct mISDN_ctrl_req cq;
194 memset(&cq, 0, sizeof(cq));
196 if (!dsp->features_rx_off)
200 if (!dsp->rx_disabled)
203 else if (dsp->dtmf.software)
205 /* echo in software */
206 else if (dsp->echo && dsp->pcm_slot_tx < 0)
208 /* bridge in software */
209 else if (dsp->conf) {
210 if (dsp->conf->software)
214 if (rx_off == dsp->rx_is_off)
218 if (dsp_debug & DEBUG_DSP_CORE)
219 printk(KERN_DEBUG "%s: no peer, no rx_off\n",
223 cq.op = MISDN_CTRL_RX_OFF;
225 if (dsp->ch.peer->ctrl(dsp->ch.peer, CONTROL_CHANNEL, &cq)) {
226 printk(KERN_DEBUG "%s: 2nd CONTROL_CHANNEL failed\n",
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);
236 dsp_rx_off(struct dsp *dsp)
238 struct dsp_conf_member *member;
240 if (dsp_options & DSP_OPT_NOHARDWARE)
245 dsp_rx_off_member(dsp);
248 /* check all members in conf */
249 list_for_each_entry(member, &dsp->conf->mlist, list) {
250 dsp_rx_off_member(member->dsp);
254 /* enable "fill empty" feature */
256 dsp_fill_empty(struct dsp *dsp)
258 struct mISDN_ctrl_req cq;
260 memset(&cq, 0, sizeof(cq));
263 if (dsp_debug & DEBUG_DSP_CORE)
264 printk(KERN_DEBUG "%s: no peer, no fill_empty\n",
268 cq.op = MISDN_CTRL_FILL_EMPTY;
270 if (dsp->ch.peer->ctrl(dsp->ch.peer, CONTROL_CHANNEL, &cq)) {
271 printk(KERN_DEBUG "%s: CONTROL_CHANNEL failed\n",
275 if (dsp_debug & DEBUG_DSP_CORE)
276 printk(KERN_DEBUG "%s: %s set fill_empty = 1\n",
277 __func__, dsp->name);
281 dsp_control_req(struct dsp *dsp, struct mISDNhead *hh, struct sk_buff *skb)
283 struct sk_buff *nskb;
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);
296 case DTMF_TONE_START: /* turn on DTMF */
301 if (dsp_debug & DEBUG_DSP_CORE)
302 printk(KERN_DEBUG "%s: start dtmf\n", __func__);
303 if (len == sizeof(int)) {
304 if (dsp_debug & DEBUG_DSP_CORE)
305 printk(KERN_NOTICE "changing DTMF Threshold "
306 "to %d\n", *((int *)data));
307 dsp->dtmf.treshold = (*(int *)data) * 10000;
310 dsp_dtmf_goertzel_init(dsp);
312 /* check dtmf hardware */
313 dsp_dtmf_hardware(dsp);
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;
321 case DSP_CONF_JOIN: /* join / update conference */
322 if (len < sizeof(int)) {
326 if (*((u32 *)data) == 0)
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 */
334 if (dsp_debug & DEBUG_DSP_CMX)
337 case DSP_CONF_SPLIT: /* remove from conference */
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)
347 case DSP_TONE_PATT_ON: /* play tone */
352 if (len < sizeof(int)) {
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));
361 dsp_cmx_hardware(dsp->conf, dsp);
367 case DSP_TONE_PATT_OFF: /* stop tone */
372 if (dsp_debug & DEBUG_DSP_CORE)
373 printk(KERN_DEBUG "%s: turn tone off\n", __func__);
375 dsp_cmx_hardware(dsp->conf, dsp);
377 /* reset tx buffers (user space data) */
382 case DSP_VOL_CHANGE_TX: /* change volume */
387 if (len < sizeof(int)) {
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);
399 case DSP_VOL_CHANGE_RX: /* change volume */
404 if (len < sizeof(int)) {
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);
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);
422 if (dsp_debug & DEBUG_DSP_CMX)
425 case DSP_ECHO_OFF: /* disable echo */
427 if (dsp_debug & DEBUG_DSP_CORE)
428 printk(KERN_DEBUG "%s: disable cmx-echo\n", __func__);
429 dsp_cmx_hardware(dsp->conf, dsp);
431 if (dsp_debug & DEBUG_DSP_CMX)
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;
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;
448 case DSP_MIX_ON: /* enable mixing of tx data */
453 if (dsp_debug & DEBUG_DSP_CORE)
454 printk(KERN_DEBUG "%s: enable mixing of "
455 "tx-data with conf mebers\n", __func__);
457 dsp_cmx_hardware(dsp->conf, dsp);
459 if (dsp_debug & DEBUG_DSP_CMX)
462 case DSP_MIX_OFF: /* disable mixing of tx data */
467 if (dsp_debug & DEBUG_DSP_CORE)
468 printk(KERN_DEBUG "%s: disable mixing of "
469 "tx-data with conf mebers\n", __func__);
471 dsp_cmx_hardware(dsp->conf, dsp);
473 if (dsp_debug & DEBUG_DSP_CMX)
476 case DSP_TXDATA_ON: /* enable txdata */
478 if (dsp_debug & DEBUG_DSP_CORE)
479 printk(KERN_DEBUG "%s: enable tx-data\n", __func__);
480 dsp_cmx_hardware(dsp->conf, dsp);
482 if (dsp_debug & DEBUG_DSP_CMX)
485 case DSP_TXDATA_OFF: /* disable txdata */
487 if (dsp_debug & DEBUG_DSP_CORE)
488 printk(KERN_DEBUG "%s: disable tx-data\n", __func__);
489 dsp_cmx_hardware(dsp->conf, dsp);
491 if (dsp_debug & DEBUG_DSP_CMX)
494 case DSP_DELAY: /* use delay algorithm instead of dynamic
500 if (len < sizeof(int)) {
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);
515 case DSP_JITTER: /* use dynamic jitter algorithm instead of
522 if (dsp_debug & DEBUG_DSP_CORE)
523 printk(KERN_DEBUG "%s: use jitter algorithm to "
524 "compensate jitter\n", __func__);
526 case DSP_TX_DEJITTER: /* use dynamic jitter algorithm for tx-buffer */
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__);
536 case DSP_TX_DEJ_OFF: /* use tx-buffer without dejittering*/
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__);
546 case DSP_PIPELINE_CFG:
551 if (len > 0 && ((char *)data)[len - 1]) {
552 printk(KERN_DEBUG "%s: pipeline config string "
553 "is not NULL terminated!\n", __func__);
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);
564 case DSP_BF_ENABLE_KEY: /* turn blowfish on */
569 if (len < 4 || len > 56) {
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);
579 cont = DSP_BF_ACCEPT;
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);
587 if (dsp->up->send(dsp->up, nskb))
593 dsp_cmx_hardware(dsp->conf, dsp);
594 dsp_dtmf_hardware(dsp);
598 case DSP_BF_DISABLE: /* turn blowfish off */
603 if (dsp_debug & DEBUG_DSP_CORE)
604 printk(KERN_DEBUG "%s: turn blowfish off\n", __func__);
606 dsp_cmx_hardware(dsp->conf, dsp);
607 dsp_dtmf_hardware(dsp);
611 if (dsp_debug & DEBUG_DSP_CORE)
612 printk(KERN_DEBUG "%s: ctrl req %x unhandled\n",
620 get_features(struct mISDNchannel *ch)
622 struct dsp *dsp = container_of(ch, struct dsp, ch);
623 struct mISDN_ctrl_req cq;
626 if (dsp_debug & DEBUG_DSP_CORE)
627 printk(KERN_DEBUG "%s: no peer, no features\n",
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",
638 if (cq.op & MISDN_CTRL_RX_OFF)
639 dsp->features_rx_off = 1;
640 if (cq.op & MISDN_CTRL_FILL_EMPTY)
641 dsp->features_fill_empty = 1;
642 if (dsp_options & DSP_OPT_NOHARDWARE)
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",
652 if (dsp_debug & DEBUG_DSP_CORE)
653 printk(KERN_DEBUG "%s: features not supported for %s\n",
654 __func__, dsp->name);
658 dsp_function(struct mISDNchannel *ch, struct sk_buff *skb)
660 struct dsp *dsp = container_of(ch, struct dsp, ch);
661 struct mISDNhead *hh;
667 hh = mISDN_HEAD_P(skb);
671 dsp->data_pending = 0;
672 /* trigger next hdlc frame, if any */
674 spin_lock_irqsave(&dsp_lock, flags);
676 schedule_work(&dsp->workq);
677 spin_unlock_irqrestore(&dsp_lock, flags);
686 if (dsp->rx_is_off) {
687 if (dsp_debug & DEBUG_DSP_CORE)
688 printk(KERN_DEBUG "%s: rx-data during rx_off"
690 __func__, dsp->name);
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 */
701 hh->prim = DL_DATA_IND;
703 return dsp->up->send(dsp->up, skb);
707 /* decrypt if enabled */
709 dsp_bf_decrypt(dsp, skb->data, skb->len);
711 if (dsp->pipeline.inuse)
712 dsp_pipeline_process_rx(&dsp->pipeline, skb->data,
714 /* change volume if requested */
716 dsp_change_volume(skb, dsp->rx_volume);
718 /* check if dtmf soft decoding is turned on */
719 if (dsp->dtmf.software) {
720 digits = dsp_dtmf_goertzel_decode(dsp, skb->data,
721 skb->len, (dsp_options&DSP_OPT_ULAW)?1:0);
723 struct sk_buff *nskb;
724 if (dsp_debug & DEBUG_DSP_DTMF)
725 printk(KERN_DEBUG "%s: digit"
726 "(%c) to layer %s\n",
727 __func__, *digits, dsp->name);
728 cont = DTMF_TONE_VAL | *digits;
729 nskb = _alloc_mISDN_skb(PH_CONTROL_IND,
730 MISDN_ID_ANY, sizeof(int), &cont,
743 /* we need to process receive data if software */
744 spin_lock_irqsave(&dsp_lock, flags);
745 if (dsp->pcm_slot_tx < 0 && dsp->pcm_slot_rx < 0) {
746 /* process data from card at cmx */
747 dsp_cmx_receive(dsp, skb);
749 spin_unlock_irqrestore(&dsp_lock, flags);
751 if (dsp->rx_disabled) {
752 /* if receive is not allowed */
755 hh->prim = DL_DATA_IND;
757 return dsp->up->send(dsp->up, skb);
759 case (PH_CONTROL_IND):
760 if (dsp_debug & DEBUG_DSP_DTMFCOEFF)
761 printk(KERN_DEBUG "%s: PH_CONTROL INDICATION "
762 "received: %x (len %d) %s\n", __func__,
763 hh->id, skb->len, dsp->name);
765 case (DTMF_HFC_COEF): /* getting coefficients */
766 if (!dsp->dtmf.hardware) {
767 if (dsp_debug & DEBUG_DSP_DTMFCOEFF)
768 printk(KERN_DEBUG "%s: ignoring DTMF "
769 "coefficients from HFC\n",
773 digits = dsp_dtmf_goertzel_decode(dsp, skb->data,
777 struct sk_buff *nskb;
778 if (dsp_debug & DEBUG_DSP_DTMF)
779 printk(KERN_DEBUG "%s: digit"
780 "(%c) to layer %s\n",
781 __func__, *digits, dsp->name);
782 k = *digits | DTMF_TONE_VAL;
783 nskb = _alloc_mISDN_skb(PH_CONTROL_IND,
784 MISDN_ID_ANY, sizeof(int), &k,
797 case (HFC_VOL_CHANGE_TX): /* change volume */
798 if (skb->len != sizeof(int)) {
802 spin_lock_irqsave(&dsp_lock, flags);
803 dsp->tx_volume = *((int *)skb->data);
804 if (dsp_debug & DEBUG_DSP_CORE)
805 printk(KERN_DEBUG "%s: change tx volume to "
806 "%d\n", __func__, dsp->tx_volume);
807 dsp_cmx_hardware(dsp->conf, dsp);
808 dsp_dtmf_hardware(dsp);
810 spin_unlock_irqrestore(&dsp_lock, flags);
813 if (dsp_debug & DEBUG_DSP_CORE)
814 printk(KERN_DEBUG "%s: ctrl ind %x unhandled "
815 "%s\n", __func__, hh->id, dsp->name);
819 case (PH_ACTIVATE_IND):
820 case (PH_ACTIVATE_CNF):
821 if (dsp_debug & DEBUG_DSP_CORE)
822 printk(KERN_DEBUG "%s: b_channel is now active %s\n",
823 __func__, dsp->name);
824 /* bchannel now active */
825 spin_lock_irqsave(&dsp_lock, flags);
827 dsp->data_pending = 0;
829 /* rx_W and rx_R will be adjusted on first frame */
832 memset(dsp->rx_buff, 0, sizeof(dsp->rx_buff));
833 dsp_cmx_hardware(dsp->conf, dsp);
834 dsp_dtmf_hardware(dsp);
836 spin_unlock_irqrestore(&dsp_lock, flags);
837 if (dsp_debug & DEBUG_DSP_CORE)
838 printk(KERN_DEBUG "%s: done with activation, sending "
839 "confirm to user space. %s\n", __func__,
841 /* send activation to upper layer */
842 hh->prim = DL_ESTABLISH_CNF;
844 return dsp->up->send(dsp->up, skb);
846 case (PH_DEACTIVATE_IND):
847 case (PH_DEACTIVATE_CNF):
848 if (dsp_debug & DEBUG_DSP_CORE)
849 printk(KERN_DEBUG "%s: b_channel is now inactive %s\n",
850 __func__, dsp->name);
851 /* bchannel now inactive */
852 spin_lock_irqsave(&dsp_lock, flags);
854 dsp->data_pending = 0;
855 dsp_cmx_hardware(dsp->conf, dsp);
857 spin_unlock_irqrestore(&dsp_lock, flags);
858 hh->prim = DL_RELEASE_CNF;
860 return dsp->up->send(dsp->up, skb);
871 if (!dsp->b_active) {
875 hh->prim = PH_DATA_REQ;
876 spin_lock_irqsave(&dsp_lock, flags);
877 skb_queue_tail(&dsp->sendq, skb);
878 schedule_work(&dsp->workq);
879 spin_unlock_irqrestore(&dsp_lock, flags);
882 /* send data to tx-buffer (if no tone is played) */
883 if (!dsp->tone.tone) {
884 spin_lock_irqsave(&dsp_lock, flags);
885 dsp_cmx_transmit(dsp, skb);
886 spin_unlock_irqrestore(&dsp_lock, flags);
889 case (PH_CONTROL_REQ):
890 spin_lock_irqsave(&dsp_lock, flags);
891 ret = dsp_control_req(dsp, hh, skb);
892 spin_unlock_irqrestore(&dsp_lock, flags);
894 case (DL_ESTABLISH_REQ):
895 case (PH_ACTIVATE_REQ):
896 if (dsp_debug & DEBUG_DSP_CORE)
897 printk(KERN_DEBUG "%s: activating b_channel %s\n",
898 __func__, dsp->name);
899 if (dsp->dtmf.hardware || dsp->dtmf.software)
900 dsp_dtmf_goertzel_init(dsp);
902 /* enable fill_empty feature */
903 if (dsp->features_fill_empty)
905 /* send ph_activate */
906 hh->prim = PH_ACTIVATE_REQ;
908 return ch->recv(ch->peer, skb);
910 case (DL_RELEASE_REQ):
911 case (PH_DEACTIVATE_REQ):
912 if (dsp_debug & DEBUG_DSP_CORE)
913 printk(KERN_DEBUG "%s: releasing b_channel %s\n",
914 __func__, dsp->name);
915 spin_lock_irqsave(&dsp_lock, flags);
917 dsp->tone.hardware = 0;
918 dsp->tone.software = 0;
919 if (timer_pending(&dsp->tone.tl))
920 del_timer(&dsp->tone.tl);
922 dsp_cmx_conf(dsp, 0); /* dsp_cmx_hardware will also be
924 skb_queue_purge(&dsp->sendq);
925 spin_unlock_irqrestore(&dsp_lock, flags);
926 hh->prim = PH_DEACTIVATE_REQ;
928 return ch->recv(ch->peer, skb);
931 if (dsp_debug & DEBUG_DSP_CORE)
932 printk(KERN_DEBUG "%s: msg %x unhandled %s\n",
933 __func__, hh->prim, dsp->name);
942 dsp_ctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
944 struct dsp *dsp = container_of(ch, struct dsp, ch);
948 if (debug & DEBUG_DSP_CTRL)
949 printk(KERN_DEBUG "%s:(%x)\n", __func__, cmd);
956 dsp->ch.peer->ctrl(dsp->ch.peer, CLOSE_CHANNEL, NULL);
958 /* wait until workqueue has finished,
959 * must lock here, or we may hit send-process currently
961 spin_lock_irqsave(&dsp_lock, flags);
963 spin_unlock_irqrestore(&dsp_lock, flags);
964 /* MUST not be locked, because it waits until queue is done. */
965 cancel_work_sync(&dsp->workq);
966 spin_lock_irqsave(&dsp_lock, flags);
967 if (timer_pending(&dsp->tone.tl))
968 del_timer(&dsp->tone.tl);
969 skb_queue_purge(&dsp->sendq);
970 if (dsp_debug & DEBUG_DSP_CTRL)
971 printk(KERN_DEBUG "%s: releasing member %s\n",
972 __func__, dsp->name);
974 dsp_cmx_conf(dsp, 0); /* dsp_cmx_hardware will also be called
976 dsp_pipeline_destroy(&dsp->pipeline);
978 if (dsp_debug & DEBUG_DSP_CTRL)
979 printk(KERN_DEBUG "%s: remove & destroy object %s\n",
980 __func__, dsp->name);
981 list_del(&dsp->list);
982 spin_unlock_irqrestore(&dsp_lock, flags);
984 if (dsp_debug & DEBUG_DSP_CTRL)
985 printk(KERN_DEBUG "%s: dsp instance released\n",
988 module_put(THIS_MODULE);
995 dsp_send_bh(struct work_struct *work)
997 struct dsp *dsp = container_of(work, struct dsp, workq);
999 struct mISDNhead *hh;
1001 if (dsp->hdlc && dsp->data_pending)
1002 return; /* wait until data has been acknowledged */
1004 /* send queued data */
1005 while ((skb = skb_dequeue(&dsp->sendq))) {
1006 /* in locked date, we must have still data in queue */
1007 if (dsp->data_pending) {
1008 if (dsp_debug & DEBUG_DSP_CORE)
1009 printk(KERN_DEBUG "%s: fifo full %s, this is "
1010 "no bug!\n", __func__, dsp->name);
1011 /* flush transparent data, if not acked */
1015 hh = mISDN_HEAD_P(skb);
1016 if (hh->prim == DL_DATA_REQ) {
1017 /* send packet up */
1019 if (dsp->up->send(dsp->up, skb))
1024 /* send packet down */
1026 dsp->data_pending = 1;
1027 if (dsp->ch.recv(dsp->ch.peer, skb)) {
1029 dsp->data_pending = 0;
1038 dspcreate(struct channel_req *crq)
1043 if (crq->protocol != ISDN_P_B_L2DSP
1044 && crq->protocol != ISDN_P_B_L2DSPHDLC)
1045 return -EPROTONOSUPPORT;
1046 ndsp = vmalloc(sizeof(struct dsp));
1048 printk(KERN_ERR "%s: vmalloc struct dsp failed\n", __func__);
1051 memset(ndsp, 0, sizeof(struct dsp));
1052 if (dsp_debug & DEBUG_DSP_CTRL)
1053 printk(KERN_DEBUG "%s: creating new dsp instance\n", __func__);
1055 /* default enabled */
1056 INIT_WORK(&ndsp->workq, (void *)dsp_send_bh);
1057 skb_queue_head_init(&ndsp->sendq);
1058 ndsp->ch.send = dsp_function;
1059 ndsp->ch.ctrl = dsp_ctrl;
1061 crq->ch = &ndsp->ch;
1062 if (crq->protocol == ISDN_P_B_L2DSP) {
1063 crq->protocol = ISDN_P_B_RAW;
1066 crq->protocol = ISDN_P_B_HDLC;
1069 if (!try_module_get(THIS_MODULE))
1070 printk(KERN_WARNING "%s:cannot get module\n",
1073 sprintf(ndsp->name, "DSP_C%x(0x%p)",
1074 ndsp->up->st->dev->id + 1, ndsp);
1075 /* set frame size to start */
1076 ndsp->features.hfc_id = -1; /* current PCM id */
1077 ndsp->features.pcm_id = -1; /* current PCM id */
1078 ndsp->pcm_slot_rx = -1; /* current CPM slot */
1079 ndsp->pcm_slot_tx = -1;
1080 ndsp->pcm_bank_rx = -1;
1081 ndsp->pcm_bank_tx = -1;
1082 ndsp->hfc_conf = -1; /* current conference number */
1083 /* set tone timer */
1084 ndsp->tone.tl.function = (void *)dsp_tone_timeout;
1085 ndsp->tone.tl.data = (long) ndsp;
1086 init_timer(&ndsp->tone.tl);
1088 if (dtmfthreshold < 20 || dtmfthreshold > 500)
1089 dtmfthreshold = 200;
1090 ndsp->dtmf.treshold = dtmfthreshold*10000;
1092 /* init pipeline append to list */
1093 spin_lock_irqsave(&dsp_lock, flags);
1094 dsp_pipeline_init(&ndsp->pipeline);
1095 list_add_tail(&ndsp->list, &dsp_ilist);
1096 spin_unlock_irqrestore(&dsp_lock, flags);
1102 static struct Bprotocol DSP = {
1103 .Bprotocols = (1 << (ISDN_P_B_L2DSP & ISDN_P_B_MASK))
1104 | (1 << (ISDN_P_B_L2DSPHDLC & ISDN_P_B_MASK)),
1109 static int dsp_init(void)
1114 printk(KERN_INFO "DSP modul %s\n", mISDN_dsp_revision);
1116 dsp_options = options;
1119 /* set packet size */
1122 if (dsp_poll > MAX_POLL) {
1123 printk(KERN_ERR "%s: Wrong poll value (%d), use %d "
1124 "maximum.\n", __func__, poll, MAX_POLL);
1129 printk(KERN_ERR "%s: Wrong poll value (%d), use 8 "
1130 "minimum.\n", __func__, dsp_poll);
1134 dsp_tics = poll * HZ / 8000;
1135 if (dsp_tics * 8000 != poll * HZ) {
1136 printk(KERN_INFO "mISDN_dsp: Cannot clock every %d "
1137 "samples (0,125 ms). It is not a multiple of "
1138 "%d HZ.\n", poll, HZ);
1144 while (poll <= MAX_POLL) {
1145 tics = (poll * HZ) / 8000;
1146 if (tics * 8000 == poll * HZ) {
1155 if (dsp_poll == 0) {
1156 printk(KERN_INFO "mISDN_dsp: There is no multiple of kernel "
1157 "clock that equals exactly the duration of 8-256 "
1158 "samples. (Choose kernel clock speed like 100, 250, "
1163 printk(KERN_INFO "mISDN_dsp: DSP clocks every %d samples. This equals "
1164 "%d jiffies.\n", dsp_poll, dsp_tics);
1166 spin_lock_init(&dsp_lock);
1167 INIT_LIST_HEAD(&dsp_ilist);
1168 INIT_LIST_HEAD(&conf_ilist);
1170 /* init conversion tables */
1171 dsp_audio_generate_law_tables();
1172 dsp_silence = (dsp_options&DSP_OPT_ULAW)?0xff:0x2a;
1173 dsp_audio_law_to_s32 = (dsp_options&DSP_OPT_ULAW)?dsp_audio_ulaw_to_s32:
1174 dsp_audio_alaw_to_s32;
1175 dsp_audio_generate_s2law_table();
1176 dsp_audio_generate_seven();
1177 dsp_audio_generate_mix_table();
1178 if (dsp_options & DSP_OPT_ULAW)
1179 dsp_audio_generate_ulaw_samples();
1180 dsp_audio_generate_volume_changes();
1182 err = dsp_pipeline_module_init();
1184 printk(KERN_ERR "mISDN_dsp: Can't initialize pipeline, "
1185 "error(%d)\n", err);
1189 err = mISDN_register_Bprotocol(&DSP);
1191 printk(KERN_ERR "Can't register %s error(%d)\n", DSP.name, err);
1195 /* set sample timer */
1196 dsp_spl_tl.function = (void *)dsp_cmx_send;
1197 dsp_spl_tl.data = 0;
1198 init_timer(&dsp_spl_tl);
1199 dsp_spl_tl.expires = jiffies + dsp_tics;
1200 dsp_spl_jiffies = dsp_spl_tl.expires;
1201 add_timer(&dsp_spl_tl);
1207 static void dsp_cleanup(void)
1209 mISDN_unregister_Bprotocol(&DSP);
1211 if (timer_pending(&dsp_spl_tl))
1212 del_timer(&dsp_spl_tl);
1214 if (!list_empty(&dsp_ilist)) {
1215 printk(KERN_ERR "mISDN_dsp: Audio DSP object inst list not "
1218 if (!list_empty(&conf_ilist)) {
1219 printk(KERN_ERR "mISDN_dsp: Conference list not empty. Not "
1220 "all memory freed.\n");
1223 dsp_pipeline_module_exit();
1226 module_init(dsp_init);
1227 module_exit(dsp_cleanup);