msm-pcm-voip.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346
  1. /* Copyright (c) 2011-2013, 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 <sound/core.h>
  21. #include <sound/soc.h>
  22. #include <sound/soc-dapm.h>
  23. #include <sound/pcm.h>
  24. #include <sound/initval.h>
  25. #include <sound/control.h>
  26. #include <asm/dma.h>
  27. #include "msm-pcm-q6.h"
  28. #include "msm-pcm-routing.h"
  29. #include "qdsp6/q6voice.h"
  30. #define VOIP_MAX_Q_LEN 10
  31. #define VOIP_MAX_VOC_PKT_SIZE 640
  32. #define VOIP_MIN_VOC_PKT_SIZE 320
  33. /* Length of the DSP frame info header added to the voc packet. */
  34. #define DSP_FRAME_HDR_LEN 1
  35. #define MODE_IS127 0x2
  36. #define MODE_4GV_NB 0x3
  37. #define MODE_4GV_WB 0x4
  38. #define MODE_AMR 0x5
  39. #define MODE_AMR_WB 0xD
  40. #define MODE_PCM 0xC
  41. #define MODE_G711 0xA
  42. #define MODE_G711A 0xF
  43. enum msm_audio_g711a_frame_type {
  44. MVS_G711A_SPEECH_GOOD,
  45. MVS_G711A_SID,
  46. MVS_G711A_NO_DATA,
  47. MVS_G711A_ERASURE
  48. };
  49. enum msm_audio_g711a_mode {
  50. MVS_G711A_MODE_MULAW,
  51. MVS_G711A_MODE_ALAW
  52. };
  53. enum msm_audio_g711_mode {
  54. MVS_G711_MODE_MULAW,
  55. MVS_G711_MODE_ALAW
  56. };
  57. enum format {
  58. FORMAT_S16_LE = 2,
  59. FORMAT_SPECIAL = 31,
  60. };
  61. enum amr_rate_type {
  62. AMR_RATE_4750, /* AMR 4.75 kbps */
  63. AMR_RATE_5150, /* AMR 5.15 kbps */
  64. AMR_RATE_5900, /* AMR 5.90 kbps */
  65. AMR_RATE_6700, /* AMR 6.70 kbps */
  66. AMR_RATE_7400, /* AMR 7.40 kbps */
  67. AMR_RATE_7950, /* AMR 7.95 kbps */
  68. AMR_RATE_10200, /* AMR 10.20 kbps */
  69. AMR_RATE_12200, /* AMR 12.20 kbps */
  70. AMR_RATE_6600, /* AMR-WB 6.60 kbps */
  71. AMR_RATE_8850, /* AMR-WB 8.85 kbps */
  72. AMR_RATE_12650, /* AMR-WB 12.65 kbps */
  73. AMR_RATE_14250, /* AMR-WB 14.25 kbps */
  74. AMR_RATE_15850, /* AMR-WB 15.85 kbps */
  75. AMR_RATE_18250, /* AMR-WB 18.25 kbps */
  76. AMR_RATE_19850, /* AMR-WB 19.85 kbps */
  77. AMR_RATE_23050, /* AMR-WB 23.05 kbps */
  78. AMR_RATE_23850, /* AMR-WB 23.85 kbps */
  79. AMR_RATE_UNDEF
  80. };
  81. enum voip_state {
  82. VOIP_STOPPED,
  83. VOIP_STARTED,
  84. };
  85. struct voip_frame {
  86. union {
  87. uint32_t frame_type;
  88. uint32_t packet_rate;
  89. } header;
  90. uint32_t len;
  91. uint8_t voc_pkt[VOIP_MAX_VOC_PKT_SIZE];
  92. };
  93. struct voip_buf_node {
  94. struct list_head list;
  95. struct voip_frame frame;
  96. };
  97. struct voip_drv_info {
  98. enum voip_state state;
  99. struct snd_pcm_substream *playback_substream;
  100. struct snd_pcm_substream *capture_substream;
  101. struct list_head in_queue;
  102. struct list_head free_in_queue;
  103. struct list_head out_queue;
  104. struct list_head free_out_queue;
  105. wait_queue_head_t out_wait;
  106. wait_queue_head_t in_wait;
  107. struct mutex lock;
  108. spinlock_t dsp_lock;
  109. spinlock_t dsp_ul_lock;
  110. uint32_t mode;
  111. uint32_t rate_type;
  112. uint32_t rate;
  113. uint32_t dtx_mode;
  114. uint8_t capture_start;
  115. uint8_t playback_start;
  116. uint8_t playback_instance;
  117. uint8_t capture_instance;
  118. unsigned int play_samp_rate;
  119. unsigned int cap_samp_rate;
  120. unsigned int pcm_size;
  121. unsigned int pcm_count;
  122. unsigned int pcm_playback_irq_pos; /* IRQ position */
  123. unsigned int pcm_playback_buf_pos; /* position in buffer */
  124. unsigned int pcm_capture_size;
  125. unsigned int pcm_capture_count;
  126. unsigned int pcm_capture_irq_pos; /* IRQ position */
  127. unsigned int pcm_capture_buf_pos; /* position in buffer */
  128. };
  129. static int voip_get_media_type(uint32_t mode, uint32_t rate_type,
  130. unsigned int samp_rate,
  131. uint32_t *media_type);
  132. static int voip_get_rate_type(uint32_t mode,
  133. uint32_t rate,
  134. uint32_t *rate_type);
  135. static int msm_voip_mode_rate_config_put(struct snd_kcontrol *kcontrol,
  136. struct snd_ctl_elem_value *ucontrol);
  137. static int msm_voip_mode_rate_config_get(struct snd_kcontrol *kcontrol,
  138. struct snd_ctl_elem_value *ucontrol);
  139. static struct voip_drv_info voip_info;
  140. static struct snd_pcm_hardware msm_pcm_hardware = {
  141. .info = (SNDRV_PCM_INFO_MMAP |
  142. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  143. SNDRV_PCM_INFO_MMAP_VALID |
  144. SNDRV_PCM_INFO_INTERLEAVED),
  145. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  146. SNDRV_PCM_FMTBIT_SPECIAL,
  147. .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000,
  148. .rate_min = 8000,
  149. .rate_max = 16000,
  150. .channels_min = 1,
  151. .channels_max = 1,
  152. .buffer_bytes_max = sizeof(struct voip_buf_node) * VOIP_MAX_Q_LEN,
  153. .period_bytes_min = VOIP_MIN_VOC_PKT_SIZE,
  154. .period_bytes_max = VOIP_MAX_VOC_PKT_SIZE,
  155. .periods_min = VOIP_MAX_Q_LEN,
  156. .periods_max = VOIP_MAX_Q_LEN,
  157. .fifo_size = 0,
  158. };
  159. static int msm_voip_mute_put(struct snd_kcontrol *kcontrol,
  160. struct snd_ctl_elem_value *ucontrol)
  161. {
  162. int mute = ucontrol->value.integer.value[0];
  163. pr_debug("%s: mute=%d\n", __func__, mute);
  164. voc_set_tx_mute(voc_get_session_id(VOIP_SESSION_NAME), TX_PATH, mute);
  165. return 0;
  166. }
  167. static int msm_voip_mute_get(struct snd_kcontrol *kcontrol,
  168. struct snd_ctl_elem_value *ucontrol)
  169. {
  170. ucontrol->value.integer.value[0] = 0;
  171. return 0;
  172. }
  173. static int msm_voip_volume_put(struct snd_kcontrol *kcontrol,
  174. struct snd_ctl_elem_value *ucontrol)
  175. {
  176. int volume = ucontrol->value.integer.value[0];
  177. pr_debug("%s: volume: %d\n", __func__, volume);
  178. voc_set_rx_vol_index(voc_get_session_id(VOIP_SESSION_NAME),
  179. RX_PATH,
  180. volume);
  181. return 0;
  182. }
  183. static int msm_voip_volume_get(struct snd_kcontrol *kcontrol,
  184. struct snd_ctl_elem_value *ucontrol)
  185. {
  186. ucontrol->value.integer.value[0] = 0;
  187. return 0;
  188. }
  189. static int msm_voip_dtx_mode_put(struct snd_kcontrol *kcontrol,
  190. struct snd_ctl_elem_value *ucontrol)
  191. {
  192. mutex_lock(&voip_info.lock);
  193. voip_info.dtx_mode = ucontrol->value.integer.value[0];
  194. pr_debug("%s: dtx: %d\n", __func__, voip_info.dtx_mode);
  195. mutex_unlock(&voip_info.lock);
  196. return 0;
  197. }
  198. static int msm_voip_dtx_mode_get(struct snd_kcontrol *kcontrol,
  199. struct snd_ctl_elem_value *ucontrol)
  200. {
  201. mutex_lock(&voip_info.lock);
  202. ucontrol->value.integer.value[0] = voip_info.dtx_mode;
  203. mutex_unlock(&voip_info.lock);
  204. return 0;
  205. }
  206. static struct snd_kcontrol_new msm_voip_controls[] = {
  207. SOC_SINGLE_EXT("Voip Tx Mute", SND_SOC_NOPM, 0, 1, 0,
  208. msm_voip_mute_get, msm_voip_mute_put),
  209. SOC_SINGLE_EXT("Voip Rx Volume", SND_SOC_NOPM, 0, 5, 0,
  210. msm_voip_volume_get, msm_voip_volume_put),
  211. SOC_SINGLE_MULTI_EXT("Voip Mode Rate Config", SND_SOC_NOPM, 0, 23850,
  212. 0, 2, msm_voip_mode_rate_config_get,
  213. msm_voip_mode_rate_config_put),
  214. SOC_SINGLE_EXT("Voip Dtx Mode", SND_SOC_NOPM, 0, 1, 0,
  215. msm_voip_dtx_mode_get, msm_voip_dtx_mode_put),
  216. };
  217. static int msm_pcm_voip_probe(struct snd_soc_platform *platform)
  218. {
  219. snd_soc_add_platform_controls(platform, msm_voip_controls,
  220. ARRAY_SIZE(msm_voip_controls));
  221. return 0;
  222. }
  223. /* sample rate supported */
  224. static unsigned int supported_sample_rates[] = {8000, 16000};
  225. /* capture path */
  226. static void voip_process_ul_pkt(uint8_t *voc_pkt,
  227. uint32_t pkt_len,
  228. void *private_data)
  229. {
  230. struct voip_buf_node *buf_node = NULL;
  231. struct voip_drv_info *prtd = private_data;
  232. unsigned long dsp_flags;
  233. if (prtd->capture_substream == NULL)
  234. return;
  235. /* Copy up-link packet into out_queue. */
  236. spin_lock_irqsave(&prtd->dsp_ul_lock, dsp_flags);
  237. /* discarding UL packets till start is received */
  238. if (!list_empty(&prtd->free_out_queue) && prtd->capture_start) {
  239. buf_node = list_first_entry(&prtd->free_out_queue,
  240. struct voip_buf_node, list);
  241. list_del(&buf_node->list);
  242. switch (prtd->mode) {
  243. case MODE_AMR_WB:
  244. case MODE_AMR: {
  245. /* Remove the DSP frame info header. Header format:
  246. * Bits 0-3: Frame rate
  247. * Bits 4-7: Frame type
  248. */
  249. buf_node->frame.header.frame_type =
  250. ((*voc_pkt) & 0xF0) >> 4;
  251. voc_pkt = voc_pkt + DSP_FRAME_HDR_LEN;
  252. buf_node->frame.len = pkt_len - DSP_FRAME_HDR_LEN;
  253. memcpy(&buf_node->frame.voc_pkt[0],
  254. voc_pkt,
  255. buf_node->frame.len);
  256. list_add_tail(&buf_node->list, &prtd->out_queue);
  257. break;
  258. }
  259. case MODE_IS127:
  260. case MODE_4GV_NB:
  261. case MODE_4GV_WB: {
  262. /* Remove the DSP frame info header.
  263. * Header format:
  264. * Bits 0-3: frame rate
  265. */
  266. buf_node->frame.header.packet_rate = (*voc_pkt) & 0x0F;
  267. voc_pkt = voc_pkt + DSP_FRAME_HDR_LEN;
  268. buf_node->frame.len = pkt_len - DSP_FRAME_HDR_LEN;
  269. memcpy(&buf_node->frame.voc_pkt[0],
  270. voc_pkt,
  271. buf_node->frame.len);
  272. list_add_tail(&buf_node->list, &prtd->out_queue);
  273. break;
  274. }
  275. case MODE_G711:
  276. case MODE_G711A:{
  277. /* G711 frames are 10ms each, but the DSP works with
  278. * 20ms frames and sends two 10ms frames per buffer.
  279. * Extract the two frames and put them in separate
  280. * buffers.
  281. */
  282. /* Remove the first DSP frame info header.
  283. * Header format: G711A
  284. * Bits 0-1: Frame type
  285. * Bits 2-3: Frame rate
  286. *
  287. * Header format: G711
  288. * Bits 2-3: Frame rate
  289. */
  290. if (prtd->mode == MODE_G711A)
  291. buf_node->frame.header.frame_type =
  292. (*voc_pkt) & 0x03;
  293. voc_pkt = voc_pkt + DSP_FRAME_HDR_LEN;
  294. /* There are two frames in the buffer. Length of the
  295. * first frame:
  296. */
  297. buf_node->frame.len = (pkt_len -
  298. 2 * DSP_FRAME_HDR_LEN) / 2;
  299. memcpy(&buf_node->frame.voc_pkt[0],
  300. voc_pkt,
  301. buf_node->frame.len);
  302. voc_pkt = voc_pkt + buf_node->frame.len;
  303. list_add_tail(&buf_node->list, &prtd->out_queue);
  304. /* Get another buffer from the free Q and fill in the
  305. * second frame.
  306. */
  307. if (!list_empty(&prtd->free_out_queue)) {
  308. buf_node =
  309. list_first_entry(&prtd->free_out_queue,
  310. struct voip_buf_node,
  311. list);
  312. list_del(&buf_node->list);
  313. /* Remove the second DSP frame info header.
  314. * Header format:
  315. * Bits 0-1: Frame type
  316. * Bits 2-3: Frame rate
  317. */
  318. if (prtd->mode == MODE_G711A)
  319. buf_node->frame.header.frame_type =
  320. (*voc_pkt) & 0x03;
  321. voc_pkt = voc_pkt + DSP_FRAME_HDR_LEN;
  322. /* There are two frames in the buffer. Length
  323. * of the second frame:
  324. */
  325. buf_node->frame.len = (pkt_len -
  326. 2 * DSP_FRAME_HDR_LEN) / 2;
  327. memcpy(&buf_node->frame.voc_pkt[0],
  328. voc_pkt,
  329. buf_node->frame.len);
  330. list_add_tail(&buf_node->list,
  331. &prtd->out_queue);
  332. } else {
  333. /* Drop the second frame */
  334. pr_err("%s: UL data dropped, read is slow\n",
  335. __func__);
  336. }
  337. break;
  338. }
  339. default: {
  340. buf_node->frame.len = pkt_len;
  341. memcpy(&buf_node->frame.voc_pkt[0],
  342. voc_pkt,
  343. buf_node->frame.len);
  344. list_add_tail(&buf_node->list, &prtd->out_queue);
  345. }
  346. }
  347. pr_debug("ul_pkt: pkt_len =%d, frame.len=%d\n", pkt_len,
  348. buf_node->frame.len);
  349. prtd->pcm_capture_irq_pos += prtd->pcm_capture_count;
  350. spin_unlock_irqrestore(&prtd->dsp_ul_lock, dsp_flags);
  351. snd_pcm_period_elapsed(prtd->capture_substream);
  352. } else {
  353. spin_unlock_irqrestore(&prtd->dsp_ul_lock, dsp_flags);
  354. pr_err("UL data dropped\n");
  355. }
  356. wake_up(&prtd->out_wait);
  357. }
  358. /* playback path */
  359. static void voip_process_dl_pkt(uint8_t *voc_pkt,
  360. uint32_t *pkt_len,
  361. void *private_data)
  362. {
  363. struct voip_buf_node *buf_node = NULL;
  364. struct voip_drv_info *prtd = private_data;
  365. unsigned long dsp_flags;
  366. if (prtd->playback_substream == NULL)
  367. return;
  368. spin_lock_irqsave(&prtd->dsp_lock, dsp_flags);
  369. if (!list_empty(&prtd->in_queue) && prtd->playback_start) {
  370. buf_node = list_first_entry(&prtd->in_queue,
  371. struct voip_buf_node, list);
  372. list_del(&buf_node->list);
  373. switch (prtd->mode) {
  374. case MODE_AMR:
  375. case MODE_AMR_WB: {
  376. /* Add the DSP frame info header. Header format:
  377. * Bits 0-3: Frame rate
  378. * Bits 4-7: Frame type
  379. */
  380. *voc_pkt = ((buf_node->frame.header.frame_type &
  381. 0x0F) << 4) | (prtd->rate_type & 0x0F);
  382. voc_pkt = voc_pkt + DSP_FRAME_HDR_LEN;
  383. *pkt_len = buf_node->frame.len + DSP_FRAME_HDR_LEN;
  384. memcpy(voc_pkt,
  385. &buf_node->frame.voc_pkt[0],
  386. buf_node->frame.len);
  387. list_add_tail(&buf_node->list, &prtd->free_in_queue);
  388. break;
  389. }
  390. case MODE_IS127:
  391. case MODE_4GV_NB:
  392. case MODE_4GV_WB: {
  393. /* Add the DSP frame info header. Header format:
  394. * Bits 0-3 : Frame rate
  395. */
  396. *voc_pkt = buf_node->frame.header.packet_rate & 0x0F;
  397. voc_pkt = voc_pkt + DSP_FRAME_HDR_LEN;
  398. *pkt_len = buf_node->frame.len + DSP_FRAME_HDR_LEN;
  399. memcpy(voc_pkt,
  400. &buf_node->frame.voc_pkt[0],
  401. buf_node->frame.len);
  402. list_add_tail(&buf_node->list, &prtd->free_in_queue);
  403. break;
  404. }
  405. case MODE_G711:
  406. case MODE_G711A:{
  407. /* G711 frames are 10ms each but the DSP expects 20ms
  408. * worth of data, so send two 10ms frames per buffer.
  409. */
  410. /* Add the first DSP frame info header. Header format:
  411. * Bits 0-1: Frame type
  412. * Bits 2-3: Frame rate
  413. */
  414. *voc_pkt = ((prtd->rate_type & 0x0F) << 2) |
  415. (buf_node->frame.header.frame_type & 0x03);
  416. voc_pkt = voc_pkt + DSP_FRAME_HDR_LEN;
  417. *pkt_len = buf_node->frame.len + DSP_FRAME_HDR_LEN;
  418. memcpy(voc_pkt,
  419. &buf_node->frame.voc_pkt[0],
  420. buf_node->frame.len);
  421. voc_pkt = voc_pkt + buf_node->frame.len;
  422. list_add_tail(&buf_node->list, &prtd->free_in_queue);
  423. if (!list_empty(&prtd->in_queue)) {
  424. /* Get the second buffer. */
  425. buf_node = list_first_entry(&prtd->in_queue,
  426. struct voip_buf_node,
  427. list);
  428. list_del(&buf_node->list);
  429. /* Add the second DSP frame info header.
  430. * Header format:
  431. * Bits 0-1: Frame type
  432. * Bits 2-3: Frame rate
  433. */
  434. *voc_pkt = ((prtd->rate_type & 0x0F) << 2) |
  435. (buf_node->frame.header.frame_type & 0x03);
  436. voc_pkt = voc_pkt + DSP_FRAME_HDR_LEN;
  437. *pkt_len = *pkt_len +
  438. buf_node->frame.len + DSP_FRAME_HDR_LEN;
  439. memcpy(voc_pkt,
  440. &buf_node->frame.voc_pkt[0],
  441. buf_node->frame.len);
  442. list_add_tail(&buf_node->list,
  443. &prtd->free_in_queue);
  444. } else {
  445. /* Only 10ms worth of data is available, signal
  446. * erasure frame.
  447. */
  448. *voc_pkt = ((prtd->rate_type & 0x0F) << 2) |
  449. (MVS_G711A_ERASURE & 0x03);
  450. *pkt_len = *pkt_len + DSP_FRAME_HDR_LEN;
  451. pr_debug("%s, Only 10ms read, erase 2nd frame\n",
  452. __func__);
  453. }
  454. break;
  455. }
  456. default: {
  457. *pkt_len = buf_node->frame.len;
  458. memcpy(voc_pkt,
  459. &buf_node->frame.voc_pkt[0],
  460. buf_node->frame.len);
  461. list_add_tail(&buf_node->list, &prtd->free_in_queue);
  462. }
  463. }
  464. pr_debug("dl_pkt: pkt_len=%d, frame_len=%d\n", *pkt_len,
  465. buf_node->frame.len);
  466. prtd->pcm_playback_irq_pos += prtd->pcm_count;
  467. spin_unlock_irqrestore(&prtd->dsp_lock, dsp_flags);
  468. snd_pcm_period_elapsed(prtd->playback_substream);
  469. } else {
  470. *pkt_len = 0;
  471. spin_unlock_irqrestore(&prtd->dsp_lock, dsp_flags);
  472. pr_err("DL data not available\n");
  473. }
  474. wake_up(&prtd->in_wait);
  475. }
  476. static struct snd_pcm_hw_constraint_list constraints_sample_rates = {
  477. .count = ARRAY_SIZE(supported_sample_rates),
  478. .list = supported_sample_rates,
  479. .mask = 0,
  480. };
  481. static int msm_pcm_playback_prepare(struct snd_pcm_substream *substream)
  482. {
  483. struct snd_pcm_runtime *runtime = substream->runtime;
  484. struct voip_drv_info *prtd = runtime->private_data;
  485. prtd->play_samp_rate = runtime->rate;
  486. prtd->pcm_size = snd_pcm_lib_buffer_bytes(substream);
  487. prtd->pcm_count = snd_pcm_lib_period_bytes(substream);
  488. prtd->pcm_playback_irq_pos = 0;
  489. prtd->pcm_playback_buf_pos = 0;
  490. return 0;
  491. }
  492. static int msm_pcm_capture_prepare(struct snd_pcm_substream *substream)
  493. {
  494. struct snd_pcm_runtime *runtime = substream->runtime;
  495. struct voip_drv_info *prtd = runtime->private_data;
  496. int ret = 0;
  497. prtd->cap_samp_rate = runtime->rate;
  498. prtd->pcm_capture_size = snd_pcm_lib_buffer_bytes(substream);
  499. prtd->pcm_capture_count = snd_pcm_lib_period_bytes(substream);
  500. prtd->pcm_capture_irq_pos = 0;
  501. prtd->pcm_capture_buf_pos = 0;
  502. return ret;
  503. }
  504. static int msm_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  505. {
  506. int ret = 0;
  507. struct snd_pcm_runtime *runtime = substream->runtime;
  508. struct voip_drv_info *prtd = runtime->private_data;
  509. switch (cmd) {
  510. case SNDRV_PCM_TRIGGER_START:
  511. case SNDRV_PCM_TRIGGER_RESUME:
  512. pr_debug("%s: Trigger start\n", __func__);
  513. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  514. prtd->capture_start = 1;
  515. else
  516. prtd->playback_start = 1;
  517. break;
  518. case SNDRV_PCM_TRIGGER_STOP:
  519. pr_debug("SNDRV_PCM_TRIGGER_STOP\n");
  520. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  521. prtd->playback_start = 0;
  522. else
  523. prtd->capture_start = 0;
  524. break;
  525. default:
  526. ret = -EINVAL;
  527. break;
  528. }
  529. return ret;
  530. }
  531. static int msm_pcm_open(struct snd_pcm_substream *substream)
  532. {
  533. struct snd_pcm_runtime *runtime = substream->runtime;
  534. struct voip_drv_info *prtd = &voip_info;
  535. int ret = 0;
  536. pr_debug("%s, VoIP\n", __func__);
  537. mutex_lock(&prtd->lock);
  538. runtime->hw = msm_pcm_hardware;
  539. ret = snd_pcm_hw_constraint_list(runtime, 0,
  540. SNDRV_PCM_HW_PARAM_RATE,
  541. &constraints_sample_rates);
  542. if (ret < 0)
  543. pr_debug("snd_pcm_hw_constraint_list failed\n");
  544. ret = snd_pcm_hw_constraint_integer(runtime,
  545. SNDRV_PCM_HW_PARAM_PERIODS);
  546. if (ret < 0) {
  547. pr_debug("snd_pcm_hw_constraint_integer failed\n");
  548. goto err;
  549. }
  550. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  551. prtd->playback_substream = substream;
  552. prtd->playback_instance++;
  553. } else {
  554. prtd->capture_substream = substream;
  555. prtd->capture_instance++;
  556. }
  557. runtime->private_data = prtd;
  558. err:
  559. mutex_unlock(&prtd->lock);
  560. return ret;
  561. }
  562. static int msm_pcm_playback_copy(struct snd_pcm_substream *substream, int a,
  563. snd_pcm_uframes_t hwoff, void __user *buf, snd_pcm_uframes_t frames)
  564. {
  565. int ret = 0;
  566. struct voip_buf_node *buf_node = NULL;
  567. struct snd_pcm_runtime *runtime = substream->runtime;
  568. struct voip_drv_info *prtd = runtime->private_data;
  569. unsigned long dsp_flags;
  570. int count = frames_to_bytes(runtime, frames);
  571. pr_debug("%s: count = %d, frames=%d\n", __func__, count, (int)frames);
  572. ret = wait_event_interruptible_timeout(prtd->in_wait,
  573. (!list_empty(&prtd->free_in_queue) ||
  574. prtd->state == VOIP_STOPPED),
  575. 1 * HZ);
  576. if (ret > 0) {
  577. if (count <= VOIP_MAX_VOC_PKT_SIZE) {
  578. spin_lock_irqsave(&prtd->dsp_lock, dsp_flags);
  579. buf_node =
  580. list_first_entry(&prtd->free_in_queue,
  581. struct voip_buf_node, list);
  582. list_del(&buf_node->list);
  583. spin_unlock_irqrestore(&prtd->dsp_lock, dsp_flags);
  584. if (prtd->mode == MODE_PCM) {
  585. ret = copy_from_user(&buf_node->frame.voc_pkt,
  586. buf, count);
  587. buf_node->frame.len = count;
  588. } else
  589. ret = copy_from_user(&buf_node->frame,
  590. buf, count);
  591. spin_lock_irqsave(&prtd->dsp_lock, dsp_flags);
  592. list_add_tail(&buf_node->list, &prtd->in_queue);
  593. spin_unlock_irqrestore(&prtd->dsp_lock, dsp_flags);
  594. } else {
  595. pr_err("%s: Write cnt %d is > VOIP_MAX_VOC_PKT_SIZE\n",
  596. __func__, count);
  597. ret = -ENOMEM;
  598. }
  599. } else if (ret == 0) {
  600. pr_err("%s: No free DL buffs\n", __func__);
  601. ret = -ETIMEDOUT;
  602. } else {
  603. pr_err("%s: playback copy was interrupted\n", __func__);
  604. }
  605. return ret;
  606. }
  607. static int msm_pcm_capture_copy(struct snd_pcm_substream *substream,
  608. int channel, snd_pcm_uframes_t hwoff, void __user *buf,
  609. snd_pcm_uframes_t frames)
  610. {
  611. int ret = 0;
  612. int count = 0;
  613. struct voip_buf_node *buf_node = NULL;
  614. struct snd_pcm_runtime *runtime = substream->runtime;
  615. struct voip_drv_info *prtd = runtime->private_data;
  616. unsigned long dsp_flags;
  617. int size;
  618. count = frames_to_bytes(runtime, frames);
  619. pr_debug("%s: count = %d\n", __func__, count);
  620. ret = wait_event_interruptible_timeout(prtd->out_wait,
  621. (!list_empty(&prtd->out_queue) ||
  622. prtd->state == VOIP_STOPPED),
  623. 1 * HZ);
  624. if (ret > 0) {
  625. if (count <= VOIP_MAX_VOC_PKT_SIZE) {
  626. spin_lock_irqsave(&prtd->dsp_ul_lock, dsp_flags);
  627. buf_node = list_first_entry(&prtd->out_queue,
  628. struct voip_buf_node, list);
  629. list_del(&buf_node->list);
  630. spin_unlock_irqrestore(&prtd->dsp_ul_lock, dsp_flags);
  631. if (prtd->mode == MODE_PCM) {
  632. ret = copy_to_user(buf,
  633. &buf_node->frame.voc_pkt,
  634. buf_node->frame.len);
  635. } else {
  636. size = sizeof(buf_node->frame.header) +
  637. sizeof(buf_node->frame.len) +
  638. buf_node->frame.len;
  639. ret = copy_to_user(buf,
  640. &buf_node->frame,
  641. size;
  642. }
  643. if (ret) {
  644. pr_err("%s: Copy to user retuned %d\n",
  645. __func__, ret);
  646. ret = -EFAULT;
  647. }
  648. spin_lock_irqsave(&prtd->dsp_ul_lock, dsp_flags);
  649. list_add_tail(&buf_node->list,
  650. &prtd->free_out_queue);
  651. spin_unlock_irqrestore(&prtd->dsp_ul_lock, dsp_flags);
  652. } else {
  653. pr_err("%s: Read count %d > VOIP_MAX_VOC_PKT_SIZE\n",
  654. __func__, count);
  655. ret = -ENOMEM;
  656. }
  657. } else if (ret == 0) {
  658. pr_err("%s: No UL data available\n", __func__);
  659. ret = -ETIMEDOUT;
  660. } else {
  661. pr_err("%s: Read was interrupted\n", __func__);
  662. ret = -ERESTARTSYS;
  663. }
  664. return ret;
  665. }
  666. static int msm_pcm_copy(struct snd_pcm_substream *substream, int a,
  667. snd_pcm_uframes_t hwoff, void __user *buf, snd_pcm_uframes_t frames)
  668. {
  669. int ret = 0;
  670. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  671. ret = msm_pcm_playback_copy(substream, a, hwoff, buf, frames);
  672. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  673. ret = msm_pcm_capture_copy(substream, a, hwoff, buf, frames);
  674. return ret;
  675. }
  676. static int msm_pcm_close(struct snd_pcm_substream *substream)
  677. {
  678. int ret = 0;
  679. struct list_head *ptr = NULL;
  680. struct list_head *next = NULL;
  681. struct voip_buf_node *buf_node = NULL;
  682. struct snd_dma_buffer *p_dma_buf, *c_dma_buf;
  683. struct snd_pcm_substream *p_substream, *c_substream;
  684. struct snd_pcm_runtime *runtime;
  685. struct voip_drv_info *prtd;
  686. unsigned long dsp_flags;
  687. if (substream == NULL) {
  688. pr_err("substream is NULL\n");
  689. return -EINVAL;
  690. }
  691. runtime = substream->runtime;
  692. prtd = runtime->private_data;
  693. wake_up(&prtd->out_wait);
  694. mutex_lock(&prtd->lock);
  695. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  696. prtd->playback_instance--;
  697. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  698. prtd->capture_instance--;
  699. if (!prtd->playback_instance && !prtd->capture_instance) {
  700. if (prtd->state == VOIP_STARTED) {
  701. prtd->state = VOIP_STOPPED;
  702. voc_end_voice_call(
  703. voc_get_session_id(VOIP_SESSION_NAME));
  704. voc_register_mvs_cb(NULL, NULL, prtd);
  705. }
  706. /* release all buffer */
  707. /* release in_queue and free_in_queue */
  708. pr_debug("release all buffer\n");
  709. p_substream = prtd->playback_substream;
  710. if (p_substream == NULL) {
  711. pr_debug("p_substream is NULL\n");
  712. goto capt;
  713. }
  714. p_dma_buf = &p_substream->dma_buffer;
  715. if (p_dma_buf == NULL) {
  716. pr_debug("p_dma_buf is NULL\n");
  717. goto capt;
  718. }
  719. if (p_dma_buf->area != NULL) {
  720. spin_lock_irqsave(&prtd->dsp_lock, dsp_flags);
  721. list_for_each_safe(ptr, next, &prtd->in_queue) {
  722. buf_node = list_entry(ptr,
  723. struct voip_buf_node, list);
  724. list_del(&buf_node->list);
  725. }
  726. list_for_each_safe(ptr, next, &prtd->free_in_queue) {
  727. buf_node = list_entry(ptr,
  728. struct voip_buf_node, list);
  729. list_del(&buf_node->list);
  730. }
  731. spin_unlock_irqrestore(&prtd->dsp_lock, dsp_flags);
  732. dma_free_coherent(p_substream->pcm->card->dev,
  733. runtime->hw.buffer_bytes_max, p_dma_buf->area,
  734. p_dma_buf->addr);
  735. p_dma_buf->area = NULL;
  736. }
  737. /* release out_queue and free_out_queue */
  738. capt: c_substream = prtd->capture_substream;
  739. if (c_substream == NULL) {
  740. pr_debug("c_substream is NULL\n");
  741. goto done;
  742. }
  743. c_dma_buf = &c_substream->dma_buffer;
  744. if (c_substream == NULL) {
  745. pr_debug("c_dma_buf is NULL.\n");
  746. goto done;
  747. }
  748. if (c_dma_buf->area != NULL) {
  749. spin_lock_irqsave(&prtd->dsp_ul_lock, dsp_flags);
  750. list_for_each_safe(ptr, next, &prtd->out_queue) {
  751. buf_node = list_entry(ptr,
  752. struct voip_buf_node, list);
  753. list_del(&buf_node->list);
  754. }
  755. list_for_each_safe(ptr, next, &prtd->free_out_queue) {
  756. buf_node = list_entry(ptr,
  757. struct voip_buf_node, list);
  758. list_del(&buf_node->list);
  759. }
  760. spin_unlock_irqrestore(&prtd->dsp_ul_lock, dsp_flags);
  761. dma_free_coherent(c_substream->pcm->card->dev,
  762. runtime->hw.buffer_bytes_max, c_dma_buf->area,
  763. c_dma_buf->addr);
  764. c_dma_buf->area = NULL;
  765. }
  766. done:
  767. prtd->capture_substream = NULL;
  768. prtd->playback_substream = NULL;
  769. }
  770. mutex_unlock(&prtd->lock);
  771. return ret;
  772. }
  773. static int msm_pcm_prepare(struct snd_pcm_substream *substream)
  774. {
  775. int ret = 0;
  776. struct snd_pcm_runtime *runtime = substream->runtime;
  777. struct voip_drv_info *prtd = runtime->private_data;
  778. uint32_t media_type = 0;
  779. uint32_t rate_type = 0;
  780. mutex_lock(&prtd->lock);
  781. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  782. ret = msm_pcm_playback_prepare(substream);
  783. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  784. ret = msm_pcm_capture_prepare(substream);
  785. if ((runtime->format != FORMAT_SPECIAL) &&
  786. ((prtd->mode == MODE_AMR) || (prtd->mode == MODE_AMR_WB) ||
  787. (prtd->mode == MODE_IS127) || (prtd->mode == MODE_4GV_NB) ||
  788. (prtd->mode == MODE_4GV_WB) || (prtd->mode == MODE_G711) ||
  789. (prtd->mode == MODE_G711A))) {
  790. pr_err("mode:%d and format:%u are not mached\n",
  791. prtd->mode, (uint32_t)runtime->format);
  792. ret = -EINVAL;
  793. goto done;
  794. }
  795. if ((runtime->format != FORMAT_S16_LE) &&
  796. (prtd->mode == MODE_PCM)) {
  797. pr_err("mode:%d and format:%u are not mached\n",
  798. prtd->mode, (uint32_t)runtime->format);
  799. ret = -EINVAL;
  800. goto done;
  801. }
  802. if (prtd->playback_instance && prtd->capture_instance
  803. && (prtd->state != VOIP_STARTED)) {
  804. ret = voip_get_rate_type(prtd->mode,
  805. prtd->rate,
  806. &rate_type);
  807. if (ret < 0) {
  808. pr_err("fail at getting rate_type\n");
  809. ret = -EINVAL;
  810. goto done;
  811. }
  812. prtd->rate_type = rate_type;
  813. ret = voip_get_media_type(prtd->mode,
  814. prtd->rate_type,
  815. prtd->play_samp_rate,
  816. &media_type);
  817. if (ret < 0) {
  818. pr_err("fail at getting media_type\n");
  819. goto done;
  820. }
  821. pr_debug(" media_type=%d, rate_type=%d\n", media_type,
  822. rate_type);
  823. if ((prtd->play_samp_rate == 8000) &&
  824. (prtd->cap_samp_rate == 8000))
  825. voc_config_vocoder(media_type, rate_type,
  826. VSS_NETWORK_ID_VOIP_NB,
  827. voip_info.dtx_mode);
  828. else if ((prtd->play_samp_rate == 16000) &&
  829. (prtd->cap_samp_rate == 16000))
  830. voc_config_vocoder(media_type, rate_type,
  831. VSS_NETWORK_ID_VOIP_WB,
  832. voip_info.dtx_mode);
  833. else {
  834. pr_debug("%s: Invalid rate playback %d, capture %d\n",
  835. __func__, prtd->play_samp_rate,
  836. prtd->cap_samp_rate);
  837. goto done;
  838. }
  839. voc_register_mvs_cb(voip_process_ul_pkt,
  840. voip_process_dl_pkt, prtd);
  841. voc_start_voice_call(voc_get_session_id(VOIP_SESSION_NAME));
  842. prtd->state = VOIP_STARTED;
  843. }
  844. done:
  845. mutex_unlock(&prtd->lock);
  846. return ret;
  847. }
  848. static snd_pcm_uframes_t
  849. msm_pcm_playback_pointer(struct snd_pcm_substream *substream)
  850. {
  851. struct snd_pcm_runtime *runtime = substream->runtime;
  852. struct voip_drv_info *prtd = runtime->private_data;
  853. pr_debug("%s\n", __func__);
  854. if (prtd->pcm_playback_irq_pos >= prtd->pcm_size)
  855. prtd->pcm_playback_irq_pos = 0;
  856. return bytes_to_frames(runtime, (prtd->pcm_playback_irq_pos));
  857. }
  858. static snd_pcm_uframes_t
  859. msm_pcm_capture_pointer(struct snd_pcm_substream *substream)
  860. {
  861. struct snd_pcm_runtime *runtime = substream->runtime;
  862. struct voip_drv_info *prtd = runtime->private_data;
  863. if (prtd->pcm_capture_irq_pos >= prtd->pcm_capture_size)
  864. prtd->pcm_capture_irq_pos = 0;
  865. return bytes_to_frames(runtime, (prtd->pcm_capture_irq_pos));
  866. }
  867. static snd_pcm_uframes_t msm_pcm_pointer(struct snd_pcm_substream *substream)
  868. {
  869. snd_pcm_uframes_t ret = 0;
  870. pr_debug("%s\n", __func__);
  871. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  872. ret = msm_pcm_playback_pointer(substream);
  873. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  874. ret = msm_pcm_capture_pointer(substream);
  875. return ret;
  876. }
  877. static int msm_pcm_mmap(struct snd_pcm_substream *substream,
  878. struct vm_area_struct *vma)
  879. {
  880. struct snd_pcm_runtime *runtime = substream->runtime;
  881. pr_debug("%s\n", __func__);
  882. dma_mmap_coherent(substream->pcm->card->dev, vma,
  883. runtime->dma_area,
  884. runtime->dma_addr,
  885. runtime->dma_bytes);
  886. return 0;
  887. }
  888. static int msm_pcm_hw_params(struct snd_pcm_substream *substream,
  889. struct snd_pcm_hw_params *params)
  890. {
  891. struct snd_pcm_runtime *runtime = substream->runtime;
  892. struct snd_dma_buffer *dma_buf = &substream->dma_buffer;
  893. struct voip_buf_node *buf_node = NULL;
  894. int i = 0, offset = 0;
  895. pr_debug("%s: voip\n", __func__);
  896. mutex_lock(&voip_info.lock);
  897. dma_buf->dev.type = SNDRV_DMA_TYPE_DEV;
  898. dma_buf->dev.dev = substream->pcm->card->dev;
  899. dma_buf->private_data = NULL;
  900. dma_buf->area = dma_alloc_coherent(substream->pcm->card->dev,
  901. runtime->hw.buffer_bytes_max,
  902. &dma_buf->addr, GFP_KERNEL);
  903. if (!dma_buf->area) {
  904. pr_err("%s:MSM VOIP dma_alloc failed\n", __func__);
  905. return -ENOMEM;
  906. }
  907. dma_buf->bytes = runtime->hw.buffer_bytes_max;
  908. memset(dma_buf->area, 0, runtime->hw.buffer_bytes_max);
  909. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  910. for (i = 0; i < VOIP_MAX_Q_LEN; i++) {
  911. buf_node = (void *)dma_buf->area + offset;
  912. list_add_tail(&buf_node->list,
  913. &voip_info.free_in_queue);
  914. offset = offset + sizeof(struct voip_buf_node);
  915. }
  916. } else {
  917. for (i = 0; i < VOIP_MAX_Q_LEN; i++) {
  918. buf_node = (void *) dma_buf->area + offset;
  919. list_add_tail(&buf_node->list,
  920. &voip_info.free_out_queue);
  921. offset = offset + sizeof(struct voip_buf_node);
  922. }
  923. }
  924. mutex_unlock(&voip_info.lock);
  925. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  926. return 0;
  927. }
  928. static int msm_voip_mode_rate_config_get(struct snd_kcontrol *kcontrol,
  929. struct snd_ctl_elem_value *ucontrol)
  930. {
  931. mutex_lock(&voip_info.lock);
  932. ucontrol->value.integer.value[0] = voip_info.mode;
  933. ucontrol->value.integer.value[1] = voip_info.rate;
  934. mutex_unlock(&voip_info.lock);
  935. return 0;
  936. }
  937. static int msm_voip_mode_rate_config_put(struct snd_kcontrol *kcontrol,
  938. struct snd_ctl_elem_value *ucontrol)
  939. {
  940. mutex_lock(&voip_info.lock);
  941. voip_info.mode = ucontrol->value.integer.value[0];
  942. voip_info.rate = ucontrol->value.integer.value[1];
  943. pr_debug("%s: mode=%d,rate=%d\n", __func__, voip_info.mode,
  944. voip_info.rate);
  945. mutex_unlock(&voip_info.lock);
  946. return 0;
  947. }
  948. static int voip_get_rate_type(uint32_t mode, uint32_t rate,
  949. uint32_t *rate_type)
  950. {
  951. int ret = 0;
  952. switch (mode) {
  953. case MODE_AMR: {
  954. switch (rate) {
  955. case 4750:
  956. *rate_type = AMR_RATE_4750;
  957. break;
  958. case 5150:
  959. *rate_type = AMR_RATE_5150;
  960. break;
  961. case 5900:
  962. *rate_type = AMR_RATE_5900;
  963. break;
  964. case 6700:
  965. *rate_type = AMR_RATE_6700;
  966. break;
  967. case 7400:
  968. *rate_type = AMR_RATE_7400;
  969. break;
  970. case 7950:
  971. *rate_type = AMR_RATE_7950;
  972. break;
  973. case 10200:
  974. *rate_type = AMR_RATE_10200;
  975. break;
  976. case 12200:
  977. *rate_type = AMR_RATE_12200;
  978. break;
  979. default:
  980. pr_err("wrong rate for AMR NB.\n");
  981. ret = -EINVAL;
  982. break;
  983. }
  984. break;
  985. }
  986. case MODE_AMR_WB: {
  987. switch (rate) {
  988. case 6600:
  989. *rate_type = AMR_RATE_6600 - AMR_RATE_6600;
  990. break;
  991. case 8850:
  992. *rate_type = AMR_RATE_8850 - AMR_RATE_6600;
  993. break;
  994. case 12650:
  995. *rate_type = AMR_RATE_12650 - AMR_RATE_6600;
  996. break;
  997. case 14250:
  998. *rate_type = AMR_RATE_14250 - AMR_RATE_6600;
  999. break;
  1000. case 15850:
  1001. *rate_type = AMR_RATE_15850 - AMR_RATE_6600;
  1002. break;
  1003. case 18250:
  1004. *rate_type = AMR_RATE_18250 - AMR_RATE_6600;
  1005. break;
  1006. case 19850:
  1007. *rate_type = AMR_RATE_19850 - AMR_RATE_6600;
  1008. break;
  1009. case 23050:
  1010. *rate_type = AMR_RATE_23050 - AMR_RATE_6600;
  1011. break;
  1012. case 23850:
  1013. *rate_type = AMR_RATE_23850 - AMR_RATE_6600;
  1014. break;
  1015. default:
  1016. pr_err("wrong rate for AMR_WB.\n");
  1017. ret = -EINVAL;
  1018. break;
  1019. }
  1020. break;
  1021. }
  1022. case MODE_PCM: {
  1023. *rate_type = 0;
  1024. break;
  1025. }
  1026. case MODE_IS127:
  1027. case MODE_4GV_NB:
  1028. case MODE_4GV_WB: {
  1029. switch (rate) {
  1030. case VOC_0_RATE:
  1031. case VOC_8_RATE:
  1032. case VOC_4_RATE:
  1033. case VOC_2_RATE:
  1034. case VOC_1_RATE:
  1035. *rate_type = rate;
  1036. break;
  1037. default:
  1038. pr_err("wrong rate for IS127/4GV_NB/WB.\n");
  1039. ret = -EINVAL;
  1040. break;
  1041. }
  1042. break;
  1043. }
  1044. case MODE_G711:
  1045. case MODE_G711A:
  1046. *rate_type = rate;
  1047. break;
  1048. default:
  1049. pr_err("wrong mode type.\n");
  1050. ret = -EINVAL;
  1051. }
  1052. pr_debug("%s, mode=%d, rate=%u, rate_type=%d\n",
  1053. __func__, mode, rate, *rate_type);
  1054. return ret;
  1055. }
  1056. static int voip_get_media_type(uint32_t mode, uint32_t rate_type,
  1057. unsigned int samp_rate,
  1058. uint32_t *media_type)
  1059. {
  1060. int ret = 0;
  1061. pr_debug("%s: mode=%d, samp_rate=%d\n", __func__,
  1062. mode, samp_rate);
  1063. switch (mode) {
  1064. case MODE_AMR:
  1065. *media_type = VSS_MEDIA_ID_AMR_NB_MODEM;
  1066. break;
  1067. case MODE_AMR_WB:
  1068. *media_type = VSS_MEDIA_ID_AMR_WB_MODEM;
  1069. break;
  1070. case MODE_PCM:
  1071. if (samp_rate == 8000)
  1072. *media_type = VSS_MEDIA_ID_PCM_NB;
  1073. else
  1074. *media_type = VSS_MEDIA_ID_PCM_WB;
  1075. break;
  1076. case MODE_IS127: /* EVRC-A */
  1077. *media_type = VSS_MEDIA_ID_EVRC_MODEM;
  1078. break;
  1079. case MODE_4GV_NB: /* EVRC-B */
  1080. *media_type = VSS_MEDIA_ID_4GV_NB_MODEM;
  1081. break;
  1082. case MODE_4GV_WB: /* EVRC-WB */
  1083. *media_type = VSS_MEDIA_ID_4GV_WB_MODEM;
  1084. break;
  1085. case MODE_G711:
  1086. case MODE_G711A:
  1087. if (rate_type == MVS_G711A_MODE_MULAW)
  1088. *media_type = VSS_MEDIA_ID_G711_MULAW;
  1089. else
  1090. *media_type = VSS_MEDIA_ID_G711_ALAW;
  1091. break;
  1092. default:
  1093. pr_debug(" input mode is not supported\n");
  1094. ret = -EINVAL;
  1095. }
  1096. pr_debug("%s: media_type is 0x%x\n", __func__, *media_type);
  1097. return ret;
  1098. }
  1099. static struct snd_pcm_ops msm_pcm_ops = {
  1100. .open = msm_pcm_open,
  1101. .copy = msm_pcm_copy,
  1102. .hw_params = msm_pcm_hw_params,
  1103. .close = msm_pcm_close,
  1104. .prepare = msm_pcm_prepare,
  1105. .trigger = msm_pcm_trigger,
  1106. .pointer = msm_pcm_pointer,
  1107. .mmap = msm_pcm_mmap,
  1108. };
  1109. static int msm_asoc_pcm_new(struct snd_soc_pcm_runtime *rtd)
  1110. {
  1111. struct snd_card *card = rtd->card->snd_card;
  1112. int ret = 0;
  1113. pr_debug("msm_asoc_pcm_new\n");
  1114. if (!card->dev->coherent_dma_mask)
  1115. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  1116. return ret;
  1117. }
  1118. static struct snd_soc_platform_driver msm_soc_platform = {
  1119. .ops = &msm_pcm_ops,
  1120. .pcm_new = msm_asoc_pcm_new,
  1121. .probe = msm_pcm_voip_probe,
  1122. };
  1123. static __devinit int msm_pcm_probe(struct platform_device *pdev)
  1124. {
  1125. pr_info("%s: dev name %s\n", __func__, dev_name(&pdev->dev));
  1126. return snd_soc_register_platform(&pdev->dev,
  1127. &msm_soc_platform);
  1128. }
  1129. static int msm_pcm_remove(struct platform_device *pdev)
  1130. {
  1131. snd_soc_unregister_platform(&pdev->dev);
  1132. return 0;
  1133. }
  1134. static struct platform_driver msm_pcm_driver = {
  1135. .driver = {
  1136. .name = "msm-voip-dsp",
  1137. .owner = THIS_MODULE,
  1138. },
  1139. .probe = msm_pcm_probe,
  1140. .remove = __devexit_p(msm_pcm_remove),
  1141. };
  1142. static int __init msm_soc_platform_init(void)
  1143. {
  1144. memset(&voip_info, 0, sizeof(voip_info));
  1145. voip_info.mode = MODE_PCM;
  1146. mutex_init(&voip_info.lock);
  1147. spin_lock_init(&voip_info.dsp_lock);
  1148. spin_lock_init(&voip_info.dsp_ul_lock);
  1149. init_waitqueue_head(&voip_info.out_wait);
  1150. init_waitqueue_head(&voip_info.in_wait);
  1151. INIT_LIST_HEAD(&voip_info.in_queue);
  1152. INIT_LIST_HEAD(&voip_info.free_in_queue);
  1153. INIT_LIST_HEAD(&voip_info.out_queue);
  1154. INIT_LIST_HEAD(&voip_info.free_out_queue);
  1155. return platform_driver_register(&msm_pcm_driver);
  1156. }
  1157. module_init(msm_soc_platform_init);
  1158. static void __exit msm_soc_platform_exit(void)
  1159. {
  1160. platform_driver_unregister(&msm_pcm_driver);
  1161. }
  1162. module_exit(msm_soc_platform_exit);
  1163. MODULE_DESCRIPTION("PCM module platform driver");
  1164. MODULE_LICENSE("GPL v2");