xref: /OK3568_Linux_fs/kernel/drivers/isdn/mISDN/dsp_cmx.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Audio crossconnecting/conferrencing (hardware level).
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Copyright 2002 by Andreas Eversberg (jolly@eversberg.eu)
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * This software may be used and distributed according to the terms
7*4882a593Smuzhiyun  * of the GNU General Public License, incorporated herein by reference.
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  */
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun /*
12*4882a593Smuzhiyun  * The process of adding and removing parties to/from a conference:
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  * There is a chain of struct dsp_conf which has one or more members in a chain
15*4882a593Smuzhiyun  * of struct dsp_conf_member.
16*4882a593Smuzhiyun  *
17*4882a593Smuzhiyun  * After a party is added, the conference is checked for hardware capability.
18*4882a593Smuzhiyun  * Also if a party is removed, the conference is checked again.
19*4882a593Smuzhiyun  *
20*4882a593Smuzhiyun  * There are 3 different solutions: -1 = software, 0 = hardware-crossconnect
21*4882a593Smuzhiyun  * 1-n = hardware-conference. The n will give the conference number.
22*4882a593Smuzhiyun  *
23*4882a593Smuzhiyun  * Depending on the change after removal or insertion of a party, hardware
24*4882a593Smuzhiyun  * commands are given.
25*4882a593Smuzhiyun  *
26*4882a593Smuzhiyun  * The current solution is stored within the struct dsp_conf entry.
27*4882a593Smuzhiyun  */
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun /*
30*4882a593Smuzhiyun  * HOW THE CMX WORKS:
31*4882a593Smuzhiyun  *
32*4882a593Smuzhiyun  * There are 3 types of interaction: One member is alone, in this case only
33*4882a593Smuzhiyun  * data flow from upper to lower layer is done.
34*4882a593Smuzhiyun  * Two members will also exchange their data so they are crossconnected.
35*4882a593Smuzhiyun  * Three or more members will be added in a conference and will hear each
36*4882a593Smuzhiyun  * other but will not receive their own speech (echo) if not enabled.
37*4882a593Smuzhiyun  *
38*4882a593Smuzhiyun  * Features of CMX are:
39*4882a593Smuzhiyun  *  - Crossconnecting or even conference, if more than two members are together.
40*4882a593Smuzhiyun  *  - Force mixing of transmit data with other crossconnect/conference members.
41*4882a593Smuzhiyun  *  - Echo generation to benchmark the delay of audio processing.
42*4882a593Smuzhiyun  *  - Use hardware to minimize cpu load, disable FIFO load and minimize delay.
43*4882a593Smuzhiyun  *  - Dejittering and clock generation.
44*4882a593Smuzhiyun  *
45*4882a593Smuzhiyun  * There are 2 buffers:
46*4882a593Smuzhiyun  *
47*4882a593Smuzhiyun  *
48*4882a593Smuzhiyun  * RX-Buffer
49*4882a593Smuzhiyun  *                 R             W
50*4882a593Smuzhiyun  *                 |             |
51*4882a593Smuzhiyun  * ----------------+-------------+-------------------
52*4882a593Smuzhiyun  *
53*4882a593Smuzhiyun  * The rx-buffer is a ring buffer used to store the received data for each
54*4882a593Smuzhiyun  * individual member. This is only the case if data needs to be dejittered
55*4882a593Smuzhiyun  * or in case of a conference where different clocks require reclocking.
56*4882a593Smuzhiyun  * The transmit-clock (R) will read the buffer.
57*4882a593Smuzhiyun  * If the clock overruns the write-pointer, we will have a buffer underrun.
58*4882a593Smuzhiyun  * If the write pointer always has a certain distance from the transmit-
59*4882a593Smuzhiyun  * clock, we will have a delay. The delay will dynamically be increased and
60*4882a593Smuzhiyun  * reduced.
61*4882a593Smuzhiyun  *
62*4882a593Smuzhiyun  *
63*4882a593Smuzhiyun  * TX-Buffer
64*4882a593Smuzhiyun  *                  R        W
65*4882a593Smuzhiyun  *                  |        |
66*4882a593Smuzhiyun  * -----------------+--------+-----------------------
67*4882a593Smuzhiyun  *
68*4882a593Smuzhiyun  * The tx-buffer is a ring buffer to queue the transmit data from user space
69*4882a593Smuzhiyun  * until it will be mixed or sent. There are two pointers, R and W. If the write
70*4882a593Smuzhiyun  * pointer W would reach or overrun R, the buffer would overrun. In this case
71*4882a593Smuzhiyun  * (some) data is dropped so that it will not overrun.
72*4882a593Smuzhiyun  * Additionally a dynamic dejittering can be enabled. this allows data from
73*4882a593Smuzhiyun  * user space that have jitter and different clock source.
74*4882a593Smuzhiyun  *
75*4882a593Smuzhiyun  *
76*4882a593Smuzhiyun  * Clock:
77*4882a593Smuzhiyun  *
78*4882a593Smuzhiyun  * A Clock is not required, if the data source has exactly one clock. In this
79*4882a593Smuzhiyun  * case the data source is forwarded to the destination.
80*4882a593Smuzhiyun  *
81*4882a593Smuzhiyun  * A Clock is required, because the data source
82*4882a593Smuzhiyun  *  - has multiple clocks.
83*4882a593Smuzhiyun  *  - has no usable clock due to jitter or packet loss (VoIP).
84*4882a593Smuzhiyun  * In this case the system's clock is used. The clock resolution depends on
85*4882a593Smuzhiyun  * the jiffie resolution.
86*4882a593Smuzhiyun  *
87*4882a593Smuzhiyun  * If a member joins a conference:
88*4882a593Smuzhiyun  *
89*4882a593Smuzhiyun  * - If a member joins, its rx_buff is set to silence and change read pointer
90*4882a593Smuzhiyun  *   to transmit clock.
91*4882a593Smuzhiyun  *
92*4882a593Smuzhiyun  * The procedure of received data from card is explained in cmx_receive.
93*4882a593Smuzhiyun  * The procedure of received data from user space is explained in cmx_transmit.
94*4882a593Smuzhiyun  * The procedure of transmit data to card is cmx_send.
95*4882a593Smuzhiyun  *
96*4882a593Smuzhiyun  *
97*4882a593Smuzhiyun  * Interaction with other features:
98*4882a593Smuzhiyun  *
99*4882a593Smuzhiyun  * DTMF:
100*4882a593Smuzhiyun  * DTMF decoding is done before the data is crossconnected.
101*4882a593Smuzhiyun  *
102*4882a593Smuzhiyun  * Volume change:
103*4882a593Smuzhiyun  * Changing rx-volume is done before the data is crossconnected. The tx-volume
104*4882a593Smuzhiyun  * must be changed whenever data is transmitted to the card by the cmx.
105*4882a593Smuzhiyun  *
106*4882a593Smuzhiyun  * Tones:
107*4882a593Smuzhiyun  * If a tone is enabled, it will be processed whenever data is transmitted to
108*4882a593Smuzhiyun  * the card. It will replace the tx-data from the user space.
109*4882a593Smuzhiyun  * If tones are generated by hardware, this conference member is removed for
110*4882a593Smuzhiyun  * this time.
111*4882a593Smuzhiyun  *
112*4882a593Smuzhiyun  * Disable rx-data:
113*4882a593Smuzhiyun  * If cmx is realized in hardware, rx data will be disabled if requested by
114*4882a593Smuzhiyun  * the upper layer. If dtmf decoding is done by software and enabled, rx data
115*4882a593Smuzhiyun  * will not be disabled but blocked to the upper layer.
116*4882a593Smuzhiyun  *
117*4882a593Smuzhiyun  * HFC conference engine:
118*4882a593Smuzhiyun  * If it is possible to realize all features using hardware, hardware will be
119*4882a593Smuzhiyun  * used if not forbidden by control command. Disabling rx-data provides
120*4882a593Smuzhiyun  * absolutely traffic free audio processing. (except for the quick 1-frame
121*4882a593Smuzhiyun  * upload of a tone loop, only once for a new tone)
122*4882a593Smuzhiyun  *
123*4882a593Smuzhiyun  */
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun /* delay.h is required for hw_lock.h */
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun #include <linux/slab.h>
128*4882a593Smuzhiyun #include <linux/delay.h>
129*4882a593Smuzhiyun #include <linux/mISDNif.h>
130*4882a593Smuzhiyun #include <linux/mISDNdsp.h>
131*4882a593Smuzhiyun #include "core.h"
132*4882a593Smuzhiyun #include "dsp.h"
133*4882a593Smuzhiyun /*
134*4882a593Smuzhiyun  * debugging of multi party conference,
135*4882a593Smuzhiyun  * by using conference even with two members
136*4882a593Smuzhiyun  */
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun /* #define CMX_CONF_DEBUG */
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun /*#define CMX_DEBUG * massive read/write pointer output */
141*4882a593Smuzhiyun /*#define CMX_DELAY_DEBUG * gives rx-buffer delay overview */
142*4882a593Smuzhiyun /*#define CMX_TX_DEBUG * massive read/write on tx-buffer with content */
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun static inline int
count_list_member(struct list_head * head)145*4882a593Smuzhiyun count_list_member(struct list_head *head)
146*4882a593Smuzhiyun {
147*4882a593Smuzhiyun 	int			cnt = 0;
148*4882a593Smuzhiyun 	struct list_head	*m;
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun 	list_for_each(m, head)
151*4882a593Smuzhiyun 		cnt++;
152*4882a593Smuzhiyun 	return cnt;
153*4882a593Smuzhiyun }
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun /*
156*4882a593Smuzhiyun  * debug cmx memory structure
157*4882a593Smuzhiyun  */
158*4882a593Smuzhiyun void
dsp_cmx_debug(struct dsp * dsp)159*4882a593Smuzhiyun dsp_cmx_debug(struct dsp *dsp)
160*4882a593Smuzhiyun {
161*4882a593Smuzhiyun 	struct dsp_conf	*conf;
162*4882a593Smuzhiyun 	struct dsp_conf_member	*member;
163*4882a593Smuzhiyun 	struct dsp		*odsp;
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	printk(KERN_DEBUG "-----Current DSP\n");
166*4882a593Smuzhiyun 	list_for_each_entry(odsp, &dsp_ilist, list) {
167*4882a593Smuzhiyun 		printk(KERN_DEBUG "* %s hardecho=%d softecho=%d txmix=%d",
168*4882a593Smuzhiyun 		       odsp->name, odsp->echo.hardware, odsp->echo.software,
169*4882a593Smuzhiyun 		       odsp->tx_mix);
170*4882a593Smuzhiyun 		if (odsp->conf)
171*4882a593Smuzhiyun 			printk(" (Conf %d)", odsp->conf->id);
172*4882a593Smuzhiyun 		if (dsp == odsp)
173*4882a593Smuzhiyun 			printk(" *this*");
174*4882a593Smuzhiyun 		printk("\n");
175*4882a593Smuzhiyun 	}
176*4882a593Smuzhiyun 	printk(KERN_DEBUG "-----Current Conf:\n");
177*4882a593Smuzhiyun 	list_for_each_entry(conf, &conf_ilist, list) {
178*4882a593Smuzhiyun 		printk(KERN_DEBUG "* Conf %d (%p)\n", conf->id, conf);
179*4882a593Smuzhiyun 		list_for_each_entry(member, &conf->mlist, list) {
180*4882a593Smuzhiyun 			printk(KERN_DEBUG
181*4882a593Smuzhiyun 			       "  - member = %s (slot_tx %d, bank_tx %d, "
182*4882a593Smuzhiyun 			       "slot_rx %d, bank_rx %d hfc_conf %d "
183*4882a593Smuzhiyun 			       "tx_data %d rx_is_off %d)%s\n",
184*4882a593Smuzhiyun 			       member->dsp->name, member->dsp->pcm_slot_tx,
185*4882a593Smuzhiyun 			       member->dsp->pcm_bank_tx, member->dsp->pcm_slot_rx,
186*4882a593Smuzhiyun 			       member->dsp->pcm_bank_rx, member->dsp->hfc_conf,
187*4882a593Smuzhiyun 			       member->dsp->tx_data, member->dsp->rx_is_off,
188*4882a593Smuzhiyun 			       (member->dsp == dsp) ? " *this*" : "");
189*4882a593Smuzhiyun 		}
190*4882a593Smuzhiyun 	}
191*4882a593Smuzhiyun 	printk(KERN_DEBUG "-----end\n");
192*4882a593Smuzhiyun }
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun /*
195*4882a593Smuzhiyun  * search conference
196*4882a593Smuzhiyun  */
197*4882a593Smuzhiyun static struct dsp_conf *
dsp_cmx_search_conf(u32 id)198*4882a593Smuzhiyun dsp_cmx_search_conf(u32 id)
199*4882a593Smuzhiyun {
200*4882a593Smuzhiyun 	struct dsp_conf *conf;
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun 	if (!id) {
203*4882a593Smuzhiyun 		printk(KERN_WARNING "%s: conference ID is 0.\n", __func__);
204*4882a593Smuzhiyun 		return NULL;
205*4882a593Smuzhiyun 	}
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 	/* search conference */
208*4882a593Smuzhiyun 	list_for_each_entry(conf, &conf_ilist, list)
209*4882a593Smuzhiyun 		if (conf->id == id)
210*4882a593Smuzhiyun 			return conf;
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun 	return NULL;
213*4882a593Smuzhiyun }
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun /*
217*4882a593Smuzhiyun  * add member to conference
218*4882a593Smuzhiyun  */
219*4882a593Smuzhiyun static int
dsp_cmx_add_conf_member(struct dsp * dsp,struct dsp_conf * conf)220*4882a593Smuzhiyun dsp_cmx_add_conf_member(struct dsp *dsp, struct dsp_conf *conf)
221*4882a593Smuzhiyun {
222*4882a593Smuzhiyun 	struct dsp_conf_member *member;
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun 	if (!conf || !dsp) {
225*4882a593Smuzhiyun 		printk(KERN_WARNING "%s: conf or dsp is 0.\n", __func__);
226*4882a593Smuzhiyun 		return -EINVAL;
227*4882a593Smuzhiyun 	}
228*4882a593Smuzhiyun 	if (dsp->member) {
229*4882a593Smuzhiyun 		printk(KERN_WARNING "%s: dsp is already member in a conf.\n",
230*4882a593Smuzhiyun 		       __func__);
231*4882a593Smuzhiyun 		return -EINVAL;
232*4882a593Smuzhiyun 	}
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun 	if (dsp->conf) {
235*4882a593Smuzhiyun 		printk(KERN_WARNING "%s: dsp is already in a conf.\n",
236*4882a593Smuzhiyun 		       __func__);
237*4882a593Smuzhiyun 		return -EINVAL;
238*4882a593Smuzhiyun 	}
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun 	member = kzalloc(sizeof(struct dsp_conf_member), GFP_ATOMIC);
241*4882a593Smuzhiyun 	if (!member) {
242*4882a593Smuzhiyun 		printk(KERN_ERR "kzalloc struct dsp_conf_member failed\n");
243*4882a593Smuzhiyun 		return -ENOMEM;
244*4882a593Smuzhiyun 	}
245*4882a593Smuzhiyun 	member->dsp = dsp;
246*4882a593Smuzhiyun 	/* clear rx buffer */
247*4882a593Smuzhiyun 	memset(dsp->rx_buff, dsp_silence, sizeof(dsp->rx_buff));
248*4882a593Smuzhiyun 	dsp->rx_init = 1; /* rx_W and rx_R will be adjusted on first frame */
249*4882a593Smuzhiyun 	dsp->rx_W = 0;
250*4882a593Smuzhiyun 	dsp->rx_R = 0;
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun 	list_add_tail(&member->list, &conf->mlist);
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun 	dsp->conf = conf;
255*4882a593Smuzhiyun 	dsp->member = member;
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun 	return 0;
258*4882a593Smuzhiyun }
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun /*
262*4882a593Smuzhiyun  * del member from conference
263*4882a593Smuzhiyun  */
264*4882a593Smuzhiyun int
dsp_cmx_del_conf_member(struct dsp * dsp)265*4882a593Smuzhiyun dsp_cmx_del_conf_member(struct dsp *dsp)
266*4882a593Smuzhiyun {
267*4882a593Smuzhiyun 	struct dsp_conf_member *member;
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 	if (!dsp) {
270*4882a593Smuzhiyun 		printk(KERN_WARNING "%s: dsp is 0.\n",
271*4882a593Smuzhiyun 		       __func__);
272*4882a593Smuzhiyun 		return -EINVAL;
273*4882a593Smuzhiyun 	}
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun 	if (!dsp->conf) {
276*4882a593Smuzhiyun 		printk(KERN_WARNING "%s: dsp is not in a conf.\n",
277*4882a593Smuzhiyun 		       __func__);
278*4882a593Smuzhiyun 		return -EINVAL;
279*4882a593Smuzhiyun 	}
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun 	if (list_empty(&dsp->conf->mlist)) {
282*4882a593Smuzhiyun 		printk(KERN_WARNING "%s: dsp has linked an empty conf.\n",
283*4882a593Smuzhiyun 		       __func__);
284*4882a593Smuzhiyun 		return -EINVAL;
285*4882a593Smuzhiyun 	}
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun 	/* find us in conf */
288*4882a593Smuzhiyun 	list_for_each_entry(member, &dsp->conf->mlist, list) {
289*4882a593Smuzhiyun 		if (member->dsp == dsp) {
290*4882a593Smuzhiyun 			list_del(&member->list);
291*4882a593Smuzhiyun 			dsp->conf = NULL;
292*4882a593Smuzhiyun 			dsp->member = NULL;
293*4882a593Smuzhiyun 			kfree(member);
294*4882a593Smuzhiyun 			return 0;
295*4882a593Smuzhiyun 		}
296*4882a593Smuzhiyun 	}
297*4882a593Smuzhiyun 	printk(KERN_WARNING
298*4882a593Smuzhiyun 	       "%s: dsp is not present in its own conf_member list.\n",
299*4882a593Smuzhiyun 	       __func__);
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun 	return -EINVAL;
302*4882a593Smuzhiyun }
303*4882a593Smuzhiyun 
304*4882a593Smuzhiyun 
305*4882a593Smuzhiyun /*
306*4882a593Smuzhiyun  * new conference
307*4882a593Smuzhiyun  */
308*4882a593Smuzhiyun static struct dsp_conf
dsp_cmx_new_conf(u32 id)309*4882a593Smuzhiyun *dsp_cmx_new_conf(u32 id)
310*4882a593Smuzhiyun {
311*4882a593Smuzhiyun 	struct dsp_conf *conf;
312*4882a593Smuzhiyun 
313*4882a593Smuzhiyun 	if (!id) {
314*4882a593Smuzhiyun 		printk(KERN_WARNING "%s: id is 0.\n",
315*4882a593Smuzhiyun 		       __func__);
316*4882a593Smuzhiyun 		return NULL;
317*4882a593Smuzhiyun 	}
318*4882a593Smuzhiyun 
319*4882a593Smuzhiyun 	conf = kzalloc(sizeof(struct dsp_conf), GFP_ATOMIC);
320*4882a593Smuzhiyun 	if (!conf) {
321*4882a593Smuzhiyun 		printk(KERN_ERR "kzalloc struct dsp_conf failed\n");
322*4882a593Smuzhiyun 		return NULL;
323*4882a593Smuzhiyun 	}
324*4882a593Smuzhiyun 	INIT_LIST_HEAD(&conf->mlist);
325*4882a593Smuzhiyun 	conf->id = id;
326*4882a593Smuzhiyun 
327*4882a593Smuzhiyun 	list_add_tail(&conf->list, &conf_ilist);
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun 	return conf;
330*4882a593Smuzhiyun }
331*4882a593Smuzhiyun 
332*4882a593Smuzhiyun 
333*4882a593Smuzhiyun /*
334*4882a593Smuzhiyun  * del conference
335*4882a593Smuzhiyun  */
336*4882a593Smuzhiyun int
dsp_cmx_del_conf(struct dsp_conf * conf)337*4882a593Smuzhiyun dsp_cmx_del_conf(struct dsp_conf *conf)
338*4882a593Smuzhiyun {
339*4882a593Smuzhiyun 	if (!conf) {
340*4882a593Smuzhiyun 		printk(KERN_WARNING "%s: conf is null.\n",
341*4882a593Smuzhiyun 		       __func__);
342*4882a593Smuzhiyun 		return -EINVAL;
343*4882a593Smuzhiyun 	}
344*4882a593Smuzhiyun 
345*4882a593Smuzhiyun 	if (!list_empty(&conf->mlist)) {
346*4882a593Smuzhiyun 		printk(KERN_WARNING "%s: conf not empty.\n",
347*4882a593Smuzhiyun 		       __func__);
348*4882a593Smuzhiyun 		return -EINVAL;
349*4882a593Smuzhiyun 	}
350*4882a593Smuzhiyun 	list_del(&conf->list);
351*4882a593Smuzhiyun 	kfree(conf);
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun 	return 0;
354*4882a593Smuzhiyun }
355*4882a593Smuzhiyun 
356*4882a593Smuzhiyun 
357*4882a593Smuzhiyun /*
358*4882a593Smuzhiyun  * send HW message to hfc card
359*4882a593Smuzhiyun  */
360*4882a593Smuzhiyun static void
dsp_cmx_hw_message(struct dsp * dsp,u32 message,u32 param1,u32 param2,u32 param3,u32 param4)361*4882a593Smuzhiyun dsp_cmx_hw_message(struct dsp *dsp, u32 message, u32 param1, u32 param2,
362*4882a593Smuzhiyun 		   u32 param3, u32 param4)
363*4882a593Smuzhiyun {
364*4882a593Smuzhiyun 	struct mISDN_ctrl_req cq;
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun 	memset(&cq, 0, sizeof(cq));
367*4882a593Smuzhiyun 	cq.op = message;
368*4882a593Smuzhiyun 	cq.p1 = param1 | (param2 << 8);
369*4882a593Smuzhiyun 	cq.p2 = param3 | (param4 << 8);
370*4882a593Smuzhiyun 	if (dsp->ch.peer)
371*4882a593Smuzhiyun 		dsp->ch.peer->ctrl(dsp->ch.peer, CONTROL_CHANNEL, &cq);
372*4882a593Smuzhiyun }
373*4882a593Smuzhiyun 
374*4882a593Smuzhiyun 
375*4882a593Smuzhiyun /*
376*4882a593Smuzhiyun  * do hardware update and set the software/hardware flag
377*4882a593Smuzhiyun  *
378*4882a593Smuzhiyun  * either a conference or a dsp instance can be given
379*4882a593Smuzhiyun  * if only dsp instance is given, the instance is not associated with a conf
380*4882a593Smuzhiyun  * and therefore removed. if a conference is given, the dsp is expected to
381*4882a593Smuzhiyun  * be member of that conference.
382*4882a593Smuzhiyun  */
383*4882a593Smuzhiyun void
dsp_cmx_hardware(struct dsp_conf * conf,struct dsp * dsp)384*4882a593Smuzhiyun dsp_cmx_hardware(struct dsp_conf *conf, struct dsp *dsp)
385*4882a593Smuzhiyun {
386*4882a593Smuzhiyun 	struct dsp_conf_member	*member, *nextm;
387*4882a593Smuzhiyun 	struct dsp		*finddsp;
388*4882a593Smuzhiyun 	int		memb = 0, i, ii, i1, i2;
389*4882a593Smuzhiyun 	int		freeunits[8];
390*4882a593Smuzhiyun 	u_char		freeslots[256];
391*4882a593Smuzhiyun 	int		same_hfc = -1, same_pcm = -1, current_conf = -1,
392*4882a593Smuzhiyun 		all_conf = 1, tx_data = 0;
393*4882a593Smuzhiyun 
394*4882a593Smuzhiyun 	/* dsp gets updated (no conf) */
395*4882a593Smuzhiyun 	if (!conf) {
396*4882a593Smuzhiyun 		if (!dsp)
397*4882a593Smuzhiyun 			return;
398*4882a593Smuzhiyun 		if (dsp_debug & DEBUG_DSP_CMX)
399*4882a593Smuzhiyun 			printk(KERN_DEBUG "%s checking dsp %s\n",
400*4882a593Smuzhiyun 			       __func__, dsp->name);
401*4882a593Smuzhiyun 	one_member:
402*4882a593Smuzhiyun 		/* remove HFC conference if enabled */
403*4882a593Smuzhiyun 		if (dsp->hfc_conf >= 0) {
404*4882a593Smuzhiyun 			if (dsp_debug & DEBUG_DSP_CMX)
405*4882a593Smuzhiyun 				printk(KERN_DEBUG
406*4882a593Smuzhiyun 				       "%s removing %s from HFC conf %d "
407*4882a593Smuzhiyun 				       "because dsp is split\n", __func__,
408*4882a593Smuzhiyun 				       dsp->name, dsp->hfc_conf);
409*4882a593Smuzhiyun 			dsp_cmx_hw_message(dsp, MISDN_CTRL_HFC_CONF_SPLIT,
410*4882a593Smuzhiyun 					   0, 0, 0, 0);
411*4882a593Smuzhiyun 			dsp->hfc_conf = -1;
412*4882a593Smuzhiyun 		}
413*4882a593Smuzhiyun 		/* process hw echo */
414*4882a593Smuzhiyun 		if (dsp->features.pcm_banks < 1)
415*4882a593Smuzhiyun 			return;
416*4882a593Smuzhiyun 		if (!dsp->echo.software && !dsp->echo.hardware) {
417*4882a593Smuzhiyun 			/* NO ECHO: remove PCM slot if assigned */
418*4882a593Smuzhiyun 			if (dsp->pcm_slot_tx >= 0 || dsp->pcm_slot_rx >= 0) {
419*4882a593Smuzhiyun 				if (dsp_debug & DEBUG_DSP_CMX)
420*4882a593Smuzhiyun 					printk(KERN_DEBUG "%s removing %s from"
421*4882a593Smuzhiyun 					       " PCM slot %d (TX) %d (RX) because"
422*4882a593Smuzhiyun 					       " dsp is split (no echo)\n",
423*4882a593Smuzhiyun 					       __func__, dsp->name,
424*4882a593Smuzhiyun 					       dsp->pcm_slot_tx, dsp->pcm_slot_rx);
425*4882a593Smuzhiyun 				dsp_cmx_hw_message(dsp, MISDN_CTRL_HFC_PCM_DISC,
426*4882a593Smuzhiyun 						   0, 0, 0, 0);
427*4882a593Smuzhiyun 				dsp->pcm_slot_tx = -1;
428*4882a593Smuzhiyun 				dsp->pcm_bank_tx = -1;
429*4882a593Smuzhiyun 				dsp->pcm_slot_rx = -1;
430*4882a593Smuzhiyun 				dsp->pcm_bank_rx = -1;
431*4882a593Smuzhiyun 			}
432*4882a593Smuzhiyun 			return;
433*4882a593Smuzhiyun 		}
434*4882a593Smuzhiyun 		/* echo is enabled, find out if we use soft or hardware */
435*4882a593Smuzhiyun 		dsp->echo.software = dsp->tx_data;
436*4882a593Smuzhiyun 		dsp->echo.hardware = 0;
437*4882a593Smuzhiyun 		/* ECHO: already echo */
438*4882a593Smuzhiyun 		if (dsp->pcm_slot_tx >= 0 && dsp->pcm_slot_rx < 0 &&
439*4882a593Smuzhiyun 		    dsp->pcm_bank_tx == 2 && dsp->pcm_bank_rx == 2) {
440*4882a593Smuzhiyun 			dsp->echo.hardware = 1;
441*4882a593Smuzhiyun 			return;
442*4882a593Smuzhiyun 		}
443*4882a593Smuzhiyun 		/* ECHO: if slot already assigned */
444*4882a593Smuzhiyun 		if (dsp->pcm_slot_tx >= 0) {
445*4882a593Smuzhiyun 			dsp->pcm_slot_rx = dsp->pcm_slot_tx;
446*4882a593Smuzhiyun 			dsp->pcm_bank_tx = 2; /* 2 means loop */
447*4882a593Smuzhiyun 			dsp->pcm_bank_rx = 2;
448*4882a593Smuzhiyun 			if (dsp_debug & DEBUG_DSP_CMX)
449*4882a593Smuzhiyun 				printk(KERN_DEBUG
450*4882a593Smuzhiyun 				       "%s refresh %s for echo using slot %d\n",
451*4882a593Smuzhiyun 				       __func__, dsp->name,
452*4882a593Smuzhiyun 				       dsp->pcm_slot_tx);
453*4882a593Smuzhiyun 			dsp_cmx_hw_message(dsp, MISDN_CTRL_HFC_PCM_CONN,
454*4882a593Smuzhiyun 					   dsp->pcm_slot_tx, 2, dsp->pcm_slot_rx, 2);
455*4882a593Smuzhiyun 			dsp->echo.hardware = 1;
456*4882a593Smuzhiyun 			return;
457*4882a593Smuzhiyun 		}
458*4882a593Smuzhiyun 		/* ECHO: find slot */
459*4882a593Smuzhiyun 		dsp->pcm_slot_tx = -1;
460*4882a593Smuzhiyun 		dsp->pcm_slot_rx = -1;
461*4882a593Smuzhiyun 		memset(freeslots, 1, sizeof(freeslots));
462*4882a593Smuzhiyun 		list_for_each_entry(finddsp, &dsp_ilist, list) {
463*4882a593Smuzhiyun 			if (finddsp->features.pcm_id == dsp->features.pcm_id) {
464*4882a593Smuzhiyun 				if (finddsp->pcm_slot_rx >= 0 &&
465*4882a593Smuzhiyun 				    finddsp->pcm_slot_rx < sizeof(freeslots))
466*4882a593Smuzhiyun 					freeslots[finddsp->pcm_slot_rx] = 0;
467*4882a593Smuzhiyun 				if (finddsp->pcm_slot_tx >= 0 &&
468*4882a593Smuzhiyun 				    finddsp->pcm_slot_tx < sizeof(freeslots))
469*4882a593Smuzhiyun 					freeslots[finddsp->pcm_slot_tx] = 0;
470*4882a593Smuzhiyun 			}
471*4882a593Smuzhiyun 		}
472*4882a593Smuzhiyun 		i = 0;
473*4882a593Smuzhiyun 		ii = dsp->features.pcm_slots;
474*4882a593Smuzhiyun 		while (i < ii) {
475*4882a593Smuzhiyun 			if (freeslots[i])
476*4882a593Smuzhiyun 				break;
477*4882a593Smuzhiyun 			i++;
478*4882a593Smuzhiyun 		}
479*4882a593Smuzhiyun 		if (i == ii) {
480*4882a593Smuzhiyun 			if (dsp_debug & DEBUG_DSP_CMX)
481*4882a593Smuzhiyun 				printk(KERN_DEBUG
482*4882a593Smuzhiyun 				       "%s no slot available for echo\n",
483*4882a593Smuzhiyun 				       __func__);
484*4882a593Smuzhiyun 			/* no more slots available */
485*4882a593Smuzhiyun 			dsp->echo.software = 1;
486*4882a593Smuzhiyun 			return;
487*4882a593Smuzhiyun 		}
488*4882a593Smuzhiyun 		/* assign free slot */
489*4882a593Smuzhiyun 		dsp->pcm_slot_tx = i;
490*4882a593Smuzhiyun 		dsp->pcm_slot_rx = i;
491*4882a593Smuzhiyun 		dsp->pcm_bank_tx = 2; /* loop */
492*4882a593Smuzhiyun 		dsp->pcm_bank_rx = 2;
493*4882a593Smuzhiyun 		if (dsp_debug & DEBUG_DSP_CMX)
494*4882a593Smuzhiyun 			printk(KERN_DEBUG
495*4882a593Smuzhiyun 			       "%s assign echo for %s using slot %d\n",
496*4882a593Smuzhiyun 			       __func__, dsp->name, dsp->pcm_slot_tx);
497*4882a593Smuzhiyun 		dsp_cmx_hw_message(dsp, MISDN_CTRL_HFC_PCM_CONN,
498*4882a593Smuzhiyun 				   dsp->pcm_slot_tx, 2, dsp->pcm_slot_rx, 2);
499*4882a593Smuzhiyun 		dsp->echo.hardware = 1;
500*4882a593Smuzhiyun 		return;
501*4882a593Smuzhiyun 	}
502*4882a593Smuzhiyun 
503*4882a593Smuzhiyun 	/* conf gets updated (all members) */
504*4882a593Smuzhiyun 	if (dsp_debug & DEBUG_DSP_CMX)
505*4882a593Smuzhiyun 		printk(KERN_DEBUG "%s checking conference %d\n",
506*4882a593Smuzhiyun 		       __func__, conf->id);
507*4882a593Smuzhiyun 
508*4882a593Smuzhiyun 	if (list_empty(&conf->mlist)) {
509*4882a593Smuzhiyun 		printk(KERN_ERR "%s: conference without members\n",
510*4882a593Smuzhiyun 		       __func__);
511*4882a593Smuzhiyun 		return;
512*4882a593Smuzhiyun 	}
513*4882a593Smuzhiyun 	member = list_entry(conf->mlist.next, struct dsp_conf_member, list);
514*4882a593Smuzhiyun 	same_hfc = member->dsp->features.hfc_id;
515*4882a593Smuzhiyun 	same_pcm = member->dsp->features.pcm_id;
516*4882a593Smuzhiyun 	/* check all members in our conference */
517*4882a593Smuzhiyun 	list_for_each_entry(member, &conf->mlist, list) {
518*4882a593Smuzhiyun 		/* check if member uses mixing */
519*4882a593Smuzhiyun 		if (member->dsp->tx_mix) {
520*4882a593Smuzhiyun 			if (dsp_debug & DEBUG_DSP_CMX)
521*4882a593Smuzhiyun 				printk(KERN_DEBUG
522*4882a593Smuzhiyun 				       "%s dsp %s cannot form a conf, because "
523*4882a593Smuzhiyun 				       "tx_mix is turned on\n", __func__,
524*4882a593Smuzhiyun 				       member->dsp->name);
525*4882a593Smuzhiyun 		conf_software:
526*4882a593Smuzhiyun 			list_for_each_entry(member, &conf->mlist, list) {
527*4882a593Smuzhiyun 				dsp = member->dsp;
528*4882a593Smuzhiyun 				/* remove HFC conference if enabled */
529*4882a593Smuzhiyun 				if (dsp->hfc_conf >= 0) {
530*4882a593Smuzhiyun 					if (dsp_debug & DEBUG_DSP_CMX)
531*4882a593Smuzhiyun 						printk(KERN_DEBUG
532*4882a593Smuzhiyun 						       "%s removing %s from HFC "
533*4882a593Smuzhiyun 						       "conf %d because not "
534*4882a593Smuzhiyun 						       "possible with hardware\n",
535*4882a593Smuzhiyun 						       __func__,
536*4882a593Smuzhiyun 						       dsp->name,
537*4882a593Smuzhiyun 						       dsp->hfc_conf);
538*4882a593Smuzhiyun 					dsp_cmx_hw_message(dsp,
539*4882a593Smuzhiyun 							   MISDN_CTRL_HFC_CONF_SPLIT,
540*4882a593Smuzhiyun 							   0, 0, 0, 0);
541*4882a593Smuzhiyun 					dsp->hfc_conf = -1;
542*4882a593Smuzhiyun 				}
543*4882a593Smuzhiyun 				/* remove PCM slot if assigned */
544*4882a593Smuzhiyun 				if (dsp->pcm_slot_tx >= 0 ||
545*4882a593Smuzhiyun 				    dsp->pcm_slot_rx >= 0) {
546*4882a593Smuzhiyun 					if (dsp_debug & DEBUG_DSP_CMX)
547*4882a593Smuzhiyun 						printk(KERN_DEBUG "%s removing "
548*4882a593Smuzhiyun 						       "%s from PCM slot %d (TX)"
549*4882a593Smuzhiyun 						       " slot %d (RX) because not"
550*4882a593Smuzhiyun 						       " possible with hardware\n",
551*4882a593Smuzhiyun 						       __func__,
552*4882a593Smuzhiyun 						       dsp->name,
553*4882a593Smuzhiyun 						       dsp->pcm_slot_tx,
554*4882a593Smuzhiyun 						       dsp->pcm_slot_rx);
555*4882a593Smuzhiyun 					dsp_cmx_hw_message(dsp,
556*4882a593Smuzhiyun 							   MISDN_CTRL_HFC_PCM_DISC,
557*4882a593Smuzhiyun 							   0, 0, 0, 0);
558*4882a593Smuzhiyun 					dsp->pcm_slot_tx = -1;
559*4882a593Smuzhiyun 					dsp->pcm_bank_tx = -1;
560*4882a593Smuzhiyun 					dsp->pcm_slot_rx = -1;
561*4882a593Smuzhiyun 					dsp->pcm_bank_rx = -1;
562*4882a593Smuzhiyun 				}
563*4882a593Smuzhiyun 			}
564*4882a593Smuzhiyun 			conf->hardware = 0;
565*4882a593Smuzhiyun 			conf->software = 1;
566*4882a593Smuzhiyun 			return;
567*4882a593Smuzhiyun 		}
568*4882a593Smuzhiyun 		/* check if member has echo turned on */
569*4882a593Smuzhiyun 		if (member->dsp->echo.hardware || member->dsp->echo.software) {
570*4882a593Smuzhiyun 			if (dsp_debug & DEBUG_DSP_CMX)
571*4882a593Smuzhiyun 				printk(KERN_DEBUG
572*4882a593Smuzhiyun 				       "%s dsp %s cannot form a conf, because "
573*4882a593Smuzhiyun 				       "echo is turned on\n", __func__,
574*4882a593Smuzhiyun 				       member->dsp->name);
575*4882a593Smuzhiyun 			goto conf_software;
576*4882a593Smuzhiyun 		}
577*4882a593Smuzhiyun 		/* check if member has tx_mix turned on */
578*4882a593Smuzhiyun 		if (member->dsp->tx_mix) {
579*4882a593Smuzhiyun 			if (dsp_debug & DEBUG_DSP_CMX)
580*4882a593Smuzhiyun 				printk(KERN_DEBUG
581*4882a593Smuzhiyun 				       "%s dsp %s cannot form a conf, because "
582*4882a593Smuzhiyun 				       "tx_mix is turned on\n",
583*4882a593Smuzhiyun 				       __func__, member->dsp->name);
584*4882a593Smuzhiyun 			goto conf_software;
585*4882a593Smuzhiyun 		}
586*4882a593Smuzhiyun 		/* check if member changes volume at an not suppoted level */
587*4882a593Smuzhiyun 		if (member->dsp->tx_volume) {
588*4882a593Smuzhiyun 			if (dsp_debug & DEBUG_DSP_CMX)
589*4882a593Smuzhiyun 				printk(KERN_DEBUG
590*4882a593Smuzhiyun 				       "%s dsp %s cannot form a conf, because "
591*4882a593Smuzhiyun 				       "tx_volume is changed\n",
592*4882a593Smuzhiyun 				       __func__, member->dsp->name);
593*4882a593Smuzhiyun 			goto conf_software;
594*4882a593Smuzhiyun 		}
595*4882a593Smuzhiyun 		if (member->dsp->rx_volume) {
596*4882a593Smuzhiyun 			if (dsp_debug & DEBUG_DSP_CMX)
597*4882a593Smuzhiyun 				printk(KERN_DEBUG
598*4882a593Smuzhiyun 				       "%s dsp %s cannot form a conf, because "
599*4882a593Smuzhiyun 				       "rx_volume is changed\n",
600*4882a593Smuzhiyun 				       __func__, member->dsp->name);
601*4882a593Smuzhiyun 			goto conf_software;
602*4882a593Smuzhiyun 		}
603*4882a593Smuzhiyun 		/* check if tx-data turned on */
604*4882a593Smuzhiyun 		if (member->dsp->tx_data) {
605*4882a593Smuzhiyun 			if (dsp_debug & DEBUG_DSP_CMX)
606*4882a593Smuzhiyun 				printk(KERN_DEBUG
607*4882a593Smuzhiyun 				       "%s dsp %s tx_data is turned on\n",
608*4882a593Smuzhiyun 				       __func__, member->dsp->name);
609*4882a593Smuzhiyun 			tx_data = 1;
610*4882a593Smuzhiyun 		}
611*4882a593Smuzhiyun 		/* check if pipeline exists */
612*4882a593Smuzhiyun 		if (member->dsp->pipeline.inuse) {
613*4882a593Smuzhiyun 			if (dsp_debug & DEBUG_DSP_CMX)
614*4882a593Smuzhiyun 				printk(KERN_DEBUG
615*4882a593Smuzhiyun 				       "%s dsp %s cannot form a conf, because "
616*4882a593Smuzhiyun 				       "pipeline exists\n", __func__,
617*4882a593Smuzhiyun 				       member->dsp->name);
618*4882a593Smuzhiyun 			goto conf_software;
619*4882a593Smuzhiyun 		}
620*4882a593Smuzhiyun 		/* check if encryption is enabled */
621*4882a593Smuzhiyun 		if (member->dsp->bf_enable) {
622*4882a593Smuzhiyun 			if (dsp_debug & DEBUG_DSP_CMX)
623*4882a593Smuzhiyun 				printk(KERN_DEBUG "%s dsp %s cannot form a "
624*4882a593Smuzhiyun 				       "conf, because encryption is enabled\n",
625*4882a593Smuzhiyun 				       __func__, member->dsp->name);
626*4882a593Smuzhiyun 			goto conf_software;
627*4882a593Smuzhiyun 		}
628*4882a593Smuzhiyun 		/* check if member is on a card with PCM support */
629*4882a593Smuzhiyun 		if (member->dsp->features.pcm_id < 0) {
630*4882a593Smuzhiyun 			if (dsp_debug & DEBUG_DSP_CMX)
631*4882a593Smuzhiyun 				printk(KERN_DEBUG
632*4882a593Smuzhiyun 				       "%s dsp %s cannot form a conf, because "
633*4882a593Smuzhiyun 				       "dsp has no PCM bus\n",
634*4882a593Smuzhiyun 				       __func__, member->dsp->name);
635*4882a593Smuzhiyun 			goto conf_software;
636*4882a593Smuzhiyun 		}
637*4882a593Smuzhiyun 		/* check if relations are on the same PCM bus */
638*4882a593Smuzhiyun 		if (member->dsp->features.pcm_id != same_pcm) {
639*4882a593Smuzhiyun 			if (dsp_debug & DEBUG_DSP_CMX)
640*4882a593Smuzhiyun 				printk(KERN_DEBUG
641*4882a593Smuzhiyun 				       "%s dsp %s cannot form a conf, because "
642*4882a593Smuzhiyun 				       "dsp is on a different PCM bus than the "
643*4882a593Smuzhiyun 				       "first dsp\n",
644*4882a593Smuzhiyun 				       __func__, member->dsp->name);
645*4882a593Smuzhiyun 			goto conf_software;
646*4882a593Smuzhiyun 		}
647*4882a593Smuzhiyun 		/* determine if members are on the same hfc chip */
648*4882a593Smuzhiyun 		if (same_hfc != member->dsp->features.hfc_id)
649*4882a593Smuzhiyun 			same_hfc = -1;
650*4882a593Smuzhiyun 		/* if there are members already in a conference */
651*4882a593Smuzhiyun 		if (current_conf < 0 && member->dsp->hfc_conf >= 0)
652*4882a593Smuzhiyun 			current_conf = member->dsp->hfc_conf;
653*4882a593Smuzhiyun 		/* if any member is not in a conference */
654*4882a593Smuzhiyun 		if (member->dsp->hfc_conf < 0)
655*4882a593Smuzhiyun 			all_conf = 0;
656*4882a593Smuzhiyun 
657*4882a593Smuzhiyun 		memb++;
658*4882a593Smuzhiyun 	}
659*4882a593Smuzhiyun 
660*4882a593Smuzhiyun 	/* if no member, this is an error */
661*4882a593Smuzhiyun 	if (memb < 1)
662*4882a593Smuzhiyun 		return;
663*4882a593Smuzhiyun 
664*4882a593Smuzhiyun 	/* one member */
665*4882a593Smuzhiyun 	if (memb == 1) {
666*4882a593Smuzhiyun 		if (dsp_debug & DEBUG_DSP_CMX)
667*4882a593Smuzhiyun 			printk(KERN_DEBUG
668*4882a593Smuzhiyun 			       "%s conf %d cannot form a HW conference, "
669*4882a593Smuzhiyun 			       "because dsp is alone\n", __func__, conf->id);
670*4882a593Smuzhiyun 		conf->hardware = 0;
671*4882a593Smuzhiyun 		conf->software = 0;
672*4882a593Smuzhiyun 		member = list_entry(conf->mlist.next, struct dsp_conf_member,
673*4882a593Smuzhiyun 				    list);
674*4882a593Smuzhiyun 		dsp = member->dsp;
675*4882a593Smuzhiyun 		goto one_member;
676*4882a593Smuzhiyun 	}
677*4882a593Smuzhiyun 
678*4882a593Smuzhiyun 	/*
679*4882a593Smuzhiyun 	 * ok, now we are sure that all members are on the same pcm.
680*4882a593Smuzhiyun 	 * now we will see if we have only two members, so we can do
681*4882a593Smuzhiyun 	 * crossconnections, which don't have any limitations.
682*4882a593Smuzhiyun 	 */
683*4882a593Smuzhiyun 
684*4882a593Smuzhiyun 	/* if we have only two members */
685*4882a593Smuzhiyun 	if (memb == 2) {
686*4882a593Smuzhiyun 		member = list_entry(conf->mlist.next, struct dsp_conf_member,
687*4882a593Smuzhiyun 				    list);
688*4882a593Smuzhiyun 		nextm = list_entry(member->list.next, struct dsp_conf_member,
689*4882a593Smuzhiyun 				   list);
690*4882a593Smuzhiyun 		/* remove HFC conference if enabled */
691*4882a593Smuzhiyun 		if (member->dsp->hfc_conf >= 0) {
692*4882a593Smuzhiyun 			if (dsp_debug & DEBUG_DSP_CMX)
693*4882a593Smuzhiyun 				printk(KERN_DEBUG
694*4882a593Smuzhiyun 				       "%s removing %s from HFC conf %d because "
695*4882a593Smuzhiyun 				       "two parties require only a PCM slot\n",
696*4882a593Smuzhiyun 				       __func__, member->dsp->name,
697*4882a593Smuzhiyun 				       member->dsp->hfc_conf);
698*4882a593Smuzhiyun 			dsp_cmx_hw_message(member->dsp,
699*4882a593Smuzhiyun 					   MISDN_CTRL_HFC_CONF_SPLIT, 0, 0, 0, 0);
700*4882a593Smuzhiyun 			member->dsp->hfc_conf = -1;
701*4882a593Smuzhiyun 		}
702*4882a593Smuzhiyun 		if (nextm->dsp->hfc_conf >= 0) {
703*4882a593Smuzhiyun 			if (dsp_debug & DEBUG_DSP_CMX)
704*4882a593Smuzhiyun 				printk(KERN_DEBUG
705*4882a593Smuzhiyun 				       "%s removing %s from HFC conf %d because "
706*4882a593Smuzhiyun 				       "two parties require only a PCM slot\n",
707*4882a593Smuzhiyun 				       __func__, nextm->dsp->name,
708*4882a593Smuzhiyun 				       nextm->dsp->hfc_conf);
709*4882a593Smuzhiyun 			dsp_cmx_hw_message(nextm->dsp,
710*4882a593Smuzhiyun 					   MISDN_CTRL_HFC_CONF_SPLIT, 0, 0, 0, 0);
711*4882a593Smuzhiyun 			nextm->dsp->hfc_conf = -1;
712*4882a593Smuzhiyun 		}
713*4882a593Smuzhiyun 		/* if members have two banks (and not on the same chip) */
714*4882a593Smuzhiyun 		if (member->dsp->features.pcm_banks > 1 &&
715*4882a593Smuzhiyun 		    nextm->dsp->features.pcm_banks > 1 &&
716*4882a593Smuzhiyun 		    member->dsp->features.hfc_id !=
717*4882a593Smuzhiyun 		    nextm->dsp->features.hfc_id) {
718*4882a593Smuzhiyun 			/* if both members have same slots with crossed banks */
719*4882a593Smuzhiyun 			if (member->dsp->pcm_slot_tx >= 0 &&
720*4882a593Smuzhiyun 			    member->dsp->pcm_slot_rx >= 0 &&
721*4882a593Smuzhiyun 			    nextm->dsp->pcm_slot_tx >= 0 &&
722*4882a593Smuzhiyun 			    nextm->dsp->pcm_slot_rx >= 0 &&
723*4882a593Smuzhiyun 			    nextm->dsp->pcm_slot_tx ==
724*4882a593Smuzhiyun 			    member->dsp->pcm_slot_rx &&
725*4882a593Smuzhiyun 			    nextm->dsp->pcm_slot_rx ==
726*4882a593Smuzhiyun 			    member->dsp->pcm_slot_tx &&
727*4882a593Smuzhiyun 			    nextm->dsp->pcm_slot_tx ==
728*4882a593Smuzhiyun 			    member->dsp->pcm_slot_tx &&
729*4882a593Smuzhiyun 			    member->dsp->pcm_bank_tx !=
730*4882a593Smuzhiyun 			    member->dsp->pcm_bank_rx &&
731*4882a593Smuzhiyun 			    nextm->dsp->pcm_bank_tx !=
732*4882a593Smuzhiyun 			    nextm->dsp->pcm_bank_rx) {
733*4882a593Smuzhiyun 				/* all members have same slot */
734*4882a593Smuzhiyun 				if (dsp_debug & DEBUG_DSP_CMX)
735*4882a593Smuzhiyun 					printk(KERN_DEBUG
736*4882a593Smuzhiyun 					       "%s dsp %s & %s stay joined on "
737*4882a593Smuzhiyun 					       "PCM slot %d bank %d (TX) bank %d "
738*4882a593Smuzhiyun 					       "(RX) (on different chips)\n",
739*4882a593Smuzhiyun 					       __func__,
740*4882a593Smuzhiyun 					       member->dsp->name,
741*4882a593Smuzhiyun 					       nextm->dsp->name,
742*4882a593Smuzhiyun 					       member->dsp->pcm_slot_tx,
743*4882a593Smuzhiyun 					       member->dsp->pcm_bank_tx,
744*4882a593Smuzhiyun 					       member->dsp->pcm_bank_rx);
745*4882a593Smuzhiyun 				conf->hardware = 1;
746*4882a593Smuzhiyun 				conf->software = tx_data;
747*4882a593Smuzhiyun 				return;
748*4882a593Smuzhiyun 			}
749*4882a593Smuzhiyun 			/* find a new slot */
750*4882a593Smuzhiyun 			memset(freeslots, 1, sizeof(freeslots));
751*4882a593Smuzhiyun 			list_for_each_entry(dsp, &dsp_ilist, list) {
752*4882a593Smuzhiyun 				if (dsp != member->dsp &&
753*4882a593Smuzhiyun 				    dsp != nextm->dsp &&
754*4882a593Smuzhiyun 				    member->dsp->features.pcm_id ==
755*4882a593Smuzhiyun 				    dsp->features.pcm_id) {
756*4882a593Smuzhiyun 					if (dsp->pcm_slot_rx >= 0 &&
757*4882a593Smuzhiyun 					    dsp->pcm_slot_rx <
758*4882a593Smuzhiyun 					    sizeof(freeslots))
759*4882a593Smuzhiyun 						freeslots[dsp->pcm_slot_rx] = 0;
760*4882a593Smuzhiyun 					if (dsp->pcm_slot_tx >= 0 &&
761*4882a593Smuzhiyun 					    dsp->pcm_slot_tx <
762*4882a593Smuzhiyun 					    sizeof(freeslots))
763*4882a593Smuzhiyun 						freeslots[dsp->pcm_slot_tx] = 0;
764*4882a593Smuzhiyun 				}
765*4882a593Smuzhiyun 			}
766*4882a593Smuzhiyun 			i = 0;
767*4882a593Smuzhiyun 			ii = member->dsp->features.pcm_slots;
768*4882a593Smuzhiyun 			while (i < ii) {
769*4882a593Smuzhiyun 				if (freeslots[i])
770*4882a593Smuzhiyun 					break;
771*4882a593Smuzhiyun 				i++;
772*4882a593Smuzhiyun 			}
773*4882a593Smuzhiyun 			if (i == ii) {
774*4882a593Smuzhiyun 				if (dsp_debug & DEBUG_DSP_CMX)
775*4882a593Smuzhiyun 					printk(KERN_DEBUG
776*4882a593Smuzhiyun 					       "%s no slot available for "
777*4882a593Smuzhiyun 					       "%s & %s\n", __func__,
778*4882a593Smuzhiyun 					       member->dsp->name,
779*4882a593Smuzhiyun 					       nextm->dsp->name);
780*4882a593Smuzhiyun 				/* no more slots available */
781*4882a593Smuzhiyun 				goto conf_software;
782*4882a593Smuzhiyun 			}
783*4882a593Smuzhiyun 			/* assign free slot */
784*4882a593Smuzhiyun 			member->dsp->pcm_slot_tx = i;
785*4882a593Smuzhiyun 			member->dsp->pcm_slot_rx = i;
786*4882a593Smuzhiyun 			nextm->dsp->pcm_slot_tx = i;
787*4882a593Smuzhiyun 			nextm->dsp->pcm_slot_rx = i;
788*4882a593Smuzhiyun 			member->dsp->pcm_bank_rx = 0;
789*4882a593Smuzhiyun 			member->dsp->pcm_bank_tx = 1;
790*4882a593Smuzhiyun 			nextm->dsp->pcm_bank_rx = 1;
791*4882a593Smuzhiyun 			nextm->dsp->pcm_bank_tx = 0;
792*4882a593Smuzhiyun 			if (dsp_debug & DEBUG_DSP_CMX)
793*4882a593Smuzhiyun 				printk(KERN_DEBUG
794*4882a593Smuzhiyun 				       "%s adding %s & %s to new PCM slot %d "
795*4882a593Smuzhiyun 				       "(TX and RX on different chips) because "
796*4882a593Smuzhiyun 				       "both members have not same slots\n",
797*4882a593Smuzhiyun 				       __func__,
798*4882a593Smuzhiyun 				       member->dsp->name,
799*4882a593Smuzhiyun 				       nextm->dsp->name,
800*4882a593Smuzhiyun 				       member->dsp->pcm_slot_tx);
801*4882a593Smuzhiyun 			dsp_cmx_hw_message(member->dsp, MISDN_CTRL_HFC_PCM_CONN,
802*4882a593Smuzhiyun 					   member->dsp->pcm_slot_tx, member->dsp->pcm_bank_tx,
803*4882a593Smuzhiyun 					   member->dsp->pcm_slot_rx, member->dsp->pcm_bank_rx);
804*4882a593Smuzhiyun 			dsp_cmx_hw_message(nextm->dsp, MISDN_CTRL_HFC_PCM_CONN,
805*4882a593Smuzhiyun 					   nextm->dsp->pcm_slot_tx, nextm->dsp->pcm_bank_tx,
806*4882a593Smuzhiyun 					   nextm->dsp->pcm_slot_rx, nextm->dsp->pcm_bank_rx);
807*4882a593Smuzhiyun 			conf->hardware = 1;
808*4882a593Smuzhiyun 			conf->software = tx_data;
809*4882a593Smuzhiyun 			return;
810*4882a593Smuzhiyun 			/* if members have one bank (or on the same chip) */
811*4882a593Smuzhiyun 		} else {
812*4882a593Smuzhiyun 			/* if both members have different crossed slots */
813*4882a593Smuzhiyun 			if (member->dsp->pcm_slot_tx >= 0 &&
814*4882a593Smuzhiyun 			    member->dsp->pcm_slot_rx >= 0 &&
815*4882a593Smuzhiyun 			    nextm->dsp->pcm_slot_tx >= 0 &&
816*4882a593Smuzhiyun 			    nextm->dsp->pcm_slot_rx >= 0 &&
817*4882a593Smuzhiyun 			    nextm->dsp->pcm_slot_tx ==
818*4882a593Smuzhiyun 			    member->dsp->pcm_slot_rx &&
819*4882a593Smuzhiyun 			    nextm->dsp->pcm_slot_rx ==
820*4882a593Smuzhiyun 			    member->dsp->pcm_slot_tx &&
821*4882a593Smuzhiyun 			    member->dsp->pcm_slot_tx !=
822*4882a593Smuzhiyun 			    member->dsp->pcm_slot_rx &&
823*4882a593Smuzhiyun 			    member->dsp->pcm_bank_tx == 0 &&
824*4882a593Smuzhiyun 			    member->dsp->pcm_bank_rx == 0 &&
825*4882a593Smuzhiyun 			    nextm->dsp->pcm_bank_tx == 0 &&
826*4882a593Smuzhiyun 			    nextm->dsp->pcm_bank_rx == 0) {
827*4882a593Smuzhiyun 				/* all members have same slot */
828*4882a593Smuzhiyun 				if (dsp_debug & DEBUG_DSP_CMX)
829*4882a593Smuzhiyun 					printk(KERN_DEBUG
830*4882a593Smuzhiyun 					       "%s dsp %s & %s stay joined on PCM "
831*4882a593Smuzhiyun 					       "slot %d (TX) %d (RX) on same chip "
832*4882a593Smuzhiyun 					       "or one bank PCM)\n", __func__,
833*4882a593Smuzhiyun 					       member->dsp->name,
834*4882a593Smuzhiyun 					       nextm->dsp->name,
835*4882a593Smuzhiyun 					       member->dsp->pcm_slot_tx,
836*4882a593Smuzhiyun 					       member->dsp->pcm_slot_rx);
837*4882a593Smuzhiyun 				conf->hardware = 1;
838*4882a593Smuzhiyun 				conf->software = tx_data;
839*4882a593Smuzhiyun 				return;
840*4882a593Smuzhiyun 			}
841*4882a593Smuzhiyun 			/* find two new slot */
842*4882a593Smuzhiyun 			memset(freeslots, 1, sizeof(freeslots));
843*4882a593Smuzhiyun 			list_for_each_entry(dsp, &dsp_ilist, list) {
844*4882a593Smuzhiyun 				if (dsp != member->dsp &&
845*4882a593Smuzhiyun 				    dsp != nextm->dsp &&
846*4882a593Smuzhiyun 				    member->dsp->features.pcm_id ==
847*4882a593Smuzhiyun 				    dsp->features.pcm_id) {
848*4882a593Smuzhiyun 					if (dsp->pcm_slot_rx >= 0 &&
849*4882a593Smuzhiyun 					    dsp->pcm_slot_rx <
850*4882a593Smuzhiyun 					    sizeof(freeslots))
851*4882a593Smuzhiyun 						freeslots[dsp->pcm_slot_rx] = 0;
852*4882a593Smuzhiyun 					if (dsp->pcm_slot_tx >= 0 &&
853*4882a593Smuzhiyun 					    dsp->pcm_slot_tx <
854*4882a593Smuzhiyun 					    sizeof(freeslots))
855*4882a593Smuzhiyun 						freeslots[dsp->pcm_slot_tx] = 0;
856*4882a593Smuzhiyun 				}
857*4882a593Smuzhiyun 			}
858*4882a593Smuzhiyun 			i1 = 0;
859*4882a593Smuzhiyun 			ii = member->dsp->features.pcm_slots;
860*4882a593Smuzhiyun 			while (i1 < ii) {
861*4882a593Smuzhiyun 				if (freeslots[i1])
862*4882a593Smuzhiyun 					break;
863*4882a593Smuzhiyun 				i1++;
864*4882a593Smuzhiyun 			}
865*4882a593Smuzhiyun 			if (i1 == ii) {
866*4882a593Smuzhiyun 				if (dsp_debug & DEBUG_DSP_CMX)
867*4882a593Smuzhiyun 					printk(KERN_DEBUG
868*4882a593Smuzhiyun 					       "%s no slot available "
869*4882a593Smuzhiyun 					       "for %s & %s\n", __func__,
870*4882a593Smuzhiyun 					       member->dsp->name,
871*4882a593Smuzhiyun 					       nextm->dsp->name);
872*4882a593Smuzhiyun 				/* no more slots available */
873*4882a593Smuzhiyun 				goto conf_software;
874*4882a593Smuzhiyun 			}
875*4882a593Smuzhiyun 			i2 = i1 + 1;
876*4882a593Smuzhiyun 			while (i2 < ii) {
877*4882a593Smuzhiyun 				if (freeslots[i2])
878*4882a593Smuzhiyun 					break;
879*4882a593Smuzhiyun 				i2++;
880*4882a593Smuzhiyun 			}
881*4882a593Smuzhiyun 			if (i2 == ii) {
882*4882a593Smuzhiyun 				if (dsp_debug & DEBUG_DSP_CMX)
883*4882a593Smuzhiyun 					printk(KERN_DEBUG
884*4882a593Smuzhiyun 					       "%s no slot available "
885*4882a593Smuzhiyun 					       "for %s & %s\n",
886*4882a593Smuzhiyun 					       __func__,
887*4882a593Smuzhiyun 					       member->dsp->name,
888*4882a593Smuzhiyun 					       nextm->dsp->name);
889*4882a593Smuzhiyun 				/* no more slots available */
890*4882a593Smuzhiyun 				goto conf_software;
891*4882a593Smuzhiyun 			}
892*4882a593Smuzhiyun 			/* assign free slots */
893*4882a593Smuzhiyun 			member->dsp->pcm_slot_tx = i1;
894*4882a593Smuzhiyun 			member->dsp->pcm_slot_rx = i2;
895*4882a593Smuzhiyun 			nextm->dsp->pcm_slot_tx = i2;
896*4882a593Smuzhiyun 			nextm->dsp->pcm_slot_rx = i1;
897*4882a593Smuzhiyun 			member->dsp->pcm_bank_rx = 0;
898*4882a593Smuzhiyun 			member->dsp->pcm_bank_tx = 0;
899*4882a593Smuzhiyun 			nextm->dsp->pcm_bank_rx = 0;
900*4882a593Smuzhiyun 			nextm->dsp->pcm_bank_tx = 0;
901*4882a593Smuzhiyun 			if (dsp_debug & DEBUG_DSP_CMX)
902*4882a593Smuzhiyun 				printk(KERN_DEBUG
903*4882a593Smuzhiyun 				       "%s adding %s & %s to new PCM slot %d "
904*4882a593Smuzhiyun 				       "(TX) %d (RX) on same chip or one bank "
905*4882a593Smuzhiyun 				       "PCM, because both members have not "
906*4882a593Smuzhiyun 				       "crossed slots\n", __func__,
907*4882a593Smuzhiyun 				       member->dsp->name,
908*4882a593Smuzhiyun 				       nextm->dsp->name,
909*4882a593Smuzhiyun 				       member->dsp->pcm_slot_tx,
910*4882a593Smuzhiyun 				       member->dsp->pcm_slot_rx);
911*4882a593Smuzhiyun 			dsp_cmx_hw_message(member->dsp, MISDN_CTRL_HFC_PCM_CONN,
912*4882a593Smuzhiyun 					   member->dsp->pcm_slot_tx, member->dsp->pcm_bank_tx,
913*4882a593Smuzhiyun 					   member->dsp->pcm_slot_rx, member->dsp->pcm_bank_rx);
914*4882a593Smuzhiyun 			dsp_cmx_hw_message(nextm->dsp, MISDN_CTRL_HFC_PCM_CONN,
915*4882a593Smuzhiyun 					   nextm->dsp->pcm_slot_tx, nextm->dsp->pcm_bank_tx,
916*4882a593Smuzhiyun 					   nextm->dsp->pcm_slot_rx, nextm->dsp->pcm_bank_rx);
917*4882a593Smuzhiyun 			conf->hardware = 1;
918*4882a593Smuzhiyun 			conf->software = tx_data;
919*4882a593Smuzhiyun 			return;
920*4882a593Smuzhiyun 		}
921*4882a593Smuzhiyun 	}
922*4882a593Smuzhiyun 
923*4882a593Smuzhiyun 	/*
924*4882a593Smuzhiyun 	 * if we have more than two, we may check if we have a conference
925*4882a593Smuzhiyun 	 * unit available on the chip. also all members must be on the same
926*4882a593Smuzhiyun 	 */
927*4882a593Smuzhiyun 
928*4882a593Smuzhiyun 	/* if not the same HFC chip */
929*4882a593Smuzhiyun 	if (same_hfc < 0) {
930*4882a593Smuzhiyun 		if (dsp_debug & DEBUG_DSP_CMX)
931*4882a593Smuzhiyun 			printk(KERN_DEBUG
932*4882a593Smuzhiyun 			       "%s conference %d cannot be formed, because "
933*4882a593Smuzhiyun 			       "members are on different chips or not "
934*4882a593Smuzhiyun 			       "on HFC chip\n",
935*4882a593Smuzhiyun 			       __func__, conf->id);
936*4882a593Smuzhiyun 		goto conf_software;
937*4882a593Smuzhiyun 	}
938*4882a593Smuzhiyun 
939*4882a593Smuzhiyun 	/* for more than two members.. */
940*4882a593Smuzhiyun 
941*4882a593Smuzhiyun 	/* if all members already have the same conference */
942*4882a593Smuzhiyun 	if (all_conf) {
943*4882a593Smuzhiyun 		conf->hardware = 1;
944*4882a593Smuzhiyun 		conf->software = tx_data;
945*4882a593Smuzhiyun 		return;
946*4882a593Smuzhiyun 	}
947*4882a593Smuzhiyun 
948*4882a593Smuzhiyun 	/*
949*4882a593Smuzhiyun 	 * if there is an existing conference, but not all members have joined
950*4882a593Smuzhiyun 	 */
951*4882a593Smuzhiyun 	if (current_conf >= 0) {
952*4882a593Smuzhiyun 	join_members:
953*4882a593Smuzhiyun 		list_for_each_entry(member, &conf->mlist, list) {
954*4882a593Smuzhiyun 			/* if no conference engine on our chip, change to
955*4882a593Smuzhiyun 			 * software */
956*4882a593Smuzhiyun 			if (!member->dsp->features.hfc_conf)
957*4882a593Smuzhiyun 				goto conf_software;
958*4882a593Smuzhiyun 			/* in case of hdlc, change to software */
959*4882a593Smuzhiyun 			if (member->dsp->hdlc)
960*4882a593Smuzhiyun 				goto conf_software;
961*4882a593Smuzhiyun 			/* join to current conference */
962*4882a593Smuzhiyun 			if (member->dsp->hfc_conf == current_conf)
963*4882a593Smuzhiyun 				continue;
964*4882a593Smuzhiyun 			/* get a free timeslot first */
965*4882a593Smuzhiyun 			memset(freeslots, 1, sizeof(freeslots));
966*4882a593Smuzhiyun 			list_for_each_entry(dsp, &dsp_ilist, list) {
967*4882a593Smuzhiyun 				/*
968*4882a593Smuzhiyun 				 * not checking current member, because
969*4882a593Smuzhiyun 				 * slot will be overwritten.
970*4882a593Smuzhiyun 				 */
971*4882a593Smuzhiyun 				if (
972*4882a593Smuzhiyun 					dsp != member->dsp &&
973*4882a593Smuzhiyun 					/* dsp must be on the same PCM */
974*4882a593Smuzhiyun 					member->dsp->features.pcm_id ==
975*4882a593Smuzhiyun 					dsp->features.pcm_id) {
976*4882a593Smuzhiyun 					/* dsp must be on a slot */
977*4882a593Smuzhiyun 					if (dsp->pcm_slot_tx >= 0 &&
978*4882a593Smuzhiyun 					    dsp->pcm_slot_tx <
979*4882a593Smuzhiyun 					    sizeof(freeslots))
980*4882a593Smuzhiyun 						freeslots[dsp->pcm_slot_tx] = 0;
981*4882a593Smuzhiyun 					if (dsp->pcm_slot_rx >= 0 &&
982*4882a593Smuzhiyun 					    dsp->pcm_slot_rx <
983*4882a593Smuzhiyun 					    sizeof(freeslots))
984*4882a593Smuzhiyun 						freeslots[dsp->pcm_slot_rx] = 0;
985*4882a593Smuzhiyun 				}
986*4882a593Smuzhiyun 			}
987*4882a593Smuzhiyun 			i = 0;
988*4882a593Smuzhiyun 			ii = member->dsp->features.pcm_slots;
989*4882a593Smuzhiyun 			while (i < ii) {
990*4882a593Smuzhiyun 				if (freeslots[i])
991*4882a593Smuzhiyun 					break;
992*4882a593Smuzhiyun 				i++;
993*4882a593Smuzhiyun 			}
994*4882a593Smuzhiyun 			if (i == ii) {
995*4882a593Smuzhiyun 				/* no more slots available */
996*4882a593Smuzhiyun 				if (dsp_debug & DEBUG_DSP_CMX)
997*4882a593Smuzhiyun 					printk(KERN_DEBUG
998*4882a593Smuzhiyun 					       "%s conference %d cannot be formed,"
999*4882a593Smuzhiyun 					       " because no slot free\n",
1000*4882a593Smuzhiyun 					       __func__, conf->id);
1001*4882a593Smuzhiyun 				goto conf_software;
1002*4882a593Smuzhiyun 			}
1003*4882a593Smuzhiyun 			if (dsp_debug & DEBUG_DSP_CMX)
1004*4882a593Smuzhiyun 				printk(KERN_DEBUG
1005*4882a593Smuzhiyun 				       "%s changing dsp %s to HW conference "
1006*4882a593Smuzhiyun 				       "%d slot %d\n", __func__,
1007*4882a593Smuzhiyun 				       member->dsp->name, current_conf, i);
1008*4882a593Smuzhiyun 			/* assign free slot & set PCM & join conf */
1009*4882a593Smuzhiyun 			member->dsp->pcm_slot_tx = i;
1010*4882a593Smuzhiyun 			member->dsp->pcm_slot_rx = i;
1011*4882a593Smuzhiyun 			member->dsp->pcm_bank_tx = 2; /* loop */
1012*4882a593Smuzhiyun 			member->dsp->pcm_bank_rx = 2;
1013*4882a593Smuzhiyun 			member->dsp->hfc_conf = current_conf;
1014*4882a593Smuzhiyun 			dsp_cmx_hw_message(member->dsp, MISDN_CTRL_HFC_PCM_CONN,
1015*4882a593Smuzhiyun 					   i, 2, i, 2);
1016*4882a593Smuzhiyun 			dsp_cmx_hw_message(member->dsp,
1017*4882a593Smuzhiyun 					   MISDN_CTRL_HFC_CONF_JOIN, current_conf, 0, 0, 0);
1018*4882a593Smuzhiyun 		}
1019*4882a593Smuzhiyun 		conf->hardware = 1;
1020*4882a593Smuzhiyun 		conf->software = tx_data;
1021*4882a593Smuzhiyun 		return;
1022*4882a593Smuzhiyun 	}
1023*4882a593Smuzhiyun 
1024*4882a593Smuzhiyun 	/*
1025*4882a593Smuzhiyun 	 * no member is in a conference yet, so we find a free one
1026*4882a593Smuzhiyun 	 */
1027*4882a593Smuzhiyun 	memset(freeunits, 1, sizeof(freeunits));
1028*4882a593Smuzhiyun 	list_for_each_entry(dsp, &dsp_ilist, list) {
1029*4882a593Smuzhiyun 		/* dsp must be on the same chip */
1030*4882a593Smuzhiyun 		if (dsp->features.hfc_id == same_hfc &&
1031*4882a593Smuzhiyun 		    /* dsp must have joined a HW conference */
1032*4882a593Smuzhiyun 		    dsp->hfc_conf >= 0 &&
1033*4882a593Smuzhiyun 		    /* slot must be within range */
1034*4882a593Smuzhiyun 		    dsp->hfc_conf < 8)
1035*4882a593Smuzhiyun 			freeunits[dsp->hfc_conf] = 0;
1036*4882a593Smuzhiyun 	}
1037*4882a593Smuzhiyun 	i = 0;
1038*4882a593Smuzhiyun 	ii = 8;
1039*4882a593Smuzhiyun 	while (i < ii) {
1040*4882a593Smuzhiyun 		if (freeunits[i])
1041*4882a593Smuzhiyun 			break;
1042*4882a593Smuzhiyun 		i++;
1043*4882a593Smuzhiyun 	}
1044*4882a593Smuzhiyun 	if (i == ii) {
1045*4882a593Smuzhiyun 		/* no more conferences available */
1046*4882a593Smuzhiyun 		if (dsp_debug & DEBUG_DSP_CMX)
1047*4882a593Smuzhiyun 			printk(KERN_DEBUG
1048*4882a593Smuzhiyun 			       "%s conference %d cannot be formed, because "
1049*4882a593Smuzhiyun 			       "no conference number free\n",
1050*4882a593Smuzhiyun 			       __func__, conf->id);
1051*4882a593Smuzhiyun 		goto conf_software;
1052*4882a593Smuzhiyun 	}
1053*4882a593Smuzhiyun 	/* join all members */
1054*4882a593Smuzhiyun 	current_conf = i;
1055*4882a593Smuzhiyun 	goto join_members;
1056*4882a593Smuzhiyun }
1057*4882a593Smuzhiyun 
1058*4882a593Smuzhiyun 
1059*4882a593Smuzhiyun /*
1060*4882a593Smuzhiyun  * conf_id != 0: join or change conference
1061*4882a593Smuzhiyun  * conf_id == 0: split from conference if not already
1062*4882a593Smuzhiyun  */
1063*4882a593Smuzhiyun int
dsp_cmx_conf(struct dsp * dsp,u32 conf_id)1064*4882a593Smuzhiyun dsp_cmx_conf(struct dsp *dsp, u32 conf_id)
1065*4882a593Smuzhiyun {
1066*4882a593Smuzhiyun 	int err;
1067*4882a593Smuzhiyun 	struct dsp_conf *conf;
1068*4882a593Smuzhiyun 	struct dsp_conf_member	*member;
1069*4882a593Smuzhiyun 
1070*4882a593Smuzhiyun 	/* if conference doesn't change */
1071*4882a593Smuzhiyun 	if (dsp->conf_id == conf_id)
1072*4882a593Smuzhiyun 		return 0;
1073*4882a593Smuzhiyun 
1074*4882a593Smuzhiyun 	/* first remove us from current conf */
1075*4882a593Smuzhiyun 	if (dsp->conf_id) {
1076*4882a593Smuzhiyun 		if (dsp_debug & DEBUG_DSP_CMX)
1077*4882a593Smuzhiyun 			printk(KERN_DEBUG "removing us from conference %d\n",
1078*4882a593Smuzhiyun 			       dsp->conf->id);
1079*4882a593Smuzhiyun 		/* remove us from conf */
1080*4882a593Smuzhiyun 		conf = dsp->conf;
1081*4882a593Smuzhiyun 		err = dsp_cmx_del_conf_member(dsp);
1082*4882a593Smuzhiyun 		if (err)
1083*4882a593Smuzhiyun 			return err;
1084*4882a593Smuzhiyun 		dsp->conf_id = 0;
1085*4882a593Smuzhiyun 
1086*4882a593Smuzhiyun 		/* update hardware */
1087*4882a593Smuzhiyun 		dsp_cmx_hardware(NULL, dsp);
1088*4882a593Smuzhiyun 
1089*4882a593Smuzhiyun 		/* conf now empty? */
1090*4882a593Smuzhiyun 		if (list_empty(&conf->mlist)) {
1091*4882a593Smuzhiyun 			if (dsp_debug & DEBUG_DSP_CMX)
1092*4882a593Smuzhiyun 				printk(KERN_DEBUG
1093*4882a593Smuzhiyun 				       "conference is empty, so we remove it.\n");
1094*4882a593Smuzhiyun 			err = dsp_cmx_del_conf(conf);
1095*4882a593Smuzhiyun 			if (err)
1096*4882a593Smuzhiyun 				return err;
1097*4882a593Smuzhiyun 		} else {
1098*4882a593Smuzhiyun 			/* update members left on conf */
1099*4882a593Smuzhiyun 			dsp_cmx_hardware(conf, NULL);
1100*4882a593Smuzhiyun 		}
1101*4882a593Smuzhiyun 	}
1102*4882a593Smuzhiyun 
1103*4882a593Smuzhiyun 	/* if split */
1104*4882a593Smuzhiyun 	if (!conf_id)
1105*4882a593Smuzhiyun 		return 0;
1106*4882a593Smuzhiyun 
1107*4882a593Smuzhiyun 	/* now add us to conf */
1108*4882a593Smuzhiyun 	if (dsp_debug & DEBUG_DSP_CMX)
1109*4882a593Smuzhiyun 		printk(KERN_DEBUG "searching conference %d\n",
1110*4882a593Smuzhiyun 		       conf_id);
1111*4882a593Smuzhiyun 	conf = dsp_cmx_search_conf(conf_id);
1112*4882a593Smuzhiyun 	if (!conf) {
1113*4882a593Smuzhiyun 		if (dsp_debug & DEBUG_DSP_CMX)
1114*4882a593Smuzhiyun 			printk(KERN_DEBUG
1115*4882a593Smuzhiyun 			       "conference doesn't exist yet, creating.\n");
1116*4882a593Smuzhiyun 		/* the conference doesn't exist, so we create */
1117*4882a593Smuzhiyun 		conf = dsp_cmx_new_conf(conf_id);
1118*4882a593Smuzhiyun 		if (!conf)
1119*4882a593Smuzhiyun 			return -EINVAL;
1120*4882a593Smuzhiyun 	} else if (!list_empty(&conf->mlist)) {
1121*4882a593Smuzhiyun 		member = list_entry(conf->mlist.next, struct dsp_conf_member,
1122*4882a593Smuzhiyun 				    list);
1123*4882a593Smuzhiyun 		if (dsp->hdlc && !member->dsp->hdlc) {
1124*4882a593Smuzhiyun 			if (dsp_debug & DEBUG_DSP_CMX)
1125*4882a593Smuzhiyun 				printk(KERN_DEBUG
1126*4882a593Smuzhiyun 				       "cannot join transparent conference.\n");
1127*4882a593Smuzhiyun 			return -EINVAL;
1128*4882a593Smuzhiyun 		}
1129*4882a593Smuzhiyun 		if (!dsp->hdlc && member->dsp->hdlc) {
1130*4882a593Smuzhiyun 			if (dsp_debug & DEBUG_DSP_CMX)
1131*4882a593Smuzhiyun 				printk(KERN_DEBUG
1132*4882a593Smuzhiyun 				       "cannot join hdlc conference.\n");
1133*4882a593Smuzhiyun 			return -EINVAL;
1134*4882a593Smuzhiyun 		}
1135*4882a593Smuzhiyun 	}
1136*4882a593Smuzhiyun 	/* add conference member */
1137*4882a593Smuzhiyun 	err = dsp_cmx_add_conf_member(dsp, conf);
1138*4882a593Smuzhiyun 	if (err)
1139*4882a593Smuzhiyun 		return err;
1140*4882a593Smuzhiyun 	dsp->conf_id = conf_id;
1141*4882a593Smuzhiyun 
1142*4882a593Smuzhiyun 	/* if we are alone, we do nothing! */
1143*4882a593Smuzhiyun 	if (list_empty(&conf->mlist)) {
1144*4882a593Smuzhiyun 		if (dsp_debug & DEBUG_DSP_CMX)
1145*4882a593Smuzhiyun 			printk(KERN_DEBUG
1146*4882a593Smuzhiyun 			       "we are alone in this conference, so exit.\n");
1147*4882a593Smuzhiyun 		/* update hardware */
1148*4882a593Smuzhiyun 		dsp_cmx_hardware(NULL, dsp);
1149*4882a593Smuzhiyun 		return 0;
1150*4882a593Smuzhiyun 	}
1151*4882a593Smuzhiyun 
1152*4882a593Smuzhiyun 	/* update members on conf */
1153*4882a593Smuzhiyun 	dsp_cmx_hardware(conf, NULL);
1154*4882a593Smuzhiyun 
1155*4882a593Smuzhiyun 	return 0;
1156*4882a593Smuzhiyun }
1157*4882a593Smuzhiyun 
1158*4882a593Smuzhiyun #ifdef CMX_DELAY_DEBUG
1159*4882a593Smuzhiyun int delaycount;
1160*4882a593Smuzhiyun static void
showdelay(struct dsp * dsp,int samples,int delay)1161*4882a593Smuzhiyun showdelay(struct dsp *dsp, int samples, int delay)
1162*4882a593Smuzhiyun {
1163*4882a593Smuzhiyun 	char bar[] = "--------------------------------------------------|";
1164*4882a593Smuzhiyun 	int sdelay;
1165*4882a593Smuzhiyun 
1166*4882a593Smuzhiyun 	delaycount += samples;
1167*4882a593Smuzhiyun 	if (delaycount < 8000)
1168*4882a593Smuzhiyun 		return;
1169*4882a593Smuzhiyun 	delaycount = 0;
1170*4882a593Smuzhiyun 
1171*4882a593Smuzhiyun 	sdelay = delay * 50 / (dsp_poll << 2);
1172*4882a593Smuzhiyun 
1173*4882a593Smuzhiyun 	printk(KERN_DEBUG "DELAY (%s) %3d >%s\n", dsp->name, delay,
1174*4882a593Smuzhiyun 	       sdelay > 50 ? "..." : bar + 50 - sdelay);
1175*4882a593Smuzhiyun }
1176*4882a593Smuzhiyun #endif
1177*4882a593Smuzhiyun 
1178*4882a593Smuzhiyun /*
1179*4882a593Smuzhiyun  * audio data is received from card
1180*4882a593Smuzhiyun  */
1181*4882a593Smuzhiyun void
dsp_cmx_receive(struct dsp * dsp,struct sk_buff * skb)1182*4882a593Smuzhiyun dsp_cmx_receive(struct dsp *dsp, struct sk_buff *skb)
1183*4882a593Smuzhiyun {
1184*4882a593Smuzhiyun 	u8 *d, *p;
1185*4882a593Smuzhiyun 	int len = skb->len;
1186*4882a593Smuzhiyun 	struct mISDNhead *hh = mISDN_HEAD_P(skb);
1187*4882a593Smuzhiyun 	int w, i, ii;
1188*4882a593Smuzhiyun 
1189*4882a593Smuzhiyun 	/* check if we have sompen */
1190*4882a593Smuzhiyun 	if (len < 1)
1191*4882a593Smuzhiyun 		return;
1192*4882a593Smuzhiyun 
1193*4882a593Smuzhiyun 	/* half of the buffer should be larger than maximum packet size */
1194*4882a593Smuzhiyun 	if (len >= CMX_BUFF_HALF) {
1195*4882a593Smuzhiyun 		printk(KERN_ERR
1196*4882a593Smuzhiyun 		       "%s line %d: packet from card is too large (%d bytes). "
1197*4882a593Smuzhiyun 		       "please make card send smaller packets OR increase "
1198*4882a593Smuzhiyun 		       "CMX_BUFF_SIZE\n", __FILE__, __LINE__, len);
1199*4882a593Smuzhiyun 		return;
1200*4882a593Smuzhiyun 	}
1201*4882a593Smuzhiyun 
1202*4882a593Smuzhiyun 	/*
1203*4882a593Smuzhiyun 	 * initialize pointers if not already -
1204*4882a593Smuzhiyun 	 * also add delay if requested by PH_SIGNAL
1205*4882a593Smuzhiyun 	 */
1206*4882a593Smuzhiyun 	if (dsp->rx_init) {
1207*4882a593Smuzhiyun 		dsp->rx_init = 0;
1208*4882a593Smuzhiyun 		if (dsp->features.unordered) {
1209*4882a593Smuzhiyun 			dsp->rx_R = (hh->id & CMX_BUFF_MASK);
1210*4882a593Smuzhiyun 			if (dsp->cmx_delay)
1211*4882a593Smuzhiyun 				dsp->rx_W = (dsp->rx_R + dsp->cmx_delay)
1212*4882a593Smuzhiyun 					& CMX_BUFF_MASK;
1213*4882a593Smuzhiyun 			else
1214*4882a593Smuzhiyun 				dsp->rx_W = (dsp->rx_R + (dsp_poll >> 1))
1215*4882a593Smuzhiyun 					& CMX_BUFF_MASK;
1216*4882a593Smuzhiyun 		} else {
1217*4882a593Smuzhiyun 			dsp->rx_R = 0;
1218*4882a593Smuzhiyun 			if (dsp->cmx_delay)
1219*4882a593Smuzhiyun 				dsp->rx_W = dsp->cmx_delay;
1220*4882a593Smuzhiyun 			else
1221*4882a593Smuzhiyun 				dsp->rx_W = dsp_poll >> 1;
1222*4882a593Smuzhiyun 		}
1223*4882a593Smuzhiyun 	}
1224*4882a593Smuzhiyun 	/* if frame contains time code, write directly */
1225*4882a593Smuzhiyun 	if (dsp->features.unordered) {
1226*4882a593Smuzhiyun 		dsp->rx_W = (hh->id & CMX_BUFF_MASK);
1227*4882a593Smuzhiyun 		/* printk(KERN_DEBUG "%s %08x\n", dsp->name, hh->id); */
1228*4882a593Smuzhiyun 	}
1229*4882a593Smuzhiyun 	/*
1230*4882a593Smuzhiyun 	 * if we underrun (or maybe overrun),
1231*4882a593Smuzhiyun 	 * we set our new read pointer, and write silence to buffer
1232*4882a593Smuzhiyun 	 */
1233*4882a593Smuzhiyun 	if (((dsp->rx_W-dsp->rx_R) & CMX_BUFF_MASK) >= CMX_BUFF_HALF) {
1234*4882a593Smuzhiyun 		if (dsp_debug & DEBUG_DSP_CLOCK)
1235*4882a593Smuzhiyun 			printk(KERN_DEBUG
1236*4882a593Smuzhiyun 			       "cmx_receive(dsp=%lx): UNDERRUN (or overrun the "
1237*4882a593Smuzhiyun 			       "maximum delay), adjusting read pointer! "
1238*4882a593Smuzhiyun 			       "(inst %s)\n", (u_long)dsp, dsp->name);
1239*4882a593Smuzhiyun 		/* flush rx buffer and set delay to dsp_poll / 2 */
1240*4882a593Smuzhiyun 		if (dsp->features.unordered) {
1241*4882a593Smuzhiyun 			dsp->rx_R = (hh->id & CMX_BUFF_MASK);
1242*4882a593Smuzhiyun 			if (dsp->cmx_delay)
1243*4882a593Smuzhiyun 				dsp->rx_W = (dsp->rx_R + dsp->cmx_delay)
1244*4882a593Smuzhiyun 					& CMX_BUFF_MASK;
1245*4882a593Smuzhiyun 			else
1246*4882a593Smuzhiyun 				dsp->rx_W = (dsp->rx_R + (dsp_poll >> 1))
1247*4882a593Smuzhiyun 					& CMX_BUFF_MASK;
1248*4882a593Smuzhiyun 		} else {
1249*4882a593Smuzhiyun 			dsp->rx_R = 0;
1250*4882a593Smuzhiyun 			if (dsp->cmx_delay)
1251*4882a593Smuzhiyun 				dsp->rx_W = dsp->cmx_delay;
1252*4882a593Smuzhiyun 			else
1253*4882a593Smuzhiyun 				dsp->rx_W = dsp_poll >> 1;
1254*4882a593Smuzhiyun 		}
1255*4882a593Smuzhiyun 		memset(dsp->rx_buff, dsp_silence, sizeof(dsp->rx_buff));
1256*4882a593Smuzhiyun 	}
1257*4882a593Smuzhiyun 	/* if we have reached double delay, jump back to middle */
1258*4882a593Smuzhiyun 	if (dsp->cmx_delay)
1259*4882a593Smuzhiyun 		if (((dsp->rx_W - dsp->rx_R) & CMX_BUFF_MASK) >=
1260*4882a593Smuzhiyun 		    (dsp->cmx_delay << 1)) {
1261*4882a593Smuzhiyun 			if (dsp_debug & DEBUG_DSP_CLOCK)
1262*4882a593Smuzhiyun 				printk(KERN_DEBUG
1263*4882a593Smuzhiyun 				       "cmx_receive(dsp=%lx): OVERRUN (because "
1264*4882a593Smuzhiyun 				       "twice the delay is reached), adjusting "
1265*4882a593Smuzhiyun 				       "read pointer! (inst %s)\n",
1266*4882a593Smuzhiyun 				       (u_long)dsp, dsp->name);
1267*4882a593Smuzhiyun 			/* flush buffer */
1268*4882a593Smuzhiyun 			if (dsp->features.unordered) {
1269*4882a593Smuzhiyun 				dsp->rx_R = (hh->id & CMX_BUFF_MASK);
1270*4882a593Smuzhiyun 				dsp->rx_W = (dsp->rx_R + dsp->cmx_delay)
1271*4882a593Smuzhiyun 					& CMX_BUFF_MASK;
1272*4882a593Smuzhiyun 			} else {
1273*4882a593Smuzhiyun 				dsp->rx_R = 0;
1274*4882a593Smuzhiyun 				dsp->rx_W = dsp->cmx_delay;
1275*4882a593Smuzhiyun 			}
1276*4882a593Smuzhiyun 			memset(dsp->rx_buff, dsp_silence, sizeof(dsp->rx_buff));
1277*4882a593Smuzhiyun 		}
1278*4882a593Smuzhiyun 
1279*4882a593Smuzhiyun 	/* show where to write */
1280*4882a593Smuzhiyun #ifdef CMX_DEBUG
1281*4882a593Smuzhiyun 	printk(KERN_DEBUG
1282*4882a593Smuzhiyun 	       "cmx_receive(dsp=%lx): rx_R(dsp)=%05x rx_W(dsp)=%05x len=%d %s\n",
1283*4882a593Smuzhiyun 	       (u_long)dsp, dsp->rx_R, dsp->rx_W, len, dsp->name);
1284*4882a593Smuzhiyun #endif
1285*4882a593Smuzhiyun 
1286*4882a593Smuzhiyun 	/* write data into rx_buffer */
1287*4882a593Smuzhiyun 	p = skb->data;
1288*4882a593Smuzhiyun 	d = dsp->rx_buff;
1289*4882a593Smuzhiyun 	w = dsp->rx_W;
1290*4882a593Smuzhiyun 	i = 0;
1291*4882a593Smuzhiyun 	ii = len;
1292*4882a593Smuzhiyun 	while (i < ii) {
1293*4882a593Smuzhiyun 		d[w++ & CMX_BUFF_MASK] = *p++;
1294*4882a593Smuzhiyun 		i++;
1295*4882a593Smuzhiyun 	}
1296*4882a593Smuzhiyun 
1297*4882a593Smuzhiyun 	/* increase write-pointer */
1298*4882a593Smuzhiyun 	dsp->rx_W = ((dsp->rx_W + len) & CMX_BUFF_MASK);
1299*4882a593Smuzhiyun #ifdef CMX_DELAY_DEBUG
1300*4882a593Smuzhiyun 	showdelay(dsp, len, (dsp->rx_W-dsp->rx_R) & CMX_BUFF_MASK);
1301*4882a593Smuzhiyun #endif
1302*4882a593Smuzhiyun }
1303*4882a593Smuzhiyun 
1304*4882a593Smuzhiyun 
1305*4882a593Smuzhiyun /*
1306*4882a593Smuzhiyun  * send (mixed) audio data to card and control jitter
1307*4882a593Smuzhiyun  */
1308*4882a593Smuzhiyun static void
dsp_cmx_send_member(struct dsp * dsp,int len,s32 * c,int members)1309*4882a593Smuzhiyun dsp_cmx_send_member(struct dsp *dsp, int len, s32 *c, int members)
1310*4882a593Smuzhiyun {
1311*4882a593Smuzhiyun 	struct dsp_conf *conf = dsp->conf;
1312*4882a593Smuzhiyun 	struct dsp *member, *other;
1313*4882a593Smuzhiyun 	register s32 sample;
1314*4882a593Smuzhiyun 	u8 *d, *p, *q, *o_q;
1315*4882a593Smuzhiyun 	struct sk_buff *nskb, *txskb;
1316*4882a593Smuzhiyun 	int r, rr, t, tt, o_r, o_rr;
1317*4882a593Smuzhiyun 	int preload = 0;
1318*4882a593Smuzhiyun 	struct mISDNhead *hh, *thh;
1319*4882a593Smuzhiyun 	int tx_data_only = 0;
1320*4882a593Smuzhiyun 
1321*4882a593Smuzhiyun 	/* don't process if: */
1322*4882a593Smuzhiyun 	if (!dsp->b_active) { /* if not active */
1323*4882a593Smuzhiyun 		dsp->last_tx = 0;
1324*4882a593Smuzhiyun 		return;
1325*4882a593Smuzhiyun 	}
1326*4882a593Smuzhiyun 	if (((dsp->conf && dsp->conf->hardware) || /* hardware conf */
1327*4882a593Smuzhiyun 	     dsp->echo.hardware) && /* OR hardware echo */
1328*4882a593Smuzhiyun 	    dsp->tx_R == dsp->tx_W && /* AND no tx-data */
1329*4882a593Smuzhiyun 	    !(dsp->tone.tone && dsp->tone.software)) { /* AND not soft tones */
1330*4882a593Smuzhiyun 		if (!dsp->tx_data) { /* no tx_data for user space required */
1331*4882a593Smuzhiyun 			dsp->last_tx = 0;
1332*4882a593Smuzhiyun 			return;
1333*4882a593Smuzhiyun 		}
1334*4882a593Smuzhiyun 		if (dsp->conf && dsp->conf->software && dsp->conf->hardware)
1335*4882a593Smuzhiyun 			tx_data_only = 1;
1336*4882a593Smuzhiyun 		if (dsp->echo.software && dsp->echo.hardware)
1337*4882a593Smuzhiyun 			tx_data_only = 1;
1338*4882a593Smuzhiyun 	}
1339*4882a593Smuzhiyun 
1340*4882a593Smuzhiyun #ifdef CMX_DEBUG
1341*4882a593Smuzhiyun 	printk(KERN_DEBUG
1342*4882a593Smuzhiyun 	       "SEND members=%d dsp=%s, conf=%p, rx_R=%05x rx_W=%05x\n",
1343*4882a593Smuzhiyun 	       members, dsp->name, conf, dsp->rx_R, dsp->rx_W);
1344*4882a593Smuzhiyun #endif
1345*4882a593Smuzhiyun 
1346*4882a593Smuzhiyun 	/* preload if we have delay set */
1347*4882a593Smuzhiyun 	if (dsp->cmx_delay && !dsp->last_tx) {
1348*4882a593Smuzhiyun 		preload = len;
1349*4882a593Smuzhiyun 		if (preload < 128)
1350*4882a593Smuzhiyun 			preload = 128;
1351*4882a593Smuzhiyun 	}
1352*4882a593Smuzhiyun 
1353*4882a593Smuzhiyun 	/* PREPARE RESULT */
1354*4882a593Smuzhiyun 	nskb = mI_alloc_skb(len + preload, GFP_ATOMIC);
1355*4882a593Smuzhiyun 	if (!nskb) {
1356*4882a593Smuzhiyun 		printk(KERN_ERR
1357*4882a593Smuzhiyun 		       "FATAL ERROR in mISDN_dsp.o: cannot alloc %d bytes\n",
1358*4882a593Smuzhiyun 		       len + preload);
1359*4882a593Smuzhiyun 		return;
1360*4882a593Smuzhiyun 	}
1361*4882a593Smuzhiyun 	hh = mISDN_HEAD_P(nskb);
1362*4882a593Smuzhiyun 	hh->prim = PH_DATA_REQ;
1363*4882a593Smuzhiyun 	hh->id = 0;
1364*4882a593Smuzhiyun 	dsp->last_tx = 1;
1365*4882a593Smuzhiyun 
1366*4882a593Smuzhiyun 	/* set pointers, indexes and stuff */
1367*4882a593Smuzhiyun 	member = dsp;
1368*4882a593Smuzhiyun 	p = dsp->tx_buff; /* transmit data */
1369*4882a593Smuzhiyun 	q = dsp->rx_buff; /* received data */
1370*4882a593Smuzhiyun 	d = skb_put(nskb, preload + len); /* result */
1371*4882a593Smuzhiyun 	t = dsp->tx_R; /* tx-pointers */
1372*4882a593Smuzhiyun 	tt = dsp->tx_W;
1373*4882a593Smuzhiyun 	r = dsp->rx_R; /* rx-pointers */
1374*4882a593Smuzhiyun 	rr = (r + len) & CMX_BUFF_MASK;
1375*4882a593Smuzhiyun 
1376*4882a593Smuzhiyun 	/* preload with silence, if required */
1377*4882a593Smuzhiyun 	if (preload) {
1378*4882a593Smuzhiyun 		memset(d, dsp_silence, preload);
1379*4882a593Smuzhiyun 		d += preload;
1380*4882a593Smuzhiyun 	}
1381*4882a593Smuzhiyun 
1382*4882a593Smuzhiyun 	/* PROCESS TONES/TX-DATA ONLY */
1383*4882a593Smuzhiyun 	if (dsp->tone.tone && dsp->tone.software) {
1384*4882a593Smuzhiyun 		/* -> copy tone */
1385*4882a593Smuzhiyun 		dsp_tone_copy(dsp, d, len);
1386*4882a593Smuzhiyun 		dsp->tx_R = 0; /* clear tx buffer */
1387*4882a593Smuzhiyun 		dsp->tx_W = 0;
1388*4882a593Smuzhiyun 		goto send_packet;
1389*4882a593Smuzhiyun 	}
1390*4882a593Smuzhiyun 	/* if we have tx-data but do not use mixing */
1391*4882a593Smuzhiyun 	if (!dsp->tx_mix && t != tt) {
1392*4882a593Smuzhiyun 		/* -> send tx-data and continue when not enough */
1393*4882a593Smuzhiyun #ifdef CMX_TX_DEBUG
1394*4882a593Smuzhiyun 		sprintf(debugbuf, "TX sending (%04x-%04x)%p: ", t, tt, p);
1395*4882a593Smuzhiyun #endif
1396*4882a593Smuzhiyun 		while (r != rr && t != tt) {
1397*4882a593Smuzhiyun #ifdef CMX_TX_DEBUG
1398*4882a593Smuzhiyun 			if (strlen(debugbuf) < 48)
1399*4882a593Smuzhiyun 				sprintf(debugbuf + strlen(debugbuf), " %02x",
1400*4882a593Smuzhiyun 					p[t]);
1401*4882a593Smuzhiyun #endif
1402*4882a593Smuzhiyun 			*d++ = p[t]; /* write tx_buff */
1403*4882a593Smuzhiyun 			t = (t + 1) & CMX_BUFF_MASK;
1404*4882a593Smuzhiyun 			r = (r + 1) & CMX_BUFF_MASK;
1405*4882a593Smuzhiyun 		}
1406*4882a593Smuzhiyun 		if (r == rr) {
1407*4882a593Smuzhiyun 			dsp->tx_R = t;
1408*4882a593Smuzhiyun #ifdef CMX_TX_DEBUG
1409*4882a593Smuzhiyun 			printk(KERN_DEBUG "%s\n", debugbuf);
1410*4882a593Smuzhiyun #endif
1411*4882a593Smuzhiyun 			goto send_packet;
1412*4882a593Smuzhiyun 		}
1413*4882a593Smuzhiyun 	}
1414*4882a593Smuzhiyun #ifdef CMX_TX_DEBUG
1415*4882a593Smuzhiyun 	printk(KERN_DEBUG "%s\n", debugbuf);
1416*4882a593Smuzhiyun #endif
1417*4882a593Smuzhiyun 
1418*4882a593Smuzhiyun 	/* PROCESS DATA (one member / no conf) */
1419*4882a593Smuzhiyun 	if (!conf || members <= 1) {
1420*4882a593Smuzhiyun 		/* -> if echo is NOT enabled */
1421*4882a593Smuzhiyun 		if (!dsp->echo.software) {
1422*4882a593Smuzhiyun 			/* -> send tx-data if available or use 0-volume */
1423*4882a593Smuzhiyun 			while (r != rr && t != tt) {
1424*4882a593Smuzhiyun 				*d++ = p[t]; /* write tx_buff */
1425*4882a593Smuzhiyun 				t = (t + 1) & CMX_BUFF_MASK;
1426*4882a593Smuzhiyun 				r = (r + 1) & CMX_BUFF_MASK;
1427*4882a593Smuzhiyun 			}
1428*4882a593Smuzhiyun 			if (r != rr) {
1429*4882a593Smuzhiyun 				if (dsp_debug & DEBUG_DSP_CLOCK)
1430*4882a593Smuzhiyun 					printk(KERN_DEBUG "%s: RX empty\n",
1431*4882a593Smuzhiyun 					       __func__);
1432*4882a593Smuzhiyun 				memset(d, dsp_silence, (rr - r) & CMX_BUFF_MASK);
1433*4882a593Smuzhiyun 			}
1434*4882a593Smuzhiyun 			/* -> if echo is enabled */
1435*4882a593Smuzhiyun 		} else {
1436*4882a593Smuzhiyun 			/*
1437*4882a593Smuzhiyun 			 * -> mix tx-data with echo if available,
1438*4882a593Smuzhiyun 			 * or use echo only
1439*4882a593Smuzhiyun 			 */
1440*4882a593Smuzhiyun 			while (r != rr && t != tt) {
1441*4882a593Smuzhiyun 				*d++ = dsp_audio_mix_law[(p[t] << 8) | q[r]];
1442*4882a593Smuzhiyun 				t = (t + 1) & CMX_BUFF_MASK;
1443*4882a593Smuzhiyun 				r = (r + 1) & CMX_BUFF_MASK;
1444*4882a593Smuzhiyun 			}
1445*4882a593Smuzhiyun 			while (r != rr) {
1446*4882a593Smuzhiyun 				*d++ = q[r]; /* echo */
1447*4882a593Smuzhiyun 				r = (r + 1) & CMX_BUFF_MASK;
1448*4882a593Smuzhiyun 			}
1449*4882a593Smuzhiyun 		}
1450*4882a593Smuzhiyun 		dsp->tx_R = t;
1451*4882a593Smuzhiyun 		goto send_packet;
1452*4882a593Smuzhiyun 	}
1453*4882a593Smuzhiyun 	/* PROCESS DATA (two members) */
1454*4882a593Smuzhiyun #ifdef CMX_CONF_DEBUG
1455*4882a593Smuzhiyun 	if (0) {
1456*4882a593Smuzhiyun #else
1457*4882a593Smuzhiyun 	if (members == 2) {
1458*4882a593Smuzhiyun #endif
1459*4882a593Smuzhiyun 		/* "other" becomes other party */
1460*4882a593Smuzhiyun 		other = (list_entry(conf->mlist.next,
1461*4882a593Smuzhiyun 				    struct dsp_conf_member, list))->dsp;
1462*4882a593Smuzhiyun 		if (other == member)
1463*4882a593Smuzhiyun 			other = (list_entry(conf->mlist.prev,
1464*4882a593Smuzhiyun 				    struct dsp_conf_member, list))->dsp;
1465*4882a593Smuzhiyun 		o_q = other->rx_buff; /* received data */
1466*4882a593Smuzhiyun 		o_rr = (other->rx_R + len) & CMX_BUFF_MASK;
1467*4882a593Smuzhiyun 		/* end of rx-pointer */
1468*4882a593Smuzhiyun 		o_r = (o_rr - rr + r) & CMX_BUFF_MASK;
1469*4882a593Smuzhiyun 		/* start rx-pointer at current read position*/
1470*4882a593Smuzhiyun 		/* -> if echo is NOT enabled */
1471*4882a593Smuzhiyun 		if (!dsp->echo.software) {
1472*4882a593Smuzhiyun 			/*
1473*4882a593Smuzhiyun 			 * -> copy other member's rx-data,
1474*4882a593Smuzhiyun 			 * if tx-data is available, mix
1475*4882a593Smuzhiyun 			 */
1476*4882a593Smuzhiyun 			while (o_r != o_rr && t != tt) {
1477*4882a593Smuzhiyun 				*d++ = dsp_audio_mix_law[(p[t] << 8) | o_q[o_r]];
1478*4882a593Smuzhiyun 				t = (t + 1) & CMX_BUFF_MASK;
1479*4882a593Smuzhiyun 				o_r = (o_r + 1) & CMX_BUFF_MASK;
1480*4882a593Smuzhiyun 			}
1481*4882a593Smuzhiyun 			while (o_r != o_rr) {
1482*4882a593Smuzhiyun 				*d++ = o_q[o_r];
1483*4882a593Smuzhiyun 				o_r = (o_r + 1) & CMX_BUFF_MASK;
1484*4882a593Smuzhiyun 			}
1485*4882a593Smuzhiyun 			/* -> if echo is enabled */
1486*4882a593Smuzhiyun 		} else {
1487*4882a593Smuzhiyun 			/*
1488*4882a593Smuzhiyun 			 * -> mix other member's rx-data with echo,
1489*4882a593Smuzhiyun 			 * if tx-data is available, mix
1490*4882a593Smuzhiyun 			 */
1491*4882a593Smuzhiyun 			while (r != rr && t != tt) {
1492*4882a593Smuzhiyun 				sample = dsp_audio_law_to_s32[p[t]] +
1493*4882a593Smuzhiyun 					dsp_audio_law_to_s32[q[r]] +
1494*4882a593Smuzhiyun 					dsp_audio_law_to_s32[o_q[o_r]];
1495*4882a593Smuzhiyun 				if (sample < -32768)
1496*4882a593Smuzhiyun 					sample = -32768;
1497*4882a593Smuzhiyun 				else if (sample > 32767)
1498*4882a593Smuzhiyun 					sample = 32767;
1499*4882a593Smuzhiyun 				*d++ = dsp_audio_s16_to_law[sample & 0xffff];
1500*4882a593Smuzhiyun 				/* tx-data + rx_data + echo */
1501*4882a593Smuzhiyun 				t = (t + 1) & CMX_BUFF_MASK;
1502*4882a593Smuzhiyun 				r = (r + 1) & CMX_BUFF_MASK;
1503*4882a593Smuzhiyun 				o_r = (o_r + 1) & CMX_BUFF_MASK;
1504*4882a593Smuzhiyun 			}
1505*4882a593Smuzhiyun 			while (r != rr) {
1506*4882a593Smuzhiyun 				*d++ = dsp_audio_mix_law[(q[r] << 8) | o_q[o_r]];
1507*4882a593Smuzhiyun 				r = (r + 1) & CMX_BUFF_MASK;
1508*4882a593Smuzhiyun 				o_r = (o_r + 1) & CMX_BUFF_MASK;
1509*4882a593Smuzhiyun 			}
1510*4882a593Smuzhiyun 		}
1511*4882a593Smuzhiyun 		dsp->tx_R = t;
1512*4882a593Smuzhiyun 		goto send_packet;
1513*4882a593Smuzhiyun 	}
1514*4882a593Smuzhiyun 	/* PROCESS DATA (three or more members) */
1515*4882a593Smuzhiyun 	/* -> if echo is NOT enabled */
1516*4882a593Smuzhiyun 	if (!dsp->echo.software) {
1517*4882a593Smuzhiyun 		/*
1518*4882a593Smuzhiyun 		 * -> subtract rx-data from conf-data,
1519*4882a593Smuzhiyun 		 * if tx-data is available, mix
1520*4882a593Smuzhiyun 		 */
1521*4882a593Smuzhiyun 		while (r != rr && t != tt) {
1522*4882a593Smuzhiyun 			sample = dsp_audio_law_to_s32[p[t]] + *c++ -
1523*4882a593Smuzhiyun 				dsp_audio_law_to_s32[q[r]];
1524*4882a593Smuzhiyun 			if (sample < -32768)
1525*4882a593Smuzhiyun 				sample = -32768;
1526*4882a593Smuzhiyun 			else if (sample > 32767)
1527*4882a593Smuzhiyun 				sample = 32767;
1528*4882a593Smuzhiyun 			*d++ = dsp_audio_s16_to_law[sample & 0xffff];
1529*4882a593Smuzhiyun 			/* conf-rx+tx */
1530*4882a593Smuzhiyun 			r = (r + 1) & CMX_BUFF_MASK;
1531*4882a593Smuzhiyun 			t = (t + 1) & CMX_BUFF_MASK;
1532*4882a593Smuzhiyun 		}
1533*4882a593Smuzhiyun 		while (r != rr) {
1534*4882a593Smuzhiyun 			sample = *c++ - dsp_audio_law_to_s32[q[r]];
1535*4882a593Smuzhiyun 			if (sample < -32768)
1536*4882a593Smuzhiyun 				sample = -32768;
1537*4882a593Smuzhiyun 			else if (sample > 32767)
1538*4882a593Smuzhiyun 				sample = 32767;
1539*4882a593Smuzhiyun 			*d++ = dsp_audio_s16_to_law[sample & 0xffff];
1540*4882a593Smuzhiyun 			/* conf-rx */
1541*4882a593Smuzhiyun 			r = (r + 1) & CMX_BUFF_MASK;
1542*4882a593Smuzhiyun 		}
1543*4882a593Smuzhiyun 		/* -> if echo is enabled */
1544*4882a593Smuzhiyun 	} else {
1545*4882a593Smuzhiyun 		/*
1546*4882a593Smuzhiyun 		 * -> encode conf-data, if tx-data
1547*4882a593Smuzhiyun 		 * is available, mix
1548*4882a593Smuzhiyun 		 */
1549*4882a593Smuzhiyun 		while (r != rr && t != tt) {
1550*4882a593Smuzhiyun 			sample = dsp_audio_law_to_s32[p[t]] + *c++;
1551*4882a593Smuzhiyun 			if (sample < -32768)
1552*4882a593Smuzhiyun 				sample = -32768;
1553*4882a593Smuzhiyun 			else if (sample > 32767)
1554*4882a593Smuzhiyun 				sample = 32767;
1555*4882a593Smuzhiyun 			*d++ = dsp_audio_s16_to_law[sample & 0xffff];
1556*4882a593Smuzhiyun 			/* conf(echo)+tx */
1557*4882a593Smuzhiyun 			t = (t + 1) & CMX_BUFF_MASK;
1558*4882a593Smuzhiyun 			r = (r + 1) & CMX_BUFF_MASK;
1559*4882a593Smuzhiyun 		}
1560*4882a593Smuzhiyun 		while (r != rr) {
1561*4882a593Smuzhiyun 			sample = *c++;
1562*4882a593Smuzhiyun 			if (sample < -32768)
1563*4882a593Smuzhiyun 				sample = -32768;
1564*4882a593Smuzhiyun 			else if (sample > 32767)
1565*4882a593Smuzhiyun 				sample = 32767;
1566*4882a593Smuzhiyun 			*d++ = dsp_audio_s16_to_law[sample & 0xffff];
1567*4882a593Smuzhiyun 			/* conf(echo) */
1568*4882a593Smuzhiyun 			r = (r + 1) & CMX_BUFF_MASK;
1569*4882a593Smuzhiyun 		}
1570*4882a593Smuzhiyun 	}
1571*4882a593Smuzhiyun 	dsp->tx_R = t;
1572*4882a593Smuzhiyun 	goto send_packet;
1573*4882a593Smuzhiyun 
1574*4882a593Smuzhiyun send_packet:
1575*4882a593Smuzhiyun 	/*
1576*4882a593Smuzhiyun 	 * send tx-data if enabled - don't filter,
1577*4882a593Smuzhiyun 	 * because we want what we send, not what we filtered
1578*4882a593Smuzhiyun 	 */
1579*4882a593Smuzhiyun 	if (dsp->tx_data) {
1580*4882a593Smuzhiyun 		if (tx_data_only) {
1581*4882a593Smuzhiyun 			hh->prim = DL_DATA_REQ;
1582*4882a593Smuzhiyun 			hh->id = 0;
1583*4882a593Smuzhiyun 			/* queue and trigger */
1584*4882a593Smuzhiyun 			skb_queue_tail(&dsp->sendq, nskb);
1585*4882a593Smuzhiyun 			schedule_work(&dsp->workq);
1586*4882a593Smuzhiyun 			/* exit because only tx_data is used */
1587*4882a593Smuzhiyun 			return;
1588*4882a593Smuzhiyun 		} else {
1589*4882a593Smuzhiyun 			txskb = mI_alloc_skb(len, GFP_ATOMIC);
1590*4882a593Smuzhiyun 			if (!txskb) {
1591*4882a593Smuzhiyun 				printk(KERN_ERR
1592*4882a593Smuzhiyun 				       "FATAL ERROR in mISDN_dsp.o: "
1593*4882a593Smuzhiyun 				       "cannot alloc %d bytes\n", len);
1594*4882a593Smuzhiyun 			} else {
1595*4882a593Smuzhiyun 				thh = mISDN_HEAD_P(txskb);
1596*4882a593Smuzhiyun 				thh->prim = DL_DATA_REQ;
1597*4882a593Smuzhiyun 				thh->id = 0;
1598*4882a593Smuzhiyun 				skb_put_data(txskb, nskb->data + preload, len);
1599*4882a593Smuzhiyun 				/* queue (trigger later) */
1600*4882a593Smuzhiyun 				skb_queue_tail(&dsp->sendq, txskb);
1601*4882a593Smuzhiyun 			}
1602*4882a593Smuzhiyun 		}
1603*4882a593Smuzhiyun 	}
1604*4882a593Smuzhiyun 
1605*4882a593Smuzhiyun 	/* send data only to card, if we don't just calculated tx_data */
1606*4882a593Smuzhiyun 	/* adjust volume */
1607*4882a593Smuzhiyun 	if (dsp->tx_volume)
1608*4882a593Smuzhiyun 		dsp_change_volume(nskb, dsp->tx_volume);
1609*4882a593Smuzhiyun 	/* pipeline */
1610*4882a593Smuzhiyun 	if (dsp->pipeline.inuse)
1611*4882a593Smuzhiyun 		dsp_pipeline_process_tx(&dsp->pipeline, nskb->data,
1612*4882a593Smuzhiyun 					nskb->len);
1613*4882a593Smuzhiyun 	/* crypt */
1614*4882a593Smuzhiyun 	if (dsp->bf_enable)
1615*4882a593Smuzhiyun 		dsp_bf_encrypt(dsp, nskb->data, nskb->len);
1616*4882a593Smuzhiyun 	/* queue and trigger */
1617*4882a593Smuzhiyun 	skb_queue_tail(&dsp->sendq, nskb);
1618*4882a593Smuzhiyun 	schedule_work(&dsp->workq);
1619*4882a593Smuzhiyun }
1620*4882a593Smuzhiyun 
1621*4882a593Smuzhiyun static u32	jittercount; /* counter for jitter check */
1622*4882a593Smuzhiyun struct timer_list dsp_spl_tl;
1623*4882a593Smuzhiyun unsigned long	dsp_spl_jiffies; /* calculate the next time to fire */
1624*4882a593Smuzhiyun static u16	dsp_count; /* last sample count */
1625*4882a593Smuzhiyun static int	dsp_count_valid; /* if we have last sample count */
1626*4882a593Smuzhiyun 
1627*4882a593Smuzhiyun void
1628*4882a593Smuzhiyun dsp_cmx_send(void *arg)
1629*4882a593Smuzhiyun {
1630*4882a593Smuzhiyun 	struct dsp_conf *conf;
1631*4882a593Smuzhiyun 	struct dsp_conf_member *member;
1632*4882a593Smuzhiyun 	struct dsp *dsp;
1633*4882a593Smuzhiyun 	int mustmix, members;
1634*4882a593Smuzhiyun 	static s32 mixbuffer[MAX_POLL + 100];
1635*4882a593Smuzhiyun 	s32 *c;
1636*4882a593Smuzhiyun 	u8 *p, *q;
1637*4882a593Smuzhiyun 	int r, rr;
1638*4882a593Smuzhiyun 	int jittercheck = 0, delay, i;
1639*4882a593Smuzhiyun 	u_long flags;
1640*4882a593Smuzhiyun 	u16 length, count;
1641*4882a593Smuzhiyun 
1642*4882a593Smuzhiyun 	/* lock */
1643*4882a593Smuzhiyun 	spin_lock_irqsave(&dsp_lock, flags);
1644*4882a593Smuzhiyun 
1645*4882a593Smuzhiyun 	if (!dsp_count_valid) {
1646*4882a593Smuzhiyun 		dsp_count = mISDN_clock_get();
1647*4882a593Smuzhiyun 		length = dsp_poll;
1648*4882a593Smuzhiyun 		dsp_count_valid = 1;
1649*4882a593Smuzhiyun 	} else {
1650*4882a593Smuzhiyun 		count = mISDN_clock_get();
1651*4882a593Smuzhiyun 		length = count - dsp_count;
1652*4882a593Smuzhiyun 		dsp_count = count;
1653*4882a593Smuzhiyun 	}
1654*4882a593Smuzhiyun 	if (length > MAX_POLL + 100)
1655*4882a593Smuzhiyun 		length = MAX_POLL + 100;
1656*4882a593Smuzhiyun 	/* printk(KERN_DEBUG "len=%d dsp_count=0x%x\n", length, dsp_count); */
1657*4882a593Smuzhiyun 
1658*4882a593Smuzhiyun 	/*
1659*4882a593Smuzhiyun 	 * check if jitter needs to be checked (this is every second)
1660*4882a593Smuzhiyun 	 */
1661*4882a593Smuzhiyun 	jittercount += length;
1662*4882a593Smuzhiyun 	if (jittercount >= 8000) {
1663*4882a593Smuzhiyun 		jittercount -= 8000;
1664*4882a593Smuzhiyun 		jittercheck = 1;
1665*4882a593Smuzhiyun 	}
1666*4882a593Smuzhiyun 
1667*4882a593Smuzhiyun 	/* loop all members that do not require conference mixing */
1668*4882a593Smuzhiyun 	list_for_each_entry(dsp, &dsp_ilist, list) {
1669*4882a593Smuzhiyun 		if (dsp->hdlc)
1670*4882a593Smuzhiyun 			continue;
1671*4882a593Smuzhiyun 		conf = dsp->conf;
1672*4882a593Smuzhiyun 		mustmix = 0;
1673*4882a593Smuzhiyun 		members = 0;
1674*4882a593Smuzhiyun 		if (conf) {
1675*4882a593Smuzhiyun 			members = count_list_member(&conf->mlist);
1676*4882a593Smuzhiyun #ifdef CMX_CONF_DEBUG
1677*4882a593Smuzhiyun 			if (conf->software && members > 1)
1678*4882a593Smuzhiyun #else
1679*4882a593Smuzhiyun 			if (conf->software && members > 2)
1680*4882a593Smuzhiyun #endif
1681*4882a593Smuzhiyun 				mustmix = 1;
1682*4882a593Smuzhiyun 		}
1683*4882a593Smuzhiyun 
1684*4882a593Smuzhiyun 		/* transmission required */
1685*4882a593Smuzhiyun 		if (!mustmix) {
1686*4882a593Smuzhiyun 			dsp_cmx_send_member(dsp, length, mixbuffer, members);
1687*4882a593Smuzhiyun 
1688*4882a593Smuzhiyun 			/*
1689*4882a593Smuzhiyun 			 * unused mixbuffer is given to prevent a
1690*4882a593Smuzhiyun 			 * potential null-pointer-bug
1691*4882a593Smuzhiyun 			 */
1692*4882a593Smuzhiyun 		}
1693*4882a593Smuzhiyun 	}
1694*4882a593Smuzhiyun 
1695*4882a593Smuzhiyun 	/* loop all members that require conference mixing */
1696*4882a593Smuzhiyun 	list_for_each_entry(conf, &conf_ilist, list) {
1697*4882a593Smuzhiyun 		/* count members and check hardware */
1698*4882a593Smuzhiyun 		members = count_list_member(&conf->mlist);
1699*4882a593Smuzhiyun #ifdef CMX_CONF_DEBUG
1700*4882a593Smuzhiyun 		if (conf->software && members > 1) {
1701*4882a593Smuzhiyun #else
1702*4882a593Smuzhiyun 		if (conf->software && members > 2) {
1703*4882a593Smuzhiyun #endif
1704*4882a593Smuzhiyun 			/* check for hdlc conf */
1705*4882a593Smuzhiyun 			member = list_entry(conf->mlist.next,
1706*4882a593Smuzhiyun 					    struct dsp_conf_member, list);
1707*4882a593Smuzhiyun 			if (member->dsp->hdlc)
1708*4882a593Smuzhiyun 				continue;
1709*4882a593Smuzhiyun 			/* mix all data */
1710*4882a593Smuzhiyun 			memset(mixbuffer, 0, length * sizeof(s32));
1711*4882a593Smuzhiyun 			list_for_each_entry(member, &conf->mlist, list) {
1712*4882a593Smuzhiyun 				dsp = member->dsp;
1713*4882a593Smuzhiyun 				/* get range of data to mix */
1714*4882a593Smuzhiyun 				c = mixbuffer;
1715*4882a593Smuzhiyun 				q = dsp->rx_buff;
1716*4882a593Smuzhiyun 				r = dsp->rx_R;
1717*4882a593Smuzhiyun 				rr = (r + length) & CMX_BUFF_MASK;
1718*4882a593Smuzhiyun 				/* add member's data */
1719*4882a593Smuzhiyun 				while (r != rr) {
1720*4882a593Smuzhiyun 					*c++ += dsp_audio_law_to_s32[q[r]];
1721*4882a593Smuzhiyun 					r = (r + 1) & CMX_BUFF_MASK;
1722*4882a593Smuzhiyun 				}
1723*4882a593Smuzhiyun 			}
1724*4882a593Smuzhiyun 
1725*4882a593Smuzhiyun 			/* process each member */
1726*4882a593Smuzhiyun 			list_for_each_entry(member, &conf->mlist, list) {
1727*4882a593Smuzhiyun 				/* transmission */
1728*4882a593Smuzhiyun 				dsp_cmx_send_member(member->dsp, length,
1729*4882a593Smuzhiyun 						    mixbuffer, members);
1730*4882a593Smuzhiyun 			}
1731*4882a593Smuzhiyun 		}
1732*4882a593Smuzhiyun 	}
1733*4882a593Smuzhiyun 
1734*4882a593Smuzhiyun 	/* delete rx-data, increment buffers, change pointers */
1735*4882a593Smuzhiyun 	list_for_each_entry(dsp, &dsp_ilist, list) {
1736*4882a593Smuzhiyun 		if (dsp->hdlc)
1737*4882a593Smuzhiyun 			continue;
1738*4882a593Smuzhiyun 		p = dsp->rx_buff;
1739*4882a593Smuzhiyun 		q = dsp->tx_buff;
1740*4882a593Smuzhiyun 		r = dsp->rx_R;
1741*4882a593Smuzhiyun 		/* move receive pointer when receiving */
1742*4882a593Smuzhiyun 		if (!dsp->rx_is_off) {
1743*4882a593Smuzhiyun 			rr = (r + length) & CMX_BUFF_MASK;
1744*4882a593Smuzhiyun 			/* delete rx-data */
1745*4882a593Smuzhiyun 			while (r != rr) {
1746*4882a593Smuzhiyun 				p[r] = dsp_silence;
1747*4882a593Smuzhiyun 				r = (r + 1) & CMX_BUFF_MASK;
1748*4882a593Smuzhiyun 			}
1749*4882a593Smuzhiyun 			/* increment rx-buffer pointer */
1750*4882a593Smuzhiyun 			dsp->rx_R = r; /* write incremented read pointer */
1751*4882a593Smuzhiyun 		}
1752*4882a593Smuzhiyun 
1753*4882a593Smuzhiyun 		/* check current rx_delay */
1754*4882a593Smuzhiyun 		delay = (dsp->rx_W-dsp->rx_R) & CMX_BUFF_MASK;
1755*4882a593Smuzhiyun 		if (delay >= CMX_BUFF_HALF)
1756*4882a593Smuzhiyun 			delay = 0; /* will be the delay before next write */
1757*4882a593Smuzhiyun 		/* check for lower delay */
1758*4882a593Smuzhiyun 		if (delay < dsp->rx_delay[0])
1759*4882a593Smuzhiyun 			dsp->rx_delay[0] = delay;
1760*4882a593Smuzhiyun 		/* check current tx_delay */
1761*4882a593Smuzhiyun 		delay = (dsp->tx_W-dsp->tx_R) & CMX_BUFF_MASK;
1762*4882a593Smuzhiyun 		if (delay >= CMX_BUFF_HALF)
1763*4882a593Smuzhiyun 			delay = 0; /* will be the delay before next write */
1764*4882a593Smuzhiyun 		/* check for lower delay */
1765*4882a593Smuzhiyun 		if (delay < dsp->tx_delay[0])
1766*4882a593Smuzhiyun 			dsp->tx_delay[0] = delay;
1767*4882a593Smuzhiyun 		if (jittercheck) {
1768*4882a593Smuzhiyun 			/* find the lowest of all rx_delays */
1769*4882a593Smuzhiyun 			delay = dsp->rx_delay[0];
1770*4882a593Smuzhiyun 			i = 1;
1771*4882a593Smuzhiyun 			while (i < MAX_SECONDS_JITTER_CHECK) {
1772*4882a593Smuzhiyun 				if (delay > dsp->rx_delay[i])
1773*4882a593Smuzhiyun 					delay = dsp->rx_delay[i];
1774*4882a593Smuzhiyun 				i++;
1775*4882a593Smuzhiyun 			}
1776*4882a593Smuzhiyun 			/*
1777*4882a593Smuzhiyun 			 * remove rx_delay only if we have delay AND we
1778*4882a593Smuzhiyun 			 * have not preset cmx_delay AND
1779*4882a593Smuzhiyun 			 * the delay is greater dsp_poll
1780*4882a593Smuzhiyun 			 */
1781*4882a593Smuzhiyun 			if (delay > dsp_poll && !dsp->cmx_delay) {
1782*4882a593Smuzhiyun 				if (dsp_debug & DEBUG_DSP_CLOCK)
1783*4882a593Smuzhiyun 					printk(KERN_DEBUG
1784*4882a593Smuzhiyun 					       "%s lowest rx_delay of %d bytes for"
1785*4882a593Smuzhiyun 					       " dsp %s are now removed.\n",
1786*4882a593Smuzhiyun 					       __func__, delay,
1787*4882a593Smuzhiyun 					       dsp->name);
1788*4882a593Smuzhiyun 				r = dsp->rx_R;
1789*4882a593Smuzhiyun 				rr = (r + delay - (dsp_poll >> 1))
1790*4882a593Smuzhiyun 					& CMX_BUFF_MASK;
1791*4882a593Smuzhiyun 				/* delete rx-data */
1792*4882a593Smuzhiyun 				while (r != rr) {
1793*4882a593Smuzhiyun 					p[r] = dsp_silence;
1794*4882a593Smuzhiyun 					r = (r + 1) & CMX_BUFF_MASK;
1795*4882a593Smuzhiyun 				}
1796*4882a593Smuzhiyun 				/* increment rx-buffer pointer */
1797*4882a593Smuzhiyun 				dsp->rx_R = r;
1798*4882a593Smuzhiyun 				/* write incremented read pointer */
1799*4882a593Smuzhiyun 			}
1800*4882a593Smuzhiyun 			/* find the lowest of all tx_delays */
1801*4882a593Smuzhiyun 			delay = dsp->tx_delay[0];
1802*4882a593Smuzhiyun 			i = 1;
1803*4882a593Smuzhiyun 			while (i < MAX_SECONDS_JITTER_CHECK) {
1804*4882a593Smuzhiyun 				if (delay > dsp->tx_delay[i])
1805*4882a593Smuzhiyun 					delay = dsp->tx_delay[i];
1806*4882a593Smuzhiyun 				i++;
1807*4882a593Smuzhiyun 			}
1808*4882a593Smuzhiyun 			/*
1809*4882a593Smuzhiyun 			 * remove delay only if we have delay AND we
1810*4882a593Smuzhiyun 			 * have enabled tx_dejitter
1811*4882a593Smuzhiyun 			 */
1812*4882a593Smuzhiyun 			if (delay > dsp_poll && dsp->tx_dejitter) {
1813*4882a593Smuzhiyun 				if (dsp_debug & DEBUG_DSP_CLOCK)
1814*4882a593Smuzhiyun 					printk(KERN_DEBUG
1815*4882a593Smuzhiyun 					       "%s lowest tx_delay of %d bytes for"
1816*4882a593Smuzhiyun 					       " dsp %s are now removed.\n",
1817*4882a593Smuzhiyun 					       __func__, delay,
1818*4882a593Smuzhiyun 					       dsp->name);
1819*4882a593Smuzhiyun 				r = dsp->tx_R;
1820*4882a593Smuzhiyun 				rr = (r + delay - (dsp_poll >> 1))
1821*4882a593Smuzhiyun 					& CMX_BUFF_MASK;
1822*4882a593Smuzhiyun 				/* delete tx-data */
1823*4882a593Smuzhiyun 				while (r != rr) {
1824*4882a593Smuzhiyun 					q[r] = dsp_silence;
1825*4882a593Smuzhiyun 					r = (r + 1) & CMX_BUFF_MASK;
1826*4882a593Smuzhiyun 				}
1827*4882a593Smuzhiyun 				/* increment rx-buffer pointer */
1828*4882a593Smuzhiyun 				dsp->tx_R = r;
1829*4882a593Smuzhiyun 				/* write incremented read pointer */
1830*4882a593Smuzhiyun 			}
1831*4882a593Smuzhiyun 			/* scroll up delays */
1832*4882a593Smuzhiyun 			i = MAX_SECONDS_JITTER_CHECK - 1;
1833*4882a593Smuzhiyun 			while (i) {
1834*4882a593Smuzhiyun 				dsp->rx_delay[i] = dsp->rx_delay[i - 1];
1835*4882a593Smuzhiyun 				dsp->tx_delay[i] = dsp->tx_delay[i - 1];
1836*4882a593Smuzhiyun 				i--;
1837*4882a593Smuzhiyun 			}
1838*4882a593Smuzhiyun 			dsp->tx_delay[0] = CMX_BUFF_HALF; /* (infinite) delay */
1839*4882a593Smuzhiyun 			dsp->rx_delay[0] = CMX_BUFF_HALF; /* (infinite) delay */
1840*4882a593Smuzhiyun 		}
1841*4882a593Smuzhiyun 	}
1842*4882a593Smuzhiyun 
1843*4882a593Smuzhiyun 	/* if next event would be in the past ... */
1844*4882a593Smuzhiyun 	if ((s32)(dsp_spl_jiffies + dsp_tics-jiffies) <= 0)
1845*4882a593Smuzhiyun 		dsp_spl_jiffies = jiffies + 1;
1846*4882a593Smuzhiyun 	else
1847*4882a593Smuzhiyun 		dsp_spl_jiffies += dsp_tics;
1848*4882a593Smuzhiyun 
1849*4882a593Smuzhiyun 	dsp_spl_tl.expires = dsp_spl_jiffies;
1850*4882a593Smuzhiyun 	add_timer(&dsp_spl_tl);
1851*4882a593Smuzhiyun 
1852*4882a593Smuzhiyun 	/* unlock */
1853*4882a593Smuzhiyun 	spin_unlock_irqrestore(&dsp_lock, flags);
1854*4882a593Smuzhiyun }
1855*4882a593Smuzhiyun 
1856*4882a593Smuzhiyun /*
1857*4882a593Smuzhiyun  * audio data is transmitted from upper layer to the dsp
1858*4882a593Smuzhiyun  */
1859*4882a593Smuzhiyun void
1860*4882a593Smuzhiyun dsp_cmx_transmit(struct dsp *dsp, struct sk_buff *skb)
1861*4882a593Smuzhiyun {
1862*4882a593Smuzhiyun 	u_int w, ww;
1863*4882a593Smuzhiyun 	u8 *d, *p;
1864*4882a593Smuzhiyun 	int space; /* todo: , l = skb->len; */
1865*4882a593Smuzhiyun #ifdef CMX_TX_DEBUG
1866*4882a593Smuzhiyun 	char debugbuf[256] = "";
1867*4882a593Smuzhiyun #endif
1868*4882a593Smuzhiyun 
1869*4882a593Smuzhiyun 	/* check if there is enough space, and then copy */
1870*4882a593Smuzhiyun 	w = dsp->tx_W;
1871*4882a593Smuzhiyun 	ww = dsp->tx_R;
1872*4882a593Smuzhiyun 	p = dsp->tx_buff;
1873*4882a593Smuzhiyun 	d = skb->data;
1874*4882a593Smuzhiyun 	space = (ww - w - 1) & CMX_BUFF_MASK;
1875*4882a593Smuzhiyun 	/* write-pointer should not overrun nor reach read pointer */
1876*4882a593Smuzhiyun 	if (space < skb->len) {
1877*4882a593Smuzhiyun 		/* write to the space we have left */
1878*4882a593Smuzhiyun 		ww = (ww - 1) & CMX_BUFF_MASK; /* end one byte prior tx_R */
1879*4882a593Smuzhiyun 		if (dsp_debug & DEBUG_DSP_CLOCK)
1880*4882a593Smuzhiyun 			printk(KERN_DEBUG "%s: TX overflow space=%d skb->len="
1881*4882a593Smuzhiyun 			       "%d, w=0x%04x, ww=0x%04x\n", __func__, space,
1882*4882a593Smuzhiyun 			       skb->len, w, ww);
1883*4882a593Smuzhiyun 	} else
1884*4882a593Smuzhiyun 		/* write until all byte are copied */
1885*4882a593Smuzhiyun 		ww = (w + skb->len) & CMX_BUFF_MASK;
1886*4882a593Smuzhiyun 	dsp->tx_W = ww;
1887*4882a593Smuzhiyun 		/* show current buffer */
1888*4882a593Smuzhiyun #ifdef CMX_DEBUG
1889*4882a593Smuzhiyun 	printk(KERN_DEBUG
1890*4882a593Smuzhiyun 	       "cmx_transmit(dsp=%lx) %d bytes to 0x%x-0x%x. %s\n",
1891*4882a593Smuzhiyun 	       (u_long)dsp, (ww - w) & CMX_BUFF_MASK, w, ww, dsp->name);
1892*4882a593Smuzhiyun #endif
1893*4882a593Smuzhiyun 
1894*4882a593Smuzhiyun 	/* copy transmit data to tx-buffer */
1895*4882a593Smuzhiyun #ifdef CMX_TX_DEBUG
1896*4882a593Smuzhiyun 	sprintf(debugbuf, "TX getting (%04x-%04x)%p: ", w, ww, p);
1897*4882a593Smuzhiyun #endif
1898*4882a593Smuzhiyun 	while (w != ww) {
1899*4882a593Smuzhiyun #ifdef CMX_TX_DEBUG
1900*4882a593Smuzhiyun 		if (strlen(debugbuf) < 48)
1901*4882a593Smuzhiyun 			sprintf(debugbuf + strlen(debugbuf), " %02x", *d);
1902*4882a593Smuzhiyun #endif
1903*4882a593Smuzhiyun 		p[w] = *d++;
1904*4882a593Smuzhiyun 		w = (w + 1) & CMX_BUFF_MASK;
1905*4882a593Smuzhiyun 	}
1906*4882a593Smuzhiyun #ifdef CMX_TX_DEBUG
1907*4882a593Smuzhiyun 	printk(KERN_DEBUG "%s\n", debugbuf);
1908*4882a593Smuzhiyun #endif
1909*4882a593Smuzhiyun 
1910*4882a593Smuzhiyun }
1911*4882a593Smuzhiyun 
1912*4882a593Smuzhiyun /*
1913*4882a593Smuzhiyun  * hdlc data is received from card and sent to all members.
1914*4882a593Smuzhiyun  */
1915*4882a593Smuzhiyun void
1916*4882a593Smuzhiyun dsp_cmx_hdlc(struct dsp *dsp, struct sk_buff *skb)
1917*4882a593Smuzhiyun {
1918*4882a593Smuzhiyun 	struct sk_buff *nskb = NULL;
1919*4882a593Smuzhiyun 	struct dsp_conf_member *member;
1920*4882a593Smuzhiyun 	struct mISDNhead *hh;
1921*4882a593Smuzhiyun 
1922*4882a593Smuzhiyun 	/* not if not active */
1923*4882a593Smuzhiyun 	if (!dsp->b_active)
1924*4882a593Smuzhiyun 		return;
1925*4882a593Smuzhiyun 
1926*4882a593Smuzhiyun 	/* check if we have sompen */
1927*4882a593Smuzhiyun 	if (skb->len < 1)
1928*4882a593Smuzhiyun 		return;
1929*4882a593Smuzhiyun 
1930*4882a593Smuzhiyun 	/* no conf */
1931*4882a593Smuzhiyun 	if (!dsp->conf) {
1932*4882a593Smuzhiyun 		/* in case of software echo */
1933*4882a593Smuzhiyun 		if (dsp->echo.software) {
1934*4882a593Smuzhiyun 			nskb = skb_clone(skb, GFP_ATOMIC);
1935*4882a593Smuzhiyun 			if (nskb) {
1936*4882a593Smuzhiyun 				hh = mISDN_HEAD_P(nskb);
1937*4882a593Smuzhiyun 				hh->prim = PH_DATA_REQ;
1938*4882a593Smuzhiyun 				hh->id = 0;
1939*4882a593Smuzhiyun 				skb_queue_tail(&dsp->sendq, nskb);
1940*4882a593Smuzhiyun 				schedule_work(&dsp->workq);
1941*4882a593Smuzhiyun 			}
1942*4882a593Smuzhiyun 		}
1943*4882a593Smuzhiyun 		return;
1944*4882a593Smuzhiyun 	}
1945*4882a593Smuzhiyun 	/* in case of hardware conference */
1946*4882a593Smuzhiyun 	if (dsp->conf->hardware)
1947*4882a593Smuzhiyun 		return;
1948*4882a593Smuzhiyun 	list_for_each_entry(member, &dsp->conf->mlist, list) {
1949*4882a593Smuzhiyun 		if (dsp->echo.software || member->dsp != dsp) {
1950*4882a593Smuzhiyun 			nskb = skb_clone(skb, GFP_ATOMIC);
1951*4882a593Smuzhiyun 			if (nskb) {
1952*4882a593Smuzhiyun 				hh = mISDN_HEAD_P(nskb);
1953*4882a593Smuzhiyun 				hh->prim = PH_DATA_REQ;
1954*4882a593Smuzhiyun 				hh->id = 0;
1955*4882a593Smuzhiyun 				skb_queue_tail(&member->dsp->sendq, nskb);
1956*4882a593Smuzhiyun 				schedule_work(&member->dsp->workq);
1957*4882a593Smuzhiyun 			}
1958*4882a593Smuzhiyun 		}
1959*4882a593Smuzhiyun 	}
1960*4882a593Smuzhiyun }
1961