dsp_cmx.c 53 KB

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