dsp_cmx.c 50 KB

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