msm-pcm-voip-v2.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713
  1. /* Copyright (c) 2012-2017, 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. snd_pcm_uframes_t hwoff, void __user *buf, snd_pcm_uframes_t frames)
  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. int count = frames_to_bytes(runtime, frames);
  669. pr_debug("%s: count = %d, frames=%d\n", __func__, count, (int)frames);
  670. if (prtd->voip_reset) {
  671. pr_debug("%s: RESET event happened during VoIP\n", __func__);
  672. return -ENETRESET;
  673. }
  674. ret = wait_event_interruptible_timeout(prtd->in_wait,
  675. (!list_empty(&prtd->free_in_queue) ||
  676. prtd->state == VOIP_STOPPED),
  677. 1 * HZ);
  678. if (prtd->voip_reset) {
  679. pr_debug("%s: RESET event happened during VoIP\n", __func__);
  680. return -ENETRESET;
  681. }
  682. if (ret > 0) {
  683. if (count <= VOIP_MAX_VOC_PKT_SIZE) {
  684. spin_lock_irqsave(&prtd->dsp_lock, dsp_flags);
  685. buf_node =
  686. list_first_entry(&prtd->free_in_queue,
  687. struct voip_buf_node, list);
  688. list_del(&buf_node->list);
  689. spin_unlock_irqrestore(&prtd->dsp_lock, dsp_flags);
  690. if (prtd->mode == MODE_PCM) {
  691. ret = copy_from_user(&buf_node->frame.voc_pkt,
  692. buf, count);
  693. if (ret) {
  694. pr_err("%s: copy from user failed %d\n",
  695. __func__, ret);
  696. return -EFAULT;
  697. }
  698. buf_node->frame.pktlen = count;
  699. } else {
  700. ret = copy_from_user(&buf_node->frame,
  701. buf, count);
  702. if (ret) {
  703. pr_err("%s: copy from user failed %d\n",
  704. __func__, ret);
  705. return -EFAULT;
  706. }
  707. if (buf_node->frame.pktlen >= count)
  708. buf_node->frame.pktlen = count -
  709. (sizeof(buf_node->frame.frm_hdr) +
  710. sizeof(buf_node->frame.pktlen));
  711. }
  712. spin_lock_irqsave(&prtd->dsp_lock, dsp_flags);
  713. list_add_tail(&buf_node->list, &prtd->in_queue);
  714. spin_unlock_irqrestore(&prtd->dsp_lock, dsp_flags);
  715. } else {
  716. pr_err("%s: Write cnt %d is > VOIP_MAX_VOC_PKT_SIZE\n",
  717. __func__, count);
  718. ret = -ENOMEM;
  719. }
  720. } else if (ret == 0) {
  721. pr_err("%s: No free DL buffs\n", __func__);
  722. ret = -ETIMEDOUT;
  723. } else {
  724. pr_err("%s: playback copy was interrupted %d\n", __func__, ret);
  725. }
  726. return ret;
  727. }
  728. static int msm_pcm_capture_copy(struct snd_pcm_substream *substream,
  729. int channel, snd_pcm_uframes_t hwoff, void __user *buf,
  730. snd_pcm_uframes_t frames)
  731. {
  732. int ret = 0;
  733. int count = 0;
  734. struct voip_buf_node *buf_node = NULL;
  735. struct snd_pcm_runtime *runtime = substream->runtime;
  736. struct voip_drv_info *prtd = runtime->private_data;
  737. unsigned long dsp_flags;
  738. int size;
  739. count = frames_to_bytes(runtime, frames);
  740. pr_debug("%s: count = %d\n", __func__, count);
  741. if (prtd->voip_reset) {
  742. pr_debug("%s: RESET event happened during VoIP\n", __func__);
  743. return -ENETRESET;
  744. }
  745. ret = wait_event_interruptible_timeout(prtd->out_wait,
  746. (!list_empty(&prtd->out_queue) ||
  747. prtd->state == VOIP_STOPPED),
  748. 1 * HZ);
  749. if (prtd->voip_reset) {
  750. pr_debug("%s: RESET event happened during VoIP\n", __func__);
  751. return -ENETRESET;
  752. }
  753. if (ret > 0) {
  754. if (count <= VOIP_MAX_VOC_PKT_SIZE) {
  755. spin_lock_irqsave(&prtd->dsp_ul_lock, dsp_flags);
  756. buf_node = list_first_entry(&prtd->out_queue,
  757. struct voip_buf_node, list);
  758. list_del(&buf_node->list);
  759. spin_unlock_irqrestore(&prtd->dsp_ul_lock, dsp_flags);
  760. if (prtd->mode == MODE_PCM) {
  761. ret = copy_to_user(buf,
  762. &buf_node->frame.voc_pkt,
  763. buf_node->frame.pktlen);
  764. } else {
  765. size = sizeof(buf_node->frame.frm_hdr) +
  766. sizeof(buf_node->frame.pktlen) +
  767. buf_node->frame.pktlen;
  768. ret = copy_to_user(buf,
  769. &buf_node->frame,
  770. size);
  771. }
  772. if (ret) {
  773. pr_err("%s: Copy to user returned %d\n",
  774. __func__, ret);
  775. ret = -EFAULT;
  776. }
  777. spin_lock_irqsave(&prtd->dsp_ul_lock, dsp_flags);
  778. list_add_tail(&buf_node->list,
  779. &prtd->free_out_queue);
  780. spin_unlock_irqrestore(&prtd->dsp_ul_lock, dsp_flags);
  781. } else {
  782. pr_err("%s: Read count %d > VOIP_MAX_VOC_PKT_SIZE\n",
  783. __func__, count);
  784. ret = -ENOMEM;
  785. }
  786. } else if (ret == 0) {
  787. pr_err_ratelimited("%s: No UL data available\n", __func__);
  788. ret = -ETIMEDOUT;
  789. } else {
  790. pr_err("%s: Read was interrupted\n", __func__);
  791. ret = -ERESTARTSYS;
  792. }
  793. return ret;
  794. }
  795. static int msm_pcm_copy(struct snd_pcm_substream *substream, int a,
  796. snd_pcm_uframes_t hwoff, void __user *buf, snd_pcm_uframes_t frames)
  797. {
  798. int ret = 0;
  799. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  800. ret = msm_pcm_playback_copy(substream, a, hwoff, buf, frames);
  801. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  802. ret = msm_pcm_capture_copy(substream, a, hwoff, buf, frames);
  803. return ret;
  804. }
  805. static int msm_pcm_close(struct snd_pcm_substream *substream)
  806. {
  807. int ret = 0;
  808. struct list_head *ptr = NULL;
  809. struct list_head *next = NULL;
  810. struct voip_buf_node *buf_node = NULL;
  811. struct snd_dma_buffer *p_dma_buf, *c_dma_buf;
  812. struct snd_pcm_substream *p_substream, *c_substream;
  813. struct snd_pcm_runtime *runtime;
  814. struct voip_drv_info *prtd;
  815. unsigned long dsp_flags;
  816. if (substream == NULL) {
  817. pr_err("substream is NULL\n");
  818. return -EINVAL;
  819. }
  820. runtime = substream->runtime;
  821. prtd = runtime->private_data;
  822. wake_up(&prtd->out_wait);
  823. mutex_lock(&prtd->lock);
  824. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  825. prtd->playback_prepare = 0;
  826. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  827. prtd->capture_prepare = 0;
  828. if (!prtd->playback_prepare && !prtd->capture_prepare) {
  829. if (prtd->state == VOIP_STARTED) {
  830. prtd->voip_reset = false;
  831. prtd->state = VOIP_STOPPED;
  832. voc_end_voice_call(
  833. voc_get_session_id(VOIP_SESSION_NAME));
  834. voc_register_mvs_cb(NULL, NULL, NULL, prtd);
  835. }
  836. /* release all buffer */
  837. /* release in_queue and free_in_queue */
  838. pr_debug("release all buffer\n");
  839. p_substream = prtd->playback_substream;
  840. if (p_substream == NULL) {
  841. pr_debug("p_substream is NULL\n");
  842. goto capt;
  843. }
  844. p_dma_buf = &p_substream->dma_buffer;
  845. if (p_dma_buf == NULL) {
  846. pr_debug("p_dma_buf is NULL\n");
  847. goto capt;
  848. }
  849. if (p_dma_buf->area != NULL) {
  850. spin_lock_irqsave(&prtd->dsp_lock, dsp_flags);
  851. list_for_each_safe(ptr, next, &prtd->in_queue) {
  852. buf_node = list_entry(ptr,
  853. struct voip_buf_node, list);
  854. list_del(&buf_node->list);
  855. }
  856. list_for_each_safe(ptr, next, &prtd->free_in_queue) {
  857. buf_node = list_entry(ptr,
  858. struct voip_buf_node, list);
  859. list_del(&buf_node->list);
  860. }
  861. spin_unlock_irqrestore(&prtd->dsp_lock, dsp_flags);
  862. dma_free_coherent(p_substream->pcm->card->dev,
  863. runtime->hw.buffer_bytes_max, p_dma_buf->area,
  864. p_dma_buf->addr);
  865. p_dma_buf->area = NULL;
  866. }
  867. /* release out_queue and free_out_queue */
  868. capt: c_substream = prtd->capture_substream;
  869. if (c_substream == NULL) {
  870. pr_debug("c_substream is NULL\n");
  871. goto done;
  872. }
  873. c_dma_buf = &c_substream->dma_buffer;
  874. if (c_substream == NULL) {
  875. pr_debug("c_dma_buf is NULL.\n");
  876. goto done;
  877. }
  878. if (c_dma_buf->area != NULL) {
  879. spin_lock_irqsave(&prtd->dsp_ul_lock, dsp_flags);
  880. list_for_each_safe(ptr, next, &prtd->out_queue) {
  881. buf_node = list_entry(ptr,
  882. struct voip_buf_node, list);
  883. list_del(&buf_node->list);
  884. }
  885. list_for_each_safe(ptr, next, &prtd->free_out_queue) {
  886. buf_node = list_entry(ptr,
  887. struct voip_buf_node, list);
  888. list_del(&buf_node->list);
  889. }
  890. spin_unlock_irqrestore(&prtd->dsp_ul_lock, dsp_flags);
  891. dma_free_coherent(c_substream->pcm->card->dev,
  892. runtime->hw.buffer_bytes_max, c_dma_buf->area,
  893. c_dma_buf->addr);
  894. c_dma_buf->area = NULL;
  895. }
  896. done:
  897. prtd->capture_substream = NULL;
  898. prtd->playback_substream = NULL;
  899. }
  900. mutex_unlock(&prtd->lock);
  901. return ret;
  902. }
  903. static int voip_config_vocoder(struct snd_pcm_substream *substream)
  904. {
  905. int ret = 0;
  906. struct snd_pcm_runtime *runtime = substream->runtime;
  907. struct voip_drv_info *prtd = runtime->private_data;
  908. uint32_t media_type = 0;
  909. uint32_t rate_type = 0;
  910. uint32_t evrc_min_rate_type = 0;
  911. uint32_t evrc_max_rate_type = 0;
  912. pr_debug("%s(): mode=%d, playback rate=%d, capture rate=%d\n",
  913. __func__, prtd->mode, prtd->play_samp_rate,
  914. prtd->cap_samp_rate);
  915. if ((runtime->format != FORMAT_S16_LE &&
  916. runtime->format != FORMAT_SPECIAL) &&
  917. ((prtd->mode == MODE_AMR) || (prtd->mode == MODE_AMR_WB) ||
  918. (prtd->mode == MODE_IS127) || (prtd->mode == MODE_4GV_NB) ||
  919. (prtd->mode == MODE_4GV_WB) || (prtd->mode == MODE_4GV_NW) ||
  920. (prtd->mode == MODE_G711) || (prtd->mode == MODE_G711A))) {
  921. pr_err("%s(): mode:%d and format:%u are not matched\n",
  922. __func__, prtd->mode, (uint32_t)runtime->format);
  923. ret = -EINVAL;
  924. goto done;
  925. }
  926. if (runtime->format != FORMAT_S16_LE && (prtd->mode == MODE_PCM)) {
  927. pr_err("%s(): mode:%d and format:%u are not matched\n",
  928. __func__, prtd->mode, runtime->format);
  929. ret = -EINVAL;
  930. goto done;
  931. }
  932. if ((prtd->mode == MODE_PCM) ||
  933. (prtd->mode == MODE_AMR) ||
  934. (prtd->mode == MODE_AMR_WB) ||
  935. (prtd->mode == MODE_G711) ||
  936. (prtd->mode == MODE_G711A)) {
  937. ret = voip_get_rate_type(prtd->mode,
  938. prtd->rate,
  939. &rate_type);
  940. if (ret < 0) {
  941. pr_err("%s(): fail at getting rate_type, ret=%d\n",
  942. __func__, ret);
  943. ret = -EINVAL;
  944. goto done;
  945. }
  946. prtd->rate_type = rate_type;
  947. pr_debug("rate_type=%d\n", rate_type);
  948. } else if ((prtd->mode == MODE_IS127) ||
  949. (prtd->mode == MODE_4GV_NB) ||
  950. (prtd->mode == MODE_4GV_WB) ||
  951. (prtd->mode == MODE_4GV_NW)) {
  952. ret = voip_get_rate_type(prtd->mode,
  953. prtd->evrc_min_rate,
  954. &evrc_min_rate_type);
  955. if (ret < 0) {
  956. pr_err("%s(): fail at getting min rate, ret=%d\n",
  957. __func__, ret);
  958. ret = -EINVAL;
  959. goto done;
  960. }
  961. if (evrc_min_rate_type == VOC_0_RATE)
  962. evrc_min_rate_type = VOC_8_RATE;
  963. ret = voip_get_rate_type(prtd->mode,
  964. prtd->evrc_max_rate,
  965. &evrc_max_rate_type);
  966. if (ret < 0) {
  967. pr_err("%s(): fail at getting max rate, ret=%d\n",
  968. __func__, ret);
  969. ret = -EINVAL;
  970. goto done;
  971. }
  972. if (evrc_max_rate_type == VOC_0_RATE)
  973. evrc_max_rate_type = VOC_1_RATE;
  974. if (evrc_max_rate_type < evrc_min_rate_type) {
  975. pr_err("%s(): Invalid EVRC min max rates: %d, %d\n",
  976. __func__, evrc_min_rate_type,
  977. evrc_max_rate_type);
  978. ret = -EINVAL;
  979. goto done;
  980. }
  981. pr_debug("%s(): min rate=%d, max rate=%d\n",
  982. __func__, evrc_min_rate_type, evrc_max_rate_type);
  983. }
  984. ret = voip_get_media_type(prtd->mode,
  985. prtd->rate_type,
  986. prtd->play_samp_rate,
  987. &media_type);
  988. if (ret < 0) {
  989. pr_err("%s(): fail at getting media_type, ret=%d\n",
  990. __func__, ret);
  991. ret = -EINVAL;
  992. goto done;
  993. }
  994. pr_debug("%s(): media_type=%d\n", __func__, media_type);
  995. if ((prtd->play_samp_rate == 8000 && prtd->cap_samp_rate == 8000) ||
  996. (prtd->play_samp_rate == 16000 && prtd->cap_samp_rate == 16000) ||
  997. (prtd->play_samp_rate == 32000 && prtd->cap_samp_rate == 32000) ||
  998. (prtd->play_samp_rate == 48000 && prtd->cap_samp_rate == 48000)) {
  999. voc_config_vocoder(media_type, rate_type,
  1000. VSS_NETWORK_ID_VOIP,
  1001. voip_info.dtx_mode,
  1002. evrc_min_rate_type,
  1003. evrc_max_rate_type);
  1004. } else {
  1005. pr_debug("%s: Invalid rate playback %d, capture %d\n",
  1006. __func__, prtd->play_samp_rate,
  1007. prtd->cap_samp_rate);
  1008. ret = -EINVAL;
  1009. }
  1010. done:
  1011. return ret;
  1012. }
  1013. static int msm_pcm_prepare(struct snd_pcm_substream *substream)
  1014. {
  1015. int ret = 0;
  1016. struct snd_pcm_runtime *runtime = substream->runtime;
  1017. struct voip_drv_info *prtd = runtime->private_data;
  1018. mutex_lock(&prtd->lock);
  1019. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  1020. ret = msm_pcm_playback_prepare(substream);
  1021. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  1022. ret = msm_pcm_capture_prepare(substream);
  1023. if (prtd->playback_prepare && prtd->capture_prepare
  1024. && (prtd->state != VOIP_STARTED)) {
  1025. ret = voip_config_vocoder(substream);
  1026. if (ret < 0) {
  1027. pr_err("%s(): fail at configuring vocoder for voip, ret=%d\n",
  1028. __func__, ret);
  1029. goto done;
  1030. }
  1031. /* Initialaizing cb variables */
  1032. voc_register_mvs_cb(voip_process_ul_pkt,
  1033. voip_process_dl_pkt,
  1034. voip_ssr_cb_fn, prtd);
  1035. ret = voc_start_voice_call(
  1036. voc_get_session_id(VOIP_SESSION_NAME));
  1037. if (ret < 0) {
  1038. pr_err("%s: voc_start_voice_call() failed err %d",
  1039. __func__, ret);
  1040. goto done;
  1041. }
  1042. prtd->state = VOIP_STARTED;
  1043. }
  1044. done:
  1045. mutex_unlock(&prtd->lock);
  1046. return ret;
  1047. }
  1048. static snd_pcm_uframes_t
  1049. msm_pcm_playback_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. pr_debug("%s\n", __func__);
  1054. if (prtd->pcm_playback_irq_pos >= prtd->pcm_size)
  1055. prtd->pcm_playback_irq_pos = 0;
  1056. return bytes_to_frames(runtime, (prtd->pcm_playback_irq_pos));
  1057. }
  1058. static snd_pcm_uframes_t
  1059. msm_pcm_capture_pointer(struct snd_pcm_substream *substream)
  1060. {
  1061. struct snd_pcm_runtime *runtime = substream->runtime;
  1062. struct voip_drv_info *prtd = runtime->private_data;
  1063. if (prtd->pcm_capture_irq_pos >= prtd->pcm_capture_size)
  1064. prtd->pcm_capture_irq_pos = 0;
  1065. return bytes_to_frames(runtime, (prtd->pcm_capture_irq_pos));
  1066. }
  1067. static snd_pcm_uframes_t msm_pcm_pointer(struct snd_pcm_substream *substream)
  1068. {
  1069. snd_pcm_uframes_t ret = 0;
  1070. pr_debug("%s\n", __func__);
  1071. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  1072. ret = msm_pcm_playback_pointer(substream);
  1073. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  1074. ret = msm_pcm_capture_pointer(substream);
  1075. return ret;
  1076. }
  1077. static int msm_pcm_mmap(struct snd_pcm_substream *substream,
  1078. struct vm_area_struct *vma)
  1079. {
  1080. struct snd_pcm_runtime *runtime = substream->runtime;
  1081. pr_debug("%s\n", __func__);
  1082. dma_mmap_coherent(substream->pcm->card->dev, vma,
  1083. runtime->dma_area,
  1084. runtime->dma_addr,
  1085. runtime->dma_bytes);
  1086. return 0;
  1087. }
  1088. static int msm_pcm_hw_params(struct snd_pcm_substream *substream,
  1089. struct snd_pcm_hw_params *params)
  1090. {
  1091. struct snd_pcm_runtime *runtime = substream->runtime;
  1092. struct snd_dma_buffer *dma_buf = &substream->dma_buffer;
  1093. struct voip_buf_node *buf_node = NULL;
  1094. int i = 0, offset = 0;
  1095. pr_debug("%s: voip\n", __func__);
  1096. mutex_lock(&voip_info.lock);
  1097. dma_buf->dev.type = SNDRV_DMA_TYPE_DEV;
  1098. dma_buf->dev.dev = substream->pcm->card->dev;
  1099. dma_buf->private_data = NULL;
  1100. dma_buf->area = dma_alloc_coherent(substream->pcm->card->dev,
  1101. runtime->hw.buffer_bytes_max,
  1102. &dma_buf->addr, GFP_KERNEL);
  1103. if (!dma_buf->area) {
  1104. pr_err("%s:MSM VOIP dma_alloc failed\n", __func__);
  1105. mutex_unlock(&voip_info.lock);
  1106. return -ENOMEM;
  1107. }
  1108. dma_buf->bytes = runtime->hw.buffer_bytes_max;
  1109. memset(dma_buf->area, 0, runtime->hw.buffer_bytes_max);
  1110. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  1111. for (i = 0; i < VOIP_MAX_Q_LEN; i++) {
  1112. buf_node = (void *)dma_buf->area + offset;
  1113. list_add_tail(&buf_node->list,
  1114. &voip_info.free_in_queue);
  1115. offset = offset + sizeof(struct voip_buf_node);
  1116. }
  1117. } else {
  1118. for (i = 0; i < VOIP_MAX_Q_LEN; i++) {
  1119. buf_node = (void *) dma_buf->area + offset;
  1120. list_add_tail(&buf_node->list,
  1121. &voip_info.free_out_queue);
  1122. offset = offset + sizeof(struct voip_buf_node);
  1123. }
  1124. }
  1125. mutex_unlock(&voip_info.lock);
  1126. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  1127. return 0;
  1128. }
  1129. static int msm_voip_mode_config_get(struct snd_kcontrol *kcontrol,
  1130. struct snd_ctl_elem_value *ucontrol)
  1131. {
  1132. mutex_lock(&voip_info.lock);
  1133. ucontrol->value.integer.value[0] = voip_info.mode;
  1134. mutex_unlock(&voip_info.lock);
  1135. return 0;
  1136. }
  1137. static int msm_voip_mode_config_put(struct snd_kcontrol *kcontrol,
  1138. struct snd_ctl_elem_value *ucontrol)
  1139. {
  1140. mutex_lock(&voip_info.lock);
  1141. voip_info.mode = ucontrol->value.integer.value[0];
  1142. pr_debug("%s: mode=%d\n", __func__, voip_info.mode);
  1143. mutex_unlock(&voip_info.lock);
  1144. return 0;
  1145. }
  1146. static int msm_voip_rate_config_put(struct snd_kcontrol *kcontrol,
  1147. struct snd_ctl_elem_value *ucontrol)
  1148. {
  1149. int ret = 0;
  1150. int rate = ucontrol->value.integer.value[0];
  1151. mutex_lock(&voip_info.lock);
  1152. if (voip_info.rate != rate) {
  1153. voip_info.rate = rate;
  1154. pr_debug("%s: rate=%d\n", __func__, voip_info.rate);
  1155. if (voip_info.state == VOIP_STARTED &&
  1156. (voip_info.mode == MODE_AMR ||
  1157. voip_info.mode == MODE_AMR_WB)) {
  1158. ret = voip_config_vocoder(
  1159. voip_info.capture_substream);
  1160. if (ret) {
  1161. pr_err("%s:Failed to configure vocoder, ret=%d\n",
  1162. __func__, ret);
  1163. goto done;
  1164. }
  1165. ret = voc_update_amr_vocoder_rate(
  1166. voc_get_session_id(VOIP_SESSION_NAME));
  1167. if (ret) {
  1168. pr_err("%s:Failed to update AMR rate, ret=%d\n",
  1169. __func__, ret);
  1170. }
  1171. }
  1172. }
  1173. done:
  1174. mutex_unlock(&voip_info.lock);
  1175. return ret;
  1176. }
  1177. static int msm_voip_evrc_min_max_rate_config_get(struct snd_kcontrol *kcontrol,
  1178. struct snd_ctl_elem_value *ucontrol)
  1179. {
  1180. mutex_lock(&voip_info.lock);
  1181. ucontrol->value.integer.value[0] = voip_info.evrc_min_rate;
  1182. ucontrol->value.integer.value[1] = voip_info.evrc_max_rate;
  1183. mutex_unlock(&voip_info.lock);
  1184. return 0;
  1185. }
  1186. static int msm_voip_evrc_min_max_rate_config_put(struct snd_kcontrol *kcontrol,
  1187. struct snd_ctl_elem_value *ucontrol)
  1188. {
  1189. mutex_lock(&voip_info.lock);
  1190. voip_info.evrc_min_rate = ucontrol->value.integer.value[0];
  1191. voip_info.evrc_max_rate = ucontrol->value.integer.value[1];
  1192. pr_debug("%s(): evrc_min_rate=%d,evrc_max_rate=%d\n", __func__,
  1193. voip_info.evrc_min_rate, voip_info.evrc_max_rate);
  1194. mutex_unlock(&voip_info.lock);
  1195. return 0;
  1196. }
  1197. static int voip_get_rate_type(uint32_t mode, uint32_t rate,
  1198. uint32_t *rate_type)
  1199. {
  1200. int ret = 0;
  1201. switch (mode) {
  1202. case MODE_AMR: {
  1203. switch (rate) {
  1204. case 4750:
  1205. *rate_type = AMR_RATE_4750;
  1206. break;
  1207. case 5150:
  1208. *rate_type = AMR_RATE_5150;
  1209. break;
  1210. case 5900:
  1211. *rate_type = AMR_RATE_5900;
  1212. break;
  1213. case 6700:
  1214. *rate_type = AMR_RATE_6700;
  1215. break;
  1216. case 7400:
  1217. *rate_type = AMR_RATE_7400;
  1218. break;
  1219. case 7950:
  1220. *rate_type = AMR_RATE_7950;
  1221. break;
  1222. case 10200:
  1223. *rate_type = AMR_RATE_10200;
  1224. break;
  1225. case 12200:
  1226. *rate_type = AMR_RATE_12200;
  1227. break;
  1228. default:
  1229. pr_err("wrong rate for AMR NB.\n");
  1230. ret = -EINVAL;
  1231. break;
  1232. }
  1233. break;
  1234. }
  1235. case MODE_AMR_WB: {
  1236. switch (rate) {
  1237. case 6600:
  1238. *rate_type = AMR_RATE_6600 - AMR_RATE_6600;
  1239. break;
  1240. case 8850:
  1241. *rate_type = AMR_RATE_8850 - AMR_RATE_6600;
  1242. break;
  1243. case 12650:
  1244. *rate_type = AMR_RATE_12650 - AMR_RATE_6600;
  1245. break;
  1246. case 14250:
  1247. *rate_type = AMR_RATE_14250 - AMR_RATE_6600;
  1248. break;
  1249. case 15850:
  1250. *rate_type = AMR_RATE_15850 - AMR_RATE_6600;
  1251. break;
  1252. case 18250:
  1253. *rate_type = AMR_RATE_18250 - AMR_RATE_6600;
  1254. break;
  1255. case 19850:
  1256. *rate_type = AMR_RATE_19850 - AMR_RATE_6600;
  1257. break;
  1258. case 23050:
  1259. *rate_type = AMR_RATE_23050 - AMR_RATE_6600;
  1260. break;
  1261. case 23850:
  1262. *rate_type = AMR_RATE_23850 - AMR_RATE_6600;
  1263. break;
  1264. default:
  1265. pr_err("wrong rate for AMR_WB.\n");
  1266. ret = -EINVAL;
  1267. break;
  1268. }
  1269. break;
  1270. }
  1271. case MODE_PCM: {
  1272. *rate_type = 0;
  1273. break;
  1274. }
  1275. case MODE_IS127:
  1276. case MODE_4GV_NB:
  1277. case MODE_4GV_WB: {
  1278. switch (rate) {
  1279. case VOC_0_RATE:
  1280. case VOC_8_RATE:
  1281. case VOC_4_RATE:
  1282. case VOC_2_RATE:
  1283. case VOC_1_RATE:
  1284. *rate_type = rate;
  1285. break;
  1286. default:
  1287. pr_err("wrong rate for IS127/4GV_NB/WB.\n");
  1288. ret = -EINVAL;
  1289. break;
  1290. }
  1291. break;
  1292. }
  1293. case MODE_4GV_NW: {
  1294. switch (rate) {
  1295. case VOC_0_RATE:
  1296. case VOC_8_RATE:
  1297. case VOC_4_RATE:
  1298. case VOC_2_RATE:
  1299. case VOC_1_RATE:
  1300. case VOC_8_RATE_NC:
  1301. *rate_type = rate;
  1302. break;
  1303. default:
  1304. pr_err("wrong rate for 4GV_NW.\n");
  1305. ret = -EINVAL;
  1306. break;
  1307. }
  1308. break;
  1309. }
  1310. case MODE_G711:
  1311. case MODE_G711A:
  1312. *rate_type = rate;
  1313. break;
  1314. default:
  1315. pr_err("wrong mode type.\n");
  1316. ret = -EINVAL;
  1317. }
  1318. pr_debug("%s, mode=%d, rate=%u, rate_type=%d\n",
  1319. __func__, mode, rate, *rate_type);
  1320. return ret;
  1321. }
  1322. static int voip_get_media_type(uint32_t mode, uint32_t rate_type,
  1323. unsigned int samp_rate,
  1324. unsigned int *media_type)
  1325. {
  1326. int ret = 0;
  1327. pr_debug("%s: mode=%d, samp_rate=%d\n", __func__,
  1328. mode, samp_rate);
  1329. switch (mode) {
  1330. case MODE_AMR:
  1331. *media_type = VSS_MEDIA_ID_AMR_NB_MODEM;
  1332. break;
  1333. case MODE_AMR_WB:
  1334. *media_type = VSS_MEDIA_ID_AMR_WB_MODEM;
  1335. break;
  1336. case MODE_PCM:
  1337. if (samp_rate == 8000)
  1338. *media_type = VSS_MEDIA_ID_PCM_8_KHZ;
  1339. else if (samp_rate == 16000)
  1340. *media_type = VSS_MEDIA_ID_PCM_16_KHZ;
  1341. else if (samp_rate == 32000)
  1342. *media_type = VSS_MEDIA_ID_PCM_32_KHZ;
  1343. else
  1344. *media_type = VSS_MEDIA_ID_PCM_48_KHZ;
  1345. break;
  1346. case MODE_IS127: /* EVRC-A */
  1347. *media_type = VSS_MEDIA_ID_EVRC_MODEM;
  1348. break;
  1349. case MODE_4GV_NB: /* EVRC-B */
  1350. *media_type = VSS_MEDIA_ID_4GV_NB_MODEM;
  1351. break;
  1352. case MODE_4GV_WB: /* EVRC-WB */
  1353. *media_type = VSS_MEDIA_ID_4GV_WB_MODEM;
  1354. break;
  1355. case MODE_4GV_NW: /* EVRC-NW */
  1356. *media_type = VSS_MEDIA_ID_4GV_NW_MODEM;
  1357. break;
  1358. case MODE_G711:
  1359. case MODE_G711A:
  1360. if (rate_type == MVS_G711A_MODE_MULAW)
  1361. *media_type = VSS_MEDIA_ID_G711_MULAW;
  1362. else
  1363. *media_type = VSS_MEDIA_ID_G711_ALAW;
  1364. break;
  1365. default:
  1366. pr_debug(" input mode is not supported\n");
  1367. ret = -EINVAL;
  1368. }
  1369. pr_debug("%s: media_type is 0x%x\n", __func__, *media_type);
  1370. return ret;
  1371. }
  1372. static const struct snd_pcm_ops msm_pcm_ops = {
  1373. .open = msm_pcm_open,
  1374. .copy = msm_pcm_copy,
  1375. .hw_params = msm_pcm_hw_params,
  1376. .close = msm_pcm_close,
  1377. .prepare = msm_pcm_prepare,
  1378. .trigger = msm_pcm_trigger,
  1379. .pointer = msm_pcm_pointer,
  1380. .mmap = msm_pcm_mmap,
  1381. };
  1382. static int msm_asoc_pcm_new(struct snd_soc_pcm_runtime *rtd)
  1383. {
  1384. struct snd_card *card = rtd->card->snd_card;
  1385. int ret = 0;
  1386. pr_debug("msm_asoc_pcm_new\n");
  1387. if (!card->dev->coherent_dma_mask)
  1388. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  1389. return ret;
  1390. }
  1391. static struct snd_soc_platform_driver msm_soc_platform = {
  1392. .ops = &msm_pcm_ops,
  1393. .pcm_new = msm_asoc_pcm_new,
  1394. .probe = msm_pcm_voip_probe,
  1395. };
  1396. static int msm_pcm_probe(struct platform_device *pdev)
  1397. {
  1398. int rc;
  1399. if (!is_voc_initialized()) {
  1400. pr_debug("%s: voice module not initialized yet, deferring probe()\n",
  1401. __func__);
  1402. rc = -EPROBE_DEFER;
  1403. goto done;
  1404. }
  1405. rc = voc_alloc_cal_shared_memory();
  1406. if (rc == -EPROBE_DEFER) {
  1407. pr_debug("%s: memory allocation for calibration deferred %d\n",
  1408. __func__, rc);
  1409. goto done;
  1410. } else if (rc < 0) {
  1411. pr_err("%s: memory allocation for calibration failed %d\n",
  1412. __func__, rc);
  1413. }
  1414. rc = voc_alloc_voip_shared_memory();
  1415. if (rc < 0) {
  1416. pr_err("%s: error allocating shared mem err %d\n",
  1417. __func__, rc);
  1418. }
  1419. pr_debug("%s: dev name %s\n", __func__, dev_name(&pdev->dev));
  1420. rc = snd_soc_register_platform(&pdev->dev,
  1421. &msm_soc_platform);
  1422. done:
  1423. return rc;
  1424. }
  1425. static int msm_pcm_remove(struct platform_device *pdev)
  1426. {
  1427. snd_soc_unregister_platform(&pdev->dev);
  1428. return 0;
  1429. }
  1430. static const struct of_device_id msm_voip_dt_match[] = {
  1431. {.compatible = "qcom,msm-voip-dsp"},
  1432. {}
  1433. };
  1434. MODULE_DEVICE_TABLE(of, msm_voip_dt_match);
  1435. static struct platform_driver msm_pcm_driver = {
  1436. .driver = {
  1437. .name = "msm-voip-dsp",
  1438. .owner = THIS_MODULE,
  1439. .of_match_table = msm_voip_dt_match,
  1440. },
  1441. .probe = msm_pcm_probe,
  1442. .remove = msm_pcm_remove,
  1443. };
  1444. int __init msm_pcm_voip_init(void)
  1445. {
  1446. memset(&voip_info, 0, sizeof(voip_info));
  1447. voip_info.mode = MODE_PCM;
  1448. mutex_init(&voip_info.lock);
  1449. spin_lock_init(&voip_info.dsp_lock);
  1450. spin_lock_init(&voip_info.dsp_ul_lock);
  1451. init_waitqueue_head(&voip_info.out_wait);
  1452. init_waitqueue_head(&voip_info.in_wait);
  1453. INIT_LIST_HEAD(&voip_info.in_queue);
  1454. INIT_LIST_HEAD(&voip_info.free_in_queue);
  1455. INIT_LIST_HEAD(&voip_info.out_queue);
  1456. INIT_LIST_HEAD(&voip_info.free_out_queue);
  1457. return platform_driver_register(&msm_pcm_driver);
  1458. }
  1459. void msm_pcm_voip_exit(void)
  1460. {
  1461. platform_driver_unregister(&msm_pcm_driver);
  1462. }
  1463. MODULE_DESCRIPTION("PCM module platform driver");
  1464. MODULE_LICENSE("GPL v2");