123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722 |
- /* Copyright (c) 2012-2017, The Linux Foundation. All rights reserved.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 and
- * only version 2 as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- */
- #include <linux/init.h>
- #include <linux/err.h>
- #include <linux/module.h>
- #include <linux/time.h>
- #include <linux/wait.h>
- #include <linux/platform_device.h>
- #include <linux/slab.h>
- #include <linux/dma-mapping.h>
- #include <linux/of_device.h>
- #include <sound/core.h>
- #include <sound/soc.h>
- #include <sound/soc-dapm.h>
- #include <sound/pcm.h>
- #include <sound/initval.h>
- #include <sound/control.h>
- #include <asm/dma.h>
- #include "msm-pcm-q6-v2.h"
- #include "msm-pcm-routing-v2.h"
- #include "q6voice.h"
- #include "audio_ocmem.h"
- #define SHARED_MEM_BUF 2
- #define VOIP_MIN_Q_LEN 2
- #define VOIP_MAX_Q_LEN 10
- #define VOIP_MAX_VOC_PKT_SIZE 4096
- #define VOIP_MIN_VOC_PKT_SIZE 320
- /* Length of the DSP frame info header added to the voc packet. */
- #define DSP_FRAME_HDR_LEN 1
- #define MODE_IS127 0x2
- #define MODE_4GV_NB 0x3
- #define MODE_4GV_WB 0x4
- #define MODE_AMR 0x5
- #define MODE_AMR_WB 0xD
- #define MODE_PCM 0xC
- #define MODE_4GV_NW 0xE
- #define MODE_G711 0xA
- #define MODE_G711A 0xF
- enum msm_audio_g711a_frame_type {
- MVS_G711A_SPEECH_GOOD,
- MVS_G711A_SID,
- MVS_G711A_NO_DATA,
- MVS_G711A_ERASURE
- };
- enum msm_audio_g711a_mode {
- MVS_G711A_MODE_MULAW,
- MVS_G711A_MODE_ALAW
- };
- enum msm_audio_g711_mode {
- MVS_G711_MODE_MULAW,
- MVS_G711_MODE_ALAW
- };
- #define VOIP_MODE_MAX MODE_G711A
- #define VOIP_RATE_MAX 23850
- enum format {
- FORMAT_S16_LE = 2,
- FORMAT_SPECIAL = 31,
- };
- enum amr_rate_type {
- AMR_RATE_4750, /* AMR 4.75 kbps */
- AMR_RATE_5150, /* AMR 5.15 kbps */
- AMR_RATE_5900, /* AMR 5.90 kbps */
- AMR_RATE_6700, /* AMR 6.70 kbps */
- AMR_RATE_7400, /* AMR 7.40 kbps */
- AMR_RATE_7950, /* AMR 7.95 kbps */
- AMR_RATE_10200, /* AMR 10.20 kbps */
- AMR_RATE_12200, /* AMR 12.20 kbps */
- AMR_RATE_6600, /* AMR-WB 6.60 kbps */
- AMR_RATE_8850, /* AMR-WB 8.85 kbps */
- AMR_RATE_12650, /* AMR-WB 12.65 kbps */
- AMR_RATE_14250, /* AMR-WB 14.25 kbps */
- AMR_RATE_15850, /* AMR-WB 15.85 kbps */
- AMR_RATE_18250, /* AMR-WB 18.25 kbps */
- AMR_RATE_19850, /* AMR-WB 19.85 kbps */
- AMR_RATE_23050, /* AMR-WB 23.05 kbps */
- AMR_RATE_23850, /* AMR-WB 23.85 kbps */
- AMR_RATE_UNDEF
- };
- enum voip_state {
- VOIP_STOPPED,
- VOIP_STARTED,
- };
- struct voip_frame_hdr {
- uint32_t timestamp;
- union {
- /*
- * Bits 0-15: Frame type
- * Bits 16-31: Frame rate
- */
- uint32_t frame_type;
- uint32_t packet_rate;
- };
- };
- struct voip_frame {
- struct voip_frame_hdr frm_hdr;
- uint32_t pktlen;
- uint8_t voc_pkt[VOIP_MAX_VOC_PKT_SIZE];
- };
- struct voip_buf_node {
- struct list_head list;
- struct voip_frame frame;
- };
- struct voip_drv_info {
- enum voip_state state;
- struct snd_pcm_substream *playback_substream;
- struct snd_pcm_substream *capture_substream;
- struct list_head in_queue;
- struct list_head free_in_queue;
- struct list_head out_queue;
- struct list_head free_out_queue;
- wait_queue_head_t out_wait;
- wait_queue_head_t in_wait;
- struct mutex lock;
- spinlock_t dsp_lock;
- spinlock_t dsp_ul_lock;
- bool voip_reset;
- uint32_t mode;
- uint32_t rate_type;
- uint32_t rate;
- uint32_t dtx_mode;
- uint8_t capture_start;
- uint8_t playback_start;
- uint8_t playback_instance;
- uint8_t capture_instance;
- unsigned int play_samp_rate;
- unsigned int cap_samp_rate;
- unsigned int pcm_size;
- unsigned int pcm_count;
- unsigned int pcm_playback_irq_pos; /* IRQ position */
- unsigned int pcm_playback_buf_pos; /* position in buffer */
- unsigned int pcm_capture_size;
- unsigned int pcm_capture_count;
- unsigned int pcm_capture_irq_pos; /* IRQ position */
- unsigned int pcm_capture_buf_pos; /* position in buffer */
- uint32_t evrc_min_rate;
- uint32_t evrc_max_rate;
- };
- static int voip_get_media_type(uint32_t mode, uint32_t rate_type,
- unsigned int samp_rate,
- unsigned int *media_type);
- static int voip_get_rate_type(uint32_t mode,
- uint32_t rate,
- uint32_t *rate_type);
- static int voip_config_vocoder(struct snd_pcm_substream *substream);
- static int msm_voip_mode_config_put(struct snd_kcontrol *kcontrol,
- struct snd_ctl_elem_value *ucontrol);
- static int msm_voip_mode_config_get(struct snd_kcontrol *kcontrol,
- struct snd_ctl_elem_value *ucontrol);
- static int msm_voip_rate_config_put(struct snd_kcontrol *kcontrol,
- struct snd_ctl_elem_value *ucontrol);
- static int msm_voip_evrc_min_max_rate_config_put(struct snd_kcontrol *kcontrol,
- struct snd_ctl_elem_value *ucontrol);
- static int msm_voip_evrc_min_max_rate_config_get(struct snd_kcontrol *kcontrol,
- struct snd_ctl_elem_value *ucontrol);
- static struct voip_drv_info voip_info;
- static struct snd_pcm_hardware msm_pcm_hardware = {
- .info = (SNDRV_PCM_INFO_MMAP |
- SNDRV_PCM_INFO_BLOCK_TRANSFER |
- SNDRV_PCM_INFO_MMAP_VALID |
- SNDRV_PCM_INFO_INTERLEAVED),
- .formats = SNDRV_PCM_FMTBIT_S16_LE |
- SNDRV_PCM_FMTBIT_SPECIAL,
- .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000,
- .rate_min = 8000,
- .rate_max = 16000,
- .channels_min = 1,
- .channels_max = 1,
- .buffer_bytes_max = sizeof(struct voip_buf_node) * VOIP_MIN_Q_LEN,
- .period_bytes_min = VOIP_MIN_VOC_PKT_SIZE,
- .period_bytes_max = VOIP_MAX_VOC_PKT_SIZE,
- .periods_min = VOIP_MIN_Q_LEN,
- .periods_max = VOIP_MAX_Q_LEN,
- .fifo_size = 0,
- };
- static int msm_voip_mute_put(struct snd_kcontrol *kcontrol,
- struct snd_ctl_elem_value *ucontrol)
- {
- int ret = 0;
- int mute = ucontrol->value.integer.value[0];
- int ramp_duration = ucontrol->value.integer.value[1];
- if ((mute < 0) || (mute > 1) || (ramp_duration < 0)) {
- pr_err(" %s Invalid arguments", __func__);
- ret = -EINVAL;
- goto done;
- }
- pr_debug("%s: mute=%d ramp_duration=%d\n", __func__, mute,
- ramp_duration);
- voc_set_tx_mute(voc_get_session_id(VOIP_SESSION_NAME), TX_PATH, mute,
- ramp_duration);
- done:
- return ret;
- }
- static int msm_voip_gain_put(struct snd_kcontrol *kcontrol,
- struct snd_ctl_elem_value *ucontrol)
- {
- int ret = 0;
- int volume = ucontrol->value.integer.value[0];
- int ramp_duration = ucontrol->value.integer.value[1];
- if ((volume < 0) || (ramp_duration < 0)) {
- pr_err(" %s Invalid arguments", __func__);
- ret = -EINVAL;
- goto done;
- }
- pr_debug("%s: volume: %d ramp_duration: %d\n", __func__, volume,
- ramp_duration);
- voc_set_rx_vol_step(voc_get_session_id(VOIP_SESSION_NAME),
- RX_PATH,
- volume,
- ramp_duration);
- done:
- return ret;
- }
- static int msm_voip_dtx_mode_put(struct snd_kcontrol *kcontrol,
- struct snd_ctl_elem_value *ucontrol)
- {
- mutex_lock(&voip_info.lock);
- voip_info.dtx_mode = ucontrol->value.integer.value[0];
- pr_debug("%s: dtx: %d\n", __func__, voip_info.dtx_mode);
- mutex_unlock(&voip_info.lock);
- return 0;
- }
- static int msm_voip_dtx_mode_get(struct snd_kcontrol *kcontrol,
- struct snd_ctl_elem_value *ucontrol)
- {
- mutex_lock(&voip_info.lock);
- ucontrol->value.integer.value[0] = voip_info.dtx_mode;
- mutex_unlock(&voip_info.lock);
- return 0;
- }
- static struct snd_kcontrol_new msm_voip_controls[] = {
- SOC_SINGLE_MULTI_EXT("Voip Tx Mute", SND_SOC_NOPM, 0,
- MAX_RAMP_DURATION,
- 0, 2, NULL, msm_voip_mute_put),
- SOC_SINGLE_MULTI_EXT("Voip Rx Gain", SND_SOC_NOPM, 0,
- MAX_RAMP_DURATION,
- 0, 2, NULL, msm_voip_gain_put),
- SOC_SINGLE_EXT("Voip Mode Config", SND_SOC_NOPM, 0, VOIP_MODE_MAX, 0,
- msm_voip_mode_config_get, msm_voip_mode_config_put),
- SOC_SINGLE_EXT("Voip Rate Config", SND_SOC_NOPM, 0, VOIP_RATE_MAX, 0,
- NULL, msm_voip_rate_config_put),
- SOC_SINGLE_MULTI_EXT("Voip Evrc Min Max Rate Config", SND_SOC_NOPM,
- 0, VOC_1_RATE, 0, 2, msm_voip_evrc_min_max_rate_config_get,
- msm_voip_evrc_min_max_rate_config_put),
- SOC_SINGLE_EXT("Voip Dtx Mode", SND_SOC_NOPM, 0, 1, 0,
- msm_voip_dtx_mode_get, msm_voip_dtx_mode_put),
- };
- static int msm_pcm_voip_probe(struct snd_soc_platform *platform)
- {
- snd_soc_add_platform_controls(platform, msm_voip_controls,
- ARRAY_SIZE(msm_voip_controls));
- return 0;
- }
- /* sample rate supported */
- static unsigned int supported_sample_rates[] = {8000, 16000};
- static void voip_ssr_cb_fn(uint32_t opcode, void *private_data)
- {
- /* Notify ASoC to send next playback/Capture to unblock write/read */
- struct voip_drv_info *prtd = private_data;
- if (opcode == 0xFFFFFFFF) {
- prtd->voip_reset = true;
- pr_debug("%s: Notify ASoC to send next playback/Capture\n",
- __func__);
- prtd->pcm_playback_irq_pos += prtd->pcm_count;
- if (prtd->state == VOIP_STARTED)
- snd_pcm_period_elapsed(prtd->playback_substream);
- wake_up(&prtd->out_wait);
- prtd->pcm_capture_irq_pos += prtd->pcm_capture_count;
- if (prtd->state == VOIP_STARTED)
- snd_pcm_period_elapsed(prtd->capture_substream);
- wake_up(&prtd->in_wait);
- } else {
- pr_err("%s: Invalid opcode during reset : %d\n",
- __func__, opcode);
- }
- }
- /* capture path */
- static void voip_process_ul_pkt(uint8_t *voc_pkt,
- uint32_t pkt_len,
- uint32_t timestamp,
- void *private_data)
- {
- struct voip_buf_node *buf_node = NULL;
- struct voip_drv_info *prtd = private_data;
- unsigned long dsp_flags;
- if (prtd->capture_substream == NULL)
- return;
- /* Copy up-link packet into out_queue. */
- spin_lock_irqsave(&prtd->dsp_ul_lock, dsp_flags);
- /* discarding UL packets till start is received */
- if (!list_empty(&prtd->free_out_queue) && prtd->capture_start) {
- buf_node = list_first_entry(&prtd->free_out_queue,
- struct voip_buf_node, list);
- list_del(&buf_node->list);
- switch (prtd->mode) {
- case MODE_AMR_WB:
- case MODE_AMR: {
- /* Remove the DSP frame info header. Header format:
- * Bits 0-3: Frame rate
- * Bits 4-7: Frame type
- */
- buf_node->frame.frm_hdr.timestamp = timestamp;
- buf_node->frame.frm_hdr.frame_type =
- ((*voc_pkt) & 0xF0) >> 4;
- voc_pkt = voc_pkt + DSP_FRAME_HDR_LEN;
- buf_node->frame.pktlen = pkt_len - DSP_FRAME_HDR_LEN;
- memcpy(&buf_node->frame.voc_pkt[0],
- voc_pkt,
- buf_node->frame.pktlen);
- list_add_tail(&buf_node->list, &prtd->out_queue);
- break;
- }
- case MODE_IS127:
- case MODE_4GV_NB:
- case MODE_4GV_WB:
- case MODE_4GV_NW: {
- /* Remove the DSP frame info header.
- * Header format:
- * Bits 0-3: frame rate
- */
- buf_node->frame.frm_hdr.timestamp = timestamp;
- buf_node->frame.frm_hdr.packet_rate = (*voc_pkt) & 0x0F;
- voc_pkt = voc_pkt + DSP_FRAME_HDR_LEN;
- buf_node->frame.pktlen = pkt_len - DSP_FRAME_HDR_LEN;
- memcpy(&buf_node->frame.voc_pkt[0],
- voc_pkt,
- buf_node->frame.pktlen);
- list_add_tail(&buf_node->list, &prtd->out_queue);
- break;
- }
- case MODE_G711:
- case MODE_G711A:{
- /* G711 frames are 10ms each, but the DSP works with
- * 20ms frames and sends two 10ms frames per buffer.
- * Extract the two frames and put them in separate
- * buffers.
- */
- /* Remove the first DSP frame info header.
- * Header format: G711A
- * Bits 0-1: Frame type
- * Bits 2-3: Frame rate
- *
- * Header format: G711
- * Bits 2-3: Frame rate
- */
- if (prtd->mode == MODE_G711A)
- buf_node->frame.frm_hdr.frame_type =
- (*voc_pkt) & 0x03;
- buf_node->frame.frm_hdr.timestamp = timestamp;
- voc_pkt = voc_pkt + DSP_FRAME_HDR_LEN;
- /* There are two frames in the buffer. Length of the
- * first frame:
- */
- buf_node->frame.pktlen = (pkt_len -
- 2 * DSP_FRAME_HDR_LEN) / 2;
- memcpy(&buf_node->frame.voc_pkt[0],
- voc_pkt,
- buf_node->frame.pktlen);
- voc_pkt = voc_pkt + buf_node->frame.pktlen;
- list_add_tail(&buf_node->list, &prtd->out_queue);
- /* Get another buffer from the free Q and fill in the
- * second frame.
- */
- if (!list_empty(&prtd->free_out_queue)) {
- buf_node =
- list_first_entry(&prtd->free_out_queue,
- struct voip_buf_node,
- list);
- list_del(&buf_node->list);
- /* Remove the second DSP frame info header.
- * Header format:
- * Bits 0-1: Frame type
- * Bits 2-3: Frame rate
- */
- if (prtd->mode == MODE_G711A)
- buf_node->frame.frm_hdr.frame_type =
- (*voc_pkt) & 0x03;
- buf_node->frame.frm_hdr.timestamp = timestamp;
- voc_pkt = voc_pkt + DSP_FRAME_HDR_LEN;
- /* There are two frames in the buffer. Length
- * of the second frame:
- */
- buf_node->frame.pktlen = (pkt_len -
- 2 * DSP_FRAME_HDR_LEN) / 2;
- memcpy(&buf_node->frame.voc_pkt[0],
- voc_pkt,
- buf_node->frame.pktlen);
- list_add_tail(&buf_node->list,
- &prtd->out_queue);
- } else {
- /* Drop the second frame */
- pr_err("%s: UL data dropped, read is slow\n",
- __func__);
- }
- break;
- }
- default: {
- buf_node->frame.frm_hdr.timestamp = timestamp;
- buf_node->frame.pktlen = pkt_len;
- memcpy(&buf_node->frame.voc_pkt[0],
- voc_pkt,
- buf_node->frame.pktlen);
- list_add_tail(&buf_node->list, &prtd->out_queue);
- }
- }
- pr_debug("%s: pkt_len =%d, frame.pktlen=%d, timestamp=%d\n",
- __func__, pkt_len, buf_node->frame.pktlen, timestamp);
- if (prtd->mode == MODE_PCM)
- prtd->pcm_capture_irq_pos += buf_node->frame.pktlen;
- else
- prtd->pcm_capture_irq_pos += prtd->pcm_capture_count;
- spin_unlock_irqrestore(&prtd->dsp_ul_lock, dsp_flags);
- snd_pcm_period_elapsed(prtd->capture_substream);
- } else {
- spin_unlock_irqrestore(&prtd->dsp_ul_lock, dsp_flags);
- pr_debug("UL data dropped\n");
- }
- wake_up(&prtd->out_wait);
- }
- /* playback path */
- static void voip_process_dl_pkt(uint8_t *voc_pkt, void *private_data)
- {
- struct voip_buf_node *buf_node = NULL;
- struct voip_drv_info *prtd = private_data;
- unsigned long dsp_flags;
- uint32_t rate_type;
- uint32_t frame_rate;
- u32 pkt_len;
- u8 *voc_addr = NULL;
- if (prtd->playback_substream == NULL)
- return;
- spin_lock_irqsave(&prtd->dsp_lock, dsp_flags);
- if (!list_empty(&prtd->in_queue) && prtd->playback_start) {
- buf_node = list_first_entry(&prtd->in_queue,
- struct voip_buf_node, list);
- list_del(&buf_node->list);
- switch (prtd->mode) {
- case MODE_AMR:
- case MODE_AMR_WB: {
- *((uint32_t *)voc_pkt) = buf_node->frame.pktlen +
- DSP_FRAME_HDR_LEN;
- /* Advance to the header of voip packet */
- voc_pkt = voc_pkt + sizeof(uint32_t);
- /*
- * Add the DSP frame info header. Header format:
- * Bits 0-3: Frame rate
- * Bits 4-7: Frame type
- */
- *voc_pkt = ((buf_node->frame.frm_hdr.frame_type &
- 0x0F) << 4);
- frame_rate = (buf_node->frame.frm_hdr.frame_type &
- 0xFFFF0000) >> 16;
- if (frame_rate) {
- if (voip_get_rate_type(prtd->mode, frame_rate,
- &rate_type)) {
- pr_err("%s(): fail at getting rate_type \n",
- __func__);
- } else
- prtd->rate_type = rate_type;
- }
- *voc_pkt |= prtd->rate_type & 0x0F;
- voc_pkt = voc_pkt + DSP_FRAME_HDR_LEN;
- memcpy(voc_pkt,
- &buf_node->frame.voc_pkt[0],
- buf_node->frame.pktlen);
- list_add_tail(&buf_node->list, &prtd->free_in_queue);
- break;
- }
- case MODE_IS127:
- case MODE_4GV_NB:
- case MODE_4GV_WB:
- case MODE_4GV_NW: {
- *((uint32_t *)voc_pkt) = buf_node->frame.pktlen +
- DSP_FRAME_HDR_LEN;
- /* Advance to the header of voip packet */
- voc_pkt = voc_pkt + sizeof(uint32_t);
- /*
- * Add the DSP frame info header. Header format:
- * Bits 0-3 : Frame rate
- */
- *voc_pkt = buf_node->frame.frm_hdr.packet_rate & 0x0F;
- voc_pkt = voc_pkt + DSP_FRAME_HDR_LEN;
- memcpy(voc_pkt,
- &buf_node->frame.voc_pkt[0],
- buf_node->frame.pktlen);
- list_add_tail(&buf_node->list, &prtd->free_in_queue);
- break;
- }
- case MODE_G711:
- case MODE_G711A:{
- /* G711 frames are 10ms each but the DSP expects 20ms
- * worth of data, so send two 10ms frames per buffer.
- */
- /* Add the first DSP frame info header. Header format:
- * Bits 0-1: Frame type
- * Bits 2-3: Frame rate
- */
- voc_addr = voc_pkt;
- voc_pkt = voc_pkt + sizeof(uint32_t);
- *voc_pkt = ((prtd->rate_type & 0x0F) << 2) |
- (buf_node->frame.frm_hdr.frame_type & 0x03);
- voc_pkt = voc_pkt + DSP_FRAME_HDR_LEN;
- pkt_len = buf_node->frame.pktlen + DSP_FRAME_HDR_LEN;
- memcpy(voc_pkt,
- &buf_node->frame.voc_pkt[0],
- buf_node->frame.pktlen);
- voc_pkt = voc_pkt + buf_node->frame.pktlen;
- list_add_tail(&buf_node->list, &prtd->free_in_queue);
- if (!list_empty(&prtd->in_queue)) {
- /* Get the second buffer. */
- buf_node = list_first_entry(&prtd->in_queue,
- struct voip_buf_node,
- list);
- list_del(&buf_node->list);
- /* Add the second DSP frame info header.
- * Header format:
- * Bits 0-1: Frame type
- * Bits 2-3: Frame rate
- */
- *voc_pkt = ((prtd->rate_type & 0x0F) << 2) |
- (buf_node->frame.frm_hdr.frame_type & 0x03);
- voc_pkt = voc_pkt + DSP_FRAME_HDR_LEN;
- pkt_len = pkt_len + buf_node->frame.pktlen +
- DSP_FRAME_HDR_LEN;
- memcpy(voc_pkt,
- &buf_node->frame.voc_pkt[0],
- buf_node->frame.pktlen);
- list_add_tail(&buf_node->list,
- &prtd->free_in_queue);
- } else {
- /* Only 10ms worth of data is available, signal
- * erasure frame.
- */
- *voc_pkt = ((prtd->rate_type & 0x0F) << 2) |
- (MVS_G711A_ERASURE & 0x03);
- pkt_len = pkt_len + DSP_FRAME_HDR_LEN;
- pr_debug("%s, Only 10ms read, erase 2nd frame\n",
- __func__);
- }
- *((uint32_t *)voc_addr) = pkt_len;
- break;
- }
- default: {
- *((uint32_t *)voc_pkt) = buf_node->frame.pktlen;
- voc_pkt = voc_pkt + sizeof(uint32_t);
- memcpy(voc_pkt,
- &buf_node->frame.voc_pkt[0],
- buf_node->frame.pktlen);
- list_add_tail(&buf_node->list, &prtd->free_in_queue);
- }
- }
- pr_debug("%s: frame.pktlen=%d\n", __func__, buf_node->frame.pktlen);
- if (prtd->mode == MODE_PCM)
- prtd->pcm_playback_irq_pos += buf_node->frame.pktlen;
- else
- prtd->pcm_playback_irq_pos += prtd->pcm_count;
- spin_unlock_irqrestore(&prtd->dsp_lock, dsp_flags);
- snd_pcm_period_elapsed(prtd->playback_substream);
- } else {
- *((uint32_t *)voc_pkt) = 0;
- spin_unlock_irqrestore(&prtd->dsp_lock, dsp_flags);
- pr_debug("DL data not available\n");
- }
- wake_up(&prtd->in_wait);
- }
- static struct snd_pcm_hw_constraint_list constraints_sample_rates = {
- .count = ARRAY_SIZE(supported_sample_rates),
- .list = supported_sample_rates,
- .mask = 0,
- };
- static int msm_pcm_playback_prepare(struct snd_pcm_substream *substream)
- {
- struct snd_pcm_runtime *runtime = substream->runtime;
- struct voip_drv_info *prtd = runtime->private_data;
- prtd->play_samp_rate = runtime->rate;
- prtd->pcm_size = snd_pcm_lib_buffer_bytes(substream);
- prtd->pcm_count = snd_pcm_lib_period_bytes(substream);
- prtd->pcm_playback_irq_pos = 0;
- prtd->pcm_playback_buf_pos = 0;
- return 0;
- }
- static int msm_pcm_capture_prepare(struct snd_pcm_substream *substream)
- {
- struct snd_pcm_runtime *runtime = substream->runtime;
- struct voip_drv_info *prtd = runtime->private_data;
- int ret = 0;
- prtd->cap_samp_rate = runtime->rate;
- prtd->pcm_capture_size = snd_pcm_lib_buffer_bytes(substream);
- prtd->pcm_capture_count = snd_pcm_lib_period_bytes(substream);
- prtd->pcm_capture_irq_pos = 0;
- prtd->pcm_capture_buf_pos = 0;
- return ret;
- }
- static int msm_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
- {
- int ret = 0;
- struct snd_pcm_runtime *runtime = substream->runtime;
- struct voip_drv_info *prtd = runtime->private_data;
- switch (cmd) {
- case SNDRV_PCM_TRIGGER_START:
- case SNDRV_PCM_TRIGGER_RESUME:
- pr_debug("%s: Trigger start\n", __func__);
- if ((!prtd->capture_start) && (!prtd->playback_start))
- voice_ocmem_process_req(VOICE, true);
- if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
- prtd->capture_start = 1;
- else
- prtd->playback_start = 1;
- break;
- case SNDRV_PCM_TRIGGER_STOP:
- pr_debug("SNDRV_PCM_TRIGGER_STOP\n");
- if (prtd->capture_start && prtd->playback_start)
- voice_ocmem_process_req(VOICE, false);
- if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
- prtd->playback_start = 0;
- else
- prtd->capture_start = 0;
- break;
- default:
- ret = -EINVAL;
- break;
- }
- return ret;
- }
- static int msm_pcm_open(struct snd_pcm_substream *substream)
- {
- struct snd_pcm_runtime *runtime = substream->runtime;
- struct voip_drv_info *prtd = &voip_info;
- int ret = 0;
- pr_debug("%s, VoIP\n", __func__);
- mutex_lock(&prtd->lock);
- runtime->hw = msm_pcm_hardware;
- ret = snd_pcm_hw_constraint_list(runtime, 0,
- SNDRV_PCM_HW_PARAM_RATE,
- &constraints_sample_rates);
- if (ret < 0)
- pr_debug("snd_pcm_hw_constraint_list failed\n");
- ret = snd_pcm_hw_constraint_integer(runtime,
- SNDRV_PCM_HW_PARAM_PERIODS);
- if (ret < 0) {
- pr_debug("snd_pcm_hw_constraint_integer failed\n");
- goto err;
- }
- if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
- prtd->playback_substream = substream;
- prtd->playback_instance++;
- } else {
- prtd->capture_substream = substream;
- prtd->capture_instance++;
- }
- runtime->private_data = prtd;
- err:
- mutex_unlock(&prtd->lock);
- return ret;
- }
- static int msm_pcm_playback_copy(struct snd_pcm_substream *substream, int a,
- snd_pcm_uframes_t hwoff, void __user *buf, snd_pcm_uframes_t frames)
- {
- int ret = 0;
- struct voip_buf_node *buf_node = NULL;
- struct snd_pcm_runtime *runtime = substream->runtime;
- struct voip_drv_info *prtd = runtime->private_data;
- unsigned long dsp_flags;
- int count = frames_to_bytes(runtime, frames);
- pr_debug("%s: count = %d, frames=%d\n", __func__, count, (int)frames);
- if (prtd->voip_reset) {
- pr_debug("%s: RESET event happened during VoIP\n", __func__);
- return -ENETRESET;
- }
- ret = wait_event_interruptible_timeout(prtd->in_wait,
- (!list_empty(&prtd->free_in_queue) ||
- prtd->state == VOIP_STOPPED),
- 1 * HZ);
- if (prtd->voip_reset) {
- pr_debug("%s: RESET event happened during VoIP\n", __func__);
- return -ENETRESET;
- }
- if (ret > 0) {
- if (count <= VOIP_MAX_VOC_PKT_SIZE) {
- spin_lock_irqsave(&prtd->dsp_lock, dsp_flags);
- buf_node =
- list_first_entry(&prtd->free_in_queue,
- struct voip_buf_node, list);
- list_del(&buf_node->list);
- spin_unlock_irqrestore(&prtd->dsp_lock, dsp_flags);
- if (prtd->mode == MODE_PCM) {
- ret = copy_from_user(&buf_node->frame.voc_pkt,
- buf, count);
- if (ret) {
- pr_err("%s: copy from user failed %d\n",
- __func__, ret);
- return -EFAULT;
- }
- buf_node->frame.pktlen = count;
- } else {
- ret = copy_from_user(&buf_node->frame,
- buf, count);
- if (ret) {
- pr_err("%s: copy from user failed %d\n",
- __func__, ret);
- return -EFAULT;
- }
- }
- spin_lock_irqsave(&prtd->dsp_lock, dsp_flags);
- list_add_tail(&buf_node->list, &prtd->in_queue);
- spin_unlock_irqrestore(&prtd->dsp_lock, dsp_flags);
- } else {
- pr_err("%s: Write cnt %d is > VOIP_MAX_VOC_PKT_SIZE\n",
- __func__, count);
- ret = -ENOMEM;
- }
- } else if (ret == 0) {
- pr_err("%s: No free DL buffs\n", __func__);
- ret = -ETIMEDOUT;
- } else {
- pr_err("%s: playback copy was interrupted %d\n", __func__, ret);
- }
- return ret;
- }
- static int msm_pcm_capture_copy(struct snd_pcm_substream *substream,
- int channel, snd_pcm_uframes_t hwoff, void __user *buf,
- snd_pcm_uframes_t frames)
- {
- int ret = 0;
- int count = 0;
- struct voip_buf_node *buf_node = NULL;
- struct snd_pcm_runtime *runtime = substream->runtime;
- struct voip_drv_info *prtd = runtime->private_data;
- unsigned long dsp_flags;
- int size;
- count = frames_to_bytes(runtime, frames);
- pr_debug("%s: count = %d\n", __func__, count);
- if (prtd->voip_reset) {
- pr_debug("%s: RESET event happened during VoIP\n", __func__);
- return -ENETRESET;
- }
- ret = wait_event_interruptible_timeout(prtd->out_wait,
- (!list_empty(&prtd->out_queue) ||
- prtd->state == VOIP_STOPPED),
- 1 * HZ);
- if (prtd->voip_reset) {
- pr_debug("%s: RESET event happened during VoIP\n", __func__);
- return -ENETRESET;
- }
- if (ret > 0) {
- if (count <= VOIP_MAX_VOC_PKT_SIZE) {
- spin_lock_irqsave(&prtd->dsp_ul_lock, dsp_flags);
- buf_node = list_first_entry(&prtd->out_queue,
- struct voip_buf_node, list);
- list_del(&buf_node->list);
- spin_unlock_irqrestore(&prtd->dsp_ul_lock, dsp_flags);
- if (prtd->mode == MODE_PCM) {
- ret = copy_to_user(buf,
- &buf_node->frame.voc_pkt,
- buf_node->frame.pktlen);
- } else {
- size = sizeof(buf_node->frame.frm_hdr) +
- sizeof(buf_node->frame.pktlen) +
- buf_node->frame.pktlen;
- ret = copy_to_user(buf,
- &buf_node->frame,
- size);
- }
- if (ret) {
- pr_err("%s: Copy to user retuned %d\n",
- __func__, ret);
- ret = -EFAULT;
- }
- spin_lock_irqsave(&prtd->dsp_ul_lock, dsp_flags);
- list_add_tail(&buf_node->list,
- &prtd->free_out_queue);
- spin_unlock_irqrestore(&prtd->dsp_ul_lock, dsp_flags);
- } else {
- pr_err("%s: Read count %d > VOIP_MAX_VOC_PKT_SIZE\n",
- __func__, count);
- ret = -ENOMEM;
- }
- } else if (ret == 0) {
- pr_err("%s: No UL data available\n", __func__);
- ret = -ETIMEDOUT;
- } else {
- pr_err("%s: Read was interrupted\n", __func__);
- ret = -ERESTARTSYS;
- }
- return ret;
- }
- static int msm_pcm_copy(struct snd_pcm_substream *substream, int a,
- snd_pcm_uframes_t hwoff, void __user *buf, snd_pcm_uframes_t frames)
- {
- int ret = 0;
- if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
- ret = msm_pcm_playback_copy(substream, a, hwoff, buf, frames);
- else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
- ret = msm_pcm_capture_copy(substream, a, hwoff, buf, frames);
- return ret;
- }
- static int msm_pcm_close(struct snd_pcm_substream *substream)
- {
- int ret = 0;
- struct list_head *ptr = NULL;
- struct list_head *next = NULL;
- struct voip_buf_node *buf_node = NULL;
- struct snd_dma_buffer *p_dma_buf, *c_dma_buf;
- struct snd_pcm_substream *p_substream, *c_substream;
- struct snd_pcm_runtime *runtime;
- struct voip_drv_info *prtd;
- unsigned long dsp_flags;
- if (substream == NULL) {
- pr_err("substream is NULL\n");
- return -EINVAL;
- }
- runtime = substream->runtime;
- prtd = runtime->private_data;
- wake_up(&prtd->out_wait);
- mutex_lock(&prtd->lock);
- if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
- prtd->playback_instance--;
- else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
- prtd->capture_instance--;
- if (!prtd->playback_instance && !prtd->capture_instance) {
- if (prtd->state == VOIP_STARTED) {
- prtd->voip_reset = false;
- prtd->state = VOIP_STOPPED;
- voc_end_voice_call(
- voc_get_session_id(VOIP_SESSION_NAME));
- voc_register_mvs_cb(NULL, NULL, NULL, prtd);
- }
- /* release all buffer */
- /* release in_queue and free_in_queue */
- pr_debug("release all buffer\n");
- p_substream = prtd->playback_substream;
- if (p_substream == NULL) {
- pr_debug("p_substream is NULL\n");
- goto capt;
- }
- p_dma_buf = &p_substream->dma_buffer;
- if (p_dma_buf == NULL) {
- pr_debug("p_dma_buf is NULL\n");
- goto capt;
- }
- if (p_dma_buf->area != NULL) {
- spin_lock_irqsave(&prtd->dsp_lock, dsp_flags);
- list_for_each_safe(ptr, next, &prtd->in_queue) {
- buf_node = list_entry(ptr,
- struct voip_buf_node, list);
- list_del(&buf_node->list);
- }
- list_for_each_safe(ptr, next, &prtd->free_in_queue) {
- buf_node = list_entry(ptr,
- struct voip_buf_node, list);
- list_del(&buf_node->list);
- }
- spin_unlock_irqrestore(&prtd->dsp_lock, dsp_flags);
- dma_free_coherent(p_substream->pcm->card->dev,
- runtime->hw.buffer_bytes_max, p_dma_buf->area,
- p_dma_buf->addr);
- p_dma_buf->area = NULL;
- }
- /* release out_queue and free_out_queue */
- capt: c_substream = prtd->capture_substream;
- if (c_substream == NULL) {
- pr_debug("c_substream is NULL\n");
- goto done;
- }
- c_dma_buf = &c_substream->dma_buffer;
- if (c_dma_buf == NULL) {
- pr_debug("c_dma_buf is NULL.\n");
- goto done;
- }
- if (c_dma_buf->area != NULL) {
- spin_lock_irqsave(&prtd->dsp_ul_lock, dsp_flags);
- list_for_each_safe(ptr, next, &prtd->out_queue) {
- buf_node = list_entry(ptr,
- struct voip_buf_node, list);
- list_del(&buf_node->list);
- }
- list_for_each_safe(ptr, next, &prtd->free_out_queue) {
- buf_node = list_entry(ptr,
- struct voip_buf_node, list);
- list_del(&buf_node->list);
- }
- spin_unlock_irqrestore(&prtd->dsp_ul_lock, dsp_flags);
- dma_free_coherent(c_substream->pcm->card->dev,
- runtime->hw.buffer_bytes_max, c_dma_buf->area,
- c_dma_buf->addr);
- c_dma_buf->area = NULL;
- }
- done:
- prtd->capture_substream = NULL;
- prtd->playback_substream = NULL;
- }
- mutex_unlock(&prtd->lock);
- return ret;
- }
- static int voip_config_vocoder(struct snd_pcm_substream *substream)
- {
- int ret = 0;
- struct snd_pcm_runtime *runtime = substream->runtime;
- struct voip_drv_info *prtd = runtime->private_data;
- uint32_t media_type = 0;
- uint32_t rate_type = 0;
- uint32_t evrc_min_rate_type = 0;
- uint32_t evrc_max_rate_type = 0;
- pr_info("%s(): mode=%d, playback sample rate=%d, capture sample rate=%d\n",
- __func__, prtd->mode, prtd->play_samp_rate, prtd->cap_samp_rate);
- if ((runtime->format != FORMAT_S16_LE &&
- runtime->format != FORMAT_SPECIAL) &&
- ((prtd->mode == MODE_AMR) || (prtd->mode == MODE_AMR_WB) ||
- (prtd->mode == MODE_IS127) || (prtd->mode == MODE_4GV_NB) ||
- (prtd->mode == MODE_4GV_WB) || (prtd->mode == MODE_4GV_NW) ||
- (prtd->mode == MODE_G711) || (prtd->mode == MODE_G711A))) {
- pr_err("%s(): mode:%d and format:%u are not matched\n",
- __func__, prtd->mode, (uint32_t)runtime->format);
- ret = -EINVAL;
- goto done;
- }
- if (runtime->format != FORMAT_S16_LE && (prtd->mode == MODE_PCM)) {
- pr_err("%s(): mode:%d and format:%u are not matched\n",
- __func__, prtd->mode, runtime->format);
- ret = -EINVAL;
- goto done;
- }
- if ((prtd->mode == MODE_PCM) ||
- (prtd->mode == MODE_AMR) ||
- (prtd->mode == MODE_AMR_WB) ||
- (prtd->mode == MODE_G711) ||
- (prtd->mode == MODE_G711A)) {
- ret = voip_get_rate_type(prtd->mode,
- prtd->rate,
- &rate_type);
- if (ret < 0) {
- pr_err("%s(): fail at getting rate_type, ret=%d\n",
- __func__, ret);
- ret = -EINVAL;
- goto done;
- }
- prtd->rate_type = rate_type;
- pr_debug("rate_type=%d\n", rate_type);
- } else if ((prtd->mode == MODE_IS127) ||
- (prtd->mode == MODE_4GV_NB) ||
- (prtd->mode == MODE_4GV_WB) ||
- (prtd->mode == MODE_4GV_NW)) {
- ret = voip_get_rate_type(prtd->mode,
- prtd->evrc_min_rate,
- &evrc_min_rate_type);
- if (ret < 0) {
- pr_err("%s(): fail at getting min rate, ret=%d\n",
- __func__, ret);
- ret = -EINVAL;
- goto done;
- }
- if (evrc_min_rate_type == VOC_0_RATE)
- evrc_min_rate_type = VOC_8_RATE;
- ret = voip_get_rate_type(prtd->mode,
- prtd->evrc_max_rate,
- &evrc_max_rate_type);
- if (ret < 0) {
- pr_err("%s(): fail at getting max rate, ret=%d\n",
- __func__, ret);
- ret = -EINVAL;
- goto done;
- }
- if (evrc_max_rate_type == VOC_0_RATE)
- evrc_max_rate_type = VOC_1_RATE;
- if (evrc_max_rate_type < evrc_min_rate_type) {
- pr_err("%s(): Invalid EVRC min max rates: %d, %d\n",
- __func__, evrc_min_rate_type,
- evrc_max_rate_type);
- ret = -EINVAL;
- goto done;
- }
- pr_debug("%s(): min rate=%d, max rate=%d\n",
- __func__, evrc_min_rate_type, evrc_max_rate_type);
- }
- ret = voip_get_media_type(prtd->mode,
- prtd->rate_type,
- prtd->play_samp_rate,
- &media_type);
- if (ret < 0) {
- pr_err("%s(): fail at getting media_type, ret=%d\n",
- __func__, ret);
- ret = -EINVAL;
- goto done;
- }
- pr_debug("%s(): media_type=%d\n", __func__, media_type);
- if ((prtd->play_samp_rate == 8000) &&
- (prtd->cap_samp_rate == 8000))
- voc_config_vocoder(media_type, rate_type,
- VSS_NETWORK_ID_VOIP_NB,
- voip_info.dtx_mode,
- evrc_min_rate_type,
- evrc_max_rate_type);
- else if ((prtd->play_samp_rate == 16000) &&
- (prtd->cap_samp_rate == 16000))
- voc_config_vocoder(media_type, rate_type,
- VSS_NETWORK_ID_VOIP_WB,
- voip_info.dtx_mode,
- evrc_min_rate_type,
- evrc_max_rate_type);
- else {
- if((prtd->play_samp_rate != 0) &&
- (prtd->cap_samp_rate == 0) )
- pr_info("%s: TX setting is not complete ", __func__);
- else
- pr_err("%s: Invalid rate playback %d, capture %d\n",
- __func__, prtd->play_samp_rate,
- prtd->cap_samp_rate);
- ret = -EINVAL;
- }
- done:
- return ret;
- }
- static int msm_pcm_prepare(struct snd_pcm_substream *substream)
- {
- int ret = 0;
- struct snd_pcm_runtime *runtime = substream->runtime;
- struct voip_drv_info *prtd = runtime->private_data;
- mutex_lock(&prtd->lock);
- if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
- ret = msm_pcm_playback_prepare(substream);
- else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
- ret = msm_pcm_capture_prepare(substream);
- if (prtd->playback_instance && prtd->capture_instance
- && (prtd->state != VOIP_STARTED)) {
- ret = voip_config_vocoder(substream);
- if (ret < 0) {
- pr_err("%s(): fail at configuring vocoder for voip, ret=%d\n",
- __func__, ret);
- goto done;
- }
- /* Initialaizing cb variables */
- voc_register_mvs_cb(voip_process_ul_pkt,
- voip_process_dl_pkt,
- voip_ssr_cb_fn, prtd);
- ret = voc_start_voice_call(
- voc_get_session_id(VOIP_SESSION_NAME));
- if (ret < 0) {
- pr_err("%s: voc_start_voice_call() failed err %d",
- __func__, ret);
- goto done;
- }
- prtd->state = VOIP_STARTED;
- }
- done:
- mutex_unlock(&prtd->lock);
- return ret;
- }
- static snd_pcm_uframes_t
- msm_pcm_playback_pointer(struct snd_pcm_substream *substream)
- {
- struct snd_pcm_runtime *runtime = substream->runtime;
- struct voip_drv_info *prtd = runtime->private_data;
- pr_debug("%s\n", __func__);
- if (prtd->pcm_playback_irq_pos >= prtd->pcm_size)
- prtd->pcm_playback_irq_pos = 0;
- return bytes_to_frames(runtime, (prtd->pcm_playback_irq_pos));
- }
- static snd_pcm_uframes_t
- msm_pcm_capture_pointer(struct snd_pcm_substream *substream)
- {
- struct snd_pcm_runtime *runtime = substream->runtime;
- struct voip_drv_info *prtd = runtime->private_data;
- if (prtd->pcm_capture_irq_pos >= prtd->pcm_capture_size)
- prtd->pcm_capture_irq_pos = 0;
- return bytes_to_frames(runtime, (prtd->pcm_capture_irq_pos));
- }
- static snd_pcm_uframes_t msm_pcm_pointer(struct snd_pcm_substream *substream)
- {
- snd_pcm_uframes_t ret = 0;
- pr_debug("%s\n", __func__);
- if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
- ret = msm_pcm_playback_pointer(substream);
- else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
- ret = msm_pcm_capture_pointer(substream);
- return ret;
- }
- static int msm_pcm_mmap(struct snd_pcm_substream *substream,
- struct vm_area_struct *vma)
- {
- struct snd_pcm_runtime *runtime = substream->runtime;
- pr_debug("%s\n", __func__);
- dma_mmap_coherent(substream->pcm->card->dev, vma,
- runtime->dma_area,
- runtime->dma_addr,
- runtime->dma_bytes);
- return 0;
- }
- static int msm_pcm_hw_params(struct snd_pcm_substream *substream,
- struct snd_pcm_hw_params *params)
- {
- struct snd_pcm_runtime *runtime = substream->runtime;
- struct snd_dma_buffer *dma_buf = &substream->dma_buffer;
- struct voip_buf_node *buf_node = NULL;
- int i = 0, offset = 0;
- int periods = VOIP_MIN_Q_LEN;
- pr_debug("%s: voip\n", __func__);
- mutex_lock(&voip_info.lock);
- periods = params_periods(params);
- pr_info("%s: periods = %d\n", __func__, periods);
- runtime->hw.buffer_bytes_max = sizeof(struct voip_buf_node) * periods;
- dma_buf->dev.type = SNDRV_DMA_TYPE_DEV;
- dma_buf->dev.dev = substream->pcm->card->dev;
- dma_buf->private_data = NULL;
- dma_buf->area = dma_alloc_coherent(substream->pcm->card->dev,
- runtime->hw.buffer_bytes_max,
- &dma_buf->addr, GFP_KERNEL);
- if (!dma_buf->area) {
- pr_err("%s:MSM VOIP dma_alloc failed\n", __func__);
- mutex_unlock(&voip_info.lock);
- return -ENOMEM;
- }
- dma_buf->bytes = runtime->hw.buffer_bytes_max;
- memset(dma_buf->area, 0, runtime->hw.buffer_bytes_max);
- if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
- for (i = 0; i < periods; i++) {
- buf_node = (void *)dma_buf->area + offset;
- list_add_tail(&buf_node->list,
- &voip_info.free_in_queue);
- offset = offset + sizeof(struct voip_buf_node);
- }
- } else {
- for (i = 0; i < periods; i++) {
- buf_node = (void *) dma_buf->area + offset;
- list_add_tail(&buf_node->list,
- &voip_info.free_out_queue);
- offset = offset + sizeof(struct voip_buf_node);
- }
- }
- mutex_unlock(&voip_info.lock);
- snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
- return 0;
- }
- static int msm_voip_mode_config_get(struct snd_kcontrol *kcontrol,
- struct snd_ctl_elem_value *ucontrol)
- {
- mutex_lock(&voip_info.lock);
- ucontrol->value.integer.value[0] = voip_info.mode;
- mutex_unlock(&voip_info.lock);
- return 0;
- }
- static int msm_voip_mode_config_put(struct snd_kcontrol *kcontrol,
- struct snd_ctl_elem_value *ucontrol)
- {
- mutex_lock(&voip_info.lock);
- voip_info.mode = ucontrol->value.integer.value[0];
- pr_debug("%s: mode=%d\n", __func__, voip_info.mode);
- mutex_unlock(&voip_info.lock);
- return 0;
- }
- static int msm_voip_rate_config_put(struct snd_kcontrol *kcontrol,
- struct snd_ctl_elem_value *ucontrol)
- {
- int ret = 0;
- int rate = ucontrol->value.integer.value[0];
- mutex_lock(&voip_info.lock);
- if (voip_info.rate != rate) {
- voip_info.rate = rate;
- pr_debug("%s: rate=%d\n", __func__, voip_info.rate);
- if (voip_info.state == VOIP_STARTED &&
- (voip_info.mode == MODE_AMR ||
- voip_info.mode == MODE_AMR_WB)) {
- ret = voip_config_vocoder(
- voip_info.capture_substream);
- if (ret) {
- pr_err("%s:Failed to configure vocoder, ret=%d\n",
- __func__, ret);
- goto done;
- }
- ret = voc_update_amr_vocoder_rate(
- voc_get_session_id(VOIP_SESSION_NAME));
- if (ret) {
- pr_err("%s:Failed to update AMR rate, ret=%d\n",
- __func__, ret);
- }
- }
- }
- done:
- mutex_unlock(&voip_info.lock);
- return ret;
- }
- static int msm_voip_evrc_min_max_rate_config_get(struct snd_kcontrol *kcontrol,
- struct snd_ctl_elem_value *ucontrol)
- {
- mutex_lock(&voip_info.lock);
- ucontrol->value.integer.value[0] = voip_info.evrc_min_rate;
- ucontrol->value.integer.value[1] = voip_info.evrc_max_rate;
- mutex_unlock(&voip_info.lock);
- return 0;
- }
- static int msm_voip_evrc_min_max_rate_config_put(struct snd_kcontrol *kcontrol,
- struct snd_ctl_elem_value *ucontrol)
- {
- mutex_lock(&voip_info.lock);
- voip_info.evrc_min_rate = ucontrol->value.integer.value[0];
- voip_info.evrc_max_rate = ucontrol->value.integer.value[1];
- pr_debug("%s(): evrc_min_rate=%d,evrc_max_rate=%d\n", __func__,
- voip_info.evrc_min_rate, voip_info.evrc_max_rate);
- mutex_unlock(&voip_info.lock);
- return 0;
- }
- static int voip_get_rate_type(uint32_t mode, uint32_t rate,
- uint32_t *rate_type)
- {
- int ret = 0;
- switch (mode) {
- case MODE_AMR: {
- switch (rate) {
- case 4750:
- *rate_type = AMR_RATE_4750;
- break;
- case 5150:
- *rate_type = AMR_RATE_5150;
- break;
- case 5900:
- *rate_type = AMR_RATE_5900;
- break;
- case 6700:
- *rate_type = AMR_RATE_6700;
- break;
- case 7400:
- *rate_type = AMR_RATE_7400;
- break;
- case 7950:
- *rate_type = AMR_RATE_7950;
- break;
- case 10200:
- *rate_type = AMR_RATE_10200;
- break;
- case 12200:
- *rate_type = AMR_RATE_12200;
- break;
- default:
- pr_err("wrong rate for AMR NB.\n");
- ret = -EINVAL;
- break;
- }
- break;
- }
- case MODE_AMR_WB: {
- switch (rate) {
- case 6600:
- *rate_type = AMR_RATE_6600 - AMR_RATE_6600;
- break;
- case 8850:
- *rate_type = AMR_RATE_8850 - AMR_RATE_6600;
- break;
- case 12650:
- *rate_type = AMR_RATE_12650 - AMR_RATE_6600;
- break;
- case 14250:
- *rate_type = AMR_RATE_14250 - AMR_RATE_6600;
- break;
- case 15850:
- *rate_type = AMR_RATE_15850 - AMR_RATE_6600;
- break;
- case 18250:
- *rate_type = AMR_RATE_18250 - AMR_RATE_6600;
- break;
- case 19850:
- *rate_type = AMR_RATE_19850 - AMR_RATE_6600;
- break;
- case 23050:
- *rate_type = AMR_RATE_23050 - AMR_RATE_6600;
- break;
- case 23850:
- *rate_type = AMR_RATE_23850 - AMR_RATE_6600;
- break;
- default:
- pr_err("wrong rate for AMR_WB.\n");
- ret = -EINVAL;
- break;
- }
- break;
- }
- case MODE_PCM: {
- *rate_type = 0;
- break;
- }
- case MODE_IS127:
- case MODE_4GV_NB:
- case MODE_4GV_WB: {
- switch (rate) {
- case VOC_0_RATE:
- case VOC_8_RATE:
- case VOC_4_RATE:
- case VOC_2_RATE:
- case VOC_1_RATE:
- *rate_type = rate;
- break;
- default:
- pr_err("wrong rate for IS127/4GV_NB/WB.\n");
- ret = -EINVAL;
- break;
- }
- break;
- }
- case MODE_4GV_NW: {
- switch (rate) {
- case VOC_0_RATE:
- case VOC_8_RATE:
- case VOC_4_RATE:
- case VOC_2_RATE:
- case VOC_1_RATE:
- case VOC_8_RATE_NC:
- *rate_type = rate;
- break;
- default:
- pr_err("wrong rate for 4GV_NW.\n");
- ret = -EINVAL;
- break;
- }
- break;
- }
- case MODE_G711:
- case MODE_G711A:
- *rate_type = rate;
- break;
- default:
- pr_err("wrong mode type.\n");
- ret = -EINVAL;
- }
- pr_debug("%s, mode=%d, rate=%u, rate_type=%d\n",
- __func__, mode, rate, *rate_type);
- return ret;
- }
- static int voip_get_media_type(uint32_t mode, uint32_t rate_type,
- unsigned int samp_rate,
- unsigned int *media_type)
- {
- int ret = 0;
- pr_debug("%s: mode=%d, samp_rate=%d\n", __func__,
- mode, samp_rate);
- switch (mode) {
- case MODE_AMR:
- *media_type = VSS_MEDIA_ID_AMR_NB_MODEM;
- break;
- case MODE_AMR_WB:
- *media_type = VSS_MEDIA_ID_AMR_WB_MODEM;
- break;
- case MODE_PCM:
- if (samp_rate == 8000)
- *media_type = VSS_MEDIA_ID_PCM_NB;
- else
- *media_type = VSS_MEDIA_ID_PCM_WB;
- break;
- case MODE_IS127: /* EVRC-A */
- *media_type = VSS_MEDIA_ID_EVRC_MODEM;
- break;
- case MODE_4GV_NB: /* EVRC-B */
- *media_type = VSS_MEDIA_ID_4GV_NB_MODEM;
- break;
- case MODE_4GV_WB: /* EVRC-WB */
- *media_type = VSS_MEDIA_ID_4GV_WB_MODEM;
- break;
- case MODE_4GV_NW: /* EVRC-NW */
- *media_type = VSS_MEDIA_ID_4GV_NW_MODEM;
- break;
- case MODE_G711:
- case MODE_G711A:
- if (rate_type == MVS_G711A_MODE_MULAW)
- *media_type = VSS_MEDIA_ID_G711_MULAW;
- else
- *media_type = VSS_MEDIA_ID_G711_ALAW;
- break;
- default:
- pr_debug(" input mode is not supported\n");
- ret = -EINVAL;
- }
- pr_debug("%s: media_type is 0x%x\n", __func__, *media_type);
- return ret;
- }
- static struct snd_pcm_ops msm_pcm_ops = {
- .open = msm_pcm_open,
- .copy = msm_pcm_copy,
- .hw_params = msm_pcm_hw_params,
- .close = msm_pcm_close,
- .prepare = msm_pcm_prepare,
- .trigger = msm_pcm_trigger,
- .pointer = msm_pcm_pointer,
- .mmap = msm_pcm_mmap,
- };
- static int msm_asoc_pcm_new(struct snd_soc_pcm_runtime *rtd)
- {
- struct snd_card *card = rtd->card->snd_card;
- int ret = 0;
- pr_debug("msm_asoc_pcm_new\n");
- if (!card->dev->coherent_dma_mask)
- card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
- return ret;
- }
- static struct snd_soc_platform_driver msm_soc_platform = {
- .ops = &msm_pcm_ops,
- .pcm_new = msm_asoc_pcm_new,
- .probe = msm_pcm_voip_probe,
- };
- static __devinit int msm_pcm_probe(struct platform_device *pdev)
- {
- int rc;
- if (!is_voc_initialized()) {
- pr_debug("%s: voice module not initialized yet, deferring probe()\n",
- __func__);
- rc = -EPROBE_DEFER;
- goto done;
- }
- rc = voc_alloc_cal_shared_memory();
- if (rc == -EPROBE_DEFER) {
- pr_debug("%s: memory allocation for calibration deferred %d\n",
- __func__, rc);
- goto done;
- } else if (rc < 0) {
- pr_err("%s: memory allocation for calibration failed %d\n",
- __func__, rc);
- }
- rc = voc_alloc_voip_shared_memory();
- if (rc < 0) {
- pr_err("%s: error allocating shared mem err %d\n",
- __func__, rc);
- }
- if (pdev->dev.of_node)
- dev_set_name(&pdev->dev, "%s", "msm-voip-dsp");
- pr_debug("%s: dev name %s\n", __func__, dev_name(&pdev->dev));
- rc = snd_soc_register_platform(&pdev->dev,
- &msm_soc_platform);
- done:
- return rc;
- }
- static int msm_pcm_remove(struct platform_device *pdev)
- {
- snd_soc_unregister_platform(&pdev->dev);
- return 0;
- }
- static const struct of_device_id msm_voip_dt_match[] = {
- {.compatible = "qcom,msm-voip-dsp"},
- {}
- };
- MODULE_DEVICE_TABLE(of, msm_voip_dt_match);
- static struct platform_driver msm_pcm_driver = {
- .driver = {
- .name = "msm-voip-dsp",
- .owner = THIS_MODULE,
- .of_match_table = msm_voip_dt_match,
- },
- .probe = msm_pcm_probe,
- .remove = __devexit_p(msm_pcm_remove),
- };
- static int __init msm_soc_platform_init(void)
- {
- memset(&voip_info, 0, sizeof(voip_info));
- voip_info.mode = MODE_PCM;
- mutex_init(&voip_info.lock);
- spin_lock_init(&voip_info.dsp_lock);
- spin_lock_init(&voip_info.dsp_ul_lock);
- init_waitqueue_head(&voip_info.out_wait);
- init_waitqueue_head(&voip_info.in_wait);
- INIT_LIST_HEAD(&voip_info.in_queue);
- INIT_LIST_HEAD(&voip_info.free_in_queue);
- INIT_LIST_HEAD(&voip_info.out_queue);
- INIT_LIST_HEAD(&voip_info.free_out_queue);
- return platform_driver_register(&msm_pcm_driver);
- }
- module_init(msm_soc_platform_init);
- static void __exit msm_soc_platform_exit(void)
- {
- platform_driver_unregister(&msm_pcm_driver);
- }
- module_exit(msm_soc_platform_exit);
- MODULE_DESCRIPTION("PCM module platform driver");
- MODULE_LICENSE("GPL v2");
|