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