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