msm-pcm-voip-v2.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708
  1. /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/init.h>
  13. #include <linux/err.h>
  14. #include <linux/module.h>
  15. #include <linux/time.h>
  16. #include <linux/wait.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/slab.h>
  19. #include <linux/dma-mapping.h>
  20. #include <linux/of_device.h>
  21. #include <sound/core.h>
  22. #include <sound/soc.h>
  23. #include <sound/soc-dapm.h>
  24. #include <sound/pcm.h>
  25. #include <sound/initval.h>
  26. #include <sound/control.h>
  27. #include <asm/dma.h>
  28. #include <dsp/q6voice.h>
  29. #include "msm-pcm-q6-v2.h"
  30. #include "msm-pcm-routing-v2.h"
  31. #define SHARED_MEM_BUF 2
  32. #define VOIP_MAX_Q_LEN 10
  33. #define VOIP_MAX_VOC_PKT_SIZE 4096
  34. #define VOIP_MIN_VOC_PKT_SIZE 320
  35. /* Length of the DSP frame info header added to the voc packet. */
  36. #define DSP_FRAME_HDR_LEN 1
  37. #define MODE_IS127 0x2
  38. #define MODE_4GV_NB 0x3
  39. #define MODE_4GV_WB 0x4
  40. #define MODE_AMR 0x5
  41. #define MODE_AMR_WB 0xD
  42. #define MODE_PCM 0xC
  43. #define MODE_4GV_NW 0xE
  44. #define MODE_G711 0xA
  45. #define MODE_G711A 0xF
  46. enum msm_audio_g711a_frame_type {
  47. MVS_G711A_SPEECH_GOOD,
  48. MVS_G711A_SID,
  49. MVS_G711A_NO_DATA,
  50. MVS_G711A_ERASURE
  51. };
  52. enum msm_audio_g711a_mode {
  53. MVS_G711A_MODE_MULAW,
  54. MVS_G711A_MODE_ALAW
  55. };
  56. enum msm_audio_g711_mode {
  57. MVS_G711_MODE_MULAW,
  58. MVS_G711_MODE_ALAW
  59. };
  60. #define VOIP_MODE_MAX MODE_G711A
  61. #define VOIP_RATE_MAX 23850
  62. enum format {
  63. FORMAT_S16_LE = 2,
  64. FORMAT_SPECIAL = 31,
  65. };
  66. enum amr_rate_type {
  67. AMR_RATE_4750, /* AMR 4.75 kbps */
  68. AMR_RATE_5150, /* AMR 5.15 kbps */
  69. AMR_RATE_5900, /* AMR 5.90 kbps */
  70. AMR_RATE_6700, /* AMR 6.70 kbps */
  71. AMR_RATE_7400, /* AMR 7.40 kbps */
  72. AMR_RATE_7950, /* AMR 7.95 kbps */
  73. AMR_RATE_10200, /* AMR 10.20 kbps */
  74. AMR_RATE_12200, /* AMR 12.20 kbps */
  75. AMR_RATE_6600, /* AMR-WB 6.60 kbps */
  76. AMR_RATE_8850, /* AMR-WB 8.85 kbps */
  77. AMR_RATE_12650, /* AMR-WB 12.65 kbps */
  78. AMR_RATE_14250, /* AMR-WB 14.25 kbps */
  79. AMR_RATE_15850, /* AMR-WB 15.85 kbps */
  80. AMR_RATE_18250, /* AMR-WB 18.25 kbps */
  81. AMR_RATE_19850, /* AMR-WB 19.85 kbps */
  82. AMR_RATE_23050, /* AMR-WB 23.05 kbps */
  83. AMR_RATE_23850, /* AMR-WB 23.85 kbps */
  84. AMR_RATE_UNDEF
  85. };
  86. enum voip_state {
  87. VOIP_STOPPED,
  88. VOIP_STARTED,
  89. };
  90. struct voip_frame_hdr {
  91. uint32_t timestamp;
  92. union {
  93. /*
  94. * Bits 0-3: Frame type
  95. * [optional] Bits 16-19: Frame rate
  96. */
  97. uint32_t frame_type;
  98. uint32_t packet_rate;
  99. };
  100. };
  101. struct voip_frame {
  102. struct voip_frame_hdr frm_hdr;
  103. uint32_t pktlen;
  104. uint8_t voc_pkt[VOIP_MAX_VOC_PKT_SIZE];
  105. };
  106. struct voip_buf_node {
  107. struct list_head list;
  108. struct voip_frame frame;
  109. };
  110. struct voip_drv_info {
  111. enum voip_state state;
  112. struct snd_pcm_substream *playback_substream;
  113. struct snd_pcm_substream *capture_substream;
  114. struct list_head in_queue;
  115. struct list_head free_in_queue;
  116. struct list_head out_queue;
  117. struct list_head free_out_queue;
  118. wait_queue_head_t out_wait;
  119. wait_queue_head_t in_wait;
  120. struct mutex lock;
  121. spinlock_t dsp_lock;
  122. spinlock_t dsp_ul_lock;
  123. bool voip_reset;
  124. uint32_t mode;
  125. uint32_t rate_type;
  126. uint32_t rate;
  127. uint32_t dtx_mode;
  128. uint8_t capture_start;
  129. uint8_t playback_start;
  130. uint8_t playback_prepare;
  131. uint8_t capture_prepare;
  132. unsigned int play_samp_rate;
  133. unsigned int cap_samp_rate;
  134. unsigned int pcm_size;
  135. unsigned int pcm_count;
  136. unsigned int pcm_playback_irq_pos; /* IRQ position */
  137. unsigned int pcm_playback_buf_pos; /* position in buffer */
  138. unsigned int pcm_capture_size;
  139. unsigned int pcm_capture_count;
  140. unsigned int pcm_capture_irq_pos; /* IRQ position */
  141. unsigned int pcm_capture_buf_pos; /* position in buffer */
  142. uint32_t evrc_min_rate;
  143. uint32_t evrc_max_rate;
  144. };
  145. static int voip_get_media_type(uint32_t mode, uint32_t rate_type,
  146. unsigned int samp_rate,
  147. unsigned int *media_type);
  148. static int voip_get_rate_type(uint32_t mode,
  149. uint32_t rate,
  150. uint32_t *rate_type);
  151. static int voip_config_vocoder(struct snd_pcm_substream *substream);
  152. static int msm_voip_mode_config_put(struct snd_kcontrol *kcontrol,
  153. struct snd_ctl_elem_value *ucontrol);
  154. static int msm_voip_mode_config_get(struct snd_kcontrol *kcontrol,
  155. struct snd_ctl_elem_value *ucontrol);
  156. static int msm_voip_rate_config_put(struct snd_kcontrol *kcontrol,
  157. struct snd_ctl_elem_value *ucontrol);
  158. static int msm_voip_evrc_min_max_rate_config_put(struct snd_kcontrol *kcontrol,
  159. struct snd_ctl_elem_value *ucontrol);
  160. static int msm_voip_evrc_min_max_rate_config_get(struct snd_kcontrol *kcontrol,
  161. struct snd_ctl_elem_value *ucontrol);
  162. static struct voip_drv_info voip_info;
  163. static struct snd_pcm_hardware msm_pcm_hardware = {
  164. .info = (SNDRV_PCM_INFO_MMAP |
  165. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  166. SNDRV_PCM_INFO_MMAP_VALID |
  167. SNDRV_PCM_INFO_INTERLEAVED),
  168. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  169. SNDRV_PCM_FMTBIT_SPECIAL,
  170. .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |
  171. SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_48000,
  172. .rate_min = 8000,
  173. .rate_max = 48000,
  174. .channels_min = 1,
  175. .channels_max = 1,
  176. .buffer_bytes_max = sizeof(struct voip_buf_node) * VOIP_MAX_Q_LEN,
  177. .period_bytes_min = VOIP_MIN_VOC_PKT_SIZE,
  178. .period_bytes_max = VOIP_MAX_VOC_PKT_SIZE,
  179. .periods_min = VOIP_MAX_Q_LEN,
  180. .periods_max = VOIP_MAX_Q_LEN,
  181. .fifo_size = 0,
  182. };
  183. static int msm_voip_mute_put(struct snd_kcontrol *kcontrol,
  184. struct snd_ctl_elem_value *ucontrol)
  185. {
  186. int ret = 0;
  187. int mute = ucontrol->value.integer.value[0];
  188. int ramp_duration = ucontrol->value.integer.value[1];
  189. if ((mute < 0) || (mute > 1) || (ramp_duration < 0)) {
  190. pr_err(" %s Invalid arguments", __func__);
  191. ret = -EINVAL;
  192. goto done;
  193. }
  194. pr_debug("%s: mute=%d ramp_duration=%d\n", __func__, mute,
  195. ramp_duration);
  196. voc_set_tx_mute(voc_get_session_id(VOIP_SESSION_NAME), TX_PATH, mute,
  197. ramp_duration);
  198. done:
  199. return ret;
  200. }
  201. static int msm_voip_gain_put(struct snd_kcontrol *kcontrol,
  202. struct snd_ctl_elem_value *ucontrol)
  203. {
  204. int ret = 0;
  205. int volume = ucontrol->value.integer.value[0];
  206. int ramp_duration = ucontrol->value.integer.value[1];
  207. if ((volume < 0) || (ramp_duration < 0)) {
  208. pr_err(" %s Invalid arguments", __func__);
  209. ret = -EINVAL;
  210. goto done;
  211. }
  212. pr_debug("%s: volume: %d ramp_duration: %d\n", __func__, volume,
  213. ramp_duration);
  214. voc_set_rx_vol_step(voc_get_session_id(VOIP_SESSION_NAME),
  215. RX_PATH,
  216. volume,
  217. ramp_duration);
  218. done:
  219. return ret;
  220. }
  221. static int msm_voip_dtx_mode_put(struct snd_kcontrol *kcontrol,
  222. struct snd_ctl_elem_value *ucontrol)
  223. {
  224. mutex_lock(&voip_info.lock);
  225. voip_info.dtx_mode = ucontrol->value.integer.value[0];
  226. pr_debug("%s: dtx: %d\n", __func__, voip_info.dtx_mode);
  227. mutex_unlock(&voip_info.lock);
  228. return 0;
  229. }
  230. static int msm_voip_dtx_mode_get(struct snd_kcontrol *kcontrol,
  231. struct snd_ctl_elem_value *ucontrol)
  232. {
  233. mutex_lock(&voip_info.lock);
  234. ucontrol->value.integer.value[0] = voip_info.dtx_mode;
  235. mutex_unlock(&voip_info.lock);
  236. return 0;
  237. }
  238. static struct snd_kcontrol_new msm_voip_controls[] = {
  239. SOC_SINGLE_MULTI_EXT("Voip Tx Mute", SND_SOC_NOPM, 0,
  240. MAX_RAMP_DURATION,
  241. 0, 2, NULL, msm_voip_mute_put),
  242. SOC_SINGLE_MULTI_EXT("Voip Rx Gain", SND_SOC_NOPM, 0,
  243. MAX_RAMP_DURATION,
  244. 0, 2, NULL, msm_voip_gain_put),
  245. SOC_SINGLE_EXT("Voip Mode Config", SND_SOC_NOPM, 0, VOIP_MODE_MAX, 0,
  246. msm_voip_mode_config_get, msm_voip_mode_config_put),
  247. SOC_SINGLE_EXT("Voip Rate Config", SND_SOC_NOPM, 0, VOIP_RATE_MAX, 0,
  248. NULL, msm_voip_rate_config_put),
  249. SOC_SINGLE_MULTI_EXT("Voip Evrc Min Max Rate Config", SND_SOC_NOPM,
  250. 0, VOC_1_RATE, 0, 2,
  251. msm_voip_evrc_min_max_rate_config_get,
  252. msm_voip_evrc_min_max_rate_config_put),
  253. SOC_SINGLE_EXT("Voip Dtx Mode", SND_SOC_NOPM, 0, 1, 0,
  254. msm_voip_dtx_mode_get, msm_voip_dtx_mode_put),
  255. };
  256. static int msm_pcm_voip_probe(struct snd_soc_platform *platform)
  257. {
  258. snd_soc_add_platform_controls(platform, msm_voip_controls,
  259. ARRAY_SIZE(msm_voip_controls));
  260. return 0;
  261. }
  262. /* sample rate supported */
  263. static unsigned int supported_sample_rates[] = {8000, 16000, 32000, 48000};
  264. static void voip_ssr_cb_fn(uint32_t opcode, void *private_data)
  265. {
  266. /* Notify ASoC to send next playback/Capture to unblock write/read */
  267. struct voip_drv_info *prtd = private_data;
  268. if (opcode == 0xFFFFFFFF) {
  269. prtd->voip_reset = true;
  270. pr_debug("%s: Notify ASoC to send next playback/Capture\n",
  271. __func__);
  272. prtd->pcm_playback_irq_pos += prtd->pcm_count;
  273. if (prtd->state == VOIP_STARTED)
  274. snd_pcm_period_elapsed(prtd->playback_substream);
  275. wake_up(&prtd->out_wait);
  276. prtd->pcm_capture_irq_pos += prtd->pcm_capture_count;
  277. if (prtd->state == VOIP_STARTED)
  278. snd_pcm_period_elapsed(prtd->capture_substream);
  279. wake_up(&prtd->in_wait);
  280. } else {
  281. pr_err("%s: Invalid opcode during reset : %d\n",
  282. __func__, opcode);
  283. }
  284. }
  285. /* capture path */
  286. static void voip_process_ul_pkt(uint8_t *voc_pkt,
  287. uint32_t pkt_len,
  288. uint32_t timestamp,
  289. void *private_data)
  290. {
  291. struct voip_buf_node *buf_node = NULL;
  292. struct voip_drv_info *prtd = private_data;
  293. unsigned long dsp_flags;
  294. if (prtd->capture_substream == NULL)
  295. return;
  296. /* Copy up-link packet into out_queue. */
  297. spin_lock_irqsave(&prtd->dsp_ul_lock, dsp_flags);
  298. /* discarding UL packets till start is received */
  299. if (!list_empty(&prtd->free_out_queue) && prtd->capture_start) {
  300. buf_node = list_first_entry(&prtd->free_out_queue,
  301. struct voip_buf_node, list);
  302. list_del(&buf_node->list);
  303. switch (prtd->mode) {
  304. case MODE_AMR_WB:
  305. case MODE_AMR: {
  306. /* Remove the DSP frame info header. Header format:
  307. * Bits 0-3: Frame rate
  308. * Bits 4-7: Frame type
  309. */
  310. buf_node->frame.frm_hdr.timestamp = timestamp;
  311. buf_node->frame.frm_hdr.frame_type =
  312. ((*voc_pkt) & 0xF0) >> 4;
  313. voc_pkt = voc_pkt + DSP_FRAME_HDR_LEN;
  314. buf_node->frame.pktlen = pkt_len - DSP_FRAME_HDR_LEN;
  315. memcpy(&buf_node->frame.voc_pkt[0],
  316. voc_pkt,
  317. buf_node->frame.pktlen);
  318. list_add_tail(&buf_node->list, &prtd->out_queue);
  319. break;
  320. }
  321. case MODE_IS127:
  322. case MODE_4GV_NB:
  323. case MODE_4GV_WB:
  324. case MODE_4GV_NW: {
  325. /* Remove the DSP frame info header.
  326. * Header format:
  327. * Bits 0-3: frame rate
  328. */
  329. buf_node->frame.frm_hdr.timestamp = timestamp;
  330. buf_node->frame.frm_hdr.packet_rate = (*voc_pkt) & 0x0F;
  331. voc_pkt = voc_pkt + DSP_FRAME_HDR_LEN;
  332. buf_node->frame.pktlen = pkt_len - DSP_FRAME_HDR_LEN;
  333. memcpy(&buf_node->frame.voc_pkt[0],
  334. voc_pkt,
  335. buf_node->frame.pktlen);
  336. list_add_tail(&buf_node->list, &prtd->out_queue);
  337. break;
  338. }
  339. case MODE_G711:
  340. case MODE_G711A:{
  341. /* G711 frames are 10ms each, but the DSP works with
  342. * 20ms frames and sends two 10ms frames per buffer.
  343. * Extract the two frames and put them in separate
  344. * buffers.
  345. */
  346. /* Remove the first DSP frame info header.
  347. * Header format: G711A
  348. * Bits 0-1: Frame type
  349. * Bits 2-3: Frame rate
  350. *
  351. * Header format: G711
  352. * Bits 2-3: Frame rate
  353. */
  354. if (prtd->mode == MODE_G711A)
  355. buf_node->frame.frm_hdr.frame_type =
  356. (*voc_pkt) & 0x03;
  357. buf_node->frame.frm_hdr.timestamp = timestamp;
  358. voc_pkt = voc_pkt + DSP_FRAME_HDR_LEN;
  359. /* There are two frames in the buffer. Length of the
  360. * first frame:
  361. */
  362. buf_node->frame.pktlen = (pkt_len -
  363. 2 * DSP_FRAME_HDR_LEN) / 2;
  364. memcpy(&buf_node->frame.voc_pkt[0],
  365. voc_pkt,
  366. buf_node->frame.pktlen);
  367. voc_pkt = voc_pkt + buf_node->frame.pktlen;
  368. list_add_tail(&buf_node->list, &prtd->out_queue);
  369. /* Get another buffer from the free Q and fill in the
  370. * second frame.
  371. */
  372. if (!list_empty(&prtd->free_out_queue)) {
  373. buf_node =
  374. list_first_entry(&prtd->free_out_queue,
  375. struct voip_buf_node,
  376. list);
  377. list_del(&buf_node->list);
  378. /* Remove the second DSP frame info header.
  379. * Header format:
  380. * Bits 0-1: Frame type
  381. * Bits 2-3: Frame rate
  382. */
  383. if (prtd->mode == MODE_G711A)
  384. buf_node->frame.frm_hdr.frame_type =
  385. (*voc_pkt) & 0x03;
  386. buf_node->frame.frm_hdr.timestamp = timestamp;
  387. voc_pkt = voc_pkt + DSP_FRAME_HDR_LEN;
  388. /* There are two frames in the buffer. Length
  389. * of the second frame:
  390. */
  391. buf_node->frame.pktlen = (pkt_len -
  392. 2 * DSP_FRAME_HDR_LEN) / 2;
  393. memcpy(&buf_node->frame.voc_pkt[0],
  394. voc_pkt,
  395. buf_node->frame.pktlen);
  396. list_add_tail(&buf_node->list,
  397. &prtd->out_queue);
  398. } else {
  399. /* Drop the second frame */
  400. pr_err("%s: UL data dropped, read is slow\n",
  401. __func__);
  402. }
  403. break;
  404. }
  405. default: {
  406. buf_node->frame.frm_hdr.timestamp = timestamp;
  407. buf_node->frame.pktlen = pkt_len;
  408. memcpy(&buf_node->frame.voc_pkt[0],
  409. voc_pkt,
  410. buf_node->frame.pktlen);
  411. list_add_tail(&buf_node->list, &prtd->out_queue);
  412. }
  413. }
  414. pr_debug("%s: pkt_len =%d, frame.pktlen=%d, timestamp=%d\n",
  415. __func__, pkt_len, buf_node->frame.pktlen, timestamp);
  416. if (prtd->mode == MODE_PCM)
  417. prtd->pcm_capture_irq_pos += buf_node->frame.pktlen;
  418. else
  419. prtd->pcm_capture_irq_pos += prtd->pcm_capture_count;
  420. spin_unlock_irqrestore(&prtd->dsp_ul_lock, dsp_flags);
  421. snd_pcm_period_elapsed(prtd->capture_substream);
  422. } else {
  423. spin_unlock_irqrestore(&prtd->dsp_ul_lock, dsp_flags);
  424. pr_err("UL data dropped\n");
  425. }
  426. wake_up(&prtd->out_wait);
  427. }
  428. /* playback path */
  429. static void voip_process_dl_pkt(uint8_t *voc_pkt, void *private_data)
  430. {
  431. struct voip_buf_node *buf_node = NULL;
  432. struct voip_drv_info *prtd = private_data;
  433. unsigned long dsp_flags;
  434. uint32_t rate_type;
  435. uint32_t frame_rate;
  436. u32 pkt_len;
  437. u8 *voc_addr = NULL;
  438. if (prtd->playback_substream == NULL)
  439. return;
  440. spin_lock_irqsave(&prtd->dsp_lock, dsp_flags);
  441. if (!list_empty(&prtd->in_queue) && prtd->playback_start) {
  442. buf_node = list_first_entry(&prtd->in_queue,
  443. struct voip_buf_node, list);
  444. list_del(&buf_node->list);
  445. switch (prtd->mode) {
  446. case MODE_AMR:
  447. case MODE_AMR_WB: {
  448. *((uint32_t *)voc_pkt) = buf_node->frame.pktlen +
  449. DSP_FRAME_HDR_LEN;
  450. /* Advance to the header of voip packet */
  451. voc_pkt = voc_pkt + sizeof(uint32_t);
  452. /*
  453. * Add the DSP frame info header. Header format:
  454. * Bits 0-3: Frame rate
  455. * Bits 4-7: Frame type
  456. */
  457. *voc_pkt = ((buf_node->frame.frm_hdr.frame_type &
  458. 0x0F) << 4);
  459. frame_rate = (buf_node->frame.frm_hdr.frame_type &
  460. 0xFFFF0000) >> 16;
  461. if (frame_rate) {
  462. if (voip_get_rate_type(prtd->mode, frame_rate,
  463. &rate_type)) {
  464. pr_err("%s(): fail at getting rate_type\n",
  465. __func__);
  466. } else
  467. prtd->rate_type = rate_type;
  468. }
  469. *voc_pkt |= prtd->rate_type & 0x0F;
  470. voc_pkt = voc_pkt + DSP_FRAME_HDR_LEN;
  471. memcpy(voc_pkt,
  472. &buf_node->frame.voc_pkt[0],
  473. buf_node->frame.pktlen);
  474. list_add_tail(&buf_node->list, &prtd->free_in_queue);
  475. break;
  476. }
  477. case MODE_IS127:
  478. case MODE_4GV_NB:
  479. case MODE_4GV_WB:
  480. case MODE_4GV_NW: {
  481. *((uint32_t *)voc_pkt) = buf_node->frame.pktlen +
  482. DSP_FRAME_HDR_LEN;
  483. /* Advance to the header of voip packet */
  484. voc_pkt = voc_pkt + sizeof(uint32_t);
  485. /*
  486. * Add the DSP frame info header. Header format:
  487. * Bits 0-3 : Frame rate
  488. */
  489. *voc_pkt = buf_node->frame.frm_hdr.packet_rate & 0x0F;
  490. voc_pkt = voc_pkt + DSP_FRAME_HDR_LEN;
  491. memcpy(voc_pkt,
  492. &buf_node->frame.voc_pkt[0],
  493. buf_node->frame.pktlen);
  494. list_add_tail(&buf_node->list, &prtd->free_in_queue);
  495. break;
  496. }
  497. case MODE_G711:
  498. case MODE_G711A:{
  499. /* G711 frames are 10ms each but the DSP expects 20ms
  500. * worth of data, so send two 10ms frames per buffer.
  501. */
  502. /* Add the first DSP frame info header. Header format:
  503. * Bits 0-1: Frame type
  504. * Bits 2-3: Frame rate
  505. */
  506. voc_addr = voc_pkt;
  507. voc_pkt = voc_pkt + sizeof(uint32_t);
  508. *voc_pkt = ((prtd->rate_type & 0x0F) << 2) |
  509. (buf_node->frame.frm_hdr.frame_type & 0x03);
  510. voc_pkt = voc_pkt + DSP_FRAME_HDR_LEN;
  511. pkt_len = buf_node->frame.pktlen + DSP_FRAME_HDR_LEN;
  512. memcpy(voc_pkt,
  513. &buf_node->frame.voc_pkt[0],
  514. buf_node->frame.pktlen);
  515. voc_pkt = voc_pkt + buf_node->frame.pktlen;
  516. list_add_tail(&buf_node->list, &prtd->free_in_queue);
  517. if (!list_empty(&prtd->in_queue)) {
  518. /* Get the second buffer. */
  519. buf_node = list_first_entry(&prtd->in_queue,
  520. struct voip_buf_node,
  521. list);
  522. list_del(&buf_node->list);
  523. /* Add the second DSP frame info header.
  524. * Header format:
  525. * Bits 0-1: Frame type
  526. * Bits 2-3: Frame rate
  527. */
  528. *voc_pkt = ((prtd->rate_type & 0x0F) << 2) |
  529. (buf_node->frame.frm_hdr.frame_type & 0x03);
  530. voc_pkt = voc_pkt + DSP_FRAME_HDR_LEN;
  531. pkt_len = pkt_len + buf_node->frame.pktlen +
  532. DSP_FRAME_HDR_LEN;
  533. memcpy(voc_pkt,
  534. &buf_node->frame.voc_pkt[0],
  535. buf_node->frame.pktlen);
  536. list_add_tail(&buf_node->list,
  537. &prtd->free_in_queue);
  538. } else {
  539. /* Only 10ms worth of data is available, signal
  540. * erasure frame.
  541. */
  542. *voc_pkt = ((prtd->rate_type & 0x0F) << 2) |
  543. (MVS_G711A_ERASURE & 0x03);
  544. pkt_len = pkt_len + DSP_FRAME_HDR_LEN;
  545. pr_debug("%s, Only 10ms read, erase 2nd frame\n",
  546. __func__);
  547. }
  548. *((uint32_t *)voc_addr) = pkt_len;
  549. break;
  550. }
  551. default: {
  552. *((uint32_t *)voc_pkt) = buf_node->frame.pktlen;
  553. voc_pkt = voc_pkt + sizeof(uint32_t);
  554. memcpy(voc_pkt,
  555. &buf_node->frame.voc_pkt[0],
  556. buf_node->frame.pktlen);
  557. list_add_tail(&buf_node->list, &prtd->free_in_queue);
  558. }
  559. }
  560. pr_debug("%s: frame.pktlen=%d\n", __func__,
  561. buf_node->frame.pktlen);
  562. if (prtd->mode == MODE_PCM)
  563. prtd->pcm_playback_irq_pos += buf_node->frame.pktlen;
  564. else
  565. prtd->pcm_playback_irq_pos += prtd->pcm_count;
  566. spin_unlock_irqrestore(&prtd->dsp_lock, dsp_flags);
  567. snd_pcm_period_elapsed(prtd->playback_substream);
  568. } else {
  569. *((uint32_t *)voc_pkt) = 0;
  570. spin_unlock_irqrestore(&prtd->dsp_lock, dsp_flags);
  571. pr_err_ratelimited("DL data not available\n");
  572. }
  573. wake_up(&prtd->in_wait);
  574. }
  575. static struct snd_pcm_hw_constraint_list constraints_sample_rates = {
  576. .count = ARRAY_SIZE(supported_sample_rates),
  577. .list = supported_sample_rates,
  578. .mask = 0,
  579. };
  580. static int msm_pcm_playback_prepare(struct snd_pcm_substream *substream)
  581. {
  582. struct snd_pcm_runtime *runtime = substream->runtime;
  583. struct voip_drv_info *prtd = runtime->private_data;
  584. prtd->play_samp_rate = runtime->rate;
  585. prtd->pcm_size = snd_pcm_lib_buffer_bytes(substream);
  586. prtd->pcm_count = snd_pcm_lib_period_bytes(substream);
  587. prtd->pcm_playback_irq_pos = 0;
  588. prtd->pcm_playback_buf_pos = 0;
  589. prtd->playback_prepare = 1;
  590. return 0;
  591. }
  592. static int msm_pcm_capture_prepare(struct snd_pcm_substream *substream)
  593. {
  594. struct snd_pcm_runtime *runtime = substream->runtime;
  595. struct voip_drv_info *prtd = runtime->private_data;
  596. int ret = 0;
  597. prtd->cap_samp_rate = runtime->rate;
  598. prtd->pcm_capture_size = snd_pcm_lib_buffer_bytes(substream);
  599. prtd->pcm_capture_count = snd_pcm_lib_period_bytes(substream);
  600. prtd->pcm_capture_irq_pos = 0;
  601. prtd->pcm_capture_buf_pos = 0;
  602. prtd->capture_prepare = 1;
  603. return ret;
  604. }
  605. static int msm_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  606. {
  607. int ret = 0;
  608. struct snd_pcm_runtime *runtime = substream->runtime;
  609. struct voip_drv_info *prtd = runtime->private_data;
  610. switch (cmd) {
  611. case SNDRV_PCM_TRIGGER_START:
  612. case SNDRV_PCM_TRIGGER_RESUME:
  613. pr_debug("%s: Trigger start\n", __func__);
  614. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  615. prtd->capture_start = 1;
  616. else
  617. prtd->playback_start = 1;
  618. break;
  619. case SNDRV_PCM_TRIGGER_STOP:
  620. pr_debug("SNDRV_PCM_TRIGGER_STOP\n");
  621. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  622. prtd->playback_start = 0;
  623. else
  624. prtd->capture_start = 0;
  625. break;
  626. default:
  627. ret = -EINVAL;
  628. break;
  629. }
  630. return ret;
  631. }
  632. static int msm_pcm_open(struct snd_pcm_substream *substream)
  633. {
  634. struct snd_pcm_runtime *runtime = substream->runtime;
  635. struct voip_drv_info *prtd = &voip_info;
  636. int ret = 0;
  637. pr_debug("%s, VoIP\n", __func__);
  638. mutex_lock(&prtd->lock);
  639. runtime->hw = msm_pcm_hardware;
  640. ret = snd_pcm_hw_constraint_list(runtime, 0,
  641. SNDRV_PCM_HW_PARAM_RATE,
  642. &constraints_sample_rates);
  643. if (ret < 0)
  644. pr_debug("snd_pcm_hw_constraint_list failed\n");
  645. ret = snd_pcm_hw_constraint_integer(runtime,
  646. SNDRV_PCM_HW_PARAM_PERIODS);
  647. if (ret < 0) {
  648. pr_debug("snd_pcm_hw_constraint_integer failed\n");
  649. goto err;
  650. }
  651. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  652. prtd->playback_substream = substream;
  653. else
  654. prtd->capture_substream = substream;
  655. runtime->private_data = prtd;
  656. err:
  657. mutex_unlock(&prtd->lock);
  658. return ret;
  659. }
  660. static int msm_pcm_playback_copy(struct snd_pcm_substream *substream, int a,
  661. unsigned long hwoff, void __user *buf, unsigned long fbytes)
  662. {
  663. int ret = 0;
  664. struct voip_buf_node *buf_node = NULL;
  665. struct snd_pcm_runtime *runtime = substream->runtime;
  666. struct voip_drv_info *prtd = runtime->private_data;
  667. unsigned long dsp_flags;
  668. pr_debug("%s: fbytes=%lu\n", __func__, fbytes);
  669. if (prtd->voip_reset) {
  670. pr_debug("%s: RESET event happened during VoIP\n", __func__);
  671. return -ENETRESET;
  672. }
  673. ret = wait_event_interruptible_timeout(prtd->in_wait,
  674. (!list_empty(&prtd->free_in_queue) ||
  675. prtd->state == VOIP_STOPPED),
  676. 1 * HZ);
  677. if (prtd->voip_reset) {
  678. pr_debug("%s: RESET event happened during VoIP\n", __func__);
  679. return -ENETRESET;
  680. }
  681. if (ret > 0) {
  682. if (fbytes <= VOIP_MAX_VOC_PKT_SIZE) {
  683. spin_lock_irqsave(&prtd->dsp_lock, dsp_flags);
  684. buf_node =
  685. list_first_entry(&prtd->free_in_queue,
  686. struct voip_buf_node, list);
  687. list_del(&buf_node->list);
  688. spin_unlock_irqrestore(&prtd->dsp_lock, dsp_flags);
  689. if (prtd->mode == MODE_PCM) {
  690. ret = copy_from_user(&buf_node->frame.voc_pkt,
  691. buf, fbytes);
  692. if (ret) {
  693. pr_err("%s: copy from user failed %d\n",
  694. __func__, ret);
  695. return -EFAULT;
  696. }
  697. buf_node->frame.pktlen = fbytes;
  698. } else {
  699. ret = copy_from_user(&buf_node->frame,
  700. buf, fbytes);
  701. if (ret) {
  702. pr_err("%s: copy from user failed %d\n",
  703. __func__, ret);
  704. return -EFAULT;
  705. }
  706. if (buf_node->frame.pktlen >= fbytes)
  707. buf_node->frame.pktlen = fbytes -
  708. (sizeof(buf_node->frame.frm_hdr) +
  709. sizeof(buf_node->frame.pktlen));
  710. }
  711. spin_lock_irqsave(&prtd->dsp_lock, dsp_flags);
  712. list_add_tail(&buf_node->list, &prtd->in_queue);
  713. spin_unlock_irqrestore(&prtd->dsp_lock, dsp_flags);
  714. } else {
  715. pr_err("%s: Write cnt %lu is > VOIP_MAX_VOC_PKT_SIZE\n",
  716. __func__, fbytes);
  717. ret = -ENOMEM;
  718. }
  719. } else if (ret == 0) {
  720. pr_err("%s: No free DL buffs\n", __func__);
  721. ret = -ETIMEDOUT;
  722. } else {
  723. pr_err("%s: playback copy was interrupted %d\n", __func__, ret);
  724. }
  725. return ret;
  726. }
  727. static int msm_pcm_capture_copy(struct snd_pcm_substream *substream,
  728. int channel, unsigned long hwoff, void __user *buf,
  729. unsigned long fbytes)
  730. {
  731. int ret = 0;
  732. struct voip_buf_node *buf_node = NULL;
  733. struct snd_pcm_runtime *runtime = substream->runtime;
  734. struct voip_drv_info *prtd = runtime->private_data;
  735. unsigned long dsp_flags;
  736. int size;
  737. pr_debug("%s: fbytes = %lu\n", __func__, fbytes);
  738. if (prtd->voip_reset) {
  739. pr_debug("%s: RESET event happened during VoIP\n", __func__);
  740. return -ENETRESET;
  741. }
  742. ret = wait_event_interruptible_timeout(prtd->out_wait,
  743. (!list_empty(&prtd->out_queue) ||
  744. prtd->state == VOIP_STOPPED),
  745. 1 * HZ);
  746. if (prtd->voip_reset) {
  747. pr_debug("%s: RESET event happened during VoIP\n", __func__);
  748. return -ENETRESET;
  749. }
  750. if (ret > 0) {
  751. if (fbytes <= VOIP_MAX_VOC_PKT_SIZE) {
  752. spin_lock_irqsave(&prtd->dsp_ul_lock, dsp_flags);
  753. buf_node = list_first_entry(&prtd->out_queue,
  754. struct voip_buf_node, list);
  755. list_del(&buf_node->list);
  756. spin_unlock_irqrestore(&prtd->dsp_ul_lock, dsp_flags);
  757. if (prtd->mode == MODE_PCM) {
  758. ret = copy_to_user(buf,
  759. &buf_node->frame.voc_pkt,
  760. buf_node->frame.pktlen);
  761. } else {
  762. size = sizeof(buf_node->frame.frm_hdr) +
  763. sizeof(buf_node->frame.pktlen) +
  764. buf_node->frame.pktlen;
  765. ret = copy_to_user(buf,
  766. &buf_node->frame,
  767. size);
  768. }
  769. if (ret) {
  770. pr_err("%s: Copy to user returned %d\n",
  771. __func__, ret);
  772. ret = -EFAULT;
  773. }
  774. spin_lock_irqsave(&prtd->dsp_ul_lock, dsp_flags);
  775. list_add_tail(&buf_node->list,
  776. &prtd->free_out_queue);
  777. spin_unlock_irqrestore(&prtd->dsp_ul_lock, dsp_flags);
  778. } else {
  779. pr_err("%s: Read fbytes %lu > VOIP_MAX_VOC_PKT_SIZE\n",
  780. __func__, fbytes);
  781. ret = -ENOMEM;
  782. }
  783. } else if (ret == 0) {
  784. pr_err_ratelimited("%s: No UL data available\n", __func__);
  785. ret = -ETIMEDOUT;
  786. } else {
  787. pr_err("%s: Read was interrupted\n", __func__);
  788. ret = -ERESTARTSYS;
  789. }
  790. return ret;
  791. }
  792. static int msm_pcm_copy(struct snd_pcm_substream *substream, int a,
  793. unsigned long hwoff, void __user *buf, unsigned long fbytes)
  794. {
  795. int ret = 0;
  796. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  797. ret = msm_pcm_playback_copy(substream, a, hwoff, buf, fbytes);
  798. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  799. ret = msm_pcm_capture_copy(substream, a, hwoff, buf, fbytes);
  800. return ret;
  801. }
  802. static int msm_pcm_close(struct snd_pcm_substream *substream)
  803. {
  804. int ret = 0;
  805. struct list_head *ptr = NULL;
  806. struct list_head *next = NULL;
  807. struct voip_buf_node *buf_node = NULL;
  808. struct snd_dma_buffer *p_dma_buf, *c_dma_buf;
  809. struct snd_pcm_substream *p_substream, *c_substream;
  810. struct snd_pcm_runtime *runtime;
  811. struct voip_drv_info *prtd;
  812. unsigned long dsp_flags;
  813. if (substream == NULL) {
  814. pr_err("substream is NULL\n");
  815. return -EINVAL;
  816. }
  817. runtime = substream->runtime;
  818. prtd = runtime->private_data;
  819. wake_up(&prtd->out_wait);
  820. mutex_lock(&prtd->lock);
  821. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  822. prtd->playback_prepare = 0;
  823. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  824. prtd->capture_prepare = 0;
  825. if (!prtd->playback_prepare && !prtd->capture_prepare) {
  826. if (prtd->state == VOIP_STARTED) {
  827. prtd->voip_reset = false;
  828. prtd->state = VOIP_STOPPED;
  829. voc_end_voice_call(
  830. voc_get_session_id(VOIP_SESSION_NAME));
  831. voc_register_mvs_cb(NULL, NULL, NULL, prtd);
  832. }
  833. /* release all buffer */
  834. /* release in_queue and free_in_queue */
  835. pr_debug("release all buffer\n");
  836. p_substream = prtd->playback_substream;
  837. if (p_substream == NULL) {
  838. pr_debug("p_substream is NULL\n");
  839. goto capt;
  840. }
  841. p_dma_buf = &p_substream->dma_buffer;
  842. if (p_dma_buf == NULL) {
  843. pr_debug("p_dma_buf is NULL\n");
  844. goto capt;
  845. }
  846. if (p_dma_buf->area != NULL) {
  847. spin_lock_irqsave(&prtd->dsp_lock, dsp_flags);
  848. list_for_each_safe(ptr, next, &prtd->in_queue) {
  849. buf_node = list_entry(ptr,
  850. struct voip_buf_node, list);
  851. list_del(&buf_node->list);
  852. }
  853. list_for_each_safe(ptr, next, &prtd->free_in_queue) {
  854. buf_node = list_entry(ptr,
  855. struct voip_buf_node, list);
  856. list_del(&buf_node->list);
  857. }
  858. spin_unlock_irqrestore(&prtd->dsp_lock, dsp_flags);
  859. dma_free_coherent(p_substream->pcm->card->dev,
  860. runtime->hw.buffer_bytes_max, p_dma_buf->area,
  861. p_dma_buf->addr);
  862. p_dma_buf->area = NULL;
  863. }
  864. /* release out_queue and free_out_queue */
  865. capt: c_substream = prtd->capture_substream;
  866. if (c_substream == NULL) {
  867. pr_debug("c_substream is NULL\n");
  868. goto done;
  869. }
  870. c_dma_buf = &c_substream->dma_buffer;
  871. if (c_substream == NULL) {
  872. pr_debug("c_dma_buf is NULL.\n");
  873. goto done;
  874. }
  875. if (c_dma_buf->area != NULL) {
  876. spin_lock_irqsave(&prtd->dsp_ul_lock, dsp_flags);
  877. list_for_each_safe(ptr, next, &prtd->out_queue) {
  878. buf_node = list_entry(ptr,
  879. struct voip_buf_node, list);
  880. list_del(&buf_node->list);
  881. }
  882. list_for_each_safe(ptr, next, &prtd->free_out_queue) {
  883. buf_node = list_entry(ptr,
  884. struct voip_buf_node, list);
  885. list_del(&buf_node->list);
  886. }
  887. spin_unlock_irqrestore(&prtd->dsp_ul_lock, dsp_flags);
  888. dma_free_coherent(c_substream->pcm->card->dev,
  889. runtime->hw.buffer_bytes_max, c_dma_buf->area,
  890. c_dma_buf->addr);
  891. c_dma_buf->area = NULL;
  892. }
  893. done:
  894. prtd->capture_substream = NULL;
  895. prtd->playback_substream = NULL;
  896. }
  897. mutex_unlock(&prtd->lock);
  898. return ret;
  899. }
  900. static int voip_config_vocoder(struct snd_pcm_substream *substream)
  901. {
  902. int ret = 0;
  903. struct snd_pcm_runtime *runtime = substream->runtime;
  904. struct voip_drv_info *prtd = runtime->private_data;
  905. uint32_t media_type = 0;
  906. uint32_t rate_type = 0;
  907. uint32_t evrc_min_rate_type = 0;
  908. uint32_t evrc_max_rate_type = 0;
  909. pr_debug("%s(): mode=%d, playback rate=%d, capture rate=%d\n",
  910. __func__, prtd->mode, prtd->play_samp_rate,
  911. prtd->cap_samp_rate);
  912. if ((runtime->format != FORMAT_S16_LE &&
  913. runtime->format != FORMAT_SPECIAL) &&
  914. ((prtd->mode == MODE_AMR) || (prtd->mode == MODE_AMR_WB) ||
  915. (prtd->mode == MODE_IS127) || (prtd->mode == MODE_4GV_NB) ||
  916. (prtd->mode == MODE_4GV_WB) || (prtd->mode == MODE_4GV_NW) ||
  917. (prtd->mode == MODE_G711) || (prtd->mode == MODE_G711A))) {
  918. pr_err("%s(): mode:%d and format:%u are not matched\n",
  919. __func__, prtd->mode, (uint32_t)runtime->format);
  920. ret = -EINVAL;
  921. goto done;
  922. }
  923. if (runtime->format != FORMAT_S16_LE && (prtd->mode == MODE_PCM)) {
  924. pr_err("%s(): mode:%d and format:%u are not matched\n",
  925. __func__, prtd->mode, runtime->format);
  926. ret = -EINVAL;
  927. goto done;
  928. }
  929. if ((prtd->mode == MODE_PCM) ||
  930. (prtd->mode == MODE_AMR) ||
  931. (prtd->mode == MODE_AMR_WB) ||
  932. (prtd->mode == MODE_G711) ||
  933. (prtd->mode == MODE_G711A)) {
  934. ret = voip_get_rate_type(prtd->mode,
  935. prtd->rate,
  936. &rate_type);
  937. if (ret < 0) {
  938. pr_err("%s(): fail at getting rate_type, ret=%d\n",
  939. __func__, ret);
  940. ret = -EINVAL;
  941. goto done;
  942. }
  943. prtd->rate_type = rate_type;
  944. pr_debug("rate_type=%d\n", rate_type);
  945. } else if ((prtd->mode == MODE_IS127) ||
  946. (prtd->mode == MODE_4GV_NB) ||
  947. (prtd->mode == MODE_4GV_WB) ||
  948. (prtd->mode == MODE_4GV_NW)) {
  949. ret = voip_get_rate_type(prtd->mode,
  950. prtd->evrc_min_rate,
  951. &evrc_min_rate_type);
  952. if (ret < 0) {
  953. pr_err("%s(): fail at getting min rate, ret=%d\n",
  954. __func__, ret);
  955. ret = -EINVAL;
  956. goto done;
  957. }
  958. if (evrc_min_rate_type == VOC_0_RATE)
  959. evrc_min_rate_type = VOC_8_RATE;
  960. ret = voip_get_rate_type(prtd->mode,
  961. prtd->evrc_max_rate,
  962. &evrc_max_rate_type);
  963. if (ret < 0) {
  964. pr_err("%s(): fail at getting max rate, ret=%d\n",
  965. __func__, ret);
  966. ret = -EINVAL;
  967. goto done;
  968. }
  969. if (evrc_max_rate_type == VOC_0_RATE)
  970. evrc_max_rate_type = VOC_1_RATE;
  971. if (evrc_max_rate_type < evrc_min_rate_type) {
  972. pr_err("%s(): Invalid EVRC min max rates: %d, %d\n",
  973. __func__, evrc_min_rate_type,
  974. evrc_max_rate_type);
  975. ret = -EINVAL;
  976. goto done;
  977. }
  978. pr_debug("%s(): min rate=%d, max rate=%d\n",
  979. __func__, evrc_min_rate_type, evrc_max_rate_type);
  980. }
  981. ret = voip_get_media_type(prtd->mode,
  982. prtd->rate_type,
  983. prtd->play_samp_rate,
  984. &media_type);
  985. if (ret < 0) {
  986. pr_err("%s(): fail at getting media_type, ret=%d\n",
  987. __func__, ret);
  988. ret = -EINVAL;
  989. goto done;
  990. }
  991. pr_debug("%s(): media_type=%d\n", __func__, media_type);
  992. if ((prtd->play_samp_rate == 8000 && prtd->cap_samp_rate == 8000) ||
  993. (prtd->play_samp_rate == 16000 && prtd->cap_samp_rate == 16000) ||
  994. (prtd->play_samp_rate == 32000 && prtd->cap_samp_rate == 32000) ||
  995. (prtd->play_samp_rate == 48000 && prtd->cap_samp_rate == 48000)) {
  996. voc_config_vocoder(media_type, rate_type,
  997. VSS_NETWORK_ID_VOIP,
  998. voip_info.dtx_mode,
  999. evrc_min_rate_type,
  1000. evrc_max_rate_type);
  1001. } else {
  1002. pr_debug("%s: Invalid rate playback %d, capture %d\n",
  1003. __func__, prtd->play_samp_rate,
  1004. prtd->cap_samp_rate);
  1005. ret = -EINVAL;
  1006. }
  1007. done:
  1008. return ret;
  1009. }
  1010. static int msm_pcm_prepare(struct snd_pcm_substream *substream)
  1011. {
  1012. int ret = 0;
  1013. struct snd_pcm_runtime *runtime = substream->runtime;
  1014. struct voip_drv_info *prtd = runtime->private_data;
  1015. mutex_lock(&prtd->lock);
  1016. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  1017. ret = msm_pcm_playback_prepare(substream);
  1018. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  1019. ret = msm_pcm_capture_prepare(substream);
  1020. if (prtd->playback_prepare && prtd->capture_prepare
  1021. && (prtd->state != VOIP_STARTED)) {
  1022. ret = voip_config_vocoder(substream);
  1023. if (ret < 0) {
  1024. pr_err("%s(): fail at configuring vocoder for voip, ret=%d\n",
  1025. __func__, ret);
  1026. goto done;
  1027. }
  1028. /* Initialaizing cb variables */
  1029. voc_register_mvs_cb(voip_process_ul_pkt,
  1030. voip_process_dl_pkt,
  1031. voip_ssr_cb_fn, prtd);
  1032. ret = voc_start_voice_call(
  1033. voc_get_session_id(VOIP_SESSION_NAME));
  1034. if (ret < 0) {
  1035. pr_err("%s: voc_start_voice_call() failed err %d",
  1036. __func__, ret);
  1037. goto done;
  1038. }
  1039. prtd->state = VOIP_STARTED;
  1040. }
  1041. done:
  1042. mutex_unlock(&prtd->lock);
  1043. return ret;
  1044. }
  1045. static snd_pcm_uframes_t
  1046. msm_pcm_playback_pointer(struct snd_pcm_substream *substream)
  1047. {
  1048. struct snd_pcm_runtime *runtime = substream->runtime;
  1049. struct voip_drv_info *prtd = runtime->private_data;
  1050. pr_debug("%s\n", __func__);
  1051. if (prtd->pcm_playback_irq_pos >= prtd->pcm_size)
  1052. prtd->pcm_playback_irq_pos = 0;
  1053. return bytes_to_frames(runtime, (prtd->pcm_playback_irq_pos));
  1054. }
  1055. static snd_pcm_uframes_t
  1056. msm_pcm_capture_pointer(struct snd_pcm_substream *substream)
  1057. {
  1058. struct snd_pcm_runtime *runtime = substream->runtime;
  1059. struct voip_drv_info *prtd = runtime->private_data;
  1060. if (prtd->pcm_capture_irq_pos >= prtd->pcm_capture_size)
  1061. prtd->pcm_capture_irq_pos = 0;
  1062. return bytes_to_frames(runtime, (prtd->pcm_capture_irq_pos));
  1063. }
  1064. static snd_pcm_uframes_t msm_pcm_pointer(struct snd_pcm_substream *substream)
  1065. {
  1066. snd_pcm_uframes_t ret = 0;
  1067. pr_debug("%s\n", __func__);
  1068. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  1069. ret = msm_pcm_playback_pointer(substream);
  1070. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  1071. ret = msm_pcm_capture_pointer(substream);
  1072. return ret;
  1073. }
  1074. static int msm_pcm_mmap(struct snd_pcm_substream *substream,
  1075. struct vm_area_struct *vma)
  1076. {
  1077. struct snd_pcm_runtime *runtime = substream->runtime;
  1078. pr_debug("%s\n", __func__);
  1079. dma_mmap_coherent(substream->pcm->card->dev, vma,
  1080. runtime->dma_area,
  1081. runtime->dma_addr,
  1082. runtime->dma_bytes);
  1083. return 0;
  1084. }
  1085. static int msm_pcm_hw_params(struct snd_pcm_substream *substream,
  1086. struct snd_pcm_hw_params *params)
  1087. {
  1088. struct snd_pcm_runtime *runtime = substream->runtime;
  1089. struct snd_dma_buffer *dma_buf = &substream->dma_buffer;
  1090. struct voip_buf_node *buf_node = NULL;
  1091. int i = 0, offset = 0;
  1092. pr_debug("%s: voip\n", __func__);
  1093. mutex_lock(&voip_info.lock);
  1094. dma_buf->dev.type = SNDRV_DMA_TYPE_DEV;
  1095. dma_buf->dev.dev = substream->pcm->card->dev;
  1096. dma_buf->private_data = NULL;
  1097. dma_buf->area = dma_alloc_coherent(substream->pcm->card->dev,
  1098. runtime->hw.buffer_bytes_max,
  1099. &dma_buf->addr, GFP_KERNEL);
  1100. if (!dma_buf->area) {
  1101. pr_err("%s:MSM VOIP dma_alloc failed\n", __func__);
  1102. mutex_unlock(&voip_info.lock);
  1103. return -ENOMEM;
  1104. }
  1105. dma_buf->bytes = runtime->hw.buffer_bytes_max;
  1106. memset(dma_buf->area, 0, runtime->hw.buffer_bytes_max);
  1107. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  1108. for (i = 0; i < VOIP_MAX_Q_LEN; i++) {
  1109. buf_node = (void *)dma_buf->area + offset;
  1110. list_add_tail(&buf_node->list,
  1111. &voip_info.free_in_queue);
  1112. offset = offset + sizeof(struct voip_buf_node);
  1113. }
  1114. } else {
  1115. for (i = 0; i < VOIP_MAX_Q_LEN; i++) {
  1116. buf_node = (void *) dma_buf->area + offset;
  1117. list_add_tail(&buf_node->list,
  1118. &voip_info.free_out_queue);
  1119. offset = offset + sizeof(struct voip_buf_node);
  1120. }
  1121. }
  1122. mutex_unlock(&voip_info.lock);
  1123. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  1124. return 0;
  1125. }
  1126. static int msm_voip_mode_config_get(struct snd_kcontrol *kcontrol,
  1127. struct snd_ctl_elem_value *ucontrol)
  1128. {
  1129. mutex_lock(&voip_info.lock);
  1130. ucontrol->value.integer.value[0] = voip_info.mode;
  1131. mutex_unlock(&voip_info.lock);
  1132. return 0;
  1133. }
  1134. static int msm_voip_mode_config_put(struct snd_kcontrol *kcontrol,
  1135. struct snd_ctl_elem_value *ucontrol)
  1136. {
  1137. mutex_lock(&voip_info.lock);
  1138. voip_info.mode = ucontrol->value.integer.value[0];
  1139. pr_debug("%s: mode=%d\n", __func__, voip_info.mode);
  1140. mutex_unlock(&voip_info.lock);
  1141. return 0;
  1142. }
  1143. static int msm_voip_rate_config_put(struct snd_kcontrol *kcontrol,
  1144. struct snd_ctl_elem_value *ucontrol)
  1145. {
  1146. int ret = 0;
  1147. int rate = ucontrol->value.integer.value[0];
  1148. mutex_lock(&voip_info.lock);
  1149. if (voip_info.rate != rate) {
  1150. voip_info.rate = rate;
  1151. pr_debug("%s: rate=%d\n", __func__, voip_info.rate);
  1152. if (voip_info.state == VOIP_STARTED &&
  1153. (voip_info.mode == MODE_AMR ||
  1154. voip_info.mode == MODE_AMR_WB)) {
  1155. ret = voip_config_vocoder(
  1156. voip_info.capture_substream);
  1157. if (ret) {
  1158. pr_err("%s:Failed to configure vocoder, ret=%d\n",
  1159. __func__, ret);
  1160. goto done;
  1161. }
  1162. ret = voc_update_amr_vocoder_rate(
  1163. voc_get_session_id(VOIP_SESSION_NAME));
  1164. if (ret) {
  1165. pr_err("%s:Failed to update AMR rate, ret=%d\n",
  1166. __func__, ret);
  1167. }
  1168. }
  1169. }
  1170. done:
  1171. mutex_unlock(&voip_info.lock);
  1172. return ret;
  1173. }
  1174. static int msm_voip_evrc_min_max_rate_config_get(struct snd_kcontrol *kcontrol,
  1175. struct snd_ctl_elem_value *ucontrol)
  1176. {
  1177. mutex_lock(&voip_info.lock);
  1178. ucontrol->value.integer.value[0] = voip_info.evrc_min_rate;
  1179. ucontrol->value.integer.value[1] = voip_info.evrc_max_rate;
  1180. mutex_unlock(&voip_info.lock);
  1181. return 0;
  1182. }
  1183. static int msm_voip_evrc_min_max_rate_config_put(struct snd_kcontrol *kcontrol,
  1184. struct snd_ctl_elem_value *ucontrol)
  1185. {
  1186. mutex_lock(&voip_info.lock);
  1187. voip_info.evrc_min_rate = ucontrol->value.integer.value[0];
  1188. voip_info.evrc_max_rate = ucontrol->value.integer.value[1];
  1189. pr_debug("%s(): evrc_min_rate=%d,evrc_max_rate=%d\n", __func__,
  1190. voip_info.evrc_min_rate, voip_info.evrc_max_rate);
  1191. mutex_unlock(&voip_info.lock);
  1192. return 0;
  1193. }
  1194. static int voip_get_rate_type(uint32_t mode, uint32_t rate,
  1195. uint32_t *rate_type)
  1196. {
  1197. int ret = 0;
  1198. switch (mode) {
  1199. case MODE_AMR: {
  1200. switch (rate) {
  1201. case 4750:
  1202. *rate_type = AMR_RATE_4750;
  1203. break;
  1204. case 5150:
  1205. *rate_type = AMR_RATE_5150;
  1206. break;
  1207. case 5900:
  1208. *rate_type = AMR_RATE_5900;
  1209. break;
  1210. case 6700:
  1211. *rate_type = AMR_RATE_6700;
  1212. break;
  1213. case 7400:
  1214. *rate_type = AMR_RATE_7400;
  1215. break;
  1216. case 7950:
  1217. *rate_type = AMR_RATE_7950;
  1218. break;
  1219. case 10200:
  1220. *rate_type = AMR_RATE_10200;
  1221. break;
  1222. case 12200:
  1223. *rate_type = AMR_RATE_12200;
  1224. break;
  1225. default:
  1226. pr_err("wrong rate for AMR NB.\n");
  1227. ret = -EINVAL;
  1228. break;
  1229. }
  1230. break;
  1231. }
  1232. case MODE_AMR_WB: {
  1233. switch (rate) {
  1234. case 6600:
  1235. *rate_type = AMR_RATE_6600 - AMR_RATE_6600;
  1236. break;
  1237. case 8850:
  1238. *rate_type = AMR_RATE_8850 - AMR_RATE_6600;
  1239. break;
  1240. case 12650:
  1241. *rate_type = AMR_RATE_12650 - AMR_RATE_6600;
  1242. break;
  1243. case 14250:
  1244. *rate_type = AMR_RATE_14250 - AMR_RATE_6600;
  1245. break;
  1246. case 15850:
  1247. *rate_type = AMR_RATE_15850 - AMR_RATE_6600;
  1248. break;
  1249. case 18250:
  1250. *rate_type = AMR_RATE_18250 - AMR_RATE_6600;
  1251. break;
  1252. case 19850:
  1253. *rate_type = AMR_RATE_19850 - AMR_RATE_6600;
  1254. break;
  1255. case 23050:
  1256. *rate_type = AMR_RATE_23050 - AMR_RATE_6600;
  1257. break;
  1258. case 23850:
  1259. *rate_type = AMR_RATE_23850 - AMR_RATE_6600;
  1260. break;
  1261. default:
  1262. pr_err("wrong rate for AMR_WB.\n");
  1263. ret = -EINVAL;
  1264. break;
  1265. }
  1266. break;
  1267. }
  1268. case MODE_PCM: {
  1269. *rate_type = 0;
  1270. break;
  1271. }
  1272. case MODE_IS127:
  1273. case MODE_4GV_NB:
  1274. case MODE_4GV_WB: {
  1275. switch (rate) {
  1276. case VOC_0_RATE:
  1277. case VOC_8_RATE:
  1278. case VOC_4_RATE:
  1279. case VOC_2_RATE:
  1280. case VOC_1_RATE:
  1281. *rate_type = rate;
  1282. break;
  1283. default:
  1284. pr_err("wrong rate for IS127/4GV_NB/WB.\n");
  1285. ret = -EINVAL;
  1286. break;
  1287. }
  1288. break;
  1289. }
  1290. case MODE_4GV_NW: {
  1291. switch (rate) {
  1292. case VOC_0_RATE:
  1293. case VOC_8_RATE:
  1294. case VOC_4_RATE:
  1295. case VOC_2_RATE:
  1296. case VOC_1_RATE:
  1297. case VOC_8_RATE_NC:
  1298. *rate_type = rate;
  1299. break;
  1300. default:
  1301. pr_err("wrong rate for 4GV_NW.\n");
  1302. ret = -EINVAL;
  1303. break;
  1304. }
  1305. break;
  1306. }
  1307. case MODE_G711:
  1308. case MODE_G711A:
  1309. *rate_type = rate;
  1310. break;
  1311. default:
  1312. pr_err("wrong mode type.\n");
  1313. ret = -EINVAL;
  1314. }
  1315. pr_debug("%s, mode=%d, rate=%u, rate_type=%d\n",
  1316. __func__, mode, rate, *rate_type);
  1317. return ret;
  1318. }
  1319. static int voip_get_media_type(uint32_t mode, uint32_t rate_type,
  1320. unsigned int samp_rate,
  1321. unsigned int *media_type)
  1322. {
  1323. int ret = 0;
  1324. pr_debug("%s: mode=%d, samp_rate=%d\n", __func__,
  1325. mode, samp_rate);
  1326. switch (mode) {
  1327. case MODE_AMR:
  1328. *media_type = VSS_MEDIA_ID_AMR_NB_MODEM;
  1329. break;
  1330. case MODE_AMR_WB:
  1331. *media_type = VSS_MEDIA_ID_AMR_WB_MODEM;
  1332. break;
  1333. case MODE_PCM:
  1334. if (samp_rate == 8000)
  1335. *media_type = VSS_MEDIA_ID_PCM_8_KHZ;
  1336. else if (samp_rate == 16000)
  1337. *media_type = VSS_MEDIA_ID_PCM_16_KHZ;
  1338. else if (samp_rate == 32000)
  1339. *media_type = VSS_MEDIA_ID_PCM_32_KHZ;
  1340. else
  1341. *media_type = VSS_MEDIA_ID_PCM_48_KHZ;
  1342. break;
  1343. case MODE_IS127: /* EVRC-A */
  1344. *media_type = VSS_MEDIA_ID_EVRC_MODEM;
  1345. break;
  1346. case MODE_4GV_NB: /* EVRC-B */
  1347. *media_type = VSS_MEDIA_ID_4GV_NB_MODEM;
  1348. break;
  1349. case MODE_4GV_WB: /* EVRC-WB */
  1350. *media_type = VSS_MEDIA_ID_4GV_WB_MODEM;
  1351. break;
  1352. case MODE_4GV_NW: /* EVRC-NW */
  1353. *media_type = VSS_MEDIA_ID_4GV_NW_MODEM;
  1354. break;
  1355. case MODE_G711:
  1356. case MODE_G711A:
  1357. if (rate_type == MVS_G711A_MODE_MULAW)
  1358. *media_type = VSS_MEDIA_ID_G711_MULAW;
  1359. else
  1360. *media_type = VSS_MEDIA_ID_G711_ALAW;
  1361. break;
  1362. default:
  1363. pr_debug(" input mode is not supported\n");
  1364. ret = -EINVAL;
  1365. }
  1366. pr_debug("%s: media_type is 0x%x\n", __func__, *media_type);
  1367. return ret;
  1368. }
  1369. static const struct snd_pcm_ops msm_pcm_ops = {
  1370. .open = msm_pcm_open,
  1371. .copy_user = msm_pcm_copy,
  1372. .hw_params = msm_pcm_hw_params,
  1373. .close = msm_pcm_close,
  1374. .prepare = msm_pcm_prepare,
  1375. .trigger = msm_pcm_trigger,
  1376. .pointer = msm_pcm_pointer,
  1377. .mmap = msm_pcm_mmap,
  1378. };
  1379. static int msm_asoc_pcm_new(struct snd_soc_pcm_runtime *rtd)
  1380. {
  1381. struct snd_card *card = rtd->card->snd_card;
  1382. int ret = 0;
  1383. pr_debug("msm_asoc_pcm_new\n");
  1384. if (!card->dev->coherent_dma_mask)
  1385. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  1386. return ret;
  1387. }
  1388. static struct snd_soc_platform_driver msm_soc_platform = {
  1389. .ops = &msm_pcm_ops,
  1390. .pcm_new = msm_asoc_pcm_new,
  1391. .probe = msm_pcm_voip_probe,
  1392. };
  1393. static int msm_pcm_probe(struct platform_device *pdev)
  1394. {
  1395. int rc;
  1396. if (!is_voc_initialized()) {
  1397. pr_debug("%s: voice module not initialized yet, deferring probe()\n",
  1398. __func__);
  1399. rc = -EPROBE_DEFER;
  1400. goto done;
  1401. }
  1402. rc = voc_alloc_cal_shared_memory();
  1403. if (rc == -EPROBE_DEFER) {
  1404. pr_debug("%s: memory allocation for calibration deferred %d\n",
  1405. __func__, rc);
  1406. goto done;
  1407. } else if (rc < 0) {
  1408. pr_err("%s: memory allocation for calibration failed %d\n",
  1409. __func__, rc);
  1410. }
  1411. rc = voc_alloc_voip_shared_memory();
  1412. if (rc < 0) {
  1413. pr_err("%s: error allocating shared mem err %d\n",
  1414. __func__, rc);
  1415. }
  1416. pr_debug("%s: dev name %s\n", __func__, dev_name(&pdev->dev));
  1417. rc = snd_soc_register_platform(&pdev->dev,
  1418. &msm_soc_platform);
  1419. done:
  1420. return rc;
  1421. }
  1422. static int msm_pcm_remove(struct platform_device *pdev)
  1423. {
  1424. snd_soc_unregister_platform(&pdev->dev);
  1425. return 0;
  1426. }
  1427. static const struct of_device_id msm_voip_dt_match[] = {
  1428. {.compatible = "qcom,msm-voip-dsp"},
  1429. {}
  1430. };
  1431. MODULE_DEVICE_TABLE(of, msm_voip_dt_match);
  1432. static struct platform_driver msm_pcm_driver = {
  1433. .driver = {
  1434. .name = "msm-voip-dsp",
  1435. .owner = THIS_MODULE,
  1436. .of_match_table = msm_voip_dt_match,
  1437. },
  1438. .probe = msm_pcm_probe,
  1439. .remove = msm_pcm_remove,
  1440. };
  1441. int __init msm_pcm_voip_init(void)
  1442. {
  1443. memset(&voip_info, 0, sizeof(voip_info));
  1444. voip_info.mode = MODE_PCM;
  1445. mutex_init(&voip_info.lock);
  1446. spin_lock_init(&voip_info.dsp_lock);
  1447. spin_lock_init(&voip_info.dsp_ul_lock);
  1448. init_waitqueue_head(&voip_info.out_wait);
  1449. init_waitqueue_head(&voip_info.in_wait);
  1450. INIT_LIST_HEAD(&voip_info.in_queue);
  1451. INIT_LIST_HEAD(&voip_info.free_in_queue);
  1452. INIT_LIST_HEAD(&voip_info.out_queue);
  1453. INIT_LIST_HEAD(&voip_info.free_out_queue);
  1454. return platform_driver_register(&msm_pcm_driver);
  1455. }
  1456. void msm_pcm_voip_exit(void)
  1457. {
  1458. platform_driver_unregister(&msm_pcm_driver);
  1459. }
  1460. MODULE_DESCRIPTION("PCM module platform driver");
  1461. MODULE_LICENSE("GPL v2");