dsp_cmx.c 52 KB

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