msm-pcm-voip-v2.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722
  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 "msm-pcm-q6-v2.h"
  29. #include "msm-pcm-routing-v2.h"
  30. #include "q6voice.h"
  31. #include "audio_ocmem.h"
  32. #define SHARED_MEM_BUF 2
  33. #define VOIP_MIN_Q_LEN 2
  34. #define VOIP_MAX_Q_LEN 10
  35. #define VOIP_MAX_VOC_PKT_SIZE 4096
  36. #define VOIP_MIN_VOC_PKT_SIZE 320
  37. /* Length of the DSP frame info header added to the voc packet. */
  38. #define DSP_FRAME_HDR_LEN 1
  39. #define MODE_IS127 0x2
  40. #define MODE_4GV_NB 0x3
  41. #define MODE_4GV_WB 0x4
  42. #define MODE_AMR 0x5
  43. #define MODE_AMR_WB 0xD
  44. #define MODE_PCM 0xC
  45. #define MODE_4GV_NW 0xE
  46. #define MODE_G711 0xA
  47. #define MODE_G711A 0xF
  48. enum msm_audio_g711a_frame_type {
  49. MVS_G711A_SPEECH_GOOD,
  50. MVS_G711A_SID,
  51. MVS_G711A_NO_DATA,
  52. MVS_G711A_ERASURE
  53. };
  54. enum msm_audio_g711a_mode {
  55. MVS_G711A_MODE_MULAW,
  56. MVS_G711A_MODE_ALAW
  57. };
  58. enum msm_audio_g711_mode {
  59. MVS_G711_MODE_MULAW,
  60. MVS_G711_MODE_ALAW
  61. };
  62. #define VOIP_MODE_MAX MODE_G711A
  63. #define VOIP_RATE_MAX 23850
  64. enum format {
  65. FORMAT_S16_LE = 2,
  66. FORMAT_SPECIAL = 31,
  67. };
  68. enum amr_rate_type {
  69. AMR_RATE_4750, /* AMR 4.75 kbps */
  70. AMR_RATE_5150, /* AMR 5.15 kbps */
  71. AMR_RATE_5900, /* AMR 5.90 kbps */
  72. AMR_RATE_6700, /* AMR 6.70 kbps */
  73. AMR_RATE_7400, /* AMR 7.40 kbps */
  74. AMR_RATE_7950, /* AMR 7.95 kbps */
  75. AMR_RATE_10200, /* AMR 10.20 kbps */
  76. AMR_RATE_12200, /* AMR 12.20 kbps */
  77. AMR_RATE_6600, /* AMR-WB 6.60 kbps */
  78. AMR_RATE_8850, /* AMR-WB 8.85 kbps */
  79. AMR_RATE_12650, /* AMR-WB 12.65 kbps */
  80. AMR_RATE_14250, /* AMR-WB 14.25 kbps */
  81. AMR_RATE_15850, /* AMR-WB 15.85 kbps */
  82. AMR_RATE_18250, /* AMR-WB 18.25 kbps */
  83. AMR_RATE_19850, /* AMR-WB 19.85 kbps */
  84. AMR_RATE_23050, /* AMR-WB 23.05 kbps */
  85. AMR_RATE_23850, /* AMR-WB 23.85 kbps */
  86. AMR_RATE_UNDEF
  87. };
  88. enum voip_state {
  89. VOIP_STOPPED,
  90. VOIP_STARTED,
  91. };
  92. struct voip_frame_hdr {
  93. uint32_t timestamp;
  94. union {
  95. /*
  96. * Bits 0-15: Frame type
  97. * Bits 16-31: Frame rate
  98. */
  99. uint32_t frame_type;
  100. uint32_t packet_rate;
  101. };
  102. };
  103. struct voip_frame {
  104. struct voip_frame_hdr frm_hdr;
  105. uint32_t pktlen;
  106. uint8_t voc_pkt[VOIP_MAX_VOC_PKT_SIZE];
  107. };
  108. struct voip_buf_node {
  109. struct list_head list;
  110. struct voip_frame frame;
  111. };
  112. struct voip_drv_info {
  113. enum voip_state state;
  114. struct snd_pcm_substream *playback_substream;
  115. struct snd_pcm_substream *capture_substream;
  116. struct list_head in_queue;
  117. struct list_head free_in_queue;
  118. struct list_head out_queue;
  119. struct list_head free_out_queue;
  120. wait_queue_head_t out_wait;
  121. wait_queue_head_t in_wait;
  122. struct mutex lock;
  123. spinlock_t dsp_lock;
  124. spinlock_t dsp_ul_lock;
  125. bool voip_reset;
  126. uint32_t mode;
  127. uint32_t rate_type;
  128. uint32_t rate;
  129. uint32_t dtx_mode;
  130. uint8_t capture_start;
  131. uint8_t playback_start;
  132. uint8_t playback_instance;
  133. uint8_t capture_instance;
  134. unsigned int play_samp_rate;
  135. unsigned int cap_samp_rate;
  136. unsigned int pcm_size;
  137. unsigned int pcm_count;
  138. unsigned int pcm_playback_irq_pos; /* IRQ position */
  139. unsigned int pcm_playback_buf_pos; /* position in buffer */
  140. unsigned int pcm_capture_size;
  141. unsigned int pcm_capture_count;
  142. unsigned int pcm_capture_irq_pos; /* IRQ position */
  143. unsigned int pcm_capture_buf_pos; /* position in buffer */
  144. uint32_t evrc_min_rate;
  145. uint32_t evrc_max_rate;
  146. };
  147. static int voip_get_media_type(uint32_t mode, uint32_t rate_type,
  148. unsigned int samp_rate,
  149. unsigned int *media_type);
  150. static int voip_get_rate_type(uint32_t mode,
  151. uint32_t rate,
  152. uint32_t *rate_type);
  153. static int voip_config_vocoder(struct snd_pcm_substream *substream);
  154. static int msm_voip_mode_config_put(struct snd_kcontrol *kcontrol,
  155. struct snd_ctl_elem_value *ucontrol);
  156. static int msm_voip_mode_config_get(struct snd_kcontrol *kcontrol,
  157. struct snd_ctl_elem_value *ucontrol);
  158. static int msm_voip_rate_config_put(struct snd_kcontrol *kcontrol,
  159. struct snd_ctl_elem_value *ucontrol);
  160. static int msm_voip_evrc_min_max_rate_config_put(struct snd_kcontrol *kcontrol,
  161. struct snd_ctl_elem_value *ucontrol);
  162. static int msm_voip_evrc_min_max_rate_config_get(struct snd_kcontrol *kcontrol,
  163. struct snd_ctl_elem_value *ucontrol);
  164. static struct voip_drv_info voip_info;
  165. static struct snd_pcm_hardware msm_pcm_hardware = {
  166. .info = (SNDRV_PCM_INFO_MMAP |
  167. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  168. SNDRV_PCM_INFO_MMAP_VALID |
  169. SNDRV_PCM_INFO_INTERLEAVED),
  170. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  171. SNDRV_PCM_FMTBIT_SPECIAL,
  172. .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000,
  173. .rate_min = 8000,
  174. .rate_max = 16000,
  175. .channels_min = 1,
  176. .channels_max = 1,
  177. .buffer_bytes_max = sizeof(struct voip_buf_node) * VOIP_MIN_Q_LEN,
  178. .period_bytes_min = VOIP_MIN_VOC_PKT_SIZE,
  179. .period_bytes_max = VOIP_MAX_VOC_PKT_SIZE,
  180. .periods_min = VOIP_MIN_Q_LEN,
  181. .periods_max = VOIP_MAX_Q_LEN,
  182. .fifo_size = 0,
  183. };
  184. static int msm_voip_mute_put(struct snd_kcontrol *kcontrol,
  185. struct snd_ctl_elem_value *ucontrol)
  186. {
  187. int ret = 0;
  188. int mute = ucontrol->value.integer.value[0];
  189. int ramp_duration = ucontrol->value.integer.value[1];
  190. if ((mute < 0) || (mute > 1) || (ramp_duration < 0)) {
  191. pr_err(" %s Invalid arguments", __func__);
  192. ret = -EINVAL;
  193. goto done;
  194. }
  195. pr_debug("%s: mute=%d ramp_duration=%d\n", __func__, mute,
  196. ramp_duration);
  197. voc_set_tx_mute(voc_get_session_id(VOIP_SESSION_NAME), TX_PATH, mute,
  198. ramp_duration);
  199. done:
  200. return ret;
  201. }
  202. static int msm_voip_gain_put(struct snd_kcontrol *kcontrol,
  203. struct snd_ctl_elem_value *ucontrol)
  204. {
  205. int ret = 0;
  206. int volume = ucontrol->value.integer.value[0];
  207. int ramp_duration = ucontrol->value.integer.value[1];
  208. if ((volume < 0) || (ramp_duration < 0)) {
  209. pr_err(" %s Invalid arguments", __func__);
  210. ret = -EINVAL;
  211. goto done;
  212. }
  213. pr_debug("%s: volume: %d ramp_duration: %d\n", __func__, volume,
  214. ramp_duration);
  215. voc_set_rx_vol_step(voc_get_session_id(VOIP_SESSION_NAME),
  216. RX_PATH,
  217. volume,
  218. ramp_duration);
  219. done:
  220. return ret;
  221. }
  222. static int msm_voip_dtx_mode_put(struct snd_kcontrol *kcontrol,
  223. struct snd_ctl_elem_value *ucontrol)
  224. {
  225. mutex_lock(&voip_info.lock);
  226. voip_info.dtx_mode = ucontrol->value.integer.value[0];
  227. pr_debug("%s: dtx: %d\n", __func__, voip_info.dtx_mode);
  228. mutex_unlock(&voip_info.lock);
  229. return 0;
  230. }
  231. static int msm_voip_dtx_mode_get(struct snd_kcontrol *kcontrol,
  232. struct snd_ctl_elem_value *ucontrol)
  233. {
  234. mutex_lock(&voip_info.lock);
  235. ucontrol->value.integer.value[0] = voip_info.dtx_mode;
  236. mutex_unlock(&voip_info.lock);
  237. return 0;
  238. }
  239. static struct snd_kcontrol_new msm_voip_controls[] = {
  240. SOC_SINGLE_MULTI_EXT("Voip Tx Mute", SND_SOC_NOPM, 0,
  241. MAX_RAMP_DURATION,
  242. 0, 2, NULL, msm_voip_mute_put),
  243. SOC_SINGLE_MULTI_EXT("Voip Rx Gain", SND_SOC_NOPM, 0,
  244. MAX_RAMP_DURATION,
  245. 0, 2, NULL, msm_voip_gain_put),
  246. SOC_SINGLE_EXT("Voip Mode Config", SND_SOC_NOPM, 0, VOIP_MODE_MAX, 0,
  247. msm_voip_mode_config_get, msm_voip_mode_config_put),
  248. SOC_SINGLE_EXT("Voip Rate Config", SND_SOC_NOPM, 0, VOIP_RATE_MAX, 0,
  249. NULL, msm_voip_rate_config_put),
  250. SOC_SINGLE_MULTI_EXT("Voip Evrc Min Max Rate Config", SND_SOC_NOPM,
  251. 0, VOC_1_RATE, 0, 2, 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};
  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_debug("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__, buf_node->frame.pktlen);
  561. if (prtd->mode == MODE_PCM)
  562. prtd->pcm_playback_irq_pos += buf_node->frame.pktlen;
  563. else
  564. prtd->pcm_playback_irq_pos += prtd->pcm_count;
  565. spin_unlock_irqrestore(&prtd->dsp_lock, dsp_flags);
  566. snd_pcm_period_elapsed(prtd->playback_substream);
  567. } else {
  568. *((uint32_t *)voc_pkt) = 0;
  569. spin_unlock_irqrestore(&prtd->dsp_lock, dsp_flags);
  570. pr_debug("DL data not available\n");
  571. }
  572. wake_up(&prtd->in_wait);
  573. }
  574. static struct snd_pcm_hw_constraint_list constraints_sample_rates = {
  575. .count = ARRAY_SIZE(supported_sample_rates),
  576. .list = supported_sample_rates,
  577. .mask = 0,
  578. };
  579. static int msm_pcm_playback_prepare(struct snd_pcm_substream *substream)
  580. {
  581. struct snd_pcm_runtime *runtime = substream->runtime;
  582. struct voip_drv_info *prtd = runtime->private_data;
  583. prtd->play_samp_rate = runtime->rate;
  584. prtd->pcm_size = snd_pcm_lib_buffer_bytes(substream);
  585. prtd->pcm_count = snd_pcm_lib_period_bytes(substream);
  586. prtd->pcm_playback_irq_pos = 0;
  587. prtd->pcm_playback_buf_pos = 0;
  588. return 0;
  589. }
  590. static int msm_pcm_capture_prepare(struct snd_pcm_substream *substream)
  591. {
  592. struct snd_pcm_runtime *runtime = substream->runtime;
  593. struct voip_drv_info *prtd = runtime->private_data;
  594. int ret = 0;
  595. prtd->cap_samp_rate = runtime->rate;
  596. prtd->pcm_capture_size = snd_pcm_lib_buffer_bytes(substream);
  597. prtd->pcm_capture_count = snd_pcm_lib_period_bytes(substream);
  598. prtd->pcm_capture_irq_pos = 0;
  599. prtd->pcm_capture_buf_pos = 0;
  600. return ret;
  601. }
  602. static int msm_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  603. {
  604. int ret = 0;
  605. struct snd_pcm_runtime *runtime = substream->runtime;
  606. struct voip_drv_info *prtd = runtime->private_data;
  607. switch (cmd) {
  608. case SNDRV_PCM_TRIGGER_START:
  609. case SNDRV_PCM_TRIGGER_RESUME:
  610. pr_debug("%s: Trigger start\n", __func__);
  611. if ((!prtd->capture_start) && (!prtd->playback_start))
  612. voice_ocmem_process_req(VOICE, true);
  613. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  614. prtd->capture_start = 1;
  615. else
  616. prtd->playback_start = 1;
  617. break;
  618. case SNDRV_PCM_TRIGGER_STOP:
  619. pr_debug("SNDRV_PCM_TRIGGER_STOP\n");
  620. if (prtd->capture_start && prtd->playback_start)
  621. voice_ocmem_process_req(VOICE, false);
  622. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  623. prtd->playback_start = 0;
  624. else
  625. prtd->capture_start = 0;
  626. break;
  627. default:
  628. ret = -EINVAL;
  629. break;
  630. }
  631. return ret;
  632. }
  633. static int msm_pcm_open(struct snd_pcm_substream *substream)
  634. {
  635. struct snd_pcm_runtime *runtime = substream->runtime;
  636. struct voip_drv_info *prtd = &voip_info;
  637. int ret = 0;
  638. pr_debug("%s, VoIP\n", __func__);
  639. mutex_lock(&prtd->lock);
  640. runtime->hw = msm_pcm_hardware;
  641. ret = snd_pcm_hw_constraint_list(runtime, 0,
  642. SNDRV_PCM_HW_PARAM_RATE,
  643. &constraints_sample_rates);
  644. if (ret < 0)
  645. pr_debug("snd_pcm_hw_constraint_list failed\n");
  646. ret = snd_pcm_hw_constraint_integer(runtime,
  647. SNDRV_PCM_HW_PARAM_PERIODS);
  648. if (ret < 0) {
  649. pr_debug("snd_pcm_hw_constraint_integer failed\n");
  650. goto err;
  651. }
  652. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  653. prtd->playback_substream = substream;
  654. prtd->playback_instance++;
  655. } else {
  656. prtd->capture_substream = substream;
  657. prtd->capture_instance++;
  658. }
  659. runtime->private_data = prtd;
  660. err:
  661. mutex_unlock(&prtd->lock);
  662. return ret;
  663. }
  664. static int msm_pcm_playback_copy(struct snd_pcm_substream *substream, int a,
  665. snd_pcm_uframes_t hwoff, void __user *buf, snd_pcm_uframes_t frames)
  666. {
  667. int ret = 0;
  668. struct voip_buf_node *buf_node = NULL;
  669. struct snd_pcm_runtime *runtime = substream->runtime;
  670. struct voip_drv_info *prtd = runtime->private_data;
  671. unsigned long dsp_flags;
  672. int count = frames_to_bytes(runtime, frames);
  673. pr_debug("%s: count = %d, frames=%d\n", __func__, count, (int)frames);
  674. if (prtd->voip_reset) {
  675. pr_debug("%s: RESET event happened during VoIP\n", __func__);
  676. return -ENETRESET;
  677. }
  678. ret = wait_event_interruptible_timeout(prtd->in_wait,
  679. (!list_empty(&prtd->free_in_queue) ||
  680. prtd->state == VOIP_STOPPED),
  681. 1 * HZ);
  682. if (prtd->voip_reset) {
  683. pr_debug("%s: RESET event happened during VoIP\n", __func__);
  684. return -ENETRESET;
  685. }
  686. if (ret > 0) {
  687. if (count <= VOIP_MAX_VOC_PKT_SIZE) {
  688. spin_lock_irqsave(&prtd->dsp_lock, dsp_flags);
  689. buf_node =
  690. list_first_entry(&prtd->free_in_queue,
  691. struct voip_buf_node, list);
  692. list_del(&buf_node->list);
  693. spin_unlock_irqrestore(&prtd->dsp_lock, dsp_flags);
  694. if (prtd->mode == MODE_PCM) {
  695. ret = copy_from_user(&buf_node->frame.voc_pkt,
  696. buf, count);
  697. if (ret) {
  698. pr_err("%s: copy from user failed %d\n",
  699. __func__, ret);
  700. return -EFAULT;
  701. }
  702. buf_node->frame.pktlen = count;
  703. } else {
  704. ret = copy_from_user(&buf_node->frame,
  705. buf, count);
  706. if (ret) {
  707. pr_err("%s: copy from user failed %d\n",
  708. __func__, ret);
  709. return -EFAULT;
  710. }
  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 retuned %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("%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_instance--;
  826. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  827. prtd->capture_instance--;
  828. if (!prtd->playback_instance && !prtd->capture_instance) {
  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_dma_buf == 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_info("%s(): mode=%d, playback sample rate=%d, capture sample rate=%d\n",
  913. __func__, prtd->mode, prtd->play_samp_rate, prtd->cap_samp_rate);
  914. if ((runtime->format != FORMAT_S16_LE &&
  915. runtime->format != FORMAT_SPECIAL) &&
  916. ((prtd->mode == MODE_AMR) || (prtd->mode == MODE_AMR_WB) ||
  917. (prtd->mode == MODE_IS127) || (prtd->mode == MODE_4GV_NB) ||
  918. (prtd->mode == MODE_4GV_WB) || (prtd->mode == MODE_4GV_NW) ||
  919. (prtd->mode == MODE_G711) || (prtd->mode == MODE_G711A))) {
  920. pr_err("%s(): mode:%d and format:%u are not matched\n",
  921. __func__, prtd->mode, (uint32_t)runtime->format);
  922. ret = -EINVAL;
  923. goto done;
  924. }
  925. if (runtime->format != FORMAT_S16_LE && (prtd->mode == MODE_PCM)) {
  926. pr_err("%s(): mode:%d and format:%u are not matched\n",
  927. __func__, prtd->mode, runtime->format);
  928. ret = -EINVAL;
  929. goto done;
  930. }
  931. if ((prtd->mode == MODE_PCM) ||
  932. (prtd->mode == MODE_AMR) ||
  933. (prtd->mode == MODE_AMR_WB) ||
  934. (prtd->mode == MODE_G711) ||
  935. (prtd->mode == MODE_G711A)) {
  936. ret = voip_get_rate_type(prtd->mode,
  937. prtd->rate,
  938. &rate_type);
  939. if (ret < 0) {
  940. pr_err("%s(): fail at getting rate_type, ret=%d\n",
  941. __func__, ret);
  942. ret = -EINVAL;
  943. goto done;
  944. }
  945. prtd->rate_type = rate_type;
  946. pr_debug("rate_type=%d\n", rate_type);
  947. } else if ((prtd->mode == MODE_IS127) ||
  948. (prtd->mode == MODE_4GV_NB) ||
  949. (prtd->mode == MODE_4GV_WB) ||
  950. (prtd->mode == MODE_4GV_NW)) {
  951. ret = voip_get_rate_type(prtd->mode,
  952. prtd->evrc_min_rate,
  953. &evrc_min_rate_type);
  954. if (ret < 0) {
  955. pr_err("%s(): fail at getting min rate, ret=%d\n",
  956. __func__, ret);
  957. ret = -EINVAL;
  958. goto done;
  959. }
  960. if (evrc_min_rate_type == VOC_0_RATE)
  961. evrc_min_rate_type = VOC_8_RATE;
  962. ret = voip_get_rate_type(prtd->mode,
  963. prtd->evrc_max_rate,
  964. &evrc_max_rate_type);
  965. if (ret < 0) {
  966. pr_err("%s(): fail at getting max rate, ret=%d\n",
  967. __func__, ret);
  968. ret = -EINVAL;
  969. goto done;
  970. }
  971. if (evrc_max_rate_type == VOC_0_RATE)
  972. evrc_max_rate_type = VOC_1_RATE;
  973. if (evrc_max_rate_type < evrc_min_rate_type) {
  974. pr_err("%s(): Invalid EVRC min max rates: %d, %d\n",
  975. __func__, evrc_min_rate_type,
  976. evrc_max_rate_type);
  977. ret = -EINVAL;
  978. goto done;
  979. }
  980. pr_debug("%s(): min rate=%d, max rate=%d\n",
  981. __func__, evrc_min_rate_type, evrc_max_rate_type);
  982. }
  983. ret = voip_get_media_type(prtd->mode,
  984. prtd->rate_type,
  985. prtd->play_samp_rate,
  986. &media_type);
  987. if (ret < 0) {
  988. pr_err("%s(): fail at getting media_type, ret=%d\n",
  989. __func__, ret);
  990. ret = -EINVAL;
  991. goto done;
  992. }
  993. pr_debug("%s(): media_type=%d\n", __func__, media_type);
  994. if ((prtd->play_samp_rate == 8000) &&
  995. (prtd->cap_samp_rate == 8000))
  996. voc_config_vocoder(media_type, rate_type,
  997. VSS_NETWORK_ID_VOIP_NB,
  998. voip_info.dtx_mode,
  999. evrc_min_rate_type,
  1000. evrc_max_rate_type);
  1001. else if ((prtd->play_samp_rate == 16000) &&
  1002. (prtd->cap_samp_rate == 16000))
  1003. voc_config_vocoder(media_type, rate_type,
  1004. VSS_NETWORK_ID_VOIP_WB,
  1005. voip_info.dtx_mode,
  1006. evrc_min_rate_type,
  1007. evrc_max_rate_type);
  1008. else {
  1009. if((prtd->play_samp_rate != 0) &&
  1010. (prtd->cap_samp_rate == 0) )
  1011. pr_info("%s: TX setting is not complete ", __func__);
  1012. else
  1013. pr_err("%s: Invalid rate playback %d, capture %d\n",
  1014. __func__, prtd->play_samp_rate,
  1015. prtd->cap_samp_rate);
  1016. ret = -EINVAL;
  1017. }
  1018. done:
  1019. return ret;
  1020. }
  1021. static int msm_pcm_prepare(struct snd_pcm_substream *substream)
  1022. {
  1023. int ret = 0;
  1024. struct snd_pcm_runtime *runtime = substream->runtime;
  1025. struct voip_drv_info *prtd = runtime->private_data;
  1026. mutex_lock(&prtd->lock);
  1027. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  1028. ret = msm_pcm_playback_prepare(substream);
  1029. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  1030. ret = msm_pcm_capture_prepare(substream);
  1031. if (prtd->playback_instance && prtd->capture_instance
  1032. && (prtd->state != VOIP_STARTED)) {
  1033. ret = voip_config_vocoder(substream);
  1034. if (ret < 0) {
  1035. pr_err("%s(): fail at configuring vocoder for voip, ret=%d\n",
  1036. __func__, ret);
  1037. goto done;
  1038. }
  1039. /* Initialaizing cb variables */
  1040. voc_register_mvs_cb(voip_process_ul_pkt,
  1041. voip_process_dl_pkt,
  1042. voip_ssr_cb_fn, prtd);
  1043. ret = voc_start_voice_call(
  1044. voc_get_session_id(VOIP_SESSION_NAME));
  1045. if (ret < 0) {
  1046. pr_err("%s: voc_start_voice_call() failed err %d",
  1047. __func__, ret);
  1048. goto done;
  1049. }
  1050. prtd->state = VOIP_STARTED;
  1051. }
  1052. done:
  1053. mutex_unlock(&prtd->lock);
  1054. return ret;
  1055. }
  1056. static snd_pcm_uframes_t
  1057. msm_pcm_playback_pointer(struct snd_pcm_substream *substream)
  1058. {
  1059. struct snd_pcm_runtime *runtime = substream->runtime;
  1060. struct voip_drv_info *prtd = runtime->private_data;
  1061. pr_debug("%s\n", __func__);
  1062. if (prtd->pcm_playback_irq_pos >= prtd->pcm_size)
  1063. prtd->pcm_playback_irq_pos = 0;
  1064. return bytes_to_frames(runtime, (prtd->pcm_playback_irq_pos));
  1065. }
  1066. static snd_pcm_uframes_t
  1067. msm_pcm_capture_pointer(struct snd_pcm_substream *substream)
  1068. {
  1069. struct snd_pcm_runtime *runtime = substream->runtime;
  1070. struct voip_drv_info *prtd = runtime->private_data;
  1071. if (prtd->pcm_capture_irq_pos >= prtd->pcm_capture_size)
  1072. prtd->pcm_capture_irq_pos = 0;
  1073. return bytes_to_frames(runtime, (prtd->pcm_capture_irq_pos));
  1074. }
  1075. static snd_pcm_uframes_t msm_pcm_pointer(struct snd_pcm_substream *substream)
  1076. {
  1077. snd_pcm_uframes_t ret = 0;
  1078. pr_debug("%s\n", __func__);
  1079. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  1080. ret = msm_pcm_playback_pointer(substream);
  1081. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  1082. ret = msm_pcm_capture_pointer(substream);
  1083. return ret;
  1084. }
  1085. static int msm_pcm_mmap(struct snd_pcm_substream *substream,
  1086. struct vm_area_struct *vma)
  1087. {
  1088. struct snd_pcm_runtime *runtime = substream->runtime;
  1089. pr_debug("%s\n", __func__);
  1090. dma_mmap_coherent(substream->pcm->card->dev, vma,
  1091. runtime->dma_area,
  1092. runtime->dma_addr,
  1093. runtime->dma_bytes);
  1094. return 0;
  1095. }
  1096. static int msm_pcm_hw_params(struct snd_pcm_substream *substream,
  1097. struct snd_pcm_hw_params *params)
  1098. {
  1099. struct snd_pcm_runtime *runtime = substream->runtime;
  1100. struct snd_dma_buffer *dma_buf = &substream->dma_buffer;
  1101. struct voip_buf_node *buf_node = NULL;
  1102. int i = 0, offset = 0;
  1103. int periods = VOIP_MIN_Q_LEN;
  1104. pr_debug("%s: voip\n", __func__);
  1105. mutex_lock(&voip_info.lock);
  1106. periods = params_periods(params);
  1107. pr_info("%s: periods = %d\n", __func__, periods);
  1108. runtime->hw.buffer_bytes_max = sizeof(struct voip_buf_node) * periods;
  1109. dma_buf->dev.type = SNDRV_DMA_TYPE_DEV;
  1110. dma_buf->dev.dev = substream->pcm->card->dev;
  1111. dma_buf->private_data = NULL;
  1112. dma_buf->area = dma_alloc_coherent(substream->pcm->card->dev,
  1113. runtime->hw.buffer_bytes_max,
  1114. &dma_buf->addr, GFP_KERNEL);
  1115. if (!dma_buf->area) {
  1116. pr_err("%s:MSM VOIP dma_alloc failed\n", __func__);
  1117. mutex_unlock(&voip_info.lock);
  1118. return -ENOMEM;
  1119. }
  1120. dma_buf->bytes = runtime->hw.buffer_bytes_max;
  1121. memset(dma_buf->area, 0, runtime->hw.buffer_bytes_max);
  1122. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  1123. for (i = 0; i < periods; i++) {
  1124. buf_node = (void *)dma_buf->area + offset;
  1125. list_add_tail(&buf_node->list,
  1126. &voip_info.free_in_queue);
  1127. offset = offset + sizeof(struct voip_buf_node);
  1128. }
  1129. } else {
  1130. for (i = 0; i < periods; i++) {
  1131. buf_node = (void *) dma_buf->area + offset;
  1132. list_add_tail(&buf_node->list,
  1133. &voip_info.free_out_queue);
  1134. offset = offset + sizeof(struct voip_buf_node);
  1135. }
  1136. }
  1137. mutex_unlock(&voip_info.lock);
  1138. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  1139. return 0;
  1140. }
  1141. static int msm_voip_mode_config_get(struct snd_kcontrol *kcontrol,
  1142. struct snd_ctl_elem_value *ucontrol)
  1143. {
  1144. mutex_lock(&voip_info.lock);
  1145. ucontrol->value.integer.value[0] = voip_info.mode;
  1146. mutex_unlock(&voip_info.lock);
  1147. return 0;
  1148. }
  1149. static int msm_voip_mode_config_put(struct snd_kcontrol *kcontrol,
  1150. struct snd_ctl_elem_value *ucontrol)
  1151. {
  1152. mutex_lock(&voip_info.lock);
  1153. voip_info.mode = ucontrol->value.integer.value[0];
  1154. pr_debug("%s: mode=%d\n", __func__, voip_info.mode);
  1155. mutex_unlock(&voip_info.lock);
  1156. return 0;
  1157. }
  1158. static int msm_voip_rate_config_put(struct snd_kcontrol *kcontrol,
  1159. struct snd_ctl_elem_value *ucontrol)
  1160. {
  1161. int ret = 0;
  1162. int rate = ucontrol->value.integer.value[0];
  1163. mutex_lock(&voip_info.lock);
  1164. if (voip_info.rate != rate) {
  1165. voip_info.rate = rate;
  1166. pr_debug("%s: rate=%d\n", __func__, voip_info.rate);
  1167. if (voip_info.state == VOIP_STARTED &&
  1168. (voip_info.mode == MODE_AMR ||
  1169. voip_info.mode == MODE_AMR_WB)) {
  1170. ret = voip_config_vocoder(
  1171. voip_info.capture_substream);
  1172. if (ret) {
  1173. pr_err("%s:Failed to configure vocoder, ret=%d\n",
  1174. __func__, ret);
  1175. goto done;
  1176. }
  1177. ret = voc_update_amr_vocoder_rate(
  1178. voc_get_session_id(VOIP_SESSION_NAME));
  1179. if (ret) {
  1180. pr_err("%s:Failed to update AMR rate, ret=%d\n",
  1181. __func__, ret);
  1182. }
  1183. }
  1184. }
  1185. done:
  1186. mutex_unlock(&voip_info.lock);
  1187. return ret;
  1188. }
  1189. static int msm_voip_evrc_min_max_rate_config_get(struct snd_kcontrol *kcontrol,
  1190. struct snd_ctl_elem_value *ucontrol)
  1191. {
  1192. mutex_lock(&voip_info.lock);
  1193. ucontrol->value.integer.value[0] = voip_info.evrc_min_rate;
  1194. ucontrol->value.integer.value[1] = voip_info.evrc_max_rate;
  1195. mutex_unlock(&voip_info.lock);
  1196. return 0;
  1197. }
  1198. static int msm_voip_evrc_min_max_rate_config_put(struct snd_kcontrol *kcontrol,
  1199. struct snd_ctl_elem_value *ucontrol)
  1200. {
  1201. mutex_lock(&voip_info.lock);
  1202. voip_info.evrc_min_rate = ucontrol->value.integer.value[0];
  1203. voip_info.evrc_max_rate = ucontrol->value.integer.value[1];
  1204. pr_debug("%s(): evrc_min_rate=%d,evrc_max_rate=%d\n", __func__,
  1205. voip_info.evrc_min_rate, voip_info.evrc_max_rate);
  1206. mutex_unlock(&voip_info.lock);
  1207. return 0;
  1208. }
  1209. static int voip_get_rate_type(uint32_t mode, uint32_t rate,
  1210. uint32_t *rate_type)
  1211. {
  1212. int ret = 0;
  1213. switch (mode) {
  1214. case MODE_AMR: {
  1215. switch (rate) {
  1216. case 4750:
  1217. *rate_type = AMR_RATE_4750;
  1218. break;
  1219. case 5150:
  1220. *rate_type = AMR_RATE_5150;
  1221. break;
  1222. case 5900:
  1223. *rate_type = AMR_RATE_5900;
  1224. break;
  1225. case 6700:
  1226. *rate_type = AMR_RATE_6700;
  1227. break;
  1228. case 7400:
  1229. *rate_type = AMR_RATE_7400;
  1230. break;
  1231. case 7950:
  1232. *rate_type = AMR_RATE_7950;
  1233. break;
  1234. case 10200:
  1235. *rate_type = AMR_RATE_10200;
  1236. break;
  1237. case 12200:
  1238. *rate_type = AMR_RATE_12200;
  1239. break;
  1240. default:
  1241. pr_err("wrong rate for AMR NB.\n");
  1242. ret = -EINVAL;
  1243. break;
  1244. }
  1245. break;
  1246. }
  1247. case MODE_AMR_WB: {
  1248. switch (rate) {
  1249. case 6600:
  1250. *rate_type = AMR_RATE_6600 - AMR_RATE_6600;
  1251. break;
  1252. case 8850:
  1253. *rate_type = AMR_RATE_8850 - AMR_RATE_6600;
  1254. break;
  1255. case 12650:
  1256. *rate_type = AMR_RATE_12650 - AMR_RATE_6600;
  1257. break;
  1258. case 14250:
  1259. *rate_type = AMR_RATE_14250 - AMR_RATE_6600;
  1260. break;
  1261. case 15850:
  1262. *rate_type = AMR_RATE_15850 - AMR_RATE_6600;
  1263. break;
  1264. case 18250:
  1265. *rate_type = AMR_RATE_18250 - AMR_RATE_6600;
  1266. break;
  1267. case 19850:
  1268. *rate_type = AMR_RATE_19850 - AMR_RATE_6600;
  1269. break;
  1270. case 23050:
  1271. *rate_type = AMR_RATE_23050 - AMR_RATE_6600;
  1272. break;
  1273. case 23850:
  1274. *rate_type = AMR_RATE_23850 - AMR_RATE_6600;
  1275. break;
  1276. default:
  1277. pr_err("wrong rate for AMR_WB.\n");
  1278. ret = -EINVAL;
  1279. break;
  1280. }
  1281. break;
  1282. }
  1283. case MODE_PCM: {
  1284. *rate_type = 0;
  1285. break;
  1286. }
  1287. case MODE_IS127:
  1288. case MODE_4GV_NB:
  1289. case MODE_4GV_WB: {
  1290. switch (rate) {
  1291. case VOC_0_RATE:
  1292. case VOC_8_RATE:
  1293. case VOC_4_RATE:
  1294. case VOC_2_RATE:
  1295. case VOC_1_RATE:
  1296. *rate_type = rate;
  1297. break;
  1298. default:
  1299. pr_err("wrong rate for IS127/4GV_NB/WB.\n");
  1300. ret = -EINVAL;
  1301. break;
  1302. }
  1303. break;
  1304. }
  1305. case MODE_4GV_NW: {
  1306. switch (rate) {
  1307. case VOC_0_RATE:
  1308. case VOC_8_RATE:
  1309. case VOC_4_RATE:
  1310. case VOC_2_RATE:
  1311. case VOC_1_RATE:
  1312. case VOC_8_RATE_NC:
  1313. *rate_type = rate;
  1314. break;
  1315. default:
  1316. pr_err("wrong rate for 4GV_NW.\n");
  1317. ret = -EINVAL;
  1318. break;
  1319. }
  1320. break;
  1321. }
  1322. case MODE_G711:
  1323. case MODE_G711A:
  1324. *rate_type = rate;
  1325. break;
  1326. default:
  1327. pr_err("wrong mode type.\n");
  1328. ret = -EINVAL;
  1329. }
  1330. pr_debug("%s, mode=%d, rate=%u, rate_type=%d\n",
  1331. __func__, mode, rate, *rate_type);
  1332. return ret;
  1333. }
  1334. static int voip_get_media_type(uint32_t mode, uint32_t rate_type,
  1335. unsigned int samp_rate,
  1336. unsigned int *media_type)
  1337. {
  1338. int ret = 0;
  1339. pr_debug("%s: mode=%d, samp_rate=%d\n", __func__,
  1340. mode, samp_rate);
  1341. switch (mode) {
  1342. case MODE_AMR:
  1343. *media_type = VSS_MEDIA_ID_AMR_NB_MODEM;
  1344. break;
  1345. case MODE_AMR_WB:
  1346. *media_type = VSS_MEDIA_ID_AMR_WB_MODEM;
  1347. break;
  1348. case MODE_PCM:
  1349. if (samp_rate == 8000)
  1350. *media_type = VSS_MEDIA_ID_PCM_NB;
  1351. else
  1352. *media_type = VSS_MEDIA_ID_PCM_WB;
  1353. break;
  1354. case MODE_IS127: /* EVRC-A */
  1355. *media_type = VSS_MEDIA_ID_EVRC_MODEM;
  1356. break;
  1357. case MODE_4GV_NB: /* EVRC-B */
  1358. *media_type = VSS_MEDIA_ID_4GV_NB_MODEM;
  1359. break;
  1360. case MODE_4GV_WB: /* EVRC-WB */
  1361. *media_type = VSS_MEDIA_ID_4GV_WB_MODEM;
  1362. break;
  1363. case MODE_4GV_NW: /* EVRC-NW */
  1364. *media_type = VSS_MEDIA_ID_4GV_NW_MODEM;
  1365. break;
  1366. case MODE_G711:
  1367. case MODE_G711A:
  1368. if (rate_type == MVS_G711A_MODE_MULAW)
  1369. *media_type = VSS_MEDIA_ID_G711_MULAW;
  1370. else
  1371. *media_type = VSS_MEDIA_ID_G711_ALAW;
  1372. break;
  1373. default:
  1374. pr_debug(" input mode is not supported\n");
  1375. ret = -EINVAL;
  1376. }
  1377. pr_debug("%s: media_type is 0x%x\n", __func__, *media_type);
  1378. return ret;
  1379. }
  1380. static struct snd_pcm_ops msm_pcm_ops = {
  1381. .open = msm_pcm_open,
  1382. .copy = msm_pcm_copy,
  1383. .hw_params = msm_pcm_hw_params,
  1384. .close = msm_pcm_close,
  1385. .prepare = msm_pcm_prepare,
  1386. .trigger = msm_pcm_trigger,
  1387. .pointer = msm_pcm_pointer,
  1388. .mmap = msm_pcm_mmap,
  1389. };
  1390. static int msm_asoc_pcm_new(struct snd_soc_pcm_runtime *rtd)
  1391. {
  1392. struct snd_card *card = rtd->card->snd_card;
  1393. int ret = 0;
  1394. pr_debug("msm_asoc_pcm_new\n");
  1395. if (!card->dev->coherent_dma_mask)
  1396. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  1397. return ret;
  1398. }
  1399. static struct snd_soc_platform_driver msm_soc_platform = {
  1400. .ops = &msm_pcm_ops,
  1401. .pcm_new = msm_asoc_pcm_new,
  1402. .probe = msm_pcm_voip_probe,
  1403. };
  1404. static __devinit int msm_pcm_probe(struct platform_device *pdev)
  1405. {
  1406. int rc;
  1407. if (!is_voc_initialized()) {
  1408. pr_debug("%s: voice module not initialized yet, deferring probe()\n",
  1409. __func__);
  1410. rc = -EPROBE_DEFER;
  1411. goto done;
  1412. }
  1413. rc = voc_alloc_cal_shared_memory();
  1414. if (rc == -EPROBE_DEFER) {
  1415. pr_debug("%s: memory allocation for calibration deferred %d\n",
  1416. __func__, rc);
  1417. goto done;
  1418. } else if (rc < 0) {
  1419. pr_err("%s: memory allocation for calibration failed %d\n",
  1420. __func__, rc);
  1421. }
  1422. rc = voc_alloc_voip_shared_memory();
  1423. if (rc < 0) {
  1424. pr_err("%s: error allocating shared mem err %d\n",
  1425. __func__, rc);
  1426. }
  1427. if (pdev->dev.of_node)
  1428. dev_set_name(&pdev->dev, "%s", "msm-voip-dsp");
  1429. pr_debug("%s: dev name %s\n", __func__, dev_name(&pdev->dev));
  1430. rc = snd_soc_register_platform(&pdev->dev,
  1431. &msm_soc_platform);
  1432. done:
  1433. return rc;
  1434. }
  1435. static int msm_pcm_remove(struct platform_device *pdev)
  1436. {
  1437. snd_soc_unregister_platform(&pdev->dev);
  1438. return 0;
  1439. }
  1440. static const struct of_device_id msm_voip_dt_match[] = {
  1441. {.compatible = "qcom,msm-voip-dsp"},
  1442. {}
  1443. };
  1444. MODULE_DEVICE_TABLE(of, msm_voip_dt_match);
  1445. static struct platform_driver msm_pcm_driver = {
  1446. .driver = {
  1447. .name = "msm-voip-dsp",
  1448. .owner = THIS_MODULE,
  1449. .of_match_table = msm_voip_dt_match,
  1450. },
  1451. .probe = msm_pcm_probe,
  1452. .remove = __devexit_p(msm_pcm_remove),
  1453. };
  1454. static int __init msm_soc_platform_init(void)
  1455. {
  1456. memset(&voip_info, 0, sizeof(voip_info));
  1457. voip_info.mode = MODE_PCM;
  1458. mutex_init(&voip_info.lock);
  1459. spin_lock_init(&voip_info.dsp_lock);
  1460. spin_lock_init(&voip_info.dsp_ul_lock);
  1461. init_waitqueue_head(&voip_info.out_wait);
  1462. init_waitqueue_head(&voip_info.in_wait);
  1463. INIT_LIST_HEAD(&voip_info.in_queue);
  1464. INIT_LIST_HEAD(&voip_info.free_in_queue);
  1465. INIT_LIST_HEAD(&voip_info.out_queue);
  1466. INIT_LIST_HEAD(&voip_info.free_out_queue);
  1467. return platform_driver_register(&msm_pcm_driver);
  1468. }
  1469. module_init(msm_soc_platform_init);
  1470. static void __exit msm_soc_platform_exit(void)
  1471. {
  1472. platform_driver_unregister(&msm_pcm_driver);
  1473. }
  1474. module_exit(msm_soc_platform_exit);
  1475. MODULE_DESCRIPTION("PCM module platform driver");
  1476. MODULE_LICENSE("GPL v2");