dsp_cmx.c 52 KB

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