dsp_cmx.c 53 KB

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