msm-pcm-voip-v2.c 43 KB

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